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"
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
43 static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
45 WARN_ON(!mutex_is_locked(&local->work_mtx));
49 * We can have multiple work items (and connection probing)
50 * scheduling this timer, but we need to take care to only
51 * reschedule it when it should fire _earlier_ than it was
52 * asked for before, or if it's not pending right now. This
53 * function ensures that. Note that it then is required to
54 * run this function for all timeouts after the first one
55 * has happened -- the work that runs from this timer will
58 static void run_again(struct ieee80211_local *local,
59 unsigned long timeout)
61 ASSERT_WORK_MTX(local);
63 if (!timer_pending(&local->work_timer) ||
64 time_before(timeout, local->work_timer.expires))
65 mod_timer(&local->work_timer, timeout);
68 static void work_free_rcu(struct rcu_head *head)
70 struct ieee80211_work *wk =
71 container_of(head, struct ieee80211_work, rcu_head);
76 void free_work(struct ieee80211_work *wk)
78 call_rcu(&wk->rcu_head, work_free_rcu);
81 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
82 struct ieee80211_supported_band *sband,
88 for (i = 0; i < supp_rates_len; i++) {
89 int rate = (supp_rates[i] & 0x7F) * 5;
91 for (j = 0; j < sband->n_bitrates; j++)
92 if (sband->bitrates[j].bitrate == rate) {
102 /* frame sending functions */
104 static void ieee80211_add_ht_ie(struct sk_buff *skb, const u8 *ht_info_ie,
105 struct ieee80211_supported_band *sband,
106 struct ieee80211_channel *channel,
107 enum ieee80211_smps_mode smps)
109 struct ieee80211_ht_info *ht_info;
111 u32 flags = channel->flags;
112 u16 cap = sband->ht_cap.cap;
115 if (!sband->ht_cap.ht_supported)
121 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
124 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
126 /* determine capability flags */
128 if (ieee80211_disable_40mhz_24ghz &&
129 sband->band == IEEE80211_BAND_2GHZ) {
130 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
131 cap &= ~IEEE80211_HT_CAP_SGI_40;
134 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
135 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
136 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
137 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
138 cap &= ~IEEE80211_HT_CAP_SGI_40;
141 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
142 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
143 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
144 cap &= ~IEEE80211_HT_CAP_SGI_40;
149 /* set SM PS mode properly */
150 cap &= ~IEEE80211_HT_CAP_SM_PS;
152 case IEEE80211_SMPS_AUTOMATIC:
153 case IEEE80211_SMPS_NUM_MODES:
155 case IEEE80211_SMPS_OFF:
156 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
157 IEEE80211_HT_CAP_SM_PS_SHIFT;
159 case IEEE80211_SMPS_STATIC:
160 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
161 IEEE80211_HT_CAP_SM_PS_SHIFT;
163 case IEEE80211_SMPS_DYNAMIC:
164 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
165 IEEE80211_HT_CAP_SM_PS_SHIFT;
169 /* reserve and fill IE */
171 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
172 *pos++ = WLAN_EID_HT_CAPABILITY;
173 *pos++ = sizeof(struct ieee80211_ht_cap);
174 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
176 /* capability flags */
177 tmp = cpu_to_le16(cap);
178 memcpy(pos, &tmp, sizeof(u16));
181 /* AMPDU parameters */
182 *pos++ = sband->ht_cap.ampdu_factor |
183 (sband->ht_cap.ampdu_density <<
184 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
187 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
188 pos += sizeof(sband->ht_cap.mcs);
190 /* extended capabilities */
191 pos += sizeof(__le16);
193 /* BF capabilities */
194 pos += sizeof(__le32);
196 /* antenna selection */
200 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
201 struct ieee80211_work *wk)
203 struct ieee80211_local *local = sdata->local;
205 struct ieee80211_mgmt *mgmt;
208 size_t offset = 0, noffset;
209 int i, len, count, rates_len, supp_rates_len;
211 struct ieee80211_supported_band *sband;
214 sband = local->hw.wiphy->bands[wk->chan->band];
216 if (wk->assoc.supp_rates_len) {
218 * Get all rates supported by the device and the AP as
219 * some APs don't like getting a superset of their rates
220 * in the association request (e.g. D-Link DAP 1353 in
223 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
224 wk->assoc.supp_rates_len,
228 * In case AP not provide any supported rates information
229 * before association, we send information element(s) with
230 * all rates that we support.
233 rates_len = sband->n_bitrates;
236 skb = alloc_skb(local->hw.extra_tx_headroom +
237 sizeof(*mgmt) + /* bit too much but doesn't matter */
238 2 + wk->assoc.ssid_len + /* SSID */
239 4 + rates_len + /* (extended) rates */
240 4 + /* power capability */
241 2 + 2 * sband->n_channels + /* supported channels */
242 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
243 wk->ie_len + /* extra IEs */
247 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
248 "frame\n", sdata->name);
251 skb_reserve(skb, local->hw.extra_tx_headroom);
253 capab = WLAN_CAPABILITY_ESS;
255 if (sband->band == IEEE80211_BAND_2GHZ) {
256 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
257 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
258 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
259 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
262 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
263 capab |= WLAN_CAPABILITY_PRIVACY;
265 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
266 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
267 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
269 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
271 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
272 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
273 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
275 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
277 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
278 IEEE80211_STYPE_REASSOC_REQ);
279 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
280 mgmt->u.reassoc_req.listen_interval =
281 cpu_to_le16(local->hw.conf.listen_interval);
282 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
286 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
287 IEEE80211_STYPE_ASSOC_REQ);
288 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
289 mgmt->u.assoc_req.listen_interval =
290 cpu_to_le16(local->hw.conf.listen_interval);
294 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
295 *pos++ = WLAN_EID_SSID;
296 *pos++ = wk->assoc.ssid_len;
297 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
299 /* add all rates which were marked to be used above */
300 supp_rates_len = rates_len;
301 if (supp_rates_len > 8)
304 len = sband->n_bitrates;
305 pos = skb_put(skb, supp_rates_len + 2);
306 *pos++ = WLAN_EID_SUPP_RATES;
307 *pos++ = supp_rates_len;
310 for (i = 0; i < sband->n_bitrates; i++) {
311 if (BIT(i) & rates) {
312 int rate = sband->bitrates[i].bitrate;
313 *pos++ = (u8) (rate / 5);
319 if (rates_len > count) {
320 pos = skb_put(skb, rates_len - count + 2);
321 *pos++ = WLAN_EID_EXT_SUPP_RATES;
322 *pos++ = rates_len - count;
324 for (i++; i < sband->n_bitrates; i++) {
325 if (BIT(i) & rates) {
326 int rate = sband->bitrates[i].bitrate;
327 *pos++ = (u8) (rate / 5);
332 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
333 /* 1. power capabilities */
334 pos = skb_put(skb, 4);
335 *pos++ = WLAN_EID_PWR_CAPABILITY;
337 *pos++ = 0; /* min tx power */
338 *pos++ = wk->chan->max_power; /* max tx power */
340 /* 2. supported channels */
341 /* TODO: get this in reg domain format */
342 pos = skb_put(skb, 2 * sband->n_channels + 2);
343 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
344 *pos++ = 2 * sband->n_channels;
345 for (i = 0; i < sband->n_channels; i++) {
346 *pos++ = ieee80211_frequency_to_channel(
347 sband->channels[i].center_freq);
348 *pos++ = 1; /* one channel in the subband*/
352 /* if present, add any custom IEs that go before HT */
353 if (wk->ie_len && wk->ie) {
354 static const u8 before_ht[] = {
357 WLAN_EID_EXT_SUPP_RATES,
358 WLAN_EID_PWR_CAPABILITY,
359 WLAN_EID_SUPPORTED_CHANNELS,
362 WLAN_EID_RRM_ENABLED_CAPABILITIES,
363 WLAN_EID_MOBILITY_DOMAIN,
364 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
366 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
367 before_ht, ARRAY_SIZE(before_ht),
369 pos = skb_put(skb, noffset - offset);
370 memcpy(pos, wk->ie + offset, noffset - offset);
374 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
375 local->hw.queues >= 4)
376 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
377 sband, wk->chan, wk->assoc.smps);
379 /* if present, add any custom non-vendor IEs that go after HT */
380 if (wk->ie_len && wk->ie) {
381 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
383 pos = skb_put(skb, noffset - offset);
384 memcpy(pos, wk->ie + offset, noffset - offset);
388 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
389 if (wk->assoc.uapsd_used) {
390 qos_info = local->uapsd_queues;
391 qos_info |= (local->uapsd_max_sp_len <<
392 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
397 pos = skb_put(skb, 9);
398 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
399 *pos++ = 7; /* len */
400 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
403 *pos++ = 2; /* WME */
404 *pos++ = 0; /* WME info */
405 *pos++ = 1; /* WME ver */
409 /* add any remaining custom (i.e. vendor specific here) IEs */
410 if (wk->ie_len && wk->ie) {
411 noffset = wk->ie_len;
412 pos = skb_put(skb, noffset - offset);
413 memcpy(pos, wk->ie + offset, noffset - offset);
416 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
417 ieee80211_tx_skb(sdata, skb);
420 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
421 struct ieee80211_work *wk)
423 struct cfg80211_bss *cbss;
424 u16 capa_val = WLAN_CAPABILITY_ESS;
426 if (wk->probe_auth.privacy)
427 capa_val |= WLAN_CAPABILITY_PRIVACY;
429 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
430 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
431 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
436 cfg80211_unlink_bss(local->hw.wiphy, cbss);
437 cfg80211_put_bss(cbss);
440 static enum work_action __must_check
441 ieee80211_direct_probe(struct ieee80211_work *wk)
443 struct ieee80211_sub_if_data *sdata = wk->sdata;
444 struct ieee80211_local *local = sdata->local;
446 wk->probe_auth.tries++;
447 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
448 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
449 sdata->name, wk->filter_ta);
452 * Most likely AP is not in the range so remove the
453 * bss struct for that AP.
455 ieee80211_remove_auth_bss(local, wk);
457 return WORK_ACT_TIMEOUT;
460 printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
461 sdata->name, wk->filter_ta, wk->probe_auth.tries);
464 * Direct probe is sent to broadcast address as some APs
465 * will not answer to direct packet in unassociated state.
467 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
468 wk->probe_auth.ssid_len, NULL, 0);
470 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
471 run_again(local, wk->timeout);
473 return WORK_ACT_NONE;
477 static enum work_action __must_check
478 ieee80211_authenticate(struct ieee80211_work *wk)
480 struct ieee80211_sub_if_data *sdata = wk->sdata;
481 struct ieee80211_local *local = sdata->local;
483 wk->probe_auth.tries++;
484 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
485 printk(KERN_DEBUG "%s: authentication with %pM"
486 " timed out\n", sdata->name, wk->filter_ta);
489 * Most likely AP is not in the range so remove the
490 * bss struct for that AP.
492 ieee80211_remove_auth_bss(local, wk);
494 return WORK_ACT_TIMEOUT;
497 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
498 sdata->name, wk->filter_ta, wk->probe_auth.tries);
500 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
501 wk->ie_len, wk->filter_ta, NULL, 0, 0);
502 wk->probe_auth.transaction = 2;
504 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
505 run_again(local, wk->timeout);
507 return WORK_ACT_NONE;
510 static enum work_action __must_check
511 ieee80211_associate(struct ieee80211_work *wk)
513 struct ieee80211_sub_if_data *sdata = wk->sdata;
514 struct ieee80211_local *local = sdata->local;
517 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
518 printk(KERN_DEBUG "%s: association with %pM"
520 sdata->name, wk->filter_ta);
523 * Most likely AP is not in the range so remove the
524 * bss struct for that AP.
527 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
529 return WORK_ACT_TIMEOUT;
532 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
533 sdata->name, wk->filter_ta, wk->assoc.tries);
534 ieee80211_send_assoc(sdata, wk);
536 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
537 run_again(local, wk->timeout);
539 return WORK_ACT_NONE;
542 static enum work_action __must_check
543 ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
546 * First time we run, do nothing -- the generic code will
547 * have switched to the right channel etc.
550 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
552 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
553 wk->chan, wk->chan_type,
554 wk->remain.duration, GFP_KERNEL);
556 return WORK_ACT_NONE;
559 return WORK_ACT_TIMEOUT;
562 static void ieee80211_auth_challenge(struct ieee80211_work *wk,
563 struct ieee80211_mgmt *mgmt,
566 struct ieee80211_sub_if_data *sdata = wk->sdata;
568 struct ieee802_11_elems elems;
570 pos = mgmt->u.auth.variable;
571 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
572 if (!elems.challenge)
574 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
575 elems.challenge - 2, elems.challenge_len + 2,
576 wk->filter_ta, wk->probe_auth.key,
577 wk->probe_auth.key_len, wk->probe_auth.key_idx);
578 wk->probe_auth.transaction = 4;
581 static enum work_action __must_check
582 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
583 struct ieee80211_mgmt *mgmt, size_t len)
585 u16 auth_alg, auth_transaction, status_code;
587 if (wk->type != IEEE80211_WORK_AUTH)
588 return WORK_ACT_MISMATCH;
591 return WORK_ACT_NONE;
593 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
594 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
595 status_code = le16_to_cpu(mgmt->u.auth.status_code);
597 if (auth_alg != wk->probe_auth.algorithm ||
598 auth_transaction != wk->probe_auth.transaction)
599 return WORK_ACT_NONE;
601 if (status_code != WLAN_STATUS_SUCCESS) {
602 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
603 wk->sdata->name, mgmt->sa, status_code);
604 return WORK_ACT_DONE;
607 switch (wk->probe_auth.algorithm) {
612 case WLAN_AUTH_SHARED_KEY:
613 if (wk->probe_auth.transaction != 4) {
614 ieee80211_auth_challenge(wk, mgmt, len);
615 /* need another frame */
616 return WORK_ACT_NONE;
621 return WORK_ACT_NONE;
624 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
625 return WORK_ACT_DONE;
628 static enum work_action __must_check
629 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
630 struct ieee80211_mgmt *mgmt, size_t len,
633 struct ieee80211_sub_if_data *sdata = wk->sdata;
634 struct ieee80211_local *local = sdata->local;
635 u16 capab_info, status_code, aid;
636 struct ieee802_11_elems elems;
639 if (wk->type != IEEE80211_WORK_ASSOC)
640 return WORK_ACT_MISMATCH;
643 * AssocResp and ReassocResp have identical structure, so process both
644 * of them in this function.
648 return WORK_ACT_NONE;
650 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
651 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
652 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
654 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
655 "status=%d aid=%d)\n",
656 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
657 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
659 pos = mgmt->u.assoc_resp.variable;
660 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
662 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
663 elems.timeout_int && elems.timeout_int_len == 5 &&
664 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
666 tu = get_unaligned_le32(elems.timeout_int + 1);
667 ms = tu * 1024 / 1000;
668 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
669 "comeback duration %u TU (%u ms)\n",
670 sdata->name, mgmt->sa, tu, ms);
671 wk->timeout = jiffies + msecs_to_jiffies(ms);
672 if (ms > IEEE80211_ASSOC_TIMEOUT)
673 run_again(local, wk->timeout);
674 return WORK_ACT_NONE;
677 if (status_code != WLAN_STATUS_SUCCESS)
678 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
679 sdata->name, mgmt->sa, status_code);
681 printk(KERN_DEBUG "%s: associated\n", sdata->name);
683 return WORK_ACT_DONE;
686 static enum work_action __must_check
687 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
688 struct ieee80211_mgmt *mgmt, size_t len,
689 struct ieee80211_rx_status *rx_status)
691 struct ieee80211_sub_if_data *sdata = wk->sdata;
692 struct ieee80211_local *local = sdata->local;
695 ASSERT_WORK_MTX(local);
697 if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
698 return WORK_ACT_MISMATCH;
701 return WORK_ACT_NONE;
703 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
705 return WORK_ACT_NONE;
707 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
708 return WORK_ACT_DONE;
711 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
714 struct ieee80211_rx_status *rx_status;
715 struct ieee80211_mgmt *mgmt;
716 struct ieee80211_work *wk;
717 enum work_action rma;
720 rx_status = (struct ieee80211_rx_status *) skb->cb;
721 mgmt = (struct ieee80211_mgmt *) skb->data;
722 fc = le16_to_cpu(mgmt->frame_control);
724 mutex_lock(&local->work_mtx);
726 list_for_each_entry(wk, &local->work_list, list) {
727 const u8 *bssid = NULL;
730 case IEEE80211_WORK_DIRECT_PROBE:
731 case IEEE80211_WORK_AUTH:
732 case IEEE80211_WORK_ASSOC:
733 bssid = wk->filter_ta;
740 * Before queuing, we already verified mgmt->sa,
741 * so this is needed just for matching.
743 if (compare_ether_addr(bssid, mgmt->bssid))
746 switch (fc & IEEE80211_FCTL_STYPE) {
747 case IEEE80211_STYPE_PROBE_RESP:
748 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
751 case IEEE80211_STYPE_AUTH:
752 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
754 case IEEE80211_STYPE_ASSOC_RESP:
755 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
758 case IEEE80211_STYPE_REASSOC_RESP:
759 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
768 * We've either received an unexpected frame, or we have
769 * multiple work items and need to match the frame to the
772 if (rma == WORK_ACT_MISMATCH)
776 * We've processed this frame for that work, so it can't
777 * belong to another work struct.
778 * NB: this is also required for correctness for 'rma'!
784 case WORK_ACT_MISMATCH:
785 /* ignore this unmatched frame */
790 list_del_rcu(&wk->list);
793 WARN(1, "unexpected: %d", rma);
796 mutex_unlock(&local->work_mtx);
798 if (rma != WORK_ACT_DONE)
801 switch (wk->done(wk, skb)) {
802 case WORK_DONE_DESTROY:
805 case WORK_DONE_REQUEUE:
807 wk->started = false; /* restart */
808 mutex_lock(&local->work_mtx);
809 list_add_tail(&wk->list, &local->work_list);
810 mutex_unlock(&local->work_mtx);
817 static void ieee80211_work_timer(unsigned long data)
819 struct ieee80211_local *local = (void *) data;
821 if (local->quiescing)
824 ieee80211_queue_work(&local->hw, &local->work_work);
827 static void ieee80211_work_work(struct work_struct *work)
829 struct ieee80211_local *local =
830 container_of(work, struct ieee80211_local, work_work);
832 struct ieee80211_work *wk, *tmp;
833 LIST_HEAD(free_work);
834 enum work_action rma;
835 bool remain_off_channel = false;
841 * ieee80211_queue_work() should have picked up most cases,
842 * here we'll pick the the rest.
844 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
847 /* first process frames to avoid timing out while a frame is pending */
848 while ((skb = skb_dequeue(&local->work_skb_queue)))
849 ieee80211_work_rx_queued_mgmt(local, skb);
851 ieee80211_recalc_idle(local);
853 mutex_lock(&local->work_mtx);
855 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
856 bool started = wk->started;
858 /* mark work as started if it's on the current off-channel */
859 if (!started && local->tmp_channel &&
860 wk->chan == local->tmp_channel &&
861 wk->chan_type == local->tmp_channel_type) {
863 wk->timeout = jiffies;
866 if (!started && !local->tmp_channel) {
868 * TODO: could optimize this by leaving the
869 * station vifs in awake mode if they
870 * happen to be on the same channel as
871 * the requested channel
873 ieee80211_offchannel_stop_beaconing(local);
874 ieee80211_offchannel_stop_station(local);
876 local->tmp_channel = wk->chan;
877 local->tmp_channel_type = wk->chan_type;
878 ieee80211_hw_config(local, 0);
880 wk->timeout = jiffies;
883 /* don't try to work with items that aren't started */
887 if (time_is_after_jiffies(wk->timeout)) {
889 * This work item isn't supposed to be worked on
890 * right now, but take care to adjust the timer
893 run_again(local, wk->timeout);
903 case IEEE80211_WORK_ABORT:
904 rma = WORK_ACT_TIMEOUT;
906 case IEEE80211_WORK_DIRECT_PROBE:
907 rma = ieee80211_direct_probe(wk);
909 case IEEE80211_WORK_AUTH:
910 rma = ieee80211_authenticate(wk);
912 case IEEE80211_WORK_ASSOC:
913 rma = ieee80211_associate(wk);
915 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
916 rma = ieee80211_remain_on_channel_timeout(wk);
920 wk->started = started;
924 /* might have changed the timeout */
925 run_again(local, wk->timeout);
927 case WORK_ACT_TIMEOUT:
928 list_del_rcu(&wk->list);
930 list_add(&wk->list, &free_work);
933 WARN(1, "unexpected: %d", rma);
937 list_for_each_entry(wk, &local->work_list, list) {
940 if (wk->chan != local->tmp_channel)
942 if (wk->chan_type != local->tmp_channel_type)
944 remain_off_channel = true;
947 if (!remain_off_channel && local->tmp_channel) {
948 local->tmp_channel = NULL;
949 ieee80211_hw_config(local, 0);
950 ieee80211_offchannel_return(local, true);
951 /* give connection some time to breathe */
952 run_again(local, jiffies + HZ/2);
955 mutex_lock(&local->scan_mtx);
957 if (list_empty(&local->work_list) && local->scan_req &&
959 ieee80211_queue_delayed_work(&local->hw,
961 round_jiffies_relative(0));
963 mutex_unlock(&local->scan_mtx);
965 mutex_unlock(&local->work_mtx);
967 ieee80211_recalc_idle(local);
969 list_for_each_entry_safe(wk, tmp, &free_work, list) {
976 void ieee80211_add_work(struct ieee80211_work *wk)
978 struct ieee80211_local *local;
980 if (WARN_ON(!wk->chan))
983 if (WARN_ON(!wk->sdata))
986 if (WARN_ON(!wk->done))
989 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
994 local = wk->sdata->local;
995 mutex_lock(&local->work_mtx);
996 list_add_tail(&wk->list, &local->work_list);
997 mutex_unlock(&local->work_mtx);
999 ieee80211_queue_work(&local->hw, &local->work_work);
1002 void ieee80211_work_init(struct ieee80211_local *local)
1004 mutex_init(&local->work_mtx);
1005 INIT_LIST_HEAD(&local->work_list);
1006 setup_timer(&local->work_timer, ieee80211_work_timer,
1007 (unsigned long)local);
1008 INIT_WORK(&local->work_work, ieee80211_work_work);
1009 skb_queue_head_init(&local->work_skb_queue);
1012 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
1014 struct ieee80211_local *local = sdata->local;
1015 struct ieee80211_work *wk;
1017 mutex_lock(&local->work_mtx);
1018 list_for_each_entry(wk, &local->work_list, list) {
1019 if (wk->sdata != sdata)
1021 wk->type = IEEE80211_WORK_ABORT;
1023 wk->timeout = jiffies;
1025 mutex_unlock(&local->work_mtx);
1027 /* run cleanups etc. */
1028 ieee80211_work_work(&local->work_work);
1030 mutex_lock(&local->work_mtx);
1031 list_for_each_entry(wk, &local->work_list, list) {
1032 if (wk->sdata != sdata)
1037 mutex_unlock(&local->work_mtx);
1040 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1041 struct sk_buff *skb)
1043 struct ieee80211_local *local = sdata->local;
1044 struct ieee80211_mgmt *mgmt;
1045 struct ieee80211_work *wk;
1049 return RX_DROP_MONITOR;
1051 mgmt = (struct ieee80211_mgmt *) skb->data;
1052 fc = le16_to_cpu(mgmt->frame_control);
1054 list_for_each_entry_rcu(wk, &local->work_list, list) {
1055 if (sdata != wk->sdata)
1057 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1059 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1062 switch (fc & IEEE80211_FCTL_STYPE) {
1063 case IEEE80211_STYPE_AUTH:
1064 case IEEE80211_STYPE_PROBE_RESP:
1065 case IEEE80211_STYPE_ASSOC_RESP:
1066 case IEEE80211_STYPE_REASSOC_RESP:
1067 skb_queue_tail(&local->work_skb_queue, skb);
1068 ieee80211_queue_work(&local->hw, &local->work_work);
1076 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1077 struct sk_buff *skb)
1080 * We are done serving the remain-on-channel command.
1082 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1083 wk->chan, wk->chan_type,
1086 return WORK_DONE_DESTROY;
1089 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1090 struct ieee80211_channel *chan,
1091 enum nl80211_channel_type channel_type,
1092 unsigned int duration, u64 *cookie)
1094 struct ieee80211_work *wk;
1096 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1100 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1102 wk->chan_type = channel_type;
1104 wk->done = ieee80211_remain_done;
1106 wk->remain.duration = duration;
1108 *cookie = (unsigned long) wk;
1110 ieee80211_add_work(wk);
1115 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1118 struct ieee80211_local *local = sdata->local;
1119 struct ieee80211_work *wk, *tmp;
1122 mutex_lock(&local->work_mtx);
1123 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1124 if ((unsigned long) wk == cookie) {
1125 wk->timeout = jiffies;
1130 mutex_unlock(&local->work_mtx);
1135 ieee80211_queue_work(&local->hw, &local->work_work);