blob: 2628222a5085f1f321f54b67f5b967f77eac67e7 [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 Bergc9ee23d2007-08-28 17:01:55 -04001831 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
Jiri Bencf0706e82007-05-05 11:45:53 -07001832
1833 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001834 basic_rates = 0;
1835 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1836
Jiri Bencf0706e82007-05-05 11:45:53 -07001837 for (i = 0; i < elems.supp_rates_len; i++) {
1838 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001839
1840 if (rate > 110)
1841 have_higher_than_11mbit = true;
1842
1843 for (j = 0; j < sband->n_bitrates; j++) {
1844 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001845 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001846 if (elems.supp_rates[i] & 0x80)
1847 basic_rates |= BIT(j);
1848 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001849 }
Johannes Berg8318d782008-01-24 19:38:38 +01001850
Jiri Bencf0706e82007-05-05 11:45:53 -07001851 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1852 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001853
1854 if (rate > 110)
1855 have_higher_than_11mbit = true;
1856
1857 for (j = 0; j < sband->n_bitrates; j++) {
1858 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001859 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001860 if (elems.ext_supp_rates[i] & 0x80)
1861 basic_rates |= BIT(j);
1862 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001863 }
Johannes Berg8318d782008-01-24 19:38:38 +01001864
1865 sta->supp_rates[local->hw.conf.channel->band] = rates;
1866 sdata->basic_rates = basic_rates;
1867
1868 /* cf. IEEE 802.11 9.2.12 */
1869 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1870 have_higher_than_11mbit)
1871 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1872 else
1873 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001874
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001875 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1876 local->ops->conf_ht) {
1877 struct ieee80211_ht_bss_info bss_info;
1878
1879 ieee80211_ht_cap_ie_to_ht_info(
1880 (struct ieee80211_ht_cap *)
1881 elems.ht_cap_elem, &sta->ht_info);
1882 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1883 (struct ieee80211_ht_addt_info *)
1884 elems.ht_info_elem, &bss_info);
1885 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1886 }
1887
Jiri Bencf0706e82007-05-05 11:45:53 -07001888 rate_control_rate_init(sta, local);
1889
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001890 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001891 sta->flags |= WLAN_STA_WME;
1892 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1893 elems.wmm_param_len);
1894 }
1895
Johannes Berg8318d782008-01-24 19:38:38 +01001896 /* set AID, ieee80211_set_associated() will tell the driver */
1897 bss_conf->aid = aid;
1898 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07001899
1900 sta_info_put(sta);
1901
1902 ieee80211_associated(dev, ifsta);
1903}
1904
1905
1906/* Caller must hold local->sta_bss_lock */
1907static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1908 struct ieee80211_sta_bss *bss)
1909{
1910 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1911 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1912 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1913}
1914
1915
1916/* Caller must hold local->sta_bss_lock */
1917static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1918 struct ieee80211_sta_bss *bss)
1919{
1920 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1921 struct ieee80211_sta_bss *b, *prev = NULL;
1922 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1923 while (b) {
1924 if (b == bss) {
1925 if (!prev)
1926 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1927 bss->hnext;
1928 else
1929 prev->hnext = bss->hnext;
1930 break;
1931 }
1932 prev = b;
1933 b = b->hnext;
1934 }
1935}
1936
1937
1938static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001939ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001940 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001941{
1942 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1943 struct ieee80211_sta_bss *bss;
1944
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001945 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07001946 if (!bss)
1947 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001948 atomic_inc(&bss->users);
1949 atomic_inc(&bss->users);
1950 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01001951 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04001952 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1953 memcpy(bss->ssid, ssid, ssid_len);
1954 bss->ssid_len = ssid_len;
1955 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001956
1957 spin_lock_bh(&local->sta_bss_lock);
1958 /* TODO: order by RSSI? */
1959 list_add_tail(&bss->list, &local->sta_bss_list);
1960 __ieee80211_rx_bss_hash_add(dev, bss);
1961 spin_unlock_bh(&local->sta_bss_lock);
1962 return bss;
1963}
1964
1965
1966static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001967ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001968 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001969{
1970 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1971 struct ieee80211_sta_bss *bss;
1972
1973 spin_lock_bh(&local->sta_bss_lock);
1974 bss = local->sta_bss_hash[STA_HASH(bssid)];
1975 while (bss) {
John W. Linvillecffdd302007-10-05 14:23:27 -04001976 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01001977 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04001978 bss->ssid_len == ssid_len &&
1979 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001980 atomic_inc(&bss->users);
1981 break;
1982 }
1983 bss = bss->hnext;
1984 }
1985 spin_unlock_bh(&local->sta_bss_lock);
1986 return bss;
1987}
1988
1989
1990static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1991{
1992 kfree(bss->wpa_ie);
1993 kfree(bss->rsn_ie);
1994 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02001995 kfree(bss->ht_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07001996 kfree(bss);
1997}
1998
1999
2000static void ieee80211_rx_bss_put(struct net_device *dev,
2001 struct ieee80211_sta_bss *bss)
2002{
2003 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2004 if (!atomic_dec_and_test(&bss->users))
2005 return;
2006
2007 spin_lock_bh(&local->sta_bss_lock);
2008 __ieee80211_rx_bss_hash_del(dev, bss);
2009 list_del(&bss->list);
2010 spin_unlock_bh(&local->sta_bss_lock);
2011 ieee80211_rx_bss_free(bss);
2012}
2013
2014
2015void ieee80211_rx_bss_list_init(struct net_device *dev)
2016{
2017 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2018 spin_lock_init(&local->sta_bss_lock);
2019 INIT_LIST_HEAD(&local->sta_bss_list);
2020}
2021
2022
2023void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2024{
2025 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2026 struct ieee80211_sta_bss *bss, *tmp;
2027
2028 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2029 ieee80211_rx_bss_put(dev, bss);
2030}
2031
2032
2033static void ieee80211_rx_bss_info(struct net_device *dev,
2034 struct ieee80211_mgmt *mgmt,
2035 size_t len,
2036 struct ieee80211_rx_status *rx_status,
2037 int beacon)
2038{
2039 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2040 struct ieee802_11_elems elems;
2041 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002042 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002043 struct ieee80211_sta_bss *bss;
2044 struct sta_info *sta;
2045 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2046 u64 timestamp;
Joe Perches0795af52007-10-03 17:59:30 -07002047 DECLARE_MAC_BUF(mac);
2048 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002049
2050 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2051 return; /* ignore ProbeResp to foreign address */
2052
2053#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002054 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002055 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002056 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002057#endif
2058
2059 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2060 if (baselen > len)
2061 return;
2062
2063 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2064
Johannes Berg51fb61e2007-12-19 01:31:27 +01002065 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002066 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2067#ifdef CONFIG_MAC80211_IBSS_DEBUG
2068 static unsigned long last_tsf_debug = 0;
2069 u64 tsf;
2070 if (local->ops->get_tsf)
2071 tsf = local->ops->get_tsf(local_to_hw(local));
2072 else
2073 tsf = -1LLU;
2074 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
Joe Perches0795af52007-10-03 17:59:30 -07002075 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2076 "%s TSF=0x%llx BCN=0x%llx diff=%lld "
Jiri Bencf0706e82007-05-05 11:45:53 -07002077 "@%lu\n",
Joe Perches0795af52007-10-03 17:59:30 -07002078 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
Jiri Bencf0706e82007-05-05 11:45:53 -07002079 (unsigned long long)tsf,
2080 (unsigned long long)timestamp,
2081 (unsigned long long)(tsf - timestamp),
2082 jiffies);
2083 last_tsf_debug = jiffies;
2084 }
2085#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2086 }
2087
John W. Linville67a4cce2007-10-12 16:40:37 -04002088 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002089
Johannes Berg51fb61e2007-12-19 01:31:27 +01002090 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002091 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2092 (sta = sta_info_get(local, mgmt->sa))) {
Johannes Berg8318d782008-01-24 19:38:38 +01002093 struct ieee80211_supported_band *sband;
2094 struct ieee80211_rate *bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002095 size_t num_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01002096 u64 supp_rates, prev_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002097 int i, j;
2098
Johannes Berg8318d782008-01-24 19:38:38 +01002099 sband = local->hw.wiphy->bands[rx_status->band];
Zhu Yiece8edd2007-11-22 10:53:21 +08002100
Johannes Berg8318d782008-01-24 19:38:38 +01002101 if (!sband) {
2102 WARN_ON(1);
2103 sband = local->hw.wiphy->bands[
2104 local->hw.conf.channel->band];
Zhu Yiece8edd2007-11-22 10:53:21 +08002105 }
Johannes Berg8318d782008-01-24 19:38:38 +01002106
2107 bitrates = sband->bitrates;
2108 num_rates = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002109
2110 supp_rates = 0;
2111 for (i = 0; i < elems.supp_rates_len +
2112 elems.ext_supp_rates_len; i++) {
2113 u8 rate = 0;
2114 int own_rate;
2115 if (i < elems.supp_rates_len)
2116 rate = elems.supp_rates[i];
2117 else if (elems.ext_supp_rates)
2118 rate = elems.ext_supp_rates
2119 [i - elems.supp_rates_len];
2120 own_rate = 5 * (rate & 0x7f);
Jiri Bencf0706e82007-05-05 11:45:53 -07002121 for (j = 0; j < num_rates; j++)
Johannes Berg8318d782008-01-24 19:38:38 +01002122 if (bitrates[j].bitrate == own_rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002123 supp_rates |= BIT(j);
2124 }
2125
Johannes Berg8318d782008-01-24 19:38:38 +01002126 prev_rates = sta->supp_rates[rx_status->band];
2127 sta->supp_rates[rx_status->band] &= supp_rates;
2128 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002129 /* No matching rates - this should not really happen.
2130 * Make sure that at least one rate is marked
2131 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002132 sta->supp_rates[rx_status->band] =
2133 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002134 }
Johannes Berg8318d782008-01-24 19:38:38 +01002135 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002136 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002137 "%s based on beacon info (0x%llx & 0x%llx -> "
2138 "0x%llx)\n",
2139 dev->name, print_mac(mac, sta->addr),
2140 (unsigned long long) prev_rates,
2141 (unsigned long long) supp_rates,
2142 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002143 }
2144 sta_info_put(sta);
2145 }
2146
2147 if (!elems.ssid)
2148 return;
2149
2150 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002151 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002152 else
Johannes Berg8318d782008-01-24 19:38:38 +01002153 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002154
Johannes Berg8318d782008-01-24 19:38:38 +01002155 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002156 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002157 if (!bss) {
Johannes Berg8318d782008-01-24 19:38:38 +01002158 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002159 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002160 if (!bss)
2161 return;
2162 } else {
2163#if 0
2164 /* TODO: order by RSSI? */
2165 spin_lock_bh(&local->sta_bss_lock);
2166 list_move_tail(&bss->list, &local->sta_bss_list);
2167 spin_unlock_bh(&local->sta_bss_lock);
2168#endif
2169 }
2170
Johannes Berg8318d782008-01-24 19:38:38 +01002171 bss->band = rx_status->band;
2172
Jiri Bencf0706e82007-05-05 11:45:53 -07002173 if (bss->probe_resp && beacon) {
2174 /* Do not allow beacon to override data from Probe Response. */
2175 ieee80211_rx_bss_put(dev, bss);
2176 return;
2177 }
2178
Daniel Drake56282212007-07-10 19:32:10 +02002179 /* save the ERP value so that it is available at association time */
2180 if (elems.erp_info && elems.erp_info_len >= 1) {
2181 bss->erp_value = elems.erp_info[0];
2182 bss->has_erp_value = 1;
2183 }
2184
Jiri Bencf0706e82007-05-05 11:45:53 -07002185 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2186 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002187
2188 bss->supp_rates_len = 0;
2189 if (elems.supp_rates) {
2190 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2191 if (clen > elems.supp_rates_len)
2192 clen = elems.supp_rates_len;
2193 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2194 clen);
2195 bss->supp_rates_len += clen;
2196 }
2197 if (elems.ext_supp_rates) {
2198 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2199 if (clen > elems.ext_supp_rates_len)
2200 clen = elems.ext_supp_rates_len;
2201 memcpy(&bss->supp_rates[bss->supp_rates_len],
2202 elems.ext_supp_rates, clen);
2203 bss->supp_rates_len += clen;
2204 }
2205
2206 if (elems.wpa &&
2207 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2208 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2209 kfree(bss->wpa_ie);
2210 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2211 if (bss->wpa_ie) {
2212 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2213 bss->wpa_ie_len = elems.wpa_len + 2;
2214 } else
2215 bss->wpa_ie_len = 0;
2216 } else if (!elems.wpa && bss->wpa_ie) {
2217 kfree(bss->wpa_ie);
2218 bss->wpa_ie = NULL;
2219 bss->wpa_ie_len = 0;
2220 }
2221
2222 if (elems.rsn &&
2223 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2224 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2225 kfree(bss->rsn_ie);
2226 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2227 if (bss->rsn_ie) {
2228 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2229 bss->rsn_ie_len = elems.rsn_len + 2;
2230 } else
2231 bss->rsn_ie_len = 0;
2232 } else if (!elems.rsn && bss->rsn_ie) {
2233 kfree(bss->rsn_ie);
2234 bss->rsn_ie = NULL;
2235 bss->rsn_ie_len = 0;
2236 }
2237
2238 if (elems.wmm_param &&
2239 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2240 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2241 kfree(bss->wmm_ie);
2242 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2243 if (bss->wmm_ie) {
2244 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2245 elems.wmm_param_len + 2);
2246 bss->wmm_ie_len = elems.wmm_param_len + 2;
2247 } else
2248 bss->wmm_ie_len = 0;
2249 } else if (!elems.wmm_param && bss->wmm_ie) {
2250 kfree(bss->wmm_ie);
2251 bss->wmm_ie = NULL;
2252 bss->wmm_ie_len = 0;
2253 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002254 if (elems.ht_cap_elem &&
2255 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2256 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2257 kfree(bss->ht_ie);
2258 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2259 if (bss->ht_ie) {
2260 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2261 elems.ht_cap_elem_len + 2);
2262 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2263 } else
2264 bss->ht_ie_len = 0;
2265 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2266 kfree(bss->ht_ie);
2267 bss->ht_ie = NULL;
2268 bss->ht_ie_len = 0;
2269 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002270
Jiri Bencf0706e82007-05-05 11:45:53 -07002271 bss->timestamp = timestamp;
2272 bss->last_update = jiffies;
2273 bss->rssi = rx_status->ssi;
2274 bss->signal = rx_status->signal;
2275 bss->noise = rx_status->noise;
2276 if (!beacon)
2277 bss->probe_resp++;
2278 ieee80211_rx_bss_put(dev, bss);
2279}
2280
2281
2282static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2283 struct ieee80211_mgmt *mgmt,
2284 size_t len,
2285 struct ieee80211_rx_status *rx_status)
2286{
2287 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2288}
2289
2290
2291static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2292 struct ieee80211_mgmt *mgmt,
2293 size_t len,
2294 struct ieee80211_rx_status *rx_status)
2295{
Jiri Bencf0706e82007-05-05 11:45:53 -07002296 struct ieee80211_sub_if_data *sdata;
2297 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002298 size_t baselen;
2299 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002300 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2301 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002302 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002303
2304 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2305
2306 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002307 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002308 return;
2309 ifsta = &sdata->u.sta;
2310
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002311 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002312 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2313 return;
2314
2315 /* Process beacon from the current BSS */
2316 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2317 if (baselen > len)
2318 return;
2319
John W. Linville67a4cce2007-10-12 16:40:37 -04002320 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002321
Daniel Drake56282212007-07-10 19:32:10 +02002322 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002323 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002324
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002325 if (elems.ht_cap_elem && elems.ht_info_elem &&
2326 elems.wmm_param && local->ops->conf_ht &&
2327 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2328 struct ieee80211_ht_bss_info bss_info;
2329
2330 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2331 (struct ieee80211_ht_addt_info *)
2332 elems.ht_info_elem, &bss_info);
2333 /* check if AP changed bss inforamation */
2334 if ((conf->ht_bss_conf.primary_channel !=
2335 bss_info.primary_channel) ||
2336 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2337 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2338 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2339 &bss_info);
2340 }
2341
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002342 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002343 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2344 elems.wmm_param_len);
2345 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002346
2347 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002348}
2349
2350
2351static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2352 struct ieee80211_if_sta *ifsta,
2353 struct ieee80211_mgmt *mgmt,
2354 size_t len,
2355 struct ieee80211_rx_status *rx_status)
2356{
2357 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2358 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2359 int tx_last_beacon;
2360 struct sk_buff *skb;
2361 struct ieee80211_mgmt *resp;
2362 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002363 DECLARE_MAC_BUF(mac);
2364#ifdef CONFIG_MAC80211_IBSS_DEBUG
2365 DECLARE_MAC_BUF(mac2);
2366 DECLARE_MAC_BUF(mac3);
2367#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002368
Johannes Berg51fb61e2007-12-19 01:31:27 +01002369 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002370 ifsta->state != IEEE80211_IBSS_JOINED ||
2371 len < 24 + 2 || !ifsta->probe_resp)
2372 return;
2373
2374 if (local->ops->tx_last_beacon)
2375 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2376 else
2377 tx_last_beacon = 1;
2378
2379#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002380 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2381 "%s (tx_last_beacon=%d)\n",
2382 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2383 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002384#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2385
2386 if (!tx_last_beacon)
2387 return;
2388
2389 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2390 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2391 return;
2392
2393 end = ((u8 *) mgmt) + len;
2394 pos = mgmt->u.probe_req.variable;
2395 if (pos[0] != WLAN_EID_SSID ||
2396 pos + 2 + pos[1] > end) {
2397 if (net_ratelimit()) {
2398 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002399 "from %s\n",
2400 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002401 }
2402 return;
2403 }
2404 if (pos[1] != 0 &&
2405 (pos[1] != ifsta->ssid_len ||
2406 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2407 /* Ignore ProbeReq for foreign SSID */
2408 return;
2409 }
2410
2411 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002412 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002413 if (!skb)
2414 return;
2415
2416 resp = (struct ieee80211_mgmt *) skb->data;
2417 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2418#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002419 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2420 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002421#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2422 ieee80211_sta_tx(dev, skb, 0);
2423}
2424
Johannes Berg4e20cb22007-12-19 01:31:24 +01002425static void ieee80211_rx_mgmt_action(struct net_device *dev,
2426 struct ieee80211_if_sta *ifsta,
2427 struct ieee80211_mgmt *mgmt,
2428 size_t len)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002429{
2430 if (len < IEEE80211_MIN_ACTION_SIZE)
2431 return;
2432
2433 switch (mgmt->u.action.category) {
2434 case WLAN_CATEGORY_BACK:
2435 switch (mgmt->u.action.u.addba_req.action_code) {
2436 case WLAN_ACTION_ADDBA_REQ:
2437 if (len < (IEEE80211_MIN_ACTION_SIZE +
2438 sizeof(mgmt->u.action.u.addba_req)))
2439 break;
2440 ieee80211_sta_process_addba_request(dev, mgmt, len);
2441 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002442 case WLAN_ACTION_ADDBA_RESP:
2443 if (len < (IEEE80211_MIN_ACTION_SIZE +
2444 sizeof(mgmt->u.action.u.addba_resp)))
2445 break;
2446 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2447 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002448 case WLAN_ACTION_DELBA:
2449 if (len < (IEEE80211_MIN_ACTION_SIZE +
2450 sizeof(mgmt->u.action.u.delba)))
2451 break;
2452 ieee80211_sta_process_delba(dev, mgmt, len);
2453 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002454 default:
2455 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002456 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002457 dev->name);
2458 break;
2459 }
2460 break;
2461 default:
2462 break;
2463 }
2464}
Jiri Bencf0706e82007-05-05 11:45:53 -07002465
2466void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2467 struct ieee80211_rx_status *rx_status)
2468{
2469 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2470 struct ieee80211_sub_if_data *sdata;
2471 struct ieee80211_if_sta *ifsta;
2472 struct ieee80211_mgmt *mgmt;
2473 u16 fc;
2474
2475 if (skb->len < 24)
2476 goto fail;
2477
2478 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2479 ifsta = &sdata->u.sta;
2480
2481 mgmt = (struct ieee80211_mgmt *) skb->data;
2482 fc = le16_to_cpu(mgmt->frame_control);
2483
2484 switch (fc & IEEE80211_FCTL_STYPE) {
2485 case IEEE80211_STYPE_PROBE_REQ:
2486 case IEEE80211_STYPE_PROBE_RESP:
2487 case IEEE80211_STYPE_BEACON:
2488 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2489 case IEEE80211_STYPE_AUTH:
2490 case IEEE80211_STYPE_ASSOC_RESP:
2491 case IEEE80211_STYPE_REASSOC_RESP:
2492 case IEEE80211_STYPE_DEAUTH:
2493 case IEEE80211_STYPE_DISASSOC:
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002494 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002495 skb_queue_tail(&ifsta->skb_queue, skb);
2496 queue_work(local->hw.workqueue, &ifsta->work);
2497 return;
2498 default:
2499 printk(KERN_DEBUG "%s: received unknown management frame - "
2500 "stype=%d\n", dev->name,
2501 (fc & IEEE80211_FCTL_STYPE) >> 4);
2502 break;
2503 }
2504
2505 fail:
2506 kfree_skb(skb);
2507}
2508
2509
2510static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2511 struct sk_buff *skb)
2512{
2513 struct ieee80211_rx_status *rx_status;
2514 struct ieee80211_sub_if_data *sdata;
2515 struct ieee80211_if_sta *ifsta;
2516 struct ieee80211_mgmt *mgmt;
2517 u16 fc;
2518
2519 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2520 ifsta = &sdata->u.sta;
2521
2522 rx_status = (struct ieee80211_rx_status *) skb->cb;
2523 mgmt = (struct ieee80211_mgmt *) skb->data;
2524 fc = le16_to_cpu(mgmt->frame_control);
2525
2526 switch (fc & IEEE80211_FCTL_STYPE) {
2527 case IEEE80211_STYPE_PROBE_REQ:
2528 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2529 rx_status);
2530 break;
2531 case IEEE80211_STYPE_PROBE_RESP:
2532 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2533 break;
2534 case IEEE80211_STYPE_BEACON:
2535 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2536 break;
2537 case IEEE80211_STYPE_AUTH:
2538 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2539 break;
2540 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002541 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002542 break;
2543 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002544 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002545 break;
2546 case IEEE80211_STYPE_DEAUTH:
2547 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2548 break;
2549 case IEEE80211_STYPE_DISASSOC:
2550 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2551 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002552 case IEEE80211_STYPE_ACTION:
2553 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len);
2554 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07002555 }
2556
2557 kfree_skb(skb);
2558}
2559
2560
Zhu Yiece8edd2007-11-22 10:53:21 +08002561ieee80211_txrx_result
2562ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2563 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002564{
2565 struct ieee80211_mgmt *mgmt;
2566 u16 fc;
2567
Zhu Yiece8edd2007-11-22 10:53:21 +08002568 if (skb->len < 2)
2569 return TXRX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -07002570
2571 mgmt = (struct ieee80211_mgmt *) skb->data;
2572 fc = le16_to_cpu(mgmt->frame_control);
2573
Zhu Yiece8edd2007-11-22 10:53:21 +08002574 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
2575 return TXRX_CONTINUE;
2576
2577 if (skb->len < 24)
2578 return TXRX_DROP;
2579
Jiri Bencf0706e82007-05-05 11:45:53 -07002580 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2581 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2582 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2583 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002584 dev_kfree_skb(skb);
2585 return TXRX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002586 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2587 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2588 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002589 dev_kfree_skb(skb);
2590 return TXRX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002591 }
2592 }
Zhu Yiece8edd2007-11-22 10:53:21 +08002593 return TXRX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002594}
2595
2596
2597static int ieee80211_sta_active_ibss(struct net_device *dev)
2598{
2599 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2600 int active = 0;
2601 struct sta_info *sta;
2602
Michael Wube8755e2007-07-27 15:43:23 +02002603 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07002604 list_for_each_entry(sta, &local->sta_list, list) {
2605 if (sta->dev == dev &&
2606 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2607 jiffies)) {
2608 active++;
2609 break;
2610 }
2611 }
Michael Wube8755e2007-07-27 15:43:23 +02002612 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07002613
2614 return active;
2615}
2616
2617
2618static void ieee80211_sta_expire(struct net_device *dev)
2619{
2620 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2621 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02002622 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07002623 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07002624
Michael Wube8755e2007-07-27 15:43:23 +02002625 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07002626 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2627 if (time_after(jiffies, sta->last_rx +
2628 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
Joe Perches0795af52007-10-03 17:59:30 -07002629 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2630 dev->name, print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +02002631 __sta_info_get(sta);
2632 sta_info_remove(sta);
2633 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07002634 }
Michael Wube8755e2007-07-27 15:43:23 +02002635 write_unlock_bh(&local->sta_lock);
2636
2637 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
2638 sta_info_free(sta);
2639 sta_info_put(sta);
2640 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002641}
2642
2643
2644static void ieee80211_sta_merge_ibss(struct net_device *dev,
2645 struct ieee80211_if_sta *ifsta)
2646{
2647 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2648
2649 ieee80211_sta_expire(dev);
2650 if (ieee80211_sta_active_ibss(dev))
2651 return;
2652
2653 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2654 "IBSS networks with same SSID (merge)\n", dev->name);
2655 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2656}
2657
2658
2659void ieee80211_sta_timer(unsigned long data)
2660{
2661 struct ieee80211_sub_if_data *sdata =
2662 (struct ieee80211_sub_if_data *) data;
2663 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2664 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2665
2666 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2667 queue_work(local->hw.workqueue, &ifsta->work);
2668}
2669
2670
2671void ieee80211_sta_work(struct work_struct *work)
2672{
2673 struct ieee80211_sub_if_data *sdata =
2674 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2675 struct net_device *dev = sdata->dev;
2676 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2677 struct ieee80211_if_sta *ifsta;
2678 struct sk_buff *skb;
2679
2680 if (!netif_running(dev))
2681 return;
2682
Zhu Yiece8edd2007-11-22 10:53:21 +08002683 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07002684 return;
2685
Johannes Berg51fb61e2007-12-19 01:31:27 +01002686 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2687 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002688 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01002689 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07002690 return;
2691 }
2692 ifsta = &sdata->u.sta;
2693
2694 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2695 ieee80211_sta_rx_queued_mgmt(dev, skb);
2696
2697 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2698 ifsta->state != IEEE80211_ASSOCIATE &&
2699 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01002700 if (ifsta->scan_ssid_len)
2701 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
2702 else
2703 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002704 return;
2705 }
2706
2707 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2708 if (ieee80211_sta_config_auth(dev, ifsta))
2709 return;
2710 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2711 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2712 return;
2713
2714 switch (ifsta->state) {
2715 case IEEE80211_DISABLED:
2716 break;
2717 case IEEE80211_AUTHENTICATE:
2718 ieee80211_authenticate(dev, ifsta);
2719 break;
2720 case IEEE80211_ASSOCIATE:
2721 ieee80211_associate(dev, ifsta);
2722 break;
2723 case IEEE80211_ASSOCIATED:
2724 ieee80211_associated(dev, ifsta);
2725 break;
2726 case IEEE80211_IBSS_SEARCH:
2727 ieee80211_sta_find_ibss(dev, ifsta);
2728 break;
2729 case IEEE80211_IBSS_JOINED:
2730 ieee80211_sta_merge_ibss(dev, ifsta);
2731 break;
2732 default:
2733 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2734 ifsta->state);
2735 break;
2736 }
2737
2738 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2739 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2740 "mixed-cell disabled - disassociate\n", dev->name);
2741
2742 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2743 ieee80211_set_disassoc(dev, ifsta, 0);
2744 }
2745}
2746
2747
2748static void ieee80211_sta_reset_auth(struct net_device *dev,
2749 struct ieee80211_if_sta *ifsta)
2750{
2751 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2752
2753 if (local->ops->reset_tsf) {
2754 /* Reset own TSF to allow time synchronization work. */
2755 local->ops->reset_tsf(local_to_hw(local));
2756 }
2757
2758 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2759
2760
2761 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2762 ifsta->auth_alg = WLAN_AUTH_OPEN;
2763 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2764 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2765 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2766 ifsta->auth_alg = WLAN_AUTH_LEAP;
2767 else
2768 ifsta->auth_alg = WLAN_AUTH_OPEN;
2769 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2770 ifsta->auth_alg);
2771 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002772 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2773 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002774 netif_carrier_off(dev);
2775}
2776
2777
2778void ieee80211_sta_req_auth(struct net_device *dev,
2779 struct ieee80211_if_sta *ifsta)
2780{
2781 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2782 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2783
Johannes Berg51fb61e2007-12-19 01:31:27 +01002784 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002785 return;
2786
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002787 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2788 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2789 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2790 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002791 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2792 queue_work(local->hw.workqueue, &ifsta->work);
2793 }
2794}
2795
2796static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2797 const char *ssid, int ssid_len)
2798{
2799 int tmp, hidden_ssid;
2800
Michael Wu48225702007-10-19 17:14:36 -04002801 if (ssid_len == ifsta->ssid_len &&
2802 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07002803 return 1;
2804
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002805 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07002806 return 0;
2807
2808 hidden_ssid = 1;
2809 tmp = ssid_len;
2810 while (tmp--) {
2811 if (ssid[tmp] != '\0') {
2812 hidden_ssid = 0;
2813 break;
2814 }
2815 }
2816
2817 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2818 return 1;
2819
2820 if (ssid_len == 1 && ssid[0] == ' ')
2821 return 1;
2822
2823 return 0;
2824}
2825
2826static int ieee80211_sta_config_auth(struct net_device *dev,
2827 struct ieee80211_if_sta *ifsta)
2828{
2829 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2830 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2831 struct ieee80211_sta_bss *bss, *selected = NULL;
2832 int top_rssi = 0, freq;
2833
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002834 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2835 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002836 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002837 ieee80211_sta_reset_auth(dev, ifsta);
2838 return 0;
2839 }
2840
2841 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01002842 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002843 list_for_each_entry(bss, &local->sta_bss_list, list) {
2844 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2845 continue;
2846
2847 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2848 !!sdata->default_key)
2849 continue;
2850
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002851 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2852 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07002853 continue;
2854
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002855 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002856 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2857 continue;
2858
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002859 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002860 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2861 continue;
2862
2863 if (!selected || top_rssi < bss->rssi) {
2864 selected = bss;
2865 top_rssi = bss->rssi;
2866 }
2867 }
2868 if (selected)
2869 atomic_inc(&selected->users);
2870 spin_unlock_bh(&local->sta_bss_lock);
2871
2872 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01002873 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002874 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07002875 ieee80211_sta_set_ssid(dev, selected->ssid,
2876 selected->ssid_len);
2877 ieee80211_sta_set_bssid(dev, selected->bssid);
2878 ieee80211_rx_bss_put(dev, selected);
2879 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002880 ieee80211_sta_reset_auth(dev, ifsta);
2881 return 0;
2882 } else {
2883 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002884 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04002885 ieee80211_sta_start_scan(dev, NULL, 0);
2886 else
2887 ieee80211_sta_start_scan(dev, ifsta->ssid,
2888 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002889 ifsta->state = IEEE80211_AUTHENTICATE;
2890 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2891 } else
2892 ifsta->state = IEEE80211_DISABLED;
2893 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002894 return -1;
2895}
2896
2897static int ieee80211_sta_join_ibss(struct net_device *dev,
2898 struct ieee80211_if_sta *ifsta,
2899 struct ieee80211_sta_bss *bss)
2900{
2901 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2902 int res, rates, i, j;
2903 struct sk_buff *skb;
2904 struct ieee80211_mgmt *mgmt;
2905 struct ieee80211_tx_control control;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002906 struct rate_selection ratesel;
Jiri Bencf0706e82007-05-05 11:45:53 -07002907 u8 *pos;
2908 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +01002909 struct ieee80211_supported_band *sband;
2910
2911 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002912
2913 /* Remove possible STA entries from other IBSS networks. */
2914 sta_info_flush(local, NULL);
2915
2916 if (local->ops->reset_tsf) {
2917 /* Reset own TSF to allow time synchronization work. */
2918 local->ops->reset_tsf(local_to_hw(local));
2919 }
2920 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2921 res = ieee80211_if_config(dev);
2922 if (res)
2923 return res;
2924
2925 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2926
2927 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2928 sdata->drop_unencrypted = bss->capability &
2929 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2930
Johannes Berg8318d782008-01-24 19:38:38 +01002931 res = ieee80211_set_freq(local, bss->freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07002932
Johannes Berg8318d782008-01-24 19:38:38 +01002933 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2934 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2935 "%d MHz\n", dev->name, local->oper_channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07002936 return -1;
2937 }
2938
2939 /* Set beacon template based on scan results */
2940 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2941 do {
2942 if (!skb)
2943 break;
2944
2945 skb_reserve(skb, local->hw.extra_tx_headroom);
2946
2947 mgmt = (struct ieee80211_mgmt *)
2948 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2949 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2950 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2951 IEEE80211_STYPE_BEACON);
2952 memset(mgmt->da, 0xff, ETH_ALEN);
2953 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2954 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2955 mgmt->u.beacon.beacon_int =
2956 cpu_to_le16(local->hw.conf.beacon_int);
2957 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2958
2959 pos = skb_put(skb, 2 + ifsta->ssid_len);
2960 *pos++ = WLAN_EID_SSID;
2961 *pos++ = ifsta->ssid_len;
2962 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2963
2964 rates = bss->supp_rates_len;
2965 if (rates > 8)
2966 rates = 8;
2967 pos = skb_put(skb, 2 + rates);
2968 *pos++ = WLAN_EID_SUPP_RATES;
2969 *pos++ = rates;
2970 memcpy(pos, bss->supp_rates, rates);
2971
Johannes Berg8318d782008-01-24 19:38:38 +01002972 if (bss->band == IEEE80211_BAND_2GHZ) {
2973 pos = skb_put(skb, 2 + 1);
2974 *pos++ = WLAN_EID_DS_PARAMS;
2975 *pos++ = 1;
2976 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2977 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002978
2979 pos = skb_put(skb, 2 + 2);
2980 *pos++ = WLAN_EID_IBSS_PARAMS;
2981 *pos++ = 2;
2982 /* FIX: set ATIM window based on scan results */
2983 *pos++ = 0;
2984 *pos++ = 0;
2985
2986 if (bss->supp_rates_len > 8) {
2987 rates = bss->supp_rates_len - 8;
2988 pos = skb_put(skb, 2 + rates);
2989 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2990 *pos++ = rates;
2991 memcpy(pos, &bss->supp_rates[8], rates);
2992 }
2993
2994 memset(&control, 0, sizeof(control));
Johannes Berg8318d782008-01-24 19:38:38 +01002995 rate_control_get_rate(dev, sband, skb, &ratesel);
Mattias Nissler1abbe492007-12-20 13:50:07 +01002996 if (!ratesel.rate) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002997 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2998 "for IBSS beacon\n", dev->name);
2999 break;
3000 }
Ivo van Doorn0f7054e2008-01-13 14:16:47 +01003001 control.vif = &sdata->vif;
Johannes Berg8318d782008-01-24 19:38:38 +01003002 control.tx_rate = ratesel.rate;
3003 if (sdata->bss_conf.use_short_preamble &&
3004 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
3005 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003006 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
Jiri Bencf0706e82007-05-05 11:45:53 -07003007 control.flags |= IEEE80211_TXCTL_NO_ACK;
3008 control.retry_limit = 1;
3009
3010 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
3011 if (ifsta->probe_resp) {
3012 mgmt = (struct ieee80211_mgmt *)
3013 ifsta->probe_resp->data;
3014 mgmt->frame_control =
3015 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
3016 IEEE80211_STYPE_PROBE_RESP);
3017 } else {
3018 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
3019 "template for IBSS\n", dev->name);
3020 }
3021
3022 if (local->ops->beacon_update &&
3023 local->ops->beacon_update(local_to_hw(local),
3024 skb, &control) == 0) {
3025 printk(KERN_DEBUG "%s: Configured IBSS beacon "
3026 "template based on scan results\n", dev->name);
3027 skb = NULL;
3028 }
3029
3030 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003031 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003032 for (i = 0; i < bss->supp_rates_len; i++) {
3033 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01003034 for (j = 0; j < sband->n_bitrates; j++)
3035 if (sband->bitrates[j].bitrate == bitrate)
Jiri Bencf0706e82007-05-05 11:45:53 -07003036 rates |= BIT(j);
3037 }
Johannes Berg8318d782008-01-24 19:38:38 +01003038 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003039 } while (0);
3040
3041 if (skb) {
3042 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
3043 "template\n", dev->name);
3044 dev_kfree_skb(skb);
3045 }
3046
3047 ifsta->state = IEEE80211_IBSS_JOINED;
3048 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3049
3050 ieee80211_rx_bss_put(dev, bss);
3051
3052 return res;
3053}
3054
3055
3056static int ieee80211_sta_create_ibss(struct net_device *dev,
3057 struct ieee80211_if_sta *ifsta)
3058{
3059 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3060 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003061 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003062 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003063 u8 bssid[ETH_ALEN], *pos;
3064 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003065 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003066
3067#if 0
3068 /* Easier testing, use fixed BSSID. */
3069 memset(bssid, 0xfe, ETH_ALEN);
3070#else
3071 /* Generate random, not broadcast, locally administered BSSID. Mix in
3072 * own MAC address to make sure that devices that do not have proper
3073 * random number generator get different BSSID. */
3074 get_random_bytes(bssid, ETH_ALEN);
3075 for (i = 0; i < ETH_ALEN; i++)
3076 bssid[i] ^= dev->dev_addr[i];
3077 bssid[0] &= ~0x01;
3078 bssid[0] |= 0x02;
3079#endif
3080
Joe Perches0795af52007-10-03 17:59:30 -07003081 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3082 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003083
Johannes Berg8318d782008-01-24 19:38:38 +01003084 bss = ieee80211_rx_bss_add(dev, bssid,
3085 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003086 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003087 if (!bss)
3088 return -ENOMEM;
3089
Johannes Berg8318d782008-01-24 19:38:38 +01003090 bss->band = local->hw.conf.channel->band;
3091 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003092
3093 if (local->hw.conf.beacon_int == 0)
3094 local->hw.conf.beacon_int = 100;
3095 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003096 bss->last_update = jiffies;
3097 bss->capability = WLAN_CAPABILITY_IBSS;
3098 if (sdata->default_key) {
3099 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3100 } else
3101 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003102 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003103 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003104 for (i = 0; i < sband->n_bitrates; i++) {
3105 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003106 *pos++ = (u8) (rate / 5);
3107 }
3108
3109 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3110}
3111
3112
3113static int ieee80211_sta_find_ibss(struct net_device *dev,
3114 struct ieee80211_if_sta *ifsta)
3115{
3116 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3117 struct ieee80211_sta_bss *bss;
3118 int found = 0;
3119 u8 bssid[ETH_ALEN];
3120 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003121 DECLARE_MAC_BUF(mac);
3122 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003123
3124 if (ifsta->ssid_len == 0)
3125 return -EINVAL;
3126
3127 active_ibss = ieee80211_sta_active_ibss(dev);
3128#ifdef CONFIG_MAC80211_IBSS_DEBUG
3129 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3130 dev->name, active_ibss);
3131#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3132 spin_lock_bh(&local->sta_bss_lock);
3133 list_for_each_entry(bss, &local->sta_bss_list, list) {
3134 if (ifsta->ssid_len != bss->ssid_len ||
3135 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3136 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3137 continue;
3138#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003139 printk(KERN_DEBUG " bssid=%s found\n",
3140 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003141#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3142 memcpy(bssid, bss->bssid, ETH_ALEN);
3143 found = 1;
3144 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3145 break;
3146 }
3147 spin_unlock_bh(&local->sta_bss_lock);
3148
3149#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003150 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3151 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003152#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3153 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003154 (bss = ieee80211_rx_bss_get(dev, bssid,
3155 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003156 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003157 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003158 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003159 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003160 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3161 }
3162#ifdef CONFIG_MAC80211_IBSS_DEBUG
3163 printk(KERN_DEBUG " did not try to join ibss\n");
3164#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3165
3166 /* Selected IBSS not found in current scan results - try to scan */
3167 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3168 !ieee80211_sta_active_ibss(dev)) {
3169 mod_timer(&ifsta->timer, jiffies +
3170 IEEE80211_IBSS_MERGE_INTERVAL);
3171 } else if (time_after(jiffies, local->last_scan_completed +
3172 IEEE80211_SCAN_INTERVAL)) {
3173 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3174 "join\n", dev->name);
3175 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3176 ifsta->ssid_len);
3177 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3178 int interval = IEEE80211_SCAN_INTERVAL;
3179
3180 if (time_after(jiffies, ifsta->ibss_join_req +
3181 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003182 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003183 (!(local->oper_channel->flags &
3184 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003185 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003186 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003187 printk(KERN_DEBUG "%s: IBSS not allowed on"
3188 " %d MHz\n", dev->name,
3189 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003190 }
3191
3192 /* No IBSS found - decrease scan interval and continue
3193 * scanning. */
3194 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3195 }
3196
3197 ifsta->state = IEEE80211_IBSS_SEARCH;
3198 mod_timer(&ifsta->timer, jiffies + interval);
3199 return 0;
3200 }
3201
3202 return 0;
3203}
3204
3205
3206int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3207{
Johannes Berg8318d782008-01-24 19:38:38 +01003208 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003209 struct ieee80211_if_sta *ifsta;
3210 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3211
3212 if (len > IEEE80211_MAX_SSID_LEN)
3213 return -EINVAL;
3214
3215 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
3216 * not defined. */
3217 if (local->ops->conf_tx) {
3218 struct ieee80211_tx_queue_params qparam;
3219 int i;
3220
3221 memset(&qparam, 0, sizeof(qparam));
Johannes Berg8318d782008-01-24 19:38:38 +01003222
Jiri Bencf0706e82007-05-05 11:45:53 -07003223 qparam.aifs = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003224
3225 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
3226 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
3227 qparam.cw_min = 31;
3228 else
3229 qparam.cw_min = 15;
3230
Jiri Bencf0706e82007-05-05 11:45:53 -07003231 qparam.cw_max = 1023;
3232 qparam.burst_time = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003233
Jiri Bencf0706e82007-05-05 11:45:53 -07003234 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -07003235 local->ops->conf_tx(local_to_hw(local),
3236 i + IEEE80211_TX_QUEUE_DATA0,
3237 &qparam);
Johannes Berg8318d782008-01-24 19:38:38 +01003238
Jiri Bencf0706e82007-05-05 11:45:53 -07003239 /* IBSS uses different parameters for Beacon sending */
3240 qparam.cw_min++;
3241 qparam.cw_min *= 2;
3242 qparam.cw_min--;
3243 local->ops->conf_tx(local_to_hw(local),
3244 IEEE80211_TX_QUEUE_BEACON, &qparam);
3245 }
3246
Jiri Bencf0706e82007-05-05 11:45:53 -07003247 ifsta = &sdata->u.sta;
3248
3249 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003250 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003251 memcpy(ifsta->ssid, ssid, len);
3252 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3253 ifsta->ssid_len = len;
3254
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003255 if (len)
3256 ifsta->flags |= IEEE80211_STA_SSID_SET;
3257 else
3258 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003259 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003260 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003261 ifsta->ibss_join_req = jiffies;
3262 ifsta->state = IEEE80211_IBSS_SEARCH;
3263 return ieee80211_sta_find_ibss(dev, ifsta);
3264 }
3265 return 0;
3266}
3267
3268
3269int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3270{
3271 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3272 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3273 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3274 *len = ifsta->ssid_len;
3275 return 0;
3276}
3277
3278
3279int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3280{
3281 struct ieee80211_sub_if_data *sdata;
3282 struct ieee80211_if_sta *ifsta;
3283 int res;
3284
3285 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3286 ifsta = &sdata->u.sta;
3287
3288 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3289 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3290 res = ieee80211_if_config(dev);
3291 if (res) {
3292 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3293 "the low-level driver\n", dev->name);
3294 return res;
3295 }
3296 }
3297
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003298 if (is_valid_ether_addr(bssid))
3299 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003300 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003301 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3302
Jiri Bencf0706e82007-05-05 11:45:53 -07003303 return 0;
3304}
3305
3306
3307static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3308 struct ieee80211_sub_if_data *sdata,
3309 int powersave)
3310{
3311 struct sk_buff *skb;
3312 struct ieee80211_hdr *nullfunc;
3313 u16 fc;
3314
3315 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3316 if (!skb) {
3317 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3318 "frame\n", sdata->dev->name);
3319 return;
3320 }
3321 skb_reserve(skb, local->hw.extra_tx_headroom);
3322
3323 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3324 memset(nullfunc, 0, 24);
3325 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3326 IEEE80211_FCTL_TODS;
3327 if (powersave)
3328 fc |= IEEE80211_FCTL_PM;
3329 nullfunc->frame_control = cpu_to_le16(fc);
3330 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3331 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3332 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3333
3334 ieee80211_sta_tx(sdata->dev, skb, 0);
3335}
3336
3337
3338void ieee80211_scan_completed(struct ieee80211_hw *hw)
3339{
3340 struct ieee80211_local *local = hw_to_local(hw);
3341 struct net_device *dev = local->scan_dev;
3342 struct ieee80211_sub_if_data *sdata;
3343 union iwreq_data wrqu;
3344
3345 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003346 memset(&wrqu, 0, sizeof(wrqu));
3347 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003348
Zhu Yiece8edd2007-11-22 10:53:21 +08003349 if (local->sta_hw_scanning) {
3350 local->sta_hw_scanning = 0;
3351 goto done;
3352 }
3353
3354 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003355 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003356 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003357 "channel after scan\n", dev->name);
3358
Johannes Berg4150c572007-09-17 01:29:23 -04003359
3360 netif_tx_lock_bh(local->mdev);
3361 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3362 local->ops->configure_filter(local_to_hw(local),
3363 FIF_BCN_PRBRESP_PROMISC,
3364 &local->filter_flags,
3365 local->mdev->mc_count,
3366 local->mdev->mc_list);
3367
3368 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003369
Johannes Berg79010422007-09-18 17:29:21 -04003370 rcu_read_lock();
3371 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003372
3373 /* No need to wake the master device. */
3374 if (sdata->dev == local->mdev)
3375 continue;
3376
Johannes Berg51fb61e2007-12-19 01:31:27 +01003377 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003378 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07003379 ieee80211_send_nullfunc(local, sdata, 0);
3380 ieee80211_sta_timer((unsigned long)sdata);
3381 }
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003382
Jiri Bencf0706e82007-05-05 11:45:53 -07003383 netif_wake_queue(sdata->dev);
3384 }
Johannes Berg79010422007-09-18 17:29:21 -04003385 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003386
Zhu Yiece8edd2007-11-22 10:53:21 +08003387done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003388 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003389 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003390 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003391 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003392 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3393 !ieee80211_sta_active_ibss(dev)))
3394 ieee80211_sta_find_ibss(dev, ifsta);
3395 }
3396}
3397EXPORT_SYMBOL(ieee80211_scan_completed);
3398
3399void ieee80211_sta_scan_work(struct work_struct *work)
3400{
3401 struct ieee80211_local *local =
3402 container_of(work, struct ieee80211_local, scan_work.work);
3403 struct net_device *dev = local->scan_dev;
3404 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003405 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003406 struct ieee80211_channel *chan;
3407 int skip;
3408 unsigned long next_delay = 0;
3409
Zhu Yiece8edd2007-11-22 10:53:21 +08003410 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003411 return;
3412
3413 switch (local->scan_state) {
3414 case SCAN_SET_CHANNEL:
Johannes Berg8318d782008-01-24 19:38:38 +01003415 /* get current scan band */
3416 if (local->scan_band < IEEE80211_NUM_BANDS)
3417 sband = local->hw.wiphy->bands[local->scan_band];
3418 else
3419 sband = NULL;
3420
3421 /* if we started at an unsupported one, advance */
3422 while (!sband && local->scan_band < IEEE80211_NUM_BANDS) {
3423 local->scan_band++;
3424 sband = local->hw.wiphy->bands[local->scan_band];
3425 local->scan_channel_idx = 0;
3426 }
3427
3428 if (!sband ||
3429 (local->scan_channel_idx >= sband->n_channels &&
3430 local->scan_band >= IEEE80211_NUM_BANDS)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003431 ieee80211_scan_completed(local_to_hw(local));
3432 return;
3433 }
Johannes Berg8318d782008-01-24 19:38:38 +01003434 skip = 0;
3435 chan = &sband->channels[local->scan_channel_idx];
3436
3437 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003438 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003439 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003440 skip = 1;
3441
3442 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003443 local->scan_channel = chan;
3444 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003445 printk(KERN_DEBUG "%s: failed to set freq to "
3446 "%d MHz for scan\n", dev->name,
3447 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003448 skip = 1;
3449 }
3450 }
3451
3452 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003453 if (local->scan_channel_idx >= sband->n_channels) {
3454 local->scan_band++;
3455 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003456 }
3457
3458 if (skip)
3459 break;
3460
3461 next_delay = IEEE80211_PROBE_DELAY +
3462 usecs_to_jiffies(local->hw.channel_change_time);
3463 local->scan_state = SCAN_SEND_PROBE;
3464 break;
3465 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003466 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003467 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003468
3469 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3470 break;
3471 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3472 local->scan_ssid_len);
3473 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003474 break;
3475 }
3476
Zhu Yiece8edd2007-11-22 10:53:21 +08003477 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003478 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3479 next_delay);
3480}
3481
3482
3483static int ieee80211_sta_start_scan(struct net_device *dev,
3484 u8 *ssid, size_t ssid_len)
3485{
3486 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3487 struct ieee80211_sub_if_data *sdata;
3488
3489 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3490 return -EINVAL;
3491
3492 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3493 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3494 * BSSID: MACAddress
3495 * SSID
3496 * ScanType: ACTIVE, PASSIVE
3497 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3498 * a Probe frame during active scanning
3499 * ChannelList
3500 * MinChannelTime (>= ProbeDelay), in TU
3501 * MaxChannelTime: (>= MinChannelTime), in TU
3502 */
3503
3504 /* MLME-SCAN.confirm
3505 * BSSDescriptionSet
3506 * ResultCode: SUCCESS, INVALID_PARAMETERS
3507 */
3508
Zhu Yiece8edd2007-11-22 10:53:21 +08003509 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003510 if (local->scan_dev == dev)
3511 return 0;
3512 return -EBUSY;
3513 }
3514
3515 if (local->ops->hw_scan) {
3516 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003517 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003518 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003519 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003520 local->scan_dev = dev;
3521 }
3522 return rc;
3523 }
3524
Zhu Yiece8edd2007-11-22 10:53:21 +08003525 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003526
Johannes Berg79010422007-09-18 17:29:21 -04003527 rcu_read_lock();
3528 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003529
3530 /* Don't stop the master interface, otherwise we can't transmit
3531 * probes! */
3532 if (sdata->dev == local->mdev)
3533 continue;
3534
Jiri Bencf0706e82007-05-05 11:45:53 -07003535 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003536 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003537 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003538 ieee80211_send_nullfunc(local, sdata, 1);
3539 }
Johannes Berg79010422007-09-18 17:29:21 -04003540 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003541
3542 if (ssid) {
3543 local->scan_ssid_len = ssid_len;
3544 memcpy(local->scan_ssid, ssid, ssid_len);
3545 } else
3546 local->scan_ssid_len = 0;
3547 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003548 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003549 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003550 local->scan_dev = dev;
3551
Johannes Berg4150c572007-09-17 01:29:23 -04003552 netif_tx_lock_bh(local->mdev);
3553 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3554 local->ops->configure_filter(local_to_hw(local),
3555 FIF_BCN_PRBRESP_PROMISC,
3556 &local->filter_flags,
3557 local->mdev->mc_count,
3558 local->mdev->mc_list);
3559 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003560
3561 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3562 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3563 IEEE80211_CHANNEL_TIME);
3564
3565 return 0;
3566}
3567
3568
3569int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3570{
3571 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3572 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3573 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3574
Johannes Berg51fb61e2007-12-19 01:31:27 +01003575 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003576 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3577
Zhu Yiece8edd2007-11-22 10:53:21 +08003578 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003579 if (local->scan_dev == dev)
3580 return 0;
3581 return -EBUSY;
3582 }
3583
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003584 ifsta->scan_ssid_len = ssid_len;
3585 if (ssid_len)
3586 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003587 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3588 queue_work(local->hw.workqueue, &ifsta->work);
3589 return 0;
3590}
3591
3592static char *
3593ieee80211_sta_scan_result(struct net_device *dev,
3594 struct ieee80211_sta_bss *bss,
3595 char *current_ev, char *end_buf)
3596{
3597 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3598 struct iw_event iwe;
3599
3600 if (time_after(jiffies,
3601 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3602 return current_ev;
3603
Jiri Bencf0706e82007-05-05 11:45:53 -07003604 memset(&iwe, 0, sizeof(iwe));
3605 iwe.cmd = SIOCGIWAP;
3606 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3607 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3608 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3609 IW_EV_ADDR_LEN);
3610
3611 memset(&iwe, 0, sizeof(iwe));
3612 iwe.cmd = SIOCGIWESSID;
3613 iwe.u.data.length = bss->ssid_len;
3614 iwe.u.data.flags = 1;
3615 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3616 bss->ssid);
3617
3618 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
3619 memset(&iwe, 0, sizeof(iwe));
3620 iwe.cmd = SIOCGIWMODE;
3621 if (bss->capability & WLAN_CAPABILITY_ESS)
3622 iwe.u.mode = IW_MODE_MASTER;
3623 else
3624 iwe.u.mode = IW_MODE_ADHOC;
3625 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3626 IW_EV_UINT_LEN);
3627 }
3628
3629 memset(&iwe, 0, sizeof(iwe));
3630 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003631 iwe.u.freq.m = bss->freq;
3632 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07003633 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3634 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003635
3636 memset(&iwe, 0, sizeof(iwe));
3637 iwe.cmd = SIOCGIWFREQ;
3638 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3639 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003640 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3641 IW_EV_FREQ_LEN);
3642
3643 memset(&iwe, 0, sizeof(iwe));
3644 iwe.cmd = IWEVQUAL;
3645 iwe.u.qual.qual = bss->signal;
3646 iwe.u.qual.level = bss->rssi;
3647 iwe.u.qual.noise = bss->noise;
3648 iwe.u.qual.updated = local->wstats_flags;
3649 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3650 IW_EV_QUAL_LEN);
3651
3652 memset(&iwe, 0, sizeof(iwe));
3653 iwe.cmd = SIOCGIWENCODE;
3654 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3655 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3656 else
3657 iwe.u.data.flags = IW_ENCODE_DISABLED;
3658 iwe.u.data.length = 0;
3659 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3660
3661 if (bss && bss->wpa_ie) {
3662 memset(&iwe, 0, sizeof(iwe));
3663 iwe.cmd = IWEVGENIE;
3664 iwe.u.data.length = bss->wpa_ie_len;
3665 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3666 bss->wpa_ie);
3667 }
3668
3669 if (bss && bss->rsn_ie) {
3670 memset(&iwe, 0, sizeof(iwe));
3671 iwe.cmd = IWEVGENIE;
3672 iwe.u.data.length = bss->rsn_ie_len;
3673 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3674 bss->rsn_ie);
3675 }
3676
3677 if (bss && bss->supp_rates_len > 0) {
3678 /* display all supported rates in readable format */
3679 char *p = current_ev + IW_EV_LCP_LEN;
3680 int i;
3681
3682 memset(&iwe, 0, sizeof(iwe));
3683 iwe.cmd = SIOCGIWRATE;
3684 /* Those two flags are ignored... */
3685 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3686
3687 for (i = 0; i < bss->supp_rates_len; i++) {
3688 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3689 0x7f) * 500000);
3690 p = iwe_stream_add_value(current_ev, p,
3691 end_buf, &iwe, IW_EV_PARAM_LEN);
3692 }
3693 current_ev = p;
3694 }
3695
3696 if (bss) {
3697 char *buf;
3698 buf = kmalloc(30, GFP_ATOMIC);
3699 if (buf) {
3700 memset(&iwe, 0, sizeof(iwe));
3701 iwe.cmd = IWEVCUSTOM;
3702 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3703 iwe.u.data.length = strlen(buf);
3704 current_ev = iwe_stream_add_point(current_ev, end_buf,
3705 &iwe, buf);
3706 kfree(buf);
3707 }
3708 }
3709
Jiri Bencf0706e82007-05-05 11:45:53 -07003710 return current_ev;
3711}
3712
3713
3714int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3715{
3716 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3717 char *current_ev = buf;
3718 char *end_buf = buf + len;
3719 struct ieee80211_sta_bss *bss;
3720
3721 spin_lock_bh(&local->sta_bss_lock);
3722 list_for_each_entry(bss, &local->sta_bss_list, list) {
3723 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3724 spin_unlock_bh(&local->sta_bss_lock);
3725 return -E2BIG;
3726 }
3727 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3728 end_buf);
3729 }
3730 spin_unlock_bh(&local->sta_bss_lock);
3731 return current_ev - buf;
3732}
3733
3734
3735int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3736{
3737 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3738 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3739 kfree(ifsta->extra_ie);
3740 if (len == 0) {
3741 ifsta->extra_ie = NULL;
3742 ifsta->extra_ie_len = 0;
3743 return 0;
3744 }
3745 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3746 if (!ifsta->extra_ie) {
3747 ifsta->extra_ie_len = 0;
3748 return -ENOMEM;
3749 }
3750 memcpy(ifsta->extra_ie, ie, len);
3751 ifsta->extra_ie_len = len;
3752 return 0;
3753}
3754
3755
3756struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3757 struct sk_buff *skb, u8 *bssid,
3758 u8 *addr)
3759{
3760 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3761 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04003762 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07003763 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003764
3765 /* TODO: Could consider removing the least recently used entry and
3766 * allow new one to be added. */
3767 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3768 if (net_ratelimit()) {
3769 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07003770 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07003771 }
3772 return NULL;
3773 }
3774
Joe Perches0795af52007-10-03 17:59:30 -07003775 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04003776 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07003777
3778 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3779 if (!sta)
3780 return NULL;
3781
Johannes Berg8318d782008-01-24 19:38:38 +01003782 sta->supp_rates[local->hw.conf.channel->band] =
3783 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003784
3785 rate_control_rate_init(sta, local);
3786
3787 return sta; /* caller will call sta_info_put() */
3788}
3789
3790
3791int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3792{
3793 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3794 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3795
3796 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3797 dev->name, reason);
3798
Johannes Berg51fb61e2007-12-19 01:31:27 +01003799 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3800 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07003801 return -EINVAL;
3802
3803 ieee80211_send_deauth(dev, ifsta, reason);
3804 ieee80211_set_disassoc(dev, ifsta, 1);
3805 return 0;
3806}
3807
3808
3809int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3810{
3811 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3812 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3813
3814 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3815 dev->name, reason);
3816
Johannes Berg51fb61e2007-12-19 01:31:27 +01003817 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003818 return -EINVAL;
3819
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003820 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003821 return -1;
3822
3823 ieee80211_send_disassoc(dev, ifsta, reason);
3824 ieee80211_set_disassoc(dev, ifsta, 0);
3825 return 0;
3826}