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