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 /*================================================================*/
56 #include <linux/module.h>
57 #include <linux/kernel.h>
58 #include <linux/sched.h>
59 #include <linux/types.h>
60 #include <linux/skbuff.h>
61 #include <linux/slab.h>
62 #include <linux/wireless.h>
63 #include <linux/netdevice.h>
64 #include <linux/etherdevice.h>
65 #include <linux/if_ether.h>
66 #include <linux/byteorder/generic.h>
68 #include <asm/byteorder.h>
70 #include "wlan_compat.h"
72 /*================================================================*/
73 /* Project Includes */
75 #include "p80211types.h"
76 #include "p80211hdr.h"
77 #include "p80211conv.h"
78 #include "p80211mgmt.h"
79 #include "p80211msg.h"
80 #include "p80211netdev.h"
81 #include "p80211ioctl.h"
82 #include "p80211req.h"
85 /*================================================================*/
88 /*================================================================*/
92 /*================================================================*/
96 /*================================================================*/
97 /* Local Static Definitions */
99 static u8 oui_rfc1042[] = {0x00, 0x00, 0x00};
100 static u8 oui_8021h[] = {0x00, 0x00, 0xf8};
102 /*================================================================*/
103 /* Local Function Declarations */
106 /*================================================================*/
107 /* Function Definitions */
109 /*----------------------------------------------------------------
110 * p80211pb_ether_to_80211
112 * Uses the contents of the ether frame and the etherconv setting
113 * to build the elements of the 802.11 frame.
115 * We don't actually set
116 * up the frame header here. That's the MAC's job. We're only handling
117 * conversion of DIXII or 802.3+LLC frames to something that works
120 * Note -- 802.11 header is NOT part of the skb. Likewise, the 802.11
121 * FCS is also not present and will need to be added elsewhere.
124 * ethconv Conversion type to perform
125 * skb skbuff containing the ether frame
126 * p80211_hdr 802.11 header
129 * 0 on success, non-zero otherwise
132 * May be called in interrupt or non-interrupt context
133 ----------------------------------------------------------------*/
134 int skb_ether_to_p80211( wlandevice_t *wlandev, u32 ethconv, struct sk_buff *skb, p80211_hdr_t *p80211_hdr, p80211_metawep_t *p80211_wep)
144 memcpy(&e_hdr, skb->data, sizeof(e_hdr));
147 WLAN_LOG_DEBUG(1, "zero-length skb!\n");
151 if ( ethconv == WLAN_ETHCONV_ENCAP ) { /* simplest case */
152 WLAN_LOG_DEBUG(3, "ENCAP len: %d\n", skb->len);
153 /* here, we don't care what kind of ether frm. Just stick it */
154 /* in the 80211 payload */
155 /* which is to say, leave the skb alone. */
157 /* step 1: classify ether frame, DIX or 802.3? */
158 proto = ntohs(e_hdr.type);
159 if ( proto <= 1500 ) {
160 WLAN_LOG_DEBUG(3, "802.3 len: %d\n", skb->len);
161 /* codes <= 1500 reserved for 802.3 lengths */
162 /* it's 802.3, pass ether payload unchanged, */
164 /* trim off ethernet header */
165 skb_pull(skb, WLAN_ETHHDR_LEN);
167 /* leave off any PAD octets. */
168 skb_trim(skb, proto);
170 WLAN_LOG_DEBUG(3, "DIXII len: %d\n", skb->len);
171 /* it's DIXII, time for some conversion */
173 /* trim off ethernet header */
174 skb_pull(skb, WLAN_ETHHDR_LEN);
177 e_snap = (wlan_snap_t *) skb_push(skb, sizeof(wlan_snap_t));
178 e_snap->type = htons(proto);
179 if ( ethconv == WLAN_ETHCONV_8021h && p80211_stt_findproto(proto) ) {
180 memcpy( e_snap->oui, oui_8021h, WLAN_IEEE_OUI_LEN);
182 memcpy( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN);
186 e_llc = (wlan_llc_t *) skb_push(skb, sizeof(wlan_llc_t));
187 e_llc->dsap = 0xAA; /* SNAP, see IEEE 802 */
194 /* Set up the 802.11 header */
195 /* It's a data frame */
196 fc = cpu_to_le16( WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
197 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
199 switch ( wlandev->macmode ) {
200 case WLAN_MACMODE_IBSS_STA:
201 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
202 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
203 memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
205 case WLAN_MACMODE_ESS_STA:
206 fc |= cpu_to_le16(WLAN_SET_FC_TODS(1));
207 memcpy(p80211_hdr->a3.a1, wlandev->bssid, ETH_ALEN);
208 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
209 memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, ETH_ALEN);
211 case WLAN_MACMODE_ESS_AP:
212 fc |= cpu_to_le16(WLAN_SET_FC_FROMDS(1));
213 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
214 memcpy(p80211_hdr->a3.a2, wlandev->bssid, ETH_ALEN);
215 memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, ETH_ALEN);
218 printk(KERN_ERR "Error: Converting eth to wlan in unknown mode.\n");
223 p80211_wep->data = NULL;
225 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
226 // XXXX need to pick keynum other than default?
229 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
231 p80211_wep->data = skb->data;
234 if ((foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
236 (wlandev->hostwep & HOSTWEP_DEFAULTKEY_MASK),
237 p80211_wep->iv, p80211_wep->icv))) {
238 printk(KERN_WARNING "Host en-WEP failed, dropping frame (%d).\n", foo);
241 fc |= cpu_to_le16(WLAN_SET_FC_ISWEP(1));
245 // skb->nh.raw = skb->data;
247 p80211_hdr->a3.fc = fc;
248 p80211_hdr->a3.dur = 0;
249 p80211_hdr->a3.seq = 0;
254 /* jkriegl: from orinoco, modified */
255 static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
256 p80211_rxmeta_t *rxmeta)
260 /* Gather wireless spy statistics: for each packet, compare the
261 * source address with out list, and if match, get the stats... */
263 for (i = 0; i < wlandev->spy_number; i++) {
265 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
266 memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
267 wlandev->spy_stat[i].level = rxmeta->signal;
268 wlandev->spy_stat[i].noise = rxmeta->noise;
269 wlandev->spy_stat[i].qual = (rxmeta->signal > rxmeta->noise) ? \
270 (rxmeta->signal - rxmeta->noise) : 0;
271 wlandev->spy_stat[i].updated = 0x7;
276 /*----------------------------------------------------------------
277 * p80211pb_80211_to_ether
279 * Uses the contents of a received 802.11 frame and the etherconv
280 * setting to build an ether frame.
282 * This function extracts the src and dest address from the 802.11
283 * frame to use in the construction of the eth frame.
286 * ethconv Conversion type to perform
287 * skb Packet buffer containing the 802.11 frame
290 * 0 on success, non-zero otherwise
293 * May be called in interrupt or non-interrupt context
294 ----------------------------------------------------------------*/
295 int skb_p80211_to_ether( wlandevice_t *wlandev, u32 ethconv, struct sk_buff *skb)
297 netdevice_t *netdev = wlandev->netdev;
299 unsigned int payload_length;
300 unsigned int payload_offset;
301 u8 daddr[WLAN_ETHADDR_LEN];
302 u8 saddr[WLAN_ETHADDR_LEN];
304 wlan_ethhdr_t *e_hdr;
310 payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
311 payload_offset = WLAN_HDR_A3_LEN;
313 w_hdr = (p80211_hdr_t *) skb->data;
315 /* setup some vars for convenience */
316 fc = le16_to_cpu(w_hdr->a3.fc);
317 if ( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
318 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
319 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
320 } else if( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 1) ) {
321 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
322 memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
323 } else if( (WLAN_GET_FC_TODS(fc) == 1) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
324 memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
325 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
327 payload_offset = WLAN_HDR_A4_LEN;
328 if (payload_length < WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN) {
329 printk(KERN_ERR "A4 frame too short!\n");
332 payload_length -= (WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN);
333 memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
334 memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
337 /* perform de-wep if necessary.. */
338 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && WLAN_GET_FC_ISWEP(fc) && (wlandev->hostwep & HOSTWEP_DECRYPT)) {
339 if (payload_length <= 8) {
340 printk(KERN_ERR "WEP frame too short (%u).\n",
344 if ((foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
345 payload_length - 8, -1,
346 skb->data + payload_offset,
347 skb->data + payload_offset + payload_length - 4))) {
348 /* de-wep failed, drop skb. */
349 WLAN_LOG_DEBUG(1, "Host de-WEP failed, dropping frame (%d).\n", foo);
350 wlandev->rx.decrypt_err++;
354 /* subtract the IV+ICV length off the payload */
356 /* chop off the IV */
358 /* chop off the ICV. */
359 skb_trim(skb, skb->len - 4);
361 wlandev->rx.decrypt++;
364 e_hdr = (wlan_ethhdr_t *) (skb->data + payload_offset);
366 e_llc = (wlan_llc_t *) (skb->data + payload_offset);
367 e_snap = (wlan_snap_t *) (skb->data + payload_offset + sizeof(wlan_llc_t));
369 /* Test for the various encodings */
370 if ( (payload_length >= sizeof(wlan_ethhdr_t)) &&
371 ( e_llc->dsap != 0xaa || e_llc->ssap != 0xaa ) &&
372 ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
373 (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
374 WLAN_LOG_DEBUG(3, "802.3 ENCAP len: %d\n", payload_length);
375 /* 802.3 Encapsulated */
376 /* Test for an overlength frame */
377 if ( payload_length > (netdev->mtu + WLAN_ETHHDR_LEN)) {
378 /* A bogus length ethfrm has been encap'd. */
379 /* Is someone trying an oflow attack? */
380 printk(KERN_ERR "ENCAP frame too large (%d > %d)\n",
381 payload_length, netdev->mtu + WLAN_ETHHDR_LEN);
385 /* Chop off the 802.11 header. it's already sane. */
386 skb_pull(skb, payload_offset);
387 /* chop off the 802.11 CRC */
388 skb_trim(skb, skb->len - WLAN_CRC_LEN);
390 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
391 (e_llc->dsap == 0xaa) &&
392 (e_llc->ssap == 0xaa) &&
393 (e_llc->ctl == 0x03) &&
394 (((memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)==0) &&
395 (ethconv == WLAN_ETHCONV_8021h) &&
396 (p80211_stt_findproto(le16_to_cpu(e_snap->type)))) ||
397 (memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)!=0)))
399 WLAN_LOG_DEBUG(3, "SNAP+RFC1042 len: %d\n", payload_length);
400 /* it's a SNAP + RFC1042 frame && protocol is in STT */
401 /* build 802.3 + RFC1042 */
403 /* Test for an overlength frame */
404 if ( payload_length > netdev->mtu ) {
405 /* A bogus length ethfrm has been sent. */
406 /* Is someone trying an oflow attack? */
407 printk(KERN_ERR "SNAP frame too large (%d > %d)\n",
408 payload_length, netdev->mtu);
412 /* chop 802.11 header from skb. */
413 skb_pull(skb, payload_offset);
415 /* create 802.3 header at beginning of skb. */
416 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
417 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
418 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
419 e_hdr->type = htons(payload_length);
421 /* chop off the 802.11 CRC */
422 skb_trim(skb, skb->len - WLAN_CRC_LEN);
424 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
425 (e_llc->dsap == 0xaa) &&
426 (e_llc->ssap == 0xaa) &&
427 (e_llc->ctl == 0x03) ) {
428 WLAN_LOG_DEBUG(3, "802.1h/RFC1042 len: %d\n", payload_length);
429 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
430 /* build a DIXII + RFC894 */
432 /* Test for an overlength frame */
433 if ((payload_length - sizeof(wlan_llc_t) - sizeof(wlan_snap_t))
435 /* A bogus length ethfrm has been sent. */
436 /* Is someone trying an oflow attack? */
437 printk(KERN_ERR "DIXII frame too large (%ld > %d)\n",
438 (long int) (payload_length - sizeof(wlan_llc_t) -
439 sizeof(wlan_snap_t)),
444 /* chop 802.11 header from skb. */
445 skb_pull(skb, payload_offset);
447 /* chop llc header from skb. */
448 skb_pull(skb, sizeof(wlan_llc_t));
450 /* chop snap header from skb. */
451 skb_pull(skb, sizeof(wlan_snap_t));
453 /* create 802.3 header at beginning of skb. */
454 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
455 e_hdr->type = e_snap->type;
456 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
457 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
459 /* chop off the 802.11 CRC */
460 skb_trim(skb, skb->len - WLAN_CRC_LEN);
462 WLAN_LOG_DEBUG(3, "NON-ENCAP len: %d\n", payload_length);
464 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
465 /* build an 802.3 frame */
466 /* allocate space and setup hostbuf */
468 /* Test for an overlength frame */
469 if ( payload_length > netdev->mtu ) {
470 /* A bogus length ethfrm has been sent. */
471 /* Is someone trying an oflow attack? */
472 printk(KERN_ERR "OTHER frame too large (%d > %d)\n",
478 /* Chop off the 802.11 header. */
479 skb_pull(skb, payload_offset);
481 /* create 802.3 header at beginning of skb. */
482 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
483 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
484 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
485 e_hdr->type = htons(payload_length);
487 /* chop off the 802.11 CRC */
488 skb_trim(skb, skb->len - WLAN_CRC_LEN);
493 * Note that eth_type_trans() expects an skb w/ skb->data pointing
494 * at the MAC header, it then sets the following skb members:
498 * It then _returns_ the value that _we're_ supposed to stuff in
499 * skb->protocol. This is nuts.
501 skb->protocol = eth_type_trans(skb, netdev);
503 /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
504 /* jkriegl: only process signal/noise if requested by iwspy */
505 if (wlandev->spy_number)
506 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source, P80211SKB_RXMETA(skb));
508 /* Free the metadata */
509 p80211skb_rxmeta_detach(skb);
514 /*----------------------------------------------------------------
515 * p80211_stt_findproto
517 * Searches the 802.1h Selective Translation Table for a given
521 * proto protocl number (in host order) to search for.
524 * 1 - if the table is empty or a match is found.
525 * 0 - if the table is non-empty and a match is not found.
528 * May be called in interrupt or non-interrupt context
529 ----------------------------------------------------------------*/
530 int p80211_stt_findproto(u16 proto)
532 /* Always return found for now. This is the behavior used by the */
533 /* Zoom Win95 driver when 802.1h mode is selected */
534 /* TODO: If necessary, add an actual search we'll probably
535 need this to match the CMAC's way of doing things.
536 Need to do some testing to confirm.
539 if (proto == 0x80f3) /* APPLETALK */
545 /*----------------------------------------------------------------
546 * p80211skb_rxmeta_detach
548 * Disconnects the frmmeta and rxmeta from an skb.
551 * wlandev The wlandev this skb belongs to.
552 * skb The skb we're attaching to.
555 * 0 on success, non-zero otherwise
558 * May be called in interrupt or non-interrupt context
559 ----------------------------------------------------------------*/
561 p80211skb_rxmeta_detach(struct sk_buff *skb)
563 p80211_rxmeta_t *rxmeta;
564 p80211_frmmeta_t *frmmeta;
567 if ( skb==NULL ) { /* bad skb */
568 WLAN_LOG_DEBUG(1, "Called w/ null skb.\n");
571 frmmeta = P80211SKB_FRMMETA(skb);
572 if ( frmmeta == NULL ) { /* no magic */
573 WLAN_LOG_DEBUG(1, "Called w/ bad frmmeta magic.\n");
576 rxmeta = frmmeta->rx;
577 if ( rxmeta == NULL ) { /* bad meta ptr */
578 WLAN_LOG_DEBUG(1, "Called w/ bad rxmeta ptr.\n");
586 memset(skb->cb, 0, sizeof(skb->cb));
591 /*----------------------------------------------------------------
592 * p80211skb_rxmeta_attach
594 * Allocates a p80211rxmeta structure, initializes it, and attaches
598 * wlandev The wlandev this skb belongs to.
599 * skb The skb we're attaching to.
602 * 0 on success, non-zero otherwise
605 * May be called in interrupt or non-interrupt context
606 ----------------------------------------------------------------*/
608 p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
611 p80211_rxmeta_t *rxmeta;
612 p80211_frmmeta_t *frmmeta;
614 /* If these already have metadata, we error out! */
615 if (P80211SKB_RXMETA(skb) != NULL) {
616 printk(KERN_ERR "%s: RXmeta already attached!\n",
622 /* Allocate the rxmeta */
623 rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
625 if ( rxmeta == NULL ) {
626 printk(KERN_ERR "%s: Failed to allocate rxmeta.\n",
632 /* Initialize the rxmeta */
633 memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
634 rxmeta->wlandev = wlandev;
635 rxmeta->hosttime = jiffies;
637 /* Overlay a frmmeta_t onto skb->cb */
638 memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
639 frmmeta = (p80211_frmmeta_t*)(skb->cb);
640 frmmeta->magic = P80211_FRMMETA_MAGIC;
641 frmmeta->rx = rxmeta;
646 /*----------------------------------------------------------------
649 * Frees an entire p80211skb by checking and freeing the meta struct
650 * and then freeing the skb.
653 * wlandev The wlandev this skb belongs to.
654 * skb The skb we're attaching to.
657 * 0 on success, non-zero otherwise
660 * May be called in interrupt or non-interrupt context
661 ----------------------------------------------------------------*/
663 p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
665 p80211_frmmeta_t *meta;
667 meta = P80211SKB_FRMMETA(skb);
668 if ( meta && meta->rx) {
669 p80211skb_rxmeta_detach(skb);
671 printk(KERN_ERR "Freeing an skb (%p) w/ no frmmeta.\n", skb);