]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/mac80211/rx.c
mac80211: as a 4-addr station, do not receive packets for other stations
[mirror_ubuntu-artful-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
80 if (status->flag & RX_FLAG_TSFT)
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 */
126 if (status->flag & RX_FLAG_TSFT) {
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 /*
145 * TODO: add following information into radiotap header once
146 * suitable fields are defined for it:
147 * - MCS index (status->rate_idx)
148 * - HT40 (status->flag & RX_FLAG_40MHZ)
149 * - short-GI (status->flag & RX_FLAG_SHORT_GI)
150 */
151 *pos = 0;
8d6f658e 152 } else {
ebe6c7ba 153 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
0fb8ca45 154 *pos = rate->bitrate / 5;
8d6f658e 155 }
601ae7f2
BR
156 pos++;
157
158 /* IEEE80211_RADIOTAP_CHANNEL */
6a86b9c7 159 put_unaligned_le16(status->freq, pos);
601ae7f2
BR
160 pos += 2;
161 if (status->band == IEEE80211_BAND_5GHZ)
6a86b9c7
JB
162 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
163 pos);
5f0b7de5
JB
164 else if (status->flag & RX_FLAG_HT)
165 put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
166 pos);
9deb1ae5 167 else if (rate->flags & IEEE80211_RATE_ERP_G)
6a86b9c7
JB
168 put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
169 pos);
601ae7f2 170 else
6a86b9c7
JB
171 put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
172 pos);
601ae7f2
BR
173 pos += 2;
174
175 /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
176 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
177 *pos = status->signal;
178 rthdr->it_present |=
179 cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
180 pos++;
181 }
182
601ae7f2
BR
183 /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
184
185 /* IEEE80211_RADIOTAP_ANTENNA */
186 *pos = status->antenna;
187 pos++;
188
601ae7f2
BR
189 /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
190
191 /* IEEE80211_RADIOTAP_RX_FLAGS */
192 /* ensure 2 byte alignment for the 2 byte field as required */
6a86b9c7 193 if ((pos - (u8 *)rthdr) & 1)
601ae7f2 194 pos++;
aae89831 195 if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
6a86b9c7
JB
196 rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
197 put_unaligned_le16(rx_flags, pos);
601ae7f2 198 pos += 2;
6d744bac
JB
199
200 if (status->flag & RX_FLAG_HT) {
201 rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
202 *pos++ = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
203 IEEE80211_RADIOTAP_MCS_HAVE_GI |
204 IEEE80211_RADIOTAP_MCS_HAVE_BW;
205 *pos = 0;
206 if (status->flag & RX_FLAG_SHORT_GI)
207 *pos |= IEEE80211_RADIOTAP_MCS_SGI;
208 if (status->flag & RX_FLAG_40MHZ)
209 *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
210 pos++;
211 *pos++ = status->rate_idx;
212 }
601ae7f2
BR
213}
214
b2e7771e
JB
215/*
216 * This function copies a received frame to all monitor interfaces and
217 * returns a cleaned-up SKB that no longer includes the FCS nor the
218 * radiotap header the driver might have added.
219 */
220static struct sk_buff *
221ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
8318d78a 222 struct ieee80211_rate *rate)
b2e7771e 223{
f1d58c25 224 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
b2e7771e 225 struct ieee80211_sub_if_data *sdata;
b2e7771e 226 int needed_headroom = 0;
b2e7771e
JB
227 struct sk_buff *skb, *skb2;
228 struct net_device *prev_dev = NULL;
229 int present_fcs_len = 0;
b2e7771e
JB
230
231 /*
232 * First, we may need to make a copy of the skb because
233 * (1) we need to modify it for radiotap (if not present), and
234 * (2) the other RX handlers will modify the skb we got.
235 *
236 * We don't need to, of course, if we aren't going to return
237 * the SKB because it has a bad FCS/PLCP checksum.
238 */
0869aea0
JB
239
240 /* room for the radiotap header based on driver features */
241 needed_headroom = ieee80211_rx_radiotap_len(local, status);
b2e7771e
JB
242
243 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
244 present_fcs_len = FCS_LEN;
245
e3cf8b3f
ZY
246 /* make sure hdr->frame_control is on the linear part */
247 if (!pskb_may_pull(origskb, 2)) {
248 dev_kfree_skb(origskb);
249 return NULL;
250 }
251
b2e7771e 252 if (!local->monitors) {
0869aea0 253 if (should_drop_frame(origskb, present_fcs_len)) {
b2e7771e
JB
254 dev_kfree_skb(origskb);
255 return NULL;
256 }
257
0869aea0 258 return remove_monitor_info(local, origskb);
b2e7771e
JB
259 }
260
0869aea0 261 if (should_drop_frame(origskb, present_fcs_len)) {
b2e7771e
JB
262 /* only need to expand headroom if necessary */
263 skb = origskb;
264 origskb = NULL;
265
266 /*
267 * This shouldn't trigger often because most devices have an
268 * RX header they pull before we get here, and that should
269 * be big enough for our radiotap information. We should
270 * probably export the length to drivers so that we can have
271 * them allocate enough headroom to start with.
272 */
273 if (skb_headroom(skb) < needed_headroom &&
c49e5ea3 274 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
b2e7771e
JB
275 dev_kfree_skb(skb);
276 return NULL;
277 }
278 } else {
279 /*
280 * Need to make a copy and possibly remove radiotap header
281 * and FCS from the original.
282 */
283 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
284
0869aea0 285 origskb = remove_monitor_info(local, origskb);
b2e7771e
JB
286
287 if (!skb)
288 return origskb;
289 }
290
0869aea0
JB
291 /* prepend radiotap information */
292 ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
b2e7771e 293
ce3edf6d 294 skb_reset_mac_header(skb);
b2e7771e
JB
295 skb->ip_summed = CHECKSUM_UNNECESSARY;
296 skb->pkt_type = PACKET_OTHERHOST;
297 skb->protocol = htons(ETH_P_802_2);
298
299 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
05c914fe 300 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
b2e7771e
JB
301 continue;
302
3d30d949
MW
303 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
304 continue;
305
9607e6b6 306 if (!ieee80211_sdata_running(sdata))
47846c9b
JB
307 continue;
308
b2e7771e
JB
309 if (prev_dev) {
310 skb2 = skb_clone(skb, GFP_ATOMIC);
311 if (skb2) {
312 skb2->dev = prev_dev;
5548a8a1 313 netif_receive_skb(skb2);
b2e7771e
JB
314 }
315 }
316
317 prev_dev = sdata->dev;
318 sdata->dev->stats.rx_packets++;
319 sdata->dev->stats.rx_bytes += skb->len;
320 }
321
322 if (prev_dev) {
323 skb->dev = prev_dev;
5548a8a1 324 netif_receive_skb(skb);
b2e7771e
JB
325 } else
326 dev_kfree_skb(skb);
327
328 return origskb;
329}
330
331
5cf121c3 332static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
6e0d114d 333{
238f74a2 334 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 335 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
6e0d114d
JB
336 int tid;
337
338 /* does the frame have a qos control field? */
238f74a2
HH
339 if (ieee80211_is_data_qos(hdr->frame_control)) {
340 u8 *qc = ieee80211_get_qos_ctl(hdr);
6e0d114d 341 /* frame has qos control */
238f74a2
HH
342 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
343 if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
554891e6 344 status->rx_flags |= IEEE80211_RX_AMSDU;
6e0d114d 345 } else {
1411f9b5
JB
346 /*
347 * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
348 *
349 * Sequence numbers for management frames, QoS data
350 * frames with a broadcast/multicast address in the
351 * Address 1 field, and all non-QoS data frames sent
352 * by QoS STAs are assigned using an additional single
353 * modulo-4096 counter, [...]
354 *
355 * We also use that counter for non-QoS STAs.
356 */
357 tid = NUM_RX_DATA_QUEUES - 1;
6e0d114d 358 }
52865dfd 359
5cf121c3 360 rx->queue = tid;
6e0d114d
JB
361 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
362 * For now, set skb->priority to 0 for other cases. */
363 rx->skb->priority = (tid > 7) ? 0 : tid;
38f3714d 364}
6e0d114d 365
d1c3a37c
JB
366/**
367 * DOC: Packet alignment
368 *
369 * Drivers always need to pass packets that are aligned to two-byte boundaries
370 * to the stack.
371 *
372 * Additionally, should, if possible, align the payload data in a way that
373 * guarantees that the contained IP header is aligned to a four-byte
374 * boundary. In the case of regular frames, this simply means aligning the
375 * payload to a four-byte boundary (because either the IP header is directly
376 * contained, or IV/RFC1042 headers that have a length divisible by four are
59d9cb07
KV
377 * in front of it). If the payload data is not properly aligned and the
378 * architecture doesn't support efficient unaligned operations, mac80211
379 * will align the data.
d1c3a37c
JB
380 *
381 * With A-MSDU frames, however, the payload data address must yield two modulo
382 * four because there are 14-byte 802.3 headers within the A-MSDU frames that
383 * push the IP header further back to a multiple of four again. Thankfully, the
384 * specs were sane enough this time around to require padding each A-MSDU
385 * subframe to a length that is a multiple of four.
386 *
387 * Padding like Atheros hardware adds which is inbetween the 802.11 header and
388 * the payload is not supported, the driver is required to move the 802.11
389 * header to be directly in front of the payload in that case.
390 */
391static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
38f3714d 392{
59d9cb07
KV
393#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
394 WARN_ONCE((unsigned long)rx->skb->data & 1,
395 "unaligned packet at 0x%p\n", rx->skb->data);
d1c3a37c 396#endif
6e0d114d
JB
397}
398
6368e4b1 399
571ecf67
JB
400/* rx handlers */
401
49461622 402static ieee80211_rx_result debug_noinline
5cf121c3 403ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
571ecf67
JB
404{
405 struct ieee80211_local *local = rx->local;
554891e6 406 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
571ecf67
JB
407 struct sk_buff *skb = rx->skb;
408
554891e6 409 if (likely(!(status->rx_flags & IEEE80211_RX_IN_SCAN)))
4080c7cd
JB
410 return RX_CONTINUE;
411
b23b025f
BG
412 if (test_bit(SCAN_HW_SCANNING, &local->scanning) ||
413 test_bit(SCAN_SW_SCANNING, &local->scanning))
f1d58c25 414 return ieee80211_scan_rx(rx->sdata, skb);
ece8eddd 415
4080c7cd
JB
416 /* scanning finished during invoking of handlers */
417 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
418 return RX_DROP_UNUSABLE;
571ecf67
JB
419}
420
3cfcf6ac
JM
421
422static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
423{
424 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
425
426 if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
427 return 0;
428
429 return ieee80211_is_robust_mgmt_frame(hdr);
430}
431
432
433static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
434{
435 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
436
437 if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
438 return 0;
439
440 return ieee80211_is_robust_mgmt_frame(hdr);
441}
442
443
444/* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
445static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
446{
447 struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
448 struct ieee80211_mmie *mmie;
449
450 if (skb->len < 24 + sizeof(*mmie) ||
451 !is_multicast_ether_addr(hdr->da))
452 return -1;
453
454 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
455 return -1; /* not a robust management frame */
456
457 mmie = (struct ieee80211_mmie *)
458 (skb->data + skb->len - sizeof(*mmie));
459 if (mmie->element_id != WLAN_EID_MMIE ||
460 mmie->length != sizeof(*mmie) - 2)
461 return -1;
462
463 return le16_to_cpu(mmie->key_id);
464}
465
466
33b64eb2 467static ieee80211_rx_result
5cf121c3 468ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
33b64eb2 469{
a7767f95
HH
470 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
471 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
47846c9b 472 char *dev_addr = rx->sdata->vif.addr;
d6d1a5a7 473
a7767f95 474 if (ieee80211_is_data(hdr->frame_control)) {
3c5772a5
JC
475 if (is_multicast_ether_addr(hdr->addr1)) {
476 if (ieee80211_has_tods(hdr->frame_control) ||
477 !ieee80211_has_fromds(hdr->frame_control))
478 return RX_DROP_MONITOR;
479 if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
480 return RX_DROP_MONITOR;
481 } else {
482 if (!ieee80211_has_a4(hdr->frame_control))
483 return RX_DROP_MONITOR;
484 if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
485 return RX_DROP_MONITOR;
486 }
33b64eb2
LCC
487 }
488
489 /* If there is not an established peer link and this is not a peer link
490 * establisment frame, beacon or probe, drop the frame.
491 */
492
b4e08ea1 493 if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
33b64eb2 494 struct ieee80211_mgmt *mgmt;
d6d1a5a7 495
a7767f95 496 if (!ieee80211_is_mgmt(hdr->frame_control))
33b64eb2
LCC
497 return RX_DROP_MONITOR;
498
a7767f95 499 if (ieee80211_is_action(hdr->frame_control)) {
33b64eb2 500 mgmt = (struct ieee80211_mgmt *)hdr;
97ad9139 501 if (mgmt->u.action.category != WLAN_CATEGORY_MESH_PLINK)
33b64eb2 502 return RX_DROP_MONITOR;
33b64eb2 503 return RX_CONTINUE;
33b64eb2
LCC
504 }
505
a7767f95
HH
506 if (ieee80211_is_probe_req(hdr->frame_control) ||
507 ieee80211_is_probe_resp(hdr->frame_control) ||
508 ieee80211_is_beacon(hdr->frame_control))
509 return RX_CONTINUE;
510
511 return RX_DROP_MONITOR;
512
513 }
514
515#define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
516
517 if (ieee80211_is_data(hdr->frame_control) &&
518 is_multicast_ether_addr(hdr->addr1) &&
3c5772a5 519 mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
33b64eb2 520 return RX_DROP_MONITOR;
902acc78 521#undef msh_h_get
d6d1a5a7 522
902acc78
JB
523 return RX_CONTINUE;
524}
33b64eb2 525
1edfb1af
JB
526#define SEQ_MODULO 0x1000
527#define SEQ_MASK 0xfff
528
529static inline int seq_less(u16 sq1, u16 sq2)
530{
531 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
532}
533
534static inline u16 seq_inc(u16 sq)
535{
536 return (sq + 1) & SEQ_MASK;
537}
538
539static inline u16 seq_sub(u16 sq1, u16 sq2)
540{
541 return (sq1 - sq2) & SEQ_MASK;
542}
543
544
545static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
546 struct tid_ampdu_rx *tid_agg_rx,
24a8fdad 547 int index)
1edfb1af 548{
24a8fdad 549 struct ieee80211_local *local = hw_to_local(hw);
1edfb1af 550 struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
4cfda47b 551 struct ieee80211_rx_status *status;
1edfb1af 552
dd318575
JB
553 lockdep_assert_held(&tid_agg_rx->reorder_lock);
554
1edfb1af
JB
555 if (!skb)
556 goto no_frame;
557
071d9ac2 558 /* release the frame from the reorder ring buffer */
1edfb1af
JB
559 tid_agg_rx->stored_mpdu_num--;
560 tid_agg_rx->reorder_buf[index] = NULL;
4cfda47b
CL
561 status = IEEE80211_SKB_RXCB(skb);
562 status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
24a8fdad 563 skb_queue_tail(&local->rx_skb_queue, skb);
1edfb1af
JB
564
565no_frame:
566 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
567}
568
569static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
570 struct tid_ampdu_rx *tid_agg_rx,
24a8fdad 571 u16 head_seq_num)
1edfb1af
JB
572{
573 int index;
574
dd318575
JB
575 lockdep_assert_held(&tid_agg_rx->reorder_lock);
576
1edfb1af
JB
577 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
578 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
579 tid_agg_rx->buf_size;
24a8fdad 580 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
1edfb1af
JB
581 }
582}
583
584/*
585 * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
586 * the skb was added to the buffer longer than this time ago, the earlier
587 * frames that have not yet been received are assumed to be lost and the skb
588 * can be released for processing. This may also release other skb's from the
589 * reorder buffer if there are no additional gaps between the frames.
2bff8ebf
CL
590 *
591 * Callers must hold tid_agg_rx->reorder_lock.
1edfb1af
JB
592 */
593#define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
594
aa0c8636 595static void ieee80211_sta_reorder_release(struct ieee80211_hw *hw,
24a8fdad 596 struct tid_ampdu_rx *tid_agg_rx)
aa0c8636 597{
2bff8ebf 598 int index, j;
aa0c8636 599
dd318575
JB
600 lockdep_assert_held(&tid_agg_rx->reorder_lock);
601
aa0c8636
CL
602 /* release the buffer until next missing frame */
603 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
604 tid_agg_rx->buf_size;
605 if (!tid_agg_rx->reorder_buf[index] &&
606 tid_agg_rx->stored_mpdu_num > 1) {
607 /*
608 * No buffers ready to be released, but check whether any
609 * frames in the reorder buffer have timed out.
610 */
aa0c8636
CL
611 int skipped = 1;
612 for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
613 j = (j + 1) % tid_agg_rx->buf_size) {
614 if (!tid_agg_rx->reorder_buf[j]) {
615 skipped++;
616 continue;
617 }
618 if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
619 HT_RX_REORDER_BUF_TIMEOUT))
2bff8ebf 620 goto set_release_timer;
aa0c8636
CL
621
622#ifdef CONFIG_MAC80211_HT_DEBUG
623 if (net_ratelimit())
0fb9a9ec
JP
624 wiphy_debug(hw->wiphy,
625 "release an RX reorder frame due to timeout on earlier frames\n");
aa0c8636 626#endif
24a8fdad 627 ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
aa0c8636
CL
628
629 /*
630 * Increment the head seq# also for the skipped slots.
631 */
632 tid_agg_rx->head_seq_num =
633 (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
634 skipped = 0;
635 }
636 } else while (tid_agg_rx->reorder_buf[index]) {
24a8fdad 637 ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
aa0c8636
CL
638 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
639 tid_agg_rx->buf_size;
640 }
2bff8ebf
CL
641
642 if (tid_agg_rx->stored_mpdu_num) {
643 j = index = seq_sub(tid_agg_rx->head_seq_num,
644 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
645
646 for (; j != (index - 1) % tid_agg_rx->buf_size;
647 j = (j + 1) % tid_agg_rx->buf_size) {
648 if (tid_agg_rx->reorder_buf[j])
649 break;
650 }
651
652 set_release_timer:
653
654 mod_timer(&tid_agg_rx->reorder_timer,
655 tid_agg_rx->reorder_time[j] +
656 HT_RX_REORDER_BUF_TIMEOUT);
657 } else {
658 del_timer(&tid_agg_rx->reorder_timer);
659 }
aa0c8636
CL
660}
661
1edfb1af
JB
662/*
663 * As this function belongs to the RX path it must be under
664 * rcu_read_lock protection. It returns false if the frame
665 * can be processed immediately, true if it was consumed.
666 */
667static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
668 struct tid_ampdu_rx *tid_agg_rx,
24a8fdad 669 struct sk_buff *skb)
1edfb1af
JB
670{
671 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
672 u16 sc = le16_to_cpu(hdr->seq_ctrl);
673 u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
674 u16 head_seq_num, buf_size;
675 int index;
2bff8ebf 676 bool ret = true;
1edfb1af 677
dd318575
JB
678 spin_lock(&tid_agg_rx->reorder_lock);
679
1edfb1af
JB
680 buf_size = tid_agg_rx->buf_size;
681 head_seq_num = tid_agg_rx->head_seq_num;
682
683 /* frame with out of date sequence number */
684 if (seq_less(mpdu_seq_num, head_seq_num)) {
685 dev_kfree_skb(skb);
2bff8ebf 686 goto out;
1edfb1af
JB
687 }
688
689 /*
690 * If frame the sequence number exceeds our buffering window
691 * size release some previous frames to make room for this one.
692 */
693 if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
694 head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
695 /* release stored frames up to new head to stack */
24a8fdad 696 ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
1edfb1af
JB
697 }
698
699 /* Now the new frame is always in the range of the reordering buffer */
700
701 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
702
703 /* check if we already stored this frame */
704 if (tid_agg_rx->reorder_buf[index]) {
705 dev_kfree_skb(skb);
2bff8ebf 706 goto out;
1edfb1af
JB
707 }
708
709 /*
710 * If the current MPDU is in the right order and nothing else
711 * is stored we can process it directly, no need to buffer it.
712 */
713 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
714 tid_agg_rx->stored_mpdu_num == 0) {
715 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2bff8ebf
CL
716 ret = false;
717 goto out;
1edfb1af
JB
718 }
719
720 /* put the frame in the reordering buffer */
721 tid_agg_rx->reorder_buf[index] = skb;
722 tid_agg_rx->reorder_time[index] = jiffies;
723 tid_agg_rx->stored_mpdu_num++;
24a8fdad 724 ieee80211_sta_reorder_release(hw, tid_agg_rx);
1edfb1af 725
2bff8ebf
CL
726 out:
727 spin_unlock(&tid_agg_rx->reorder_lock);
728 return ret;
1edfb1af
JB
729}
730
731/*
732 * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
733 * true if the MPDU was buffered, false if it should be processed.
734 */
24a8fdad 735static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx)
1edfb1af 736{
2569a826
JB
737 struct sk_buff *skb = rx->skb;
738 struct ieee80211_local *local = rx->local;
1edfb1af
JB
739 struct ieee80211_hw *hw = &local->hw;
740 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2569a826 741 struct sta_info *sta = rx->sta;
1edfb1af
JB
742 struct tid_ampdu_rx *tid_agg_rx;
743 u16 sc;
744 int tid;
745
746 if (!ieee80211_is_data_qos(hdr->frame_control))
2569a826 747 goto dont_reorder;
1edfb1af
JB
748
749 /*
750 * filter the QoS data rx stream according to
751 * STA/TID and check if this STA/TID is on aggregation
752 */
753
1edfb1af 754 if (!sta)
2569a826 755 goto dont_reorder;
1edfb1af
JB
756
757 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
758
a87f736d
JB
759 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
760 if (!tid_agg_rx)
761 goto dont_reorder;
1edfb1af
JB
762
763 /* qos null data frames are excluded */
764 if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
a87f736d 765 goto dont_reorder;
1edfb1af
JB
766
767 /* new, potentially un-ordered, ampdu frame - process it */
768
769 /* reset session timer */
770 if (tid_agg_rx->timeout)
771 mod_timer(&tid_agg_rx->session_timer,
772 TU_TO_EXP_TIME(tid_agg_rx->timeout));
773
774 /* if this mpdu is fragmented - terminate rx aggregation session */
775 sc = le16_to_cpu(hdr->seq_ctrl);
776 if (sc & IEEE80211_SCTL_FRAG) {
c1475ca9 777 skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
344eec67
JB
778 skb_queue_tail(&rx->sdata->skb_queue, skb);
779 ieee80211_queue_work(&local->hw, &rx->sdata->work);
2569a826 780 return;
1edfb1af
JB
781 }
782
a87f736d
JB
783 /*
784 * No locking needed -- we will only ever process one
785 * RX packet at a time, and thus own tid_agg_rx. All
786 * other code manipulating it needs to (and does) make
787 * sure that we cannot get to it any more before doing
788 * anything with it.
789 */
24a8fdad 790 if (ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb))
2569a826
JB
791 return;
792
793 dont_reorder:
24a8fdad 794 skb_queue_tail(&local->rx_skb_queue, skb);
1edfb1af 795}
33b64eb2 796
49461622 797static ieee80211_rx_result debug_noinline
5cf121c3 798ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
571ecf67 799{
a7767f95 800 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
554891e6 801 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
571ecf67
JB
802
803 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
804 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
a7767f95 805 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
5cf121c3 806 rx->sta->last_seq_ctrl[rx->queue] ==
571ecf67 807 hdr->seq_ctrl)) {
554891e6 808 if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
571ecf67
JB
809 rx->local->dot11FrameDuplicateCount++;
810 rx->sta->num_duplicates++;
811 }
b1f93314 812 return RX_DROP_UNUSABLE;
571ecf67 813 } else
5cf121c3 814 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
571ecf67
JB
815 }
816
571ecf67
JB
817 if (unlikely(rx->skb->len < 16)) {
818 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
e4c26add 819 return RX_DROP_MONITOR;
571ecf67
JB
820 }
821
571ecf67
JB
822 /* Drop disallowed frame classes based on STA auth/assoc state;
823 * IEEE 802.11, Chap 5.5.
824 *
ccd7b362
JB
825 * mac80211 filters only based on association state, i.e. it drops
826 * Class 3 frames from not associated stations. hostapd sends
571ecf67
JB
827 * deauth/disassoc frames when needed. In addition, hostapd is
828 * responsible for filtering on both auth and assoc states.
829 */
33b64eb2 830
902acc78 831 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
33b64eb2 832 return ieee80211_rx_mesh_check(rx);
33b64eb2 833
a7767f95
HH
834 if (unlikely((ieee80211_is_data(hdr->frame_control) ||
835 ieee80211_is_pspoll(hdr->frame_control)) &&
05c914fe 836 rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
1be7fe8d 837 rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
07346f81 838 (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
a7767f95
HH
839 if ((!ieee80211_has_fromds(hdr->frame_control) &&
840 !ieee80211_has_tods(hdr->frame_control) &&
841 ieee80211_is_data(hdr->frame_control)) ||
554891e6 842 !(status->rx_flags & IEEE80211_RX_RA_MATCH)) {
571ecf67
JB
843 /* Drop IBSS frames and frames for other hosts
844 * silently. */
e4c26add 845 return RX_DROP_MONITOR;
571ecf67
JB
846 }
847
e4c26add 848 return RX_DROP_MONITOR;
571ecf67
JB
849 }
850
9ae54c84 851 return RX_CONTINUE;
570bd537
JB
852}
853
854
49461622 855static ieee80211_rx_result debug_noinline
5cf121c3 856ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
570bd537 857{
eb9fb5b8
JB
858 struct sk_buff *skb = rx->skb;
859 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
860 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3017b80b
JB
861 int keyidx;
862 int hdrlen;
e4c26add 863 ieee80211_rx_result result = RX_DROP_UNUSABLE;
e31b8213 864 struct ieee80211_key *sta_ptk = NULL;
3cfcf6ac 865 int mmie_keyidx = -1;
761ab470 866 __le16 fc;
570bd537 867
3017b80b
JB
868 /*
869 * Key selection 101
870 *
3cfcf6ac 871 * There are four types of keys:
3017b80b 872 * - GTK (group keys)
3cfcf6ac 873 * - IGTK (group keys for management frames)
3017b80b
JB
874 * - PTK (pairwise keys)
875 * - STK (station-to-station pairwise keys)
876 *
877 * When selecting a key, we have to distinguish between multicast
878 * (including broadcast) and unicast frames, the latter can only
3cfcf6ac
JM
879 * use PTKs and STKs while the former always use GTKs and IGTKs.
880 * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
881 * unicast frames can also use key indices like GTKs. Hence, if we
882 * don't have a PTK/STK we check the key index for a WEP key.
3017b80b 883 *
8dc06a1c
JB
884 * Note that in a regular BSS, multicast frames are sent by the
885 * AP only, associated stations unicast the frame to the AP first
886 * which then multicasts it on their behalf.
887 *
3017b80b
JB
888 * There is also a slight problem in IBSS mode: GTKs are negotiated
889 * with each station, that is something we don't currently handle.
8dc06a1c
JB
890 * The spec seems to expect that one negotiates the same key with
891 * every station but there's no such requirement; VLANs could be
892 * possible.
3017b80b
JB
893 */
894
3017b80b 895 /*
1990af8d 896 * No point in finding a key and decrypting if the frame is neither
3017b80b
JB
897 * addressed to us nor a multicast frame.
898 */
554891e6 899 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
9ae54c84 900 return RX_CONTINUE;
3017b80b 901
2569a826
JB
902 /* start without a key */
903 rx->key = NULL;
904
d4e46a3d 905 if (rx->sta)
e31b8213 906 sta_ptk = rcu_dereference(rx->sta->ptk);
d4e46a3d 907
761ab470
JB
908 fc = hdr->frame_control;
909
910 if (!ieee80211_has_protected(fc))
0c7c10c7
JM
911 mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
912
e31b8213
JB
913 if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
914 rx->key = sta_ptk;
dc1580dd
JB
915 if ((status->flag & RX_FLAG_DECRYPTED) &&
916 (status->flag & RX_FLAG_IV_STRIPPED))
917 return RX_CONTINUE;
0c7c10c7 918 /* Skip decryption if the frame is not protected. */
761ab470 919 if (!ieee80211_has_protected(fc))
0c7c10c7 920 return RX_CONTINUE;
3cfcf6ac
JM
921 } else if (mmie_keyidx >= 0) {
922 /* Broadcast/multicast robust management frame / BIP */
eb9fb5b8
JB
923 if ((status->flag & RX_FLAG_DECRYPTED) &&
924 (status->flag & RX_FLAG_IV_STRIPPED))
3cfcf6ac
JM
925 return RX_CONTINUE;
926
927 if (mmie_keyidx < NUM_DEFAULT_KEYS ||
928 mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
929 return RX_DROP_MONITOR; /* unexpected BIP keyidx */
e31b8213
JB
930 if (rx->sta)
931 rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
932 if (!rx->key)
933 rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
761ab470 934 } else if (!ieee80211_has_protected(fc)) {
0c7c10c7
JM
935 /*
936 * The frame was not protected, so skip decryption. However, we
937 * need to set rx->key if there is a key that could have been
938 * used so that the frame may be dropped if encryption would
939 * have been expected.
940 */
941 struct ieee80211_key *key = NULL;
897bed8b
JB
942 struct ieee80211_sub_if_data *sdata = rx->sdata;
943 int i;
944
761ab470 945 if (ieee80211_is_mgmt(fc) &&
0c7c10c7
JM
946 is_multicast_ether_addr(hdr->addr1) &&
947 (key = rcu_dereference(rx->sdata->default_mgmt_key)))
948 rx->key = key;
897bed8b
JB
949 else {
950 if (rx->sta) {
951 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
952 key = rcu_dereference(rx->sta->gtk[i]);
953 if (key)
954 break;
955 }
956 }
957 if (!key) {
958 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
959 key = rcu_dereference(sdata->keys[i]);
960 if (key)
961 break;
962 }
963 }
964 if (key)
965 rx->key = key;
966 }
0c7c10c7 967 return RX_CONTINUE;
571ecf67 968 } else {
39184b15 969 u8 keyid;
3017b80b
JB
970 /*
971 * The device doesn't give us the IV so we won't be
972 * able to look up the key. That's ok though, we
973 * don't need to decrypt the frame, we just won't
974 * be able to keep statistics accurate.
975 * Except for key threshold notifications, should
976 * we somehow allow the driver to tell us which key
977 * the hardware used if this flag is set?
978 */
eb9fb5b8
JB
979 if ((status->flag & RX_FLAG_DECRYPTED) &&
980 (status->flag & RX_FLAG_IV_STRIPPED))
9ae54c84 981 return RX_CONTINUE;
3017b80b 982
761ab470 983 hdrlen = ieee80211_hdrlen(fc);
3017b80b
JB
984
985 if (rx->skb->len < 8 + hdrlen)
e4c26add 986 return RX_DROP_UNUSABLE; /* TODO: count this? */
3017b80b
JB
987
988 /*
989 * no need to call ieee80211_wep_get_keyidx,
990 * it verifies a bunch of things we've done already
991 */
39184b15
ZY
992 skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
993 keyidx = keyid >> 6;
3017b80b 994
e31b8213
JB
995 /* check per-station GTK first, if multicast packet */
996 if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
997 rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
3017b80b 998
e31b8213
JB
999 /* if not found, try default key */
1000 if (!rx->key) {
1001 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
1002
1003 /*
1004 * RSNA-protected unicast frames should always be
1005 * sent with pairwise or station-to-station keys,
1006 * but for WEP we allow using a key index as well.
1007 */
1008 if (rx->key &&
1009 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1010 rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
1011 !is_multicast_ether_addr(hdr->addr1))
1012 rx->key = NULL;
1013 }
571ecf67
JB
1014 }
1015
3017b80b 1016 if (rx->key) {
571ecf67 1017 rx->key->tx_rx_count++;
011bfcc4 1018 /* TODO: add threshold stuff again */
1990af8d 1019 } else {
e4c26add 1020 return RX_DROP_MONITOR;
70f08765
JB
1021 }
1022
39184b15
ZY
1023 if (skb_linearize(rx->skb))
1024 return RX_DROP_UNUSABLE;
761ab470 1025 /* the hdr variable is invalid now! */
1990af8d 1026
97359d12
JB
1027 switch (rx->key->conf.cipher) {
1028 case WLAN_CIPHER_SUITE_WEP40:
1029 case WLAN_CIPHER_SUITE_WEP104:
761ab470
JB
1030 /* Check for weak IVs if possible */
1031 if (rx->sta && ieee80211_is_data(fc) &&
1032 (!(status->flag & RX_FLAG_IV_STRIPPED) ||
1033 !(status->flag & RX_FLAG_DECRYPTED)) &&
1034 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
1035 rx->sta->wep_weak_iv_count++;
1036
e2f036da
MN
1037 result = ieee80211_crypto_wep_decrypt(rx);
1038 break;
97359d12 1039 case WLAN_CIPHER_SUITE_TKIP:
e2f036da
MN
1040 result = ieee80211_crypto_tkip_decrypt(rx);
1041 break;
97359d12 1042 case WLAN_CIPHER_SUITE_CCMP:
e2f036da
MN
1043 result = ieee80211_crypto_ccmp_decrypt(rx);
1044 break;
97359d12 1045 case WLAN_CIPHER_SUITE_AES_CMAC:
3cfcf6ac
JM
1046 result = ieee80211_crypto_aes_cmac_decrypt(rx);
1047 break;
3ffc2a90
JB
1048 default:
1049 /*
1050 * We can reach here only with HW-only algorithms
1051 * but why didn't it decrypt the frame?!
1052 */
1053 return RX_DROP_UNUSABLE;
70f08765
JB
1054 }
1055
e2f036da 1056 /* either the frame has been decrypted or will be dropped */
eb9fb5b8 1057 status->flag |= RX_FLAG_DECRYPTED;
e2f036da
MN
1058
1059 return result;
70f08765
JB
1060}
1061
572e0012
KV
1062static ieee80211_rx_result debug_noinline
1063ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
1064{
1065 struct ieee80211_local *local;
1066 struct ieee80211_hdr *hdr;
1067 struct sk_buff *skb;
1068
1069 local = rx->local;
1070 skb = rx->skb;
1071 hdr = (struct ieee80211_hdr *) skb->data;
1072
1073 if (!local->pspolling)
1074 return RX_CONTINUE;
1075
1076 if (!ieee80211_has_fromds(hdr->frame_control))
1077 /* this is not from AP */
1078 return RX_CONTINUE;
1079
1080 if (!ieee80211_is_data(hdr->frame_control))
1081 return RX_CONTINUE;
1082
1083 if (!ieee80211_has_moredata(hdr->frame_control)) {
1084 /* AP has no more frames buffered for us */
1085 local->pspolling = false;
1086 return RX_CONTINUE;
1087 }
1088
1089 /* more data bit is set, let's request a new frame from the AP */
1090 ieee80211_send_pspoll(local, rx->sdata);
1091
1092 return RX_CONTINUE;
1093}
1094
133b8226 1095static void ap_sta_ps_start(struct sta_info *sta)
571ecf67 1096{
133b8226 1097 struct ieee80211_sub_if_data *sdata = sta->sdata;
4571d3bf 1098 struct ieee80211_local *local = sdata->local;
0795af57 1099
3e122be0 1100 atomic_inc(&sdata->bss->num_sta_ps);
af818581 1101 set_sta_flags(sta, WLAN_STA_PS_STA);
d057e5a3
AN
1102 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1103 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
571ecf67 1104#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0c68ae26 1105 printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
47846c9b 1106 sdata->name, sta->sta.addr, sta->sta.aid);
571ecf67
JB
1107#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1108}
1109
ff9458d3 1110static void ap_sta_ps_end(struct sta_info *sta)
571ecf67 1111{
133b8226 1112 struct ieee80211_sub_if_data *sdata = sta->sdata;
571ecf67 1113
3e122be0 1114 atomic_dec(&sdata->bss->num_sta_ps);
004c872e 1115
571ecf67 1116#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0c68ae26 1117 printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
47846c9b 1118 sdata->name, sta->sta.addr, sta->sta.aid);
571ecf67 1119#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
004c872e 1120
af818581 1121 if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
571ecf67 1122#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
af818581 1123 printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
47846c9b 1124 sdata->name, sta->sta.addr, sta->sta.aid);
571ecf67 1125#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
af818581
JB
1126 return;
1127 }
1128
1129 ieee80211_sta_ps_deliver_wakeup(sta);
571ecf67
JB
1130}
1131
d057e5a3
AN
1132int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
1133{
1134 struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
1135 bool in_ps;
1136
1137 WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
1138
1139 /* Don't let the same PS state be set twice */
1140 in_ps = test_sta_flags(sta_inf, WLAN_STA_PS_STA);
1141 if ((start && in_ps) || (!start && !in_ps))
1142 return -EINVAL;
1143
1144 if (start)
1145 ap_sta_ps_start(sta_inf);
1146 else
1147 ap_sta_ps_end(sta_inf);
1148
1149 return 0;
1150}
1151EXPORT_SYMBOL(ieee80211_sta_ps_transition);
1152
49461622 1153static ieee80211_rx_result debug_noinline
5cf121c3 1154ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
571ecf67
JB
1155{
1156 struct sta_info *sta = rx->sta;
eb9fb5b8
JB
1157 struct sk_buff *skb = rx->skb;
1158 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
1159 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
571ecf67
JB
1160
1161 if (!sta)
9ae54c84 1162 return RX_CONTINUE;
571ecf67 1163
b291ba11
JB
1164 /*
1165 * Update last_rx only for IBSS packets which are for the current
1166 * BSSID to avoid keeping the current IBSS network alive in cases
1167 * where other STAs start using different BSSID.
1168 */
05c914fe 1169 if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
71364716 1170 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
05c914fe 1171 NL80211_IFTYPE_ADHOC);
46900298 1172 if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
571ecf67 1173 sta->last_rx = jiffies;
b291ba11
JB
1174 } else if (!is_multicast_ether_addr(hdr->addr1)) {
1175 /*
33b64eb2
LCC
1176 * Mesh beacons will update last_rx when if they are found to
1177 * match the current local configuration when processed.
571ecf67 1178 */
b291ba11 1179 sta->last_rx = jiffies;
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;
554891e6 2547 rx->flags = 0;
2569a826
JB
2548
2549 CALL_RXH(ieee80211_rx_h_decrypt)
2550 CALL_RXH(ieee80211_rx_h_check_more_data)
2551 CALL_RXH(ieee80211_rx_h_sta_process)
2552 CALL_RXH(ieee80211_rx_h_defragment)
2553 CALL_RXH(ieee80211_rx_h_ps_poll)
2554 CALL_RXH(ieee80211_rx_h_michael_mic_verify)
2555 /* must be after MMIC verify so header is counted in MPDU mic */
2556 CALL_RXH(ieee80211_rx_h_remove_qos_control)
2557 CALL_RXH(ieee80211_rx_h_amsdu)
bf94e17b 2558#ifdef CONFIG_MAC80211_MESH
aa0c8636 2559 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
2569a826 2560 CALL_RXH(ieee80211_rx_h_mesh_fwding);
bf94e17b 2561#endif
2569a826 2562 CALL_RXH(ieee80211_rx_h_data)
24a8fdad 2563 CALL_RXH(ieee80211_rx_h_ctrl);
2e161f78 2564 CALL_RXH(ieee80211_rx_h_mgmt_check)
2569a826 2565 CALL_RXH(ieee80211_rx_h_action)
2e161f78
JB
2566 CALL_RXH(ieee80211_rx_h_userspace_mgmt)
2567 CALL_RXH(ieee80211_rx_h_action_return)
2569a826 2568 CALL_RXH(ieee80211_rx_h_mgmt)
49461622 2569
aa0c8636
CL
2570 rxh_next:
2571 ieee80211_rx_handlers_result(rx, res);
24a8fdad 2572 spin_lock(&rx->local->rx_skb_queue.lock);
49461622 2573#undef CALL_RXH
aa0c8636 2574 }
24a8fdad
CL
2575
2576 rx->local->running_rx_handler = false;
2577
2578 unlock:
2579 spin_unlock(&rx->local->rx_skb_queue.lock);
aa0c8636
CL
2580}
2581
4406c376 2582static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
aa0c8636 2583{
aa0c8636
CL
2584 ieee80211_rx_result res = RX_DROP_MONITOR;
2585
aa0c8636
CL
2586#define CALL_RXH(rxh) \
2587 do { \
2588 res = rxh(rx); \
2589 if (res != RX_CONTINUE) \
2590 goto rxh_next; \
2591 } while (0);
2592
2593 CALL_RXH(ieee80211_rx_h_passive_scan)
2594 CALL_RXH(ieee80211_rx_h_check)
2595
24a8fdad 2596 ieee80211_rx_reorder_ampdu(rx);
aa0c8636 2597
24a8fdad 2598 ieee80211_rx_handlers(rx);
aa0c8636 2599 return;
49461622 2600
2569a826 2601 rxh_next:
aa0c8636
CL
2602 ieee80211_rx_handlers_result(rx, res);
2603
2604#undef CALL_RXH
58905290
JB
2605}
2606
2bff8ebf 2607/*
dd318575
JB
2608 * This function makes calls into the RX path, therefore
2609 * it has to be invoked under RCU read lock.
2bff8ebf
CL
2610 */
2611void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
2612{
554891e6
JB
2613 struct ieee80211_rx_data rx = {
2614 .sta = sta,
2615 .sdata = sta->sdata,
2616 .local = sta->local,
2617 .queue = tid,
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);