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)
85 if (len & 1) /* padding for RX_FLAGS if necessary */
92 * ieee80211_add_rx_radiotap_header - add radiotap header
94 * add a radiotap header containing all the fields which the hardware provided.
97 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
99 struct ieee80211_rate *rate,
102 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
103 struct ieee80211_radiotap_header *rthdr;
107 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
108 memset(rthdr, 0, rtap_len);
110 /* radiotap header, set always present flags */
112 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
113 (1 << IEEE80211_RADIOTAP_CHANNEL) |
114 (1 << IEEE80211_RADIOTAP_ANTENNA) |
115 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
116 rthdr->it_len = cpu_to_le16(rtap_len);
118 pos = (unsigned char *)(rthdr+1);
120 /* the order of the following fields is important */
122 /* IEEE80211_RADIOTAP_TSFT */
123 if (status->flag & RX_FLAG_TSFT) {
124 put_unaligned_le64(status->mactime, pos);
126 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
130 /* IEEE80211_RADIOTAP_FLAGS */
131 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
132 *pos |= IEEE80211_RADIOTAP_F_FCS;
133 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
134 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
135 if (status->flag & RX_FLAG_SHORTPRE)
136 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
139 /* IEEE80211_RADIOTAP_RATE */
140 if (status->flag & RX_FLAG_HT) {
142 * TODO: add following information into radiotap header once
143 * suitable fields are defined for it:
144 * - MCS index (status->rate_idx)
145 * - HT40 (status->flag & RX_FLAG_40MHZ)
146 * - short-GI (status->flag & RX_FLAG_SHORT_GI)
150 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
151 *pos = rate->bitrate / 5;
155 /* IEEE80211_RADIOTAP_CHANNEL */
156 put_unaligned_le16(status->freq, pos);
158 if (status->band == IEEE80211_BAND_5GHZ)
159 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
161 else if (status->flag & RX_FLAG_HT)
162 put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
164 else if (rate->flags & IEEE80211_RATE_ERP_G)
165 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
168 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
172 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
173 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
174 *pos = status->signal;
176 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
180 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
182 /* IEEE80211_RADIOTAP_ANTENNA */
183 *pos = status->antenna;
186 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
188 /* IEEE80211_RADIOTAP_RX_FLAGS */
189 /* ensure 2 byte alignment for the 2 byte field as required */
190 if ((pos - (u8 *)rthdr) & 1)
192 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
193 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
194 put_unaligned_le16(rx_flags, pos);
199 * This function copies a received frame to all monitor interfaces and
200 * returns a cleaned-up SKB that no longer includes the FCS nor the
201 * radiotap header the driver might have added.
203 static struct sk_buff *
204 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
205 struct ieee80211_rate *rate)
207 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
208 struct ieee80211_sub_if_data *sdata;
209 int needed_headroom = 0;
210 struct sk_buff *skb, *skb2;
211 struct net_device *prev_dev = NULL;
212 int present_fcs_len = 0;
215 * First, we may need to make a copy of the skb because
216 * (1) we need to modify it for radiotap (if not present), and
217 * (2) the other RX handlers will modify the skb we got.
219 * We don't need to, of course, if we aren't going to return
220 * the SKB because it has a bad FCS/PLCP checksum.
223 /* room for the radiotap header based on driver features */
224 needed_headroom = ieee80211_rx_radiotap_len(local, status);
226 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
227 present_fcs_len = FCS_LEN;
229 /* make sure hdr->frame_control is on the linear part */
230 if (!pskb_may_pull(origskb, 2)) {
231 dev_kfree_skb(origskb);
235 if (!local->monitors) {
236 if (should_drop_frame(origskb, present_fcs_len)) {
237 dev_kfree_skb(origskb);
241 return remove_monitor_info(local, origskb);
244 if (should_drop_frame(origskb, present_fcs_len)) {
245 /* only need to expand headroom if necessary */
250 * This shouldn't trigger often because most devices have an
251 * RX header they pull before we get here, and that should
252 * be big enough for our radiotap information. We should
253 * probably export the length to drivers so that we can have
254 * them allocate enough headroom to start with.
256 if (skb_headroom(skb) < needed_headroom &&
257 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
263 * Need to make a copy and possibly remove radiotap header
264 * and FCS from the original.
266 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
268 origskb = remove_monitor_info(local, origskb);
274 /* prepend radiotap information */
275 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
277 skb_reset_mac_header(skb);
278 skb->ip_summed = CHECKSUM_UNNECESSARY;
279 skb->pkt_type = PACKET_OTHERHOST;
280 skb->protocol = htons(ETH_P_802_2);
282 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
283 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
286 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
289 if (!ieee80211_sdata_running(sdata))
293 skb2 = skb_clone(skb, GFP_ATOMIC);
295 skb2->dev = prev_dev;
296 netif_receive_skb(skb2);
300 prev_dev = sdata->dev;
301 sdata->dev->stats.rx_packets++;
302 sdata->dev->stats.rx_bytes += skb->len;
307 netif_receive_skb(skb);
315 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
317 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
318 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
321 /* does the frame have a qos control field? */
322 if (ieee80211_is_data_qos(hdr->frame_control)) {
323 u8 *qc = ieee80211_get_qos_ctl(hdr);
324 /* frame has qos control */
325 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
326 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
327 status->rx_flags |= IEEE80211_RX_AMSDU;
330 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
332 * Sequence numbers for management frames, QoS data
333 * frames with a broadcast/multicast address in the
334 * Address 1 field, and all non-QoS data frames sent
335 * by QoS STAs are assigned using an additional single
336 * modulo-4096 counter, [...]
338 * We also use that counter for non-QoS STAs.
340 tid = NUM_RX_DATA_QUEUES - 1;
344 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
345 * For now, set skb->priority to 0 for other cases. */
346 rx->skb->priority = (tid > 7) ? 0 : tid;
350 * DOC: Packet alignment
352 * Drivers always need to pass packets that are aligned to two-byte boundaries
355 * Additionally, should, if possible, align the payload data in a way that
356 * guarantees that the contained IP header is aligned to a four-byte
357 * boundary. In the case of regular frames, this simply means aligning the
358 * payload to a four-byte boundary (because either the IP header is directly
359 * contained, or IV/RFC1042 headers that have a length divisible by four are
360 * in front of it). If the payload data is not properly aligned and the
361 * architecture doesn't support efficient unaligned operations, mac80211
362 * will align the data.
364 * With A-MSDU frames, however, the payload data address must yield two modulo
365 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
366 * push the IP header further back to a multiple of four again. Thankfully, the
367 * specs were sane enough this time around to require padding each A-MSDU
368 * subframe to a length that is a multiple of four.
370 * Padding like Atheros hardware adds which is inbetween the 802.11 header and
371 * the payload is not supported, the driver is required to move the 802.11
372 * header to be directly in front of the payload in that case.
374 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
376 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
377 WARN_ONCE((unsigned long)rx->skb->data & 1,
378 "unaligned packet at 0x%p\n", rx->skb->data);
385 static ieee80211_rx_result debug_noinline
386 ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
388 struct ieee80211_local *local = rx->local;
389 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
390 struct sk_buff *skb = rx->skb;
392 if (likely(!(status->rx_flags & IEEE80211_RX_IN_SCAN)))
395 if (test_bit(SCAN_HW_SCANNING, &local->scanning))
396 return ieee80211_scan_rx(rx->sdata, skb);
398 if (test_bit(SCAN_SW_SCANNING, &local->scanning)) {
399 /* drop all the other packets during a software scan anyway */
400 if (ieee80211_scan_rx(rx->sdata, skb) != RX_QUEUED)
405 /* scanning finished during invoking of handlers */
406 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
407 return RX_DROP_UNUSABLE;
411 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
413 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
415 if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
418 return ieee80211_is_robust_mgmt_frame(hdr);
422 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
424 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
426 if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
429 return ieee80211_is_robust_mgmt_frame(hdr);
433 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
434 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
436 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
437 struct ieee80211_mmie *mmie;
439 if (skb->len < 24 + sizeof(*mmie) ||
440 !is_multicast_ether_addr(hdr->da))
443 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
444 return -1; /* not a robust management frame */
446 mmie = (struct ieee80211_mmie *)
447 (skb->data + skb->len - sizeof(*mmie));
448 if (mmie->element_id != WLAN_EID_MMIE ||
449 mmie->length != sizeof(*mmie) - 2)
452 return le16_to_cpu(mmie->key_id);
456 static ieee80211_rx_result
457 ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
459 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
460 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
461 char *dev_addr = rx->sdata->vif.addr;
463 if (ieee80211_is_data(hdr->frame_control)) {
464 if (is_multicast_ether_addr(hdr->addr1)) {
465 if (ieee80211_has_tods(hdr->frame_control) ||
466 !ieee80211_has_fromds(hdr->frame_control))
467 return RX_DROP_MONITOR;
468 if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
469 return RX_DROP_MONITOR;
471 if (!ieee80211_has_a4(hdr->frame_control))
472 return RX_DROP_MONITOR;
473 if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
474 return RX_DROP_MONITOR;
478 /* If there is not an established peer link and this is not a peer link
479 * establisment frame, beacon or probe, drop the frame.
482 if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
483 struct ieee80211_mgmt *mgmt;
485 if (!ieee80211_is_mgmt(hdr->frame_control))
486 return RX_DROP_MONITOR;
488 if (ieee80211_is_action(hdr->frame_control)) {
489 mgmt = (struct ieee80211_mgmt *)hdr;
490 if (mgmt->u.action.category != WLAN_CATEGORY_MESH_PLINK)
491 return RX_DROP_MONITOR;
495 if (ieee80211_is_probe_req(hdr->frame_control) ||
496 ieee80211_is_probe_resp(hdr->frame_control) ||
497 ieee80211_is_beacon(hdr->frame_control))
500 return RX_DROP_MONITOR;
504 #define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
506 if (ieee80211_is_data(hdr->frame_control) &&
507 is_multicast_ether_addr(hdr->addr1) &&
508 mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
509 return RX_DROP_MONITOR;
515 #define SEQ_MODULO 0x1000
516 #define SEQ_MASK 0xfff
518 static inline int seq_less(u16 sq1, u16 sq2)
520 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
523 static inline u16 seq_inc(u16 sq)
525 return (sq + 1) & SEQ_MASK;
528 static inline u16 seq_sub(u16 sq1, u16 sq2)
530 return (sq1 - sq2) & SEQ_MASK;
534 static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
535 struct tid_ampdu_rx *tid_agg_rx,
537 struct sk_buff_head *frames)
539 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
544 /* release the frame from the reorder ring buffer */
545 tid_agg_rx->stored_mpdu_num--;
546 tid_agg_rx->reorder_buf[index] = NULL;
547 __skb_queue_tail(frames, skb);
550 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
553 static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
554 struct tid_ampdu_rx *tid_agg_rx,
556 struct sk_buff_head *frames)
560 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
561 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
562 tid_agg_rx->buf_size;
563 ieee80211_release_reorder_frame(hw, tid_agg_rx, index, frames);
568 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
569 * the skb was added to the buffer longer than this time ago, the earlier
570 * frames that have not yet been received are assumed to be lost and the skb
571 * can be released for processing. This may also release other skb's from the
572 * reorder buffer if there are no additional gaps between the frames.
574 * Callers must hold tid_agg_rx->reorder_lock.
576 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
578 static void ieee80211_sta_reorder_release(struct ieee80211_hw *hw,
579 struct tid_ampdu_rx *tid_agg_rx,
580 struct sk_buff_head *frames)
584 /* release the buffer until next missing frame */
585 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
586 tid_agg_rx->buf_size;
587 if (!tid_agg_rx->reorder_buf[index] &&
588 tid_agg_rx->stored_mpdu_num > 1) {
590 * No buffers ready to be released, but check whether any
591 * frames in the reorder buffer have timed out.
594 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
595 j = (j + 1) % tid_agg_rx->buf_size) {
596 if (!tid_agg_rx->reorder_buf[j]) {
600 if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
601 HT_RX_REORDER_BUF_TIMEOUT))
602 goto set_release_timer;
604 #ifdef CONFIG_MAC80211_HT_DEBUG
606 wiphy_debug(hw->wiphy,
607 "release an RX reorder frame due to timeout on earlier frames\n");
609 ieee80211_release_reorder_frame(hw, tid_agg_rx,
613 * Increment the head seq# also for the skipped slots.
615 tid_agg_rx->head_seq_num =
616 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
619 } else while (tid_agg_rx->reorder_buf[index]) {
620 ieee80211_release_reorder_frame(hw, tid_agg_rx, index, frames);
621 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
622 tid_agg_rx->buf_size;
625 if (tid_agg_rx->stored_mpdu_num) {
626 j = index = seq_sub(tid_agg_rx->head_seq_num,
627 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
629 for (; j != (index - 1) % tid_agg_rx->buf_size;
630 j = (j + 1) % tid_agg_rx->buf_size) {
631 if (tid_agg_rx->reorder_buf[j])
637 mod_timer(&tid_agg_rx->reorder_timer,
638 tid_agg_rx->reorder_time[j] +
639 HT_RX_REORDER_BUF_TIMEOUT);
641 del_timer(&tid_agg_rx->reorder_timer);
646 * As this function belongs to the RX path it must be under
647 * rcu_read_lock protection. It returns false if the frame
648 * can be processed immediately, true if it was consumed.
650 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
651 struct tid_ampdu_rx *tid_agg_rx,
653 struct sk_buff_head *frames)
655 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
656 u16 sc = le16_to_cpu(hdr->seq_ctrl);
657 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
658 u16 head_seq_num, buf_size;
662 buf_size = tid_agg_rx->buf_size;
663 head_seq_num = tid_agg_rx->head_seq_num;
665 spin_lock(&tid_agg_rx->reorder_lock);
666 /* frame with out of date sequence number */
667 if (seq_less(mpdu_seq_num, head_seq_num)) {
673 * If frame the sequence number exceeds our buffering window
674 * size release some previous frames to make room for this one.
676 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
677 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
678 /* release stored frames up to new head to stack */
679 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num,
683 /* Now the new frame is always in the range of the reordering buffer */
685 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
687 /* check if we already stored this frame */
688 if (tid_agg_rx->reorder_buf[index]) {
694 * If the current MPDU is in the right order and nothing else
695 * is stored we can process it directly, no need to buffer it.
697 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
698 tid_agg_rx->stored_mpdu_num == 0) {
699 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
704 /* put the frame in the reordering buffer */
705 tid_agg_rx->reorder_buf[index] = skb;
706 tid_agg_rx->reorder_time[index] = jiffies;
707 tid_agg_rx->stored_mpdu_num++;
708 ieee80211_sta_reorder_release(hw, tid_agg_rx, frames);
711 spin_unlock(&tid_agg_rx->reorder_lock);
716 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
717 * true if the MPDU was buffered, false if it should be processed.
719 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
720 struct sk_buff_head *frames)
722 struct sk_buff *skb = rx->skb;
723 struct ieee80211_local *local = rx->local;
724 struct ieee80211_hw *hw = &local->hw;
725 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
726 struct sta_info *sta = rx->sta;
727 struct tid_ampdu_rx *tid_agg_rx;
731 if (!ieee80211_is_data_qos(hdr->frame_control))
735 * filter the QoS data rx stream according to
736 * STA/TID and check if this STA/TID is on aggregation
742 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
744 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
748 /* qos null data frames are excluded */
749 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
752 /* new, potentially un-ordered, ampdu frame - process it */
754 /* reset session timer */
755 if (tid_agg_rx->timeout)
756 mod_timer(&tid_agg_rx->session_timer,
757 TU_TO_EXP_TIME(tid_agg_rx->timeout));
759 /* if this mpdu is fragmented - terminate rx aggregation session */
760 sc = le16_to_cpu(hdr->seq_ctrl);
761 if (sc & IEEE80211_SCTL_FRAG) {
762 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
763 skb_queue_tail(&rx->sdata->skb_queue, skb);
764 ieee80211_queue_work(&local->hw, &rx->sdata->work);
769 * No locking needed -- we will only ever process one
770 * RX packet at a time, and thus own tid_agg_rx. All
771 * other code manipulating it needs to (and does) make
772 * sure that we cannot get to it any more before doing
775 if (ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb, frames))
779 __skb_queue_tail(frames, skb);
782 static ieee80211_rx_result debug_noinline
783 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
785 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
786 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
788 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
789 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
790 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
791 rx->sta->last_seq_ctrl[rx->queue] ==
793 if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
794 rx->local->dot11FrameDuplicateCount++;
795 rx->sta->num_duplicates++;
797 return RX_DROP_MONITOR;
799 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
802 if (unlikely(rx->skb->len < 16)) {
803 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
804 return RX_DROP_MONITOR;
807 /* Drop disallowed frame classes based on STA auth/assoc state;
808 * IEEE 802.11, Chap 5.5.
810 * mac80211 filters only based on association state, i.e. it drops
811 * Class 3 frames from not associated stations. hostapd sends
812 * deauth/disassoc frames when needed. In addition, hostapd is
813 * responsible for filtering on both auth and assoc states.
816 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
817 return ieee80211_rx_mesh_check(rx);
819 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
820 ieee80211_is_pspoll(hdr->frame_control)) &&
821 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
822 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
823 (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
824 if ((!ieee80211_has_fromds(hdr->frame_control) &&
825 !ieee80211_has_tods(hdr->frame_control) &&
826 ieee80211_is_data(hdr->frame_control)) ||
827 !(status->rx_flags & IEEE80211_RX_RA_MATCH)) {
828 /* Drop IBSS frames and frames for other hosts
830 return RX_DROP_MONITOR;
833 return RX_DROP_MONITOR;
840 static ieee80211_rx_result debug_noinline
841 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
843 struct sk_buff *skb = rx->skb;
844 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
845 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
848 ieee80211_rx_result result = RX_DROP_UNUSABLE;
849 struct ieee80211_key *sta_ptk = NULL;
850 int mmie_keyidx = -1;
856 * There are four types of keys:
858 * - IGTK (group keys for management frames)
859 * - PTK (pairwise keys)
860 * - STK (station-to-station pairwise keys)
862 * When selecting a key, we have to distinguish between multicast
863 * (including broadcast) and unicast frames, the latter can only
864 * use PTKs and STKs while the former always use GTKs and IGTKs.
865 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
866 * unicast frames can also use key indices like GTKs. Hence, if we
867 * don't have a PTK/STK we check the key index for a WEP key.
869 * Note that in a regular BSS, multicast frames are sent by the
870 * AP only, associated stations unicast the frame to the AP first
871 * which then multicasts it on their behalf.
873 * There is also a slight problem in IBSS mode: GTKs are negotiated
874 * with each station, that is something we don't currently handle.
875 * The spec seems to expect that one negotiates the same key with
876 * every station but there's no such requirement; VLANs could be
881 * No point in finding a key and decrypting if the frame is neither
882 * addressed to us nor a multicast frame.
884 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
887 /* start without a key */
891 sta_ptk = rcu_dereference(rx->sta->ptk);
893 fc = hdr->frame_control;
895 if (!ieee80211_has_protected(fc))
896 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
898 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
900 if ((status->flag & RX_FLAG_DECRYPTED) &&
901 (status->flag & RX_FLAG_IV_STRIPPED))
903 /* Skip decryption if the frame is not protected. */
904 if (!ieee80211_has_protected(fc))
906 } else if (mmie_keyidx >= 0) {
907 /* Broadcast/multicast robust management frame / BIP */
908 if ((status->flag & RX_FLAG_DECRYPTED) &&
909 (status->flag & RX_FLAG_IV_STRIPPED))
912 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
913 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
914 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
916 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
918 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
919 } else if (!ieee80211_has_protected(fc)) {
921 * The frame was not protected, so skip decryption. However, we
922 * need to set rx->key if there is a key that could have been
923 * used so that the frame may be dropped if encryption would
924 * have been expected.
926 struct ieee80211_key *key = NULL;
927 if (ieee80211_is_mgmt(fc) &&
928 is_multicast_ether_addr(hdr->addr1) &&
929 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
931 else if ((key = rcu_dereference(rx->sdata->default_key)))
937 * The device doesn't give us the IV so we won't be
938 * able to look up the key. That's ok though, we
939 * don't need to decrypt the frame, we just won't
940 * be able to keep statistics accurate.
941 * Except for key threshold notifications, should
942 * we somehow allow the driver to tell us which key
943 * the hardware used if this flag is set?
945 if ((status->flag & RX_FLAG_DECRYPTED) &&
946 (status->flag & RX_FLAG_IV_STRIPPED))
949 hdrlen = ieee80211_hdrlen(fc);
951 if (rx->skb->len < 8 + hdrlen)
952 return RX_DROP_UNUSABLE; /* TODO: count this? */
955 * no need to call ieee80211_wep_get_keyidx,
956 * it verifies a bunch of things we've done already
958 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
961 /* check per-station GTK first, if multicast packet */
962 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
963 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
965 /* if not found, try default key */
967 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
970 * RSNA-protected unicast frames should always be
971 * sent with pairwise or station-to-station keys,
972 * but for WEP we allow using a key index as well.
975 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
976 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
977 !is_multicast_ether_addr(hdr->addr1))
983 rx->key->tx_rx_count++;
984 /* TODO: add threshold stuff again */
986 return RX_DROP_MONITOR;
989 if (skb_linearize(rx->skb))
990 return RX_DROP_UNUSABLE;
991 /* the hdr variable is invalid now! */
993 switch (rx->key->conf.cipher) {
994 case WLAN_CIPHER_SUITE_WEP40:
995 case WLAN_CIPHER_SUITE_WEP104:
996 /* Check for weak IVs if possible */
997 if (rx->sta && ieee80211_is_data(fc) &&
998 (!(status->flag & RX_FLAG_IV_STRIPPED) ||
999 !(status->flag & RX_FLAG_DECRYPTED)) &&
1000 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
1001 rx->sta->wep_weak_iv_count++;
1003 result = ieee80211_crypto_wep_decrypt(rx);
1005 case WLAN_CIPHER_SUITE_TKIP:
1006 result = ieee80211_crypto_tkip_decrypt(rx);
1008 case WLAN_CIPHER_SUITE_CCMP:
1009 result = ieee80211_crypto_ccmp_decrypt(rx);
1011 case WLAN_CIPHER_SUITE_AES_CMAC:
1012 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1016 * We can reach here only with HW-only algorithms
1017 * but why didn't it decrypt the frame?!
1019 return RX_DROP_UNUSABLE;
1022 /* either the frame has been decrypted or will be dropped */
1023 status->flag |= RX_FLAG_DECRYPTED;
1028 static ieee80211_rx_result debug_noinline
1029 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1031 struct ieee80211_local *local;
1032 struct ieee80211_hdr *hdr;
1033 struct sk_buff *skb;
1037 hdr = (struct ieee80211_hdr *) skb->data;
1039 if (!local->pspolling)
1042 if (!ieee80211_has_fromds(hdr->frame_control))
1043 /* this is not from AP */
1046 if (!ieee80211_is_data(hdr->frame_control))
1049 if (!ieee80211_has_moredata(hdr->frame_control)) {
1050 /* AP has no more frames buffered for us */
1051 local->pspolling = false;
1055 /* more data bit is set, let's request a new frame from the AP */
1056 ieee80211_send_pspoll(local, rx->sdata);
1061 static void ap_sta_ps_start(struct sta_info *sta)
1063 struct ieee80211_sub_if_data *sdata = sta->sdata;
1064 struct ieee80211_local *local = sdata->local;
1066 atomic_inc(&sdata->bss->num_sta_ps);
1067 set_sta_flags(sta, WLAN_STA_PS_STA);
1068 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1069 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1070 printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
1071 sdata->name, sta->sta.addr, sta->sta.aid);
1072 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1075 static void ap_sta_ps_end(struct sta_info *sta)
1077 struct ieee80211_sub_if_data *sdata = sta->sdata;
1079 atomic_dec(&sdata->bss->num_sta_ps);
1081 clear_sta_flags(sta, WLAN_STA_PS_STA);
1083 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1084 printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
1085 sdata->name, sta->sta.addr, sta->sta.aid);
1086 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1088 if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
1089 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1090 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
1091 sdata->name, sta->sta.addr, sta->sta.aid);
1092 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1096 ieee80211_sta_ps_deliver_wakeup(sta);
1099 static ieee80211_rx_result debug_noinline
1100 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1102 struct sta_info *sta = rx->sta;
1103 struct sk_buff *skb = rx->skb;
1104 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1105 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1111 * Update last_rx only for IBSS packets which are for the current
1112 * BSSID to avoid keeping the current IBSS network alive in cases
1113 * where other STAs start using different BSSID.
1115 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1116 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1117 NL80211_IFTYPE_ADHOC);
1118 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
1119 sta->last_rx = jiffies;
1120 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1122 * Mesh beacons will update last_rx when if they are found to
1123 * match the current local configuration when processed.
1125 sta->last_rx = jiffies;
1128 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
1131 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1132 ieee80211_sta_rx_notify(rx->sdata, hdr);
1134 sta->rx_fragments++;
1135 sta->rx_bytes += rx->skb->len;
1136 sta->last_signal = status->signal;
1139 * Change STA power saving mode only at the end of a frame
1140 * exchange sequence.
1142 if (!ieee80211_has_morefrags(hdr->frame_control) &&
1143 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1144 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1145 if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
1147 * Ignore doze->wake transitions that are
1148 * indicated by non-data frames, the standard
1149 * is unclear here, but for example going to
1150 * PS mode and then scanning would cause a
1151 * doze->wake transition for the probe request,
1152 * and that is clearly undesirable.
1154 if (ieee80211_is_data(hdr->frame_control) &&
1155 !ieee80211_has_pm(hdr->frame_control))
1158 if (ieee80211_has_pm(hdr->frame_control))
1159 ap_sta_ps_start(sta);
1164 * Drop (qos-)data::nullfunc frames silently, since they
1165 * are used only to control station power saving mode.
1167 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1168 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1169 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1172 * If we receive a 4-addr nullfunc frame from a STA
1173 * that was not moved to a 4-addr STA vlan yet, drop
1174 * the frame to the monitor interface, to make sure
1175 * that hostapd sees it
1177 if (ieee80211_has_a4(hdr->frame_control) &&
1178 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1179 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1180 !rx->sdata->u.vlan.sta)))
1181 return RX_DROP_MONITOR;
1183 * Update counter and free packet here to avoid
1184 * counting this as a dropped packed.
1187 dev_kfree_skb(rx->skb);
1192 } /* ieee80211_rx_h_sta_process */
1194 static inline struct ieee80211_fragment_entry *
1195 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1196 unsigned int frag, unsigned int seq, int rx_queue,
1197 struct sk_buff **skb)
1199 struct ieee80211_fragment_entry *entry;
1202 idx = sdata->fragment_next;
1203 entry = &sdata->fragments[sdata->fragment_next++];
1204 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1205 sdata->fragment_next = 0;
1207 if (!skb_queue_empty(&entry->skb_list)) {
1208 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1209 struct ieee80211_hdr *hdr =
1210 (struct ieee80211_hdr *) entry->skb_list.next->data;
1211 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
1212 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
1213 "addr1=%pM addr2=%pM\n",
1215 jiffies - entry->first_frag_time, entry->seq,
1216 entry->last_frag, hdr->addr1, hdr->addr2);
1218 __skb_queue_purge(&entry->skb_list);
1221 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1223 entry->first_frag_time = jiffies;
1225 entry->rx_queue = rx_queue;
1226 entry->last_frag = frag;
1228 entry->extra_len = 0;
1233 static inline struct ieee80211_fragment_entry *
1234 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
1235 unsigned int frag, unsigned int seq,
1236 int rx_queue, struct ieee80211_hdr *hdr)
1238 struct ieee80211_fragment_entry *entry;
1241 idx = sdata->fragment_next;
1242 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1243 struct ieee80211_hdr *f_hdr;
1247 idx = IEEE80211_FRAGMENT_MAX - 1;
1249 entry = &sdata->fragments[idx];
1250 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1251 entry->rx_queue != rx_queue ||
1252 entry->last_frag + 1 != frag)
1255 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1258 * Check ftype and addresses are equal, else check next fragment
1260 if (((hdr->frame_control ^ f_hdr->frame_control) &
1261 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1262 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
1263 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
1266 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1267 __skb_queue_purge(&entry->skb_list);
1276 static ieee80211_rx_result debug_noinline
1277 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1279 struct ieee80211_hdr *hdr;
1282 unsigned int frag, seq;
1283 struct ieee80211_fragment_entry *entry;
1284 struct sk_buff *skb;
1285 struct ieee80211_rx_status *status;
1287 hdr = (struct ieee80211_hdr *)rx->skb->data;
1288 fc = hdr->frame_control;
1289 sc = le16_to_cpu(hdr->seq_ctrl);
1290 frag = sc & IEEE80211_SCTL_FRAG;
1292 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
1293 (rx->skb)->len < 24 ||
1294 is_multicast_ether_addr(hdr->addr1))) {
1295 /* not fragmented */
1298 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1300 if (skb_linearize(rx->skb))
1301 return RX_DROP_UNUSABLE;
1304 * skb_linearize() might change the skb->data and
1305 * previously cached variables (in this case, hdr) need to
1306 * be refreshed with the new data.
1308 hdr = (struct ieee80211_hdr *)rx->skb->data;
1309 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1312 /* This is the first fragment of a new frame. */
1313 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
1314 rx->queue, &(rx->skb));
1315 if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
1316 ieee80211_has_protected(fc)) {
1317 int queue = ieee80211_is_mgmt(fc) ?
1318 NUM_RX_DATA_QUEUES : rx->queue;
1319 /* Store CCMP PN so that we can verify that the next
1320 * fragment has a sequential PN value. */
1322 memcpy(entry->last_pn,
1323 rx->key->u.ccmp.rx_pn[queue],
1329 /* This is a fragment for a frame that should already be pending in
1330 * fragment cache. Add this fragment to the end of the pending entry.
1332 entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
1334 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1335 return RX_DROP_MONITOR;
1338 /* Verify that MPDUs within one MSDU have sequential PN values.
1339 * (IEEE 802.11i, 8.3.3.4.5) */
1342 u8 pn[CCMP_PN_LEN], *rpn;
1344 if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
1345 return RX_DROP_UNUSABLE;
1346 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
1347 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
1352 queue = ieee80211_is_mgmt(fc) ?
1353 NUM_RX_DATA_QUEUES : rx->queue;
1354 rpn = rx->key->u.ccmp.rx_pn[queue];
1355 if (memcmp(pn, rpn, CCMP_PN_LEN))
1356 return RX_DROP_UNUSABLE;
1357 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
1360 skb_pull(rx->skb, ieee80211_hdrlen(fc));
1361 __skb_queue_tail(&entry->skb_list, rx->skb);
1362 entry->last_frag = frag;
1363 entry->extra_len += rx->skb->len;
1364 if (ieee80211_has_morefrags(fc)) {
1369 rx->skb = __skb_dequeue(&entry->skb_list);
1370 if (skb_tailroom(rx->skb) < entry->extra_len) {
1371 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1372 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1374 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1375 __skb_queue_purge(&entry->skb_list);
1376 return RX_DROP_UNUSABLE;
1379 while ((skb = __skb_dequeue(&entry->skb_list))) {
1380 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1384 /* Complete frame has been reassembled - process it now */
1385 status = IEEE80211_SKB_RXCB(rx->skb);
1386 status->rx_flags |= IEEE80211_RX_FRAGMENTED;
1390 rx->sta->rx_packets++;
1391 if (is_multicast_ether_addr(hdr->addr1))
1392 rx->local->dot11MulticastReceivedFrameCount++;
1394 ieee80211_led_rx(rx->local);
1398 static ieee80211_rx_result debug_noinline
1399 ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
1401 struct ieee80211_sub_if_data *sdata = rx->sdata;
1402 __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
1403 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1405 if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
1406 !(status->rx_flags & IEEE80211_RX_RA_MATCH)))
1409 if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
1410 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1411 return RX_DROP_UNUSABLE;
1413 if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
1414 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1416 set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
1418 /* Free PS Poll skb here instead of returning RX_DROP that would
1419 * count as an dropped frame. */
1420 dev_kfree_skb(rx->skb);
1425 static ieee80211_rx_result debug_noinline
1426 ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
1428 u8 *data = rx->skb->data;
1429 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
1431 if (!ieee80211_is_data_qos(hdr->frame_control))
1434 /* remove the qos control field, update frame type and meta-data */
1435 memmove(data + IEEE80211_QOS_CTL_LEN, data,
1436 ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
1437 hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
1438 /* change frame type to non QOS */
1439 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1445 ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
1447 if (unlikely(!rx->sta ||
1448 !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
1455 ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
1457 struct sk_buff *skb = rx->skb;
1458 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1461 * Pass through unencrypted frames if the hardware has
1462 * decrypted them already.
1464 if (status->flag & RX_FLAG_DECRYPTED)
1467 /* Drop unencrypted frames if key is set. */
1468 if (unlikely(!ieee80211_has_protected(fc) &&
1469 !ieee80211_is_nullfunc(fc) &&
1470 ieee80211_is_data(fc) &&
1471 (rx->key || rx->sdata->drop_unencrypted)))
1478 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
1480 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1481 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1482 __le16 fc = hdr->frame_control;
1485 * Pass through unencrypted frames if the hardware has
1486 * decrypted them already.
1488 if (status->flag & RX_FLAG_DECRYPTED)
1491 if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
1492 if (unlikely(!ieee80211_has_protected(fc) &&
1493 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
1496 /* BIP does not use Protected field, so need to check MMIE */
1497 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
1498 ieee80211_get_mmie_keyidx(rx->skb) < 0))
1501 * When using MFP, Action frames are not allowed prior to
1502 * having configured keys.
1504 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
1505 ieee80211_is_robust_mgmt_frame(
1506 (struct ieee80211_hdr *) rx->skb->data)))
1514 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
1516 struct ieee80211_sub_if_data *sdata = rx->sdata;
1517 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1519 if (ieee80211_has_a4(hdr->frame_control) &&
1520 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
1523 if (is_multicast_ether_addr(hdr->addr1) &&
1524 ((sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta) ||
1525 (sdata->vif.type == NL80211_IFTYPE_STATION && sdata->u.mgd.use_4addr)))
1528 return ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
1532 * requires that rx->skb is a frame with ethernet header
1534 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
1536 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
1537 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1538 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1541 * Allow EAPOL frames to us/the PAE group address regardless
1542 * of whether the frame was encrypted or not.
1544 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
1545 (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
1546 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1549 if (ieee80211_802_1x_port_control(rx) ||
1550 ieee80211_drop_unencrypted(rx, fc))
1557 * requires that rx->skb is a frame with ethernet header
1560 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
1562 struct ieee80211_sub_if_data *sdata = rx->sdata;
1563 struct net_device *dev = sdata->dev;
1564 struct sk_buff *skb, *xmit_skb;
1565 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1566 struct sta_info *dsta;
1567 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1572 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1573 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1574 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
1575 (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
1576 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
1577 if (is_multicast_ether_addr(ehdr->h_dest)) {
1579 * send multicast frames both to higher layers in
1580 * local net stack and back to the wireless medium
1582 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1583 if (!xmit_skb && net_ratelimit())
1584 printk(KERN_DEBUG "%s: failed to clone "
1585 "multicast frame\n", dev->name);
1587 dsta = sta_info_get(sdata, skb->data);
1590 * The destination station is associated to
1591 * this AP (in this VLAN), so send the frame
1592 * directly to it and do not pass it to local
1602 int align __maybe_unused;
1604 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1606 * 'align' will only take the values 0 or 2 here
1607 * since all frames are required to be aligned
1608 * to 2-byte boundaries when being passed to
1609 * mac80211. That also explains the __skb_push()
1612 align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
1614 if (WARN_ON(skb_headroom(skb) < 3)) {
1618 u8 *data = skb->data;
1619 size_t len = skb_headlen(skb);
1621 memmove(skb->data, data, len);
1622 skb_set_tail_pointer(skb, len);
1628 /* deliver to local stack */
1629 skb->protocol = eth_type_trans(skb, dev);
1630 memset(skb->cb, 0, sizeof(skb->cb));
1631 netif_receive_skb(skb);
1636 /* send to wireless media */
1637 xmit_skb->protocol = htons(ETH_P_802_3);
1638 skb_reset_network_header(xmit_skb);
1639 skb_reset_mac_header(xmit_skb);
1640 dev_queue_xmit(xmit_skb);
1644 static ieee80211_rx_result debug_noinline
1645 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
1647 struct net_device *dev = rx->sdata->dev;
1648 struct sk_buff *skb = rx->skb;
1649 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1650 __le16 fc = hdr->frame_control;
1651 struct sk_buff_head frame_list;
1652 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1654 if (unlikely(!ieee80211_is_data(fc)))
1657 if (unlikely(!ieee80211_is_data_present(fc)))
1658 return RX_DROP_MONITOR;
1660 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
1663 if (ieee80211_has_a4(hdr->frame_control) &&
1664 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1665 !rx->sdata->u.vlan.sta)
1666 return RX_DROP_UNUSABLE;
1668 if (is_multicast_ether_addr(hdr->addr1) &&
1669 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1670 rx->sdata->u.vlan.sta) ||
1671 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1672 rx->sdata->u.mgd.use_4addr)))
1673 return RX_DROP_UNUSABLE;
1676 __skb_queue_head_init(&frame_list);
1678 if (skb_linearize(skb))
1679 return RX_DROP_UNUSABLE;
1681 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
1682 rx->sdata->vif.type,
1683 rx->local->hw.extra_tx_headroom);
1685 while (!skb_queue_empty(&frame_list)) {
1686 rx->skb = __skb_dequeue(&frame_list);
1688 if (!ieee80211_frame_allowed(rx, fc)) {
1689 dev_kfree_skb(rx->skb);
1692 dev->stats.rx_packets++;
1693 dev->stats.rx_bytes += rx->skb->len;
1695 ieee80211_deliver_skb(rx);
1701 #ifdef CONFIG_MAC80211_MESH
1702 static ieee80211_rx_result
1703 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
1705 struct ieee80211_hdr *hdr;
1706 struct ieee80211s_hdr *mesh_hdr;
1707 unsigned int hdrlen;
1708 struct sk_buff *skb = rx->skb, *fwd_skb;
1709 struct ieee80211_local *local = rx->local;
1710 struct ieee80211_sub_if_data *sdata = rx->sdata;
1711 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1713 hdr = (struct ieee80211_hdr *) skb->data;
1714 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1715 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
1717 if (!ieee80211_is_data(hdr->frame_control))
1722 return RX_DROP_MONITOR;
1724 if (mesh_hdr->flags & MESH_FLAGS_AE) {
1725 struct mesh_path *mppath;
1729 if (is_multicast_ether_addr(hdr->addr1)) {
1730 mpp_addr = hdr->addr3;
1731 proxied_addr = mesh_hdr->eaddr1;
1733 mpp_addr = hdr->addr4;
1734 proxied_addr = mesh_hdr->eaddr2;
1738 mppath = mpp_path_lookup(proxied_addr, sdata);
1740 mpp_path_add(proxied_addr, mpp_addr, sdata);
1742 spin_lock_bh(&mppath->state_lock);
1743 if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
1744 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
1745 spin_unlock_bh(&mppath->state_lock);
1750 /* Frame has reached destination. Don't forward */
1751 if (!is_multicast_ether_addr(hdr->addr1) &&
1752 compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
1757 if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
1759 IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
1760 dropped_frames_ttl);
1762 struct ieee80211_hdr *fwd_hdr;
1763 struct ieee80211_tx_info *info;
1765 fwd_skb = skb_copy(skb, GFP_ATOMIC);
1767 if (!fwd_skb && net_ratelimit())
1768 printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
1771 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
1772 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
1773 info = IEEE80211_SKB_CB(fwd_skb);
1774 memset(info, 0, sizeof(*info));
1775 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1776 info->control.vif = &rx->sdata->vif;
1777 skb_set_queue_mapping(skb,
1778 ieee80211_select_queue(rx->sdata, fwd_skb));
1779 ieee80211_set_qos_hdr(local, skb);
1780 if (is_multicast_ether_addr(fwd_hdr->addr1))
1781 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1786 * Save TA to addr1 to send TA a path error if a
1787 * suitable next hop is not found
1789 memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
1791 err = mesh_nexthop_lookup(fwd_skb, sdata);
1792 /* Failed to immediately resolve next hop:
1793 * fwded frame was dropped or will be added
1794 * later to the pending skb queue. */
1796 return RX_DROP_MONITOR;
1798 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1801 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
1803 ieee80211_add_pending_skb(local, fwd_skb);
1807 if (is_multicast_ether_addr(hdr->addr1) ||
1808 sdata->dev->flags & IFF_PROMISC)
1811 return RX_DROP_MONITOR;
1815 static ieee80211_rx_result debug_noinline
1816 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
1818 struct ieee80211_sub_if_data *sdata = rx->sdata;
1819 struct ieee80211_local *local = rx->local;
1820 struct net_device *dev = sdata->dev;
1821 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1822 __le16 fc = hdr->frame_control;
1825 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
1828 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
1829 return RX_DROP_MONITOR;
1832 * Allow the cooked monitor interface of an AP to see 4-addr frames so
1833 * that a 4-addr station can be detected and moved into a separate VLAN
1835 if (ieee80211_has_a4(hdr->frame_control) &&
1836 sdata->vif.type == NL80211_IFTYPE_AP)
1837 return RX_DROP_MONITOR;
1839 err = __ieee80211_data_to_8023(rx);
1841 return RX_DROP_UNUSABLE;
1843 if (!ieee80211_frame_allowed(rx, fc))
1844 return RX_DROP_MONITOR;
1848 dev->stats.rx_packets++;
1849 dev->stats.rx_bytes += rx->skb->len;
1851 if (ieee80211_is_data(hdr->frame_control) &&
1852 !is_multicast_ether_addr(hdr->addr1) &&
1853 local->hw.conf.dynamic_ps_timeout > 0 && local->ps_sdata) {
1854 mod_timer(&local->dynamic_ps_timer, jiffies +
1855 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
1858 ieee80211_deliver_skb(rx);
1863 static ieee80211_rx_result debug_noinline
1864 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
1866 struct ieee80211_local *local = rx->local;
1867 struct ieee80211_hw *hw = &local->hw;
1868 struct sk_buff *skb = rx->skb;
1869 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
1870 struct tid_ampdu_rx *tid_agg_rx;
1874 if (likely(!ieee80211_is_ctl(bar->frame_control)))
1877 if (ieee80211_is_back_req(bar->frame_control)) {
1879 __le16 control, start_seq_num;
1880 } __packed bar_data;
1883 return RX_DROP_MONITOR;
1885 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
1886 &bar_data, sizeof(bar_data)))
1887 return RX_DROP_MONITOR;
1889 tid = le16_to_cpu(bar_data.control) >> 12;
1891 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
1893 return RX_DROP_MONITOR;
1895 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
1897 /* reset session timer */
1898 if (tid_agg_rx->timeout)
1899 mod_timer(&tid_agg_rx->session_timer,
1900 TU_TO_EXP_TIME(tid_agg_rx->timeout));
1902 /* release stored frames up to start of BAR */
1903 ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num,
1910 * After this point, we only want management frames,
1911 * so we can drop all remaining control frames to
1912 * cooked monitor interfaces.
1914 return RX_DROP_MONITOR;
1917 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
1918 struct ieee80211_mgmt *mgmt,
1921 struct ieee80211_local *local = sdata->local;
1922 struct sk_buff *skb;
1923 struct ieee80211_mgmt *resp;
1925 if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
1926 /* Not to own unicast address */
1930 if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
1931 compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
1932 /* Not from the current AP or not associated yet. */
1936 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
1937 /* Too short SA Query request frame */
1941 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
1945 skb_reserve(skb, local->hw.extra_tx_headroom);
1946 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
1947 memset(resp, 0, 24);
1948 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1949 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
1950 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
1951 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1952 IEEE80211_STYPE_ACTION);
1953 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
1954 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
1955 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
1956 memcpy(resp->u.action.u.sa_query.trans_id,
1957 mgmt->u.action.u.sa_query.trans_id,
1958 WLAN_SA_QUERY_TR_ID_LEN);
1960 ieee80211_tx_skb(sdata, skb);
1963 static ieee80211_rx_result debug_noinline
1964 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
1966 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1967 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1970 * From here on, look only at management frames.
1971 * Data and control frames are already handled,
1972 * and unknown (reserved) frames are useless.
1974 if (rx->skb->len < 24)
1975 return RX_DROP_MONITOR;
1977 if (!ieee80211_is_mgmt(mgmt->frame_control))
1978 return RX_DROP_MONITOR;
1980 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
1981 return RX_DROP_MONITOR;
1983 if (ieee80211_drop_unencrypted_mgmt(rx))
1984 return RX_DROP_UNUSABLE;
1989 static ieee80211_rx_result debug_noinline
1990 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
1992 struct ieee80211_local *local = rx->local;
1993 struct ieee80211_sub_if_data *sdata = rx->sdata;
1994 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
1995 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1996 int len = rx->skb->len;
1998 if (!ieee80211_is_action(mgmt->frame_control))
2001 /* drop too small frames */
2002 if (len < IEEE80211_MIN_ACTION_SIZE)
2003 return RX_DROP_UNUSABLE;
2005 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
2006 return RX_DROP_UNUSABLE;
2008 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
2009 return RX_DROP_UNUSABLE;
2011 switch (mgmt->u.action.category) {
2012 case WLAN_CATEGORY_BACK:
2014 * The aggregation code is not prepared to handle
2015 * anything but STA/AP due to the BSSID handling;
2016 * IBSS could work in the code but isn't supported
2017 * by drivers or the standard.
2019 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2020 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2021 sdata->vif.type != NL80211_IFTYPE_AP)
2024 /* verify action_code is present */
2025 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2028 switch (mgmt->u.action.u.addba_req.action_code) {
2029 case WLAN_ACTION_ADDBA_REQ:
2030 if (len < (IEEE80211_MIN_ACTION_SIZE +
2031 sizeof(mgmt->u.action.u.addba_req)))
2034 case WLAN_ACTION_ADDBA_RESP:
2035 if (len < (IEEE80211_MIN_ACTION_SIZE +
2036 sizeof(mgmt->u.action.u.addba_resp)))
2039 case WLAN_ACTION_DELBA:
2040 if (len < (IEEE80211_MIN_ACTION_SIZE +
2041 sizeof(mgmt->u.action.u.delba)))
2049 case WLAN_CATEGORY_SPECTRUM_MGMT:
2050 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2053 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2056 /* verify action_code is present */
2057 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2060 switch (mgmt->u.action.u.measurement.action_code) {
2061 case WLAN_ACTION_SPCT_MSR_REQ:
2062 if (len < (IEEE80211_MIN_ACTION_SIZE +
2063 sizeof(mgmt->u.action.u.measurement)))
2065 ieee80211_process_measurement_req(sdata, mgmt, len);
2067 case WLAN_ACTION_SPCT_CHL_SWITCH:
2068 if (len < (IEEE80211_MIN_ACTION_SIZE +
2069 sizeof(mgmt->u.action.u.chan_switch)))
2072 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2075 if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
2081 case WLAN_CATEGORY_SA_QUERY:
2082 if (len < (IEEE80211_MIN_ACTION_SIZE +
2083 sizeof(mgmt->u.action.u.sa_query)))
2086 switch (mgmt->u.action.u.sa_query.action) {
2087 case WLAN_ACTION_SA_QUERY_REQUEST:
2088 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2090 ieee80211_process_sa_query_req(sdata, mgmt, len);
2094 case WLAN_CATEGORY_MESH_PLINK:
2095 case WLAN_CATEGORY_MESH_PATH_SEL:
2096 if (!ieee80211_vif_is_mesh(&sdata->vif))
2104 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2105 /* will return in the next handlers */
2110 rx->sta->rx_packets++;
2111 dev_kfree_skb(rx->skb);
2115 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2116 skb_queue_tail(&sdata->skb_queue, rx->skb);
2117 ieee80211_queue_work(&local->hw, &sdata->work);
2119 rx->sta->rx_packets++;
2123 static ieee80211_rx_result debug_noinline
2124 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2126 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2128 /* skip known-bad action frames and return them in the next handler */
2129 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2133 * Getting here means the kernel doesn't know how to handle
2134 * it, but maybe userspace does ... include returned frames
2135 * so userspace can register for those to know whether ones
2136 * it transmitted were processed or returned.
2139 if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq,
2140 rx->skb->data, rx->skb->len,
2143 rx->sta->rx_packets++;
2144 dev_kfree_skb(rx->skb);
2152 static ieee80211_rx_result debug_noinline
2153 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
2155 struct ieee80211_local *local = rx->local;
2156 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2157 struct sk_buff *nskb;
2158 struct ieee80211_sub_if_data *sdata = rx->sdata;
2159 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2161 if (!ieee80211_is_action(mgmt->frame_control))
2165 * For AP mode, hostapd is responsible for handling any action
2166 * frames that we didn't handle, including returning unknown
2167 * ones. For all other modes we will return them to the sender,
2168 * setting the 0x80 bit in the action category, as required by
2169 * 802.11-2007 7.3.1.11.
2170 * Newer versions of hostapd shall also use the management frame
2171 * registration mechanisms, but older ones still use cooked
2172 * monitor interfaces so push all frames there.
2174 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
2175 (sdata->vif.type == NL80211_IFTYPE_AP ||
2176 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
2177 return RX_DROP_MONITOR;
2179 /* do not return rejected action frames */
2180 if (mgmt->u.action.category & 0x80)
2181 return RX_DROP_UNUSABLE;
2183 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2186 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
2188 nmgmt->u.action.category |= 0x80;
2189 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
2190 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
2192 memset(nskb->cb, 0, sizeof(nskb->cb));
2194 ieee80211_tx_skb(rx->sdata, nskb);
2196 dev_kfree_skb(rx->skb);
2200 static ieee80211_rx_result debug_noinline
2201 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
2203 struct ieee80211_sub_if_data *sdata = rx->sdata;
2204 ieee80211_rx_result rxs;
2205 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2208 rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
2209 if (rxs != RX_CONTINUE)
2212 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
2214 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
2215 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2216 sdata->vif.type != NL80211_IFTYPE_STATION)
2217 return RX_DROP_MONITOR;
2220 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2221 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2222 /* process for all: mesh, mlme, ibss */
2224 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2225 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2226 /* process only for station */
2227 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2228 return RX_DROP_MONITOR;
2230 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
2231 case cpu_to_le16(IEEE80211_STYPE_AUTH):
2232 /* process only for ibss */
2233 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
2234 return RX_DROP_MONITOR;
2237 return RX_DROP_MONITOR;
2240 /* queue up frame and kick off work to process it */
2241 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2242 skb_queue_tail(&sdata->skb_queue, rx->skb);
2243 ieee80211_queue_work(&rx->local->hw, &sdata->work);
2245 rx->sta->rx_packets++;
2250 static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
2251 struct ieee80211_rx_data *rx)
2254 unsigned int hdrlen;
2256 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2257 if (rx->skb->len >= hdrlen + 4)
2258 keyidx = rx->skb->data[hdrlen + 3] >> 6;
2264 * Some hardware seem to generate incorrect Michael MIC
2265 * reports; ignore them to avoid triggering countermeasures.
2270 if (!ieee80211_has_protected(hdr->frame_control))
2273 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
2275 * APs with pairwise keys should never receive Michael MIC
2276 * errors for non-zero keyidx because these are reserved for
2277 * group keys and only the AP is sending real multicast
2278 * frames in the BSS.
2283 if (!ieee80211_is_data(hdr->frame_control) &&
2284 !ieee80211_is_auth(hdr->frame_control))
2287 mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
2291 /* TODO: use IEEE80211_RX_FRAGMENTED */
2292 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2293 struct ieee80211_rate *rate)
2295 struct ieee80211_sub_if_data *sdata;
2296 struct ieee80211_local *local = rx->local;
2297 struct ieee80211_rtap_hdr {
2298 struct ieee80211_radiotap_header hdr;
2304 struct sk_buff *skb = rx->skb, *skb2;
2305 struct net_device *prev_dev = NULL;
2306 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2309 * If cooked monitor has been processed already, then
2310 * don't do it again. If not, set the flag.
2312 if (rx->flags & IEEE80211_RX_CMNTR)
2314 rx->flags |= IEEE80211_RX_CMNTR;
2316 if (skb_headroom(skb) < sizeof(*rthdr) &&
2317 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
2320 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
2321 memset(rthdr, 0, sizeof(*rthdr));
2322 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
2323 rthdr->hdr.it_present =
2324 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
2325 (1 << IEEE80211_RADIOTAP_CHANNEL));
2328 rthdr->rate_or_pad = rate->bitrate / 5;
2329 rthdr->hdr.it_present |=
2330 cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2332 rthdr->chan_freq = cpu_to_le16(status->freq);
2334 if (status->band == IEEE80211_BAND_5GHZ)
2335 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
2336 IEEE80211_CHAN_5GHZ);
2338 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
2339 IEEE80211_CHAN_2GHZ);
2341 skb_set_mac_header(skb, 0);
2342 skb->ip_summed = CHECKSUM_UNNECESSARY;
2343 skb->pkt_type = PACKET_OTHERHOST;
2344 skb->protocol = htons(ETH_P_802_2);
2346 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2347 if (!ieee80211_sdata_running(sdata))
2350 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
2351 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2355 skb2 = skb_clone(skb, GFP_ATOMIC);
2357 skb2->dev = prev_dev;
2358 netif_receive_skb(skb2);
2362 prev_dev = sdata->dev;
2363 sdata->dev->stats.rx_packets++;
2364 sdata->dev->stats.rx_bytes += skb->len;
2368 skb->dev = prev_dev;
2369 netif_receive_skb(skb);
2377 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
2378 ieee80211_rx_result res)
2381 case RX_DROP_MONITOR:
2382 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2384 rx->sta->rx_dropped++;
2387 struct ieee80211_rate *rate = NULL;
2388 struct ieee80211_supported_band *sband;
2389 struct ieee80211_rx_status *status;
2391 status = IEEE80211_SKB_RXCB((rx->skb));
2393 sband = rx->local->hw.wiphy->bands[status->band];
2394 if (!(status->flag & RX_FLAG_HT))
2395 rate = &sband->bitrates[status->rate_idx];
2397 ieee80211_rx_cooked_monitor(rx, rate);
2400 case RX_DROP_UNUSABLE:
2401 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2403 rx->sta->rx_dropped++;
2404 dev_kfree_skb(rx->skb);
2407 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
2412 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
2413 struct sk_buff_head *frames)
2415 ieee80211_rx_result res = RX_DROP_MONITOR;
2416 struct sk_buff *skb;
2418 #define CALL_RXH(rxh) \
2421 if (res != RX_CONTINUE) \
2425 while ((skb = __skb_dequeue(frames))) {
2427 * all the other fields are valid across frames
2428 * that belong to an aMPDU since they are on the
2429 * same TID from the same station
2434 CALL_RXH(ieee80211_rx_h_decrypt)
2435 CALL_RXH(ieee80211_rx_h_check_more_data)
2436 CALL_RXH(ieee80211_rx_h_sta_process)
2437 CALL_RXH(ieee80211_rx_h_defragment)
2438 CALL_RXH(ieee80211_rx_h_ps_poll)
2439 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2440 /* must be after MMIC verify so header is counted in MPDU mic */
2441 CALL_RXH(ieee80211_rx_h_remove_qos_control)
2442 CALL_RXH(ieee80211_rx_h_amsdu)
2443 #ifdef CONFIG_MAC80211_MESH
2444 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2445 CALL_RXH(ieee80211_rx_h_mesh_fwding);
2447 CALL_RXH(ieee80211_rx_h_data)
2449 /* special treatment -- needs the queue */
2450 res = ieee80211_rx_h_ctrl(rx, frames);
2451 if (res != RX_CONTINUE)
2454 CALL_RXH(ieee80211_rx_h_mgmt_check)
2455 CALL_RXH(ieee80211_rx_h_action)
2456 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
2457 CALL_RXH(ieee80211_rx_h_action_return)
2458 CALL_RXH(ieee80211_rx_h_mgmt)
2461 ieee80211_rx_handlers_result(rx, res);
2467 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
2469 struct sk_buff_head reorder_release;
2470 ieee80211_rx_result res = RX_DROP_MONITOR;
2472 __skb_queue_head_init(&reorder_release);
2474 #define CALL_RXH(rxh) \
2477 if (res != RX_CONTINUE) \
2481 CALL_RXH(ieee80211_rx_h_passive_scan)
2482 CALL_RXH(ieee80211_rx_h_check)
2484 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
2486 ieee80211_rx_handlers(rx, &reorder_release);
2490 ieee80211_rx_handlers_result(rx, res);
2496 * This function makes calls into the RX path. Therefore the
2497 * caller must hold the sta_info->lock and everything has to
2498 * be under rcu_read_lock protection as well.
2500 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
2502 struct sk_buff_head frames;
2503 struct ieee80211_rx_data rx = {
2505 .sdata = sta->sdata,
2506 .local = sta->local,
2509 struct tid_ampdu_rx *tid_agg_rx;
2511 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
2515 __skb_queue_head_init(&frames);
2517 spin_lock(&tid_agg_rx->reorder_lock);
2518 ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx, &frames);
2519 spin_unlock(&tid_agg_rx->reorder_lock);
2521 ieee80211_rx_handlers(&rx, &frames);
2524 /* main receive path */
2526 static int prepare_for_handlers(struct ieee80211_rx_data *rx,
2527 struct ieee80211_hdr *hdr)
2529 struct ieee80211_sub_if_data *sdata = rx->sdata;
2530 struct sk_buff *skb = rx->skb;
2531 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2532 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
2533 int multicast = is_multicast_ether_addr(hdr->addr1);
2535 switch (sdata->vif.type) {
2536 case NL80211_IFTYPE_STATION:
2537 if (!bssid && !sdata->u.mgd.use_4addr)
2540 compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
2541 if (!(sdata->dev->flags & IFF_PROMISC))
2543 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2546 case NL80211_IFTYPE_ADHOC:
2549 if (ieee80211_is_beacon(hdr->frame_control)) {
2552 else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
2553 if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
2555 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2556 } else if (!multicast &&
2557 compare_ether_addr(sdata->vif.addr,
2559 if (!(sdata->dev->flags & IFF_PROMISC))
2561 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2562 } else if (!rx->sta) {
2564 if (status->flag & RX_FLAG_HT)
2565 rate_idx = 0; /* TODO: HT rates */
2567 rate_idx = status->rate_idx;
2568 rx->sta = ieee80211_ibss_add_sta(sdata, bssid,
2569 hdr->addr2, BIT(rate_idx), GFP_ATOMIC);
2572 case NL80211_IFTYPE_MESH_POINT:
2574 compare_ether_addr(sdata->vif.addr,
2576 if (!(sdata->dev->flags & IFF_PROMISC))
2579 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2582 case NL80211_IFTYPE_AP_VLAN:
2583 case NL80211_IFTYPE_AP:
2585 if (compare_ether_addr(sdata->vif.addr,
2588 } else if (!ieee80211_bssid_match(bssid,
2590 if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
2592 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
2595 case NL80211_IFTYPE_WDS:
2596 if (bssid || !ieee80211_is_data(hdr->frame_control))
2598 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
2602 /* should never get here */
2611 * This function returns whether or not the SKB
2612 * was destined for RX processing or not, which,
2613 * if consume is true, is equivalent to whether
2614 * or not the skb was consumed.
2616 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
2617 struct sk_buff *skb, bool consume)
2619 struct ieee80211_local *local = rx->local;
2620 struct ieee80211_sub_if_data *sdata = rx->sdata;
2621 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2622 struct ieee80211_hdr *hdr = (void *)skb->data;
2626 status->rx_flags |= IEEE80211_RX_RA_MATCH;
2627 prepares = prepare_for_handlers(rx, hdr);
2632 if (status->flag & RX_FLAG_MMIC_ERROR) {
2633 if (status->rx_flags & IEEE80211_RX_RA_MATCH)
2634 ieee80211_rx_michael_mic_report(hdr, rx);
2639 skb = skb_copy(skb, GFP_ATOMIC);
2641 if (net_ratelimit())
2642 wiphy_debug(local->hw.wiphy,
2643 "failed to copy multicast frame for %s\n",
2651 ieee80211_invoke_rx_handlers(rx);
2656 * This is the actual Rx frames handler. as it blongs to Rx path it must
2657 * be called with rcu_read_lock protection.
2659 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
2660 struct sk_buff *skb)
2662 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2663 struct ieee80211_local *local = hw_to_local(hw);
2664 struct ieee80211_sub_if_data *sdata;
2665 struct ieee80211_hdr *hdr;
2667 struct ieee80211_rx_data rx;
2668 struct ieee80211_sub_if_data *prev;
2669 struct sta_info *sta, *tmp, *prev_sta;
2672 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
2673 memset(&rx, 0, sizeof(rx));
2677 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
2678 local->dot11ReceivedFragmentCount++;
2680 if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
2681 test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
2682 status->rx_flags |= IEEE80211_RX_IN_SCAN;
2684 if (ieee80211_is_mgmt(fc))
2685 err = skb_linearize(skb);
2687 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
2694 hdr = (struct ieee80211_hdr *)skb->data;
2695 ieee80211_parse_qos(&rx);
2696 ieee80211_verify_alignment(&rx);
2698 if (ieee80211_is_data(fc)) {
2701 for_each_sta_info(local, hdr->addr2, sta, tmp) {
2708 rx.sdata = prev_sta->sdata;
2709 ieee80211_prepare_and_rx_handle(&rx, skb, false);
2716 rx.sdata = prev_sta->sdata;
2718 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
2725 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2726 if (!ieee80211_sdata_running(sdata))
2729 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2730 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
2734 * frame is destined for this interface, but if it's
2735 * not also for the previous one we handle that after
2736 * the loop to avoid copying the SKB once too much
2744 rx.sta = sta_info_get_bss(prev, hdr->addr2);
2746 ieee80211_prepare_and_rx_handle(&rx, skb, false);
2752 rx.sta = sta_info_get_bss(prev, hdr->addr2);
2755 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
2763 * This is the receive path handler. It is called by a low level driver when an
2764 * 802.11 MPDU is received from the hardware.
2766 void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
2768 struct ieee80211_local *local = hw_to_local(hw);
2769 struct ieee80211_rate *rate = NULL;
2770 struct ieee80211_supported_band *sband;
2771 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2773 WARN_ON_ONCE(softirq_count() == 0);
2775 if (WARN_ON(status->band < 0 ||
2776 status->band >= IEEE80211_NUM_BANDS))
2779 sband = local->hw.wiphy->bands[status->band];
2780 if (WARN_ON(!sband))
2784 * If we're suspending, it is possible although not too likely
2785 * that we'd be receiving frames after having already partially
2786 * quiesced the stack. We can't process such frames then since
2787 * that might, for example, cause stations to be added or other
2788 * driver callbacks be invoked.
2790 if (unlikely(local->quiescing || local->suspended))
2794 * The same happens when we're not even started,
2795 * but that's worth a warning.
2797 if (WARN_ON(!local->started))
2800 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
2802 * Validate the rate, unless a PLCP error means that
2803 * we probably can't have a valid rate here anyway.
2806 if (status->flag & RX_FLAG_HT) {
2808 * rate_idx is MCS index, which can be [0-76]
2811 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
2813 * Anything else would be some sort of driver or
2814 * hardware error. The driver should catch hardware
2817 if (WARN((status->rate_idx < 0 ||
2818 status->rate_idx > 76),
2819 "Rate marked as an HT rate but passed "
2820 "status->rate_idx is not "
2821 "an MCS index [0-76]: %d (0x%02x)\n",
2826 if (WARN_ON(status->rate_idx < 0 ||
2827 status->rate_idx >= sband->n_bitrates))
2829 rate = &sband->bitrates[status->rate_idx];
2833 status->rx_flags = 0;
2836 * key references and virtual interfaces are protected using RCU
2837 * and this requires that we are in a read-side RCU section during
2838 * receive processing
2843 * Frames with failed FCS/PLCP checksum are not returned,
2844 * all other frames are returned without radiotap header
2845 * if it was previously present.
2846 * Also, frames with less than 16 bytes are dropped.
2848 skb = ieee80211_rx_monitor(local, skb, rate);
2854 __ieee80211_rx_handle_packet(hw, skb);
2862 EXPORT_SYMBOL(ieee80211_rx);
2864 /* This is a version of the rx handler that can be called from hard irq
2865 * context. Post the skb on the queue and schedule the tasklet */
2866 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
2868 struct ieee80211_local *local = hw_to_local(hw);
2870 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2872 skb->pkt_type = IEEE80211_RX_MSG;
2873 skb_queue_tail(&local->skb_queue, skb);
2874 tasklet_schedule(&local->tasklet);
2876 EXPORT_SYMBOL(ieee80211_rx_irqsafe);