2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/rcupdate.h>
17 #include <net/mac80211.h>
18 #include <net/ieee80211_radiotap.h>
20 #include "ieee80211_i.h"
21 #include "ieee80211_led.h"
28 * monitor mode reception
30 * This function cleans up the SKB, i.e. it removes all the stuff
31 * only useful for monitoring.
33 static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
37 skb_pull(skb, rtap_len);
39 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
40 if (likely(skb->len > FCS_LEN))
41 skb_trim(skb, skb->len - FCS_LEN);
53 static inline int should_drop_frame(struct ieee80211_rx_status *status,
58 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
60 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
62 if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
64 if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
65 cpu_to_le16(IEEE80211_FTYPE_CTL)) &&
66 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
67 cpu_to_le16(IEEE80211_STYPE_PSPOLL)))
73 * This function copies a received frame to all monitor interfaces and
74 * returns a cleaned-up SKB that no longer includes the FCS nor the
75 * radiotap header the driver might have added.
77 static struct sk_buff *
78 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
79 struct ieee80211_rx_status *status)
81 struct ieee80211_sub_if_data *sdata;
82 struct ieee80211_rate *rate;
83 int needed_headroom = 0;
84 struct ieee80211_radiotap_header *rthdr;
85 __le64 *rttsft = NULL;
86 struct ieee80211_rtap_fixed_data {
92 u8 padding_for_rxflags;
94 } __attribute__ ((packed)) *rtfixed;
95 struct sk_buff *skb, *skb2;
96 struct net_device *prev_dev = NULL;
97 int present_fcs_len = 0;
101 * First, we may need to make a copy of the skb because
102 * (1) we need to modify it for radiotap (if not present), and
103 * (2) the other RX handlers will modify the skb we got.
105 * We don't need to, of course, if we aren't going to return
106 * the SKB because it has a bad FCS/PLCP checksum.
108 if (status->flag & RX_FLAG_RADIOTAP)
109 rtap_len = ieee80211_get_radiotap_len(origskb->data);
111 /* room for radiotap header, always present fields and TSFT */
112 needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
114 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
115 present_fcs_len = FCS_LEN;
117 if (!local->monitors) {
118 if (should_drop_frame(status, origskb, present_fcs_len,
120 dev_kfree_skb(origskb);
124 return remove_monitor_info(local, origskb, rtap_len);
127 if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
128 /* only need to expand headroom if necessary */
133 * This shouldn't trigger often because most devices have an
134 * RX header they pull before we get here, and that should
135 * be big enough for our radiotap information. We should
136 * probably export the length to drivers so that we can have
137 * them allocate enough headroom to start with.
139 if (skb_headroom(skb) < needed_headroom &&
140 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
146 * Need to make a copy and possibly remove radiotap header
147 * and FCS from the original.
149 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
151 origskb = remove_monitor_info(local, origskb, rtap_len);
157 /* if necessary, prepend radiotap information */
158 if (!(status->flag & RX_FLAG_RADIOTAP)) {
159 rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
160 rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
161 if (status->flag & RX_FLAG_TSFT) {
162 rttsft = (void *) skb_push(skb, sizeof(*rttsft));
165 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
166 memset(rthdr, 0, sizeof(*rthdr));
167 memset(rtfixed, 0, sizeof(*rtfixed));
169 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
170 (1 << IEEE80211_RADIOTAP_RATE) |
171 (1 << IEEE80211_RADIOTAP_CHANNEL) |
172 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
173 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
175 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
176 rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
179 *rttsft = cpu_to_le64(status->mactime);
181 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
184 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
185 rtfixed->rx_flags = 0;
187 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
189 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
191 rate = ieee80211_get_rate(local, status->phymode,
194 rtfixed->rate = rate->rate / 5;
196 rtfixed->chan_freq = cpu_to_le16(status->freq);
198 if (status->phymode == MODE_IEEE80211A)
199 rtfixed->chan_flags =
200 cpu_to_le16(IEEE80211_CHAN_OFDM |
201 IEEE80211_CHAN_5GHZ);
203 rtfixed->chan_flags =
204 cpu_to_le16(IEEE80211_CHAN_DYN |
205 IEEE80211_CHAN_2GHZ);
207 rtfixed->antsignal = status->ssi;
208 rthdr->it_len = cpu_to_le16(rtap_len);
211 skb_reset_mac_header(skb);
212 skb->ip_summed = CHECKSUM_UNNECESSARY;
213 skb->pkt_type = PACKET_OTHERHOST;
214 skb->protocol = htons(ETH_P_802_2);
216 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
217 if (!netif_running(sdata->dev))
220 if (sdata->type != IEEE80211_IF_TYPE_MNTR)
224 skb2 = skb_clone(skb, GFP_ATOMIC);
226 skb2->dev = prev_dev;
231 prev_dev = sdata->dev;
232 sdata->dev->stats.rx_packets++;
233 sdata->dev->stats.rx_bytes += skb->len;
248 * these don't have dev/sdata fields in the rx data
249 * The sta value should also not be used because it may
250 * be NULL even though a STA (in IBSS mode) will be added.
253 static ieee80211_txrx_result
254 ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
256 u8 *data = rx->skb->data;
259 /* does the frame have a qos control field? */
260 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
261 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
262 /* frame has qos control */
263 tid = qc[0] & QOS_CONTROL_TID_MASK;
264 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
265 rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
267 rx->flags &= ~IEEE80211_TXRXD_RX_AMSDU;
269 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
270 /* Separate TID for management frames */
271 tid = NUM_RX_DATA_QUEUES - 1;
273 /* no qos control present */
274 tid = 0; /* 802.1d - Best Effort */
278 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
279 /* only a debug counter, sta might not be assigned properly yet */
281 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
283 rx->u.rx.queue = tid;
284 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
285 * For now, set skb->priority to 0 for other cases. */
286 rx->skb->priority = (tid > 7) ? 0 : tid;
288 return TXRX_CONTINUE;
292 u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
294 struct ieee80211_rx_status *status)
296 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
297 u32 load = 0, hdrtime;
298 struct ieee80211_rate *rate;
299 struct ieee80211_hw_mode *mode = local->hw.conf.mode;
302 /* Estimate total channel use caused by this frame */
304 if (unlikely(mode->num_rates < 0))
305 return TXRX_CONTINUE;
307 rate = &mode->rates[0];
308 for (i = 0; i < mode->num_rates; i++) {
309 if (mode->rates[i].val == status->rate) {
310 rate = &mode->rates[i];
315 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
316 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
318 if (mode->mode == MODE_IEEE80211A ||
319 (mode->mode == MODE_IEEE80211G &&
320 rate->flags & IEEE80211_RATE_ERP))
321 hdrtime = CHAN_UTIL_HDR_SHORT;
323 hdrtime = CHAN_UTIL_HDR_LONG;
326 if (!is_multicast_ether_addr(hdr->addr1))
329 load += skb->len * rate->rate_inv;
331 /* Divide channel_use by 8 to avoid wrapping around the counter */
332 load >>= CHAN_UTIL_SHIFT;
337 ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
339 ieee80211_rx_h_parse_qos,
345 static ieee80211_txrx_result
346 ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
349 rx->sta->channel_use_raw += rx->u.rx.load;
350 rx->sdata->channel_use_raw += rx->u.rx.load;
351 return TXRX_CONTINUE;
354 static ieee80211_txrx_result
355 ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
357 struct ieee80211_local *local = rx->local;
358 struct sk_buff *skb = rx->skb;
360 if (unlikely(local->sta_hw_scanning))
361 return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
363 if (unlikely(local->sta_sw_scanning)) {
364 /* drop all the other packets during a software scan anyway */
365 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
371 if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
372 /* scanning finished during invoking of handlers */
373 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
377 return TXRX_CONTINUE;
380 static ieee80211_txrx_result
381 ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
383 struct ieee80211_hdr *hdr;
384 hdr = (struct ieee80211_hdr *) rx->skb->data;
386 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
387 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
388 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
389 rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
391 if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
392 rx->local->dot11FrameDuplicateCount++;
393 rx->sta->num_duplicates++;
397 rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
400 if (unlikely(rx->skb->len < 16)) {
401 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
405 /* Drop disallowed frame classes based on STA auth/assoc state;
406 * IEEE 802.11, Chap 5.5.
408 * 80211.o does filtering only based on association state, i.e., it
409 * drops Class 3 frames from not associated stations. hostapd sends
410 * deauth/disassoc frames when needed. In addition, hostapd is
411 * responsible for filtering on both auth and assoc states.
413 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
414 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
415 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
416 rx->sdata->type != IEEE80211_IF_TYPE_IBSS &&
417 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
418 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
419 !(rx->fc & IEEE80211_FCTL_TODS) &&
420 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
421 || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
422 /* Drop IBSS frames and frames for other hosts
430 return TXRX_CONTINUE;
434 static ieee80211_txrx_result
435 ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
437 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
440 ieee80211_txrx_result result = TXRX_DROP;
441 struct ieee80211_key *stakey = NULL;
446 * There are three types of keys:
448 * - PTK (pairwise keys)
449 * - STK (station-to-station pairwise keys)
451 * When selecting a key, we have to distinguish between multicast
452 * (including broadcast) and unicast frames, the latter can only
453 * use PTKs and STKs while the former always use GTKs. Unless, of
454 * course, actual WEP keys ("pre-RSNA") are used, then unicast
455 * frames can also use key indizes like GTKs. Hence, if we don't
456 * have a PTK/STK we check the key index for a WEP key.
458 * Note that in a regular BSS, multicast frames are sent by the
459 * AP only, associated stations unicast the frame to the AP first
460 * which then multicasts it on their behalf.
462 * There is also a slight problem in IBSS mode: GTKs are negotiated
463 * with each station, that is something we don't currently handle.
464 * The spec seems to expect that one negotiates the same key with
465 * every station but there's no such requirement; VLANs could be
469 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
470 return TXRX_CONTINUE;
473 * No point in finding a key and decrypting if the frame is neither
474 * addressed to us nor a multicast frame.
476 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
477 return TXRX_CONTINUE;
480 stakey = rcu_dereference(rx->sta->key);
482 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
486 * The device doesn't give us the IV so we won't be
487 * able to look up the key. That's ok though, we
488 * don't need to decrypt the frame, we just won't
489 * be able to keep statistics accurate.
490 * Except for key threshold notifications, should
491 * we somehow allow the driver to tell us which key
492 * the hardware used if this flag is set?
494 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
495 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
496 return TXRX_CONTINUE;
498 hdrlen = ieee80211_get_hdrlen(rx->fc);
500 if (rx->skb->len < 8 + hdrlen)
501 return TXRX_DROP; /* TODO: count this? */
504 * no need to call ieee80211_wep_get_keyidx,
505 * it verifies a bunch of things we've done already
507 keyidx = rx->skb->data[hdrlen + 3] >> 6;
509 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
512 * RSNA-protected unicast frames should always be sent with
513 * pairwise or station-to-station keys, but for WEP we allow
514 * using a key index as well.
516 if (rx->key && rx->key->conf.alg != ALG_WEP &&
517 !is_multicast_ether_addr(hdr->addr1))
522 rx->key->tx_rx_count++;
523 /* TODO: add threshold stuff again */
525 #ifdef CONFIG_MAC80211_DEBUG
527 printk(KERN_DEBUG "%s: RX protected frame,"
528 " but have no key\n", rx->dev->name);
529 #endif /* CONFIG_MAC80211_DEBUG */
533 /* Check for weak IVs if possible */
534 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
535 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
536 (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
537 !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
538 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
539 rx->sta->wep_weak_iv_count++;
541 switch (rx->key->conf.alg) {
543 result = ieee80211_crypto_wep_decrypt(rx);
546 result = ieee80211_crypto_tkip_decrypt(rx);
549 result = ieee80211_crypto_ccmp_decrypt(rx);
553 /* either the frame has been decrypted or will be dropped */
554 rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
559 static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
561 struct ieee80211_sub_if_data *sdata;
562 DECLARE_MAC_BUF(mac);
564 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
567 atomic_inc(&sdata->bss->num_sta_ps);
568 sta->flags |= WLAN_STA_PS;
570 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
571 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
572 dev->name, print_mac(mac, sta->addr), sta->aid);
573 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
576 static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
578 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
581 struct ieee80211_sub_if_data *sdata;
582 struct ieee80211_tx_packet_data *pkt_data;
583 DECLARE_MAC_BUF(mac);
585 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
587 atomic_dec(&sdata->bss->num_sta_ps);
588 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
590 if (!skb_queue_empty(&sta->ps_tx_buf)) {
591 if (local->ops->set_tim)
592 local->ops->set_tim(local_to_hw(local), sta->aid, 0);
594 bss_tim_clear(local, sdata->bss, sta->aid);
596 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
597 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
598 dev->name, print_mac(mac, sta->addr), sta->aid);
599 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
600 /* Send all buffered frames to the station */
601 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
602 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
604 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
607 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
608 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
609 local->total_ps_buffered--;
611 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
612 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
613 "since STA not sleeping anymore\n", dev->name,
614 print_mac(mac, sta->addr), sta->aid);
615 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
616 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
623 static ieee80211_txrx_result
624 ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
626 struct sta_info *sta = rx->sta;
627 struct net_device *dev = rx->dev;
628 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
631 return TXRX_CONTINUE;
633 /* Update last_rx only for IBSS packets which are for the current
634 * BSSID to avoid keeping the current IBSS network alive in cases where
635 * other STAs are using different BSSID. */
636 if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
637 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
638 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
639 sta->last_rx = jiffies;
641 if (!is_multicast_ether_addr(hdr->addr1) ||
642 rx->sdata->type == IEEE80211_IF_TYPE_STA) {
643 /* Update last_rx only for unicast frames in order to prevent
644 * the Probe Request frames (the only broadcast frames from a
645 * STA in infrastructure mode) from keeping a connection alive.
647 sta->last_rx = jiffies;
650 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
651 return TXRX_CONTINUE;
654 sta->rx_bytes += rx->skb->len;
655 sta->last_rssi = rx->u.rx.status->ssi;
656 sta->last_signal = rx->u.rx.status->signal;
657 sta->last_noise = rx->u.rx.status->noise;
659 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
660 /* Change STA power saving mode only in the end of a frame
661 * exchange sequence */
662 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
663 rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
664 else if (!(sta->flags & WLAN_STA_PS) &&
665 (rx->fc & IEEE80211_FCTL_PM))
666 ap_sta_ps_start(dev, sta);
669 /* Drop data::nullfunc frames silently, since they are used only to
670 * control station power saving mode. */
671 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
672 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
673 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
674 /* Update counter and free packet here to avoid counting this
675 * as a dropped packed. */
677 dev_kfree_skb(rx->skb);
681 return TXRX_CONTINUE;
682 } /* ieee80211_rx_h_sta_process */
684 static inline struct ieee80211_fragment_entry *
685 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
686 unsigned int frag, unsigned int seq, int rx_queue,
687 struct sk_buff **skb)
689 struct ieee80211_fragment_entry *entry;
692 idx = sdata->fragment_next;
693 entry = &sdata->fragments[sdata->fragment_next++];
694 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
695 sdata->fragment_next = 0;
697 if (!skb_queue_empty(&entry->skb_list)) {
698 #ifdef CONFIG_MAC80211_DEBUG
699 struct ieee80211_hdr *hdr =
700 (struct ieee80211_hdr *) entry->skb_list.next->data;
701 DECLARE_MAC_BUF(mac);
702 DECLARE_MAC_BUF(mac2);
703 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
704 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
705 "addr1=%s addr2=%s\n",
706 sdata->dev->name, idx,
707 jiffies - entry->first_frag_time, entry->seq,
708 entry->last_frag, print_mac(mac, hdr->addr1),
709 print_mac(mac2, hdr->addr2));
710 #endif /* CONFIG_MAC80211_DEBUG */
711 __skb_queue_purge(&entry->skb_list);
714 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
716 entry->first_frag_time = jiffies;
718 entry->rx_queue = rx_queue;
719 entry->last_frag = frag;
721 entry->extra_len = 0;
726 static inline struct ieee80211_fragment_entry *
727 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
728 u16 fc, unsigned int frag, unsigned int seq,
729 int rx_queue, struct ieee80211_hdr *hdr)
731 struct ieee80211_fragment_entry *entry;
734 idx = sdata->fragment_next;
735 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
736 struct ieee80211_hdr *f_hdr;
741 idx = IEEE80211_FRAGMENT_MAX - 1;
743 entry = &sdata->fragments[idx];
744 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
745 entry->rx_queue != rx_queue ||
746 entry->last_frag + 1 != frag)
749 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
750 f_fc = le16_to_cpu(f_hdr->frame_control);
752 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
753 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
754 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
757 if (entry->first_frag_time + 2 * HZ < jiffies) {
758 __skb_queue_purge(&entry->skb_list);
767 static ieee80211_txrx_result
768 ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
770 struct ieee80211_hdr *hdr;
772 unsigned int frag, seq;
773 struct ieee80211_fragment_entry *entry;
775 DECLARE_MAC_BUF(mac);
777 hdr = (struct ieee80211_hdr *) rx->skb->data;
778 sc = le16_to_cpu(hdr->seq_ctrl);
779 frag = sc & IEEE80211_SCTL_FRAG;
781 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
782 (rx->skb)->len < 24 ||
783 is_multicast_ether_addr(hdr->addr1))) {
787 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
789 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
792 /* This is the first fragment of a new frame. */
793 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
794 rx->u.rx.queue, &(rx->skb));
795 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
796 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
797 /* Store CCMP PN so that we can verify that the next
798 * fragment has a sequential PN value. */
800 memcpy(entry->last_pn,
801 rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
807 /* This is a fragment for a frame that should already be pending in
808 * fragment cache. Add this fragment to the end of the pending entry.
810 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
811 rx->u.rx.queue, hdr);
813 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
817 /* Verify that MPDUs within one MSDU have sequential PN values.
818 * (IEEE 802.11i, 8.3.3.4.5) */
821 u8 pn[CCMP_PN_LEN], *rpn;
822 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
824 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
825 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
830 rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
831 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
833 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
835 " PN=%02x%02x%02x%02x%02x%02x "
836 "(expected %02x%02x%02x%02x%02x%02x)\n",
837 rx->dev->name, print_mac(mac, hdr->addr2),
838 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
839 rpn[5], pn[0], pn[1], pn[2], pn[3],
843 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
846 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
847 __skb_queue_tail(&entry->skb_list, rx->skb);
848 entry->last_frag = frag;
849 entry->extra_len += rx->skb->len;
850 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
855 rx->skb = __skb_dequeue(&entry->skb_list);
856 if (skb_tailroom(rx->skb) < entry->extra_len) {
857 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
858 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
860 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
861 __skb_queue_purge(&entry->skb_list);
865 while ((skb = __skb_dequeue(&entry->skb_list))) {
866 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
870 /* Complete frame has been reassembled - process it now */
871 rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
875 rx->sta->rx_packets++;
876 if (is_multicast_ether_addr(hdr->addr1))
877 rx->local->dot11MulticastReceivedFrameCount++;
879 ieee80211_led_rx(rx->local);
880 return TXRX_CONTINUE;
883 static ieee80211_txrx_result
884 ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
886 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
889 DECLARE_MAC_BUF(mac);
891 if (likely(!rx->sta ||
892 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
893 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
894 !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
895 return TXRX_CONTINUE;
897 if ((sdata->type != IEEE80211_IF_TYPE_AP) &&
898 (sdata->type != IEEE80211_IF_TYPE_VLAN))
901 skb = skb_dequeue(&rx->sta->tx_filtered);
903 skb = skb_dequeue(&rx->sta->ps_tx_buf);
905 rx->local->total_ps_buffered--;
907 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
908 skb_queue_empty(&rx->sta->ps_tx_buf);
911 struct ieee80211_hdr *hdr =
912 (struct ieee80211_hdr *) skb->data;
914 /* tell TX path to send one frame even though the STA may
915 * still remain is PS mode after this frame exchange */
918 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
919 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
920 print_mac(mac, rx->sta->addr), rx->sta->aid,
921 skb_queue_len(&rx->sta->ps_tx_buf));
922 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
924 /* Use MoreData flag to indicate whether there are more
925 * buffered frames for this STA */
926 if (no_pending_pkts) {
927 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
928 rx->sta->flags &= ~WLAN_STA_TIM;
930 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
934 if (no_pending_pkts) {
935 if (rx->local->ops->set_tim)
936 rx->local->ops->set_tim(local_to_hw(rx->local),
939 bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
941 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
942 } else if (!rx->u.rx.sent_ps_buffered) {
943 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
944 "though there is no buffered frames for it\n",
945 rx->dev->name, print_mac(mac, rx->sta->addr));
946 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
950 /* Free PS Poll skb here instead of returning TXRX_DROP that would
951 * count as an dropped frame. */
952 dev_kfree_skb(rx->skb);
957 static ieee80211_txrx_result
958 ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
961 u8 *data = rx->skb->data;
962 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
964 if (!WLAN_FC_IS_QOS_DATA(fc))
965 return TXRX_CONTINUE;
967 /* remove the qos control field, update frame type and meta-data */
968 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
969 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
970 /* change frame type to non QOS */
971 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
972 hdr->frame_control = cpu_to_le16(fc);
974 return TXRX_CONTINUE;
978 ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
980 if (unlikely(rx->sdata->ieee802_1x_pac &&
981 (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
982 #ifdef CONFIG_MAC80211_DEBUG
983 printk(KERN_DEBUG "%s: dropped frame "
984 "(unauthorized port)\n", rx->dev->name);
985 #endif /* CONFIG_MAC80211_DEBUG */
993 ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
996 * Pass through unencrypted frames if the hardware has
997 * decrypted them already.
999 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
1002 /* Drop unencrypted frames if key is set. */
1003 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1004 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1005 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
1006 (rx->key || rx->sdata->drop_unencrypted))) {
1007 if (net_ratelimit())
1008 printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
1009 "encryption\n", rx->dev->name);
1016 ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
1018 struct net_device *dev = rx->dev;
1019 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1020 u16 fc, hdrlen, ethertype;
1024 struct sk_buff *skb = rx->skb;
1025 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1026 DECLARE_MAC_BUF(mac);
1027 DECLARE_MAC_BUF(mac2);
1028 DECLARE_MAC_BUF(mac3);
1029 DECLARE_MAC_BUF(mac4);
1033 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1036 hdrlen = ieee80211_get_hdrlen(fc);
1038 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1040 * IEEE 802.11 address fields:
1041 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1042 * 0 0 DA SA BSSID n/a
1043 * 0 1 DA BSSID SA n/a
1044 * 1 0 BSSID SA DA n/a
1048 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1049 case IEEE80211_FCTL_TODS:
1051 memcpy(dst, hdr->addr3, ETH_ALEN);
1052 memcpy(src, hdr->addr2, ETH_ALEN);
1054 if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP &&
1055 sdata->type != IEEE80211_IF_TYPE_VLAN)) {
1056 if (net_ratelimit())
1057 printk(KERN_DEBUG "%s: dropped ToDS frame "
1058 "(BSSID=%s SA=%s DA=%s)\n",
1060 print_mac(mac, hdr->addr1),
1061 print_mac(mac2, hdr->addr2),
1062 print_mac(mac3, hdr->addr3));
1066 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1068 memcpy(dst, hdr->addr3, ETH_ALEN);
1069 memcpy(src, hdr->addr4, ETH_ALEN);
1071 if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) {
1072 if (net_ratelimit())
1073 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
1074 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
1076 print_mac(mac, hdr->addr1),
1077 print_mac(mac2, hdr->addr2),
1078 print_mac(mac3, hdr->addr3),
1079 print_mac(mac4, hdr->addr4));
1083 case IEEE80211_FCTL_FROMDS:
1085 memcpy(dst, hdr->addr1, ETH_ALEN);
1086 memcpy(src, hdr->addr3, ETH_ALEN);
1088 if (sdata->type != IEEE80211_IF_TYPE_STA ||
1089 (is_multicast_ether_addr(dst) &&
1090 !compare_ether_addr(src, dev->dev_addr)))
1095 memcpy(dst, hdr->addr1, ETH_ALEN);
1096 memcpy(src, hdr->addr2, ETH_ALEN);
1098 if (sdata->type != IEEE80211_IF_TYPE_IBSS) {
1099 if (net_ratelimit()) {
1100 printk(KERN_DEBUG "%s: dropped IBSS frame "
1101 "(DA=%s SA=%s BSSID=%s)\n",
1103 print_mac(mac, hdr->addr1),
1104 print_mac(mac2, hdr->addr2),
1105 print_mac(mac3, hdr->addr3));
1112 if (unlikely(skb->len - hdrlen < 8)) {
1113 if (net_ratelimit()) {
1114 printk(KERN_DEBUG "%s: RX too short data frame "
1115 "payload\n", dev->name);
1120 payload = skb->data + hdrlen;
1121 ethertype = (payload[6] << 8) | payload[7];
1123 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1124 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1125 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1126 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1127 * replace EtherType */
1128 skb_pull(skb, hdrlen + 6);
1129 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1130 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1132 struct ethhdr *ehdr;
1135 skb_pull(skb, hdrlen);
1136 len = htons(skb->len);
1137 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1138 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1139 memcpy(ehdr->h_source, src, ETH_ALEN);
1140 ehdr->h_proto = len;
1146 * requires that rx->skb is a frame with ethernet header
1148 static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
1150 static const u8 pae_group_addr[ETH_ALEN]
1151 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1152 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1155 * Allow EAPOL frames to us/the PAE group address regardless
1156 * of whether the frame was encrypted or not.
1158 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1159 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1160 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1163 if (ieee80211_802_1x_port_control(rx) ||
1164 ieee80211_drop_unencrypted(rx))
1171 * requires that rx->skb is a frame with ethernet header
1174 ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
1176 struct net_device *dev = rx->dev;
1177 struct ieee80211_local *local = rx->local;
1178 struct sk_buff *skb, *xmit_skb;
1179 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1180 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1181 struct sta_info *dsta;
1186 if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP ||
1187 sdata->type == IEEE80211_IF_TYPE_VLAN) &&
1188 (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
1189 if (is_multicast_ether_addr(ehdr->h_dest)) {
1191 * send multicast frames both to higher layers in
1192 * local net stack and back to the wireless medium
1194 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1195 if (!xmit_skb && net_ratelimit())
1196 printk(KERN_DEBUG "%s: failed to clone "
1197 "multicast frame\n", dev->name);
1199 dsta = sta_info_get(local, skb->data);
1200 if (dsta && dsta->dev == dev) {
1202 * The destination station is associated to
1203 * this AP (in this VLAN), so send the frame
1204 * directly to it and do not pass it to local
1216 /* deliver to local stack */
1217 skb->protocol = eth_type_trans(skb, dev);
1218 memset(skb->cb, 0, sizeof(skb->cb));
1223 /* send to wireless media */
1224 xmit_skb->protocol = htons(ETH_P_802_3);
1225 skb_reset_network_header(xmit_skb);
1226 skb_reset_mac_header(xmit_skb);
1227 dev_queue_xmit(xmit_skb);
1231 static ieee80211_txrx_result
1232 ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
1234 struct net_device *dev = rx->dev;
1235 struct ieee80211_local *local = rx->local;
1238 struct sk_buff *skb = rx->skb, *frame = NULL;
1239 const struct ethhdr *eth;
1243 DECLARE_MAC_BUF(mac);
1246 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1247 return TXRX_CONTINUE;
1249 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1252 if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
1253 return TXRX_CONTINUE;
1255 err = ieee80211_data_to_8023(rx);
1261 dev->stats.rx_packets++;
1262 dev->stats.rx_bytes += skb->len;
1264 /* skip the wrapping header */
1265 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1269 while (skb != frame) {
1271 __be16 len = eth->h_proto;
1272 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1274 remaining = skb->len;
1275 memcpy(dst, eth->h_dest, ETH_ALEN);
1276 memcpy(src, eth->h_source, ETH_ALEN);
1278 padding = ((4 - subframe_len) & 0x3);
1279 /* the last MSDU has no padding */
1280 if (subframe_len > remaining) {
1281 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
1285 skb_pull(skb, sizeof(struct ethhdr));
1286 /* if last subframe reuse skb */
1287 if (remaining <= subframe_len + padding)
1290 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1296 skb_reserve(frame, local->hw.extra_tx_headroom +
1297 sizeof(struct ethhdr));
1298 memcpy(skb_put(frame, ntohs(len)), skb->data,
1301 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1304 printk(KERN_DEBUG "%s: wrong buffer size ",
1306 dev_kfree_skb(frame);
1311 skb_reset_network_header(frame);
1313 frame->priority = skb->priority;
1316 payload = frame->data;
1317 ethertype = (payload[6] << 8) | payload[7];
1319 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1320 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1321 compare_ether_addr(payload,
1322 bridge_tunnel_header) == 0)) {
1323 /* remove RFC1042 or Bridge-Tunnel
1324 * encapsulation and replace EtherType */
1326 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1327 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1329 memcpy(skb_push(frame, sizeof(__be16)),
1330 &len, sizeof(__be16));
1331 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1332 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1335 if (!ieee80211_frame_allowed(rx)) {
1336 if (skb == frame) /* last frame */
1338 dev_kfree_skb(frame);
1342 ieee80211_deliver_skb(rx);
1348 static ieee80211_txrx_result
1349 ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
1351 struct net_device *dev = rx->dev;
1356 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
1357 return TXRX_CONTINUE;
1359 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
1362 err = ieee80211_data_to_8023(rx);
1366 if (!ieee80211_frame_allowed(rx))
1371 dev->stats.rx_packets++;
1372 dev->stats.rx_bytes += rx->skb->len;
1374 ieee80211_deliver_skb(rx);
1379 static ieee80211_txrx_result
1380 ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
1382 struct ieee80211_sub_if_data *sdata;
1384 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
1387 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
1388 if ((sdata->type == IEEE80211_IF_TYPE_STA ||
1389 sdata->type == IEEE80211_IF_TYPE_IBSS) &&
1390 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
1391 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
1398 static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
1399 struct ieee80211_local *local,
1400 ieee80211_rx_handler *handlers,
1401 struct ieee80211_txrx_data *rx,
1402 struct sta_info *sta)
1404 ieee80211_rx_handler *handler;
1405 ieee80211_txrx_result res = TXRX_DROP;
1407 for (handler = handlers; *handler != NULL; handler++) {
1408 res = (*handler)(rx);
1414 I802_DEBUG_INC(local->rx_handlers_drop);
1419 I802_DEBUG_INC(local->rx_handlers_queued);
1425 if (res == TXRX_DROP)
1426 dev_kfree_skb(rx->skb);
1430 static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
1431 ieee80211_rx_handler *handlers,
1432 struct ieee80211_txrx_data *rx,
1433 struct sta_info *sta)
1435 if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
1437 dev_kfree_skb(rx->skb);
1440 static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1441 struct ieee80211_hdr *hdr,
1442 struct sta_info *sta,
1443 struct ieee80211_txrx_data *rx)
1446 DECLARE_MAC_BUF(mac);
1447 DECLARE_MAC_BUF(mac2);
1449 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1450 if (rx->skb->len >= hdrlen + 4)
1451 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1455 if (net_ratelimit())
1456 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
1457 "failure from %s to %s keyidx=%d\n",
1458 dev->name, print_mac(mac, hdr->addr2),
1459 print_mac(mac2, hdr->addr1), keyidx);
1463 * Some hardware seem to generate incorrect Michael MIC
1464 * reports; ignore them to avoid triggering countermeasures.
1466 if (net_ratelimit())
1467 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1468 "error for unknown address %s\n",
1469 dev->name, print_mac(mac, hdr->addr2));
1473 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1474 if (net_ratelimit())
1475 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1476 "error for a frame with no PROTECTED flag (src "
1477 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
1481 if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) {
1483 * APs with pairwise keys should never receive Michael MIC
1484 * errors for non-zero keyidx because these are reserved for
1485 * group keys and only the AP is sending real multicast
1486 * frames in the BSS.
1488 if (net_ratelimit())
1489 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1490 "a frame with non-zero keyidx (%d)"
1491 " (src %s)\n", dev->name, keyidx,
1492 print_mac(mac, hdr->addr2));
1496 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1497 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1498 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
1499 if (net_ratelimit())
1500 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1501 "error for a frame that cannot be encrypted "
1502 "(fc=0x%04x) (src %s)\n",
1503 dev->name, rx->fc, print_mac(mac, hdr->addr2));
1507 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
1509 dev_kfree_skb(rx->skb);
1513 ieee80211_rx_handler ieee80211_rx_handlers[] =
1515 ieee80211_rx_h_if_stats,
1516 ieee80211_rx_h_passive_scan,
1517 ieee80211_rx_h_check,
1518 ieee80211_rx_h_decrypt,
1519 ieee80211_rx_h_sta_process,
1520 ieee80211_rx_h_defragment,
1521 ieee80211_rx_h_ps_poll,
1522 ieee80211_rx_h_michael_mic_verify,
1523 /* this must be after decryption - so header is counted in MPDU mic
1524 * must be before pae and data, so QOS_DATA format frames
1525 * are not passed to user space by these functions
1527 ieee80211_rx_h_remove_qos_control,
1528 ieee80211_rx_h_amsdu,
1529 ieee80211_rx_h_data,
1530 ieee80211_rx_h_mgmt,
1534 /* main receive path */
1536 static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
1537 u8 *bssid, struct ieee80211_txrx_data *rx,
1538 struct ieee80211_hdr *hdr)
1540 int multicast = is_multicast_ether_addr(hdr->addr1);
1542 switch (sdata->type) {
1543 case IEEE80211_IF_TYPE_STA:
1546 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1547 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1549 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1550 } else if (!multicast &&
1551 compare_ether_addr(sdata->dev->dev_addr,
1553 if (!(sdata->dev->flags & IFF_PROMISC))
1555 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1558 case IEEE80211_IF_TYPE_IBSS:
1561 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
1562 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1564 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1565 } else if (!multicast &&
1566 compare_ether_addr(sdata->dev->dev_addr,
1568 if (!(sdata->dev->flags & IFF_PROMISC))
1570 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1571 } else if (!rx->sta)
1572 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1575 case IEEE80211_IF_TYPE_VLAN:
1576 case IEEE80211_IF_TYPE_AP:
1578 if (compare_ether_addr(sdata->dev->dev_addr,
1581 } else if (!ieee80211_bssid_match(bssid,
1582 sdata->dev->dev_addr)) {
1583 if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1585 rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
1587 if (sdata->dev == sdata->local->mdev &&
1588 !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
1589 /* do not receive anything via
1590 * master device when not scanning */
1593 case IEEE80211_IF_TYPE_WDS:
1595 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1597 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1600 case IEEE80211_IF_TYPE_MNTR:
1601 /* take everything */
1603 case IEEE80211_IF_TYPE_INVALID:
1604 /* should never get here */
1613 * This is the actual Rx frames handler. as it blongs to Rx path it must
1614 * be called with rcu_read_lock protection.
1616 void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw, struct sk_buff *skb,
1617 struct ieee80211_rx_status *status, u32 load)
1619 struct ieee80211_local *local = hw_to_local(hw);
1620 struct ieee80211_sub_if_data *sdata;
1621 struct sta_info *sta;
1622 struct ieee80211_hdr *hdr;
1623 struct ieee80211_txrx_data rx;
1626 struct ieee80211_sub_if_data *prev = NULL;
1627 struct sk_buff *skb_new;
1631 hdr = (struct ieee80211_hdr *) skb->data;
1632 memset(&rx, 0, sizeof(rx));
1636 rx.u.rx.status = status;
1637 rx.u.rx.load = load;
1638 rx.fc = le16_to_cpu(hdr->frame_control);
1639 type = rx.fc & IEEE80211_FCTL_FTYPE;
1642 * Drivers are required to align the payload data to a four-byte
1643 * boundary, so the last two bits of the address where it starts
1644 * may not be set. The header is required to be directly before
1645 * the payload data, padding like atheros hardware adds which is
1646 * inbetween the 802.11 header and the payload is not supported,
1647 * the driver is required to move the 802.11 header further back
1650 hdrlen = ieee80211_get_hdrlen(rx.fc);
1651 WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3);
1653 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
1654 local->dot11ReceivedFragmentCount++;
1656 sta = rx.sta = sta_info_get(local, hdr->addr2);
1658 rx.dev = rx.sta->dev;
1659 rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
1662 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
1663 ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
1667 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
1668 rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
1670 if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
1671 sta) != TXRX_CONTINUE)
1675 if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
1676 !atomic_read(&local->iff_promiscs) &&
1677 !is_multicast_ether_addr(hdr->addr1)) {
1678 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
1679 ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
1686 bssid = ieee80211_get_bssid(hdr, skb->len);
1688 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1689 if (!netif_running(sdata->dev))
1692 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
1695 rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
1696 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
1697 /* prepare_for_handlers can change sta */
1704 * frame is destined for this interface, but if it's not
1705 * also for the previous one we handle that after the
1706 * loop to avoid copying the SKB once too much
1715 * frame was destined for the previous interface
1716 * so invoke RX handlers for it
1719 skb_new = skb_copy(skb, GFP_ATOMIC);
1721 if (net_ratelimit())
1722 printk(KERN_DEBUG "%s: failed to copy "
1723 "multicast frame for %s",
1724 wiphy_name(local->hw.wiphy),
1728 rx.fc = le16_to_cpu(hdr->frame_control);
1732 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1737 rx.fc = le16_to_cpu(hdr->frame_control);
1741 ieee80211_invoke_rx_handlers(local, local->rx_handlers,
1751 #define SEQ_MODULO 0x1000
1752 #define SEQ_MASK 0xfff
1754 static inline int seq_less(u16 sq1, u16 sq2)
1756 return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
1759 static inline u16 seq_inc(u16 sq)
1761 return ((sq + 1) & SEQ_MASK);
1764 static inline u16 seq_sub(u16 sq1, u16 sq2)
1766 return ((sq1 - sq2) & SEQ_MASK);
1770 u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
1771 struct tid_ampdu_rx *tid_agg_rx,
1772 struct sk_buff *skb, u16 mpdu_seq_num,
1775 struct ieee80211_local *local = hw_to_local(hw);
1776 struct ieee80211_rx_status status;
1777 u16 head_seq_num, buf_size;
1781 buf_size = tid_agg_rx->buf_size;
1782 head_seq_num = tid_agg_rx->head_seq_num;
1784 /* frame with out of date sequence number */
1785 if (seq_less(mpdu_seq_num, head_seq_num)) {
1790 /* if frame sequence number exceeds our buffering window size or
1791 * block Ack Request arrived - release stored frames */
1792 if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
1793 /* new head to the ordering buffer */
1795 head_seq_num = mpdu_seq_num;
1798 seq_inc(seq_sub(mpdu_seq_num, buf_size));
1799 /* release stored frames up to new head to stack */
1800 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1801 index = seq_sub(tid_agg_rx->head_seq_num,
1803 % tid_agg_rx->buf_size;
1805 if (tid_agg_rx->reorder_buf[index]) {
1806 /* release the reordered frames to stack */
1808 tid_agg_rx->reorder_buf[index]->cb,
1810 pkt_load = ieee80211_rx_load_stats(local,
1811 tid_agg_rx->reorder_buf[index],
1813 __ieee80211_rx_handle_packet(hw,
1814 tid_agg_rx->reorder_buf[index],
1816 tid_agg_rx->stored_mpdu_num--;
1817 tid_agg_rx->reorder_buf[index] = NULL;
1819 tid_agg_rx->head_seq_num =
1820 seq_inc(tid_agg_rx->head_seq_num);
1826 /* now the new frame is always in the range of the reordering */
1828 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
1829 % tid_agg_rx->buf_size;
1830 /* check if we already stored this frame */
1831 if (tid_agg_rx->reorder_buf[index]) {
1836 /* if arrived mpdu is in the right order and nothing else stored */
1837 /* release it immediately */
1838 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1839 tid_agg_rx->stored_mpdu_num == 0) {
1840 tid_agg_rx->head_seq_num =
1841 seq_inc(tid_agg_rx->head_seq_num);
1845 /* put the frame in the reordering buffer */
1846 tid_agg_rx->reorder_buf[index] = skb;
1847 tid_agg_rx->stored_mpdu_num++;
1848 /* release the buffer until next missing frame */
1849 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
1850 % tid_agg_rx->buf_size;
1851 while (tid_agg_rx->reorder_buf[index]) {
1852 /* release the reordered frame back to stack */
1853 memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
1855 pkt_load = ieee80211_rx_load_stats(local,
1856 tid_agg_rx->reorder_buf[index],
1858 __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
1860 tid_agg_rx->stored_mpdu_num--;
1861 tid_agg_rx->reorder_buf[index] = NULL;
1862 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
1863 index = seq_sub(tid_agg_rx->head_seq_num,
1864 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
1869 u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
1870 struct sk_buff *skb)
1872 struct ieee80211_hw *hw = &local->hw;
1873 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1874 struct sta_info *sta;
1875 struct tid_ampdu_rx *tid_agg_rx;
1881 sta = sta_info_get(local, hdr->addr2);
1885 fc = le16_to_cpu(hdr->frame_control);
1887 /* filter the QoS data rx stream according to
1888 * STA/TID and check if this STA/TID is on aggregation */
1889 if (!WLAN_FC_IS_QOS_DATA(fc))
1892 qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
1893 tid = qc[0] & QOS_CONTROL_TID_MASK;
1894 tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
1896 if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
1899 /* null data frames are excluded */
1900 if (unlikely(fc & IEEE80211_STYPE_QOS_NULLFUNC))
1903 /* new un-ordered ampdu frame - process it */
1905 /* reset session timer */
1906 if (tid_agg_rx->timeout) {
1907 unsigned long expires =
1908 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1909 mod_timer(&tid_agg_rx->session_timer, expires);
1912 /* if this mpdu is fragmented - terminate rx aggregation session */
1913 sc = le16_to_cpu(hdr->seq_ctrl);
1914 if (sc & IEEE80211_SCTL_FRAG) {
1915 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
1916 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
1921 /* according to mpdu sequence number deal with reordering buffer */
1922 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1923 ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
1932 * This is the receive path handler. It is called by a low level driver when an
1933 * 802.11 MPDU is received from the hardware.
1935 void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
1936 struct ieee80211_rx_status *status)
1938 struct ieee80211_local *local = hw_to_local(hw);
1942 * key references and virtual interfaces are protected using RCU
1943 * and this requires that we are in a read-side RCU section during
1944 * receive processing
1949 * Frames with failed FCS/PLCP checksum are not returned,
1950 * all other frames are returned without radiotap header
1951 * if it was previously present.
1952 * Also, frames with less than 16 bytes are dropped.
1954 skb = ieee80211_rx_monitor(local, skb, status);
1960 pkt_load = ieee80211_rx_load_stats(local, skb, status);
1961 local->channel_use_raw += pkt_load;
1963 if (!ieee80211_rx_reorder_ampdu(local, skb))
1964 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load);
1968 EXPORT_SYMBOL(__ieee80211_rx);
1970 /* This is a version of the rx handler that can be called from hard irq
1971 * context. Post the skb on the queue and schedule the tasklet */
1972 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
1973 struct ieee80211_rx_status *status)
1975 struct ieee80211_local *local = hw_to_local(hw);
1977 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
1979 skb->dev = local->mdev;
1980 /* copy status into skb->cb for use by tasklet */
1981 memcpy(skb->cb, status, sizeof(*status));
1982 skb->pkt_type = IEEE80211_RX_MSG;
1983 skb_queue_tail(&local->skb_queue, skb);
1984 tasklet_schedule(&local->tasklet);
1986 EXPORT_SYMBOL(ieee80211_rx_irqsafe);