1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
7 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
9 * Copyright (C) 2018-2020 Intel Corporation
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 <linux/export.h>
20 #include <linux/bitops.h>
21 #include <net/mac80211.h>
22 #include <net/ieee80211_radiotap.h>
23 #include <asm/unaligned.h>
25 #include "ieee80211_i.h"
26 #include "driver-ops.h"
35 static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
37 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
39 u64_stats_update_begin(&tstats->syncp);
41 tstats->rx_bytes += len;
42 u64_stats_update_end(&tstats->syncp);
46 * monitor mode reception
48 * This function cleans up the SKB, i.e. it removes all the stuff
49 * only useful for monitoring.
51 static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
52 unsigned int present_fcs_len,
53 unsigned int rtap_space)
55 struct ieee80211_hdr *hdr;
60 __pskb_trim(skb, skb->len - present_fcs_len);
61 __pskb_pull(skb, rtap_space);
63 hdr = (void *)skb->data;
64 fc = hdr->frame_control;
67 * Remove the HT-Control field (if present) on management
68 * frames after we've sent the frame to monitoring. We
69 * (currently) don't need it, and don't properly parse
70 * frames with it present, due to the assumption of a
71 * fixed management header length.
73 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
76 hdrlen = ieee80211_hdrlen(fc);
77 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
79 if (!pskb_may_pull(skb, hdrlen)) {
84 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
85 hdrlen - IEEE80211_HT_CTL_LEN);
86 __pskb_pull(skb, IEEE80211_HT_CTL_LEN);
91 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
92 unsigned int rtap_space)
94 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
95 struct ieee80211_hdr *hdr;
97 hdr = (void *)(skb->data + rtap_space);
99 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
100 RX_FLAG_FAILED_PLCP_CRC |
101 RX_FLAG_ONLY_MONITOR |
105 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
108 if (ieee80211_is_ctl(hdr->frame_control) &&
109 !ieee80211_is_pspoll(hdr->frame_control) &&
110 !ieee80211_is_back_req(hdr->frame_control))
117 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
118 struct ieee80211_rx_status *status,
123 /* always present fields */
124 len = sizeof(struct ieee80211_radiotap_header) + 8;
126 /* allocate extra bitmaps */
128 len += 4 * hweight8(status->chains);
129 /* vendor presence bitmap */
130 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
133 if (ieee80211_have_rx_timestamp(status)) {
137 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
140 /* antenna field, if we don't have per-chain info */
144 /* padding for RX_FLAGS if necessary */
147 if (status->encoding == RX_ENC_HT) /* HT info */
150 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
155 if (status->encoding == RX_ENC_VHT) {
160 if (local->hw.radiotap_timestamp.units_pos >= 0) {
165 if (status->encoding == RX_ENC_HE &&
166 status->flag & RX_FLAG_RADIOTAP_HE) {
169 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
172 if (status->encoding == RX_ENC_HE &&
173 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
176 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
179 if (status->flag & RX_FLAG_NO_PSDU)
182 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
185 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
188 if (status->chains) {
189 /* antenna and antenna signal fields */
190 len += 2 * hweight8(status->chains);
193 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
194 struct ieee80211_vendor_radiotap *rtap;
195 int vendor_data_offset = 0;
198 * The position to look at depends on the existence (or non-
199 * existence) of other elements, so take that into account...
201 if (status->flag & RX_FLAG_RADIOTAP_HE)
202 vendor_data_offset +=
203 sizeof(struct ieee80211_radiotap_he);
204 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
205 vendor_data_offset +=
206 sizeof(struct ieee80211_radiotap_he_mu);
207 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
208 vendor_data_offset +=
209 sizeof(struct ieee80211_radiotap_lsig);
211 rtap = (void *)&skb->data[vendor_data_offset];
213 /* alignment for fixed 6-byte vendor data header */
215 /* vendor data header */
217 if (WARN_ON(rtap->align == 0))
219 len = ALIGN(len, rtap->align);
220 len += rtap->len + rtap->pad;
226 static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
231 struct ieee80211_hdr_3addr hdr;
234 } __packed __aligned(2) action;
239 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
241 if (skb->len < rtap_space + sizeof(action) +
242 VHT_MUMIMO_GROUPS_DATA_LEN)
245 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
248 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
250 if (!ieee80211_is_action(action.hdr.frame_control))
253 if (action.category != WLAN_CATEGORY_VHT)
256 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
259 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
262 skb = skb_copy(skb, GFP_ATOMIC);
266 skb_queue_tail(&sdata->skb_queue, skb);
267 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
271 * ieee80211_add_rx_radiotap_header - add radiotap header
273 * add a radiotap header containing all the fields which the hardware provided.
276 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
278 struct ieee80211_rate *rate,
279 int rtap_len, bool has_fcs)
281 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
282 struct ieee80211_radiotap_header *rthdr;
287 u16 channel_flags = 0;
289 unsigned long chains = status->chains;
290 struct ieee80211_vendor_radiotap rtap = {};
291 struct ieee80211_radiotap_he he = {};
292 struct ieee80211_radiotap_he_mu he_mu = {};
293 struct ieee80211_radiotap_lsig lsig = {};
295 if (status->flag & RX_FLAG_RADIOTAP_HE) {
296 he = *(struct ieee80211_radiotap_he *)skb->data;
297 skb_pull(skb, sizeof(he));
298 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
301 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
302 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
303 skb_pull(skb, sizeof(he_mu));
306 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
307 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
308 skb_pull(skb, sizeof(lsig));
311 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
312 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
313 /* rtap.len and rtap.pad are undone immediately */
314 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
318 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
321 rthdr = skb_push(skb, rtap_len);
322 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
323 it_present = &rthdr->it_present;
325 /* radiotap header, set always present flags */
326 rthdr->it_len = cpu_to_le16(rtap_len);
327 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
328 BIT(IEEE80211_RADIOTAP_CHANNEL) |
329 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
332 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
334 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
336 BIT(IEEE80211_RADIOTAP_EXT) |
337 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
338 put_unaligned_le32(it_present_val, it_present);
340 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
341 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
344 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
345 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
346 BIT(IEEE80211_RADIOTAP_EXT);
347 put_unaligned_le32(it_present_val, it_present);
349 it_present_val = rtap.present;
352 put_unaligned_le32(it_present_val, it_present);
354 pos = (void *)(it_present + 1);
356 /* the order of the following fields is important */
358 /* IEEE80211_RADIOTAP_TSFT */
359 if (ieee80211_have_rx_timestamp(status)) {
361 while ((pos - (u8 *)rthdr) & 7)
364 ieee80211_calculate_rx_timestamp(local, status,
367 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
371 /* IEEE80211_RADIOTAP_FLAGS */
372 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
373 *pos |= IEEE80211_RADIOTAP_F_FCS;
374 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
375 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
376 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
377 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
380 /* IEEE80211_RADIOTAP_RATE */
381 if (!rate || status->encoding != RX_ENC_LEGACY) {
383 * Without rate information don't add it. If we have,
384 * MCS information is a separate field in radiotap,
385 * added below. The byte here is needed as padding
386 * for the channel though, so initialise it to 0.
391 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
392 if (status->bw == RATE_INFO_BW_10)
394 else if (status->bw == RATE_INFO_BW_5)
396 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
400 /* IEEE80211_RADIOTAP_CHANNEL */
401 /* TODO: frequency offset in KHz */
402 put_unaligned_le16(status->freq, pos);
404 if (status->bw == RATE_INFO_BW_10)
405 channel_flags |= IEEE80211_CHAN_HALF;
406 else if (status->bw == RATE_INFO_BW_5)
407 channel_flags |= IEEE80211_CHAN_QUARTER;
409 if (status->band == NL80211_BAND_5GHZ ||
410 status->band == NL80211_BAND_6GHZ)
411 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
412 else if (status->encoding != RX_ENC_LEGACY)
413 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
414 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
415 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
417 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
419 channel_flags |= IEEE80211_CHAN_2GHZ;
420 put_unaligned_le16(channel_flags, pos);
423 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
424 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
425 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
426 *pos = status->signal;
428 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
432 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
434 if (!status->chains) {
435 /* IEEE80211_RADIOTAP_ANTENNA */
436 *pos = status->antenna;
440 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
442 /* IEEE80211_RADIOTAP_RX_FLAGS */
443 /* ensure 2 byte alignment for the 2 byte field as required */
444 if ((pos - (u8 *)rthdr) & 1)
446 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
447 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
448 put_unaligned_le16(rx_flags, pos);
451 if (status->encoding == RX_ENC_HT) {
454 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
455 *pos++ = local->hw.radiotap_mcs_details;
457 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
458 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
459 if (status->bw == RATE_INFO_BW_40)
460 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
461 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
462 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
463 if (status->enc_flags & RX_ENC_FLAG_LDPC)
464 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
465 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
466 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
468 *pos++ = status->rate_idx;
471 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
474 /* ensure 4 byte alignment */
475 while ((pos - (u8 *)rthdr) & 3)
478 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
479 put_unaligned_le32(status->ampdu_reference, pos);
481 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
482 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
483 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
484 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
485 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
486 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
487 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
488 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
489 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
490 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
491 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
492 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
493 put_unaligned_le16(flags, pos);
495 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
496 *pos++ = status->ampdu_delimiter_crc;
502 if (status->encoding == RX_ENC_VHT) {
503 u16 known = local->hw.radiotap_vht_details;
505 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
506 put_unaligned_le16(known, pos);
509 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
510 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
511 /* in VHT, STBC is binary */
512 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
513 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
514 if (status->enc_flags & RX_ENC_FLAG_BF)
515 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
518 switch (status->bw) {
519 case RATE_INFO_BW_80:
522 case RATE_INFO_BW_160:
525 case RATE_INFO_BW_40:
532 *pos = (status->rate_idx << 4) | status->nss;
535 if (status->enc_flags & RX_ENC_FLAG_LDPC)
536 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
544 if (local->hw.radiotap_timestamp.units_pos >= 0) {
546 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
549 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
551 /* ensure 8 byte alignment */
552 while ((pos - (u8 *)rthdr) & 7)
555 put_unaligned_le64(status->device_timestamp, pos);
558 if (local->hw.radiotap_timestamp.accuracy >= 0) {
559 accuracy = local->hw.radiotap_timestamp.accuracy;
560 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
562 put_unaligned_le16(accuracy, pos);
565 *pos++ = local->hw.radiotap_timestamp.units_pos;
569 if (status->encoding == RX_ENC_HE &&
570 status->flag & RX_FLAG_RADIOTAP_HE) {
571 #define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
573 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
574 he.data6 |= HE_PREP(DATA6_NSTS,
575 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
577 he.data3 |= HE_PREP(DATA3_STBC, 1);
579 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
582 #define CHECK_GI(s) \
583 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
584 (int)NL80211_RATE_INFO_HE_GI_##s)
590 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
591 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
592 he.data3 |= HE_PREP(DATA3_CODING,
593 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
595 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
597 switch (status->bw) {
598 case RATE_INFO_BW_20:
599 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
600 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
602 case RATE_INFO_BW_40:
603 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
604 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
606 case RATE_INFO_BW_80:
607 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
608 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
610 case RATE_INFO_BW_160:
611 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
612 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
614 case RATE_INFO_BW_HE_RU:
615 #define CHECK_RU_ALLOC(s) \
616 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
617 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
625 CHECK_RU_ALLOC(2x996);
627 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
631 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
634 /* ensure 2 byte alignment */
635 while ((pos - (u8 *)rthdr) & 1)
637 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
638 memcpy(pos, &he, sizeof(he));
642 if (status->encoding == RX_ENC_HE &&
643 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
644 /* ensure 2 byte alignment */
645 while ((pos - (u8 *)rthdr) & 1)
647 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
648 memcpy(pos, &he_mu, sizeof(he_mu));
649 pos += sizeof(he_mu);
652 if (status->flag & RX_FLAG_NO_PSDU) {
654 cpu_to_le32(1 << IEEE80211_RADIOTAP_ZERO_LEN_PSDU);
655 *pos++ = status->zero_length_psdu_type;
658 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
659 /* ensure 2 byte alignment */
660 while ((pos - (u8 *)rthdr) & 1)
662 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_LSIG);
663 memcpy(pos, &lsig, sizeof(lsig));
667 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
668 *pos++ = status->chain_signal[chain];
672 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
673 /* ensure 2 byte alignment for the vendor field as required */
674 if ((pos - (u8 *)rthdr) & 1)
676 *pos++ = rtap.oui[0];
677 *pos++ = rtap.oui[1];
678 *pos++ = rtap.oui[2];
680 put_unaligned_le16(rtap.len, pos);
682 /* align the actual payload as requested */
683 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
685 /* data (and possible padding) already follows */
689 static struct sk_buff *
690 ieee80211_make_monitor_skb(struct ieee80211_local *local,
691 struct sk_buff **origskb,
692 struct ieee80211_rate *rate,
693 int rtap_space, bool use_origskb)
695 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
696 int rt_hdrlen, needed_headroom;
699 /* room for the radiotap header based on driver features */
700 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
701 needed_headroom = rt_hdrlen - rtap_space;
704 /* only need to expand headroom if necessary */
709 * This shouldn't trigger often because most devices have an
710 * RX header they pull before we get here, and that should
711 * be big enough for our radiotap information. We should
712 * probably export the length to drivers so that we can have
713 * them allocate enough headroom to start with.
715 if (skb_headroom(skb) < needed_headroom &&
716 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
722 * Need to make a copy and possibly remove radiotap header
723 * and FCS from the original.
725 skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
731 /* prepend radiotap information */
732 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
734 skb_reset_mac_header(skb);
735 skb->ip_summed = CHECKSUM_UNNECESSARY;
736 skb->pkt_type = PACKET_OTHERHOST;
737 skb->protocol = htons(ETH_P_802_2);
743 * This function copies a received frame to all monitor interfaces and
744 * returns a cleaned-up SKB that no longer includes the FCS nor the
745 * radiotap header the driver might have added.
747 static struct sk_buff *
748 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
749 struct ieee80211_rate *rate)
751 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
752 struct ieee80211_sub_if_data *sdata;
753 struct sk_buff *monskb = NULL;
754 int present_fcs_len = 0;
755 unsigned int rtap_space = 0;
756 struct ieee80211_sub_if_data *monitor_sdata =
757 rcu_dereference(local->monitor_sdata);
758 bool only_monitor = false;
759 unsigned int min_head_len;
761 if (status->flag & RX_FLAG_RADIOTAP_HE)
762 rtap_space += sizeof(struct ieee80211_radiotap_he);
764 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
765 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
767 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
768 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
770 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
771 struct ieee80211_vendor_radiotap *rtap =
772 (void *)(origskb->data + rtap_space);
774 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
777 min_head_len = rtap_space;
780 * First, we may need to make a copy of the skb because
781 * (1) we need to modify it for radiotap (if not present), and
782 * (2) the other RX handlers will modify the skb we got.
784 * We don't need to, of course, if we aren't going to return
785 * the SKB because it has a bad FCS/PLCP checksum.
788 if (!(status->flag & RX_FLAG_NO_PSDU)) {
789 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
790 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
793 dev_kfree_skb(origskb);
796 present_fcs_len = FCS_LEN;
799 /* also consider the hdr->frame_control */
803 /* ensure that the expected data elements are in skb head */
804 if (!pskb_may_pull(origskb, min_head_len)) {
805 dev_kfree_skb(origskb);
809 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
811 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
813 dev_kfree_skb(origskb);
817 return ieee80211_clean_skb(origskb, present_fcs_len,
821 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
823 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
824 bool last_monitor = list_is_last(&sdata->u.mntr.list,
828 monskb = ieee80211_make_monitor_skb(local, &origskb,
840 skb = skb_clone(monskb, GFP_ATOMIC);
844 skb->dev = sdata->dev;
845 ieee80211_rx_stats(skb->dev, skb->len);
846 netif_receive_skb(skb);
854 /* this happens if last_monitor was erroneously false */
855 dev_kfree_skb(monskb);
861 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
864 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
866 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
867 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
868 int tid, seqno_idx, security_idx;
870 /* does the frame have a qos control field? */
871 if (ieee80211_is_data_qos(hdr->frame_control)) {
872 u8 *qc = ieee80211_get_qos_ctl(hdr);
873 /* frame has qos control */
874 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
875 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
876 status->rx_flags |= IEEE80211_RX_AMSDU;
882 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
884 * Sequence numbers for management frames, QoS data
885 * frames with a broadcast/multicast address in the
886 * Address 1 field, and all non-QoS data frames sent
887 * by QoS STAs are assigned using an additional single
888 * modulo-4096 counter, [...]
890 * We also use that counter for non-QoS STAs.
892 seqno_idx = IEEE80211_NUM_TIDS;
894 if (ieee80211_is_mgmt(hdr->frame_control))
895 security_idx = IEEE80211_NUM_TIDS;
899 rx->seqno_idx = seqno_idx;
900 rx->security_idx = security_idx;
901 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
902 * For now, set skb->priority to 0 for other cases. */
903 rx->skb->priority = (tid > 7) ? 0 : tid;
907 * DOC: Packet alignment
909 * Drivers always need to pass packets that are aligned to two-byte boundaries
912 * Additionally, should, if possible, align the payload data in a way that
913 * guarantees that the contained IP header is aligned to a four-byte
914 * boundary. In the case of regular frames, this simply means aligning the
915 * payload to a four-byte boundary (because either the IP header is directly
916 * contained, or IV/RFC1042 headers that have a length divisible by four are
917 * in front of it). If the payload data is not properly aligned and the
918 * architecture doesn't support efficient unaligned operations, mac80211
919 * will align the data.
921 * With A-MSDU frames, however, the payload data address must yield two modulo
922 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
923 * push the IP header further back to a multiple of four again. Thankfully, the
924 * specs were sane enough this time around to require padding each A-MSDU
925 * subframe to a length that is a multiple of four.
927 * Padding like Atheros hardware adds which is between the 802.11 header and
928 * the payload is not supported, the driver is required to move the 802.11
929 * header to be directly in front of the payload in that case.
931 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
933 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
934 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
941 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
943 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
945 if (is_multicast_ether_addr(hdr->addr1))
948 return ieee80211_is_robust_mgmt_frame(skb);
952 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
954 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
956 if (!is_multicast_ether_addr(hdr->addr1))
959 return ieee80211_is_robust_mgmt_frame(skb);
963 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
964 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
966 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
967 struct ieee80211_mmie *mmie;
968 struct ieee80211_mmie_16 *mmie16;
970 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
973 if (!ieee80211_is_robust_mgmt_frame(skb) &&
974 !ieee80211_is_beacon(hdr->frame_control))
975 return -1; /* not a robust management frame */
977 mmie = (struct ieee80211_mmie *)
978 (skb->data + skb->len - sizeof(*mmie));
979 if (mmie->element_id == WLAN_EID_MMIE &&
980 mmie->length == sizeof(*mmie) - 2)
981 return le16_to_cpu(mmie->key_id);
983 mmie16 = (struct ieee80211_mmie_16 *)
984 (skb->data + skb->len - sizeof(*mmie16));
985 if (skb->len >= 24 + sizeof(*mmie16) &&
986 mmie16->element_id == WLAN_EID_MMIE &&
987 mmie16->length == sizeof(*mmie16) - 2)
988 return le16_to_cpu(mmie16->key_id);
993 static int ieee80211_get_keyid(struct sk_buff *skb,
994 const struct ieee80211_cipher_scheme *cs)
996 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1004 fc = hdr->frame_control;
1005 hdrlen = ieee80211_hdrlen(fc);
1008 minlen = hdrlen + cs->hdr_len;
1009 key_idx_off = hdrlen + cs->key_idx_off;
1010 key_idx_shift = cs->key_idx_shift;
1012 /* WEP, TKIP, CCMP and GCMP */
1013 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1014 key_idx_off = hdrlen + 3;
1018 if (unlikely(skb->len < minlen))
1021 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1024 keyid &= cs->key_idx_mask;
1025 keyid >>= key_idx_shift;
1027 /* cs could use more than the usual two bits for the keyid */
1028 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1034 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
1036 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1037 char *dev_addr = rx->sdata->vif.addr;
1039 if (ieee80211_is_data(hdr->frame_control)) {
1040 if (is_multicast_ether_addr(hdr->addr1)) {
1041 if (ieee80211_has_tods(hdr->frame_control) ||
1042 !ieee80211_has_fromds(hdr->frame_control))
1043 return RX_DROP_MONITOR;
1044 if (ether_addr_equal(hdr->addr3, dev_addr))
1045 return RX_DROP_MONITOR;
1047 if (!ieee80211_has_a4(hdr->frame_control))
1048 return RX_DROP_MONITOR;
1049 if (ether_addr_equal(hdr->addr4, dev_addr))
1050 return RX_DROP_MONITOR;
1054 /* If there is not an established peer link and this is not a peer link
1055 * establisment frame, beacon or probe, drop the frame.
1058 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
1059 struct ieee80211_mgmt *mgmt;
1061 if (!ieee80211_is_mgmt(hdr->frame_control))
1062 return RX_DROP_MONITOR;
1064 if (ieee80211_is_action(hdr->frame_control)) {
1067 /* make sure category field is present */
1068 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1069 return RX_DROP_MONITOR;
1071 mgmt = (struct ieee80211_mgmt *)hdr;
1072 category = mgmt->u.action.category;
1073 if (category != WLAN_CATEGORY_MESH_ACTION &&
1074 category != WLAN_CATEGORY_SELF_PROTECTED)
1075 return RX_DROP_MONITOR;
1079 if (ieee80211_is_probe_req(hdr->frame_control) ||
1080 ieee80211_is_probe_resp(hdr->frame_control) ||
1081 ieee80211_is_beacon(hdr->frame_control) ||
1082 ieee80211_is_auth(hdr->frame_control))
1085 return RX_DROP_MONITOR;
1091 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1094 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1095 struct sk_buff *tail = skb_peek_tail(frames);
1096 struct ieee80211_rx_status *status;
1098 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1104 status = IEEE80211_SKB_RXCB(tail);
1105 if (status->flag & RX_FLAG_AMSDU_MORE)
1111 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1112 struct tid_ampdu_rx *tid_agg_rx,
1114 struct sk_buff_head *frames)
1116 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1117 struct sk_buff *skb;
1118 struct ieee80211_rx_status *status;
1120 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1122 if (skb_queue_empty(skb_list))
1125 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1126 __skb_queue_purge(skb_list);
1130 /* release frames from the reorder ring buffer */
1131 tid_agg_rx->stored_mpdu_num--;
1132 while ((skb = __skb_dequeue(skb_list))) {
1133 status = IEEE80211_SKB_RXCB(skb);
1134 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1135 __skb_queue_tail(frames, skb);
1139 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
1140 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1143 static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
1144 struct tid_ampdu_rx *tid_agg_rx,
1146 struct sk_buff_head *frames)
1150 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1152 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1153 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1154 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1160 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1161 * the skb was added to the buffer longer than this time ago, the earlier
1162 * frames that have not yet been received are assumed to be lost and the skb
1163 * can be released for processing. This may also release other skb's from the
1164 * reorder buffer if there are no additional gaps between the frames.
1166 * Callers must hold tid_agg_rx->reorder_lock.
1168 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1170 static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
1171 struct tid_ampdu_rx *tid_agg_rx,
1172 struct sk_buff_head *frames)
1176 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1178 /* release the buffer until next missing frame */
1179 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1180 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
1181 tid_agg_rx->stored_mpdu_num) {
1183 * No buffers ready to be released, but check whether any
1184 * frames in the reorder buffer have timed out.
1187 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1188 j = (j + 1) % tid_agg_rx->buf_size) {
1189 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
1194 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
1195 HT_RX_REORDER_BUF_TIMEOUT))
1196 goto set_release_timer;
1198 /* don't leave incomplete A-MSDUs around */
1199 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1200 i = (i + 1) % tid_agg_rx->buf_size)
1201 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1203 ht_dbg_ratelimited(sdata,
1204 "release an RX reorder frame due to timeout on earlier frames\n");
1205 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1209 * Increment the head seq# also for the skipped slots.
1211 tid_agg_rx->head_seq_num =
1212 (tid_agg_rx->head_seq_num +
1213 skipped) & IEEE80211_SN_MASK;
1216 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1217 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1219 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1222 if (tid_agg_rx->stored_mpdu_num) {
1223 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1225 for (; j != (index - 1) % tid_agg_rx->buf_size;
1226 j = (j + 1) % tid_agg_rx->buf_size) {
1227 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
1233 if (!tid_agg_rx->removed)
1234 mod_timer(&tid_agg_rx->reorder_timer,
1235 tid_agg_rx->reorder_time[j] + 1 +
1236 HT_RX_REORDER_BUF_TIMEOUT);
1238 del_timer(&tid_agg_rx->reorder_timer);
1243 * As this function belongs to the RX path it must be under
1244 * rcu_read_lock protection. It returns false if the frame
1245 * can be processed immediately, true if it was consumed.
1247 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1248 struct tid_ampdu_rx *tid_agg_rx,
1249 struct sk_buff *skb,
1250 struct sk_buff_head *frames)
1252 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1253 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1254 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1255 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1256 u16 head_seq_num, buf_size;
1260 spin_lock(&tid_agg_rx->reorder_lock);
1263 * Offloaded BA sessions have no known starting sequence number so pick
1264 * one from first Rxed frame for this tid after BA was started.
1266 if (unlikely(tid_agg_rx->auto_seq)) {
1267 tid_agg_rx->auto_seq = false;
1268 tid_agg_rx->ssn = mpdu_seq_num;
1269 tid_agg_rx->head_seq_num = mpdu_seq_num;
1272 buf_size = tid_agg_rx->buf_size;
1273 head_seq_num = tid_agg_rx->head_seq_num;
1276 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1279 if (unlikely(!tid_agg_rx->started)) {
1280 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1284 tid_agg_rx->started = true;
1287 /* frame with out of date sequence number */
1288 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1294 * If frame the sequence number exceeds our buffering window
1295 * size release some previous frames to make room for this one.
1297 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1298 head_seq_num = ieee80211_sn_inc(
1299 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1300 /* release stored frames up to new head to stack */
1301 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
1302 head_seq_num, frames);
1305 /* Now the new frame is always in the range of the reordering buffer */
1307 index = mpdu_seq_num % tid_agg_rx->buf_size;
1309 /* check if we already stored this frame */
1310 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1316 * If the current MPDU is in the right order and nothing else
1317 * is stored we can process it directly, no need to buffer it.
1318 * If it is first but there's something stored, we may be able
1319 * to release frames after this one.
1321 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1322 tid_agg_rx->stored_mpdu_num == 0) {
1323 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1324 tid_agg_rx->head_seq_num =
1325 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1330 /* put the frame in the reordering buffer */
1331 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1332 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1333 tid_agg_rx->reorder_time[index] = jiffies;
1334 tid_agg_rx->stored_mpdu_num++;
1335 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1339 spin_unlock(&tid_agg_rx->reorder_lock);
1344 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1345 * true if the MPDU was buffered, false if it should be processed.
1347 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1348 struct sk_buff_head *frames)
1350 struct sk_buff *skb = rx->skb;
1351 struct ieee80211_local *local = rx->local;
1352 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1353 struct sta_info *sta = rx->sta;
1354 struct tid_ampdu_rx *tid_agg_rx;
1358 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1359 is_multicast_ether_addr(hdr->addr1))
1363 * filter the QoS data rx stream according to
1364 * STA/TID and check if this STA/TID is on aggregation
1370 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1371 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1372 tid = ieee80211_get_tid(hdr);
1374 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1376 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1377 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1378 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1379 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1380 WLAN_BACK_RECIPIENT,
1381 WLAN_REASON_QSTA_REQUIRE_SETUP);
1385 /* qos null data frames are excluded */
1386 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1389 /* not part of a BA session */
1390 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1391 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1394 /* new, potentially un-ordered, ampdu frame - process it */
1396 /* reset session timer */
1397 if (tid_agg_rx->timeout)
1398 tid_agg_rx->last_rx = jiffies;
1400 /* if this mpdu is fragmented - terminate rx aggregation session */
1401 sc = le16_to_cpu(hdr->seq_ctrl);
1402 if (sc & IEEE80211_SCTL_FRAG) {
1403 skb_queue_tail(&rx->sdata->skb_queue, skb);
1404 ieee80211_queue_work(&local->hw, &rx->sdata->work);
1409 * No locking needed -- we will only ever process one
1410 * RX packet at a time, and thus own tid_agg_rx. All
1411 * other code manipulating it needs to (and does) make
1412 * sure that we cannot get to it any more before doing
1415 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1420 __skb_queue_tail(frames, skb);
1423 static ieee80211_rx_result debug_noinline
1424 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1426 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1427 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1429 if (status->flag & RX_FLAG_DUP_VALIDATED)
1433 * Drop duplicate 802.11 retransmissions
1434 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1437 if (rx->skb->len < 24)
1440 if (ieee80211_is_ctl(hdr->frame_control) ||
1441 ieee80211_is_any_nullfunc(hdr->frame_control) ||
1442 is_multicast_ether_addr(hdr->addr1))
1448 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1449 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1450 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1451 rx->sta->rx_stats.num_duplicates++;
1452 return RX_DROP_UNUSABLE;
1453 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1454 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1460 static ieee80211_rx_result debug_noinline
1461 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1463 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1465 /* Drop disallowed frame classes based on STA auth/assoc state;
1466 * IEEE 802.11, Chap 5.5.
1468 * mac80211 filters only based on association state, i.e. it drops
1469 * Class 3 frames from not associated stations. hostapd sends
1470 * deauth/disassoc frames when needed. In addition, hostapd is
1471 * responsible for filtering on both auth and assoc states.
1474 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1475 return ieee80211_rx_mesh_check(rx);
1477 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1478 ieee80211_is_pspoll(hdr->frame_control)) &&
1479 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1480 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
1481 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1482 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1484 * accept port control frames from the AP even when it's not
1485 * yet marked ASSOC to prevent a race where we don't set the
1486 * assoc bit quickly enough before it sends the first frame
1488 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1489 ieee80211_is_data_present(hdr->frame_control)) {
1490 unsigned int hdrlen;
1493 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1495 if (rx->skb->len < hdrlen + 8)
1496 return RX_DROP_MONITOR;
1498 skb_copy_bits(rx->skb, hdrlen + 6, ðertype, 2);
1499 if (ethertype == rx->sdata->control_port_protocol)
1503 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1504 cfg80211_rx_spurious_frame(rx->sdata->dev,
1507 return RX_DROP_UNUSABLE;
1509 return RX_DROP_MONITOR;
1516 static ieee80211_rx_result debug_noinline
1517 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1519 struct ieee80211_local *local;
1520 struct ieee80211_hdr *hdr;
1521 struct sk_buff *skb;
1525 hdr = (struct ieee80211_hdr *) skb->data;
1527 if (!local->pspolling)
1530 if (!ieee80211_has_fromds(hdr->frame_control))
1531 /* this is not from AP */
1534 if (!ieee80211_is_data(hdr->frame_control))
1537 if (!ieee80211_has_moredata(hdr->frame_control)) {
1538 /* AP has no more frames buffered for us */
1539 local->pspolling = false;
1543 /* more data bit is set, let's request a new frame from the AP */
1544 ieee80211_send_pspoll(local, rx->sdata);
1549 static void sta_ps_start(struct sta_info *sta)
1551 struct ieee80211_sub_if_data *sdata = sta->sdata;
1552 struct ieee80211_local *local = sdata->local;
1556 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1557 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1558 ps = &sdata->bss->ps;
1562 atomic_inc(&ps->num_sta_ps);
1563 set_sta_flag(sta, WLAN_STA_PS_STA);
1564 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1565 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1566 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1567 sta->sta.addr, sta->sta.aid);
1569 ieee80211_clear_fast_xmit(sta);
1571 if (!sta->sta.txq[0])
1574 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
1575 struct ieee80211_txq *txq = sta->sta.txq[tid];
1576 struct txq_info *txqi = to_txq_info(txq);
1578 spin_lock(&local->active_txq_lock[txq->ac]);
1579 if (!list_empty(&txqi->schedule_order))
1580 list_del_init(&txqi->schedule_order);
1581 spin_unlock(&local->active_txq_lock[txq->ac]);
1583 if (txq_has_queue(txq))
1584 set_bit(tid, &sta->txq_buffered_tids);
1586 clear_bit(tid, &sta->txq_buffered_tids);
1590 static void sta_ps_end(struct sta_info *sta)
1592 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1593 sta->sta.addr, sta->sta.aid);
1595 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1597 * Clear the flag only if the other one is still set
1598 * so that the TX path won't start TX'ing new frames
1599 * directly ... In the case that the driver flag isn't
1600 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1602 clear_sta_flag(sta, WLAN_STA_PS_STA);
1603 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1604 sta->sta.addr, sta->sta.aid);
1608 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1609 clear_sta_flag(sta, WLAN_STA_PS_STA);
1610 ieee80211_sta_ps_deliver_wakeup(sta);
1613 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1615 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1618 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1620 /* Don't let the same PS state be set twice */
1621 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1622 if ((start && in_ps) || (!start && !in_ps))
1632 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1634 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1636 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1638 if (test_sta_flag(sta, WLAN_STA_SP))
1641 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1642 ieee80211_sta_ps_deliver_poll_response(sta);
1644 set_sta_flag(sta, WLAN_STA_PSPOLL);
1646 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1648 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1650 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1651 int ac = ieee80211_ac_from_tid(tid);
1654 * If this AC is not trigger-enabled do nothing unless the
1655 * driver is calling us after it already checked.
1657 * NB: This could/should check a separate bitmap of trigger-
1658 * enabled queues, but for now we only implement uAPSD w/o
1659 * TSPEC changes to the ACs, so they're always the same.
1661 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1662 tid != IEEE80211_NUM_TIDS)
1665 /* if we are in a service period, do nothing */
1666 if (test_sta_flag(sta, WLAN_STA_SP))
1669 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1670 ieee80211_sta_ps_deliver_uapsd(sta);
1672 set_sta_flag(sta, WLAN_STA_UAPSD);
1674 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1676 static ieee80211_rx_result debug_noinline
1677 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1679 struct ieee80211_sub_if_data *sdata = rx->sdata;
1680 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1681 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1686 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1687 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1691 * The device handles station powersave, so don't do anything about
1692 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1693 * it to mac80211 since they're handled.)
1695 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1699 * Don't do anything if the station isn't already asleep. In
1700 * the uAPSD case, the station will probably be marked asleep,
1701 * in the PS-Poll case the station must be confused ...
1703 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1706 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1707 ieee80211_sta_pspoll(&rx->sta->sta);
1709 /* Free PS Poll skb here instead of returning RX_DROP that would
1710 * count as an dropped frame. */
1711 dev_kfree_skb(rx->skb);
1714 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1715 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1716 ieee80211_has_pm(hdr->frame_control) &&
1717 (ieee80211_is_data_qos(hdr->frame_control) ||
1718 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1719 u8 tid = ieee80211_get_tid(hdr);
1721 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1727 static ieee80211_rx_result debug_noinline
1728 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1730 struct sta_info *sta = rx->sta;
1731 struct sk_buff *skb = rx->skb;
1732 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1733 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1740 * Update last_rx only for IBSS packets which are for the current
1741 * BSSID and for station already AUTHORIZED to avoid keeping the
1742 * current IBSS network alive in cases where other STAs start
1743 * using different BSSID. This will also give the station another
1744 * chance to restart the authentication/authorization in case
1745 * something went wrong the first time.
1747 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1748 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1749 NL80211_IFTYPE_ADHOC);
1750 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1751 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1752 sta->rx_stats.last_rx = jiffies;
1753 if (ieee80211_is_data(hdr->frame_control) &&
1754 !is_multicast_ether_addr(hdr->addr1))
1755 sta->rx_stats.last_rate =
1756 sta_stats_encode_rate(status);
1758 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1759 sta->rx_stats.last_rx = jiffies;
1760 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
1761 is_multicast_ether_addr(hdr->addr1)) {
1763 * Mesh beacons will update last_rx when if they are found to
1764 * match the current local configuration when processed.
1766 sta->rx_stats.last_rx = jiffies;
1767 if (ieee80211_is_data(hdr->frame_control))
1768 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1771 sta->rx_stats.fragments++;
1773 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1774 sta->rx_stats.bytes += rx->skb->len;
1775 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1777 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1778 sta->rx_stats.last_signal = status->signal;
1779 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1782 if (status->chains) {
1783 sta->rx_stats.chains = status->chains;
1784 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1785 int signal = status->chain_signal[i];
1787 if (!(status->chains & BIT(i)))
1790 sta->rx_stats.chain_signal_last[i] = signal;
1791 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1796 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1800 * Change STA power saving mode only at the end of a frame
1801 * exchange sequence, and only for a data or management
1802 * frame as specified in IEEE 802.11-2016 11.2.3.2
1804 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1805 !ieee80211_has_morefrags(hdr->frame_control) &&
1806 !is_multicast_ether_addr(hdr->addr1) &&
1807 (ieee80211_is_mgmt(hdr->frame_control) ||
1808 ieee80211_is_data(hdr->frame_control)) &&
1809 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1810 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1811 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1812 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1813 if (!ieee80211_has_pm(hdr->frame_control))
1816 if (ieee80211_has_pm(hdr->frame_control))
1821 /* mesh power save support */
1822 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1823 ieee80211_mps_rx_h_sta_process(sta, hdr);
1826 * Drop (qos-)data::nullfunc frames silently, since they
1827 * are used only to control station power saving mode.
1829 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1830 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1833 * If we receive a 4-addr nullfunc frame from a STA
1834 * that was not moved to a 4-addr STA vlan yet send
1835 * the event to userspace and for older hostapd drop
1836 * the frame to the monitor interface.
1838 if (ieee80211_has_a4(hdr->frame_control) &&
1839 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1840 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1841 !rx->sdata->u.vlan.sta))) {
1842 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1843 cfg80211_rx_unexpected_4addr_frame(
1844 rx->sdata->dev, sta->sta.addr,
1846 return RX_DROP_MONITOR;
1849 * Update counter and free packet here to avoid
1850 * counting this as a dropped packed.
1852 sta->rx_stats.packets++;
1853 dev_kfree_skb(rx->skb);
1858 } /* ieee80211_rx_h_sta_process */
1860 static struct ieee80211_key *
1861 ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1863 struct ieee80211_key *key = NULL;
1864 struct ieee80211_sub_if_data *sdata = rx->sdata;
1867 /* Make sure key gets set if either BIGTK key index is set so that
1868 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1869 * Beacon frames and Beacon frames that claim to use another BIGTK key
1870 * index (i.e., a key that we do not have).
1874 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1877 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1884 key = rcu_dereference(rx->sta->gtk[idx]);
1886 key = rcu_dereference(sdata->keys[idx]);
1887 if (!key && rx->sta)
1888 key = rcu_dereference(rx->sta->gtk[idx2]);
1890 key = rcu_dereference(sdata->keys[idx2]);
1895 static ieee80211_rx_result debug_noinline
1896 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1898 struct sk_buff *skb = rx->skb;
1899 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1900 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1902 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1903 struct ieee80211_key *sta_ptk = NULL;
1904 struct ieee80211_key *ptk_idx = NULL;
1905 int mmie_keyidx = -1;
1907 const struct ieee80211_cipher_scheme *cs = NULL;
1909 if (ieee80211_is_ext(hdr->frame_control))
1915 * There are five types of keys:
1916 * - GTK (group keys)
1917 * - IGTK (group keys for management frames)
1918 * - BIGTK (group keys for Beacon frames)
1919 * - PTK (pairwise keys)
1920 * - STK (station-to-station pairwise keys)
1922 * When selecting a key, we have to distinguish between multicast
1923 * (including broadcast) and unicast frames, the latter can only
1924 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1925 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1926 * then unicast frames can also use key indices like GTKs. Hence, if we
1927 * don't have a PTK/STK we check the key index for a WEP key.
1929 * Note that in a regular BSS, multicast frames are sent by the
1930 * AP only, associated stations unicast the frame to the AP first
1931 * which then multicasts it on their behalf.
1933 * There is also a slight problem in IBSS mode: GTKs are negotiated
1934 * with each station, that is something we don't currently handle.
1935 * The spec seems to expect that one negotiates the same key with
1936 * every station but there's no such requirement; VLANs could be
1940 /* start without a key */
1942 fc = hdr->frame_control;
1945 int keyid = rx->sta->ptk_idx;
1946 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1948 if (ieee80211_has_protected(fc)) {
1949 cs = rx->sta->cipher_scheme;
1950 keyid = ieee80211_get_keyid(rx->skb, cs);
1952 if (unlikely(keyid < 0))
1953 return RX_DROP_UNUSABLE;
1955 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
1959 if (!ieee80211_has_protected(fc))
1960 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1962 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1963 rx->key = ptk_idx ? ptk_idx : sta_ptk;
1964 if ((status->flag & RX_FLAG_DECRYPTED) &&
1965 (status->flag & RX_FLAG_IV_STRIPPED))
1967 /* Skip decryption if the frame is not protected. */
1968 if (!ieee80211_has_protected(fc))
1970 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1971 /* Broadcast/multicast robust management frame / BIP */
1972 if ((status->flag & RX_FLAG_DECRYPTED) &&
1973 (status->flag & RX_FLAG_IV_STRIPPED))
1976 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1977 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
1978 NUM_DEFAULT_BEACON_KEYS) {
1979 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1982 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1985 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1987 return RX_CONTINUE; /* Beacon protection not in use */
1988 } else if (mmie_keyidx >= 0) {
1989 /* Broadcast/multicast robust management frame / BIP */
1990 if ((status->flag & RX_FLAG_DECRYPTED) &&
1991 (status->flag & RX_FLAG_IV_STRIPPED))
1994 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1995 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1996 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1998 if (ieee80211_is_group_privacy_action(skb) &&
1999 test_sta_flag(rx->sta, WLAN_STA_MFP))
2000 return RX_DROP_MONITOR;
2002 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
2005 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
2006 } else if (!ieee80211_has_protected(fc)) {
2008 * The frame was not protected, so skip decryption. However, we
2009 * need to set rx->key if there is a key that could have been
2010 * used so that the frame may be dropped if encryption would
2011 * have been expected.
2013 struct ieee80211_key *key = NULL;
2014 struct ieee80211_sub_if_data *sdata = rx->sdata;
2017 if (ieee80211_is_beacon(fc)) {
2018 key = ieee80211_rx_get_bigtk(rx, -1);
2019 } else if (ieee80211_is_mgmt(fc) &&
2020 is_multicast_ether_addr(hdr->addr1)) {
2021 key = rcu_dereference(rx->sdata->default_mgmt_key);
2024 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2025 key = rcu_dereference(rx->sta->gtk[i]);
2031 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2032 key = rcu_dereference(sdata->keys[i]);
2043 * The device doesn't give us the IV so we won't be
2044 * able to look up the key. That's ok though, we
2045 * don't need to decrypt the frame, we just won't
2046 * be able to keep statistics accurate.
2047 * Except for key threshold notifications, should
2048 * we somehow allow the driver to tell us which key
2049 * the hardware used if this flag is set?
2051 if ((status->flag & RX_FLAG_DECRYPTED) &&
2052 (status->flag & RX_FLAG_IV_STRIPPED))
2055 keyidx = ieee80211_get_keyid(rx->skb, cs);
2057 if (unlikely(keyidx < 0))
2058 return RX_DROP_UNUSABLE;
2060 /* check per-station GTK first, if multicast packet */
2061 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
2062 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
2064 /* if not found, try default key */
2066 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2069 * RSNA-protected unicast frames should always be
2070 * sent with pairwise or station-to-station keys,
2071 * but for WEP we allow using a key index as well.
2074 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2075 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2076 !is_multicast_ether_addr(hdr->addr1))
2082 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2083 return RX_DROP_MONITOR;
2085 /* TODO: add threshold stuff again */
2087 return RX_DROP_MONITOR;
2090 switch (rx->key->conf.cipher) {
2091 case WLAN_CIPHER_SUITE_WEP40:
2092 case WLAN_CIPHER_SUITE_WEP104:
2093 result = ieee80211_crypto_wep_decrypt(rx);
2095 case WLAN_CIPHER_SUITE_TKIP:
2096 result = ieee80211_crypto_tkip_decrypt(rx);
2098 case WLAN_CIPHER_SUITE_CCMP:
2099 result = ieee80211_crypto_ccmp_decrypt(
2100 rx, IEEE80211_CCMP_MIC_LEN);
2102 case WLAN_CIPHER_SUITE_CCMP_256:
2103 result = ieee80211_crypto_ccmp_decrypt(
2104 rx, IEEE80211_CCMP_256_MIC_LEN);
2106 case WLAN_CIPHER_SUITE_AES_CMAC:
2107 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2109 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2110 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2112 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2113 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2114 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2116 case WLAN_CIPHER_SUITE_GCMP:
2117 case WLAN_CIPHER_SUITE_GCMP_256:
2118 result = ieee80211_crypto_gcmp_decrypt(rx);
2121 result = ieee80211_crypto_hw_decrypt(rx);
2124 /* the hdr variable is invalid after the decrypt handlers */
2126 /* either the frame has been decrypted or will be dropped */
2127 status->flag |= RX_FLAG_DECRYPTED;
2129 if (unlikely(ieee80211_is_beacon(fc) && result == RX_DROP_UNUSABLE))
2130 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2131 skb->data, skb->len);
2136 static inline struct ieee80211_fragment_entry *
2137 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
2138 unsigned int frag, unsigned int seq, int rx_queue,
2139 struct sk_buff **skb)
2141 struct ieee80211_fragment_entry *entry;
2143 entry = &sdata->fragments[sdata->fragment_next++];
2144 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
2145 sdata->fragment_next = 0;
2147 if (!skb_queue_empty(&entry->skb_list))
2148 __skb_queue_purge(&entry->skb_list);
2150 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2152 entry->first_frag_time = jiffies;
2154 entry->rx_queue = rx_queue;
2155 entry->last_frag = frag;
2156 entry->check_sequential_pn = false;
2157 entry->extra_len = 0;
2162 static inline struct ieee80211_fragment_entry *
2163 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
2164 unsigned int frag, unsigned int seq,
2165 int rx_queue, struct ieee80211_hdr *hdr)
2167 struct ieee80211_fragment_entry *entry;
2170 idx = sdata->fragment_next;
2171 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2172 struct ieee80211_hdr *f_hdr;
2173 struct sk_buff *f_skb;
2177 idx = IEEE80211_FRAGMENT_MAX - 1;
2179 entry = &sdata->fragments[idx];
2180 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2181 entry->rx_queue != rx_queue ||
2182 entry->last_frag + 1 != frag)
2185 f_skb = __skb_peek(&entry->skb_list);
2186 f_hdr = (struct ieee80211_hdr *) f_skb->data;
2189 * Check ftype and addresses are equal, else check next fragment
2191 if (((hdr->frame_control ^ f_hdr->frame_control) &
2192 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
2193 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2194 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
2197 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
2198 __skb_queue_purge(&entry->skb_list);
2207 static ieee80211_rx_result debug_noinline
2208 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
2210 struct ieee80211_hdr *hdr;
2213 unsigned int frag, seq;
2214 struct ieee80211_fragment_entry *entry;
2215 struct sk_buff *skb;
2217 hdr = (struct ieee80211_hdr *)rx->skb->data;
2218 fc = hdr->frame_control;
2220 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
2223 sc = le16_to_cpu(hdr->seq_ctrl);
2224 frag = sc & IEEE80211_SCTL_FRAG;
2226 if (is_multicast_ether_addr(hdr->addr1)) {
2227 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
2231 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2234 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2236 if (skb_linearize(rx->skb))
2237 return RX_DROP_UNUSABLE;
2240 * skb_linearize() might change the skb->data and
2241 * previously cached variables (in this case, hdr) need to
2242 * be refreshed with the new data.
2244 hdr = (struct ieee80211_hdr *)rx->skb->data;
2245 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2248 /* This is the first fragment of a new frame. */
2249 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
2250 rx->seqno_idx, &(rx->skb));
2252 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2253 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2254 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2255 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2256 ieee80211_has_protected(fc)) {
2257 int queue = rx->security_idx;
2259 /* Store CCMP/GCMP PN so that we can verify that the
2260 * next fragment has a sequential PN value.
2262 entry->check_sequential_pn = true;
2263 memcpy(entry->last_pn,
2264 rx->key->u.ccmp.rx_pn[queue],
2265 IEEE80211_CCMP_PN_LEN);
2266 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2268 offsetof(struct ieee80211_key,
2270 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2271 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2272 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2273 IEEE80211_GCMP_PN_LEN);
2278 /* This is a fragment for a frame that should already be pending in
2279 * fragment cache. Add this fragment to the end of the pending entry.
2281 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
2282 rx->seqno_idx, hdr);
2284 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2285 return RX_DROP_MONITOR;
2288 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2289 * MPDU PN values are not incrementing in steps of 1."
2290 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2291 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2293 if (entry->check_sequential_pn) {
2295 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2299 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
2300 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
2301 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
2302 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
2303 return RX_DROP_UNUSABLE;
2304 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2305 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2310 queue = rx->security_idx;
2311 rpn = rx->key->u.ccmp.rx_pn[queue];
2312 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2313 return RX_DROP_UNUSABLE;
2314 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2317 skb_pull(rx->skb, ieee80211_hdrlen(fc));
2318 __skb_queue_tail(&entry->skb_list, rx->skb);
2319 entry->last_frag = frag;
2320 entry->extra_len += rx->skb->len;
2321 if (ieee80211_has_morefrags(fc)) {
2326 rx->skb = __skb_dequeue(&entry->skb_list);
2327 if (skb_tailroom(rx->skb) < entry->extra_len) {
2328 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2329 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2331 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2332 __skb_queue_purge(&entry->skb_list);
2333 return RX_DROP_UNUSABLE;
2336 while ((skb = __skb_dequeue(&entry->skb_list))) {
2337 skb_put_data(rx->skb, skb->data, skb->len);
2342 ieee80211_led_rx(rx->local);
2345 rx->sta->rx_stats.packets++;
2349 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2351 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2357 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2359 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
2360 struct sk_buff *skb = rx->skb;
2361 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2364 * Pass through unencrypted frames if the hardware has
2365 * decrypted them already.
2367 if (status->flag & RX_FLAG_DECRYPTED)
2370 /* check mesh EAPOL frames first */
2371 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2372 ieee80211_is_data(fc))) {
2373 struct ieee80211s_hdr *mesh_hdr;
2374 u16 hdr_len = ieee80211_hdrlen(fc);
2375 u16 ethertype_offset;
2378 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2381 /* make sure fixed part of mesh header is there, also checks skb len */
2382 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2385 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2386 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2387 sizeof(rfc1042_header);
2389 if (skb_copy_bits(rx->skb, ethertype_offset, ðertype, 2) == 0 &&
2390 ethertype == rx->sdata->control_port_protocol)
2395 /* Drop unencrypted frames if key is set. */
2396 if (unlikely(!ieee80211_has_protected(fc) &&
2397 !ieee80211_is_any_nullfunc(fc) &&
2398 ieee80211_is_data(fc) && rx->key))
2404 static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2406 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2407 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2408 __le16 fc = hdr->frame_control;
2411 * Pass through unencrypted frames if the hardware has
2412 * decrypted them already.
2414 if (status->flag & RX_FLAG_DECRYPTED)
2417 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2418 if (unlikely(!ieee80211_has_protected(fc) &&
2419 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2421 if (ieee80211_is_deauth(fc) ||
2422 ieee80211_is_disassoc(fc))
2423 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2428 /* BIP does not use Protected field, so need to check MMIE */
2429 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2430 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2431 if (ieee80211_is_deauth(fc) ||
2432 ieee80211_is_disassoc(fc))
2433 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2438 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
2439 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2440 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2446 * When using MFP, Action frames are not allowed prior to
2447 * having configured keys.
2449 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2450 ieee80211_is_robust_mgmt_frame(rx->skb)))
2458 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2460 struct ieee80211_sub_if_data *sdata = rx->sdata;
2461 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2462 bool check_port_control = false;
2463 struct ethhdr *ehdr;
2466 *port_control = false;
2467 if (ieee80211_has_a4(hdr->frame_control) &&
2468 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2471 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2472 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2474 if (!sdata->u.mgd.use_4addr)
2476 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
2477 check_port_control = true;
2480 if (is_multicast_ether_addr(hdr->addr1) &&
2481 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2484 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2488 ehdr = (struct ethhdr *) rx->skb->data;
2489 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2490 *port_control = true;
2491 else if (check_port_control)
2498 * requires that rx->skb is a frame with ethernet header
2500 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2502 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2503 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2504 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2507 * Allow EAPOL frames to us/the PAE group address regardless
2508 * of whether the frame was encrypted or not.
2510 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
2511 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2512 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
2515 if (ieee80211_802_1x_port_control(rx) ||
2516 ieee80211_drop_unencrypted(rx, fc))
2522 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2523 struct ieee80211_rx_data *rx)
2525 struct ieee80211_sub_if_data *sdata = rx->sdata;
2526 struct net_device *dev = sdata->dev;
2528 if (unlikely((skb->protocol == sdata->control_port_protocol ||
2529 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2530 !sdata->control_port_no_preauth)) &&
2531 sdata->control_port_over_nl80211)) {
2532 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2533 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
2535 cfg80211_rx_control_port(dev, skb, noencrypt);
2538 memset(skb->cb, 0, sizeof(skb->cb));
2540 /* deliver to local stack */
2542 list_add_tail(&skb->list, rx->list);
2544 netif_receive_skb(skb);
2549 * requires that rx->skb is a frame with ethernet header
2552 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2554 struct ieee80211_sub_if_data *sdata = rx->sdata;
2555 struct net_device *dev = sdata->dev;
2556 struct sk_buff *skb, *xmit_skb;
2557 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2558 struct sta_info *dsta;
2563 ieee80211_rx_stats(dev, skb->len);
2566 /* The seqno index has the same property as needed
2567 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2568 * for non-QoS-data frames. Here we know it's a data
2569 * frame, so count MSDUs.
2571 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2572 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2573 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2576 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2577 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2578 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2579 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2580 if (is_multicast_ether_addr(ehdr->h_dest) &&
2581 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2583 * send multicast frames both to higher layers in
2584 * local net stack and back to the wireless medium
2586 xmit_skb = skb_copy(skb, GFP_ATOMIC);
2588 net_info_ratelimited("%s: failed to clone multicast frame\n",
2590 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2591 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2592 dsta = sta_info_get(sdata, ehdr->h_dest);
2595 * The destination station is associated to
2596 * this AP (in this VLAN), so send the frame
2597 * directly to it and do not pass it to local
2606 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2608 /* 'align' will only take the values 0 or 2 here since all
2609 * frames are required to be aligned to 2-byte boundaries
2610 * when being passed to mac80211; the code here works just
2611 * as well if that isn't true, but mac80211 assumes it can
2612 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2616 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2618 if (WARN_ON(skb_headroom(skb) < 3)) {
2622 u8 *data = skb->data;
2623 size_t len = skb_headlen(skb);
2625 memmove(skb->data, data, len);
2626 skb_set_tail_pointer(skb, len);
2633 skb->protocol = eth_type_trans(skb, dev);
2634 ieee80211_deliver_skb_to_local_stack(skb, rx);
2639 * Send to wireless media and increase priority by 256 to
2640 * keep the received priority instead of reclassifying
2641 * the frame (see cfg80211_classify8021d).
2643 xmit_skb->priority += 256;
2644 xmit_skb->protocol = htons(ETH_P_802_3);
2645 skb_reset_network_header(xmit_skb);
2646 skb_reset_mac_header(xmit_skb);
2647 dev_queue_xmit(xmit_skb);
2651 static ieee80211_rx_result debug_noinline
2652 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
2654 struct net_device *dev = rx->sdata->dev;
2655 struct sk_buff *skb = rx->skb;
2656 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2657 __le16 fc = hdr->frame_control;
2658 struct sk_buff_head frame_list;
2659 struct ethhdr ethhdr;
2660 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
2662 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2665 } else switch (rx->sdata->vif.type) {
2666 case NL80211_IFTYPE_AP:
2667 case NL80211_IFTYPE_AP_VLAN:
2670 case NL80211_IFTYPE_STATION:
2672 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2675 case NL80211_IFTYPE_MESH_POINT:
2683 __skb_queue_head_init(&frame_list);
2685 if (ieee80211_data_to_8023_exthdr(skb, ðhdr,
2686 rx->sdata->vif.addr,
2687 rx->sdata->vif.type,
2689 return RX_DROP_UNUSABLE;
2691 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2692 rx->sdata->vif.type,
2693 rx->local->hw.extra_tx_headroom,
2694 check_da, check_sa);
2696 while (!skb_queue_empty(&frame_list)) {
2697 rx->skb = __skb_dequeue(&frame_list);
2699 if (!ieee80211_frame_allowed(rx, fc)) {
2700 dev_kfree_skb(rx->skb);
2704 ieee80211_deliver_skb(rx);
2710 static ieee80211_rx_result debug_noinline
2711 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2713 struct sk_buff *skb = rx->skb;
2714 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2715 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2716 __le16 fc = hdr->frame_control;
2718 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2721 if (unlikely(!ieee80211_is_data(fc)))
2724 if (unlikely(!ieee80211_is_data_present(fc)))
2725 return RX_DROP_MONITOR;
2727 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2728 switch (rx->sdata->vif.type) {
2729 case NL80211_IFTYPE_AP_VLAN:
2730 if (!rx->sdata->u.vlan.sta)
2731 return RX_DROP_UNUSABLE;
2733 case NL80211_IFTYPE_STATION:
2734 if (!rx->sdata->u.mgd.use_4addr)
2735 return RX_DROP_UNUSABLE;
2738 return RX_DROP_UNUSABLE;
2742 if (is_multicast_ether_addr(hdr->addr1))
2743 return RX_DROP_UNUSABLE;
2745 return __ieee80211_rx_h_amsdu(rx, 0);
2748 #ifdef CONFIG_MAC80211_MESH
2749 static ieee80211_rx_result
2750 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2752 struct ieee80211_hdr *fwd_hdr, *hdr;
2753 struct ieee80211_tx_info *info;
2754 struct ieee80211s_hdr *mesh_hdr;
2755 struct sk_buff *skb = rx->skb, *fwd_skb;
2756 struct ieee80211_local *local = rx->local;
2757 struct ieee80211_sub_if_data *sdata = rx->sdata;
2758 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2762 hdr = (struct ieee80211_hdr *) skb->data;
2763 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2765 /* make sure fixed part of mesh header is there, also checks skb len */
2766 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2767 return RX_DROP_MONITOR;
2769 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2771 /* make sure full mesh header is there, also checks skb len */
2772 if (!pskb_may_pull(rx->skb,
2773 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2774 return RX_DROP_MONITOR;
2776 /* reload pointers */
2777 hdr = (struct ieee80211_hdr *) skb->data;
2778 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2780 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2781 return RX_DROP_MONITOR;
2783 /* frame is in RMC, don't forward */
2784 if (ieee80211_is_data(hdr->frame_control) &&
2785 is_multicast_ether_addr(hdr->addr1) &&
2786 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2787 return RX_DROP_MONITOR;
2789 if (!ieee80211_is_data(hdr->frame_control))
2793 return RX_DROP_MONITOR;
2795 if (mesh_hdr->flags & MESH_FLAGS_AE) {
2796 struct mesh_path *mppath;
2800 if (is_multicast_ether_addr(hdr->addr1)) {
2801 mpp_addr = hdr->addr3;
2802 proxied_addr = mesh_hdr->eaddr1;
2803 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2804 MESH_FLAGS_AE_A5_A6) {
2805 /* has_a4 already checked in ieee80211_rx_mesh_check */
2806 mpp_addr = hdr->addr4;
2807 proxied_addr = mesh_hdr->eaddr2;
2809 return RX_DROP_MONITOR;
2813 mppath = mpp_path_lookup(sdata, proxied_addr);
2815 mpp_path_add(sdata, proxied_addr, mpp_addr);
2817 spin_lock_bh(&mppath->state_lock);
2818 if (!ether_addr_equal(mppath->mpp, mpp_addr))
2819 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2820 mppath->exp_time = jiffies;
2821 spin_unlock_bh(&mppath->state_lock);
2826 /* Frame has reached destination. Don't forward */
2827 if (!is_multicast_ether_addr(hdr->addr1) &&
2828 ether_addr_equal(sdata->vif.addr, hdr->addr3))
2831 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2832 q = sdata->vif.hw_queue[ac];
2833 if (ieee80211_queue_stopped(&local->hw, q)) {
2834 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2835 return RX_DROP_MONITOR;
2837 skb_set_queue_mapping(skb, q);
2839 if (!--mesh_hdr->ttl) {
2840 if (!is_multicast_ether_addr(hdr->addr1))
2841 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2842 dropped_frames_ttl);
2846 if (!ifmsh->mshcfg.dot11MeshForwarding)
2849 if (sdata->crypto_tx_tailroom_needed_cnt)
2850 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2852 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
2853 sdata->encrypt_headroom,
2854 tailroom, GFP_ATOMIC);
2858 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
2859 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2860 info = IEEE80211_SKB_CB(fwd_skb);
2861 memset(info, 0, sizeof(*info));
2862 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
2863 info->control.vif = &rx->sdata->vif;
2864 info->control.jiffies = jiffies;
2865 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2866 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2867 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2868 /* update power mode indication when forwarding */
2869 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2870 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2871 /* mesh power mode flags updated in mesh_nexthop_lookup */
2872 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2874 /* unable to resolve next hop */
2875 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2877 WLAN_REASON_MESH_PATH_NOFORWARD,
2879 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2881 return RX_DROP_MONITOR;
2884 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2885 ieee80211_add_pending_skb(local, fwd_skb);
2887 if (is_multicast_ether_addr(hdr->addr1))
2889 return RX_DROP_MONITOR;
2893 static ieee80211_rx_result debug_noinline
2894 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2896 struct ieee80211_sub_if_data *sdata = rx->sdata;
2897 struct ieee80211_local *local = rx->local;
2898 struct net_device *dev = sdata->dev;
2899 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2900 __le16 fc = hdr->frame_control;
2904 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2907 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2908 return RX_DROP_MONITOR;
2911 * Send unexpected-4addr-frame event to hostapd. For older versions,
2912 * also drop the frame to cooked monitor interfaces.
2914 if (ieee80211_has_a4(hdr->frame_control) &&
2915 sdata->vif.type == NL80211_IFTYPE_AP) {
2917 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2918 cfg80211_rx_unexpected_4addr_frame(
2919 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
2920 return RX_DROP_MONITOR;
2923 err = __ieee80211_data_to_8023(rx, &port_control);
2925 return RX_DROP_UNUSABLE;
2927 if (!ieee80211_frame_allowed(rx, fc))
2928 return RX_DROP_MONITOR;
2930 /* directly handle TDLS channel switch requests/responses */
2931 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2932 cpu_to_be16(ETH_P_TDLS))) {
2933 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2935 if (pskb_may_pull(rx->skb,
2936 offsetof(struct ieee80211_tdls_data, u)) &&
2937 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2938 tf->category == WLAN_CATEGORY_TDLS &&
2939 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2940 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2941 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2942 schedule_work(&local->tdls_chsw_work);
2944 rx->sta->rx_stats.packets++;
2950 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2951 unlikely(port_control) && sdata->bss) {
2952 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2960 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2961 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2962 !is_multicast_ether_addr(
2963 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2964 (!local->scanning &&
2965 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2966 mod_timer(&local->dynamic_ps_timer, jiffies +
2967 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2969 ieee80211_deliver_skb(rx);
2974 static ieee80211_rx_result debug_noinline
2975 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
2977 struct sk_buff *skb = rx->skb;
2978 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2979 struct tid_ampdu_rx *tid_agg_rx;
2983 if (likely(!ieee80211_is_ctl(bar->frame_control)))
2986 if (ieee80211_is_back_req(bar->frame_control)) {
2988 __le16 control, start_seq_num;
2989 } __packed bar_data;
2990 struct ieee80211_event event = {
2991 .type = BAR_RX_EVENT,
2995 return RX_DROP_MONITOR;
2997 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2998 &bar_data, sizeof(bar_data)))
2999 return RX_DROP_MONITOR;
3001 tid = le16_to_cpu(bar_data.control) >> 12;
3003 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3004 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3005 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3006 WLAN_BACK_RECIPIENT,
3007 WLAN_REASON_QSTA_REQUIRE_SETUP);
3009 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3011 return RX_DROP_MONITOR;
3013 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
3014 event.u.ba.tid = tid;
3015 event.u.ba.ssn = start_seq_num;
3016 event.u.ba.sta = &rx->sta->sta;
3018 /* reset session timer */
3019 if (tid_agg_rx->timeout)
3020 mod_timer(&tid_agg_rx->session_timer,
3021 TU_TO_EXP_TIME(tid_agg_rx->timeout));
3023 spin_lock(&tid_agg_rx->reorder_lock);
3024 /* release stored frames up to start of BAR */
3025 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
3026 start_seq_num, frames);
3027 spin_unlock(&tid_agg_rx->reorder_lock);
3029 drv_event_callback(rx->local, rx->sdata, &event);
3036 * After this point, we only want management frames,
3037 * so we can drop all remaining control frames to
3038 * cooked monitor interfaces.
3040 return RX_DROP_MONITOR;
3043 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3044 struct ieee80211_mgmt *mgmt,
3047 struct ieee80211_local *local = sdata->local;
3048 struct sk_buff *skb;
3049 struct ieee80211_mgmt *resp;
3051 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
3052 /* Not to own unicast address */
3056 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3057 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
3058 /* Not from the current AP or not associated yet. */
3062 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3063 /* Too short SA Query request frame */
3067 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3071 skb_reserve(skb, local->hw.extra_tx_headroom);
3072 resp = skb_put_zero(skb, 24);
3073 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3074 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3075 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
3076 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3077 IEEE80211_STYPE_ACTION);
3078 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3079 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3080 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3081 memcpy(resp->u.action.u.sa_query.trans_id,
3082 mgmt->u.action.u.sa_query.trans_id,
3083 WLAN_SA_QUERY_TR_ID_LEN);
3085 ieee80211_tx_skb(sdata, skb);
3088 static ieee80211_rx_result debug_noinline
3089 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3091 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3092 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3094 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3098 * From here on, look only at management frames.
3099 * Data and control frames are already handled,
3100 * and unknown (reserved) frames are useless.
3102 if (rx->skb->len < 24)
3103 return RX_DROP_MONITOR;
3105 if (!ieee80211_is_mgmt(mgmt->frame_control))
3106 return RX_DROP_MONITOR;
3108 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3109 ieee80211_is_beacon(mgmt->frame_control) &&
3110 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3113 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3114 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3115 sig = status->signal;
3117 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3118 rx->skb->data, rx->skb->len,
3119 ieee80211_rx_status_to_khz(status),
3121 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3124 if (ieee80211_drop_unencrypted_mgmt(rx))
3125 return RX_DROP_UNUSABLE;
3130 static ieee80211_rx_result debug_noinline
3131 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3133 struct ieee80211_local *local = rx->local;
3134 struct ieee80211_sub_if_data *sdata = rx->sdata;
3135 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3136 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3137 int len = rx->skb->len;
3139 if (!ieee80211_is_action(mgmt->frame_control))
3142 /* drop too small frames */
3143 if (len < IEEE80211_MIN_ACTION_SIZE)
3144 return RX_DROP_UNUSABLE;
3146 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3147 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3148 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3149 return RX_DROP_UNUSABLE;
3151 switch (mgmt->u.action.category) {
3152 case WLAN_CATEGORY_HT:
3153 /* reject HT action frames from stations not supporting HT */
3154 if (!rx->sta->sta.ht_cap.ht_supported)
3157 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3158 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3159 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3160 sdata->vif.type != NL80211_IFTYPE_AP &&
3161 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3164 /* verify action & smps_control/chanwidth are present */
3165 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3168 switch (mgmt->u.action.u.ht_smps.action) {
3169 case WLAN_HT_ACTION_SMPS: {
3170 struct ieee80211_supported_band *sband;
3171 enum ieee80211_smps_mode smps_mode;
3172 struct sta_opmode_info sta_opmode = {};
3174 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3175 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3178 /* convert to HT capability */
3179 switch (mgmt->u.action.u.ht_smps.smps_control) {
3180 case WLAN_HT_SMPS_CONTROL_DISABLED:
3181 smps_mode = IEEE80211_SMPS_OFF;
3183 case WLAN_HT_SMPS_CONTROL_STATIC:
3184 smps_mode = IEEE80211_SMPS_STATIC;
3186 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3187 smps_mode = IEEE80211_SMPS_DYNAMIC;
3193 /* if no change do nothing */
3194 if (rx->sta->sta.smps_mode == smps_mode)
3196 rx->sta->sta.smps_mode = smps_mode;
3197 sta_opmode.smps_mode =
3198 ieee80211_smps_mode_to_smps_mode(smps_mode);
3199 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3201 sband = rx->local->hw.wiphy->bands[status->band];
3203 rate_control_rate_update(local, sband, rx->sta,
3204 IEEE80211_RC_SMPS_CHANGED);
3205 cfg80211_sta_opmode_change_notify(sdata->dev,
3211 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3212 struct ieee80211_supported_band *sband;
3213 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
3214 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
3215 struct sta_opmode_info sta_opmode = {};
3217 /* If it doesn't support 40 MHz it can't change ... */
3218 if (!(rx->sta->sta.ht_cap.cap &
3219 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3222 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
3223 max_bw = IEEE80211_STA_RX_BW_20;
3225 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3227 /* set cur_max_bandwidth and recalc sta bw */
3228 rx->sta->cur_max_bandwidth = max_bw;
3229 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
3231 if (rx->sta->sta.bandwidth == new_bw)
3234 rx->sta->sta.bandwidth = new_bw;
3235 sband = rx->local->hw.wiphy->bands[status->band];
3237 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
3238 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
3240 rate_control_rate_update(local, sband, rx->sta,
3241 IEEE80211_RC_BW_CHANGED);
3242 cfg80211_sta_opmode_change_notify(sdata->dev,
3253 case WLAN_CATEGORY_PUBLIC:
3254 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3256 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3260 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3262 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3263 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3265 if (len < offsetof(struct ieee80211_mgmt,
3266 u.action.u.ext_chan_switch.variable))
3269 case WLAN_CATEGORY_VHT:
3270 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3271 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3272 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3273 sdata->vif.type != NL80211_IFTYPE_AP &&
3274 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3277 /* verify action code is present */
3278 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3281 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3282 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3283 /* verify opmode is present */
3284 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3288 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3289 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3297 case WLAN_CATEGORY_BACK:
3298 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3299 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3300 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3301 sdata->vif.type != NL80211_IFTYPE_AP &&
3302 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3305 /* verify action_code is present */
3306 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3309 switch (mgmt->u.action.u.addba_req.action_code) {
3310 case WLAN_ACTION_ADDBA_REQ:
3311 if (len < (IEEE80211_MIN_ACTION_SIZE +
3312 sizeof(mgmt->u.action.u.addba_req)))
3315 case WLAN_ACTION_ADDBA_RESP:
3316 if (len < (IEEE80211_MIN_ACTION_SIZE +
3317 sizeof(mgmt->u.action.u.addba_resp)))
3320 case WLAN_ACTION_DELBA:
3321 if (len < (IEEE80211_MIN_ACTION_SIZE +
3322 sizeof(mgmt->u.action.u.delba)))
3330 case WLAN_CATEGORY_SPECTRUM_MGMT:
3331 /* verify action_code is present */
3332 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3335 switch (mgmt->u.action.u.measurement.action_code) {
3336 case WLAN_ACTION_SPCT_MSR_REQ:
3337 if (status->band != NL80211_BAND_5GHZ)
3340 if (len < (IEEE80211_MIN_ACTION_SIZE +
3341 sizeof(mgmt->u.action.u.measurement)))
3344 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3347 ieee80211_process_measurement_req(sdata, mgmt, len);
3349 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3351 if (len < (IEEE80211_MIN_ACTION_SIZE +
3352 sizeof(mgmt->u.action.u.chan_switch)))
3355 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3356 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3357 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3360 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3361 bssid = sdata->u.mgd.bssid;
3362 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3363 bssid = sdata->u.ibss.bssid;
3364 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3369 if (!ether_addr_equal(mgmt->bssid, bssid))
3376 case WLAN_CATEGORY_SELF_PROTECTED:
3377 if (len < (IEEE80211_MIN_ACTION_SIZE +
3378 sizeof(mgmt->u.action.u.self_prot.action_code)))
3381 switch (mgmt->u.action.u.self_prot.action_code) {
3382 case WLAN_SP_MESH_PEERING_OPEN:
3383 case WLAN_SP_MESH_PEERING_CLOSE:
3384 case WLAN_SP_MESH_PEERING_CONFIRM:
3385 if (!ieee80211_vif_is_mesh(&sdata->vif))
3387 if (sdata->u.mesh.user_mpm)
3388 /* userspace handles this frame */
3391 case WLAN_SP_MGK_INFORM:
3392 case WLAN_SP_MGK_ACK:
3393 if (!ieee80211_vif_is_mesh(&sdata->vif))
3398 case WLAN_CATEGORY_MESH_ACTION:
3399 if (len < (IEEE80211_MIN_ACTION_SIZE +
3400 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3403 if (!ieee80211_vif_is_mesh(&sdata->vif))
3405 if (mesh_action_is_path_sel(mgmt) &&
3406 !mesh_path_sel_is_hwmp(sdata))
3414 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3415 /* will return in the next handlers */
3420 rx->sta->rx_stats.packets++;
3421 dev_kfree_skb(rx->skb);
3425 skb_queue_tail(&sdata->skb_queue, rx->skb);
3426 ieee80211_queue_work(&local->hw, &sdata->work);
3428 rx->sta->rx_stats.packets++;
3432 static ieee80211_rx_result debug_noinline
3433 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3435 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3438 /* skip known-bad action frames and return them in the next handler */
3439 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3443 * Getting here means the kernel doesn't know how to handle
3444 * it, but maybe userspace does ... include returned frames
3445 * so userspace can register for those to know whether ones
3446 * it transmitted were processed or returned.
3449 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3450 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3451 sig = status->signal;
3453 if (cfg80211_rx_mgmt_khz(&rx->sdata->wdev,
3454 ieee80211_rx_status_to_khz(status), sig,
3455 rx->skb->data, rx->skb->len, 0)) {
3457 rx->sta->rx_stats.packets++;
3458 dev_kfree_skb(rx->skb);
3465 static ieee80211_rx_result debug_noinline
3466 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3468 struct ieee80211_sub_if_data *sdata = rx->sdata;
3469 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3470 int len = rx->skb->len;
3472 if (!ieee80211_is_action(mgmt->frame_control))
3475 switch (mgmt->u.action.category) {
3476 case WLAN_CATEGORY_SA_QUERY:
3477 if (len < (IEEE80211_MIN_ACTION_SIZE +
3478 sizeof(mgmt->u.action.u.sa_query)))
3481 switch (mgmt->u.action.u.sa_query.action) {
3482 case WLAN_ACTION_SA_QUERY_REQUEST:
3483 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3485 ieee80211_process_sa_query_req(sdata, mgmt, len);
3495 rx->sta->rx_stats.packets++;
3496 dev_kfree_skb(rx->skb);
3500 static ieee80211_rx_result debug_noinline
3501 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3503 struct ieee80211_local *local = rx->local;
3504 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3505 struct sk_buff *nskb;
3506 struct ieee80211_sub_if_data *sdata = rx->sdata;
3507 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3509 if (!ieee80211_is_action(mgmt->frame_control))
3513 * For AP mode, hostapd is responsible for handling any action
3514 * frames that we didn't handle, including returning unknown
3515 * ones. For all other modes we will return them to the sender,
3516 * setting the 0x80 bit in the action category, as required by
3517 * 802.11-2012 9.24.4.
3518 * Newer versions of hostapd shall also use the management frame
3519 * registration mechanisms, but older ones still use cooked
3520 * monitor interfaces so push all frames there.
3522 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3523 (sdata->vif.type == NL80211_IFTYPE_AP ||
3524 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3525 return RX_DROP_MONITOR;
3527 if (is_multicast_ether_addr(mgmt->da))
3528 return RX_DROP_MONITOR;
3530 /* do not return rejected action frames */
3531 if (mgmt->u.action.category & 0x80)
3532 return RX_DROP_UNUSABLE;
3534 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3537 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3539 nmgmt->u.action.category |= 0x80;
3540 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3541 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3543 memset(nskb->cb, 0, sizeof(nskb->cb));
3545 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3546 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3548 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3549 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3550 IEEE80211_TX_CTL_NO_CCK_RATE;
3551 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3553 local->hw.offchannel_tx_hw_queue;
3556 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3559 dev_kfree_skb(rx->skb);
3563 static ieee80211_rx_result debug_noinline
3564 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3566 struct ieee80211_sub_if_data *sdata = rx->sdata;
3567 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3569 if (!ieee80211_is_ext(hdr->frame_control))
3572 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3573 return RX_DROP_MONITOR;
3575 /* for now only beacons are ext, so queue them */
3576 skb_queue_tail(&sdata->skb_queue, rx->skb);
3577 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3579 rx->sta->rx_stats.packets++;
3584 static ieee80211_rx_result debug_noinline
3585 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3587 struct ieee80211_sub_if_data *sdata = rx->sdata;
3588 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3591 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3593 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3594 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3595 sdata->vif.type != NL80211_IFTYPE_OCB &&
3596 sdata->vif.type != NL80211_IFTYPE_STATION)
3597 return RX_DROP_MONITOR;
3600 case cpu_to_le16(IEEE80211_STYPE_AUTH):
3601 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3602 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3603 /* process for all: mesh, mlme, ibss */
3605 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3606 if (is_multicast_ether_addr(mgmt->da) &&
3607 !is_broadcast_ether_addr(mgmt->da))
3608 return RX_DROP_MONITOR;
3610 /* process only for station/IBSS */
3611 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3612 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3613 return RX_DROP_MONITOR;
3615 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3616 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3617 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3618 if (is_multicast_ether_addr(mgmt->da) &&
3619 !is_broadcast_ether_addr(mgmt->da))
3620 return RX_DROP_MONITOR;
3622 /* process only for station */
3623 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3624 return RX_DROP_MONITOR;
3626 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3627 /* process only for ibss and mesh */
3628 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3629 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3630 return RX_DROP_MONITOR;
3633 return RX_DROP_MONITOR;
3636 /* queue up frame and kick off work to process it */
3637 skb_queue_tail(&sdata->skb_queue, rx->skb);
3638 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3640 rx->sta->rx_stats.packets++;
3645 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3646 struct ieee80211_rate *rate)
3648 struct ieee80211_sub_if_data *sdata;
3649 struct ieee80211_local *local = rx->local;
3650 struct sk_buff *skb = rx->skb, *skb2;
3651 struct net_device *prev_dev = NULL;
3652 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3653 int needed_headroom;
3656 * If cooked monitor has been processed already, then
3657 * don't do it again. If not, set the flag.
3659 if (rx->flags & IEEE80211_RX_CMNTR)
3661 rx->flags |= IEEE80211_RX_CMNTR;
3663 /* If there are no cooked monitor interfaces, just free the SKB */
3664 if (!local->cooked_mntrs)
3667 /* vendor data is long removed here */
3668 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3669 /* room for the radiotap header based on driver features */
3670 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3672 if (skb_headroom(skb) < needed_headroom &&
3673 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3676 /* prepend radiotap information */
3677 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3680 skb_reset_mac_header(skb);
3681 skb->ip_summed = CHECKSUM_UNNECESSARY;
3682 skb->pkt_type = PACKET_OTHERHOST;
3683 skb->protocol = htons(ETH_P_802_2);
3685 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3686 if (!ieee80211_sdata_running(sdata))
3689 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3690 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3694 skb2 = skb_clone(skb, GFP_ATOMIC);
3696 skb2->dev = prev_dev;
3697 netif_receive_skb(skb2);
3701 prev_dev = sdata->dev;
3702 ieee80211_rx_stats(sdata->dev, skb->len);
3706 skb->dev = prev_dev;
3707 netif_receive_skb(skb);
3715 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3716 ieee80211_rx_result res)
3719 case RX_DROP_MONITOR:
3720 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3722 rx->sta->rx_stats.dropped++;
3725 struct ieee80211_rate *rate = NULL;
3726 struct ieee80211_supported_band *sband;
3727 struct ieee80211_rx_status *status;
3729 status = IEEE80211_SKB_RXCB((rx->skb));
3731 sband = rx->local->hw.wiphy->bands[status->band];
3732 if (status->encoding == RX_ENC_LEGACY)
3733 rate = &sband->bitrates[status->rate_idx];
3735 ieee80211_rx_cooked_monitor(rx, rate);
3738 case RX_DROP_UNUSABLE:
3739 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3741 rx->sta->rx_stats.dropped++;
3742 dev_kfree_skb(rx->skb);
3745 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3750 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3751 struct sk_buff_head *frames)
3753 ieee80211_rx_result res = RX_DROP_MONITOR;
3754 struct sk_buff *skb;
3756 #define CALL_RXH(rxh) \
3759 if (res != RX_CONTINUE) \
3763 /* Lock here to avoid hitting all of the data used in the RX
3764 * path (e.g. key data, station data, ...) concurrently when
3765 * a frame is released from the reorder buffer due to timeout
3766 * from the timer, potentially concurrently with RX from the
3769 spin_lock_bh(&rx->local->rx_path_lock);
3771 while ((skb = __skb_dequeue(frames))) {
3773 * all the other fields are valid across frames
3774 * that belong to an aMPDU since they are on the
3775 * same TID from the same station
3779 CALL_RXH(ieee80211_rx_h_check_more_data);
3780 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3781 CALL_RXH(ieee80211_rx_h_sta_process);
3782 CALL_RXH(ieee80211_rx_h_decrypt);
3783 CALL_RXH(ieee80211_rx_h_defragment);
3784 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3785 /* must be after MMIC verify so header is counted in MPDU mic */
3786 #ifdef CONFIG_MAC80211_MESH
3787 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3788 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3790 CALL_RXH(ieee80211_rx_h_amsdu);
3791 CALL_RXH(ieee80211_rx_h_data);
3793 /* special treatment -- needs the queue */
3794 res = ieee80211_rx_h_ctrl(rx, frames);
3795 if (res != RX_CONTINUE)
3798 CALL_RXH(ieee80211_rx_h_mgmt_check);
3799 CALL_RXH(ieee80211_rx_h_action);
3800 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3801 CALL_RXH(ieee80211_rx_h_action_post_userspace);
3802 CALL_RXH(ieee80211_rx_h_action_return);
3803 CALL_RXH(ieee80211_rx_h_ext);
3804 CALL_RXH(ieee80211_rx_h_mgmt);
3807 ieee80211_rx_handlers_result(rx, res);
3812 spin_unlock_bh(&rx->local->rx_path_lock);
3815 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3817 struct sk_buff_head reorder_release;
3818 ieee80211_rx_result res = RX_DROP_MONITOR;
3820 __skb_queue_head_init(&reorder_release);
3822 #define CALL_RXH(rxh) \
3825 if (res != RX_CONTINUE) \
3829 CALL_RXH(ieee80211_rx_h_check_dup);
3830 CALL_RXH(ieee80211_rx_h_check);
3832 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3834 ieee80211_rx_handlers(rx, &reorder_release);
3838 ieee80211_rx_handlers_result(rx, res);
3844 * This function makes calls into the RX path, therefore
3845 * it has to be invoked under RCU read lock.
3847 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3849 struct sk_buff_head frames;
3850 struct ieee80211_rx_data rx = {
3852 .sdata = sta->sdata,
3853 .local = sta->local,
3854 /* This is OK -- must be QoS data frame */
3855 .security_idx = tid,
3858 struct tid_ampdu_rx *tid_agg_rx;
3860 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3864 __skb_queue_head_init(&frames);
3866 spin_lock(&tid_agg_rx->reorder_lock);
3867 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3868 spin_unlock(&tid_agg_rx->reorder_lock);
3870 if (!skb_queue_empty(&frames)) {
3871 struct ieee80211_event event = {
3872 .type = BA_FRAME_TIMEOUT,
3874 .u.ba.sta = &sta->sta,
3876 drv_event_callback(rx.local, rx.sdata, &event);
3879 ieee80211_rx_handlers(&rx, &frames);
3882 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3883 u16 ssn, u64 filtered,
3886 struct sta_info *sta;
3887 struct tid_ampdu_rx *tid_agg_rx;
3888 struct sk_buff_head frames;
3889 struct ieee80211_rx_data rx = {
3890 /* This is OK -- must be QoS data frame */
3891 .security_idx = tid,
3896 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3899 __skb_queue_head_init(&frames);
3901 sta = container_of(pubsta, struct sta_info, sta);
3904 rx.sdata = sta->sdata;
3905 rx.local = sta->local;
3908 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3912 spin_lock_bh(&tid_agg_rx->reorder_lock);
3914 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3917 /* release all frames in the reorder buffer */
3918 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3919 IEEE80211_SN_MODULO;
3920 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3922 /* update ssn to match received ssn */
3923 tid_agg_rx->head_seq_num = ssn;
3925 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3929 /* handle the case that received ssn is behind the mac ssn.
3930 * it can be tid_agg_rx->buf_size behind and still be valid */
3931 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3932 if (diff >= tid_agg_rx->buf_size) {
3933 tid_agg_rx->reorder_buf_filtered = 0;
3936 filtered = filtered >> diff;
3940 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3941 int index = (ssn + i) % tid_agg_rx->buf_size;
3943 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3944 if (filtered & BIT_ULL(i))
3945 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3948 /* now process also frames that the filter marking released */
3949 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3952 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3954 ieee80211_rx_handlers(&rx, &frames);
3959 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3961 /* main receive path */
3963 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
3965 struct ieee80211_sub_if_data *sdata = rx->sdata;
3966 struct sk_buff *skb = rx->skb;
3967 struct ieee80211_hdr *hdr = (void *)skb->data;
3968 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3969 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
3970 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
3971 ieee80211_is_s1g_beacon(hdr->frame_control);
3973 switch (sdata->vif.type) {
3974 case NL80211_IFTYPE_STATION:
3975 if (!bssid && !sdata->u.mgd.use_4addr)
3977 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
3981 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3982 case NL80211_IFTYPE_ADHOC:
3985 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3986 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3988 if (ieee80211_is_beacon(hdr->frame_control))
3990 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3993 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3997 if (status->encoding != RX_ENC_LEGACY)
3998 rate_idx = 0; /* TODO: HT/VHT rates */
4000 rate_idx = status->rate_idx;
4001 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4005 case NL80211_IFTYPE_OCB:
4008 if (!ieee80211_is_data_present(hdr->frame_control))
4010 if (!is_broadcast_ether_addr(bssid))
4013 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
4017 if (status->encoding != RX_ENC_LEGACY)
4018 rate_idx = 0; /* TODO: HT rates */
4020 rate_idx = status->rate_idx;
4021 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4025 case NL80211_IFTYPE_MESH_POINT:
4026 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4030 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4031 case NL80211_IFTYPE_AP_VLAN:
4032 case NL80211_IFTYPE_AP:
4034 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4036 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
4038 * Accept public action frames even when the
4039 * BSSID doesn't match, this is used for P2P
4040 * and location updates. Note that mac80211
4041 * itself never looks at these frames.
4044 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4046 if (ieee80211_is_public_action(hdr, skb->len))
4048 return ieee80211_is_beacon(hdr->frame_control);
4051 if (!ieee80211_has_tods(hdr->frame_control)) {
4052 /* ignore data frames to TDLS-peers */
4053 if (ieee80211_is_data(hdr->frame_control))
4055 /* ignore action frames to TDLS-peers */
4056 if (ieee80211_is_action(hdr->frame_control) &&
4057 !is_broadcast_ether_addr(bssid) &&
4058 !ether_addr_equal(bssid, hdr->addr1))
4063 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4064 * the BSSID - we've checked that already but may have accepted
4065 * the wildcard (ff:ff:ff:ff:ff:ff).
4068 * The BSSID of the Data frame is determined as follows:
4069 * a) If the STA is contained within an AP or is associated
4070 * with an AP, the BSSID is the address currently in use
4071 * by the STA contained in the AP.
4073 * So we should not accept data frames with an address that's
4076 * Accepting it also opens a security problem because stations
4077 * could encrypt it with the GTK and inject traffic that way.
4079 if (ieee80211_is_data(hdr->frame_control) && multicast)
4083 case NL80211_IFTYPE_WDS:
4084 if (bssid || !ieee80211_is_data(hdr->frame_control))
4086 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
4087 case NL80211_IFTYPE_P2P_DEVICE:
4088 return ieee80211_is_public_action(hdr, skb->len) ||
4089 ieee80211_is_probe_req(hdr->frame_control) ||
4090 ieee80211_is_probe_resp(hdr->frame_control) ||
4091 ieee80211_is_beacon(hdr->frame_control);
4092 case NL80211_IFTYPE_NAN:
4093 /* Currently no frames on NAN interface are allowed */
4103 void ieee80211_check_fast_rx(struct sta_info *sta)
4105 struct ieee80211_sub_if_data *sdata = sta->sdata;
4106 struct ieee80211_local *local = sdata->local;
4107 struct ieee80211_key *key;
4108 struct ieee80211_fast_rx fastrx = {
4110 .vif_type = sdata->vif.type,
4111 .control_port_protocol = sdata->control_port_protocol,
4112 }, *old, *new = NULL;
4113 bool assign = false;
4115 /* use sparse to check that we don't return without updating */
4116 __acquire(check_fast_rx);
4118 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4119 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4120 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4121 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4123 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4125 /* fast-rx doesn't do reordering */
4126 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4127 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4130 switch (sdata->vif.type) {
4131 case NL80211_IFTYPE_STATION:
4132 if (sta->sta.tdls) {
4133 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4134 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4135 fastrx.expected_ds_bits = 0;
4137 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4138 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4139 fastrx.expected_ds_bits =
4140 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4143 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4144 fastrx.expected_ds_bits |=
4145 cpu_to_le16(IEEE80211_FCTL_TODS);
4146 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4147 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4150 if (!sdata->u.mgd.powersave)
4153 /* software powersave is a huge mess, avoid all of it */
4154 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4156 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4157 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4160 case NL80211_IFTYPE_AP_VLAN:
4161 case NL80211_IFTYPE_AP:
4162 /* parallel-rx requires this, at least with calls to
4163 * ieee80211_sta_ps_transition()
4165 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4167 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4168 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4169 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4171 fastrx.internal_forward =
4172 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4173 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4174 !sdata->u.vlan.sta);
4176 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4177 sdata->u.vlan.sta) {
4178 fastrx.expected_ds_bits |=
4179 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4180 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4181 fastrx.internal_forward = 0;
4189 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4193 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4195 switch (key->conf.cipher) {
4196 case WLAN_CIPHER_SUITE_TKIP:
4197 /* we don't want to deal with MMIC in fast-rx */
4199 case WLAN_CIPHER_SUITE_CCMP:
4200 case WLAN_CIPHER_SUITE_CCMP_256:
4201 case WLAN_CIPHER_SUITE_GCMP:
4202 case WLAN_CIPHER_SUITE_GCMP_256:
4205 /* We also don't want to deal with
4206 * WEP or cipher scheme.
4212 fastrx.icv_len = key->conf.icv_len;
4219 __release(check_fast_rx);
4222 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4224 spin_lock_bh(&sta->lock);
4225 old = rcu_dereference_protected(sta->fast_rx, true);
4226 rcu_assign_pointer(sta->fast_rx, new);
4227 spin_unlock_bh(&sta->lock);
4230 kfree_rcu(old, rcu_head);
4233 void ieee80211_clear_fast_rx(struct sta_info *sta)
4235 struct ieee80211_fast_rx *old;
4237 spin_lock_bh(&sta->lock);
4238 old = rcu_dereference_protected(sta->fast_rx, true);
4239 RCU_INIT_POINTER(sta->fast_rx, NULL);
4240 spin_unlock_bh(&sta->lock);
4243 kfree_rcu(old, rcu_head);
4246 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4248 struct ieee80211_local *local = sdata->local;
4249 struct sta_info *sta;
4251 lockdep_assert_held(&local->sta_mtx);
4253 list_for_each_entry(sta, &local->sta_list, list) {
4254 if (sdata != sta->sdata &&
4255 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4257 ieee80211_check_fast_rx(sta);
4261 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4263 struct ieee80211_local *local = sdata->local;
4265 mutex_lock(&local->sta_mtx);
4266 __ieee80211_check_fast_rx_iface(sdata);
4267 mutex_unlock(&local->sta_mtx);
4270 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4271 struct ieee80211_fast_rx *fast_rx)
4273 struct sk_buff *skb = rx->skb;
4274 struct ieee80211_hdr *hdr = (void *)skb->data;
4275 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4276 struct sta_info *sta = rx->sta;
4277 int orig_len = skb->len;
4278 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4279 int snap_offs = hdrlen;
4281 u8 snap[sizeof(rfc1042_header)];
4283 } *payload __aligned(2);
4287 } addrs __aligned(2);
4288 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4290 if (fast_rx->uses_rss)
4291 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4293 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4294 * to a common data structure; drivers can implement that per queue
4295 * but we don't have that information in mac80211
4297 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4300 #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4302 /* If using encryption, we also need to have:
4303 * - PN_VALIDATED: similar, but the implementation is tricky
4304 * - DECRYPTED: necessary for PN_VALIDATED
4307 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4310 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4313 if (unlikely(ieee80211_is_frag(hdr)))
4316 /* Since our interface address cannot be multicast, this
4317 * implicitly also rejects multicast frames without the
4320 * We shouldn't get any *data* frames not addressed to us
4321 * (AP mode will accept multicast *management* frames), but
4322 * punting here will make it go through the full checks in
4323 * ieee80211_accept_frame().
4325 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4328 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4329 IEEE80211_FCTL_TODS)) !=
4330 fast_rx->expected_ds_bits)
4333 /* assign the key to drop unencrypted frames (later)
4334 * and strip the IV/MIC if necessary
4336 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4337 /* GCMP header length is the same */
4338 snap_offs += IEEE80211_CCMP_HDR_LEN;
4341 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4342 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4345 payload = (void *)(skb->data + snap_offs);
4347 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4350 /* Don't handle these here since they require special code.
4351 * Accept AARP and IPX even though they should come with a
4352 * bridge-tunnel header - but if we get them this way then
4353 * there's little point in discarding them.
4355 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4356 payload->proto == fast_rx->control_port_protocol))
4360 /* after this point, don't punt to the slowpath! */
4362 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4363 pskb_trim(skb, skb->len - fast_rx->icv_len))
4366 /* statistics part of ieee80211_rx_h_sta_process() */
4367 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4368 stats->last_signal = status->signal;
4369 if (!fast_rx->uses_rss)
4370 ewma_signal_add(&sta->rx_stats_avg.signal,
4374 if (status->chains) {
4377 stats->chains = status->chains;
4378 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4379 int signal = status->chain_signal[i];
4381 if (!(status->chains & BIT(i)))
4384 stats->chain_signal_last[i] = signal;
4385 if (!fast_rx->uses_rss)
4386 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4390 /* end of statistics */
4392 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4395 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4396 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4403 stats->last_rx = jiffies;
4404 stats->last_rate = sta_stats_encode_rate(status);
4409 /* do the header conversion - first grab the addresses */
4410 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4411 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4412 /* remove the SNAP but leave the ethertype */
4413 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4414 /* push the addresses in front */
4415 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4417 skb->dev = fast_rx->dev;
4419 ieee80211_rx_stats(fast_rx->dev, skb->len);
4421 /* The seqno index has the same property as needed
4422 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4423 * for non-QoS-data frames. Here we know it's a data
4424 * frame, so count MSDUs.
4426 u64_stats_update_begin(&stats->syncp);
4427 stats->msdu[rx->seqno_idx]++;
4428 stats->bytes += orig_len;
4429 u64_stats_update_end(&stats->syncp);
4431 if (fast_rx->internal_forward) {
4432 struct sk_buff *xmit_skb = NULL;
4433 if (is_multicast_ether_addr(addrs.da)) {
4434 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4435 } else if (!ether_addr_equal(addrs.da, addrs.sa) &&
4436 sta_info_get(rx->sdata, addrs.da)) {
4443 * Send to wireless media and increase priority by 256
4444 * to keep the received priority instead of
4445 * reclassifying the frame (see cfg80211_classify8021d).
4447 xmit_skb->priority += 256;
4448 xmit_skb->protocol = htons(ETH_P_802_3);
4449 skb_reset_network_header(xmit_skb);
4450 skb_reset_mac_header(xmit_skb);
4451 dev_queue_xmit(xmit_skb);
4458 /* deliver to local stack */
4459 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4460 memset(skb->cb, 0, sizeof(skb->cb));
4462 list_add_tail(&skb->list, rx->list);
4464 netif_receive_skb(skb);
4474 * This function returns whether or not the SKB
4475 * was destined for RX processing or not, which,
4476 * if consume is true, is equivalent to whether
4477 * or not the skb was consumed.
4479 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4480 struct sk_buff *skb, bool consume)
4482 struct ieee80211_local *local = rx->local;
4483 struct ieee80211_sub_if_data *sdata = rx->sdata;
4487 /* See if we can do fast-rx; if we have to copy we already lost,
4488 * so punt in that case. We should never have to deliver a data
4489 * frame to multiple interfaces anyway.
4491 * We skip the ieee80211_accept_frame() call and do the necessary
4492 * checking inside ieee80211_invoke_fast_rx().
4494 if (consume && rx->sta) {
4495 struct ieee80211_fast_rx *fast_rx;
4497 fast_rx = rcu_dereference(rx->sta->fast_rx);
4498 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4502 if (!ieee80211_accept_frame(rx))
4506 skb = skb_copy(skb, GFP_ATOMIC);
4508 if (net_ratelimit())
4509 wiphy_debug(local->hw.wiphy,
4510 "failed to copy skb for %s\n",
4518 ieee80211_invoke_rx_handlers(rx);
4523 * This is the actual Rx frames handler. as it belongs to Rx path it must
4524 * be called with rcu_read_lock protection.
4526 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
4527 struct ieee80211_sta *pubsta,
4528 struct sk_buff *skb,
4529 struct list_head *list)
4531 struct ieee80211_local *local = hw_to_local(hw);
4532 struct ieee80211_sub_if_data *sdata;
4533 struct ieee80211_hdr *hdr;
4535 struct ieee80211_rx_data rx;
4536 struct ieee80211_sub_if_data *prev;
4537 struct rhlist_head *tmp;
4540 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
4541 memset(&rx, 0, sizeof(rx));
4546 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
4547 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4549 if (ieee80211_is_mgmt(fc)) {
4550 /* drop frame if too short for header */
4551 if (skb->len < ieee80211_hdrlen(fc))
4554 err = skb_linearize(skb);
4556 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4564 hdr = (struct ieee80211_hdr *)skb->data;
4565 ieee80211_parse_qos(&rx);
4566 ieee80211_verify_alignment(&rx);
4568 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4569 ieee80211_is_beacon(hdr->frame_control) ||
4570 ieee80211_is_s1g_beacon(hdr->frame_control)))
4571 ieee80211_scan_rx(local, skb);
4573 if (ieee80211_is_data(fc)) {
4574 struct sta_info *sta, *prev_sta;
4577 rx.sta = container_of(pubsta, struct sta_info, sta);
4578 rx.sdata = rx.sta->sdata;
4579 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4586 for_each_sta_info(local, hdr->addr2, sta, tmp) {
4593 rx.sdata = prev_sta->sdata;
4594 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4601 rx.sdata = prev_sta->sdata;
4603 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4611 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4612 if (!ieee80211_sdata_running(sdata))
4615 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4616 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4620 * frame is destined for this interface, but if it's
4621 * not also for the previous one we handle that after
4622 * the loop to avoid copying the SKB once too much
4630 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4632 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4638 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4641 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4650 * This is the receive path handler. It is called by a low level driver when an
4651 * 802.11 MPDU is received from the hardware.
4653 void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4654 struct sk_buff *skb, struct list_head *list)
4656 struct ieee80211_local *local = hw_to_local(hw);
4657 struct ieee80211_rate *rate = NULL;
4658 struct ieee80211_supported_band *sband;
4659 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4661 WARN_ON_ONCE(softirq_count() == 0);
4663 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4666 sband = local->hw.wiphy->bands[status->band];
4667 if (WARN_ON(!sband))
4671 * If we're suspending, it is possible although not too likely
4672 * that we'd be receiving frames after having already partially
4673 * quiesced the stack. We can't process such frames then since
4674 * that might, for example, cause stations to be added or other
4675 * driver callbacks be invoked.
4677 if (unlikely(local->quiescing || local->suspended))
4680 /* We might be during a HW reconfig, prevent Rx for the same reason */
4681 if (unlikely(local->in_reconfig))
4685 * The same happens when we're not even started,
4686 * but that's worth a warning.
4688 if (WARN_ON(!local->started))
4691 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4693 * Validate the rate, unless a PLCP error means that
4694 * we probably can't have a valid rate here anyway.
4697 switch (status->encoding) {
4700 * rate_idx is MCS index, which can be [0-76]
4703 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
4705 * Anything else would be some sort of driver or
4706 * hardware error. The driver should catch hardware
4709 if (WARN(status->rate_idx > 76,
4710 "Rate marked as an HT rate but passed "
4711 "status->rate_idx is not "
4712 "an MCS index [0-76]: %d (0x%02x)\n",
4718 if (WARN_ONCE(status->rate_idx > 9 ||
4721 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4722 status->rate_idx, status->nss))
4726 if (WARN_ONCE(status->rate_idx > 11 ||
4729 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4730 status->rate_idx, status->nss))
4737 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4739 rate = &sband->bitrates[status->rate_idx];
4743 status->rx_flags = 0;
4746 * Frames with failed FCS/PLCP checksum are not returned,
4747 * all other frames are returned without radiotap header
4748 * if it was previously present.
4749 * Also, frames with less than 16 bytes are dropped.
4751 skb = ieee80211_rx_monitor(local, skb, rate);
4755 ieee80211_tpt_led_trig_rx(local,
4756 ((struct ieee80211_hdr *)skb->data)->frame_control,
4759 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
4765 EXPORT_SYMBOL(ieee80211_rx_list);
4767 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4768 struct sk_buff *skb, struct napi_struct *napi)
4770 struct sk_buff *tmp;
4775 * key references and virtual interfaces are protected using RCU
4776 * and this requires that we are in a read-side RCU section during
4777 * receive processing
4780 ieee80211_rx_list(hw, pubsta, skb, &list);
4784 netif_receive_skb_list(&list);
4788 list_for_each_entry_safe(skb, tmp, &list, list) {
4789 skb_list_del_init(skb);
4790 napi_gro_receive(napi, skb);
4793 EXPORT_SYMBOL(ieee80211_rx_napi);
4795 /* This is a version of the rx handler that can be called from hard irq
4796 * context. Post the skb on the queue and schedule the tasklet */
4797 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4799 struct ieee80211_local *local = hw_to_local(hw);
4801 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4803 skb->pkt_type = IEEE80211_RX_MSG;
4804 skb_queue_tail(&local->skb_queue, skb);
4805 tasklet_schedule(&local->tasklet);
4807 EXPORT_SYMBOL(ieee80211_rx_irqsafe);