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-2023 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/kcov.h>
21 #include <linux/bitops.h>
22 #include <kunit/visibility.h>
23 #include <net/mac80211.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <asm/unaligned.h>
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
38 * monitor mode reception
40 * This function cleans up the SKB, i.e. it removes all the stuff
41 * only useful for monitoring.
43 static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
44 unsigned int present_fcs_len,
45 unsigned int rtap_space)
47 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
48 struct ieee80211_hdr *hdr;
53 __pskb_trim(skb, skb->len - present_fcs_len);
54 pskb_pull(skb, rtap_space);
56 /* After pulling radiotap header, clear all flags that indicate
59 status->flag &= ~(RX_FLAG_RADIOTAP_TLV_AT_END |
60 RX_FLAG_RADIOTAP_LSIG |
61 RX_FLAG_RADIOTAP_HE_MU |
64 hdr = (void *)skb->data;
65 fc = hdr->frame_control;
68 * Remove the HT-Control field (if present) on management
69 * frames after we've sent the frame to monitoring. We
70 * (currently) don't need it, and don't properly parse
71 * frames with it present, due to the assumption of a
72 * fixed management header length.
74 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
77 hdrlen = ieee80211_hdrlen(fc);
78 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
80 if (!pskb_may_pull(skb, hdrlen)) {
85 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
86 hdrlen - IEEE80211_HT_CTL_LEN);
87 pskb_pull(skb, IEEE80211_HT_CTL_LEN);
92 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
93 unsigned int rtap_space)
95 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
96 struct ieee80211_hdr *hdr;
98 hdr = (void *)(skb->data + rtap_space);
100 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
101 RX_FLAG_FAILED_PLCP_CRC |
102 RX_FLAG_ONLY_MONITOR |
106 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
109 if (ieee80211_is_ctl(hdr->frame_control) &&
110 !ieee80211_is_pspoll(hdr->frame_control) &&
111 !ieee80211_is_back_req(hdr->frame_control))
118 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
119 struct ieee80211_rx_status *status,
124 /* always present fields */
125 len = sizeof(struct ieee80211_radiotap_header) + 8;
127 /* allocate extra bitmaps */
129 len += 4 * hweight8(status->chains);
131 if (ieee80211_have_rx_timestamp(status)) {
135 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
138 /* antenna field, if we don't have per-chain info */
142 /* padding for RX_FLAGS if necessary */
145 if (status->encoding == RX_ENC_HT) /* HT info */
148 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
153 if (status->encoding == RX_ENC_VHT) {
158 if (local->hw.radiotap_timestamp.units_pos >= 0) {
163 if (status->encoding == RX_ENC_HE &&
164 status->flag & RX_FLAG_RADIOTAP_HE) {
167 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
170 if (status->encoding == RX_ENC_HE &&
171 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
174 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
177 if (status->flag & RX_FLAG_NO_PSDU)
180 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
183 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
186 if (status->chains) {
187 /* antenna and antenna signal fields */
188 len += 2 * hweight8(status->chains);
191 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) {
195 * The position to look at depends on the existence (or non-
196 * existence) of other elements, so take that into account...
198 if (status->flag & RX_FLAG_RADIOTAP_HE)
200 sizeof(struct ieee80211_radiotap_he);
201 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
203 sizeof(struct ieee80211_radiotap_he_mu);
204 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
206 sizeof(struct ieee80211_radiotap_lsig);
208 /* ensure 4 byte alignment for TLV */
211 /* TLVs until the mac header */
212 len += skb_mac_header(skb) - &skb->data[tlv_offset];
218 static void __ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
220 struct sta_info *sta,
223 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
226 status->link_valid = 1;
227 status->link_id = link_id;
229 status->link_valid = 0;
232 skb_queue_tail(&sdata->skb_queue, skb);
233 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
235 sta->deflink.rx_stats.packets++;
238 static void ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
240 struct sta_info *sta,
244 __ieee80211_queue_skb_to_iface(sdata, link_id, sta, skb);
247 static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
252 struct ieee80211_hdr_3addr hdr;
255 } __packed __aligned(2) action;
260 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
262 if (skb->len < rtap_space + sizeof(action) +
263 VHT_MUMIMO_GROUPS_DATA_LEN)
266 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
269 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
271 if (!ieee80211_is_action(action.hdr.frame_control))
274 if (action.category != WLAN_CATEGORY_VHT)
277 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
280 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
283 skb = skb_copy(skb, GFP_ATOMIC);
287 ieee80211_queue_skb_to_iface(sdata, -1, NULL, skb);
291 * ieee80211_add_rx_radiotap_header - add radiotap header
293 * add a radiotap header containing all the fields which the hardware provided.
296 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
298 struct ieee80211_rate *rate,
299 int rtap_len, bool has_fcs)
301 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
302 struct ieee80211_radiotap_header *rthdr;
307 u16 channel_flags = 0;
310 unsigned long chains = status->chains;
311 struct ieee80211_radiotap_he he = {};
312 struct ieee80211_radiotap_he_mu he_mu = {};
313 struct ieee80211_radiotap_lsig lsig = {};
315 if (status->flag & RX_FLAG_RADIOTAP_HE) {
316 he = *(struct ieee80211_radiotap_he *)skb->data;
317 skb_pull(skb, sizeof(he));
318 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
321 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
322 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
323 skb_pull(skb, sizeof(he_mu));
326 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
327 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
328 skb_pull(skb, sizeof(lsig));
331 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END) {
332 /* data is pointer at tlv all other info was pulled off */
333 tlvs_len = skb_mac_header(skb) - skb->data;
337 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
340 rthdr = skb_push(skb, rtap_len - tlvs_len);
341 memset(rthdr, 0, rtap_len - tlvs_len);
342 it_present = &rthdr->it_present;
344 /* radiotap header, set always present flags */
345 rthdr->it_len = cpu_to_le16(rtap_len);
346 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
347 BIT(IEEE80211_RADIOTAP_CHANNEL) |
348 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
351 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
353 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
355 BIT(IEEE80211_RADIOTAP_EXT) |
356 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
357 put_unaligned_le32(it_present_val, it_present);
359 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
360 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
363 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END)
364 it_present_val |= BIT(IEEE80211_RADIOTAP_TLV);
366 put_unaligned_le32(it_present_val, it_present);
368 /* This references through an offset into it_optional[] rather
369 * than via it_present otherwise later uses of pos will cause
370 * the compiler to think we have walked past the end of the
373 pos = (void *)&rthdr->it_optional[it_present + 1 - rthdr->it_optional];
375 /* the order of the following fields is important */
377 /* IEEE80211_RADIOTAP_TSFT */
378 if (ieee80211_have_rx_timestamp(status)) {
380 while ((pos - (u8 *)rthdr) & 7)
383 ieee80211_calculate_rx_timestamp(local, status,
386 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_TSFT));
390 /* IEEE80211_RADIOTAP_FLAGS */
391 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
392 *pos |= IEEE80211_RADIOTAP_F_FCS;
393 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
394 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
395 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
396 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
399 /* IEEE80211_RADIOTAP_RATE */
400 if (!rate || status->encoding != RX_ENC_LEGACY) {
402 * Without rate information don't add it. If we have,
403 * MCS information is a separate field in radiotap,
404 * added below. The byte here is needed as padding
405 * for the channel though, so initialise it to 0.
410 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_RATE));
411 if (status->bw == RATE_INFO_BW_10)
413 else if (status->bw == RATE_INFO_BW_5)
415 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
419 /* IEEE80211_RADIOTAP_CHANNEL */
420 /* TODO: frequency offset in KHz */
421 put_unaligned_le16(status->freq, pos);
423 if (status->bw == RATE_INFO_BW_10)
424 channel_flags |= IEEE80211_CHAN_HALF;
425 else if (status->bw == RATE_INFO_BW_5)
426 channel_flags |= IEEE80211_CHAN_QUARTER;
428 if (status->band == NL80211_BAND_5GHZ ||
429 status->band == NL80211_BAND_6GHZ)
430 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
431 else if (status->encoding != RX_ENC_LEGACY)
432 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
433 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
434 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
436 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
438 channel_flags |= IEEE80211_CHAN_2GHZ;
439 put_unaligned_le16(channel_flags, pos);
442 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
443 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
444 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
445 *pos = status->signal;
447 cpu_to_le32(BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL));
451 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
453 if (!status->chains) {
454 /* IEEE80211_RADIOTAP_ANTENNA */
455 *pos = status->antenna;
459 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
461 /* IEEE80211_RADIOTAP_RX_FLAGS */
462 /* ensure 2 byte alignment for the 2 byte field as required */
463 if ((pos - (u8 *)rthdr) & 1)
465 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
466 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
467 put_unaligned_le16(rx_flags, pos);
470 if (status->encoding == RX_ENC_HT) {
473 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_MCS));
474 *pos = local->hw.radiotap_mcs_details;
475 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
476 *pos |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
477 if (status->enc_flags & RX_ENC_FLAG_LDPC)
478 *pos |= IEEE80211_RADIOTAP_MCS_HAVE_FEC;
481 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
482 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
483 if (status->bw == RATE_INFO_BW_40)
484 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
485 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
486 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
487 if (status->enc_flags & RX_ENC_FLAG_LDPC)
488 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
489 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
490 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
492 *pos++ = status->rate_idx;
495 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
498 /* ensure 4 byte alignment */
499 while ((pos - (u8 *)rthdr) & 3)
502 cpu_to_le32(BIT(IEEE80211_RADIOTAP_AMPDU_STATUS));
503 put_unaligned_le32(status->ampdu_reference, pos);
505 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
506 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
507 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
508 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
509 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
510 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
511 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
512 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
513 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
514 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
515 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
516 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
517 put_unaligned_le16(flags, pos);
519 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
520 *pos++ = status->ampdu_delimiter_crc;
526 if (status->encoding == RX_ENC_VHT) {
527 u16 known = local->hw.radiotap_vht_details;
529 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_VHT));
530 put_unaligned_le16(known, pos);
533 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
534 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
535 /* in VHT, STBC is binary */
536 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
537 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
538 if (status->enc_flags & RX_ENC_FLAG_BF)
539 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
542 switch (status->bw) {
543 case RATE_INFO_BW_80:
546 case RATE_INFO_BW_160:
549 case RATE_INFO_BW_40:
556 *pos = (status->rate_idx << 4) | status->nss;
559 if (status->enc_flags & RX_ENC_FLAG_LDPC)
560 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
568 if (local->hw.radiotap_timestamp.units_pos >= 0) {
574 cpu_to_le32(BIT(IEEE80211_RADIOTAP_TIMESTAMP));
576 /* ensure 8 byte alignment */
577 while ((pos - (u8 *)rthdr) & 7)
580 if (status->flag & RX_FLAG_MACTIME_IS_RTAP_TS64) {
581 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_64BIT;
582 ts = status->mactime;
584 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
585 ts = status->device_timestamp;
588 put_unaligned_le64(ts, pos);
591 if (local->hw.radiotap_timestamp.accuracy >= 0) {
592 accuracy = local->hw.radiotap_timestamp.accuracy;
593 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
595 put_unaligned_le16(accuracy, pos);
598 *pos++ = local->hw.radiotap_timestamp.units_pos;
602 if (status->encoding == RX_ENC_HE &&
603 status->flag & RX_FLAG_RADIOTAP_HE) {
604 #define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
606 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
607 he.data6 |= HE_PREP(DATA6_NSTS,
608 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
610 he.data3 |= HE_PREP(DATA3_STBC, 1);
612 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
615 #define CHECK_GI(s) \
616 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
617 (int)NL80211_RATE_INFO_HE_GI_##s)
623 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
624 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
625 he.data3 |= HE_PREP(DATA3_CODING,
626 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
628 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
630 switch (status->bw) {
631 case RATE_INFO_BW_20:
632 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
633 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
635 case RATE_INFO_BW_40:
636 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
637 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
639 case RATE_INFO_BW_80:
640 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
641 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
643 case RATE_INFO_BW_160:
644 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
645 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
647 case RATE_INFO_BW_HE_RU:
648 #define CHECK_RU_ALLOC(s) \
649 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
650 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
658 CHECK_RU_ALLOC(2x996);
660 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
664 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
667 /* ensure 2 byte alignment */
668 while ((pos - (u8 *)rthdr) & 1)
670 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE));
671 memcpy(pos, &he, sizeof(he));
675 if (status->encoding == RX_ENC_HE &&
676 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
677 /* ensure 2 byte alignment */
678 while ((pos - (u8 *)rthdr) & 1)
680 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_HE_MU));
681 memcpy(pos, &he_mu, sizeof(he_mu));
682 pos += sizeof(he_mu);
685 if (status->flag & RX_FLAG_NO_PSDU) {
687 cpu_to_le32(BIT(IEEE80211_RADIOTAP_ZERO_LEN_PSDU));
688 *pos++ = status->zero_length_psdu_type;
691 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
692 /* ensure 2 byte alignment */
693 while ((pos - (u8 *)rthdr) & 1)
695 rthdr->it_present |= cpu_to_le32(BIT(IEEE80211_RADIOTAP_LSIG));
696 memcpy(pos, &lsig, sizeof(lsig));
700 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
701 *pos++ = status->chain_signal[chain];
706 static struct sk_buff *
707 ieee80211_make_monitor_skb(struct ieee80211_local *local,
708 struct sk_buff **origskb,
709 struct ieee80211_rate *rate,
710 int rtap_space, bool use_origskb)
712 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
713 int rt_hdrlen, needed_headroom;
716 /* room for the radiotap header based on driver features */
717 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
718 needed_headroom = rt_hdrlen - rtap_space;
721 /* only need to expand headroom if necessary */
726 * This shouldn't trigger often because most devices have an
727 * RX header they pull before we get here, and that should
728 * be big enough for our radiotap information. We should
729 * probably export the length to drivers so that we can have
730 * them allocate enough headroom to start with.
732 if (skb_headroom(skb) < needed_headroom &&
733 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
739 * Need to make a copy and possibly remove radiotap header
740 * and FCS from the original.
742 skb = skb_copy_expand(*origskb, needed_headroom + NET_SKB_PAD,
749 /* prepend radiotap information */
750 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
752 skb_reset_mac_header(skb);
753 skb->ip_summed = CHECKSUM_UNNECESSARY;
754 skb->pkt_type = PACKET_OTHERHOST;
755 skb->protocol = htons(ETH_P_802_2);
761 * This function copies a received frame to all monitor interfaces and
762 * returns a cleaned-up SKB that no longer includes the FCS nor the
763 * radiotap header the driver might have added.
765 static struct sk_buff *
766 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
767 struct ieee80211_rate *rate)
769 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
770 struct ieee80211_sub_if_data *sdata;
771 struct sk_buff *monskb = NULL;
772 int present_fcs_len = 0;
773 unsigned int rtap_space = 0;
774 struct ieee80211_sub_if_data *monitor_sdata =
775 rcu_dereference(local->monitor_sdata);
776 bool only_monitor = false;
777 unsigned int min_head_len;
779 if (WARN_ON_ONCE(status->flag & RX_FLAG_RADIOTAP_TLV_AT_END &&
780 !skb_mac_header_was_set(origskb))) {
781 /* with this skb no way to know where frame payload starts */
782 dev_kfree_skb(origskb);
786 if (status->flag & RX_FLAG_RADIOTAP_HE)
787 rtap_space += sizeof(struct ieee80211_radiotap_he);
789 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
790 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
792 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
793 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
795 if (status->flag & RX_FLAG_RADIOTAP_TLV_AT_END)
796 rtap_space += skb_mac_header(origskb) - &origskb->data[rtap_space];
798 min_head_len = rtap_space;
801 * First, we may need to make a copy of the skb because
802 * (1) we need to modify it for radiotap (if not present), and
803 * (2) the other RX handlers will modify the skb we got.
805 * We don't need to, of course, if we aren't going to return
806 * the SKB because it has a bad FCS/PLCP checksum.
809 if (!(status->flag & RX_FLAG_NO_PSDU)) {
810 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
811 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
814 dev_kfree_skb(origskb);
817 present_fcs_len = FCS_LEN;
820 /* also consider the hdr->frame_control */
824 /* ensure that the expected data elements are in skb head */
825 if (!pskb_may_pull(origskb, min_head_len)) {
826 dev_kfree_skb(origskb);
830 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
832 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
834 dev_kfree_skb(origskb);
838 return ieee80211_clean_skb(origskb, present_fcs_len,
842 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
844 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
845 bool last_monitor = list_is_last(&sdata->u.mntr.list,
849 monskb = ieee80211_make_monitor_skb(local, &origskb,
861 skb = skb_clone(monskb, GFP_ATOMIC);
865 skb->dev = sdata->dev;
866 dev_sw_netstats_rx_add(skb->dev, skb->len);
867 netif_receive_skb(skb);
875 /* this happens if last_monitor was erroneously false */
876 dev_kfree_skb(monskb);
882 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
885 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
887 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
888 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
889 int tid, seqno_idx, security_idx;
891 /* does the frame have a qos control field? */
892 if (ieee80211_is_data_qos(hdr->frame_control)) {
893 u8 *qc = ieee80211_get_qos_ctl(hdr);
894 /* frame has qos control */
895 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
896 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
897 status->rx_flags |= IEEE80211_RX_AMSDU;
903 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
905 * Sequence numbers for management frames, QoS data
906 * frames with a broadcast/multicast address in the
907 * Address 1 field, and all non-QoS data frames sent
908 * by QoS STAs are assigned using an additional single
909 * modulo-4096 counter, [...]
911 * We also use that counter for non-QoS STAs.
913 seqno_idx = IEEE80211_NUM_TIDS;
915 if (ieee80211_is_mgmt(hdr->frame_control))
916 security_idx = IEEE80211_NUM_TIDS;
920 rx->seqno_idx = seqno_idx;
921 rx->security_idx = security_idx;
922 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
923 * For now, set skb->priority to 0 for other cases. */
924 rx->skb->priority = (tid > 7) ? 0 : tid;
928 * DOC: Packet alignment
930 * Drivers always need to pass packets that are aligned to two-byte boundaries
933 * Additionally, they should, if possible, align the payload data in a way that
934 * guarantees that the contained IP header is aligned to a four-byte
935 * boundary. In the case of regular frames, this simply means aligning the
936 * payload to a four-byte boundary (because either the IP header is directly
937 * contained, or IV/RFC1042 headers that have a length divisible by four are
938 * in front of it). If the payload data is not properly aligned and the
939 * architecture doesn't support efficient unaligned operations, mac80211
940 * will align the data.
942 * With A-MSDU frames, however, the payload data address must yield two modulo
943 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
944 * push the IP header further back to a multiple of four again. Thankfully, the
945 * specs were sane enough this time around to require padding each A-MSDU
946 * subframe to a length that is a multiple of four.
948 * Padding like Atheros hardware adds which is between the 802.11 header and
949 * the payload is not supported; the driver is required to move the 802.11
950 * header to be directly in front of the payload in that case.
952 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
954 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
955 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
962 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
964 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
966 if (is_multicast_ether_addr(hdr->addr1))
969 return ieee80211_is_robust_mgmt_frame(skb);
973 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
975 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
977 if (!is_multicast_ether_addr(hdr->addr1))
980 return ieee80211_is_robust_mgmt_frame(skb);
984 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
985 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
987 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
988 struct ieee80211_mmie *mmie;
989 struct ieee80211_mmie_16 *mmie16;
991 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
994 if (!ieee80211_is_robust_mgmt_frame(skb) &&
995 !ieee80211_is_beacon(hdr->frame_control))
996 return -1; /* not a robust management frame */
998 mmie = (struct ieee80211_mmie *)
999 (skb->data + skb->len - sizeof(*mmie));
1000 if (mmie->element_id == WLAN_EID_MMIE &&
1001 mmie->length == sizeof(*mmie) - 2)
1002 return le16_to_cpu(mmie->key_id);
1004 mmie16 = (struct ieee80211_mmie_16 *)
1005 (skb->data + skb->len - sizeof(*mmie16));
1006 if (skb->len >= 24 + sizeof(*mmie16) &&
1007 mmie16->element_id == WLAN_EID_MMIE &&
1008 mmie16->length == sizeof(*mmie16) - 2)
1009 return le16_to_cpu(mmie16->key_id);
1014 static int ieee80211_get_keyid(struct sk_buff *skb)
1016 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1017 __le16 fc = hdr->frame_control;
1018 int hdrlen = ieee80211_hdrlen(fc);
1021 /* WEP, TKIP, CCMP and GCMP */
1022 if (unlikely(skb->len < hdrlen + IEEE80211_WEP_IV_LEN))
1025 skb_copy_bits(skb, hdrlen + 3, &keyid, 1);
1032 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
1034 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1035 char *dev_addr = rx->sdata->vif.addr;
1037 if (ieee80211_is_data(hdr->frame_control)) {
1038 if (is_multicast_ether_addr(hdr->addr1)) {
1039 if (ieee80211_has_tods(hdr->frame_control) ||
1040 !ieee80211_has_fromds(hdr->frame_control))
1041 return RX_DROP_MONITOR;
1042 if (ether_addr_equal(hdr->addr3, dev_addr))
1043 return RX_DROP_MONITOR;
1045 if (!ieee80211_has_a4(hdr->frame_control))
1046 return RX_DROP_MONITOR;
1047 if (ether_addr_equal(hdr->addr4, dev_addr))
1048 return RX_DROP_MONITOR;
1052 /* If there is not an established peer link and this is not a peer link
1053 * establisment frame, beacon or probe, drop the frame.
1056 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
1057 struct ieee80211_mgmt *mgmt;
1059 if (!ieee80211_is_mgmt(hdr->frame_control))
1060 return RX_DROP_MONITOR;
1062 if (ieee80211_is_action(hdr->frame_control)) {
1065 /* make sure category field is present */
1066 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1067 return RX_DROP_MONITOR;
1069 mgmt = (struct ieee80211_mgmt *)hdr;
1070 category = mgmt->u.action.category;
1071 if (category != WLAN_CATEGORY_MESH_ACTION &&
1072 category != WLAN_CATEGORY_SELF_PROTECTED)
1073 return RX_DROP_MONITOR;
1077 if (ieee80211_is_probe_req(hdr->frame_control) ||
1078 ieee80211_is_probe_resp(hdr->frame_control) ||
1079 ieee80211_is_beacon(hdr->frame_control) ||
1080 ieee80211_is_auth(hdr->frame_control))
1083 return RX_DROP_MONITOR;
1089 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1092 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1093 struct sk_buff *tail = skb_peek_tail(frames);
1094 struct ieee80211_rx_status *status;
1096 if (tid_agg_rx->reorder_buf_filtered &&
1097 tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1103 status = IEEE80211_SKB_RXCB(tail);
1104 if (status->flag & RX_FLAG_AMSDU_MORE)
1110 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1111 struct tid_ampdu_rx *tid_agg_rx,
1113 struct sk_buff_head *frames)
1115 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1116 struct sk_buff *skb;
1117 struct ieee80211_rx_status *status;
1119 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1121 if (skb_queue_empty(skb_list))
1124 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1125 __skb_queue_purge(skb_list);
1129 /* release frames from the reorder ring buffer */
1130 tid_agg_rx->stored_mpdu_num--;
1131 while ((skb = __skb_dequeue(skb_list))) {
1132 status = IEEE80211_SKB_RXCB(skb);
1133 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1134 __skb_queue_tail(frames, skb);
1138 if (tid_agg_rx->reorder_buf_filtered)
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_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1352 struct sta_info *sta = rx->sta;
1353 struct tid_ampdu_rx *tid_agg_rx;
1357 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1358 is_multicast_ether_addr(hdr->addr1))
1362 * filter the QoS data rx stream according to
1363 * STA/TID and check if this STA/TID is on aggregation
1369 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1370 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1371 tid = ieee80211_get_tid(hdr);
1373 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1375 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1376 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1377 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1378 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1379 WLAN_BACK_RECIPIENT,
1380 WLAN_REASON_QSTA_REQUIRE_SETUP);
1384 /* qos null data frames are excluded */
1385 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1388 /* not part of a BA session */
1389 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
1392 /* new, potentially un-ordered, ampdu frame - process it */
1394 /* reset session timer */
1395 if (tid_agg_rx->timeout)
1396 tid_agg_rx->last_rx = jiffies;
1398 /* if this mpdu is fragmented - terminate rx aggregation session */
1399 sc = le16_to_cpu(hdr->seq_ctrl);
1400 if (sc & IEEE80211_SCTL_FRAG) {
1401 ieee80211_queue_skb_to_iface(rx->sdata, rx->link_id, NULL, skb);
1406 * No locking needed -- we will only ever process one
1407 * RX packet at a time, and thus own tid_agg_rx. All
1408 * other code manipulating it needs to (and does) make
1409 * sure that we cannot get to it any more before doing
1412 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1417 __skb_queue_tail(frames, skb);
1420 static ieee80211_rx_result debug_noinline
1421 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1423 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1424 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1426 if (status->flag & RX_FLAG_DUP_VALIDATED)
1430 * Drop duplicate 802.11 retransmissions
1431 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1434 if (rx->skb->len < 24)
1437 if (ieee80211_is_ctl(hdr->frame_control) ||
1438 ieee80211_is_any_nullfunc(hdr->frame_control) ||
1439 is_multicast_ether_addr(hdr->addr1))
1445 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1446 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1447 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1448 rx->link_sta->rx_stats.num_duplicates++;
1449 return RX_DROP_U_DUP;
1450 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1451 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1457 static ieee80211_rx_result debug_noinline
1458 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1460 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1462 /* Drop disallowed frame classes based on STA auth/assoc state;
1463 * IEEE 802.11, Chap 5.5.
1465 * mac80211 filters only based on association state, i.e. it drops
1466 * Class 3 frames from not associated stations. hostapd sends
1467 * deauth/disassoc frames when needed. In addition, hostapd is
1468 * responsible for filtering on both auth and assoc states.
1471 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1472 return ieee80211_rx_mesh_check(rx);
1474 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1475 ieee80211_is_pspoll(hdr->frame_control)) &&
1476 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1477 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1478 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1480 * accept port control frames from the AP even when it's not
1481 * yet marked ASSOC to prevent a race where we don't set the
1482 * assoc bit quickly enough before it sends the first frame
1484 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1485 ieee80211_is_data_present(hdr->frame_control)) {
1486 unsigned int hdrlen;
1489 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1491 if (rx->skb->len < hdrlen + 8)
1492 return RX_DROP_MONITOR;
1494 skb_copy_bits(rx->skb, hdrlen + 6, ðertype, 2);
1495 if (ethertype == rx->sdata->control_port_protocol)
1499 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1500 cfg80211_rx_spurious_frame(rx->sdata->dev,
1503 return RX_DROP_U_SPURIOUS;
1505 return RX_DROP_MONITOR;
1512 static ieee80211_rx_result debug_noinline
1513 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1515 struct ieee80211_local *local;
1516 struct ieee80211_hdr *hdr;
1517 struct sk_buff *skb;
1521 hdr = (struct ieee80211_hdr *) skb->data;
1523 if (!local->pspolling)
1526 if (!ieee80211_has_fromds(hdr->frame_control))
1527 /* this is not from AP */
1530 if (!ieee80211_is_data(hdr->frame_control))
1533 if (!ieee80211_has_moredata(hdr->frame_control)) {
1534 /* AP has no more frames buffered for us */
1535 local->pspolling = false;
1539 /* more data bit is set, let's request a new frame from the AP */
1540 ieee80211_send_pspoll(local, rx->sdata);
1545 static void sta_ps_start(struct sta_info *sta)
1547 struct ieee80211_sub_if_data *sdata = sta->sdata;
1548 struct ieee80211_local *local = sdata->local;
1552 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1553 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1554 ps = &sdata->bss->ps;
1558 atomic_inc(&ps->num_sta_ps);
1559 set_sta_flag(sta, WLAN_STA_PS_STA);
1560 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1561 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1562 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1563 sta->sta.addr, sta->sta.aid);
1565 ieee80211_clear_fast_xmit(sta);
1567 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
1568 struct ieee80211_txq *txq = sta->sta.txq[tid];
1569 struct txq_info *txqi = to_txq_info(txq);
1571 spin_lock(&local->active_txq_lock[txq->ac]);
1572 if (!list_empty(&txqi->schedule_order))
1573 list_del_init(&txqi->schedule_order);
1574 spin_unlock(&local->active_txq_lock[txq->ac]);
1576 if (txq_has_queue(txq))
1577 set_bit(tid, &sta->txq_buffered_tids);
1579 clear_bit(tid, &sta->txq_buffered_tids);
1583 static void sta_ps_end(struct sta_info *sta)
1585 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1586 sta->sta.addr, sta->sta.aid);
1588 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1590 * Clear the flag only if the other one is still set
1591 * so that the TX path won't start TX'ing new frames
1592 * directly ... In the case that the driver flag isn't
1593 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1595 clear_sta_flag(sta, WLAN_STA_PS_STA);
1596 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1597 sta->sta.addr, sta->sta.aid);
1601 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1602 clear_sta_flag(sta, WLAN_STA_PS_STA);
1603 ieee80211_sta_ps_deliver_wakeup(sta);
1606 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1608 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1611 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1613 /* Don't let the same PS state be set twice */
1614 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1615 if ((start && in_ps) || (!start && !in_ps))
1625 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1627 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1629 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1631 if (test_sta_flag(sta, WLAN_STA_SP))
1634 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1635 ieee80211_sta_ps_deliver_poll_response(sta);
1637 set_sta_flag(sta, WLAN_STA_PSPOLL);
1639 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1641 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1643 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1644 int ac = ieee80211_ac_from_tid(tid);
1647 * If this AC is not trigger-enabled do nothing unless the
1648 * driver is calling us after it already checked.
1650 * NB: This could/should check a separate bitmap of trigger-
1651 * enabled queues, but for now we only implement uAPSD w/o
1652 * TSPEC changes to the ACs, so they're always the same.
1654 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1655 tid != IEEE80211_NUM_TIDS)
1658 /* if we are in a service period, do nothing */
1659 if (test_sta_flag(sta, WLAN_STA_SP))
1662 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1663 ieee80211_sta_ps_deliver_uapsd(sta);
1665 set_sta_flag(sta, WLAN_STA_UAPSD);
1667 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1669 static ieee80211_rx_result debug_noinline
1670 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1672 struct ieee80211_sub_if_data *sdata = rx->sdata;
1673 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1674 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1679 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1680 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1684 * The device handles station powersave, so don't do anything about
1685 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1686 * it to mac80211 since they're handled.)
1688 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1692 * Don't do anything if the station isn't already asleep. In
1693 * the uAPSD case, the station will probably be marked asleep,
1694 * in the PS-Poll case the station must be confused ...
1696 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1699 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1700 ieee80211_sta_pspoll(&rx->sta->sta);
1702 /* Free PS Poll skb here instead of returning RX_DROP that would
1703 * count as an dropped frame. */
1704 dev_kfree_skb(rx->skb);
1707 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1708 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1709 ieee80211_has_pm(hdr->frame_control) &&
1710 (ieee80211_is_data_qos(hdr->frame_control) ||
1711 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1712 u8 tid = ieee80211_get_tid(hdr);
1714 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1720 static ieee80211_rx_result debug_noinline
1721 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1723 struct sta_info *sta = rx->sta;
1724 struct link_sta_info *link_sta = rx->link_sta;
1725 struct sk_buff *skb = rx->skb;
1726 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1727 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1730 if (!sta || !link_sta)
1734 * Update last_rx only for IBSS packets which are for the current
1735 * BSSID and for station already AUTHORIZED to avoid keeping the
1736 * current IBSS network alive in cases where other STAs start
1737 * using different BSSID. This will also give the station another
1738 * chance to restart the authentication/authorization in case
1739 * something went wrong the first time.
1741 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1742 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1743 NL80211_IFTYPE_ADHOC);
1744 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1745 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1746 link_sta->rx_stats.last_rx = jiffies;
1747 if (ieee80211_is_data_present(hdr->frame_control) &&
1748 !is_multicast_ether_addr(hdr->addr1))
1749 link_sta->rx_stats.last_rate =
1750 sta_stats_encode_rate(status);
1752 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1753 link_sta->rx_stats.last_rx = jiffies;
1754 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
1755 !is_multicast_ether_addr(hdr->addr1)) {
1757 * Mesh beacons will update last_rx when if they are found to
1758 * match the current local configuration when processed.
1760 link_sta->rx_stats.last_rx = jiffies;
1761 if (ieee80211_is_data_present(hdr->frame_control))
1762 link_sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1765 link_sta->rx_stats.fragments++;
1767 u64_stats_update_begin(&link_sta->rx_stats.syncp);
1768 link_sta->rx_stats.bytes += rx->skb->len;
1769 u64_stats_update_end(&link_sta->rx_stats.syncp);
1771 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1772 link_sta->rx_stats.last_signal = status->signal;
1773 ewma_signal_add(&link_sta->rx_stats_avg.signal,
1777 if (status->chains) {
1778 link_sta->rx_stats.chains = status->chains;
1779 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1780 int signal = status->chain_signal[i];
1782 if (!(status->chains & BIT(i)))
1785 link_sta->rx_stats.chain_signal_last[i] = signal;
1786 ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
1791 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1795 * Change STA power saving mode only at the end of a frame
1796 * exchange sequence, and only for a data or management
1797 * frame as specified in IEEE 802.11-2016 11.2.3.2
1799 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1800 !ieee80211_has_morefrags(hdr->frame_control) &&
1801 !is_multicast_ether_addr(hdr->addr1) &&
1802 (ieee80211_is_mgmt(hdr->frame_control) ||
1803 ieee80211_is_data(hdr->frame_control)) &&
1804 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1805 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1806 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1807 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1808 if (!ieee80211_has_pm(hdr->frame_control))
1811 if (ieee80211_has_pm(hdr->frame_control))
1816 /* mesh power save support */
1817 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1818 ieee80211_mps_rx_h_sta_process(sta, hdr);
1821 * Drop (qos-)data::nullfunc frames silently, since they
1822 * are used only to control station power saving mode.
1824 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1825 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1828 * If we receive a 4-addr nullfunc frame from a STA
1829 * that was not moved to a 4-addr STA vlan yet send
1830 * the event to userspace and for older hostapd drop
1831 * the frame to the monitor interface.
1833 if (ieee80211_has_a4(hdr->frame_control) &&
1834 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1835 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1836 !rx->sdata->u.vlan.sta))) {
1837 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1838 cfg80211_rx_unexpected_4addr_frame(
1839 rx->sdata->dev, sta->sta.addr,
1841 return RX_DROP_M_UNEXPECTED_4ADDR_FRAME;
1844 * Update counter and free packet here to avoid
1845 * counting this as a dropped packed.
1847 link_sta->rx_stats.packets++;
1848 dev_kfree_skb(rx->skb);
1853 } /* ieee80211_rx_h_sta_process */
1855 static struct ieee80211_key *
1856 ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1858 struct ieee80211_key *key = NULL;
1861 /* Make sure key gets set if either BIGTK key index is set so that
1862 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1863 * Beacon frames and Beacon frames that claim to use another BIGTK key
1864 * index (i.e., a key that we do not have).
1868 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1871 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1878 key = rcu_dereference(rx->link_sta->gtk[idx]);
1880 key = rcu_dereference(rx->link->gtk[idx]);
1881 if (!key && rx->link_sta)
1882 key = rcu_dereference(rx->link_sta->gtk[idx2]);
1884 key = rcu_dereference(rx->link->gtk[idx2]);
1889 static ieee80211_rx_result debug_noinline
1890 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1892 struct sk_buff *skb = rx->skb;
1893 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1894 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1896 ieee80211_rx_result result = RX_DROP_U_DECRYPT_FAIL;
1897 struct ieee80211_key *sta_ptk = NULL;
1898 struct ieee80211_key *ptk_idx = NULL;
1899 int mmie_keyidx = -1;
1902 if (ieee80211_is_ext(hdr->frame_control))
1908 * There are five types of keys:
1909 * - GTK (group keys)
1910 * - IGTK (group keys for management frames)
1911 * - BIGTK (group keys for Beacon frames)
1912 * - PTK (pairwise keys)
1913 * - STK (station-to-station pairwise keys)
1915 * When selecting a key, we have to distinguish between multicast
1916 * (including broadcast) and unicast frames, the latter can only
1917 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1918 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1919 * then unicast frames can also use key indices like GTKs. Hence, if we
1920 * don't have a PTK/STK we check the key index for a WEP key.
1922 * Note that in a regular BSS, multicast frames are sent by the
1923 * AP only, associated stations unicast the frame to the AP first
1924 * which then multicasts it on their behalf.
1926 * There is also a slight problem in IBSS mode: GTKs are negotiated
1927 * with each station, that is something we don't currently handle.
1928 * The spec seems to expect that one negotiates the same key with
1929 * every station but there's no such requirement; VLANs could be
1933 /* start without a key */
1935 fc = hdr->frame_control;
1938 int keyid = rx->sta->ptk_idx;
1939 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1941 if (ieee80211_has_protected(fc) &&
1942 !(status->flag & RX_FLAG_IV_STRIPPED)) {
1943 keyid = ieee80211_get_keyid(rx->skb);
1945 if (unlikely(keyid < 0))
1946 return RX_DROP_U_NO_KEY_ID;
1948 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
1952 if (!ieee80211_has_protected(fc))
1953 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1955 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1956 rx->key = ptk_idx ? ptk_idx : sta_ptk;
1957 if ((status->flag & RX_FLAG_DECRYPTED) &&
1958 (status->flag & RX_FLAG_IV_STRIPPED))
1960 /* Skip decryption if the frame is not protected. */
1961 if (!ieee80211_has_protected(fc))
1963 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1964 /* Broadcast/multicast robust management frame / BIP */
1965 if ((status->flag & RX_FLAG_DECRYPTED) &&
1966 (status->flag & RX_FLAG_IV_STRIPPED))
1969 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1970 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
1971 NUM_DEFAULT_BEACON_KEYS) {
1973 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1976 return RX_DROP_M_BAD_BCN_KEYIDX;
1979 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1981 return RX_CONTINUE; /* Beacon protection not in use */
1982 } else if (mmie_keyidx >= 0) {
1983 /* Broadcast/multicast robust management frame / BIP */
1984 if ((status->flag & RX_FLAG_DECRYPTED) &&
1985 (status->flag & RX_FLAG_IV_STRIPPED))
1988 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1989 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1990 return RX_DROP_M_BAD_MGMT_KEYIDX; /* unexpected BIP keyidx */
1992 if (ieee80211_is_group_privacy_action(skb) &&
1993 test_sta_flag(rx->sta, WLAN_STA_MFP))
1994 return RX_DROP_MONITOR;
1996 rx->key = rcu_dereference(rx->link_sta->gtk[mmie_keyidx]);
1999 rx->key = rcu_dereference(rx->link->gtk[mmie_keyidx]);
2000 } else if (!ieee80211_has_protected(fc)) {
2002 * The frame was not protected, so skip decryption. However, we
2003 * need to set rx->key if there is a key that could have been
2004 * used so that the frame may be dropped if encryption would
2005 * have been expected.
2007 struct ieee80211_key *key = NULL;
2010 if (ieee80211_is_beacon(fc)) {
2011 key = ieee80211_rx_get_bigtk(rx, -1);
2012 } else if (ieee80211_is_mgmt(fc) &&
2013 is_multicast_ether_addr(hdr->addr1)) {
2014 key = rcu_dereference(rx->link->default_mgmt_key);
2017 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2018 key = rcu_dereference(rx->link_sta->gtk[i]);
2024 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2025 key = rcu_dereference(rx->link->gtk[i]);
2036 * The device doesn't give us the IV so we won't be
2037 * able to look up the key. That's ok though, we
2038 * don't need to decrypt the frame, we just won't
2039 * be able to keep statistics accurate.
2040 * Except for key threshold notifications, should
2041 * we somehow allow the driver to tell us which key
2042 * the hardware used if this flag is set?
2044 if ((status->flag & RX_FLAG_DECRYPTED) &&
2045 (status->flag & RX_FLAG_IV_STRIPPED))
2048 keyidx = ieee80211_get_keyid(rx->skb);
2050 if (unlikely(keyidx < 0))
2051 return RX_DROP_U_NO_KEY_ID;
2053 /* check per-station GTK first, if multicast packet */
2054 if (is_multicast_ether_addr(hdr->addr1) && rx->link_sta)
2055 rx->key = rcu_dereference(rx->link_sta->gtk[keyidx]);
2057 /* if not found, try default key */
2059 if (is_multicast_ether_addr(hdr->addr1))
2060 rx->key = rcu_dereference(rx->link->gtk[keyidx]);
2062 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2065 * RSNA-protected unicast frames should always be
2066 * sent with pairwise or station-to-station keys,
2067 * but for WEP we allow using a key index as well.
2070 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2071 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2072 !is_multicast_ether_addr(hdr->addr1))
2078 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2079 return RX_DROP_MONITOR;
2081 /* TODO: add threshold stuff again */
2083 return RX_DROP_MONITOR;
2086 switch (rx->key->conf.cipher) {
2087 case WLAN_CIPHER_SUITE_WEP40:
2088 case WLAN_CIPHER_SUITE_WEP104:
2089 result = ieee80211_crypto_wep_decrypt(rx);
2091 case WLAN_CIPHER_SUITE_TKIP:
2092 result = ieee80211_crypto_tkip_decrypt(rx);
2094 case WLAN_CIPHER_SUITE_CCMP:
2095 result = ieee80211_crypto_ccmp_decrypt(
2096 rx, IEEE80211_CCMP_MIC_LEN);
2098 case WLAN_CIPHER_SUITE_CCMP_256:
2099 result = ieee80211_crypto_ccmp_decrypt(
2100 rx, IEEE80211_CCMP_256_MIC_LEN);
2102 case WLAN_CIPHER_SUITE_AES_CMAC:
2103 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2105 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2106 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2108 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2109 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2110 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2112 case WLAN_CIPHER_SUITE_GCMP:
2113 case WLAN_CIPHER_SUITE_GCMP_256:
2114 result = ieee80211_crypto_gcmp_decrypt(rx);
2117 result = RX_DROP_U_BAD_CIPHER;
2120 /* the hdr variable is invalid after the decrypt handlers */
2122 /* either the frame has been decrypted or will be dropped */
2123 status->flag |= RX_FLAG_DECRYPTED;
2125 if (unlikely(ieee80211_is_beacon(fc) && RX_RES_IS_UNUSABLE(result) &&
2127 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2128 skb->data, skb->len);
2133 void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2137 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2138 skb_queue_head_init(&cache->entries[i].skb_list);
2141 void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2145 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2146 __skb_queue_purge(&cache->entries[i].skb_list);
2149 static inline struct ieee80211_fragment_entry *
2150 ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
2151 unsigned int frag, unsigned int seq, int rx_queue,
2152 struct sk_buff **skb)
2154 struct ieee80211_fragment_entry *entry;
2156 entry = &cache->entries[cache->next++];
2157 if (cache->next >= IEEE80211_FRAGMENT_MAX)
2160 __skb_queue_purge(&entry->skb_list);
2162 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2164 entry->first_frag_time = jiffies;
2166 entry->rx_queue = rx_queue;
2167 entry->last_frag = frag;
2168 entry->check_sequential_pn = false;
2169 entry->extra_len = 0;
2174 static inline struct ieee80211_fragment_entry *
2175 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
2176 unsigned int frag, unsigned int seq,
2177 int rx_queue, struct ieee80211_hdr *hdr)
2179 struct ieee80211_fragment_entry *entry;
2183 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2184 struct ieee80211_hdr *f_hdr;
2185 struct sk_buff *f_skb;
2189 idx = IEEE80211_FRAGMENT_MAX - 1;
2191 entry = &cache->entries[idx];
2192 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2193 entry->rx_queue != rx_queue ||
2194 entry->last_frag + 1 != frag)
2197 f_skb = __skb_peek(&entry->skb_list);
2198 f_hdr = (struct ieee80211_hdr *) f_skb->data;
2201 * Check ftype and addresses are equal, else check next fragment
2203 if (((hdr->frame_control ^ f_hdr->frame_control) &
2204 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
2205 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2206 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
2209 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
2210 __skb_queue_purge(&entry->skb_list);
2219 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2222 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2223 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2224 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2225 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2226 ieee80211_has_protected(fc);
2229 static ieee80211_rx_result debug_noinline
2230 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
2232 struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
2233 struct ieee80211_hdr *hdr;
2236 unsigned int frag, seq;
2237 struct ieee80211_fragment_entry *entry;
2238 struct sk_buff *skb;
2239 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2241 hdr = (struct ieee80211_hdr *)rx->skb->data;
2242 fc = hdr->frame_control;
2244 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
2247 sc = le16_to_cpu(hdr->seq_ctrl);
2248 frag = sc & IEEE80211_SCTL_FRAG;
2251 cache = &rx->sta->frags;
2253 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2256 if (is_multicast_ether_addr(hdr->addr1))
2257 return RX_DROP_MONITOR;
2259 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2261 if (skb_linearize(rx->skb))
2262 return RX_DROP_U_OOM;
2265 * skb_linearize() might change the skb->data and
2266 * previously cached variables (in this case, hdr) need to
2267 * be refreshed with the new data.
2269 hdr = (struct ieee80211_hdr *)rx->skb->data;
2270 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2273 /* This is the first fragment of a new frame. */
2274 entry = ieee80211_reassemble_add(cache, frag, seq,
2275 rx->seqno_idx, &(rx->skb));
2276 if (requires_sequential_pn(rx, fc)) {
2277 int queue = rx->security_idx;
2279 /* Store CCMP/GCMP PN so that we can verify that the
2280 * next fragment has a sequential PN value.
2282 entry->check_sequential_pn = true;
2283 entry->is_protected = true;
2284 entry->key_color = rx->key->color;
2285 memcpy(entry->last_pn,
2286 rx->key->u.ccmp.rx_pn[queue],
2287 IEEE80211_CCMP_PN_LEN);
2288 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2290 offsetof(struct ieee80211_key,
2292 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2293 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2294 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2295 IEEE80211_GCMP_PN_LEN);
2296 } else if (rx->key &&
2297 (ieee80211_has_protected(fc) ||
2298 (status->flag & RX_FLAG_DECRYPTED))) {
2299 entry->is_protected = true;
2300 entry->key_color = rx->key->color;
2305 /* This is a fragment for a frame that should already be pending in
2306 * fragment cache. Add this fragment to the end of the pending entry.
2308 entry = ieee80211_reassemble_find(cache, frag, seq,
2309 rx->seqno_idx, hdr);
2311 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2312 return RX_DROP_MONITOR;
2315 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2316 * MPDU PN values are not incrementing in steps of 1."
2317 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2318 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2320 if (entry->check_sequential_pn) {
2322 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2324 if (!requires_sequential_pn(rx, fc))
2325 return RX_DROP_U_NONSEQ_PN;
2327 /* Prevent mixed key and fragment cache attacks */
2328 if (entry->key_color != rx->key->color)
2329 return RX_DROP_U_BAD_KEY_COLOR;
2331 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2332 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2338 rpn = rx->ccm_gcm.pn;
2339 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2340 return RX_DROP_U_REPLAY;
2341 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2342 } else if (entry->is_protected &&
2344 (!ieee80211_has_protected(fc) &&
2345 !(status->flag & RX_FLAG_DECRYPTED)) ||
2346 rx->key->color != entry->key_color)) {
2347 /* Drop this as a mixed key or fragment cache attack, even
2348 * if for TKIP Michael MIC should protect us, and WEP is a
2349 * lost cause anyway.
2351 return RX_DROP_U_EXPECT_DEFRAG_PROT;
2352 } else if (entry->is_protected && rx->key &&
2353 entry->key_color != rx->key->color &&
2354 (status->flag & RX_FLAG_DECRYPTED)) {
2355 return RX_DROP_U_BAD_KEY_COLOR;
2358 skb_pull(rx->skb, ieee80211_hdrlen(fc));
2359 __skb_queue_tail(&entry->skb_list, rx->skb);
2360 entry->last_frag = frag;
2361 entry->extra_len += rx->skb->len;
2362 if (ieee80211_has_morefrags(fc)) {
2367 rx->skb = __skb_dequeue(&entry->skb_list);
2368 if (skb_tailroom(rx->skb) < entry->extra_len) {
2369 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2370 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2372 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2373 __skb_queue_purge(&entry->skb_list);
2374 return RX_DROP_U_OOM;
2377 while ((skb = __skb_dequeue(&entry->skb_list))) {
2378 skb_put_data(rx->skb, skb->data, skb->len);
2383 ieee80211_led_rx(rx->local);
2385 rx->link_sta->rx_stats.packets++;
2389 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2391 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2397 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2399 struct sk_buff *skb = rx->skb;
2400 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2403 * Pass through unencrypted frames if the hardware has
2404 * decrypted them already.
2406 if (status->flag & RX_FLAG_DECRYPTED)
2409 /* Drop unencrypted frames if key is set. */
2410 if (unlikely(!ieee80211_has_protected(fc) &&
2411 !ieee80211_is_any_nullfunc(fc) &&
2412 ieee80211_is_data(fc) && rx->key))
2418 VISIBLE_IF_MAC80211_KUNIT ieee80211_rx_result
2419 ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2421 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2422 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2423 __le16 fc = mgmt->frame_control;
2426 * Pass through unencrypted frames if the hardware has
2427 * decrypted them already.
2429 if (status->flag & RX_FLAG_DECRYPTED)
2432 /* drop unicast protected dual (that wasn't protected) */
2433 if (ieee80211_is_action(fc) &&
2434 mgmt->u.action.category == WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION)
2435 return RX_DROP_U_UNPROT_DUAL;
2437 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2438 if (unlikely(!ieee80211_has_protected(fc) &&
2439 ieee80211_is_unicast_robust_mgmt_frame(rx->skb))) {
2440 if (ieee80211_is_deauth(fc) ||
2441 ieee80211_is_disassoc(fc)) {
2443 * Permit unprotected deauth/disassoc frames
2444 * during 4-way-HS (key is installed after HS).
2449 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2453 return RX_DROP_U_UNPROT_UCAST_MGMT;
2455 /* BIP does not use Protected field, so need to check MMIE */
2456 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2457 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2458 if (ieee80211_is_deauth(fc) ||
2459 ieee80211_is_disassoc(fc))
2460 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2463 return RX_DROP_U_UNPROT_MCAST_MGMT;
2465 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
2466 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2467 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2470 return RX_DROP_U_UNPROT_BEACON;
2473 * When using MFP, Action frames are not allowed prior to
2474 * having configured keys.
2476 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2477 ieee80211_is_robust_mgmt_frame(rx->skb)))
2478 return RX_DROP_U_UNPROT_ACTION;
2480 /* drop unicast public action frames when using MPF */
2481 if (is_unicast_ether_addr(mgmt->da) &&
2482 ieee80211_is_protected_dual_of_public_action(rx->skb))
2483 return RX_DROP_U_UNPROT_UNICAST_PUB_ACTION;
2487 * Drop robust action frames before assoc regardless of MFP state,
2488 * after assoc we also have decided on MFP or not.
2490 if (ieee80211_is_action(fc) &&
2491 ieee80211_is_robust_mgmt_frame(rx->skb) &&
2492 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))
2493 return RX_DROP_U_UNPROT_ROBUST_ACTION;
2497 EXPORT_SYMBOL_IF_MAC80211_KUNIT(ieee80211_drop_unencrypted_mgmt);
2499 static ieee80211_rx_result
2500 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2502 struct ieee80211_sub_if_data *sdata = rx->sdata;
2503 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2504 bool check_port_control = false;
2505 struct ethhdr *ehdr;
2508 *port_control = false;
2509 if (ieee80211_has_a4(hdr->frame_control) &&
2510 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2511 return RX_DROP_U_UNEXPECTED_VLAN_4ADDR;
2513 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2514 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2515 if (!sdata->u.mgd.use_4addr)
2516 return RX_DROP_U_UNEXPECTED_STA_4ADDR;
2517 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
2518 check_port_control = true;
2521 if (is_multicast_ether_addr(hdr->addr1) &&
2522 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2523 return RX_DROP_U_UNEXPECTED_VLAN_MCAST;
2525 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2527 return RX_DROP_U_INVALID_8023;
2529 ehdr = (struct ethhdr *) rx->skb->data;
2530 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2531 *port_control = true;
2532 else if (check_port_control)
2533 return RX_DROP_U_NOT_PORT_CONTROL;
2538 bool ieee80211_is_our_addr(struct ieee80211_sub_if_data *sdata,
2539 const u8 *addr, int *out_link_id)
2541 unsigned int link_id;
2543 /* non-MLO, or MLD address replaced by hardware */
2544 if (ether_addr_equal(sdata->vif.addr, addr))
2547 if (!ieee80211_vif_is_mld(&sdata->vif))
2550 for (link_id = 0; link_id < ARRAY_SIZE(sdata->vif.link_conf); link_id++) {
2551 struct ieee80211_bss_conf *conf;
2553 conf = rcu_dereference(sdata->vif.link_conf[link_id]);
2557 if (ether_addr_equal(conf->addr, addr)) {
2559 *out_link_id = link_id;
2568 * requires that rx->skb is a frame with ethernet header
2570 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2572 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2573 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2574 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2577 * Allow EAPOL frames to us/the PAE group address regardless of
2578 * whether the frame was encrypted or not, and always disallow
2579 * all other destination addresses for them.
2581 if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2582 return ieee80211_is_our_addr(rx->sdata, ehdr->h_dest, NULL) ||
2583 ether_addr_equal(ehdr->h_dest, pae_group_addr);
2585 if (ieee80211_802_1x_port_control(rx) ||
2586 ieee80211_drop_unencrypted(rx, fc))
2592 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2593 struct ieee80211_rx_data *rx)
2595 struct ieee80211_sub_if_data *sdata = rx->sdata;
2596 struct net_device *dev = sdata->dev;
2598 if (unlikely((skb->protocol == sdata->control_port_protocol ||
2599 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2600 !sdata->control_port_no_preauth)) &&
2601 sdata->control_port_over_nl80211)) {
2602 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2603 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
2605 cfg80211_rx_control_port(dev, skb, noencrypt, rx->link_id);
2608 struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2610 memset(skb->cb, 0, sizeof(skb->cb));
2613 * 802.1X over 802.11 requires that the authenticator address
2614 * be used for EAPOL frames. However, 802.1X allows the use of
2615 * the PAE group address instead. If the interface is part of
2616 * a bridge and we pass the frame with the PAE group address,
2617 * then the bridge will forward it to the network (even if the
2618 * client was not associated yet), which isn't supposed to
2620 * To avoid that, rewrite the destination address to our own
2621 * address, so that the authenticator (e.g. hostapd) will see
2622 * the frame, but bridge won't forward it anywhere else. Note
2623 * that due to earlier filtering, the only other address can
2624 * be the PAE group address, unless the hardware allowed them
2625 * through in 802.3 offloaded mode.
2627 if (unlikely(skb->protocol == sdata->control_port_protocol &&
2628 !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2629 ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2631 /* deliver to local stack */
2633 list_add_tail(&skb->list, rx->list);
2635 netif_receive_skb(skb);
2640 * requires that rx->skb is a frame with ethernet header
2643 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2645 struct ieee80211_sub_if_data *sdata = rx->sdata;
2646 struct net_device *dev = sdata->dev;
2647 struct sk_buff *skb, *xmit_skb;
2648 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2649 struct sta_info *dsta;
2654 dev_sw_netstats_rx_add(dev, skb->len);
2657 /* The seqno index has the same property as needed
2658 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2659 * for non-QoS-data frames. Here we know it's a data
2660 * frame, so count MSDUs.
2662 u64_stats_update_begin(&rx->link_sta->rx_stats.syncp);
2663 rx->link_sta->rx_stats.msdu[rx->seqno_idx]++;
2664 u64_stats_update_end(&rx->link_sta->rx_stats.syncp);
2667 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2668 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2669 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2670 ehdr->h_proto != rx->sdata->control_port_protocol &&
2671 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2672 if (is_multicast_ether_addr(ehdr->h_dest) &&
2673 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2675 * send multicast frames both to higher layers in
2676 * local net stack and back to the wireless medium
2678 xmit_skb = skb_copy(skb, GFP_ATOMIC);
2680 net_info_ratelimited("%s: failed to clone multicast frame\n",
2682 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2683 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2684 dsta = sta_info_get(sdata, ehdr->h_dest);
2687 * The destination station is associated to
2688 * this AP (in this VLAN), so send the frame
2689 * directly to it and do not pass it to local
2698 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2700 /* 'align' will only take the values 0 or 2 here since all
2701 * frames are required to be aligned to 2-byte boundaries
2702 * when being passed to mac80211; the code here works just
2703 * as well if that isn't true, but mac80211 assumes it can
2704 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2708 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2710 if (WARN_ON(skb_headroom(skb) < 3)) {
2714 u8 *data = skb->data;
2715 size_t len = skb_headlen(skb);
2717 memmove(skb->data, data, len);
2718 skb_set_tail_pointer(skb, len);
2725 skb->protocol = eth_type_trans(skb, dev);
2726 ieee80211_deliver_skb_to_local_stack(skb, rx);
2731 * Send to wireless media and increase priority by 256 to
2732 * keep the received priority instead of reclassifying
2733 * the frame (see cfg80211_classify8021d).
2735 xmit_skb->priority += 256;
2736 xmit_skb->protocol = htons(ETH_P_802_3);
2737 skb_reset_network_header(xmit_skb);
2738 skb_reset_mac_header(xmit_skb);
2739 dev_queue_xmit(xmit_skb);
2743 #ifdef CONFIG_MAC80211_MESH
2745 ieee80211_rx_mesh_fast_forward(struct ieee80211_sub_if_data *sdata,
2746 struct sk_buff *skb, int hdrlen)
2748 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2749 struct ieee80211_mesh_fast_tx *entry = NULL;
2750 struct ieee80211s_hdr *mesh_hdr;
2751 struct tid_ampdu_tx *tid_tx;
2752 struct sta_info *sta;
2756 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(eth));
2757 if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
2758 entry = mesh_fast_tx_get(sdata, mesh_hdr->eaddr1);
2759 else if (!(mesh_hdr->flags & MESH_FLAGS_AE))
2760 entry = mesh_fast_tx_get(sdata, skb->data);
2764 sta = rcu_dereference(entry->mpath->next_hop);
2768 if (skb_linearize(skb))
2771 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
2772 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
2774 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
2777 if (tid_tx->timeout)
2778 tid_tx->last_tx = jiffies;
2781 ieee80211_aggr_check(sdata, sta, skb);
2783 if (ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
2787 skb->protocol = htons(skb->len - hdrlen);
2788 skb_set_network_header(skb, hdrlen + 2);
2790 skb->dev = sdata->dev;
2791 memcpy(ð, skb->data, ETH_HLEN - 2);
2793 __ieee80211_xmit_fast(sdata, sta, &entry->fast_tx, skb, tid_tx,
2794 eth.h_dest, eth.h_source);
2795 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2796 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2802 static ieee80211_rx_result
2803 ieee80211_rx_mesh_data(struct ieee80211_sub_if_data *sdata, struct sta_info *sta,
2804 struct sk_buff *skb)
2806 #ifdef CONFIG_MAC80211_MESH
2807 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2808 struct ieee80211_local *local = sdata->local;
2809 uint16_t fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA;
2810 struct ieee80211_hdr hdr = {
2811 .frame_control = cpu_to_le16(fc)
2813 struct ieee80211_hdr *fwd_hdr;
2814 struct ieee80211s_hdr *mesh_hdr;
2815 struct ieee80211_tx_info *info;
2816 struct sk_buff *fwd_skb;
2820 int hdrlen, mesh_hdrlen;
2823 if (!ieee80211_vif_is_mesh(&sdata->vif))
2826 if (!pskb_may_pull(skb, sizeof(*eth) + 6))
2827 return RX_DROP_MONITOR;
2829 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + sizeof(*eth));
2830 mesh_hdrlen = ieee80211_get_mesh_hdrlen(mesh_hdr);
2832 if (!pskb_may_pull(skb, sizeof(*eth) + mesh_hdrlen))
2833 return RX_DROP_MONITOR;
2835 eth = (struct ethhdr *)skb->data;
2836 multicast = is_multicast_ether_addr(eth->h_dest);
2838 mesh_hdr = (struct ieee80211s_hdr *)(eth + 1);
2840 return RX_DROP_MONITOR;
2842 /* frame is in RMC, don't forward */
2843 if (is_multicast_ether_addr(eth->h_dest) &&
2844 mesh_rmc_check(sdata, eth->h_source, mesh_hdr))
2845 return RX_DROP_MONITOR;
2847 /* forward packet */
2848 if (sdata->crypto_tx_tailroom_needed_cnt)
2849 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2851 if (mesh_hdr->flags & MESH_FLAGS_AE) {
2852 struct mesh_path *mppath;
2854 bool update = false;
2857 proxied_addr = mesh_hdr->eaddr1;
2858 else if ((mesh_hdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6)
2859 /* has_a4 already checked in ieee80211_rx_mesh_check */
2860 proxied_addr = mesh_hdr->eaddr2;
2862 return RX_DROP_MONITOR;
2865 mppath = mpp_path_lookup(sdata, proxied_addr);
2867 mpp_path_add(sdata, proxied_addr, eth->h_source);
2869 spin_lock_bh(&mppath->state_lock);
2870 if (!ether_addr_equal(mppath->mpp, eth->h_source)) {
2871 memcpy(mppath->mpp, eth->h_source, ETH_ALEN);
2874 mppath->exp_time = jiffies;
2875 spin_unlock_bh(&mppath->state_lock);
2878 /* flush fast xmit cache if the address path changed */
2880 mesh_fast_tx_flush_addr(sdata, proxied_addr);
2885 /* Frame has reached destination. Don't forward */
2886 if (ether_addr_equal(sdata->vif.addr, eth->h_dest))
2889 if (!--mesh_hdr->ttl) {
2893 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
2894 return RX_DROP_MONITOR;
2897 if (!ifmsh->mshcfg.dot11MeshForwarding) {
2898 if (is_multicast_ether_addr(eth->h_dest))
2901 return RX_DROP_MONITOR;
2904 skb_set_queue_mapping(skb, ieee802_1d_to_ac[skb->priority]);
2907 ieee80211_rx_mesh_fast_forward(sdata, skb, mesh_hdrlen))
2910 ieee80211_fill_mesh_addresses(&hdr, &hdr.frame_control,
2911 eth->h_dest, eth->h_source);
2912 hdrlen = ieee80211_hdrlen(hdr.frame_control);
2914 int extra_head = sizeof(struct ieee80211_hdr) - sizeof(*eth);
2916 fwd_skb = skb_copy_expand(skb, local->tx_headroom + extra_head +
2917 IEEE80211_ENCRYPT_HEADROOM,
2918 tailroom, GFP_ATOMIC);
2925 if (skb_cow_head(fwd_skb, hdrlen - sizeof(struct ethhdr)))
2926 return RX_DROP_U_OOM;
2928 if (skb_linearize(fwd_skb))
2929 return RX_DROP_U_OOM;
2932 fwd_hdr = skb_push(fwd_skb, hdrlen - sizeof(struct ethhdr));
2933 memcpy(fwd_hdr, &hdr, hdrlen - 2);
2934 qos = ieee80211_get_qos_ctl(fwd_hdr);
2935 qos[0] = qos[1] = 0;
2937 skb_reset_mac_header(fwd_skb);
2938 hdrlen += mesh_hdrlen;
2939 if (ieee80211_get_8023_tunnel_proto(fwd_skb->data + hdrlen,
2940 &fwd_skb->protocol))
2943 fwd_skb->protocol = htons(fwd_skb->len - hdrlen);
2944 skb_set_network_header(fwd_skb, hdrlen + 2);
2946 info = IEEE80211_SKB_CB(fwd_skb);
2947 memset(info, 0, sizeof(*info));
2948 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
2949 info->control.vif = &sdata->vif;
2950 info->control.jiffies = jiffies;
2951 fwd_skb->dev = sdata->dev;
2953 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2954 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2955 /* update power mode indication when forwarding */
2956 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2957 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2958 /* mesh power mode flags updated in mesh_nexthop_lookup */
2959 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2961 /* unable to resolve next hop */
2963 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2965 WLAN_REASON_MESH_PATH_NOFORWARD,
2967 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2972 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2973 ieee80211_add_pending_skb(local, fwd_skb);
2979 ieee80211_strip_8023_mesh_hdr(skb);
2985 static ieee80211_rx_result debug_noinline
2986 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
2988 struct net_device *dev = rx->sdata->dev;
2989 struct sk_buff *skb = rx->skb;
2990 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2991 __le16 fc = hdr->frame_control;
2992 struct sk_buff_head frame_list;
2993 ieee80211_rx_result res;
2994 struct ethhdr ethhdr;
2995 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
2997 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3000 } else switch (rx->sdata->vif.type) {
3001 case NL80211_IFTYPE_AP:
3002 case NL80211_IFTYPE_AP_VLAN:
3005 case NL80211_IFTYPE_STATION:
3007 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
3010 case NL80211_IFTYPE_MESH_POINT:
3019 __skb_queue_head_init(&frame_list);
3021 if (ieee80211_data_to_8023_exthdr(skb, ðhdr,
3022 rx->sdata->vif.addr,
3023 rx->sdata->vif.type,
3025 return RX_DROP_U_BAD_AMSDU;
3027 if (rx->sta->amsdu_mesh_control < 0) {
3031 for (i = 0; i <= 2; i++) {
3032 if (!ieee80211_is_valid_amsdu(skb, i))
3044 rx->sta->amsdu_mesh_control = valid;
3047 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
3048 rx->sdata->vif.type,
3049 rx->local->hw.extra_tx_headroom,
3051 rx->sta->amsdu_mesh_control);
3053 while (!skb_queue_empty(&frame_list)) {
3054 rx->skb = __skb_dequeue(&frame_list);
3056 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
3066 if (!ieee80211_frame_allowed(rx, fc))
3069 ieee80211_deliver_skb(rx);
3073 dev_kfree_skb(rx->skb);
3079 static ieee80211_rx_result debug_noinline
3080 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
3082 struct sk_buff *skb = rx->skb;
3083 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3084 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3085 __le16 fc = hdr->frame_control;
3087 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
3090 if (unlikely(!ieee80211_is_data(fc)))
3093 if (unlikely(!ieee80211_is_data_present(fc)))
3094 return RX_DROP_MONITOR;
3096 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
3097 switch (rx->sdata->vif.type) {
3098 case NL80211_IFTYPE_AP_VLAN:
3099 if (!rx->sdata->u.vlan.sta)
3100 return RX_DROP_U_BAD_4ADDR;
3102 case NL80211_IFTYPE_STATION:
3103 if (!rx->sdata->u.mgd.use_4addr)
3104 return RX_DROP_U_BAD_4ADDR;
3106 case NL80211_IFTYPE_MESH_POINT:
3109 return RX_DROP_U_BAD_4ADDR;
3113 if (is_multicast_ether_addr(hdr->addr1) || !rx->sta)
3114 return RX_DROP_U_BAD_AMSDU;
3118 * We should not receive A-MSDUs on pre-HT connections,
3119 * and HT connections cannot use old ciphers. Thus drop
3120 * them, as in those cases we couldn't even have SPP
3123 switch (rx->key->conf.cipher) {
3124 case WLAN_CIPHER_SUITE_WEP40:
3125 case WLAN_CIPHER_SUITE_WEP104:
3126 case WLAN_CIPHER_SUITE_TKIP:
3127 return RX_DROP_U_BAD_AMSDU_CIPHER;
3133 return __ieee80211_rx_h_amsdu(rx, 0);
3136 static ieee80211_rx_result debug_noinline
3137 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
3139 struct ieee80211_sub_if_data *sdata = rx->sdata;
3140 struct ieee80211_local *local = rx->local;
3141 struct net_device *dev = sdata->dev;
3142 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
3143 __le16 fc = hdr->frame_control;
3144 ieee80211_rx_result res;
3147 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
3150 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3151 return RX_DROP_MONITOR;
3154 * Send unexpected-4addr-frame event to hostapd. For older versions,
3155 * also drop the frame to cooked monitor interfaces.
3157 if (ieee80211_has_a4(hdr->frame_control) &&
3158 sdata->vif.type == NL80211_IFTYPE_AP) {
3160 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3161 cfg80211_rx_unexpected_4addr_frame(
3162 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
3163 return RX_DROP_MONITOR;
3166 res = __ieee80211_data_to_8023(rx, &port_control);
3167 if (unlikely(res != RX_CONTINUE))
3170 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
3171 if (res != RX_CONTINUE)
3174 if (!ieee80211_frame_allowed(rx, fc))
3175 return RX_DROP_MONITOR;
3177 /* directly handle TDLS channel switch requests/responses */
3178 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3179 cpu_to_be16(ETH_P_TDLS))) {
3180 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3182 if (pskb_may_pull(rx->skb,
3183 offsetof(struct ieee80211_tdls_data, u)) &&
3184 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3185 tf->category == WLAN_CATEGORY_TDLS &&
3186 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3187 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
3188 rx->skb->protocol = cpu_to_be16(ETH_P_TDLS);
3189 __ieee80211_queue_skb_to_iface(sdata, rx->link_id,
3195 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3196 unlikely(port_control) && sdata->bss) {
3197 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3205 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3206 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
3207 !is_multicast_ether_addr(
3208 ((struct ethhdr *)rx->skb->data)->h_dest) &&
3209 (!local->scanning &&
3210 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3211 mod_timer(&local->dynamic_ps_timer, jiffies +
3212 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
3214 ieee80211_deliver_skb(rx);
3219 static ieee80211_rx_result debug_noinline
3220 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
3222 struct sk_buff *skb = rx->skb;
3223 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
3224 struct tid_ampdu_rx *tid_agg_rx;
3228 if (likely(!ieee80211_is_ctl(bar->frame_control)))
3231 if (ieee80211_is_back_req(bar->frame_control)) {
3233 __le16 control, start_seq_num;
3234 } __packed bar_data;
3235 struct ieee80211_event event = {
3236 .type = BAR_RX_EVENT,
3240 return RX_DROP_MONITOR;
3242 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3243 &bar_data, sizeof(bar_data)))
3244 return RX_DROP_MONITOR;
3246 tid = le16_to_cpu(bar_data.control) >> 12;
3248 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3249 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3250 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3251 WLAN_BACK_RECIPIENT,
3252 WLAN_REASON_QSTA_REQUIRE_SETUP);
3254 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3256 return RX_DROP_MONITOR;
3258 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
3259 event.u.ba.tid = tid;
3260 event.u.ba.ssn = start_seq_num;
3261 event.u.ba.sta = &rx->sta->sta;
3263 /* reset session timer */
3264 if (tid_agg_rx->timeout)
3265 mod_timer(&tid_agg_rx->session_timer,
3266 TU_TO_EXP_TIME(tid_agg_rx->timeout));
3268 spin_lock(&tid_agg_rx->reorder_lock);
3269 /* release stored frames up to start of BAR */
3270 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
3271 start_seq_num, frames);
3272 spin_unlock(&tid_agg_rx->reorder_lock);
3274 drv_event_callback(rx->local, rx->sdata, &event);
3281 * After this point, we only want management frames,
3282 * so we can drop all remaining control frames to
3283 * cooked monitor interfaces.
3285 return RX_DROP_MONITOR;
3288 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3289 struct ieee80211_mgmt *mgmt,
3292 struct ieee80211_local *local = sdata->local;
3293 struct sk_buff *skb;
3294 struct ieee80211_mgmt *resp;
3296 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
3297 /* Not to own unicast address */
3301 if (!ether_addr_equal(mgmt->sa, sdata->deflink.u.mgd.bssid) ||
3302 !ether_addr_equal(mgmt->bssid, sdata->deflink.u.mgd.bssid)) {
3303 /* Not from the current AP or not associated yet. */
3307 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3308 /* Too short SA Query request frame */
3312 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3316 skb_reserve(skb, local->hw.extra_tx_headroom);
3317 resp = skb_put_zero(skb, 24);
3318 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3319 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3320 memcpy(resp->bssid, sdata->deflink.u.mgd.bssid, ETH_ALEN);
3321 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3322 IEEE80211_STYPE_ACTION);
3323 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3324 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3325 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3326 memcpy(resp->u.action.u.sa_query.trans_id,
3327 mgmt->u.action.u.sa_query.trans_id,
3328 WLAN_SA_QUERY_TR_ID_LEN);
3330 ieee80211_tx_skb(sdata, skb);
3334 ieee80211_rx_check_bss_color_collision(struct ieee80211_rx_data *rx)
3336 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3337 const struct element *ie;
3340 if (!wiphy_ext_feature_isset(rx->local->hw.wiphy,
3341 NL80211_EXT_FEATURE_BSS_COLOR))
3344 if (ieee80211_hw_check(&rx->local->hw, DETECTS_COLOR_COLLISION))
3347 if (rx->sdata->vif.bss_conf.csa_active)
3350 baselen = mgmt->u.beacon.variable - rx->skb->data;
3351 if (baselen > rx->skb->len)
3354 ie = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION,
3355 mgmt->u.beacon.variable,
3356 rx->skb->len - baselen);
3357 if (ie && ie->datalen >= sizeof(struct ieee80211_he_operation) &&
3358 ie->datalen >= ieee80211_he_oper_size(ie->data + 1)) {
3359 struct ieee80211_bss_conf *bss_conf = &rx->sdata->vif.bss_conf;
3360 const struct ieee80211_he_operation *he_oper;
3363 he_oper = (void *)(ie->data + 1);
3364 if (le32_get_bits(he_oper->he_oper_params,
3365 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED))
3368 color = le32_get_bits(he_oper->he_oper_params,
3369 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3370 if (color == bss_conf->he_bss_color.color)
3371 ieee80211_obss_color_collision_notify(&rx->sdata->vif,
3377 static ieee80211_rx_result debug_noinline
3378 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3380 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3381 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3383 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3387 * From here on, look only at management frames.
3388 * Data and control frames are already handled,
3389 * and unknown (reserved) frames are useless.
3391 if (rx->skb->len < 24)
3392 return RX_DROP_MONITOR;
3394 if (!ieee80211_is_mgmt(mgmt->frame_control))
3395 return RX_DROP_MONITOR;
3397 /* drop too small action frames */
3398 if (ieee80211_is_action(mgmt->frame_control) &&
3399 rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
3400 return RX_DROP_U_RUNT_ACTION;
3402 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3403 ieee80211_is_beacon(mgmt->frame_control) &&
3404 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3407 /* sw bss color collision detection */
3408 ieee80211_rx_check_bss_color_collision(rx);
3410 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3411 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3412 sig = status->signal;
3414 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3415 rx->skb->data, rx->skb->len,
3416 ieee80211_rx_status_to_khz(status),
3418 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3421 return ieee80211_drop_unencrypted_mgmt(rx);
3425 ieee80211_process_rx_twt_action(struct ieee80211_rx_data *rx)
3427 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)rx->skb->data;
3428 struct ieee80211_sub_if_data *sdata = rx->sdata;
3430 /* TWT actions are only supported in AP for the moment */
3431 if (sdata->vif.type != NL80211_IFTYPE_AP)
3434 if (!rx->local->ops->add_twt_setup)
3437 if (!sdata->vif.bss_conf.twt_responder)
3443 switch (mgmt->u.action.u.s1g.action_code) {
3444 case WLAN_S1G_TWT_SETUP: {
3445 struct ieee80211_twt_setup *twt;
3447 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3448 1 + /* action code */
3449 sizeof(struct ieee80211_twt_setup) +
3450 2 /* TWT req_type agrt */)
3453 twt = (void *)mgmt->u.action.u.s1g.variable;
3454 if (twt->element_id != WLAN_EID_S1G_TWT)
3457 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE +
3458 4 + /* action code + token + tlv */
3462 return true; /* queue the frame */
3464 case WLAN_S1G_TWT_TEARDOWN:
3465 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE + 2)
3468 return true; /* queue the frame */
3476 static ieee80211_rx_result debug_noinline
3477 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3479 struct ieee80211_local *local = rx->local;
3480 struct ieee80211_sub_if_data *sdata = rx->sdata;
3481 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3482 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3483 int len = rx->skb->len;
3485 if (!ieee80211_is_action(mgmt->frame_control))
3488 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3489 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3490 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3491 return RX_DROP_U_ACTION_UNKNOWN_SRC;
3493 switch (mgmt->u.action.category) {
3494 case WLAN_CATEGORY_HT:
3495 /* reject HT action frames from stations not supporting HT */
3496 if (!rx->link_sta->pub->ht_cap.ht_supported)
3499 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3500 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3501 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3502 sdata->vif.type != NL80211_IFTYPE_AP &&
3503 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3506 /* verify action & smps_control/chanwidth are present */
3507 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3510 switch (mgmt->u.action.u.ht_smps.action) {
3511 case WLAN_HT_ACTION_SMPS: {
3512 struct ieee80211_supported_band *sband;
3513 enum ieee80211_smps_mode smps_mode;
3514 struct sta_opmode_info sta_opmode = {};
3516 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3517 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3520 /* convert to HT capability */
3521 switch (mgmt->u.action.u.ht_smps.smps_control) {
3522 case WLAN_HT_SMPS_CONTROL_DISABLED:
3523 smps_mode = IEEE80211_SMPS_OFF;
3525 case WLAN_HT_SMPS_CONTROL_STATIC:
3526 smps_mode = IEEE80211_SMPS_STATIC;
3528 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3529 smps_mode = IEEE80211_SMPS_DYNAMIC;
3535 /* if no change do nothing */
3536 if (rx->link_sta->pub->smps_mode == smps_mode)
3538 rx->link_sta->pub->smps_mode = smps_mode;
3539 sta_opmode.smps_mode =
3540 ieee80211_smps_mode_to_smps_mode(smps_mode);
3541 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3543 sband = rx->local->hw.wiphy->bands[status->band];
3545 rate_control_rate_update(local, sband, rx->sta, 0,
3546 IEEE80211_RC_SMPS_CHANGED);
3547 cfg80211_sta_opmode_change_notify(sdata->dev,
3553 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3554 struct ieee80211_supported_band *sband;
3555 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
3556 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
3557 struct sta_opmode_info sta_opmode = {};
3559 /* If it doesn't support 40 MHz it can't change ... */
3560 if (!(rx->link_sta->pub->ht_cap.cap &
3561 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3564 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
3565 max_bw = IEEE80211_STA_RX_BW_20;
3567 max_bw = ieee80211_sta_cap_rx_bw(rx->link_sta);
3569 /* set cur_max_bandwidth and recalc sta bw */
3570 rx->link_sta->cur_max_bandwidth = max_bw;
3571 new_bw = ieee80211_sta_cur_vht_bw(rx->link_sta);
3573 if (rx->link_sta->pub->bandwidth == new_bw)
3576 rx->link_sta->pub->bandwidth = new_bw;
3577 sband = rx->local->hw.wiphy->bands[status->band];
3579 ieee80211_sta_rx_bw_to_chan_width(rx->link_sta);
3580 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
3582 rate_control_rate_update(local, sband, rx->sta, 0,
3583 IEEE80211_RC_BW_CHANGED);
3584 cfg80211_sta_opmode_change_notify(sdata->dev,
3595 case WLAN_CATEGORY_PUBLIC:
3596 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3598 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3602 if (!ether_addr_equal(mgmt->bssid, sdata->deflink.u.mgd.bssid))
3604 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3605 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3607 if (len < offsetof(struct ieee80211_mgmt,
3608 u.action.u.ext_chan_switch.variable))
3611 case WLAN_CATEGORY_VHT:
3612 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3613 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3614 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3615 sdata->vif.type != NL80211_IFTYPE_AP &&
3616 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3619 /* verify action code is present */
3620 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3623 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3624 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3625 /* verify opmode is present */
3626 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3630 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3631 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3639 case WLAN_CATEGORY_BACK:
3640 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3641 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3642 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3643 sdata->vif.type != NL80211_IFTYPE_AP &&
3644 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3647 /* verify action_code is present */
3648 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3651 switch (mgmt->u.action.u.addba_req.action_code) {
3652 case WLAN_ACTION_ADDBA_REQ:
3653 if (len < (IEEE80211_MIN_ACTION_SIZE +
3654 sizeof(mgmt->u.action.u.addba_req)))
3657 case WLAN_ACTION_ADDBA_RESP:
3658 if (len < (IEEE80211_MIN_ACTION_SIZE +
3659 sizeof(mgmt->u.action.u.addba_resp)))
3662 case WLAN_ACTION_DELBA:
3663 if (len < (IEEE80211_MIN_ACTION_SIZE +
3664 sizeof(mgmt->u.action.u.delba)))
3672 case WLAN_CATEGORY_SPECTRUM_MGMT:
3673 /* verify action_code is present */
3674 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3677 switch (mgmt->u.action.u.measurement.action_code) {
3678 case WLAN_ACTION_SPCT_MSR_REQ:
3679 if (status->band != NL80211_BAND_5GHZ)
3682 if (len < (IEEE80211_MIN_ACTION_SIZE +
3683 sizeof(mgmt->u.action.u.measurement)))
3686 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3689 ieee80211_process_measurement_req(sdata, mgmt, len);
3691 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3693 if (len < (IEEE80211_MIN_ACTION_SIZE +
3694 sizeof(mgmt->u.action.u.chan_switch)))
3697 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3698 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3699 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3702 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3703 bssid = sdata->deflink.u.mgd.bssid;
3704 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3705 bssid = sdata->u.ibss.bssid;
3706 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3711 if (!ether_addr_equal(mgmt->bssid, bssid))
3718 case WLAN_CATEGORY_SELF_PROTECTED:
3719 if (len < (IEEE80211_MIN_ACTION_SIZE +
3720 sizeof(mgmt->u.action.u.self_prot.action_code)))
3723 switch (mgmt->u.action.u.self_prot.action_code) {
3724 case WLAN_SP_MESH_PEERING_OPEN:
3725 case WLAN_SP_MESH_PEERING_CLOSE:
3726 case WLAN_SP_MESH_PEERING_CONFIRM:
3727 if (!ieee80211_vif_is_mesh(&sdata->vif))
3729 if (sdata->u.mesh.user_mpm)
3730 /* userspace handles this frame */
3733 case WLAN_SP_MGK_INFORM:
3734 case WLAN_SP_MGK_ACK:
3735 if (!ieee80211_vif_is_mesh(&sdata->vif))
3740 case WLAN_CATEGORY_MESH_ACTION:
3741 if (len < (IEEE80211_MIN_ACTION_SIZE +
3742 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3745 if (!ieee80211_vif_is_mesh(&sdata->vif))
3747 if (mesh_action_is_path_sel(mgmt) &&
3748 !mesh_path_sel_is_hwmp(sdata))
3751 case WLAN_CATEGORY_S1G:
3752 if (len < offsetofend(typeof(*mgmt),
3753 u.action.u.s1g.action_code))
3756 switch (mgmt->u.action.u.s1g.action_code) {
3757 case WLAN_S1G_TWT_SETUP:
3758 case WLAN_S1G_TWT_TEARDOWN:
3759 if (ieee80211_process_rx_twt_action(rx))
3771 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3772 /* will return in the next handlers */
3777 rx->link_sta->rx_stats.packets++;
3778 dev_kfree_skb(rx->skb);
3782 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
3786 static ieee80211_rx_result debug_noinline
3787 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3789 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3790 struct cfg80211_rx_info info = {
3791 .freq = ieee80211_rx_status_to_khz(status),
3792 .buf = rx->skb->data,
3793 .len = rx->skb->len,
3794 .link_id = rx->link_id,
3795 .have_link_id = rx->link_id >= 0,
3798 /* skip known-bad action frames and return them in the next handler */
3799 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3803 * Getting here means the kernel doesn't know how to handle
3804 * it, but maybe userspace does ... include returned frames
3805 * so userspace can register for those to know whether ones
3806 * it transmitted were processed or returned.
3809 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3810 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3811 info.sig_dbm = status->signal;
3813 if (ieee80211_is_timing_measurement(rx->skb) ||
3814 ieee80211_is_ftm(rx->skb)) {
3815 info.rx_tstamp = ktime_to_ns(skb_hwtstamps(rx->skb)->hwtstamp);
3816 info.ack_tstamp = ktime_to_ns(status->ack_tx_hwtstamp);
3819 if (cfg80211_rx_mgmt_ext(&rx->sdata->wdev, &info)) {
3821 rx->link_sta->rx_stats.packets++;
3822 dev_kfree_skb(rx->skb);
3829 static ieee80211_rx_result debug_noinline
3830 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3832 struct ieee80211_sub_if_data *sdata = rx->sdata;
3833 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3834 int len = rx->skb->len;
3836 if (!ieee80211_is_action(mgmt->frame_control))
3839 switch (mgmt->u.action.category) {
3840 case WLAN_CATEGORY_SA_QUERY:
3841 if (len < (IEEE80211_MIN_ACTION_SIZE +
3842 sizeof(mgmt->u.action.u.sa_query)))
3845 switch (mgmt->u.action.u.sa_query.action) {
3846 case WLAN_ACTION_SA_QUERY_REQUEST:
3847 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3849 ieee80211_process_sa_query_req(sdata, mgmt, len);
3859 rx->link_sta->rx_stats.packets++;
3860 dev_kfree_skb(rx->skb);
3864 static ieee80211_rx_result debug_noinline
3865 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3867 struct ieee80211_local *local = rx->local;
3868 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3869 struct sk_buff *nskb;
3870 struct ieee80211_sub_if_data *sdata = rx->sdata;
3871 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3873 if (!ieee80211_is_action(mgmt->frame_control))
3877 * For AP mode, hostapd is responsible for handling any action
3878 * frames that we didn't handle, including returning unknown
3879 * ones. For all other modes we will return them to the sender,
3880 * setting the 0x80 bit in the action category, as required by
3881 * 802.11-2012 9.24.4.
3882 * Newer versions of hostapd shall also use the management frame
3883 * registration mechanisms, but older ones still use cooked
3884 * monitor interfaces so push all frames there.
3886 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3887 (sdata->vif.type == NL80211_IFTYPE_AP ||
3888 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3889 return RX_DROP_MONITOR;
3891 if (is_multicast_ether_addr(mgmt->da))
3892 return RX_DROP_MONITOR;
3894 /* do not return rejected action frames */
3895 if (mgmt->u.action.category & 0x80)
3896 return RX_DROP_U_REJECTED_ACTION_RESPONSE;
3898 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3901 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3903 nmgmt->u.action.category |= 0x80;
3904 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3905 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3907 memset(nskb->cb, 0, sizeof(nskb->cb));
3909 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3910 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3912 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3913 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3914 IEEE80211_TX_CTL_NO_CCK_RATE;
3915 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3917 local->hw.offchannel_tx_hw_queue;
3920 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7, -1,
3923 dev_kfree_skb(rx->skb);
3927 static ieee80211_rx_result debug_noinline
3928 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3930 struct ieee80211_sub_if_data *sdata = rx->sdata;
3931 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3933 if (!ieee80211_is_ext(hdr->frame_control))
3936 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3937 return RX_DROP_MONITOR;
3939 /* for now only beacons are ext, so queue them */
3940 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
3945 static ieee80211_rx_result debug_noinline
3946 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3948 struct ieee80211_sub_if_data *sdata = rx->sdata;
3949 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3952 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3954 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3955 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3956 sdata->vif.type != NL80211_IFTYPE_OCB &&
3957 sdata->vif.type != NL80211_IFTYPE_STATION)
3958 return RX_DROP_MONITOR;
3961 case cpu_to_le16(IEEE80211_STYPE_AUTH):
3962 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3963 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3964 /* process for all: mesh, mlme, ibss */
3966 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3967 if (is_multicast_ether_addr(mgmt->da) &&
3968 !is_broadcast_ether_addr(mgmt->da))
3969 return RX_DROP_MONITOR;
3971 /* process only for station/IBSS */
3972 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3973 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3974 return RX_DROP_MONITOR;
3976 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3977 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3978 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3979 if (is_multicast_ether_addr(mgmt->da) &&
3980 !is_broadcast_ether_addr(mgmt->da))
3981 return RX_DROP_MONITOR;
3983 /* process only for station */
3984 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3985 return RX_DROP_MONITOR;
3987 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3988 /* process only for ibss and mesh */
3989 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3990 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3991 return RX_DROP_MONITOR;
3994 return RX_DROP_MONITOR;
3997 ieee80211_queue_skb_to_iface(sdata, rx->link_id, rx->sta, rx->skb);
4002 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
4003 struct ieee80211_rate *rate,
4004 ieee80211_rx_result reason)
4006 struct ieee80211_sub_if_data *sdata;
4007 struct ieee80211_local *local = rx->local;
4008 struct sk_buff *skb = rx->skb, *skb2;
4009 struct net_device *prev_dev = NULL;
4010 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4011 int needed_headroom;
4014 * If cooked monitor has been processed already, then
4015 * don't do it again. If not, set the flag.
4017 if (rx->flags & IEEE80211_RX_CMNTR)
4019 rx->flags |= IEEE80211_RX_CMNTR;
4021 /* If there are no cooked monitor interfaces, just free the SKB */
4022 if (!local->cooked_mntrs)
4025 /* room for the radiotap header based on driver features */
4026 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
4028 if (skb_headroom(skb) < needed_headroom &&
4029 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
4032 /* prepend radiotap information */
4033 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
4036 skb_reset_mac_header(skb);
4037 skb->ip_summed = CHECKSUM_UNNECESSARY;
4038 skb->pkt_type = PACKET_OTHERHOST;
4039 skb->protocol = htons(ETH_P_802_2);
4041 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4042 if (!ieee80211_sdata_running(sdata))
4045 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
4046 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
4050 skb2 = skb_clone(skb, GFP_ATOMIC);
4052 skb2->dev = prev_dev;
4053 netif_receive_skb(skb2);
4057 prev_dev = sdata->dev;
4058 dev_sw_netstats_rx_add(sdata->dev, skb->len);
4062 skb->dev = prev_dev;
4063 netif_receive_skb(skb);
4068 kfree_skb_reason(skb, (__force u32)reason);
4071 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
4072 ieee80211_rx_result res)
4074 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4075 struct ieee80211_supported_band *sband;
4076 struct ieee80211_rate *rate = NULL;
4078 if (res == RX_QUEUED) {
4079 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
4083 if (res != RX_CONTINUE) {
4084 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
4086 rx->link_sta->rx_stats.dropped++;
4089 if (u32_get_bits((__force u32)res, SKB_DROP_REASON_SUBSYS_MASK) ==
4090 SKB_DROP_REASON_SUBSYS_MAC80211_UNUSABLE) {
4091 kfree_skb_reason(rx->skb, (__force u32)res);
4095 sband = rx->local->hw.wiphy->bands[status->band];
4096 if (status->encoding == RX_ENC_LEGACY)
4097 rate = &sband->bitrates[status->rate_idx];
4099 ieee80211_rx_cooked_monitor(rx, rate, res);
4102 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
4103 struct sk_buff_head *frames)
4105 ieee80211_rx_result res = RX_DROP_MONITOR;
4106 struct sk_buff *skb;
4108 #define CALL_RXH(rxh) \
4111 if (res != RX_CONTINUE) \
4115 /* Lock here to avoid hitting all of the data used in the RX
4116 * path (e.g. key data, station data, ...) concurrently when
4117 * a frame is released from the reorder buffer due to timeout
4118 * from the timer, potentially concurrently with RX from the
4121 spin_lock_bh(&rx->local->rx_path_lock);
4123 while ((skb = __skb_dequeue(frames))) {
4125 * all the other fields are valid across frames
4126 * that belong to an aMPDU since they are on the
4127 * same TID from the same station
4131 if (WARN_ON_ONCE(!rx->link))
4134 CALL_RXH(ieee80211_rx_h_check_more_data);
4135 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
4136 CALL_RXH(ieee80211_rx_h_sta_process);
4137 CALL_RXH(ieee80211_rx_h_decrypt);
4138 CALL_RXH(ieee80211_rx_h_defragment);
4139 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
4140 /* must be after MMIC verify so header is counted in MPDU mic */
4141 CALL_RXH(ieee80211_rx_h_amsdu);
4142 CALL_RXH(ieee80211_rx_h_data);
4144 /* special treatment -- needs the queue */
4145 res = ieee80211_rx_h_ctrl(rx, frames);
4146 if (res != RX_CONTINUE)
4149 CALL_RXH(ieee80211_rx_h_mgmt_check);
4150 CALL_RXH(ieee80211_rx_h_action);
4151 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
4152 CALL_RXH(ieee80211_rx_h_action_post_userspace);
4153 CALL_RXH(ieee80211_rx_h_action_return);
4154 CALL_RXH(ieee80211_rx_h_ext);
4155 CALL_RXH(ieee80211_rx_h_mgmt);
4158 ieee80211_rx_handlers_result(rx, res);
4163 spin_unlock_bh(&rx->local->rx_path_lock);
4166 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
4168 struct sk_buff_head reorder_release;
4169 ieee80211_rx_result res = RX_DROP_MONITOR;
4171 __skb_queue_head_init(&reorder_release);
4173 #define CALL_RXH(rxh) \
4176 if (res != RX_CONTINUE) \
4180 CALL_RXH(ieee80211_rx_h_check_dup);
4181 CALL_RXH(ieee80211_rx_h_check);
4183 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
4185 ieee80211_rx_handlers(rx, &reorder_release);
4189 ieee80211_rx_handlers_result(rx, res);
4195 ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
4197 return !!(sta->valid_links & BIT(link_id));
4200 static bool ieee80211_rx_data_set_link(struct ieee80211_rx_data *rx,
4203 rx->link_id = link_id;
4204 rx->link = rcu_dereference(rx->sdata->link[link_id]);
4209 if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id))
4212 rx->link_sta = rcu_dereference(rx->sta->link[link_id]);
4214 return rx->link && rx->link_sta;
4217 static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx,
4218 struct sta_info *sta, int link_id)
4220 rx->link_id = link_id;
4224 rx->local = sta->sdata->local;
4226 rx->sdata = sta->sdata;
4227 rx->link_sta = &sta->deflink;
4229 rx->link_sta = NULL;
4233 rx->link = &rx->sdata->deflink;
4234 else if (!ieee80211_rx_data_set_link(rx, link_id))
4241 * This function makes calls into the RX path, therefore
4242 * it has to be invoked under RCU read lock.
4244 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
4246 struct sk_buff_head frames;
4247 struct ieee80211_rx_data rx = {
4248 /* This is OK -- must be QoS data frame */
4249 .security_idx = tid,
4252 struct tid_ampdu_rx *tid_agg_rx;
4255 /* FIXME: statistics won't be right with this */
4256 if (sta->sta.valid_links)
4257 link_id = ffs(sta->sta.valid_links) - 1;
4259 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
4262 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4266 __skb_queue_head_init(&frames);
4268 spin_lock(&tid_agg_rx->reorder_lock);
4269 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4270 spin_unlock(&tid_agg_rx->reorder_lock);
4272 if (!skb_queue_empty(&frames)) {
4273 struct ieee80211_event event = {
4274 .type = BA_FRAME_TIMEOUT,
4276 .u.ba.sta = &sta->sta,
4278 drv_event_callback(rx.local, rx.sdata, &event);
4281 ieee80211_rx_handlers(&rx, &frames);
4284 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
4285 u16 ssn, u64 filtered,
4288 struct ieee80211_local *local;
4289 struct sta_info *sta;
4290 struct tid_ampdu_rx *tid_agg_rx;
4291 struct sk_buff_head frames;
4292 struct ieee80211_rx_data rx = {
4293 /* This is OK -- must be QoS data frame */
4294 .security_idx = tid,
4299 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
4302 __skb_queue_head_init(&frames);
4304 sta = container_of(pubsta, struct sta_info, sta);
4306 local = sta->sdata->local;
4307 WARN_ONCE(local->hw.max_rx_aggregation_subframes > 64,
4308 "RX BA marker can't support max_rx_aggregation_subframes %u > 64\n",
4309 local->hw.max_rx_aggregation_subframes);
4311 if (!ieee80211_rx_data_set_sta(&rx, sta, -1))
4315 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
4319 spin_lock_bh(&tid_agg_rx->reorder_lock);
4321 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
4324 /* release all frames in the reorder buffer */
4325 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
4326 IEEE80211_SN_MODULO;
4327 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
4329 /* update ssn to match received ssn */
4330 tid_agg_rx->head_seq_num = ssn;
4332 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
4336 /* handle the case that received ssn is behind the mac ssn.
4337 * it can be tid_agg_rx->buf_size behind and still be valid */
4338 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4339 if (diff >= tid_agg_rx->buf_size) {
4340 tid_agg_rx->reorder_buf_filtered = 0;
4343 filtered = filtered >> diff;
4347 for (i = 0; i < tid_agg_rx->buf_size; i++) {
4348 int index = (ssn + i) % tid_agg_rx->buf_size;
4350 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4351 if (filtered & BIT_ULL(i))
4352 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4355 /* now process also frames that the filter marking released */
4356 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4359 spin_unlock_bh(&tid_agg_rx->reorder_lock);
4361 ieee80211_rx_handlers(&rx, &frames);
4366 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4368 /* main receive path */
4370 static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
4372 return ether_addr_equal(raddr, addr) ||
4373 is_broadcast_ether_addr(raddr);
4376 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
4378 struct ieee80211_sub_if_data *sdata = rx->sdata;
4379 struct sk_buff *skb = rx->skb;
4380 struct ieee80211_hdr *hdr = (void *)skb->data;
4381 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4382 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
4383 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4384 ieee80211_is_s1g_beacon(hdr->frame_control);
4386 switch (sdata->vif.type) {
4387 case NL80211_IFTYPE_STATION:
4388 if (!bssid && !sdata->u.mgd.use_4addr)
4390 if (ieee80211_is_first_frag(hdr->seq_ctrl) &&
4391 ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4395 return ieee80211_is_our_addr(sdata, hdr->addr1, &rx->link_id);
4396 case NL80211_IFTYPE_ADHOC:
4399 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
4400 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
4401 !is_valid_ether_addr(hdr->addr2))
4403 if (ieee80211_is_beacon(hdr->frame_control))
4405 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
4408 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4412 if (status->encoding != RX_ENC_LEGACY)
4413 rate_idx = 0; /* TODO: HT/VHT rates */
4415 rate_idx = status->rate_idx;
4416 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4420 case NL80211_IFTYPE_OCB:
4423 if (!ieee80211_is_data_present(hdr->frame_control))
4425 if (!is_broadcast_ether_addr(bssid))
4428 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
4432 if (status->encoding != RX_ENC_LEGACY)
4433 rate_idx = 0; /* TODO: HT rates */
4435 rate_idx = status->rate_idx;
4436 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4440 case NL80211_IFTYPE_MESH_POINT:
4441 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4445 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4446 case NL80211_IFTYPE_AP_VLAN:
4447 case NL80211_IFTYPE_AP:
4449 return ieee80211_is_our_addr(sdata, hdr->addr1,
4452 if (!is_broadcast_ether_addr(bssid) &&
4453 !ieee80211_is_our_addr(sdata, bssid, NULL)) {
4455 * Accept public action frames even when the
4456 * BSSID doesn't match, this is used for P2P
4457 * and location updates. Note that mac80211
4458 * itself never looks at these frames.
4461 !ieee80211_is_our_addr(sdata, hdr->addr1,
4464 if (ieee80211_is_public_action(hdr, skb->len))
4466 return ieee80211_is_beacon(hdr->frame_control);
4469 if (!ieee80211_has_tods(hdr->frame_control)) {
4470 /* ignore data frames to TDLS-peers */
4471 if (ieee80211_is_data(hdr->frame_control))
4473 /* ignore action frames to TDLS-peers */
4474 if (ieee80211_is_action(hdr->frame_control) &&
4475 !is_broadcast_ether_addr(bssid) &&
4476 !ether_addr_equal(bssid, hdr->addr1))
4481 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4482 * the BSSID - we've checked that already but may have accepted
4483 * the wildcard (ff:ff:ff:ff:ff:ff).
4486 * The BSSID of the Data frame is determined as follows:
4487 * a) If the STA is contained within an AP or is associated
4488 * with an AP, the BSSID is the address currently in use
4489 * by the STA contained in the AP.
4491 * So we should not accept data frames with an address that's
4494 * Accepting it also opens a security problem because stations
4495 * could encrypt it with the GTK and inject traffic that way.
4497 if (ieee80211_is_data(hdr->frame_control) && multicast)
4501 case NL80211_IFTYPE_P2P_DEVICE:
4502 return ieee80211_is_public_action(hdr, skb->len) ||
4503 ieee80211_is_probe_req(hdr->frame_control) ||
4504 ieee80211_is_probe_resp(hdr->frame_control) ||
4505 ieee80211_is_beacon(hdr->frame_control);
4506 case NL80211_IFTYPE_NAN:
4507 /* Currently no frames on NAN interface are allowed */
4517 void ieee80211_check_fast_rx(struct sta_info *sta)
4519 struct ieee80211_sub_if_data *sdata = sta->sdata;
4520 struct ieee80211_local *local = sdata->local;
4521 struct ieee80211_key *key;
4522 struct ieee80211_fast_rx fastrx = {
4524 .vif_type = sdata->vif.type,
4525 .control_port_protocol = sdata->control_port_protocol,
4526 }, *old, *new = NULL;
4528 bool set_offload = false;
4529 bool assign = false;
4532 /* use sparse to check that we don't return without updating */
4533 __acquire(check_fast_rx);
4535 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4536 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4537 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4538 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4540 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4542 /* fast-rx doesn't do reordering */
4543 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4544 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4547 switch (sdata->vif.type) {
4548 case NL80211_IFTYPE_STATION:
4549 if (sta->sta.tdls) {
4550 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4551 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4552 fastrx.expected_ds_bits = 0;
4554 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4555 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4556 fastrx.expected_ds_bits =
4557 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4560 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4561 fastrx.expected_ds_bits |=
4562 cpu_to_le16(IEEE80211_FCTL_TODS);
4563 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4564 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4567 if (!sdata->u.mgd.powersave)
4570 /* software powersave is a huge mess, avoid all of it */
4571 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4573 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4574 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4577 case NL80211_IFTYPE_AP_VLAN:
4578 case NL80211_IFTYPE_AP:
4579 /* parallel-rx requires this, at least with calls to
4580 * ieee80211_sta_ps_transition()
4582 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4584 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4585 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4586 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4588 fastrx.internal_forward =
4589 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4590 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4591 !sdata->u.vlan.sta);
4593 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4594 sdata->u.vlan.sta) {
4595 fastrx.expected_ds_bits |=
4596 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4597 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4598 fastrx.internal_forward = 0;
4602 case NL80211_IFTYPE_MESH_POINT:
4603 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_FROMDS |
4604 IEEE80211_FCTL_TODS);
4605 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4606 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4612 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4616 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4618 key = rcu_dereference(sdata->default_unicast_key);
4620 switch (key->conf.cipher) {
4621 case WLAN_CIPHER_SUITE_TKIP:
4622 /* we don't want to deal with MMIC in fast-rx */
4624 case WLAN_CIPHER_SUITE_CCMP:
4625 case WLAN_CIPHER_SUITE_CCMP_256:
4626 case WLAN_CIPHER_SUITE_GCMP:
4627 case WLAN_CIPHER_SUITE_GCMP_256:
4630 /* We also don't want to deal with
4631 * WEP or cipher scheme.
4637 fastrx.icv_len = key->conf.icv_len;
4644 __release(check_fast_rx);
4647 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4649 offload_flags = get_bss_sdata(sdata)->vif.offload_flags;
4650 offload = offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED;
4652 if (assign && offload)
4653 set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4655 set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4658 drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4660 spin_lock_bh(&sta->lock);
4661 old = rcu_dereference_protected(sta->fast_rx, true);
4662 rcu_assign_pointer(sta->fast_rx, new);
4663 spin_unlock_bh(&sta->lock);
4666 kfree_rcu(old, rcu_head);
4669 void ieee80211_clear_fast_rx(struct sta_info *sta)
4671 struct ieee80211_fast_rx *old;
4673 spin_lock_bh(&sta->lock);
4674 old = rcu_dereference_protected(sta->fast_rx, true);
4675 RCU_INIT_POINTER(sta->fast_rx, NULL);
4676 spin_unlock_bh(&sta->lock);
4679 kfree_rcu(old, rcu_head);
4682 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4684 struct ieee80211_local *local = sdata->local;
4685 struct sta_info *sta;
4687 lockdep_assert_wiphy(local->hw.wiphy);
4689 list_for_each_entry(sta, &local->sta_list, list) {
4690 if (sdata != sta->sdata &&
4691 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4693 ieee80211_check_fast_rx(sta);
4697 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4699 struct ieee80211_local *local = sdata->local;
4701 lockdep_assert_wiphy(local->hw.wiphy);
4703 __ieee80211_check_fast_rx_iface(sdata);
4706 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4707 struct ieee80211_fast_rx *fast_rx,
4710 struct ieee80211_sta_rx_stats *stats;
4711 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4712 struct sta_info *sta = rx->sta;
4713 struct link_sta_info *link_sta;
4714 struct sk_buff *skb = rx->skb;
4715 void *sa = skb->data + ETH_ALEN;
4716 void *da = skb->data;
4718 if (rx->link_id >= 0) {
4719 link_sta = rcu_dereference(sta->link[rx->link_id]);
4720 if (WARN_ON_ONCE(!link_sta)) {
4721 dev_kfree_skb(rx->skb);
4725 link_sta = &sta->deflink;
4728 stats = &link_sta->rx_stats;
4729 if (fast_rx->uses_rss)
4730 stats = this_cpu_ptr(link_sta->pcpu_rx_stats);
4732 /* statistics part of ieee80211_rx_h_sta_process() */
4733 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4734 stats->last_signal = status->signal;
4735 if (!fast_rx->uses_rss)
4736 ewma_signal_add(&link_sta->rx_stats_avg.signal,
4740 if (status->chains) {
4743 stats->chains = status->chains;
4744 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4745 int signal = status->chain_signal[i];
4747 if (!(status->chains & BIT(i)))
4750 stats->chain_signal_last[i] = signal;
4751 if (!fast_rx->uses_rss)
4752 ewma_signal_add(&link_sta->rx_stats_avg.chain_signal[i],
4756 /* end of statistics */
4758 stats->last_rx = jiffies;
4759 stats->last_rate = sta_stats_encode_rate(status);
4764 skb->dev = fast_rx->dev;
4766 dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4768 /* The seqno index has the same property as needed
4769 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4770 * for non-QoS-data frames. Here we know it's a data
4771 * frame, so count MSDUs.
4773 u64_stats_update_begin(&stats->syncp);
4774 stats->msdu[rx->seqno_idx]++;
4775 stats->bytes += orig_len;
4776 u64_stats_update_end(&stats->syncp);
4778 if (fast_rx->internal_forward) {
4779 struct sk_buff *xmit_skb = NULL;
4780 if (is_multicast_ether_addr(da)) {
4781 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4782 } else if (!ether_addr_equal(da, sa) &&
4783 sta_info_get(rx->sdata, da)) {
4790 * Send to wireless media and increase priority by 256
4791 * to keep the received priority instead of
4792 * reclassifying the frame (see cfg80211_classify8021d).
4794 xmit_skb->priority += 256;
4795 xmit_skb->protocol = htons(ETH_P_802_3);
4796 skb_reset_network_header(xmit_skb);
4797 skb_reset_mac_header(xmit_skb);
4798 dev_queue_xmit(xmit_skb);
4805 /* deliver to local stack */
4806 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4807 ieee80211_deliver_skb_to_local_stack(skb, rx);
4810 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4811 struct ieee80211_fast_rx *fast_rx)
4813 struct sk_buff *skb = rx->skb;
4814 struct ieee80211_hdr *hdr = (void *)skb->data;
4815 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4816 static ieee80211_rx_result res;
4817 int orig_len = skb->len;
4818 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4819 int snap_offs = hdrlen;
4821 u8 snap[sizeof(rfc1042_header)];
4823 } *payload __aligned(2);
4827 } addrs __aligned(2);
4828 struct ieee80211_sta_rx_stats *stats;
4830 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4831 * to a common data structure; drivers can implement that per queue
4832 * but we don't have that information in mac80211
4834 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4837 #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4839 /* If using encryption, we also need to have:
4840 * - PN_VALIDATED: similar, but the implementation is tricky
4841 * - DECRYPTED: necessary for PN_VALIDATED
4844 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4847 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4850 if (unlikely(ieee80211_is_frag(hdr)))
4853 /* Since our interface address cannot be multicast, this
4854 * implicitly also rejects multicast frames without the
4857 * We shouldn't get any *data* frames not addressed to us
4858 * (AP mode will accept multicast *management* frames), but
4859 * punting here will make it go through the full checks in
4860 * ieee80211_accept_frame().
4862 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4865 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4866 IEEE80211_FCTL_TODS)) !=
4867 fast_rx->expected_ds_bits)
4870 /* assign the key to drop unencrypted frames (later)
4871 * and strip the IV/MIC if necessary
4873 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4874 /* GCMP header length is the same */
4875 snap_offs += IEEE80211_CCMP_HDR_LEN;
4878 if (!ieee80211_vif_is_mesh(&rx->sdata->vif) &&
4879 !(status->rx_flags & IEEE80211_RX_AMSDU)) {
4880 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4883 payload = (void *)(skb->data + snap_offs);
4885 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4888 /* Don't handle these here since they require special code.
4889 * Accept AARP and IPX even though they should come with a
4890 * bridge-tunnel header - but if we get them this way then
4891 * there's little point in discarding them.
4893 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4894 payload->proto == fast_rx->control_port_protocol))
4898 /* after this point, don't punt to the slowpath! */
4900 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4901 pskb_trim(skb, skb->len - fast_rx->icv_len))
4904 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4907 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4908 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4915 /* do the header conversion - first grab the addresses */
4916 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4917 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4918 if (ieee80211_vif_is_mesh(&rx->sdata->vif)) {
4919 skb_pull(skb, snap_offs - 2);
4920 put_unaligned_be16(skb->len - 2, skb->data);
4922 skb_postpull_rcsum(skb, skb->data + snap_offs,
4923 sizeof(rfc1042_header) + 2);
4925 /* remove the SNAP but leave the ethertype */
4926 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4928 /* push the addresses in front */
4929 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4931 res = ieee80211_rx_mesh_data(rx->sdata, rx->sta, rx->skb);
4941 ieee80211_rx_8023(rx, fast_rx, orig_len);
4947 if (fast_rx->uses_rss)
4948 stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats);
4950 stats = &rx->link_sta->rx_stats;
4957 * This function returns whether or not the SKB
4958 * was destined for RX processing or not, which,
4959 * if consume is true, is equivalent to whether
4960 * or not the skb was consumed.
4962 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4963 struct sk_buff *skb, bool consume)
4965 struct ieee80211_local *local = rx->local;
4966 struct ieee80211_sub_if_data *sdata = rx->sdata;
4967 struct ieee80211_hdr *hdr = (void *)skb->data;
4968 struct link_sta_info *link_sta = rx->link_sta;
4969 struct ieee80211_link_data *link = rx->link;
4973 /* See if we can do fast-rx; if we have to copy we already lost,
4974 * so punt in that case. We should never have to deliver a data
4975 * frame to multiple interfaces anyway.
4977 * We skip the ieee80211_accept_frame() call and do the necessary
4978 * checking inside ieee80211_invoke_fast_rx().
4980 if (consume && rx->sta) {
4981 struct ieee80211_fast_rx *fast_rx;
4983 fast_rx = rcu_dereference(rx->sta->fast_rx);
4984 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4988 if (!ieee80211_accept_frame(rx))
4992 struct skb_shared_hwtstamps *shwt;
4994 rx->skb = skb_copy(skb, GFP_ATOMIC);
4996 if (net_ratelimit())
4997 wiphy_debug(local->hw.wiphy,
4998 "failed to copy skb for %s\n",
5003 /* skb_copy() does not copy the hw timestamps, so copy it
5006 shwt = skb_hwtstamps(rx->skb);
5007 shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
5009 /* Update the hdr pointer to the new skb for translation below */
5010 hdr = (struct ieee80211_hdr *)rx->skb->data;
5013 if (unlikely(rx->sta && rx->sta->sta.mlo) &&
5014 is_unicast_ether_addr(hdr->addr1) &&
5015 !ieee80211_is_probe_resp(hdr->frame_control) &&
5016 !ieee80211_is_beacon(hdr->frame_control)) {
5017 /* translate to MLD addresses */
5018 if (ether_addr_equal(link->conf->addr, hdr->addr1))
5019 ether_addr_copy(hdr->addr1, rx->sdata->vif.addr);
5020 if (ether_addr_equal(link_sta->addr, hdr->addr2))
5021 ether_addr_copy(hdr->addr2, rx->sta->addr);
5022 /* translate A3 only if it's the BSSID */
5023 if (!ieee80211_has_tods(hdr->frame_control) &&
5024 !ieee80211_has_fromds(hdr->frame_control)) {
5025 if (ether_addr_equal(link_sta->addr, hdr->addr3))
5026 ether_addr_copy(hdr->addr3, rx->sta->addr);
5027 else if (ether_addr_equal(link->conf->addr, hdr->addr3))
5028 ether_addr_copy(hdr->addr3, rx->sdata->vif.addr);
5030 /* not needed for A4 since it can only carry the SA */
5033 ieee80211_invoke_rx_handlers(rx);
5037 static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
5038 struct ieee80211_sta *pubsta,
5039 struct sk_buff *skb,
5040 struct list_head *list)
5042 struct ieee80211_local *local = hw_to_local(hw);
5043 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5044 struct ieee80211_fast_rx *fast_rx;
5045 struct ieee80211_rx_data rx;
5046 struct sta_info *sta;
5049 memset(&rx, 0, sizeof(rx));
5055 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5057 /* drop frame if too short for header */
5058 if (skb->len < sizeof(struct ethhdr))
5064 if (status->link_valid)
5065 link_id = status->link_id;
5068 * TODO: Should the frame be dropped if the right link_id is not
5069 * available? Or may be it is fine in the current form to proceed with
5070 * the frame processing because with frame being in 802.3 format,
5071 * link_id is used only for stats purpose and updating the stats on
5072 * the deflink is fine?
5074 sta = container_of(pubsta, struct sta_info, sta);
5075 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
5078 fast_rx = rcu_dereference(rx.sta->fast_rx);
5082 ieee80211_rx_8023(&rx, fast_rx, skb->len);
5089 static bool ieee80211_rx_for_interface(struct ieee80211_rx_data *rx,
5090 struct sk_buff *skb, bool consume)
5092 struct link_sta_info *link_sta;
5093 struct ieee80211_hdr *hdr = (void *)skb->data;
5094 struct sta_info *sta;
5098 * Look up link station first, in case there's a
5099 * chance that they might have a link address that
5100 * is identical to the MLD address, that way we'll
5101 * have the link information if needed.
5103 link_sta = link_sta_info_get_bss(rx->sdata, hdr->addr2);
5105 sta = link_sta->sta;
5106 link_id = link_sta->link_id;
5108 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5110 sta = sta_info_get_bss(rx->sdata, hdr->addr2);
5111 if (status->link_valid)
5112 link_id = status->link_id;
5115 if (!ieee80211_rx_data_set_sta(rx, sta, link_id))
5118 return ieee80211_prepare_and_rx_handle(rx, skb, consume);
5122 * This is the actual Rx frames handler. as it belongs to Rx path it must
5123 * be called with rcu_read_lock protection.
5125 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
5126 struct ieee80211_sta *pubsta,
5127 struct sk_buff *skb,
5128 struct list_head *list)
5130 struct ieee80211_local *local = hw_to_local(hw);
5131 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5132 struct ieee80211_sub_if_data *sdata;
5133 struct ieee80211_hdr *hdr;
5135 struct ieee80211_rx_data rx;
5136 struct ieee80211_sub_if_data *prev;
5137 struct rhlist_head *tmp;
5140 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
5141 memset(&rx, 0, sizeof(rx));
5147 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
5148 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
5150 if (ieee80211_is_mgmt(fc)) {
5151 /* drop frame if too short for header */
5152 if (skb->len < ieee80211_hdrlen(fc))
5155 err = skb_linearize(skb);
5157 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
5165 hdr = (struct ieee80211_hdr *)skb->data;
5166 ieee80211_parse_qos(&rx);
5167 ieee80211_verify_alignment(&rx);
5169 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
5170 ieee80211_is_beacon(hdr->frame_control) ||
5171 ieee80211_is_s1g_beacon(hdr->frame_control)))
5172 ieee80211_scan_rx(local, skb);
5174 if (ieee80211_is_data(fc)) {
5175 struct sta_info *sta, *prev_sta;
5178 if (status->link_valid)
5179 link_id = status->link_id;
5182 sta = container_of(pubsta, struct sta_info, sta);
5183 if (!ieee80211_rx_data_set_sta(&rx, sta, link_id))
5187 * In MLO connection, fetch the link_id using addr2
5188 * when the driver does not pass link_id in status.
5189 * When the address translation is already performed by
5190 * driver/hw, the valid link_id must be passed in
5194 if (!status->link_valid && pubsta->mlo) {
5195 struct ieee80211_hdr *hdr = (void *)skb->data;
5196 struct link_sta_info *link_sta;
5198 link_sta = link_sta_info_get_bss(rx.sdata,
5203 ieee80211_rx_data_set_link(&rx, link_sta->link_id);
5206 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5213 for_each_sta_info(local, hdr->addr2, sta, tmp) {
5219 rx.sdata = prev_sta->sdata;
5220 if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
5223 if (!status->link_valid && prev_sta->sta.mlo)
5226 ieee80211_prepare_and_rx_handle(&rx, skb, false);
5232 rx.sdata = prev_sta->sdata;
5233 if (!ieee80211_rx_data_set_sta(&rx, prev_sta, link_id))
5236 if (!status->link_valid && prev_sta->sta.mlo)
5239 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
5247 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
5248 if (!ieee80211_sdata_running(sdata))
5251 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
5252 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5256 * frame is destined for this interface, but if it's
5257 * not also for the previous one we handle that after
5258 * the loop to avoid copying the SKB once too much
5267 ieee80211_rx_for_interface(&rx, skb, false);
5275 if (ieee80211_rx_for_interface(&rx, skb, true))
5284 * This is the receive path handler. It is called by a low level driver when an
5285 * 802.11 MPDU is received from the hardware.
5287 void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5288 struct sk_buff *skb, struct list_head *list)
5290 struct ieee80211_local *local = hw_to_local(hw);
5291 struct ieee80211_rate *rate = NULL;
5292 struct ieee80211_supported_band *sband;
5293 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
5294 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5296 WARN_ON_ONCE(softirq_count() == 0);
5298 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
5301 sband = local->hw.wiphy->bands[status->band];
5302 if (WARN_ON(!sband))
5306 * If we're suspending, it is possible although not too likely
5307 * that we'd be receiving frames after having already partially
5308 * quiesced the stack. We can't process such frames then since
5309 * that might, for example, cause stations to be added or other
5310 * driver callbacks be invoked.
5312 if (unlikely(local->quiescing || local->suspended))
5315 /* We might be during a HW reconfig, prevent Rx for the same reason */
5316 if (unlikely(local->in_reconfig))
5320 * The same happens when we're not even started,
5321 * but that's worth a warning.
5323 if (WARN_ON(!local->started))
5326 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
5328 * Validate the rate, unless a PLCP error means that
5329 * we probably can't have a valid rate here anyway.
5332 switch (status->encoding) {
5335 * rate_idx is MCS index, which can be [0-76]
5338 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
5340 * Anything else would be some sort of driver or
5341 * hardware error. The driver should catch hardware
5344 if (WARN(status->rate_idx > 76,
5345 "Rate marked as an HT rate but passed "
5346 "status->rate_idx is not "
5347 "an MCS index [0-76]: %d (0x%02x)\n",
5353 if (WARN_ONCE(status->rate_idx > 11 ||
5356 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
5357 status->rate_idx, status->nss))
5361 if (WARN_ONCE(status->rate_idx > 11 ||
5364 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
5365 status->rate_idx, status->nss))
5369 if (WARN_ONCE(status->rate_idx > 15 ||
5372 status->eht.gi > NL80211_RATE_INFO_EHT_GI_3_2,
5373 "Rate marked as an EHT rate but data is invalid: MCS:%d, NSS:%d, GI:%d\n",
5374 status->rate_idx, status->nss, status->eht.gi))
5381 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
5383 rate = &sband->bitrates[status->rate_idx];
5387 if (WARN_ON_ONCE(status->link_id >= IEEE80211_LINK_UNSPECIFIED))
5390 status->rx_flags = 0;
5392 kcov_remote_start_common(skb_get_kcov_handle(skb));
5395 * Frames with failed FCS/PLCP checksum are not returned,
5396 * all other frames are returned without radiotap header
5397 * if it was previously present.
5398 * Also, frames with less than 16 bytes are dropped.
5400 if (!(status->flag & RX_FLAG_8023))
5401 skb = ieee80211_rx_monitor(local, skb, rate);
5403 if ((status->flag & RX_FLAG_8023) ||
5404 ieee80211_is_data_present(hdr->frame_control))
5405 ieee80211_tpt_led_trig_rx(local, skb->len);
5407 if (status->flag & RX_FLAG_8023)
5408 __ieee80211_rx_handle_8023(hw, pubsta, skb, list);
5410 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
5418 EXPORT_SYMBOL(ieee80211_rx_list);
5420 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
5421 struct sk_buff *skb, struct napi_struct *napi)
5423 struct sk_buff *tmp;
5428 * key references and virtual interfaces are protected using RCU
5429 * and this requires that we are in a read-side RCU section during
5430 * receive processing
5433 ieee80211_rx_list(hw, pubsta, skb, &list);
5437 netif_receive_skb_list(&list);
5441 list_for_each_entry_safe(skb, tmp, &list, list) {
5442 skb_list_del_init(skb);
5443 napi_gro_receive(napi, skb);
5446 EXPORT_SYMBOL(ieee80211_rx_napi);
5448 /* This is a version of the rx handler that can be called from hard irq
5449 * context. Post the skb on the queue and schedule the tasklet */
5450 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
5452 struct ieee80211_local *local = hw_to_local(hw);
5454 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
5456 skb->pkt_type = IEEE80211_RX_MSG;
5457 skb_queue_tail(&local->skb_queue, skb);
5458 tasklet_schedule(&local->tasklet);
5460 EXPORT_SYMBOL(ieee80211_rx_irqsafe);