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 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/sched.h>
58 #include <linux/types.h>
59 #include <linux/skbuff.h>
60 #include <linux/slab.h>
61 #include <linux/wireless.h>
62 #include <linux/netdevice.h>
63 #include <linux/etherdevice.h>
64 #include <linux/if_ether.h>
65 #include <linux/byteorder/generic.h>
67 #include <asm/byteorder.h>
69 #include "p80211types.h"
70 #include "p80211hdr.h"
71 #include "p80211conv.h"
72 #include "p80211mgmt.h"
73 #include "p80211msg.h"
74 #include "p80211netdev.h"
75 #include "p80211ioctl.h"
76 #include "p80211req.h"
78 static const u8 oui_rfc1042[] = { 0x00, 0x00, 0x00 };
79 static const u8 oui_8021h[] = { 0x00, 0x00, 0xf8 };
81 /*----------------------------------------------------------------
82 * p80211pb_ether_to_80211
84 * Uses the contents of the ether frame and the etherconv setting
85 * to build the elements of the 802.11 frame.
87 * We don't actually set
88 * up the frame header here. That's the MAC's job. We're only handling
89 * conversion of DIXII or 802.3+LLC frames to something that works
92 * Note -- 802.11 header is NOT part of the skb. Likewise, the 802.11
93 * FCS is also not present and will need to be added elsewhere.
96 * ethconv Conversion type to perform
97 * skb skbuff containing the ether frame
98 * p80211_hdr 802.11 header
101 * 0 on success, non-zero otherwise
104 * May be called in interrupt or non-interrupt context
105 *----------------------------------------------------------------
107 int skb_ether_to_p80211(struct wlandevice *wlandev, u32 ethconv,
108 struct sk_buff *skb, union p80211_hdr *p80211_hdr,
109 struct p80211_metawep *p80211_wep)
113 struct wlan_ethhdr e_hdr;
114 struct wlan_llc *e_llc;
115 struct wlan_snap *e_snap;
118 memcpy(&e_hdr, skb->data, sizeof(e_hdr));
121 pr_debug("zero-length skb!\n");
125 if (ethconv == WLAN_ETHCONV_ENCAP) { /* simplest case */
126 pr_debug("ENCAP len: %d\n", skb->len);
127 /* here, we don't care what kind of ether frm. Just stick it */
128 /* in the 80211 payload */
129 /* which is to say, leave the skb alone. */
131 /* step 1: classify ether frame, DIX or 802.3? */
132 proto = ntohs(e_hdr.type);
133 if (proto <= ETH_DATA_LEN) {
134 pr_debug("802.3 len: %d\n", skb->len);
135 /* codes <= 1500 reserved for 802.3 lengths */
136 /* it's 802.3, pass ether payload unchanged, */
138 /* trim off ethernet header */
139 skb_pull(skb, ETH_HLEN);
141 /* leave off any PAD octets. */
142 skb_trim(skb, proto);
144 pr_debug("DIXII len: %d\n", skb->len);
145 /* it's DIXII, time for some conversion */
147 /* trim off ethernet header */
148 skb_pull(skb, ETH_HLEN);
151 e_snap = skb_push(skb, sizeof(struct wlan_snap));
152 e_snap->type = htons(proto);
153 if (ethconv == WLAN_ETHCONV_8021h &&
154 p80211_stt_findproto(proto)) {
155 memcpy(e_snap->oui, oui_8021h,
158 memcpy(e_snap->oui, oui_rfc1042,
163 e_llc = skb_push(skb, sizeof(struct wlan_llc));
164 e_llc->dsap = 0xAA; /* SNAP, see IEEE 802 */
170 /* Set up the 802.11 header */
171 /* It's a data frame */
172 fc = cpu_to_le16(WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
173 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY));
175 switch (wlandev->macmode) {
176 case WLAN_MACMODE_IBSS_STA:
177 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
178 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
179 memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
181 case WLAN_MACMODE_ESS_STA:
182 fc |= cpu_to_le16(WLAN_SET_FC_TODS(1));
183 memcpy(p80211_hdr->a3.a1, wlandev->bssid, ETH_ALEN);
184 memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
185 memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, ETH_ALEN);
187 case WLAN_MACMODE_ESS_AP:
188 fc |= cpu_to_le16(WLAN_SET_FC_FROMDS(1));
189 memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
190 memcpy(p80211_hdr->a3.a2, wlandev->bssid, ETH_ALEN);
191 memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, ETH_ALEN);
194 netdev_err(wlandev->netdev,
195 "Error: Converting eth to wlan in unknown mode.\n");
199 p80211_wep->data = NULL;
201 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) &&
202 (wlandev->hostwep & HOSTWEP_ENCRYPT)) {
203 /* XXXX need to pick keynum other than default? */
205 p80211_wep->data = kmalloc(skb->len, GFP_ATOMIC);
206 if (!p80211_wep->data)
208 foo = wep_encrypt(wlandev, skb->data, p80211_wep->data,
210 wlandev->hostwep & HOSTWEP_DEFAULTKEY_MASK,
211 p80211_wep->iv, p80211_wep->icv);
213 netdev_warn(wlandev->netdev,
214 "Host en-WEP failed, dropping frame (%d).\n",
216 kfree(p80211_wep->data);
219 fc |= cpu_to_le16(WLAN_SET_FC_ISWEP(1));
222 /* skb->nh.raw = skb->data; */
224 p80211_hdr->a3.fc = fc;
225 p80211_hdr->a3.dur = 0;
226 p80211_hdr->a3.seq = 0;
231 /* jkriegl: from orinoco, modified */
232 static void orinoco_spy_gather(struct wlandevice *wlandev, char *mac,
233 struct p80211_rxmeta *rxmeta)
237 /* Gather wireless spy statistics: for each packet, compare the
238 * source address with out list, and if match, get the stats...
241 for (i = 0; i < wlandev->spy_number; i++) {
242 if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
243 wlandev->spy_stat[i].level = rxmeta->signal;
244 wlandev->spy_stat[i].noise = rxmeta->noise;
245 wlandev->spy_stat[i].qual =
247 rxmeta->noise) ? (rxmeta->signal -
249 wlandev->spy_stat[i].updated = 0x7;
254 /*----------------------------------------------------------------
255 * p80211pb_80211_to_ether
257 * Uses the contents of a received 802.11 frame and the etherconv
258 * setting to build an ether frame.
260 * This function extracts the src and dest address from the 802.11
261 * frame to use in the construction of the eth frame.
264 * ethconv Conversion type to perform
265 * skb Packet buffer containing the 802.11 frame
268 * 0 on success, non-zero otherwise
271 * May be called in interrupt or non-interrupt context
272 *----------------------------------------------------------------
274 int skb_p80211_to_ether(struct wlandevice *wlandev, u32 ethconv,
277 struct net_device *netdev = wlandev->netdev;
279 unsigned int payload_length;
280 unsigned int payload_offset;
283 union p80211_hdr *w_hdr;
284 struct wlan_ethhdr *e_hdr;
285 struct wlan_llc *e_llc;
286 struct wlan_snap *e_snap;
290 payload_length = skb->len - WLAN_HDR_A3_LEN - WLAN_CRC_LEN;
291 payload_offset = WLAN_HDR_A3_LEN;
293 w_hdr = (union p80211_hdr *)skb->data;
295 /* setup some vars for convenience */
296 fc = le16_to_cpu(w_hdr->a3.fc);
297 if ((WLAN_GET_FC_TODS(fc) == 0) && (WLAN_GET_FC_FROMDS(fc) == 0)) {
298 ether_addr_copy(daddr, w_hdr->a3.a1);
299 ether_addr_copy(saddr, w_hdr->a3.a2);
300 } else if ((WLAN_GET_FC_TODS(fc) == 0) &&
301 (WLAN_GET_FC_FROMDS(fc) == 1)) {
302 ether_addr_copy(daddr, w_hdr->a3.a1);
303 ether_addr_copy(saddr, w_hdr->a3.a3);
304 } else if ((WLAN_GET_FC_TODS(fc) == 1) &&
305 (WLAN_GET_FC_FROMDS(fc) == 0)) {
306 ether_addr_copy(daddr, w_hdr->a3.a3);
307 ether_addr_copy(saddr, w_hdr->a3.a2);
309 payload_offset = WLAN_HDR_A4_LEN;
310 if (payload_length < WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN) {
311 netdev_err(netdev, "A4 frame too short!\n");
314 payload_length -= (WLAN_HDR_A4_LEN - WLAN_HDR_A3_LEN);
315 ether_addr_copy(daddr, w_hdr->a4.a3);
316 ether_addr_copy(saddr, w_hdr->a4.a4);
319 /* perform de-wep if necessary.. */
320 if ((wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) &&
321 WLAN_GET_FC_ISWEP(fc) &&
322 (wlandev->hostwep & HOSTWEP_DECRYPT)) {
323 if (payload_length <= 8) {
325 "WEP frame too short (%u).\n", skb->len);
328 foo = wep_decrypt(wlandev, skb->data + payload_offset + 4,
329 payload_length - 8, -1,
330 skb->data + payload_offset,
331 skb->data + payload_offset +
334 /* de-wep failed, drop skb. */
335 pr_debug("Host de-WEP failed, dropping frame (%d).\n",
337 wlandev->rx.decrypt_err++;
341 /* subtract the IV+ICV length off the payload */
343 /* chop off the IV */
345 /* chop off the ICV. */
346 skb_trim(skb, skb->len - 4);
348 wlandev->rx.decrypt++;
351 e_hdr = (struct wlan_ethhdr *)(skb->data + payload_offset);
353 e_llc = (struct wlan_llc *)(skb->data + payload_offset);
355 (struct wlan_snap *)(skb->data + payload_offset +
356 sizeof(struct wlan_llc));
358 /* Test for the various encodings */
359 if ((payload_length >= sizeof(struct wlan_ethhdr)) &&
360 (e_llc->dsap != 0xaa || e_llc->ssap != 0xaa) &&
361 ((!ether_addr_equal_unaligned(daddr, e_hdr->daddr)) ||
362 (!ether_addr_equal_unaligned(saddr, e_hdr->saddr)))) {
363 pr_debug("802.3 ENCAP len: %d\n", payload_length);
364 /* 802.3 Encapsulated */
365 /* Test for an overlength frame */
366 if (payload_length > (netdev->mtu + ETH_HLEN)) {
367 /* A bogus length ethfrm has been encap'd. */
368 /* Is someone trying an oflow attack? */
369 netdev_err(netdev, "ENCAP frame too large (%d > %d)\n",
370 payload_length, netdev->mtu + ETH_HLEN);
374 /* Chop off the 802.11 header. it's already sane. */
375 skb_pull(skb, payload_offset);
376 /* chop off the 802.11 CRC */
377 skb_trim(skb, skb->len - WLAN_CRC_LEN);
379 } else if ((payload_length >= sizeof(struct wlan_llc) +
380 sizeof(struct wlan_snap)) &&
381 (e_llc->dsap == 0xaa) &&
382 (e_llc->ssap == 0xaa) &&
383 (e_llc->ctl == 0x03) &&
384 (((memcmp(e_snap->oui, oui_rfc1042,
385 WLAN_IEEE_OUI_LEN) == 0) &&
386 (ethconv == WLAN_ETHCONV_8021h) &&
387 (p80211_stt_findproto(be16_to_cpu(e_snap->type)))) ||
388 (memcmp(e_snap->oui, oui_rfc1042, WLAN_IEEE_OUI_LEN) !=
390 pr_debug("SNAP+RFC1042 len: %d\n", payload_length);
391 /* it's a SNAP + RFC1042 frame && protocol is in STT */
392 /* build 802.3 + RFC1042 */
394 /* Test for an overlength frame */
395 if (payload_length > netdev->mtu) {
396 /* A bogus length ethfrm has been sent. */
397 /* Is someone trying an oflow attack? */
398 netdev_err(netdev, "SNAP frame too large (%d > %d)\n",
399 payload_length, netdev->mtu);
403 /* chop 802.11 header from skb. */
404 skb_pull(skb, payload_offset);
406 /* create 802.3 header at beginning of skb. */
407 e_hdr = skb_push(skb, ETH_HLEN);
408 ether_addr_copy(e_hdr->daddr, daddr);
409 ether_addr_copy(e_hdr->saddr, saddr);
410 e_hdr->type = htons(payload_length);
412 /* chop off the 802.11 CRC */
413 skb_trim(skb, skb->len - WLAN_CRC_LEN);
415 } else if ((payload_length >= sizeof(struct wlan_llc) +
416 sizeof(struct wlan_snap)) &&
417 (e_llc->dsap == 0xaa) &&
418 (e_llc->ssap == 0xaa) &&
419 (e_llc->ctl == 0x03)) {
420 pr_debug("802.1h/RFC1042 len: %d\n", payload_length);
421 /* it's an 802.1h frame || (an RFC1042 && protocol not in STT)
422 * build a DIXII + RFC894
425 /* Test for an overlength frame */
426 if ((payload_length - sizeof(struct wlan_llc) -
427 sizeof(struct wlan_snap))
429 /* A bogus length ethfrm has been sent. */
430 /* Is someone trying an oflow attack? */
431 netdev_err(netdev, "DIXII frame too large (%ld > %d)\n",
432 (long int)(payload_length -
433 sizeof(struct wlan_llc) -
434 sizeof(struct wlan_snap)), netdev->mtu);
438 /* chop 802.11 header from skb. */
439 skb_pull(skb, payload_offset);
441 /* chop llc header from skb. */
442 skb_pull(skb, sizeof(struct wlan_llc));
444 /* chop snap header from skb. */
445 skb_pull(skb, sizeof(struct wlan_snap));
447 /* create 802.3 header at beginning of skb. */
448 e_hdr = skb_push(skb, ETH_HLEN);
449 e_hdr->type = e_snap->type;
450 ether_addr_copy(e_hdr->daddr, daddr);
451 ether_addr_copy(e_hdr->saddr, saddr);
453 /* chop off the 802.11 CRC */
454 skb_trim(skb, skb->len - WLAN_CRC_LEN);
456 pr_debug("NON-ENCAP len: %d\n", payload_length);
458 /* it's a generic 80211+LLC or IPX 'Raw 802.3' */
459 /* build an 802.3 frame */
460 /* allocate space and setup hostbuf */
462 /* Test for an overlength frame */
463 if (payload_length > netdev->mtu) {
464 /* A bogus length ethfrm has been sent. */
465 /* Is someone trying an oflow attack? */
466 netdev_err(netdev, "OTHER frame too large (%d > %d)\n",
467 payload_length, netdev->mtu);
471 /* Chop off the 802.11 header. */
472 skb_pull(skb, payload_offset);
474 /* create 802.3 header at beginning of skb. */
475 e_hdr = skb_push(skb, ETH_HLEN);
476 ether_addr_copy(e_hdr->daddr, daddr);
477 ether_addr_copy(e_hdr->saddr, saddr);
478 e_hdr->type = htons(payload_length);
480 /* chop off the 802.11 CRC */
481 skb_trim(skb, skb->len - WLAN_CRC_LEN);
485 * Note that eth_type_trans() expects an skb w/ skb->data pointing
486 * at the MAC header, it then sets the following skb members:
490 * It then _returns_ the value that _we're_ supposed to stuff in
491 * skb->protocol. This is nuts.
493 skb->protocol = eth_type_trans(skb, netdev);
495 /* jkriegl: process signal and noise as set in hfa384x_int_rx() */
496 /* jkriegl: only process signal/noise if requested by iwspy */
497 if (wlandev->spy_number)
498 orinoco_spy_gather(wlandev, eth_hdr(skb)->h_source,
499 P80211SKB_RXMETA(skb));
501 /* Free the metadata */
502 p80211skb_rxmeta_detach(skb);
507 /*----------------------------------------------------------------
508 * p80211_stt_findproto
510 * Searches the 802.1h Selective Translation Table for a given
514 * proto protocol number (in host order) to search for.
517 * 1 - if the table is empty or a match is found.
518 * 0 - if the table is non-empty and a match is not found.
521 * May be called in interrupt or non-interrupt context
522 *----------------------------------------------------------------
524 int p80211_stt_findproto(u16 proto)
526 /* Always return found for now. This is the behavior used by the */
527 /* Zoom Win95 driver when 802.1h mode is selected */
528 /* TODO: If necessary, add an actual search we'll probably
529 * need this to match the CMAC's way of doing things.
530 * Need to do some testing to confirm.
533 if (proto == ETH_P_AARP) /* APPLETALK */
539 /*----------------------------------------------------------------
540 * p80211skb_rxmeta_detach
542 * Disconnects the frmmeta and rxmeta from an skb.
545 * wlandev The wlandev this skb belongs to.
546 * skb The skb we're attaching to.
549 * 0 on success, non-zero otherwise
552 * May be called in interrupt or non-interrupt context
553 *----------------------------------------------------------------
555 void p80211skb_rxmeta_detach(struct sk_buff *skb)
557 struct p80211_rxmeta *rxmeta;
558 struct p80211_frmmeta *frmmeta;
561 if (!skb) { /* bad skb */
562 pr_debug("Called w/ null skb.\n");
565 frmmeta = P80211SKB_FRMMETA(skb);
566 if (!frmmeta) { /* no magic */
567 pr_debug("Called w/ bad frmmeta magic.\n");
570 rxmeta = frmmeta->rx;
571 if (!rxmeta) { /* bad meta ptr */
572 pr_debug("Called w/ bad rxmeta ptr.\n");
580 memset(skb->cb, 0, sizeof(skb->cb));
583 /*----------------------------------------------------------------
584 * p80211skb_rxmeta_attach
586 * Allocates a p80211rxmeta structure, initializes it, and attaches
590 * wlandev The wlandev this skb belongs to.
591 * skb The skb we're attaching to.
594 * 0 on success, non-zero otherwise
597 * May be called in interrupt or non-interrupt context
598 *----------------------------------------------------------------
600 int p80211skb_rxmeta_attach(struct wlandevice *wlandev, struct sk_buff *skb)
603 struct p80211_rxmeta *rxmeta;
604 struct p80211_frmmeta *frmmeta;
606 /* If these already have metadata, we error out! */
607 if (P80211SKB_RXMETA(skb)) {
608 netdev_err(wlandev->netdev,
609 "%s: RXmeta already attached!\n", wlandev->name);
614 /* Allocate the rxmeta */
615 rxmeta = kzalloc(sizeof(*rxmeta), GFP_ATOMIC);
622 /* Initialize the rxmeta */
623 rxmeta->wlandev = wlandev;
624 rxmeta->hosttime = jiffies;
626 /* Overlay a frmmeta_t onto skb->cb */
627 memset(skb->cb, 0, sizeof(struct p80211_frmmeta));
628 frmmeta = (struct p80211_frmmeta *)(skb->cb);
629 frmmeta->magic = P80211_FRMMETA_MAGIC;
630 frmmeta->rx = rxmeta;
635 /*----------------------------------------------------------------
638 * Frees an entire p80211skb by checking and freeing the meta struct
639 * and then freeing the skb.
642 * wlandev The wlandev this skb belongs to.
643 * skb The skb we're attaching to.
646 * 0 on success, non-zero otherwise
649 * May be called in interrupt or non-interrupt context
650 *----------------------------------------------------------------
652 void p80211skb_free(struct wlandevice *wlandev, struct sk_buff *skb)
654 struct p80211_frmmeta *meta;
656 meta = P80211SKB_FRMMETA(skb);
657 if (meta && meta->rx)
658 p80211skb_rxmeta_detach(skb);
660 netdev_err(wlandev->netdev,
661 "Freeing an skb (%p) w/ no frmmeta.\n", skb);