mac80211: implement off-channel mgmt TX
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / work.c
blob2b5c3f267198d256ec2a40c99c8e0539027ad71a
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 <linux/slab.h>
23 #include <net/mac80211.h>
24 #include <asm/unaligned.h>
26 #include "ieee80211_i.h"
27 #include "rate.h"
29 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
30 #define IEEE80211_AUTH_MAX_TRIES 3
31 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
32 #define IEEE80211_ASSOC_MAX_TRIES 3
33 #define IEEE80211_MAX_PROBE_TRIES 5
35 enum work_action {
36 WORK_ACT_MISMATCH,
37 WORK_ACT_NONE,
38 WORK_ACT_TIMEOUT,
39 WORK_ACT_DONE,
43 /* utils */
44 static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
46 lockdep_assert_held(&local->mtx);
50 * We can have multiple work items (and connection probing)
51 * scheduling this timer, but we need to take care to only
52 * reschedule it when it should fire _earlier_ than it was
53 * asked for before, or if it's not pending right now. This
54 * function ensures that. Note that it then is required to
55 * run this function for all timeouts after the first one
56 * has happened -- the work that runs from this timer will
57 * do that.
59 static void run_again(struct ieee80211_local *local,
60 unsigned long timeout)
62 ASSERT_WORK_MTX(local);
64 if (!timer_pending(&local->work_timer) ||
65 time_before(timeout, local->work_timer.expires))
66 mod_timer(&local->work_timer, timeout);
69 static void work_free_rcu(struct rcu_head *head)
71 struct ieee80211_work *wk =
72 container_of(head, struct ieee80211_work, rcu_head);
74 kfree(wk);
77 void free_work(struct ieee80211_work *wk)
79 call_rcu(&wk->rcu_head, work_free_rcu);
82 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
83 struct ieee80211_supported_band *sband,
84 u32 *rates)
86 int i, j, count;
87 *rates = 0;
88 count = 0;
89 for (i = 0; i < supp_rates_len; i++) {
90 int rate = (supp_rates[i] & 0x7F) * 5;
92 for (j = 0; j < sband->n_bitrates; j++)
93 if (sband->bitrates[j].bitrate == rate) {
94 *rates |= BIT(j);
95 count++;
96 break;
100 return count;
103 /* frame sending functions */
105 static void ieee80211_add_ht_ie(struct sk_buff *skb, const u8 *ht_info_ie,
106 struct ieee80211_supported_band *sband,
107 struct ieee80211_channel *channel,
108 enum ieee80211_smps_mode smps)
110 struct ieee80211_ht_info *ht_info;
111 u8 *pos;
112 u32 flags = channel->flags;
113 u16 cap = sband->ht_cap.cap;
114 __le16 tmp;
116 if (!sband->ht_cap.ht_supported)
117 return;
119 if (!ht_info_ie)
120 return;
122 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
123 return;
125 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
127 /* determine capability flags */
129 if (ieee80211_disable_40mhz_24ghz &&
130 sband->band == IEEE80211_BAND_2GHZ) {
131 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
132 cap &= ~IEEE80211_HT_CAP_SGI_40;
135 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
136 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
137 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
138 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
139 cap &= ~IEEE80211_HT_CAP_SGI_40;
141 break;
142 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
143 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
144 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
145 cap &= ~IEEE80211_HT_CAP_SGI_40;
147 break;
150 /* set SM PS mode properly */
151 cap &= ~IEEE80211_HT_CAP_SM_PS;
152 switch (smps) {
153 case IEEE80211_SMPS_AUTOMATIC:
154 case IEEE80211_SMPS_NUM_MODES:
155 WARN_ON(1);
156 case IEEE80211_SMPS_OFF:
157 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
158 IEEE80211_HT_CAP_SM_PS_SHIFT;
159 break;
160 case IEEE80211_SMPS_STATIC:
161 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
162 IEEE80211_HT_CAP_SM_PS_SHIFT;
163 break;
164 case IEEE80211_SMPS_DYNAMIC:
165 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
166 IEEE80211_HT_CAP_SM_PS_SHIFT;
167 break;
170 /* reserve and fill IE */
172 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
173 *pos++ = WLAN_EID_HT_CAPABILITY;
174 *pos++ = sizeof(struct ieee80211_ht_cap);
175 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
177 /* capability flags */
178 tmp = cpu_to_le16(cap);
179 memcpy(pos, &tmp, sizeof(u16));
180 pos += sizeof(u16);
182 /* AMPDU parameters */
183 *pos++ = sband->ht_cap.ampdu_factor |
184 (sband->ht_cap.ampdu_density <<
185 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
187 /* MCS set */
188 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
189 pos += sizeof(sband->ht_cap.mcs);
191 /* extended capabilities */
192 pos += sizeof(__le16);
194 /* BF capabilities */
195 pos += sizeof(__le32);
197 /* antenna selection */
198 pos += sizeof(u8);
201 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
202 struct ieee80211_work *wk)
204 struct ieee80211_local *local = sdata->local;
205 struct sk_buff *skb;
206 struct ieee80211_mgmt *mgmt;
207 u8 *pos, qos_info;
208 const u8 *ies;
209 size_t offset = 0, noffset;
210 int i, len, count, rates_len, supp_rates_len;
211 u16 capab;
212 struct ieee80211_supported_band *sband;
213 u32 rates = 0;
215 sband = local->hw.wiphy->bands[wk->chan->band];
217 if (wk->assoc.supp_rates_len) {
219 * Get all rates supported by the device and the AP as
220 * some APs don't like getting a superset of their rates
221 * in the association request (e.g. D-Link DAP 1353 in
222 * b-only mode)...
224 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
225 wk->assoc.supp_rates_len,
226 sband, &rates);
227 } else {
229 * In case AP not provide any supported rates information
230 * before association, we send information element(s) with
231 * all rates that we support.
233 rates = ~0;
234 rates_len = sband->n_bitrates;
237 skb = alloc_skb(local->hw.extra_tx_headroom +
238 sizeof(*mgmt) + /* bit too much but doesn't matter */
239 2 + wk->assoc.ssid_len + /* SSID */
240 4 + rates_len + /* (extended) rates */
241 4 + /* power capability */
242 2 + 2 * sband->n_channels + /* supported channels */
243 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
244 wk->ie_len + /* extra IEs */
245 9, /* WMM */
246 GFP_KERNEL);
247 if (!skb) {
248 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
249 "frame\n", sdata->name);
250 return;
252 skb_reserve(skb, local->hw.extra_tx_headroom);
254 capab = WLAN_CAPABILITY_ESS;
256 if (sband->band == IEEE80211_BAND_2GHZ) {
257 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
258 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
259 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
260 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
263 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
264 capab |= WLAN_CAPABILITY_PRIVACY;
266 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
267 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
268 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
270 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
271 memset(mgmt, 0, 24);
272 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
273 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
274 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
276 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
277 skb_put(skb, 10);
278 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
279 IEEE80211_STYPE_REASSOC_REQ);
280 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
281 mgmt->u.reassoc_req.listen_interval =
282 cpu_to_le16(local->hw.conf.listen_interval);
283 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
284 ETH_ALEN);
285 } else {
286 skb_put(skb, 4);
287 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
288 IEEE80211_STYPE_ASSOC_REQ);
289 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
290 mgmt->u.assoc_req.listen_interval =
291 cpu_to_le16(local->hw.conf.listen_interval);
294 /* SSID */
295 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
296 *pos++ = WLAN_EID_SSID;
297 *pos++ = wk->assoc.ssid_len;
298 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
300 /* add all rates which were marked to be used above */
301 supp_rates_len = rates_len;
302 if (supp_rates_len > 8)
303 supp_rates_len = 8;
305 len = sband->n_bitrates;
306 pos = skb_put(skb, supp_rates_len + 2);
307 *pos++ = WLAN_EID_SUPP_RATES;
308 *pos++ = supp_rates_len;
310 count = 0;
311 for (i = 0; i < sband->n_bitrates; i++) {
312 if (BIT(i) & rates) {
313 int rate = sband->bitrates[i].bitrate;
314 *pos++ = (u8) (rate / 5);
315 if (++count == 8)
316 break;
320 if (rates_len > count) {
321 pos = skb_put(skb, rates_len - count + 2);
322 *pos++ = WLAN_EID_EXT_SUPP_RATES;
323 *pos++ = rates_len - count;
325 for (i++; i < sband->n_bitrates; i++) {
326 if (BIT(i) & rates) {
327 int rate = sband->bitrates[i].bitrate;
328 *pos++ = (u8) (rate / 5);
333 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
334 /* 1. power capabilities */
335 pos = skb_put(skb, 4);
336 *pos++ = WLAN_EID_PWR_CAPABILITY;
337 *pos++ = 2;
338 *pos++ = 0; /* min tx power */
339 *pos++ = wk->chan->max_power; /* max tx power */
341 /* 2. supported channels */
342 /* TODO: get this in reg domain format */
343 pos = skb_put(skb, 2 * sband->n_channels + 2);
344 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
345 *pos++ = 2 * sband->n_channels;
346 for (i = 0; i < sband->n_channels; i++) {
347 *pos++ = ieee80211_frequency_to_channel(
348 sband->channels[i].center_freq);
349 *pos++ = 1; /* one channel in the subband*/
353 /* if present, add any custom IEs that go before HT */
354 if (wk->ie_len && wk->ie) {
355 static const u8 before_ht[] = {
356 WLAN_EID_SSID,
357 WLAN_EID_SUPP_RATES,
358 WLAN_EID_EXT_SUPP_RATES,
359 WLAN_EID_PWR_CAPABILITY,
360 WLAN_EID_SUPPORTED_CHANNELS,
361 WLAN_EID_RSN,
362 WLAN_EID_QOS_CAPA,
363 WLAN_EID_RRM_ENABLED_CAPABILITIES,
364 WLAN_EID_MOBILITY_DOMAIN,
365 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
367 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
368 before_ht, ARRAY_SIZE(before_ht),
369 offset);
370 pos = skb_put(skb, noffset - offset);
371 memcpy(pos, wk->ie + offset, noffset - offset);
372 offset = noffset;
375 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
376 local->hw.queues >= 4)
377 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
378 sband, wk->chan, wk->assoc.smps);
380 /* if present, add any custom non-vendor IEs that go after HT */
381 if (wk->ie_len && wk->ie) {
382 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
383 offset);
384 pos = skb_put(skb, noffset - offset);
385 memcpy(pos, wk->ie + offset, noffset - offset);
386 offset = noffset;
389 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
390 if (wk->assoc.uapsd_used) {
391 qos_info = local->uapsd_queues;
392 qos_info |= (local->uapsd_max_sp_len <<
393 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
394 } else {
395 qos_info = 0;
398 pos = skb_put(skb, 9);
399 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
400 *pos++ = 7; /* len */
401 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
402 *pos++ = 0x50;
403 *pos++ = 0xf2;
404 *pos++ = 2; /* WME */
405 *pos++ = 0; /* WME info */
406 *pos++ = 1; /* WME ver */
407 *pos++ = qos_info;
410 /* add any remaining custom (i.e. vendor specific here) IEs */
411 if (wk->ie_len && wk->ie) {
412 noffset = wk->ie_len;
413 pos = skb_put(skb, noffset - offset);
414 memcpy(pos, wk->ie + offset, noffset - offset);
417 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
418 ieee80211_tx_skb(sdata, skb);
421 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
422 struct ieee80211_work *wk)
424 struct cfg80211_bss *cbss;
425 u16 capa_val = WLAN_CAPABILITY_ESS;
427 if (wk->probe_auth.privacy)
428 capa_val |= WLAN_CAPABILITY_PRIVACY;
430 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
431 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
432 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
433 capa_val);
434 if (!cbss)
435 return;
437 cfg80211_unlink_bss(local->hw.wiphy, cbss);
438 cfg80211_put_bss(cbss);
441 static enum work_action __must_check
442 ieee80211_direct_probe(struct ieee80211_work *wk)
444 struct ieee80211_sub_if_data *sdata = wk->sdata;
445 struct ieee80211_local *local = sdata->local;
447 wk->probe_auth.tries++;
448 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
449 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
450 sdata->name, wk->filter_ta);
453 * Most likely AP is not in the range so remove the
454 * bss struct for that AP.
456 ieee80211_remove_auth_bss(local, wk);
458 return WORK_ACT_TIMEOUT;
461 printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
462 sdata->name, wk->filter_ta, wk->probe_auth.tries);
465 * Direct probe is sent to broadcast address as some APs
466 * will not answer to direct packet in unassociated state.
468 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
469 wk->probe_auth.ssid_len, NULL, 0);
471 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
472 run_again(local, wk->timeout);
474 return WORK_ACT_NONE;
478 static enum work_action __must_check
479 ieee80211_authenticate(struct ieee80211_work *wk)
481 struct ieee80211_sub_if_data *sdata = wk->sdata;
482 struct ieee80211_local *local = sdata->local;
484 wk->probe_auth.tries++;
485 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
486 printk(KERN_DEBUG "%s: authentication with %pM"
487 " timed out\n", sdata->name, wk->filter_ta);
490 * Most likely AP is not in the range so remove the
491 * bss struct for that AP.
493 ieee80211_remove_auth_bss(local, wk);
495 return WORK_ACT_TIMEOUT;
498 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
499 sdata->name, wk->filter_ta, wk->probe_auth.tries);
501 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
502 wk->ie_len, wk->filter_ta, NULL, 0, 0);
503 wk->probe_auth.transaction = 2;
505 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
506 run_again(local, wk->timeout);
508 return WORK_ACT_NONE;
511 static enum work_action __must_check
512 ieee80211_associate(struct ieee80211_work *wk)
514 struct ieee80211_sub_if_data *sdata = wk->sdata;
515 struct ieee80211_local *local = sdata->local;
517 wk->assoc.tries++;
518 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
519 printk(KERN_DEBUG "%s: association with %pM"
520 " timed out\n",
521 sdata->name, wk->filter_ta);
524 * Most likely AP is not in the range so remove the
525 * bss struct for that AP.
527 if (wk->assoc.bss)
528 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
530 return WORK_ACT_TIMEOUT;
533 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
534 sdata->name, wk->filter_ta, wk->assoc.tries);
535 ieee80211_send_assoc(sdata, wk);
537 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
538 run_again(local, wk->timeout);
540 return WORK_ACT_NONE;
543 static enum work_action __must_check
544 ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
547 * First time we run, do nothing -- the generic code will
548 * have switched to the right channel etc.
550 if (!wk->started) {
551 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
553 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
554 wk->chan, wk->chan_type,
555 wk->remain.duration, GFP_KERNEL);
557 return WORK_ACT_NONE;
560 return WORK_ACT_TIMEOUT;
563 static enum work_action __must_check
564 ieee80211_offchannel_tx(struct ieee80211_work *wk)
566 if (!wk->started) {
567 wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);
570 * After this, offchan_tx.frame remains but now is no
571 * longer a valid pointer -- we still need it as the
572 * cookie for canceling this work.
574 ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);
576 return WORK_ACT_NONE;
579 return WORK_ACT_TIMEOUT;
582 static enum work_action __must_check
583 ieee80211_assoc_beacon_wait(struct ieee80211_work *wk)
585 if (wk->started)
586 return WORK_ACT_TIMEOUT;
589 * Wait up to one beacon interval ...
590 * should this be more if we miss one?
592 printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
593 wk->sdata->name, wk->filter_ta);
594 wk->timeout = TU_TO_EXP_TIME(wk->assoc.bss->beacon_interval);
595 return WORK_ACT_NONE;
598 static void ieee80211_auth_challenge(struct ieee80211_work *wk,
599 struct ieee80211_mgmt *mgmt,
600 size_t len)
602 struct ieee80211_sub_if_data *sdata = wk->sdata;
603 u8 *pos;
604 struct ieee802_11_elems elems;
606 pos = mgmt->u.auth.variable;
607 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
608 if (!elems.challenge)
609 return;
610 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
611 elems.challenge - 2, elems.challenge_len + 2,
612 wk->filter_ta, wk->probe_auth.key,
613 wk->probe_auth.key_len, wk->probe_auth.key_idx);
614 wk->probe_auth.transaction = 4;
617 static enum work_action __must_check
618 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
619 struct ieee80211_mgmt *mgmt, size_t len)
621 u16 auth_alg, auth_transaction, status_code;
623 if (wk->type != IEEE80211_WORK_AUTH)
624 return WORK_ACT_MISMATCH;
626 if (len < 24 + 6)
627 return WORK_ACT_NONE;
629 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
630 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
631 status_code = le16_to_cpu(mgmt->u.auth.status_code);
633 if (auth_alg != wk->probe_auth.algorithm ||
634 auth_transaction != wk->probe_auth.transaction)
635 return WORK_ACT_NONE;
637 if (status_code != WLAN_STATUS_SUCCESS) {
638 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
639 wk->sdata->name, mgmt->sa, status_code);
640 return WORK_ACT_DONE;
643 switch (wk->probe_auth.algorithm) {
644 case WLAN_AUTH_OPEN:
645 case WLAN_AUTH_LEAP:
646 case WLAN_AUTH_FT:
647 break;
648 case WLAN_AUTH_SHARED_KEY:
649 if (wk->probe_auth.transaction != 4) {
650 ieee80211_auth_challenge(wk, mgmt, len);
651 /* need another frame */
652 return WORK_ACT_NONE;
654 break;
655 default:
656 WARN_ON(1);
657 return WORK_ACT_NONE;
660 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
661 return WORK_ACT_DONE;
664 static enum work_action __must_check
665 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
666 struct ieee80211_mgmt *mgmt, size_t len,
667 bool reassoc)
669 struct ieee80211_sub_if_data *sdata = wk->sdata;
670 struct ieee80211_local *local = sdata->local;
671 u16 capab_info, status_code, aid;
672 struct ieee802_11_elems elems;
673 u8 *pos;
675 if (wk->type != IEEE80211_WORK_ASSOC)
676 return WORK_ACT_MISMATCH;
679 * AssocResp and ReassocResp have identical structure, so process both
680 * of them in this function.
683 if (len < 24 + 6)
684 return WORK_ACT_NONE;
686 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
687 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
688 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
690 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
691 "status=%d aid=%d)\n",
692 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
693 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
695 pos = mgmt->u.assoc_resp.variable;
696 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
698 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
699 elems.timeout_int && elems.timeout_int_len == 5 &&
700 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
701 u32 tu, ms;
702 tu = get_unaligned_le32(elems.timeout_int + 1);
703 ms = tu * 1024 / 1000;
704 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
705 "comeback duration %u TU (%u ms)\n",
706 sdata->name, mgmt->sa, tu, ms);
707 wk->timeout = jiffies + msecs_to_jiffies(ms);
708 if (ms > IEEE80211_ASSOC_TIMEOUT)
709 run_again(local, wk->timeout);
710 return WORK_ACT_NONE;
713 if (status_code != WLAN_STATUS_SUCCESS)
714 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
715 sdata->name, mgmt->sa, status_code);
716 else
717 printk(KERN_DEBUG "%s: associated\n", sdata->name);
719 return WORK_ACT_DONE;
722 static enum work_action __must_check
723 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
724 struct ieee80211_mgmt *mgmt, size_t len,
725 struct ieee80211_rx_status *rx_status)
727 struct ieee80211_sub_if_data *sdata = wk->sdata;
728 struct ieee80211_local *local = sdata->local;
729 size_t baselen;
731 ASSERT_WORK_MTX(local);
733 if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
734 return WORK_ACT_MISMATCH;
736 if (len < 24 + 12)
737 return WORK_ACT_NONE;
739 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
740 if (baselen > len)
741 return WORK_ACT_NONE;
743 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
744 return WORK_ACT_DONE;
747 static enum work_action __must_check
748 ieee80211_rx_mgmt_beacon(struct ieee80211_work *wk,
749 struct ieee80211_mgmt *mgmt, size_t len)
751 struct ieee80211_sub_if_data *sdata = wk->sdata;
752 struct ieee80211_local *local = sdata->local;
754 ASSERT_WORK_MTX(local);
756 if (wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
757 return WORK_ACT_MISMATCH;
759 if (len < 24 + 12)
760 return WORK_ACT_NONE;
762 printk(KERN_DEBUG "%s: beacon received\n", sdata->name);
763 return WORK_ACT_DONE;
766 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
767 struct sk_buff *skb)
769 struct ieee80211_rx_status *rx_status;
770 struct ieee80211_mgmt *mgmt;
771 struct ieee80211_work *wk;
772 enum work_action rma = WORK_ACT_NONE;
773 u16 fc;
775 rx_status = (struct ieee80211_rx_status *) skb->cb;
776 mgmt = (struct ieee80211_mgmt *) skb->data;
777 fc = le16_to_cpu(mgmt->frame_control);
779 mutex_lock(&local->mtx);
781 list_for_each_entry(wk, &local->work_list, list) {
782 const u8 *bssid = NULL;
784 switch (wk->type) {
785 case IEEE80211_WORK_DIRECT_PROBE:
786 case IEEE80211_WORK_AUTH:
787 case IEEE80211_WORK_ASSOC:
788 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
789 bssid = wk->filter_ta;
790 break;
791 default:
792 continue;
796 * Before queuing, we already verified mgmt->sa,
797 * so this is needed just for matching.
799 if (compare_ether_addr(bssid, mgmt->bssid))
800 continue;
802 switch (fc & IEEE80211_FCTL_STYPE) {
803 case IEEE80211_STYPE_BEACON:
804 rma = ieee80211_rx_mgmt_beacon(wk, mgmt, skb->len);
805 break;
806 case IEEE80211_STYPE_PROBE_RESP:
807 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
808 rx_status);
809 break;
810 case IEEE80211_STYPE_AUTH:
811 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
812 break;
813 case IEEE80211_STYPE_ASSOC_RESP:
814 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
815 skb->len, false);
816 break;
817 case IEEE80211_STYPE_REASSOC_RESP:
818 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
819 skb->len, true);
820 break;
821 default:
822 WARN_ON(1);
823 rma = WORK_ACT_NONE;
827 * We've either received an unexpected frame, or we have
828 * multiple work items and need to match the frame to the
829 * right one.
831 if (rma == WORK_ACT_MISMATCH)
832 continue;
835 * We've processed this frame for that work, so it can't
836 * belong to another work struct.
837 * NB: this is also required for correctness for 'rma'!
839 break;
842 switch (rma) {
843 case WORK_ACT_MISMATCH:
844 /* ignore this unmatched frame */
845 break;
846 case WORK_ACT_NONE:
847 break;
848 case WORK_ACT_DONE:
849 list_del_rcu(&wk->list);
850 break;
851 default:
852 WARN(1, "unexpected: %d", rma);
855 mutex_unlock(&local->mtx);
857 if (rma != WORK_ACT_DONE)
858 goto out;
860 switch (wk->done(wk, skb)) {
861 case WORK_DONE_DESTROY:
862 free_work(wk);
863 break;
864 case WORK_DONE_REQUEUE:
865 synchronize_rcu();
866 wk->started = false; /* restart */
867 mutex_lock(&local->mtx);
868 list_add_tail(&wk->list, &local->work_list);
869 mutex_unlock(&local->mtx);
872 out:
873 kfree_skb(skb);
876 static void ieee80211_work_timer(unsigned long data)
878 struct ieee80211_local *local = (void *) data;
880 if (local->quiescing)
881 return;
883 ieee80211_queue_work(&local->hw, &local->work_work);
886 static void ieee80211_work_work(struct work_struct *work)
888 struct ieee80211_local *local =
889 container_of(work, struct ieee80211_local, work_work);
890 struct sk_buff *skb;
891 struct ieee80211_work *wk, *tmp;
892 LIST_HEAD(free_work);
893 enum work_action rma;
894 bool remain_off_channel = false;
896 if (local->scanning)
897 return;
900 * ieee80211_queue_work() should have picked up most cases,
901 * here we'll pick the rest.
903 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
904 return;
906 /* first process frames to avoid timing out while a frame is pending */
907 while ((skb = skb_dequeue(&local->work_skb_queue)))
908 ieee80211_work_rx_queued_mgmt(local, skb);
910 mutex_lock(&local->mtx);
912 ieee80211_recalc_idle(local);
914 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
915 bool started = wk->started;
917 /* mark work as started if it's on the current off-channel */
918 if (!started && local->tmp_channel &&
919 wk->chan == local->tmp_channel &&
920 wk->chan_type == local->tmp_channel_type) {
921 started = true;
922 wk->timeout = jiffies;
925 if (!started && !local->tmp_channel) {
927 * TODO: could optimize this by leaving the
928 * station vifs in awake mode if they
929 * happen to be on the same channel as
930 * the requested channel
932 ieee80211_offchannel_stop_beaconing(local);
933 ieee80211_offchannel_stop_station(local);
935 local->tmp_channel = wk->chan;
936 local->tmp_channel_type = wk->chan_type;
937 ieee80211_hw_config(local, 0);
938 started = true;
939 wk->timeout = jiffies;
942 /* don't try to work with items that aren't started */
943 if (!started)
944 continue;
946 if (time_is_after_jiffies(wk->timeout)) {
948 * This work item isn't supposed to be worked on
949 * right now, but take care to adjust the timer
950 * properly.
952 run_again(local, wk->timeout);
953 continue;
956 switch (wk->type) {
957 default:
958 WARN_ON(1);
959 /* nothing */
960 rma = WORK_ACT_NONE;
961 break;
962 case IEEE80211_WORK_ABORT:
963 rma = WORK_ACT_TIMEOUT;
964 break;
965 case IEEE80211_WORK_DIRECT_PROBE:
966 rma = ieee80211_direct_probe(wk);
967 break;
968 case IEEE80211_WORK_AUTH:
969 rma = ieee80211_authenticate(wk);
970 break;
971 case IEEE80211_WORK_ASSOC:
972 rma = ieee80211_associate(wk);
973 break;
974 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
975 rma = ieee80211_remain_on_channel_timeout(wk);
976 break;
977 case IEEE80211_WORK_OFFCHANNEL_TX:
978 rma = ieee80211_offchannel_tx(wk);
979 break;
980 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
981 rma = ieee80211_assoc_beacon_wait(wk);
982 break;
985 wk->started = started;
987 switch (rma) {
988 case WORK_ACT_NONE:
989 /* might have changed the timeout */
990 run_again(local, wk->timeout);
991 break;
992 case WORK_ACT_TIMEOUT:
993 list_del_rcu(&wk->list);
994 synchronize_rcu();
995 list_add(&wk->list, &free_work);
996 break;
997 default:
998 WARN(1, "unexpected: %d", rma);
1002 list_for_each_entry(wk, &local->work_list, list) {
1003 if (!wk->started)
1004 continue;
1005 if (wk->chan != local->tmp_channel)
1006 continue;
1007 if (wk->chan_type != local->tmp_channel_type)
1008 continue;
1009 remain_off_channel = true;
1012 if (!remain_off_channel && local->tmp_channel) {
1013 local->tmp_channel = NULL;
1014 ieee80211_hw_config(local, 0);
1015 ieee80211_offchannel_return(local, true);
1016 /* give connection some time to breathe */
1017 run_again(local, jiffies + HZ/2);
1020 if (list_empty(&local->work_list) && local->scan_req &&
1021 !local->scanning)
1022 ieee80211_queue_delayed_work(&local->hw,
1023 &local->scan_work,
1024 round_jiffies_relative(0));
1026 ieee80211_recalc_idle(local);
1028 mutex_unlock(&local->mtx);
1030 list_for_each_entry_safe(wk, tmp, &free_work, list) {
1031 wk->done(wk, NULL);
1032 list_del(&wk->list);
1033 kfree(wk);
1037 void ieee80211_add_work(struct ieee80211_work *wk)
1039 struct ieee80211_local *local;
1041 if (WARN_ON(!wk->chan))
1042 return;
1044 if (WARN_ON(!wk->sdata))
1045 return;
1047 if (WARN_ON(!wk->done))
1048 return;
1050 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
1051 return;
1053 wk->started = false;
1055 local = wk->sdata->local;
1056 mutex_lock(&local->mtx);
1057 list_add_tail(&wk->list, &local->work_list);
1058 mutex_unlock(&local->mtx);
1060 ieee80211_queue_work(&local->hw, &local->work_work);
1063 void ieee80211_work_init(struct ieee80211_local *local)
1065 INIT_LIST_HEAD(&local->work_list);
1066 setup_timer(&local->work_timer, ieee80211_work_timer,
1067 (unsigned long)local);
1068 INIT_WORK(&local->work_work, ieee80211_work_work);
1069 skb_queue_head_init(&local->work_skb_queue);
1072 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
1074 struct ieee80211_local *local = sdata->local;
1075 struct ieee80211_work *wk;
1077 mutex_lock(&local->mtx);
1078 list_for_each_entry(wk, &local->work_list, list) {
1079 if (wk->sdata != sdata)
1080 continue;
1081 wk->type = IEEE80211_WORK_ABORT;
1082 wk->started = true;
1083 wk->timeout = jiffies;
1085 mutex_unlock(&local->mtx);
1087 /* run cleanups etc. */
1088 ieee80211_work_work(&local->work_work);
1090 mutex_lock(&local->mtx);
1091 list_for_each_entry(wk, &local->work_list, list) {
1092 if (wk->sdata != sdata)
1093 continue;
1094 WARN_ON(1);
1095 break;
1097 mutex_unlock(&local->mtx);
1100 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1101 struct sk_buff *skb)
1103 struct ieee80211_local *local = sdata->local;
1104 struct ieee80211_mgmt *mgmt;
1105 struct ieee80211_work *wk;
1106 u16 fc;
1108 if (skb->len < 24)
1109 return RX_DROP_MONITOR;
1111 mgmt = (struct ieee80211_mgmt *) skb->data;
1112 fc = le16_to_cpu(mgmt->frame_control);
1114 list_for_each_entry_rcu(wk, &local->work_list, list) {
1115 if (sdata != wk->sdata)
1116 continue;
1117 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1118 continue;
1119 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1120 continue;
1122 switch (fc & IEEE80211_FCTL_STYPE) {
1123 case IEEE80211_STYPE_AUTH:
1124 case IEEE80211_STYPE_PROBE_RESP:
1125 case IEEE80211_STYPE_ASSOC_RESP:
1126 case IEEE80211_STYPE_REASSOC_RESP:
1127 case IEEE80211_STYPE_BEACON:
1128 skb_queue_tail(&local->work_skb_queue, skb);
1129 ieee80211_queue_work(&local->hw, &local->work_work);
1130 return RX_QUEUED;
1134 return RX_CONTINUE;
1137 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1138 struct sk_buff *skb)
1141 * We are done serving the remain-on-channel command.
1143 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1144 wk->chan, wk->chan_type,
1145 GFP_KERNEL);
1147 return WORK_DONE_DESTROY;
1150 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1151 struct ieee80211_channel *chan,
1152 enum nl80211_channel_type channel_type,
1153 unsigned int duration, u64 *cookie)
1155 struct ieee80211_work *wk;
1157 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1158 if (!wk)
1159 return -ENOMEM;
1161 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1162 wk->chan = chan;
1163 wk->chan_type = channel_type;
1164 wk->sdata = sdata;
1165 wk->done = ieee80211_remain_done;
1167 wk->remain.duration = duration;
1169 *cookie = (unsigned long) wk;
1171 ieee80211_add_work(wk);
1173 return 0;
1176 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1177 u64 cookie)
1179 struct ieee80211_local *local = sdata->local;
1180 struct ieee80211_work *wk, *tmp;
1181 bool found = false;
1183 mutex_lock(&local->mtx);
1184 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1185 if ((unsigned long) wk == cookie) {
1186 wk->timeout = jiffies;
1187 found = true;
1188 break;
1191 mutex_unlock(&local->mtx);
1193 if (!found)
1194 return -ENOENT;
1196 ieee80211_queue_work(&local->hw, &local->work_work);
1198 return 0;