]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/mac80211/work.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[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_MISMATCH,
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_MISMATCH;
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         if (wk->type != IEEE80211_WORK_ASSOC)
640                 return WORK_ACT_MISMATCH;
641
642         /*
643          * AssocResp and ReassocResp have identical structure, so process both
644          * of them in this function.
645          */
646
647         if (len < 24 + 6)
648                 return WORK_ACT_NONE;
649
650         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
651         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
652         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
653
654         printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
655                "status=%d aid=%d)\n",
656                sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
657                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
658
659         pos = mgmt->u.assoc_resp.variable;
660         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
661
662         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
663             elems.timeout_int && elems.timeout_int_len == 5 &&
664             elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
665                 u32 tu, ms;
666                 tu = get_unaligned_le32(elems.timeout_int + 1);
667                 ms = tu * 1024 / 1000;
668                 printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
669                        "comeback duration %u TU (%u ms)\n",
670                        sdata->name, mgmt->sa, tu, ms);
671                 wk->timeout = jiffies + msecs_to_jiffies(ms);
672                 if (ms > IEEE80211_ASSOC_TIMEOUT)
673                         run_again(local, wk->timeout);
674                 return WORK_ACT_NONE;
675         }
676
677         if (status_code != WLAN_STATUS_SUCCESS)
678                 printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
679                        sdata->name, mgmt->sa, status_code);
680         else
681                 printk(KERN_DEBUG "%s: associated\n", sdata->name);
682
683         return WORK_ACT_DONE;
684 }
685
686 static enum work_action __must_check
687 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
688                              struct ieee80211_mgmt *mgmt, size_t len,
689                              struct ieee80211_rx_status *rx_status)
690 {
691         struct ieee80211_sub_if_data *sdata = wk->sdata;
692         struct ieee80211_local *local = sdata->local;
693         size_t baselen;
694
695         ASSERT_WORK_MTX(local);
696
697         if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
698                 return WORK_ACT_MISMATCH;
699
700         if (len < 24 + 12)
701                 return WORK_ACT_NONE;
702
703         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
704         if (baselen > len)
705                 return WORK_ACT_NONE;
706
707         printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
708         return WORK_ACT_DONE;
709 }
710
711 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
712                                           struct sk_buff *skb)
713 {
714         struct ieee80211_rx_status *rx_status;
715         struct ieee80211_mgmt *mgmt;
716         struct ieee80211_work *wk;
717         enum work_action rma;
718         u16 fc;
719
720         rx_status = (struct ieee80211_rx_status *) skb->cb;
721         mgmt = (struct ieee80211_mgmt *) skb->data;
722         fc = le16_to_cpu(mgmt->frame_control);
723
724         mutex_lock(&local->work_mtx);
725
726         list_for_each_entry(wk, &local->work_list, list) {
727                 const u8 *bssid = NULL;
728
729                 switch (wk->type) {
730                 case IEEE80211_WORK_DIRECT_PROBE:
731                 case IEEE80211_WORK_AUTH:
732                 case IEEE80211_WORK_ASSOC:
733                         bssid = wk->filter_ta;
734                         break;
735                 default:
736                         continue;
737                 }
738
739                 /*
740                  * Before queuing, we already verified mgmt->sa,
741                  * so this is needed just for matching.
742                  */
743                 if (compare_ether_addr(bssid, mgmt->bssid))
744                         continue;
745
746                 switch (fc & IEEE80211_FCTL_STYPE) {
747                 case IEEE80211_STYPE_PROBE_RESP:
748                         rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
749                                                            rx_status);
750                         break;
751                 case IEEE80211_STYPE_AUTH:
752                         rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
753                         break;
754                 case IEEE80211_STYPE_ASSOC_RESP:
755                         rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
756                                                            skb->len, false);
757                         break;
758                 case IEEE80211_STYPE_REASSOC_RESP:
759                         rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
760                                                            skb->len, true);
761                         break;
762                 default:
763                         WARN_ON(1);
764                         rma = WORK_ACT_NONE;
765                 }
766
767                 /*
768                  * We've either received an unexpected frame, or we have
769                  * multiple work items and need to match the frame to the
770                  * right one.
771                  */
772                 if (rma == WORK_ACT_MISMATCH)
773                         continue;
774
775                 /*
776                  * We've processed this frame for that work, so it can't
777                  * belong to another work struct.
778                  * NB: this is also required for correctness for 'rma'!
779                  */
780                 break;
781         }
782
783         switch (rma) {
784         case WORK_ACT_MISMATCH:
785                 /* ignore this unmatched frame */
786                 break;
787         case WORK_ACT_NONE:
788                 break;
789         case WORK_ACT_DONE:
790                 list_del_rcu(&wk->list);
791                 break;
792         default:
793                 WARN(1, "unexpected: %d", rma);
794         }
795
796         mutex_unlock(&local->work_mtx);
797
798         if (rma != WORK_ACT_DONE)
799                 goto out;
800
801         switch (wk->done(wk, skb)) {
802         case WORK_DONE_DESTROY:
803                 free_work(wk);
804                 break;
805         case WORK_DONE_REQUEUE:
806                 synchronize_rcu();
807                 wk->started = false; /* restart */
808                 mutex_lock(&local->work_mtx);
809                 list_add_tail(&wk->list, &local->work_list);
810                 mutex_unlock(&local->work_mtx);
811         }
812
813  out:
814         kfree_skb(skb);
815 }
816
817 static void ieee80211_work_timer(unsigned long data)
818 {
819         struct ieee80211_local *local = (void *) data;
820
821         if (local->quiescing)
822                 return;
823
824         ieee80211_queue_work(&local->hw, &local->work_work);
825 }
826
827 static void ieee80211_work_work(struct work_struct *work)
828 {
829         struct ieee80211_local *local =
830                 container_of(work, struct ieee80211_local, work_work);
831         struct sk_buff *skb;
832         struct ieee80211_work *wk, *tmp;
833         LIST_HEAD(free_work);
834         enum work_action rma;
835         bool remain_off_channel = false;
836
837         if (local->scanning)
838                 return;
839
840         /*
841          * ieee80211_queue_work() should have picked up most cases,
842          * here we'll pick the the rest.
843          */
844         if (WARN(local->suspended, "work scheduled while going to suspend\n"))
845                 return;
846
847         /* first process frames to avoid timing out while a frame is pending */
848         while ((skb = skb_dequeue(&local->work_skb_queue)))
849                 ieee80211_work_rx_queued_mgmt(local, skb);
850
851         ieee80211_recalc_idle(local);
852
853         mutex_lock(&local->work_mtx);
854
855         list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
856                 bool started = wk->started;
857
858                 /* mark work as started if it's on the current off-channel */
859                 if (!started && local->tmp_channel &&
860                     wk->chan == local->tmp_channel &&
861                     wk->chan_type == local->tmp_channel_type) {
862                         started = true;
863                         wk->timeout = jiffies;
864                 }
865
866                 if (!started && !local->tmp_channel) {
867                         /*
868                          * TODO: could optimize this by leaving the
869                          *       station vifs in awake mode if they
870                          *       happen to be on the same channel as
871                          *       the requested channel
872                          */
873                         ieee80211_offchannel_stop_beaconing(local);
874                         ieee80211_offchannel_stop_station(local);
875
876                         local->tmp_channel = wk->chan;
877                         local->tmp_channel_type = wk->chan_type;
878                         ieee80211_hw_config(local, 0);
879                         started = true;
880                         wk->timeout = jiffies;
881                 }
882
883                 /* don't try to work with items that aren't started */
884                 if (!started)
885                         continue;
886
887                 if (time_is_after_jiffies(wk->timeout)) {
888                         /*
889                          * This work item isn't supposed to be worked on
890                          * right now, but take care to adjust the timer
891                          * properly.
892                          */
893                         run_again(local, wk->timeout);
894                         continue;
895                 }
896
897                 switch (wk->type) {
898                 default:
899                         WARN_ON(1);
900                         /* nothing */
901                         rma = WORK_ACT_NONE;
902                         break;
903                 case IEEE80211_WORK_ABORT:
904                         rma = WORK_ACT_TIMEOUT;
905                         break;
906                 case IEEE80211_WORK_DIRECT_PROBE:
907                         rma = ieee80211_direct_probe(wk);
908                         break;
909                 case IEEE80211_WORK_AUTH:
910                         rma = ieee80211_authenticate(wk);
911                         break;
912                 case IEEE80211_WORK_ASSOC:
913                         rma = ieee80211_associate(wk);
914                         break;
915                 case IEEE80211_WORK_REMAIN_ON_CHANNEL:
916                         rma = ieee80211_remain_on_channel_timeout(wk);
917                         break;
918                 }
919
920                 wk->started = started;
921
922                 switch (rma) {
923                 case WORK_ACT_NONE:
924                         /* might have changed the timeout */
925                         run_again(local, wk->timeout);
926                         break;
927                 case WORK_ACT_TIMEOUT:
928                         list_del_rcu(&wk->list);
929                         synchronize_rcu();
930                         list_add(&wk->list, &free_work);
931                         break;
932                 default:
933                         WARN(1, "unexpected: %d", rma);
934                 }
935         }
936
937         list_for_each_entry(wk, &local->work_list, list) {
938                 if (!wk->started)
939                         continue;
940                 if (wk->chan != local->tmp_channel)
941                         continue;
942                 if (wk->chan_type != local->tmp_channel_type)
943                         continue;
944                 remain_off_channel = true;
945         }
946
947         if (!remain_off_channel && local->tmp_channel) {
948                 local->tmp_channel = NULL;
949                 ieee80211_hw_config(local, 0);
950                 ieee80211_offchannel_return(local, true);
951                 /* give connection some time to breathe */
952                 run_again(local, jiffies + HZ/2);
953         }
954
955         mutex_lock(&local->scan_mtx);
956
957         if (list_empty(&local->work_list) && local->scan_req &&
958             !local->scanning)
959                 ieee80211_queue_delayed_work(&local->hw,
960                                              &local->scan_work,
961                                              round_jiffies_relative(0));
962
963         mutex_unlock(&local->scan_mtx);
964
965         mutex_unlock(&local->work_mtx);
966
967         ieee80211_recalc_idle(local);
968
969         list_for_each_entry_safe(wk, tmp, &free_work, list) {
970                 wk->done(wk, NULL);
971                 list_del(&wk->list);
972                 kfree(wk);
973         }
974 }
975
976 void ieee80211_add_work(struct ieee80211_work *wk)
977 {
978         struct ieee80211_local *local;
979
980         if (WARN_ON(!wk->chan))
981                 return;
982
983         if (WARN_ON(!wk->sdata))
984                 return;
985
986         if (WARN_ON(!wk->done))
987                 return;
988
989         if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
990                 return;
991
992         wk->started = false;
993
994         local = wk->sdata->local;
995         mutex_lock(&local->work_mtx);
996         list_add_tail(&wk->list, &local->work_list);
997         mutex_unlock(&local->work_mtx);
998
999         ieee80211_queue_work(&local->hw, &local->work_work);
1000 }
1001
1002 void ieee80211_work_init(struct ieee80211_local *local)
1003 {
1004         mutex_init(&local->work_mtx);
1005         INIT_LIST_HEAD(&local->work_list);
1006         setup_timer(&local->work_timer, ieee80211_work_timer,
1007                     (unsigned long)local);
1008         INIT_WORK(&local->work_work, ieee80211_work_work);
1009         skb_queue_head_init(&local->work_skb_queue);
1010 }
1011
1012 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
1013 {
1014         struct ieee80211_local *local = sdata->local;
1015         struct ieee80211_work *wk;
1016
1017         mutex_lock(&local->work_mtx);
1018         list_for_each_entry(wk, &local->work_list, list) {
1019                 if (wk->sdata != sdata)
1020                         continue;
1021                 wk->type = IEEE80211_WORK_ABORT;
1022                 wk->started = true;
1023                 wk->timeout = jiffies;
1024         }
1025         mutex_unlock(&local->work_mtx);
1026
1027         /* run cleanups etc. */
1028         ieee80211_work_work(&local->work_work);
1029
1030         mutex_lock(&local->work_mtx);
1031         list_for_each_entry(wk, &local->work_list, list) {
1032                 if (wk->sdata != sdata)
1033                         continue;
1034                 WARN_ON(1);
1035                 break;
1036         }
1037         mutex_unlock(&local->work_mtx);
1038 }
1039
1040 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1041                                            struct sk_buff *skb)
1042 {
1043         struct ieee80211_local *local = sdata->local;
1044         struct ieee80211_mgmt *mgmt;
1045         struct ieee80211_work *wk;
1046         u16 fc;
1047
1048         if (skb->len < 24)
1049                 return RX_DROP_MONITOR;
1050
1051         mgmt = (struct ieee80211_mgmt *) skb->data;
1052         fc = le16_to_cpu(mgmt->frame_control);
1053
1054         list_for_each_entry_rcu(wk, &local->work_list, list) {
1055                 if (sdata != wk->sdata)
1056                         continue;
1057                 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
1058                         continue;
1059                 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
1060                         continue;
1061
1062                 switch (fc & IEEE80211_FCTL_STYPE) {
1063                 case IEEE80211_STYPE_AUTH:
1064                 case IEEE80211_STYPE_PROBE_RESP:
1065                 case IEEE80211_STYPE_ASSOC_RESP:
1066                 case IEEE80211_STYPE_REASSOC_RESP:
1067                         skb_queue_tail(&local->work_skb_queue, skb);
1068                         ieee80211_queue_work(&local->hw, &local->work_work);
1069                         return RX_QUEUED;
1070                 }
1071         }
1072
1073         return RX_CONTINUE;
1074 }
1075
1076 static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
1077                                                    struct sk_buff *skb)
1078 {
1079         /*
1080          * We are done serving the remain-on-channel command.
1081          */
1082         cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
1083                                            wk->chan, wk->chan_type,
1084                                            GFP_KERNEL);
1085
1086         return WORK_DONE_DESTROY;
1087 }
1088
1089 int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1090                                    struct ieee80211_channel *chan,
1091                                    enum nl80211_channel_type channel_type,
1092                                    unsigned int duration, u64 *cookie)
1093 {
1094         struct ieee80211_work *wk;
1095
1096         wk = kzalloc(sizeof(*wk), GFP_KERNEL);
1097         if (!wk)
1098                 return -ENOMEM;
1099
1100         wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
1101         wk->chan = chan;
1102         wk->chan_type = channel_type;
1103         wk->sdata = sdata;
1104         wk->done = ieee80211_remain_done;
1105
1106         wk->remain.duration = duration;
1107
1108         *cookie = (unsigned long) wk;
1109
1110         ieee80211_add_work(wk);
1111
1112         return 0;
1113 }
1114
1115 int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
1116                                           u64 cookie)
1117 {
1118         struct ieee80211_local *local = sdata->local;
1119         struct ieee80211_work *wk, *tmp;
1120         bool found = false;
1121
1122         mutex_lock(&local->work_mtx);
1123         list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
1124                 if ((unsigned long) wk == cookie) {
1125                         wk->timeout = jiffies;
1126                         found = true;
1127                         break;
1128                 }
1129         }
1130         mutex_unlock(&local->work_mtx);
1131
1132         if (!found)
1133                 return -ENOENT;
1134
1135         ieee80211_queue_work(&local->hw, &local->work_work);
1136
1137         return 0;
1138 }