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