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-2010 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/jiffies.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/rcupdate.h>
19 #include <net/mac80211.h>
20 #include <net/ieee80211_radiotap.h>
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
32 * monitor mode reception
34 * This function cleans up the SKB, i.e. it removes all the stuff
35 * only useful for monitoring.
37 static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
40 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
41 if (likely(skb->len > FCS_LEN))
42 __pskb_trim(skb, skb->len - FCS_LEN);
54 static inline int should_drop_frame(struct sk_buff *skb,
57 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
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))
64 if (ieee80211_is_ctl(hdr->frame_control) &&
65 !ieee80211_is_pspoll(hdr->frame_control) &&
66 !ieee80211_is_back_req(hdr->frame_control))
72 ieee80211_rx_radiotap_len(struct ieee80211_local *local,
73 struct ieee80211_rx_status *status)
77 /* always present fields */
78 len = sizeof(struct ieee80211_radiotap_header) + 9;
80 if (status->flag & RX_FLAG_TSFT)
82 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
84 if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
87 if (len & 1) /* padding for RX_FLAGS if necessary */
94 * ieee80211_add_rx_radiotap_header - add radiotap header
96 * add a radiotap header containing all the fields which the hardware provided.
99 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
101 struct ieee80211_rate *rate,
104 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
105 struct ieee80211_radiotap_header *rthdr;
109 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
110 memset(rthdr, 0, rtap_len);
112 /* radiotap header, set always present flags */
114 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
115 (1 << IEEE80211_RADIOTAP_CHANNEL) |
116 (1 << IEEE80211_RADIOTAP_ANTENNA) |
117 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
118 rthdr->it_len = cpu_to_le16(rtap_len);
120 pos = (unsigned char *)(rthdr+1);
122 /* the order of the following fields is important */
124 /* IEEE80211_RADIOTAP_TSFT */
125 if (status->flag & RX_FLAG_TSFT) {
126 put_unaligned_le64(status->mactime, pos);
128 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
132 /* IEEE80211_RADIOTAP_FLAGS */
133 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
134 *pos |= IEEE80211_RADIOTAP_F_FCS;
135 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
136 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
137 if (status->flag & RX_FLAG_SHORTPRE)
138 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
141 /* IEEE80211_RADIOTAP_RATE */
142 if (status->flag & RX_FLAG_HT) {
144 * TODO: add following information into radiotap header once
145 * suitable fields are defined for it:
146 * - MCS index (status->rate_idx)
147 * - HT40 (status->flag & RX_FLAG_40MHZ)
148 * - short-GI (status->flag & RX_FLAG_SHORT_GI)
152 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
153 *pos = rate->bitrate / 5;
157 /* IEEE80211_RADIOTAP_CHANNEL */
158 put_unaligned_le16(status->freq, pos);
160 if (status->band == IEEE80211_BAND_5GHZ)
161 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
163 else if (status->flag & RX_FLAG_HT)
164 put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
166 else if (rate->flags & IEEE80211_RATE_ERP_G)
167 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
170 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
174 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
175 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
176 *pos = status->signal;
178 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
182 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
184 /* IEEE80211_RADIOTAP_ANTENNA */
185 *pos = status->antenna;
188 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
190 /* IEEE80211_RADIOTAP_RX_FLAGS */
191 /* ensure 2 byte alignment for the 2 byte field as required */
192 if ((pos - (u8 *)rthdr) & 1)
194 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
195 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
196 put_unaligned_le16(rx_flags, pos);
201 * This function copies a received frame to all monitor interfaces and
202 * returns a cleaned-up SKB that no longer includes the FCS nor the
203 * radiotap header the driver might have added.
205 static struct sk_buff *
206 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
207 struct ieee80211_rate *rate)
209 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
210 struct ieee80211_sub_if_data *sdata;
211 int needed_headroom = 0;
212 struct sk_buff *skb, *skb2;
213 struct net_device *prev_dev = NULL;
214 int present_fcs_len = 0;
217 * First, we may need to make a copy of the skb because
218 * (1) we need to modify it for radiotap (if not present), and
219 * (2) the other RX handlers will modify the skb we got.
221 * We don't need to, of course, if we aren't going to return
222 * the SKB because it has a bad FCS/PLCP checksum.
225 /* room for the radiotap header based on driver features */
226 needed_headroom = ieee80211_rx_radiotap_len(local, status);
228 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
229 present_fcs_len = FCS_LEN;
231 /* make sure hdr->frame_control is on the linear part */
232 if (!pskb_may_pull(origskb, 2)) {
233 dev_kfree_skb(origskb);
237 if (!local->monitors) {
238 if (should_drop_frame(origskb, present_fcs_len)) {
239 dev_kfree_skb(origskb);
243 return remove_monitor_info(local, origskb);
246 if (should_drop_frame(origskb, present_fcs_len)) {
247 /* only need to expand headroom if necessary */
252 * This shouldn't trigger often because most devices have an
253 * RX header they pull before we get here, and that should
254 * be big enough for our radiotap information. We should
255 * probably export the length to drivers so that we can have
256 * them allocate enough headroom to start with.
258 if (skb_headroom(skb) < needed_headroom &&
259 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
265 * Need to make a copy and possibly remove radiotap header
266 * and FCS from the original.
268 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
270 origskb = remove_monitor_info(local, origskb);
276 /* prepend radiotap information */
277 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
279 skb_reset_mac_header(skb);
280 skb->ip_summed = CHECKSUM_UNNECESSARY;
281 skb->pkt_type = PACKET_OTHERHOST;
282 skb->protocol = htons(ETH_P_802_2);
284 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
285 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
288 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
291 if (!ieee80211_sdata_running(sdata))
295 skb2 = skb_clone(skb, GFP_ATOMIC);
297 skb2->dev = prev_dev;
302 prev_dev = sdata->dev;
303 sdata->dev->stats.rx_packets++;
304 sdata->dev->stats.rx_bytes += skb->len;
317 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
319 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
322 /* does the frame have a qos control field? */
323 if (ieee80211_is_data_qos(hdr->frame_control)) {
324 u8 *qc = ieee80211_get_qos_ctl(hdr);
325 /* frame has qos control */
326 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
327 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
328 rx->flags |= IEEE80211_RX_AMSDU;
330 rx->flags &= ~IEEE80211_RX_AMSDU;
333 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
335 * Sequence numbers for management frames, QoS data
336 * frames with a broadcast/multicast address in the
337 * Address 1 field, and all non-QoS data frames sent
338 * by QoS STAs are assigned using an additional single
339 * modulo-4096 counter, [...]
341 * We also use that counter for non-QoS STAs.
343 tid = NUM_RX_DATA_QUEUES - 1;
347 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
348 * For now, set skb->priority to 0 for other cases. */
349 rx->skb->priority = (tid > 7) ? 0 : tid;
353 * DOC: Packet alignment
355 * Drivers always need to pass packets that are aligned to two-byte boundaries
358 * Additionally, should, if possible, align the payload data in a way that
359 * guarantees that the contained IP header is aligned to a four-byte
360 * boundary. In the case of regular frames, this simply means aligning the
361 * payload to a four-byte boundary (because either the IP header is directly
362 * contained, or IV/RFC1042 headers that have a length divisible by four are
363 * in front of it). If the payload data is not properly aligned and the
364 * architecture doesn't support efficient unaligned operations, mac80211
365 * will align the data.
367 * With A-MSDU frames, however, the payload data address must yield two modulo
368 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
369 * push the IP header further back to a multiple of four again. Thankfully, the
370 * specs were sane enough this time around to require padding each A-MSDU
371 * subframe to a length that is a multiple of four.
373 * Padding like Atheros hardware adds which is inbetween the 802.11 header and
374 * the payload is not supported, the driver is required to move the 802.11
375 * header to be directly in front of the payload in that case.
377 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
379 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
380 WARN_ONCE((unsigned long)rx->skb->data & 1,
381 "unaligned packet at 0x%p\n", rx->skb->data);
388 static ieee80211_rx_result debug_noinline
389 ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
391 struct ieee80211_local *local = rx->local;
392 struct sk_buff *skb = rx->skb;
394 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning)))
395 return ieee80211_scan_rx(rx->sdata, skb);
397 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning) &&
398 (rx->flags & IEEE80211_RX_IN_SCAN))) {
399 /* drop all the other packets during a software scan anyway */
400 if (ieee80211_scan_rx(rx->sdata, skb) != RX_QUEUED)
405 if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
406 /* scanning finished during invoking of handlers */
407 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
408 return RX_DROP_UNUSABLE;
415 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
417 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
419 if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
422 return ieee80211_is_robust_mgmt_frame(hdr);
426 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
428 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
430 if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
433 return ieee80211_is_robust_mgmt_frame(hdr);
437 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
438 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
440 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
441 struct ieee80211_mmie *mmie;
443 if (skb->len < 24 + sizeof(*mmie) ||
444 !is_multicast_ether_addr(hdr->da))
447 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
448 return -1; /* not a robust management frame */
450 mmie = (struct ieee80211_mmie *)
451 (skb->data + skb->len - sizeof(*mmie));
452 if (mmie->element_id != WLAN_EID_MMIE ||
453 mmie->length != sizeof(*mmie) - 2)
456 return le16_to_cpu(mmie->key_id);
460 static ieee80211_rx_result
461 ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
463 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
464 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
465 char *dev_addr = rx->sdata->vif.addr;
467 if (ieee80211_is_data(hdr->frame_control)) {
468 if (is_multicast_ether_addr(hdr->addr1)) {
469 if (ieee80211_has_tods(hdr->frame_control) ||
470 !ieee80211_has_fromds(hdr->frame_control))
471 return RX_DROP_MONITOR;
472 if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
473 return RX_DROP_MONITOR;
475 if (!ieee80211_has_a4(hdr->frame_control))
476 return RX_DROP_MONITOR;
477 if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
478 return RX_DROP_MONITOR;
482 /* If there is not an established peer link and this is not a peer link
483 * establisment frame, beacon or probe, drop the frame.
486 if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
487 struct ieee80211_mgmt *mgmt;
489 if (!ieee80211_is_mgmt(hdr->frame_control))
490 return RX_DROP_MONITOR;
492 if (ieee80211_is_action(hdr->frame_control)) {
493 mgmt = (struct ieee80211_mgmt *)hdr;
494 if (mgmt->u.action.category != WLAN_CATEGORY_MESH_PLINK)
495 return RX_DROP_MONITOR;
499 if (ieee80211_is_probe_req(hdr->frame_control) ||
500 ieee80211_is_probe_resp(hdr->frame_control) ||
501 ieee80211_is_beacon(hdr->frame_control))
504 return RX_DROP_MONITOR;
508 #define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
510 if (ieee80211_is_data(hdr->frame_control) &&
511 is_multicast_ether_addr(hdr->addr1) &&
512 mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
513 return RX_DROP_MONITOR;
519 #define SEQ_MODULO 0x1000
520 #define SEQ_MASK 0xfff
522 static inline int seq_less(u16 sq1, u16 sq2)
524 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
527 static inline u16 seq_inc(u16 sq)
529 return (sq + 1) & SEQ_MASK;
532 static inline u16 seq_sub(u16 sq1, u16 sq2)
534 return (sq1 - sq2) & SEQ_MASK;
538 static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
539 struct tid_ampdu_rx *tid_agg_rx,
541 struct sk_buff_head *frames)
543 struct ieee80211_supported_band *sband;
544 struct ieee80211_rate *rate = NULL;
545 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
546 struct ieee80211_rx_status *status;
551 status = IEEE80211_SKB_RXCB(skb);
553 /* release the reordered frames to stack */
554 sband = hw->wiphy->bands[status->band];
555 if (!(status->flag & RX_FLAG_HT))
556 rate = &sband->bitrates[status->rate_idx];
557 tid_agg_rx->stored_mpdu_num--;
558 tid_agg_rx->reorder_buf[index] = NULL;
559 __skb_queue_tail(frames, skb);
562 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
565 static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
566 struct tid_ampdu_rx *tid_agg_rx,
568 struct sk_buff_head *frames)
572 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
573 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
574 tid_agg_rx->buf_size;
575 ieee80211_release_reorder_frame(hw, tid_agg_rx, index, frames);
580 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
581 * the skb was added to the buffer longer than this time ago, the earlier
582 * frames that have not yet been received are assumed to be lost and the skb
583 * can be released for processing. This may also release other skb's from the
584 * reorder buffer if there are no additional gaps between the frames.
586 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
589 * As this function belongs to the RX path it must be under
590 * rcu_read_lock protection. It returns false if the frame
591 * can be processed immediately, true if it was consumed.
593 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
594 struct tid_ampdu_rx *tid_agg_rx,
596 struct sk_buff_head *frames)
598 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
599 u16 sc = le16_to_cpu(hdr->seq_ctrl);
600 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
601 u16 head_seq_num, buf_size;
604 buf_size = tid_agg_rx->buf_size;
605 head_seq_num = tid_agg_rx->head_seq_num;
607 /* frame with out of date sequence number */
608 if (seq_less(mpdu_seq_num, head_seq_num)) {
614 * If frame the sequence number exceeds our buffering window
615 * size release some previous frames to make room for this one.
617 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
618 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
619 /* release stored frames up to new head to stack */
620 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num,
624 /* Now the new frame is always in the range of the reordering buffer */
626 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
628 /* check if we already stored this frame */
629 if (tid_agg_rx->reorder_buf[index]) {
635 * If the current MPDU is in the right order and nothing else
636 * is stored we can process it directly, no need to buffer it.
638 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
639 tid_agg_rx->stored_mpdu_num == 0) {
640 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
644 /* put the frame in the reordering buffer */
645 tid_agg_rx->reorder_buf[index] = skb;
646 tid_agg_rx->reorder_time[index] = jiffies;
647 tid_agg_rx->stored_mpdu_num++;
648 /* release the buffer until next missing frame */
649 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
650 tid_agg_rx->buf_size;
651 if (!tid_agg_rx->reorder_buf[index] &&
652 tid_agg_rx->stored_mpdu_num > 1) {
654 * No buffers ready to be released, but check whether any
655 * frames in the reorder buffer have timed out.
659 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
660 j = (j + 1) % tid_agg_rx->buf_size) {
661 if (!tid_agg_rx->reorder_buf[j]) {
665 if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
666 HT_RX_REORDER_BUF_TIMEOUT))
669 #ifdef CONFIG_MAC80211_HT_DEBUG
671 printk(KERN_DEBUG "%s: release an RX reorder "
672 "frame due to timeout on earlier "
674 wiphy_name(hw->wiphy));
676 ieee80211_release_reorder_frame(hw, tid_agg_rx,
680 * Increment the head seq# also for the skipped slots.
682 tid_agg_rx->head_seq_num =
683 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
686 } else while (tid_agg_rx->reorder_buf[index]) {
687 ieee80211_release_reorder_frame(hw, tid_agg_rx, index, frames);
688 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
689 tid_agg_rx->buf_size;
696 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
697 * true if the MPDU was buffered, false if it should be processed.
699 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
700 struct sk_buff_head *frames)
702 struct sk_buff *skb = rx->skb;
703 struct ieee80211_local *local = rx->local;
704 struct ieee80211_hw *hw = &local->hw;
705 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
706 struct sta_info *sta = rx->sta;
707 struct tid_ampdu_rx *tid_agg_rx;
711 if (!ieee80211_is_data_qos(hdr->frame_control))
715 * filter the QoS data rx stream according to
716 * STA/TID and check if this STA/TID is on aggregation
722 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
724 spin_lock(&sta->lock);
726 if (!sta->ampdu_mlme.tid_active_rx[tid])
727 goto dont_reorder_unlock;
729 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
731 /* qos null data frames are excluded */
732 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
733 goto dont_reorder_unlock;
735 /* new, potentially un-ordered, ampdu frame - process it */
737 /* reset session timer */
738 if (tid_agg_rx->timeout)
739 mod_timer(&tid_agg_rx->session_timer,
740 TU_TO_EXP_TIME(tid_agg_rx->timeout));
742 /* if this mpdu is fragmented - terminate rx aggregation session */
743 sc = le16_to_cpu(hdr->seq_ctrl);
744 if (sc & IEEE80211_SCTL_FRAG) {
745 spin_unlock(&sta->lock);
746 __ieee80211_stop_rx_ba_session(sta, tid, WLAN_BACK_RECIPIENT,
747 WLAN_REASON_QSTA_REQUIRE_SETUP);
752 if (ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb, frames)) {
753 spin_unlock(&sta->lock);
758 spin_unlock(&sta->lock);
760 __skb_queue_tail(frames, skb);
763 static ieee80211_rx_result debug_noinline
764 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
766 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
768 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
769 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
770 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
771 rx->sta->last_seq_ctrl[rx->queue] ==
773 if (rx->flags & IEEE80211_RX_RA_MATCH) {
774 rx->local->dot11FrameDuplicateCount++;
775 rx->sta->num_duplicates++;
777 return RX_DROP_MONITOR;
779 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
782 if (unlikely(rx->skb->len < 16)) {
783 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
784 return RX_DROP_MONITOR;
787 /* Drop disallowed frame classes based on STA auth/assoc state;
788 * IEEE 802.11, Chap 5.5.
790 * mac80211 filters only based on association state, i.e. it drops
791 * Class 3 frames from not associated stations. hostapd sends
792 * deauth/disassoc frames when needed. In addition, hostapd is
793 * responsible for filtering on both auth and assoc states.
796 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
797 return ieee80211_rx_mesh_check(rx);
799 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
800 ieee80211_is_pspoll(hdr->frame_control)) &&
801 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
802 (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
803 if ((!ieee80211_has_fromds(hdr->frame_control) &&
804 !ieee80211_has_tods(hdr->frame_control) &&
805 ieee80211_is_data(hdr->frame_control)) ||
806 !(rx->flags & IEEE80211_RX_RA_MATCH)) {
807 /* Drop IBSS frames and frames for other hosts
809 return RX_DROP_MONITOR;
812 return RX_DROP_MONITOR;
819 static ieee80211_rx_result debug_noinline
820 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
822 struct sk_buff *skb = rx->skb;
823 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
824 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
827 ieee80211_rx_result result = RX_DROP_UNUSABLE;
828 struct ieee80211_key *stakey = NULL;
829 int mmie_keyidx = -1;
834 * There are four types of keys:
836 * - IGTK (group keys for management frames)
837 * - PTK (pairwise keys)
838 * - STK (station-to-station pairwise keys)
840 * When selecting a key, we have to distinguish between multicast
841 * (including broadcast) and unicast frames, the latter can only
842 * use PTKs and STKs while the former always use GTKs and IGTKs.
843 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
844 * unicast frames can also use key indices like GTKs. Hence, if we
845 * don't have a PTK/STK we check the key index for a WEP key.
847 * Note that in a regular BSS, multicast frames are sent by the
848 * AP only, associated stations unicast the frame to the AP first
849 * which then multicasts it on their behalf.
851 * There is also a slight problem in IBSS mode: GTKs are negotiated
852 * with each station, that is something we don't currently handle.
853 * The spec seems to expect that one negotiates the same key with
854 * every station but there's no such requirement; VLANs could be
859 * No point in finding a key and decrypting if the frame is neither
860 * addressed to us nor a multicast frame.
862 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
865 /* start without a key */
869 stakey = rcu_dereference(rx->sta->key);
871 if (!ieee80211_has_protected(hdr->frame_control))
872 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
874 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
876 /* Skip decryption if the frame is not protected. */
877 if (!ieee80211_has_protected(hdr->frame_control))
879 } else if (mmie_keyidx >= 0) {
880 /* Broadcast/multicast robust management frame / BIP */
881 if ((status->flag & RX_FLAG_DECRYPTED) &&
882 (status->flag & RX_FLAG_IV_STRIPPED))
885 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
886 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
887 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
888 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
889 } else if (!ieee80211_has_protected(hdr->frame_control)) {
891 * The frame was not protected, so skip decryption. However, we
892 * need to set rx->key if there is a key that could have been
893 * used so that the frame may be dropped if encryption would
894 * have been expected.
896 struct ieee80211_key *key = NULL;
897 if (ieee80211_is_mgmt(hdr->frame_control) &&
898 is_multicast_ether_addr(hdr->addr1) &&
899 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
901 else if ((key = rcu_dereference(rx->sdata->default_key)))
907 * The device doesn't give us the IV so we won't be
908 * able to look up the key. That's ok though, we
909 * don't need to decrypt the frame, we just won't
910 * be able to keep statistics accurate.
911 * Except for key threshold notifications, should
912 * we somehow allow the driver to tell us which key
913 * the hardware used if this flag is set?
915 if ((status->flag & RX_FLAG_DECRYPTED) &&
916 (status->flag & RX_FLAG_IV_STRIPPED))
919 hdrlen = ieee80211_hdrlen(hdr->frame_control);
921 if (rx->skb->len < 8 + hdrlen)
922 return RX_DROP_UNUSABLE; /* TODO: count this? */
925 * no need to call ieee80211_wep_get_keyidx,
926 * it verifies a bunch of things we've done already
928 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
931 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
934 * RSNA-protected unicast frames should always be sent with
935 * pairwise or station-to-station keys, but for WEP we allow
936 * using a key index as well.
938 if (rx->key && rx->key->conf.alg != ALG_WEP &&
939 !is_multicast_ether_addr(hdr->addr1))
944 rx->key->tx_rx_count++;
945 /* TODO: add threshold stuff again */
947 return RX_DROP_MONITOR;
950 if (skb_linearize(rx->skb))
951 return RX_DROP_UNUSABLE;
953 hdr = (struct ieee80211_hdr *)rx->skb->data;
955 /* Check for weak IVs if possible */
956 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
957 ieee80211_is_data(hdr->frame_control) &&
958 (!(status->flag & RX_FLAG_IV_STRIPPED) ||
959 !(status->flag & RX_FLAG_DECRYPTED)) &&
960 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
961 rx->sta->wep_weak_iv_count++;
963 switch (rx->key->conf.alg) {
965 result = ieee80211_crypto_wep_decrypt(rx);
968 result = ieee80211_crypto_tkip_decrypt(rx);
971 result = ieee80211_crypto_ccmp_decrypt(rx);
974 result = ieee80211_crypto_aes_cmac_decrypt(rx);
978 /* either the frame has been decrypted or will be dropped */
979 status->flag |= RX_FLAG_DECRYPTED;
984 static ieee80211_rx_result debug_noinline
985 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
987 struct ieee80211_local *local;
988 struct ieee80211_hdr *hdr;
993 hdr = (struct ieee80211_hdr *) skb->data;
995 if (!local->pspolling)
998 if (!ieee80211_has_fromds(hdr->frame_control))
999 /* this is not from AP */
1002 if (!ieee80211_is_data(hdr->frame_control))
1005 if (!ieee80211_has_moredata(hdr->frame_control)) {
1006 /* AP has no more frames buffered for us */
1007 local->pspolling = false;
1011 /* more data bit is set, let's request a new frame from the AP */
1012 ieee80211_send_pspoll(local, rx->sdata);
1017 static void ap_sta_ps_start(struct sta_info *sta)
1019 struct ieee80211_sub_if_data *sdata = sta->sdata;
1020 struct ieee80211_local *local = sdata->local;
1022 atomic_inc(&sdata->bss->num_sta_ps);
1023 set_sta_flags(sta, WLAN_STA_PS_STA);
1024 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1025 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1026 printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
1027 sdata->name, sta->sta.addr, sta->sta.aid);
1028 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1031 static void ap_sta_ps_end(struct sta_info *sta)
1033 struct ieee80211_sub_if_data *sdata = sta->sdata;
1035 atomic_dec(&sdata->bss->num_sta_ps);
1037 clear_sta_flags(sta, WLAN_STA_PS_STA);
1039 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1040 printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
1041 sdata->name, sta->sta.addr, sta->sta.aid);
1042 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1044 if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
1045 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1046 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
1047 sdata->name, sta->sta.addr, sta->sta.aid);
1048 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1052 ieee80211_sta_ps_deliver_wakeup(sta);
1055 static ieee80211_rx_result debug_noinline
1056 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1058 struct sta_info *sta = rx->sta;
1059 struct sk_buff *skb = rx->skb;
1060 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1061 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1067 * Update last_rx only for IBSS packets which are for the current
1068 * BSSID to avoid keeping the current IBSS network alive in cases
1069 * where other STAs start using different BSSID.
1071 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1072 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1073 NL80211_IFTYPE_ADHOC);
1074 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
1075 sta->last_rx = jiffies;
1076 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1078 * Mesh beacons will update last_rx when if they are found to
1079 * match the current local configuration when processed.
1081 sta->last_rx = jiffies;
1084 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1087 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1088 ieee80211_sta_rx_notify(rx->sdata, hdr);
1090 sta->rx_fragments++;
1091 sta->rx_bytes += rx->skb->len;
1092 sta->last_signal = status->signal;
1095 * Change STA power saving mode only at the end of a frame
1096 * exchange sequence.
1098 if (!ieee80211_has_morefrags(hdr->frame_control) &&
1099 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1100 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1101 if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
1103 * Ignore doze->wake transitions that are
1104 * indicated by non-data frames, the standard
1105 * is unclear here, but for example going to
1106 * PS mode and then scanning would cause a
1107 * doze->wake transition for the probe request,
1108 * and that is clearly undesirable.
1110 if (ieee80211_is_data(hdr->frame_control) &&
1111 !ieee80211_has_pm(hdr->frame_control))
1114 if (ieee80211_has_pm(hdr->frame_control))
1115 ap_sta_ps_start(sta);
1120 * Drop (qos-)data::nullfunc frames silently, since they
1121 * are used only to control station power saving mode.
1123 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1124 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1125 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1128 * If we receive a 4-addr nullfunc frame from a STA
1129 * that was not moved to a 4-addr STA vlan yet, drop
1130 * the frame to the monitor interface, to make sure
1131 * that hostapd sees it
1133 if (ieee80211_has_a4(hdr->frame_control) &&
1134 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1135 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1136 !rx->sdata->u.vlan.sta)))
1137 return RX_DROP_MONITOR;
1139 * Update counter and free packet here to avoid
1140 * counting this as a dropped packed.
1143 dev_kfree_skb(rx->skb);
1148 } /* ieee80211_rx_h_sta_process */
1150 static inline struct ieee80211_fragment_entry *
1151 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1152 unsigned int frag, unsigned int seq, int rx_queue,
1153 struct sk_buff **skb)
1155 struct ieee80211_fragment_entry *entry;
1158 idx = sdata->fragment_next;
1159 entry = &sdata->fragments[sdata->fragment_next++];
1160 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1161 sdata->fragment_next = 0;
1163 if (!skb_queue_empty(&entry->skb_list)) {
1164 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1165 struct ieee80211_hdr *hdr =
1166 (struct ieee80211_hdr *) entry->skb_list.next->data;
1167 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
1168 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
1169 "addr1=%pM addr2=%pM\n",
1171 jiffies - entry->first_frag_time, entry->seq,
1172 entry->last_frag, hdr->addr1, hdr->addr2);
1174 __skb_queue_purge(&entry->skb_list);
1177 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1179 entry->first_frag_time = jiffies;
1181 entry->rx_queue = rx_queue;
1182 entry->last_frag = frag;
1184 entry->extra_len = 0;
1189 static inline struct ieee80211_fragment_entry *
1190 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
1191 unsigned int frag, unsigned int seq,
1192 int rx_queue, struct ieee80211_hdr *hdr)
1194 struct ieee80211_fragment_entry *entry;
1197 idx = sdata->fragment_next;
1198 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1199 struct ieee80211_hdr *f_hdr;
1203 idx = IEEE80211_FRAGMENT_MAX - 1;
1205 entry = &sdata->fragments[idx];
1206 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1207 entry->rx_queue != rx_queue ||
1208 entry->last_frag + 1 != frag)
1211 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1214 * Check ftype and addresses are equal, else check next fragment
1216 if (((hdr->frame_control ^ f_hdr->frame_control) &
1217 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1218 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
1219 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
1222 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1223 __skb_queue_purge(&entry->skb_list);
1232 static ieee80211_rx_result debug_noinline
1233 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1235 struct ieee80211_hdr *hdr;
1238 unsigned int frag, seq;
1239 struct ieee80211_fragment_entry *entry;
1240 struct sk_buff *skb;
1242 hdr = (struct ieee80211_hdr *)rx->skb->data;
1243 fc = hdr->frame_control;
1244 sc = le16_to_cpu(hdr->seq_ctrl);
1245 frag = sc & IEEE80211_SCTL_FRAG;
1247 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
1248 (rx->skb)->len < 24 ||
1249 is_multicast_ether_addr(hdr->addr1))) {
1250 /* not fragmented */
1253 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1255 if (skb_linearize(rx->skb))
1256 return RX_DROP_UNUSABLE;
1258 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1261 /* This is the first fragment of a new frame. */
1262 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
1263 rx->queue, &(rx->skb));
1264 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
1265 ieee80211_has_protected(fc)) {
1266 /* Store CCMP PN so that we can verify that the next
1267 * fragment has a sequential PN value. */
1269 memcpy(entry->last_pn,
1270 rx->key->u.ccmp.rx_pn[rx->queue],
1276 /* This is a fragment for a frame that should already be pending in
1277 * fragment cache. Add this fragment to the end of the pending entry.
1279 entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
1281 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1282 return RX_DROP_MONITOR;
1285 /* Verify that MPDUs within one MSDU have sequential PN values.
1286 * (IEEE 802.11i, 8.3.3.4.5) */
1289 u8 pn[CCMP_PN_LEN], *rpn;
1290 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
1291 return RX_DROP_UNUSABLE;
1292 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1293 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1298 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
1299 if (memcmp(pn, rpn, CCMP_PN_LEN))
1300 return RX_DROP_UNUSABLE;
1301 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1304 skb_pull(rx->skb, ieee80211_hdrlen(fc));
1305 __skb_queue_tail(&entry->skb_list, rx->skb);
1306 entry->last_frag = frag;
1307 entry->extra_len += rx->skb->len;
1308 if (ieee80211_has_morefrags(fc)) {
1313 rx->skb = __skb_dequeue(&entry->skb_list);
1314 if (skb_tailroom(rx->skb) < entry->extra_len) {
1315 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1316 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1318 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1319 __skb_queue_purge(&entry->skb_list);
1320 return RX_DROP_UNUSABLE;
1323 while ((skb = __skb_dequeue(&entry->skb_list))) {
1324 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1328 /* Complete frame has been reassembled - process it now */
1329 rx->flags |= IEEE80211_RX_FRAGMENTED;
1333 rx->sta->rx_packets++;
1334 if (is_multicast_ether_addr(hdr->addr1))
1335 rx->local->dot11MulticastReceivedFrameCount++;
1337 ieee80211_led_rx(rx->local);
1341 static ieee80211_rx_result debug_noinline
1342 ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
1344 struct ieee80211_sub_if_data *sdata = rx->sdata;
1345 __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
1347 if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
1348 !(rx->flags & IEEE80211_RX_RA_MATCH)))
1351 if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1352 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1353 return RX_DROP_UNUSABLE;
1355 if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
1356 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1358 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
1360 /* Free PS Poll skb here instead of returning RX_DROP that would
1361 * count as an dropped frame. */
1362 dev_kfree_skb(rx->skb);
1367 static ieee80211_rx_result debug_noinline
1368 ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
1370 u8 *data = rx->skb->data;
1371 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
1373 if (!ieee80211_is_data_qos(hdr->frame_control))
1376 /* remove the qos control field, update frame type and meta-data */
1377 memmove(data + IEEE80211_QOS_CTL_LEN, data,
1378 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1379 hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
1380 /* change frame type to non QOS */
1381 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1387 ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
1389 if (unlikely(!rx->sta ||
1390 !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
1397 ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
1399 struct sk_buff *skb = rx->skb;
1400 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1403 * Pass through unencrypted frames if the hardware has
1404 * decrypted them already.
1406 if (status->flag & RX_FLAG_DECRYPTED)
1409 /* Drop unencrypted frames if key is set. */
1410 if (unlikely(!ieee80211_has_protected(fc) &&
1411 !ieee80211_is_nullfunc(fc) &&
1412 ieee80211_is_data(fc) &&
1413 (rx->key || rx->sdata->drop_unencrypted)))
1420 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
1422 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1423 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1424 __le16 fc = hdr->frame_control;
1427 * Pass through unencrypted frames if the hardware has
1428 * decrypted them already.
1430 if (status->flag & RX_FLAG_DECRYPTED)
1433 if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1434 if (unlikely(!ieee80211_has_protected(fc) &&
1435 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1438 /* BIP does not use Protected field, so need to check MMIE */
1439 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
1440 ieee80211_get_mmie_keyidx(rx->skb) < 0))
1443 * When using MFP, Action frames are not allowed prior to
1444 * having configured keys.
1446 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1447 ieee80211_is_robust_mgmt_frame(
1448 (struct ieee80211_hdr *) rx->skb->data)))
1456 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
1458 struct ieee80211_sub_if_data *sdata = rx->sdata;
1459 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1461 if (ieee80211_has_a4(hdr->frame_control) &&
1462 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
1465 if (is_multicast_ether_addr(hdr->addr1) &&
1466 ((sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta) ||
1467 (sdata->vif.type == NL80211_IFTYPE_STATION && sdata->u.mgd.use_4addr)))
1470 return ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
1474 * requires that rx->skb is a frame with ethernet header
1476 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
1478 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
1479 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1480 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1483 * Allow EAPOL frames to us/the PAE group address regardless
1484 * of whether the frame was encrypted or not.
1486 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1487 (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
1488 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1491 if (ieee80211_802_1x_port_control(rx) ||
1492 ieee80211_drop_unencrypted(rx, fc))
1499 * requires that rx->skb is a frame with ethernet header
1502 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
1504 struct ieee80211_sub_if_data *sdata = rx->sdata;
1505 struct net_device *dev = sdata->dev;
1506 struct sk_buff *skb, *xmit_skb;
1507 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1508 struct sta_info *dsta;
1513 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1514 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1515 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
1516 (rx->flags & IEEE80211_RX_RA_MATCH) &&
1517 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
1518 if (is_multicast_ether_addr(ehdr->h_dest)) {
1520 * send multicast frames both to higher layers in
1521 * local net stack and back to the wireless medium
1523 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1524 if (!xmit_skb && net_ratelimit())
1525 printk(KERN_DEBUG "%s: failed to clone "
1526 "multicast frame\n", dev->name);
1528 dsta = sta_info_get(sdata, skb->data);
1531 * The destination station is associated to
1532 * this AP (in this VLAN), so send the frame
1533 * directly to it and do not pass it to local
1543 int align __maybe_unused;
1545 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1547 * 'align' will only take the values 0 or 2 here
1548 * since all frames are required to be aligned
1549 * to 2-byte boundaries when being passed to
1550 * mac80211. That also explains the __skb_push()
1553 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
1555 if (WARN_ON(skb_headroom(skb) < 3)) {
1559 u8 *data = skb->data;
1560 size_t len = skb_headlen(skb);
1562 memmove(skb->data, data, len);
1563 skb_set_tail_pointer(skb, len);
1569 /* deliver to local stack */
1570 skb->protocol = eth_type_trans(skb, dev);
1571 memset(skb->cb, 0, sizeof(skb->cb));
1577 /* send to wireless media */
1578 xmit_skb->protocol = htons(ETH_P_802_3);
1579 skb_reset_network_header(xmit_skb);
1580 skb_reset_mac_header(xmit_skb);
1581 dev_queue_xmit(xmit_skb);
1585 static ieee80211_rx_result debug_noinline
1586 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
1588 struct net_device *dev = rx->sdata->dev;
1589 struct sk_buff *skb = rx->skb;
1590 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1591 __le16 fc = hdr->frame_control;
1592 struct sk_buff_head frame_list;
1594 if (unlikely(!ieee80211_is_data(fc)))
1597 if (unlikely(!ieee80211_is_data_present(fc)))
1598 return RX_DROP_MONITOR;
1600 if (!(rx->flags & IEEE80211_RX_AMSDU))
1603 if (ieee80211_has_a4(hdr->frame_control) &&
1604 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1605 !rx->sdata->u.vlan.sta)
1606 return RX_DROP_UNUSABLE;
1608 if (is_multicast_ether_addr(hdr->addr1) &&
1609 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1610 rx->sdata->u.vlan.sta) ||
1611 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1612 rx->sdata->u.mgd.use_4addr)))
1613 return RX_DROP_UNUSABLE;
1616 __skb_queue_head_init(&frame_list);
1618 if (skb_linearize(skb))
1619 return RX_DROP_UNUSABLE;
1621 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
1622 rx->sdata->vif.type,
1623 rx->local->hw.extra_tx_headroom);
1625 while (!skb_queue_empty(&frame_list)) {
1626 rx->skb = __skb_dequeue(&frame_list);
1628 if (!ieee80211_frame_allowed(rx, fc)) {
1629 dev_kfree_skb(rx->skb);
1632 dev->stats.rx_packets++;
1633 dev->stats.rx_bytes += rx->skb->len;
1635 ieee80211_deliver_skb(rx);
1641 #ifdef CONFIG_MAC80211_MESH
1642 static ieee80211_rx_result
1643 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1645 struct ieee80211_hdr *hdr;
1646 struct ieee80211s_hdr *mesh_hdr;
1647 unsigned int hdrlen;
1648 struct sk_buff *skb = rx->skb, *fwd_skb;
1649 struct ieee80211_local *local = rx->local;
1650 struct ieee80211_sub_if_data *sdata = rx->sdata;
1652 hdr = (struct ieee80211_hdr *) skb->data;
1653 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1654 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1656 if (!ieee80211_is_data(hdr->frame_control))
1661 return RX_DROP_MONITOR;
1663 if (mesh_hdr->flags & MESH_FLAGS_AE) {
1664 struct mesh_path *mppath;
1668 if (is_multicast_ether_addr(hdr->addr1)) {
1669 mpp_addr = hdr->addr3;
1670 proxied_addr = mesh_hdr->eaddr1;
1672 mpp_addr = hdr->addr4;
1673 proxied_addr = mesh_hdr->eaddr2;
1677 mppath = mpp_path_lookup(proxied_addr, sdata);
1679 mpp_path_add(proxied_addr, mpp_addr, sdata);
1681 spin_lock_bh(&mppath->state_lock);
1682 if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1683 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
1684 spin_unlock_bh(&mppath->state_lock);
1689 /* Frame has reached destination. Don't forward */
1690 if (!is_multicast_ether_addr(hdr->addr1) &&
1691 compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
1696 if (rx->flags & IEEE80211_RX_RA_MATCH) {
1698 IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
1699 dropped_frames_ttl);
1701 struct ieee80211_hdr *fwd_hdr;
1702 struct ieee80211_tx_info *info;
1704 fwd_skb = skb_copy(skb, GFP_ATOMIC);
1706 if (!fwd_skb && net_ratelimit())
1707 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
1710 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
1711 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
1712 info = IEEE80211_SKB_CB(fwd_skb);
1713 memset(info, 0, sizeof(*info));
1714 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1715 info->control.vif = &rx->sdata->vif;
1716 skb_set_queue_mapping(skb,
1717 ieee80211_select_queue(rx->sdata, fwd_skb));
1718 ieee80211_set_qos_hdr(local, skb);
1719 if (is_multicast_ether_addr(fwd_hdr->addr1))
1720 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1725 * Save TA to addr1 to send TA a path error if a
1726 * suitable next hop is not found
1728 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
1730 err = mesh_nexthop_lookup(fwd_skb, sdata);
1731 /* Failed to immediately resolve next hop:
1732 * fwded frame was dropped or will be added
1733 * later to the pending skb queue. */
1735 return RX_DROP_MONITOR;
1737 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1740 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1742 ieee80211_add_pending_skb(local, fwd_skb);
1746 if (is_multicast_ether_addr(hdr->addr1) ||
1747 sdata->dev->flags & IFF_PROMISC)
1750 return RX_DROP_MONITOR;
1754 static ieee80211_rx_result debug_noinline
1755 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
1757 struct ieee80211_sub_if_data *sdata = rx->sdata;
1758 struct ieee80211_local *local = rx->local;
1759 struct net_device *dev = sdata->dev;
1760 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1761 __le16 fc = hdr->frame_control;
1764 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
1767 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
1768 return RX_DROP_MONITOR;
1771 * Allow the cooked monitor interface of an AP to see 4-addr frames so
1772 * that a 4-addr station can be detected and moved into a separate VLAN
1774 if (ieee80211_has_a4(hdr->frame_control) &&
1775 sdata->vif.type == NL80211_IFTYPE_AP)
1776 return RX_DROP_MONITOR;
1778 err = __ieee80211_data_to_8023(rx);
1780 return RX_DROP_UNUSABLE;
1782 if (!ieee80211_frame_allowed(rx, fc))
1783 return RX_DROP_MONITOR;
1787 dev->stats.rx_packets++;
1788 dev->stats.rx_bytes += rx->skb->len;
1790 if (ieee80211_is_data(hdr->frame_control) &&
1791 !is_multicast_ether_addr(hdr->addr1) &&
1792 local->hw.conf.dynamic_ps_timeout > 0 && local->ps_sdata) {
1793 mod_timer(&local->dynamic_ps_timer, jiffies +
1794 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
1797 ieee80211_deliver_skb(rx);
1802 static ieee80211_rx_result debug_noinline
1803 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
1805 struct ieee80211_local *local = rx->local;
1806 struct ieee80211_hw *hw = &local->hw;
1807 struct sk_buff *skb = rx->skb;
1808 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
1809 struct tid_ampdu_rx *tid_agg_rx;
1813 if (likely(!ieee80211_is_ctl(bar->frame_control)))
1816 if (ieee80211_is_back_req(bar->frame_control)) {
1818 return RX_DROP_MONITOR;
1819 spin_lock(&rx->sta->lock);
1820 tid = le16_to_cpu(bar->control) >> 12;
1821 if (!rx->sta->ampdu_mlme.tid_active_rx[tid]) {
1822 spin_unlock(&rx->sta->lock);
1823 return RX_DROP_MONITOR;
1825 tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
1827 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1829 /* reset session timer */
1830 if (tid_agg_rx->timeout)
1831 mod_timer(&tid_agg_rx->session_timer,
1832 TU_TO_EXP_TIME(tid_agg_rx->timeout));
1834 /* release stored frames up to start of BAR */
1835 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num,
1838 spin_unlock(&rx->sta->lock);
1845 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
1846 struct ieee80211_mgmt *mgmt,
1849 struct ieee80211_local *local = sdata->local;
1850 struct sk_buff *skb;
1851 struct ieee80211_mgmt *resp;
1853 if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
1854 /* Not to own unicast address */
1858 if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
1859 compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
1860 /* Not from the current AP or not associated yet. */
1864 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
1865 /* Too short SA Query request frame */
1869 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
1873 skb_reserve(skb, local->hw.extra_tx_headroom);
1874 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
1875 memset(resp, 0, 24);
1876 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1877 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
1878 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
1879 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1880 IEEE80211_STYPE_ACTION);
1881 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
1882 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
1883 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
1884 memcpy(resp->u.action.u.sa_query.trans_id,
1885 mgmt->u.action.u.sa_query.trans_id,
1886 WLAN_SA_QUERY_TR_ID_LEN);
1888 ieee80211_tx_skb(sdata, skb);
1891 static ieee80211_rx_result debug_noinline
1892 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
1894 struct ieee80211_local *local = rx->local;
1895 struct ieee80211_sub_if_data *sdata = rx->sdata;
1896 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1897 struct sk_buff *nskb;
1898 struct ieee80211_rx_status *status;
1899 int len = rx->skb->len;
1901 if (!ieee80211_is_action(mgmt->frame_control))
1904 /* drop too small frames */
1905 if (len < IEEE80211_MIN_ACTION_SIZE)
1906 return RX_DROP_UNUSABLE;
1908 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
1909 return RX_DROP_UNUSABLE;
1911 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
1912 return RX_DROP_UNUSABLE;
1914 if (ieee80211_drop_unencrypted_mgmt(rx))
1915 return RX_DROP_UNUSABLE;
1917 switch (mgmt->u.action.category) {
1918 case WLAN_CATEGORY_BACK:
1920 * The aggregation code is not prepared to handle
1921 * anything but STA/AP due to the BSSID handling;
1922 * IBSS could work in the code but isn't supported
1923 * by drivers or the standard.
1925 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
1926 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1927 sdata->vif.type != NL80211_IFTYPE_AP)
1930 /* verify action_code is present */
1931 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
1934 switch (mgmt->u.action.u.addba_req.action_code) {
1935 case WLAN_ACTION_ADDBA_REQ:
1936 if (len < (IEEE80211_MIN_ACTION_SIZE +
1937 sizeof(mgmt->u.action.u.addba_req)))
1938 return RX_DROP_MONITOR;
1939 ieee80211_process_addba_request(local, rx->sta, mgmt, len);
1941 case WLAN_ACTION_ADDBA_RESP:
1942 if (len < (IEEE80211_MIN_ACTION_SIZE +
1943 sizeof(mgmt->u.action.u.addba_resp)))
1945 ieee80211_process_addba_resp(local, rx->sta, mgmt, len);
1947 case WLAN_ACTION_DELBA:
1948 if (len < (IEEE80211_MIN_ACTION_SIZE +
1949 sizeof(mgmt->u.action.u.delba)))
1951 ieee80211_process_delba(sdata, rx->sta, mgmt, len);
1955 case WLAN_CATEGORY_SPECTRUM_MGMT:
1956 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
1959 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1962 /* verify action_code is present */
1963 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
1966 switch (mgmt->u.action.u.measurement.action_code) {
1967 case WLAN_ACTION_SPCT_MSR_REQ:
1968 if (len < (IEEE80211_MIN_ACTION_SIZE +
1969 sizeof(mgmt->u.action.u.measurement)))
1971 ieee80211_process_measurement_req(sdata, mgmt, len);
1973 case WLAN_ACTION_SPCT_CHL_SWITCH:
1974 if (len < (IEEE80211_MIN_ACTION_SIZE +
1975 sizeof(mgmt->u.action.u.chan_switch)))
1978 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1981 if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
1984 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
1987 case WLAN_CATEGORY_SA_QUERY:
1988 if (len < (IEEE80211_MIN_ACTION_SIZE +
1989 sizeof(mgmt->u.action.u.sa_query)))
1992 switch (mgmt->u.action.u.sa_query.action) {
1993 case WLAN_ACTION_SA_QUERY_REQUEST:
1994 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1996 ieee80211_process_sa_query_req(sdata, mgmt, len);
2000 case WLAN_CATEGORY_MESH_PLINK:
2001 case WLAN_CATEGORY_MESH_PATH_SEL:
2002 if (ieee80211_vif_is_mesh(&sdata->vif))
2003 return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
2008 * For AP mode, hostapd is responsible for handling any action
2009 * frames that we didn't handle, including returning unknown
2010 * ones. For all other modes we will return them to the sender,
2011 * setting the 0x80 bit in the action category, as required by
2012 * 802.11-2007 7.3.1.11.
2014 if (sdata->vif.type == NL80211_IFTYPE_AP ||
2015 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2016 return RX_DROP_MONITOR;
2019 * Getting here means the kernel doesn't know how to handle
2020 * it, but maybe userspace does ... include returned frames
2021 * so userspace can register for those to know whether ones
2022 * it transmitted were processed or returned.
2024 status = IEEE80211_SKB_RXCB(rx->skb);
2026 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2027 cfg80211_rx_action(rx->sdata->dev, status->freq,
2028 rx->skb->data, rx->skb->len,
2032 /* do not return rejected action frames */
2033 if (mgmt->u.action.category & 0x80)
2034 return RX_DROP_UNUSABLE;
2036 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2039 struct ieee80211_mgmt *mgmt = (void *)nskb->data;
2041 mgmt->u.action.category |= 0x80;
2042 memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
2043 memcpy(mgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
2045 memset(nskb->cb, 0, sizeof(nskb->cb));
2047 ieee80211_tx_skb(rx->sdata, nskb);
2052 rx->sta->rx_packets++;
2053 dev_kfree_skb(rx->skb);
2057 static ieee80211_rx_result debug_noinline
2058 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
2060 struct ieee80211_sub_if_data *sdata = rx->sdata;
2061 ieee80211_rx_result rxs;
2063 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
2064 return RX_DROP_MONITOR;
2066 if (ieee80211_drop_unencrypted_mgmt(rx))
2067 return RX_DROP_UNUSABLE;
2069 rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
2070 if (rxs != RX_CONTINUE)
2073 if (ieee80211_vif_is_mesh(&sdata->vif))
2074 return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
2076 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2077 return ieee80211_ibss_rx_mgmt(sdata, rx->skb);
2079 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2080 return ieee80211_sta_rx_mgmt(sdata, rx->skb);
2082 return RX_DROP_MONITOR;
2085 static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
2086 struct ieee80211_rx_data *rx)
2089 unsigned int hdrlen;
2091 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2092 if (rx->skb->len >= hdrlen + 4)
2093 keyidx = rx->skb->data[hdrlen + 3] >> 6;
2099 * Some hardware seem to generate incorrect Michael MIC
2100 * reports; ignore them to avoid triggering countermeasures.
2105 if (!ieee80211_has_protected(hdr->frame_control))
2108 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
2110 * APs with pairwise keys should never receive Michael MIC
2111 * errors for non-zero keyidx because these are reserved for
2112 * group keys and only the AP is sending real multicast
2113 * frames in the BSS.
2118 if (!ieee80211_is_data(hdr->frame_control) &&
2119 !ieee80211_is_auth(hdr->frame_control))
2122 mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
2126 /* TODO: use IEEE80211_RX_FRAGMENTED */
2127 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2128 struct ieee80211_rate *rate)
2130 struct ieee80211_sub_if_data *sdata;
2131 struct ieee80211_local *local = rx->local;
2132 struct ieee80211_rtap_hdr {
2133 struct ieee80211_radiotap_header hdr;
2138 } __attribute__ ((packed)) *rthdr;
2139 struct sk_buff *skb = rx->skb, *skb2;
2140 struct net_device *prev_dev = NULL;
2141 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2143 if (status->flag & RX_FLAG_INTERNAL_CMTR)
2146 if (skb_headroom(skb) < sizeof(*rthdr) &&
2147 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
2150 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
2151 memset(rthdr, 0, sizeof(*rthdr));
2152 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
2153 rthdr->hdr.it_present =
2154 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
2155 (1 << IEEE80211_RADIOTAP_CHANNEL));
2158 rthdr->rate_or_pad = rate->bitrate / 5;
2159 rthdr->hdr.it_present |=
2160 cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2162 rthdr->chan_freq = cpu_to_le16(status->freq);
2164 if (status->band == IEEE80211_BAND_5GHZ)
2165 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
2166 IEEE80211_CHAN_5GHZ);
2168 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
2169 IEEE80211_CHAN_2GHZ);
2171 skb_set_mac_header(skb, 0);
2172 skb->ip_summed = CHECKSUM_UNNECESSARY;
2173 skb->pkt_type = PACKET_OTHERHOST;
2174 skb->protocol = htons(ETH_P_802_2);
2176 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2177 if (!ieee80211_sdata_running(sdata))
2180 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
2181 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2185 skb2 = skb_clone(skb, GFP_ATOMIC);
2187 skb2->dev = prev_dev;
2192 prev_dev = sdata->dev;
2193 sdata->dev->stats.rx_packets++;
2194 sdata->dev->stats.rx_bytes += skb->len;
2198 skb->dev = prev_dev;
2204 status->flag |= RX_FLAG_INTERNAL_CMTR;
2212 static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
2213 struct ieee80211_rx_data *rx,
2214 struct sk_buff *skb,
2215 struct ieee80211_rate *rate)
2217 struct sk_buff_head reorder_release;
2218 ieee80211_rx_result res = RX_DROP_MONITOR;
2220 __skb_queue_head_init(&reorder_release);
2225 #define CALL_RXH(rxh) \
2228 if (res != RX_CONTINUE) \
2233 * NB: the rxh_next label works even if we jump
2234 * to it from here because then the list will
2235 * be empty, which is a trivial check
2237 CALL_RXH(ieee80211_rx_h_passive_scan)
2238 CALL_RXH(ieee80211_rx_h_check)
2240 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
2242 while ((skb = __skb_dequeue(&reorder_release))) {
2244 * all the other fields are valid across frames
2245 * that belong to an aMPDU since they are on the
2246 * same TID from the same station
2250 CALL_RXH(ieee80211_rx_h_decrypt)
2251 CALL_RXH(ieee80211_rx_h_check_more_data)
2252 CALL_RXH(ieee80211_rx_h_sta_process)
2253 CALL_RXH(ieee80211_rx_h_defragment)
2254 CALL_RXH(ieee80211_rx_h_ps_poll)
2255 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2256 /* must be after MMIC verify so header is counted in MPDU mic */
2257 CALL_RXH(ieee80211_rx_h_remove_qos_control)
2258 CALL_RXH(ieee80211_rx_h_amsdu)
2259 #ifdef CONFIG_MAC80211_MESH
2260 if (ieee80211_vif_is_mesh(&sdata->vif))
2261 CALL_RXH(ieee80211_rx_h_mesh_fwding);
2263 CALL_RXH(ieee80211_rx_h_data)
2265 /* special treatment -- needs the queue */
2266 res = ieee80211_rx_h_ctrl(rx, &reorder_release);
2267 if (res != RX_CONTINUE)
2270 CALL_RXH(ieee80211_rx_h_action)
2271 CALL_RXH(ieee80211_rx_h_mgmt)
2277 case RX_DROP_MONITOR:
2278 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
2280 rx->sta->rx_dropped++;
2283 ieee80211_rx_cooked_monitor(rx, rate);
2285 case RX_DROP_UNUSABLE:
2286 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
2288 rx->sta->rx_dropped++;
2289 dev_kfree_skb(rx->skb);
2292 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
2298 /* main receive path */
2300 static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
2301 struct ieee80211_rx_data *rx,
2302 struct ieee80211_hdr *hdr)
2304 struct sk_buff *skb = rx->skb;
2305 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2306 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
2307 int multicast = is_multicast_ether_addr(hdr->addr1);
2309 switch (sdata->vif.type) {
2310 case NL80211_IFTYPE_STATION:
2311 if (!bssid && !sdata->u.mgd.use_4addr)
2314 compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
2315 if (!(sdata->dev->flags & IFF_PROMISC))
2317 rx->flags &= ~IEEE80211_RX_RA_MATCH;
2320 case NL80211_IFTYPE_ADHOC:
2323 if (ieee80211_is_beacon(hdr->frame_control)) {
2326 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
2327 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
2329 rx->flags &= ~IEEE80211_RX_RA_MATCH;
2330 } else if (!multicast &&
2331 compare_ether_addr(sdata->vif.addr,
2333 if (!(sdata->dev->flags & IFF_PROMISC))
2335 rx->flags &= ~IEEE80211_RX_RA_MATCH;
2336 } else if (!rx->sta) {
2338 if (status->flag & RX_FLAG_HT)
2339 rate_idx = 0; /* TODO: HT rates */
2341 rate_idx = status->rate_idx;
2342 rx->sta = ieee80211_ibss_add_sta(sdata, bssid,
2343 hdr->addr2, BIT(rate_idx), GFP_ATOMIC);
2346 case NL80211_IFTYPE_MESH_POINT:
2348 compare_ether_addr(sdata->vif.addr,
2350 if (!(sdata->dev->flags & IFF_PROMISC))
2353 rx->flags &= ~IEEE80211_RX_RA_MATCH;
2356 case NL80211_IFTYPE_AP_VLAN:
2357 case NL80211_IFTYPE_AP:
2359 if (compare_ether_addr(sdata->vif.addr,
2362 } else if (!ieee80211_bssid_match(bssid,
2364 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
2366 rx->flags &= ~IEEE80211_RX_RA_MATCH;
2369 case NL80211_IFTYPE_WDS:
2370 if (bssid || !ieee80211_is_data(hdr->frame_control))
2372 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2375 case NL80211_IFTYPE_MONITOR:
2376 case NL80211_IFTYPE_UNSPECIFIED:
2377 case __NL80211_IFTYPE_AFTER_LAST:
2378 /* should never get here */
2387 * This is the actual Rx frames handler. as it blongs to Rx path it must
2388 * be called with rcu_read_lock protection.
2390 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2391 struct sk_buff *skb,
2392 struct ieee80211_rate *rate)
2394 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2395 struct ieee80211_local *local = hw_to_local(hw);
2396 struct ieee80211_sub_if_data *sdata;
2397 struct ieee80211_hdr *hdr;
2399 struct ieee80211_rx_data rx;
2401 struct ieee80211_sub_if_data *prev = NULL;
2402 struct sk_buff *skb_new;
2403 struct sta_info *sta, *tmp;
2404 bool found_sta = false;
2407 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
2408 memset(&rx, 0, sizeof(rx));
2412 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
2413 local->dot11ReceivedFragmentCount++;
2415 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2416 test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
2417 rx.flags |= IEEE80211_RX_IN_SCAN;
2419 if (ieee80211_is_mgmt(fc))
2420 err = skb_linearize(skb);
2422 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
2429 hdr = (struct ieee80211_hdr *)skb->data;
2430 ieee80211_parse_qos(&rx);
2431 ieee80211_verify_alignment(&rx);
2433 if (ieee80211_is_data(fc)) {
2434 for_each_sta_info(local, hdr->addr2, sta, tmp) {
2437 rx.sdata = sta->sdata;
2439 rx.flags |= IEEE80211_RX_RA_MATCH;
2440 prepares = prepare_for_handlers(rx.sdata, &rx, hdr);
2442 if (status->flag & RX_FLAG_MMIC_ERROR) {
2443 if (rx.flags & IEEE80211_RX_RA_MATCH)
2444 ieee80211_rx_michael_mic_report(hdr, &rx);
2451 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2452 if (!ieee80211_sdata_running(sdata))
2455 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2456 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2460 * frame is destined for this interface, but if it's
2461 * not also for the previous one we handle that after
2462 * the loop to avoid copying the SKB once too much
2470 rx.sta = sta_info_get_bss(prev, hdr->addr2);
2472 rx.flags |= IEEE80211_RX_RA_MATCH;
2473 prepares = prepare_for_handlers(prev, &rx, hdr);
2478 if (status->flag & RX_FLAG_MMIC_ERROR) {
2480 if (rx.flags & IEEE80211_RX_RA_MATCH)
2481 ieee80211_rx_michael_mic_report(hdr,
2487 * frame was destined for the previous interface
2488 * so invoke RX handlers for it
2491 skb_new = skb_copy(skb, GFP_ATOMIC);
2493 if (net_ratelimit())
2494 printk(KERN_DEBUG "%s: failed to copy "
2495 "multicast frame for %s\n",
2496 wiphy_name(local->hw.wiphy),
2500 ieee80211_invoke_rx_handlers(prev, &rx, skb_new, rate);
2506 rx.sta = sta_info_get_bss(prev, hdr->addr2);
2508 rx.flags |= IEEE80211_RX_RA_MATCH;
2509 prepares = prepare_for_handlers(prev, &rx, hdr);
2516 ieee80211_invoke_rx_handlers(prev, &rx, skb, rate);
2522 * This is the receive path handler. It is called by a low level driver when an
2523 * 802.11 MPDU is received from the hardware.
2525 void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
2527 struct ieee80211_local *local = hw_to_local(hw);
2528 struct ieee80211_rate *rate = NULL;
2529 struct ieee80211_supported_band *sband;
2530 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2532 WARN_ON_ONCE(softirq_count() == 0);
2534 if (WARN_ON(status->band < 0 ||
2535 status->band >= IEEE80211_NUM_BANDS))
2538 sband = local->hw.wiphy->bands[status->band];
2539 if (WARN_ON(!sband))
2543 * If we're suspending, it is possible although not too likely
2544 * that we'd be receiving frames after having already partially
2545 * quiesced the stack. We can't process such frames then since
2546 * that might, for example, cause stations to be added or other
2547 * driver callbacks be invoked.
2549 if (unlikely(local->quiescing || local->suspended))
2553 * The same happens when we're not even started,
2554 * but that's worth a warning.
2556 if (WARN_ON(!local->started))
2559 if (status->flag & RX_FLAG_HT) {
2561 * rate_idx is MCS index, which can be [0-76] as documented on:
2563 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
2565 * Anything else would be some sort of driver or hardware error.
2566 * The driver should catch hardware errors.
2568 if (WARN((status->rate_idx < 0 ||
2569 status->rate_idx > 76),
2570 "Rate marked as an HT rate but passed "
2571 "status->rate_idx is not "
2572 "an MCS index [0-76]: %d (0x%02x)\n",
2577 if (WARN_ON(status->rate_idx < 0 ||
2578 status->rate_idx >= sband->n_bitrates))
2580 rate = &sband->bitrates[status->rate_idx];
2584 * key references and virtual interfaces are protected using RCU
2585 * and this requires that we are in a read-side RCU section during
2586 * receive processing
2591 * Frames with failed FCS/PLCP checksum are not returned,
2592 * all other frames are returned without radiotap header
2593 * if it was previously present.
2594 * Also, frames with less than 16 bytes are dropped.
2596 skb = ieee80211_rx_monitor(local, skb, rate);
2602 __ieee80211_rx_handle_packet(hw, skb, rate);
2610 EXPORT_SYMBOL(ieee80211_rx);
2612 /* This is a version of the rx handler that can be called from hard irq
2613 * context. Post the skb on the queue and schedule the tasklet */
2614 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
2616 struct ieee80211_local *local = hw_to_local(hw);
2618 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2620 skb->pkt_type = IEEE80211_RX_MSG;
2621 skb_queue_tail(&local->skb_queue, skb);
2622 tasklet_schedule(&local->tasklet);
2624 EXPORT_SYMBOL(ieee80211_rx_irqsafe);