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