blob: 67c38235a3c51cc779616021da8188441db1c49e [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#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>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070029#include <net/mac80211.h>
Johannes Berg60f8b39c2008-09-08 17:44:22 +020030
Jiri Bencf0706e82007-05-05 11:45:53 -070031#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040032#include "rate.h"
33#include "led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010034#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070035
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +030036#define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
Jiri Bencf0706e82007-05-05 11:45:53 -070037#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)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#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)
Dan Williams872ba532008-06-04 13:59:34 -040047#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070048
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)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010055#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
Johannes Berg9c80d3d2008-09-08 15:41:59 +020060/* mgmt header + 1 byte category code */
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020061#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
62
63#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
64#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
65#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020066#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
67#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020068
Ron Rindjunsky07db2182007-12-25 17:00:33 +020069/* next values represent the buffer size for A-MPDU frame.
70 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
71#define IEEE80211_MIN_AMPDU_BUF 0x8
72#define IEEE80211_MAX_AMPDU_BUF 0x40
73
Johannes Berg60f8b39c2008-09-08 17:44:22 +020074/* BSS handling */
Jiri Bencf0706e82007-05-05 11:45:53 -070075static struct ieee80211_sta_bss *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120076ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
Johannes Berg60f8b39c2008-09-08 17:44:22 +020077 u8 *ssid, u8 ssid_len)
78{
79 struct ieee80211_sta_bss *bss;
80
81 spin_lock_bh(&local->sta_bss_lock);
82 bss = local->sta_bss_hash[STA_HASH(bssid)];
83 while (bss) {
84 if (!bss_mesh_cfg(bss) &&
85 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
86 bss->freq == freq &&
87 bss->ssid_len == ssid_len &&
88 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
89 atomic_inc(&bss->users);
90 break;
91 }
92 bss = bss->hnext;
93 }
94 spin_unlock_bh(&local->sta_bss_lock);
95 return bss;
96}
97
98/* Caller must hold local->sta_bss_lock */
99static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
100 struct ieee80211_sta_bss *bss)
101{
102 u8 hash_idx;
103
104 if (bss_mesh_cfg(bss))
105 hash_idx = mesh_id_hash(bss_mesh_id(bss),
106 bss_mesh_id_len(bss));
107 else
108 hash_idx = STA_HASH(bss->bssid);
109
110 bss->hnext = local->sta_bss_hash[hash_idx];
111 local->sta_bss_hash[hash_idx] = bss;
112}
113
114/* Caller must hold local->sta_bss_lock */
115static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
116 struct ieee80211_sta_bss *bss)
117{
118 struct ieee80211_sta_bss *b, *prev = NULL;
119 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
120 while (b) {
121 if (b == bss) {
122 if (!prev)
123 local->sta_bss_hash[STA_HASH(bss->bssid)] =
124 bss->hnext;
125 else
126 prev->hnext = bss->hnext;
127 break;
128 }
129 prev = b;
130 b = b->hnext;
131 }
132}
133
134static struct ieee80211_sta_bss *
135ieee80211_rx_bss_add(struct ieee80211_sub_if_data *sdata, u8 *bssid, int freq,
136 u8 *ssid, u8 ssid_len)
137{
138 struct ieee80211_local *local = sdata->local;
139 struct ieee80211_sta_bss *bss;
140
141 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
142 if (!bss)
143 return NULL;
144 atomic_inc(&bss->users);
145 atomic_inc(&bss->users);
146 memcpy(bss->bssid, bssid, ETH_ALEN);
147 bss->freq = freq;
148 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
149 memcpy(bss->ssid, ssid, ssid_len);
150 bss->ssid_len = ssid_len;
151 }
152
153 spin_lock_bh(&local->sta_bss_lock);
154 /* TODO: order by RSSI? */
155 list_add_tail(&bss->list, &local->sta_bss_list);
156 __ieee80211_rx_bss_hash_add(local, bss);
157 spin_unlock_bh(&local->sta_bss_lock);
158 return bss;
159}
160
161#ifdef CONFIG_MAC80211_MESH
162static struct ieee80211_sta_bss *
163ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
164 u8 *mesh_cfg, int freq)
165{
166 struct ieee80211_sta_bss *bss;
167
168 spin_lock_bh(&local->sta_bss_lock);
169 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
170 while (bss) {
171 if (bss_mesh_cfg(bss) &&
172 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
173 bss->freq == freq &&
174 mesh_id_len == bss->mesh_id_len &&
175 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
176 mesh_id_len))) {
177 atomic_inc(&bss->users);
178 break;
179 }
180 bss = bss->hnext;
181 }
182 spin_unlock_bh(&local->sta_bss_lock);
183 return bss;
184}
185
186static struct ieee80211_sta_bss *
187ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
188 u8 *mesh_cfg, int mesh_config_len, int freq)
189{
190 struct ieee80211_sta_bss *bss;
191
192 if (mesh_config_len != MESH_CFG_LEN)
193 return NULL;
194
195 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
196 if (!bss)
197 return NULL;
198
199 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
200 if (!bss->mesh_cfg) {
201 kfree(bss);
202 return NULL;
203 }
204
205 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
206 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
207 if (!bss->mesh_id) {
208 kfree(bss->mesh_cfg);
209 kfree(bss);
210 return NULL;
211 }
212 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
213 }
214
215 atomic_inc(&bss->users);
216 atomic_inc(&bss->users);
217 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
218 bss->mesh_id_len = mesh_id_len;
219 bss->freq = freq;
220 spin_lock_bh(&local->sta_bss_lock);
221 /* TODO: order by RSSI? */
222 list_add_tail(&bss->list, &local->sta_bss_list);
223 __ieee80211_rx_bss_hash_add(local, bss);
224 spin_unlock_bh(&local->sta_bss_lock);
225 return bss;
226}
227#endif
228
229static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
230{
231 kfree(bss->ies);
232 kfree(bss_mesh_id(bss));
233 kfree(bss_mesh_cfg(bss));
234 kfree(bss);
235}
236
Johannes Berg3e122be2008-07-09 14:40:34 +0200237static void ieee80211_rx_bss_put(struct ieee80211_local *local,
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200238 struct ieee80211_sta_bss *bss)
239{
240 local_bh_disable();
241 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
242 local_bh_enable();
243 return;
244 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700245
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200246 __ieee80211_rx_bss_hash_del(local, bss);
247 list_del(&bss->list);
248 spin_unlock_bh(&local->sta_bss_lock);
249 ieee80211_rx_bss_free(bss);
250}
Jiri Bencf0706e82007-05-05 11:45:53 -0700251
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200252void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
253{
254 spin_lock_init(&local->sta_bss_lock);
255 INIT_LIST_HEAD(&local->sta_bss_list);
256}
257
258void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
259{
260 struct ieee80211_sta_bss *bss, *tmp;
261
262 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
263 ieee80211_rx_bss_put(local, bss);
264}
265
266static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300267{
268 u8 *end, *pos;
269
270 pos = bss->ies;
271 if (pos == NULL)
272 return NULL;
273 end = pos + bss->ies_len;
274
275 while (pos + 1 < end) {
276 if (pos + 2 + pos[1] > end)
277 break;
278 if (pos[0] == ie)
279 return pos;
280 pos += 2 + pos[1];
281 }
282
283 return NULL;
284}
285
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200286/* utils */
Jiri Bencf0706e82007-05-05 11:45:53 -0700287static int ecw2cw(int ecw)
288{
Johannes Bergac2bf322008-02-14 00:30:35 +0100289 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700290}
291
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200292/* frame sending functions */
293void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
294 int encrypt)
295{
296 skb->dev = sdata->local->mdev;
297 skb_set_mac_header(skb, 0);
298 skb_set_network_header(skb, 0);
299 skb_set_transport_header(skb, 0);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100300
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200301 skb->iif = sdata->dev->ifindex;
302 skb->do_not_encrypt = !encrypt;
303
304 dev_queue_xmit(skb);
305}
306
307static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
308 struct ieee80211_if_sta *ifsta,
309 int transaction, u8 *extra, size_t extra_len,
310 int encrypt)
311{
312 struct ieee80211_local *local = sdata->local;
313 struct sk_buff *skb;
314 struct ieee80211_mgmt *mgmt;
315
316 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
317 sizeof(*mgmt) + 6 + extra_len);
318 if (!skb) {
319 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
320 "frame\n", sdata->dev->name);
321 return;
322 }
323 skb_reserve(skb, local->hw.extra_tx_headroom);
324
325 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
326 memset(mgmt, 0, 24 + 6);
327 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
328 IEEE80211_STYPE_AUTH);
329 if (encrypt)
330 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
331 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
332 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
333 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
334 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
335 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
336 ifsta->auth_transaction = transaction + 1;
337 mgmt->u.auth.status_code = cpu_to_le16(0);
338 if (extra)
339 memcpy(skb_put(skb, extra_len), extra, extra_len);
340
341 ieee80211_sta_tx(sdata, skb, encrypt);
342}
343
344static void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
345 u8 *ssid, size_t ssid_len)
346{
347 struct ieee80211_local *local = sdata->local;
348 struct ieee80211_supported_band *sband;
349 struct sk_buff *skb;
350 struct ieee80211_mgmt *mgmt;
351 u8 *pos, *supp_rates, *esupp_rates = NULL;
352 int i;
353
354 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
355 if (!skb) {
356 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
357 "request\n", sdata->dev->name);
358 return;
359 }
360 skb_reserve(skb, local->hw.extra_tx_headroom);
361
362 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
363 memset(mgmt, 0, 24);
364 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
365 IEEE80211_STYPE_PROBE_REQ);
366 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
367 if (dst) {
368 memcpy(mgmt->da, dst, ETH_ALEN);
369 memcpy(mgmt->bssid, dst, ETH_ALEN);
370 } else {
371 memset(mgmt->da, 0xff, ETH_ALEN);
372 memset(mgmt->bssid, 0xff, ETH_ALEN);
373 }
374 pos = skb_put(skb, 2 + ssid_len);
375 *pos++ = WLAN_EID_SSID;
376 *pos++ = ssid_len;
377 memcpy(pos, ssid, ssid_len);
378
379 supp_rates = skb_put(skb, 2);
380 supp_rates[0] = WLAN_EID_SUPP_RATES;
381 supp_rates[1] = 0;
382 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
383
384 for (i = 0; i < sband->n_bitrates; i++) {
385 struct ieee80211_rate *rate = &sband->bitrates[i];
386 if (esupp_rates) {
387 pos = skb_put(skb, 1);
388 esupp_rates[1]++;
389 } else if (supp_rates[1] == 8) {
390 esupp_rates = skb_put(skb, 3);
391 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
392 esupp_rates[1] = 1;
393 pos = &esupp_rates[2];
394 } else {
395 pos = skb_put(skb, 1);
396 supp_rates[1]++;
397 }
398 *pos = rate->bitrate / 5;
399 }
400
401 ieee80211_sta_tx(sdata, skb, 0);
402}
403
404/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200405static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100406 struct ieee80211_sta_bss *bss,
407 int ibss)
408{
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100409 struct ieee80211_local *local = sdata->local;
410 int i, have_higher_than_11mbit = 0;
411
412
413 /* cf. IEEE 802.11 9.2.12 */
414 for (i = 0; i < bss->supp_rates_len; i++)
415 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
416 have_higher_than_11mbit = 1;
417
418 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
419 have_higher_than_11mbit)
420 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
421 else
422 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
423
424
425 if (local->ops->conf_tx) {
426 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100427
428 memset(&qparam, 0, sizeof(qparam));
429
430 qparam.aifs = 2;
431
432 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
433 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
434 qparam.cw_min = 31;
435 else
436 qparam.cw_min = 15;
437
438 qparam.cw_max = 1023;
439 qparam.txop = 0;
440
Johannes Berge100bb62008-04-30 18:51:21 +0200441 for (i = 0; i < local_to_hw(local)->queues; i++)
442 local->ops->conf_tx(local_to_hw(local), i, &qparam);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100443 }
444}
445
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200446static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -0700447 struct ieee80211_if_sta *ifsta,
448 u8 *wmm_param, size_t wmm_param_len)
449{
Jiri Bencf0706e82007-05-05 11:45:53 -0700450 struct ieee80211_tx_queue_params params;
451 size_t left;
452 int count;
453 u8 *pos;
454
Johannes Berg3434fbd2008-05-03 00:59:37 +0200455 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
456 return;
457
458 if (!wmm_param)
459 return;
460
Jiri Bencf0706e82007-05-05 11:45:53 -0700461 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
462 return;
463 count = wmm_param[6] & 0x0f;
464 if (count == ifsta->wmm_last_param_set)
465 return;
466 ifsta->wmm_last_param_set = count;
467
468 pos = wmm_param + 8;
469 left = wmm_param_len - 8;
470
471 memset(&params, 0, sizeof(params));
472
473 if (!local->ops->conf_tx)
474 return;
475
476 local->wmm_acm = 0;
477 for (; left >= 4; left -= 4, pos += 4) {
478 int aci = (pos[0] >> 5) & 0x03;
479 int acm = (pos[0] >> 4) & 0x01;
480 int queue;
481
482 switch (aci) {
483 case 1:
Johannes Berge100bb62008-04-30 18:51:21 +0200484 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200485 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700486 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700487 break;
488 case 2:
Johannes Berge100bb62008-04-30 18:51:21 +0200489 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200490 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700491 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700492 break;
493 case 3:
Johannes Berge100bb62008-04-30 18:51:21 +0200494 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200495 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700496 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700497 break;
498 case 0:
499 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200500 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200501 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700502 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700503 break;
504 }
505
506 params.aifs = pos[0] & 0x0f;
507 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
508 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +0200509 params.txop = get_unaligned_le16(pos + 2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200510#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700511 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100512 "cWmin=%d cWmax=%d txop=%d\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200513 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100514 params.cw_max, params.txop);
515#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700516 /* TODO: handle ACM (block TX, fallback to next lowest allowed
517 * AC for now) */
518 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
519 printk(KERN_DEBUG "%s: failed to set TX queue "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200520 "parameters for queue %d\n", local->mdev->name, queue);
Jiri Bencf0706e82007-05-05 11:45:53 -0700521 }
522 }
523}
524
John W. Linville50c4afb2008-04-15 14:09:27 -0400525static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
526 bool use_protection,
527 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200528{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100529 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Tomas Winklerebd74482008-07-01 10:44:50 +0300530#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200531 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700532 DECLARE_MAC_BUF(mac);
Tomas Winklerebd74482008-07-01 10:44:50 +0300533#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100534 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200535
Johannes Berg471b3ef2007-12-28 14:32:58 +0100536 if (use_protection != bss_conf->use_cts_prot) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200537#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200538 if (net_ratelimit()) {
539 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700540 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100541 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200542 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700543 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200544 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200545#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100546 bss_conf->use_cts_prot = use_protection;
547 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200548 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200549
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200550 if (use_short_preamble != bss_conf->use_short_preamble) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200551#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake7e9ed182007-07-27 15:43:24 +0200552 if (net_ratelimit()) {
553 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700554 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100555 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200556 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700557 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200558 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200559#endif
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200560 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100561 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200562 }
Daniel Draked9430a32007-07-27 15:43:24 +0200563
Johannes Berg471b3ef2007-12-28 14:32:58 +0100564 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200565}
566
John W. Linville50c4afb2008-04-15 14:09:27 -0400567static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
568 u8 erp_value)
569{
570 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
571 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
572
573 return ieee80211_handle_protect_preamb(sdata,
574 use_protection, use_short_preamble);
575}
576
577static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
578 struct ieee80211_sta_bss *bss)
579{
580 u32 changed = 0;
581
582 if (bss->has_erp_value)
583 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
584 else {
585 u16 capab = bss->capability;
586 changed |= ieee80211_handle_protect_preamb(sdata, false,
587 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
588 }
589
590 return changed;
591}
592
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200593int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
594 struct ieee80211_ht_info *ht_info)
595{
596
597 if (ht_info == NULL)
598 return -EINVAL;
599
600 memset(ht_info, 0, sizeof(*ht_info));
601
602 if (ht_cap_ie) {
603 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
604
605 ht_info->ht_supported = 1;
606 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
607 ht_info->ampdu_factor =
608 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
609 ht_info->ampdu_density =
610 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
611 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
612 } else
613 ht_info->ht_supported = 0;
614
615 return 0;
616}
617
618int ieee80211_ht_addt_info_ie_to_ht_bss_info(
619 struct ieee80211_ht_addt_info *ht_add_info_ie,
620 struct ieee80211_ht_bss_info *bss_info)
621{
622 if (bss_info == NULL)
623 return -EINVAL;
624
625 memset(bss_info, 0, sizeof(*bss_info));
626
627 if (ht_add_info_ie) {
628 u16 op_mode;
629 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
630
631 bss_info->primary_channel = ht_add_info_ie->control_chan;
632 bss_info->bss_cap = ht_add_info_ie->ht_param;
633 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
634 }
635
636 return 0;
637}
Daniel Drake56282212007-07-10 19:32:10 +0200638
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200639static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
640 struct ieee80211_if_sta *ifsta)
641{
642 union iwreq_data wrqu;
643 memset(&wrqu, 0, sizeof(wrqu));
644 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
645 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
646 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
647 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
648}
649
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200650static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700651 struct ieee80211_if_sta *ifsta)
652{
Jiri Bencf0706e82007-05-05 11:45:53 -0700653 union iwreq_data wrqu;
654
Jiri Bencf0706e82007-05-05 11:45:53 -0700655 if (ifsta->assocreq_ies) {
Jouni Malinen087d8332008-08-19 10:54:32 +0300656 memset(&wrqu, 0, sizeof(wrqu));
657 wrqu.data.length = ifsta->assocreq_ies_len;
David S. Millerb171e192008-08-29 23:06:00 -0700658 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
Jouni Malinen087d8332008-08-19 10:54:32 +0300659 ifsta->assocreq_ies);
Jiri Bencf0706e82007-05-05 11:45:53 -0700660 }
661 if (ifsta->assocresp_ies) {
Jouni Malinen087d8332008-08-19 10:54:32 +0300662 memset(&wrqu, 0, sizeof(wrqu));
663 wrqu.data.length = ifsta->assocresp_ies_len;
David S. Millerb171e192008-08-29 23:06:00 -0700664 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
Jouni Malinen087d8332008-08-19 10:54:32 +0300665 ifsta->assocresp_ies);
Jiri Bencf0706e82007-05-05 11:45:53 -0700666 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700667}
668
669
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200670static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200671 struct ieee80211_if_sta *ifsta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700672{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100673 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700674 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100675 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700676
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200677 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400678
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200679 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400680
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200681 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
682 return;
Daniel Drake56282212007-07-10 19:32:10 +0200683
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200684 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
685 conf->channel->center_freq,
686 ifsta->ssid, ifsta->ssid_len);
687 if (bss) {
688 /* set timing information */
689 sdata->bss_conf.beacon_int = bss->beacon_int;
690 sdata->bss_conf.timestamp = bss->timestamp;
691 sdata->bss_conf.dtim_period = bss->dtim_period;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700692
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200693 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700694
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200695 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -0700696 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100697
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200698 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
699 changed |= BSS_CHANGED_HT;
700 sdata->bss_conf.assoc_ht = 1;
701 sdata->bss_conf.ht_conf = &conf->ht_conf;
702 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
703 }
704
705 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
706 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
707 ieee80211_sta_send_associnfo(sdata, ifsta);
708
709 ifsta->last_probe = jiffies;
710 ieee80211_led_assoc(local, 1);
711
712 sdata->bss_conf.assoc = 1;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100713 ieee80211_bss_info_change_notify(sdata, changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300714
Tomas Winkler24e64622008-09-08 17:33:40 +0200715 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200716 netif_carrier_on(sdata->dev);
Guy Cohen8db93692008-07-03 19:56:13 +0300717
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200718 ieee80211_sta_send_apinfo(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700719}
720
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300721static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
722 struct ieee80211_if_sta *ifsta)
723{
724 DECLARE_MAC_BUF(mac);
725
726 ifsta->direct_probe_tries++;
727 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
728 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
729 sdata->dev->name, print_mac(mac, ifsta->bssid));
730 ifsta->state = IEEE80211_STA_MLME_DISABLED;
731 return;
732 }
733
734 printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
735 sdata->dev->name, print_mac(mac, ifsta->bssid),
736 ifsta->direct_probe_tries);
737
738 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
739
740 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
741
742 /* Direct probe is sent to broadcast address as some APs
743 * will not answer to direct packet in unassociated state.
744 */
745 ieee80211_send_probe_req(sdata, NULL,
746 ifsta->ssid, ifsta->ssid_len);
747
748 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
749}
750
Jiri Bencf0706e82007-05-05 11:45:53 -0700751
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200752static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700753 struct ieee80211_if_sta *ifsta)
754{
Joe Perches0795af52007-10-03 17:59:30 -0700755 DECLARE_MAC_BUF(mac);
756
Jiri Bencf0706e82007-05-05 11:45:53 -0700757 ifsta->auth_tries++;
758 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700759 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700760 " timed out\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200761 sdata->dev->name, print_mac(mac, ifsta->bssid));
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300762 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700763 return;
764 }
765
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300766 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700767 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200768 sdata->dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700769
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200770 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700771
772 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
773}
774
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200775static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
776 struct ieee80211_supported_band *sband,
777 u64 *rates)
778{
779 int i, j, count;
780 *rates = 0;
781 count = 0;
782 for (i = 0; i < bss->supp_rates_len; i++) {
783 int rate = (bss->supp_rates[i] & 0x7F) * 5;
784
785 for (j = 0; j < sband->n_bitrates; j++)
786 if (sband->bitrates[j].bitrate == rate) {
787 *rates |= BIT(j);
788 count++;
789 break;
790 }
791 }
792
793 return count;
794}
Jiri Bencf0706e82007-05-05 11:45:53 -0700795
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200796static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700797 struct ieee80211_if_sta *ifsta)
798{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200799 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700800 struct sk_buff *skb;
801 struct ieee80211_mgmt *mgmt;
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300802 u8 *pos, *ies, *ht_add_ie;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200803 int i, len, count, rates_len, supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700804 u16 capab;
805 struct ieee80211_sta_bss *bss;
806 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100807 struct ieee80211_supported_band *sband;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200808 u64 rates = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700809
810 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
811 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
812 ifsta->ssid_len);
813 if (!skb) {
814 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200815 "frame\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -0700816 return;
817 }
818 skb_reserve(skb, local->hw.extra_tx_headroom);
819
Johannes Berg8318d782008-01-24 19:38:38 +0100820 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
821
Jiri Bencf0706e82007-05-05 11:45:53 -0700822 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100823
824 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
825 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
826 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
827 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
828 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700829 }
Johannes Berg8318d782008-01-24 19:38:38 +0100830
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200831 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100832 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400833 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700834 if (bss) {
835 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
836 capab |= WLAN_CAPABILITY_PRIVACY;
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300837 if (bss->wmm_used)
Jiri Bencf0706e82007-05-05 11:45:53 -0700838 wmm = 1;
Helmut Schaa0d580a72008-05-20 09:56:37 +0200839
840 /* get all rates supported by the device and the AP as
841 * some APs don't like getting a superset of their rates
842 * in the association request (e.g. D-Link DAP 1353 in
843 * b-only mode) */
844 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
845
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300846 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
847 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
848 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
849
Johannes Berg3e122be2008-07-09 14:40:34 +0200850 ieee80211_rx_bss_put(local, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200851 } else {
852 rates = ~0;
853 rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700854 }
855
856 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
857 memset(mgmt, 0, 24);
858 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200859 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700860 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
861
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400862 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700863 skb_put(skb, 10);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700864 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
865 IEEE80211_STYPE_REASSOC_REQ);
Jiri Bencf0706e82007-05-05 11:45:53 -0700866 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
Tomas Winklerea95bba2008-07-18 13:53:00 +0800867 mgmt->u.reassoc_req.listen_interval =
868 cpu_to_le16(local->hw.conf.listen_interval);
Jiri Bencf0706e82007-05-05 11:45:53 -0700869 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
870 ETH_ALEN);
871 } else {
872 skb_put(skb, 4);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700873 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
874 IEEE80211_STYPE_ASSOC_REQ);
Jiri Bencf0706e82007-05-05 11:45:53 -0700875 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Tomas Winklerea95bba2008-07-18 13:53:00 +0800876 mgmt->u.reassoc_req.listen_interval =
877 cpu_to_le16(local->hw.conf.listen_interval);
Jiri Bencf0706e82007-05-05 11:45:53 -0700878 }
879
880 /* SSID */
881 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
882 *pos++ = WLAN_EID_SSID;
883 *pos++ = ifsta->ssid_len;
884 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
885
Helmut Schaa0d580a72008-05-20 09:56:37 +0200886 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200887 supp_rates_len = rates_len;
888 if (supp_rates_len > 8)
889 supp_rates_len = 8;
Jiri Bencf0706e82007-05-05 11:45:53 -0700890
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200891 len = sband->n_bitrates;
892 pos = skb_put(skb, supp_rates_len + 2);
893 *pos++ = WLAN_EID_SUPP_RATES;
894 *pos++ = supp_rates_len;
895
896 count = 0;
897 for (i = 0; i < sband->n_bitrates; i++) {
898 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100899 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700900 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200901 if (++count == 8)
902 break;
903 }
904 }
905
Jan-Espen Pettersen8ab65b02008-08-25 20:29:22 +0200906 if (rates_len > count) {
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200907 pos = skb_put(skb, rates_len - count + 2);
908 *pos++ = WLAN_EID_EXT_SUPP_RATES;
909 *pos++ = rates_len - count;
910
911 for (i++; i < sband->n_bitrates; i++) {
912 if (BIT(i) & rates) {
913 int rate = sband->bitrates[i].bitrate;
914 *pos++ = (u8) (rate / 5);
915 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700916 }
917 }
918
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300919 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
920 /* 1. power capabilities */
921 pos = skb_put(skb, 4);
922 *pos++ = WLAN_EID_PWR_CAPABILITY;
923 *pos++ = 2;
924 *pos++ = 0; /* min tx power */
925 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
926
927 /* 2. supported channels */
928 /* TODO: get this in reg domain format */
929 pos = skb_put(skb, 2 * sband->n_channels + 2);
930 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
931 *pos++ = 2 * sband->n_channels;
932 for (i = 0; i < sband->n_channels; i++) {
933 *pos++ = ieee80211_frequency_to_channel(
934 sband->channels[i].center_freq);
935 *pos++ = 1; /* one channel in the subband*/
936 }
937 }
938
Jiri Bencf0706e82007-05-05 11:45:53 -0700939 if (ifsta->extra_ie) {
940 pos = skb_put(skb, ifsta->extra_ie_len);
941 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
942 }
943
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400944 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700945 pos = skb_put(skb, 9);
946 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
947 *pos++ = 7; /* len */
948 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
949 *pos++ = 0x50;
950 *pos++ = 0xf2;
951 *pos++ = 2; /* WME */
952 *pos++ = 0; /* WME info */
953 *pos++ = 1; /* WME ver */
954 *pos++ = 0;
955 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200956
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200957 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200958 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300959 sband->ht_info.ht_supported &&
960 (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800961 struct ieee80211_ht_addt_info *ht_add_info =
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300962 (struct ieee80211_ht_addt_info *)ht_add_ie;
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800963 u16 cap = sband->ht_info.cap;
964 __le16 tmp;
965 u32 flags = local->hw.conf.channel->flags;
966
967 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
968 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
969 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
970 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
971 cap &= ~IEEE80211_HT_CAP_SGI_40;
972 }
973 break;
974 case IEEE80211_HT_IE_CHA_SEC_BELOW:
975 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
976 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
977 cap &= ~IEEE80211_HT_CAP_SGI_40;
978 }
979 break;
980 }
981
982 tmp = cpu_to_le16(cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200983 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
984 *pos++ = WLAN_EID_HT_CAPABILITY;
985 *pos++ = sizeof(struct ieee80211_ht_cap);
986 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
987 memcpy(pos, &tmp, sizeof(u16));
988 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100989 /* TODO: needs a define here for << 2 */
990 *pos++ = sband->ht_info.ampdu_factor |
991 (sband->ht_info.ampdu_density << 2);
992 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200993 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700994
995 kfree(ifsta->assocreq_ies);
996 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200997 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700998 if (ifsta->assocreq_ies)
999 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
1000
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001001 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001002}
1003
1004
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001005static void ieee80211_send_deauth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001006 struct ieee80211_if_sta *ifsta, u16 reason)
1007{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001008 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001009 struct sk_buff *skb;
1010 struct ieee80211_mgmt *mgmt;
1011
1012 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
1013 if (!skb) {
1014 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001015 "frame\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001016 return;
1017 }
1018 skb_reserve(skb, local->hw.extra_tx_headroom);
1019
1020 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1021 memset(mgmt, 0, 24);
1022 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001023 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07001024 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001025 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1026 IEEE80211_STYPE_DEAUTH);
Jiri Bencf0706e82007-05-05 11:45:53 -07001027 skb_put(skb, 2);
1028 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1029
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001030 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001031}
1032
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001033static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
1034{
1035 if (!sdata || !sdata->default_key ||
1036 sdata->default_key->conf.alg != ALG_WEP)
1037 return 0;
1038 return 1;
1039}
Jiri Bencf0706e82007-05-05 11:45:53 -07001040
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001041static void ieee80211_send_disassoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001042 struct ieee80211_if_sta *ifsta, u16 reason)
1043{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001044 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001045 struct sk_buff *skb;
1046 struct ieee80211_mgmt *mgmt;
1047
1048 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
1049 if (!skb) {
1050 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001051 "frame\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001052 return;
1053 }
1054 skb_reserve(skb, local->hw.extra_tx_headroom);
1055
1056 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1057 memset(mgmt, 0, 24);
1058 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001059 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07001060 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001061 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1062 IEEE80211_STYPE_DISASSOC);
Jiri Bencf0706e82007-05-05 11:45:53 -07001063 skb_put(skb, 2);
1064 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
1065
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001066 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001067}
1068
Tomas Winkleraa458d12008-09-09 00:32:12 +03001069static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1070 struct ieee80211_if_sta *ifsta, bool deauth,
1071 bool self_disconnected, u16 reason)
1072{
1073 struct ieee80211_local *local = sdata->local;
1074 struct sta_info *sta;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001075 u32 changed = BSS_CHANGED_ASSOC;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001076
1077 rcu_read_lock();
1078
1079 sta = sta_info_get(local, ifsta->bssid);
1080 if (!sta) {
1081 rcu_read_unlock();
1082 return;
1083 }
1084
1085 if (deauth) {
1086 ifsta->direct_probe_tries = 0;
1087 ifsta->auth_tries = 0;
1088 }
1089 ifsta->assoc_scan_tries = 0;
1090 ifsta->assoc_tries = 0;
1091
Tomas Winkler24e64622008-09-08 17:33:40 +02001092 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001093 netif_carrier_off(sdata->dev);
1094
1095 ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
1096
1097 if (self_disconnected) {
1098 if (deauth)
1099 ieee80211_send_deauth(sdata, ifsta, reason);
1100 else
1101 ieee80211_send_disassoc(sdata, ifsta, reason);
1102 }
1103
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001104 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
1105 changed |= ieee80211_reset_erp_info(sdata);
1106
1107 if (sdata->bss_conf.assoc_ht)
1108 changed |= BSS_CHANGED_HT;
1109
1110 sdata->bss_conf.assoc_ht = 0;
1111 sdata->bss_conf.ht_conf = NULL;
1112 sdata->bss_conf.ht_bss_conf = NULL;
1113
1114 ieee80211_led_assoc(local, 0);
1115 sdata->bss_conf.assoc = 0;
1116
1117 ieee80211_sta_send_apinfo(sdata, ifsta);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001118
1119 if (self_disconnected)
1120 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1121
1122 sta_info_unlink(&sta);
1123
1124 rcu_read_unlock();
1125
1126 sta_info_destroy(sta);
1127}
Jiri Bencf0706e82007-05-05 11:45:53 -07001128
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001129static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001130 struct ieee80211_if_sta *ifsta)
1131{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001132 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001133 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001134 int bss_privacy;
1135 int wep_privacy;
1136 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -07001137
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001138 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -07001139 return 0;
1140
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001141 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001142 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001143 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001144 if (!bss)
1145 return 0;
1146
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001147 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001148 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001149 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -07001150
Johannes Berg3e122be2008-07-09 14:40:34 +02001151 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07001152
Johannes Berg5b98b1f2007-11-03 13:11:10 +00001153 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
1154 return 0;
1155
1156 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001157}
1158
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001159static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001160 struct ieee80211_if_sta *ifsta)
1161{
Joe Perches0795af52007-10-03 17:59:30 -07001162 DECLARE_MAC_BUF(mac);
1163
Jiri Bencf0706e82007-05-05 11:45:53 -07001164 ifsta->assoc_tries++;
1165 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -07001166 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001167 " timed out\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001168 sdata->dev->name, print_mac(mac, ifsta->bssid));
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001169 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001170 return;
1171 }
1172
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001173 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -07001174 printk(KERN_DEBUG "%s: associate with AP %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001175 sdata->dev->name, print_mac(mac, ifsta->bssid));
1176 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001177 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001178 "mixed-cell disabled - abort association\n", sdata->dev->name);
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001179 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001180 return;
1181 }
1182
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001183 ieee80211_send_assoc(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001184
1185 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1186}
1187
1188
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001189static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001190 struct ieee80211_if_sta *ifsta)
1191{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001192 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001193 struct sta_info *sta;
1194 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -07001195 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001196
1197 /* TODO: start monitoring current AP signal quality and number of
1198 * missed beacons. Scan other channels every now and then and search
1199 * for better APs. */
1200 /* TODO: remove expired BSSes */
1201
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001202 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001203
Johannes Bergd0709a62008-02-25 16:27:46 +01001204 rcu_read_lock();
1205
Jiri Bencf0706e82007-05-05 11:45:53 -07001206 sta = sta_info_get(local, ifsta->bssid);
1207 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -07001208 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001209 sdata->dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001210 disassoc = 1;
1211 } else {
1212 disassoc = 0;
1213 if (time_after(jiffies,
1214 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001215 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001216 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -07001217 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -07001218 "range\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001219 sdata->dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001220 disassoc = 1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001221 } else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001222 ieee80211_send_probe_req(sdata, ifsta->bssid,
Jiri Bencf0706e82007-05-05 11:45:53 -07001223 local->scan_ssid,
1224 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001225 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001226 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001227 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001228 if (time_after(jiffies, ifsta->last_probe +
1229 IEEE80211_PROBE_INTERVAL)) {
1230 ifsta->last_probe = jiffies;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001231 ieee80211_send_probe_req(sdata, ifsta->bssid,
Jiri Bencf0706e82007-05-05 11:45:53 -07001232 ifsta->ssid,
1233 ifsta->ssid_len);
1234 }
1235 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001236 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001237
1238 rcu_read_unlock();
1239
Tomas Winkleraa458d12008-09-09 00:32:12 +03001240 if (disassoc)
1241 ieee80211_set_disassoc(sdata, ifsta, true, true,
1242 WLAN_REASON_PREV_AUTH_NOT_VALID);
1243 else
Jiri Bencf0706e82007-05-05 11:45:53 -07001244 mod_timer(&ifsta->timer, jiffies +
1245 IEEE80211_MONITORING_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001246}
1247
1248
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001249static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001250 struct ieee80211_if_sta *ifsta)
1251{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001252 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001253 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001254 ieee80211_associate(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001255}
1256
1257
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001258static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001259 struct ieee80211_if_sta *ifsta,
1260 struct ieee80211_mgmt *mgmt,
1261 size_t len)
1262{
1263 u8 *pos;
1264 struct ieee802_11_elems elems;
1265
Jiri Bencf0706e82007-05-05 11:45:53 -07001266 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001267 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001268 if (!elems.challenge)
Jiri Bencf0706e82007-05-05 11:45:53 -07001269 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001270 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
Jiri Bencf0706e82007-05-05 11:45:53 -07001271 elems.challenge_len + 2, 1);
1272}
1273
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001274static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001275 u8 dialog_token, u16 status, u16 policy,
1276 u16 buf_size, u16 timeout)
1277{
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001278 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001279 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001280 struct sk_buff *skb;
1281 struct ieee80211_mgmt *mgmt;
1282 u16 capab;
1283
Ester Kummer3acea5b2008-04-17 16:05:14 -07001284 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1285
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001286 if (!skb) {
1287 printk(KERN_DEBUG "%s: failed to allocate buffer "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001288 "for addba resp frame\n", sdata->dev->name);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001289 return;
1290 }
1291
1292 skb_reserve(skb, local->hw.extra_tx_headroom);
1293 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1294 memset(mgmt, 0, 24);
1295 memcpy(mgmt->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001296 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001297 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001298 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001299 else
1300 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001301 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1302 IEEE80211_STYPE_ACTION);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001303
1304 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1305 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1306 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1307 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1308
1309 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1310 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1311 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1312
1313 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1314 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1315 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1316
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001317 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001318
1319 return;
1320}
1321
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001322void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata, const u8 *da,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001323 u16 tid, u8 dialog_token, u16 start_seq_num,
1324 u16 agg_size, u16 timeout)
1325{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001326 struct ieee80211_local *local = sdata->local;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001327 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1328 struct sk_buff *skb;
1329 struct ieee80211_mgmt *mgmt;
1330 u16 capab;
1331
Ester Kummer3acea5b2008-04-17 16:05:14 -07001332 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001333
1334 if (!skb) {
1335 printk(KERN_ERR "%s: failed to allocate buffer "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001336 "for addba request frame\n", sdata->dev->name);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001337 return;
1338 }
1339 skb_reserve(skb, local->hw.extra_tx_headroom);
1340 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1341 memset(mgmt, 0, 24);
1342 memcpy(mgmt->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001343 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001344 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001345 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001346 else
1347 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1348
Harvey Harrisone7827a72008-07-15 18:44:13 -07001349 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1350 IEEE80211_STYPE_ACTION);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001351
1352 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1353
1354 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1355 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1356
1357 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1358 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1359 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1360 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1361
1362 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1363
1364 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1365 mgmt->u.action.u.addba_req.start_seq_num =
1366 cpu_to_le16(start_seq_num << 4);
1367
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001368 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001369}
1370
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001371/*
1372 * After accepting the AddBA Request we activated a timer,
1373 * resetting it after each frame that arrives from the originator.
1374 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1375 */
1376static void sta_rx_agg_session_timer_expired(unsigned long data)
1377{
1378 /* not an elegant detour, but there is no choice as the timer passes
1379 * only one argument, and various sta_info are needed here, so init
1380 * flow in sta_info_create gives the TID as data, while the timer_to_id
1381 * array gives the sta through container_of */
1382 u8 *ptid = (u8 *)data;
1383 u8 *timer_to_id = ptid - *ptid;
1384 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1385 timer_to_tid[0]);
1386
1387#ifdef CONFIG_MAC80211_HT_DEBUG
1388 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1389#endif
1390 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
1391 (u16)*ptid, WLAN_BACK_TIMER,
1392 WLAN_REASON_QSTA_TIMEOUT);
1393}
1394
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001395static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001396 struct ieee80211_mgmt *mgmt,
1397 size_t len)
1398{
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001399 struct ieee80211_hw *hw = &local->hw;
1400 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001401 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001402 struct tid_ampdu_rx *tid_agg_rx;
1403 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001404 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001405 int ret = -EOPNOTSUPP;
1406 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001407
Johannes Bergd0709a62008-02-25 16:27:46 +01001408 rcu_read_lock();
1409
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001410 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001411 if (!sta) {
1412 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001413 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001414 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001415
1416 /* extract session parameters from addba request frame */
1417 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1418 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001419 start_seq_num =
1420 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001421
1422 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1423 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1424 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1425 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1426
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001427 status = WLAN_STATUS_REQUEST_DECLINED;
1428
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001429 /* sanity check for incoming parameters:
1430 * check if configuration can support the BA policy
1431 * and if buffer size does not exceeds max value */
1432 if (((ba_policy != 1)
1433 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1434 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1435 status = WLAN_STATUS_INVALID_QOS_PARAM;
1436#ifdef CONFIG_MAC80211_HT_DEBUG
1437 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001438 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001439 "%s on tid %u. policy %d, buffer size %d\n",
1440 print_mac(mac, mgmt->sa), tid, ba_policy,
1441 buf_size);
1442#endif /* CONFIG_MAC80211_HT_DEBUG */
1443 goto end_no_lock;
1444 }
1445 /* determine default buffer size */
1446 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001447 struct ieee80211_supported_band *sband;
1448
1449 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001450 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001451 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001452 }
1453
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001454
1455 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001456 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001457
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001458 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001459#ifdef CONFIG_MAC80211_HT_DEBUG
1460 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001461 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001462 "%s on tid %u\n",
1463 print_mac(mac, mgmt->sa), tid);
1464#endif /* CONFIG_MAC80211_HT_DEBUG */
1465 goto end;
1466 }
1467
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001468 /* prepare A-MPDU MLME for Rx aggregation */
1469 sta->ampdu_mlme.tid_rx[tid] =
1470 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1471 if (!sta->ampdu_mlme.tid_rx[tid]) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001472#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001473 if (net_ratelimit())
1474 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1475 tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001476#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001477 goto end;
1478 }
1479 /* rx timer */
1480 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1481 sta_rx_agg_session_timer_expired;
1482 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1483 (unsigned long)&sta->timer_to_tid[tid];
1484 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1485
1486 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1487
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001488 /* prepare reordering buffer */
1489 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301490 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001491 if (!tid_agg_rx->reorder_buf) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001492#ifdef CONFIG_MAC80211_HT_DEBUG
Johannes Berg03147df2008-02-26 00:39:28 +01001493 if (net_ratelimit())
1494 printk(KERN_ERR "can not allocate reordering buffer "
1495 "to tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001496#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001497 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001498 goto end;
1499 }
1500 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301501 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001502
1503 if (local->ops->ampdu_action)
1504 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001505 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001506#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001507 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001508#endif /* CONFIG_MAC80211_HT_DEBUG */
1509
1510 if (ret) {
1511 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001512 kfree(tid_agg_rx);
1513 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001514 goto end;
1515 }
1516
1517 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001518 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001519 tid_agg_rx->dialog_token = dialog_token;
1520 tid_agg_rx->ssn = start_seq_num;
1521 tid_agg_rx->head_seq_num = start_seq_num;
1522 tid_agg_rx->buf_size = buf_size;
1523 tid_agg_rx->timeout = timeout;
1524 tid_agg_rx->stored_mpdu_num = 0;
1525 status = WLAN_STATUS_SUCCESS;
1526end:
Johannes Berg07346f812008-05-03 01:02:02 +02001527 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001528
1529end_no_lock:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001530 ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
Johannes Bergd0709a62008-02-25 16:27:46 +01001531 dialog_token, status, 1, buf_size, timeout);
1532 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001533}
Jiri Bencf0706e82007-05-05 11:45:53 -07001534
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001535static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001536 struct ieee80211_mgmt *mgmt,
1537 size_t len)
1538{
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001539 struct ieee80211_hw *hw = &local->hw;
1540 struct sta_info *sta;
1541 u16 capab;
1542 u16 tid;
1543 u8 *state;
1544
Johannes Bergd0709a62008-02-25 16:27:46 +01001545 rcu_read_lock();
1546
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001547 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001548 if (!sta) {
1549 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001550 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001551 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001552
1553 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1554 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1555
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001556 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001557
Johannes Berg07346f812008-05-03 01:02:02 +02001558 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001559
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001560 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001561 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001562 goto addba_resp_exit;
1563 }
1564
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001565 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001566 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001567 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001568#ifdef CONFIG_MAC80211_HT_DEBUG
1569 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1570#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001571 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001572 }
1573
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001574 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001575#ifdef CONFIG_MAC80211_HT_DEBUG
1576 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1577#endif /* CONFIG_MAC80211_HT_DEBUG */
1578 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1579 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001580 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001581 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001582
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001583 if (*state == HT_AGG_STATE_OPERATIONAL)
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001584 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001585
Johannes Berg07346f812008-05-03 01:02:02 +02001586 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001587 } else {
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001588 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001589 /* this will allow the state check in stop_BA_session */
1590 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001591 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001592 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1593 WLAN_BACK_INITIATOR);
1594 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001595
1596addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001597 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001598}
1599
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001600void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata, const u8 *da, u16 tid,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001601 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001602{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001603 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001604 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1605 struct sk_buff *skb;
1606 struct ieee80211_mgmt *mgmt;
1607 u16 params;
1608
Ester Kummer3acea5b2008-04-17 16:05:14 -07001609 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001610
1611 if (!skb) {
1612 printk(KERN_ERR "%s: failed to allocate buffer "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001613 "for delba frame\n", sdata->dev->name);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001614 return;
1615 }
1616
1617 skb_reserve(skb, local->hw.extra_tx_headroom);
1618 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1619 memset(mgmt, 0, 24);
1620 memcpy(mgmt->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001621 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001622 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001623 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001624 else
1625 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001626 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1627 IEEE80211_STYPE_ACTION);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001628
1629 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1630
1631 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1632 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1633 params = (u16)(initiator << 11); /* bit 11 initiator */
1634 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1635
1636 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1637 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1638
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001639 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001640}
1641
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001642void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001643{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001644 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001645 struct sk_buff *skb;
1646 struct ieee80211_bar *bar;
1647 u16 bar_control = 0;
1648
1649 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1650 if (!skb) {
1651 printk(KERN_ERR "%s: failed to allocate buffer for "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001652 "bar frame\n", sdata->dev->name);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001653 return;
1654 }
1655 skb_reserve(skb, local->hw.extra_tx_headroom);
1656 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1657 memset(bar, 0, sizeof(*bar));
Harvey Harrisone7827a72008-07-15 18:44:13 -07001658 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1659 IEEE80211_STYPE_BACK_REQ);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001660 memcpy(bar->ra, ra, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001661 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001662 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1663 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1664 bar_control |= (u16)(tid << 12);
1665 bar->control = cpu_to_le16(bar_control);
1666 bar->start_seq_num = cpu_to_le16(ssn);
1667
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001668 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001669}
1670
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001671void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001672 u16 initiator, u16 reason)
1673{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001674 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001675 struct ieee80211_hw *hw = &local->hw;
1676 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001677 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001678 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001679
Johannes Bergd0709a62008-02-25 16:27:46 +01001680 rcu_read_lock();
1681
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001682 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001683 if (!sta) {
1684 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001685 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001686 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001687
1688 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001689 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001690 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001691 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001692 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001693 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001694 return;
1695 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001696 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001697 HT_AGG_STATE_REQ_STOP_BA_MSK |
1698 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001699 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001700
1701 /* stop HW Rx aggregation. ampdu_action existence
1702 * already verified in session init so we add the BUG_ON */
1703 BUG_ON(!local->ops->ampdu_action);
1704
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001705#ifdef CONFIG_MAC80211_HT_DEBUG
1706 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1707 print_mac(mac, ra), tid);
1708#endif /* CONFIG_MAC80211_HT_DEBUG */
1709
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001710 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001711 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001712 if (ret)
1713 printk(KERN_DEBUG "HW problem - can not stop rx "
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001714 "aggregation for tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001715
1716 /* shutdown timer has not expired */
1717 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001718 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001719
1720 /* check if this is a self generated aggregation halt */
1721 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001722 ieee80211_send_delba(sdata, ra, tid, 0, reason);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001723
1724 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001725 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1726 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001727 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001728 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1729 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1730 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001731 }
1732 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001733 /* free resources */
1734 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1735 kfree(sta->ampdu_mlme.tid_rx[tid]);
1736 sta->ampdu_mlme.tid_rx[tid] = NULL;
1737 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001738
Johannes Bergd0709a62008-02-25 16:27:46 +01001739 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001740}
1741
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001742
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001743static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001744 struct ieee80211_mgmt *mgmt, size_t len)
1745{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001746 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001747 struct sta_info *sta;
1748 u16 tid, params;
1749 u16 initiator;
1750 DECLARE_MAC_BUF(mac);
1751
Johannes Bergd0709a62008-02-25 16:27:46 +01001752 rcu_read_lock();
1753
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001754 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001755 if (!sta) {
1756 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001757 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001758 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001759
1760 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1761 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1762 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1763
1764#ifdef CONFIG_MAC80211_HT_DEBUG
1765 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001766 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1767 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001768 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001769 mgmt->u.action.u.delba.reason_code);
1770#endif /* CONFIG_MAC80211_HT_DEBUG */
1771
1772 if (initiator == WLAN_BACK_INITIATOR)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001773 ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001774 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001775 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001776 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001777 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001778 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001779 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001780 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1781 WLAN_BACK_RECIPIENT);
1782 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001783 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001784}
1785
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001786/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001787 * After sending add Block Ack request we activated a timer until
1788 * add Block Ack response will arrive from the recipient.
1789 * If this timer expires sta_addba_resp_timer_expired will be executed.
1790 */
1791void sta_addba_resp_timer_expired(unsigned long data)
1792{
1793 /* not an elegant detour, but there is no choice as the timer passes
1794 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001795 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001796 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301797 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001798 struct sta_info *temp_sta = container_of((void *)data,
1799 struct sta_info, timer_to_tid[tid]);
1800
1801 struct ieee80211_local *local = temp_sta->local;
1802 struct ieee80211_hw *hw = &local->hw;
1803 struct sta_info *sta;
1804 u8 *state;
1805
Johannes Bergd0709a62008-02-25 16:27:46 +01001806 rcu_read_lock();
1807
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001808 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001809 if (!sta) {
1810 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001811 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001812 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001813
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001814 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001815 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001816 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001817 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001818 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001819 *state = HT_AGG_STATE_IDLE;
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001820#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001821 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1822 "expecting addBA response there", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001823#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001824 goto timer_expired_exit;
1825 }
1826
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001827#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001828 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001829#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001830
1831 /* go through the state check in stop_BA_session */
1832 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001833 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001834 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1835 WLAN_BACK_INITIATOR);
1836
1837timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001838 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001839}
1840
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001841void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001842{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001843 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001844 int i;
1845
1846 for (i = 0; i < STA_TID_NUM; i++) {
1847 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1848 WLAN_BACK_INITIATOR);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001849 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001850 WLAN_BACK_RECIPIENT,
1851 WLAN_REASON_QSTA_LEAVE_QBSS);
1852 }
1853}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001854
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001855static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
Assaf Kraussb6623482008-06-16 16:09:49 +03001856 struct ieee80211_msrment_ie *request_ie,
1857 const u8 *da, const u8 *bssid,
1858 u8 dialog_token)
1859{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001860 struct ieee80211_local *local = sdata->local;
Assaf Kraussb6623482008-06-16 16:09:49 +03001861 struct sk_buff *skb;
1862 struct ieee80211_mgmt *msr_report;
1863
1864 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1865 sizeof(struct ieee80211_msrment_ie));
1866
1867 if (!skb) {
1868 printk(KERN_ERR "%s: failed to allocate buffer for "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001869 "measurement report frame\n", sdata->dev->name);
Assaf Kraussb6623482008-06-16 16:09:49 +03001870 return;
1871 }
1872
1873 skb_reserve(skb, local->hw.extra_tx_headroom);
1874 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1875 memset(msr_report, 0, 24);
1876 memcpy(msr_report->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001877 memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
Assaf Kraussb6623482008-06-16 16:09:49 +03001878 memcpy(msr_report->bssid, bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001879 msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
Assaf Kraussb6623482008-06-16 16:09:49 +03001880 IEEE80211_STYPE_ACTION);
1881
1882 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1883 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1884 msr_report->u.action.u.measurement.action_code =
1885 WLAN_ACTION_SPCT_MSR_RPRT;
1886 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1887
1888 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1889 msr_report->u.action.u.measurement.length =
1890 sizeof(struct ieee80211_msrment_ie);
1891
1892 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1893 sizeof(struct ieee80211_msrment_ie));
1894 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1895 msr_report->u.action.u.measurement.msr_elem.mode |=
1896 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1897 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1898
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001899 ieee80211_sta_tx(sdata, skb, 0);
Assaf Kraussb6623482008-06-16 16:09:49 +03001900}
1901
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001902static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
Assaf Kraussb6623482008-06-16 16:09:49 +03001903 struct ieee80211_mgmt *mgmt,
1904 size_t len)
1905{
1906 /*
1907 * Ignoring measurement request is spec violation.
1908 * Mandatory measurements must be reported optional
1909 * measurements might be refused or reported incapable
1910 * For now just refuse
1911 * TODO: Answer basic measurement as unmeasured
1912 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001913 ieee80211_send_refuse_measurement_request(sdata,
Assaf Kraussb6623482008-06-16 16:09:49 +03001914 &mgmt->u.action.u.measurement.msr_elem,
1915 mgmt->sa, mgmt->bssid,
1916 mgmt->u.action.u.measurement.dialog_token);
1917}
1918
1919
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001920static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001921 struct ieee80211_if_sta *ifsta,
1922 struct ieee80211_mgmt *mgmt,
1923 size_t len)
1924{
Jiri Bencf0706e82007-05-05 11:45:53 -07001925 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001926 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001927
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001928 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001929 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07001930 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001931
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001932 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001933 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001934
Johannes Berg51fb61e2007-12-19 01:31:27 +01001935 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001936 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001937 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001938
Johannes Berg51fb61e2007-12-19 01:31:27 +01001939 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001940 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001941 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001942
1943 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1944 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1945 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1946
Johannes Berg51fb61e2007-12-19 01:31:27 +01001947 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg135a2112008-06-16 20:55:29 +02001948 /*
1949 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e82007-05-05 11:45:53 -07001950 * networks and most implementations do not seem to use it.
1951 * However, try to reply to authentication attempts if someone
1952 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001953 */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001954 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
Jiri Bencf0706e82007-05-05 11:45:53 -07001955 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001956 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001957 }
1958
1959 if (auth_alg != ifsta->auth_alg ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001960 auth_transaction != ifsta->auth_transaction)
Jiri Bencf0706e82007-05-05 11:45:53 -07001961 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001962
1963 if (status_code != WLAN_STATUS_SUCCESS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001964 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1965 u8 algs[3];
1966 const int num_algs = ARRAY_SIZE(algs);
1967 int i, pos;
1968 algs[0] = algs[1] = algs[2] = 0xff;
1969 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1970 algs[0] = WLAN_AUTH_OPEN;
1971 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1972 algs[1] = WLAN_AUTH_SHARED_KEY;
1973 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1974 algs[2] = WLAN_AUTH_LEAP;
1975 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1976 pos = 0;
1977 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1978 pos = 1;
1979 else
1980 pos = 2;
1981 for (i = 0; i < num_algs; i++) {
1982 pos++;
1983 if (pos >= num_algs)
1984 pos = 0;
1985 if (algs[pos] == ifsta->auth_alg ||
1986 algs[pos] == 0xff)
1987 continue;
1988 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001989 !ieee80211_sta_wep_configured(sdata))
Jiri Bencf0706e82007-05-05 11:45:53 -07001990 continue;
1991 ifsta->auth_alg = algs[pos];
Jiri Bencf0706e82007-05-05 11:45:53 -07001992 break;
1993 }
1994 }
1995 return;
1996 }
1997
1998 switch (ifsta->auth_alg) {
1999 case WLAN_AUTH_OPEN:
2000 case WLAN_AUTH_LEAP:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002001 ieee80211_auth_completed(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002002 break;
2003 case WLAN_AUTH_SHARED_KEY:
2004 if (ifsta->auth_transaction == 4)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002005 ieee80211_auth_completed(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002006 else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002007 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002008 break;
2009 }
2010}
2011
2012
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002013static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002014 struct ieee80211_if_sta *ifsta,
2015 struct ieee80211_mgmt *mgmt,
2016 size_t len)
2017{
2018 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07002019 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002020
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002021 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07002022 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002023
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002024 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07002025 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002026
2027 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2028
Johannes Berg988c0f72008-04-17 19:21:22 +02002029 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002030 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07002031
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002032 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
2033 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
2034 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002035 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002036 mod_timer(&ifsta->timer, jiffies +
2037 IEEE80211_RETRY_AUTH_INTERVAL);
2038 }
2039
Tomas Winkleraa458d12008-09-09 00:32:12 +03002040 ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002041 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002042}
2043
2044
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002045static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002046 struct ieee80211_if_sta *ifsta,
2047 struct ieee80211_mgmt *mgmt,
2048 size_t len)
2049{
2050 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07002051 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002052
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002053 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07002054 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002055
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002056 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07002057 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002058
2059 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2060
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002061 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002062 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07002063
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002064 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
2065 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002066 mod_timer(&ifsta->timer, jiffies +
2067 IEEE80211_RETRY_AUTH_INTERVAL);
2068 }
2069
Tomas Winkleraa458d12008-09-09 00:32:12 +03002070 ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002071}
2072
2073
Johannes Berg471b3ef2007-12-28 14:32:58 +01002074static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002075 struct ieee80211_if_sta *ifsta,
2076 struct ieee80211_mgmt *mgmt,
2077 size_t len,
2078 int reassoc)
2079{
Johannes Berg471b3ef2007-12-28 14:32:58 +01002080 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002081 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002082 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01002083 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002084 u16 capab_info, status_code, aid;
2085 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002086 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002087 u8 *pos;
2088 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07002089 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01002090 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002091
2092 /* AssocResp and ReassocResp have identical structure, so process both
2093 * of them in this function. */
2094
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002095 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
Jiri Bencf0706e82007-05-05 11:45:53 -07002096 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002097
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002098 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07002099 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002100
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002101 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07002102 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002103
2104 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2105 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2106 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002107
Joe Perches0795af52007-10-03 17:59:30 -07002108 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07002109 "status=%d aid=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002110 sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02002111 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07002112
2113 if (status_code != WLAN_STATUS_SUCCESS) {
2114 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002115 sdata->dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02002116 /* if this was a reassociation, ensure we try a "full"
2117 * association next time. This works around some broken APs
2118 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002119 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07002120 return;
2121 }
2122
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002123 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2124 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002125 "set\n", sdata->dev->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002126 aid &= ~(BIT(15) | BIT(14));
2127
Jiri Bencf0706e82007-05-05 11:45:53 -07002128 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04002129 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002130
2131 if (!elems.supp_rates) {
2132 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002133 sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07002134 return;
2135 }
2136
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002137 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07002138 ifsta->aid = aid;
2139 ifsta->ap_capab = capab_info;
2140
2141 kfree(ifsta->assocresp_ies);
2142 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002143 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002144 if (ifsta->assocresp_ies)
2145 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2146
Johannes Bergd0709a62008-02-25 16:27:46 +01002147 rcu_read_lock();
2148
Jiri Bencf0706e82007-05-05 11:45:53 -07002149 /* Add STA entry for the AP */
2150 sta = sta_info_get(local, ifsta->bssid);
2151 if (!sta) {
2152 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01002153 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01002154
Johannes Berg73651ee2008-02-25 16:27:47 +01002155 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2156 if (!sta) {
2157 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002158 " the AP\n", sdata->dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01002159 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002160 return;
2161 }
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002162 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002163 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002164 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002165 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002166 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002167 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07002168 sta->last_noise = bss->noise;
Johannes Berg3e122be2008-07-09 14:40:34 +02002169 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002170 }
Johannes Berg73651ee2008-02-25 16:27:47 +01002171
2172 err = sta_info_insert(sta);
2173 if (err) {
2174 printk(KERN_DEBUG "%s: failed to insert STA entry for"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002175 " the AP (error %d)\n", sdata->dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01002176 rcu_read_unlock();
2177 return;
2178 }
Ron Rindjunskya61dae12008-08-10 00:54:34 +03002179 /* update new sta with its last rx activity */
2180 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -07002181 }
2182
Johannes Berg73651ee2008-02-25 16:27:47 +01002183 /*
2184 * FIXME: Do we really need to update the sta_info's information here?
2185 * We already know about the AP (we found it in our list) so it
2186 * should already be filled with the right info, no?
2187 * As is stands, all this is racy because typically we assume
2188 * the information that is filled in here (except flags) doesn't
2189 * change while a STA structure is alive. As such, it should move
2190 * to between the sta_info_alloc() and sta_info_insert() above.
2191 */
2192
Johannes Berg07346f812008-05-03 01:02:02 +02002193 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2194 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07002195
2196 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002197 basic_rates = 0;
2198 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2199
Jiri Bencf0706e82007-05-05 11:45:53 -07002200 for (i = 0; i < elems.supp_rates_len; i++) {
2201 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002202
2203 if (rate > 110)
2204 have_higher_than_11mbit = true;
2205
2206 for (j = 0; j < sband->n_bitrates; j++) {
2207 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002208 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002209 if (elems.supp_rates[i] & 0x80)
2210 basic_rates |= BIT(j);
2211 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002212 }
Johannes Berg8318d782008-01-24 19:38:38 +01002213
Jiri Bencf0706e82007-05-05 11:45:53 -07002214 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2215 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002216
2217 if (rate > 110)
2218 have_higher_than_11mbit = true;
2219
2220 for (j = 0; j < sband->n_bitrates; j++) {
2221 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002222 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002223 if (elems.ext_supp_rates[i] & 0x80)
2224 basic_rates |= BIT(j);
2225 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002226 }
Johannes Berg8318d782008-01-24 19:38:38 +01002227
2228 sta->supp_rates[local->hw.conf.channel->band] = rates;
2229 sdata->basic_rates = basic_rates;
2230
2231 /* cf. IEEE 802.11 9.2.12 */
2232 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2233 have_higher_than_11mbit)
2234 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2235 else
2236 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002237
Johannes Berg3434fbd2008-05-03 00:59:37 +02002238 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2239 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002240 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002241 ieee80211_ht_cap_ie_to_ht_info(
2242 (struct ieee80211_ht_cap *)
2243 elems.ht_cap_elem, &sta->ht_info);
2244 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2245 (struct ieee80211_ht_addt_info *)
2246 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002247 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002248 }
2249
Jiri Bencf0706e82007-05-05 11:45:53 -07002250 rate_control_rate_init(sta, local);
2251
Johannes Berg3434fbd2008-05-03 00:59:37 +02002252 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002253 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002254 rcu_read_unlock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002255 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002256 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002257 } else
2258 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002259
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002260 /* set AID and assoc capability,
2261 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002262 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002263 bss_conf->assoc_capability = capab_info;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02002264 ieee80211_set_associated(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002265
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002266 ieee80211_associated(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002267}
2268
2269
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002270static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
Bruno Randolfa6072682008-02-18 11:21:15 +09002271 struct ieee80211_if_sta *ifsta,
2272 struct ieee80211_sta_bss *bss)
2273{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002274 struct ieee80211_local *local = sdata->local;
Bruno Randolfa6072682008-02-18 11:21:15 +09002275 int res, rates, i, j;
2276 struct sk_buff *skb;
2277 struct ieee80211_mgmt *mgmt;
Bruno Randolfa6072682008-02-18 11:21:15 +09002278 u8 *pos;
Bruno Randolfa6072682008-02-18 11:21:15 +09002279 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002280 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002281
2282 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2283
2284 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002285 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002286
2287 if (local->ops->reset_tsf) {
2288 /* Reset own TSF to allow time synchronization work. */
2289 local->ops->reset_tsf(local_to_hw(local));
2290 }
2291 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
Johannes Berg9d139c82008-07-09 14:40:37 +02002292 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Bruno Randolfa6072682008-02-18 11:21:15 +09002293 if (res)
2294 return res;
2295
2296 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2297
Bruno Randolfa6072682008-02-18 11:21:15 +09002298 sdata->drop_unencrypted = bss->capability &
2299 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2300
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002301 res = ieee80211_set_freq(sdata, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002302
Assaf Kraussbe038b32008-06-05 19:55:21 +03002303 if (res)
2304 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002305
Johannes Berg9d139c82008-07-09 14:40:37 +02002306 /* Build IBSS probe response */
Bruno Randolfa6072682008-02-18 11:21:15 +09002307 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
Johannes Berg9d139c82008-07-09 14:40:37 +02002308 if (skb) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002309 skb_reserve(skb, local->hw.extra_tx_headroom);
2310
2311 mgmt = (struct ieee80211_mgmt *)
2312 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2313 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
Harvey Harrisone7827a72008-07-15 18:44:13 -07002314 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2315 IEEE80211_STYPE_PROBE_RESP);
Bruno Randolfa6072682008-02-18 11:21:15 +09002316 memset(mgmt->da, 0xff, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002317 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Bruno Randolfa6072682008-02-18 11:21:15 +09002318 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2319 mgmt->u.beacon.beacon_int =
2320 cpu_to_le16(local->hw.conf.beacon_int);
Assaf Krauss4faeb862008-06-30 17:23:16 +08002321 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
Bruno Randolfa6072682008-02-18 11:21:15 +09002322 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2323
2324 pos = skb_put(skb, 2 + ifsta->ssid_len);
2325 *pos++ = WLAN_EID_SSID;
2326 *pos++ = ifsta->ssid_len;
2327 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2328
2329 rates = bss->supp_rates_len;
2330 if (rates > 8)
2331 rates = 8;
2332 pos = skb_put(skb, 2 + rates);
2333 *pos++ = WLAN_EID_SUPP_RATES;
2334 *pos++ = rates;
2335 memcpy(pos, bss->supp_rates, rates);
2336
2337 if (bss->band == IEEE80211_BAND_2GHZ) {
2338 pos = skb_put(skb, 2 + 1);
2339 *pos++ = WLAN_EID_DS_PARAMS;
2340 *pos++ = 1;
2341 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2342 }
2343
2344 pos = skb_put(skb, 2 + 2);
2345 *pos++ = WLAN_EID_IBSS_PARAMS;
2346 *pos++ = 2;
2347 /* FIX: set ATIM window based on scan results */
2348 *pos++ = 0;
2349 *pos++ = 0;
2350
2351 if (bss->supp_rates_len > 8) {
2352 rates = bss->supp_rates_len - 8;
2353 pos = skb_put(skb, 2 + rates);
2354 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2355 *pos++ = rates;
2356 memcpy(pos, &bss->supp_rates[8], rates);
2357 }
2358
Johannes Berg9d139c82008-07-09 14:40:37 +02002359 ifsta->probe_resp = skb;
Johannes Berge039fa42008-05-15 12:55:29 +02002360
Johannes Berg9d139c82008-07-09 14:40:37 +02002361 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Bruno Randolfa6072682008-02-18 11:21:15 +09002362 }
2363
Johannes Berg9d139c82008-07-09 14:40:37 +02002364 rates = 0;
2365 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2366 for (i = 0; i < bss->supp_rates_len; i++) {
2367 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2368 for (j = 0; j < sband->n_bitrates; j++)
2369 if (sband->bitrates[j].bitrate == bitrate)
2370 rates |= BIT(j);
2371 }
2372 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2373
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002374 ieee80211_sta_def_wmm_params(sdata, bss, 1);
Johannes Berg9d139c82008-07-09 14:40:37 +02002375
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002376 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
Bruno Randolfa6072682008-02-18 11:21:15 +09002377 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2378
Dan Williams507b06d2008-06-03 23:39:55 -04002379 memset(&wrqu, 0, sizeof(wrqu));
2380 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002381 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09002382
2383 return res;
2384}
2385
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002386u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2387 struct ieee802_11_elems *elems,
2388 enum ieee80211_band band)
2389{
2390 struct ieee80211_supported_band *sband;
2391 struct ieee80211_rate *bitrates;
2392 size_t num_rates;
2393 u64 supp_rates;
2394 int i, j;
2395 sband = local->hw.wiphy->bands[band];
2396
2397 if (!sband) {
2398 WARN_ON(1);
2399 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2400 }
2401
2402 bitrates = sband->bitrates;
2403 num_rates = sband->n_bitrates;
2404 supp_rates = 0;
2405 for (i = 0; i < elems->supp_rates_len +
2406 elems->ext_supp_rates_len; i++) {
2407 u8 rate = 0;
2408 int own_rate;
2409 if (i < elems->supp_rates_len)
2410 rate = elems->supp_rates[i];
2411 else if (elems->ext_supp_rates)
2412 rate = elems->ext_supp_rates
2413 [i - elems->supp_rates_len];
2414 own_rate = 5 * (rate & 0x7f);
2415 for (j = 0; j < num_rates; j++)
2416 if (bitrates[j].bitrate == own_rate)
2417 supp_rates |= BIT(j);
2418 }
2419 return supp_rates;
2420}
2421
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002422static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
2423 enum ieee80211_band band)
2424{
2425 struct ieee80211_supported_band *sband;
2426 struct ieee80211_rate *bitrates;
2427 u64 mandatory_rates;
2428 enum ieee80211_rate_flags mandatory_flag;
2429 int i;
2430
2431 sband = local->hw.wiphy->bands[band];
2432 if (!sband) {
2433 WARN_ON(1);
2434 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2435 }
2436
2437 if (band == IEEE80211_BAND_2GHZ)
2438 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
2439 else
2440 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
2441
2442 bitrates = sband->bitrates;
2443 mandatory_rates = 0;
2444 for (i = 0; i < sband->n_bitrates; i++)
2445 if (bitrates[i].flags & mandatory_flag)
2446 mandatory_rates |= BIT(i);
2447 return mandatory_rates;
2448}
Bruno Randolfa6072682008-02-18 11:21:15 +09002449
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002450static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002451 struct ieee80211_mgmt *mgmt,
2452 size_t len,
2453 struct ieee80211_rx_status *rx_status,
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002454 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002455{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002456 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002457 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002458 struct ieee80211_sta_bss *bss;
2459 struct sta_info *sta;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002460 struct ieee80211_channel *channel;
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002461 u64 beacon_timestamp, rx_timestamp;
2462 u64 supp_rates = 0;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002463 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002464 enum ieee80211_band band = rx_status->band;
Joe Perches0795af52007-10-03 17:59:30 -07002465 DECLARE_MAC_BUF(mac);
2466 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002467
Johannes Berg5bda6172008-09-08 11:05:10 +02002468 if (elems->ds_params && elems->ds_params_len == 1)
2469 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
2470 else
2471 freq = rx_status->freq;
2472
2473 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2474
2475 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2476 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002477
Ester Kummerae6a44e2008-06-27 18:54:48 +03002478 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002479 elems->mesh_config && mesh_matches_local(elems, sdata)) {
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002480 supp_rates = ieee80211_sta_get_rates(local, elems, band);
Johannes Berg902acc72008-02-23 15:17:19 +01002481
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002482 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002483 mesh_peer_accepts_plinks(elems));
Johannes Berg902acc72008-02-23 15:17:19 +01002484 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002485
Ester Kummerae6a44e2008-06-27 18:54:48 +03002486 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002487 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002488 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2489
Johannes Berg69e6c012008-09-08 11:05:08 +02002490 rcu_read_lock();
2491
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002492 sta = sta_info_get(local, mgmt->sa);
2493 if (sta) {
2494 u64 prev_rates;
2495
2496 prev_rates = sta->supp_rates[band];
2497 /* make sure mandatory rates are always added */
2498 sta->supp_rates[band] = supp_rates |
2499 ieee80211_sta_get_mandatory_rates(local, band);
2500
2501#ifdef CONFIG_MAC80211_IBSS_DEBUG
2502 if (sta->supp_rates[band] != prev_rates)
2503 printk(KERN_DEBUG "%s: updated supp_rates set "
2504 "for %s based on beacon info (0x%llx | "
2505 "0x%llx -> 0x%llx)\n",
2506 sdata->dev->name, print_mac(mac, sta->addr),
2507 (unsigned long long) prev_rates,
2508 (unsigned long long) supp_rates,
2509 (unsigned long long) sta->supp_rates[band]);
2510#endif
2511 } else {
2512 ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
2513 mgmt->sa, supp_rates);
Jiri Bencf0706e82007-05-05 11:45:53 -07002514 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002515
Johannes Berg69e6c012008-09-08 11:05:08 +02002516 rcu_read_unlock();
2517 }
Johannes Bergd0709a62008-02-25 16:27:46 +01002518
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002519#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002520 if (elems->mesh_config)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002521 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002522 elems->mesh_id_len, elems->mesh_config, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002523 else
2524#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002525 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002526 elems->ssid, elems->ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002527 if (!bss) {
2528#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002529 if (elems->mesh_config)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002530 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002531 elems->mesh_id_len, elems->mesh_config,
2532 elems->mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002533 else
2534#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002535 bss = ieee80211_rx_bss_add(sdata, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002536 elems->ssid, elems->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002537 if (!bss)
2538 return;
2539 } else {
2540#if 0
2541 /* TODO: order by RSSI? */
2542 spin_lock_bh(&local->sta_bss_lock);
2543 list_move_tail(&bss->list, &local->sta_bss_list);
2544 spin_unlock_bh(&local->sta_bss_lock);
2545#endif
2546 }
2547
Daniel Drake56282212007-07-10 19:32:10 +02002548 /* save the ERP value so that it is available at association time */
Ester Kummerae6a44e2008-06-27 18:54:48 +03002549 if (elems->erp_info && elems->erp_info_len >= 1) {
2550 bss->erp_value = elems->erp_info[0];
Daniel Drake56282212007-07-10 19:32:10 +02002551 bss->has_erp_value = 1;
2552 }
2553
Jiri Bencf0706e82007-05-05 11:45:53 -07002554 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2555 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002556
Emmanuel Grumbach98f7dfd2008-07-18 13:52:59 +08002557 if (elems->tim) {
2558 struct ieee80211_tim_ie *tim_ie =
2559 (struct ieee80211_tim_ie *)elems->tim;
2560 bss->dtim_period = tim_ie->dtim_period;
2561 }
2562
2563 /* set default value for buggy APs */
2564 if (!elems->tim || bss->dtim_period == 0)
2565 bss->dtim_period = 1;
2566
Jiri Bencf0706e82007-05-05 11:45:53 -07002567 bss->supp_rates_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002568 if (elems->supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002569 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002570 if (clen > elems->supp_rates_len)
2571 clen = elems->supp_rates_len;
2572 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
Jiri Bencf0706e82007-05-05 11:45:53 -07002573 clen);
2574 bss->supp_rates_len += clen;
2575 }
Ester Kummerae6a44e2008-06-27 18:54:48 +03002576 if (elems->ext_supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002577 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002578 if (clen > elems->ext_supp_rates_len)
2579 clen = elems->ext_supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07002580 memcpy(&bss->supp_rates[bss->supp_rates_len],
Ester Kummerae6a44e2008-06-27 18:54:48 +03002581 elems->ext_supp_rates, clen);
Jiri Bencf0706e82007-05-05 11:45:53 -07002582 bss->supp_rates_len += clen;
2583 }
2584
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002585 bss->band = band;
Johannes Berg30b89b02008-04-16 17:43:20 +02002586
Johannes Berg5bda6172008-09-08 11:05:10 +02002587 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2588
Johannes Berg30b89b02008-04-16 17:43:20 +02002589 bss->timestamp = beacon_timestamp;
2590 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002591 bss->signal = rx_status->signal;
2592 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002593 bss->qual = rx_status->qual;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002594 if (!beacon)
2595 bss->last_probe_resp = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002596 /*
2597 * In STA mode, the remaining parameters should not be overridden
2598 * by beacons because they're not necessarily accurate there.
2599 */
2600 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002601 bss->last_probe_resp && beacon) {
Johannes Berg3e122be2008-07-09 14:40:34 +02002602 ieee80211_rx_bss_put(local, bss);
Johannes Berg30b89b02008-04-16 17:43:20 +02002603 return;
2604 }
2605
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03002606 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
2607 kfree(bss->ies);
2608 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002609 }
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03002610 if (bss->ies) {
2611 memcpy(bss->ies, elems->ie_start, elems->total_len);
2612 bss->ies_len = elems->total_len;
2613 } else
2614 bss->ies_len = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002615
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03002616 bss->wmm_used = elems->wmm_param || elems->wmm_info;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002617
2618 /* check if we need to merge IBSS */
2619 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2620 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002621 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002622 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03002623 elems->ssid_len == sdata->u.sta.ssid_len &&
2624 memcmp(elems->ssid, sdata->u.sta.ssid,
2625 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002626 if (rx_status->flag & RX_FLAG_TSFT) {
2627 /* in order for correct IBSS merging we need mactime
2628 *
2629 * since mactime is defined as the time the first data
2630 * symbol of the frame hits the PHY, and the timestamp
2631 * of the beacon is defined as "the time that the data
2632 * symbol containing the first bit of the timestamp is
2633 * transmitted to the PHY plus the transmitting STA’s
2634 * delays through its local PHY from the MAC-PHY
2635 * interface to its interface with the WM"
2636 * (802.11 11.1.2) - equals the time this bit arrives at
2637 * the receiver - we have to take into account the
2638 * offset between the two.
2639 * e.g: at 1 MBit that means mactime is 192 usec earlier
2640 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2641 */
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002642 int rate = local->hw.wiphy->bands[band]->
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002643 bitrates[rx_status->rate_idx].bitrate;
2644 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2645 } else if (local && local->ops && local->ops->get_tsf)
2646 /* second best option: get current TSF */
2647 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2648 else
2649 /* can't merge without knowing the TSF */
2650 rx_timestamp = -1LLU;
2651#ifdef CONFIG_MAC80211_IBSS_DEBUG
2652 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2653 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2654 print_mac(mac, mgmt->sa),
2655 print_mac(mac2, mgmt->bssid),
2656 (unsigned long long)rx_timestamp,
2657 (unsigned long long)beacon_timestamp,
2658 (unsigned long long)(rx_timestamp - beacon_timestamp),
2659 jiffies);
2660#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2661 if (beacon_timestamp > rx_timestamp) {
John W. Linville576fdea2008-08-26 20:33:34 -04002662#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002663 printk(KERN_DEBUG "%s: beacon TSF higher than "
2664 "local TSF - IBSS merge with BSSID %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002665 sdata->dev->name, print_mac(mac, mgmt->bssid));
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002666#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002667 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
2668 ieee80211_ibss_add_sta(sdata, NULL,
Vladimir Koutny87291c02008-06-13 16:50:44 +02002669 mgmt->bssid, mgmt->sa,
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002670 supp_rates);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002671 }
2672 }
2673
Johannes Berg3e122be2008-07-09 14:40:34 +02002674 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002675}
2676
2677
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002678static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002679 struct ieee80211_mgmt *mgmt,
2680 size_t len,
2681 struct ieee80211_rx_status *rx_status)
2682{
Ester Kummerae6a44e2008-06-27 18:54:48 +03002683 size_t baselen;
2684 struct ieee802_11_elems elems;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002685 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002686
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002687 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
2688 return; /* ignore ProbeResp to foreign address */
2689
Ester Kummerae6a44e2008-06-27 18:54:48 +03002690 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2691 if (baselen > len)
2692 return;
2693
2694 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2695 &elems);
2696
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002697 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2698
2699 /* direct probe may be part of the association flow */
2700 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
2701 &ifsta->request)) {
2702 printk(KERN_DEBUG "%s direct probe responded\n",
2703 sdata->dev->name);
2704 ieee80211_authenticate(sdata, ifsta);
2705 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002706}
2707
2708
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002709static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002710 struct ieee80211_mgmt *mgmt,
2711 size_t len,
2712 struct ieee80211_rx_status *rx_status)
2713{
Jiri Bencf0706e82007-05-05 11:45:53 -07002714 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002715 size_t baselen;
2716 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002717 struct ieee80211_local *local = sdata->local;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002718 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002719 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002720
Ester Kummerae6a44e2008-06-27 18:54:48 +03002721 /* Process beacon from the current BSS */
2722 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2723 if (baselen > len)
2724 return;
2725
2726 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2727
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002728 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002729
Johannes Berg51fb61e2007-12-19 01:31:27 +01002730 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002731 return;
2732 ifsta = &sdata->u.sta;
2733
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002734 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002735 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2736 return;
2737
Reinette Chatred18ef292008-04-09 16:56:15 -07002738 /* Do not send changes to driver if we are scanning. This removes
Johannes Bergfe3fa822008-09-08 11:05:09 +02002739 * requirement that a driver's bss_info_changed/conf_tx functions
2740 * need to be atomic.
2741 * This is really ugly code, we should rewrite scanning and make
2742 * all this more understandable for humans.
2743 */
Reinette Chatred18ef292008-04-09 16:56:15 -07002744 if (local->sta_sw_scanning || local->sta_hw_scanning)
2745 return;
2746
Johannes Bergfe3fa822008-09-08 11:05:09 +02002747 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2748 elems.wmm_param_len);
2749
Daniel Drake56282212007-07-10 19:32:10 +02002750 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002751 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002752 else {
2753 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2754 changed |= ieee80211_handle_protect_preamb(sdata, false,
2755 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2756 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002757
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002758 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002759 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002760 struct ieee80211_ht_bss_info bss_info;
2761
2762 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2763 (struct ieee80211_ht_addt_info *)
2764 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002765 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2766 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002767 }
2768
Johannes Berg471b3ef2007-12-28 14:32:58 +01002769 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002770}
2771
2772
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002773static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002774 struct ieee80211_if_sta *ifsta,
2775 struct ieee80211_mgmt *mgmt,
2776 size_t len,
2777 struct ieee80211_rx_status *rx_status)
2778{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002779 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002780 int tx_last_beacon;
2781 struct sk_buff *skb;
2782 struct ieee80211_mgmt *resp;
2783 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002784 DECLARE_MAC_BUF(mac);
2785#ifdef CONFIG_MAC80211_IBSS_DEBUG
2786 DECLARE_MAC_BUF(mac2);
2787 DECLARE_MAC_BUF(mac3);
2788#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002789
Johannes Berg51fb61e2007-12-19 01:31:27 +01002790 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002791 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002792 len < 24 + 2 || !ifsta->probe_resp)
2793 return;
2794
2795 if (local->ops->tx_last_beacon)
2796 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2797 else
2798 tx_last_beacon = 1;
2799
2800#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002801 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2802 "%s (tx_last_beacon=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002803 sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
Joe Perches0795af52007-10-03 17:59:30 -07002804 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002805#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2806
2807 if (!tx_last_beacon)
2808 return;
2809
2810 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2811 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2812 return;
2813
2814 end = ((u8 *) mgmt) + len;
2815 pos = mgmt->u.probe_req.variable;
2816 if (pos[0] != WLAN_EID_SSID ||
2817 pos + 2 + pos[1] > end) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002818#ifdef CONFIG_MAC80211_IBSS_DEBUG
2819 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2820 "from %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002821 sdata->dev->name, print_mac(mac, mgmt->sa));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002822#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002823 return;
2824 }
2825 if (pos[1] != 0 &&
2826 (pos[1] != ifsta->ssid_len ||
2827 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2828 /* Ignore ProbeReq for foreign SSID */
2829 return;
2830 }
2831
2832 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002833 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002834 if (!skb)
2835 return;
2836
2837 resp = (struct ieee80211_mgmt *) skb->data;
2838 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2839#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002840 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002841 sdata->dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002842#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002843 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002844}
2845
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002846static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
Johannes Berg4e20cb22007-12-19 01:31:24 +01002847 struct ieee80211_if_sta *ifsta,
2848 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002849 size_t len,
2850 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002851{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002852 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002853
Johannes Berg9c80d3d2008-09-08 15:41:59 +02002854 /* all categories we currently handle have action_code */
2855 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002856 return;
2857
2858 switch (mgmt->u.action.category) {
Assaf Kraussb6623482008-06-16 16:09:49 +03002859 case WLAN_CATEGORY_SPECTRUM_MGMT:
2860 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2861 break;
Johannes Berg5fd12d42008-09-08 16:31:48 +02002862 switch (mgmt->u.action.u.measurement.action_code) {
Assaf Kraussb6623482008-06-16 16:09:49 +03002863 case WLAN_ACTION_SPCT_MSR_REQ:
2864 if (len < (IEEE80211_MIN_ACTION_SIZE +
2865 sizeof(mgmt->u.action.u.measurement)))
2866 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002867 ieee80211_sta_process_measurement_req(sdata, mgmt, len);
Assaf Kraussb6623482008-06-16 16:09:49 +03002868 break;
2869 }
2870 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002871 case WLAN_CATEGORY_BACK:
2872 switch (mgmt->u.action.u.addba_req.action_code) {
2873 case WLAN_ACTION_ADDBA_REQ:
2874 if (len < (IEEE80211_MIN_ACTION_SIZE +
2875 sizeof(mgmt->u.action.u.addba_req)))
2876 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002877 ieee80211_sta_process_addba_request(local, mgmt, len);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002878 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002879 case WLAN_ACTION_ADDBA_RESP:
2880 if (len < (IEEE80211_MIN_ACTION_SIZE +
2881 sizeof(mgmt->u.action.u.addba_resp)))
2882 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002883 ieee80211_sta_process_addba_resp(local, mgmt, len);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002884 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002885 case WLAN_ACTION_DELBA:
2886 if (len < (IEEE80211_MIN_ACTION_SIZE +
2887 sizeof(mgmt->u.action.u.delba)))
2888 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002889 ieee80211_sta_process_delba(sdata, mgmt, len);
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002890 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002891 }
2892 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002893 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002894 if (ieee80211_vif_is_mesh(&sdata->vif))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002895 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002896 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002897 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002898 if (ieee80211_vif_is_mesh(&sdata->vif))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002899 mesh_rx_path_sel_frame(sdata, mgmt, len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002900 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002901 }
2902}
Jiri Bencf0706e82007-05-05 11:45:53 -07002903
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002904void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
Jiri Bencf0706e82007-05-05 11:45:53 -07002905 struct ieee80211_rx_status *rx_status)
2906{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002907 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002908 struct ieee80211_if_sta *ifsta;
2909 struct ieee80211_mgmt *mgmt;
2910 u16 fc;
2911
2912 if (skb->len < 24)
2913 goto fail;
2914
Jiri Bencf0706e82007-05-05 11:45:53 -07002915 ifsta = &sdata->u.sta;
2916
2917 mgmt = (struct ieee80211_mgmt *) skb->data;
2918 fc = le16_to_cpu(mgmt->frame_control);
2919
2920 switch (fc & IEEE80211_FCTL_STYPE) {
2921 case IEEE80211_STYPE_PROBE_REQ:
2922 case IEEE80211_STYPE_PROBE_RESP:
2923 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002924 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002925 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2926 case IEEE80211_STYPE_AUTH:
2927 case IEEE80211_STYPE_ASSOC_RESP:
2928 case IEEE80211_STYPE_REASSOC_RESP:
2929 case IEEE80211_STYPE_DEAUTH:
2930 case IEEE80211_STYPE_DISASSOC:
2931 skb_queue_tail(&ifsta->skb_queue, skb);
2932 queue_work(local->hw.workqueue, &ifsta->work);
2933 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002934 }
2935
2936 fail:
2937 kfree_skb(skb);
2938}
2939
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002940static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002941 struct sk_buff *skb)
2942{
2943 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002944 struct ieee80211_if_sta *ifsta;
2945 struct ieee80211_mgmt *mgmt;
2946 u16 fc;
2947
Jiri Bencf0706e82007-05-05 11:45:53 -07002948 ifsta = &sdata->u.sta;
2949
2950 rx_status = (struct ieee80211_rx_status *) skb->cb;
2951 mgmt = (struct ieee80211_mgmt *) skb->data;
2952 fc = le16_to_cpu(mgmt->frame_control);
2953
2954 switch (fc & IEEE80211_FCTL_STYPE) {
2955 case IEEE80211_STYPE_PROBE_REQ:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002956 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002957 rx_status);
2958 break;
2959 case IEEE80211_STYPE_PROBE_RESP:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002960 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
Jiri Bencf0706e82007-05-05 11:45:53 -07002961 break;
2962 case IEEE80211_STYPE_BEACON:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002963 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Jiri Bencf0706e82007-05-05 11:45:53 -07002964 break;
2965 case IEEE80211_STYPE_AUTH:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002966 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002967 break;
2968 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002969 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002970 break;
2971 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002972 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002973 break;
2974 case IEEE80211_STYPE_DEAUTH:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002975 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002976 break;
2977 case IEEE80211_STYPE_DISASSOC:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002978 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002979 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002980 case IEEE80211_STYPE_ACTION:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002981 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002982 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07002983 }
2984
2985 kfree_skb(skb);
2986}
2987
2988
Johannes Berg9ae54c82008-01-31 19:48:20 +01002989ieee80211_rx_result
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002990ieee80211_sta_rx_scan(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
Zhu Yiece8edd2007-11-22 10:53:21 +08002991 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002992{
2993 struct ieee80211_mgmt *mgmt;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07002994 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002995
Zhu Yiece8edd2007-11-22 10:53:21 +08002996 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002997 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002998
2999 mgmt = (struct ieee80211_mgmt *) skb->data;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003000 fc = mgmt->frame_control;
Jiri Bencf0706e82007-05-05 11:45:53 -07003001
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003002 if (ieee80211_is_ctl(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003003 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003004
3005 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003006 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003007
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003008 if (ieee80211_is_probe_resp(fc)) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003009 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003010 dev_kfree_skb(skb);
3011 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003012 }
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003013
3014 if (ieee80211_is_beacon(fc)) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003015 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003016 dev_kfree_skb(skb);
3017 return RX_QUEUED;
3018 }
3019
Johannes Berg9ae54c82008-01-31 19:48:20 +01003020 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003021}
3022
3023
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003024static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -07003025{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003026 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003027 int active = 0;
3028 struct sta_info *sta;
3029
Johannes Bergd0709a62008-02-25 16:27:46 +01003030 rcu_read_lock();
3031
3032 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3033 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003034 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3035 jiffies)) {
3036 active++;
3037 break;
3038 }
3039 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003040
3041 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003042
3043 return active;
3044}
3045
3046
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003047static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003048{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003049 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003050 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003051 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003052 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003053 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003054
Johannes Bergd0709a62008-02-25 16:27:46 +01003055 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003056 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003057 if (time_after(jiffies, sta->last_rx + exp_time)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003058#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003059 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003060 sdata->dev->name, print_mac(mac, sta->addr));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003061#endif
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003062 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003063 if (sta)
3064 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003065 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003066 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003067
Johannes Bergd0709a62008-02-25 16:27:46 +01003068 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3069 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003070}
3071
3072
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003073static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003074 struct ieee80211_if_sta *ifsta)
3075{
3076 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3077
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003078 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
3079 if (ieee80211_sta_active_ibss(sdata))
Jiri Bencf0706e82007-05-05 11:45:53 -07003080 return;
3081
3082 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003083 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
3084 ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003085}
3086
3087
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003088#ifdef CONFIG_MAC80211_MESH
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003089static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003090 struct ieee80211_if_sta *ifsta)
3091{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003092 bool free_plinks;
3093
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003094 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3095 mesh_path_expire(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003096
3097 free_plinks = mesh_plink_availables(sdata);
3098 if (free_plinks != sdata->u.sta.accepting_plinks)
Johannes Berg9d139c82008-07-09 14:40:37 +02003099 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003100
3101 mod_timer(&ifsta->timer, jiffies +
3102 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3103}
3104
3105
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003106void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003107{
3108 struct ieee80211_if_sta *ifsta;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003109 ifsta = &sdata->u.sta;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003110 ifsta->state = IEEE80211_STA_MLME_MESH_UP;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003111 ieee80211_sta_timer((unsigned long)sdata);
Luis Carlos Cobo56a6d132008-07-29 19:59:31 +02003112 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003113}
3114#endif
3115
3116
Jiri Bencf0706e82007-05-05 11:45:53 -07003117void ieee80211_sta_timer(unsigned long data)
3118{
3119 struct ieee80211_sub_if_data *sdata =
3120 (struct ieee80211_sub_if_data *) data;
3121 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003122 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003123
3124 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3125 queue_work(local->hw.workqueue, &ifsta->work);
3126}
3127
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003128static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003129 struct ieee80211_if_sta *ifsta)
3130{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003131 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003132
3133 if (local->ops->reset_tsf) {
3134 /* Reset own TSF to allow time synchronization work. */
3135 local->ops->reset_tsf(local_to_hw(local));
3136 }
3137
3138 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3139
3140
3141 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3142 ifsta->auth_alg = WLAN_AUTH_OPEN;
3143 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3144 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3145 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3146 ifsta->auth_alg = WLAN_AUTH_LEAP;
3147 else
3148 ifsta->auth_alg = WLAN_AUTH_OPEN;
Jiri Bencf0706e82007-05-05 11:45:53 -07003149 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003150 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +03003151 ifsta->assoc_scan_tries = 0;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003152 ifsta->direct_probe_tries = 0;
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +03003153 ifsta->auth_tries = 0;
3154 ifsta->assoc_tries = 0;
Tomas Winkler24e64622008-09-08 17:33:40 +02003155 netif_tx_stop_all_queues(sdata->dev);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003156 netif_carrier_off(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003157}
3158
3159
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003160void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003161 struct ieee80211_if_sta *ifsta)
3162{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003163 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003164
Johannes Berg51fb61e2007-12-19 01:31:27 +01003165 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003166 return;
3167
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003168 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
Tomas Winkler3b7ee692008-09-08 17:33:38 +02003169 IEEE80211_STA_AUTO_BSSID_SEL)) &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003170 (ifsta->flags & (IEEE80211_STA_SSID_SET |
Tomas Winkler3b7ee692008-09-08 17:33:38 +02003171 IEEE80211_STA_AUTO_SSID_SEL))) {
3172
3173 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
3174 ieee80211_set_disassoc(sdata, ifsta, true, true,
3175 WLAN_REASON_DEAUTH_LEAVING);
3176
Jiri Bencf0706e82007-05-05 11:45:53 -07003177 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3178 queue_work(local->hw.workqueue, &ifsta->work);
3179 }
3180}
3181
3182static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3183 const char *ssid, int ssid_len)
3184{
3185 int tmp, hidden_ssid;
3186
Michael Wu48225702007-10-19 17:14:36 -04003187 if (ssid_len == ifsta->ssid_len &&
3188 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003189 return 1;
3190
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003191 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003192 return 0;
3193
3194 hidden_ssid = 1;
3195 tmp = ssid_len;
3196 while (tmp--) {
3197 if (ssid[tmp] != '\0') {
3198 hidden_ssid = 0;
3199 break;
3200 }
3201 }
3202
3203 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3204 return 1;
3205
3206 if (ssid_len == 1 && ssid[0] == ' ')
3207 return 1;
3208
3209 return 0;
3210}
3211
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003212static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003213 struct ieee80211_if_sta *ifsta)
3214{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003215 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003216 struct ieee80211_sta_bss *bss;
Johannes Berg8318d782008-01-24 19:38:38 +01003217 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003218 u8 bssid[ETH_ALEN], *pos;
3219 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003220 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003221 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003222
3223#if 0
3224 /* Easier testing, use fixed BSSID. */
3225 memset(bssid, 0xfe, ETH_ALEN);
3226#else
3227 /* Generate random, not broadcast, locally administered BSSID. Mix in
3228 * own MAC address to make sure that devices that do not have proper
3229 * random number generator get different BSSID. */
3230 get_random_bytes(bssid, ETH_ALEN);
3231 for (i = 0; i < ETH_ALEN; i++)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003232 bssid[i] ^= sdata->dev->dev_addr[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07003233 bssid[0] &= ~0x01;
3234 bssid[0] |= 0x02;
3235#endif
3236
Joe Perches0795af52007-10-03 17:59:30 -07003237 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003238 sdata->dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003239
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003240 bss = ieee80211_rx_bss_add(sdata, bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01003241 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003242 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003243 if (!bss)
3244 return -ENOMEM;
3245
Johannes Berg8318d782008-01-24 19:38:38 +01003246 bss->band = local->hw.conf.channel->band;
3247 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003248
3249 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03003250 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -07003251 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003252 bss->last_update = jiffies;
3253 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003254
3255 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003256 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003257 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003258 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003259
Johannes Berg8318d782008-01-24 19:38:38 +01003260 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003261 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003262 for (i = 0; i < sband->n_bitrates; i++) {
3263 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003264 *pos++ = (u8) (rate / 5);
3265 }
3266
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003267 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003268 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003269 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003270}
3271
3272
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003273static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003274 struct ieee80211_if_sta *ifsta)
3275{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003276 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003277 struct ieee80211_sta_bss *bss;
3278 int found = 0;
3279 u8 bssid[ETH_ALEN];
3280 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003281 DECLARE_MAC_BUF(mac);
3282 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003283
3284 if (ifsta->ssid_len == 0)
3285 return -EINVAL;
3286
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003287 active_ibss = ieee80211_sta_active_ibss(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003288#ifdef CONFIG_MAC80211_IBSS_DEBUG
3289 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003290 sdata->dev->name, active_ibss);
Jiri Bencf0706e82007-05-05 11:45:53 -07003291#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3292 spin_lock_bh(&local->sta_bss_lock);
3293 list_for_each_entry(bss, &local->sta_bss_list, list) {
3294 if (ifsta->ssid_len != bss->ssid_len ||
3295 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3296 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3297 continue;
3298#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003299 printk(KERN_DEBUG " bssid=%s found\n",
3300 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003301#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3302 memcpy(bssid, bss->bssid, ETH_ALEN);
3303 found = 1;
3304 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3305 break;
3306 }
3307 spin_unlock_bh(&local->sta_bss_lock);
3308
3309#ifdef CONFIG_MAC80211_IBSS_DEBUG
Vladimir Koutny6e438292008-07-07 14:23:01 +02003310 if (found)
3311 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3312 "%s\n", print_mac(mac, bssid),
3313 print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003314#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Daniel Drake80693ce2008-07-19 23:31:17 +01003315
3316 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003317 int ret;
Daniel Drake80693ce2008-07-19 23:31:17 +01003318 int search_freq;
3319
3320 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3321 search_freq = bss->freq;
3322 else
3323 search_freq = local->hw.conf.channel->center_freq;
3324
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003325 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
Daniel Drake80693ce2008-07-19 23:31:17 +01003326 ifsta->ssid, ifsta->ssid_len);
3327 if (!bss)
3328 goto dont_join;
3329
Joe Perches0795af52007-10-03 17:59:30 -07003330 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003331 " based on configured SSID\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003332 sdata->dev->name, print_mac(mac, bssid));
3333 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003334 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003335 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003336 }
Daniel Drake80693ce2008-07-19 23:31:17 +01003337
3338dont_join:
Jiri Bencf0706e82007-05-05 11:45:53 -07003339#ifdef CONFIG_MAC80211_IBSS_DEBUG
3340 printk(KERN_DEBUG " did not try to join ibss\n");
3341#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3342
3343 /* Selected IBSS not found in current scan results - try to scan */
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003344 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003345 !ieee80211_sta_active_ibss(sdata)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003346 mod_timer(&ifsta->timer, jiffies +
3347 IEEE80211_IBSS_MERGE_INTERVAL);
3348 } else if (time_after(jiffies, local->last_scan_completed +
3349 IEEE80211_SCAN_INTERVAL)) {
3350 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003351 "join\n", sdata->dev->name);
3352 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
Jiri Bencf0706e82007-05-05 11:45:53 -07003353 ifsta->ssid_len);
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003354 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003355 int interval = IEEE80211_SCAN_INTERVAL;
3356
3357 if (time_after(jiffies, ifsta->ibss_join_req +
3358 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003359 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003360 (!(local->oper_channel->flags &
3361 IEEE80211_CHAN_NO_IBSS)))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003362 return ieee80211_sta_create_ibss(sdata, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003363 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003364 printk(KERN_DEBUG "%s: IBSS not allowed on"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003365 " %d MHz\n", sdata->dev->name,
Johannes Berg8318d782008-01-24 19:38:38 +01003366 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003367 }
3368
3369 /* No IBSS found - decrease scan interval and continue
3370 * scanning. */
3371 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3372 }
3373
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003374 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
Jiri Bencf0706e82007-05-05 11:45:53 -07003375 mod_timer(&ifsta->timer, jiffies + interval);
3376 return 0;
3377 }
3378
3379 return 0;
3380}
3381
3382
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003383int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003384{
Jiri Bencf0706e82007-05-05 11:45:53 -07003385 struct ieee80211_if_sta *ifsta;
Johannes Berg9d139c82008-07-09 14:40:37 +02003386 int res;
Jiri Bencf0706e82007-05-05 11:45:53 -07003387
3388 if (len > IEEE80211_MAX_SSID_LEN)
3389 return -EINVAL;
3390
Jiri Bencf0706e82007-05-05 11:45:53 -07003391 ifsta = &sdata->u.sta;
3392
Johannes Berg9d139c82008-07-09 14:40:37 +02003393 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3394 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3395 memcpy(ifsta->ssid, ssid, len);
3396 ifsta->ssid_len = len;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003397 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Johannes Berg9d139c82008-07-09 14:40:37 +02003398
3399 res = 0;
3400 /*
3401 * Hack! MLME code needs to be cleaned up to have different
3402 * entry points for configuration and internal selection change
3403 */
3404 if (netif_running(sdata->dev))
3405 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3406 if (res) {
3407 printk(KERN_DEBUG "%s: Failed to config new SSID to "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003408 "the low-level driver\n", sdata->dev->name);
Johannes Berg9d139c82008-07-09 14:40:37 +02003409 return res;
3410 }
3411 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003412
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003413 if (len)
3414 ifsta->flags |= IEEE80211_STA_SSID_SET;
3415 else
3416 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg9d139c82008-07-09 14:40:37 +02003417
Johannes Berg51fb61e2007-12-19 01:31:27 +01003418 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003419 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003420 ifsta->ibss_join_req = jiffies;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003421 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003422 return ieee80211_sta_find_ibss(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003423 }
Johannes Berg9d139c82008-07-09 14:40:37 +02003424
Jiri Bencf0706e82007-05-05 11:45:53 -07003425 return 0;
3426}
3427
3428
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003429int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003430{
Jiri Bencf0706e82007-05-05 11:45:53 -07003431 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3432 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3433 *len = ifsta->ssid_len;
3434 return 0;
3435}
3436
3437
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003438int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
Jiri Bencf0706e82007-05-05 11:45:53 -07003439{
Jiri Bencf0706e82007-05-05 11:45:53 -07003440 struct ieee80211_if_sta *ifsta;
3441 int res;
3442
Jiri Bencf0706e82007-05-05 11:45:53 -07003443 ifsta = &sdata->u.sta;
3444
3445 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3446 memcpy(ifsta->bssid, bssid, ETH_ALEN);
Johannes Berg9d139c82008-07-09 14:40:37 +02003447 res = 0;
3448 /*
3449 * Hack! See also ieee80211_sta_set_ssid.
3450 */
3451 if (netif_running(sdata->dev))
3452 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Jiri Bencf0706e82007-05-05 11:45:53 -07003453 if (res) {
3454 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003455 "the low-level driver\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07003456 return res;
3457 }
3458 }
3459
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003460 if (is_valid_ether_addr(bssid))
3461 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003462 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003463 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3464
Jiri Bencf0706e82007-05-05 11:45:53 -07003465 return 0;
3466}
3467
3468
3469static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3470 struct ieee80211_sub_if_data *sdata,
3471 int powersave)
3472{
3473 struct sk_buff *skb;
3474 struct ieee80211_hdr *nullfunc;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003475 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003476
3477 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3478 if (!skb) {
3479 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3480 "frame\n", sdata->dev->name);
3481 return;
3482 }
3483 skb_reserve(skb, local->hw.extra_tx_headroom);
3484
3485 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3486 memset(nullfunc, 0, 24);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003487 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3488 IEEE80211_FCTL_TODS);
Jiri Bencf0706e82007-05-05 11:45:53 -07003489 if (powersave)
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003490 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3491 nullfunc->frame_control = fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003492 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3493 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3494 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3495
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003496 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003497}
3498
3499
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003500static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3501{
3502 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3503 ieee80211_vif_is_mesh(&sdata->vif))
3504 ieee80211_sta_timer((unsigned long)sdata);
3505}
3506
Jiri Bencf0706e82007-05-05 11:45:53 -07003507void ieee80211_scan_completed(struct ieee80211_hw *hw)
3508{
3509 struct ieee80211_local *local = hw_to_local(hw);
3510 struct net_device *dev = local->scan_dev;
3511 struct ieee80211_sub_if_data *sdata;
3512 union iwreq_data wrqu;
3513
3514 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003515 memset(&wrqu, 0, sizeof(wrqu));
3516 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003517
Zhu Yiece8edd2007-11-22 10:53:21 +08003518 if (local->sta_hw_scanning) {
3519 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003520 if (ieee80211_hw_config(local))
3521 printk(KERN_DEBUG "%s: failed to restore operational "
3522 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003523 /* Restart STA timer for HW scan case */
3524 rcu_read_lock();
3525 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3526 ieee80211_restart_sta_timer(sdata);
3527 rcu_read_unlock();
3528
Zhu Yiece8edd2007-11-22 10:53:21 +08003529 goto done;
3530 }
3531
3532 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003533 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003534 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003535 "channel after scan\n", dev->name);
3536
Johannes Berg4150c572007-09-17 01:29:23 -04003537
3538 netif_tx_lock_bh(local->mdev);
David S. Millere308a5d2008-07-15 00:13:44 -07003539 netif_addr_lock(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003540 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3541 local->ops->configure_filter(local_to_hw(local),
3542 FIF_BCN_PRBRESP_PROMISC,
3543 &local->filter_flags,
3544 local->mdev->mc_count,
3545 local->mdev->mc_list);
3546
David S. Millere308a5d2008-07-15 00:13:44 -07003547 netif_addr_unlock(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003548 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003549
Johannes Berg79010422007-09-18 17:29:21 -04003550 rcu_read_lock();
3551 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003552 /* Tell AP we're back */
Tomas Winkler24e64622008-09-08 17:33:40 +02003553 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3554 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
3555 ieee80211_send_nullfunc(local, sdata, 0);
3556 netif_tx_wake_all_queues(sdata->dev);
3557 }
3558 } else
3559 netif_tx_wake_all_queues(sdata->dev);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003560
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003561 ieee80211_restart_sta_timer(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003562 }
Johannes Berg79010422007-09-18 17:29:21 -04003563 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003564
Zhu Yiece8edd2007-11-22 10:53:21 +08003565done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003566 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003567 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003568 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003569 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003570 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003571 !ieee80211_sta_active_ibss(sdata)))
3572 ieee80211_sta_find_ibss(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003573 }
3574}
3575EXPORT_SYMBOL(ieee80211_scan_completed);
3576
3577void ieee80211_sta_scan_work(struct work_struct *work)
3578{
3579 struct ieee80211_local *local =
3580 container_of(work, struct ieee80211_local, scan_work.work);
3581 struct net_device *dev = local->scan_dev;
3582 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003583 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003584 struct ieee80211_channel *chan;
3585 int skip;
3586 unsigned long next_delay = 0;
3587
Zhu Yiece8edd2007-11-22 10:53:21 +08003588 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003589 return;
3590
3591 switch (local->scan_state) {
3592 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003593 /*
3594 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3595 * after we successfully scanned the last channel of the last
3596 * band (and the last band is supported by the hw)
3597 */
Johannes Berg8318d782008-01-24 19:38:38 +01003598 if (local->scan_band < IEEE80211_NUM_BANDS)
3599 sband = local->hw.wiphy->bands[local->scan_band];
3600 else
3601 sband = NULL;
3602
Johannes Berg69d464d2008-01-28 17:08:56 +01003603 /*
3604 * If we are at an unsupported band and have more bands
3605 * left to scan, advance to the next supported one.
3606 */
3607 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003608 local->scan_band++;
3609 sband = local->hw.wiphy->bands[local->scan_band];
3610 local->scan_channel_idx = 0;
3611 }
3612
Johannes Berg69d464d2008-01-28 17:08:56 +01003613 /* if no more bands/channels left, complete scan */
3614 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003615 ieee80211_scan_completed(local_to_hw(local));
3616 return;
3617 }
Johannes Berg8318d782008-01-24 19:38:38 +01003618 skip = 0;
3619 chan = &sband->channels[local->scan_channel_idx];
3620
3621 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003622 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003623 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003624 skip = 1;
3625
3626 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003627 local->scan_channel = chan;
3628 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003629 printk(KERN_DEBUG "%s: failed to set freq to "
3630 "%d MHz for scan\n", dev->name,
3631 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003632 skip = 1;
3633 }
3634 }
3635
Johannes Berg69d464d2008-01-28 17:08:56 +01003636 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003637 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003638 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003639 /*
3640 * scan_band may end up == IEEE80211_NUM_BANDS, but
3641 * we'll catch that case above and complete the scan
3642 * if that is the case.
3643 */
Johannes Berg8318d782008-01-24 19:38:38 +01003644 local->scan_band++;
3645 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003646 }
3647
3648 if (skip)
3649 break;
3650
3651 next_delay = IEEE80211_PROBE_DELAY +
3652 usecs_to_jiffies(local->hw.channel_change_time);
3653 local->scan_state = SCAN_SEND_PROBE;
3654 break;
3655 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003656 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003657 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003658
3659 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3660 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003661 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
Johannes Berg8318d782008-01-24 19:38:38 +01003662 local->scan_ssid_len);
3663 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003664 break;
3665 }
3666
Zhu Yiece8edd2007-11-22 10:53:21 +08003667 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003668 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3669 next_delay);
3670}
3671
3672
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003673static int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *scan_sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003674 u8 *ssid, size_t ssid_len)
3675{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003676 struct ieee80211_local *local = scan_sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003677 struct ieee80211_sub_if_data *sdata;
3678
3679 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3680 return -EINVAL;
3681
3682 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3683 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3684 * BSSID: MACAddress
3685 * SSID
3686 * ScanType: ACTIVE, PASSIVE
3687 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3688 * a Probe frame during active scanning
3689 * ChannelList
3690 * MinChannelTime (>= ProbeDelay), in TU
3691 * MaxChannelTime: (>= MinChannelTime), in TU
3692 */
3693
3694 /* MLME-SCAN.confirm
3695 * BSSDescriptionSet
3696 * ResultCode: SUCCESS, INVALID_PARAMETERS
3697 */
3698
Zhu Yiece8edd2007-11-22 10:53:21 +08003699 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003700 if (local->scan_dev == scan_sdata->dev)
Jiri Bencf0706e82007-05-05 11:45:53 -07003701 return 0;
3702 return -EBUSY;
3703 }
3704
3705 if (local->ops->hw_scan) {
3706 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003707 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003708 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003709 local->sta_hw_scanning = 1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003710 local->scan_dev = scan_sdata->dev;
Jiri Bencf0706e82007-05-05 11:45:53 -07003711 }
3712 return rc;
3713 }
3714
Zhu Yiece8edd2007-11-22 10:53:21 +08003715 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003716
Johannes Berg79010422007-09-18 17:29:21 -04003717 rcu_read_lock();
3718 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Tomas Winkler24e64622008-09-08 17:33:40 +02003719 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3720 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
3721 netif_tx_stop_all_queues(sdata->dev);
3722 ieee80211_send_nullfunc(local, sdata, 1);
3723 }
3724 } else
3725 netif_tx_stop_all_queues(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003726 }
Johannes Berg79010422007-09-18 17:29:21 -04003727 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003728
3729 if (ssid) {
3730 local->scan_ssid_len = ssid_len;
3731 memcpy(local->scan_ssid, ssid, ssid_len);
3732 } else
3733 local->scan_ssid_len = 0;
3734 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003735 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003736 local->scan_band = IEEE80211_BAND_2GHZ;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003737 local->scan_dev = scan_sdata->dev;
Jiri Bencf0706e82007-05-05 11:45:53 -07003738
David S. Millerb9e40852008-07-15 00:15:08 -07003739 netif_addr_lock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003740 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3741 local->ops->configure_filter(local_to_hw(local),
3742 FIF_BCN_PRBRESP_PROMISC,
3743 &local->filter_flags,
3744 local->mdev->mc_count,
3745 local->mdev->mc_list);
David S. Millerb9e40852008-07-15 00:15:08 -07003746 netif_addr_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003747
3748 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3749 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3750 IEEE80211_CHANNEL_TIME);
3751
3752 return 0;
3753}
3754
3755
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003756int ieee80211_sta_req_scan(struct ieee80211_sub_if_data *sdata, u8 *ssid, size_t ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003757{
Jiri Bencf0706e82007-05-05 11:45:53 -07003758 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003759 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003760
Johannes Berg51fb61e2007-12-19 01:31:27 +01003761 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003762 return ieee80211_sta_start_scan(sdata, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003763
Zhu Yiece8edd2007-11-22 10:53:21 +08003764 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003765 if (local->scan_dev == sdata->dev)
Jiri Bencf0706e82007-05-05 11:45:53 -07003766 return 0;
3767 return -EBUSY;
3768 }
3769
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003770 ifsta->scan_ssid_len = ssid_len;
3771 if (ssid_len)
3772 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003773 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3774 queue_work(local->hw.workqueue, &ifsta->work);
3775 return 0;
3776}
3777
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03003778
3779static void ieee80211_sta_add_scan_ies(struct iw_request_info *info,
3780 struct ieee80211_sta_bss *bss,
3781 char **current_ev, char *end_buf)
3782{
3783 u8 *pos, *end, *next;
3784 struct iw_event iwe;
3785
3786 if (bss == NULL || bss->ies == NULL)
3787 return;
3788
3789 /*
3790 * If needed, fragment the IEs buffer (at IE boundaries) into short
3791 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
3792 */
3793 pos = bss->ies;
3794 end = pos + bss->ies_len;
3795
3796 while (end - pos > IW_GENERIC_IE_MAX) {
3797 next = pos + 2 + pos[1];
3798 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
3799 next = next + 2 + next[1];
3800
3801 memset(&iwe, 0, sizeof(iwe));
3802 iwe.cmd = IWEVGENIE;
3803 iwe.u.data.length = next - pos;
3804 *current_ev = iwe_stream_add_point(info, *current_ev,
3805 end_buf, &iwe, pos);
3806
3807 pos = next;
3808 }
3809
3810 if (end > pos) {
3811 memset(&iwe, 0, sizeof(iwe));
3812 iwe.cmd = IWEVGENIE;
3813 iwe.u.data.length = end - pos;
3814 *current_ev = iwe_stream_add_point(info, *current_ev,
3815 end_buf, &iwe, pos);
3816 }
3817}
3818
3819
Jiri Bencf0706e82007-05-05 11:45:53 -07003820static char *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003821ieee80211_sta_scan_result(struct ieee80211_local *local,
David S. Millerccc58052008-06-16 18:50:49 -07003822 struct iw_request_info *info,
Jiri Bencf0706e82007-05-05 11:45:53 -07003823 struct ieee80211_sta_bss *bss,
3824 char *current_ev, char *end_buf)
3825{
Jiri Bencf0706e82007-05-05 11:45:53 -07003826 struct iw_event iwe;
3827
3828 if (time_after(jiffies,
3829 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3830 return current_ev;
3831
Jiri Bencf0706e82007-05-05 11:45:53 -07003832 memset(&iwe, 0, sizeof(iwe));
3833 iwe.cmd = SIOCGIWAP;
3834 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3835 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07003836 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07003837 IW_EV_ADDR_LEN);
3838
3839 memset(&iwe, 0, sizeof(iwe));
3840 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01003841 if (bss_mesh_cfg(bss)) {
3842 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003843 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07003844 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3845 &iwe, bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003846 } else {
3847 iwe.u.data.length = bss->ssid_len;
3848 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07003849 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3850 &iwe, bss->ssid);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003851 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003852
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08003853 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3854 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003855 memset(&iwe, 0, sizeof(iwe));
3856 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01003857 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003858 iwe.u.mode = IW_MODE_MESH;
3859 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07003860 iwe.u.mode = IW_MODE_MASTER;
3861 else
3862 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07003863 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
3864 &iwe, IW_EV_UINT_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07003865 }
3866
3867 memset(&iwe, 0, sizeof(iwe));
3868 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003869 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3870 iwe.u.freq.e = 0;
David S. Millerccc58052008-06-16 18:50:49 -07003871 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07003872 IW_EV_FREQ_LEN);
3873
3874 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03003875 iwe.cmd = SIOCGIWFREQ;
3876 iwe.u.freq.m = bss->freq;
3877 iwe.u.freq.e = 6;
David S. Millerccc58052008-06-16 18:50:49 -07003878 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Tomas Winkler9381be02008-05-23 01:36:36 +03003879 IW_EV_FREQ_LEN);
3880 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e82007-05-05 11:45:53 -07003881 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003882 iwe.u.qual.qual = bss->qual;
3883 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07003884 iwe.u.qual.noise = bss->noise;
3885 iwe.u.qual.updated = local->wstats_flags;
David S. Millerccc58052008-06-16 18:50:49 -07003886 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07003887 IW_EV_QUAL_LEN);
3888
3889 memset(&iwe, 0, sizeof(iwe));
3890 iwe.cmd = SIOCGIWENCODE;
3891 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3892 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3893 else
3894 iwe.u.data.flags = IW_ENCODE_DISABLED;
3895 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07003896 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3897 &iwe, "");
Jiri Bencf0706e82007-05-05 11:45:53 -07003898
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03003899 ieee80211_sta_add_scan_ies(info, bss, &current_ev, end_buf);
Emmanuel Grumbache6231572008-05-27 20:00:11 +03003900
Jiri Bencf0706e82007-05-05 11:45:53 -07003901 if (bss && bss->supp_rates_len > 0) {
3902 /* display all supported rates in readable format */
David S. Millerccc58052008-06-16 18:50:49 -07003903 char *p = current_ev + iwe_stream_lcp_len(info);
Jiri Bencf0706e82007-05-05 11:45:53 -07003904 int i;
3905
3906 memset(&iwe, 0, sizeof(iwe));
3907 iwe.cmd = SIOCGIWRATE;
3908 /* Those two flags are ignored... */
3909 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3910
3911 for (i = 0; i < bss->supp_rates_len; i++) {
3912 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3913 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07003914 p = iwe_stream_add_value(info, current_ev, p,
Jiri Bencf0706e82007-05-05 11:45:53 -07003915 end_buf, &iwe, IW_EV_PARAM_LEN);
3916 }
3917 current_ev = p;
3918 }
3919
3920 if (bss) {
3921 char *buf;
3922 buf = kmalloc(30, GFP_ATOMIC);
3923 if (buf) {
3924 memset(&iwe, 0, sizeof(iwe));
3925 iwe.cmd = IWEVCUSTOM;
3926 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3927 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07003928 current_ev = iwe_stream_add_point(info, current_ev,
3929 end_buf,
Jiri Bencf0706e82007-05-05 11:45:53 -07003930 &iwe, buf);
Emmanuel Grumbachbf998f62008-06-24 13:38:00 +03003931 memset(&iwe, 0, sizeof(iwe));
3932 iwe.cmd = IWEVCUSTOM;
3933 sprintf(buf, " Last beacon: %dms ago",
3934 jiffies_to_msecs(jiffies - bss->last_update));
3935 iwe.u.data.length = strlen(buf);
3936 current_ev = iwe_stream_add_point(info, current_ev,
3937 end_buf, &iwe, buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07003938 kfree(buf);
3939 }
3940 }
3941
Johannes Berg902acc72008-02-23 15:17:19 +01003942 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003943 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04003944 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003945 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003946 if (buf) {
3947 memset(&iwe, 0, sizeof(iwe));
3948 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04003949 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003950 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07003951 current_ev = iwe_stream_add_point(info, current_ev,
3952 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003953 &iwe, buf);
3954 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04003955 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
3956 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003957 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07003958 current_ev = iwe_stream_add_point(info, current_ev,
3959 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003960 &iwe, buf);
3961 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04003962 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
3963 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003964 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07003965 current_ev = iwe_stream_add_point(info, current_ev,
3966 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003967 &iwe, buf);
3968 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04003969 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
3970 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003971 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07003972 current_ev = iwe_stream_add_point(info, current_ev,
3973 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08003974 &iwe, buf);
3975 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04003976 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
3977 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003978 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07003979 current_ev = iwe_stream_add_point(info, current_ev,
3980 end_buf,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003981 &iwe, buf);
3982 kfree(buf);
3983 }
3984 }
3985
Jiri Bencf0706e82007-05-05 11:45:53 -07003986 return current_ev;
3987}
3988
3989
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003990int ieee80211_sta_scan_results(struct ieee80211_local *local,
David S. Millerccc58052008-06-16 18:50:49 -07003991 struct iw_request_info *info,
3992 char *buf, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003993{
Jiri Bencf0706e82007-05-05 11:45:53 -07003994 char *current_ev = buf;
3995 char *end_buf = buf + len;
3996 struct ieee80211_sta_bss *bss;
3997
3998 spin_lock_bh(&local->sta_bss_lock);
3999 list_for_each_entry(bss, &local->sta_bss_list, list) {
4000 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4001 spin_unlock_bh(&local->sta_bss_lock);
4002 return -E2BIG;
4003 }
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004004 current_ev = ieee80211_sta_scan_result(local, info, bss,
David S. Millerccc58052008-06-16 18:50:49 -07004005 current_ev, end_buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004006 }
4007 spin_unlock_bh(&local->sta_bss_lock);
4008 return current_ev - buf;
4009}
4010
4011
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004012int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07004013{
Jiri Bencf0706e82007-05-05 11:45:53 -07004014 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004015
Jiri Bencf0706e82007-05-05 11:45:53 -07004016 kfree(ifsta->extra_ie);
4017 if (len == 0) {
4018 ifsta->extra_ie = NULL;
4019 ifsta->extra_ie_len = 0;
4020 return 0;
4021 }
4022 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4023 if (!ifsta->extra_ie) {
4024 ifsta->extra_ie_len = 0;
4025 return -ENOMEM;
4026 }
4027 memcpy(ifsta->extra_ie, ie, len);
4028 ifsta->extra_ie_len = len;
4029 return 0;
4030}
4031
4032
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004033struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
Johannes Berg988c0f72008-04-17 19:21:22 +02004034 struct sk_buff *skb, u8 *bssid,
Vladimir Koutny87291c02008-06-13 16:50:44 +02004035 u8 *addr, u64 supp_rates)
Jiri Bencf0706e82007-05-05 11:45:53 -07004036{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004037 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07004038 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -07004039 DECLARE_MAC_BUF(mac);
Vladimir Koutny87291c02008-06-13 16:50:44 +02004040 int band = local->hw.conf.channel->band;
Jiri Bencf0706e82007-05-05 11:45:53 -07004041
4042 /* TODO: Could consider removing the least recently used entry and
4043 * allow new one to be added. */
4044 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4045 if (net_ratelimit()) {
4046 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004047 "entry %s\n", sdata->dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004048 }
4049 return NULL;
4050 }
4051
Emmanuel Grumbach1e188632008-07-10 17:54:14 +03004052 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
Vladimir Koutny87291c02008-06-13 16:50:44 +02004053 return NULL;
4054
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004055#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07004056 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004057 wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004058#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07004059
Johannes Berg73651ee2008-02-25 16:27:47 +01004060 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4061 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004062 return NULL;
4063
Johannes Berg07346f812008-05-03 01:02:02 +02004064 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004065
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03004066 /* make sure mandatory rates are always added */
4067 sta->supp_rates[band] = supp_rates |
4068 ieee80211_sta_get_mandatory_rates(local, band);
Jiri Bencf0706e82007-05-05 11:45:53 -07004069
4070 rate_control_rate_init(sta, local);
4071
Johannes Berg93e5deb2008-04-01 15:21:00 +02004072 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004073 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004074
Johannes Bergd0709a62008-02-25 16:27:46 +01004075 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004076}
4077
4078
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004079static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
4080 struct ieee80211_if_sta *ifsta)
4081{
4082 struct ieee80211_local *local = sdata->local;
4083 struct ieee80211_sta_bss *bss, *selected = NULL;
4084 int top_rssi = 0, freq;
4085
4086 spin_lock_bh(&local->sta_bss_lock);
4087 freq = local->oper_channel->center_freq;
4088 list_for_each_entry(bss, &local->sta_bss_list, list) {
4089 if (!(bss->capability & WLAN_CAPABILITY_ESS))
4090 continue;
4091
4092 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
4093 IEEE80211_STA_AUTO_BSSID_SEL |
4094 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
4095 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
4096 !!sdata->default_key))
4097 continue;
4098
4099 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
4100 bss->freq != freq)
4101 continue;
4102
4103 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
4104 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
4105 continue;
4106
4107 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
4108 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
4109 continue;
4110
4111 if (!selected || top_rssi < bss->signal) {
4112 selected = bss;
4113 top_rssi = bss->signal;
4114 }
4115 }
4116 if (selected)
4117 atomic_inc(&selected->users);
4118 spin_unlock_bh(&local->sta_bss_lock);
4119
4120 if (selected) {
4121 ieee80211_set_freq(sdata, selected->freq);
4122 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
4123 ieee80211_sta_set_ssid(sdata, selected->ssid,
4124 selected->ssid_len);
4125 ieee80211_sta_set_bssid(sdata, selected->bssid);
4126 ieee80211_sta_def_wmm_params(sdata, selected, 0);
4127
4128 /* Send out direct probe if no probe resp was received or
4129 * the one we have is outdated
4130 */
4131 if (!selected->last_probe_resp ||
4132 time_after(jiffies, selected->last_probe_resp
4133 + IEEE80211_SCAN_RESULT_EXPIRE))
4134 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
4135 else
4136 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
4137
4138 ieee80211_rx_bss_put(local, selected);
4139 ieee80211_sta_reset_auth(sdata, ifsta);
4140 return 0;
4141 } else {
4142 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
4143 ifsta->assoc_scan_tries++;
4144 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
4145 ieee80211_sta_start_scan(sdata, NULL, 0);
4146 else
4147 ieee80211_sta_start_scan(sdata, ifsta->ssid,
4148 ifsta->ssid_len);
4149 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
4150 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
4151 } else
4152 ifsta->state = IEEE80211_STA_MLME_DISABLED;
4153 }
4154 return -1;
4155}
4156
4157
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004158int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
Jiri Bencf0706e82007-05-05 11:45:53 -07004159{
Jiri Bencf0706e82007-05-05 11:45:53 -07004160 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4161
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004162 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004163 sdata->dev->name, reason);
Jiri Bencf0706e82007-05-05 11:45:53 -07004164
Johannes Berg51fb61e2007-12-19 01:31:27 +01004165 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4166 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004167 return -EINVAL;
4168
Tomas Winkleraa458d12008-09-09 00:32:12 +03004169 ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
Jiri Bencf0706e82007-05-05 11:45:53 -07004170 return 0;
4171}
4172
4173
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004174int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
Jiri Bencf0706e82007-05-05 11:45:53 -07004175{
Jiri Bencf0706e82007-05-05 11:45:53 -07004176 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4177
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004178 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004179 sdata->dev->name, reason);
Jiri Bencf0706e82007-05-05 11:45:53 -07004180
Johannes Berg51fb61e2007-12-19 01:31:27 +01004181 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004182 return -EINVAL;
4183
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004184 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004185 return -1;
4186
Tomas Winkleraa458d12008-09-09 00:32:12 +03004187 ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
Jiri Bencf0706e82007-05-05 11:45:53 -07004188 return 0;
4189}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004190
4191void ieee80211_notify_mac(struct ieee80211_hw *hw,
4192 enum ieee80211_notification_types notif_type)
4193{
4194 struct ieee80211_local *local = hw_to_local(hw);
4195 struct ieee80211_sub_if_data *sdata;
4196
4197 switch (notif_type) {
4198 case IEEE80211_NOTIFY_RE_ASSOC:
4199 rcu_read_lock();
4200 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg3e122be2008-07-09 14:40:34 +02004201 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4202 continue;
Mohamed Abbas84363e62008-04-04 16:59:58 -07004203
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004204 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004205 }
4206 rcu_read_unlock();
4207 break;
4208 }
4209}
4210EXPORT_SYMBOL(ieee80211_notify_mac);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02004211
4212void ieee80211_sta_work(struct work_struct *work)
4213{
4214 struct ieee80211_sub_if_data *sdata =
4215 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
4216 struct ieee80211_local *local = sdata->local;
4217 struct ieee80211_if_sta *ifsta;
4218 struct sk_buff *skb;
4219
4220 if (!netif_running(sdata->dev))
4221 return;
4222
4223 if (local->sta_sw_scanning || local->sta_hw_scanning)
4224 return;
4225
4226 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4227 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
4228 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
4229 return;
4230 ifsta = &sdata->u.sta;
4231
4232 while ((skb = skb_dequeue(&ifsta->skb_queue)))
4233 ieee80211_sta_rx_queued_mgmt(sdata, skb);
4234
4235#ifdef CONFIG_MAC80211_MESH
4236 if (ifsta->preq_queue_len &&
4237 time_after(jiffies,
4238 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
4239 mesh_path_start_discovery(sdata);
4240#endif
4241
4242 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
4243 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
4244 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
4245 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
4246 if (ifsta->scan_ssid_len)
4247 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
4248 else
4249 ieee80211_sta_start_scan(sdata, NULL, 0);
4250 return;
4251 }
4252
4253 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
4254 if (ieee80211_sta_config_auth(sdata, ifsta))
4255 return;
4256 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
4257 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
4258 return;
4259
4260 switch (ifsta->state) {
4261 case IEEE80211_STA_MLME_DISABLED:
4262 break;
4263 case IEEE80211_STA_MLME_DIRECT_PROBE:
4264 ieee80211_direct_probe(sdata, ifsta);
4265 break;
4266 case IEEE80211_STA_MLME_AUTHENTICATE:
4267 ieee80211_authenticate(sdata, ifsta);
4268 break;
4269 case IEEE80211_STA_MLME_ASSOCIATE:
4270 ieee80211_associate(sdata, ifsta);
4271 break;
4272 case IEEE80211_STA_MLME_ASSOCIATED:
4273 ieee80211_associated(sdata, ifsta);
4274 break;
4275 case IEEE80211_STA_MLME_IBSS_SEARCH:
4276 ieee80211_sta_find_ibss(sdata, ifsta);
4277 break;
4278 case IEEE80211_STA_MLME_IBSS_JOINED:
4279 ieee80211_sta_merge_ibss(sdata, ifsta);
4280 break;
4281#ifdef CONFIG_MAC80211_MESH
4282 case IEEE80211_STA_MLME_MESH_UP:
4283 ieee80211_mesh_housekeeping(sdata, ifsta);
4284 break;
4285#endif
4286 default:
4287 WARN_ON(1);
4288 break;
4289 }
4290
4291 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
4292 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
4293 "mixed-cell disabled - disassociate\n", sdata->dev->name);
4294
4295 ieee80211_set_disassoc(sdata, ifsta, false, true,
4296 WLAN_REASON_UNSPECIFIED);
4297 }
4298}