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