mac80211: generalise work handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / work.c
blob8b8961d806aba6d609a7ad9adca64a7ae86fb374
1 /*
2 * mac80211 work implementation
4 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
5 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/delay.h>
17 #include <linux/if_ether.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/etherdevice.h>
21 #include <linux/crc32.h>
22 #include <net/mac80211.h>
23 #include <asm/unaligned.h>
25 #include "ieee80211_i.h"
26 #include "rate.h"
28 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
29 #define IEEE80211_AUTH_MAX_TRIES 3
30 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
31 #define IEEE80211_ASSOC_MAX_TRIES 3
32 #define IEEE80211_MAX_PROBE_TRIES 5
34 enum work_action {
35 WORK_ACT_NONE,
36 WORK_ACT_TIMEOUT,
37 WORK_ACT_DONE,
41 /* utils */
42 static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
44 WARN_ON(!mutex_is_locked(&local->work_mtx));
48 * We can have multiple work items (and connection probing)
49 * scheduling this timer, but we need to take care to only
50 * reschedule it when it should fire _earlier_ than it was
51 * asked for before, or if it's not pending right now. This
52 * function ensures that. Note that it then is required to
53 * run this function for all timeouts after the first one
54 * has happened -- the work that runs from this timer will
55 * do that.
57 static void run_again(struct ieee80211_local *local,
58 unsigned long timeout)
60 ASSERT_WORK_MTX(local);
62 if (!timer_pending(&local->work_timer) ||
63 time_before(timeout, local->work_timer.expires))
64 mod_timer(&local->work_timer, timeout);
67 static void work_free_rcu(struct rcu_head *head)
69 struct ieee80211_work *wk =
70 container_of(head, struct ieee80211_work, rcu_head);
72 kfree(wk);
75 void free_work(struct ieee80211_work *wk)
77 call_rcu(&wk->rcu_head, work_free_rcu);
80 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
81 struct ieee80211_supported_band *sband,
82 u32 *rates)
84 int i, j, count;
85 *rates = 0;
86 count = 0;
87 for (i = 0; i < supp_rates_len; i++) {
88 int rate = (supp_rates[i] & 0x7F) * 5;
90 for (j = 0; j < sband->n_bitrates; j++)
91 if (sband->bitrates[j].bitrate == rate) {
92 *rates |= BIT(j);
93 count++;
94 break;
98 return count;
101 /* frame sending functions */
103 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
104 struct ieee80211_work *wk)
106 struct ieee80211_local *local = sdata->local;
107 struct sk_buff *skb;
108 struct ieee80211_mgmt *mgmt;
109 u8 *pos;
110 const u8 *ies, *ht_ie;
111 int i, len, count, rates_len, supp_rates_len;
112 u16 capab;
113 struct ieee80211_supported_band *sband;
114 u32 rates = 0;
116 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
117 sizeof(*mgmt) + 200 + wk->ie_len +
118 wk->assoc.ssid_len);
119 if (!skb) {
120 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
121 "frame\n", sdata->name);
122 return;
124 skb_reserve(skb, local->hw.extra_tx_headroom);
126 sband = local->hw.wiphy->bands[wk->chan->band];
128 capab = WLAN_CAPABILITY_ESS;
130 if (sband->band == IEEE80211_BAND_2GHZ) {
131 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
132 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
133 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
134 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
137 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
138 capab |= WLAN_CAPABILITY_PRIVACY;
141 * Get all rates supported by the device and the AP as
142 * some APs don't like getting a superset of their rates
143 * in the association request (e.g. D-Link DAP 1353 in
144 * b-only mode)...
146 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
147 wk->assoc.supp_rates_len,
148 sband, &rates);
150 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
151 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
152 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
154 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
155 memset(mgmt, 0, 24);
156 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
157 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
158 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
160 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
161 skb_put(skb, 10);
162 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
163 IEEE80211_STYPE_REASSOC_REQ);
164 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
165 mgmt->u.reassoc_req.listen_interval =
166 cpu_to_le16(local->hw.conf.listen_interval);
167 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
168 ETH_ALEN);
169 } else {
170 skb_put(skb, 4);
171 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
172 IEEE80211_STYPE_ASSOC_REQ);
173 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
174 mgmt->u.assoc_req.listen_interval =
175 cpu_to_le16(local->hw.conf.listen_interval);
178 /* SSID */
179 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
180 *pos++ = WLAN_EID_SSID;
181 *pos++ = wk->assoc.ssid_len;
182 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
184 /* add all rates which were marked to be used above */
185 supp_rates_len = rates_len;
186 if (supp_rates_len > 8)
187 supp_rates_len = 8;
189 len = sband->n_bitrates;
190 pos = skb_put(skb, supp_rates_len + 2);
191 *pos++ = WLAN_EID_SUPP_RATES;
192 *pos++ = supp_rates_len;
194 count = 0;
195 for (i = 0; i < sband->n_bitrates; i++) {
196 if (BIT(i) & rates) {
197 int rate = sband->bitrates[i].bitrate;
198 *pos++ = (u8) (rate / 5);
199 if (++count == 8)
200 break;
204 if (rates_len > count) {
205 pos = skb_put(skb, rates_len - count + 2);
206 *pos++ = WLAN_EID_EXT_SUPP_RATES;
207 *pos++ = rates_len - count;
209 for (i++; i < sband->n_bitrates; i++) {
210 if (BIT(i) & rates) {
211 int rate = sband->bitrates[i].bitrate;
212 *pos++ = (u8) (rate / 5);
217 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
218 /* 1. power capabilities */
219 pos = skb_put(skb, 4);
220 *pos++ = WLAN_EID_PWR_CAPABILITY;
221 *pos++ = 2;
222 *pos++ = 0; /* min tx power */
223 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
225 /* 2. supported channels */
226 /* TODO: get this in reg domain format */
227 pos = skb_put(skb, 2 * sband->n_channels + 2);
228 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
229 *pos++ = 2 * sband->n_channels;
230 for (i = 0; i < sband->n_channels; i++) {
231 *pos++ = ieee80211_frequency_to_channel(
232 sband->channels[i].center_freq);
233 *pos++ = 1; /* one channel in the subband*/
237 if (wk->ie_len && wk->ie) {
238 pos = skb_put(skb, wk->ie_len);
239 memcpy(pos, wk->ie, wk->ie_len);
242 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
243 pos = skb_put(skb, 9);
244 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
245 *pos++ = 7; /* len */
246 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
247 *pos++ = 0x50;
248 *pos++ = 0xf2;
249 *pos++ = 2; /* WME */
250 *pos++ = 0; /* WME info */
251 *pos++ = 1; /* WME ver */
252 *pos++ = 0;
255 /* wmm support is a must to HT */
257 * IEEE802.11n does not allow TKIP/WEP as pairwise
258 * ciphers in HT mode. We still associate in non-ht
259 * mode (11a/b/g) if any one of these ciphers is
260 * configured as pairwise.
262 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
263 (local->hw.queues >= 4) &&
264 sband->ht_cap.ht_supported &&
265 (ht_ie = wk->assoc.ht_information_ie) &&
266 ht_ie[1] >= sizeof(struct ieee80211_ht_info)) {
267 struct ieee80211_ht_info *ht_info =
268 (struct ieee80211_ht_info *)(ht_ie + 2);
269 u16 cap = sband->ht_cap.cap;
270 __le16 tmp;
271 u32 flags = local->hw.conf.channel->flags;
273 /* determine capability flags */
275 if (ieee80211_disable_40mhz_24ghz &&
276 sband->band == IEEE80211_BAND_2GHZ) {
277 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
278 cap &= ~IEEE80211_HT_CAP_SGI_40;
281 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
282 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
283 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
284 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
285 cap &= ~IEEE80211_HT_CAP_SGI_40;
287 break;
288 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
289 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
290 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
291 cap &= ~IEEE80211_HT_CAP_SGI_40;
293 break;
296 /* set SM PS mode properly */
297 cap &= ~IEEE80211_HT_CAP_SM_PS;
298 switch (wk->assoc.smps) {
299 case IEEE80211_SMPS_AUTOMATIC:
300 case IEEE80211_SMPS_NUM_MODES:
301 WARN_ON(1);
302 case IEEE80211_SMPS_OFF:
303 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
304 IEEE80211_HT_CAP_SM_PS_SHIFT;
305 break;
306 case IEEE80211_SMPS_STATIC:
307 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
308 IEEE80211_HT_CAP_SM_PS_SHIFT;
309 break;
310 case IEEE80211_SMPS_DYNAMIC:
311 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
312 IEEE80211_HT_CAP_SM_PS_SHIFT;
313 break;
316 /* reserve and fill IE */
318 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
319 *pos++ = WLAN_EID_HT_CAPABILITY;
320 *pos++ = sizeof(struct ieee80211_ht_cap);
321 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
323 /* capability flags */
324 tmp = cpu_to_le16(cap);
325 memcpy(pos, &tmp, sizeof(u16));
326 pos += sizeof(u16);
328 /* AMPDU parameters */
329 *pos++ = sband->ht_cap.ampdu_factor |
330 (sband->ht_cap.ampdu_density <<
331 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
333 /* MCS set */
334 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
335 pos += sizeof(sband->ht_cap.mcs);
337 /* extended capabilities */
338 pos += sizeof(__le16);
340 /* BF capabilities */
341 pos += sizeof(__le32);
343 /* antenna selection */
344 pos += sizeof(u8);
347 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
348 ieee80211_tx_skb(sdata, skb);
351 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
352 struct ieee80211_work *wk)
354 struct cfg80211_bss *cbss;
355 u16 capa_val = WLAN_CAPABILITY_ESS;
357 if (wk->probe_auth.privacy)
358 capa_val |= WLAN_CAPABILITY_PRIVACY;
360 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
361 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
362 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
363 capa_val);
364 if (!cbss)
365 return;
367 cfg80211_unlink_bss(local->hw.wiphy, cbss);
368 cfg80211_put_bss(cbss);
371 static enum work_action __must_check
372 ieee80211_direct_probe(struct ieee80211_work *wk)
374 struct ieee80211_sub_if_data *sdata = wk->sdata;
375 struct ieee80211_local *local = sdata->local;
377 wk->probe_auth.tries++;
378 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
379 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
380 sdata->name, wk->filter_ta);
383 * Most likely AP is not in the range so remove the
384 * bss struct for that AP.
386 ieee80211_remove_auth_bss(local, wk);
389 * We might have a pending scan which had no chance to run yet
390 * due to work needing to be done. Hence, queue the STAs work
391 * again for that.
393 ieee80211_queue_work(&local->hw, &local->work_work);
394 return WORK_ACT_TIMEOUT;
397 printk(KERN_DEBUG "%s: direct probe to AP %pM (try %d)\n",
398 sdata->name, wk->filter_ta, wk->probe_auth.tries);
401 * Direct probe is sent to broadcast address as some APs
402 * will not answer to direct packet in unassociated state.
404 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
405 wk->probe_auth.ssid_len, NULL, 0);
407 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
408 run_again(local, wk->timeout);
410 return WORK_ACT_NONE;
414 static enum work_action __must_check
415 ieee80211_authenticate(struct ieee80211_work *wk)
417 struct ieee80211_sub_if_data *sdata = wk->sdata;
418 struct ieee80211_local *local = sdata->local;
420 wk->probe_auth.tries++;
421 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
422 printk(KERN_DEBUG "%s: authentication with AP %pM"
423 " timed out\n", sdata->name, wk->filter_ta);
426 * Most likely AP is not in the range so remove the
427 * bss struct for that AP.
429 ieee80211_remove_auth_bss(local, wk);
432 * We might have a pending scan which had no chance to run yet
433 * due to work needing to be done. Hence, queue the STAs work
434 * again for that.
436 ieee80211_queue_work(&local->hw, &local->work_work);
437 return WORK_ACT_TIMEOUT;
440 printk(KERN_DEBUG "%s: authenticate with AP %pM (try %d)\n",
441 sdata->name, wk->filter_ta, wk->probe_auth.tries);
443 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
444 wk->ie_len, wk->filter_ta, NULL, 0, 0);
445 wk->probe_auth.transaction = 2;
447 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
448 run_again(local, wk->timeout);
450 return WORK_ACT_NONE;
453 static enum work_action __must_check
454 ieee80211_associate(struct ieee80211_work *wk)
456 struct ieee80211_sub_if_data *sdata = wk->sdata;
457 struct ieee80211_local *local = sdata->local;
459 wk->assoc.tries++;
460 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
461 printk(KERN_DEBUG "%s: association with AP %pM"
462 " timed out\n",
463 sdata->name, wk->filter_ta);
466 * Most likely AP is not in the range so remove the
467 * bss struct for that AP.
469 if (wk->assoc.bss)
470 cfg80211_unlink_bss(local->hw.wiphy,
471 &wk->assoc.bss->cbss);
474 * We might have a pending scan which had no chance to run yet
475 * due to work needing to be done. Hence, queue the STAs work
476 * again for that.
478 ieee80211_queue_work(&local->hw, &local->work_work);
479 return WORK_ACT_TIMEOUT;
482 printk(KERN_DEBUG "%s: associate with AP %pM (try %d)\n",
483 sdata->name, wk->filter_ta, wk->assoc.tries);
484 ieee80211_send_assoc(sdata, wk);
486 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
487 run_again(local, wk->timeout);
489 return WORK_ACT_NONE;
492 static void ieee80211_auth_challenge(struct ieee80211_work *wk,
493 struct ieee80211_mgmt *mgmt,
494 size_t len)
496 struct ieee80211_sub_if_data *sdata = wk->sdata;
497 u8 *pos;
498 struct ieee802_11_elems elems;
500 pos = mgmt->u.auth.variable;
501 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
502 if (!elems.challenge)
503 return;
504 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
505 elems.challenge - 2, elems.challenge_len + 2,
506 wk->filter_ta, wk->probe_auth.key,
507 wk->probe_auth.key_len, wk->probe_auth.key_idx);
508 wk->probe_auth.transaction = 4;
511 static enum work_action __must_check
512 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
513 struct ieee80211_mgmt *mgmt, size_t len)
515 u16 auth_alg, auth_transaction, status_code;
517 if (wk->type != IEEE80211_WORK_AUTH)
518 return WORK_ACT_NONE;
520 if (len < 24 + 6)
521 return WORK_ACT_NONE;
523 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
524 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
525 status_code = le16_to_cpu(mgmt->u.auth.status_code);
527 if (auth_alg != wk->probe_auth.algorithm ||
528 auth_transaction != wk->probe_auth.transaction)
529 return WORK_ACT_NONE;
531 if (status_code != WLAN_STATUS_SUCCESS) {
532 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
533 wk->sdata->name, mgmt->sa, status_code);
534 return WORK_ACT_DONE;
537 switch (wk->probe_auth.algorithm) {
538 case WLAN_AUTH_OPEN:
539 case WLAN_AUTH_LEAP:
540 case WLAN_AUTH_FT:
541 break;
542 case WLAN_AUTH_SHARED_KEY:
543 if (wk->probe_auth.transaction != 4) {
544 ieee80211_auth_challenge(wk, mgmt, len);
545 /* need another frame */
546 return WORK_ACT_NONE;
548 break;
549 default:
550 WARN_ON(1);
551 return WORK_ACT_NONE;
554 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
555 return WORK_ACT_DONE;
558 static enum work_action __must_check
559 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
560 struct ieee80211_mgmt *mgmt, size_t len,
561 bool reassoc)
563 struct ieee80211_sub_if_data *sdata = wk->sdata;
564 struct ieee80211_local *local = sdata->local;
565 u16 capab_info, status_code, aid;
566 struct ieee802_11_elems elems;
567 u8 *pos;
570 * AssocResp and ReassocResp have identical structure, so process both
571 * of them in this function.
574 if (len < 24 + 6)
575 return WORK_ACT_NONE;
577 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
578 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
579 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
581 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
582 "status=%d aid=%d)\n",
583 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
584 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
586 pos = mgmt->u.assoc_resp.variable;
587 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
589 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
590 elems.timeout_int && elems.timeout_int_len == 5 &&
591 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
592 u32 tu, ms;
593 tu = get_unaligned_le32(elems.timeout_int + 1);
594 ms = tu * 1024 / 1000;
595 printk(KERN_DEBUG "%s: AP rejected association temporarily; "
596 "comeback duration %u TU (%u ms)\n",
597 sdata->name, tu, ms);
598 wk->timeout = jiffies + msecs_to_jiffies(ms);
599 if (ms > IEEE80211_ASSOC_TIMEOUT)
600 run_again(local, wk->timeout);
601 return WORK_ACT_NONE;
604 if (status_code != WLAN_STATUS_SUCCESS)
605 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
606 sdata->name, status_code);
607 else
608 printk(KERN_DEBUG "%s: associated\n", sdata->name);
610 return WORK_ACT_DONE;
613 static enum work_action __must_check
614 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
615 struct ieee80211_mgmt *mgmt, size_t len,
616 struct ieee80211_rx_status *rx_status)
618 struct ieee80211_sub_if_data *sdata = wk->sdata;
619 struct ieee80211_local *local = sdata->local;
620 size_t baselen;
622 ASSERT_WORK_MTX(local);
624 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
625 if (baselen > len)
626 return WORK_ACT_NONE;
628 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
629 return WORK_ACT_DONE;
632 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
633 struct sk_buff *skb)
635 struct ieee80211_rx_status *rx_status;
636 struct ieee80211_mgmt *mgmt;
637 struct ieee80211_work *wk;
638 enum work_action rma = WORK_ACT_NONE;
639 u16 fc;
641 rx_status = (struct ieee80211_rx_status *) skb->cb;
642 mgmt = (struct ieee80211_mgmt *) skb->data;
643 fc = le16_to_cpu(mgmt->frame_control);
645 mutex_lock(&local->work_mtx);
647 list_for_each_entry(wk, &local->work_list, list) {
648 const u8 *bssid = NULL;
650 switch (wk->type) {
651 case IEEE80211_WORK_DIRECT_PROBE:
652 case IEEE80211_WORK_AUTH:
653 case IEEE80211_WORK_ASSOC:
654 bssid = wk->filter_ta;
655 break;
656 default:
657 continue;
661 * Before queuing, we already verified mgmt->sa,
662 * so this is needed just for matching.
664 if (compare_ether_addr(bssid, mgmt->bssid))
665 continue;
667 switch (fc & IEEE80211_FCTL_STYPE) {
668 case IEEE80211_STYPE_PROBE_RESP:
669 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
670 rx_status);
671 break;
672 case IEEE80211_STYPE_AUTH:
673 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
674 break;
675 case IEEE80211_STYPE_ASSOC_RESP:
676 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
677 skb->len, false);
678 break;
679 case IEEE80211_STYPE_REASSOC_RESP:
680 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
681 skb->len, true);
682 break;
683 default:
684 WARN_ON(1);
687 * We've processed this frame for that work, so it can't
688 * belong to another work struct.
689 * NB: this is also required for correctness for 'rma'!
691 break;
694 switch (rma) {
695 case WORK_ACT_NONE:
696 break;
697 case WORK_ACT_DONE:
698 list_del_rcu(&wk->list);
699 break;
700 default:
701 WARN(1, "unexpected: %d", rma);
704 mutex_unlock(&local->work_mtx);
706 if (rma != WORK_ACT_DONE)
707 goto out;
709 switch (wk->done(wk, skb)) {
710 case WORK_DONE_DESTROY:
711 free_work(wk);
712 break;
713 case WORK_DONE_REQUEUE:
714 synchronize_rcu();
715 wk->timeout = jiffies; /* run again directly */
716 mutex_lock(&local->work_mtx);
717 list_add_tail(&wk->list, &local->work_list);
718 mutex_unlock(&local->work_mtx);
721 out:
722 kfree_skb(skb);
725 static void ieee80211_work_timer(unsigned long data)
727 struct ieee80211_local *local = (void *) data;
729 if (local->quiescing)
730 return;
732 ieee80211_queue_work(&local->hw, &local->work_work);
735 static void ieee80211_work_work(struct work_struct *work)
737 struct ieee80211_local *local =
738 container_of(work, struct ieee80211_local, work_work);
739 struct sk_buff *skb;
740 struct ieee80211_work *wk, *tmp;
741 LIST_HEAD(free_work);
742 enum work_action rma;
744 if (local->scanning)
745 return;
748 * ieee80211_queue_work() should have picked up most cases,
749 * here we'll pick the the rest.
751 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
752 return;
754 /* first process frames to avoid timing out while a frame is pending */
755 while ((skb = skb_dequeue(&local->work_skb_queue)))
756 ieee80211_work_rx_queued_mgmt(local, skb);
758 ieee80211_recalc_idle(local);
760 mutex_lock(&local->work_mtx);
762 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
763 if (time_is_after_jiffies(wk->timeout)) {
765 * This work item isn't supposed to be worked on
766 * right now, but take care to adjust the timer
767 * properly.
769 run_again(local, wk->timeout);
770 continue;
773 switch (wk->type) {
774 default:
775 WARN_ON(1);
776 /* nothing */
777 rma = WORK_ACT_NONE;
778 break;
779 case IEEE80211_WORK_DIRECT_PROBE:
780 rma = ieee80211_direct_probe(wk);
781 break;
782 case IEEE80211_WORK_AUTH:
783 rma = ieee80211_authenticate(wk);
784 break;
785 case IEEE80211_WORK_ASSOC:
786 rma = ieee80211_associate(wk);
787 break;
790 switch (rma) {
791 case WORK_ACT_NONE:
792 /* no action required */
793 break;
794 case WORK_ACT_TIMEOUT:
795 list_del_rcu(&wk->list);
796 synchronize_rcu();
797 list_add(&wk->list, &free_work);
798 break;
799 default:
800 WARN(1, "unexpected: %d", rma);
804 if (list_empty(&local->work_list) && local->scan_req)
805 ieee80211_queue_delayed_work(&local->hw,
806 &local->scan_work,
807 round_jiffies_relative(0));
809 mutex_unlock(&local->work_mtx);
811 list_for_each_entry_safe(wk, tmp, &free_work, list) {
812 wk->done(wk, NULL);
813 list_del(&wk->list);
814 kfree(wk);
818 void ieee80211_add_work(struct ieee80211_work *wk)
820 struct ieee80211_local *local;
822 if (WARN_ON(!wk->chan))
823 return;
825 if (WARN_ON(!wk->sdata))
826 return;
828 if (WARN_ON(!wk->done))
829 return;
831 wk->timeout = jiffies;
833 local = wk->sdata->local;
834 mutex_lock(&local->work_mtx);
835 list_add_tail(&wk->list, &local->work_list);
836 mutex_unlock(&local->work_mtx);
838 ieee80211_queue_work(&local->hw, &local->work_work);
841 void ieee80211_work_init(struct ieee80211_local *local)
843 mutex_init(&local->work_mtx);
844 INIT_LIST_HEAD(&local->work_list);
845 setup_timer(&local->work_timer, ieee80211_work_timer,
846 (unsigned long)local);
847 INIT_WORK(&local->work_work, ieee80211_work_work);
848 skb_queue_head_init(&local->work_skb_queue);
851 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
853 struct ieee80211_local *local = sdata->local;
854 struct ieee80211_work *wk, *tmp;
856 mutex_lock(&local->work_mtx);
857 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
858 if (wk->sdata != sdata)
859 continue;
860 list_del(&wk->list);
861 free_work(wk);
863 mutex_unlock(&local->work_mtx);
866 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
867 struct sk_buff *skb)
869 struct ieee80211_local *local = sdata->local;
870 struct ieee80211_mgmt *mgmt;
871 struct ieee80211_work *wk;
872 u16 fc;
874 if (skb->len < 24)
875 return RX_DROP_MONITOR;
877 mgmt = (struct ieee80211_mgmt *) skb->data;
878 fc = le16_to_cpu(mgmt->frame_control);
880 list_for_each_entry_rcu(wk, &local->work_list, list) {
881 if (sdata != wk->sdata)
882 continue;
883 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
884 continue;
885 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
886 continue;
888 switch (fc & IEEE80211_FCTL_STYPE) {
889 case IEEE80211_STYPE_AUTH:
890 case IEEE80211_STYPE_PROBE_RESP:
891 case IEEE80211_STYPE_ASSOC_RESP:
892 case IEEE80211_STYPE_REASSOC_RESP:
893 case IEEE80211_STYPE_DEAUTH:
894 case IEEE80211_STYPE_DISASSOC:
895 skb_queue_tail(&local->work_skb_queue, skb);
896 ieee80211_queue_work(&local->hw, &local->work_work);
897 return RX_QUEUED;
901 return RX_CONTINUE;