1 #include <linux/etherdevice.h>
2 #include <net/lib80211.h>
3 #include <linux/if_arp.h>
5 #include "hostap_80211.h"
9 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
10 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
11 static unsigned char rfc1042_header[] =
12 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
13 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
14 static unsigned char bridge_tunnel_header[] =
15 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
16 /* No encapsulation header if EtherType < 0x600 (=length) */
18 void hostap_dump_rx_80211(const char *name, struct sk_buff *skb,
19 struct hostap_80211_rx_status *rx_stats)
21 struct ieee80211_hdr *hdr;
24 hdr = (struct ieee80211_hdr *) skb->data;
26 printk(KERN_DEBUG "%s: RX signal=%d noise=%d rate=%d len=%d "
28 name, rx_stats->signal, rx_stats->noise, rx_stats->rate,
34 fc = le16_to_cpu(hdr->frame_control);
35 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d)%s%s",
36 fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
37 (fc & IEEE80211_FCTL_STYPE) >> 4,
38 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
39 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
41 if (skb->len < IEEE80211_DATA_HDR3_LEN) {
46 printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
47 le16_to_cpu(hdr->seq_ctrl));
49 printk(KERN_DEBUG " A1=%pM", hdr->addr1);
50 printk(" A2=%pM", hdr->addr2);
51 printk(" A3=%pM", hdr->addr3);
53 printk(" A4=%pM", hdr->addr4);
58 /* Send RX frame to netif with 802.11 (and possible prism) header.
59 * Called from hardware or software IRQ context. */
60 int prism2_rx_80211(struct net_device *dev, struct sk_buff *skb,
61 struct hostap_80211_rx_status *rx_stats, int type)
63 struct hostap_interface *iface;
65 int hdrlen, phdrlen, head_need, tail_need;
67 int prism_header, ret;
68 struct ieee80211_hdr *fhdr;
70 iface = netdev_priv(dev);
73 if (dev->type == ARPHRD_IEEE80211_PRISM) {
74 if (local->monitor_type == PRISM2_MONITOR_PRISM) {
76 phdrlen = sizeof(struct linux_wlan_ng_prism_hdr);
77 } else { /* local->monitor_type == PRISM2_MONITOR_CAPHDR */
79 phdrlen = sizeof(struct linux_wlan_ng_cap_hdr);
81 } else if (dev->type == ARPHRD_IEEE80211_RADIOTAP) {
83 phdrlen = sizeof(struct hostap_radiotap_rx);
89 fhdr = (struct ieee80211_hdr *) skb->data;
90 fc = le16_to_cpu(fhdr->frame_control);
92 if (type == PRISM2_RX_MGMT && (fc & IEEE80211_FCTL_VERS)) {
93 printk(KERN_DEBUG "%s: dropped management frame with header "
94 "version %d\n", dev->name, fc & IEEE80211_FCTL_VERS);
95 dev_kfree_skb_any(skb);
99 hdrlen = hostap_80211_get_hdrlen(fhdr->frame_control);
101 /* check if there is enough room for extra data; if not, expand skb
102 * buffer to be large enough for the changes */
105 #ifdef PRISM2_ADD_BOGUS_CRC
107 #endif /* PRISM2_ADD_BOGUS_CRC */
109 head_need -= skb_headroom(skb);
110 tail_need -= skb_tailroom(skb);
112 if (head_need > 0 || tail_need > 0) {
113 if (pskb_expand_head(skb, head_need > 0 ? head_need : 0,
114 tail_need > 0 ? tail_need : 0,
116 printk(KERN_DEBUG "%s: prism2_rx_80211 failed to "
117 "reallocate skb buffer\n", dev->name);
118 dev_kfree_skb_any(skb);
123 /* We now have an skb with enough head and tail room, so just insert
126 #ifdef PRISM2_ADD_BOGUS_CRC
127 memset(skb_put(skb, 4), 0xff, 4); /* Prism2 strips CRC */
128 #endif /* PRISM2_ADD_BOGUS_CRC */
130 if (prism_header == 1) {
131 struct linux_wlan_ng_prism_hdr *hdr;
132 hdr = (struct linux_wlan_ng_prism_hdr *)
133 skb_push(skb, phdrlen);
134 memset(hdr, 0, phdrlen);
135 hdr->msgcode = LWNG_CAP_DID_BASE;
136 hdr->msglen = sizeof(*hdr);
137 memcpy(hdr->devname, dev->name, sizeof(hdr->devname));
138 #define LWNG_SETVAL(f,i,s,l,d) \
139 hdr->f.did = LWNG_CAP_DID_BASE | (i << 12); \
140 hdr->f.status = s; hdr->f.len = l; hdr->f.data = d
141 LWNG_SETVAL(hosttime, 1, 0, 4, jiffies);
142 LWNG_SETVAL(mactime, 2, 0, 4, rx_stats->mac_time);
143 LWNG_SETVAL(channel, 3, 1 /* no value */, 4, 0);
144 LWNG_SETVAL(rssi, 4, 1 /* no value */, 4, 0);
145 LWNG_SETVAL(sq, 5, 1 /* no value */, 4, 0);
146 LWNG_SETVAL(signal, 6, 0, 4, rx_stats->signal);
147 LWNG_SETVAL(noise, 7, 0, 4, rx_stats->noise);
148 LWNG_SETVAL(rate, 8, 0, 4, rx_stats->rate / 5);
149 LWNG_SETVAL(istx, 9, 0, 4, 0);
150 LWNG_SETVAL(frmlen, 10, 0, 4, skb->len - phdrlen);
152 } else if (prism_header == 2) {
153 struct linux_wlan_ng_cap_hdr *hdr;
154 hdr = (struct linux_wlan_ng_cap_hdr *)
155 skb_push(skb, phdrlen);
156 memset(hdr, 0, phdrlen);
157 hdr->version = htonl(LWNG_CAPHDR_VERSION);
158 hdr->length = htonl(phdrlen);
159 hdr->mactime = __cpu_to_be64(rx_stats->mac_time);
160 hdr->hosttime = __cpu_to_be64(jiffies);
161 hdr->phytype = htonl(4); /* dss_dot11_b */
162 hdr->channel = htonl(local->channel);
163 hdr->datarate = htonl(rx_stats->rate);
164 hdr->antenna = htonl(0); /* unknown */
165 hdr->priority = htonl(0); /* unknown */
166 hdr->ssi_type = htonl(3); /* raw */
167 hdr->ssi_signal = htonl(rx_stats->signal);
168 hdr->ssi_noise = htonl(rx_stats->noise);
169 hdr->preamble = htonl(0); /* unknown */
170 hdr->encoding = htonl(1); /* cck */
171 } else if (prism_header == 3) {
172 struct hostap_radiotap_rx *hdr;
173 hdr = (struct hostap_radiotap_rx *)skb_push(skb, phdrlen);
174 memset(hdr, 0, phdrlen);
175 hdr->hdr.it_len = cpu_to_le16(phdrlen);
176 hdr->hdr.it_present =
177 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
178 (1 << IEEE80211_RADIOTAP_CHANNEL) |
179 (1 << IEEE80211_RADIOTAP_RATE) |
180 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
181 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE));
182 hdr->tsft = cpu_to_le64(rx_stats->mac_time);
183 hdr->chan_freq = cpu_to_le16(freq_list[local->channel - 1]);
184 hdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_CCK |
185 IEEE80211_CHAN_2GHZ);
186 hdr->rate = rx_stats->rate / 5;
187 hdr->dbm_antsignal = rx_stats->signal;
188 hdr->dbm_antnoise = rx_stats->noise;
191 ret = skb->len - phdrlen;
193 skb_reset_mac_header(skb);
194 skb_pull(skb, hdrlen);
196 skb_pull(skb, phdrlen);
197 skb->pkt_type = PACKET_OTHERHOST;
198 skb->protocol = cpu_to_be16(ETH_P_802_2);
199 memset(skb->cb, 0, sizeof(skb->cb));
206 /* Called only as a tasklet (software IRQ) */
207 static void monitor_rx(struct net_device *dev, struct sk_buff *skb,
208 struct hostap_80211_rx_status *rx_stats)
210 struct net_device_stats *stats;
213 len = prism2_rx_80211(dev, skb, rx_stats, PRISM2_RX_MONITOR);
214 stats = hostap_get_stats(dev);
216 stats->rx_bytes += len;
220 /* Called only as a tasklet (software IRQ) */
221 static struct prism2_frag_entry *
222 prism2_frag_cache_find(local_info_t *local, unsigned int seq,
223 unsigned int frag, u8 *src, u8 *dst)
225 struct prism2_frag_entry *entry;
228 for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
229 entry = &local->frag_cache[i];
230 if (entry->skb != NULL &&
231 time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
232 printk(KERN_DEBUG "%s: expiring fragment cache entry "
233 "seq=%u last_frag=%u\n",
234 local->dev->name, entry->seq, entry->last_frag);
235 dev_kfree_skb(entry->skb);
239 if (entry->skb != NULL && entry->seq == seq &&
240 (entry->last_frag + 1 == frag || frag == -1) &&
241 memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
242 memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
250 /* Called only as a tasklet (software IRQ) */
251 static struct sk_buff *
252 prism2_frag_cache_get(local_info_t *local, struct ieee80211_hdr *hdr)
254 struct sk_buff *skb = NULL;
256 unsigned int frag, seq;
257 struct prism2_frag_entry *entry;
259 sc = le16_to_cpu(hdr->seq_ctrl);
260 frag = sc & IEEE80211_SCTL_FRAG;
261 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
264 /* Reserve enough space to fit maximum frame length */
265 skb = dev_alloc_skb(local->dev->mtu +
266 sizeof(struct ieee80211_hdr) +
269 8 /* WEP */ + ETH_ALEN /* WDS */);
273 entry = &local->frag_cache[local->frag_next_idx];
274 local->frag_next_idx++;
275 if (local->frag_next_idx >= PRISM2_FRAG_CACHE_LEN)
276 local->frag_next_idx = 0;
278 if (entry->skb != NULL)
279 dev_kfree_skb(entry->skb);
281 entry->first_frag_time = jiffies;
283 entry->last_frag = frag;
285 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
286 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
288 /* received a fragment of a frame for which the head fragment
289 * should have already been received */
290 entry = prism2_frag_cache_find(local, seq, frag, hdr->addr2,
293 entry->last_frag = frag;
302 /* Called only as a tasklet (software IRQ) */
303 static int prism2_frag_cache_invalidate(local_info_t *local,
304 struct ieee80211_hdr *hdr)
308 struct prism2_frag_entry *entry;
310 sc = le16_to_cpu(hdr->seq_ctrl);
311 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
313 entry = prism2_frag_cache_find(local, seq, -1, hdr->addr2, hdr->addr1);
316 printk(KERN_DEBUG "%s: could not invalidate fragment cache "
318 local->dev->name, seq);
327 static struct hostap_bss_info *__hostap_get_bss(local_info_t *local, u8 *bssid,
328 u8 *ssid, size_t ssid_len)
330 struct list_head *ptr;
331 struct hostap_bss_info *bss;
333 list_for_each(ptr, &local->bss_list) {
334 bss = list_entry(ptr, struct hostap_bss_info, list);
335 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
337 (ssid_len == bss->ssid_len &&
338 memcmp(ssid, bss->ssid, ssid_len) == 0))) {
339 list_move(&bss->list, &local->bss_list);
348 static struct hostap_bss_info *__hostap_add_bss(local_info_t *local, u8 *bssid,
349 u8 *ssid, size_t ssid_len)
351 struct hostap_bss_info *bss;
353 if (local->num_bss_info >= HOSTAP_MAX_BSS_COUNT) {
354 bss = list_entry(local->bss_list.prev,
355 struct hostap_bss_info, list);
356 list_del(&bss->list);
357 local->num_bss_info--;
359 bss = (struct hostap_bss_info *)
360 kmalloc(sizeof(*bss), GFP_ATOMIC);
365 memset(bss, 0, sizeof(*bss));
366 memcpy(bss->bssid, bssid, ETH_ALEN);
367 memcpy(bss->ssid, ssid, ssid_len);
368 bss->ssid_len = ssid_len;
369 local->num_bss_info++;
370 list_add(&bss->list, &local->bss_list);
375 static void __hostap_expire_bss(local_info_t *local)
377 struct hostap_bss_info *bss;
379 while (local->num_bss_info > 0) {
380 bss = list_entry(local->bss_list.prev,
381 struct hostap_bss_info, list);
382 if (!time_after(jiffies, bss->last_update + 60 * HZ))
385 list_del(&bss->list);
386 local->num_bss_info--;
392 /* Both IEEE 802.11 Beacon and Probe Response frames have similar structure, so
393 * the same routine can be used to parse both of them. */
394 static void hostap_rx_sta_beacon(local_info_t *local, struct sk_buff *skb,
397 struct hostap_ieee80211_mgmt *mgmt;
400 u8 *ssid = NULL, *wpa = NULL, *rsn = NULL;
401 size_t ssid_len = 0, wpa_len = 0, rsn_len = 0;
402 struct hostap_bss_info *bss;
404 if (skb->len < IEEE80211_MGMT_HDR_LEN + sizeof(mgmt->u.beacon))
407 mgmt = (struct hostap_ieee80211_mgmt *) skb->data;
408 pos = mgmt->u.beacon.variable;
409 left = skb->len - (pos - skb->data);
412 if (2 + pos[1] > left)
413 return; /* parse failed */
419 case WLAN_EID_GENERIC:
421 pos[2] == 0x00 && pos[3] == 0x50 &&
422 pos[4] == 0xf2 && pos[5] == 1) {
424 wpa_len = pos[1] + 2;
429 rsn_len = pos[1] + 2;
431 case WLAN_EID_DS_PARAMS:
440 if (wpa_len > MAX_WPA_IE_LEN)
441 wpa_len = MAX_WPA_IE_LEN;
442 if (rsn_len > MAX_WPA_IE_LEN)
443 rsn_len = MAX_WPA_IE_LEN;
444 if (ssid_len > sizeof(bss->ssid))
445 ssid_len = sizeof(bss->ssid);
447 spin_lock(&local->lock);
448 bss = __hostap_get_bss(local, mgmt->bssid, ssid, ssid_len);
450 bss = __hostap_add_bss(local, mgmt->bssid, ssid, ssid_len);
452 bss->last_update = jiffies;
454 bss->capab_info = le16_to_cpu(mgmt->u.beacon.capab_info);
456 memcpy(bss->wpa_ie, wpa, wpa_len);
457 bss->wpa_ie_len = wpa_len;
461 memcpy(bss->rsn_ie, rsn, rsn_len);
462 bss->rsn_ie_len = rsn_len;
467 __hostap_expire_bss(local);
468 spin_unlock(&local->lock);
473 hostap_rx_frame_mgmt(local_info_t *local, struct sk_buff *skb,
474 struct hostap_80211_rx_status *rx_stats, u16 type,
477 if (local->iw_mode == IW_MODE_MASTER)
478 hostap_update_sta_ps(local, (struct ieee80211_hdr *) skb->data);
480 if (local->hostapd && type == IEEE80211_FTYPE_MGMT) {
481 if (stype == IEEE80211_STYPE_BEACON &&
482 local->iw_mode == IW_MODE_MASTER) {
483 struct sk_buff *skb2;
484 /* Process beacon frames also in kernel driver to
485 * update STA(AP) table statistics */
486 skb2 = skb_clone(skb, GFP_ATOMIC);
488 hostap_rx(skb2->dev, skb2, rx_stats);
491 /* send management frames to the user space daemon for
493 local->apdevstats.rx_packets++;
494 local->apdevstats.rx_bytes += skb->len;
495 if (local->apdev == NULL)
497 prism2_rx_80211(local->apdev, skb, rx_stats, PRISM2_RX_MGMT);
501 if (local->iw_mode == IW_MODE_MASTER) {
502 if (type != IEEE80211_FTYPE_MGMT &&
503 type != IEEE80211_FTYPE_CTL) {
504 printk(KERN_DEBUG "%s: unknown management frame "
505 "(type=0x%02x, stype=0x%02x) dropped\n",
506 skb->dev->name, type >> 2, stype >> 4);
510 hostap_rx(skb->dev, skb, rx_stats);
512 } else if (type == IEEE80211_FTYPE_MGMT &&
513 (stype == IEEE80211_STYPE_BEACON ||
514 stype == IEEE80211_STYPE_PROBE_RESP)) {
515 hostap_rx_sta_beacon(local, skb, stype);
517 } else if (type == IEEE80211_FTYPE_MGMT &&
518 (stype == IEEE80211_STYPE_ASSOC_RESP ||
519 stype == IEEE80211_STYPE_REASSOC_RESP)) {
520 /* Ignore (Re)AssocResp silently since these are not currently
521 * needed but are still received when WPA/RSN mode is enabled.
525 printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: dropped unhandled"
526 " management frame in non-Host AP mode (type=%d:%d)\n",
527 skb->dev->name, type >> 2, stype >> 4);
533 /* Called only as a tasklet (software IRQ) */
534 static struct net_device *prism2_rx_get_wds(local_info_t *local,
537 struct hostap_interface *iface = NULL;
538 struct list_head *ptr;
540 read_lock_bh(&local->iface_lock);
541 list_for_each(ptr, &local->hostap_interfaces) {
542 iface = list_entry(ptr, struct hostap_interface, list);
543 if (iface->type == HOSTAP_INTERFACE_WDS &&
544 memcmp(iface->u.wds.remote_addr, addr, ETH_ALEN) == 0)
548 read_unlock_bh(&local->iface_lock);
550 return iface ? iface->dev : NULL;
555 hostap_rx_frame_wds(local_info_t *local, struct ieee80211_hdr *hdr, u16 fc,
556 struct net_device **wds)
558 /* FIX: is this really supposed to accept WDS frames only in Master
559 * mode? What about Repeater or Managed with WDS frames? */
560 if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) !=
561 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS) &&
562 (local->iw_mode != IW_MODE_MASTER || !(fc & IEEE80211_FCTL_TODS)))
563 return 0; /* not a WDS frame */
565 /* Possible WDS frame: either IEEE 802.11 compliant (if FromDS)
566 * or own non-standard frame with 4th address after payload */
567 if (memcmp(hdr->addr1, local->dev->dev_addr, ETH_ALEN) != 0 &&
568 (hdr->addr1[0] != 0xff || hdr->addr1[1] != 0xff ||
569 hdr->addr1[2] != 0xff || hdr->addr1[3] != 0xff ||
570 hdr->addr1[4] != 0xff || hdr->addr1[5] != 0xff)) {
571 /* RA (or BSSID) is not ours - drop */
572 PDEBUG(DEBUG_EXTRA2, "%s: received WDS frame with "
573 "not own or broadcast %s=%pM\n",
575 fc & IEEE80211_FCTL_FROMDS ? "RA" : "BSSID",
580 /* check if the frame came from a registered WDS connection */
581 *wds = prism2_rx_get_wds(local, hdr->addr2);
582 if (*wds == NULL && fc & IEEE80211_FCTL_FROMDS &&
583 (local->iw_mode != IW_MODE_INFRA ||
584 !(local->wds_type & HOSTAP_WDS_AP_CLIENT) ||
585 memcmp(hdr->addr2, local->bssid, ETH_ALEN) != 0)) {
586 /* require that WDS link has been registered with TA or the
587 * frame is from current AP when using 'AP client mode' */
588 PDEBUG(DEBUG_EXTRA, "%s: received WDS[4 addr] frame "
589 "from unknown TA=%pM\n",
590 local->dev->name, hdr->addr2);
591 if (local->ap && local->ap->autom_ap_wds)
592 hostap_wds_link_oper(local, hdr->addr2, WDS_ADD);
596 if (*wds && !(fc & IEEE80211_FCTL_FROMDS) && local->ap &&
597 hostap_is_sta_assoc(local->ap, hdr->addr2)) {
598 /* STA is actually associated with us even though it has a
599 * registered WDS link. Assume it is in 'AP client' mode.
600 * Since this is a 3-addr frame, assume it is not (bogus) WDS
601 * frame and process it like any normal ToDS frame from
610 static int hostap_is_eapol_frame(local_info_t *local, struct sk_buff *skb)
612 struct net_device *dev = local->dev;
614 struct ieee80211_hdr *hdr;
620 hdr = (struct ieee80211_hdr *) skb->data;
621 fc = le16_to_cpu(hdr->frame_control);
623 /* check that the frame is unicast frame to us */
624 if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
625 IEEE80211_FCTL_TODS &&
626 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
627 memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
628 /* ToDS frame with own addr BSSID and DA */
629 } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
630 IEEE80211_FCTL_FROMDS &&
631 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
632 /* FromDS frame with own addr as DA */
636 if (skb->len < 24 + 8)
639 /* check for port access entity Ethernet type */
640 pos = skb->data + 24;
641 ethertype = (pos[6] << 8) | pos[7];
642 if (ethertype == ETH_P_PAE)
649 /* Called only as a tasklet (software IRQ) */
651 hostap_rx_frame_decrypt(local_info_t *local, struct sk_buff *skb,
652 struct lib80211_crypt_data *crypt)
654 struct ieee80211_hdr *hdr;
657 if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
660 hdr = (struct ieee80211_hdr *) skb->data;
661 hdrlen = hostap_80211_get_hdrlen(hdr->frame_control);
663 if (local->tkip_countermeasures &&
664 strcmp(crypt->ops->name, "TKIP") == 0) {
665 if (net_ratelimit()) {
666 printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
667 "received packet from %pM\n",
668 local->dev->name, hdr->addr2);
673 atomic_inc(&crypt->refcnt);
674 res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
675 atomic_dec(&crypt->refcnt);
677 printk(KERN_DEBUG "%s: decryption failed (SA=%pM) res=%d\n",
678 local->dev->name, hdr->addr2, res);
679 local->comm_tallies.rx_discards_wep_undecryptable++;
687 /* Called only as a tasklet (software IRQ) */
689 hostap_rx_frame_decrypt_msdu(local_info_t *local, struct sk_buff *skb,
690 int keyidx, struct lib80211_crypt_data *crypt)
692 struct ieee80211_hdr *hdr;
695 if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
698 hdr = (struct ieee80211_hdr *) skb->data;
699 hdrlen = hostap_80211_get_hdrlen(hdr->frame_control);
701 atomic_inc(&crypt->refcnt);
702 res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
703 atomic_dec(&crypt->refcnt);
705 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
706 " (SA=%pM keyidx=%d)\n",
707 local->dev->name, hdr->addr2, keyidx);
715 /* All received frames are sent to this function. @skb contains the frame in
716 * IEEE 802.11 format, i.e., in the format it was sent over air.
717 * This function is called only as a tasklet (software IRQ). */
718 void hostap_80211_rx(struct net_device *dev, struct sk_buff *skb,
719 struct hostap_80211_rx_status *rx_stats)
721 struct hostap_interface *iface;
723 struct ieee80211_hdr *hdr;
725 u16 fc, type, stype, sc;
726 struct net_device *wds = NULL;
727 struct net_device_stats *stats;
730 struct sk_buff *skb2 = NULL;
732 int frame_authorized = 0;
733 int from_assoc_ap = 0;
736 struct lib80211_crypt_data *crypt = NULL;
740 iface = netdev_priv(dev);
741 local = iface->local;
742 iface->stats.rx_packets++;
743 iface->stats.rx_bytes += skb->len;
745 /* dev is the master radio device; change this to be the default
746 * virtual interface (this may be changed to WDS device below) */
748 iface = netdev_priv(dev);
750 hdr = (struct ieee80211_hdr *) skb->data;
751 stats = hostap_get_stats(dev);
756 fc = le16_to_cpu(hdr->frame_control);
757 type = fc & IEEE80211_FCTL_FTYPE;
758 stype = fc & IEEE80211_FCTL_STYPE;
759 sc = le16_to_cpu(hdr->seq_ctrl);
760 frag = sc & IEEE80211_SCTL_FRAG;
761 hdrlen = hostap_80211_get_hdrlen(hdr->frame_control);
763 /* Put this code here so that we avoid duplicating it in all
764 * Rx paths. - Jean II */
765 #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
766 /* If spy monitoring on */
767 if (iface->spy_data.spy_number > 0) {
768 struct iw_quality wstats;
769 wstats.level = rx_stats->signal;
770 wstats.noise = rx_stats->noise;
771 wstats.updated = IW_QUAL_LEVEL_UPDATED | IW_QUAL_NOISE_UPDATED
772 | IW_QUAL_QUAL_INVALID | IW_QUAL_DBM;
773 /* Update spy records */
774 wireless_spy_update(dev, hdr->addr2, &wstats);
776 #endif /* IW_WIRELESS_SPY */
777 hostap_update_rx_stats(local->ap, hdr, rx_stats);
779 if (local->iw_mode == IW_MODE_MONITOR) {
780 monitor_rx(dev, skb, rx_stats);
784 if (local->host_decrypt) {
786 if (skb->len >= hdrlen + 3)
787 idx = skb->data[hdrlen + 3] >> 6;
788 crypt = local->crypt_info.crypt[idx];
791 /* Use station specific key to override default keys if the
792 * receiver address is a unicast address ("individual RA"). If
793 * bcrx_sta_key parameter is set, station specific key is used
794 * even with broad/multicast targets (this is against IEEE
795 * 802.11, but makes it easier to use different keys with
796 * stations that do not support WEP key mapping). */
798 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
799 (void) hostap_handle_sta_crypto(local, hdr, &crypt,
802 /* allow NULL decrypt to indicate an station specific override
803 * for default encryption */
804 if (crypt && (crypt->ops == NULL ||
805 crypt->ops->decrypt_mpdu == NULL))
808 if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
810 /* This seems to be triggered by some (multicast?)
811 * frames from other than current BSS, so just drop the
812 * frames silently instead of filling system log with
814 printk(KERN_DEBUG "%s: WEP decryption failed (not set)"
816 local->dev->name, hdr->addr2);
818 local->comm_tallies.rx_discards_wep_undecryptable++;
823 if (type != IEEE80211_FTYPE_DATA) {
824 if (type == IEEE80211_FTYPE_MGMT &&
825 stype == IEEE80211_STYPE_AUTH &&
826 fc & IEEE80211_FCTL_PROTECTED && local->host_decrypt &&
827 (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
829 printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
830 "from %pM\n", dev->name, hdr->addr2);
831 /* TODO: could inform hostapd about this so that it
832 * could send auth failure report */
836 if (hostap_rx_frame_mgmt(local, skb, rx_stats, type, stype))
842 /* Data frame - extract src/dst addresses */
843 if (skb->len < IEEE80211_DATA_HDR3_LEN)
846 switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
847 case IEEE80211_FCTL_FROMDS:
848 memcpy(dst, hdr->addr1, ETH_ALEN);
849 memcpy(src, hdr->addr3, ETH_ALEN);
851 case IEEE80211_FCTL_TODS:
852 memcpy(dst, hdr->addr3, ETH_ALEN);
853 memcpy(src, hdr->addr2, ETH_ALEN);
855 case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
856 if (skb->len < IEEE80211_DATA_HDR4_LEN)
858 memcpy(dst, hdr->addr3, ETH_ALEN);
859 memcpy(src, hdr->addr4, ETH_ALEN);
862 memcpy(dst, hdr->addr1, ETH_ALEN);
863 memcpy(src, hdr->addr2, ETH_ALEN);
867 if (hostap_rx_frame_wds(local, hdr, fc, &wds))
870 skb->dev = dev = wds;
871 stats = hostap_get_stats(dev);
874 if (local->iw_mode == IW_MODE_MASTER && !wds &&
875 (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
876 IEEE80211_FCTL_FROMDS &&
878 memcmp(hdr->addr2, local->assoc_ap_addr, ETH_ALEN) == 0) {
879 /* Frame from BSSID of the AP for which we are a client */
880 skb->dev = dev = local->stadev;
881 stats = hostap_get_stats(dev);
885 if ((local->iw_mode == IW_MODE_MASTER ||
886 local->iw_mode == IW_MODE_REPEAT) &&
888 switch (hostap_handle_sta_rx(local, dev, skb, rx_stats,
890 case AP_RX_CONTINUE_NOT_AUTHORIZED:
891 frame_authorized = 0;
894 frame_authorized = 1;
903 /* Nullfunc frames may have PS-bit set, so they must be passed to
904 * hostap_handle_sta_rx() before being dropped here. */
905 if (stype != IEEE80211_STYPE_DATA &&
906 stype != IEEE80211_STYPE_DATA_CFACK &&
907 stype != IEEE80211_STYPE_DATA_CFPOLL &&
908 stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
909 if (stype != IEEE80211_STYPE_NULLFUNC)
910 printk(KERN_DEBUG "%s: RX: dropped data frame "
911 "with no data (type=0x%02x, subtype=0x%02x)\n",
912 dev->name, type >> 2, stype >> 4);
916 /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
918 if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
919 (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
921 hdr = (struct ieee80211_hdr *) skb->data;
923 /* skb: hdr + (possibly fragmented) plaintext payload */
925 if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
926 (frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
928 struct sk_buff *frag_skb =
929 prism2_frag_cache_get(local, hdr);
931 printk(KERN_DEBUG "%s: Rx cannot get skb from "
932 "fragment cache (morefrag=%d seq=%u frag=%u)\n",
933 dev->name, (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
934 (sc & IEEE80211_SCTL_SEQ) >> 4, frag);
942 if (frag_skb->tail + flen > frag_skb->end) {
943 printk(KERN_WARNING "%s: host decrypted and "
944 "reassembled frame did not fit skb\n",
946 prism2_frag_cache_invalidate(local, hdr);
951 /* copy first fragment (including full headers) into
952 * beginning of the fragment cache skb */
953 skb_copy_from_linear_data(skb, skb_put(frag_skb, flen),
956 /* append frame payload to the end of the fragment
958 skb_copy_from_linear_data_offset(skb, hdrlen,
965 if (fc & IEEE80211_FCTL_MOREFRAGS) {
966 /* more fragments expected - leave the skb in fragment
967 * cache for now; it will be delivered to upper layers
968 * after all fragments have been received */
972 /* this was the last fragment and the frame will be
973 * delivered, so remove skb from fragment cache */
975 hdr = (struct ieee80211_hdr *) skb->data;
976 prism2_frag_cache_invalidate(local, hdr);
979 /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
980 * encrypted/authenticated */
982 if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
983 hostap_rx_frame_decrypt_msdu(local, skb, keyidx, crypt))
986 hdr = (struct ieee80211_hdr *) skb->data;
987 if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !local->open_wep) {
988 if (local->ieee_802_1x &&
989 hostap_is_eapol_frame(local, skb)) {
990 /* pass unencrypted EAPOL frames even if encryption is
992 PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X - passing "
993 "unencrypted EAPOL frame\n", local->dev->name);
995 printk(KERN_DEBUG "%s: encryption configured, but RX "
996 "frame not encrypted (SA=%pM)\n",
997 local->dev->name, hdr->addr2);
1002 if (local->drop_unencrypted && !(fc & IEEE80211_FCTL_PROTECTED) &&
1003 !hostap_is_eapol_frame(local, skb)) {
1004 if (net_ratelimit()) {
1005 printk(KERN_DEBUG "%s: dropped unencrypted RX data "
1006 "frame from %pM (drop_unencrypted=1)\n",
1007 dev->name, hdr->addr2);
1012 /* skb: hdr + (possible reassembled) full plaintext payload */
1014 payload = skb->data + hdrlen;
1015 ethertype = (payload[6] << 8) | payload[7];
1017 /* If IEEE 802.1X is used, check whether the port is authorized to send
1018 * the received frame. */
1019 if (local->ieee_802_1x && local->iw_mode == IW_MODE_MASTER) {
1020 if (ethertype == ETH_P_PAE) {
1021 PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X frame\n",
1023 if (local->hostapd && local->apdev) {
1024 /* Send IEEE 802.1X frames to the user
1025 * space daemon for processing */
1026 prism2_rx_80211(local->apdev, skb, rx_stats,
1028 local->apdevstats.rx_packets++;
1029 local->apdevstats.rx_bytes += skb->len;
1032 } else if (!frame_authorized) {
1033 printk(KERN_DEBUG "%s: dropped frame from "
1034 "unauthorized port (IEEE 802.1X): "
1035 "ethertype=0x%04x\n",
1036 dev->name, ethertype);
1041 /* convert hdr + possible LLC headers into Ethernet header */
1042 if (skb->len - hdrlen >= 8 &&
1043 ((memcmp(payload, rfc1042_header, 6) == 0 &&
1044 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1045 memcmp(payload, bridge_tunnel_header, 6) == 0)) {
1046 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1047 * replace EtherType */
1048 skb_pull(skb, hdrlen + 6);
1049 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1050 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1053 /* Leave Ethernet header part of hdr and full payload */
1054 skb_pull(skb, hdrlen);
1055 len = htons(skb->len);
1056 memcpy(skb_push(skb, 2), &len, 2);
1057 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1058 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1061 if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
1062 IEEE80211_FCTL_TODS) &&
1063 skb->len >= ETH_HLEN + ETH_ALEN) {
1064 /* Non-standard frame: get addr4 from its bogus location after
1066 skb_copy_from_linear_data_offset(skb, skb->len - ETH_ALEN,
1067 skb->data + ETH_ALEN,
1069 skb_trim(skb, skb->len - ETH_ALEN);
1072 stats->rx_packets++;
1073 stats->rx_bytes += skb->len;
1075 if (local->iw_mode == IW_MODE_MASTER && !wds &&
1076 local->ap->bridge_packets) {
1077 if (dst[0] & 0x01) {
1078 /* copy multicast frame both to the higher layers and
1079 * to the wireless media */
1080 local->ap->bridged_multicast++;
1081 skb2 = skb_clone(skb, GFP_ATOMIC);
1083 printk(KERN_DEBUG "%s: skb_clone failed for "
1084 "multicast frame\n", dev->name);
1085 } else if (hostap_is_sta_authorized(local->ap, dst)) {
1086 /* send frame directly to the associated STA using
1087 * wireless media and not passing to higher layers */
1088 local->ap->bridged_unicast++;
1095 /* send to wireless media */
1097 skb2->protocol = cpu_to_be16(ETH_P_802_3);
1098 skb_reset_mac_header(skb2);
1099 skb_reset_network_header(skb2);
1100 /* skb2->network_header += ETH_HLEN; */
1101 dev_queue_xmit(skb2);
1105 skb->protocol = eth_type_trans(skb, dev);
1106 memset(skb->cb, 0, sizeof(skb->cb));
1112 hostap_handle_sta_release(sta);
1118 stats->rx_dropped++;
1123 EXPORT_SYMBOL(hostap_80211_rx);