1 /* src/p80211/p80211conv.c
3 * Ether/802.11 conversions and packet buffer routines
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
6 * --------------------------------------------------------------------
10 * The contents of this file are subject to the Mozilla Public
11 * License Version 1.1 (the "License"); you may not use this file
12 * except in compliance with the License. You may obtain a copy of
13 * the License at http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS
16 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
20 * Alternatively, the contents of this file may be used under the
21 * terms of the GNU Public License version 2 (the "GPL"), in which
22 * case the provisions of the GPL are applicable instead of the
23 * above. If you wish to allow the use of your version of this file
24 * only under the terms of the GPL and not to allow others to use
25 * your version of this file under the MPL, indicate your decision
26 * by deleting the provisions above and replace them with the notice
27 * and other provisions required by the GPL. If you do not delete
28 * the provisions above, a recipient may use your version of this
29 * file under either the MPL or the GPL.
31 * --------------------------------------------------------------------
33 * Inquiries regarding the linux-wlan Open Source project can be
36 * AbsoluteValue Systems Inc.
38 * http://www.linux-wlan.com
40 * --------------------------------------------------------------------
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
45 * --------------------------------------------------------------------
47 * This file defines the functions that perform Ethernet to/from
48 * 802.11 frame conversions.
50 * --------------------------------------------------------------------
52 /*================================================================*/
55 #define __NO_VERSION__ /* prevent the static definition */
58 #include <linux/version.h>
60 #include <linux/module.h>
61 #include <linux/kernel.h>
62 #include <linux/sched.h>
63 #include <linux/types.h>
64 #include <linux/skbuff.h>
65 #include <linux/slab.h>
66 #include <linux/wireless.h>
67 #include <linux/netdevice.h>
68 #include <linux/etherdevice.h>
69 #include <linux/if_ether.h>
71 #include <asm/byteorder.h>
73 #include "wlan_compat.h"
75 /*================================================================*/
76 /* Project Includes */
78 #include "p80211types.h"
79 #include "p80211hdr.h"
80 #include "p80211conv.h"
81 #include "p80211mgmt.h"
82 #include "p80211msg.h"
83 #include "p80211netdev.h"
84 #include "p80211ioctl.h"
85 #include "p80211req.h"
88 /*================================================================*/
91 /*================================================================*/
95 /*================================================================*/
99 /*================================================================*/
100 /* Local Static Definitions */
102 static UINT8 oui_rfc1042[] = {0x00, 0x00, 0x00};
103 static UINT8 oui_8021h[] = {0x00, 0x00, 0xf8};
105 /*================================================================*/
106 /* Local Function Declarations */
109 /*================================================================*/
110 /* Function Definitions */
112 /*----------------------------------------------------------------
113 * p80211pb_ether_to_80211
115 * Uses the contents of the ether frame and the etherconv setting
116 * to build the elements of the 802.11 frame.
118 * We don't actually set
119 * up the frame header here. That's the MAC's job. We're only handling
120 * conversion of DIXII or 802.3+LLC frames to something that works
123 * Note -- 802.11 header is NOT part of the skb. Likewise, the 802.11
124 * FCS is also not present and will need to be added elsewhere.
127 * ethconv Conversion type to perform
128 * skb skbuff containing the ether frame
129 * p80211_hdr 802.11 header
132 * 0 on success, non-zero otherwise
135 * May be called in interrupt or non-interrupt context
136 ----------------------------------------------------------------*/
137 int skb_ether_to_p80211( wlandevice_t *wlandev, UINT32 ethconv, struct sk_buff *skb, p80211_hdr_t *p80211_hdr, p80211_metawep_t *p80211_wep)
148 memcpy(&e_hdr, skb->data, sizeof(e_hdr));
151 WLAN_LOG_DEBUG(1, "zero-length skb!\n");
155 if ( ethconv == WLAN_ETHCONV_ENCAP ) { /* simplest case */
156 WLAN_LOG_DEBUG(3, "ENCAP len: %d\n", skb->len);
157 /* here, we don't care what kind of ether frm. Just stick it */
158 /* in the 80211 payload */
159 /* which is to say, leave the skb alone. */
161 /* step 1: classify ether frame, DIX or 802.3? */
162 proto = ntohs(e_hdr.type);
163 if ( proto <= 1500 ) {
164 WLAN_LOG_DEBUG(3, "802.3 len: %d\n", skb->len);
165 /* codes <= 1500 reserved for 802.3 lengths */
166 /* it's 802.3, pass ether payload unchanged, */
168 /* trim off ethernet header */
169 skb_pull(skb, WLAN_ETHHDR_LEN);
171 /* leave off any PAD octets. */
172 skb_trim(skb, proto);
174 WLAN_LOG_DEBUG(3, "DIXII len: %d\n", skb->len);
175 /* it's DIXII, time for some conversion */
177 /* trim off ethernet header */
178 skb_pull(skb, WLAN_ETHHDR_LEN);
181 e_snap = (wlan_snap_t *) skb_push(skb, sizeof(wlan_snap_t));
182 e_snap->type = htons(proto);
183 if ( ethconv == WLAN_ETHCONV_8021h && p80211_stt_findproto(proto) ) {
184 memcpy( e_snap->oui, oui_8021h, WLAN_IEEE_OUI_LEN);
186 memcpy( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN);
190 e_llc = (wlan_llc_t *) skb_push(skb, sizeof(wlan_llc_t));
191 e_llc->dsap = 0xAA; /* SNAP, see IEEE 802 */
198 /* Set up the 802.11 header */
199 /* It's a data frame */
200 fc = host2ieee16( WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
201 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
203 switch ( wlandev->macmode ) {
204 case WLAN_MACMODE_IBSS_STA:
205 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, WLAN_ADDR_LEN);
206 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, WLAN_ADDR_LEN);
207 memcpy(p80211_hdr->a3.a3, wlandev->bssid, WLAN_ADDR_LEN);
209 case WLAN_MACMODE_ESS_STA:
210 fc |= host2ieee16(WLAN_SET_FC_TODS(1));
211 memcpy(p80211_hdr->a3.a1, wlandev->bssid, WLAN_ADDR_LEN);
212 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, WLAN_ADDR_LEN);
213 memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, WLAN_ADDR_LEN);
215 case WLAN_MACMODE_ESS_AP:
216 fc |= host2ieee16(WLAN_SET_FC_FROMDS(1));
217 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, WLAN_ADDR_LEN);
218 memcpy(p80211_hdr->a3.a2, wlandev->bssid, WLAN_ADDR_LEN);
219 memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, WLAN_ADDR_LEN);
222 WLAN_LOG_ERROR("Error: Converting eth to wlan in unknown mode.\n");
227 p80211_wep->data = NULL;
229 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
230 // XXXX need to pick keynum other than default?
233 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
235 p80211_wep->data = skb->data;
238 if ((foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
240 (wlandev->hostwep & HOSTWEP_DEFAULTKEY_MASK),
241 p80211_wep->iv, p80211_wep->icv))) {
242 WLAN_LOG_WARNING("Host en-WEP failed, dropping frame (%d).\n", foo);
245 fc |= host2ieee16(WLAN_SET_FC_ISWEP(1));
249 // skb->nh.raw = skb->data;
251 p80211_hdr->a3.fc = fc;
252 p80211_hdr->a3.dur = 0;
253 p80211_hdr->a3.seq = 0;
259 /* jkriegl: from orinoco, modified */
260 static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
261 p80211_rxmeta_t *rxmeta)
265 /* Gather wireless spy statistics: for each packet, compare the
266 * source address with out list, and if match, get the stats... */
268 for (i = 0; i < wlandev->spy_number; i++) {
270 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
271 memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
272 wlandev->spy_stat[i].level = rxmeta->signal;
273 wlandev->spy_stat[i].noise = rxmeta->noise;
274 wlandev->spy_stat[i].qual = (rxmeta->signal > rxmeta->noise) ? \
275 (rxmeta->signal - rxmeta->noise) : 0;
276 wlandev->spy_stat[i].updated = 0x7;
281 /*----------------------------------------------------------------
282 * p80211pb_80211_to_ether
284 * Uses the contents of a received 802.11 frame and the etherconv
285 * setting to build an ether frame.
287 * This function extracts the src and dest address from the 802.11
288 * frame to use in the construction of the eth frame.
291 * ethconv Conversion type to perform
292 * skb Packet buffer containing the 802.11 frame
295 * 0 on success, non-zero otherwise
298 * May be called in interrupt or non-interrupt context
299 ----------------------------------------------------------------*/
300 int skb_p80211_to_ether( wlandevice_t *wlandev, UINT32 ethconv, struct sk_buff *skb)
302 netdevice_t *netdev = wlandev->netdev;
306 UINT8 daddr[WLAN_ETHADDR_LEN];
307 UINT8 saddr[WLAN_ETHADDR_LEN];
309 wlan_ethhdr_t *e_hdr;
317 payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
318 payload_offset = WLAN_HDR_A3_LEN;
320 w_hdr = (p80211_hdr_t *) skb->data;
322 /* setup some vars for convenience */
323 fc = ieee2host16(w_hdr->a3.fc);
324 if ( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
325 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
326 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
327 } else if( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 1) ) {
328 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
329 memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
330 } else if( (WLAN_GET_FC_TODS(fc) == 1) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
331 memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
332 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
334 payload_offset = WLAN_HDR_A4_LEN;
335 payload_length -= ( WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN );
336 if (payload_length < 0 ) {
337 WLAN_LOG_ERROR("A4 frame too short!\n");
340 memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
341 memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
344 /* perform de-wep if necessary.. */
345 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && WLAN_GET_FC_ISWEP(fc) && (wlandev->hostwep & HOSTWEP_DECRYPT)) {
346 if (payload_length <= 8) {
347 WLAN_LOG_ERROR("WEP frame too short (%u).\n",
351 if ((foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
352 payload_length - 8, -1,
353 skb->data + payload_offset,
354 skb->data + payload_offset + payload_length - 4))) {
355 /* de-wep failed, drop skb. */
356 WLAN_LOG_DEBUG(1, "Host de-WEP failed, dropping frame (%d).\n", foo);
357 wlandev->rx.decrypt_err++;
361 /* subtract the IV+ICV length off the payload */
363 /* chop off the IV */
365 /* chop off the ICV. */
366 skb_trim(skb, skb->len - 4);
368 wlandev->rx.decrypt++;
371 e_hdr = (wlan_ethhdr_t *) (skb->data + payload_offset);
373 e_llc = (wlan_llc_t *) (skb->data + payload_offset);
374 e_snap = (wlan_snap_t *) (skb->data + payload_offset + sizeof(wlan_llc_t));
376 /* Test for the various encodings */
377 if ( (payload_length >= sizeof(wlan_ethhdr_t)) &&
378 ( e_llc->dsap != 0xaa || e_llc->ssap != 0xaa ) &&
379 ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
380 (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
381 WLAN_LOG_DEBUG(3, "802.3 ENCAP len: %d\n", payload_length);
382 /* 802.3 Encapsulated */
384 /* Chop off the 802.11 header. it's already sane. */
385 skb_pull(skb, payload_offset);
386 /* chop off the 802.11 CRC */
387 skb_trim(skb, skb->len - WLAN_CRC_LEN);
389 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
390 (e_llc->dsap == 0xaa) &&
391 (e_llc->ssap == 0xaa) &&
392 (e_llc->ctl == 0x03) &&
393 (((memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)==0) &&
394 (ethconv == WLAN_ETHCONV_8021h) &&
395 (p80211_stt_findproto(ieee2host16(e_snap->type)))) ||
396 (memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)!=0)))
398 WLAN_LOG_DEBUG(3, "SNAP+RFC1042 len: %d\n", payload_length);
399 /* it's a SNAP + RFC1042 frame && protocol is in STT */
400 /* build 802.3 + RFC1042 */
402 /* chop 802.11 header from skb. */
403 skb_pull(skb, payload_offset);
405 /* create 802.3 header at beginning of skb. */
406 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
407 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
408 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
409 e_hdr->type = htons(payload_length);
411 /* chop off the 802.11 CRC */
412 skb_trim(skb, skb->len - WLAN_CRC_LEN);
414 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
415 (e_llc->dsap == 0xaa) &&
416 (e_llc->ssap == 0xaa) &&
417 (e_llc->ctl == 0x03) ) {
418 WLAN_LOG_DEBUG(3, "802.1h/RFC1042 len: %d\n", payload_length);
419 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
420 /* build a DIXII + RFC894 */
422 /* chop 802.11 header from skb. */
423 skb_pull(skb, payload_offset);
425 /* chop llc header from skb. */
426 skb_pull(skb, sizeof(wlan_llc_t));
428 /* chop snap header from skb. */
429 skb_pull(skb, sizeof(wlan_snap_t));
431 /* create 802.3 header at beginning of skb. */
432 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
433 e_hdr->type = e_snap->type;
434 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
435 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
437 /* chop off the 802.11 CRC */
438 skb_trim(skb, skb->len - WLAN_CRC_LEN);
440 WLAN_LOG_DEBUG(3, "NON-ENCAP len: %d\n", payload_length);
442 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
443 /* build an 802.3 frame */
444 /* allocate space and setup hostbuf */
446 /* Chop off the 802.11 header. */
447 skb_pull(skb, payload_offset);
449 /* create 802.3 header at beginning of skb. */
450 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
451 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
452 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
453 e_hdr->type = htons(payload_length);
455 /* chop off the 802.11 CRC */
456 skb_trim(skb, skb->len - WLAN_CRC_LEN);
460 skb->protocol = eth_type_trans(skb, netdev);
461 skb_reset_mac_header(skb);
463 /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
464 /* jkriegl: only process signal/noise if requested by iwspy */
465 if (wlandev->spy_number)
466 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source, P80211SKB_RXMETA(skb));
468 /* Free the metadata */
469 p80211skb_rxmeta_detach(skb);
475 /*----------------------------------------------------------------
476 * p80211_stt_findproto
478 * Searches the 802.1h Selective Translation Table for a given
482 * proto protocl number (in host order) to search for.
485 * 1 - if the table is empty or a match is found.
486 * 0 - if the table is non-empty and a match is not found.
489 * May be called in interrupt or non-interrupt context
490 ----------------------------------------------------------------*/
491 int p80211_stt_findproto(UINT16 proto)
493 /* Always return found for now. This is the behavior used by the */
494 /* Zoom Win95 driver when 802.1h mode is selected */
495 /* TODO: If necessary, add an actual search we'll probably
496 need this to match the CMAC's way of doing things.
497 Need to do some testing to confirm.
500 if (proto == 0x80f3) /* APPLETALK */
506 /*----------------------------------------------------------------
507 * p80211skb_rxmeta_detach
509 * Disconnects the frmmeta and rxmeta from an skb.
512 * wlandev The wlandev this skb belongs to.
513 * skb The skb we're attaching to.
516 * 0 on success, non-zero otherwise
519 * May be called in interrupt or non-interrupt context
520 ----------------------------------------------------------------*/
522 p80211skb_rxmeta_detach(struct sk_buff *skb)
524 p80211_rxmeta_t *rxmeta;
525 p80211_frmmeta_t *frmmeta;
529 if ( skb==NULL ) { /* bad skb */
530 WLAN_LOG_DEBUG(1, "Called w/ null skb.\n");
533 frmmeta = P80211SKB_FRMMETA(skb);
534 if ( frmmeta == NULL ) { /* no magic */
535 WLAN_LOG_DEBUG(1, "Called w/ bad frmmeta magic.\n");
538 rxmeta = frmmeta->rx;
539 if ( rxmeta == NULL ) { /* bad meta ptr */
540 WLAN_LOG_DEBUG(1, "Called w/ bad rxmeta ptr.\n");
548 memset(skb->cb, 0, sizeof(skb->cb));
554 /*----------------------------------------------------------------
555 * p80211skb_rxmeta_attach
557 * Allocates a p80211rxmeta structure, initializes it, and attaches
561 * wlandev The wlandev this skb belongs to.
562 * skb The skb we're attaching to.
565 * 0 on success, non-zero otherwise
568 * May be called in interrupt or non-interrupt context
569 ----------------------------------------------------------------*/
571 p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
574 p80211_rxmeta_t *rxmeta;
575 p80211_frmmeta_t *frmmeta;
579 /* If these already have metadata, we error out! */
580 if (P80211SKB_RXMETA(skb) != NULL) {
581 WLAN_LOG_ERROR("%s: RXmeta already attached!\n",
587 /* Allocate the rxmeta */
588 rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
590 if ( rxmeta == NULL ) {
591 WLAN_LOG_ERROR("%s: Failed to allocate rxmeta.\n",
597 /* Initialize the rxmeta */
598 memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
599 rxmeta->wlandev = wlandev;
600 rxmeta->hosttime = jiffies;
602 /* Overlay a frmmeta_t onto skb->cb */
603 memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
604 frmmeta = (p80211_frmmeta_t*)(skb->cb);
605 frmmeta->magic = P80211_FRMMETA_MAGIC;
606 frmmeta->rx = rxmeta;
612 /*----------------------------------------------------------------
615 * Frees an entire p80211skb by checking and freeing the meta struct
616 * and then freeing the skb.
619 * wlandev The wlandev this skb belongs to.
620 * skb The skb we're attaching to.
623 * 0 on success, non-zero otherwise
626 * May be called in interrupt or non-interrupt context
627 ----------------------------------------------------------------*/
629 p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
631 p80211_frmmeta_t *meta;
633 meta = P80211SKB_FRMMETA(skb);
634 if ( meta && meta->rx) {
635 p80211skb_rxmeta_detach(skb);
637 WLAN_LOG_ERROR("Freeing an skb (%p) w/ no frmmeta.\n", skb);