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"
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
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];
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,
226 skb = alloc_skb(local->hw.extra_tx_headroom +
227 sizeof(*mgmt) + /* bit too much but doesn't matter */
228 2 + wk->assoc.ssid_len + /* SSID */
229 4 + rates_len + /* (extended) rates */
230 4 + /* power capability */
231 2 + 2 * sband->n_channels + /* supported channels */
232 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
233 wk->ie_len + /* extra IEs */
237 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
238 "frame\n", sdata->name);
241 skb_reserve(skb, local->hw.extra_tx_headroom);
243 capab = WLAN_CAPABILITY_ESS;
245 if (sband->band == IEEE80211_BAND_2GHZ) {
246 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
247 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
248 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
249 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
252 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
253 capab |= WLAN_CAPABILITY_PRIVACY;
255 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
256 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
257 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
259 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
261 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
262 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
263 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
265 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
267 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
268 IEEE80211_STYPE_REASSOC_REQ);
269 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
270 mgmt->u.reassoc_req.listen_interval =
271 cpu_to_le16(local->hw.conf.listen_interval);
272 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
276 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
277 IEEE80211_STYPE_ASSOC_REQ);
278 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
279 mgmt->u.assoc_req.listen_interval =
280 cpu_to_le16(local->hw.conf.listen_interval);
284 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
285 *pos++ = WLAN_EID_SSID;
286 *pos++ = wk->assoc.ssid_len;
287 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
289 /* add all rates which were marked to be used above */
290 supp_rates_len = rates_len;
291 if (supp_rates_len > 8)
294 len = sband->n_bitrates;
295 pos = skb_put(skb, supp_rates_len + 2);
296 *pos++ = WLAN_EID_SUPP_RATES;
297 *pos++ = supp_rates_len;
300 for (i = 0; i < sband->n_bitrates; i++) {
301 if (BIT(i) & rates) {
302 int rate = sband->bitrates[i].bitrate;
303 *pos++ = (u8) (rate / 5);
309 if (rates_len > count) {
310 pos = skb_put(skb, rates_len - count + 2);
311 *pos++ = WLAN_EID_EXT_SUPP_RATES;
312 *pos++ = rates_len - count;
314 for (i++; i < sband->n_bitrates; i++) {
315 if (BIT(i) & rates) {
316 int rate = sband->bitrates[i].bitrate;
317 *pos++ = (u8) (rate / 5);
322 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
323 /* 1. power capabilities */
324 pos = skb_put(skb, 4);
325 *pos++ = WLAN_EID_PWR_CAPABILITY;
327 *pos++ = 0; /* min tx power */
328 *pos++ = wk->chan->max_power; /* max tx power */
330 /* 2. supported channels */
331 /* TODO: get this in reg domain format */
332 pos = skb_put(skb, 2 * sband->n_channels + 2);
333 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
334 *pos++ = 2 * sband->n_channels;
335 for (i = 0; i < sband->n_channels; i++) {
336 *pos++ = ieee80211_frequency_to_channel(
337 sband->channels[i].center_freq);
338 *pos++ = 1; /* one channel in the subband*/
342 /* if present, add any custom IEs that go before HT */
343 if (wk->ie_len && wk->ie) {
344 static const u8 before_ht[] = {
347 WLAN_EID_EXT_SUPP_RATES,
348 WLAN_EID_PWR_CAPABILITY,
349 WLAN_EID_SUPPORTED_CHANNELS,
352 WLAN_EID_RRM_ENABLED_CAPABILITIES,
353 WLAN_EID_MOBILITY_DOMAIN,
354 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
356 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
357 before_ht, ARRAY_SIZE(before_ht),
359 pos = skb_put(skb, noffset - offset);
360 memcpy(pos, wk->ie + offset, noffset - offset);
364 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
365 local->hw.queues >= 4)
366 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
367 sband, wk->chan, wk->assoc.smps);
369 /* if present, add any custom non-vendor IEs that go after HT */
370 if (wk->ie_len && wk->ie) {
371 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
373 pos = skb_put(skb, noffset - offset);
374 memcpy(pos, wk->ie + offset, noffset - offset);
378 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
379 if (wk->assoc.uapsd_used) {
380 qos_info = local->uapsd_queues;
381 qos_info |= (local->uapsd_max_sp_len <<
382 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
387 pos = skb_put(skb, 9);
388 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
389 *pos++ = 7; /* len */
390 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
393 *pos++ = 2; /* WME */
394 *pos++ = 0; /* WME info */
395 *pos++ = 1; /* WME ver */
399 /* add any remaining custom (i.e. vendor specific here) IEs */
400 if (wk->ie_len && wk->ie) {
401 noffset = wk->ie_len;
402 pos = skb_put(skb, noffset - offset);
403 memcpy(pos, wk->ie + offset, noffset - offset);
406 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
407 ieee80211_tx_skb(sdata, skb);
410 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
411 struct ieee80211_work *wk)
413 struct cfg80211_bss *cbss;
414 u16 capa_val = WLAN_CAPABILITY_ESS;
416 if (wk->probe_auth.privacy)
417 capa_val |= WLAN_CAPABILITY_PRIVACY;
419 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
420 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
421 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
426 cfg80211_unlink_bss(local->hw.wiphy, cbss);
427 cfg80211_put_bss(cbss);
430 static enum work_action __must_check
431 ieee80211_direct_probe(struct ieee80211_work *wk)
433 struct ieee80211_sub_if_data *sdata = wk->sdata;
434 struct ieee80211_local *local = sdata->local;
436 wk->probe_auth.tries++;
437 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
438 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
439 sdata->name, wk->filter_ta);
442 * Most likely AP is not in the range so remove the
443 * bss struct for that AP.
445 ieee80211_remove_auth_bss(local, wk);
447 return WORK_ACT_TIMEOUT;
450 printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
451 sdata->name, wk->filter_ta, wk->probe_auth.tries);
454 * Direct probe is sent to broadcast address as some APs
455 * will not answer to direct packet in unassociated state.
457 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
458 wk->probe_auth.ssid_len, NULL, 0);
460 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
461 run_again(local, wk->timeout);
463 return WORK_ACT_NONE;
467 static enum work_action __must_check
468 ieee80211_authenticate(struct ieee80211_work *wk)
470 struct ieee80211_sub_if_data *sdata = wk->sdata;
471 struct ieee80211_local *local = sdata->local;
473 wk->probe_auth.tries++;
474 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
475 printk(KERN_DEBUG "%s: authentication with %pM"
476 " timed out\n", sdata->name, wk->filter_ta);
479 * Most likely AP is not in the range so remove the
480 * bss struct for that AP.
482 ieee80211_remove_auth_bss(local, wk);
484 return WORK_ACT_TIMEOUT;
487 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
488 sdata->name, wk->filter_ta, wk->probe_auth.tries);
490 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
491 wk->ie_len, wk->filter_ta, NULL, 0, 0);
492 wk->probe_auth.transaction = 2;
494 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
495 run_again(local, wk->timeout);
497 return WORK_ACT_NONE;
500 static enum work_action __must_check
501 ieee80211_associate(struct ieee80211_work *wk)
503 struct ieee80211_sub_if_data *sdata = wk->sdata;
504 struct ieee80211_local *local = sdata->local;
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 void ieee80211_auth_challenge(struct ieee80211_work *wk,
553 struct ieee80211_mgmt *mgmt,
556 struct ieee80211_sub_if_data *sdata = wk->sdata;
558 struct ieee802_11_elems elems;
560 pos = mgmt->u.auth.variable;
561 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
562 if (!elems.challenge)
564 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
565 elems.challenge - 2, elems.challenge_len + 2,
566 wk->filter_ta, wk->probe_auth.key,
567 wk->probe_auth.key_len, wk->probe_auth.key_idx);
568 wk->probe_auth.transaction = 4;
571 static enum work_action __must_check
572 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
573 struct ieee80211_mgmt *mgmt, size_t len)
575 u16 auth_alg, auth_transaction, status_code;
577 if (wk->type != IEEE80211_WORK_AUTH)
578 return WORK_ACT_NONE;
581 return WORK_ACT_NONE;
583 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
584 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
585 status_code = le16_to_cpu(mgmt->u.auth.status_code);
587 if (auth_alg != wk->probe_auth.algorithm ||
588 auth_transaction != wk->probe_auth.transaction)
589 return WORK_ACT_NONE;
591 if (status_code != WLAN_STATUS_SUCCESS) {
592 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
593 wk->sdata->name, mgmt->sa, status_code);
594 return WORK_ACT_DONE;
597 switch (wk->probe_auth.algorithm) {
602 case WLAN_AUTH_SHARED_KEY:
603 if (wk->probe_auth.transaction != 4) {
604 ieee80211_auth_challenge(wk, mgmt, len);
605 /* need another frame */
606 return WORK_ACT_NONE;
611 return WORK_ACT_NONE;
614 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
615 return WORK_ACT_DONE;
618 static enum work_action __must_check
619 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
620 struct ieee80211_mgmt *mgmt, size_t len,
623 struct ieee80211_sub_if_data *sdata = wk->sdata;
624 struct ieee80211_local *local = sdata->local;
625 u16 capab_info, status_code, aid;
626 struct ieee802_11_elems elems;
630 * AssocResp and ReassocResp have identical structure, so process both
631 * of them in this function.
635 return WORK_ACT_NONE;
637 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
638 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
639 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
641 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
642 "status=%d aid=%d)\n",
643 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
644 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
646 pos = mgmt->u.assoc_resp.variable;
647 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
649 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
650 elems.timeout_int && elems.timeout_int_len == 5 &&
651 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
653 tu = get_unaligned_le32(elems.timeout_int + 1);
654 ms = tu * 1024 / 1000;
655 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
656 "comeback duration %u TU (%u ms)\n",
657 sdata->name, mgmt->sa, tu, ms);
658 wk->timeout = jiffies + msecs_to_jiffies(ms);
659 if (ms > IEEE80211_ASSOC_TIMEOUT)
660 run_again(local, wk->timeout);
661 return WORK_ACT_NONE;
664 if (status_code != WLAN_STATUS_SUCCESS)
665 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
666 sdata->name, mgmt->sa, status_code);
668 printk(KERN_DEBUG "%s: associated\n", sdata->name);
670 return WORK_ACT_DONE;
673 static enum work_action __must_check
674 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
675 struct ieee80211_mgmt *mgmt, size_t len,
676 struct ieee80211_rx_status *rx_status)
678 struct ieee80211_sub_if_data *sdata = wk->sdata;
679 struct ieee80211_local *local = sdata->local;
682 ASSERT_WORK_MTX(local);
684 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
686 return WORK_ACT_NONE;
688 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
689 return WORK_ACT_DONE;
692 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
695 struct ieee80211_rx_status *rx_status;
696 struct ieee80211_mgmt *mgmt;
697 struct ieee80211_work *wk;
698 enum work_action rma = WORK_ACT_NONE;
701 rx_status = (struct ieee80211_rx_status *) skb->cb;
702 mgmt = (struct ieee80211_mgmt *) skb->data;
703 fc = le16_to_cpu(mgmt->frame_control);
705 mutex_lock(&local->work_mtx);
707 list_for_each_entry(wk, &local->work_list, list) {
708 const u8 *bssid = NULL;
711 case IEEE80211_WORK_DIRECT_PROBE:
712 case IEEE80211_WORK_AUTH:
713 case IEEE80211_WORK_ASSOC:
714 bssid = wk->filter_ta;
721 * Before queuing, we already verified mgmt->sa,
722 * so this is needed just for matching.
724 if (compare_ether_addr(bssid, mgmt->bssid))
727 switch (fc & IEEE80211_FCTL_STYPE) {
728 case IEEE80211_STYPE_PROBE_RESP:
729 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
732 case IEEE80211_STYPE_AUTH:
733 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
735 case IEEE80211_STYPE_ASSOC_RESP:
736 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
739 case IEEE80211_STYPE_REASSOC_RESP:
740 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
747 * We've processed this frame for that work, so it can't
748 * belong to another work struct.
749 * NB: this is also required for correctness for 'rma'!
758 list_del_rcu(&wk->list);
761 WARN(1, "unexpected: %d", rma);
764 mutex_unlock(&local->work_mtx);
766 if (rma != WORK_ACT_DONE)
769 switch (wk->done(wk, skb)) {
770 case WORK_DONE_DESTROY:
773 case WORK_DONE_REQUEUE:
775 wk->started = false; /* restart */
776 mutex_lock(&local->work_mtx);
777 list_add_tail(&wk->list, &local->work_list);
778 mutex_unlock(&local->work_mtx);
785 static void ieee80211_work_timer(unsigned long data)
787 struct ieee80211_local *local = (void *) data;
789 if (local->quiescing)
792 ieee80211_queue_work(&local->hw, &local->work_work);
795 static void ieee80211_work_work(struct work_struct *work)
797 struct ieee80211_local *local =
798 container_of(work, struct ieee80211_local, work_work);
800 struct ieee80211_work *wk, *tmp;
801 LIST_HEAD(free_work);
802 enum work_action rma;
803 bool remain_off_channel = false;
809 * ieee80211_queue_work() should have picked up most cases,
810 * here we'll pick the the rest.
812 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
815 /* first process frames to avoid timing out while a frame is pending */
816 while ((skb = skb_dequeue(&local->work_skb_queue)))
817 ieee80211_work_rx_queued_mgmt(local, skb);
819 ieee80211_recalc_idle(local);
821 mutex_lock(&local->work_mtx);
823 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
824 bool started = wk->started;
826 /* mark work as started if it's on the current off-channel */
827 if (!started && local->tmp_channel &&
828 wk->chan == local->tmp_channel &&
829 wk->chan_type == local->tmp_channel_type) {
831 wk->timeout = jiffies;
834 if (!started && !local->tmp_channel) {
836 * TODO: could optimize this by leaving the
837 * station vifs in awake mode if they
838 * happen to be on the same channel as
839 * the requested channel
841 ieee80211_offchannel_stop_beaconing(local);
842 ieee80211_offchannel_stop_station(local);
844 local->tmp_channel = wk->chan;
845 local->tmp_channel_type = wk->chan_type;
846 ieee80211_hw_config(local, 0);
848 wk->timeout = jiffies;
851 /* don't try to work with items that aren't started */
855 if (time_is_after_jiffies(wk->timeout)) {
857 * This work item isn't supposed to be worked on
858 * right now, but take care to adjust the timer
861 run_again(local, wk->timeout);
871 case IEEE80211_WORK_ABORT:
872 rma = WORK_ACT_TIMEOUT;
874 case IEEE80211_WORK_DIRECT_PROBE:
875 rma = ieee80211_direct_probe(wk);
877 case IEEE80211_WORK_AUTH:
878 rma = ieee80211_authenticate(wk);
880 case IEEE80211_WORK_ASSOC:
881 rma = ieee80211_associate(wk);
883 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
884 rma = ieee80211_remain_on_channel_timeout(wk);
888 wk->started = started;
892 /* might have changed the timeout */
893 run_again(local, wk->timeout);
895 case WORK_ACT_TIMEOUT:
896 list_del_rcu(&wk->list);
898 list_add(&wk->list, &free_work);
901 WARN(1, "unexpected: %d", rma);
905 list_for_each_entry(wk, &local->work_list, list) {
908 if (wk->chan != local->tmp_channel)
910 if (wk->chan_type != local->tmp_channel_type)
912 remain_off_channel = true;
915 if (!remain_off_channel && local->tmp_channel) {
916 local->tmp_channel = NULL;
917 ieee80211_hw_config(local, 0);
918 ieee80211_offchannel_return(local, true);
919 /* give connection some time to breathe */
920 run_again(local, jiffies + HZ/2);
923 mutex_lock(&local->scan_mtx);
925 if (list_empty(&local->work_list) && local->scan_req &&
927 ieee80211_queue_delayed_work(&local->hw,
929 round_jiffies_relative(0));
931 mutex_unlock(&local->scan_mtx);
933 mutex_unlock(&local->work_mtx);
935 ieee80211_recalc_idle(local);
937 list_for_each_entry_safe(wk, tmp, &free_work, list) {
944 void ieee80211_add_work(struct ieee80211_work *wk)
946 struct ieee80211_local *local;
948 if (WARN_ON(!wk->chan))
951 if (WARN_ON(!wk->sdata))
954 if (WARN_ON(!wk->done))
957 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
962 local = wk->sdata->local;
963 mutex_lock(&local->work_mtx);
964 list_add_tail(&wk->list, &local->work_list);
965 mutex_unlock(&local->work_mtx);
967 ieee80211_queue_work(&local->hw, &local->work_work);
970 void ieee80211_work_init(struct ieee80211_local *local)
972 mutex_init(&local->work_mtx);
973 INIT_LIST_HEAD(&local->work_list);
974 setup_timer(&local->work_timer, ieee80211_work_timer,
975 (unsigned long)local);
976 INIT_WORK(&local->work_work, ieee80211_work_work);
977 skb_queue_head_init(&local->work_skb_queue);
980 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
982 struct ieee80211_local *local = sdata->local;
983 struct ieee80211_work *wk;
985 mutex_lock(&local->work_mtx);
986 list_for_each_entry(wk, &local->work_list, list) {
987 if (wk->sdata != sdata)
989 wk->type = IEEE80211_WORK_ABORT;
991 wk->timeout = jiffies;
993 mutex_unlock(&local->work_mtx);
995 /* run cleanups etc. */
996 ieee80211_work_work(&local->work_work);
998 mutex_lock(&local->work_mtx);
999 list_for_each_entry(wk, &local->work_list, list) {
1000 if (wk->sdata != sdata)
1005 mutex_unlock(&local->work_mtx);
1008 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1009 struct sk_buff *skb)
1011 struct ieee80211_local *local = sdata->local;
1012 struct ieee80211_mgmt *mgmt;
1013 struct ieee80211_work *wk;
1017 return RX_DROP_MONITOR;
1019 mgmt = (struct ieee80211_mgmt *) skb->data;
1020 fc = le16_to_cpu(mgmt->frame_control);
1022 list_for_each_entry_rcu(wk, &local->work_list, list) {
1023 if (sdata != wk->sdata)
1025 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1027 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1030 switch (fc & IEEE80211_FCTL_STYPE) {
1031 case IEEE80211_STYPE_AUTH:
1032 case IEEE80211_STYPE_PROBE_RESP:
1033 case IEEE80211_STYPE_ASSOC_RESP:
1034 case IEEE80211_STYPE_REASSOC_RESP:
1035 skb_queue_tail(&local->work_skb_queue, skb);
1036 ieee80211_queue_work(&local->hw, &local->work_work);
1044 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1045 struct sk_buff *skb)
1048 * We are done serving the remain-on-channel command.
1050 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1051 wk->chan, wk->chan_type,
1054 return WORK_DONE_DESTROY;
1057 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1058 struct ieee80211_channel *chan,
1059 enum nl80211_channel_type channel_type,
1060 unsigned int duration, u64 *cookie)
1062 struct ieee80211_work *wk;
1064 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1068 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1070 wk->chan_type = channel_type;
1072 wk->done = ieee80211_remain_done;
1074 wk->remain.duration = duration;
1076 *cookie = (unsigned long) wk;
1078 ieee80211_add_work(wk);
1083 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1086 struct ieee80211_local *local = sdata->local;
1087 struct ieee80211_work *wk, *tmp;
1090 mutex_lock(&local->work_mtx);
1091 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1092 if ((unsigned long) wk == cookie) {
1093 wk->timeout = jiffies;
1098 mutex_unlock(&local->work_mtx);
1103 ieee80211_queue_work(&local->hw, &local->work_work);