blob: 04ae9de55d74b2088c628c4a008ac9d04c9e6b59 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 - 2017 Intel Deutschland GmbH
8 *
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.
12 */
13
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>
26
27#include "ieee80211_i.h"
28#include "driver-ops.h"
29#include "led.h"
30#include "mesh.h"
31#include "wep.h"
32#include "wpa.h"
33#include "tkip.h"
34#include "wme.h"
35#include "rate.h"
36
37static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
38{
39 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
40
41 u64_stats_update_begin(&tstats->syncp);
42 tstats->rx_packets++;
43 tstats->rx_bytes += len;
44 u64_stats_update_end(&tstats->syncp);
45}
46
47static u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
48 enum nl80211_iftype type)
49{
50 __le16 fc = hdr->frame_control;
51
52 if (ieee80211_is_data(fc)) {
53 if (len < 24) /* drop incorrect hdr len (data) */
54 return NULL;
55
56 if (ieee80211_has_a4(fc))
57 return NULL;
58 if (ieee80211_has_tods(fc))
59 return hdr->addr1;
60 if (ieee80211_has_fromds(fc))
61 return hdr->addr2;
62
63 return hdr->addr3;
64 }
65
66 if (ieee80211_is_mgmt(fc)) {
67 if (len < 24) /* drop incorrect hdr len (mgmt) */
68 return NULL;
69 return hdr->addr3;
70 }
71
72 if (ieee80211_is_ctl(fc)) {
73 if (ieee80211_is_pspoll(fc))
74 return hdr->addr1;
75
76 if (ieee80211_is_back_req(fc)) {
77 switch (type) {
78 case NL80211_IFTYPE_STATION:
79 return hdr->addr2;
80 case NL80211_IFTYPE_AP:
81 case NL80211_IFTYPE_AP_VLAN:
82 return hdr->addr1;
83 default:
84 break; /* fall through to the return */
85 }
86 }
87 }
88
89 return NULL;
90}
91
92/*
93 * monitor mode reception
94 *
95 * This function cleans up the SKB, i.e. it removes all the stuff
96 * only useful for monitoring.
97 */
98static void remove_monitor_info(struct sk_buff *skb,
99 unsigned int present_fcs_len,
100 unsigned int rtap_vendor_space)
101{
102 if (present_fcs_len)
103 __pskb_trim(skb, skb->len - present_fcs_len);
104 __pskb_pull(skb, rtap_vendor_space);
105}
106
107static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
108 unsigned int rtap_vendor_space)
109{
110 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
111 struct ieee80211_hdr *hdr;
112
113 hdr = (void *)(skb->data + rtap_vendor_space);
114
115 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
116 RX_FLAG_FAILED_PLCP_CRC |
117 RX_FLAG_ONLY_MONITOR))
118 return true;
119
120 if (unlikely(skb->len < 16 + present_fcs_len + rtap_vendor_space))
121 return true;
122
123 if (ieee80211_is_ctl(hdr->frame_control) &&
124 !ieee80211_is_pspoll(hdr->frame_control) &&
125 !ieee80211_is_back_req(hdr->frame_control))
126 return true;
127
128 return false;
129}
130
131static int
132ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
133 struct ieee80211_rx_status *status,
134 struct sk_buff *skb)
135{
136 int len;
137
138 /* always present fields */
139 len = sizeof(struct ieee80211_radiotap_header) + 8;
140
141 /* allocate extra bitmaps */
142 if (status->chains)
143 len += 4 * hweight8(status->chains);
144 /* vendor presence bitmap */
145 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
146 len += 4;
147
148 if (ieee80211_have_rx_timestamp(status)) {
149 len = ALIGN(len, 8);
150 len += 8;
151 }
152 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
153 len += 1;
154
155 /* antenna field, if we don't have per-chain info */
156 if (!status->chains)
157 len += 1;
158
159 /* padding for RX_FLAGS if necessary */
160 len = ALIGN(len, 2);
161
162 if (status->encoding == RX_ENC_HT) /* HT info */
163 len += 3;
164
165 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
166 len = ALIGN(len, 4);
167 len += 8;
168 }
169
170 if (status->encoding == RX_ENC_VHT) {
171 len = ALIGN(len, 2);
172 len += 12;
173 }
174
175 if (local->hw.radiotap_timestamp.units_pos >= 0) {
176 len = ALIGN(len, 8);
177 len += 12;
178 }
179
180 if (status->chains) {
181 /* antenna and antenna signal fields */
182 len += 2 * hweight8(status->chains);
183 }
184
185 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
186 struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
187
188 /* alignment for fixed 6-byte vendor data header */
189 len = ALIGN(len, 2);
190 /* vendor data header */
191 len += 6;
192 if (WARN_ON(rtap->align == 0))
193 rtap->align = 1;
194 len = ALIGN(len, rtap->align);
195 len += rtap->len + rtap->pad;
196 }
197
198 return len;
199}
200
201static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
202 struct sk_buff *skb,
203 int rtap_vendor_space)
204{
205 struct {
206 struct ieee80211_hdr_3addr hdr;
207 u8 category;
208 u8 action_code;
209 } __packed __aligned(2) action;
210
211 if (!sdata)
212 return;
213
214 BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
215
216 if (skb->len < rtap_vendor_space + sizeof(action) +
217 VHT_MUMIMO_GROUPS_DATA_LEN)
218 return;
219
220 if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
221 return;
222
223 skb_copy_bits(skb, rtap_vendor_space, &action, sizeof(action));
224
225 if (!ieee80211_is_action(action.hdr.frame_control))
226 return;
227
228 if (action.category != WLAN_CATEGORY_VHT)
229 return;
230
231 if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
232 return;
233
234 if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
235 return;
236
237 skb = skb_copy(skb, GFP_ATOMIC);
238 if (!skb)
239 return;
240
241 skb_queue_tail(&sdata->skb_queue, skb);
242 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
243}
244
245/*
246 * ieee80211_add_rx_radiotap_header - add radiotap header
247 *
248 * add a radiotap header containing all the fields which the hardware provided.
249 */
250static void
251ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
252 struct sk_buff *skb,
253 struct ieee80211_rate *rate,
254 int rtap_len, bool has_fcs)
255{
256 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
257 struct ieee80211_radiotap_header *rthdr;
258 unsigned char *pos;
259 __le32 *it_present;
260 u32 it_present_val;
261 u16 rx_flags = 0;
262 u16 channel_flags = 0;
263 int mpdulen, chain;
264 unsigned long chains = status->chains;
265 struct ieee80211_vendor_radiotap rtap = {};
266
267 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
268 rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
269 /* rtap.len and rtap.pad are undone immediately */
270 skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
271 }
272
273 mpdulen = skb->len;
274 if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
275 mpdulen += FCS_LEN;
276
277 rthdr = skb_push(skb, rtap_len);
278 memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
279 it_present = &rthdr->it_present;
280
281 /* radiotap header, set always present flags */
282 rthdr->it_len = cpu_to_le16(rtap_len);
283 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
284 BIT(IEEE80211_RADIOTAP_CHANNEL) |
285 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
286
287 if (!status->chains)
288 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
289
290 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
291 it_present_val |=
292 BIT(IEEE80211_RADIOTAP_EXT) |
293 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
294 put_unaligned_le32(it_present_val, it_present);
295 it_present++;
296 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
297 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
298 }
299
300 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
301 it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
302 BIT(IEEE80211_RADIOTAP_EXT);
303 put_unaligned_le32(it_present_val, it_present);
304 it_present++;
305 it_present_val = rtap.present;
306 }
307
308 put_unaligned_le32(it_present_val, it_present);
309
310 pos = (void *)(it_present + 1);
311
312 /* the order of the following fields is important */
313
314 /* IEEE80211_RADIOTAP_TSFT */
315 if (ieee80211_have_rx_timestamp(status)) {
316 /* padding */
317 while ((pos - (u8 *)rthdr) & 7)
318 *pos++ = 0;
319 put_unaligned_le64(
320 ieee80211_calculate_rx_timestamp(local, status,
321 mpdulen, 0),
322 pos);
323 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
324 pos += 8;
325 }
326
327 /* IEEE80211_RADIOTAP_FLAGS */
328 if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
329 *pos |= IEEE80211_RADIOTAP_F_FCS;
330 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
331 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
332 if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
333 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
334 pos++;
335
336 /* IEEE80211_RADIOTAP_RATE */
337 if (!rate || status->encoding != RX_ENC_LEGACY) {
338 /*
339 * Without rate information don't add it. If we have,
340 * MCS information is a separate field in radiotap,
341 * added below. The byte here is needed as padding
342 * for the channel though, so initialise it to 0.
343 */
344 *pos = 0;
345 } else {
346 int shift = 0;
347 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
348 if (status->bw == RATE_INFO_BW_10)
349 shift = 1;
350 else if (status->bw == RATE_INFO_BW_5)
351 shift = 2;
352 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
353 }
354 pos++;
355
356 /* IEEE80211_RADIOTAP_CHANNEL */
357 put_unaligned_le16(status->freq, pos);
358 pos += 2;
359 if (status->bw == RATE_INFO_BW_10)
360 channel_flags |= IEEE80211_CHAN_HALF;
361 else if (status->bw == RATE_INFO_BW_5)
362 channel_flags |= IEEE80211_CHAN_QUARTER;
363
364 if (status->band == NL80211_BAND_5GHZ)
365 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
366 else if (status->encoding != RX_ENC_LEGACY)
367 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
368 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
369 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
370 else if (rate)
371 channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
372 else
373 channel_flags |= IEEE80211_CHAN_2GHZ;
374 put_unaligned_le16(channel_flags, pos);
375 pos += 2;
376
377 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
378 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
379 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
380 *pos = status->signal;
381 rthdr->it_present |=
382 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
383 pos++;
384 }
385
386 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
387
388 if (!status->chains) {
389 /* IEEE80211_RADIOTAP_ANTENNA */
390 *pos = status->antenna;
391 pos++;
392 }
393
394 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
395
396 /* IEEE80211_RADIOTAP_RX_FLAGS */
397 /* ensure 2 byte alignment for the 2 byte field as required */
398 if ((pos - (u8 *)rthdr) & 1)
399 *pos++ = 0;
400 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
401 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
402 put_unaligned_le16(rx_flags, pos);
403 pos += 2;
404
405 if (status->encoding == RX_ENC_HT) {
406 unsigned int stbc;
407
408 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
409 *pos++ = local->hw.radiotap_mcs_details;
410 *pos = 0;
411 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
412 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
413 if (status->bw == RATE_INFO_BW_40)
414 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
415 if (status->enc_flags & RX_ENC_FLAG_HT_GF)
416 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
417 if (status->enc_flags & RX_ENC_FLAG_LDPC)
418 *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
419 stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
420 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
421 pos++;
422 *pos++ = status->rate_idx;
423 }
424
425 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
426 u16 flags = 0;
427
428 /* ensure 4 byte alignment */
429 while ((pos - (u8 *)rthdr) & 3)
430 pos++;
431 rthdr->it_present |=
432 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
433 put_unaligned_le32(status->ampdu_reference, pos);
434 pos += 4;
435 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
436 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
437 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
438 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
439 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
440 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
441 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
442 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
443 put_unaligned_le16(flags, pos);
444 pos += 2;
445 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
446 *pos++ = status->ampdu_delimiter_crc;
447 else
448 *pos++ = 0;
449 *pos++ = 0;
450 }
451
452 if (status->encoding == RX_ENC_VHT) {
453 u16 known = local->hw.radiotap_vht_details;
454
455 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
456 put_unaligned_le16(known, pos);
457 pos += 2;
458 /* flags */
459 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
460 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
461 /* in VHT, STBC is binary */
462 if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
463 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
464 if (status->enc_flags & RX_ENC_FLAG_BF)
465 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
466 pos++;
467 /* bandwidth */
468 switch (status->bw) {
469 case RATE_INFO_BW_80:
470 *pos++ = 4;
471 break;
472 case RATE_INFO_BW_160:
473 *pos++ = 11;
474 break;
475 case RATE_INFO_BW_40:
476 *pos++ = 1;
477 break;
478 default:
479 *pos++ = 0;
480 }
481 /* MCS/NSS */
482 *pos = (status->rate_idx << 4) | status->nss;
483 pos += 4;
484 /* coding field */
485 if (status->enc_flags & RX_ENC_FLAG_LDPC)
486 *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
487 pos++;
488 /* group ID */
489 pos++;
490 /* partial_aid */
491 pos += 2;
492 }
493
494 if (local->hw.radiotap_timestamp.units_pos >= 0) {
495 u16 accuracy = 0;
496 u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
497
498 rthdr->it_present |=
499 cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
500
501 /* ensure 8 byte alignment */
502 while ((pos - (u8 *)rthdr) & 7)
503 pos++;
504
505 put_unaligned_le64(status->device_timestamp, pos);
506 pos += sizeof(u64);
507
508 if (local->hw.radiotap_timestamp.accuracy >= 0) {
509 accuracy = local->hw.radiotap_timestamp.accuracy;
510 flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
511 }
512 put_unaligned_le16(accuracy, pos);
513 pos += sizeof(u16);
514
515 *pos++ = local->hw.radiotap_timestamp.units_pos;
516 *pos++ = flags;
517 }
518
519 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
520 *pos++ = status->chain_signal[chain];
521 *pos++ = chain;
522 }
523
524 if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
525 /* ensure 2 byte alignment for the vendor field as required */
526 if ((pos - (u8 *)rthdr) & 1)
527 *pos++ = 0;
528 *pos++ = rtap.oui[0];
529 *pos++ = rtap.oui[1];
530 *pos++ = rtap.oui[2];
531 *pos++ = rtap.subns;
532 put_unaligned_le16(rtap.len, pos);
533 pos += 2;
534 /* align the actual payload as requested */
535 while ((pos - (u8 *)rthdr) & (rtap.align - 1))
536 *pos++ = 0;
537 /* data (and possible padding) already follows */
538 }
539}
540
541static struct sk_buff *
542ieee80211_make_monitor_skb(struct ieee80211_local *local,
543 struct sk_buff **origskb,
544 struct ieee80211_rate *rate,
545 int rtap_vendor_space, bool use_origskb)
546{
547 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
548 int rt_hdrlen, needed_headroom;
549 struct sk_buff *skb;
550
551 /* room for the radiotap header based on driver features */
552 rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
553 needed_headroom = rt_hdrlen - rtap_vendor_space;
554
555 if (use_origskb) {
556 /* only need to expand headroom if necessary */
557 skb = *origskb;
558 *origskb = NULL;
559
560 /*
561 * This shouldn't trigger often because most devices have an
562 * RX header they pull before we get here, and that should
563 * be big enough for our radiotap information. We should
564 * probably export the length to drivers so that we can have
565 * them allocate enough headroom to start with.
566 */
567 if (skb_headroom(skb) < needed_headroom &&
568 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
569 dev_kfree_skb(skb);
570 return NULL;
571 }
572 } else {
573 /*
574 * Need to make a copy and possibly remove radiotap header
575 * and FCS from the original.
576 */
577 skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
578
579 if (!skb)
580 return NULL;
581 }
582
583 /* prepend radiotap information */
584 ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
585
586 skb_reset_mac_header(skb);
587 skb->ip_summed = CHECKSUM_UNNECESSARY;
588 skb->pkt_type = PACKET_OTHERHOST;
589 skb->protocol = htons(ETH_P_802_2);
590
591 return skb;
592}
593
594/*
595 * This function copies a received frame to all monitor interfaces and
596 * returns a cleaned-up SKB that no longer includes the FCS nor the
597 * radiotap header the driver might have added.
598 */
599static struct sk_buff *
600ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
601 struct ieee80211_rate *rate)
602{
603 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
604 struct ieee80211_sub_if_data *sdata;
605 struct sk_buff *monskb = NULL;
606 int present_fcs_len = 0;
607 unsigned int rtap_vendor_space = 0;
608 struct ieee80211_sub_if_data *monitor_sdata =
609 rcu_dereference(local->monitor_sdata);
610 bool only_monitor = false;
611
612 if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
613 struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
614
615 rtap_vendor_space = sizeof(*rtap) + rtap->len + rtap->pad;
616 }
617
618 /*
619 * First, we may need to make a copy of the skb because
620 * (1) we need to modify it for radiotap (if not present), and
621 * (2) the other RX handlers will modify the skb we got.
622 *
623 * We don't need to, of course, if we aren't going to return
624 * the SKB because it has a bad FCS/PLCP checksum.
625 */
626
627 if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
628 if (unlikely(origskb->len <= FCS_LEN)) {
629 /* driver bug */
630 WARN_ON(1);
631 dev_kfree_skb(origskb);
632 return NULL;
633 }
634 present_fcs_len = FCS_LEN;
635 }
636
637 /* ensure hdr->frame_control and vendor radiotap data are in skb head */
638 if (!pskb_may_pull(origskb, 2 + rtap_vendor_space)) {
639 dev_kfree_skb(origskb);
640 return NULL;
641 }
642
643 only_monitor = should_drop_frame(origskb, present_fcs_len,
644 rtap_vendor_space);
645
646 if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
647 if (only_monitor) {
648 dev_kfree_skb(origskb);
649 return NULL;
650 }
651
652 remove_monitor_info(origskb, present_fcs_len,
653 rtap_vendor_space);
654 return origskb;
655 }
656
657 ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_vendor_space);
658
659 list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
660 bool last_monitor = list_is_last(&sdata->u.mntr.list,
661 &local->mon_list);
662
663 if (!monskb)
664 monskb = ieee80211_make_monitor_skb(local, &origskb,
665 rate,
666 rtap_vendor_space,
667 only_monitor &&
668 last_monitor);
669
670 if (monskb) {
671 struct sk_buff *skb;
672
673 if (last_monitor) {
674 skb = monskb;
675 monskb = NULL;
676 } else {
677 skb = skb_clone(monskb, GFP_ATOMIC);
678 }
679
680 if (skb) {
681 skb->dev = sdata->dev;
682 ieee80211_rx_stats(skb->dev, skb->len);
683 netif_receive_skb(skb);
684 }
685 }
686
687 if (last_monitor)
688 break;
689 }
690
691 /* this happens if last_monitor was erroneously false */
692 dev_kfree_skb(monskb);
693
694 /* ditto */
695 if (!origskb)
696 return NULL;
697
698 remove_monitor_info(origskb, present_fcs_len, rtap_vendor_space);
699 return origskb;
700}
701
702static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
703{
704 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
705 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
706 int tid, seqno_idx, security_idx;
707
708 /* does the frame have a qos control field? */
709 if (ieee80211_is_data_qos(hdr->frame_control)) {
710 u8 *qc = ieee80211_get_qos_ctl(hdr);
711 /* frame has qos control */
712 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
713 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
714 status->rx_flags |= IEEE80211_RX_AMSDU;
715
716 seqno_idx = tid;
717 security_idx = tid;
718 } else {
719 /*
720 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
721 *
722 * Sequence numbers for management frames, QoS data
723 * frames with a broadcast/multicast address in the
724 * Address 1 field, and all non-QoS data frames sent
725 * by QoS STAs are assigned using an additional single
726 * modulo-4096 counter, [...]
727 *
728 * We also use that counter for non-QoS STAs.
729 */
730 seqno_idx = IEEE80211_NUM_TIDS;
731 security_idx = 0;
732 if (ieee80211_is_mgmt(hdr->frame_control))
733 security_idx = IEEE80211_NUM_TIDS;
734 tid = 0;
735 }
736
737 rx->seqno_idx = seqno_idx;
738 rx->security_idx = security_idx;
739 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
740 * For now, set skb->priority to 0 for other cases. */
741 rx->skb->priority = (tid > 7) ? 0 : tid;
742}
743
744/**
745 * DOC: Packet alignment
746 *
747 * Drivers always need to pass packets that are aligned to two-byte boundaries
748 * to the stack.
749 *
750 * Additionally, should, if possible, align the payload data in a way that
751 * guarantees that the contained IP header is aligned to a four-byte
752 * boundary. In the case of regular frames, this simply means aligning the
753 * payload to a four-byte boundary (because either the IP header is directly
754 * contained, or IV/RFC1042 headers that have a length divisible by four are
755 * in front of it). If the payload data is not properly aligned and the
756 * architecture doesn't support efficient unaligned operations, mac80211
757 * will align the data.
758 *
759 * With A-MSDU frames, however, the payload data address must yield two modulo
760 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
761 * push the IP header further back to a multiple of four again. Thankfully, the
762 * specs were sane enough this time around to require padding each A-MSDU
763 * subframe to a length that is a multiple of four.
764 *
765 * Padding like Atheros hardware adds which is between the 802.11 header and
766 * the payload is not supported, the driver is required to move the 802.11
767 * header to be directly in front of the payload in that case.
768 */
769static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
770{
771#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
772 WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
773#endif
774}
775
776
777/* rx handlers */
778
779static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
780{
781 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
782
783 if (is_multicast_ether_addr(hdr->addr1))
784 return 0;
785
786 return ieee80211_is_robust_mgmt_frame(skb);
787}
788
789
790static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
791{
792 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
793
794 if (!is_multicast_ether_addr(hdr->addr1))
795 return 0;
796
797 return ieee80211_is_robust_mgmt_frame(skb);
798}
799
800
801/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
802static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
803{
804 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
805 struct ieee80211_mmie *mmie;
806 struct ieee80211_mmie_16 *mmie16;
807
808 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
809 return -1;
810
811 if (!ieee80211_is_robust_mgmt_frame(skb))
812 return -1; /* not a robust management frame */
813
814 mmie = (struct ieee80211_mmie *)
815 (skb->data + skb->len - sizeof(*mmie));
816 if (mmie->element_id == WLAN_EID_MMIE &&
817 mmie->length == sizeof(*mmie) - 2)
818 return le16_to_cpu(mmie->key_id);
819
820 mmie16 = (struct ieee80211_mmie_16 *)
821 (skb->data + skb->len - sizeof(*mmie16));
822 if (skb->len >= 24 + sizeof(*mmie16) &&
823 mmie16->element_id == WLAN_EID_MMIE &&
824 mmie16->length == sizeof(*mmie16) - 2)
825 return le16_to_cpu(mmie16->key_id);
826
827 return -1;
828}
829
830static int ieee80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
831 struct sk_buff *skb)
832{
833 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
834 __le16 fc;
835 int hdrlen;
836 u8 keyid;
837
838 fc = hdr->frame_control;
839 hdrlen = ieee80211_hdrlen(fc);
840
841 if (skb->len < hdrlen + cs->hdr_len)
842 return -EINVAL;
843
844 skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
845 keyid &= cs->key_idx_mask;
846 keyid >>= cs->key_idx_shift;
847
848 return keyid;
849}
850
851static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
852{
853 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
854 char *dev_addr = rx->sdata->vif.addr;
855
856 if (ieee80211_is_data(hdr->frame_control)) {
857 if (is_multicast_ether_addr(hdr->addr1)) {
858 if (ieee80211_has_tods(hdr->frame_control) ||
859 !ieee80211_has_fromds(hdr->frame_control))
860 return RX_DROP_MONITOR;
861 if (ether_addr_equal(hdr->addr3, dev_addr))
862 return RX_DROP_MONITOR;
863 } else {
864 if (!ieee80211_has_a4(hdr->frame_control))
865 return RX_DROP_MONITOR;
866 if (ether_addr_equal(hdr->addr4, dev_addr))
867 return RX_DROP_MONITOR;
868 }
869 }
870
871 /* If there is not an established peer link and this is not a peer link
872 * establisment frame, beacon or probe, drop the frame.
873 */
874
875 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
876 struct ieee80211_mgmt *mgmt;
877
878 if (!ieee80211_is_mgmt(hdr->frame_control))
879 return RX_DROP_MONITOR;
880
881 if (ieee80211_is_action(hdr->frame_control)) {
882 u8 category;
883
884 /* make sure category field is present */
885 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
886 return RX_DROP_MONITOR;
887
888 mgmt = (struct ieee80211_mgmt *)hdr;
889 category = mgmt->u.action.category;
890 if (category != WLAN_CATEGORY_MESH_ACTION &&
891 category != WLAN_CATEGORY_SELF_PROTECTED)
892 return RX_DROP_MONITOR;
893 return RX_CONTINUE;
894 }
895
896 if (ieee80211_is_probe_req(hdr->frame_control) ||
897 ieee80211_is_probe_resp(hdr->frame_control) ||
898 ieee80211_is_beacon(hdr->frame_control) ||
899 ieee80211_is_auth(hdr->frame_control))
900 return RX_CONTINUE;
901
902 return RX_DROP_MONITOR;
903 }
904
905 return RX_CONTINUE;
906}
907
908static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
909 int index)
910{
911 struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
912 struct sk_buff *tail = skb_peek_tail(frames);
913 struct ieee80211_rx_status *status;
914
915 if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
916 return true;
917
918 if (!tail)
919 return false;
920
921 status = IEEE80211_SKB_RXCB(tail);
922 if (status->flag & RX_FLAG_AMSDU_MORE)
923 return false;
924
925 return true;
926}
927
928static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
929 struct tid_ampdu_rx *tid_agg_rx,
930 int index,
931 struct sk_buff_head *frames)
932{
933 struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
934 struct sk_buff *skb;
935 struct ieee80211_rx_status *status;
936
937 lockdep_assert_held(&tid_agg_rx->reorder_lock);
938
939 if (skb_queue_empty(skb_list))
940 goto no_frame;
941
942 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
943 __skb_queue_purge(skb_list);
944 goto no_frame;
945 }
946
947 /* release frames from the reorder ring buffer */
948 tid_agg_rx->stored_mpdu_num--;
949 while ((skb = __skb_dequeue(skb_list))) {
950 status = IEEE80211_SKB_RXCB(skb);
951 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
952 __skb_queue_tail(frames, skb);
953 }
954
955no_frame:
956 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
957 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
958}
959
960static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
961 struct tid_ampdu_rx *tid_agg_rx,
962 u16 head_seq_num,
963 struct sk_buff_head *frames)
964{
965 int index;
966
967 lockdep_assert_held(&tid_agg_rx->reorder_lock);
968
969 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
970 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
971 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
972 frames);
973 }
974}
975
976/*
977 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
978 * the skb was added to the buffer longer than this time ago, the earlier
979 * frames that have not yet been received are assumed to be lost and the skb
980 * can be released for processing. This may also release other skb's from the
981 * reorder buffer if there are no additional gaps between the frames.
982 *
983 * Callers must hold tid_agg_rx->reorder_lock.
984 */
985#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
986
987static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
988 struct tid_ampdu_rx *tid_agg_rx,
989 struct sk_buff_head *frames)
990{
991 int index, i, j;
992
993 lockdep_assert_held(&tid_agg_rx->reorder_lock);
994
995 /* release the buffer until next missing frame */
996 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
997 if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
998 tid_agg_rx->stored_mpdu_num) {
999 /*
1000 * No buffers ready to be released, but check whether any
1001 * frames in the reorder buffer have timed out.
1002 */
1003 int skipped = 1;
1004 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
1005 j = (j + 1) % tid_agg_rx->buf_size) {
1006 if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
1007 skipped++;
1008 continue;
1009 }
1010 if (skipped &&
1011 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
1012 HT_RX_REORDER_BUF_TIMEOUT))
1013 goto set_release_timer;
1014
1015 /* don't leave incomplete A-MSDUs around */
1016 for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
1017 i = (i + 1) % tid_agg_rx->buf_size)
1018 __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
1019
1020 ht_dbg_ratelimited(sdata,
1021 "release an RX reorder frame due to timeout on earlier frames\n");
1022 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
1023 frames);
1024
1025 /*
1026 * Increment the head seq# also for the skipped slots.
1027 */
1028 tid_agg_rx->head_seq_num =
1029 (tid_agg_rx->head_seq_num +
1030 skipped) & IEEE80211_SN_MASK;
1031 skipped = 0;
1032 }
1033 } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1034 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
1035 frames);
1036 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1037 }
1038
1039 if (tid_agg_rx->stored_mpdu_num) {
1040 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
1041
1042 for (; j != (index - 1) % tid_agg_rx->buf_size;
1043 j = (j + 1) % tid_agg_rx->buf_size) {
1044 if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
1045 break;
1046 }
1047
1048 set_release_timer:
1049
1050 if (!tid_agg_rx->removed)
1051 mod_timer(&tid_agg_rx->reorder_timer,
1052 tid_agg_rx->reorder_time[j] + 1 +
1053 HT_RX_REORDER_BUF_TIMEOUT);
1054 } else {
1055 del_timer(&tid_agg_rx->reorder_timer);
1056 }
1057}
1058
1059/*
1060 * As this function belongs to the RX path it must be under
1061 * rcu_read_lock protection. It returns false if the frame
1062 * can be processed immediately, true if it was consumed.
1063 */
1064static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1065 struct tid_ampdu_rx *tid_agg_rx,
1066 struct sk_buff *skb,
1067 struct sk_buff_head *frames)
1068{
1069 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1070 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1071 u16 sc = le16_to_cpu(hdr->seq_ctrl);
1072 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
1073 u16 head_seq_num, buf_size;
1074 int index;
1075 bool ret = true;
1076
1077 spin_lock(&tid_agg_rx->reorder_lock);
1078
1079 /*
1080 * Offloaded BA sessions have no known starting sequence number so pick
1081 * one from first Rxed frame for this tid after BA was started.
1082 */
1083 if (unlikely(tid_agg_rx->auto_seq)) {
1084 tid_agg_rx->auto_seq = false;
1085 tid_agg_rx->ssn = mpdu_seq_num;
1086 tid_agg_rx->head_seq_num = mpdu_seq_num;
1087 }
1088
1089 buf_size = tid_agg_rx->buf_size;
1090 head_seq_num = tid_agg_rx->head_seq_num;
1091
1092 /*
1093 * If the current MPDU's SN is smaller than the SSN, it shouldn't
1094 * be reordered.
1095 */
1096 if (unlikely(!tid_agg_rx->started)) {
1097 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1098 ret = false;
1099 goto out;
1100 }
1101 tid_agg_rx->started = true;
1102 }
1103
1104 /* frame with out of date sequence number */
1105 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1106 dev_kfree_skb(skb);
1107 goto out;
1108 }
1109
1110 /*
1111 * If frame the sequence number exceeds our buffering window
1112 * size release some previous frames to make room for this one.
1113 */
1114 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
1115 head_seq_num = ieee80211_sn_inc(
1116 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1117 /* release stored frames up to new head to stack */
1118 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
1119 head_seq_num, frames);
1120 }
1121
1122 /* Now the new frame is always in the range of the reordering buffer */
1123
1124 index = mpdu_seq_num % tid_agg_rx->buf_size;
1125
1126 /* check if we already stored this frame */
1127 if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
1128 dev_kfree_skb(skb);
1129 goto out;
1130 }
1131
1132 /*
1133 * If the current MPDU is in the right order and nothing else
1134 * is stored we can process it directly, no need to buffer it.
1135 * If it is first but there's something stored, we may be able
1136 * to release frames after this one.
1137 */
1138 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
1139 tid_agg_rx->stored_mpdu_num == 0) {
1140 if (!(status->flag & RX_FLAG_AMSDU_MORE))
1141 tid_agg_rx->head_seq_num =
1142 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1143 ret = false;
1144 goto out;
1145 }
1146
1147 /* put the frame in the reordering buffer */
1148 __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
1149 if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1150 tid_agg_rx->reorder_time[index] = jiffies;
1151 tid_agg_rx->stored_mpdu_num++;
1152 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1153 }
1154
1155 out:
1156 spin_unlock(&tid_agg_rx->reorder_lock);
1157 return ret;
1158}
1159
1160/*
1161 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
1162 * true if the MPDU was buffered, false if it should be processed.
1163 */
1164static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
1165 struct sk_buff_head *frames)
1166{
1167 struct sk_buff *skb = rx->skb;
1168 struct ieee80211_local *local = rx->local;
1169 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1170 struct sta_info *sta = rx->sta;
1171 struct tid_ampdu_rx *tid_agg_rx;
1172 u16 sc;
1173 u8 tid, ack_policy;
1174
1175 if (!ieee80211_is_data_qos(hdr->frame_control) ||
1176 is_multicast_ether_addr(hdr->addr1))
1177 goto dont_reorder;
1178
1179 /*
1180 * filter the QoS data rx stream according to
1181 * STA/TID and check if this STA/TID is on aggregation
1182 */
1183
1184 if (!sta)
1185 goto dont_reorder;
1186
1187 ack_policy = *ieee80211_get_qos_ctl(hdr) &
1188 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1189 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1190
1191 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
1192 if (!tid_agg_rx) {
1193 if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1194 !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
1195 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
1196 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
1197 WLAN_BACK_RECIPIENT,
1198 WLAN_REASON_QSTA_REQUIRE_SETUP);
1199 goto dont_reorder;
1200 }
1201
1202 /* qos null data frames are excluded */
1203 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
1204 goto dont_reorder;
1205
1206 /* not part of a BA session */
1207 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
1208 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
1209 goto dont_reorder;
1210
1211 /* new, potentially un-ordered, ampdu frame - process it */
1212
1213 /* reset session timer */
1214 if (tid_agg_rx->timeout)
1215 tid_agg_rx->last_rx = jiffies;
1216
1217 /* if this mpdu is fragmented - terminate rx aggregation session */
1218 sc = le16_to_cpu(hdr->seq_ctrl);
1219 if (sc & IEEE80211_SCTL_FRAG) {
1220 skb_queue_tail(&rx->sdata->skb_queue, skb);
1221 ieee80211_queue_work(&local->hw, &rx->sdata->work);
1222 return;
1223 }
1224
1225 /*
1226 * No locking needed -- we will only ever process one
1227 * RX packet at a time, and thus own tid_agg_rx. All
1228 * other code manipulating it needs to (and does) make
1229 * sure that we cannot get to it any more before doing
1230 * anything with it.
1231 */
1232 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
1233 frames))
1234 return;
1235
1236 dont_reorder:
1237 __skb_queue_tail(frames, skb);
1238}
1239
1240static ieee80211_rx_result debug_noinline
1241ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
1242{
1243 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1244 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1245
1246 if (status->flag & RX_FLAG_DUP_VALIDATED)
1247 return RX_CONTINUE;
1248
1249 /*
1250 * Drop duplicate 802.11 retransmissions
1251 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
1252 */
1253
1254 if (rx->skb->len < 24)
1255 return RX_CONTINUE;
1256
1257 if (ieee80211_is_ctl(hdr->frame_control) ||
1258 ieee80211_is_any_nullfunc(hdr->frame_control) ||
1259 is_multicast_ether_addr(hdr->addr1))
1260 return RX_CONTINUE;
1261
1262 if (!rx->sta)
1263 return RX_CONTINUE;
1264
1265 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
1266 rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
1267 I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
1268 rx->sta->rx_stats.num_duplicates++;
1269 return RX_DROP_UNUSABLE;
1270 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
1271 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1272 }
1273
1274 return RX_CONTINUE;
1275}
1276
1277static ieee80211_rx_result debug_noinline
1278ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
1279{
1280 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
1281
1282 /* Drop disallowed frame classes based on STA auth/assoc state;
1283 * IEEE 802.11, Chap 5.5.
1284 *
1285 * mac80211 filters only based on association state, i.e. it drops
1286 * Class 3 frames from not associated stations. hostapd sends
1287 * deauth/disassoc frames when needed. In addition, hostapd is
1288 * responsible for filtering on both auth and assoc states.
1289 */
1290
1291 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1292 return ieee80211_rx_mesh_check(rx);
1293
1294 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
1295 ieee80211_is_pspoll(hdr->frame_control)) &&
1296 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1297 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
1298 rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
1299 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
1300 /*
1301 * accept port control frames from the AP even when it's not
1302 * yet marked ASSOC to prevent a race where we don't set the
1303 * assoc bit quickly enough before it sends the first frame
1304 */
1305 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1306 ieee80211_is_data_present(hdr->frame_control)) {
1307 unsigned int hdrlen;
1308 __be16 ethertype;
1309
1310 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1311
1312 if (rx->skb->len < hdrlen + 8)
1313 return RX_DROP_MONITOR;
1314
1315 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1316 if (ethertype == rx->sdata->control_port_protocol)
1317 return RX_CONTINUE;
1318 }
1319
1320 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1321 cfg80211_rx_spurious_frame(rx->sdata->dev,
1322 hdr->addr2,
1323 GFP_ATOMIC))
1324 return RX_DROP_UNUSABLE;
1325
1326 return RX_DROP_MONITOR;
1327 }
1328
1329 return RX_CONTINUE;
1330}
1331
1332
1333static ieee80211_rx_result debug_noinline
1334ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1335{
1336 struct ieee80211_local *local;
1337 struct ieee80211_hdr *hdr;
1338 struct sk_buff *skb;
1339
1340 local = rx->local;
1341 skb = rx->skb;
1342 hdr = (struct ieee80211_hdr *) skb->data;
1343
1344 if (!local->pspolling)
1345 return RX_CONTINUE;
1346
1347 if (!ieee80211_has_fromds(hdr->frame_control))
1348 /* this is not from AP */
1349 return RX_CONTINUE;
1350
1351 if (!ieee80211_is_data(hdr->frame_control))
1352 return RX_CONTINUE;
1353
1354 if (!ieee80211_has_moredata(hdr->frame_control)) {
1355 /* AP has no more frames buffered for us */
1356 local->pspolling = false;
1357 return RX_CONTINUE;
1358 }
1359
1360 /* more data bit is set, let's request a new frame from the AP */
1361 ieee80211_send_pspoll(local, rx->sdata);
1362
1363 return RX_CONTINUE;
1364}
1365
1366static void sta_ps_start(struct sta_info *sta)
1367{
1368 struct ieee80211_sub_if_data *sdata = sta->sdata;
1369 struct ieee80211_local *local = sdata->local;
1370 struct ps_data *ps;
1371 int tid;
1372
1373 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1374 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1375 ps = &sdata->bss->ps;
1376 else
1377 return;
1378
1379 atomic_inc(&ps->num_sta_ps);
1380 set_sta_flag(sta, WLAN_STA_PS_STA);
1381 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1382 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1383 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1384 sta->sta.addr, sta->sta.aid);
1385
1386 ieee80211_clear_fast_xmit(sta);
1387
1388 if (!sta->sta.txq[0])
1389 return;
1390
1391 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1392 if (txq_has_queue(sta->sta.txq[tid]))
1393 set_bit(tid, &sta->txq_buffered_tids);
1394 else
1395 clear_bit(tid, &sta->txq_buffered_tids);
1396 }
1397}
1398
1399static void sta_ps_end(struct sta_info *sta)
1400{
1401 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1402 sta->sta.addr, sta->sta.aid);
1403
1404 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
1405 /*
1406 * Clear the flag only if the other one is still set
1407 * so that the TX path won't start TX'ing new frames
1408 * directly ... In the case that the driver flag isn't
1409 * set ieee80211_sta_ps_deliver_wakeup() will clear it.
1410 */
1411 clear_sta_flag(sta, WLAN_STA_PS_STA);
1412 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1413 sta->sta.addr, sta->sta.aid);
1414 return;
1415 }
1416
1417 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1418 clear_sta_flag(sta, WLAN_STA_PS_STA);
1419 ieee80211_sta_ps_deliver_wakeup(sta);
1420}
1421
1422int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
1423{
1424 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1425 bool in_ps;
1426
1427 WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
1428
1429 /* Don't let the same PS state be set twice */
1430 in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
1431 if ((start && in_ps) || (!start && !in_ps))
1432 return -EINVAL;
1433
1434 if (start)
1435 sta_ps_start(sta);
1436 else
1437 sta_ps_end(sta);
1438
1439 return 0;
1440}
1441EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1442
1443void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
1444{
1445 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1446
1447 if (test_sta_flag(sta, WLAN_STA_SP))
1448 return;
1449
1450 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1451 ieee80211_sta_ps_deliver_poll_response(sta);
1452 else
1453 set_sta_flag(sta, WLAN_STA_PSPOLL);
1454}
1455EXPORT_SYMBOL(ieee80211_sta_pspoll);
1456
1457void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
1458{
1459 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1460 int ac = ieee80211_ac_from_tid(tid);
1461
1462 /*
1463 * If this AC is not trigger-enabled do nothing unless the
1464 * driver is calling us after it already checked.
1465 *
1466 * NB: This could/should check a separate bitmap of trigger-
1467 * enabled queues, but for now we only implement uAPSD w/o
1468 * TSPEC changes to the ACs, so they're always the same.
1469 */
1470 if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
1471 tid != IEEE80211_NUM_TIDS)
1472 return;
1473
1474 /* if we are in a service period, do nothing */
1475 if (test_sta_flag(sta, WLAN_STA_SP))
1476 return;
1477
1478 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1479 ieee80211_sta_ps_deliver_uapsd(sta);
1480 else
1481 set_sta_flag(sta, WLAN_STA_UAPSD);
1482}
1483EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
1484
1485static ieee80211_rx_result debug_noinline
1486ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1487{
1488 struct ieee80211_sub_if_data *sdata = rx->sdata;
1489 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1490 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1491
1492 if (!rx->sta)
1493 return RX_CONTINUE;
1494
1495 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1496 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1497 return RX_CONTINUE;
1498
1499 /*
1500 * The device handles station powersave, so don't do anything about
1501 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1502 * it to mac80211 since they're handled.)
1503 */
1504 if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
1505 return RX_CONTINUE;
1506
1507 /*
1508 * Don't do anything if the station isn't already asleep. In
1509 * the uAPSD case, the station will probably be marked asleep,
1510 * in the PS-Poll case the station must be confused ...
1511 */
1512 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
1513 return RX_CONTINUE;
1514
1515 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
1516 ieee80211_sta_pspoll(&rx->sta->sta);
1517
1518 /* Free PS Poll skb here instead of returning RX_DROP that would
1519 * count as an dropped frame. */
1520 dev_kfree_skb(rx->skb);
1521
1522 return RX_QUEUED;
1523 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1524 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1525 ieee80211_has_pm(hdr->frame_control) &&
1526 (ieee80211_is_data_qos(hdr->frame_control) ||
1527 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1528 u8 tid;
1529
1530 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1531
1532 ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
1533 }
1534
1535 return RX_CONTINUE;
1536}
1537
1538static ieee80211_rx_result debug_noinline
1539ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
1540{
1541 struct sta_info *sta = rx->sta;
1542 struct sk_buff *skb = rx->skb;
1543 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1544 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1545 int i;
1546
1547 if (!sta)
1548 return RX_CONTINUE;
1549
1550 /*
1551 * Update last_rx only for IBSS packets which are for the current
1552 * BSSID and for station already AUTHORIZED to avoid keeping the
1553 * current IBSS network alive in cases where other STAs start
1554 * using different BSSID. This will also give the station another
1555 * chance to restart the authentication/authorization in case
1556 * something went wrong the first time.
1557 */
1558 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
1559 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
1560 NL80211_IFTYPE_ADHOC);
1561 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1562 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
1563 sta->rx_stats.last_rx = jiffies;
1564 if (ieee80211_is_data(hdr->frame_control) &&
1565 !is_multicast_ether_addr(hdr->addr1))
1566 sta->rx_stats.last_rate =
1567 sta_stats_encode_rate(status);
1568 }
1569 } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
1570 sta->rx_stats.last_rx = jiffies;
1571 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1572 /*
1573 * Mesh beacons will update last_rx when if they are found to
1574 * match the current local configuration when processed.
1575 */
1576 sta->rx_stats.last_rx = jiffies;
1577 if (ieee80211_is_data(hdr->frame_control))
1578 sta->rx_stats.last_rate = sta_stats_encode_rate(status);
1579 }
1580
1581 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1582 ieee80211_sta_rx_notify(rx->sdata, hdr);
1583
1584 sta->rx_stats.fragments++;
1585
1586 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
1587 sta->rx_stats.bytes += rx->skb->len;
1588 u64_stats_update_end(&rx->sta->rx_stats.syncp);
1589
1590 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1591 sta->rx_stats.last_signal = status->signal;
1592 ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
1593 }
1594
1595 if (status->chains) {
1596 sta->rx_stats.chains = status->chains;
1597 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1598 int signal = status->chain_signal[i];
1599
1600 if (!(status->chains & BIT(i)))
1601 continue;
1602
1603 sta->rx_stats.chain_signal_last[i] = signal;
1604 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
1605 -signal);
1606 }
1607 }
1608
1609 /*
1610 * Change STA power saving mode only at the end of a frame
1611 * exchange sequence.
1612 */
1613 if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
1614 !ieee80211_has_morefrags(hdr->frame_control) &&
1615 !ieee80211_is_back_req(hdr->frame_control) &&
1616 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1617 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1618 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1619 /*
1620 * PM bit is only checked in frames where it isn't reserved,
1621 * in AP mode it's reserved in non-bufferable management frames
1622 * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1623 * BAR frames should be ignored as specified in
1624 * IEEE 802.11-2012 10.2.1.2.
1625 */
1626 (!ieee80211_is_mgmt(hdr->frame_control) ||
1627 ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
1628 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
1629 if (!ieee80211_has_pm(hdr->frame_control))
1630 sta_ps_end(sta);
1631 } else {
1632 if (ieee80211_has_pm(hdr->frame_control))
1633 sta_ps_start(sta);
1634 }
1635 }
1636
1637 /* mesh power save support */
1638 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1639 ieee80211_mps_rx_h_sta_process(sta, hdr);
1640
1641 /*
1642 * Drop (qos-)data::nullfunc frames silently, since they
1643 * are used only to control station power saving mode.
1644 */
1645 if (ieee80211_is_any_nullfunc(hdr->frame_control)) {
1646 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
1647
1648 /*
1649 * If we receive a 4-addr nullfunc frame from a STA
1650 * that was not moved to a 4-addr STA vlan yet send
1651 * the event to userspace and for older hostapd drop
1652 * the frame to the monitor interface.
1653 */
1654 if (ieee80211_has_a4(hdr->frame_control) &&
1655 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1656 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1657 !rx->sdata->u.vlan.sta))) {
1658 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1659 cfg80211_rx_unexpected_4addr_frame(
1660 rx->sdata->dev, sta->sta.addr,
1661 GFP_ATOMIC);
1662 return RX_DROP_MONITOR;
1663 }
1664 /*
1665 * Update counter and free packet here to avoid
1666 * counting this as a dropped packed.
1667 */
1668 sta->rx_stats.packets++;
1669 dev_kfree_skb(rx->skb);
1670 return RX_QUEUED;
1671 }
1672
1673 return RX_CONTINUE;
1674} /* ieee80211_rx_h_sta_process */
1675
1676static ieee80211_rx_result debug_noinline
1677ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1678{
1679 struct sk_buff *skb = rx->skb;
1680 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1681 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1682 int keyidx;
1683 int hdrlen;
1684 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1685 struct ieee80211_key *sta_ptk = NULL;
1686 int mmie_keyidx = -1;
1687 __le16 fc;
1688 const struct ieee80211_cipher_scheme *cs = NULL;
1689
1690 /*
1691 * Key selection 101
1692 *
1693 * There are four types of keys:
1694 * - GTK (group keys)
1695 * - IGTK (group keys for management frames)
1696 * - PTK (pairwise keys)
1697 * - STK (station-to-station pairwise keys)
1698 *
1699 * When selecting a key, we have to distinguish between multicast
1700 * (including broadcast) and unicast frames, the latter can only
1701 * use PTKs and STKs while the former always use GTKs and IGTKs.
1702 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1703 * unicast frames can also use key indices like GTKs. Hence, if we
1704 * don't have a PTK/STK we check the key index for a WEP key.
1705 *
1706 * Note that in a regular BSS, multicast frames are sent by the
1707 * AP only, associated stations unicast the frame to the AP first
1708 * which then multicasts it on their behalf.
1709 *
1710 * There is also a slight problem in IBSS mode: GTKs are negotiated
1711 * with each station, that is something we don't currently handle.
1712 * The spec seems to expect that one negotiates the same key with
1713 * every station but there's no such requirement; VLANs could be
1714 * possible.
1715 */
1716
1717 /* start without a key */
1718 rx->key = NULL;
1719 fc = hdr->frame_control;
1720
1721 if (rx->sta) {
1722 int keyid = rx->sta->ptk_idx;
1723
1724 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1725 cs = rx->sta->cipher_scheme;
1726 keyid = ieee80211_get_cs_keyid(cs, rx->skb);
1727 if (unlikely(keyid < 0))
1728 return RX_DROP_UNUSABLE;
1729 }
1730 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1731 }
1732
1733 if (!ieee80211_has_protected(fc))
1734 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1735
1736 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1737 rx->key = sta_ptk;
1738 if ((status->flag & RX_FLAG_DECRYPTED) &&
1739 (status->flag & RX_FLAG_IV_STRIPPED))
1740 return RX_CONTINUE;
1741 /* Skip decryption if the frame is not protected. */
1742 if (!ieee80211_has_protected(fc))
1743 return RX_CONTINUE;
1744 } else if (mmie_keyidx >= 0) {
1745 /* Broadcast/multicast robust management frame / BIP */
1746 if ((status->flag & RX_FLAG_DECRYPTED) &&
1747 (status->flag & RX_FLAG_IV_STRIPPED))
1748 return RX_CONTINUE;
1749
1750 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1751 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1752 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1753 if (rx->sta) {
1754 if (ieee80211_is_group_privacy_action(skb) &&
1755 test_sta_flag(rx->sta, WLAN_STA_MFP))
1756 return RX_DROP_MONITOR;
1757
1758 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1759 }
1760 if (!rx->key)
1761 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1762 } else if (!ieee80211_has_protected(fc)) {
1763 /*
1764 * The frame was not protected, so skip decryption. However, we
1765 * need to set rx->key if there is a key that could have been
1766 * used so that the frame may be dropped if encryption would
1767 * have been expected.
1768 */
1769 struct ieee80211_key *key = NULL;
1770 struct ieee80211_sub_if_data *sdata = rx->sdata;
1771 int i;
1772
1773 if (ieee80211_is_mgmt(fc) &&
1774 is_multicast_ether_addr(hdr->addr1) &&
1775 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1776 rx->key = key;
1777 else {
1778 if (rx->sta) {
1779 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1780 key = rcu_dereference(rx->sta->gtk[i]);
1781 if (key)
1782 break;
1783 }
1784 }
1785 if (!key) {
1786 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1787 key = rcu_dereference(sdata->keys[i]);
1788 if (key)
1789 break;
1790 }
1791 }
1792 if (key)
1793 rx->key = key;
1794 }
1795 return RX_CONTINUE;
1796 } else {
1797 u8 keyid;
1798
1799 /*
1800 * The device doesn't give us the IV so we won't be
1801 * able to look up the key. That's ok though, we
1802 * don't need to decrypt the frame, we just won't
1803 * be able to keep statistics accurate.
1804 * Except for key threshold notifications, should
1805 * we somehow allow the driver to tell us which key
1806 * the hardware used if this flag is set?
1807 */
1808 if ((status->flag & RX_FLAG_DECRYPTED) &&
1809 (status->flag & RX_FLAG_IV_STRIPPED))
1810 return RX_CONTINUE;
1811
1812 hdrlen = ieee80211_hdrlen(fc);
1813
1814 if (cs) {
1815 keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
1816
1817 if (unlikely(keyidx < 0))
1818 return RX_DROP_UNUSABLE;
1819 } else {
1820 if (rx->skb->len < 8 + hdrlen)
1821 return RX_DROP_UNUSABLE; /* TODO: count this? */
1822 /*
1823 * no need to call ieee80211_wep_get_keyidx,
1824 * it verifies a bunch of things we've done already
1825 */
1826 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1827 keyidx = keyid >> 6;
1828 }
1829
1830 /* check per-station GTK first, if multicast packet */
1831 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1832 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1833
1834 /* if not found, try default key */
1835 if (!rx->key) {
1836 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1837
1838 /*
1839 * RSNA-protected unicast frames should always be
1840 * sent with pairwise or station-to-station keys,
1841 * but for WEP we allow using a key index as well.
1842 */
1843 if (rx->key &&
1844 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1845 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1846 !is_multicast_ether_addr(hdr->addr1))
1847 rx->key = NULL;
1848 }
1849 }
1850
1851 if (rx->key) {
1852 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1853 return RX_DROP_MONITOR;
1854
1855 /* TODO: add threshold stuff again */
1856 } else {
1857 return RX_DROP_MONITOR;
1858 }
1859
1860 switch (rx->key->conf.cipher) {
1861 case WLAN_CIPHER_SUITE_WEP40:
1862 case WLAN_CIPHER_SUITE_WEP104:
1863 result = ieee80211_crypto_wep_decrypt(rx);
1864 break;
1865 case WLAN_CIPHER_SUITE_TKIP:
1866 result = ieee80211_crypto_tkip_decrypt(rx);
1867 break;
1868 case WLAN_CIPHER_SUITE_CCMP:
1869 result = ieee80211_crypto_ccmp_decrypt(
1870 rx, IEEE80211_CCMP_MIC_LEN);
1871 break;
1872 case WLAN_CIPHER_SUITE_CCMP_256:
1873 result = ieee80211_crypto_ccmp_decrypt(
1874 rx, IEEE80211_CCMP_256_MIC_LEN);
1875 break;
1876 case WLAN_CIPHER_SUITE_AES_CMAC:
1877 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1878 break;
1879 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1880 result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
1881 break;
1882 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1883 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1884 result = ieee80211_crypto_aes_gmac_decrypt(rx);
1885 break;
1886 case WLAN_CIPHER_SUITE_GCMP:
1887 case WLAN_CIPHER_SUITE_GCMP_256:
1888 result = ieee80211_crypto_gcmp_decrypt(rx);
1889 break;
1890 default:
1891 result = ieee80211_crypto_hw_decrypt(rx);
1892 }
1893
1894 /* the hdr variable is invalid after the decrypt handlers */
1895
1896 /* either the frame has been decrypted or will be dropped */
1897 status->flag |= RX_FLAG_DECRYPTED;
1898
1899 return result;
1900}
1901
1902static inline struct ieee80211_fragment_entry *
1903ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1904 unsigned int frag, unsigned int seq, int rx_queue,
1905 struct sk_buff **skb)
1906{
1907 struct ieee80211_fragment_entry *entry;
1908
1909 entry = &sdata->fragments[sdata->fragment_next++];
1910 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1911 sdata->fragment_next = 0;
1912
1913 if (!skb_queue_empty(&entry->skb_list))
1914 __skb_queue_purge(&entry->skb_list);
1915
1916 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1917 *skb = NULL;
1918 entry->first_frag_time = jiffies;
1919 entry->seq = seq;
1920 entry->rx_queue = rx_queue;
1921 entry->last_frag = frag;
1922 entry->check_sequential_pn = false;
1923 entry->extra_len = 0;
1924
1925 return entry;
1926}
1927
1928static inline struct ieee80211_fragment_entry *
1929ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
1930 unsigned int frag, unsigned int seq,
1931 int rx_queue, struct ieee80211_hdr *hdr)
1932{
1933 struct ieee80211_fragment_entry *entry;
1934 int i, idx;
1935
1936 idx = sdata->fragment_next;
1937 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1938 struct ieee80211_hdr *f_hdr;
1939
1940 idx--;
1941 if (idx < 0)
1942 idx = IEEE80211_FRAGMENT_MAX - 1;
1943
1944 entry = &sdata->fragments[idx];
1945 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1946 entry->rx_queue != rx_queue ||
1947 entry->last_frag + 1 != frag)
1948 continue;
1949
1950 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
1951
1952 /*
1953 * Check ftype and addresses are equal, else check next fragment
1954 */
1955 if (((hdr->frame_control ^ f_hdr->frame_control) &
1956 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
1957 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1958 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
1959 continue;
1960
1961 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
1962 __skb_queue_purge(&entry->skb_list);
1963 continue;
1964 }
1965 return entry;
1966 }
1967
1968 return NULL;
1969}
1970
1971static ieee80211_rx_result debug_noinline
1972ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
1973{
1974 struct ieee80211_hdr *hdr;
1975 u16 sc;
1976 __le16 fc;
1977 unsigned int frag, seq;
1978 struct ieee80211_fragment_entry *entry;
1979 struct sk_buff *skb;
1980
1981 hdr = (struct ieee80211_hdr *)rx->skb->data;
1982 fc = hdr->frame_control;
1983
1984 if (ieee80211_is_ctl(fc))
1985 return RX_CONTINUE;
1986
1987 sc = le16_to_cpu(hdr->seq_ctrl);
1988 frag = sc & IEEE80211_SCTL_FRAG;
1989
1990 if (is_multicast_ether_addr(hdr->addr1)) {
1991 I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
1992 goto out_no_led;
1993 }
1994
1995 if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
1996 goto out;
1997
1998 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1999
2000 if (skb_linearize(rx->skb))
2001 return RX_DROP_UNUSABLE;
2002
2003 /*
2004 * skb_linearize() might change the skb->data and
2005 * previously cached variables (in this case, hdr) need to
2006 * be refreshed with the new data.
2007 */
2008 hdr = (struct ieee80211_hdr *)rx->skb->data;
2009 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2010
2011 if (frag == 0) {
2012 /* This is the first fragment of a new frame. */
2013 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
2014 rx->seqno_idx, &(rx->skb));
2015 if (rx->key &&
2016 (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
2017 rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
2018 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
2019 rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
2020 ieee80211_has_protected(fc)) {
2021 int queue = rx->security_idx;
2022
2023 /* Store CCMP/GCMP PN so that we can verify that the
2024 * next fragment has a sequential PN value.
2025 */
2026 entry->check_sequential_pn = true;
2027 memcpy(entry->last_pn,
2028 rx->key->u.ccmp.rx_pn[queue],
2029 IEEE80211_CCMP_PN_LEN);
2030 BUILD_BUG_ON(offsetof(struct ieee80211_key,
2031 u.ccmp.rx_pn) !=
2032 offsetof(struct ieee80211_key,
2033 u.gcmp.rx_pn));
2034 BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
2035 sizeof(rx->key->u.gcmp.rx_pn[queue]));
2036 BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
2037 IEEE80211_GCMP_PN_LEN);
2038 }
2039 return RX_QUEUED;
2040 }
2041
2042 /* This is a fragment for a frame that should already be pending in
2043 * fragment cache. Add this fragment to the end of the pending entry.
2044 */
2045 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
2046 rx->seqno_idx, hdr);
2047 if (!entry) {
2048 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2049 return RX_DROP_MONITOR;
2050 }
2051
2052 /* "The receiver shall discard MSDUs and MMPDUs whose constituent
2053 * MPDU PN values are not incrementing in steps of 1."
2054 * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
2055 * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
2056 */
2057 if (entry->check_sequential_pn) {
2058 int i;
2059 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
2060 int queue;
2061
2062 if (!rx->key ||
2063 (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
2064 rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
2065 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
2066 rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
2067 return RX_DROP_UNUSABLE;
2068 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
2069 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
2070 pn[i]++;
2071 if (pn[i])
2072 break;
2073 }
2074 queue = rx->security_idx;
2075 rpn = rx->key->u.ccmp.rx_pn[queue];
2076 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
2077 return RX_DROP_UNUSABLE;
2078 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
2079 }
2080
2081 skb_pull(rx->skb, ieee80211_hdrlen(fc));
2082 __skb_queue_tail(&entry->skb_list, rx->skb);
2083 entry->last_frag = frag;
2084 entry->extra_len += rx->skb->len;
2085 if (ieee80211_has_morefrags(fc)) {
2086 rx->skb = NULL;
2087 return RX_QUEUED;
2088 }
2089
2090 rx->skb = __skb_dequeue(&entry->skb_list);
2091 if (skb_tailroom(rx->skb) < entry->extra_len) {
2092 I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
2093 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
2094 GFP_ATOMIC))) {
2095 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
2096 __skb_queue_purge(&entry->skb_list);
2097 return RX_DROP_UNUSABLE;
2098 }
2099 }
2100 while ((skb = __skb_dequeue(&entry->skb_list))) {
2101 skb_put_data(rx->skb, skb->data, skb->len);
2102 dev_kfree_skb(skb);
2103 }
2104
2105 out:
2106 ieee80211_led_rx(rx->local);
2107 out_no_led:
2108 if (rx->sta)
2109 rx->sta->rx_stats.packets++;
2110 return RX_CONTINUE;
2111}
2112
2113static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
2114{
2115 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
2116 return -EACCES;
2117
2118 return 0;
2119}
2120
2121static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
2122{
2123 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
2124 struct sk_buff *skb = rx->skb;
2125 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2126
2127 /*
2128 * Pass through unencrypted frames if the hardware has
2129 * decrypted them already.
2130 */
2131 if (status->flag & RX_FLAG_DECRYPTED)
2132 return 0;
2133
2134 /* check mesh EAPOL frames first */
2135 if (unlikely(rx->sta && ieee80211_vif_is_mesh(&rx->sdata->vif) &&
2136 ieee80211_is_data(fc))) {
2137 struct ieee80211s_hdr *mesh_hdr;
2138 u16 hdr_len = ieee80211_hdrlen(fc);
2139 u16 ethertype_offset;
2140 __be16 ethertype;
2141
2142 if (!ether_addr_equal(hdr->addr1, rx->sdata->vif.addr))
2143 goto drop_check;
2144
2145 /* make sure fixed part of mesh header is there, also checks skb len */
2146 if (!pskb_may_pull(rx->skb, hdr_len + 6))
2147 goto drop_check;
2148
2149 mesh_hdr = (struct ieee80211s_hdr *)(skb->data + hdr_len);
2150 ethertype_offset = hdr_len + ieee80211_get_mesh_hdrlen(mesh_hdr) +
2151 sizeof(rfc1042_header);
2152
2153 if (skb_copy_bits(rx->skb, ethertype_offset, &ethertype, 2) == 0 &&
2154 ethertype == rx->sdata->control_port_protocol)
2155 return 0;
2156 }
2157
2158drop_check:
2159 /* Drop unencrypted frames if key is set. */
2160 if (unlikely(!ieee80211_has_protected(fc) &&
2161 !ieee80211_is_any_nullfunc(fc) &&
2162 ieee80211_is_data(fc) && rx->key))
2163 return -EACCES;
2164
2165 return 0;
2166}
2167
2168static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
2169{
2170 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2171 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2172 __le16 fc = hdr->frame_control;
2173
2174 /*
2175 * Pass through unencrypted frames if the hardware has
2176 * decrypted them already.
2177 */
2178 if (status->flag & RX_FLAG_DECRYPTED)
2179 return 0;
2180
2181 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
2182 if (unlikely(!ieee80211_has_protected(fc) &&
2183 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
2184 rx->key)) {
2185 if (ieee80211_is_deauth(fc) ||
2186 ieee80211_is_disassoc(fc))
2187 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2188 rx->skb->data,
2189 rx->skb->len);
2190 return -EACCES;
2191 }
2192 /* BIP does not use Protected field, so need to check MMIE */
2193 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
2194 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
2195 if (ieee80211_is_deauth(fc) ||
2196 ieee80211_is_disassoc(fc))
2197 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
2198 rx->skb->data,
2199 rx->skb->len);
2200 return -EACCES;
2201 }
2202 /*
2203 * When using MFP, Action frames are not allowed prior to
2204 * having configured keys.
2205 */
2206 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
2207 ieee80211_is_robust_mgmt_frame(rx->skb)))
2208 return -EACCES;
2209 }
2210
2211 return 0;
2212}
2213
2214static int
2215__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
2216{
2217 struct ieee80211_sub_if_data *sdata = rx->sdata;
2218 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2219 bool check_port_control = false;
2220 struct ethhdr *ehdr;
2221 int ret;
2222
2223 *port_control = false;
2224 if (ieee80211_has_a4(hdr->frame_control) &&
2225 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
2226 return -1;
2227
2228 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2229 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
2230
2231 if (!sdata->u.mgd.use_4addr)
2232 return -1;
2233 else
2234 check_port_control = true;
2235 }
2236
2237 if (is_multicast_ether_addr(hdr->addr1) &&
2238 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
2239 return -1;
2240
2241 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
2242 if (ret < 0)
2243 return ret;
2244
2245 ehdr = (struct ethhdr *) rx->skb->data;
2246 if (ehdr->h_proto == rx->sdata->control_port_protocol)
2247 *port_control = true;
2248 else if (check_port_control)
2249 return -1;
2250
2251 return 0;
2252}
2253
2254/*
2255 * requires that rx->skb is a frame with ethernet header
2256 */
2257static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
2258{
2259 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
2260 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
2261 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2262
2263 /*
2264 * Allow EAPOL frames to us/the PAE group address regardless
2265 * of whether the frame was encrypted or not.
2266 */
2267 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
2268 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
2269 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
2270 return true;
2271
2272 if (ieee80211_802_1x_port_control(rx) ||
2273 ieee80211_drop_unencrypted(rx, fc))
2274 return false;
2275
2276 return true;
2277}
2278
2279/*
2280 * requires that rx->skb is a frame with ethernet header
2281 */
2282static void
2283ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2284{
2285 struct ieee80211_sub_if_data *sdata = rx->sdata;
2286 struct net_device *dev = sdata->dev;
2287 struct sk_buff *skb, *xmit_skb;
2288 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
2289 struct sta_info *dsta;
2290
2291 skb = rx->skb;
2292 xmit_skb = NULL;
2293
2294 ieee80211_rx_stats(dev, skb->len);
2295
2296 if (rx->sta) {
2297 /* The seqno index has the same property as needed
2298 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
2299 * for non-QoS-data frames. Here we know it's a data
2300 * frame, so count MSDUs.
2301 */
2302 u64_stats_update_begin(&rx->sta->rx_stats.syncp);
2303 rx->sta->rx_stats.msdu[rx->seqno_idx]++;
2304 u64_stats_update_end(&rx->sta->rx_stats.syncp);
2305 }
2306
2307 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
2308 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
2309 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
2310 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
2311 if (is_multicast_ether_addr(ehdr->h_dest) &&
2312 ieee80211_vif_get_num_mcast_if(sdata) != 0) {
2313 /*
2314 * send multicast frames both to higher layers in
2315 * local net stack and back to the wireless medium
2316 */
2317 xmit_skb = skb_copy(skb, GFP_ATOMIC);
2318 if (!xmit_skb)
2319 net_info_ratelimited("%s: failed to clone multicast frame\n",
2320 dev->name);
2321 } else if (!is_multicast_ether_addr(ehdr->h_dest)) {
2322 dsta = sta_info_get(sdata, skb->data);
2323 if (dsta) {
2324 /*
2325 * The destination station is associated to
2326 * this AP (in this VLAN), so send the frame
2327 * directly to it and do not pass it to local
2328 * net stack.
2329 */
2330 xmit_skb = skb;
2331 skb = NULL;
2332 }
2333 }
2334 }
2335
2336#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2337 if (skb) {
2338 /* 'align' will only take the values 0 or 2 here since all
2339 * frames are required to be aligned to 2-byte boundaries
2340 * when being passed to mac80211; the code here works just
2341 * as well if that isn't true, but mac80211 assumes it can
2342 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
2343 */
2344 int align;
2345
2346 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
2347 if (align) {
2348 if (WARN_ON(skb_headroom(skb) < 3)) {
2349 dev_kfree_skb(skb);
2350 skb = NULL;
2351 } else {
2352 u8 *data = skb->data;
2353 size_t len = skb_headlen(skb);
2354 skb->data -= align;
2355 memmove(skb->data, data, len);
2356 skb_set_tail_pointer(skb, len);
2357 }
2358 }
2359 }
2360#endif
2361
2362 if (skb) {
2363 /* deliver to local stack */
2364 skb->protocol = eth_type_trans(skb, dev);
2365 memset(skb->cb, 0, sizeof(skb->cb));
2366 if (rx->napi)
2367 napi_gro_receive(rx->napi, skb);
2368 else
2369 netif_receive_skb(skb);
2370 }
2371
2372 if (xmit_skb) {
2373 /*
2374 * Send to wireless media and increase priority by 256 to
2375 * keep the received priority instead of reclassifying
2376 * the frame (see cfg80211_classify8021d).
2377 */
2378 xmit_skb->priority += 256;
2379 xmit_skb->protocol = htons(ETH_P_802_3);
2380 skb_reset_network_header(xmit_skb);
2381 skb_reset_mac_header(xmit_skb);
2382 dev_queue_xmit(xmit_skb);
2383 }
2384}
2385
2386static ieee80211_rx_result debug_noinline
2387ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2388{
2389 struct net_device *dev = rx->sdata->dev;
2390 struct sk_buff *skb = rx->skb;
2391 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2392 __le16 fc = hdr->frame_control;
2393 struct sk_buff_head frame_list;
2394 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2395 struct ethhdr ethhdr;
2396 const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
2397
2398 if (unlikely(!ieee80211_is_data(fc)))
2399 return RX_CONTINUE;
2400
2401 if (unlikely(!ieee80211_is_data_present(fc)))
2402 return RX_DROP_MONITOR;
2403
2404 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
2405 return RX_CONTINUE;
2406
2407 if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
2408 switch (rx->sdata->vif.type) {
2409 case NL80211_IFTYPE_AP_VLAN:
2410 if (!rx->sdata->u.vlan.sta)
2411 return RX_DROP_UNUSABLE;
2412 break;
2413 case NL80211_IFTYPE_STATION:
2414 if (!rx->sdata->u.mgd.use_4addr)
2415 return RX_DROP_UNUSABLE;
2416 break;
2417 default:
2418 return RX_DROP_UNUSABLE;
2419 }
2420 check_da = NULL;
2421 check_sa = NULL;
2422 } else switch (rx->sdata->vif.type) {
2423 case NL80211_IFTYPE_AP:
2424 case NL80211_IFTYPE_AP_VLAN:
2425 check_da = NULL;
2426 break;
2427 case NL80211_IFTYPE_STATION:
2428 if (!rx->sta ||
2429 !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
2430 check_sa = NULL;
2431 break;
2432 case NL80211_IFTYPE_MESH_POINT:
2433 check_sa = NULL;
2434 break;
2435 default:
2436 break;
2437 }
2438
2439 if (is_multicast_ether_addr(hdr->addr1))
2440 return RX_DROP_UNUSABLE;
2441
2442 skb->dev = dev;
2443 __skb_queue_head_init(&frame_list);
2444
2445 if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
2446 rx->sdata->vif.addr,
2447 rx->sdata->vif.type))
2448 return RX_DROP_UNUSABLE;
2449
2450 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2451 rx->sdata->vif.type,
2452 rx->local->hw.extra_tx_headroom,
2453 check_da, check_sa);
2454
2455 while (!skb_queue_empty(&frame_list)) {
2456 rx->skb = __skb_dequeue(&frame_list);
2457
2458 if (!ieee80211_frame_allowed(rx, fc)) {
2459 dev_kfree_skb(rx->skb);
2460 continue;
2461 }
2462
2463 ieee80211_deliver_skb(rx);
2464 }
2465
2466 return RX_QUEUED;
2467}
2468
2469#ifdef CONFIG_MAC80211_MESH
2470static ieee80211_rx_result
2471ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2472{
2473 struct ieee80211_hdr *fwd_hdr, *hdr;
2474 struct ieee80211_tx_info *info;
2475 struct ieee80211s_hdr *mesh_hdr;
2476 struct sk_buff *skb = rx->skb, *fwd_skb;
2477 struct ieee80211_local *local = rx->local;
2478 struct ieee80211_sub_if_data *sdata = rx->sdata;
2479 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2480 u16 ac, q, hdrlen;
2481
2482 hdr = (struct ieee80211_hdr *) skb->data;
2483 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2484
2485 /* make sure fixed part of mesh header is there, also checks skb len */
2486 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2487 return RX_DROP_MONITOR;
2488
2489 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2490
2491 /* make sure full mesh header is there, also checks skb len */
2492 if (!pskb_may_pull(rx->skb,
2493 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2494 return RX_DROP_MONITOR;
2495
2496 /* reload pointers */
2497 hdr = (struct ieee80211_hdr *) skb->data;
2498 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2499
2500 if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
2501 return RX_DROP_MONITOR;
2502
2503 /* frame is in RMC, don't forward */
2504 if (ieee80211_is_data(hdr->frame_control) &&
2505 is_multicast_ether_addr(hdr->addr1) &&
2506 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2507 return RX_DROP_MONITOR;
2508
2509 if (!ieee80211_is_data(hdr->frame_control))
2510 return RX_CONTINUE;
2511
2512 if (!mesh_hdr->ttl)
2513 return RX_DROP_MONITOR;
2514
2515 if (mesh_hdr->flags & MESH_FLAGS_AE) {
2516 struct mesh_path *mppath;
2517 char *proxied_addr;
2518 char *mpp_addr;
2519
2520 if (is_multicast_ether_addr(hdr->addr1)) {
2521 mpp_addr = hdr->addr3;
2522 proxied_addr = mesh_hdr->eaddr1;
2523 } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
2524 MESH_FLAGS_AE_A5_A6) {
2525 /* has_a4 already checked in ieee80211_rx_mesh_check */
2526 mpp_addr = hdr->addr4;
2527 proxied_addr = mesh_hdr->eaddr2;
2528 } else {
2529 return RX_DROP_MONITOR;
2530 }
2531
2532 rcu_read_lock();
2533 mppath = mpp_path_lookup(sdata, proxied_addr);
2534 if (!mppath) {
2535 mpp_path_add(sdata, proxied_addr, mpp_addr);
2536 } else {
2537 spin_lock_bh(&mppath->state_lock);
2538 if (!ether_addr_equal(mppath->mpp, mpp_addr))
2539 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
2540 mppath->exp_time = jiffies;
2541 spin_unlock_bh(&mppath->state_lock);
2542 }
2543 rcu_read_unlock();
2544 }
2545
2546 /* Frame has reached destination. Don't forward */
2547 if (!is_multicast_ether_addr(hdr->addr1) &&
2548 ether_addr_equal(sdata->vif.addr, hdr->addr3))
2549 return RX_CONTINUE;
2550
2551 ac = ieee80211_select_queue_80211(sdata, skb, hdr);
2552 q = sdata->vif.hw_queue[ac];
2553 if (ieee80211_queue_stopped(&local->hw, q)) {
2554 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
2555 return RX_DROP_MONITOR;
2556 }
2557 skb_set_queue_mapping(skb, q);
2558
2559 if (!--mesh_hdr->ttl) {
2560 if (!is_multicast_ether_addr(hdr->addr1))
2561 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
2562 dropped_frames_ttl);
2563 goto out;
2564 }
2565
2566 if (!ifmsh->mshcfg.dot11MeshForwarding)
2567 goto out;
2568
2569 fwd_skb = skb_copy_expand(skb, local->tx_headroom +
2570 sdata->encrypt_headroom, 0, GFP_ATOMIC);
2571 if (!fwd_skb) {
2572 net_info_ratelimited("%s: failed to clone mesh frame\n",
2573 sdata->name);
2574 goto out;
2575 }
2576
2577 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
2578 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
2579 info = IEEE80211_SKB_CB(fwd_skb);
2580 memset(info, 0, sizeof(*info));
2581 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2582 info->control.vif = &rx->sdata->vif;
2583 info->control.jiffies = jiffies;
2584 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2585 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2586 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
2587 /* update power mode indication when forwarding */
2588 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
2589 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
2590 /* mesh power mode flags updated in mesh_nexthop_lookup */
2591 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2592 } else {
2593 /* unable to resolve next hop */
2594 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
2595 fwd_hdr->addr3, 0,
2596 WLAN_REASON_MESH_PATH_NOFORWARD,
2597 fwd_hdr->addr2);
2598 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
2599 kfree_skb(fwd_skb);
2600 return RX_DROP_MONITOR;
2601 }
2602
2603 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2604 ieee80211_add_pending_skb(local, fwd_skb);
2605 out:
2606 if (is_multicast_ether_addr(hdr->addr1))
2607 return RX_CONTINUE;
2608 return RX_DROP_MONITOR;
2609}
2610#endif
2611
2612static ieee80211_rx_result debug_noinline
2613ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2614{
2615 struct ieee80211_sub_if_data *sdata = rx->sdata;
2616 struct ieee80211_local *local = rx->local;
2617 struct net_device *dev = sdata->dev;
2618 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2619 __le16 fc = hdr->frame_control;
2620 bool port_control;
2621 int err;
2622
2623 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
2624 return RX_CONTINUE;
2625
2626 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
2627 return RX_DROP_MONITOR;
2628
2629 /*
2630 * Send unexpected-4addr-frame event to hostapd. For older versions,
2631 * also drop the frame to cooked monitor interfaces.
2632 */
2633 if (ieee80211_has_a4(hdr->frame_control) &&
2634 sdata->vif.type == NL80211_IFTYPE_AP) {
2635 if (rx->sta &&
2636 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2637 cfg80211_rx_unexpected_4addr_frame(
2638 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
2639 return RX_DROP_MONITOR;
2640 }
2641
2642 err = __ieee80211_data_to_8023(rx, &port_control);
2643 if (unlikely(err))
2644 return RX_DROP_UNUSABLE;
2645
2646 if (!ieee80211_frame_allowed(rx, fc))
2647 return RX_DROP_MONITOR;
2648
2649 /* directly handle TDLS channel switch requests/responses */
2650 if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
2651 cpu_to_be16(ETH_P_TDLS))) {
2652 struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
2653
2654 if (pskb_may_pull(rx->skb,
2655 offsetof(struct ieee80211_tdls_data, u)) &&
2656 tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
2657 tf->category == WLAN_CATEGORY_TDLS &&
2658 (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
2659 tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
2660 skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
2661 schedule_work(&local->tdls_chsw_work);
2662 if (rx->sta)
2663 rx->sta->rx_stats.packets++;
2664
2665 return RX_QUEUED;
2666 }
2667 }
2668
2669 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2670 unlikely(port_control) && sdata->bss) {
2671 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2672 u.ap);
2673 dev = sdata->dev;
2674 rx->sdata = sdata;
2675 }
2676
2677 rx->skb->dev = dev;
2678
2679 if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2680 local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2681 !is_multicast_ether_addr(
2682 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2683 (!local->scanning &&
2684 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
2685 mod_timer(&local->dynamic_ps_timer, jiffies +
2686 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2687
2688 ieee80211_deliver_skb(rx);
2689
2690 return RX_QUEUED;
2691}
2692
2693static ieee80211_rx_result debug_noinline
2694ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
2695{
2696 struct sk_buff *skb = rx->skb;
2697 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
2698 struct tid_ampdu_rx *tid_agg_rx;
2699 u16 start_seq_num;
2700 u16 tid;
2701
2702 if (likely(!ieee80211_is_ctl(bar->frame_control)))
2703 return RX_CONTINUE;
2704
2705 if (ieee80211_is_back_req(bar->frame_control)) {
2706 struct {
2707 __le16 control, start_seq_num;
2708 } __packed bar_data;
2709 struct ieee80211_event event = {
2710 .type = BAR_RX_EVENT,
2711 };
2712
2713 if (!rx->sta)
2714 return RX_DROP_MONITOR;
2715
2716 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2717 &bar_data, sizeof(bar_data)))
2718 return RX_DROP_MONITOR;
2719
2720 tid = le16_to_cpu(bar_data.control) >> 12;
2721
2722 if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
2723 !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
2724 ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
2725 WLAN_BACK_RECIPIENT,
2726 WLAN_REASON_QSTA_REQUIRE_SETUP);
2727
2728 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2729 if (!tid_agg_rx)
2730 return RX_DROP_MONITOR;
2731
2732 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
2733 event.u.ba.tid = tid;
2734 event.u.ba.ssn = start_seq_num;
2735 event.u.ba.sta = &rx->sta->sta;
2736
2737 /* reset session timer */
2738 if (tid_agg_rx->timeout)
2739 mod_timer(&tid_agg_rx->session_timer,
2740 TU_TO_EXP_TIME(tid_agg_rx->timeout));
2741
2742 spin_lock(&tid_agg_rx->reorder_lock);
2743 /* release stored frames up to start of BAR */
2744 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
2745 start_seq_num, frames);
2746 spin_unlock(&tid_agg_rx->reorder_lock);
2747
2748 drv_event_callback(rx->local, rx->sdata, &event);
2749
2750 kfree_skb(skb);
2751 return RX_QUEUED;
2752 }
2753
2754 /*
2755 * After this point, we only want management frames,
2756 * so we can drop all remaining control frames to
2757 * cooked monitor interfaces.
2758 */
2759 return RX_DROP_MONITOR;
2760}
2761
2762static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2763 struct ieee80211_mgmt *mgmt,
2764 size_t len)
2765{
2766 struct ieee80211_local *local = sdata->local;
2767 struct sk_buff *skb;
2768 struct ieee80211_mgmt *resp;
2769
2770 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
2771 /* Not to own unicast address */
2772 return;
2773 }
2774
2775 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2776 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
2777 /* Not from the current AP or not associated yet. */
2778 return;
2779 }
2780
2781 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2782 /* Too short SA Query request frame */
2783 return;
2784 }
2785
2786 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2787 if (skb == NULL)
2788 return;
2789
2790 skb_reserve(skb, local->hw.extra_tx_headroom);
2791 resp = skb_put_zero(skb, 24);
2792 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2793 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
2794 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
2795 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2796 IEEE80211_STYPE_ACTION);
2797 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2798 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2799 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2800 memcpy(resp->u.action.u.sa_query.trans_id,
2801 mgmt->u.action.u.sa_query.trans_id,
2802 WLAN_SA_QUERY_TR_ID_LEN);
2803
2804 ieee80211_tx_skb(sdata, skb);
2805}
2806
2807static ieee80211_rx_result debug_noinline
2808ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2809{
2810 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2811 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2812
2813 /*
2814 * From here on, look only at management frames.
2815 * Data and control frames are already handled,
2816 * and unknown (reserved) frames are useless.
2817 */
2818 if (rx->skb->len < 24)
2819 return RX_DROP_MONITOR;
2820
2821 if (!ieee80211_is_mgmt(mgmt->frame_control))
2822 return RX_DROP_MONITOR;
2823
2824 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2825 ieee80211_is_beacon(mgmt->frame_control) &&
2826 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
2827 int sig = 0;
2828
2829 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
2830 sig = status->signal;
2831
2832 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2833 rx->skb->data, rx->skb->len,
2834 status->freq, sig);
2835 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2836 }
2837
2838 if (ieee80211_drop_unencrypted_mgmt(rx))
2839 return RX_DROP_UNUSABLE;
2840
2841 return RX_CONTINUE;
2842}
2843
2844static ieee80211_rx_result debug_noinline
2845ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2846{
2847 struct ieee80211_local *local = rx->local;
2848 struct ieee80211_sub_if_data *sdata = rx->sdata;
2849 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2850 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2851 int len = rx->skb->len;
2852
2853 if (!ieee80211_is_action(mgmt->frame_control))
2854 return RX_CONTINUE;
2855
2856 /* drop too small frames */
2857 if (len < IEEE80211_MIN_ACTION_SIZE)
2858 return RX_DROP_UNUSABLE;
2859
2860 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
2861 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2862 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
2863 return RX_DROP_UNUSABLE;
2864
2865 switch (mgmt->u.action.category) {
2866 case WLAN_CATEGORY_HT:
2867 /* reject HT action frames from stations not supporting HT */
2868 if (!rx->sta->sta.ht_cap.ht_supported)
2869 goto invalid;
2870
2871 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2872 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2873 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2874 sdata->vif.type != NL80211_IFTYPE_AP &&
2875 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2876 break;
2877
2878 /* verify action & smps_control/chanwidth are present */
2879 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2880 goto invalid;
2881
2882 switch (mgmt->u.action.u.ht_smps.action) {
2883 case WLAN_HT_ACTION_SMPS: {
2884 struct ieee80211_supported_band *sband;
2885 enum ieee80211_smps_mode smps_mode;
2886
2887 /* convert to HT capability */
2888 switch (mgmt->u.action.u.ht_smps.smps_control) {
2889 case WLAN_HT_SMPS_CONTROL_DISABLED:
2890 smps_mode = IEEE80211_SMPS_OFF;
2891 break;
2892 case WLAN_HT_SMPS_CONTROL_STATIC:
2893 smps_mode = IEEE80211_SMPS_STATIC;
2894 break;
2895 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
2896 smps_mode = IEEE80211_SMPS_DYNAMIC;
2897 break;
2898 default:
2899 goto invalid;
2900 }
2901
2902 /* if no change do nothing */
2903 if (rx->sta->sta.smps_mode == smps_mode)
2904 goto handled;
2905 rx->sta->sta.smps_mode = smps_mode;
2906
2907 sband = rx->local->hw.wiphy->bands[status->band];
2908
2909 rate_control_rate_update(local, sband, rx->sta,
2910 IEEE80211_RC_SMPS_CHANGED);
2911 goto handled;
2912 }
2913 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2914 struct ieee80211_supported_band *sband;
2915 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
2916 enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
2917
2918 /* If it doesn't support 40 MHz it can't change ... */
2919 if (!(rx->sta->sta.ht_cap.cap &
2920 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
2921 goto handled;
2922
2923 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
2924 max_bw = IEEE80211_STA_RX_BW_20;
2925 else
2926 max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
2927
2928 /* set cur_max_bandwidth and recalc sta bw */
2929 rx->sta->cur_max_bandwidth = max_bw;
2930 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
2931
2932 if (rx->sta->sta.bandwidth == new_bw)
2933 goto handled;
2934
2935 rx->sta->sta.bandwidth = new_bw;
2936 sband = rx->local->hw.wiphy->bands[status->band];
2937
2938 rate_control_rate_update(local, sband, rx->sta,
2939 IEEE80211_RC_BW_CHANGED);
2940 goto handled;
2941 }
2942 default:
2943 goto invalid;
2944 }
2945
2946 break;
2947 case WLAN_CATEGORY_PUBLIC:
2948 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2949 goto invalid;
2950 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2951 break;
2952 if (!rx->sta)
2953 break;
2954 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2955 break;
2956 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2957 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2958 break;
2959 if (len < offsetof(struct ieee80211_mgmt,
2960 u.action.u.ext_chan_switch.variable))
2961 goto invalid;
2962 goto queue;
2963 case WLAN_CATEGORY_VHT:
2964 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2965 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2966 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2967 sdata->vif.type != NL80211_IFTYPE_AP &&
2968 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2969 break;
2970
2971 /* verify action code is present */
2972 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2973 goto invalid;
2974
2975 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2976 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
2977 /* verify opmode is present */
2978 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2979 goto invalid;
2980 goto queue;
2981 }
2982 case WLAN_VHT_ACTION_GROUPID_MGMT: {
2983 if (len < IEEE80211_MIN_ACTION_SIZE + 25)
2984 goto invalid;
2985 goto queue;
2986 }
2987 default:
2988 break;
2989 }
2990 break;
2991 case WLAN_CATEGORY_BACK:
2992 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2993 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2994 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2995 sdata->vif.type != NL80211_IFTYPE_AP &&
2996 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2997 break;
2998
2999 /* verify action_code is present */
3000 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3001 break;
3002
3003 switch (mgmt->u.action.u.addba_req.action_code) {
3004 case WLAN_ACTION_ADDBA_REQ:
3005 if (len < (IEEE80211_MIN_ACTION_SIZE +
3006 sizeof(mgmt->u.action.u.addba_req)))
3007 goto invalid;
3008 break;
3009 case WLAN_ACTION_ADDBA_RESP:
3010 if (len < (IEEE80211_MIN_ACTION_SIZE +
3011 sizeof(mgmt->u.action.u.addba_resp)))
3012 goto invalid;
3013 break;
3014 case WLAN_ACTION_DELBA:
3015 if (len < (IEEE80211_MIN_ACTION_SIZE +
3016 sizeof(mgmt->u.action.u.delba)))
3017 goto invalid;
3018 break;
3019 default:
3020 goto invalid;
3021 }
3022
3023 goto queue;
3024 case WLAN_CATEGORY_SPECTRUM_MGMT:
3025 /* verify action_code is present */
3026 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3027 break;
3028
3029 switch (mgmt->u.action.u.measurement.action_code) {
3030 case WLAN_ACTION_SPCT_MSR_REQ:
3031 if (status->band != NL80211_BAND_5GHZ)
3032 break;
3033
3034 if (len < (IEEE80211_MIN_ACTION_SIZE +
3035 sizeof(mgmt->u.action.u.measurement)))
3036 break;
3037
3038 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3039 break;
3040
3041 ieee80211_process_measurement_req(sdata, mgmt, len);
3042 goto handled;
3043 case WLAN_ACTION_SPCT_CHL_SWITCH: {
3044 u8 *bssid;
3045 if (len < (IEEE80211_MIN_ACTION_SIZE +
3046 sizeof(mgmt->u.action.u.chan_switch)))
3047 break;
3048
3049 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3050 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3051 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3052 break;
3053
3054 if (sdata->vif.type == NL80211_IFTYPE_STATION)
3055 bssid = sdata->u.mgd.bssid;
3056 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3057 bssid = sdata->u.ibss.bssid;
3058 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
3059 bssid = mgmt->sa;
3060 else
3061 break;
3062
3063 if (!ether_addr_equal(mgmt->bssid, bssid))
3064 break;
3065
3066 goto queue;
3067 }
3068 }
3069 break;
3070 case WLAN_CATEGORY_SA_QUERY:
3071 if (len < (IEEE80211_MIN_ACTION_SIZE +
3072 sizeof(mgmt->u.action.u.sa_query)))
3073 break;
3074
3075 switch (mgmt->u.action.u.sa_query.action) {
3076 case WLAN_ACTION_SA_QUERY_REQUEST:
3077 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3078 break;
3079 ieee80211_process_sa_query_req(sdata, mgmt, len);
3080 goto handled;
3081 }
3082 break;
3083 case WLAN_CATEGORY_SELF_PROTECTED:
3084 if (len < (IEEE80211_MIN_ACTION_SIZE +
3085 sizeof(mgmt->u.action.u.self_prot.action_code)))
3086 break;
3087
3088 switch (mgmt->u.action.u.self_prot.action_code) {
3089 case WLAN_SP_MESH_PEERING_OPEN:
3090 case WLAN_SP_MESH_PEERING_CLOSE:
3091 case WLAN_SP_MESH_PEERING_CONFIRM:
3092 if (!ieee80211_vif_is_mesh(&sdata->vif))
3093 goto invalid;
3094 if (sdata->u.mesh.user_mpm)
3095 /* userspace handles this frame */
3096 break;
3097 goto queue;
3098 case WLAN_SP_MGK_INFORM:
3099 case WLAN_SP_MGK_ACK:
3100 if (!ieee80211_vif_is_mesh(&sdata->vif))
3101 goto invalid;
3102 break;
3103 }
3104 break;
3105 case WLAN_CATEGORY_MESH_ACTION:
3106 if (len < (IEEE80211_MIN_ACTION_SIZE +
3107 sizeof(mgmt->u.action.u.mesh_action.action_code)))
3108 break;
3109
3110 if (!ieee80211_vif_is_mesh(&sdata->vif))
3111 break;
3112 if (mesh_action_is_path_sel(mgmt) &&
3113 !mesh_path_sel_is_hwmp(sdata))
3114 break;
3115 goto queue;
3116 }
3117
3118 return RX_CONTINUE;
3119
3120 invalid:
3121 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
3122 /* will return in the next handlers */
3123 return RX_CONTINUE;
3124
3125 handled:
3126 if (rx->sta)
3127 rx->sta->rx_stats.packets++;
3128 dev_kfree_skb(rx->skb);
3129 return RX_QUEUED;
3130
3131 queue:
3132 skb_queue_tail(&sdata->skb_queue, rx->skb);
3133 ieee80211_queue_work(&local->hw, &sdata->work);
3134 if (rx->sta)
3135 rx->sta->rx_stats.packets++;
3136 return RX_QUEUED;
3137}
3138
3139static ieee80211_rx_result debug_noinline
3140ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
3141{
3142 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3143 int sig = 0;
3144
3145 /* skip known-bad action frames and return them in the next handler */
3146 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
3147 return RX_CONTINUE;
3148
3149 /*
3150 * Getting here means the kernel doesn't know how to handle
3151 * it, but maybe userspace does ... include returned frames
3152 * so userspace can register for those to know whether ones
3153 * it transmitted were processed or returned.
3154 */
3155
3156 if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
3157 sig = status->signal;
3158
3159 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
3160 rx->skb->data, rx->skb->len, 0)) {
3161 if (rx->sta)
3162 rx->sta->rx_stats.packets++;
3163 dev_kfree_skb(rx->skb);
3164 return RX_QUEUED;
3165 }
3166
3167 return RX_CONTINUE;
3168}
3169
3170static ieee80211_rx_result debug_noinline
3171ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
3172{
3173 struct ieee80211_local *local = rx->local;
3174 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
3175 struct sk_buff *nskb;
3176 struct ieee80211_sub_if_data *sdata = rx->sdata;
3177 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
3178
3179 if (!ieee80211_is_action(mgmt->frame_control))
3180 return RX_CONTINUE;
3181
3182 /*
3183 * For AP mode, hostapd is responsible for handling any action
3184 * frames that we didn't handle, including returning unknown
3185 * ones. For all other modes we will return them to the sender,
3186 * setting the 0x80 bit in the action category, as required by
3187 * 802.11-2012 9.24.4.
3188 * Newer versions of hostapd shall also use the management frame
3189 * registration mechanisms, but older ones still use cooked
3190 * monitor interfaces so push all frames there.
3191 */
3192 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
3193 (sdata->vif.type == NL80211_IFTYPE_AP ||
3194 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
3195 return RX_DROP_MONITOR;
3196
3197 if (is_multicast_ether_addr(mgmt->da))
3198 return RX_DROP_MONITOR;
3199
3200 /* do not return rejected action frames */
3201 if (mgmt->u.action.category & 0x80)
3202 return RX_DROP_UNUSABLE;
3203
3204 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
3205 GFP_ATOMIC);
3206 if (nskb) {
3207 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
3208
3209 nmgmt->u.action.category |= 0x80;
3210 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
3211 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
3212
3213 memset(nskb->cb, 0, sizeof(nskb->cb));
3214
3215 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
3216 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
3217
3218 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
3219 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
3220 IEEE80211_TX_CTL_NO_CCK_RATE;
3221 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3222 info->hw_queue =
3223 local->hw.offchannel_tx_hw_queue;
3224 }
3225
3226 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
3227 status->band);
3228 }
3229 dev_kfree_skb(rx->skb);
3230 return RX_QUEUED;
3231}
3232
3233static ieee80211_rx_result debug_noinline
3234ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
3235{
3236 struct ieee80211_sub_if_data *sdata = rx->sdata;
3237 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
3238 __le16 stype;
3239
3240 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
3241
3242 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3243 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3244 sdata->vif.type != NL80211_IFTYPE_OCB &&
3245 sdata->vif.type != NL80211_IFTYPE_STATION)
3246 return RX_DROP_MONITOR;
3247
3248 switch (stype) {
3249 case cpu_to_le16(IEEE80211_STYPE_AUTH):
3250 case cpu_to_le16(IEEE80211_STYPE_BEACON):
3251 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3252 /* process for all: mesh, mlme, ibss */
3253 break;
3254 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
3255 if (is_multicast_ether_addr(mgmt->da) &&
3256 !is_broadcast_ether_addr(mgmt->da))
3257 return RX_DROP_MONITOR;
3258
3259 /* process only for station/IBSS */
3260 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3261 sdata->vif.type != NL80211_IFTYPE_ADHOC)
3262 return RX_DROP_MONITOR;
3263 break;
3264 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3265 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3266 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
3267 if (is_multicast_ether_addr(mgmt->da) &&
3268 !is_broadcast_ether_addr(mgmt->da))
3269 return RX_DROP_MONITOR;
3270
3271 /* process only for station */
3272 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3273 return RX_DROP_MONITOR;
3274 break;
3275 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
3276 /* process only for ibss and mesh */
3277 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3278 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3279 return RX_DROP_MONITOR;
3280 break;
3281 default:
3282 return RX_DROP_MONITOR;
3283 }
3284
3285 /* queue up frame and kick off work to process it */
3286 skb_queue_tail(&sdata->skb_queue, rx->skb);
3287 ieee80211_queue_work(&rx->local->hw, &sdata->work);
3288 if (rx->sta)
3289 rx->sta->rx_stats.packets++;
3290
3291 return RX_QUEUED;
3292}
3293
3294static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
3295 struct ieee80211_rate *rate)
3296{
3297 struct ieee80211_sub_if_data *sdata;
3298 struct ieee80211_local *local = rx->local;
3299 struct sk_buff *skb = rx->skb, *skb2;
3300 struct net_device *prev_dev = NULL;
3301 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3302 int needed_headroom;
3303
3304 /*
3305 * If cooked monitor has been processed already, then
3306 * don't do it again. If not, set the flag.
3307 */
3308 if (rx->flags & IEEE80211_RX_CMNTR)
3309 goto out_free_skb;
3310 rx->flags |= IEEE80211_RX_CMNTR;
3311
3312 /* If there are no cooked monitor interfaces, just free the SKB */
3313 if (!local->cooked_mntrs)
3314 goto out_free_skb;
3315
3316 /* vendor data is long removed here */
3317 status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
3318 /* room for the radiotap header based on driver features */
3319 needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
3320
3321 if (skb_headroom(skb) < needed_headroom &&
3322 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
3323 goto out_free_skb;
3324
3325 /* prepend radiotap information */
3326 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
3327 false);
3328
3329 skb_reset_mac_header(skb);
3330 skb->ip_summed = CHECKSUM_UNNECESSARY;
3331 skb->pkt_type = PACKET_OTHERHOST;
3332 skb->protocol = htons(ETH_P_802_2);
3333
3334 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3335 if (!ieee80211_sdata_running(sdata))
3336 continue;
3337
3338 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3339 !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
3340 continue;
3341
3342 if (prev_dev) {
3343 skb2 = skb_clone(skb, GFP_ATOMIC);
3344 if (skb2) {
3345 skb2->dev = prev_dev;
3346 netif_receive_skb(skb2);
3347 }
3348 }
3349
3350 prev_dev = sdata->dev;
3351 ieee80211_rx_stats(sdata->dev, skb->len);
3352 }
3353
3354 if (prev_dev) {
3355 skb->dev = prev_dev;
3356 netif_receive_skb(skb);
3357 return;
3358 }
3359
3360 out_free_skb:
3361 dev_kfree_skb(skb);
3362}
3363
3364static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
3365 ieee80211_rx_result res)
3366{
3367 switch (res) {
3368 case RX_DROP_MONITOR:
3369 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3370 if (rx->sta)
3371 rx->sta->rx_stats.dropped++;
3372 /* fall through */
3373 case RX_CONTINUE: {
3374 struct ieee80211_rate *rate = NULL;
3375 struct ieee80211_supported_band *sband;
3376 struct ieee80211_rx_status *status;
3377
3378 status = IEEE80211_SKB_RXCB((rx->skb));
3379
3380 sband = rx->local->hw.wiphy->bands[status->band];
3381 if (!(status->encoding == RX_ENC_HT) &&
3382 !(status->encoding == RX_ENC_VHT))
3383 rate = &sband->bitrates[status->rate_idx];
3384
3385 ieee80211_rx_cooked_monitor(rx, rate);
3386 break;
3387 }
3388 case RX_DROP_UNUSABLE:
3389 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
3390 if (rx->sta)
3391 rx->sta->rx_stats.dropped++;
3392 dev_kfree_skb(rx->skb);
3393 break;
3394 case RX_QUEUED:
3395 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
3396 break;
3397 }
3398}
3399
3400static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3401 struct sk_buff_head *frames)
3402{
3403 ieee80211_rx_result res = RX_DROP_MONITOR;
3404 struct sk_buff *skb;
3405
3406#define CALL_RXH(rxh) \
3407 do { \
3408 res = rxh(rx); \
3409 if (res != RX_CONTINUE) \
3410 goto rxh_next; \
3411 } while (0)
3412
3413 /* Lock here to avoid hitting all of the data used in the RX
3414 * path (e.g. key data, station data, ...) concurrently when
3415 * a frame is released from the reorder buffer due to timeout
3416 * from the timer, potentially concurrently with RX from the
3417 * driver.
3418 */
3419 spin_lock_bh(&rx->local->rx_path_lock);
3420
3421 while ((skb = __skb_dequeue(frames))) {
3422 /*
3423 * all the other fields are valid across frames
3424 * that belong to an aMPDU since they are on the
3425 * same TID from the same station
3426 */
3427 rx->skb = skb;
3428
3429 CALL_RXH(ieee80211_rx_h_check_more_data);
3430 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
3431 CALL_RXH(ieee80211_rx_h_sta_process);
3432 CALL_RXH(ieee80211_rx_h_decrypt);
3433 CALL_RXH(ieee80211_rx_h_defragment);
3434 CALL_RXH(ieee80211_rx_h_michael_mic_verify);
3435 /* must be after MMIC verify so header is counted in MPDU mic */
3436#ifdef CONFIG_MAC80211_MESH
3437 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
3438 CALL_RXH(ieee80211_rx_h_mesh_fwding);
3439#endif
3440 CALL_RXH(ieee80211_rx_h_amsdu);
3441 CALL_RXH(ieee80211_rx_h_data);
3442
3443 /* special treatment -- needs the queue */
3444 res = ieee80211_rx_h_ctrl(rx, frames);
3445 if (res != RX_CONTINUE)
3446 goto rxh_next;
3447
3448 CALL_RXH(ieee80211_rx_h_mgmt_check);
3449 CALL_RXH(ieee80211_rx_h_action);
3450 CALL_RXH(ieee80211_rx_h_userspace_mgmt);
3451 CALL_RXH(ieee80211_rx_h_action_return);
3452 CALL_RXH(ieee80211_rx_h_mgmt);
3453
3454 rxh_next:
3455 ieee80211_rx_handlers_result(rx, res);
3456
3457#undef CALL_RXH
3458 }
3459
3460 spin_unlock_bh(&rx->local->rx_path_lock);
3461}
3462
3463static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
3464{
3465 struct sk_buff_head reorder_release;
3466 ieee80211_rx_result res = RX_DROP_MONITOR;
3467
3468 __skb_queue_head_init(&reorder_release);
3469
3470#define CALL_RXH(rxh) \
3471 do { \
3472 res = rxh(rx); \
3473 if (res != RX_CONTINUE) \
3474 goto rxh_next; \
3475 } while (0)
3476
3477 CALL_RXH(ieee80211_rx_h_check_dup);
3478 CALL_RXH(ieee80211_rx_h_check);
3479
3480 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
3481
3482 ieee80211_rx_handlers(rx, &reorder_release);
3483 return;
3484
3485 rxh_next:
3486 ieee80211_rx_handlers_result(rx, res);
3487
3488#undef CALL_RXH
3489}
3490
3491/*
3492 * This function makes calls into the RX path, therefore
3493 * it has to be invoked under RCU read lock.
3494 */
3495void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3496{
3497 struct sk_buff_head frames;
3498 struct ieee80211_rx_data rx = {
3499 .sta = sta,
3500 .sdata = sta->sdata,
3501 .local = sta->local,
3502 /* This is OK -- must be QoS data frame */
3503 .security_idx = tid,
3504 .seqno_idx = tid,
3505 .napi = NULL, /* must be NULL to not have races */
3506 };
3507 struct tid_ampdu_rx *tid_agg_rx;
3508
3509 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3510 if (!tid_agg_rx)
3511 return;
3512
3513 __skb_queue_head_init(&frames);
3514
3515 spin_lock(&tid_agg_rx->reorder_lock);
3516 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3517 spin_unlock(&tid_agg_rx->reorder_lock);
3518
3519 if (!skb_queue_empty(&frames)) {
3520 struct ieee80211_event event = {
3521 .type = BA_FRAME_TIMEOUT,
3522 .u.ba.tid = tid,
3523 .u.ba.sta = &sta->sta,
3524 };
3525 drv_event_callback(rx.local, rx.sdata, &event);
3526 }
3527
3528 ieee80211_rx_handlers(&rx, &frames);
3529}
3530
3531void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
3532 u16 ssn, u64 filtered,
3533 u16 received_mpdus)
3534{
3535 struct sta_info *sta;
3536 struct tid_ampdu_rx *tid_agg_rx;
3537 struct sk_buff_head frames;
3538 struct ieee80211_rx_data rx = {
3539 /* This is OK -- must be QoS data frame */
3540 .security_idx = tid,
3541 .seqno_idx = tid,
3542 };
3543 int i, diff;
3544
3545 if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
3546 return;
3547
3548 __skb_queue_head_init(&frames);
3549
3550 sta = container_of(pubsta, struct sta_info, sta);
3551
3552 rx.sta = sta;
3553 rx.sdata = sta->sdata;
3554 rx.local = sta->local;
3555
3556 rcu_read_lock();
3557 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3558 if (!tid_agg_rx)
3559 goto out;
3560
3561 spin_lock_bh(&tid_agg_rx->reorder_lock);
3562
3563 if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
3564 int release;
3565
3566 /* release all frames in the reorder buffer */
3567 release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
3568 IEEE80211_SN_MODULO;
3569 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
3570 release, &frames);
3571 /* update ssn to match received ssn */
3572 tid_agg_rx->head_seq_num = ssn;
3573 } else {
3574 ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
3575 &frames);
3576 }
3577
3578 /* handle the case that received ssn is behind the mac ssn.
3579 * it can be tid_agg_rx->buf_size behind and still be valid */
3580 diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
3581 if (diff >= tid_agg_rx->buf_size) {
3582 tid_agg_rx->reorder_buf_filtered = 0;
3583 goto release;
3584 }
3585 filtered = filtered >> diff;
3586 ssn += diff;
3587
3588 /* update bitmap */
3589 for (i = 0; i < tid_agg_rx->buf_size; i++) {
3590 int index = (ssn + i) % tid_agg_rx->buf_size;
3591
3592 tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
3593 if (filtered & BIT_ULL(i))
3594 tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
3595 }
3596
3597 /* now process also frames that the filter marking released */
3598 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
3599
3600release:
3601 spin_unlock_bh(&tid_agg_rx->reorder_lock);
3602
3603 ieee80211_rx_handlers(&rx, &frames);
3604
3605 out:
3606 rcu_read_unlock();
3607}
3608EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
3609
3610/* main receive path */
3611
3612static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
3613{
3614 struct ieee80211_sub_if_data *sdata = rx->sdata;
3615 struct sk_buff *skb = rx->skb;
3616 struct ieee80211_hdr *hdr = (void *)skb->data;
3617 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3618 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
3619 bool multicast = is_multicast_ether_addr(hdr->addr1);
3620
3621 switch (sdata->vif.type) {
3622 case NL80211_IFTYPE_STATION:
3623 if (!bssid && !sdata->u.mgd.use_4addr)
3624 return false;
3625 if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
3626 return false;
3627 if (multicast)
3628 return true;
3629 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3630 case NL80211_IFTYPE_ADHOC:
3631 if (!bssid)
3632 return false;
3633 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3634 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3635 return false;
3636 if (ieee80211_is_beacon(hdr->frame_control))
3637 return true;
3638 if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
3639 return false;
3640 if (!multicast &&
3641 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3642 return false;
3643 if (!rx->sta) {
3644 int rate_idx;
3645 if (status->encoding != RX_ENC_LEGACY)
3646 rate_idx = 0; /* TODO: HT/VHT rates */
3647 else
3648 rate_idx = status->rate_idx;
3649 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3650 BIT(rate_idx));
3651 }
3652 return true;
3653 case NL80211_IFTYPE_OCB:
3654 if (!bssid)
3655 return false;
3656 if (!ieee80211_is_data_present(hdr->frame_control))
3657 return false;
3658 if (!is_broadcast_ether_addr(bssid))
3659 return false;
3660 if (!multicast &&
3661 !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
3662 return false;
3663 if (!rx->sta) {
3664 int rate_idx;
3665 if (status->encoding != RX_ENC_LEGACY)
3666 rate_idx = 0; /* TODO: HT rates */
3667 else
3668 rate_idx = status->rate_idx;
3669 ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
3670 BIT(rate_idx));
3671 }
3672 return true;
3673 case NL80211_IFTYPE_MESH_POINT:
3674 if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
3675 return false;
3676 if (multicast)
3677 return true;
3678 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3679 case NL80211_IFTYPE_AP_VLAN:
3680 case NL80211_IFTYPE_AP:
3681 if (!bssid)
3682 return ether_addr_equal(sdata->vif.addr, hdr->addr1);
3683
3684 if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3685 /*
3686 * Accept public action frames even when the
3687 * BSSID doesn't match, this is used for P2P
3688 * and location updates. Note that mac80211
3689 * itself never looks at these frames.
3690 */
3691 if (!multicast &&
3692 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3693 return false;
3694 if (ieee80211_is_public_action(hdr, skb->len))
3695 return true;
3696 return ieee80211_is_beacon(hdr->frame_control);
3697 }
3698
3699 if (!ieee80211_has_tods(hdr->frame_control)) {
3700 /* ignore data frames to TDLS-peers */
3701 if (ieee80211_is_data(hdr->frame_control))
3702 return false;
3703 /* ignore action frames to TDLS-peers */
3704 if (ieee80211_is_action(hdr->frame_control) &&
3705 !is_broadcast_ether_addr(bssid) &&
3706 !ether_addr_equal(bssid, hdr->addr1))
3707 return false;
3708 }
3709
3710 /*
3711 * 802.11-2016 Table 9-26 says that for data frames, A1 must be
3712 * the BSSID - we've checked that already but may have accepted
3713 * the wildcard (ff:ff:ff:ff:ff:ff).
3714 *
3715 * It also says:
3716 * The BSSID of the Data frame is determined as follows:
3717 * a) If the STA is contained within an AP or is associated
3718 * with an AP, the BSSID is the address currently in use
3719 * by the STA contained in the AP.
3720 *
3721 * So we should not accept data frames with an address that's
3722 * multicast.
3723 *
3724 * Accepting it also opens a security problem because stations
3725 * could encrypt it with the GTK and inject traffic that way.
3726 */
3727 if (ieee80211_is_data(hdr->frame_control) && multicast)
3728 return false;
3729
3730 return true;
3731 case NL80211_IFTYPE_WDS:
3732 if (bssid || !ieee80211_is_data(hdr->frame_control))
3733 return false;
3734 return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
3735 case NL80211_IFTYPE_P2P_DEVICE:
3736 return ieee80211_is_public_action(hdr, skb->len) ||
3737 ieee80211_is_probe_req(hdr->frame_control) ||
3738 ieee80211_is_probe_resp(hdr->frame_control) ||
3739 ieee80211_is_beacon(hdr->frame_control);
3740 case NL80211_IFTYPE_NAN:
3741 /* Currently no frames on NAN interface are allowed */
3742 return false;
3743 default:
3744 break;
3745 }
3746
3747 WARN_ON_ONCE(1);
3748 return false;
3749}
3750
3751void ieee80211_check_fast_rx(struct sta_info *sta)
3752{
3753 struct ieee80211_sub_if_data *sdata = sta->sdata;
3754 struct ieee80211_local *local = sdata->local;
3755 struct ieee80211_key *key;
3756 struct ieee80211_fast_rx fastrx = {
3757 .dev = sdata->dev,
3758 .vif_type = sdata->vif.type,
3759 .control_port_protocol = sdata->control_port_protocol,
3760 }, *old, *new = NULL;
3761 bool assign = false;
3762
3763 /* use sparse to check that we don't return without updating */
3764 __acquire(check_fast_rx);
3765
3766 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
3767 BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
3768 ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
3769 ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
3770
3771 fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
3772
3773 /* fast-rx doesn't do reordering */
3774 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
3775 !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
3776 goto clear;
3777
3778 switch (sdata->vif.type) {
3779 case NL80211_IFTYPE_STATION:
3780 /* 4-addr is harder to deal with, later maybe */
3781 if (sdata->u.mgd.use_4addr)
3782 goto clear;
3783 /* software powersave is a huge mess, avoid all of it */
3784 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
3785 goto clear;
3786 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3787 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
3788 goto clear;
3789 if (sta->sta.tdls) {
3790 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3791 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3792 fastrx.expected_ds_bits = 0;
3793 } else {
3794 fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
3795 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
3796 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3797 fastrx.expected_ds_bits =
3798 cpu_to_le16(IEEE80211_FCTL_FROMDS);
3799 }
3800 break;
3801 case NL80211_IFTYPE_AP_VLAN:
3802 case NL80211_IFTYPE_AP:
3803 /* parallel-rx requires this, at least with calls to
3804 * ieee80211_sta_ps_transition()
3805 */
3806 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
3807 goto clear;
3808 fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
3809 fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3810 fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
3811
3812 fastrx.internal_forward =
3813 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
3814 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
3815 !sdata->u.vlan.sta);
3816 break;
3817 default:
3818 goto clear;
3819 }
3820
3821 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3822 goto clear;
3823
3824 rcu_read_lock();
3825 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
3826 if (key) {
3827 switch (key->conf.cipher) {
3828 case WLAN_CIPHER_SUITE_TKIP:
3829 /* we don't want to deal with MMIC in fast-rx */
3830 goto clear_rcu;
3831 case WLAN_CIPHER_SUITE_CCMP:
3832 case WLAN_CIPHER_SUITE_CCMP_256:
3833 case WLAN_CIPHER_SUITE_GCMP:
3834 case WLAN_CIPHER_SUITE_GCMP_256:
3835 break;
3836 default:
3837 /* we also don't want to deal with WEP or cipher scheme
3838 * since those require looking up the key idx in the
3839 * frame, rather than assuming the PTK is used
3840 * (we need to revisit this once we implement the real
3841 * PTK index, which is now valid in the spec, but we
3842 * haven't implemented that part yet)
3843 */
3844 goto clear_rcu;
3845 }
3846
3847 fastrx.key = true;
3848 fastrx.icv_len = key->conf.icv_len;
3849 }
3850
3851 assign = true;
3852 clear_rcu:
3853 rcu_read_unlock();
3854 clear:
3855 __release(check_fast_rx);
3856
3857 if (assign)
3858 new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
3859
3860 spin_lock_bh(&sta->lock);
3861 old = rcu_dereference_protected(sta->fast_rx, true);
3862 rcu_assign_pointer(sta->fast_rx, new);
3863 spin_unlock_bh(&sta->lock);
3864
3865 if (old)
3866 kfree_rcu(old, rcu_head);
3867}
3868
3869void ieee80211_clear_fast_rx(struct sta_info *sta)
3870{
3871 struct ieee80211_fast_rx *old;
3872
3873 spin_lock_bh(&sta->lock);
3874 old = rcu_dereference_protected(sta->fast_rx, true);
3875 RCU_INIT_POINTER(sta->fast_rx, NULL);
3876 spin_unlock_bh(&sta->lock);
3877
3878 if (old)
3879 kfree_rcu(old, rcu_head);
3880}
3881
3882void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3883{
3884 struct ieee80211_local *local = sdata->local;
3885 struct sta_info *sta;
3886
3887 lockdep_assert_held(&local->sta_mtx);
3888
3889 list_for_each_entry(sta, &local->sta_list, list) {
3890 if (sdata != sta->sdata &&
3891 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3892 continue;
3893 ieee80211_check_fast_rx(sta);
3894 }
3895}
3896
3897void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
3898{
3899 struct ieee80211_local *local = sdata->local;
3900
3901 mutex_lock(&local->sta_mtx);
3902 __ieee80211_check_fast_rx_iface(sdata);
3903 mutex_unlock(&local->sta_mtx);
3904}
3905
3906static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
3907 struct ieee80211_fast_rx *fast_rx)
3908{
3909 struct sk_buff *skb = rx->skb;
3910 struct ieee80211_hdr *hdr = (void *)skb->data;
3911 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3912 struct sta_info *sta = rx->sta;
3913 int orig_len = skb->len;
3914 int snap_offs = ieee80211_hdrlen(hdr->frame_control);
3915 struct {
3916 u8 snap[sizeof(rfc1042_header)];
3917 __be16 proto;
3918 } *payload __aligned(2);
3919 struct {
3920 u8 da[ETH_ALEN];
3921 u8 sa[ETH_ALEN];
3922 } addrs __aligned(2);
3923 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
3924
3925 if (fast_rx->uses_rss)
3926 stats = this_cpu_ptr(sta->pcpu_rx_stats);
3927
3928 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
3929 * to a common data structure; drivers can implement that per queue
3930 * but we don't have that information in mac80211
3931 */
3932 if (!(status->flag & RX_FLAG_DUP_VALIDATED))
3933 return false;
3934
3935#define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
3936
3937 /* If using encryption, we also need to have:
3938 * - PN_VALIDATED: similar, but the implementation is tricky
3939 * - DECRYPTED: necessary for PN_VALIDATED
3940 */
3941 if (fast_rx->key &&
3942 (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
3943 return false;
3944
3945 /* we don't deal with A-MSDU deaggregation here */
3946 if (status->rx_flags & IEEE80211_RX_AMSDU)
3947 return false;
3948
3949 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
3950 return false;
3951
3952 if (unlikely(ieee80211_is_frag(hdr)))
3953 return false;
3954
3955 /* Since our interface address cannot be multicast, this
3956 * implicitly also rejects multicast frames without the
3957 * explicit check.
3958 *
3959 * We shouldn't get any *data* frames not addressed to us
3960 * (AP mode will accept multicast *management* frames), but
3961 * punting here will make it go through the full checks in
3962 * ieee80211_accept_frame().
3963 */
3964 if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
3965 return false;
3966
3967 if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
3968 IEEE80211_FCTL_TODS)) !=
3969 fast_rx->expected_ds_bits)
3970 return false;
3971
3972 /* assign the key to drop unencrypted frames (later)
3973 * and strip the IV/MIC if necessary
3974 */
3975 if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
3976 /* GCMP header length is the same */
3977 snap_offs += IEEE80211_CCMP_HDR_LEN;
3978 }
3979
3980 if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
3981 goto drop;
3982 payload = (void *)(skb->data + snap_offs);
3983
3984 if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
3985 return false;
3986
3987 /* Don't handle these here since they require special code.
3988 * Accept AARP and IPX even though they should come with a
3989 * bridge-tunnel header - but if we get them this way then
3990 * there's little point in discarding them.
3991 */
3992 if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
3993 payload->proto == fast_rx->control_port_protocol))
3994 return false;
3995
3996 /* after this point, don't punt to the slowpath! */
3997
3998 if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
3999 pskb_trim(skb, skb->len - fast_rx->icv_len))
4000 goto drop;
4001
4002 if (unlikely(fast_rx->sta_notify)) {
4003 ieee80211_sta_rx_notify(rx->sdata, hdr);
4004 fast_rx->sta_notify = false;
4005 }
4006
4007 /* statistics part of ieee80211_rx_h_sta_process() */
4008 stats->last_rx = jiffies;
4009 stats->last_rate = sta_stats_encode_rate(status);
4010
4011 stats->fragments++;
4012 stats->packets++;
4013
4014 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
4015 stats->last_signal = status->signal;
4016 if (!fast_rx->uses_rss)
4017 ewma_signal_add(&sta->rx_stats_avg.signal,
4018 -status->signal);
4019 }
4020
4021 if (status->chains) {
4022 int i;
4023
4024 stats->chains = status->chains;
4025 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
4026 int signal = status->chain_signal[i];
4027
4028 if (!(status->chains & BIT(i)))
4029 continue;
4030
4031 stats->chain_signal_last[i] = signal;
4032 if (!fast_rx->uses_rss)
4033 ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
4034 -signal);
4035 }
4036 }
4037 /* end of statistics */
4038
4039 if (rx->key && !ieee80211_has_protected(hdr->frame_control))
4040 goto drop;
4041
4042 /* do the header conversion - first grab the addresses */
4043 ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
4044 ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
4045 /* remove the SNAP but leave the ethertype */
4046 skb_pull(skb, snap_offs + sizeof(rfc1042_header));
4047 /* push the addresses in front */
4048 memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
4049
4050 skb->dev = fast_rx->dev;
4051
4052 ieee80211_rx_stats(fast_rx->dev, skb->len);
4053
4054 /* The seqno index has the same property as needed
4055 * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
4056 * for non-QoS-data frames. Here we know it's a data
4057 * frame, so count MSDUs.
4058 */
4059 u64_stats_update_begin(&stats->syncp);
4060 stats->msdu[rx->seqno_idx]++;
4061 stats->bytes += orig_len;
4062 u64_stats_update_end(&stats->syncp);
4063
4064 if (fast_rx->internal_forward) {
4065 struct sk_buff *xmit_skb = NULL;
4066 bool multicast = is_multicast_ether_addr(skb->data);
4067
4068 if (multicast) {
4069 xmit_skb = skb_copy(skb, GFP_ATOMIC);
4070 } else if (sta_info_get(rx->sdata, skb->data)) {
4071 xmit_skb = skb;
4072 skb = NULL;
4073 }
4074
4075 if (xmit_skb) {
4076 /*
4077 * Send to wireless media and increase priority by 256
4078 * to keep the received priority instead of
4079 * reclassifying the frame (see cfg80211_classify8021d).
4080 */
4081 xmit_skb->priority += 256;
4082 xmit_skb->protocol = htons(ETH_P_802_3);
4083 skb_reset_network_header(xmit_skb);
4084 skb_reset_mac_header(xmit_skb);
4085 dev_queue_xmit(xmit_skb);
4086 }
4087
4088 if (!skb)
4089 return true;
4090 }
4091
4092 /* deliver to local stack */
4093 skb->protocol = eth_type_trans(skb, fast_rx->dev);
4094 memset(skb->cb, 0, sizeof(skb->cb));
4095 if (rx->napi)
4096 napi_gro_receive(rx->napi, skb);
4097 else
4098 netif_receive_skb(skb);
4099
4100 return true;
4101 drop:
4102 dev_kfree_skb(skb);
4103 stats->dropped++;
4104 return true;
4105}
4106
4107/*
4108 * This function returns whether or not the SKB
4109 * was destined for RX processing or not, which,
4110 * if consume is true, is equivalent to whether
4111 * or not the skb was consumed.
4112 */
4113static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
4114 struct sk_buff *skb, bool consume)
4115{
4116 struct ieee80211_local *local = rx->local;
4117 struct ieee80211_sub_if_data *sdata = rx->sdata;
4118
4119 rx->skb = skb;
4120
4121 /* See if we can do fast-rx; if we have to copy we already lost,
4122 * so punt in that case. We should never have to deliver a data
4123 * frame to multiple interfaces anyway.
4124 *
4125 * We skip the ieee80211_accept_frame() call and do the necessary
4126 * checking inside ieee80211_invoke_fast_rx().
4127 */
4128 if (consume && rx->sta) {
4129 struct ieee80211_fast_rx *fast_rx;
4130
4131 fast_rx = rcu_dereference(rx->sta->fast_rx);
4132 if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
4133 return true;
4134 }
4135
4136 if (!ieee80211_accept_frame(rx))
4137 return false;
4138
4139 if (!consume) {
4140 skb = skb_copy(skb, GFP_ATOMIC);
4141 if (!skb) {
4142 if (net_ratelimit())
4143 wiphy_debug(local->hw.wiphy,
4144 "failed to copy skb for %s\n",
4145 sdata->name);
4146 return true;
4147 }
4148
4149 rx->skb = skb;
4150 }
4151
4152 ieee80211_invoke_rx_handlers(rx);
4153 return true;
4154}
4155
4156/*
4157 * This is the actual Rx frames handler. as it belongs to Rx path it must
4158 * be called with rcu_read_lock protection.
4159 */
4160static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
4161 struct ieee80211_sta *pubsta,
4162 struct sk_buff *skb,
4163 struct napi_struct *napi)
4164{
4165 struct ieee80211_local *local = hw_to_local(hw);
4166 struct ieee80211_sub_if_data *sdata;
4167 struct ieee80211_hdr *hdr;
4168 __le16 fc;
4169 struct ieee80211_rx_data rx;
4170 struct ieee80211_sub_if_data *prev;
4171 struct rhlist_head *tmp;
4172 int err = 0;
4173
4174 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
4175 memset(&rx, 0, sizeof(rx));
4176 rx.skb = skb;
4177 rx.local = local;
4178 rx.napi = napi;
4179
4180 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
4181 I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
4182
4183 if (ieee80211_is_mgmt(fc)) {
4184 /* drop frame if too short for header */
4185 if (skb->len < ieee80211_hdrlen(fc))
4186 err = -ENOBUFS;
4187 else
4188 err = skb_linearize(skb);
4189 } else {
4190 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4191 }
4192
4193 if (err) {
4194 dev_kfree_skb(skb);
4195 return;
4196 }
4197
4198 hdr = (struct ieee80211_hdr *)skb->data;
4199 ieee80211_parse_qos(&rx);
4200 ieee80211_verify_alignment(&rx);
4201
4202 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
4203 ieee80211_is_beacon(hdr->frame_control)))
4204 ieee80211_scan_rx(local, skb);
4205
4206 if (ieee80211_is_data(fc)) {
4207 struct sta_info *sta, *prev_sta;
4208
4209 if (pubsta) {
4210 rx.sta = container_of(pubsta, struct sta_info, sta);
4211 rx.sdata = rx.sta->sdata;
4212 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4213 return;
4214 goto out;
4215 }
4216
4217 prev_sta = NULL;
4218
4219 for_each_sta_info(local, hdr->addr2, sta, tmp) {
4220 if (!prev_sta) {
4221 prev_sta = sta;
4222 continue;
4223 }
4224
4225 rx.sta = prev_sta;
4226 rx.sdata = prev_sta->sdata;
4227 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4228
4229 prev_sta = sta;
4230 }
4231
4232 if (prev_sta) {
4233 rx.sta = prev_sta;
4234 rx.sdata = prev_sta->sdata;
4235
4236 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4237 return;
4238 goto out;
4239 }
4240 }
4241
4242 prev = NULL;
4243
4244 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4245 if (!ieee80211_sdata_running(sdata))
4246 continue;
4247
4248 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
4249 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4250 continue;
4251
4252 /*
4253 * frame is destined for this interface, but if it's
4254 * not also for the previous one we handle that after
4255 * the loop to avoid copying the SKB once too much
4256 */
4257
4258 if (!prev) {
4259 prev = sdata;
4260 continue;
4261 }
4262
4263 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4264 rx.sdata = prev;
4265 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4266
4267 prev = sdata;
4268 }
4269
4270 if (prev) {
4271 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4272 rx.sdata = prev;
4273
4274 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
4275 return;
4276 }
4277
4278 out:
4279 dev_kfree_skb(skb);
4280}
4281
4282/*
4283 * This is the receive path handler. It is called by a low level driver when an
4284 * 802.11 MPDU is received from the hardware.
4285 */
4286void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
4287 struct sk_buff *skb, struct napi_struct *napi)
4288{
4289 struct ieee80211_local *local = hw_to_local(hw);
4290 struct ieee80211_rate *rate = NULL;
4291 struct ieee80211_supported_band *sband;
4292 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
4293
4294 WARN_ON_ONCE(softirq_count() == 0);
4295
4296 if (WARN_ON(status->band >= NUM_NL80211_BANDS))
4297 goto drop;
4298
4299 sband = local->hw.wiphy->bands[status->band];
4300 if (WARN_ON(!sband))
4301 goto drop;
4302
4303 /*
4304 * If we're suspending, it is possible although not too likely
4305 * that we'd be receiving frames after having already partially
4306 * quiesced the stack. We can't process such frames then since
4307 * that might, for example, cause stations to be added or other
4308 * driver callbacks be invoked.
4309 */
4310 if (unlikely(local->quiescing || local->suspended))
4311 goto drop;
4312
4313 /* We might be during a HW reconfig, prevent Rx for the same reason */
4314 if (unlikely(local->in_reconfig))
4315 goto drop;
4316
4317 /*
4318 * The same happens when we're not even started,
4319 * but that's worth a warning.
4320 */
4321 if (WARN_ON(!local->started))
4322 goto drop;
4323
4324 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
4325 /*
4326 * Validate the rate, unless a PLCP error means that
4327 * we probably can't have a valid rate here anyway.
4328 */
4329
4330 switch (status->encoding) {
4331 case RX_ENC_HT:
4332 /*
4333 * rate_idx is MCS index, which can be [0-76]
4334 * as documented on:
4335 *
4336 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
4337 *
4338 * Anything else would be some sort of driver or
4339 * hardware error. The driver should catch hardware
4340 * errors.
4341 */
4342 if (WARN(status->rate_idx > 76,
4343 "Rate marked as an HT rate but passed "
4344 "status->rate_idx is not "
4345 "an MCS index [0-76]: %d (0x%02x)\n",
4346 status->rate_idx,
4347 status->rate_idx))
4348 goto drop;
4349 break;
4350 case RX_ENC_VHT:
4351 if (WARN_ONCE(status->rate_idx > 9 ||
4352 !status->nss ||
4353 status->nss > 8,
4354 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
4355 status->rate_idx, status->nss))
4356 goto drop;
4357 break;
4358 default:
4359 WARN_ON_ONCE(1);
4360 /* fall through */
4361 case RX_ENC_LEGACY:
4362 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
4363 goto drop;
4364 rate = &sband->bitrates[status->rate_idx];
4365 }
4366 }
4367
4368 status->rx_flags = 0;
4369
4370 /*
4371 * key references and virtual interfaces are protected using RCU
4372 * and this requires that we are in a read-side RCU section during
4373 * receive processing
4374 */
4375 rcu_read_lock();
4376
4377 /*
4378 * Frames with failed FCS/PLCP checksum are not returned,
4379 * all other frames are returned without radiotap header
4380 * if it was previously present.
4381 * Also, frames with less than 16 bytes are dropped.
4382 */
4383 skb = ieee80211_rx_monitor(local, skb, rate);
4384 if (!skb) {
4385 rcu_read_unlock();
4386 return;
4387 }
4388
4389 ieee80211_tpt_led_trig_rx(local,
4390 ((struct ieee80211_hdr *)skb->data)->frame_control,
4391 skb->len);
4392
4393 __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
4394
4395 rcu_read_unlock();
4396
4397 return;
4398 drop:
4399 kfree_skb(skb);
4400}
4401EXPORT_SYMBOL(ieee80211_rx_napi);
4402
4403/* This is a version of the rx handler that can be called from hard irq
4404 * context. Post the skb on the queue and schedule the tasklet */
4405void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
4406{
4407 struct ieee80211_local *local = hw_to_local(hw);
4408
4409 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
4410
4411 skb->pkt_type = IEEE80211_RX_MSG;
4412 skb_queue_tail(&local->skb_queue, skb);
4413 tasklet_schedule(&local->tasklet);
4414}
4415EXPORT_SYMBOL(ieee80211_rx_irqsafe);