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