Commit | Line | Data |
---|---|---|
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 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/skbuff.h> | |
14 | #include <linux/netdevice.h> | |
15 | #include <linux/etherdevice.h> | |
d4e46a3d | 16 | #include <linux/rcupdate.h> |
571ecf67 JB |
17 | #include <net/mac80211.h> |
18 | #include <net/ieee80211_radiotap.h> | |
19 | ||
20 | #include "ieee80211_i.h" | |
21 | #include "ieee80211_led.h" | |
571ecf67 JB |
22 | #include "wep.h" |
23 | #include "wpa.h" | |
24 | #include "tkip.h" | |
25 | #include "wme.h" | |
26 | ||
b2e7771e JB |
27 | /* |
28 | * monitor mode reception | |
29 | * | |
30 | * This function cleans up the SKB, i.e. it removes all the stuff | |
31 | * only useful for monitoring. | |
32 | */ | |
33 | static struct sk_buff *remove_monitor_info(struct ieee80211_local *local, | |
34 | struct sk_buff *skb, | |
35 | int rtap_len) | |
36 | { | |
37 | skb_pull(skb, rtap_len); | |
38 | ||
39 | if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) { | |
40 | if (likely(skb->len > FCS_LEN)) | |
41 | skb_trim(skb, skb->len - FCS_LEN); | |
42 | else { | |
43 | /* driver bug */ | |
44 | WARN_ON(1); | |
45 | dev_kfree_skb(skb); | |
46 | skb = NULL; | |
47 | } | |
48 | } | |
49 | ||
50 | return skb; | |
51 | } | |
52 | ||
53 | static inline int should_drop_frame(struct ieee80211_rx_status *status, | |
54 | struct sk_buff *skb, | |
55 | int present_fcs_len, | |
56 | int radiotap_len) | |
57 | { | |
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 + radiotap_len)) | |
63 | return 1; | |
98f0b0a3 RR |
64 | if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) == |
65 | cpu_to_le16(IEEE80211_FTYPE_CTL)) && | |
66 | ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) != | |
67 | cpu_to_le16(IEEE80211_STYPE_PSPOLL))) | |
b2e7771e JB |
68 | return 1; |
69 | return 0; | |
70 | } | |
71 | ||
72 | /* | |
73 | * This function copies a received frame to all monitor interfaces and | |
74 | * returns a cleaned-up SKB that no longer includes the FCS nor the | |
75 | * radiotap header the driver might have added. | |
76 | */ | |
77 | static struct sk_buff * | |
78 | ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb, | |
79 | struct ieee80211_rx_status *status) | |
80 | { | |
81 | struct ieee80211_sub_if_data *sdata; | |
82 | struct ieee80211_rate *rate; | |
83 | int needed_headroom = 0; | |
c49e5ea3 JB |
84 | struct ieee80211_radiotap_header *rthdr; |
85 | __le64 *rttsft = NULL; | |
86 | struct ieee80211_rtap_fixed_data { | |
b2e7771e JB |
87 | u8 flags; |
88 | u8 rate; | |
89 | __le16 chan_freq; | |
90 | __le16 chan_flags; | |
91 | u8 antsignal; | |
92 | u8 padding_for_rxflags; | |
93 | __le16 rx_flags; | |
c49e5ea3 | 94 | } __attribute__ ((packed)) *rtfixed; |
b2e7771e JB |
95 | struct sk_buff *skb, *skb2; |
96 | struct net_device *prev_dev = NULL; | |
97 | int present_fcs_len = 0; | |
98 | int rtap_len = 0; | |
99 | ||
100 | /* | |
101 | * First, we may need to make a copy of the skb because | |
102 | * (1) we need to modify it for radiotap (if not present), and | |
103 | * (2) the other RX handlers will modify the skb we got. | |
104 | * | |
105 | * We don't need to, of course, if we aren't going to return | |
106 | * the SKB because it has a bad FCS/PLCP checksum. | |
107 | */ | |
108 | if (status->flag & RX_FLAG_RADIOTAP) | |
109 | rtap_len = ieee80211_get_radiotap_len(origskb->data); | |
110 | else | |
c49e5ea3 JB |
111 | /* room for radiotap header, always present fields and TSFT */ |
112 | needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8; | |
b2e7771e JB |
113 | |
114 | if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) | |
115 | present_fcs_len = FCS_LEN; | |
116 | ||
117 | if (!local->monitors) { | |
118 | if (should_drop_frame(status, origskb, present_fcs_len, | |
119 | rtap_len)) { | |
120 | dev_kfree_skb(origskb); | |
121 | return NULL; | |
122 | } | |
123 | ||
124 | return remove_monitor_info(local, origskb, rtap_len); | |
125 | } | |
126 | ||
127 | if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) { | |
128 | /* only need to expand headroom if necessary */ | |
129 | skb = origskb; | |
130 | origskb = NULL; | |
131 | ||
132 | /* | |
133 | * This shouldn't trigger often because most devices have an | |
134 | * RX header they pull before we get here, and that should | |
135 | * be big enough for our radiotap information. We should | |
136 | * probably export the length to drivers so that we can have | |
137 | * them allocate enough headroom to start with. | |
138 | */ | |
139 | if (skb_headroom(skb) < needed_headroom && | |
c49e5ea3 | 140 | pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) { |
b2e7771e JB |
141 | dev_kfree_skb(skb); |
142 | return NULL; | |
143 | } | |
144 | } else { | |
145 | /* | |
146 | * Need to make a copy and possibly remove radiotap header | |
147 | * and FCS from the original. | |
148 | */ | |
149 | skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC); | |
150 | ||
151 | origskb = remove_monitor_info(local, origskb, rtap_len); | |
152 | ||
153 | if (!skb) | |
154 | return origskb; | |
155 | } | |
156 | ||
157 | /* if necessary, prepend radiotap information */ | |
158 | if (!(status->flag & RX_FLAG_RADIOTAP)) { | |
c49e5ea3 JB |
159 | rtfixed = (void *) skb_push(skb, sizeof(*rtfixed)); |
160 | rtap_len = sizeof(*rthdr) + sizeof(*rtfixed); | |
161 | if (status->flag & RX_FLAG_TSFT) { | |
162 | rttsft = (void *) skb_push(skb, sizeof(*rttsft)); | |
163 | rtap_len += 8; | |
164 | } | |
b2e7771e JB |
165 | rthdr = (void *) skb_push(skb, sizeof(*rthdr)); |
166 | memset(rthdr, 0, sizeof(*rthdr)); | |
c49e5ea3 JB |
167 | memset(rtfixed, 0, sizeof(*rtfixed)); |
168 | rthdr->it_present = | |
b2e7771e JB |
169 | cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) | |
170 | (1 << IEEE80211_RADIOTAP_RATE) | | |
171 | (1 << IEEE80211_RADIOTAP_CHANNEL) | | |
172 | (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) | | |
173 | (1 << IEEE80211_RADIOTAP_RX_FLAGS)); | |
c49e5ea3 JB |
174 | rtfixed->flags = 0; |
175 | if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) | |
176 | rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS; | |
177 | ||
178 | if (rttsft) { | |
179 | *rttsft = cpu_to_le64(status->mactime); | |
180 | rthdr->it_present |= | |
181 | cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT); | |
182 | } | |
b2e7771e JB |
183 | |
184 | /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */ | |
c49e5ea3 | 185 | rtfixed->rx_flags = 0; |
b2e7771e JB |
186 | if (status->flag & |
187 | (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC)) | |
c49e5ea3 | 188 | rtfixed->rx_flags |= |
b2e7771e JB |
189 | cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS); |
190 | ||
191 | rate = ieee80211_get_rate(local, status->phymode, | |
192 | status->rate); | |
193 | if (rate) | |
c49e5ea3 | 194 | rtfixed->rate = rate->rate / 5; |
b2e7771e | 195 | |
c49e5ea3 | 196 | rtfixed->chan_freq = cpu_to_le16(status->freq); |
b2e7771e JB |
197 | |
198 | if (status->phymode == MODE_IEEE80211A) | |
c49e5ea3 | 199 | rtfixed->chan_flags = |
b2e7771e JB |
200 | cpu_to_le16(IEEE80211_CHAN_OFDM | |
201 | IEEE80211_CHAN_5GHZ); | |
202 | else | |
c49e5ea3 | 203 | rtfixed->chan_flags = |
b2e7771e JB |
204 | cpu_to_le16(IEEE80211_CHAN_DYN | |
205 | IEEE80211_CHAN_2GHZ); | |
206 | ||
c49e5ea3 JB |
207 | rtfixed->antsignal = status->ssi; |
208 | rthdr->it_len = cpu_to_le16(rtap_len); | |
b2e7771e JB |
209 | } |
210 | ||
211 | skb_set_mac_header(skb, 0); | |
212 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
213 | skb->pkt_type = PACKET_OTHERHOST; | |
214 | skb->protocol = htons(ETH_P_802_2); | |
215 | ||
216 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { | |
217 | if (!netif_running(sdata->dev)) | |
218 | continue; | |
219 | ||
220 | if (sdata->type != IEEE80211_IF_TYPE_MNTR) | |
221 | continue; | |
222 | ||
223 | if (prev_dev) { | |
224 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
225 | if (skb2) { | |
226 | skb2->dev = prev_dev; | |
227 | netif_rx(skb2); | |
228 | } | |
229 | } | |
230 | ||
231 | prev_dev = sdata->dev; | |
232 | sdata->dev->stats.rx_packets++; | |
233 | sdata->dev->stats.rx_bytes += skb->len; | |
234 | } | |
235 | ||
236 | if (prev_dev) { | |
237 | skb->dev = prev_dev; | |
238 | netif_rx(skb); | |
239 | } else | |
240 | dev_kfree_skb(skb); | |
241 | ||
242 | return origskb; | |
243 | } | |
244 | ||
245 | ||
571ecf67 JB |
246 | /* pre-rx handlers |
247 | * | |
248 | * these don't have dev/sdata fields in the rx data | |
52865dfd JB |
249 | * The sta value should also not be used because it may |
250 | * be NULL even though a STA (in IBSS mode) will be added. | |
571ecf67 JB |
251 | */ |
252 | ||
6e0d114d JB |
253 | static ieee80211_txrx_result |
254 | ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx) | |
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) |
64bd4b69 | 265 | rx->flags |= IEEE80211_TXRXD_RX_AMSDU; |
fd4c7f2f | 266 | else |
64bd4b69 | 267 | rx->flags &= ~IEEE80211_TXRXD_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 | |
6e0d114d | 278 | I802_DEBUG_INC(rx->local->wme_rx_queue[tid]); |
52865dfd JB |
279 | /* only a debug counter, sta might not be assigned properly yet */ |
280 | if (rx->sta) | |
6e0d114d | 281 | I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]); |
6e0d114d JB |
282 | |
283 | rx->u.rx.queue = tid; | |
284 | /* Set skb->priority to 1d tag if highest order bit of TID is not set. | |
285 | * For now, set skb->priority to 0 for other cases. */ | |
286 | rx->skb->priority = (tid > 7) ? 0 : tid; | |
287 | ||
288 | return TXRX_CONTINUE; | |
289 | } | |
290 | ||
571ecf67 JB |
291 | static ieee80211_txrx_result |
292 | ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx) | |
293 | { | |
294 | struct ieee80211_local *local = rx->local; | |
295 | struct sk_buff *skb = rx->skb; | |
296 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; | |
297 | u32 load = 0, hdrtime; | |
298 | struct ieee80211_rate *rate; | |
299 | struct ieee80211_hw_mode *mode = local->hw.conf.mode; | |
300 | int i; | |
301 | ||
302 | /* Estimate total channel use caused by this frame */ | |
303 | ||
304 | if (unlikely(mode->num_rates < 0)) | |
305 | return TXRX_CONTINUE; | |
306 | ||
307 | rate = &mode->rates[0]; | |
308 | for (i = 0; i < mode->num_rates; i++) { | |
309 | if (mode->rates[i].val == rx->u.rx.status->rate) { | |
310 | rate = &mode->rates[i]; | |
311 | break; | |
312 | } | |
313 | } | |
314 | ||
315 | /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values, | |
316 | * 1 usec = 1/8 * (1080 / 10) = 13.5 */ | |
317 | ||
318 | if (mode->mode == MODE_IEEE80211A || | |
571ecf67 JB |
319 | (mode->mode == MODE_IEEE80211G && |
320 | rate->flags & IEEE80211_RATE_ERP)) | |
321 | hdrtime = CHAN_UTIL_HDR_SHORT; | |
322 | else | |
323 | hdrtime = CHAN_UTIL_HDR_LONG; | |
324 | ||
325 | load = hdrtime; | |
326 | if (!is_multicast_ether_addr(hdr->addr1)) | |
327 | load += hdrtime; | |
328 | ||
329 | load += skb->len * rate->rate_inv; | |
330 | ||
331 | /* Divide channel_use by 8 to avoid wrapping around the counter */ | |
332 | load >>= CHAN_UTIL_SHIFT; | |
333 | local->channel_use_raw += load; | |
571ecf67 JB |
334 | rx->u.rx.load = load; |
335 | ||
336 | return TXRX_CONTINUE; | |
337 | } | |
338 | ||
339 | ieee80211_rx_handler ieee80211_rx_pre_handlers[] = | |
340 | { | |
341 | ieee80211_rx_h_parse_qos, | |
342 | ieee80211_rx_h_load_stats, | |
343 | NULL | |
344 | }; | |
345 | ||
346 | /* rx handlers */ | |
347 | ||
348 | static ieee80211_txrx_result | |
349 | ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx) | |
350 | { | |
52865dfd JB |
351 | if (rx->sta) |
352 | rx->sta->channel_use_raw += rx->u.rx.load; | |
571ecf67 JB |
353 | rx->sdata->channel_use_raw += rx->u.rx.load; |
354 | return TXRX_CONTINUE; | |
355 | } | |
356 | ||
571ecf67 JB |
357 | static ieee80211_txrx_result |
358 | ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx) | |
359 | { | |
360 | struct ieee80211_local *local = rx->local; | |
361 | struct sk_buff *skb = rx->skb; | |
362 | ||
ece8eddd ZY |
363 | if (unlikely(local->sta_hw_scanning)) |
364 | return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status); | |
365 | ||
366 | if (unlikely(local->sta_sw_scanning)) { | |
367 | /* drop all the other packets during a software scan anyway */ | |
368 | if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status) | |
369 | != TXRX_QUEUED) | |
370 | dev_kfree_skb(skb); | |
571ecf67 JB |
371 | return TXRX_QUEUED; |
372 | } | |
373 | ||
badffb72 | 374 | if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) { |
571ecf67 JB |
375 | /* scanning finished during invoking of handlers */ |
376 | I802_DEBUG_INC(local->rx_handlers_drop_passive_scan); | |
377 | return TXRX_DROP; | |
378 | } | |
379 | ||
380 | return TXRX_CONTINUE; | |
381 | } | |
382 | ||
383 | static ieee80211_txrx_result | |
384 | ieee80211_rx_h_check(struct ieee80211_txrx_data *rx) | |
385 | { | |
386 | struct ieee80211_hdr *hdr; | |
571ecf67 JB |
387 | hdr = (struct ieee80211_hdr *) rx->skb->data; |
388 | ||
389 | /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */ | |
390 | if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) { | |
391 | if (unlikely(rx->fc & IEEE80211_FCTL_RETRY && | |
392 | rx->sta->last_seq_ctrl[rx->u.rx.queue] == | |
393 | hdr->seq_ctrl)) { | |
badffb72 | 394 | if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) { |
571ecf67 JB |
395 | rx->local->dot11FrameDuplicateCount++; |
396 | rx->sta->num_duplicates++; | |
397 | } | |
398 | return TXRX_DROP; | |
399 | } else | |
400 | rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl; | |
401 | } | |
402 | ||
571ecf67 JB |
403 | if (unlikely(rx->skb->len < 16)) { |
404 | I802_DEBUG_INC(rx->local->rx_handlers_drop_short); | |
405 | return TXRX_DROP; | |
406 | } | |
407 | ||
badffb72 | 408 | if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) |
571ecf67 JB |
409 | rx->skb->pkt_type = PACKET_OTHERHOST; |
410 | else if (compare_ether_addr(rx->dev->dev_addr, hdr->addr1) == 0) | |
411 | rx->skb->pkt_type = PACKET_HOST; | |
412 | else if (is_multicast_ether_addr(hdr->addr1)) { | |
413 | if (is_broadcast_ether_addr(hdr->addr1)) | |
414 | rx->skb->pkt_type = PACKET_BROADCAST; | |
415 | else | |
416 | rx->skb->pkt_type = PACKET_MULTICAST; | |
417 | } else | |
418 | rx->skb->pkt_type = PACKET_OTHERHOST; | |
419 | ||
420 | /* Drop disallowed frame classes based on STA auth/assoc state; | |
421 | * IEEE 802.11, Chap 5.5. | |
422 | * | |
423 | * 80211.o does filtering only based on association state, i.e., it | |
424 | * drops Class 3 frames from not associated stations. hostapd sends | |
425 | * deauth/disassoc frames when needed. In addition, hostapd is | |
426 | * responsible for filtering on both auth and assoc states. | |
427 | */ | |
428 | if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA || | |
429 | ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL && | |
430 | (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) && | |
431 | rx->sdata->type != IEEE80211_IF_TYPE_IBSS && | |
432 | (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) { | |
433 | if ((!(rx->fc & IEEE80211_FCTL_FROMDS) && | |
434 | !(rx->fc & IEEE80211_FCTL_TODS) && | |
435 | (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) | |
badffb72 | 436 | || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) { |
571ecf67 JB |
437 | /* Drop IBSS frames and frames for other hosts |
438 | * silently. */ | |
439 | return TXRX_DROP; | |
440 | } | |
441 | ||
f9d540ee | 442 | return TXRX_DROP; |
571ecf67 JB |
443 | } |
444 | ||
570bd537 JB |
445 | return TXRX_CONTINUE; |
446 | } | |
447 | ||
448 | ||
449 | static ieee80211_txrx_result | |
1990af8d | 450 | ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx) |
570bd537 JB |
451 | { |
452 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data; | |
3017b80b JB |
453 | int keyidx; |
454 | int hdrlen; | |
e2f036da | 455 | ieee80211_txrx_result result = TXRX_DROP; |
d4e46a3d | 456 | struct ieee80211_key *stakey = NULL; |
570bd537 | 457 | |
3017b80b JB |
458 | /* |
459 | * Key selection 101 | |
460 | * | |
461 | * There are three types of keys: | |
462 | * - GTK (group keys) | |
463 | * - PTK (pairwise keys) | |
464 | * - STK (station-to-station pairwise keys) | |
465 | * | |
466 | * When selecting a key, we have to distinguish between multicast | |
467 | * (including broadcast) and unicast frames, the latter can only | |
468 | * use PTKs and STKs while the former always use GTKs. Unless, of | |
469 | * course, actual WEP keys ("pre-RSNA") are used, then unicast | |
470 | * frames can also use key indizes like GTKs. Hence, if we don't | |
471 | * have a PTK/STK we check the key index for a WEP key. | |
472 | * | |
8dc06a1c JB |
473 | * Note that in a regular BSS, multicast frames are sent by the |
474 | * AP only, associated stations unicast the frame to the AP first | |
475 | * which then multicasts it on their behalf. | |
476 | * | |
3017b80b JB |
477 | * There is also a slight problem in IBSS mode: GTKs are negotiated |
478 | * with each station, that is something we don't currently handle. | |
8dc06a1c JB |
479 | * The spec seems to expect that one negotiates the same key with |
480 | * every station but there's no such requirement; VLANs could be | |
481 | * possible. | |
3017b80b JB |
482 | */ |
483 | ||
484 | if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) | |
485 | return TXRX_CONTINUE; | |
571ecf67 | 486 | |
3017b80b | 487 | /* |
1990af8d | 488 | * No point in finding a key and decrypting if the frame is neither |
3017b80b JB |
489 | * addressed to us nor a multicast frame. |
490 | */ | |
badffb72 | 491 | if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) |
3017b80b JB |
492 | return TXRX_CONTINUE; |
493 | ||
d4e46a3d JB |
494 | if (rx->sta) |
495 | stakey = rcu_dereference(rx->sta->key); | |
496 | ||
497 | if (!is_multicast_ether_addr(hdr->addr1) && stakey) { | |
498 | rx->key = stakey; | |
571ecf67 | 499 | } else { |
3017b80b JB |
500 | /* |
501 | * The device doesn't give us the IV so we won't be | |
502 | * able to look up the key. That's ok though, we | |
503 | * don't need to decrypt the frame, we just won't | |
504 | * be able to keep statistics accurate. | |
505 | * Except for key threshold notifications, should | |
506 | * we somehow allow the driver to tell us which key | |
507 | * the hardware used if this flag is set? | |
508 | */ | |
7848ba7d JB |
509 | if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) && |
510 | (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED)) | |
3017b80b JB |
511 | return TXRX_CONTINUE; |
512 | ||
513 | hdrlen = ieee80211_get_hdrlen(rx->fc); | |
514 | ||
515 | if (rx->skb->len < 8 + hdrlen) | |
516 | return TXRX_DROP; /* TODO: count this? */ | |
517 | ||
518 | /* | |
519 | * no need to call ieee80211_wep_get_keyidx, | |
520 | * it verifies a bunch of things we've done already | |
521 | */ | |
522 | keyidx = rx->skb->data[hdrlen + 3] >> 6; | |
523 | ||
d4e46a3d | 524 | rx->key = rcu_dereference(rx->sdata->keys[keyidx]); |
3017b80b JB |
525 | |
526 | /* | |
527 | * RSNA-protected unicast frames should always be sent with | |
528 | * pairwise or station-to-station keys, but for WEP we allow | |
529 | * using a key index as well. | |
530 | */ | |
8f20fc24 | 531 | if (rx->key && rx->key->conf.alg != ALG_WEP && |
3017b80b JB |
532 | !is_multicast_ether_addr(hdr->addr1)) |
533 | rx->key = NULL; | |
571ecf67 JB |
534 | } |
535 | ||
3017b80b | 536 | if (rx->key) { |
571ecf67 | 537 | rx->key->tx_rx_count++; |
011bfcc4 | 538 | /* TODO: add threshold stuff again */ |
1990af8d | 539 | } else { |
7f3ad894 | 540 | #ifdef CONFIG_MAC80211_DEBUG |
70f08765 JB |
541 | if (net_ratelimit()) |
542 | printk(KERN_DEBUG "%s: RX protected frame," | |
543 | " but have no key\n", rx->dev->name); | |
7f3ad894 | 544 | #endif /* CONFIG_MAC80211_DEBUG */ |
70f08765 JB |
545 | return TXRX_DROP; |
546 | } | |
547 | ||
1990af8d JB |
548 | /* Check for weak IVs if possible */ |
549 | if (rx->sta && rx->key->conf.alg == ALG_WEP && | |
550 | ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) && | |
551 | (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) || | |
552 | !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) && | |
553 | ieee80211_wep_is_weak_iv(rx->skb, rx->key)) | |
554 | rx->sta->wep_weak_iv_count++; | |
555 | ||
70f08765 JB |
556 | switch (rx->key->conf.alg) { |
557 | case ALG_WEP: | |
e2f036da MN |
558 | result = ieee80211_crypto_wep_decrypt(rx); |
559 | break; | |
70f08765 | 560 | case ALG_TKIP: |
e2f036da MN |
561 | result = ieee80211_crypto_tkip_decrypt(rx); |
562 | break; | |
70f08765 | 563 | case ALG_CCMP: |
e2f036da MN |
564 | result = ieee80211_crypto_ccmp_decrypt(rx); |
565 | break; | |
70f08765 JB |
566 | } |
567 | ||
e2f036da MN |
568 | /* either the frame has been decrypted or will be dropped */ |
569 | rx->u.rx.status->flag |= RX_FLAG_DECRYPTED; | |
570 | ||
571 | return result; | |
70f08765 JB |
572 | } |
573 | ||
571ecf67 JB |
574 | static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta) |
575 | { | |
576 | struct ieee80211_sub_if_data *sdata; | |
0795af57 JP |
577 | DECLARE_MAC_BUF(mac); |
578 | ||
571ecf67 JB |
579 | sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev); |
580 | ||
581 | if (sdata->bss) | |
582 | atomic_inc(&sdata->bss->num_sta_ps); | |
583 | sta->flags |= WLAN_STA_PS; | |
584 | sta->pspoll = 0; | |
585 | #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG | |
0795af57 JP |
586 | printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n", |
587 | dev->name, print_mac(mac, sta->addr), sta->aid); | |
571ecf67 JB |
588 | #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ |
589 | } | |
590 | ||
591 | static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta) | |
592 | { | |
593 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
594 | struct sk_buff *skb; | |
595 | int sent = 0; | |
596 | struct ieee80211_sub_if_data *sdata; | |
597 | struct ieee80211_tx_packet_data *pkt_data; | |
0795af57 | 598 | DECLARE_MAC_BUF(mac); |
571ecf67 JB |
599 | |
600 | sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev); | |
601 | if (sdata->bss) | |
602 | atomic_dec(&sdata->bss->num_sta_ps); | |
603 | sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM); | |
604 | sta->pspoll = 0; | |
605 | if (!skb_queue_empty(&sta->ps_tx_buf)) { | |
606 | if (local->ops->set_tim) | |
607 | local->ops->set_tim(local_to_hw(local), sta->aid, 0); | |
608 | if (sdata->bss) | |
609 | bss_tim_clear(local, sdata->bss, sta->aid); | |
610 | } | |
611 | #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG | |
0795af57 JP |
612 | printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n", |
613 | dev->name, print_mac(mac, sta->addr), sta->aid); | |
571ecf67 JB |
614 | #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ |
615 | /* Send all buffered frames to the station */ | |
616 | while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) { | |
617 | pkt_data = (struct ieee80211_tx_packet_data *) skb->cb; | |
618 | sent++; | |
e8bf9649 | 619 | pkt_data->flags |= IEEE80211_TXPD_REQUEUE; |
571ecf67 JB |
620 | dev_queue_xmit(skb); |
621 | } | |
622 | while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) { | |
623 | pkt_data = (struct ieee80211_tx_packet_data *) skb->cb; | |
624 | local->total_ps_buffered--; | |
625 | sent++; | |
626 | #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG | |
0795af57 | 627 | printk(KERN_DEBUG "%s: STA %s aid %d send PS frame " |
571ecf67 | 628 | "since STA not sleeping anymore\n", dev->name, |
0795af57 | 629 | print_mac(mac, sta->addr), sta->aid); |
571ecf67 | 630 | #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ |
e8bf9649 | 631 | pkt_data->flags |= IEEE80211_TXPD_REQUEUE; |
571ecf67 JB |
632 | dev_queue_xmit(skb); |
633 | } | |
634 | ||
635 | return sent; | |
636 | } | |
637 | ||
638 | static ieee80211_txrx_result | |
639 | ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx) | |
640 | { | |
641 | struct sta_info *sta = rx->sta; | |
642 | struct net_device *dev = rx->dev; | |
643 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data; | |
644 | ||
645 | if (!sta) | |
646 | return TXRX_CONTINUE; | |
647 | ||
648 | /* Update last_rx only for IBSS packets which are for the current | |
649 | * BSSID to avoid keeping the current IBSS network alive in cases where | |
650 | * other STAs are using different BSSID. */ | |
651 | if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) { | |
652 | u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len); | |
653 | if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0) | |
654 | sta->last_rx = jiffies; | |
655 | } else | |
656 | if (!is_multicast_ether_addr(hdr->addr1) || | |
657 | rx->sdata->type == IEEE80211_IF_TYPE_STA) { | |
658 | /* Update last_rx only for unicast frames in order to prevent | |
659 | * the Probe Request frames (the only broadcast frames from a | |
660 | * STA in infrastructure mode) from keeping a connection alive. | |
661 | */ | |
662 | sta->last_rx = jiffies; | |
663 | } | |
664 | ||
badffb72 | 665 | if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) |
571ecf67 JB |
666 | return TXRX_CONTINUE; |
667 | ||
668 | sta->rx_fragments++; | |
669 | sta->rx_bytes += rx->skb->len; | |
6c55aa97 LF |
670 | sta->last_rssi = rx->u.rx.status->ssi; |
671 | sta->last_signal = rx->u.rx.status->signal; | |
672 | sta->last_noise = rx->u.rx.status->noise; | |
571ecf67 JB |
673 | |
674 | if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) { | |
675 | /* Change STA power saving mode only in the end of a frame | |
676 | * exchange sequence */ | |
677 | if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM)) | |
678 | rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta); | |
679 | else if (!(sta->flags & WLAN_STA_PS) && | |
680 | (rx->fc & IEEE80211_FCTL_PM)) | |
681 | ap_sta_ps_start(dev, sta); | |
682 | } | |
683 | ||
684 | /* Drop data::nullfunc frames silently, since they are used only to | |
685 | * control station power saving mode. */ | |
686 | if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA && | |
687 | (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) { | |
688 | I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc); | |
689 | /* Update counter and free packet here to avoid counting this | |
690 | * as a dropped packed. */ | |
691 | sta->rx_packets++; | |
692 | dev_kfree_skb(rx->skb); | |
693 | return TXRX_QUEUED; | |
694 | } | |
695 | ||
696 | return TXRX_CONTINUE; | |
697 | } /* ieee80211_rx_h_sta_process */ | |
698 | ||
571ecf67 JB |
699 | static inline struct ieee80211_fragment_entry * |
700 | ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata, | |
701 | unsigned int frag, unsigned int seq, int rx_queue, | |
702 | struct sk_buff **skb) | |
703 | { | |
704 | struct ieee80211_fragment_entry *entry; | |
705 | int idx; | |
706 | ||
707 | idx = sdata->fragment_next; | |
708 | entry = &sdata->fragments[sdata->fragment_next++]; | |
709 | if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX) | |
710 | sdata->fragment_next = 0; | |
711 | ||
712 | if (!skb_queue_empty(&entry->skb_list)) { | |
713 | #ifdef CONFIG_MAC80211_DEBUG | |
714 | struct ieee80211_hdr *hdr = | |
715 | (struct ieee80211_hdr *) entry->skb_list.next->data; | |
0795af57 JP |
716 | DECLARE_MAC_BUF(mac); |
717 | DECLARE_MAC_BUF(mac2); | |
571ecf67 JB |
718 | printk(KERN_DEBUG "%s: RX reassembly removed oldest " |
719 | "fragment entry (idx=%d age=%lu seq=%d last_frag=%d " | |
0795af57 | 720 | "addr1=%s addr2=%s\n", |
571ecf67 JB |
721 | sdata->dev->name, idx, |
722 | jiffies - entry->first_frag_time, entry->seq, | |
0795af57 JP |
723 | entry->last_frag, print_mac(mac, hdr->addr1), |
724 | print_mac(mac2, hdr->addr2)); | |
571ecf67 JB |
725 | #endif /* CONFIG_MAC80211_DEBUG */ |
726 | __skb_queue_purge(&entry->skb_list); | |
727 | } | |
728 | ||
729 | __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */ | |
730 | *skb = NULL; | |
731 | entry->first_frag_time = jiffies; | |
732 | entry->seq = seq; | |
733 | entry->rx_queue = rx_queue; | |
734 | entry->last_frag = frag; | |
735 | entry->ccmp = 0; | |
736 | entry->extra_len = 0; | |
737 | ||
738 | return entry; | |
739 | } | |
740 | ||
741 | static inline struct ieee80211_fragment_entry * | |
742 | ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata, | |
743 | u16 fc, unsigned int frag, unsigned int seq, | |
744 | int rx_queue, struct ieee80211_hdr *hdr) | |
745 | { | |
746 | struct ieee80211_fragment_entry *entry; | |
747 | int i, idx; | |
748 | ||
749 | idx = sdata->fragment_next; | |
750 | for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) { | |
751 | struct ieee80211_hdr *f_hdr; | |
752 | u16 f_fc; | |
753 | ||
754 | idx--; | |
755 | if (idx < 0) | |
756 | idx = IEEE80211_FRAGMENT_MAX - 1; | |
757 | ||
758 | entry = &sdata->fragments[idx]; | |
759 | if (skb_queue_empty(&entry->skb_list) || entry->seq != seq || | |
760 | entry->rx_queue != rx_queue || | |
761 | entry->last_frag + 1 != frag) | |
762 | continue; | |
763 | ||
764 | f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data; | |
765 | f_fc = le16_to_cpu(f_hdr->frame_control); | |
766 | ||
767 | if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) || | |
768 | compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 || | |
769 | compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0) | |
770 | continue; | |
771 | ||
772 | if (entry->first_frag_time + 2 * HZ < jiffies) { | |
773 | __skb_queue_purge(&entry->skb_list); | |
774 | continue; | |
775 | } | |
776 | return entry; | |
777 | } | |
778 | ||
779 | return NULL; | |
780 | } | |
781 | ||
782 | static ieee80211_txrx_result | |
783 | ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx) | |
784 | { | |
785 | struct ieee80211_hdr *hdr; | |
786 | u16 sc; | |
787 | unsigned int frag, seq; | |
788 | struct ieee80211_fragment_entry *entry; | |
789 | struct sk_buff *skb; | |
0795af57 | 790 | DECLARE_MAC_BUF(mac); |
571ecf67 JB |
791 | |
792 | hdr = (struct ieee80211_hdr *) rx->skb->data; | |
793 | sc = le16_to_cpu(hdr->seq_ctrl); | |
794 | frag = sc & IEEE80211_SCTL_FRAG; | |
795 | ||
796 | if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) || | |
797 | (rx->skb)->len < 24 || | |
798 | is_multicast_ether_addr(hdr->addr1))) { | |
799 | /* not fragmented */ | |
800 | goto out; | |
801 | } | |
802 | I802_DEBUG_INC(rx->local->rx_handlers_fragments); | |
803 | ||
804 | seq = (sc & IEEE80211_SCTL_SEQ) >> 4; | |
805 | ||
806 | if (frag == 0) { | |
807 | /* This is the first fragment of a new frame. */ | |
808 | entry = ieee80211_reassemble_add(rx->sdata, frag, seq, | |
809 | rx->u.rx.queue, &(rx->skb)); | |
8f20fc24 | 810 | if (rx->key && rx->key->conf.alg == ALG_CCMP && |
571ecf67 JB |
811 | (rx->fc & IEEE80211_FCTL_PROTECTED)) { |
812 | /* Store CCMP PN so that we can verify that the next | |
813 | * fragment has a sequential PN value. */ | |
814 | entry->ccmp = 1; | |
815 | memcpy(entry->last_pn, | |
816 | rx->key->u.ccmp.rx_pn[rx->u.rx.queue], | |
817 | CCMP_PN_LEN); | |
818 | } | |
819 | return TXRX_QUEUED; | |
820 | } | |
821 | ||
822 | /* This is a fragment for a frame that should already be pending in | |
823 | * fragment cache. Add this fragment to the end of the pending entry. | |
824 | */ | |
825 | entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq, | |
826 | rx->u.rx.queue, hdr); | |
827 | if (!entry) { | |
828 | I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag); | |
829 | return TXRX_DROP; | |
830 | } | |
831 | ||
832 | /* Verify that MPDUs within one MSDU have sequential PN values. | |
833 | * (IEEE 802.11i, 8.3.3.4.5) */ | |
834 | if (entry->ccmp) { | |
835 | int i; | |
836 | u8 pn[CCMP_PN_LEN], *rpn; | |
8f20fc24 | 837 | if (!rx->key || rx->key->conf.alg != ALG_CCMP) |
571ecf67 JB |
838 | return TXRX_DROP; |
839 | memcpy(pn, entry->last_pn, CCMP_PN_LEN); | |
840 | for (i = CCMP_PN_LEN - 1; i >= 0; i--) { | |
841 | pn[i]++; | |
842 | if (pn[i]) | |
843 | break; | |
844 | } | |
845 | rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue]; | |
846 | if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) { | |
1a84f3fd JB |
847 | if (net_ratelimit()) |
848 | printk(KERN_DEBUG "%s: defrag: CCMP PN not " | |
0795af57 | 849 | "sequential A2=%s" |
1a84f3fd JB |
850 | " PN=%02x%02x%02x%02x%02x%02x " |
851 | "(expected %02x%02x%02x%02x%02x%02x)\n", | |
0795af57 | 852 | rx->dev->name, print_mac(mac, hdr->addr2), |
1a84f3fd JB |
853 | rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], |
854 | rpn[5], pn[0], pn[1], pn[2], pn[3], | |
855 | pn[4], pn[5]); | |
571ecf67 JB |
856 | return TXRX_DROP; |
857 | } | |
858 | memcpy(entry->last_pn, pn, CCMP_PN_LEN); | |
859 | } | |
860 | ||
861 | skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc)); | |
862 | __skb_queue_tail(&entry->skb_list, rx->skb); | |
863 | entry->last_frag = frag; | |
864 | entry->extra_len += rx->skb->len; | |
865 | if (rx->fc & IEEE80211_FCTL_MOREFRAGS) { | |
866 | rx->skb = NULL; | |
867 | return TXRX_QUEUED; | |
868 | } | |
869 | ||
870 | rx->skb = __skb_dequeue(&entry->skb_list); | |
871 | if (skb_tailroom(rx->skb) < entry->extra_len) { | |
872 | I802_DEBUG_INC(rx->local->rx_expand_skb_head2); | |
873 | if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len, | |
874 | GFP_ATOMIC))) { | |
875 | I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag); | |
876 | __skb_queue_purge(&entry->skb_list); | |
877 | return TXRX_DROP; | |
878 | } | |
879 | } | |
880 | while ((skb = __skb_dequeue(&entry->skb_list))) { | |
881 | memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len); | |
882 | dev_kfree_skb(skb); | |
883 | } | |
884 | ||
885 | /* Complete frame has been reassembled - process it now */ | |
badffb72 | 886 | rx->flags |= IEEE80211_TXRXD_FRAGMENTED; |
571ecf67 JB |
887 | |
888 | out: | |
889 | if (rx->sta) | |
890 | rx->sta->rx_packets++; | |
891 | if (is_multicast_ether_addr(hdr->addr1)) | |
892 | rx->local->dot11MulticastReceivedFrameCount++; | |
893 | else | |
894 | ieee80211_led_rx(rx->local); | |
895 | return TXRX_CONTINUE; | |
896 | } | |
897 | ||
898 | static ieee80211_txrx_result | |
899 | ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx) | |
900 | { | |
98f0b0a3 | 901 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev); |
571ecf67 JB |
902 | struct sk_buff *skb; |
903 | int no_pending_pkts; | |
0795af57 | 904 | DECLARE_MAC_BUF(mac); |
571ecf67 JB |
905 | |
906 | if (likely(!rx->sta || | |
907 | (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL || | |
908 | (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL || | |
badffb72 | 909 | !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))) |
571ecf67 JB |
910 | return TXRX_CONTINUE; |
911 | ||
98f0b0a3 RR |
912 | if ((sdata->type != IEEE80211_IF_TYPE_AP) && |
913 | (sdata->type != IEEE80211_IF_TYPE_VLAN)) | |
914 | return TXRX_DROP; | |
915 | ||
571ecf67 JB |
916 | skb = skb_dequeue(&rx->sta->tx_filtered); |
917 | if (!skb) { | |
918 | skb = skb_dequeue(&rx->sta->ps_tx_buf); | |
919 | if (skb) | |
920 | rx->local->total_ps_buffered--; | |
921 | } | |
922 | no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) && | |
923 | skb_queue_empty(&rx->sta->ps_tx_buf); | |
924 | ||
925 | if (skb) { | |
926 | struct ieee80211_hdr *hdr = | |
927 | (struct ieee80211_hdr *) skb->data; | |
928 | ||
929 | /* tell TX path to send one frame even though the STA may | |
930 | * still remain is PS mode after this frame exchange */ | |
931 | rx->sta->pspoll = 1; | |
932 | ||
933 | #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG | |
0795af57 JP |
934 | printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n", |
935 | print_mac(mac, rx->sta->addr), rx->sta->aid, | |
571ecf67 JB |
936 | skb_queue_len(&rx->sta->ps_tx_buf)); |
937 | #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ | |
938 | ||
939 | /* Use MoreData flag to indicate whether there are more | |
940 | * buffered frames for this STA */ | |
941 | if (no_pending_pkts) { | |
942 | hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA); | |
943 | rx->sta->flags &= ~WLAN_STA_TIM; | |
944 | } else | |
945 | hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); | |
946 | ||
947 | dev_queue_xmit(skb); | |
948 | ||
949 | if (no_pending_pkts) { | |
950 | if (rx->local->ops->set_tim) | |
951 | rx->local->ops->set_tim(local_to_hw(rx->local), | |
952 | rx->sta->aid, 0); | |
953 | if (rx->sdata->bss) | |
954 | bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid); | |
955 | } | |
956 | #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG | |
957 | } else if (!rx->u.rx.sent_ps_buffered) { | |
0795af57 | 958 | printk(KERN_DEBUG "%s: STA %s sent PS Poll even " |
571ecf67 | 959 | "though there is no buffered frames for it\n", |
0795af57 | 960 | rx->dev->name, print_mac(mac, rx->sta->addr)); |
571ecf67 JB |
961 | #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ |
962 | ||
963 | } | |
964 | ||
965 | /* Free PS Poll skb here instead of returning TXRX_DROP that would | |
966 | * count as an dropped frame. */ | |
967 | dev_kfree_skb(rx->skb); | |
968 | ||
969 | return TXRX_QUEUED; | |
970 | } | |
971 | ||
6e0d114d JB |
972 | static ieee80211_txrx_result |
973 | ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx) | |
974 | { | |
975 | u16 fc = rx->fc; | |
976 | u8 *data = rx->skb->data; | |
977 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data; | |
978 | ||
979 | if (!WLAN_FC_IS_QOS_DATA(fc)) | |
980 | return TXRX_CONTINUE; | |
981 | ||
982 | /* remove the qos control field, update frame type and meta-data */ | |
983 | memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2); | |
984 | hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2); | |
985 | /* change frame type to non QOS */ | |
986 | rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA; | |
987 | hdr->frame_control = cpu_to_le16(fc); | |
988 | ||
989 | return TXRX_CONTINUE; | |
990 | } | |
991 | ||
76ee65bf RR |
992 | static int |
993 | ieee80211_drop_802_1x_pae(struct ieee80211_txrx_data *rx, int hdrlen) | |
571ecf67 | 994 | { |
76ee65bf | 995 | if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb, hdrlen) && |
badffb72 | 996 | rx->sdata->type != IEEE80211_IF_TYPE_STA && |
f9d540ee | 997 | (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) |
76ee65bf | 998 | return 0; |
571ecf67 JB |
999 | |
1000 | if (unlikely(rx->sdata->ieee802_1x && | |
1001 | (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA && | |
1002 | (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC && | |
1003 | (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) && | |
76ee65bf | 1004 | !ieee80211_is_eapol(rx->skb, hdrlen))) { |
571ecf67 | 1005 | #ifdef CONFIG_MAC80211_DEBUG |
76ee65bf RR |
1006 | printk(KERN_DEBUG "%s: dropped frame " |
1007 | "(unauthorized port)\n", rx->dev->name); | |
571ecf67 | 1008 | #endif /* CONFIG_MAC80211_DEBUG */ |
76ee65bf | 1009 | return -EACCES; |
571ecf67 JB |
1010 | } |
1011 | ||
76ee65bf | 1012 | return 0; |
571ecf67 JB |
1013 | } |
1014 | ||
76ee65bf RR |
1015 | static int |
1016 | ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx, int hdrlen) | |
571ecf67 | 1017 | { |
3017b80b | 1018 | /* |
7848ba7d JB |
1019 | * Pass through unencrypted frames if the hardware has |
1020 | * decrypted them already. | |
3017b80b | 1021 | */ |
7848ba7d | 1022 | if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED) |
76ee65bf | 1023 | return 0; |
571ecf67 JB |
1024 | |
1025 | /* Drop unencrypted frames if key is set. */ | |
1026 | if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) && | |
1027 | (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA && | |
1028 | (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC && | |
8312512e | 1029 | (rx->key || rx->sdata->drop_unencrypted) && |
76ee65bf RR |
1030 | (rx->sdata->eapol == 0 || |
1031 | !ieee80211_is_eapol(rx->skb, hdrlen)))) { | |
1a84f3fd JB |
1032 | if (net_ratelimit()) |
1033 | printk(KERN_DEBUG "%s: RX non-WEP frame, but expected " | |
1034 | "encryption\n", rx->dev->name); | |
76ee65bf | 1035 | return -EACCES; |
571ecf67 | 1036 | } |
76ee65bf | 1037 | return 0; |
571ecf67 JB |
1038 | } |
1039 | ||
76ee65bf RR |
1040 | static int |
1041 | ieee80211_data_to_8023(struct ieee80211_txrx_data *rx) | |
571ecf67 JB |
1042 | { |
1043 | struct net_device *dev = rx->dev; | |
571ecf67 JB |
1044 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data; |
1045 | u16 fc, hdrlen, ethertype; | |
1046 | u8 *payload; | |
1047 | u8 dst[ETH_ALEN]; | |
1048 | u8 src[ETH_ALEN]; | |
76ee65bf | 1049 | struct sk_buff *skb = rx->skb; |
571ecf67 | 1050 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
0795af57 JP |
1051 | DECLARE_MAC_BUF(mac); |
1052 | DECLARE_MAC_BUF(mac2); | |
1053 | DECLARE_MAC_BUF(mac3); | |
1054 | DECLARE_MAC_BUF(mac4); | |
571ecf67 JB |
1055 | |
1056 | fc = rx->fc; | |
571ecf67 JB |
1057 | |
1058 | if (unlikely(!WLAN_FC_DATA_PRESENT(fc))) | |
76ee65bf | 1059 | return -1; |
571ecf67 JB |
1060 | |
1061 | hdrlen = ieee80211_get_hdrlen(fc); | |
1062 | ||
1063 | /* convert IEEE 802.11 header + possible LLC headers into Ethernet | |
1064 | * header | |
1065 | * IEEE 802.11 address fields: | |
1066 | * ToDS FromDS Addr1 Addr2 Addr3 Addr4 | |
1067 | * 0 0 DA SA BSSID n/a | |
1068 | * 0 1 DA BSSID SA n/a | |
1069 | * 1 0 BSSID SA DA n/a | |
1070 | * 1 1 RA TA DA SA | |
1071 | */ | |
1072 | ||
1073 | switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) { | |
1074 | case IEEE80211_FCTL_TODS: | |
1075 | /* BSSID SA DA */ | |
1076 | memcpy(dst, hdr->addr3, ETH_ALEN); | |
1077 | memcpy(src, hdr->addr2, ETH_ALEN); | |
1078 | ||
1079 | if (unlikely(sdata->type != IEEE80211_IF_TYPE_AP && | |
1080 | sdata->type != IEEE80211_IF_TYPE_VLAN)) { | |
1a84f3fd JB |
1081 | if (net_ratelimit()) |
1082 | printk(KERN_DEBUG "%s: dropped ToDS frame " | |
0795af57 | 1083 | "(BSSID=%s SA=%s DA=%s)\n", |
1a84f3fd | 1084 | dev->name, |
0795af57 JP |
1085 | print_mac(mac, hdr->addr1), |
1086 | print_mac(mac2, hdr->addr2), | |
1087 | print_mac(mac3, hdr->addr3)); | |
76ee65bf | 1088 | return -1; |
571ecf67 JB |
1089 | } |
1090 | break; | |
1091 | case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS): | |
1092 | /* RA TA DA SA */ | |
1093 | memcpy(dst, hdr->addr3, ETH_ALEN); | |
1094 | memcpy(src, hdr->addr4, ETH_ALEN); | |
1095 | ||
1096 | if (unlikely(sdata->type != IEEE80211_IF_TYPE_WDS)) { | |
1a84f3fd JB |
1097 | if (net_ratelimit()) |
1098 | printk(KERN_DEBUG "%s: dropped FromDS&ToDS " | |
0795af57 | 1099 | "frame (RA=%s TA=%s DA=%s SA=%s)\n", |
1a84f3fd | 1100 | rx->dev->name, |
0795af57 JP |
1101 | print_mac(mac, hdr->addr1), |
1102 | print_mac(mac2, hdr->addr2), | |
1103 | print_mac(mac3, hdr->addr3), | |
1104 | print_mac(mac4, hdr->addr4)); | |
76ee65bf | 1105 | return -1; |
571ecf67 JB |
1106 | } |
1107 | break; | |
1108 | case IEEE80211_FCTL_FROMDS: | |
1109 | /* DA BSSID SA */ | |
1110 | memcpy(dst, hdr->addr1, ETH_ALEN); | |
1111 | memcpy(src, hdr->addr3, ETH_ALEN); | |
1112 | ||
b3316157 JL |
1113 | if (sdata->type != IEEE80211_IF_TYPE_STA || |
1114 | (is_multicast_ether_addr(dst) && | |
1115 | !compare_ether_addr(src, dev->dev_addr))) | |
76ee65bf | 1116 | return -1; |
571ecf67 JB |
1117 | break; |
1118 | case 0: | |
1119 | /* DA SA BSSID */ | |
1120 | memcpy(dst, hdr->addr1, ETH_ALEN); | |
1121 | memcpy(src, hdr->addr2, ETH_ALEN); | |
1122 | ||
1123 | if (sdata->type != IEEE80211_IF_TYPE_IBSS) { | |
1124 | if (net_ratelimit()) { | |
0795af57 JP |
1125 | printk(KERN_DEBUG "%s: dropped IBSS frame " |
1126 | "(DA=%s SA=%s BSSID=%s)\n", | |
1127 | dev->name, | |
1128 | print_mac(mac, hdr->addr1), | |
1129 | print_mac(mac2, hdr->addr2), | |
1130 | print_mac(mac3, hdr->addr3)); | |
571ecf67 | 1131 | } |
76ee65bf | 1132 | return -1; |
571ecf67 JB |
1133 | } |
1134 | break; | |
1135 | } | |
1136 | ||
571ecf67 JB |
1137 | if (unlikely(skb->len - hdrlen < 8)) { |
1138 | if (net_ratelimit()) { | |
1139 | printk(KERN_DEBUG "%s: RX too short data frame " | |
1140 | "payload\n", dev->name); | |
1141 | } | |
76ee65bf | 1142 | return -1; |
571ecf67 JB |
1143 | } |
1144 | ||
76ee65bf | 1145 | payload = skb->data + hdrlen; |
571ecf67 JB |
1146 | ethertype = (payload[6] << 8) | payload[7]; |
1147 | ||
1148 | if (likely((compare_ether_addr(payload, rfc1042_header) == 0 && | |
1149 | ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || | |
1150 | compare_ether_addr(payload, bridge_tunnel_header) == 0)) { | |
1151 | /* remove RFC1042 or Bridge-Tunnel encapsulation and | |
1152 | * replace EtherType */ | |
1153 | skb_pull(skb, hdrlen + 6); | |
1154 | memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN); | |
1155 | memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN); | |
1156 | } else { | |
1157 | struct ethhdr *ehdr; | |
1158 | __be16 len; | |
1159 | skb_pull(skb, hdrlen); | |
1160 | len = htons(skb->len); | |
1161 | ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr)); | |
1162 | memcpy(ehdr->h_dest, dst, ETH_ALEN); | |
1163 | memcpy(ehdr->h_source, src, ETH_ALEN); | |
1164 | ehdr->h_proto = len; | |
1165 | } | |
76ee65bf RR |
1166 | return 0; |
1167 | } | |
571ecf67 | 1168 | |
76ee65bf RR |
1169 | static void |
1170 | ieee80211_deliver_skb(struct ieee80211_txrx_data *rx) | |
1171 | { | |
1172 | struct net_device *dev = rx->dev; | |
1173 | struct ieee80211_local *local = rx->local; | |
1174 | struct sk_buff *skb, *xmit_skb; | |
1175 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
571ecf67 | 1176 | |
76ee65bf RR |
1177 | skb = rx->skb; |
1178 | xmit_skb = NULL; | |
571ecf67 JB |
1179 | |
1180 | if (local->bridge_packets && (sdata->type == IEEE80211_IF_TYPE_AP | |
badffb72 JS |
1181 | || sdata->type == IEEE80211_IF_TYPE_VLAN) && |
1182 | (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) { | |
571ecf67 JB |
1183 | if (is_multicast_ether_addr(skb->data)) { |
1184 | /* send multicast frames both to higher layers in | |
1185 | * local net stack and back to the wireless media */ | |
76ee65bf RR |
1186 | xmit_skb = skb_copy(skb, GFP_ATOMIC); |
1187 | if (!xmit_skb && net_ratelimit()) | |
571ecf67 JB |
1188 | printk(KERN_DEBUG "%s: failed to clone " |
1189 | "multicast frame\n", dev->name); | |
1190 | } else { | |
1191 | struct sta_info *dsta; | |
1192 | dsta = sta_info_get(local, skb->data); | |
1193 | if (dsta && !dsta->dev) { | |
1a84f3fd JB |
1194 | if (net_ratelimit()) |
1195 | printk(KERN_DEBUG "Station with null " | |
1196 | "dev structure!\n"); | |
571ecf67 JB |
1197 | } else if (dsta && dsta->dev == dev) { |
1198 | /* Destination station is associated to this | |
1199 | * AP, so send the frame directly to it and | |
1200 | * do not pass the frame to local net stack. | |
1201 | */ | |
76ee65bf | 1202 | xmit_skb = skb; |
571ecf67 JB |
1203 | skb = NULL; |
1204 | } | |
1205 | if (dsta) | |
1206 | sta_info_put(dsta); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | if (skb) { | |
1211 | /* deliver to local stack */ | |
1212 | skb->protocol = eth_type_trans(skb, dev); | |
1213 | memset(skb->cb, 0, sizeof(skb->cb)); | |
1214 | netif_rx(skb); | |
1215 | } | |
1216 | ||
76ee65bf | 1217 | if (xmit_skb) { |
571ecf67 | 1218 | /* send to wireless media */ |
f831e909 | 1219 | xmit_skb->protocol = htons(ETH_P_802_3); |
76ee65bf RR |
1220 | skb_set_network_header(xmit_skb, 0); |
1221 | skb_set_mac_header(xmit_skb, 0); | |
1222 | dev_queue_xmit(xmit_skb); | |
571ecf67 | 1223 | } |
76ee65bf RR |
1224 | } |
1225 | ||
fd4c7f2f RR |
1226 | static ieee80211_txrx_result |
1227 | ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx) | |
1228 | { | |
1229 | struct net_device *dev = rx->dev; | |
1230 | struct ieee80211_local *local = rx->local; | |
1231 | u16 fc, ethertype; | |
1232 | u8 *payload; | |
1233 | struct sk_buff *skb = rx->skb, *frame = NULL; | |
1234 | const struct ethhdr *eth; | |
1235 | int remaining, err; | |
1236 | u8 dst[ETH_ALEN]; | |
1237 | u8 src[ETH_ALEN]; | |
1238 | DECLARE_MAC_BUF(mac); | |
1239 | ||
1240 | fc = rx->fc; | |
1241 | if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) | |
1242 | return TXRX_CONTINUE; | |
1243 | ||
1244 | if (unlikely(!WLAN_FC_DATA_PRESENT(fc))) | |
1245 | return TXRX_DROP; | |
1246 | ||
64bd4b69 | 1247 | if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU)) |
fd4c7f2f RR |
1248 | return TXRX_CONTINUE; |
1249 | ||
1250 | err = ieee80211_data_to_8023(rx); | |
1251 | if (unlikely(err)) | |
1252 | return TXRX_DROP; | |
1253 | ||
1254 | skb->dev = dev; | |
1255 | ||
1256 | dev->stats.rx_packets++; | |
1257 | dev->stats.rx_bytes += skb->len; | |
1258 | ||
1259 | /* skip the wrapping header */ | |
1260 | eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr)); | |
1261 | if (!eth) | |
1262 | return TXRX_DROP; | |
1263 | ||
1264 | while (skb != frame) { | |
1265 | u8 padding; | |
1266 | __be16 len = eth->h_proto; | |
1267 | unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len); | |
1268 | ||
1269 | remaining = skb->len; | |
1270 | memcpy(dst, eth->h_dest, ETH_ALEN); | |
1271 | memcpy(src, eth->h_source, ETH_ALEN); | |
1272 | ||
1273 | padding = ((4 - subframe_len) & 0x3); | |
1274 | /* the last MSDU has no padding */ | |
1275 | if (subframe_len > remaining) { | |
1276 | printk(KERN_DEBUG "%s: wrong buffer size", dev->name); | |
1277 | return TXRX_DROP; | |
1278 | } | |
1279 | ||
1280 | skb_pull(skb, sizeof(struct ethhdr)); | |
1281 | /* if last subframe reuse skb */ | |
1282 | if (remaining <= subframe_len + padding) | |
1283 | frame = skb; | |
1284 | else { | |
1285 | frame = dev_alloc_skb(local->hw.extra_tx_headroom + | |
1286 | subframe_len); | |
1287 | ||
1288 | if (frame == NULL) | |
1289 | return TXRX_DROP; | |
1290 | ||
1291 | skb_reserve(frame, local->hw.extra_tx_headroom + | |
1292 | sizeof(struct ethhdr)); | |
1293 | memcpy(skb_put(frame, ntohs(len)), skb->data, | |
1294 | ntohs(len)); | |
1295 | ||
1296 | eth = (struct ethhdr *) skb_pull(skb, ntohs(len) + | |
1297 | padding); | |
1298 | if (!eth) { | |
1299 | printk(KERN_DEBUG "%s: wrong buffer size ", | |
1300 | dev->name); | |
1301 | dev_kfree_skb(frame); | |
1302 | return TXRX_DROP; | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | skb_set_network_header(frame, 0); | |
1307 | frame->dev = dev; | |
1308 | frame->priority = skb->priority; | |
1309 | rx->skb = frame; | |
1310 | ||
1311 | if ((ieee80211_drop_802_1x_pae(rx, 0)) || | |
1312 | (ieee80211_drop_unencrypted(rx, 0))) { | |
1313 | if (skb == frame) /* last frame */ | |
1314 | return TXRX_DROP; | |
1315 | dev_kfree_skb(frame); | |
1316 | continue; | |
1317 | } | |
1318 | ||
1319 | payload = frame->data; | |
1320 | ethertype = (payload[6] << 8) | payload[7]; | |
1321 | ||
1322 | if (likely((compare_ether_addr(payload, rfc1042_header) == 0 && | |
1323 | ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || | |
1324 | compare_ether_addr(payload, | |
1325 | bridge_tunnel_header) == 0)) { | |
1326 | /* remove RFC1042 or Bridge-Tunnel | |
1327 | * encapsulation and replace EtherType */ | |
1328 | skb_pull(frame, 6); | |
1329 | memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN); | |
1330 | memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN); | |
1331 | } else { | |
1332 | memcpy(skb_push(frame, sizeof(__be16)), &len, | |
1333 | sizeof(__be16)); | |
1334 | memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN); | |
1335 | memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN); | |
1336 | } | |
1337 | ||
1338 | ||
1339 | ieee80211_deliver_skb(rx); | |
1340 | } | |
1341 | ||
1342 | return TXRX_QUEUED; | |
1343 | } | |
1344 | ||
76ee65bf RR |
1345 | static ieee80211_txrx_result |
1346 | ieee80211_rx_h_data(struct ieee80211_txrx_data *rx) | |
1347 | { | |
1348 | struct net_device *dev = rx->dev; | |
1349 | u16 fc; | |
1350 | int err, hdrlen; | |
1351 | ||
1352 | fc = rx->fc; | |
1353 | if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) | |
1354 | return TXRX_CONTINUE; | |
1355 | ||
1356 | if (unlikely(!WLAN_FC_DATA_PRESENT(fc))) | |
1357 | return TXRX_DROP; | |
1358 | ||
1359 | hdrlen = ieee80211_get_hdrlen(fc); | |
1360 | ||
1361 | if ((ieee80211_drop_802_1x_pae(rx, hdrlen)) || | |
1362 | (ieee80211_drop_unencrypted(rx, hdrlen))) | |
1363 | return TXRX_DROP; | |
1364 | ||
1365 | err = ieee80211_data_to_8023(rx); | |
1366 | if (unlikely(err)) | |
1367 | return TXRX_DROP; | |
1368 | ||
1369 | rx->skb->dev = dev; | |
1370 | ||
1371 | dev->stats.rx_packets++; | |
1372 | dev->stats.rx_bytes += rx->skb->len; | |
1373 | ||
1374 | ieee80211_deliver_skb(rx); | |
571ecf67 JB |
1375 | |
1376 | return TXRX_QUEUED; | |
1377 | } | |
1378 | ||
1379 | static ieee80211_txrx_result | |
1380 | ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx) | |
1381 | { | |
1382 | struct ieee80211_sub_if_data *sdata; | |
1383 | ||
badffb72 | 1384 | if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) |
571ecf67 JB |
1385 | return TXRX_DROP; |
1386 | ||
1387 | sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev); | |
1388 | if ((sdata->type == IEEE80211_IF_TYPE_STA || | |
1389 | sdata->type == IEEE80211_IF_TYPE_IBSS) && | |
ddd3d2be | 1390 | !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)) |
571ecf67 | 1391 | ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status); |
f9d540ee JB |
1392 | else |
1393 | return TXRX_DROP; | |
1394 | ||
571ecf67 JB |
1395 | return TXRX_QUEUED; |
1396 | } | |
1397 | ||
1398 | static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers( | |
1399 | struct ieee80211_local *local, | |
1400 | ieee80211_rx_handler *handlers, | |
1401 | struct ieee80211_txrx_data *rx, | |
1402 | struct sta_info *sta) | |
1403 | { | |
1404 | ieee80211_rx_handler *handler; | |
1405 | ieee80211_txrx_result res = TXRX_DROP; | |
1406 | ||
1407 | for (handler = handlers; *handler != NULL; handler++) { | |
1408 | res = (*handler)(rx); | |
8e6f0032 JB |
1409 | |
1410 | switch (res) { | |
1411 | case TXRX_CONTINUE: | |
1412 | continue; | |
1413 | case TXRX_DROP: | |
1414 | I802_DEBUG_INC(local->rx_handlers_drop); | |
1415 | if (sta) | |
1416 | sta->rx_dropped++; | |
1417 | break; | |
1418 | case TXRX_QUEUED: | |
1419 | I802_DEBUG_INC(local->rx_handlers_queued); | |
571ecf67 JB |
1420 | break; |
1421 | } | |
8e6f0032 | 1422 | break; |
571ecf67 JB |
1423 | } |
1424 | ||
8e6f0032 | 1425 | if (res == TXRX_DROP) |
571ecf67 | 1426 | dev_kfree_skb(rx->skb); |
571ecf67 JB |
1427 | return res; |
1428 | } | |
1429 | ||
1430 | static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local, | |
1431 | ieee80211_rx_handler *handlers, | |
1432 | struct ieee80211_txrx_data *rx, | |
1433 | struct sta_info *sta) | |
1434 | { | |
1435 | if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) == | |
1436 | TXRX_CONTINUE) | |
1437 | dev_kfree_skb(rx->skb); | |
1438 | } | |
1439 | ||
1440 | static void ieee80211_rx_michael_mic_report(struct net_device *dev, | |
1441 | struct ieee80211_hdr *hdr, | |
1442 | struct sta_info *sta, | |
1443 | struct ieee80211_txrx_data *rx) | |
1444 | { | |
1445 | int keyidx, hdrlen; | |
0795af57 JP |
1446 | DECLARE_MAC_BUF(mac); |
1447 | DECLARE_MAC_BUF(mac2); | |
571ecf67 JB |
1448 | |
1449 | hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb); | |
1450 | if (rx->skb->len >= hdrlen + 4) | |
1451 | keyidx = rx->skb->data[hdrlen + 3] >> 6; | |
1452 | else | |
1453 | keyidx = -1; | |
1454 | ||
1a84f3fd JB |
1455 | if (net_ratelimit()) |
1456 | printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC " | |
0795af57 JP |
1457 | "failure from %s to %s keyidx=%d\n", |
1458 | dev->name, print_mac(mac, hdr->addr2), | |
1459 | print_mac(mac2, hdr->addr1), keyidx); | |
571ecf67 JB |
1460 | |
1461 | if (!sta) { | |
aa0daf0e JB |
1462 | /* |
1463 | * Some hardware seem to generate incorrect Michael MIC | |
1464 | * reports; ignore them to avoid triggering countermeasures. | |
1465 | */ | |
1a84f3fd JB |
1466 | if (net_ratelimit()) |
1467 | printk(KERN_DEBUG "%s: ignored spurious Michael MIC " | |
0795af57 JP |
1468 | "error for unknown address %s\n", |
1469 | dev->name, print_mac(mac, hdr->addr2)); | |
571ecf67 JB |
1470 | goto ignore; |
1471 | } | |
1472 | ||
1473 | if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) { | |
1a84f3fd JB |
1474 | if (net_ratelimit()) |
1475 | printk(KERN_DEBUG "%s: ignored spurious Michael MIC " | |
aa0daf0e | 1476 | "error for a frame with no PROTECTED flag (src " |
0795af57 | 1477 | "%s)\n", dev->name, print_mac(mac, hdr->addr2)); |
571ecf67 JB |
1478 | goto ignore; |
1479 | } | |
1480 | ||
7848ba7d | 1481 | if (rx->sdata->type == IEEE80211_IF_TYPE_AP && keyidx) { |
aa0daf0e JB |
1482 | /* |
1483 | * APs with pairwise keys should never receive Michael MIC | |
1484 | * errors for non-zero keyidx because these are reserved for | |
1485 | * group keys and only the AP is sending real multicast | |
1486 | * frames in the BSS. | |
1487 | */ | |
eb063c17 JB |
1488 | if (net_ratelimit()) |
1489 | printk(KERN_DEBUG "%s: ignored Michael MIC error for " | |
1490 | "a frame with non-zero keyidx (%d)" | |
0795af57 JP |
1491 | " (src %s)\n", dev->name, keyidx, |
1492 | print_mac(mac, hdr->addr2)); | |
eb063c17 | 1493 | goto ignore; |
571ecf67 JB |
1494 | } |
1495 | ||
1496 | if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA && | |
1497 | ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT || | |
1498 | (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) { | |
1a84f3fd JB |
1499 | if (net_ratelimit()) |
1500 | printk(KERN_DEBUG "%s: ignored spurious Michael MIC " | |
1501 | "error for a frame that cannot be encrypted " | |
0795af57 JP |
1502 | "(fc=0x%04x) (src %s)\n", |
1503 | dev->name, rx->fc, print_mac(mac, hdr->addr2)); | |
571ecf67 JB |
1504 | goto ignore; |
1505 | } | |
1506 | ||
eb063c17 | 1507 | mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr); |
571ecf67 JB |
1508 | ignore: |
1509 | dev_kfree_skb(rx->skb); | |
1510 | rx->skb = NULL; | |
1511 | } | |
1512 | ||
1513 | ieee80211_rx_handler ieee80211_rx_handlers[] = | |
1514 | { | |
1515 | ieee80211_rx_h_if_stats, | |
571ecf67 JB |
1516 | ieee80211_rx_h_passive_scan, |
1517 | ieee80211_rx_h_check, | |
4f0d18e2 | 1518 | ieee80211_rx_h_decrypt, |
70f08765 | 1519 | ieee80211_rx_h_sta_process, |
571ecf67 JB |
1520 | ieee80211_rx_h_defragment, |
1521 | ieee80211_rx_h_ps_poll, | |
1522 | ieee80211_rx_h_michael_mic_verify, | |
1523 | /* this must be after decryption - so header is counted in MPDU mic | |
1524 | * must be before pae and data, so QOS_DATA format frames | |
1525 | * are not passed to user space by these functions | |
1526 | */ | |
1527 | ieee80211_rx_h_remove_qos_control, | |
fd4c7f2f | 1528 | ieee80211_rx_h_amsdu, |
571ecf67 JB |
1529 | ieee80211_rx_h_data, |
1530 | ieee80211_rx_h_mgmt, | |
1531 | NULL | |
1532 | }; | |
1533 | ||
1534 | /* main receive path */ | |
1535 | ||
23a24def JB |
1536 | static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata, |
1537 | u8 *bssid, struct ieee80211_txrx_data *rx, | |
1538 | struct ieee80211_hdr *hdr) | |
1539 | { | |
1540 | int multicast = is_multicast_ether_addr(hdr->addr1); | |
1541 | ||
1542 | switch (sdata->type) { | |
1543 | case IEEE80211_IF_TYPE_STA: | |
1544 | if (!bssid) | |
1545 | return 0; | |
1546 | if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) { | |
badffb72 | 1547 | if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) |
23a24def | 1548 | return 0; |
badffb72 | 1549 | rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH; |
23a24def JB |
1550 | } else if (!multicast && |
1551 | compare_ether_addr(sdata->dev->dev_addr, | |
1552 | hdr->addr1) != 0) { | |
4150c572 | 1553 | if (!(sdata->dev->flags & IFF_PROMISC)) |
23a24def | 1554 | return 0; |
badffb72 | 1555 | rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH; |
23a24def JB |
1556 | } |
1557 | break; | |
1558 | case IEEE80211_IF_TYPE_IBSS: | |
1559 | if (!bssid) | |
1560 | return 0; | |
1561 | if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) { | |
badffb72 | 1562 | if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) |
23a24def | 1563 | return 0; |
badffb72 | 1564 | rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH; |
23a24def JB |
1565 | } else if (!multicast && |
1566 | compare_ether_addr(sdata->dev->dev_addr, | |
1567 | hdr->addr1) != 0) { | |
4150c572 | 1568 | if (!(sdata->dev->flags & IFF_PROMISC)) |
23a24def | 1569 | return 0; |
badffb72 | 1570 | rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH; |
23a24def JB |
1571 | } else if (!rx->sta) |
1572 | rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb, | |
1573 | bssid, hdr->addr2); | |
1574 | break; | |
fb1c1cd6 | 1575 | case IEEE80211_IF_TYPE_VLAN: |
23a24def JB |
1576 | case IEEE80211_IF_TYPE_AP: |
1577 | if (!bssid) { | |
1578 | if (compare_ether_addr(sdata->dev->dev_addr, | |
1579 | hdr->addr1)) | |
1580 | return 0; | |
1581 | } else if (!ieee80211_bssid_match(bssid, | |
1582 | sdata->dev->dev_addr)) { | |
badffb72 | 1583 | if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) |
23a24def | 1584 | return 0; |
badffb72 | 1585 | rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH; |
23a24def | 1586 | } |
badffb72 JS |
1587 | if (sdata->dev == sdata->local->mdev && |
1588 | !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) | |
23a24def JB |
1589 | /* do not receive anything via |
1590 | * master device when not scanning */ | |
1591 | return 0; | |
1592 | break; | |
1593 | case IEEE80211_IF_TYPE_WDS: | |
1594 | if (bssid || | |
1595 | (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) | |
1596 | return 0; | |
1597 | if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2)) | |
1598 | return 0; | |
1599 | break; | |
fb1c1cd6 JB |
1600 | case IEEE80211_IF_TYPE_MNTR: |
1601 | /* take everything */ | |
1602 | break; | |
a2897552 | 1603 | case IEEE80211_IF_TYPE_INVALID: |
fb1c1cd6 JB |
1604 | /* should never get here */ |
1605 | WARN_ON(1); | |
1606 | break; | |
23a24def JB |
1607 | } |
1608 | ||
1609 | return 1; | |
1610 | } | |
1611 | ||
571ecf67 JB |
1612 | /* |
1613 | * This is the receive path handler. It is called by a low level driver when an | |
1614 | * 802.11 MPDU is received from the hardware. | |
1615 | */ | |
1616 | void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb, | |
1617 | struct ieee80211_rx_status *status) | |
1618 | { | |
1619 | struct ieee80211_local *local = hw_to_local(hw); | |
1620 | struct ieee80211_sub_if_data *sdata; | |
1621 | struct sta_info *sta; | |
1622 | struct ieee80211_hdr *hdr; | |
1623 | struct ieee80211_txrx_data rx; | |
1624 | u16 type; | |
b2e7771e | 1625 | int prepres; |
8e6f0032 JB |
1626 | struct ieee80211_sub_if_data *prev = NULL; |
1627 | struct sk_buff *skb_new; | |
1628 | u8 *bssid; | |
d10f2150 | 1629 | int hdrlen; |
571ecf67 | 1630 | |
d4e46a3d | 1631 | /* |
79010420 JB |
1632 | * key references and virtual interfaces are protected using RCU |
1633 | * and this requires that we are in a read-side RCU section during | |
1634 | * receive processing | |
d4e46a3d JB |
1635 | */ |
1636 | rcu_read_lock(); | |
1637 | ||
b2e7771e JB |
1638 | /* |
1639 | * Frames with failed FCS/PLCP checksum are not returned, | |
1640 | * all other frames are returned without radiotap header | |
1641 | * if it was previously present. | |
1642 | * Also, frames with less than 16 bytes are dropped. | |
1643 | */ | |
1644 | skb = ieee80211_rx_monitor(local, skb, status); | |
1645 | if (!skb) { | |
1646 | rcu_read_unlock(); | |
1647 | return; | |
1648 | } | |
1649 | ||
571ecf67 JB |
1650 | hdr = (struct ieee80211_hdr *) skb->data; |
1651 | memset(&rx, 0, sizeof(rx)); | |
1652 | rx.skb = skb; | |
1653 | rx.local = local; | |
1654 | ||
1655 | rx.u.rx.status = status; | |
b2e7771e | 1656 | rx.fc = le16_to_cpu(hdr->frame_control); |
571ecf67 | 1657 | type = rx.fc & IEEE80211_FCTL_FTYPE; |
72abd81b | 1658 | |
d10f2150 DM |
1659 | /* |
1660 | * Drivers are required to align the payload data to a four-byte | |
1661 | * boundary, so the last two bits of the address where it starts | |
1662 | * may not be set. The header is required to be directly before | |
1663 | * the payload data, padding like atheros hardware adds which is | |
1664 | * inbetween the 802.11 header and the payload is not supported, | |
1665 | * the driver is required to move the 802.11 header further back | |
1666 | * in that case. | |
1667 | */ | |
1668 | hdrlen = ieee80211_get_hdrlen(rx.fc); | |
1669 | WARN_ON_ONCE(((unsigned long)(skb->data + hdrlen)) & 3); | |
1670 | ||
b2e7771e | 1671 | if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT) |
571ecf67 | 1672 | local->dot11ReceivedFragmentCount++; |
571ecf67 | 1673 | |
b2e7771e JB |
1674 | sta = rx.sta = sta_info_get(local, hdr->addr2); |
1675 | if (sta) { | |
1676 | rx.dev = rx.sta->dev; | |
1677 | rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev); | |
1678 | } | |
571ecf67 | 1679 | |
571ecf67 JB |
1680 | if ((status->flag & RX_FLAG_MMIC_ERROR)) { |
1681 | ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx); | |
1682 | goto end; | |
1683 | } | |
1684 | ||
ece8eddd | 1685 | if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning)) |
badffb72 | 1686 | rx.flags |= IEEE80211_TXRXD_RXIN_SCAN; |
571ecf67 JB |
1687 | |
1688 | if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx, | |
1689 | sta) != TXRX_CONTINUE) | |
1690 | goto end; | |
1691 | skb = rx.skb; | |
1692 | ||
c9ee23df | 1693 | if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) && |
53918994 JB |
1694 | !atomic_read(&local->iff_promiscs) && |
1695 | !is_multicast_ether_addr(hdr->addr1)) { | |
badffb72 | 1696 | rx.flags |= IEEE80211_TXRXD_RXRA_MATCH; |
571ecf67 | 1697 | ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx, |
23a24def | 1698 | rx.sta); |
8e6f0032 | 1699 | sta_info_put(sta); |
d4e46a3d | 1700 | rcu_read_unlock(); |
8e6f0032 JB |
1701 | return; |
1702 | } | |
1703 | ||
b2e7771e | 1704 | bssid = ieee80211_get_bssid(hdr, skb->len); |
8e6f0032 | 1705 | |
79010420 | 1706 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
2a8a9a88 JB |
1707 | if (!netif_running(sdata->dev)) |
1708 | continue; | |
1709 | ||
b2e7771e JB |
1710 | if (sdata->type == IEEE80211_IF_TYPE_MNTR) |
1711 | continue; | |
1712 | ||
1713 | rx.flags |= IEEE80211_TXRXD_RXRA_MATCH; | |
23a24def JB |
1714 | prepres = prepare_for_handlers(sdata, bssid, &rx, hdr); |
1715 | /* prepare_for_handlers can change sta */ | |
1716 | sta = rx.sta; | |
1717 | ||
1718 | if (!prepres) | |
1719 | continue; | |
8e6f0032 | 1720 | |
340e11f3 JB |
1721 | /* |
1722 | * frame is destined for this interface, but if it's not | |
1723 | * also for the previous one we handle that after the | |
1724 | * loop to avoid copying the SKB once too much | |
1725 | */ | |
1726 | ||
1727 | if (!prev) { | |
1728 | prev = sdata; | |
1729 | continue; | |
8e6f0032 | 1730 | } |
340e11f3 JB |
1731 | |
1732 | /* | |
1733 | * frame was destined for the previous interface | |
1734 | * so invoke RX handlers for it | |
1735 | */ | |
1736 | ||
1737 | skb_new = skb_copy(skb, GFP_ATOMIC); | |
1738 | if (!skb_new) { | |
1739 | if (net_ratelimit()) | |
1740 | printk(KERN_DEBUG "%s: failed to copy " | |
1741 | "multicast frame for %s", | |
dd1cd4c6 JB |
1742 | wiphy_name(local->hw.wiphy), |
1743 | prev->dev->name); | |
340e11f3 JB |
1744 | continue; |
1745 | } | |
1746 | rx.skb = skb_new; | |
1747 | rx.dev = prev->dev; | |
1748 | rx.sdata = prev; | |
1749 | ieee80211_invoke_rx_handlers(local, local->rx_handlers, | |
1750 | &rx, sta); | |
8e6f0032 | 1751 | prev = sdata; |
571ecf67 | 1752 | } |
8e6f0032 JB |
1753 | if (prev) { |
1754 | rx.skb = skb; | |
1755 | rx.dev = prev->dev; | |
1756 | rx.sdata = prev; | |
1757 | ieee80211_invoke_rx_handlers(local, local->rx_handlers, | |
1758 | &rx, sta); | |
1759 | } else | |
1760 | dev_kfree_skb(skb); | |
571ecf67 | 1761 | |
8e6f0032 | 1762 | end: |
d4e46a3d JB |
1763 | rcu_read_unlock(); |
1764 | ||
571ecf67 JB |
1765 | if (sta) |
1766 | sta_info_put(sta); | |
1767 | } | |
1768 | EXPORT_SYMBOL(__ieee80211_rx); | |
1769 | ||
1770 | /* This is a version of the rx handler that can be called from hard irq | |
1771 | * context. Post the skb on the queue and schedule the tasklet */ | |
1772 | void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb, | |
1773 | struct ieee80211_rx_status *status) | |
1774 | { | |
1775 | struct ieee80211_local *local = hw_to_local(hw); | |
1776 | ||
1777 | BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb)); | |
1778 | ||
1779 | skb->dev = local->mdev; | |
1780 | /* copy status into skb->cb for use by tasklet */ | |
1781 | memcpy(skb->cb, status, sizeof(*status)); | |
1782 | skb->pkt_type = IEEE80211_RX_MSG; | |
1783 | skb_queue_tail(&local->skb_queue, skb); | |
1784 | tasklet_schedule(&local->tasklet); | |
1785 | } | |
1786 | EXPORT_SYMBOL(ieee80211_rx_irqsafe); |