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