]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac80211/mlme.c
mac80211: make ieee80211_rx_h_mgmt more readable
[mirror_ubuntu-bionic-kernel.git] / net / mac80211 / mlme.c
CommitLineData
f0706e82
JB
1/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
5b323edb 14#include <linux/delay.h>
f0706e82
JB
15#include <linux/if_ether.h>
16#include <linux/skbuff.h>
17#include <linux/netdevice.h>
18#include <linux/if_arp.h>
19#include <linux/wireless.h>
20#include <linux/random.h>
21#include <linux/etherdevice.h>
d0709a65 22#include <linux/rtnetlink.h>
f0706e82 23#include <net/iw_handler.h>
f0706e82 24#include <net/mac80211.h>
60f8b39c 25
f0706e82 26#include "ieee80211_i.h"
2c8dccc7
JB
27#include "rate.h"
28#include "led.h"
f709fc69 29#include "mesh.h"
f0706e82 30
6042a3e3 31#define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
f0706e82
JB
32#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
33#define IEEE80211_AUTH_MAX_TRIES 3
34#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
35#define IEEE80211_ASSOC_MAX_TRIES 3
36#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
f709fc69 37#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
f0706e82
JB
38#define IEEE80211_PROBE_INTERVAL (60 * HZ)
39#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
40#define IEEE80211_SCAN_INTERVAL (2 * HZ)
41#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
872ba533 42#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
f0706e82 43
f0706e82
JB
44#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
45#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
f709fc69 46#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
f0706e82
JB
47
48#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
49
50
5484e237
JB
51/* utils */
52static int ecw2cw(int ecw)
60f8b39c 53{
5484e237 54 return (1 << ecw) - 1;
60f8b39c
JB
55}
56
57static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
43ac2ca3
JM
58{
59 u8 *end, *pos;
60
61 pos = bss->ies;
62 if (pos == NULL)
63 return NULL;
64 end = pos + bss->ies_len;
65
66 while (pos + 1 < end) {
67 if (pos + 2 + pos[1] > end)
68 break;
69 if (pos[0] == ie)
70 return pos;
71 pos += 2 + pos[1];
72 }
73
74 return NULL;
75}
76
9ac19a90
JB
77static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
78 struct ieee80211_supported_band *sband,
79 u64 *rates)
80{
81 int i, j, count;
82 *rates = 0;
83 count = 0;
84 for (i = 0; i < bss->supp_rates_len; i++) {
85 int rate = (bss->supp_rates[i] & 0x7F) * 5;
86
87 for (j = 0; j < sband->n_bitrates; j++)
88 if (sband->bitrates[j].bitrate == rate) {
89 *rates |= BIT(j);
90 count++;
91 break;
92 }
93 }
94
95 return count;
96}
97
60f8b39c
JB
98/* frame sending functions */
99void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
100 int encrypt)
101{
102 skb->dev = sdata->local->mdev;
103 skb_set_mac_header(skb, 0);
104 skb_set_network_header(skb, 0);
105 skb_set_transport_header(skb, 0);
e2839d8f 106
60f8b39c
JB
107 skb->iif = sdata->dev->ifindex;
108 skb->do_not_encrypt = !encrypt;
109
110 dev_queue_xmit(skb);
111}
112
113static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
114 struct ieee80211_if_sta *ifsta,
115 int transaction, u8 *extra, size_t extra_len,
116 int encrypt)
117{
118 struct ieee80211_local *local = sdata->local;
119 struct sk_buff *skb;
120 struct ieee80211_mgmt *mgmt;
121
122 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
123 sizeof(*mgmt) + 6 + extra_len);
124 if (!skb) {
125 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
126 "frame\n", sdata->dev->name);
127 return;
128 }
129 skb_reserve(skb, local->hw.extra_tx_headroom);
130
131 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
132 memset(mgmt, 0, 24 + 6);
133 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
134 IEEE80211_STYPE_AUTH);
135 if (encrypt)
136 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
137 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
138 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
139 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
140 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
141 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
142 ifsta->auth_transaction = transaction + 1;
143 mgmt->u.auth.status_code = cpu_to_le16(0);
144 if (extra)
145 memcpy(skb_put(skb, extra_len), extra, extra_len);
146
147 ieee80211_sta_tx(sdata, skb, encrypt);
148}
149
0a51b27e
JB
150void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
151 u8 *ssid, size_t ssid_len)
60f8b39c
JB
152{
153 struct ieee80211_local *local = sdata->local;
154 struct ieee80211_supported_band *sband;
155 struct sk_buff *skb;
156 struct ieee80211_mgmt *mgmt;
157 u8 *pos, *supp_rates, *esupp_rates = NULL;
158 int i;
159
160 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
161 if (!skb) {
162 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
163 "request\n", sdata->dev->name);
164 return;
165 }
166 skb_reserve(skb, local->hw.extra_tx_headroom);
167
168 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
169 memset(mgmt, 0, 24);
170 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
171 IEEE80211_STYPE_PROBE_REQ);
172 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
173 if (dst) {
174 memcpy(mgmt->da, dst, ETH_ALEN);
175 memcpy(mgmt->bssid, dst, ETH_ALEN);
176 } else {
177 memset(mgmt->da, 0xff, ETH_ALEN);
178 memset(mgmt->bssid, 0xff, ETH_ALEN);
179 }
180 pos = skb_put(skb, 2 + ssid_len);
181 *pos++ = WLAN_EID_SSID;
182 *pos++ = ssid_len;
183 memcpy(pos, ssid, ssid_len);
184
185 supp_rates = skb_put(skb, 2);
186 supp_rates[0] = WLAN_EID_SUPP_RATES;
187 supp_rates[1] = 0;
188 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
189
190 for (i = 0; i < sband->n_bitrates; i++) {
191 struct ieee80211_rate *rate = &sband->bitrates[i];
192 if (esupp_rates) {
193 pos = skb_put(skb, 1);
194 esupp_rates[1]++;
195 } else if (supp_rates[1] == 8) {
196 esupp_rates = skb_put(skb, 3);
197 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
198 esupp_rates[1] = 1;
199 pos = &esupp_rates[2];
200 } else {
201 pos = skb_put(skb, 1);
202 supp_rates[1]++;
203 }
204 *pos = rate->bitrate / 5;
205 }
206
207 ieee80211_sta_tx(sdata, skb, 0);
208}
209
9ac19a90
JB
210static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
211 struct ieee80211_if_sta *ifsta)
212{
213 struct ieee80211_local *local = sdata->local;
214 struct sk_buff *skb;
215 struct ieee80211_mgmt *mgmt;
216 u8 *pos, *ies, *ht_add_ie;
217 int i, len, count, rates_len, supp_rates_len;
218 u16 capab;
219 struct ieee80211_sta_bss *bss;
220 int wmm = 0;
221 struct ieee80211_supported_band *sband;
222 u64 rates = 0;
223
224 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
225 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
226 ifsta->ssid_len);
227 if (!skb) {
228 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
229 "frame\n", sdata->dev->name);
230 return;
231 }
232 skb_reserve(skb, local->hw.extra_tx_headroom);
233
234 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
235
236 capab = ifsta->capab;
237
238 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
239 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
240 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
241 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
242 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
243 }
244
245 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
246 local->hw.conf.channel->center_freq,
247 ifsta->ssid, ifsta->ssid_len);
248 if (bss) {
249 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
250 capab |= WLAN_CAPABILITY_PRIVACY;
251 if (bss->wmm_used)
252 wmm = 1;
253
254 /* get all rates supported by the device and the AP as
255 * some APs don't like getting a superset of their rates
256 * in the association request (e.g. D-Link DAP 1353 in
257 * b-only mode) */
258 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
259
260 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
261 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
262 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
263
264 ieee80211_rx_bss_put(local, bss);
265 } else {
266 rates = ~0;
267 rates_len = sband->n_bitrates;
268 }
269
270 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
271 memset(mgmt, 0, 24);
272 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
273 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
274 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
275
276 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
277 skb_put(skb, 10);
278 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
279 IEEE80211_STYPE_REASSOC_REQ);
280 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
281 mgmt->u.reassoc_req.listen_interval =
282 cpu_to_le16(local->hw.conf.listen_interval);
283 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
284 ETH_ALEN);
285 } else {
286 skb_put(skb, 4);
287 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
288 IEEE80211_STYPE_ASSOC_REQ);
289 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
290 mgmt->u.reassoc_req.listen_interval =
291 cpu_to_le16(local->hw.conf.listen_interval);
292 }
293
294 /* SSID */
295 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
296 *pos++ = WLAN_EID_SSID;
297 *pos++ = ifsta->ssid_len;
298 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
299
300 /* add all rates which were marked to be used above */
301 supp_rates_len = rates_len;
302 if (supp_rates_len > 8)
303 supp_rates_len = 8;
304
305 len = sband->n_bitrates;
306 pos = skb_put(skb, supp_rates_len + 2);
307 *pos++ = WLAN_EID_SUPP_RATES;
308 *pos++ = supp_rates_len;
309
310 count = 0;
311 for (i = 0; i < sband->n_bitrates; i++) {
312 if (BIT(i) & rates) {
313 int rate = sband->bitrates[i].bitrate;
314 *pos++ = (u8) (rate / 5);
315 if (++count == 8)
316 break;
317 }
318 }
319
320 if (rates_len > count) {
321 pos = skb_put(skb, rates_len - count + 2);
322 *pos++ = WLAN_EID_EXT_SUPP_RATES;
323 *pos++ = rates_len - count;
324
325 for (i++; i < sband->n_bitrates; i++) {
326 if (BIT(i) & rates) {
327 int rate = sband->bitrates[i].bitrate;
328 *pos++ = (u8) (rate / 5);
329 }
330 }
331 }
332
333 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
334 /* 1. power capabilities */
335 pos = skb_put(skb, 4);
336 *pos++ = WLAN_EID_PWR_CAPABILITY;
337 *pos++ = 2;
338 *pos++ = 0; /* min tx power */
339 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
340
341 /* 2. supported channels */
342 /* TODO: get this in reg domain format */
343 pos = skb_put(skb, 2 * sband->n_channels + 2);
344 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
345 *pos++ = 2 * sband->n_channels;
346 for (i = 0; i < sband->n_channels; i++) {
347 *pos++ = ieee80211_frequency_to_channel(
348 sband->channels[i].center_freq);
349 *pos++ = 1; /* one channel in the subband*/
350 }
351 }
352
353 if (ifsta->extra_ie) {
354 pos = skb_put(skb, ifsta->extra_ie_len);
355 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
356 }
357
358 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
359 pos = skb_put(skb, 9);
360 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
361 *pos++ = 7; /* len */
362 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
363 *pos++ = 0x50;
364 *pos++ = 0xf2;
365 *pos++ = 2; /* WME */
366 *pos++ = 0; /* WME info */
367 *pos++ = 1; /* WME ver */
368 *pos++ = 0;
369 }
370
371 /* wmm support is a must to HT */
372 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
373 sband->ht_info.ht_supported &&
374 (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
375 struct ieee80211_ht_addt_info *ht_add_info =
376 (struct ieee80211_ht_addt_info *)ht_add_ie;
377 u16 cap = sband->ht_info.cap;
378 __le16 tmp;
379 u32 flags = local->hw.conf.channel->flags;
380
381 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
382 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
383 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
384 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
385 cap &= ~IEEE80211_HT_CAP_SGI_40;
386 }
387 break;
388 case IEEE80211_HT_IE_CHA_SEC_BELOW:
389 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
390 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
391 cap &= ~IEEE80211_HT_CAP_SGI_40;
392 }
393 break;
394 }
395
396 tmp = cpu_to_le16(cap);
397 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
398 *pos++ = WLAN_EID_HT_CAPABILITY;
399 *pos++ = sizeof(struct ieee80211_ht_cap);
400 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
401 memcpy(pos, &tmp, sizeof(u16));
402 pos += sizeof(u16);
403 /* TODO: needs a define here for << 2 */
404 *pos++ = sband->ht_info.ampdu_factor |
405 (sband->ht_info.ampdu_density << 2);
406 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
407 }
408
409 kfree(ifsta->assocreq_ies);
410 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
411 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
412 if (ifsta->assocreq_ies)
413 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
414
415 ieee80211_sta_tx(sdata, skb, 0);
416}
417
418
ef422bc0
JB
419static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
420 u16 stype, u16 reason)
9ac19a90
JB
421{
422 struct ieee80211_local *local = sdata->local;
ef422bc0 423 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
9ac19a90
JB
424 struct sk_buff *skb;
425 struct ieee80211_mgmt *mgmt;
426
427 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
428 if (!skb) {
ef422bc0
JB
429 printk(KERN_DEBUG "%s: failed to allocate buffer for "
430 "deauth/disassoc frame\n", sdata->dev->name);
9ac19a90
JB
431 return;
432 }
433 skb_reserve(skb, local->hw.extra_tx_headroom);
434
435 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
436 memset(mgmt, 0, 24);
437 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
438 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
439 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
ef422bc0 440 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
9ac19a90 441 skb_put(skb, 2);
ef422bc0 442 /* u.deauth.reason_code == u.disassoc.reason_code */
9ac19a90
JB
443 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
444
445 ieee80211_sta_tx(sdata, skb, 0);
446}
447
60f8b39c 448/* MLME */
f698d856 449static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
b079ada7 450 struct ieee80211_sta_bss *bss)
e2839d8f 451{
e2839d8f
VK
452 struct ieee80211_local *local = sdata->local;
453 int i, have_higher_than_11mbit = 0;
454
e2839d8f
VK
455 /* cf. IEEE 802.11 9.2.12 */
456 for (i = 0; i < bss->supp_rates_len; i++)
457 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
458 have_higher_than_11mbit = 1;
459
460 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
461 have_higher_than_11mbit)
462 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
463 else
464 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
465
3d35f7c6 466 ieee80211_set_wmm_default(sdata);
e2839d8f
VK
467}
468
f698d856 469static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
f0706e82
JB
470 struct ieee80211_if_sta *ifsta,
471 u8 *wmm_param, size_t wmm_param_len)
472{
f0706e82
JB
473 struct ieee80211_tx_queue_params params;
474 size_t left;
475 int count;
476 u8 *pos;
477
3434fbd3
JB
478 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
479 return;
480
481 if (!wmm_param)
482 return;
483
f0706e82
JB
484 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
485 return;
486 count = wmm_param[6] & 0x0f;
487 if (count == ifsta->wmm_last_param_set)
488 return;
489 ifsta->wmm_last_param_set = count;
490
491 pos = wmm_param + 8;
492 left = wmm_param_len - 8;
493
494 memset(&params, 0, sizeof(params));
495
496 if (!local->ops->conf_tx)
497 return;
498
499 local->wmm_acm = 0;
500 for (; left >= 4; left -= 4, pos += 4) {
501 int aci = (pos[0] >> 5) & 0x03;
502 int acm = (pos[0] >> 4) & 0x01;
503 int queue;
504
505 switch (aci) {
506 case 1:
e100bb64 507 queue = 3;
988c0f72 508 if (acm)
f0706e82 509 local->wmm_acm |= BIT(0) | BIT(3);
f0706e82
JB
510 break;
511 case 2:
e100bb64 512 queue = 1;
988c0f72 513 if (acm)
f0706e82 514 local->wmm_acm |= BIT(4) | BIT(5);
f0706e82
JB
515 break;
516 case 3:
e100bb64 517 queue = 0;
988c0f72 518 if (acm)
f0706e82 519 local->wmm_acm |= BIT(6) | BIT(7);
f0706e82
JB
520 break;
521 case 0:
522 default:
e100bb64 523 queue = 2;
988c0f72 524 if (acm)
f0706e82 525 local->wmm_acm |= BIT(1) | BIT(2);
f0706e82
JB
526 break;
527 }
528
529 params.aifs = pos[0] & 0x0f;
530 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
531 params.cw_min = ecw2cw(pos[1] & 0x0f);
f434b2d1 532 params.txop = get_unaligned_le16(pos + 2);
f4ea83dd 533#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
f0706e82 534 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
3330d7be 535 "cWmin=%d cWmax=%d txop=%d\n",
f698d856 536 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
3330d7be
JB
537 params.cw_max, params.txop);
538#endif
f0706e82
JB
539 /* TODO: handle ACM (block TX, fallback to next lowest allowed
540 * AC for now) */
541 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
542 printk(KERN_DEBUG "%s: failed to set TX queue "
f698d856 543 "parameters for queue %d\n", local->mdev->name, queue);
f0706e82
JB
544 }
545 }
546}
547
50c4afb9
JL
548static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
549 bool use_protection,
550 bool use_short_preamble)
5628221c 551{
471b3efd 552 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
ebd74487 553#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
5628221c 554 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
0795af57 555 DECLARE_MAC_BUF(mac);
ebd74487 556#endif
471b3efd 557 u32 changed = 0;
5628221c 558
471b3efd 559 if (use_protection != bss_conf->use_cts_prot) {
f4ea83dd 560#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
5628221c
DD
561 if (net_ratelimit()) {
562 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
0795af57 563 "%s)\n",
471b3efd 564 sdata->dev->name,
5628221c 565 use_protection ? "enabled" : "disabled",
0795af57 566 print_mac(mac, ifsta->bssid));
5628221c 567 }
f4ea83dd 568#endif
471b3efd
JB
569 bss_conf->use_cts_prot = use_protection;
570 changed |= BSS_CHANGED_ERP_CTS_PROT;
5628221c 571 }
7e9ed188 572
d43c7b37 573 if (use_short_preamble != bss_conf->use_short_preamble) {
f4ea83dd 574#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
7e9ed188
DD
575 if (net_ratelimit()) {
576 printk(KERN_DEBUG "%s: switched to %s barker preamble"
0795af57 577 " (BSSID=%s)\n",
471b3efd 578 sdata->dev->name,
d43c7b37 579 use_short_preamble ? "short" : "long",
0795af57 580 print_mac(mac, ifsta->bssid));
7e9ed188 581 }
f4ea83dd 582#endif
d43c7b37 583 bss_conf->use_short_preamble = use_short_preamble;
471b3efd 584 changed |= BSS_CHANGED_ERP_PREAMBLE;
7e9ed188 585 }
d9430a32 586
471b3efd 587 return changed;
5628221c
DD
588}
589
50c4afb9
JL
590static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
591 u8 erp_value)
592{
593 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
594 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
595
596 return ieee80211_handle_protect_preamb(sdata,
597 use_protection, use_short_preamble);
598}
599
600static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
601 struct ieee80211_sta_bss *bss)
602{
603 u32 changed = 0;
604
605 if (bss->has_erp_value)
606 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
607 else {
608 u16 capab = bss->capability;
609 changed |= ieee80211_handle_protect_preamb(sdata, false,
610 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
611 }
612
613 return changed;
614}
615
f5e5bf25
TW
616static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
617 struct ieee80211_if_sta *ifsta)
618{
619 union iwreq_data wrqu;
620 memset(&wrqu, 0, sizeof(wrqu));
621 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
622 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
623 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
624 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
625}
626
f698d856 627static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
628 struct ieee80211_if_sta *ifsta)
629{
f0706e82
JB
630 union iwreq_data wrqu;
631
f0706e82 632 if (ifsta->assocreq_ies) {
087d833e
JM
633 memset(&wrqu, 0, sizeof(wrqu));
634 wrqu.data.length = ifsta->assocreq_ies_len;
b171e19e 635 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
087d833e 636 ifsta->assocreq_ies);
f0706e82 637 }
087d833e
JM
638 if (ifsta->assocresp_ies) {
639 memset(&wrqu, 0, sizeof(wrqu));
640 wrqu.data.length = ifsta->assocresp_ies_len;
b171e19e 641 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
087d833e 642 ifsta->assocresp_ies);
f0706e82 643 }
f0706e82
JB
644}
645
646
f698d856 647static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
f5e5bf25 648 struct ieee80211_if_sta *ifsta)
f0706e82 649{
471b3efd 650 struct ieee80211_local *local = sdata->local;
38668c05 651 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
471b3efd 652 u32 changed = BSS_CHANGED_ASSOC;
f0706e82 653
f5e5bf25 654 struct ieee80211_sta_bss *bss;
d6f2da5b 655
f5e5bf25 656 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
5628221c 657
f5e5bf25
TW
658 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
659 return;
21c0cbe7 660
f5e5bf25
TW
661 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
662 conf->channel->center_freq,
663 ifsta->ssid, ifsta->ssid_len);
664 if (bss) {
665 /* set timing information */
666 sdata->bss_conf.beacon_int = bss->beacon_int;
667 sdata->bss_conf.timestamp = bss->timestamp;
668 sdata->bss_conf.dtim_period = bss->dtim_period;
21c0cbe7 669
f5e5bf25 670 changed |= ieee80211_handle_bss_capability(sdata, bss);
9ac19a90
JB
671
672 ieee80211_rx_bss_put(local, bss);
f0706e82
JB
673 }
674
9ac19a90
JB
675 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
676 changed |= BSS_CHANGED_HT;
677 sdata->bss_conf.assoc_ht = 1;
678 sdata->bss_conf.ht_conf = &conf->ht_conf;
679 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
f0706e82 680 }
3434fbd3 681
9ac19a90
JB
682 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
683 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
684 ieee80211_sta_send_associnfo(sdata, ifsta);
9306102e 685
9ac19a90
JB
686 ifsta->last_probe = jiffies;
687 ieee80211_led_assoc(local, 1);
9306102e 688
9ac19a90
JB
689 sdata->bss_conf.assoc = 1;
690 ieee80211_bss_info_change_notify(sdata, changed);
f0706e82 691
9ac19a90
JB
692 netif_tx_start_all_queues(sdata->dev);
693 netif_carrier_on(sdata->dev);
f0706e82 694
9ac19a90 695 ieee80211_sta_send_apinfo(sdata, ifsta);
f0706e82
JB
696}
697
9ac19a90
JB
698static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
699 struct ieee80211_if_sta *ifsta)
f0706e82 700{
9ac19a90 701 DECLARE_MAC_BUF(mac);
f0706e82 702
9ac19a90
JB
703 ifsta->direct_probe_tries++;
704 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
705 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
706 sdata->dev->name, print_mac(mac, ifsta->bssid));
707 ifsta->state = IEEE80211_STA_MLME_DISABLED;
f0706e82
JB
708 return;
709 }
f0706e82 710
9ac19a90
JB
711 printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
712 sdata->dev->name, print_mac(mac, ifsta->bssid),
713 ifsta->direct_probe_tries);
f0706e82 714
9ac19a90 715 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
f0706e82 716
9ac19a90
JB
717 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
718
719 /* Direct probe is sent to broadcast address as some APs
720 * will not answer to direct packet in unassociated state.
721 */
722 ieee80211_send_probe_req(sdata, NULL,
723 ifsta->ssid, ifsta->ssid_len);
724
725 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
60f8b39c 726}
f0706e82 727
9ac19a90
JB
728
729static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
730 struct ieee80211_if_sta *ifsta)
f0706e82 731{
9ac19a90 732 DECLARE_MAC_BUF(mac);
f0706e82 733
9ac19a90
JB
734 ifsta->auth_tries++;
735 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
736 printk(KERN_DEBUG "%s: authentication with AP %s"
737 " timed out\n",
738 sdata->dev->name, print_mac(mac, ifsta->bssid));
739 ifsta->state = IEEE80211_STA_MLME_DISABLED;
f0706e82
JB
740 return;
741 }
f0706e82 742
9ac19a90
JB
743 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
744 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
745 sdata->dev->name, print_mac(mac, ifsta->bssid));
f0706e82 746
9ac19a90
JB
747 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
748
749 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
f0706e82
JB
750}
751
aa458d17
TW
752static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
753 struct ieee80211_if_sta *ifsta, bool deauth,
754 bool self_disconnected, u16 reason)
755{
756 struct ieee80211_local *local = sdata->local;
757 struct sta_info *sta;
f5e5bf25 758 u32 changed = BSS_CHANGED_ASSOC;
aa458d17
TW
759
760 rcu_read_lock();
761
762 sta = sta_info_get(local, ifsta->bssid);
763 if (!sta) {
764 rcu_read_unlock();
765 return;
766 }
767
768 if (deauth) {
769 ifsta->direct_probe_tries = 0;
770 ifsta->auth_tries = 0;
771 }
772 ifsta->assoc_scan_tries = 0;
773 ifsta->assoc_tries = 0;
774
24e64622 775 netif_tx_stop_all_queues(sdata->dev);
aa458d17
TW
776 netif_carrier_off(sdata->dev);
777
778 ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
779
780 if (self_disconnected) {
781 if (deauth)
ef422bc0
JB
782 ieee80211_send_deauth_disassoc(sdata,
783 IEEE80211_STYPE_DEAUTH, reason);
aa458d17 784 else
ef422bc0
JB
785 ieee80211_send_deauth_disassoc(sdata,
786 IEEE80211_STYPE_DISASSOC, reason);
aa458d17
TW
787 }
788
f5e5bf25
TW
789 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
790 changed |= ieee80211_reset_erp_info(sdata);
791
792 if (sdata->bss_conf.assoc_ht)
793 changed |= BSS_CHANGED_HT;
794
795 sdata->bss_conf.assoc_ht = 0;
796 sdata->bss_conf.ht_conf = NULL;
797 sdata->bss_conf.ht_bss_conf = NULL;
798
799 ieee80211_led_assoc(local, 0);
800 sdata->bss_conf.assoc = 0;
801
802 ieee80211_sta_send_apinfo(sdata, ifsta);
aa458d17
TW
803
804 if (self_disconnected)
805 ifsta->state = IEEE80211_STA_MLME_DISABLED;
806
807 sta_info_unlink(&sta);
808
809 rcu_read_unlock();
810
811 sta_info_destroy(sta);
812}
f0706e82 813
9ac19a90
JB
814static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
815{
816 if (!sdata || !sdata->default_key ||
817 sdata->default_key->conf.alg != ALG_WEP)
818 return 0;
819 return 1;
820}
821
f698d856 822static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
823 struct ieee80211_if_sta *ifsta)
824{
f698d856 825 struct ieee80211_local *local = sdata->local;
f0706e82 826 struct ieee80211_sta_bss *bss;
5b98b1f7
JB
827 int bss_privacy;
828 int wep_privacy;
829 int privacy_invoked;
f0706e82 830
5b98b1f7 831 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
f0706e82
JB
832 return 0;
833
f698d856 834 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
8318d78a 835 local->hw.conf.channel->center_freq,
cffdd30d 836 ifsta->ssid, ifsta->ssid_len);
f0706e82
JB
837 if (!bss)
838 return 0;
839
5b98b1f7 840 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
f698d856 841 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
5b98b1f7 842 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
f0706e82 843
3e122be0 844 ieee80211_rx_bss_put(local, bss);
f0706e82 845
5b98b1f7
JB
846 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
847 return 0;
848
849 return 1;
f0706e82
JB
850}
851
f698d856 852static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
853 struct ieee80211_if_sta *ifsta)
854{
0795af57
JP
855 DECLARE_MAC_BUF(mac);
856
f0706e82
JB
857 ifsta->assoc_tries++;
858 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
0795af57 859 printk(KERN_DEBUG "%s: association with AP %s"
f0706e82 860 " timed out\n",
f698d856 861 sdata->dev->name, print_mac(mac, ifsta->bssid));
48c2fc59 862 ifsta->state = IEEE80211_STA_MLME_DISABLED;
f0706e82
JB
863 return;
864 }
865
48c2fc59 866 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
0795af57 867 printk(KERN_DEBUG "%s: associate with AP %s\n",
f698d856
JBG
868 sdata->dev->name, print_mac(mac, ifsta->bssid));
869 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
f0706e82 870 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
f698d856 871 "mixed-cell disabled - abort association\n", sdata->dev->name);
48c2fc59 872 ifsta->state = IEEE80211_STA_MLME_DISABLED;
f0706e82
JB
873 return;
874 }
875
f698d856 876 ieee80211_send_assoc(sdata, ifsta);
f0706e82
JB
877
878 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
879}
880
881
f698d856 882static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
883 struct ieee80211_if_sta *ifsta)
884{
f698d856 885 struct ieee80211_local *local = sdata->local;
f0706e82
JB
886 struct sta_info *sta;
887 int disassoc;
0795af57 888 DECLARE_MAC_BUF(mac);
f0706e82
JB
889
890 /* TODO: start monitoring current AP signal quality and number of
891 * missed beacons. Scan other channels every now and then and search
892 * for better APs. */
893 /* TODO: remove expired BSSes */
894
48c2fc59 895 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
f0706e82 896
d0709a65
JB
897 rcu_read_lock();
898
f0706e82
JB
899 sta = sta_info_get(local, ifsta->bssid);
900 if (!sta) {
0795af57 901 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
f698d856 902 sdata->dev->name, print_mac(mac, ifsta->bssid));
f0706e82
JB
903 disassoc = 1;
904 } else {
905 disassoc = 0;
906 if (time_after(jiffies,
907 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
d6f2da5b 908 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
f0706e82 909 printk(KERN_DEBUG "%s: No ProbeResp from "
0795af57 910 "current AP %s - assume out of "
f0706e82 911 "range\n",
f698d856 912 sdata->dev->name, print_mac(mac, ifsta->bssid));
f0706e82 913 disassoc = 1;
d6f2da5b 914 } else
f698d856 915 ieee80211_send_probe_req(sdata, ifsta->bssid,
f0706e82
JB
916 local->scan_ssid,
917 local->scan_ssid_len);
d6f2da5b 918 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
f0706e82 919 } else {
d6f2da5b 920 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
f0706e82
JB
921 if (time_after(jiffies, ifsta->last_probe +
922 IEEE80211_PROBE_INTERVAL)) {
923 ifsta->last_probe = jiffies;
f698d856 924 ieee80211_send_probe_req(sdata, ifsta->bssid,
f0706e82
JB
925 ifsta->ssid,
926 ifsta->ssid_len);
927 }
928 }
f0706e82 929 }
d0709a65
JB
930
931 rcu_read_unlock();
932
60f8b39c
JB
933 if (disassoc)
934 ieee80211_set_disassoc(sdata, ifsta, true, true,
935 WLAN_REASON_PREV_AUTH_NOT_VALID);
936 else
937 mod_timer(&ifsta->timer, jiffies +
938 IEEE80211_MONITORING_INTERVAL);
f0706e82
JB
939}
940
941
f698d856 942static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
943 struct ieee80211_if_sta *ifsta)
944{
f698d856 945 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
d6f2da5b 946 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
f698d856 947 ieee80211_associate(sdata, ifsta);
f0706e82
JB
948}
949
950
f698d856 951static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
952 struct ieee80211_if_sta *ifsta,
953 struct ieee80211_mgmt *mgmt,
954 size_t len)
955{
956 u8 *pos;
957 struct ieee802_11_elems elems;
958
f0706e82 959 pos = mgmt->u.auth.variable;
67a4cce4 960 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
f4ea83dd 961 if (!elems.challenge)
f0706e82 962 return;
f698d856 963 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
f0706e82
JB
964 elems.challenge_len + 2, 1);
965}
966
f698d856 967static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
968 struct ieee80211_if_sta *ifsta,
969 struct ieee80211_mgmt *mgmt,
970 size_t len)
971{
f0706e82 972 u16 auth_alg, auth_transaction, status_code;
0795af57 973 DECLARE_MAC_BUF(mac);
f0706e82 974
48c2fc59 975 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
f4ea83dd 976 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
f0706e82 977 return;
f0706e82 978
f4ea83dd 979 if (len < 24 + 6)
f0706e82 980 return;
f0706e82 981
51fb61e7 982 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
f4ea83dd 983 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
f0706e82 984 return;
f0706e82 985
51fb61e7 986 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
f4ea83dd 987 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
f0706e82 988 return;
f0706e82
JB
989
990 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
991 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
992 status_code = le16_to_cpu(mgmt->u.auth.status_code);
993
51fb61e7 994 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
135a2110
JB
995 /*
996 * IEEE 802.11 standard does not require authentication in IBSS
f0706e82
JB
997 * networks and most implementations do not seem to use it.
998 * However, try to reply to authentication attempts if someone
999 * has actually implemented this.
135a2110 1000 */
f4ea83dd 1001 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
f0706e82 1002 return;
f698d856 1003 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
f0706e82
JB
1004 }
1005
1006 if (auth_alg != ifsta->auth_alg ||
f4ea83dd 1007 auth_transaction != ifsta->auth_transaction)
f0706e82 1008 return;
f0706e82
JB
1009
1010 if (status_code != WLAN_STATUS_SUCCESS) {
f0706e82
JB
1011 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1012 u8 algs[3];
1013 const int num_algs = ARRAY_SIZE(algs);
1014 int i, pos;
1015 algs[0] = algs[1] = algs[2] = 0xff;
1016 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1017 algs[0] = WLAN_AUTH_OPEN;
1018 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1019 algs[1] = WLAN_AUTH_SHARED_KEY;
1020 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1021 algs[2] = WLAN_AUTH_LEAP;
1022 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1023 pos = 0;
1024 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1025 pos = 1;
1026 else
1027 pos = 2;
1028 for (i = 0; i < num_algs; i++) {
1029 pos++;
1030 if (pos >= num_algs)
1031 pos = 0;
1032 if (algs[pos] == ifsta->auth_alg ||
1033 algs[pos] == 0xff)
1034 continue;
1035 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
f698d856 1036 !ieee80211_sta_wep_configured(sdata))
f0706e82
JB
1037 continue;
1038 ifsta->auth_alg = algs[pos];
f0706e82
JB
1039 break;
1040 }
1041 }
1042 return;
1043 }
1044
1045 switch (ifsta->auth_alg) {
1046 case WLAN_AUTH_OPEN:
1047 case WLAN_AUTH_LEAP:
f698d856 1048 ieee80211_auth_completed(sdata, ifsta);
f0706e82
JB
1049 break;
1050 case WLAN_AUTH_SHARED_KEY:
1051 if (ifsta->auth_transaction == 4)
f698d856 1052 ieee80211_auth_completed(sdata, ifsta);
f0706e82 1053 else
f698d856 1054 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
f0706e82
JB
1055 break;
1056 }
1057}
1058
1059
f698d856 1060static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1061 struct ieee80211_if_sta *ifsta,
1062 struct ieee80211_mgmt *mgmt,
1063 size_t len)
1064{
1065 u16 reason_code;
0795af57 1066 DECLARE_MAC_BUF(mac);
f0706e82 1067
f4ea83dd 1068 if (len < 24 + 2)
f0706e82 1069 return;
f0706e82 1070
f4ea83dd 1071 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
f0706e82 1072 return;
f0706e82
JB
1073
1074 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1075
988c0f72 1076 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
f698d856 1077 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
f0706e82 1078
48c2fc59
TW
1079 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1080 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1081 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
9859b81e 1082 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
f0706e82
JB
1083 mod_timer(&ifsta->timer, jiffies +
1084 IEEE80211_RETRY_AUTH_INTERVAL);
1085 }
1086
aa458d17 1087 ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
d6f2da5b 1088 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
f0706e82
JB
1089}
1090
1091
f698d856 1092static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1093 struct ieee80211_if_sta *ifsta,
1094 struct ieee80211_mgmt *mgmt,
1095 size_t len)
1096{
1097 u16 reason_code;
0795af57 1098 DECLARE_MAC_BUF(mac);
f0706e82 1099
f4ea83dd 1100 if (len < 24 + 2)
f0706e82 1101 return;
f0706e82 1102
f4ea83dd 1103 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
f0706e82 1104 return;
f0706e82
JB
1105
1106 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1107
d6f2da5b 1108 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
f698d856 1109 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
f0706e82 1110
48c2fc59
TW
1111 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1112 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
f0706e82
JB
1113 mod_timer(&ifsta->timer, jiffies +
1114 IEEE80211_RETRY_AUTH_INTERVAL);
1115 }
1116
aa458d17 1117 ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
f0706e82
JB
1118}
1119
1120
471b3efd 1121static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1122 struct ieee80211_if_sta *ifsta,
1123 struct ieee80211_mgmt *mgmt,
1124 size_t len,
1125 int reassoc)
1126{
471b3efd 1127 struct ieee80211_local *local = sdata->local;
8318d78a 1128 struct ieee80211_supported_band *sband;
f0706e82 1129 struct sta_info *sta;
8318d78a 1130 u64 rates, basic_rates;
f0706e82
JB
1131 u16 capab_info, status_code, aid;
1132 struct ieee802_11_elems elems;
471b3efd 1133 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
f0706e82
JB
1134 u8 *pos;
1135 int i, j;
0795af57 1136 DECLARE_MAC_BUF(mac);
8318d78a 1137 bool have_higher_than_11mbit = false;
f0706e82
JB
1138
1139 /* AssocResp and ReassocResp have identical structure, so process both
1140 * of them in this function. */
1141
48c2fc59 1142 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
f0706e82 1143 return;
f0706e82 1144
f4ea83dd 1145 if (len < 24 + 6)
f0706e82 1146 return;
f0706e82 1147
f4ea83dd 1148 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
f0706e82 1149 return;
f0706e82
JB
1150
1151 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1152 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1153 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
f0706e82 1154
0795af57 1155 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
f0706e82 1156 "status=%d aid=%d)\n",
f698d856 1157 sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
ddd68587 1158 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
f0706e82
JB
1159
1160 if (status_code != WLAN_STATUS_SUCCESS) {
1161 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
f698d856 1162 sdata->dev->name, status_code);
8a69aa93
DD
1163 /* if this was a reassociation, ensure we try a "full"
1164 * association next time. This works around some broken APs
1165 * which do not correctly reject reassociation requests. */
d6f2da5b 1166 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
f0706e82
JB
1167 return;
1168 }
1169
1dd84aa2
JB
1170 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1171 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
f698d856 1172 "set\n", sdata->dev->name, aid);
1dd84aa2
JB
1173 aid &= ~(BIT(15) | BIT(14));
1174
f0706e82 1175 pos = mgmt->u.assoc_resp.variable;
67a4cce4 1176 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
f0706e82
JB
1177
1178 if (!elems.supp_rates) {
1179 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
f698d856 1180 sdata->dev->name);
f0706e82
JB
1181 return;
1182 }
1183
f698d856 1184 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
f0706e82
JB
1185 ifsta->aid = aid;
1186 ifsta->ap_capab = capab_info;
1187
1188 kfree(ifsta->assocresp_ies);
1189 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
0ec0b7ac 1190 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
f0706e82
JB
1191 if (ifsta->assocresp_ies)
1192 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1193
d0709a65
JB
1194 rcu_read_lock();
1195
f0706e82
JB
1196 /* Add STA entry for the AP */
1197 sta = sta_info_get(local, ifsta->bssid);
1198 if (!sta) {
1199 struct ieee80211_sta_bss *bss;
73651ee6 1200 int err;
d0709a65 1201
73651ee6
JB
1202 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1203 if (!sta) {
1204 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
f698d856 1205 " the AP\n", sdata->dev->name);
d0709a65 1206 rcu_read_unlock();
f0706e82
JB
1207 return;
1208 }
60f8b39c
JB
1209 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1210 local->hw.conf.channel->center_freq,
1211 ifsta->ssid, ifsta->ssid_len);
1212 if (bss) {
1213 sta->last_signal = bss->signal;
1214 sta->last_qual = bss->qual;
1215 sta->last_noise = bss->noise;
1216 ieee80211_rx_bss_put(local, bss);
1217 }
f709fc69 1218
60f8b39c
JB
1219 err = sta_info_insert(sta);
1220 if (err) {
1221 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1222 " the AP (error %d)\n", sdata->dev->name, err);
1223 rcu_read_unlock();
1224 return;
f709fc69 1225 }
60f8b39c
JB
1226 /* update new sta with its last rx activity */
1227 sta->last_rx = jiffies;
f709fc69 1228 }
f709fc69 1229
60f8b39c
JB
1230 /*
1231 * FIXME: Do we really need to update the sta_info's information here?
1232 * We already know about the AP (we found it in our list) so it
1233 * should already be filled with the right info, no?
1234 * As is stands, all this is racy because typically we assume
1235 * the information that is filled in here (except flags) doesn't
1236 * change while a STA structure is alive. As such, it should move
1237 * to between the sta_info_alloc() and sta_info_insert() above.
1238 */
f709fc69 1239
60f8b39c
JB
1240 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1241 WLAN_STA_AUTHORIZED);
05e5e883 1242
60f8b39c
JB
1243 rates = 0;
1244 basic_rates = 0;
1245 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
f709fc69 1246
60f8b39c
JB
1247 for (i = 0; i < elems.supp_rates_len; i++) {
1248 int rate = (elems.supp_rates[i] & 0x7f) * 5;
f709fc69 1249
60f8b39c
JB
1250 if (rate > 110)
1251 have_higher_than_11mbit = true;
1252
1253 for (j = 0; j < sband->n_bitrates; j++) {
1254 if (sband->bitrates[j].bitrate == rate)
1255 rates |= BIT(j);
1256 if (elems.supp_rates[i] & 0x80)
1257 basic_rates |= BIT(j);
f709fc69 1258 }
f709fc69
LCC
1259 }
1260
60f8b39c
JB
1261 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1262 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
f0706e82 1263
60f8b39c
JB
1264 if (rate > 110)
1265 have_higher_than_11mbit = true;
f0706e82 1266
60f8b39c
JB
1267 for (j = 0; j < sband->n_bitrates; j++) {
1268 if (sband->bitrates[j].bitrate == rate)
1269 rates |= BIT(j);
1270 if (elems.ext_supp_rates[i] & 0x80)
1271 basic_rates |= BIT(j);
1272 }
1ebebea8 1273 }
f0706e82 1274
60f8b39c
JB
1275 sta->supp_rates[local->hw.conf.channel->band] = rates;
1276 sdata->basic_rates = basic_rates;
1277
1278 /* cf. IEEE 802.11 9.2.12 */
1279 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1280 have_higher_than_11mbit)
1281 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1282 else
1283 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
f0706e82 1284
60f8b39c
JB
1285 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1286 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1287 struct ieee80211_ht_bss_info bss_info;
1288 ieee80211_ht_cap_ie_to_ht_info(
1289 (struct ieee80211_ht_cap *)
1290 elems.ht_cap_elem, &sta->ht_info);
1291 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1292 (struct ieee80211_ht_addt_info *)
1293 elems.ht_info_elem, &bss_info);
1294 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
1295 }
f0706e82 1296
60f8b39c 1297 rate_control_rate_init(sta, local);
f0706e82 1298
60f8b39c
JB
1299 if (elems.wmm_param) {
1300 set_sta_flags(sta, WLAN_STA_WME);
1301 rcu_read_unlock();
1302 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1303 elems.wmm_param_len);
1304 } else
1305 rcu_read_unlock();
f0706e82 1306
60f8b39c
JB
1307 /* set AID and assoc capability,
1308 * ieee80211_set_associated() will tell the driver */
1309 bss_conf->aid = aid;
1310 bss_conf->assoc_capability = capab_info;
1311 ieee80211_set_associated(sdata, ifsta);
f0706e82 1312
60f8b39c 1313 ieee80211_associated(sdata, ifsta);
f0706e82
JB
1314}
1315
1316
f698d856 1317static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
a607268a
BR
1318 struct ieee80211_if_sta *ifsta,
1319 struct ieee80211_sta_bss *bss)
1320{
f698d856 1321 struct ieee80211_local *local = sdata->local;
a607268a
BR
1322 int res, rates, i, j;
1323 struct sk_buff *skb;
1324 struct ieee80211_mgmt *mgmt;
a607268a 1325 u8 *pos;
a607268a 1326 struct ieee80211_supported_band *sband;
507b06d0 1327 union iwreq_data wrqu;
a607268a
BR
1328
1329 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1330
1331 /* Remove possible STA entries from other IBSS networks. */
dc6676b7 1332 sta_info_flush_delayed(sdata);
a607268a
BR
1333
1334 if (local->ops->reset_tsf) {
1335 /* Reset own TSF to allow time synchronization work. */
1336 local->ops->reset_tsf(local_to_hw(local));
1337 }
1338 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
9d139c81 1339 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
a607268a
BR
1340 if (res)
1341 return res;
1342
1343 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
1344
a607268a
BR
1345 sdata->drop_unencrypted = bss->capability &
1346 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
1347
f698d856 1348 res = ieee80211_set_freq(sdata, bss->freq);
a607268a 1349
be038b37
AK
1350 if (res)
1351 return res;
a607268a 1352
9d139c81 1353 /* Build IBSS probe response */
a607268a 1354 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
9d139c81 1355 if (skb) {
a607268a
BR
1356 skb_reserve(skb, local->hw.extra_tx_headroom);
1357
1358 mgmt = (struct ieee80211_mgmt *)
1359 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1360 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
e7827a70
HH
1361 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1362 IEEE80211_STYPE_PROBE_RESP);
a607268a 1363 memset(mgmt->da, 0xff, ETH_ALEN);
f698d856 1364 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
a607268a
BR
1365 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1366 mgmt->u.beacon.beacon_int =
1367 cpu_to_le16(local->hw.conf.beacon_int);
4faeb860 1368 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
a607268a
BR
1369 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
1370
1371 pos = skb_put(skb, 2 + ifsta->ssid_len);
1372 *pos++ = WLAN_EID_SSID;
1373 *pos++ = ifsta->ssid_len;
1374 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
1375
1376 rates = bss->supp_rates_len;
1377 if (rates > 8)
1378 rates = 8;
1379 pos = skb_put(skb, 2 + rates);
1380 *pos++ = WLAN_EID_SUPP_RATES;
1381 *pos++ = rates;
1382 memcpy(pos, bss->supp_rates, rates);
1383
1384 if (bss->band == IEEE80211_BAND_2GHZ) {
1385 pos = skb_put(skb, 2 + 1);
1386 *pos++ = WLAN_EID_DS_PARAMS;
1387 *pos++ = 1;
1388 *pos++ = ieee80211_frequency_to_channel(bss->freq);
1389 }
1390
1391 pos = skb_put(skb, 2 + 2);
1392 *pos++ = WLAN_EID_IBSS_PARAMS;
1393 *pos++ = 2;
1394 /* FIX: set ATIM window based on scan results */
1395 *pos++ = 0;
1396 *pos++ = 0;
1397
1398 if (bss->supp_rates_len > 8) {
1399 rates = bss->supp_rates_len - 8;
1400 pos = skb_put(skb, 2 + rates);
1401 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1402 *pos++ = rates;
1403 memcpy(pos, &bss->supp_rates[8], rates);
1404 }
1405
9d139c81 1406 ifsta->probe_resp = skb;
e039fa4a 1407
9d139c81
JB
1408 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1409 }
a607268a 1410
9d139c81
JB
1411 rates = 0;
1412 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1413 for (i = 0; i < bss->supp_rates_len; i++) {
1414 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
1415 for (j = 0; j < sband->n_bitrates; j++)
1416 if (sband->bitrates[j].bitrate == bitrate)
1417 rates |= BIT(j);
a607268a 1418 }
9d139c81
JB
1419 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
1420
b079ada7 1421 ieee80211_sta_def_wmm_params(sdata, bss);
a607268a 1422
48c2fc59 1423 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
a607268a
BR
1424 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1425
507b06d0
DW
1426 memset(&wrqu, 0, sizeof(wrqu));
1427 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
f698d856 1428 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
a607268a
BR
1429
1430 return res;
1431}
1432
f709fc69
LCC
1433u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
1434 struct ieee802_11_elems *elems,
1435 enum ieee80211_band band)
1436{
1437 struct ieee80211_supported_band *sband;
1438 struct ieee80211_rate *bitrates;
1439 size_t num_rates;
1440 u64 supp_rates;
1441 int i, j;
1442 sband = local->hw.wiphy->bands[band];
1443
1444 if (!sband) {
1445 WARN_ON(1);
1446 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1447 }
1448
1449 bitrates = sband->bitrates;
1450 num_rates = sband->n_bitrates;
1451 supp_rates = 0;
1452 for (i = 0; i < elems->supp_rates_len +
1453 elems->ext_supp_rates_len; i++) {
1454 u8 rate = 0;
1455 int own_rate;
1456 if (i < elems->supp_rates_len)
1457 rate = elems->supp_rates[i];
1458 else if (elems->ext_supp_rates)
1459 rate = elems->ext_supp_rates
1460 [i - elems->supp_rates_len];
1461 own_rate = 5 * (rate & 0x7f);
1462 for (j = 0; j < num_rates; j++)
1463 if (bitrates[j].bitrate == own_rate)
1464 supp_rates |= BIT(j);
1465 }
1466 return supp_rates;
1467}
1468
8e1535d5
EG
1469static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
1470 enum ieee80211_band band)
1471{
1472 struct ieee80211_supported_band *sband;
1473 struct ieee80211_rate *bitrates;
1474 u64 mandatory_rates;
1475 enum ieee80211_rate_flags mandatory_flag;
1476 int i;
1477
1478 sband = local->hw.wiphy->bands[band];
1479 if (!sband) {
1480 WARN_ON(1);
1481 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1482 }
1483
1484 if (band == IEEE80211_BAND_2GHZ)
1485 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
1486 else
1487 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
1488
1489 bitrates = sband->bitrates;
1490 mandatory_rates = 0;
1491 for (i = 0; i < sband->n_bitrates; i++)
1492 if (bitrates[i].flags & mandatory_flag)
1493 mandatory_rates |= BIT(i);
1494 return mandatory_rates;
1495}
a607268a 1496
98c8fccf
JB
1497static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1498 struct ieee80211_mgmt *mgmt,
1499 size_t len,
1500 struct ieee80211_rx_status *rx_status,
1501 struct ieee802_11_elems *elems,
1502 bool beacon)
1503{
1504 struct ieee80211_local *local = sdata->local;
1505 int freq;
1506 struct ieee80211_sta_bss *bss;
1507 struct sta_info *sta;
1508 struct ieee80211_channel *channel;
1509 u64 beacon_timestamp, rx_timestamp;
1510 u64 supp_rates = 0;
1511 enum ieee80211_band band = rx_status->band;
1512 DECLARE_MAC_BUF(mac);
1513 DECLARE_MAC_BUF(mac2);
1514
1515 if (elems->ds_params && elems->ds_params_len == 1)
1516 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1517 else
1518 freq = rx_status->freq;
1519
1520 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1521
1522 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1523 return;
1524
1525 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
1526 elems->mesh_config && mesh_matches_local(elems, sdata)) {
1527 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1528
1529 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
1530 mesh_peer_accepts_plinks(elems));
1531 }
1532
1533 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
1534 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1535 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1536
1537 rcu_read_lock();
1538
1539 sta = sta_info_get(local, mgmt->sa);
1540 if (sta) {
1541 u64 prev_rates;
1542
1543 prev_rates = sta->supp_rates[band];
1544 /* make sure mandatory rates are always added */
1545 sta->supp_rates[band] = supp_rates |
1546 ieee80211_sta_get_mandatory_rates(local, band);
1547
1548#ifdef CONFIG_MAC80211_IBSS_DEBUG
1549 if (sta->supp_rates[band] != prev_rates)
1550 printk(KERN_DEBUG "%s: updated supp_rates set "
1551 "for %s based on beacon info (0x%llx | "
1552 "0x%llx -> 0x%llx)\n",
1553 sdata->dev->name, print_mac(mac, sta->addr),
1554 (unsigned long long) prev_rates,
1555 (unsigned long long) supp_rates,
1556 (unsigned long long) sta->supp_rates[band]);
1557#endif
1558 } else {
1559 ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
1560 mgmt->sa, supp_rates);
1561 }
1562
1563 rcu_read_unlock();
1564 }
1565
1566 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1567 freq, beacon);
1568 if (!bss)
1569 return;
1570
1571 /* was just updated in ieee80211_bss_info_update */
1572 beacon_timestamp = bss->timestamp;
1573
30b89b0f
JB
1574 /*
1575 * In STA mode, the remaining parameters should not be overridden
1576 * by beacons because they're not necessarily accurate there.
1577 */
1578 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
9859b81e 1579 bss->last_probe_resp && beacon) {
3e122be0 1580 ieee80211_rx_bss_put(local, bss);
30b89b0f
JB
1581 return;
1582 }
1583
9d9bf77d
BR
1584 /* check if we need to merge IBSS */
1585 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
fba4a1e6 1586 bss->capability & WLAN_CAPABILITY_IBSS &&
9d9bf77d 1587 bss->freq == local->oper_channel->center_freq &&
ae6a44e3
EK
1588 elems->ssid_len == sdata->u.sta.ssid_len &&
1589 memcmp(elems->ssid, sdata->u.sta.ssid,
1590 sdata->u.sta.ssid_len) == 0) {
9d9bf77d
BR
1591 if (rx_status->flag & RX_FLAG_TSFT) {
1592 /* in order for correct IBSS merging we need mactime
1593 *
1594 * since mactime is defined as the time the first data
1595 * symbol of the frame hits the PHY, and the timestamp
1596 * of the beacon is defined as "the time that the data
1597 * symbol containing the first bit of the timestamp is
1598 * transmitted to the PHY plus the transmitting STA’s
1599 * delays through its local PHY from the MAC-PHY
1600 * interface to its interface with the WM"
1601 * (802.11 11.1.2) - equals the time this bit arrives at
1602 * the receiver - we have to take into account the
1603 * offset between the two.
1604 * e.g: at 1 MBit that means mactime is 192 usec earlier
1605 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
1606 */
8e1535d5 1607 int rate = local->hw.wiphy->bands[band]->
9d9bf77d
BR
1608 bitrates[rx_status->rate_idx].bitrate;
1609 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
1610 } else if (local && local->ops && local->ops->get_tsf)
1611 /* second best option: get current TSF */
1612 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
1613 else
1614 /* can't merge without knowing the TSF */
1615 rx_timestamp = -1LLU;
1616#ifdef CONFIG_MAC80211_IBSS_DEBUG
1617 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1618 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
1619 print_mac(mac, mgmt->sa),
1620 print_mac(mac2, mgmt->bssid),
1621 (unsigned long long)rx_timestamp,
1622 (unsigned long long)beacon_timestamp,
1623 (unsigned long long)(rx_timestamp - beacon_timestamp),
1624 jiffies);
1625#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1626 if (beacon_timestamp > rx_timestamp) {
576fdeae 1627#ifdef CONFIG_MAC80211_IBSS_DEBUG
f4ea83dd
JB
1628 printk(KERN_DEBUG "%s: beacon TSF higher than "
1629 "local TSF - IBSS merge with BSSID %s\n",
f698d856 1630 sdata->dev->name, print_mac(mac, mgmt->bssid));
fba4a1e6 1631#endif
f698d856
JBG
1632 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
1633 ieee80211_ibss_add_sta(sdata, NULL,
87291c02 1634 mgmt->bssid, mgmt->sa,
8e1535d5 1635 supp_rates);
9d9bf77d
BR
1636 }
1637 }
1638
3e122be0 1639 ieee80211_rx_bss_put(local, bss);
f0706e82
JB
1640}
1641
1642
f698d856 1643static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1644 struct ieee80211_mgmt *mgmt,
1645 size_t len,
1646 struct ieee80211_rx_status *rx_status)
1647{
ae6a44e3
EK
1648 size_t baselen;
1649 struct ieee802_11_elems elems;
9859b81e 1650 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
ae6a44e3 1651
8e7cdbb6
TW
1652 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1653 return; /* ignore ProbeResp to foreign address */
1654
ae6a44e3
EK
1655 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1656 if (baselen > len)
1657 return;
1658
1659 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1660 &elems);
1661
98c8fccf 1662 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
9859b81e
RR
1663
1664 /* direct probe may be part of the association flow */
1665 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
1666 &ifsta->request)) {
1667 printk(KERN_DEBUG "%s direct probe responded\n",
1668 sdata->dev->name);
1669 ieee80211_authenticate(sdata, ifsta);
1670 }
f0706e82
JB
1671}
1672
1673
f698d856 1674static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1675 struct ieee80211_mgmt *mgmt,
1676 size_t len,
1677 struct ieee80211_rx_status *rx_status)
1678{
f0706e82 1679 struct ieee80211_if_sta *ifsta;
f0706e82
JB
1680 size_t baselen;
1681 struct ieee802_11_elems elems;
f698d856 1682 struct ieee80211_local *local = sdata->local;
d3c990fb 1683 struct ieee80211_conf *conf = &local->hw.conf;
471b3efd 1684 u32 changed = 0;
f0706e82 1685
ae6a44e3
EK
1686 /* Process beacon from the current BSS */
1687 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1688 if (baselen > len)
1689 return;
1690
1691 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1692
98c8fccf 1693 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
f0706e82 1694
51fb61e7 1695 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82
JB
1696 return;
1697 ifsta = &sdata->u.sta;
1698
d6f2da5b 1699 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
f0706e82
JB
1700 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1701 return;
1702
fe3fa827
JB
1703 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1704 elems.wmm_param_len);
1705
5628221c 1706 if (elems.erp_info && elems.erp_info_len >= 1)
471b3efd 1707 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
50c4afb9
JL
1708 else {
1709 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
1710 changed |= ieee80211_handle_protect_preamb(sdata, false,
1711 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
1712 }
f0706e82 1713
d3c990fb 1714 if (elems.ht_cap_elem && elems.ht_info_elem &&
38668c05 1715 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
d3c990fb
RR
1716 struct ieee80211_ht_bss_info bss_info;
1717
1718 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1719 (struct ieee80211_ht_addt_info *)
1720 elems.ht_info_elem, &bss_info);
38668c05
TW
1721 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
1722 &bss_info);
d3c990fb
RR
1723 }
1724
471b3efd 1725 ieee80211_bss_info_change_notify(sdata, changed);
f0706e82
JB
1726}
1727
1728
f698d856 1729static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1730 struct ieee80211_if_sta *ifsta,
1731 struct ieee80211_mgmt *mgmt,
1732 size_t len,
1733 struct ieee80211_rx_status *rx_status)
1734{
f698d856 1735 struct ieee80211_local *local = sdata->local;
f0706e82
JB
1736 int tx_last_beacon;
1737 struct sk_buff *skb;
1738 struct ieee80211_mgmt *resp;
1739 u8 *pos, *end;
0795af57
JP
1740 DECLARE_MAC_BUF(mac);
1741#ifdef CONFIG_MAC80211_IBSS_DEBUG
1742 DECLARE_MAC_BUF(mac2);
1743 DECLARE_MAC_BUF(mac3);
1744#endif
f0706e82 1745
51fb61e7 1746 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
48c2fc59 1747 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
f0706e82
JB
1748 len < 24 + 2 || !ifsta->probe_resp)
1749 return;
1750
1751 if (local->ops->tx_last_beacon)
1752 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1753 else
1754 tx_last_beacon = 1;
1755
1756#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57
JP
1757 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1758 "%s (tx_last_beacon=%d)\n",
f698d856 1759 sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
0795af57 1760 print_mac(mac3, mgmt->bssid), tx_last_beacon);
f0706e82
JB
1761#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1762
1763 if (!tx_last_beacon)
1764 return;
1765
1766 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1767 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1768 return;
1769
1770 end = ((u8 *) mgmt) + len;
1771 pos = mgmt->u.probe_req.variable;
1772 if (pos[0] != WLAN_EID_SSID ||
1773 pos + 2 + pos[1] > end) {
f4ea83dd
JB
1774#ifdef CONFIG_MAC80211_IBSS_DEBUG
1775 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1776 "from %s\n",
f698d856 1777 sdata->dev->name, print_mac(mac, mgmt->sa));
f4ea83dd 1778#endif
f0706e82
JB
1779 return;
1780 }
1781 if (pos[1] != 0 &&
1782 (pos[1] != ifsta->ssid_len ||
1783 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1784 /* Ignore ProbeReq for foreign SSID */
1785 return;
1786 }
1787
1788 /* Reply with ProbeResp */
0ec0b7ac 1789 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
f0706e82
JB
1790 if (!skb)
1791 return;
1792
1793 resp = (struct ieee80211_mgmt *) skb->data;
1794 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1795#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57 1796 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
f698d856 1797 sdata->dev->name, print_mac(mac, resp->da));
f0706e82 1798#endif /* CONFIG_MAC80211_IBSS_DEBUG */
f698d856 1799 ieee80211_sta_tx(sdata, skb, 0);
f0706e82
JB
1800}
1801
f698d856 1802static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
4e20cb29
JB
1803 struct ieee80211_if_sta *ifsta,
1804 struct ieee80211_mgmt *mgmt,
f709fc69
LCC
1805 size_t len,
1806 struct ieee80211_rx_status *rx_status)
9f985b0e 1807{
39192c0b
JB
1808 /* currently we only handle mesh interface action frames here */
1809 if (!ieee80211_vif_is_mesh(&sdata->vif))
9f985b0e
RR
1810 return;
1811
1812 switch (mgmt->u.action.category) {
f709fc69 1813 case PLINK_CATEGORY:
39192c0b 1814 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
f709fc69 1815 break;
f709fc69 1816 case MESH_PATH_SEL_CATEGORY:
39192c0b 1817 mesh_rx_path_sel_frame(sdata, mgmt, len);
f709fc69 1818 break;
9f985b0e
RR
1819 }
1820}
f0706e82 1821
f698d856 1822void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
f0706e82
JB
1823 struct ieee80211_rx_status *rx_status)
1824{
f698d856 1825 struct ieee80211_local *local = sdata->local;
f0706e82
JB
1826 struct ieee80211_if_sta *ifsta;
1827 struct ieee80211_mgmt *mgmt;
1828 u16 fc;
1829
1830 if (skb->len < 24)
1831 goto fail;
1832
f0706e82
JB
1833 ifsta = &sdata->u.sta;
1834
1835 mgmt = (struct ieee80211_mgmt *) skb->data;
1836 fc = le16_to_cpu(mgmt->frame_control);
1837
1838 switch (fc & IEEE80211_FCTL_STYPE) {
1839 case IEEE80211_STYPE_PROBE_REQ:
1840 case IEEE80211_STYPE_PROBE_RESP:
1841 case IEEE80211_STYPE_BEACON:
f709fc69 1842 case IEEE80211_STYPE_ACTION:
f0706e82
JB
1843 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1844 case IEEE80211_STYPE_AUTH:
1845 case IEEE80211_STYPE_ASSOC_RESP:
1846 case IEEE80211_STYPE_REASSOC_RESP:
1847 case IEEE80211_STYPE_DEAUTH:
1848 case IEEE80211_STYPE_DISASSOC:
1849 skb_queue_tail(&ifsta->skb_queue, skb);
1850 queue_work(local->hw.workqueue, &ifsta->work);
1851 return;
f0706e82
JB
1852 }
1853
1854 fail:
1855 kfree_skb(skb);
1856}
1857
f698d856 1858static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1859 struct sk_buff *skb)
1860{
1861 struct ieee80211_rx_status *rx_status;
f0706e82
JB
1862 struct ieee80211_if_sta *ifsta;
1863 struct ieee80211_mgmt *mgmt;
1864 u16 fc;
1865
f0706e82
JB
1866 ifsta = &sdata->u.sta;
1867
1868 rx_status = (struct ieee80211_rx_status *) skb->cb;
1869 mgmt = (struct ieee80211_mgmt *) skb->data;
1870 fc = le16_to_cpu(mgmt->frame_control);
1871
1872 switch (fc & IEEE80211_FCTL_STYPE) {
1873 case IEEE80211_STYPE_PROBE_REQ:
f698d856 1874 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
f0706e82
JB
1875 rx_status);
1876 break;
1877 case IEEE80211_STYPE_PROBE_RESP:
f698d856 1878 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
f0706e82
JB
1879 break;
1880 case IEEE80211_STYPE_BEACON:
f698d856 1881 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
f0706e82
JB
1882 break;
1883 case IEEE80211_STYPE_AUTH:
f698d856 1884 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
f0706e82
JB
1885 break;
1886 case IEEE80211_STYPE_ASSOC_RESP:
471b3efd 1887 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
f0706e82
JB
1888 break;
1889 case IEEE80211_STYPE_REASSOC_RESP:
471b3efd 1890 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
f0706e82
JB
1891 break;
1892 case IEEE80211_STYPE_DEAUTH:
f698d856 1893 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
f0706e82
JB
1894 break;
1895 case IEEE80211_STYPE_DISASSOC:
f698d856 1896 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
f0706e82 1897 break;
9f985b0e 1898 case IEEE80211_STYPE_ACTION:
f698d856 1899 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
9f985b0e 1900 break;
f0706e82
JB
1901 }
1902
1903 kfree_skb(skb);
1904}
1905
1906
f698d856 1907static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
f0706e82 1908{
f698d856 1909 struct ieee80211_local *local = sdata->local;
f0706e82
JB
1910 int active = 0;
1911 struct sta_info *sta;
1912
d0709a65
JB
1913 rcu_read_lock();
1914
1915 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1916 if (sta->sdata == sdata &&
f0706e82
JB
1917 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1918 jiffies)) {
1919 active++;
1920 break;
1921 }
1922 }
d0709a65
JB
1923
1924 rcu_read_unlock();
f0706e82
JB
1925
1926 return active;
1927}
1928
1929
f698d856 1930static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
f0706e82 1931{
f698d856 1932 struct ieee80211_local *local = sdata->local;
f0706e82 1933 struct sta_info *sta, *tmp;
be8755e1 1934 LIST_HEAD(tmp_list);
0795af57 1935 DECLARE_MAC_BUF(mac);
d0709a65 1936 unsigned long flags;
f0706e82 1937
d0709a65 1938 spin_lock_irqsave(&local->sta_lock, flags);
f0706e82 1939 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
f709fc69 1940 if (time_after(jiffies, sta->last_rx + exp_time)) {
f4ea83dd 1941#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57 1942 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
f698d856 1943 sdata->dev->name, print_mac(mac, sta->addr));
f4ea83dd 1944#endif
cb585bcc 1945 __sta_info_unlink(&sta);
d0709a65
JB
1946 if (sta)
1947 list_add(&sta->list, &tmp_list);
f0706e82 1948 }
d0709a65 1949 spin_unlock_irqrestore(&local->sta_lock, flags);
be8755e1 1950
d0709a65
JB
1951 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
1952 sta_info_destroy(sta);
f0706e82
JB
1953}
1954
1955
f698d856 1956static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1957 struct ieee80211_if_sta *ifsta)
1958{
1959 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1960
f698d856
JBG
1961 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
1962 if (ieee80211_sta_active_ibss(sdata))
f0706e82
JB
1963 return;
1964
1965 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
f698d856
JBG
1966 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
1967 ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
f0706e82
JB
1968}
1969
1970
f709fc69 1971#ifdef CONFIG_MAC80211_MESH
f698d856 1972static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
f709fc69
LCC
1973 struct ieee80211_if_sta *ifsta)
1974{
f709fc69
LCC
1975 bool free_plinks;
1976
f698d856
JBG
1977 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
1978 mesh_path_expire(sdata);
f709fc69
LCC
1979
1980 free_plinks = mesh_plink_availables(sdata);
1981 if (free_plinks != sdata->u.sta.accepting_plinks)
9d139c81 1982 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
f709fc69
LCC
1983
1984 mod_timer(&ifsta->timer, jiffies +
1985 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
1986}
1987
1988
f698d856 1989void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
f709fc69
LCC
1990{
1991 struct ieee80211_if_sta *ifsta;
f709fc69 1992 ifsta = &sdata->u.sta;
48c2fc59 1993 ifsta->state = IEEE80211_STA_MLME_MESH_UP;
f709fc69 1994 ieee80211_sta_timer((unsigned long)sdata);
56a6d13d 1995 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
f709fc69
LCC
1996}
1997#endif
1998
1999
f0706e82
JB
2000void ieee80211_sta_timer(unsigned long data)
2001{
60f8b39c
JB
2002 struct ieee80211_sub_if_data *sdata =
2003 (struct ieee80211_sub_if_data *) data;
2004 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2005 struct ieee80211_local *local = sdata->local;
f0706e82 2006
60f8b39c
JB
2007 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2008 queue_work(local->hw.workqueue, &ifsta->work);
f0706e82
JB
2009}
2010
f698d856 2011static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
2012 struct ieee80211_if_sta *ifsta)
2013{
f698d856 2014 struct ieee80211_local *local = sdata->local;
f0706e82
JB
2015
2016 if (local->ops->reset_tsf) {
2017 /* Reset own TSF to allow time synchronization work. */
2018 local->ops->reset_tsf(local_to_hw(local));
2019 }
2020
2021 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2022
2023
2024 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2025 ifsta->auth_alg = WLAN_AUTH_OPEN;
2026 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2027 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2028 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2029 ifsta->auth_alg = WLAN_AUTH_LEAP;
2030 else
2031 ifsta->auth_alg = WLAN_AUTH_OPEN;
f0706e82 2032 ifsta->auth_transaction = -1;
d6f2da5b 2033 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
6042a3e3 2034 ifsta->assoc_scan_tries = 0;
9859b81e 2035 ifsta->direct_probe_tries = 0;
6042a3e3
RR
2036 ifsta->auth_tries = 0;
2037 ifsta->assoc_tries = 0;
24e64622 2038 netif_tx_stop_all_queues(sdata->dev);
f698d856 2039 netif_carrier_off(sdata->dev);
f0706e82
JB
2040}
2041
2042
f698d856 2043void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
2044 struct ieee80211_if_sta *ifsta)
2045{
f698d856 2046 struct ieee80211_local *local = sdata->local;
f0706e82 2047
51fb61e7 2048 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82
JB
2049 return;
2050
d6f2da5b 2051 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3b7ee69d 2052 IEEE80211_STA_AUTO_BSSID_SEL)) &&
d6f2da5b 2053 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3b7ee69d
TW
2054 IEEE80211_STA_AUTO_SSID_SEL))) {
2055
2056 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
2057 ieee80211_set_disassoc(sdata, ifsta, true, true,
2058 WLAN_REASON_DEAUTH_LEAVING);
2059
f0706e82
JB
2060 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2061 queue_work(local->hw.workqueue, &ifsta->work);
2062 }
2063}
2064
2065static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2066 const char *ssid, int ssid_len)
2067{
2068 int tmp, hidden_ssid;
2069
48225709
MW
2070 if (ssid_len == ifsta->ssid_len &&
2071 !memcmp(ifsta->ssid, ssid, ssid_len))
f0706e82
JB
2072 return 1;
2073
d6f2da5b 2074 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
f0706e82
JB
2075 return 0;
2076
2077 hidden_ssid = 1;
2078 tmp = ssid_len;
2079 while (tmp--) {
2080 if (ssid[tmp] != '\0') {
2081 hidden_ssid = 0;
2082 break;
2083 }
2084 }
2085
2086 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2087 return 1;
2088
2089 if (ssid_len == 1 && ssid[0] == ' ')
2090 return 1;
2091
2092 return 0;
2093}
2094
f698d856 2095static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
2096 struct ieee80211_if_sta *ifsta)
2097{
f698d856 2098 struct ieee80211_local *local = sdata->local;
f0706e82 2099 struct ieee80211_sta_bss *bss;
8318d78a 2100 struct ieee80211_supported_band *sband;
f0706e82
JB
2101 u8 bssid[ETH_ALEN], *pos;
2102 int i;
167ad6f7 2103 int ret;
0795af57 2104 DECLARE_MAC_BUF(mac);
f0706e82
JB
2105
2106#if 0
2107 /* Easier testing, use fixed BSSID. */
2108 memset(bssid, 0xfe, ETH_ALEN);
2109#else
2110 /* Generate random, not broadcast, locally administered BSSID. Mix in
2111 * own MAC address to make sure that devices that do not have proper
2112 * random number generator get different BSSID. */
2113 get_random_bytes(bssid, ETH_ALEN);
2114 for (i = 0; i < ETH_ALEN; i++)
f698d856 2115 bssid[i] ^= sdata->dev->dev_addr[i];
f0706e82
JB
2116 bssid[0] &= ~0x01;
2117 bssid[0] |= 0x02;
2118#endif
2119
0795af57 2120 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
f698d856 2121 sdata->dev->name, print_mac(mac, bssid));
f0706e82 2122
98c8fccf 2123 bss = ieee80211_rx_bss_add(local, bssid,
8318d78a 2124 local->hw.conf.channel->center_freq,
cffdd30d 2125 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
f0706e82
JB
2126 if (!bss)
2127 return -ENOMEM;
2128
8318d78a
JB
2129 bss->band = local->hw.conf.channel->band;
2130 sband = local->hw.wiphy->bands[bss->band];
f0706e82
JB
2131
2132 if (local->hw.conf.beacon_int == 0)
dc0ae30c 2133 local->hw.conf.beacon_int = 100;
f0706e82 2134 bss->beacon_int = local->hw.conf.beacon_int;
f0706e82
JB
2135 bss->last_update = jiffies;
2136 bss->capability = WLAN_CAPABILITY_IBSS;
988c0f72
JB
2137
2138 if (sdata->default_key)
f0706e82 2139 bss->capability |= WLAN_CAPABILITY_PRIVACY;
988c0f72 2140 else
f0706e82 2141 sdata->drop_unencrypted = 0;
988c0f72 2142
8318d78a 2143 bss->supp_rates_len = sband->n_bitrates;
f0706e82 2144 pos = bss->supp_rates;
8318d78a
JB
2145 for (i = 0; i < sband->n_bitrates; i++) {
2146 int rate = sband->bitrates[i].bitrate;
f0706e82
JB
2147 *pos++ = (u8) (rate / 5);
2148 }
2149
f698d856 2150 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3e122be0 2151 ieee80211_rx_bss_put(local, bss);
167ad6f7 2152 return ret;
f0706e82
JB
2153}
2154
2155
f698d856 2156static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
2157 struct ieee80211_if_sta *ifsta)
2158{
f698d856 2159 struct ieee80211_local *local = sdata->local;
f0706e82
JB
2160 struct ieee80211_sta_bss *bss;
2161 int found = 0;
2162 u8 bssid[ETH_ALEN];
2163 int active_ibss;
0795af57
JP
2164 DECLARE_MAC_BUF(mac);
2165 DECLARE_MAC_BUF(mac2);
f0706e82
JB
2166
2167 if (ifsta->ssid_len == 0)
2168 return -EINVAL;
2169
f698d856 2170 active_ibss = ieee80211_sta_active_ibss(sdata);
f0706e82
JB
2171#ifdef CONFIG_MAC80211_IBSS_DEBUG
2172 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
f698d856 2173 sdata->dev->name, active_ibss);
f0706e82
JB
2174#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2175 spin_lock_bh(&local->sta_bss_lock);
2176 list_for_each_entry(bss, &local->sta_bss_list, list) {
2177 if (ifsta->ssid_len != bss->ssid_len ||
2178 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2179 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2180 continue;
2181#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57
JP
2182 printk(KERN_DEBUG " bssid=%s found\n",
2183 print_mac(mac, bss->bssid));
f0706e82
JB
2184#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2185 memcpy(bssid, bss->bssid, ETH_ALEN);
2186 found = 1;
2187 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2188 break;
2189 }
2190 spin_unlock_bh(&local->sta_bss_lock);
2191
2192#ifdef CONFIG_MAC80211_IBSS_DEBUG
6e43829b
VK
2193 if (found)
2194 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
2195 "%s\n", print_mac(mac, bssid),
2196 print_mac(mac2, ifsta->bssid));
f0706e82 2197#endif /* CONFIG_MAC80211_IBSS_DEBUG */
80693ceb
DD
2198
2199 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
167ad6f7 2200 int ret;
80693ceb
DD
2201 int search_freq;
2202
2203 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2204 search_freq = bss->freq;
2205 else
2206 search_freq = local->hw.conf.channel->center_freq;
2207
f698d856 2208 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
80693ceb
DD
2209 ifsta->ssid, ifsta->ssid_len);
2210 if (!bss)
2211 goto dont_join;
2212
0795af57 2213 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
f0706e82 2214 " based on configured SSID\n",
f698d856
JBG
2215 sdata->dev->name, print_mac(mac, bssid));
2216 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3e122be0 2217 ieee80211_rx_bss_put(local, bss);
167ad6f7 2218 return ret;
f0706e82 2219 }
80693ceb
DD
2220
2221dont_join:
f0706e82
JB
2222#ifdef CONFIG_MAC80211_IBSS_DEBUG
2223 printk(KERN_DEBUG " did not try to join ibss\n");
2224#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2225
2226 /* Selected IBSS not found in current scan results - try to scan */
48c2fc59 2227 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
f698d856 2228 !ieee80211_sta_active_ibss(sdata)) {
f0706e82
JB
2229 mod_timer(&ifsta->timer, jiffies +
2230 IEEE80211_IBSS_MERGE_INTERVAL);
2231 } else if (time_after(jiffies, local->last_scan_completed +
2232 IEEE80211_SCAN_INTERVAL)) {
2233 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
f698d856
JBG
2234 "join\n", sdata->dev->name);
2235 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
f0706e82 2236 ifsta->ssid_len);
48c2fc59 2237 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
f0706e82
JB
2238 int interval = IEEE80211_SCAN_INTERVAL;
2239
2240 if (time_after(jiffies, ifsta->ibss_join_req +
2241 IEEE80211_IBSS_JOIN_TIMEOUT)) {
d6f2da5b 2242 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
8318d78a
JB
2243 (!(local->oper_channel->flags &
2244 IEEE80211_CHAN_NO_IBSS)))
f698d856 2245 return ieee80211_sta_create_ibss(sdata, ifsta);
d6f2da5b 2246 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
8318d78a 2247 printk(KERN_DEBUG "%s: IBSS not allowed on"
f698d856 2248 " %d MHz\n", sdata->dev->name,
8318d78a 2249 local->hw.conf.channel->center_freq);
f0706e82
JB
2250 }
2251
2252 /* No IBSS found - decrease scan interval and continue
2253 * scanning. */
2254 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2255 }
2256
48c2fc59 2257 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
f0706e82
JB
2258 mod_timer(&ifsta->timer, jiffies + interval);
2259 return 0;
2260 }
2261
2262 return 0;
2263}
2264
2265
f698d856 2266int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
f0706e82 2267{
f0706e82 2268 struct ieee80211_if_sta *ifsta;
9d139c81 2269 int res;
f0706e82
JB
2270
2271 if (len > IEEE80211_MAX_SSID_LEN)
2272 return -EINVAL;
2273
f0706e82
JB
2274 ifsta = &sdata->u.sta;
2275
9d139c81
JB
2276 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
2277 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
2278 memcpy(ifsta->ssid, ssid, len);
2279 ifsta->ssid_len = len;
d6f2da5b 2280 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
9d139c81
JB
2281
2282 res = 0;
2283 /*
2284 * Hack! MLME code needs to be cleaned up to have different
2285 * entry points for configuration and internal selection change
2286 */
2287 if (netif_running(sdata->dev))
2288 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
2289 if (res) {
2290 printk(KERN_DEBUG "%s: Failed to config new SSID to "
f698d856 2291 "the low-level driver\n", sdata->dev->name);
9d139c81
JB
2292 return res;
2293 }
2294 }
f0706e82 2295
d6f2da5b
JS
2296 if (len)
2297 ifsta->flags |= IEEE80211_STA_SSID_SET;
2298 else
2299 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
9d139c81 2300
51fb61e7 2301 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
d6f2da5b 2302 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
f0706e82 2303 ifsta->ibss_join_req = jiffies;
48c2fc59 2304 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
f698d856 2305 return ieee80211_sta_find_ibss(sdata, ifsta);
f0706e82 2306 }
9d139c81 2307
f0706e82
JB
2308 return 0;
2309}
2310
2311
f698d856 2312int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
f0706e82 2313{
f0706e82
JB
2314 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2315 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2316 *len = ifsta->ssid_len;
2317 return 0;
2318}
2319
2320
f698d856 2321int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
f0706e82 2322{
f0706e82
JB
2323 struct ieee80211_if_sta *ifsta;
2324 int res;
2325
f0706e82
JB
2326 ifsta = &sdata->u.sta;
2327
2328 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2329 memcpy(ifsta->bssid, bssid, ETH_ALEN);
9d139c81
JB
2330 res = 0;
2331 /*
2332 * Hack! See also ieee80211_sta_set_ssid.
2333 */
2334 if (netif_running(sdata->dev))
2335 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
f0706e82
JB
2336 if (res) {
2337 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
f698d856 2338 "the low-level driver\n", sdata->dev->name);
f0706e82
JB
2339 return res;
2340 }
2341 }
2342
d6f2da5b
JS
2343 if (is_valid_ether_addr(bssid))
2344 ifsta->flags |= IEEE80211_STA_BSSID_SET;
f0706e82 2345 else
d6f2da5b
JS
2346 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2347
f0706e82
JB
2348 return 0;
2349}
2350
2351
f698d856 2352int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
f0706e82 2353{
f0706e82 2354 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
988c0f72 2355
f0706e82
JB
2356 kfree(ifsta->extra_ie);
2357 if (len == 0) {
2358 ifsta->extra_ie = NULL;
2359 ifsta->extra_ie_len = 0;
2360 return 0;
2361 }
2362 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2363 if (!ifsta->extra_ie) {
2364 ifsta->extra_ie_len = 0;
2365 return -ENOMEM;
2366 }
2367 memcpy(ifsta->extra_ie, ie, len);
2368 ifsta->extra_ie_len = len;
2369 return 0;
2370}
2371
2372
f698d856 2373struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
988c0f72 2374 struct sk_buff *skb, u8 *bssid,
87291c02 2375 u8 *addr, u64 supp_rates)
f0706e82 2376{
f698d856 2377 struct ieee80211_local *local = sdata->local;
f0706e82 2378 struct sta_info *sta;
0795af57 2379 DECLARE_MAC_BUF(mac);
87291c02 2380 int band = local->hw.conf.channel->band;
f0706e82
JB
2381
2382 /* TODO: Could consider removing the least recently used entry and
2383 * allow new one to be added. */
2384 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2385 if (net_ratelimit()) {
2386 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
f698d856 2387 "entry %s\n", sdata->dev->name, print_mac(mac, addr));
f0706e82
JB
2388 }
2389 return NULL;
2390 }
2391
1e188637 2392 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
87291c02
VK
2393 return NULL;
2394
f4ea83dd 2395#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0795af57 2396 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
f698d856 2397 wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
f4ea83dd 2398#endif
f0706e82 2399
73651ee6
JB
2400 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
2401 if (!sta)
f0706e82
JB
2402 return NULL;
2403
07346f81 2404 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
238814fd 2405
8e1535d5
EG
2406 /* make sure mandatory rates are always added */
2407 sta->supp_rates[band] = supp_rates |
2408 ieee80211_sta_get_mandatory_rates(local, band);
f0706e82
JB
2409
2410 rate_control_rate_init(sta, local);
2411
93e5deb1 2412 if (sta_info_insert(sta))
73651ee6 2413 return NULL;
73651ee6 2414
d0709a65 2415 return sta;
f0706e82
JB
2416}
2417
2418
60f8b39c
JB
2419static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
2420 struct ieee80211_if_sta *ifsta)
2421{
2422 struct ieee80211_local *local = sdata->local;
2423 struct ieee80211_sta_bss *bss, *selected = NULL;
2424 int top_rssi = 0, freq;
2425
2426 spin_lock_bh(&local->sta_bss_lock);
2427 freq = local->oper_channel->center_freq;
2428 list_for_each_entry(bss, &local->sta_bss_list, list) {
2429 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2430 continue;
2431
2432 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2433 IEEE80211_STA_AUTO_BSSID_SEL |
2434 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
2435 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2436 !!sdata->default_key))
2437 continue;
2438
2439 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2440 bss->freq != freq)
2441 continue;
2442
2443 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2444 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2445 continue;
2446
2447 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2448 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2449 continue;
2450
2451 if (!selected || top_rssi < bss->signal) {
2452 selected = bss;
2453 top_rssi = bss->signal;
2454 }
2455 }
2456 if (selected)
2457 atomic_inc(&selected->users);
2458 spin_unlock_bh(&local->sta_bss_lock);
2459
2460 if (selected) {
2461 ieee80211_set_freq(sdata, selected->freq);
2462 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2463 ieee80211_sta_set_ssid(sdata, selected->ssid,
2464 selected->ssid_len);
2465 ieee80211_sta_set_bssid(sdata, selected->bssid);
b079ada7 2466 ieee80211_sta_def_wmm_params(sdata, selected);
60f8b39c
JB
2467
2468 /* Send out direct probe if no probe resp was received or
2469 * the one we have is outdated
2470 */
2471 if (!selected->last_probe_resp ||
2472 time_after(jiffies, selected->last_probe_resp
2473 + IEEE80211_SCAN_RESULT_EXPIRE))
2474 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2475 else
2476 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2477
2478 ieee80211_rx_bss_put(local, selected);
2479 ieee80211_sta_reset_auth(sdata, ifsta);
2480 return 0;
2481 } else {
2482 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2483 ifsta->assoc_scan_tries++;
2484 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2485 ieee80211_sta_start_scan(sdata, NULL, 0);
2486 else
2487 ieee80211_sta_start_scan(sdata, ifsta->ssid,
2488 ifsta->ssid_len);
2489 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2490 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2491 } else
2492 ifsta->state = IEEE80211_STA_MLME_DISABLED;
2493 }
2494 return -1;
2495}
2496
2497
f698d856 2498int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
f0706e82 2499{
f0706e82
JB
2500 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2501
f4ea83dd 2502 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
f698d856 2503 sdata->dev->name, reason);
f0706e82 2504
51fb61e7
JB
2505 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2506 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
f0706e82
JB
2507 return -EINVAL;
2508
aa458d17 2509 ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
f0706e82
JB
2510 return 0;
2511}
2512
2513
f698d856 2514int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
f0706e82 2515{
f0706e82
JB
2516 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2517
f4ea83dd 2518 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
f698d856 2519 sdata->dev->name, reason);
f0706e82 2520
51fb61e7 2521 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82
JB
2522 return -EINVAL;
2523
d6f2da5b 2524 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
f0706e82
JB
2525 return -1;
2526
aa458d17 2527 ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
f0706e82
JB
2528 return 0;
2529}
84363e6e
MA
2530
2531void ieee80211_notify_mac(struct ieee80211_hw *hw,
2532 enum ieee80211_notification_types notif_type)
2533{
2534 struct ieee80211_local *local = hw_to_local(hw);
2535 struct ieee80211_sub_if_data *sdata;
2536
2537 switch (notif_type) {
2538 case IEEE80211_NOTIFY_RE_ASSOC:
2539 rcu_read_lock();
2540 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3e122be0
JB
2541 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2542 continue;
84363e6e 2543
f698d856 2544 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
84363e6e
MA
2545 }
2546 rcu_read_unlock();
2547 break;
2548 }
2549}
2550EXPORT_SYMBOL(ieee80211_notify_mac);
60f8b39c
JB
2551
2552void ieee80211_sta_work(struct work_struct *work)
2553{
2554 struct ieee80211_sub_if_data *sdata =
2555 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2556 struct ieee80211_local *local = sdata->local;
2557 struct ieee80211_if_sta *ifsta;
2558 struct sk_buff *skb;
2559
2560 if (!netif_running(sdata->dev))
2561 return;
2562
2563 if (local->sta_sw_scanning || local->sta_hw_scanning)
2564 return;
2565
2566 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2567 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2568 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
2569 return;
2570 ifsta = &sdata->u.sta;
2571
2572 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2573 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2574
2575#ifdef CONFIG_MAC80211_MESH
2576 if (ifsta->preq_queue_len &&
2577 time_after(jiffies,
2578 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
2579 mesh_path_start_discovery(sdata);
2580#endif
2581
2582 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
2583 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
2584 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
2585 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
a0fe8b33 2586 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
60f8b39c
JB
2587 return;
2588 }
2589
2590 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2591 if (ieee80211_sta_config_auth(sdata, ifsta))
2592 return;
2593 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2594 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2595 return;
2596
2597 switch (ifsta->state) {
2598 case IEEE80211_STA_MLME_DISABLED:
2599 break;
2600 case IEEE80211_STA_MLME_DIRECT_PROBE:
2601 ieee80211_direct_probe(sdata, ifsta);
2602 break;
2603 case IEEE80211_STA_MLME_AUTHENTICATE:
2604 ieee80211_authenticate(sdata, ifsta);
2605 break;
2606 case IEEE80211_STA_MLME_ASSOCIATE:
2607 ieee80211_associate(sdata, ifsta);
2608 break;
2609 case IEEE80211_STA_MLME_ASSOCIATED:
2610 ieee80211_associated(sdata, ifsta);
2611 break;
2612 case IEEE80211_STA_MLME_IBSS_SEARCH:
2613 ieee80211_sta_find_ibss(sdata, ifsta);
2614 break;
2615 case IEEE80211_STA_MLME_IBSS_JOINED:
2616 ieee80211_sta_merge_ibss(sdata, ifsta);
2617 break;
2618#ifdef CONFIG_MAC80211_MESH
2619 case IEEE80211_STA_MLME_MESH_UP:
2620 ieee80211_mesh_housekeeping(sdata, ifsta);
2621 break;
2622#endif
2623 default:
2624 WARN_ON(1);
2625 break;
2626 }
2627
2628 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
2629 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2630 "mixed-cell disabled - disassociate\n", sdata->dev->name);
2631
2632 ieee80211_set_disassoc(sdata, ifsta, false, true,
2633 WLAN_REASON_UNSPECIFIED);
2634 }
2635}
0a51b27e
JB
2636
2637void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2638{
2639 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2640 struct ieee80211_if_sta *ifsta;
2641
2642 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
2643 ifsta = &sdata->u.sta;
2644 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2645 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
2646 !ieee80211_sta_active_ibss(sdata)))
2647 ieee80211_sta_find_ibss(sdata, ifsta);
2648 }
2649}