]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/mac80211/rx.c
mac80211: require four hardware queues for QoS/HT
[mirror_ubuntu-zesty-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>
5 * Copyright 2007 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
ab46623e 12#include <linux/jiffies.h>
571ecf67
JB
13#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
d4e46a3d 17#include <linux/rcupdate.h>
571ecf67
JB
18#include <net/mac80211.h>
19#include <net/ieee80211_radiotap.h>
20
21#include "ieee80211_i.h"
2c8dccc7 22#include "led.h"
33b64eb2 23#include "mesh.h"
571ecf67
JB
24#include "wep.h"
25#include "wpa.h"
26#include "tkip.h"
27#include "wme.h"
28
71364716
RR
29u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
30 struct tid_ampdu_rx *tid_agg_rx,
31 struct sk_buff *skb, u16 mpdu_seq_num,
32 int bar_req);
b2e7771e
JB
33/*
34 * monitor mode reception
35 *
36 * This function cleans up the SKB, i.e. it removes all the stuff
37 * only useful for monitoring.
38 */
39static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
40 struct sk_buff *skb,
41 int rtap_len)
42{
43 skb_pull(skb, rtap_len);
44
45 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
46 if (likely(skb->len > FCS_LEN))
47 skb_trim(skb, skb->len - FCS_LEN);
48 else {
49 /* driver bug */
50 WARN_ON(1);
51 dev_kfree_skb(skb);
52 skb = NULL;
53 }
54 }
55
56 return skb;
57}
58
59static inline int should_drop_frame(struct ieee80211_rx_status *status,
60 struct sk_buff *skb,
61 int present_fcs_len,
62 int radiotap_len)
63{
64 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
65
66 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
67 return 1;
68 if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
69 return 1;
98f0b0a3
RR
70 if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
71 cpu_to_le16(IEEE80211_FTYPE_CTL)) &&
72 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
71364716
RR
73 cpu_to_le16(IEEE80211_STYPE_PSPOLL)) &&
74 ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
75 cpu_to_le16(IEEE80211_STYPE_BACK_REQ)))
b2e7771e
JB
76 return 1;
77 return 0;
78}
79
80/*
81 * This function copies a received frame to all monitor interfaces and
82 * returns a cleaned-up SKB that no longer includes the FCS nor the
83 * radiotap header the driver might have added.
84 */
85static struct sk_buff *
86ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
8318d78a
JB
87 struct ieee80211_rx_status *status,
88 struct ieee80211_rate *rate)
b2e7771e
JB
89{
90 struct ieee80211_sub_if_data *sdata;
b2e7771e 91 int needed_headroom = 0;
c49e5ea3
JB
92 struct ieee80211_radiotap_header *rthdr;
93 __le64 *rttsft = NULL;
94 struct ieee80211_rtap_fixed_data {
b2e7771e
JB
95 u8 flags;
96 u8 rate;
97 __le16 chan_freq;
98 __le16 chan_flags;
99 u8 antsignal;
100 u8 padding_for_rxflags;
101 __le16 rx_flags;
c49e5ea3 102 } __attribute__ ((packed)) *rtfixed;
b2e7771e
JB
103 struct sk_buff *skb, *skb2;
104 struct net_device *prev_dev = NULL;
105 int present_fcs_len = 0;
106 int rtap_len = 0;
107
108 /*
109 * First, we may need to make a copy of the skb because
110 * (1) we need to modify it for radiotap (if not present), and
111 * (2) the other RX handlers will modify the skb we got.
112 *
113 * We don't need to, of course, if we aren't going to return
114 * the SKB because it has a bad FCS/PLCP checksum.
115 */
116 if (status->flag & RX_FLAG_RADIOTAP)
117 rtap_len = ieee80211_get_radiotap_len(origskb->data);
118 else
c49e5ea3
JB
119 /* room for radiotap header, always present fields and TSFT */
120 needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
b2e7771e
JB
121
122 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
123 present_fcs_len = FCS_LEN;
124
125 if (!local->monitors) {
126 if (should_drop_frame(status, origskb, present_fcs_len,
127 rtap_len)) {
128 dev_kfree_skb(origskb);
129 return NULL;
130 }
131
132 return remove_monitor_info(local, origskb, rtap_len);
133 }
134
135 if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
136 /* only need to expand headroom if necessary */
137 skb = origskb;
138 origskb = NULL;
139
140 /*
141 * This shouldn't trigger often because most devices have an
142 * RX header they pull before we get here, and that should
143 * be big enough for our radiotap information. We should
144 * probably export the length to drivers so that we can have
145 * them allocate enough headroom to start with.
146 */
147 if (skb_headroom(skb) < needed_headroom &&
c49e5ea3 148 pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
b2e7771e
JB
149 dev_kfree_skb(skb);
150 return NULL;
151 }
152 } else {
153 /*
154 * Need to make a copy and possibly remove radiotap header
155 * and FCS from the original.
156 */
157 skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
158
159 origskb = remove_monitor_info(local, origskb, rtap_len);
160
161 if (!skb)
162 return origskb;
163 }
164
165 /* if necessary, prepend radiotap information */
166 if (!(status->flag & RX_FLAG_RADIOTAP)) {
c49e5ea3
JB
167 rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
168 rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
169 if (status->flag & RX_FLAG_TSFT) {
170 rttsft = (void *) skb_push(skb, sizeof(*rttsft));
171 rtap_len += 8;
172 }
b2e7771e
JB
173 rthdr = (void *) skb_push(skb, sizeof(*rthdr));
174 memset(rthdr, 0, sizeof(*rthdr));
c49e5ea3
JB
175 memset(rtfixed, 0, sizeof(*rtfixed));
176 rthdr->it_present =
b2e7771e
JB
177 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
178 (1 << IEEE80211_RADIOTAP_RATE) |
179 (1 << IEEE80211_RADIOTAP_CHANNEL) |
180 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
181 (1 << IEEE80211_RADIOTAP_RX_FLAGS));
c49e5ea3
JB
182 rtfixed->flags = 0;
183 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
184 rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
185
186 if (rttsft) {
187 *rttsft = cpu_to_le64(status->mactime);
188 rthdr->it_present |=
189 cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
190 }
b2e7771e
JB
191
192 /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
c49e5ea3 193 rtfixed->rx_flags = 0;
b2e7771e
JB
194 if (status->flag &
195 (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
c49e5ea3 196 rtfixed->rx_flags |=
b2e7771e
JB
197 cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
198
8318d78a 199 rtfixed->rate = rate->bitrate / 5;
b2e7771e 200
c49e5ea3 201 rtfixed->chan_freq = cpu_to_le16(status->freq);
b2e7771e 202
8318d78a 203 if (status->band == IEEE80211_BAND_5GHZ)
c49e5ea3 204 rtfixed->chan_flags =
b2e7771e
JB
205 cpu_to_le16(IEEE80211_CHAN_OFDM |
206 IEEE80211_CHAN_5GHZ);
207 else
c49e5ea3 208 rtfixed->chan_flags =
b2e7771e
JB
209 cpu_to_le16(IEEE80211_CHAN_DYN |
210 IEEE80211_CHAN_2GHZ);
211
c49e5ea3
JB
212 rtfixed->antsignal = status->ssi;
213 rthdr->it_len = cpu_to_le16(rtap_len);
b2e7771e
JB
214 }
215
ce3edf6d 216 skb_reset_mac_header(skb);
b2e7771e
JB
217 skb->ip_summed = CHECKSUM_UNNECESSARY;
218 skb->pkt_type = PACKET_OTHERHOST;
219 skb->protocol = htons(ETH_P_802_2);
220
221 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
222 if (!netif_running(sdata->dev))
223 continue;
224
51fb61e7 225 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR)
b2e7771e
JB
226 continue;
227
3d30d949
MW
228 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
229 continue;
230
b2e7771e
JB
231 if (prev_dev) {
232 skb2 = skb_clone(skb, GFP_ATOMIC);
233 if (skb2) {
234 skb2->dev = prev_dev;
235 netif_rx(skb2);
236 }
237 }
238
239 prev_dev = sdata->dev;
240 sdata->dev->stats.rx_packets++;
241 sdata->dev->stats.rx_bytes += skb->len;
242 }
243
244 if (prev_dev) {
245 skb->dev = prev_dev;
246 netif_rx(skb);
247 } else
248 dev_kfree_skb(skb);
249
250 return origskb;
251}
252
253
5cf121c3 254static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
6e0d114d
JB
255{
256 u8 *data = rx->skb->data;
257 int tid;
258
259 /* does the frame have a qos control field? */
260 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
261 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
262 /* frame has qos control */
263 tid = qc[0] & QOS_CONTROL_TID_MASK;
fd4c7f2f 264 if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
5cf121c3 265 rx->flags |= IEEE80211_RX_AMSDU;
fd4c7f2f 266 else
5cf121c3 267 rx->flags &= ~IEEE80211_RX_AMSDU;
6e0d114d
JB
268 } else {
269 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
270 /* Separate TID for management frames */
271 tid = NUM_RX_DATA_QUEUES - 1;
272 } else {
273 /* no qos control present */
274 tid = 0; /* 802.1d - Best Effort */
275 }
276 }
52865dfd 277
5cf121c3 278 rx->queue = tid;
6e0d114d
JB
279 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
280 * For now, set skb->priority to 0 for other cases. */
281 rx->skb->priority = (tid > 7) ? 0 : tid;
38f3714d 282}
6e0d114d 283
5cf121c3 284static void ieee80211_verify_ip_alignment(struct ieee80211_rx_data *rx)
38f3714d
JB
285{
286#ifdef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
287 int hdrlen;
288
289 if (!WLAN_FC_DATA_PRESENT(rx->fc))
290 return;
291
292 /*
293 * Drivers are required to align the payload data in a way that
294 * guarantees that the contained IP header is aligned to a four-
295 * byte boundary. In the case of regular frames, this simply means
296 * aligning the payload to a four-byte boundary (because either
297 * the IP header is directly contained, or IV/RFC1042 headers that
298 * have a length divisible by four are in front of it.
299 *
300 * With A-MSDU frames, however, the payload data address must
301 * yield two modulo four because there are 14-byte 802.3 headers
302 * within the A-MSDU frames that push the IP header further back
303 * to a multiple of four again. Thankfully, the specs were sane
304 * enough this time around to require padding each A-MSDU subframe
305 * to a length that is a multiple of four.
306 *
307 * Padding like atheros hardware adds which is inbetween the 802.11
308 * header and the payload is not supported, the driver is required
309 * to move the 802.11 header further back in that case.
310 */
311 hdrlen = ieee80211_get_hdrlen(rx->fc);
5cf121c3 312 if (rx->flags & IEEE80211_RX_AMSDU)
38f3714d
JB
313 hdrlen += ETH_HLEN;
314 WARN_ON_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3);
315#endif
6e0d114d
JB
316}
317
6368e4b1 318
71ebb4aa 319static u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
8318d78a
JB
320 struct sk_buff *skb,
321 struct ieee80211_rx_status *status,
322 struct ieee80211_rate *rate)
571ecf67 323{
571ecf67
JB
324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
325 u32 load = 0, hdrtime;
571ecf67
JB
326
327 /* Estimate total channel use caused by this frame */
328
571ecf67
JB
329 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
330 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
331
8318d78a
JB
332 if (status->band == IEEE80211_BAND_5GHZ ||
333 (status->band == IEEE80211_BAND_5GHZ &&
334 rate->flags & IEEE80211_RATE_ERP_G))
571ecf67
JB
335 hdrtime = CHAN_UTIL_HDR_SHORT;
336 else
337 hdrtime = CHAN_UTIL_HDR_LONG;
338
339 load = hdrtime;
340 if (!is_multicast_ether_addr(hdr->addr1))
341 load += hdrtime;
342
8318d78a
JB
343 /* TODO: optimise again */
344 load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
571ecf67
JB
345
346 /* Divide channel_use by 8 to avoid wrapping around the counter */
347 load >>= CHAN_UTIL_SHIFT;
571ecf67 348
6368e4b1 349 return load;
571ecf67
JB
350}
351
571ecf67
JB
352/* rx handlers */
353
9ae54c84 354static ieee80211_rx_result
5cf121c3 355ieee80211_rx_h_if_stats(struct ieee80211_rx_data *rx)
571ecf67 356{
52865dfd 357 if (rx->sta)
5cf121c3
JB
358 rx->sta->channel_use_raw += rx->load;
359 rx->sdata->channel_use_raw += rx->load;
9ae54c84 360 return RX_CONTINUE;
571ecf67
JB
361}
362
9ae54c84 363static ieee80211_rx_result
5cf121c3 364ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
571ecf67
JB
365{
366 struct ieee80211_local *local = rx->local;
367 struct sk_buff *skb = rx->skb;
368
ece8eddd 369 if (unlikely(local->sta_hw_scanning))
5cf121c3 370 return ieee80211_sta_rx_scan(rx->dev, skb, rx->status);
ece8eddd
ZY
371
372 if (unlikely(local->sta_sw_scanning)) {
373 /* drop all the other packets during a software scan anyway */
5cf121c3 374 if (ieee80211_sta_rx_scan(rx->dev, skb, rx->status)
9ae54c84 375 != RX_QUEUED)
ece8eddd 376 dev_kfree_skb(skb);
9ae54c84 377 return RX_QUEUED;
571ecf67
JB
378 }
379
5cf121c3 380 if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
571ecf67
JB
381 /* scanning finished during invoking of handlers */
382 I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
e4c26add 383 return RX_DROP_UNUSABLE;
571ecf67
JB
384 }
385
9ae54c84 386 return RX_CONTINUE;
571ecf67
JB
387}
388
33b64eb2 389static ieee80211_rx_result
5cf121c3 390ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
33b64eb2
LCC
391{
392 int hdrlen = ieee80211_get_hdrlen(rx->fc);
393 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
d6d1a5a7
JB
394
395#define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
396
33b64eb2
LCC
397 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
398 if (!((rx->fc & IEEE80211_FCTL_FROMDS) &&
399 (rx->fc & IEEE80211_FCTL_TODS)))
400 return RX_DROP_MONITOR;
401 if (memcmp(hdr->addr4, rx->dev->dev_addr, ETH_ALEN) == 0)
402 return RX_DROP_MONITOR;
403 }
404
405 /* If there is not an established peer link and this is not a peer link
406 * establisment frame, beacon or probe, drop the frame.
407 */
408
b4e08ea1 409 if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
33b64eb2 410 struct ieee80211_mgmt *mgmt;
d6d1a5a7 411
33b64eb2
LCC
412 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
413 return RX_DROP_MONITOR;
414
415 switch (rx->fc & IEEE80211_FCTL_STYPE) {
416 case IEEE80211_STYPE_ACTION:
417 mgmt = (struct ieee80211_mgmt *)hdr;
418 if (mgmt->u.action.category != PLINK_CATEGORY)
419 return RX_DROP_MONITOR;
420 /* fall through on else */
421 case IEEE80211_STYPE_PROBE_REQ:
422 case IEEE80211_STYPE_PROBE_RESP:
423 case IEEE80211_STYPE_BEACON:
424 return RX_CONTINUE;
425 break;
426 default:
427 return RX_DROP_MONITOR;
428 }
429
430 } else if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
966a5428 431 is_multicast_ether_addr(hdr->addr1) &&
33b64eb2
LCC
432 mesh_rmc_check(hdr->addr4, msh_h_get(hdr, hdrlen), rx->dev))
433 return RX_DROP_MONITOR;
902acc78 434#undef msh_h_get
d6d1a5a7 435
902acc78
JB
436 return RX_CONTINUE;
437}
33b64eb2
LCC
438
439
9ae54c84 440static ieee80211_rx_result
5cf121c3 441ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
571ecf67
JB
442{
443 struct ieee80211_hdr *hdr;
33b64eb2 444
571ecf67
JB
445 hdr = (struct ieee80211_hdr *) rx->skb->data;
446
447 /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
448 if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
449 if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
5cf121c3 450 rx->sta->last_seq_ctrl[rx->queue] ==
571ecf67 451 hdr->seq_ctrl)) {
5cf121c3 452 if (rx->flags & IEEE80211_RX_RA_MATCH) {
571ecf67
JB
453 rx->local->dot11FrameDuplicateCount++;
454 rx->sta->num_duplicates++;
455 }
e4c26add 456 return RX_DROP_MONITOR;
571ecf67 457 } else
5cf121c3 458 rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
571ecf67
JB
459 }
460
571ecf67
JB
461 if (unlikely(rx->skb->len < 16)) {
462 I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
e4c26add 463 return RX_DROP_MONITOR;
571ecf67
JB
464 }
465
571ecf67
JB
466 /* Drop disallowed frame classes based on STA auth/assoc state;
467 * IEEE 802.11, Chap 5.5.
468 *
469 * 80211.o does filtering only based on association state, i.e., it
470 * drops Class 3 frames from not associated stations. hostapd sends
471 * deauth/disassoc frames when needed. In addition, hostapd is
472 * responsible for filtering on both auth and assoc states.
473 */
33b64eb2 474
902acc78 475 if (ieee80211_vif_is_mesh(&rx->sdata->vif))
33b64eb2 476 return ieee80211_rx_mesh_check(rx);
33b64eb2 477
571ecf67
JB
478 if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
479 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
480 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
51fb61e7 481 rx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
571ecf67
JB
482 (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
483 if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
484 !(rx->fc & IEEE80211_FCTL_TODS) &&
485 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
5cf121c3 486 || !(rx->flags & IEEE80211_RX_RA_MATCH)) {
571ecf67
JB
487 /* Drop IBSS frames and frames for other hosts
488 * silently. */
e4c26add 489 return RX_DROP_MONITOR;
571ecf67
JB
490 }
491
e4c26add 492 return RX_DROP_MONITOR;
571ecf67
JB
493 }
494
9ae54c84 495 return RX_CONTINUE;
570bd537
JB
496}
497
498
9ae54c84 499static ieee80211_rx_result
5cf121c3 500ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
570bd537
JB
501{
502 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
3017b80b
JB
503 int keyidx;
504 int hdrlen;
e4c26add 505 ieee80211_rx_result result = RX_DROP_UNUSABLE;
d4e46a3d 506 struct ieee80211_key *stakey = NULL;
570bd537 507
3017b80b
JB
508 /*
509 * Key selection 101
510 *
511 * There are three types of keys:
512 * - GTK (group keys)
513 * - PTK (pairwise keys)
514 * - STK (station-to-station pairwise keys)
515 *
516 * When selecting a key, we have to distinguish between multicast
517 * (including broadcast) and unicast frames, the latter can only
518 * use PTKs and STKs while the former always use GTKs. Unless, of
519 * course, actual WEP keys ("pre-RSNA") are used, then unicast
520 * frames can also use key indizes like GTKs. Hence, if we don't
521 * have a PTK/STK we check the key index for a WEP key.
522 *
8dc06a1c
JB
523 * Note that in a regular BSS, multicast frames are sent by the
524 * AP only, associated stations unicast the frame to the AP first
525 * which then multicasts it on their behalf.
526 *
3017b80b
JB
527 * There is also a slight problem in IBSS mode: GTKs are negotiated
528 * with each station, that is something we don't currently handle.
8dc06a1c
JB
529 * The spec seems to expect that one negotiates the same key with
530 * every station but there's no such requirement; VLANs could be
531 * possible.
3017b80b
JB
532 */
533
534 if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
9ae54c84 535 return RX_CONTINUE;
571ecf67 536
3017b80b 537 /*
1990af8d 538 * No point in finding a key and decrypting if the frame is neither
3017b80b
JB
539 * addressed to us nor a multicast frame.
540 */
5cf121c3 541 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
9ae54c84 542 return RX_CONTINUE;
3017b80b 543
d4e46a3d
JB
544 if (rx->sta)
545 stakey = rcu_dereference(rx->sta->key);
546
547 if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
548 rx->key = stakey;
571ecf67 549 } else {
3017b80b
JB
550 /*
551 * The device doesn't give us the IV so we won't be
552 * able to look up the key. That's ok though, we
553 * don't need to decrypt the frame, we just won't
554 * be able to keep statistics accurate.
555 * Except for key threshold notifications, should
556 * we somehow allow the driver to tell us which key
557 * the hardware used if this flag is set?
558 */
5cf121c3
JB
559 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
560 (rx->status->flag & RX_FLAG_IV_STRIPPED))
9ae54c84 561 return RX_CONTINUE;
3017b80b
JB
562
563 hdrlen = ieee80211_get_hdrlen(rx->fc);
564
565 if (rx->skb->len < 8 + hdrlen)
e4c26add 566 return RX_DROP_UNUSABLE; /* TODO: count this? */
3017b80b
JB
567
568 /*
569 * no need to call ieee80211_wep_get_keyidx,
570 * it verifies a bunch of things we've done already
571 */
572 keyidx = rx->skb->data[hdrlen + 3] >> 6;
573
d4e46a3d 574 rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
3017b80b
JB
575
576 /*
577 * RSNA-protected unicast frames should always be sent with
578 * pairwise or station-to-station keys, but for WEP we allow
579 * using a key index as well.
580 */
8f20fc24 581 if (rx->key && rx->key->conf.alg != ALG_WEP &&
3017b80b
JB
582 !is_multicast_ether_addr(hdr->addr1))
583 rx->key = NULL;
571ecf67
JB
584 }
585
3017b80b 586 if (rx->key) {
571ecf67 587 rx->key->tx_rx_count++;
011bfcc4 588 /* TODO: add threshold stuff again */
1990af8d 589 } else {
7f3ad894 590#ifdef CONFIG_MAC80211_DEBUG
70f08765
JB
591 if (net_ratelimit())
592 printk(KERN_DEBUG "%s: RX protected frame,"
593 " but have no key\n", rx->dev->name);
7f3ad894 594#endif /* CONFIG_MAC80211_DEBUG */
e4c26add 595 return RX_DROP_MONITOR;
70f08765
JB
596 }
597
1990af8d
JB
598 /* Check for weak IVs if possible */
599 if (rx->sta && rx->key->conf.alg == ALG_WEP &&
600 ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
5cf121c3
JB
601 (!(rx->status->flag & RX_FLAG_IV_STRIPPED) ||
602 !(rx->status->flag & RX_FLAG_DECRYPTED)) &&
1990af8d
JB
603 ieee80211_wep_is_weak_iv(rx->skb, rx->key))
604 rx->sta->wep_weak_iv_count++;
605
70f08765
JB
606 switch (rx->key->conf.alg) {
607 case ALG_WEP:
e2f036da
MN
608 result = ieee80211_crypto_wep_decrypt(rx);
609 break;
70f08765 610 case ALG_TKIP:
e2f036da
MN
611 result = ieee80211_crypto_tkip_decrypt(rx);
612 break;
70f08765 613 case ALG_CCMP:
e2f036da
MN
614 result = ieee80211_crypto_ccmp_decrypt(rx);
615 break;
70f08765
JB
616 }
617
e2f036da 618 /* either the frame has been decrypted or will be dropped */
5cf121c3 619 rx->status->flag |= RX_FLAG_DECRYPTED;
e2f036da
MN
620
621 return result;
70f08765
JB
622}
623
571ecf67
JB
624static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
625{
626 struct ieee80211_sub_if_data *sdata;
0795af57
JP
627 DECLARE_MAC_BUF(mac);
628
d0709a65 629 sdata = sta->sdata;
571ecf67
JB
630
631 if (sdata->bss)
632 atomic_inc(&sdata->bss->num_sta_ps);
633 sta->flags |= WLAN_STA_PS;
4a9a66e9 634 sta->flags &= ~WLAN_STA_PSPOLL;
571ecf67 635#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
636 printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
637 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67
JB
638#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
639}
640
641static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
642{
643 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
644 struct sk_buff *skb;
645 int sent = 0;
646 struct ieee80211_sub_if_data *sdata;
647 struct ieee80211_tx_packet_data *pkt_data;
0795af57 648 DECLARE_MAC_BUF(mac);
571ecf67 649
d0709a65 650 sdata = sta->sdata;
004c872e 651
571ecf67
JB
652 if (sdata->bss)
653 atomic_dec(&sdata->bss->num_sta_ps);
004c872e 654
836341a7 655 sta->flags &= ~(WLAN_STA_PS | WLAN_STA_PSPOLL);
004c872e
JB
656
657 if (!skb_queue_empty(&sta->ps_tx_buf))
658 sta_info_clear_tim_bit(sta);
659
571ecf67 660#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
661 printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
662 dev->name, print_mac(mac, sta->addr), sta->aid);
571ecf67 663#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
004c872e 664
571ecf67
JB
665 /* Send all buffered frames to the station */
666 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
667 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
668 sent++;
e8bf9649 669 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
670 dev_queue_xmit(skb);
671 }
672 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
673 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
674 local->total_ps_buffered--;
675 sent++;
676#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57 677 printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
571ecf67 678 "since STA not sleeping anymore\n", dev->name,
0795af57 679 print_mac(mac, sta->addr), sta->aid);
571ecf67 680#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e8bf9649 681 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
571ecf67
JB
682 dev_queue_xmit(skb);
683 }
684
685 return sent;
686}
687
9ae54c84 688static ieee80211_rx_result
5cf121c3 689ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
571ecf67
JB
690{
691 struct sta_info *sta = rx->sta;
692 struct net_device *dev = rx->dev;
693 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
694
695 if (!sta)
9ae54c84 696 return RX_CONTINUE;
571ecf67
JB
697
698 /* Update last_rx only for IBSS packets which are for the current
699 * BSSID to avoid keeping the current IBSS network alive in cases where
700 * other STAs are using different BSSID. */
51fb61e7 701 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
71364716
RR
702 u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
703 IEEE80211_IF_TYPE_IBSS);
571ecf67
JB
704 if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
705 sta->last_rx = jiffies;
706 } else
707 if (!is_multicast_ether_addr(hdr->addr1) ||
51fb61e7 708 rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
571ecf67
JB
709 /* Update last_rx only for unicast frames in order to prevent
710 * the Probe Request frames (the only broadcast frames from a
711 * STA in infrastructure mode) from keeping a connection alive.
33b64eb2
LCC
712 * Mesh beacons will update last_rx when if they are found to
713 * match the current local configuration when processed.
571ecf67
JB
714 */
715 sta->last_rx = jiffies;
716 }
717
5cf121c3 718 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
9ae54c84 719 return RX_CONTINUE;
571ecf67
JB
720
721 sta->rx_fragments++;
722 sta->rx_bytes += rx->skb->len;
5cf121c3
JB
723 sta->last_rssi = rx->status->ssi;
724 sta->last_signal = rx->status->signal;
725 sta->last_noise = rx->status->noise;
571ecf67
JB
726
727 if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
728 /* Change STA power saving mode only in the end of a frame
729 * exchange sequence */
730 if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
5cf121c3 731 rx->sent_ps_buffered += ap_sta_ps_end(dev, sta);
571ecf67
JB
732 else if (!(sta->flags & WLAN_STA_PS) &&
733 (rx->fc & IEEE80211_FCTL_PM))
734 ap_sta_ps_start(dev, sta);
735 }
736
737 /* Drop data::nullfunc frames silently, since they are used only to
738 * control station power saving mode. */
739 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
740 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
741 I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
742 /* Update counter and free packet here to avoid counting this
743 * as a dropped packed. */
744 sta->rx_packets++;
745 dev_kfree_skb(rx->skb);
9ae54c84 746 return RX_QUEUED;
571ecf67
JB
747 }
748
9ae54c84 749 return RX_CONTINUE;
571ecf67
JB
750} /* ieee80211_rx_h_sta_process */
751
571ecf67
JB
752static inline struct ieee80211_fragment_entry *
753ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
754 unsigned int frag, unsigned int seq, int rx_queue,
755 struct sk_buff **skb)
756{
757 struct ieee80211_fragment_entry *entry;
758 int idx;
759
760 idx = sdata->fragment_next;
761 entry = &sdata->fragments[sdata->fragment_next++];
762 if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
763 sdata->fragment_next = 0;
764
765 if (!skb_queue_empty(&entry->skb_list)) {
766#ifdef CONFIG_MAC80211_DEBUG
767 struct ieee80211_hdr *hdr =
768 (struct ieee80211_hdr *) entry->skb_list.next->data;
0795af57
JP
769 DECLARE_MAC_BUF(mac);
770 DECLARE_MAC_BUF(mac2);
571ecf67
JB
771 printk(KERN_DEBUG "%s: RX reassembly removed oldest "
772 "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
0795af57 773 "addr1=%s addr2=%s\n",
571ecf67
JB
774 sdata->dev->name, idx,
775 jiffies - entry->first_frag_time, entry->seq,
0795af57
JP
776 entry->last_frag, print_mac(mac, hdr->addr1),
777 print_mac(mac2, hdr->addr2));
571ecf67
JB
778#endif /* CONFIG_MAC80211_DEBUG */
779 __skb_queue_purge(&entry->skb_list);
780 }
781
782 __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
783 *skb = NULL;
784 entry->first_frag_time = jiffies;
785 entry->seq = seq;
786 entry->rx_queue = rx_queue;
787 entry->last_frag = frag;
788 entry->ccmp = 0;
789 entry->extra_len = 0;
790
791 return entry;
792}
793
794static inline struct ieee80211_fragment_entry *
795ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
796 u16 fc, unsigned int frag, unsigned int seq,
797 int rx_queue, struct ieee80211_hdr *hdr)
798{
799 struct ieee80211_fragment_entry *entry;
800 int i, idx;
801
802 idx = sdata->fragment_next;
803 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
804 struct ieee80211_hdr *f_hdr;
805 u16 f_fc;
806
807 idx--;
808 if (idx < 0)
809 idx = IEEE80211_FRAGMENT_MAX - 1;
810
811 entry = &sdata->fragments[idx];
812 if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
813 entry->rx_queue != rx_queue ||
814 entry->last_frag + 1 != frag)
815 continue;
816
817 f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
818 f_fc = le16_to_cpu(f_hdr->frame_control);
819
820 if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
821 compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
822 compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
823 continue;
824
ab46623e 825 if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
571ecf67
JB
826 __skb_queue_purge(&entry->skb_list);
827 continue;
828 }
829 return entry;
830 }
831
832 return NULL;
833}
834
9ae54c84 835static ieee80211_rx_result
5cf121c3 836ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
571ecf67
JB
837{
838 struct ieee80211_hdr *hdr;
839 u16 sc;
840 unsigned int frag, seq;
841 struct ieee80211_fragment_entry *entry;
842 struct sk_buff *skb;
0795af57 843 DECLARE_MAC_BUF(mac);
571ecf67
JB
844
845 hdr = (struct ieee80211_hdr *) rx->skb->data;
846 sc = le16_to_cpu(hdr->seq_ctrl);
847 frag = sc & IEEE80211_SCTL_FRAG;
848
849 if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
850 (rx->skb)->len < 24 ||
851 is_multicast_ether_addr(hdr->addr1))) {
852 /* not fragmented */
853 goto out;
854 }
855 I802_DEBUG_INC(rx->local->rx_handlers_fragments);
856
857 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
858
859 if (frag == 0) {
860 /* This is the first fragment of a new frame. */
861 entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
5cf121c3 862 rx->queue, &(rx->skb));
8f20fc24 863 if (rx->key && rx->key->conf.alg == ALG_CCMP &&
571ecf67
JB
864 (rx->fc & IEEE80211_FCTL_PROTECTED)) {
865 /* Store CCMP PN so that we can verify that the next
866 * fragment has a sequential PN value. */
867 entry->ccmp = 1;
868 memcpy(entry->last_pn,
5cf121c3 869 rx->key->u.ccmp.rx_pn[rx->queue],
571ecf67
JB
870 CCMP_PN_LEN);
871 }
9ae54c84 872 return RX_QUEUED;
571ecf67
JB
873 }
874
875 /* This is a fragment for a frame that should already be pending in
876 * fragment cache. Add this fragment to the end of the pending entry.
877 */
878 entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
5cf121c3 879 rx->queue, hdr);
571ecf67
JB
880 if (!entry) {
881 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
e4c26add 882 return RX_DROP_MONITOR;
571ecf67
JB
883 }
884
885 /* Verify that MPDUs within one MSDU have sequential PN values.
886 * (IEEE 802.11i, 8.3.3.4.5) */
887 if (entry->ccmp) {
888 int i;
889 u8 pn[CCMP_PN_LEN], *rpn;
8f20fc24 890 if (!rx->key || rx->key->conf.alg != ALG_CCMP)
e4c26add 891 return RX_DROP_UNUSABLE;
571ecf67
JB
892 memcpy(pn, entry->last_pn, CCMP_PN_LEN);
893 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
894 pn[i]++;
895 if (pn[i])
896 break;
897 }
5cf121c3 898 rpn = rx->key->u.ccmp.rx_pn[rx->queue];
571ecf67 899 if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
1a84f3fd
JB
900 if (net_ratelimit())
901 printk(KERN_DEBUG "%s: defrag: CCMP PN not "
0795af57 902 "sequential A2=%s"
1a84f3fd
JB
903 " PN=%02x%02x%02x%02x%02x%02x "
904 "(expected %02x%02x%02x%02x%02x%02x)\n",
0795af57 905 rx->dev->name, print_mac(mac, hdr->addr2),
1a84f3fd
JB
906 rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
907 rpn[5], pn[0], pn[1], pn[2], pn[3],
908 pn[4], pn[5]);
e4c26add 909 return RX_DROP_UNUSABLE;
571ecf67
JB
910 }
911 memcpy(entry->last_pn, pn, CCMP_PN_LEN);
912 }
913
914 skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
915 __skb_queue_tail(&entry->skb_list, rx->skb);
916 entry->last_frag = frag;
917 entry->extra_len += rx->skb->len;
918 if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
919 rx->skb = NULL;
9ae54c84 920 return RX_QUEUED;
571ecf67
JB
921 }
922
923 rx->skb = __skb_dequeue(&entry->skb_list);
924 if (skb_tailroom(rx->skb) < entry->extra_len) {
925 I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
926 if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
927 GFP_ATOMIC))) {
928 I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
929 __skb_queue_purge(&entry->skb_list);
e4c26add 930 return RX_DROP_UNUSABLE;
571ecf67
JB
931 }
932 }
933 while ((skb = __skb_dequeue(&entry->skb_list))) {
934 memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
935 dev_kfree_skb(skb);
936 }
937
938 /* Complete frame has been reassembled - process it now */
5cf121c3 939 rx->flags |= IEEE80211_RX_FRAGMENTED;
571ecf67
JB
940
941 out:
942 if (rx->sta)
943 rx->sta->rx_packets++;
944 if (is_multicast_ether_addr(hdr->addr1))
945 rx->local->dot11MulticastReceivedFrameCount++;
946 else
947 ieee80211_led_rx(rx->local);
9ae54c84 948 return RX_CONTINUE;
571ecf67
JB
949}
950
9ae54c84 951static ieee80211_rx_result
5cf121c3 952ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
571ecf67 953{
98f0b0a3 954 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
571ecf67
JB
955 struct sk_buff *skb;
956 int no_pending_pkts;
0795af57 957 DECLARE_MAC_BUF(mac);
571ecf67
JB
958
959 if (likely(!rx->sta ||
960 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
961 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
5cf121c3 962 !(rx->flags & IEEE80211_RX_RA_MATCH)))
9ae54c84 963 return RX_CONTINUE;
571ecf67 964
51fb61e7
JB
965 if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
966 (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
e4c26add 967 return RX_DROP_UNUSABLE;
98f0b0a3 968
571ecf67
JB
969 skb = skb_dequeue(&rx->sta->tx_filtered);
970 if (!skb) {
971 skb = skb_dequeue(&rx->sta->ps_tx_buf);
972 if (skb)
973 rx->local->total_ps_buffered--;
974 }
975 no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
976 skb_queue_empty(&rx->sta->ps_tx_buf);
977
978 if (skb) {
979 struct ieee80211_hdr *hdr =
980 (struct ieee80211_hdr *) skb->data;
981
4a9a66e9
JB
982 /*
983 * Tell TX path to send one frame even though the STA may
984 * still remain is PS mode after this frame exchange.
985 */
986 rx->sta->flags |= WLAN_STA_PSPOLL;
571ecf67
JB
987
988#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57
JP
989 printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
990 print_mac(mac, rx->sta->addr), rx->sta->aid,
571ecf67
JB
991 skb_queue_len(&rx->sta->ps_tx_buf));
992#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
993
994 /* Use MoreData flag to indicate whether there are more
995 * buffered frames for this STA */
836341a7 996 if (no_pending_pkts)
571ecf67 997 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
836341a7 998 else
571ecf67
JB
999 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1000
1001 dev_queue_xmit(skb);
1002
004c872e
JB
1003 if (no_pending_pkts)
1004 sta_info_clear_tim_bit(rx->sta);
571ecf67 1005#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
5cf121c3 1006 } else if (!rx->sent_ps_buffered) {
004c872e
JB
1007 /*
1008 * FIXME: This can be the result of a race condition between
1009 * us expiring a frame and the station polling for it.
1010 * Should we send it a null-func frame indicating we
1011 * have nothing buffered for it?
1012 */
0795af57 1013 printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
571ecf67 1014 "though there is no buffered frames for it\n",
0795af57 1015 rx->dev->name, print_mac(mac, rx->sta->addr));
571ecf67 1016#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
571ecf67
JB
1017 }
1018
9ae54c84 1019 /* Free PS Poll skb here instead of returning RX_DROP that would
571ecf67
JB
1020 * count as an dropped frame. */
1021 dev_kfree_skb(rx->skb);
1022
9ae54c84 1023 return RX_QUEUED;
571ecf67
JB
1024}
1025
9ae54c84 1026static ieee80211_rx_result
5cf121c3 1027ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
6e0d114d
JB
1028{
1029 u16 fc = rx->fc;
1030 u8 *data = rx->skb->data;
1031 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
1032
1033 if (!WLAN_FC_IS_QOS_DATA(fc))
9ae54c84 1034 return RX_CONTINUE;
6e0d114d
JB
1035
1036 /* remove the qos control field, update frame type and meta-data */
1037 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
1038 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
1039 /* change frame type to non QOS */
1040 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
1041 hdr->frame_control = cpu_to_le16(fc);
1042
9ae54c84 1043 return RX_CONTINUE;
6e0d114d
JB
1044}
1045
76ee65bf 1046static int
5cf121c3 1047ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
571ecf67 1048{
238814fd 1049 if (unlikely(!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED))) {
571ecf67 1050#ifdef CONFIG_MAC80211_DEBUG
238814fd
JB
1051 if (net_ratelimit())
1052 printk(KERN_DEBUG "%s: dropped frame "
1053 "(unauthorized port)\n", rx->dev->name);
571ecf67 1054#endif /* CONFIG_MAC80211_DEBUG */
76ee65bf 1055 return -EACCES;
571ecf67
JB
1056 }
1057
76ee65bf 1058 return 0;
571ecf67
JB
1059}
1060
76ee65bf 1061static int
5cf121c3 1062ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx)
571ecf67 1063{
3017b80b 1064 /*
7848ba7d
JB
1065 * Pass through unencrypted frames if the hardware has
1066 * decrypted them already.
3017b80b 1067 */
5cf121c3 1068 if (rx->status->flag & RX_FLAG_DECRYPTED)
76ee65bf 1069 return 0;
571ecf67
JB
1070
1071 /* Drop unencrypted frames if key is set. */
1072 if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
1073 (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
1074 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
b3fc9c6c 1075 (rx->key || rx->sdata->drop_unencrypted)))
76ee65bf 1076 return -EACCES;
b3fc9c6c 1077
76ee65bf 1078 return 0;
571ecf67
JB
1079}
1080
76ee65bf 1081static int
5cf121c3 1082ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
571ecf67
JB
1083{
1084 struct net_device *dev = rx->dev;
571ecf67
JB
1085 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
1086 u16 fc, hdrlen, ethertype;
1087 u8 *payload;
1088 u8 dst[ETH_ALEN];
1089 u8 src[ETH_ALEN];
76ee65bf 1090 struct sk_buff *skb = rx->skb;
571ecf67 1091 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
0795af57
JP
1092 DECLARE_MAC_BUF(mac);
1093 DECLARE_MAC_BUF(mac2);
1094 DECLARE_MAC_BUF(mac3);
1095 DECLARE_MAC_BUF(mac4);
571ecf67
JB
1096
1097 fc = rx->fc;
571ecf67
JB
1098
1099 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
76ee65bf 1100 return -1;
571ecf67
JB
1101
1102 hdrlen = ieee80211_get_hdrlen(fc);
1103
902acc78 1104 if (ieee80211_vif_is_mesh(&sdata->vif)) {
33b64eb2
LCC
1105 int meshhdrlen = ieee80211_get_mesh_hdrlen(
1106 (struct ieee80211s_hdr *) (skb->data + hdrlen));
1107 /* Copy on cb:
1108 * - mesh header: to be used for mesh forwarding
1109 * decision. It will also be used as mesh header template at
1110 * tx.c:ieee80211_subif_start_xmit() if interface
1111 * type is mesh and skb->pkt_type == PACKET_OTHERHOST
1112 * - ta: to be used if a RERR needs to be sent.
1113 */
1114 memcpy(skb->cb, skb->data + hdrlen, meshhdrlen);
1115 memcpy(MESH_PREQ(skb), hdr->addr2, ETH_ALEN);
1116 hdrlen += meshhdrlen;
1117 }
33b64eb2 1118
571ecf67
JB
1119 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
1120 * header
1121 * IEEE 802.11 address fields:
1122 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
1123 * 0 0 DA SA BSSID n/a
1124 * 0 1 DA BSSID SA n/a
1125 * 1 0 BSSID SA DA n/a
1126 * 1 1 RA TA DA SA
1127 */
1128
1129 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
1130 case IEEE80211_FCTL_TODS:
1131 /* BSSID SA DA */
1132 memcpy(dst, hdr->addr3, ETH_ALEN);
1133 memcpy(src, hdr->addr2, ETH_ALEN);
1134
51fb61e7
JB
1135 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
1136 sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
1a84f3fd
JB
1137 if (net_ratelimit())
1138 printk(KERN_DEBUG "%s: dropped ToDS frame "
0795af57 1139 "(BSSID=%s SA=%s DA=%s)\n",
1a84f3fd 1140 dev->name,
0795af57
JP
1141 print_mac(mac, hdr->addr1),
1142 print_mac(mac2, hdr->addr2),
1143 print_mac(mac3, hdr->addr3));
76ee65bf 1144 return -1;
571ecf67
JB
1145 }
1146 break;
1147 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
1148 /* RA TA DA SA */
1149 memcpy(dst, hdr->addr3, ETH_ALEN);
1150 memcpy(src, hdr->addr4, ETH_ALEN);
1151
33b64eb2
LCC
1152 if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS &&
1153 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT)) {
1154 if (net_ratelimit())
1155 printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
0795af57 1156 "frame (RA=%s TA=%s DA=%s SA=%s)\n",
1a84f3fd 1157 rx->dev->name,
0795af57
JP
1158 print_mac(mac, hdr->addr1),
1159 print_mac(mac2, hdr->addr2),
1160 print_mac(mac3, hdr->addr3),
1161 print_mac(mac4, hdr->addr4));
76ee65bf 1162 return -1;
571ecf67
JB
1163 }
1164 break;
1165 case IEEE80211_FCTL_FROMDS:
1166 /* DA BSSID SA */
1167 memcpy(dst, hdr->addr1, ETH_ALEN);
1168 memcpy(src, hdr->addr3, ETH_ALEN);
1169
51fb61e7 1170 if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
b3316157
JL
1171 (is_multicast_ether_addr(dst) &&
1172 !compare_ether_addr(src, dev->dev_addr)))
76ee65bf 1173 return -1;
571ecf67
JB
1174 break;
1175 case 0:
1176 /* DA SA BSSID */
1177 memcpy(dst, hdr->addr1, ETH_ALEN);
1178 memcpy(src, hdr->addr2, ETH_ALEN);
1179
51fb61e7 1180 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
571ecf67 1181 if (net_ratelimit()) {
0795af57
JP
1182 printk(KERN_DEBUG "%s: dropped IBSS frame "
1183 "(DA=%s SA=%s BSSID=%s)\n",
1184 dev->name,
1185 print_mac(mac, hdr->addr1),
1186 print_mac(mac2, hdr->addr2),
1187 print_mac(mac3, hdr->addr3));
571ecf67 1188 }
76ee65bf 1189 return -1;
571ecf67
JB
1190 }
1191 break;
1192 }
1193
571ecf67
JB
1194 if (unlikely(skb->len - hdrlen < 8)) {
1195 if (net_ratelimit()) {
1196 printk(KERN_DEBUG "%s: RX too short data frame "
1197 "payload\n", dev->name);
1198 }
76ee65bf 1199 return -1;
571ecf67
JB
1200 }
1201
76ee65bf 1202 payload = skb->data + hdrlen;
571ecf67
JB
1203 ethertype = (payload[6] << 8) | payload[7];
1204
1205 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
1206 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1207 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
1208 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1209 * replace EtherType */
1210 skb_pull(skb, hdrlen + 6);
1211 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1212 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1213 } else {
1214 struct ethhdr *ehdr;
1215 __be16 len;
ce3edf6d 1216
571ecf67
JB
1217 skb_pull(skb, hdrlen);
1218 len = htons(skb->len);
1219 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
1220 memcpy(ehdr->h_dest, dst, ETH_ALEN);
1221 memcpy(ehdr->h_source, src, ETH_ALEN);
1222 ehdr->h_proto = len;
1223 }
76ee65bf
RR
1224 return 0;
1225}
571ecf67 1226
ce3edf6d
JB
1227/*
1228 * requires that rx->skb is a frame with ethernet header
1229 */
5cf121c3 1230static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx)
ce3edf6d
JB
1231{
1232 static const u8 pae_group_addr[ETH_ALEN]
1233 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
1234 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1235
1236 /*
1237 * Allow EAPOL frames to us/the PAE group address regardless
1238 * of whether the frame was encrypted or not.
1239 */
1240 if (ehdr->h_proto == htons(ETH_P_PAE) &&
1241 (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
1242 compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
1243 return true;
1244
1245 if (ieee80211_802_1x_port_control(rx) ||
1246 ieee80211_drop_unencrypted(rx))
1247 return false;
1248
1249 return true;
1250}
1251
1252/*
1253 * requires that rx->skb is a frame with ethernet header
1254 */
76ee65bf 1255static void
5cf121c3 1256ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
76ee65bf
RR
1257{
1258 struct net_device *dev = rx->dev;
1259 struct ieee80211_local *local = rx->local;
1260 struct sk_buff *skb, *xmit_skb;
1261 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
ce3edf6d
JB
1262 struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
1263 struct sta_info *dsta;
571ecf67 1264
76ee65bf
RR
1265 skb = rx->skb;
1266 xmit_skb = NULL;
571ecf67 1267
51fb61e7
JB
1268 if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
1269 sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
5cf121c3 1270 (rx->flags & IEEE80211_RX_RA_MATCH)) {
ce3edf6d
JB
1271 if (is_multicast_ether_addr(ehdr->h_dest)) {
1272 /*
1273 * send multicast frames both to higher layers in
1274 * local net stack and back to the wireless medium
1275 */
76ee65bf
RR
1276 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1277 if (!xmit_skb && net_ratelimit())
571ecf67
JB
1278 printk(KERN_DEBUG "%s: failed to clone "
1279 "multicast frame\n", dev->name);
1280 } else {
571ecf67 1281 dsta = sta_info_get(local, skb->data);
d0709a65 1282 if (dsta && dsta->sdata->dev == dev) {
ce3edf6d
JB
1283 /*
1284 * The destination station is associated to
1285 * this AP (in this VLAN), so send the frame
1286 * directly to it and do not pass it to local
1287 * net stack.
571ecf67 1288 */
76ee65bf 1289 xmit_skb = skb;
571ecf67
JB
1290 skb = NULL;
1291 }
571ecf67
JB
1292 }
1293 }
1294
33b64eb2 1295 /* Mesh forwarding */
902acc78 1296 if (ieee80211_vif_is_mesh(&sdata->vif)) {
33b64eb2
LCC
1297 u8 *mesh_ttl = &((struct ieee80211s_hdr *)skb->cb)->ttl;
1298 (*mesh_ttl)--;
1299
1300 if (is_multicast_ether_addr(skb->data)) {
1301 if (*mesh_ttl > 0) {
1302 xmit_skb = skb_copy(skb, GFP_ATOMIC);
1303 if (!xmit_skb && net_ratelimit())
1304 printk(KERN_DEBUG "%s: failed to clone "
1305 "multicast frame\n", dev->name);
1306 else
1307 xmit_skb->pkt_type = PACKET_OTHERHOST;
1308 } else
902acc78
JB
1309 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.sta,
1310 dropped_frames_ttl);
33b64eb2
LCC
1311 } else if (skb->pkt_type != PACKET_OTHERHOST &&
1312 compare_ether_addr(dev->dev_addr, skb->data) != 0) {
1313 if (*mesh_ttl == 0) {
902acc78
JB
1314 IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.sta,
1315 dropped_frames_ttl);
33b64eb2
LCC
1316 dev_kfree_skb(skb);
1317 skb = NULL;
1318 } else {
1319 xmit_skb = skb;
1320 xmit_skb->pkt_type = PACKET_OTHERHOST;
1321 if (!(dev->flags & IFF_PROMISC))
1322 skb = NULL;
1323 }
1324 }
1325 }
33b64eb2 1326
571ecf67
JB
1327 if (skb) {
1328 /* deliver to local stack */
1329 skb->protocol = eth_type_trans(skb, dev);
1330 memset(skb->cb, 0, sizeof(skb->cb));
1331 netif_rx(skb);
1332 }
1333
76ee65bf 1334 if (xmit_skb) {
571ecf67 1335 /* send to wireless media */
f831e909 1336 xmit_skb->protocol = htons(ETH_P_802_3);
ce3edf6d
JB
1337 skb_reset_network_header(xmit_skb);
1338 skb_reset_mac_header(xmit_skb);
76ee65bf 1339 dev_queue_xmit(xmit_skb);
571ecf67 1340 }
76ee65bf
RR
1341}
1342
9ae54c84 1343static ieee80211_rx_result
5cf121c3 1344ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
fd4c7f2f
RR
1345{
1346 struct net_device *dev = rx->dev;
1347 struct ieee80211_local *local = rx->local;
1348 u16 fc, ethertype;
1349 u8 *payload;
1350 struct sk_buff *skb = rx->skb, *frame = NULL;
1351 const struct ethhdr *eth;
1352 int remaining, err;
1353 u8 dst[ETH_ALEN];
1354 u8 src[ETH_ALEN];
1355 DECLARE_MAC_BUF(mac);
1356
1357 fc = rx->fc;
1358 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
9ae54c84 1359 return RX_CONTINUE;
fd4c7f2f
RR
1360
1361 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
e4c26add 1362 return RX_DROP_MONITOR;
fd4c7f2f 1363
5cf121c3 1364 if (!(rx->flags & IEEE80211_RX_AMSDU))
9ae54c84 1365 return RX_CONTINUE;
fd4c7f2f
RR
1366
1367 err = ieee80211_data_to_8023(rx);
1368 if (unlikely(err))
e4c26add 1369 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1370
1371 skb->dev = dev;
1372
1373 dev->stats.rx_packets++;
1374 dev->stats.rx_bytes += skb->len;
1375
1376 /* skip the wrapping header */
1377 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
1378 if (!eth)
e4c26add 1379 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1380
1381 while (skb != frame) {
1382 u8 padding;
1383 __be16 len = eth->h_proto;
1384 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
1385
1386 remaining = skb->len;
1387 memcpy(dst, eth->h_dest, ETH_ALEN);
1388 memcpy(src, eth->h_source, ETH_ALEN);
1389
1390 padding = ((4 - subframe_len) & 0x3);
1391 /* the last MSDU has no padding */
1392 if (subframe_len > remaining) {
1393 printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
e4c26add 1394 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1395 }
1396
1397 skb_pull(skb, sizeof(struct ethhdr));
1398 /* if last subframe reuse skb */
1399 if (remaining <= subframe_len + padding)
1400 frame = skb;
1401 else {
1402 frame = dev_alloc_skb(local->hw.extra_tx_headroom +
1403 subframe_len);
1404
1405 if (frame == NULL)
e4c26add 1406 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1407
1408 skb_reserve(frame, local->hw.extra_tx_headroom +
1409 sizeof(struct ethhdr));
1410 memcpy(skb_put(frame, ntohs(len)), skb->data,
1411 ntohs(len));
1412
1413 eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
1414 padding);
1415 if (!eth) {
1416 printk(KERN_DEBUG "%s: wrong buffer size ",
1417 dev->name);
1418 dev_kfree_skb(frame);
e4c26add 1419 return RX_DROP_UNUSABLE;
fd4c7f2f
RR
1420 }
1421 }
1422
ce3edf6d 1423 skb_reset_network_header(frame);
fd4c7f2f
RR
1424 frame->dev = dev;
1425 frame->priority = skb->priority;
1426 rx->skb = frame;
1427
fd4c7f2f
RR
1428 payload = frame->data;
1429 ethertype = (payload[6] << 8) | payload[7];
1430
1431 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
ce3edf6d
JB
1432 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1433 compare_ether_addr(payload,
1434 bridge_tunnel_header) == 0)) {
fd4c7f2f
RR
1435 /* remove RFC1042 or Bridge-Tunnel
1436 * encapsulation and replace EtherType */
1437 skb_pull(frame, 6);
1438 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1439 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1440 } else {
ce3edf6d
JB
1441 memcpy(skb_push(frame, sizeof(__be16)),
1442 &len, sizeof(__be16));
fd4c7f2f
RR
1443 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
1444 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
1445 }
1446
ce3edf6d
JB
1447 if (!ieee80211_frame_allowed(rx)) {
1448 if (skb == frame) /* last frame */
e4c26add 1449 return RX_DROP_UNUSABLE;
ce3edf6d
JB
1450 dev_kfree_skb(frame);
1451 continue;
1452 }
fd4c7f2f
RR
1453
1454 ieee80211_deliver_skb(rx);
1455 }
1456
9ae54c84 1457 return RX_QUEUED;
fd4c7f2f
RR
1458}
1459
9ae54c84 1460static ieee80211_rx_result
5cf121c3 1461ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
76ee65bf
RR
1462{
1463 struct net_device *dev = rx->dev;
1464 u16 fc;
ce3edf6d 1465 int err;
76ee65bf
RR
1466
1467 fc = rx->fc;
1468 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
9ae54c84 1469 return RX_CONTINUE;
76ee65bf
RR
1470
1471 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
e4c26add 1472 return RX_DROP_MONITOR;
76ee65bf 1473
76ee65bf
RR
1474 err = ieee80211_data_to_8023(rx);
1475 if (unlikely(err))
e4c26add 1476 return RX_DROP_UNUSABLE;
76ee65bf 1477
ce3edf6d 1478 if (!ieee80211_frame_allowed(rx))
e4c26add 1479 return RX_DROP_MONITOR;
ce3edf6d 1480
76ee65bf
RR
1481 rx->skb->dev = dev;
1482
1483 dev->stats.rx_packets++;
1484 dev->stats.rx_bytes += rx->skb->len;
1485
1486 ieee80211_deliver_skb(rx);
571ecf67 1487
9ae54c84 1488 return RX_QUEUED;
571ecf67
JB
1489}
1490
9ae54c84 1491static ieee80211_rx_result
5cf121c3 1492ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
71364716
RR
1493{
1494 struct ieee80211_local *local = rx->local;
1495 struct ieee80211_hw *hw = &local->hw;
1496 struct sk_buff *skb = rx->skb;
1497 struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
1498 struct tid_ampdu_rx *tid_agg_rx;
1499 u16 start_seq_num;
1500 u16 tid;
1501
1502 if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
9ae54c84 1503 return RX_CONTINUE;
71364716
RR
1504
1505 if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
1506 if (!rx->sta)
9ae54c84 1507 return RX_CONTINUE;
71364716 1508 tid = le16_to_cpu(bar->control) >> 12;
cee24a3e
RR
1509 if (rx->sta->ampdu_mlme.tid_state_rx[tid]
1510 != HT_AGG_STATE_OPERATIONAL)
9ae54c84 1511 return RX_CONTINUE;
cee24a3e 1512 tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
71364716
RR
1513
1514 start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
1515
1516 /* reset session timer */
1517 if (tid_agg_rx->timeout) {
1518 unsigned long expires =
1519 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
1520 mod_timer(&tid_agg_rx->session_timer, expires);
1521 }
1522
1523 /* manage reordering buffer according to requested */
1524 /* sequence number */
1525 rcu_read_lock();
1526 ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
1527 start_seq_num, 1);
1528 rcu_read_unlock();
e4c26add 1529 return RX_DROP_UNUSABLE;
71364716
RR
1530 }
1531
9ae54c84 1532 return RX_CONTINUE;
71364716
RR
1533}
1534
9ae54c84 1535static ieee80211_rx_result
5cf121c3 1536ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
571ecf67
JB
1537{
1538 struct ieee80211_sub_if_data *sdata;
1539
5cf121c3 1540 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
e4c26add 1541 return RX_DROP_MONITOR;
571ecf67
JB
1542
1543 sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
51fb61e7 1544 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
33b64eb2
LCC
1545 sdata->vif.type == IEEE80211_IF_TYPE_IBSS ||
1546 sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT) &&
ddd3d2be 1547 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
5cf121c3 1548 ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->status);
f9d540ee 1549 else
e4c26add 1550 return RX_DROP_MONITOR;
f9d540ee 1551
9ae54c84 1552 return RX_QUEUED;
571ecf67
JB
1553}
1554
571ecf67
JB
1555static void ieee80211_rx_michael_mic_report(struct net_device *dev,
1556 struct ieee80211_hdr *hdr,
5cf121c3 1557 struct ieee80211_rx_data *rx)
571ecf67
JB
1558{
1559 int keyidx, hdrlen;
0795af57
JP
1560 DECLARE_MAC_BUF(mac);
1561 DECLARE_MAC_BUF(mac2);
571ecf67
JB
1562
1563 hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
1564 if (rx->skb->len >= hdrlen + 4)
1565 keyidx = rx->skb->data[hdrlen + 3] >> 6;
1566 else
1567 keyidx = -1;
1568
1a84f3fd
JB
1569 if (net_ratelimit())
1570 printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
0795af57
JP
1571 "failure from %s to %s keyidx=%d\n",
1572 dev->name, print_mac(mac, hdr->addr2),
1573 print_mac(mac2, hdr->addr1), keyidx);
571ecf67 1574
8944b79f 1575 if (!rx->sta) {
aa0daf0e
JB
1576 /*
1577 * Some hardware seem to generate incorrect Michael MIC
1578 * reports; ignore them to avoid triggering countermeasures.
1579 */
1a84f3fd
JB
1580 if (net_ratelimit())
1581 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
0795af57
JP
1582 "error for unknown address %s\n",
1583 dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1584 goto ignore;
1585 }
1586
1587 if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
1a84f3fd
JB
1588 if (net_ratelimit())
1589 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
aa0daf0e 1590 "error for a frame with no PROTECTED flag (src "
0795af57 1591 "%s)\n", dev->name, print_mac(mac, hdr->addr2));
571ecf67
JB
1592 goto ignore;
1593 }
1594
51fb61e7 1595 if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
aa0daf0e
JB
1596 /*
1597 * APs with pairwise keys should never receive Michael MIC
1598 * errors for non-zero keyidx because these are reserved for
1599 * group keys and only the AP is sending real multicast
1600 * frames in the BSS.
1601 */
eb063c17
JB
1602 if (net_ratelimit())
1603 printk(KERN_DEBUG "%s: ignored Michael MIC error for "
1604 "a frame with non-zero keyidx (%d)"
0795af57
JP
1605 " (src %s)\n", dev->name, keyidx,
1606 print_mac(mac, hdr->addr2));
eb063c17 1607 goto ignore;
571ecf67
JB
1608 }
1609
1610 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
1611 ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
1612 (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
1a84f3fd
JB
1613 if (net_ratelimit())
1614 printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
1615 "error for a frame that cannot be encrypted "
0795af57
JP
1616 "(fc=0x%04x) (src %s)\n",
1617 dev->name, rx->fc, print_mac(mac, hdr->addr2));
571ecf67
JB
1618 goto ignore;
1619 }
1620
eb063c17 1621 mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
571ecf67
JB
1622 ignore:
1623 dev_kfree_skb(rx->skb);
1624 rx->skb = NULL;
1625}
1626
5cf121c3
JB
1627/* TODO: use IEEE80211_RX_FRAGMENTED */
1628static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx)
3d30d949
MW
1629{
1630 struct ieee80211_sub_if_data *sdata;
1631 struct ieee80211_local *local = rx->local;
1632 struct ieee80211_rtap_hdr {
1633 struct ieee80211_radiotap_header hdr;
1634 u8 flags;
1635 u8 rate;
1636 __le16 chan_freq;
1637 __le16 chan_flags;
1638 } __attribute__ ((packed)) *rthdr;
1639 struct sk_buff *skb = rx->skb, *skb2;
1640 struct net_device *prev_dev = NULL;
5cf121c3 1641 struct ieee80211_rx_status *status = rx->status;
3d30d949 1642
5cf121c3 1643 if (rx->flags & IEEE80211_RX_CMNTR_REPORTED)
3d30d949
MW
1644 goto out_free_skb;
1645
1646 if (skb_headroom(skb) < sizeof(*rthdr) &&
1647 pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
1648 goto out_free_skb;
1649
1650 rthdr = (void *)skb_push(skb, sizeof(*rthdr));
1651 memset(rthdr, 0, sizeof(*rthdr));
1652 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1653 rthdr->hdr.it_present =
1654 cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1655 (1 << IEEE80211_RADIOTAP_RATE) |
1656 (1 << IEEE80211_RADIOTAP_CHANNEL));
1657
5cf121c3 1658 rthdr->rate = rx->rate->bitrate / 5;
3d30d949
MW
1659 rthdr->chan_freq = cpu_to_le16(status->freq);
1660
1661 if (status->band == IEEE80211_BAND_5GHZ)
1662 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
1663 IEEE80211_CHAN_5GHZ);
1664 else
1665 rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
1666 IEEE80211_CHAN_2GHZ);
1667
1668 skb_set_mac_header(skb, 0);
1669 skb->ip_summed = CHECKSUM_UNNECESSARY;
1670 skb->pkt_type = PACKET_OTHERHOST;
1671 skb->protocol = htons(ETH_P_802_2);
1672
1673 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1674 if (!netif_running(sdata->dev))
1675 continue;
1676
1677 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR ||
1678 !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
1679 continue;
1680
1681 if (prev_dev) {
1682 skb2 = skb_clone(skb, GFP_ATOMIC);
1683 if (skb2) {
1684 skb2->dev = prev_dev;
1685 netif_rx(skb2);
1686 }
1687 }
1688
1689 prev_dev = sdata->dev;
1690 sdata->dev->stats.rx_packets++;
1691 sdata->dev->stats.rx_bytes += skb->len;
1692 }
1693
1694 if (prev_dev) {
1695 skb->dev = prev_dev;
1696 netif_rx(skb);
1697 skb = NULL;
1698 } else
1699 goto out_free_skb;
1700
5cf121c3 1701 rx->flags |= IEEE80211_RX_CMNTR_REPORTED;
3d30d949
MW
1702 return;
1703
1704 out_free_skb:
1705 dev_kfree_skb(skb);
1706}
1707
5cf121c3 1708typedef ieee80211_rx_result (*ieee80211_rx_handler)(struct ieee80211_rx_data *);
58905290 1709static ieee80211_rx_handler ieee80211_rx_handlers[] =
571ecf67
JB
1710{
1711 ieee80211_rx_h_if_stats,
571ecf67
JB
1712 ieee80211_rx_h_passive_scan,
1713 ieee80211_rx_h_check,
4f0d18e2 1714 ieee80211_rx_h_decrypt,
70f08765 1715 ieee80211_rx_h_sta_process,
571ecf67
JB
1716 ieee80211_rx_h_defragment,
1717 ieee80211_rx_h_ps_poll,
1718 ieee80211_rx_h_michael_mic_verify,
1719 /* this must be after decryption - so header is counted in MPDU mic
1720 * must be before pae and data, so QOS_DATA format frames
1721 * are not passed to user space by these functions
1722 */
1723 ieee80211_rx_h_remove_qos_control,
fd4c7f2f 1724 ieee80211_rx_h_amsdu,
571ecf67 1725 ieee80211_rx_h_data,
71364716 1726 ieee80211_rx_h_ctrl,
571ecf67
JB
1727 ieee80211_rx_h_mgmt,
1728 NULL
1729};
1730
8944b79f 1731static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
5cf121c3 1732 struct ieee80211_rx_data *rx,
8944b79f 1733 struct sk_buff *skb)
58905290
JB
1734{
1735 ieee80211_rx_handler *handler;
1736 ieee80211_rx_result res = RX_DROP_MONITOR;
1737
8944b79f
JB
1738 rx->skb = skb;
1739 rx->sdata = sdata;
1740 rx->dev = sdata->dev;
1741
58905290
JB
1742 for (handler = ieee80211_rx_handlers; *handler != NULL; handler++) {
1743 res = (*handler)(rx);
1744
1745 switch (res) {
1746 case RX_CONTINUE:
1747 continue;
1748 case RX_DROP_UNUSABLE:
1749 case RX_DROP_MONITOR:
8944b79f
JB
1750 I802_DEBUG_INC(sdata->local->rx_handlers_drop);
1751 if (rx->sta)
1752 rx->sta->rx_dropped++;
58905290
JB
1753 break;
1754 case RX_QUEUED:
8944b79f 1755 I802_DEBUG_INC(sdata->local->rx_handlers_queued);
58905290
JB
1756 break;
1757 }
1758 break;
1759 }
1760
1761 switch (res) {
3d30d949 1762 case RX_CONTINUE:
58905290 1763 case RX_DROP_MONITOR:
3d30d949
MW
1764 ieee80211_rx_cooked_monitor(rx);
1765 break;
58905290 1766 case RX_DROP_UNUSABLE:
58905290
JB
1767 dev_kfree_skb(rx->skb);
1768 break;
1769 }
1770}
1771
571ecf67
JB
1772/* main receive path */
1773
23a24def 1774static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
5cf121c3 1775 u8 *bssid, struct ieee80211_rx_data *rx,
23a24def
JB
1776 struct ieee80211_hdr *hdr)
1777{
1778 int multicast = is_multicast_ether_addr(hdr->addr1);
1779
51fb61e7 1780 switch (sdata->vif.type) {
23a24def
JB
1781 case IEEE80211_IF_TYPE_STA:
1782 if (!bssid)
1783 return 0;
1784 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
5cf121c3 1785 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
23a24def 1786 return 0;
5cf121c3 1787 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def
JB
1788 } else if (!multicast &&
1789 compare_ether_addr(sdata->dev->dev_addr,
1790 hdr->addr1) != 0) {
4150c572 1791 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1792 return 0;
5cf121c3 1793 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def
JB
1794 }
1795 break;
1796 case IEEE80211_IF_TYPE_IBSS:
1797 if (!bssid)
1798 return 0;
9d9bf77d
BR
1799 if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
1800 (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
1801 return 1;
1802 else if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
5cf121c3 1803 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
23a24def 1804 return 0;
5cf121c3 1805 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def
JB
1806 } else if (!multicast &&
1807 compare_ether_addr(sdata->dev->dev_addr,
1808 hdr->addr1) != 0) {
4150c572 1809 if (!(sdata->dev->flags & IFF_PROMISC))
23a24def 1810 return 0;
5cf121c3 1811 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def
JB
1812 } else if (!rx->sta)
1813 rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
1814 bssid, hdr->addr2);
1815 break;
6032f934
JB
1816 case IEEE80211_IF_TYPE_MESH_POINT:
1817 if (!multicast &&
1818 compare_ether_addr(sdata->dev->dev_addr,
1819 hdr->addr1) != 0) {
1820 if (!(sdata->dev->flags & IFF_PROMISC))
1821 return 0;
1822
5cf121c3 1823 rx->flags &= ~IEEE80211_RX_RA_MATCH;
6032f934
JB
1824 }
1825 break;
fb1c1cd6 1826 case IEEE80211_IF_TYPE_VLAN:
23a24def
JB
1827 case IEEE80211_IF_TYPE_AP:
1828 if (!bssid) {
1829 if (compare_ether_addr(sdata->dev->dev_addr,
1830 hdr->addr1))
1831 return 0;
1832 } else if (!ieee80211_bssid_match(bssid,
1833 sdata->dev->dev_addr)) {
5cf121c3 1834 if (!(rx->flags & IEEE80211_RX_IN_SCAN))
23a24def 1835 return 0;
5cf121c3 1836 rx->flags &= ~IEEE80211_RX_RA_MATCH;
23a24def 1837 }
badffb72 1838 if (sdata->dev == sdata->local->mdev &&
5cf121c3 1839 !(rx->flags & IEEE80211_RX_IN_SCAN))
23a24def
JB
1840 /* do not receive anything via
1841 * master device when not scanning */
1842 return 0;
1843 break;
1844 case IEEE80211_IF_TYPE_WDS:
1845 if (bssid ||
1846 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
1847 return 0;
1848 if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
1849 return 0;
1850 break;
fb1c1cd6
JB
1851 case IEEE80211_IF_TYPE_MNTR:
1852 /* take everything */
1853 break;
a2897552 1854 case IEEE80211_IF_TYPE_INVALID:
fb1c1cd6
JB
1855 /* should never get here */
1856 WARN_ON(1);
1857 break;
23a24def
JB
1858 }
1859
1860 return 1;
1861}
1862
571ecf67 1863/*
6368e4b1
RR
1864 * This is the actual Rx frames handler. as it blongs to Rx path it must
1865 * be called with rcu_read_lock protection.
571ecf67 1866 */
71ebb4aa
RR
1867static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
1868 struct sk_buff *skb,
1869 struct ieee80211_rx_status *status,
8318d78a
JB
1870 u32 load,
1871 struct ieee80211_rate *rate)
571ecf67
JB
1872{
1873 struct ieee80211_local *local = hw_to_local(hw);
1874 struct ieee80211_sub_if_data *sdata;
571ecf67 1875 struct ieee80211_hdr *hdr;
5cf121c3 1876 struct ieee80211_rx_data rx;
571ecf67 1877 u16 type;
6368e4b1 1878 int prepares;
8e6f0032
JB
1879 struct ieee80211_sub_if_data *prev = NULL;
1880 struct sk_buff *skb_new;
1881 u8 *bssid;
571ecf67
JB
1882
1883 hdr = (struct ieee80211_hdr *) skb->data;
1884 memset(&rx, 0, sizeof(rx));
1885 rx.skb = skb;
1886 rx.local = local;
1887
5cf121c3
JB
1888 rx.status = status;
1889 rx.load = load;
1890 rx.rate = rate;
b2e7771e 1891 rx.fc = le16_to_cpu(hdr->frame_control);
571ecf67 1892 type = rx.fc & IEEE80211_FCTL_FTYPE;
72abd81b 1893
b2e7771e 1894 if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
571ecf67 1895 local->dot11ReceivedFragmentCount++;
571ecf67 1896
8944b79f
JB
1897 rx.sta = sta_info_get(local, hdr->addr2);
1898 if (rx.sta) {
d0709a65
JB
1899 rx.sdata = rx.sta->sdata;
1900 rx.dev = rx.sta->sdata->dev;
b2e7771e 1901 }
571ecf67 1902
571ecf67 1903 if ((status->flag & RX_FLAG_MMIC_ERROR)) {
8944b79f 1904 ieee80211_rx_michael_mic_report(local->mdev, hdr, &rx);
d0709a65 1905 return;
571ecf67
JB
1906 }
1907
ece8eddd 1908 if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
5cf121c3 1909 rx.flags |= IEEE80211_RX_IN_SCAN;
571ecf67 1910
38f3714d
JB
1911 ieee80211_parse_qos(&rx);
1912 ieee80211_verify_ip_alignment(&rx);
1913
571ecf67
JB
1914 skb = rx.skb;
1915
79010420 1916 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2a8a9a88
JB
1917 if (!netif_running(sdata->dev))
1918 continue;
1919
51fb61e7 1920 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
b2e7771e
JB
1921 continue;
1922
51fb61e7 1923 bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
5cf121c3 1924 rx.flags |= IEEE80211_RX_RA_MATCH;
6368e4b1 1925 prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
23a24def 1926
6368e4b1 1927 if (!prepares)
23a24def 1928 continue;
8e6f0032 1929
340e11f3
JB
1930 /*
1931 * frame is destined for this interface, but if it's not
1932 * also for the previous one we handle that after the
1933 * loop to avoid copying the SKB once too much
1934 */
1935
1936 if (!prev) {
1937 prev = sdata;
1938 continue;
8e6f0032 1939 }
340e11f3
JB
1940
1941 /*
1942 * frame was destined for the previous interface
1943 * so invoke RX handlers for it
1944 */
1945
1946 skb_new = skb_copy(skb, GFP_ATOMIC);
1947 if (!skb_new) {
1948 if (net_ratelimit())
1949 printk(KERN_DEBUG "%s: failed to copy "
1950 "multicast frame for %s",
dd1cd4c6
JB
1951 wiphy_name(local->hw.wiphy),
1952 prev->dev->name);
340e11f3
JB
1953 continue;
1954 }
69f817b6 1955 rx.fc = le16_to_cpu(hdr->frame_control);
8944b79f 1956 ieee80211_invoke_rx_handlers(prev, &rx, skb_new);
8e6f0032 1957 prev = sdata;
571ecf67 1958 }
8e6f0032 1959 if (prev) {
69f817b6 1960 rx.fc = le16_to_cpu(hdr->frame_control);
8944b79f 1961 ieee80211_invoke_rx_handlers(prev, &rx, skb);
8e6f0032
JB
1962 } else
1963 dev_kfree_skb(skb);
571ecf67 1964}
6368e4b1 1965
b580781e
RR
1966#define SEQ_MODULO 0x1000
1967#define SEQ_MASK 0xfff
1968
1969static inline int seq_less(u16 sq1, u16 sq2)
1970{
1971 return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
1972}
1973
1974static inline u16 seq_inc(u16 sq)
1975{
1976 return ((sq + 1) & SEQ_MASK);
1977}
1978
1979static inline u16 seq_sub(u16 sq1, u16 sq2)
1980{
1981 return ((sq1 - sq2) & SEQ_MASK);
1982}
1983
1984
71364716
RR
1985/*
1986 * As it function blongs to Rx path it must be called with
1987 * the proper rcu_read_lock protection for its flow.
1988 */
b580781e
RR
1989u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
1990 struct tid_ampdu_rx *tid_agg_rx,
1991 struct sk_buff *skb, u16 mpdu_seq_num,
1992 int bar_req)
1993{
1994 struct ieee80211_local *local = hw_to_local(hw);
1995 struct ieee80211_rx_status status;
1996 u16 head_seq_num, buf_size;
1997 int index;
1998 u32 pkt_load;
8318d78a
JB
1999 struct ieee80211_supported_band *sband;
2000 struct ieee80211_rate *rate;
b580781e
RR
2001
2002 buf_size = tid_agg_rx->buf_size;
2003 head_seq_num = tid_agg_rx->head_seq_num;
2004
2005 /* frame with out of date sequence number */
2006 if (seq_less(mpdu_seq_num, head_seq_num)) {
2007 dev_kfree_skb(skb);
2008 return 1;
2009 }
2010
2011 /* if frame sequence number exceeds our buffering window size or
2012 * block Ack Request arrived - release stored frames */
2013 if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
2014 /* new head to the ordering buffer */
2015 if (bar_req)
2016 head_seq_num = mpdu_seq_num;
2017 else
2018 head_seq_num =
2019 seq_inc(seq_sub(mpdu_seq_num, buf_size));
2020 /* release stored frames up to new head to stack */
2021 while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
2022 index = seq_sub(tid_agg_rx->head_seq_num,
2023 tid_agg_rx->ssn)
2024 % tid_agg_rx->buf_size;
2025
2026 if (tid_agg_rx->reorder_buf[index]) {
2027 /* release the reordered frames to stack */
2028 memcpy(&status,
2029 tid_agg_rx->reorder_buf[index]->cb,
2030 sizeof(status));
8318d78a
JB
2031 sband = local->hw.wiphy->bands[status.band];
2032 rate = &sband->bitrates[status.rate_idx];
b580781e
RR
2033 pkt_load = ieee80211_rx_load_stats(local,
2034 tid_agg_rx->reorder_buf[index],
8318d78a 2035 &status, rate);
b580781e
RR
2036 __ieee80211_rx_handle_packet(hw,
2037 tid_agg_rx->reorder_buf[index],
8318d78a 2038 &status, pkt_load, rate);
b580781e
RR
2039 tid_agg_rx->stored_mpdu_num--;
2040 tid_agg_rx->reorder_buf[index] = NULL;
2041 }
2042 tid_agg_rx->head_seq_num =
2043 seq_inc(tid_agg_rx->head_seq_num);
2044 }
2045 if (bar_req)
2046 return 1;
2047 }
2048
2049 /* now the new frame is always in the range of the reordering */
2050 /* buffer window */
2051 index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
2052 % tid_agg_rx->buf_size;
2053 /* check if we already stored this frame */
2054 if (tid_agg_rx->reorder_buf[index]) {
2055 dev_kfree_skb(skb);
2056 return 1;
2057 }
2058
2059 /* if arrived mpdu is in the right order and nothing else stored */
2060 /* release it immediately */
2061 if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
2062 tid_agg_rx->stored_mpdu_num == 0) {
2063 tid_agg_rx->head_seq_num =
2064 seq_inc(tid_agg_rx->head_seq_num);
2065 return 0;
2066 }
2067
2068 /* put the frame in the reordering buffer */
2069 tid_agg_rx->reorder_buf[index] = skb;
2070 tid_agg_rx->stored_mpdu_num++;
2071 /* release the buffer until next missing frame */
2072 index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
2073 % tid_agg_rx->buf_size;
2074 while (tid_agg_rx->reorder_buf[index]) {
2075 /* release the reordered frame back to stack */
2076 memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
2077 sizeof(status));
8318d78a
JB
2078 sband = local->hw.wiphy->bands[status.band];
2079 rate = &sband->bitrates[status.rate_idx];
b580781e
RR
2080 pkt_load = ieee80211_rx_load_stats(local,
2081 tid_agg_rx->reorder_buf[index],
8318d78a 2082 &status, rate);
b580781e 2083 __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
8318d78a 2084 &status, pkt_load, rate);
b580781e
RR
2085 tid_agg_rx->stored_mpdu_num--;
2086 tid_agg_rx->reorder_buf[index] = NULL;
2087 tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
2088 index = seq_sub(tid_agg_rx->head_seq_num,
2089 tid_agg_rx->ssn) % tid_agg_rx->buf_size;
2090 }
2091 return 1;
2092}
2093
71ebb4aa
RR
2094static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
2095 struct sk_buff *skb)
b580781e
RR
2096{
2097 struct ieee80211_hw *hw = &local->hw;
2098 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2099 struct sta_info *sta;
2100 struct tid_ampdu_rx *tid_agg_rx;
2101 u16 fc, sc;
2102 u16 mpdu_seq_num;
2103 u8 ret = 0, *qc;
2104 int tid;
2105
2106 sta = sta_info_get(local, hdr->addr2);
2107 if (!sta)
2108 return ret;
2109
2110 fc = le16_to_cpu(hdr->frame_control);
2111
2112 /* filter the QoS data rx stream according to
2113 * STA/TID and check if this STA/TID is on aggregation */
2114 if (!WLAN_FC_IS_QOS_DATA(fc))
2115 goto end_reorder;
2116
2117 qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
2118 tid = qc[0] & QOS_CONTROL_TID_MASK;
b580781e 2119
cee24a3e 2120 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL)
b580781e
RR
2121 goto end_reorder;
2122
cee24a3e
RR
2123 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
2124
b580781e 2125 /* null data frames are excluded */
8b6bbe75 2126 if (unlikely(fc & IEEE80211_STYPE_NULLFUNC))
b580781e
RR
2127 goto end_reorder;
2128
2129 /* new un-ordered ampdu frame - process it */
2130
2131 /* reset session timer */
2132 if (tid_agg_rx->timeout) {
2133 unsigned long expires =
2134 jiffies + (tid_agg_rx->timeout / 1000) * HZ;
2135 mod_timer(&tid_agg_rx->session_timer, expires);
2136 }
2137
2138 /* if this mpdu is fragmented - terminate rx aggregation session */
2139 sc = le16_to_cpu(hdr->seq_ctrl);
2140 if (sc & IEEE80211_SCTL_FRAG) {
d0709a65 2141 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
b580781e
RR
2142 tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
2143 ret = 1;
2144 goto end_reorder;
2145 }
2146
2147 /* according to mpdu sequence number deal with reordering buffer */
2148 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
2149 ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
2150 mpdu_seq_num, 0);
d0709a65 2151 end_reorder:
b580781e
RR
2152 return ret;
2153}
2154
6368e4b1
RR
2155/*
2156 * This is the receive path handler. It is called by a low level driver when an
2157 * 802.11 MPDU is received from the hardware.
2158 */
2159void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
2160 struct ieee80211_rx_status *status)
2161{
2162 struct ieee80211_local *local = hw_to_local(hw);
2163 u32 pkt_load;
8318d78a
JB
2164 struct ieee80211_rate *rate = NULL;
2165 struct ieee80211_supported_band *sband;
2166
2167 if (status->band < 0 ||
13d8fd2d 2168 status->band >= IEEE80211_NUM_BANDS) {
8318d78a
JB
2169 WARN_ON(1);
2170 return;
2171 }
2172
2173 sband = local->hw.wiphy->bands[status->band];
2174
2175 if (!sband ||
2176 status->rate_idx < 0 ||
2177 status->rate_idx >= sband->n_bitrates) {
2178 WARN_ON(1);
2179 return;
2180 }
2181
2182 rate = &sband->bitrates[status->rate_idx];
6368e4b1
RR
2183
2184 /*
2185 * key references and virtual interfaces are protected using RCU
2186 * and this requires that we are in a read-side RCU section during
2187 * receive processing
2188 */
2189 rcu_read_lock();
2190
2191 /*
2192 * Frames with failed FCS/PLCP checksum are not returned,
2193 * all other frames are returned without radiotap header
2194 * if it was previously present.
2195 * Also, frames with less than 16 bytes are dropped.
2196 */
8318d78a 2197 skb = ieee80211_rx_monitor(local, skb, status, rate);
6368e4b1
RR
2198 if (!skb) {
2199 rcu_read_unlock();
2200 return;
2201 }
2202
8318d78a 2203 pkt_load = ieee80211_rx_load_stats(local, skb, status, rate);
b580781e 2204 local->channel_use_raw += pkt_load;
6368e4b1 2205
b580781e 2206 if (!ieee80211_rx_reorder_ampdu(local, skb))
8318d78a 2207 __ieee80211_rx_handle_packet(hw, skb, status, pkt_load, rate);
6368e4b1
RR
2208
2209 rcu_read_unlock();
2210}
571ecf67
JB
2211EXPORT_SYMBOL(__ieee80211_rx);
2212
2213/* This is a version of the rx handler that can be called from hard irq
2214 * context. Post the skb on the queue and schedule the tasklet */
2215void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
2216 struct ieee80211_rx_status *status)
2217{
2218 struct ieee80211_local *local = hw_to_local(hw);
2219
2220 BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
2221
2222 skb->dev = local->mdev;
2223 /* copy status into skb->cb for use by tasklet */
2224 memcpy(skb->cb, status, sizeof(*status));
2225 skb->pkt_type = IEEE80211_RX_MSG;
2226 skb_queue_tail(&local->skb_queue, skb);
2227 tasklet_schedule(&local->tasklet);
2228}
2229EXPORT_SYMBOL(ieee80211_rx_irqsafe);