1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
9 * Copyright (C) 2018-2021 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 <net/mac80211.h>
23 #include <net/ieee80211_radiotap.h>
24 #include <asm/unaligned.h>
26 #include "ieee80211_i.h"
27 #include "driver-ops.h"
37 * monitor mode reception
39 * This function cleans up the SKB, i.e. it removes all the stuff
40 * only useful for monitoring.
42 static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
43 unsigned int present_fcs_len,
44 unsigned int rtap_space)
46 struct ieee80211_hdr *hdr;
51 __pskb_trim(skb, skb->len - present_fcs_len);
52 __pskb_pull(skb, rtap_space);
54 hdr = (void *)skb->data;
55 fc = hdr->frame_control;
58 * Remove the HT-Control field (if present) on management
59 * frames after we've sent the frame to monitoring. We
60 * (currently) don't need it, and don't properly parse
61 * frames with it present, due to the assumption of a
62 * fixed management header length.
64 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
67 hdrlen = ieee80211_hdrlen(fc);
68 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
70 if (!pskb_may_pull(skb, hdrlen)) {
75 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
76 hdrlen - IEEE80211_HT_CTL_LEN);
77 __pskb_pull(skb, IEEE80211_HT_CTL_LEN);
82 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
83 unsigned int rtap_space)
85 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
86 struct ieee80211_hdr *hdr;
88 hdr = (void *)(skb->data + rtap_space);
90 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
91 RX_FLAG_FAILED_PLCP_CRC |
92 RX_FLAG_ONLY_MONITOR |
96 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
99 if (ieee80211_is_ctl(hdr->frame_control) &&
100 !ieee80211_is_pspoll(hdr->frame_control) &&
101 !ieee80211_is_back_req(hdr->frame_control))
108 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
109 struct ieee80211_rx_status *status,
114 /* always present fields */
115 len = sizeof(struct ieee80211_radiotap_header) + 8;
117 /* allocate extra bitmaps */
119 len += 4 * hweight8(status->chains);
120 /* vendor presence bitmap */
121 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
124 if (ieee80211_have_rx_timestamp(status)) {
128 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
131 /* antenna field, if we don't have per-chain info */
135 /* padding for RX_FLAGS if necessary */
138 if (status->encoding == RX_ENC_HT) /* HT info */
141 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
146 if (status->encoding == RX_ENC_VHT) {
151 if (local->hw.radiotap_timestamp.units_pos >= 0) {
156 if (status->encoding == RX_ENC_HE &&
157 status->flag & RX_FLAG_RADIOTAP_HE) {
160 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
163 if (status->encoding == RX_ENC_HE &&
164 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
167 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
170 if (status->flag & RX_FLAG_NO_PSDU)
173 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
176 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
179 if (status->chains) {
180 /* antenna and antenna signal fields */
181 len += 2 * hweight8(status->chains);
184 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
185 struct ieee80211_vendor_radiotap *rtap;
186 int vendor_data_offset = 0;
189 * The position to look at depends on the existence (or non-
190 * existence) of other elements, so take that into account...
192 if (status->flag & RX_FLAG_RADIOTAP_HE)
193 vendor_data_offset +=
194 sizeof(struct ieee80211_radiotap_he);
195 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
196 vendor_data_offset +=
197 sizeof(struct ieee80211_radiotap_he_mu);
198 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
199 vendor_data_offset +=
200 sizeof(struct ieee80211_radiotap_lsig);
202 rtap = (void *)&skb->data[vendor_data_offset];
204 /* alignment for fixed 6-byte vendor data header */
206 /* vendor data header */
208 if (WARN_ON(rtap->align == 0))
210 len = ALIGN(len, rtap->align);
211 len += rtap->len + rtap->pad;
217 static void __ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
218 struct sta_info *sta,
221 skb_queue_tail(&sdata->skb_queue, skb);
222 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
224 sta->rx_stats.packets++;
227 static void ieee80211_queue_skb_to_iface(struct ieee80211_sub_if_data *sdata,
228 struct sta_info *sta,
232 __ieee80211_queue_skb_to_iface(sdata, sta, skb);
235 static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
240 struct ieee80211_hdr_3addr hdr;
243 } __packed __aligned(2) action;
248 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
250 if (skb->len < rtap_space + sizeof(action) +
251 VHT_MUMIMO_GROUPS_DATA_LEN)
254 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
257 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
259 if (!ieee80211_is_action(action.hdr.frame_control))
262 if (action.category != WLAN_CATEGORY_VHT)
265 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
268 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
271 skb = skb_copy(skb, GFP_ATOMIC);
275 ieee80211_queue_skb_to_iface(sdata, NULL, skb);
279 * ieee80211_add_rx_radiotap_header - add radiotap header
281 * add a radiotap header containing all the fields which the hardware provided.
284 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
286 struct ieee80211_rate *rate,
287 int rtap_len, bool has_fcs)
289 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
290 struct ieee80211_radiotap_header *rthdr;
295 u16 channel_flags = 0;
297 unsigned long chains = status->chains;
298 struct ieee80211_vendor_radiotap rtap = {};
299 struct ieee80211_radiotap_he he = {};
300 struct ieee80211_radiotap_he_mu he_mu = {};
301 struct ieee80211_radiotap_lsig lsig = {};
303 if (status->flag & RX_FLAG_RADIOTAP_HE) {
304 he = *(struct ieee80211_radiotap_he *)skb->data;
305 skb_pull(skb, sizeof(he));
306 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
309 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
310 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
311 skb_pull(skb, sizeof(he_mu));
314 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
315 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
316 skb_pull(skb, sizeof(lsig));
319 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
320 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
321 /* rtap.len and rtap.pad are undone immediately */
322 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
326 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
329 rthdr = skb_push(skb, rtap_len);
330 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
331 it_present = &rthdr->it_present;
333 /* radiotap header, set always present flags */
334 rthdr->it_len = cpu_to_le16(rtap_len);
335 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
336 BIT(IEEE80211_RADIOTAP_CHANNEL) |
337 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
340 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
342 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
344 BIT(IEEE80211_RADIOTAP_EXT) |
345 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
346 put_unaligned_le32(it_present_val, it_present);
348 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
349 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
352 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
353 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
354 BIT(IEEE80211_RADIOTAP_EXT);
355 put_unaligned_le32(it_present_val, it_present);
357 it_present_val = rtap.present;
360 put_unaligned_le32(it_present_val, it_present);
362 pos = (void *)(it_present + 1);
364 /* the order of the following fields is important */
366 /* IEEE80211_RADIOTAP_TSFT */
367 if (ieee80211_have_rx_timestamp(status)) {
369 while ((pos - (u8 *)rthdr) & 7)
372 ieee80211_calculate_rx_timestamp(local, status,
375 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
379 /* IEEE80211_RADIOTAP_FLAGS */
380 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
381 *pos |= IEEE80211_RADIOTAP_F_FCS;
382 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
383 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
384 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
385 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
388 /* IEEE80211_RADIOTAP_RATE */
389 if (!rate || status->encoding != RX_ENC_LEGACY) {
391 * Without rate information don't add it. If we have,
392 * MCS information is a separate field in radiotap,
393 * added below. The byte here is needed as padding
394 * for the channel though, so initialise it to 0.
399 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
400 if (status->bw == RATE_INFO_BW_10)
402 else if (status->bw == RATE_INFO_BW_5)
404 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
408 /* IEEE80211_RADIOTAP_CHANNEL */
409 /* TODO: frequency offset in KHz */
410 put_unaligned_le16(status->freq, pos);
412 if (status->bw == RATE_INFO_BW_10)
413 channel_flags |= IEEE80211_CHAN_HALF;
414 else if (status->bw == RATE_INFO_BW_5)
415 channel_flags |= IEEE80211_CHAN_QUARTER;
417 if (status->band == NL80211_BAND_5GHZ ||
418 status->band == NL80211_BAND_6GHZ)
419 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
420 else if (status->encoding != RX_ENC_LEGACY)
421 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
422 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
423 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
425 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
427 channel_flags |= IEEE80211_CHAN_2GHZ;
428 put_unaligned_le16(channel_flags, pos);
431 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
432 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
433 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
434 *pos = status->signal;
436 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
440 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
442 if (!status->chains) {
443 /* IEEE80211_RADIOTAP_ANTENNA */
444 *pos = status->antenna;
448 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
450 /* IEEE80211_RADIOTAP_RX_FLAGS */
451 /* ensure 2 byte alignment for the 2 byte field as required */
452 if ((pos - (u8 *)rthdr) & 1)
454 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
455 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
456 put_unaligned_le16(rx_flags, pos);
459 if (status->encoding == RX_ENC_HT) {
462 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
463 *pos++ = local->hw.radiotap_mcs_details;
465 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
466 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
467 if (status->bw == RATE_INFO_BW_40)
468 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
469 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
470 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
471 if (status->enc_flags & RX_ENC_FLAG_LDPC)
472 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
473 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
474 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
476 *pos++ = status->rate_idx;
479 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
482 /* ensure 4 byte alignment */
483 while ((pos - (u8 *)rthdr) & 3)
486 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
487 put_unaligned_le32(status->ampdu_reference, pos);
489 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
490 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
491 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
492 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
493 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
494 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
495 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
496 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
497 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
498 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
499 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
500 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
501 put_unaligned_le16(flags, pos);
503 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
504 *pos++ = status->ampdu_delimiter_crc;
510 if (status->encoding == RX_ENC_VHT) {
511 u16 known = local->hw.radiotap_vht_details;
513 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
514 put_unaligned_le16(known, pos);
517 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
518 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
519 /* in VHT, STBC is binary */
520 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
521 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
522 if (status->enc_flags & RX_ENC_FLAG_BF)
523 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
526 switch (status->bw) {
527 case RATE_INFO_BW_80:
530 case RATE_INFO_BW_160:
533 case RATE_INFO_BW_40:
540 *pos = (status->rate_idx << 4) | status->nss;
543 if (status->enc_flags & RX_ENC_FLAG_LDPC)
544 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
552 if (local->hw.radiotap_timestamp.units_pos >= 0) {
554 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
557 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
559 /* ensure 8 byte alignment */
560 while ((pos - (u8 *)rthdr) & 7)
563 put_unaligned_le64(status->device_timestamp, pos);
566 if (local->hw.radiotap_timestamp.accuracy >= 0) {
567 accuracy = local->hw.radiotap_timestamp.accuracy;
568 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
570 put_unaligned_le16(accuracy, pos);
573 *pos++ = local->hw.radiotap_timestamp.units_pos;
577 if (status->encoding == RX_ENC_HE &&
578 status->flag & RX_FLAG_RADIOTAP_HE) {
579 #define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
581 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
582 he.data6 |= HE_PREP(DATA6_NSTS,
583 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
585 he.data3 |= HE_PREP(DATA3_STBC, 1);
587 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
590 #define CHECK_GI(s) \
591 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
592 (int)NL80211_RATE_INFO_HE_GI_##s)
598 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
599 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
600 he.data3 |= HE_PREP(DATA3_CODING,
601 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
603 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
605 switch (status->bw) {
606 case RATE_INFO_BW_20:
607 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
608 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
610 case RATE_INFO_BW_40:
611 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
612 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
614 case RATE_INFO_BW_80:
615 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
616 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
618 case RATE_INFO_BW_160:
619 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
620 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
622 case RATE_INFO_BW_HE_RU:
623 #define CHECK_RU_ALLOC(s) \
624 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
625 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
633 CHECK_RU_ALLOC(2x996);
635 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
639 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
642 /* ensure 2 byte alignment */
643 while ((pos - (u8 *)rthdr) & 1)
645 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
646 memcpy(pos, &he, sizeof(he));
650 if (status->encoding == RX_ENC_HE &&
651 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
652 /* ensure 2 byte alignment */
653 while ((pos - (u8 *)rthdr) & 1)
655 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
656 memcpy(pos, &he_mu, sizeof(he_mu));
657 pos += sizeof(he_mu);
660 if (status->flag & RX_FLAG_NO_PSDU) {
662 cpu_to_le32(1 << IEEE80211_RADIOTAP_ZERO_LEN_PSDU);
663 *pos++ = status->zero_length_psdu_type;
666 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
667 /* ensure 2 byte alignment */
668 while ((pos - (u8 *)rthdr) & 1)
670 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_LSIG);
671 memcpy(pos, &lsig, sizeof(lsig));
675 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
676 *pos++ = status->chain_signal[chain];
680 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
681 /* ensure 2 byte alignment for the vendor field as required */
682 if ((pos - (u8 *)rthdr) & 1)
684 *pos++ = rtap.oui[0];
685 *pos++ = rtap.oui[1];
686 *pos++ = rtap.oui[2];
688 put_unaligned_le16(rtap.len, pos);
690 /* align the actual payload as requested */
691 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
693 /* data (and possible padding) already follows */
697 static struct sk_buff *
698 ieee80211_make_monitor_skb(struct ieee80211_local *local,
699 struct sk_buff **origskb,
700 struct ieee80211_rate *rate,
701 int rtap_space, bool use_origskb)
703 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
704 int rt_hdrlen, needed_headroom;
707 /* room for the radiotap header based on driver features */
708 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
709 needed_headroom = rt_hdrlen - rtap_space;
712 /* only need to expand headroom if necessary */
717 * This shouldn't trigger often because most devices have an
718 * RX header they pull before we get here, and that should
719 * be big enough for our radiotap information. We should
720 * probably export the length to drivers so that we can have
721 * them allocate enough headroom to start with.
723 if (skb_headroom(skb) < needed_headroom &&
724 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
730 * Need to make a copy and possibly remove radiotap header
731 * and FCS from the original.
733 skb = skb_copy_expand(*origskb, needed_headroom + NET_SKB_PAD,
740 /* prepend radiotap information */
741 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
743 skb_reset_mac_header(skb);
744 skb->ip_summed = CHECKSUM_UNNECESSARY;
745 skb->pkt_type = PACKET_OTHERHOST;
746 skb->protocol = htons(ETH_P_802_2);
752 * This function copies a received frame to all monitor interfaces and
753 * returns a cleaned-up SKB that no longer includes the FCS nor the
754 * radiotap header the driver might have added.
756 static struct sk_buff *
757 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
758 struct ieee80211_rate *rate)
760 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
761 struct ieee80211_sub_if_data *sdata;
762 struct sk_buff *monskb = NULL;
763 int present_fcs_len = 0;
764 unsigned int rtap_space = 0;
765 struct ieee80211_sub_if_data *monitor_sdata =
766 rcu_dereference(local->monitor_sdata);
767 bool only_monitor = false;
768 unsigned int min_head_len;
770 if (status->flag & RX_FLAG_RADIOTAP_HE)
771 rtap_space += sizeof(struct ieee80211_radiotap_he);
773 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
774 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
776 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
777 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
779 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
780 struct ieee80211_vendor_radiotap *rtap =
781 (void *)(origskb->data + rtap_space);
783 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
786 min_head_len = rtap_space;
789 * First, we may need to make a copy of the skb because
790 * (1) we need to modify it for radiotap (if not present), and
791 * (2) the other RX handlers will modify the skb we got.
793 * We don't need to, of course, if we aren't going to return
794 * the SKB because it has a bad FCS/PLCP checksum.
797 if (!(status->flag & RX_FLAG_NO_PSDU)) {
798 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
799 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
802 dev_kfree_skb(origskb);
805 present_fcs_len = FCS_LEN;
808 /* also consider the hdr->frame_control */
812 /* ensure that the expected data elements are in skb head */
813 if (!pskb_may_pull(origskb, min_head_len)) {
814 dev_kfree_skb(origskb);
818 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
820 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
822 dev_kfree_skb(origskb);
826 return ieee80211_clean_skb(origskb, present_fcs_len,
830 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
832 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
833 bool last_monitor = list_is_last(&sdata->u.mntr.list,
837 monskb = ieee80211_make_monitor_skb(local, &origskb,
849 skb = skb_clone(monskb, GFP_ATOMIC);
853 skb->dev = sdata->dev;
854 dev_sw_netstats_rx_add(skb->dev, skb->len);
855 netif_receive_skb(skb);
863 /* this happens if last_monitor was erroneously false */
864 dev_kfree_skb(monskb);
870 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
873 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
875 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
876 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
877 int tid, seqno_idx, security_idx;
879 /* does the frame have a qos control field? */
880 if (ieee80211_is_data_qos(hdr->frame_control)) {
881 u8 *qc = ieee80211_get_qos_ctl(hdr);
882 /* frame has qos control */
883 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
884 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
885 status->rx_flags |= IEEE80211_RX_AMSDU;
891 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
893 * Sequence numbers for management frames, QoS data
894 * frames with a broadcast/multicast address in the
895 * Address 1 field, and all non-QoS data frames sent
896 * by QoS STAs are assigned using an additional single
897 * modulo-4096 counter, [...]
899 * We also use that counter for non-QoS STAs.
901 seqno_idx = IEEE80211_NUM_TIDS;
903 if (ieee80211_is_mgmt(hdr->frame_control))
904 security_idx = IEEE80211_NUM_TIDS;
908 rx->seqno_idx = seqno_idx;
909 rx->security_idx = security_idx;
910 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
911 * For now, set skb->priority to 0 for other cases. */
912 rx->skb->priority = (tid > 7) ? 0 : tid;
916 * DOC: Packet alignment
918 * Drivers always need to pass packets that are aligned to two-byte boundaries
921 * Additionally, should, if possible, align the payload data in a way that
922 * guarantees that the contained IP header is aligned to a four-byte
923 * boundary. In the case of regular frames, this simply means aligning the
924 * payload to a four-byte boundary (because either the IP header is directly
925 * contained, or IV/RFC1042 headers that have a length divisible by four are
926 * in front of it). If the payload data is not properly aligned and the
927 * architecture doesn't support efficient unaligned operations, mac80211
928 * will align the data.
930 * With A-MSDU frames, however, the payload data address must yield two modulo
931 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
932 * push the IP header further back to a multiple of four again. Thankfully, the
933 * specs were sane enough this time around to require padding each A-MSDU
934 * subframe to a length that is a multiple of four.
936 * Padding like Atheros hardware adds which is between the 802.11 header and
937 * the payload is not supported, the driver is required to move the 802.11
938 * header to be directly in front of the payload in that case.
940 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
942 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
943 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
950 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
952 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
954 if (is_multicast_ether_addr(hdr->addr1))
957 return ieee80211_is_robust_mgmt_frame(skb);
961 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
963 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
965 if (!is_multicast_ether_addr(hdr->addr1))
968 return ieee80211_is_robust_mgmt_frame(skb);
972 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
973 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
975 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
976 struct ieee80211_mmie *mmie;
977 struct ieee80211_mmie_16 *mmie16;
979 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
982 if (!ieee80211_is_robust_mgmt_frame(skb) &&
983 !ieee80211_is_beacon(hdr->frame_control))
984 return -1; /* not a robust management frame */
986 mmie = (struct ieee80211_mmie *)
987 (skb->data + skb->len - sizeof(*mmie));
988 if (mmie->element_id == WLAN_EID_MMIE &&
989 mmie->length == sizeof(*mmie) - 2)
990 return le16_to_cpu(mmie->key_id);
992 mmie16 = (struct ieee80211_mmie_16 *)
993 (skb->data + skb->len - sizeof(*mmie16));
994 if (skb->len >= 24 + sizeof(*mmie16) &&
995 mmie16->element_id == WLAN_EID_MMIE &&
996 mmie16->length == sizeof(*mmie16) - 2)
997 return le16_to_cpu(mmie16->key_id);
1002 static int ieee80211_get_keyid(struct sk_buff *skb,
1003 const struct ieee80211_cipher_scheme *cs)
1005 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1013 fc = hdr->frame_control;
1014 hdrlen = ieee80211_hdrlen(fc);
1017 minlen = hdrlen + cs->hdr_len;
1018 key_idx_off = hdrlen + cs->key_idx_off;
1019 key_idx_shift = cs->key_idx_shift;
1021 /* WEP, TKIP, CCMP and GCMP */
1022 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1023 key_idx_off = hdrlen + 3;
1027 if (unlikely(skb->len < minlen))
1030 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1033 keyid &= cs->key_idx_mask;
1034 keyid >>= key_idx_shift;
1036 /* cs could use more than the usual two bits for the keyid */
1037 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1043 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
1045 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1046 char *dev_addr = rx->sdata->vif.addr;
1048 if (ieee80211_is_data(hdr->frame_control)) {
1049 if (is_multicast_ether_addr(hdr->addr1)) {
1050 if (ieee80211_has_tods(hdr->frame_control) ||
1051 !ieee80211_has_fromds(hdr->frame_control))
1052 return RX_DROP_MONITOR;
1053 if (ether_addr_equal(hdr->addr3, dev_addr))
1054 return RX_DROP_MONITOR;
1056 if (!ieee80211_has_a4(hdr->frame_control))
1057 return RX_DROP_MONITOR;
1058 if (ether_addr_equal(hdr->addr4, dev_addr))
1059 return RX_DROP_MONITOR;
1063 /* If there is not an established peer link and this is not a peer link
1064 * establisment frame, beacon or probe, drop the frame.
1067 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
1068 struct ieee80211_mgmt *mgmt;
1070 if (!ieee80211_is_mgmt(hdr->frame_control))
1071 return RX_DROP_MONITOR;
1073 if (ieee80211_is_action(hdr->frame_control)) {
1076 /* make sure category field is present */
1077 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1078 return RX_DROP_MONITOR;
1080 mgmt = (struct ieee80211_mgmt *)hdr;
1081 category = mgmt->u.action.category;
1082 if (category != WLAN_CATEGORY_MESH_ACTION &&
1083 category != WLAN_CATEGORY_SELF_PROTECTED)
1084 return RX_DROP_MONITOR;
1088 if (ieee80211_is_probe_req(hdr->frame_control) ||
1089 ieee80211_is_probe_resp(hdr->frame_control) ||
1090 ieee80211_is_beacon(hdr->frame_control) ||
1091 ieee80211_is_auth(hdr->frame_control))
1094 return RX_DROP_MONITOR;
1100 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1103 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1104 struct sk_buff *tail = skb_peek_tail(frames);
1105 struct ieee80211_rx_status *status;
1107 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1113 status = IEEE80211_SKB_RXCB(tail);
1114 if (status->flag & RX_FLAG_AMSDU_MORE)
1120 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1121 struct tid_ampdu_rx *tid_agg_rx,
1123 struct sk_buff_head *frames)
1125 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1126 struct sk_buff *skb;
1127 struct ieee80211_rx_status *status;
1129 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1131 if (skb_queue_empty(skb_list))
1134 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1135 __skb_queue_purge(skb_list);
1139 /* release frames from the reorder ring buffer */
1140 tid_agg_rx->stored_mpdu_num--;
1141 while ((skb = __skb_dequeue(skb_list))) {
1142 status = IEEE80211_SKB_RXCB(skb);
1143 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1144 __skb_queue_tail(frames, skb);
1148 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
1149 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1152 static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
1153 struct tid_ampdu_rx *tid_agg_rx,
1155 struct sk_buff_head *frames)
1159 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1161 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1162 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1163 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1169 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1170 * the skb was added to the buffer longer than this time ago, the earlier
1171 * frames that have not yet been received are assumed to be lost and the skb
1172 * can be released for processing. This may also release other skb's from the
1173 * reorder buffer if there are no additional gaps between the frames.
1175 * Callers must hold tid_agg_rx->reorder_lock.
1177 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1179 static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
1180 struct tid_ampdu_rx *tid_agg_rx,
1181 struct sk_buff_head *frames)
1185 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1187 /* release the buffer until next missing frame */
1188 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1189 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
1190 tid_agg_rx->stored_mpdu_num) {
1192 * No buffers ready to be released, but check whether any
1193 * frames in the reorder buffer have timed out.
1196 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1197 j = (j + 1) % tid_agg_rx->buf_size) {
1198 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
1203 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
1204 HT_RX_REORDER_BUF_TIMEOUT))
1205 goto set_release_timer;
1207 /* don't leave incomplete A-MSDUs around */
1208 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1209 i = (i + 1) % tid_agg_rx->buf_size)
1210 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1212 ht_dbg_ratelimited(sdata,
1213 "release an RX reorder frame due to timeout on earlier frames\n");
1214 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1218 * Increment the head seq# also for the skipped slots.
1220 tid_agg_rx->head_seq_num =
1221 (tid_agg_rx->head_seq_num +
1222 skipped) & IEEE80211_SN_MASK;
1225 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1226 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1228 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1231 if (tid_agg_rx->stored_mpdu_num) {
1232 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1234 for (; j != (index - 1) % tid_agg_rx->buf_size;
1235 j = (j + 1) % tid_agg_rx->buf_size) {
1236 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
1242 if (!tid_agg_rx->removed)
1243 mod_timer(&tid_agg_rx->reorder_timer,
1244 tid_agg_rx->reorder_time[j] + 1 +
1245 HT_RX_REORDER_BUF_TIMEOUT);
1247 del_timer(&tid_agg_rx->reorder_timer);
1252 * As this function belongs to the RX path it must be under
1253 * rcu_read_lock protection. It returns false if the frame
1254 * can be processed immediately, true if it was consumed.
1256 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1257 struct tid_ampdu_rx *tid_agg_rx,
1258 struct sk_buff *skb,
1259 struct sk_buff_head *frames)
1261 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1262 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1263 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1264 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1265 u16 head_seq_num, buf_size;
1269 spin_lock(&tid_agg_rx->reorder_lock);
1272 * Offloaded BA sessions have no known starting sequence number so pick
1273 * one from first Rxed frame for this tid after BA was started.
1275 if (unlikely(tid_agg_rx->auto_seq)) {
1276 tid_agg_rx->auto_seq = false;
1277 tid_agg_rx->ssn = mpdu_seq_num;
1278 tid_agg_rx->head_seq_num = mpdu_seq_num;
1281 buf_size = tid_agg_rx->buf_size;
1282 head_seq_num = tid_agg_rx->head_seq_num;
1285 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1288 if (unlikely(!tid_agg_rx->started)) {
1289 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1293 tid_agg_rx->started = true;
1296 /* frame with out of date sequence number */
1297 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1303 * If frame the sequence number exceeds our buffering window
1304 * size release some previous frames to make room for this one.
1306 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1307 head_seq_num = ieee80211_sn_inc(
1308 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1309 /* release stored frames up to new head to stack */
1310 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
1311 head_seq_num, frames);
1314 /* Now the new frame is always in the range of the reordering buffer */
1316 index = mpdu_seq_num % tid_agg_rx->buf_size;
1318 /* check if we already stored this frame */
1319 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1325 * If the current MPDU is in the right order and nothing else
1326 * is stored we can process it directly, no need to buffer it.
1327 * If it is first but there's something stored, we may be able
1328 * to release frames after this one.
1330 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1331 tid_agg_rx->stored_mpdu_num == 0) {
1332 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1333 tid_agg_rx->head_seq_num =
1334 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1339 /* put the frame in the reordering buffer */
1340 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1341 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1342 tid_agg_rx->reorder_time[index] = jiffies;
1343 tid_agg_rx->stored_mpdu_num++;
1344 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1348 spin_unlock(&tid_agg_rx->reorder_lock);
1353 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1354 * true if the MPDU was buffered, false if it should be processed.
1356 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1357 struct sk_buff_head *frames)
1359 struct sk_buff *skb = rx->skb;
1360 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1361 struct sta_info *sta = rx->sta;
1362 struct tid_ampdu_rx *tid_agg_rx;
1366 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1367 is_multicast_ether_addr(hdr->addr1))
1371 * filter the QoS data rx stream according to
1372 * STA/TID and check if this STA/TID is on aggregation
1378 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1379 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1380 tid = ieee80211_get_tid(hdr);
1382 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1384 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1385 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1386 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1387 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1388 WLAN_BACK_RECIPIENT,
1389 WLAN_REASON_QSTA_REQUIRE_SETUP);
1393 /* qos null data frames are excluded */
1394 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1397 /* not part of a BA session */
1398 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1399 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1402 /* new, potentially un-ordered, ampdu frame - process it */
1404 /* reset session timer */
1405 if (tid_agg_rx->timeout)
1406 tid_agg_rx->last_rx = jiffies;
1408 /* if this mpdu is fragmented - terminate rx aggregation session */
1409 sc = le16_to_cpu(hdr->seq_ctrl);
1410 if (sc & IEEE80211_SCTL_FRAG) {
1411 ieee80211_queue_skb_to_iface(rx->sdata, NULL, skb);
1416 * No locking needed -- we will only ever process one
1417 * RX packet at a time, and thus own tid_agg_rx. All
1418 * other code manipulating it needs to (and does) make
1419 * sure that we cannot get to it any more before doing
1422 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1427 __skb_queue_tail(frames, skb);
1430 static ieee80211_rx_result debug_noinline
1431 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1433 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1434 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1436 if (status->flag & RX_FLAG_DUP_VALIDATED)
1440 * Drop duplicate 802.11 retransmissions
1441 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1444 if (rx->skb->len < 24)
1447 if (ieee80211_is_ctl(hdr->frame_control) ||
1448 ieee80211_is_any_nullfunc(hdr->frame_control) ||
1449 is_multicast_ether_addr(hdr->addr1))
1455 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1456 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1457 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1458 rx->sta->rx_stats.num_duplicates++;
1459 return RX_DROP_UNUSABLE;
1460 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1461 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1467 static ieee80211_rx_result debug_noinline
1468 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1470 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1472 /* Drop disallowed frame classes based on STA auth/assoc state;
1473 * IEEE 802.11, Chap 5.5.
1475 * mac80211 filters only based on association state, i.e. it drops
1476 * Class 3 frames from not associated stations. hostapd sends
1477 * deauth/disassoc frames when needed. In addition, hostapd is
1478 * responsible for filtering on both auth and assoc states.
1481 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1482 return ieee80211_rx_mesh_check(rx);
1484 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1485 ieee80211_is_pspoll(hdr->frame_control)) &&
1486 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1487 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1488 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1490 * accept port control frames from the AP even when it's not
1491 * yet marked ASSOC to prevent a race where we don't set the
1492 * assoc bit quickly enough before it sends the first frame
1494 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1495 ieee80211_is_data_present(hdr->frame_control)) {
1496 unsigned int hdrlen;
1499 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1501 if (rx->skb->len < hdrlen + 8)
1502 return RX_DROP_MONITOR;
1504 skb_copy_bits(rx->skb, hdrlen + 6, ðertype, 2);
1505 if (ethertype == rx->sdata->control_port_protocol)
1509 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1510 cfg80211_rx_spurious_frame(rx->sdata->dev,
1513 return RX_DROP_UNUSABLE;
1515 return RX_DROP_MONITOR;
1522 static ieee80211_rx_result debug_noinline
1523 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1525 struct ieee80211_local *local;
1526 struct ieee80211_hdr *hdr;
1527 struct sk_buff *skb;
1531 hdr = (struct ieee80211_hdr *) skb->data;
1533 if (!local->pspolling)
1536 if (!ieee80211_has_fromds(hdr->frame_control))
1537 /* this is not from AP */
1540 if (!ieee80211_is_data(hdr->frame_control))
1543 if (!ieee80211_has_moredata(hdr->frame_control)) {
1544 /* AP has no more frames buffered for us */
1545 local->pspolling = false;
1549 /* more data bit is set, let's request a new frame from the AP */
1550 ieee80211_send_pspoll(local, rx->sdata);
1555 static void sta_ps_start(struct sta_info *sta)
1557 struct ieee80211_sub_if_data *sdata = sta->sdata;
1558 struct ieee80211_local *local = sdata->local;
1562 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1563 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1564 ps = &sdata->bss->ps;
1568 atomic_inc(&ps->num_sta_ps);
1569 set_sta_flag(sta, WLAN_STA_PS_STA);
1570 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1571 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1572 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1573 sta->sta.addr, sta->sta.aid);
1575 ieee80211_clear_fast_xmit(sta);
1577 if (!sta->sta.txq[0])
1580 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
1581 struct ieee80211_txq *txq = sta->sta.txq[tid];
1583 ieee80211_unschedule_txq(&local->hw, txq, false);
1585 if (txq_has_queue(txq))
1586 set_bit(tid, &sta->txq_buffered_tids);
1588 clear_bit(tid, &sta->txq_buffered_tids);
1592 static void sta_ps_end(struct sta_info *sta)
1594 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1595 sta->sta.addr, sta->sta.aid);
1597 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1599 * Clear the flag only if the other one is still set
1600 * so that the TX path won't start TX'ing new frames
1601 * directly ... In the case that the driver flag isn't
1602 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1604 clear_sta_flag(sta, WLAN_STA_PS_STA);
1605 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1606 sta->sta.addr, sta->sta.aid);
1610 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1611 clear_sta_flag(sta, WLAN_STA_PS_STA);
1612 ieee80211_sta_ps_deliver_wakeup(sta);
1615 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1617 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1620 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1622 /* Don't let the same PS state be set twice */
1623 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1624 if ((start && in_ps) || (!start && !in_ps))
1634 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1636 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1638 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1640 if (test_sta_flag(sta, WLAN_STA_SP))
1643 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1644 ieee80211_sta_ps_deliver_poll_response(sta);
1646 set_sta_flag(sta, WLAN_STA_PSPOLL);
1648 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1650 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1652 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1653 int ac = ieee80211_ac_from_tid(tid);
1656 * If this AC is not trigger-enabled do nothing unless the
1657 * driver is calling us after it already checked.
1659 * NB: This could/should check a separate bitmap of trigger-
1660 * enabled queues, but for now we only implement uAPSD w/o
1661 * TSPEC changes to the ACs, so they're always the same.
1663 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1664 tid != IEEE80211_NUM_TIDS)
1667 /* if we are in a service period, do nothing */
1668 if (test_sta_flag(sta, WLAN_STA_SP))
1671 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1672 ieee80211_sta_ps_deliver_uapsd(sta);
1674 set_sta_flag(sta, WLAN_STA_UAPSD);
1676 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1678 static ieee80211_rx_result debug_noinline
1679 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1681 struct ieee80211_sub_if_data *sdata = rx->sdata;
1682 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1683 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1688 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1689 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1693 * The device handles station powersave, so don't do anything about
1694 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1695 * it to mac80211 since they're handled.)
1697 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1701 * Don't do anything if the station isn't already asleep. In
1702 * the uAPSD case, the station will probably be marked asleep,
1703 * in the PS-Poll case the station must be confused ...
1705 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1708 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1709 ieee80211_sta_pspoll(&rx->sta->sta);
1711 /* Free PS Poll skb here instead of returning RX_DROP that would
1712 * count as an dropped frame. */
1713 dev_kfree_skb(rx->skb);
1716 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1717 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1718 ieee80211_has_pm(hdr->frame_control) &&
1719 (ieee80211_is_data_qos(hdr->frame_control) ||
1720 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1721 u8 tid = ieee80211_get_tid(hdr);
1723 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1729 static ieee80211_rx_result debug_noinline
1730 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1732 struct sta_info *sta = rx->sta;
1733 struct sk_buff *skb = rx->skb;
1734 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1735 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1742 * Update last_rx only for IBSS packets which are for the current
1743 * BSSID and for station already AUTHORIZED to avoid keeping the
1744 * current IBSS network alive in cases where other STAs start
1745 * using different BSSID. This will also give the station another
1746 * chance to restart the authentication/authorization in case
1747 * something went wrong the first time.
1749 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1750 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1751 NL80211_IFTYPE_ADHOC);
1752 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1753 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1754 sta->rx_stats.last_rx = jiffies;
1755 if (ieee80211_is_data(hdr->frame_control) &&
1756 !is_multicast_ether_addr(hdr->addr1))
1757 sta->rx_stats.last_rate =
1758 sta_stats_encode_rate(status);
1760 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1761 sta->rx_stats.last_rx = jiffies;
1762 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
1763 !is_multicast_ether_addr(hdr->addr1)) {
1765 * Mesh beacons will update last_rx when if they are found to
1766 * match the current local configuration when processed.
1768 sta->rx_stats.last_rx = jiffies;
1769 if (ieee80211_is_data(hdr->frame_control))
1770 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1773 sta->rx_stats.fragments++;
1775 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1776 sta->rx_stats.bytes += rx->skb->len;
1777 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1779 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1780 sta->rx_stats.last_signal = status->signal;
1781 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1784 if (status->chains) {
1785 sta->rx_stats.chains = status->chains;
1786 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1787 int signal = status->chain_signal[i];
1789 if (!(status->chains & BIT(i)))
1792 sta->rx_stats.chain_signal_last[i] = signal;
1793 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1798 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1802 * Change STA power saving mode only at the end of a frame
1803 * exchange sequence, and only for a data or management
1804 * frame as specified in IEEE 802.11-2016 11.2.3.2
1806 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1807 !ieee80211_has_morefrags(hdr->frame_control) &&
1808 !is_multicast_ether_addr(hdr->addr1) &&
1809 (ieee80211_is_mgmt(hdr->frame_control) ||
1810 ieee80211_is_data(hdr->frame_control)) &&
1811 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1812 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1813 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1814 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1815 if (!ieee80211_has_pm(hdr->frame_control))
1818 if (ieee80211_has_pm(hdr->frame_control))
1823 /* mesh power save support */
1824 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1825 ieee80211_mps_rx_h_sta_process(sta, hdr);
1828 * Drop (qos-)data::nullfunc frames silently, since they
1829 * are used only to control station power saving mode.
1831 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1832 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1835 * If we receive a 4-addr nullfunc frame from a STA
1836 * that was not moved to a 4-addr STA vlan yet send
1837 * the event to userspace and for older hostapd drop
1838 * the frame to the monitor interface.
1840 if (ieee80211_has_a4(hdr->frame_control) &&
1841 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1842 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1843 !rx->sdata->u.vlan.sta))) {
1844 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1845 cfg80211_rx_unexpected_4addr_frame(
1846 rx->sdata->dev, sta->sta.addr,
1848 return RX_DROP_MONITOR;
1851 * Update counter and free packet here to avoid
1852 * counting this as a dropped packed.
1854 sta->rx_stats.packets++;
1855 dev_kfree_skb(rx->skb);
1860 } /* ieee80211_rx_h_sta_process */
1862 static struct ieee80211_key *
1863 ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1865 struct ieee80211_key *key = NULL;
1866 struct ieee80211_sub_if_data *sdata = rx->sdata;
1869 /* Make sure key gets set if either BIGTK key index is set so that
1870 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1871 * Beacon frames and Beacon frames that claim to use another BIGTK key
1872 * index (i.e., a key that we do not have).
1876 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1879 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1886 key = rcu_dereference(rx->sta->gtk[idx]);
1888 key = rcu_dereference(sdata->keys[idx]);
1889 if (!key && rx->sta)
1890 key = rcu_dereference(rx->sta->gtk[idx2]);
1892 key = rcu_dereference(sdata->keys[idx2]);
1897 static ieee80211_rx_result debug_noinline
1898 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1900 struct sk_buff *skb = rx->skb;
1901 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1902 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1904 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1905 struct ieee80211_key *sta_ptk = NULL;
1906 struct ieee80211_key *ptk_idx = NULL;
1907 int mmie_keyidx = -1;
1909 const struct ieee80211_cipher_scheme *cs = NULL;
1911 if (ieee80211_is_ext(hdr->frame_control))
1917 * There are five types of keys:
1918 * - GTK (group keys)
1919 * - IGTK (group keys for management frames)
1920 * - BIGTK (group keys for Beacon frames)
1921 * - PTK (pairwise keys)
1922 * - STK (station-to-station pairwise keys)
1924 * When selecting a key, we have to distinguish between multicast
1925 * (including broadcast) and unicast frames, the latter can only
1926 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1927 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1928 * then unicast frames can also use key indices like GTKs. Hence, if we
1929 * don't have a PTK/STK we check the key index for a WEP key.
1931 * Note that in a regular BSS, multicast frames are sent by the
1932 * AP only, associated stations unicast the frame to the AP first
1933 * which then multicasts it on their behalf.
1935 * There is also a slight problem in IBSS mode: GTKs are negotiated
1936 * with each station, that is something we don't currently handle.
1937 * The spec seems to expect that one negotiates the same key with
1938 * every station but there's no such requirement; VLANs could be
1942 /* start without a key */
1944 fc = hdr->frame_control;
1947 int keyid = rx->sta->ptk_idx;
1948 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1950 if (ieee80211_has_protected(fc)) {
1951 cs = rx->sta->cipher_scheme;
1952 keyid = ieee80211_get_keyid(rx->skb, cs);
1954 if (unlikely(keyid < 0))
1955 return RX_DROP_UNUSABLE;
1957 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
1961 if (!ieee80211_has_protected(fc))
1962 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1964 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1965 rx->key = ptk_idx ? ptk_idx : sta_ptk;
1966 if ((status->flag & RX_FLAG_DECRYPTED) &&
1967 (status->flag & RX_FLAG_IV_STRIPPED))
1969 /* Skip decryption if the frame is not protected. */
1970 if (!ieee80211_has_protected(fc))
1972 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1973 /* Broadcast/multicast robust management frame / BIP */
1974 if ((status->flag & RX_FLAG_DECRYPTED) &&
1975 (status->flag & RX_FLAG_IV_STRIPPED))
1978 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1979 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
1980 NUM_DEFAULT_BEACON_KEYS) {
1981 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1984 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1987 rx->key = ieee80211_rx_get_bigtk(rx, mmie_keyidx);
1989 return RX_CONTINUE; /* Beacon protection not in use */
1990 } else if (mmie_keyidx >= 0) {
1991 /* Broadcast/multicast robust management frame / BIP */
1992 if ((status->flag & RX_FLAG_DECRYPTED) &&
1993 (status->flag & RX_FLAG_IV_STRIPPED))
1996 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1997 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1998 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
2000 if (ieee80211_is_group_privacy_action(skb) &&
2001 test_sta_flag(rx->sta, WLAN_STA_MFP))
2002 return RX_DROP_MONITOR;
2004 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
2007 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
2008 } else if (!ieee80211_has_protected(fc)) {
2010 * The frame was not protected, so skip decryption. However, we
2011 * need to set rx->key if there is a key that could have been
2012 * used so that the frame may be dropped if encryption would
2013 * have been expected.
2015 struct ieee80211_key *key = NULL;
2016 struct ieee80211_sub_if_data *sdata = rx->sdata;
2019 if (ieee80211_is_beacon(fc)) {
2020 key = ieee80211_rx_get_bigtk(rx, -1);
2021 } else if (ieee80211_is_mgmt(fc) &&
2022 is_multicast_ether_addr(hdr->addr1)) {
2023 key = rcu_dereference(rx->sdata->default_mgmt_key);
2026 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2027 key = rcu_dereference(rx->sta->gtk[i]);
2033 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
2034 key = rcu_dereference(sdata->keys[i]);
2045 * The device doesn't give us the IV so we won't be
2046 * able to look up the key. That's ok though, we
2047 * don't need to decrypt the frame, we just won't
2048 * be able to keep statistics accurate.
2049 * Except for key threshold notifications, should
2050 * we somehow allow the driver to tell us which key
2051 * the hardware used if this flag is set?
2053 if ((status->flag & RX_FLAG_DECRYPTED) &&
2054 (status->flag & RX_FLAG_IV_STRIPPED))
2057 keyidx = ieee80211_get_keyid(rx->skb, cs);
2059 if (unlikely(keyidx < 0))
2060 return RX_DROP_UNUSABLE;
2062 /* check per-station GTK first, if multicast packet */
2063 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
2064 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
2066 /* if not found, try default key */
2068 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
2071 * RSNA-protected unicast frames should always be
2072 * sent with pairwise or station-to-station keys,
2073 * but for WEP we allow using a key index as well.
2076 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
2077 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
2078 !is_multicast_ether_addr(hdr->addr1))
2084 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
2085 return RX_DROP_MONITOR;
2087 /* TODO: add threshold stuff again */
2089 return RX_DROP_MONITOR;
2092 switch (rx->key->conf.cipher) {
2093 case WLAN_CIPHER_SUITE_WEP40:
2094 case WLAN_CIPHER_SUITE_WEP104:
2095 result = ieee80211_crypto_wep_decrypt(rx);
2097 case WLAN_CIPHER_SUITE_TKIP:
2098 result = ieee80211_crypto_tkip_decrypt(rx);
2100 case WLAN_CIPHER_SUITE_CCMP:
2101 result = ieee80211_crypto_ccmp_decrypt(
2102 rx, IEEE80211_CCMP_MIC_LEN);
2104 case WLAN_CIPHER_SUITE_CCMP_256:
2105 result = ieee80211_crypto_ccmp_decrypt(
2106 rx, IEEE80211_CCMP_256_MIC_LEN);
2108 case WLAN_CIPHER_SUITE_AES_CMAC:
2109 result = ieee80211_crypto_aes_cmac_decrypt(rx);
2111 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2112 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
2114 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2115 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2116 result = ieee80211_crypto_aes_gmac_decrypt(rx);
2118 case WLAN_CIPHER_SUITE_GCMP:
2119 case WLAN_CIPHER_SUITE_GCMP_256:
2120 result = ieee80211_crypto_gcmp_decrypt(rx);
2123 result = ieee80211_crypto_hw_decrypt(rx);
2126 /* the hdr variable is invalid after the decrypt handlers */
2128 /* either the frame has been decrypted or will be dropped */
2129 status->flag |= RX_FLAG_DECRYPTED;
2131 if (unlikely(ieee80211_is_beacon(fc) && result == RX_DROP_UNUSABLE))
2132 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2133 skb->data, skb->len);
2138 void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2142 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2143 skb_queue_head_init(&cache->entries[i].skb_list);
2146 void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2150 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2151 __skb_queue_purge(&cache->entries[i].skb_list);
2154 static inline struct ieee80211_fragment_entry *
2155 ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
2156 unsigned int frag, unsigned int seq, int rx_queue,
2157 struct sk_buff **skb)
2159 struct ieee80211_fragment_entry *entry;
2161 entry = &cache->entries[cache->next++];
2162 if (cache->next >= IEEE80211_FRAGMENT_MAX)
2165 __skb_queue_purge(&entry->skb_list);
2167 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2169 entry->first_frag_time = jiffies;
2171 entry->rx_queue = rx_queue;
2172 entry->last_frag = frag;
2173 entry->check_sequential_pn = false;
2174 entry->extra_len = 0;
2179 static inline struct ieee80211_fragment_entry *
2180 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
2181 unsigned int frag, unsigned int seq,
2182 int rx_queue, struct ieee80211_hdr *hdr)
2184 struct ieee80211_fragment_entry *entry;
2188 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2189 struct ieee80211_hdr *f_hdr;
2190 struct sk_buff *f_skb;
2194 idx = IEEE80211_FRAGMENT_MAX - 1;
2196 entry = &cache->entries[idx];
2197 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2198 entry->rx_queue != rx_queue ||
2199 entry->last_frag + 1 != frag)
2202 f_skb = __skb_peek(&entry->skb_list);
2203 f_hdr = (struct ieee80211_hdr *) f_skb->data;
2206 * Check ftype and addresses are equal, else check next fragment
2208 if (((hdr->frame_control ^ f_hdr->frame_control) &
2209 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
2210 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2211 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
2214 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
2215 __skb_queue_purge(&entry->skb_list);
2224 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2227 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2228 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2229 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2230 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2231 ieee80211_has_protected(fc);
2234 static ieee80211_rx_result debug_noinline
2235 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
2237 struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
2238 struct ieee80211_hdr *hdr;
2241 unsigned int frag, seq;
2242 struct ieee80211_fragment_entry *entry;
2243 struct sk_buff *skb;
2244 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2246 hdr = (struct ieee80211_hdr *)rx->skb->data;
2247 fc = hdr->frame_control;
2249 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
2252 sc = le16_to_cpu(hdr->seq_ctrl);
2253 frag = sc & IEEE80211_SCTL_FRAG;
2256 cache = &rx->sta->frags;
2258 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2261 if (is_multicast_ether_addr(hdr->addr1))
2262 return RX_DROP_MONITOR;
2264 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2266 if (skb_linearize(rx->skb))
2267 return RX_DROP_UNUSABLE;
2270 * skb_linearize() might change the skb->data and
2271 * previously cached variables (in this case, hdr) need to
2272 * be refreshed with the new data.
2274 hdr = (struct ieee80211_hdr *)rx->skb->data;
2275 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2278 /* This is the first fragment of a new frame. */
2279 entry = ieee80211_reassemble_add(cache, frag, seq,
2280 rx->seqno_idx, &(rx->skb));
2281 if (requires_sequential_pn(rx, fc)) {
2282 int queue = rx->security_idx;
2284 /* Store CCMP/GCMP PN so that we can verify that the
2285 * next fragment has a sequential PN value.
2287 entry->check_sequential_pn = true;
2288 entry->is_protected = true;
2289 entry->key_color = rx->key->color;
2290 memcpy(entry->last_pn,
2291 rx->key->u.ccmp.rx_pn[queue],
2292 IEEE80211_CCMP_PN_LEN);
2293 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2295 offsetof(struct ieee80211_key,
2297 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2298 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2299 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2300 IEEE80211_GCMP_PN_LEN);
2301 } else if (rx->key &&
2302 (ieee80211_has_protected(fc) ||
2303 (status->flag & RX_FLAG_DECRYPTED))) {
2304 entry->is_protected = true;
2305 entry->key_color = rx->key->color;
2310 /* This is a fragment for a frame that should already be pending in
2311 * fragment cache. Add this fragment to the end of the pending entry.
2313 entry = ieee80211_reassemble_find(cache, frag, seq,
2314 rx->seqno_idx, hdr);
2316 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2317 return RX_DROP_MONITOR;
2320 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2321 * MPDU PN values are not incrementing in steps of 1."
2322 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2323 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2325 if (entry->check_sequential_pn) {
2327 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2329 if (!requires_sequential_pn(rx, fc))
2330 return RX_DROP_UNUSABLE;
2332 /* Prevent mixed key and fragment cache attacks */
2333 if (entry->key_color != rx->key->color)
2334 return RX_DROP_UNUSABLE;
2336 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2337 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2343 rpn = rx->ccm_gcm.pn;
2344 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2345 return RX_DROP_UNUSABLE;
2346 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2347 } else if (entry->is_protected &&
2349 (!ieee80211_has_protected(fc) &&
2350 !(status->flag & RX_FLAG_DECRYPTED)) ||
2351 rx->key->color != entry->key_color)) {
2352 /* Drop this as a mixed key or fragment cache attack, even
2353 * if for TKIP Michael MIC should protect us, and WEP is a
2354 * lost cause anyway.
2356 return RX_DROP_UNUSABLE;
2357 } else if (entry->is_protected && rx->key &&
2358 entry->key_color != rx->key->color &&
2359 (status->flag & RX_FLAG_DECRYPTED)) {
2360 return RX_DROP_UNUSABLE;
2363 skb_pull(rx->skb, ieee80211_hdrlen(fc));
2364 __skb_queue_tail(&entry->skb_list, rx->skb);
2365 entry->last_frag = frag;
2366 entry->extra_len += rx->skb->len;
2367 if (ieee80211_has_morefrags(fc)) {
2372 rx->skb = __skb_dequeue(&entry->skb_list);
2373 if (skb_tailroom(rx->skb) < entry->extra_len) {
2374 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2375 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2377 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2378 __skb_queue_purge(&entry->skb_list);
2379 return RX_DROP_UNUSABLE;
2382 while ((skb = __skb_dequeue(&entry->skb_list))) {
2383 skb_put_data(rx->skb, skb->data, skb->len);
2388 ieee80211_led_rx(rx->local);
2390 rx->sta->rx_stats.packets++;
2394 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2396 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2402 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2404 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
2405 struct sk_buff *skb = rx->skb;
2406 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2409 * Pass through unencrypted frames if the hardware has
2410 * decrypted them already.
2412 if (status->flag & RX_FLAG_DECRYPTED)
2415 /* check mesh EAPOL frames first */
2416 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2417 ieee80211_is_data(fc))) {
2418 struct ieee80211s_hdr *mesh_hdr;
2419 u16 hdr_len = ieee80211_hdrlen(fc);
2420 u16 ethertype_offset;
2423 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2426 /* make sure fixed part of mesh header is there, also checks skb len */
2427 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2430 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2431 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2432 sizeof(rfc1042_header);
2434 if (skb_copy_bits(rx->skb, ethertype_offset, ðertype, 2) == 0 &&
2435 ethertype == rx->sdata->control_port_protocol)
2440 /* Drop unencrypted frames if key is set. */
2441 if (unlikely(!ieee80211_has_protected(fc) &&
2442 !ieee80211_is_any_nullfunc(fc) &&
2443 ieee80211_is_data(fc) && rx->key))
2449 static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2451 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2452 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2453 __le16 fc = hdr->frame_control;
2456 * Pass through unencrypted frames if the hardware has
2457 * decrypted them already.
2459 if (status->flag & RX_FLAG_DECRYPTED)
2462 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2463 if (unlikely(!ieee80211_has_protected(fc) &&
2464 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2466 if (ieee80211_is_deauth(fc) ||
2467 ieee80211_is_disassoc(fc))
2468 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2473 /* BIP does not use Protected field, so need to check MMIE */
2474 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2475 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2476 if (ieee80211_is_deauth(fc) ||
2477 ieee80211_is_disassoc(fc))
2478 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2483 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
2484 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2485 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2491 * When using MFP, Action frames are not allowed prior to
2492 * having configured keys.
2494 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2495 ieee80211_is_robust_mgmt_frame(rx->skb)))
2503 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2505 struct ieee80211_sub_if_data *sdata = rx->sdata;
2506 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2507 bool check_port_control = false;
2508 struct ethhdr *ehdr;
2511 *port_control = false;
2512 if (ieee80211_has_a4(hdr->frame_control) &&
2513 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2516 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2517 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2519 if (!sdata->u.mgd.use_4addr)
2521 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
2522 check_port_control = true;
2525 if (is_multicast_ether_addr(hdr->addr1) &&
2526 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2529 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2533 ehdr = (struct ethhdr *) rx->skb->data;
2534 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2535 *port_control = true;
2536 else if (check_port_control)
2543 * requires that rx->skb is a frame with ethernet header
2545 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2547 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2548 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2549 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2552 * Allow EAPOL frames to us/the PAE group address regardless of
2553 * whether the frame was encrypted or not, and always disallow
2554 * all other destination addresses for them.
2556 if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2557 return ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2558 ether_addr_equal(ehdr->h_dest, pae_group_addr);
2560 if (ieee80211_802_1x_port_control(rx) ||
2561 ieee80211_drop_unencrypted(rx, fc))
2567 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2568 struct ieee80211_rx_data *rx)
2570 struct ieee80211_sub_if_data *sdata = rx->sdata;
2571 struct net_device *dev = sdata->dev;
2573 if (unlikely((skb->protocol == sdata->control_port_protocol ||
2574 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2575 !sdata->control_port_no_preauth)) &&
2576 sdata->control_port_over_nl80211)) {
2577 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2578 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
2580 cfg80211_rx_control_port(dev, skb, noencrypt);
2583 struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2585 memset(skb->cb, 0, sizeof(skb->cb));
2588 * 802.1X over 802.11 requires that the authenticator address
2589 * be used for EAPOL frames. However, 802.1X allows the use of
2590 * the PAE group address instead. If the interface is part of
2591 * a bridge and we pass the frame with the PAE group address,
2592 * then the bridge will forward it to the network (even if the
2593 * client was not associated yet), which isn't supposed to
2595 * To avoid that, rewrite the destination address to our own
2596 * address, so that the authenticator (e.g. hostapd) will see
2597 * the frame, but bridge won't forward it anywhere else. Note
2598 * that due to earlier filtering, the only other address can
2599 * be the PAE group address.
2601 if (unlikely(skb->protocol == sdata->control_port_protocol &&
2602 !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2603 ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2605 /* deliver to local stack */
2607 list_add_tail(&skb->list, rx->list);
2609 netif_receive_skb(skb);
2614 * requires that rx->skb is a frame with ethernet header
2617 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2619 struct ieee80211_sub_if_data *sdata = rx->sdata;
2620 struct net_device *dev = sdata->dev;
2621 struct sk_buff *skb, *xmit_skb;
2622 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2623 struct sta_info *dsta;
2628 dev_sw_netstats_rx_add(dev, skb->len);
2631 /* The seqno index has the same property as needed
2632 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2633 * for non-QoS-data frames. Here we know it's a data
2634 * frame, so count MSDUs.
2636 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2637 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2638 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2641 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2642 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2643 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2644 ehdr->h_proto != rx->sdata->control_port_protocol &&
2645 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2646 if (is_multicast_ether_addr(ehdr->h_dest) &&
2647 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2649 * send multicast frames both to higher layers in
2650 * local net stack and back to the wireless medium
2652 xmit_skb = skb_copy(skb, GFP_ATOMIC);
2654 net_info_ratelimited("%s: failed to clone multicast frame\n",
2656 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2657 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2658 dsta = sta_info_get(sdata, ehdr->h_dest);
2661 * The destination station is associated to
2662 * this AP (in this VLAN), so send the frame
2663 * directly to it and do not pass it to local
2672 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2674 /* 'align' will only take the values 0 or 2 here since all
2675 * frames are required to be aligned to 2-byte boundaries
2676 * when being passed to mac80211; the code here works just
2677 * as well if that isn't true, but mac80211 assumes it can
2678 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2682 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2684 if (WARN_ON(skb_headroom(skb) < 3)) {
2688 u8 *data = skb->data;
2689 size_t len = skb_headlen(skb);
2691 memmove(skb->data, data, len);
2692 skb_set_tail_pointer(skb, len);
2699 skb->protocol = eth_type_trans(skb, dev);
2700 ieee80211_deliver_skb_to_local_stack(skb, rx);
2705 * Send to wireless media and increase priority by 256 to
2706 * keep the received priority instead of reclassifying
2707 * the frame (see cfg80211_classify8021d).
2709 xmit_skb->priority += 256;
2710 xmit_skb->protocol = htons(ETH_P_802_3);
2711 skb_reset_network_header(xmit_skb);
2712 skb_reset_mac_header(xmit_skb);
2713 dev_queue_xmit(xmit_skb);
2717 static ieee80211_rx_result debug_noinline
2718 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
2720 struct net_device *dev = rx->sdata->dev;
2721 struct sk_buff *skb = rx->skb;
2722 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2723 __le16 fc = hdr->frame_control;
2724 struct sk_buff_head frame_list;
2725 struct ethhdr ethhdr;
2726 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
2728 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2731 } else switch (rx->sdata->vif.type) {
2732 case NL80211_IFTYPE_AP:
2733 case NL80211_IFTYPE_AP_VLAN:
2736 case NL80211_IFTYPE_STATION:
2738 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2741 case NL80211_IFTYPE_MESH_POINT:
2749 __skb_queue_head_init(&frame_list);
2751 if (ieee80211_data_to_8023_exthdr(skb, ðhdr,
2752 rx->sdata->vif.addr,
2753 rx->sdata->vif.type,
2755 return RX_DROP_UNUSABLE;
2757 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2758 rx->sdata->vif.type,
2759 rx->local->hw.extra_tx_headroom,
2760 check_da, check_sa);
2762 while (!skb_queue_empty(&frame_list)) {
2763 rx->skb = __skb_dequeue(&frame_list);
2765 if (!ieee80211_frame_allowed(rx, fc)) {
2766 dev_kfree_skb(rx->skb);
2770 ieee80211_deliver_skb(rx);
2776 static ieee80211_rx_result debug_noinline
2777 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2779 struct sk_buff *skb = rx->skb;
2780 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2781 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2782 __le16 fc = hdr->frame_control;
2784 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2787 if (unlikely(!ieee80211_is_data(fc)))
2790 if (unlikely(!ieee80211_is_data_present(fc)))
2791 return RX_DROP_MONITOR;
2793 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2794 switch (rx->sdata->vif.type) {
2795 case NL80211_IFTYPE_AP_VLAN:
2796 if (!rx->sdata->u.vlan.sta)
2797 return RX_DROP_UNUSABLE;
2799 case NL80211_IFTYPE_STATION:
2800 if (!rx->sdata->u.mgd.use_4addr)
2801 return RX_DROP_UNUSABLE;
2804 return RX_DROP_UNUSABLE;
2808 if (is_multicast_ether_addr(hdr->addr1))
2809 return RX_DROP_UNUSABLE;
2813 * We should not receive A-MSDUs on pre-HT connections,
2814 * and HT connections cannot use old ciphers. Thus drop
2815 * them, as in those cases we couldn't even have SPP
2818 switch (rx->key->conf.cipher) {
2819 case WLAN_CIPHER_SUITE_WEP40:
2820 case WLAN_CIPHER_SUITE_WEP104:
2821 case WLAN_CIPHER_SUITE_TKIP:
2822 return RX_DROP_UNUSABLE;
2828 return __ieee80211_rx_h_amsdu(rx, 0);
2831 #ifdef CONFIG_MAC80211_MESH
2832 static ieee80211_rx_result
2833 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2835 struct ieee80211_hdr *fwd_hdr, *hdr;
2836 struct ieee80211_tx_info *info;
2837 struct ieee80211s_hdr *mesh_hdr;
2838 struct sk_buff *skb = rx->skb, *fwd_skb;
2839 struct ieee80211_local *local = rx->local;
2840 struct ieee80211_sub_if_data *sdata = rx->sdata;
2841 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2845 hdr = (struct ieee80211_hdr *) skb->data;
2846 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2848 /* make sure fixed part of mesh header is there, also checks skb len */
2849 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2850 return RX_DROP_MONITOR;
2852 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2854 /* make sure full mesh header is there, also checks skb len */
2855 if (!pskb_may_pull(rx->skb,
2856 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2857 return RX_DROP_MONITOR;
2859 /* reload pointers */
2860 hdr = (struct ieee80211_hdr *) skb->data;
2861 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2863 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2864 return RX_DROP_MONITOR;
2866 /* frame is in RMC, don't forward */
2867 if (ieee80211_is_data(hdr->frame_control) &&
2868 is_multicast_ether_addr(hdr->addr1) &&
2869 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2870 return RX_DROP_MONITOR;
2872 if (!ieee80211_is_data(hdr->frame_control))
2876 return RX_DROP_MONITOR;
2878 if (mesh_hdr->flags & MESH_FLAGS_AE) {
2879 struct mesh_path *mppath;
2883 if (is_multicast_ether_addr(hdr->addr1)) {
2884 mpp_addr = hdr->addr3;
2885 proxied_addr = mesh_hdr->eaddr1;
2886 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2887 MESH_FLAGS_AE_A5_A6) {
2888 /* has_a4 already checked in ieee80211_rx_mesh_check */
2889 mpp_addr = hdr->addr4;
2890 proxied_addr = mesh_hdr->eaddr2;
2892 return RX_DROP_MONITOR;
2896 mppath = mpp_path_lookup(sdata, proxied_addr);
2898 mpp_path_add(sdata, proxied_addr, mpp_addr);
2900 spin_lock_bh(&mppath->state_lock);
2901 if (!ether_addr_equal(mppath->mpp, mpp_addr))
2902 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2903 mppath->exp_time = jiffies;
2904 spin_unlock_bh(&mppath->state_lock);
2909 /* Frame has reached destination. Don't forward */
2910 if (!is_multicast_ether_addr(hdr->addr1) &&
2911 ether_addr_equal(sdata->vif.addr, hdr->addr3))
2914 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2915 q = sdata->vif.hw_queue[ac];
2916 if (ieee80211_queue_stopped(&local->hw, q)) {
2917 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2918 return RX_DROP_MONITOR;
2920 skb_set_queue_mapping(skb, q);
2922 if (!--mesh_hdr->ttl) {
2923 if (!is_multicast_ether_addr(hdr->addr1))
2924 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2925 dropped_frames_ttl);
2929 if (!ifmsh->mshcfg.dot11MeshForwarding)
2932 if (sdata->crypto_tx_tailroom_needed_cnt)
2933 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2935 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
2936 sdata->encrypt_headroom,
2937 tailroom, GFP_ATOMIC);
2941 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
2942 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2943 info = IEEE80211_SKB_CB(fwd_skb);
2944 memset(info, 0, sizeof(*info));
2945 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
2946 info->control.vif = &rx->sdata->vif;
2947 info->control.jiffies = jiffies;
2948 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2949 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2950 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2951 /* update power mode indication when forwarding */
2952 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2953 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2954 /* mesh power mode flags updated in mesh_nexthop_lookup */
2955 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2957 /* unable to resolve next hop */
2958 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2960 WLAN_REASON_MESH_PATH_NOFORWARD,
2962 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2964 return RX_DROP_MONITOR;
2967 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2968 ieee80211_add_pending_skb(local, fwd_skb);
2970 if (is_multicast_ether_addr(hdr->addr1))
2972 return RX_DROP_MONITOR;
2976 static ieee80211_rx_result debug_noinline
2977 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2979 struct ieee80211_sub_if_data *sdata = rx->sdata;
2980 struct ieee80211_local *local = rx->local;
2981 struct net_device *dev = sdata->dev;
2982 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2983 __le16 fc = hdr->frame_control;
2987 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2990 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2991 return RX_DROP_MONITOR;
2994 * Send unexpected-4addr-frame event to hostapd. For older versions,
2995 * also drop the frame to cooked monitor interfaces.
2997 if (ieee80211_has_a4(hdr->frame_control) &&
2998 sdata->vif.type == NL80211_IFTYPE_AP) {
3000 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3001 cfg80211_rx_unexpected_4addr_frame(
3002 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
3003 return RX_DROP_MONITOR;
3006 err = __ieee80211_data_to_8023(rx, &port_control);
3008 return RX_DROP_UNUSABLE;
3010 if (!ieee80211_frame_allowed(rx, fc))
3011 return RX_DROP_MONITOR;
3013 /* directly handle TDLS channel switch requests/responses */
3014 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3015 cpu_to_be16(ETH_P_TDLS))) {
3016 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3018 if (pskb_may_pull(rx->skb,
3019 offsetof(struct ieee80211_tdls_data, u)) &&
3020 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3021 tf->category == WLAN_CATEGORY_TDLS &&
3022 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3023 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
3024 rx->skb->protocol = cpu_to_be16(ETH_P_TDLS);
3025 __ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
3030 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3031 unlikely(port_control) && sdata->bss) {
3032 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3040 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3041 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
3042 !is_multicast_ether_addr(
3043 ((struct ethhdr *)rx->skb->data)->h_dest) &&
3044 (!local->scanning &&
3045 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3046 mod_timer(&local->dynamic_ps_timer, jiffies +
3047 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
3049 ieee80211_deliver_skb(rx);
3054 static ieee80211_rx_result debug_noinline
3055 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
3057 struct sk_buff *skb = rx->skb;
3058 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
3059 struct tid_ampdu_rx *tid_agg_rx;
3063 if (likely(!ieee80211_is_ctl(bar->frame_control)))
3066 if (ieee80211_is_back_req(bar->frame_control)) {
3068 __le16 control, start_seq_num;
3069 } __packed bar_data;
3070 struct ieee80211_event event = {
3071 .type = BAR_RX_EVENT,
3075 return RX_DROP_MONITOR;
3077 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3078 &bar_data, sizeof(bar_data)))
3079 return RX_DROP_MONITOR;
3081 tid = le16_to_cpu(bar_data.control) >> 12;
3083 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3084 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3085 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3086 WLAN_BACK_RECIPIENT,
3087 WLAN_REASON_QSTA_REQUIRE_SETUP);
3089 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3091 return RX_DROP_MONITOR;
3093 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
3094 event.u.ba.tid = tid;
3095 event.u.ba.ssn = start_seq_num;
3096 event.u.ba.sta = &rx->sta->sta;
3098 /* reset session timer */
3099 if (tid_agg_rx->timeout)
3100 mod_timer(&tid_agg_rx->session_timer,
3101 TU_TO_EXP_TIME(tid_agg_rx->timeout));
3103 spin_lock(&tid_agg_rx->reorder_lock);
3104 /* release stored frames up to start of BAR */
3105 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
3106 start_seq_num, frames);
3107 spin_unlock(&tid_agg_rx->reorder_lock);
3109 drv_event_callback(rx->local, rx->sdata, &event);
3116 * After this point, we only want management frames,
3117 * so we can drop all remaining control frames to
3118 * cooked monitor interfaces.
3120 return RX_DROP_MONITOR;
3123 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3124 struct ieee80211_mgmt *mgmt,
3127 struct ieee80211_local *local = sdata->local;
3128 struct sk_buff *skb;
3129 struct ieee80211_mgmt *resp;
3131 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
3132 /* Not to own unicast address */
3136 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3137 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
3138 /* Not from the current AP or not associated yet. */
3142 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3143 /* Too short SA Query request frame */
3147 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3151 skb_reserve(skb, local->hw.extra_tx_headroom);
3152 resp = skb_put_zero(skb, 24);
3153 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3154 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3155 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
3156 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3157 IEEE80211_STYPE_ACTION);
3158 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3159 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3160 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3161 memcpy(resp->u.action.u.sa_query.trans_id,
3162 mgmt->u.action.u.sa_query.trans_id,
3163 WLAN_SA_QUERY_TR_ID_LEN);
3165 ieee80211_tx_skb(sdata, skb);
3168 static ieee80211_rx_result debug_noinline
3169 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3171 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3172 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3174 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3178 * From here on, look only at management frames.
3179 * Data and control frames are already handled,
3180 * and unknown (reserved) frames are useless.
3182 if (rx->skb->len < 24)
3183 return RX_DROP_MONITOR;
3185 if (!ieee80211_is_mgmt(mgmt->frame_control))
3186 return RX_DROP_MONITOR;
3188 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3189 ieee80211_is_beacon(mgmt->frame_control) &&
3190 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3193 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3194 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3195 sig = status->signal;
3197 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3198 rx->skb->data, rx->skb->len,
3199 ieee80211_rx_status_to_khz(status),
3201 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3204 if (ieee80211_drop_unencrypted_mgmt(rx))
3205 return RX_DROP_UNUSABLE;
3210 static ieee80211_rx_result debug_noinline
3211 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3213 struct ieee80211_local *local = rx->local;
3214 struct ieee80211_sub_if_data *sdata = rx->sdata;
3215 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3216 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3217 int len = rx->skb->len;
3219 if (!ieee80211_is_action(mgmt->frame_control))
3222 /* drop too small frames */
3223 if (len < IEEE80211_MIN_ACTION_SIZE)
3224 return RX_DROP_UNUSABLE;
3226 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3227 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3228 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3229 return RX_DROP_UNUSABLE;
3231 switch (mgmt->u.action.category) {
3232 case WLAN_CATEGORY_HT:
3233 /* reject HT action frames from stations not supporting HT */
3234 if (!rx->sta->sta.ht_cap.ht_supported)
3237 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3238 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3239 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3240 sdata->vif.type != NL80211_IFTYPE_AP &&
3241 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3244 /* verify action & smps_control/chanwidth are present */
3245 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3248 switch (mgmt->u.action.u.ht_smps.action) {
3249 case WLAN_HT_ACTION_SMPS: {
3250 struct ieee80211_supported_band *sband;
3251 enum ieee80211_smps_mode smps_mode;
3252 struct sta_opmode_info sta_opmode = {};
3254 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3255 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3258 /* convert to HT capability */
3259 switch (mgmt->u.action.u.ht_smps.smps_control) {
3260 case WLAN_HT_SMPS_CONTROL_DISABLED:
3261 smps_mode = IEEE80211_SMPS_OFF;
3263 case WLAN_HT_SMPS_CONTROL_STATIC:
3264 smps_mode = IEEE80211_SMPS_STATIC;
3266 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3267 smps_mode = IEEE80211_SMPS_DYNAMIC;
3273 /* if no change do nothing */
3274 if (rx->sta->sta.smps_mode == smps_mode)
3276 rx->sta->sta.smps_mode = smps_mode;
3277 sta_opmode.smps_mode =
3278 ieee80211_smps_mode_to_smps_mode(smps_mode);
3279 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3281 sband = rx->local->hw.wiphy->bands[status->band];
3283 rate_control_rate_update(local, sband, rx->sta,
3284 IEEE80211_RC_SMPS_CHANGED);
3285 cfg80211_sta_opmode_change_notify(sdata->dev,
3291 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3292 struct ieee80211_supported_band *sband;
3293 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
3294 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
3295 struct sta_opmode_info sta_opmode = {};
3297 /* If it doesn't support 40 MHz it can't change ... */
3298 if (!(rx->sta->sta.ht_cap.cap &
3299 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3302 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
3303 max_bw = IEEE80211_STA_RX_BW_20;
3305 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3307 /* set cur_max_bandwidth and recalc sta bw */
3308 rx->sta->cur_max_bandwidth = max_bw;
3309 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
3311 if (rx->sta->sta.bandwidth == new_bw)
3314 rx->sta->sta.bandwidth = new_bw;
3315 sband = rx->local->hw.wiphy->bands[status->band];
3317 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
3318 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
3320 rate_control_rate_update(local, sband, rx->sta,
3321 IEEE80211_RC_BW_CHANGED);
3322 cfg80211_sta_opmode_change_notify(sdata->dev,
3333 case WLAN_CATEGORY_PUBLIC:
3334 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3336 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3340 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3342 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3343 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3345 if (len < offsetof(struct ieee80211_mgmt,
3346 u.action.u.ext_chan_switch.variable))
3349 case WLAN_CATEGORY_VHT:
3350 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3351 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3352 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3353 sdata->vif.type != NL80211_IFTYPE_AP &&
3354 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3357 /* verify action code is present */
3358 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3361 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3362 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3363 /* verify opmode is present */
3364 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3368 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3369 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3377 case WLAN_CATEGORY_BACK:
3378 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3379 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3380 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3381 sdata->vif.type != NL80211_IFTYPE_AP &&
3382 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3385 /* verify action_code is present */
3386 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3389 switch (mgmt->u.action.u.addba_req.action_code) {
3390 case WLAN_ACTION_ADDBA_REQ:
3391 if (len < (IEEE80211_MIN_ACTION_SIZE +
3392 sizeof(mgmt->u.action.u.addba_req)))
3395 case WLAN_ACTION_ADDBA_RESP:
3396 if (len < (IEEE80211_MIN_ACTION_SIZE +
3397 sizeof(mgmt->u.action.u.addba_resp)))
3400 case WLAN_ACTION_DELBA:
3401 if (len < (IEEE80211_MIN_ACTION_SIZE +
3402 sizeof(mgmt->u.action.u.delba)))
3410 case WLAN_CATEGORY_SPECTRUM_MGMT:
3411 /* verify action_code is present */
3412 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3415 switch (mgmt->u.action.u.measurement.action_code) {
3416 case WLAN_ACTION_SPCT_MSR_REQ:
3417 if (status->band != NL80211_BAND_5GHZ)
3420 if (len < (IEEE80211_MIN_ACTION_SIZE +
3421 sizeof(mgmt->u.action.u.measurement)))
3424 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3427 ieee80211_process_measurement_req(sdata, mgmt, len);
3429 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3431 if (len < (IEEE80211_MIN_ACTION_SIZE +
3432 sizeof(mgmt->u.action.u.chan_switch)))
3435 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3436 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3437 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3440 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3441 bssid = sdata->u.mgd.bssid;
3442 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3443 bssid = sdata->u.ibss.bssid;
3444 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3449 if (!ether_addr_equal(mgmt->bssid, bssid))
3456 case WLAN_CATEGORY_SELF_PROTECTED:
3457 if (len < (IEEE80211_MIN_ACTION_SIZE +
3458 sizeof(mgmt->u.action.u.self_prot.action_code)))
3461 switch (mgmt->u.action.u.self_prot.action_code) {
3462 case WLAN_SP_MESH_PEERING_OPEN:
3463 case WLAN_SP_MESH_PEERING_CLOSE:
3464 case WLAN_SP_MESH_PEERING_CONFIRM:
3465 if (!ieee80211_vif_is_mesh(&sdata->vif))
3467 if (sdata->u.mesh.user_mpm)
3468 /* userspace handles this frame */
3471 case WLAN_SP_MGK_INFORM:
3472 case WLAN_SP_MGK_ACK:
3473 if (!ieee80211_vif_is_mesh(&sdata->vif))
3478 case WLAN_CATEGORY_MESH_ACTION:
3479 if (len < (IEEE80211_MIN_ACTION_SIZE +
3480 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3483 if (!ieee80211_vif_is_mesh(&sdata->vif))
3485 if (mesh_action_is_path_sel(mgmt) &&
3486 !mesh_path_sel_is_hwmp(sdata))
3494 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3495 /* will return in the next handlers */
3500 rx->sta->rx_stats.packets++;
3501 dev_kfree_skb(rx->skb);
3505 ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
3509 static ieee80211_rx_result debug_noinline
3510 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3512 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3515 /* skip known-bad action frames and return them in the next handler */
3516 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3520 * Getting here means the kernel doesn't know how to handle
3521 * it, but maybe userspace does ... include returned frames
3522 * so userspace can register for those to know whether ones
3523 * it transmitted were processed or returned.
3526 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3527 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3528 sig = status->signal;
3530 if (cfg80211_rx_mgmt_khz(&rx->sdata->wdev,
3531 ieee80211_rx_status_to_khz(status), sig,
3532 rx->skb->data, rx->skb->len, 0)) {
3534 rx->sta->rx_stats.packets++;
3535 dev_kfree_skb(rx->skb);
3542 static ieee80211_rx_result debug_noinline
3543 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3545 struct ieee80211_sub_if_data *sdata = rx->sdata;
3546 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3547 int len = rx->skb->len;
3549 if (!ieee80211_is_action(mgmt->frame_control))
3552 switch (mgmt->u.action.category) {
3553 case WLAN_CATEGORY_SA_QUERY:
3554 if (len < (IEEE80211_MIN_ACTION_SIZE +
3555 sizeof(mgmt->u.action.u.sa_query)))
3558 switch (mgmt->u.action.u.sa_query.action) {
3559 case WLAN_ACTION_SA_QUERY_REQUEST:
3560 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3562 ieee80211_process_sa_query_req(sdata, mgmt, len);
3572 rx->sta->rx_stats.packets++;
3573 dev_kfree_skb(rx->skb);
3577 static ieee80211_rx_result debug_noinline
3578 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3580 struct ieee80211_local *local = rx->local;
3581 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3582 struct sk_buff *nskb;
3583 struct ieee80211_sub_if_data *sdata = rx->sdata;
3584 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3586 if (!ieee80211_is_action(mgmt->frame_control))
3590 * For AP mode, hostapd is responsible for handling any action
3591 * frames that we didn't handle, including returning unknown
3592 * ones. For all other modes we will return them to the sender,
3593 * setting the 0x80 bit in the action category, as required by
3594 * 802.11-2012 9.24.4.
3595 * Newer versions of hostapd shall also use the management frame
3596 * registration mechanisms, but older ones still use cooked
3597 * monitor interfaces so push all frames there.
3599 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3600 (sdata->vif.type == NL80211_IFTYPE_AP ||
3601 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3602 return RX_DROP_MONITOR;
3604 if (is_multicast_ether_addr(mgmt->da))
3605 return RX_DROP_MONITOR;
3607 /* do not return rejected action frames */
3608 if (mgmt->u.action.category & 0x80)
3609 return RX_DROP_UNUSABLE;
3611 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3614 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3616 nmgmt->u.action.category |= 0x80;
3617 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3618 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3620 memset(nskb->cb, 0, sizeof(nskb->cb));
3622 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3623 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3625 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3626 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3627 IEEE80211_TX_CTL_NO_CCK_RATE;
3628 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3630 local->hw.offchannel_tx_hw_queue;
3633 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3636 dev_kfree_skb(rx->skb);
3640 static ieee80211_rx_result debug_noinline
3641 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3643 struct ieee80211_sub_if_data *sdata = rx->sdata;
3644 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3646 if (!ieee80211_is_ext(hdr->frame_control))
3649 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3650 return RX_DROP_MONITOR;
3652 /* for now only beacons are ext, so queue them */
3653 ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
3658 static ieee80211_rx_result debug_noinline
3659 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3661 struct ieee80211_sub_if_data *sdata = rx->sdata;
3662 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3665 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3667 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3668 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3669 sdata->vif.type != NL80211_IFTYPE_OCB &&
3670 sdata->vif.type != NL80211_IFTYPE_STATION)
3671 return RX_DROP_MONITOR;
3674 case cpu_to_le16(IEEE80211_STYPE_AUTH):
3675 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3676 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3677 /* process for all: mesh, mlme, ibss */
3679 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3680 if (is_multicast_ether_addr(mgmt->da) &&
3681 !is_broadcast_ether_addr(mgmt->da))
3682 return RX_DROP_MONITOR;
3684 /* process only for station/IBSS */
3685 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3686 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3687 return RX_DROP_MONITOR;
3689 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3690 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3691 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3692 if (is_multicast_ether_addr(mgmt->da) &&
3693 !is_broadcast_ether_addr(mgmt->da))
3694 return RX_DROP_MONITOR;
3696 /* process only for station */
3697 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3698 return RX_DROP_MONITOR;
3700 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3701 /* process only for ibss and mesh */
3702 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3703 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3704 return RX_DROP_MONITOR;
3707 return RX_DROP_MONITOR;
3710 ieee80211_queue_skb_to_iface(sdata, rx->sta, rx->skb);
3715 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3716 struct ieee80211_rate *rate)
3718 struct ieee80211_sub_if_data *sdata;
3719 struct ieee80211_local *local = rx->local;
3720 struct sk_buff *skb = rx->skb, *skb2;
3721 struct net_device *prev_dev = NULL;
3722 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3723 int needed_headroom;
3726 * If cooked monitor has been processed already, then
3727 * don't do it again. If not, set the flag.
3729 if (rx->flags & IEEE80211_RX_CMNTR)
3731 rx->flags |= IEEE80211_RX_CMNTR;
3733 /* If there are no cooked monitor interfaces, just free the SKB */
3734 if (!local->cooked_mntrs)
3737 /* vendor data is long removed here */
3738 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3739 /* room for the radiotap header based on driver features */
3740 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3742 if (skb_headroom(skb) < needed_headroom &&
3743 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3746 /* prepend radiotap information */
3747 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3750 skb_reset_mac_header(skb);
3751 skb->ip_summed = CHECKSUM_UNNECESSARY;
3752 skb->pkt_type = PACKET_OTHERHOST;
3753 skb->protocol = htons(ETH_P_802_2);
3755 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3756 if (!ieee80211_sdata_running(sdata))
3759 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3760 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3764 skb2 = skb_clone(skb, GFP_ATOMIC);
3766 skb2->dev = prev_dev;
3767 netif_receive_skb(skb2);
3771 prev_dev = sdata->dev;
3772 dev_sw_netstats_rx_add(sdata->dev, skb->len);
3776 skb->dev = prev_dev;
3777 netif_receive_skb(skb);
3785 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3786 ieee80211_rx_result res)
3789 case RX_DROP_MONITOR:
3790 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3792 rx->sta->rx_stats.dropped++;
3795 struct ieee80211_rate *rate = NULL;
3796 struct ieee80211_supported_band *sband;
3797 struct ieee80211_rx_status *status;
3799 status = IEEE80211_SKB_RXCB((rx->skb));
3801 sband = rx->local->hw.wiphy->bands[status->band];
3802 if (status->encoding == RX_ENC_LEGACY)
3803 rate = &sband->bitrates[status->rate_idx];
3805 ieee80211_rx_cooked_monitor(rx, rate);
3808 case RX_DROP_UNUSABLE:
3809 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3811 rx->sta->rx_stats.dropped++;
3812 dev_kfree_skb(rx->skb);
3815 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3820 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3821 struct sk_buff_head *frames)
3823 ieee80211_rx_result res = RX_DROP_MONITOR;
3824 struct sk_buff *skb;
3826 #define CALL_RXH(rxh) \
3829 if (res != RX_CONTINUE) \
3833 /* Lock here to avoid hitting all of the data used in the RX
3834 * path (e.g. key data, station data, ...) concurrently when
3835 * a frame is released from the reorder buffer due to timeout
3836 * from the timer, potentially concurrently with RX from the
3839 spin_lock_bh(&rx->local->rx_path_lock);
3841 while ((skb = __skb_dequeue(frames))) {
3843 * all the other fields are valid across frames
3844 * that belong to an aMPDU since they are on the
3845 * same TID from the same station
3849 CALL_RXH(ieee80211_rx_h_check_more_data);
3850 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3851 CALL_RXH(ieee80211_rx_h_sta_process);
3852 CALL_RXH(ieee80211_rx_h_decrypt);
3853 CALL_RXH(ieee80211_rx_h_defragment);
3854 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3855 /* must be after MMIC verify so header is counted in MPDU mic */
3856 #ifdef CONFIG_MAC80211_MESH
3857 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3858 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3860 CALL_RXH(ieee80211_rx_h_amsdu);
3861 CALL_RXH(ieee80211_rx_h_data);
3863 /* special treatment -- needs the queue */
3864 res = ieee80211_rx_h_ctrl(rx, frames);
3865 if (res != RX_CONTINUE)
3868 CALL_RXH(ieee80211_rx_h_mgmt_check);
3869 CALL_RXH(ieee80211_rx_h_action);
3870 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3871 CALL_RXH(ieee80211_rx_h_action_post_userspace);
3872 CALL_RXH(ieee80211_rx_h_action_return);
3873 CALL_RXH(ieee80211_rx_h_ext);
3874 CALL_RXH(ieee80211_rx_h_mgmt);
3877 ieee80211_rx_handlers_result(rx, res);
3882 spin_unlock_bh(&rx->local->rx_path_lock);
3885 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3887 struct sk_buff_head reorder_release;
3888 ieee80211_rx_result res = RX_DROP_MONITOR;
3890 __skb_queue_head_init(&reorder_release);
3892 #define CALL_RXH(rxh) \
3895 if (res != RX_CONTINUE) \
3899 CALL_RXH(ieee80211_rx_h_check_dup);
3900 CALL_RXH(ieee80211_rx_h_check);
3902 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3904 ieee80211_rx_handlers(rx, &reorder_release);
3908 ieee80211_rx_handlers_result(rx, res);
3914 * This function makes calls into the RX path, therefore
3915 * it has to be invoked under RCU read lock.
3917 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3919 struct sk_buff_head frames;
3920 struct ieee80211_rx_data rx = {
3922 .sdata = sta->sdata,
3923 .local = sta->local,
3924 /* This is OK -- must be QoS data frame */
3925 .security_idx = tid,
3928 struct tid_ampdu_rx *tid_agg_rx;
3930 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3934 __skb_queue_head_init(&frames);
3936 spin_lock(&tid_agg_rx->reorder_lock);
3937 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3938 spin_unlock(&tid_agg_rx->reorder_lock);
3940 if (!skb_queue_empty(&frames)) {
3941 struct ieee80211_event event = {
3942 .type = BA_FRAME_TIMEOUT,
3944 .u.ba.sta = &sta->sta,
3946 drv_event_callback(rx.local, rx.sdata, &event);
3949 ieee80211_rx_handlers(&rx, &frames);
3952 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3953 u16 ssn, u64 filtered,
3956 struct sta_info *sta;
3957 struct tid_ampdu_rx *tid_agg_rx;
3958 struct sk_buff_head frames;
3959 struct ieee80211_rx_data rx = {
3960 /* This is OK -- must be QoS data frame */
3961 .security_idx = tid,
3966 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3969 __skb_queue_head_init(&frames);
3971 sta = container_of(pubsta, struct sta_info, sta);
3974 rx.sdata = sta->sdata;
3975 rx.local = sta->local;
3978 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3982 spin_lock_bh(&tid_agg_rx->reorder_lock);
3984 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3987 /* release all frames in the reorder buffer */
3988 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3989 IEEE80211_SN_MODULO;
3990 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3992 /* update ssn to match received ssn */
3993 tid_agg_rx->head_seq_num = ssn;
3995 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3999 /* handle the case that received ssn is behind the mac ssn.
4000 * it can be tid_agg_rx->buf_size behind and still be valid */
4001 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4002 if (diff >= tid_agg_rx->buf_size) {
4003 tid_agg_rx->reorder_buf_filtered = 0;
4006 filtered = filtered >> diff;
4010 for (i = 0; i < tid_agg_rx->buf_size; i++) {
4011 int index = (ssn + i) % tid_agg_rx->buf_size;
4013 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4014 if (filtered & BIT_ULL(i))
4015 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4018 /* now process also frames that the filter marking released */
4019 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4022 spin_unlock_bh(&tid_agg_rx->reorder_lock);
4024 ieee80211_rx_handlers(&rx, &frames);
4029 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4031 /* main receive path */
4033 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
4035 struct ieee80211_sub_if_data *sdata = rx->sdata;
4036 struct sk_buff *skb = rx->skb;
4037 struct ieee80211_hdr *hdr = (void *)skb->data;
4038 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4039 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
4040 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4041 ieee80211_is_s1g_beacon(hdr->frame_control);
4043 switch (sdata->vif.type) {
4044 case NL80211_IFTYPE_STATION:
4045 if (!bssid && !sdata->u.mgd.use_4addr)
4047 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4051 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4052 case NL80211_IFTYPE_ADHOC:
4055 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
4056 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
4058 if (ieee80211_is_beacon(hdr->frame_control))
4060 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
4063 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4067 if (status->encoding != RX_ENC_LEGACY)
4068 rate_idx = 0; /* TODO: HT/VHT rates */
4070 rate_idx = status->rate_idx;
4071 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4075 case NL80211_IFTYPE_OCB:
4078 if (!ieee80211_is_data_present(hdr->frame_control))
4080 if (!is_broadcast_ether_addr(bssid))
4083 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
4087 if (status->encoding != RX_ENC_LEGACY)
4088 rate_idx = 0; /* TODO: HT rates */
4090 rate_idx = status->rate_idx;
4091 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4095 case NL80211_IFTYPE_MESH_POINT:
4096 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4100 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4101 case NL80211_IFTYPE_AP_VLAN:
4102 case NL80211_IFTYPE_AP:
4104 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4106 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
4108 * Accept public action frames even when the
4109 * BSSID doesn't match, this is used for P2P
4110 * and location updates. Note that mac80211
4111 * itself never looks at these frames.
4114 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4116 if (ieee80211_is_public_action(hdr, skb->len))
4118 return ieee80211_is_beacon(hdr->frame_control);
4121 if (!ieee80211_has_tods(hdr->frame_control)) {
4122 /* ignore data frames to TDLS-peers */
4123 if (ieee80211_is_data(hdr->frame_control))
4125 /* ignore action frames to TDLS-peers */
4126 if (ieee80211_is_action(hdr->frame_control) &&
4127 !is_broadcast_ether_addr(bssid) &&
4128 !ether_addr_equal(bssid, hdr->addr1))
4133 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4134 * the BSSID - we've checked that already but may have accepted
4135 * the wildcard (ff:ff:ff:ff:ff:ff).
4138 * The BSSID of the Data frame is determined as follows:
4139 * a) If the STA is contained within an AP or is associated
4140 * with an AP, the BSSID is the address currently in use
4141 * by the STA contained in the AP.
4143 * So we should not accept data frames with an address that's
4146 * Accepting it also opens a security problem because stations
4147 * could encrypt it with the GTK and inject traffic that way.
4149 if (ieee80211_is_data(hdr->frame_control) && multicast)
4153 case NL80211_IFTYPE_P2P_DEVICE:
4154 return ieee80211_is_public_action(hdr, skb->len) ||
4155 ieee80211_is_probe_req(hdr->frame_control) ||
4156 ieee80211_is_probe_resp(hdr->frame_control) ||
4157 ieee80211_is_beacon(hdr->frame_control);
4158 case NL80211_IFTYPE_NAN:
4159 /* Currently no frames on NAN interface are allowed */
4169 void ieee80211_check_fast_rx(struct sta_info *sta)
4171 struct ieee80211_sub_if_data *sdata = sta->sdata;
4172 struct ieee80211_local *local = sdata->local;
4173 struct ieee80211_key *key;
4174 struct ieee80211_fast_rx fastrx = {
4176 .vif_type = sdata->vif.type,
4177 .control_port_protocol = sdata->control_port_protocol,
4178 }, *old, *new = NULL;
4179 bool set_offload = false;
4180 bool assign = false;
4183 /* use sparse to check that we don't return without updating */
4184 __acquire(check_fast_rx);
4186 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4187 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4188 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4189 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4191 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4193 /* fast-rx doesn't do reordering */
4194 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4195 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4198 switch (sdata->vif.type) {
4199 case NL80211_IFTYPE_STATION:
4200 if (sta->sta.tdls) {
4201 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4202 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4203 fastrx.expected_ds_bits = 0;
4205 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4206 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4207 fastrx.expected_ds_bits =
4208 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4211 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4212 fastrx.expected_ds_bits |=
4213 cpu_to_le16(IEEE80211_FCTL_TODS);
4214 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4215 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4218 if (!sdata->u.mgd.powersave)
4221 /* software powersave is a huge mess, avoid all of it */
4222 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4224 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4225 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4228 case NL80211_IFTYPE_AP_VLAN:
4229 case NL80211_IFTYPE_AP:
4230 /* parallel-rx requires this, at least with calls to
4231 * ieee80211_sta_ps_transition()
4233 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4235 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4236 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4237 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4239 fastrx.internal_forward =
4240 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4241 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4242 !sdata->u.vlan.sta);
4244 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4245 sdata->u.vlan.sta) {
4246 fastrx.expected_ds_bits |=
4247 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4248 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4249 fastrx.internal_forward = 0;
4257 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4261 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4263 key = rcu_dereference(sdata->default_unicast_key);
4265 switch (key->conf.cipher) {
4266 case WLAN_CIPHER_SUITE_TKIP:
4267 /* we don't want to deal with MMIC in fast-rx */
4269 case WLAN_CIPHER_SUITE_CCMP:
4270 case WLAN_CIPHER_SUITE_CCMP_256:
4271 case WLAN_CIPHER_SUITE_GCMP:
4272 case WLAN_CIPHER_SUITE_GCMP_256:
4275 /* We also don't want to deal with
4276 * WEP or cipher scheme.
4282 fastrx.icv_len = key->conf.icv_len;
4289 __release(check_fast_rx);
4292 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4295 (sdata->vif.offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED);
4298 set_offload = !test_and_set_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4300 set_offload = test_and_clear_sta_flag(sta, WLAN_STA_DECAP_OFFLOAD);
4303 drv_sta_set_decap_offload(local, sdata, &sta->sta, assign);
4305 spin_lock_bh(&sta->lock);
4306 old = rcu_dereference_protected(sta->fast_rx, true);
4307 rcu_assign_pointer(sta->fast_rx, new);
4308 spin_unlock_bh(&sta->lock);
4311 kfree_rcu(old, rcu_head);
4314 void ieee80211_clear_fast_rx(struct sta_info *sta)
4316 struct ieee80211_fast_rx *old;
4318 spin_lock_bh(&sta->lock);
4319 old = rcu_dereference_protected(sta->fast_rx, true);
4320 RCU_INIT_POINTER(sta->fast_rx, NULL);
4321 spin_unlock_bh(&sta->lock);
4324 kfree_rcu(old, rcu_head);
4327 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4329 struct ieee80211_local *local = sdata->local;
4330 struct sta_info *sta;
4332 lockdep_assert_held(&local->sta_mtx);
4334 list_for_each_entry(sta, &local->sta_list, list) {
4335 if (sdata != sta->sdata &&
4336 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4338 ieee80211_check_fast_rx(sta);
4342 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4344 struct ieee80211_local *local = sdata->local;
4346 mutex_lock(&local->sta_mtx);
4347 __ieee80211_check_fast_rx_iface(sdata);
4348 mutex_unlock(&local->sta_mtx);
4351 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
4352 struct ieee80211_fast_rx *fast_rx,
4355 struct ieee80211_sta_rx_stats *stats;
4356 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
4357 struct sta_info *sta = rx->sta;
4358 struct sk_buff *skb = rx->skb;
4359 void *sa = skb->data + ETH_ALEN;
4360 void *da = skb->data;
4362 stats = &sta->rx_stats;
4363 if (fast_rx->uses_rss)
4364 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4366 /* statistics part of ieee80211_rx_h_sta_process() */
4367 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4368 stats->last_signal = status->signal;
4369 if (!fast_rx->uses_rss)
4370 ewma_signal_add(&sta->rx_stats_avg.signal,
4374 if (status->chains) {
4377 stats->chains = status->chains;
4378 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4379 int signal = status->chain_signal[i];
4381 if (!(status->chains & BIT(i)))
4384 stats->chain_signal_last[i] = signal;
4385 if (!fast_rx->uses_rss)
4386 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4390 /* end of statistics */
4392 stats->last_rx = jiffies;
4393 stats->last_rate = sta_stats_encode_rate(status);
4398 skb->dev = fast_rx->dev;
4400 dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
4402 /* The seqno index has the same property as needed
4403 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4404 * for non-QoS-data frames. Here we know it's a data
4405 * frame, so count MSDUs.
4407 u64_stats_update_begin(&stats->syncp);
4408 stats->msdu[rx->seqno_idx]++;
4409 stats->bytes += orig_len;
4410 u64_stats_update_end(&stats->syncp);
4412 if (fast_rx->internal_forward) {
4413 struct sk_buff *xmit_skb = NULL;
4414 if (is_multicast_ether_addr(da)) {
4415 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4416 } else if (!ether_addr_equal(da, sa) &&
4417 sta_info_get(rx->sdata, da)) {
4424 * Send to wireless media and increase priority by 256
4425 * to keep the received priority instead of
4426 * reclassifying the frame (see cfg80211_classify8021d).
4428 xmit_skb->priority += 256;
4429 xmit_skb->protocol = htons(ETH_P_802_3);
4430 skb_reset_network_header(xmit_skb);
4431 skb_reset_mac_header(xmit_skb);
4432 dev_queue_xmit(xmit_skb);
4439 /* deliver to local stack */
4440 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4441 memset(skb->cb, 0, sizeof(skb->cb));
4443 list_add_tail(&skb->list, rx->list);
4445 netif_receive_skb(skb);
4449 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4450 struct ieee80211_fast_rx *fast_rx)
4452 struct sk_buff *skb = rx->skb;
4453 struct ieee80211_hdr *hdr = (void *)skb->data;
4454 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4455 struct sta_info *sta = rx->sta;
4456 int orig_len = skb->len;
4457 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4458 int snap_offs = hdrlen;
4460 u8 snap[sizeof(rfc1042_header)];
4462 } *payload __aligned(2);
4466 } addrs __aligned(2);
4467 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4469 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4470 * to a common data structure; drivers can implement that per queue
4471 * but we don't have that information in mac80211
4473 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4476 #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4478 /* If using encryption, we also need to have:
4479 * - PN_VALIDATED: similar, but the implementation is tricky
4480 * - DECRYPTED: necessary for PN_VALIDATED
4483 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4486 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4489 if (unlikely(ieee80211_is_frag(hdr)))
4492 /* Since our interface address cannot be multicast, this
4493 * implicitly also rejects multicast frames without the
4496 * We shouldn't get any *data* frames not addressed to us
4497 * (AP mode will accept multicast *management* frames), but
4498 * punting here will make it go through the full checks in
4499 * ieee80211_accept_frame().
4501 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4504 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4505 IEEE80211_FCTL_TODS)) !=
4506 fast_rx->expected_ds_bits)
4509 /* assign the key to drop unencrypted frames (later)
4510 * and strip the IV/MIC if necessary
4512 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4513 /* GCMP header length is the same */
4514 snap_offs += IEEE80211_CCMP_HDR_LEN;
4517 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4518 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4521 payload = (void *)(skb->data + snap_offs);
4523 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4526 /* Don't handle these here since they require special code.
4527 * Accept AARP and IPX even though they should come with a
4528 * bridge-tunnel header - but if we get them this way then
4529 * there's little point in discarding them.
4531 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4532 payload->proto == fast_rx->control_port_protocol))
4536 /* after this point, don't punt to the slowpath! */
4538 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4539 pskb_trim(skb, skb->len - fast_rx->icv_len))
4542 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4545 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4546 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4553 /* do the header conversion - first grab the addresses */
4554 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4555 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4556 /* remove the SNAP but leave the ethertype */
4557 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4558 /* push the addresses in front */
4559 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4561 ieee80211_rx_8023(rx, fast_rx, orig_len);
4566 if (fast_rx->uses_rss)
4567 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4574 * This function returns whether or not the SKB
4575 * was destined for RX processing or not, which,
4576 * if consume is true, is equivalent to whether
4577 * or not the skb was consumed.
4579 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4580 struct sk_buff *skb, bool consume)
4582 struct ieee80211_local *local = rx->local;
4583 struct ieee80211_sub_if_data *sdata = rx->sdata;
4587 /* See if we can do fast-rx; if we have to copy we already lost,
4588 * so punt in that case. We should never have to deliver a data
4589 * frame to multiple interfaces anyway.
4591 * We skip the ieee80211_accept_frame() call and do the necessary
4592 * checking inside ieee80211_invoke_fast_rx().
4594 if (consume && rx->sta) {
4595 struct ieee80211_fast_rx *fast_rx;
4597 fast_rx = rcu_dereference(rx->sta->fast_rx);
4598 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4602 if (!ieee80211_accept_frame(rx))
4606 skb = skb_copy(skb, GFP_ATOMIC);
4608 if (net_ratelimit())
4609 wiphy_debug(local->hw.wiphy,
4610 "failed to copy skb for %s\n",
4618 ieee80211_invoke_rx_handlers(rx);
4622 static void __ieee80211_rx_handle_8023(struct ieee80211_hw *hw,
4623 struct ieee80211_sta *pubsta,
4624 struct sk_buff *skb,
4625 struct list_head *list)
4627 struct ieee80211_local *local = hw_to_local(hw);
4628 struct ieee80211_fast_rx *fast_rx;
4629 struct ieee80211_rx_data rx;
4631 memset(&rx, 0, sizeof(rx));
4636 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4638 /* drop frame if too short for header */
4639 if (skb->len < sizeof(struct ethhdr))
4645 rx.sta = container_of(pubsta, struct sta_info, sta);
4646 rx.sdata = rx.sta->sdata;
4648 fast_rx = rcu_dereference(rx.sta->fast_rx);
4652 ieee80211_rx_8023(&rx, fast_rx, skb->len);
4660 * This is the actual Rx frames handler. as it belongs to Rx path it must
4661 * be called with rcu_read_lock protection.
4663 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
4664 struct ieee80211_sta *pubsta,
4665 struct sk_buff *skb,
4666 struct list_head *list)
4668 struct ieee80211_local *local = hw_to_local(hw);
4669 struct ieee80211_sub_if_data *sdata;
4670 struct ieee80211_hdr *hdr;
4672 struct ieee80211_rx_data rx;
4673 struct ieee80211_sub_if_data *prev;
4674 struct rhlist_head *tmp;
4677 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
4678 memset(&rx, 0, sizeof(rx));
4683 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
4684 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4686 if (ieee80211_is_mgmt(fc)) {
4687 /* drop frame if too short for header */
4688 if (skb->len < ieee80211_hdrlen(fc))
4691 err = skb_linearize(skb);
4693 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4701 hdr = (struct ieee80211_hdr *)skb->data;
4702 ieee80211_parse_qos(&rx);
4703 ieee80211_verify_alignment(&rx);
4705 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4706 ieee80211_is_beacon(hdr->frame_control) ||
4707 ieee80211_is_s1g_beacon(hdr->frame_control)))
4708 ieee80211_scan_rx(local, skb);
4710 if (ieee80211_is_data(fc)) {
4711 struct sta_info *sta, *prev_sta;
4714 rx.sta = container_of(pubsta, struct sta_info, sta);
4715 rx.sdata = rx.sta->sdata;
4716 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4723 for_each_sta_info(local, hdr->addr2, sta, tmp) {
4730 rx.sdata = prev_sta->sdata;
4731 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4738 rx.sdata = prev_sta->sdata;
4740 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4748 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4749 if (!ieee80211_sdata_running(sdata))
4752 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4753 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4757 * frame is destined for this interface, but if it's
4758 * not also for the previous one we handle that after
4759 * the loop to avoid copying the SKB once too much
4767 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4769 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4775 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4778 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4787 * This is the receive path handler. It is called by a low level driver when an
4788 * 802.11 MPDU is received from the hardware.
4790 void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4791 struct sk_buff *skb, struct list_head *list)
4793 struct ieee80211_local *local = hw_to_local(hw);
4794 struct ieee80211_rate *rate = NULL;
4795 struct ieee80211_supported_band *sband;
4796 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4798 WARN_ON_ONCE(softirq_count() == 0);
4800 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4803 sband = local->hw.wiphy->bands[status->band];
4804 if (WARN_ON(!sband))
4808 * If we're suspending, it is possible although not too likely
4809 * that we'd be receiving frames after having already partially
4810 * quiesced the stack. We can't process such frames then since
4811 * that might, for example, cause stations to be added or other
4812 * driver callbacks be invoked.
4814 if (unlikely(local->quiescing || local->suspended))
4817 /* We might be during a HW reconfig, prevent Rx for the same reason */
4818 if (unlikely(local->in_reconfig))
4822 * The same happens when we're not even started,
4823 * but that's worth a warning.
4825 if (WARN_ON(!local->started))
4828 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4830 * Validate the rate, unless a PLCP error means that
4831 * we probably can't have a valid rate here anyway.
4834 switch (status->encoding) {
4837 * rate_idx is MCS index, which can be [0-76]
4840 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
4842 * Anything else would be some sort of driver or
4843 * hardware error. The driver should catch hardware
4846 if (WARN(status->rate_idx > 76,
4847 "Rate marked as an HT rate but passed "
4848 "status->rate_idx is not "
4849 "an MCS index [0-76]: %d (0x%02x)\n",
4855 if (WARN_ONCE(status->rate_idx > 9 ||
4858 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4859 status->rate_idx, status->nss))
4863 if (WARN_ONCE(status->rate_idx > 11 ||
4866 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4867 status->rate_idx, status->nss))
4874 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4876 rate = &sband->bitrates[status->rate_idx];
4880 status->rx_flags = 0;
4882 kcov_remote_start_common(skb_get_kcov_handle(skb));
4885 * Frames with failed FCS/PLCP checksum are not returned,
4886 * all other frames are returned without radiotap header
4887 * if it was previously present.
4888 * Also, frames with less than 16 bytes are dropped.
4890 if (!(status->flag & RX_FLAG_8023))
4891 skb = ieee80211_rx_monitor(local, skb, rate);
4893 ieee80211_tpt_led_trig_rx(local,
4894 ((struct ieee80211_hdr *)skb->data)->frame_control,
4897 if (status->flag & RX_FLAG_8023)
4898 __ieee80211_rx_handle_8023(hw, pubsta, skb, list);
4900 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
4908 EXPORT_SYMBOL(ieee80211_rx_list);
4910 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4911 struct sk_buff *skb, struct napi_struct *napi)
4913 struct sk_buff *tmp;
4918 * key references and virtual interfaces are protected using RCU
4919 * and this requires that we are in a read-side RCU section during
4920 * receive processing
4923 ieee80211_rx_list(hw, pubsta, skb, &list);
4927 netif_receive_skb_list(&list);
4931 list_for_each_entry_safe(skb, tmp, &list, list) {
4932 skb_list_del_init(skb);
4933 napi_gro_receive(napi, skb);
4936 EXPORT_SYMBOL(ieee80211_rx_napi);
4938 /* This is a version of the rx handler that can be called from hard irq
4939 * context. Post the skb on the queue and schedule the tasklet */
4940 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4942 struct ieee80211_local *local = hw_to_local(hw);
4944 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4946 skb->pkt_type = IEEE80211_RX_MSG;
4947 skb_queue_tail(&local->skb_queue, skb);
4948 tasklet_schedule(&local->tasklet);
4950 EXPORT_SYMBOL(ieee80211_rx_irqsafe);