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];
216 * Get all rates supported by the device and the AP as
217 * some APs don't like getting a superset of their rates
218 * in the association request (e.g. D-Link DAP 1353 in
221 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
222 wk->assoc.supp_rates_len,
225 skb = alloc_skb(local->hw.extra_tx_headroom +
226 sizeof(*mgmt) + /* bit too much but doesn't matter */
227 2 + wk->assoc.ssid_len + /* SSID */
228 4 + rates_len + /* (extended) rates */
229 4 + /* power capability */
230 2 + 2 * sband->n_channels + /* supported channels */
231 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
232 wk->ie_len + /* extra IEs */
236 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
237 "frame\n", sdata->name);
240 skb_reserve(skb, local->hw.extra_tx_headroom);
242 capab = WLAN_CAPABILITY_ESS;
244 if (sband->band == IEEE80211_BAND_2GHZ) {
245 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
246 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
247 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
248 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
251 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
252 capab |= WLAN_CAPABILITY_PRIVACY;
254 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
255 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
256 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
258 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
260 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
261 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
262 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
264 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
266 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
267 IEEE80211_STYPE_REASSOC_REQ);
268 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
269 mgmt->u.reassoc_req.listen_interval =
270 cpu_to_le16(local->hw.conf.listen_interval);
271 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
275 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
276 IEEE80211_STYPE_ASSOC_REQ);
277 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
278 mgmt->u.assoc_req.listen_interval =
279 cpu_to_le16(local->hw.conf.listen_interval);
283 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
284 *pos++ = WLAN_EID_SSID;
285 *pos++ = wk->assoc.ssid_len;
286 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
288 /* add all rates which were marked to be used above */
289 supp_rates_len = rates_len;
290 if (supp_rates_len > 8)
293 len = sband->n_bitrates;
294 pos = skb_put(skb, supp_rates_len + 2);
295 *pos++ = WLAN_EID_SUPP_RATES;
296 *pos++ = supp_rates_len;
299 for (i = 0; i < sband->n_bitrates; i++) {
300 if (BIT(i) & rates) {
301 int rate = sband->bitrates[i].bitrate;
302 *pos++ = (u8) (rate / 5);
308 if (rates_len > count) {
309 pos = skb_put(skb, rates_len - count + 2);
310 *pos++ = WLAN_EID_EXT_SUPP_RATES;
311 *pos++ = rates_len - count;
313 for (i++; i < sband->n_bitrates; i++) {
314 if (BIT(i) & rates) {
315 int rate = sband->bitrates[i].bitrate;
316 *pos++ = (u8) (rate / 5);
321 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
322 /* 1. power capabilities */
323 pos = skb_put(skb, 4);
324 *pos++ = WLAN_EID_PWR_CAPABILITY;
326 *pos++ = 0; /* min tx power */
327 *pos++ = wk->chan->max_power; /* max tx power */
329 /* 2. supported channels */
330 /* TODO: get this in reg domain format */
331 pos = skb_put(skb, 2 * sband->n_channels + 2);
332 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
333 *pos++ = 2 * sband->n_channels;
334 for (i = 0; i < sband->n_channels; i++) {
335 *pos++ = ieee80211_frequency_to_channel(
336 sband->channels[i].center_freq);
337 *pos++ = 1; /* one channel in the subband*/
341 /* if present, add any custom IEs that go before HT */
342 if (wk->ie_len && wk->ie) {
343 static const u8 before_ht[] = {
346 WLAN_EID_EXT_SUPP_RATES,
347 WLAN_EID_PWR_CAPABILITY,
348 WLAN_EID_SUPPORTED_CHANNELS,
351 WLAN_EID_RRM_ENABLED_CAPABILITIES,
352 WLAN_EID_MOBILITY_DOMAIN,
353 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
355 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
356 before_ht, ARRAY_SIZE(before_ht),
358 pos = skb_put(skb, noffset - offset);
359 memcpy(pos, wk->ie + offset, noffset - offset);
363 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
364 local->hw.queues >= 4)
365 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
366 sband, wk->chan, wk->assoc.smps);
368 /* if present, add any custom non-vendor IEs that go after HT */
369 if (wk->ie_len && wk->ie) {
370 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
372 pos = skb_put(skb, noffset - offset);
373 memcpy(pos, wk->ie + offset, noffset - offset);
377 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
378 if (wk->assoc.uapsd_used) {
379 qos_info = local->uapsd_queues;
380 qos_info |= (local->uapsd_max_sp_len <<
381 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
386 pos = skb_put(skb, 9);
387 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
388 *pos++ = 7; /* len */
389 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
392 *pos++ = 2; /* WME */
393 *pos++ = 0; /* WME info */
394 *pos++ = 1; /* WME ver */
398 /* add any remaining custom (i.e. vendor specific here) IEs */
399 if (wk->ie_len && wk->ie) {
400 noffset = wk->ie_len;
401 pos = skb_put(skb, noffset - offset);
402 memcpy(pos, wk->ie + offset, noffset - offset);
405 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
406 ieee80211_tx_skb(sdata, skb);
409 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
410 struct ieee80211_work *wk)
412 struct cfg80211_bss *cbss;
413 u16 capa_val = WLAN_CAPABILITY_ESS;
415 if (wk->probe_auth.privacy)
416 capa_val |= WLAN_CAPABILITY_PRIVACY;
418 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
419 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
420 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
425 cfg80211_unlink_bss(local->hw.wiphy, cbss);
426 cfg80211_put_bss(cbss);
429 static enum work_action __must_check
430 ieee80211_direct_probe(struct ieee80211_work *wk)
432 struct ieee80211_sub_if_data *sdata = wk->sdata;
433 struct ieee80211_local *local = sdata->local;
435 wk->probe_auth.tries++;
436 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
437 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
438 sdata->name, wk->filter_ta);
441 * Most likely AP is not in the range so remove the
442 * bss struct for that AP.
444 ieee80211_remove_auth_bss(local, wk);
446 return WORK_ACT_TIMEOUT;
449 printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
450 sdata->name, wk->filter_ta, wk->probe_auth.tries);
453 * Direct probe is sent to broadcast address as some APs
454 * will not answer to direct packet in unassociated state.
456 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
457 wk->probe_auth.ssid_len, NULL, 0);
459 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
460 run_again(local, wk->timeout);
462 return WORK_ACT_NONE;
466 static enum work_action __must_check
467 ieee80211_authenticate(struct ieee80211_work *wk)
469 struct ieee80211_sub_if_data *sdata = wk->sdata;
470 struct ieee80211_local *local = sdata->local;
472 wk->probe_auth.tries++;
473 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
474 printk(KERN_DEBUG "%s: authentication with %pM"
475 " timed out\n", sdata->name, wk->filter_ta);
478 * Most likely AP is not in the range so remove the
479 * bss struct for that AP.
481 ieee80211_remove_auth_bss(local, wk);
483 return WORK_ACT_TIMEOUT;
486 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
487 sdata->name, wk->filter_ta, wk->probe_auth.tries);
489 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
490 wk->ie_len, wk->filter_ta, NULL, 0, 0);
491 wk->probe_auth.transaction = 2;
493 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
494 run_again(local, wk->timeout);
496 return WORK_ACT_NONE;
499 static enum work_action __must_check
500 ieee80211_associate(struct ieee80211_work *wk)
502 struct ieee80211_sub_if_data *sdata = wk->sdata;
503 struct ieee80211_local *local = sdata->local;
506 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
507 printk(KERN_DEBUG "%s: association with %pM"
509 sdata->name, wk->filter_ta);
512 * Most likely AP is not in the range so remove the
513 * bss struct for that AP.
516 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
518 return WORK_ACT_TIMEOUT;
521 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
522 sdata->name, wk->filter_ta, wk->assoc.tries);
523 ieee80211_send_assoc(sdata, wk);
525 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
526 run_again(local, wk->timeout);
528 return WORK_ACT_NONE;
531 static enum work_action __must_check
532 ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
535 * First time we run, do nothing -- the generic code will
536 * have switched to the right channel etc.
539 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
541 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
542 wk->chan, wk->chan_type,
543 wk->remain.duration, GFP_KERNEL);
545 return WORK_ACT_NONE;
548 return WORK_ACT_TIMEOUT;
551 static void ieee80211_auth_challenge(struct ieee80211_work *wk,
552 struct ieee80211_mgmt *mgmt,
555 struct ieee80211_sub_if_data *sdata = wk->sdata;
557 struct ieee802_11_elems elems;
559 pos = mgmt->u.auth.variable;
560 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
561 if (!elems.challenge)
563 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
564 elems.challenge - 2, elems.challenge_len + 2,
565 wk->filter_ta, wk->probe_auth.key,
566 wk->probe_auth.key_len, wk->probe_auth.key_idx);
567 wk->probe_auth.transaction = 4;
570 static enum work_action __must_check
571 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
572 struct ieee80211_mgmt *mgmt, size_t len)
574 u16 auth_alg, auth_transaction, status_code;
576 if (wk->type != IEEE80211_WORK_AUTH)
577 return WORK_ACT_NONE;
580 return WORK_ACT_NONE;
582 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
583 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
584 status_code = le16_to_cpu(mgmt->u.auth.status_code);
586 if (auth_alg != wk->probe_auth.algorithm ||
587 auth_transaction != wk->probe_auth.transaction)
588 return WORK_ACT_NONE;
590 if (status_code != WLAN_STATUS_SUCCESS) {
591 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
592 wk->sdata->name, mgmt->sa, status_code);
593 return WORK_ACT_DONE;
596 switch (wk->probe_auth.algorithm) {
601 case WLAN_AUTH_SHARED_KEY:
602 if (wk->probe_auth.transaction != 4) {
603 ieee80211_auth_challenge(wk, mgmt, len);
604 /* need another frame */
605 return WORK_ACT_NONE;
610 return WORK_ACT_NONE;
613 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
614 return WORK_ACT_DONE;
617 static enum work_action __must_check
618 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
619 struct ieee80211_mgmt *mgmt, size_t len,
622 struct ieee80211_sub_if_data *sdata = wk->sdata;
623 struct ieee80211_local *local = sdata->local;
624 u16 capab_info, status_code, aid;
625 struct ieee802_11_elems elems;
629 * AssocResp and ReassocResp have identical structure, so process both
630 * of them in this function.
634 return WORK_ACT_NONE;
636 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
637 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
638 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
640 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
641 "status=%d aid=%d)\n",
642 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
643 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
645 pos = mgmt->u.assoc_resp.variable;
646 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
648 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
649 elems.timeout_int && elems.timeout_int_len == 5 &&
650 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
652 tu = get_unaligned_le32(elems.timeout_int + 1);
653 ms = tu * 1024 / 1000;
654 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
655 "comeback duration %u TU (%u ms)\n",
656 sdata->name, mgmt->sa, tu, ms);
657 wk->timeout = jiffies + msecs_to_jiffies(ms);
658 if (ms > IEEE80211_ASSOC_TIMEOUT)
659 run_again(local, wk->timeout);
660 return WORK_ACT_NONE;
663 if (status_code != WLAN_STATUS_SUCCESS)
664 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
665 sdata->name, mgmt->sa, status_code);
667 printk(KERN_DEBUG "%s: associated\n", sdata->name);
669 return WORK_ACT_DONE;
672 static enum work_action __must_check
673 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
674 struct ieee80211_mgmt *mgmt, size_t len,
675 struct ieee80211_rx_status *rx_status)
677 struct ieee80211_sub_if_data *sdata = wk->sdata;
678 struct ieee80211_local *local = sdata->local;
681 ASSERT_WORK_MTX(local);
683 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
685 return WORK_ACT_NONE;
687 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
688 return WORK_ACT_DONE;
691 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
694 struct ieee80211_rx_status *rx_status;
695 struct ieee80211_mgmt *mgmt;
696 struct ieee80211_work *wk;
697 enum work_action rma = WORK_ACT_NONE;
700 rx_status = (struct ieee80211_rx_status *) skb->cb;
701 mgmt = (struct ieee80211_mgmt *) skb->data;
702 fc = le16_to_cpu(mgmt->frame_control);
704 mutex_lock(&local->work_mtx);
706 list_for_each_entry(wk, &local->work_list, list) {
707 const u8 *bssid = NULL;
710 case IEEE80211_WORK_DIRECT_PROBE:
711 case IEEE80211_WORK_AUTH:
712 case IEEE80211_WORK_ASSOC:
713 bssid = wk->filter_ta;
720 * Before queuing, we already verified mgmt->sa,
721 * so this is needed just for matching.
723 if (compare_ether_addr(bssid, mgmt->bssid))
726 switch (fc & IEEE80211_FCTL_STYPE) {
727 case IEEE80211_STYPE_PROBE_RESP:
728 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
731 case IEEE80211_STYPE_AUTH:
732 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
734 case IEEE80211_STYPE_ASSOC_RESP:
735 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
738 case IEEE80211_STYPE_REASSOC_RESP:
739 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
746 * We've processed this frame for that work, so it can't
747 * belong to another work struct.
748 * NB: this is also required for correctness for 'rma'!
757 list_del_rcu(&wk->list);
760 WARN(1, "unexpected: %d", rma);
763 mutex_unlock(&local->work_mtx);
765 if (rma != WORK_ACT_DONE)
768 switch (wk->done(wk, skb)) {
769 case WORK_DONE_DESTROY:
772 case WORK_DONE_REQUEUE:
774 wk->started = false; /* restart */
775 mutex_lock(&local->work_mtx);
776 list_add_tail(&wk->list, &local->work_list);
777 mutex_unlock(&local->work_mtx);
784 static void ieee80211_work_timer(unsigned long data)
786 struct ieee80211_local *local = (void *) data;
788 if (local->quiescing)
791 ieee80211_queue_work(&local->hw, &local->work_work);
794 static void ieee80211_work_work(struct work_struct *work)
796 struct ieee80211_local *local =
797 container_of(work, struct ieee80211_local, work_work);
799 struct ieee80211_work *wk, *tmp;
800 LIST_HEAD(free_work);
801 enum work_action rma;
802 bool remain_off_channel = false;
808 * ieee80211_queue_work() should have picked up most cases,
809 * here we'll pick the the rest.
811 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
814 /* first process frames to avoid timing out while a frame is pending */
815 while ((skb = skb_dequeue(&local->work_skb_queue)))
816 ieee80211_work_rx_queued_mgmt(local, skb);
818 ieee80211_recalc_idle(local);
820 mutex_lock(&local->work_mtx);
822 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
823 bool started = wk->started;
825 /* mark work as started if it's on the current off-channel */
826 if (!started && local->tmp_channel &&
827 wk->chan == local->tmp_channel &&
828 wk->chan_type == local->tmp_channel_type) {
830 wk->timeout = jiffies;
833 if (!started && !local->tmp_channel) {
835 * TODO: could optimize this by leaving the
836 * station vifs in awake mode if they
837 * happen to be on the same channel as
838 * the requested channel
840 ieee80211_offchannel_stop_beaconing(local);
841 ieee80211_offchannel_stop_station(local);
843 local->tmp_channel = wk->chan;
844 local->tmp_channel_type = wk->chan_type;
845 ieee80211_hw_config(local, 0);
847 wk->timeout = jiffies;
850 /* don't try to work with items that aren't started */
854 if (time_is_after_jiffies(wk->timeout)) {
856 * This work item isn't supposed to be worked on
857 * right now, but take care to adjust the timer
860 run_again(local, wk->timeout);
870 case IEEE80211_WORK_ABORT:
871 rma = WORK_ACT_TIMEOUT;
873 case IEEE80211_WORK_DIRECT_PROBE:
874 rma = ieee80211_direct_probe(wk);
876 case IEEE80211_WORK_AUTH:
877 rma = ieee80211_authenticate(wk);
879 case IEEE80211_WORK_ASSOC:
880 rma = ieee80211_associate(wk);
882 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
883 rma = ieee80211_remain_on_channel_timeout(wk);
887 wk->started = started;
891 /* might have changed the timeout */
892 run_again(local, wk->timeout);
894 case WORK_ACT_TIMEOUT:
895 list_del_rcu(&wk->list);
897 list_add(&wk->list, &free_work);
900 WARN(1, "unexpected: %d", rma);
904 list_for_each_entry(wk, &local->work_list, list) {
907 if (wk->chan != local->tmp_channel)
909 if (wk->chan_type != local->tmp_channel_type)
911 remain_off_channel = true;
914 if (!remain_off_channel && local->tmp_channel) {
915 local->tmp_channel = NULL;
916 ieee80211_hw_config(local, 0);
917 ieee80211_offchannel_return(local, true);
918 /* give connection some time to breathe */
919 run_again(local, jiffies + HZ/2);
922 mutex_lock(&local->scan_mtx);
924 if (list_empty(&local->work_list) && local->scan_req &&
926 ieee80211_queue_delayed_work(&local->hw,
928 round_jiffies_relative(0));
930 mutex_unlock(&local->scan_mtx);
932 mutex_unlock(&local->work_mtx);
934 ieee80211_recalc_idle(local);
936 list_for_each_entry_safe(wk, tmp, &free_work, list) {
943 void ieee80211_add_work(struct ieee80211_work *wk)
945 struct ieee80211_local *local;
947 if (WARN_ON(!wk->chan))
950 if (WARN_ON(!wk->sdata))
953 if (WARN_ON(!wk->done))
956 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
961 local = wk->sdata->local;
962 mutex_lock(&local->work_mtx);
963 list_add_tail(&wk->list, &local->work_list);
964 mutex_unlock(&local->work_mtx);
966 ieee80211_queue_work(&local->hw, &local->work_work);
969 void ieee80211_work_init(struct ieee80211_local *local)
971 mutex_init(&local->work_mtx);
972 INIT_LIST_HEAD(&local->work_list);
973 setup_timer(&local->work_timer, ieee80211_work_timer,
974 (unsigned long)local);
975 INIT_WORK(&local->work_work, ieee80211_work_work);
976 skb_queue_head_init(&local->work_skb_queue);
979 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
981 struct ieee80211_local *local = sdata->local;
982 struct ieee80211_work *wk;
984 mutex_lock(&local->work_mtx);
985 list_for_each_entry(wk, &local->work_list, list) {
986 if (wk->sdata != sdata)
988 wk->type = IEEE80211_WORK_ABORT;
990 wk->timeout = jiffies;
992 mutex_unlock(&local->work_mtx);
994 /* run cleanups etc. */
995 ieee80211_work_work(&local->work_work);
997 mutex_lock(&local->work_mtx);
998 list_for_each_entry(wk, &local->work_list, list) {
999 if (wk->sdata != sdata)
1004 mutex_unlock(&local->work_mtx);
1007 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1008 struct sk_buff *skb)
1010 struct ieee80211_local *local = sdata->local;
1011 struct ieee80211_mgmt *mgmt;
1012 struct ieee80211_work *wk;
1016 return RX_DROP_MONITOR;
1018 mgmt = (struct ieee80211_mgmt *) skb->data;
1019 fc = le16_to_cpu(mgmt->frame_control);
1021 list_for_each_entry_rcu(wk, &local->work_list, list) {
1022 if (sdata != wk->sdata)
1024 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1026 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1029 switch (fc & IEEE80211_FCTL_STYPE) {
1030 case IEEE80211_STYPE_AUTH:
1031 case IEEE80211_STYPE_PROBE_RESP:
1032 case IEEE80211_STYPE_ASSOC_RESP:
1033 case IEEE80211_STYPE_REASSOC_RESP:
1034 skb_queue_tail(&local->work_skb_queue, skb);
1035 ieee80211_queue_work(&local->hw, &local->work_work);
1043 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1044 struct sk_buff *skb)
1047 * We are done serving the remain-on-channel command.
1049 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1050 wk->chan, wk->chan_type,
1053 return WORK_DONE_DESTROY;
1056 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1057 struct ieee80211_channel *chan,
1058 enum nl80211_channel_type channel_type,
1059 unsigned int duration, u64 *cookie)
1061 struct ieee80211_work *wk;
1063 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1067 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1069 wk->chan_type = channel_type;
1071 wk->done = ieee80211_remain_done;
1073 wk->remain.duration = duration;
1075 *cookie = (unsigned long) wk;
1077 ieee80211_add_work(wk);
1082 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1085 struct ieee80211_local *local = sdata->local;
1086 struct ieee80211_work *wk, *tmp;
1089 mutex_lock(&local->work_mtx);
1090 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1091 if ((unsigned long) wk == cookie) {
1092 wk->timeout = jiffies;
1097 mutex_unlock(&local->work_mtx);
1102 ieee80211_queue_work(&local->hw, &local->work_work);