mac80211: make off-channel work generic
[linux-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 <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;
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         /*
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);
235         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
242         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
254         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 */
327                 *pos++ = wk->chan->max_power; /* max tx power */
328
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
341         /* if present, add any custom IEs that go before HT */
342         if (wk->ie_len && wk->ie) {
343                 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;
361         }
362
363         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
368         /* 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
377         if (wk->assoc.wmm_used && local->hw.queues >= 4) {
378                 pos = skb_put(skb, 9);
379                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
380                 *pos++ = 7; /* len */
381                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
382                 *pos++ = 0x50;
383                 *pos++ = 0xf2;
384                 *pos++ = 2; /* WME */
385                 *pos++ = 0; /* WME info */
386                 *pos++ = 1; /* WME ver */
387                 *pos++ = 0;
388         }
389
390         /* add any remaining custom (i.e. vendor specific here) IEs */
391         if (wk->ie_len && wk->ie) {
392                 noffset = wk->ie_len;
393                 pos = skb_put(skb, noffset - offset);
394                 memcpy(pos, wk->ie + offset, noffset - offset);
395         }
396
397         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
398         ieee80211_tx_skb(sdata, skb);
399 }
400
401 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
402                                       struct ieee80211_work *wk)
403 {
404         struct cfg80211_bss *cbss;
405         u16 capa_val = WLAN_CAPABILITY_ESS;
406
407         if (wk->probe_auth.privacy)
408                 capa_val |= WLAN_CAPABILITY_PRIVACY;
409
410         cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
411                                 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
412                                 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
413                                 capa_val);
414         if (!cbss)
415                 return;
416
417         cfg80211_unlink_bss(local->hw.wiphy, cbss);
418         cfg80211_put_bss(cbss);
419 }
420
421 static enum work_action __must_check
422 ieee80211_direct_probe(struct ieee80211_work *wk)
423 {
424         struct ieee80211_sub_if_data *sdata = wk->sdata;
425         struct ieee80211_local *local = sdata->local;
426
427         wk->probe_auth.tries++;
428         if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
429                 printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
430                        sdata->name, wk->filter_ta);
431
432                 /*
433                  * Most likely AP is not in the range so remove the
434                  * bss struct for that AP.
435                  */
436                 ieee80211_remove_auth_bss(local, wk);
437
438                 /*
439                  * We might have a pending scan which had no chance to run yet
440                  * due to work needing to be done. Hence, queue the STAs work
441                  * again for that.
442                  */
443                 ieee80211_queue_work(&local->hw, &local->work_work);
444                 return WORK_ACT_TIMEOUT;
445         }
446
447         printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
448                         sdata->name, wk->filter_ta, wk->probe_auth.tries);
449
450         /*
451          * Direct probe is sent to broadcast address as some APs
452          * will not answer to direct packet in unassociated state.
453          */
454         ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
455                                  wk->probe_auth.ssid_len, NULL, 0);
456
457         wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
458         run_again(local, wk->timeout);
459
460         return WORK_ACT_NONE;
461 }
462
463
464 static enum work_action __must_check
465 ieee80211_authenticate(struct ieee80211_work *wk)
466 {
467         struct ieee80211_sub_if_data *sdata = wk->sdata;
468         struct ieee80211_local *local = sdata->local;
469
470         wk->probe_auth.tries++;
471         if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
472                 printk(KERN_DEBUG "%s: authentication with %pM"
473                        " timed out\n", sdata->name, wk->filter_ta);
474
475                 /*
476                  * Most likely AP is not in the range so remove the
477                  * bss struct for that AP.
478                  */
479                 ieee80211_remove_auth_bss(local, wk);
480
481                 /*
482                  * We might have a pending scan which had no chance to run yet
483                  * due to work needing to be done. Hence, queue the STAs work
484                  * again for that.
485                  */
486                 ieee80211_queue_work(&local->hw, &local->work_work);
487                 return WORK_ACT_TIMEOUT;
488         }
489
490         printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
491                sdata->name, wk->filter_ta, wk->probe_auth.tries);
492
493         ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
494                             wk->ie_len, wk->filter_ta, NULL, 0, 0);
495         wk->probe_auth.transaction = 2;
496
497         wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
498         run_again(local, wk->timeout);
499
500         return WORK_ACT_NONE;
501 }
502
503 static enum work_action __must_check
504 ieee80211_associate(struct ieee80211_work *wk)
505 {
506         struct ieee80211_sub_if_data *sdata = wk->sdata;
507         struct ieee80211_local *local = sdata->local;
508
509         wk->assoc.tries++;
510         if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
511                 printk(KERN_DEBUG "%s: association with %pM"
512                        " timed out\n",
513                        sdata->name, wk->filter_ta);
514
515                 /*
516                  * Most likely AP is not in the range so remove the
517                  * bss struct for that AP.
518                  */
519                 if (wk->assoc.bss)
520                         cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
521
522                 /*
523                  * We might have a pending scan which had no chance to run yet
524                  * due to work needing to be done. Hence, queue the STAs work
525                  * again for that.
526                  */
527                 ieee80211_queue_work(&local->hw, &local->work_work);
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->remain.started) {
549                 wk->remain.started = true;
550                 wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
551
552                 cfg80211_ready_on_channel(wk->sdata->dev, (u64)wk, wk->chan,
553                                           wk->chan_type, wk->remain.duration,
554                                           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                 /* mark work as started if it's on the current off-channel */
835                 if (!wk->started && local->tmp_channel &&
836                     wk->chan == local->tmp_channel &&
837                     wk->chan_type == local->tmp_channel_type) {
838                         wk->started = true;
839                 }
840
841                 if (!wk->started && !local->tmp_channel) {
842                         /*
843                          * TODO: could optimize this by leaving the
844                          *       station vifs in awake mode if they
845                          *       happen to be on the same channel as
846                          *       the requested channel
847                          */
848                         ieee80211_offchannel_stop_beaconing(local);
849                         ieee80211_offchannel_stop_station(local);
850
851                         local->tmp_channel = wk->chan;
852                         local->tmp_channel_type = wk->chan_type;
853                         ieee80211_hw_config(local, 0);
854                         wk->started = true;
855                         wk->timeout = jiffies;
856                 }
857
858                 /* don't try to work with items that aren't started */
859                 if (!wk->started)
860                         continue;
861
862                 if (time_is_after_jiffies(wk->timeout)) {
863                         /*
864                          * This work item isn't supposed to be worked on
865                          * right now, but take care to adjust the timer
866                          * properly.
867                          */
868                         run_again(local, wk->timeout);
869                         continue;
870                 }
871
872                 switch (wk->type) {
873                 default:
874                         WARN_ON(1);
875                         /* nothing */
876                         rma = WORK_ACT_NONE;
877                         break;
878                 case IEEE80211_WORK_ABORT:
879                         rma = WORK_ACT_TIMEOUT;
880                 case IEEE80211_WORK_DIRECT_PROBE:
881                         rma = ieee80211_direct_probe(wk);
882                         break;
883                 case IEEE80211_WORK_AUTH:
884                         rma = ieee80211_authenticate(wk);
885                         break;
886                 case IEEE80211_WORK_ASSOC:
887                         rma = ieee80211_associate(wk);
888                         break;
889                 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
890                         rma = ieee80211_remain_on_channel_timeout(wk);
891                         break;
892                 }
893
894                 switch (rma) {
895                 case WORK_ACT_NONE:
896                         /* might have changed the timeout */
897                         run_again(local, wk->timeout);
898                         break;
899                 case WORK_ACT_TIMEOUT:
900                         list_del_rcu(&wk->list);
901                         synchronize_rcu();
902                         list_add(&wk->list, &free_work);
903                         break;
904                 default:
905                         WARN(1, "unexpected: %d", rma);
906                 }
907         }
908
909         list_for_each_entry(wk, &local->work_list, list) {
910                 if (!wk->started)
911                         continue;
912                 if (wk->chan != local->tmp_channel)
913                         continue;
914                 if (wk->chan_type != local->tmp_channel_type)
915                         continue;
916                 remain_off_channel = true;
917         }
918
919         if (!remain_off_channel && local->tmp_channel) {
920                 local->tmp_channel = NULL;
921                 ieee80211_hw_config(local, 0);
922                 ieee80211_offchannel_return(local, true);
923                 /* give connection some time to breathe */
924                 run_again(local, jiffies + HZ/2);
925         }
926
927         if (list_empty(&local->work_list) && local->scan_req)
928                 ieee80211_queue_delayed_work(&local->hw,
929                                              &local->scan_work,
930                                              round_jiffies_relative(0));
931
932         mutex_unlock(&local->work_mtx);
933
934         ieee80211_recalc_idle(local);
935
936         list_for_each_entry_safe(wk, tmp, &free_work, list) {
937                 wk->done(wk, NULL);
938                 list_del(&wk->list);
939                 kfree(wk);
940         }
941 }
942
943 void ieee80211_add_work(struct ieee80211_work *wk)
944 {
945         struct ieee80211_local *local;
946
947         if (WARN_ON(!wk->chan))
948                 return;
949
950         if (WARN_ON(!wk->sdata))
951                 return;
952
953         if (WARN_ON(!wk->done))
954                 return;
955
956         wk->started = false;
957
958         local = wk->sdata->local;
959         mutex_lock(&local->work_mtx);
960         list_add_tail(&wk->list, &local->work_list);
961         mutex_unlock(&local->work_mtx);
962
963         ieee80211_queue_work(&local->hw, &local->work_work);
964 }
965
966 void ieee80211_work_init(struct ieee80211_local *local)
967 {
968         mutex_init(&local->work_mtx);
969         INIT_LIST_HEAD(&local->work_list);
970         setup_timer(&local->work_timer, ieee80211_work_timer,
971                     (unsigned long)local);
972         INIT_WORK(&local->work_work, ieee80211_work_work);
973         skb_queue_head_init(&local->work_skb_queue);
974 }
975
976 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
977 {
978         struct ieee80211_local *local = sdata->local;
979         struct ieee80211_work *wk;
980
981         mutex_lock(&local->work_mtx);
982         list_for_each_entry(wk, &local->work_list, list) {
983                 if (wk->sdata != sdata)
984                         continue;
985                 wk->type = IEEE80211_WORK_ABORT;
986                 wk->started = true;
987                 wk->timeout = jiffies;
988         }
989         mutex_unlock(&local->work_mtx);
990
991         /* run cleanups etc. */
992         ieee80211_work_work(&local->work_work);
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                 WARN_ON(1);
999                 break;
1000         }
1001         mutex_unlock(&local->work_mtx);
1002 }
1003
1004 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1005                                            struct sk_buff *skb)
1006 {
1007         struct ieee80211_local *local = sdata->local;
1008         struct ieee80211_mgmt *mgmt;
1009         struct ieee80211_work *wk;
1010         u16 fc;
1011
1012         if (skb->len < 24)
1013                 return RX_DROP_MONITOR;
1014
1015         mgmt = (struct ieee80211_mgmt *) skb->data;
1016         fc = le16_to_cpu(mgmt->frame_control);
1017
1018         list_for_each_entry_rcu(wk, &local->work_list, list) {
1019                 if (sdata != wk->sdata)
1020                         continue;
1021                 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1022                         continue;
1023                 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1024                         continue;
1025
1026                 switch (fc & IEEE80211_FCTL_STYPE) {
1027                 case IEEE80211_STYPE_AUTH:
1028                 case IEEE80211_STYPE_PROBE_RESP:
1029                 case IEEE80211_STYPE_ASSOC_RESP:
1030                 case IEEE80211_STYPE_REASSOC_RESP:
1031                 case IEEE80211_STYPE_DEAUTH:
1032                 case IEEE80211_STYPE_DISASSOC:
1033                         skb_queue_tail(&local->work_skb_queue, skb);
1034                         ieee80211_queue_work(&local->hw, &local->work_work);
1035                         return RX_QUEUED;
1036                 }
1037         }
1038
1039         return RX_CONTINUE;
1040 }
1041
1042 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1043                                                    struct sk_buff *skb)
1044 {
1045         /*
1046          * We are done serving the remain-on-channel command.
1047          */
1048         cfg80211_remain_on_channel_expired(wk->sdata->dev, (u64)wk,
1049                                            wk->chan, wk->chan_type,
1050                                            GFP_KERNEL);
1051
1052         return WORK_DONE_DESTROY;
1053 }
1054
1055 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1056                                    struct ieee80211_channel *chan,
1057                                    enum nl80211_channel_type channel_type,
1058                                    unsigned int duration, u64 *cookie)
1059 {
1060         struct ieee80211_work *wk;
1061
1062         wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1063         if (!wk)
1064                 return -ENOMEM;
1065
1066         wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1067         wk->chan = chan;
1068         wk->chan_type = channel_type;
1069         wk->sdata = sdata;
1070         wk->done = ieee80211_remain_done;
1071
1072         wk->remain.duration = duration;
1073
1074         *cookie = (u64)wk;
1075
1076         ieee80211_add_work(wk);
1077
1078         return 0;
1079 }
1080
1081 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1082                                           u64 cookie)
1083 {
1084         struct ieee80211_local *local = sdata->local;
1085         struct ieee80211_work *wk, *tmp;
1086         bool found = false;
1087
1088         mutex_lock(&local->work_mtx);
1089         list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1090                 if ((u64)wk == cookie) {
1091                         wk->timeout = jiffies;
1092                         found = true;
1093                         break;
1094                 }
1095         }
1096         mutex_unlock(&local->work_mtx);
1097
1098         if (!found)
1099                 return -ENOENT;
1100
1101         ieee80211_queue_work(&local->hw, &local->work_work);
1102
1103         return 0;
1104 }