blob: 204f0a4db96911c41df34a209fa7ead03f4aa511 [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
33#define IEEE80211_MAX_PROBE_TRIES 5
34
35enum work_action {
Johannes Bergb8d92c92010-05-11 12:42:04 +020036 WORK_ACT_MISMATCH,
Johannes Bergaf6b6372009-12-23 13:15:35 +010037 WORK_ACT_NONE,
38 WORK_ACT_TIMEOUT,
39 WORK_ACT_DONE,
40};
41
42
43/* utils */
44static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
45{
Johannes Berga1699b72010-07-30 16:46:07 +020046 lockdep_assert_held(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +010047}
48
49/*
50 * We can have multiple work items (and connection probing)
51 * scheduling this timer, but we need to take care to only
52 * reschedule it when it should fire _earlier_ than it was
53 * asked for before, or if it's not pending right now. This
54 * function ensures that. Note that it then is required to
55 * run this function for all timeouts after the first one
56 * has happened -- the work that runs from this timer will
57 * do that.
58 */
59static void run_again(struct ieee80211_local *local,
60 unsigned long timeout)
61{
62 ASSERT_WORK_MTX(local);
63
64 if (!timer_pending(&local->work_timer) ||
65 time_before(timeout, local->work_timer.expires))
66 mod_timer(&local->work_timer, timeout);
67}
68
69static void work_free_rcu(struct rcu_head *head)
70{
71 struct ieee80211_work *wk =
72 container_of(head, struct ieee80211_work, rcu_head);
73
74 kfree(wk);
75}
76
77void free_work(struct ieee80211_work *wk)
78{
79 call_rcu(&wk->rcu_head, work_free_rcu);
80}
81
82static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
83 struct ieee80211_supported_band *sband,
84 u32 *rates)
85{
86 int i, j, count;
87 *rates = 0;
88 count = 0;
89 for (i = 0; i < supp_rates_len; i++) {
90 int rate = (supp_rates[i] & 0x7F) * 5;
91
92 for (j = 0; j < sband->n_bitrates; j++)
93 if (sband->bitrates[j].bitrate == rate) {
94 *rates |= BIT(j);
95 count++;
96 break;
97 }
98 }
99
100 return count;
101}
102
103/* frame sending functions */
104
Johannes Berg77c81442009-12-23 13:15:37 +0100105static void ieee80211_add_ht_ie(struct sk_buff *skb, const u8 *ht_info_ie,
106 struct ieee80211_supported_band *sband,
107 struct ieee80211_channel *channel,
108 enum ieee80211_smps_mode smps)
109{
110 struct ieee80211_ht_info *ht_info;
111 u8 *pos;
112 u32 flags = channel->flags;
113 u16 cap = sband->ht_cap.cap;
114 __le16 tmp;
115
116 if (!sband->ht_cap.ht_supported)
117 return;
118
119 if (!ht_info_ie)
120 return;
121
122 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
123 return;
124
125 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
126
127 /* determine capability flags */
128
Johannes Berg77c81442009-12-23 13:15:37 +0100129 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
130 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
131 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
132 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
133 cap &= ~IEEE80211_HT_CAP_SGI_40;
134 }
135 break;
136 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
137 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
138 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
139 cap &= ~IEEE80211_HT_CAP_SGI_40;
140 }
141 break;
142 }
143
144 /* set SM PS mode properly */
145 cap &= ~IEEE80211_HT_CAP_SM_PS;
146 switch (smps) {
147 case IEEE80211_SMPS_AUTOMATIC:
148 case IEEE80211_SMPS_NUM_MODES:
149 WARN_ON(1);
150 case IEEE80211_SMPS_OFF:
151 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
152 IEEE80211_HT_CAP_SM_PS_SHIFT;
153 break;
154 case IEEE80211_SMPS_STATIC:
155 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
156 IEEE80211_HT_CAP_SM_PS_SHIFT;
157 break;
158 case IEEE80211_SMPS_DYNAMIC:
159 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
160 IEEE80211_HT_CAP_SM_PS_SHIFT;
161 break;
162 }
163
164 /* reserve and fill IE */
165
166 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
167 *pos++ = WLAN_EID_HT_CAPABILITY;
168 *pos++ = sizeof(struct ieee80211_ht_cap);
169 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
170
171 /* capability flags */
172 tmp = cpu_to_le16(cap);
173 memcpy(pos, &tmp, sizeof(u16));
174 pos += sizeof(u16);
175
176 /* AMPDU parameters */
177 *pos++ = sband->ht_cap.ampdu_factor |
178 (sband->ht_cap.ampdu_density <<
179 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
180
181 /* MCS set */
182 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
183 pos += sizeof(sband->ht_cap.mcs);
184
185 /* extended capabilities */
186 pos += sizeof(__le16);
187
188 /* BF capabilities */
189 pos += sizeof(__le32);
190
191 /* antenna selection */
192 pos += sizeof(u8);
193}
194
Johannes Bergaf6b6372009-12-23 13:15:35 +0100195static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
196 struct ieee80211_work *wk)
197{
198 struct ieee80211_local *local = sdata->local;
199 struct sk_buff *skb;
200 struct ieee80211_mgmt *mgmt;
Kalle Valoab133152010-01-12 10:42:31 +0200201 u8 *pos, qos_info;
Johannes Berg77c81442009-12-23 13:15:37 +0100202 const u8 *ies;
Johannes Berg8e664fb2009-12-23 13:15:38 +0100203 size_t offset = 0, noffset;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100204 int i, len, count, rates_len, supp_rates_len;
205 u16 capab;
206 struct ieee80211_supported_band *sband;
207 u32 rates = 0;
208
Johannes Berg77c81442009-12-23 13:15:37 +0100209 sband = local->hw.wiphy->bands[wk->chan->band];
210
Stanislaw Gruszka76f27362010-04-28 17:03:15 +0200211 if (wk->assoc.supp_rates_len) {
212 /*
213 * Get all rates supported by the device and the AP as
214 * some APs don't like getting a superset of their rates
215 * in the association request (e.g. D-Link DAP 1353 in
216 * b-only mode)...
217 */
218 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
219 wk->assoc.supp_rates_len,
220 sband, &rates);
221 } else {
222 /*
223 * In case AP not provide any supported rates information
224 * before association, we send information element(s) with
225 * all rates that we support.
226 */
227 rates = ~0;
228 rates_len = sband->n_bitrates;
229 }
Johannes Berg77c81442009-12-23 13:15:37 +0100230
231 skb = alloc_skb(local->hw.extra_tx_headroom +
232 sizeof(*mgmt) + /* bit too much but doesn't matter */
233 2 + wk->assoc.ssid_len + /* SSID */
234 4 + rates_len + /* (extended) rates */
235 4 + /* power capability */
236 2 + 2 * sband->n_channels + /* supported channels */
237 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
238 wk->ie_len + /* extra IEs */
239 9, /* WMM */
240 GFP_KERNEL);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100241 if (!skb) {
242 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
243 "frame\n", sdata->name);
244 return;
245 }
246 skb_reserve(skb, local->hw.extra_tx_headroom);
247
Johannes Bergaf6b6372009-12-23 13:15:35 +0100248 capab = WLAN_CAPABILITY_ESS;
249
250 if (sband->band == IEEE80211_BAND_2GHZ) {
251 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
252 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
253 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
254 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
255 }
256
257 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
258 capab |= WLAN_CAPABILITY_PRIVACY;
259
Johannes Bergaf6b6372009-12-23 13:15:35 +0100260 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
261 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
262 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
263
264 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
265 memset(mgmt, 0, 24);
266 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
267 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
268 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
269
270 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
271 skb_put(skb, 10);
272 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
273 IEEE80211_STYPE_REASSOC_REQ);
274 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
275 mgmt->u.reassoc_req.listen_interval =
276 cpu_to_le16(local->hw.conf.listen_interval);
277 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
278 ETH_ALEN);
279 } else {
280 skb_put(skb, 4);
281 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
282 IEEE80211_STYPE_ASSOC_REQ);
283 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
284 mgmt->u.assoc_req.listen_interval =
285 cpu_to_le16(local->hw.conf.listen_interval);
286 }
287
288 /* SSID */
289 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
290 *pos++ = WLAN_EID_SSID;
291 *pos++ = wk->assoc.ssid_len;
292 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
293
294 /* add all rates which were marked to be used above */
295 supp_rates_len = rates_len;
296 if (supp_rates_len > 8)
297 supp_rates_len = 8;
298
299 len = sband->n_bitrates;
300 pos = skb_put(skb, supp_rates_len + 2);
301 *pos++ = WLAN_EID_SUPP_RATES;
302 *pos++ = supp_rates_len;
303
304 count = 0;
305 for (i = 0; i < sband->n_bitrates; i++) {
306 if (BIT(i) & rates) {
307 int rate = sband->bitrates[i].bitrate;
308 *pos++ = (u8) (rate / 5);
309 if (++count == 8)
310 break;
311 }
312 }
313
314 if (rates_len > count) {
315 pos = skb_put(skb, rates_len - count + 2);
316 *pos++ = WLAN_EID_EXT_SUPP_RATES;
317 *pos++ = rates_len - count;
318
319 for (i++; i < sband->n_bitrates; i++) {
320 if (BIT(i) & rates) {
321 int rate = sband->bitrates[i].bitrate;
322 *pos++ = (u8) (rate / 5);
323 }
324 }
325 }
326
327 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
328 /* 1. power capabilities */
329 pos = skb_put(skb, 4);
330 *pos++ = WLAN_EID_PWR_CAPABILITY;
331 *pos++ = 2;
332 *pos++ = 0; /* min tx power */
Johannes Berg77c81442009-12-23 13:15:37 +0100333 *pos++ = wk->chan->max_power; /* max tx power */
Johannes Bergaf6b6372009-12-23 13:15:35 +0100334
335 /* 2. supported channels */
336 /* TODO: get this in reg domain format */
337 pos = skb_put(skb, 2 * sband->n_channels + 2);
338 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
339 *pos++ = 2 * sband->n_channels;
340 for (i = 0; i < sband->n_channels; i++) {
341 *pos++ = ieee80211_frequency_to_channel(
342 sband->channels[i].center_freq);
343 *pos++ = 1; /* one channel in the subband*/
344 }
345 }
346
Johannes Berg8e664fb2009-12-23 13:15:38 +0100347 /* if present, add any custom IEs that go before HT */
Johannes Bergaf6b6372009-12-23 13:15:35 +0100348 if (wk->ie_len && wk->ie) {
Johannes Berg8e664fb2009-12-23 13:15:38 +0100349 static const u8 before_ht[] = {
350 WLAN_EID_SSID,
351 WLAN_EID_SUPP_RATES,
352 WLAN_EID_EXT_SUPP_RATES,
353 WLAN_EID_PWR_CAPABILITY,
354 WLAN_EID_SUPPORTED_CHANNELS,
355 WLAN_EID_RSN,
356 WLAN_EID_QOS_CAPA,
357 WLAN_EID_RRM_ENABLED_CAPABILITIES,
358 WLAN_EID_MOBILITY_DOMAIN,
359 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
360 };
361 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
362 before_ht, ARRAY_SIZE(before_ht),
363 offset);
364 pos = skb_put(skb, noffset - offset);
365 memcpy(pos, wk->ie + offset, noffset - offset);
366 offset = noffset;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100367 }
368
Johannes Berg77c81442009-12-23 13:15:37 +0100369 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
370 local->hw.queues >= 4)
371 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
372 sband, wk->chan, wk->assoc.smps);
373
Johannes Berg8e664fb2009-12-23 13:15:38 +0100374 /* if present, add any custom non-vendor IEs that go after HT */
375 if (wk->ie_len && wk->ie) {
376 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
377 offset);
378 pos = skb_put(skb, noffset - offset);
379 memcpy(pos, wk->ie + offset, noffset - offset);
380 offset = noffset;
381 }
382
Johannes Bergaf6b6372009-12-23 13:15:35 +0100383 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
Kalle Valoab133152010-01-12 10:42:31 +0200384 if (wk->assoc.uapsd_used) {
Kalle Valo50ae0cf2010-01-12 10:42:39 +0200385 qos_info = local->uapsd_queues;
386 qos_info |= (local->uapsd_max_sp_len <<
Kalle Valoab133152010-01-12 10:42:31 +0200387 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
388 } else {
389 qos_info = 0;
390 }
391
Johannes Bergaf6b6372009-12-23 13:15:35 +0100392 pos = skb_put(skb, 9);
393 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
394 *pos++ = 7; /* len */
395 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
396 *pos++ = 0x50;
397 *pos++ = 0xf2;
398 *pos++ = 2; /* WME */
399 *pos++ = 0; /* WME info */
400 *pos++ = 1; /* WME ver */
Kalle Valoab133152010-01-12 10:42:31 +0200401 *pos++ = qos_info;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100402 }
403
Johannes Berg8e664fb2009-12-23 13:15:38 +0100404 /* add any remaining custom (i.e. vendor specific here) IEs */
405 if (wk->ie_len && wk->ie) {
406 noffset = wk->ie_len;
407 pos = skb_put(skb, noffset - offset);
408 memcpy(pos, wk->ie + offset, noffset - offset);
409 }
410
Johannes Bergaf6b6372009-12-23 13:15:35 +0100411 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
412 ieee80211_tx_skb(sdata, skb);
413}
414
415static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
416 struct ieee80211_work *wk)
417{
418 struct cfg80211_bss *cbss;
419 u16 capa_val = WLAN_CAPABILITY_ESS;
420
421 if (wk->probe_auth.privacy)
422 capa_val |= WLAN_CAPABILITY_PRIVACY;
423
424 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
425 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
426 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
427 capa_val);
428 if (!cbss)
429 return;
430
431 cfg80211_unlink_bss(local->hw.wiphy, cbss);
432 cfg80211_put_bss(cbss);
433}
434
435static enum work_action __must_check
436ieee80211_direct_probe(struct ieee80211_work *wk)
437{
438 struct ieee80211_sub_if_data *sdata = wk->sdata;
439 struct ieee80211_local *local = sdata->local;
440
441 wk->probe_auth.tries++;
442 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100443 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100444 sdata->name, wk->filter_ta);
445
446 /*
447 * Most likely AP is not in the range so remove the
448 * bss struct for that AP.
449 */
450 ieee80211_remove_auth_bss(local, wk);
451
Johannes Bergaf6b6372009-12-23 13:15:35 +0100452 return WORK_ACT_TIMEOUT;
453 }
454
Ben Greear2f886752010-12-07 11:27:01 -0800455 printk(KERN_DEBUG "%s: direct probe to %pM (try %d/%i)\n",
456 sdata->name, wk->filter_ta, wk->probe_auth.tries,
457 IEEE80211_AUTH_MAX_TRIES);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100458
459 /*
460 * Direct probe is sent to broadcast address as some APs
461 * will not answer to direct packet in unassociated state.
462 */
463 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
464 wk->probe_auth.ssid_len, NULL, 0);
465
466 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
467 run_again(local, wk->timeout);
468
469 return WORK_ACT_NONE;
470}
471
472
473static enum work_action __must_check
474ieee80211_authenticate(struct ieee80211_work *wk)
475{
476 struct ieee80211_sub_if_data *sdata = wk->sdata;
477 struct ieee80211_local *local = sdata->local;
478
479 wk->probe_auth.tries++;
480 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100481 printk(KERN_DEBUG "%s: authentication with %pM"
Johannes Bergaf6b6372009-12-23 13:15:35 +0100482 " timed out\n", sdata->name, wk->filter_ta);
483
484 /*
485 * Most likely AP is not in the range so remove the
486 * bss struct for that AP.
487 */
488 ieee80211_remove_auth_bss(local, wk);
489
Johannes Bergaf6b6372009-12-23 13:15:35 +0100490 return WORK_ACT_TIMEOUT;
491 }
492
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100493 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100494 sdata->name, wk->filter_ta, wk->probe_auth.tries);
495
496 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
497 wk->ie_len, wk->filter_ta, NULL, 0, 0);
498 wk->probe_auth.transaction = 2;
499
500 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
501 run_again(local, wk->timeout);
502
503 return WORK_ACT_NONE;
504}
505
506static enum work_action __must_check
507ieee80211_associate(struct ieee80211_work *wk)
508{
509 struct ieee80211_sub_if_data *sdata = wk->sdata;
510 struct ieee80211_local *local = sdata->local;
511
512 wk->assoc.tries++;
513 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100514 printk(KERN_DEBUG "%s: association with %pM"
Johannes Bergaf6b6372009-12-23 13:15:35 +0100515 " timed out\n",
516 sdata->name, wk->filter_ta);
517
518 /*
519 * Most likely AP is not in the range so remove the
520 * bss struct for that AP.
521 */
522 if (wk->assoc.bss)
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100523 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100524
Johannes Bergaf6b6372009-12-23 13:15:35 +0100525 return WORK_ACT_TIMEOUT;
526 }
527
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100528 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100529 sdata->name, wk->filter_ta, wk->assoc.tries);
530 ieee80211_send_assoc(sdata, wk);
531
532 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
533 run_again(local, wk->timeout);
534
535 return WORK_ACT_NONE;
536}
537
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100538static enum work_action __must_check
539ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
540{
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100541 /*
542 * First time we run, do nothing -- the generic code will
543 * have switched to the right channel etc.
544 */
Johannes Berg723bae72010-01-25 13:36:36 +0100545 if (!wk->started) {
Johannes Berge4da8c32009-12-23 13:15:43 +0100546 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
547
Kalle Valo1990ca62009-12-30 14:42:20 +0200548 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
549 wk->chan, wk->chan_type,
550 wk->remain.duration, GFP_KERNEL);
Johannes Berge4da8c32009-12-23 13:15:43 +0100551
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100552 return WORK_ACT_NONE;
553 }
554
Johannes Berge4da8c32009-12-23 13:15:43 +0100555 return WORK_ACT_TIMEOUT;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100556}
557
Johannes Berge5b900d2010-07-29 16:08:55 +0200558static enum work_action __must_check
Johannes Bergf30221e2010-11-25 10:02:30 +0100559ieee80211_offchannel_tx(struct ieee80211_work *wk)
560{
561 if (!wk->started) {
562 wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);
563
564 /*
565 * After this, offchan_tx.frame remains but now is no
566 * longer a valid pointer -- we still need it as the
567 * cookie for canceling this work.
568 */
569 ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);
570
571 return WORK_ACT_NONE;
572 }
573
574 return WORK_ACT_TIMEOUT;
575}
576
577static enum work_action __must_check
Johannes Berge5b900d2010-07-29 16:08:55 +0200578ieee80211_assoc_beacon_wait(struct ieee80211_work *wk)
579{
580 if (wk->started)
581 return WORK_ACT_TIMEOUT;
582
583 /*
584 * Wait up to one beacon interval ...
585 * should this be more if we miss one?
586 */
587 printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
588 wk->sdata->name, wk->filter_ta);
589 wk->timeout = TU_TO_EXP_TIME(wk->assoc.bss->beacon_interval);
590 return WORK_ACT_NONE;
591}
592
Johannes Bergaf6b6372009-12-23 13:15:35 +0100593static void ieee80211_auth_challenge(struct ieee80211_work *wk,
594 struct ieee80211_mgmt *mgmt,
595 size_t len)
596{
597 struct ieee80211_sub_if_data *sdata = wk->sdata;
598 u8 *pos;
599 struct ieee802_11_elems elems;
600
601 pos = mgmt->u.auth.variable;
602 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
603 if (!elems.challenge)
604 return;
605 ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
606 elems.challenge - 2, elems.challenge_len + 2,
607 wk->filter_ta, wk->probe_auth.key,
608 wk->probe_auth.key_len, wk->probe_auth.key_idx);
609 wk->probe_auth.transaction = 4;
610}
611
612static enum work_action __must_check
613ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
614 struct ieee80211_mgmt *mgmt, size_t len)
615{
616 u16 auth_alg, auth_transaction, status_code;
617
618 if (wk->type != IEEE80211_WORK_AUTH)
Johannes Bergb8d92c92010-05-11 12:42:04 +0200619 return WORK_ACT_MISMATCH;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100620
621 if (len < 24 + 6)
622 return WORK_ACT_NONE;
623
624 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
625 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
626 status_code = le16_to_cpu(mgmt->u.auth.status_code);
627
628 if (auth_alg != wk->probe_auth.algorithm ||
629 auth_transaction != wk->probe_auth.transaction)
630 return WORK_ACT_NONE;
631
632 if (status_code != WLAN_STATUS_SUCCESS) {
633 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
634 wk->sdata->name, mgmt->sa, status_code);
635 return WORK_ACT_DONE;
636 }
637
638 switch (wk->probe_auth.algorithm) {
639 case WLAN_AUTH_OPEN:
640 case WLAN_AUTH_LEAP:
641 case WLAN_AUTH_FT:
642 break;
643 case WLAN_AUTH_SHARED_KEY:
644 if (wk->probe_auth.transaction != 4) {
645 ieee80211_auth_challenge(wk, mgmt, len);
646 /* need another frame */
647 return WORK_ACT_NONE;
648 }
649 break;
650 default:
651 WARN_ON(1);
652 return WORK_ACT_NONE;
653 }
654
655 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
656 return WORK_ACT_DONE;
657}
658
659static enum work_action __must_check
660ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
661 struct ieee80211_mgmt *mgmt, size_t len,
662 bool reassoc)
663{
664 struct ieee80211_sub_if_data *sdata = wk->sdata;
665 struct ieee80211_local *local = sdata->local;
666 u16 capab_info, status_code, aid;
667 struct ieee802_11_elems elems;
668 u8 *pos;
669
Johannes Bergb8d92c92010-05-11 12:42:04 +0200670 if (wk->type != IEEE80211_WORK_ASSOC)
671 return WORK_ACT_MISMATCH;
672
Johannes Bergaf6b6372009-12-23 13:15:35 +0100673 /*
674 * AssocResp and ReassocResp have identical structure, so process both
675 * of them in this function.
676 */
677
678 if (len < 24 + 6)
679 return WORK_ACT_NONE;
680
681 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
682 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
683 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
684
685 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
686 "status=%d aid=%d)\n",
687 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
688 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
689
690 pos = mgmt->u.assoc_resp.variable;
691 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
692
693 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
694 elems.timeout_int && elems.timeout_int_len == 5 &&
695 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
696 u32 tu, ms;
697 tu = get_unaligned_le32(elems.timeout_int + 1);
698 ms = tu * 1024 / 1000;
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100699 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
Johannes Bergaf6b6372009-12-23 13:15:35 +0100700 "comeback duration %u TU (%u ms)\n",
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100701 sdata->name, mgmt->sa, tu, ms);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100702 wk->timeout = jiffies + msecs_to_jiffies(ms);
703 if (ms > IEEE80211_ASSOC_TIMEOUT)
704 run_again(local, wk->timeout);
705 return WORK_ACT_NONE;
706 }
707
708 if (status_code != WLAN_STATUS_SUCCESS)
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100709 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
710 sdata->name, mgmt->sa, status_code);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100711 else
712 printk(KERN_DEBUG "%s: associated\n", sdata->name);
713
714 return WORK_ACT_DONE;
715}
716
717static enum work_action __must_check
718ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
719 struct ieee80211_mgmt *mgmt, size_t len,
720 struct ieee80211_rx_status *rx_status)
721{
722 struct ieee80211_sub_if_data *sdata = wk->sdata;
723 struct ieee80211_local *local = sdata->local;
724 size_t baselen;
725
726 ASSERT_WORK_MTX(local);
727
Johannes Bergb8d92c92010-05-11 12:42:04 +0200728 if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
729 return WORK_ACT_MISMATCH;
730
731 if (len < 24 + 12)
732 return WORK_ACT_NONE;
733
Johannes Bergaf6b6372009-12-23 13:15:35 +0100734 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
735 if (baselen > len)
736 return WORK_ACT_NONE;
737
738 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
739 return WORK_ACT_DONE;
740}
741
Johannes Berge5b900d2010-07-29 16:08:55 +0200742static enum work_action __must_check
743ieee80211_rx_mgmt_beacon(struct ieee80211_work *wk,
744 struct ieee80211_mgmt *mgmt, size_t len)
745{
746 struct ieee80211_sub_if_data *sdata = wk->sdata;
747 struct ieee80211_local *local = sdata->local;
748
749 ASSERT_WORK_MTX(local);
750
751 if (wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
752 return WORK_ACT_MISMATCH;
753
754 if (len < 24 + 12)
755 return WORK_ACT_NONE;
756
757 printk(KERN_DEBUG "%s: beacon received\n", sdata->name);
758 return WORK_ACT_DONE;
759}
760
Johannes Bergaf6b6372009-12-23 13:15:35 +0100761static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
762 struct sk_buff *skb)
763{
764 struct ieee80211_rx_status *rx_status;
765 struct ieee80211_mgmt *mgmt;
766 struct ieee80211_work *wk;
Christoph Fritz021570e2010-06-16 16:37:34 +0200767 enum work_action rma = WORK_ACT_NONE;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100768 u16 fc;
769
770 rx_status = (struct ieee80211_rx_status *) skb->cb;
771 mgmt = (struct ieee80211_mgmt *) skb->data;
772 fc = le16_to_cpu(mgmt->frame_control);
773
Johannes Berga1699b72010-07-30 16:46:07 +0200774 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100775
776 list_for_each_entry(wk, &local->work_list, list) {
777 const u8 *bssid = NULL;
778
779 switch (wk->type) {
780 case IEEE80211_WORK_DIRECT_PROBE:
781 case IEEE80211_WORK_AUTH:
782 case IEEE80211_WORK_ASSOC:
Johannes Berge5b900d2010-07-29 16:08:55 +0200783 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
Johannes Bergaf6b6372009-12-23 13:15:35 +0100784 bssid = wk->filter_ta;
785 break;
786 default:
787 continue;
788 }
789
790 /*
791 * Before queuing, we already verified mgmt->sa,
792 * so this is needed just for matching.
793 */
794 if (compare_ether_addr(bssid, mgmt->bssid))
795 continue;
796
797 switch (fc & IEEE80211_FCTL_STYPE) {
Johannes Berge5b900d2010-07-29 16:08:55 +0200798 case IEEE80211_STYPE_BEACON:
799 rma = ieee80211_rx_mgmt_beacon(wk, mgmt, skb->len);
800 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100801 case IEEE80211_STYPE_PROBE_RESP:
802 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
803 rx_status);
804 break;
805 case IEEE80211_STYPE_AUTH:
806 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
807 break;
808 case IEEE80211_STYPE_ASSOC_RESP:
809 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
810 skb->len, false);
811 break;
812 case IEEE80211_STYPE_REASSOC_RESP:
813 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
814 skb->len, true);
815 break;
816 default:
817 WARN_ON(1);
Johannes Bergb8d92c92010-05-11 12:42:04 +0200818 rma = WORK_ACT_NONE;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100819 }
Johannes Bergb8d92c92010-05-11 12:42:04 +0200820
821 /*
822 * We've either received an unexpected frame, or we have
823 * multiple work items and need to match the frame to the
824 * right one.
825 */
826 if (rma == WORK_ACT_MISMATCH)
827 continue;
828
Johannes Bergaf6b6372009-12-23 13:15:35 +0100829 /*
830 * We've processed this frame for that work, so it can't
831 * belong to another work struct.
832 * NB: this is also required for correctness for 'rma'!
833 */
834 break;
835 }
836
837 switch (rma) {
Johannes Bergb8d92c92010-05-11 12:42:04 +0200838 case WORK_ACT_MISMATCH:
839 /* ignore this unmatched frame */
840 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100841 case WORK_ACT_NONE:
842 break;
843 case WORK_ACT_DONE:
844 list_del_rcu(&wk->list);
845 break;
846 default:
847 WARN(1, "unexpected: %d", rma);
848 }
849
Johannes Berga1699b72010-07-30 16:46:07 +0200850 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100851
852 if (rma != WORK_ACT_DONE)
853 goto out;
854
855 switch (wk->done(wk, skb)) {
856 case WORK_DONE_DESTROY:
857 free_work(wk);
858 break;
859 case WORK_DONE_REQUEUE:
860 synchronize_rcu();
Johannes Berge4da8c32009-12-23 13:15:43 +0100861 wk->started = false; /* restart */
Johannes Berga1699b72010-07-30 16:46:07 +0200862 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100863 list_add_tail(&wk->list, &local->work_list);
Johannes Berga1699b72010-07-30 16:46:07 +0200864 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100865 }
866
867 out:
868 kfree_skb(skb);
869}
870
Ben Greearda2fd1f2011-02-07 13:44:36 -0800871static bool ieee80211_work_ct_coexists(enum nl80211_channel_type wk_ct,
872 enum nl80211_channel_type oper_ct)
873{
874 switch (wk_ct) {
875 case NL80211_CHAN_NO_HT:
876 return true;
877 case NL80211_CHAN_HT20:
878 if (oper_ct != NL80211_CHAN_NO_HT)
879 return true;
880 return false;
881 case NL80211_CHAN_HT40MINUS:
882 case NL80211_CHAN_HT40PLUS:
883 return (wk_ct == oper_ct);
884 }
885 WARN_ON(1); /* shouldn't get here */
886 return false;
887}
888
889static enum nl80211_channel_type
890ieee80211_calc_ct(enum nl80211_channel_type wk_ct,
891 enum nl80211_channel_type oper_ct)
892{
893 switch (wk_ct) {
894 case NL80211_CHAN_NO_HT:
895 return oper_ct;
896 case NL80211_CHAN_HT20:
897 if (oper_ct != NL80211_CHAN_NO_HT)
898 return oper_ct;
899 return wk_ct;
900 case NL80211_CHAN_HT40MINUS:
901 case NL80211_CHAN_HT40PLUS:
902 return wk_ct;
903 }
904 WARN_ON(1); /* shouldn't get here */
905 return wk_ct;
906}
907
908
Johannes Bergaf6b6372009-12-23 13:15:35 +0100909static void ieee80211_work_timer(unsigned long data)
910{
911 struct ieee80211_local *local = (void *) data;
912
913 if (local->quiescing)
914 return;
915
916 ieee80211_queue_work(&local->hw, &local->work_work);
917}
918
919static void ieee80211_work_work(struct work_struct *work)
920{
921 struct ieee80211_local *local =
922 container_of(work, struct ieee80211_local, work_work);
923 struct sk_buff *skb;
924 struct ieee80211_work *wk, *tmp;
925 LIST_HEAD(free_work);
926 enum work_action rma;
Johannes Berge4da8c32009-12-23 13:15:43 +0100927 bool remain_off_channel = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100928
929 if (local->scanning)
930 return;
931
932 /*
933 * ieee80211_queue_work() should have picked up most cases,
Walter Goldens77c20612010-05-18 04:44:54 -0700934 * here we'll pick the rest.
Johannes Bergaf6b6372009-12-23 13:15:35 +0100935 */
936 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
937 return;
938
939 /* first process frames to avoid timing out while a frame is pending */
940 while ((skb = skb_dequeue(&local->work_skb_queue)))
941 ieee80211_work_rx_queued_mgmt(local, skb);
942
Johannes Berga1699b72010-07-30 16:46:07 +0200943 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100944
Johannes Berg7da7cc12010-08-05 17:02:38 +0200945 ieee80211_recalc_idle(local);
946
Johannes Bergaf6b6372009-12-23 13:15:35 +0100947 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
Johannes Berg723bae72010-01-25 13:36:36 +0100948 bool started = wk->started;
949
Johannes Berge4da8c32009-12-23 13:15:43 +0100950 /* mark work as started if it's on the current off-channel */
Johannes Berg723bae72010-01-25 13:36:36 +0100951 if (!started && local->tmp_channel &&
Johannes Berge4da8c32009-12-23 13:15:43 +0100952 wk->chan == local->tmp_channel &&
953 wk->chan_type == local->tmp_channel_type) {
Johannes Berg723bae72010-01-25 13:36:36 +0100954 started = true;
Johannes Berg81ac3462010-01-06 15:30:58 +0100955 wk->timeout = jiffies;
Johannes Berge4da8c32009-12-23 13:15:43 +0100956 }
957
Johannes Berg723bae72010-01-25 13:36:36 +0100958 if (!started && !local->tmp_channel) {
Ben Greearb23b0252011-02-04 11:54:17 -0800959 bool on_oper_chan;
960 bool tmp_chan_changed = false;
961 bool on_oper_chan2;
Ben Greearda2fd1f2011-02-07 13:44:36 -0800962 enum nl80211_channel_type wk_ct;
Ben Greearb23b0252011-02-04 11:54:17 -0800963 on_oper_chan = ieee80211_cfg_on_oper_channel(local);
Ben Greearda2fd1f2011-02-07 13:44:36 -0800964
965 /* Work with existing channel type if possible. */
966 wk_ct = wk->chan_type;
967 if (wk->chan == local->hw.conf.channel)
968 wk_ct = ieee80211_calc_ct(wk->chan_type,
969 local->hw.conf.channel_type);
970
Ben Greearb23b0252011-02-04 11:54:17 -0800971 if (local->tmp_channel)
972 if ((local->tmp_channel != wk->chan) ||
Ben Greearda2fd1f2011-02-07 13:44:36 -0800973 (local->tmp_channel_type != wk_ct))
Ben Greearb23b0252011-02-04 11:54:17 -0800974 tmp_chan_changed = true;
Johannes Berge4da8c32009-12-23 13:15:43 +0100975
976 local->tmp_channel = wk->chan;
Ben Greearda2fd1f2011-02-07 13:44:36 -0800977 local->tmp_channel_type = wk_ct;
Ben Greearb23b0252011-02-04 11:54:17 -0800978 /*
979 * Leave the station vifs in awake mode if they
980 * happen to be on the same channel as
981 * the requested channel.
982 */
983 on_oper_chan2 = ieee80211_cfg_on_oper_channel(local);
984 if (on_oper_chan != on_oper_chan2) {
985 if (on_oper_chan2) {
986 /* going off oper channel, PS too */
987 ieee80211_offchannel_stop_vifs(local,
988 true);
989 ieee80211_hw_config(local, 0);
990 } else {
991 /* going on channel, but leave PS
992 * off-channel. */
993 ieee80211_hw_config(local, 0);
994 ieee80211_offchannel_return(local,
995 true,
996 false);
997 }
998 } else if (tmp_chan_changed)
999 /* Still off-channel, but on some other
1000 * channel, so update hardware.
1001 * PS should already be off-channel.
1002 */
1003 ieee80211_hw_config(local, 0);
1004
Johannes Berg723bae72010-01-25 13:36:36 +01001005 started = true;
Johannes Berge4da8c32009-12-23 13:15:43 +01001006 wk->timeout = jiffies;
1007 }
1008
1009 /* don't try to work with items that aren't started */
Johannes Berg723bae72010-01-25 13:36:36 +01001010 if (!started)
Johannes Berge4da8c32009-12-23 13:15:43 +01001011 continue;
1012
Johannes Bergaf6b6372009-12-23 13:15:35 +01001013 if (time_is_after_jiffies(wk->timeout)) {
1014 /*
1015 * This work item isn't supposed to be worked on
1016 * right now, but take care to adjust the timer
1017 * properly.
1018 */
1019 run_again(local, wk->timeout);
1020 continue;
1021 }
1022
1023 switch (wk->type) {
1024 default:
1025 WARN_ON(1);
1026 /* nothing */
1027 rma = WORK_ACT_NONE;
1028 break;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001029 case IEEE80211_WORK_ABORT:
1030 rma = WORK_ACT_TIMEOUT;
Juuso Oikarinen0e0a2282010-02-26 08:13:41 +02001031 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001032 case IEEE80211_WORK_DIRECT_PROBE:
1033 rma = ieee80211_direct_probe(wk);
1034 break;
1035 case IEEE80211_WORK_AUTH:
1036 rma = ieee80211_authenticate(wk);
1037 break;
1038 case IEEE80211_WORK_ASSOC:
1039 rma = ieee80211_associate(wk);
1040 break;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001041 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
1042 rma = ieee80211_remain_on_channel_timeout(wk);
1043 break;
Johannes Bergf30221e2010-11-25 10:02:30 +01001044 case IEEE80211_WORK_OFFCHANNEL_TX:
1045 rma = ieee80211_offchannel_tx(wk);
1046 break;
Johannes Berge5b900d2010-07-29 16:08:55 +02001047 case IEEE80211_WORK_ASSOC_BEACON_WAIT:
1048 rma = ieee80211_assoc_beacon_wait(wk);
1049 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001050 }
1051
Johannes Berg723bae72010-01-25 13:36:36 +01001052 wk->started = started;
1053
Johannes Bergaf6b6372009-12-23 13:15:35 +01001054 switch (rma) {
1055 case WORK_ACT_NONE:
Johannes Berge4da8c32009-12-23 13:15:43 +01001056 /* might have changed the timeout */
1057 run_again(local, wk->timeout);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001058 break;
1059 case WORK_ACT_TIMEOUT:
1060 list_del_rcu(&wk->list);
1061 synchronize_rcu();
1062 list_add(&wk->list, &free_work);
1063 break;
1064 default:
1065 WARN(1, "unexpected: %d", rma);
1066 }
1067 }
1068
Johannes Berge4da8c32009-12-23 13:15:43 +01001069 list_for_each_entry(wk, &local->work_list, list) {
1070 if (!wk->started)
1071 continue;
1072 if (wk->chan != local->tmp_channel)
1073 continue;
Ben Greearda2fd1f2011-02-07 13:44:36 -08001074 if (ieee80211_work_ct_coexists(wk->chan_type,
1075 local->tmp_channel_type))
Johannes Berge4da8c32009-12-23 13:15:43 +01001076 continue;
1077 remain_off_channel = true;
1078 }
1079
1080 if (!remain_off_channel && local->tmp_channel) {
Ben Greearb23b0252011-02-04 11:54:17 -08001081 bool on_oper_chan = ieee80211_cfg_on_oper_channel(local);
Johannes Berge4da8c32009-12-23 13:15:43 +01001082 local->tmp_channel = NULL;
Ben Greearb23b0252011-02-04 11:54:17 -08001083 /* If tmp_channel wasn't operating channel, then
1084 * we need to go back on-channel.
1085 * NOTE: If we can ever be here while scannning,
1086 * or if the hw_config() channel config logic changes,
1087 * then we may need to do a more thorough check to see if
1088 * we still need to do a hardware config. Currently,
1089 * we cannot be here while scanning, however.
1090 */
1091 if (ieee80211_cfg_on_oper_channel(local) && !on_oper_chan)
1092 ieee80211_hw_config(local, 0);
1093
1094 /* At the least, we need to disable offchannel_ps,
1095 * so just go ahead and run the entire offchannel
1096 * return logic here. We *could* skip enabling
1097 * beaconing if we were already on-oper-channel
1098 * as a future optimization.
1099 */
1100 ieee80211_offchannel_return(local, true, true);
1101
Johannes Berge4da8c32009-12-23 13:15:43 +01001102 /* give connection some time to breathe */
1103 run_again(local, jiffies + HZ/2);
1104 }
1105
Teemu Paasikivi68dd5b72010-04-09 13:07:55 +03001106 if (list_empty(&local->work_list) && local->scan_req &&
1107 !local->scanning)
Johannes Bergaf6b6372009-12-23 13:15:35 +01001108 ieee80211_queue_delayed_work(&local->hw,
1109 &local->scan_work,
1110 round_jiffies_relative(0));
1111
Johannes Berge4da8c32009-12-23 13:15:43 +01001112 ieee80211_recalc_idle(local);
1113
Johannes Berg7da7cc12010-08-05 17:02:38 +02001114 mutex_unlock(&local->mtx);
1115
Johannes Bergaf6b6372009-12-23 13:15:35 +01001116 list_for_each_entry_safe(wk, tmp, &free_work, list) {
1117 wk->done(wk, NULL);
1118 list_del(&wk->list);
1119 kfree(wk);
1120 }
1121}
1122
1123void ieee80211_add_work(struct ieee80211_work *wk)
1124{
1125 struct ieee80211_local *local;
1126
1127 if (WARN_ON(!wk->chan))
1128 return;
1129
1130 if (WARN_ON(!wk->sdata))
1131 return;
1132
1133 if (WARN_ON(!wk->done))
1134 return;
1135
Johannes Berg81ac3462010-01-06 15:30:58 +01001136 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
1137 return;
1138
Johannes Berge4da8c32009-12-23 13:15:43 +01001139 wk->started = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001140
1141 local = wk->sdata->local;
Johannes Berga1699b72010-07-30 16:46:07 +02001142 mutex_lock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001143 list_add_tail(&wk->list, &local->work_list);
Johannes Berga1699b72010-07-30 16:46:07 +02001144 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001145
1146 ieee80211_queue_work(&local->hw, &local->work_work);
1147}
1148
1149void ieee80211_work_init(struct ieee80211_local *local)
1150{
Johannes Bergaf6b6372009-12-23 13:15:35 +01001151 INIT_LIST_HEAD(&local->work_list);
1152 setup_timer(&local->work_timer, ieee80211_work_timer,
1153 (unsigned long)local);
1154 INIT_WORK(&local->work_work, ieee80211_work_work);
1155 skb_queue_head_init(&local->work_skb_queue);
1156}
1157
1158void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
1159{
1160 struct ieee80211_local *local = sdata->local;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001161 struct ieee80211_work *wk;
Herton Ronaldo Krzesinski8808f642010-12-13 11:43:51 -02001162 bool cleanup = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001163
Johannes Berga1699b72010-07-30 16:46:07 +02001164 mutex_lock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001165 list_for_each_entry(wk, &local->work_list, list) {
Johannes Bergaf6b6372009-12-23 13:15:35 +01001166 if (wk->sdata != sdata)
1167 continue;
Herton Ronaldo Krzesinski8808f642010-12-13 11:43:51 -02001168 cleanup = true;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001169 wk->type = IEEE80211_WORK_ABORT;
Johannes Berge4da8c32009-12-23 13:15:43 +01001170 wk->started = true;
1171 wk->timeout = jiffies;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001172 }
Johannes Berga1699b72010-07-30 16:46:07 +02001173 mutex_unlock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001174
1175 /* run cleanups etc. */
Herton Ronaldo Krzesinski8808f642010-12-13 11:43:51 -02001176 if (cleanup)
1177 ieee80211_work_work(&local->work_work);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001178
Johannes Berga1699b72010-07-30 16:46:07 +02001179 mutex_lock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001180 list_for_each_entry(wk, &local->work_list, list) {
1181 if (wk->sdata != sdata)
1182 continue;
1183 WARN_ON(1);
1184 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +01001185 }
Johannes Berga1699b72010-07-30 16:46:07 +02001186 mutex_unlock(&local->mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01001187}
1188
1189ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1190 struct sk_buff *skb)
1191{
1192 struct ieee80211_local *local = sdata->local;
1193 struct ieee80211_mgmt *mgmt;
1194 struct ieee80211_work *wk;
1195 u16 fc;
1196
1197 if (skb->len < 24)
1198 return RX_DROP_MONITOR;
1199
1200 mgmt = (struct ieee80211_mgmt *) skb->data;
1201 fc = le16_to_cpu(mgmt->frame_control);
1202
1203 list_for_each_entry_rcu(wk, &local->work_list, list) {
1204 if (sdata != wk->sdata)
1205 continue;
1206 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1207 continue;
1208 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1209 continue;
1210
1211 switch (fc & IEEE80211_FCTL_STYPE) {
1212 case IEEE80211_STYPE_AUTH:
1213 case IEEE80211_STYPE_PROBE_RESP:
1214 case IEEE80211_STYPE_ASSOC_RESP:
1215 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berge5b900d2010-07-29 16:08:55 +02001216 case IEEE80211_STYPE_BEACON:
Johannes Bergaf6b6372009-12-23 13:15:35 +01001217 skb_queue_tail(&local->work_skb_queue, skb);
1218 ieee80211_queue_work(&local->hw, &local->work_work);
1219 return RX_QUEUED;
1220 }
1221 }
1222
1223 return RX_CONTINUE;
1224}
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001225
Johannes Berge4da8c32009-12-23 13:15:43 +01001226static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1227 struct sk_buff *skb)
1228{
1229 /*
1230 * We are done serving the remain-on-channel command.
1231 */
Kalle Valo1990ca62009-12-30 14:42:20 +02001232 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
Johannes Berge4da8c32009-12-23 13:15:43 +01001233 wk->chan, wk->chan_type,
1234 GFP_KERNEL);
1235
1236 return WORK_DONE_DESTROY;
1237}
1238
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001239int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1240 struct ieee80211_channel *chan,
1241 enum nl80211_channel_type channel_type,
1242 unsigned int duration, u64 *cookie)
1243{
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001244 struct ieee80211_work *wk;
1245
1246 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1247 if (!wk)
1248 return -ENOMEM;
1249
1250 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1251 wk->chan = chan;
Johannes Berge4da8c32009-12-23 13:15:43 +01001252 wk->chan_type = channel_type;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001253 wk->sdata = sdata;
Johannes Berge4da8c32009-12-23 13:15:43 +01001254 wk->done = ieee80211_remain_done;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001255
Johannes Berge4da8c32009-12-23 13:15:43 +01001256 wk->remain.duration = duration;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001257
Kalle Valo1990ca62009-12-30 14:42:20 +02001258 *cookie = (unsigned long) wk;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001259
1260 ieee80211_add_work(wk);
1261
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001262 return 0;
1263}
1264
1265int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1266 u64 cookie)
1267{
1268 struct ieee80211_local *local = sdata->local;
1269 struct ieee80211_work *wk, *tmp;
1270 bool found = false;
1271
Johannes Berga1699b72010-07-30 16:46:07 +02001272 mutex_lock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001273 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
Kalle Valo1990ca62009-12-30 14:42:20 +02001274 if ((unsigned long) wk == cookie) {
Johannes Berge4da8c32009-12-23 13:15:43 +01001275 wk->timeout = jiffies;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001276 found = true;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001277 break;
1278 }
1279 }
Johannes Berga1699b72010-07-30 16:46:07 +02001280 mutex_unlock(&local->mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001281
1282 if (!found)
1283 return -ENOENT;
1284
Johannes Berge4da8c32009-12-23 13:15:43 +01001285 ieee80211_queue_work(&local->hw, &local->work_work);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001286
1287 return 0;
1288}