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