blob: b688425d755547bebba0bca8b95d2958b3512901 [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
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
18#include <linux/wireless.h>
19#include <linux/random.h>
20#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010021#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070022#include <net/iw_handler.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070023#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020024#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020025
Jiri Bencf0706e82007-05-05 11:45:53 -070026#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040027#include "rate.h"
28#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070029
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +030030#define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
Jiri Bencf0706e82007-05-05 11:45:53 -070031#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
32#define IEEE80211_AUTH_MAX_TRIES 3
33#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
34#define IEEE80211_ASSOC_MAX_TRIES 3
35#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
36#define IEEE80211_PROBE_INTERVAL (60 * HZ)
37#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
38#define IEEE80211_SCAN_INTERVAL (2 * HZ)
39#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
Dan Williams872ba532008-06-04 13:59:34 -040040#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070041
Jiri Bencf0706e82007-05-05 11:45:53 -070042#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
43#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
44
45#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
46
47
Johannes Berg5484e232008-09-08 17:44:27 +020048/* utils */
49static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +020050{
Johannes Berg5484e232008-09-08 17:44:27 +020051 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +020052}
53
Johannes Bergc2b13452008-09-11 00:01:55 +020054static u8 *ieee80211_bss_get_ie(struct ieee80211_bss *bss, u8 ie)
Jouni Malinen43ac2ca2008-08-15 22:21:27 +030055{
56 u8 *end, *pos;
57
58 pos = bss->ies;
59 if (pos == NULL)
60 return NULL;
61 end = pos + bss->ies_len;
62
63 while (pos + 1 < end) {
64 if (pos + 2 + pos[1] > end)
65 break;
66 if (pos[0] == ie)
67 return pos;
68 pos += 2 + pos[1];
69 }
70
71 return NULL;
72}
73
Johannes Bergc2b13452008-09-11 00:01:55 +020074static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
Johannes Berg9ac19a92008-09-09 10:57:09 +020075 struct ieee80211_supported_band *sband,
76 u64 *rates)
77{
78 int i, j, count;
79 *rates = 0;
80 count = 0;
81 for (i = 0; i < bss->supp_rates_len; i++) {
82 int rate = (bss->supp_rates[i] & 0x7F) * 5;
83
84 for (j = 0; j < sband->n_bitrates; j++)
85 if (sband->bitrates[j].bitrate == rate) {
86 *rates |= BIT(j);
87 count++;
88 break;
89 }
90 }
91
92 return count;
93}
94
Johannes Berg9c6bd792008-09-11 00:01:52 +020095/* also used by mesh code */
96u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
97 struct ieee802_11_elems *elems,
98 enum ieee80211_band band)
Johannes Berg60f8b392008-09-08 17:44:22 +020099{
Johannes Berg9c6bd792008-09-11 00:01:52 +0200100 struct ieee80211_supported_band *sband;
101 struct ieee80211_rate *bitrates;
102 size_t num_rates;
103 u64 supp_rates;
104 int i, j;
105 sband = local->hw.wiphy->bands[band];
Johannes Berg60f8b392008-09-08 17:44:22 +0200106
Johannes Berg9c6bd792008-09-11 00:01:52 +0200107 if (!sband) {
108 WARN_ON(1);
109 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Berg60f8b392008-09-08 17:44:22 +0200110 }
Johannes Berg60f8b392008-09-08 17:44:22 +0200111
Johannes Berg9c6bd792008-09-11 00:01:52 +0200112 bitrates = sband->bitrates;
113 num_rates = sband->n_bitrates;
114 supp_rates = 0;
115 for (i = 0; i < elems->supp_rates_len +
116 elems->ext_supp_rates_len; i++) {
117 u8 rate = 0;
118 int own_rate;
119 if (i < elems->supp_rates_len)
120 rate = elems->supp_rates[i];
121 else if (elems->ext_supp_rates)
122 rate = elems->ext_supp_rates
123 [i - elems->supp_rates_len];
124 own_rate = 5 * (rate & 0x7f);
125 for (j = 0; j < num_rates; j++)
126 if (bitrates[j].bitrate == own_rate)
127 supp_rates |= BIT(j);
128 }
129 return supp_rates;
Johannes Berg60f8b392008-09-08 17:44:22 +0200130}
131
Johannes Berg9c6bd792008-09-11 00:01:52 +0200132/* frame sending functions */
133
134/* also used by scanning code */
Johannes Berg0a51b272008-09-08 17:44:25 +0200135void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
136 u8 *ssid, size_t ssid_len)
Johannes Berg60f8b392008-09-08 17:44:22 +0200137{
138 struct ieee80211_local *local = sdata->local;
139 struct ieee80211_supported_band *sband;
140 struct sk_buff *skb;
141 struct ieee80211_mgmt *mgmt;
142 u8 *pos, *supp_rates, *esupp_rates = NULL;
143 int i;
144
145 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
146 if (!skb) {
147 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
148 "request\n", sdata->dev->name);
149 return;
150 }
151 skb_reserve(skb, local->hw.extra_tx_headroom);
152
153 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
154 memset(mgmt, 0, 24);
155 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
156 IEEE80211_STYPE_PROBE_REQ);
157 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
158 if (dst) {
159 memcpy(mgmt->da, dst, ETH_ALEN);
160 memcpy(mgmt->bssid, dst, ETH_ALEN);
161 } else {
162 memset(mgmt->da, 0xff, ETH_ALEN);
163 memset(mgmt->bssid, 0xff, ETH_ALEN);
164 }
165 pos = skb_put(skb, 2 + ssid_len);
166 *pos++ = WLAN_EID_SSID;
167 *pos++ = ssid_len;
168 memcpy(pos, ssid, ssid_len);
169
170 supp_rates = skb_put(skb, 2);
171 supp_rates[0] = WLAN_EID_SUPP_RATES;
172 supp_rates[1] = 0;
173 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
174
175 for (i = 0; i < sband->n_bitrates; i++) {
176 struct ieee80211_rate *rate = &sband->bitrates[i];
177 if (esupp_rates) {
178 pos = skb_put(skb, 1);
179 esupp_rates[1]++;
180 } else if (supp_rates[1] == 8) {
181 esupp_rates = skb_put(skb, 3);
182 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
183 esupp_rates[1] = 1;
184 pos = &esupp_rates[2];
185 } else {
186 pos = skb_put(skb, 1);
187 supp_rates[1]++;
188 }
189 *pos = rate->bitrate / 5;
190 }
191
Johannes Berge50db652008-09-09 15:07:09 +0200192 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg60f8b392008-09-08 17:44:22 +0200193}
194
Johannes Berg9c6bd792008-09-11 00:01:52 +0200195static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
196 struct ieee80211_if_sta *ifsta,
197 int transaction, u8 *extra, size_t extra_len,
198 int encrypt)
199{
200 struct ieee80211_local *local = sdata->local;
201 struct sk_buff *skb;
202 struct ieee80211_mgmt *mgmt;
203
204 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
205 sizeof(*mgmt) + 6 + extra_len);
206 if (!skb) {
207 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
208 "frame\n", sdata->dev->name);
209 return;
210 }
211 skb_reserve(skb, local->hw.extra_tx_headroom);
212
213 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
214 memset(mgmt, 0, 24 + 6);
215 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
216 IEEE80211_STYPE_AUTH);
217 if (encrypt)
218 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
219 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
220 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
221 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
222 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
223 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
224 ifsta->auth_transaction = transaction + 1;
225 mgmt->u.auth.status_code = cpu_to_le16(0);
226 if (extra)
227 memcpy(skb_put(skb, extra_len), extra, extra_len);
228
229 ieee80211_tx_skb(sdata, skb, encrypt);
230}
231
Johannes Berg9ac19a92008-09-09 10:57:09 +0200232static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
233 struct ieee80211_if_sta *ifsta)
234{
235 struct ieee80211_local *local = sdata->local;
236 struct sk_buff *skb;
237 struct ieee80211_mgmt *mgmt;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200238 u8 *pos, *ies, *ht_ie;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200239 int i, len, count, rates_len, supp_rates_len;
240 u16 capab;
Johannes Bergc2b13452008-09-11 00:01:55 +0200241 struct ieee80211_bss *bss;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200242 int wmm = 0;
243 struct ieee80211_supported_band *sband;
244 u64 rates = 0;
245
246 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
247 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
248 ifsta->ssid_len);
249 if (!skb) {
250 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
251 "frame\n", sdata->dev->name);
252 return;
253 }
254 skb_reserve(skb, local->hw.extra_tx_headroom);
255
256 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
257
258 capab = ifsta->capab;
259
260 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
261 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
262 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
263 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
264 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
265 }
266
267 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
268 local->hw.conf.channel->center_freq,
269 ifsta->ssid, ifsta->ssid_len);
270 if (bss) {
271 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
272 capab |= WLAN_CAPABILITY_PRIVACY;
273 if (bss->wmm_used)
274 wmm = 1;
275
276 /* get all rates supported by the device and the AP as
277 * some APs don't like getting a superset of their rates
278 * in the association request (e.g. D-Link DAP 1353 in
279 * b-only mode) */
280 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
281
282 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
283 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
284 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
285
286 ieee80211_rx_bss_put(local, bss);
287 } else {
288 rates = ~0;
289 rates_len = sband->n_bitrates;
290 }
291
292 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
293 memset(mgmt, 0, 24);
294 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
295 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
296 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
297
298 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
299 skb_put(skb, 10);
300 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
301 IEEE80211_STYPE_REASSOC_REQ);
302 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
303 mgmt->u.reassoc_req.listen_interval =
304 cpu_to_le16(local->hw.conf.listen_interval);
305 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
306 ETH_ALEN);
307 } else {
308 skb_put(skb, 4);
309 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
310 IEEE80211_STYPE_ASSOC_REQ);
311 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Rami Rosen13554122008-12-16 22:38:29 +0200312 mgmt->u.assoc_req.listen_interval =
Johannes Berg9ac19a92008-09-09 10:57:09 +0200313 cpu_to_le16(local->hw.conf.listen_interval);
314 }
315
316 /* SSID */
317 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
318 *pos++ = WLAN_EID_SSID;
319 *pos++ = ifsta->ssid_len;
320 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
321
322 /* add all rates which were marked to be used above */
323 supp_rates_len = rates_len;
324 if (supp_rates_len > 8)
325 supp_rates_len = 8;
326
327 len = sband->n_bitrates;
328 pos = skb_put(skb, supp_rates_len + 2);
329 *pos++ = WLAN_EID_SUPP_RATES;
330 *pos++ = supp_rates_len;
331
332 count = 0;
333 for (i = 0; i < sband->n_bitrates; i++) {
334 if (BIT(i) & rates) {
335 int rate = sband->bitrates[i].bitrate;
336 *pos++ = (u8) (rate / 5);
337 if (++count == 8)
338 break;
339 }
340 }
341
342 if (rates_len > count) {
343 pos = skb_put(skb, rates_len - count + 2);
344 *pos++ = WLAN_EID_EXT_SUPP_RATES;
345 *pos++ = rates_len - count;
346
347 for (i++; i < sband->n_bitrates; i++) {
348 if (BIT(i) & rates) {
349 int rate = sband->bitrates[i].bitrate;
350 *pos++ = (u8) (rate / 5);
351 }
352 }
353 }
354
355 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
356 /* 1. power capabilities */
357 pos = skb_put(skb, 4);
358 *pos++ = WLAN_EID_PWR_CAPABILITY;
359 *pos++ = 2;
360 *pos++ = 0; /* min tx power */
361 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
362
363 /* 2. supported channels */
364 /* TODO: get this in reg domain format */
365 pos = skb_put(skb, 2 * sband->n_channels + 2);
366 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
367 *pos++ = 2 * sband->n_channels;
368 for (i = 0; i < sband->n_channels; i++) {
369 *pos++ = ieee80211_frequency_to_channel(
370 sband->channels[i].center_freq);
371 *pos++ = 1; /* one channel in the subband*/
372 }
373 }
374
375 if (ifsta->extra_ie) {
376 pos = skb_put(skb, ifsta->extra_ie_len);
377 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
378 }
379
380 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
381 pos = skb_put(skb, 9);
382 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
383 *pos++ = 7; /* len */
384 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
385 *pos++ = 0x50;
386 *pos++ = 0xf2;
387 *pos++ = 2; /* WME */
388 *pos++ = 0; /* WME info */
389 *pos++ = 1; /* WME ver */
390 *pos++ = 0;
391 }
392
393 /* wmm support is a must to HT */
Vasanthakumar Thiagarajaneb469362008-12-23 21:30:50 +0530394 /*
395 * IEEE802.11n does not allow TKIP/WEP as pairwise
396 * ciphers in HT mode. We still associate in non-ht
397 * mode (11a/b/g) if any one of these ciphers is
398 * configured as pairwise.
399 */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200400 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200401 sband->ht_cap.ht_supported &&
402 (ht_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_INFORMATION)) &&
Vasanthakumar Thiagarajaneb469362008-12-23 21:30:50 +0530403 ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
404 (!(ifsta->flags & IEEE80211_STA_TKIP_WEP_USED))) {
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200405 struct ieee80211_ht_info *ht_info =
406 (struct ieee80211_ht_info *)(ht_ie + 2);
407 u16 cap = sband->ht_cap.cap;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200408 __le16 tmp;
409 u32 flags = local->hw.conf.channel->flags;
410
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200411 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
412 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Johannes Berg9ac19a92008-09-09 10:57:09 +0200413 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200414 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200415 cap &= ~IEEE80211_HT_CAP_SGI_40;
416 }
417 break;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200418 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Johannes Berg9ac19a92008-09-09 10:57:09 +0200419 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200420 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200421 cap &= ~IEEE80211_HT_CAP_SGI_40;
422 }
423 break;
424 }
425
426 tmp = cpu_to_le16(cap);
427 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
428 *pos++ = WLAN_EID_HT_CAPABILITY;
429 *pos++ = sizeof(struct ieee80211_ht_cap);
430 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
431 memcpy(pos, &tmp, sizeof(u16));
432 pos += sizeof(u16);
433 /* TODO: needs a define here for << 2 */
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200434 *pos++ = sband->ht_cap.ampdu_factor |
435 (sband->ht_cap.ampdu_density << 2);
436 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
Johannes Berg9ac19a92008-09-09 10:57:09 +0200437 }
438
439 kfree(ifsta->assocreq_ies);
440 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
441 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
442 if (ifsta->assocreq_ies)
443 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
444
Johannes Berge50db652008-09-09 15:07:09 +0200445 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200446}
447
448
Johannes Bergef422bc2008-09-09 10:58:25 +0200449static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
450 u16 stype, u16 reason)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200451{
452 struct ieee80211_local *local = sdata->local;
Johannes Bergef422bc2008-09-09 10:58:25 +0200453 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200454 struct sk_buff *skb;
455 struct ieee80211_mgmt *mgmt;
456
457 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
458 if (!skb) {
Johannes Bergef422bc2008-09-09 10:58:25 +0200459 printk(KERN_DEBUG "%s: failed to allocate buffer for "
460 "deauth/disassoc frame\n", sdata->dev->name);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200461 return;
462 }
463 skb_reserve(skb, local->hw.extra_tx_headroom);
464
465 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
466 memset(mgmt, 0, 24);
467 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
468 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
469 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Johannes Bergef422bc2008-09-09 10:58:25 +0200470 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200471 skb_put(skb, 2);
Johannes Bergef422bc2008-09-09 10:58:25 +0200472 /* u.deauth.reason_code == u.disassoc.reason_code */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200473 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
474
Johannes Berge50db652008-09-09 15:07:09 +0200475 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg9ac19a92008-09-09 10:57:09 +0200476}
477
Johannes Berg60f8b392008-09-08 17:44:22 +0200478/* MLME */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200479static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
Johannes Bergc2b13452008-09-11 00:01:55 +0200480 struct ieee80211_bss *bss)
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100481{
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100482 struct ieee80211_local *local = sdata->local;
483 int i, have_higher_than_11mbit = 0;
484
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100485 /* cf. IEEE 802.11 9.2.12 */
486 for (i = 0; i < bss->supp_rates_len; i++)
487 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
488 have_higher_than_11mbit = 1;
489
490 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
491 have_higher_than_11mbit)
492 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
493 else
494 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
495
Johannes Berg3d35f7c2008-09-09 12:54:11 +0200496 ieee80211_set_wmm_default(sdata);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100497}
498
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200499static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -0700500 struct ieee80211_if_sta *ifsta,
501 u8 *wmm_param, size_t wmm_param_len)
502{
Jiri Bencf0706e82007-05-05 11:45:53 -0700503 struct ieee80211_tx_queue_params params;
504 size_t left;
505 int count;
506 u8 *pos;
507
Johannes Berg3434fbd2008-05-03 00:59:37 +0200508 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
509 return;
510
511 if (!wmm_param)
512 return;
513
Jiri Bencf0706e82007-05-05 11:45:53 -0700514 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
515 return;
516 count = wmm_param[6] & 0x0f;
517 if (count == ifsta->wmm_last_param_set)
518 return;
519 ifsta->wmm_last_param_set = count;
520
521 pos = wmm_param + 8;
522 left = wmm_param_len - 8;
523
524 memset(&params, 0, sizeof(params));
525
526 if (!local->ops->conf_tx)
527 return;
528
529 local->wmm_acm = 0;
530 for (; left >= 4; left -= 4, pos += 4) {
531 int aci = (pos[0] >> 5) & 0x03;
532 int acm = (pos[0] >> 4) & 0x01;
533 int queue;
534
535 switch (aci) {
536 case 1:
Johannes Berge100bb62008-04-30 18:51:21 +0200537 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200538 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700539 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700540 break;
541 case 2:
Johannes Berge100bb62008-04-30 18:51:21 +0200542 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200543 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700544 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700545 break;
546 case 3:
Johannes Berge100bb62008-04-30 18:51:21 +0200547 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200548 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700549 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700550 break;
551 case 0:
552 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200553 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200554 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700555 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700556 break;
557 }
558
559 params.aifs = pos[0] & 0x0f;
560 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
561 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +0200562 params.txop = get_unaligned_le16(pos + 2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200563#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700564 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100565 "cWmin=%d cWmax=%d txop=%d\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200566 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100567 params.cw_max, params.txop);
568#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700569 /* TODO: handle ACM (block TX, fallback to next lowest allowed
570 * AC for now) */
571 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
572 printk(KERN_DEBUG "%s: failed to set TX queue "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200573 "parameters for queue %d\n", local->mdev->name, queue);
Jiri Bencf0706e82007-05-05 11:45:53 -0700574 }
575 }
576}
577
Johannes Berg7a5158e2008-10-08 10:59:33 +0200578static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
579 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +0200580{
Johannes Bergbda39332008-10-11 01:51:51 +0200581 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Tomas Winklerebd74482008-07-01 10:44:50 +0300582#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200583 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Tomas Winklerebd74482008-07-01 10:44:50 +0300584#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100585 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +0200586 bool use_protection;
587 bool use_short_preamble;
588 bool use_short_slot;
589
590 if (erp_valid) {
591 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
592 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
593 } else {
594 use_protection = false;
595 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
596 }
597
598 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Daniel Drake56282212007-07-10 19:32:10 +0200599
Johannes Berg471b3ef2007-12-28 14:32:58 +0100600 if (use_protection != bss_conf->use_cts_prot) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200601#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200602 if (net_ratelimit()) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700603 printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100604 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200605 use_protection ? "enabled" : "disabled",
Johannes Berg0c68ae262008-10-27 15:56:10 -0700606 ifsta->bssid);
Daniel Drake56282212007-07-10 19:32:10 +0200607 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200608#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100609 bss_conf->use_cts_prot = use_protection;
610 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200611 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200612
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200613 if (use_short_preamble != bss_conf->use_short_preamble) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200614#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake7e9ed182007-07-27 15:43:24 +0200615 if (net_ratelimit()) {
616 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Johannes Berg0c68ae262008-10-27 15:56:10 -0700617 " (BSSID=%pM)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100618 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200619 use_short_preamble ? "short" : "long",
Johannes Berg0c68ae262008-10-27 15:56:10 -0700620 ifsta->bssid);
Daniel Drake7e9ed182007-07-27 15:43:24 +0200621 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200622#endif
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200623 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100624 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200625 }
Daniel Draked9430a32007-07-27 15:43:24 +0200626
Johannes Berg7a5158e2008-10-08 10:59:33 +0200627 if (use_short_slot != bss_conf->use_short_slot) {
628#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
629 if (net_ratelimit()) {
Christian Lamparter391429c2009-01-18 02:24:15 +0100630 printk(KERN_DEBUG "%s: switched to %s slot time"
631 " (BSSID=%pM)\n",
Johannes Berg7a5158e2008-10-08 10:59:33 +0200632 sdata->dev->name,
633 use_short_slot ? "short" : "long",
634 ifsta->bssid);
635 }
636#endif
637 bss_conf->use_short_slot = use_short_slot;
638 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -0400639 }
640
641 return changed;
642}
643
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200644static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
645 struct ieee80211_if_sta *ifsta)
646{
647 union iwreq_data wrqu;
648 memset(&wrqu, 0, sizeof(wrqu));
649 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
650 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
651 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
652 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
653}
654
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200655static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700656 struct ieee80211_if_sta *ifsta)
657{
Linus Torvalds74af0252008-09-05 12:38:09 -0700658 char *buf;
659 size_t len;
660 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700661 union iwreq_data wrqu;
662
Linus Torvalds74af0252008-09-05 12:38:09 -0700663 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
664 return;
665
666 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
667 ifsta->assocresp_ies_len), GFP_KERNEL);
668 if (!buf)
669 return;
670
671 len = sprintf(buf, "ASSOCINFO(");
Jiri Bencf0706e82007-05-05 11:45:53 -0700672 if (ifsta->assocreq_ies) {
Linus Torvalds74af0252008-09-05 12:38:09 -0700673 len += sprintf(buf + len, "ReqIEs=");
674 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
675 len += sprintf(buf + len, "%02x",
676 ifsta->assocreq_ies[i]);
677 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700678 }
679 if (ifsta->assocresp_ies) {
Linus Torvalds74af0252008-09-05 12:38:09 -0700680 if (ifsta->assocreq_ies)
681 len += sprintf(buf + len, " ");
682 len += sprintf(buf + len, "RespIEs=");
683 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
684 len += sprintf(buf + len, "%02x",
685 ifsta->assocresp_ies[i]);
686 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700687 }
Linus Torvalds74af0252008-09-05 12:38:09 -0700688 len += sprintf(buf + len, ")");
689
690 if (len > IW_CUSTOM_MAX) {
691 len = sprintf(buf, "ASSOCRESPIE=");
692 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
693 len += sprintf(buf + len, "%02x",
694 ifsta->assocresp_ies[i]);
695 }
696 }
697
John W. Linvillead788b52008-10-01 15:45:02 -0400698 if (len <= IW_CUSTOM_MAX) {
699 memset(&wrqu, 0, sizeof(wrqu));
700 wrqu.data.length = len;
701 wireless_send_event(sdata->dev, IWEVCUSTOM, &wrqu, buf);
702 }
Linus Torvalds74af0252008-09-05 12:38:09 -0700703
704 kfree(buf);
Jiri Bencf0706e82007-05-05 11:45:53 -0700705}
706
707
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200708static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Bergae5eb022008-10-14 16:58:37 +0200709 struct ieee80211_if_sta *ifsta,
710 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700711{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100712 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700713 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700714
Johannes Bergc2b13452008-09-11 00:01:55 +0200715 struct ieee80211_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400716
Johannes Bergae5eb022008-10-14 16:58:37 +0200717 bss_info_changed |= BSS_CHANGED_ASSOC;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200718 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400719
Johannes Berg05c914f2008-09-11 00:01:58 +0200720 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200721 return;
Daniel Drake56282212007-07-10 19:32:10 +0200722
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200723 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
724 conf->channel->center_freq,
725 ifsta->ssid, ifsta->ssid_len);
726 if (bss) {
727 /* set timing information */
Johannes Bergbda39332008-10-11 01:51:51 +0200728 sdata->vif.bss_conf.beacon_int = bss->beacon_int;
729 sdata->vif.bss_conf.timestamp = bss->timestamp;
730 sdata->vif.bss_conf.dtim_period = bss->dtim_period;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700731
Johannes Bergae5eb022008-10-14 16:58:37 +0200732 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Johannes Berg7a5158e2008-10-08 10:59:33 +0200733 bss->capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700734
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200735 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -0700736 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100737
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200738 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
739 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
740 ieee80211_sta_send_associnfo(sdata, ifsta);
741
742 ifsta->last_probe = jiffies;
743 ieee80211_led_assoc(local, 1);
744
Johannes Bergbda39332008-10-11 01:51:51 +0200745 sdata->vif.bss_conf.assoc = 1;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200746 /*
747 * For now just always ask the driver to update the basic rateset
748 * when we have associated, we aren't checking whether it actually
749 * changed or not.
750 */
Johannes Bergae5eb022008-10-14 16:58:37 +0200751 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
752 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300753
Kalle Valoe0cb6862008-12-18 23:35:13 +0200754 if (local->powersave) {
Kalle Valo520eb822008-12-18 23:35:27 +0200755 if (local->dynamic_ps_timeout > 0)
756 mod_timer(&local->dynamic_ps_timer, jiffies +
757 msecs_to_jiffies(local->dynamic_ps_timeout));
758 else {
759 conf->flags |= IEEE80211_CONF_PS;
760 ieee80211_hw_config(local,
761 IEEE80211_CONF_CHANGE_PS);
762 }
Kalle Valoe0cb6862008-12-18 23:35:13 +0200763 }
764
Tomas Winkler24e64622008-09-08 17:33:40 +0200765 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200766 netif_carrier_on(sdata->dev);
Guy Cohen8db93692008-07-03 19:56:13 +0300767
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200768 ieee80211_sta_send_apinfo(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700769}
770
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300771static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
772 struct ieee80211_if_sta *ifsta)
773{
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300774 ifsta->direct_probe_tries++;
775 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700776 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
777 sdata->dev->name, ifsta->bssid);
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300778 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Johannes Berg4a68ec52008-10-16 21:44:44 +0200779 ieee80211_sta_send_apinfo(sdata, ifsta);
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300780 return;
781 }
782
Johannes Berg0c68ae262008-10-27 15:56:10 -0700783 printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
784 sdata->dev->name, ifsta->bssid,
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300785 ifsta->direct_probe_tries);
786
787 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
788
789 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
790
791 /* Direct probe is sent to broadcast address as some APs
792 * will not answer to direct packet in unassociated state.
793 */
794 ieee80211_send_probe_req(sdata, NULL,
795 ifsta->ssid, ifsta->ssid_len);
796
797 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
798}
799
Jiri Bencf0706e82007-05-05 11:45:53 -0700800
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200801static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700802 struct ieee80211_if_sta *ifsta)
803{
804 ifsta->auth_tries++;
805 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700806 printk(KERN_DEBUG "%s: authentication with AP %pM"
Jiri Bencf0706e82007-05-05 11:45:53 -0700807 " timed out\n",
Johannes Berg0c68ae262008-10-27 15:56:10 -0700808 sdata->dev->name, ifsta->bssid);
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300809 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Johannes Berg4a68ec52008-10-16 21:44:44 +0200810 ieee80211_sta_send_apinfo(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700811 return;
812 }
813
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300814 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700815 printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
816 sdata->dev->name, ifsta->bssid);
Jiri Bencf0706e82007-05-05 11:45:53 -0700817
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200818 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700819
820 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
821}
822
Vivek Natarajan5925d972008-11-21 22:19:50 -0800823/*
824 * The disassoc 'reason' argument can be either our own reason
825 * if self disconnected or a reason code from the AP.
826 */
Tomas Winkleraa458d12008-09-09 00:32:12 +0300827static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
828 struct ieee80211_if_sta *ifsta, bool deauth,
829 bool self_disconnected, u16 reason)
830{
831 struct ieee80211_local *local = sdata->local;
832 struct sta_info *sta;
Kalle Valoe0cb6862008-12-18 23:35:13 +0200833 u32 changed = 0, config_changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +0300834
835 rcu_read_lock();
836
837 sta = sta_info_get(local, ifsta->bssid);
838 if (!sta) {
839 rcu_read_unlock();
840 return;
841 }
842
843 if (deauth) {
844 ifsta->direct_probe_tries = 0;
845 ifsta->auth_tries = 0;
846 }
847 ifsta->assoc_scan_tries = 0;
848 ifsta->assoc_tries = 0;
849
Tomas Winkler24e64622008-09-08 17:33:40 +0200850 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300851 netif_carrier_off(sdata->dev);
852
Johannes Berg17741cd2008-09-11 00:02:02 +0200853 ieee80211_sta_tear_down_BA_sessions(sdata, sta->sta.addr);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300854
855 if (self_disconnected) {
856 if (deauth)
Johannes Bergef422bc2008-09-09 10:58:25 +0200857 ieee80211_send_deauth_disassoc(sdata,
858 IEEE80211_STYPE_DEAUTH, reason);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300859 else
Johannes Bergef422bc2008-09-09 10:58:25 +0200860 ieee80211_send_deauth_disassoc(sdata,
861 IEEE80211_STYPE_DISASSOC, reason);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300862 }
863
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200864 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
865 changed |= ieee80211_reset_erp_info(sdata);
866
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200867 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +0200868 changed |= BSS_CHANGED_ASSOC;
869 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +0200870
871 ieee80211_sta_send_apinfo(sdata, ifsta);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300872
Vivek Natarajan5925d972008-11-21 22:19:50 -0800873 if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT)
Tomas Winkleraa458d12008-09-09 00:32:12 +0300874 ifsta->state = IEEE80211_STA_MLME_DISABLED;
875
Tomas Winkleraa458d12008-09-09 00:32:12 +0300876 rcu_read_unlock();
877
Johannes Bergae5eb022008-10-14 16:58:37 +0200878 local->hw.conf.ht.enabled = false;
Sujith094d05d2008-12-12 11:57:43 +0530879 local->oper_channel_type = NL80211_CHAN_NO_HT;
Kalle Valoe0cb6862008-12-18 23:35:13 +0200880 config_changed |= IEEE80211_CONF_CHANGE_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +0200881
Kalle Valo520eb822008-12-18 23:35:27 +0200882 del_timer_sync(&local->dynamic_ps_timer);
883 cancel_work_sync(&local->dynamic_ps_enable_work);
884
Kalle Valoe0cb6862008-12-18 23:35:13 +0200885 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
886 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
887 config_changed |= IEEE80211_CONF_CHANGE_PS;
888 }
889
890 ieee80211_hw_config(local, config_changed);
Johannes Bergae5eb022008-10-14 16:58:37 +0200891 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +0200892
893 rcu_read_lock();
894
895 sta = sta_info_get(local, ifsta->bssid);
896 if (!sta) {
897 rcu_read_unlock();
898 return;
899 }
900
901 sta_info_unlink(&sta);
902
903 rcu_read_unlock();
904
905 sta_info_destroy(sta);
Tomas Winkleraa458d12008-09-09 00:32:12 +0300906}
Jiri Bencf0706e82007-05-05 11:45:53 -0700907
Johannes Berg9ac19a92008-09-09 10:57:09 +0200908static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
909{
910 if (!sdata || !sdata->default_key ||
911 sdata->default_key->conf.alg != ALG_WEP)
912 return 0;
913 return 1;
914}
915
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200916static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700917 struct ieee80211_if_sta *ifsta)
918{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200919 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +0200920 struct ieee80211_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000921 int bss_privacy;
922 int wep_privacy;
923 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700924
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000925 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700926 return 0;
927
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200928 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100929 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400930 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700931 if (!bss)
932 return 0;
933
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000934 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200935 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000936 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700937
Johannes Berg3e122be2008-07-09 14:40:34 +0200938 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -0700939
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000940 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
941 return 0;
942
943 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700944}
945
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200946static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700947 struct ieee80211_if_sta *ifsta)
948{
949 ifsta->assoc_tries++;
950 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700951 printk(KERN_DEBUG "%s: association with AP %pM"
Jiri Bencf0706e82007-05-05 11:45:53 -0700952 " timed out\n",
Johannes Berg0c68ae262008-10-27 15:56:10 -0700953 sdata->dev->name, ifsta->bssid);
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300954 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Johannes Berg4a68ec52008-10-16 21:44:44 +0200955 ieee80211_sta_send_apinfo(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700956 return;
957 }
958
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300959 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700960 printk(KERN_DEBUG "%s: associate with AP %pM\n",
961 sdata->dev->name, ifsta->bssid);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200962 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700963 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200964 "mixed-cell disabled - abort association\n", sdata->dev->name);
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300965 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700966 return;
967 }
968
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200969 ieee80211_send_assoc(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700970
971 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
972}
973
974
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200975static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700976 struct ieee80211_if_sta *ifsta)
977{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200978 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700979 struct sta_info *sta;
980 int disassoc;
981
982 /* TODO: start monitoring current AP signal quality and number of
983 * missed beacons. Scan other channels every now and then and search
984 * for better APs. */
985 /* TODO: remove expired BSSes */
986
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300987 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700988
Johannes Bergd0709a62008-02-25 16:27:46 +0100989 rcu_read_lock();
990
Jiri Bencf0706e82007-05-05 11:45:53 -0700991 sta = sta_info_get(local, ifsta->bssid);
992 if (!sta) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700993 printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
994 sdata->dev->name, ifsta->bssid);
Jiri Bencf0706e82007-05-05 11:45:53 -0700995 disassoc = 1;
996 } else {
997 disassoc = 0;
998 if (time_after(jiffies,
999 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001000 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001001 printk(KERN_DEBUG "%s: No ProbeResp from "
Johannes Berg0c68ae262008-10-27 15:56:10 -07001002 "current AP %pM - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -07001003 "range\n",
Johannes Berg0c68ae262008-10-27 15:56:10 -07001004 sdata->dev->name, ifsta->bssid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001005 disassoc = 1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001006 } else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001007 ieee80211_send_probe_req(sdata, ifsta->bssid,
Johannes Berg4dfe51e2008-09-19 05:10:34 +02001008 ifsta->ssid,
1009 ifsta->ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001010 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001011 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001012 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001013 if (time_after(jiffies, ifsta->last_probe +
1014 IEEE80211_PROBE_INTERVAL)) {
1015 ifsta->last_probe = jiffies;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001016 ieee80211_send_probe_req(sdata, ifsta->bssid,
Jiri Bencf0706e82007-05-05 11:45:53 -07001017 ifsta->ssid,
1018 ifsta->ssid_len);
1019 }
1020 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001021 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001022
1023 rcu_read_unlock();
1024
Tomas Winkleraa458d12008-09-09 00:32:12 +03001025 if (disassoc)
1026 ieee80211_set_disassoc(sdata, ifsta, true, true,
1027 WLAN_REASON_PREV_AUTH_NOT_VALID);
1028 else
Jiri Bencf0706e82007-05-05 11:45:53 -07001029 mod_timer(&ifsta->timer, jiffies +
1030 IEEE80211_MONITORING_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001031}
1032
1033
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001034static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001035 struct ieee80211_if_sta *ifsta)
1036{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001037 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001038 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001039 ieee80211_associate(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001040}
1041
1042
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001043static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001044 struct ieee80211_if_sta *ifsta,
1045 struct ieee80211_mgmt *mgmt,
1046 size_t len)
1047{
1048 u8 *pos;
1049 struct ieee802_11_elems elems;
1050
Jiri Bencf0706e82007-05-05 11:45:53 -07001051 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001052 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001053 if (!elems.challenge)
Jiri Bencf0706e82007-05-05 11:45:53 -07001054 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001055 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
Jiri Bencf0706e82007-05-05 11:45:53 -07001056 elems.challenge_len + 2, 1);
1057}
1058
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001059static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001060 struct ieee80211_if_sta *ifsta,
1061 struct ieee80211_mgmt *mgmt,
1062 size_t len)
1063{
Jiri Bencf0706e82007-05-05 11:45:53 -07001064 u16 auth_alg, auth_transaction, status_code;
1065
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001066 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001067 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Jiri Bencf0706e82007-05-05 11:45:53 -07001068 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001069
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001070 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001071 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001072
Johannes Berg05c914f2008-09-11 00:01:58 +02001073 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001074 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001075 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001076
Johannes Berg05c914f2008-09-11 00:01:58 +02001077 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001078 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001079 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001080
1081 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1082 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1083 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1084
Johannes Berg05c914f2008-09-11 00:01:58 +02001085 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg135a2112008-06-16 20:55:29 +02001086 /*
1087 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e82007-05-05 11:45:53 -07001088 * networks and most implementations do not seem to use it.
1089 * However, try to reply to authentication attempts if someone
1090 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001091 */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001092 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
Jiri Bencf0706e82007-05-05 11:45:53 -07001093 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001094 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001095 }
1096
1097 if (auth_alg != ifsta->auth_alg ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001098 auth_transaction != ifsta->auth_transaction)
Jiri Bencf0706e82007-05-05 11:45:53 -07001099 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001100
1101 if (status_code != WLAN_STATUS_SUCCESS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001102 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1103 u8 algs[3];
1104 const int num_algs = ARRAY_SIZE(algs);
1105 int i, pos;
1106 algs[0] = algs[1] = algs[2] = 0xff;
1107 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1108 algs[0] = WLAN_AUTH_OPEN;
1109 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1110 algs[1] = WLAN_AUTH_SHARED_KEY;
1111 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1112 algs[2] = WLAN_AUTH_LEAP;
1113 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1114 pos = 0;
1115 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1116 pos = 1;
1117 else
1118 pos = 2;
1119 for (i = 0; i < num_algs; i++) {
1120 pos++;
1121 if (pos >= num_algs)
1122 pos = 0;
1123 if (algs[pos] == ifsta->auth_alg ||
1124 algs[pos] == 0xff)
1125 continue;
1126 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001127 !ieee80211_sta_wep_configured(sdata))
Jiri Bencf0706e82007-05-05 11:45:53 -07001128 continue;
1129 ifsta->auth_alg = algs[pos];
Jiri Bencf0706e82007-05-05 11:45:53 -07001130 break;
1131 }
1132 }
1133 return;
1134 }
1135
1136 switch (ifsta->auth_alg) {
1137 case WLAN_AUTH_OPEN:
1138 case WLAN_AUTH_LEAP:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001139 ieee80211_auth_completed(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001140 break;
1141 case WLAN_AUTH_SHARED_KEY:
1142 if (ifsta->auth_transaction == 4)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001143 ieee80211_auth_completed(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001144 else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001145 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001146 break;
1147 }
1148}
1149
1150
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001151static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001152 struct ieee80211_if_sta *ifsta,
1153 struct ieee80211_mgmt *mgmt,
1154 size_t len)
1155{
1156 u16 reason_code;
1157
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001158 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001159 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001160
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001161 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001162 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001163
1164 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1165
Johannes Berg988c0f72008-04-17 19:21:22 +02001166 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Zhu Yi97c8b012008-10-28 15:58:31 +08001167 printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
1168 sdata->dev->name, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001169
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001170 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1171 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1172 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001173 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001174 mod_timer(&ifsta->timer, jiffies +
1175 IEEE80211_RETRY_AUTH_INTERVAL);
1176 }
1177
Tomas Winkleraa458d12008-09-09 00:32:12 +03001178 ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001179 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001180}
1181
1182
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001183static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001184 struct ieee80211_if_sta *ifsta,
1185 struct ieee80211_mgmt *mgmt,
1186 size_t len)
1187{
1188 u16 reason_code;
1189
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001190 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001191 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001192
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001193 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001194 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001195
1196 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1197
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001198 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Zhu Yi97c8b012008-10-28 15:58:31 +08001199 printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
1200 sdata->dev->name, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001201
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001202 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1203 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001204 mod_timer(&ifsta->timer, jiffies +
1205 IEEE80211_RETRY_AUTH_INTERVAL);
1206 }
1207
Vivek Natarajan5925d972008-11-21 22:19:50 -08001208 ieee80211_set_disassoc(sdata, ifsta, false, false, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001209}
1210
1211
Johannes Berg471b3ef2007-12-28 14:32:58 +01001212static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001213 struct ieee80211_if_sta *ifsta,
1214 struct ieee80211_mgmt *mgmt,
1215 size_t len,
1216 int reassoc)
1217{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001218 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01001219 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001220 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001221 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001222 u16 capab_info, status_code, aid;
1223 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02001224 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001225 u8 *pos;
Johannes Bergae5eb022008-10-14 16:58:37 +02001226 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001227 int i, j;
Johannes Bergddf4ac52008-10-22 11:41:38 +02001228 bool have_higher_than_11mbit = false, newsta = false;
Johannes Bergae5eb022008-10-14 16:58:37 +02001229 u16 ap_ht_cap_flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07001230
1231 /* AssocResp and ReassocResp have identical structure, so process both
1232 * of them in this function. */
1233
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001234 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
Jiri Bencf0706e82007-05-05 11:45:53 -07001235 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001236
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001237 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001238 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001239
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001240 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001241 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001242
1243 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1244 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1245 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001246
Johannes Berg0c68ae262008-10-27 15:56:10 -07001247 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001248 "status=%d aid=%d)\n",
Johannes Berg0c68ae262008-10-27 15:56:10 -07001249 sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
Johannes Bergddd68582007-10-22 14:51:37 +02001250 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001251
1252 if (status_code != WLAN_STATUS_SUCCESS) {
1253 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001254 sdata->dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001255 /* if this was a reassociation, ensure we try a "full"
1256 * association next time. This works around some broken APs
1257 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001258 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001259 return;
1260 }
1261
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001262 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1263 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001264 "set\n", sdata->dev->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001265 aid &= ~(BIT(15) | BIT(14));
1266
Jiri Bencf0706e82007-05-05 11:45:53 -07001267 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001268 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001269
1270 if (!elems.supp_rates) {
1271 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001272 sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001273 return;
1274 }
1275
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001276 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001277 ifsta->aid = aid;
1278 ifsta->ap_capab = capab_info;
1279
1280 kfree(ifsta->assocresp_ies);
1281 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001282 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001283 if (ifsta->assocresp_ies)
1284 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1285
Johannes Bergd0709a62008-02-25 16:27:46 +01001286 rcu_read_lock();
1287
Jiri Bencf0706e82007-05-05 11:45:53 -07001288 /* Add STA entry for the AP */
1289 sta = sta_info_get(local, ifsta->bssid);
1290 if (!sta) {
Johannes Bergc2b13452008-09-11 00:01:55 +02001291 struct ieee80211_bss *bss;
Johannes Bergddf4ac52008-10-22 11:41:38 +02001292
1293 newsta = true;
Johannes Bergd0709a62008-02-25 16:27:46 +01001294
Johannes Berg73651ee2008-02-25 16:27:47 +01001295 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1296 if (!sta) {
1297 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001298 " the AP\n", sdata->dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01001299 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001300 return;
1301 }
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001302 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001303 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001304 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001305 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001306 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02001307 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07001308 sta->last_noise = bss->noise;
Johannes Berg3e122be2008-07-09 14:40:34 +02001309 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07001310 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001311
Ron Rindjunskya61dae12008-08-10 00:54:34 +03001312 /* update new sta with its last rx activity */
1313 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -07001314 }
1315
Johannes Berg73651ee2008-02-25 16:27:47 +01001316 /*
1317 * FIXME: Do we really need to update the sta_info's information here?
1318 * We already know about the AP (we found it in our list) so it
1319 * should already be filled with the right info, no?
1320 * As is stands, all this is racy because typically we assume
1321 * the information that is filled in here (except flags) doesn't
1322 * change while a STA structure is alive. As such, it should move
1323 * to between the sta_info_alloc() and sta_info_insert() above.
1324 */
1325
Johannes Berg07346f812008-05-03 01:02:02 +02001326 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1327 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07001328
1329 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001330 basic_rates = 0;
1331 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1332
Jiri Bencf0706e82007-05-05 11:45:53 -07001333 for (i = 0; i < elems.supp_rates_len; i++) {
1334 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Tomas Winklerd61272c2008-10-30 17:08:08 +02001335 bool is_basic = !!(elems.supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001336
1337 if (rate > 110)
1338 have_higher_than_11mbit = true;
1339
1340 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001341 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001342 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001343 if (is_basic)
1344 basic_rates |= BIT(j);
1345 break;
1346 }
Johannes Berg8318d782008-01-24 19:38:38 +01001347 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001348 }
Johannes Berg8318d782008-01-24 19:38:38 +01001349
Jiri Bencf0706e82007-05-05 11:45:53 -07001350 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1351 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Tomas Winklerd61272c2008-10-30 17:08:08 +02001352 bool is_basic = !!(elems.supp_rates[i] & 0x80);
Johannes Berg8318d782008-01-24 19:38:38 +01001353
1354 if (rate > 110)
1355 have_higher_than_11mbit = true;
1356
1357 for (j = 0; j < sband->n_bitrates; j++) {
Tomas Winklerd61272c2008-10-30 17:08:08 +02001358 if (sband->bitrates[j].bitrate == rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001359 rates |= BIT(j);
Tomas Winklerd61272c2008-10-30 17:08:08 +02001360 if (is_basic)
1361 basic_rates |= BIT(j);
1362 break;
1363 }
Johannes Berg8318d782008-01-24 19:38:38 +01001364 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001365 }
Johannes Berg8318d782008-01-24 19:38:38 +01001366
Johannes Berg323ce792008-09-11 02:45:11 +02001367 sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
Johannes Bergbda39332008-10-11 01:51:51 +02001368 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01001369
1370 /* cf. IEEE 802.11 9.2.12 */
1371 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1372 have_higher_than_11mbit)
1373 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1374 else
1375 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001376
Johannes Bergae5eb022008-10-14 16:58:37 +02001377 if (elems.ht_cap_elem)
1378 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02001379 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02001380
1381 ap_ht_cap_flags = sta->sta.ht_cap.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001382
Johannes Berg4b7679a2008-09-18 18:14:18 +02001383 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001384
Johannes Bergddf4ac52008-10-22 11:41:38 +02001385 if (elems.wmm_param)
Johannes Berg07346f812008-05-03 01:02:02 +02001386 set_sta_flags(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02001387
1388 if (newsta) {
1389 int err = sta_info_insert(sta);
1390 if (err) {
1391 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1392 " the AP (error %d)\n", sdata->dev->name, err);
1393 rcu_read_unlock();
1394 return;
1395 }
1396 }
1397
1398 rcu_read_unlock();
1399
1400 if (elems.wmm_param)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001401 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07001402 elems.wmm_param_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001403
Johannes Bergae5eb022008-10-14 16:58:37 +02001404 if (elems.ht_info_elem && elems.wmm_param &&
1405 (ifsta->flags & IEEE80211_STA_WMM_ENABLED))
1406 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1407 ap_ht_cap_flags);
1408
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001409 /* set AID and assoc capability,
1410 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01001411 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001412 bss_conf->assoc_capability = capab_info;
Johannes Bergae5eb022008-10-14 16:58:37 +02001413 ieee80211_set_associated(sdata, ifsta, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001414
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001415 ieee80211_associated(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001416}
1417
1418
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001419static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
Bruno Randolfa6072682008-02-18 11:21:15 +09001420 struct ieee80211_if_sta *ifsta,
Johannes Bergc2b13452008-09-11 00:01:55 +02001421 struct ieee80211_bss *bss)
Bruno Randolfa6072682008-02-18 11:21:15 +09001422{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001423 struct ieee80211_local *local = sdata->local;
Bruno Randolfa6072682008-02-18 11:21:15 +09001424 int res, rates, i, j;
1425 struct sk_buff *skb;
1426 struct ieee80211_mgmt *mgmt;
Bruno Randolfa6072682008-02-18 11:21:15 +09001427 u8 *pos;
Bruno Randolfa6072682008-02-18 11:21:15 +09001428 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04001429 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09001430
Rami Rosene2ef12d2008-10-22 09:58:39 +02001431 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
1432 if (!skb) {
1433 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1434 "response\n", sdata->dev->name);
1435 return -ENOMEM;
1436 }
1437
Bruno Randolfa6072682008-02-18 11:21:15 +09001438 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1439
1440 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02001441 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09001442
1443 if (local->ops->reset_tsf) {
1444 /* Reset own TSF to allow time synchronization work. */
1445 local->ops->reset_tsf(local_to_hw(local));
1446 }
1447 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
Johannes Berg9d139c82008-07-09 14:40:37 +02001448 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Bruno Randolfa6072682008-02-18 11:21:15 +09001449 if (res)
1450 return res;
1451
1452 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
1453
Bruno Randolfa6072682008-02-18 11:21:15 +09001454 sdata->drop_unencrypted = bss->capability &
1455 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
1456
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001457 res = ieee80211_set_freq(sdata, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09001458
Assaf Kraussbe038b32008-06-05 19:55:21 +03001459 if (res)
1460 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09001461
Johannes Berg9d139c82008-07-09 14:40:37 +02001462 /* Build IBSS probe response */
Bruno Randolfa6072682008-02-18 11:21:15 +09001463
Rami Rosene2ef12d2008-10-22 09:58:39 +02001464 skb_reserve(skb, local->hw.extra_tx_headroom);
Bruno Randolfa6072682008-02-18 11:21:15 +09001465
Rami Rosene2ef12d2008-10-22 09:58:39 +02001466 mgmt = (struct ieee80211_mgmt *)
1467 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1468 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1469 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1470 IEEE80211_STYPE_PROBE_RESP);
1471 memset(mgmt->da, 0xff, ETH_ALEN);
1472 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1473 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1474 mgmt->u.beacon.beacon_int =
1475 cpu_to_le16(local->hw.conf.beacon_int);
1476 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
1477 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
Bruno Randolfa6072682008-02-18 11:21:15 +09001478
Rami Rosene2ef12d2008-10-22 09:58:39 +02001479 pos = skb_put(skb, 2 + ifsta->ssid_len);
1480 *pos++ = WLAN_EID_SSID;
1481 *pos++ = ifsta->ssid_len;
1482 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
Bruno Randolfa6072682008-02-18 11:21:15 +09001483
Rami Rosene2ef12d2008-10-22 09:58:39 +02001484 rates = bss->supp_rates_len;
1485 if (rates > 8)
1486 rates = 8;
1487 pos = skb_put(skb, 2 + rates);
1488 *pos++ = WLAN_EID_SUPP_RATES;
1489 *pos++ = rates;
1490 memcpy(pos, bss->supp_rates, rates);
Bruno Randolfa6072682008-02-18 11:21:15 +09001491
Rami Rosene2ef12d2008-10-22 09:58:39 +02001492 if (bss->band == IEEE80211_BAND_2GHZ) {
1493 pos = skb_put(skb, 2 + 1);
1494 *pos++ = WLAN_EID_DS_PARAMS;
1495 *pos++ = 1;
1496 *pos++ = ieee80211_frequency_to_channel(bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09001497 }
1498
Rami Rosene2ef12d2008-10-22 09:58:39 +02001499 pos = skb_put(skb, 2 + 2);
1500 *pos++ = WLAN_EID_IBSS_PARAMS;
1501 *pos++ = 2;
1502 /* FIX: set ATIM window based on scan results */
1503 *pos++ = 0;
1504 *pos++ = 0;
1505
1506 if (bss->supp_rates_len > 8) {
1507 rates = bss->supp_rates_len - 8;
1508 pos = skb_put(skb, 2 + rates);
1509 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1510 *pos++ = rates;
1511 memcpy(pos, &bss->supp_rates[8], rates);
1512 }
1513
1514 ifsta->probe_resp = skb;
1515
1516 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1517
1518
Johannes Berg9d139c82008-07-09 14:40:37 +02001519 rates = 0;
1520 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1521 for (i = 0; i < bss->supp_rates_len; i++) {
1522 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
1523 for (j = 0; j < sband->n_bitrates; j++)
1524 if (sband->bitrates[j].bitrate == bitrate)
1525 rates |= BIT(j);
1526 }
1527 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
1528
Johannes Bergb079ada2008-09-09 09:32:59 +02001529 ieee80211_sta_def_wmm_params(sdata, bss);
Johannes Berg9d139c82008-07-09 14:40:37 +02001530
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001531 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
Bruno Randolfa6072682008-02-18 11:21:15 +09001532 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1533
Emmanuel Grumbach4492bea2008-09-22 17:10:10 +03001534 ieee80211_led_assoc(local, true);
1535
Dan Williams507b06d2008-06-03 23:39:55 -04001536 memset(&wrqu, 0, sizeof(wrqu));
1537 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001538 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09001539
1540 return res;
1541}
1542
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001543static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001544 struct ieee80211_mgmt *mgmt,
1545 size_t len,
1546 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001547 struct ieee802_11_elems *elems,
1548 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07001549{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001550 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001551 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02001552 struct ieee80211_bss *bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07001553 struct sta_info *sta;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01001554 struct ieee80211_channel *channel;
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001555 u64 beacon_timestamp, rx_timestamp;
1556 u64 supp_rates = 0;
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001557 enum ieee80211_band band = rx_status->band;
Jiri Bencf0706e82007-05-05 11:45:53 -07001558
Johannes Berg5bda6172008-09-08 11:05:10 +02001559 if (elems->ds_params && elems->ds_params_len == 1)
1560 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1561 else
1562 freq = rx_status->freq;
1563
1564 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1565
1566 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1567 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001568
Johannes Berg05c914f2008-09-11 00:01:58 +02001569 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001570 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001571 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1572
Johannes Berg69e6c012008-09-08 11:05:08 +02001573 rcu_read_lock();
1574
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001575 sta = sta_info_get(local, mgmt->sa);
1576 if (sta) {
1577 u64 prev_rates;
1578
Johannes Berg323ce792008-09-11 02:45:11 +02001579 prev_rates = sta->sta.supp_rates[band];
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001580 /* make sure mandatory rates are always added */
Johannes Berg323ce792008-09-11 02:45:11 +02001581 sta->sta.supp_rates[band] = supp_rates |
Johannes Berg96dd22a2008-09-11 00:01:57 +02001582 ieee80211_mandatory_rates(local, band);
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001583
1584#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg323ce792008-09-11 02:45:11 +02001585 if (sta->sta.supp_rates[band] != prev_rates)
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001586 printk(KERN_DEBUG "%s: updated supp_rates set "
Johannes Berg0c68ae262008-10-27 15:56:10 -07001587 "for %pM based on beacon info (0x%llx | "
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001588 "0x%llx -> 0x%llx)\n",
Johannes Berg17741cd2008-09-11 00:02:02 +02001589 sdata->dev->name,
Johannes Berg0c68ae262008-10-27 15:56:10 -07001590 sta->sta.addr,
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001591 (unsigned long long) prev_rates,
1592 (unsigned long long) supp_rates,
Johannes Berg323ce792008-09-11 02:45:11 +02001593 (unsigned long long) sta->sta.supp_rates[band]);
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03001594#endif
1595 } else {
Rami Rosenab1f5c02008-12-11 14:00:25 +02001596 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
Jiri Bencf0706e82007-05-05 11:45:53 -07001597 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001598
Johannes Berg69e6c012008-09-08 11:05:08 +02001599 rcu_read_unlock();
1600 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001601
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001602 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1603 freq, beacon);
1604 if (!bss)
1605 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001606
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001607 /* was just updated in ieee80211_bss_info_update */
1608 beacon_timestamp = bss->timestamp;
Daniel Drake56282212007-07-10 19:32:10 +02001609
Johannes Berg30b89b02008-04-16 17:43:20 +02001610 /*
1611 * In STA mode, the remaining parameters should not be overridden
1612 * by beacons because they're not necessarily accurate there.
1613 */
Johannes Berg05c914f2008-09-11 00:01:58 +02001614 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001615 bss->last_probe_resp && beacon) {
Johannes Berg3e122be2008-07-09 14:40:34 +02001616 ieee80211_rx_bss_put(local, bss);
Johannes Berg30b89b02008-04-16 17:43:20 +02001617 return;
1618 }
1619
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001620 /* check if we need to merge IBSS */
Johannes Berg05c914f2008-09-11 00:01:58 +02001621 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && beacon &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01001622 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001623 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03001624 elems->ssid_len == sdata->u.sta.ssid_len &&
1625 memcmp(elems->ssid, sdata->u.sta.ssid,
1626 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001627 if (rx_status->flag & RX_FLAG_TSFT) {
1628 /* in order for correct IBSS merging we need mactime
1629 *
1630 * since mactime is defined as the time the first data
1631 * symbol of the frame hits the PHY, and the timestamp
1632 * of the beacon is defined as "the time that the data
1633 * symbol containing the first bit of the timestamp is
1634 * transmitted to the PHY plus the transmitting STA’s
1635 * delays through its local PHY from the MAC-PHY
1636 * interface to its interface with the WM"
1637 * (802.11 11.1.2) - equals the time this bit arrives at
1638 * the receiver - we have to take into account the
1639 * offset between the two.
1640 * e.g: at 1 MBit that means mactime is 192 usec earlier
1641 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
1642 */
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02001643 int rate;
1644 if (rx_status->flag & RX_FLAG_HT) {
1645 rate = 65; /* TODO: HT rates */
1646 } else {
1647 rate = local->hw.wiphy->bands[band]->
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001648 bitrates[rx_status->rate_idx].bitrate;
Jouni Malinen0fb8ca42008-12-12 14:38:33 +02001649 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001650 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
1651 } else if (local && local->ops && local->ops->get_tsf)
1652 /* second best option: get current TSF */
1653 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
1654 else
1655 /* can't merge without knowing the TSF */
1656 rx_timestamp = -1LLU;
1657#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -07001658 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
1659 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
1660 mgmt->sa, mgmt->bssid,
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001661 (unsigned long long)rx_timestamp,
1662 (unsigned long long)beacon_timestamp,
1663 (unsigned long long)(rx_timestamp - beacon_timestamp),
1664 jiffies);
1665#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1666 if (beacon_timestamp > rx_timestamp) {
John W. Linville576fdea2008-08-26 20:33:34 -04001667#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001668 printk(KERN_DEBUG "%s: beacon TSF higher than "
Johannes Berg0c68ae262008-10-27 15:56:10 -07001669 "local TSF - IBSS merge with BSSID %pM\n",
1670 sdata->dev->name, mgmt->bssid);
Johannes Bergfba4a1e2008-02-21 11:08:33 +01001671#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001672 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
Rami Rosenab1f5c02008-12-11 14:00:25 +02001673 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09001674 }
1675 }
1676
Johannes Berg3e122be2008-07-09 14:40:34 +02001677 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07001678}
1679
1680
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001681static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001682 struct ieee80211_mgmt *mgmt,
1683 size_t len,
1684 struct ieee80211_rx_status *rx_status)
1685{
Ester Kummerae6a44e2008-06-27 18:54:48 +03001686 size_t baselen;
1687 struct ieee802_11_elems elems;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001688 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Ester Kummerae6a44e2008-06-27 18:54:48 +03001689
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03001690 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1691 return; /* ignore ProbeResp to foreign address */
1692
Ester Kummerae6a44e2008-06-27 18:54:48 +03001693 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1694 if (baselen > len)
1695 return;
1696
1697 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1698 &elems);
1699
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001700 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001701
1702 /* direct probe may be part of the association flow */
1703 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
1704 &ifsta->request)) {
1705 printk(KERN_DEBUG "%s direct probe responded\n",
1706 sdata->dev->name);
1707 ieee80211_authenticate(sdata, ifsta);
1708 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001709}
1710
1711
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001712static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001713 struct ieee80211_mgmt *mgmt,
1714 size_t len,
1715 struct ieee80211_rx_status *rx_status)
1716{
Jiri Bencf0706e82007-05-05 11:45:53 -07001717 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07001718 size_t baselen;
1719 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001720 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001721 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001722 bool erp_valid;
1723 u8 erp_value = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001724
Ester Kummerae6a44e2008-06-27 18:54:48 +03001725 /* Process beacon from the current BSS */
1726 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1727 if (baselen > len)
1728 return;
1729
1730 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1731
Johannes Berg98c8fcc2008-09-08 17:44:26 +02001732 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
Jiri Bencf0706e82007-05-05 11:45:53 -07001733
Johannes Berg05c914f2008-09-11 00:01:58 +02001734 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Jiri Bencf0706e82007-05-05 11:45:53 -07001735 return;
1736 ifsta = &sdata->u.sta;
1737
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001738 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07001739 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1740 return;
1741
Johannes Bergfe3fa822008-09-08 11:05:09 +02001742 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1743 elems.wmm_param_len);
1744
Johannes Berg7a5158e2008-10-08 10:59:33 +02001745
1746 if (elems.erp_info && elems.erp_info_len >= 1) {
1747 erp_valid = true;
1748 erp_value = elems.erp_info[0];
1749 } else {
1750 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04001751 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02001752 changed |= ieee80211_handle_bss_capability(sdata,
1753 le16_to_cpu(mgmt->u.beacon.capab_info),
1754 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07001755
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001756
Johannes Bergae5eb022008-10-14 16:58:37 +02001757 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
1758 struct sta_info *sta;
1759 struct ieee80211_supported_band *sband;
1760 u16 ap_ht_cap_flags;
1761
1762 rcu_read_lock();
1763
1764 sta = sta_info_get(local, ifsta->bssid);
1765 if (!sta) {
1766 rcu_read_unlock();
1767 return;
1768 }
1769
1770 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1771
1772 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1773 elems.ht_cap_elem, &sta->sta.ht_cap);
1774
1775 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1776
1777 rcu_read_unlock();
1778
1779 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1780 ap_ht_cap_flags);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001781 }
1782
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001783 if (elems.country_elem) {
1784 /* Note we are only reviewing this on beacons
1785 * for the BSSID we are associated to */
1786 regulatory_hint_11d(local->hw.wiphy,
1787 elems.country_elem, elems.country_elem_len);
1788 }
1789
Johannes Berg471b3ef2007-12-28 14:32:58 +01001790 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07001791}
1792
1793
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001794static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001795 struct ieee80211_if_sta *ifsta,
1796 struct ieee80211_mgmt *mgmt,
1797 size_t len,
1798 struct ieee80211_rx_status *rx_status)
1799{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001800 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001801 int tx_last_beacon;
1802 struct sk_buff *skb;
1803 struct ieee80211_mgmt *resp;
1804 u8 *pos, *end;
1805
Johannes Berg05c914f2008-09-11 00:01:58 +02001806 if (sdata->vif.type != NL80211_IFTYPE_ADHOC ||
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001807 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
Jiri Bencf0706e82007-05-05 11:45:53 -07001808 len < 24 + 2 || !ifsta->probe_resp)
1809 return;
1810
1811 if (local->ops->tx_last_beacon)
1812 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1813 else
1814 tx_last_beacon = 1;
1815
1816#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -07001817 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
1818 " (tx_last_beacon=%d)\n",
1819 sdata->dev->name, mgmt->sa, mgmt->da,
1820 mgmt->bssid, tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07001821#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1822
1823 if (!tx_last_beacon)
1824 return;
1825
1826 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1827 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1828 return;
1829
1830 end = ((u8 *) mgmt) + len;
1831 pos = mgmt->u.probe_req.variable;
1832 if (pos[0] != WLAN_EID_SSID ||
1833 pos + 2 + pos[1] > end) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001834#ifdef CONFIG_MAC80211_IBSS_DEBUG
1835 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Johannes Berg0c68ae262008-10-27 15:56:10 -07001836 "from %pM\n",
1837 sdata->dev->name, mgmt->sa);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001838#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07001839 return;
1840 }
1841 if (pos[1] != 0 &&
1842 (pos[1] != ifsta->ssid_len ||
1843 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1844 /* Ignore ProbeReq for foreign SSID */
1845 return;
1846 }
1847
1848 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001849 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001850 if (!skb)
1851 return;
1852
1853 resp = (struct ieee80211_mgmt *) skb->data;
1854 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1855#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -07001856 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
1857 sdata->dev->name, resp->da);
Jiri Bencf0706e82007-05-05 11:45:53 -07001858#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Johannes Berge50db652008-09-09 15:07:09 +02001859 ieee80211_tx_skb(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001860}
1861
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001862void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
Jiri Bencf0706e82007-05-05 11:45:53 -07001863 struct ieee80211_rx_status *rx_status)
1864{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001865 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001866 struct ieee80211_if_sta *ifsta;
1867 struct ieee80211_mgmt *mgmt;
1868 u16 fc;
1869
1870 if (skb->len < 24)
1871 goto fail;
1872
Jiri Bencf0706e82007-05-05 11:45:53 -07001873 ifsta = &sdata->u.sta;
1874
1875 mgmt = (struct ieee80211_mgmt *) skb->data;
1876 fc = le16_to_cpu(mgmt->frame_control);
1877
1878 switch (fc & IEEE80211_FCTL_STYPE) {
1879 case IEEE80211_STYPE_PROBE_REQ:
1880 case IEEE80211_STYPE_PROBE_RESP:
1881 case IEEE80211_STYPE_BEACON:
1882 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1883 case IEEE80211_STYPE_AUTH:
1884 case IEEE80211_STYPE_ASSOC_RESP:
1885 case IEEE80211_STYPE_REASSOC_RESP:
1886 case IEEE80211_STYPE_DEAUTH:
1887 case IEEE80211_STYPE_DISASSOC:
1888 skb_queue_tail(&ifsta->skb_queue, skb);
1889 queue_work(local->hw.workqueue, &ifsta->work);
1890 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001891 }
1892
1893 fail:
1894 kfree_skb(skb);
1895}
1896
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001897static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001898 struct sk_buff *skb)
1899{
1900 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07001901 struct ieee80211_if_sta *ifsta;
1902 struct ieee80211_mgmt *mgmt;
1903 u16 fc;
1904
Jiri Bencf0706e82007-05-05 11:45:53 -07001905 ifsta = &sdata->u.sta;
1906
1907 rx_status = (struct ieee80211_rx_status *) skb->cb;
1908 mgmt = (struct ieee80211_mgmt *) skb->data;
1909 fc = le16_to_cpu(mgmt->frame_control);
1910
1911 switch (fc & IEEE80211_FCTL_STYPE) {
1912 case IEEE80211_STYPE_PROBE_REQ:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001913 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
Jiri Bencf0706e82007-05-05 11:45:53 -07001914 rx_status);
1915 break;
1916 case IEEE80211_STYPE_PROBE_RESP:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001917 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001918 break;
1919 case IEEE80211_STYPE_BEACON:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001920 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001921 break;
1922 case IEEE80211_STYPE_AUTH:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001923 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001924 break;
1925 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01001926 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001927 break;
1928 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01001929 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07001930 break;
1931 case IEEE80211_STYPE_DEAUTH:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001932 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001933 break;
1934 case IEEE80211_STYPE_DISASSOC:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001935 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001936 break;
1937 }
1938
1939 kfree_skb(skb);
1940}
1941
1942
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001943static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -07001944{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001945 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001946 int active = 0;
1947 struct sta_info *sta;
1948
Johannes Bergd0709a62008-02-25 16:27:46 +01001949 rcu_read_lock();
1950
1951 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1952 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001953 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1954 jiffies)) {
1955 active++;
1956 break;
1957 }
1958 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001959
1960 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001961
1962 return active;
1963}
1964
1965
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001966static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001967 struct ieee80211_if_sta *ifsta)
1968{
1969 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1970
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001971 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
1972 if (ieee80211_sta_active_ibss(sdata))
Jiri Bencf0706e82007-05-05 11:45:53 -07001973 return;
1974
1975 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001976 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
Johannes Bergc2b13452008-09-11 00:01:55 +02001977 ieee80211_request_scan(sdata, ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001978}
1979
1980
Johannes Berg9c6bd792008-09-11 00:01:52 +02001981static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07001982{
1983 struct ieee80211_sub_if_data *sdata =
1984 (struct ieee80211_sub_if_data *) data;
1985 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001986 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001987
1988 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1989 queue_work(local->hw.workqueue, &ifsta->work);
1990}
1991
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001992static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001993 struct ieee80211_if_sta *ifsta)
1994{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001995 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001996
1997 if (local->ops->reset_tsf) {
1998 /* Reset own TSF to allow time synchronization work. */
1999 local->ops->reset_tsf(local_to_hw(local));
2000 }
2001
2002 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2003
2004
2005 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2006 ifsta->auth_alg = WLAN_AUTH_OPEN;
2007 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2008 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2009 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2010 ifsta->auth_alg = WLAN_AUTH_LEAP;
2011 else
2012 ifsta->auth_alg = WLAN_AUTH_OPEN;
Jiri Bencf0706e82007-05-05 11:45:53 -07002013 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002014 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +03002015 ifsta->assoc_scan_tries = 0;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002016 ifsta->direct_probe_tries = 0;
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +03002017 ifsta->auth_tries = 0;
2018 ifsta->assoc_tries = 0;
Tomas Winkler24e64622008-09-08 17:33:40 +02002019 netif_tx_stop_all_queues(sdata->dev);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002020 netif_carrier_off(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002021}
2022
2023
Jiri Bencf0706e82007-05-05 11:45:53 -07002024static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2025 const char *ssid, int ssid_len)
2026{
2027 int tmp, hidden_ssid;
2028
Michael Wu48225702007-10-19 17:14:36 -04002029 if (ssid_len == ifsta->ssid_len &&
2030 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07002031 return 1;
2032
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002033 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07002034 return 0;
2035
2036 hidden_ssid = 1;
2037 tmp = ssid_len;
2038 while (tmp--) {
2039 if (ssid[tmp] != '\0') {
2040 hidden_ssid = 0;
2041 break;
2042 }
2043 }
2044
Fabio Rossicb3da8c2008-11-26 22:44:23 +01002045 if (hidden_ssid && (ifsta->ssid_len == ssid_len || ssid_len == 0))
Jiri Bencf0706e82007-05-05 11:45:53 -07002046 return 1;
2047
2048 if (ssid_len == 1 && ssid[0] == ' ')
2049 return 1;
2050
2051 return 0;
2052}
2053
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002054static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002055 struct ieee80211_if_sta *ifsta)
2056{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002057 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02002058 struct ieee80211_bss *bss;
Johannes Berg8318d782008-01-24 19:38:38 +01002059 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002060 u8 bssid[ETH_ALEN], *pos;
2061 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03002062 int ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002063
2064#if 0
2065 /* Easier testing, use fixed BSSID. */
2066 memset(bssid, 0xfe, ETH_ALEN);
2067#else
2068 /* Generate random, not broadcast, locally administered BSSID. Mix in
2069 * own MAC address to make sure that devices that do not have proper
2070 * random number generator get different BSSID. */
2071 get_random_bytes(bssid, ETH_ALEN);
2072 for (i = 0; i < ETH_ALEN; i++)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002073 bssid[i] ^= sdata->dev->dev_addr[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07002074 bssid[0] &= ~0x01;
2075 bssid[0] |= 0x02;
2076#endif
2077
Johannes Berg0c68ae262008-10-27 15:56:10 -07002078 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
2079 sdata->dev->name, bssid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002080
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002081 bss = ieee80211_rx_bss_add(local, bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002082 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002083 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002084 if (!bss)
2085 return -ENOMEM;
2086
Johannes Berg8318d782008-01-24 19:38:38 +01002087 bss->band = local->hw.conf.channel->band;
2088 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002089
2090 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03002091 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -07002092 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07002093 bss->last_update = jiffies;
2094 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02002095
2096 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07002097 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02002098 else
Jiri Bencf0706e82007-05-05 11:45:53 -07002099 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02002100
Johannes Berg8318d782008-01-24 19:38:38 +01002101 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002102 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01002103 for (i = 0; i < sband->n_bitrates; i++) {
2104 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07002105 *pos++ = (u8) (rate / 5);
2106 }
2107
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002108 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02002109 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03002110 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002111}
2112
2113
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002114static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002115 struct ieee80211_if_sta *ifsta)
2116{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002117 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02002118 struct ieee80211_bss *bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002119 int found = 0;
2120 u8 bssid[ETH_ALEN];
2121 int active_ibss;
2122
2123 if (ifsta->ssid_len == 0)
2124 return -EINVAL;
2125
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002126 active_ibss = ieee80211_sta_active_ibss(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07002127#ifdef CONFIG_MAC80211_IBSS_DEBUG
2128 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002129 sdata->dev->name, active_ibss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002130#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Johannes Bergc2b13452008-09-11 00:01:55 +02002131 spin_lock_bh(&local->bss_lock);
2132 list_for_each_entry(bss, &local->bss_list, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002133 if (ifsta->ssid_len != bss->ssid_len ||
2134 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2135 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2136 continue;
2137#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -07002138 printk(KERN_DEBUG " bssid=%pM found\n", bss->bssid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002139#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2140 memcpy(bssid, bss->bssid, ETH_ALEN);
2141 found = 1;
2142 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2143 break;
2144 }
Johannes Bergc2b13452008-09-11 00:01:55 +02002145 spin_unlock_bh(&local->bss_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07002146
2147#ifdef CONFIG_MAC80211_IBSS_DEBUG
Vladimir Koutny6e438292008-07-07 14:23:01 +02002148 if (found)
Johannes Berg0c68ae262008-10-27 15:56:10 -07002149 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
2150 "%pM\n", bssid, ifsta->bssid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002151#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Daniel Drake80693ce2008-07-19 23:31:17 +01002152
2153 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03002154 int ret;
Daniel Drake80693ce2008-07-19 23:31:17 +01002155 int search_freq;
2156
2157 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2158 search_freq = bss->freq;
2159 else
2160 search_freq = local->hw.conf.channel->center_freq;
2161
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002162 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
Daniel Drake80693ce2008-07-19 23:31:17 +01002163 ifsta->ssid, ifsta->ssid_len);
2164 if (!bss)
2165 goto dont_join;
2166
Johannes Berg0c68ae262008-10-27 15:56:10 -07002167 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
Jiri Bencf0706e82007-05-05 11:45:53 -07002168 " based on configured SSID\n",
Johannes Berg0c68ae262008-10-27 15:56:10 -07002169 sdata->dev->name, bssid);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002170 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02002171 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03002172 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002173 }
Daniel Drake80693ce2008-07-19 23:31:17 +01002174
2175dont_join:
Jiri Bencf0706e82007-05-05 11:45:53 -07002176#ifdef CONFIG_MAC80211_IBSS_DEBUG
2177 printk(KERN_DEBUG " did not try to join ibss\n");
2178#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2179
2180 /* Selected IBSS not found in current scan results - try to scan */
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002181 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002182 !ieee80211_sta_active_ibss(sdata)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002183 mod_timer(&ifsta->timer, jiffies +
2184 IEEE80211_IBSS_MERGE_INTERVAL);
2185 } else if (time_after(jiffies, local->last_scan_completed +
2186 IEEE80211_SCAN_INTERVAL)) {
2187 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002188 "join\n", sdata->dev->name);
Johannes Bergc2b13452008-09-11 00:01:55 +02002189 return ieee80211_request_scan(sdata, ifsta->ssid,
Jiri Bencf0706e82007-05-05 11:45:53 -07002190 ifsta->ssid_len);
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002191 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002192 int interval = IEEE80211_SCAN_INTERVAL;
2193
2194 if (time_after(jiffies, ifsta->ibss_join_req +
2195 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002196 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002197 (!(local->oper_channel->flags &
2198 IEEE80211_CHAN_NO_IBSS)))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002199 return ieee80211_sta_create_ibss(sdata, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002200 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01002201 printk(KERN_DEBUG "%s: IBSS not allowed on"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002202 " %d MHz\n", sdata->dev->name,
Johannes Berg8318d782008-01-24 19:38:38 +01002203 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07002204 }
2205
2206 /* No IBSS found - decrease scan interval and continue
2207 * scanning. */
2208 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2209 }
2210
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002211 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002212 mod_timer(&ifsta->timer, jiffies + interval);
2213 return 0;
2214 }
2215
2216 return 0;
2217}
2218
2219
Johannes Berg60f8b392008-09-08 17:44:22 +02002220static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
2221 struct ieee80211_if_sta *ifsta)
2222{
2223 struct ieee80211_local *local = sdata->local;
Johannes Bergc2b13452008-09-11 00:01:55 +02002224 struct ieee80211_bss *bss, *selected = NULL;
Johannes Berg60f8b392008-09-08 17:44:22 +02002225 int top_rssi = 0, freq;
2226
Johannes Bergc2b13452008-09-11 00:01:55 +02002227 spin_lock_bh(&local->bss_lock);
Johannes Berg60f8b392008-09-08 17:44:22 +02002228 freq = local->oper_channel->center_freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002229 list_for_each_entry(bss, &local->bss_list, list) {
Johannes Berg60f8b392008-09-08 17:44:22 +02002230 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2231 continue;
2232
2233 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2234 IEEE80211_STA_AUTO_BSSID_SEL |
2235 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
2236 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2237 !!sdata->default_key))
2238 continue;
2239
2240 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2241 bss->freq != freq)
2242 continue;
2243
2244 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2245 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2246 continue;
2247
2248 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2249 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2250 continue;
2251
2252 if (!selected || top_rssi < bss->signal) {
2253 selected = bss;
2254 top_rssi = bss->signal;
2255 }
2256 }
2257 if (selected)
2258 atomic_inc(&selected->users);
Johannes Bergc2b13452008-09-11 00:01:55 +02002259 spin_unlock_bh(&local->bss_lock);
Johannes Berg60f8b392008-09-08 17:44:22 +02002260
2261 if (selected) {
2262 ieee80211_set_freq(sdata, selected->freq);
2263 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2264 ieee80211_sta_set_ssid(sdata, selected->ssid,
2265 selected->ssid_len);
2266 ieee80211_sta_set_bssid(sdata, selected->bssid);
Johannes Bergb079ada2008-09-09 09:32:59 +02002267 ieee80211_sta_def_wmm_params(sdata, selected);
Johannes Berg60f8b392008-09-08 17:44:22 +02002268
2269 /* Send out direct probe if no probe resp was received or
2270 * the one we have is outdated
2271 */
2272 if (!selected->last_probe_resp ||
2273 time_after(jiffies, selected->last_probe_resp
2274 + IEEE80211_SCAN_RESULT_EXPIRE))
2275 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2276 else
2277 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2278
2279 ieee80211_rx_bss_put(local, selected);
2280 ieee80211_sta_reset_auth(sdata, ifsta);
2281 return 0;
2282 } else {
2283 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2284 ifsta->assoc_scan_tries++;
2285 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
Johannes Bergc2b13452008-09-11 00:01:55 +02002286 ieee80211_start_scan(sdata, NULL, 0);
Johannes Berg60f8b392008-09-08 17:44:22 +02002287 else
Johannes Bergc2b13452008-09-11 00:01:55 +02002288 ieee80211_start_scan(sdata, ifsta->ssid,
Johannes Berg60f8b392008-09-08 17:44:22 +02002289 ifsta->ssid_len);
2290 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2291 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2292 } else
2293 ifsta->state = IEEE80211_STA_MLME_DISABLED;
2294 }
2295 return -1;
2296}
2297
2298
Johannes Berg9c6bd792008-09-11 00:01:52 +02002299static void ieee80211_sta_work(struct work_struct *work)
Johannes Berg60f8b392008-09-08 17:44:22 +02002300{
2301 struct ieee80211_sub_if_data *sdata =
2302 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2303 struct ieee80211_local *local = sdata->local;
2304 struct ieee80211_if_sta *ifsta;
2305 struct sk_buff *skb;
2306
2307 if (!netif_running(sdata->dev))
2308 return;
2309
Johannes Bergc2b13452008-09-11 00:01:55 +02002310 if (local->sw_scanning || local->hw_scanning)
Johannes Berg60f8b392008-09-08 17:44:22 +02002311 return;
2312
Johannes Berg05c914f2008-09-11 00:01:58 +02002313 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION &&
2314 sdata->vif.type != NL80211_IFTYPE_ADHOC))
Johannes Berg60f8b392008-09-08 17:44:22 +02002315 return;
2316 ifsta = &sdata->u.sta;
2317
2318 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2319 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2320
Johannes Berg60f8b392008-09-08 17:44:22 +02002321 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
2322 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
2323 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
2324 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Johannes Bergc2b13452008-09-11 00:01:55 +02002325 ieee80211_start_scan(sdata, ifsta->scan_ssid,
2326 ifsta->scan_ssid_len);
Johannes Berg60f8b392008-09-08 17:44:22 +02002327 return;
2328 }
2329
2330 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2331 if (ieee80211_sta_config_auth(sdata, ifsta))
2332 return;
2333 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2334 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2335 return;
2336
2337 switch (ifsta->state) {
2338 case IEEE80211_STA_MLME_DISABLED:
2339 break;
2340 case IEEE80211_STA_MLME_DIRECT_PROBE:
2341 ieee80211_direct_probe(sdata, ifsta);
2342 break;
2343 case IEEE80211_STA_MLME_AUTHENTICATE:
2344 ieee80211_authenticate(sdata, ifsta);
2345 break;
2346 case IEEE80211_STA_MLME_ASSOCIATE:
2347 ieee80211_associate(sdata, ifsta);
2348 break;
2349 case IEEE80211_STA_MLME_ASSOCIATED:
2350 ieee80211_associated(sdata, ifsta);
2351 break;
2352 case IEEE80211_STA_MLME_IBSS_SEARCH:
2353 ieee80211_sta_find_ibss(sdata, ifsta);
2354 break;
2355 case IEEE80211_STA_MLME_IBSS_JOINED:
2356 ieee80211_sta_merge_ibss(sdata, ifsta);
2357 break;
Johannes Berg60f8b392008-09-08 17:44:22 +02002358 default:
2359 WARN_ON(1);
2360 break;
2361 }
2362
2363 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
2364 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2365 "mixed-cell disabled - disassociate\n", sdata->dev->name);
2366
2367 ieee80211_set_disassoc(sdata, ifsta, false, true,
2368 WLAN_REASON_UNSPECIFIED);
2369 }
2370}
Johannes Berg0a51b272008-09-08 17:44:25 +02002371
Johannes Berga1678f82008-09-11 00:01:47 +02002372static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2373{
Johannes Berg05c914f2008-09-11 00:01:58 +02002374 if (sdata->vif.type == NL80211_IFTYPE_STATION)
Johannes Berg7c950692008-09-11 00:01:48 +02002375 queue_work(sdata->local->hw.workqueue,
2376 &sdata->u.sta.work);
Johannes Berga1678f82008-09-11 00:01:47 +02002377}
2378
Johannes Berg9c6bd792008-09-11 00:01:52 +02002379/* interface setup */
2380void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2381{
2382 struct ieee80211_if_sta *ifsta;
2383
2384 ifsta = &sdata->u.sta;
2385 INIT_WORK(&ifsta->work, ieee80211_sta_work);
2386 setup_timer(&ifsta->timer, ieee80211_sta_timer,
2387 (unsigned long) sdata);
2388 skb_queue_head_init(&ifsta->skb_queue);
2389
2390 ifsta->capab = WLAN_CAPABILITY_ESS;
2391 ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
2392 IEEE80211_AUTH_ALG_SHARED_KEY;
2393 ifsta->flags |= IEEE80211_STA_CREATE_IBSS |
2394 IEEE80211_STA_AUTO_BSSID_SEL |
2395 IEEE80211_STA_AUTO_CHANNEL_SEL;
2396 if (ieee80211_num_regular_queues(&sdata->local->hw) >= 4)
2397 ifsta->flags |= IEEE80211_STA_WMM_ENABLED;
2398}
2399
2400/*
2401 * Add a new IBSS station, will also be called by the RX code when,
2402 * in IBSS mode, receiving a frame from a yet-unknown station, hence
2403 * must be callable in atomic context.
2404 */
2405struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
Rami Rosenab1f5c02008-12-11 14:00:25 +02002406 u8 *bssid,u8 *addr, u64 supp_rates)
Johannes Berg9c6bd792008-09-11 00:01:52 +02002407{
2408 struct ieee80211_local *local = sdata->local;
2409 struct sta_info *sta;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002410 int band = local->hw.conf.channel->band;
2411
2412 /* TODO: Could consider removing the least recently used entry and
2413 * allow new one to be added. */
2414 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2415 if (net_ratelimit()) {
2416 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Johannes Berg0c68ae262008-10-27 15:56:10 -07002417 "entry %pM\n", sdata->dev->name, addr);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002418 }
2419 return NULL;
2420 }
2421
2422 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
2423 return NULL;
2424
2425#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -07002426 printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
2427 wiphy_name(local->hw.wiphy), addr, sdata->dev->name);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002428#endif
2429
2430 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
2431 if (!sta)
2432 return NULL;
2433
2434 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
2435
2436 /* make sure mandatory rates are always added */
Johannes Berg323ce792008-09-11 02:45:11 +02002437 sta->sta.supp_rates[band] = supp_rates |
Johannes Berg96dd22a2008-09-11 00:01:57 +02002438 ieee80211_mandatory_rates(local, band);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002439
Johannes Berg4b7679a2008-09-18 18:14:18 +02002440 rate_control_rate_init(sta);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002441
2442 if (sta_info_insert(sta))
2443 return NULL;
2444
2445 return sta;
2446}
2447
2448/* configuration hooks */
2449void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
2450 struct ieee80211_if_sta *ifsta)
2451{
2452 struct ieee80211_local *local = sdata->local;
2453
Johannes Berg05c914f2008-09-11 00:01:58 +02002454 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg9c6bd792008-09-11 00:01:52 +02002455 return;
2456
2457 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2458 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2459 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2460 IEEE80211_STA_AUTO_SSID_SEL))) {
2461
2462 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
2463 ieee80211_set_disassoc(sdata, ifsta, true, true,
2464 WLAN_REASON_DEAUTH_LEAVING);
2465
2466 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2467 queue_work(local->hw.workqueue, &ifsta->work);
2468 }
2469}
2470
2471int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
2472{
2473 struct ieee80211_if_sta *ifsta;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002474
2475 if (len > IEEE80211_MAX_SSID_LEN)
2476 return -EINVAL;
2477
2478 ifsta = &sdata->u.sta;
2479
2480 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
2481 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
2482 memcpy(ifsta->ssid, ssid, len);
2483 ifsta->ssid_len = len;
2484 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002485 }
2486
2487 if (len)
2488 ifsta->flags |= IEEE80211_STA_SSID_SET;
2489 else
2490 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2491
Johannes Berg05c914f2008-09-11 00:01:58 +02002492 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg9c6bd792008-09-11 00:01:52 +02002493 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2494 ifsta->ibss_join_req = jiffies;
2495 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2496 return ieee80211_sta_find_ibss(sdata, ifsta);
2497 }
2498
2499 return 0;
2500}
2501
2502int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2503{
2504 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2505 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2506 *len = ifsta->ssid_len;
2507 return 0;
2508}
2509
2510int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2511{
2512 struct ieee80211_if_sta *ifsta;
2513 int res;
2514
2515 ifsta = &sdata->u.sta;
2516
2517 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2518 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2519 res = 0;
2520 /*
2521 * Hack! See also ieee80211_sta_set_ssid.
2522 */
2523 if (netif_running(sdata->dev))
2524 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2525 if (res) {
2526 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2527 "the low-level driver\n", sdata->dev->name);
2528 return res;
2529 }
2530 }
2531
2532 if (is_valid_ether_addr(bssid))
2533 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2534 else
2535 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2536
2537 return 0;
2538}
2539
2540int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
2541{
2542 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2543
2544 kfree(ifsta->extra_ie);
2545 if (len == 0) {
2546 ifsta->extra_ie = NULL;
2547 ifsta->extra_ie_len = 0;
2548 return 0;
2549 }
2550 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2551 if (!ifsta->extra_ie) {
2552 ifsta->extra_ie_len = 0;
2553 return -ENOMEM;
2554 }
2555 memcpy(ifsta->extra_ie, ie, len);
2556 ifsta->extra_ie_len = len;
2557 return 0;
2558}
2559
2560int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2561{
2562 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2563
2564 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2565 sdata->dev->name, reason);
2566
Johannes Berg05c914f2008-09-11 00:01:58 +02002567 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2568 sdata->vif.type != NL80211_IFTYPE_ADHOC)
Johannes Berg9c6bd792008-09-11 00:01:52 +02002569 return -EINVAL;
2570
2571 ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
2572 return 0;
2573}
2574
2575int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2576{
2577 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2578
2579 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2580 sdata->dev->name, reason);
2581
Johannes Berg05c914f2008-09-11 00:01:58 +02002582 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg9c6bd792008-09-11 00:01:52 +02002583 return -EINVAL;
2584
2585 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
2586 return -1;
2587
2588 ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
2589 return 0;
2590}
2591
2592/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02002593void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2594{
2595 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2596 struct ieee80211_if_sta *ifsta;
2597
Johannes Berg05c914f2008-09-11 00:01:58 +02002598 if (sdata && sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg0a51b272008-09-08 17:44:25 +02002599 ifsta = &sdata->u.sta;
2600 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2601 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
2602 !ieee80211_sta_active_ibss(sdata)))
2603 ieee80211_sta_find_ibss(sdata, ifsta);
2604 }
Johannes Berga1678f82008-09-11 00:01:47 +02002605
2606 /* Restart STA timers */
2607 rcu_read_lock();
2608 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2609 ieee80211_restart_sta_timer(sdata);
2610 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02002611}
Kalle Valo520eb822008-12-18 23:35:27 +02002612
2613void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
2614{
2615 struct ieee80211_local *local =
2616 container_of(work, struct ieee80211_local,
2617 dynamic_ps_disable_work);
2618
2619 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2620 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2621 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2622 }
2623
2624 ieee80211_wake_queues_by_reason(&local->hw,
2625 IEEE80211_QUEUE_STOP_REASON_PS);
2626}
2627
2628void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
2629{
2630 struct ieee80211_local *local =
2631 container_of(work, struct ieee80211_local,
2632 dynamic_ps_enable_work);
2633
2634 if (local->hw.conf.flags & IEEE80211_CONF_PS)
2635 return;
2636
2637 local->hw.conf.flags |= IEEE80211_CONF_PS;
2638
2639 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2640}
2641
2642void ieee80211_dynamic_ps_timer(unsigned long data)
2643{
2644 struct ieee80211_local *local = (void *) data;
2645
2646 queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
2647}