mac80211: generalise work handling
[safe/jmp/linux-2.6] / 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_send_assoc(struct ieee80211_sub_if_data *sdata,
104                                  struct ieee80211_work *wk)
105 {
106         struct ieee80211_local *local = sdata->local;
107         struct sk_buff *skb;
108         struct ieee80211_mgmt *mgmt;
109         u8 *pos;
110         const u8 *ies, *ht_ie;
111         int i, len, count, rates_len, supp_rates_len;
112         u16 capab;
113         struct ieee80211_supported_band *sband;
114         u32 rates = 0;
115
116         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
117                             sizeof(*mgmt) + 200 + wk->ie_len +
118                             wk->assoc.ssid_len);
119         if (!skb) {
120                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
121                        "frame\n", sdata->name);
122                 return;
123         }
124         skb_reserve(skb, local->hw.extra_tx_headroom);
125
126         sband = local->hw.wiphy->bands[wk->chan->band];
127
128         capab = WLAN_CAPABILITY_ESS;
129
130         if (sband->band == IEEE80211_BAND_2GHZ) {
131                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
132                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
133                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
134                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
135         }
136
137         if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
138                 capab |= WLAN_CAPABILITY_PRIVACY;
139
140         /*
141          * Get all rates supported by the device and the AP as
142          * some APs don't like getting a superset of their rates
143          * in the association request (e.g. D-Link DAP 1353 in
144          * b-only mode)...
145          */
146         rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
147                                                wk->assoc.supp_rates_len,
148                                                sband, &rates);
149
150         if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
151             (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
152                 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
153
154         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
155         memset(mgmt, 0, 24);
156         memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
157         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
158         memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
159
160         if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
161                 skb_put(skb, 10);
162                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
163                                                   IEEE80211_STYPE_REASSOC_REQ);
164                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
165                 mgmt->u.reassoc_req.listen_interval =
166                                 cpu_to_le16(local->hw.conf.listen_interval);
167                 memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
168                        ETH_ALEN);
169         } else {
170                 skb_put(skb, 4);
171                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
172                                                   IEEE80211_STYPE_ASSOC_REQ);
173                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
174                 mgmt->u.assoc_req.listen_interval =
175                                 cpu_to_le16(local->hw.conf.listen_interval);
176         }
177
178         /* SSID */
179         ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
180         *pos++ = WLAN_EID_SSID;
181         *pos++ = wk->assoc.ssid_len;
182         memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
183
184         /* add all rates which were marked to be used above */
185         supp_rates_len = rates_len;
186         if (supp_rates_len > 8)
187                 supp_rates_len = 8;
188
189         len = sband->n_bitrates;
190         pos = skb_put(skb, supp_rates_len + 2);
191         *pos++ = WLAN_EID_SUPP_RATES;
192         *pos++ = supp_rates_len;
193
194         count = 0;
195         for (i = 0; i < sband->n_bitrates; i++) {
196                 if (BIT(i) & rates) {
197                         int rate = sband->bitrates[i].bitrate;
198                         *pos++ = (u8) (rate / 5);
199                         if (++count == 8)
200                                 break;
201                 }
202         }
203
204         if (rates_len > count) {
205                 pos = skb_put(skb, rates_len - count + 2);
206                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
207                 *pos++ = rates_len - count;
208
209                 for (i++; i < sband->n_bitrates; i++) {
210                         if (BIT(i) & rates) {
211                                 int rate = sband->bitrates[i].bitrate;
212                                 *pos++ = (u8) (rate / 5);
213                         }
214                 }
215         }
216
217         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
218                 /* 1. power capabilities */
219                 pos = skb_put(skb, 4);
220                 *pos++ = WLAN_EID_PWR_CAPABILITY;
221                 *pos++ = 2;
222                 *pos++ = 0; /* min tx power */
223                 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
224
225                 /* 2. supported channels */
226                 /* TODO: get this in reg domain format */
227                 pos = skb_put(skb, 2 * sband->n_channels + 2);
228                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
229                 *pos++ = 2 * sband->n_channels;
230                 for (i = 0; i < sband->n_channels; i++) {
231                         *pos++ = ieee80211_frequency_to_channel(
232                                         sband->channels[i].center_freq);
233                         *pos++ = 1; /* one channel in the subband*/
234                 }
235         }
236
237         if (wk->ie_len && wk->ie) {
238                 pos = skb_put(skb, wk->ie_len);
239                 memcpy(pos, wk->ie, wk->ie_len);
240         }
241
242         if (wk->assoc.wmm_used && local->hw.queues >= 4) {
243                 pos = skb_put(skb, 9);
244                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
245                 *pos++ = 7; /* len */
246                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
247                 *pos++ = 0x50;
248                 *pos++ = 0xf2;
249                 *pos++ = 2; /* WME */
250                 *pos++ = 0; /* WME info */
251                 *pos++ = 1; /* WME ver */
252                 *pos++ = 0;
253         }
254
255         /* wmm support is a must to HT */
256         /*
257          * IEEE802.11n does not allow TKIP/WEP as pairwise
258          * ciphers in HT mode. We still associate in non-ht
259          * mode (11a/b/g) if any one of these ciphers is
260          * configured as pairwise.
261          */
262         if (wk->assoc.use_11n && wk->assoc.wmm_used &&
263             (local->hw.queues >= 4) &&
264             sband->ht_cap.ht_supported &&
265             (ht_ie = wk->assoc.ht_information_ie) &&
266             ht_ie[1] >= sizeof(struct ieee80211_ht_info)) {
267                 struct ieee80211_ht_info *ht_info =
268                         (struct ieee80211_ht_info *)(ht_ie + 2);
269                 u16 cap = sband->ht_cap.cap;
270                 __le16 tmp;
271                 u32 flags = local->hw.conf.channel->flags;
272
273                 /* determine capability flags */
274
275                 if (ieee80211_disable_40mhz_24ghz &&
276                     sband->band == IEEE80211_BAND_2GHZ) {
277                         cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
278                         cap &= ~IEEE80211_HT_CAP_SGI_40;
279                 }
280
281                 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
282                 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
283                         if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
284                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
285                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
286                         }
287                         break;
288                 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
289                         if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
290                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
291                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
292                         }
293                         break;
294                 }
295
296                 /* set SM PS mode properly */
297                 cap &= ~IEEE80211_HT_CAP_SM_PS;
298                 switch (wk->assoc.smps) {
299                 case IEEE80211_SMPS_AUTOMATIC:
300                 case IEEE80211_SMPS_NUM_MODES:
301                         WARN_ON(1);
302                 case IEEE80211_SMPS_OFF:
303                         cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
304                                 IEEE80211_HT_CAP_SM_PS_SHIFT;
305                         break;
306                 case IEEE80211_SMPS_STATIC:
307                         cap |= WLAN_HT_CAP_SM_PS_STATIC <<
308                                 IEEE80211_HT_CAP_SM_PS_SHIFT;
309                         break;
310                 case IEEE80211_SMPS_DYNAMIC:
311                         cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
312                                 IEEE80211_HT_CAP_SM_PS_SHIFT;
313                         break;
314                 }
315
316                 /* reserve and fill IE */
317
318                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
319                 *pos++ = WLAN_EID_HT_CAPABILITY;
320                 *pos++ = sizeof(struct ieee80211_ht_cap);
321                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
322
323                 /* capability flags */
324                 tmp = cpu_to_le16(cap);
325                 memcpy(pos, &tmp, sizeof(u16));
326                 pos += sizeof(u16);
327
328                 /* AMPDU parameters */
329                 *pos++ = sband->ht_cap.ampdu_factor |
330                          (sband->ht_cap.ampdu_density <<
331                                 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
332
333                 /* MCS set */
334                 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
335                 pos += sizeof(sband->ht_cap.mcs);
336
337                 /* extended capabilities */
338                 pos += sizeof(__le16);
339
340                 /* BF capabilities */
341                 pos += sizeof(__le32);
342
343                 /* antenna selection */
344                 pos += sizeof(u8);
345         }
346
347         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
348         ieee80211_tx_skb(sdata, skb);
349 }
350
351 static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
352                                       struct ieee80211_work *wk)
353 {
354         struct cfg80211_bss *cbss;
355         u16 capa_val = WLAN_CAPABILITY_ESS;
356
357         if (wk->probe_auth.privacy)
358                 capa_val |= WLAN_CAPABILITY_PRIVACY;
359
360         cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
361                                 wk->probe_auth.ssid, wk->probe_auth.ssid_len,
362                                 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
363                                 capa_val);
364         if (!cbss)
365                 return;
366
367         cfg80211_unlink_bss(local->hw.wiphy, cbss);
368         cfg80211_put_bss(cbss);
369 }
370
371 static enum work_action __must_check
372 ieee80211_direct_probe(struct ieee80211_work *wk)
373 {
374         struct ieee80211_sub_if_data *sdata = wk->sdata;
375         struct ieee80211_local *local = sdata->local;
376
377         wk->probe_auth.tries++;
378         if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
379                 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
380                        sdata->name, wk->filter_ta);
381
382                 /*
383                  * Most likely AP is not in the range so remove the
384                  * bss struct for that AP.
385                  */
386                 ieee80211_remove_auth_bss(local, wk);
387
388                 /*
389                  * We might have a pending scan which had no chance to run yet
390                  * due to work needing to be done. Hence, queue the STAs work
391                  * again for that.
392                  */
393                 ieee80211_queue_work(&local->hw, &local->work_work);
394                 return WORK_ACT_TIMEOUT;
395         }
396
397         printk(KERN_DEBUG "%s: direct probe to AP %pM (try %d)\n",
398                         sdata->name, wk->filter_ta, wk->probe_auth.tries);
399
400         /*
401          * Direct probe is sent to broadcast address as some APs
402          * will not answer to direct packet in unassociated state.
403          */
404         ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
405                                  wk->probe_auth.ssid_len, NULL, 0);
406
407         wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
408         run_again(local, wk->timeout);
409
410         return WORK_ACT_NONE;
411 }
412
413
414 static enum work_action __must_check
415 ieee80211_authenticate(struct ieee80211_work *wk)
416 {
417         struct ieee80211_sub_if_data *sdata = wk->sdata;
418         struct ieee80211_local *local = sdata->local;
419
420         wk->probe_auth.tries++;
421         if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
422                 printk(KERN_DEBUG "%s: authentication with AP %pM"
423                        " timed out\n", sdata->name, wk->filter_ta);
424
425                 /*
426                  * Most likely AP is not in the range so remove the
427                  * bss struct for that AP.
428                  */
429                 ieee80211_remove_auth_bss(local, wk);
430
431                 /*
432                  * We might have a pending scan which had no chance to run yet
433                  * due to work needing to be done. Hence, queue the STAs work
434                  * again for that.
435                  */
436                 ieee80211_queue_work(&local->hw, &local->work_work);
437                 return WORK_ACT_TIMEOUT;
438         }
439
440         printk(KERN_DEBUG "%s: authenticate with AP %pM (try %d)\n",
441                sdata->name, wk->filter_ta, wk->probe_auth.tries);
442
443         ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
444                             wk->ie_len, wk->filter_ta, NULL, 0, 0);
445         wk->probe_auth.transaction = 2;
446
447         wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
448         run_again(local, wk->timeout);
449
450         return WORK_ACT_NONE;
451 }
452
453 static enum work_action __must_check
454 ieee80211_associate(struct ieee80211_work *wk)
455 {
456         struct ieee80211_sub_if_data *sdata = wk->sdata;
457         struct ieee80211_local *local = sdata->local;
458
459         wk->assoc.tries++;
460         if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
461                 printk(KERN_DEBUG "%s: association with AP %pM"
462                        " timed out\n",
463                        sdata->name, wk->filter_ta);
464
465                 /*
466                  * Most likely AP is not in the range so remove the
467                  * bss struct for that AP.
468                  */
469                 if (wk->assoc.bss)
470                         cfg80211_unlink_bss(local->hw.wiphy,
471                                             &wk->assoc.bss->cbss);
472
473                 /*
474                  * We might have a pending scan which had no chance to run yet
475                  * due to work needing to be done. Hence, queue the STAs work
476                  * again for that.
477                  */
478                 ieee80211_queue_work(&local->hw, &local->work_work);
479                 return WORK_ACT_TIMEOUT;
480         }
481
482         printk(KERN_DEBUG "%s: associate with AP %pM (try %d)\n",
483                sdata->name, wk->filter_ta, wk->assoc.tries);
484         ieee80211_send_assoc(sdata, wk);
485
486         wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
487         run_again(local, wk->timeout);
488
489         return WORK_ACT_NONE;
490 }
491
492 static void ieee80211_auth_challenge(struct ieee80211_work *wk,
493                                      struct ieee80211_mgmt *mgmt,
494                                      size_t len)
495 {
496         struct ieee80211_sub_if_data *sdata = wk->sdata;
497         u8 *pos;
498         struct ieee802_11_elems elems;
499
500         pos = mgmt->u.auth.variable;
501         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
502         if (!elems.challenge)
503                 return;
504         ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
505                             elems.challenge - 2, elems.challenge_len + 2,
506                             wk->filter_ta, wk->probe_auth.key,
507                             wk->probe_auth.key_len, wk->probe_auth.key_idx);
508         wk->probe_auth.transaction = 4;
509 }
510
511 static enum work_action __must_check
512 ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
513                        struct ieee80211_mgmt *mgmt, size_t len)
514 {
515         u16 auth_alg, auth_transaction, status_code;
516
517         if (wk->type != IEEE80211_WORK_AUTH)
518                 return WORK_ACT_NONE;
519
520         if (len < 24 + 6)
521                 return WORK_ACT_NONE;
522
523         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
524         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
525         status_code = le16_to_cpu(mgmt->u.auth.status_code);
526
527         if (auth_alg != wk->probe_auth.algorithm ||
528             auth_transaction != wk->probe_auth.transaction)
529                 return WORK_ACT_NONE;
530
531         if (status_code != WLAN_STATUS_SUCCESS) {
532                 printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
533                        wk->sdata->name, mgmt->sa, status_code);
534                 return WORK_ACT_DONE;
535         }
536
537         switch (wk->probe_auth.algorithm) {
538         case WLAN_AUTH_OPEN:
539         case WLAN_AUTH_LEAP:
540         case WLAN_AUTH_FT:
541                 break;
542         case WLAN_AUTH_SHARED_KEY:
543                 if (wk->probe_auth.transaction != 4) {
544                         ieee80211_auth_challenge(wk, mgmt, len);
545                         /* need another frame */
546                         return WORK_ACT_NONE;
547                 }
548                 break;
549         default:
550                 WARN_ON(1);
551                 return WORK_ACT_NONE;
552         }
553
554         printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
555         return WORK_ACT_DONE;
556 }
557
558 static enum work_action __must_check
559 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
560                              struct ieee80211_mgmt *mgmt, size_t len,
561                              bool reassoc)
562 {
563         struct ieee80211_sub_if_data *sdata = wk->sdata;
564         struct ieee80211_local *local = sdata->local;
565         u16 capab_info, status_code, aid;
566         struct ieee802_11_elems elems;
567         u8 *pos;
568
569         /*
570          * AssocResp and ReassocResp have identical structure, so process both
571          * of them in this function.
572          */
573
574         if (len < 24 + 6)
575                 return WORK_ACT_NONE;
576
577         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
578         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
579         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
580
581         printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
582                "status=%d aid=%d)\n",
583                sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
584                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
585
586         pos = mgmt->u.assoc_resp.variable;
587         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
588
589         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
590             elems.timeout_int && elems.timeout_int_len == 5 &&
591             elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
592                 u32 tu, ms;
593                 tu = get_unaligned_le32(elems.timeout_int + 1);
594                 ms = tu * 1024 / 1000;
595                 printk(KERN_DEBUG "%s: AP rejected association temporarily; "
596                        "comeback duration %u TU (%u ms)\n",
597                        sdata->name, tu, ms);
598                 wk->timeout = jiffies + msecs_to_jiffies(ms);
599                 if (ms > IEEE80211_ASSOC_TIMEOUT)
600                         run_again(local, wk->timeout);
601                 return WORK_ACT_NONE;
602         }
603
604         if (status_code != WLAN_STATUS_SUCCESS)
605                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
606                        sdata->name, status_code);
607         else
608                 printk(KERN_DEBUG "%s: associated\n", sdata->name);
609
610         return WORK_ACT_DONE;
611 }
612
613 static enum work_action __must_check
614 ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
615                              struct ieee80211_mgmt *mgmt, size_t len,
616                              struct ieee80211_rx_status *rx_status)
617 {
618         struct ieee80211_sub_if_data *sdata = wk->sdata;
619         struct ieee80211_local *local = sdata->local;
620         size_t baselen;
621
622         ASSERT_WORK_MTX(local);
623
624         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
625         if (baselen > len)
626                 return WORK_ACT_NONE;
627
628         printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
629         return WORK_ACT_DONE;
630 }
631
632 static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
633                                           struct sk_buff *skb)
634 {
635         struct ieee80211_rx_status *rx_status;
636         struct ieee80211_mgmt *mgmt;
637         struct ieee80211_work *wk;
638         enum work_action rma = WORK_ACT_NONE;
639         u16 fc;
640
641         rx_status = (struct ieee80211_rx_status *) skb->cb;
642         mgmt = (struct ieee80211_mgmt *) skb->data;
643         fc = le16_to_cpu(mgmt->frame_control);
644
645         mutex_lock(&local->work_mtx);
646
647         list_for_each_entry(wk, &local->work_list, list) {
648                 const u8 *bssid = NULL;
649
650                 switch (wk->type) {
651                 case IEEE80211_WORK_DIRECT_PROBE:
652                 case IEEE80211_WORK_AUTH:
653                 case IEEE80211_WORK_ASSOC:
654                         bssid = wk->filter_ta;
655                         break;
656                 default:
657                         continue;
658                 }
659
660                 /*
661                  * Before queuing, we already verified mgmt->sa,
662                  * so this is needed just for matching.
663                  */
664                 if (compare_ether_addr(bssid, mgmt->bssid))
665                         continue;
666
667                 switch (fc & IEEE80211_FCTL_STYPE) {
668                 case IEEE80211_STYPE_PROBE_RESP:
669                         rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
670                                                            rx_status);
671                         break;
672                 case IEEE80211_STYPE_AUTH:
673                         rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
674                         break;
675                 case IEEE80211_STYPE_ASSOC_RESP:
676                         rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
677                                                            skb->len, false);
678                         break;
679                 case IEEE80211_STYPE_REASSOC_RESP:
680                         rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
681                                                            skb->len, true);
682                         break;
683                 default:
684                         WARN_ON(1);
685                 }
686                 /*
687                  * We've processed this frame for that work, so it can't
688                  * belong to another work struct.
689                  * NB: this is also required for correctness for 'rma'!
690                  */
691                 break;
692         }
693
694         switch (rma) {
695         case WORK_ACT_NONE:
696                 break;
697         case WORK_ACT_DONE:
698                 list_del_rcu(&wk->list);
699                 break;
700         default:
701                 WARN(1, "unexpected: %d", rma);
702         }
703
704         mutex_unlock(&local->work_mtx);
705
706         if (rma != WORK_ACT_DONE)
707                 goto out;
708
709         switch (wk->done(wk, skb)) {
710         case WORK_DONE_DESTROY:
711                 free_work(wk);
712                 break;
713         case WORK_DONE_REQUEUE:
714                 synchronize_rcu();
715                 wk->timeout = jiffies; /* run again directly */
716                 mutex_lock(&local->work_mtx);
717                 list_add_tail(&wk->list, &local->work_list);
718                 mutex_unlock(&local->work_mtx);
719         }
720
721  out:
722         kfree_skb(skb);
723 }
724
725 static void ieee80211_work_timer(unsigned long data)
726 {
727         struct ieee80211_local *local = (void *) data;
728
729         if (local->quiescing)
730                 return;
731
732         ieee80211_queue_work(&local->hw, &local->work_work);
733 }
734
735 static void ieee80211_work_work(struct work_struct *work)
736 {
737         struct ieee80211_local *local =
738                 container_of(work, struct ieee80211_local, work_work);
739         struct sk_buff *skb;
740         struct ieee80211_work *wk, *tmp;
741         LIST_HEAD(free_work);
742         enum work_action rma;
743
744         if (local->scanning)
745                 return;
746
747         /*
748          * ieee80211_queue_work() should have picked up most cases,
749          * here we'll pick the the rest.
750          */
751         if (WARN(local->suspended, "work scheduled while going to suspend\n"))
752                 return;
753
754         /* first process frames to avoid timing out while a frame is pending */
755         while ((skb = skb_dequeue(&local->work_skb_queue)))
756                 ieee80211_work_rx_queued_mgmt(local, skb);
757
758         ieee80211_recalc_idle(local);
759
760         mutex_lock(&local->work_mtx);
761
762         list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
763                 if (time_is_after_jiffies(wk->timeout)) {
764                         /*
765                          * This work item isn't supposed to be worked on
766                          * right now, but take care to adjust the timer
767                          * properly.
768                          */
769                         run_again(local, wk->timeout);
770                         continue;
771                 }
772
773                 switch (wk->type) {
774                 default:
775                         WARN_ON(1);
776                         /* nothing */
777                         rma = WORK_ACT_NONE;
778                         break;
779                 case IEEE80211_WORK_DIRECT_PROBE:
780                         rma = ieee80211_direct_probe(wk);
781                         break;
782                 case IEEE80211_WORK_AUTH:
783                         rma = ieee80211_authenticate(wk);
784                         break;
785                 case IEEE80211_WORK_ASSOC:
786                         rma = ieee80211_associate(wk);
787                         break;
788                 }
789
790                 switch (rma) {
791                 case WORK_ACT_NONE:
792                         /* no action required */
793                         break;
794                 case WORK_ACT_TIMEOUT:
795                         list_del_rcu(&wk->list);
796                         synchronize_rcu();
797                         list_add(&wk->list, &free_work);
798                         break;
799                 default:
800                         WARN(1, "unexpected: %d", rma);
801                 }
802         }
803
804         if (list_empty(&local->work_list) && local->scan_req)
805                 ieee80211_queue_delayed_work(&local->hw,
806                                              &local->scan_work,
807                                              round_jiffies_relative(0));
808
809         mutex_unlock(&local->work_mtx);
810
811         list_for_each_entry_safe(wk, tmp, &free_work, list) {
812                 wk->done(wk, NULL);
813                 list_del(&wk->list);
814                 kfree(wk);
815         }
816 }
817
818 void ieee80211_add_work(struct ieee80211_work *wk)
819 {
820         struct ieee80211_local *local;
821
822         if (WARN_ON(!wk->chan))
823                 return;
824
825         if (WARN_ON(!wk->sdata))
826                 return;
827
828         if (WARN_ON(!wk->done))
829                 return;
830
831         wk->timeout = jiffies;
832
833         local = wk->sdata->local;
834         mutex_lock(&local->work_mtx);
835         list_add_tail(&wk->list, &local->work_list);
836         mutex_unlock(&local->work_mtx);
837
838         ieee80211_queue_work(&local->hw, &local->work_work);
839 }
840
841 void ieee80211_work_init(struct ieee80211_local *local)
842 {
843         mutex_init(&local->work_mtx);
844         INIT_LIST_HEAD(&local->work_list);
845         setup_timer(&local->work_timer, ieee80211_work_timer,
846                     (unsigned long)local);
847         INIT_WORK(&local->work_work, ieee80211_work_work);
848         skb_queue_head_init(&local->work_skb_queue);
849 }
850
851 void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
852 {
853         struct ieee80211_local *local = sdata->local;
854         struct ieee80211_work *wk, *tmp;
855
856         mutex_lock(&local->work_mtx);
857         list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
858                 if (wk->sdata != sdata)
859                         continue;
860                 list_del(&wk->list);
861                 free_work(wk);
862         }
863         mutex_unlock(&local->work_mtx);
864 }
865
866 ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
867                                            struct sk_buff *skb)
868 {
869         struct ieee80211_local *local = sdata->local;
870         struct ieee80211_mgmt *mgmt;
871         struct ieee80211_work *wk;
872         u16 fc;
873
874         if (skb->len < 24)
875                 return RX_DROP_MONITOR;
876
877         mgmt = (struct ieee80211_mgmt *) skb->data;
878         fc = le16_to_cpu(mgmt->frame_control);
879
880         list_for_each_entry_rcu(wk, &local->work_list, list) {
881                 if (sdata != wk->sdata)
882                         continue;
883                 if (compare_ether_addr(wk->filter_ta, mgmt->sa))
884                         continue;
885                 if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
886                         continue;
887
888                 switch (fc & IEEE80211_FCTL_STYPE) {
889                 case IEEE80211_STYPE_AUTH:
890                 case IEEE80211_STYPE_PROBE_RESP:
891                 case IEEE80211_STYPE_ASSOC_RESP:
892                 case IEEE80211_STYPE_REASSOC_RESP:
893                 case IEEE80211_STYPE_DEAUTH:
894                 case IEEE80211_STYPE_DISASSOC:
895                         skb_queue_tail(&local->work_skb_queue, skb);
896                         ieee80211_queue_work(&local->hw, &local->work_work);
897                         return RX_QUEUED;
898                 }
899         }
900
901         return RX_CONTINUE;
902 }