2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/jiffies.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/skbuff.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/rcupdate.h>
21 #include <linux/export.h>
22 #include <linux/bitops.h>
23 #include <net/mac80211.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <asm/unaligned.h>
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
37 static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
39 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
41 u64_stats_update_begin(&tstats->syncp);
43 tstats->rx_bytes += len;
44 u64_stats_update_end(&tstats->syncp);
47 static u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
48 enum nl80211_iftype type)
50 __le16 fc = hdr->frame_control;
52 if (ieee80211_is_data(fc)) {
53 if (len < 24) /* drop incorrect hdr len (data) */
56 if (ieee80211_has_a4(fc))
58 if (ieee80211_has_tods(fc))
60 if (ieee80211_has_fromds(fc))
66 if (ieee80211_is_mgmt(fc)) {
67 if (len < 24) /* drop incorrect hdr len (mgmt) */
72 if (ieee80211_is_ctl(fc)) {
73 if (ieee80211_is_pspoll(fc))
76 if (ieee80211_is_back_req(fc)) {
78 case NL80211_IFTYPE_STATION:
80 case NL80211_IFTYPE_AP:
81 case NL80211_IFTYPE_AP_VLAN:
84 break; /* fall through to the return */
93 * monitor mode reception
95 * This function cleans up the SKB, i.e. it removes all the stuff
96 * only useful for monitoring.
98 static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
100 unsigned int rtap_vendor_space)
102 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
103 if (likely(skb->len > FCS_LEN))
104 __pskb_trim(skb, skb->len - FCS_LEN);
113 __pskb_pull(skb, rtap_vendor_space);
118 static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
119 unsigned int rtap_vendor_space)
121 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
122 struct ieee80211_hdr *hdr;
124 hdr = (void *)(skb->data + rtap_vendor_space);
126 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
127 RX_FLAG_FAILED_PLCP_CRC |
128 RX_FLAG_ONLY_MONITOR))
131 if (unlikely(skb->len < 16 + present_fcs_len + rtap_vendor_space))
134 if (ieee80211_is_ctl(hdr->frame_control) &&
135 !ieee80211_is_pspoll(hdr->frame_control) &&
136 !ieee80211_is_back_req(hdr->frame_control))
143 ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
144 struct ieee80211_rx_status *status,
149 /* always present fields */
150 len = sizeof(struct ieee80211_radiotap_header) + 8;
152 /* allocate extra bitmaps */
154 len += 4 * hweight8(status->chains);
156 if (ieee80211_have_rx_timestamp(status)) {
160 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
163 /* antenna field, if we don't have per-chain info */
167 /* padding for RX_FLAGS if necessary */
170 if (status->flag & RX_FLAG_HT) /* HT info */
173 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
178 if (status->flag & RX_FLAG_VHT) {
183 if (status->chains) {
184 /* antenna and antenna signal fields */
185 len += 2 * hweight8(status->chains);
188 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
189 struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
191 /* vendor presence bitmap */
193 /* alignment for fixed 6-byte vendor data header */
195 /* vendor data header */
197 if (WARN_ON(rtap->align == 0))
199 len = ALIGN(len, rtap->align);
200 len += rtap->len + rtap->pad;
207 * ieee80211_add_rx_radiotap_header - add radiotap header
209 * add a radiotap header containing all the fields which the hardware provided.
212 ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
214 struct ieee80211_rate *rate,
215 int rtap_len, bool has_fcs)
217 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
218 struct ieee80211_radiotap_header *rthdr;
223 u16 channel_flags = 0;
225 unsigned long chains = status->chains;
226 struct ieee80211_vendor_radiotap rtap = {};
228 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
229 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
230 /* rtap.len and rtap.pad are undone immediately */
231 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
235 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
238 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
239 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
240 it_present = &rthdr->it_present;
242 /* radiotap header, set always present flags */
243 rthdr->it_len = cpu_to_le16(rtap_len);
244 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
245 BIT(IEEE80211_RADIOTAP_CHANNEL) |
246 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
249 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
251 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
253 BIT(IEEE80211_RADIOTAP_EXT) |
254 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
255 put_unaligned_le32(it_present_val, it_present);
257 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
258 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
261 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
262 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
263 BIT(IEEE80211_RADIOTAP_EXT);
264 put_unaligned_le32(it_present_val, it_present);
266 it_present_val = rtap.present;
269 put_unaligned_le32(it_present_val, it_present);
271 pos = (void *)(it_present + 1);
273 /* the order of the following fields is important */
275 /* IEEE80211_RADIOTAP_TSFT */
276 if (ieee80211_have_rx_timestamp(status)) {
278 while ((pos - (u8 *)rthdr) & 7)
281 ieee80211_calculate_rx_timestamp(local, status,
284 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
288 /* IEEE80211_RADIOTAP_FLAGS */
289 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
290 *pos |= IEEE80211_RADIOTAP_F_FCS;
291 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
292 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
293 if (status->flag & RX_FLAG_SHORTPRE)
294 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
297 /* IEEE80211_RADIOTAP_RATE */
298 if (!rate || status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) {
300 * Without rate information don't add it. If we have,
301 * MCS information is a separate field in radiotap,
302 * added below. The byte here is needed as padding
303 * for the channel though, so initialise it to 0.
308 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
309 if (status->flag & RX_FLAG_10MHZ)
311 else if (status->flag & RX_FLAG_5MHZ)
313 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
317 /* IEEE80211_RADIOTAP_CHANNEL */
318 put_unaligned_le16(status->freq, pos);
320 if (status->flag & RX_FLAG_10MHZ)
321 channel_flags |= IEEE80211_CHAN_HALF;
322 else if (status->flag & RX_FLAG_5MHZ)
323 channel_flags |= IEEE80211_CHAN_QUARTER;
325 if (status->band == NL80211_BAND_5GHZ)
326 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
327 else if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
328 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
329 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
330 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
332 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
334 channel_flags |= IEEE80211_CHAN_2GHZ;
335 put_unaligned_le16(channel_flags, pos);
338 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
339 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
340 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
341 *pos = status->signal;
343 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
347 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
349 if (!status->chains) {
350 /* IEEE80211_RADIOTAP_ANTENNA */
351 *pos = status->antenna;
355 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
357 /* IEEE80211_RADIOTAP_RX_FLAGS */
358 /* ensure 2 byte alignment for the 2 byte field as required */
359 if ((pos - (u8 *)rthdr) & 1)
361 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
362 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
363 put_unaligned_le16(rx_flags, pos);
366 if (status->flag & RX_FLAG_HT) {
369 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
370 *pos++ = local->hw.radiotap_mcs_details;
372 if (status->flag & RX_FLAG_SHORT_GI)
373 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
374 if (status->flag & RX_FLAG_40MHZ)
375 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
376 if (status->flag & RX_FLAG_HT_GF)
377 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
378 if (status->flag & RX_FLAG_LDPC)
379 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
380 stbc = (status->flag & RX_FLAG_STBC_MASK) >> RX_FLAG_STBC_SHIFT;
381 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
383 *pos++ = status->rate_idx;
386 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
389 /* ensure 4 byte alignment */
390 while ((pos - (u8 *)rthdr) & 3)
393 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
394 put_unaligned_le32(status->ampdu_reference, pos);
396 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
397 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
398 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
399 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
400 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
401 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
402 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
403 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
404 put_unaligned_le16(flags, pos);
406 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
407 *pos++ = status->ampdu_delimiter_crc;
413 if (status->flag & RX_FLAG_VHT) {
414 u16 known = local->hw.radiotap_vht_details;
416 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
417 put_unaligned_le16(known, pos);
420 if (status->flag & RX_FLAG_SHORT_GI)
421 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
422 /* in VHT, STBC is binary */
423 if (status->flag & RX_FLAG_STBC_MASK)
424 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
425 if (status->vht_flag & RX_VHT_FLAG_BF)
426 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
429 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
431 else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
433 else if (status->flag & RX_FLAG_40MHZ)
438 *pos = (status->rate_idx << 4) | status->vht_nss;
441 if (status->flag & RX_FLAG_LDPC)
442 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
450 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
451 *pos++ = status->chain_signal[chain];
455 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
456 /* ensure 2 byte alignment for the vendor field as required */
457 if ((pos - (u8 *)rthdr) & 1)
459 *pos++ = rtap.oui[0];
460 *pos++ = rtap.oui[1];
461 *pos++ = rtap.oui[2];
463 put_unaligned_le16(rtap.len, pos);
465 /* align the actual payload as requested */
466 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
468 /* data (and possible padding) already follows */
473 * This function copies a received frame to all monitor interfaces and
474 * returns a cleaned-up SKB that no longer includes the FCS nor the
475 * radiotap header the driver might have added.
477 static struct sk_buff *
478 ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
479 struct ieee80211_rate *rate)
481 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
482 struct ieee80211_sub_if_data *sdata;
483 int rt_hdrlen, needed_headroom;
484 struct sk_buff *skb, *skb2;
485 struct net_device *prev_dev = NULL;
486 int present_fcs_len = 0;
487 unsigned int rtap_vendor_space = 0;
488 struct ieee80211_mgmt *mgmt;
489 struct ieee80211_sub_if_data *monitor_sdata =
490 rcu_dereference(local->monitor_sdata);
492 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
493 struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
495 rtap_vendor_space = sizeof(*rtap) + rtap->len + rtap->pad;
499 * First, we may need to make a copy of the skb because
500 * (1) we need to modify it for radiotap (if not present), and
501 * (2) the other RX handlers will modify the skb we got.
503 * We don't need to, of course, if we aren't going to return
504 * the SKB because it has a bad FCS/PLCP checksum.
507 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
508 present_fcs_len = FCS_LEN;
510 /* ensure hdr->frame_control and vendor radiotap data are in skb head */
511 if (!pskb_may_pull(origskb, 2 + rtap_vendor_space)) {
512 dev_kfree_skb(origskb);
516 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
517 if (should_drop_frame(origskb, present_fcs_len,
518 rtap_vendor_space)) {
519 dev_kfree_skb(origskb);
523 return remove_monitor_info(local, origskb, rtap_vendor_space);
526 /* room for the radiotap header based on driver features */
527 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, origskb);
528 needed_headroom = rt_hdrlen - rtap_vendor_space;
530 if (should_drop_frame(origskb, present_fcs_len, rtap_vendor_space)) {
531 /* only need to expand headroom if necessary */
536 * This shouldn't trigger often because most devices have an
537 * RX header they pull before we get here, and that should
538 * be big enough for our radiotap information. We should
539 * probably export the length to drivers so that we can have
540 * them allocate enough headroom to start with.
542 if (skb_headroom(skb) < needed_headroom &&
543 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
549 * Need to make a copy and possibly remove radiotap header
550 * and FCS from the original.
552 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
554 origskb = remove_monitor_info(local, origskb,
561 /* prepend radiotap information */
562 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
564 skb_reset_mac_header(skb);
565 skb->ip_summed = CHECKSUM_UNNECESSARY;
566 skb->pkt_type = PACKET_OTHERHOST;
567 skb->protocol = htons(ETH_P_802_2);
569 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
570 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
573 if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
576 if (!ieee80211_sdata_running(sdata))
580 skb2 = skb_clone(skb, GFP_ATOMIC);
582 skb2->dev = prev_dev;
583 netif_receive_skb(skb2);
587 prev_dev = sdata->dev;
588 ieee80211_rx_stats(sdata->dev, skb->len);
591 mgmt = (void *)skb->data;
593 skb->len >= IEEE80211_MIN_ACTION_SIZE + 1 + VHT_MUMIMO_GROUPS_DATA_LEN &&
594 ieee80211_is_action(mgmt->frame_control) &&
595 mgmt->u.action.category == WLAN_CATEGORY_VHT &&
596 mgmt->u.action.u.vht_group_notif.action_code == WLAN_VHT_ACTION_GROUPID_MGMT &&
597 is_valid_ether_addr(monitor_sdata->u.mntr.mu_follow_addr) &&
598 ether_addr_equal(mgmt->da, monitor_sdata->u.mntr.mu_follow_addr)) {
599 struct sk_buff *mu_skb = skb_copy(skb, GFP_ATOMIC);
602 mu_skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
603 skb_queue_tail(&monitor_sdata->skb_queue, mu_skb);
604 ieee80211_queue_work(&local->hw, &monitor_sdata->work);
610 netif_receive_skb(skb);
617 static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
619 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
620 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
621 int tid, seqno_idx, security_idx;
623 /* does the frame have a qos control field? */
624 if (ieee80211_is_data_qos(hdr->frame_control)) {
625 u8 *qc = ieee80211_get_qos_ctl(hdr);
626 /* frame has qos control */
627 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
628 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
629 status->rx_flags |= IEEE80211_RX_AMSDU;
635 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
637 * Sequence numbers for management frames, QoS data
638 * frames with a broadcast/multicast address in the
639 * Address 1 field, and all non-QoS data frames sent
640 * by QoS STAs are assigned using an additional single
641 * modulo-4096 counter, [...]
643 * We also use that counter for non-QoS STAs.
645 seqno_idx = IEEE80211_NUM_TIDS;
647 if (ieee80211_is_mgmt(hdr->frame_control))
648 security_idx = IEEE80211_NUM_TIDS;
652 rx->seqno_idx = seqno_idx;
653 rx->security_idx = security_idx;
654 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
655 * For now, set skb->priority to 0 for other cases. */
656 rx->skb->priority = (tid > 7) ? 0 : tid;
660 * DOC: Packet alignment
662 * Drivers always need to pass packets that are aligned to two-byte boundaries
665 * Additionally, should, if possible, align the payload data in a way that
666 * guarantees that the contained IP header is aligned to a four-byte
667 * boundary. In the case of regular frames, this simply means aligning the
668 * payload to a four-byte boundary (because either the IP header is directly
669 * contained, or IV/RFC1042 headers that have a length divisible by four are
670 * in front of it). If the payload data is not properly aligned and the
671 * architecture doesn't support efficient unaligned operations, mac80211
672 * will align the data.
674 * With A-MSDU frames, however, the payload data address must yield two modulo
675 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
676 * push the IP header further back to a multiple of four again. Thankfully, the
677 * specs were sane enough this time around to require padding each A-MSDU
678 * subframe to a length that is a multiple of four.
680 * Padding like Atheros hardware adds which is between the 802.11 header and
681 * the payload is not supported, the driver is required to move the 802.11
682 * header to be directly in front of the payload in that case.
684 static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
686 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
687 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
694 static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
696 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
698 if (is_multicast_ether_addr(hdr->addr1))
701 return ieee80211_is_robust_mgmt_frame(skb);
705 static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
707 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
709 if (!is_multicast_ether_addr(hdr->addr1))
712 return ieee80211_is_robust_mgmt_frame(skb);
716 /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
717 static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
719 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
720 struct ieee80211_mmie *mmie;
721 struct ieee80211_mmie_16 *mmie16;
723 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
726 if (!ieee80211_is_robust_mgmt_frame(skb))
727 return -1; /* not a robust management frame */
729 mmie = (struct ieee80211_mmie *)
730 (skb->data + skb->len - sizeof(*mmie));
731 if (mmie->element_id == WLAN_EID_MMIE &&
732 mmie->length == sizeof(*mmie) - 2)
733 return le16_to_cpu(mmie->key_id);
735 mmie16 = (struct ieee80211_mmie_16 *)
736 (skb->data + skb->len - sizeof(*mmie16));
737 if (skb->len >= 24 + sizeof(*mmie16) &&
738 mmie16->element_id == WLAN_EID_MMIE &&
739 mmie16->length == sizeof(*mmie16) - 2)
740 return le16_to_cpu(mmie16->key_id);
745 static int ieee80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
748 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
753 fc = hdr->frame_control;
754 hdrlen = ieee80211_hdrlen(fc);
756 if (skb->len < hdrlen + cs->hdr_len)
759 skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
760 keyid &= cs->key_idx_mask;
761 keyid >>= cs->key_idx_shift;
766 static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
768 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
769 char *dev_addr = rx->sdata->vif.addr;
771 if (ieee80211_is_data(hdr->frame_control)) {
772 if (is_multicast_ether_addr(hdr->addr1)) {
773 if (ieee80211_has_tods(hdr->frame_control) ||
774 !ieee80211_has_fromds(hdr->frame_control))
775 return RX_DROP_MONITOR;
776 if (ether_addr_equal(hdr->addr3, dev_addr))
777 return RX_DROP_MONITOR;
779 if (!ieee80211_has_a4(hdr->frame_control))
780 return RX_DROP_MONITOR;
781 if (ether_addr_equal(hdr->addr4, dev_addr))
782 return RX_DROP_MONITOR;
786 /* If there is not an established peer link and this is not a peer link
787 * establisment frame, beacon or probe, drop the frame.
790 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
791 struct ieee80211_mgmt *mgmt;
793 if (!ieee80211_is_mgmt(hdr->frame_control))
794 return RX_DROP_MONITOR;
796 if (ieee80211_is_action(hdr->frame_control)) {
799 /* make sure category field is present */
800 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
801 return RX_DROP_MONITOR;
803 mgmt = (struct ieee80211_mgmt *)hdr;
804 category = mgmt->u.action.category;
805 if (category != WLAN_CATEGORY_MESH_ACTION &&
806 category != WLAN_CATEGORY_SELF_PROTECTED)
807 return RX_DROP_MONITOR;
811 if (ieee80211_is_probe_req(hdr->frame_control) ||
812 ieee80211_is_probe_resp(hdr->frame_control) ||
813 ieee80211_is_beacon(hdr->frame_control) ||
814 ieee80211_is_auth(hdr->frame_control))
817 return RX_DROP_MONITOR;
823 static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
826 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
827 struct sk_buff *tail = skb_peek_tail(frames);
828 struct ieee80211_rx_status *status;
830 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
836 status = IEEE80211_SKB_RXCB(tail);
837 if (status->flag & RX_FLAG_AMSDU_MORE)
843 static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
844 struct tid_ampdu_rx *tid_agg_rx,
846 struct sk_buff_head *frames)
848 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
850 struct ieee80211_rx_status *status;
852 lockdep_assert_held(&tid_agg_rx->reorder_lock);
854 if (skb_queue_empty(skb_list))
857 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
858 __skb_queue_purge(skb_list);
862 /* release frames from the reorder ring buffer */
863 tid_agg_rx->stored_mpdu_num--;
864 while ((skb = __skb_dequeue(skb_list))) {
865 status = IEEE80211_SKB_RXCB(skb);
866 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
867 __skb_queue_tail(frames, skb);
871 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
872 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
875 static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
876 struct tid_ampdu_rx *tid_agg_rx,
878 struct sk_buff_head *frames)
882 lockdep_assert_held(&tid_agg_rx->reorder_lock);
884 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
885 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
886 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
892 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
893 * the skb was added to the buffer longer than this time ago, the earlier
894 * frames that have not yet been received are assumed to be lost and the skb
895 * can be released for processing. This may also release other skb's from the
896 * reorder buffer if there are no additional gaps between the frames.
898 * Callers must hold tid_agg_rx->reorder_lock.
900 #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
902 static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
903 struct tid_ampdu_rx *tid_agg_rx,
904 struct sk_buff_head *frames)
908 lockdep_assert_held(&tid_agg_rx->reorder_lock);
910 /* release the buffer until next missing frame */
911 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
912 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
913 tid_agg_rx->stored_mpdu_num) {
915 * No buffers ready to be released, but check whether any
916 * frames in the reorder buffer have timed out.
919 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
920 j = (j + 1) % tid_agg_rx->buf_size) {
921 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
926 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
927 HT_RX_REORDER_BUF_TIMEOUT))
928 goto set_release_timer;
930 /* don't leave incomplete A-MSDUs around */
931 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
932 i = (i + 1) % tid_agg_rx->buf_size)
933 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
935 ht_dbg_ratelimited(sdata,
936 "release an RX reorder frame due to timeout on earlier frames\n");
937 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
941 * Increment the head seq# also for the skipped slots.
943 tid_agg_rx->head_seq_num =
944 (tid_agg_rx->head_seq_num +
945 skipped) & IEEE80211_SN_MASK;
948 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
949 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
951 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
954 if (tid_agg_rx->stored_mpdu_num) {
955 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
957 for (; j != (index - 1) % tid_agg_rx->buf_size;
958 j = (j + 1) % tid_agg_rx->buf_size) {
959 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
965 if (!tid_agg_rx->removed)
966 mod_timer(&tid_agg_rx->reorder_timer,
967 tid_agg_rx->reorder_time[j] + 1 +
968 HT_RX_REORDER_BUF_TIMEOUT);
970 del_timer(&tid_agg_rx->reorder_timer);
975 * As this function belongs to the RX path it must be under
976 * rcu_read_lock protection. It returns false if the frame
977 * can be processed immediately, true if it was consumed.
979 static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
980 struct tid_ampdu_rx *tid_agg_rx,
982 struct sk_buff_head *frames)
984 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
985 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
986 u16 sc = le16_to_cpu(hdr->seq_ctrl);
987 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
988 u16 head_seq_num, buf_size;
992 spin_lock(&tid_agg_rx->reorder_lock);
995 * Offloaded BA sessions have no known starting sequence number so pick
996 * one from first Rxed frame for this tid after BA was started.
998 if (unlikely(tid_agg_rx->auto_seq)) {
999 tid_agg_rx->auto_seq = false;
1000 tid_agg_rx->ssn = mpdu_seq_num;
1001 tid_agg_rx->head_seq_num = mpdu_seq_num;
1004 buf_size = tid_agg_rx->buf_size;
1005 head_seq_num = tid_agg_rx->head_seq_num;
1007 /* frame with out of date sequence number */
1008 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1014 * If frame the sequence number exceeds our buffering window
1015 * size release some previous frames to make room for this one.
1017 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1018 head_seq_num = ieee80211_sn_inc(
1019 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1020 /* release stored frames up to new head to stack */
1021 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
1022 head_seq_num, frames);
1025 /* Now the new frame is always in the range of the reordering buffer */
1027 index = mpdu_seq_num % tid_agg_rx->buf_size;
1029 /* check if we already stored this frame */
1030 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1036 * If the current MPDU is in the right order and nothing else
1037 * is stored we can process it directly, no need to buffer it.
1038 * If it is first but there's something stored, we may be able
1039 * to release frames after this one.
1041 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1042 tid_agg_rx->stored_mpdu_num == 0) {
1043 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1044 tid_agg_rx->head_seq_num =
1045 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1050 /* put the frame in the reordering buffer */
1051 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1052 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1053 tid_agg_rx->reorder_time[index] = jiffies;
1054 tid_agg_rx->stored_mpdu_num++;
1055 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1059 spin_unlock(&tid_agg_rx->reorder_lock);
1064 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1065 * true if the MPDU was buffered, false if it should be processed.
1067 static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1068 struct sk_buff_head *frames)
1070 struct sk_buff *skb = rx->skb;
1071 struct ieee80211_local *local = rx->local;
1072 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1073 struct sta_info *sta = rx->sta;
1074 struct tid_ampdu_rx *tid_agg_rx;
1078 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1079 is_multicast_ether_addr(hdr->addr1))
1083 * filter the QoS data rx stream according to
1084 * STA/TID and check if this STA/TID is on aggregation
1090 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1091 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1092 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1094 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1098 /* qos null data frames are excluded */
1099 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1102 /* not part of a BA session */
1103 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1104 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1107 /* new, potentially un-ordered, ampdu frame - process it */
1109 /* reset session timer */
1110 if (tid_agg_rx->timeout)
1111 tid_agg_rx->last_rx = jiffies;
1113 /* if this mpdu is fragmented - terminate rx aggregation session */
1114 sc = le16_to_cpu(hdr->seq_ctrl);
1115 if (sc & IEEE80211_SCTL_FRAG) {
1116 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
1117 skb_queue_tail(&rx->sdata->skb_queue, skb);
1118 ieee80211_queue_work(&local->hw, &rx->sdata->work);
1123 * No locking needed -- we will only ever process one
1124 * RX packet at a time, and thus own tid_agg_rx. All
1125 * other code manipulating it needs to (and does) make
1126 * sure that we cannot get to it any more before doing
1129 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1134 __skb_queue_tail(frames, skb);
1137 static ieee80211_rx_result debug_noinline
1138 ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1140 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1141 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1143 if (status->flag & RX_FLAG_DUP_VALIDATED)
1147 * Drop duplicate 802.11 retransmissions
1148 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1151 if (rx->skb->len < 24)
1154 if (ieee80211_is_ctl(hdr->frame_control) ||
1155 ieee80211_is_qos_nullfunc(hdr->frame_control) ||
1156 is_multicast_ether_addr(hdr->addr1))
1162 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1163 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1164 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1165 rx->sta->rx_stats.num_duplicates++;
1166 return RX_DROP_UNUSABLE;
1167 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1168 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1174 static ieee80211_rx_result debug_noinline
1175 ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1177 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1179 /* Drop disallowed frame classes based on STA auth/assoc state;
1180 * IEEE 802.11, Chap 5.5.
1182 * mac80211 filters only based on association state, i.e. it drops
1183 * Class 3 frames from not associated stations. hostapd sends
1184 * deauth/disassoc frames when needed. In addition, hostapd is
1185 * responsible for filtering on both auth and assoc states.
1188 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1189 return ieee80211_rx_mesh_check(rx);
1191 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1192 ieee80211_is_pspoll(hdr->frame_control)) &&
1193 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1194 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
1195 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1196 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1198 * accept port control frames from the AP even when it's not
1199 * yet marked ASSOC to prevent a race where we don't set the
1200 * assoc bit quickly enough before it sends the first frame
1202 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1203 ieee80211_is_data_present(hdr->frame_control)) {
1204 unsigned int hdrlen;
1207 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1209 if (rx->skb->len < hdrlen + 8)
1210 return RX_DROP_MONITOR;
1212 skb_copy_bits(rx->skb, hdrlen + 6, ðertype, 2);
1213 if (ethertype == rx->sdata->control_port_protocol)
1217 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1218 cfg80211_rx_spurious_frame(rx->sdata->dev,
1221 return RX_DROP_UNUSABLE;
1223 return RX_DROP_MONITOR;
1230 static ieee80211_rx_result debug_noinline
1231 ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1233 struct ieee80211_local *local;
1234 struct ieee80211_hdr *hdr;
1235 struct sk_buff *skb;
1239 hdr = (struct ieee80211_hdr *) skb->data;
1241 if (!local->pspolling)
1244 if (!ieee80211_has_fromds(hdr->frame_control))
1245 /* this is not from AP */
1248 if (!ieee80211_is_data(hdr->frame_control))
1251 if (!ieee80211_has_moredata(hdr->frame_control)) {
1252 /* AP has no more frames buffered for us */
1253 local->pspolling = false;
1257 /* more data bit is set, let's request a new frame from the AP */
1258 ieee80211_send_pspoll(local, rx->sdata);
1263 static void sta_ps_start(struct sta_info *sta)
1265 struct ieee80211_sub_if_data *sdata = sta->sdata;
1266 struct ieee80211_local *local = sdata->local;
1270 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1271 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1272 ps = &sdata->bss->ps;
1276 atomic_inc(&ps->num_sta_ps);
1277 set_sta_flag(sta, WLAN_STA_PS_STA);
1278 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1279 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1280 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1281 sta->sta.addr, sta->sta.aid);
1283 ieee80211_clear_fast_xmit(sta);
1285 if (!sta->sta.txq[0])
1288 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1289 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1291 if (txqi->tin.backlog_packets)
1292 set_bit(tid, &sta->txq_buffered_tids);
1294 clear_bit(tid, &sta->txq_buffered_tids);
1298 static void sta_ps_end(struct sta_info *sta)
1300 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1301 sta->sta.addr, sta->sta.aid);
1303 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1305 * Clear the flag only if the other one is still set
1306 * so that the TX path won't start TX'ing new frames
1307 * directly ... In the case that the driver flag isn't
1308 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1310 clear_sta_flag(sta, WLAN_STA_PS_STA);
1311 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1312 sta->sta.addr, sta->sta.aid);
1316 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1317 clear_sta_flag(sta, WLAN_STA_PS_STA);
1318 ieee80211_sta_ps_deliver_wakeup(sta);
1321 int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1323 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1326 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1328 /* Don't let the same PS state be set twice */
1329 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1330 if ((start && in_ps) || (!start && !in_ps))
1340 EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1342 void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1344 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1346 if (test_sta_flag(sta, WLAN_STA_SP))
1349 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1350 ieee80211_sta_ps_deliver_poll_response(sta);
1352 set_sta_flag(sta, WLAN_STA_PSPOLL);
1354 EXPORT_SYMBOL(ieee80211_sta_pspoll);
1356 void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1358 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1359 u8 ac = ieee802_1d_to_ac[tid & 7];
1362 * If this AC is not trigger-enabled do nothing.
1364 * NB: This could/should check a separate bitmap of trigger-
1365 * enabled queues, but for now we only implement uAPSD w/o
1366 * TSPEC changes to the ACs, so they're always the same.
1368 if (!(sta->sta.uapsd_queues & BIT(ac)))
1371 /* if we are in a service period, do nothing */
1372 if (test_sta_flag(sta, WLAN_STA_SP))
1375 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1376 ieee80211_sta_ps_deliver_uapsd(sta);
1378 set_sta_flag(sta, WLAN_STA_UAPSD);
1380 EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1382 static ieee80211_rx_result debug_noinline
1383 ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1385 struct ieee80211_sub_if_data *sdata = rx->sdata;
1386 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1387 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1392 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1393 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1397 * The device handles station powersave, so don't do anything about
1398 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1399 * it to mac80211 since they're handled.)
1401 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1405 * Don't do anything if the station isn't already asleep. In
1406 * the uAPSD case, the station will probably be marked asleep,
1407 * in the PS-Poll case the station must be confused ...
1409 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1412 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1413 ieee80211_sta_pspoll(&rx->sta->sta);
1415 /* Free PS Poll skb here instead of returning RX_DROP that would
1416 * count as an dropped frame. */
1417 dev_kfree_skb(rx->skb);
1420 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1421 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1422 ieee80211_has_pm(hdr->frame_control) &&
1423 (ieee80211_is_data_qos(hdr->frame_control) ||
1424 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1427 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1429 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1435 static ieee80211_rx_result debug_noinline
1436 ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1438 struct sta_info *sta = rx->sta;
1439 struct sk_buff *skb = rx->skb;
1440 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1441 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1448 * Update last_rx only for IBSS packets which are for the current
1449 * BSSID and for station already AUTHORIZED to avoid keeping the
1450 * current IBSS network alive in cases where other STAs start
1451 * using different BSSID. This will also give the station another
1452 * chance to restart the authentication/authorization in case
1453 * something went wrong the first time.
1455 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1456 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1457 NL80211_IFTYPE_ADHOC);
1458 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1459 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1460 sta->rx_stats.last_rx = jiffies;
1461 if (ieee80211_is_data(hdr->frame_control) &&
1462 !is_multicast_ether_addr(hdr->addr1))
1463 sta->rx_stats.last_rate =
1464 sta_stats_encode_rate(status);
1466 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1467 sta->rx_stats.last_rx = jiffies;
1468 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1470 * Mesh beacons will update last_rx when if they are found to
1471 * match the current local configuration when processed.
1473 sta->rx_stats.last_rx = jiffies;
1474 if (ieee80211_is_data(hdr->frame_control))
1475 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1478 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1479 ieee80211_sta_rx_notify(rx->sdata, hdr);
1481 sta->rx_stats.fragments++;
1483 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1484 sta->rx_stats.bytes += rx->skb->len;
1485 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1487 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1488 sta->rx_stats.last_signal = status->signal;
1489 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1492 if (status->chains) {
1493 sta->rx_stats.chains = status->chains;
1494 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1495 int signal = status->chain_signal[i];
1497 if (!(status->chains & BIT(i)))
1500 sta->rx_stats.chain_signal_last[i] = signal;
1501 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1507 * Change STA power saving mode only at the end of a frame
1508 * exchange sequence.
1510 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1511 !ieee80211_has_morefrags(hdr->frame_control) &&
1512 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1513 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1514 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1515 /* PM bit is only checked in frames where it isn't reserved,
1516 * in AP mode it's reserved in non-bufferable management frames
1517 * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1519 (!ieee80211_is_mgmt(hdr->frame_control) ||
1520 ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
1521 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1522 if (!ieee80211_has_pm(hdr->frame_control))
1525 if (ieee80211_has_pm(hdr->frame_control))
1530 /* mesh power save support */
1531 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1532 ieee80211_mps_rx_h_sta_process(sta, hdr);
1535 * Drop (qos-)data::nullfunc frames silently, since they
1536 * are used only to control station power saving mode.
1538 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1539 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1540 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1543 * If we receive a 4-addr nullfunc frame from a STA
1544 * that was not moved to a 4-addr STA vlan yet send
1545 * the event to userspace and for older hostapd drop
1546 * the frame to the monitor interface.
1548 if (ieee80211_has_a4(hdr->frame_control) &&
1549 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1550 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1551 !rx->sdata->u.vlan.sta))) {
1552 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1553 cfg80211_rx_unexpected_4addr_frame(
1554 rx->sdata->dev, sta->sta.addr,
1556 return RX_DROP_MONITOR;
1559 * Update counter and free packet here to avoid
1560 * counting this as a dropped packed.
1562 sta->rx_stats.packets++;
1563 dev_kfree_skb(rx->skb);
1568 } /* ieee80211_rx_h_sta_process */
1570 static ieee80211_rx_result debug_noinline
1571 ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1573 struct sk_buff *skb = rx->skb;
1574 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1575 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1578 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1579 struct ieee80211_key *sta_ptk = NULL;
1580 int mmie_keyidx = -1;
1582 const struct ieee80211_cipher_scheme *cs = NULL;
1587 * There are four types of keys:
1588 * - GTK (group keys)
1589 * - IGTK (group keys for management frames)
1590 * - PTK (pairwise keys)
1591 * - STK (station-to-station pairwise keys)
1593 * When selecting a key, we have to distinguish between multicast
1594 * (including broadcast) and unicast frames, the latter can only
1595 * use PTKs and STKs while the former always use GTKs and IGTKs.
1596 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1597 * unicast frames can also use key indices like GTKs. Hence, if we
1598 * don't have a PTK/STK we check the key index for a WEP key.
1600 * Note that in a regular BSS, multicast frames are sent by the
1601 * AP only, associated stations unicast the frame to the AP first
1602 * which then multicasts it on their behalf.
1604 * There is also a slight problem in IBSS mode: GTKs are negotiated
1605 * with each station, that is something we don't currently handle.
1606 * The spec seems to expect that one negotiates the same key with
1607 * every station but there's no such requirement; VLANs could be
1611 /* start without a key */
1613 fc = hdr->frame_control;
1616 int keyid = rx->sta->ptk_idx;
1618 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1619 cs = rx->sta->cipher_scheme;
1620 keyid = ieee80211_get_cs_keyid(cs, rx->skb);
1621 if (unlikely(keyid < 0))
1622 return RX_DROP_UNUSABLE;
1624 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1627 if (!ieee80211_has_protected(fc))
1628 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1630 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1632 if ((status->flag & RX_FLAG_DECRYPTED) &&
1633 (status->flag & RX_FLAG_IV_STRIPPED))
1635 /* Skip decryption if the frame is not protected. */
1636 if (!ieee80211_has_protected(fc))
1638 } else if (mmie_keyidx >= 0) {
1639 /* Broadcast/multicast robust management frame / BIP */
1640 if ((status->flag & RX_FLAG_DECRYPTED) &&
1641 (status->flag & RX_FLAG_IV_STRIPPED))
1644 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1645 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1646 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1648 if (ieee80211_is_group_privacy_action(skb) &&
1649 test_sta_flag(rx->sta, WLAN_STA_MFP))
1650 return RX_DROP_MONITOR;
1652 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1655 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1656 } else if (!ieee80211_has_protected(fc)) {
1658 * The frame was not protected, so skip decryption. However, we
1659 * need to set rx->key if there is a key that could have been
1660 * used so that the frame may be dropped if encryption would
1661 * have been expected.
1663 struct ieee80211_key *key = NULL;
1664 struct ieee80211_sub_if_data *sdata = rx->sdata;
1667 if (ieee80211_is_mgmt(fc) &&
1668 is_multicast_ether_addr(hdr->addr1) &&
1669 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1673 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1674 key = rcu_dereference(rx->sta->gtk[i]);
1680 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1681 key = rcu_dereference(sdata->keys[i]);
1694 * The device doesn't give us the IV so we won't be
1695 * able to look up the key. That's ok though, we
1696 * don't need to decrypt the frame, we just won't
1697 * be able to keep statistics accurate.
1698 * Except for key threshold notifications, should
1699 * we somehow allow the driver to tell us which key
1700 * the hardware used if this flag is set?
1702 if ((status->flag & RX_FLAG_DECRYPTED) &&
1703 (status->flag & RX_FLAG_IV_STRIPPED))
1706 hdrlen = ieee80211_hdrlen(fc);
1709 keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
1711 if (unlikely(keyidx < 0))
1712 return RX_DROP_UNUSABLE;
1714 if (rx->skb->len < 8 + hdrlen)
1715 return RX_DROP_UNUSABLE; /* TODO: count this? */
1717 * no need to call ieee80211_wep_get_keyidx,
1718 * it verifies a bunch of things we've done already
1720 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1721 keyidx = keyid >> 6;
1724 /* check per-station GTK first, if multicast packet */
1725 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1726 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1728 /* if not found, try default key */
1730 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1733 * RSNA-protected unicast frames should always be
1734 * sent with pairwise or station-to-station keys,
1735 * but for WEP we allow using a key index as well.
1738 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1739 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1740 !is_multicast_ether_addr(hdr->addr1))
1746 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1747 return RX_DROP_MONITOR;
1749 /* TODO: add threshold stuff again */
1751 return RX_DROP_MONITOR;
1754 switch (rx->key->conf.cipher) {
1755 case WLAN_CIPHER_SUITE_WEP40:
1756 case WLAN_CIPHER_SUITE_WEP104:
1757 result = ieee80211_crypto_wep_decrypt(rx);
1759 case WLAN_CIPHER_SUITE_TKIP:
1760 result = ieee80211_crypto_tkip_decrypt(rx);
1762 case WLAN_CIPHER_SUITE_CCMP:
1763 result = ieee80211_crypto_ccmp_decrypt(
1764 rx, IEEE80211_CCMP_MIC_LEN);
1766 case WLAN_CIPHER_SUITE_CCMP_256:
1767 result = ieee80211_crypto_ccmp_decrypt(
1768 rx, IEEE80211_CCMP_256_MIC_LEN);
1770 case WLAN_CIPHER_SUITE_AES_CMAC:
1771 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1773 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1774 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1776 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1777 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1778 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1780 case WLAN_CIPHER_SUITE_GCMP:
1781 case WLAN_CIPHER_SUITE_GCMP_256:
1782 result = ieee80211_crypto_gcmp_decrypt(rx);
1785 result = ieee80211_crypto_hw_decrypt(rx);
1788 /* the hdr variable is invalid after the decrypt handlers */
1790 /* either the frame has been decrypted or will be dropped */
1791 status->flag |= RX_FLAG_DECRYPTED;
1796 static inline struct ieee80211_fragment_entry *
1797 ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1798 unsigned int frag, unsigned int seq, int rx_queue,
1799 struct sk_buff **skb)
1801 struct ieee80211_fragment_entry *entry;
1803 entry = &sdata->fragments[sdata->fragment_next++];
1804 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1805 sdata->fragment_next = 0;
1807 if (!skb_queue_empty(&entry->skb_list))
1808 __skb_queue_purge(&entry->skb_list);
1810 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1812 entry->first_frag_time = jiffies;
1814 entry->rx_queue = rx_queue;
1815 entry->last_frag = frag;
1816 entry->check_sequential_pn = false;
1817 entry->extra_len = 0;
1822 static inline struct ieee80211_fragment_entry *
1823 ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
1824 unsigned int frag, unsigned int seq,
1825 int rx_queue, struct ieee80211_hdr *hdr)
1827 struct ieee80211_fragment_entry *entry;
1830 idx = sdata->fragment_next;
1831 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1832 struct ieee80211_hdr *f_hdr;
1836 idx = IEEE80211_FRAGMENT_MAX - 1;
1838 entry = &sdata->fragments[idx];
1839 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1840 entry->rx_queue != rx_queue ||
1841 entry->last_frag + 1 != frag)
1844 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1847 * Check ftype and addresses are equal, else check next fragment
1849 if (((hdr->frame_control ^ f_hdr->frame_control) &
1850 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1851 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1852 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
1855 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1856 __skb_queue_purge(&entry->skb_list);
1865 static ieee80211_rx_result debug_noinline
1866 ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1868 struct ieee80211_hdr *hdr;
1871 unsigned int frag, seq;
1872 struct ieee80211_fragment_entry *entry;
1873 struct sk_buff *skb;
1874 struct ieee80211_rx_status *status;
1876 hdr = (struct ieee80211_hdr *)rx->skb->data;
1877 fc = hdr->frame_control;
1879 if (ieee80211_is_ctl(fc))
1882 sc = le16_to_cpu(hdr->seq_ctrl);
1883 frag = sc & IEEE80211_SCTL_FRAG;
1885 if (is_multicast_ether_addr(hdr->addr1)) {
1886 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
1890 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1893 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1895 if (skb_linearize(rx->skb))
1896 return RX_DROP_UNUSABLE;
1899 * skb_linearize() might change the skb->data and
1900 * previously cached variables (in this case, hdr) need to
1901 * be refreshed with the new data.
1903 hdr = (struct ieee80211_hdr *)rx->skb->data;
1904 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1907 /* This is the first fragment of a new frame. */
1908 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
1909 rx->seqno_idx, &(rx->skb));
1911 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
1912 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
1913 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
1914 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
1915 ieee80211_has_protected(fc)) {
1916 int queue = rx->security_idx;
1918 /* Store CCMP/GCMP PN so that we can verify that the
1919 * next fragment has a sequential PN value.
1921 entry->check_sequential_pn = true;
1922 memcpy(entry->last_pn,
1923 rx->key->u.ccmp.rx_pn[queue],
1924 IEEE80211_CCMP_PN_LEN);
1925 BUILD_BUG_ON(offsetof(struct ieee80211_key,
1927 offsetof(struct ieee80211_key,
1929 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
1930 sizeof(rx->key->u.gcmp.rx_pn[queue]));
1931 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
1932 IEEE80211_GCMP_PN_LEN);
1937 /* This is a fragment for a frame that should already be pending in
1938 * fragment cache. Add this fragment to the end of the pending entry.
1940 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1941 rx->seqno_idx, hdr);
1943 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1944 return RX_DROP_MONITOR;
1947 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
1948 * MPDU PN values are not incrementing in steps of 1."
1949 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
1950 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
1952 if (entry->check_sequential_pn) {
1954 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
1958 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
1959 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
1960 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
1961 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
1962 return RX_DROP_UNUSABLE;
1963 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
1964 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
1969 queue = rx->security_idx;
1970 rpn = rx->key->u.ccmp.rx_pn[queue];
1971 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
1972 return RX_DROP_UNUSABLE;
1973 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
1976 skb_pull(rx->skb, ieee80211_hdrlen(fc));
1977 __skb_queue_tail(&entry->skb_list, rx->skb);
1978 entry->last_frag = frag;
1979 entry->extra_len += rx->skb->len;
1980 if (ieee80211_has_morefrags(fc)) {
1985 rx->skb = __skb_dequeue(&entry->skb_list);
1986 if (skb_tailroom(rx->skb) < entry->extra_len) {
1987 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
1988 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1990 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1991 __skb_queue_purge(&entry->skb_list);
1992 return RX_DROP_UNUSABLE;
1995 while ((skb = __skb_dequeue(&entry->skb_list))) {
1996 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
2000 /* Complete frame has been reassembled - process it now */
2001 status = IEEE80211_SKB_RXCB(rx->skb);
2004 ieee80211_led_rx(rx->local);
2007 rx->sta->rx_stats.packets++;
2011 static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2013 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2019 static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2021 struct sk_buff *skb = rx->skb;
2022 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2025 * Pass through unencrypted frames if the hardware has
2026 * decrypted them already.
2028 if (status->flag & RX_FLAG_DECRYPTED)
2031 /* Drop unencrypted frames if key is set. */
2032 if (unlikely(!ieee80211_has_protected(fc) &&
2033 !ieee80211_is_nullfunc(fc) &&
2034 ieee80211_is_data(fc) && rx->key))
2040 static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2042 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2043 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2044 __le16 fc = hdr->frame_control;
2047 * Pass through unencrypted frames if the hardware has
2048 * decrypted them already.
2050 if (status->flag & RX_FLAG_DECRYPTED)
2053 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2054 if (unlikely(!ieee80211_has_protected(fc) &&
2055 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2057 if (ieee80211_is_deauth(fc) ||
2058 ieee80211_is_disassoc(fc))
2059 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2064 /* BIP does not use Protected field, so need to check MMIE */
2065 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2066 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2067 if (ieee80211_is_deauth(fc) ||
2068 ieee80211_is_disassoc(fc))
2069 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2075 * When using MFP, Action frames are not allowed prior to
2076 * having configured keys.
2078 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2079 ieee80211_is_robust_mgmt_frame(rx->skb)))
2087 __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2089 struct ieee80211_sub_if_data *sdata = rx->sdata;
2090 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2091 bool check_port_control = false;
2092 struct ethhdr *ehdr;
2095 *port_control = false;
2096 if (ieee80211_has_a4(hdr->frame_control) &&
2097 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2100 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2101 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2103 if (!sdata->u.mgd.use_4addr)
2106 check_port_control = true;
2109 if (is_multicast_ether_addr(hdr->addr1) &&
2110 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2113 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2117 ehdr = (struct ethhdr *) rx->skb->data;
2118 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2119 *port_control = true;
2120 else if (check_port_control)
2127 * requires that rx->skb is a frame with ethernet header
2129 static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2131 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2132 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2133 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2136 * Allow EAPOL frames to us/the PAE group address regardless
2137 * of whether the frame was encrypted or not.
2139 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
2140 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2141 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
2144 if (ieee80211_802_1x_port_control(rx) ||
2145 ieee80211_drop_unencrypted(rx, fc))
2152 * requires that rx->skb is a frame with ethernet header
2155 ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2157 struct ieee80211_sub_if_data *sdata = rx->sdata;
2158 struct net_device *dev = sdata->dev;
2159 struct sk_buff *skb, *xmit_skb;
2160 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2161 struct sta_info *dsta;
2166 ieee80211_rx_stats(dev, skb->len);
2169 /* The seqno index has the same property as needed
2170 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2171 * for non-QoS-data frames. Here we know it's a data
2172 * frame, so count MSDUs.
2174 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2175 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2176 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2179 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2180 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2181 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2182 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2183 if (is_multicast_ether_addr(ehdr->h_dest)) {
2185 * send multicast frames both to higher layers in
2186 * local net stack and back to the wireless medium
2188 xmit_skb = skb_copy(skb, GFP_ATOMIC);
2190 net_info_ratelimited("%s: failed to clone multicast frame\n",
2193 dsta = sta_info_get(sdata, skb->data);
2196 * The destination station is associated to
2197 * this AP (in this VLAN), so send the frame
2198 * directly to it and do not pass it to local
2207 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2209 /* 'align' will only take the values 0 or 2 here since all
2210 * frames are required to be aligned to 2-byte boundaries
2211 * when being passed to mac80211; the code here works just
2212 * as well if that isn't true, but mac80211 assumes it can
2213 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2217 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2219 if (WARN_ON(skb_headroom(skb) < 3)) {
2223 u8 *data = skb->data;
2224 size_t len = skb_headlen(skb);
2226 memmove(skb->data, data, len);
2227 skb_set_tail_pointer(skb, len);
2234 /* deliver to local stack */
2235 skb->protocol = eth_type_trans(skb, dev);
2236 memset(skb->cb, 0, sizeof(skb->cb));
2238 napi_gro_receive(rx->napi, skb);
2240 netif_receive_skb(skb);
2245 * Send to wireless media and increase priority by 256 to
2246 * keep the received priority instead of reclassifying
2247 * the frame (see cfg80211_classify8021d).
2249 xmit_skb->priority += 256;
2250 xmit_skb->protocol = htons(ETH_P_802_3);
2251 skb_reset_network_header(xmit_skb);
2252 skb_reset_mac_header(xmit_skb);
2253 dev_queue_xmit(xmit_skb);
2257 static ieee80211_rx_result debug_noinline
2258 ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2260 struct net_device *dev = rx->sdata->dev;
2261 struct sk_buff *skb = rx->skb;
2262 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2263 __le16 fc = hdr->frame_control;
2264 struct sk_buff_head frame_list;
2265 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2267 if (unlikely(!ieee80211_is_data(fc)))
2270 if (unlikely(!ieee80211_is_data_present(fc)))
2271 return RX_DROP_MONITOR;
2273 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2276 if (ieee80211_has_a4(hdr->frame_control) &&
2277 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2278 !rx->sdata->u.vlan.sta)
2279 return RX_DROP_UNUSABLE;
2281 if (is_multicast_ether_addr(hdr->addr1) &&
2282 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2283 rx->sdata->u.vlan.sta) ||
2284 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2285 rx->sdata->u.mgd.use_4addr)))
2286 return RX_DROP_UNUSABLE;
2289 __skb_queue_head_init(&frame_list);
2291 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2292 rx->sdata->vif.type,
2293 rx->local->hw.extra_tx_headroom, true);
2295 while (!skb_queue_empty(&frame_list)) {
2296 rx->skb = __skb_dequeue(&frame_list);
2298 if (!ieee80211_frame_allowed(rx, fc)) {
2299 dev_kfree_skb(rx->skb);
2303 ieee80211_deliver_skb(rx);
2309 #ifdef CONFIG_MAC80211_MESH
2310 static ieee80211_rx_result
2311 ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2313 struct ieee80211_hdr *fwd_hdr, *hdr;
2314 struct ieee80211_tx_info *info;
2315 struct ieee80211s_hdr *mesh_hdr;
2316 struct sk_buff *skb = rx->skb, *fwd_skb;
2317 struct ieee80211_local *local = rx->local;
2318 struct ieee80211_sub_if_data *sdata = rx->sdata;
2319 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2322 hdr = (struct ieee80211_hdr *) skb->data;
2323 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2325 /* make sure fixed part of mesh header is there, also checks skb len */
2326 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2327 return RX_DROP_MONITOR;
2329 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2331 /* make sure full mesh header is there, also checks skb len */
2332 if (!pskb_may_pull(rx->skb,
2333 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2334 return RX_DROP_MONITOR;
2336 /* reload pointers */
2337 hdr = (struct ieee80211_hdr *) skb->data;
2338 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2340 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2341 return RX_DROP_MONITOR;
2343 /* frame is in RMC, don't forward */
2344 if (ieee80211_is_data(hdr->frame_control) &&
2345 is_multicast_ether_addr(hdr->addr1) &&
2346 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2347 return RX_DROP_MONITOR;
2349 if (!ieee80211_is_data(hdr->frame_control))
2353 return RX_DROP_MONITOR;
2355 if (mesh_hdr->flags & MESH_FLAGS_AE) {
2356 struct mesh_path *mppath;
2360 if (is_multicast_ether_addr(hdr->addr1)) {
2361 mpp_addr = hdr->addr3;
2362 proxied_addr = mesh_hdr->eaddr1;
2363 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
2364 /* has_a4 already checked in ieee80211_rx_mesh_check */
2365 mpp_addr = hdr->addr4;
2366 proxied_addr = mesh_hdr->eaddr2;
2368 return RX_DROP_MONITOR;
2372 mppath = mpp_path_lookup(sdata, proxied_addr);
2374 mpp_path_add(sdata, proxied_addr, mpp_addr);
2376 spin_lock_bh(&mppath->state_lock);
2377 if (!ether_addr_equal(mppath->mpp, mpp_addr))
2378 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2379 mppath->exp_time = jiffies;
2380 spin_unlock_bh(&mppath->state_lock);
2385 /* Frame has reached destination. Don't forward */
2386 if (!is_multicast_ether_addr(hdr->addr1) &&
2387 ether_addr_equal(sdata->vif.addr, hdr->addr3))
2390 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2391 q = sdata->vif.hw_queue[ac];
2392 if (ieee80211_queue_stopped(&local->hw, q)) {
2393 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2394 return RX_DROP_MONITOR;
2396 skb_set_queue_mapping(skb, q);
2398 if (!--mesh_hdr->ttl) {
2399 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
2403 if (!ifmsh->mshcfg.dot11MeshForwarding)
2406 fwd_skb = skb_copy(skb, GFP_ATOMIC);
2408 net_info_ratelimited("%s: failed to clone mesh frame\n",
2413 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
2414 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2415 info = IEEE80211_SKB_CB(fwd_skb);
2416 memset(info, 0, sizeof(*info));
2417 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2418 info->control.vif = &rx->sdata->vif;
2419 info->control.jiffies = jiffies;
2420 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2421 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2422 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2423 /* update power mode indication when forwarding */
2424 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2425 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2426 /* mesh power mode flags updated in mesh_nexthop_lookup */
2427 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2429 /* unable to resolve next hop */
2430 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2432 WLAN_REASON_MESH_PATH_NOFORWARD,
2434 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2436 return RX_DROP_MONITOR;
2439 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2440 ieee80211_add_pending_skb(local, fwd_skb);
2442 if (is_multicast_ether_addr(hdr->addr1))
2444 return RX_DROP_MONITOR;
2448 static ieee80211_rx_result debug_noinline
2449 ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2451 struct ieee80211_sub_if_data *sdata = rx->sdata;
2452 struct ieee80211_local *local = rx->local;
2453 struct net_device *dev = sdata->dev;
2454 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2455 __le16 fc = hdr->frame_control;
2459 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2462 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2463 return RX_DROP_MONITOR;
2466 * Send unexpected-4addr-frame event to hostapd. For older versions,
2467 * also drop the frame to cooked monitor interfaces.
2469 if (ieee80211_has_a4(hdr->frame_control) &&
2470 sdata->vif.type == NL80211_IFTYPE_AP) {
2472 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2473 cfg80211_rx_unexpected_4addr_frame(
2474 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
2475 return RX_DROP_MONITOR;
2478 err = __ieee80211_data_to_8023(rx, &port_control);
2480 return RX_DROP_UNUSABLE;
2482 if (!ieee80211_frame_allowed(rx, fc))
2483 return RX_DROP_MONITOR;
2485 /* directly handle TDLS channel switch requests/responses */
2486 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2487 cpu_to_be16(ETH_P_TDLS))) {
2488 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2490 if (pskb_may_pull(rx->skb,
2491 offsetof(struct ieee80211_tdls_data, u)) &&
2492 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2493 tf->category == WLAN_CATEGORY_TDLS &&
2494 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2495 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2496 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2497 schedule_work(&local->tdls_chsw_work);
2499 rx->sta->rx_stats.packets++;
2505 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2506 unlikely(port_control) && sdata->bss) {
2507 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2515 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2516 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2517 !is_multicast_ether_addr(
2518 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2519 (!local->scanning &&
2520 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2521 mod_timer(&local->dynamic_ps_timer, jiffies +
2522 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2524 ieee80211_deliver_skb(rx);
2529 static ieee80211_rx_result debug_noinline
2530 ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
2532 struct sk_buff *skb = rx->skb;
2533 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2534 struct tid_ampdu_rx *tid_agg_rx;
2538 if (likely(!ieee80211_is_ctl(bar->frame_control)))
2541 if (ieee80211_is_back_req(bar->frame_control)) {
2543 __le16 control, start_seq_num;
2544 } __packed bar_data;
2545 struct ieee80211_event event = {
2546 .type = BAR_RX_EVENT,
2550 return RX_DROP_MONITOR;
2552 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2553 &bar_data, sizeof(bar_data)))
2554 return RX_DROP_MONITOR;
2556 tid = le16_to_cpu(bar_data.control) >> 12;
2558 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2560 return RX_DROP_MONITOR;
2562 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
2563 event.u.ba.tid = tid;
2564 event.u.ba.ssn = start_seq_num;
2565 event.u.ba.sta = &rx->sta->sta;
2567 /* reset session timer */
2568 if (tid_agg_rx->timeout)
2569 mod_timer(&tid_agg_rx->session_timer,
2570 TU_TO_EXP_TIME(tid_agg_rx->timeout));
2572 spin_lock(&tid_agg_rx->reorder_lock);
2573 /* release stored frames up to start of BAR */
2574 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
2575 start_seq_num, frames);
2576 spin_unlock(&tid_agg_rx->reorder_lock);
2578 drv_event_callback(rx->local, rx->sdata, &event);
2585 * After this point, we only want management frames,
2586 * so we can drop all remaining control frames to
2587 * cooked monitor interfaces.
2589 return RX_DROP_MONITOR;
2592 static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2593 struct ieee80211_mgmt *mgmt,
2596 struct ieee80211_local *local = sdata->local;
2597 struct sk_buff *skb;
2598 struct ieee80211_mgmt *resp;
2600 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
2601 /* Not to own unicast address */
2605 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2606 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
2607 /* Not from the current AP or not associated yet. */
2611 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2612 /* Too short SA Query request frame */
2616 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2620 skb_reserve(skb, local->hw.extra_tx_headroom);
2621 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2622 memset(resp, 0, 24);
2623 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2624 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
2625 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
2626 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2627 IEEE80211_STYPE_ACTION);
2628 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2629 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2630 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2631 memcpy(resp->u.action.u.sa_query.trans_id,
2632 mgmt->u.action.u.sa_query.trans_id,
2633 WLAN_SA_QUERY_TR_ID_LEN);
2635 ieee80211_tx_skb(sdata, skb);
2638 static ieee80211_rx_result debug_noinline
2639 ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2641 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2642 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2645 * From here on, look only at management frames.
2646 * Data and control frames are already handled,
2647 * and unknown (reserved) frames are useless.
2649 if (rx->skb->len < 24)
2650 return RX_DROP_MONITOR;
2652 if (!ieee80211_is_mgmt(mgmt->frame_control))
2653 return RX_DROP_MONITOR;
2655 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2656 ieee80211_is_beacon(mgmt->frame_control) &&
2657 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
2660 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
2661 sig = status->signal;
2663 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2664 rx->skb->data, rx->skb->len,
2666 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2669 if (ieee80211_drop_unencrypted_mgmt(rx))
2670 return RX_DROP_UNUSABLE;
2675 static ieee80211_rx_result debug_noinline
2676 ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2678 struct ieee80211_local *local = rx->local;
2679 struct ieee80211_sub_if_data *sdata = rx->sdata;
2680 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2681 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2682 int len = rx->skb->len;
2684 if (!ieee80211_is_action(mgmt->frame_control))
2687 /* drop too small frames */
2688 if (len < IEEE80211_MIN_ACTION_SIZE)
2689 return RX_DROP_UNUSABLE;
2691 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
2692 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2693 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
2694 return RX_DROP_UNUSABLE;
2696 switch (mgmt->u.action.category) {
2697 case WLAN_CATEGORY_HT:
2698 /* reject HT action frames from stations not supporting HT */
2699 if (!rx->sta->sta.ht_cap.ht_supported)
2702 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2703 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2704 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2705 sdata->vif.type != NL80211_IFTYPE_AP &&
2706 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2709 /* verify action & smps_control/chanwidth are present */
2710 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2713 switch (mgmt->u.action.u.ht_smps.action) {
2714 case WLAN_HT_ACTION_SMPS: {
2715 struct ieee80211_supported_band *sband;
2716 enum ieee80211_smps_mode smps_mode;
2718 /* convert to HT capability */
2719 switch (mgmt->u.action.u.ht_smps.smps_control) {
2720 case WLAN_HT_SMPS_CONTROL_DISABLED:
2721 smps_mode = IEEE80211_SMPS_OFF;
2723 case WLAN_HT_SMPS_CONTROL_STATIC:
2724 smps_mode = IEEE80211_SMPS_STATIC;
2726 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
2727 smps_mode = IEEE80211_SMPS_DYNAMIC;
2733 /* if no change do nothing */
2734 if (rx->sta->sta.smps_mode == smps_mode)
2736 rx->sta->sta.smps_mode = smps_mode;
2738 sband = rx->local->hw.wiphy->bands[status->band];
2740 rate_control_rate_update(local, sband, rx->sta,
2741 IEEE80211_RC_SMPS_CHANGED);
2744 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2745 struct ieee80211_supported_band *sband;
2746 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
2747 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
2749 /* If it doesn't support 40 MHz it can't change ... */
2750 if (!(rx->sta->sta.ht_cap.cap &
2751 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
2754 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
2755 max_bw = IEEE80211_STA_RX_BW_20;
2757 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2759 /* set cur_max_bandwidth and recalc sta bw */
2760 rx->sta->cur_max_bandwidth = max_bw;
2761 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
2763 if (rx->sta->sta.bandwidth == new_bw)
2766 rx->sta->sta.bandwidth = new_bw;
2767 sband = rx->local->hw.wiphy->bands[status->band];
2769 rate_control_rate_update(local, sband, rx->sta,
2770 IEEE80211_RC_BW_CHANGED);
2778 case WLAN_CATEGORY_PUBLIC:
2779 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2781 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2785 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2787 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2788 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2790 if (len < offsetof(struct ieee80211_mgmt,
2791 u.action.u.ext_chan_switch.variable))
2794 case WLAN_CATEGORY_VHT:
2795 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2796 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2797 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2798 sdata->vif.type != NL80211_IFTYPE_AP &&
2799 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2802 /* verify action code is present */
2803 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2806 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2807 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
2810 /* verify opmode is present */
2811 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2814 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
2816 ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
2817 opmode, status->band);
2820 case WLAN_VHT_ACTION_GROUPID_MGMT: {
2821 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
2829 case WLAN_CATEGORY_BACK:
2830 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2831 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2832 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2833 sdata->vif.type != NL80211_IFTYPE_AP &&
2834 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2837 /* verify action_code is present */
2838 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2841 switch (mgmt->u.action.u.addba_req.action_code) {
2842 case WLAN_ACTION_ADDBA_REQ:
2843 if (len < (IEEE80211_MIN_ACTION_SIZE +
2844 sizeof(mgmt->u.action.u.addba_req)))
2847 case WLAN_ACTION_ADDBA_RESP:
2848 if (len < (IEEE80211_MIN_ACTION_SIZE +
2849 sizeof(mgmt->u.action.u.addba_resp)))
2852 case WLAN_ACTION_DELBA:
2853 if (len < (IEEE80211_MIN_ACTION_SIZE +
2854 sizeof(mgmt->u.action.u.delba)))
2862 case WLAN_CATEGORY_SPECTRUM_MGMT:
2863 /* verify action_code is present */
2864 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2867 switch (mgmt->u.action.u.measurement.action_code) {
2868 case WLAN_ACTION_SPCT_MSR_REQ:
2869 if (status->band != NL80211_BAND_5GHZ)
2872 if (len < (IEEE80211_MIN_ACTION_SIZE +
2873 sizeof(mgmt->u.action.u.measurement)))
2876 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2879 ieee80211_process_measurement_req(sdata, mgmt, len);
2881 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2883 if (len < (IEEE80211_MIN_ACTION_SIZE +
2884 sizeof(mgmt->u.action.u.chan_switch)))
2887 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2888 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2889 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2892 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2893 bssid = sdata->u.mgd.bssid;
2894 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2895 bssid = sdata->u.ibss.bssid;
2896 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2901 if (!ether_addr_equal(mgmt->bssid, bssid))
2908 case WLAN_CATEGORY_SA_QUERY:
2909 if (len < (IEEE80211_MIN_ACTION_SIZE +
2910 sizeof(mgmt->u.action.u.sa_query)))
2913 switch (mgmt->u.action.u.sa_query.action) {
2914 case WLAN_ACTION_SA_QUERY_REQUEST:
2915 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2917 ieee80211_process_sa_query_req(sdata, mgmt, len);
2921 case WLAN_CATEGORY_SELF_PROTECTED:
2922 if (len < (IEEE80211_MIN_ACTION_SIZE +
2923 sizeof(mgmt->u.action.u.self_prot.action_code)))
2926 switch (mgmt->u.action.u.self_prot.action_code) {
2927 case WLAN_SP_MESH_PEERING_OPEN:
2928 case WLAN_SP_MESH_PEERING_CLOSE:
2929 case WLAN_SP_MESH_PEERING_CONFIRM:
2930 if (!ieee80211_vif_is_mesh(&sdata->vif))
2932 if (sdata->u.mesh.user_mpm)
2933 /* userspace handles this frame */
2936 case WLAN_SP_MGK_INFORM:
2937 case WLAN_SP_MGK_ACK:
2938 if (!ieee80211_vif_is_mesh(&sdata->vif))
2943 case WLAN_CATEGORY_MESH_ACTION:
2944 if (len < (IEEE80211_MIN_ACTION_SIZE +
2945 sizeof(mgmt->u.action.u.mesh_action.action_code)))
2948 if (!ieee80211_vif_is_mesh(&sdata->vif))
2950 if (mesh_action_is_path_sel(mgmt) &&
2951 !mesh_path_sel_is_hwmp(sdata))
2959 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2960 /* will return in the next handlers */
2965 rx->sta->rx_stats.packets++;
2966 dev_kfree_skb(rx->skb);
2970 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2971 skb_queue_tail(&sdata->skb_queue, rx->skb);
2972 ieee80211_queue_work(&local->hw, &sdata->work);
2974 rx->sta->rx_stats.packets++;
2978 static ieee80211_rx_result debug_noinline
2979 ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2981 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2984 /* skip known-bad action frames and return them in the next handler */
2985 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2989 * Getting here means the kernel doesn't know how to handle
2990 * it, but maybe userspace does ... include returned frames
2991 * so userspace can register for those to know whether ones
2992 * it transmitted were processed or returned.
2995 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
2996 sig = status->signal;
2998 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
2999 rx->skb->data, rx->skb->len, 0)) {
3001 rx->sta->rx_stats.packets++;
3002 dev_kfree_skb(rx->skb);
3009 static ieee80211_rx_result debug_noinline
3010 ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3012 struct ieee80211_local *local = rx->local;
3013 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3014 struct sk_buff *nskb;
3015 struct ieee80211_sub_if_data *sdata = rx->sdata;
3016 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3018 if (!ieee80211_is_action(mgmt->frame_control))
3022 * For AP mode, hostapd is responsible for handling any action
3023 * frames that we didn't handle, including returning unknown
3024 * ones. For all other modes we will return them to the sender,
3025 * setting the 0x80 bit in the action category, as required by
3026 * 802.11-2012 9.24.4.
3027 * Newer versions of hostapd shall also use the management frame
3028 * registration mechanisms, but older ones still use cooked
3029 * monitor interfaces so push all frames there.
3031 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3032 (sdata->vif.type == NL80211_IFTYPE_AP ||
3033 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3034 return RX_DROP_MONITOR;
3036 if (is_multicast_ether_addr(mgmt->da))
3037 return RX_DROP_MONITOR;
3039 /* do not return rejected action frames */
3040 if (mgmt->u.action.category & 0x80)
3041 return RX_DROP_UNUSABLE;
3043 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3046 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3048 nmgmt->u.action.category |= 0x80;
3049 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3050 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3052 memset(nskb->cb, 0, sizeof(nskb->cb));
3054 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3055 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3057 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3058 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3059 IEEE80211_TX_CTL_NO_CCK_RATE;
3060 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3062 local->hw.offchannel_tx_hw_queue;
3065 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3068 dev_kfree_skb(rx->skb);
3072 static ieee80211_rx_result debug_noinline
3073 ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3075 struct ieee80211_sub_if_data *sdata = rx->sdata;
3076 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3079 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3081 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3082 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3083 sdata->vif.type != NL80211_IFTYPE_OCB &&
3084 sdata->vif.type != NL80211_IFTYPE_STATION)
3085 return RX_DROP_MONITOR;
3088 case cpu_to_le16(IEEE80211_STYPE_AUTH):
3089 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3090 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3091 /* process for all: mesh, mlme, ibss */
3093 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3094 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3095 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3096 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3097 if (is_multicast_ether_addr(mgmt->da) &&
3098 !is_broadcast_ether_addr(mgmt->da))
3099 return RX_DROP_MONITOR;
3101 /* process only for station */
3102 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3103 return RX_DROP_MONITOR;
3105 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3106 /* process only for ibss and mesh */
3107 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3108 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3109 return RX_DROP_MONITOR;
3112 return RX_DROP_MONITOR;
3115 /* queue up frame and kick off work to process it */
3116 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
3117 skb_queue_tail(&sdata->skb_queue, rx->skb);
3118 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3120 rx->sta->rx_stats.packets++;
3125 static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3126 struct ieee80211_rate *rate)
3128 struct ieee80211_sub_if_data *sdata;
3129 struct ieee80211_local *local = rx->local;
3130 struct sk_buff *skb = rx->skb, *skb2;
3131 struct net_device *prev_dev = NULL;
3132 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3133 int needed_headroom;
3136 * If cooked monitor has been processed already, then
3137 * don't do it again. If not, set the flag.
3139 if (rx->flags & IEEE80211_RX_CMNTR)
3141 rx->flags |= IEEE80211_RX_CMNTR;
3143 /* If there are no cooked monitor interfaces, just free the SKB */
3144 if (!local->cooked_mntrs)
3147 /* vendor data is long removed here */
3148 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3149 /* room for the radiotap header based on driver features */
3150 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3152 if (skb_headroom(skb) < needed_headroom &&
3153 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3156 /* prepend radiotap information */
3157 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3160 skb_reset_mac_header(skb);
3161 skb->ip_summed = CHECKSUM_UNNECESSARY;
3162 skb->pkt_type = PACKET_OTHERHOST;
3163 skb->protocol = htons(ETH_P_802_2);
3165 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3166 if (!ieee80211_sdata_running(sdata))
3169 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3170 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3174 skb2 = skb_clone(skb, GFP_ATOMIC);
3176 skb2->dev = prev_dev;
3177 netif_receive_skb(skb2);
3181 prev_dev = sdata->dev;
3182 ieee80211_rx_stats(sdata->dev, skb->len);
3186 skb->dev = prev_dev;
3187 netif_receive_skb(skb);
3195 static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3196 ieee80211_rx_result res)
3199 case RX_DROP_MONITOR:
3200 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3202 rx->sta->rx_stats.dropped++;
3205 struct ieee80211_rate *rate = NULL;
3206 struct ieee80211_supported_band *sband;
3207 struct ieee80211_rx_status *status;
3209 status = IEEE80211_SKB_RXCB((rx->skb));
3211 sband = rx->local->hw.wiphy->bands[status->band];
3212 if (!(status->flag & RX_FLAG_HT) &&
3213 !(status->flag & RX_FLAG_VHT))
3214 rate = &sband->bitrates[status->rate_idx];
3216 ieee80211_rx_cooked_monitor(rx, rate);
3219 case RX_DROP_UNUSABLE:
3220 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3222 rx->sta->rx_stats.dropped++;
3223 dev_kfree_skb(rx->skb);
3226 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3231 static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3232 struct sk_buff_head *frames)
3234 ieee80211_rx_result res = RX_DROP_MONITOR;
3235 struct sk_buff *skb;
3237 #define CALL_RXH(rxh) \
3240 if (res != RX_CONTINUE) \
3244 /* Lock here to avoid hitting all of the data used in the RX
3245 * path (e.g. key data, station data, ...) concurrently when
3246 * a frame is released from the reorder buffer due to timeout
3247 * from the timer, potentially concurrently with RX from the
3250 spin_lock_bh(&rx->local->rx_path_lock);
3252 while ((skb = __skb_dequeue(frames))) {
3254 * all the other fields are valid across frames
3255 * that belong to an aMPDU since they are on the
3256 * same TID from the same station
3260 CALL_RXH(ieee80211_rx_h_check_more_data);
3261 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3262 CALL_RXH(ieee80211_rx_h_sta_process);
3263 CALL_RXH(ieee80211_rx_h_decrypt);
3264 CALL_RXH(ieee80211_rx_h_defragment);
3265 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3266 /* must be after MMIC verify so header is counted in MPDU mic */
3267 #ifdef CONFIG_MAC80211_MESH
3268 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3269 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3271 CALL_RXH(ieee80211_rx_h_amsdu);
3272 CALL_RXH(ieee80211_rx_h_data);
3274 /* special treatment -- needs the queue */
3275 res = ieee80211_rx_h_ctrl(rx, frames);
3276 if (res != RX_CONTINUE)
3279 CALL_RXH(ieee80211_rx_h_mgmt_check);
3280 CALL_RXH(ieee80211_rx_h_action);
3281 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3282 CALL_RXH(ieee80211_rx_h_action_return);
3283 CALL_RXH(ieee80211_rx_h_mgmt);
3286 ieee80211_rx_handlers_result(rx, res);
3291 spin_unlock_bh(&rx->local->rx_path_lock);
3294 static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3296 struct sk_buff_head reorder_release;
3297 ieee80211_rx_result res = RX_DROP_MONITOR;
3299 __skb_queue_head_init(&reorder_release);
3301 #define CALL_RXH(rxh) \
3304 if (res != RX_CONTINUE) \
3308 CALL_RXH(ieee80211_rx_h_check_dup);
3309 CALL_RXH(ieee80211_rx_h_check);
3311 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3313 ieee80211_rx_handlers(rx, &reorder_release);
3317 ieee80211_rx_handlers_result(rx, res);
3323 * This function makes calls into the RX path, therefore
3324 * it has to be invoked under RCU read lock.
3326 void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3328 struct sk_buff_head frames;
3329 struct ieee80211_rx_data rx = {
3331 .sdata = sta->sdata,
3332 .local = sta->local,
3333 /* This is OK -- must be QoS data frame */
3334 .security_idx = tid,
3336 .napi = NULL, /* must be NULL to not have races */
3338 struct tid_ampdu_rx *tid_agg_rx;
3340 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3344 __skb_queue_head_init(&frames);
3346 spin_lock(&tid_agg_rx->reorder_lock);
3347 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3348 spin_unlock(&tid_agg_rx->reorder_lock);
3350 if (!skb_queue_empty(&frames)) {
3351 struct ieee80211_event event = {
3352 .type = BA_FRAME_TIMEOUT,
3354 .u.ba.sta = &sta->sta,
3356 drv_event_callback(rx.local, rx.sdata, &event);
3359 ieee80211_rx_handlers(&rx, &frames);
3362 void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3363 u16 ssn, u64 filtered,
3366 struct sta_info *sta;
3367 struct tid_ampdu_rx *tid_agg_rx;
3368 struct sk_buff_head frames;
3369 struct ieee80211_rx_data rx = {
3370 /* This is OK -- must be QoS data frame */
3371 .security_idx = tid,
3376 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3379 __skb_queue_head_init(&frames);
3381 sta = container_of(pubsta, struct sta_info, sta);
3384 rx.sdata = sta->sdata;
3385 rx.local = sta->local;
3388 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3392 spin_lock_bh(&tid_agg_rx->reorder_lock);
3394 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3397 /* release all frames in the reorder buffer */
3398 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3399 IEEE80211_SN_MODULO;
3400 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3402 /* update ssn to match received ssn */
3403 tid_agg_rx->head_seq_num = ssn;
3405 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3409 /* handle the case that received ssn is behind the mac ssn.
3410 * it can be tid_agg_rx->buf_size behind and still be valid */
3411 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3412 if (diff >= tid_agg_rx->buf_size) {
3413 tid_agg_rx->reorder_buf_filtered = 0;
3416 filtered = filtered >> diff;
3420 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3421 int index = (ssn + i) % tid_agg_rx->buf_size;
3423 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3424 if (filtered & BIT_ULL(i))
3425 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3428 /* now process also frames that the filter marking released */
3429 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3432 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3434 ieee80211_rx_handlers(&rx, &frames);
3439 EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3441 /* main receive path */
3443 static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
3445 struct ieee80211_sub_if_data *sdata = rx->sdata;
3446 struct sk_buff *skb = rx->skb;
3447 struct ieee80211_hdr *hdr = (void *)skb->data;
3448 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3449 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
3450 int multicast = is_multicast_ether_addr(hdr->addr1);
3452 switch (sdata->vif.type) {
3453 case NL80211_IFTYPE_STATION:
3454 if (!bssid && !sdata->u.mgd.use_4addr)
3458 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3459 case NL80211_IFTYPE_ADHOC:
3462 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3463 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3465 if (ieee80211_is_beacon(hdr->frame_control))
3467 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3470 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3474 if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3475 rate_idx = 0; /* TODO: HT/VHT rates */
3477 rate_idx = status->rate_idx;
3478 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3482 case NL80211_IFTYPE_OCB:
3485 if (!ieee80211_is_data_present(hdr->frame_control))
3487 if (!is_broadcast_ether_addr(bssid))
3490 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
3494 if (status->flag & RX_FLAG_HT)
3495 rate_idx = 0; /* TODO: HT rates */
3497 rate_idx = status->rate_idx;
3498 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3502 case NL80211_IFTYPE_MESH_POINT:
3505 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3506 case NL80211_IFTYPE_AP_VLAN:
3507 case NL80211_IFTYPE_AP:
3509 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3511 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3513 * Accept public action frames even when the
3514 * BSSID doesn't match, this is used for P2P
3515 * and location updates. Note that mac80211
3516 * itself never looks at these frames.
3519 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3521 if (ieee80211_is_public_action(hdr, skb->len))
3523 return ieee80211_is_beacon(hdr->frame_control);
3526 if (!ieee80211_has_tods(hdr->frame_control)) {
3527 /* ignore data frames to TDLS-peers */
3528 if (ieee80211_is_data(hdr->frame_control))
3530 /* ignore action frames to TDLS-peers */
3531 if (ieee80211_is_action(hdr->frame_control) &&
3532 !is_broadcast_ether_addr(bssid) &&
3533 !ether_addr_equal(bssid, hdr->addr1))
3537 case NL80211_IFTYPE_WDS:
3538 if (bssid || !ieee80211_is_data(hdr->frame_control))
3540 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
3541 case NL80211_IFTYPE_P2P_DEVICE:
3542 return ieee80211_is_public_action(hdr, skb->len) ||
3543 ieee80211_is_probe_req(hdr->frame_control) ||
3544 ieee80211_is_probe_resp(hdr->frame_control) ||
3545 ieee80211_is_beacon(hdr->frame_control);
3554 void ieee80211_check_fast_rx(struct sta_info *sta)
3556 struct ieee80211_sub_if_data *sdata = sta->sdata;
3557 struct ieee80211_local *local = sdata->local;
3558 struct ieee80211_key *key;
3559 struct ieee80211_fast_rx fastrx = {
3561 .vif_type = sdata->vif.type,
3562 .control_port_protocol = sdata->control_port_protocol,
3563 }, *old, *new = NULL;
3564 bool assign = false;
3566 /* use sparse to check that we don't return without updating */
3567 __acquire(check_fast_rx);
3569 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
3570 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
3571 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
3572 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
3574 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
3576 /* fast-rx doesn't do reordering */
3577 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
3578 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
3581 switch (sdata->vif.type) {
3582 case NL80211_IFTYPE_STATION:
3583 /* 4-addr is harder to deal with, later maybe */
3584 if (sdata->u.mgd.use_4addr)
3586 /* software powersave is a huge mess, avoid all of it */
3587 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3589 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3590 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3592 if (sta->sta.tdls) {
3593 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3594 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3595 fastrx.expected_ds_bits = 0;
3597 fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
3598 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3599 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3600 fastrx.expected_ds_bits =
3601 cpu_to_le16(IEEE80211_FCTL_FROMDS);
3604 case NL80211_IFTYPE_AP_VLAN:
3605 case NL80211_IFTYPE_AP:
3606 /* parallel-rx requires this, at least with calls to
3607 * ieee80211_sta_ps_transition()
3609 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
3611 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3612 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3613 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
3615 fastrx.internal_forward =
3616 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
3617 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
3618 !sdata->u.vlan.sta);
3624 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3628 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
3630 switch (key->conf.cipher) {
3631 case WLAN_CIPHER_SUITE_TKIP:
3632 /* we don't want to deal with MMIC in fast-rx */
3634 case WLAN_CIPHER_SUITE_CCMP:
3635 case WLAN_CIPHER_SUITE_CCMP_256:
3636 case WLAN_CIPHER_SUITE_GCMP:
3637 case WLAN_CIPHER_SUITE_GCMP_256:
3640 /* we also don't want to deal with WEP or cipher scheme
3641 * since those require looking up the key idx in the
3642 * frame, rather than assuming the PTK is used
3643 * (we need to revisit this once we implement the real
3644 * PTK index, which is now valid in the spec, but we
3645 * haven't implemented that part yet)
3651 fastrx.icv_len = key->conf.icv_len;
3658 __release(check_fast_rx);
3661 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
3663 spin_lock_bh(&sta->lock);
3664 old = rcu_dereference_protected(sta->fast_rx, true);
3665 rcu_assign_pointer(sta->fast_rx, new);
3666 spin_unlock_bh(&sta->lock);
3669 kfree_rcu(old, rcu_head);
3672 void ieee80211_clear_fast_rx(struct sta_info *sta)
3674 struct ieee80211_fast_rx *old;
3676 spin_lock_bh(&sta->lock);
3677 old = rcu_dereference_protected(sta->fast_rx, true);
3678 RCU_INIT_POINTER(sta->fast_rx, NULL);
3679 spin_unlock_bh(&sta->lock);
3682 kfree_rcu(old, rcu_head);
3685 void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3687 struct ieee80211_local *local = sdata->local;
3688 struct sta_info *sta;
3690 lockdep_assert_held(&local->sta_mtx);
3692 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3693 if (sdata != sta->sdata &&
3694 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3696 ieee80211_check_fast_rx(sta);
3700 void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3702 struct ieee80211_local *local = sdata->local;
3704 mutex_lock(&local->sta_mtx);
3705 __ieee80211_check_fast_rx_iface(sdata);
3706 mutex_unlock(&local->sta_mtx);
3709 static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
3710 struct ieee80211_fast_rx *fast_rx)
3712 struct sk_buff *skb = rx->skb;
3713 struct ieee80211_hdr *hdr = (void *)skb->data;
3714 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3715 struct sta_info *sta = rx->sta;
3716 int orig_len = skb->len;
3717 int snap_offs = ieee80211_hdrlen(hdr->frame_control);
3719 u8 snap[sizeof(rfc1042_header)];
3721 } *payload __aligned(2);
3725 } addrs __aligned(2);
3726 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
3728 if (fast_rx->uses_rss)
3729 stats = this_cpu_ptr(sta->pcpu_rx_stats);
3731 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
3732 * to a common data structure; drivers can implement that per queue
3733 * but we don't have that information in mac80211
3735 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
3738 #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
3740 /* If using encryption, we also need to have:
3741 * - PN_VALIDATED: similar, but the implementation is tricky
3742 * - DECRYPTED: necessary for PN_VALIDATED
3745 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
3748 /* we don't deal with A-MSDU deaggregation here */
3749 if (status->rx_flags & IEEE80211_RX_AMSDU)
3752 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3755 if (unlikely(ieee80211_is_frag(hdr)))
3758 /* Since our interface address cannot be multicast, this
3759 * implicitly also rejects multicast frames without the
3762 * We shouldn't get any *data* frames not addressed to us
3763 * (AP mode will accept multicast *management* frames), but
3764 * punting here will make it go through the full checks in
3765 * ieee80211_accept_frame().
3767 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
3770 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
3771 IEEE80211_FCTL_TODS)) !=
3772 fast_rx->expected_ds_bits)
3775 /* assign the key to drop unencrypted frames (later)
3776 * and strip the IV/MIC if necessary
3778 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
3779 /* GCMP header length is the same */
3780 snap_offs += IEEE80211_CCMP_HDR_LEN;
3783 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
3785 payload = (void *)(skb->data + snap_offs);
3787 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
3790 /* Don't handle these here since they require special code.
3791 * Accept AARP and IPX even though they should come with a
3792 * bridge-tunnel header - but if we get them this way then
3793 * there's little point in discarding them.
3795 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
3796 payload->proto == fast_rx->control_port_protocol))
3799 /* after this point, don't punt to the slowpath! */
3801 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
3802 pskb_trim(skb, skb->len - fast_rx->icv_len))
3805 if (unlikely(fast_rx->sta_notify)) {
3806 ieee80211_sta_rx_notify(rx->sdata, hdr);
3807 fast_rx->sta_notify = false;
3810 /* statistics part of ieee80211_rx_h_sta_process() */
3811 stats->last_rx = jiffies;
3812 stats->last_rate = sta_stats_encode_rate(status);
3816 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
3817 stats->last_signal = status->signal;
3818 if (!fast_rx->uses_rss)
3819 ewma_signal_add(&sta->rx_stats_avg.signal,
3823 if (status->chains) {
3826 stats->chains = status->chains;
3827 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
3828 int signal = status->chain_signal[i];
3830 if (!(status->chains & BIT(i)))
3833 stats->chain_signal_last[i] = signal;
3834 if (!fast_rx->uses_rss)
3835 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
3839 /* end of statistics */
3841 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
3844 /* do the header conversion - first grab the addresses */
3845 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
3846 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
3847 /* remove the SNAP but leave the ethertype */
3848 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
3849 /* push the addresses in front */
3850 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
3852 skb->dev = fast_rx->dev;
3854 ieee80211_rx_stats(fast_rx->dev, skb->len);
3856 /* The seqno index has the same property as needed
3857 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
3858 * for non-QoS-data frames. Here we know it's a data
3859 * frame, so count MSDUs.
3861 u64_stats_update_begin(&stats->syncp);
3862 stats->msdu[rx->seqno_idx]++;
3863 stats->bytes += orig_len;
3864 u64_stats_update_end(&stats->syncp);
3866 if (fast_rx->internal_forward) {
3867 struct sta_info *dsta = sta_info_get(rx->sdata, skb->data);
3871 * Send to wireless media and increase priority by 256
3872 * to keep the received priority instead of
3873 * reclassifying the frame (see cfg80211_classify8021d).
3875 skb->priority += 256;
3876 skb->protocol = htons(ETH_P_802_3);
3877 skb_reset_network_header(skb);
3878 skb_reset_mac_header(skb);
3879 dev_queue_xmit(skb);
3884 /* deliver to local stack */
3885 skb->protocol = eth_type_trans(skb, fast_rx->dev);
3886 memset(skb->cb, 0, sizeof(skb->cb));
3888 napi_gro_receive(rx->napi, skb);
3890 netif_receive_skb(skb);
3900 * This function returns whether or not the SKB
3901 * was destined for RX processing or not, which,
3902 * if consume is true, is equivalent to whether
3903 * or not the skb was consumed.
3905 static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
3906 struct sk_buff *skb, bool consume)
3908 struct ieee80211_local *local = rx->local;
3909 struct ieee80211_sub_if_data *sdata = rx->sdata;
3913 /* See if we can do fast-rx; if we have to copy we already lost,
3914 * so punt in that case. We should never have to deliver a data
3915 * frame to multiple interfaces anyway.
3917 * We skip the ieee80211_accept_frame() call and do the necessary
3918 * checking inside ieee80211_invoke_fast_rx().
3920 if (consume && rx->sta) {
3921 struct ieee80211_fast_rx *fast_rx;
3923 fast_rx = rcu_dereference(rx->sta->fast_rx);
3924 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
3928 if (!ieee80211_accept_frame(rx))
3932 skb = skb_copy(skb, GFP_ATOMIC);
3934 if (net_ratelimit())
3935 wiphy_debug(local->hw.wiphy,
3936 "failed to copy skb for %s\n",
3944 ieee80211_invoke_rx_handlers(rx);
3949 * This is the actual Rx frames handler. as it belongs to Rx path it must
3950 * be called with rcu_read_lock protection.
3952 static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
3953 struct ieee80211_sta *pubsta,
3954 struct sk_buff *skb,
3955 struct napi_struct *napi)
3957 struct ieee80211_local *local = hw_to_local(hw);
3958 struct ieee80211_sub_if_data *sdata;
3959 struct ieee80211_hdr *hdr;
3961 struct ieee80211_rx_data rx;
3962 struct ieee80211_sub_if_data *prev;
3963 struct rhash_head *tmp;
3966 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
3967 memset(&rx, 0, sizeof(rx));
3972 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
3973 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
3975 if (ieee80211_is_mgmt(fc)) {
3976 /* drop frame if too short for header */
3977 if (skb->len < ieee80211_hdrlen(fc))
3980 err = skb_linearize(skb);
3982 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
3990 hdr = (struct ieee80211_hdr *)skb->data;
3991 ieee80211_parse_qos(&rx);
3992 ieee80211_verify_alignment(&rx);
3994 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
3995 ieee80211_is_beacon(hdr->frame_control)))
3996 ieee80211_scan_rx(local, skb);
3999 rx.sta = container_of(pubsta, struct sta_info, sta);
4000 rx.sdata = rx.sta->sdata;
4001 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4004 } else if (ieee80211_is_data(fc)) {
4005 struct sta_info *sta, *prev_sta;
4006 const struct bucket_table *tbl;
4010 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
4012 for_each_sta_info(local, tbl, hdr->addr2, sta, tmp) {
4019 rx.sdata = prev_sta->sdata;
4020 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4027 rx.sdata = prev_sta->sdata;
4029 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4037 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4038 if (!ieee80211_sdata_running(sdata))
4041 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4042 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4046 * frame is destined for this interface, but if it's
4047 * not also for the previous one we handle that after
4048 * the loop to avoid copying the SKB once too much
4056 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4058 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4064 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4067 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4076 * This is the receive path handler. It is called by a low level driver when an
4077 * 802.11 MPDU is received from the hardware.
4079 void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4080 struct sk_buff *skb, struct napi_struct *napi)
4082 struct ieee80211_local *local = hw_to_local(hw);
4083 struct ieee80211_rate *rate = NULL;
4084 struct ieee80211_supported_band *sband;
4085 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4087 WARN_ON_ONCE(softirq_count() == 0);
4089 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4092 sband = local->hw.wiphy->bands[status->band];
4093 if (WARN_ON(!sband))
4097 * If we're suspending, it is possible although not too likely
4098 * that we'd be receiving frames after having already partially
4099 * quiesced the stack. We can't process such frames then since
4100 * that might, for example, cause stations to be added or other
4101 * driver callbacks be invoked.
4103 if (unlikely(local->quiescing || local->suspended))
4106 /* We might be during a HW reconfig, prevent Rx for the same reason */
4107 if (unlikely(local->in_reconfig))
4111 * The same happens when we're not even started,
4112 * but that's worth a warning.
4114 if (WARN_ON(!local->started))
4117 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4119 * Validate the rate, unless a PLCP error means that
4120 * we probably can't have a valid rate here anyway.
4123 if (status->flag & RX_FLAG_HT) {
4125 * rate_idx is MCS index, which can be [0-76]
4128 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4130 * Anything else would be some sort of driver or
4131 * hardware error. The driver should catch hardware
4134 if (WARN(status->rate_idx > 76,
4135 "Rate marked as an HT rate but passed "
4136 "status->rate_idx is not "
4137 "an MCS index [0-76]: %d (0x%02x)\n",
4141 } else if (status->flag & RX_FLAG_VHT) {
4142 if (WARN_ONCE(status->rate_idx > 9 ||
4144 status->vht_nss > 8,
4145 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4146 status->rate_idx, status->vht_nss))
4149 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4151 rate = &sband->bitrates[status->rate_idx];
4155 status->rx_flags = 0;
4158 * key references and virtual interfaces are protected using RCU
4159 * and this requires that we are in a read-side RCU section during
4160 * receive processing
4165 * Frames with failed FCS/PLCP checksum are not returned,
4166 * all other frames are returned without radiotap header
4167 * if it was previously present.
4168 * Also, frames with less than 16 bytes are dropped.
4170 skb = ieee80211_rx_monitor(local, skb, rate);
4176 ieee80211_tpt_led_trig_rx(local,
4177 ((struct ieee80211_hdr *)skb->data)->frame_control,
4180 __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
4188 EXPORT_SYMBOL(ieee80211_rx_napi);
4190 /* This is a version of the rx handler that can be called from hard irq
4191 * context. Post the skb on the queue and schedule the tasklet */
4192 void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4194 struct ieee80211_local *local = hw_to_local(hw);
4196 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4198 skb->pkt_type = IEEE80211_RX_MSG;
4199 skb_queue_tail(&local->skb_queue, skb);
4200 tasklet_schedule(&local->tasklet);
4202 EXPORT_SYMBOL(ieee80211_rx_irqsafe);