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
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
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);
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,
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) {
101 /* frame sending functions */
103 static void ieee80211_add_ht_ie(struct sk_buff *skb, const u8 *ht_info_ie,
104 struct ieee80211_supported_band *sband,
105 struct ieee80211_channel *channel,
106 enum ieee80211_smps_mode smps)
108 struct ieee80211_ht_info *ht_info;
110 u32 flags = channel->flags;
111 u16 cap = sband->ht_cap.cap;
114 if (!sband->ht_cap.ht_supported)
120 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
123 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
125 /* determine capability flags */
127 if (ieee80211_disable_40mhz_24ghz &&
128 sband->band == IEEE80211_BAND_2GHZ) {
129 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
130 cap &= ~IEEE80211_HT_CAP_SGI_40;
133 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
134 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
135 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
136 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
137 cap &= ~IEEE80211_HT_CAP_SGI_40;
140 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
141 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
142 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
143 cap &= ~IEEE80211_HT_CAP_SGI_40;
148 /* set SM PS mode properly */
149 cap &= ~IEEE80211_HT_CAP_SM_PS;
151 case IEEE80211_SMPS_AUTOMATIC:
152 case IEEE80211_SMPS_NUM_MODES:
154 case IEEE80211_SMPS_OFF:
155 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
156 IEEE80211_HT_CAP_SM_PS_SHIFT;
158 case IEEE80211_SMPS_STATIC:
159 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
160 IEEE80211_HT_CAP_SM_PS_SHIFT;
162 case IEEE80211_SMPS_DYNAMIC:
163 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
164 IEEE80211_HT_CAP_SM_PS_SHIFT;
168 /* reserve and fill IE */
170 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
171 *pos++ = WLAN_EID_HT_CAPABILITY;
172 *pos++ = sizeof(struct ieee80211_ht_cap);
173 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
175 /* capability flags */
176 tmp = cpu_to_le16(cap);
177 memcpy(pos, &tmp, sizeof(u16));
180 /* AMPDU parameters */
181 *pos++ = sband->ht_cap.ampdu_factor |
182 (sband->ht_cap.ampdu_density <<
183 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
186 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
187 pos += sizeof(sband->ht_cap.mcs);
189 /* extended capabilities */
190 pos += sizeof(__le16);
192 /* BF capabilities */
193 pos += sizeof(__le32);
195 /* antenna selection */
199 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
200 struct ieee80211_work *wk)
202 struct ieee80211_local *local = sdata->local;
204 struct ieee80211_mgmt *mgmt;
207 size_t offset = 0, noffset;
208 int i, len, count, rates_len, supp_rates_len;
210 struct ieee80211_supported_band *sband;
213 sband = local->hw.wiphy->bands[wk->chan->band];
215 if (wk->assoc.supp_rates_len) {
217 * Get all rates supported by the device and the AP as
218 * some APs don't like getting a superset of their rates
219 * in the association request (e.g. D-Link DAP 1353 in
222 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
223 wk->assoc.supp_rates_len,
227 * In case AP not provide any supported rates information
228 * before association, we send information element(s) with
229 * all rates that we support.
232 rates_len = sband->n_bitrates;
235 skb = alloc_skb(local->hw.extra_tx_headroom +
236 sizeof(*mgmt) + /* bit too much but doesn't matter */
237 2 + wk->assoc.ssid_len + /* SSID */
238 4 + rates_len + /* (extended) rates */
239 4 + /* power capability */
240 2 + 2 * sband->n_channels + /* supported channels */
241 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
242 wk->ie_len + /* extra IEs */
246 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
247 "frame\n", sdata->name);
250 skb_reserve(skb, local->hw.extra_tx_headroom);
252 capab = WLAN_CAPABILITY_ESS;
254 if (sband->band == IEEE80211_BAND_2GHZ) {
255 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
256 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
257 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
258 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
261 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
262 capab |= WLAN_CAPABILITY_PRIVACY;
264 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
265 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
266 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
268 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
270 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
271 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
272 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
274 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
276 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
277 IEEE80211_STYPE_REASSOC_REQ);
278 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
279 mgmt->u.reassoc_req.listen_interval =
280 cpu_to_le16(local->hw.conf.listen_interval);
281 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
285 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
286 IEEE80211_STYPE_ASSOC_REQ);
287 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
288 mgmt->u.assoc_req.listen_interval =
289 cpu_to_le16(local->hw.conf.listen_interval);
293 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
294 *pos++ = WLAN_EID_SSID;
295 *pos++ = wk->assoc.ssid_len;
296 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
298 /* add all rates which were marked to be used above */
299 supp_rates_len = rates_len;
300 if (supp_rates_len > 8)
303 len = sband->n_bitrates;
304 pos = skb_put(skb, supp_rates_len + 2);
305 *pos++ = WLAN_EID_SUPP_RATES;
306 *pos++ = supp_rates_len;
309 for (i = 0; i < sband->n_bitrates; i++) {
310 if (BIT(i) & rates) {
311 int rate = sband->bitrates[i].bitrate;
312 *pos++ = (u8) (rate / 5);
318 if (rates_len > count) {
319 pos = skb_put(skb, rates_len - count + 2);
320 *pos++ = WLAN_EID_EXT_SUPP_RATES;
321 *pos++ = rates_len - count;
323 for (i++; i < sband->n_bitrates; i++) {
324 if (BIT(i) & rates) {
325 int rate = sband->bitrates[i].bitrate;
326 *pos++ = (u8) (rate / 5);
331 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
332 /* 1. power capabilities */
333 pos = skb_put(skb, 4);
334 *pos++ = WLAN_EID_PWR_CAPABILITY;
336 *pos++ = 0; /* min tx power */
337 *pos++ = wk->chan->max_power; /* max tx power */
339 /* 2. supported channels */
340 /* TODO: get this in reg domain format */
341 pos = skb_put(skb, 2 * sband->n_channels + 2);
342 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
343 *pos++ = 2 * sband->n_channels;
344 for (i = 0; i < sband->n_channels; i++) {
345 *pos++ = ieee80211_frequency_to_channel(
346 sband->channels[i].center_freq);
347 *pos++ = 1; /* one channel in the subband*/
351 /* if present, add any custom IEs that go before HT */
352 if (wk->ie_len && wk->ie) {
353 static const u8 before_ht[] = {
356 WLAN_EID_EXT_SUPP_RATES,
357 WLAN_EID_PWR_CAPABILITY,
358 WLAN_EID_SUPPORTED_CHANNELS,
361 WLAN_EID_RRM_ENABLED_CAPABILITIES,
362 WLAN_EID_MOBILITY_DOMAIN,
363 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
365 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
366 before_ht, ARRAY_SIZE(before_ht),
368 pos = skb_put(skb, noffset - offset);
369 memcpy(pos, wk->ie + offset, noffset - offset);
373 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
374 local->hw.queues >= 4)
375 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
376 sband, wk->chan, wk->assoc.smps);
378 /* if present, add any custom non-vendor IEs that go after HT */
379 if (wk->ie_len && wk->ie) {
380 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
382 pos = skb_put(skb, noffset - offset);
383 memcpy(pos, wk->ie + offset, noffset - offset);
387 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
388 if (wk->assoc.uapsd_used) {
389 qos_info = local->uapsd_queues;
390 qos_info |= (local->uapsd_max_sp_len <<
391 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
396 pos = skb_put(skb, 9);
397 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
398 *pos++ = 7; /* len */
399 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
402 *pos++ = 2; /* WME */
403 *pos++ = 0; /* WME info */
404 *pos++ = 1; /* WME ver */
408 /* add any remaining custom (i.e. vendor specific here) IEs */
409 if (wk->ie_len && wk->ie) {
410 noffset = wk->ie_len;
411 pos = skb_put(skb, noffset - offset);
412 memcpy(pos, wk->ie + offset, noffset - offset);
415 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
416 ieee80211_tx_skb(sdata, skb);
419 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
420 struct ieee80211_work *wk)
422 struct cfg80211_bss *cbss;
423 u16 capa_val = WLAN_CAPABILITY_ESS;
425 if (wk->probe_auth.privacy)
426 capa_val |= WLAN_CAPABILITY_PRIVACY;
428 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
429 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
430 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
435 cfg80211_unlink_bss(local->hw.wiphy, cbss);
436 cfg80211_put_bss(cbss);
439 static enum work_action __must_check
440 ieee80211_direct_probe(struct ieee80211_work *wk)
442 struct ieee80211_sub_if_data *sdata = wk->sdata;
443 struct ieee80211_local *local = sdata->local;
445 wk->probe_auth.tries++;
446 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
447 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
448 sdata->name, wk->filter_ta);
451 * Most likely AP is not in the range so remove the
452 * bss struct for that AP.
454 ieee80211_remove_auth_bss(local, wk);
456 return WORK_ACT_TIMEOUT;
459 printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
460 sdata->name, wk->filter_ta, wk->probe_auth.tries);
463 * Direct probe is sent to broadcast address as some APs
464 * will not answer to direct packet in unassociated state.
466 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
467 wk->probe_auth.ssid_len, NULL, 0);
469 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
470 run_again(local, wk->timeout);
472 return WORK_ACT_NONE;
476 static enum work_action __must_check
477 ieee80211_authenticate(struct ieee80211_work *wk)
479 struct ieee80211_sub_if_data *sdata = wk->sdata;
480 struct ieee80211_local *local = sdata->local;
482 wk->probe_auth.tries++;
483 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
484 printk(KERN_DEBUG "%s: authentication with %pM"
485 " timed out\n", sdata->name, wk->filter_ta);
488 * Most likely AP is not in the range so remove the
489 * bss struct for that AP.
491 ieee80211_remove_auth_bss(local, wk);
493 return WORK_ACT_TIMEOUT;
496 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
497 sdata->name, wk->filter_ta, wk->probe_auth.tries);
499 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
500 wk->ie_len, wk->filter_ta, NULL, 0, 0);
501 wk->probe_auth.transaction = 2;
503 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
504 run_again(local, wk->timeout);
506 return WORK_ACT_NONE;
509 static enum work_action __must_check
510 ieee80211_associate(struct ieee80211_work *wk)
512 struct ieee80211_sub_if_data *sdata = wk->sdata;
513 struct ieee80211_local *local = sdata->local;
516 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
517 printk(KERN_DEBUG "%s: association with %pM"
519 sdata->name, wk->filter_ta);
522 * Most likely AP is not in the range so remove the
523 * bss struct for that AP.
526 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
528 return WORK_ACT_TIMEOUT;
531 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
532 sdata->name, wk->filter_ta, wk->assoc.tries);
533 ieee80211_send_assoc(sdata, wk);
535 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
536 run_again(local, wk->timeout);
538 return WORK_ACT_NONE;
541 static enum work_action __must_check
542 ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
545 * First time we run, do nothing -- the generic code will
546 * have switched to the right channel etc.
549 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
551 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
552 wk->chan, wk->chan_type,
553 wk->remain.duration, GFP_KERNEL);
555 return WORK_ACT_NONE;
558 return WORK_ACT_TIMEOUT;
561 static void ieee80211_auth_challenge(struct ieee80211_work *wk,
562 struct ieee80211_mgmt *mgmt,
565 struct ieee80211_sub_if_data *sdata = wk->sdata;
567 struct ieee802_11_elems elems;
569 pos = mgmt->u.auth.variable;
570 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
571 if (!elems.challenge)
573 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
574 elems.challenge - 2, elems.challenge_len + 2,
575 wk->filter_ta, wk->probe_auth.key,
576 wk->probe_auth.key_len, wk->probe_auth.key_idx);
577 wk->probe_auth.transaction = 4;
580 static enum work_action __must_check
581 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
582 struct ieee80211_mgmt *mgmt, size_t len)
584 u16 auth_alg, auth_transaction, status_code;
586 if (wk->type != IEEE80211_WORK_AUTH)
587 return WORK_ACT_NONE;
590 return WORK_ACT_NONE;
592 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
593 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
594 status_code = le16_to_cpu(mgmt->u.auth.status_code);
596 if (auth_alg != wk->probe_auth.algorithm ||
597 auth_transaction != wk->probe_auth.transaction)
598 return WORK_ACT_NONE;
600 if (status_code != WLAN_STATUS_SUCCESS) {
601 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
602 wk->sdata->name, mgmt->sa, status_code);
603 return WORK_ACT_DONE;
606 switch (wk->probe_auth.algorithm) {
611 case WLAN_AUTH_SHARED_KEY:
612 if (wk->probe_auth.transaction != 4) {
613 ieee80211_auth_challenge(wk, mgmt, len);
614 /* need another frame */
615 return WORK_ACT_NONE;
620 return WORK_ACT_NONE;
623 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
624 return WORK_ACT_DONE;
627 static enum work_action __must_check
628 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
629 struct ieee80211_mgmt *mgmt, size_t len,
632 struct ieee80211_sub_if_data *sdata = wk->sdata;
633 struct ieee80211_local *local = sdata->local;
634 u16 capab_info, status_code, aid;
635 struct ieee802_11_elems elems;
639 * AssocResp and ReassocResp have identical structure, so process both
640 * of them in this function.
644 return WORK_ACT_NONE;
646 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
647 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
648 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
650 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
651 "status=%d aid=%d)\n",
652 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
653 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
655 pos = mgmt->u.assoc_resp.variable;
656 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
658 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
659 elems.timeout_int && elems.timeout_int_len == 5 &&
660 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
662 tu = get_unaligned_le32(elems.timeout_int + 1);
663 ms = tu * 1024 / 1000;
664 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
665 "comeback duration %u TU (%u ms)\n",
666 sdata->name, mgmt->sa, tu, ms);
667 wk->timeout = jiffies + msecs_to_jiffies(ms);
668 if (ms > IEEE80211_ASSOC_TIMEOUT)
669 run_again(local, wk->timeout);
670 return WORK_ACT_NONE;
673 if (status_code != WLAN_STATUS_SUCCESS)
674 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
675 sdata->name, mgmt->sa, status_code);
677 printk(KERN_DEBUG "%s: associated\n", sdata->name);
679 return WORK_ACT_DONE;
682 static enum work_action __must_check
683 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
684 struct ieee80211_mgmt *mgmt, size_t len,
685 struct ieee80211_rx_status *rx_status)
687 struct ieee80211_sub_if_data *sdata = wk->sdata;
688 struct ieee80211_local *local = sdata->local;
691 ASSERT_WORK_MTX(local);
693 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
695 return WORK_ACT_NONE;
697 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
698 return WORK_ACT_DONE;
701 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
704 struct ieee80211_rx_status *rx_status;
705 struct ieee80211_mgmt *mgmt;
706 struct ieee80211_work *wk;
707 enum work_action rma = WORK_ACT_NONE;
710 rx_status = (struct ieee80211_rx_status *) skb->cb;
711 mgmt = (struct ieee80211_mgmt *) skb->data;
712 fc = le16_to_cpu(mgmt->frame_control);
714 mutex_lock(&local->work_mtx);
716 list_for_each_entry(wk, &local->work_list, list) {
717 const u8 *bssid = NULL;
720 case IEEE80211_WORK_DIRECT_PROBE:
721 case IEEE80211_WORK_AUTH:
722 case IEEE80211_WORK_ASSOC:
723 bssid = wk->filter_ta;
730 * Before queuing, we already verified mgmt->sa,
731 * so this is needed just for matching.
733 if (compare_ether_addr(bssid, mgmt->bssid))
736 switch (fc & IEEE80211_FCTL_STYPE) {
737 case IEEE80211_STYPE_PROBE_RESP:
738 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
741 case IEEE80211_STYPE_AUTH:
742 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
744 case IEEE80211_STYPE_ASSOC_RESP:
745 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
748 case IEEE80211_STYPE_REASSOC_RESP:
749 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
756 * We've processed this frame for that work, so it can't
757 * belong to another work struct.
758 * NB: this is also required for correctness for 'rma'!
767 list_del_rcu(&wk->list);
770 WARN(1, "unexpected: %d", rma);
773 mutex_unlock(&local->work_mtx);
775 if (rma != WORK_ACT_DONE)
778 switch (wk->done(wk, skb)) {
779 case WORK_DONE_DESTROY:
782 case WORK_DONE_REQUEUE:
784 wk->started = false; /* restart */
785 mutex_lock(&local->work_mtx);
786 list_add_tail(&wk->list, &local->work_list);
787 mutex_unlock(&local->work_mtx);
794 static void ieee80211_work_timer(unsigned long data)
796 struct ieee80211_local *local = (void *) data;
798 if (local->quiescing)
801 ieee80211_queue_work(&local->hw, &local->work_work);
804 static void ieee80211_work_work(struct work_struct *work)
806 struct ieee80211_local *local =
807 container_of(work, struct ieee80211_local, work_work);
809 struct ieee80211_work *wk, *tmp;
810 LIST_HEAD(free_work);
811 enum work_action rma;
812 bool remain_off_channel = false;
818 * ieee80211_queue_work() should have picked up most cases,
819 * here we'll pick the the rest.
821 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
824 /* first process frames to avoid timing out while a frame is pending */
825 while ((skb = skb_dequeue(&local->work_skb_queue)))
826 ieee80211_work_rx_queued_mgmt(local, skb);
828 ieee80211_recalc_idle(local);
830 mutex_lock(&local->work_mtx);
832 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
833 bool started = wk->started;
835 /* mark work as started if it's on the current off-channel */
836 if (!started && local->tmp_channel &&
837 wk->chan == local->tmp_channel &&
838 wk->chan_type == local->tmp_channel_type) {
840 wk->timeout = jiffies;
843 if (!started && !local->tmp_channel) {
845 * TODO: could optimize this by leaving the
846 * station vifs in awake mode if they
847 * happen to be on the same channel as
848 * the requested channel
850 ieee80211_offchannel_stop_beaconing(local);
851 ieee80211_offchannel_stop_station(local);
853 local->tmp_channel = wk->chan;
854 local->tmp_channel_type = wk->chan_type;
855 ieee80211_hw_config(local, 0);
857 wk->timeout = jiffies;
860 /* don't try to work with items that aren't started */
864 if (time_is_after_jiffies(wk->timeout)) {
866 * This work item isn't supposed to be worked on
867 * right now, but take care to adjust the timer
870 run_again(local, wk->timeout);
880 case IEEE80211_WORK_ABORT:
881 rma = WORK_ACT_TIMEOUT;
883 case IEEE80211_WORK_DIRECT_PROBE:
884 rma = ieee80211_direct_probe(wk);
886 case IEEE80211_WORK_AUTH:
887 rma = ieee80211_authenticate(wk);
889 case IEEE80211_WORK_ASSOC:
890 rma = ieee80211_associate(wk);
892 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
893 rma = ieee80211_remain_on_channel_timeout(wk);
897 wk->started = started;
901 /* might have changed the timeout */
902 run_again(local, wk->timeout);
904 case WORK_ACT_TIMEOUT:
905 list_del_rcu(&wk->list);
907 list_add(&wk->list, &free_work);
910 WARN(1, "unexpected: %d", rma);
914 list_for_each_entry(wk, &local->work_list, list) {
917 if (wk->chan != local->tmp_channel)
919 if (wk->chan_type != local->tmp_channel_type)
921 remain_off_channel = true;
924 if (!remain_off_channel && local->tmp_channel) {
925 local->tmp_channel = NULL;
926 ieee80211_hw_config(local, 0);
927 ieee80211_offchannel_return(local, true);
928 /* give connection some time to breathe */
929 run_again(local, jiffies + HZ/2);
932 mutex_lock(&local->scan_mtx);
934 if (list_empty(&local->work_list) && local->scan_req &&
936 ieee80211_queue_delayed_work(&local->hw,
938 round_jiffies_relative(0));
940 mutex_unlock(&local->scan_mtx);
942 mutex_unlock(&local->work_mtx);
944 ieee80211_recalc_idle(local);
946 list_for_each_entry_safe(wk, tmp, &free_work, list) {
953 void ieee80211_add_work(struct ieee80211_work *wk)
955 struct ieee80211_local *local;
957 if (WARN_ON(!wk->chan))
960 if (WARN_ON(!wk->sdata))
963 if (WARN_ON(!wk->done))
966 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
971 local = wk->sdata->local;
972 mutex_lock(&local->work_mtx);
973 list_add_tail(&wk->list, &local->work_list);
974 mutex_unlock(&local->work_mtx);
976 ieee80211_queue_work(&local->hw, &local->work_work);
979 void ieee80211_work_init(struct ieee80211_local *local)
981 mutex_init(&local->work_mtx);
982 INIT_LIST_HEAD(&local->work_list);
983 setup_timer(&local->work_timer, ieee80211_work_timer,
984 (unsigned long)local);
985 INIT_WORK(&local->work_work, ieee80211_work_work);
986 skb_queue_head_init(&local->work_skb_queue);
989 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
991 struct ieee80211_local *local = sdata->local;
992 struct ieee80211_work *wk;
994 mutex_lock(&local->work_mtx);
995 list_for_each_entry(wk, &local->work_list, list) {
996 if (wk->sdata != sdata)
998 wk->type = IEEE80211_WORK_ABORT;
1000 wk->timeout = jiffies;
1002 mutex_unlock(&local->work_mtx);
1004 /* run cleanups etc. */
1005 ieee80211_work_work(&local->work_work);
1007 mutex_lock(&local->work_mtx);
1008 list_for_each_entry(wk, &local->work_list, list) {
1009 if (wk->sdata != sdata)
1014 mutex_unlock(&local->work_mtx);
1017 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1018 struct sk_buff *skb)
1020 struct ieee80211_local *local = sdata->local;
1021 struct ieee80211_mgmt *mgmt;
1022 struct ieee80211_work *wk;
1026 return RX_DROP_MONITOR;
1028 mgmt = (struct ieee80211_mgmt *) skb->data;
1029 fc = le16_to_cpu(mgmt->frame_control);
1031 list_for_each_entry_rcu(wk, &local->work_list, list) {
1032 if (sdata != wk->sdata)
1034 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1036 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1039 switch (fc & IEEE80211_FCTL_STYPE) {
1040 case IEEE80211_STYPE_AUTH:
1041 case IEEE80211_STYPE_PROBE_RESP:
1042 case IEEE80211_STYPE_ASSOC_RESP:
1043 case IEEE80211_STYPE_REASSOC_RESP:
1044 skb_queue_tail(&local->work_skb_queue, skb);
1045 ieee80211_queue_work(&local->hw, &local->work_work);
1053 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1054 struct sk_buff *skb)
1057 * We are done serving the remain-on-channel command.
1059 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1060 wk->chan, wk->chan_type,
1063 return WORK_DONE_DESTROY;
1066 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1067 struct ieee80211_channel *chan,
1068 enum nl80211_channel_type channel_type,
1069 unsigned int duration, u64 *cookie)
1071 struct ieee80211_work *wk;
1073 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1077 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1079 wk->chan_type = channel_type;
1081 wk->done = ieee80211_remain_done;
1083 wk->remain.duration = duration;
1085 *cookie = (unsigned long) wk;
1087 ieee80211_add_work(wk);
1092 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1095 struct ieee80211_local *local = sdata->local;
1096 struct ieee80211_work *wk, *tmp;
1099 mutex_lock(&local->work_mtx);
1100 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1101 if ((unsigned long) wk == cookie) {
1102 wk->timeout = jiffies;
1107 mutex_unlock(&local->work_mtx);
1112 ieee80211_queue_work(&local->hw, &local->work_work);