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>
74 #include "wlan_compat.h"
76 /*================================================================*/
77 /* Project Includes */
79 #include "p80211types.h"
80 #include "p80211hdr.h"
81 #include "p80211conv.h"
82 #include "p80211mgmt.h"
83 #include "p80211msg.h"
84 #include "p80211netdev.h"
85 #include "p80211ioctl.h"
86 #include "p80211req.h"
89 /*================================================================*/
92 /*================================================================*/
96 /*================================================================*/
100 /*================================================================*/
101 /* Local Static Definitions */
103 static UINT8 oui_rfc1042[] = {0x00, 0x00, 0x00};
104 static UINT8 oui_8021h[] = {0x00, 0x00, 0xf8};
106 /*================================================================*/
107 /* Local Function Declarations */
110 /*================================================================*/
111 /* Function Definitions */
113 /*----------------------------------------------------------------
114 * p80211pb_ether_to_80211
116 * Uses the contents of the ether frame and the etherconv setting
117 * to build the elements of the 802.11 frame.
119 * We don't actually set
120 * up the frame header here. That's the MAC's job. We're only handling
121 * conversion of DIXII or 802.3+LLC frames to something that works
124 * Note -- 802.11 header is NOT part of the skb. Likewise, the 802.11
125 * FCS is also not present and will need to be added elsewhere.
128 * ethconv Conversion type to perform
129 * skb skbuff containing the ether frame
130 * p80211_hdr 802.11 header
133 * 0 on success, non-zero otherwise
136 * May be called in interrupt or non-interrupt context
137 ----------------------------------------------------------------*/
138 int skb_ether_to_p80211( wlandevice_t *wlandev, UINT32 ethconv, struct sk_buff *skb, p80211_hdr_t *p80211_hdr, p80211_metawep_t *p80211_wep)
149 memcpy(&e_hdr, skb->data, sizeof(e_hdr));
152 WLAN_LOG_DEBUG(1, "zero-length skb!\n");
156 if ( ethconv == WLAN_ETHCONV_ENCAP ) { /* simplest case */
157 WLAN_LOG_DEBUG(3, "ENCAP len: %d\n", skb->len);
158 /* here, we don't care what kind of ether frm. Just stick it */
159 /* in the 80211 payload */
160 /* which is to say, leave the skb alone. */
162 /* step 1: classify ether frame, DIX or 802.3? */
163 proto = ntohs(e_hdr.type);
164 if ( proto <= 1500 ) {
165 WLAN_LOG_DEBUG(3, "802.3 len: %d\n", skb->len);
166 /* codes <= 1500 reserved for 802.3 lengths */
167 /* it's 802.3, pass ether payload unchanged, */
169 /* trim off ethernet header */
170 skb_pull(skb, WLAN_ETHHDR_LEN);
172 /* leave off any PAD octets. */
173 skb_trim(skb, proto);
175 WLAN_LOG_DEBUG(3, "DIXII len: %d\n", skb->len);
176 /* it's DIXII, time for some conversion */
178 /* trim off ethernet header */
179 skb_pull(skb, WLAN_ETHHDR_LEN);
182 e_snap = (wlan_snap_t *) skb_push(skb, sizeof(wlan_snap_t));
183 e_snap->type = htons(proto);
184 if ( ethconv == WLAN_ETHCONV_8021h && p80211_stt_findproto(proto) ) {
185 memcpy( e_snap->oui, oui_8021h, WLAN_IEEE_OUI_LEN);
187 memcpy( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN);
191 e_llc = (wlan_llc_t *) skb_push(skb, sizeof(wlan_llc_t));
192 e_llc->dsap = 0xAA; /* SNAP, see IEEE 802 */
199 /* Set up the 802.11 header */
200 /* It's a data frame */
201 fc = host2ieee16( WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
202 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
204 switch ( wlandev->macmode ) {
205 case WLAN_MACMODE_IBSS_STA:
206 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, WLAN_ADDR_LEN);
207 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, WLAN_ADDR_LEN);
208 memcpy(p80211_hdr->a3.a3, wlandev->bssid, WLAN_ADDR_LEN);
210 case WLAN_MACMODE_ESS_STA:
211 fc |= host2ieee16(WLAN_SET_FC_TODS(1));
212 memcpy(p80211_hdr->a3.a1, wlandev->bssid, WLAN_ADDR_LEN);
213 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, WLAN_ADDR_LEN);
214 memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, WLAN_ADDR_LEN);
216 case WLAN_MACMODE_ESS_AP:
217 fc |= host2ieee16(WLAN_SET_FC_FROMDS(1));
218 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, WLAN_ADDR_LEN);
219 memcpy(p80211_hdr->a3.a2, wlandev->bssid, WLAN_ADDR_LEN);
220 memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, WLAN_ADDR_LEN);
223 WLAN_LOG_ERROR("Error: Converting eth to wlan in unknown mode.\n");
228 p80211_wep->data = NULL;
230 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
231 // XXXX need to pick keynum other than default?
234 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
236 p80211_wep->data = skb->data;
239 if ((foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
241 (wlandev->hostwep & HOSTWEP_DEFAULTKEY_MASK),
242 p80211_wep->iv, p80211_wep->icv))) {
243 WLAN_LOG_WARNING("Host en-WEP failed, dropping frame (%d).\n", foo);
246 fc |= host2ieee16(WLAN_SET_FC_ISWEP(1));
250 // skb->nh.raw = skb->data;
252 p80211_hdr->a3.fc = fc;
253 p80211_hdr->a3.dur = 0;
254 p80211_hdr->a3.seq = 0;
260 /* jkriegl: from orinoco, modified */
261 static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
262 p80211_rxmeta_t *rxmeta)
266 /* Gather wireless spy statistics: for each packet, compare the
267 * source address with out list, and if match, get the stats... */
269 for (i = 0; i < wlandev->spy_number; i++) {
271 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
272 memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
273 wlandev->spy_stat[i].level = rxmeta->signal;
274 wlandev->spy_stat[i].noise = rxmeta->noise;
275 wlandev->spy_stat[i].qual = (rxmeta->signal > rxmeta->noise) ? \
276 (rxmeta->signal - rxmeta->noise) : 0;
277 wlandev->spy_stat[i].updated = 0x7;
282 /*----------------------------------------------------------------
283 * p80211pb_80211_to_ether
285 * Uses the contents of a received 802.11 frame and the etherconv
286 * setting to build an ether frame.
288 * This function extracts the src and dest address from the 802.11
289 * frame to use in the construction of the eth frame.
292 * ethconv Conversion type to perform
293 * skb Packet buffer containing the 802.11 frame
296 * 0 on success, non-zero otherwise
299 * May be called in interrupt or non-interrupt context
300 ----------------------------------------------------------------*/
301 int skb_p80211_to_ether( wlandevice_t *wlandev, UINT32 ethconv, struct sk_buff *skb)
303 netdevice_t *netdev = wlandev->netdev;
307 UINT8 daddr[WLAN_ETHADDR_LEN];
308 UINT8 saddr[WLAN_ETHADDR_LEN];
310 wlan_ethhdr_t *e_hdr;
318 payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
319 payload_offset = WLAN_HDR_A3_LEN;
321 w_hdr = (p80211_hdr_t *) skb->data;
323 /* setup some vars for convenience */
324 fc = ieee2host16(w_hdr->a3.fc);
325 if ( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
326 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
327 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
328 } else if( (WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 1) ) {
329 memcpy(daddr, w_hdr->a3.a1, WLAN_ETHADDR_LEN);
330 memcpy(saddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
331 } else if( (WLAN_GET_FC_TODS(fc) == 1) && (WLAN_GET_FC_FROMDS(fc) == 0) ) {
332 memcpy(daddr, w_hdr->a3.a3, WLAN_ETHADDR_LEN);
333 memcpy(saddr, w_hdr->a3.a2, WLAN_ETHADDR_LEN);
335 payload_offset = WLAN_HDR_A4_LEN;
336 payload_length -= ( WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN );
337 if (payload_length < 0 ) {
338 WLAN_LOG_ERROR("A4 frame too short!\n");
341 memcpy(daddr, w_hdr->a4.a3, WLAN_ETHADDR_LEN);
342 memcpy(saddr, w_hdr->a4.a4, WLAN_ETHADDR_LEN);
345 /* perform de-wep if necessary.. */
346 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) && WLAN_GET_FC_ISWEP(fc) && (wlandev->hostwep & HOSTWEP_DECRYPT)) {
347 if (payload_length <= 8) {
348 WLAN_LOG_ERROR("WEP frame too short (%u).\n",
352 if ((foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
353 payload_length - 8, -1,
354 skb->data + payload_offset,
355 skb->data + payload_offset + payload_length - 4))) {
356 /* de-wep failed, drop skb. */
357 WLAN_LOG_DEBUG(1, "Host de-WEP failed, dropping frame (%d).\n", foo);
358 wlandev->rx.decrypt_err++;
362 /* subtract the IV+ICV length off the payload */
364 /* chop off the IV */
366 /* chop off the ICV. */
367 skb_trim(skb, skb->len - 4);
369 wlandev->rx.decrypt++;
372 e_hdr = (wlan_ethhdr_t *) (skb->data + payload_offset);
374 e_llc = (wlan_llc_t *) (skb->data + payload_offset);
375 e_snap = (wlan_snap_t *) (skb->data + payload_offset + sizeof(wlan_llc_t));
377 /* Test for the various encodings */
378 if ( (payload_length >= sizeof(wlan_ethhdr_t)) &&
379 ( e_llc->dsap != 0xaa || e_llc->ssap != 0xaa ) &&
380 ((memcmp(daddr, e_hdr->daddr, WLAN_ETHADDR_LEN) == 0) ||
381 (memcmp(saddr, e_hdr->saddr, WLAN_ETHADDR_LEN) == 0))) {
382 WLAN_LOG_DEBUG(3, "802.3 ENCAP len: %d\n", payload_length);
383 /* 802.3 Encapsulated */
384 /* Test for an overlength frame */
385 if ( payload_length > (netdev->mtu + WLAN_ETHHDR_LEN)) {
386 /* A bogus length ethfrm has been encap'd. */
387 /* Is someone trying an oflow attack? */
388 WLAN_LOG_ERROR("ENCAP frame too large (%d > %d)\n",
389 payload_length, netdev->mtu + WLAN_ETHHDR_LEN);
393 /* Chop off the 802.11 header. it's already sane. */
394 skb_pull(skb, payload_offset);
395 /* chop off the 802.11 CRC */
396 skb_trim(skb, skb->len - WLAN_CRC_LEN);
398 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
399 (e_llc->dsap == 0xaa) &&
400 (e_llc->ssap == 0xaa) &&
401 (e_llc->ctl == 0x03) &&
402 (((memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)==0) &&
403 (ethconv == WLAN_ETHCONV_8021h) &&
404 (p80211_stt_findproto(ieee2host16(e_snap->type)))) ||
405 (memcmp( e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN)!=0)))
407 WLAN_LOG_DEBUG(3, "SNAP+RFC1042 len: %d\n", payload_length);
408 /* it's a SNAP + RFC1042 frame && protocol is in STT */
409 /* build 802.3 + RFC1042 */
411 /* Test for an overlength frame */
412 if ( payload_length > netdev->mtu ) {
413 /* A bogus length ethfrm has been sent. */
414 /* Is someone trying an oflow attack? */
415 WLAN_LOG_ERROR("SNAP frame too large (%d > %d)\n",
416 payload_length, netdev->mtu);
420 /* chop 802.11 header from skb. */
421 skb_pull(skb, payload_offset);
423 /* create 802.3 header at beginning of skb. */
424 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
425 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
426 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
427 e_hdr->type = htons(payload_length);
429 /* chop off the 802.11 CRC */
430 skb_trim(skb, skb->len - WLAN_CRC_LEN);
432 } else if ((payload_length >= sizeof(wlan_llc_t) + sizeof(wlan_snap_t)) &&
433 (e_llc->dsap == 0xaa) &&
434 (e_llc->ssap == 0xaa) &&
435 (e_llc->ctl == 0x03) ) {
436 WLAN_LOG_DEBUG(3, "802.1h/RFC1042 len: %d\n", payload_length);
437 /* it's an 802.1h frame || (an RFC1042 && protocol is not in STT) */
438 /* build a DIXII + RFC894 */
440 /* Test for an overlength frame */
441 if ((payload_length - sizeof(wlan_llc_t) - sizeof(wlan_snap_t))
443 /* A bogus length ethfrm has been sent. */
444 /* Is someone trying an oflow attack? */
445 WLAN_LOG_ERROR("DIXII frame too large (%ld > %d)\n",
446 (long int) (payload_length - sizeof(wlan_llc_t) -
447 sizeof(wlan_snap_t)),
452 /* chop 802.11 header from skb. */
453 skb_pull(skb, payload_offset);
455 /* chop llc header from skb. */
456 skb_pull(skb, sizeof(wlan_llc_t));
458 /* chop snap header from skb. */
459 skb_pull(skb, sizeof(wlan_snap_t));
461 /* create 802.3 header at beginning of skb. */
462 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
463 e_hdr->type = e_snap->type;
464 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
465 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
467 /* chop off the 802.11 CRC */
468 skb_trim(skb, skb->len - WLAN_CRC_LEN);
470 WLAN_LOG_DEBUG(3, "NON-ENCAP len: %d\n", payload_length);
472 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
473 /* build an 802.3 frame */
474 /* allocate space and setup hostbuf */
476 /* Test for an overlength frame */
477 if ( payload_length > netdev->mtu ) {
478 /* A bogus length ethfrm has been sent. */
479 /* Is someone trying an oflow attack? */
480 WLAN_LOG_ERROR("OTHER frame too large (%d > %d)\n",
486 /* Chop off the 802.11 header. */
487 skb_pull(skb, payload_offset);
489 /* create 802.3 header at beginning of skb. */
490 e_hdr = (wlan_ethhdr_t *) skb_push(skb, WLAN_ETHHDR_LEN);
491 memcpy(e_hdr->daddr, daddr, WLAN_ETHADDR_LEN);
492 memcpy(e_hdr->saddr, saddr, WLAN_ETHADDR_LEN);
493 e_hdr->type = htons(payload_length);
495 /* chop off the 802.11 CRC */
496 skb_trim(skb, skb->len - WLAN_CRC_LEN);
500 skb->protocol = eth_type_trans(skb, netdev);
501 skb_reset_mac_header(skb);
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);
515 /*----------------------------------------------------------------
516 * p80211_stt_findproto
518 * Searches the 802.1h Selective Translation Table for a given
522 * proto protocl number (in host order) to search for.
525 * 1 - if the table is empty or a match is found.
526 * 0 - if the table is non-empty and a match is not found.
529 * May be called in interrupt or non-interrupt context
530 ----------------------------------------------------------------*/
531 int p80211_stt_findproto(UINT16 proto)
533 /* Always return found for now. This is the behavior used by the */
534 /* Zoom Win95 driver when 802.1h mode is selected */
535 /* TODO: If necessary, add an actual search we'll probably
536 need this to match the CMAC's way of doing things.
537 Need to do some testing to confirm.
540 if (proto == 0x80f3) /* APPLETALK */
546 /*----------------------------------------------------------------
547 * p80211skb_rxmeta_detach
549 * Disconnects the frmmeta and rxmeta from an skb.
552 * wlandev The wlandev this skb belongs to.
553 * skb The skb we're attaching to.
556 * 0 on success, non-zero otherwise
559 * May be called in interrupt or non-interrupt context
560 ----------------------------------------------------------------*/
562 p80211skb_rxmeta_detach(struct sk_buff *skb)
564 p80211_rxmeta_t *rxmeta;
565 p80211_frmmeta_t *frmmeta;
569 if ( skb==NULL ) { /* bad skb */
570 WLAN_LOG_DEBUG(1, "Called w/ null skb.\n");
573 frmmeta = P80211SKB_FRMMETA(skb);
574 if ( frmmeta == NULL ) { /* no magic */
575 WLAN_LOG_DEBUG(1, "Called w/ bad frmmeta magic.\n");
578 rxmeta = frmmeta->rx;
579 if ( rxmeta == NULL ) { /* bad meta ptr */
580 WLAN_LOG_DEBUG(1, "Called w/ bad rxmeta ptr.\n");
588 memset(skb->cb, 0, sizeof(skb->cb));
594 /*----------------------------------------------------------------
595 * p80211skb_rxmeta_attach
597 * Allocates a p80211rxmeta structure, initializes it, and attaches
601 * wlandev The wlandev this skb belongs to.
602 * skb The skb we're attaching to.
605 * 0 on success, non-zero otherwise
608 * May be called in interrupt or non-interrupt context
609 ----------------------------------------------------------------*/
611 p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
614 p80211_rxmeta_t *rxmeta;
615 p80211_frmmeta_t *frmmeta;
619 /* If these already have metadata, we error out! */
620 if (P80211SKB_RXMETA(skb) != NULL) {
621 WLAN_LOG_ERROR("%s: RXmeta already attached!\n",
627 /* Allocate the rxmeta */
628 rxmeta = kmalloc(sizeof(p80211_rxmeta_t), GFP_ATOMIC);
630 if ( rxmeta == NULL ) {
631 WLAN_LOG_ERROR("%s: Failed to allocate rxmeta.\n",
637 /* Initialize the rxmeta */
638 memset(rxmeta, 0, sizeof(p80211_rxmeta_t));
639 rxmeta->wlandev = wlandev;
640 rxmeta->hosttime = jiffies;
642 /* Overlay a frmmeta_t onto skb->cb */
643 memset(skb->cb, 0, sizeof(p80211_frmmeta_t));
644 frmmeta = (p80211_frmmeta_t*)(skb->cb);
645 frmmeta->magic = P80211_FRMMETA_MAGIC;
646 frmmeta->rx = rxmeta;
652 /*----------------------------------------------------------------
655 * Frees an entire p80211skb by checking and freeing the meta struct
656 * and then freeing the skb.
659 * wlandev The wlandev this skb belongs to.
660 * skb The skb we're attaching to.
663 * 0 on success, non-zero otherwise
666 * May be called in interrupt or non-interrupt context
667 ----------------------------------------------------------------*/
669 p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
671 p80211_frmmeta_t *meta;
673 meta = P80211SKB_FRMMETA(skb);
674 if ( meta && meta->rx) {
675 p80211skb_rxmeta_detach(skb);
677 WLAN_LOG_ERROR("Freeing an skb (%p) w/ no frmmeta.\n", skb);