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"
28 #include "driver-ops.h"
30 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
31 #define IEEE80211_AUTH_MAX_TRIES 3
32 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
33 #define IEEE80211_ASSOC_MAX_TRIES 3
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
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 void free_work(struct ieee80211_work *wk)
71 kfree_rcu(wk, rcu_head);
74 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
75 struct ieee80211_supported_band *sband,
81 for (i = 0; i < supp_rates_len; i++) {
82 int rate = (supp_rates[i] & 0x7F) * 5;
84 for (j = 0; j < sband->n_bitrates; j++)
85 if (sband->bitrates[j].bitrate == rate) {
95 /* frame sending functions */
97 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
98 struct sk_buff *skb, const u8 *ht_info_ie,
99 struct ieee80211_supported_band *sband,
100 struct ieee80211_channel *channel,
101 enum ieee80211_smps_mode smps)
103 struct ieee80211_ht_info *ht_info;
105 u32 flags = channel->flags;
107 struct ieee80211_sta_ht_cap ht_cap;
109 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
111 if (!sband->ht_cap.ht_supported)
117 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
120 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
121 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
123 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
125 /* determine capability flags */
128 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
129 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
130 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
131 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
132 cap &= ~IEEE80211_HT_CAP_SGI_40;
135 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
136 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
137 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
138 cap &= ~IEEE80211_HT_CAP_SGI_40;
143 /* set SM PS mode properly */
144 cap &= ~IEEE80211_HT_CAP_SM_PS;
146 case IEEE80211_SMPS_AUTOMATIC:
147 case IEEE80211_SMPS_NUM_MODES:
149 case IEEE80211_SMPS_OFF:
150 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
151 IEEE80211_HT_CAP_SM_PS_SHIFT;
153 case IEEE80211_SMPS_STATIC:
154 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
155 IEEE80211_HT_CAP_SM_PS_SHIFT;
157 case IEEE80211_SMPS_DYNAMIC:
158 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
159 IEEE80211_HT_CAP_SM_PS_SHIFT;
163 /* reserve and fill IE */
164 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
165 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
168 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
169 struct ieee80211_work *wk)
171 struct ieee80211_local *local = sdata->local;
173 struct ieee80211_mgmt *mgmt;
175 size_t offset = 0, noffset;
176 int i, count, rates_len, supp_rates_len;
178 struct ieee80211_supported_band *sband;
181 sband = local->hw.wiphy->bands[wk->chan->band];
183 if (wk->assoc.supp_rates_len) {
185 * Get all rates supported by the device and the AP as
186 * some APs don't like getting a superset of their rates
187 * in the association request (e.g. D-Link DAP 1353 in
190 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
191 wk->assoc.supp_rates_len,
195 * In case AP not provide any supported rates information
196 * before association, we send information element(s) with
197 * all rates that we support.
200 rates_len = sband->n_bitrates;
203 skb = alloc_skb(local->hw.extra_tx_headroom +
204 sizeof(*mgmt) + /* bit too much but doesn't matter */
205 2 + wk->assoc.ssid_len + /* SSID */
206 4 + rates_len + /* (extended) rates */
207 4 + /* power capability */
208 2 + 2 * sband->n_channels + /* supported channels */
209 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
210 wk->ie_len + /* extra IEs */
216 skb_reserve(skb, local->hw.extra_tx_headroom);
218 capab = WLAN_CAPABILITY_ESS;
220 if (sband->band == IEEE80211_BAND_2GHZ) {
221 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
222 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
223 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
224 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
227 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
228 capab |= WLAN_CAPABILITY_PRIVACY;
230 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
231 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
232 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
234 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
236 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
237 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
238 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
240 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
242 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
243 IEEE80211_STYPE_REASSOC_REQ);
244 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
245 mgmt->u.reassoc_req.listen_interval =
246 cpu_to_le16(local->hw.conf.listen_interval);
247 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
251 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
252 IEEE80211_STYPE_ASSOC_REQ);
253 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
254 mgmt->u.assoc_req.listen_interval =
255 cpu_to_le16(local->hw.conf.listen_interval);
259 pos = skb_put(skb, 2 + wk->assoc.ssid_len);
260 *pos++ = WLAN_EID_SSID;
261 *pos++ = wk->assoc.ssid_len;
262 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
264 /* add all rates which were marked to be used above */
265 supp_rates_len = rates_len;
266 if (supp_rates_len > 8)
269 pos = skb_put(skb, supp_rates_len + 2);
270 *pos++ = WLAN_EID_SUPP_RATES;
271 *pos++ = supp_rates_len;
274 for (i = 0; i < sband->n_bitrates; i++) {
275 if (BIT(i) & rates) {
276 int rate = sband->bitrates[i].bitrate;
277 *pos++ = (u8) (rate / 5);
283 if (rates_len > count) {
284 pos = skb_put(skb, rates_len - count + 2);
285 *pos++ = WLAN_EID_EXT_SUPP_RATES;
286 *pos++ = rates_len - count;
288 for (i++; i < sband->n_bitrates; i++) {
289 if (BIT(i) & rates) {
290 int rate = sband->bitrates[i].bitrate;
291 *pos++ = (u8) (rate / 5);
296 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
297 /* 1. power capabilities */
298 pos = skb_put(skb, 4);
299 *pos++ = WLAN_EID_PWR_CAPABILITY;
301 *pos++ = 0; /* min tx power */
302 *pos++ = wk->chan->max_power; /* max tx power */
304 /* 2. supported channels */
305 /* TODO: get this in reg domain format */
306 pos = skb_put(skb, 2 * sband->n_channels + 2);
307 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
308 *pos++ = 2 * sband->n_channels;
309 for (i = 0; i < sband->n_channels; i++) {
310 *pos++ = ieee80211_frequency_to_channel(
311 sband->channels[i].center_freq);
312 *pos++ = 1; /* one channel in the subband*/
316 /* if present, add any custom IEs that go before HT */
317 if (wk->ie_len && wk->ie) {
318 static const u8 before_ht[] = {
321 WLAN_EID_EXT_SUPP_RATES,
322 WLAN_EID_PWR_CAPABILITY,
323 WLAN_EID_SUPPORTED_CHANNELS,
326 WLAN_EID_RRM_ENABLED_CAPABILITIES,
327 WLAN_EID_MOBILITY_DOMAIN,
328 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
330 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
331 before_ht, ARRAY_SIZE(before_ht),
333 pos = skb_put(skb, noffset - offset);
334 memcpy(pos, wk->ie + offset, noffset - offset);
338 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
339 local->hw.queues >= 4)
340 ieee80211_add_ht_ie(sdata, skb, wk->assoc.ht_information_ie,
341 sband, wk->chan, wk->assoc.smps);
343 /* if present, add any custom non-vendor IEs that go after HT */
344 if (wk->ie_len && wk->ie) {
345 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
347 pos = skb_put(skb, noffset - offset);
348 memcpy(pos, wk->ie + offset, noffset - offset);
352 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
353 if (wk->assoc.uapsd_used) {
354 qos_info = local->uapsd_queues;
355 qos_info |= (local->uapsd_max_sp_len <<
356 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
361 pos = skb_put(skb, 9);
362 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
363 *pos++ = 7; /* len */
364 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
367 *pos++ = 2; /* WME */
368 *pos++ = 0; /* WME info */
369 *pos++ = 1; /* WME ver */
373 /* add any remaining custom (i.e. vendor specific here) IEs */
374 if (wk->ie_len && wk->ie) {
375 noffset = wk->ie_len;
376 pos = skb_put(skb, noffset - offset);
377 memcpy(pos, wk->ie + offset, noffset - offset);
380 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
381 ieee80211_tx_skb(sdata, skb);
384 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
385 struct ieee80211_work *wk)
387 struct cfg80211_bss *cbss;
388 u16 capa_val = WLAN_CAPABILITY_ESS;
390 if (wk->probe_auth.privacy)
391 capa_val |= WLAN_CAPABILITY_PRIVACY;
393 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
394 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
395 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
400 cfg80211_unlink_bss(local->hw.wiphy, cbss);
401 cfg80211_put_bss(cbss);
404 static enum work_action __must_check
405 ieee80211_direct_probe(struct ieee80211_work *wk)
407 struct ieee80211_sub_if_data *sdata = wk->sdata;
408 struct ieee80211_local *local = sdata->local;
410 if (!wk->probe_auth.synced) {
411 int ret = drv_tx_sync(local, sdata, wk->filter_ta,
412 IEEE80211_TX_SYNC_AUTH);
414 return WORK_ACT_TIMEOUT;
416 wk->probe_auth.synced = true;
418 wk->probe_auth.tries++;
419 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
420 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
421 sdata->name, wk->filter_ta);
424 * Most likely AP is not in the range so remove the
425 * bss struct for that AP.
427 ieee80211_remove_auth_bss(local, wk);
429 return WORK_ACT_TIMEOUT;
432 printk(KERN_DEBUG "%s: direct probe to %pM (try %d/%i)\n",
433 sdata->name, wk->filter_ta, wk->probe_auth.tries,
434 IEEE80211_AUTH_MAX_TRIES);
437 * Direct probe is sent to broadcast address as some APs
438 * will not answer to direct packet in unassociated state.
440 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
441 wk->probe_auth.ssid_len, NULL, 0,
442 (u32) -1, true, false);
444 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
445 run_again(local, wk->timeout);
447 return WORK_ACT_NONE;
451 static enum work_action __must_check
452 ieee80211_authenticate(struct ieee80211_work *wk)
454 struct ieee80211_sub_if_data *sdata = wk->sdata;
455 struct ieee80211_local *local = sdata->local;
457 if (!wk->probe_auth.synced) {
458 int ret = drv_tx_sync(local, sdata, wk->filter_ta,
459 IEEE80211_TX_SYNC_AUTH);
461 return WORK_ACT_TIMEOUT;
463 wk->probe_auth.synced = true;
465 wk->probe_auth.tries++;
466 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
467 printk(KERN_DEBUG "%s: authentication with %pM"
468 " timed out\n", sdata->name, wk->filter_ta);
471 * Most likely AP is not in the range so remove the
472 * bss struct for that AP.
474 ieee80211_remove_auth_bss(local, wk);
476 return WORK_ACT_TIMEOUT;
479 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
480 sdata->name, wk->filter_ta, wk->probe_auth.tries);
482 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
483 wk->ie_len, wk->filter_ta, NULL, 0, 0);
484 wk->probe_auth.transaction = 2;
486 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
487 run_again(local, wk->timeout);
489 return WORK_ACT_NONE;
492 static enum work_action __must_check
493 ieee80211_associate(struct ieee80211_work *wk)
495 struct ieee80211_sub_if_data *sdata = wk->sdata;
496 struct ieee80211_local *local = sdata->local;
498 if (!wk->assoc.synced) {
499 int ret = drv_tx_sync(local, sdata, wk->filter_ta,
500 IEEE80211_TX_SYNC_ASSOC);
502 return WORK_ACT_TIMEOUT;
504 wk->assoc.synced = true;
507 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
508 printk(KERN_DEBUG "%s: association with %pM"
510 sdata->name, wk->filter_ta);
513 * Most likely AP is not in the range so remove the
514 * bss struct for that AP.
517 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
519 return WORK_ACT_TIMEOUT;
522 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
523 sdata->name, wk->filter_ta, wk->assoc.tries);
524 ieee80211_send_assoc(sdata, wk);
526 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
527 run_again(local, wk->timeout);
529 return WORK_ACT_NONE;
532 static enum work_action __must_check
533 ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
536 * First time we run, do nothing -- the generic code will
537 * have switched to the right channel etc.
540 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
542 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
543 wk->chan, wk->chan_type,
544 wk->remain.duration, GFP_KERNEL);
546 return WORK_ACT_NONE;
549 return WORK_ACT_TIMEOUT;
552 static enum work_action __must_check
553 ieee80211_offchannel_tx(struct ieee80211_work *wk)
556 wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);
559 * After this, offchan_tx.frame remains but now is no
560 * longer a valid pointer -- we still need it as the
561 * cookie for canceling this work/status matching.
563 ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);
565 return WORK_ACT_NONE;
568 return WORK_ACT_TIMEOUT;
571 static enum work_action __must_check
572 ieee80211_assoc_beacon_wait(struct ieee80211_work *wk)
575 return WORK_ACT_TIMEOUT;
578 * Wait up to one beacon interval ...
579 * should this be more if we miss one?
581 printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
582 wk->sdata->name, wk->filter_ta);
583 wk->timeout = TU_TO_EXP_TIME(wk->assoc.bss->beacon_interval);
584 return WORK_ACT_NONE;
587 static void ieee80211_auth_challenge(struct ieee80211_work *wk,
588 struct ieee80211_mgmt *mgmt,
591 struct ieee80211_sub_if_data *sdata = wk->sdata;
593 struct ieee802_11_elems elems;
595 pos = mgmt->u.auth.variable;
596 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
597 if (!elems.challenge)
599 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
600 elems.challenge - 2, elems.challenge_len + 2,
601 wk->filter_ta, wk->probe_auth.key,
602 wk->probe_auth.key_len, wk->probe_auth.key_idx);
603 wk->probe_auth.transaction = 4;
606 static enum work_action __must_check
607 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
608 struct ieee80211_mgmt *mgmt, size_t len)
610 u16 auth_alg, auth_transaction, status_code;
612 if (wk->type != IEEE80211_WORK_AUTH)
613 return WORK_ACT_MISMATCH;
616 return WORK_ACT_NONE;
618 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
619 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
620 status_code = le16_to_cpu(mgmt->u.auth.status_code);
622 if (auth_alg != wk->probe_auth.algorithm ||
623 auth_transaction != wk->probe_auth.transaction)
624 return WORK_ACT_NONE;
626 if (status_code != WLAN_STATUS_SUCCESS) {
627 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
628 wk->sdata->name, mgmt->sa, status_code);
629 return WORK_ACT_DONE;
632 switch (wk->probe_auth.algorithm) {
637 case WLAN_AUTH_SHARED_KEY:
638 if (wk->probe_auth.transaction != 4) {
639 ieee80211_auth_challenge(wk, mgmt, len);
640 /* need another frame */
641 return WORK_ACT_NONE;
646 return WORK_ACT_NONE;
649 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
650 return WORK_ACT_DONE;
653 static enum work_action __must_check
654 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
655 struct ieee80211_mgmt *mgmt, size_t len,
658 struct ieee80211_sub_if_data *sdata = wk->sdata;
659 struct ieee80211_local *local = sdata->local;
660 u16 capab_info, status_code, aid;
661 struct ieee802_11_elems elems;
664 if (wk->type != IEEE80211_WORK_ASSOC)
665 return WORK_ACT_MISMATCH;
668 * AssocResp and ReassocResp have identical structure, so process both
669 * of them in this function.
673 return WORK_ACT_NONE;
675 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
676 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
677 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
679 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
680 "status=%d aid=%d)\n",
681 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
682 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
684 pos = mgmt->u.assoc_resp.variable;
685 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
687 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
688 elems.timeout_int && elems.timeout_int_len == 5 &&
689 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
691 tu = get_unaligned_le32(elems.timeout_int + 1);
692 ms = tu * 1024 / 1000;
693 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
694 "comeback duration %u TU (%u ms)\n",
695 sdata->name, mgmt->sa, tu, ms);
696 wk->timeout = jiffies + msecs_to_jiffies(ms);
697 if (ms > IEEE80211_ASSOC_TIMEOUT)
698 run_again(local, wk->timeout);
699 return WORK_ACT_NONE;
702 if (status_code != WLAN_STATUS_SUCCESS)
703 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
704 sdata->name, mgmt->sa, status_code);
706 printk(KERN_DEBUG "%s: associated\n", sdata->name);
708 return WORK_ACT_DONE;
711 static enum work_action __must_check
712 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
713 struct ieee80211_mgmt *mgmt, size_t len,
714 struct ieee80211_rx_status *rx_status)
716 struct ieee80211_sub_if_data *sdata = wk->sdata;
717 struct ieee80211_local *local = sdata->local;
720 ASSERT_WORK_MTX(local);
722 if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
723 return WORK_ACT_MISMATCH;
726 return WORK_ACT_NONE;
728 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
730 return WORK_ACT_NONE;
732 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
733 return WORK_ACT_DONE;
736 static enum work_action __must_check
737 ieee80211_rx_mgmt_beacon(struct ieee80211_work *wk,
738 struct ieee80211_mgmt *mgmt, size_t len)
740 struct ieee80211_sub_if_data *sdata = wk->sdata;
741 struct ieee80211_local *local = sdata->local;
743 ASSERT_WORK_MTX(local);
745 if (wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
746 return WORK_ACT_MISMATCH;
749 return WORK_ACT_NONE;
751 printk(KERN_DEBUG "%s: beacon received\n", sdata->name);
752 return WORK_ACT_DONE;
755 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
758 struct ieee80211_rx_status *rx_status;
759 struct ieee80211_mgmt *mgmt;
760 struct ieee80211_work *wk;
761 enum work_action rma = WORK_ACT_NONE;
764 rx_status = (struct ieee80211_rx_status *) skb->cb;
765 mgmt = (struct ieee80211_mgmt *) skb->data;
766 fc = le16_to_cpu(mgmt->frame_control);
768 mutex_lock(&local->mtx);
770 list_for_each_entry(wk, &local->work_list, list) {
771 const u8 *bssid = NULL;
774 case IEEE80211_WORK_DIRECT_PROBE:
775 case IEEE80211_WORK_AUTH:
776 case IEEE80211_WORK_ASSOC:
777 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
778 bssid = wk->filter_ta;
785 * Before queuing, we already verified mgmt->sa,
786 * so this is needed just for matching.
788 if (compare_ether_addr(bssid, mgmt->bssid))
791 switch (fc & IEEE80211_FCTL_STYPE) {
792 case IEEE80211_STYPE_BEACON:
793 rma = ieee80211_rx_mgmt_beacon(wk, mgmt, skb->len);
795 case IEEE80211_STYPE_PROBE_RESP:
796 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
799 case IEEE80211_STYPE_AUTH:
800 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
802 case IEEE80211_STYPE_ASSOC_RESP:
803 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
806 case IEEE80211_STYPE_REASSOC_RESP:
807 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
816 * We've either received an unexpected frame, or we have
817 * multiple work items and need to match the frame to the
820 if (rma == WORK_ACT_MISMATCH)
824 * We've processed this frame for that work, so it can't
825 * belong to another work struct.
826 * NB: this is also required for correctness for 'rma'!
832 case WORK_ACT_MISMATCH:
833 /* ignore this unmatched frame */
838 list_del_rcu(&wk->list);
841 WARN(1, "unexpected: %d", rma);
844 mutex_unlock(&local->mtx);
846 if (rma != WORK_ACT_DONE)
849 switch (wk->done(wk, skb)) {
850 case WORK_DONE_DESTROY:
853 case WORK_DONE_REQUEUE:
855 wk->started = false; /* restart */
856 mutex_lock(&local->mtx);
857 list_add_tail(&wk->list, &local->work_list);
858 mutex_unlock(&local->mtx);
865 static bool ieee80211_work_ct_coexists(enum nl80211_channel_type wk_ct,
866 enum nl80211_channel_type oper_ct)
869 case NL80211_CHAN_NO_HT:
871 case NL80211_CHAN_HT20:
872 if (oper_ct != NL80211_CHAN_NO_HT)
875 case NL80211_CHAN_HT40MINUS:
876 case NL80211_CHAN_HT40PLUS:
877 return (wk_ct == oper_ct);
879 WARN_ON(1); /* shouldn't get here */
883 static enum nl80211_channel_type
884 ieee80211_calc_ct(enum nl80211_channel_type wk_ct,
885 enum nl80211_channel_type oper_ct)
888 case NL80211_CHAN_NO_HT:
890 case NL80211_CHAN_HT20:
891 if (oper_ct != NL80211_CHAN_NO_HT)
894 case NL80211_CHAN_HT40MINUS:
895 case NL80211_CHAN_HT40PLUS:
898 WARN_ON(1); /* shouldn't get here */
903 static void ieee80211_work_timer(unsigned long data)
905 struct ieee80211_local *local = (void *) data;
907 if (local->quiescing)
910 ieee80211_queue_work(&local->hw, &local->work_work);
913 static void ieee80211_work_work(struct work_struct *work)
915 struct ieee80211_local *local =
916 container_of(work, struct ieee80211_local, work_work);
918 struct ieee80211_work *wk, *tmp;
919 LIST_HEAD(free_work);
920 enum work_action rma;
921 bool remain_off_channel = false;
927 * ieee80211_queue_work() should have picked up most cases,
928 * here we'll pick the rest.
930 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
933 /* first process frames to avoid timing out while a frame is pending */
934 while ((skb = skb_dequeue(&local->work_skb_queue)))
935 ieee80211_work_rx_queued_mgmt(local, skb);
937 mutex_lock(&local->mtx);
939 ieee80211_recalc_idle(local);
941 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
942 bool started = wk->started;
944 /* mark work as started if it's on the current off-channel */
945 if (!started && local->tmp_channel &&
946 wk->chan == local->tmp_channel &&
947 wk->chan_type == local->tmp_channel_type) {
949 wk->timeout = jiffies;
952 if (!started && !local->tmp_channel) {
953 bool on_oper_chan, on_oper_chan2;
954 enum nl80211_channel_type wk_ct;
956 on_oper_chan = ieee80211_cfg_on_oper_channel(local);
958 /* Work with existing channel type if possible. */
959 wk_ct = wk->chan_type;
960 if (wk->chan == local->hw.conf.channel)
961 wk_ct = ieee80211_calc_ct(wk->chan_type,
962 local->hw.conf.channel_type);
964 local->tmp_channel = wk->chan;
965 local->tmp_channel_type = wk_ct;
967 * Leave the station vifs in awake mode if they
968 * happen to be on the same channel as
969 * the requested channel.
971 on_oper_chan2 = ieee80211_cfg_on_oper_channel(local);
972 if (on_oper_chan != on_oper_chan2) {
974 /* going off oper channel, PS too */
975 ieee80211_offchannel_stop_vifs(local,
977 ieee80211_hw_config(local, 0);
979 /* going on channel, but leave PS
981 ieee80211_hw_config(local, 0);
982 ieee80211_offchannel_return(local,
989 wk->timeout = jiffies;
992 /* don't try to work with items that aren't started */
996 if (time_is_after_jiffies(wk->timeout)) {
998 * This work item isn't supposed to be worked on
999 * right now, but take care to adjust the timer
1002 run_again(local, wk->timeout);
1010 rma = WORK_ACT_NONE;
1012 case IEEE80211_WORK_ABORT:
1013 rma = WORK_ACT_TIMEOUT;
1015 case IEEE80211_WORK_DIRECT_PROBE:
1016 rma = ieee80211_direct_probe(wk);
1018 case IEEE80211_WORK_AUTH:
1019 rma = ieee80211_authenticate(wk);
1021 case IEEE80211_WORK_ASSOC:
1022 rma = ieee80211_associate(wk);
1024 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
1025 rma = ieee80211_remain_on_channel_timeout(wk);
1027 case IEEE80211_WORK_OFFCHANNEL_TX:
1028 rma = ieee80211_offchannel_tx(wk);
1030 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
1031 rma = ieee80211_assoc_beacon_wait(wk);
1035 wk->started = started;
1039 /* might have changed the timeout */
1040 run_again(local, wk->timeout);
1042 case WORK_ACT_TIMEOUT:
1043 list_del_rcu(&wk->list);
1045 list_add(&wk->list, &free_work);
1048 WARN(1, "unexpected: %d", rma);
1052 list_for_each_entry(wk, &local->work_list, list) {
1055 if (wk->chan != local->tmp_channel)
1057 if (!ieee80211_work_ct_coexists(wk->chan_type,
1058 local->tmp_channel_type))
1060 remain_off_channel = true;
1063 if (!remain_off_channel && local->tmp_channel) {
1064 local->tmp_channel = NULL;
1065 /* If tmp_channel wasn't operating channel, then
1066 * we need to go back on-channel.
1067 * NOTE: If we can ever be here while scannning,
1068 * or if the hw_config() channel config logic changes,
1069 * then we may need to do a more thorough check to see if
1070 * we still need to do a hardware config. Currently,
1071 * we cannot be here while scanning, however.
1073 if (!ieee80211_cfg_on_oper_channel(local))
1074 ieee80211_hw_config(local, 0);
1076 /* At the least, we need to disable offchannel_ps,
1077 * so just go ahead and run the entire offchannel
1078 * return logic here. We *could* skip enabling
1079 * beaconing if we were already on-oper-channel
1080 * as a future optimization.
1082 ieee80211_offchannel_return(local, true, true);
1084 /* give connection some time to breathe */
1085 run_again(local, jiffies + HZ/2);
1088 if (list_empty(&local->work_list) && local->scan_req &&
1090 ieee80211_queue_delayed_work(&local->hw,
1092 round_jiffies_relative(0));
1094 ieee80211_recalc_idle(local);
1096 mutex_unlock(&local->mtx);
1098 list_for_each_entry_safe(wk, tmp, &free_work, list) {
1100 list_del(&wk->list);
1105 void ieee80211_add_work(struct ieee80211_work *wk)
1107 struct ieee80211_local *local;
1109 if (WARN_ON(!wk->chan))
1112 if (WARN_ON(!wk->sdata))
1115 if (WARN_ON(!wk->done))
1118 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
1121 wk->started = false;
1123 local = wk->sdata->local;
1124 mutex_lock(&local->mtx);
1125 list_add_tail(&wk->list, &local->work_list);
1126 mutex_unlock(&local->mtx);
1128 ieee80211_queue_work(&local->hw, &local->work_work);
1131 void ieee80211_work_init(struct ieee80211_local *local)
1133 INIT_LIST_HEAD(&local->work_list);
1134 setup_timer(&local->work_timer, ieee80211_work_timer,
1135 (unsigned long)local);
1136 INIT_WORK(&local->work_work, ieee80211_work_work);
1137 skb_queue_head_init(&local->work_skb_queue);
1140 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
1142 struct ieee80211_local *local = sdata->local;
1143 struct ieee80211_work *wk;
1144 bool cleanup = false;
1146 mutex_lock(&local->mtx);
1147 list_for_each_entry(wk, &local->work_list, list) {
1148 if (wk->sdata != sdata)
1151 wk->type = IEEE80211_WORK_ABORT;
1153 wk->timeout = jiffies;
1155 mutex_unlock(&local->mtx);
1157 /* run cleanups etc. */
1159 ieee80211_work_work(&local->work_work);
1161 mutex_lock(&local->mtx);
1162 list_for_each_entry(wk, &local->work_list, list) {
1163 if (wk->sdata != sdata)
1168 mutex_unlock(&local->mtx);
1171 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1172 struct sk_buff *skb)
1174 struct ieee80211_local *local = sdata->local;
1175 struct ieee80211_mgmt *mgmt;
1176 struct ieee80211_work *wk;
1180 return RX_DROP_MONITOR;
1182 mgmt = (struct ieee80211_mgmt *) skb->data;
1183 fc = le16_to_cpu(mgmt->frame_control);
1185 list_for_each_entry_rcu(wk, &local->work_list, list) {
1186 if (sdata != wk->sdata)
1188 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1190 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1193 switch (fc & IEEE80211_FCTL_STYPE) {
1194 case IEEE80211_STYPE_AUTH:
1195 case IEEE80211_STYPE_PROBE_RESP:
1196 case IEEE80211_STYPE_ASSOC_RESP:
1197 case IEEE80211_STYPE_REASSOC_RESP:
1198 case IEEE80211_STYPE_BEACON:
1199 skb_queue_tail(&local->work_skb_queue, skb);
1200 ieee80211_queue_work(&local->hw, &local->work_work);
1208 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1209 struct sk_buff *skb)
1212 * We are done serving the remain-on-channel command.
1214 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1215 wk->chan, wk->chan_type,
1218 return WORK_DONE_DESTROY;
1221 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1222 struct ieee80211_channel *chan,
1223 enum nl80211_channel_type channel_type,
1224 unsigned int duration, u64 *cookie)
1226 struct ieee80211_work *wk;
1228 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1232 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1234 wk->chan_type = channel_type;
1236 wk->done = ieee80211_remain_done;
1238 wk->remain.duration = duration;
1240 *cookie = (unsigned long) wk;
1242 ieee80211_add_work(wk);
1247 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1250 struct ieee80211_local *local = sdata->local;
1251 struct ieee80211_work *wk, *tmp;
1254 mutex_lock(&local->mtx);
1255 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1256 if ((unsigned long) wk == cookie) {
1257 wk->timeout = jiffies;
1262 mutex_unlock(&local->mtx);
1267 ieee80211_queue_work(&local->hw, &local->work_work);