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];
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_MISMATCH;
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;
629 if (wk->type != IEEE80211_WORK_ASSOC)
630 return WORK_ACT_MISMATCH;
633 * AssocResp and ReassocResp have identical structure, so process both
634 * of them in this function.
638 return WORK_ACT_NONE;
640 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
641 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
642 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
644 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
645 "status=%d aid=%d)\n",
646 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
647 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
649 pos = mgmt->u.assoc_resp.variable;
650 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
652 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
653 elems.timeout_int && elems.timeout_int_len == 5 &&
654 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
656 tu = get_unaligned_le32(elems.timeout_int + 1);
657 ms = tu * 1024 / 1000;
658 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
659 "comeback duration %u TU (%u ms)\n",
660 sdata->name, mgmt->sa, tu, ms);
661 wk->timeout = jiffies + msecs_to_jiffies(ms);
662 if (ms > IEEE80211_ASSOC_TIMEOUT)
663 run_again(local, wk->timeout);
664 return WORK_ACT_NONE;
667 if (status_code != WLAN_STATUS_SUCCESS)
668 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
669 sdata->name, mgmt->sa, status_code);
671 printk(KERN_DEBUG "%s: associated\n", sdata->name);
673 return WORK_ACT_DONE;
676 static enum work_action __must_check
677 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
678 struct ieee80211_mgmt *mgmt, size_t len,
679 struct ieee80211_rx_status *rx_status)
681 struct ieee80211_sub_if_data *sdata = wk->sdata;
682 struct ieee80211_local *local = sdata->local;
685 ASSERT_WORK_MTX(local);
687 if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
688 return WORK_ACT_MISMATCH;
691 return WORK_ACT_NONE;
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;
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,
758 * We've either received an unexpected frame, or we have
759 * multiple work items and need to match the frame to the
762 if (rma == WORK_ACT_MISMATCH)
766 * We've processed this frame for that work, so it can't
767 * belong to another work struct.
768 * NB: this is also required for correctness for 'rma'!
774 case WORK_ACT_MISMATCH:
775 /* ignore this unmatched frame */
780 list_del_rcu(&wk->list);
783 WARN(1, "unexpected: %d", rma);
786 mutex_unlock(&local->work_mtx);
788 if (rma != WORK_ACT_DONE)
791 switch (wk->done(wk, skb)) {
792 case WORK_DONE_DESTROY:
795 case WORK_DONE_REQUEUE:
797 wk->started = false; /* restart */
798 mutex_lock(&local->work_mtx);
799 list_add_tail(&wk->list, &local->work_list);
800 mutex_unlock(&local->work_mtx);
807 static void ieee80211_work_timer(unsigned long data)
809 struct ieee80211_local *local = (void *) data;
811 if (local->quiescing)
814 ieee80211_queue_work(&local->hw, &local->work_work);
817 static void ieee80211_work_work(struct work_struct *work)
819 struct ieee80211_local *local =
820 container_of(work, struct ieee80211_local, work_work);
822 struct ieee80211_work *wk, *tmp;
823 LIST_HEAD(free_work);
824 enum work_action rma;
825 bool remain_off_channel = false;
831 * ieee80211_queue_work() should have picked up most cases,
832 * here we'll pick the the rest.
834 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
837 /* first process frames to avoid timing out while a frame is pending */
838 while ((skb = skb_dequeue(&local->work_skb_queue)))
839 ieee80211_work_rx_queued_mgmt(local, skb);
841 ieee80211_recalc_idle(local);
843 mutex_lock(&local->work_mtx);
845 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
846 bool started = wk->started;
848 /* mark work as started if it's on the current off-channel */
849 if (!started && local->tmp_channel &&
850 wk->chan == local->tmp_channel &&
851 wk->chan_type == local->tmp_channel_type) {
853 wk->timeout = jiffies;
856 if (!started && !local->tmp_channel) {
858 * TODO: could optimize this by leaving the
859 * station vifs in awake mode if they
860 * happen to be on the same channel as
861 * the requested channel
863 ieee80211_offchannel_stop_beaconing(local);
864 ieee80211_offchannel_stop_station(local);
866 local->tmp_channel = wk->chan;
867 local->tmp_channel_type = wk->chan_type;
868 ieee80211_hw_config(local, 0);
870 wk->timeout = jiffies;
873 /* don't try to work with items that aren't started */
877 if (time_is_after_jiffies(wk->timeout)) {
879 * This work item isn't supposed to be worked on
880 * right now, but take care to adjust the timer
883 run_again(local, wk->timeout);
893 case IEEE80211_WORK_ABORT:
894 rma = WORK_ACT_TIMEOUT;
896 case IEEE80211_WORK_DIRECT_PROBE:
897 rma = ieee80211_direct_probe(wk);
899 case IEEE80211_WORK_AUTH:
900 rma = ieee80211_authenticate(wk);
902 case IEEE80211_WORK_ASSOC:
903 rma = ieee80211_associate(wk);
905 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
906 rma = ieee80211_remain_on_channel_timeout(wk);
910 wk->started = started;
914 /* might have changed the timeout */
915 run_again(local, wk->timeout);
917 case WORK_ACT_TIMEOUT:
918 list_del_rcu(&wk->list);
920 list_add(&wk->list, &free_work);
923 WARN(1, "unexpected: %d", rma);
927 list_for_each_entry(wk, &local->work_list, list) {
930 if (wk->chan != local->tmp_channel)
932 if (wk->chan_type != local->tmp_channel_type)
934 remain_off_channel = true;
937 if (!remain_off_channel && local->tmp_channel) {
938 local->tmp_channel = NULL;
939 ieee80211_hw_config(local, 0);
940 ieee80211_offchannel_return(local, true);
941 /* give connection some time to breathe */
942 run_again(local, jiffies + HZ/2);
945 if (list_empty(&local->work_list) && local->scan_req)
946 ieee80211_queue_delayed_work(&local->hw,
948 round_jiffies_relative(0));
950 mutex_unlock(&local->work_mtx);
952 ieee80211_recalc_idle(local);
954 list_for_each_entry_safe(wk, tmp, &free_work, list) {
961 void ieee80211_add_work(struct ieee80211_work *wk)
963 struct ieee80211_local *local;
965 if (WARN_ON(!wk->chan))
968 if (WARN_ON(!wk->sdata))
971 if (WARN_ON(!wk->done))
974 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
979 local = wk->sdata->local;
980 mutex_lock(&local->work_mtx);
981 list_add_tail(&wk->list, &local->work_list);
982 mutex_unlock(&local->work_mtx);
984 ieee80211_queue_work(&local->hw, &local->work_work);
987 void ieee80211_work_init(struct ieee80211_local *local)
989 mutex_init(&local->work_mtx);
990 INIT_LIST_HEAD(&local->work_list);
991 setup_timer(&local->work_timer, ieee80211_work_timer,
992 (unsigned long)local);
993 INIT_WORK(&local->work_work, ieee80211_work_work);
994 skb_queue_head_init(&local->work_skb_queue);
997 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
999 struct ieee80211_local *local = sdata->local;
1000 struct ieee80211_work *wk;
1002 mutex_lock(&local->work_mtx);
1003 list_for_each_entry(wk, &local->work_list, list) {
1004 if (wk->sdata != sdata)
1006 wk->type = IEEE80211_WORK_ABORT;
1008 wk->timeout = jiffies;
1010 mutex_unlock(&local->work_mtx);
1012 /* run cleanups etc. */
1013 ieee80211_work_work(&local->work_work);
1015 mutex_lock(&local->work_mtx);
1016 list_for_each_entry(wk, &local->work_list, list) {
1017 if (wk->sdata != sdata)
1022 mutex_unlock(&local->work_mtx);
1025 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1026 struct sk_buff *skb)
1028 struct ieee80211_local *local = sdata->local;
1029 struct ieee80211_mgmt *mgmt;
1030 struct ieee80211_work *wk;
1034 return RX_DROP_MONITOR;
1036 mgmt = (struct ieee80211_mgmt *) skb->data;
1037 fc = le16_to_cpu(mgmt->frame_control);
1039 list_for_each_entry_rcu(wk, &local->work_list, list) {
1040 if (sdata != wk->sdata)
1042 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1044 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1047 switch (fc & IEEE80211_FCTL_STYPE) {
1048 case IEEE80211_STYPE_AUTH:
1049 case IEEE80211_STYPE_PROBE_RESP:
1050 case IEEE80211_STYPE_ASSOC_RESP:
1051 case IEEE80211_STYPE_REASSOC_RESP:
1052 skb_queue_tail(&local->work_skb_queue, skb);
1053 ieee80211_queue_work(&local->hw, &local->work_work);
1061 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1062 struct sk_buff *skb)
1065 * We are done serving the remain-on-channel command.
1067 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1068 wk->chan, wk->chan_type,
1071 return WORK_DONE_DESTROY;
1074 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1075 struct ieee80211_channel *chan,
1076 enum nl80211_channel_type channel_type,
1077 unsigned int duration, u64 *cookie)
1079 struct ieee80211_work *wk;
1081 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1085 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1087 wk->chan_type = channel_type;
1089 wk->done = ieee80211_remain_done;
1091 wk->remain.duration = duration;
1093 *cookie = (unsigned long) wk;
1095 ieee80211_add_work(wk);
1100 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1103 struct ieee80211_local *local = sdata->local;
1104 struct ieee80211_work *wk, *tmp;
1107 mutex_lock(&local->work_mtx);
1108 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1109 if ((unsigned long) wk == cookie) {
1110 wk->timeout = jiffies;
1115 mutex_unlock(&local->work_mtx);
1120 ieee80211_queue_work(&local->hw, &local->work_work);