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