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"
36 static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
38 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
40 u64_stats_update_begin(&tstats->syncp);
42 tstats->rx_bytes += len;
43 u64_stats_update_end(&tstats->syncp);
47 * monitor mode reception
49 * This function cleans up the SKB, i.e. it removes all the stuff
50 * only useful for monitoring.
52 static struct sk_buff *ieee80211_clean_skb(struct sk_buff *skb,
53 unsigned int present_fcs_len,
54 unsigned int rtap_space)
56 struct ieee80211_hdr *hdr;
61 __pskb_trim(skb, skb->len - present_fcs_len);
62 __pskb_pull(skb, rtap_space);
64 hdr = (void *)skb->data;
65 fc = hdr->frame_control;
68 * Remove the HT-Control field (if present) on management
69 * frames after we've sent the frame to monitoring. We
70 * (currently) don't need it, and don't properly parse
71 * frames with it present, due to the assumption of a
72 * fixed management header length.
74 if (likely(!ieee80211_is_mgmt(fc) || !ieee80211_has_order(fc)))
77 hdrlen = ieee80211_hdrlen(fc);
78 hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
80 if (!pskb_may_pull(skb, hdrlen)) {
85 memmove(skb->data + IEEE80211_HT_CTL_LEN, skb->data,
86 hdrlen - IEEE80211_HT_CTL_LEN);
87 __pskb_pull(skb, IEEE80211_HT_CTL_LEN);
92 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
93 unsigned int rtap_space)
95 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
96 struct ieee80211_hdr *hdr;
98 hdr = (void *)(skb->data + rtap_space);
100 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
101 RX_FLAG_FAILED_PLCP_CRC |
102 RX_FLAG_ONLY_MONITOR |
106 if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
109 if (ieee80211_is_ctl(hdr->frame_control) &&
110 !ieee80211_is_pspoll(hdr->frame_control) &&
111 !ieee80211_is_back_req(hdr->frame_control))
118 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
119 struct ieee80211_rx_status *status,
124 /* always present fields */
125 len = sizeof(struct ieee80211_radiotap_header) + 8;
127 /* allocate extra bitmaps */
129 len += 4 * hweight8(status->chains);
130 /* vendor presence bitmap */
131 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
134 if (ieee80211_have_rx_timestamp(status)) {
138 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
141 /* antenna field, if we don't have per-chain info */
145 /* padding for RX_FLAGS if necessary */
148 if (status->encoding == RX_ENC_HT) /* HT info */
151 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
156 if (status->encoding == RX_ENC_VHT) {
161 if (local->hw.radiotap_timestamp.units_pos >= 0) {
166 if (status->encoding == RX_ENC_HE &&
167 status->flag & RX_FLAG_RADIOTAP_HE) {
170 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
173 if (status->encoding == RX_ENC_HE &&
174 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
177 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
180 if (status->flag & RX_FLAG_NO_PSDU)
183 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
186 BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) != 4);
189 if (status->chains) {
190 /* antenna and antenna signal fields */
191 len += 2 * hweight8(status->chains);
194 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
195 struct ieee80211_vendor_radiotap *rtap;
196 int vendor_data_offset = 0;
199 * The position to look at depends on the existence (or non-
200 * existence) of other elements, so take that into account...
202 if (status->flag & RX_FLAG_RADIOTAP_HE)
203 vendor_data_offset +=
204 sizeof(struct ieee80211_radiotap_he);
205 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
206 vendor_data_offset +=
207 sizeof(struct ieee80211_radiotap_he_mu);
208 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
209 vendor_data_offset +=
210 sizeof(struct ieee80211_radiotap_lsig);
212 rtap = (void *)&skb->data[vendor_data_offset];
214 /* alignment for fixed 6-byte vendor data header */
216 /* vendor data header */
218 if (WARN_ON(rtap->align == 0))
220 len = ALIGN(len, rtap->align);
221 len += rtap->len + rtap->pad;
227 static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
232 struct ieee80211_hdr_3addr hdr;
235 } __packed __aligned(2) action;
240 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
242 if (skb->len < rtap_space + sizeof(action) +
243 VHT_MUMIMO_GROUPS_DATA_LEN)
246 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
249 skb_copy_bits(skb, rtap_space, &action, sizeof(action));
251 if (!ieee80211_is_action(action.hdr.frame_control))
254 if (action.category != WLAN_CATEGORY_VHT)
257 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
260 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
263 skb = skb_copy(skb, GFP_ATOMIC);
267 skb_queue_tail(&sdata->skb_queue, skb);
268 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
272 * ieee80211_add_rx_radiotap_header - add radiotap header
274 * add a radiotap header containing all the fields which the hardware provided.
277 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
279 struct ieee80211_rate *rate,
280 int rtap_len, bool has_fcs)
282 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
283 struct ieee80211_radiotap_header *rthdr;
288 u16 channel_flags = 0;
290 unsigned long chains = status->chains;
291 struct ieee80211_vendor_radiotap rtap = {};
292 struct ieee80211_radiotap_he he = {};
293 struct ieee80211_radiotap_he_mu he_mu = {};
294 struct ieee80211_radiotap_lsig lsig = {};
296 if (status->flag & RX_FLAG_RADIOTAP_HE) {
297 he = *(struct ieee80211_radiotap_he *)skb->data;
298 skb_pull(skb, sizeof(he));
299 WARN_ON_ONCE(status->encoding != RX_ENC_HE);
302 if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
303 he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
304 skb_pull(skb, sizeof(he_mu));
307 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
308 lsig = *(struct ieee80211_radiotap_lsig *)skb->data;
309 skb_pull(skb, sizeof(lsig));
312 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
313 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
314 /* rtap.len and rtap.pad are undone immediately */
315 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
319 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
322 rthdr = skb_push(skb, rtap_len);
323 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
324 it_present = &rthdr->it_present;
326 /* radiotap header, set always present flags */
327 rthdr->it_len = cpu_to_le16(rtap_len);
328 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
329 BIT(IEEE80211_RADIOTAP_CHANNEL) |
330 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
333 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
335 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
337 BIT(IEEE80211_RADIOTAP_EXT) |
338 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
339 put_unaligned_le32(it_present_val, it_present);
341 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
342 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
345 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
346 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
347 BIT(IEEE80211_RADIOTAP_EXT);
348 put_unaligned_le32(it_present_val, it_present);
350 it_present_val = rtap.present;
353 put_unaligned_le32(it_present_val, it_present);
355 pos = (void *)(it_present + 1);
357 /* the order of the following fields is important */
359 /* IEEE80211_RADIOTAP_TSFT */
360 if (ieee80211_have_rx_timestamp(status)) {
362 while ((pos - (u8 *)rthdr) & 7)
365 ieee80211_calculate_rx_timestamp(local, status,
368 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
372 /* IEEE80211_RADIOTAP_FLAGS */
373 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
374 *pos |= IEEE80211_RADIOTAP_F_FCS;
375 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
376 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
377 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
378 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
381 /* IEEE80211_RADIOTAP_RATE */
382 if (!rate || status->encoding != RX_ENC_LEGACY) {
384 * Without rate information don't add it. If we have,
385 * MCS information is a separate field in radiotap,
386 * added below. The byte here is needed as padding
387 * for the channel though, so initialise it to 0.
392 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
393 if (status->bw == RATE_INFO_BW_10)
395 else if (status->bw == RATE_INFO_BW_5)
397 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
401 /* IEEE80211_RADIOTAP_CHANNEL */
402 /* TODO: frequency offset in KHz */
403 put_unaligned_le16(status->freq, pos);
405 if (status->bw == RATE_INFO_BW_10)
406 channel_flags |= IEEE80211_CHAN_HALF;
407 else if (status->bw == RATE_INFO_BW_5)
408 channel_flags |= IEEE80211_CHAN_QUARTER;
410 if (status->band == NL80211_BAND_5GHZ ||
411 status->band == NL80211_BAND_6GHZ)
412 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
413 else if (status->encoding != RX_ENC_LEGACY)
414 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
415 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
416 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
418 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
420 channel_flags |= IEEE80211_CHAN_2GHZ;
421 put_unaligned_le16(channel_flags, pos);
424 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
425 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
426 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
427 *pos = status->signal;
429 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
433 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
435 if (!status->chains) {
436 /* IEEE80211_RADIOTAP_ANTENNA */
437 *pos = status->antenna;
441 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
443 /* IEEE80211_RADIOTAP_RX_FLAGS */
444 /* ensure 2 byte alignment for the 2 byte field as required */
445 if ((pos - (u8 *)rthdr) & 1)
447 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
448 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
449 put_unaligned_le16(rx_flags, pos);
452 if (status->encoding == RX_ENC_HT) {
455 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
456 *pos++ = local->hw.radiotap_mcs_details;
458 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
459 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
460 if (status->bw == RATE_INFO_BW_40)
461 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
462 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
463 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
464 if (status->enc_flags & RX_ENC_FLAG_LDPC)
465 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
466 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
467 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
469 *pos++ = status->rate_idx;
472 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
475 /* ensure 4 byte alignment */
476 while ((pos - (u8 *)rthdr) & 3)
479 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
480 put_unaligned_le32(status->ampdu_reference, pos);
482 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
483 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
484 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
485 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
486 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
487 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
488 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
489 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
490 if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
491 flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
492 if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
493 flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
494 put_unaligned_le16(flags, pos);
496 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
497 *pos++ = status->ampdu_delimiter_crc;
503 if (status->encoding == RX_ENC_VHT) {
504 u16 known = local->hw.radiotap_vht_details;
506 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
507 put_unaligned_le16(known, pos);
510 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
511 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
512 /* in VHT, STBC is binary */
513 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
514 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
515 if (status->enc_flags & RX_ENC_FLAG_BF)
516 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
519 switch (status->bw) {
520 case RATE_INFO_BW_80:
523 case RATE_INFO_BW_160:
526 case RATE_INFO_BW_40:
533 *pos = (status->rate_idx << 4) | status->nss;
536 if (status->enc_flags & RX_ENC_FLAG_LDPC)
537 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
545 if (local->hw.radiotap_timestamp.units_pos >= 0) {
547 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
550 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
552 /* ensure 8 byte alignment */
553 while ((pos - (u8 *)rthdr) & 7)
556 put_unaligned_le64(status->device_timestamp, pos);
559 if (local->hw.radiotap_timestamp.accuracy >= 0) {
560 accuracy = local->hw.radiotap_timestamp.accuracy;
561 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
563 put_unaligned_le16(accuracy, pos);
566 *pos++ = local->hw.radiotap_timestamp.units_pos;
570 if (status->encoding == RX_ENC_HE &&
571 status->flag & RX_FLAG_RADIOTAP_HE) {
572 #define HE_PREP(f, val) le16_encode_bits(val, IEEE80211_RADIOTAP_HE_##f)
574 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
575 he.data6 |= HE_PREP(DATA6_NSTS,
576 FIELD_GET(RX_ENC_FLAG_STBC_MASK,
578 he.data3 |= HE_PREP(DATA3_STBC, 1);
580 he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
583 #define CHECK_GI(s) \
584 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
585 (int)NL80211_RATE_INFO_HE_GI_##s)
591 he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
592 he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
593 he.data3 |= HE_PREP(DATA3_CODING,
594 !!(status->enc_flags & RX_ENC_FLAG_LDPC));
596 he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
598 switch (status->bw) {
599 case RATE_INFO_BW_20:
600 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
601 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
603 case RATE_INFO_BW_40:
604 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
605 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
607 case RATE_INFO_BW_80:
608 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
609 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
611 case RATE_INFO_BW_160:
612 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
613 IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
615 case RATE_INFO_BW_HE_RU:
616 #define CHECK_RU_ALLOC(s) \
617 BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
618 NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
626 CHECK_RU_ALLOC(2x996);
628 he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
632 WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
635 /* ensure 2 byte alignment */
636 while ((pos - (u8 *)rthdr) & 1)
638 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
639 memcpy(pos, &he, sizeof(he));
643 if (status->encoding == RX_ENC_HE &&
644 status->flag & RX_FLAG_RADIOTAP_HE_MU) {
645 /* ensure 2 byte alignment */
646 while ((pos - (u8 *)rthdr) & 1)
648 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
649 memcpy(pos, &he_mu, sizeof(he_mu));
650 pos += sizeof(he_mu);
653 if (status->flag & RX_FLAG_NO_PSDU) {
655 cpu_to_le32(1 << IEEE80211_RADIOTAP_ZERO_LEN_PSDU);
656 *pos++ = status->zero_length_psdu_type;
659 if (status->flag & RX_FLAG_RADIOTAP_LSIG) {
660 /* ensure 2 byte alignment */
661 while ((pos - (u8 *)rthdr) & 1)
663 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_LSIG);
664 memcpy(pos, &lsig, sizeof(lsig));
668 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
669 *pos++ = status->chain_signal[chain];
673 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
674 /* ensure 2 byte alignment for the vendor field as required */
675 if ((pos - (u8 *)rthdr) & 1)
677 *pos++ = rtap.oui[0];
678 *pos++ = rtap.oui[1];
679 *pos++ = rtap.oui[2];
681 put_unaligned_le16(rtap.len, pos);
683 /* align the actual payload as requested */
684 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
686 /* data (and possible padding) already follows */
690 static struct sk_buff *
691 ieee80211_make_monitor_skb(struct ieee80211_local *local,
692 struct sk_buff **origskb,
693 struct ieee80211_rate *rate,
694 int rtap_space, bool use_origskb)
696 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
697 int rt_hdrlen, needed_headroom;
700 /* room for the radiotap header based on driver features */
701 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
702 needed_headroom = rt_hdrlen - rtap_space;
705 /* only need to expand headroom if necessary */
710 * This shouldn't trigger often because most devices have an
711 * RX header they pull before we get here, and that should
712 * be big enough for our radiotap information. We should
713 * probably export the length to drivers so that we can have
714 * them allocate enough headroom to start with.
716 if (skb_headroom(skb) < needed_headroom &&
717 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
723 * Need to make a copy and possibly remove radiotap header
724 * and FCS from the original.
726 skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
732 /* prepend radiotap information */
733 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
735 skb_reset_mac_header(skb);
736 skb->ip_summed = CHECKSUM_UNNECESSARY;
737 skb->pkt_type = PACKET_OTHERHOST;
738 skb->protocol = htons(ETH_P_802_2);
744 * This function copies a received frame to all monitor interfaces and
745 * returns a cleaned-up SKB that no longer includes the FCS nor the
746 * radiotap header the driver might have added.
748 static struct sk_buff *
749 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
750 struct ieee80211_rate *rate)
752 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
753 struct ieee80211_sub_if_data *sdata;
754 struct sk_buff *monskb = NULL;
755 int present_fcs_len = 0;
756 unsigned int rtap_space = 0;
757 struct ieee80211_sub_if_data *monitor_sdata =
758 rcu_dereference(local->monitor_sdata);
759 bool only_monitor = false;
760 unsigned int min_head_len;
762 if (status->flag & RX_FLAG_RADIOTAP_HE)
763 rtap_space += sizeof(struct ieee80211_radiotap_he);
765 if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
766 rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
768 if (status->flag & RX_FLAG_RADIOTAP_LSIG)
769 rtap_space += sizeof(struct ieee80211_radiotap_lsig);
771 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
772 struct ieee80211_vendor_radiotap *rtap =
773 (void *)(origskb->data + rtap_space);
775 rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
778 min_head_len = rtap_space;
781 * First, we may need to make a copy of the skb because
782 * (1) we need to modify it for radiotap (if not present), and
783 * (2) the other RX handlers will modify the skb we got.
785 * We don't need to, of course, if we aren't going to return
786 * the SKB because it has a bad FCS/PLCP checksum.
789 if (!(status->flag & RX_FLAG_NO_PSDU)) {
790 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
791 if (unlikely(origskb->len <= FCS_LEN + rtap_space)) {
794 dev_kfree_skb(origskb);
797 present_fcs_len = FCS_LEN;
800 /* also consider the hdr->frame_control */
804 /* ensure that the expected data elements are in skb head */
805 if (!pskb_may_pull(origskb, min_head_len)) {
806 dev_kfree_skb(origskb);
810 only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
812 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
814 dev_kfree_skb(origskb);
818 return ieee80211_clean_skb(origskb, present_fcs_len,
822 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
824 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
825 bool last_monitor = list_is_last(&sdata->u.mntr.list,
829 monskb = ieee80211_make_monitor_skb(local, &origskb,
841 skb = skb_clone(monskb, GFP_ATOMIC);
845 skb->dev = sdata->dev;
846 ieee80211_rx_stats(skb->dev, skb->len);
847 netif_receive_skb(skb);
855 /* this happens if last_monitor was erroneously false */
856 dev_kfree_skb(monskb);
862 return ieee80211_clean_skb(origskb, present_fcs_len, rtap_space);
865 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
867 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
868 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
869 int tid, seqno_idx, security_idx;
871 /* does the frame have a qos control field? */
872 if (ieee80211_is_data_qos(hdr->frame_control)) {
873 u8 *qc = ieee80211_get_qos_ctl(hdr);
874 /* frame has qos control */
875 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
876 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
877 status->rx_flags |= IEEE80211_RX_AMSDU;
883 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
885 * Sequence numbers for management frames, QoS data
886 * frames with a broadcast/multicast address in the
887 * Address 1 field, and all non-QoS data frames sent
888 * by QoS STAs are assigned using an additional single
889 * modulo-4096 counter, [...]
891 * We also use that counter for non-QoS STAs.
893 seqno_idx = IEEE80211_NUM_TIDS;
895 if (ieee80211_is_mgmt(hdr->frame_control))
896 security_idx = IEEE80211_NUM_TIDS;
900 rx->seqno_idx = seqno_idx;
901 rx->security_idx = security_idx;
902 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
903 * For now, set skb->priority to 0 for other cases. */
904 rx->skb->priority = (tid > 7) ? 0 : tid;
908 * DOC: Packet alignment
910 * Drivers always need to pass packets that are aligned to two-byte boundaries
913 * Additionally, should, if possible, align the payload data in a way that
914 * guarantees that the contained IP header is aligned to a four-byte
915 * boundary. In the case of regular frames, this simply means aligning the
916 * payload to a four-byte boundary (because either the IP header is directly
917 * contained, or IV/RFC1042 headers that have a length divisible by four are
918 * in front of it). If the payload data is not properly aligned and the
919 * architecture doesn't support efficient unaligned operations, mac80211
920 * will align the data.
922 * With A-MSDU frames, however, the payload data address must yield two modulo
923 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
924 * push the IP header further back to a multiple of four again. Thankfully, the
925 * specs were sane enough this time around to require padding each A-MSDU
926 * subframe to a length that is a multiple of four.
928 * Padding like Atheros hardware adds which is between the 802.11 header and
929 * the payload is not supported, the driver is required to move the 802.11
930 * header to be directly in front of the payload in that case.
932 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
934 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
935 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
942 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
944 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
946 if (is_multicast_ether_addr(hdr->addr1))
949 return ieee80211_is_robust_mgmt_frame(skb);
953 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
955 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
957 if (!is_multicast_ether_addr(hdr->addr1))
960 return ieee80211_is_robust_mgmt_frame(skb);
964 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
965 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
967 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
968 struct ieee80211_mmie *mmie;
969 struct ieee80211_mmie_16 *mmie16;
971 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
974 if (!ieee80211_is_robust_mgmt_frame(skb) &&
975 !ieee80211_is_beacon(hdr->frame_control))
976 return -1; /* not a robust management frame */
978 mmie = (struct ieee80211_mmie *)
979 (skb->data + skb->len - sizeof(*mmie));
980 if (mmie->element_id == WLAN_EID_MMIE &&
981 mmie->length == sizeof(*mmie) - 2)
982 return le16_to_cpu(mmie->key_id);
984 mmie16 = (struct ieee80211_mmie_16 *)
985 (skb->data + skb->len - sizeof(*mmie16));
986 if (skb->len >= 24 + sizeof(*mmie16) &&
987 mmie16->element_id == WLAN_EID_MMIE &&
988 mmie16->length == sizeof(*mmie16) - 2)
989 return le16_to_cpu(mmie16->key_id);
994 static int ieee80211_get_keyid(struct sk_buff *skb,
995 const struct ieee80211_cipher_scheme *cs)
997 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1005 fc = hdr->frame_control;
1006 hdrlen = ieee80211_hdrlen(fc);
1009 minlen = hdrlen + cs->hdr_len;
1010 key_idx_off = hdrlen + cs->key_idx_off;
1011 key_idx_shift = cs->key_idx_shift;
1013 /* WEP, TKIP, CCMP and GCMP */
1014 minlen = hdrlen + IEEE80211_WEP_IV_LEN;
1015 key_idx_off = hdrlen + 3;
1019 if (unlikely(skb->len < minlen))
1022 skb_copy_bits(skb, key_idx_off, &keyid, 1);
1025 keyid &= cs->key_idx_mask;
1026 keyid >>= key_idx_shift;
1028 /* cs could use more than the usual two bits for the keyid */
1029 if (unlikely(keyid >= NUM_DEFAULT_KEYS))
1035 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
1037 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1038 char *dev_addr = rx->sdata->vif.addr;
1040 if (ieee80211_is_data(hdr->frame_control)) {
1041 if (is_multicast_ether_addr(hdr->addr1)) {
1042 if (ieee80211_has_tods(hdr->frame_control) ||
1043 !ieee80211_has_fromds(hdr->frame_control))
1044 return RX_DROP_MONITOR;
1045 if (ether_addr_equal(hdr->addr3, dev_addr))
1046 return RX_DROP_MONITOR;
1048 if (!ieee80211_has_a4(hdr->frame_control))
1049 return RX_DROP_MONITOR;
1050 if (ether_addr_equal(hdr->addr4, dev_addr))
1051 return RX_DROP_MONITOR;
1055 /* If there is not an established peer link and this is not a peer link
1056 * establisment frame, beacon or probe, drop the frame.
1059 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
1060 struct ieee80211_mgmt *mgmt;
1062 if (!ieee80211_is_mgmt(hdr->frame_control))
1063 return RX_DROP_MONITOR;
1065 if (ieee80211_is_action(hdr->frame_control)) {
1068 /* make sure category field is present */
1069 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
1070 return RX_DROP_MONITOR;
1072 mgmt = (struct ieee80211_mgmt *)hdr;
1073 category = mgmt->u.action.category;
1074 if (category != WLAN_CATEGORY_MESH_ACTION &&
1075 category != WLAN_CATEGORY_SELF_PROTECTED)
1076 return RX_DROP_MONITOR;
1080 if (ieee80211_is_probe_req(hdr->frame_control) ||
1081 ieee80211_is_probe_resp(hdr->frame_control) ||
1082 ieee80211_is_beacon(hdr->frame_control) ||
1083 ieee80211_is_auth(hdr->frame_control))
1086 return RX_DROP_MONITOR;
1092 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
1095 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
1096 struct sk_buff *tail = skb_peek_tail(frames);
1097 struct ieee80211_rx_status *status;
1099 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
1105 status = IEEE80211_SKB_RXCB(tail);
1106 if (status->flag & RX_FLAG_AMSDU_MORE)
1112 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1113 struct tid_ampdu_rx *tid_agg_rx,
1115 struct sk_buff_head *frames)
1117 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
1118 struct sk_buff *skb;
1119 struct ieee80211_rx_status *status;
1121 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1123 if (skb_queue_empty(skb_list))
1126 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1127 __skb_queue_purge(skb_list);
1131 /* release frames from the reorder ring buffer */
1132 tid_agg_rx->stored_mpdu_num--;
1133 while ((skb = __skb_dequeue(skb_list))) {
1134 status = IEEE80211_SKB_RXCB(skb);
1135 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
1136 __skb_queue_tail(frames, skb);
1140 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
1141 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1144 static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
1145 struct tid_ampdu_rx *tid_agg_rx,
1147 struct sk_buff_head *frames)
1151 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1153 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
1154 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1155 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1161 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
1162 * the skb was added to the buffer longer than this time ago, the earlier
1163 * frames that have not yet been received are assumed to be lost and the skb
1164 * can be released for processing. This may also release other skb's from the
1165 * reorder buffer if there are no additional gaps between the frames.
1167 * Callers must hold tid_agg_rx->reorder_lock.
1169 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
1171 static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
1172 struct tid_ampdu_rx *tid_agg_rx,
1173 struct sk_buff_head *frames)
1177 lockdep_assert_held(&tid_agg_rx->reorder_lock);
1179 /* release the buffer until next missing frame */
1180 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1181 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
1182 tid_agg_rx->stored_mpdu_num) {
1184 * No buffers ready to be released, but check whether any
1185 * frames in the reorder buffer have timed out.
1188 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1189 j = (j + 1) % tid_agg_rx->buf_size) {
1190 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
1195 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
1196 HT_RX_REORDER_BUF_TIMEOUT))
1197 goto set_release_timer;
1199 /* don't leave incomplete A-MSDUs around */
1200 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1201 i = (i + 1) % tid_agg_rx->buf_size)
1202 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1204 ht_dbg_ratelimited(sdata,
1205 "release an RX reorder frame due to timeout on earlier frames\n");
1206 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1210 * Increment the head seq# also for the skipped slots.
1212 tid_agg_rx->head_seq_num =
1213 (tid_agg_rx->head_seq_num +
1214 skipped) & IEEE80211_SN_MASK;
1217 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1218 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1220 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1223 if (tid_agg_rx->stored_mpdu_num) {
1224 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1226 for (; j != (index - 1) % tid_agg_rx->buf_size;
1227 j = (j + 1) % tid_agg_rx->buf_size) {
1228 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
1234 if (!tid_agg_rx->removed)
1235 mod_timer(&tid_agg_rx->reorder_timer,
1236 tid_agg_rx->reorder_time[j] + 1 +
1237 HT_RX_REORDER_BUF_TIMEOUT);
1239 del_timer(&tid_agg_rx->reorder_timer);
1244 * As this function belongs to the RX path it must be under
1245 * rcu_read_lock protection. It returns false if the frame
1246 * can be processed immediately, true if it was consumed.
1248 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1249 struct tid_ampdu_rx *tid_agg_rx,
1250 struct sk_buff *skb,
1251 struct sk_buff_head *frames)
1253 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1254 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1255 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1256 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1257 u16 head_seq_num, buf_size;
1261 spin_lock(&tid_agg_rx->reorder_lock);
1264 * Offloaded BA sessions have no known starting sequence number so pick
1265 * one from first Rxed frame for this tid after BA was started.
1267 if (unlikely(tid_agg_rx->auto_seq)) {
1268 tid_agg_rx->auto_seq = false;
1269 tid_agg_rx->ssn = mpdu_seq_num;
1270 tid_agg_rx->head_seq_num = mpdu_seq_num;
1273 buf_size = tid_agg_rx->buf_size;
1274 head_seq_num = tid_agg_rx->head_seq_num;
1277 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1280 if (unlikely(!tid_agg_rx->started)) {
1281 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1285 tid_agg_rx->started = true;
1288 /* frame with out of date sequence number */
1289 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1295 * If frame the sequence number exceeds our buffering window
1296 * size release some previous frames to make room for this one.
1298 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1299 head_seq_num = ieee80211_sn_inc(
1300 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1301 /* release stored frames up to new head to stack */
1302 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
1303 head_seq_num, frames);
1306 /* Now the new frame is always in the range of the reordering buffer */
1308 index = mpdu_seq_num % tid_agg_rx->buf_size;
1310 /* check if we already stored this frame */
1311 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1317 * If the current MPDU is in the right order and nothing else
1318 * is stored we can process it directly, no need to buffer it.
1319 * If it is first but there's something stored, we may be able
1320 * to release frames after this one.
1322 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1323 tid_agg_rx->stored_mpdu_num == 0) {
1324 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1325 tid_agg_rx->head_seq_num =
1326 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1331 /* put the frame in the reordering buffer */
1332 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1333 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1334 tid_agg_rx->reorder_time[index] = jiffies;
1335 tid_agg_rx->stored_mpdu_num++;
1336 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1340 spin_unlock(&tid_agg_rx->reorder_lock);
1345 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1346 * true if the MPDU was buffered, false if it should be processed.
1348 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1349 struct sk_buff_head *frames)
1351 struct sk_buff *skb = rx->skb;
1352 struct ieee80211_local *local = rx->local;
1353 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1354 struct sta_info *sta = rx->sta;
1355 struct tid_ampdu_rx *tid_agg_rx;
1359 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1360 is_multicast_ether_addr(hdr->addr1))
1364 * filter the QoS data rx stream according to
1365 * STA/TID and check if this STA/TID is on aggregation
1371 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1372 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1373 tid = ieee80211_get_tid(hdr);
1375 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1377 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1378 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1379 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1380 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1381 WLAN_BACK_RECIPIENT,
1382 WLAN_REASON_QSTA_REQUIRE_SETUP);
1386 /* qos null data frames are excluded */
1387 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1390 /* not part of a BA session */
1391 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
1394 /* new, potentially un-ordered, ampdu frame - process it */
1396 /* reset session timer */
1397 if (tid_agg_rx->timeout)
1398 tid_agg_rx->last_rx = jiffies;
1400 /* if this mpdu is fragmented - terminate rx aggregation session */
1401 sc = le16_to_cpu(hdr->seq_ctrl);
1402 if (sc & IEEE80211_SCTL_FRAG) {
1403 skb_queue_tail(&rx->sdata->skb_queue, skb);
1404 ieee80211_queue_work(&local->hw, &rx->sdata->work);
1409 * No locking needed -- we will only ever process one
1410 * RX packet at a time, and thus own tid_agg_rx. All
1411 * other code manipulating it needs to (and does) make
1412 * sure that we cannot get to it any more before doing
1415 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1420 __skb_queue_tail(frames, skb);
1423 static ieee80211_rx_result debug_noinline
1424 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1426 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1427 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1429 if (status->flag & RX_FLAG_DUP_VALIDATED)
1433 * Drop duplicate 802.11 retransmissions
1434 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1437 if (rx->skb->len < 24)
1440 if (ieee80211_is_ctl(hdr->frame_control) ||
1441 ieee80211_is_any_nullfunc(hdr->frame_control) ||
1442 is_multicast_ether_addr(hdr->addr1))
1448 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1449 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1450 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1451 rx->sta->rx_stats.num_duplicates++;
1452 return RX_DROP_UNUSABLE;
1453 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1454 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1460 static ieee80211_rx_result debug_noinline
1461 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1463 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1465 /* Drop disallowed frame classes based on STA auth/assoc state;
1466 * IEEE 802.11, Chap 5.5.
1468 * mac80211 filters only based on association state, i.e. it drops
1469 * Class 3 frames from not associated stations. hostapd sends
1470 * deauth/disassoc frames when needed. In addition, hostapd is
1471 * responsible for filtering on both auth and assoc states.
1474 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1475 return ieee80211_rx_mesh_check(rx);
1477 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1478 ieee80211_is_pspoll(hdr->frame_control)) &&
1479 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1480 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
1481 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1482 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1484 * accept port control frames from the AP even when it's not
1485 * yet marked ASSOC to prevent a race where we don't set the
1486 * assoc bit quickly enough before it sends the first frame
1488 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1489 ieee80211_is_data_present(hdr->frame_control)) {
1490 unsigned int hdrlen;
1493 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1495 if (rx->skb->len < hdrlen + 8)
1496 return RX_DROP_MONITOR;
1498 skb_copy_bits(rx->skb, hdrlen + 6, ðertype, 2);
1499 if (ethertype == rx->sdata->control_port_protocol)
1503 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1504 cfg80211_rx_spurious_frame(rx->sdata->dev,
1507 return RX_DROP_UNUSABLE;
1509 return RX_DROP_MONITOR;
1516 static ieee80211_rx_result debug_noinline
1517 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1519 struct ieee80211_local *local;
1520 struct ieee80211_hdr *hdr;
1521 struct sk_buff *skb;
1525 hdr = (struct ieee80211_hdr *) skb->data;
1527 if (!local->pspolling)
1530 if (!ieee80211_has_fromds(hdr->frame_control))
1531 /* this is not from AP */
1534 if (!ieee80211_is_data(hdr->frame_control))
1537 if (!ieee80211_has_moredata(hdr->frame_control)) {
1538 /* AP has no more frames buffered for us */
1539 local->pspolling = false;
1543 /* more data bit is set, let's request a new frame from the AP */
1544 ieee80211_send_pspoll(local, rx->sdata);
1549 static void sta_ps_start(struct sta_info *sta)
1551 struct ieee80211_sub_if_data *sdata = sta->sdata;
1552 struct ieee80211_local *local = sdata->local;
1556 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1557 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1558 ps = &sdata->bss->ps;
1562 atomic_inc(&ps->num_sta_ps);
1563 set_sta_flag(sta, WLAN_STA_PS_STA);
1564 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1565 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1566 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1567 sta->sta.addr, sta->sta.aid);
1569 ieee80211_clear_fast_xmit(sta);
1571 if (!sta->sta.txq[0])
1574 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
1575 struct ieee80211_txq *txq = sta->sta.txq[tid];
1576 struct txq_info *txqi = to_txq_info(txq);
1578 spin_lock(&local->active_txq_lock[txq->ac]);
1579 if (!list_empty(&txqi->schedule_order))
1580 list_del_init(&txqi->schedule_order);
1581 spin_unlock(&local->active_txq_lock[txq->ac]);
1583 if (txq_has_queue(txq))
1584 set_bit(tid, &sta->txq_buffered_tids);
1586 clear_bit(tid, &sta->txq_buffered_tids);
1590 static void sta_ps_end(struct sta_info *sta)
1592 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1593 sta->sta.addr, sta->sta.aid);
1595 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1597 * Clear the flag only if the other one is still set
1598 * so that the TX path won't start TX'ing new frames
1599 * directly ... In the case that the driver flag isn't
1600 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1602 clear_sta_flag(sta, WLAN_STA_PS_STA);
1603 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1604 sta->sta.addr, sta->sta.aid);
1608 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1609 clear_sta_flag(sta, WLAN_STA_PS_STA);
1610 ieee80211_sta_ps_deliver_wakeup(sta);
1613 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1615 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1618 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1620 /* Don't let the same PS state be set twice */
1621 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1622 if ((start && in_ps) || (!start && !in_ps))
1632 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1634 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1636 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1638 if (test_sta_flag(sta, WLAN_STA_SP))
1641 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1642 ieee80211_sta_ps_deliver_poll_response(sta);
1644 set_sta_flag(sta, WLAN_STA_PSPOLL);
1646 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1648 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1650 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1651 int ac = ieee80211_ac_from_tid(tid);
1654 * If this AC is not trigger-enabled do nothing unless the
1655 * driver is calling us after it already checked.
1657 * NB: This could/should check a separate bitmap of trigger-
1658 * enabled queues, but for now we only implement uAPSD w/o
1659 * TSPEC changes to the ACs, so they're always the same.
1661 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1662 tid != IEEE80211_NUM_TIDS)
1665 /* if we are in a service period, do nothing */
1666 if (test_sta_flag(sta, WLAN_STA_SP))
1669 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1670 ieee80211_sta_ps_deliver_uapsd(sta);
1672 set_sta_flag(sta, WLAN_STA_UAPSD);
1674 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1676 static ieee80211_rx_result debug_noinline
1677 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1679 struct ieee80211_sub_if_data *sdata = rx->sdata;
1680 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1681 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1686 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1687 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1691 * The device handles station powersave, so don't do anything about
1692 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1693 * it to mac80211 since they're handled.)
1695 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1699 * Don't do anything if the station isn't already asleep. In
1700 * the uAPSD case, the station will probably be marked asleep,
1701 * in the PS-Poll case the station must be confused ...
1703 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1706 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1707 ieee80211_sta_pspoll(&rx->sta->sta);
1709 /* Free PS Poll skb here instead of returning RX_DROP that would
1710 * count as an dropped frame. */
1711 dev_kfree_skb(rx->skb);
1714 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1715 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1716 ieee80211_has_pm(hdr->frame_control) &&
1717 (ieee80211_is_data_qos(hdr->frame_control) ||
1718 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1719 u8 tid = ieee80211_get_tid(hdr);
1721 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1727 static ieee80211_rx_result debug_noinline
1728 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1730 struct sta_info *sta = rx->sta;
1731 struct sk_buff *skb = rx->skb;
1732 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1733 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1740 * Update last_rx only for IBSS packets which are for the current
1741 * BSSID and for station already AUTHORIZED to avoid keeping the
1742 * current IBSS network alive in cases where other STAs start
1743 * using different BSSID. This will also give the station another
1744 * chance to restart the authentication/authorization in case
1745 * something went wrong the first time.
1747 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1748 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1749 NL80211_IFTYPE_ADHOC);
1750 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1751 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1752 sta->rx_stats.last_rx = jiffies;
1753 if (ieee80211_is_data(hdr->frame_control) &&
1754 !is_multicast_ether_addr(hdr->addr1))
1755 sta->rx_stats.last_rate =
1756 sta_stats_encode_rate(status);
1758 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1759 sta->rx_stats.last_rx = jiffies;
1760 } else if (!ieee80211_is_s1g_beacon(hdr->frame_control) &&
1761 !is_multicast_ether_addr(hdr->addr1)) {
1763 * Mesh beacons will update last_rx when if they are found to
1764 * match the current local configuration when processed.
1766 sta->rx_stats.last_rx = jiffies;
1767 if (ieee80211_is_data(hdr->frame_control))
1768 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1771 sta->rx_stats.fragments++;
1773 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1774 sta->rx_stats.bytes += rx->skb->len;
1775 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1777 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1778 sta->rx_stats.last_signal = status->signal;
1779 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1782 if (status->chains) {
1783 sta->rx_stats.chains = status->chains;
1784 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1785 int signal = status->chain_signal[i];
1787 if (!(status->chains & BIT(i)))
1790 sta->rx_stats.chain_signal_last[i] = signal;
1791 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1796 if (ieee80211_is_s1g_beacon(hdr->frame_control))
1800 * Change STA power saving mode only at the end of a frame
1801 * exchange sequence, and only for a data or management
1802 * frame as specified in IEEE 802.11-2016 11.2.3.2
1804 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1805 !ieee80211_has_morefrags(hdr->frame_control) &&
1806 !is_multicast_ether_addr(hdr->addr1) &&
1807 (ieee80211_is_mgmt(hdr->frame_control) ||
1808 ieee80211_is_data(hdr->frame_control)) &&
1809 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1810 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1811 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
1812 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1813 if (!ieee80211_has_pm(hdr->frame_control))
1816 if (ieee80211_has_pm(hdr->frame_control))
1821 /* mesh power save support */
1822 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1823 ieee80211_mps_rx_h_sta_process(sta, hdr);
1826 * Drop (qos-)data::nullfunc frames silently, since they
1827 * are used only to control station power saving mode.
1829 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1830 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1833 * If we receive a 4-addr nullfunc frame from a STA
1834 * that was not moved to a 4-addr STA vlan yet send
1835 * the event to userspace and for older hostapd drop
1836 * the frame to the monitor interface.
1838 if (ieee80211_has_a4(hdr->frame_control) &&
1839 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1840 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1841 !rx->sdata->u.vlan.sta))) {
1842 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1843 cfg80211_rx_unexpected_4addr_frame(
1844 rx->sdata->dev, sta->sta.addr,
1846 return RX_DROP_MONITOR;
1849 * Update counter and free packet here to avoid
1850 * counting this as a dropped packed.
1852 sta->rx_stats.packets++;
1853 dev_kfree_skb(rx->skb);
1858 } /* ieee80211_rx_h_sta_process */
1860 static struct ieee80211_key *
1861 ieee80211_rx_get_bigtk(struct ieee80211_rx_data *rx, int idx)
1863 struct ieee80211_key *key = NULL;
1864 struct ieee80211_sub_if_data *sdata = rx->sdata;
1867 /* Make sure key gets set if either BIGTK key index is set so that
1868 * ieee80211_drop_unencrypted_mgmt() can properly drop both unprotected
1869 * Beacon frames and Beacon frames that claim to use another BIGTK key
1870 * index (i.e., a key that we do not have).
1874 idx = NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1877 if (idx == NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1884 key = rcu_dereference(rx->sta->gtk[idx]);
1886 key = rcu_dereference(sdata->keys[idx]);
1887 if (!key && rx->sta)
1888 key = rcu_dereference(rx->sta->gtk[idx2]);
1890 key = rcu_dereference(sdata->keys[idx2]);
1895 static ieee80211_rx_result debug_noinline
1896 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1898 struct sk_buff *skb = rx->skb;
1899 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1900 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1902 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1903 struct ieee80211_key *sta_ptk = NULL;
1904 struct ieee80211_key *ptk_idx = NULL;
1905 int mmie_keyidx = -1;
1907 const struct ieee80211_cipher_scheme *cs = NULL;
1909 if (ieee80211_is_ext(hdr->frame_control))
1915 * There are five types of keys:
1916 * - GTK (group keys)
1917 * - IGTK (group keys for management frames)
1918 * - BIGTK (group keys for Beacon frames)
1919 * - PTK (pairwise keys)
1920 * - STK (station-to-station pairwise keys)
1922 * When selecting a key, we have to distinguish between multicast
1923 * (including broadcast) and unicast frames, the latter can only
1924 * use PTKs and STKs while the former always use GTKs, IGTKs, and
1925 * BIGTKs. Unless, of course, actual WEP keys ("pre-RSNA") are used,
1926 * then unicast frames can also use key indices like GTKs. Hence, if we
1927 * don't have a PTK/STK we check the key index for a WEP key.
1929 * Note that in a regular BSS, multicast frames are sent by the
1930 * AP only, associated stations unicast the frame to the AP first
1931 * which then multicasts it on their behalf.
1933 * There is also a slight problem in IBSS mode: GTKs are negotiated
1934 * with each station, that is something we don't currently handle.
1935 * The spec seems to expect that one negotiates the same key with
1936 * every station but there's no such requirement; VLANs could be
1940 /* start without a key */
1942 fc = hdr->frame_control;
1945 int keyid = rx->sta->ptk_idx;
1946 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1948 if (ieee80211_has_protected(fc) &&
1949 !(status->flag & RX_FLAG_IV_STRIPPED)) {
1950 cs = rx->sta->cipher_scheme;
1951 keyid = ieee80211_get_keyid(rx->skb, cs);
1953 if (unlikely(keyid < 0))
1954 return RX_DROP_UNUSABLE;
1956 ptk_idx = rcu_dereference(rx->sta->ptk[keyid]);
1960 if (!ieee80211_has_protected(fc))
1961 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1963 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1964 rx->key = ptk_idx ? ptk_idx : sta_ptk;
1965 if ((status->flag & RX_FLAG_DECRYPTED) &&
1966 (status->flag & RX_FLAG_IV_STRIPPED))
1968 /* Skip decryption if the frame is not protected. */
1969 if (!ieee80211_has_protected(fc))
1971 } else if (mmie_keyidx >= 0 && ieee80211_is_beacon(fc)) {
1972 /* Broadcast/multicast robust management frame / BIP */
1973 if ((status->flag & RX_FLAG_DECRYPTED) &&
1974 (status->flag & RX_FLAG_IV_STRIPPED))
1977 if (mmie_keyidx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS ||
1978 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS +
1979 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 &&
2133 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2134 skb->data, skb->len);
2139 void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
2143 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2144 skb_queue_head_init(&cache->entries[i].skb_list);
2147 void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
2151 for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
2152 __skb_queue_purge(&cache->entries[i].skb_list);
2155 static inline struct ieee80211_fragment_entry *
2156 ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
2157 unsigned int frag, unsigned int seq, int rx_queue,
2158 struct sk_buff **skb)
2160 struct ieee80211_fragment_entry *entry;
2162 entry = &cache->entries[cache->next++];
2163 if (cache->next >= IEEE80211_FRAGMENT_MAX)
2166 __skb_queue_purge(&entry->skb_list);
2168 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
2170 entry->first_frag_time = jiffies;
2172 entry->rx_queue = rx_queue;
2173 entry->last_frag = frag;
2174 entry->check_sequential_pn = false;
2175 entry->extra_len = 0;
2180 static inline struct ieee80211_fragment_entry *
2181 ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
2182 unsigned int frag, unsigned int seq,
2183 int rx_queue, struct ieee80211_hdr *hdr)
2185 struct ieee80211_fragment_entry *entry;
2189 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
2190 struct ieee80211_hdr *f_hdr;
2191 struct sk_buff *f_skb;
2195 idx = IEEE80211_FRAGMENT_MAX - 1;
2197 entry = &cache->entries[idx];
2198 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
2199 entry->rx_queue != rx_queue ||
2200 entry->last_frag + 1 != frag)
2203 f_skb = __skb_peek(&entry->skb_list);
2204 f_hdr = (struct ieee80211_hdr *) f_skb->data;
2207 * Check ftype and addresses are equal, else check next fragment
2209 if (((hdr->frame_control ^ f_hdr->frame_control) &
2210 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
2211 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
2212 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
2215 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
2216 __skb_queue_purge(&entry->skb_list);
2225 static bool requires_sequential_pn(struct ieee80211_rx_data *rx, __le16 fc)
2228 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2229 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2230 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2231 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2232 ieee80211_has_protected(fc);
2235 static ieee80211_rx_result debug_noinline
2236 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
2238 struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
2239 struct ieee80211_hdr *hdr;
2242 unsigned int frag, seq;
2243 struct ieee80211_fragment_entry *entry;
2244 struct sk_buff *skb;
2245 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2247 hdr = (struct ieee80211_hdr *)rx->skb->data;
2248 fc = hdr->frame_control;
2250 if (ieee80211_is_ctl(fc) || ieee80211_is_ext(fc))
2253 sc = le16_to_cpu(hdr->seq_ctrl);
2254 frag = sc & IEEE80211_SCTL_FRAG;
2257 cache = &rx->sta->frags;
2259 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
2262 if (is_multicast_ether_addr(hdr->addr1))
2263 return RX_DROP_MONITOR;
2265 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
2267 if (skb_linearize(rx->skb))
2268 return RX_DROP_UNUSABLE;
2271 * skb_linearize() might change the skb->data and
2272 * previously cached variables (in this case, hdr) need to
2273 * be refreshed with the new data.
2275 hdr = (struct ieee80211_hdr *)rx->skb->data;
2276 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2279 /* This is the first fragment of a new frame. */
2280 entry = ieee80211_reassemble_add(cache, frag, seq,
2281 rx->seqno_idx, &(rx->skb));
2282 if (requires_sequential_pn(rx, fc)) {
2283 int queue = rx->security_idx;
2285 /* Store CCMP/GCMP PN so that we can verify that the
2286 * next fragment has a sequential PN value.
2288 entry->check_sequential_pn = true;
2289 entry->is_protected = true;
2290 entry->key_color = rx->key->color;
2291 memcpy(entry->last_pn,
2292 rx->key->u.ccmp.rx_pn[queue],
2293 IEEE80211_CCMP_PN_LEN);
2294 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2296 offsetof(struct ieee80211_key,
2298 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2299 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2300 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2301 IEEE80211_GCMP_PN_LEN);
2302 } else if (rx->key &&
2303 (ieee80211_has_protected(fc) ||
2304 (status->flag & RX_FLAG_DECRYPTED))) {
2305 entry->is_protected = true;
2306 entry->key_color = rx->key->color;
2311 /* This is a fragment for a frame that should already be pending in
2312 * fragment cache. Add this fragment to the end of the pending entry.
2314 entry = ieee80211_reassemble_find(cache, frag, seq,
2315 rx->seqno_idx, hdr);
2317 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2318 return RX_DROP_MONITOR;
2321 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2322 * MPDU PN values are not incrementing in steps of 1."
2323 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2324 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2326 if (entry->check_sequential_pn) {
2328 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2330 if (!requires_sequential_pn(rx, fc))
2331 return RX_DROP_UNUSABLE;
2333 /* Prevent mixed key and fragment cache attacks */
2334 if (entry->key_color != rx->key->color)
2335 return RX_DROP_UNUSABLE;
2337 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2338 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2344 rpn = rx->ccm_gcm.pn;
2345 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2346 return RX_DROP_UNUSABLE;
2347 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2348 } else if (entry->is_protected &&
2350 (!ieee80211_has_protected(fc) &&
2351 !(status->flag & RX_FLAG_DECRYPTED)) ||
2352 rx->key->color != entry->key_color)) {
2353 /* Drop this as a mixed key or fragment cache attack, even
2354 * if for TKIP Michael MIC should protect us, and WEP is a
2355 * lost cause anyway.
2357 return RX_DROP_UNUSABLE;
2358 } else if (entry->is_protected && rx->key &&
2359 entry->key_color != rx->key->color &&
2360 (status->flag & RX_FLAG_DECRYPTED)) {
2361 return RX_DROP_UNUSABLE;
2364 skb_pull(rx->skb, ieee80211_hdrlen(fc));
2365 __skb_queue_tail(&entry->skb_list, rx->skb);
2366 entry->last_frag = frag;
2367 entry->extra_len += rx->skb->len;
2368 if (ieee80211_has_morefrags(fc)) {
2373 rx->skb = __skb_dequeue(&entry->skb_list);
2374 if (skb_tailroom(rx->skb) < entry->extra_len) {
2375 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2376 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2378 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2379 __skb_queue_purge(&entry->skb_list);
2380 return RX_DROP_UNUSABLE;
2383 while ((skb = __skb_dequeue(&entry->skb_list))) {
2384 skb_put_data(rx->skb, skb->data, skb->len);
2389 ieee80211_led_rx(rx->local);
2391 rx->sta->rx_stats.packets++;
2395 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2397 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2403 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2405 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
2406 struct sk_buff *skb = rx->skb;
2407 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2410 * Pass through unencrypted frames if the hardware has
2411 * decrypted them already.
2413 if (status->flag & RX_FLAG_DECRYPTED)
2416 /* check mesh EAPOL frames first */
2417 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2418 ieee80211_is_data(fc))) {
2419 struct ieee80211s_hdr *mesh_hdr;
2420 u16 hdr_len = ieee80211_hdrlen(fc);
2421 u16 ethertype_offset;
2424 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2427 /* make sure fixed part of mesh header is there, also checks skb len */
2428 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2431 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2432 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2433 sizeof(rfc1042_header);
2435 if (skb_copy_bits(rx->skb, ethertype_offset, ðertype, 2) == 0 &&
2436 ethertype == rx->sdata->control_port_protocol)
2441 /* Drop unencrypted frames if key is set. */
2442 if (unlikely(!ieee80211_has_protected(fc) &&
2443 !ieee80211_is_any_nullfunc(fc) &&
2444 ieee80211_is_data(fc) && rx->key))
2450 static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2452 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2453 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2454 __le16 fc = hdr->frame_control;
2457 * Pass through unencrypted frames if the hardware has
2458 * decrypted them already.
2460 if (status->flag & RX_FLAG_DECRYPTED)
2463 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2464 if (unlikely(!ieee80211_has_protected(fc) &&
2465 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2467 if (ieee80211_is_deauth(fc) ||
2468 ieee80211_is_disassoc(fc))
2469 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2474 /* BIP does not use Protected field, so need to check MMIE */
2475 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2476 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2477 if (ieee80211_is_deauth(fc) ||
2478 ieee80211_is_disassoc(fc))
2479 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2484 if (unlikely(ieee80211_is_beacon(fc) && rx->key &&
2485 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2486 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2492 * When using MFP, Action frames are not allowed prior to
2493 * having configured keys.
2495 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2496 ieee80211_is_robust_mgmt_frame(rx->skb)))
2504 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2506 struct ieee80211_sub_if_data *sdata = rx->sdata;
2507 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2508 bool check_port_control = false;
2509 struct ethhdr *ehdr;
2512 *port_control = false;
2513 if (ieee80211_has_a4(hdr->frame_control) &&
2514 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2517 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2518 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2520 if (!sdata->u.mgd.use_4addr)
2522 else if (!ether_addr_equal(hdr->addr1, sdata->vif.addr))
2523 check_port_control = true;
2526 if (is_multicast_ether_addr(hdr->addr1) &&
2527 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2530 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2534 ehdr = (struct ethhdr *) rx->skb->data;
2535 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2536 *port_control = true;
2537 else if (check_port_control)
2544 * requires that rx->skb is a frame with ethernet header
2546 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2548 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2549 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2550 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2553 * Allow EAPOL frames to us/the PAE group address regardless of
2554 * whether the frame was encrypted or not, and always disallow
2555 * all other destination addresses for them.
2557 if (unlikely(ehdr->h_proto == rx->sdata->control_port_protocol))
2558 return ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2559 ether_addr_equal(ehdr->h_dest, pae_group_addr);
2561 if (ieee80211_802_1x_port_control(rx) ||
2562 ieee80211_drop_unencrypted(rx, fc))
2568 static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
2569 struct ieee80211_rx_data *rx)
2571 struct ieee80211_sub_if_data *sdata = rx->sdata;
2572 struct net_device *dev = sdata->dev;
2574 if (unlikely((skb->protocol == sdata->control_port_protocol ||
2575 (skb->protocol == cpu_to_be16(ETH_P_PREAUTH) &&
2576 !sdata->control_port_no_preauth)) &&
2577 sdata->control_port_over_nl80211)) {
2578 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2579 bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
2581 cfg80211_rx_control_port(dev, skb, noencrypt);
2584 struct ethhdr *ehdr = (void *)skb_mac_header(skb);
2586 memset(skb->cb, 0, sizeof(skb->cb));
2589 * 802.1X over 802.11 requires that the authenticator address
2590 * be used for EAPOL frames. However, 802.1X allows the use of
2591 * the PAE group address instead. If the interface is part of
2592 * a bridge and we pass the frame with the PAE group address,
2593 * then the bridge will forward it to the network (even if the
2594 * client was not associated yet), which isn't supposed to
2596 * To avoid that, rewrite the destination address to our own
2597 * address, so that the authenticator (e.g. hostapd) will see
2598 * the frame, but bridge won't forward it anywhere else. Note
2599 * that due to earlier filtering, the only other address can
2600 * be the PAE group address.
2602 if (unlikely(skb->protocol == sdata->control_port_protocol &&
2603 !ether_addr_equal(ehdr->h_dest, sdata->vif.addr)))
2604 ether_addr_copy(ehdr->h_dest, sdata->vif.addr);
2606 /* deliver to local stack */
2608 list_add_tail(&skb->list, rx->list);
2610 netif_receive_skb(skb);
2615 * requires that rx->skb is a frame with ethernet header
2618 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2620 struct ieee80211_sub_if_data *sdata = rx->sdata;
2621 struct net_device *dev = sdata->dev;
2622 struct sk_buff *skb, *xmit_skb;
2623 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2624 struct sta_info *dsta;
2629 ieee80211_rx_stats(dev, skb->len);
2632 /* The seqno index has the same property as needed
2633 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2634 * for non-QoS-data frames. Here we know it's a data
2635 * frame, so count MSDUs.
2637 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2638 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2639 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2642 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2643 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2644 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2645 ehdr->h_proto != rx->sdata->control_port_protocol &&
2646 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2647 if (is_multicast_ether_addr(ehdr->h_dest) &&
2648 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2650 * send multicast frames both to higher layers in
2651 * local net stack and back to the wireless medium
2653 xmit_skb = skb_copy(skb, GFP_ATOMIC);
2655 net_info_ratelimited("%s: failed to clone multicast frame\n",
2657 } else if (!is_multicast_ether_addr(ehdr->h_dest) &&
2658 !ether_addr_equal(ehdr->h_dest, ehdr->h_source)) {
2659 dsta = sta_info_get(sdata, ehdr->h_dest);
2662 * The destination station is associated to
2663 * this AP (in this VLAN), so send the frame
2664 * directly to it and do not pass it to local
2673 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2675 /* 'align' will only take the values 0 or 2 here since all
2676 * frames are required to be aligned to 2-byte boundaries
2677 * when being passed to mac80211; the code here works just
2678 * as well if that isn't true, but mac80211 assumes it can
2679 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2683 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2685 if (WARN_ON(skb_headroom(skb) < 3)) {
2689 u8 *data = skb->data;
2690 size_t len = skb_headlen(skb);
2692 memmove(skb->data, data, len);
2693 skb_set_tail_pointer(skb, len);
2700 skb->protocol = eth_type_trans(skb, dev);
2701 ieee80211_deliver_skb_to_local_stack(skb, rx);
2706 * Send to wireless media and increase priority by 256 to
2707 * keep the received priority instead of reclassifying
2708 * the frame (see cfg80211_classify8021d).
2710 xmit_skb->priority += 256;
2711 xmit_skb->protocol = htons(ETH_P_802_3);
2712 skb_reset_network_header(xmit_skb);
2713 skb_reset_mac_header(xmit_skb);
2714 dev_queue_xmit(xmit_skb);
2718 static ieee80211_rx_result debug_noinline
2719 __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
2721 struct net_device *dev = rx->sdata->dev;
2722 struct sk_buff *skb = rx->skb;
2723 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2724 __le16 fc = hdr->frame_control;
2725 struct sk_buff_head frame_list;
2726 struct ethhdr ethhdr;
2727 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
2729 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2732 } else switch (rx->sdata->vif.type) {
2733 case NL80211_IFTYPE_AP:
2734 case NL80211_IFTYPE_AP_VLAN:
2737 case NL80211_IFTYPE_STATION:
2739 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2742 case NL80211_IFTYPE_MESH_POINT:
2750 __skb_queue_head_init(&frame_list);
2752 if (ieee80211_data_to_8023_exthdr(skb, ðhdr,
2753 rx->sdata->vif.addr,
2754 rx->sdata->vif.type,
2756 return RX_DROP_UNUSABLE;
2758 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2759 rx->sdata->vif.type,
2760 rx->local->hw.extra_tx_headroom,
2761 check_da, check_sa);
2763 while (!skb_queue_empty(&frame_list)) {
2764 rx->skb = __skb_dequeue(&frame_list);
2766 if (!ieee80211_frame_allowed(rx, fc)) {
2767 dev_kfree_skb(rx->skb);
2771 ieee80211_deliver_skb(rx);
2777 static ieee80211_rx_result debug_noinline
2778 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2780 struct sk_buff *skb = rx->skb;
2781 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2782 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2783 __le16 fc = hdr->frame_control;
2785 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2788 if (unlikely(!ieee80211_is_data(fc)))
2791 if (unlikely(!ieee80211_is_data_present(fc)))
2792 return RX_DROP_MONITOR;
2794 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2795 switch (rx->sdata->vif.type) {
2796 case NL80211_IFTYPE_AP_VLAN:
2797 if (!rx->sdata->u.vlan.sta)
2798 return RX_DROP_UNUSABLE;
2800 case NL80211_IFTYPE_STATION:
2801 if (!rx->sdata->u.mgd.use_4addr)
2802 return RX_DROP_UNUSABLE;
2805 return RX_DROP_UNUSABLE;
2809 if (is_multicast_ether_addr(hdr->addr1))
2810 return RX_DROP_UNUSABLE;
2814 * We should not receive A-MSDUs on pre-HT connections,
2815 * and HT connections cannot use old ciphers. Thus drop
2816 * them, as in those cases we couldn't even have SPP
2819 switch (rx->key->conf.cipher) {
2820 case WLAN_CIPHER_SUITE_WEP40:
2821 case WLAN_CIPHER_SUITE_WEP104:
2822 case WLAN_CIPHER_SUITE_TKIP:
2823 return RX_DROP_UNUSABLE;
2829 return __ieee80211_rx_h_amsdu(rx, 0);
2832 #ifdef CONFIG_MAC80211_MESH
2833 static ieee80211_rx_result
2834 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2836 struct ieee80211_hdr *fwd_hdr, *hdr;
2837 struct ieee80211_tx_info *info;
2838 struct ieee80211s_hdr *mesh_hdr;
2839 struct sk_buff *skb = rx->skb, *fwd_skb;
2840 struct ieee80211_local *local = rx->local;
2841 struct ieee80211_sub_if_data *sdata = rx->sdata;
2842 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2846 hdr = (struct ieee80211_hdr *) skb->data;
2847 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2849 /* make sure fixed part of mesh header is there, also checks skb len */
2850 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2851 return RX_DROP_MONITOR;
2853 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2855 /* make sure full mesh header is there, also checks skb len */
2856 if (!pskb_may_pull(rx->skb,
2857 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2858 return RX_DROP_MONITOR;
2860 /* reload pointers */
2861 hdr = (struct ieee80211_hdr *) skb->data;
2862 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2864 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2865 return RX_DROP_MONITOR;
2867 /* frame is in RMC, don't forward */
2868 if (ieee80211_is_data(hdr->frame_control) &&
2869 is_multicast_ether_addr(hdr->addr1) &&
2870 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2871 return RX_DROP_MONITOR;
2873 if (!ieee80211_is_data(hdr->frame_control))
2877 return RX_DROP_MONITOR;
2879 if (mesh_hdr->flags & MESH_FLAGS_AE) {
2880 struct mesh_path *mppath;
2884 if (is_multicast_ether_addr(hdr->addr1)) {
2885 mpp_addr = hdr->addr3;
2886 proxied_addr = mesh_hdr->eaddr1;
2887 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2888 MESH_FLAGS_AE_A5_A6) {
2889 /* has_a4 already checked in ieee80211_rx_mesh_check */
2890 mpp_addr = hdr->addr4;
2891 proxied_addr = mesh_hdr->eaddr2;
2893 return RX_DROP_MONITOR;
2897 mppath = mpp_path_lookup(sdata, proxied_addr);
2899 mpp_path_add(sdata, proxied_addr, mpp_addr);
2901 spin_lock_bh(&mppath->state_lock);
2902 if (!ether_addr_equal(mppath->mpp, mpp_addr))
2903 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2904 mppath->exp_time = jiffies;
2905 spin_unlock_bh(&mppath->state_lock);
2910 /* Frame has reached destination. Don't forward */
2911 if (!is_multicast_ether_addr(hdr->addr1) &&
2912 ether_addr_equal(sdata->vif.addr, hdr->addr3))
2915 ac = ieee802_1d_to_ac[skb->priority];
2916 q = sdata->vif.hw_queue[ac];
2917 if (ieee80211_queue_stopped(&local->hw, q)) {
2918 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2919 return RX_DROP_MONITOR;
2921 skb_set_queue_mapping(skb, ac);
2923 if (!--mesh_hdr->ttl) {
2924 if (!is_multicast_ether_addr(hdr->addr1))
2925 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2926 dropped_frames_ttl);
2930 if (!ifmsh->mshcfg.dot11MeshForwarding)
2933 if (sdata->crypto_tx_tailroom_needed_cnt)
2934 tailroom = IEEE80211_ENCRYPT_TAILROOM;
2936 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
2937 sdata->encrypt_headroom,
2938 tailroom, GFP_ATOMIC);
2942 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
2943 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2944 info = IEEE80211_SKB_CB(fwd_skb);
2945 memset(info, 0, sizeof(*info));
2946 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
2947 info->control.vif = &rx->sdata->vif;
2948 info->control.jiffies = jiffies;
2949 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2950 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2951 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2952 /* update power mode indication when forwarding */
2953 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2954 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2955 /* mesh power mode flags updated in mesh_nexthop_lookup */
2956 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2958 /* unable to resolve next hop */
2959 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2961 WLAN_REASON_MESH_PATH_NOFORWARD,
2963 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2965 return RX_DROP_MONITOR;
2968 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2969 ieee80211_add_pending_skb(local, fwd_skb);
2971 if (is_multicast_ether_addr(hdr->addr1))
2973 return RX_DROP_MONITOR;
2977 static ieee80211_rx_result debug_noinline
2978 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2980 struct ieee80211_sub_if_data *sdata = rx->sdata;
2981 struct ieee80211_local *local = rx->local;
2982 struct net_device *dev = sdata->dev;
2983 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2984 __le16 fc = hdr->frame_control;
2988 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2991 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2992 return RX_DROP_MONITOR;
2995 * Send unexpected-4addr-frame event to hostapd. For older versions,
2996 * also drop the frame to cooked monitor interfaces.
2998 if (ieee80211_has_a4(hdr->frame_control) &&
2999 sdata->vif.type == NL80211_IFTYPE_AP) {
3001 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
3002 cfg80211_rx_unexpected_4addr_frame(
3003 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
3004 return RX_DROP_MONITOR;
3007 err = __ieee80211_data_to_8023(rx, &port_control);
3009 return RX_DROP_UNUSABLE;
3011 if (!ieee80211_frame_allowed(rx, fc))
3012 return RX_DROP_MONITOR;
3014 /* directly handle TDLS channel switch requests/responses */
3015 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
3016 cpu_to_be16(ETH_P_TDLS))) {
3017 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
3019 if (pskb_may_pull(rx->skb,
3020 offsetof(struct ieee80211_tdls_data, u)) &&
3021 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
3022 tf->category == WLAN_CATEGORY_TDLS &&
3023 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
3024 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
3025 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
3026 schedule_work(&local->tdls_chsw_work);
3028 rx->sta->rx_stats.packets++;
3034 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
3035 unlikely(port_control) && sdata->bss) {
3036 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
3044 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
3045 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
3046 !is_multicast_ether_addr(
3047 ((struct ethhdr *)rx->skb->data)->h_dest) &&
3048 (!local->scanning &&
3049 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
3050 mod_timer(&local->dynamic_ps_timer, jiffies +
3051 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
3053 ieee80211_deliver_skb(rx);
3058 static ieee80211_rx_result debug_noinline
3059 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
3061 struct sk_buff *skb = rx->skb;
3062 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
3063 struct tid_ampdu_rx *tid_agg_rx;
3067 if (likely(!ieee80211_is_ctl(bar->frame_control)))
3070 if (ieee80211_is_back_req(bar->frame_control)) {
3072 __le16 control, start_seq_num;
3073 } __packed bar_data;
3074 struct ieee80211_event event = {
3075 .type = BAR_RX_EVENT,
3079 return RX_DROP_MONITOR;
3081 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
3082 &bar_data, sizeof(bar_data)))
3083 return RX_DROP_MONITOR;
3085 tid = le16_to_cpu(bar_data.control) >> 12;
3087 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
3088 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
3089 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
3090 WLAN_BACK_RECIPIENT,
3091 WLAN_REASON_QSTA_REQUIRE_SETUP);
3093 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
3095 return RX_DROP_MONITOR;
3097 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
3098 event.u.ba.tid = tid;
3099 event.u.ba.ssn = start_seq_num;
3100 event.u.ba.sta = &rx->sta->sta;
3102 /* reset session timer */
3103 if (tid_agg_rx->timeout)
3104 mod_timer(&tid_agg_rx->session_timer,
3105 TU_TO_EXP_TIME(tid_agg_rx->timeout));
3107 spin_lock(&tid_agg_rx->reorder_lock);
3108 /* release stored frames up to start of BAR */
3109 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
3110 start_seq_num, frames);
3111 spin_unlock(&tid_agg_rx->reorder_lock);
3113 drv_event_callback(rx->local, rx->sdata, &event);
3120 * After this point, we only want management frames,
3121 * so we can drop all remaining control frames to
3122 * cooked monitor interfaces.
3124 return RX_DROP_MONITOR;
3127 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
3128 struct ieee80211_mgmt *mgmt,
3131 struct ieee80211_local *local = sdata->local;
3132 struct sk_buff *skb;
3133 struct ieee80211_mgmt *resp;
3135 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
3136 /* Not to own unicast address */
3140 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
3141 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
3142 /* Not from the current AP or not associated yet. */
3146 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
3147 /* Too short SA Query request frame */
3151 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
3155 skb_reserve(skb, local->hw.extra_tx_headroom);
3156 resp = skb_put_zero(skb, 24);
3157 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3158 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
3159 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
3160 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3161 IEEE80211_STYPE_ACTION);
3162 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
3163 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
3164 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
3165 memcpy(resp->u.action.u.sa_query.trans_id,
3166 mgmt->u.action.u.sa_query.trans_id,
3167 WLAN_SA_QUERY_TR_ID_LEN);
3169 ieee80211_tx_skb(sdata, skb);
3172 static ieee80211_rx_result debug_noinline
3173 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
3175 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3176 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3178 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
3182 * From here on, look only at management frames.
3183 * Data and control frames are already handled,
3184 * and unknown (reserved) frames are useless.
3186 if (rx->skb->len < 24)
3187 return RX_DROP_MONITOR;
3189 if (!ieee80211_is_mgmt(mgmt->frame_control))
3190 return RX_DROP_MONITOR;
3192 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
3193 ieee80211_is_beacon(mgmt->frame_control) &&
3194 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
3197 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3198 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3199 sig = status->signal;
3201 cfg80211_report_obss_beacon_khz(rx->local->hw.wiphy,
3202 rx->skb->data, rx->skb->len,
3203 ieee80211_rx_status_to_khz(status),
3205 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
3208 if (ieee80211_drop_unencrypted_mgmt(rx))
3209 return RX_DROP_UNUSABLE;
3214 static ieee80211_rx_result debug_noinline
3215 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
3217 struct ieee80211_local *local = rx->local;
3218 struct ieee80211_sub_if_data *sdata = rx->sdata;
3219 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3220 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3221 int len = rx->skb->len;
3223 if (!ieee80211_is_action(mgmt->frame_control))
3226 /* drop too small frames */
3227 if (len < IEEE80211_MIN_ACTION_SIZE)
3228 return RX_DROP_UNUSABLE;
3230 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
3231 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
3232 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
3233 return RX_DROP_UNUSABLE;
3235 switch (mgmt->u.action.category) {
3236 case WLAN_CATEGORY_HT:
3237 /* reject HT action frames from stations not supporting HT */
3238 if (!rx->sta->sta.ht_cap.ht_supported)
3241 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3242 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3243 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3244 sdata->vif.type != NL80211_IFTYPE_AP &&
3245 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3248 /* verify action & smps_control/chanwidth are present */
3249 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3252 switch (mgmt->u.action.u.ht_smps.action) {
3253 case WLAN_HT_ACTION_SMPS: {
3254 struct ieee80211_supported_band *sband;
3255 enum ieee80211_smps_mode smps_mode;
3256 struct sta_opmode_info sta_opmode = {};
3258 if (sdata->vif.type != NL80211_IFTYPE_AP &&
3259 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
3262 /* convert to HT capability */
3263 switch (mgmt->u.action.u.ht_smps.smps_control) {
3264 case WLAN_HT_SMPS_CONTROL_DISABLED:
3265 smps_mode = IEEE80211_SMPS_OFF;
3267 case WLAN_HT_SMPS_CONTROL_STATIC:
3268 smps_mode = IEEE80211_SMPS_STATIC;
3270 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
3271 smps_mode = IEEE80211_SMPS_DYNAMIC;
3277 /* if no change do nothing */
3278 if (rx->sta->sta.smps_mode == smps_mode)
3280 rx->sta->sta.smps_mode = smps_mode;
3281 sta_opmode.smps_mode =
3282 ieee80211_smps_mode_to_smps_mode(smps_mode);
3283 sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
3285 sband = rx->local->hw.wiphy->bands[status->band];
3287 rate_control_rate_update(local, sband, rx->sta,
3288 IEEE80211_RC_SMPS_CHANGED);
3289 cfg80211_sta_opmode_change_notify(sdata->dev,
3295 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
3296 struct ieee80211_supported_band *sband;
3297 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
3298 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
3299 struct sta_opmode_info sta_opmode = {};
3301 /* If it doesn't support 40 MHz it can't change ... */
3302 if (!(rx->sta->sta.ht_cap.cap &
3303 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
3306 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
3307 max_bw = IEEE80211_STA_RX_BW_20;
3309 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
3311 /* set cur_max_bandwidth and recalc sta bw */
3312 rx->sta->cur_max_bandwidth = max_bw;
3313 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
3315 if (rx->sta->sta.bandwidth == new_bw)
3318 rx->sta->sta.bandwidth = new_bw;
3319 sband = rx->local->hw.wiphy->bands[status->band];
3321 ieee80211_sta_rx_bw_to_chan_width(rx->sta);
3322 sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
3324 rate_control_rate_update(local, sband, rx->sta,
3325 IEEE80211_RC_BW_CHANGED);
3326 cfg80211_sta_opmode_change_notify(sdata->dev,
3337 case WLAN_CATEGORY_PUBLIC:
3338 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3340 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3344 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3346 if (mgmt->u.action.u.ext_chan_switch.action_code !=
3347 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3349 if (len < offsetof(struct ieee80211_mgmt,
3350 u.action.u.ext_chan_switch.variable))
3353 case WLAN_CATEGORY_VHT:
3354 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3355 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3356 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3357 sdata->vif.type != NL80211_IFTYPE_AP &&
3358 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3361 /* verify action code is present */
3362 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3365 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
3366 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
3367 /* verify opmode is present */
3368 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
3372 case WLAN_VHT_ACTION_GROUPID_MGMT: {
3373 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
3381 case WLAN_CATEGORY_BACK:
3382 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3383 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3384 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3385 sdata->vif.type != NL80211_IFTYPE_AP &&
3386 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3389 /* verify action_code is present */
3390 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3393 switch (mgmt->u.action.u.addba_req.action_code) {
3394 case WLAN_ACTION_ADDBA_REQ:
3395 if (len < (IEEE80211_MIN_ACTION_SIZE +
3396 sizeof(mgmt->u.action.u.addba_req)))
3399 case WLAN_ACTION_ADDBA_RESP:
3400 if (len < (IEEE80211_MIN_ACTION_SIZE +
3401 sizeof(mgmt->u.action.u.addba_resp)))
3404 case WLAN_ACTION_DELBA:
3405 if (len < (IEEE80211_MIN_ACTION_SIZE +
3406 sizeof(mgmt->u.action.u.delba)))
3414 case WLAN_CATEGORY_SPECTRUM_MGMT:
3415 /* verify action_code is present */
3416 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3419 switch (mgmt->u.action.u.measurement.action_code) {
3420 case WLAN_ACTION_SPCT_MSR_REQ:
3421 if (status->band != NL80211_BAND_5GHZ)
3424 if (len < (IEEE80211_MIN_ACTION_SIZE +
3425 sizeof(mgmt->u.action.u.measurement)))
3428 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3431 ieee80211_process_measurement_req(sdata, mgmt, len);
3433 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3435 if (len < (IEEE80211_MIN_ACTION_SIZE +
3436 sizeof(mgmt->u.action.u.chan_switch)))
3439 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3440 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3441 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3444 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3445 bssid = sdata->u.mgd.bssid;
3446 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3447 bssid = sdata->u.ibss.bssid;
3448 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3453 if (!ether_addr_equal(mgmt->bssid, bssid))
3460 case WLAN_CATEGORY_SELF_PROTECTED:
3461 if (len < (IEEE80211_MIN_ACTION_SIZE +
3462 sizeof(mgmt->u.action.u.self_prot.action_code)))
3465 switch (mgmt->u.action.u.self_prot.action_code) {
3466 case WLAN_SP_MESH_PEERING_OPEN:
3467 case WLAN_SP_MESH_PEERING_CLOSE:
3468 case WLAN_SP_MESH_PEERING_CONFIRM:
3469 if (!ieee80211_vif_is_mesh(&sdata->vif))
3471 if (sdata->u.mesh.user_mpm)
3472 /* userspace handles this frame */
3475 case WLAN_SP_MGK_INFORM:
3476 case WLAN_SP_MGK_ACK:
3477 if (!ieee80211_vif_is_mesh(&sdata->vif))
3482 case WLAN_CATEGORY_MESH_ACTION:
3483 if (len < (IEEE80211_MIN_ACTION_SIZE +
3484 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3487 if (!ieee80211_vif_is_mesh(&sdata->vif))
3489 if (mesh_action_is_path_sel(mgmt) &&
3490 !mesh_path_sel_is_hwmp(sdata))
3498 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3499 /* will return in the next handlers */
3504 rx->sta->rx_stats.packets++;
3505 dev_kfree_skb(rx->skb);
3509 skb_queue_tail(&sdata->skb_queue, rx->skb);
3510 ieee80211_queue_work(&local->hw, &sdata->work);
3512 rx->sta->rx_stats.packets++;
3516 static ieee80211_rx_result debug_noinline
3517 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3519 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3522 /* skip known-bad action frames and return them in the next handler */
3523 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3527 * Getting here means the kernel doesn't know how to handle
3528 * it, but maybe userspace does ... include returned frames
3529 * so userspace can register for those to know whether ones
3530 * it transmitted were processed or returned.
3533 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
3534 !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
3535 sig = status->signal;
3537 if (cfg80211_rx_mgmt_khz(&rx->sdata->wdev,
3538 ieee80211_rx_status_to_khz(status), sig,
3539 rx->skb->data, rx->skb->len, 0)) {
3541 rx->sta->rx_stats.packets++;
3542 dev_kfree_skb(rx->skb);
3549 static ieee80211_rx_result debug_noinline
3550 ieee80211_rx_h_action_post_userspace(struct ieee80211_rx_data *rx)
3552 struct ieee80211_sub_if_data *sdata = rx->sdata;
3553 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3554 int len = rx->skb->len;
3556 if (!ieee80211_is_action(mgmt->frame_control))
3559 switch (mgmt->u.action.category) {
3560 case WLAN_CATEGORY_SA_QUERY:
3561 if (len < (IEEE80211_MIN_ACTION_SIZE +
3562 sizeof(mgmt->u.action.u.sa_query)))
3565 switch (mgmt->u.action.u.sa_query.action) {
3566 case WLAN_ACTION_SA_QUERY_REQUEST:
3567 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3569 ieee80211_process_sa_query_req(sdata, mgmt, len);
3579 rx->sta->rx_stats.packets++;
3580 dev_kfree_skb(rx->skb);
3584 static ieee80211_rx_result debug_noinline
3585 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3587 struct ieee80211_local *local = rx->local;
3588 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3589 struct sk_buff *nskb;
3590 struct ieee80211_sub_if_data *sdata = rx->sdata;
3591 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3593 if (!ieee80211_is_action(mgmt->frame_control))
3597 * For AP mode, hostapd is responsible for handling any action
3598 * frames that we didn't handle, including returning unknown
3599 * ones. For all other modes we will return them to the sender,
3600 * setting the 0x80 bit in the action category, as required by
3601 * 802.11-2012 9.24.4.
3602 * Newer versions of hostapd shall also use the management frame
3603 * registration mechanisms, but older ones still use cooked
3604 * monitor interfaces so push all frames there.
3606 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3607 (sdata->vif.type == NL80211_IFTYPE_AP ||
3608 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3609 return RX_DROP_MONITOR;
3611 if (is_multicast_ether_addr(mgmt->da))
3612 return RX_DROP_MONITOR;
3614 /* do not return rejected action frames */
3615 if (mgmt->u.action.category & 0x80)
3616 return RX_DROP_UNUSABLE;
3618 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3621 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3623 nmgmt->u.action.category |= 0x80;
3624 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3625 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3627 memset(nskb->cb, 0, sizeof(nskb->cb));
3629 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3630 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3632 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3633 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3634 IEEE80211_TX_CTL_NO_CCK_RATE;
3635 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3637 local->hw.offchannel_tx_hw_queue;
3640 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3643 dev_kfree_skb(rx->skb);
3647 static ieee80211_rx_result debug_noinline
3648 ieee80211_rx_h_ext(struct ieee80211_rx_data *rx)
3650 struct ieee80211_sub_if_data *sdata = rx->sdata;
3651 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
3653 if (!ieee80211_is_ext(hdr->frame_control))
3656 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3657 return RX_DROP_MONITOR;
3659 /* for now only beacons are ext, so queue them */
3660 skb_queue_tail(&sdata->skb_queue, rx->skb);
3661 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3663 rx->sta->rx_stats.packets++;
3668 static ieee80211_rx_result debug_noinline
3669 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3671 struct ieee80211_sub_if_data *sdata = rx->sdata;
3672 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3675 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3677 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3678 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3679 sdata->vif.type != NL80211_IFTYPE_OCB &&
3680 sdata->vif.type != NL80211_IFTYPE_STATION)
3681 return RX_DROP_MONITOR;
3684 case cpu_to_le16(IEEE80211_STYPE_AUTH):
3685 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3686 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3687 /* process for all: mesh, mlme, ibss */
3689 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3690 if (is_multicast_ether_addr(mgmt->da) &&
3691 !is_broadcast_ether_addr(mgmt->da))
3692 return RX_DROP_MONITOR;
3694 /* process only for station/IBSS */
3695 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3696 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3697 return RX_DROP_MONITOR;
3699 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3700 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3701 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3702 if (is_multicast_ether_addr(mgmt->da) &&
3703 !is_broadcast_ether_addr(mgmt->da))
3704 return RX_DROP_MONITOR;
3706 /* process only for station */
3707 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3708 return RX_DROP_MONITOR;
3710 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3711 /* process only for ibss and mesh */
3712 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3713 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3714 return RX_DROP_MONITOR;
3717 return RX_DROP_MONITOR;
3720 /* queue up frame and kick off work to process it */
3721 skb_queue_tail(&sdata->skb_queue, rx->skb);
3722 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3724 rx->sta->rx_stats.packets++;
3729 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3730 struct ieee80211_rate *rate)
3732 struct ieee80211_sub_if_data *sdata;
3733 struct ieee80211_local *local = rx->local;
3734 struct sk_buff *skb = rx->skb, *skb2;
3735 struct net_device *prev_dev = NULL;
3736 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3737 int needed_headroom;
3740 * If cooked monitor has been processed already, then
3741 * don't do it again. If not, set the flag.
3743 if (rx->flags & IEEE80211_RX_CMNTR)
3745 rx->flags |= IEEE80211_RX_CMNTR;
3747 /* If there are no cooked monitor interfaces, just free the SKB */
3748 if (!local->cooked_mntrs)
3751 /* vendor data is long removed here */
3752 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3753 /* room for the radiotap header based on driver features */
3754 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3756 if (skb_headroom(skb) < needed_headroom &&
3757 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3760 /* prepend radiotap information */
3761 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3764 skb_reset_mac_header(skb);
3765 skb->ip_summed = CHECKSUM_UNNECESSARY;
3766 skb->pkt_type = PACKET_OTHERHOST;
3767 skb->protocol = htons(ETH_P_802_2);
3769 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3770 if (!ieee80211_sdata_running(sdata))
3773 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3774 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3778 skb2 = skb_clone(skb, GFP_ATOMIC);
3780 skb2->dev = prev_dev;
3781 netif_receive_skb(skb2);
3785 prev_dev = sdata->dev;
3786 ieee80211_rx_stats(sdata->dev, skb->len);
3790 skb->dev = prev_dev;
3791 netif_receive_skb(skb);
3799 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3800 ieee80211_rx_result res)
3803 case RX_DROP_MONITOR:
3804 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3806 rx->sta->rx_stats.dropped++;
3809 struct ieee80211_rate *rate = NULL;
3810 struct ieee80211_supported_band *sband;
3811 struct ieee80211_rx_status *status;
3813 status = IEEE80211_SKB_RXCB((rx->skb));
3815 sband = rx->local->hw.wiphy->bands[status->band];
3816 if (status->encoding == RX_ENC_LEGACY)
3817 rate = &sband->bitrates[status->rate_idx];
3819 ieee80211_rx_cooked_monitor(rx, rate);
3822 case RX_DROP_UNUSABLE:
3823 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3825 rx->sta->rx_stats.dropped++;
3826 dev_kfree_skb(rx->skb);
3829 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3834 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3835 struct sk_buff_head *frames)
3837 ieee80211_rx_result res = RX_DROP_MONITOR;
3838 struct sk_buff *skb;
3840 #define CALL_RXH(rxh) \
3843 if (res != RX_CONTINUE) \
3847 /* Lock here to avoid hitting all of the data used in the RX
3848 * path (e.g. key data, station data, ...) concurrently when
3849 * a frame is released from the reorder buffer due to timeout
3850 * from the timer, potentially concurrently with RX from the
3853 spin_lock_bh(&rx->local->rx_path_lock);
3855 while ((skb = __skb_dequeue(frames))) {
3857 * all the other fields are valid across frames
3858 * that belong to an aMPDU since they are on the
3859 * same TID from the same station
3863 CALL_RXH(ieee80211_rx_h_check_more_data);
3864 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3865 CALL_RXH(ieee80211_rx_h_sta_process);
3866 CALL_RXH(ieee80211_rx_h_decrypt);
3867 CALL_RXH(ieee80211_rx_h_defragment);
3868 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3869 /* must be after MMIC verify so header is counted in MPDU mic */
3870 #ifdef CONFIG_MAC80211_MESH
3871 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3872 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3874 CALL_RXH(ieee80211_rx_h_amsdu);
3875 CALL_RXH(ieee80211_rx_h_data);
3877 /* special treatment -- needs the queue */
3878 res = ieee80211_rx_h_ctrl(rx, frames);
3879 if (res != RX_CONTINUE)
3882 CALL_RXH(ieee80211_rx_h_mgmt_check);
3883 CALL_RXH(ieee80211_rx_h_action);
3884 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3885 CALL_RXH(ieee80211_rx_h_action_post_userspace);
3886 CALL_RXH(ieee80211_rx_h_action_return);
3887 CALL_RXH(ieee80211_rx_h_ext);
3888 CALL_RXH(ieee80211_rx_h_mgmt);
3891 ieee80211_rx_handlers_result(rx, res);
3896 spin_unlock_bh(&rx->local->rx_path_lock);
3899 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3901 struct sk_buff_head reorder_release;
3902 ieee80211_rx_result res = RX_DROP_MONITOR;
3904 __skb_queue_head_init(&reorder_release);
3906 #define CALL_RXH(rxh) \
3909 if (res != RX_CONTINUE) \
3913 CALL_RXH(ieee80211_rx_h_check_dup);
3914 CALL_RXH(ieee80211_rx_h_check);
3916 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3918 ieee80211_rx_handlers(rx, &reorder_release);
3922 ieee80211_rx_handlers_result(rx, res);
3928 * This function makes calls into the RX path, therefore
3929 * it has to be invoked under RCU read lock.
3931 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3933 struct sk_buff_head frames;
3934 struct ieee80211_rx_data rx = {
3936 .sdata = sta->sdata,
3937 .local = sta->local,
3938 /* This is OK -- must be QoS data frame */
3939 .security_idx = tid,
3942 struct tid_ampdu_rx *tid_agg_rx;
3944 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3948 __skb_queue_head_init(&frames);
3950 spin_lock(&tid_agg_rx->reorder_lock);
3951 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3952 spin_unlock(&tid_agg_rx->reorder_lock);
3954 if (!skb_queue_empty(&frames)) {
3955 struct ieee80211_event event = {
3956 .type = BA_FRAME_TIMEOUT,
3958 .u.ba.sta = &sta->sta,
3960 drv_event_callback(rx.local, rx.sdata, &event);
3963 ieee80211_rx_handlers(&rx, &frames);
3966 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3967 u16 ssn, u64 filtered,
3970 struct sta_info *sta;
3971 struct tid_ampdu_rx *tid_agg_rx;
3972 struct sk_buff_head frames;
3973 struct ieee80211_rx_data rx = {
3974 /* This is OK -- must be QoS data frame */
3975 .security_idx = tid,
3980 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3983 __skb_queue_head_init(&frames);
3985 sta = container_of(pubsta, struct sta_info, sta);
3988 rx.sdata = sta->sdata;
3989 rx.local = sta->local;
3992 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3996 spin_lock_bh(&tid_agg_rx->reorder_lock);
3998 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
4001 /* release all frames in the reorder buffer */
4002 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
4003 IEEE80211_SN_MODULO;
4004 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
4006 /* update ssn to match received ssn */
4007 tid_agg_rx->head_seq_num = ssn;
4009 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
4013 /* handle the case that received ssn is behind the mac ssn.
4014 * it can be tid_agg_rx->buf_size behind and still be valid */
4015 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
4016 if (diff >= tid_agg_rx->buf_size) {
4017 tid_agg_rx->reorder_buf_filtered = 0;
4020 filtered = filtered >> diff;
4024 for (i = 0; i < tid_agg_rx->buf_size; i++) {
4025 int index = (ssn + i) % tid_agg_rx->buf_size;
4027 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
4028 if (filtered & BIT_ULL(i))
4029 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
4032 /* now process also frames that the filter marking released */
4033 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
4036 spin_unlock_bh(&tid_agg_rx->reorder_lock);
4038 ieee80211_rx_handlers(&rx, &frames);
4043 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
4045 /* main receive path */
4047 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
4049 struct ieee80211_sub_if_data *sdata = rx->sdata;
4050 struct sk_buff *skb = rx->skb;
4051 struct ieee80211_hdr *hdr = (void *)skb->data;
4052 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4053 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
4054 bool multicast = is_multicast_ether_addr(hdr->addr1) ||
4055 ieee80211_is_s1g_beacon(hdr->frame_control);
4057 switch (sdata->vif.type) {
4058 case NL80211_IFTYPE_STATION:
4059 if (!bssid && !sdata->u.mgd.use_4addr)
4061 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
4065 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4066 case NL80211_IFTYPE_ADHOC:
4069 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
4070 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2) ||
4071 !is_valid_ether_addr(hdr->addr2))
4073 if (ieee80211_is_beacon(hdr->frame_control))
4075 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
4078 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4082 if (status->encoding != RX_ENC_LEGACY)
4083 rate_idx = 0; /* TODO: HT/VHT rates */
4085 rate_idx = status->rate_idx;
4086 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
4090 case NL80211_IFTYPE_OCB:
4093 if (!ieee80211_is_data_present(hdr->frame_control))
4095 if (!is_broadcast_ether_addr(bssid))
4098 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
4102 if (status->encoding != RX_ENC_LEGACY)
4103 rate_idx = 0; /* TODO: HT rates */
4105 rate_idx = status->rate_idx;
4106 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
4110 case NL80211_IFTYPE_MESH_POINT:
4111 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
4115 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4116 case NL80211_IFTYPE_AP_VLAN:
4117 case NL80211_IFTYPE_AP:
4119 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
4121 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
4123 * Accept public action frames even when the
4124 * BSSID doesn't match, this is used for P2P
4125 * and location updates. Note that mac80211
4126 * itself never looks at these frames.
4129 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
4131 if (ieee80211_is_public_action(hdr, skb->len))
4133 return ieee80211_is_beacon(hdr->frame_control);
4136 if (!ieee80211_has_tods(hdr->frame_control)) {
4137 /* ignore data frames to TDLS-peers */
4138 if (ieee80211_is_data(hdr->frame_control))
4140 /* ignore action frames to TDLS-peers */
4141 if (ieee80211_is_action(hdr->frame_control) &&
4142 !is_broadcast_ether_addr(bssid) &&
4143 !ether_addr_equal(bssid, hdr->addr1))
4148 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
4149 * the BSSID - we've checked that already but may have accepted
4150 * the wildcard (ff:ff:ff:ff:ff:ff).
4153 * The BSSID of the Data frame is determined as follows:
4154 * a) If the STA is contained within an AP or is associated
4155 * with an AP, the BSSID is the address currently in use
4156 * by the STA contained in the AP.
4158 * So we should not accept data frames with an address that's
4161 * Accepting it also opens a security problem because stations
4162 * could encrypt it with the GTK and inject traffic that way.
4164 if (ieee80211_is_data(hdr->frame_control) && multicast)
4168 case NL80211_IFTYPE_WDS:
4169 if (bssid || !ieee80211_is_data(hdr->frame_control))
4171 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
4172 case NL80211_IFTYPE_P2P_DEVICE:
4173 return ieee80211_is_public_action(hdr, skb->len) ||
4174 ieee80211_is_probe_req(hdr->frame_control) ||
4175 ieee80211_is_probe_resp(hdr->frame_control) ||
4176 ieee80211_is_beacon(hdr->frame_control);
4177 case NL80211_IFTYPE_NAN:
4178 /* Currently no frames on NAN interface are allowed */
4188 void ieee80211_check_fast_rx(struct sta_info *sta)
4190 struct ieee80211_sub_if_data *sdata = sta->sdata;
4191 struct ieee80211_local *local = sdata->local;
4192 struct ieee80211_key *key;
4193 struct ieee80211_fast_rx fastrx = {
4195 .vif_type = sdata->vif.type,
4196 .control_port_protocol = sdata->control_port_protocol,
4197 }, *old, *new = NULL;
4198 bool assign = false;
4200 /* use sparse to check that we don't return without updating */
4201 __acquire(check_fast_rx);
4203 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
4204 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
4205 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
4206 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
4208 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
4210 /* fast-rx doesn't do reordering */
4211 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
4212 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
4215 switch (sdata->vif.type) {
4216 case NL80211_IFTYPE_STATION:
4217 if (sta->sta.tdls) {
4218 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4219 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4220 fastrx.expected_ds_bits = 0;
4222 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
4223 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
4224 fastrx.expected_ds_bits =
4225 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4228 if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
4229 fastrx.expected_ds_bits |=
4230 cpu_to_le16(IEEE80211_FCTL_TODS);
4231 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4232 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4235 if (!sdata->u.mgd.powersave)
4238 /* software powersave is a huge mess, avoid all of it */
4239 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
4241 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
4242 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
4245 case NL80211_IFTYPE_AP_VLAN:
4246 case NL80211_IFTYPE_AP:
4247 /* parallel-rx requires this, at least with calls to
4248 * ieee80211_sta_ps_transition()
4250 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
4252 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
4253 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
4254 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
4256 fastrx.internal_forward =
4257 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
4258 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
4259 !sdata->u.vlan.sta);
4261 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
4262 sdata->u.vlan.sta) {
4263 fastrx.expected_ds_bits |=
4264 cpu_to_le16(IEEE80211_FCTL_FROMDS);
4265 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
4266 fastrx.internal_forward = 0;
4274 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
4278 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4280 key = rcu_dereference(sdata->default_unicast_key);
4282 switch (key->conf.cipher) {
4283 case WLAN_CIPHER_SUITE_TKIP:
4284 /* we don't want to deal with MMIC in fast-rx */
4286 case WLAN_CIPHER_SUITE_CCMP:
4287 case WLAN_CIPHER_SUITE_CCMP_256:
4288 case WLAN_CIPHER_SUITE_GCMP:
4289 case WLAN_CIPHER_SUITE_GCMP_256:
4292 /* We also don't want to deal with
4293 * WEP or cipher scheme.
4299 fastrx.icv_len = key->conf.icv_len;
4306 __release(check_fast_rx);
4309 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
4311 spin_lock_bh(&sta->lock);
4312 old = rcu_dereference_protected(sta->fast_rx, true);
4313 rcu_assign_pointer(sta->fast_rx, new);
4314 spin_unlock_bh(&sta->lock);
4317 kfree_rcu(old, rcu_head);
4320 void ieee80211_clear_fast_rx(struct sta_info *sta)
4322 struct ieee80211_fast_rx *old;
4324 spin_lock_bh(&sta->lock);
4325 old = rcu_dereference_protected(sta->fast_rx, true);
4326 RCU_INIT_POINTER(sta->fast_rx, NULL);
4327 spin_unlock_bh(&sta->lock);
4330 kfree_rcu(old, rcu_head);
4333 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4335 struct ieee80211_local *local = sdata->local;
4336 struct sta_info *sta;
4338 lockdep_assert_held(&local->sta_mtx);
4340 list_for_each_entry(sta, &local->sta_list, list) {
4341 if (sdata != sta->sdata &&
4342 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
4344 ieee80211_check_fast_rx(sta);
4348 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
4350 struct ieee80211_local *local = sdata->local;
4352 mutex_lock(&local->sta_mtx);
4353 __ieee80211_check_fast_rx_iface(sdata);
4354 mutex_unlock(&local->sta_mtx);
4357 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
4358 struct ieee80211_fast_rx *fast_rx)
4360 struct sk_buff *skb = rx->skb;
4361 struct ieee80211_hdr *hdr = (void *)skb->data;
4362 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4363 struct sta_info *sta = rx->sta;
4364 int orig_len = skb->len;
4365 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
4366 int snap_offs = hdrlen;
4368 u8 snap[sizeof(rfc1042_header)];
4370 } *payload __aligned(2);
4374 } addrs __aligned(2);
4375 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
4377 if (fast_rx->uses_rss)
4378 stats = this_cpu_ptr(sta->pcpu_rx_stats);
4380 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
4381 * to a common data structure; drivers can implement that per queue
4382 * but we don't have that information in mac80211
4384 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
4387 #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
4389 /* If using encryption, we also need to have:
4390 * - PN_VALIDATED: similar, but the implementation is tricky
4391 * - DECRYPTED: necessary for PN_VALIDATED
4394 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
4397 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
4400 if (unlikely(ieee80211_is_frag(hdr)))
4403 /* Since our interface address cannot be multicast, this
4404 * implicitly also rejects multicast frames without the
4407 * We shouldn't get any *data* frames not addressed to us
4408 * (AP mode will accept multicast *management* frames), but
4409 * punting here will make it go through the full checks in
4410 * ieee80211_accept_frame().
4412 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
4415 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
4416 IEEE80211_FCTL_TODS)) !=
4417 fast_rx->expected_ds_bits)
4420 /* assign the key to drop unencrypted frames (later)
4421 * and strip the IV/MIC if necessary
4423 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
4424 /* GCMP header length is the same */
4425 snap_offs += IEEE80211_CCMP_HDR_LEN;
4428 if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
4429 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
4432 payload = (void *)(skb->data + snap_offs);
4434 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
4437 /* Don't handle these here since they require special code.
4438 * Accept AARP and IPX even though they should come with a
4439 * bridge-tunnel header - but if we get them this way then
4440 * there's little point in discarding them.
4442 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
4443 payload->proto == fast_rx->control_port_protocol))
4447 /* after this point, don't punt to the slowpath! */
4449 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
4450 pskb_trim(skb, skb->len - fast_rx->icv_len))
4453 /* statistics part of ieee80211_rx_h_sta_process() */
4454 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4455 stats->last_signal = status->signal;
4456 if (!fast_rx->uses_rss)
4457 ewma_signal_add(&sta->rx_stats_avg.signal,
4461 if (status->chains) {
4464 stats->chains = status->chains;
4465 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4466 int signal = status->chain_signal[i];
4468 if (!(status->chains & BIT(i)))
4471 stats->chain_signal_last[i] = signal;
4472 if (!fast_rx->uses_rss)
4473 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4477 /* end of statistics */
4479 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4482 if (status->rx_flags & IEEE80211_RX_AMSDU) {
4483 if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
4490 stats->last_rx = jiffies;
4491 stats->last_rate = sta_stats_encode_rate(status);
4496 /* do the header conversion - first grab the addresses */
4497 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4498 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4499 /* remove the SNAP but leave the ethertype */
4500 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4501 /* push the addresses in front */
4502 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4504 skb->dev = fast_rx->dev;
4506 ieee80211_rx_stats(fast_rx->dev, skb->len);
4508 /* The seqno index has the same property as needed
4509 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4510 * for non-QoS-data frames. Here we know it's a data
4511 * frame, so count MSDUs.
4513 u64_stats_update_begin(&stats->syncp);
4514 stats->msdu[rx->seqno_idx]++;
4515 stats->bytes += orig_len;
4516 u64_stats_update_end(&stats->syncp);
4518 if (fast_rx->internal_forward) {
4519 struct sk_buff *xmit_skb = NULL;
4520 if (is_multicast_ether_addr(addrs.da)) {
4521 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4522 } else if (!ether_addr_equal(addrs.da, addrs.sa) &&
4523 sta_info_get(rx->sdata, addrs.da)) {
4530 * Send to wireless media and increase priority by 256
4531 * to keep the received priority instead of
4532 * reclassifying the frame (see cfg80211_classify8021d).
4534 xmit_skb->priority += 256;
4535 xmit_skb->protocol = htons(ETH_P_802_3);
4536 skb_reset_network_header(xmit_skb);
4537 skb_reset_mac_header(xmit_skb);
4538 dev_queue_xmit(xmit_skb);
4545 /* deliver to local stack */
4546 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4547 memset(skb->cb, 0, sizeof(skb->cb));
4549 list_add_tail(&skb->list, rx->list);
4551 netif_receive_skb(skb);
4561 * This function returns whether or not the SKB
4562 * was destined for RX processing or not, which,
4563 * if consume is true, is equivalent to whether
4564 * or not the skb was consumed.
4566 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4567 struct sk_buff *skb, bool consume)
4569 struct ieee80211_local *local = rx->local;
4570 struct ieee80211_sub_if_data *sdata = rx->sdata;
4574 /* See if we can do fast-rx; if we have to copy we already lost,
4575 * so punt in that case. We should never have to deliver a data
4576 * frame to multiple interfaces anyway.
4578 * We skip the ieee80211_accept_frame() call and do the necessary
4579 * checking inside ieee80211_invoke_fast_rx().
4581 if (consume && rx->sta) {
4582 struct ieee80211_fast_rx *fast_rx;
4584 fast_rx = rcu_dereference(rx->sta->fast_rx);
4585 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4589 if (!ieee80211_accept_frame(rx))
4593 skb = skb_copy(skb, GFP_ATOMIC);
4595 if (net_ratelimit())
4596 wiphy_debug(local->hw.wiphy,
4597 "failed to copy skb for %s\n",
4605 ieee80211_invoke_rx_handlers(rx);
4610 * This is the actual Rx frames handler. as it belongs to Rx path it must
4611 * be called with rcu_read_lock protection.
4613 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
4614 struct ieee80211_sta *pubsta,
4615 struct sk_buff *skb,
4616 struct list_head *list)
4618 struct ieee80211_local *local = hw_to_local(hw);
4619 struct ieee80211_sub_if_data *sdata;
4620 struct ieee80211_hdr *hdr;
4622 struct ieee80211_rx_data rx;
4623 struct ieee80211_sub_if_data *prev;
4624 struct rhlist_head *tmp;
4627 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
4628 memset(&rx, 0, sizeof(rx));
4633 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
4634 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4636 if (ieee80211_is_mgmt(fc)) {
4637 /* drop frame if too short for header */
4638 if (skb->len < ieee80211_hdrlen(fc))
4641 err = skb_linearize(skb);
4643 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4651 hdr = (struct ieee80211_hdr *)skb->data;
4652 ieee80211_parse_qos(&rx);
4653 ieee80211_verify_alignment(&rx);
4655 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4656 ieee80211_is_beacon(hdr->frame_control) ||
4657 ieee80211_is_s1g_beacon(hdr->frame_control)))
4658 ieee80211_scan_rx(local, skb);
4660 if (ieee80211_is_data(fc)) {
4661 struct sta_info *sta, *prev_sta;
4664 rx.sta = container_of(pubsta, struct sta_info, sta);
4665 rx.sdata = rx.sta->sdata;
4666 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4673 for_each_sta_info(local, hdr->addr2, sta, tmp) {
4680 rx.sdata = prev_sta->sdata;
4681 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4688 rx.sdata = prev_sta->sdata;
4690 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4698 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4699 if (!ieee80211_sdata_running(sdata))
4702 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4703 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4707 * frame is destined for this interface, but if it's
4708 * not also for the previous one we handle that after
4709 * the loop to avoid copying the SKB once too much
4717 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4719 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4725 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4728 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4737 * This is the receive path handler. It is called by a low level driver when an
4738 * 802.11 MPDU is received from the hardware.
4740 void ieee80211_rx_list(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4741 struct sk_buff *skb, struct list_head *list)
4743 struct ieee80211_local *local = hw_to_local(hw);
4744 struct ieee80211_rate *rate = NULL;
4745 struct ieee80211_supported_band *sband;
4746 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4748 WARN_ON_ONCE(softirq_count() == 0);
4750 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4753 sband = local->hw.wiphy->bands[status->band];
4754 if (WARN_ON(!sband))
4758 * If we're suspending, it is possible although not too likely
4759 * that we'd be receiving frames after having already partially
4760 * quiesced the stack. We can't process such frames then since
4761 * that might, for example, cause stations to be added or other
4762 * driver callbacks be invoked.
4764 if (unlikely(local->quiescing || local->suspended))
4767 /* We might be during a HW reconfig, prevent Rx for the same reason */
4768 if (unlikely(local->in_reconfig))
4772 * The same happens when we're not even started,
4773 * but that's worth a warning.
4775 if (WARN_ON(!local->started))
4778 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4780 * Validate the rate, unless a PLCP error means that
4781 * we probably can't have a valid rate here anyway.
4784 switch (status->encoding) {
4787 * rate_idx is MCS index, which can be [0-76]
4790 * https://wireless.wiki.kernel.org/en/developers/Documentation/ieee80211/802.11n
4792 * Anything else would be some sort of driver or
4793 * hardware error. The driver should catch hardware
4796 if (WARN(status->rate_idx > 76,
4797 "Rate marked as an HT rate but passed "
4798 "status->rate_idx is not "
4799 "an MCS index [0-76]: %d (0x%02x)\n",
4805 if (WARN_ONCE(status->rate_idx > 11 ||
4808 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4809 status->rate_idx, status->nss))
4813 if (WARN_ONCE(status->rate_idx > 11 ||
4816 "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
4817 status->rate_idx, status->nss))
4824 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4826 rate = &sband->bitrates[status->rate_idx];
4830 status->rx_flags = 0;
4833 * Frames with failed FCS/PLCP checksum are not returned,
4834 * all other frames are returned without radiotap header
4835 * if it was previously present.
4836 * Also, frames with less than 16 bytes are dropped.
4838 skb = ieee80211_rx_monitor(local, skb, rate);
4842 ieee80211_tpt_led_trig_rx(local,
4843 ((struct ieee80211_hdr *)skb->data)->frame_control,
4846 __ieee80211_rx_handle_packet(hw, pubsta, skb, list);
4852 EXPORT_SYMBOL(ieee80211_rx_list);
4854 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4855 struct sk_buff *skb, struct napi_struct *napi)
4857 struct sk_buff *tmp;
4862 * key references and virtual interfaces are protected using RCU
4863 * and this requires that we are in a read-side RCU section during
4864 * receive processing
4867 ieee80211_rx_list(hw, pubsta, skb, &list);
4871 netif_receive_skb_list(&list);
4875 list_for_each_entry_safe(skb, tmp, &list, list) {
4876 skb_list_del_init(skb);
4877 napi_gro_receive(napi, skb);
4880 EXPORT_SYMBOL(ieee80211_rx_napi);
4882 /* This is a version of the rx handler that can be called from hard irq
4883 * context. Post the skb on the queue and schedule the tasklet */
4884 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4886 struct ieee80211_local *local = hw_to_local(hw);
4888 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4890 skb->pkt_type = IEEE80211_RX_MSG;
4891 skb_queue_tail(&local->skb_queue, skb);
4892 tasklet_schedule(&local->tasklet);
4894 EXPORT_SYMBOL(ieee80211_rx_irqsafe);