blob: ac3549690b8eec8dd37366f29f04d740dc3c34b8 [file] [log] [blame]
Johannes Bergaf6b6372009-12-23 13:15:35 +01001/*
2 * mac80211 work implementation
3 *
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>
10 *
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.
14 */
15
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Johannes Bergaf6b6372009-12-23 13:15:35 +010023#include <net/mac80211.h>
24#include <asm/unaligned.h>
25
26#include "ieee80211_i.h"
27#include "rate.h"
28
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
Johannes Bergaf6b6372009-12-23 13:15:35 +010033
34enum work_action {
Johannes Bergb8d92c92010-05-11 12:42:04 +020035 WORK_ACT_MISMATCH,
Johannes Bergaf6b6372009-12-23 13:15:35 +010036 WORK_ACT_NONE,
37 WORK_ACT_TIMEOUT,
38 WORK_ACT_DONE,
39};
40
41
42/* utils */
43static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
44{
Johannes Berga1699b72010-07-30 16:46:07 +020045 lockdep_assert_held(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +010046}
47
48/*
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
56 * do that.
57 */
58static void run_again(struct ieee80211_local *local,
59 unsigned long timeout)
60{
61 ASSERT_WORK_MTX(local);
62
63 if (!timer_pending(&local->work_timer) ||
64 time_before(timeout, local->work_timer.expires))
65 mod_timer(&local->work_timer, timeout);
66}
67
Johannes Bergaf6b6372009-12-23 13:15:35 +010068void free_work(struct ieee80211_work *wk)
69{
Lai Jiangshana74ce142011-03-18 12:14:15 +080070 kfree_rcu(wk, rcu_head);
Johannes Bergaf6b6372009-12-23 13:15:35 +010071}
72
73static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
74 struct ieee80211_supported_band *sband,
75 u32 *rates)
76{
77 int i, j, count;
78 *rates = 0;
79 count = 0;
80 for (i = 0; i < supp_rates_len; i++) {
81 int rate = (supp_rates[i] & 0x7F) * 5;
82
83 for (j = 0; j < sband->n_bitrates; j++)
84 if (sband->bitrates[j].bitrate == rate) {
85 *rates |= BIT(j);
86 count++;
87 break;
88 }
89 }
90
91 return count;
92}
93
94/* frame sending functions */
95
Johannes Berg77c81442009-12-23 13:15:37 +010096static void ieee80211_add_ht_ie(struct sk_buff *skb, const u8 *ht_info_ie,
97 struct ieee80211_supported_band *sband,
98 struct ieee80211_channel *channel,
99 enum ieee80211_smps_mode smps)
100{
101 struct ieee80211_ht_info *ht_info;
102 u8 *pos;
103 u32 flags = channel->flags;
104 u16 cap = sband->ht_cap.cap;
105 __le16 tmp;
106
107 if (!sband->ht_cap.ht_supported)
108 return;
109
110 if (!ht_info_ie)
111 return;
112
113 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
114 return;
115
116 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
117
118 /* determine capability flags */
119
Johannes Berg77c81442009-12-23 13:15:37 +0100120 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
121 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
122 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
123 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
124 cap &= ~IEEE80211_HT_CAP_SGI_40;
125 }
126 break;
127 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
128 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
129 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
130 cap &= ~IEEE80211_HT_CAP_SGI_40;
131 }
132 break;
133 }
134
135 /* set SM PS mode properly */
136 cap &= ~IEEE80211_HT_CAP_SM_PS;
137 switch (smps) {
138 case IEEE80211_SMPS_AUTOMATIC:
139 case IEEE80211_SMPS_NUM_MODES:
140 WARN_ON(1);
141 case IEEE80211_SMPS_OFF:
142 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
143 IEEE80211_HT_CAP_SM_PS_SHIFT;
144 break;
145 case IEEE80211_SMPS_STATIC:
146 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
147 IEEE80211_HT_CAP_SM_PS_SHIFT;
148 break;
149 case IEEE80211_SMPS_DYNAMIC:
150 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
151 IEEE80211_HT_CAP_SM_PS_SHIFT;
152 break;
153 }
154
155 /* reserve and fill IE */
156
157 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
158 *pos++ = WLAN_EID_HT_CAPABILITY;
159 *pos++ = sizeof(struct ieee80211_ht_cap);
160 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
161
162 /* capability flags */
163 tmp = cpu_to_le16(cap);
164 memcpy(pos, &tmp, sizeof(u16));
165 pos += sizeof(u16);
166
167 /* AMPDU parameters */
168 *pos++ = sband->ht_cap.ampdu_factor |
169 (sband->ht_cap.ampdu_density <<
170 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
171
172 /* MCS set */
173 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
174 pos += sizeof(sband->ht_cap.mcs);
175
176 /* extended capabilities */
177 pos += sizeof(__le16);
178
179 /* BF capabilities */
180 pos += sizeof(__le32);
181
182 /* antenna selection */
183 pos += sizeof(u8);
184}
185
Johannes Bergaf6b6372009-12-23 13:15:35 +0100186static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
187 struct ieee80211_work *wk)
188{
189 struct ieee80211_local *local = sdata->local;
190 struct sk_buff *skb;
191 struct ieee80211_mgmt *mgmt;
Kalle Valoab133152010-01-12 10:42:31 +0200192 u8 *pos, qos_info;
Johannes Berg77c81442009-12-23 13:15:37 +0100193 const u8 *ies;
Johannes Berg8e664fb2009-12-23 13:15:38 +0100194 size_t offset = 0, noffset;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100195 int i, len, count, rates_len, supp_rates_len;
196 u16 capab;
197 struct ieee80211_supported_band *sband;
198 u32 rates = 0;
199
Johannes Berg77c81442009-12-23 13:15:37 +0100200 sband = local->hw.wiphy->bands[wk->chan->band];
201
Stanislaw Gruszka76f27362010-04-28 17:03:15 +0200202 if (wk->assoc.supp_rates_len) {
203 /*
204 * Get all rates supported by the device and the AP as
205 * some APs don't like getting a superset of their rates
206 * in the association request (e.g. D-Link DAP 1353 in
207 * b-only mode)...
208 */
209 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
210 wk->assoc.supp_rates_len,
211 sband, &rates);
212 } else {
213 /*
214 * In case AP not provide any supported rates information
215 * before association, we send information element(s) with
216 * all rates that we support.
217 */
218 rates = ~0;
219 rates_len = sband->n_bitrates;
220 }
Johannes Berg77c81442009-12-23 13:15:37 +0100221
222 skb = alloc_skb(local->hw.extra_tx_headroom +
223 sizeof(*mgmt) + /* bit too much but doesn't matter */
224 2 + wk->assoc.ssid_len + /* SSID */
225 4 + rates_len + /* (extended) rates */
226 4 + /* power capability */
227 2 + 2 * sband->n_channels + /* supported channels */
228 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
229 wk->ie_len + /* extra IEs */
230 9, /* WMM */
231 GFP_KERNEL);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100232 if (!skb) {
233 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
234 "frame\n", sdata->name);
235 return;
236 }
237 skb_reserve(skb, local->hw.extra_tx_headroom);
238
Johannes Bergaf6b6372009-12-23 13:15:35 +0100239 capab = WLAN_CAPABILITY_ESS;
240
241 if (sband->band == IEEE80211_BAND_2GHZ) {
242 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
243 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
244 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
245 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
246 }
247
248 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
249 capab |= WLAN_CAPABILITY_PRIVACY;
250
Johannes Bergaf6b6372009-12-23 13:15:35 +0100251 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
252 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
253 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
254
255 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
256 memset(mgmt, 0, 24);
257 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
258 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
259 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
260
261 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
262 skb_put(skb, 10);
263 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
264 IEEE80211_STYPE_REASSOC_REQ);
265 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
266 mgmt->u.reassoc_req.listen_interval =
267 cpu_to_le16(local->hw.conf.listen_interval);
268 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
269 ETH_ALEN);
270 } else {
271 skb_put(skb, 4);
272 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
273 IEEE80211_STYPE_ASSOC_REQ);
274 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
275 mgmt->u.assoc_req.listen_interval =
276 cpu_to_le16(local->hw.conf.listen_interval);
277 }
278
279 /* SSID */
280 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
281 *pos++ = WLAN_EID_SSID;
282 *pos++ = wk->assoc.ssid_len;
283 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
284
285 /* add all rates which were marked to be used above */
286 supp_rates_len = rates_len;
287 if (supp_rates_len > 8)
288 supp_rates_len = 8;
289
290 len = sband->n_bitrates;
291 pos = skb_put(skb, supp_rates_len + 2);
292 *pos++ = WLAN_EID_SUPP_RATES;
293 *pos++ = supp_rates_len;
294
295 count = 0;
296 for (i = 0; i < sband->n_bitrates; i++) {
297 if (BIT(i) & rates) {
298 int rate = sband->bitrates[i].bitrate;
299 *pos++ = (u8) (rate / 5);
300 if (++count == 8)
301 break;
302 }
303 }
304
305 if (rates_len > count) {
306 pos = skb_put(skb, rates_len - count + 2);
307 *pos++ = WLAN_EID_EXT_SUPP_RATES;
308 *pos++ = rates_len - count;
309
310 for (i++; i < sband->n_bitrates; i++) {
311 if (BIT(i) & rates) {
312 int rate = sband->bitrates[i].bitrate;
313 *pos++ = (u8) (rate / 5);
314 }
315 }
316 }
317
318 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
319 /* 1. power capabilities */
320 pos = skb_put(skb, 4);
321 *pos++ = WLAN_EID_PWR_CAPABILITY;
322 *pos++ = 2;
323 *pos++ = 0; /* min tx power */
Johannes Berg77c81442009-12-23 13:15:37 +0100324 *pos++ = wk->chan->max_power; /* max tx power */
Johannes Bergaf6b6372009-12-23 13:15:35 +0100325
326 /* 2. supported channels */
327 /* TODO: get this in reg domain format */
328 pos = skb_put(skb, 2 * sband->n_channels + 2);
329 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
330 *pos++ = 2 * sband->n_channels;
331 for (i = 0; i < sband->n_channels; i++) {
332 *pos++ = ieee80211_frequency_to_channel(
333 sband->channels[i].center_freq);
334 *pos++ = 1; /* one channel in the subband*/
335 }
336 }
337
Johannes Berg8e664fb2009-12-23 13:15:38 +0100338 /* if present, add any custom IEs that go before HT */
Johannes Bergaf6b6372009-12-23 13:15:35 +0100339 if (wk->ie_len && wk->ie) {
Johannes Berg8e664fb2009-12-23 13:15:38 +0100340 static const u8 before_ht[] = {
341 WLAN_EID_SSID,
342 WLAN_EID_SUPP_RATES,
343 WLAN_EID_EXT_SUPP_RATES,
344 WLAN_EID_PWR_CAPABILITY,
345 WLAN_EID_SUPPORTED_CHANNELS,
346 WLAN_EID_RSN,
347 WLAN_EID_QOS_CAPA,
348 WLAN_EID_RRM_ENABLED_CAPABILITIES,
349 WLAN_EID_MOBILITY_DOMAIN,
350 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
351 };
352 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
353 before_ht, ARRAY_SIZE(before_ht),
354 offset);
355 pos = skb_put(skb, noffset - offset);
356 memcpy(pos, wk->ie + offset, noffset - offset);
357 offset = noffset;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100358 }
359
Johannes Berg77c81442009-12-23 13:15:37 +0100360 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
361 local->hw.queues >= 4)
362 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
363 sband, wk->chan, wk->assoc.smps);
364
Johannes Berg8e664fb2009-12-23 13:15:38 +0100365 /* if present, add any custom non-vendor IEs that go after HT */
366 if (wk->ie_len && wk->ie) {
367 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
368 offset);
369 pos = skb_put(skb, noffset - offset);
370 memcpy(pos, wk->ie + offset, noffset - offset);
371 offset = noffset;
372 }
373
Johannes Bergaf6b6372009-12-23 13:15:35 +0100374 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
Kalle Valoab133152010-01-12 10:42:31 +0200375 if (wk->assoc.uapsd_used) {
Kalle Valo50ae0cf2010-01-12 10:42:39 +0200376 qos_info = local->uapsd_queues;
377 qos_info |= (local->uapsd_max_sp_len <<
Kalle Valoab133152010-01-12 10:42:31 +0200378 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
379 } else {
380 qos_info = 0;
381 }
382
Johannes Bergaf6b6372009-12-23 13:15:35 +0100383 pos = skb_put(skb, 9);
384 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
385 *pos++ = 7; /* len */
386 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
387 *pos++ = 0x50;
388 *pos++ = 0xf2;
389 *pos++ = 2; /* WME */
390 *pos++ = 0; /* WME info */
391 *pos++ = 1; /* WME ver */
Kalle Valoab133152010-01-12 10:42:31 +0200392 *pos++ = qos_info;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100393 }
394
Johannes Berg8e664fb2009-12-23 13:15:38 +0100395 /* add any remaining custom (i.e. vendor specific here) IEs */
396 if (wk->ie_len && wk->ie) {
397 noffset = wk->ie_len;
398 pos = skb_put(skb, noffset - offset);
399 memcpy(pos, wk->ie + offset, noffset - offset);
400 }
401
Johannes Bergaf6b6372009-12-23 13:15:35 +0100402 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
403 ieee80211_tx_skb(sdata, skb);
404}
405
406static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
407 struct ieee80211_work *wk)
408{
409 struct cfg80211_bss *cbss;
410 u16 capa_val = WLAN_CAPABILITY_ESS;
411
412 if (wk->probe_auth.privacy)
413 capa_val |= WLAN_CAPABILITY_PRIVACY;
414
415 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
416 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
417 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
418 capa_val);
419 if (!cbss)
420 return;
421
422 cfg80211_unlink_bss(local->hw.wiphy, cbss);
423 cfg80211_put_bss(cbss);
424}
425
426static enum work_action __must_check
427ieee80211_direct_probe(struct ieee80211_work *wk)
428{
429 struct ieee80211_sub_if_data *sdata = wk->sdata;
430 struct ieee80211_local *local = sdata->local;
431
432 wk->probe_auth.tries++;
433 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100434 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100435 sdata->name, wk->filter_ta);
436
437 /*
438 * Most likely AP is not in the range so remove the
439 * bss struct for that AP.
440 */
441 ieee80211_remove_auth_bss(local, wk);
442
Johannes Bergaf6b6372009-12-23 13:15:35 +0100443 return WORK_ACT_TIMEOUT;
444 }
445
Ben Greear2f886752010-12-07 11:27:01 -0800446 printk(KERN_DEBUG "%s: direct probe to %pM (try %d/%i)\n",
447 sdata->name, wk->filter_ta, wk->probe_auth.tries,
448 IEEE80211_AUTH_MAX_TRIES);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100449
450 /*
451 * Direct probe is sent to broadcast address as some APs
452 * will not answer to direct packet in unassociated state.
453 */
454 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
455 wk->probe_auth.ssid_len, NULL, 0);
456
457 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
458 run_again(local, wk->timeout);
459
460 return WORK_ACT_NONE;
461}
462
463
464static enum work_action __must_check
465ieee80211_authenticate(struct ieee80211_work *wk)
466{
467 struct ieee80211_sub_if_data *sdata = wk->sdata;
468 struct ieee80211_local *local = sdata->local;
469
470 wk->probe_auth.tries++;
471 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100472 printk(KERN_DEBUG "%s: authentication with %pM"
Johannes Bergaf6b6372009-12-23 13:15:35 +0100473 " timed out\n", sdata->name, wk->filter_ta);
474
475 /*
476 * Most likely AP is not in the range so remove the
477 * bss struct for that AP.
478 */
479 ieee80211_remove_auth_bss(local, wk);
480
Johannes Bergaf6b6372009-12-23 13:15:35 +0100481 return WORK_ACT_TIMEOUT;
482 }
483
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100484 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100485 sdata->name, wk->filter_ta, wk->probe_auth.tries);
486
487 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
488 wk->ie_len, wk->filter_ta, NULL, 0, 0);
489 wk->probe_auth.transaction = 2;
490
491 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
492 run_again(local, wk->timeout);
493
494 return WORK_ACT_NONE;
495}
496
497static enum work_action __must_check
498ieee80211_associate(struct ieee80211_work *wk)
499{
500 struct ieee80211_sub_if_data *sdata = wk->sdata;
501 struct ieee80211_local *local = sdata->local;
502
503 wk->assoc.tries++;
504 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100505 printk(KERN_DEBUG "%s: association with %pM"
Johannes Bergaf6b6372009-12-23 13:15:35 +0100506 " timed out\n",
507 sdata->name, wk->filter_ta);
508
509 /*
510 * Most likely AP is not in the range so remove the
511 * bss struct for that AP.
512 */
513 if (wk->assoc.bss)
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100514 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100515
Johannes Bergaf6b6372009-12-23 13:15:35 +0100516 return WORK_ACT_TIMEOUT;
517 }
518
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100519 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100520 sdata->name, wk->filter_ta, wk->assoc.tries);
521 ieee80211_send_assoc(sdata, wk);
522
523 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
524 run_again(local, wk->timeout);
525
526 return WORK_ACT_NONE;
527}
528
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100529static enum work_action __must_check
530ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
531{
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100532 /*
533 * First time we run, do nothing -- the generic code will
534 * have switched to the right channel etc.
535 */
Johannes Berg723bae72010-01-25 13:36:36 +0100536 if (!wk->started) {
Johannes Berge4da8c32009-12-23 13:15:43 +0100537 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
538
Kalle Valo1990ca62009-12-30 14:42:20 +0200539 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
540 wk->chan, wk->chan_type,
541 wk->remain.duration, GFP_KERNEL);
Johannes Berge4da8c32009-12-23 13:15:43 +0100542
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100543 return WORK_ACT_NONE;
544 }
545
Johannes Berge4da8c32009-12-23 13:15:43 +0100546 return WORK_ACT_TIMEOUT;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100547}
548
Johannes Berge5b900d2010-07-29 16:08:55 +0200549static enum work_action __must_check
Johannes Bergf30221e2010-11-25 10:02:30 +0100550ieee80211_offchannel_tx(struct ieee80211_work *wk)
551{
552 if (!wk->started) {
553 wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);
554
555 /*
556 * After this, offchan_tx.frame remains but now is no
557 * longer a valid pointer -- we still need it as the
558 * cookie for canceling this work.
559 */
560 ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);
561
562 return WORK_ACT_NONE;
563 }
564
565 return WORK_ACT_TIMEOUT;
566}
567
568static enum work_action __must_check
Johannes Berge5b900d2010-07-29 16:08:55 +0200569ieee80211_assoc_beacon_wait(struct ieee80211_work *wk)
570{
571 if (wk->started)
572 return WORK_ACT_TIMEOUT;
573
574 /*
575 * Wait up to one beacon interval ...
576 * should this be more if we miss one?
577 */
578 printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
579 wk->sdata->name, wk->filter_ta);
580 wk->timeout = TU_TO_EXP_TIME(wk->assoc.bss->beacon_interval);
581 return WORK_ACT_NONE;
582}
583
Johannes Bergaf6b6372009-12-23 13:15:35 +0100584static void ieee80211_auth_challenge(struct ieee80211_work *wk,
585 struct ieee80211_mgmt *mgmt,
586 size_t len)
587{
588 struct ieee80211_sub_if_data *sdata = wk->sdata;
589 u8 *pos;
590 struct ieee802_11_elems elems;
591
592 pos = mgmt->u.auth.variable;
593 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
594 if (!elems.challenge)
595 return;
596 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
597 elems.challenge - 2, elems.challenge_len + 2,
598 wk->filter_ta, wk->probe_auth.key,
599 wk->probe_auth.key_len, wk->probe_auth.key_idx);
600 wk->probe_auth.transaction = 4;
601}
602
603static enum work_action __must_check
604ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
605 struct ieee80211_mgmt *mgmt, size_t len)
606{
607 u16 auth_alg, auth_transaction, status_code;
608
609 if (wk->type != IEEE80211_WORK_AUTH)
Johannes Bergb8d92c92010-05-11 12:42:04 +0200610 return WORK_ACT_MISMATCH;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100611
612 if (len < 24 + 6)
613 return WORK_ACT_NONE;
614
615 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
616 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
617 status_code = le16_to_cpu(mgmt->u.auth.status_code);
618
619 if (auth_alg != wk->probe_auth.algorithm ||
620 auth_transaction != wk->probe_auth.transaction)
621 return WORK_ACT_NONE;
622
623 if (status_code != WLAN_STATUS_SUCCESS) {
624 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
625 wk->sdata->name, mgmt->sa, status_code);
626 return WORK_ACT_DONE;
627 }
628
629 switch (wk->probe_auth.algorithm) {
630 case WLAN_AUTH_OPEN:
631 case WLAN_AUTH_LEAP:
632 case WLAN_AUTH_FT:
633 break;
634 case WLAN_AUTH_SHARED_KEY:
635 if (wk->probe_auth.transaction != 4) {
636 ieee80211_auth_challenge(wk, mgmt, len);
637 /* need another frame */
638 return WORK_ACT_NONE;
639 }
640 break;
641 default:
642 WARN_ON(1);
643 return WORK_ACT_NONE;
644 }
645
646 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
647 return WORK_ACT_DONE;
648}
649
650static enum work_action __must_check
651ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
652 struct ieee80211_mgmt *mgmt, size_t len,
653 bool reassoc)
654{
655 struct ieee80211_sub_if_data *sdata = wk->sdata;
656 struct ieee80211_local *local = sdata->local;
657 u16 capab_info, status_code, aid;
658 struct ieee802_11_elems elems;
659 u8 *pos;
660
Johannes Bergb8d92c92010-05-11 12:42:04 +0200661 if (wk->type != IEEE80211_WORK_ASSOC)
662 return WORK_ACT_MISMATCH;
663
Johannes Bergaf6b6372009-12-23 13:15:35 +0100664 /*
665 * AssocResp and ReassocResp have identical structure, so process both
666 * of them in this function.
667 */
668
669 if (len < 24 + 6)
670 return WORK_ACT_NONE;
671
672 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
673 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
674 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
675
676 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
677 "status=%d aid=%d)\n",
678 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
679 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
680
681 pos = mgmt->u.assoc_resp.variable;
682 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
683
684 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
685 elems.timeout_int && elems.timeout_int_len == 5 &&
686 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
687 u32 tu, ms;
688 tu = get_unaligned_le32(elems.timeout_int + 1);
689 ms = tu * 1024 / 1000;
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100690 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
Johannes Bergaf6b6372009-12-23 13:15:35 +0100691 "comeback duration %u TU (%u ms)\n",
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100692 sdata->name, mgmt->sa, tu, ms);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100693 wk->timeout = jiffies + msecs_to_jiffies(ms);
694 if (ms > IEEE80211_ASSOC_TIMEOUT)
695 run_again(local, wk->timeout);
696 return WORK_ACT_NONE;
697 }
698
699 if (status_code != WLAN_STATUS_SUCCESS)
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100700 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
701 sdata->name, mgmt->sa, status_code);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100702 else
703 printk(KERN_DEBUG "%s: associated\n", sdata->name);
704
705 return WORK_ACT_DONE;
706}
707
708static enum work_action __must_check
709ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
710 struct ieee80211_mgmt *mgmt, size_t len,
711 struct ieee80211_rx_status *rx_status)
712{
713 struct ieee80211_sub_if_data *sdata = wk->sdata;
714 struct ieee80211_local *local = sdata->local;
715 size_t baselen;
716
717 ASSERT_WORK_MTX(local);
718
Johannes Bergb8d92c92010-05-11 12:42:04 +0200719 if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
720 return WORK_ACT_MISMATCH;
721
722 if (len < 24 + 12)
723 return WORK_ACT_NONE;
724
Johannes Bergaf6b6372009-12-23 13:15:35 +0100725 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
726 if (baselen > len)
727 return WORK_ACT_NONE;
728
729 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
730 return WORK_ACT_DONE;
731}
732
Johannes Berge5b900d2010-07-29 16:08:55 +0200733static enum work_action __must_check
734ieee80211_rx_mgmt_beacon(struct ieee80211_work *wk,
735 struct ieee80211_mgmt *mgmt, size_t len)
736{
737 struct ieee80211_sub_if_data *sdata = wk->sdata;
738 struct ieee80211_local *local = sdata->local;
739
740 ASSERT_WORK_MTX(local);
741
742 if (wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
743 return WORK_ACT_MISMATCH;
744
745 if (len < 24 + 12)
746 return WORK_ACT_NONE;
747
748 printk(KERN_DEBUG "%s: beacon received\n", sdata->name);
749 return WORK_ACT_DONE;
750}
751
Johannes Bergaf6b6372009-12-23 13:15:35 +0100752static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
753 struct sk_buff *skb)
754{
755 struct ieee80211_rx_status *rx_status;
756 struct ieee80211_mgmt *mgmt;
757 struct ieee80211_work *wk;
Christoph Fritz021570e2010-06-16 16:37:34 +0200758 enum work_action rma = WORK_ACT_NONE;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100759 u16 fc;
760
761 rx_status = (struct ieee80211_rx_status *) skb->cb;
762 mgmt = (struct ieee80211_mgmt *) skb->data;
763 fc = le16_to_cpu(mgmt->frame_control);
764
Johannes Berga1699b72010-07-30 16:46:07 +0200765 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100766
767 list_for_each_entry(wk, &local->work_list, list) {
768 const u8 *bssid = NULL;
769
770 switch (wk->type) {
771 case IEEE80211_WORK_DIRECT_PROBE:
772 case IEEE80211_WORK_AUTH:
773 case IEEE80211_WORK_ASSOC:
Johannes Berge5b900d2010-07-29 16:08:55 +0200774 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
Johannes Bergaf6b6372009-12-23 13:15:35 +0100775 bssid = wk->filter_ta;
776 break;
777 default:
778 continue;
779 }
780
781 /*
782 * Before queuing, we already verified mgmt->sa,
783 * so this is needed just for matching.
784 */
785 if (compare_ether_addr(bssid, mgmt->bssid))
786 continue;
787
788 switch (fc & IEEE80211_FCTL_STYPE) {
Johannes Berge5b900d2010-07-29 16:08:55 +0200789 case IEEE80211_STYPE_BEACON:
790 rma = ieee80211_rx_mgmt_beacon(wk, mgmt, skb->len);
791 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100792 case IEEE80211_STYPE_PROBE_RESP:
793 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
794 rx_status);
795 break;
796 case IEEE80211_STYPE_AUTH:
797 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
798 break;
799 case IEEE80211_STYPE_ASSOC_RESP:
800 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
801 skb->len, false);
802 break;
803 case IEEE80211_STYPE_REASSOC_RESP:
804 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
805 skb->len, true);
806 break;
807 default:
808 WARN_ON(1);
Johannes Bergb8d92c92010-05-11 12:42:04 +0200809 rma = WORK_ACT_NONE;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100810 }
Johannes Bergb8d92c92010-05-11 12:42:04 +0200811
812 /*
813 * We've either received an unexpected frame, or we have
814 * multiple work items and need to match the frame to the
815 * right one.
816 */
817 if (rma == WORK_ACT_MISMATCH)
818 continue;
819
Johannes Bergaf6b6372009-12-23 13:15:35 +0100820 /*
821 * We've processed this frame for that work, so it can't
822 * belong to another work struct.
823 * NB: this is also required for correctness for 'rma'!
824 */
825 break;
826 }
827
828 switch (rma) {
Johannes Bergb8d92c92010-05-11 12:42:04 +0200829 case WORK_ACT_MISMATCH:
830 /* ignore this unmatched frame */
831 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100832 case WORK_ACT_NONE:
833 break;
834 case WORK_ACT_DONE:
835 list_del_rcu(&wk->list);
836 break;
837 default:
838 WARN(1, "unexpected: %d", rma);
839 }
840
Johannes Berga1699b72010-07-30 16:46:07 +0200841 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100842
843 if (rma != WORK_ACT_DONE)
844 goto out;
845
846 switch (wk->done(wk, skb)) {
847 case WORK_DONE_DESTROY:
848 free_work(wk);
849 break;
850 case WORK_DONE_REQUEUE:
851 synchronize_rcu();
Johannes Berge4da8c32009-12-23 13:15:43 +0100852 wk->started = false; /* restart */
Johannes Berga1699b72010-07-30 16:46:07 +0200853 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100854 list_add_tail(&wk->list, &local->work_list);
Johannes Berga1699b72010-07-30 16:46:07 +0200855 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100856 }
857
858 out:
859 kfree_skb(skb);
860}
861
Ben Greearda2fd1f2011-02-07 13:44:36 -0800862static bool ieee80211_work_ct_coexists(enum nl80211_channel_type wk_ct,
863 enum nl80211_channel_type oper_ct)
864{
865 switch (wk_ct) {
866 case NL80211_CHAN_NO_HT:
867 return true;
868 case NL80211_CHAN_HT20:
869 if (oper_ct != NL80211_CHAN_NO_HT)
870 return true;
871 return false;
872 case NL80211_CHAN_HT40MINUS:
873 case NL80211_CHAN_HT40PLUS:
874 return (wk_ct == oper_ct);
875 }
876 WARN_ON(1); /* shouldn't get here */
877 return false;
878}
879
880static enum nl80211_channel_type
881ieee80211_calc_ct(enum nl80211_channel_type wk_ct,
882 enum nl80211_channel_type oper_ct)
883{
884 switch (wk_ct) {
885 case NL80211_CHAN_NO_HT:
886 return oper_ct;
887 case NL80211_CHAN_HT20:
888 if (oper_ct != NL80211_CHAN_NO_HT)
889 return oper_ct;
890 return wk_ct;
891 case NL80211_CHAN_HT40MINUS:
892 case NL80211_CHAN_HT40PLUS:
893 return wk_ct;
894 }
895 WARN_ON(1); /* shouldn't get here */
896 return wk_ct;
897}
898
899
Johannes Bergaf6b6372009-12-23 13:15:35 +0100900static void ieee80211_work_timer(unsigned long data)
901{
902 struct ieee80211_local *local = (void *) data;
903
904 if (local->quiescing)
905 return;
906
907 ieee80211_queue_work(&local->hw, &local->work_work);
908}
909
910static void ieee80211_work_work(struct work_struct *work)
911{
912 struct ieee80211_local *local =
913 container_of(work, struct ieee80211_local, work_work);
914 struct sk_buff *skb;
915 struct ieee80211_work *wk, *tmp;
916 LIST_HEAD(free_work);
917 enum work_action rma;
Johannes Berge4da8c32009-12-23 13:15:43 +0100918 bool remain_off_channel = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100919
920 if (local->scanning)
921 return;
922
923 /*
924 * ieee80211_queue_work() should have picked up most cases,
Walter Goldens77c20612010-05-18 04:44:54 -0700925 * here we'll pick the rest.
Johannes Bergaf6b6372009-12-23 13:15:35 +0100926 */
927 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
928 return;
929
930 /* first process frames to avoid timing out while a frame is pending */
931 while ((skb = skb_dequeue(&local->work_skb_queue)))
932 ieee80211_work_rx_queued_mgmt(local, skb);
933
Johannes Berga1699b72010-07-30 16:46:07 +0200934 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100935
Johannes Berg7da7cc12010-08-05 17:02:38 +0200936 ieee80211_recalc_idle(local);
937
Johannes Bergaf6b6372009-12-23 13:15:35 +0100938 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
Johannes Berg723bae72010-01-25 13:36:36 +0100939 bool started = wk->started;
940
Johannes Berge4da8c32009-12-23 13:15:43 +0100941 /* mark work as started if it's on the current off-channel */
Johannes Berg723bae72010-01-25 13:36:36 +0100942 if (!started && local->tmp_channel &&
Johannes Berge4da8c32009-12-23 13:15:43 +0100943 wk->chan == local->tmp_channel &&
944 wk->chan_type == local->tmp_channel_type) {
Johannes Berg723bae72010-01-25 13:36:36 +0100945 started = true;
Johannes Berg81ac3462010-01-06 15:30:58 +0100946 wk->timeout = jiffies;
Johannes Berge4da8c32009-12-23 13:15:43 +0100947 }
948
Johannes Berg723bae72010-01-25 13:36:36 +0100949 if (!started && !local->tmp_channel) {
Ben Greearb23b0252011-02-04 11:54:17 -0800950 bool on_oper_chan;
951 bool tmp_chan_changed = false;
952 bool on_oper_chan2;
Ben Greearda2fd1f2011-02-07 13:44:36 -0800953 enum nl80211_channel_type wk_ct;
Ben Greearb23b0252011-02-04 11:54:17 -0800954 on_oper_chan = ieee80211_cfg_on_oper_channel(local);
Ben Greearda2fd1f2011-02-07 13:44:36 -0800955
956 /* Work with existing channel type if possible. */
957 wk_ct = wk->chan_type;
958 if (wk->chan == local->hw.conf.channel)
959 wk_ct = ieee80211_calc_ct(wk->chan_type,
960 local->hw.conf.channel_type);
961
Ben Greearb23b0252011-02-04 11:54:17 -0800962 if (local->tmp_channel)
963 if ((local->tmp_channel != wk->chan) ||
Ben Greearda2fd1f2011-02-07 13:44:36 -0800964 (local->tmp_channel_type != wk_ct))
Ben Greearb23b0252011-02-04 11:54:17 -0800965 tmp_chan_changed = true;
Johannes Berge4da8c32009-12-23 13:15:43 +0100966
967 local->tmp_channel = wk->chan;
Ben Greearda2fd1f2011-02-07 13:44:36 -0800968 local->tmp_channel_type = wk_ct;
Ben Greearb23b0252011-02-04 11:54:17 -0800969 /*
970 * Leave the station vifs in awake mode if they
971 * happen to be on the same channel as
972 * the requested channel.
973 */
974 on_oper_chan2 = ieee80211_cfg_on_oper_channel(local);
975 if (on_oper_chan != on_oper_chan2) {
976 if (on_oper_chan2) {
977 /* going off oper channel, PS too */
978 ieee80211_offchannel_stop_vifs(local,
979 true);
980 ieee80211_hw_config(local, 0);
981 } else {
982 /* going on channel, but leave PS
983 * off-channel. */
984 ieee80211_hw_config(local, 0);
985 ieee80211_offchannel_return(local,
986 true,
987 false);
988 }
989 } else if (tmp_chan_changed)
990 /* Still off-channel, but on some other
991 * channel, so update hardware.
992 * PS should already be off-channel.
993 */
994 ieee80211_hw_config(local, 0);
995
Johannes Berg723bae72010-01-25 13:36:36 +0100996 started = true;
Johannes Berge4da8c32009-12-23 13:15:43 +0100997 wk->timeout = jiffies;
998 }
999
1000 /* don't try to work with items that aren't started */
Johannes Berg723bae72010-01-25 13:36:36 +01001001 if (!started)
Johannes Berge4da8c32009-12-23 13:15:43 +01001002 continue;
1003
Johannes Bergaf6b6372009-12-23 13:15:35 +01001004 if (time_is_after_jiffies(wk->timeout)) {
1005 /*
1006 * This work item isn't supposed to be worked on
1007 * right now, but take care to adjust the timer
1008 * properly.
1009 */
1010 run_again(local, wk->timeout);
1011 continue;
1012 }
1013
1014 switch (wk->type) {
1015 default:
1016 WARN_ON(1);
1017 /* nothing */
1018 rma = WORK_ACT_NONE;
1019 break;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001020 case IEEE80211_WORK_ABORT:
1021 rma = WORK_ACT_TIMEOUT;
Juuso Oikarinen0e0a2282010-02-26 08:13:41 +02001022 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001023 case IEEE80211_WORK_DIRECT_PROBE:
1024 rma = ieee80211_direct_probe(wk);
1025 break;
1026 case IEEE80211_WORK_AUTH:
1027 rma = ieee80211_authenticate(wk);
1028 break;
1029 case IEEE80211_WORK_ASSOC:
1030 rma = ieee80211_associate(wk);
1031 break;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001032 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
1033 rma = ieee80211_remain_on_channel_timeout(wk);
1034 break;
Johannes Bergf30221e2010-11-25 10:02:30 +01001035 case IEEE80211_WORK_OFFCHANNEL_TX:
1036 rma = ieee80211_offchannel_tx(wk);
1037 break;
Johannes Berge5b900d2010-07-29 16:08:55 +02001038 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
1039 rma = ieee80211_assoc_beacon_wait(wk);
1040 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001041 }
1042
Johannes Berg723bae72010-01-25 13:36:36 +01001043 wk->started = started;
1044
Johannes Bergaf6b6372009-12-23 13:15:35 +01001045 switch (rma) {
1046 case WORK_ACT_NONE:
Johannes Berge4da8c32009-12-23 13:15:43 +01001047 /* might have changed the timeout */
1048 run_again(local, wk->timeout);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001049 break;
1050 case WORK_ACT_TIMEOUT:
1051 list_del_rcu(&wk->list);
1052 synchronize_rcu();
1053 list_add(&wk->list, &free_work);
1054 break;
1055 default:
1056 WARN(1, "unexpected: %d", rma);
1057 }
1058 }
1059
Johannes Berge4da8c32009-12-23 13:15:43 +01001060 list_for_each_entry(wk, &local->work_list, list) {
1061 if (!wk->started)
1062 continue;
1063 if (wk->chan != local->tmp_channel)
1064 continue;
Ben Greearda2fd1f2011-02-07 13:44:36 -08001065 if (ieee80211_work_ct_coexists(wk->chan_type,
1066 local->tmp_channel_type))
Johannes Berge4da8c32009-12-23 13:15:43 +01001067 continue;
1068 remain_off_channel = true;
1069 }
1070
1071 if (!remain_off_channel && local->tmp_channel) {
Ben Greearb23b0252011-02-04 11:54:17 -08001072 bool on_oper_chan = ieee80211_cfg_on_oper_channel(local);
Johannes Berge4da8c32009-12-23 13:15:43 +01001073 local->tmp_channel = NULL;
Ben Greearb23b0252011-02-04 11:54:17 -08001074 /* If tmp_channel wasn't operating channel, then
1075 * we need to go back on-channel.
1076 * NOTE: If we can ever be here while scannning,
1077 * or if the hw_config() channel config logic changes,
1078 * then we may need to do a more thorough check to see if
1079 * we still need to do a hardware config. Currently,
1080 * we cannot be here while scanning, however.
1081 */
1082 if (ieee80211_cfg_on_oper_channel(local) && !on_oper_chan)
1083 ieee80211_hw_config(local, 0);
1084
1085 /* At the least, we need to disable offchannel_ps,
1086 * so just go ahead and run the entire offchannel
1087 * return logic here. We *could* skip enabling
1088 * beaconing if we were already on-oper-channel
1089 * as a future optimization.
1090 */
1091 ieee80211_offchannel_return(local, true, true);
1092
Johannes Berge4da8c32009-12-23 13:15:43 +01001093 /* give connection some time to breathe */
1094 run_again(local, jiffies + HZ/2);
1095 }
1096
Teemu Paasikivi68dd5b72010-04-09 13:07:55 +03001097 if (list_empty(&local->work_list) && local->scan_req &&
1098 !local->scanning)
Johannes Bergaf6b6372009-12-23 13:15:35 +01001099 ieee80211_queue_delayed_work(&local->hw,
1100 &local->scan_work,
1101 round_jiffies_relative(0));
1102
Johannes Berge4da8c32009-12-23 13:15:43 +01001103 ieee80211_recalc_idle(local);
1104
Johannes Berg7da7cc12010-08-05 17:02:38 +02001105 mutex_unlock(&local->mtx);
1106
Johannes Bergaf6b6372009-12-23 13:15:35 +01001107 list_for_each_entry_safe(wk, tmp, &free_work, list) {
1108 wk->done(wk, NULL);
1109 list_del(&wk->list);
1110 kfree(wk);
1111 }
1112}
1113
1114void ieee80211_add_work(struct ieee80211_work *wk)
1115{
1116 struct ieee80211_local *local;
1117
1118 if (WARN_ON(!wk->chan))
1119 return;
1120
1121 if (WARN_ON(!wk->sdata))
1122 return;
1123
1124 if (WARN_ON(!wk->done))
1125 return;
1126
Johannes Berg81ac3462010-01-06 15:30:58 +01001127 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
1128 return;
1129
Johannes Berge4da8c32009-12-23 13:15:43 +01001130 wk->started = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001131
1132 local = wk->sdata->local;
Johannes Berga1699b72010-07-30 16:46:07 +02001133 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001134 list_add_tail(&wk->list, &local->work_list);
Johannes Berga1699b72010-07-30 16:46:07 +02001135 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001136
1137 ieee80211_queue_work(&local->hw, &local->work_work);
1138}
1139
1140void ieee80211_work_init(struct ieee80211_local *local)
1141{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001142 INIT_LIST_HEAD(&local->work_list);
1143 setup_timer(&local->work_timer, ieee80211_work_timer,
1144 (unsigned long)local);
1145 INIT_WORK(&local->work_work, ieee80211_work_work);
1146 skb_queue_head_init(&local->work_skb_queue);
1147}
1148
1149void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
1150{
1151 struct ieee80211_local *local = sdata->local;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001152 struct ieee80211_work *wk;
Herton Ronaldo Krzesinski8808f642010-12-13 11:43:51 -02001153 bool cleanup = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001154
Johannes Berga1699b72010-07-30 16:46:07 +02001155 mutex_lock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001156 list_for_each_entry(wk, &local->work_list, list) {
Johannes Bergaf6b6372009-12-23 13:15:35 +01001157 if (wk->sdata != sdata)
1158 continue;
Herton Ronaldo Krzesinski8808f642010-12-13 11:43:51 -02001159 cleanup = true;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001160 wk->type = IEEE80211_WORK_ABORT;
Johannes Berge4da8c32009-12-23 13:15:43 +01001161 wk->started = true;
1162 wk->timeout = jiffies;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001163 }
Johannes Berga1699b72010-07-30 16:46:07 +02001164 mutex_unlock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001165
1166 /* run cleanups etc. */
Herton Ronaldo Krzesinski8808f642010-12-13 11:43:51 -02001167 if (cleanup)
1168 ieee80211_work_work(&local->work_work);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001169
Johannes Berga1699b72010-07-30 16:46:07 +02001170 mutex_lock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001171 list_for_each_entry(wk, &local->work_list, list) {
1172 if (wk->sdata != sdata)
1173 continue;
1174 WARN_ON(1);
1175 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001176 }
Johannes Berga1699b72010-07-30 16:46:07 +02001177 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001178}
1179
1180ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1181 struct sk_buff *skb)
1182{
1183 struct ieee80211_local *local = sdata->local;
1184 struct ieee80211_mgmt *mgmt;
1185 struct ieee80211_work *wk;
1186 u16 fc;
1187
1188 if (skb->len < 24)
1189 return RX_DROP_MONITOR;
1190
1191 mgmt = (struct ieee80211_mgmt *) skb->data;
1192 fc = le16_to_cpu(mgmt->frame_control);
1193
1194 list_for_each_entry_rcu(wk, &local->work_list, list) {
1195 if (sdata != wk->sdata)
1196 continue;
1197 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1198 continue;
1199 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1200 continue;
1201
1202 switch (fc & IEEE80211_FCTL_STYPE) {
1203 case IEEE80211_STYPE_AUTH:
1204 case IEEE80211_STYPE_PROBE_RESP:
1205 case IEEE80211_STYPE_ASSOC_RESP:
1206 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berge5b900d2010-07-29 16:08:55 +02001207 case IEEE80211_STYPE_BEACON:
Johannes Bergaf6b6372009-12-23 13:15:35 +01001208 skb_queue_tail(&local->work_skb_queue, skb);
1209 ieee80211_queue_work(&local->hw, &local->work_work);
1210 return RX_QUEUED;
1211 }
1212 }
1213
1214 return RX_CONTINUE;
1215}
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001216
Johannes Berge4da8c32009-12-23 13:15:43 +01001217static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1218 struct sk_buff *skb)
1219{
1220 /*
1221 * We are done serving the remain-on-channel command.
1222 */
Kalle Valo1990ca62009-12-30 14:42:20 +02001223 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
Johannes Berge4da8c32009-12-23 13:15:43 +01001224 wk->chan, wk->chan_type,
1225 GFP_KERNEL);
1226
1227 return WORK_DONE_DESTROY;
1228}
1229
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001230int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1231 struct ieee80211_channel *chan,
1232 enum nl80211_channel_type channel_type,
1233 unsigned int duration, u64 *cookie)
1234{
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001235 struct ieee80211_work *wk;
1236
1237 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1238 if (!wk)
1239 return -ENOMEM;
1240
1241 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1242 wk->chan = chan;
Johannes Berge4da8c32009-12-23 13:15:43 +01001243 wk->chan_type = channel_type;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001244 wk->sdata = sdata;
Johannes Berge4da8c32009-12-23 13:15:43 +01001245 wk->done = ieee80211_remain_done;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001246
Johannes Berge4da8c32009-12-23 13:15:43 +01001247 wk->remain.duration = duration;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001248
Kalle Valo1990ca62009-12-30 14:42:20 +02001249 *cookie = (unsigned long) wk;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001250
1251 ieee80211_add_work(wk);
1252
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001253 return 0;
1254}
1255
1256int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1257 u64 cookie)
1258{
1259 struct ieee80211_local *local = sdata->local;
1260 struct ieee80211_work *wk, *tmp;
1261 bool found = false;
1262
Johannes Berga1699b72010-07-30 16:46:07 +02001263 mutex_lock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001264 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
Kalle Valo1990ca62009-12-30 14:42:20 +02001265 if ((unsigned long) wk == cookie) {
Johannes Berge4da8c32009-12-23 13:15:43 +01001266 wk->timeout = jiffies;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001267 found = true;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001268 break;
1269 }
1270 }
Johannes Berga1699b72010-07-30 16:46:07 +02001271 mutex_unlock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001272
1273 if (!found)
1274 return -ENOENT;
1275
Johannes Berge4da8c32009-12-23 13:15:43 +01001276 ieee80211_queue_work(&local->hw, &local->work_work);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001277
1278 return 0;
1279}