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