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