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/pci.h>
27 #include <linux/proc_fs.h>
28 #include <linux/skbuff.h>
29 #include <linux/slab.h>
30 #include <linux/tcp.h>
31 #include <linux/types.h>
32 #include <linux/version.h>
33 #include <linux/wireless.h>
34 #include <linux/etherdevice.h>
35 #include <asm/uaccess.h>
36 #include <linux/ctype.h>
38 #include <net/ieee80211.h>
40 static inline void ieee80211_monitor_rx(struct ieee80211_device *ieee,
42 struct ieee80211_rx_stats *rx_stats)
44 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
45 u16 fc = le16_to_cpu(hdr->frame_ctl);
48 skb->mac.raw = skb->data;
49 skb_pull(skb, ieee80211_get_hdrlen(fc));
50 skb->pkt_type = PACKET_OTHERHOST;
51 skb->protocol = __constant_htons(ETH_P_80211_RAW);
52 memset(skb->cb, 0, sizeof(skb->cb));
57 /* Called only as a tasklet (software IRQ) */
58 static struct ieee80211_frag_entry *
59 ieee80211_frag_cache_find(struct ieee80211_device *ieee, unsigned int seq,
60 unsigned int frag, u8 *src, u8 *dst)
62 struct ieee80211_frag_entry *entry;
65 for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
66 entry = &ieee->frag_cache[i];
67 if (entry->skb != NULL &&
68 time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
70 "expiring fragment cache entry "
71 "seq=%u last_frag=%u\n",
72 entry->seq, entry->last_frag);
73 dev_kfree_skb_any(entry->skb);
77 if (entry->skb != NULL && entry->seq == seq &&
78 (entry->last_frag + 1 == frag || frag == -1) &&
79 memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
80 memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
87 /* Called only as a tasklet (software IRQ) */
88 static struct sk_buff *
89 ieee80211_frag_cache_get(struct ieee80211_device *ieee,
90 struct ieee80211_hdr *hdr)
92 struct sk_buff *skb = NULL;
94 unsigned int frag, seq;
95 struct ieee80211_frag_entry *entry;
97 sc = le16_to_cpu(hdr->seq_ctl);
98 frag = WLAN_GET_SEQ_FRAG(sc);
99 seq = WLAN_GET_SEQ_SEQ(sc);
102 /* Reserve enough space to fit maximum frame length */
103 skb = dev_alloc_skb(ieee->dev->mtu +
104 sizeof(struct ieee80211_hdr) +
107 8 /* WEP */ + ETH_ALEN /* WDS */);
111 entry = &ieee->frag_cache[ieee->frag_next_idx];
112 ieee->frag_next_idx++;
113 if (ieee->frag_next_idx >= IEEE80211_FRAG_CACHE_LEN)
114 ieee->frag_next_idx = 0;
116 if (entry->skb != NULL)
117 dev_kfree_skb_any(entry->skb);
119 entry->first_frag_time = jiffies;
121 entry->last_frag = frag;
123 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
124 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
126 /* received a fragment of a frame for which the head fragment
127 * should have already been received */
128 entry = ieee80211_frag_cache_find(ieee, seq, frag, hdr->addr2,
131 entry->last_frag = frag;
140 /* Called only as a tasklet (software IRQ) */
141 static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
142 struct ieee80211_hdr *hdr)
146 struct ieee80211_frag_entry *entry;
148 sc = le16_to_cpu(hdr->seq_ctl);
149 seq = WLAN_GET_SEQ_SEQ(sc);
151 entry = ieee80211_frag_cache_find(ieee, seq, -1, hdr->addr2,
155 IEEE80211_DEBUG_FRAG(
156 "could not invalidate fragment cache "
157 "entry (seq=%u)\n", seq);
167 /* ieee80211_rx_frame_mgtmt
169 * Responsible for handling management control frames
171 * Called by ieee80211_rx */
173 ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
174 struct ieee80211_rx_stats *rx_stats, u16 type,
177 if (ieee->iw_mode == IW_MODE_MASTER) {
178 printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
182 hostap_update_sta_ps(ieee, (struct hostap_ieee80211_hdr *)
186 if (ieee->hostapd && type == WLAN_FC_TYPE_MGMT) {
187 if (stype == WLAN_FC_STYPE_BEACON &&
188 ieee->iw_mode == IW_MODE_MASTER) {
189 struct sk_buff *skb2;
190 /* Process beacon frames also in kernel driver to
191 * update STA(AP) table statistics */
192 skb2 = skb_clone(skb, GFP_ATOMIC);
194 hostap_rx(skb2->dev, skb2, rx_stats);
197 /* send management frames to the user space daemon for
199 ieee->apdevstats.rx_packets++;
200 ieee->apdevstats.rx_bytes += skb->len;
201 prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
205 if (ieee->iw_mode == IW_MODE_MASTER) {
206 if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
207 printk(KERN_DEBUG "%s: unknown management frame "
208 "(type=0x%02x, stype=0x%02x) dropped\n",
209 skb->dev->name, type, stype);
213 hostap_rx(skb->dev, skb, rx_stats);
217 printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
218 "received in non-Host AP mode\n", skb->dev->name);
224 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
225 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
226 static unsigned char rfc1042_header[] =
227 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
228 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
229 static unsigned char bridge_tunnel_header[] =
230 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
231 /* No encapsulation header if EtherType < 0x600 (=length) */
233 /* Called by ieee80211_rx_frame_decrypt */
234 static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
237 struct net_device *dev = ieee->dev;
239 struct ieee80211_hdr *hdr;
245 hdr = (struct ieee80211_hdr *) skb->data;
246 fc = le16_to_cpu(hdr->frame_ctl);
248 /* check that the frame is unicast frame to us */
249 if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
250 IEEE80211_FCTL_TODS &&
251 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
252 memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
253 /* ToDS frame with own addr BSSID and DA */
254 } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
255 IEEE80211_FCTL_FROMDS &&
256 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
257 /* FromDS frame with own addr as DA */
261 if (skb->len < 24 + 8)
264 /* check for port access entity Ethernet type */
265 pos = skb->data + 24;
266 ethertype = (pos[6] << 8) | pos[7];
267 if (ethertype == ETH_P_PAE)
273 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
275 ieee80211_rx_frame_decrypt(struct ieee80211_device* ieee, struct sk_buff *skb,
276 struct ieee80211_crypt_data *crypt)
278 struct ieee80211_hdr *hdr;
281 if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
284 hdr = (struct ieee80211_hdr *) skb->data;
285 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
287 #ifdef CONFIG_IEEE80211_CRYPT_TKIP
288 if (ieee->tkip_countermeasures &&
289 strcmp(crypt->ops->name, "TKIP") == 0) {
290 if (net_ratelimit()) {
291 printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
292 "received packet from " MAC_FMT "\n",
293 ieee->dev->name, MAC_ARG(hdr->addr2));
299 atomic_inc(&crypt->refcnt);
300 res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
301 atomic_dec(&crypt->refcnt);
303 IEEE80211_DEBUG_DROP(
304 "decryption failed (SA=" MAC_FMT
305 ") res=%d\n", MAC_ARG(hdr->addr2), res);
307 IEEE80211_DEBUG_DROP("Decryption failed ICV "
308 "mismatch (key %d)\n",
309 skb->data[hdrlen + 3] >> 6);
310 ieee->ieee_stats.rx_discards_undecryptable++;
318 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
320 ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device* ieee, struct sk_buff *skb,
321 int keyidx, struct ieee80211_crypt_data *crypt)
323 struct ieee80211_hdr *hdr;
326 if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
329 hdr = (struct ieee80211_hdr *) skb->data;
330 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
332 atomic_inc(&crypt->refcnt);
333 res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
334 atomic_dec(&crypt->refcnt);
336 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
337 " (SA=" MAC_FMT " keyidx=%d)\n",
338 ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
346 /* All received frames are sent to this function. @skb contains the frame in
347 * IEEE 802.11 format, i.e., in the format it was sent over air.
348 * This function is called only as a tasklet (software IRQ). */
349 int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
350 struct ieee80211_rx_stats *rx_stats)
352 struct net_device *dev = ieee->dev;
353 struct ieee80211_hdr *hdr;
355 u16 fc, type, stype, sc;
356 struct net_device_stats *stats;
361 struct net_device *wds = NULL;
362 struct sk_buff *skb2 = NULL;
363 struct net_device *wds = NULL;
364 int frame_authorized = 0;
365 int from_assoc_ap = 0;
370 struct ieee80211_crypt_data *crypt = NULL;
373 hdr = (struct ieee80211_hdr *)skb->data;
374 stats = &ieee->stats;
377 printk(KERN_INFO "%s: SKB length < 10\n",
382 fc = le16_to_cpu(hdr->frame_ctl);
383 type = WLAN_FC_GET_TYPE(fc);
384 stype = WLAN_FC_GET_STYPE(fc);
385 sc = le16_to_cpu(hdr->seq_ctl);
386 frag = WLAN_GET_SEQ_FRAG(sc);
387 hdrlen = ieee80211_get_hdrlen(fc);
390 #if WIRELESS_EXT > 15
391 /* Put this code here so that we avoid duplicating it in all
392 * Rx paths. - Jean II */
393 #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
394 /* If spy monitoring on */
395 if (iface->spy_data.spy_number > 0) {
396 struct iw_quality wstats;
397 wstats.level = rx_stats->signal;
398 wstats.noise = rx_stats->noise;
399 wstats.updated = 6; /* No qual value */
400 /* Update spy records */
401 wireless_spy_update(dev, hdr->addr2, &wstats);
403 #endif /* IW_WIRELESS_SPY */
404 #endif /* WIRELESS_EXT > 15 */
405 hostap_update_rx_stats(local->ap, hdr, rx_stats);
408 #if WIRELESS_EXT > 15
409 if (ieee->iw_mode == IW_MODE_MONITOR) {
410 ieee80211_monitor_rx(ieee, skb, rx_stats);
412 stats->rx_bytes += skb->len;
417 if (ieee->host_decrypt) {
419 if (skb->len >= hdrlen + 3)
420 idx = skb->data[hdrlen + 3] >> 6;
421 crypt = ieee->crypt[idx];
425 /* Use station specific key to override default keys if the
426 * receiver address is a unicast address ("individual RA"). If
427 * bcrx_sta_key parameter is set, station specific key is used
428 * even with broad/multicast targets (this is against IEEE
429 * 802.11, but makes it easier to use different keys with
430 * stations that do not support WEP key mapping). */
432 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
433 (void) hostap_handle_sta_crypto(local, hdr, &crypt,
437 /* allow NULL decrypt to indicate an station specific override
438 * for default encryption */
439 if (crypt && (crypt->ops == NULL ||
440 crypt->ops->decrypt_mpdu == NULL))
443 if (!crypt && (fc & IEEE80211_FCTL_WEP)) {
444 /* This seems to be triggered by some (multicast?)
445 * frames from other than current BSS, so just drop the
446 * frames silently instead of filling system log with
448 IEEE80211_DEBUG_DROP("Decryption failed (not set)"
449 " (SA=" MAC_FMT ")\n",
450 MAC_ARG(hdr->addr2));
451 ieee->ieee_stats.rx_discards_undecryptable++;
457 if (type != WLAN_FC_TYPE_DATA) {
458 if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
459 fc & IEEE80211_FCTL_WEP && ieee->host_decrypt &&
460 (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0)
462 printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
463 "from " MAC_FMT "\n", dev->name,
464 MAC_ARG(hdr->addr2));
465 /* TODO: could inform hostapd about this so that it
466 * could send auth failure report */
470 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
477 /* Data frame - extract src/dst addresses */
478 if (skb->len < IEEE80211_3ADDR_LEN)
481 switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
482 case IEEE80211_FCTL_FROMDS:
483 memcpy(dst, hdr->addr1, ETH_ALEN);
484 memcpy(src, hdr->addr3, ETH_ALEN);
486 case IEEE80211_FCTL_TODS:
487 memcpy(dst, hdr->addr3, ETH_ALEN);
488 memcpy(src, hdr->addr2, ETH_ALEN);
490 case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
491 if (skb->len < IEEE80211_4ADDR_LEN)
493 memcpy(dst, hdr->addr3, ETH_ALEN);
494 memcpy(src, hdr->addr4, ETH_ALEN);
497 memcpy(dst, hdr->addr1, ETH_ALEN);
498 memcpy(src, hdr->addr2, ETH_ALEN);
503 if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
506 skb->dev = dev = wds;
507 stats = hostap_get_stats(dev);
510 if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
511 (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == IEEE80211_FCTL_FROMDS &&
513 memcmp(hdr->addr2, ieee->assoc_ap_addr, ETH_ALEN) == 0) {
514 /* Frame from BSSID of the AP for which we are a client */
515 skb->dev = dev = ieee->stadev;
516 stats = hostap_get_stats(dev);
521 dev->last_rx = jiffies;
524 if ((ieee->iw_mode == IW_MODE_MASTER ||
525 ieee->iw_mode == IW_MODE_REPEAT) &&
527 switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
529 case AP_RX_CONTINUE_NOT_AUTHORIZED:
530 frame_authorized = 0;
533 frame_authorized = 1;
543 /* Nullfunc frames may have PS-bit set, so they must be passed to
544 * hostap_handle_sta_rx() before being dropped here. */
545 if (stype != IEEE80211_STYPE_DATA &&
546 stype != IEEE80211_STYPE_DATA_CFACK &&
547 stype != IEEE80211_STYPE_DATA_CFPOLL &&
548 stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
549 if (stype != IEEE80211_STYPE_NULLFUNC)
550 IEEE80211_DEBUG_DROP(
551 "RX: dropped data frame "
552 "with no data (type=0x%02x, "
553 "subtype=0x%02x, len=%d)\n",
554 type, stype, skb->len);
558 /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
560 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
561 (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
564 hdr = (struct ieee80211_hdr *) skb->data;
566 /* skb: hdr + (possibly fragmented) plaintext payload */
567 // PR: FIXME: hostap has additional conditions in the "if" below:
568 // ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
569 if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
571 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
572 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
575 IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
576 "Rx cannot get skb from fragment "
577 "cache (morefrag=%d seq=%u frag=%u)\n",
578 (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
579 WLAN_GET_SEQ_SEQ(sc), frag);
587 if (frag_skb->tail + flen > frag_skb->end) {
588 printk(KERN_WARNING "%s: host decrypted and "
589 "reassembled frame did not fit skb\n",
591 ieee80211_frag_cache_invalidate(ieee, hdr);
596 /* copy first fragment (including full headers) into
597 * beginning of the fragment cache skb */
598 memcpy(skb_put(frag_skb, flen), skb->data, flen);
600 /* append frame payload to the end of the fragment
602 memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
605 dev_kfree_skb_any(skb);
608 if (fc & IEEE80211_FCTL_MOREFRAGS) {
609 /* more fragments expected - leave the skb in fragment
610 * cache for now; it will be delivered to upper layers
611 * after all fragments have been received */
615 /* this was the last fragment and the frame will be
616 * delivered, so remove skb from fragment cache */
618 hdr = (struct ieee80211_hdr *) skb->data;
619 ieee80211_frag_cache_invalidate(ieee, hdr);
622 /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
623 * encrypted/authenticated */
624 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
625 ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
628 hdr = (struct ieee80211_hdr *) skb->data;
629 if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep) {
630 if (/*ieee->ieee802_1x &&*/
631 ieee80211_is_eapol_frame(ieee, skb)) {
632 #ifdef CONFIG_IEEE80211_DEBUG
633 /* pass unencrypted EAPOL frames even if encryption is
635 struct eapol *eap = (struct eapol *)(skb->data +
637 IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
638 eap_get_type(eap->type));
641 IEEE80211_DEBUG_DROP(
642 "encryption configured, but RX "
643 "frame not encrypted (SA=" MAC_FMT ")\n",
644 MAC_ARG(hdr->addr2));
649 #ifdef CONFIG_IEEE80211_DEBUG
650 if (crypt && !(fc & IEEE80211_FCTL_WEP) &&
651 ieee80211_is_eapol_frame(ieee, skb)) {
652 struct eapol *eap = (struct eapol *)(skb->data +
654 IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
655 eap_get_type(eap->type));
659 if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep &&
660 !ieee80211_is_eapol_frame(ieee, skb)) {
661 IEEE80211_DEBUG_DROP(
662 "dropped unencrypted RX data "
663 "frame from " MAC_FMT
664 " (drop_unencrypted=1)\n",
665 MAC_ARG(hdr->addr2));
669 /* skb: hdr + (possible reassembled) full plaintext payload */
671 payload = skb->data + hdrlen;
672 ethertype = (payload[6] << 8) | payload[7];
675 /* If IEEE 802.1X is used, check whether the port is authorized to send
676 * the received frame. */
677 if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
678 if (ethertype == ETH_P_PAE) {
679 printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
681 if (ieee->hostapd && ieee->apdev) {
682 /* Send IEEE 802.1X frames to the user
683 * space daemon for processing */
684 prism2_rx_80211(ieee->apdev, skb, rx_stats,
686 ieee->apdevstats.rx_packets++;
687 ieee->apdevstats.rx_bytes += skb->len;
690 } else if (!frame_authorized) {
691 printk(KERN_DEBUG "%s: dropped frame from "
692 "unauthorized port (IEEE 802.1X): "
693 "ethertype=0x%04x\n",
694 dev->name, ethertype);
700 /* convert hdr + possible LLC headers into Ethernet header */
701 if (skb->len - hdrlen >= 8 &&
702 ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
703 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
704 memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
705 /* remove RFC1042 or Bridge-Tunnel encapsulation and
706 * replace EtherType */
707 skb_pull(skb, hdrlen + SNAP_SIZE);
708 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
709 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
712 /* Leave Ethernet header part of hdr and full payload */
713 skb_pull(skb, hdrlen);
714 len = htons(skb->len);
715 memcpy(skb_push(skb, 2), &len, 2);
716 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
717 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
721 if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
722 IEEE80211_FCTL_TODS) &&
723 skb->len >= ETH_HLEN + ETH_ALEN) {
724 /* Non-standard frame: get addr4 from its bogus location after
726 memcpy(skb->data + ETH_ALEN,
727 skb->data + skb->len - ETH_ALEN, ETH_ALEN);
728 skb_trim(skb, skb->len - ETH_ALEN);
733 stats->rx_bytes += skb->len;
736 if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
737 ieee->ap->bridge_packets) {
739 /* copy multicast frame both to the higher layers and
740 * to the wireless media */
741 ieee->ap->bridged_multicast++;
742 skb2 = skb_clone(skb, GFP_ATOMIC);
744 printk(KERN_DEBUG "%s: skb_clone failed for "
745 "multicast frame\n", dev->name);
746 } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
747 /* send frame directly to the associated STA using
748 * wireless media and not passing to higher layers */
749 ieee->ap->bridged_unicast++;
756 /* send to wireless media */
757 skb2->protocol = __constant_htons(ETH_P_802_3);
758 skb2->mac.raw = skb2->nh.raw = skb2->data;
759 /* skb2->nh.raw = skb2->data + ETH_HLEN; */
761 dev_queue_xmit(skb2);
767 skb->protocol = eth_type_trans(skb, dev);
768 memset(skb->cb, 0, sizeof(skb->cb));
770 skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
777 hostap_handle_sta_release(sta);
784 /* Returning 0 indicates to caller that we have not handled the SKB--
785 * so it is still allocated and can be used again by underlying
786 * hardware as a DMA target */
790 #define MGMT_FRAME_FIXED_PART_LENGTH 0x24
792 static inline int ieee80211_is_ofdm_rate(u8 rate)
794 switch (rate & ~IEEE80211_BASIC_RATE_MASK) {
795 case IEEE80211_OFDM_RATE_6MB:
796 case IEEE80211_OFDM_RATE_9MB:
797 case IEEE80211_OFDM_RATE_12MB:
798 case IEEE80211_OFDM_RATE_18MB:
799 case IEEE80211_OFDM_RATE_24MB:
800 case IEEE80211_OFDM_RATE_36MB:
801 case IEEE80211_OFDM_RATE_48MB:
802 case IEEE80211_OFDM_RATE_54MB:
809 static inline int ieee80211_network_init(
810 struct ieee80211_device *ieee,
811 struct ieee80211_probe_response *beacon,
812 struct ieee80211_network *network,
813 struct ieee80211_rx_stats *stats)
815 #ifdef CONFIG_IEEE80211_DEBUG
819 struct ieee80211_info_element *info_element;
823 /* Pull out fixed field data */
824 memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
825 network->capability = beacon->capability;
826 network->last_scanned = jiffies;
827 network->time_stamp[0] = beacon->time_stamp[0];
828 network->time_stamp[1] = beacon->time_stamp[1];
829 network->beacon_interval = beacon->beacon_interval;
830 /* Where to pull this? beacon->listen_interval;*/
831 network->listen_interval = 0x0A;
832 network->rates_len = network->rates_ex_len = 0;
833 network->last_associate = 0;
834 network->ssid_len = 0;
836 network->atim_window = 0;
838 if (stats->freq == IEEE80211_52GHZ_BAND) {
839 /* for A band (No DS info) */
840 network->channel = stats->received_channel;
842 network->flags |= NETWORK_HAS_CCK;
844 network->wpa_ie_len = 0;
845 network->rsn_ie_len = 0;
847 info_element = &beacon->info_element;
848 left = stats->len - ((void *)info_element - (void *)beacon);
849 while (left >= sizeof(struct ieee80211_info_element_hdr)) {
850 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
851 IEEE80211_DEBUG_SCAN("SCAN: parse failed: info_element->len + 2 > left : info_element->len+2=%d left=%d.\n",
852 info_element->len + sizeof(struct ieee80211_info_element),
857 switch (info_element->id) {
859 if (ieee80211_is_empty_essid(info_element->data,
860 info_element->len)) {
861 network->flags |= NETWORK_EMPTY_ESSID;
865 network->ssid_len = min(info_element->len,
866 (u8)IW_ESSID_MAX_SIZE);
867 memcpy(network->ssid, info_element->data, network->ssid_len);
868 if (network->ssid_len < IW_ESSID_MAX_SIZE)
869 memset(network->ssid + network->ssid_len, 0,
870 IW_ESSID_MAX_SIZE - network->ssid_len);
872 IEEE80211_DEBUG_SCAN("MFIE_TYPE_SSID: '%s' len=%d.\n",
873 network->ssid, network->ssid_len);
876 case MFIE_TYPE_RATES:
877 #ifdef CONFIG_IEEE80211_DEBUG
880 network->rates_len = min(info_element->len, MAX_RATES_LENGTH);
881 for (i = 0; i < network->rates_len; i++) {
882 network->rates[i] = info_element->data[i];
883 #ifdef CONFIG_IEEE80211_DEBUG
884 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
886 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
887 network->flags |= NETWORK_HAS_OFDM;
888 if (info_element->data[i] &
889 IEEE80211_BASIC_RATE_MASK)
895 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES: '%s' (%d)\n",
896 rates_str, network->rates_len);
899 case MFIE_TYPE_RATES_EX:
900 #ifdef CONFIG_IEEE80211_DEBUG
903 network->rates_ex_len = min(info_element->len, MAX_RATES_EX_LENGTH);
904 for (i = 0; i < network->rates_ex_len; i++) {
905 network->rates_ex[i] = info_element->data[i];
906 #ifdef CONFIG_IEEE80211_DEBUG
907 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
909 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
910 network->flags |= NETWORK_HAS_OFDM;
911 if (info_element->data[i] &
912 IEEE80211_BASIC_RATE_MASK)
918 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
919 rates_str, network->rates_ex_len);
922 case MFIE_TYPE_DS_SET:
923 IEEE80211_DEBUG_SCAN("MFIE_TYPE_DS_SET: %d\n",
924 info_element->data[0]);
925 if (stats->freq == IEEE80211_24GHZ_BAND)
926 network->channel = info_element->data[0];
929 case MFIE_TYPE_FH_SET:
930 IEEE80211_DEBUG_SCAN("MFIE_TYPE_FH_SET: ignored\n");
933 case MFIE_TYPE_CF_SET:
934 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CF_SET: ignored\n");
938 IEEE80211_DEBUG_SCAN("MFIE_TYPE_TIM: ignored\n");
941 case MFIE_TYPE_IBSS_SET:
942 IEEE80211_DEBUG_SCAN("MFIE_TYPE_IBSS_SET: ignored\n");
945 case MFIE_TYPE_CHALLENGE:
946 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CHALLENGE: ignored\n");
949 case MFIE_TYPE_GENERIC:
950 IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n",
952 if (info_element->len >= 4 &&
953 info_element->data[0] == 0x00 &&
954 info_element->data[1] == 0x50 &&
955 info_element->data[2] == 0xf2 &&
956 info_element->data[3] == 0x01) {
957 network->wpa_ie_len = min(info_element->len + 2,
959 memcpy(network->wpa_ie, info_element,
960 network->wpa_ie_len);
965 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RSN: %d bytes\n",
967 network->rsn_ie_len = min(info_element->len + 2,
969 memcpy(network->rsn_ie, info_element,
970 network->rsn_ie_len);
974 IEEE80211_DEBUG_SCAN("unsupported IE %d\n",
979 left -= sizeof(struct ieee80211_info_element_hdr) +
981 info_element = (struct ieee80211_info_element *)
982 &info_element->data[info_element->len];
986 if (stats->freq == IEEE80211_52GHZ_BAND)
987 network->mode = IEEE_A;
989 if (network->flags & NETWORK_HAS_OFDM)
990 network->mode |= IEEE_G;
991 if (network->flags & NETWORK_HAS_CCK)
992 network->mode |= IEEE_B;
995 if (network->mode == 0) {
996 IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
998 escape_essid(network->ssid,
1000 MAC_ARG(network->bssid));
1004 if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
1005 network->flags |= NETWORK_EMPTY_ESSID;
1007 memcpy(&network->stats, stats, sizeof(network->stats));
1012 static inline int is_same_network(struct ieee80211_network *src,
1013 struct ieee80211_network *dst)
1015 /* A network is only a duplicate if the channel, BSSID, and ESSID
1016 * all match. We treat all <hidden> with the same BSSID and channel
1018 return ((src->ssid_len == dst->ssid_len) &&
1019 (src->channel == dst->channel) &&
1020 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
1021 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1024 static inline void update_network(struct ieee80211_network *dst,
1025 struct ieee80211_network *src)
1027 memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
1028 dst->capability = src->capability;
1029 memcpy(dst->rates, src->rates, src->rates_len);
1030 dst->rates_len = src->rates_len;
1031 memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1032 dst->rates_ex_len = src->rates_ex_len;
1034 dst->mode = src->mode;
1035 dst->flags = src->flags;
1036 dst->time_stamp[0] = src->time_stamp[0];
1037 dst->time_stamp[1] = src->time_stamp[1];
1039 dst->beacon_interval = src->beacon_interval;
1040 dst->listen_interval = src->listen_interval;
1041 dst->atim_window = src->atim_window;
1043 memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1044 dst->wpa_ie_len = src->wpa_ie_len;
1045 memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1046 dst->rsn_ie_len = src->rsn_ie_len;
1048 dst->last_scanned = jiffies;
1049 /* dst->last_associate is not overwritten */
1052 static inline void ieee80211_process_probe_response(
1053 struct ieee80211_device *ieee,
1054 struct ieee80211_probe_response *beacon,
1055 struct ieee80211_rx_stats *stats)
1057 struct ieee80211_network network;
1058 struct ieee80211_network *target;
1059 struct ieee80211_network *oldest = NULL;
1060 #ifdef CONFIG_IEEE80211_DEBUG
1061 struct ieee80211_info_element *info_element = &beacon->info_element;
1063 unsigned long flags;
1065 IEEE80211_DEBUG_SCAN(
1066 "'%s' (" MAC_FMT "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1067 escape_essid(info_element->data, info_element->len),
1068 MAC_ARG(beacon->header.addr3),
1069 (beacon->capability & (1<<0xf)) ? '1' : '0',
1070 (beacon->capability & (1<<0xe)) ? '1' : '0',
1071 (beacon->capability & (1<<0xd)) ? '1' : '0',
1072 (beacon->capability & (1<<0xc)) ? '1' : '0',
1073 (beacon->capability & (1<<0xb)) ? '1' : '0',
1074 (beacon->capability & (1<<0xa)) ? '1' : '0',
1075 (beacon->capability & (1<<0x9)) ? '1' : '0',
1076 (beacon->capability & (1<<0x8)) ? '1' : '0',
1077 (beacon->capability & (1<<0x7)) ? '1' : '0',
1078 (beacon->capability & (1<<0x6)) ? '1' : '0',
1079 (beacon->capability & (1<<0x5)) ? '1' : '0',
1080 (beacon->capability & (1<<0x4)) ? '1' : '0',
1081 (beacon->capability & (1<<0x3)) ? '1' : '0',
1082 (beacon->capability & (1<<0x2)) ? '1' : '0',
1083 (beacon->capability & (1<<0x1)) ? '1' : '0',
1084 (beacon->capability & (1<<0x0)) ? '1' : '0');
1086 if (ieee80211_network_init(ieee, beacon, &network, stats)) {
1087 IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
1088 escape_essid(info_element->data,
1090 MAC_ARG(beacon->header.addr3),
1091 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1092 IEEE80211_STYPE_PROBE_RESP ?
1093 "PROBE RESPONSE" : "BEACON");
1097 /* The network parsed correctly -- so now we scan our known networks
1098 * to see if we can find it in our list.
1100 * NOTE: This search is definitely not optimized. Once its doing
1101 * the "right thing" we'll optimize it for efficiency if
1104 /* Search for this entry in the list and update it if it is
1107 spin_lock_irqsave(&ieee->lock, flags);
1109 list_for_each_entry(target, &ieee->network_list, list) {
1110 if (is_same_network(target, &network))
1113 if ((oldest == NULL) ||
1114 (target->last_scanned < oldest->last_scanned))
1118 /* If we didn't find a match, then get a new network slot to initialize
1119 * with this beacon's information */
1120 if (&target->list == &ieee->network_list) {
1121 if (list_empty(&ieee->network_free_list)) {
1122 /* If there are no more slots, expire the oldest */
1123 list_del(&oldest->list);
1125 IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
1127 escape_essid(target->ssid,
1129 MAC_ARG(target->bssid));
1131 /* Otherwise just pull from the free list */
1132 target = list_entry(ieee->network_free_list.next,
1133 struct ieee80211_network, list);
1134 list_del(ieee->network_free_list.next);
1138 #ifdef CONFIG_IEEE80211_DEBUG
1139 IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
1140 escape_essid(network.ssid,
1142 MAC_ARG(network.bssid),
1143 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1144 IEEE80211_STYPE_PROBE_RESP ?
1145 "PROBE RESPONSE" : "BEACON");
1147 memcpy(target, &network, sizeof(*target));
1148 list_add_tail(&target->list, &ieee->network_list);
1150 IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
1151 escape_essid(target->ssid,
1153 MAC_ARG(target->bssid),
1154 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1155 IEEE80211_STYPE_PROBE_RESP ?
1156 "PROBE RESPONSE" : "BEACON");
1157 update_network(target, &network);
1160 spin_unlock_irqrestore(&ieee->lock, flags);
1163 void ieee80211_rx_mgt(struct ieee80211_device *ieee,
1164 struct ieee80211_hdr *header,
1165 struct ieee80211_rx_stats *stats)
1167 switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
1168 case IEEE80211_STYPE_ASSOC_RESP:
1169 IEEE80211_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
1170 WLAN_FC_GET_STYPE(header->frame_ctl));
1173 case IEEE80211_STYPE_REASSOC_RESP:
1174 IEEE80211_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
1175 WLAN_FC_GET_STYPE(header->frame_ctl));
1178 case IEEE80211_STYPE_PROBE_RESP:
1179 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1180 WLAN_FC_GET_STYPE(header->frame_ctl));
1181 IEEE80211_DEBUG_SCAN("Probe response\n");
1182 ieee80211_process_probe_response(
1183 ieee, (struct ieee80211_probe_response *)header, stats);
1186 case IEEE80211_STYPE_BEACON:
1187 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
1188 WLAN_FC_GET_STYPE(header->frame_ctl));
1189 IEEE80211_DEBUG_SCAN("Beacon\n");
1190 ieee80211_process_probe_response(
1191 ieee, (struct ieee80211_probe_response *)header, stats);
1195 IEEE80211_DEBUG_MGMT("received UNKNOWN (%d)\n",
1196 WLAN_FC_GET_STYPE(header->frame_ctl));
1197 IEEE80211_WARNING("%s: Unknown management packet: %d\n",
1199 WLAN_FC_GET_STYPE(header->frame_ctl));
1205 EXPORT_SYMBOL(ieee80211_rx_mgt);
1206 EXPORT_SYMBOL(ieee80211_rx);