2 * Original code based Host AP (software wireless LAN access point) driver
3 * for Intersil Prism2/2.5/3 - hostap.o module, common routines
5 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
7 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
8 * Copyright (c) 2004, Intel Corporation
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation. See README and COPYING for
16 #include <linux/compiler.h>
17 #include <linux/config.h>
18 #include <linux/errno.h>
19 #include <linux/if_arp.h>
20 #include <linux/in6.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/proc_fs.h>
27 #include <linux/skbuff.h>
28 #include <linux/slab.h>
29 #include <linux/tcp.h>
30 #include <linux/types.h>
31 #include <linux/version.h>
32 #include <linux/wireless.h>
33 #include <linux/etherdevice.h>
34 #include <asm/uaccess.h>
35 #include <linux/ctype.h>
37 #include <net/ieee80211.h>
39 static inline void ieee80211_monitor_rx(struct ieee80211_device *ieee,
41 struct ieee80211_rx_stats *rx_stats)
43 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
44 u16 fc = le16_to_cpu(hdr->frame_ctl);
47 skb->mac.raw = skb->data;
48 skb_pull(skb, ieee80211_get_hdrlen(fc));
49 skb->pkt_type = PACKET_OTHERHOST;
50 skb->protocol = __constant_htons(ETH_P_80211_RAW);
51 memset(skb->cb, 0, sizeof(skb->cb));
56 /* Called only as a tasklet (software IRQ) */
57 static struct ieee80211_frag_entry *
58 ieee80211_frag_cache_find(struct ieee80211_device *ieee, unsigned int seq,
59 unsigned int frag, u8 *src, u8 *dst)
61 struct ieee80211_frag_entry *entry;
64 for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
65 entry = &ieee->frag_cache[i];
66 if (entry->skb != NULL &&
67 time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
69 "expiring fragment cache entry "
70 "seq=%u last_frag=%u\n",
71 entry->seq, entry->last_frag);
72 dev_kfree_skb_any(entry->skb);
76 if (entry->skb != NULL && entry->seq == seq &&
77 (entry->last_frag + 1 == frag || frag == -1) &&
78 memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
79 memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
86 /* Called only as a tasklet (software IRQ) */
87 static struct sk_buff *
88 ieee80211_frag_cache_get(struct ieee80211_device *ieee,
89 struct ieee80211_hdr *hdr)
91 struct sk_buff *skb = NULL;
93 unsigned int frag, seq;
94 struct ieee80211_frag_entry *entry;
96 sc = le16_to_cpu(hdr->seq_ctl);
97 frag = WLAN_GET_SEQ_FRAG(sc);
98 seq = WLAN_GET_SEQ_SEQ(sc);
101 /* Reserve enough space to fit maximum frame length */
102 skb = dev_alloc_skb(ieee->dev->mtu +
103 sizeof(struct ieee80211_hdr) +
106 8 /* WEP */ + ETH_ALEN /* WDS */);
110 entry = &ieee->frag_cache[ieee->frag_next_idx];
111 ieee->frag_next_idx++;
112 if (ieee->frag_next_idx >= IEEE80211_FRAG_CACHE_LEN)
113 ieee->frag_next_idx = 0;
115 if (entry->skb != NULL)
116 dev_kfree_skb_any(entry->skb);
118 entry->first_frag_time = jiffies;
120 entry->last_frag = frag;
122 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
123 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
125 /* received a fragment of a frame for which the head fragment
126 * should have already been received */
127 entry = ieee80211_frag_cache_find(ieee, seq, frag, hdr->addr2,
130 entry->last_frag = frag;
139 /* Called only as a tasklet (software IRQ) */
140 static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
141 struct ieee80211_hdr *hdr)
145 struct ieee80211_frag_entry *entry;
147 sc = le16_to_cpu(hdr->seq_ctl);
148 seq = WLAN_GET_SEQ_SEQ(sc);
150 entry = ieee80211_frag_cache_find(ieee, seq, -1, hdr->addr2,
154 IEEE80211_DEBUG_FRAG(
155 "could not invalidate fragment cache "
156 "entry (seq=%u)\n", seq);
166 /* ieee80211_rx_frame_mgtmt
168 * Responsible for handling management control frames
170 * Called by ieee80211_rx */
172 ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
173 struct ieee80211_rx_stats *rx_stats, u16 type,
176 if (ieee->iw_mode == IW_MODE_MASTER) {
177 printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
181 hostap_update_sta_ps(ieee, (struct hostap_ieee80211_hdr *)
185 if (ieee->hostapd && type == WLAN_FC_TYPE_MGMT) {
186 if (stype == WLAN_FC_STYPE_BEACON &&
187 ieee->iw_mode == IW_MODE_MASTER) {
188 struct sk_buff *skb2;
189 /* Process beacon frames also in kernel driver to
190 * update STA(AP) table statistics */
191 skb2 = skb_clone(skb, GFP_ATOMIC);
193 hostap_rx(skb2->dev, skb2, rx_stats);
196 /* send management frames to the user space daemon for
198 ieee->apdevstats.rx_packets++;
199 ieee->apdevstats.rx_bytes += skb->len;
200 prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
204 if (ieee->iw_mode == IW_MODE_MASTER) {
205 if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
206 printk(KERN_DEBUG "%s: unknown management frame "
207 "(type=0x%02x, stype=0x%02x) dropped\n",
208 skb->dev->name, type, stype);
212 hostap_rx(skb->dev, skb, rx_stats);
216 printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
217 "received in non-Host AP mode\n", skb->dev->name);
223 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
224 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
225 static unsigned char rfc1042_header[] =
226 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
227 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
228 static unsigned char bridge_tunnel_header[] =
229 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
230 /* No encapsulation header if EtherType < 0x600 (=length) */
232 /* Called by ieee80211_rx_frame_decrypt */
233 static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
236 struct net_device *dev = ieee->dev;
238 struct ieee80211_hdr *hdr;
244 hdr = (struct ieee80211_hdr *) skb->data;
245 fc = le16_to_cpu(hdr->frame_ctl);
247 /* check that the frame is unicast frame to us */
248 if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
249 IEEE80211_FCTL_TODS &&
250 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
251 memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
252 /* ToDS frame with own addr BSSID and DA */
253 } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
254 IEEE80211_FCTL_FROMDS &&
255 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
256 /* FromDS frame with own addr as DA */
260 if (skb->len < 24 + 8)
263 /* check for port access entity Ethernet type */
264 pos = skb->data + 24;
265 ethertype = (pos[6] << 8) | pos[7];
266 if (ethertype == ETH_P_PAE)
272 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
274 ieee80211_rx_frame_decrypt(struct ieee80211_device* ieee, struct sk_buff *skb,
275 struct ieee80211_crypt_data *crypt)
277 struct ieee80211_hdr *hdr;
280 if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
283 hdr = (struct ieee80211_hdr *) skb->data;
284 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
286 #ifdef CONFIG_IEEE80211_CRYPT_TKIP
287 if (ieee->tkip_countermeasures &&
288 strcmp(crypt->ops->name, "TKIP") == 0) {
289 if (net_ratelimit()) {
290 printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
291 "received packet from " MAC_FMT "\n",
292 ieee->dev->name, MAC_ARG(hdr->addr2));
298 atomic_inc(&crypt->refcnt);
299 res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
300 atomic_dec(&crypt->refcnt);
302 IEEE80211_DEBUG_DROP(
303 "decryption failed (SA=" MAC_FMT
304 ") res=%d\n", MAC_ARG(hdr->addr2), res);
306 IEEE80211_DEBUG_DROP("Decryption failed ICV "
307 "mismatch (key %d)\n",
308 skb->data[hdrlen + 3] >> 6);
309 ieee->ieee_stats.rx_discards_undecryptable++;
317 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
319 ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device* ieee, struct sk_buff *skb,
320 int keyidx, struct ieee80211_crypt_data *crypt)
322 struct ieee80211_hdr *hdr;
325 if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
328 hdr = (struct ieee80211_hdr *) skb->data;
329 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
331 atomic_inc(&crypt->refcnt);
332 res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
333 atomic_dec(&crypt->refcnt);
335 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
336 " (SA=" MAC_FMT " keyidx=%d)\n",
337 ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
345 /* All received frames are sent to this function. @skb contains the frame in
346 * IEEE 802.11 format, i.e., in the format it was sent over air.
347 * This function is called only as a tasklet (software IRQ). */
348 int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
349 struct ieee80211_rx_stats *rx_stats)
351 struct net_device *dev = ieee->dev;
352 struct ieee80211_hdr *hdr;
354 u16 fc, type, stype, sc;
355 struct net_device_stats *stats;
360 struct net_device *wds = NULL;
361 struct sk_buff *skb2 = NULL;
362 struct net_device *wds = NULL;
363 int frame_authorized = 0;
364 int from_assoc_ap = 0;
369 struct ieee80211_crypt_data *crypt = NULL;
372 hdr = (struct ieee80211_hdr *)skb->data;
373 stats = &ieee->stats;
376 printk(KERN_INFO "%s: SKB length < 10\n",
381 fc = le16_to_cpu(hdr->frame_ctl);
382 type = WLAN_FC_GET_TYPE(fc);
383 stype = WLAN_FC_GET_STYPE(fc);
384 sc = le16_to_cpu(hdr->seq_ctl);
385 frag = WLAN_GET_SEQ_FRAG(sc);
386 hdrlen = ieee80211_get_hdrlen(fc);
389 #if WIRELESS_EXT > 15
390 /* Put this code here so that we avoid duplicating it in all
391 * Rx paths. - Jean II */
392 #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
393 /* If spy monitoring on */
394 if (iface->spy_data.spy_number > 0) {
395 struct iw_quality wstats;
396 wstats.level = rx_stats->signal;
397 wstats.noise = rx_stats->noise;
398 wstats.updated = 6; /* No qual value */
399 /* Update spy records */
400 wireless_spy_update(dev, hdr->addr2, &wstats);
402 #endif /* IW_WIRELESS_SPY */
403 #endif /* WIRELESS_EXT > 15 */
404 hostap_update_rx_stats(local->ap, hdr, rx_stats);
407 #if WIRELESS_EXT > 15
408 if (ieee->iw_mode == IW_MODE_MONITOR) {
409 ieee80211_monitor_rx(ieee, skb, rx_stats);
411 stats->rx_bytes += skb->len;
416 if (ieee->host_decrypt) {
418 if (skb->len >= hdrlen + 3)
419 idx = skb->data[hdrlen + 3] >> 6;
420 crypt = ieee->crypt[idx];
424 /* Use station specific key to override default keys if the
425 * receiver address is a unicast address ("individual RA"). If
426 * bcrx_sta_key parameter is set, station specific key is used
427 * even with broad/multicast targets (this is against IEEE
428 * 802.11, but makes it easier to use different keys with
429 * stations that do not support WEP key mapping). */
431 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
432 (void) hostap_handle_sta_crypto(local, hdr, &crypt,
436 /* allow NULL decrypt to indicate an station specific override
437 * for default encryption */
438 if (crypt && (crypt->ops == NULL ||
439 crypt->ops->decrypt_mpdu == NULL))
442 if (!crypt && (fc & IEEE80211_FCTL_WEP)) {
443 /* This seems to be triggered by some (multicast?)
444 * frames from other than current BSS, so just drop the
445 * frames silently instead of filling system log with
447 IEEE80211_DEBUG_DROP("Decryption failed (not set)"
448 " (SA=" MAC_FMT ")\n",
449 MAC_ARG(hdr->addr2));
450 ieee->ieee_stats.rx_discards_undecryptable++;
456 if (type != WLAN_FC_TYPE_DATA) {
457 if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
458 fc & IEEE80211_FCTL_WEP && ieee->host_decrypt &&
459 (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0)
461 printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
462 "from " MAC_FMT "\n", dev->name,
463 MAC_ARG(hdr->addr2));
464 /* TODO: could inform hostapd about this so that it
465 * could send auth failure report */
469 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
476 /* Data frame - extract src/dst addresses */
477 if (skb->len < IEEE80211_3ADDR_LEN)
480 switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
481 case IEEE80211_FCTL_FROMDS:
482 memcpy(dst, hdr->addr1, ETH_ALEN);
483 memcpy(src, hdr->addr3, ETH_ALEN);
485 case IEEE80211_FCTL_TODS:
486 memcpy(dst, hdr->addr3, ETH_ALEN);
487 memcpy(src, hdr->addr2, ETH_ALEN);
489 case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
490 if (skb->len < IEEE80211_4ADDR_LEN)
492 memcpy(dst, hdr->addr3, ETH_ALEN);
493 memcpy(src, hdr->addr4, ETH_ALEN);
496 memcpy(dst, hdr->addr1, ETH_ALEN);
497 memcpy(src, hdr->addr2, ETH_ALEN);
502 if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
505 skb->dev = dev = wds;
506 stats = hostap_get_stats(dev);
509 if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
510 (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == IEEE80211_FCTL_FROMDS &&
512 memcmp(hdr->addr2, ieee->assoc_ap_addr, ETH_ALEN) == 0) {
513 /* Frame from BSSID of the AP for which we are a client */
514 skb->dev = dev = ieee->stadev;
515 stats = hostap_get_stats(dev);
520 dev->last_rx = jiffies;
523 if ((ieee->iw_mode == IW_MODE_MASTER ||
524 ieee->iw_mode == IW_MODE_REPEAT) &&
526 switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
528 case AP_RX_CONTINUE_NOT_AUTHORIZED:
529 frame_authorized = 0;
532 frame_authorized = 1;
542 /* Nullfunc frames may have PS-bit set, so they must be passed to
543 * hostap_handle_sta_rx() before being dropped here. */
544 if (stype != IEEE80211_STYPE_DATA &&
545 stype != IEEE80211_STYPE_DATA_CFACK &&
546 stype != IEEE80211_STYPE_DATA_CFPOLL &&
547 stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
548 if (stype != IEEE80211_STYPE_NULLFUNC)
549 IEEE80211_DEBUG_DROP(
550 "RX: dropped data frame "
551 "with no data (type=0x%02x, "
552 "subtype=0x%02x, len=%d)\n",
553 type, stype, skb->len);
557 /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
559 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
560 (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
563 hdr = (struct ieee80211_hdr *) skb->data;
565 /* skb: hdr + (possibly fragmented) plaintext payload */
566 // PR: FIXME: hostap has additional conditions in the "if" below:
567 // ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
568 if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
570 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
571 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
574 IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
575 "Rx cannot get skb from fragment "
576 "cache (morefrag=%d seq=%u frag=%u)\n",
577 (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
578 WLAN_GET_SEQ_SEQ(sc), frag);
586 if (frag_skb->tail + flen > frag_skb->end) {
587 printk(KERN_WARNING "%s: host decrypted and "
588 "reassembled frame did not fit skb\n",
590 ieee80211_frag_cache_invalidate(ieee, hdr);
595 /* copy first fragment (including full headers) into
596 * beginning of the fragment cache skb */
597 memcpy(skb_put(frag_skb, flen), skb->data, flen);
599 /* append frame payload to the end of the fragment
601 memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
604 dev_kfree_skb_any(skb);
607 if (fc & IEEE80211_FCTL_MOREFRAGS) {
608 /* more fragments expected - leave the skb in fragment
609 * cache for now; it will be delivered to upper layers
610 * after all fragments have been received */
614 /* this was the last fragment and the frame will be
615 * delivered, so remove skb from fragment cache */
617 hdr = (struct ieee80211_hdr *) skb->data;
618 ieee80211_frag_cache_invalidate(ieee, hdr);
621 /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
622 * encrypted/authenticated */
623 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
624 ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
627 hdr = (struct ieee80211_hdr *) skb->data;
628 if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep) {
629 if (/*ieee->ieee802_1x &&*/
630 ieee80211_is_eapol_frame(ieee, skb)) {
631 #ifdef CONFIG_IEEE80211_DEBUG
632 /* pass unencrypted EAPOL frames even if encryption is
634 struct eapol *eap = (struct eapol *)(skb->data +
636 IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
637 eap_get_type(eap->type));
640 IEEE80211_DEBUG_DROP(
641 "encryption configured, but RX "
642 "frame not encrypted (SA=" MAC_FMT ")\n",
643 MAC_ARG(hdr->addr2));
648 #ifdef CONFIG_IEEE80211_DEBUG
649 if (crypt && !(fc & IEEE80211_FCTL_WEP) &&
650 ieee80211_is_eapol_frame(ieee, skb)) {
651 struct eapol *eap = (struct eapol *)(skb->data +
653 IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
654 eap_get_type(eap->type));
658 if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep &&
659 !ieee80211_is_eapol_frame(ieee, skb)) {
660 IEEE80211_DEBUG_DROP(
661 "dropped unencrypted RX data "
662 "frame from " MAC_FMT
663 " (drop_unencrypted=1)\n",
664 MAC_ARG(hdr->addr2));
668 /* skb: hdr + (possible reassembled) full plaintext payload */
670 payload = skb->data + hdrlen;
671 ethertype = (payload[6] << 8) | payload[7];
674 /* If IEEE 802.1X is used, check whether the port is authorized to send
675 * the received frame. */
676 if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
677 if (ethertype == ETH_P_PAE) {
678 printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
680 if (ieee->hostapd && ieee->apdev) {
681 /* Send IEEE 802.1X frames to the user
682 * space daemon for processing */
683 prism2_rx_80211(ieee->apdev, skb, rx_stats,
685 ieee->apdevstats.rx_packets++;
686 ieee->apdevstats.rx_bytes += skb->len;
689 } else if (!frame_authorized) {
690 printk(KERN_DEBUG "%s: dropped frame from "
691 "unauthorized port (IEEE 802.1X): "
692 "ethertype=0x%04x\n",
693 dev->name, ethertype);
699 /* convert hdr + possible LLC headers into Ethernet header */
700 if (skb->len - hdrlen >= 8 &&
701 ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
702 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
703 memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
704 /* remove RFC1042 or Bridge-Tunnel encapsulation and
705 * replace EtherType */
706 skb_pull(skb, hdrlen + SNAP_SIZE);
707 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
708 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
711 /* Leave Ethernet header part of hdr and full payload */
712 skb_pull(skb, hdrlen);
713 len = htons(skb->len);
714 memcpy(skb_push(skb, 2), &len, 2);
715 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
716 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
720 if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
721 IEEE80211_FCTL_TODS) &&
722 skb->len >= ETH_HLEN + ETH_ALEN) {
723 /* Non-standard frame: get addr4 from its bogus location after
725 memcpy(skb->data + ETH_ALEN,
726 skb->data + skb->len - ETH_ALEN, ETH_ALEN);
727 skb_trim(skb, skb->len - ETH_ALEN);
732 stats->rx_bytes += skb->len;
735 if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
736 ieee->ap->bridge_packets) {
738 /* copy multicast frame both to the higher layers and
739 * to the wireless media */
740 ieee->ap->bridged_multicast++;
741 skb2 = skb_clone(skb, GFP_ATOMIC);
743 printk(KERN_DEBUG "%s: skb_clone failed for "
744 "multicast frame\n", dev->name);
745 } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
746 /* send frame directly to the associated STA using
747 * wireless media and not passing to higher layers */
748 ieee->ap->bridged_unicast++;
755 /* send to wireless media */
756 skb2->protocol = __constant_htons(ETH_P_802_3);
757 skb2->mac.raw = skb2->nh.raw = skb2->data;
758 /* skb2->nh.raw = skb2->data + ETH_HLEN; */
760 dev_queue_xmit(skb2);
766 skb->protocol = eth_type_trans(skb, dev);
767 memset(skb->cb, 0, sizeof(skb->cb));
769 skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
776 hostap_handle_sta_release(sta);
783 /* Returning 0 indicates to caller that we have not handled the SKB--
784 * so it is still allocated and can be used again by underlying
785 * hardware as a DMA target */
789 #define MGMT_FRAME_FIXED_PART_LENGTH 0x24
791 static inline int ieee80211_is_ofdm_rate(u8 rate)
793 switch (rate & ~IEEE80211_BASIC_RATE_MASK) {
794 case IEEE80211_OFDM_RATE_6MB:
795 case IEEE80211_OFDM_RATE_9MB:
796 case IEEE80211_OFDM_RATE_12MB:
797 case IEEE80211_OFDM_RATE_18MB:
798 case IEEE80211_OFDM_RATE_24MB:
799 case IEEE80211_OFDM_RATE_36MB:
800 case IEEE80211_OFDM_RATE_48MB:
801 case IEEE80211_OFDM_RATE_54MB:
808 static inline int ieee80211_network_init(
809 struct ieee80211_device *ieee,
810 struct ieee80211_probe_response *beacon,
811 struct ieee80211_network *network,
812 struct ieee80211_rx_stats *stats)
814 #ifdef CONFIG_IEEE80211_DEBUG
818 struct ieee80211_info_element *info_element;
822 /* Pull out fixed field data */
823 memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
824 network->capability = beacon->capability;
825 network->last_scanned = jiffies;
826 network->time_stamp[0] = beacon->time_stamp[0];
827 network->time_stamp[1] = beacon->time_stamp[1];
828 network->beacon_interval = beacon->beacon_interval;
829 /* Where to pull this? beacon->listen_interval;*/
830 network->listen_interval = 0x0A;
831 network->rates_len = network->rates_ex_len = 0;
832 network->last_associate = 0;
833 network->ssid_len = 0;
835 network->atim_window = 0;
837 if (stats->freq == IEEE80211_52GHZ_BAND) {
838 /* for A band (No DS info) */
839 network->channel = stats->received_channel;
841 network->flags |= NETWORK_HAS_CCK;
843 network->wpa_ie_len = 0;
844 network->rsn_ie_len = 0;
846 info_element = &beacon->info_element;
847 left = stats->len - ((void *)info_element - (void *)beacon);
848 while (left >= sizeof(struct ieee80211_info_element_hdr)) {
849 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
850 IEEE80211_DEBUG_SCAN("SCAN: parse failed: info_element->len + 2 > left : info_element->len+2=%d left=%d.\n",
851 info_element->len + sizeof(struct ieee80211_info_element),
856 switch (info_element->id) {
858 if (ieee80211_is_empty_essid(info_element->data,
859 info_element->len)) {
860 network->flags |= NETWORK_EMPTY_ESSID;
864 network->ssid_len = min(info_element->len,
865 (u8)IW_ESSID_MAX_SIZE);
866 memcpy(network->ssid, info_element->data, network->ssid_len);
867 if (network->ssid_len < IW_ESSID_MAX_SIZE)
868 memset(network->ssid + network->ssid_len, 0,
869 IW_ESSID_MAX_SIZE - network->ssid_len);
871 IEEE80211_DEBUG_SCAN("MFIE_TYPE_SSID: '%s' len=%d.\n",
872 network->ssid, network->ssid_len);
875 case MFIE_TYPE_RATES:
876 #ifdef CONFIG_IEEE80211_DEBUG
879 network->rates_len = min(info_element->len, MAX_RATES_LENGTH);
880 for (i = 0; i < network->rates_len; i++) {
881 network->rates[i] = info_element->data[i];
882 #ifdef CONFIG_IEEE80211_DEBUG
883 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
885 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
886 network->flags |= NETWORK_HAS_OFDM;
887 if (info_element->data[i] &
888 IEEE80211_BASIC_RATE_MASK)
894 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES: '%s' (%d)\n",
895 rates_str, network->rates_len);
898 case MFIE_TYPE_RATES_EX:
899 #ifdef CONFIG_IEEE80211_DEBUG
902 network->rates_ex_len = min(info_element->len, MAX_RATES_EX_LENGTH);
903 for (i = 0; i < network->rates_ex_len; i++) {
904 network->rates_ex[i] = info_element->data[i];
905 #ifdef CONFIG_IEEE80211_DEBUG
906 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
908 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
909 network->flags |= NETWORK_HAS_OFDM;
910 if (info_element->data[i] &
911 IEEE80211_BASIC_RATE_MASK)
917 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
918 rates_str, network->rates_ex_len);
921 case MFIE_TYPE_DS_SET:
922 IEEE80211_DEBUG_SCAN("MFIE_TYPE_DS_SET: %d\n",
923 info_element->data[0]);
924 if (stats->freq == IEEE80211_24GHZ_BAND)
925 network->channel = info_element->data[0];
928 case MFIE_TYPE_FH_SET:
929 IEEE80211_DEBUG_SCAN("MFIE_TYPE_FH_SET: ignored\n");
932 case MFIE_TYPE_CF_SET:
933 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CF_SET: ignored\n");
937 IEEE80211_DEBUG_SCAN("MFIE_TYPE_TIM: ignored\n");
940 case MFIE_TYPE_IBSS_SET:
941 IEEE80211_DEBUG_SCAN("MFIE_TYPE_IBSS_SET: ignored\n");
944 case MFIE_TYPE_CHALLENGE:
945 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CHALLENGE: ignored\n");
948 case MFIE_TYPE_GENERIC:
949 IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n",
951 if (info_element->len >= 4 &&
952 info_element->data[0] == 0x00 &&
953 info_element->data[1] == 0x50 &&
954 info_element->data[2] == 0xf2 &&
955 info_element->data[3] == 0x01) {
956 network->wpa_ie_len = min(info_element->len + 2,
958 memcpy(network->wpa_ie, info_element,
959 network->wpa_ie_len);
964 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RSN: %d bytes\n",
966 network->rsn_ie_len = min(info_element->len + 2,
968 memcpy(network->rsn_ie, info_element,
969 network->rsn_ie_len);
973 IEEE80211_DEBUG_SCAN("unsupported IE %d\n",
978 left -= sizeof(struct ieee80211_info_element_hdr) +
980 info_element = (struct ieee80211_info_element *)
981 &info_element->data[info_element->len];
985 if (stats->freq == IEEE80211_52GHZ_BAND)
986 network->mode = IEEE_A;
988 if (network->flags & NETWORK_HAS_OFDM)
989 network->mode |= IEEE_G;
990 if (network->flags & NETWORK_HAS_CCK)
991 network->mode |= IEEE_B;
994 if (network->mode == 0) {
995 IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
997 escape_essid(network->ssid,
999 MAC_ARG(network->bssid));
1003 if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
1004 network->flags |= NETWORK_EMPTY_ESSID;
1006 memcpy(&network->stats, stats, sizeof(network->stats));
1011 static inline int is_same_network(struct ieee80211_network *src,
1012 struct ieee80211_network *dst)
1014 /* A network is only a duplicate if the channel, BSSID, and ESSID
1015 * all match. We treat all <hidden> with the same BSSID and channel
1017 return ((src->ssid_len == dst->ssid_len) &&
1018 (src->channel == dst->channel) &&
1019 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
1020 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1023 static inline void update_network(struct ieee80211_network *dst,
1024 struct ieee80211_network *src)
1026 memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
1027 dst->capability = src->capability;
1028 memcpy(dst->rates, src->rates, src->rates_len);
1029 dst->rates_len = src->rates_len;
1030 memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1031 dst->rates_ex_len = src->rates_ex_len;
1033 dst->mode = src->mode;
1034 dst->flags = src->flags;
1035 dst->time_stamp[0] = src->time_stamp[0];
1036 dst->time_stamp[1] = src->time_stamp[1];
1038 dst->beacon_interval = src->beacon_interval;
1039 dst->listen_interval = src->listen_interval;
1040 dst->atim_window = src->atim_window;
1042 memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1043 dst->wpa_ie_len = src->wpa_ie_len;
1044 memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1045 dst->rsn_ie_len = src->rsn_ie_len;
1047 dst->last_scanned = jiffies;
1048 /* dst->last_associate is not overwritten */
1051 static inline void ieee80211_process_probe_response(
1052 struct ieee80211_device *ieee,
1053 struct ieee80211_probe_response *beacon,
1054 struct ieee80211_rx_stats *stats)
1056 struct ieee80211_network network;
1057 struct ieee80211_network *target;
1058 struct ieee80211_network *oldest = NULL;
1059 #ifdef CONFIG_IEEE80211_DEBUG
1060 struct ieee80211_info_element *info_element = &beacon->info_element;
1062 unsigned long flags;
1064 IEEE80211_DEBUG_SCAN(
1065 "'%s' (" MAC_FMT "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1066 escape_essid(info_element->data, info_element->len),
1067 MAC_ARG(beacon->header.addr3),
1068 (beacon->capability & (1<<0xf)) ? '1' : '0',
1069 (beacon->capability & (1<<0xe)) ? '1' : '0',
1070 (beacon->capability & (1<<0xd)) ? '1' : '0',
1071 (beacon->capability & (1<<0xc)) ? '1' : '0',
1072 (beacon->capability & (1<<0xb)) ? '1' : '0',
1073 (beacon->capability & (1<<0xa)) ? '1' : '0',
1074 (beacon->capability & (1<<0x9)) ? '1' : '0',
1075 (beacon->capability & (1<<0x8)) ? '1' : '0',
1076 (beacon->capability & (1<<0x7)) ? '1' : '0',
1077 (beacon->capability & (1<<0x6)) ? '1' : '0',
1078 (beacon->capability & (1<<0x5)) ? '1' : '0',
1079 (beacon->capability & (1<<0x4)) ? '1' : '0',
1080 (beacon->capability & (1<<0x3)) ? '1' : '0',
1081 (beacon->capability & (1<<0x2)) ? '1' : '0',
1082 (beacon->capability & (1<<0x1)) ? '1' : '0',
1083 (beacon->capability & (1<<0x0)) ? '1' : '0');
1085 if (ieee80211_network_init(ieee, beacon, &network, stats)) {
1086 IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
1087 escape_essid(info_element->data,
1089 MAC_ARG(beacon->header.addr3),
1090 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1091 IEEE80211_STYPE_PROBE_RESP ?
1092 "PROBE RESPONSE" : "BEACON");
1096 /* The network parsed correctly -- so now we scan our known networks
1097 * to see if we can find it in our list.
1099 * NOTE: This search is definitely not optimized. Once its doing
1100 * the "right thing" we'll optimize it for efficiency if
1103 /* Search for this entry in the list and update it if it is
1106 spin_lock_irqsave(&ieee->lock, flags);
1108 list_for_each_entry(target, &ieee->network_list, list) {
1109 if (is_same_network(target, &network))
1112 if ((oldest == NULL) ||
1113 (target->last_scanned < oldest->last_scanned))
1117 /* If we didn't find a match, then get a new network slot to initialize
1118 * with this beacon's information */
1119 if (&target->list == &ieee->network_list) {
1120 if (list_empty(&ieee->network_free_list)) {
1121 /* If there are no more slots, expire the oldest */
1122 list_del(&oldest->list);
1124 IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
1126 escape_essid(target->ssid,
1128 MAC_ARG(target->bssid));
1130 /* Otherwise just pull from the free list */
1131 target = list_entry(ieee->network_free_list.next,
1132 struct ieee80211_network, list);
1133 list_del(ieee->network_free_list.next);
1137 #ifdef CONFIG_IEEE80211_DEBUG
1138 IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
1139 escape_essid(network.ssid,
1141 MAC_ARG(network.bssid),
1142 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1143 IEEE80211_STYPE_PROBE_RESP ?
1144 "PROBE RESPONSE" : "BEACON");
1146 memcpy(target, &network, sizeof(*target));
1147 list_add_tail(&target->list, &ieee->network_list);
1149 IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
1150 escape_essid(target->ssid,
1152 MAC_ARG(target->bssid),
1153 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1154 IEEE80211_STYPE_PROBE_RESP ?
1155 "PROBE RESPONSE" : "BEACON");
1156 update_network(target, &network);
1159 spin_unlock_irqrestore(&ieee->lock, flags);
1162 void ieee80211_rx_mgt(struct ieee80211_device *ieee,
1163 struct ieee80211_hdr *header,
1164 struct ieee80211_rx_stats *stats)
1166 switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
1167 case IEEE80211_STYPE_ASSOC_RESP:
1168 IEEE80211_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
1169 WLAN_FC_GET_STYPE(header->frame_ctl));
1172 case IEEE80211_STYPE_REASSOC_RESP:
1173 IEEE80211_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
1174 WLAN_FC_GET_STYPE(header->frame_ctl));
1177 case IEEE80211_STYPE_PROBE_RESP:
1178 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1179 WLAN_FC_GET_STYPE(header->frame_ctl));
1180 IEEE80211_DEBUG_SCAN("Probe response\n");
1181 ieee80211_process_probe_response(
1182 ieee, (struct ieee80211_probe_response *)header, stats);
1185 case IEEE80211_STYPE_BEACON:
1186 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
1187 WLAN_FC_GET_STYPE(header->frame_ctl));
1188 IEEE80211_DEBUG_SCAN("Beacon\n");
1189 ieee80211_process_probe_response(
1190 ieee, (struct ieee80211_probe_response *)header, stats);
1194 IEEE80211_DEBUG_MGMT("received UNKNOWN (%d)\n",
1195 WLAN_FC_GET_STYPE(header->frame_ctl));
1196 IEEE80211_WARNING("%s: Unknown management packet: %d\n",
1198 WLAN_FC_GET_STYPE(header->frame_ctl));
1204 EXPORT_SYMBOL(ieee80211_rx_mgt);
1205 EXPORT_SYMBOL(ieee80211_rx);