]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/mac80211/rx.c
mac80211: move VHT related RX_FLAG to another variable
[mirror_ubuntu-jammy-kernel.git] / net / mac80211 / rx.c
CommitLineData
571ecf67
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
84040805 5 * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
571ecf67
JB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
ab46623e 12#include <linux/jiffies.h>
5a0e3ad6 13#include <linux/slab.h>
571ecf67
JB
14#include <linux/kernel.h>
15#include <linux/skbuff.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
d4e46a3d 18#include <linux/rcupdate.h>
bc3b2d7f 19#include <linux/export.h>
571ecf67
JB
20#include <net/mac80211.h>
21#include <net/ieee80211_radiotap.h>
d26ad377 22#include <asm/unaligned.h>
571ecf67
JB
23
24#include "ieee80211_i.h"
24487981 25#include "driver-ops.h"
2c8dccc7 26#include "led.h"
33b64eb2 27#include "mesh.h"
571ecf67
JB
28#include "wep.h"
29#include "wpa.h"
30#include "tkip.h"
31#include "wme.h"
1d8d3dec 32#include "rate.h"
571ecf67 33
b2e7771e
JB
34/*
35 * monitor mode reception
36 *
37 * This function cleans up the SKB, i.e. it removes all the stuff
38 * only useful for monitoring.
39 */
40static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
0869aea0 41 struct sk_buff *skb)
b2e7771e 42{
b2e7771e
JB
43 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
44 if (likely(skb->len > FCS_LEN))
e3cf8b3f 45 __pskb_trim(skb, skb->len - FCS_LEN);
b2e7771e
JB
46 else {
47 /* driver bug */
48 WARN_ON(1);
49 dev_kfree_skb(skb);
0624760c 50 return NULL;
b2e7771e
JB
51 }
52 }
53
54 return skb;
55}
56
1df332e8 57static inline int should_drop_frame(struct sk_buff *skb, int present_fcs_len)
b2e7771e 58{
f1d58c25 59 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
605f1a5b
JB
60 struct ieee80211_hdr *hdr;
61
0059b2b1 62 hdr = (void *)(skb->data);
b2e7771e 63
4c298677
JB
64 if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
65 RX_FLAG_FAILED_PLCP_CRC |
66 RX_FLAG_AMPDU_IS_ZEROLEN))
b2e7771e 67 return 1;
0059b2b1 68 if (unlikely(skb->len < 16 + present_fcs_len))
b2e7771e 69 return 1;
87228f57
HH
70 if (ieee80211_is_ctl(hdr->frame_control) &&
71 !ieee80211_is_pspoll(hdr->frame_control) &&
72 !ieee80211_is_back_req(hdr->frame_control))
b2e7771e
JB
73 return 1;
74 return 0;
75}
76
601ae7f2 77static int
90b9e446
JB
78ieee80211_rx_radiotap_space(struct ieee80211_local *local,
79 struct ieee80211_rx_status *status)
601ae7f2
BR
80{
81 int len;
82
83 /* always present fields */
a144f378 84 len = sizeof(struct ieee80211_radiotap_header) + 8;
601ae7f2 85
a144f378 86 /* allocate extra bitmaps */
a144f378
JB
87 if (status->chains)
88 len += 4 * hweight8(status->chains);
90b9e446
JB
89
90 if (ieee80211_have_rx_timestamp(status)) {
91 len = ALIGN(len, 8);
601ae7f2 92 len += 8;
90b9e446 93 }
7fee5372 94 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
601ae7f2 95 len += 1;
601ae7f2 96
a144f378
JB
97 /* antenna field, if we don't have per-chain info */
98 if (!status->chains)
99 len += 1;
100
90b9e446
JB
101 /* padding for RX_FLAGS if necessary */
102 len = ALIGN(len, 2);
601ae7f2 103
6d744bac
JB
104 if (status->flag & RX_FLAG_HT) /* HT info */
105 len += 3;
106
4c298677 107 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
90b9e446 108 len = ALIGN(len, 4);
4c298677
JB
109 len += 8;
110 }
111
51648921
JB
112 if (status->flag & RX_FLAG_VHT) {
113 len = ALIGN(len, 2);
114 len += 12;
115 }
116
a144f378
JB
117 if (status->chains) {
118 /* antenna and antenna signal fields */
119 len += 2 * hweight8(status->chains);
120 }
121
601ae7f2
BR
122 return len;
123}
124
00ea6deb 125/*
601ae7f2
BR
126 * ieee80211_add_rx_radiotap_header - add radiotap header
127 *
128 * add a radiotap header containing all the fields which the hardware provided.
129 */
130static void
131ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
132 struct sk_buff *skb,
601ae7f2 133 struct ieee80211_rate *rate,
973ef21a 134 int rtap_len, bool has_fcs)
601ae7f2 135{
f1d58c25 136 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
601ae7f2
BR
137 struct ieee80211_radiotap_header *rthdr;
138 unsigned char *pos;
a144f378
JB
139 __le32 *it_present;
140 u32 it_present_val;
6a86b9c7 141 u16 rx_flags = 0;
a5e70697 142 u16 channel_flags = 0;
a144f378
JB
143 int mpdulen, chain;
144 unsigned long chains = status->chains;
f4bda337
TP
145
146 mpdulen = skb->len;
147 if (!(has_fcs && (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)))
148 mpdulen += FCS_LEN;
601ae7f2
BR
149
150 rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
151 memset(rthdr, 0, rtap_len);
a144f378 152 it_present = &rthdr->it_present;
601ae7f2
BR
153
154 /* radiotap header, set always present flags */
0059b2b1 155 rthdr->it_len = cpu_to_le16(rtap_len);
a144f378
JB
156 it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
157 BIT(IEEE80211_RADIOTAP_CHANNEL) |
158 BIT(IEEE80211_RADIOTAP_RX_FLAGS);
159
160 if (!status->chains)
161 it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
162
163 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
164 it_present_val |=
165 BIT(IEEE80211_RADIOTAP_EXT) |
166 BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
167 put_unaligned_le32(it_present_val, it_present);
168 it_present++;
169 it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
170 BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
171 }
601ae7f2 172
a144f378
JB
173 put_unaligned_le32(it_present_val, it_present);
174
175 pos = (void *)(it_present + 1);
176
601ae7f2
BR
177 /* the order of the following fields is important */
178
179 /* IEEE80211_RADIOTAP_TSFT */
f4bda337 180 if (ieee80211_have_rx_timestamp(status)) {
90b9e446
JB
181 /* padding */
182 while ((pos - (u8 *)rthdr) & 7)
183 *pos++ = 0;
f4bda337
TP
184 put_unaligned_le64(
185 ieee80211_calculate_rx_timestamp(local, status,
186 mpdulen, 0),
187 pos);
1df332e8 188 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
601ae7f2
BR
189 pos += 8;
190 }
191
192 /* IEEE80211_RADIOTAP_FLAGS */
973ef21a 193 if (has_fcs && (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS))
601ae7f2 194 *pos |= IEEE80211_RADIOTAP_F_FCS;
aae89831
JB
195 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
196 *pos |= IEEE80211_RADIOTAP_F_BADFCS;
b4f28bbb
BR
197 if (status->flag & RX_FLAG_SHORTPRE)
198 *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
601ae7f2
BR
199 pos++;
200
201 /* IEEE80211_RADIOTAP_RATE */
5614618e 202 if (!rate || status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) {
0fb8ca45 203 /*
f8d1ccf1 204 * Without rate information don't add it. If we have,
38f37be2 205 * MCS information is a separate field in radiotap,
73b48099
JB
206 * added below. The byte here is needed as padding
207 * for the channel though, so initialise it to 0.
0fb8ca45
JM
208 */
209 *pos = 0;
8d6f658e 210 } else {
2103dec1 211 int shift = 0;
ebe6c7ba 212 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
2103dec1
SW
213 if (status->flag & RX_FLAG_10MHZ)
214 shift = 1;
215 else if (status->flag & RX_FLAG_5MHZ)
216 shift = 2;
217 *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
8d6f658e 218 }
601ae7f2
BR
219 pos++;
220
221 /* IEEE80211_RADIOTAP_CHANNEL */
6a86b9c7 222 put_unaligned_le16(status->freq, pos);
601ae7f2 223 pos += 2;
a5e70697
SW
224 if (status->flag & RX_FLAG_10MHZ)
225 channel_flags |= IEEE80211_CHAN_HALF;
226 else if (status->flag & RX_FLAG_5MHZ)
227 channel_flags |= IEEE80211_CHAN_QUARTER;
228
601ae7f2 229 if (status->band == IEEE80211_BAND_5GHZ)
a5e70697 230 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
5614618e 231 else if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
a5e70697 232 channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
f8d1ccf1 233 else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
a5e70697 234 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
f8d1ccf1 235 else if (rate)
a5e70697 236 channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
f8d1ccf1 237 else
a5e70697
SW
238 channel_flags |= IEEE80211_CHAN_2GHZ;
239 put_unaligned_le16(channel_flags, pos);
601ae7f2
BR
240 pos += 2;
241
242 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
fe8431f8
FF
243 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM &&
244 !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
601ae7f2
BR
245 *pos = status->signal;
246 rthdr->it_present |=
247 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
248 pos++;
249 }
250
601ae7f2
BR
251 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
252
a144f378
JB
253 if (!status->chains) {
254 /* IEEE80211_RADIOTAP_ANTENNA */
255 *pos = status->antenna;
256 pos++;
257 }
601ae7f2 258
601ae7f2
BR
259 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
260
261 /* IEEE80211_RADIOTAP_RX_FLAGS */
262 /* ensure 2 byte alignment for the 2 byte field as required */
6a86b9c7 263 if ((pos - (u8 *)rthdr) & 1)
90b9e446 264 *pos++ = 0;
aae89831 265 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
6a86b9c7
JB
266 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
267 put_unaligned_le16(rx_flags, pos);
601ae7f2 268 pos += 2;
6d744bac
JB
269
270 if (status->flag & RX_FLAG_HT) {
786677d1
OR
271 unsigned int stbc;
272
6d744bac 273 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
ac55d2fe 274 *pos++ = local->hw.radiotap_mcs_details;
6d744bac
JB
275 *pos = 0;
276 if (status->flag & RX_FLAG_SHORT_GI)
277 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
278 if (status->flag & RX_FLAG_40MHZ)
279 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
ac55d2fe
JB
280 if (status->flag & RX_FLAG_HT_GF)
281 *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
786677d1
OR
282 stbc = (status->flag & RX_FLAG_STBC_MASK) >> RX_FLAG_STBC_SHIFT;
283 *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
6d744bac
JB
284 pos++;
285 *pos++ = status->rate_idx;
286 }
4c298677
JB
287
288 if (status->flag & RX_FLAG_AMPDU_DETAILS) {
289 u16 flags = 0;
290
291 /* ensure 4 byte alignment */
292 while ((pos - (u8 *)rthdr) & 3)
293 pos++;
294 rthdr->it_present |=
295 cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
296 put_unaligned_le32(status->ampdu_reference, pos);
297 pos += 4;
298 if (status->flag & RX_FLAG_AMPDU_REPORT_ZEROLEN)
299 flags |= IEEE80211_RADIOTAP_AMPDU_REPORT_ZEROLEN;
300 if (status->flag & RX_FLAG_AMPDU_IS_ZEROLEN)
301 flags |= IEEE80211_RADIOTAP_AMPDU_IS_ZEROLEN;
302 if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
303 flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
304 if (status->flag & RX_FLAG_AMPDU_IS_LAST)
305 flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
306 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
307 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
308 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
309 flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
310 put_unaligned_le16(flags, pos);
311 pos += 2;
312 if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
313 *pos++ = status->ampdu_delimiter_crc;
314 else
315 *pos++ = 0;
316 *pos++ = 0;
317 }
90b9e446 318
51648921
JB
319 if (status->flag & RX_FLAG_VHT) {
320 u16 known = local->hw.radiotap_vht_details;
321
322 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
323 /* known field - how to handle 80+80? */
1b8d242a 324 if (status->vht_flag & RX_VHT_FLAG_80P80MHZ)
51648921
JB
325 known &= ~IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
326 put_unaligned_le16(known, pos);
327 pos += 2;
328 /* flags */
329 if (status->flag & RX_FLAG_SHORT_GI)
330 *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
331 pos++;
332 /* bandwidth */
1b8d242a 333 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
51648921 334 *pos++ = 4;
1b8d242a 335 else if (status->vht_flag & RX_VHT_FLAG_80P80MHZ)
51648921 336 *pos++ = 0; /* marked not known above */
1b8d242a 337 else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
51648921
JB
338 *pos++ = 11;
339 else if (status->flag & RX_FLAG_40MHZ)
340 *pos++ = 1;
341 else /* 20 MHz */
342 *pos++ = 0;
343 /* MCS/NSS */
344 *pos = (status->rate_idx << 4) | status->vht_nss;
345 pos += 4;
346 /* coding field */
347 pos++;
348 /* group ID */
349 pos++;
350 /* partial_aid */
351 pos += 2;
352 }
353
a144f378
JB
354 for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
355 *pos++ = status->chain_signal[chain];
356 *pos++ = chain;
357 }
601ae7f2
BR
358}
359
b2e7771e
JB
360/*
361 * This function copies a received frame to all monitor interfaces and
362 * returns a cleaned-up SKB that no longer includes the FCS nor the
363 * radiotap header the driver might have added.
364 */
365static struct sk_buff *
366ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
8318d78a 367 struct ieee80211_rate *rate)
b2e7771e 368{
f1d58c25 369 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
b2e7771e 370 struct ieee80211_sub_if_data *sdata;
293702a3 371 int needed_headroom;
b2e7771e
JB
372 struct sk_buff *skb, *skb2;
373 struct net_device *prev_dev = NULL;
374 int present_fcs_len = 0;
b2e7771e
JB
375
376 /*
377 * First, we may need to make a copy of the skb because
378 * (1) we need to modify it for radiotap (if not present), and
379 * (2) the other RX handlers will modify the skb we got.
380 *
381 * We don't need to, of course, if we aren't going to return
382 * the SKB because it has a bad FCS/PLCP checksum.
383 */
0869aea0 384
b2e7771e
JB
385 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
386 present_fcs_len = FCS_LEN;
387
0059b2b1
EG
388 /* ensure hdr->frame_control is in skb head */
389 if (!pskb_may_pull(origskb, 2)) {
e3cf8b3f
ZY
390 dev_kfree_skb(origskb);
391 return NULL;
392 }
393
b2e7771e 394 if (!local->monitors) {
0869aea0 395 if (should_drop_frame(origskb, present_fcs_len)) {
b2e7771e
JB
396 dev_kfree_skb(origskb);
397 return NULL;
398 }
399
0869aea0 400 return remove_monitor_info(local, origskb);
b2e7771e
JB
401 }
402
751413ea
HS
403 /* room for the radiotap header based on driver features */
404 needed_headroom = ieee80211_rx_radiotap_space(local, status);
405
0869aea0 406 if (should_drop_frame(origskb, present_fcs_len)) {
b2e7771e
JB
407 /* only need to expand headroom if necessary */
408 skb = origskb;
409 origskb = NULL;
410
411 /*
412 * This shouldn't trigger often because most devices have an
413 * RX header they pull before we get here, and that should
414 * be big enough for our radiotap information. We should
415 * probably export the length to drivers so that we can have
416 * them allocate enough headroom to start with.
417 */
418 if (skb_headroom(skb) < needed_headroom &&
c49e5ea3 419 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
b2e7771e
JB
420 dev_kfree_skb(skb);
421 return NULL;
422 }
423 } else {
424 /*
425 * Need to make a copy and possibly remove radiotap header
426 * and FCS from the original.
427 */
428 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
429
0869aea0 430 origskb = remove_monitor_info(local, origskb);
b2e7771e
JB
431
432 if (!skb)
433 return origskb;
434 }
435
0869aea0 436 /* prepend radiotap information */
973ef21a
FF
437 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
438 true);
b2e7771e 439
ce3edf6d 440 skb_reset_mac_header(skb);
b2e7771e
JB
441 skb->ip_summed = CHECKSUM_UNNECESSARY;
442 skb->pkt_type = PACKET_OTHERHOST;
443 skb->protocol = htons(ETH_P_802_2);
444
445 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
05c914fe 446 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
b2e7771e
JB
447 continue;
448
3d30d949
MW
449 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
450 continue;
451
9607e6b6 452 if (!ieee80211_sdata_running(sdata))
47846c9b
JB
453 continue;
454
b2e7771e
JB
455 if (prev_dev) {
456 skb2 = skb_clone(skb, GFP_ATOMIC);
457 if (skb2) {
458 skb2->dev = prev_dev;
5548a8a1 459 netif_receive_skb(skb2);
b2e7771e
JB
460 }
461 }
462
463 prev_dev = sdata->dev;
464 sdata->dev->stats.rx_packets++;
465 sdata->dev->stats.rx_bytes += skb->len;
466 }
467
468 if (prev_dev) {
469 skb->dev = prev_dev;
5548a8a1 470 netif_receive_skb(skb);
b2e7771e
JB
471 } else
472 dev_kfree_skb(skb);
473
474 return origskb;
475}
476
5cf121c3 477static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
6e0d114d 478{
238f74a2 479 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 480 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
9e26297a 481 int tid, seqno_idx, security_idx;
6e0d114d
JB
482
483 /* does the frame have a qos control field? */
238f74a2
HH
484 if (ieee80211_is_data_qos(hdr->frame_control)) {
485 u8 *qc = ieee80211_get_qos_ctl(hdr);
6e0d114d 486 /* frame has qos control */
238f74a2 487 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
04b7dcf9 488 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
554891e6 489 status->rx_flags |= IEEE80211_RX_AMSDU;
9e26297a
JB
490
491 seqno_idx = tid;
492 security_idx = tid;
6e0d114d 493 } else {
1411f9b5
JB
494 /*
495 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
496 *
497 * Sequence numbers for management frames, QoS data
498 * frames with a broadcast/multicast address in the
499 * Address 1 field, and all non-QoS data frames sent
500 * by QoS STAs are assigned using an additional single
501 * modulo-4096 counter, [...]
502 *
503 * We also use that counter for non-QoS STAs.
504 */
5a306f58 505 seqno_idx = IEEE80211_NUM_TIDS;
9e26297a
JB
506 security_idx = 0;
507 if (ieee80211_is_mgmt(hdr->frame_control))
5a306f58 508 security_idx = IEEE80211_NUM_TIDS;
9e26297a 509 tid = 0;
6e0d114d 510 }
52865dfd 511
9e26297a
JB
512 rx->seqno_idx = seqno_idx;
513 rx->security_idx = security_idx;
6e0d114d
JB
514 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
515 * For now, set skb->priority to 0 for other cases. */
516 rx->skb->priority = (tid > 7) ? 0 : tid;
38f3714d 517}
6e0d114d 518
d1c3a37c
JB
519/**
520 * DOC: Packet alignment
521 *
522 * Drivers always need to pass packets that are aligned to two-byte boundaries
523 * to the stack.
524 *
525 * Additionally, should, if possible, align the payload data in a way that
526 * guarantees that the contained IP header is aligned to a four-byte
527 * boundary. In the case of regular frames, this simply means aligning the
528 * payload to a four-byte boundary (because either the IP header is directly
529 * contained, or IV/RFC1042 headers that have a length divisible by four are
59d9cb07
KV
530 * in front of it). If the payload data is not properly aligned and the
531 * architecture doesn't support efficient unaligned operations, mac80211
532 * will align the data.
d1c3a37c
JB
533 *
534 * With A-MSDU frames, however, the payload data address must yield two modulo
535 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
536 * push the IP header further back to a multiple of four again. Thankfully, the
537 * specs were sane enough this time around to require padding each A-MSDU
538 * subframe to a length that is a multiple of four.
539 *
25985edc 540 * Padding like Atheros hardware adds which is between the 802.11 header and
d1c3a37c
JB
541 * the payload is not supported, the driver is required to move the 802.11
542 * header to be directly in front of the payload in that case.
543 */
544static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
38f3714d 545{
59d9cb07
KV
546#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
547 WARN_ONCE((unsigned long)rx->skb->data & 1,
548 "unaligned packet at 0x%p\n", rx->skb->data);
d1c3a37c 549#endif
6e0d114d
JB
550}
551
6368e4b1 552
571ecf67
JB
553/* rx handlers */
554
3cfcf6ac
JM
555static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
556{
557 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
558
d8ca16db 559 if (is_multicast_ether_addr(hdr->addr1))
3cfcf6ac
JM
560 return 0;
561
d8ca16db 562 return ieee80211_is_robust_mgmt_frame(skb);
3cfcf6ac
JM
563}
564
565
566static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
567{
568 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
569
d8ca16db 570 if (!is_multicast_ether_addr(hdr->addr1))
3cfcf6ac
JM
571 return 0;
572
d8ca16db 573 return ieee80211_is_robust_mgmt_frame(skb);
3cfcf6ac
JM
574}
575
576
577/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
578static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
579{
580 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
581 struct ieee80211_mmie *mmie;
582
1df332e8 583 if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
3cfcf6ac
JM
584 return -1;
585
d8ca16db 586 if (!ieee80211_is_robust_mgmt_frame(skb))
3cfcf6ac
JM
587 return -1; /* not a robust management frame */
588
589 mmie = (struct ieee80211_mmie *)
590 (skb->data + skb->len - sizeof(*mmie));
591 if (mmie->element_id != WLAN_EID_MMIE ||
592 mmie->length != sizeof(*mmie) - 2)
593 return -1;
594
595 return le16_to_cpu(mmie->key_id);
596}
597
2475b1cc
MS
598static int iwl80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
599 struct sk_buff *skb)
600{
601 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
602 __le16 fc;
603 int hdrlen;
604 u8 keyid;
605
606 fc = hdr->frame_control;
607 hdrlen = ieee80211_hdrlen(fc);
608
609 if (skb->len < hdrlen + cs->hdr_len)
610 return -EINVAL;
611
612 skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
613 keyid &= cs->key_idx_mask;
614 keyid >>= cs->key_idx_shift;
615
616 return keyid;
617}
618
1df332e8 619static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
33b64eb2 620{
a7767f95 621 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
47846c9b 622 char *dev_addr = rx->sdata->vif.addr;
d6d1a5a7 623
a7767f95 624 if (ieee80211_is_data(hdr->frame_control)) {
3c5772a5
JC
625 if (is_multicast_ether_addr(hdr->addr1)) {
626 if (ieee80211_has_tods(hdr->frame_control) ||
1df332e8 627 !ieee80211_has_fromds(hdr->frame_control))
3c5772a5 628 return RX_DROP_MONITOR;
b203ca39 629 if (ether_addr_equal(hdr->addr3, dev_addr))
3c5772a5
JC
630 return RX_DROP_MONITOR;
631 } else {
632 if (!ieee80211_has_a4(hdr->frame_control))
633 return RX_DROP_MONITOR;
b203ca39 634 if (ether_addr_equal(hdr->addr4, dev_addr))
3c5772a5
JC
635 return RX_DROP_MONITOR;
636 }
33b64eb2
LCC
637 }
638
639 /* If there is not an established peer link and this is not a peer link
640 * establisment frame, beacon or probe, drop the frame.
641 */
642
57cf8043 643 if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
33b64eb2 644 struct ieee80211_mgmt *mgmt;
d6d1a5a7 645
a7767f95 646 if (!ieee80211_is_mgmt(hdr->frame_control))
33b64eb2
LCC
647 return RX_DROP_MONITOR;
648
a7767f95 649 if (ieee80211_is_action(hdr->frame_control)) {
d3aaec8a 650 u8 category;
9b395bc3
JB
651
652 /* make sure category field is present */
653 if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
654 return RX_DROP_MONITOR;
655
33b64eb2 656 mgmt = (struct ieee80211_mgmt *)hdr;
d3aaec8a
JC
657 category = mgmt->u.action.category;
658 if (category != WLAN_CATEGORY_MESH_ACTION &&
1df332e8 659 category != WLAN_CATEGORY_SELF_PROTECTED)
33b64eb2 660 return RX_DROP_MONITOR;
33b64eb2 661 return RX_CONTINUE;
33b64eb2
LCC
662 }
663
a7767f95
HH
664 if (ieee80211_is_probe_req(hdr->frame_control) ||
665 ieee80211_is_probe_resp(hdr->frame_control) ||
71839121
JC
666 ieee80211_is_beacon(hdr->frame_control) ||
667 ieee80211_is_auth(hdr->frame_control))
a7767f95
HH
668 return RX_CONTINUE;
669
670 return RX_DROP_MONITOR;
a7767f95
HH
671 }
672
902acc78
JB
673 return RX_CONTINUE;
674}
33b64eb2 675
d3b2fb53 676static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
1edfb1af 677 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
678 int index,
679 struct sk_buff_head *frames)
1edfb1af 680{
1edfb1af 681 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
4cfda47b 682 struct ieee80211_rx_status *status;
1edfb1af 683
dd318575
JB
684 lockdep_assert_held(&tid_agg_rx->reorder_lock);
685
1edfb1af
JB
686 if (!skb)
687 goto no_frame;
688
071d9ac2 689 /* release the frame from the reorder ring buffer */
1edfb1af
JB
690 tid_agg_rx->stored_mpdu_num--;
691 tid_agg_rx->reorder_buf[index] = NULL;
4cfda47b
CL
692 status = IEEE80211_SKB_RXCB(skb);
693 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
f9e124fb 694 __skb_queue_tail(frames, skb);
1edfb1af
JB
695
696no_frame:
9a886586 697 tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
1edfb1af
JB
698}
699
d3b2fb53 700static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
1edfb1af 701 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
702 u16 head_seq_num,
703 struct sk_buff_head *frames)
1edfb1af
JB
704{
705 int index;
706
dd318575
JB
707 lockdep_assert_held(&tid_agg_rx->reorder_lock);
708
9a886586 709 while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2e3049b7 710 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
f9e124fb
CL
711 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
712 frames);
1edfb1af
JB
713 }
714}
715
716/*
717 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
718 * the skb was added to the buffer longer than this time ago, the earlier
719 * frames that have not yet been received are assumed to be lost and the skb
720 * can be released for processing. This may also release other skb's from the
721 * reorder buffer if there are no additional gaps between the frames.
2bff8ebf
CL
722 *
723 * Callers must hold tid_agg_rx->reorder_lock.
1edfb1af
JB
724 */
725#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
726
d3b2fb53 727static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
f9e124fb
CL
728 struct tid_ampdu_rx *tid_agg_rx,
729 struct sk_buff_head *frames)
aa0c8636 730{
2bff8ebf 731 int index, j;
aa0c8636 732
dd318575
JB
733 lockdep_assert_held(&tid_agg_rx->reorder_lock);
734
aa0c8636 735 /* release the buffer until next missing frame */
2e3049b7 736 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
aa0c8636 737 if (!tid_agg_rx->reorder_buf[index] &&
07ae2dfc 738 tid_agg_rx->stored_mpdu_num) {
aa0c8636
CL
739 /*
740 * No buffers ready to be released, but check whether any
741 * frames in the reorder buffer have timed out.
742 */
aa0c8636
CL
743 int skipped = 1;
744 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
745 j = (j + 1) % tid_agg_rx->buf_size) {
746 if (!tid_agg_rx->reorder_buf[j]) {
747 skipped++;
748 continue;
749 }
499fe9a4
DH
750 if (skipped &&
751 !time_after(jiffies, tid_agg_rx->reorder_time[j] +
aa0c8636 752 HT_RX_REORDER_BUF_TIMEOUT))
2bff8ebf 753 goto set_release_timer;
aa0c8636 754
bdcbd8e0
JB
755 ht_dbg_ratelimited(sdata,
756 "release an RX reorder frame due to timeout on earlier frames\n");
f9e124fb
CL
757 ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
758 frames);
aa0c8636
CL
759
760 /*
761 * Increment the head seq# also for the skipped slots.
762 */
763 tid_agg_rx->head_seq_num =
9a886586
JB
764 (tid_agg_rx->head_seq_num +
765 skipped) & IEEE80211_SN_MASK;
aa0c8636
CL
766 skipped = 0;
767 }
768 } else while (tid_agg_rx->reorder_buf[index]) {
f9e124fb
CL
769 ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
770 frames);
2e3049b7 771 index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
aa0c8636 772 }
2bff8ebf
CL
773
774 if (tid_agg_rx->stored_mpdu_num) {
2e3049b7 775 j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
2bff8ebf
CL
776
777 for (; j != (index - 1) % tid_agg_rx->buf_size;
778 j = (j + 1) % tid_agg_rx->buf_size) {
779 if (tid_agg_rx->reorder_buf[j])
780 break;
781 }
782
783 set_release_timer:
784
785 mod_timer(&tid_agg_rx->reorder_timer,
334df731 786 tid_agg_rx->reorder_time[j] + 1 +
2bff8ebf
CL
787 HT_RX_REORDER_BUF_TIMEOUT);
788 } else {
789 del_timer(&tid_agg_rx->reorder_timer);
790 }
aa0c8636
CL
791}
792
1edfb1af
JB
793/*
794 * As this function belongs to the RX path it must be under
795 * rcu_read_lock protection. It returns false if the frame
796 * can be processed immediately, true if it was consumed.
797 */
d3b2fb53 798static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
1edfb1af 799 struct tid_ampdu_rx *tid_agg_rx,
f9e124fb
CL
800 struct sk_buff *skb,
801 struct sk_buff_head *frames)
1edfb1af
JB
802{
803 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
804 u16 sc = le16_to_cpu(hdr->seq_ctrl);
805 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
806 u16 head_seq_num, buf_size;
807 int index;
2bff8ebf 808 bool ret = true;
1edfb1af 809
dd318575
JB
810 spin_lock(&tid_agg_rx->reorder_lock);
811
1edfb1af
JB
812 buf_size = tid_agg_rx->buf_size;
813 head_seq_num = tid_agg_rx->head_seq_num;
814
815 /* frame with out of date sequence number */
9a886586 816 if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
1edfb1af 817 dev_kfree_skb(skb);
2bff8ebf 818 goto out;
1edfb1af
JB
819 }
820
821 /*
822 * If frame the sequence number exceeds our buffering window
823 * size release some previous frames to make room for this one.
824 */
9a886586
JB
825 if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
826 head_seq_num = ieee80211_sn_inc(
827 ieee80211_sn_sub(mpdu_seq_num, buf_size));
1edfb1af 828 /* release stored frames up to new head to stack */
d3b2fb53 829 ieee80211_release_reorder_frames(sdata, tid_agg_rx,
f9e124fb 830 head_seq_num, frames);
1edfb1af
JB
831 }
832
833 /* Now the new frame is always in the range of the reordering buffer */
834
2e3049b7 835 index = mpdu_seq_num % tid_agg_rx->buf_size;
1edfb1af
JB
836
837 /* check if we already stored this frame */
838 if (tid_agg_rx->reorder_buf[index]) {
839 dev_kfree_skb(skb);
2bff8ebf 840 goto out;
1edfb1af
JB
841 }
842
843 /*
844 * If the current MPDU is in the right order and nothing else
845 * is stored we can process it directly, no need to buffer it.
c835b214
JB
846 * If it is first but there's something stored, we may be able
847 * to release frames after this one.
1edfb1af
JB
848 */
849 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
850 tid_agg_rx->stored_mpdu_num == 0) {
9a886586
JB
851 tid_agg_rx->head_seq_num =
852 ieee80211_sn_inc(tid_agg_rx->head_seq_num);
2bff8ebf
CL
853 ret = false;
854 goto out;
1edfb1af
JB
855 }
856
857 /* put the frame in the reordering buffer */
858 tid_agg_rx->reorder_buf[index] = skb;
859 tid_agg_rx->reorder_time[index] = jiffies;
860 tid_agg_rx->stored_mpdu_num++;
f9e124fb 861 ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
1edfb1af 862
2bff8ebf
CL
863 out:
864 spin_unlock(&tid_agg_rx->reorder_lock);
865 return ret;
1edfb1af
JB
866}
867
868/*
869 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
870 * true if the MPDU was buffered, false if it should be processed.
871 */
f9e124fb
CL
872static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
873 struct sk_buff_head *frames)
1edfb1af 874{
2569a826
JB
875 struct sk_buff *skb = rx->skb;
876 struct ieee80211_local *local = rx->local;
1edfb1af 877 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
660c6a44 878 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2569a826 879 struct sta_info *sta = rx->sta;
1edfb1af
JB
880 struct tid_ampdu_rx *tid_agg_rx;
881 u16 sc;
6cc00d54 882 u8 tid, ack_policy;
1edfb1af 883
051a41fa
JB
884 if (!ieee80211_is_data_qos(hdr->frame_control) ||
885 is_multicast_ether_addr(hdr->addr1))
2569a826 886 goto dont_reorder;
1edfb1af
JB
887
888 /*
889 * filter the QoS data rx stream according to
890 * STA/TID and check if this STA/TID is on aggregation
891 */
892
1edfb1af 893 if (!sta)
2569a826 894 goto dont_reorder;
1edfb1af 895
6cc00d54
TP
896 ack_policy = *ieee80211_get_qos_ctl(hdr) &
897 IEEE80211_QOS_CTL_ACK_POLICY_MASK;
1edfb1af
JB
898 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
899
a87f736d
JB
900 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
901 if (!tid_agg_rx)
902 goto dont_reorder;
1edfb1af
JB
903
904 /* qos null data frames are excluded */
905 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
a87f736d 906 goto dont_reorder;
1edfb1af 907
6cc00d54
TP
908 /* not part of a BA session */
909 if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
910 ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
911 goto dont_reorder;
912
660c6a44
TP
913 /* not actually part of this BA session */
914 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
915 goto dont_reorder;
916
1edfb1af
JB
917 /* new, potentially un-ordered, ampdu frame - process it */
918
919 /* reset session timer */
920 if (tid_agg_rx->timeout)
12d3952f 921 tid_agg_rx->last_rx = jiffies;
1edfb1af
JB
922
923 /* if this mpdu is fragmented - terminate rx aggregation session */
924 sc = le16_to_cpu(hdr->seq_ctrl);
925 if (sc & IEEE80211_SCTL_FRAG) {
c1475ca9 926 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
344eec67
JB
927 skb_queue_tail(&rx->sdata->skb_queue, skb);
928 ieee80211_queue_work(&local->hw, &rx->sdata->work);
2569a826 929 return;
1edfb1af
JB
930 }
931
a87f736d
JB
932 /*
933 * No locking needed -- we will only ever process one
934 * RX packet at a time, and thus own tid_agg_rx. All
935 * other code manipulating it needs to (and does) make
936 * sure that we cannot get to it any more before doing
937 * anything with it.
938 */
f9e124fb
CL
939 if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
940 frames))
2569a826
JB
941 return;
942
943 dont_reorder:
f9e124fb 944 __skb_queue_tail(frames, skb);
1edfb1af 945}
33b64eb2 946
49461622 947static ieee80211_rx_result debug_noinline
5cf121c3 948ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
571ecf67 949{
a7767f95 950 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 951 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
571ecf67 952
6b0f3274
JB
953 /*
954 * Drop duplicate 802.11 retransmissions
955 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
956 */
957 if (rx->skb->len >= 24 && rx->sta &&
958 !ieee80211_is_ctl(hdr->frame_control) &&
959 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
960 !is_multicast_ether_addr(hdr->addr1)) {
a7767f95 961 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
9e26297a 962 rx->sta->last_seq_ctrl[rx->seqno_idx] ==
571ecf67 963 hdr->seq_ctrl)) {
554891e6 964 if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
571ecf67
JB
965 rx->local->dot11FrameDuplicateCount++;
966 rx->sta->num_duplicates++;
967 }
b1f93314 968 return RX_DROP_UNUSABLE;
0cfcefef 969 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
9e26297a 970 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
0cfcefef 971 }
571ecf67
JB
972 }
973
571ecf67
JB
974 if (unlikely(rx->skb->len < 16)) {
975 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
e4c26add 976 return RX_DROP_MONITOR;
571ecf67
JB
977 }
978
571ecf67
JB
979 /* Drop disallowed frame classes based on STA auth/assoc state;
980 * IEEE 802.11, Chap 5.5.
981 *
ccd7b362
JB
982 * mac80211 filters only based on association state, i.e. it drops
983 * Class 3 frames from not associated stations. hostapd sends
571ecf67
JB
984 * deauth/disassoc frames when needed. In addition, hostapd is
985 * responsible for filtering on both auth and assoc states.
986 */
33b64eb2 987
902acc78 988 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
33b64eb2 989 return ieee80211_rx_mesh_check(rx);
33b64eb2 990
a7767f95
HH
991 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
992 ieee80211_is_pspoll(hdr->frame_control)) &&
05c914fe 993 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1be7fe8d 994 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
c2c98fde 995 (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
7852e361
JB
996 /*
997 * accept port control frames from the AP even when it's not
998 * yet marked ASSOC to prevent a race where we don't set the
999 * assoc bit quickly enough before it sends the first frame
1000 */
1001 if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
2a33bee2 1002 ieee80211_is_data_present(hdr->frame_control)) {
6dbda2d0
JB
1003 unsigned int hdrlen;
1004 __be16 ethertype;
1005
1006 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1007
1008 if (rx->skb->len < hdrlen + 8)
1009 return RX_DROP_MONITOR;
1010
1011 skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
1012 if (ethertype == rx->sdata->control_port_protocol)
2a33bee2
GE
1013 return RX_CONTINUE;
1014 }
21fc7560
JB
1015
1016 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
1017 cfg80211_rx_spurious_frame(rx->sdata->dev,
1018 hdr->addr2,
1019 GFP_ATOMIC))
1020 return RX_DROP_UNUSABLE;
1021
e4c26add 1022 return RX_DROP_MONITOR;
2a33bee2 1023 }
571ecf67 1024
9ae54c84 1025 return RX_CONTINUE;
570bd537
JB
1026}
1027
1028
572e0012
KV
1029static ieee80211_rx_result debug_noinline
1030ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1031{
1032 struct ieee80211_local *local;
1033 struct ieee80211_hdr *hdr;
1034 struct sk_buff *skb;
1035
1036 local = rx->local;
1037 skb = rx->skb;
1038 hdr = (struct ieee80211_hdr *) skb->data;
1039
1040 if (!local->pspolling)
1041 return RX_CONTINUE;
1042
1043 if (!ieee80211_has_fromds(hdr->frame_control))
1044 /* this is not from AP */
1045 return RX_CONTINUE;
1046
1047 if (!ieee80211_is_data(hdr->frame_control))
1048 return RX_CONTINUE;
1049
1050 if (!ieee80211_has_moredata(hdr->frame_control)) {
1051 /* AP has no more frames buffered for us */
1052 local->pspolling = false;
1053 return RX_CONTINUE;
1054 }
1055
1056 /* more data bit is set, let's request a new frame from the AP */
1057 ieee80211_send_pspoll(local, rx->sdata);
1058
1059 return RX_CONTINUE;
1060}
1061
d012a605 1062static void sta_ps_start(struct sta_info *sta)
571ecf67 1063{
133b8226 1064 struct ieee80211_sub_if_data *sdata = sta->sdata;
4571d3bf 1065 struct ieee80211_local *local = sdata->local;
d012a605 1066 struct ps_data *ps;
0795af57 1067
d012a605
MP
1068 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1069 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1070 ps = &sdata->bss->ps;
1071 else
1072 return;
1073
1074 atomic_inc(&ps->num_sta_ps);
c2c98fde 1075 set_sta_flag(sta, WLAN_STA_PS_STA);
d057e5a3
AN
1076 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1077 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
bdcbd8e0
JB
1078 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1079 sta->sta.addr, sta->sta.aid);
571ecf67
JB
1080}
1081
d012a605 1082static void sta_ps_end(struct sta_info *sta)
571ecf67 1083{
bdcbd8e0
JB
1084 ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
1085 sta->sta.addr, sta->sta.aid);
004c872e 1086
c2c98fde 1087 if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
bdcbd8e0
JB
1088 ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
1089 sta->sta.addr, sta->sta.aid);
af818581
JB
1090 return;
1091 }
1092
1093 ieee80211_sta_ps_deliver_wakeup(sta);
571ecf67
JB
1094}
1095
d057e5a3
AN
1096int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
1097{
1098 struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
1099 bool in_ps;
1100
1101 WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
1102
1103 /* Don't let the same PS state be set twice */
c2c98fde 1104 in_ps = test_sta_flag(sta_inf, WLAN_STA_PS_STA);
d057e5a3
AN
1105 if ((start && in_ps) || (!start && !in_ps))
1106 return -EINVAL;
1107
1108 if (start)
d012a605 1109 sta_ps_start(sta_inf);
d057e5a3 1110 else
d012a605 1111 sta_ps_end(sta_inf);
d057e5a3
AN
1112
1113 return 0;
1114}
1115EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1116
47086fc5
JB
1117static ieee80211_rx_result debug_noinline
1118ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
1119{
1120 struct ieee80211_sub_if_data *sdata = rx->sdata;
1121 struct ieee80211_hdr *hdr = (void *)rx->skb->data;
1122 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
1123 int tid, ac;
1124
1125 if (!rx->sta || !(status->rx_flags & IEEE80211_RX_RA_MATCH))
1126 return RX_CONTINUE;
1127
1128 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1129 sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
1130 return RX_CONTINUE;
1131
1132 /*
1133 * The device handles station powersave, so don't do anything about
1134 * uAPSD and PS-Poll frames (the latter shouldn't even come up from
1135 * it to mac80211 since they're handled.)
1136 */
1137 if (sdata->local->hw.flags & IEEE80211_HW_AP_LINK_PS)
1138 return RX_CONTINUE;
1139
1140 /*
1141 * Don't do anything if the station isn't already asleep. In
1142 * the uAPSD case, the station will probably be marked asleep,
1143 * in the PS-Poll case the station must be confused ...
1144 */
c2c98fde 1145 if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
47086fc5
JB
1146 return RX_CONTINUE;
1147
1148 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
c2c98fde
JB
1149 if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
1150 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
deeaee19
JB
1151 ieee80211_sta_ps_deliver_poll_response(rx->sta);
1152 else
c2c98fde 1153 set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
deeaee19 1154 }
47086fc5
JB
1155
1156 /* Free PS Poll skb here instead of returning RX_DROP that would
1157 * count as an dropped frame. */
1158 dev_kfree_skb(rx->skb);
1159
1160 return RX_QUEUED;
1161 } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
1162 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
1163 ieee80211_has_pm(hdr->frame_control) &&
1164 (ieee80211_is_data_qos(hdr->frame_control) ||
1165 ieee80211_is_qos_nullfunc(hdr->frame_control))) {
1166 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
1167 ac = ieee802_1d_to_ac[tid & 7];
1168
1169 /*
1170 * If this AC is not trigger-enabled do nothing.
1171 *
1172 * NB: This could/should check a separate bitmap of trigger-
1173 * enabled queues, but for now we only implement uAPSD w/o
1174 * TSPEC changes to the ACs, so they're always the same.
1175 */
1176 if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
1177 return RX_CONTINUE;
1178
1179 /* if we are in a service period, do nothing */
c2c98fde 1180 if (test_sta_flag(rx->sta, WLAN_STA_SP))
47086fc5
JB
1181 return RX_CONTINUE;
1182
c2c98fde 1183 if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
47086fc5
JB
1184 ieee80211_sta_ps_deliver_uapsd(rx->sta);
1185 else
c2c98fde 1186 set_sta_flag(rx->sta, WLAN_STA_UAPSD);
47086fc5
JB
1187 }
1188
1189 return RX_CONTINUE;
1190}
1191
49461622 1192static ieee80211_rx_result debug_noinline
5cf121c3 1193ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
571ecf67
JB
1194{
1195 struct sta_info *sta = rx->sta;
eb9fb5b8
JB
1196 struct sk_buff *skb = rx->skb;
1197 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1198 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
ef0621e8 1199 int i;
571ecf67
JB
1200
1201 if (!sta)
9ae54c84 1202 return RX_CONTINUE;
571ecf67 1203
b291ba11
JB
1204 /*
1205 * Update last_rx only for IBSS packets which are for the current
e584da5e
AQ
1206 * BSSID and for station already AUTHORIZED to avoid keeping the
1207 * current IBSS network alive in cases where other STAs start
1208 * using different BSSID. This will also give the station another
1209 * chance to restart the authentication/authorization in case
1210 * something went wrong the first time.
b291ba11 1211 */
05c914fe 1212 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
71364716 1213 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
05c914fe 1214 NL80211_IFTYPE_ADHOC);
e584da5e
AQ
1215 if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
1216 test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
571ecf67 1217 sta->last_rx = jiffies;
3af6334c
FF
1218 if (ieee80211_is_data(hdr->frame_control)) {
1219 sta->last_rx_rate_idx = status->rate_idx;
1220 sta->last_rx_rate_flag = status->flag;
1b8d242a 1221 sta->last_rx_rate_vht_flag = status->vht_flag;
5614618e 1222 sta->last_rx_rate_vht_nss = status->vht_nss;
3af6334c
FF
1223 }
1224 }
b291ba11
JB
1225 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1226 /*
33b64eb2
LCC
1227 * Mesh beacons will update last_rx when if they are found to
1228 * match the current local configuration when processed.
571ecf67 1229 */
b291ba11 1230 sta->last_rx = jiffies;
3af6334c
FF
1231 if (ieee80211_is_data(hdr->frame_control)) {
1232 sta->last_rx_rate_idx = status->rate_idx;
1233 sta->last_rx_rate_flag = status->flag;
5614618e 1234 sta->last_rx_rate_vht_nss = status->vht_nss;
3af6334c 1235 }
571ecf67
JB
1236 }
1237
554891e6 1238 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
9ae54c84 1239 return RX_CONTINUE;
571ecf67 1240
3cf335d5
KV
1241 if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
1242 ieee80211_sta_rx_notify(rx->sdata, hdr);
1243
571ecf67
JB
1244 sta->rx_fragments++;
1245 sta->rx_bytes += rx->skb->len;
fe8431f8
FF
1246 if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
1247 sta->last_signal = status->signal;
1248 ewma_add(&sta->avg_signal, -status->signal);
1249 }
571ecf67 1250
ef0621e8
FF
1251 if (status->chains) {
1252 sta->chains = status->chains;
1253 for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
1254 int signal = status->chain_signal[i];
1255
1256 if (!(status->chains & BIT(i)))
1257 continue;
1258
1259 sta->chain_signal_last[i] = signal;
1260 ewma_add(&sta->chain_signal_avg[i], -signal);
1261 }
1262 }
1263
72eaa43a
JB
1264 /*
1265 * Change STA power saving mode only at the end of a frame
1266 * exchange sequence.
1267 */
d057e5a3
AN
1268 if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
1269 !ieee80211_has_morefrags(hdr->frame_control) &&
4cfda47b 1270 !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
05c914fe 1271 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
b4ba544c
JB
1272 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1273 /* PM bit is only checked in frames where it isn't reserved,
1274 * in AP mode it's reserved in non-bufferable management frames
1275 * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
1276 */
1277 (!ieee80211_is_mgmt(hdr->frame_control) ||
1278 ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
c2c98fde 1279 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
b4ba544c 1280 if (!ieee80211_has_pm(hdr->frame_control))
d012a605 1281 sta_ps_end(sta);
72eaa43a
JB
1282 } else {
1283 if (ieee80211_has_pm(hdr->frame_control))
d012a605 1284 sta_ps_start(sta);
72eaa43a 1285 }
571ecf67
JB
1286 }
1287
3f52b7e3
MP
1288 /* mesh power save support */
1289 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
1290 ieee80211_mps_rx_h_sta_process(sta, hdr);
1291
22403def
JB
1292 /*
1293 * Drop (qos-)data::nullfunc frames silently, since they
1294 * are used only to control station power saving mode.
1295 */
1296 if (ieee80211_is_nullfunc(hdr->frame_control) ||
1297 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
571ecf67 1298 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
d524215f
FF
1299
1300 /*
1301 * If we receive a 4-addr nullfunc frame from a STA
e7f4a940
JB
1302 * that was not moved to a 4-addr STA vlan yet send
1303 * the event to userspace and for older hostapd drop
1304 * the frame to the monitor interface.
d524215f
FF
1305 */
1306 if (ieee80211_has_a4(hdr->frame_control) &&
1307 (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
1308 (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
e7f4a940
JB
1309 !rx->sdata->u.vlan.sta))) {
1310 if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
1311 cfg80211_rx_unexpected_4addr_frame(
1312 rx->sdata->dev, sta->sta.addr,
1313 GFP_ATOMIC);
d524215f 1314 return RX_DROP_MONITOR;
e7f4a940 1315 }
22403def
JB
1316 /*
1317 * Update counter and free packet here to avoid
1318 * counting this as a dropped packed.
1319 */
571ecf67
JB
1320 sta->rx_packets++;
1321 dev_kfree_skb(rx->skb);
9ae54c84 1322 return RX_QUEUED;
571ecf67
JB
1323 }
1324
9ae54c84 1325 return RX_CONTINUE;
571ecf67
JB
1326} /* ieee80211_rx_h_sta_process */
1327
86c228a7
JA
1328static ieee80211_rx_result debug_noinline
1329ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
1330{
1331 struct sk_buff *skb = rx->skb;
1332 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1333 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1334 int keyidx;
1335 int hdrlen;
1336 ieee80211_rx_result result = RX_DROP_UNUSABLE;
1337 struct ieee80211_key *sta_ptk = NULL;
1338 int mmie_keyidx = -1;
1339 __le16 fc;
2475b1cc 1340 const struct ieee80211_cipher_scheme *cs = NULL;
86c228a7
JA
1341
1342 /*
1343 * Key selection 101
1344 *
1345 * There are four types of keys:
1346 * - GTK (group keys)
1347 * - IGTK (group keys for management frames)
1348 * - PTK (pairwise keys)
1349 * - STK (station-to-station pairwise keys)
1350 *
1351 * When selecting a key, we have to distinguish between multicast
1352 * (including broadcast) and unicast frames, the latter can only
1353 * use PTKs and STKs while the former always use GTKs and IGTKs.
1354 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
1355 * unicast frames can also use key indices like GTKs. Hence, if we
1356 * don't have a PTK/STK we check the key index for a WEP key.
1357 *
1358 * Note that in a regular BSS, multicast frames are sent by the
1359 * AP only, associated stations unicast the frame to the AP first
1360 * which then multicasts it on their behalf.
1361 *
1362 * There is also a slight problem in IBSS mode: GTKs are negotiated
1363 * with each station, that is something we don't currently handle.
1364 * The spec seems to expect that one negotiates the same key with
1365 * every station but there's no such requirement; VLANs could be
1366 * possible.
1367 */
1368
1369 /*
1370 * No point in finding a key and decrypting if the frame is neither
1371 * addressed to us nor a multicast frame.
1372 */
1373 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
1374 return RX_CONTINUE;
1375
1376 /* start without a key */
1377 rx->key = NULL;
2475b1cc 1378 fc = hdr->frame_control;
86c228a7 1379
2475b1cc
MS
1380 if (rx->sta) {
1381 int keyid = rx->sta->ptk_idx;
86c228a7 1382
2475b1cc
MS
1383 if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
1384 cs = rx->sta->cipher_scheme;
1385 keyid = iwl80211_get_cs_keyid(cs, rx->skb);
1386 if (unlikely(keyid < 0))
1387 return RX_DROP_UNUSABLE;
1388 }
1389 sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
1390 }
86c228a7
JA
1391
1392 if (!ieee80211_has_protected(fc))
1393 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
1394
1395 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
1396 rx->key = sta_ptk;
1397 if ((status->flag & RX_FLAG_DECRYPTED) &&
1398 (status->flag & RX_FLAG_IV_STRIPPED))
1399 return RX_CONTINUE;
1400 /* Skip decryption if the frame is not protected. */
1401 if (!ieee80211_has_protected(fc))
1402 return RX_CONTINUE;
1403 } else if (mmie_keyidx >= 0) {
1404 /* Broadcast/multicast robust management frame / BIP */
1405 if ((status->flag & RX_FLAG_DECRYPTED) &&
1406 (status->flag & RX_FLAG_IV_STRIPPED))
1407 return RX_CONTINUE;
1408
1409 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
1410 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
1411 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
1412 if (rx->sta)
1413 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
1414 if (!rx->key)
1415 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
1416 } else if (!ieee80211_has_protected(fc)) {
1417 /*
1418 * The frame was not protected, so skip decryption. However, we
1419 * need to set rx->key if there is a key that could have been
1420 * used so that the frame may be dropped if encryption would
1421 * have been expected.
1422 */
1423 struct ieee80211_key *key = NULL;
1424 struct ieee80211_sub_if_data *sdata = rx->sdata;
1425 int i;
1426
1427 if (ieee80211_is_mgmt(fc) &&
1428 is_multicast_ether_addr(hdr->addr1) &&
1429 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
1430 rx->key = key;
1431 else {
1432 if (rx->sta) {
1433 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1434 key = rcu_dereference(rx->sta->gtk[i]);
1435 if (key)
1436 break;
1437 }
1438 }
1439 if (!key) {
1440 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1441 key = rcu_dereference(sdata->keys[i]);
1442 if (key)
1443 break;
1444 }
1445 }
1446 if (key)
1447 rx->key = key;
1448 }
1449 return RX_CONTINUE;
1450 } else {
1451 u8 keyid;
2475b1cc 1452
86c228a7
JA
1453 /*
1454 * The device doesn't give us the IV so we won't be
1455 * able to look up the key. That's ok though, we
1456 * don't need to decrypt the frame, we just won't
1457 * be able to keep statistics accurate.
1458 * Except for key threshold notifications, should
1459 * we somehow allow the driver to tell us which key
1460 * the hardware used if this flag is set?
1461 */
1462 if ((status->flag & RX_FLAG_DECRYPTED) &&
1463 (status->flag & RX_FLAG_IV_STRIPPED))
1464 return RX_CONTINUE;
1465
1466 hdrlen = ieee80211_hdrlen(fc);
1467
2475b1cc
MS
1468 if (cs) {
1469 keyidx = iwl80211_get_cs_keyid(cs, rx->skb);
86c228a7 1470
2475b1cc
MS
1471 if (unlikely(keyidx < 0))
1472 return RX_DROP_UNUSABLE;
1473 } else {
1474 if (rx->skb->len < 8 + hdrlen)
1475 return RX_DROP_UNUSABLE; /* TODO: count this? */
1476 /*
1477 * no need to call ieee80211_wep_get_keyidx,
1478 * it verifies a bunch of things we've done already
1479 */
1480 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
1481 keyidx = keyid >> 6;
1482 }
86c228a7
JA
1483
1484 /* check per-station GTK first, if multicast packet */
1485 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
1486 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
1487
1488 /* if not found, try default key */
1489 if (!rx->key) {
1490 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1491
1492 /*
1493 * RSNA-protected unicast frames should always be
1494 * sent with pairwise or station-to-station keys,
1495 * but for WEP we allow using a key index as well.
1496 */
1497 if (rx->key &&
1498 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1499 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1500 !is_multicast_ether_addr(hdr->addr1))
1501 rx->key = NULL;
1502 }
1503 }
1504
1505 if (rx->key) {
1506 if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
1507 return RX_DROP_MONITOR;
1508
1509 rx->key->tx_rx_count++;
1510 /* TODO: add threshold stuff again */
1511 } else {
1512 return RX_DROP_MONITOR;
1513 }
1514
1515 switch (rx->key->conf.cipher) {
1516 case WLAN_CIPHER_SUITE_WEP40:
1517 case WLAN_CIPHER_SUITE_WEP104:
1518 result = ieee80211_crypto_wep_decrypt(rx);
1519 break;
1520 case WLAN_CIPHER_SUITE_TKIP:
1521 result = ieee80211_crypto_tkip_decrypt(rx);
1522 break;
1523 case WLAN_CIPHER_SUITE_CCMP:
1524 result = ieee80211_crypto_ccmp_decrypt(rx);
1525 break;
1526 case WLAN_CIPHER_SUITE_AES_CMAC:
1527 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1528 break;
1529 default:
2475b1cc 1530 result = ieee80211_crypto_hw_decrypt(rx);
86c228a7
JA
1531 }
1532
1533 /* the hdr variable is invalid after the decrypt handlers */
1534
1535 /* either the frame has been decrypted or will be dropped */
1536 status->flag |= RX_FLAG_DECRYPTED;
1537
1538 return result;
1539}
1540
571ecf67
JB
1541static inline struct ieee80211_fragment_entry *
1542ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
1543 unsigned int frag, unsigned int seq, int rx_queue,
1544 struct sk_buff **skb)
1545{
1546 struct ieee80211_fragment_entry *entry;
571ecf67 1547
571ecf67
JB
1548 entry = &sdata->fragments[sdata->fragment_next++];
1549 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
1550 sdata->fragment_next = 0;
1551
bdcbd8e0 1552 if (!skb_queue_empty(&entry->skb_list))
571ecf67 1553 __skb_queue_purge(&entry->skb_list);
571ecf67
JB
1554
1555 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
1556 *skb = NULL;
1557 entry->first_frag_time = jiffies;
1558 entry->seq = seq;
1559 entry->rx_queue = rx_queue;
1560 entry->last_frag = frag;
1561 entry->ccmp = 0;
1562 entry->extra_len = 0;
1563
1564 return entry;
1565}
1566
1567static inline struct ieee80211_fragment_entry *
1568ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
b73d70ad 1569 unsigned int frag, unsigned int seq,
571ecf67
JB
1570 int rx_queue, struct ieee80211_hdr *hdr)
1571{
1572 struct ieee80211_fragment_entry *entry;
1573 int i, idx;
1574
1575 idx = sdata->fragment_next;
1576 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
1577 struct ieee80211_hdr *f_hdr;
571ecf67
JB
1578
1579 idx--;
1580 if (idx < 0)
1581 idx = IEEE80211_FRAGMENT_MAX - 1;
1582
1583 entry = &sdata->fragments[idx];
1584 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
1585 entry->rx_queue != rx_queue ||
1586 entry->last_frag + 1 != frag)
1587 continue;
1588
b73d70ad 1589 f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
571ecf67 1590
b73d70ad
HH
1591 /*
1592 * Check ftype and addresses are equal, else check next fragment
1593 */
1594 if (((hdr->frame_control ^ f_hdr->frame_control) &
1595 cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
b203ca39
JP
1596 !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
1597 !ether_addr_equal(hdr->addr2, f_hdr->addr2))
571ecf67
JB
1598 continue;
1599
ab46623e 1600 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
571ecf67
JB
1601 __skb_queue_purge(&entry->skb_list);
1602 continue;
1603 }
1604 return entry;
1605 }
1606
1607 return NULL;
1608}
1609
49461622 1610static ieee80211_rx_result debug_noinline
5cf121c3 1611ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
571ecf67
JB
1612{
1613 struct ieee80211_hdr *hdr;
1614 u16 sc;
358c8d9d 1615 __le16 fc;
571ecf67
JB
1616 unsigned int frag, seq;
1617 struct ieee80211_fragment_entry *entry;
1618 struct sk_buff *skb;
554891e6 1619 struct ieee80211_rx_status *status;
571ecf67 1620
b73d70ad 1621 hdr = (struct ieee80211_hdr *)rx->skb->data;
358c8d9d 1622 fc = hdr->frame_control;
f7fbf70e
JC
1623
1624 if (ieee80211_is_ctl(fc))
1625 return RX_CONTINUE;
1626
571ecf67
JB
1627 sc = le16_to_cpu(hdr->seq_ctrl);
1628 frag = sc & IEEE80211_SCTL_FRAG;
1629
358c8d9d 1630 if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
571ecf67
JB
1631 is_multicast_ether_addr(hdr->addr1))) {
1632 /* not fragmented */
1633 goto out;
1634 }
1635 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
1636
e3cf8b3f
ZY
1637 if (skb_linearize(rx->skb))
1638 return RX_DROP_UNUSABLE;
1639
058897a4
AK
1640 /*
1641 * skb_linearize() might change the skb->data and
1642 * previously cached variables (in this case, hdr) need to
1643 * be refreshed with the new data.
1644 */
1645 hdr = (struct ieee80211_hdr *)rx->skb->data;
571ecf67
JB
1646 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1647
1648 if (frag == 0) {
1649 /* This is the first fragment of a new frame. */
1650 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
9e26297a 1651 rx->seqno_idx, &(rx->skb));
97359d12 1652 if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
358c8d9d 1653 ieee80211_has_protected(fc)) {
9e26297a 1654 int queue = rx->security_idx;
571ecf67
JB
1655 /* Store CCMP PN so that we can verify that the next
1656 * fragment has a sequential PN value. */
1657 entry->ccmp = 1;
1658 memcpy(entry->last_pn,
9190252c 1659 rx->key->u.ccmp.rx_pn[queue],
4325f6ca 1660 IEEE80211_CCMP_PN_LEN);
571ecf67 1661 }
9ae54c84 1662 return RX_QUEUED;
571ecf67
JB
1663 }
1664
1665 /* This is a fragment for a frame that should already be pending in
1666 * fragment cache. Add this fragment to the end of the pending entry.
1667 */
9e26297a
JB
1668 entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
1669 rx->seqno_idx, hdr);
571ecf67
JB
1670 if (!entry) {
1671 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
e4c26add 1672 return RX_DROP_MONITOR;
571ecf67
JB
1673 }
1674
1675 /* Verify that MPDUs within one MSDU have sequential PN values.
1676 * (IEEE 802.11i, 8.3.3.4.5) */
1677 if (entry->ccmp) {
1678 int i;
4325f6ca 1679 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
9190252c 1680 int queue;
97359d12 1681 if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
e4c26add 1682 return RX_DROP_UNUSABLE;
4325f6ca
JB
1683 memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
1684 for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
571ecf67
JB
1685 pn[i]++;
1686 if (pn[i])
1687 break;
1688 }
9e26297a 1689 queue = rx->security_idx;
9190252c 1690 rpn = rx->key->u.ccmp.rx_pn[queue];
4325f6ca 1691 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
e4c26add 1692 return RX_DROP_UNUSABLE;
4325f6ca 1693 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
571ecf67
JB
1694 }
1695
358c8d9d 1696 skb_pull(rx->skb, ieee80211_hdrlen(fc));
571ecf67
JB
1697 __skb_queue_tail(&entry->skb_list, rx->skb);
1698 entry->last_frag = frag;
1699 entry->extra_len += rx->skb->len;
358c8d9d 1700 if (ieee80211_has_morefrags(fc)) {
571ecf67 1701 rx->skb = NULL;
9ae54c84 1702 return RX_QUEUED;
571ecf67
JB
1703 }
1704
1705 rx->skb = __skb_dequeue(&entry->skb_list);
1706 if (skb_tailroom(rx->skb) < entry->extra_len) {
1707 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
1708 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
1709 GFP_ATOMIC))) {
1710 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
1711 __skb_queue_purge(&entry->skb_list);
e4c26add 1712 return RX_DROP_UNUSABLE;
571ecf67
JB
1713 }
1714 }
1715 while ((skb = __skb_dequeue(&entry->skb_list))) {
1716 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
1717 dev_kfree_skb(skb);
1718 }
1719
1720 /* Complete frame has been reassembled - process it now */
554891e6
JB
1721 status = IEEE80211_SKB_RXCB(rx->skb);
1722 status->rx_flags |= IEEE80211_RX_FRAGMENTED;
571ecf67
JB
1723
1724 out:
1725 if (rx->sta)
1726 rx->sta->rx_packets++;
1727 if (is_multicast_ether_addr(hdr->addr1))
1728 rx->local->dot11MulticastReceivedFrameCount++;
1729 else
1730 ieee80211_led_rx(rx->local);
9ae54c84 1731 return RX_CONTINUE;
571ecf67
JB
1732}
1733
1df332e8 1734static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
571ecf67 1735{
1df332e8 1736 if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
76ee65bf 1737 return -EACCES;
571ecf67 1738
76ee65bf 1739 return 0;
571ecf67
JB
1740}
1741
1df332e8 1742static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
571ecf67 1743{
eb9fb5b8
JB
1744 struct sk_buff *skb = rx->skb;
1745 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1746
3017b80b 1747 /*
7848ba7d
JB
1748 * Pass through unencrypted frames if the hardware has
1749 * decrypted them already.
3017b80b 1750 */
eb9fb5b8 1751 if (status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1752 return 0;
571ecf67
JB
1753
1754 /* Drop unencrypted frames if key is set. */
358c8d9d
HH
1755 if (unlikely(!ieee80211_has_protected(fc) &&
1756 !ieee80211_is_nullfunc(fc) &&
f2ca3ea4 1757 ieee80211_is_data(fc) &&
b3fc9c6c 1758 (rx->key || rx->sdata->drop_unencrypted)))
76ee65bf 1759 return -EACCES;
bef5d1c7
JB
1760
1761 return 0;
1762}
1763
1df332e8 1764static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
bef5d1c7
JB
1765{
1766 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
e3efca0a 1767 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
bef5d1c7 1768 __le16 fc = hdr->frame_control;
bef5d1c7 1769
e3efca0a
JM
1770 /*
1771 * Pass through unencrypted frames if the hardware has
1772 * decrypted them already.
1773 */
1774 if (status->flag & RX_FLAG_DECRYPTED)
1775 return 0;
bef5d1c7 1776
c2c98fde 1777 if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
d211e90e
JM
1778 if (unlikely(!ieee80211_has_protected(fc) &&
1779 ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
cf4e594e 1780 rx->key)) {
6ff57cf8
JB
1781 if (ieee80211_is_deauth(fc) ||
1782 ieee80211_is_disassoc(fc))
1783 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1784 rx->skb->data,
1785 rx->skb->len);
f2ca3ea4 1786 return -EACCES;
cf4e594e 1787 }
f2ca3ea4 1788 /* BIP does not use Protected field, so need to check MMIE */
f64f9e71 1789 if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
cf4e594e 1790 ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
6ff57cf8
JB
1791 if (ieee80211_is_deauth(fc) ||
1792 ieee80211_is_disassoc(fc))
1793 cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
1794 rx->skb->data,
1795 rx->skb->len);
f2ca3ea4 1796 return -EACCES;
cf4e594e 1797 }
f2ca3ea4
JM
1798 /*
1799 * When using MFP, Action frames are not allowed prior to
1800 * having configured keys.
1801 */
1802 if (unlikely(ieee80211_is_action(fc) && !rx->key &&
d8ca16db 1803 ieee80211_is_robust_mgmt_frame(rx->skb)))
f2ca3ea4
JM
1804 return -EACCES;
1805 }
b3fc9c6c 1806
76ee65bf 1807 return 0;
571ecf67
JB
1808}
1809
76ee65bf 1810static int
4114fa21 1811__ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
571ecf67 1812{
eb9fb5b8 1813 struct ieee80211_sub_if_data *sdata = rx->sdata;
f14543ee 1814 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
fbb327c5
FF
1815 bool check_port_control = false;
1816 struct ethhdr *ehdr;
1817 int ret;
f14543ee 1818
4114fa21 1819 *port_control = false;
9bc383de
JB
1820 if (ieee80211_has_a4(hdr->frame_control) &&
1821 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
f14543ee 1822 return -1;
9bc383de 1823
fbb327c5
FF
1824 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1825 !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
1826
1827 if (!sdata->u.mgd.use_4addr)
1828 return -1;
1829 else
1830 check_port_control = true;
1831 }
1832
9bc383de 1833 if (is_multicast_ether_addr(hdr->addr1) &&
fbb327c5 1834 sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
f14543ee 1835 return -1;
571ecf67 1836
fbb327c5 1837 ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
4114fa21 1838 if (ret < 0)
fbb327c5
FF
1839 return ret;
1840
1841 ehdr = (struct ethhdr *) rx->skb->data;
4114fa21
FF
1842 if (ehdr->h_proto == rx->sdata->control_port_protocol)
1843 *port_control = true;
1844 else if (check_port_control)
fbb327c5
FF
1845 return -1;
1846
1847 return 0;
76ee65bf 1848}
571ecf67 1849
ce3edf6d
JB
1850/*
1851 * requires that rx->skb is a frame with ethernet header
1852 */
358c8d9d 1853static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
ce3edf6d 1854{
c97c23e3 1855 static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
ce3edf6d
JB
1856 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1857 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1858
1859 /*
1860 * Allow EAPOL frames to us/the PAE group address regardless
1861 * of whether the frame was encrypted or not.
1862 */
a621fa4d 1863 if (ehdr->h_proto == rx->sdata->control_port_protocol &&
3bc7945e
JP
1864 (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
1865 ether_addr_equal(ehdr->h_dest, pae_group_addr)))
ce3edf6d
JB
1866 return true;
1867
1868 if (ieee80211_802_1x_port_control(rx) ||
358c8d9d 1869 ieee80211_drop_unencrypted(rx, fc))
ce3edf6d
JB
1870 return false;
1871
1872 return true;
1873}
1874
1875/*
1876 * requires that rx->skb is a frame with ethernet header
1877 */
76ee65bf 1878static void
5cf121c3 1879ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
76ee65bf 1880{
eb9fb5b8
JB
1881 struct ieee80211_sub_if_data *sdata = rx->sdata;
1882 struct net_device *dev = sdata->dev;
76ee65bf 1883 struct sk_buff *skb, *xmit_skb;
ce3edf6d
JB
1884 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1885 struct sta_info *dsta;
554891e6 1886 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
571ecf67 1887
76ee65bf
RR
1888 skb = rx->skb;
1889 xmit_skb = NULL;
571ecf67 1890
05c914fe
JB
1891 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1892 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
213cd118 1893 !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
554891e6 1894 (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
9bc383de 1895 (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
ce3edf6d
JB
1896 if (is_multicast_ether_addr(ehdr->h_dest)) {
1897 /*
1898 * send multicast frames both to higher layers in
1899 * local net stack and back to the wireless medium
1900 */
76ee65bf 1901 xmit_skb = skb_copy(skb, GFP_ATOMIC);
e87cc472 1902 if (!xmit_skb)
bdcbd8e0 1903 net_info_ratelimited("%s: failed to clone multicast frame\n",
e87cc472 1904 dev->name);
571ecf67 1905 } else {
abe60632
JB
1906 dsta = sta_info_get(sdata, skb->data);
1907 if (dsta) {
ce3edf6d
JB
1908 /*
1909 * The destination station is associated to
1910 * this AP (in this VLAN), so send the frame
1911 * directly to it and do not pass it to local
1912 * net stack.
571ecf67 1913 */
76ee65bf 1914 xmit_skb = skb;
571ecf67
JB
1915 skb = NULL;
1916 }
571ecf67
JB
1917 }
1918 }
1919
59d9cb07 1920#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
9e890a1f
JB
1921 if (skb) {
1922 /* 'align' will only take the values 0 or 2 here since all
1923 * frames are required to be aligned to 2-byte boundaries
1924 * when being passed to mac80211; the code here works just
1925 * as well if that isn't true, but mac80211 assumes it can
1926 * access fields as 2-byte aligned (e.g. for ether_addr_equal)
d1c3a37c 1927 */
9e890a1f
JB
1928 int align;
1929
1930 align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
d1c3a37c
JB
1931 if (align) {
1932 if (WARN_ON(skb_headroom(skb) < 3)) {
1933 dev_kfree_skb(skb);
1934 skb = NULL;
1935 } else {
1936 u8 *data = skb->data;
8ce0b589
ZY
1937 size_t len = skb_headlen(skb);
1938 skb->data -= align;
1939 memmove(skb->data, data, len);
1940 skb_set_tail_pointer(skb, len);
d1c3a37c
JB
1941 }
1942 }
9e890a1f 1943 }
d1c3a37c
JB
1944#endif
1945
9e890a1f
JB
1946 if (skb) {
1947 /* deliver to local stack */
1948 skb->protocol = eth_type_trans(skb, dev);
1949 memset(skb->cb, 0, sizeof(skb->cb));
1950 netif_receive_skb(skb);
571ecf67
JB
1951 }
1952
76ee65bf 1953 if (xmit_skb) {
aef6c928
HS
1954 /*
1955 * Send to wireless media and increase priority by 256 to
1956 * keep the received priority instead of reclassifying
1957 * the frame (see cfg80211_classify8021d).
1958 */
1959 xmit_skb->priority += 256;
f831e909 1960 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
1961 skb_reset_network_header(xmit_skb);
1962 skb_reset_mac_header(xmit_skb);
76ee65bf 1963 dev_queue_xmit(xmit_skb);
571ecf67 1964 }
76ee65bf
RR
1965}
1966
49461622 1967static ieee80211_rx_result debug_noinline
5cf121c3 1968ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
fd4c7f2f 1969{
eb9fb5b8 1970 struct net_device *dev = rx->sdata->dev;
eaf85ca7 1971 struct sk_buff *skb = rx->skb;
358c8d9d
HH
1972 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1973 __le16 fc = hdr->frame_control;
eaf85ca7 1974 struct sk_buff_head frame_list;
554891e6 1975 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
fd4c7f2f 1976
358c8d9d 1977 if (unlikely(!ieee80211_is_data(fc)))
9ae54c84 1978 return RX_CONTINUE;
fd4c7f2f 1979
358c8d9d 1980 if (unlikely(!ieee80211_is_data_present(fc)))
e4c26add 1981 return RX_DROP_MONITOR;
fd4c7f2f 1982
554891e6 1983 if (!(status->rx_flags & IEEE80211_RX_AMSDU))
9ae54c84 1984 return RX_CONTINUE;
fd4c7f2f 1985
eaf85ca7
ZY
1986 if (ieee80211_has_a4(hdr->frame_control) &&
1987 rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1988 !rx->sdata->u.vlan.sta)
e4c26add 1989 return RX_DROP_UNUSABLE;
fd4c7f2f 1990
eaf85ca7
ZY
1991 if (is_multicast_ether_addr(hdr->addr1) &&
1992 ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1993 rx->sdata->u.vlan.sta) ||
1994 (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
1995 rx->sdata->u.mgd.use_4addr)))
e4c26add 1996 return RX_DROP_UNUSABLE;
fd4c7f2f 1997
eaf85ca7
ZY
1998 skb->dev = dev;
1999 __skb_queue_head_init(&frame_list);
fd4c7f2f 2000
e3cf8b3f
ZY
2001 if (skb_linearize(skb))
2002 return RX_DROP_UNUSABLE;
2003
eaf85ca7
ZY
2004 ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
2005 rx->sdata->vif.type,
8b3becad 2006 rx->local->hw.extra_tx_headroom, true);
fd4c7f2f 2007
eaf85ca7
ZY
2008 while (!skb_queue_empty(&frame_list)) {
2009 rx->skb = __skb_dequeue(&frame_list);
fd4c7f2f 2010
358c8d9d 2011 if (!ieee80211_frame_allowed(rx, fc)) {
eaf85ca7 2012 dev_kfree_skb(rx->skb);
ce3edf6d
JB
2013 continue;
2014 }
eaf85ca7
ZY
2015 dev->stats.rx_packets++;
2016 dev->stats.rx_bytes += rx->skb->len;
fd4c7f2f
RR
2017
2018 ieee80211_deliver_skb(rx);
2019 }
2020
9ae54c84 2021 return RX_QUEUED;
fd4c7f2f
RR
2022}
2023
bf94e17b 2024#ifdef CONFIG_MAC80211_MESH
b0dee578 2025static ieee80211_rx_result
e32f85f7
LCC
2026ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
2027{
30789eb6
TP
2028 struct ieee80211_hdr *fwd_hdr, *hdr;
2029 struct ieee80211_tx_info *info;
e32f85f7 2030 struct ieee80211s_hdr *mesh_hdr;
e32f85f7 2031 struct sk_buff *skb = rx->skb, *fwd_skb;
3b8d81e0 2032 struct ieee80211_local *local = rx->local;
eb9fb5b8 2033 struct ieee80211_sub_if_data *sdata = rx->sdata;
554891e6 2034 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
30789eb6 2035 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
30789eb6 2036 u16 q, hdrlen;
e32f85f7
LCC
2037
2038 hdr = (struct ieee80211_hdr *) skb->data;
2039 hdrlen = ieee80211_hdrlen(hdr->frame_control);
9b395bc3
JB
2040
2041 /* make sure fixed part of mesh header is there, also checks skb len */
2042 if (!pskb_may_pull(rx->skb, hdrlen + 6))
2043 return RX_DROP_MONITOR;
2044
2045 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2046
2047 /* make sure full mesh header is there, also checks skb len */
2048 if (!pskb_may_pull(rx->skb,
2049 hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
2050 return RX_DROP_MONITOR;
2051
2052 /* reload pointers */
2053 hdr = (struct ieee80211_hdr *) skb->data;
e32f85f7
LCC
2054 mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
2055
2157fdd6
TP
2056 /* frame is in RMC, don't forward */
2057 if (ieee80211_is_data(hdr->frame_control) &&
2058 is_multicast_ether_addr(hdr->addr1) &&
bf7cd94d 2059 mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
2157fdd6
TP
2060 return RX_DROP_MONITOR;
2061
555cb715
JC
2062 if (!ieee80211_is_data(hdr->frame_control) ||
2063 !(status->rx_flags & IEEE80211_RX_RA_MATCH))
e32f85f7
LCC
2064 return RX_CONTINUE;
2065
2066 if (!mesh_hdr->ttl)
e32f85f7
LCC
2067 return RX_DROP_MONITOR;
2068
43b7b314 2069 if (mesh_hdr->flags & MESH_FLAGS_AE) {
79617dee 2070 struct mesh_path *mppath;
43b7b314
JC
2071 char *proxied_addr;
2072 char *mpp_addr;
2073
2074 if (is_multicast_ether_addr(hdr->addr1)) {
2075 mpp_addr = hdr->addr3;
2076 proxied_addr = mesh_hdr->eaddr1;
9b395bc3
JB
2077 } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
2078 /* has_a4 already checked in ieee80211_rx_mesh_check */
43b7b314
JC
2079 mpp_addr = hdr->addr4;
2080 proxied_addr = mesh_hdr->eaddr2;
9b395bc3
JB
2081 } else {
2082 return RX_DROP_MONITOR;
43b7b314 2083 }
79617dee 2084
79617dee 2085 rcu_read_lock();
bf7cd94d 2086 mppath = mpp_path_lookup(sdata, proxied_addr);
79617dee 2087 if (!mppath) {
bf7cd94d 2088 mpp_path_add(sdata, proxied_addr, mpp_addr);
79617dee
Y
2089 } else {
2090 spin_lock_bh(&mppath->state_lock);
b203ca39 2091 if (!ether_addr_equal(mppath->mpp, mpp_addr))
43b7b314 2092 memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
79617dee
Y
2093 spin_unlock_bh(&mppath->state_lock);
2094 }
2095 rcu_read_unlock();
2096 }
2097
3c5772a5
JC
2098 /* Frame has reached destination. Don't forward */
2099 if (!is_multicast_ether_addr(hdr->addr1) &&
b203ca39 2100 ether_addr_equal(sdata->vif.addr, hdr->addr3))
e32f85f7
LCC
2101 return RX_CONTINUE;
2102
00e96dec 2103 q = ieee80211_select_queue_80211(sdata, skb, hdr);
d3c1597b 2104 if (ieee80211_queue_stopped(&local->hw, q)) {
30789eb6 2105 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
d3c1597b
TP
2106 return RX_DROP_MONITOR;
2107 }
2108 skb_set_queue_mapping(skb, q);
e32f85f7 2109
30789eb6
TP
2110 if (!--mesh_hdr->ttl) {
2111 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
2ac64cd1 2112 goto out;
30789eb6
TP
2113 }
2114
d665508b
CYY
2115 if (!ifmsh->mshcfg.dot11MeshForwarding)
2116 goto out;
2117
30789eb6
TP
2118 fwd_skb = skb_copy(skb, GFP_ATOMIC);
2119 if (!fwd_skb) {
bdcbd8e0 2120 net_info_ratelimited("%s: failed to clone mesh frame\n",
e87cc472 2121 sdata->name);
30789eb6
TP
2122 goto out;
2123 }
2124
2125 fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
9d6d6f49 2126 fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
30789eb6
TP
2127 info = IEEE80211_SKB_CB(fwd_skb);
2128 memset(info, 0, sizeof(*info));
2129 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
2130 info->control.vif = &rx->sdata->vif;
2131 info->control.jiffies = jiffies;
2132 if (is_multicast_ether_addr(fwd_hdr->addr1)) {
2133 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
2134 memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
3f52b7e3
MP
2135 /* update power mode indication when forwarding */
2136 ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
bf7cd94d 2137 } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
3f52b7e3 2138 /* mesh power mode flags updated in mesh_nexthop_lookup */
30789eb6
TP
2139 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
2140 } else {
2141 /* unable to resolve next hop */
bf7cd94d 2142 mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
f63f8421
CYY
2143 fwd_hdr->addr3, 0,
2144 WLAN_REASON_MESH_PATH_NOFORWARD,
2145 fwd_hdr->addr2);
30789eb6 2146 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
74b8cc3d 2147 kfree_skb(fwd_skb);
30789eb6 2148 return RX_DROP_MONITOR;
e32f85f7
LCC
2149 }
2150
30789eb6
TP
2151 IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
2152 ieee80211_add_pending_skb(local, fwd_skb);
b51aff05 2153 out:
3c5772a5 2154 if (is_multicast_ether_addr(hdr->addr1) ||
eb9fb5b8 2155 sdata->dev->flags & IFF_PROMISC)
e32f85f7
LCC
2156 return RX_CONTINUE;
2157 else
2158 return RX_DROP_MONITOR;
2159}
bf94e17b 2160#endif
e32f85f7 2161
49461622 2162static ieee80211_rx_result debug_noinline
5cf121c3 2163ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
76ee65bf 2164{
eb9fb5b8 2165 struct ieee80211_sub_if_data *sdata = rx->sdata;
e15276a4 2166 struct ieee80211_local *local = rx->local;
eb9fb5b8 2167 struct net_device *dev = sdata->dev;
358c8d9d
HH
2168 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
2169 __le16 fc = hdr->frame_control;
4114fa21 2170 bool port_control;
ce3edf6d 2171 int err;
76ee65bf 2172
358c8d9d 2173 if (unlikely(!ieee80211_is_data(hdr->frame_control)))
9ae54c84 2174 return RX_CONTINUE;
76ee65bf 2175
358c8d9d 2176 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
e4c26add 2177 return RX_DROP_MONITOR;
76ee65bf 2178
f14543ee 2179 /*
e7f4a940
JB
2180 * Send unexpected-4addr-frame event to hostapd. For older versions,
2181 * also drop the frame to cooked monitor interfaces.
f14543ee
FF
2182 */
2183 if (ieee80211_has_a4(hdr->frame_control) &&
e7f4a940
JB
2184 sdata->vif.type == NL80211_IFTYPE_AP) {
2185 if (rx->sta &&
2186 !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
2187 cfg80211_rx_unexpected_4addr_frame(
2188 rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
f14543ee 2189 return RX_DROP_MONITOR;
e7f4a940 2190 }
f14543ee 2191
4114fa21 2192 err = __ieee80211_data_to_8023(rx, &port_control);
76ee65bf 2193 if (unlikely(err))
e4c26add 2194 return RX_DROP_UNUSABLE;
76ee65bf 2195
358c8d9d 2196 if (!ieee80211_frame_allowed(rx, fc))
e4c26add 2197 return RX_DROP_MONITOR;
ce3edf6d 2198
4114fa21
FF
2199 if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
2200 unlikely(port_control) && sdata->bss) {
2201 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2202 u.ap);
2203 dev = sdata->dev;
2204 rx->sdata = sdata;
2205 }
2206
76ee65bf
RR
2207 rx->skb->dev = dev;
2208
2209 dev->stats.rx_packets++;
2210 dev->stats.rx_bytes += rx->skb->len;
2211
08ca944e 2212 if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
8c99f691
RM
2213 !is_multicast_ether_addr(
2214 ((struct ethhdr *)rx->skb->data)->h_dest) &&
2215 (!local->scanning &&
2216 !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
e15276a4
VN
2217 mod_timer(&local->dynamic_ps_timer, jiffies +
2218 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
2219 }
2220
76ee65bf 2221 ieee80211_deliver_skb(rx);
571ecf67 2222
9ae54c84 2223 return RX_QUEUED;
571ecf67
JB
2224}
2225
49461622 2226static ieee80211_rx_result debug_noinline
f9e124fb 2227ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
71364716 2228{
71364716 2229 struct sk_buff *skb = rx->skb;
a7767f95 2230 struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
71364716
RR
2231 struct tid_ampdu_rx *tid_agg_rx;
2232 u16 start_seq_num;
2233 u16 tid;
2234
a7767f95 2235 if (likely(!ieee80211_is_ctl(bar->frame_control)))
9ae54c84 2236 return RX_CONTINUE;
71364716 2237
a7767f95 2238 if (ieee80211_is_back_req(bar->frame_control)) {
8ae5977f
JB
2239 struct {
2240 __le16 control, start_seq_num;
2241 } __packed bar_data;
2242
71364716 2243 if (!rx->sta)
a02ae758 2244 return RX_DROP_MONITOR;
8ae5977f
JB
2245
2246 if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
2247 &bar_data, sizeof(bar_data)))
2248 return RX_DROP_MONITOR;
2249
8ae5977f 2250 tid = le16_to_cpu(bar_data.control) >> 12;
a87f736d
JB
2251
2252 tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
2253 if (!tid_agg_rx)
a02ae758 2254 return RX_DROP_MONITOR;
71364716 2255
8ae5977f 2256 start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
71364716
RR
2257
2258 /* reset session timer */
20ad19d0
JB
2259 if (tid_agg_rx->timeout)
2260 mod_timer(&tid_agg_rx->session_timer,
2261 TU_TO_EXP_TIME(tid_agg_rx->timeout));
71364716 2262
dd318575 2263 spin_lock(&tid_agg_rx->reorder_lock);
a02ae758 2264 /* release stored frames up to start of BAR */
d3b2fb53 2265 ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
f9e124fb 2266 start_seq_num, frames);
dd318575
JB
2267 spin_unlock(&tid_agg_rx->reorder_lock);
2268
a02ae758
JB
2269 kfree_skb(skb);
2270 return RX_QUEUED;
71364716
RR
2271 }
2272
08daecae
JB
2273 /*
2274 * After this point, we only want management frames,
2275 * so we can drop all remaining control frames to
2276 * cooked monitor interfaces.
2277 */
2278 return RX_DROP_MONITOR;
71364716
RR
2279}
2280
f4f727a6
JM
2281static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
2282 struct ieee80211_mgmt *mgmt,
2283 size_t len)
fea14732
JM
2284{
2285 struct ieee80211_local *local = sdata->local;
2286 struct sk_buff *skb;
2287 struct ieee80211_mgmt *resp;
2288
b203ca39 2289 if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
fea14732
JM
2290 /* Not to own unicast address */
2291 return;
2292 }
2293
b203ca39
JP
2294 if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
2295 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
77fdaa12 2296 /* Not from the current AP or not associated yet. */
fea14732
JM
2297 return;
2298 }
2299
2300 if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
2301 /* Too short SA Query request frame */
2302 return;
2303 }
2304
2305 skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
2306 if (skb == NULL)
2307 return;
2308
2309 skb_reserve(skb, local->hw.extra_tx_headroom);
2310 resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
2311 memset(resp, 0, 24);
2312 memcpy(resp->da, mgmt->sa, ETH_ALEN);
47846c9b 2313 memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
46900298 2314 memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
fea14732
JM
2315 resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2316 IEEE80211_STYPE_ACTION);
2317 skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
2318 resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
2319 resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
2320 memcpy(resp->u.action.u.sa_query.trans_id,
2321 mgmt->u.action.u.sa_query.trans_id,
2322 WLAN_SA_QUERY_TR_ID_LEN);
2323
62ae67be 2324 ieee80211_tx_skb(sdata, skb);
fea14732
JM
2325}
2326
2e161f78
JB
2327static ieee80211_rx_result debug_noinline
2328ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
2329{
2330 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 2331 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78
JB
2332
2333 /*
2334 * From here on, look only at management frames.
2335 * Data and control frames are already handled,
2336 * and unknown (reserved) frames are useless.
2337 */
2338 if (rx->skb->len < 24)
2339 return RX_DROP_MONITOR;
2340
2341 if (!ieee80211_is_mgmt(mgmt->frame_control))
2342 return RX_DROP_MONITOR;
2343
ee971924
JB
2344 if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
2345 ieee80211_is_beacon(mgmt->frame_control) &&
2346 !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
804483e9
JB
2347 int sig = 0;
2348
2349 if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
2350 sig = status->signal;
2351
ee971924
JB
2352 cfg80211_report_obss_beacon(rx->local->hw.wiphy,
2353 rx->skb->data, rx->skb->len,
37c73b5f 2354 status->freq, sig);
ee971924
JB
2355 rx->flags |= IEEE80211_RX_BEACON_REPORTED;
2356 }
2357
554891e6 2358 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
2e161f78
JB
2359 return RX_DROP_MONITOR;
2360
2361 if (ieee80211_drop_unencrypted_mgmt(rx))
2362 return RX_DROP_UNUSABLE;
2363
2364 return RX_CONTINUE;
2365}
2366
de1ede7a
JB
2367static ieee80211_rx_result debug_noinline
2368ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2369{
2370 struct ieee80211_local *local = rx->local;
eb9fb5b8 2371 struct ieee80211_sub_if_data *sdata = rx->sdata;
de1ede7a 2372 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
554891e6 2373 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
de1ede7a
JB
2374 int len = rx->skb->len;
2375
2376 if (!ieee80211_is_action(mgmt->frame_control))
2377 return RX_CONTINUE;
2378
026331c4
JM
2379 /* drop too small frames */
2380 if (len < IEEE80211_MIN_ACTION_SIZE)
84040805 2381 return RX_DROP_UNUSABLE;
de1ede7a 2382
815b8092 2383 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
cd7760e6
SW
2384 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2385 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
84040805 2386 return RX_DROP_UNUSABLE;
de1ede7a 2387
554891e6 2388 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
84040805 2389 return RX_DROP_UNUSABLE;
97ebe12a 2390
de1ede7a 2391 switch (mgmt->u.action.category) {
1d8d3dec
JB
2392 case WLAN_CATEGORY_HT:
2393 /* reject HT action frames from stations not supporting HT */
2394 if (!rx->sta->sta.ht_cap.ht_supported)
2395 goto invalid;
2396
2397 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2398 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2399 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2400 sdata->vif.type != NL80211_IFTYPE_AP &&
2401 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2402 break;
2403
ec61cd63 2404 /* verify action & smps_control/chanwidth are present */
1d8d3dec
JB
2405 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2406 goto invalid;
2407
2408 switch (mgmt->u.action.u.ht_smps.action) {
2409 case WLAN_HT_ACTION_SMPS: {
2410 struct ieee80211_supported_band *sband;
af0ed69b 2411 enum ieee80211_smps_mode smps_mode;
1d8d3dec
JB
2412
2413 /* convert to HT capability */
2414 switch (mgmt->u.action.u.ht_smps.smps_control) {
2415 case WLAN_HT_SMPS_CONTROL_DISABLED:
af0ed69b 2416 smps_mode = IEEE80211_SMPS_OFF;
1d8d3dec
JB
2417 break;
2418 case WLAN_HT_SMPS_CONTROL_STATIC:
af0ed69b 2419 smps_mode = IEEE80211_SMPS_STATIC;
1d8d3dec
JB
2420 break;
2421 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
af0ed69b 2422 smps_mode = IEEE80211_SMPS_DYNAMIC;
1d8d3dec
JB
2423 break;
2424 default:
2425 goto invalid;
2426 }
1d8d3dec
JB
2427
2428 /* if no change do nothing */
af0ed69b 2429 if (rx->sta->sta.smps_mode == smps_mode)
1d8d3dec 2430 goto handled;
af0ed69b 2431 rx->sta->sta.smps_mode = smps_mode;
1d8d3dec
JB
2432
2433 sband = rx->local->hw.wiphy->bands[status->band];
2434
64f68e5d
JB
2435 rate_control_rate_update(local, sband, rx->sta,
2436 IEEE80211_RC_SMPS_CHANGED);
1d8d3dec
JB
2437 goto handled;
2438 }
ec61cd63
JB
2439 case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
2440 struct ieee80211_supported_band *sband;
2441 u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
e1a0c6b3 2442 enum ieee80211_sta_rx_bandwidth new_bw;
ec61cd63
JB
2443
2444 /* If it doesn't support 40 MHz it can't change ... */
e1a0c6b3
JB
2445 if (!(rx->sta->sta.ht_cap.cap &
2446 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
ec61cd63
JB
2447 goto handled;
2448
e1a0c6b3
JB
2449 if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
2450 new_bw = IEEE80211_STA_RX_BW_20;
2451 else
2452 new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
ec61cd63 2453
e1a0c6b3 2454 if (rx->sta->sta.bandwidth == new_bw)
ec61cd63
JB
2455 goto handled;
2456
ec61cd63
JB
2457 sband = rx->local->hw.wiphy->bands[status->band];
2458
2459 rate_control_rate_update(local, sband, rx->sta,
2460 IEEE80211_RC_BW_CHANGED);
2461 goto handled;
2462 }
1d8d3dec
JB
2463 default:
2464 goto invalid;
2465 }
2466
0af83d3d 2467 break;
1b3a2e49
JB
2468 case WLAN_CATEGORY_PUBLIC:
2469 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2470 goto invalid;
2471 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2472 break;
2473 if (!rx->sta)
2474 break;
2475 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
2476 break;
2477 if (mgmt->u.action.u.ext_chan_switch.action_code !=
2478 WLAN_PUB_ACTION_EXT_CHANSW_ANN)
2479 break;
2480 if (len < offsetof(struct ieee80211_mgmt,
2481 u.action.u.ext_chan_switch.variable))
2482 goto invalid;
2483 goto queue;
0af83d3d
JB
2484 case WLAN_CATEGORY_VHT:
2485 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2486 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
2487 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
2488 sdata->vif.type != NL80211_IFTYPE_AP &&
2489 sdata->vif.type != NL80211_IFTYPE_ADHOC)
2490 break;
2491
2492 /* verify action code is present */
2493 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2494 goto invalid;
2495
2496 switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
2497 case WLAN_VHT_ACTION_OPMODE_NOTIF: {
2498 u8 opmode;
2499
2500 /* verify opmode is present */
2501 if (len < IEEE80211_MIN_ACTION_SIZE + 2)
2502 goto invalid;
2503
2504 opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
2505
2506 ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
bee7f586
JB
2507 opmode, status->band,
2508 false);
0af83d3d
JB
2509 goto handled;
2510 }
2511 default:
2512 break;
2513 }
1d8d3dec 2514 break;
de1ede7a 2515 case WLAN_CATEGORY_BACK:
8abd3f9b 2516 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
ae2772b3 2517 sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
8abd3f9b 2518 sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
13c40c54
AS
2519 sdata->vif.type != NL80211_IFTYPE_AP &&
2520 sdata->vif.type != NL80211_IFTYPE_ADHOC)
84040805 2521 break;
8abd3f9b 2522
026331c4
JM
2523 /* verify action_code is present */
2524 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2525 break;
2526
de1ede7a
JB
2527 switch (mgmt->u.action.u.addba_req.action_code) {
2528 case WLAN_ACTION_ADDBA_REQ:
2529 if (len < (IEEE80211_MIN_ACTION_SIZE +
2530 sizeof(mgmt->u.action.u.addba_req)))
bed7ee6e
JB
2531 goto invalid;
2532 break;
de1ede7a
JB
2533 case WLAN_ACTION_ADDBA_RESP:
2534 if (len < (IEEE80211_MIN_ACTION_SIZE +
2535 sizeof(mgmt->u.action.u.addba_resp)))
bed7ee6e
JB
2536 goto invalid;
2537 break;
de1ede7a
JB
2538 case WLAN_ACTION_DELBA:
2539 if (len < (IEEE80211_MIN_ACTION_SIZE +
2540 sizeof(mgmt->u.action.u.delba)))
bed7ee6e
JB
2541 goto invalid;
2542 break;
2543 default:
2544 goto invalid;
de1ede7a 2545 }
bed7ee6e 2546
8b58ff83 2547 goto queue;
39192c0b 2548 case WLAN_CATEGORY_SPECTRUM_MGMT:
026331c4
JM
2549 /* verify action_code is present */
2550 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2551 break;
2552
39192c0b
JB
2553 switch (mgmt->u.action.u.measurement.action_code) {
2554 case WLAN_ACTION_SPCT_MSR_REQ:
cd7760e6
SW
2555 if (status->band != IEEE80211_BAND_5GHZ)
2556 break;
2557
39192c0b
JB
2558 if (len < (IEEE80211_MIN_ACTION_SIZE +
2559 sizeof(mgmt->u.action.u.measurement)))
84040805 2560 break;
cd7760e6
SW
2561
2562 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2563 break;
2564
39192c0b 2565 ieee80211_process_measurement_req(sdata, mgmt, len);
84040805 2566 goto handled;
cd7760e6
SW
2567 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2568 u8 *bssid;
2569 if (len < (IEEE80211_MIN_ACTION_SIZE +
2570 sizeof(mgmt->u.action.u.chan_switch)))
84040805 2571 break;
cc32abd4 2572
cd7760e6 2573 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
b8456a14
CYY
2574 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2575 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
cd7760e6
SW
2576 break;
2577
2578 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2579 bssid = sdata->u.mgd.bssid;
2580 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2581 bssid = sdata->u.ibss.bssid;
b8456a14
CYY
2582 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2583 bssid = mgmt->sa;
cd7760e6
SW
2584 else
2585 break;
2586
2587 if (!ether_addr_equal(mgmt->bssid, bssid))
84040805 2588 break;
c481ec97 2589
8b58ff83 2590 goto queue;
cd7760e6 2591 }
39192c0b
JB
2592 }
2593 break;
fea14732
JM
2594 case WLAN_CATEGORY_SA_QUERY:
2595 if (len < (IEEE80211_MIN_ACTION_SIZE +
2596 sizeof(mgmt->u.action.u.sa_query)))
84040805
JB
2597 break;
2598
fea14732
JM
2599 switch (mgmt->u.action.u.sa_query.action) {
2600 case WLAN_ACTION_SA_QUERY_REQUEST:
2601 if (sdata->vif.type != NL80211_IFTYPE_STATION)
84040805 2602 break;
fea14732 2603 ieee80211_process_sa_query_req(sdata, mgmt, len);
84040805 2604 goto handled;
fea14732
JM
2605 }
2606 break;
8db09850 2607 case WLAN_CATEGORY_SELF_PROTECTED:
9b395bc3
JB
2608 if (len < (IEEE80211_MIN_ACTION_SIZE +
2609 sizeof(mgmt->u.action.u.self_prot.action_code)))
2610 break;
2611
8db09850
TP
2612 switch (mgmt->u.action.u.self_prot.action_code) {
2613 case WLAN_SP_MESH_PEERING_OPEN:
2614 case WLAN_SP_MESH_PEERING_CLOSE:
2615 case WLAN_SP_MESH_PEERING_CONFIRM:
2616 if (!ieee80211_vif_is_mesh(&sdata->vif))
2617 goto invalid;
a6dad6a2 2618 if (sdata->u.mesh.user_mpm)
8db09850
TP
2619 /* userspace handles this frame */
2620 break;
2621 goto queue;
2622 case WLAN_SP_MGK_INFORM:
2623 case WLAN_SP_MGK_ACK:
2624 if (!ieee80211_vif_is_mesh(&sdata->vif))
2625 goto invalid;
2626 break;
2627 }
2628 break;
d3aaec8a 2629 case WLAN_CATEGORY_MESH_ACTION:
9b395bc3
JB
2630 if (len < (IEEE80211_MIN_ACTION_SIZE +
2631 sizeof(mgmt->u.action.u.mesh_action.action_code)))
2632 break;
2633
77a121c3
JB
2634 if (!ieee80211_vif_is_mesh(&sdata->vif))
2635 break;
25d49e4d 2636 if (mesh_action_is_path_sel(mgmt) &&
1df332e8 2637 !mesh_path_sel_is_hwmp(sdata))
c7108a71
JC
2638 break;
2639 goto queue;
84040805 2640 }
026331c4 2641
2e161f78
JB
2642 return RX_CONTINUE;
2643
bed7ee6e 2644 invalid:
554891e6 2645 status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
2e161f78
JB
2646 /* will return in the next handlers */
2647 return RX_CONTINUE;
2648
2649 handled:
2650 if (rx->sta)
2651 rx->sta->rx_packets++;
2652 dev_kfree_skb(rx->skb);
2653 return RX_QUEUED;
2654
2655 queue:
2656 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
2657 skb_queue_tail(&sdata->skb_queue, rx->skb);
2658 ieee80211_queue_work(&local->hw, &sdata->work);
2659 if (rx->sta)
2660 rx->sta->rx_packets++;
2661 return RX_QUEUED;
2662}
2663
2664static ieee80211_rx_result debug_noinline
2665ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
2666{
554891e6 2667 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
804483e9 2668 int sig = 0;
2e161f78
JB
2669
2670 /* skip known-bad action frames and return them in the next handler */
554891e6 2671 if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
2e161f78 2672 return RX_CONTINUE;
d7907448 2673
026331c4
JM
2674 /*
2675 * Getting here means the kernel doesn't know how to handle
2676 * it, but maybe userspace does ... include returned frames
2677 * so userspace can register for those to know whether ones
2678 * it transmitted were processed or returned.
2679 */
026331c4 2680
804483e9
JB
2681 if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
2682 sig = status->signal;
2683
71bbc994 2684 if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
19504cf5 2685 rx->skb->data, rx->skb->len, 0, GFP_ATOMIC)) {
2e161f78
JB
2686 if (rx->sta)
2687 rx->sta->rx_packets++;
2688 dev_kfree_skb(rx->skb);
2689 return RX_QUEUED;
2690 }
2691
2e161f78
JB
2692 return RX_CONTINUE;
2693}
2694
2695static ieee80211_rx_result debug_noinline
2696ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
2697{
2698 struct ieee80211_local *local = rx->local;
2699 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
2700 struct sk_buff *nskb;
2701 struct ieee80211_sub_if_data *sdata = rx->sdata;
554891e6 2702 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2e161f78
JB
2703
2704 if (!ieee80211_is_action(mgmt->frame_control))
2705 return RX_CONTINUE;
2706
2707 /*
2708 * For AP mode, hostapd is responsible for handling any action
2709 * frames that we didn't handle, including returning unknown
2710 * ones. For all other modes we will return them to the sender,
2711 * setting the 0x80 bit in the action category, as required by
4b5ebccc 2712 * 802.11-2012 9.24.4.
2e161f78
JB
2713 * Newer versions of hostapd shall also use the management frame
2714 * registration mechanisms, but older ones still use cooked
2715 * monitor interfaces so push all frames there.
2716 */
554891e6 2717 if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
2e161f78
JB
2718 (sdata->vif.type == NL80211_IFTYPE_AP ||
2719 sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
2720 return RX_DROP_MONITOR;
026331c4 2721
4b5ebccc
JB
2722 if (is_multicast_ether_addr(mgmt->da))
2723 return RX_DROP_MONITOR;
2724
84040805
JB
2725 /* do not return rejected action frames */
2726 if (mgmt->u.action.category & 0x80)
2727 return RX_DROP_UNUSABLE;
2728
2729 nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
2730 GFP_ATOMIC);
2731 if (nskb) {
292b4df6 2732 struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
84040805 2733
292b4df6
JL
2734 nmgmt->u.action.category |= 0x80;
2735 memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
2736 memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
84040805
JB
2737
2738 memset(nskb->cb, 0, sizeof(nskb->cb));
2739
07e5a5f5
JB
2740 if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
2741 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
2742
2743 info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
2744 IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
2745 IEEE80211_TX_CTL_NO_CCK_RATE;
2746 if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
2747 info->hw_queue =
2748 local->hw.offchannel_tx_hw_queue;
2749 }
2750
2751 __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
2752 status->band);
de1ede7a 2753 }
39192c0b
JB
2754 dev_kfree_skb(rx->skb);
2755 return RX_QUEUED;
de1ede7a
JB
2756}
2757
49461622 2758static ieee80211_rx_result debug_noinline
5cf121c3 2759ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
571ecf67 2760{
eb9fb5b8 2761 struct ieee80211_sub_if_data *sdata = rx->sdata;
77a121c3
JB
2762 struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
2763 __le16 stype;
571ecf67 2764
77a121c3 2765 stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
472dbc45 2766
77a121c3
JB
2767 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
2768 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2769 sdata->vif.type != NL80211_IFTYPE_STATION)
2770 return RX_DROP_MONITOR;
472dbc45 2771
77a121c3 2772 switch (stype) {
66e67e41 2773 case cpu_to_le16(IEEE80211_STYPE_AUTH):
77a121c3
JB
2774 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2775 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2776 /* process for all: mesh, mlme, ibss */
2777 break;
66e67e41
JB
2778 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
2779 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
77a121c3
JB
2780 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2781 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2c31333a
CL
2782 if (is_multicast_ether_addr(mgmt->da) &&
2783 !is_broadcast_ether_addr(mgmt->da))
2784 return RX_DROP_MONITOR;
2785
77a121c3
JB
2786 /* process only for station */
2787 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2788 return RX_DROP_MONITOR;
2789 break;
2790 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
9fb04b50
TP
2791 /* process only for ibss and mesh */
2792 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2793 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
77a121c3
JB
2794 return RX_DROP_MONITOR;
2795 break;
2796 default:
2797 return RX_DROP_MONITOR;
2798 }
f9d540ee 2799
77a121c3 2800 /* queue up frame and kick off work to process it */
c1475ca9 2801 rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
77a121c3
JB
2802 skb_queue_tail(&sdata->skb_queue, rx->skb);
2803 ieee80211_queue_work(&rx->local->hw, &sdata->work);
8b58ff83
JB
2804 if (rx->sta)
2805 rx->sta->rx_packets++;
46900298 2806
77a121c3 2807 return RX_QUEUED;
571ecf67
JB
2808}
2809
5cf121c3 2810/* TODO: use IEEE80211_RX_FRAGMENTED */
5f0b7de5
JB
2811static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
2812 struct ieee80211_rate *rate)
3d30d949
MW
2813{
2814 struct ieee80211_sub_if_data *sdata;
2815 struct ieee80211_local *local = rx->local;
3d30d949
MW
2816 struct sk_buff *skb = rx->skb, *skb2;
2817 struct net_device *prev_dev = NULL;
eb9fb5b8 2818 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
293702a3 2819 int needed_headroom;
3d30d949 2820
554891e6
JB
2821 /*
2822 * If cooked monitor has been processed already, then
2823 * don't do it again. If not, set the flag.
2824 */
2825 if (rx->flags & IEEE80211_RX_CMNTR)
7c1e1831 2826 goto out_free_skb;
554891e6 2827 rx->flags |= IEEE80211_RX_CMNTR;
7c1e1831 2828
152c477a
JB
2829 /* If there are no cooked monitor interfaces, just free the SKB */
2830 if (!local->cooked_mntrs)
2831 goto out_free_skb;
2832
293702a3 2833 /* room for the radiotap header based on driver features */
90b9e446 2834 needed_headroom = ieee80211_rx_radiotap_space(local, status);
3d30d949 2835
293702a3
JB
2836 if (skb_headroom(skb) < needed_headroom &&
2837 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
2838 goto out_free_skb;
3d30d949 2839
293702a3 2840 /* prepend radiotap information */
973ef21a
FF
2841 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
2842 false);
3d30d949
MW
2843
2844 skb_set_mac_header(skb, 0);
2845 skb->ip_summed = CHECKSUM_UNNECESSARY;
2846 skb->pkt_type = PACKET_OTHERHOST;
2847 skb->protocol = htons(ETH_P_802_2);
2848
2849 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
9607e6b6 2850 if (!ieee80211_sdata_running(sdata))
3d30d949
MW
2851 continue;
2852
05c914fe 2853 if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
3d30d949
MW
2854 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
2855 continue;
2856
2857 if (prev_dev) {
2858 skb2 = skb_clone(skb, GFP_ATOMIC);
2859 if (skb2) {
2860 skb2->dev = prev_dev;
5548a8a1 2861 netif_receive_skb(skb2);
3d30d949
MW
2862 }
2863 }
2864
2865 prev_dev = sdata->dev;
2866 sdata->dev->stats.rx_packets++;
2867 sdata->dev->stats.rx_bytes += skb->len;
2868 }
2869
2870 if (prev_dev) {
2871 skb->dev = prev_dev;
5548a8a1 2872 netif_receive_skb(skb);
554891e6
JB
2873 return;
2874 }
3d30d949
MW
2875
2876 out_free_skb:
2877 dev_kfree_skb(skb);
2878}
2879
aa0c8636
CL
2880static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
2881 ieee80211_rx_result res)
2882{
2883 switch (res) {
2884 case RX_DROP_MONITOR:
2885 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2886 if (rx->sta)
2887 rx->sta->rx_dropped++;
2888 /* fall through */
2889 case RX_CONTINUE: {
2890 struct ieee80211_rate *rate = NULL;
2891 struct ieee80211_supported_band *sband;
2892 struct ieee80211_rx_status *status;
2893
2894 status = IEEE80211_SKB_RXCB((rx->skb));
2895
2896 sband = rx->local->hw.wiphy->bands[status->band];
5614618e
JB
2897 if (!(status->flag & RX_FLAG_HT) &&
2898 !(status->flag & RX_FLAG_VHT))
aa0c8636
CL
2899 rate = &sband->bitrates[status->rate_idx];
2900
2901 ieee80211_rx_cooked_monitor(rx, rate);
2902 break;
2903 }
2904 case RX_DROP_UNUSABLE:
2905 I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
2906 if (rx->sta)
2907 rx->sta->rx_dropped++;
2908 dev_kfree_skb(rx->skb);
2909 break;
2910 case RX_QUEUED:
2911 I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
2912 break;
2913 }
2914}
571ecf67 2915
f9e124fb
CL
2916static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
2917 struct sk_buff_head *frames)
58905290 2918{
58905290 2919 ieee80211_rx_result res = RX_DROP_MONITOR;
aa0c8636 2920 struct sk_buff *skb;
8944b79f 2921
e32f85f7
LCC
2922#define CALL_RXH(rxh) \
2923 do { \
2924 res = rxh(rx); \
2925 if (res != RX_CONTINUE) \
2569a826 2926 goto rxh_next; \
e32f85f7 2927 } while (0);
49461622 2928
f9e124fb 2929 spin_lock_bh(&rx->local->rx_path_lock);
24a8fdad 2930
f9e124fb 2931 while ((skb = __skb_dequeue(frames))) {
2569a826
JB
2932 /*
2933 * all the other fields are valid across frames
2934 * that belong to an aMPDU since they are on the
2935 * same TID from the same station
2936 */
2937 rx->skb = skb;
2938
2569a826 2939 CALL_RXH(ieee80211_rx_h_check_more_data)
47086fc5 2940 CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
2569a826 2941 CALL_RXH(ieee80211_rx_h_sta_process)
86c228a7 2942 CALL_RXH(ieee80211_rx_h_decrypt)
2569a826 2943 CALL_RXH(ieee80211_rx_h_defragment)
2569a826
JB
2944 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2945 /* must be after MMIC verify so header is counted in MPDU mic */
bf94e17b 2946#ifdef CONFIG_MAC80211_MESH
aa0c8636 2947 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2569a826 2948 CALL_RXH(ieee80211_rx_h_mesh_fwding);
bf94e17b 2949#endif
2154c81c 2950 CALL_RXH(ieee80211_rx_h_amsdu)
2569a826 2951 CALL_RXH(ieee80211_rx_h_data)
f9e124fb
CL
2952
2953 /* special treatment -- needs the queue */
2954 res = ieee80211_rx_h_ctrl(rx, frames);
2955 if (res != RX_CONTINUE)
2956 goto rxh_next;
2957
2e161f78 2958 CALL_RXH(ieee80211_rx_h_mgmt_check)
2569a826 2959 CALL_RXH(ieee80211_rx_h_action)
2e161f78
JB
2960 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
2961 CALL_RXH(ieee80211_rx_h_action_return)
2569a826 2962 CALL_RXH(ieee80211_rx_h_mgmt)
49461622 2963
aa0c8636
CL
2964 rxh_next:
2965 ieee80211_rx_handlers_result(rx, res);
f9e124fb 2966
49461622 2967#undef CALL_RXH
aa0c8636 2968 }
24a8fdad 2969
f9e124fb 2970 spin_unlock_bh(&rx->local->rx_path_lock);
aa0c8636
CL
2971}
2972
4406c376 2973static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
aa0c8636 2974{
f9e124fb 2975 struct sk_buff_head reorder_release;
aa0c8636
CL
2976 ieee80211_rx_result res = RX_DROP_MONITOR;
2977
f9e124fb
CL
2978 __skb_queue_head_init(&reorder_release);
2979
aa0c8636
CL
2980#define CALL_RXH(rxh) \
2981 do { \
2982 res = rxh(rx); \
2983 if (res != RX_CONTINUE) \
2984 goto rxh_next; \
2985 } while (0);
2986
aa0c8636
CL
2987 CALL_RXH(ieee80211_rx_h_check)
2988
f9e124fb 2989 ieee80211_rx_reorder_ampdu(rx, &reorder_release);
aa0c8636 2990
f9e124fb 2991 ieee80211_rx_handlers(rx, &reorder_release);
aa0c8636 2992 return;
49461622 2993
2569a826 2994 rxh_next:
aa0c8636
CL
2995 ieee80211_rx_handlers_result(rx, res);
2996
2997#undef CALL_RXH
58905290
JB
2998}
2999
2bff8ebf 3000/*
dd318575
JB
3001 * This function makes calls into the RX path, therefore
3002 * it has to be invoked under RCU read lock.
2bff8ebf
CL
3003 */
3004void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
3005{
f9e124fb 3006 struct sk_buff_head frames;
554891e6
JB
3007 struct ieee80211_rx_data rx = {
3008 .sta = sta,
3009 .sdata = sta->sdata,
3010 .local = sta->local,
9e26297a
JB
3011 /* This is OK -- must be QoS data frame */
3012 .security_idx = tid,
3013 .seqno_idx = tid,
fcf8bd3b 3014 .flags = 0,
554891e6 3015 };
2c15a0cf
CL
3016 struct tid_ampdu_rx *tid_agg_rx;
3017
3018 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
3019 if (!tid_agg_rx)
3020 return;
2bff8ebf 3021
f9e124fb
CL
3022 __skb_queue_head_init(&frames);
3023
2c15a0cf 3024 spin_lock(&tid_agg_rx->reorder_lock);
f9e124fb 3025 ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
2c15a0cf 3026 spin_unlock(&tid_agg_rx->reorder_lock);
2bff8ebf 3027
f9e124fb 3028 ieee80211_rx_handlers(&rx, &frames);
2bff8ebf
CL
3029}
3030
571ecf67
JB
3031/* main receive path */
3032
3c2723f5
JB
3033static bool prepare_for_handlers(struct ieee80211_rx_data *rx,
3034 struct ieee80211_hdr *hdr)
23a24def 3035{
20b01f80 3036 struct ieee80211_sub_if_data *sdata = rx->sdata;
eb9fb5b8
JB
3037 struct sk_buff *skb = rx->skb;
3038 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3039 u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
23a24def
JB
3040 int multicast = is_multicast_ether_addr(hdr->addr1);
3041
51fb61e7 3042 switch (sdata->vif.type) {
05c914fe 3043 case NL80211_IFTYPE_STATION:
9bc383de 3044 if (!bssid && !sdata->u.mgd.use_4addr)
3c2723f5 3045 return false;
77fdaa12 3046 if (!multicast &&
b203ca39 3047 !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
4f312336
FF
3048 if (!(sdata->dev->flags & IFF_PROMISC) ||
3049 sdata->u.mgd.use_4addr)
3c2723f5 3050 return false;
554891e6 3051 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
23a24def
JB
3052 }
3053 break;
05c914fe 3054 case NL80211_IFTYPE_ADHOC:
23a24def 3055 if (!bssid)
3c2723f5 3056 return false;
6329b8d9
FF
3057 if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
3058 ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
3c2723f5 3059 return false;
a7767f95 3060 if (ieee80211_is_beacon(hdr->frame_control)) {
3c2723f5 3061 return true;
d48b2968 3062 } else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
3c2723f5 3063 return false;
23a24def 3064 } else if (!multicast &&
b203ca39 3065 !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
4150c572 3066 if (!(sdata->dev->flags & IFF_PROMISC))
3c2723f5 3067 return false;
554891e6 3068 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
0fb8ca45
JM
3069 } else if (!rx->sta) {
3070 int rate_idx;
5614618e
JB
3071 if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
3072 rate_idx = 0; /* TODO: HT/VHT rates */
0fb8ca45 3073 else
eb9fb5b8 3074 rate_idx = status->rate_idx;
8bf11d8d
JB
3075 ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
3076 BIT(rate_idx));
0fb8ca45 3077 }
23a24def 3078 break;
05c914fe 3079 case NL80211_IFTYPE_MESH_POINT:
6032f934 3080 if (!multicast &&
b203ca39 3081 !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
6032f934 3082 if (!(sdata->dev->flags & IFF_PROMISC))
3c2723f5 3083 return false;
6032f934 3084
554891e6 3085 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
6032f934
JB
3086 }
3087 break;
05c914fe
JB
3088 case NL80211_IFTYPE_AP_VLAN:
3089 case NL80211_IFTYPE_AP:
23a24def 3090 if (!bssid) {
b203ca39 3091 if (!ether_addr_equal(sdata->vif.addr, hdr->addr1))
3c2723f5 3092 return false;
f142c6b9 3093 } else if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
3df6eaea
JB
3094 /*
3095 * Accept public action frames even when the
3096 * BSSID doesn't match, this is used for P2P
3097 * and location updates. Note that mac80211
3098 * itself never looks at these frames.
3099 */
2b9ccd4e
JB
3100 if (!multicast &&
3101 !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3c2723f5 3102 return false;
d48b2968 3103 if (ieee80211_is_public_action(hdr, skb->len))
3c2723f5 3104 return true;
d48b2968 3105 if (!ieee80211_is_beacon(hdr->frame_control))
3c2723f5 3106 return false;
554891e6 3107 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
23a24def 3108 }
23a24def 3109 break;
05c914fe 3110 case NL80211_IFTYPE_WDS:
a7767f95 3111 if (bssid || !ieee80211_is_data(hdr->frame_control))
3c2723f5 3112 return false;
b203ca39 3113 if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
3c2723f5 3114 return false;
23a24def 3115 break;
f142c6b9
JB
3116 case NL80211_IFTYPE_P2P_DEVICE:
3117 if (!ieee80211_is_public_action(hdr, skb->len) &&
3118 !ieee80211_is_probe_req(hdr->frame_control) &&
3119 !ieee80211_is_probe_resp(hdr->frame_control) &&
3120 !ieee80211_is_beacon(hdr->frame_control))
3c2723f5 3121 return false;
1b737f88
JB
3122 if (!ether_addr_equal(sdata->vif.addr, hdr->addr1) &&
3123 !multicast)
f142c6b9
JB
3124 status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
3125 break;
2ca27bcf 3126 default:
fb1c1cd6 3127 /* should never get here */
f142c6b9 3128 WARN_ON_ONCE(1);
fb1c1cd6 3129 break;
23a24def
JB
3130 }
3131
3c2723f5 3132 return true;
23a24def
JB
3133}
3134
4406c376
JB
3135/*
3136 * This function returns whether or not the SKB
3137 * was destined for RX processing or not, which,
3138 * if consume is true, is equivalent to whether
3139 * or not the skb was consumed.
3140 */
3141static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
3142 struct sk_buff *skb, bool consume)
3143{
3144 struct ieee80211_local *local = rx->local;
3145 struct ieee80211_sub_if_data *sdata = rx->sdata;
3146 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3147 struct ieee80211_hdr *hdr = (void *)skb->data;
4406c376
JB
3148
3149 rx->skb = skb;
554891e6 3150 status->rx_flags |= IEEE80211_RX_RA_MATCH;
4406c376 3151
40791942 3152 if (!prepare_for_handlers(rx, hdr))
4406c376
JB
3153 return false;
3154
4406c376
JB
3155 if (!consume) {
3156 skb = skb_copy(skb, GFP_ATOMIC);
3157 if (!skb) {
3158 if (net_ratelimit())
3159 wiphy_debug(local->hw.wiphy,
b305dae4 3160 "failed to copy skb for %s\n",
4406c376
JB
3161 sdata->name);
3162 return true;
3163 }
3164
3165 rx->skb = skb;
3166 }
3167
3168 ieee80211_invoke_rx_handlers(rx);
3169 return true;
3170}
3171
571ecf67 3172/*
6368e4b1
RR
3173 * This is the actual Rx frames handler. as it blongs to Rx path it must
3174 * be called with rcu_read_lock protection.
571ecf67 3175 */
71ebb4aa 3176static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
071d9ac2 3177 struct sk_buff *skb)
571ecf67
JB
3178{
3179 struct ieee80211_local *local = hw_to_local(hw);
3180 struct ieee80211_sub_if_data *sdata;
571ecf67 3181 struct ieee80211_hdr *hdr;
e3cf8b3f 3182 __le16 fc;
5cf121c3 3183 struct ieee80211_rx_data rx;
4406c376 3184 struct ieee80211_sub_if_data *prev;
56af3268 3185 struct sta_info *sta, *tmp, *prev_sta;
e3cf8b3f 3186 int err = 0;
571ecf67 3187
e3cf8b3f 3188 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
571ecf67
JB
3189 memset(&rx, 0, sizeof(rx));
3190 rx.skb = skb;
3191 rx.local = local;
72abd81b 3192
e3cf8b3f 3193 if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
571ecf67 3194 local->dot11ReceivedFragmentCount++;
571ecf67 3195
4a4f1a58
JB
3196 if (ieee80211_is_mgmt(fc)) {
3197 /* drop frame if too short for header */
3198 if (skb->len < ieee80211_hdrlen(fc))
3199 err = -ENOBUFS;
3200 else
3201 err = skb_linearize(skb);
3202 } else {
e3cf8b3f 3203 err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
4a4f1a58 3204 }
e3cf8b3f
ZY
3205
3206 if (err) {
3207 dev_kfree_skb(skb);
3208 return;
3209 }
3210
3211 hdr = (struct ieee80211_hdr *)skb->data;
38f3714d 3212 ieee80211_parse_qos(&rx);
d1c3a37c 3213 ieee80211_verify_alignment(&rx);
38f3714d 3214
d48b2968
JB
3215 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
3216 ieee80211_is_beacon(hdr->frame_control)))
3217 ieee80211_scan_rx(local, skb);
3218
e3cf8b3f 3219 if (ieee80211_is_data(fc)) {
56af3268 3220 prev_sta = NULL;
4406c376 3221
7852e361 3222 for_each_sta_info(local, hdr->addr2, sta, tmp) {
56af3268
BG
3223 if (!prev_sta) {
3224 prev_sta = sta;
3225 continue;
3226 }
3227
3228 rx.sta = prev_sta;
3229 rx.sdata = prev_sta->sdata;
4406c376 3230 ieee80211_prepare_and_rx_handle(&rx, skb, false);
abe60632 3231
56af3268 3232 prev_sta = sta;
4406c376 3233 }
56af3268
BG
3234
3235 if (prev_sta) {
3236 rx.sta = prev_sta;
3237 rx.sdata = prev_sta->sdata;
3238
4406c376 3239 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
56af3268 3240 return;
8e26d5ad 3241 goto out;
56af3268 3242 }
4406c376
JB
3243 }
3244
4b0dd98e 3245 prev = NULL;
b2e7771e 3246
4b0dd98e
JB
3247 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3248 if (!ieee80211_sdata_running(sdata))
3249 continue;
340e11f3 3250
4b0dd98e
JB
3251 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
3252 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3253 continue;
340e11f3 3254
4b0dd98e
JB
3255 /*
3256 * frame is destined for this interface, but if it's
3257 * not also for the previous one we handle that after
3258 * the loop to avoid copying the SKB once too much
3259 */
4bb29f8c 3260
4b0dd98e 3261 if (!prev) {
abe60632 3262 prev = sdata;
4b0dd98e 3263 continue;
340e11f3 3264 }
4bb29f8c 3265
7852e361 3266 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4b0dd98e
JB
3267 rx.sdata = prev;
3268 ieee80211_prepare_and_rx_handle(&rx, skb, false);
4bb29f8c 3269
4b0dd98e
JB
3270 prev = sdata;
3271 }
3272
3273 if (prev) {
7852e361 3274 rx.sta = sta_info_get_bss(prev, hdr->addr2);
4b0dd98e 3275 rx.sdata = prev;
4406c376 3276
4b0dd98e
JB
3277 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
3278 return;
571ecf67 3279 }
4406c376 3280
8e26d5ad 3281 out:
4406c376 3282 dev_kfree_skb(skb);
571ecf67 3283}
6368e4b1
RR
3284
3285/*
3286 * This is the receive path handler. It is called by a low level driver when an
3287 * 802.11 MPDU is received from the hardware.
3288 */
103bf9f7 3289void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
6368e4b1
RR
3290{
3291 struct ieee80211_local *local = hw_to_local(hw);
8318d78a
JB
3292 struct ieee80211_rate *rate = NULL;
3293 struct ieee80211_supported_band *sband;
f1d58c25 3294 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
8318d78a 3295
d20ef63d
JB
3296 WARN_ON_ONCE(softirq_count() == 0);
3297
444e3803 3298 if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
77a980dc 3299 goto drop;
8318d78a
JB
3300
3301 sband = local->hw.wiphy->bands[status->band];
77a980dc
JB
3302 if (WARN_ON(!sband))
3303 goto drop;
8318d78a 3304
89c3a8ac
JB
3305 /*
3306 * If we're suspending, it is possible although not too likely
3307 * that we'd be receiving frames after having already partially
3308 * quiesced the stack. We can't process such frames then since
3309 * that might, for example, cause stations to be added or other
3310 * driver callbacks be invoked.
3311 */
77a980dc
JB
3312 if (unlikely(local->quiescing || local->suspended))
3313 goto drop;
89c3a8ac 3314
04800ada
AN
3315 /* We might be during a HW reconfig, prevent Rx for the same reason */
3316 if (unlikely(local->in_reconfig))
3317 goto drop;
3318
ea77f12f
JB
3319 /*
3320 * The same happens when we're not even started,
3321 * but that's worth a warning.
3322 */
77a980dc
JB
3323 if (WARN_ON(!local->started))
3324 goto drop;
ea77f12f 3325
fc885189 3326 if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
e5d6eb83 3327 /*
fc885189
JB
3328 * Validate the rate, unless a PLCP error means that
3329 * we probably can't have a valid rate here anyway.
e5d6eb83 3330 */
fc885189
JB
3331
3332 if (status->flag & RX_FLAG_HT) {
3333 /*
3334 * rate_idx is MCS index, which can be [0-76]
3335 * as documented on:
3336 *
3337 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
3338 *
3339 * Anything else would be some sort of driver or
3340 * hardware error. The driver should catch hardware
3341 * errors.
3342 */
444e3803 3343 if (WARN(status->rate_idx > 76,
fc885189
JB
3344 "Rate marked as an HT rate but passed "
3345 "status->rate_idx is not "
3346 "an MCS index [0-76]: %d (0x%02x)\n",
3347 status->rate_idx,
3348 status->rate_idx))
3349 goto drop;
5614618e
JB
3350 } else if (status->flag & RX_FLAG_VHT) {
3351 if (WARN_ONCE(status->rate_idx > 9 ||
3352 !status->vht_nss ||
3353 status->vht_nss > 8,
3354 "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
3355 status->rate_idx, status->vht_nss))
3356 goto drop;
fc885189 3357 } else {
444e3803 3358 if (WARN_ON(status->rate_idx >= sband->n_bitrates))
fc885189
JB
3359 goto drop;
3360 rate = &sband->bitrates[status->rate_idx];
3361 }
0fb8ca45 3362 }
6368e4b1 3363
554891e6
JB
3364 status->rx_flags = 0;
3365
6368e4b1
RR
3366 /*
3367 * key references and virtual interfaces are protected using RCU
3368 * and this requires that we are in a read-side RCU section during
3369 * receive processing
3370 */
3371 rcu_read_lock();
3372
3373 /*
3374 * Frames with failed FCS/PLCP checksum are not returned,
3375 * all other frames are returned without radiotap header
3376 * if it was previously present.
3377 * Also, frames with less than 16 bytes are dropped.
3378 */
f1d58c25 3379 skb = ieee80211_rx_monitor(local, skb, rate);
6368e4b1
RR
3380 if (!skb) {
3381 rcu_read_unlock();
3382 return;
3383 }
3384
e1e54068
JB
3385 ieee80211_tpt_led_trig_rx(local,
3386 ((struct ieee80211_hdr *)skb->data)->frame_control,
3387 skb->len);
071d9ac2 3388 __ieee80211_rx_handle_packet(hw, skb);
6368e4b1
RR
3389
3390 rcu_read_unlock();
77a980dc
JB
3391
3392 return;
3393 drop:
3394 kfree_skb(skb);
6368e4b1 3395}
103bf9f7 3396EXPORT_SYMBOL(ieee80211_rx);
571ecf67
JB
3397
3398/* This is a version of the rx handler that can be called from hard irq
3399 * context. Post the skb on the queue and schedule the tasklet */
f1d58c25 3400void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
571ecf67
JB
3401{
3402 struct ieee80211_local *local = hw_to_local(hw);
3403
3404 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
3405
571ecf67
JB
3406 skb->pkt_type = IEEE80211_RX_MSG;
3407 skb_queue_tail(&local->skb_queue, skb);
3408 tasklet_schedule(&local->tasklet);
3409}
3410EXPORT_SYMBOL(ieee80211_rx_irqsafe);