]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac80211/mlme.c
mac80211: Replace ieee80211_tx_control->key_idx with ieee80211_key_conf
[mirror_ubuntu-bionic-kernel.git] / net / mac80211 / mlme.c
CommitLineData
f0706e82
JB
1/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
f0706e82
JB
15 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
5b323edb 19#include <linux/delay.h>
f0706e82
JB
20#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
d0709a65 27#include <linux/rtnetlink.h>
f0706e82
JB
28#include <net/iw_handler.h>
29#include <asm/types.h>
f0706e82
JB
30
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
2c8dccc7
JB
33#include "rate.h"
34#include "led.h"
f709fc69 35#include "mesh.h"
f0706e82
JB
36
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
f709fc69 42#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
f0706e82
JB
43#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48
49#define IEEE80211_PROBE_DELAY (HZ / 33)
50#define IEEE80211_CHANNEL_TIME (HZ / 33)
51#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
f709fc69 55#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
f0706e82
JB
56
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
f0706e82
JB
60#define ERP_INFO_USE_PROTECTION BIT(1)
61
9f985b0e
RR
62/* mgmt header + 1 byte action code */
63#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
64
65#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
688b88a4
RR
68#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
9f985b0e 70
07db2183
RR
71/* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73#define IEEE80211_MIN_AMPDU_BUF 0x8
74#define IEEE80211_MAX_AMPDU_BUF 0x40
75
f0706e82
JB
76static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78static struct ieee80211_sta_bss *
8318d78a 79ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
cffdd30d 80 u8 *ssid, u8 ssid_len);
f0706e82
JB
81static void ieee80211_rx_bss_put(struct net_device *dev,
82 struct ieee80211_sta_bss *bss);
83static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85static int ieee80211_sta_wep_configured(struct net_device *dev);
86static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
90
91
ee385855
LCC
92void ieee802_11_parse_elems(u8 *start, size_t len,
93 struct ieee802_11_elems *elems)
f0706e82
JB
94{
95 size_t left = len;
96 u8 *pos = start;
f0706e82
JB
97
98 memset(elems, 0, sizeof(*elems));
99
100 while (left >= 2) {
101 u8 id, elen;
102
103 id = *pos++;
104 elen = *pos++;
105 left -= 2;
106
67a4cce4
JL
107 if (elen > left)
108 return;
f0706e82
JB
109
110 switch (id) {
111 case WLAN_EID_SSID:
112 elems->ssid = pos;
113 elems->ssid_len = elen;
114 break;
115 case WLAN_EID_SUPP_RATES:
116 elems->supp_rates = pos;
117 elems->supp_rates_len = elen;
118 break;
119 case WLAN_EID_FH_PARAMS:
120 elems->fh_params = pos;
121 elems->fh_params_len = elen;
122 break;
123 case WLAN_EID_DS_PARAMS:
124 elems->ds_params = pos;
125 elems->ds_params_len = elen;
126 break;
127 case WLAN_EID_CF_PARAMS:
128 elems->cf_params = pos;
129 elems->cf_params_len = elen;
130 break;
131 case WLAN_EID_TIM:
132 elems->tim = pos;
133 elems->tim_len = elen;
134 break;
135 case WLAN_EID_IBSS_PARAMS:
136 elems->ibss_params = pos;
137 elems->ibss_params_len = elen;
138 break;
139 case WLAN_EID_CHALLENGE:
140 elems->challenge = pos;
141 elems->challenge_len = elen;
142 break;
143 case WLAN_EID_WPA:
144 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
145 pos[2] == 0xf2) {
146 /* Microsoft OUI (00:50:F2) */
147 if (pos[3] == 1) {
148 /* OUI Type 1 - WPA IE */
149 elems->wpa = pos;
150 elems->wpa_len = elen;
151 } else if (elen >= 5 && pos[3] == 2) {
152 if (pos[4] == 0) {
153 elems->wmm_info = pos;
154 elems->wmm_info_len = elen;
155 } else if (pos[4] == 1) {
156 elems->wmm_param = pos;
157 elems->wmm_param_len = elen;
158 }
159 }
160 }
161 break;
162 case WLAN_EID_RSN:
163 elems->rsn = pos;
164 elems->rsn_len = elen;
165 break;
166 case WLAN_EID_ERP_INFO:
167 elems->erp_info = pos;
168 elems->erp_info_len = elen;
169 break;
170 case WLAN_EID_EXT_SUPP_RATES:
171 elems->ext_supp_rates = pos;
172 elems->ext_supp_rates_len = elen;
173 break;
c7153508
RR
174 case WLAN_EID_HT_CAPABILITY:
175 elems->ht_cap_elem = pos;
176 elems->ht_cap_elem_len = elen;
177 break;
178 case WLAN_EID_HT_EXTRA_INFO:
179 elems->ht_info_elem = pos;
180 elems->ht_info_elem_len = elen;
181 break;
ee385855
LCC
182 case WLAN_EID_MESH_ID:
183 elems->mesh_id = pos;
184 elems->mesh_id_len = elen;
185 break;
186 case WLAN_EID_MESH_CONFIG:
187 elems->mesh_config = pos;
188 elems->mesh_config_len = elen;
189 break;
190 case WLAN_EID_PEER_LINK:
191 elems->peer_link = pos;
192 elems->peer_link_len = elen;
193 break;
194 case WLAN_EID_PREQ:
195 elems->preq = pos;
196 elems->preq_len = elen;
197 break;
198 case WLAN_EID_PREP:
199 elems->prep = pos;
200 elems->prep_len = elen;
201 break;
202 case WLAN_EID_PERR:
203 elems->perr = pos;
204 elems->perr_len = elen;
205 break;
f0706e82 206 default:
f0706e82
JB
207 break;
208 }
209
210 left -= elen;
211 pos += elen;
212 }
f0706e82
JB
213}
214
215
f0706e82
JB
216static int ecw2cw(int ecw)
217{
ac2bf324 218 return (1 << ecw) - 1;
f0706e82
JB
219}
220
e2839d8f
VK
221
222static void ieee80211_sta_def_wmm_params(struct net_device *dev,
223 struct ieee80211_sta_bss *bss,
224 int ibss)
225{
226 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
227 struct ieee80211_local *local = sdata->local;
228 int i, have_higher_than_11mbit = 0;
229
230
231 /* cf. IEEE 802.11 9.2.12 */
232 for (i = 0; i < bss->supp_rates_len; i++)
233 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
234 have_higher_than_11mbit = 1;
235
236 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
237 have_higher_than_11mbit)
238 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
239 else
240 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
241
242
243 if (local->ops->conf_tx) {
244 struct ieee80211_tx_queue_params qparam;
e2839d8f
VK
245
246 memset(&qparam, 0, sizeof(qparam));
247
248 qparam.aifs = 2;
249
250 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
251 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
252 qparam.cw_min = 31;
253 else
254 qparam.cw_min = 15;
255
256 qparam.cw_max = 1023;
257 qparam.txop = 0;
258
259 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
260 local->ops->conf_tx(local_to_hw(local),
261 i + IEEE80211_TX_QUEUE_DATA0,
262 &qparam);
263
264 if (ibss) {
265 /* IBSS uses different parameters for Beacon sending */
266 qparam.cw_min++;
267 qparam.cw_min *= 2;
268 qparam.cw_min--;
269 local->ops->conf_tx(local_to_hw(local),
270 IEEE80211_TX_QUEUE_BEACON, &qparam);
271 }
272 }
273}
274
f0706e82
JB
275static void ieee80211_sta_wmm_params(struct net_device *dev,
276 struct ieee80211_if_sta *ifsta,
277 u8 *wmm_param, size_t wmm_param_len)
278{
279 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
280 struct ieee80211_tx_queue_params params;
281 size_t left;
282 int count;
283 u8 *pos;
284
285 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
286 return;
287 count = wmm_param[6] & 0x0f;
288 if (count == ifsta->wmm_last_param_set)
289 return;
290 ifsta->wmm_last_param_set = count;
291
292 pos = wmm_param + 8;
293 left = wmm_param_len - 8;
294
295 memset(&params, 0, sizeof(params));
296
297 if (!local->ops->conf_tx)
298 return;
299
300 local->wmm_acm = 0;
301 for (; left >= 4; left -= 4, pos += 4) {
302 int aci = (pos[0] >> 5) & 0x03;
303 int acm = (pos[0] >> 4) & 0x01;
304 int queue;
305
306 switch (aci) {
307 case 1:
308 queue = IEEE80211_TX_QUEUE_DATA3;
309 if (acm) {
310 local->wmm_acm |= BIT(0) | BIT(3);
311 }
312 break;
313 case 2:
314 queue = IEEE80211_TX_QUEUE_DATA1;
315 if (acm) {
316 local->wmm_acm |= BIT(4) | BIT(5);
317 }
318 break;
319 case 3:
320 queue = IEEE80211_TX_QUEUE_DATA0;
321 if (acm) {
322 local->wmm_acm |= BIT(6) | BIT(7);
323 }
324 break;
325 case 0:
326 default:
327 queue = IEEE80211_TX_QUEUE_DATA2;
328 if (acm) {
329 local->wmm_acm |= BIT(1) | BIT(2);
330 }
331 break;
332 }
333
334 params.aifs = pos[0] & 0x0f;
335 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
336 params.cw_min = ecw2cw(pos[1] & 0x0f);
3330d7be
JB
337 params.txop = pos[2] | (pos[3] << 8);
338#ifdef CONFIG_MAC80211_DEBUG
f0706e82 339 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
3330d7be 340 "cWmin=%d cWmax=%d txop=%d\n",
f0706e82 341 dev->name, queue, aci, acm, params.aifs, params.cw_min,
3330d7be
JB
342 params.cw_max, params.txop);
343#endif
f0706e82
JB
344 /* TODO: handle ACM (block TX, fallback to next lowest allowed
345 * AC for now) */
346 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
347 printk(KERN_DEBUG "%s: failed to set TX queue "
348 "parameters for queue %d\n", dev->name, queue);
349 }
350 }
351}
352
50c4afb9
JL
353static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
354 bool use_protection,
355 bool use_short_preamble)
5628221c 356{
471b3efd 357 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
5628221c 358 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
0795af57 359 DECLARE_MAC_BUF(mac);
471b3efd 360 u32 changed = 0;
5628221c 361
471b3efd 362 if (use_protection != bss_conf->use_cts_prot) {
5628221c
DD
363 if (net_ratelimit()) {
364 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
0795af57 365 "%s)\n",
471b3efd 366 sdata->dev->name,
5628221c 367 use_protection ? "enabled" : "disabled",
0795af57 368 print_mac(mac, ifsta->bssid));
5628221c 369 }
471b3efd
JB
370 bss_conf->use_cts_prot = use_protection;
371 changed |= BSS_CHANGED_ERP_CTS_PROT;
5628221c 372 }
7e9ed188 373
d43c7b37 374 if (use_short_preamble != bss_conf->use_short_preamble) {
7e9ed188
DD
375 if (net_ratelimit()) {
376 printk(KERN_DEBUG "%s: switched to %s barker preamble"
0795af57 377 " (BSSID=%s)\n",
471b3efd 378 sdata->dev->name,
d43c7b37 379 use_short_preamble ? "short" : "long",
0795af57 380 print_mac(mac, ifsta->bssid));
7e9ed188 381 }
d43c7b37 382 bss_conf->use_short_preamble = use_short_preamble;
471b3efd 383 changed |= BSS_CHANGED_ERP_PREAMBLE;
7e9ed188 384 }
d9430a32 385
471b3efd 386 return changed;
5628221c
DD
387}
388
50c4afb9
JL
389static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
390 u8 erp_value)
391{
392 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
393 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
394
395 return ieee80211_handle_protect_preamb(sdata,
396 use_protection, use_short_preamble);
397}
398
399static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
400 struct ieee80211_sta_bss *bss)
401{
402 u32 changed = 0;
403
404 if (bss->has_erp_value)
405 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
406 else {
407 u16 capab = bss->capability;
408 changed |= ieee80211_handle_protect_preamb(sdata, false,
409 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
410 }
411
412 return changed;
413}
414
c7153508
RR
415int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
416 struct ieee80211_ht_info *ht_info)
417{
418
419 if (ht_info == NULL)
420 return -EINVAL;
421
422 memset(ht_info, 0, sizeof(*ht_info));
423
424 if (ht_cap_ie) {
425 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
426
427 ht_info->ht_supported = 1;
428 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
429 ht_info->ampdu_factor =
430 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
431 ht_info->ampdu_density =
432 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
433 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
434 } else
435 ht_info->ht_supported = 0;
436
437 return 0;
438}
439
440int ieee80211_ht_addt_info_ie_to_ht_bss_info(
441 struct ieee80211_ht_addt_info *ht_add_info_ie,
442 struct ieee80211_ht_bss_info *bss_info)
443{
444 if (bss_info == NULL)
445 return -EINVAL;
446
447 memset(bss_info, 0, sizeof(*bss_info));
448
449 if (ht_add_info_ie) {
450 u16 op_mode;
451 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
452
453 bss_info->primary_channel = ht_add_info_ie->control_chan;
454 bss_info->bss_cap = ht_add_info_ie->ht_param;
455 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
456 }
457
458 return 0;
459}
5628221c 460
f0706e82
JB
461static void ieee80211_sta_send_associnfo(struct net_device *dev,
462 struct ieee80211_if_sta *ifsta)
463{
464 char *buf;
465 size_t len;
466 int i;
467 union iwreq_data wrqu;
468
469 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
470 return;
471
472 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
0ec0b7ac 473 ifsta->assocresp_ies_len), GFP_KERNEL);
f0706e82
JB
474 if (!buf)
475 return;
476
477 len = sprintf(buf, "ASSOCINFO(");
478 if (ifsta->assocreq_ies) {
479 len += sprintf(buf + len, "ReqIEs=");
480 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
481 len += sprintf(buf + len, "%02x",
482 ifsta->assocreq_ies[i]);
483 }
484 }
485 if (ifsta->assocresp_ies) {
486 if (ifsta->assocreq_ies)
487 len += sprintf(buf + len, " ");
488 len += sprintf(buf + len, "RespIEs=");
489 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
490 len += sprintf(buf + len, "%02x",
491 ifsta->assocresp_ies[i]);
492 }
493 }
494 len += sprintf(buf + len, ")");
495
496 if (len > IW_CUSTOM_MAX) {
497 len = sprintf(buf, "ASSOCRESPIE=");
498 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
499 len += sprintf(buf + len, "%02x",
500 ifsta->assocresp_ies[i]);
501 }
502 }
503
504 memset(&wrqu, 0, sizeof(wrqu));
505 wrqu.data.length = len;
506 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
507
508 kfree(buf);
509}
510
511
512static void ieee80211_set_associated(struct net_device *dev,
d6f2da5b 513 struct ieee80211_if_sta *ifsta,
47f0c502 514 bool assoc)
f0706e82 515{
471b3efd
JB
516 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
517 struct ieee80211_local *local = sdata->local;
38668c05 518 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
f0706e82 519 union iwreq_data wrqu;
471b3efd 520 u32 changed = BSS_CHANGED_ASSOC;
f0706e82 521
f0706e82 522 if (assoc) {
5628221c 523 struct ieee80211_sta_bss *bss;
d6f2da5b
JS
524
525 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
526
51fb61e7 527 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82 528 return;
5628221c 529
65c107ab 530 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
38668c05 531 conf->channel->center_freq,
cffdd30d 532 ifsta->ssid, ifsta->ssid_len);
5628221c 533 if (bss) {
21c0cbe7
TW
534 /* set timing information */
535 sdata->bss_conf.beacon_int = bss->beacon_int;
536 sdata->bss_conf.timestamp = bss->timestamp;
537
50c4afb9 538 changed |= ieee80211_handle_bss_capability(sdata, bss);
21c0cbe7 539
5628221c
DD
540 ieee80211_rx_bss_put(dev, bss);
541 }
542
38668c05
TW
543 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
544 changed |= BSS_CHANGED_HT;
545 sdata->bss_conf.assoc_ht = 1;
546 sdata->bss_conf.ht_conf = &conf->ht_conf;
547 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
548 }
549
f0706e82 550 netif_carrier_on(dev);
d6f2da5b 551 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
f0706e82
JB
552 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
553 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
554 ieee80211_sta_send_associnfo(dev, ifsta);
555 } else {
85249e5f 556 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
d6f2da5b 557 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
f0706e82 558 netif_carrier_off(dev);
d9430a32 559 ieee80211_reset_erp_info(dev);
38668c05
TW
560
561 sdata->bss_conf.assoc_ht = 0;
562 sdata->bss_conf.ht_conf = NULL;
563 sdata->bss_conf.ht_bss_conf = NULL;
564
f0706e82
JB
565 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
566 }
f0706e82 567 ifsta->last_probe = jiffies;
47f0c502 568 ieee80211_led_assoc(local, assoc);
471b3efd 569
b2205256 570 sdata->bss_conf.assoc = assoc;
471b3efd 571 ieee80211_bss_info_change_notify(sdata, changed);
41a7be48
RC
572 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
573 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
f0706e82
JB
574}
575
576static void ieee80211_set_disassoc(struct net_device *dev,
577 struct ieee80211_if_sta *ifsta, int deauth)
578{
579 if (deauth)
580 ifsta->auth_tries = 0;
581 ifsta->assoc_tries = 0;
582 ieee80211_set_associated(dev, ifsta, 0);
583}
584
ee385855
LCC
585void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
586 int encrypt)
f0706e82
JB
587{
588 struct ieee80211_sub_if_data *sdata;
589 struct ieee80211_tx_packet_data *pkt_data;
590
591 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
592 skb->dev = sdata->local->mdev;
593 skb_set_mac_header(skb, 0);
594 skb_set_network_header(skb, 0);
595 skb_set_transport_header(skb, 0);
596
597 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
598 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
599 pkt_data->ifindex = sdata->dev->ifindex;
e8bf9649
JS
600 if (!encrypt)
601 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
f0706e82
JB
602
603 dev_queue_xmit(skb);
604}
605
606
607static void ieee80211_send_auth(struct net_device *dev,
608 struct ieee80211_if_sta *ifsta,
609 int transaction, u8 *extra, size_t extra_len,
610 int encrypt)
611{
612 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
613 struct sk_buff *skb;
614 struct ieee80211_mgmt *mgmt;
615
616 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
617 sizeof(*mgmt) + 6 + extra_len);
618 if (!skb) {
619 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
620 "frame\n", dev->name);
621 return;
622 }
623 skb_reserve(skb, local->hw.extra_tx_headroom);
624
625 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
626 memset(mgmt, 0, 24 + 6);
627 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
628 IEEE80211_STYPE_AUTH);
629 if (encrypt)
630 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
631 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
632 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
633 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
634 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
635 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
636 ifsta->auth_transaction = transaction + 1;
637 mgmt->u.auth.status_code = cpu_to_le16(0);
638 if (extra)
639 memcpy(skb_put(skb, extra_len), extra, extra_len);
640
641 ieee80211_sta_tx(dev, skb, encrypt);
642}
643
644
645static void ieee80211_authenticate(struct net_device *dev,
646 struct ieee80211_if_sta *ifsta)
647{
0795af57
JP
648 DECLARE_MAC_BUF(mac);
649
f0706e82
JB
650 ifsta->auth_tries++;
651 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
0795af57 652 printk(KERN_DEBUG "%s: authentication with AP %s"
f0706e82 653 " timed out\n",
0795af57 654 dev->name, print_mac(mac, ifsta->bssid));
f0706e82
JB
655 ifsta->state = IEEE80211_DISABLED;
656 return;
657 }
658
659 ifsta->state = IEEE80211_AUTHENTICATE;
0795af57
JP
660 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
661 dev->name, print_mac(mac, ifsta->bssid));
f0706e82
JB
662
663 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
664
665 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
666}
667
668
669static void ieee80211_send_assoc(struct net_device *dev,
670 struct ieee80211_if_sta *ifsta)
671{
672 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
f0706e82
JB
673 struct sk_buff *skb;
674 struct ieee80211_mgmt *mgmt;
675 u8 *pos, *ies;
676 int i, len;
677 u16 capab;
678 struct ieee80211_sta_bss *bss;
679 int wmm = 0;
8318d78a 680 struct ieee80211_supported_band *sband;
f0706e82
JB
681
682 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
683 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
684 ifsta->ssid_len);
685 if (!skb) {
686 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
687 "frame\n", dev->name);
688 return;
689 }
690 skb_reserve(skb, local->hw.extra_tx_headroom);
691
8318d78a
JB
692 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
693
f0706e82 694 capab = ifsta->capab;
8318d78a
JB
695
696 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
697 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
698 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
699 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
700 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
f0706e82 701 }
8318d78a
JB
702
703 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
704 local->hw.conf.channel->center_freq,
cffdd30d 705 ifsta->ssid, ifsta->ssid_len);
f0706e82
JB
706 if (bss) {
707 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
708 capab |= WLAN_CAPABILITY_PRIVACY;
709 if (bss->wmm_ie) {
710 wmm = 1;
711 }
712 ieee80211_rx_bss_put(dev, bss);
713 }
714
715 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
716 memset(mgmt, 0, 24);
717 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
718 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
719 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
720
d6f2da5b 721 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
f0706e82
JB
722 skb_put(skb, 10);
723 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
724 IEEE80211_STYPE_REASSOC_REQ);
725 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
726 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
727 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
728 ETH_ALEN);
729 } else {
730 skb_put(skb, 4);
731 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
732 IEEE80211_STYPE_ASSOC_REQ);
733 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
734 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
735 }
736
737 /* SSID */
738 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
739 *pos++ = WLAN_EID_SSID;
740 *pos++ = ifsta->ssid_len;
741 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
742
8318d78a 743 len = sband->n_bitrates;
f0706e82
JB
744 if (len > 8)
745 len = 8;
746 pos = skb_put(skb, len + 2);
747 *pos++ = WLAN_EID_SUPP_RATES;
748 *pos++ = len;
749 for (i = 0; i < len; i++) {
8318d78a 750 int rate = sband->bitrates[i].bitrate;
f0706e82
JB
751 *pos++ = (u8) (rate / 5);
752 }
753
8318d78a
JB
754 if (sband->n_bitrates > len) {
755 pos = skb_put(skb, sband->n_bitrates - len + 2);
f0706e82 756 *pos++ = WLAN_EID_EXT_SUPP_RATES;
8318d78a
JB
757 *pos++ = sband->n_bitrates - len;
758 for (i = len; i < sband->n_bitrates; i++) {
759 int rate = sband->bitrates[i].bitrate;
f0706e82
JB
760 *pos++ = (u8) (rate / 5);
761 }
762 }
763
764 if (ifsta->extra_ie) {
765 pos = skb_put(skb, ifsta->extra_ie_len);
766 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
767 }
768
d6f2da5b 769 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
f0706e82
JB
770 pos = skb_put(skb, 9);
771 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
772 *pos++ = 7; /* len */
773 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
774 *pos++ = 0x50;
775 *pos++ = 0xf2;
776 *pos++ = 2; /* WME */
777 *pos++ = 0; /* WME info */
778 *pos++ = 1; /* WME ver */
779 *pos++ = 0;
780 }
c7153508 781 /* wmm support is a must to HT */
8318d78a
JB
782 if (wmm && sband->ht_info.ht_supported) {
783 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
c7153508
RR
784 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
785 *pos++ = WLAN_EID_HT_CAPABILITY;
786 *pos++ = sizeof(struct ieee80211_ht_cap);
787 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
788 memcpy(pos, &tmp, sizeof(u16));
789 pos += sizeof(u16);
8318d78a
JB
790 /* TODO: needs a define here for << 2 */
791 *pos++ = sband->ht_info.ampdu_factor |
792 (sband->ht_info.ampdu_density << 2);
793 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
c7153508 794 }
f0706e82
JB
795
796 kfree(ifsta->assocreq_ies);
797 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
0ec0b7ac 798 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
f0706e82
JB
799 if (ifsta->assocreq_ies)
800 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
801
802 ieee80211_sta_tx(dev, skb, 0);
803}
804
805
806static void ieee80211_send_deauth(struct net_device *dev,
807 struct ieee80211_if_sta *ifsta, u16 reason)
808{
809 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
810 struct sk_buff *skb;
811 struct ieee80211_mgmt *mgmt;
812
813 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
814 if (!skb) {
815 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
816 "frame\n", dev->name);
817 return;
818 }
819 skb_reserve(skb, local->hw.extra_tx_headroom);
820
821 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
822 memset(mgmt, 0, 24);
823 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
824 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
825 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
826 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
827 IEEE80211_STYPE_DEAUTH);
828 skb_put(skb, 2);
829 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
830
831 ieee80211_sta_tx(dev, skb, 0);
832}
833
834
835static void ieee80211_send_disassoc(struct net_device *dev,
836 struct ieee80211_if_sta *ifsta, u16 reason)
837{
838 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
839 struct sk_buff *skb;
840 struct ieee80211_mgmt *mgmt;
841
842 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
843 if (!skb) {
844 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
845 "frame\n", dev->name);
846 return;
847 }
848 skb_reserve(skb, local->hw.extra_tx_headroom);
849
850 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
851 memset(mgmt, 0, 24);
852 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
853 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
854 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
855 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
856 IEEE80211_STYPE_DISASSOC);
857 skb_put(skb, 2);
858 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
859
860 ieee80211_sta_tx(dev, skb, 0);
861}
862
863
864static int ieee80211_privacy_mismatch(struct net_device *dev,
865 struct ieee80211_if_sta *ifsta)
866{
65c107ab 867 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
f0706e82 868 struct ieee80211_sta_bss *bss;
5b98b1f7
JB
869 int bss_privacy;
870 int wep_privacy;
871 int privacy_invoked;
f0706e82 872
5b98b1f7 873 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
f0706e82
JB
874 return 0;
875
8318d78a
JB
876 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
877 local->hw.conf.channel->center_freq,
cffdd30d 878 ifsta->ssid, ifsta->ssid_len);
f0706e82
JB
879 if (!bss)
880 return 0;
881
5b98b1f7
JB
882 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
883 wep_privacy = !!ieee80211_sta_wep_configured(dev);
884 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
f0706e82
JB
885
886 ieee80211_rx_bss_put(dev, bss);
887
5b98b1f7
JB
888 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
889 return 0;
890
891 return 1;
f0706e82
JB
892}
893
894
895static void ieee80211_associate(struct net_device *dev,
896 struct ieee80211_if_sta *ifsta)
897{
0795af57
JP
898 DECLARE_MAC_BUF(mac);
899
f0706e82
JB
900 ifsta->assoc_tries++;
901 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
0795af57 902 printk(KERN_DEBUG "%s: association with AP %s"
f0706e82 903 " timed out\n",
0795af57 904 dev->name, print_mac(mac, ifsta->bssid));
f0706e82
JB
905 ifsta->state = IEEE80211_DISABLED;
906 return;
907 }
908
909 ifsta->state = IEEE80211_ASSOCIATE;
0795af57
JP
910 printk(KERN_DEBUG "%s: associate with AP %s\n",
911 dev->name, print_mac(mac, ifsta->bssid));
f0706e82
JB
912 if (ieee80211_privacy_mismatch(dev, ifsta)) {
913 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
914 "mixed-cell disabled - abort association\n", dev->name);
915 ifsta->state = IEEE80211_DISABLED;
916 return;
917 }
918
919 ieee80211_send_assoc(dev, ifsta);
920
921 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
922}
923
924
925static void ieee80211_associated(struct net_device *dev,
926 struct ieee80211_if_sta *ifsta)
927{
928 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
929 struct sta_info *sta;
930 int disassoc;
0795af57 931 DECLARE_MAC_BUF(mac);
f0706e82
JB
932
933 /* TODO: start monitoring current AP signal quality and number of
934 * missed beacons. Scan other channels every now and then and search
935 * for better APs. */
936 /* TODO: remove expired BSSes */
937
938 ifsta->state = IEEE80211_ASSOCIATED;
939
d0709a65
JB
940 rcu_read_lock();
941
f0706e82
JB
942 sta = sta_info_get(local, ifsta->bssid);
943 if (!sta) {
0795af57
JP
944 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
945 dev->name, print_mac(mac, ifsta->bssid));
f0706e82
JB
946 disassoc = 1;
947 } else {
948 disassoc = 0;
949 if (time_after(jiffies,
950 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
d6f2da5b 951 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
f0706e82 952 printk(KERN_DEBUG "%s: No ProbeResp from "
0795af57 953 "current AP %s - assume out of "
f0706e82 954 "range\n",
0795af57 955 dev->name, print_mac(mac, ifsta->bssid));
f0706e82 956 disassoc = 1;
d0709a65 957 sta_info_unlink(&sta);
d6f2da5b 958 } else
f0706e82
JB
959 ieee80211_send_probe_req(dev, ifsta->bssid,
960 local->scan_ssid,
961 local->scan_ssid_len);
d6f2da5b 962 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
f0706e82 963 } else {
d6f2da5b 964 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
f0706e82
JB
965 if (time_after(jiffies, ifsta->last_probe +
966 IEEE80211_PROBE_INTERVAL)) {
967 ifsta->last_probe = jiffies;
968 ieee80211_send_probe_req(dev, ifsta->bssid,
969 ifsta->ssid,
970 ifsta->ssid_len);
971 }
972 }
f0706e82 973 }
d0709a65
JB
974
975 rcu_read_unlock();
976
3b96766f 977 if (disassoc && sta)
d0709a65 978 sta_info_destroy(sta);
d0709a65 979
f0706e82 980 if (disassoc) {
2d192d95
MW
981 ifsta->state = IEEE80211_DISABLED;
982 ieee80211_set_associated(dev, ifsta, 0);
f0706e82
JB
983 } else {
984 mod_timer(&ifsta->timer, jiffies +
985 IEEE80211_MONITORING_INTERVAL);
986 }
987}
988
989
990static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
991 u8 *ssid, size_t ssid_len)
992{
993 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
8318d78a 994 struct ieee80211_supported_band *sband;
f0706e82
JB
995 struct sk_buff *skb;
996 struct ieee80211_mgmt *mgmt;
997 u8 *pos, *supp_rates, *esupp_rates = NULL;
998 int i;
999
1000 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1001 if (!skb) {
1002 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1003 "request\n", dev->name);
1004 return;
1005 }
1006 skb_reserve(skb, local->hw.extra_tx_headroom);
1007
1008 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1009 memset(mgmt, 0, 24);
1010 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1011 IEEE80211_STYPE_PROBE_REQ);
1012 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1013 if (dst) {
1014 memcpy(mgmt->da, dst, ETH_ALEN);
1015 memcpy(mgmt->bssid, dst, ETH_ALEN);
1016 } else {
1017 memset(mgmt->da, 0xff, ETH_ALEN);
1018 memset(mgmt->bssid, 0xff, ETH_ALEN);
1019 }
1020 pos = skb_put(skb, 2 + ssid_len);
1021 *pos++ = WLAN_EID_SSID;
1022 *pos++ = ssid_len;
1023 memcpy(pos, ssid, ssid_len);
1024
1025 supp_rates = skb_put(skb, 2);
1026 supp_rates[0] = WLAN_EID_SUPP_RATES;
1027 supp_rates[1] = 0;
8318d78a
JB
1028 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1029
1030 for (i = 0; i < sband->n_bitrates; i++) {
1031 struct ieee80211_rate *rate = &sband->bitrates[i];
f0706e82
JB
1032 if (esupp_rates) {
1033 pos = skb_put(skb, 1);
1034 esupp_rates[1]++;
1035 } else if (supp_rates[1] == 8) {
1036 esupp_rates = skb_put(skb, 3);
1037 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1038 esupp_rates[1] = 1;
1039 pos = &esupp_rates[2];
1040 } else {
1041 pos = skb_put(skb, 1);
1042 supp_rates[1]++;
1043 }
8318d78a 1044 *pos = rate->bitrate / 5;
f0706e82
JB
1045 }
1046
1047 ieee80211_sta_tx(dev, skb, 0);
1048}
1049
1050
1051static int ieee80211_sta_wep_configured(struct net_device *dev)
1052{
1053 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1054 if (!sdata || !sdata->default_key ||
8f20fc24 1055 sdata->default_key->conf.alg != ALG_WEP)
f0706e82
JB
1056 return 0;
1057 return 1;
1058}
1059
1060
1061static void ieee80211_auth_completed(struct net_device *dev,
1062 struct ieee80211_if_sta *ifsta)
1063{
1064 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
d6f2da5b 1065 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
f0706e82
JB
1066 ieee80211_associate(dev, ifsta);
1067}
1068
1069
1070static void ieee80211_auth_challenge(struct net_device *dev,
1071 struct ieee80211_if_sta *ifsta,
1072 struct ieee80211_mgmt *mgmt,
1073 size_t len)
1074{
1075 u8 *pos;
1076 struct ieee802_11_elems elems;
1077
1078 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1079 pos = mgmt->u.auth.variable;
67a4cce4 1080 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
f0706e82
JB
1081 if (!elems.challenge) {
1082 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1083 "frame\n", dev->name);
1084 return;
1085 }
1086 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1087 elems.challenge_len + 2, 1);
1088}
1089
9f985b0e
RR
1090static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1091 u8 dialog_token, u16 status, u16 policy,
1092 u16 buf_size, u16 timeout)
1093{
1094 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1095 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1096 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1097 struct sk_buff *skb;
1098 struct ieee80211_mgmt *mgmt;
1099 u16 capab;
1100
07db2183
RR
1101 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1102 sizeof(mgmt->u.action.u.addba_resp));
9f985b0e
RR
1103 if (!skb) {
1104 printk(KERN_DEBUG "%s: failed to allocate buffer "
1105 "for addba resp frame\n", dev->name);
1106 return;
1107 }
1108
1109 skb_reserve(skb, local->hw.extra_tx_headroom);
1110 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1111 memset(mgmt, 0, 24);
1112 memcpy(mgmt->da, da, ETH_ALEN);
1113 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
51fb61e7 1114 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
9f985b0e
RR
1115 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1116 else
1117 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1118 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1119 IEEE80211_STYPE_ACTION);
1120
1121 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1122 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1123 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1124 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1125
1126 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1127 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1128 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1129
1130 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1131 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1132 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1133
1134 ieee80211_sta_tx(dev, skb, 0);
1135
1136 return;
1137}
1138
eadc8d9e
RR
1139void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1140 u16 tid, u8 dialog_token, u16 start_seq_num,
1141 u16 agg_size, u16 timeout)
1142{
1143 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1144 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1145 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1146 struct sk_buff *skb;
1147 struct ieee80211_mgmt *mgmt;
1148 u16 capab;
1149
1150 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1151 sizeof(mgmt->u.action.u.addba_req));
1152
1153
1154 if (!skb) {
1155 printk(KERN_ERR "%s: failed to allocate buffer "
1156 "for addba request frame\n", dev->name);
1157 return;
1158 }
1159 skb_reserve(skb, local->hw.extra_tx_headroom);
1160 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1161 memset(mgmt, 0, 24);
1162 memcpy(mgmt->da, da, ETH_ALEN);
1163 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1164 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1165 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1166 else
1167 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1168
1169 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1170 IEEE80211_STYPE_ACTION);
1171
1172 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1173
1174 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1175 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1176
1177 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1178 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1179 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1180 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1181
1182 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1183
1184 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1185 mgmt->u.action.u.addba_req.start_seq_num =
1186 cpu_to_le16(start_seq_num << 4);
1187
1188 ieee80211_sta_tx(dev, skb, 0);
1189}
1190
9f985b0e
RR
1191static void ieee80211_sta_process_addba_request(struct net_device *dev,
1192 struct ieee80211_mgmt *mgmt,
1193 size_t len)
1194{
1195 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
07db2183
RR
1196 struct ieee80211_hw *hw = &local->hw;
1197 struct ieee80211_conf *conf = &hw->conf;
9f985b0e 1198 struct sta_info *sta;
07db2183
RR
1199 struct tid_ampdu_rx *tid_agg_rx;
1200 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
9f985b0e 1201 u8 dialog_token;
07db2183
RR
1202 int ret = -EOPNOTSUPP;
1203 DECLARE_MAC_BUF(mac);
9f985b0e 1204
d0709a65
JB
1205 rcu_read_lock();
1206
9f985b0e 1207 sta = sta_info_get(local, mgmt->sa);
d0709a65
JB
1208 if (!sta) {
1209 rcu_read_unlock();
9f985b0e 1210 return;
d0709a65 1211 }
9f985b0e
RR
1212
1213 /* extract session parameters from addba request frame */
1214 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1215 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
07db2183
RR
1216 start_seq_num =
1217 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
9f985b0e
RR
1218
1219 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1220 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1221 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1222 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1223
9f985b0e
RR
1224 status = WLAN_STATUS_REQUEST_DECLINED;
1225
07db2183
RR
1226 /* sanity check for incoming parameters:
1227 * check if configuration can support the BA policy
1228 * and if buffer size does not exceeds max value */
1229 if (((ba_policy != 1)
1230 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1231 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1232 status = WLAN_STATUS_INVALID_QOS_PARAM;
1233#ifdef CONFIG_MAC80211_HT_DEBUG
1234 if (net_ratelimit())
7b9d44cd 1235 printk(KERN_DEBUG "AddBA Req with bad params from "
07db2183
RR
1236 "%s on tid %u. policy %d, buffer size %d\n",
1237 print_mac(mac, mgmt->sa), tid, ba_policy,
1238 buf_size);
1239#endif /* CONFIG_MAC80211_HT_DEBUG */
1240 goto end_no_lock;
1241 }
1242 /* determine default buffer size */
1243 if (buf_size == 0) {
8318d78a
JB
1244 struct ieee80211_supported_band *sband;
1245
1246 sband = local->hw.wiphy->bands[conf->channel->band];
07db2183 1247 buf_size = IEEE80211_MIN_AMPDU_BUF;
8318d78a 1248 buf_size = buf_size << sband->ht_info.ampdu_factor;
07db2183
RR
1249 }
1250
07db2183
RR
1251
1252 /* examine state machine */
1253 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1254
cee24a3e 1255 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
07db2183
RR
1256#ifdef CONFIG_MAC80211_HT_DEBUG
1257 if (net_ratelimit())
7b9d44cd 1258 printk(KERN_DEBUG "unexpected AddBA Req from "
07db2183
RR
1259 "%s on tid %u\n",
1260 print_mac(mac, mgmt->sa), tid);
1261#endif /* CONFIG_MAC80211_HT_DEBUG */
1262 goto end;
1263 }
1264
cee24a3e
RR
1265 /* prepare A-MPDU MLME for Rx aggregation */
1266 sta->ampdu_mlme.tid_rx[tid] =
1267 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1268 if (!sta->ampdu_mlme.tid_rx[tid]) {
1269 if (net_ratelimit())
1270 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1271 tid);
1272 goto end;
1273 }
1274 /* rx timer */
1275 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1276 sta_rx_agg_session_timer_expired;
1277 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1278 (unsigned long)&sta->timer_to_tid[tid];
1279 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1280
1281 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1282
07db2183
RR
1283 /* prepare reordering buffer */
1284 tid_agg_rx->reorder_buf =
1285 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
03147dfc
JB
1286 if (!tid_agg_rx->reorder_buf) {
1287 if (net_ratelimit())
1288 printk(KERN_ERR "can not allocate reordering buffer "
1289 "to tid %d\n", tid);
cee24a3e 1290 kfree(sta->ampdu_mlme.tid_rx[tid]);
07db2183
RR
1291 goto end;
1292 }
1293 memset(tid_agg_rx->reorder_buf, 0,
1294 buf_size * sizeof(struct sk_buf *));
1295
1296 if (local->ops->ampdu_action)
1297 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
0df3ef45 1298 sta->addr, tid, &start_seq_num);
07db2183 1299#ifdef CONFIG_MAC80211_HT_DEBUG
513a1025 1300 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
07db2183
RR
1301#endif /* CONFIG_MAC80211_HT_DEBUG */
1302
1303 if (ret) {
1304 kfree(tid_agg_rx->reorder_buf);
cee24a3e
RR
1305 kfree(tid_agg_rx);
1306 sta->ampdu_mlme.tid_rx[tid] = NULL;
07db2183
RR
1307 goto end;
1308 }
1309
1310 /* change state and send addba resp */
cee24a3e 1311 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
07db2183
RR
1312 tid_agg_rx->dialog_token = dialog_token;
1313 tid_agg_rx->ssn = start_seq_num;
1314 tid_agg_rx->head_seq_num = start_seq_num;
1315 tid_agg_rx->buf_size = buf_size;
1316 tid_agg_rx->timeout = timeout;
1317 tid_agg_rx->stored_mpdu_num = 0;
1318 status = WLAN_STATUS_SUCCESS;
1319end:
1320 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1321
1322end_no_lock:
d0709a65
JB
1323 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1324 dialog_token, status, 1, buf_size, timeout);
1325 rcu_read_unlock();
9f985b0e 1326}
f0706e82 1327
eadc8d9e
RR
1328static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1329 struct ieee80211_mgmt *mgmt,
1330 size_t len)
1331{
1332 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1333 struct ieee80211_hw *hw = &local->hw;
1334 struct sta_info *sta;
1335 u16 capab;
1336 u16 tid;
1337 u8 *state;
1338
d0709a65
JB
1339 rcu_read_lock();
1340
eadc8d9e 1341 sta = sta_info_get(local, mgmt->sa);
d0709a65
JB
1342 if (!sta) {
1343 rcu_read_unlock();
eadc8d9e 1344 return;
d0709a65 1345 }
eadc8d9e
RR
1346
1347 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1348 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1349
cee24a3e 1350 state = &sta->ampdu_mlme.tid_state_tx[tid];
eadc8d9e
RR
1351
1352 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1353
cee24a3e
RR
1354 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1355 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1356 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1357 "%d\n", *state);
1358 goto addba_resp_exit;
1359 }
1360
eadc8d9e 1361 if (mgmt->u.action.u.addba_resp.dialog_token !=
cee24a3e 1362 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
eadc8d9e
RR
1363 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1364#ifdef CONFIG_MAC80211_HT_DEBUG
1365 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1366#endif /* CONFIG_MAC80211_HT_DEBUG */
cee24a3e 1367 goto addba_resp_exit;
eadc8d9e
RR
1368 }
1369
cee24a3e 1370 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
eadc8d9e
RR
1371#ifdef CONFIG_MAC80211_HT_DEBUG
1372 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1373#endif /* CONFIG_MAC80211_HT_DEBUG */
1374 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1375 == WLAN_STATUS_SUCCESS) {
eadc8d9e
RR
1376 if (*state & HT_ADDBA_RECEIVED_MSK)
1377 printk(KERN_DEBUG "double addBA response\n");
1378
1379 *state |= HT_ADDBA_RECEIVED_MSK;
cee24a3e 1380 sta->ampdu_mlme.addba_req_num[tid] = 0;
eadc8d9e
RR
1381
1382 if (*state == HT_AGG_STATE_OPERATIONAL) {
1383 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1384 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1385 }
1386
1387 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1388 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1389 } else {
1390 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1391
cee24a3e 1392 sta->ampdu_mlme.addba_req_num[tid]++;
eadc8d9e
RR
1393 /* this will allow the state check in stop_BA_session */
1394 *state = HT_AGG_STATE_OPERATIONAL;
1395 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1396 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1397 WLAN_BACK_INITIATOR);
1398 }
cee24a3e
RR
1399
1400addba_resp_exit:
d0709a65 1401 rcu_read_unlock();
eadc8d9e
RR
1402}
1403
1404void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1405 u16 initiator, u16 reason_code)
07db2183
RR
1406{
1407 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1408 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1409 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1410 struct sk_buff *skb;
1411 struct ieee80211_mgmt *mgmt;
1412 u16 params;
1413
1414 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1415 sizeof(mgmt->u.action.u.delba));
1416
1417 if (!skb) {
1418 printk(KERN_ERR "%s: failed to allocate buffer "
1419 "for delba frame\n", dev->name);
1420 return;
1421 }
1422
1423 skb_reserve(skb, local->hw.extra_tx_headroom);
1424 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1425 memset(mgmt, 0, 24);
1426 memcpy(mgmt->da, da, ETH_ALEN);
1427 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
51fb61e7 1428 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
07db2183
RR
1429 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1430 else
1431 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1432 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1433 IEEE80211_STYPE_ACTION);
1434
1435 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1436
1437 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1438 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1439 params = (u16)(initiator << 11); /* bit 11 initiator */
1440 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1441
1442 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1443 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1444
1445 ieee80211_sta_tx(dev, skb, 0);
1446}
1447
1448void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1449 u16 initiator, u16 reason)
1450{
1451 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1452 struct ieee80211_hw *hw = &local->hw;
1453 struct sta_info *sta;
b580781e 1454 int ret, i;
513a1025 1455 DECLARE_MAC_BUF(mac);
07db2183 1456
d0709a65
JB
1457 rcu_read_lock();
1458
07db2183 1459 sta = sta_info_get(local, ra);
d0709a65
JB
1460 if (!sta) {
1461 rcu_read_unlock();
07db2183 1462 return;
d0709a65 1463 }
07db2183
RR
1464
1465 /* check if TID is in operational state */
1466 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
cee24a3e 1467 if (sta->ampdu_mlme.tid_state_rx[tid]
07db2183
RR
1468 != HT_AGG_STATE_OPERATIONAL) {
1469 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
d0709a65 1470 rcu_read_unlock();
07db2183
RR
1471 return;
1472 }
cee24a3e 1473 sta->ampdu_mlme.tid_state_rx[tid] =
07db2183
RR
1474 HT_AGG_STATE_REQ_STOP_BA_MSK |
1475 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
513a1025 1476 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
07db2183
RR
1477
1478 /* stop HW Rx aggregation. ampdu_action existence
1479 * already verified in session init so we add the BUG_ON */
1480 BUG_ON(!local->ops->ampdu_action);
1481
513a1025
RR
1482#ifdef CONFIG_MAC80211_HT_DEBUG
1483 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1484 print_mac(mac, ra), tid);
1485#endif /* CONFIG_MAC80211_HT_DEBUG */
1486
07db2183 1487 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
0df3ef45 1488 ra, tid, NULL);
07db2183
RR
1489 if (ret)
1490 printk(KERN_DEBUG "HW problem - can not stop rx "
1491 "aggergation for tid %d\n", tid);
1492
1493 /* shutdown timer has not expired */
1494 if (initiator != WLAN_BACK_TIMER)
cee24a3e 1495 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
07db2183
RR
1496
1497 /* check if this is a self generated aggregation halt */
1498 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1499 ieee80211_send_delba(dev, ra, tid, 0, reason);
1500
1501 /* free the reordering buffer */
cee24a3e
RR
1502 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1503 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
b580781e 1504 /* release the reordered frames */
cee24a3e
RR
1505 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1506 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1507 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
b580781e
RR
1508 }
1509 }
cee24a3e
RR
1510 /* free resources */
1511 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1512 kfree(sta->ampdu_mlme.tid_rx[tid]);
1513 sta->ampdu_mlme.tid_rx[tid] = NULL;
1514 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
07db2183 1515
d0709a65 1516 rcu_read_unlock();
07db2183
RR
1517}
1518
eadc8d9e 1519
688b88a4
RR
1520static void ieee80211_sta_process_delba(struct net_device *dev,
1521 struct ieee80211_mgmt *mgmt, size_t len)
1522{
1523 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1524 struct sta_info *sta;
1525 u16 tid, params;
1526 u16 initiator;
1527 DECLARE_MAC_BUF(mac);
1528
d0709a65
JB
1529 rcu_read_lock();
1530
688b88a4 1531 sta = sta_info_get(local, mgmt->sa);
d0709a65
JB
1532 if (!sta) {
1533 rcu_read_unlock();
688b88a4 1534 return;
d0709a65 1535 }
688b88a4
RR
1536
1537 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1538 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1539 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1540
1541#ifdef CONFIG_MAC80211_HT_DEBUG
1542 if (net_ratelimit())
d92684e6
RR
1543 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1544 print_mac(mac, mgmt->sa),
2e354ed7 1545 initiator ? "initiator" : "recipient", tid,
688b88a4
RR
1546 mgmt->u.action.u.delba.reason_code);
1547#endif /* CONFIG_MAC80211_HT_DEBUG */
1548
1549 if (initiator == WLAN_BACK_INITIATOR)
1550 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1551 WLAN_BACK_INITIATOR, 0);
d92684e6
RR
1552 else { /* WLAN_BACK_RECIPIENT */
1553 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
cee24a3e 1554 sta->ampdu_mlme.tid_state_tx[tid] =
d92684e6
RR
1555 HT_AGG_STATE_OPERATIONAL;
1556 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1557 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1558 WLAN_BACK_RECIPIENT);
1559 }
d0709a65 1560 rcu_read_unlock();
688b88a4
RR
1561}
1562
eadc8d9e
RR
1563/*
1564 * After sending add Block Ack request we activated a timer until
1565 * add Block Ack response will arrive from the recipient.
1566 * If this timer expires sta_addba_resp_timer_expired will be executed.
1567 */
1568void sta_addba_resp_timer_expired(unsigned long data)
1569{
1570 /* not an elegant detour, but there is no choice as the timer passes
1571 * only one argument, and both sta_info and TID are needed, so init
73651ee6 1572 * flow in sta_info_create gives the TID as data, while the timer_to_id
eadc8d9e
RR
1573 * array gives the sta through container_of */
1574 u16 tid = *(int *)data;
1575 struct sta_info *temp_sta = container_of((void *)data,
1576 struct sta_info, timer_to_tid[tid]);
1577
1578 struct ieee80211_local *local = temp_sta->local;
1579 struct ieee80211_hw *hw = &local->hw;
1580 struct sta_info *sta;
1581 u8 *state;
1582
d0709a65
JB
1583 rcu_read_lock();
1584
eadc8d9e 1585 sta = sta_info_get(local, temp_sta->addr);
d0709a65
JB
1586 if (!sta) {
1587 rcu_read_unlock();
eadc8d9e 1588 return;
d0709a65 1589 }
eadc8d9e 1590
cee24a3e 1591 state = &sta->ampdu_mlme.tid_state_tx[tid];
eadc8d9e
RR
1592 /* check if the TID waits for addBA response */
1593 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1594 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1595 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1596 *state = HT_AGG_STATE_IDLE;
1597 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1598 "expecting addBA response there", tid);
1599 goto timer_expired_exit;
1600 }
1601
1602 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1603
1604 /* go through the state check in stop_BA_session */
1605 *state = HT_AGG_STATE_OPERATIONAL;
1606 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1607 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1608 WLAN_BACK_INITIATOR);
1609
1610timer_expired_exit:
d0709a65 1611 rcu_read_unlock();
eadc8d9e
RR
1612}
1613
07db2183 1614/*
7b9d44cd
RR
1615 * After accepting the AddBA Request we activated a timer,
1616 * resetting it after each frame that arrives from the originator.
07db2183
RR
1617 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1618 */
1619void sta_rx_agg_session_timer_expired(unsigned long data)
1620{
1621 /* not an elegant detour, but there is no choice as the timer passes
1622 * only one argument, and verious sta_info are needed here, so init
73651ee6 1623 * flow in sta_info_create gives the TID as data, while the timer_to_id
07db2183
RR
1624 * array gives the sta through container_of */
1625 u8 *ptid = (u8 *)data;
1626 u8 *timer_to_id = ptid - *ptid;
1627 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1628 timer_to_tid[0]);
1629
1630 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
d0709a65
JB
1631 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1632 (u16)*ptid, WLAN_BACK_TIMER,
07db2183
RR
1633 WLAN_REASON_QSTA_TIMEOUT);
1634}
1635
85249e5f
RR
1636void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1637{
1638 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1639 int i;
1640
1641 for (i = 0; i < STA_TID_NUM; i++) {
1642 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1643 WLAN_BACK_INITIATOR);
1644 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1645 WLAN_BACK_RECIPIENT,
1646 WLAN_REASON_QSTA_LEAVE_QBSS);
1647 }
1648}
07db2183 1649
f0706e82
JB
1650static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1651 struct ieee80211_if_sta *ifsta,
1652 struct ieee80211_mgmt *mgmt,
1653 size_t len)
1654{
1655 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1656 u16 auth_alg, auth_transaction, status_code;
0795af57 1657 DECLARE_MAC_BUF(mac);
f0706e82
JB
1658
1659 if (ifsta->state != IEEE80211_AUTHENTICATE &&
51fb61e7 1660 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
f0706e82 1661 printk(KERN_DEBUG "%s: authentication frame received from "
0795af57
JP
1662 "%s, but not in authenticate state - ignored\n",
1663 dev->name, print_mac(mac, mgmt->sa));
f0706e82
JB
1664 return;
1665 }
1666
1667 if (len < 24 + 6) {
1668 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
0795af57
JP
1669 "received from %s - ignored\n",
1670 dev->name, len, print_mac(mac, mgmt->sa));
f0706e82
JB
1671 return;
1672 }
1673
51fb61e7 1674 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
f0706e82
JB
1675 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1676 printk(KERN_DEBUG "%s: authentication frame received from "
0795af57
JP
1677 "unknown AP (SA=%s BSSID=%s) - "
1678 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1679 print_mac(mac, mgmt->bssid));
f0706e82
JB
1680 return;
1681 }
1682
51fb61e7 1683 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
f0706e82
JB
1684 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1685 printk(KERN_DEBUG "%s: authentication frame received from "
0795af57
JP
1686 "unknown BSSID (SA=%s BSSID=%s) - "
1687 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1688 print_mac(mac, mgmt->bssid));
f0706e82
JB
1689 return;
1690 }
1691
1692 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1693 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1694 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1695
0795af57 1696 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
f0706e82 1697 "transaction=%d status=%d)\n",
0795af57 1698 dev->name, print_mac(mac, mgmt->sa), auth_alg,
f0706e82
JB
1699 auth_transaction, status_code);
1700
51fb61e7 1701 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
f0706e82
JB
1702 /* IEEE 802.11 standard does not require authentication in IBSS
1703 * networks and most implementations do not seem to use it.
1704 * However, try to reply to authentication attempts if someone
1705 * has actually implemented this.
1706 * TODO: Could implement shared key authentication. */
1707 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1708 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1709 "frame (alg=%d transaction=%d)\n",
1710 dev->name, auth_alg, auth_transaction);
1711 return;
1712 }
1713 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1714 }
1715
1716 if (auth_alg != ifsta->auth_alg ||
1717 auth_transaction != ifsta->auth_transaction) {
1718 printk(KERN_DEBUG "%s: unexpected authentication frame "
1719 "(alg=%d transaction=%d)\n",
1720 dev->name, auth_alg, auth_transaction);
1721 return;
1722 }
1723
1724 if (status_code != WLAN_STATUS_SUCCESS) {
1725 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1726 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1727 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1728 u8 algs[3];
1729 const int num_algs = ARRAY_SIZE(algs);
1730 int i, pos;
1731 algs[0] = algs[1] = algs[2] = 0xff;
1732 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1733 algs[0] = WLAN_AUTH_OPEN;
1734 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1735 algs[1] = WLAN_AUTH_SHARED_KEY;
1736 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1737 algs[2] = WLAN_AUTH_LEAP;
1738 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1739 pos = 0;
1740 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1741 pos = 1;
1742 else
1743 pos = 2;
1744 for (i = 0; i < num_algs; i++) {
1745 pos++;
1746 if (pos >= num_algs)
1747 pos = 0;
1748 if (algs[pos] == ifsta->auth_alg ||
1749 algs[pos] == 0xff)
1750 continue;
1751 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1752 !ieee80211_sta_wep_configured(dev))
1753 continue;
1754 ifsta->auth_alg = algs[pos];
1755 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1756 "next try\n",
1757 dev->name, ifsta->auth_alg);
1758 break;
1759 }
1760 }
1761 return;
1762 }
1763
1764 switch (ifsta->auth_alg) {
1765 case WLAN_AUTH_OPEN:
1766 case WLAN_AUTH_LEAP:
1767 ieee80211_auth_completed(dev, ifsta);
1768 break;
1769 case WLAN_AUTH_SHARED_KEY:
1770 if (ifsta->auth_transaction == 4)
1771 ieee80211_auth_completed(dev, ifsta);
1772 else
1773 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1774 break;
1775 }
1776}
1777
1778
1779static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1780 struct ieee80211_if_sta *ifsta,
1781 struct ieee80211_mgmt *mgmt,
1782 size_t len)
1783{
1784 u16 reason_code;
0795af57 1785 DECLARE_MAC_BUF(mac);
f0706e82
JB
1786
1787 if (len < 24 + 2) {
1788 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
0795af57
JP
1789 "received from %s - ignored\n",
1790 dev->name, len, print_mac(mac, mgmt->sa));
f0706e82
JB
1791 return;
1792 }
1793
1794 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1795 printk(KERN_DEBUG "%s: deauthentication frame received from "
0795af57
JP
1796 "unknown AP (SA=%s BSSID=%s) - "
1797 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1798 print_mac(mac, mgmt->bssid));
f0706e82
JB
1799 return;
1800 }
1801
1802 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1803
0795af57 1804 printk(KERN_DEBUG "%s: RX deauthentication from %s"
f0706e82 1805 " (reason=%d)\n",
0795af57 1806 dev->name, print_mac(mac, mgmt->sa), reason_code);
f0706e82 1807
d6f2da5b 1808 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
f0706e82
JB
1809 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1810 }
1811
1812 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1813 ifsta->state == IEEE80211_ASSOCIATE ||
1814 ifsta->state == IEEE80211_ASSOCIATED) {
1815 ifsta->state = IEEE80211_AUTHENTICATE;
1816 mod_timer(&ifsta->timer, jiffies +
1817 IEEE80211_RETRY_AUTH_INTERVAL);
1818 }
1819
1820 ieee80211_set_disassoc(dev, ifsta, 1);
d6f2da5b 1821 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
f0706e82
JB
1822}
1823
1824
1825static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1826 struct ieee80211_if_sta *ifsta,
1827 struct ieee80211_mgmt *mgmt,
1828 size_t len)
1829{
1830 u16 reason_code;
0795af57 1831 DECLARE_MAC_BUF(mac);
f0706e82
JB
1832
1833 if (len < 24 + 2) {
1834 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
0795af57
JP
1835 "received from %s - ignored\n",
1836 dev->name, len, print_mac(mac, mgmt->sa));
f0706e82
JB
1837 return;
1838 }
1839
1840 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1841 printk(KERN_DEBUG "%s: disassociation frame received from "
0795af57
JP
1842 "unknown AP (SA=%s BSSID=%s) - "
1843 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1844 print_mac(mac, mgmt->bssid));
f0706e82
JB
1845 return;
1846 }
1847
1848 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1849
0795af57 1850 printk(KERN_DEBUG "%s: RX disassociation from %s"
f0706e82 1851 " (reason=%d)\n",
0795af57 1852 dev->name, print_mac(mac, mgmt->sa), reason_code);
f0706e82 1853
d6f2da5b 1854 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
f0706e82
JB
1855 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1856
1857 if (ifsta->state == IEEE80211_ASSOCIATED) {
1858 ifsta->state = IEEE80211_ASSOCIATE;
1859 mod_timer(&ifsta->timer, jiffies +
1860 IEEE80211_RETRY_AUTH_INTERVAL);
1861 }
1862
1863 ieee80211_set_disassoc(dev, ifsta, 0);
1864}
1865
1866
471b3efd 1867static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1868 struct ieee80211_if_sta *ifsta,
1869 struct ieee80211_mgmt *mgmt,
1870 size_t len,
1871 int reassoc)
1872{
471b3efd
JB
1873 struct ieee80211_local *local = sdata->local;
1874 struct net_device *dev = sdata->dev;
8318d78a 1875 struct ieee80211_supported_band *sband;
f0706e82 1876 struct sta_info *sta;
8318d78a 1877 u64 rates, basic_rates;
f0706e82
JB
1878 u16 capab_info, status_code, aid;
1879 struct ieee802_11_elems elems;
471b3efd 1880 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
f0706e82
JB
1881 u8 *pos;
1882 int i, j;
0795af57 1883 DECLARE_MAC_BUF(mac);
8318d78a 1884 bool have_higher_than_11mbit = false;
f0706e82
JB
1885
1886 /* AssocResp and ReassocResp have identical structure, so process both
1887 * of them in this function. */
1888
1889 if (ifsta->state != IEEE80211_ASSOCIATE) {
1890 printk(KERN_DEBUG "%s: association frame received from "
0795af57
JP
1891 "%s, but not in associate state - ignored\n",
1892 dev->name, print_mac(mac, mgmt->sa));
f0706e82
JB
1893 return;
1894 }
1895
1896 if (len < 24 + 6) {
1897 printk(KERN_DEBUG "%s: too short (%zd) association frame "
0795af57
JP
1898 "received from %s - ignored\n",
1899 dev->name, len, print_mac(mac, mgmt->sa));
f0706e82
JB
1900 return;
1901 }
1902
1903 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1904 printk(KERN_DEBUG "%s: association frame received from "
0795af57
JP
1905 "unknown AP (SA=%s BSSID=%s) - "
1906 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1907 print_mac(mac, mgmt->bssid));
f0706e82
JB
1908 return;
1909 }
1910
1911 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1912 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1913 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
f0706e82 1914
0795af57 1915 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
f0706e82 1916 "status=%d aid=%d)\n",
0795af57 1917 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
ddd68587 1918 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
f0706e82
JB
1919
1920 if (status_code != WLAN_STATUS_SUCCESS) {
1921 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1922 dev->name, status_code);
8a69aa93
DD
1923 /* if this was a reassociation, ensure we try a "full"
1924 * association next time. This works around some broken APs
1925 * which do not correctly reject reassociation requests. */
d6f2da5b 1926 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
f0706e82
JB
1927 return;
1928 }
1929
1dd84aa2
JB
1930 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1931 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1932 "set\n", dev->name, aid);
1933 aid &= ~(BIT(15) | BIT(14));
1934
f0706e82 1935 pos = mgmt->u.assoc_resp.variable;
67a4cce4 1936 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
f0706e82
JB
1937
1938 if (!elems.supp_rates) {
1939 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1940 dev->name);
1941 return;
1942 }
1943
1944 printk(KERN_DEBUG "%s: associated\n", dev->name);
1945 ifsta->aid = aid;
1946 ifsta->ap_capab = capab_info;
1947
1948 kfree(ifsta->assocresp_ies);
1949 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
0ec0b7ac 1950 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
f0706e82
JB
1951 if (ifsta->assocresp_ies)
1952 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1953
d0709a65
JB
1954 rcu_read_lock();
1955
f0706e82
JB
1956 /* Add STA entry for the AP */
1957 sta = sta_info_get(local, ifsta->bssid);
1958 if (!sta) {
1959 struct ieee80211_sta_bss *bss;
73651ee6 1960 int err;
d0709a65 1961
73651ee6
JB
1962 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1963 if (!sta) {
1964 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1965 " the AP\n", dev->name);
d0709a65 1966 rcu_read_unlock();
f0706e82
JB
1967 return;
1968 }
65c107ab 1969 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
8318d78a 1970 local->hw.conf.channel->center_freq,
cffdd30d 1971 ifsta->ssid, ifsta->ssid_len);
f0706e82
JB
1972 if (bss) {
1973 sta->last_rssi = bss->rssi;
1974 sta->last_signal = bss->signal;
1975 sta->last_noise = bss->noise;
1976 ieee80211_rx_bss_put(dev, bss);
1977 }
73651ee6
JB
1978
1979 err = sta_info_insert(sta);
1980 if (err) {
1981 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1982 " the AP (error %d)\n", dev->name, err);
73651ee6
JB
1983 rcu_read_unlock();
1984 return;
1985 }
f0706e82
JB
1986 }
1987
73651ee6
JB
1988 /*
1989 * FIXME: Do we really need to update the sta_info's information here?
1990 * We already know about the AP (we found it in our list) so it
1991 * should already be filled with the right info, no?
1992 * As is stands, all this is racy because typically we assume
1993 * the information that is filled in here (except flags) doesn't
1994 * change while a STA structure is alive. As such, it should move
1995 * to between the sta_info_alloc() and sta_info_insert() above.
1996 */
1997
238814fd
JB
1998 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1999 WLAN_STA_AUTHORIZED;
f0706e82
JB
2000
2001 rates = 0;
8318d78a
JB
2002 basic_rates = 0;
2003 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2004
f0706e82
JB
2005 for (i = 0; i < elems.supp_rates_len; i++) {
2006 int rate = (elems.supp_rates[i] & 0x7f) * 5;
8318d78a
JB
2007
2008 if (rate > 110)
2009 have_higher_than_11mbit = true;
2010
2011 for (j = 0; j < sband->n_bitrates; j++) {
2012 if (sband->bitrates[j].bitrate == rate)
f0706e82 2013 rates |= BIT(j);
8318d78a
JB
2014 if (elems.supp_rates[i] & 0x80)
2015 basic_rates |= BIT(j);
2016 }
f0706e82 2017 }
8318d78a 2018
f0706e82
JB
2019 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2020 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
8318d78a
JB
2021
2022 if (rate > 110)
2023 have_higher_than_11mbit = true;
2024
2025 for (j = 0; j < sband->n_bitrates; j++) {
2026 if (sband->bitrates[j].bitrate == rate)
f0706e82 2027 rates |= BIT(j);
8318d78a
JB
2028 if (elems.ext_supp_rates[i] & 0x80)
2029 basic_rates |= BIT(j);
2030 }
f0706e82 2031 }
8318d78a
JB
2032
2033 sta->supp_rates[local->hw.conf.channel->band] = rates;
2034 sdata->basic_rates = basic_rates;
2035
2036 /* cf. IEEE 802.11 9.2.12 */
2037 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2038 have_higher_than_11mbit)
2039 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2040 else
2041 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
f0706e82 2042
38668c05 2043 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
d3c990fb 2044 struct ieee80211_ht_bss_info bss_info;
d3c990fb
RR
2045 ieee80211_ht_cap_ie_to_ht_info(
2046 (struct ieee80211_ht_cap *)
2047 elems.ht_cap_elem, &sta->ht_info);
2048 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2049 (struct ieee80211_ht_addt_info *)
2050 elems.ht_info_elem, &bss_info);
38668c05 2051 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
d3c990fb
RR
2052 }
2053
f0706e82
JB
2054 rate_control_rate_init(sta, local);
2055
d6f2da5b 2056 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
f0706e82 2057 sta->flags |= WLAN_STA_WME;
e5f98f2d 2058 rcu_read_unlock();
f0706e82
JB
2059 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2060 elems.wmm_param_len);
e5f98f2d
JB
2061 } else
2062 rcu_read_unlock();
f0706e82 2063
21c0cbe7
TW
2064 /* set AID and assoc capability,
2065 * ieee80211_set_associated() will tell the driver */
8318d78a 2066 bss_conf->aid = aid;
21c0cbe7 2067 bss_conf->assoc_capability = capab_info;
8318d78a 2068 ieee80211_set_associated(dev, ifsta, 1);
f0706e82 2069
f0706e82
JB
2070 ieee80211_associated(dev, ifsta);
2071}
2072
2073
2074/* Caller must hold local->sta_bss_lock */
2075static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2076 struct ieee80211_sta_bss *bss)
2077{
2078 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
f709fc69 2079 u8 hash_idx;
902acc78
JB
2080
2081 if (bss_mesh_cfg(bss))
2082 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2083 bss_mesh_id_len(bss));
f709fc69 2084 else
f709fc69 2085 hash_idx = STA_HASH(bss->bssid);
902acc78 2086
f709fc69
LCC
2087 bss->hnext = local->sta_bss_hash[hash_idx];
2088 local->sta_bss_hash[hash_idx] = bss;
f0706e82
JB
2089}
2090
2091
2092/* Caller must hold local->sta_bss_lock */
2093static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2094 struct ieee80211_sta_bss *bss)
2095{
2096 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2097 struct ieee80211_sta_bss *b, *prev = NULL;
2098 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2099 while (b) {
2100 if (b == bss) {
2101 if (!prev)
2102 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2103 bss->hnext;
2104 else
2105 prev->hnext = bss->hnext;
2106 break;
2107 }
2108 prev = b;
2109 b = b->hnext;
2110 }
2111}
2112
2113
2114static struct ieee80211_sta_bss *
8318d78a 2115ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
cffdd30d 2116 u8 *ssid, u8 ssid_len)
f0706e82
JB
2117{
2118 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2119 struct ieee80211_sta_bss *bss;
2120
dd00cc48 2121 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
f0706e82
JB
2122 if (!bss)
2123 return NULL;
f0706e82
JB
2124 atomic_inc(&bss->users);
2125 atomic_inc(&bss->users);
2126 memcpy(bss->bssid, bssid, ETH_ALEN);
8318d78a 2127 bss->freq = freq;
cffdd30d
JL
2128 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2129 memcpy(bss->ssid, ssid, ssid_len);
2130 bss->ssid_len = ssid_len;
2131 }
f0706e82
JB
2132
2133 spin_lock_bh(&local->sta_bss_lock);
2134 /* TODO: order by RSSI? */
2135 list_add_tail(&bss->list, &local->sta_bss_list);
2136 __ieee80211_rx_bss_hash_add(dev, bss);
2137 spin_unlock_bh(&local->sta_bss_lock);
2138 return bss;
2139}
2140
f0706e82 2141static struct ieee80211_sta_bss *
8318d78a 2142ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
cffdd30d 2143 u8 *ssid, u8 ssid_len)
f0706e82
JB
2144{
2145 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2146 struct ieee80211_sta_bss *bss;
2147
2148 spin_lock_bh(&local->sta_bss_lock);
2149 bss = local->sta_bss_hash[STA_HASH(bssid)];
2150 while (bss) {
902acc78
JB
2151 if (!bss_mesh_cfg(bss) &&
2152 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
8318d78a 2153 bss->freq == freq &&
cffdd30d
JL
2154 bss->ssid_len == ssid_len &&
2155 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
f0706e82
JB
2156 atomic_inc(&bss->users);
2157 break;
2158 }
2159 bss = bss->hnext;
2160 }
2161 spin_unlock_bh(&local->sta_bss_lock);
2162 return bss;
2163}
2164
f709fc69
LCC
2165#ifdef CONFIG_MAC80211_MESH
2166static struct ieee80211_sta_bss *
2167ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2168 u8 *mesh_cfg, int freq)
2169{
2170 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2171 struct ieee80211_sta_bss *bss;
2172
2173 spin_lock_bh(&local->sta_bss_lock);
2174 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2175 while (bss) {
902acc78
JB
2176 if (bss_mesh_cfg(bss) &&
2177 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
f709fc69
LCC
2178 bss->freq == freq &&
2179 mesh_id_len == bss->mesh_id_len &&
2180 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2181 mesh_id_len))) {
2182 atomic_inc(&bss->users);
2183 break;
2184 }
2185 bss = bss->hnext;
2186 }
2187 spin_unlock_bh(&local->sta_bss_lock);
2188 return bss;
2189}
2190
2191static struct ieee80211_sta_bss *
2192ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
05e5e883 2193 u8 *mesh_cfg, int mesh_config_len, int freq)
f709fc69
LCC
2194{
2195 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2196 struct ieee80211_sta_bss *bss;
2197
05e5e883
LCC
2198 if (mesh_config_len != MESH_CFG_LEN)
2199 return NULL;
2200
f709fc69
LCC
2201 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2202 if (!bss)
2203 return NULL;
2204
24736701 2205 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
f709fc69
LCC
2206 if (!bss->mesh_cfg) {
2207 kfree(bss);
2208 return NULL;
2209 }
2210
2211 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2212 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2213 if (!bss->mesh_id) {
2214 kfree(bss->mesh_cfg);
2215 kfree(bss);
2216 return NULL;
2217 }
2218 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2219 }
2220
2221 atomic_inc(&bss->users);
2222 atomic_inc(&bss->users);
24736701 2223 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
f709fc69
LCC
2224 bss->mesh_id_len = mesh_id_len;
2225 bss->freq = freq;
2226 spin_lock_bh(&local->sta_bss_lock);
2227 /* TODO: order by RSSI? */
2228 list_add_tail(&bss->list, &local->sta_bss_list);
2229 __ieee80211_rx_bss_hash_add(dev, bss);
2230 spin_unlock_bh(&local->sta_bss_lock);
2231 return bss;
2232}
2233#endif
f0706e82
JB
2234
2235static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2236{
2237 kfree(bss->wpa_ie);
2238 kfree(bss->rsn_ie);
2239 kfree(bss->wmm_ie);
c7153508 2240 kfree(bss->ht_ie);
902acc78
JB
2241 kfree(bss_mesh_id(bss));
2242 kfree(bss_mesh_cfg(bss));
f0706e82
JB
2243 kfree(bss);
2244}
2245
2246
2247static void ieee80211_rx_bss_put(struct net_device *dev,
2248 struct ieee80211_sta_bss *bss)
2249{
2250 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1ebebea8
PE
2251
2252 local_bh_disable();
2253 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2254 local_bh_enable();
f0706e82 2255 return;
1ebebea8 2256 }
f0706e82 2257
f0706e82
JB
2258 __ieee80211_rx_bss_hash_del(dev, bss);
2259 list_del(&bss->list);
2260 spin_unlock_bh(&local->sta_bss_lock);
2261 ieee80211_rx_bss_free(bss);
2262}
2263
2264
2265void ieee80211_rx_bss_list_init(struct net_device *dev)
2266{
2267 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2268 spin_lock_init(&local->sta_bss_lock);
2269 INIT_LIST_HEAD(&local->sta_bss_list);
2270}
2271
2272
2273void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2274{
2275 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2276 struct ieee80211_sta_bss *bss, *tmp;
2277
2278 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2279 ieee80211_rx_bss_put(dev, bss);
2280}
2281
2282
a607268a
BR
2283static int ieee80211_sta_join_ibss(struct net_device *dev,
2284 struct ieee80211_if_sta *ifsta,
2285 struct ieee80211_sta_bss *bss)
2286{
2287 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2288 int res, rates, i, j;
2289 struct sk_buff *skb;
2290 struct ieee80211_mgmt *mgmt;
2291 struct ieee80211_tx_control control;
2292 struct rate_selection ratesel;
2293 u8 *pos;
2294 struct ieee80211_sub_if_data *sdata;
2295 struct ieee80211_supported_band *sband;
2296
2297 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2298
9dd6aed0
JB
2299 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2300
a607268a 2301 /* Remove possible STA entries from other IBSS networks. */
dc6676b7 2302 sta_info_flush_delayed(sdata);
a607268a
BR
2303
2304 if (local->ops->reset_tsf) {
2305 /* Reset own TSF to allow time synchronization work. */
2306 local->ops->reset_tsf(local_to_hw(local));
2307 }
2308 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2309 res = ieee80211_if_config(dev);
2310 if (res)
2311 return res;
2312
2313 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2314
a607268a
BR
2315 sdata->drop_unencrypted = bss->capability &
2316 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2317
2318 res = ieee80211_set_freq(local, bss->freq);
2319
2320 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2321 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2322 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2323 return -1;
2324 }
2325
2326 /* Set beacon template */
2327 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2328 do {
2329 if (!skb)
2330 break;
2331
2332 skb_reserve(skb, local->hw.extra_tx_headroom);
2333
2334 mgmt = (struct ieee80211_mgmt *)
2335 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2336 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2337 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2338 IEEE80211_STYPE_BEACON);
2339 memset(mgmt->da, 0xff, ETH_ALEN);
2340 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2341 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2342 mgmt->u.beacon.beacon_int =
2343 cpu_to_le16(local->hw.conf.beacon_int);
2344 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2345
2346 pos = skb_put(skb, 2 + ifsta->ssid_len);
2347 *pos++ = WLAN_EID_SSID;
2348 *pos++ = ifsta->ssid_len;
2349 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2350
2351 rates = bss->supp_rates_len;
2352 if (rates > 8)
2353 rates = 8;
2354 pos = skb_put(skb, 2 + rates);
2355 *pos++ = WLAN_EID_SUPP_RATES;
2356 *pos++ = rates;
2357 memcpy(pos, bss->supp_rates, rates);
2358
2359 if (bss->band == IEEE80211_BAND_2GHZ) {
2360 pos = skb_put(skb, 2 + 1);
2361 *pos++ = WLAN_EID_DS_PARAMS;
2362 *pos++ = 1;
2363 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2364 }
2365
2366 pos = skb_put(skb, 2 + 2);
2367 *pos++ = WLAN_EID_IBSS_PARAMS;
2368 *pos++ = 2;
2369 /* FIX: set ATIM window based on scan results */
2370 *pos++ = 0;
2371 *pos++ = 0;
2372
2373 if (bss->supp_rates_len > 8) {
2374 rates = bss->supp_rates_len - 8;
2375 pos = skb_put(skb, 2 + rates);
2376 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2377 *pos++ = rates;
2378 memcpy(pos, &bss->supp_rates[8], rates);
2379 }
2380
2381 memset(&control, 0, sizeof(control));
2382 rate_control_get_rate(dev, sband, skb, &ratesel);
2383 if (!ratesel.rate) {
2384 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2385 "for IBSS beacon\n", dev->name);
2386 break;
2387 }
2388 control.vif = &sdata->vif;
2389 control.tx_rate = ratesel.rate;
2390 if (sdata->bss_conf.use_short_preamble &&
2391 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2392 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2393 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2394 control.flags |= IEEE80211_TXCTL_NO_ACK;
2395 control.retry_limit = 1;
2396
2397 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2398 if (ifsta->probe_resp) {
2399 mgmt = (struct ieee80211_mgmt *)
2400 ifsta->probe_resp->data;
2401 mgmt->frame_control =
2402 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2403 IEEE80211_STYPE_PROBE_RESP);
2404 } else {
2405 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2406 "template for IBSS\n", dev->name);
2407 }
2408
2409 if (local->ops->beacon_update &&
2410 local->ops->beacon_update(local_to_hw(local),
2411 skb, &control) == 0) {
2412 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2413 "template\n", dev->name);
2414 skb = NULL;
2415 }
2416
2417 rates = 0;
2418 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2419 for (i = 0; i < bss->supp_rates_len; i++) {
2420 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2421 for (j = 0; j < sband->n_bitrates; j++)
2422 if (sband->bitrates[j].bitrate == bitrate)
2423 rates |= BIT(j);
2424 }
2425 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
e2839d8f
VK
2426
2427 ieee80211_sta_def_wmm_params(dev, bss, 1);
a607268a
BR
2428 } while (0);
2429
2430 if (skb) {
2431 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2432 "template\n", dev->name);
2433 dev_kfree_skb(skb);
2434 }
2435
2436 ifsta->state = IEEE80211_IBSS_JOINED;
2437 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2438
2439 ieee80211_rx_bss_put(dev, bss);
2440
2441 return res;
2442}
2443
f709fc69
LCC
2444u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2445 struct ieee802_11_elems *elems,
2446 enum ieee80211_band band)
2447{
2448 struct ieee80211_supported_band *sband;
2449 struct ieee80211_rate *bitrates;
2450 size_t num_rates;
2451 u64 supp_rates;
2452 int i, j;
2453 sband = local->hw.wiphy->bands[band];
2454
2455 if (!sband) {
2456 WARN_ON(1);
2457 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2458 }
2459
2460 bitrates = sband->bitrates;
2461 num_rates = sband->n_bitrates;
2462 supp_rates = 0;
2463 for (i = 0; i < elems->supp_rates_len +
2464 elems->ext_supp_rates_len; i++) {
2465 u8 rate = 0;
2466 int own_rate;
2467 if (i < elems->supp_rates_len)
2468 rate = elems->supp_rates[i];
2469 else if (elems->ext_supp_rates)
2470 rate = elems->ext_supp_rates
2471 [i - elems->supp_rates_len];
2472 own_rate = 5 * (rate & 0x7f);
2473 for (j = 0; j < num_rates; j++)
2474 if (bitrates[j].bitrate == own_rate)
2475 supp_rates |= BIT(j);
2476 }
2477 return supp_rates;
2478}
2479
a607268a 2480
f0706e82
JB
2481static void ieee80211_rx_bss_info(struct net_device *dev,
2482 struct ieee80211_mgmt *mgmt,
2483 size_t len,
2484 struct ieee80211_rx_status *rx_status,
2485 int beacon)
2486{
2487 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2488 struct ieee802_11_elems elems;
2489 size_t baselen;
8318d78a 2490 int freq, clen;
f0706e82
JB
2491 struct ieee80211_sta_bss *bss;
2492 struct sta_info *sta;
2493 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
9d9bf77d 2494 u64 beacon_timestamp, rx_timestamp;
fab7d4a2 2495 struct ieee80211_channel *channel;
0795af57
JP
2496 DECLARE_MAC_BUF(mac);
2497 DECLARE_MAC_BUF(mac2);
f0706e82
JB
2498
2499 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2500 return; /* ignore ProbeResp to foreign address */
2501
2502#if 0
0795af57 2503 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
f0706e82 2504 dev->name, beacon ? "Beacon" : "Probe Response",
0795af57 2505 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
f0706e82
JB
2506#endif
2507
2508 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2509 if (baselen > len)
2510 return;
2511
9d9bf77d 2512 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
67a4cce4 2513 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
f0706e82 2514
902acc78
JB
2515 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2516 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2517 u64 rates = ieee80211_sta_get_rates(local, &elems,
2518 rx_status->band);
2519
2520 mesh_neighbour_update(mgmt->sa, rates, dev,
2521 mesh_peer_accepts_plinks(&elems, dev));
2522 }
f709fc69 2523
d0709a65
JB
2524 rcu_read_lock();
2525
51fb61e7 2526 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
f0706e82
JB
2527 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2528 (sta = sta_info_get(local, mgmt->sa))) {
f709fc69
LCC
2529 u64 prev_rates;
2530 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2531 rx_status->band);
f0706e82 2532
8318d78a
JB
2533 prev_rates = sta->supp_rates[rx_status->band];
2534 sta->supp_rates[rx_status->band] &= supp_rates;
2535 if (sta->supp_rates[rx_status->band] == 0) {
f0706e82
JB
2536 /* No matching rates - this should not really happen.
2537 * Make sure that at least one rate is marked
2538 * supported to avoid issues with TX rate ctrl. */
8318d78a
JB
2539 sta->supp_rates[rx_status->band] =
2540 sdata->u.sta.supp_rates_bits[rx_status->band];
f0706e82 2541 }
8318d78a 2542 if (sta->supp_rates[rx_status->band] != prev_rates) {
f0706e82 2543 printk(KERN_DEBUG "%s: updated supp_rates set for "
8318d78a
JB
2544 "%s based on beacon info (0x%llx & 0x%llx -> "
2545 "0x%llx)\n",
2546 dev->name, print_mac(mac, sta->addr),
2547 (unsigned long long) prev_rates,
2548 (unsigned long long) supp_rates,
2549 (unsigned long long) sta->supp_rates[rx_status->band]);
f0706e82 2550 }
f0706e82
JB
2551 }
2552
d0709a65
JB
2553 rcu_read_unlock();
2554
f0706e82 2555 if (elems.ds_params && elems.ds_params_len == 1)
8318d78a 2556 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
f0706e82 2557 else
8318d78a 2558 freq = rx_status->freq;
f0706e82 2559
fab7d4a2
JB
2560 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2561
2562 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2563 return;
2564
f709fc69
LCC
2565#ifdef CONFIG_MAC80211_MESH
2566 if (elems.mesh_config)
2567 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2568 elems.mesh_id_len, elems.mesh_config, freq);
2569 else
2570#endif
2571 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
cffdd30d 2572 elems.ssid, elems.ssid_len);
f709fc69
LCC
2573 if (!bss) {
2574#ifdef CONFIG_MAC80211_MESH
2575 if (elems.mesh_config)
2576 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
05e5e883
LCC
2577 elems.mesh_id_len, elems.mesh_config,
2578 elems.mesh_config_len, freq);
f709fc69
LCC
2579 else
2580#endif
2581 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2582 elems.ssid, elems.ssid_len);
f0706e82
JB
2583 if (!bss)
2584 return;
2585 } else {
2586#if 0
2587 /* TODO: order by RSSI? */
2588 spin_lock_bh(&local->sta_bss_lock);
2589 list_move_tail(&bss->list, &local->sta_bss_list);
2590 spin_unlock_bh(&local->sta_bss_lock);
2591#endif
2592 }
2593
5628221c
DD
2594 /* save the ERP value so that it is available at association time */
2595 if (elems.erp_info && elems.erp_info_len >= 1) {
2596 bss->erp_value = elems.erp_info[0];
2597 bss->has_erp_value = 1;
2598 }
2599
30b89b0f
JB
2600 if (elems.ht_cap_elem &&
2601 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2602 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2603 kfree(bss->ht_ie);
2604 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2605 if (bss->ht_ie) {
2606 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2607 elems.ht_cap_elem_len + 2);
2608 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2609 } else
2610 bss->ht_ie_len = 0;
2611 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2612 kfree(bss->ht_ie);
2613 bss->ht_ie = NULL;
2614 bss->ht_ie_len = 0;
2615 }
2616
f0706e82
JB
2617 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2618 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
f0706e82
JB
2619
2620 bss->supp_rates_len = 0;
2621 if (elems.supp_rates) {
2622 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2623 if (clen > elems.supp_rates_len)
2624 clen = elems.supp_rates_len;
2625 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2626 clen);
2627 bss->supp_rates_len += clen;
2628 }
2629 if (elems.ext_supp_rates) {
2630 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2631 if (clen > elems.ext_supp_rates_len)
2632 clen = elems.ext_supp_rates_len;
2633 memcpy(&bss->supp_rates[bss->supp_rates_len],
2634 elems.ext_supp_rates, clen);
2635 bss->supp_rates_len += clen;
2636 }
2637
30b89b0f
JB
2638 bss->band = rx_status->band;
2639
2640 bss->timestamp = beacon_timestamp;
2641 bss->last_update = jiffies;
2642 bss->rssi = rx_status->ssi;
2643 bss->signal = rx_status->signal;
2644 bss->noise = rx_status->noise;
2645 if (!beacon && !bss->probe_resp)
2646 bss->probe_resp = true;
2647
2648 /*
2649 * In STA mode, the remaining parameters should not be overridden
2650 * by beacons because they're not necessarily accurate there.
2651 */
2652 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2653 bss->probe_resp && beacon) {
2654 ieee80211_rx_bss_put(dev, bss);
2655 return;
2656 }
2657
f0706e82
JB
2658 if (elems.wpa &&
2659 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2660 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2661 kfree(bss->wpa_ie);
2662 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2663 if (bss->wpa_ie) {
2664 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2665 bss->wpa_ie_len = elems.wpa_len + 2;
2666 } else
2667 bss->wpa_ie_len = 0;
2668 } else if (!elems.wpa && bss->wpa_ie) {
2669 kfree(bss->wpa_ie);
2670 bss->wpa_ie = NULL;
2671 bss->wpa_ie_len = 0;
2672 }
2673
2674 if (elems.rsn &&
2675 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2676 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2677 kfree(bss->rsn_ie);
2678 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2679 if (bss->rsn_ie) {
2680 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2681 bss->rsn_ie_len = elems.rsn_len + 2;
2682 } else
2683 bss->rsn_ie_len = 0;
2684 } else if (!elems.rsn && bss->rsn_ie) {
2685 kfree(bss->rsn_ie);
2686 bss->rsn_ie = NULL;
2687 bss->rsn_ie_len = 0;
2688 }
2689
30b89b0f
JB
2690 /*
2691 * Cf.
2692 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2693 *
2694 * quoting:
2695 *
2696 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2697 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2698 * Alliance (September 1, 2004) is incorporated by reference herein.
2699 * The inclusion of the WMM Parameters in probe responses and
2700 * association responses is mandatory for WMM enabled networks. The
2701 * inclusion of the WMM Parameters in beacons, however, is optional.
2702 */
2703
f0706e82
JB
2704 if (elems.wmm_param &&
2705 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2706 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2707 kfree(bss->wmm_ie);
2708 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2709 if (bss->wmm_ie) {
2710 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2711 elems.wmm_param_len + 2);
2712 bss->wmm_ie_len = elems.wmm_param_len + 2;
2713 } else
2714 bss->wmm_ie_len = 0;
a46f025d
AK
2715 } else if (elems.wmm_info &&
2716 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_info_len ||
2717 memcmp(bss->wmm_ie, elems.wmm_info, elems.wmm_info_len))) {
2718 /* As for certain AP's Fifth bit is not set in WMM IE in
2719 * beacon frames.So while parsing the beacon frame the
2720 * wmm_info structure is used instead of wmm_param.
2721 * wmm_info structure was never used to set bss->wmm_ie.
2722 * This code fixes this problem by copying the WME
2723 * information from wmm_info to bss->wmm_ie and enabling
2724 * n-band association.
2725 */
2726 kfree(bss->wmm_ie);
2727 bss->wmm_ie = kmalloc(elems.wmm_info_len + 2, GFP_ATOMIC);
2728 if (bss->wmm_ie) {
2729 memcpy(bss->wmm_ie, elems.wmm_info - 2,
2730 elems.wmm_info_len + 2);
2731 bss->wmm_ie_len = elems.wmm_info_len + 2;
2732 } else
2733 bss->wmm_ie_len = 0;
2734 } else if (!elems.wmm_param && !elems.wmm_info && bss->wmm_ie) {
f0706e82
JB
2735 kfree(bss->wmm_ie);
2736 bss->wmm_ie = NULL;
2737 bss->wmm_ie_len = 0;
2738 }
9d9bf77d
BR
2739
2740 /* check if we need to merge IBSS */
2741 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2742 !local->sta_sw_scanning && !local->sta_hw_scanning &&
fba4a1e6 2743 bss->capability & WLAN_CAPABILITY_IBSS &&
9d9bf77d
BR
2744 bss->freq == local->oper_channel->center_freq &&
2745 elems.ssid_len == sdata->u.sta.ssid_len &&
2746 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2747 if (rx_status->flag & RX_FLAG_TSFT) {
2748 /* in order for correct IBSS merging we need mactime
2749 *
2750 * since mactime is defined as the time the first data
2751 * symbol of the frame hits the PHY, and the timestamp
2752 * of the beacon is defined as "the time that the data
2753 * symbol containing the first bit of the timestamp is
2754 * transmitted to the PHY plus the transmitting STA’s
2755 * delays through its local PHY from the MAC-PHY
2756 * interface to its interface with the WM"
2757 * (802.11 11.1.2) - equals the time this bit arrives at
2758 * the receiver - we have to take into account the
2759 * offset between the two.
2760 * e.g: at 1 MBit that means mactime is 192 usec earlier
2761 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2762 */
2763 int rate = local->hw.wiphy->bands[rx_status->band]->
2764 bitrates[rx_status->rate_idx].bitrate;
2765 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2766 } else if (local && local->ops && local->ops->get_tsf)
2767 /* second best option: get current TSF */
2768 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2769 else
2770 /* can't merge without knowing the TSF */
2771 rx_timestamp = -1LLU;
2772#ifdef CONFIG_MAC80211_IBSS_DEBUG
2773 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2774 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2775 print_mac(mac, mgmt->sa),
2776 print_mac(mac2, mgmt->bssid),
2777 (unsigned long long)rx_timestamp,
2778 (unsigned long long)beacon_timestamp,
2779 (unsigned long long)(rx_timestamp - beacon_timestamp),
2780 jiffies);
2781#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2782 if (beacon_timestamp > rx_timestamp) {
fba4a1e6 2783#ifndef CONFIG_MAC80211_IBSS_DEBUG
d97cf015 2784 if (net_ratelimit())
fba4a1e6 2785#endif
9d9bf77d
BR
2786 printk(KERN_DEBUG "%s: beacon TSF higher than "
2787 "local TSF - IBSS merge with BSSID %s\n",
2788 dev->name, print_mac(mac, mgmt->bssid));
2789 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2790 ieee80211_ibss_add_sta(dev, NULL,
2791 mgmt->bssid, mgmt->sa);
2792 }
2793 }
2794
f0706e82
JB
2795 ieee80211_rx_bss_put(dev, bss);
2796}
2797
2798
2799static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2800 struct ieee80211_mgmt *mgmt,
2801 size_t len,
2802 struct ieee80211_rx_status *rx_status)
2803{
2804 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2805}
2806
2807
2808static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2809 struct ieee80211_mgmt *mgmt,
2810 size_t len,
2811 struct ieee80211_rx_status *rx_status)
2812{
f0706e82
JB
2813 struct ieee80211_sub_if_data *sdata;
2814 struct ieee80211_if_sta *ifsta;
f0706e82
JB
2815 size_t baselen;
2816 struct ieee802_11_elems elems;
d3c990fb
RR
2817 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2818 struct ieee80211_conf *conf = &local->hw.conf;
471b3efd 2819 u32 changed = 0;
f0706e82
JB
2820
2821 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2822
2823 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
51fb61e7 2824 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82
JB
2825 return;
2826 ifsta = &sdata->u.sta;
2827
d6f2da5b 2828 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
f0706e82
JB
2829 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2830 return;
2831
2832 /* Process beacon from the current BSS */
2833 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2834 if (baselen > len)
2835 return;
2836
67a4cce4 2837 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
f0706e82 2838
d18ef29f
RC
2839 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2840 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2841 elems.wmm_param_len);
2842 }
2843
2844 /* Do not send changes to driver if we are scanning. This removes
2845 * requirement that driver's bss_info_changed function needs to be
2846 * atomic. */
2847 if (local->sta_sw_scanning || local->sta_hw_scanning)
2848 return;
2849
5628221c 2850 if (elems.erp_info && elems.erp_info_len >= 1)
471b3efd 2851 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
50c4afb9
JL
2852 else {
2853 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2854 changed |= ieee80211_handle_protect_preamb(sdata, false,
2855 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2856 }
f0706e82 2857
d3c990fb 2858 if (elems.ht_cap_elem && elems.ht_info_elem &&
38668c05 2859 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
d3c990fb
RR
2860 struct ieee80211_ht_bss_info bss_info;
2861
2862 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2863 (struct ieee80211_ht_addt_info *)
2864 elems.ht_info_elem, &bss_info);
38668c05
TW
2865 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2866 &bss_info);
d3c990fb
RR
2867 }
2868
471b3efd 2869 ieee80211_bss_info_change_notify(sdata, changed);
f0706e82
JB
2870}
2871
2872
2873static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2874 struct ieee80211_if_sta *ifsta,
2875 struct ieee80211_mgmt *mgmt,
2876 size_t len,
2877 struct ieee80211_rx_status *rx_status)
2878{
2879 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2880 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2881 int tx_last_beacon;
2882 struct sk_buff *skb;
2883 struct ieee80211_mgmt *resp;
2884 u8 *pos, *end;
0795af57
JP
2885 DECLARE_MAC_BUF(mac);
2886#ifdef CONFIG_MAC80211_IBSS_DEBUG
2887 DECLARE_MAC_BUF(mac2);
2888 DECLARE_MAC_BUF(mac3);
2889#endif
f0706e82 2890
51fb61e7 2891 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
f0706e82
JB
2892 ifsta->state != IEEE80211_IBSS_JOINED ||
2893 len < 24 + 2 || !ifsta->probe_resp)
2894 return;
2895
2896 if (local->ops->tx_last_beacon)
2897 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2898 else
2899 tx_last_beacon = 1;
2900
2901#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57
JP
2902 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2903 "%s (tx_last_beacon=%d)\n",
2904 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2905 print_mac(mac3, mgmt->bssid), tx_last_beacon);
f0706e82
JB
2906#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2907
2908 if (!tx_last_beacon)
2909 return;
2910
2911 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2912 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2913 return;
2914
2915 end = ((u8 *) mgmt) + len;
2916 pos = mgmt->u.probe_req.variable;
2917 if (pos[0] != WLAN_EID_SSID ||
2918 pos + 2 + pos[1] > end) {
2919 if (net_ratelimit()) {
2920 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
0795af57
JP
2921 "from %s\n",
2922 dev->name, print_mac(mac, mgmt->sa));
f0706e82
JB
2923 }
2924 return;
2925 }
2926 if (pos[1] != 0 &&
2927 (pos[1] != ifsta->ssid_len ||
2928 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2929 /* Ignore ProbeReq for foreign SSID */
2930 return;
2931 }
2932
2933 /* Reply with ProbeResp */
0ec0b7ac 2934 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
f0706e82
JB
2935 if (!skb)
2936 return;
2937
2938 resp = (struct ieee80211_mgmt *) skb->data;
2939 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2940#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57
JP
2941 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2942 dev->name, print_mac(mac, resp->da));
f0706e82
JB
2943#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2944 ieee80211_sta_tx(dev, skb, 0);
2945}
2946
4e20cb29
JB
2947static void ieee80211_rx_mgmt_action(struct net_device *dev,
2948 struct ieee80211_if_sta *ifsta,
2949 struct ieee80211_mgmt *mgmt,
f709fc69
LCC
2950 size_t len,
2951 struct ieee80211_rx_status *rx_status)
9f985b0e 2952{
f709fc69 2953 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
f709fc69 2954
9f985b0e
RR
2955 if (len < IEEE80211_MIN_ACTION_SIZE)
2956 return;
2957
2958 switch (mgmt->u.action.category) {
2959 case WLAN_CATEGORY_BACK:
2960 switch (mgmt->u.action.u.addba_req.action_code) {
2961 case WLAN_ACTION_ADDBA_REQ:
2962 if (len < (IEEE80211_MIN_ACTION_SIZE +
2963 sizeof(mgmt->u.action.u.addba_req)))
2964 break;
2965 ieee80211_sta_process_addba_request(dev, mgmt, len);
2966 break;
eadc8d9e
RR
2967 case WLAN_ACTION_ADDBA_RESP:
2968 if (len < (IEEE80211_MIN_ACTION_SIZE +
2969 sizeof(mgmt->u.action.u.addba_resp)))
2970 break;
2971 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2972 break;
688b88a4
RR
2973 case WLAN_ACTION_DELBA:
2974 if (len < (IEEE80211_MIN_ACTION_SIZE +
2975 sizeof(mgmt->u.action.u.delba)))
2976 break;
2977 ieee80211_sta_process_delba(dev, mgmt, len);
2978 break;
9f985b0e
RR
2979 default:
2980 if (net_ratelimit())
688b88a4 2981 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
9f985b0e
RR
2982 dev->name);
2983 break;
2984 }
2985 break;
f709fc69 2986 case PLINK_CATEGORY:
902acc78 2987 if (ieee80211_vif_is_mesh(&sdata->vif))
f709fc69
LCC
2988 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2989 break;
f709fc69 2990 case MESH_PATH_SEL_CATEGORY:
902acc78 2991 if (ieee80211_vif_is_mesh(&sdata->vif))
f709fc69
LCC
2992 mesh_rx_path_sel_frame(dev, mgmt, len);
2993 break;
9f985b0e 2994 default:
f709fc69
LCC
2995 if (net_ratelimit())
2996 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2997 "category=%d\n", dev->name, mgmt->u.action.category);
9f985b0e
RR
2998 break;
2999 }
3000}
f0706e82
JB
3001
3002void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3003 struct ieee80211_rx_status *rx_status)
3004{
3005 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3006 struct ieee80211_sub_if_data *sdata;
3007 struct ieee80211_if_sta *ifsta;
3008 struct ieee80211_mgmt *mgmt;
3009 u16 fc;
3010
3011 if (skb->len < 24)
3012 goto fail;
3013
3014 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3015 ifsta = &sdata->u.sta;
3016
3017 mgmt = (struct ieee80211_mgmt *) skb->data;
3018 fc = le16_to_cpu(mgmt->frame_control);
3019
3020 switch (fc & IEEE80211_FCTL_STYPE) {
3021 case IEEE80211_STYPE_PROBE_REQ:
3022 case IEEE80211_STYPE_PROBE_RESP:
3023 case IEEE80211_STYPE_BEACON:
f709fc69 3024 case IEEE80211_STYPE_ACTION:
f0706e82
JB
3025 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3026 case IEEE80211_STYPE_AUTH:
3027 case IEEE80211_STYPE_ASSOC_RESP:
3028 case IEEE80211_STYPE_REASSOC_RESP:
3029 case IEEE80211_STYPE_DEAUTH:
3030 case IEEE80211_STYPE_DISASSOC:
3031 skb_queue_tail(&ifsta->skb_queue, skb);
3032 queue_work(local->hw.workqueue, &ifsta->work);
3033 return;
3034 default:
3035 printk(KERN_DEBUG "%s: received unknown management frame - "
3036 "stype=%d\n", dev->name,
3037 (fc & IEEE80211_FCTL_STYPE) >> 4);
3038 break;
3039 }
3040
3041 fail:
3042 kfree_skb(skb);
3043}
3044
3045
3046static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3047 struct sk_buff *skb)
3048{
3049 struct ieee80211_rx_status *rx_status;
3050 struct ieee80211_sub_if_data *sdata;
3051 struct ieee80211_if_sta *ifsta;
3052 struct ieee80211_mgmt *mgmt;
3053 u16 fc;
3054
3055 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3056 ifsta = &sdata->u.sta;
3057
3058 rx_status = (struct ieee80211_rx_status *) skb->cb;
3059 mgmt = (struct ieee80211_mgmt *) skb->data;
3060 fc = le16_to_cpu(mgmt->frame_control);
3061
3062 switch (fc & IEEE80211_FCTL_STYPE) {
3063 case IEEE80211_STYPE_PROBE_REQ:
3064 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3065 rx_status);
3066 break;
3067 case IEEE80211_STYPE_PROBE_RESP:
3068 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3069 break;
3070 case IEEE80211_STYPE_BEACON:
3071 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3072 break;
3073 case IEEE80211_STYPE_AUTH:
3074 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3075 break;
3076 case IEEE80211_STYPE_ASSOC_RESP:
471b3efd 3077 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
f0706e82
JB
3078 break;
3079 case IEEE80211_STYPE_REASSOC_RESP:
471b3efd 3080 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
f0706e82
JB
3081 break;
3082 case IEEE80211_STYPE_DEAUTH:
3083 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3084 break;
3085 case IEEE80211_STYPE_DISASSOC:
3086 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3087 break;
9f985b0e 3088 case IEEE80211_STYPE_ACTION:
f709fc69 3089 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
9f985b0e 3090 break;
f0706e82
JB
3091 }
3092
3093 kfree_skb(skb);
3094}
3095
3096
9ae54c84 3097ieee80211_rx_result
ece8eddd
ZY
3098ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3099 struct ieee80211_rx_status *rx_status)
f0706e82
JB
3100{
3101 struct ieee80211_mgmt *mgmt;
3102 u16 fc;
3103
ece8eddd 3104 if (skb->len < 2)
e4c26add 3105 return RX_DROP_UNUSABLE;
f0706e82
JB
3106
3107 mgmt = (struct ieee80211_mgmt *) skb->data;
3108 fc = le16_to_cpu(mgmt->frame_control);
3109
ece8eddd 3110 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
9ae54c84 3111 return RX_CONTINUE;
ece8eddd
ZY
3112
3113 if (skb->len < 24)
e4c26add 3114 return RX_DROP_MONITOR;
ece8eddd 3115
f0706e82
JB
3116 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3117 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3118 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3119 skb->len, rx_status);
ece8eddd 3120 dev_kfree_skb(skb);
9ae54c84 3121 return RX_QUEUED;
f0706e82
JB
3122 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3123 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3124 rx_status);
ece8eddd 3125 dev_kfree_skb(skb);
9ae54c84 3126 return RX_QUEUED;
f0706e82
JB
3127 }
3128 }
9ae54c84 3129 return RX_CONTINUE;
f0706e82
JB
3130}
3131
3132
3133static int ieee80211_sta_active_ibss(struct net_device *dev)
3134{
3135 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3136 int active = 0;
3137 struct sta_info *sta;
d0709a65 3138 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
f0706e82 3139
d0709a65
JB
3140 rcu_read_lock();
3141
3142 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3143 if (sta->sdata == sdata &&
f0706e82
JB
3144 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3145 jiffies)) {
3146 active++;
3147 break;
3148 }
3149 }
d0709a65
JB
3150
3151 rcu_read_unlock();
f0706e82
JB
3152
3153 return active;
3154}
3155
3156
f709fc69 3157static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
f0706e82
JB
3158{
3159 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3160 struct sta_info *sta, *tmp;
be8755e1 3161 LIST_HEAD(tmp_list);
0795af57 3162 DECLARE_MAC_BUF(mac);
d0709a65 3163 unsigned long flags;
f0706e82 3164
d0709a65 3165 spin_lock_irqsave(&local->sta_lock, flags);
f0706e82 3166 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
f709fc69 3167 if (time_after(jiffies, sta->last_rx + exp_time)) {
0795af57
JP
3168 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3169 dev->name, print_mac(mac, sta->addr));
cb585bcc 3170 __sta_info_unlink(&sta);
d0709a65
JB
3171 if (sta)
3172 list_add(&sta->list, &tmp_list);
f0706e82 3173 }
d0709a65 3174 spin_unlock_irqrestore(&local->sta_lock, flags);
be8755e1 3175
d0709a65
JB
3176 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3177 sta_info_destroy(sta);
f0706e82
JB
3178}
3179
3180
3181static void ieee80211_sta_merge_ibss(struct net_device *dev,
3182 struct ieee80211_if_sta *ifsta)
3183{
3184 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3185
f709fc69 3186 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
f0706e82
JB
3187 if (ieee80211_sta_active_ibss(dev))
3188 return;
3189
3190 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3191 "IBSS networks with same SSID (merge)\n", dev->name);
3192 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3193}
3194
3195
f709fc69
LCC
3196#ifdef CONFIG_MAC80211_MESH
3197static void ieee80211_mesh_housekeeping(struct net_device *dev,
3198 struct ieee80211_if_sta *ifsta)
3199{
3200 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3201 bool free_plinks;
3202
3203 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3204 mesh_path_expire(dev);
3205
3206 free_plinks = mesh_plink_availables(sdata);
3207 if (free_plinks != sdata->u.sta.accepting_plinks)
3208 ieee80211_if_config_beacon(dev);
3209
3210 mod_timer(&ifsta->timer, jiffies +
3211 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3212}
3213
3214
3215void ieee80211_start_mesh(struct net_device *dev)
3216{
3217 struct ieee80211_if_sta *ifsta;
3218 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3219 ifsta = &sdata->u.sta;
3220 ifsta->state = IEEE80211_MESH_UP;
3221 ieee80211_sta_timer((unsigned long)sdata);
3222}
3223#endif
3224
3225
f0706e82
JB
3226void ieee80211_sta_timer(unsigned long data)
3227{
3228 struct ieee80211_sub_if_data *sdata =
3229 (struct ieee80211_sub_if_data *) data;
3230 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3231 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3232
3233 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3234 queue_work(local->hw.workqueue, &ifsta->work);
3235}
3236
f0706e82
JB
3237void ieee80211_sta_work(struct work_struct *work)
3238{
3239 struct ieee80211_sub_if_data *sdata =
3240 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3241 struct net_device *dev = sdata->dev;
3242 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3243 struct ieee80211_if_sta *ifsta;
3244 struct sk_buff *skb;
3245
3246 if (!netif_running(dev))
3247 return;
3248
ece8eddd 3249 if (local->sta_sw_scanning || local->sta_hw_scanning)
f0706e82
JB
3250 return;
3251
51fb61e7 3252 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
f709fc69
LCC
3253 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3254 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
f0706e82 3255 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
51fb61e7 3256 "(type=%d)\n", dev->name, sdata->vif.type);
f0706e82
JB
3257 return;
3258 }
3259 ifsta = &sdata->u.sta;
3260
3261 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3262 ieee80211_sta_rx_queued_mgmt(dev, skb);
3263
f709fc69 3264#ifdef CONFIG_MAC80211_MESH
902acc78
JB
3265 if (ifsta->preq_queue_len &&
3266 time_after(jiffies,
3267 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
f709fc69
LCC
3268 mesh_path_start_discovery(dev);
3269#endif
3270
f0706e82
JB
3271 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3272 ifsta->state != IEEE80211_ASSOCIATE &&
3273 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
a0af5f14
HS
3274 if (ifsta->scan_ssid_len)
3275 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3276 else
3277 ieee80211_sta_start_scan(dev, NULL, 0);
f0706e82
JB
3278 return;
3279 }
3280
3281 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3282 if (ieee80211_sta_config_auth(dev, ifsta))
3283 return;
3284 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3285 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3286 return;
3287
3288 switch (ifsta->state) {
3289 case IEEE80211_DISABLED:
3290 break;
3291 case IEEE80211_AUTHENTICATE:
3292 ieee80211_authenticate(dev, ifsta);
3293 break;
3294 case IEEE80211_ASSOCIATE:
3295 ieee80211_associate(dev, ifsta);
3296 break;
3297 case IEEE80211_ASSOCIATED:
3298 ieee80211_associated(dev, ifsta);
3299 break;
3300 case IEEE80211_IBSS_SEARCH:
3301 ieee80211_sta_find_ibss(dev, ifsta);
3302 break;
3303 case IEEE80211_IBSS_JOINED:
3304 ieee80211_sta_merge_ibss(dev, ifsta);
3305 break;
f709fc69
LCC
3306#ifdef CONFIG_MAC80211_MESH
3307 case IEEE80211_MESH_UP:
3308 ieee80211_mesh_housekeeping(dev, ifsta);
3309 break;
3310#endif
f0706e82
JB
3311 default:
3312 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3313 ifsta->state);
3314 break;
3315 }
3316
3317 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3318 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3319 "mixed-cell disabled - disassociate\n", dev->name);
3320
3321 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3322 ieee80211_set_disassoc(dev, ifsta, 0);
3323 }
3324}
3325
3326
3327static void ieee80211_sta_reset_auth(struct net_device *dev,
3328 struct ieee80211_if_sta *ifsta)
3329{
3330 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3331
3332 if (local->ops->reset_tsf) {
3333 /* Reset own TSF to allow time synchronization work. */
3334 local->ops->reset_tsf(local_to_hw(local));
3335 }
3336
3337 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3338
3339
3340 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3341 ifsta->auth_alg = WLAN_AUTH_OPEN;
3342 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3343 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3344 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3345 ifsta->auth_alg = WLAN_AUTH_LEAP;
3346 else
3347 ifsta->auth_alg = WLAN_AUTH_OPEN;
3348 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3349 ifsta->auth_alg);
3350 ifsta->auth_transaction = -1;
d6f2da5b
JS
3351 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3352 ifsta->auth_tries = ifsta->assoc_tries = 0;
f0706e82
JB
3353 netif_carrier_off(dev);
3354}
3355
3356
3357void ieee80211_sta_req_auth(struct net_device *dev,
3358 struct ieee80211_if_sta *ifsta)
3359{
3360 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3361 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3362
51fb61e7 3363 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82
JB
3364 return;
3365
d6f2da5b
JS
3366 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3367 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3368 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3369 IEEE80211_STA_AUTO_SSID_SEL))) {
f0706e82
JB
3370 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3371 queue_work(local->hw.workqueue, &ifsta->work);
3372 }
3373}
3374
3375static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3376 const char *ssid, int ssid_len)
3377{
3378 int tmp, hidden_ssid;
3379
48225709
MW
3380 if (ssid_len == ifsta->ssid_len &&
3381 !memcmp(ifsta->ssid, ssid, ssid_len))
f0706e82
JB
3382 return 1;
3383
d6f2da5b 3384 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
f0706e82
JB
3385 return 0;
3386
3387 hidden_ssid = 1;
3388 tmp = ssid_len;
3389 while (tmp--) {
3390 if (ssid[tmp] != '\0') {
3391 hidden_ssid = 0;
3392 break;
3393 }
3394 }
3395
3396 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3397 return 1;
3398
3399 if (ssid_len == 1 && ssid[0] == ' ')
3400 return 1;
3401
3402 return 0;
3403}
3404
3405static int ieee80211_sta_config_auth(struct net_device *dev,
3406 struct ieee80211_if_sta *ifsta)
3407{
3408 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3409 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3410 struct ieee80211_sta_bss *bss, *selected = NULL;
3411 int top_rssi = 0, freq;
3412
d6f2da5b
JS
3413 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3414 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
f0706e82 3415 ifsta->state = IEEE80211_AUTHENTICATE;
f0706e82
JB
3416 ieee80211_sta_reset_auth(dev, ifsta);
3417 return 0;
3418 }
3419
3420 spin_lock_bh(&local->sta_bss_lock);
8318d78a 3421 freq = local->oper_channel->center_freq;
f0706e82
JB
3422 list_for_each_entry(bss, &local->sta_bss_list, list) {
3423 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3424 continue;
3425
3426 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3427 !!sdata->default_key)
3428 continue;
3429
d6f2da5b
JS
3430 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3431 bss->freq != freq)
f0706e82
JB
3432 continue;
3433
d6f2da5b 3434 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
f0706e82
JB
3435 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3436 continue;
3437
d6f2da5b 3438 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
f0706e82
JB
3439 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3440 continue;
3441
3442 if (!selected || top_rssi < bss->rssi) {
3443 selected = bss;
3444 top_rssi = bss->rssi;
3445 }
3446 }
3447 if (selected)
3448 atomic_inc(&selected->users);
3449 spin_unlock_bh(&local->sta_bss_lock);
3450
3451 if (selected) {
8318d78a 3452 ieee80211_set_freq(local, selected->freq);
d6f2da5b 3453 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
f0706e82
JB
3454 ieee80211_sta_set_ssid(dev, selected->ssid,
3455 selected->ssid_len);
3456 ieee80211_sta_set_bssid(dev, selected->bssid);
e2839d8f 3457 ieee80211_sta_def_wmm_params(dev, selected, 0);
f0706e82
JB
3458 ieee80211_rx_bss_put(dev, selected);
3459 ifsta->state = IEEE80211_AUTHENTICATE;
f0706e82
JB
3460 ieee80211_sta_reset_auth(dev, ifsta);
3461 return 0;
3462 } else {
3463 if (ifsta->state != IEEE80211_AUTHENTICATE) {
d6f2da5b 3464 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
b9bf1e60
JL
3465 ieee80211_sta_start_scan(dev, NULL, 0);
3466 else
3467 ieee80211_sta_start_scan(dev, ifsta->ssid,
3468 ifsta->ssid_len);
f0706e82
JB
3469 ifsta->state = IEEE80211_AUTHENTICATE;
3470 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3471 } else
3472 ifsta->state = IEEE80211_DISABLED;
3473 }
f0706e82
JB
3474 return -1;
3475}
3476
f0706e82
JB
3477
3478static int ieee80211_sta_create_ibss(struct net_device *dev,
3479 struct ieee80211_if_sta *ifsta)
3480{
3481 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3482 struct ieee80211_sta_bss *bss;
cffdd30d 3483 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
8318d78a 3484 struct ieee80211_supported_band *sband;
f0706e82
JB
3485 u8 bssid[ETH_ALEN], *pos;
3486 int i;
0795af57 3487 DECLARE_MAC_BUF(mac);
f0706e82
JB
3488
3489#if 0
3490 /* Easier testing, use fixed BSSID. */
3491 memset(bssid, 0xfe, ETH_ALEN);
3492#else
3493 /* Generate random, not broadcast, locally administered BSSID. Mix in
3494 * own MAC address to make sure that devices that do not have proper
3495 * random number generator get different BSSID. */
3496 get_random_bytes(bssid, ETH_ALEN);
3497 for (i = 0; i < ETH_ALEN; i++)
3498 bssid[i] ^= dev->dev_addr[i];
3499 bssid[0] &= ~0x01;
3500 bssid[0] |= 0x02;
3501#endif
3502
0795af57
JP
3503 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3504 dev->name, print_mac(mac, bssid));
f0706e82 3505
8318d78a
JB
3506 bss = ieee80211_rx_bss_add(dev, bssid,
3507 local->hw.conf.channel->center_freq,
cffdd30d 3508 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
f0706e82
JB
3509 if (!bss)
3510 return -ENOMEM;
3511
8318d78a
JB
3512 bss->band = local->hw.conf.channel->band;
3513 sband = local->hw.wiphy->bands[bss->band];
f0706e82
JB
3514
3515 if (local->hw.conf.beacon_int == 0)
f709fc69 3516 local->hw.conf.beacon_int = 10000;
f0706e82 3517 bss->beacon_int = local->hw.conf.beacon_int;
f0706e82
JB
3518 bss->last_update = jiffies;
3519 bss->capability = WLAN_CAPABILITY_IBSS;
3520 if (sdata->default_key) {
3521 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3522 } else
3523 sdata->drop_unencrypted = 0;
8318d78a 3524 bss->supp_rates_len = sband->n_bitrates;
f0706e82 3525 pos = bss->supp_rates;
8318d78a
JB
3526 for (i = 0; i < sband->n_bitrates; i++) {
3527 int rate = sband->bitrates[i].bitrate;
f0706e82
JB
3528 *pos++ = (u8) (rate / 5);
3529 }
3530
3531 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3532}
3533
3534
3535static int ieee80211_sta_find_ibss(struct net_device *dev,
3536 struct ieee80211_if_sta *ifsta)
3537{
3538 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3539 struct ieee80211_sta_bss *bss;
3540 int found = 0;
3541 u8 bssid[ETH_ALEN];
3542 int active_ibss;
0795af57
JP
3543 DECLARE_MAC_BUF(mac);
3544 DECLARE_MAC_BUF(mac2);
f0706e82
JB
3545
3546 if (ifsta->ssid_len == 0)
3547 return -EINVAL;
3548
3549 active_ibss = ieee80211_sta_active_ibss(dev);
3550#ifdef CONFIG_MAC80211_IBSS_DEBUG
3551 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3552 dev->name, active_ibss);
3553#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3554 spin_lock_bh(&local->sta_bss_lock);
3555 list_for_each_entry(bss, &local->sta_bss_list, list) {
3556 if (ifsta->ssid_len != bss->ssid_len ||
3557 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3558 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3559 continue;
3560#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57
JP
3561 printk(KERN_DEBUG " bssid=%s found\n",
3562 print_mac(mac, bss->bssid));
f0706e82
JB
3563#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3564 memcpy(bssid, bss->bssid, ETH_ALEN);
3565 found = 1;
3566 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3567 break;
3568 }
3569 spin_unlock_bh(&local->sta_bss_lock);
3570
3571#ifdef CONFIG_MAC80211_IBSS_DEBUG
0795af57
JP
3572 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3573 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
f0706e82
JB
3574#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3575 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
8318d78a
JB
3576 (bss = ieee80211_rx_bss_get(dev, bssid,
3577 local->hw.conf.channel->center_freq,
cffdd30d 3578 ifsta->ssid, ifsta->ssid_len))) {
0795af57 3579 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
f0706e82 3580 " based on configured SSID\n",
0795af57 3581 dev->name, print_mac(mac, bssid));
f0706e82
JB
3582 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3583 }
3584#ifdef CONFIG_MAC80211_IBSS_DEBUG
3585 printk(KERN_DEBUG " did not try to join ibss\n");
3586#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3587
3588 /* Selected IBSS not found in current scan results - try to scan */
3589 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3590 !ieee80211_sta_active_ibss(dev)) {
3591 mod_timer(&ifsta->timer, jiffies +
3592 IEEE80211_IBSS_MERGE_INTERVAL);
3593 } else if (time_after(jiffies, local->last_scan_completed +
3594 IEEE80211_SCAN_INTERVAL)) {
3595 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3596 "join\n", dev->name);
3597 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3598 ifsta->ssid_len);
3599 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3600 int interval = IEEE80211_SCAN_INTERVAL;
3601
3602 if (time_after(jiffies, ifsta->ibss_join_req +
3603 IEEE80211_IBSS_JOIN_TIMEOUT)) {
d6f2da5b 3604 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
8318d78a
JB
3605 (!(local->oper_channel->flags &
3606 IEEE80211_CHAN_NO_IBSS)))
f0706e82 3607 return ieee80211_sta_create_ibss(dev, ifsta);
d6f2da5b 3608 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
8318d78a
JB
3609 printk(KERN_DEBUG "%s: IBSS not allowed on"
3610 " %d MHz\n", dev->name,
3611 local->hw.conf.channel->center_freq);
f0706e82
JB
3612 }
3613
3614 /* No IBSS found - decrease scan interval and continue
3615 * scanning. */
3616 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3617 }
3618
3619 ifsta->state = IEEE80211_IBSS_SEARCH;
3620 mod_timer(&ifsta->timer, jiffies + interval);
3621 return 0;
3622 }
3623
3624 return 0;
3625}
3626
3627
3628int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3629{
8318d78a 3630 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
f0706e82 3631 struct ieee80211_if_sta *ifsta;
f0706e82
JB
3632
3633 if (len > IEEE80211_MAX_SSID_LEN)
3634 return -EINVAL;
3635
f0706e82
JB
3636 ifsta = &sdata->u.sta;
3637
3638 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
d6f2da5b 3639 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
f0706e82
JB
3640 memcpy(ifsta->ssid, ssid, len);
3641 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3642 ifsta->ssid_len = len;
3643
d6f2da5b
JS
3644 if (len)
3645 ifsta->flags |= IEEE80211_STA_SSID_SET;
3646 else
3647 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
51fb61e7 3648 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
d6f2da5b 3649 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
f0706e82
JB
3650 ifsta->ibss_join_req = jiffies;
3651 ifsta->state = IEEE80211_IBSS_SEARCH;
3652 return ieee80211_sta_find_ibss(dev, ifsta);
3653 }
3654 return 0;
3655}
3656
3657
3658int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3659{
3660 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3661 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3662 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3663 *len = ifsta->ssid_len;
3664 return 0;
3665}
3666
3667
3668int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3669{
3670 struct ieee80211_sub_if_data *sdata;
3671 struct ieee80211_if_sta *ifsta;
3672 int res;
3673
3674 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3675 ifsta = &sdata->u.sta;
3676
3677 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3678 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3679 res = ieee80211_if_config(dev);
3680 if (res) {
3681 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3682 "the low-level driver\n", dev->name);
3683 return res;
3684 }
3685 }
3686
d6f2da5b
JS
3687 if (is_valid_ether_addr(bssid))
3688 ifsta->flags |= IEEE80211_STA_BSSID_SET;
f0706e82 3689 else
d6f2da5b
JS
3690 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3691
f0706e82
JB
3692 return 0;
3693}
3694
3695
3696static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3697 struct ieee80211_sub_if_data *sdata,
3698 int powersave)
3699{
3700 struct sk_buff *skb;
3701 struct ieee80211_hdr *nullfunc;
3702 u16 fc;
3703
3704 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3705 if (!skb) {
3706 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3707 "frame\n", sdata->dev->name);
3708 return;
3709 }
3710 skb_reserve(skb, local->hw.extra_tx_headroom);
3711
3712 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3713 memset(nullfunc, 0, 24);
3714 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3715 IEEE80211_FCTL_TODS;
3716 if (powersave)
3717 fc |= IEEE80211_FCTL_PM;
3718 nullfunc->frame_control = cpu_to_le16(fc);
3719 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3720 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3721 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3722
3723 ieee80211_sta_tx(sdata->dev, skb, 0);
3724}
3725
3726
69d3b6f4
JB
3727static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3728{
3729 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3730 ieee80211_vif_is_mesh(&sdata->vif))
3731 ieee80211_sta_timer((unsigned long)sdata);
3732}
3733
f0706e82
JB
3734void ieee80211_scan_completed(struct ieee80211_hw *hw)
3735{
3736 struct ieee80211_local *local = hw_to_local(hw);
3737 struct net_device *dev = local->scan_dev;
3738 struct ieee80211_sub_if_data *sdata;
3739 union iwreq_data wrqu;
3740
3741 local->last_scan_completed = jiffies;
ece8eddd
ZY
3742 memset(&wrqu, 0, sizeof(wrqu));
3743 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
f0706e82 3744
ece8eddd
ZY
3745 if (local->sta_hw_scanning) {
3746 local->sta_hw_scanning = 0;
675ef586
MA
3747 if (ieee80211_hw_config(local))
3748 printk(KERN_DEBUG "%s: failed to restore operational "
3749 "channel after scan\n", dev->name);
69d3b6f4
JB
3750 /* Restart STA timer for HW scan case */
3751 rcu_read_lock();
3752 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3753 ieee80211_restart_sta_timer(sdata);
3754 rcu_read_unlock();
3755
ece8eddd
ZY
3756 goto done;
3757 }
3758
3759 local->sta_sw_scanning = 0;
f0706e82 3760 if (ieee80211_hw_config(local))
4b50e388 3761 printk(KERN_DEBUG "%s: failed to restore operational "
f0706e82
JB
3762 "channel after scan\n", dev->name);
3763
4150c572
JB
3764
3765 netif_tx_lock_bh(local->mdev);
3766 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3767 local->ops->configure_filter(local_to_hw(local),
3768 FIF_BCN_PRBRESP_PROMISC,
3769 &local->filter_flags,
3770 local->mdev->mc_count,
3771 local->mdev->mc_list);
3772
3773 netif_tx_unlock_bh(local->mdev);
f0706e82 3774
79010420
JB
3775 rcu_read_lock();
3776 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
14042cbe
MN
3777
3778 /* No need to wake the master device. */
3779 if (sdata->dev == local->mdev)
3780 continue;
3781
69d3b6f4
JB
3782 /* Tell AP we're back */
3783 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3784 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3785 ieee80211_send_nullfunc(local, sdata, 0);
14042cbe 3786
69d3b6f4 3787 ieee80211_restart_sta_timer(sdata);
f709fc69 3788
f0706e82
JB
3789 netif_wake_queue(sdata->dev);
3790 }
79010420 3791 rcu_read_unlock();
f0706e82 3792
ece8eddd 3793done:
f0706e82 3794 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
51fb61e7 3795 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
f0706e82 3796 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
d6f2da5b 3797 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
f0706e82
JB
3798 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3799 !ieee80211_sta_active_ibss(dev)))
3800 ieee80211_sta_find_ibss(dev, ifsta);
3801 }
3802}
3803EXPORT_SYMBOL(ieee80211_scan_completed);
3804
3805void ieee80211_sta_scan_work(struct work_struct *work)
3806{
3807 struct ieee80211_local *local =
3808 container_of(work, struct ieee80211_local, scan_work.work);
3809 struct net_device *dev = local->scan_dev;
3810 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
8318d78a 3811 struct ieee80211_supported_band *sband;
f0706e82
JB
3812 struct ieee80211_channel *chan;
3813 int skip;
3814 unsigned long next_delay = 0;
3815
ece8eddd 3816 if (!local->sta_sw_scanning)
f0706e82
JB
3817 return;
3818
3819 switch (local->scan_state) {
3820 case SCAN_SET_CHANNEL:
69d464d5
JB
3821 /*
3822 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3823 * after we successfully scanned the last channel of the last
3824 * band (and the last band is supported by the hw)
3825 */
8318d78a
JB
3826 if (local->scan_band < IEEE80211_NUM_BANDS)
3827 sband = local->hw.wiphy->bands[local->scan_band];
3828 else
3829 sband = NULL;
3830
69d464d5
JB
3831 /*
3832 * If we are at an unsupported band and have more bands
3833 * left to scan, advance to the next supported one.
3834 */
3835 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
8318d78a
JB
3836 local->scan_band++;
3837 sband = local->hw.wiphy->bands[local->scan_band];
3838 local->scan_channel_idx = 0;
3839 }
3840
69d464d5
JB
3841 /* if no more bands/channels left, complete scan */
3842 if (!sband || local->scan_channel_idx >= sband->n_channels) {
f0706e82
JB
3843 ieee80211_scan_completed(local_to_hw(local));
3844 return;
3845 }
8318d78a
JB
3846 skip = 0;
3847 chan = &sband->channels[local->scan_channel_idx];
3848
3849 if (chan->flags & IEEE80211_CHAN_DISABLED ||
51fb61e7 3850 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
8318d78a 3851 chan->flags & IEEE80211_CHAN_NO_IBSS))
f0706e82
JB
3852 skip = 1;
3853
3854 if (!skip) {
f0706e82
JB
3855 local->scan_channel = chan;
3856 if (ieee80211_hw_config(local)) {
8318d78a
JB
3857 printk(KERN_DEBUG "%s: failed to set freq to "
3858 "%d MHz for scan\n", dev->name,
3859 chan->center_freq);
f0706e82
JB
3860 skip = 1;
3861 }
3862 }
3863
69d464d5 3864 /* advance state machine to next channel/band */
f0706e82 3865 local->scan_channel_idx++;
8318d78a 3866 if (local->scan_channel_idx >= sband->n_channels) {
69d464d5
JB
3867 /*
3868 * scan_band may end up == IEEE80211_NUM_BANDS, but
3869 * we'll catch that case above and complete the scan
3870 * if that is the case.
3871 */
8318d78a
JB
3872 local->scan_band++;
3873 local->scan_channel_idx = 0;
f0706e82
JB
3874 }
3875
3876 if (skip)
3877 break;
3878
3879 next_delay = IEEE80211_PROBE_DELAY +
3880 usecs_to_jiffies(local->hw.channel_change_time);
3881 local->scan_state = SCAN_SEND_PROBE;
3882 break;
3883 case SCAN_SEND_PROBE:
8318d78a 3884 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
f0706e82 3885 local->scan_state = SCAN_SET_CHANNEL;
8318d78a
JB
3886
3887 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3888 break;
3889 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3890 local->scan_ssid_len);
3891 next_delay = IEEE80211_CHANNEL_TIME;
f0706e82
JB
3892 break;
3893 }
3894
ece8eddd 3895 if (local->sta_sw_scanning)
f0706e82
JB
3896 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3897 next_delay);
3898}
3899
3900
3901static int ieee80211_sta_start_scan(struct net_device *dev,
3902 u8 *ssid, size_t ssid_len)
3903{
3904 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3905 struct ieee80211_sub_if_data *sdata;
3906
3907 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3908 return -EINVAL;
3909
3910 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3911 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3912 * BSSID: MACAddress
3913 * SSID
3914 * ScanType: ACTIVE, PASSIVE
3915 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3916 * a Probe frame during active scanning
3917 * ChannelList
3918 * MinChannelTime (>= ProbeDelay), in TU
3919 * MaxChannelTime: (>= MinChannelTime), in TU
3920 */
3921
3922 /* MLME-SCAN.confirm
3923 * BSSDescriptionSet
3924 * ResultCode: SUCCESS, INVALID_PARAMETERS
3925 */
3926
ece8eddd 3927 if (local->sta_sw_scanning || local->sta_hw_scanning) {
f0706e82
JB
3928 if (local->scan_dev == dev)
3929 return 0;
3930 return -EBUSY;
3931 }
3932
3933 if (local->ops->hw_scan) {
3934 int rc = local->ops->hw_scan(local_to_hw(local),
ece8eddd 3935 ssid, ssid_len);
f0706e82 3936 if (!rc) {
ece8eddd 3937 local->sta_hw_scanning = 1;
f0706e82
JB
3938 local->scan_dev = dev;
3939 }
3940 return rc;
3941 }
3942
ece8eddd 3943 local->sta_sw_scanning = 1;
f0706e82 3944
79010420
JB
3945 rcu_read_lock();
3946 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
14042cbe
MN
3947
3948 /* Don't stop the master interface, otherwise we can't transmit
3949 * probes! */
3950 if (sdata->dev == local->mdev)
3951 continue;
3952
f0706e82 3953 netif_stop_queue(sdata->dev);
51fb61e7 3954 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
d6f2da5b 3955 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
f0706e82
JB
3956 ieee80211_send_nullfunc(local, sdata, 1);
3957 }
79010420 3958 rcu_read_unlock();
f0706e82
JB
3959
3960 if (ssid) {
3961 local->scan_ssid_len = ssid_len;
3962 memcpy(local->scan_ssid, ssid, ssid_len);
3963 } else
3964 local->scan_ssid_len = 0;
3965 local->scan_state = SCAN_SET_CHANNEL;
f0706e82 3966 local->scan_channel_idx = 0;
8318d78a 3967 local->scan_band = IEEE80211_BAND_2GHZ;
f0706e82
JB
3968 local->scan_dev = dev;
3969
4150c572
JB
3970 netif_tx_lock_bh(local->mdev);
3971 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3972 local->ops->configure_filter(local_to_hw(local),
3973 FIF_BCN_PRBRESP_PROMISC,
3974 &local->filter_flags,
3975 local->mdev->mc_count,
3976 local->mdev->mc_list);
3977 netif_tx_unlock_bh(local->mdev);
f0706e82
JB
3978
3979 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3980 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3981 IEEE80211_CHANNEL_TIME);
3982
3983 return 0;
3984}
3985
3986
3987int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3988{
3989 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3990 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3991 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3992
51fb61e7 3993 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82
JB
3994 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3995
ece8eddd 3996 if (local->sta_sw_scanning || local->sta_hw_scanning) {
f0706e82
JB
3997 if (local->scan_dev == dev)
3998 return 0;
3999 return -EBUSY;
4000 }
4001
a0af5f14
HS
4002 ifsta->scan_ssid_len = ssid_len;
4003 if (ssid_len)
4004 memcpy(ifsta->scan_ssid, ssid, ssid_len);
f0706e82
JB
4005 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4006 queue_work(local->hw.workqueue, &ifsta->work);
4007 return 0;
4008}
4009
4010static char *
4011ieee80211_sta_scan_result(struct net_device *dev,
4012 struct ieee80211_sta_bss *bss,
4013 char *current_ev, char *end_buf)
4014{
4015 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4016 struct iw_event iwe;
4017
4018 if (time_after(jiffies,
4019 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4020 return current_ev;
4021
f0706e82
JB
4022 memset(&iwe, 0, sizeof(iwe));
4023 iwe.cmd = SIOCGIWAP;
4024 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4025 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4026 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4027 IW_EV_ADDR_LEN);
4028
4029 memset(&iwe, 0, sizeof(iwe));
4030 iwe.cmd = SIOCGIWESSID;
902acc78
JB
4031 if (bss_mesh_cfg(bss)) {
4032 iwe.u.data.length = bss_mesh_id_len(bss);
f709fc69
LCC
4033 iwe.u.data.flags = 1;
4034 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
902acc78 4035 bss_mesh_id(bss));
f709fc69
LCC
4036 } else {
4037 iwe.u.data.length = bss->ssid_len;
4038 iwe.u.data.flags = 1;
4039 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4040 bss->ssid);
4041 }
f0706e82 4042
1d1b5359
LCC
4043 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4044 || bss_mesh_cfg(bss)) {
f0706e82
JB
4045 memset(&iwe, 0, sizeof(iwe));
4046 iwe.cmd = SIOCGIWMODE;
902acc78 4047 if (bss_mesh_cfg(bss))
f709fc69
LCC
4048 iwe.u.mode = IW_MODE_MESH;
4049 else if (bss->capability & WLAN_CAPABILITY_ESS)
f0706e82
JB
4050 iwe.u.mode = IW_MODE_MASTER;
4051 else
4052 iwe.u.mode = IW_MODE_ADHOC;
4053 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4054 IW_EV_UINT_LEN);
4055 }
4056
4057 memset(&iwe, 0, sizeof(iwe));
4058 iwe.cmd = SIOCGIWFREQ;
8318d78a
JB
4059 iwe.u.freq.m = bss->freq;
4060 iwe.u.freq.e = 6;
f0706e82
JB
4061 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4062 IW_EV_FREQ_LEN);
8318d78a
JB
4063
4064 memset(&iwe, 0, sizeof(iwe));
4065 iwe.cmd = SIOCGIWFREQ;
4066 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4067 iwe.u.freq.e = 0;
f0706e82
JB
4068 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4069 IW_EV_FREQ_LEN);
4070
4071 memset(&iwe, 0, sizeof(iwe));
4072 iwe.cmd = IWEVQUAL;
4073 iwe.u.qual.qual = bss->signal;
4074 iwe.u.qual.level = bss->rssi;
4075 iwe.u.qual.noise = bss->noise;
4076 iwe.u.qual.updated = local->wstats_flags;
4077 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4078 IW_EV_QUAL_LEN);
4079
4080 memset(&iwe, 0, sizeof(iwe));
4081 iwe.cmd = SIOCGIWENCODE;
4082 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4083 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4084 else
4085 iwe.u.data.flags = IW_ENCODE_DISABLED;
4086 iwe.u.data.length = 0;
4087 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4088
4089 if (bss && bss->wpa_ie) {
4090 memset(&iwe, 0, sizeof(iwe));
4091 iwe.cmd = IWEVGENIE;
4092 iwe.u.data.length = bss->wpa_ie_len;
4093 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4094 bss->wpa_ie);
4095 }
4096
4097 if (bss && bss->rsn_ie) {
4098 memset(&iwe, 0, sizeof(iwe));
4099 iwe.cmd = IWEVGENIE;
4100 iwe.u.data.length = bss->rsn_ie_len;
4101 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4102 bss->rsn_ie);
4103 }
4104
4105 if (bss && bss->supp_rates_len > 0) {
4106 /* display all supported rates in readable format */
4107 char *p = current_ev + IW_EV_LCP_LEN;
4108 int i;
4109
4110 memset(&iwe, 0, sizeof(iwe));
4111 iwe.cmd = SIOCGIWRATE;
4112 /* Those two flags are ignored... */
4113 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4114
4115 for (i = 0; i < bss->supp_rates_len; i++) {
4116 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4117 0x7f) * 500000);
4118 p = iwe_stream_add_value(current_ev, p,
4119 end_buf, &iwe, IW_EV_PARAM_LEN);
4120 }
4121 current_ev = p;
4122 }
4123
4124 if (bss) {
4125 char *buf;
4126 buf = kmalloc(30, GFP_ATOMIC);
4127 if (buf) {
4128 memset(&iwe, 0, sizeof(iwe));
4129 iwe.cmd = IWEVCUSTOM;
4130 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4131 iwe.u.data.length = strlen(buf);
4132 current_ev = iwe_stream_add_point(current_ev, end_buf,
4133 &iwe, buf);
4134 kfree(buf);
4135 }
4136 }
4137
902acc78 4138 if (bss_mesh_cfg(bss)) {
f709fc69 4139 char *buf;
24736701 4140 u8 *cfg = bss_mesh_cfg(bss);
4f5d4c4d 4141 buf = kmalloc(50, GFP_ATOMIC);
f709fc69
LCC
4142 if (buf) {
4143 memset(&iwe, 0, sizeof(iwe));
4144 iwe.cmd = IWEVCUSTOM;
24736701 4145 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4f5d4c4d
LCC
4146 iwe.u.data.length = strlen(buf);
4147 current_ev = iwe_stream_add_point(current_ev, end_buf,
4148 &iwe, buf);
4149 sprintf(buf, "Path Selection Protocol ID: "
24736701
JL
4150 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4151 cfg[4]);
4f5d4c4d
LCC
4152 iwe.u.data.length = strlen(buf);
4153 current_ev = iwe_stream_add_point(current_ev, end_buf,
4154 &iwe, buf);
4155 sprintf(buf, "Path Selection Metric ID: "
24736701
JL
4156 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4157 cfg[8]);
4f5d4c4d
LCC
4158 iwe.u.data.length = strlen(buf);
4159 current_ev = iwe_stream_add_point(current_ev, end_buf,
4160 &iwe, buf);
4161 sprintf(buf, "Congestion Control Mode ID: "
24736701
JL
4162 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4163 cfg[11], cfg[12]);
4f5d4c4d
LCC
4164 iwe.u.data.length = strlen(buf);
4165 current_ev = iwe_stream_add_point(current_ev, end_buf,
4166 &iwe, buf);
4167 sprintf(buf, "Channel Precedence: "
24736701
JL
4168 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4169 cfg[15], cfg[16]);
f709fc69
LCC
4170 iwe.u.data.length = strlen(buf);
4171 current_ev = iwe_stream_add_point(current_ev, end_buf,
4172 &iwe, buf);
4173 kfree(buf);
4174 }
4175 }
4176
f0706e82
JB
4177 return current_ev;
4178}
4179
4180
4181int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4182{
4183 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4184 char *current_ev = buf;
4185 char *end_buf = buf + len;
4186 struct ieee80211_sta_bss *bss;
4187
4188 spin_lock_bh(&local->sta_bss_lock);
4189 list_for_each_entry(bss, &local->sta_bss_list, list) {
4190 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4191 spin_unlock_bh(&local->sta_bss_lock);
4192 return -E2BIG;
4193 }
4194 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4195 end_buf);
4196 }
4197 spin_unlock_bh(&local->sta_bss_lock);
4198 return current_ev - buf;
4199}
4200
4201
4202int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4203{
4204 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4205 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4206 kfree(ifsta->extra_ie);
4207 if (len == 0) {
4208 ifsta->extra_ie = NULL;
4209 ifsta->extra_ie_len = 0;
4210 return 0;
4211 }
4212 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4213 if (!ifsta->extra_ie) {
4214 ifsta->extra_ie_len = 0;
4215 return -ENOMEM;
4216 }
4217 memcpy(ifsta->extra_ie, ie, len);
4218 ifsta->extra_ie_len = len;
4219 return 0;
4220}
4221
4222
4223struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4224 struct sk_buff *skb, u8 *bssid,
4225 u8 *addr)
4226{
4227 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4228 struct sta_info *sta;
91fa558b 4229 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
0795af57 4230 DECLARE_MAC_BUF(mac);
f0706e82
JB
4231
4232 /* TODO: Could consider removing the least recently used entry and
4233 * allow new one to be added. */
4234 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4235 if (net_ratelimit()) {
4236 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
0795af57 4237 "entry %s\n", dev->name, print_mac(mac, addr));
f0706e82
JB
4238 }
4239 return NULL;
4240 }
4241
0795af57 4242 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
dd1cd4c6 4243 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
f0706e82 4244
73651ee6
JB
4245 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4246 if (!sta)
f0706e82
JB
4247 return NULL;
4248
238814fd
JB
4249 sta->flags |= WLAN_STA_AUTHORIZED;
4250
8318d78a
JB
4251 sta->supp_rates[local->hw.conf.channel->band] =
4252 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
f0706e82
JB
4253
4254 rate_control_rate_init(sta, local);
4255
93e5deb1 4256 if (sta_info_insert(sta))
73651ee6 4257 return NULL;
73651ee6 4258
d0709a65 4259 return sta;
f0706e82
JB
4260}
4261
4262
4263int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4264{
4265 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4266 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4267
4268 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4269 dev->name, reason);
4270
51fb61e7
JB
4271 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4272 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
f0706e82
JB
4273 return -EINVAL;
4274
4275 ieee80211_send_deauth(dev, ifsta, reason);
4276 ieee80211_set_disassoc(dev, ifsta, 1);
4277 return 0;
4278}
4279
4280
4281int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4282{
4283 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4284 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4285
4286 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4287 dev->name, reason);
4288
51fb61e7 4289 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
f0706e82
JB
4290 return -EINVAL;
4291
d6f2da5b 4292 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
f0706e82
JB
4293 return -1;
4294
4295 ieee80211_send_disassoc(dev, ifsta, reason);
4296 ieee80211_set_disassoc(dev, ifsta, 0);
4297 return 0;
4298}
84363e6e
MA
4299
4300void ieee80211_notify_mac(struct ieee80211_hw *hw,
4301 enum ieee80211_notification_types notif_type)
4302{
4303 struct ieee80211_local *local = hw_to_local(hw);
4304 struct ieee80211_sub_if_data *sdata;
4305
4306 switch (notif_type) {
4307 case IEEE80211_NOTIFY_RE_ASSOC:
4308 rcu_read_lock();
4309 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4310
4311 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4312 ieee80211_sta_req_auth(sdata->dev,
4313 &sdata->u.sta);
4314 }
4315
4316 }
4317 rcu_read_unlock();
4318 break;
4319 }
4320}
4321EXPORT_SYMBOL(ieee80211_notify_mac);