1 #include <linux/etherdevice.h>
2 #include <net/ieee80211_crypt.h>
4 #include "hostap_80211.h"
8 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
9 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
10 static unsigned char rfc1042_header[] =
11 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
12 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
13 static unsigned char bridge_tunnel_header[] =
14 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
15 /* No encapsulation header if EtherType < 0x600 (=length) */
17 void hostap_dump_rx_80211(const char *name, struct sk_buff *skb,
18 struct hostap_80211_rx_status *rx_stats)
20 struct ieee80211_hdr_4addr *hdr;
23 hdr = (struct ieee80211_hdr_4addr *) skb->data;
25 printk(KERN_DEBUG "%s: RX signal=%d noise=%d rate=%d len=%d "
27 name, rx_stats->signal, rx_stats->noise, rx_stats->rate,
33 fc = le16_to_cpu(hdr->frame_ctl);
34 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d)%s%s",
35 fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
36 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
37 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
39 if (skb->len < IEEE80211_DATA_HDR3_LEN) {
44 printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
45 le16_to_cpu(hdr->seq_ctl));
47 printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR,
48 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), MAC2STR(hdr->addr3));
50 printk(" A4=" MACSTR, MAC2STR(hdr->addr4));
55 /* Send RX frame to netif with 802.11 (and possible prism) header.
56 * Called from hardware or software IRQ context. */
57 int prism2_rx_80211(struct net_device *dev, struct sk_buff *skb,
58 struct hostap_80211_rx_status *rx_stats, int type)
60 struct hostap_interface *iface;
62 int hdrlen, phdrlen, head_need, tail_need;
64 int prism_header, ret;
65 struct ieee80211_hdr_4addr *hdr;
67 iface = netdev_priv(dev);
69 dev->last_rx = jiffies;
71 if (dev->type == ARPHRD_IEEE80211_PRISM) {
72 if (local->monitor_type == PRISM2_MONITOR_PRISM) {
74 phdrlen = sizeof(struct linux_wlan_ng_prism_hdr);
75 } else { /* local->monitor_type == PRISM2_MONITOR_CAPHDR */
77 phdrlen = sizeof(struct linux_wlan_ng_cap_hdr);
84 hdr = (struct ieee80211_hdr_4addr *) skb->data;
85 fc = le16_to_cpu(hdr->frame_ctl);
87 if (type == PRISM2_RX_MGMT && (fc & IEEE80211_FCTL_VERS)) {
88 printk(KERN_DEBUG "%s: dropped management frame with header "
89 "version %d\n", dev->name, fc & IEEE80211_FCTL_VERS);
90 dev_kfree_skb_any(skb);
94 hdrlen = hostap_80211_get_hdrlen(fc);
96 /* check if there is enough room for extra data; if not, expand skb
97 * buffer to be large enough for the changes */
100 #ifdef PRISM2_ADD_BOGUS_CRC
102 #endif /* PRISM2_ADD_BOGUS_CRC */
104 head_need -= skb_headroom(skb);
105 tail_need -= skb_tailroom(skb);
107 if (head_need > 0 || tail_need > 0) {
108 if (pskb_expand_head(skb, head_need > 0 ? head_need : 0,
109 tail_need > 0 ? tail_need : 0,
111 printk(KERN_DEBUG "%s: prism2_rx_80211 failed to "
112 "reallocate skb buffer\n", dev->name);
113 dev_kfree_skb_any(skb);
118 /* We now have an skb with enough head and tail room, so just insert
121 #ifdef PRISM2_ADD_BOGUS_CRC
122 memset(skb_put(skb, 4), 0xff, 4); /* Prism2 strips CRC */
123 #endif /* PRISM2_ADD_BOGUS_CRC */
125 if (prism_header == 1) {
126 struct linux_wlan_ng_prism_hdr *hdr;
127 hdr = (struct linux_wlan_ng_prism_hdr *)
128 skb_push(skb, phdrlen);
129 memset(hdr, 0, phdrlen);
130 hdr->msgcode = LWNG_CAP_DID_BASE;
131 hdr->msglen = sizeof(*hdr);
132 memcpy(hdr->devname, dev->name, sizeof(hdr->devname));
133 #define LWNG_SETVAL(f,i,s,l,d) \
134 hdr->f.did = LWNG_CAP_DID_BASE | (i << 12); \
135 hdr->f.status = s; hdr->f.len = l; hdr->f.data = d
136 LWNG_SETVAL(hosttime, 1, 0, 4, jiffies);
137 LWNG_SETVAL(mactime, 2, 0, 4, rx_stats->mac_time);
138 LWNG_SETVAL(channel, 3, 1 /* no value */, 4, 0);
139 LWNG_SETVAL(rssi, 4, 1 /* no value */, 4, 0);
140 LWNG_SETVAL(sq, 5, 1 /* no value */, 4, 0);
141 LWNG_SETVAL(signal, 6, 0, 4, rx_stats->signal);
142 LWNG_SETVAL(noise, 7, 0, 4, rx_stats->noise);
143 LWNG_SETVAL(rate, 8, 0, 4, rx_stats->rate / 5);
144 LWNG_SETVAL(istx, 9, 0, 4, 0);
145 LWNG_SETVAL(frmlen, 10, 0, 4, skb->len - phdrlen);
147 } else if (prism_header == 2) {
148 struct linux_wlan_ng_cap_hdr *hdr;
149 hdr = (struct linux_wlan_ng_cap_hdr *)
150 skb_push(skb, phdrlen);
151 memset(hdr, 0, phdrlen);
152 hdr->version = htonl(LWNG_CAPHDR_VERSION);
153 hdr->length = htonl(phdrlen);
154 hdr->mactime = __cpu_to_be64(rx_stats->mac_time);
155 hdr->hosttime = __cpu_to_be64(jiffies);
156 hdr->phytype = htonl(4); /* dss_dot11_b */
157 hdr->channel = htonl(local->channel);
158 hdr->datarate = htonl(rx_stats->rate);
159 hdr->antenna = htonl(0); /* unknown */
160 hdr->priority = htonl(0); /* unknown */
161 hdr->ssi_type = htonl(3); /* raw */
162 hdr->ssi_signal = htonl(rx_stats->signal);
163 hdr->ssi_noise = htonl(rx_stats->noise);
164 hdr->preamble = htonl(0); /* unknown */
165 hdr->encoding = htonl(1); /* cck */
168 ret = skb->len - phdrlen;
170 skb->mac.raw = skb->data;
171 skb_pull(skb, hdrlen);
173 skb_pull(skb, phdrlen);
174 skb->pkt_type = PACKET_OTHERHOST;
175 skb->protocol = __constant_htons(ETH_P_802_2);
176 memset(skb->cb, 0, sizeof(skb->cb));
183 /* Called only as a tasklet (software IRQ) */
184 static void monitor_rx(struct net_device *dev, struct sk_buff *skb,
185 struct hostap_80211_rx_status *rx_stats)
187 struct net_device_stats *stats;
190 len = prism2_rx_80211(dev, skb, rx_stats, PRISM2_RX_MONITOR);
191 stats = hostap_get_stats(dev);
193 stats->rx_bytes += len;
197 /* Called only as a tasklet (software IRQ) */
198 static struct prism2_frag_entry *
199 prism2_frag_cache_find(local_info_t *local, unsigned int seq,
200 unsigned int frag, u8 *src, u8 *dst)
202 struct prism2_frag_entry *entry;
205 for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
206 entry = &local->frag_cache[i];
207 if (entry->skb != NULL &&
208 time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
209 printk(KERN_DEBUG "%s: expiring fragment cache entry "
210 "seq=%u last_frag=%u\n",
211 local->dev->name, entry->seq, entry->last_frag);
212 dev_kfree_skb(entry->skb);
216 if (entry->skb != NULL && entry->seq == seq &&
217 (entry->last_frag + 1 == frag || frag == -1) &&
218 memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
219 memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
227 /* Called only as a tasklet (software IRQ) */
228 static struct sk_buff *
229 prism2_frag_cache_get(local_info_t *local, struct ieee80211_hdr_4addr *hdr)
231 struct sk_buff *skb = NULL;
233 unsigned int frag, seq;
234 struct prism2_frag_entry *entry;
236 sc = le16_to_cpu(hdr->seq_ctl);
237 frag = WLAN_GET_SEQ_FRAG(sc);
238 seq = WLAN_GET_SEQ_SEQ(sc) >> 4;
241 /* Reserve enough space to fit maximum frame length */
242 skb = dev_alloc_skb(local->dev->mtu +
243 sizeof(struct ieee80211_hdr_4addr) +
246 8 /* WEP */ + ETH_ALEN /* WDS */);
250 entry = &local->frag_cache[local->frag_next_idx];
251 local->frag_next_idx++;
252 if (local->frag_next_idx >= PRISM2_FRAG_CACHE_LEN)
253 local->frag_next_idx = 0;
255 if (entry->skb != NULL)
256 dev_kfree_skb(entry->skb);
258 entry->first_frag_time = jiffies;
260 entry->last_frag = frag;
262 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
263 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
265 /* received a fragment of a frame for which the head fragment
266 * should have already been received */
267 entry = prism2_frag_cache_find(local, seq, frag, hdr->addr2,
270 entry->last_frag = frag;
279 /* Called only as a tasklet (software IRQ) */
280 static int prism2_frag_cache_invalidate(local_info_t *local,
281 struct ieee80211_hdr_4addr *hdr)
285 struct prism2_frag_entry *entry;
287 sc = le16_to_cpu(hdr->seq_ctl);
288 seq = WLAN_GET_SEQ_SEQ(sc) >> 4;
290 entry = prism2_frag_cache_find(local, seq, -1, hdr->addr2, hdr->addr1);
293 printk(KERN_DEBUG "%s: could not invalidate fragment cache "
295 local->dev->name, seq);
304 static struct hostap_bss_info *__hostap_get_bss(local_info_t *local, u8 *bssid,
305 u8 *ssid, size_t ssid_len)
307 struct list_head *ptr;
308 struct hostap_bss_info *bss;
310 list_for_each(ptr, &local->bss_list) {
311 bss = list_entry(ptr, struct hostap_bss_info, list);
312 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
314 (ssid_len == bss->ssid_len &&
315 memcmp(ssid, bss->ssid, ssid_len) == 0))) {
316 list_move(&bss->list, &local->bss_list);
325 static struct hostap_bss_info *__hostap_add_bss(local_info_t *local, u8 *bssid,
326 u8 *ssid, size_t ssid_len)
328 struct hostap_bss_info *bss;
330 if (local->num_bss_info >= HOSTAP_MAX_BSS_COUNT) {
331 bss = list_entry(local->bss_list.prev,
332 struct hostap_bss_info, list);
333 list_del(&bss->list);
334 local->num_bss_info--;
336 bss = (struct hostap_bss_info *)
337 kmalloc(sizeof(*bss), GFP_ATOMIC);
342 memset(bss, 0, sizeof(*bss));
343 memcpy(bss->bssid, bssid, ETH_ALEN);
344 memcpy(bss->ssid, ssid, ssid_len);
345 bss->ssid_len = ssid_len;
346 local->num_bss_info++;
347 list_add(&bss->list, &local->bss_list);
352 static void __hostap_expire_bss(local_info_t *local)
354 struct hostap_bss_info *bss;
356 while (local->num_bss_info > 0) {
357 bss = list_entry(local->bss_list.prev,
358 struct hostap_bss_info, list);
359 if (!time_after(jiffies, bss->last_update + 60 * HZ))
362 list_del(&bss->list);
363 local->num_bss_info--;
369 /* Both IEEE 802.11 Beacon and Probe Response frames have similar structure, so
370 * the same routine can be used to parse both of them. */
371 static void hostap_rx_sta_beacon(local_info_t *local, struct sk_buff *skb,
374 struct hostap_ieee80211_mgmt *mgmt;
377 u8 *ssid = NULL, *wpa = NULL, *rsn = NULL;
378 size_t ssid_len = 0, wpa_len = 0, rsn_len = 0;
379 struct hostap_bss_info *bss;
381 if (skb->len < IEEE80211_MGMT_HDR_LEN + sizeof(mgmt->u.beacon))
384 mgmt = (struct hostap_ieee80211_mgmt *) skb->data;
385 pos = mgmt->u.beacon.variable;
386 left = skb->len - (pos - skb->data);
389 if (2 + pos[1] > left)
390 return; /* parse failed */
396 case WLAN_EID_GENERIC:
398 pos[2] == 0x00 && pos[3] == 0x50 &&
399 pos[4] == 0xf2 && pos[5] == 1) {
401 wpa_len = pos[1] + 2;
406 rsn_len = pos[1] + 2;
408 case WLAN_EID_DS_PARAMS:
417 if (wpa_len > MAX_WPA_IE_LEN)
418 wpa_len = MAX_WPA_IE_LEN;
419 if (rsn_len > MAX_WPA_IE_LEN)
420 rsn_len = MAX_WPA_IE_LEN;
421 if (ssid_len > sizeof(bss->ssid))
422 ssid_len = sizeof(bss->ssid);
424 spin_lock(&local->lock);
425 bss = __hostap_get_bss(local, mgmt->bssid, ssid, ssid_len);
427 bss = __hostap_add_bss(local, mgmt->bssid, ssid, ssid_len);
429 bss->last_update = jiffies;
431 bss->capab_info = le16_to_cpu(mgmt->u.beacon.capab_info);
433 memcpy(bss->wpa_ie, wpa, wpa_len);
434 bss->wpa_ie_len = wpa_len;
438 memcpy(bss->rsn_ie, rsn, rsn_len);
439 bss->rsn_ie_len = rsn_len;
444 __hostap_expire_bss(local);
445 spin_unlock(&local->lock);
450 hostap_rx_frame_mgmt(local_info_t *local, struct sk_buff *skb,
451 struct hostap_80211_rx_status *rx_stats, u16 type,
454 if (local->iw_mode == IW_MODE_MASTER) {
455 hostap_update_sta_ps(local, (struct ieee80211_hdr_4addr *)
459 if (local->hostapd && type == IEEE80211_FTYPE_MGMT) {
460 if (stype == IEEE80211_STYPE_BEACON &&
461 local->iw_mode == IW_MODE_MASTER) {
462 struct sk_buff *skb2;
463 /* Process beacon frames also in kernel driver to
464 * update STA(AP) table statistics */
465 skb2 = skb_clone(skb, GFP_ATOMIC);
467 hostap_rx(skb2->dev, skb2, rx_stats);
470 /* send management frames to the user space daemon for
472 local->apdevstats.rx_packets++;
473 local->apdevstats.rx_bytes += skb->len;
474 if (local->apdev == NULL)
476 prism2_rx_80211(local->apdev, skb, rx_stats, PRISM2_RX_MGMT);
480 if (local->iw_mode == IW_MODE_MASTER) {
481 if (type != IEEE80211_FTYPE_MGMT &&
482 type != IEEE80211_FTYPE_CTL) {
483 printk(KERN_DEBUG "%s: unknown management frame "
484 "(type=0x%02x, stype=0x%02x) dropped\n",
485 skb->dev->name, type >> 2, stype >> 4);
489 hostap_rx(skb->dev, skb, rx_stats);
491 } else if (type == IEEE80211_FTYPE_MGMT &&
492 (stype == IEEE80211_STYPE_BEACON ||
493 stype == IEEE80211_STYPE_PROBE_RESP)) {
494 hostap_rx_sta_beacon(local, skb, stype);
496 } else if (type == IEEE80211_FTYPE_MGMT &&
497 (stype == IEEE80211_STYPE_ASSOC_RESP ||
498 stype == IEEE80211_STYPE_REASSOC_RESP)) {
499 /* Ignore (Re)AssocResp silently since these are not currently
500 * needed but are still received when WPA/RSN mode is enabled.
504 printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: dropped unhandled"
505 " management frame in non-Host AP mode (type=%d:%d)\n",
506 skb->dev->name, type >> 2, stype >> 4);
512 /* Called only as a tasklet (software IRQ) */
513 static struct net_device *prism2_rx_get_wds(local_info_t *local,
516 struct hostap_interface *iface = NULL;
517 struct list_head *ptr;
519 read_lock_bh(&local->iface_lock);
520 list_for_each(ptr, &local->hostap_interfaces) {
521 iface = list_entry(ptr, struct hostap_interface, list);
522 if (iface->type == HOSTAP_INTERFACE_WDS &&
523 memcmp(iface->u.wds.remote_addr, addr, ETH_ALEN) == 0)
527 read_unlock_bh(&local->iface_lock);
529 return iface ? iface->dev : NULL;
534 hostap_rx_frame_wds(local_info_t *local, struct ieee80211_hdr_4addr *hdr,
535 u16 fc, struct net_device **wds)
537 /* FIX: is this really supposed to accept WDS frames only in Master
538 * mode? What about Repeater or Managed with WDS frames? */
539 if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) !=
540 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS) &&
541 (local->iw_mode != IW_MODE_MASTER || !(fc & IEEE80211_FCTL_TODS)))
542 return 0; /* not a WDS frame */
544 /* Possible WDS frame: either IEEE 802.11 compliant (if FromDS)
545 * or own non-standard frame with 4th address after payload */
546 if (memcmp(hdr->addr1, local->dev->dev_addr, ETH_ALEN) != 0 &&
547 (hdr->addr1[0] != 0xff || hdr->addr1[1] != 0xff ||
548 hdr->addr1[2] != 0xff || hdr->addr1[3] != 0xff ||
549 hdr->addr1[4] != 0xff || hdr->addr1[5] != 0xff)) {
550 /* RA (or BSSID) is not ours - drop */
551 PDEBUG(DEBUG_EXTRA, "%s: received WDS frame with "
552 "not own or broadcast %s=" MACSTR "\n",
554 fc & IEEE80211_FCTL_FROMDS ? "RA" : "BSSID",
555 MAC2STR(hdr->addr1));
559 /* check if the frame came from a registered WDS connection */
560 *wds = prism2_rx_get_wds(local, hdr->addr2);
561 if (*wds == NULL && fc & IEEE80211_FCTL_FROMDS &&
562 (local->iw_mode != IW_MODE_INFRA ||
563 !(local->wds_type & HOSTAP_WDS_AP_CLIENT) ||
564 memcmp(hdr->addr2, local->bssid, ETH_ALEN) != 0)) {
565 /* require that WDS link has been registered with TA or the
566 * frame is from current AP when using 'AP client mode' */
567 PDEBUG(DEBUG_EXTRA, "%s: received WDS[4 addr] frame "
568 "from unknown TA=" MACSTR "\n",
569 local->dev->name, MAC2STR(hdr->addr2));
570 if (local->ap && local->ap->autom_ap_wds)
571 hostap_wds_link_oper(local, hdr->addr2, WDS_ADD);
575 if (*wds && !(fc & IEEE80211_FCTL_FROMDS) && local->ap &&
576 hostap_is_sta_assoc(local->ap, hdr->addr2)) {
577 /* STA is actually associated with us even though it has a
578 * registered WDS link. Assume it is in 'AP client' mode.
579 * Since this is a 3-addr frame, assume it is not (bogus) WDS
580 * frame and process it like any normal ToDS frame from
589 static int hostap_is_eapol_frame(local_info_t *local, struct sk_buff *skb)
591 struct net_device *dev = local->dev;
593 struct ieee80211_hdr_4addr *hdr;
599 hdr = (struct ieee80211_hdr_4addr *) skb->data;
600 fc = le16_to_cpu(hdr->frame_ctl);
602 /* check that the frame is unicast frame to us */
603 if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
604 IEEE80211_FCTL_TODS &&
605 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
606 memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
607 /* ToDS frame with own addr BSSID and DA */
608 } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
609 IEEE80211_FCTL_FROMDS &&
610 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
611 /* FromDS frame with own addr as DA */
615 if (skb->len < 24 + 8)
618 /* check for port access entity Ethernet type */
619 pos = skb->data + 24;
620 ethertype = (pos[6] << 8) | pos[7];
621 if (ethertype == ETH_P_PAE)
628 /* Called only as a tasklet (software IRQ) */
630 hostap_rx_frame_decrypt(local_info_t *local, struct sk_buff *skb,
631 struct ieee80211_crypt_data *crypt)
633 struct ieee80211_hdr_4addr *hdr;
636 if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
639 hdr = (struct ieee80211_hdr_4addr *) skb->data;
640 hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
642 if (local->tkip_countermeasures &&
643 strcmp(crypt->ops->name, "TKIP") == 0) {
644 if (net_ratelimit()) {
645 printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
646 "received packet from " MACSTR "\n",
647 local->dev->name, MAC2STR(hdr->addr2));
652 atomic_inc(&crypt->refcnt);
653 res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
654 atomic_dec(&crypt->refcnt);
656 printk(KERN_DEBUG "%s: decryption failed (SA=" MACSTR
658 local->dev->name, MAC2STR(hdr->addr2), res);
659 local->comm_tallies.rx_discards_wep_undecryptable++;
667 /* Called only as a tasklet (software IRQ) */
669 hostap_rx_frame_decrypt_msdu(local_info_t *local, struct sk_buff *skb,
670 int keyidx, struct ieee80211_crypt_data *crypt)
672 struct ieee80211_hdr_4addr *hdr;
675 if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
678 hdr = (struct ieee80211_hdr_4addr *) skb->data;
679 hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
681 atomic_inc(&crypt->refcnt);
682 res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
683 atomic_dec(&crypt->refcnt);
685 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
686 " (SA=" MACSTR " keyidx=%d)\n",
687 local->dev->name, MAC2STR(hdr->addr2), keyidx);
695 /* All received frames are sent to this function. @skb contains the frame in
696 * IEEE 802.11 format, i.e., in the format it was sent over air.
697 * This function is called only as a tasklet (software IRQ). */
698 void hostap_80211_rx(struct net_device *dev, struct sk_buff *skb,
699 struct hostap_80211_rx_status *rx_stats)
701 struct hostap_interface *iface;
703 struct ieee80211_hdr_4addr *hdr;
705 u16 fc, type, stype, sc;
706 struct net_device *wds = NULL;
707 struct net_device_stats *stats;
710 struct sk_buff *skb2 = NULL;
712 int frame_authorized = 0;
713 int from_assoc_ap = 0;
716 struct ieee80211_crypt_data *crypt = NULL;
720 iface = netdev_priv(dev);
721 local = iface->local;
722 iface->stats.rx_packets++;
723 iface->stats.rx_bytes += skb->len;
725 /* dev is the master radio device; change this to be the default
726 * virtual interface (this may be changed to WDS device below) */
728 iface = netdev_priv(dev);
730 hdr = (struct ieee80211_hdr_4addr *) skb->data;
731 stats = hostap_get_stats(dev);
736 fc = le16_to_cpu(hdr->frame_ctl);
737 type = WLAN_FC_GET_TYPE(fc);
738 stype = WLAN_FC_GET_STYPE(fc);
739 sc = le16_to_cpu(hdr->seq_ctl);
740 frag = WLAN_GET_SEQ_FRAG(sc);
741 hdrlen = hostap_80211_get_hdrlen(fc);
743 /* Put this code here so that we avoid duplicating it in all
744 * Rx paths. - Jean II */
745 #ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
746 /* If spy monitoring on */
747 if (iface->spy_data.spy_number > 0) {
748 struct iw_quality wstats;
749 wstats.level = rx_stats->signal;
750 wstats.noise = rx_stats->noise;
751 wstats.updated = IW_QUAL_LEVEL_UPDATED | IW_QUAL_NOISE_UPDATED
752 | IW_QUAL_QUAL_INVALID | IW_QUAL_DBM;
753 /* Update spy records */
754 wireless_spy_update(dev, hdr->addr2, &wstats);
756 #endif /* IW_WIRELESS_SPY */
757 hostap_update_rx_stats(local->ap, hdr, rx_stats);
759 if (local->iw_mode == IW_MODE_MONITOR) {
760 monitor_rx(dev, skb, rx_stats);
764 if (local->host_decrypt) {
766 if (skb->len >= hdrlen + 3)
767 idx = skb->data[hdrlen + 3] >> 6;
768 crypt = local->crypt[idx];
771 /* Use station specific key to override default keys if the
772 * receiver address is a unicast address ("individual RA"). If
773 * bcrx_sta_key parameter is set, station specific key is used
774 * even with broad/multicast targets (this is against IEEE
775 * 802.11, but makes it easier to use different keys with
776 * stations that do not support WEP key mapping). */
778 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
779 (void) hostap_handle_sta_crypto(local, hdr, &crypt,
782 /* allow NULL decrypt to indicate an station specific override
783 * for default encryption */
784 if (crypt && (crypt->ops == NULL ||
785 crypt->ops->decrypt_mpdu == NULL))
788 if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
790 /* This seems to be triggered by some (multicast?)
791 * frames from other than current BSS, so just drop the
792 * frames silently instead of filling system log with
794 printk(KERN_DEBUG "%s: WEP decryption failed (not set)"
795 " (SA=" MACSTR ")\n",
796 local->dev->name, MAC2STR(hdr->addr2));
798 local->comm_tallies.rx_discards_wep_undecryptable++;
803 if (type != IEEE80211_FTYPE_DATA) {
804 if (type == IEEE80211_FTYPE_MGMT &&
805 stype == IEEE80211_STYPE_AUTH &&
806 fc & IEEE80211_FCTL_PROTECTED && local->host_decrypt &&
807 (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
809 printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
810 "from " MACSTR "\n", dev->name,
811 MAC2STR(hdr->addr2));
812 /* TODO: could inform hostapd about this so that it
813 * could send auth failure report */
817 if (hostap_rx_frame_mgmt(local, skb, rx_stats, type, stype))
823 /* Data frame - extract src/dst addresses */
824 if (skb->len < IEEE80211_DATA_HDR3_LEN)
827 switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
828 case IEEE80211_FCTL_FROMDS:
829 memcpy(dst, hdr->addr1, ETH_ALEN);
830 memcpy(src, hdr->addr3, ETH_ALEN);
832 case IEEE80211_FCTL_TODS:
833 memcpy(dst, hdr->addr3, ETH_ALEN);
834 memcpy(src, hdr->addr2, ETH_ALEN);
836 case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
837 if (skb->len < IEEE80211_DATA_HDR4_LEN)
839 memcpy(dst, hdr->addr3, ETH_ALEN);
840 memcpy(src, hdr->addr4, ETH_ALEN);
843 memcpy(dst, hdr->addr1, ETH_ALEN);
844 memcpy(src, hdr->addr2, ETH_ALEN);
848 if (hostap_rx_frame_wds(local, hdr, fc, &wds))
851 skb->dev = dev = wds;
852 stats = hostap_get_stats(dev);
855 if (local->iw_mode == IW_MODE_MASTER && !wds &&
856 (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
857 IEEE80211_FCTL_FROMDS &&
859 memcmp(hdr->addr2, local->assoc_ap_addr, ETH_ALEN) == 0) {
860 /* Frame from BSSID of the AP for which we are a client */
861 skb->dev = dev = local->stadev;
862 stats = hostap_get_stats(dev);
866 dev->last_rx = jiffies;
868 if ((local->iw_mode == IW_MODE_MASTER ||
869 local->iw_mode == IW_MODE_REPEAT) &&
871 switch (hostap_handle_sta_rx(local, dev, skb, rx_stats,
873 case AP_RX_CONTINUE_NOT_AUTHORIZED:
874 frame_authorized = 0;
877 frame_authorized = 1;
886 /* Nullfunc frames may have PS-bit set, so they must be passed to
887 * hostap_handle_sta_rx() before being dropped here. */
888 if (stype != IEEE80211_STYPE_DATA &&
889 stype != IEEE80211_STYPE_DATA_CFACK &&
890 stype != IEEE80211_STYPE_DATA_CFPOLL &&
891 stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
892 if (stype != IEEE80211_STYPE_NULLFUNC)
893 printk(KERN_DEBUG "%s: RX: dropped data frame "
894 "with no data (type=0x%02x, subtype=0x%02x)\n",
895 dev->name, type >> 2, stype >> 4);
899 /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
901 if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
902 (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
904 hdr = (struct ieee80211_hdr_4addr *) skb->data;
906 /* skb: hdr + (possibly fragmented) plaintext payload */
908 if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
909 (frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
911 struct sk_buff *frag_skb =
912 prism2_frag_cache_get(local, hdr);
914 printk(KERN_DEBUG "%s: Rx cannot get skb from "
915 "fragment cache (morefrag=%d seq=%u frag=%u)\n",
916 dev->name, (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
917 WLAN_GET_SEQ_SEQ(sc) >> 4, frag);
925 if (frag_skb->tail + flen > frag_skb->end) {
926 printk(KERN_WARNING "%s: host decrypted and "
927 "reassembled frame did not fit skb\n",
929 prism2_frag_cache_invalidate(local, hdr);
934 /* copy first fragment (including full headers) into
935 * beginning of the fragment cache skb */
936 memcpy(skb_put(frag_skb, flen), skb->data, flen);
938 /* append frame payload to the end of the fragment
940 memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
946 if (fc & IEEE80211_FCTL_MOREFRAGS) {
947 /* more fragments expected - leave the skb in fragment
948 * cache for now; it will be delivered to upper layers
949 * after all fragments have been received */
953 /* this was the last fragment and the frame will be
954 * delivered, so remove skb from fragment cache */
956 hdr = (struct ieee80211_hdr_4addr *) skb->data;
957 prism2_frag_cache_invalidate(local, hdr);
960 /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
961 * encrypted/authenticated */
963 if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
964 hostap_rx_frame_decrypt_msdu(local, skb, keyidx, crypt))
967 hdr = (struct ieee80211_hdr_4addr *) skb->data;
968 if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !local->open_wep) {
969 if (local->ieee_802_1x &&
970 hostap_is_eapol_frame(local, skb)) {
971 /* pass unencrypted EAPOL frames even if encryption is
973 PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X - passing "
974 "unencrypted EAPOL frame\n", local->dev->name);
976 printk(KERN_DEBUG "%s: encryption configured, but RX "
977 "frame not encrypted (SA=" MACSTR ")\n",
978 local->dev->name, MAC2STR(hdr->addr2));
983 if (local->drop_unencrypted && !(fc & IEEE80211_FCTL_PROTECTED) &&
984 !hostap_is_eapol_frame(local, skb)) {
985 if (net_ratelimit()) {
986 printk(KERN_DEBUG "%s: dropped unencrypted RX data "
987 "frame from " MACSTR " (drop_unencrypted=1)\n",
988 dev->name, MAC2STR(hdr->addr2));
993 /* skb: hdr + (possible reassembled) full plaintext payload */
995 payload = skb->data + hdrlen;
996 ethertype = (payload[6] << 8) | payload[7];
998 /* If IEEE 802.1X is used, check whether the port is authorized to send
999 * the received frame. */
1000 if (local->ieee_802_1x && local->iw_mode == IW_MODE_MASTER) {
1001 if (ethertype == ETH_P_PAE) {
1002 PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X frame\n",
1004 if (local->hostapd && local->apdev) {
1005 /* Send IEEE 802.1X frames to the user
1006 * space daemon for processing */
1007 prism2_rx_80211(local->apdev, skb, rx_stats,
1009 local->apdevstats.rx_packets++;
1010 local->apdevstats.rx_bytes += skb->len;
1013 } else if (!frame_authorized) {
1014 printk(KERN_DEBUG "%s: dropped frame from "
1015 "unauthorized port (IEEE 802.1X): "
1016 "ethertype=0x%04x\n",
1017 dev->name, ethertype);
1022 /* convert hdr + possible LLC headers into Ethernet header */
1023 if (skb->len - hdrlen >= 8 &&
1024 ((memcmp(payload, rfc1042_header, 6) == 0 &&
1025 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1026 memcmp(payload, bridge_tunnel_header, 6) == 0)) {
1027 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1028 * replace EtherType */
1029 skb_pull(skb, hdrlen + 6);
1030 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1031 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1034 /* Leave Ethernet header part of hdr and full payload */
1035 skb_pull(skb, hdrlen);
1036 len = htons(skb->len);
1037 memcpy(skb_push(skb, 2), &len, 2);
1038 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1039 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1042 if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
1043 IEEE80211_FCTL_TODS) &&
1044 skb->len >= ETH_HLEN + ETH_ALEN) {
1045 /* Non-standard frame: get addr4 from its bogus location after
1047 memcpy(skb->data + ETH_ALEN,
1048 skb->data + skb->len - ETH_ALEN, ETH_ALEN);
1049 skb_trim(skb, skb->len - ETH_ALEN);
1052 stats->rx_packets++;
1053 stats->rx_bytes += skb->len;
1055 if (local->iw_mode == IW_MODE_MASTER && !wds &&
1056 local->ap->bridge_packets) {
1057 if (dst[0] & 0x01) {
1058 /* copy multicast frame both to the higher layers and
1059 * to the wireless media */
1060 local->ap->bridged_multicast++;
1061 skb2 = skb_clone(skb, GFP_ATOMIC);
1063 printk(KERN_DEBUG "%s: skb_clone failed for "
1064 "multicast frame\n", dev->name);
1065 } else if (hostap_is_sta_authorized(local->ap, dst)) {
1066 /* send frame directly to the associated STA using
1067 * wireless media and not passing to higher layers */
1068 local->ap->bridged_unicast++;
1075 /* send to wireless media */
1076 skb2->protocol = __constant_htons(ETH_P_802_3);
1077 skb2->mac.raw = skb2->nh.raw = skb2->data;
1078 /* skb2->nh.raw = skb2->data + ETH_HLEN; */
1080 dev_queue_xmit(skb2);
1084 skb->protocol = eth_type_trans(skb, dev);
1085 memset(skb->cb, 0, sizeof(skb->cb));
1092 hostap_handle_sta_release(sta);
1098 stats->rx_dropped++;
1103 EXPORT_SYMBOL(hostap_80211_rx);