blob: a8251a23917bc160f95a8779e0020a2814537a8a [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070027#include <net/iw_handler.h>
28#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070029
30#include <net/mac80211.h>
31#include "ieee80211_i.h"
32#include "ieee80211_rate.h"
Michael Buesch47f0c502007-09-27 15:10:44 +020033#include "ieee80211_led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070034
35#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
36#define IEEE80211_AUTH_MAX_TRIES 3
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_MAX_TRIES 3
39#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
40#define IEEE80211_PROBE_INTERVAL (60 * HZ)
41#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
42#define IEEE80211_SCAN_INTERVAL (2 * HZ)
43#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
44#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
45
46#define IEEE80211_PROBE_DELAY (HZ / 33)
47#define IEEE80211_CHANNEL_TIME (HZ / 33)
48#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
49#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
50#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
51#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
52
53#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
54
55
56#define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
57
58#define ERP_INFO_USE_PROTECTION BIT(1)
59
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020060/* mgmt header + 1 byte action code */
61#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
62
63#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
64#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
65#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020066#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
67#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020068
Ron Rindjunsky07db2182007-12-25 17:00:33 +020069/* next values represent the buffer size for A-MPDU frame.
70 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
71#define IEEE80211_MIN_AMPDU_BUF 0x8
72#define IEEE80211_MAX_AMPDU_BUF 0x40
73
Jiri Bencf0706e82007-05-05 11:45:53 -070074static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
75 u8 *ssid, size_t ssid_len);
76static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010077ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040078 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070079static void ieee80211_rx_bss_put(struct net_device *dev,
80 struct ieee80211_sta_bss *bss);
81static int ieee80211_sta_find_ibss(struct net_device *dev,
82 struct ieee80211_if_sta *ifsta);
83static int ieee80211_sta_wep_configured(struct net_device *dev);
84static int ieee80211_sta_start_scan(struct net_device *dev,
85 u8 *ssid, size_t ssid_len);
86static int ieee80211_sta_config_auth(struct net_device *dev,
87 struct ieee80211_if_sta *ifsta);
88
89
90/* Parsed Information Elements */
91struct ieee802_11_elems {
Johannes Berg55582352007-07-10 19:32:09 +020092 /* pointers to IEs */
Jiri Bencf0706e82007-05-05 11:45:53 -070093 u8 *ssid;
Jiri Bencf0706e82007-05-05 11:45:53 -070094 u8 *supp_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -070095 u8 *fh_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070096 u8 *ds_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070097 u8 *cf_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070098 u8 *tim;
Jiri Bencf0706e82007-05-05 11:45:53 -070099 u8 *ibss_params;
Jiri Bencf0706e82007-05-05 11:45:53 -0700100 u8 *challenge;
Jiri Bencf0706e82007-05-05 11:45:53 -0700101 u8 *wpa;
Jiri Bencf0706e82007-05-05 11:45:53 -0700102 u8 *rsn;
Jiri Bencf0706e82007-05-05 11:45:53 -0700103 u8 *erp_info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700104 u8 *ext_supp_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700105 u8 *wmm_info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700106 u8 *wmm_param;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200107 u8 *ht_cap_elem;
108 u8 *ht_info_elem;
Johannes Berg55582352007-07-10 19:32:09 +0200109 /* length of them, respectively */
110 u8 ssid_len;
111 u8 supp_rates_len;
112 u8 fh_params_len;
113 u8 ds_params_len;
114 u8 cf_params_len;
115 u8 tim_len;
116 u8 ibss_params_len;
117 u8 challenge_len;
118 u8 wpa_len;
119 u8 rsn_len;
120 u8 erp_info_len;
121 u8 ext_supp_rates_len;
122 u8 wmm_info_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700123 u8 wmm_param_len;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200124 u8 ht_cap_elem_len;
125 u8 ht_info_elem_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700126};
127
John W. Linville67a4cce2007-10-12 16:40:37 -0400128static void ieee802_11_parse_elems(u8 *start, size_t len,
129 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -0700130{
131 size_t left = len;
132 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -0700133
134 memset(elems, 0, sizeof(*elems));
135
136 while (left >= 2) {
137 u8 id, elen;
138
139 id = *pos++;
140 elen = *pos++;
141 left -= 2;
142
John W. Linville67a4cce2007-10-12 16:40:37 -0400143 if (elen > left)
144 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700145
146 switch (id) {
147 case WLAN_EID_SSID:
148 elems->ssid = pos;
149 elems->ssid_len = elen;
150 break;
151 case WLAN_EID_SUPP_RATES:
152 elems->supp_rates = pos;
153 elems->supp_rates_len = elen;
154 break;
155 case WLAN_EID_FH_PARAMS:
156 elems->fh_params = pos;
157 elems->fh_params_len = elen;
158 break;
159 case WLAN_EID_DS_PARAMS:
160 elems->ds_params = pos;
161 elems->ds_params_len = elen;
162 break;
163 case WLAN_EID_CF_PARAMS:
164 elems->cf_params = pos;
165 elems->cf_params_len = elen;
166 break;
167 case WLAN_EID_TIM:
168 elems->tim = pos;
169 elems->tim_len = elen;
170 break;
171 case WLAN_EID_IBSS_PARAMS:
172 elems->ibss_params = pos;
173 elems->ibss_params_len = elen;
174 break;
175 case WLAN_EID_CHALLENGE:
176 elems->challenge = pos;
177 elems->challenge_len = elen;
178 break;
179 case WLAN_EID_WPA:
180 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
181 pos[2] == 0xf2) {
182 /* Microsoft OUI (00:50:F2) */
183 if (pos[3] == 1) {
184 /* OUI Type 1 - WPA IE */
185 elems->wpa = pos;
186 elems->wpa_len = elen;
187 } else if (elen >= 5 && pos[3] == 2) {
188 if (pos[4] == 0) {
189 elems->wmm_info = pos;
190 elems->wmm_info_len = elen;
191 } else if (pos[4] == 1) {
192 elems->wmm_param = pos;
193 elems->wmm_param_len = elen;
194 }
195 }
196 }
197 break;
198 case WLAN_EID_RSN:
199 elems->rsn = pos;
200 elems->rsn_len = elen;
201 break;
202 case WLAN_EID_ERP_INFO:
203 elems->erp_info = pos;
204 elems->erp_info_len = elen;
205 break;
206 case WLAN_EID_EXT_SUPP_RATES:
207 elems->ext_supp_rates = pos;
208 elems->ext_supp_rates_len = elen;
209 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200210 case WLAN_EID_HT_CAPABILITY:
211 elems->ht_cap_elem = pos;
212 elems->ht_cap_elem_len = elen;
213 break;
214 case WLAN_EID_HT_EXTRA_INFO:
215 elems->ht_info_elem = pos;
216 elems->ht_info_elem_len = elen;
217 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700218 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700219 break;
220 }
221
222 left -= elen;
223 pos += elen;
224 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700225}
226
227
Jiri Bencf0706e82007-05-05 11:45:53 -0700228static int ecw2cw(int ecw)
229{
230 int cw = 1;
231 while (ecw > 0) {
232 cw <<= 1;
233 ecw--;
234 }
235 return cw - 1;
236}
237
Jiri Bencf0706e82007-05-05 11:45:53 -0700238static void ieee80211_sta_wmm_params(struct net_device *dev,
239 struct ieee80211_if_sta *ifsta,
240 u8 *wmm_param, size_t wmm_param_len)
241{
242 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
243 struct ieee80211_tx_queue_params params;
244 size_t left;
245 int count;
246 u8 *pos;
247
248 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
249 return;
250 count = wmm_param[6] & 0x0f;
251 if (count == ifsta->wmm_last_param_set)
252 return;
253 ifsta->wmm_last_param_set = count;
254
255 pos = wmm_param + 8;
256 left = wmm_param_len - 8;
257
258 memset(&params, 0, sizeof(params));
259
260 if (!local->ops->conf_tx)
261 return;
262
263 local->wmm_acm = 0;
264 for (; left >= 4; left -= 4, pos += 4) {
265 int aci = (pos[0] >> 5) & 0x03;
266 int acm = (pos[0] >> 4) & 0x01;
267 int queue;
268
269 switch (aci) {
270 case 1:
271 queue = IEEE80211_TX_QUEUE_DATA3;
272 if (acm) {
273 local->wmm_acm |= BIT(0) | BIT(3);
274 }
275 break;
276 case 2:
277 queue = IEEE80211_TX_QUEUE_DATA1;
278 if (acm) {
279 local->wmm_acm |= BIT(4) | BIT(5);
280 }
281 break;
282 case 3:
283 queue = IEEE80211_TX_QUEUE_DATA0;
284 if (acm) {
285 local->wmm_acm |= BIT(6) | BIT(7);
286 }
287 break;
288 case 0:
289 default:
290 queue = IEEE80211_TX_QUEUE_DATA2;
291 if (acm) {
292 local->wmm_acm |= BIT(1) | BIT(2);
293 }
294 break;
295 }
296
297 params.aifs = pos[0] & 0x0f;
298 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
299 params.cw_min = ecw2cw(pos[1] & 0x0f);
300 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
301 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
302 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
303 "cWmin=%d cWmax=%d burst=%d\n",
304 dev->name, queue, aci, acm, params.aifs, params.cw_min,
305 params.cw_max, params.burst_time);
306 /* TODO: handle ACM (block TX, fallback to next lowest allowed
307 * AC for now) */
308 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
309 printk(KERN_DEBUG "%s: failed to set TX queue "
310 "parameters for queue %d\n", dev->name, queue);
311 }
312 }
313}
314
315
Johannes Berg471b3ef2007-12-28 14:32:58 +0100316static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
317 u8 erp_value)
Daniel Drake56282212007-07-10 19:32:10 +0200318{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100319 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200320 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100321 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
322 bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Joe Perches0795af52007-10-03 17:59:30 -0700323 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100324 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200325
Johannes Berg471b3ef2007-12-28 14:32:58 +0100326 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200327 if (net_ratelimit()) {
328 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700329 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100330 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200331 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700332 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200333 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100334 bss_conf->use_cts_prot = use_protection;
335 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200336 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200337
Johannes Berg471b3ef2007-12-28 14:32:58 +0100338 if (preamble_mode != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200339 if (net_ratelimit()) {
340 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700341 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100342 sdata->dev->name,
Daniel Drake7e9ed182007-07-27 15:43:24 +0200343 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
344 "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700345 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200346 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100347 bss_conf->use_short_preamble = preamble_mode;
348 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200349 }
Daniel Draked9430a32007-07-27 15:43:24 +0200350
Johannes Berg471b3ef2007-12-28 14:32:58 +0100351 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200352}
353
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200354int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
355 struct ieee80211_ht_info *ht_info)
356{
357
358 if (ht_info == NULL)
359 return -EINVAL;
360
361 memset(ht_info, 0, sizeof(*ht_info));
362
363 if (ht_cap_ie) {
364 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
365
366 ht_info->ht_supported = 1;
367 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
368 ht_info->ampdu_factor =
369 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
370 ht_info->ampdu_density =
371 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
372 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
373 } else
374 ht_info->ht_supported = 0;
375
376 return 0;
377}
378
379int ieee80211_ht_addt_info_ie_to_ht_bss_info(
380 struct ieee80211_ht_addt_info *ht_add_info_ie,
381 struct ieee80211_ht_bss_info *bss_info)
382{
383 if (bss_info == NULL)
384 return -EINVAL;
385
386 memset(bss_info, 0, sizeof(*bss_info));
387
388 if (ht_add_info_ie) {
389 u16 op_mode;
390 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
391
392 bss_info->primary_channel = ht_add_info_ie->control_chan;
393 bss_info->bss_cap = ht_add_info_ie->ht_param;
394 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
395 }
396
397 return 0;
398}
Daniel Drake56282212007-07-10 19:32:10 +0200399
Jiri Bencf0706e82007-05-05 11:45:53 -0700400static void ieee80211_sta_send_associnfo(struct net_device *dev,
401 struct ieee80211_if_sta *ifsta)
402{
403 char *buf;
404 size_t len;
405 int i;
406 union iwreq_data wrqu;
407
408 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
409 return;
410
411 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200412 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700413 if (!buf)
414 return;
415
416 len = sprintf(buf, "ASSOCINFO(");
417 if (ifsta->assocreq_ies) {
418 len += sprintf(buf + len, "ReqIEs=");
419 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
420 len += sprintf(buf + len, "%02x",
421 ifsta->assocreq_ies[i]);
422 }
423 }
424 if (ifsta->assocresp_ies) {
425 if (ifsta->assocreq_ies)
426 len += sprintf(buf + len, " ");
427 len += sprintf(buf + len, "RespIEs=");
428 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
429 len += sprintf(buf + len, "%02x",
430 ifsta->assocresp_ies[i]);
431 }
432 }
433 len += sprintf(buf + len, ")");
434
435 if (len > IW_CUSTOM_MAX) {
436 len = sprintf(buf, "ASSOCRESPIE=");
437 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
438 len += sprintf(buf + len, "%02x",
439 ifsta->assocresp_ies[i]);
440 }
441 }
442
443 memset(&wrqu, 0, sizeof(wrqu));
444 wrqu.data.length = len;
445 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
446
447 kfree(buf);
448}
449
450
451static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400452 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200453 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700454{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100455 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
456 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700457 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100458 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700459
Jiri Bencf0706e82007-05-05 11:45:53 -0700460 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200461 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400462
463 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
464
Johannes Berg51fb61e2007-12-19 01:31:27 +0100465 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700466 return;
Daniel Drake56282212007-07-10 19:32:10 +0200467
John W. Linville65c107a2007-10-05 14:23:27 -0400468 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100469 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400470 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200471 if (bss) {
472 if (bss->has_erp_value)
Johannes Berg471b3ef2007-12-28 14:32:58 +0100473 changed |= ieee80211_handle_erp_ie(
474 sdata, bss->erp_value);
Daniel Drake56282212007-07-10 19:32:10 +0200475 ieee80211_rx_bss_put(dev, bss);
476 }
477
Jiri Bencf0706e82007-05-05 11:45:53 -0700478 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400479 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700480 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
481 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
482 ieee80211_sta_send_associnfo(dev, ifsta);
483 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400484 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
485
Jiri Bencf0706e82007-05-05 11:45:53 -0700486 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200487 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700488 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
489 }
490 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
491 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
492 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200493 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100494
Tomas Winklerb2205252008-01-27 16:18:22 +0200495 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100496 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -0700497}
498
499static void ieee80211_set_disassoc(struct net_device *dev,
500 struct ieee80211_if_sta *ifsta, int deauth)
501{
502 if (deauth)
503 ifsta->auth_tries = 0;
504 ifsta->assoc_tries = 0;
505 ieee80211_set_associated(dev, ifsta, 0);
506}
507
508static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
509 int encrypt)
510{
511 struct ieee80211_sub_if_data *sdata;
512 struct ieee80211_tx_packet_data *pkt_data;
513
514 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
515 skb->dev = sdata->local->mdev;
516 skb_set_mac_header(skb, 0);
517 skb_set_network_header(skb, 0);
518 skb_set_transport_header(skb, 0);
519
520 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
521 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
522 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400523 if (!encrypt)
524 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700525
526 dev_queue_xmit(skb);
527}
528
529
530static void ieee80211_send_auth(struct net_device *dev,
531 struct ieee80211_if_sta *ifsta,
532 int transaction, u8 *extra, size_t extra_len,
533 int encrypt)
534{
535 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
536 struct sk_buff *skb;
537 struct ieee80211_mgmt *mgmt;
538
539 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
540 sizeof(*mgmt) + 6 + extra_len);
541 if (!skb) {
542 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
543 "frame\n", dev->name);
544 return;
545 }
546 skb_reserve(skb, local->hw.extra_tx_headroom);
547
548 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
549 memset(mgmt, 0, 24 + 6);
550 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
551 IEEE80211_STYPE_AUTH);
552 if (encrypt)
553 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
554 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
555 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
556 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
557 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
558 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
559 ifsta->auth_transaction = transaction + 1;
560 mgmt->u.auth.status_code = cpu_to_le16(0);
561 if (extra)
562 memcpy(skb_put(skb, extra_len), extra, extra_len);
563
564 ieee80211_sta_tx(dev, skb, encrypt);
565}
566
567
568static void ieee80211_authenticate(struct net_device *dev,
569 struct ieee80211_if_sta *ifsta)
570{
Joe Perches0795af52007-10-03 17:59:30 -0700571 DECLARE_MAC_BUF(mac);
572
Jiri Bencf0706e82007-05-05 11:45:53 -0700573 ifsta->auth_tries++;
574 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700575 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700576 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700577 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700578 ifsta->state = IEEE80211_DISABLED;
579 return;
580 }
581
582 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700583 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
584 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700585
586 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
587
588 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
589}
590
591
592static void ieee80211_send_assoc(struct net_device *dev,
593 struct ieee80211_if_sta *ifsta)
594{
595 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700596 struct sk_buff *skb;
597 struct ieee80211_mgmt *mgmt;
598 u8 *pos, *ies;
599 int i, len;
600 u16 capab;
601 struct ieee80211_sta_bss *bss;
602 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100603 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700604
605 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
606 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
607 ifsta->ssid_len);
608 if (!skb) {
609 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
610 "frame\n", dev->name);
611 return;
612 }
613 skb_reserve(skb, local->hw.extra_tx_headroom);
614
Johannes Berg8318d782008-01-24 19:38:38 +0100615 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
616
Jiri Bencf0706e82007-05-05 11:45:53 -0700617 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100618
619 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
620 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
621 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
622 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
623 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700624 }
Johannes Berg8318d782008-01-24 19:38:38 +0100625
626 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
627 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400628 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700629 if (bss) {
630 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
631 capab |= WLAN_CAPABILITY_PRIVACY;
632 if (bss->wmm_ie) {
633 wmm = 1;
634 }
635 ieee80211_rx_bss_put(dev, bss);
636 }
637
638 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
639 memset(mgmt, 0, 24);
640 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
641 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
642 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
643
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400644 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700645 skb_put(skb, 10);
646 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
647 IEEE80211_STYPE_REASSOC_REQ);
648 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
649 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
650 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
651 ETH_ALEN);
652 } else {
653 skb_put(skb, 4);
654 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
655 IEEE80211_STYPE_ASSOC_REQ);
656 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
657 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
658 }
659
660 /* SSID */
661 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
662 *pos++ = WLAN_EID_SSID;
663 *pos++ = ifsta->ssid_len;
664 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
665
Johannes Berg8318d782008-01-24 19:38:38 +0100666 len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700667 if (len > 8)
668 len = 8;
669 pos = skb_put(skb, len + 2);
670 *pos++ = WLAN_EID_SUPP_RATES;
671 *pos++ = len;
672 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100673 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700674 *pos++ = (u8) (rate / 5);
675 }
676
Johannes Berg8318d782008-01-24 19:38:38 +0100677 if (sband->n_bitrates > len) {
678 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700679 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100680 *pos++ = sband->n_bitrates - len;
681 for (i = len; i < sband->n_bitrates; i++) {
682 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700683 *pos++ = (u8) (rate / 5);
684 }
685 }
686
687 if (ifsta->extra_ie) {
688 pos = skb_put(skb, ifsta->extra_ie_len);
689 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
690 }
691
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400692 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700693 pos = skb_put(skb, 9);
694 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
695 *pos++ = 7; /* len */
696 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
697 *pos++ = 0x50;
698 *pos++ = 0xf2;
699 *pos++ = 2; /* WME */
700 *pos++ = 0; /* WME info */
701 *pos++ = 1; /* WME ver */
702 *pos++ = 0;
703 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200704 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100705 if (wmm && sband->ht_info.ht_supported) {
706 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200707 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
708 *pos++ = WLAN_EID_HT_CAPABILITY;
709 *pos++ = sizeof(struct ieee80211_ht_cap);
710 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
711 memcpy(pos, &tmp, sizeof(u16));
712 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100713 /* TODO: needs a define here for << 2 */
714 *pos++ = sband->ht_info.ampdu_factor |
715 (sband->ht_info.ampdu_density << 2);
716 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200717 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700718
719 kfree(ifsta->assocreq_ies);
720 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200721 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700722 if (ifsta->assocreq_ies)
723 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
724
725 ieee80211_sta_tx(dev, skb, 0);
726}
727
728
729static void ieee80211_send_deauth(struct net_device *dev,
730 struct ieee80211_if_sta *ifsta, u16 reason)
731{
732 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
733 struct sk_buff *skb;
734 struct ieee80211_mgmt *mgmt;
735
736 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
737 if (!skb) {
738 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
739 "frame\n", dev->name);
740 return;
741 }
742 skb_reserve(skb, local->hw.extra_tx_headroom);
743
744 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
745 memset(mgmt, 0, 24);
746 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
747 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
748 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
749 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
750 IEEE80211_STYPE_DEAUTH);
751 skb_put(skb, 2);
752 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
753
754 ieee80211_sta_tx(dev, skb, 0);
755}
756
757
758static void ieee80211_send_disassoc(struct net_device *dev,
759 struct ieee80211_if_sta *ifsta, u16 reason)
760{
761 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
762 struct sk_buff *skb;
763 struct ieee80211_mgmt *mgmt;
764
765 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
766 if (!skb) {
767 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
768 "frame\n", dev->name);
769 return;
770 }
771 skb_reserve(skb, local->hw.extra_tx_headroom);
772
773 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
774 memset(mgmt, 0, 24);
775 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
776 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
777 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
778 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
779 IEEE80211_STYPE_DISASSOC);
780 skb_put(skb, 2);
781 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
782
783 ieee80211_sta_tx(dev, skb, 0);
784}
785
786
787static int ieee80211_privacy_mismatch(struct net_device *dev,
788 struct ieee80211_if_sta *ifsta)
789{
John W. Linville65c107a2007-10-05 14:23:27 -0400790 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700791 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000792 int bss_privacy;
793 int wep_privacy;
794 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700795
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000796 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700797 return 0;
798
Johannes Berg8318d782008-01-24 19:38:38 +0100799 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
800 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400801 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700802 if (!bss)
803 return 0;
804
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000805 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
806 wep_privacy = !!ieee80211_sta_wep_configured(dev);
807 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700808
809 ieee80211_rx_bss_put(dev, bss);
810
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000811 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
812 return 0;
813
814 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700815}
816
817
818static void ieee80211_associate(struct net_device *dev,
819 struct ieee80211_if_sta *ifsta)
820{
Joe Perches0795af52007-10-03 17:59:30 -0700821 DECLARE_MAC_BUF(mac);
822
Jiri Bencf0706e82007-05-05 11:45:53 -0700823 ifsta->assoc_tries++;
824 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700825 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700826 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700827 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700828 ifsta->state = IEEE80211_DISABLED;
829 return;
830 }
831
832 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700833 printk(KERN_DEBUG "%s: associate with AP %s\n",
834 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700835 if (ieee80211_privacy_mismatch(dev, ifsta)) {
836 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
837 "mixed-cell disabled - abort association\n", dev->name);
838 ifsta->state = IEEE80211_DISABLED;
839 return;
840 }
841
842 ieee80211_send_assoc(dev, ifsta);
843
844 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
845}
846
847
848static void ieee80211_associated(struct net_device *dev,
849 struct ieee80211_if_sta *ifsta)
850{
851 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
852 struct sta_info *sta;
853 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700854 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700855
856 /* TODO: start monitoring current AP signal quality and number of
857 * missed beacons. Scan other channels every now and then and search
858 * for better APs. */
859 /* TODO: remove expired BSSes */
860
861 ifsta->state = IEEE80211_ASSOCIATED;
862
863 sta = sta_info_get(local, ifsta->bssid);
864 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700865 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
866 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700867 disassoc = 1;
868 } else {
869 disassoc = 0;
870 if (time_after(jiffies,
871 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400872 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700873 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700874 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700875 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700876 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700877 disassoc = 1;
Michael Wube8755e2007-07-27 15:43:23 +0200878 sta_info_free(sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400879 } else
Jiri Bencf0706e82007-05-05 11:45:53 -0700880 ieee80211_send_probe_req(dev, ifsta->bssid,
881 local->scan_ssid,
882 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400883 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700884 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400885 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700886 if (time_after(jiffies, ifsta->last_probe +
887 IEEE80211_PROBE_INTERVAL)) {
888 ifsta->last_probe = jiffies;
889 ieee80211_send_probe_req(dev, ifsta->bssid,
890 ifsta->ssid,
891 ifsta->ssid_len);
892 }
893 }
894 sta_info_put(sta);
895 }
896 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500897 ifsta->state = IEEE80211_DISABLED;
898 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700899 } else {
900 mod_timer(&ifsta->timer, jiffies +
901 IEEE80211_MONITORING_INTERVAL);
902 }
903}
904
905
906static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
907 u8 *ssid, size_t ssid_len)
908{
909 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100910 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700911 struct sk_buff *skb;
912 struct ieee80211_mgmt *mgmt;
913 u8 *pos, *supp_rates, *esupp_rates = NULL;
914 int i;
915
916 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
917 if (!skb) {
918 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
919 "request\n", dev->name);
920 return;
921 }
922 skb_reserve(skb, local->hw.extra_tx_headroom);
923
924 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
925 memset(mgmt, 0, 24);
926 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
927 IEEE80211_STYPE_PROBE_REQ);
928 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
929 if (dst) {
930 memcpy(mgmt->da, dst, ETH_ALEN);
931 memcpy(mgmt->bssid, dst, ETH_ALEN);
932 } else {
933 memset(mgmt->da, 0xff, ETH_ALEN);
934 memset(mgmt->bssid, 0xff, ETH_ALEN);
935 }
936 pos = skb_put(skb, 2 + ssid_len);
937 *pos++ = WLAN_EID_SSID;
938 *pos++ = ssid_len;
939 memcpy(pos, ssid, ssid_len);
940
941 supp_rates = skb_put(skb, 2);
942 supp_rates[0] = WLAN_EID_SUPP_RATES;
943 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100944 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
945
946 for (i = 0; i < sband->n_bitrates; i++) {
947 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -0700948 if (esupp_rates) {
949 pos = skb_put(skb, 1);
950 esupp_rates[1]++;
951 } else if (supp_rates[1] == 8) {
952 esupp_rates = skb_put(skb, 3);
953 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
954 esupp_rates[1] = 1;
955 pos = &esupp_rates[2];
956 } else {
957 pos = skb_put(skb, 1);
958 supp_rates[1]++;
959 }
Johannes Berg8318d782008-01-24 19:38:38 +0100960 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -0700961 }
962
963 ieee80211_sta_tx(dev, skb, 0);
964}
965
966
967static int ieee80211_sta_wep_configured(struct net_device *dev)
968{
969 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
970 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400971 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -0700972 return 0;
973 return 1;
974}
975
976
977static void ieee80211_auth_completed(struct net_device *dev,
978 struct ieee80211_if_sta *ifsta)
979{
980 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400981 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700982 ieee80211_associate(dev, ifsta);
983}
984
985
986static void ieee80211_auth_challenge(struct net_device *dev,
987 struct ieee80211_if_sta *ifsta,
988 struct ieee80211_mgmt *mgmt,
989 size_t len)
990{
991 u8 *pos;
992 struct ieee802_11_elems elems;
993
994 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
995 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -0400996 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -0700997 if (!elems.challenge) {
998 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
999 "frame\n", dev->name);
1000 return;
1001 }
1002 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1003 elems.challenge_len + 2, 1);
1004}
1005
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001006static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1007 u8 dialog_token, u16 status, u16 policy,
1008 u16 buf_size, u16 timeout)
1009{
1010 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1011 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1012 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1013 struct sk_buff *skb;
1014 struct ieee80211_mgmt *mgmt;
1015 u16 capab;
1016
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001017 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1018 sizeof(mgmt->u.action.u.addba_resp));
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001019 if (!skb) {
1020 printk(KERN_DEBUG "%s: failed to allocate buffer "
1021 "for addba resp frame\n", dev->name);
1022 return;
1023 }
1024
1025 skb_reserve(skb, local->hw.extra_tx_headroom);
1026 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1027 memset(mgmt, 0, 24);
1028 memcpy(mgmt->da, da, ETH_ALEN);
1029 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001030 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001031 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1032 else
1033 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1034 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1035 IEEE80211_STYPE_ACTION);
1036
1037 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1038 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1039 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1040 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1041
1042 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1043 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1044 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1045
1046 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1047 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1048 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1049
1050 ieee80211_sta_tx(dev, skb, 0);
1051
1052 return;
1053}
1054
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001055void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1056 u16 tid, u8 dialog_token, u16 start_seq_num,
1057 u16 agg_size, u16 timeout)
1058{
1059 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1060 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1061 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1062 struct sk_buff *skb;
1063 struct ieee80211_mgmt *mgmt;
1064 u16 capab;
1065
1066 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1067 sizeof(mgmt->u.action.u.addba_req));
1068
1069
1070 if (!skb) {
1071 printk(KERN_ERR "%s: failed to allocate buffer "
1072 "for addba request frame\n", dev->name);
1073 return;
1074 }
1075 skb_reserve(skb, local->hw.extra_tx_headroom);
1076 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1077 memset(mgmt, 0, 24);
1078 memcpy(mgmt->da, da, ETH_ALEN);
1079 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1080 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1081 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1082 else
1083 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1084
1085 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1086 IEEE80211_STYPE_ACTION);
1087
1088 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1089
1090 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1091 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1092
1093 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1094 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1095 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1096 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1097
1098 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1099
1100 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1101 mgmt->u.action.u.addba_req.start_seq_num =
1102 cpu_to_le16(start_seq_num << 4);
1103
1104 ieee80211_sta_tx(dev, skb, 0);
1105}
1106
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001107static void ieee80211_sta_process_addba_request(struct net_device *dev,
1108 struct ieee80211_mgmt *mgmt,
1109 size_t len)
1110{
1111 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001112 struct ieee80211_hw *hw = &local->hw;
1113 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001114 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001115 struct tid_ampdu_rx *tid_agg_rx;
1116 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001117 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001118 int ret = -EOPNOTSUPP;
1119 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001120
1121 sta = sta_info_get(local, mgmt->sa);
1122 if (!sta)
1123 return;
1124
1125 /* extract session parameters from addba request frame */
1126 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1127 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001128 start_seq_num =
1129 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001130
1131 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1132 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1133 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1134 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1135
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001136 status = WLAN_STATUS_REQUEST_DECLINED;
1137
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001138 /* sanity check for incoming parameters:
1139 * check if configuration can support the BA policy
1140 * and if buffer size does not exceeds max value */
1141 if (((ba_policy != 1)
1142 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1143 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1144 status = WLAN_STATUS_INVALID_QOS_PARAM;
1145#ifdef CONFIG_MAC80211_HT_DEBUG
1146 if (net_ratelimit())
1147 printk(KERN_DEBUG "Block Ack Req with bad params from "
1148 "%s on tid %u. policy %d, buffer size %d\n",
1149 print_mac(mac, mgmt->sa), tid, ba_policy,
1150 buf_size);
1151#endif /* CONFIG_MAC80211_HT_DEBUG */
1152 goto end_no_lock;
1153 }
1154 /* determine default buffer size */
1155 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001156 struct ieee80211_supported_band *sband;
1157
1158 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001159 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001160 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001161 }
1162
1163 tid_agg_rx = &sta->ampdu_mlme.tid_rx[tid];
1164
1165 /* examine state machine */
1166 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1167
1168 if (tid_agg_rx->state != HT_AGG_STATE_IDLE) {
1169#ifdef CONFIG_MAC80211_HT_DEBUG
1170 if (net_ratelimit())
1171 printk(KERN_DEBUG "unexpected Block Ack Req from "
1172 "%s on tid %u\n",
1173 print_mac(mac, mgmt->sa), tid);
1174#endif /* CONFIG_MAC80211_HT_DEBUG */
1175 goto end;
1176 }
1177
1178 /* prepare reordering buffer */
1179 tid_agg_rx->reorder_buf =
1180 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001181 if (!tid_agg_rx->reorder_buf) {
1182 if (net_ratelimit())
1183 printk(KERN_ERR "can not allocate reordering buffer "
1184 "to tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001185 goto end;
1186 }
1187 memset(tid_agg_rx->reorder_buf, 0,
1188 buf_size * sizeof(struct sk_buf *));
1189
1190 if (local->ops->ampdu_action)
1191 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001192 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001193#ifdef CONFIG_MAC80211_HT_DEBUG
1194 printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1195#endif /* CONFIG_MAC80211_HT_DEBUG */
1196
1197 if (ret) {
1198 kfree(tid_agg_rx->reorder_buf);
1199 goto end;
1200 }
1201
1202 /* change state and send addba resp */
1203 tid_agg_rx->state = HT_AGG_STATE_OPERATIONAL;
1204 tid_agg_rx->dialog_token = dialog_token;
1205 tid_agg_rx->ssn = start_seq_num;
1206 tid_agg_rx->head_seq_num = start_seq_num;
1207 tid_agg_rx->buf_size = buf_size;
1208 tid_agg_rx->timeout = timeout;
1209 tid_agg_rx->stored_mpdu_num = 0;
1210 status = WLAN_STATUS_SUCCESS;
1211end:
1212 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1213
1214end_no_lock:
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001215 ieee80211_send_addba_resp(sta->dev, sta->addr, tid, dialog_token,
1216 status, 1, buf_size, timeout);
1217 sta_info_put(sta);
1218}
Jiri Bencf0706e82007-05-05 11:45:53 -07001219
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001220static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1221 struct ieee80211_mgmt *mgmt,
1222 size_t len)
1223{
1224 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1225 struct ieee80211_hw *hw = &local->hw;
1226 struct sta_info *sta;
1227 u16 capab;
1228 u16 tid;
1229 u8 *state;
1230
1231 sta = sta_info_get(local, mgmt->sa);
1232 if (!sta)
1233 return;
1234
1235 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1236 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1237
1238 state = &sta->ampdu_mlme.tid_tx[tid].state;
1239
1240 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1241
1242 if (mgmt->u.action.u.addba_resp.dialog_token !=
1243 sta->ampdu_mlme.tid_tx[tid].dialog_token) {
1244 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1245#ifdef CONFIG_MAC80211_HT_DEBUG
1246 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1247#endif /* CONFIG_MAC80211_HT_DEBUG */
1248 sta_info_put(sta);
1249 return;
1250 }
1251
1252 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
1253#ifdef CONFIG_MAC80211_HT_DEBUG
1254 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1255#endif /* CONFIG_MAC80211_HT_DEBUG */
1256 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1257 == WLAN_STATUS_SUCCESS) {
1258 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1259 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1260 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1261 "%d\n", *state);
1262 sta_info_put(sta);
1263 return;
1264 }
1265
1266 if (*state & HT_ADDBA_RECEIVED_MSK)
1267 printk(KERN_DEBUG "double addBA response\n");
1268
1269 *state |= HT_ADDBA_RECEIVED_MSK;
1270 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
1271
1272 if (*state == HT_AGG_STATE_OPERATIONAL) {
1273 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1274 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1275 }
1276
1277 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1278 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1279 } else {
1280 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1281
1282 sta->ampdu_mlme.tid_tx[tid].addba_req_num++;
1283 /* this will allow the state check in stop_BA_session */
1284 *state = HT_AGG_STATE_OPERATIONAL;
1285 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1286 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1287 WLAN_BACK_INITIATOR);
1288 }
1289 sta_info_put(sta);
1290}
1291
1292void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1293 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001294{
1295 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1296 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1297 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1298 struct sk_buff *skb;
1299 struct ieee80211_mgmt *mgmt;
1300 u16 params;
1301
1302 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1303 sizeof(mgmt->u.action.u.delba));
1304
1305 if (!skb) {
1306 printk(KERN_ERR "%s: failed to allocate buffer "
1307 "for delba frame\n", dev->name);
1308 return;
1309 }
1310
1311 skb_reserve(skb, local->hw.extra_tx_headroom);
1312 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1313 memset(mgmt, 0, 24);
1314 memcpy(mgmt->da, da, ETH_ALEN);
1315 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001316 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001317 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1318 else
1319 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1320 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1321 IEEE80211_STYPE_ACTION);
1322
1323 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1324
1325 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1326 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1327 params = (u16)(initiator << 11); /* bit 11 initiator */
1328 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1329
1330 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1331 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1332
1333 ieee80211_sta_tx(dev, skb, 0);
1334}
1335
1336void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1337 u16 initiator, u16 reason)
1338{
1339 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1340 struct ieee80211_hw *hw = &local->hw;
1341 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001342 int ret, i;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001343
1344 sta = sta_info_get(local, ra);
1345 if (!sta)
1346 return;
1347
1348 /* check if TID is in operational state */
1349 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1350 if (sta->ampdu_mlme.tid_rx[tid].state
1351 != HT_AGG_STATE_OPERATIONAL) {
1352 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001353 sta_info_put(sta);
1354 return;
1355 }
1356 sta->ampdu_mlme.tid_rx[tid].state =
1357 HT_AGG_STATE_REQ_STOP_BA_MSK |
1358 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1359 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1360
1361 /* stop HW Rx aggregation. ampdu_action existence
1362 * already verified in session init so we add the BUG_ON */
1363 BUG_ON(!local->ops->ampdu_action);
1364
1365 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001366 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001367 if (ret)
1368 printk(KERN_DEBUG "HW problem - can not stop rx "
1369 "aggergation for tid %d\n", tid);
1370
1371 /* shutdown timer has not expired */
1372 if (initiator != WLAN_BACK_TIMER)
1373 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid].
1374 session_timer);
1375
1376 /* check if this is a self generated aggregation halt */
1377 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1378 ieee80211_send_delba(dev, ra, tid, 0, reason);
1379
1380 /* free the reordering buffer */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001381 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid].buf_size; i++) {
1382 if (sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]) {
1383 /* release the reordered frames */
1384 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]);
1385 sta->ampdu_mlme.tid_rx[tid].stored_mpdu_num--;
1386 sta->ampdu_mlme.tid_rx[tid].reorder_buf[i] = NULL;
1387 }
1388 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001389 kfree(sta->ampdu_mlme.tid_rx[tid].reorder_buf);
1390
1391 sta->ampdu_mlme.tid_rx[tid].state = HT_AGG_STATE_IDLE;
1392 sta_info_put(sta);
1393}
1394
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001395
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001396static void ieee80211_sta_process_delba(struct net_device *dev,
1397 struct ieee80211_mgmt *mgmt, size_t len)
1398{
1399 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1400 struct sta_info *sta;
1401 u16 tid, params;
1402 u16 initiator;
1403 DECLARE_MAC_BUF(mac);
1404
1405 sta = sta_info_get(local, mgmt->sa);
1406 if (!sta)
1407 return;
1408
1409 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1410 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1411 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1412
1413#ifdef CONFIG_MAC80211_HT_DEBUG
1414 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001415 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1416 print_mac(mac, mgmt->sa),
1417 initiator ? "recipient" : "initiator", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001418 mgmt->u.action.u.delba.reason_code);
1419#endif /* CONFIG_MAC80211_HT_DEBUG */
1420
1421 if (initiator == WLAN_BACK_INITIATOR)
1422 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1423 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001424 else { /* WLAN_BACK_RECIPIENT */
1425 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1426 sta->ampdu_mlme.tid_tx[tid].state =
1427 HT_AGG_STATE_OPERATIONAL;
1428 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1429 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1430 WLAN_BACK_RECIPIENT);
1431 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001432 sta_info_put(sta);
1433}
1434
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001435/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001436 * After sending add Block Ack request we activated a timer until
1437 * add Block Ack response will arrive from the recipient.
1438 * If this timer expires sta_addba_resp_timer_expired will be executed.
1439 */
1440void sta_addba_resp_timer_expired(unsigned long data)
1441{
1442 /* not an elegant detour, but there is no choice as the timer passes
1443 * only one argument, and both sta_info and TID are needed, so init
1444 * flow in sta_info_add gives the TID as data, while the timer_to_id
1445 * array gives the sta through container_of */
1446 u16 tid = *(int *)data;
1447 struct sta_info *temp_sta = container_of((void *)data,
1448 struct sta_info, timer_to_tid[tid]);
1449
1450 struct ieee80211_local *local = temp_sta->local;
1451 struct ieee80211_hw *hw = &local->hw;
1452 struct sta_info *sta;
1453 u8 *state;
1454
1455 sta = sta_info_get(local, temp_sta->addr);
1456 if (!sta)
1457 return;
1458
1459 state = &sta->ampdu_mlme.tid_tx[tid].state;
1460 /* check if the TID waits for addBA response */
1461 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1462 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1463 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1464 *state = HT_AGG_STATE_IDLE;
1465 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1466 "expecting addBA response there", tid);
1467 goto timer_expired_exit;
1468 }
1469
1470 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1471
1472 /* go through the state check in stop_BA_session */
1473 *state = HT_AGG_STATE_OPERATIONAL;
1474 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1475 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1476 WLAN_BACK_INITIATOR);
1477
1478timer_expired_exit:
1479 sta_info_put(sta);
1480}
1481
1482/*
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001483 * After receiving Block Ack Request (BAR) we activated a
1484 * timer after each frame arrives from the originator.
1485 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1486 */
1487void sta_rx_agg_session_timer_expired(unsigned long data)
1488{
1489 /* not an elegant detour, but there is no choice as the timer passes
1490 * only one argument, and verious sta_info are needed here, so init
1491 * flow in sta_info_add gives the TID as data, while the timer_to_id
1492 * array gives the sta through container_of */
1493 u8 *ptid = (u8 *)data;
1494 u8 *timer_to_id = ptid - *ptid;
1495 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1496 timer_to_tid[0]);
1497
1498 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1499 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr, (u16)*ptid,
1500 WLAN_BACK_TIMER,
1501 WLAN_REASON_QSTA_TIMEOUT);
1502}
1503
1504
Jiri Bencf0706e82007-05-05 11:45:53 -07001505static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1506 struct ieee80211_if_sta *ifsta,
1507 struct ieee80211_mgmt *mgmt,
1508 size_t len)
1509{
1510 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1511 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001512 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001513
1514 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001515 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001516 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001517 "%s, but not in authenticate state - ignored\n",
1518 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001519 return;
1520 }
1521
1522 if (len < 24 + 6) {
1523 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001524 "received from %s - ignored\n",
1525 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001526 return;
1527 }
1528
Johannes Berg51fb61e2007-12-19 01:31:27 +01001529 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001530 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1531 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001532 "unknown AP (SA=%s BSSID=%s) - "
1533 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1534 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001535 return;
1536 }
1537
Johannes Berg51fb61e2007-12-19 01:31:27 +01001538 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001539 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1540 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001541 "unknown BSSID (SA=%s BSSID=%s) - "
1542 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1543 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001544 return;
1545 }
1546
1547 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1548 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1549 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1550
Joe Perches0795af52007-10-03 17:59:30 -07001551 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001552 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001553 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001554 auth_transaction, status_code);
1555
Johannes Berg51fb61e2007-12-19 01:31:27 +01001556 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001557 /* IEEE 802.11 standard does not require authentication in IBSS
1558 * networks and most implementations do not seem to use it.
1559 * However, try to reply to authentication attempts if someone
1560 * has actually implemented this.
1561 * TODO: Could implement shared key authentication. */
1562 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1563 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1564 "frame (alg=%d transaction=%d)\n",
1565 dev->name, auth_alg, auth_transaction);
1566 return;
1567 }
1568 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1569 }
1570
1571 if (auth_alg != ifsta->auth_alg ||
1572 auth_transaction != ifsta->auth_transaction) {
1573 printk(KERN_DEBUG "%s: unexpected authentication frame "
1574 "(alg=%d transaction=%d)\n",
1575 dev->name, auth_alg, auth_transaction);
1576 return;
1577 }
1578
1579 if (status_code != WLAN_STATUS_SUCCESS) {
1580 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1581 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1582 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1583 u8 algs[3];
1584 const int num_algs = ARRAY_SIZE(algs);
1585 int i, pos;
1586 algs[0] = algs[1] = algs[2] = 0xff;
1587 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1588 algs[0] = WLAN_AUTH_OPEN;
1589 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1590 algs[1] = WLAN_AUTH_SHARED_KEY;
1591 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1592 algs[2] = WLAN_AUTH_LEAP;
1593 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1594 pos = 0;
1595 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1596 pos = 1;
1597 else
1598 pos = 2;
1599 for (i = 0; i < num_algs; i++) {
1600 pos++;
1601 if (pos >= num_algs)
1602 pos = 0;
1603 if (algs[pos] == ifsta->auth_alg ||
1604 algs[pos] == 0xff)
1605 continue;
1606 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1607 !ieee80211_sta_wep_configured(dev))
1608 continue;
1609 ifsta->auth_alg = algs[pos];
1610 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1611 "next try\n",
1612 dev->name, ifsta->auth_alg);
1613 break;
1614 }
1615 }
1616 return;
1617 }
1618
1619 switch (ifsta->auth_alg) {
1620 case WLAN_AUTH_OPEN:
1621 case WLAN_AUTH_LEAP:
1622 ieee80211_auth_completed(dev, ifsta);
1623 break;
1624 case WLAN_AUTH_SHARED_KEY:
1625 if (ifsta->auth_transaction == 4)
1626 ieee80211_auth_completed(dev, ifsta);
1627 else
1628 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1629 break;
1630 }
1631}
1632
1633
1634static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1635 struct ieee80211_if_sta *ifsta,
1636 struct ieee80211_mgmt *mgmt,
1637 size_t len)
1638{
1639 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001640 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001641
1642 if (len < 24 + 2) {
1643 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001644 "received from %s - ignored\n",
1645 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001646 return;
1647 }
1648
1649 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1650 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001651 "unknown AP (SA=%s BSSID=%s) - "
1652 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1653 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001654 return;
1655 }
1656
1657 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1658
Joe Perches0795af52007-10-03 17:59:30 -07001659 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001660 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001661 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001662
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001663 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001664 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1665 }
1666
1667 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1668 ifsta->state == IEEE80211_ASSOCIATE ||
1669 ifsta->state == IEEE80211_ASSOCIATED) {
1670 ifsta->state = IEEE80211_AUTHENTICATE;
1671 mod_timer(&ifsta->timer, jiffies +
1672 IEEE80211_RETRY_AUTH_INTERVAL);
1673 }
1674
1675 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001676 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001677}
1678
1679
1680static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1681 struct ieee80211_if_sta *ifsta,
1682 struct ieee80211_mgmt *mgmt,
1683 size_t len)
1684{
1685 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001686 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001687
1688 if (len < 24 + 2) {
1689 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001690 "received from %s - ignored\n",
1691 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001692 return;
1693 }
1694
1695 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1696 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001697 "unknown AP (SA=%s BSSID=%s) - "
1698 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1699 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001700 return;
1701 }
1702
1703 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1704
Joe Perches0795af52007-10-03 17:59:30 -07001705 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001706 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001707 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001708
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001709 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001710 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1711
1712 if (ifsta->state == IEEE80211_ASSOCIATED) {
1713 ifsta->state = IEEE80211_ASSOCIATE;
1714 mod_timer(&ifsta->timer, jiffies +
1715 IEEE80211_RETRY_AUTH_INTERVAL);
1716 }
1717
1718 ieee80211_set_disassoc(dev, ifsta, 0);
1719}
1720
1721
Johannes Berg471b3ef2007-12-28 14:32:58 +01001722static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001723 struct ieee80211_if_sta *ifsta,
1724 struct ieee80211_mgmt *mgmt,
1725 size_t len,
1726 int reassoc)
1727{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001728 struct ieee80211_local *local = sdata->local;
1729 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001730 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001731 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001732 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001733 u16 capab_info, status_code, aid;
1734 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001735 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001736 u8 *pos;
1737 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001738 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001739 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001740
1741 /* AssocResp and ReassocResp have identical structure, so process both
1742 * of them in this function. */
1743
1744 if (ifsta->state != IEEE80211_ASSOCIATE) {
1745 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001746 "%s, but not in associate state - ignored\n",
1747 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001748 return;
1749 }
1750
1751 if (len < 24 + 6) {
1752 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001753 "received from %s - ignored\n",
1754 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001755 return;
1756 }
1757
1758 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1759 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001760 "unknown AP (SA=%s BSSID=%s) - "
1761 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1762 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001763 return;
1764 }
1765
1766 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1767 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1768 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001769
Joe Perches0795af52007-10-03 17:59:30 -07001770 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001771 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001772 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001773 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001774
1775 if (status_code != WLAN_STATUS_SUCCESS) {
1776 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1777 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001778 /* if this was a reassociation, ensure we try a "full"
1779 * association next time. This works around some broken APs
1780 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001781 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001782 return;
1783 }
1784
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001785 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1786 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1787 "set\n", dev->name, aid);
1788 aid &= ~(BIT(15) | BIT(14));
1789
Jiri Bencf0706e82007-05-05 11:45:53 -07001790 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001791 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001792
1793 if (!elems.supp_rates) {
1794 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1795 dev->name);
1796 return;
1797 }
1798
1799 printk(KERN_DEBUG "%s: associated\n", dev->name);
1800 ifsta->aid = aid;
1801 ifsta->ap_capab = capab_info;
1802
1803 kfree(ifsta->assocresp_ies);
1804 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001805 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001806 if (ifsta->assocresp_ies)
1807 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1808
Jiri Bencf0706e82007-05-05 11:45:53 -07001809 /* Add STA entry for the AP */
1810 sta = sta_info_get(local, ifsta->bssid);
1811 if (!sta) {
1812 struct ieee80211_sta_bss *bss;
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001813 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001814 if (!sta) {
1815 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1816 " AP\n", dev->name);
1817 return;
1818 }
John W. Linville65c107a2007-10-05 14:23:27 -04001819 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001820 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001821 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001822 if (bss) {
1823 sta->last_rssi = bss->rssi;
1824 sta->last_signal = bss->signal;
1825 sta->last_noise = bss->noise;
1826 ieee80211_rx_bss_put(dev, bss);
1827 }
1828 }
1829
1830 sta->dev = dev;
Johannes Berg238814f2008-01-28 17:19:37 +01001831 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1832 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001833
1834 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001835 basic_rates = 0;
1836 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1837
Jiri Bencf0706e82007-05-05 11:45:53 -07001838 for (i = 0; i < elems.supp_rates_len; i++) {
1839 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001840
1841 if (rate > 110)
1842 have_higher_than_11mbit = true;
1843
1844 for (j = 0; j < sband->n_bitrates; j++) {
1845 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001846 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001847 if (elems.supp_rates[i] & 0x80)
1848 basic_rates |= BIT(j);
1849 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001850 }
Johannes Berg8318d782008-01-24 19:38:38 +01001851
Jiri Bencf0706e82007-05-05 11:45:53 -07001852 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1853 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001854
1855 if (rate > 110)
1856 have_higher_than_11mbit = true;
1857
1858 for (j = 0; j < sband->n_bitrates; j++) {
1859 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001860 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001861 if (elems.ext_supp_rates[i] & 0x80)
1862 basic_rates |= BIT(j);
1863 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001864 }
Johannes Berg8318d782008-01-24 19:38:38 +01001865
1866 sta->supp_rates[local->hw.conf.channel->band] = rates;
1867 sdata->basic_rates = basic_rates;
1868
1869 /* cf. IEEE 802.11 9.2.12 */
1870 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1871 have_higher_than_11mbit)
1872 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1873 else
1874 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001875
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001876 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1877 local->ops->conf_ht) {
1878 struct ieee80211_ht_bss_info bss_info;
1879
1880 ieee80211_ht_cap_ie_to_ht_info(
1881 (struct ieee80211_ht_cap *)
1882 elems.ht_cap_elem, &sta->ht_info);
1883 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1884 (struct ieee80211_ht_addt_info *)
1885 elems.ht_info_elem, &bss_info);
1886 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1887 }
1888
Jiri Bencf0706e82007-05-05 11:45:53 -07001889 rate_control_rate_init(sta, local);
1890
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001891 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001892 sta->flags |= WLAN_STA_WME;
1893 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1894 elems.wmm_param_len);
1895 }
1896
Johannes Berg8318d782008-01-24 19:38:38 +01001897 /* set AID, ieee80211_set_associated() will tell the driver */
1898 bss_conf->aid = aid;
1899 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07001900
1901 sta_info_put(sta);
1902
1903 ieee80211_associated(dev, ifsta);
1904}
1905
1906
1907/* Caller must hold local->sta_bss_lock */
1908static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1909 struct ieee80211_sta_bss *bss)
1910{
1911 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1912 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1913 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1914}
1915
1916
1917/* Caller must hold local->sta_bss_lock */
1918static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1919 struct ieee80211_sta_bss *bss)
1920{
1921 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1922 struct ieee80211_sta_bss *b, *prev = NULL;
1923 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1924 while (b) {
1925 if (b == bss) {
1926 if (!prev)
1927 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1928 bss->hnext;
1929 else
1930 prev->hnext = bss->hnext;
1931 break;
1932 }
1933 prev = b;
1934 b = b->hnext;
1935 }
1936}
1937
1938
1939static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001940ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001941 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001942{
1943 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1944 struct ieee80211_sta_bss *bss;
1945
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001946 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07001947 if (!bss)
1948 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001949 atomic_inc(&bss->users);
1950 atomic_inc(&bss->users);
1951 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01001952 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04001953 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1954 memcpy(bss->ssid, ssid, ssid_len);
1955 bss->ssid_len = ssid_len;
1956 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001957
1958 spin_lock_bh(&local->sta_bss_lock);
1959 /* TODO: order by RSSI? */
1960 list_add_tail(&bss->list, &local->sta_bss_list);
1961 __ieee80211_rx_bss_hash_add(dev, bss);
1962 spin_unlock_bh(&local->sta_bss_lock);
1963 return bss;
1964}
1965
1966
1967static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001968ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001969 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001970{
1971 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1972 struct ieee80211_sta_bss *bss;
1973
1974 spin_lock_bh(&local->sta_bss_lock);
1975 bss = local->sta_bss_hash[STA_HASH(bssid)];
1976 while (bss) {
John W. Linvillecffdd302007-10-05 14:23:27 -04001977 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01001978 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04001979 bss->ssid_len == ssid_len &&
1980 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001981 atomic_inc(&bss->users);
1982 break;
1983 }
1984 bss = bss->hnext;
1985 }
1986 spin_unlock_bh(&local->sta_bss_lock);
1987 return bss;
1988}
1989
1990
1991static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1992{
1993 kfree(bss->wpa_ie);
1994 kfree(bss->rsn_ie);
1995 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02001996 kfree(bss->ht_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07001997 kfree(bss);
1998}
1999
2000
2001static void ieee80211_rx_bss_put(struct net_device *dev,
2002 struct ieee80211_sta_bss *bss)
2003{
2004 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2005 if (!atomic_dec_and_test(&bss->users))
2006 return;
2007
2008 spin_lock_bh(&local->sta_bss_lock);
2009 __ieee80211_rx_bss_hash_del(dev, bss);
2010 list_del(&bss->list);
2011 spin_unlock_bh(&local->sta_bss_lock);
2012 ieee80211_rx_bss_free(bss);
2013}
2014
2015
2016void ieee80211_rx_bss_list_init(struct net_device *dev)
2017{
2018 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2019 spin_lock_init(&local->sta_bss_lock);
2020 INIT_LIST_HEAD(&local->sta_bss_list);
2021}
2022
2023
2024void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2025{
2026 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2027 struct ieee80211_sta_bss *bss, *tmp;
2028
2029 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2030 ieee80211_rx_bss_put(dev, bss);
2031}
2032
2033
2034static void ieee80211_rx_bss_info(struct net_device *dev,
2035 struct ieee80211_mgmt *mgmt,
2036 size_t len,
2037 struct ieee80211_rx_status *rx_status,
2038 int beacon)
2039{
2040 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2041 struct ieee802_11_elems elems;
2042 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002043 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002044 struct ieee80211_sta_bss *bss;
2045 struct sta_info *sta;
2046 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2047 u64 timestamp;
Joe Perches0795af52007-10-03 17:59:30 -07002048 DECLARE_MAC_BUF(mac);
2049 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002050
2051 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2052 return; /* ignore ProbeResp to foreign address */
2053
2054#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002055 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002056 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002057 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002058#endif
2059
2060 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2061 if (baselen > len)
2062 return;
2063
2064 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2065
Johannes Berg51fb61e2007-12-19 01:31:27 +01002066 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002067 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2068#ifdef CONFIG_MAC80211_IBSS_DEBUG
2069 static unsigned long last_tsf_debug = 0;
2070 u64 tsf;
2071 if (local->ops->get_tsf)
2072 tsf = local->ops->get_tsf(local_to_hw(local));
2073 else
2074 tsf = -1LLU;
2075 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
Joe Perches0795af52007-10-03 17:59:30 -07002076 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2077 "%s TSF=0x%llx BCN=0x%llx diff=%lld "
Jiri Bencf0706e82007-05-05 11:45:53 -07002078 "@%lu\n",
Joe Perches0795af52007-10-03 17:59:30 -07002079 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
Jiri Bencf0706e82007-05-05 11:45:53 -07002080 (unsigned long long)tsf,
2081 (unsigned long long)timestamp,
2082 (unsigned long long)(tsf - timestamp),
2083 jiffies);
2084 last_tsf_debug = jiffies;
2085 }
2086#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2087 }
2088
John W. Linville67a4cce2007-10-12 16:40:37 -04002089 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002090
Johannes Berg51fb61e2007-12-19 01:31:27 +01002091 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002092 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2093 (sta = sta_info_get(local, mgmt->sa))) {
Johannes Berg8318d782008-01-24 19:38:38 +01002094 struct ieee80211_supported_band *sband;
2095 struct ieee80211_rate *bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002096 size_t num_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01002097 u64 supp_rates, prev_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002098 int i, j;
2099
Johannes Berg8318d782008-01-24 19:38:38 +01002100 sband = local->hw.wiphy->bands[rx_status->band];
Zhu Yiece8edd2007-11-22 10:53:21 +08002101
Johannes Berg8318d782008-01-24 19:38:38 +01002102 if (!sband) {
2103 WARN_ON(1);
2104 sband = local->hw.wiphy->bands[
2105 local->hw.conf.channel->band];
Zhu Yiece8edd2007-11-22 10:53:21 +08002106 }
Johannes Berg8318d782008-01-24 19:38:38 +01002107
2108 bitrates = sband->bitrates;
2109 num_rates = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002110
2111 supp_rates = 0;
2112 for (i = 0; i < elems.supp_rates_len +
2113 elems.ext_supp_rates_len; i++) {
2114 u8 rate = 0;
2115 int own_rate;
2116 if (i < elems.supp_rates_len)
2117 rate = elems.supp_rates[i];
2118 else if (elems.ext_supp_rates)
2119 rate = elems.ext_supp_rates
2120 [i - elems.supp_rates_len];
2121 own_rate = 5 * (rate & 0x7f);
Jiri Bencf0706e82007-05-05 11:45:53 -07002122 for (j = 0; j < num_rates; j++)
Johannes Berg8318d782008-01-24 19:38:38 +01002123 if (bitrates[j].bitrate == own_rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002124 supp_rates |= BIT(j);
2125 }
2126
Johannes Berg8318d782008-01-24 19:38:38 +01002127 prev_rates = sta->supp_rates[rx_status->band];
2128 sta->supp_rates[rx_status->band] &= supp_rates;
2129 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002130 /* No matching rates - this should not really happen.
2131 * Make sure that at least one rate is marked
2132 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002133 sta->supp_rates[rx_status->band] =
2134 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002135 }
Johannes Berg8318d782008-01-24 19:38:38 +01002136 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002137 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002138 "%s based on beacon info (0x%llx & 0x%llx -> "
2139 "0x%llx)\n",
2140 dev->name, print_mac(mac, sta->addr),
2141 (unsigned long long) prev_rates,
2142 (unsigned long long) supp_rates,
2143 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002144 }
2145 sta_info_put(sta);
2146 }
2147
2148 if (!elems.ssid)
2149 return;
2150
2151 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002152 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002153 else
Johannes Berg8318d782008-01-24 19:38:38 +01002154 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002155
Johannes Berg8318d782008-01-24 19:38:38 +01002156 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002157 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002158 if (!bss) {
Johannes Berg8318d782008-01-24 19:38:38 +01002159 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002160 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002161 if (!bss)
2162 return;
2163 } else {
2164#if 0
2165 /* TODO: order by RSSI? */
2166 spin_lock_bh(&local->sta_bss_lock);
2167 list_move_tail(&bss->list, &local->sta_bss_list);
2168 spin_unlock_bh(&local->sta_bss_lock);
2169#endif
2170 }
2171
Johannes Berg8318d782008-01-24 19:38:38 +01002172 bss->band = rx_status->band;
2173
Jiri Bencf0706e82007-05-05 11:45:53 -07002174 if (bss->probe_resp && beacon) {
2175 /* Do not allow beacon to override data from Probe Response. */
2176 ieee80211_rx_bss_put(dev, bss);
2177 return;
2178 }
2179
Daniel Drake56282212007-07-10 19:32:10 +02002180 /* save the ERP value so that it is available at association time */
2181 if (elems.erp_info && elems.erp_info_len >= 1) {
2182 bss->erp_value = elems.erp_info[0];
2183 bss->has_erp_value = 1;
2184 }
2185
Jiri Bencf0706e82007-05-05 11:45:53 -07002186 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2187 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002188
2189 bss->supp_rates_len = 0;
2190 if (elems.supp_rates) {
2191 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2192 if (clen > elems.supp_rates_len)
2193 clen = elems.supp_rates_len;
2194 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2195 clen);
2196 bss->supp_rates_len += clen;
2197 }
2198 if (elems.ext_supp_rates) {
2199 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2200 if (clen > elems.ext_supp_rates_len)
2201 clen = elems.ext_supp_rates_len;
2202 memcpy(&bss->supp_rates[bss->supp_rates_len],
2203 elems.ext_supp_rates, clen);
2204 bss->supp_rates_len += clen;
2205 }
2206
2207 if (elems.wpa &&
2208 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2209 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2210 kfree(bss->wpa_ie);
2211 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2212 if (bss->wpa_ie) {
2213 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2214 bss->wpa_ie_len = elems.wpa_len + 2;
2215 } else
2216 bss->wpa_ie_len = 0;
2217 } else if (!elems.wpa && bss->wpa_ie) {
2218 kfree(bss->wpa_ie);
2219 bss->wpa_ie = NULL;
2220 bss->wpa_ie_len = 0;
2221 }
2222
2223 if (elems.rsn &&
2224 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2225 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2226 kfree(bss->rsn_ie);
2227 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2228 if (bss->rsn_ie) {
2229 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2230 bss->rsn_ie_len = elems.rsn_len + 2;
2231 } else
2232 bss->rsn_ie_len = 0;
2233 } else if (!elems.rsn && bss->rsn_ie) {
2234 kfree(bss->rsn_ie);
2235 bss->rsn_ie = NULL;
2236 bss->rsn_ie_len = 0;
2237 }
2238
2239 if (elems.wmm_param &&
2240 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2241 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2242 kfree(bss->wmm_ie);
2243 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2244 if (bss->wmm_ie) {
2245 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2246 elems.wmm_param_len + 2);
2247 bss->wmm_ie_len = elems.wmm_param_len + 2;
2248 } else
2249 bss->wmm_ie_len = 0;
2250 } else if (!elems.wmm_param && bss->wmm_ie) {
2251 kfree(bss->wmm_ie);
2252 bss->wmm_ie = NULL;
2253 bss->wmm_ie_len = 0;
2254 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002255 if (elems.ht_cap_elem &&
2256 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2257 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2258 kfree(bss->ht_ie);
2259 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2260 if (bss->ht_ie) {
2261 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2262 elems.ht_cap_elem_len + 2);
2263 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2264 } else
2265 bss->ht_ie_len = 0;
2266 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2267 kfree(bss->ht_ie);
2268 bss->ht_ie = NULL;
2269 bss->ht_ie_len = 0;
2270 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002271
Jiri Bencf0706e82007-05-05 11:45:53 -07002272 bss->timestamp = timestamp;
2273 bss->last_update = jiffies;
2274 bss->rssi = rx_status->ssi;
2275 bss->signal = rx_status->signal;
2276 bss->noise = rx_status->noise;
2277 if (!beacon)
2278 bss->probe_resp++;
2279 ieee80211_rx_bss_put(dev, bss);
2280}
2281
2282
2283static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2284 struct ieee80211_mgmt *mgmt,
2285 size_t len,
2286 struct ieee80211_rx_status *rx_status)
2287{
2288 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2289}
2290
2291
2292static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2293 struct ieee80211_mgmt *mgmt,
2294 size_t len,
2295 struct ieee80211_rx_status *rx_status)
2296{
Jiri Bencf0706e82007-05-05 11:45:53 -07002297 struct ieee80211_sub_if_data *sdata;
2298 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002299 size_t baselen;
2300 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002301 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2302 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002303 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002304
2305 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2306
2307 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002308 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002309 return;
2310 ifsta = &sdata->u.sta;
2311
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002312 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002313 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2314 return;
2315
2316 /* Process beacon from the current BSS */
2317 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2318 if (baselen > len)
2319 return;
2320
John W. Linville67a4cce2007-10-12 16:40:37 -04002321 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002322
Daniel Drake56282212007-07-10 19:32:10 +02002323 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002324 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002325
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002326 if (elems.ht_cap_elem && elems.ht_info_elem &&
2327 elems.wmm_param && local->ops->conf_ht &&
2328 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2329 struct ieee80211_ht_bss_info bss_info;
2330
2331 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2332 (struct ieee80211_ht_addt_info *)
2333 elems.ht_info_elem, &bss_info);
2334 /* check if AP changed bss inforamation */
2335 if ((conf->ht_bss_conf.primary_channel !=
2336 bss_info.primary_channel) ||
2337 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2338 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2339 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2340 &bss_info);
2341 }
2342
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002343 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002344 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2345 elems.wmm_param_len);
2346 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002347
2348 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002349}
2350
2351
2352static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2353 struct ieee80211_if_sta *ifsta,
2354 struct ieee80211_mgmt *mgmt,
2355 size_t len,
2356 struct ieee80211_rx_status *rx_status)
2357{
2358 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2359 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2360 int tx_last_beacon;
2361 struct sk_buff *skb;
2362 struct ieee80211_mgmt *resp;
2363 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002364 DECLARE_MAC_BUF(mac);
2365#ifdef CONFIG_MAC80211_IBSS_DEBUG
2366 DECLARE_MAC_BUF(mac2);
2367 DECLARE_MAC_BUF(mac3);
2368#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002369
Johannes Berg51fb61e2007-12-19 01:31:27 +01002370 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002371 ifsta->state != IEEE80211_IBSS_JOINED ||
2372 len < 24 + 2 || !ifsta->probe_resp)
2373 return;
2374
2375 if (local->ops->tx_last_beacon)
2376 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2377 else
2378 tx_last_beacon = 1;
2379
2380#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002381 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2382 "%s (tx_last_beacon=%d)\n",
2383 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2384 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002385#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2386
2387 if (!tx_last_beacon)
2388 return;
2389
2390 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2391 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2392 return;
2393
2394 end = ((u8 *) mgmt) + len;
2395 pos = mgmt->u.probe_req.variable;
2396 if (pos[0] != WLAN_EID_SSID ||
2397 pos + 2 + pos[1] > end) {
2398 if (net_ratelimit()) {
2399 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002400 "from %s\n",
2401 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002402 }
2403 return;
2404 }
2405 if (pos[1] != 0 &&
2406 (pos[1] != ifsta->ssid_len ||
2407 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2408 /* Ignore ProbeReq for foreign SSID */
2409 return;
2410 }
2411
2412 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002413 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002414 if (!skb)
2415 return;
2416
2417 resp = (struct ieee80211_mgmt *) skb->data;
2418 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2419#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002420 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2421 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002422#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2423 ieee80211_sta_tx(dev, skb, 0);
2424}
2425
Johannes Berg4e20cb22007-12-19 01:31:24 +01002426static void ieee80211_rx_mgmt_action(struct net_device *dev,
2427 struct ieee80211_if_sta *ifsta,
2428 struct ieee80211_mgmt *mgmt,
2429 size_t len)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002430{
2431 if (len < IEEE80211_MIN_ACTION_SIZE)
2432 return;
2433
2434 switch (mgmt->u.action.category) {
2435 case WLAN_CATEGORY_BACK:
2436 switch (mgmt->u.action.u.addba_req.action_code) {
2437 case WLAN_ACTION_ADDBA_REQ:
2438 if (len < (IEEE80211_MIN_ACTION_SIZE +
2439 sizeof(mgmt->u.action.u.addba_req)))
2440 break;
2441 ieee80211_sta_process_addba_request(dev, mgmt, len);
2442 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002443 case WLAN_ACTION_ADDBA_RESP:
2444 if (len < (IEEE80211_MIN_ACTION_SIZE +
2445 sizeof(mgmt->u.action.u.addba_resp)))
2446 break;
2447 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2448 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002449 case WLAN_ACTION_DELBA:
2450 if (len < (IEEE80211_MIN_ACTION_SIZE +
2451 sizeof(mgmt->u.action.u.delba)))
2452 break;
2453 ieee80211_sta_process_delba(dev, mgmt, len);
2454 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002455 default:
2456 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002457 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002458 dev->name);
2459 break;
2460 }
2461 break;
2462 default:
2463 break;
2464 }
2465}
Jiri Bencf0706e82007-05-05 11:45:53 -07002466
2467void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2468 struct ieee80211_rx_status *rx_status)
2469{
2470 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2471 struct ieee80211_sub_if_data *sdata;
2472 struct ieee80211_if_sta *ifsta;
2473 struct ieee80211_mgmt *mgmt;
2474 u16 fc;
2475
2476 if (skb->len < 24)
2477 goto fail;
2478
2479 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2480 ifsta = &sdata->u.sta;
2481
2482 mgmt = (struct ieee80211_mgmt *) skb->data;
2483 fc = le16_to_cpu(mgmt->frame_control);
2484
2485 switch (fc & IEEE80211_FCTL_STYPE) {
2486 case IEEE80211_STYPE_PROBE_REQ:
2487 case IEEE80211_STYPE_PROBE_RESP:
2488 case IEEE80211_STYPE_BEACON:
2489 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2490 case IEEE80211_STYPE_AUTH:
2491 case IEEE80211_STYPE_ASSOC_RESP:
2492 case IEEE80211_STYPE_REASSOC_RESP:
2493 case IEEE80211_STYPE_DEAUTH:
2494 case IEEE80211_STYPE_DISASSOC:
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002495 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002496 skb_queue_tail(&ifsta->skb_queue, skb);
2497 queue_work(local->hw.workqueue, &ifsta->work);
2498 return;
2499 default:
2500 printk(KERN_DEBUG "%s: received unknown management frame - "
2501 "stype=%d\n", dev->name,
2502 (fc & IEEE80211_FCTL_STYPE) >> 4);
2503 break;
2504 }
2505
2506 fail:
2507 kfree_skb(skb);
2508}
2509
2510
2511static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2512 struct sk_buff *skb)
2513{
2514 struct ieee80211_rx_status *rx_status;
2515 struct ieee80211_sub_if_data *sdata;
2516 struct ieee80211_if_sta *ifsta;
2517 struct ieee80211_mgmt *mgmt;
2518 u16 fc;
2519
2520 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2521 ifsta = &sdata->u.sta;
2522
2523 rx_status = (struct ieee80211_rx_status *) skb->cb;
2524 mgmt = (struct ieee80211_mgmt *) skb->data;
2525 fc = le16_to_cpu(mgmt->frame_control);
2526
2527 switch (fc & IEEE80211_FCTL_STYPE) {
2528 case IEEE80211_STYPE_PROBE_REQ:
2529 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2530 rx_status);
2531 break;
2532 case IEEE80211_STYPE_PROBE_RESP:
2533 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2534 break;
2535 case IEEE80211_STYPE_BEACON:
2536 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2537 break;
2538 case IEEE80211_STYPE_AUTH:
2539 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2540 break;
2541 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002542 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002543 break;
2544 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002545 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002546 break;
2547 case IEEE80211_STYPE_DEAUTH:
2548 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2549 break;
2550 case IEEE80211_STYPE_DISASSOC:
2551 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2552 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002553 case IEEE80211_STYPE_ACTION:
2554 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len);
2555 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07002556 }
2557
2558 kfree_skb(skb);
2559}
2560
2561
Johannes Berg9ae54c82008-01-31 19:48:20 +01002562ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08002563ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2564 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002565{
2566 struct ieee80211_mgmt *mgmt;
2567 u16 fc;
2568
Zhu Yiece8edd2007-11-22 10:53:21 +08002569 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002570 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002571
2572 mgmt = (struct ieee80211_mgmt *) skb->data;
2573 fc = le16_to_cpu(mgmt->frame_control);
2574
Zhu Yiece8edd2007-11-22 10:53:21 +08002575 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01002576 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08002577
2578 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002579 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08002580
Jiri Bencf0706e82007-05-05 11:45:53 -07002581 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2582 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2583 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2584 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002585 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002586 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002587 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2588 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2589 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002590 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002591 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002592 }
2593 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002594 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002595}
2596
2597
2598static int ieee80211_sta_active_ibss(struct net_device *dev)
2599{
2600 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2601 int active = 0;
2602 struct sta_info *sta;
2603
Michael Wube8755e2007-07-27 15:43:23 +02002604 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07002605 list_for_each_entry(sta, &local->sta_list, list) {
2606 if (sta->dev == dev &&
2607 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2608 jiffies)) {
2609 active++;
2610 break;
2611 }
2612 }
Michael Wube8755e2007-07-27 15:43:23 +02002613 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07002614
2615 return active;
2616}
2617
2618
2619static void ieee80211_sta_expire(struct net_device *dev)
2620{
2621 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2622 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02002623 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07002624 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002625
Michael Wube8755e2007-07-27 15:43:23 +02002626 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07002627 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2628 if (time_after(jiffies, sta->last_rx +
2629 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
Joe Perches0795af52007-10-03 17:59:30 -07002630 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2631 dev->name, print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +02002632 __sta_info_get(sta);
2633 sta_info_remove(sta);
2634 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07002635 }
Michael Wube8755e2007-07-27 15:43:23 +02002636 write_unlock_bh(&local->sta_lock);
2637
2638 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
2639 sta_info_free(sta);
2640 sta_info_put(sta);
2641 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002642}
2643
2644
2645static void ieee80211_sta_merge_ibss(struct net_device *dev,
2646 struct ieee80211_if_sta *ifsta)
2647{
2648 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2649
2650 ieee80211_sta_expire(dev);
2651 if (ieee80211_sta_active_ibss(dev))
2652 return;
2653
2654 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2655 "IBSS networks with same SSID (merge)\n", dev->name);
2656 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2657}
2658
2659
2660void ieee80211_sta_timer(unsigned long data)
2661{
2662 struct ieee80211_sub_if_data *sdata =
2663 (struct ieee80211_sub_if_data *) data;
2664 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2665 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2666
2667 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2668 queue_work(local->hw.workqueue, &ifsta->work);
2669}
2670
2671
2672void ieee80211_sta_work(struct work_struct *work)
2673{
2674 struct ieee80211_sub_if_data *sdata =
2675 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2676 struct net_device *dev = sdata->dev;
2677 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2678 struct ieee80211_if_sta *ifsta;
2679 struct sk_buff *skb;
2680
2681 if (!netif_running(dev))
2682 return;
2683
Zhu Yiece8edd2007-11-22 10:53:21 +08002684 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07002685 return;
2686
Johannes Berg51fb61e2007-12-19 01:31:27 +01002687 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2688 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002689 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01002690 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07002691 return;
2692 }
2693 ifsta = &sdata->u.sta;
2694
2695 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2696 ieee80211_sta_rx_queued_mgmt(dev, skb);
2697
2698 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2699 ifsta->state != IEEE80211_ASSOCIATE &&
2700 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01002701 if (ifsta->scan_ssid_len)
2702 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
2703 else
2704 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002705 return;
2706 }
2707
2708 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2709 if (ieee80211_sta_config_auth(dev, ifsta))
2710 return;
2711 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2712 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2713 return;
2714
2715 switch (ifsta->state) {
2716 case IEEE80211_DISABLED:
2717 break;
2718 case IEEE80211_AUTHENTICATE:
2719 ieee80211_authenticate(dev, ifsta);
2720 break;
2721 case IEEE80211_ASSOCIATE:
2722 ieee80211_associate(dev, ifsta);
2723 break;
2724 case IEEE80211_ASSOCIATED:
2725 ieee80211_associated(dev, ifsta);
2726 break;
2727 case IEEE80211_IBSS_SEARCH:
2728 ieee80211_sta_find_ibss(dev, ifsta);
2729 break;
2730 case IEEE80211_IBSS_JOINED:
2731 ieee80211_sta_merge_ibss(dev, ifsta);
2732 break;
2733 default:
2734 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2735 ifsta->state);
2736 break;
2737 }
2738
2739 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2740 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2741 "mixed-cell disabled - disassociate\n", dev->name);
2742
2743 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2744 ieee80211_set_disassoc(dev, ifsta, 0);
2745 }
2746}
2747
2748
2749static void ieee80211_sta_reset_auth(struct net_device *dev,
2750 struct ieee80211_if_sta *ifsta)
2751{
2752 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2753
2754 if (local->ops->reset_tsf) {
2755 /* Reset own TSF to allow time synchronization work. */
2756 local->ops->reset_tsf(local_to_hw(local));
2757 }
2758
2759 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2760
2761
2762 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2763 ifsta->auth_alg = WLAN_AUTH_OPEN;
2764 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2765 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2766 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2767 ifsta->auth_alg = WLAN_AUTH_LEAP;
2768 else
2769 ifsta->auth_alg = WLAN_AUTH_OPEN;
2770 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2771 ifsta->auth_alg);
2772 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002773 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2774 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002775 netif_carrier_off(dev);
2776}
2777
2778
2779void ieee80211_sta_req_auth(struct net_device *dev,
2780 struct ieee80211_if_sta *ifsta)
2781{
2782 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2783 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2784
Johannes Berg51fb61e2007-12-19 01:31:27 +01002785 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002786 return;
2787
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002788 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2789 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2790 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2791 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002792 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2793 queue_work(local->hw.workqueue, &ifsta->work);
2794 }
2795}
2796
2797static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2798 const char *ssid, int ssid_len)
2799{
2800 int tmp, hidden_ssid;
2801
Michael Wu48225702007-10-19 17:14:36 -04002802 if (ssid_len == ifsta->ssid_len &&
2803 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07002804 return 1;
2805
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002806 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07002807 return 0;
2808
2809 hidden_ssid = 1;
2810 tmp = ssid_len;
2811 while (tmp--) {
2812 if (ssid[tmp] != '\0') {
2813 hidden_ssid = 0;
2814 break;
2815 }
2816 }
2817
2818 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2819 return 1;
2820
2821 if (ssid_len == 1 && ssid[0] == ' ')
2822 return 1;
2823
2824 return 0;
2825}
2826
2827static int ieee80211_sta_config_auth(struct net_device *dev,
2828 struct ieee80211_if_sta *ifsta)
2829{
2830 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2831 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2832 struct ieee80211_sta_bss *bss, *selected = NULL;
2833 int top_rssi = 0, freq;
2834
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002835 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2836 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002837 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002838 ieee80211_sta_reset_auth(dev, ifsta);
2839 return 0;
2840 }
2841
2842 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01002843 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002844 list_for_each_entry(bss, &local->sta_bss_list, list) {
2845 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2846 continue;
2847
2848 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2849 !!sdata->default_key)
2850 continue;
2851
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002852 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2853 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07002854 continue;
2855
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002856 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002857 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2858 continue;
2859
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002860 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002861 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2862 continue;
2863
2864 if (!selected || top_rssi < bss->rssi) {
2865 selected = bss;
2866 top_rssi = bss->rssi;
2867 }
2868 }
2869 if (selected)
2870 atomic_inc(&selected->users);
2871 spin_unlock_bh(&local->sta_bss_lock);
2872
2873 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01002874 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002875 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07002876 ieee80211_sta_set_ssid(dev, selected->ssid,
2877 selected->ssid_len);
2878 ieee80211_sta_set_bssid(dev, selected->bssid);
2879 ieee80211_rx_bss_put(dev, selected);
2880 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002881 ieee80211_sta_reset_auth(dev, ifsta);
2882 return 0;
2883 } else {
2884 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002885 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04002886 ieee80211_sta_start_scan(dev, NULL, 0);
2887 else
2888 ieee80211_sta_start_scan(dev, ifsta->ssid,
2889 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002890 ifsta->state = IEEE80211_AUTHENTICATE;
2891 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2892 } else
2893 ifsta->state = IEEE80211_DISABLED;
2894 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002895 return -1;
2896}
2897
2898static int ieee80211_sta_join_ibss(struct net_device *dev,
2899 struct ieee80211_if_sta *ifsta,
2900 struct ieee80211_sta_bss *bss)
2901{
2902 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2903 int res, rates, i, j;
2904 struct sk_buff *skb;
2905 struct ieee80211_mgmt *mgmt;
2906 struct ieee80211_tx_control control;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002907 struct rate_selection ratesel;
Jiri Bencf0706e82007-05-05 11:45:53 -07002908 u8 *pos;
2909 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +01002910 struct ieee80211_supported_band *sband;
2911
2912 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002913
2914 /* Remove possible STA entries from other IBSS networks. */
2915 sta_info_flush(local, NULL);
2916
2917 if (local->ops->reset_tsf) {
2918 /* Reset own TSF to allow time synchronization work. */
2919 local->ops->reset_tsf(local_to_hw(local));
2920 }
2921 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2922 res = ieee80211_if_config(dev);
2923 if (res)
2924 return res;
2925
2926 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2927
2928 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2929 sdata->drop_unencrypted = bss->capability &
2930 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2931
Johannes Berg8318d782008-01-24 19:38:38 +01002932 res = ieee80211_set_freq(local, bss->freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07002933
Johannes Berg8318d782008-01-24 19:38:38 +01002934 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2935 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2936 "%d MHz\n", dev->name, local->oper_channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07002937 return -1;
2938 }
2939
2940 /* Set beacon template based on scan results */
2941 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2942 do {
2943 if (!skb)
2944 break;
2945
2946 skb_reserve(skb, local->hw.extra_tx_headroom);
2947
2948 mgmt = (struct ieee80211_mgmt *)
2949 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2950 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2951 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2952 IEEE80211_STYPE_BEACON);
2953 memset(mgmt->da, 0xff, ETH_ALEN);
2954 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2955 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2956 mgmt->u.beacon.beacon_int =
2957 cpu_to_le16(local->hw.conf.beacon_int);
2958 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2959
2960 pos = skb_put(skb, 2 + ifsta->ssid_len);
2961 *pos++ = WLAN_EID_SSID;
2962 *pos++ = ifsta->ssid_len;
2963 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2964
2965 rates = bss->supp_rates_len;
2966 if (rates > 8)
2967 rates = 8;
2968 pos = skb_put(skb, 2 + rates);
2969 *pos++ = WLAN_EID_SUPP_RATES;
2970 *pos++ = rates;
2971 memcpy(pos, bss->supp_rates, rates);
2972
Johannes Berg8318d782008-01-24 19:38:38 +01002973 if (bss->band == IEEE80211_BAND_2GHZ) {
2974 pos = skb_put(skb, 2 + 1);
2975 *pos++ = WLAN_EID_DS_PARAMS;
2976 *pos++ = 1;
2977 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2978 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002979
2980 pos = skb_put(skb, 2 + 2);
2981 *pos++ = WLAN_EID_IBSS_PARAMS;
2982 *pos++ = 2;
2983 /* FIX: set ATIM window based on scan results */
2984 *pos++ = 0;
2985 *pos++ = 0;
2986
2987 if (bss->supp_rates_len > 8) {
2988 rates = bss->supp_rates_len - 8;
2989 pos = skb_put(skb, 2 + rates);
2990 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2991 *pos++ = rates;
2992 memcpy(pos, &bss->supp_rates[8], rates);
2993 }
2994
2995 memset(&control, 0, sizeof(control));
Johannes Berg8318d782008-01-24 19:38:38 +01002996 rate_control_get_rate(dev, sband, skb, &ratesel);
Mattias Nissler1abbe492007-12-20 13:50:07 +01002997 if (!ratesel.rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002998 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2999 "for IBSS beacon\n", dev->name);
3000 break;
3001 }
Ivo van Doorn0f7054e2008-01-13 14:16:47 +01003002 control.vif = &sdata->vif;
Johannes Berg8318d782008-01-24 19:38:38 +01003003 control.tx_rate = ratesel.rate;
3004 if (sdata->bss_conf.use_short_preamble &&
3005 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
3006 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003007 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
Jiri Bencf0706e82007-05-05 11:45:53 -07003008 control.flags |= IEEE80211_TXCTL_NO_ACK;
3009 control.retry_limit = 1;
3010
3011 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
3012 if (ifsta->probe_resp) {
3013 mgmt = (struct ieee80211_mgmt *)
3014 ifsta->probe_resp->data;
3015 mgmt->frame_control =
3016 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
3017 IEEE80211_STYPE_PROBE_RESP);
3018 } else {
3019 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
3020 "template for IBSS\n", dev->name);
3021 }
3022
3023 if (local->ops->beacon_update &&
3024 local->ops->beacon_update(local_to_hw(local),
3025 skb, &control) == 0) {
3026 printk(KERN_DEBUG "%s: Configured IBSS beacon "
3027 "template based on scan results\n", dev->name);
3028 skb = NULL;
3029 }
3030
3031 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003032 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003033 for (i = 0; i < bss->supp_rates_len; i++) {
3034 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01003035 for (j = 0; j < sband->n_bitrates; j++)
3036 if (sband->bitrates[j].bitrate == bitrate)
Jiri Bencf0706e82007-05-05 11:45:53 -07003037 rates |= BIT(j);
3038 }
Johannes Berg8318d782008-01-24 19:38:38 +01003039 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003040 } while (0);
3041
3042 if (skb) {
3043 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
3044 "template\n", dev->name);
3045 dev_kfree_skb(skb);
3046 }
3047
3048 ifsta->state = IEEE80211_IBSS_JOINED;
3049 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3050
3051 ieee80211_rx_bss_put(dev, bss);
3052
3053 return res;
3054}
3055
3056
3057static int ieee80211_sta_create_ibss(struct net_device *dev,
3058 struct ieee80211_if_sta *ifsta)
3059{
3060 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3061 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003062 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003063 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003064 u8 bssid[ETH_ALEN], *pos;
3065 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003066 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003067
3068#if 0
3069 /* Easier testing, use fixed BSSID. */
3070 memset(bssid, 0xfe, ETH_ALEN);
3071#else
3072 /* Generate random, not broadcast, locally administered BSSID. Mix in
3073 * own MAC address to make sure that devices that do not have proper
3074 * random number generator get different BSSID. */
3075 get_random_bytes(bssid, ETH_ALEN);
3076 for (i = 0; i < ETH_ALEN; i++)
3077 bssid[i] ^= dev->dev_addr[i];
3078 bssid[0] &= ~0x01;
3079 bssid[0] |= 0x02;
3080#endif
3081
Joe Perches0795af52007-10-03 17:59:30 -07003082 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3083 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003084
Johannes Berg8318d782008-01-24 19:38:38 +01003085 bss = ieee80211_rx_bss_add(dev, bssid,
3086 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003087 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003088 if (!bss)
3089 return -ENOMEM;
3090
Johannes Berg8318d782008-01-24 19:38:38 +01003091 bss->band = local->hw.conf.channel->band;
3092 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003093
3094 if (local->hw.conf.beacon_int == 0)
3095 local->hw.conf.beacon_int = 100;
3096 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003097 bss->last_update = jiffies;
3098 bss->capability = WLAN_CAPABILITY_IBSS;
3099 if (sdata->default_key) {
3100 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3101 } else
3102 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003103 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003104 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003105 for (i = 0; i < sband->n_bitrates; i++) {
3106 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003107 *pos++ = (u8) (rate / 5);
3108 }
3109
3110 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3111}
3112
3113
3114static int ieee80211_sta_find_ibss(struct net_device *dev,
3115 struct ieee80211_if_sta *ifsta)
3116{
3117 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3118 struct ieee80211_sta_bss *bss;
3119 int found = 0;
3120 u8 bssid[ETH_ALEN];
3121 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003122 DECLARE_MAC_BUF(mac);
3123 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003124
3125 if (ifsta->ssid_len == 0)
3126 return -EINVAL;
3127
3128 active_ibss = ieee80211_sta_active_ibss(dev);
3129#ifdef CONFIG_MAC80211_IBSS_DEBUG
3130 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3131 dev->name, active_ibss);
3132#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3133 spin_lock_bh(&local->sta_bss_lock);
3134 list_for_each_entry(bss, &local->sta_bss_list, list) {
3135 if (ifsta->ssid_len != bss->ssid_len ||
3136 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3137 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3138 continue;
3139#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003140 printk(KERN_DEBUG " bssid=%s found\n",
3141 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003142#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3143 memcpy(bssid, bss->bssid, ETH_ALEN);
3144 found = 1;
3145 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3146 break;
3147 }
3148 spin_unlock_bh(&local->sta_bss_lock);
3149
3150#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003151 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3152 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003153#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3154 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003155 (bss = ieee80211_rx_bss_get(dev, bssid,
3156 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003157 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003158 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003159 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003160 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003161 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3162 }
3163#ifdef CONFIG_MAC80211_IBSS_DEBUG
3164 printk(KERN_DEBUG " did not try to join ibss\n");
3165#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3166
3167 /* Selected IBSS not found in current scan results - try to scan */
3168 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3169 !ieee80211_sta_active_ibss(dev)) {
3170 mod_timer(&ifsta->timer, jiffies +
3171 IEEE80211_IBSS_MERGE_INTERVAL);
3172 } else if (time_after(jiffies, local->last_scan_completed +
3173 IEEE80211_SCAN_INTERVAL)) {
3174 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3175 "join\n", dev->name);
3176 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3177 ifsta->ssid_len);
3178 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3179 int interval = IEEE80211_SCAN_INTERVAL;
3180
3181 if (time_after(jiffies, ifsta->ibss_join_req +
3182 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003183 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003184 (!(local->oper_channel->flags &
3185 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003186 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003187 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003188 printk(KERN_DEBUG "%s: IBSS not allowed on"
3189 " %d MHz\n", dev->name,
3190 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003191 }
3192
3193 /* No IBSS found - decrease scan interval and continue
3194 * scanning. */
3195 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3196 }
3197
3198 ifsta->state = IEEE80211_IBSS_SEARCH;
3199 mod_timer(&ifsta->timer, jiffies + interval);
3200 return 0;
3201 }
3202
3203 return 0;
3204}
3205
3206
3207int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3208{
Johannes Berg8318d782008-01-24 19:38:38 +01003209 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003210 struct ieee80211_if_sta *ifsta;
3211 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3212
3213 if (len > IEEE80211_MAX_SSID_LEN)
3214 return -EINVAL;
3215
3216 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
3217 * not defined. */
3218 if (local->ops->conf_tx) {
3219 struct ieee80211_tx_queue_params qparam;
3220 int i;
3221
3222 memset(&qparam, 0, sizeof(qparam));
Johannes Berg8318d782008-01-24 19:38:38 +01003223
Jiri Bencf0706e82007-05-05 11:45:53 -07003224 qparam.aifs = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003225
3226 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
3227 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
3228 qparam.cw_min = 31;
3229 else
3230 qparam.cw_min = 15;
3231
Jiri Bencf0706e82007-05-05 11:45:53 -07003232 qparam.cw_max = 1023;
3233 qparam.burst_time = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003234
Jiri Bencf0706e82007-05-05 11:45:53 -07003235 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -07003236 local->ops->conf_tx(local_to_hw(local),
3237 i + IEEE80211_TX_QUEUE_DATA0,
3238 &qparam);
Johannes Berg8318d782008-01-24 19:38:38 +01003239
Jiri Bencf0706e82007-05-05 11:45:53 -07003240 /* IBSS uses different parameters for Beacon sending */
3241 qparam.cw_min++;
3242 qparam.cw_min *= 2;
3243 qparam.cw_min--;
3244 local->ops->conf_tx(local_to_hw(local),
3245 IEEE80211_TX_QUEUE_BEACON, &qparam);
3246 }
3247
Jiri Bencf0706e82007-05-05 11:45:53 -07003248 ifsta = &sdata->u.sta;
3249
3250 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003251 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003252 memcpy(ifsta->ssid, ssid, len);
3253 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3254 ifsta->ssid_len = len;
3255
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003256 if (len)
3257 ifsta->flags |= IEEE80211_STA_SSID_SET;
3258 else
3259 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003260 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003261 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003262 ifsta->ibss_join_req = jiffies;
3263 ifsta->state = IEEE80211_IBSS_SEARCH;
3264 return ieee80211_sta_find_ibss(dev, ifsta);
3265 }
3266 return 0;
3267}
3268
3269
3270int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3271{
3272 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3273 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3274 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3275 *len = ifsta->ssid_len;
3276 return 0;
3277}
3278
3279
3280int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3281{
3282 struct ieee80211_sub_if_data *sdata;
3283 struct ieee80211_if_sta *ifsta;
3284 int res;
3285
3286 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3287 ifsta = &sdata->u.sta;
3288
3289 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3290 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3291 res = ieee80211_if_config(dev);
3292 if (res) {
3293 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3294 "the low-level driver\n", dev->name);
3295 return res;
3296 }
3297 }
3298
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003299 if (is_valid_ether_addr(bssid))
3300 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003301 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003302 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3303
Jiri Bencf0706e82007-05-05 11:45:53 -07003304 return 0;
3305}
3306
3307
3308static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3309 struct ieee80211_sub_if_data *sdata,
3310 int powersave)
3311{
3312 struct sk_buff *skb;
3313 struct ieee80211_hdr *nullfunc;
3314 u16 fc;
3315
3316 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3317 if (!skb) {
3318 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3319 "frame\n", sdata->dev->name);
3320 return;
3321 }
3322 skb_reserve(skb, local->hw.extra_tx_headroom);
3323
3324 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3325 memset(nullfunc, 0, 24);
3326 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3327 IEEE80211_FCTL_TODS;
3328 if (powersave)
3329 fc |= IEEE80211_FCTL_PM;
3330 nullfunc->frame_control = cpu_to_le16(fc);
3331 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3332 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3333 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3334
3335 ieee80211_sta_tx(sdata->dev, skb, 0);
3336}
3337
3338
3339void ieee80211_scan_completed(struct ieee80211_hw *hw)
3340{
3341 struct ieee80211_local *local = hw_to_local(hw);
3342 struct net_device *dev = local->scan_dev;
3343 struct ieee80211_sub_if_data *sdata;
3344 union iwreq_data wrqu;
3345
3346 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003347 memset(&wrqu, 0, sizeof(wrqu));
3348 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003349
Zhu Yiece8edd2007-11-22 10:53:21 +08003350 if (local->sta_hw_scanning) {
3351 local->sta_hw_scanning = 0;
3352 goto done;
3353 }
3354
3355 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003356 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003357 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003358 "channel after scan\n", dev->name);
3359
Johannes Berg4150c572007-09-17 01:29:23 -04003360
3361 netif_tx_lock_bh(local->mdev);
3362 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3363 local->ops->configure_filter(local_to_hw(local),
3364 FIF_BCN_PRBRESP_PROMISC,
3365 &local->filter_flags,
3366 local->mdev->mc_count,
3367 local->mdev->mc_list);
3368
3369 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003370
Johannes Berg79010422007-09-18 17:29:21 -04003371 rcu_read_lock();
3372 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003373
3374 /* No need to wake the master device. */
3375 if (sdata->dev == local->mdev)
3376 continue;
3377
Johannes Berg51fb61e2007-12-19 01:31:27 +01003378 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003379 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07003380 ieee80211_send_nullfunc(local, sdata, 0);
3381 ieee80211_sta_timer((unsigned long)sdata);
3382 }
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003383
Jiri Bencf0706e82007-05-05 11:45:53 -07003384 netif_wake_queue(sdata->dev);
3385 }
Johannes Berg79010422007-09-18 17:29:21 -04003386 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003387
Zhu Yiece8edd2007-11-22 10:53:21 +08003388done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003389 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003390 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003391 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003392 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003393 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3394 !ieee80211_sta_active_ibss(dev)))
3395 ieee80211_sta_find_ibss(dev, ifsta);
3396 }
3397}
3398EXPORT_SYMBOL(ieee80211_scan_completed);
3399
3400void ieee80211_sta_scan_work(struct work_struct *work)
3401{
3402 struct ieee80211_local *local =
3403 container_of(work, struct ieee80211_local, scan_work.work);
3404 struct net_device *dev = local->scan_dev;
3405 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003406 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003407 struct ieee80211_channel *chan;
3408 int skip;
3409 unsigned long next_delay = 0;
3410
Zhu Yiece8edd2007-11-22 10:53:21 +08003411 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003412 return;
3413
3414 switch (local->scan_state) {
3415 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003416 /*
3417 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3418 * after we successfully scanned the last channel of the last
3419 * band (and the last band is supported by the hw)
3420 */
Johannes Berg8318d782008-01-24 19:38:38 +01003421 if (local->scan_band < IEEE80211_NUM_BANDS)
3422 sband = local->hw.wiphy->bands[local->scan_band];
3423 else
3424 sband = NULL;
3425
Johannes Berg69d464d2008-01-28 17:08:56 +01003426 /*
3427 * If we are at an unsupported band and have more bands
3428 * left to scan, advance to the next supported one.
3429 */
3430 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003431 local->scan_band++;
3432 sband = local->hw.wiphy->bands[local->scan_band];
3433 local->scan_channel_idx = 0;
3434 }
3435
Johannes Berg69d464d2008-01-28 17:08:56 +01003436 /* if no more bands/channels left, complete scan */
3437 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003438 ieee80211_scan_completed(local_to_hw(local));
3439 return;
3440 }
Johannes Berg8318d782008-01-24 19:38:38 +01003441 skip = 0;
3442 chan = &sband->channels[local->scan_channel_idx];
3443
3444 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003445 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003446 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003447 skip = 1;
3448
3449 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003450 local->scan_channel = chan;
3451 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003452 printk(KERN_DEBUG "%s: failed to set freq to "
3453 "%d MHz for scan\n", dev->name,
3454 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003455 skip = 1;
3456 }
3457 }
3458
Johannes Berg69d464d2008-01-28 17:08:56 +01003459 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003460 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003461 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003462 /*
3463 * scan_band may end up == IEEE80211_NUM_BANDS, but
3464 * we'll catch that case above and complete the scan
3465 * if that is the case.
3466 */
Johannes Berg8318d782008-01-24 19:38:38 +01003467 local->scan_band++;
3468 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003469 }
3470
3471 if (skip)
3472 break;
3473
3474 next_delay = IEEE80211_PROBE_DELAY +
3475 usecs_to_jiffies(local->hw.channel_change_time);
3476 local->scan_state = SCAN_SEND_PROBE;
3477 break;
3478 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003479 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003480 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003481
3482 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3483 break;
3484 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3485 local->scan_ssid_len);
3486 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003487 break;
3488 }
3489
Zhu Yiece8edd2007-11-22 10:53:21 +08003490 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003491 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3492 next_delay);
3493}
3494
3495
3496static int ieee80211_sta_start_scan(struct net_device *dev,
3497 u8 *ssid, size_t ssid_len)
3498{
3499 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3500 struct ieee80211_sub_if_data *sdata;
3501
3502 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3503 return -EINVAL;
3504
3505 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3506 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3507 * BSSID: MACAddress
3508 * SSID
3509 * ScanType: ACTIVE, PASSIVE
3510 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3511 * a Probe frame during active scanning
3512 * ChannelList
3513 * MinChannelTime (>= ProbeDelay), in TU
3514 * MaxChannelTime: (>= MinChannelTime), in TU
3515 */
3516
3517 /* MLME-SCAN.confirm
3518 * BSSDescriptionSet
3519 * ResultCode: SUCCESS, INVALID_PARAMETERS
3520 */
3521
Zhu Yiece8edd2007-11-22 10:53:21 +08003522 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003523 if (local->scan_dev == dev)
3524 return 0;
3525 return -EBUSY;
3526 }
3527
3528 if (local->ops->hw_scan) {
3529 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003530 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003531 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003532 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003533 local->scan_dev = dev;
3534 }
3535 return rc;
3536 }
3537
Zhu Yiece8edd2007-11-22 10:53:21 +08003538 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003539
Johannes Berg79010422007-09-18 17:29:21 -04003540 rcu_read_lock();
3541 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003542
3543 /* Don't stop the master interface, otherwise we can't transmit
3544 * probes! */
3545 if (sdata->dev == local->mdev)
3546 continue;
3547
Jiri Bencf0706e82007-05-05 11:45:53 -07003548 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003549 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003550 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003551 ieee80211_send_nullfunc(local, sdata, 1);
3552 }
Johannes Berg79010422007-09-18 17:29:21 -04003553 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003554
3555 if (ssid) {
3556 local->scan_ssid_len = ssid_len;
3557 memcpy(local->scan_ssid, ssid, ssid_len);
3558 } else
3559 local->scan_ssid_len = 0;
3560 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003561 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003562 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003563 local->scan_dev = dev;
3564
Johannes Berg4150c572007-09-17 01:29:23 -04003565 netif_tx_lock_bh(local->mdev);
3566 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3567 local->ops->configure_filter(local_to_hw(local),
3568 FIF_BCN_PRBRESP_PROMISC,
3569 &local->filter_flags,
3570 local->mdev->mc_count,
3571 local->mdev->mc_list);
3572 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003573
3574 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3575 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3576 IEEE80211_CHANNEL_TIME);
3577
3578 return 0;
3579}
3580
3581
3582int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3583{
3584 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3585 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3586 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3587
Johannes Berg51fb61e2007-12-19 01:31:27 +01003588 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003589 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3590
Zhu Yiece8edd2007-11-22 10:53:21 +08003591 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003592 if (local->scan_dev == dev)
3593 return 0;
3594 return -EBUSY;
3595 }
3596
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003597 ifsta->scan_ssid_len = ssid_len;
3598 if (ssid_len)
3599 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003600 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3601 queue_work(local->hw.workqueue, &ifsta->work);
3602 return 0;
3603}
3604
3605static char *
3606ieee80211_sta_scan_result(struct net_device *dev,
3607 struct ieee80211_sta_bss *bss,
3608 char *current_ev, char *end_buf)
3609{
3610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3611 struct iw_event iwe;
3612
3613 if (time_after(jiffies,
3614 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3615 return current_ev;
3616
Jiri Bencf0706e82007-05-05 11:45:53 -07003617 memset(&iwe, 0, sizeof(iwe));
3618 iwe.cmd = SIOCGIWAP;
3619 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3620 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3621 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3622 IW_EV_ADDR_LEN);
3623
3624 memset(&iwe, 0, sizeof(iwe));
3625 iwe.cmd = SIOCGIWESSID;
3626 iwe.u.data.length = bss->ssid_len;
3627 iwe.u.data.flags = 1;
3628 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3629 bss->ssid);
3630
3631 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
3632 memset(&iwe, 0, sizeof(iwe));
3633 iwe.cmd = SIOCGIWMODE;
3634 if (bss->capability & WLAN_CAPABILITY_ESS)
3635 iwe.u.mode = IW_MODE_MASTER;
3636 else
3637 iwe.u.mode = IW_MODE_ADHOC;
3638 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3639 IW_EV_UINT_LEN);
3640 }
3641
3642 memset(&iwe, 0, sizeof(iwe));
3643 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003644 iwe.u.freq.m = bss->freq;
3645 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07003646 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3647 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003648
3649 memset(&iwe, 0, sizeof(iwe));
3650 iwe.cmd = SIOCGIWFREQ;
3651 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3652 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003653 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3654 IW_EV_FREQ_LEN);
3655
3656 memset(&iwe, 0, sizeof(iwe));
3657 iwe.cmd = IWEVQUAL;
3658 iwe.u.qual.qual = bss->signal;
3659 iwe.u.qual.level = bss->rssi;
3660 iwe.u.qual.noise = bss->noise;
3661 iwe.u.qual.updated = local->wstats_flags;
3662 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3663 IW_EV_QUAL_LEN);
3664
3665 memset(&iwe, 0, sizeof(iwe));
3666 iwe.cmd = SIOCGIWENCODE;
3667 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3668 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3669 else
3670 iwe.u.data.flags = IW_ENCODE_DISABLED;
3671 iwe.u.data.length = 0;
3672 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3673
3674 if (bss && bss->wpa_ie) {
3675 memset(&iwe, 0, sizeof(iwe));
3676 iwe.cmd = IWEVGENIE;
3677 iwe.u.data.length = bss->wpa_ie_len;
3678 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3679 bss->wpa_ie);
3680 }
3681
3682 if (bss && bss->rsn_ie) {
3683 memset(&iwe, 0, sizeof(iwe));
3684 iwe.cmd = IWEVGENIE;
3685 iwe.u.data.length = bss->rsn_ie_len;
3686 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3687 bss->rsn_ie);
3688 }
3689
3690 if (bss && bss->supp_rates_len > 0) {
3691 /* display all supported rates in readable format */
3692 char *p = current_ev + IW_EV_LCP_LEN;
3693 int i;
3694
3695 memset(&iwe, 0, sizeof(iwe));
3696 iwe.cmd = SIOCGIWRATE;
3697 /* Those two flags are ignored... */
3698 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3699
3700 for (i = 0; i < bss->supp_rates_len; i++) {
3701 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3702 0x7f) * 500000);
3703 p = iwe_stream_add_value(current_ev, p,
3704 end_buf, &iwe, IW_EV_PARAM_LEN);
3705 }
3706 current_ev = p;
3707 }
3708
3709 if (bss) {
3710 char *buf;
3711 buf = kmalloc(30, GFP_ATOMIC);
3712 if (buf) {
3713 memset(&iwe, 0, sizeof(iwe));
3714 iwe.cmd = IWEVCUSTOM;
3715 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3716 iwe.u.data.length = strlen(buf);
3717 current_ev = iwe_stream_add_point(current_ev, end_buf,
3718 &iwe, buf);
3719 kfree(buf);
3720 }
3721 }
3722
Jiri Bencf0706e82007-05-05 11:45:53 -07003723 return current_ev;
3724}
3725
3726
3727int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3728{
3729 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3730 char *current_ev = buf;
3731 char *end_buf = buf + len;
3732 struct ieee80211_sta_bss *bss;
3733
3734 spin_lock_bh(&local->sta_bss_lock);
3735 list_for_each_entry(bss, &local->sta_bss_list, list) {
3736 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3737 spin_unlock_bh(&local->sta_bss_lock);
3738 return -E2BIG;
3739 }
3740 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3741 end_buf);
3742 }
3743 spin_unlock_bh(&local->sta_bss_lock);
3744 return current_ev - buf;
3745}
3746
3747
3748int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3749{
3750 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3751 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3752 kfree(ifsta->extra_ie);
3753 if (len == 0) {
3754 ifsta->extra_ie = NULL;
3755 ifsta->extra_ie_len = 0;
3756 return 0;
3757 }
3758 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3759 if (!ifsta->extra_ie) {
3760 ifsta->extra_ie_len = 0;
3761 return -ENOMEM;
3762 }
3763 memcpy(ifsta->extra_ie, ie, len);
3764 ifsta->extra_ie_len = len;
3765 return 0;
3766}
3767
3768
3769struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3770 struct sk_buff *skb, u8 *bssid,
3771 u8 *addr)
3772{
3773 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3774 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04003775 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07003776 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003777
3778 /* TODO: Could consider removing the least recently used entry and
3779 * allow new one to be added. */
3780 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3781 if (net_ratelimit()) {
3782 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07003783 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07003784 }
3785 return NULL;
3786 }
3787
Joe Perches0795af52007-10-03 17:59:30 -07003788 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04003789 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07003790
3791 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3792 if (!sta)
3793 return NULL;
3794
Johannes Berg238814f2008-01-28 17:19:37 +01003795 sta->flags |= WLAN_STA_AUTHORIZED;
3796
Johannes Berg8318d782008-01-24 19:38:38 +01003797 sta->supp_rates[local->hw.conf.channel->band] =
3798 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003799
3800 rate_control_rate_init(sta, local);
3801
3802 return sta; /* caller will call sta_info_put() */
3803}
3804
3805
3806int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3807{
3808 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3809 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3810
3811 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3812 dev->name, reason);
3813
Johannes Berg51fb61e2007-12-19 01:31:27 +01003814 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3815 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07003816 return -EINVAL;
3817
3818 ieee80211_send_deauth(dev, ifsta, reason);
3819 ieee80211_set_disassoc(dev, ifsta, 1);
3820 return 0;
3821}
3822
3823
3824int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3825{
3826 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3827 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3828
3829 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3830 dev->name, reason);
3831
Johannes Berg51fb61e2007-12-19 01:31:27 +01003832 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003833 return -EINVAL;
3834
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003835 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003836 return -1;
3837
3838 ieee80211_send_disassoc(dev, ifsta, reason);
3839 ieee80211_set_disassoc(dev, ifsta, 0);
3840 return 0;
3841}