blob: 81bd5d592bb489f837018a93e15ed5a49a0bc258 [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>
22#include <net/mac80211.h>
23#include <asm/unaligned.h>
24
25#include "ieee80211_i.h"
26#include "rate.h"
27
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
33
34enum work_action {
35 WORK_ACT_NONE,
36 WORK_ACT_TIMEOUT,
37 WORK_ACT_DONE,
38};
39
40
41/* utils */
42static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
43{
44 WARN_ON(!mutex_is_locked(&local->work_mtx));
45}
46
47/*
48 * We can have multiple work items (and connection probing)
49 * scheduling this timer, but we need to take care to only
50 * reschedule it when it should fire _earlier_ than it was
51 * asked for before, or if it's not pending right now. This
52 * function ensures that. Note that it then is required to
53 * run this function for all timeouts after the first one
54 * has happened -- the work that runs from this timer will
55 * do that.
56 */
57static void run_again(struct ieee80211_local *local,
58 unsigned long timeout)
59{
60 ASSERT_WORK_MTX(local);
61
62 if (!timer_pending(&local->work_timer) ||
63 time_before(timeout, local->work_timer.expires))
64 mod_timer(&local->work_timer, timeout);
65}
66
67static void work_free_rcu(struct rcu_head *head)
68{
69 struct ieee80211_work *wk =
70 container_of(head, struct ieee80211_work, rcu_head);
71
72 kfree(wk);
73}
74
75void free_work(struct ieee80211_work *wk)
76{
77 call_rcu(&wk->rcu_head, work_free_rcu);
78}
79
80static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
81 struct ieee80211_supported_band *sband,
82 u32 *rates)
83{
84 int i, j, count;
85 *rates = 0;
86 count = 0;
87 for (i = 0; i < supp_rates_len; i++) {
88 int rate = (supp_rates[i] & 0x7F) * 5;
89
90 for (j = 0; j < sband->n_bitrates; j++)
91 if (sband->bitrates[j].bitrate == rate) {
92 *rates |= BIT(j);
93 count++;
94 break;
95 }
96 }
97
98 return count;
99}
100
101/* frame sending functions */
102
Johannes Berg77c81442009-12-23 13:15:37 +0100103static void ieee80211_add_ht_ie(struct sk_buff *skb, const u8 *ht_info_ie,
104 struct ieee80211_supported_band *sband,
105 struct ieee80211_channel *channel,
106 enum ieee80211_smps_mode smps)
107{
108 struct ieee80211_ht_info *ht_info;
109 u8 *pos;
110 u32 flags = channel->flags;
111 u16 cap = sband->ht_cap.cap;
112 __le16 tmp;
113
114 if (!sband->ht_cap.ht_supported)
115 return;
116
117 if (!ht_info_ie)
118 return;
119
120 if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
121 return;
122
123 ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
124
125 /* determine capability flags */
126
127 if (ieee80211_disable_40mhz_24ghz &&
128 sband->band == IEEE80211_BAND_2GHZ) {
129 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
130 cap &= ~IEEE80211_HT_CAP_SGI_40;
131 }
132
133 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
134 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
135 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
136 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
137 cap &= ~IEEE80211_HT_CAP_SGI_40;
138 }
139 break;
140 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
141 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
142 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
143 cap &= ~IEEE80211_HT_CAP_SGI_40;
144 }
145 break;
146 }
147
148 /* set SM PS mode properly */
149 cap &= ~IEEE80211_HT_CAP_SM_PS;
150 switch (smps) {
151 case IEEE80211_SMPS_AUTOMATIC:
152 case IEEE80211_SMPS_NUM_MODES:
153 WARN_ON(1);
154 case IEEE80211_SMPS_OFF:
155 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
156 IEEE80211_HT_CAP_SM_PS_SHIFT;
157 break;
158 case IEEE80211_SMPS_STATIC:
159 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
160 IEEE80211_HT_CAP_SM_PS_SHIFT;
161 break;
162 case IEEE80211_SMPS_DYNAMIC:
163 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
164 IEEE80211_HT_CAP_SM_PS_SHIFT;
165 break;
166 }
167
168 /* reserve and fill IE */
169
170 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
171 *pos++ = WLAN_EID_HT_CAPABILITY;
172 *pos++ = sizeof(struct ieee80211_ht_cap);
173 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
174
175 /* capability flags */
176 tmp = cpu_to_le16(cap);
177 memcpy(pos, &tmp, sizeof(u16));
178 pos += sizeof(u16);
179
180 /* AMPDU parameters */
181 *pos++ = sband->ht_cap.ampdu_factor |
182 (sband->ht_cap.ampdu_density <<
183 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
184
185 /* MCS set */
186 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
187 pos += sizeof(sband->ht_cap.mcs);
188
189 /* extended capabilities */
190 pos += sizeof(__le16);
191
192 /* BF capabilities */
193 pos += sizeof(__le32);
194
195 /* antenna selection */
196 pos += sizeof(u8);
197}
198
Johannes Bergaf6b6372009-12-23 13:15:35 +0100199static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
200 struct ieee80211_work *wk)
201{
202 struct ieee80211_local *local = sdata->local;
203 struct sk_buff *skb;
204 struct ieee80211_mgmt *mgmt;
Kalle Valoab133152010-01-12 10:42:31 +0200205 u8 *pos, qos_info;
Johannes Berg77c81442009-12-23 13:15:37 +0100206 const u8 *ies;
Johannes Berg8e664fb2009-12-23 13:15:38 +0100207 size_t offset = 0, noffset;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100208 int i, len, count, rates_len, supp_rates_len;
209 u16 capab;
210 struct ieee80211_supported_band *sband;
211 u32 rates = 0;
212
Johannes Berg77c81442009-12-23 13:15:37 +0100213 sband = local->hw.wiphy->bands[wk->chan->band];
214
215 /*
216 * Get all rates supported by the device and the AP as
217 * some APs don't like getting a superset of their rates
218 * in the association request (e.g. D-Link DAP 1353 in
219 * b-only mode)...
220 */
221 rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
222 wk->assoc.supp_rates_len,
223 sband, &rates);
224
225 skb = alloc_skb(local->hw.extra_tx_headroom +
226 sizeof(*mgmt) + /* bit too much but doesn't matter */
227 2 + wk->assoc.ssid_len + /* SSID */
228 4 + rates_len + /* (extended) rates */
229 4 + /* power capability */
230 2 + 2 * sband->n_channels + /* supported channels */
231 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
232 wk->ie_len + /* extra IEs */
233 9, /* WMM */
234 GFP_KERNEL);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100235 if (!skb) {
236 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
237 "frame\n", sdata->name);
238 return;
239 }
240 skb_reserve(skb, local->hw.extra_tx_headroom);
241
Johannes Bergaf6b6372009-12-23 13:15:35 +0100242 capab = WLAN_CAPABILITY_ESS;
243
244 if (sband->band == IEEE80211_BAND_2GHZ) {
245 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
246 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
247 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
248 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
249 }
250
251 if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
252 capab |= WLAN_CAPABILITY_PRIVACY;
253
Johannes Bergaf6b6372009-12-23 13:15:35 +0100254 if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
255 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
256 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
257
258 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
259 memset(mgmt, 0, 24);
260 memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
261 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
262 memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
263
264 if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
265 skb_put(skb, 10);
266 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
267 IEEE80211_STYPE_REASSOC_REQ);
268 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
269 mgmt->u.reassoc_req.listen_interval =
270 cpu_to_le16(local->hw.conf.listen_interval);
271 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
272 ETH_ALEN);
273 } else {
274 skb_put(skb, 4);
275 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
276 IEEE80211_STYPE_ASSOC_REQ);
277 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
278 mgmt->u.assoc_req.listen_interval =
279 cpu_to_le16(local->hw.conf.listen_interval);
280 }
281
282 /* SSID */
283 ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
284 *pos++ = WLAN_EID_SSID;
285 *pos++ = wk->assoc.ssid_len;
286 memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
287
288 /* add all rates which were marked to be used above */
289 supp_rates_len = rates_len;
290 if (supp_rates_len > 8)
291 supp_rates_len = 8;
292
293 len = sband->n_bitrates;
294 pos = skb_put(skb, supp_rates_len + 2);
295 *pos++ = WLAN_EID_SUPP_RATES;
296 *pos++ = supp_rates_len;
297
298 count = 0;
299 for (i = 0; i < sband->n_bitrates; i++) {
300 if (BIT(i) & rates) {
301 int rate = sband->bitrates[i].bitrate;
302 *pos++ = (u8) (rate / 5);
303 if (++count == 8)
304 break;
305 }
306 }
307
308 if (rates_len > count) {
309 pos = skb_put(skb, rates_len - count + 2);
310 *pos++ = WLAN_EID_EXT_SUPP_RATES;
311 *pos++ = rates_len - count;
312
313 for (i++; i < sband->n_bitrates; i++) {
314 if (BIT(i) & rates) {
315 int rate = sband->bitrates[i].bitrate;
316 *pos++ = (u8) (rate / 5);
317 }
318 }
319 }
320
321 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
322 /* 1. power capabilities */
323 pos = skb_put(skb, 4);
324 *pos++ = WLAN_EID_PWR_CAPABILITY;
325 *pos++ = 2;
326 *pos++ = 0; /* min tx power */
Johannes Berg77c81442009-12-23 13:15:37 +0100327 *pos++ = wk->chan->max_power; /* max tx power */
Johannes Bergaf6b6372009-12-23 13:15:35 +0100328
329 /* 2. supported channels */
330 /* TODO: get this in reg domain format */
331 pos = skb_put(skb, 2 * sband->n_channels + 2);
332 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
333 *pos++ = 2 * sband->n_channels;
334 for (i = 0; i < sband->n_channels; i++) {
335 *pos++ = ieee80211_frequency_to_channel(
336 sband->channels[i].center_freq);
337 *pos++ = 1; /* one channel in the subband*/
338 }
339 }
340
Johannes Berg8e664fb2009-12-23 13:15:38 +0100341 /* if present, add any custom IEs that go before HT */
Johannes Bergaf6b6372009-12-23 13:15:35 +0100342 if (wk->ie_len && wk->ie) {
Johannes Berg8e664fb2009-12-23 13:15:38 +0100343 static const u8 before_ht[] = {
344 WLAN_EID_SSID,
345 WLAN_EID_SUPP_RATES,
346 WLAN_EID_EXT_SUPP_RATES,
347 WLAN_EID_PWR_CAPABILITY,
348 WLAN_EID_SUPPORTED_CHANNELS,
349 WLAN_EID_RSN,
350 WLAN_EID_QOS_CAPA,
351 WLAN_EID_RRM_ENABLED_CAPABILITIES,
352 WLAN_EID_MOBILITY_DOMAIN,
353 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
354 };
355 noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
356 before_ht, ARRAY_SIZE(before_ht),
357 offset);
358 pos = skb_put(skb, noffset - offset);
359 memcpy(pos, wk->ie + offset, noffset - offset);
360 offset = noffset;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100361 }
362
Johannes Berg77c81442009-12-23 13:15:37 +0100363 if (wk->assoc.use_11n && wk->assoc.wmm_used &&
364 local->hw.queues >= 4)
365 ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
366 sband, wk->chan, wk->assoc.smps);
367
Johannes Berg8e664fb2009-12-23 13:15:38 +0100368 /* if present, add any custom non-vendor IEs that go after HT */
369 if (wk->ie_len && wk->ie) {
370 noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
371 offset);
372 pos = skb_put(skb, noffset - offset);
373 memcpy(pos, wk->ie + offset, noffset - offset);
374 offset = noffset;
375 }
376
Johannes Bergaf6b6372009-12-23 13:15:35 +0100377 if (wk->assoc.wmm_used && local->hw.queues >= 4) {
Kalle Valoab133152010-01-12 10:42:31 +0200378 if (wk->assoc.uapsd_used) {
Kalle Valo50ae0cf2010-01-12 10:42:39 +0200379 qos_info = local->uapsd_queues;
380 qos_info |= (local->uapsd_max_sp_len <<
Kalle Valoab133152010-01-12 10:42:31 +0200381 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
382 } else {
383 qos_info = 0;
384 }
385
Johannes Bergaf6b6372009-12-23 13:15:35 +0100386 pos = skb_put(skb, 9);
387 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
388 *pos++ = 7; /* len */
389 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
390 *pos++ = 0x50;
391 *pos++ = 0xf2;
392 *pos++ = 2; /* WME */
393 *pos++ = 0; /* WME info */
394 *pos++ = 1; /* WME ver */
Kalle Valoab133152010-01-12 10:42:31 +0200395 *pos++ = qos_info;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100396 }
397
Johannes Berg8e664fb2009-12-23 13:15:38 +0100398 /* add any remaining custom (i.e. vendor specific here) IEs */
399 if (wk->ie_len && wk->ie) {
400 noffset = wk->ie_len;
401 pos = skb_put(skb, noffset - offset);
402 memcpy(pos, wk->ie + offset, noffset - offset);
403 }
404
Johannes Bergaf6b6372009-12-23 13:15:35 +0100405 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
406 ieee80211_tx_skb(sdata, skb);
407}
408
409static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
410 struct ieee80211_work *wk)
411{
412 struct cfg80211_bss *cbss;
413 u16 capa_val = WLAN_CAPABILITY_ESS;
414
415 if (wk->probe_auth.privacy)
416 capa_val |= WLAN_CAPABILITY_PRIVACY;
417
418 cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
419 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
420 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
421 capa_val);
422 if (!cbss)
423 return;
424
425 cfg80211_unlink_bss(local->hw.wiphy, cbss);
426 cfg80211_put_bss(cbss);
427}
428
429static enum work_action __must_check
430ieee80211_direct_probe(struct ieee80211_work *wk)
431{
432 struct ieee80211_sub_if_data *sdata = wk->sdata;
433 struct ieee80211_local *local = sdata->local;
434
435 wk->probe_auth.tries++;
436 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100437 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100438 sdata->name, wk->filter_ta);
439
440 /*
441 * Most likely AP is not in the range so remove the
442 * bss struct for that AP.
443 */
444 ieee80211_remove_auth_bss(local, wk);
445
Johannes Bergaf6b6372009-12-23 13:15:35 +0100446 return WORK_ACT_TIMEOUT;
447 }
448
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100449 printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100450 sdata->name, wk->filter_ta, wk->probe_auth.tries);
451
452 /*
453 * Direct probe is sent to broadcast address as some APs
454 * will not answer to direct packet in unassociated state.
455 */
456 ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
457 wk->probe_auth.ssid_len, NULL, 0);
458
459 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
460 run_again(local, wk->timeout);
461
462 return WORK_ACT_NONE;
463}
464
465
466static enum work_action __must_check
467ieee80211_authenticate(struct ieee80211_work *wk)
468{
469 struct ieee80211_sub_if_data *sdata = wk->sdata;
470 struct ieee80211_local *local = sdata->local;
471
472 wk->probe_auth.tries++;
473 if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100474 printk(KERN_DEBUG "%s: authentication with %pM"
Johannes Bergaf6b6372009-12-23 13:15:35 +0100475 " timed out\n", sdata->name, wk->filter_ta);
476
477 /*
478 * Most likely AP is not in the range so remove the
479 * bss struct for that AP.
480 */
481 ieee80211_remove_auth_bss(local, wk);
482
Johannes Bergaf6b6372009-12-23 13:15:35 +0100483 return WORK_ACT_TIMEOUT;
484 }
485
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100486 printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100487 sdata->name, wk->filter_ta, wk->probe_auth.tries);
488
489 ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
490 wk->ie_len, wk->filter_ta, NULL, 0, 0);
491 wk->probe_auth.transaction = 2;
492
493 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
494 run_again(local, wk->timeout);
495
496 return WORK_ACT_NONE;
497}
498
499static enum work_action __must_check
500ieee80211_associate(struct ieee80211_work *wk)
501{
502 struct ieee80211_sub_if_data *sdata = wk->sdata;
503 struct ieee80211_local *local = sdata->local;
504
505 wk->assoc.tries++;
506 if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100507 printk(KERN_DEBUG "%s: association with %pM"
Johannes Bergaf6b6372009-12-23 13:15:35 +0100508 " timed out\n",
509 sdata->name, wk->filter_ta);
510
511 /*
512 * Most likely AP is not in the range so remove the
513 * bss struct for that AP.
514 */
515 if (wk->assoc.bss)
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100516 cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100517
Johannes Bergaf6b6372009-12-23 13:15:35 +0100518 return WORK_ACT_TIMEOUT;
519 }
520
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100521 printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
Johannes Bergaf6b6372009-12-23 13:15:35 +0100522 sdata->name, wk->filter_ta, wk->assoc.tries);
523 ieee80211_send_assoc(sdata, wk);
524
525 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
526 run_again(local, wk->timeout);
527
528 return WORK_ACT_NONE;
529}
530
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100531static enum work_action __must_check
532ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
533{
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100534 /*
535 * First time we run, do nothing -- the generic code will
536 * have switched to the right channel etc.
537 */
Johannes Berge4da8c32009-12-23 13:15:43 +0100538 if (!wk->remain.started) {
539 wk->remain.started = true;
540 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
541
Kalle Valo1990ca62009-12-30 14:42:20 +0200542 cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
543 wk->chan, wk->chan_type,
544 wk->remain.duration, GFP_KERNEL);
Johannes Berge4da8c32009-12-23 13:15:43 +0100545
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100546 return WORK_ACT_NONE;
547 }
548
Johannes Berge4da8c32009-12-23 13:15:43 +0100549 return WORK_ACT_TIMEOUT;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100550}
551
Johannes Bergaf6b6372009-12-23 13:15:35 +0100552static void ieee80211_auth_challenge(struct ieee80211_work *wk,
553 struct ieee80211_mgmt *mgmt,
554 size_t len)
555{
556 struct ieee80211_sub_if_data *sdata = wk->sdata;
557 u8 *pos;
558 struct ieee802_11_elems elems;
559
560 pos = mgmt->u.auth.variable;
561 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
562 if (!elems.challenge)
563 return;
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;
569}
570
571static enum work_action __must_check
572ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
573 struct ieee80211_mgmt *mgmt, size_t len)
574{
575 u16 auth_alg, auth_transaction, status_code;
576
577 if (wk->type != IEEE80211_WORK_AUTH)
578 return WORK_ACT_NONE;
579
580 if (len < 24 + 6)
581 return WORK_ACT_NONE;
582
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);
586
587 if (auth_alg != wk->probe_auth.algorithm ||
588 auth_transaction != wk->probe_auth.transaction)
589 return WORK_ACT_NONE;
590
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;
595 }
596
597 switch (wk->probe_auth.algorithm) {
598 case WLAN_AUTH_OPEN:
599 case WLAN_AUTH_LEAP:
600 case WLAN_AUTH_FT:
601 break;
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;
607 }
608 break;
609 default:
610 WARN_ON(1);
611 return WORK_ACT_NONE;
612 }
613
614 printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
615 return WORK_ACT_DONE;
616}
617
618static enum work_action __must_check
619ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
620 struct ieee80211_mgmt *mgmt, size_t len,
621 bool reassoc)
622{
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;
627 u8 *pos;
628
629 /*
630 * AssocResp and ReassocResp have identical structure, so process both
631 * of them in this function.
632 */
633
634 if (len < 24 + 6)
635 return WORK_ACT_NONE;
636
637 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
638 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
639 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
640
641 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
642 "status=%d aid=%d)\n",
643 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
644 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
645
646 pos = mgmt->u.assoc_resp.variable;
647 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
648
649 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
650 elems.timeout_int && elems.timeout_int_len == 5 &&
651 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
652 u32 tu, ms;
653 tu = get_unaligned_le32(elems.timeout_int + 1);
654 ms = tu * 1024 / 1000;
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100655 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
Johannes Bergaf6b6372009-12-23 13:15:35 +0100656 "comeback duration %u TU (%u ms)\n",
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100657 sdata->name, mgmt->sa, tu, ms);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100658 wk->timeout = jiffies + msecs_to_jiffies(ms);
659 if (ms > IEEE80211_ASSOC_TIMEOUT)
660 run_again(local, wk->timeout);
661 return WORK_ACT_NONE;
662 }
663
664 if (status_code != WLAN_STATUS_SUCCESS)
Johannes Berg7d3a1c32009-12-23 13:15:36 +0100665 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
666 sdata->name, mgmt->sa, status_code);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100667 else
668 printk(KERN_DEBUG "%s: associated\n", sdata->name);
669
670 return WORK_ACT_DONE;
671}
672
673static enum work_action __must_check
674ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
675 struct ieee80211_mgmt *mgmt, size_t len,
676 struct ieee80211_rx_status *rx_status)
677{
678 struct ieee80211_sub_if_data *sdata = wk->sdata;
679 struct ieee80211_local *local = sdata->local;
680 size_t baselen;
681
682 ASSERT_WORK_MTX(local);
683
684 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
685 if (baselen > len)
686 return WORK_ACT_NONE;
687
688 printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
689 return WORK_ACT_DONE;
690}
691
692static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
693 struct sk_buff *skb)
694{
695 struct ieee80211_rx_status *rx_status;
696 struct ieee80211_mgmt *mgmt;
697 struct ieee80211_work *wk;
698 enum work_action rma = WORK_ACT_NONE;
699 u16 fc;
700
701 rx_status = (struct ieee80211_rx_status *) skb->cb;
702 mgmt = (struct ieee80211_mgmt *) skb->data;
703 fc = le16_to_cpu(mgmt->frame_control);
704
705 mutex_lock(&local->work_mtx);
706
707 list_for_each_entry(wk, &local->work_list, list) {
708 const u8 *bssid = NULL;
709
710 switch (wk->type) {
711 case IEEE80211_WORK_DIRECT_PROBE:
712 case IEEE80211_WORK_AUTH:
713 case IEEE80211_WORK_ASSOC:
714 bssid = wk->filter_ta;
715 break;
716 default:
717 continue;
718 }
719
720 /*
721 * Before queuing, we already verified mgmt->sa,
722 * so this is needed just for matching.
723 */
724 if (compare_ether_addr(bssid, mgmt->bssid))
725 continue;
726
727 switch (fc & IEEE80211_FCTL_STYPE) {
728 case IEEE80211_STYPE_PROBE_RESP:
729 rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
730 rx_status);
731 break;
732 case IEEE80211_STYPE_AUTH:
733 rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
734 break;
735 case IEEE80211_STYPE_ASSOC_RESP:
736 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
737 skb->len, false);
738 break;
739 case IEEE80211_STYPE_REASSOC_RESP:
740 rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
741 skb->len, true);
742 break;
743 default:
744 WARN_ON(1);
745 }
746 /*
747 * We've processed this frame for that work, so it can't
748 * belong to another work struct.
749 * NB: this is also required for correctness for 'rma'!
750 */
751 break;
752 }
753
754 switch (rma) {
755 case WORK_ACT_NONE:
756 break;
757 case WORK_ACT_DONE:
758 list_del_rcu(&wk->list);
759 break;
760 default:
761 WARN(1, "unexpected: %d", rma);
762 }
763
764 mutex_unlock(&local->work_mtx);
765
766 if (rma != WORK_ACT_DONE)
767 goto out;
768
769 switch (wk->done(wk, skb)) {
770 case WORK_DONE_DESTROY:
771 free_work(wk);
772 break;
773 case WORK_DONE_REQUEUE:
774 synchronize_rcu();
Johannes Berge4da8c32009-12-23 13:15:43 +0100775 wk->started = false; /* restart */
Johannes Bergaf6b6372009-12-23 13:15:35 +0100776 mutex_lock(&local->work_mtx);
777 list_add_tail(&wk->list, &local->work_list);
778 mutex_unlock(&local->work_mtx);
779 }
780
781 out:
782 kfree_skb(skb);
783}
784
785static void ieee80211_work_timer(unsigned long data)
786{
787 struct ieee80211_local *local = (void *) data;
788
789 if (local->quiescing)
790 return;
791
792 ieee80211_queue_work(&local->hw, &local->work_work);
793}
794
795static void ieee80211_work_work(struct work_struct *work)
796{
797 struct ieee80211_local *local =
798 container_of(work, struct ieee80211_local, work_work);
799 struct sk_buff *skb;
800 struct ieee80211_work *wk, *tmp;
801 LIST_HEAD(free_work);
802 enum work_action rma;
Johannes Berge4da8c32009-12-23 13:15:43 +0100803 bool remain_off_channel = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100804
805 if (local->scanning)
806 return;
807
808 /*
809 * ieee80211_queue_work() should have picked up most cases,
810 * here we'll pick the the rest.
811 */
812 if (WARN(local->suspended, "work scheduled while going to suspend\n"))
813 return;
814
815 /* first process frames to avoid timing out while a frame is pending */
816 while ((skb = skb_dequeue(&local->work_skb_queue)))
817 ieee80211_work_rx_queued_mgmt(local, skb);
818
819 ieee80211_recalc_idle(local);
820
821 mutex_lock(&local->work_mtx);
822
823 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
Johannes Berge4da8c32009-12-23 13:15:43 +0100824 /* mark work as started if it's on the current off-channel */
825 if (!wk->started && local->tmp_channel &&
826 wk->chan == local->tmp_channel &&
827 wk->chan_type == local->tmp_channel_type) {
828 wk->started = true;
Johannes Berg81ac3462010-01-06 15:30:58 +0100829 wk->timeout = jiffies;
Johannes Berge4da8c32009-12-23 13:15:43 +0100830 }
831
832 if (!wk->started && !local->tmp_channel) {
833 /*
834 * TODO: could optimize this by leaving the
835 * station vifs in awake mode if they
836 * happen to be on the same channel as
837 * the requested channel
838 */
839 ieee80211_offchannel_stop_beaconing(local);
840 ieee80211_offchannel_stop_station(local);
841
842 local->tmp_channel = wk->chan;
843 local->tmp_channel_type = wk->chan_type;
844 ieee80211_hw_config(local, 0);
845 wk->started = true;
846 wk->timeout = jiffies;
847 }
848
849 /* don't try to work with items that aren't started */
850 if (!wk->started)
851 continue;
852
Johannes Bergaf6b6372009-12-23 13:15:35 +0100853 if (time_is_after_jiffies(wk->timeout)) {
854 /*
855 * This work item isn't supposed to be worked on
856 * right now, but take care to adjust the timer
857 * properly.
858 */
859 run_again(local, wk->timeout);
860 continue;
861 }
862
863 switch (wk->type) {
864 default:
865 WARN_ON(1);
866 /* nothing */
867 rma = WORK_ACT_NONE;
868 break;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100869 case IEEE80211_WORK_ABORT:
870 rma = WORK_ACT_TIMEOUT;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100871 case IEEE80211_WORK_DIRECT_PROBE:
872 rma = ieee80211_direct_probe(wk);
873 break;
874 case IEEE80211_WORK_AUTH:
875 rma = ieee80211_authenticate(wk);
876 break;
877 case IEEE80211_WORK_ASSOC:
878 rma = ieee80211_associate(wk);
879 break;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100880 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
881 rma = ieee80211_remain_on_channel_timeout(wk);
882 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100883 }
884
885 switch (rma) {
886 case WORK_ACT_NONE:
Johannes Berge4da8c32009-12-23 13:15:43 +0100887 /* might have changed the timeout */
888 run_again(local, wk->timeout);
Johannes Bergaf6b6372009-12-23 13:15:35 +0100889 break;
890 case WORK_ACT_TIMEOUT:
891 list_del_rcu(&wk->list);
892 synchronize_rcu();
893 list_add(&wk->list, &free_work);
894 break;
895 default:
896 WARN(1, "unexpected: %d", rma);
897 }
898 }
899
Johannes Berge4da8c32009-12-23 13:15:43 +0100900 list_for_each_entry(wk, &local->work_list, list) {
901 if (!wk->started)
902 continue;
903 if (wk->chan != local->tmp_channel)
904 continue;
905 if (wk->chan_type != local->tmp_channel_type)
906 continue;
907 remain_off_channel = true;
908 }
909
910 if (!remain_off_channel && local->tmp_channel) {
911 local->tmp_channel = NULL;
912 ieee80211_hw_config(local, 0);
913 ieee80211_offchannel_return(local, true);
914 /* give connection some time to breathe */
915 run_again(local, jiffies + HZ/2);
916 }
917
Johannes Bergaf6b6372009-12-23 13:15:35 +0100918 if (list_empty(&local->work_list) && local->scan_req)
919 ieee80211_queue_delayed_work(&local->hw,
920 &local->scan_work,
921 round_jiffies_relative(0));
922
923 mutex_unlock(&local->work_mtx);
924
Johannes Berge4da8c32009-12-23 13:15:43 +0100925 ieee80211_recalc_idle(local);
926
Johannes Bergaf6b6372009-12-23 13:15:35 +0100927 list_for_each_entry_safe(wk, tmp, &free_work, list) {
928 wk->done(wk, NULL);
929 list_del(&wk->list);
930 kfree(wk);
931 }
932}
933
934void ieee80211_add_work(struct ieee80211_work *wk)
935{
936 struct ieee80211_local *local;
937
938 if (WARN_ON(!wk->chan))
939 return;
940
941 if (WARN_ON(!wk->sdata))
942 return;
943
944 if (WARN_ON(!wk->done))
945 return;
946
Johannes Berg81ac3462010-01-06 15:30:58 +0100947 if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
948 return;
949
Johannes Berge4da8c32009-12-23 13:15:43 +0100950 wk->started = false;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100951
952 local = wk->sdata->local;
953 mutex_lock(&local->work_mtx);
954 list_add_tail(&wk->list, &local->work_list);
955 mutex_unlock(&local->work_mtx);
956
957 ieee80211_queue_work(&local->hw, &local->work_work);
958}
959
960void ieee80211_work_init(struct ieee80211_local *local)
961{
962 mutex_init(&local->work_mtx);
963 INIT_LIST_HEAD(&local->work_list);
964 setup_timer(&local->work_timer, ieee80211_work_timer,
965 (unsigned long)local);
966 INIT_WORK(&local->work_work, ieee80211_work_work);
967 skb_queue_head_init(&local->work_skb_queue);
968}
969
970void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
971{
972 struct ieee80211_local *local = sdata->local;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100973 struct ieee80211_work *wk;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100974
975 mutex_lock(&local->work_mtx);
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100976 list_for_each_entry(wk, &local->work_list, list) {
Johannes Bergaf6b6372009-12-23 13:15:35 +0100977 if (wk->sdata != sdata)
978 continue;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100979 wk->type = IEEE80211_WORK_ABORT;
Johannes Berge4da8c32009-12-23 13:15:43 +0100980 wk->started = true;
981 wk->timeout = jiffies;
Johannes Bergb8bc4b02009-12-23 13:15:42 +0100982 }
983 mutex_unlock(&local->work_mtx);
984
985 /* run cleanups etc. */
986 ieee80211_work_work(&local->work_work);
987
988 mutex_lock(&local->work_mtx);
989 list_for_each_entry(wk, &local->work_list, list) {
990 if (wk->sdata != sdata)
991 continue;
992 WARN_ON(1);
993 break;
Johannes Bergaf6b6372009-12-23 13:15:35 +0100994 }
995 mutex_unlock(&local->work_mtx);
996}
997
998ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
999 struct sk_buff *skb)
1000{
1001 struct ieee80211_local *local = sdata->local;
1002 struct ieee80211_mgmt *mgmt;
1003 struct ieee80211_work *wk;
1004 u16 fc;
1005
1006 if (skb->len < 24)
1007 return RX_DROP_MONITOR;
1008
1009 mgmt = (struct ieee80211_mgmt *) skb->data;
1010 fc = le16_to_cpu(mgmt->frame_control);
1011
1012 list_for_each_entry_rcu(wk, &local->work_list, list) {
1013 if (sdata != wk->sdata)
1014 continue;
1015 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1016 continue;
1017 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1018 continue;
1019
1020 switch (fc & IEEE80211_FCTL_STYPE) {
1021 case IEEE80211_STYPE_AUTH:
1022 case IEEE80211_STYPE_PROBE_RESP:
1023 case IEEE80211_STYPE_ASSOC_RESP:
1024 case IEEE80211_STYPE_REASSOC_RESP:
1025 case IEEE80211_STYPE_DEAUTH:
1026 case IEEE80211_STYPE_DISASSOC:
1027 skb_queue_tail(&local->work_skb_queue, skb);
1028 ieee80211_queue_work(&local->hw, &local->work_work);
1029 return RX_QUEUED;
1030 }
1031 }
1032
1033 return RX_CONTINUE;
1034}
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001035
Johannes Berge4da8c32009-12-23 13:15:43 +01001036static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1037 struct sk_buff *skb)
1038{
1039 /*
1040 * We are done serving the remain-on-channel command.
1041 */
Kalle Valo1990ca62009-12-30 14:42:20 +02001042 cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
Johannes Berge4da8c32009-12-23 13:15:43 +01001043 wk->chan, wk->chan_type,
1044 GFP_KERNEL);
1045
1046 return WORK_DONE_DESTROY;
1047}
1048
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001049int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1050 struct ieee80211_channel *chan,
1051 enum nl80211_channel_type channel_type,
1052 unsigned int duration, u64 *cookie)
1053{
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001054 struct ieee80211_work *wk;
1055
1056 wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1057 if (!wk)
1058 return -ENOMEM;
1059
1060 wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1061 wk->chan = chan;
Johannes Berge4da8c32009-12-23 13:15:43 +01001062 wk->chan_type = channel_type;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001063 wk->sdata = sdata;
Johannes Berge4da8c32009-12-23 13:15:43 +01001064 wk->done = ieee80211_remain_done;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001065
Johannes Berge4da8c32009-12-23 13:15:43 +01001066 wk->remain.duration = duration;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001067
Kalle Valo1990ca62009-12-30 14:42:20 +02001068 *cookie = (unsigned long) wk;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001069
1070 ieee80211_add_work(wk);
1071
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001072 return 0;
1073}
1074
1075int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1076 u64 cookie)
1077{
1078 struct ieee80211_local *local = sdata->local;
1079 struct ieee80211_work *wk, *tmp;
1080 bool found = false;
1081
1082 mutex_lock(&local->work_mtx);
1083 list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
Kalle Valo1990ca62009-12-30 14:42:20 +02001084 if ((unsigned long) wk == cookie) {
Johannes Berge4da8c32009-12-23 13:15:43 +01001085 wk->timeout = jiffies;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001086 found = true;
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001087 break;
1088 }
1089 }
1090 mutex_unlock(&local->work_mtx);
1091
1092 if (!found)
1093 return -ENOENT;
1094
Johannes Berge4da8c32009-12-23 13:15:43 +01001095 ieee80211_queue_work(&local->hw, &local->work_work);
Johannes Bergb8bc4b02009-12-23 13:15:42 +01001096
1097 return 0;
1098}