nl80211: Generate deauth/disassoc event for locally generated frames
[safe/jmp/linux-2.6] / net / mac80211 / mlme.c
1 /*
2  * BSS client mode implementation
3  * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4  * Copyright 2004, Instant802 Networks, Inc.
5  * Copyright 2005, Devicescape Software, Inc.
6  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
7  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/rtnetlink.h>
20 #include <net/mac80211.h>
21 #include <asm/unaligned.h>
22
23 #include "ieee80211_i.h"
24 #include "rate.h"
25 #include "led.h"
26
27 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
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_MONITORING_INTERVAL (2 * HZ)
33 #define IEEE80211_PROBE_IDLE_TIME (60 * HZ)
34 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
35
36 /* utils */
37 static int ecw2cw(int ecw)
38 {
39         return (1 << ecw) - 1;
40 }
41
42 static u8 *ieee80211_bss_get_ie(struct ieee80211_bss *bss, u8 ie)
43 {
44         u8 *end, *pos;
45
46         pos = bss->cbss.information_elements;
47         if (pos == NULL)
48                 return NULL;
49         end = pos + bss->cbss.len_information_elements;
50
51         while (pos + 1 < end) {
52                 if (pos + 2 + pos[1] > end)
53                         break;
54                 if (pos[0] == ie)
55                         return pos;
56                 pos += 2 + pos[1];
57         }
58
59         return NULL;
60 }
61
62 static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
63                                       struct ieee80211_supported_band *sband,
64                                       u32 *rates)
65 {
66         int i, j, count;
67         *rates = 0;
68         count = 0;
69         for (i = 0; i < bss->supp_rates_len; i++) {
70                 int rate = (bss->supp_rates[i] & 0x7F) * 5;
71
72                 for (j = 0; j < sband->n_bitrates; j++)
73                         if (sband->bitrates[j].bitrate == rate) {
74                                 *rates |= BIT(j);
75                                 count++;
76                                 break;
77                         }
78         }
79
80         return count;
81 }
82
83 /* frame sending functions */
84
85 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
86 {
87         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
88         struct ieee80211_local *local = sdata->local;
89         struct sk_buff *skb;
90         struct ieee80211_mgmt *mgmt;
91         u8 *pos, *ies, *ht_ie;
92         int i, len, count, rates_len, supp_rates_len;
93         u16 capab;
94         struct ieee80211_bss *bss;
95         int wmm = 0;
96         struct ieee80211_supported_band *sband;
97         u32 rates = 0;
98
99         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
100                             sizeof(*mgmt) + 200 + ifmgd->extra_ie_len +
101                             ifmgd->ssid_len);
102         if (!skb) {
103                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
104                        "frame\n", sdata->dev->name);
105                 return;
106         }
107         skb_reserve(skb, local->hw.extra_tx_headroom);
108
109         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
110
111         capab = ifmgd->capab;
112
113         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
114                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
115                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
116                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
117                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
118         }
119
120         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
121                                    local->hw.conf.channel->center_freq,
122                                    ifmgd->ssid, ifmgd->ssid_len);
123         if (bss) {
124                 if (bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
125                         capab |= WLAN_CAPABILITY_PRIVACY;
126                 if (bss->wmm_used)
127                         wmm = 1;
128
129                 /* get all rates supported by the device and the AP as
130                  * some APs don't like getting a superset of their rates
131                  * in the association request (e.g. D-Link DAP 1353 in
132                  * b-only mode) */
133                 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
134
135                 if ((bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
136                     (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
137                         capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
138
139                 ieee80211_rx_bss_put(local, bss);
140         } else {
141                 rates = ~0;
142                 rates_len = sband->n_bitrates;
143         }
144
145         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
146         memset(mgmt, 0, 24);
147         memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
148         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
149         memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
150
151         if (ifmgd->flags & IEEE80211_STA_PREV_BSSID_SET) {
152                 skb_put(skb, 10);
153                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
154                                                   IEEE80211_STYPE_REASSOC_REQ);
155                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
156                 mgmt->u.reassoc_req.listen_interval =
157                                 cpu_to_le16(local->hw.conf.listen_interval);
158                 memcpy(mgmt->u.reassoc_req.current_ap, ifmgd->prev_bssid,
159                        ETH_ALEN);
160         } else {
161                 skb_put(skb, 4);
162                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
163                                                   IEEE80211_STYPE_ASSOC_REQ);
164                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
165                 mgmt->u.assoc_req.listen_interval =
166                                 cpu_to_le16(local->hw.conf.listen_interval);
167         }
168
169         /* SSID */
170         ies = pos = skb_put(skb, 2 + ifmgd->ssid_len);
171         *pos++ = WLAN_EID_SSID;
172         *pos++ = ifmgd->ssid_len;
173         memcpy(pos, ifmgd->ssid, ifmgd->ssid_len);
174
175         /* add all rates which were marked to be used above */
176         supp_rates_len = rates_len;
177         if (supp_rates_len > 8)
178                 supp_rates_len = 8;
179
180         len = sband->n_bitrates;
181         pos = skb_put(skb, supp_rates_len + 2);
182         *pos++ = WLAN_EID_SUPP_RATES;
183         *pos++ = supp_rates_len;
184
185         count = 0;
186         for (i = 0; i < sband->n_bitrates; i++) {
187                 if (BIT(i) & rates) {
188                         int rate = sband->bitrates[i].bitrate;
189                         *pos++ = (u8) (rate / 5);
190                         if (++count == 8)
191                                 break;
192                 }
193         }
194
195         if (rates_len > count) {
196                 pos = skb_put(skb, rates_len - count + 2);
197                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
198                 *pos++ = rates_len - count;
199
200                 for (i++; i < sband->n_bitrates; i++) {
201                         if (BIT(i) & rates) {
202                                 int rate = sband->bitrates[i].bitrate;
203                                 *pos++ = (u8) (rate / 5);
204                         }
205                 }
206         }
207
208         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
209                 /* 1. power capabilities */
210                 pos = skb_put(skb, 4);
211                 *pos++ = WLAN_EID_PWR_CAPABILITY;
212                 *pos++ = 2;
213                 *pos++ = 0; /* min tx power */
214                 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
215
216                 /* 2. supported channels */
217                 /* TODO: get this in reg domain format */
218                 pos = skb_put(skb, 2 * sband->n_channels + 2);
219                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
220                 *pos++ = 2 * sband->n_channels;
221                 for (i = 0; i < sband->n_channels; i++) {
222                         *pos++ = ieee80211_frequency_to_channel(
223                                         sband->channels[i].center_freq);
224                         *pos++ = 1; /* one channel in the subband*/
225                 }
226         }
227
228         if (ifmgd->extra_ie) {
229                 pos = skb_put(skb, ifmgd->extra_ie_len);
230                 memcpy(pos, ifmgd->extra_ie, ifmgd->extra_ie_len);
231         }
232
233         if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
234                 pos = skb_put(skb, 9);
235                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
236                 *pos++ = 7; /* len */
237                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
238                 *pos++ = 0x50;
239                 *pos++ = 0xf2;
240                 *pos++ = 2; /* WME */
241                 *pos++ = 0; /* WME info */
242                 *pos++ = 1; /* WME ver */
243                 *pos++ = 0;
244         }
245
246         /* wmm support is a must to HT */
247         /*
248          * IEEE802.11n does not allow TKIP/WEP as pairwise
249          * ciphers in HT mode. We still associate in non-ht
250          * mode (11a/b/g) if any one of these ciphers is
251          * configured as pairwise.
252          */
253         if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
254             sband->ht_cap.ht_supported &&
255             (ht_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_INFORMATION)) &&
256             ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
257             (!(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))) {
258                 struct ieee80211_ht_info *ht_info =
259                         (struct ieee80211_ht_info *)(ht_ie + 2);
260                 u16 cap = sband->ht_cap.cap;
261                 __le16 tmp;
262                 u32 flags = local->hw.conf.channel->flags;
263
264                 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
265                 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
266                         if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
267                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
268                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
269                         }
270                         break;
271                 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
272                         if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
273                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
274                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
275                         }
276                         break;
277                 }
278
279                 tmp = cpu_to_le16(cap);
280                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
281                 *pos++ = WLAN_EID_HT_CAPABILITY;
282                 *pos++ = sizeof(struct ieee80211_ht_cap);
283                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
284                 memcpy(pos, &tmp, sizeof(u16));
285                 pos += sizeof(u16);
286                 /* TODO: needs a define here for << 2 */
287                 *pos++ = sband->ht_cap.ampdu_factor |
288                          (sband->ht_cap.ampdu_density << 2);
289                 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
290         }
291
292         kfree(ifmgd->assocreq_ies);
293         ifmgd->assocreq_ies_len = (skb->data + skb->len) - ies;
294         ifmgd->assocreq_ies = kmalloc(ifmgd->assocreq_ies_len, GFP_KERNEL);
295         if (ifmgd->assocreq_ies)
296                 memcpy(ifmgd->assocreq_ies, ies, ifmgd->assocreq_ies_len);
297
298         ieee80211_tx_skb(sdata, skb, 0);
299 }
300
301
302 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
303                                            u16 stype, u16 reason)
304 {
305         struct ieee80211_local *local = sdata->local;
306         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
307         struct sk_buff *skb;
308         struct ieee80211_mgmt *mgmt;
309
310         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
311         if (!skb) {
312                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
313                        "deauth/disassoc frame\n", sdata->dev->name);
314                 return;
315         }
316         skb_reserve(skb, local->hw.extra_tx_headroom);
317
318         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
319         memset(mgmt, 0, 24);
320         memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
321         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
322         memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
323         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
324         skb_put(skb, 2);
325         /* u.deauth.reason_code == u.disassoc.reason_code */
326         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
327
328         if (stype == IEEE80211_STYPE_DEAUTH)
329                 cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, skb->len);
330         else
331                 cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, skb->len);
332         ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
333 }
334
335 void ieee80211_send_pspoll(struct ieee80211_local *local,
336                            struct ieee80211_sub_if_data *sdata)
337 {
338         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
339         struct ieee80211_pspoll *pspoll;
340         struct sk_buff *skb;
341         u16 fc;
342
343         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
344         if (!skb) {
345                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
346                        "pspoll frame\n", sdata->dev->name);
347                 return;
348         }
349         skb_reserve(skb, local->hw.extra_tx_headroom);
350
351         pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
352         memset(pspoll, 0, sizeof(*pspoll));
353         fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
354         pspoll->frame_control = cpu_to_le16(fc);
355         pspoll->aid = cpu_to_le16(ifmgd->aid);
356
357         /* aid in PS-Poll has its two MSBs each set to 1 */
358         pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
359
360         memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
361         memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
362
363         ieee80211_tx_skb(sdata, skb, 0);
364 }
365
366 /* MLME */
367 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
368                                      struct ieee80211_if_managed *ifmgd,
369                                      u8 *wmm_param, size_t wmm_param_len)
370 {
371         struct ieee80211_tx_queue_params params;
372         size_t left;
373         int count;
374         u8 *pos;
375
376         if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
377                 return;
378
379         if (!wmm_param)
380                 return;
381
382         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
383                 return;
384         count = wmm_param[6] & 0x0f;
385         if (count == ifmgd->wmm_last_param_set)
386                 return;
387         ifmgd->wmm_last_param_set = count;
388
389         pos = wmm_param + 8;
390         left = wmm_param_len - 8;
391
392         memset(&params, 0, sizeof(params));
393
394         local->wmm_acm = 0;
395         for (; left >= 4; left -= 4, pos += 4) {
396                 int aci = (pos[0] >> 5) & 0x03;
397                 int acm = (pos[0] >> 4) & 0x01;
398                 int queue;
399
400                 switch (aci) {
401                 case 1: /* AC_BK */
402                         queue = 3;
403                         if (acm)
404                                 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
405                         break;
406                 case 2: /* AC_VI */
407                         queue = 1;
408                         if (acm)
409                                 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
410                         break;
411                 case 3: /* AC_VO */
412                         queue = 0;
413                         if (acm)
414                                 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
415                         break;
416                 case 0: /* AC_BE */
417                 default:
418                         queue = 2;
419                         if (acm)
420                                 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
421                         break;
422                 }
423
424                 params.aifs = pos[0] & 0x0f;
425                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
426                 params.cw_min = ecw2cw(pos[1] & 0x0f);
427                 params.txop = get_unaligned_le16(pos + 2);
428 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
429                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
430                        "cWmin=%d cWmax=%d txop=%d\n",
431                        local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
432                        params.cw_max, params.txop);
433 #endif
434                 if (local->ops->conf_tx &&
435                     local->ops->conf_tx(local_to_hw(local), queue, &params)) {
436                         printk(KERN_DEBUG "%s: failed to set TX queue "
437                                "parameters for queue %d\n", local->mdev->name, queue);
438                 }
439         }
440 }
441
442 static bool ieee80211_check_tim(struct ieee802_11_elems *elems, u16 aid)
443 {
444         u8 mask;
445         u8 index, indexn1, indexn2;
446         struct ieee80211_tim_ie *tim = (struct ieee80211_tim_ie *) elems->tim;
447
448         if (unlikely(!tim || elems->tim_len < 4))
449                 return false;
450
451         aid &= 0x3fff;
452         index = aid / 8;
453         mask  = 1 << (aid & 7);
454
455         indexn1 = tim->bitmap_ctrl & 0xfe;
456         indexn2 = elems->tim_len + indexn1 - 4;
457
458         if (index < indexn1 || index > indexn2)
459                 return false;
460
461         index -= indexn1;
462
463         return !!(tim->virtual_map[index] & mask);
464 }
465
466 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
467                                            u16 capab, bool erp_valid, u8 erp)
468 {
469         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
470 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
471         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
472 #endif
473         u32 changed = 0;
474         bool use_protection;
475         bool use_short_preamble;
476         bool use_short_slot;
477
478         if (erp_valid) {
479                 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
480                 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
481         } else {
482                 use_protection = false;
483                 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
484         }
485
486         use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
487
488         if (use_protection != bss_conf->use_cts_prot) {
489 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
490                 if (net_ratelimit()) {
491                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
492                                sdata->dev->name,
493                                use_protection ? "enabled" : "disabled",
494                                ifmgd->bssid);
495                 }
496 #endif
497                 bss_conf->use_cts_prot = use_protection;
498                 changed |= BSS_CHANGED_ERP_CTS_PROT;
499         }
500
501         if (use_short_preamble != bss_conf->use_short_preamble) {
502 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
503                 if (net_ratelimit()) {
504                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
505                                " (BSSID=%pM)\n",
506                                sdata->dev->name,
507                                use_short_preamble ? "short" : "long",
508                                ifmgd->bssid);
509                 }
510 #endif
511                 bss_conf->use_short_preamble = use_short_preamble;
512                 changed |= BSS_CHANGED_ERP_PREAMBLE;
513         }
514
515         if (use_short_slot != bss_conf->use_short_slot) {
516 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
517                 if (net_ratelimit()) {
518                         printk(KERN_DEBUG "%s: switched to %s slot time"
519                                " (BSSID=%pM)\n",
520                                sdata->dev->name,
521                                use_short_slot ? "short" : "long",
522                                ifmgd->bssid);
523                 }
524 #endif
525                 bss_conf->use_short_slot = use_short_slot;
526                 changed |= BSS_CHANGED_ERP_SLOT;
527         }
528
529         return changed;
530 }
531
532 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata)
533 {
534         union iwreq_data wrqu;
535
536         memset(&wrqu, 0, sizeof(wrqu));
537         if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED)
538                 memcpy(wrqu.ap_addr.sa_data, sdata->u.mgd.bssid, ETH_ALEN);
539         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
540         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
541 }
542
543 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata)
544 {
545         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
546         char *buf;
547         size_t len;
548         int i;
549         union iwreq_data wrqu;
550
551         if (!ifmgd->assocreq_ies && !ifmgd->assocresp_ies)
552                 return;
553
554         buf = kmalloc(50 + 2 * (ifmgd->assocreq_ies_len +
555                                 ifmgd->assocresp_ies_len), GFP_KERNEL);
556         if (!buf)
557                 return;
558
559         len = sprintf(buf, "ASSOCINFO(");
560         if (ifmgd->assocreq_ies) {
561                 len += sprintf(buf + len, "ReqIEs=");
562                 for (i = 0; i < ifmgd->assocreq_ies_len; i++) {
563                         len += sprintf(buf + len, "%02x",
564                                        ifmgd->assocreq_ies[i]);
565                 }
566         }
567         if (ifmgd->assocresp_ies) {
568                 if (ifmgd->assocreq_ies)
569                         len += sprintf(buf + len, " ");
570                 len += sprintf(buf + len, "RespIEs=");
571                 for (i = 0; i < ifmgd->assocresp_ies_len; i++) {
572                         len += sprintf(buf + len, "%02x",
573                                        ifmgd->assocresp_ies[i]);
574                 }
575         }
576         len += sprintf(buf + len, ")");
577
578         if (len > IW_CUSTOM_MAX) {
579                 len = sprintf(buf, "ASSOCRESPIE=");
580                 for (i = 0; i < ifmgd->assocresp_ies_len; i++) {
581                         len += sprintf(buf + len, "%02x",
582                                        ifmgd->assocresp_ies[i]);
583                 }
584         }
585
586         if (len <= IW_CUSTOM_MAX) {
587                 memset(&wrqu, 0, sizeof(wrqu));
588                 wrqu.data.length = len;
589                 wireless_send_event(sdata->dev, IWEVCUSTOM, &wrqu, buf);
590         }
591
592         kfree(buf);
593 }
594
595
596 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
597                                      u32 bss_info_changed)
598 {
599         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
600         struct ieee80211_local *local = sdata->local;
601         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
602
603         struct ieee80211_bss *bss;
604
605         bss_info_changed |= BSS_CHANGED_ASSOC;
606         ifmgd->flags |= IEEE80211_STA_ASSOCIATED;
607
608         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
609                                    conf->channel->center_freq,
610                                    ifmgd->ssid, ifmgd->ssid_len);
611         if (bss) {
612                 /* set timing information */
613                 sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
614                 sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
615                 sdata->vif.bss_conf.dtim_period = bss->dtim_period;
616
617                 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
618                         bss->cbss.capability, bss->has_erp_value, bss->erp_value);
619
620                 cfg80211_hold_bss(&bss->cbss);
621
622                 ieee80211_rx_bss_put(local, bss);
623         }
624
625         ifmgd->flags |= IEEE80211_STA_PREV_BSSID_SET;
626         memcpy(ifmgd->prev_bssid, sdata->u.mgd.bssid, ETH_ALEN);
627         ieee80211_sta_send_associnfo(sdata);
628
629         ifmgd->last_probe = jiffies;
630         ieee80211_led_assoc(local, 1);
631
632         sdata->vif.bss_conf.assoc = 1;
633         /*
634          * For now just always ask the driver to update the basic rateset
635          * when we have associated, we aren't checking whether it actually
636          * changed or not.
637          */
638         bss_info_changed |= BSS_CHANGED_BASIC_RATES;
639         ieee80211_bss_info_change_notify(sdata, bss_info_changed);
640
641         if (local->powersave) {
642                 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS) &&
643                     local->hw.conf.dynamic_ps_timeout > 0) {
644                         mod_timer(&local->dynamic_ps_timer, jiffies +
645                                   msecs_to_jiffies(
646                                         local->hw.conf.dynamic_ps_timeout));
647                 } else {
648                         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
649                                 ieee80211_send_nullfunc(local, sdata, 1);
650                         conf->flags |= IEEE80211_CONF_PS;
651                         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
652                 }
653         }
654
655         netif_tx_start_all_queues(sdata->dev);
656         netif_carrier_on(sdata->dev);
657
658         ieee80211_sta_send_apinfo(sdata);
659 }
660
661 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata)
662 {
663         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
664         struct ieee80211_local *local = sdata->local;
665
666         ifmgd->direct_probe_tries++;
667         if (ifmgd->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
668                 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
669                        sdata->dev->name, ifmgd->bssid);
670                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
671                 ieee80211_sta_send_apinfo(sdata);
672
673                 /*
674                  * Most likely AP is not in the range so remove the
675                  * bss information associated to the AP
676                  */
677                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
678                                 sdata->local->hw.conf.channel->center_freq,
679                                 ifmgd->ssid, ifmgd->ssid_len);
680
681                 /*
682                  * We might have a pending scan which had no chance to run yet
683                  * due to state == IEEE80211_STA_MLME_DIRECT_PROBE.
684                  * Hence, queue the STAs work again
685                  */
686                 queue_work(local->hw.workqueue, &ifmgd->work);
687                 return;
688         }
689
690         printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
691                         sdata->dev->name, ifmgd->bssid,
692                         ifmgd->direct_probe_tries);
693
694         ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
695
696         set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifmgd->request);
697
698         /* Direct probe is sent to broadcast address as some APs
699          * will not answer to direct packet in unassociated state.
700          */
701         ieee80211_send_probe_req(sdata, NULL,
702                                  ifmgd->ssid, ifmgd->ssid_len, NULL, 0);
703
704         mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
705 }
706
707
708 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata)
709 {
710         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
711         struct ieee80211_local *local = sdata->local;
712         u8 *ies;
713         size_t ies_len;
714
715         ifmgd->auth_tries++;
716         if (ifmgd->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
717                 printk(KERN_DEBUG "%s: authentication with AP %pM"
718                        " timed out\n",
719                        sdata->dev->name, ifmgd->bssid);
720                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
721                 ieee80211_sta_send_apinfo(sdata);
722                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
723                                 sdata->local->hw.conf.channel->center_freq,
724                                 ifmgd->ssid, ifmgd->ssid_len);
725
726                 /*
727                  * We might have a pending scan which had no chance to run yet
728                  * due to state == IEEE80211_STA_MLME_AUTHENTICATE.
729                  * Hence, queue the STAs work again
730                  */
731                 queue_work(local->hw.workqueue, &ifmgd->work);
732                 return;
733         }
734
735         ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
736         printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
737                sdata->dev->name, ifmgd->bssid);
738
739         if (ifmgd->flags & IEEE80211_STA_EXT_SME) {
740                 ies = ifmgd->sme_auth_ie;
741                 ies_len = ifmgd->sme_auth_ie_len;
742         } else {
743                 ies = NULL;
744                 ies_len = 0;
745         }
746         ieee80211_send_auth(sdata, 1, ifmgd->auth_alg, ies, ies_len,
747                             ifmgd->bssid, 0);
748         ifmgd->auth_transaction = 2;
749
750         mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
751 }
752
753 /*
754  * The disassoc 'reason' argument can be either our own reason
755  * if self disconnected or a reason code from the AP.
756  */
757 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
758                                    bool deauth, bool self_disconnected,
759                                    u16 reason)
760 {
761         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
762         struct ieee80211_local *local = sdata->local;
763         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
764         struct ieee80211_bss *bss;
765         struct sta_info *sta;
766         u32 changed = 0, config_changed = 0;
767
768         rcu_read_lock();
769
770         sta = sta_info_get(local, ifmgd->bssid);
771         if (!sta) {
772                 rcu_read_unlock();
773                 return;
774         }
775
776         if (deauth) {
777                 ifmgd->direct_probe_tries = 0;
778                 ifmgd->auth_tries = 0;
779         }
780         ifmgd->assoc_scan_tries = 0;
781         ifmgd->assoc_tries = 0;
782
783         netif_tx_stop_all_queues(sdata->dev);
784         netif_carrier_off(sdata->dev);
785
786         ieee80211_sta_tear_down_BA_sessions(sta);
787
788         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
789                                    conf->channel->center_freq,
790                                    ifmgd->ssid, ifmgd->ssid_len);
791
792         if (bss) {
793                 cfg80211_unhold_bss(&bss->cbss);
794                 ieee80211_rx_bss_put(local, bss);
795         }
796
797         if (self_disconnected) {
798                 if (deauth)
799                         ieee80211_send_deauth_disassoc(sdata,
800                                 IEEE80211_STYPE_DEAUTH, reason);
801                 else
802                         ieee80211_send_deauth_disassoc(sdata,
803                                 IEEE80211_STYPE_DISASSOC, reason);
804         }
805
806         ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
807         changed |= ieee80211_reset_erp_info(sdata);
808
809         ieee80211_led_assoc(local, 0);
810         changed |= BSS_CHANGED_ASSOC;
811         sdata->vif.bss_conf.assoc = false;
812
813         ieee80211_sta_send_apinfo(sdata);
814
815         if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT) {
816                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
817                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
818                                 sdata->local->hw.conf.channel->center_freq,
819                                 ifmgd->ssid, ifmgd->ssid_len);
820         }
821
822         rcu_read_unlock();
823
824         /* channel(_type) changes are handled by ieee80211_hw_config */
825         local->oper_channel_type = NL80211_CHAN_NO_HT;
826
827         local->power_constr_level = 0;
828
829         del_timer_sync(&local->dynamic_ps_timer);
830         cancel_work_sync(&local->dynamic_ps_enable_work);
831
832         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
833                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
834                 config_changed |= IEEE80211_CONF_CHANGE_PS;
835         }
836
837         ieee80211_hw_config(local, config_changed);
838         ieee80211_bss_info_change_notify(sdata, changed);
839
840         rcu_read_lock();
841
842         sta = sta_info_get(local, ifmgd->bssid);
843         if (!sta) {
844                 rcu_read_unlock();
845                 return;
846         }
847
848         sta_info_unlink(&sta);
849
850         rcu_read_unlock();
851
852         sta_info_destroy(sta);
853 }
854
855 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
856 {
857         if (!sdata || !sdata->default_key ||
858             sdata->default_key->conf.alg != ALG_WEP)
859                 return 0;
860         return 1;
861 }
862
863 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata)
864 {
865         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
866         struct ieee80211_local *local = sdata->local;
867         struct ieee80211_bss *bss;
868         int bss_privacy;
869         int wep_privacy;
870         int privacy_invoked;
871
872         if (!ifmgd || (ifmgd->flags & IEEE80211_STA_EXT_SME))
873                 return 0;
874
875         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
876                                    local->hw.conf.channel->center_freq,
877                                    ifmgd->ssid, ifmgd->ssid_len);
878         if (!bss)
879                 return 0;
880
881         bss_privacy = !!(bss->cbss.capability & WLAN_CAPABILITY_PRIVACY);
882         wep_privacy = !!ieee80211_sta_wep_configured(sdata);
883         privacy_invoked = !!(ifmgd->flags & IEEE80211_STA_PRIVACY_INVOKED);
884
885         ieee80211_rx_bss_put(local, bss);
886
887         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
888                 return 0;
889
890         return 1;
891 }
892
893 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata)
894 {
895         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
896         struct ieee80211_local *local = sdata->local;
897
898         ifmgd->assoc_tries++;
899         if (ifmgd->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
900                 printk(KERN_DEBUG "%s: association with AP %pM"
901                        " timed out\n",
902                        sdata->dev->name, ifmgd->bssid);
903                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
904                 ieee80211_sta_send_apinfo(sdata);
905                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
906                                 sdata->local->hw.conf.channel->center_freq,
907                                 ifmgd->ssid, ifmgd->ssid_len);
908                 /*
909                  * We might have a pending scan which had no chance to run yet
910                  * due to state == IEEE80211_STA_MLME_ASSOCIATE.
911                  * Hence, queue the STAs work again
912                  */
913                 queue_work(local->hw.workqueue, &ifmgd->work);
914                 return;
915         }
916
917         ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
918         printk(KERN_DEBUG "%s: associate with AP %pM\n",
919                sdata->dev->name, ifmgd->bssid);
920         if (ieee80211_privacy_mismatch(sdata)) {
921                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
922                        "mixed-cell disabled - abort association\n", sdata->dev->name);
923                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
924                 return;
925         }
926
927         ieee80211_send_assoc(sdata);
928
929         mod_timer(&ifmgd->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
930 }
931
932 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
933                              struct ieee80211_hdr *hdr)
934 {
935         /*
936          * We can postpone the mgd.timer whenever receiving unicast frames
937          * from AP because we know that the connection is working both ways
938          * at that time. But multicast frames (and hence also beacons) must
939          * be ignored here, because we need to trigger the timer during
940          * data idle periods for sending the periodical probe request to
941          * the AP.
942          */
943         if (!is_multicast_ether_addr(hdr->addr1))
944                 mod_timer(&sdata->u.mgd.timer,
945                           jiffies + IEEE80211_MONITORING_INTERVAL);
946 }
947
948 void ieee80211_beacon_loss_work(struct work_struct *work)
949 {
950         struct ieee80211_sub_if_data *sdata =
951                 container_of(work, struct ieee80211_sub_if_data,
952                              u.mgd.beacon_loss_work);
953         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
954
955 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
956         if (net_ratelimit()) {
957                 printk(KERN_DEBUG "%s: driver reports beacon loss from AP %pM "
958                        "- sending probe request\n", sdata->dev->name,
959                        sdata->u.mgd.bssid);
960         }
961 #endif
962
963         ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
964         ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
965                                  ifmgd->ssid_len, NULL, 0);
966
967         mod_timer(&ifmgd->timer, jiffies + IEEE80211_MONITORING_INTERVAL);
968 }
969
970 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
971 {
972         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
973
974         queue_work(sdata->local->hw.workqueue,
975                    &sdata->u.mgd.beacon_loss_work);
976 }
977 EXPORT_SYMBOL(ieee80211_beacon_loss);
978
979 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata)
980 {
981         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
982         struct ieee80211_local *local = sdata->local;
983         struct sta_info *sta;
984         bool disassoc = false;
985
986         /* TODO: start monitoring current AP signal quality and number of
987          * missed beacons. Scan other channels every now and then and search
988          * for better APs. */
989         /* TODO: remove expired BSSes */
990
991         ifmgd->state = IEEE80211_STA_MLME_ASSOCIATED;
992
993         rcu_read_lock();
994
995         sta = sta_info_get(local, ifmgd->bssid);
996         if (!sta) {
997                 printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
998                        sdata->dev->name, ifmgd->bssid);
999                 disassoc = true;
1000                 goto unlock;
1001         }
1002
1003         if ((ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) &&
1004             time_after(jiffies, sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1005                 printk(KERN_DEBUG "%s: no probe response from AP %pM "
1006                        "- disassociating\n",
1007                        sdata->dev->name, ifmgd->bssid);
1008                 disassoc = true;
1009                 ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1010                 goto unlock;
1011         }
1012
1013         /*
1014          * Beacon filtering is only enabled with power save and then the
1015          * stack should not check for beacon loss.
1016          */
1017         if (!((local->hw.flags & IEEE80211_HW_BEACON_FILTER) &&
1018               (local->hw.conf.flags & IEEE80211_CONF_PS)) &&
1019             time_after(jiffies,
1020                        ifmgd->last_beacon + IEEE80211_MONITORING_INTERVAL)) {
1021 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1022                 if (net_ratelimit()) {
1023                         printk(KERN_DEBUG "%s: beacon loss from AP %pM "
1024                                "- sending probe request\n",
1025                                sdata->dev->name, ifmgd->bssid);
1026                 }
1027 #endif
1028                 ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
1029                 ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
1030                                          ifmgd->ssid_len, NULL, 0);
1031                 goto unlock;
1032
1033         }
1034
1035         if (time_after(jiffies, sta->last_rx + IEEE80211_PROBE_IDLE_TIME)) {
1036                 ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
1037                 ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
1038                                          ifmgd->ssid_len, NULL, 0);
1039         }
1040
1041  unlock:
1042         rcu_read_unlock();
1043
1044         if (disassoc)
1045                 ieee80211_set_disassoc(sdata, true, true,
1046                                         WLAN_REASON_PREV_AUTH_NOT_VALID);
1047         else
1048                 mod_timer(&ifmgd->timer, jiffies +
1049                                       IEEE80211_MONITORING_INTERVAL);
1050 }
1051
1052
1053 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata)
1054 {
1055         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1056
1057         printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
1058         ifmgd->flags |= IEEE80211_STA_AUTHENTICATED;
1059         if (ifmgd->flags & IEEE80211_STA_EXT_SME) {
1060                 /* Wait for SME to request association */
1061                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
1062         } else
1063                 ieee80211_associate(sdata);
1064 }
1065
1066
1067 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1068                                      struct ieee80211_mgmt *mgmt,
1069                                      size_t len)
1070 {
1071         u8 *pos;
1072         struct ieee802_11_elems elems;
1073
1074         pos = mgmt->u.auth.variable;
1075         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1076         if (!elems.challenge)
1077                 return;
1078         ieee80211_send_auth(sdata, 3, sdata->u.mgd.auth_alg,
1079                             elems.challenge - 2, elems.challenge_len + 2,
1080                             sdata->u.mgd.bssid, 1);
1081         sdata->u.mgd.auth_transaction = 4;
1082 }
1083
1084 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1085                                    struct ieee80211_mgmt *mgmt,
1086                                    size_t len)
1087 {
1088         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1089         u16 auth_alg, auth_transaction, status_code;
1090
1091         if (ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE)
1092                 return;
1093
1094         if (len < 24 + 6)
1095                 return;
1096
1097         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
1098                 return;
1099
1100         if (memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
1101                 return;
1102
1103         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1104         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1105         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1106
1107         if (auth_alg != ifmgd->auth_alg ||
1108             auth_transaction != ifmgd->auth_transaction)
1109                 return;
1110
1111         if (status_code != WLAN_STATUS_SUCCESS) {
1112                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1113                         u8 algs[3];
1114                         const int num_algs = ARRAY_SIZE(algs);
1115                         int i, pos;
1116                         algs[0] = algs[1] = algs[2] = 0xff;
1117                         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1118                                 algs[0] = WLAN_AUTH_OPEN;
1119                         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1120                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1121                         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1122                                 algs[2] = WLAN_AUTH_LEAP;
1123                         if (ifmgd->auth_alg == WLAN_AUTH_OPEN)
1124                                 pos = 0;
1125                         else if (ifmgd->auth_alg == WLAN_AUTH_SHARED_KEY)
1126                                 pos = 1;
1127                         else
1128                                 pos = 2;
1129                         for (i = 0; i < num_algs; i++) {
1130                                 pos++;
1131                                 if (pos >= num_algs)
1132                                         pos = 0;
1133                                 if (algs[pos] == ifmgd->auth_alg ||
1134                                     algs[pos] == 0xff)
1135                                         continue;
1136                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1137                                     !ieee80211_sta_wep_configured(sdata))
1138                                         continue;
1139                                 ifmgd->auth_alg = algs[pos];
1140                                 break;
1141                         }
1142                 }
1143                 return;
1144         }
1145
1146         switch (ifmgd->auth_alg) {
1147         case WLAN_AUTH_OPEN:
1148         case WLAN_AUTH_LEAP:
1149         case WLAN_AUTH_FT:
1150                 ieee80211_auth_completed(sdata);
1151                 cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len);
1152                 break;
1153         case WLAN_AUTH_SHARED_KEY:
1154                 if (ifmgd->auth_transaction == 4) {
1155                         ieee80211_auth_completed(sdata);
1156                         cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len);
1157                 } else
1158                         ieee80211_auth_challenge(sdata, mgmt, len);
1159                 break;
1160         }
1161 }
1162
1163
1164 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1165                                      struct ieee80211_mgmt *mgmt,
1166                                      size_t len)
1167 {
1168         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1169         u16 reason_code;
1170
1171         if (len < 24 + 2)
1172                 return;
1173
1174         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
1175                 return;
1176
1177         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1178
1179         if (ifmgd->flags & IEEE80211_STA_AUTHENTICATED)
1180                 printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
1181                                 sdata->dev->name, reason_code);
1182
1183         if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
1184             (ifmgd->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1185              ifmgd->state == IEEE80211_STA_MLME_ASSOCIATE ||
1186              ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED)) {
1187                 ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1188                 mod_timer(&ifmgd->timer, jiffies +
1189                                       IEEE80211_RETRY_AUTH_INTERVAL);
1190         }
1191
1192         ieee80211_set_disassoc(sdata, true, false, 0);
1193         ifmgd->flags &= ~IEEE80211_STA_AUTHENTICATED;
1194         cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, len);
1195 }
1196
1197
1198 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1199                                        struct ieee80211_mgmt *mgmt,
1200                                        size_t len)
1201 {
1202         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1203         u16 reason_code;
1204
1205         if (len < 24 + 2)
1206                 return;
1207
1208         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
1209                 return;
1210
1211         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1212
1213         if (ifmgd->flags & IEEE80211_STA_ASSOCIATED)
1214                 printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
1215                                 sdata->dev->name, reason_code);
1216
1217         if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
1218             ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED) {
1219                 ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
1220                 mod_timer(&ifmgd->timer, jiffies +
1221                                       IEEE80211_RETRY_AUTH_INTERVAL);
1222         }
1223
1224         ieee80211_set_disassoc(sdata, false, false, reason_code);
1225         cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, len);
1226 }
1227
1228
1229 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1230                                          struct ieee80211_mgmt *mgmt,
1231                                          size_t len,
1232                                          int reassoc)
1233 {
1234         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1235         struct ieee80211_local *local = sdata->local;
1236         struct ieee80211_supported_band *sband;
1237         struct sta_info *sta;
1238         u32 rates, basic_rates;
1239         u16 capab_info, status_code, aid;
1240         struct ieee802_11_elems elems;
1241         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1242         u8 *pos;
1243         u32 changed = 0;
1244         int i, j;
1245         bool have_higher_than_11mbit = false, newsta = false;
1246         u16 ap_ht_cap_flags;
1247
1248         /* AssocResp and ReassocResp have identical structure, so process both
1249          * of them in this function. */
1250
1251         if (ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
1252                 return;
1253
1254         if (len < 24 + 6)
1255                 return;
1256
1257         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
1258                 return;
1259
1260         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1261         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1262         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1263
1264         printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
1265                "status=%d aid=%d)\n",
1266                sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
1267                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1268
1269         pos = mgmt->u.assoc_resp.variable;
1270         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1271
1272         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
1273             elems.timeout_int && elems.timeout_int_len == 5 &&
1274             elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
1275                 u32 tu, ms;
1276                 tu = get_unaligned_le32(elems.timeout_int + 1);
1277                 ms = tu * 1024 / 1000;
1278                 printk(KERN_DEBUG "%s: AP rejected association temporarily; "
1279                        "comeback duration %u TU (%u ms)\n",
1280                        sdata->dev->name, tu, ms);
1281                 if (ms > IEEE80211_ASSOC_TIMEOUT)
1282                         mod_timer(&ifmgd->timer,
1283                                   jiffies + msecs_to_jiffies(ms));
1284                 return;
1285         }
1286
1287         if (status_code != WLAN_STATUS_SUCCESS) {
1288                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1289                        sdata->dev->name, status_code);
1290                 /* if this was a reassociation, ensure we try a "full"
1291                  * association next time. This works around some broken APs
1292                  * which do not correctly reject reassociation requests. */
1293                 ifmgd->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1294                 return;
1295         }
1296
1297         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1298                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1299                        "set\n", sdata->dev->name, aid);
1300         aid &= ~(BIT(15) | BIT(14));
1301
1302         if (!elems.supp_rates) {
1303                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1304                        sdata->dev->name);
1305                 return;
1306         }
1307
1308         printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1309         ifmgd->aid = aid;
1310         ifmgd->ap_capab = capab_info;
1311
1312         kfree(ifmgd->assocresp_ies);
1313         ifmgd->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1314         ifmgd->assocresp_ies = kmalloc(ifmgd->assocresp_ies_len, GFP_KERNEL);
1315         if (ifmgd->assocresp_ies)
1316                 memcpy(ifmgd->assocresp_ies, pos, ifmgd->assocresp_ies_len);
1317
1318         rcu_read_lock();
1319
1320         /* Add STA entry for the AP */
1321         sta = sta_info_get(local, ifmgd->bssid);
1322         if (!sta) {
1323                 newsta = true;
1324
1325                 sta = sta_info_alloc(sdata, ifmgd->bssid, GFP_ATOMIC);
1326                 if (!sta) {
1327                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1328                                " the AP\n", sdata->dev->name);
1329                         rcu_read_unlock();
1330                         return;
1331                 }
1332
1333                 /* update new sta with its last rx activity */
1334                 sta->last_rx = jiffies;
1335         }
1336
1337         /*
1338          * FIXME: Do we really need to update the sta_info's information here?
1339          *        We already know about the AP (we found it in our list) so it
1340          *        should already be filled with the right info, no?
1341          *        As is stands, all this is racy because typically we assume
1342          *        the information that is filled in here (except flags) doesn't
1343          *        change while a STA structure is alive. As such, it should move
1344          *        to between the sta_info_alloc() and sta_info_insert() above.
1345          */
1346
1347         set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1348                            WLAN_STA_AUTHORIZED);
1349
1350         rates = 0;
1351         basic_rates = 0;
1352         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1353
1354         for (i = 0; i < elems.supp_rates_len; i++) {
1355                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1356                 bool is_basic = !!(elems.supp_rates[i] & 0x80);
1357
1358                 if (rate > 110)
1359                         have_higher_than_11mbit = true;
1360
1361                 for (j = 0; j < sband->n_bitrates; j++) {
1362                         if (sband->bitrates[j].bitrate == rate) {
1363                                 rates |= BIT(j);
1364                                 if (is_basic)
1365                                         basic_rates |= BIT(j);
1366                                 break;
1367                         }
1368                 }
1369         }
1370
1371         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1372                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1373                 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
1374
1375                 if (rate > 110)
1376                         have_higher_than_11mbit = true;
1377
1378                 for (j = 0; j < sband->n_bitrates; j++) {
1379                         if (sband->bitrates[j].bitrate == rate) {
1380                                 rates |= BIT(j);
1381                                 if (is_basic)
1382                                         basic_rates |= BIT(j);
1383                                 break;
1384                         }
1385                 }
1386         }
1387
1388         sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
1389         sdata->vif.bss_conf.basic_rates = basic_rates;
1390
1391         /* cf. IEEE 802.11 9.2.12 */
1392         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1393             have_higher_than_11mbit)
1394                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1395         else
1396                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1397
1398         /* If TKIP/WEP is used, no need to parse AP's HT capabilities */
1399         if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))
1400                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1401                                 elems.ht_cap_elem, &sta->sta.ht_cap);
1402
1403         ap_ht_cap_flags = sta->sta.ht_cap.cap;
1404
1405         rate_control_rate_init(sta);
1406
1407         if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
1408                 set_sta_flags(sta, WLAN_STA_MFP);
1409
1410         if (elems.wmm_param)
1411                 set_sta_flags(sta, WLAN_STA_WME);
1412
1413         if (newsta) {
1414                 int err = sta_info_insert(sta);
1415                 if (err) {
1416                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
1417                                " the AP (error %d)\n", sdata->dev->name, err);
1418                         rcu_read_unlock();
1419                         return;
1420                 }
1421         }
1422
1423         rcu_read_unlock();
1424
1425         if (elems.wmm_param)
1426                 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1427                                          elems.wmm_param_len);
1428
1429         if (elems.ht_info_elem && elems.wmm_param &&
1430             (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
1431             !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))
1432                 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1433                                                ap_ht_cap_flags);
1434
1435         /* set AID and assoc capability,
1436          * ieee80211_set_associated() will tell the driver */
1437         bss_conf->aid = aid;
1438         bss_conf->assoc_capability = capab_info;
1439         ieee80211_set_associated(sdata, changed);
1440
1441         /*
1442          * initialise the time of last beacon to be the association time,
1443          * otherwise beacon loss check will trigger immediately
1444          */
1445         ifmgd->last_beacon = jiffies;
1446
1447         ieee80211_associated(sdata);
1448         cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len);
1449 }
1450
1451
1452 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1453                                   struct ieee80211_mgmt *mgmt,
1454                                   size_t len,
1455                                   struct ieee80211_rx_status *rx_status,
1456                                   struct ieee802_11_elems *elems,
1457                                   bool beacon)
1458 {
1459         struct ieee80211_local *local = sdata->local;
1460         int freq;
1461         struct ieee80211_bss *bss;
1462         struct ieee80211_channel *channel;
1463
1464         if (elems->ds_params && elems->ds_params_len == 1)
1465                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1466         else
1467                 freq = rx_status->freq;
1468
1469         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1470
1471         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1472                 return;
1473
1474         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1475                                         channel, beacon);
1476         if (!bss)
1477                 return;
1478
1479         if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
1480             (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN) == 0)) {
1481                 struct ieee80211_channel_sw_ie *sw_elem =
1482                         (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
1483                 ieee80211_process_chanswitch(sdata, sw_elem, bss);
1484         }
1485
1486         ieee80211_rx_bss_put(local, bss);
1487 }
1488
1489
1490 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1491                                          struct ieee80211_mgmt *mgmt,
1492                                          size_t len,
1493                                          struct ieee80211_rx_status *rx_status)
1494 {
1495         struct ieee80211_if_managed *ifmgd;
1496         size_t baselen;
1497         struct ieee802_11_elems elems;
1498
1499         ifmgd = &sdata->u.mgd;
1500
1501         if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1502                 return; /* ignore ProbeResp to foreign address */
1503
1504         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1505         if (baselen > len)
1506                 return;
1507
1508         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1509                                 &elems);
1510
1511         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1512
1513         /* direct probe may be part of the association flow */
1514         if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
1515                                &ifmgd->request)) {
1516                 printk(KERN_DEBUG "%s direct probe responded\n",
1517                        sdata->dev->name);
1518                 ieee80211_authenticate(sdata);
1519         }
1520
1521         if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL)
1522                 ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1523 }
1524
1525 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1526                                      struct ieee80211_mgmt *mgmt,
1527                                      size_t len,
1528                                      struct ieee80211_rx_status *rx_status)
1529 {
1530         struct ieee80211_if_managed *ifmgd;
1531         size_t baselen;
1532         struct ieee802_11_elems elems;
1533         struct ieee80211_local *local = sdata->local;
1534         u32 changed = 0;
1535         bool erp_valid, directed_tim;
1536         u8 erp_value = 0;
1537
1538         /* Process beacon from the current BSS */
1539         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1540         if (baselen > len)
1541                 return;
1542
1543         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1544
1545         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
1546
1547         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1548                 return;
1549
1550         ifmgd = &sdata->u.mgd;
1551
1552         if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED) ||
1553             memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
1554                 return;
1555
1556         if (rx_status->freq != local->hw.conf.channel->center_freq)
1557                 return;
1558
1559         ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1560                                  elems.wmm_param_len);
1561
1562         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1563                 directed_tim = ieee80211_check_tim(&elems, ifmgd->aid);
1564
1565                 if (directed_tim) {
1566                         if (local->hw.conf.dynamic_ps_timeout > 0) {
1567                                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1568                                 ieee80211_hw_config(local,
1569                                                     IEEE80211_CONF_CHANGE_PS);
1570                                 ieee80211_send_nullfunc(local, sdata, 0);
1571                         } else {
1572                                 local->pspolling = true;
1573
1574                                 /*
1575                                  * Here is assumed that the driver will be
1576                                  * able to send ps-poll frame and receive a
1577                                  * response even though power save mode is
1578                                  * enabled, but some drivers might require
1579                                  * to disable power save here. This needs
1580                                  * to be investigated.
1581                                  */
1582                                 ieee80211_send_pspoll(local, sdata);
1583                         }
1584                 }
1585         }
1586
1587         if (elems.erp_info && elems.erp_info_len >= 1) {
1588                 erp_valid = true;
1589                 erp_value = elems.erp_info[0];
1590         } else {
1591                 erp_valid = false;
1592         }
1593         changed |= ieee80211_handle_bss_capability(sdata,
1594                         le16_to_cpu(mgmt->u.beacon.capab_info),
1595                         erp_valid, erp_value);
1596
1597
1598         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1599             !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED)) {
1600                 struct sta_info *sta;
1601                 struct ieee80211_supported_band *sband;
1602                 u16 ap_ht_cap_flags;
1603
1604                 rcu_read_lock();
1605
1606                 sta = sta_info_get(local, ifmgd->bssid);
1607                 if (!sta) {
1608                         rcu_read_unlock();
1609                         return;
1610                 }
1611
1612                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1613
1614                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1615                                 elems.ht_cap_elem, &sta->sta.ht_cap);
1616
1617                 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1618
1619                 rcu_read_unlock();
1620
1621                 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1622                                                ap_ht_cap_flags);
1623         }
1624
1625         if (elems.country_elem) {
1626                 /* Note we are only reviewing this on beacons
1627                  * for the BSSID we are associated to */
1628                 regulatory_hint_11d(local->hw.wiphy,
1629                         elems.country_elem, elems.country_elem_len);
1630
1631                 /* TODO: IBSS also needs this */
1632                 if (elems.pwr_constr_elem)
1633                         ieee80211_handle_pwr_constr(sdata,
1634                                 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1635                                 elems.pwr_constr_elem,
1636                                 elems.pwr_constr_elem_len);
1637         }
1638
1639         ieee80211_bss_info_change_notify(sdata, changed);
1640 }
1641
1642 ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1643                                           struct sk_buff *skb,
1644                                           struct ieee80211_rx_status *rx_status)
1645 {
1646         struct ieee80211_local *local = sdata->local;
1647         struct ieee80211_mgmt *mgmt;
1648         u16 fc;
1649
1650         if (skb->len < 24)
1651                 return RX_DROP_MONITOR;
1652
1653         mgmt = (struct ieee80211_mgmt *) skb->data;
1654         fc = le16_to_cpu(mgmt->frame_control);
1655
1656         switch (fc & IEEE80211_FCTL_STYPE) {
1657         case IEEE80211_STYPE_PROBE_REQ:
1658         case IEEE80211_STYPE_PROBE_RESP:
1659         case IEEE80211_STYPE_BEACON:
1660                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1661         case IEEE80211_STYPE_AUTH:
1662         case IEEE80211_STYPE_ASSOC_RESP:
1663         case IEEE80211_STYPE_REASSOC_RESP:
1664         case IEEE80211_STYPE_DEAUTH:
1665         case IEEE80211_STYPE_DISASSOC:
1666                 skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
1667                 queue_work(local->hw.workqueue, &sdata->u.mgd.work);
1668                 return RX_QUEUED;
1669         }
1670
1671         return RX_DROP_MONITOR;
1672 }
1673
1674 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1675                                          struct sk_buff *skb)
1676 {
1677         struct ieee80211_rx_status *rx_status;
1678         struct ieee80211_mgmt *mgmt;
1679         u16 fc;
1680
1681         rx_status = (struct ieee80211_rx_status *) skb->cb;
1682         mgmt = (struct ieee80211_mgmt *) skb->data;
1683         fc = le16_to_cpu(mgmt->frame_control);
1684
1685         switch (fc & IEEE80211_FCTL_STYPE) {
1686         case IEEE80211_STYPE_PROBE_RESP:
1687                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
1688                                              rx_status);
1689                 break;
1690         case IEEE80211_STYPE_BEACON:
1691                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1692                                          rx_status);
1693                 break;
1694         case IEEE80211_STYPE_AUTH:
1695                 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
1696                 break;
1697         case IEEE80211_STYPE_ASSOC_RESP:
1698                 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 0);
1699                 break;
1700         case IEEE80211_STYPE_REASSOC_RESP:
1701                 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 1);
1702                 break;
1703         case IEEE80211_STYPE_DEAUTH:
1704                 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
1705                 break;
1706         case IEEE80211_STYPE_DISASSOC:
1707                 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1708                 break;
1709         }
1710
1711         kfree_skb(skb);
1712 }
1713
1714 static void ieee80211_sta_timer(unsigned long data)
1715 {
1716         struct ieee80211_sub_if_data *sdata =
1717                 (struct ieee80211_sub_if_data *) data;
1718         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1719         struct ieee80211_local *local = sdata->local;
1720
1721         set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
1722         queue_work(local->hw.workqueue, &ifmgd->work);
1723 }
1724
1725 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata)
1726 {
1727         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1728         struct ieee80211_local *local = sdata->local;
1729
1730         if (local->ops->reset_tsf) {
1731                 /* Reset own TSF to allow time synchronization work. */
1732                 local->ops->reset_tsf(local_to_hw(local));
1733         }
1734
1735         ifmgd->wmm_last_param_set = -1; /* allow any WMM update */
1736
1737
1738         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1739                 ifmgd->auth_alg = WLAN_AUTH_OPEN;
1740         else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1741                 ifmgd->auth_alg = WLAN_AUTH_SHARED_KEY;
1742         else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1743                 ifmgd->auth_alg = WLAN_AUTH_LEAP;
1744         else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_FT)
1745                 ifmgd->auth_alg = WLAN_AUTH_FT;
1746         else
1747                 ifmgd->auth_alg = WLAN_AUTH_OPEN;
1748         ifmgd->auth_transaction = -1;
1749         ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
1750         ifmgd->assoc_scan_tries = 0;
1751         ifmgd->direct_probe_tries = 0;
1752         ifmgd->auth_tries = 0;
1753         ifmgd->assoc_tries = 0;
1754         netif_tx_stop_all_queues(sdata->dev);
1755         netif_carrier_off(sdata->dev);
1756 }
1757
1758 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata)
1759 {
1760         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1761         struct ieee80211_local *local = sdata->local;
1762         struct ieee80211_bss *bss;
1763         u8 *bssid = ifmgd->bssid, *ssid = ifmgd->ssid;
1764         u8 ssid_len = ifmgd->ssid_len;
1765         u16 capa_mask = WLAN_CAPABILITY_ESS;
1766         u16 capa_val = WLAN_CAPABILITY_ESS;
1767         struct ieee80211_channel *chan = local->oper_channel;
1768
1769         if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
1770             ifmgd->flags & (IEEE80211_STA_AUTO_SSID_SEL |
1771                             IEEE80211_STA_AUTO_BSSID_SEL |
1772                             IEEE80211_STA_AUTO_CHANNEL_SEL)) {
1773                 capa_mask |= WLAN_CAPABILITY_PRIVACY;
1774                 if (sdata->default_key)
1775                         capa_val |= WLAN_CAPABILITY_PRIVACY;
1776         }
1777
1778         if (ifmgd->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
1779                 chan = NULL;
1780
1781         if (ifmgd->flags & IEEE80211_STA_AUTO_BSSID_SEL)
1782                 bssid = NULL;
1783
1784         if (ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL) {
1785                 ssid = NULL;
1786                 ssid_len = 0;
1787         }
1788
1789         bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan,
1790                                        bssid, ssid, ssid_len,
1791                                        capa_mask, capa_val);
1792
1793         if (bss) {
1794                 ieee80211_set_freq(sdata, bss->cbss.channel->center_freq);
1795                 if (!(ifmgd->flags & IEEE80211_STA_SSID_SET))
1796                         ieee80211_sta_set_ssid(sdata, bss->ssid,
1797                                                bss->ssid_len);
1798                 ieee80211_sta_set_bssid(sdata, bss->cbss.bssid);
1799                 ieee80211_sta_def_wmm_params(sdata, bss->supp_rates_len,
1800                                                     bss->supp_rates);
1801                 if (sdata->u.mgd.mfp == IEEE80211_MFP_REQUIRED)
1802                         sdata->u.mgd.flags |= IEEE80211_STA_MFP_ENABLED;
1803                 else
1804                         sdata->u.mgd.flags &= ~IEEE80211_STA_MFP_ENABLED;
1805
1806                 /* Send out direct probe if no probe resp was received or
1807                  * the one we have is outdated
1808                  */
1809                 if (!bss->last_probe_resp ||
1810                     time_after(jiffies, bss->last_probe_resp
1811                                         + IEEE80211_SCAN_RESULT_EXPIRE))
1812                         ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1813                 else
1814                         ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
1815
1816                 ieee80211_rx_bss_put(local, bss);
1817                 ieee80211_sta_reset_auth(sdata);
1818                 return 0;
1819         } else {
1820                 if (ifmgd->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
1821                         ifmgd->assoc_scan_tries++;
1822                         /* XXX maybe racy? */
1823                         if (local->scan_req)
1824                                 return -1;
1825                         memcpy(local->int_scan_req.ssids[0].ssid,
1826                                ifmgd->ssid, IEEE80211_MAX_SSID_LEN);
1827                         if (ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL)
1828                                 local->int_scan_req.ssids[0].ssid_len = 0;
1829                         else
1830                                 local->int_scan_req.ssids[0].ssid_len = ifmgd->ssid_len;
1831
1832                         if (ieee80211_start_scan(sdata, &local->int_scan_req))
1833                                 ieee80211_scan_failed(local);
1834
1835                         ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
1836                         set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
1837                 } else {
1838                         ifmgd->assoc_scan_tries = 0;
1839                         ifmgd->state = IEEE80211_STA_MLME_DISABLED;
1840                 }
1841         }
1842         return -1;
1843 }
1844
1845
1846 static void ieee80211_sta_work(struct work_struct *work)
1847 {
1848         struct ieee80211_sub_if_data *sdata =
1849                 container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
1850         struct ieee80211_local *local = sdata->local;
1851         struct ieee80211_if_managed *ifmgd;
1852         struct sk_buff *skb;
1853
1854         if (!netif_running(sdata->dev))
1855                 return;
1856
1857         if (local->sw_scanning || local->hw_scanning)
1858                 return;
1859
1860         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1861                 return;
1862         ifmgd = &sdata->u.mgd;
1863
1864         while ((skb = skb_dequeue(&ifmgd->skb_queue)))
1865                 ieee80211_sta_rx_queued_mgmt(sdata, skb);
1866
1867         if (ifmgd->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
1868             ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE &&
1869             ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE &&
1870             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request)) {
1871                 /*
1872                  * The call to ieee80211_start_scan can fail but ieee80211_request_scan
1873                  * (which queued ieee80211_sta_work) did not return an error. Thus, call
1874                  * ieee80211_scan_failed here if ieee80211_start_scan fails in order to
1875                  * notify the scan requester.
1876                  */
1877                 if (ieee80211_start_scan(sdata, local->scan_req))
1878                         ieee80211_scan_failed(local);
1879                 return;
1880         }
1881
1882         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request)) {
1883                 if (ieee80211_sta_config_auth(sdata))
1884                         return;
1885                 clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
1886         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request))
1887                 return;
1888
1889         switch (ifmgd->state) {
1890         case IEEE80211_STA_MLME_DISABLED:
1891                 break;
1892         case IEEE80211_STA_MLME_DIRECT_PROBE:
1893                 ieee80211_direct_probe(sdata);
1894                 break;
1895         case IEEE80211_STA_MLME_AUTHENTICATE:
1896                 ieee80211_authenticate(sdata);
1897                 break;
1898         case IEEE80211_STA_MLME_ASSOCIATE:
1899                 ieee80211_associate(sdata);
1900                 break;
1901         case IEEE80211_STA_MLME_ASSOCIATED:
1902                 ieee80211_associated(sdata);
1903                 break;
1904         default:
1905                 WARN_ON(1);
1906                 break;
1907         }
1908
1909         if (ieee80211_privacy_mismatch(sdata)) {
1910                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
1911                        "mixed-cell disabled - disassociate\n", sdata->dev->name);
1912
1913                 ieee80211_set_disassoc(sdata, false, true,
1914                                         WLAN_REASON_UNSPECIFIED);
1915         }
1916 }
1917
1918 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
1919 {
1920         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1921                 /*
1922                  * Need to update last_beacon to avoid beacon loss
1923                  * test to trigger.
1924                  */
1925                 sdata->u.mgd.last_beacon = jiffies;
1926
1927
1928                 queue_work(sdata->local->hw.workqueue,
1929                            &sdata->u.mgd.work);
1930         }
1931 }
1932
1933 /* interface setup */
1934 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
1935 {
1936         struct ieee80211_if_managed *ifmgd;
1937
1938         ifmgd = &sdata->u.mgd;
1939         INIT_WORK(&ifmgd->work, ieee80211_sta_work);
1940         INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
1941         INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
1942         setup_timer(&ifmgd->timer, ieee80211_sta_timer,
1943                     (unsigned long) sdata);
1944         setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
1945                     (unsigned long) sdata);
1946         skb_queue_head_init(&ifmgd->skb_queue);
1947
1948         ifmgd->capab = WLAN_CAPABILITY_ESS;
1949         ifmgd->auth_algs = IEEE80211_AUTH_ALG_OPEN |
1950                 IEEE80211_AUTH_ALG_SHARED_KEY;
1951         ifmgd->flags |= IEEE80211_STA_CREATE_IBSS |
1952                 IEEE80211_STA_AUTO_BSSID_SEL |
1953                 IEEE80211_STA_AUTO_CHANNEL_SEL;
1954         if (sdata->local->hw.queues >= 4)
1955                 ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
1956 }
1957
1958 /* configuration hooks */
1959 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata)
1960 {
1961         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1962         struct ieee80211_local *local = sdata->local;
1963
1964         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1965                 return;
1966
1967         if ((ifmgd->flags & (IEEE80211_STA_BSSID_SET |
1968                              IEEE80211_STA_AUTO_BSSID_SEL)) &&
1969             (ifmgd->flags & (IEEE80211_STA_SSID_SET |
1970                              IEEE80211_STA_AUTO_SSID_SEL))) {
1971
1972                 if (ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED)
1973                         ieee80211_set_disassoc(sdata, true, true,
1974                                                WLAN_REASON_DEAUTH_LEAVING);
1975
1976                 if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) ||
1977                     ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
1978                         set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
1979                 else if (ifmgd->flags & IEEE80211_STA_EXT_SME)
1980                         set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
1981                 queue_work(local->hw.workqueue, &ifmgd->work);
1982         }
1983 }
1984
1985 int ieee80211_sta_commit(struct ieee80211_sub_if_data *sdata)
1986 {
1987         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1988
1989         if (ifmgd->ssid_len)
1990                 ifmgd->flags |= IEEE80211_STA_SSID_SET;
1991         else
1992                 ifmgd->flags &= ~IEEE80211_STA_SSID_SET;
1993
1994         return 0;
1995 }
1996
1997 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
1998 {
1999         struct ieee80211_if_managed *ifmgd;
2000
2001         if (len > IEEE80211_MAX_SSID_LEN)
2002                 return -EINVAL;
2003
2004         ifmgd = &sdata->u.mgd;
2005
2006         if (ifmgd->ssid_len != len || memcmp(ifmgd->ssid, ssid, len) != 0) {
2007                 /*
2008                  * Do not use reassociation if SSID is changed (different ESS).
2009                  */
2010                 ifmgd->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2011                 memset(ifmgd->ssid, 0, sizeof(ifmgd->ssid));
2012                 memcpy(ifmgd->ssid, ssid, len);
2013                 ifmgd->ssid_len = len;
2014         }
2015
2016         return ieee80211_sta_commit(sdata);
2017 }
2018
2019 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2020 {
2021         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2022         memcpy(ssid, ifmgd->ssid, ifmgd->ssid_len);
2023         *len = ifmgd->ssid_len;
2024         return 0;
2025 }
2026
2027 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2028 {
2029         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2030
2031         if (is_valid_ether_addr(bssid)) {
2032                 memcpy(ifmgd->bssid, bssid, ETH_ALEN);
2033                 ifmgd->flags |= IEEE80211_STA_BSSID_SET;
2034         } else {
2035                 memset(ifmgd->bssid, 0, ETH_ALEN);
2036                 ifmgd->flags &= ~IEEE80211_STA_BSSID_SET;
2037         }
2038
2039         if (netif_running(sdata->dev)) {
2040                 if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) {
2041                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2042                                "the low-level driver\n", sdata->dev->name);
2043                 }
2044         }
2045
2046         return ieee80211_sta_commit(sdata);
2047 }
2048
2049 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata,
2050                                const char *ie, size_t len)
2051 {
2052         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2053
2054         kfree(ifmgd->extra_ie);
2055         if (len == 0) {
2056                 ifmgd->extra_ie = NULL;
2057                 ifmgd->extra_ie_len = 0;
2058                 return 0;
2059         }
2060         ifmgd->extra_ie = kmalloc(len, GFP_KERNEL);
2061         if (!ifmgd->extra_ie) {
2062                 ifmgd->extra_ie_len = 0;
2063                 return -ENOMEM;
2064         }
2065         memcpy(ifmgd->extra_ie, ie, len);
2066         ifmgd->extra_ie_len = len;
2067         return 0;
2068 }
2069
2070 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2071 {
2072         printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2073                sdata->dev->name, reason);
2074
2075         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2076                 return -EINVAL;
2077
2078         ieee80211_set_disassoc(sdata, true, true, reason);
2079         return 0;
2080 }
2081
2082 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2083 {
2084         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2085
2086         printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2087                sdata->dev->name, reason);
2088
2089         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2090                 return -EINVAL;
2091
2092         if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED))
2093                 return -ENOLINK;
2094
2095         ieee80211_set_disassoc(sdata, false, true, reason);
2096         return 0;
2097 }
2098
2099 /* scan finished notification */
2100 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2101 {
2102         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2103
2104         /* Restart STA timers */
2105         rcu_read_lock();
2106         list_for_each_entry_rcu(sdata, &local->interfaces, list)
2107                 ieee80211_restart_sta_timer(sdata);
2108         rcu_read_unlock();
2109 }
2110
2111 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
2112 {
2113         struct ieee80211_local *local =
2114                 container_of(work, struct ieee80211_local,
2115                              dynamic_ps_disable_work);
2116
2117         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2118                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2119                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2120         }
2121
2122         ieee80211_wake_queues_by_reason(&local->hw,
2123                                         IEEE80211_QUEUE_STOP_REASON_PS);
2124 }
2125
2126 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
2127 {
2128         struct ieee80211_local *local =
2129                 container_of(work, struct ieee80211_local,
2130                              dynamic_ps_enable_work);
2131         /* XXX: using scan_sdata is completely broken! */
2132         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2133
2134         if (local->hw.conf.flags & IEEE80211_CONF_PS)
2135                 return;
2136
2137         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK && sdata)
2138                 ieee80211_send_nullfunc(local, sdata, 1);
2139
2140         local->hw.conf.flags |= IEEE80211_CONF_PS;
2141         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2142 }
2143
2144 void ieee80211_dynamic_ps_timer(unsigned long data)
2145 {
2146         struct ieee80211_local *local = (void *) data;
2147
2148         queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
2149 }
2150
2151 void ieee80211_send_nullfunc(struct ieee80211_local *local,
2152                              struct ieee80211_sub_if_data *sdata,
2153                              int powersave)
2154 {
2155         struct sk_buff *skb;
2156         struct ieee80211_hdr *nullfunc;
2157         __le16 fc;
2158
2159         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2160                 return;
2161
2162         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2163         if (!skb) {
2164                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2165                        "frame\n", sdata->dev->name);
2166                 return;
2167         }
2168         skb_reserve(skb, local->hw.extra_tx_headroom);
2169
2170         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2171         memset(nullfunc, 0, 24);
2172         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2173                          IEEE80211_FCTL_TODS);
2174         if (powersave)
2175                 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
2176         nullfunc->frame_control = fc;
2177         memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2178         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2179         memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2180
2181         ieee80211_tx_skb(sdata, skb, 0);
2182 }