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