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 "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)) {
69 printk(KERN_DEBUG "%s: expiring fragment cache entry "
70 "seq=%u last_frag=%u\n",
71 ieee->dev->name, 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 printk(KERN_DEBUG "%s: could not invalidate fragment cache "
156 ieee->dev->name, 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 #ifdef CONFIG_IEEE80211_CRYPT
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_WPA
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 printk(KERN_DEBUG "%s: decryption failed (SA=" MAC_FMT
305 ieee->dev->name, MAC_ARG(hdr->addr2), res);
307 printk(KERN_DEBUG "%s: WEP decryption failed ICV "
308 "mismatch (key %d)\n",
309 ieee->dev->name, skb->data[hdrlen + 3] >> 6);
310 ieee->ieee_stats.rx_discards_wep_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);
344 #endif /* CONFIG_IEEE80211_CRYPT */
347 /* All received frames are sent to this function. @skb contains the frame in
348 * IEEE 802.11 format, i.e., in the format it was sent over air.
349 * This function is called only as a tasklet (software IRQ). */
350 int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
351 struct ieee80211_rx_stats *rx_stats)
353 struct net_device *dev = ieee->dev;
354 struct ieee80211_hdr *hdr;
356 u16 fc, type, stype, sc;
357 struct net_device_stats *stats;
362 struct net_device *wds = NULL;
363 struct sk_buff *skb2 = NULL;
364 struct net_device *wds = NULL;
365 int frame_authorized = 0;
366 int from_assoc_ap = 0;
371 #ifdef CONFIG_IEEE80211_CRYPT
372 struct ieee80211_crypt_data *crypt = NULL;
376 hdr = (struct ieee80211_hdr *)skb->data;
377 stats = &ieee->stats;
380 printk(KERN_INFO "%s: SKB length < 10\n",
385 fc = le16_to_cpu(hdr->frame_ctl);
386 type = WLAN_FC_GET_TYPE(fc);
387 stype = WLAN_FC_GET_STYPE(fc);
388 sc = le16_to_cpu(hdr->seq_ctl);
389 frag = WLAN_GET_SEQ_FRAG(sc);
390 hdrlen = ieee80211_get_hdrlen(fc);
393 #if WIRELESS_EXT > 15
394 /* Put this code here so that we avoid duplicating it in all
395 * Rx paths. - Jean II */
396 #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
397 /* If spy monitoring on */
398 if (iface->spy_data.spy_number > 0) {
399 struct iw_quality wstats;
400 wstats.level = rx_stats->signal;
401 wstats.noise = rx_stats->noise;
402 wstats.updated = 6; /* No qual value */
403 /* Update spy records */
404 wireless_spy_update(dev, hdr->addr2, &wstats);
406 #endif /* IW_WIRELESS_SPY */
407 #endif /* WIRELESS_EXT > 15 */
408 hostap_update_rx_stats(local->ap, hdr, rx_stats);
411 #if WIRELESS_EXT > 15
412 if (ieee->iw_mode == IW_MODE_MONITOR) {
413 ieee80211_monitor_rx(ieee, skb, rx_stats);
415 stats->rx_bytes += skb->len;
420 #ifdef CONFIG_IEEE80211_CRYPT
421 if (ieee->host_decrypt) {
423 if (skb->len >= hdrlen + 3)
424 idx = skb->data[hdrlen + 3] >> 6;
425 crypt = ieee->crypt[idx];
429 /* Use station specific key to override default keys if the
430 * receiver address is a unicast address ("individual RA"). If
431 * bcrx_sta_key parameter is set, station specific key is used
432 * even with broad/multicast targets (this is against IEEE
433 * 802.11, but makes it easier to use different keys with
434 * stations that do not support WEP key mapping). */
436 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
437 (void) hostap_handle_sta_crypto(local, hdr, &crypt,
441 /* allow NULL decrypt to indicate an station specific override
442 * for default encryption */
443 if (crypt && (crypt->ops == NULL ||
444 crypt->ops->decrypt_mpdu == NULL))
447 if (!crypt && (fc & IEEE80211_FCTL_WEP)) {
449 /* This seems to be triggered by some (multicast?)
450 * frames from other than current BSS, so just drop the
451 * frames silently instead of filling system log with
453 printk(KERN_DEBUG "%s: WEP decryption failed (not set)"
454 " (SA=" MAC_FMT ")\n",
455 ieee->dev->name, MAC_ARG(hdr->addr2));
457 ieee->ieee_stats.rx_discards_wep_undecryptable++;
461 #endif /* CONFIG_IEEE80211_CRYPT */
464 if (type != WLAN_FC_TYPE_DATA) {
465 if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
466 fc & IEEE80211_FCTL_WEP && ieee->host_decrypt &&
467 (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0)
469 printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
470 "from " MAC_FMT "\n", dev->name,
471 MAC_ARG(hdr->addr2));
472 /* TODO: could inform hostapd about this so that it
473 * could send auth failure report */
477 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
484 /* Data frame - extract src/dst addresses */
485 if (skb->len < IEEE80211_DATA_HDR3_LEN)
488 switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
489 case IEEE80211_FCTL_FROMDS:
490 memcpy(dst, hdr->addr1, ETH_ALEN);
491 memcpy(src, hdr->addr3, ETH_ALEN);
493 case IEEE80211_FCTL_TODS:
494 memcpy(dst, hdr->addr3, ETH_ALEN);
495 memcpy(src, hdr->addr2, ETH_ALEN);
497 case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
498 if (skb->len < IEEE80211_DATA_HDR4_LEN)
500 memcpy(dst, hdr->addr3, ETH_ALEN);
501 memcpy(src, hdr->addr4, ETH_ALEN);
504 memcpy(dst, hdr->addr1, ETH_ALEN);
505 memcpy(src, hdr->addr2, ETH_ALEN);
510 if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
513 skb->dev = dev = wds;
514 stats = hostap_get_stats(dev);
517 if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
518 (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == IEEE80211_FCTL_FROMDS &&
520 memcmp(hdr->addr2, ieee->assoc_ap_addr, ETH_ALEN) == 0) {
521 /* Frame from BSSID of the AP for which we are a client */
522 skb->dev = dev = ieee->stadev;
523 stats = hostap_get_stats(dev);
528 dev->last_rx = jiffies;
531 if ((ieee->iw_mode == IW_MODE_MASTER ||
532 ieee->iw_mode == IW_MODE_REPEAT) &&
534 switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
536 case AP_RX_CONTINUE_NOT_AUTHORIZED:
537 frame_authorized = 0;
540 frame_authorized = 1;
550 /* Nullfunc frames may have PS-bit set, so they must be passed to
551 * hostap_handle_sta_rx() before being dropped here. */
552 if (stype != IEEE80211_STYPE_DATA &&
553 stype != IEEE80211_STYPE_DATA_CFACK &&
554 stype != IEEE80211_STYPE_DATA_CFPOLL &&
555 stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
556 if (stype != IEEE80211_STYPE_NULLFUNC)
557 printk(KERN_DEBUG "%s: RX: dropped data frame "
558 "with no data (type=0x%02x, subtype=0x%02x, len=%d)\n",
559 dev->name, type, stype, skb->len);
563 /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
565 #ifdef CONFIG_IEEE80211_CRYPT
566 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
567 (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
570 hdr = (struct ieee80211_hdr *) skb->data;
572 /* skb: hdr + (possibly fragmented) plaintext payload */
573 // PR: FIXME: hostap has additional conditions in the "if" below:
574 // ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
575 if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
577 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
578 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
581 IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
582 "Rx cannot get skb from fragment "
583 "cache (morefrag=%d seq=%u frag=%u)\n",
584 (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
585 WLAN_GET_SEQ_SEQ(sc), frag);
593 if (frag_skb->tail + flen > frag_skb->end) {
594 printk(KERN_WARNING "%s: host decrypted and "
595 "reassembled frame did not fit skb\n",
597 ieee80211_frag_cache_invalidate(ieee, hdr);
602 /* copy first fragment (including full headers) into
603 * beginning of the fragment cache skb */
604 memcpy(skb_put(frag_skb, flen), skb->data, flen);
606 /* append frame payload to the end of the fragment
608 memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
611 dev_kfree_skb_any(skb);
614 if (fc & IEEE80211_FCTL_MOREFRAGS) {
615 /* more fragments expected - leave the skb in fragment
616 * cache for now; it will be delivered to upper layers
617 * after all fragments have been received */
621 /* this was the last fragment and the frame will be
622 * delivered, so remove skb from fragment cache */
624 hdr = (struct ieee80211_hdr *) skb->data;
625 ieee80211_frag_cache_invalidate(ieee, hdr);
628 /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
629 * encrypted/authenticated */
630 #ifdef CONFIG_IEEE80211_CRYPT
631 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
632 ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
635 hdr = (struct ieee80211_hdr *) skb->data;
636 if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep) {
637 if (/*ieee->ieee802_1x &&*/
638 ieee80211_is_eapol_frame(ieee, skb)) {
639 #ifdef CONFIG_IEEE80211_DEBUG
640 /* pass unencrypted EAPOL frames even if encryption is
642 struct eapol *eap = (struct eapol *)(skb->data +
644 IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
645 eap_get_type(eap->type));
648 printk(KERN_DEBUG "%s: encryption configured, but RX "
649 "frame not encrypted (SA=" MAC_FMT ")\n",
650 ieee->dev->name, MAC_ARG(hdr->addr2));
655 #ifdef CONFIG_IEEE80211_DEBUG
656 if (crypt && !(fc & IEEE80211_FCTL_WEP) &&
657 ieee80211_is_eapol_frame(ieee, skb)) {
658 struct eapol *eap = (struct eapol *)(skb->data +
660 IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
661 eap_get_type(eap->type));
665 if (/*ieee->drop_unencrypted*/ crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep &&
666 !ieee80211_is_eapol_frame(ieee, skb)) {
667 if (net_ratelimit()) {
668 printk(KERN_DEBUG "%s: dropped unencrypted RX data "
669 "frame from " MAC_FMT " (drop_unencrypted=1)\n",
670 dev->name, MAC_ARG(hdr->addr2));
674 #endif /* CONFIG_IEEE80211_CRYPT */
676 /* skb: hdr + (possible reassembled) full plaintext payload */
678 payload = skb->data + hdrlen;
679 ethertype = (payload[6] << 8) | payload[7];
682 /* If IEEE 802.1X is used, check whether the port is authorized to send
683 * the received frame. */
684 if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
685 if (ethertype == ETH_P_PAE) {
686 printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
688 if (ieee->hostapd && ieee->apdev) {
689 /* Send IEEE 802.1X frames to the user
690 * space daemon for processing */
691 prism2_rx_80211(ieee->apdev, skb, rx_stats,
693 ieee->apdevstats.rx_packets++;
694 ieee->apdevstats.rx_bytes += skb->len;
697 } else if (!frame_authorized) {
698 printk(KERN_DEBUG "%s: dropped frame from "
699 "unauthorized port (IEEE 802.1X): "
700 "ethertype=0x%04x\n",
701 dev->name, ethertype);
707 /* convert hdr + possible LLC headers into Ethernet header */
708 if (skb->len - hdrlen >= 8 &&
709 ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
710 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
711 memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
712 /* remove RFC1042 or Bridge-Tunnel encapsulation and
713 * replace EtherType */
714 skb_pull(skb, hdrlen + SNAP_SIZE);
715 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
716 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
719 /* Leave Ethernet header part of hdr and full payload */
720 skb_pull(skb, hdrlen);
721 len = htons(skb->len);
722 memcpy(skb_push(skb, 2), &len, 2);
723 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
724 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
728 if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
729 IEEE80211_FCTL_TODS) &&
730 skb->len >= ETH_HLEN + ETH_ALEN) {
731 /* Non-standard frame: get addr4 from its bogus location after
733 memcpy(skb->data + ETH_ALEN,
734 skb->data + skb->len - ETH_ALEN, ETH_ALEN);
735 skb_trim(skb, skb->len - ETH_ALEN);
740 stats->rx_bytes += skb->len;
743 if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
744 ieee->ap->bridge_packets) {
746 /* copy multicast frame both to the higher layers and
747 * to the wireless media */
748 ieee->ap->bridged_multicast++;
749 skb2 = skb_clone(skb, GFP_ATOMIC);
751 printk(KERN_DEBUG "%s: skb_clone failed for "
752 "multicast frame\n", dev->name);
753 } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
754 /* send frame directly to the associated STA using
755 * wireless media and not passing to higher layers */
756 ieee->ap->bridged_unicast++;
763 /* send to wireless media */
764 skb2->protocol = __constant_htons(ETH_P_802_3);
765 skb2->mac.raw = skb2->nh.raw = skb2->data;
766 /* skb2->nh.raw = skb2->data + ETH_HLEN; */
768 dev_queue_xmit(skb2);
774 skb->protocol = eth_type_trans(skb, dev);
775 memset(skb->cb, 0, sizeof(skb->cb));
777 skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
784 hostap_handle_sta_release(sta);
791 /* Returning 0 indicates to caller that we have not handled the SKB--
792 * so it is still allocated and can be used again by underlying
793 * hardware as a DMA target */
797 #define MGMT_FRAME_FIXED_PART_LENGTH 0x24
799 static inline int ieee80211_is_ofdm_rate(u8 rate)
801 switch (rate & ~IEEE80211_BASIC_RATE_MASK) {
802 case IEEE80211_OFDM_RATE_6MB:
803 case IEEE80211_OFDM_RATE_9MB:
804 case IEEE80211_OFDM_RATE_12MB:
805 case IEEE80211_OFDM_RATE_18MB:
806 case IEEE80211_OFDM_RATE_24MB:
807 case IEEE80211_OFDM_RATE_36MB:
808 case IEEE80211_OFDM_RATE_48MB:
809 case IEEE80211_OFDM_RATE_54MB:
816 static inline int ieee80211_network_init(
817 struct ieee80211_device *ieee,
818 struct ieee80211_probe_response *beacon,
819 struct ieee80211_network *network,
820 struct ieee80211_rx_stats *stats)
822 struct ieee80211_info_element *info_element;
826 /* Pull out fixed field data */
827 memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
828 network->capability = beacon->capability;
829 network->last_scanned = jiffies;
830 network->time_stamp[0] = beacon->time_stamp[0];
831 network->time_stamp[1] = beacon->time_stamp[1];
832 network->beacon_interval = beacon->beacon_interval;
833 /* Where to pull this? beacon->listen_interval;*/
834 network->listen_interval = 0x0A;
835 network->rates_len = network->rates_ex_len = 0;
836 network->last_associate = 0;
837 network->ssid_len = 0;
839 network->atim_window = 0;
841 if (stats->freq == IEEE80211_52GHZ_BAND) {
842 /* for A band (No DS info) */
843 network->channel = stats->received_channel;
845 network->flags |= NETWORK_HAS_CCK;
847 #ifdef CONFIG_IEEE80211_WPA
848 network->wpa_ie_len = 0;
849 network->rsn_ie_len = 0;
850 #endif /* CONFIG_IEEE80211_WPA */
852 info_element = &beacon->info_element;
853 left = stats->len - ((void *)info_element - (void *)beacon);
854 while (left >= sizeof(struct ieee80211_info_element_hdr)) {
855 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
856 IEEE80211_DEBUG_SCAN("SCAN: parse failed: info_element->len + 2 > left : info_element->len+2=%d left=%d.\n",
857 info_element->len + sizeof(struct ieee80211_info_element),
862 switch (info_element->id) {
864 if (ieee80211_is_empty_essid(info_element->data,
865 info_element->len)) {
866 network->flags |= NETWORK_EMPTY_ESSID;
870 network->ssid_len = min(info_element->len,
871 (u8)IW_ESSID_MAX_SIZE);
872 memcpy(network->ssid, info_element->data, network->ssid_len);
873 if (network->ssid_len < IW_ESSID_MAX_SIZE)
874 memset(network->ssid + network->ssid_len, 0,
875 IW_ESSID_MAX_SIZE - network->ssid_len);
877 IEEE80211_DEBUG_SCAN("MFIE_TYPE_SSID: '%s' len=%d.\n",
878 network->ssid, network->ssid_len);
881 case MFIE_TYPE_RATES:
882 network->rates_len = min(info_element->len, MAX_RATES_LENGTH);
883 for (i = 0; i < network->rates_len; i++) {
884 network->rates[i] = info_element->data[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)
895 case MFIE_TYPE_RATES_EX:
896 network->rates_ex_len = min(info_element->len, MAX_RATES_EX_LENGTH);
897 for (i = 0; i < network->rates_ex_len; i++) {
898 network->rates_ex[i] = info_element->data[i];
899 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
900 network->flags |= NETWORK_HAS_OFDM;
901 if (info_element->data[i] &
902 IEEE80211_BASIC_RATE_MASK)
909 case MFIE_TYPE_DS_SET:
910 IEEE80211_DEBUG_SCAN("MFIE_TYPE_DS_SET: %d\n",
911 info_element->data[0]);
912 if (stats->freq == IEEE80211_24GHZ_BAND)
913 network->channel = info_element->data[0];
916 case MFIE_TYPE_FH_SET:
917 IEEE80211_DEBUG_SCAN("MFIE_TYPE_FH_SET: ignored\n");
920 case MFIE_TYPE_CF_SET:
921 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CF_SET: ignored\n");
925 IEEE80211_DEBUG_SCAN("MFIE_TYPE_TIM: ignored\n");
928 case MFIE_TYPE_IBSS_SET:
929 IEEE80211_DEBUG_SCAN("MFIE_TYPE_IBSS_SET: ignored\n");
932 case MFIE_TYPE_CHALLENGE:
933 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CHALLENGE: ignored\n");
936 #ifdef CONFIG_IEEE80211_WPA
937 case MFIE_TYPE_GENERIC:
938 IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n",
940 if (info_element->len >= 4 &&
941 info_element->data[0] == 0x00 &&
942 info_element->data[1] == 0x50 &&
943 info_element->data[2] == 0xf2 &&
944 info_element->data[3] == 0x01) {
945 network->wpa_ie_len = min(info_element->len + 2,
947 memcpy(network->wpa_ie, info_element,
948 network->wpa_ie_len);
953 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RSN: %d bytes\n",
955 network->rsn_ie_len = min(info_element->len + 2,
957 memcpy(network->rsn_ie, info_element,
958 network->rsn_ie_len);
963 IEEE80211_DEBUG_SCAN("unsupported IE %d\n",
968 left -= sizeof(struct ieee80211_info_element_hdr) +
970 info_element = (struct ieee80211_info_element *)
971 &info_element->data[info_element->len];
975 if (stats->freq == IEEE80211_52GHZ_BAND) {
976 network->mode = IEEE_A;
978 if (network->flags & NETWORK_HAS_OFDM)
979 network->mode |= IEEE_G;
980 if (network->flags & NETWORK_HAS_CCK)
981 network->mode |= IEEE_B;
984 if (network->mode == 0) {
985 IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
987 escape_essid(network->ssid,
989 MAC_ARG(network->bssid));
993 if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
994 network->flags |= NETWORK_EMPTY_ESSID;
996 memcpy(&network->stats, stats, sizeof(network->stats));
1001 static inline int is_same_network(struct ieee80211_network *src,
1002 struct ieee80211_network *dst)
1004 /* A network is only a duplicate if the channel, BSSID, and ESSID
1005 * all match. We treat all <hidden> with the same BSSID and channel
1007 return ((src->ssid_len == dst->ssid_len) &&
1008 (src->channel == dst->channel) &&
1009 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
1010 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1013 static inline void update_network(struct ieee80211_network *dst,
1014 struct ieee80211_network *src)
1016 memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
1017 dst->capability = src->capability;
1018 memcpy(dst->rates, src->rates, src->rates_len);
1019 dst->rates_len = src->rates_len;
1020 memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1021 dst->rates_ex_len = src->rates_ex_len;
1023 dst->mode = src->mode;
1024 dst->flags = src->flags;
1025 dst->time_stamp[0] = src->time_stamp[0];
1026 dst->time_stamp[1] = src->time_stamp[1];
1028 dst->beacon_interval = src->beacon_interval;
1029 dst->listen_interval = src->listen_interval;
1030 dst->atim_window = src->atim_window;
1032 #ifdef CONFIG_IEEE80211_WPA
1033 memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1034 dst->wpa_ie_len = src->wpa_ie_len;
1035 memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1036 dst->rsn_ie_len = src->rsn_ie_len;
1037 #endif /* CONFIG_IEEE80211_WPA */
1039 dst->last_scanned = jiffies;
1040 /* dst->last_associate is not overwritten */
1043 static inline void ieee80211_process_probe_response(
1044 struct ieee80211_device *ieee,
1045 struct ieee80211_probe_response *beacon,
1046 struct ieee80211_rx_stats *stats)
1048 struct ieee80211_network network;
1049 struct ieee80211_network *target;
1050 struct ieee80211_network *oldest = NULL;
1051 #ifdef CONFIG_IEEE80211_DEBUG
1052 struct ieee80211_info_element *info_element = &beacon->info_element;
1055 IEEE80211_DEBUG_SCAN(
1056 "'%s' (" MAC_FMT "): %c%c%c%c-%c%c%c%c\n",
1057 escape_essid(info_element->data, info_element->len),
1058 MAC_ARG(beacon->header.addr3),
1059 (beacon->capability & BIT(7)) ? '1' : '0',
1060 (beacon->capability & BIT(6)) ? '1' : '0',
1061 (beacon->capability & BIT(5)) ? '1' : '0',
1062 (beacon->capability & BIT(4)) ? '1' : '0',
1063 (beacon->capability & BIT(3)) ? '1' : '0',
1064 (beacon->capability & BIT(2)) ? '1' : '0',
1065 (beacon->capability & BIT(1)) ? '1' : '0',
1066 (beacon->capability & BIT(0)) ? '1' : '0');
1068 if (ieee80211_network_init(ieee, beacon, &network, stats)) {
1069 IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
1070 escape_essid(info_element->data,
1072 MAC_ARG(beacon->header.addr3),
1073 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1074 IEEE80211_STYPE_PROBE_RESP ?
1075 "PROBE RESPONSE" : "BEACON");
1079 /* The network parsed correctly -- so now we scan our known networks
1080 * to see if we can find it in our list.
1082 * NOTE: This search is definitely not optimized. Once its doing
1083 * the "right thing" we'll optimize it for efficiency if
1086 /* Search for this entry in the list and update it if it is
1088 list_for_each_entry(target, &ieee->network_list, list) {
1089 if (is_same_network(target, &network))
1092 if ((oldest == NULL) ||
1093 (target->last_scanned < oldest->last_scanned))
1097 /* If we didn't find a match, then get a new network slot to initialize
1098 * with this beacon's information */
1099 if (&target->list == &ieee->network_list) {
1100 if (list_empty(&ieee->network_free_list)) {
1101 /* If there are no more slots, expire the oldest */
1102 list_del(&oldest->list);
1104 IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
1106 escape_essid(target->ssid,
1108 MAC_ARG(target->bssid));
1110 /* Otherwise just pull from the free list */
1111 target = list_entry(ieee->network_free_list.next,
1112 struct ieee80211_network, list);
1113 list_del(ieee->network_free_list.next);
1117 #ifdef CONFIG_IEEE80211_DEBUG
1118 IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
1119 escape_essid(network.ssid,
1121 MAC_ARG(network.bssid),
1122 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1123 IEEE80211_STYPE_PROBE_RESP ?
1124 "PROBE RESPONSE" : "BEACON");
1126 memcpy(target, &network, sizeof(*target));
1127 list_add_tail(&target->list, &ieee->network_list);
1129 IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
1130 escape_essid(target->ssid,
1132 MAC_ARG(target->bssid),
1133 WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1134 IEEE80211_STYPE_PROBE_RESP ?
1135 "PROBE RESPONSE" : "BEACON");
1136 update_network(target, &network);
1140 void ieee80211_rx_mgt(struct ieee80211_device *ieee,
1141 struct ieee80211_hdr *header,
1142 struct ieee80211_rx_stats *stats)
1144 switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
1145 case IEEE80211_STYPE_ASSOC_RESP:
1146 IEEE80211_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
1147 WLAN_FC_GET_STYPE(header->frame_ctl));
1150 case IEEE80211_STYPE_REASSOC_RESP:
1151 IEEE80211_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
1152 WLAN_FC_GET_STYPE(header->frame_ctl));
1155 case IEEE80211_STYPE_PROBE_RESP:
1156 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1157 WLAN_FC_GET_STYPE(header->frame_ctl));
1158 IEEE80211_DEBUG_SCAN("Probe response\n");
1159 ieee80211_process_probe_response(
1160 ieee, (struct ieee80211_probe_response *)header, stats);
1163 case IEEE80211_STYPE_BEACON:
1164 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
1165 WLAN_FC_GET_STYPE(header->frame_ctl));
1166 IEEE80211_DEBUG_SCAN("Beacon\n");
1167 ieee80211_process_probe_response(
1168 ieee, (struct ieee80211_probe_response *)header, stats);
1172 IEEE80211_DEBUG_MGMT("received UNKNOWN (%d)\n",
1173 WLAN_FC_GET_STYPE(header->frame_ctl));
1174 IEEE80211_WARNING("%s: Unknown management packet: %d\n",
1176 WLAN_FC_GET_STYPE(header->frame_ctl));
1182 EXPORT_SYMBOL(ieee80211_rx_mgt);
1183 EXPORT_SYMBOL(ieee80211_rx);