blob: 87830c04a1caf4d1904da04efa169162b9060d9c [file] [log] [blame]
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -070027#include <net/iw_handler.h>
28#include <asm/types.h>
Jiri Bencf0706e822007-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 Bencf0706e822007-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
66
Jiri Bencf0706e822007-05-05 11:45:53 -070067static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
68 u8 *ssid, size_t ssid_len);
69static struct ieee80211_sta_bss *
John W. Linvillecffdd302007-10-05 14:23:27 -040070ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel,
71 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -070072static void ieee80211_rx_bss_put(struct net_device *dev,
73 struct ieee80211_sta_bss *bss);
74static int ieee80211_sta_find_ibss(struct net_device *dev,
75 struct ieee80211_if_sta *ifsta);
76static int ieee80211_sta_wep_configured(struct net_device *dev);
77static int ieee80211_sta_start_scan(struct net_device *dev,
78 u8 *ssid, size_t ssid_len);
79static int ieee80211_sta_config_auth(struct net_device *dev,
80 struct ieee80211_if_sta *ifsta);
81
82
83/* Parsed Information Elements */
84struct ieee802_11_elems {
Johannes Berg55582352007-07-10 19:32:09 +020085 /* pointers to IEs */
Jiri Bencf0706e822007-05-05 11:45:53 -070086 u8 *ssid;
Jiri Bencf0706e822007-05-05 11:45:53 -070087 u8 *supp_rates;
Jiri Bencf0706e822007-05-05 11:45:53 -070088 u8 *fh_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070089 u8 *ds_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070090 u8 *cf_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070091 u8 *tim;
Jiri Bencf0706e822007-05-05 11:45:53 -070092 u8 *ibss_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070093 u8 *challenge;
Jiri Bencf0706e822007-05-05 11:45:53 -070094 u8 *wpa;
Jiri Bencf0706e822007-05-05 11:45:53 -070095 u8 *rsn;
Jiri Bencf0706e822007-05-05 11:45:53 -070096 u8 *erp_info;
Jiri Bencf0706e822007-05-05 11:45:53 -070097 u8 *ext_supp_rates;
Jiri Bencf0706e822007-05-05 11:45:53 -070098 u8 *wmm_info;
Jiri Bencf0706e822007-05-05 11:45:53 -070099 u8 *wmm_param;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200100 u8 *ht_cap_elem;
101 u8 *ht_info_elem;
Johannes Berg55582352007-07-10 19:32:09 +0200102 /* length of them, respectively */
103 u8 ssid_len;
104 u8 supp_rates_len;
105 u8 fh_params_len;
106 u8 ds_params_len;
107 u8 cf_params_len;
108 u8 tim_len;
109 u8 ibss_params_len;
110 u8 challenge_len;
111 u8 wpa_len;
112 u8 rsn_len;
113 u8 erp_info_len;
114 u8 ext_supp_rates_len;
115 u8 wmm_info_len;
Jiri Bencf0706e822007-05-05 11:45:53 -0700116 u8 wmm_param_len;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200117 u8 ht_cap_elem_len;
118 u8 ht_info_elem_len;
Jiri Bencf0706e822007-05-05 11:45:53 -0700119};
120
John W. Linville67a4cce2007-10-12 16:40:37 -0400121static void ieee802_11_parse_elems(u8 *start, size_t len,
122 struct ieee802_11_elems *elems)
Jiri Bencf0706e822007-05-05 11:45:53 -0700123{
124 size_t left = len;
125 u8 *pos = start;
Jiri Bencf0706e822007-05-05 11:45:53 -0700126
127 memset(elems, 0, sizeof(*elems));
128
129 while (left >= 2) {
130 u8 id, elen;
131
132 id = *pos++;
133 elen = *pos++;
134 left -= 2;
135
John W. Linville67a4cce2007-10-12 16:40:37 -0400136 if (elen > left)
137 return;
Jiri Bencf0706e822007-05-05 11:45:53 -0700138
139 switch (id) {
140 case WLAN_EID_SSID:
141 elems->ssid = pos;
142 elems->ssid_len = elen;
143 break;
144 case WLAN_EID_SUPP_RATES:
145 elems->supp_rates = pos;
146 elems->supp_rates_len = elen;
147 break;
148 case WLAN_EID_FH_PARAMS:
149 elems->fh_params = pos;
150 elems->fh_params_len = elen;
151 break;
152 case WLAN_EID_DS_PARAMS:
153 elems->ds_params = pos;
154 elems->ds_params_len = elen;
155 break;
156 case WLAN_EID_CF_PARAMS:
157 elems->cf_params = pos;
158 elems->cf_params_len = elen;
159 break;
160 case WLAN_EID_TIM:
161 elems->tim = pos;
162 elems->tim_len = elen;
163 break;
164 case WLAN_EID_IBSS_PARAMS:
165 elems->ibss_params = pos;
166 elems->ibss_params_len = elen;
167 break;
168 case WLAN_EID_CHALLENGE:
169 elems->challenge = pos;
170 elems->challenge_len = elen;
171 break;
172 case WLAN_EID_WPA:
173 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
174 pos[2] == 0xf2) {
175 /* Microsoft OUI (00:50:F2) */
176 if (pos[3] == 1) {
177 /* OUI Type 1 - WPA IE */
178 elems->wpa = pos;
179 elems->wpa_len = elen;
180 } else if (elen >= 5 && pos[3] == 2) {
181 if (pos[4] == 0) {
182 elems->wmm_info = pos;
183 elems->wmm_info_len = elen;
184 } else if (pos[4] == 1) {
185 elems->wmm_param = pos;
186 elems->wmm_param_len = elen;
187 }
188 }
189 }
190 break;
191 case WLAN_EID_RSN:
192 elems->rsn = pos;
193 elems->rsn_len = elen;
194 break;
195 case WLAN_EID_ERP_INFO:
196 elems->erp_info = pos;
197 elems->erp_info_len = elen;
198 break;
199 case WLAN_EID_EXT_SUPP_RATES:
200 elems->ext_supp_rates = pos;
201 elems->ext_supp_rates_len = elen;
202 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200203 case WLAN_EID_HT_CAPABILITY:
204 elems->ht_cap_elem = pos;
205 elems->ht_cap_elem_len = elen;
206 break;
207 case WLAN_EID_HT_EXTRA_INFO:
208 elems->ht_info_elem = pos;
209 elems->ht_info_elem_len = elen;
210 break;
Jiri Bencf0706e822007-05-05 11:45:53 -0700211 default:
Jiri Bencf0706e822007-05-05 11:45:53 -0700212 break;
213 }
214
215 left -= elen;
216 pos += elen;
217 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700218}
219
220
Jiri Bencf0706e822007-05-05 11:45:53 -0700221static int ecw2cw(int ecw)
222{
223 int cw = 1;
224 while (ecw > 0) {
225 cw <<= 1;
226 ecw--;
227 }
228 return cw - 1;
229}
230
Jiri Bencf0706e822007-05-05 11:45:53 -0700231static void ieee80211_sta_wmm_params(struct net_device *dev,
232 struct ieee80211_if_sta *ifsta,
233 u8 *wmm_param, size_t wmm_param_len)
234{
235 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
236 struct ieee80211_tx_queue_params params;
237 size_t left;
238 int count;
239 u8 *pos;
240
241 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
242 return;
243 count = wmm_param[6] & 0x0f;
244 if (count == ifsta->wmm_last_param_set)
245 return;
246 ifsta->wmm_last_param_set = count;
247
248 pos = wmm_param + 8;
249 left = wmm_param_len - 8;
250
251 memset(&params, 0, sizeof(params));
252
253 if (!local->ops->conf_tx)
254 return;
255
256 local->wmm_acm = 0;
257 for (; left >= 4; left -= 4, pos += 4) {
258 int aci = (pos[0] >> 5) & 0x03;
259 int acm = (pos[0] >> 4) & 0x01;
260 int queue;
261
262 switch (aci) {
263 case 1:
264 queue = IEEE80211_TX_QUEUE_DATA3;
265 if (acm) {
266 local->wmm_acm |= BIT(0) | BIT(3);
267 }
268 break;
269 case 2:
270 queue = IEEE80211_TX_QUEUE_DATA1;
271 if (acm) {
272 local->wmm_acm |= BIT(4) | BIT(5);
273 }
274 break;
275 case 3:
276 queue = IEEE80211_TX_QUEUE_DATA0;
277 if (acm) {
278 local->wmm_acm |= BIT(6) | BIT(7);
279 }
280 break;
281 case 0:
282 default:
283 queue = IEEE80211_TX_QUEUE_DATA2;
284 if (acm) {
285 local->wmm_acm |= BIT(1) | BIT(2);
286 }
287 break;
288 }
289
290 params.aifs = pos[0] & 0x0f;
291 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
292 params.cw_min = ecw2cw(pos[1] & 0x0f);
293 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
294 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
295 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
296 "cWmin=%d cWmax=%d burst=%d\n",
297 dev->name, queue, aci, acm, params.aifs, params.cw_min,
298 params.cw_max, params.burst_time);
299 /* TODO: handle ACM (block TX, fallback to next lowest allowed
300 * AC for now) */
301 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
302 printk(KERN_DEBUG "%s: failed to set TX queue "
303 "parameters for queue %d\n", dev->name, queue);
304 }
305 }
306}
307
308
Daniel Drake56282212007-07-10 19:32:10 +0200309static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
310{
Daniel Drake56282212007-07-10 19:32:10 +0200311 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
312 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
313 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200314 int preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Daniel Draked9430a32007-07-27 15:43:24 +0200315 u8 changes = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700316 DECLARE_MAC_BUF(mac);
Daniel Drake56282212007-07-10 19:32:10 +0200317
Jiri Slaby13262ff2007-08-28 17:01:54 -0400318 if (use_protection != !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION)) {
Daniel Drake56282212007-07-10 19:32:10 +0200319 if (net_ratelimit()) {
320 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700321 "%s)\n",
Daniel Drake56282212007-07-10 19:32:10 +0200322 dev->name,
323 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700324 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200325 }
Jiri Slaby13262ff2007-08-28 17:01:54 -0400326 if (use_protection)
327 sdata->flags |= IEEE80211_SDATA_USE_PROTECTION;
328 else
329 sdata->flags &= ~IEEE80211_SDATA_USE_PROTECTION;
Daniel Draked9430a32007-07-27 15:43:24 +0200330 changes |= IEEE80211_ERP_CHANGE_PROTECTION;
Daniel Drake56282212007-07-10 19:32:10 +0200331 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200332
Jiri Slaby13262ff2007-08-28 17:01:54 -0400333 if (preamble_mode != !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE)) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200334 if (net_ratelimit()) {
335 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700336 " (BSSID=%s)\n",
Daniel Drake7e9ed182007-07-27 15:43:24 +0200337 dev->name,
338 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
339 "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700340 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200341 }
Jiri Slaby13262ff2007-08-28 17:01:54 -0400342 if (preamble_mode)
343 sdata->flags &= ~IEEE80211_SDATA_SHORT_PREAMBLE;
344 else
345 sdata->flags |= IEEE80211_SDATA_SHORT_PREAMBLE;
Daniel Draked9430a32007-07-27 15:43:24 +0200346 changes |= IEEE80211_ERP_CHANGE_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200347 }
Daniel Draked9430a32007-07-27 15:43:24 +0200348
349 if (changes)
350 ieee80211_erp_info_change_notify(dev, changes);
Daniel Drake56282212007-07-10 19:32:10 +0200351}
352
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200353int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
354 struct ieee80211_ht_info *ht_info)
355{
356
357 if (ht_info == NULL)
358 return -EINVAL;
359
360 memset(ht_info, 0, sizeof(*ht_info));
361
362 if (ht_cap_ie) {
363 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
364
365 ht_info->ht_supported = 1;
366 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
367 ht_info->ampdu_factor =
368 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
369 ht_info->ampdu_density =
370 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
371 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
372 } else
373 ht_info->ht_supported = 0;
374
375 return 0;
376}
377
378int ieee80211_ht_addt_info_ie_to_ht_bss_info(
379 struct ieee80211_ht_addt_info *ht_add_info_ie,
380 struct ieee80211_ht_bss_info *bss_info)
381{
382 if (bss_info == NULL)
383 return -EINVAL;
384
385 memset(bss_info, 0, sizeof(*bss_info));
386
387 if (ht_add_info_ie) {
388 u16 op_mode;
389 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
390
391 bss_info->primary_channel = ht_add_info_ie->control_chan;
392 bss_info->bss_cap = ht_add_info_ie->ht_param;
393 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
394 }
395
396 return 0;
397}
Daniel Drake56282212007-07-10 19:32:10 +0200398
Jiri Bencf0706e822007-05-05 11:45:53 -0700399static void ieee80211_sta_send_associnfo(struct net_device *dev,
400 struct ieee80211_if_sta *ifsta)
401{
402 char *buf;
403 size_t len;
404 int i;
405 union iwreq_data wrqu;
406
407 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
408 return;
409
410 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200411 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700412 if (!buf)
413 return;
414
415 len = sprintf(buf, "ASSOCINFO(");
416 if (ifsta->assocreq_ies) {
417 len += sprintf(buf + len, "ReqIEs=");
418 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
419 len += sprintf(buf + len, "%02x",
420 ifsta->assocreq_ies[i]);
421 }
422 }
423 if (ifsta->assocresp_ies) {
424 if (ifsta->assocreq_ies)
425 len += sprintf(buf + len, " ");
426 len += sprintf(buf + len, "RespIEs=");
427 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
428 len += sprintf(buf + len, "%02x",
429 ifsta->assocresp_ies[i]);
430 }
431 }
432 len += sprintf(buf + len, ")");
433
434 if (len > IW_CUSTOM_MAX) {
435 len = sprintf(buf, "ASSOCRESPIE=");
436 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
437 len += sprintf(buf + len, "%02x",
438 ifsta->assocresp_ies[i]);
439 }
440 }
441
442 memset(&wrqu, 0, sizeof(wrqu));
443 wrqu.data.length = len;
444 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
445
446 kfree(buf);
447}
448
449
450static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400451 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200452 bool assoc)
Jiri Bencf0706e822007-05-05 11:45:53 -0700453{
Michael Buesch47f0c502007-09-27 15:10:44 +0200454 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700455 union iwreq_data wrqu;
456
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400457 if (!!(ifsta->flags & IEEE80211_STA_ASSOCIATED) == assoc)
Jiri Bencf0706e822007-05-05 11:45:53 -0700458 return;
459
Jiri Bencf0706e822007-05-05 11:45:53 -0700460 if (assoc) {
461 struct ieee80211_sub_if_data *sdata;
Daniel Drake56282212007-07-10 19:32:10 +0200462 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400463
464 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
465
Jiri Bencf0706e822007-05-05 11:45:53 -0700466 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
467 if (sdata->type != IEEE80211_IF_TYPE_STA)
468 return;
Daniel Drake56282212007-07-10 19:32:10 +0200469
John W. Linville65c107a2007-10-05 14:23:27 -0400470 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
John W. Linvillecffdd302007-10-05 14:23:27 -0400471 local->hw.conf.channel,
472 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200473 if (bss) {
474 if (bss->has_erp_value)
475 ieee80211_handle_erp_ie(dev, bss->erp_value);
476 ieee80211_rx_bss_put(dev, bss);
477 }
478
Jiri Bencf0706e822007-05-05 11:45:53 -0700479 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400480 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -0700481 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
482 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
483 ieee80211_sta_send_associnfo(dev, ifsta);
484 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400485 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
486
Jiri Bencf0706e822007-05-05 11:45:53 -0700487 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200488 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -0700489 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
490 }
491 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
492 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
493 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200494 ieee80211_led_assoc(local, assoc);
Jiri Bencf0706e822007-05-05 11:45:53 -0700495}
496
497static void ieee80211_set_disassoc(struct net_device *dev,
498 struct ieee80211_if_sta *ifsta, int deauth)
499{
500 if (deauth)
501 ifsta->auth_tries = 0;
502 ifsta->assoc_tries = 0;
503 ieee80211_set_associated(dev, ifsta, 0);
504}
505
506static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
507 int encrypt)
508{
509 struct ieee80211_sub_if_data *sdata;
510 struct ieee80211_tx_packet_data *pkt_data;
511
512 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
513 skb->dev = sdata->local->mdev;
514 skb_set_mac_header(skb, 0);
515 skb_set_network_header(skb, 0);
516 skb_set_transport_header(skb, 0);
517
518 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
519 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
520 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400521 if (!encrypt)
522 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e822007-05-05 11:45:53 -0700523
524 dev_queue_xmit(skb);
525}
526
527
528static void ieee80211_send_auth(struct net_device *dev,
529 struct ieee80211_if_sta *ifsta,
530 int transaction, u8 *extra, size_t extra_len,
531 int encrypt)
532{
533 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
534 struct sk_buff *skb;
535 struct ieee80211_mgmt *mgmt;
536
537 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
538 sizeof(*mgmt) + 6 + extra_len);
539 if (!skb) {
540 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
541 "frame\n", dev->name);
542 return;
543 }
544 skb_reserve(skb, local->hw.extra_tx_headroom);
545
546 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
547 memset(mgmt, 0, 24 + 6);
548 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
549 IEEE80211_STYPE_AUTH);
550 if (encrypt)
551 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
552 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
553 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
554 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
555 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
556 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
557 ifsta->auth_transaction = transaction + 1;
558 mgmt->u.auth.status_code = cpu_to_le16(0);
559 if (extra)
560 memcpy(skb_put(skb, extra_len), extra, extra_len);
561
562 ieee80211_sta_tx(dev, skb, encrypt);
563}
564
565
566static void ieee80211_authenticate(struct net_device *dev,
567 struct ieee80211_if_sta *ifsta)
568{
Joe Perches0795af52007-10-03 17:59:30 -0700569 DECLARE_MAC_BUF(mac);
570
Jiri Bencf0706e822007-05-05 11:45:53 -0700571 ifsta->auth_tries++;
572 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700573 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -0700574 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700575 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700576 ifsta->state = IEEE80211_DISABLED;
577 return;
578 }
579
580 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700581 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
582 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700583
584 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
585
586 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
587}
588
589
590static void ieee80211_send_assoc(struct net_device *dev,
591 struct ieee80211_if_sta *ifsta)
592{
593 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
594 struct ieee80211_hw_mode *mode;
595 struct sk_buff *skb;
596 struct ieee80211_mgmt *mgmt;
597 u8 *pos, *ies;
598 int i, len;
599 u16 capab;
600 struct ieee80211_sta_bss *bss;
601 int wmm = 0;
602
603 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
604 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
605 ifsta->ssid_len);
606 if (!skb) {
607 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
608 "frame\n", dev->name);
609 return;
610 }
611 skb_reserve(skb, local->hw.extra_tx_headroom);
612
613 mode = local->oper_hw_mode;
614 capab = ifsta->capab;
615 if (mode->mode == MODE_IEEE80211G) {
616 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
617 WLAN_CAPABILITY_SHORT_PREAMBLE;
618 }
John W. Linvillecffdd302007-10-05 14:23:27 -0400619 bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel,
620 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -0700621 if (bss) {
622 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
623 capab |= WLAN_CAPABILITY_PRIVACY;
624 if (bss->wmm_ie) {
625 wmm = 1;
626 }
627 ieee80211_rx_bss_put(dev, bss);
628 }
629
630 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
631 memset(mgmt, 0, 24);
632 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
633 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
634 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
635
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400636 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700637 skb_put(skb, 10);
638 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
639 IEEE80211_STYPE_REASSOC_REQ);
640 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
641 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
642 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
643 ETH_ALEN);
644 } else {
645 skb_put(skb, 4);
646 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
647 IEEE80211_STYPE_ASSOC_REQ);
648 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
649 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
650 }
651
652 /* SSID */
653 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
654 *pos++ = WLAN_EID_SSID;
655 *pos++ = ifsta->ssid_len;
656 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
657
658 len = mode->num_rates;
659 if (len > 8)
660 len = 8;
661 pos = skb_put(skb, len + 2);
662 *pos++ = WLAN_EID_SUPP_RATES;
663 *pos++ = len;
664 for (i = 0; i < len; i++) {
665 int rate = mode->rates[i].rate;
Jiri Bencf0706e822007-05-05 11:45:53 -0700666 *pos++ = (u8) (rate / 5);
667 }
668
669 if (mode->num_rates > len) {
670 pos = skb_put(skb, mode->num_rates - len + 2);
671 *pos++ = WLAN_EID_EXT_SUPP_RATES;
672 *pos++ = mode->num_rates - len;
673 for (i = len; i < mode->num_rates; i++) {
674 int rate = mode->rates[i].rate;
Jiri Bencf0706e822007-05-05 11:45:53 -0700675 *pos++ = (u8) (rate / 5);
676 }
677 }
678
679 if (ifsta->extra_ie) {
680 pos = skb_put(skb, ifsta->extra_ie_len);
681 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
682 }
683
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400684 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700685 pos = skb_put(skb, 9);
686 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
687 *pos++ = 7; /* len */
688 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
689 *pos++ = 0x50;
690 *pos++ = 0xf2;
691 *pos++ = 2; /* WME */
692 *pos++ = 0; /* WME info */
693 *pos++ = 1; /* WME ver */
694 *pos++ = 0;
695 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200696 /* wmm support is a must to HT */
697 if (wmm && mode->ht_info.ht_supported) {
698 __le16 tmp = cpu_to_le16(mode->ht_info.cap);
699 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
700 *pos++ = WLAN_EID_HT_CAPABILITY;
701 *pos++ = sizeof(struct ieee80211_ht_cap);
702 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
703 memcpy(pos, &tmp, sizeof(u16));
704 pos += sizeof(u16);
705 *pos++ = (mode->ht_info.ampdu_factor |
706 (mode->ht_info.ampdu_density << 2));
707 memcpy(pos, mode->ht_info.supp_mcs_set, 16);
708 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700709
710 kfree(ifsta->assocreq_ies);
711 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200712 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700713 if (ifsta->assocreq_ies)
714 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
715
716 ieee80211_sta_tx(dev, skb, 0);
717}
718
719
720static void ieee80211_send_deauth(struct net_device *dev,
721 struct ieee80211_if_sta *ifsta, u16 reason)
722{
723 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
724 struct sk_buff *skb;
725 struct ieee80211_mgmt *mgmt;
726
727 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
728 if (!skb) {
729 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
730 "frame\n", dev->name);
731 return;
732 }
733 skb_reserve(skb, local->hw.extra_tx_headroom);
734
735 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
736 memset(mgmt, 0, 24);
737 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
738 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
739 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
740 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
741 IEEE80211_STYPE_DEAUTH);
742 skb_put(skb, 2);
743 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
744
745 ieee80211_sta_tx(dev, skb, 0);
746}
747
748
749static void ieee80211_send_disassoc(struct net_device *dev,
750 struct ieee80211_if_sta *ifsta, u16 reason)
751{
752 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
753 struct sk_buff *skb;
754 struct ieee80211_mgmt *mgmt;
755
756 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
757 if (!skb) {
758 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
759 "frame\n", dev->name);
760 return;
761 }
762 skb_reserve(skb, local->hw.extra_tx_headroom);
763
764 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
765 memset(mgmt, 0, 24);
766 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
767 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
768 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
769 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
770 IEEE80211_STYPE_DISASSOC);
771 skb_put(skb, 2);
772 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
773
774 ieee80211_sta_tx(dev, skb, 0);
775}
776
777
778static int ieee80211_privacy_mismatch(struct net_device *dev,
779 struct ieee80211_if_sta *ifsta)
780{
John W. Linville65c107a2007-10-05 14:23:27 -0400781 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700782 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000783 int bss_privacy;
784 int wep_privacy;
785 int privacy_invoked;
Jiri Bencf0706e822007-05-05 11:45:53 -0700786
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000787 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e822007-05-05 11:45:53 -0700788 return 0;
789
John W. Linvillecffdd302007-10-05 14:23:27 -0400790 bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel,
791 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -0700792 if (!bss)
793 return 0;
794
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000795 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
796 wep_privacy = !!ieee80211_sta_wep_configured(dev);
797 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e822007-05-05 11:45:53 -0700798
799 ieee80211_rx_bss_put(dev, bss);
800
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000801 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
802 return 0;
803
804 return 1;
Jiri Bencf0706e822007-05-05 11:45:53 -0700805}
806
807
808static void ieee80211_associate(struct net_device *dev,
809 struct ieee80211_if_sta *ifsta)
810{
Joe Perches0795af52007-10-03 17:59:30 -0700811 DECLARE_MAC_BUF(mac);
812
Jiri Bencf0706e822007-05-05 11:45:53 -0700813 ifsta->assoc_tries++;
814 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700815 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -0700816 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700817 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700818 ifsta->state = IEEE80211_DISABLED;
819 return;
820 }
821
822 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700823 printk(KERN_DEBUG "%s: associate with AP %s\n",
824 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700825 if (ieee80211_privacy_mismatch(dev, ifsta)) {
826 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
827 "mixed-cell disabled - abort association\n", dev->name);
828 ifsta->state = IEEE80211_DISABLED;
829 return;
830 }
831
832 ieee80211_send_assoc(dev, ifsta);
833
834 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
835}
836
837
838static void ieee80211_associated(struct net_device *dev,
839 struct ieee80211_if_sta *ifsta)
840{
841 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
842 struct sta_info *sta;
843 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700844 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -0700845
846 /* TODO: start monitoring current AP signal quality and number of
847 * missed beacons. Scan other channels every now and then and search
848 * for better APs. */
849 /* TODO: remove expired BSSes */
850
851 ifsta->state = IEEE80211_ASSOCIATED;
852
853 sta = sta_info_get(local, ifsta->bssid);
854 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700855 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
856 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700857 disassoc = 1;
858 } else {
859 disassoc = 0;
860 if (time_after(jiffies,
861 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400862 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700863 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700864 "current AP %s - assume out of "
Jiri Bencf0706e822007-05-05 11:45:53 -0700865 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700866 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700867 disassoc = 1;
Michael Wube8755e2007-07-27 15:43:23 +0200868 sta_info_free(sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400869 } else
Jiri Bencf0706e822007-05-05 11:45:53 -0700870 ieee80211_send_probe_req(dev, ifsta->bssid,
871 local->scan_ssid,
872 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400873 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700874 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400875 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700876 if (time_after(jiffies, ifsta->last_probe +
877 IEEE80211_PROBE_INTERVAL)) {
878 ifsta->last_probe = jiffies;
879 ieee80211_send_probe_req(dev, ifsta->bssid,
880 ifsta->ssid,
881 ifsta->ssid_len);
882 }
883 }
884 sta_info_put(sta);
885 }
886 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500887 ifsta->state = IEEE80211_DISABLED;
888 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -0700889 } else {
890 mod_timer(&ifsta->timer, jiffies +
891 IEEE80211_MONITORING_INTERVAL);
892 }
893}
894
895
896static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
897 u8 *ssid, size_t ssid_len)
898{
899 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
900 struct ieee80211_hw_mode *mode;
901 struct sk_buff *skb;
902 struct ieee80211_mgmt *mgmt;
903 u8 *pos, *supp_rates, *esupp_rates = NULL;
904 int i;
905
906 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
907 if (!skb) {
908 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
909 "request\n", dev->name);
910 return;
911 }
912 skb_reserve(skb, local->hw.extra_tx_headroom);
913
914 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
915 memset(mgmt, 0, 24);
916 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
917 IEEE80211_STYPE_PROBE_REQ);
918 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
919 if (dst) {
920 memcpy(mgmt->da, dst, ETH_ALEN);
921 memcpy(mgmt->bssid, dst, ETH_ALEN);
922 } else {
923 memset(mgmt->da, 0xff, ETH_ALEN);
924 memset(mgmt->bssid, 0xff, ETH_ALEN);
925 }
926 pos = skb_put(skb, 2 + ssid_len);
927 *pos++ = WLAN_EID_SSID;
928 *pos++ = ssid_len;
929 memcpy(pos, ssid, ssid_len);
930
931 supp_rates = skb_put(skb, 2);
932 supp_rates[0] = WLAN_EID_SUPP_RATES;
933 supp_rates[1] = 0;
934 mode = local->oper_hw_mode;
935 for (i = 0; i < mode->num_rates; i++) {
936 struct ieee80211_rate *rate = &mode->rates[i];
937 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
938 continue;
939 if (esupp_rates) {
940 pos = skb_put(skb, 1);
941 esupp_rates[1]++;
942 } else if (supp_rates[1] == 8) {
943 esupp_rates = skb_put(skb, 3);
944 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
945 esupp_rates[1] = 1;
946 pos = &esupp_rates[2];
947 } else {
948 pos = skb_put(skb, 1);
949 supp_rates[1]++;
950 }
Johannes Bergb708e612007-09-14 11:10:25 -0400951 *pos = rate->rate / 5;
Jiri Bencf0706e822007-05-05 11:45:53 -0700952 }
953
954 ieee80211_sta_tx(dev, skb, 0);
955}
956
957
958static int ieee80211_sta_wep_configured(struct net_device *dev)
959{
960 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
961 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400962 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e822007-05-05 11:45:53 -0700963 return 0;
964 return 1;
965}
966
967
968static void ieee80211_auth_completed(struct net_device *dev,
969 struct ieee80211_if_sta *ifsta)
970{
971 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400972 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -0700973 ieee80211_associate(dev, ifsta);
974}
975
976
977static void ieee80211_auth_challenge(struct net_device *dev,
978 struct ieee80211_if_sta *ifsta,
979 struct ieee80211_mgmt *mgmt,
980 size_t len)
981{
982 u8 *pos;
983 struct ieee802_11_elems elems;
984
985 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
986 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -0400987 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -0700988 if (!elems.challenge) {
989 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
990 "frame\n", dev->name);
991 return;
992 }
993 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
994 elems.challenge_len + 2, 1);
995}
996
Ron Rindjunsky9f985b02007-11-26 16:14:32 +0200997static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
998 u8 dialog_token, u16 status, u16 policy,
999 u16 buf_size, u16 timeout)
1000{
1001 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1002 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1003 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1004 struct sk_buff *skb;
1005 struct ieee80211_mgmt *mgmt;
1006 u16 capab;
1007
1008 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1009 if (!skb) {
1010 printk(KERN_DEBUG "%s: failed to allocate buffer "
1011 "for addba resp frame\n", dev->name);
1012 return;
1013 }
1014
1015 skb_reserve(skb, local->hw.extra_tx_headroom);
1016 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1017 memset(mgmt, 0, 24);
1018 memcpy(mgmt->da, da, ETH_ALEN);
1019 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1020 if (sdata->type == IEEE80211_IF_TYPE_AP)
1021 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1022 else
1023 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1024 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1025 IEEE80211_STYPE_ACTION);
1026
1027 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1028 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1029 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1030 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1031
1032 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1033 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1034 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1035
1036 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1037 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1038 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1039
1040 ieee80211_sta_tx(dev, skb, 0);
1041
1042 return;
1043}
1044
1045static void ieee80211_sta_process_addba_request(struct net_device *dev,
1046 struct ieee80211_mgmt *mgmt,
1047 size_t len)
1048{
1049 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1050 struct sta_info *sta;
1051 u16 capab, tid, timeout, ba_policy, buf_size, status;
1052 u8 dialog_token;
1053
1054 sta = sta_info_get(local, mgmt->sa);
1055 if (!sta)
1056 return;
1057
1058 /* extract session parameters from addba request frame */
1059 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1060 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1061
1062 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1063 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1064 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1065 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1066
1067 /* TODO - currently aggregation is declined (A-MPDU add BA request
1068 * acceptance is not obligatory by 802.11n draft), but here is
1069 * the entry point for dealing with it */
1070#ifdef MAC80211_HT_DEBUG
1071 if (net_ratelimit())
1072 printk(KERN_DEBUG "Add Block Ack request arrived,"
1073 " currently denying it\n");
1074#endif /* MAC80211_HT_DEBUG */
1075
1076 status = WLAN_STATUS_REQUEST_DECLINED;
1077
1078 ieee80211_send_addba_resp(sta->dev, sta->addr, tid, dialog_token,
1079 status, 1, buf_size, timeout);
1080 sta_info_put(sta);
1081}
Jiri Bencf0706e822007-05-05 11:45:53 -07001082
1083static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1084 struct ieee80211_if_sta *ifsta,
1085 struct ieee80211_mgmt *mgmt,
1086 size_t len)
1087{
1088 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1089 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001090 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001091
1092 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1093 sdata->type != IEEE80211_IF_TYPE_IBSS) {
1094 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001095 "%s, but not in authenticate state - ignored\n",
1096 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001097 return;
1098 }
1099
1100 if (len < 24 + 6) {
1101 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001102 "received from %s - ignored\n",
1103 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001104 return;
1105 }
1106
1107 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
1108 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1109 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001110 "unknown AP (SA=%s BSSID=%s) - "
1111 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1112 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001113 return;
1114 }
1115
1116 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
1117 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1118 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001119 "unknown BSSID (SA=%s BSSID=%s) - "
1120 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1121 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001122 return;
1123 }
1124
1125 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1126 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1127 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1128
Joe Perches0795af52007-10-03 17:59:30 -07001129 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e822007-05-05 11:45:53 -07001130 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001131 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e822007-05-05 11:45:53 -07001132 auth_transaction, status_code);
1133
1134 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
1135 /* IEEE 802.11 standard does not require authentication in IBSS
1136 * networks and most implementations do not seem to use it.
1137 * However, try to reply to authentication attempts if someone
1138 * has actually implemented this.
1139 * TODO: Could implement shared key authentication. */
1140 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1141 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1142 "frame (alg=%d transaction=%d)\n",
1143 dev->name, auth_alg, auth_transaction);
1144 return;
1145 }
1146 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1147 }
1148
1149 if (auth_alg != ifsta->auth_alg ||
1150 auth_transaction != ifsta->auth_transaction) {
1151 printk(KERN_DEBUG "%s: unexpected authentication frame "
1152 "(alg=%d transaction=%d)\n",
1153 dev->name, auth_alg, auth_transaction);
1154 return;
1155 }
1156
1157 if (status_code != WLAN_STATUS_SUCCESS) {
1158 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1159 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1160 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1161 u8 algs[3];
1162 const int num_algs = ARRAY_SIZE(algs);
1163 int i, pos;
1164 algs[0] = algs[1] = algs[2] = 0xff;
1165 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1166 algs[0] = WLAN_AUTH_OPEN;
1167 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1168 algs[1] = WLAN_AUTH_SHARED_KEY;
1169 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1170 algs[2] = WLAN_AUTH_LEAP;
1171 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1172 pos = 0;
1173 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1174 pos = 1;
1175 else
1176 pos = 2;
1177 for (i = 0; i < num_algs; i++) {
1178 pos++;
1179 if (pos >= num_algs)
1180 pos = 0;
1181 if (algs[pos] == ifsta->auth_alg ||
1182 algs[pos] == 0xff)
1183 continue;
1184 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1185 !ieee80211_sta_wep_configured(dev))
1186 continue;
1187 ifsta->auth_alg = algs[pos];
1188 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1189 "next try\n",
1190 dev->name, ifsta->auth_alg);
1191 break;
1192 }
1193 }
1194 return;
1195 }
1196
1197 switch (ifsta->auth_alg) {
1198 case WLAN_AUTH_OPEN:
1199 case WLAN_AUTH_LEAP:
1200 ieee80211_auth_completed(dev, ifsta);
1201 break;
1202 case WLAN_AUTH_SHARED_KEY:
1203 if (ifsta->auth_transaction == 4)
1204 ieee80211_auth_completed(dev, ifsta);
1205 else
1206 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1207 break;
1208 }
1209}
1210
1211
1212static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1213 struct ieee80211_if_sta *ifsta,
1214 struct ieee80211_mgmt *mgmt,
1215 size_t len)
1216{
1217 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001218 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001219
1220 if (len < 24 + 2) {
1221 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001222 "received from %s - ignored\n",
1223 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001224 return;
1225 }
1226
1227 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1228 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001229 "unknown AP (SA=%s BSSID=%s) - "
1230 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1231 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001232 return;
1233 }
1234
1235 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1236
Joe Perches0795af52007-10-03 17:59:30 -07001237 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07001238 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001239 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e822007-05-05 11:45:53 -07001240
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001241 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001242 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1243 }
1244
1245 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1246 ifsta->state == IEEE80211_ASSOCIATE ||
1247 ifsta->state == IEEE80211_ASSOCIATED) {
1248 ifsta->state = IEEE80211_AUTHENTICATE;
1249 mod_timer(&ifsta->timer, jiffies +
1250 IEEE80211_RETRY_AUTH_INTERVAL);
1251 }
1252
1253 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001254 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -07001255}
1256
1257
1258static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1259 struct ieee80211_if_sta *ifsta,
1260 struct ieee80211_mgmt *mgmt,
1261 size_t len)
1262{
1263 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001264 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001265
1266 if (len < 24 + 2) {
1267 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001268 "received from %s - ignored\n",
1269 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001270 return;
1271 }
1272
1273 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1274 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001275 "unknown AP (SA=%s BSSID=%s) - "
1276 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1277 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001278 return;
1279 }
1280
1281 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1282
Joe Perches0795af52007-10-03 17:59:30 -07001283 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07001284 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001285 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e822007-05-05 11:45:53 -07001286
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001287 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07001288 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1289
1290 if (ifsta->state == IEEE80211_ASSOCIATED) {
1291 ifsta->state = IEEE80211_ASSOCIATE;
1292 mod_timer(&ifsta->timer, jiffies +
1293 IEEE80211_RETRY_AUTH_INTERVAL);
1294 }
1295
1296 ieee80211_set_disassoc(dev, ifsta, 0);
1297}
1298
1299
1300static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1301 struct ieee80211_if_sta *ifsta,
1302 struct ieee80211_mgmt *mgmt,
1303 size_t len,
1304 int reassoc)
1305{
1306 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1307 struct ieee80211_hw_mode *mode;
1308 struct sta_info *sta;
1309 u32 rates;
1310 u16 capab_info, status_code, aid;
1311 struct ieee802_11_elems elems;
1312 u8 *pos;
1313 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001314 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001315
1316 /* AssocResp and ReassocResp have identical structure, so process both
1317 * of them in this function. */
1318
1319 if (ifsta->state != IEEE80211_ASSOCIATE) {
1320 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001321 "%s, but not in associate state - ignored\n",
1322 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001323 return;
1324 }
1325
1326 if (len < 24 + 6) {
1327 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001328 "received from %s - ignored\n",
1329 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001330 return;
1331 }
1332
1333 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1334 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001335 "unknown AP (SA=%s BSSID=%s) - "
1336 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1337 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001338 return;
1339 }
1340
1341 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1342 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1343 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e822007-05-05 11:45:53 -07001344
Joe Perches0795af52007-10-03 17:59:30 -07001345 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e822007-05-05 11:45:53 -07001346 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001347 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001348 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e822007-05-05 11:45:53 -07001349
1350 if (status_code != WLAN_STATUS_SUCCESS) {
1351 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1352 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001353 /* if this was a reassociation, ensure we try a "full"
1354 * association next time. This works around some broken APs
1355 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001356 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07001357 return;
1358 }
1359
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001360 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1361 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1362 "set\n", dev->name, aid);
1363 aid &= ~(BIT(15) | BIT(14));
1364
Jiri Bencf0706e822007-05-05 11:45:53 -07001365 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001366 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07001367
1368 if (!elems.supp_rates) {
1369 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1370 dev->name);
1371 return;
1372 }
1373
Daniel Drake56282212007-07-10 19:32:10 +02001374 /* it probably doesn't, but if the frame includes an ERP value then
1375 * update our stored copy */
1376 if (elems.erp_info && elems.erp_info_len >= 1) {
1377 struct ieee80211_sta_bss *bss
John W. Linville65c107a2007-10-05 14:23:27 -04001378 = ieee80211_rx_bss_get(dev, ifsta->bssid,
John W. Linvillecffdd302007-10-05 14:23:27 -04001379 local->hw.conf.channel,
1380 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +02001381 if (bss) {
1382 bss->erp_value = elems.erp_info[0];
1383 bss->has_erp_value = 1;
1384 ieee80211_rx_bss_put(dev, bss);
1385 }
1386 }
1387
Jiri Bencf0706e822007-05-05 11:45:53 -07001388 printk(KERN_DEBUG "%s: associated\n", dev->name);
1389 ifsta->aid = aid;
1390 ifsta->ap_capab = capab_info;
1391
1392 kfree(ifsta->assocresp_ies);
1393 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001394 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07001395 if (ifsta->assocresp_ies)
1396 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1397
1398 ieee80211_set_associated(dev, ifsta, 1);
1399
1400 /* Add STA entry for the AP */
1401 sta = sta_info_get(local, ifsta->bssid);
1402 if (!sta) {
1403 struct ieee80211_sta_bss *bss;
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001404 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07001405 if (!sta) {
1406 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1407 " AP\n", dev->name);
1408 return;
1409 }
John W. Linville65c107a2007-10-05 14:23:27 -04001410 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
John W. Linvillecffdd302007-10-05 14:23:27 -04001411 local->hw.conf.channel,
1412 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07001413 if (bss) {
1414 sta->last_rssi = bss->rssi;
1415 sta->last_signal = bss->signal;
1416 sta->last_noise = bss->noise;
1417 ieee80211_rx_bss_put(dev, bss);
1418 }
1419 }
1420
1421 sta->dev = dev;
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001422 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
Jiri Bencf0706e822007-05-05 11:45:53 -07001423
1424 rates = 0;
1425 mode = local->oper_hw_mode;
1426 for (i = 0; i < elems.supp_rates_len; i++) {
1427 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Jiri Bencf0706e822007-05-05 11:45:53 -07001428 for (j = 0; j < mode->num_rates; j++)
1429 if (mode->rates[j].rate == rate)
1430 rates |= BIT(j);
1431 }
1432 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1433 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Jiri Bencf0706e822007-05-05 11:45:53 -07001434 for (j = 0; j < mode->num_rates; j++)
1435 if (mode->rates[j].rate == rate)
1436 rates |= BIT(j);
1437 }
1438 sta->supp_rates = rates;
1439
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001440 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1441 local->ops->conf_ht) {
1442 struct ieee80211_ht_bss_info bss_info;
1443
1444 ieee80211_ht_cap_ie_to_ht_info(
1445 (struct ieee80211_ht_cap *)
1446 elems.ht_cap_elem, &sta->ht_info);
1447 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1448 (struct ieee80211_ht_addt_info *)
1449 elems.ht_info_elem, &bss_info);
1450 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1451 }
1452
Jiri Bencf0706e822007-05-05 11:45:53 -07001453 rate_control_rate_init(sta, local);
1454
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001455 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001456 sta->flags |= WLAN_STA_WME;
1457 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1458 elems.wmm_param_len);
1459 }
1460
1461
1462 sta_info_put(sta);
1463
1464 ieee80211_associated(dev, ifsta);
1465}
1466
1467
1468/* Caller must hold local->sta_bss_lock */
1469static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1470 struct ieee80211_sta_bss *bss)
1471{
1472 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1473 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1474 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1475}
1476
1477
1478/* Caller must hold local->sta_bss_lock */
1479static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1480 struct ieee80211_sta_bss *bss)
1481{
1482 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1483 struct ieee80211_sta_bss *b, *prev = NULL;
1484 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1485 while (b) {
1486 if (b == bss) {
1487 if (!prev)
1488 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1489 bss->hnext;
1490 else
1491 prev->hnext = bss->hnext;
1492 break;
1493 }
1494 prev = b;
1495 b = b->hnext;
1496 }
1497}
1498
1499
1500static struct ieee80211_sta_bss *
John W. Linvillecffdd302007-10-05 14:23:27 -04001501ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int channel,
1502 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07001503{
1504 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1505 struct ieee80211_sta_bss *bss;
1506
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001507 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07001508 if (!bss)
1509 return NULL;
Jiri Bencf0706e822007-05-05 11:45:53 -07001510 atomic_inc(&bss->users);
1511 atomic_inc(&bss->users);
1512 memcpy(bss->bssid, bssid, ETH_ALEN);
John W. Linville65c107a2007-10-05 14:23:27 -04001513 bss->channel = channel;
John W. Linvillecffdd302007-10-05 14:23:27 -04001514 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1515 memcpy(bss->ssid, ssid, ssid_len);
1516 bss->ssid_len = ssid_len;
1517 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001518
1519 spin_lock_bh(&local->sta_bss_lock);
1520 /* TODO: order by RSSI? */
1521 list_add_tail(&bss->list, &local->sta_bss_list);
1522 __ieee80211_rx_bss_hash_add(dev, bss);
1523 spin_unlock_bh(&local->sta_bss_lock);
1524 return bss;
1525}
1526
1527
1528static struct ieee80211_sta_bss *
John W. Linvillecffdd302007-10-05 14:23:27 -04001529ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel,
1530 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07001531{
1532 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1533 struct ieee80211_sta_bss *bss;
1534
1535 spin_lock_bh(&local->sta_bss_lock);
1536 bss = local->sta_bss_hash[STA_HASH(bssid)];
1537 while (bss) {
John W. Linvillecffdd302007-10-05 14:23:27 -04001538 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
1539 bss->channel == channel &&
1540 bss->ssid_len == ssid_len &&
1541 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001542 atomic_inc(&bss->users);
1543 break;
1544 }
1545 bss = bss->hnext;
1546 }
1547 spin_unlock_bh(&local->sta_bss_lock);
1548 return bss;
1549}
1550
1551
1552static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1553{
1554 kfree(bss->wpa_ie);
1555 kfree(bss->rsn_ie);
1556 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02001557 kfree(bss->ht_ie);
Jiri Bencf0706e822007-05-05 11:45:53 -07001558 kfree(bss);
1559}
1560
1561
1562static void ieee80211_rx_bss_put(struct net_device *dev,
1563 struct ieee80211_sta_bss *bss)
1564{
1565 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1566 if (!atomic_dec_and_test(&bss->users))
1567 return;
1568
1569 spin_lock_bh(&local->sta_bss_lock);
1570 __ieee80211_rx_bss_hash_del(dev, bss);
1571 list_del(&bss->list);
1572 spin_unlock_bh(&local->sta_bss_lock);
1573 ieee80211_rx_bss_free(bss);
1574}
1575
1576
1577void ieee80211_rx_bss_list_init(struct net_device *dev)
1578{
1579 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1580 spin_lock_init(&local->sta_bss_lock);
1581 INIT_LIST_HEAD(&local->sta_bss_list);
1582}
1583
1584
1585void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1586{
1587 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1588 struct ieee80211_sta_bss *bss, *tmp;
1589
1590 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1591 ieee80211_rx_bss_put(dev, bss);
1592}
1593
1594
1595static void ieee80211_rx_bss_info(struct net_device *dev,
1596 struct ieee80211_mgmt *mgmt,
1597 size_t len,
1598 struct ieee80211_rx_status *rx_status,
1599 int beacon)
1600{
1601 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1602 struct ieee802_11_elems elems;
1603 size_t baselen;
John W. Linville67a4cce2007-10-12 16:40:37 -04001604 int channel, clen;
Jiri Bencf0706e822007-05-05 11:45:53 -07001605 struct ieee80211_sta_bss *bss;
1606 struct sta_info *sta;
1607 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1608 u64 timestamp;
Joe Perches0795af52007-10-03 17:59:30 -07001609 DECLARE_MAC_BUF(mac);
1610 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07001611
1612 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1613 return; /* ignore ProbeResp to foreign address */
1614
1615#if 0
Joe Perches0795af52007-10-03 17:59:30 -07001616 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e822007-05-05 11:45:53 -07001617 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07001618 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07001619#endif
1620
1621 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1622 if (baselen > len)
1623 return;
1624
1625 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1626
1627 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1628 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1629#ifdef CONFIG_MAC80211_IBSS_DEBUG
1630 static unsigned long last_tsf_debug = 0;
1631 u64 tsf;
1632 if (local->ops->get_tsf)
1633 tsf = local->ops->get_tsf(local_to_hw(local));
1634 else
1635 tsf = -1LLU;
1636 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
Joe Perches0795af52007-10-03 17:59:30 -07001637 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1638 "%s TSF=0x%llx BCN=0x%llx diff=%lld "
Jiri Bencf0706e822007-05-05 11:45:53 -07001639 "@%lu\n",
Joe Perches0795af52007-10-03 17:59:30 -07001640 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
Jiri Bencf0706e822007-05-05 11:45:53 -07001641 (unsigned long long)tsf,
1642 (unsigned long long)timestamp,
1643 (unsigned long long)(tsf - timestamp),
1644 jiffies);
1645 last_tsf_debug = jiffies;
1646 }
1647#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1648 }
1649
John W. Linville67a4cce2007-10-12 16:40:37 -04001650 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07001651
1652 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1653 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1654 (sta = sta_info_get(local, mgmt->sa))) {
1655 struct ieee80211_hw_mode *mode;
1656 struct ieee80211_rate *rates;
1657 size_t num_rates;
1658 u32 supp_rates, prev_rates;
1659 int i, j;
1660
Zhu Yiece8edd2007-11-22 10:53:21 +08001661 mode = local->sta_sw_scanning ?
Jiri Bencf0706e822007-05-05 11:45:53 -07001662 local->scan_hw_mode : local->oper_hw_mode;
Zhu Yiece8edd2007-11-22 10:53:21 +08001663
1664 if (local->sta_hw_scanning) {
1665 /* search for the correct mode matches the beacon */
1666 list_for_each_entry(mode, &local->modes_list, list)
1667 if (mode->mode == rx_status->phymode)
1668 break;
1669
1670 if (mode == NULL)
1671 mode = local->oper_hw_mode;
1672 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001673 rates = mode->rates;
1674 num_rates = mode->num_rates;
1675
1676 supp_rates = 0;
1677 for (i = 0; i < elems.supp_rates_len +
1678 elems.ext_supp_rates_len; i++) {
1679 u8 rate = 0;
1680 int own_rate;
1681 if (i < elems.supp_rates_len)
1682 rate = elems.supp_rates[i];
1683 else if (elems.ext_supp_rates)
1684 rate = elems.ext_supp_rates
1685 [i - elems.supp_rates_len];
1686 own_rate = 5 * (rate & 0x7f);
Jiri Bencf0706e822007-05-05 11:45:53 -07001687 for (j = 0; j < num_rates; j++)
1688 if (rates[j].rate == own_rate)
1689 supp_rates |= BIT(j);
1690 }
1691
1692 prev_rates = sta->supp_rates;
1693 sta->supp_rates &= supp_rates;
1694 if (sta->supp_rates == 0) {
1695 /* No matching rates - this should not really happen.
1696 * Make sure that at least one rate is marked
1697 * supported to avoid issues with TX rate ctrl. */
1698 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1699 }
1700 if (sta->supp_rates != prev_rates) {
1701 printk(KERN_DEBUG "%s: updated supp_rates set for "
Joe Perches0795af52007-10-03 17:59:30 -07001702 "%s based on beacon info (0x%x & 0x%x -> "
Jiri Bencf0706e822007-05-05 11:45:53 -07001703 "0x%x)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001704 dev->name, print_mac(mac, sta->addr), prev_rates,
Jiri Bencf0706e822007-05-05 11:45:53 -07001705 supp_rates, sta->supp_rates);
1706 }
1707 sta_info_put(sta);
1708 }
1709
1710 if (!elems.ssid)
1711 return;
1712
1713 if (elems.ds_params && elems.ds_params_len == 1)
1714 channel = elems.ds_params[0];
1715 else
1716 channel = rx_status->channel;
1717
John W. Linvillecffdd302007-10-05 14:23:27 -04001718 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, channel,
1719 elems.ssid, elems.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07001720 if (!bss) {
John W. Linvillecffdd302007-10-05 14:23:27 -04001721 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, channel,
1722 elems.ssid, elems.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07001723 if (!bss)
1724 return;
1725 } else {
1726#if 0
1727 /* TODO: order by RSSI? */
1728 spin_lock_bh(&local->sta_bss_lock);
1729 list_move_tail(&bss->list, &local->sta_bss_list);
1730 spin_unlock_bh(&local->sta_bss_lock);
1731#endif
1732 }
1733
1734 if (bss->probe_resp && beacon) {
1735 /* Do not allow beacon to override data from Probe Response. */
1736 ieee80211_rx_bss_put(dev, bss);
1737 return;
1738 }
1739
Daniel Drake56282212007-07-10 19:32:10 +02001740 /* save the ERP value so that it is available at association time */
1741 if (elems.erp_info && elems.erp_info_len >= 1) {
1742 bss->erp_value = elems.erp_info[0];
1743 bss->has_erp_value = 1;
1744 }
1745
Jiri Bencf0706e822007-05-05 11:45:53 -07001746 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1747 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e822007-05-05 11:45:53 -07001748
1749 bss->supp_rates_len = 0;
1750 if (elems.supp_rates) {
1751 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1752 if (clen > elems.supp_rates_len)
1753 clen = elems.supp_rates_len;
1754 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1755 clen);
1756 bss->supp_rates_len += clen;
1757 }
1758 if (elems.ext_supp_rates) {
1759 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1760 if (clen > elems.ext_supp_rates_len)
1761 clen = elems.ext_supp_rates_len;
1762 memcpy(&bss->supp_rates[bss->supp_rates_len],
1763 elems.ext_supp_rates, clen);
1764 bss->supp_rates_len += clen;
1765 }
1766
1767 if (elems.wpa &&
1768 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1769 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1770 kfree(bss->wpa_ie);
1771 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1772 if (bss->wpa_ie) {
1773 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1774 bss->wpa_ie_len = elems.wpa_len + 2;
1775 } else
1776 bss->wpa_ie_len = 0;
1777 } else if (!elems.wpa && bss->wpa_ie) {
1778 kfree(bss->wpa_ie);
1779 bss->wpa_ie = NULL;
1780 bss->wpa_ie_len = 0;
1781 }
1782
1783 if (elems.rsn &&
1784 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1785 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1786 kfree(bss->rsn_ie);
1787 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1788 if (bss->rsn_ie) {
1789 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1790 bss->rsn_ie_len = elems.rsn_len + 2;
1791 } else
1792 bss->rsn_ie_len = 0;
1793 } else if (!elems.rsn && bss->rsn_ie) {
1794 kfree(bss->rsn_ie);
1795 bss->rsn_ie = NULL;
1796 bss->rsn_ie_len = 0;
1797 }
1798
1799 if (elems.wmm_param &&
1800 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1801 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1802 kfree(bss->wmm_ie);
1803 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1804 if (bss->wmm_ie) {
1805 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1806 elems.wmm_param_len + 2);
1807 bss->wmm_ie_len = elems.wmm_param_len + 2;
1808 } else
1809 bss->wmm_ie_len = 0;
1810 } else if (!elems.wmm_param && bss->wmm_ie) {
1811 kfree(bss->wmm_ie);
1812 bss->wmm_ie = NULL;
1813 bss->wmm_ie_len = 0;
1814 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02001815 if (elems.ht_cap_elem &&
1816 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
1817 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
1818 kfree(bss->ht_ie);
1819 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
1820 if (bss->ht_ie) {
1821 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
1822 elems.ht_cap_elem_len + 2);
1823 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
1824 } else
1825 bss->ht_ie_len = 0;
1826 } else if (!elems.ht_cap_elem && bss->ht_ie) {
1827 kfree(bss->ht_ie);
1828 bss->ht_ie = NULL;
1829 bss->ht_ie_len = 0;
1830 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001831
1832 bss->hw_mode = rx_status->phymode;
Jiri Bencf0706e822007-05-05 11:45:53 -07001833 bss->freq = rx_status->freq;
1834 if (channel != rx_status->channel &&
1835 (bss->hw_mode == MODE_IEEE80211G ||
1836 bss->hw_mode == MODE_IEEE80211B) &&
1837 channel >= 1 && channel <= 14) {
1838 static const int freq_list[] = {
1839 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1840 2447, 2452, 2457, 2462, 2467, 2472, 2484
1841 };
1842 /* IEEE 802.11g/b mode can receive packets from neighboring
1843 * channels, so map the channel into frequency. */
1844 bss->freq = freq_list[channel - 1];
1845 }
1846 bss->timestamp = timestamp;
1847 bss->last_update = jiffies;
1848 bss->rssi = rx_status->ssi;
1849 bss->signal = rx_status->signal;
1850 bss->noise = rx_status->noise;
1851 if (!beacon)
1852 bss->probe_resp++;
1853 ieee80211_rx_bss_put(dev, bss);
1854}
1855
1856
1857static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1858 struct ieee80211_mgmt *mgmt,
1859 size_t len,
1860 struct ieee80211_rx_status *rx_status)
1861{
1862 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1863}
1864
1865
1866static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1867 struct ieee80211_mgmt *mgmt,
1868 size_t len,
1869 struct ieee80211_rx_status *rx_status)
1870{
Jiri Bencf0706e822007-05-05 11:45:53 -07001871 struct ieee80211_sub_if_data *sdata;
1872 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e822007-05-05 11:45:53 -07001873 size_t baselen;
1874 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001875 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1876 struct ieee80211_conf *conf = &local->hw.conf;
Jiri Bencf0706e822007-05-05 11:45:53 -07001877
1878 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1879
1880 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1881 if (sdata->type != IEEE80211_IF_TYPE_STA)
1882 return;
1883 ifsta = &sdata->u.sta;
1884
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001885 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07001886 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1887 return;
1888
1889 /* Process beacon from the current BSS */
1890 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1891 if (baselen > len)
1892 return;
1893
John W. Linville67a4cce2007-10-12 16:40:37 -04001894 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07001895
Daniel Drake56282212007-07-10 19:32:10 +02001896 if (elems.erp_info && elems.erp_info_len >= 1)
1897 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
Jiri Bencf0706e822007-05-05 11:45:53 -07001898
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001899 if (elems.ht_cap_elem && elems.ht_info_elem &&
1900 elems.wmm_param && local->ops->conf_ht &&
1901 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
1902 struct ieee80211_ht_bss_info bss_info;
1903
1904 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1905 (struct ieee80211_ht_addt_info *)
1906 elems.ht_info_elem, &bss_info);
1907 /* check if AP changed bss inforamation */
1908 if ((conf->ht_bss_conf.primary_channel !=
1909 bss_info.primary_channel) ||
1910 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
1911 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
1912 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
1913 &bss_info);
1914 }
1915
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001916 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001917 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1918 elems.wmm_param_len);
1919 }
1920}
1921
1922
1923static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1924 struct ieee80211_if_sta *ifsta,
1925 struct ieee80211_mgmt *mgmt,
1926 size_t len,
1927 struct ieee80211_rx_status *rx_status)
1928{
1929 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1930 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1931 int tx_last_beacon;
1932 struct sk_buff *skb;
1933 struct ieee80211_mgmt *resp;
1934 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07001935 DECLARE_MAC_BUF(mac);
1936#ifdef CONFIG_MAC80211_IBSS_DEBUG
1937 DECLARE_MAC_BUF(mac2);
1938 DECLARE_MAC_BUF(mac3);
1939#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07001940
1941 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1942 ifsta->state != IEEE80211_IBSS_JOINED ||
1943 len < 24 + 2 || !ifsta->probe_resp)
1944 return;
1945
1946 if (local->ops->tx_last_beacon)
1947 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1948 else
1949 tx_last_beacon = 1;
1950
1951#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07001952 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1953 "%s (tx_last_beacon=%d)\n",
1954 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1955 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e822007-05-05 11:45:53 -07001956#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1957
1958 if (!tx_last_beacon)
1959 return;
1960
1961 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1962 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1963 return;
1964
1965 end = ((u8 *) mgmt) + len;
1966 pos = mgmt->u.probe_req.variable;
1967 if (pos[0] != WLAN_EID_SSID ||
1968 pos + 2 + pos[1] > end) {
1969 if (net_ratelimit()) {
1970 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07001971 "from %s\n",
1972 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001973 }
1974 return;
1975 }
1976 if (pos[1] != 0 &&
1977 (pos[1] != ifsta->ssid_len ||
1978 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1979 /* Ignore ProbeReq for foreign SSID */
1980 return;
1981 }
1982
1983 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001984 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07001985 if (!skb)
1986 return;
1987
1988 resp = (struct ieee80211_mgmt *) skb->data;
1989 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1990#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07001991 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1992 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07001993#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1994 ieee80211_sta_tx(dev, skb, 0);
1995}
1996
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001997void ieee80211_rx_mgmt_action(struct net_device *dev,
1998 struct ieee80211_if_sta *ifsta,
1999 struct ieee80211_mgmt *mgmt,
2000 size_t len)
2001{
2002 if (len < IEEE80211_MIN_ACTION_SIZE)
2003 return;
2004
2005 switch (mgmt->u.action.category) {
2006 case WLAN_CATEGORY_BACK:
2007 switch (mgmt->u.action.u.addba_req.action_code) {
2008 case WLAN_ACTION_ADDBA_REQ:
2009 if (len < (IEEE80211_MIN_ACTION_SIZE +
2010 sizeof(mgmt->u.action.u.addba_req)))
2011 break;
2012 ieee80211_sta_process_addba_request(dev, mgmt, len);
2013 break;
2014 default:
2015 if (net_ratelimit())
2016 printk(KERN_DEBUG "%s: received unsupported BACK\n",
2017 dev->name);
2018 break;
2019 }
2020 break;
2021 default:
2022 break;
2023 }
2024}
Jiri Bencf0706e822007-05-05 11:45:53 -07002025
2026void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2027 struct ieee80211_rx_status *rx_status)
2028{
2029 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2030 struct ieee80211_sub_if_data *sdata;
2031 struct ieee80211_if_sta *ifsta;
2032 struct ieee80211_mgmt *mgmt;
2033 u16 fc;
2034
2035 if (skb->len < 24)
2036 goto fail;
2037
2038 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2039 ifsta = &sdata->u.sta;
2040
2041 mgmt = (struct ieee80211_mgmt *) skb->data;
2042 fc = le16_to_cpu(mgmt->frame_control);
2043
2044 switch (fc & IEEE80211_FCTL_STYPE) {
2045 case IEEE80211_STYPE_PROBE_REQ:
2046 case IEEE80211_STYPE_PROBE_RESP:
2047 case IEEE80211_STYPE_BEACON:
2048 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2049 case IEEE80211_STYPE_AUTH:
2050 case IEEE80211_STYPE_ASSOC_RESP:
2051 case IEEE80211_STYPE_REASSOC_RESP:
2052 case IEEE80211_STYPE_DEAUTH:
2053 case IEEE80211_STYPE_DISASSOC:
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002054 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e822007-05-05 11:45:53 -07002055 skb_queue_tail(&ifsta->skb_queue, skb);
2056 queue_work(local->hw.workqueue, &ifsta->work);
2057 return;
2058 default:
2059 printk(KERN_DEBUG "%s: received unknown management frame - "
2060 "stype=%d\n", dev->name,
2061 (fc & IEEE80211_FCTL_STYPE) >> 4);
2062 break;
2063 }
2064
2065 fail:
2066 kfree_skb(skb);
2067}
2068
2069
2070static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2071 struct sk_buff *skb)
2072{
2073 struct ieee80211_rx_status *rx_status;
2074 struct ieee80211_sub_if_data *sdata;
2075 struct ieee80211_if_sta *ifsta;
2076 struct ieee80211_mgmt *mgmt;
2077 u16 fc;
2078
2079 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2080 ifsta = &sdata->u.sta;
2081
2082 rx_status = (struct ieee80211_rx_status *) skb->cb;
2083 mgmt = (struct ieee80211_mgmt *) skb->data;
2084 fc = le16_to_cpu(mgmt->frame_control);
2085
2086 switch (fc & IEEE80211_FCTL_STYPE) {
2087 case IEEE80211_STYPE_PROBE_REQ:
2088 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2089 rx_status);
2090 break;
2091 case IEEE80211_STYPE_PROBE_RESP:
2092 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2093 break;
2094 case IEEE80211_STYPE_BEACON:
2095 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2096 break;
2097 case IEEE80211_STYPE_AUTH:
2098 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2099 break;
2100 case IEEE80211_STYPE_ASSOC_RESP:
2101 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
2102 break;
2103 case IEEE80211_STYPE_REASSOC_RESP:
2104 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
2105 break;
2106 case IEEE80211_STYPE_DEAUTH:
2107 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2108 break;
2109 case IEEE80211_STYPE_DISASSOC:
2110 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2111 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002112 case IEEE80211_STYPE_ACTION:
2113 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len);
2114 break;
Jiri Bencf0706e822007-05-05 11:45:53 -07002115 }
2116
2117 kfree_skb(skb);
2118}
2119
2120
Zhu Yiece8edd2007-11-22 10:53:21 +08002121ieee80211_txrx_result
2122ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2123 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e822007-05-05 11:45:53 -07002124{
2125 struct ieee80211_mgmt *mgmt;
2126 u16 fc;
2127
Zhu Yiece8edd2007-11-22 10:53:21 +08002128 if (skb->len < 2)
2129 return TXRX_DROP;
Jiri Bencf0706e822007-05-05 11:45:53 -07002130
2131 mgmt = (struct ieee80211_mgmt *) skb->data;
2132 fc = le16_to_cpu(mgmt->frame_control);
2133
Zhu Yiece8edd2007-11-22 10:53:21 +08002134 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
2135 return TXRX_CONTINUE;
2136
2137 if (skb->len < 24)
2138 return TXRX_DROP;
2139
Jiri Bencf0706e822007-05-05 11:45:53 -07002140 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2141 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2142 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2143 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002144 dev_kfree_skb(skb);
2145 return TXRX_QUEUED;
Jiri Bencf0706e822007-05-05 11:45:53 -07002146 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2147 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2148 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002149 dev_kfree_skb(skb);
2150 return TXRX_QUEUED;
Jiri Bencf0706e822007-05-05 11:45:53 -07002151 }
2152 }
Zhu Yiece8edd2007-11-22 10:53:21 +08002153 return TXRX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002154}
2155
2156
2157static int ieee80211_sta_active_ibss(struct net_device *dev)
2158{
2159 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2160 int active = 0;
2161 struct sta_info *sta;
2162
Michael Wube8755e2007-07-27 15:43:23 +02002163 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07002164 list_for_each_entry(sta, &local->sta_list, list) {
2165 if (sta->dev == dev &&
2166 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2167 jiffies)) {
2168 active++;
2169 break;
2170 }
2171 }
Michael Wube8755e2007-07-27 15:43:23 +02002172 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07002173
2174 return active;
2175}
2176
2177
2178static void ieee80211_sta_expire(struct net_device *dev)
2179{
2180 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2181 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02002182 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07002183 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07002184
Michael Wube8755e2007-07-27 15:43:23 +02002185 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07002186 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2187 if (time_after(jiffies, sta->last_rx +
2188 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
Joe Perches0795af52007-10-03 17:59:30 -07002189 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2190 dev->name, print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +02002191 __sta_info_get(sta);
2192 sta_info_remove(sta);
2193 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e822007-05-05 11:45:53 -07002194 }
Michael Wube8755e2007-07-27 15:43:23 +02002195 write_unlock_bh(&local->sta_lock);
2196
2197 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
2198 sta_info_free(sta);
2199 sta_info_put(sta);
2200 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002201}
2202
2203
2204static void ieee80211_sta_merge_ibss(struct net_device *dev,
2205 struct ieee80211_if_sta *ifsta)
2206{
2207 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2208
2209 ieee80211_sta_expire(dev);
2210 if (ieee80211_sta_active_ibss(dev))
2211 return;
2212
2213 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2214 "IBSS networks with same SSID (merge)\n", dev->name);
2215 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2216}
2217
2218
2219void ieee80211_sta_timer(unsigned long data)
2220{
2221 struct ieee80211_sub_if_data *sdata =
2222 (struct ieee80211_sub_if_data *) data;
2223 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2224 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2225
2226 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2227 queue_work(local->hw.workqueue, &ifsta->work);
2228}
2229
2230
2231void ieee80211_sta_work(struct work_struct *work)
2232{
2233 struct ieee80211_sub_if_data *sdata =
2234 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2235 struct net_device *dev = sdata->dev;
2236 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2237 struct ieee80211_if_sta *ifsta;
2238 struct sk_buff *skb;
2239
2240 if (!netif_running(dev))
2241 return;
2242
Zhu Yiece8edd2007-11-22 10:53:21 +08002243 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07002244 return;
2245
2246 if (sdata->type != IEEE80211_IF_TYPE_STA &&
2247 sdata->type != IEEE80211_IF_TYPE_IBSS) {
2248 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2249 "(type=%d)\n", dev->name, sdata->type);
2250 return;
2251 }
2252 ifsta = &sdata->u.sta;
2253
2254 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2255 ieee80211_sta_rx_queued_mgmt(dev, skb);
2256
2257 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2258 ifsta->state != IEEE80211_ASSOCIATE &&
2259 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01002260 if (ifsta->scan_ssid_len)
2261 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
2262 else
2263 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07002264 return;
2265 }
2266
2267 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2268 if (ieee80211_sta_config_auth(dev, ifsta))
2269 return;
2270 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2271 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2272 return;
2273
2274 switch (ifsta->state) {
2275 case IEEE80211_DISABLED:
2276 break;
2277 case IEEE80211_AUTHENTICATE:
2278 ieee80211_authenticate(dev, ifsta);
2279 break;
2280 case IEEE80211_ASSOCIATE:
2281 ieee80211_associate(dev, ifsta);
2282 break;
2283 case IEEE80211_ASSOCIATED:
2284 ieee80211_associated(dev, ifsta);
2285 break;
2286 case IEEE80211_IBSS_SEARCH:
2287 ieee80211_sta_find_ibss(dev, ifsta);
2288 break;
2289 case IEEE80211_IBSS_JOINED:
2290 ieee80211_sta_merge_ibss(dev, ifsta);
2291 break;
2292 default:
2293 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2294 ifsta->state);
2295 break;
2296 }
2297
2298 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2299 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2300 "mixed-cell disabled - disassociate\n", dev->name);
2301
2302 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2303 ieee80211_set_disassoc(dev, ifsta, 0);
2304 }
2305}
2306
2307
2308static void ieee80211_sta_reset_auth(struct net_device *dev,
2309 struct ieee80211_if_sta *ifsta)
2310{
2311 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2312
2313 if (local->ops->reset_tsf) {
2314 /* Reset own TSF to allow time synchronization work. */
2315 local->ops->reset_tsf(local_to_hw(local));
2316 }
2317
2318 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2319
2320
2321 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2322 ifsta->auth_alg = WLAN_AUTH_OPEN;
2323 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2324 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2325 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2326 ifsta->auth_alg = WLAN_AUTH_LEAP;
2327 else
2328 ifsta->auth_alg = WLAN_AUTH_OPEN;
2329 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2330 ifsta->auth_alg);
2331 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002332 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2333 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07002334 netif_carrier_off(dev);
2335}
2336
2337
2338void ieee80211_sta_req_auth(struct net_device *dev,
2339 struct ieee80211_if_sta *ifsta)
2340{
2341 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2342 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2343
2344 if (sdata->type != IEEE80211_IF_TYPE_STA)
2345 return;
2346
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002347 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2348 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2349 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2350 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002351 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2352 queue_work(local->hw.workqueue, &ifsta->work);
2353 }
2354}
2355
2356static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2357 const char *ssid, int ssid_len)
2358{
2359 int tmp, hidden_ssid;
2360
Michael Wu48225702007-10-19 17:14:36 -04002361 if (ssid_len == ifsta->ssid_len &&
2362 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e822007-05-05 11:45:53 -07002363 return 1;
2364
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002365 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e822007-05-05 11:45:53 -07002366 return 0;
2367
2368 hidden_ssid = 1;
2369 tmp = ssid_len;
2370 while (tmp--) {
2371 if (ssid[tmp] != '\0') {
2372 hidden_ssid = 0;
2373 break;
2374 }
2375 }
2376
2377 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2378 return 1;
2379
2380 if (ssid_len == 1 && ssid[0] == ' ')
2381 return 1;
2382
2383 return 0;
2384}
2385
2386static int ieee80211_sta_config_auth(struct net_device *dev,
2387 struct ieee80211_if_sta *ifsta)
2388{
2389 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2390 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2391 struct ieee80211_sta_bss *bss, *selected = NULL;
2392 int top_rssi = 0, freq;
2393
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002394 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2395 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002396 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002397 ieee80211_sta_reset_auth(dev, ifsta);
2398 return 0;
2399 }
2400
2401 spin_lock_bh(&local->sta_bss_lock);
2402 freq = local->oper_channel->freq;
2403 list_for_each_entry(bss, &local->sta_bss_list, list) {
2404 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2405 continue;
2406
2407 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2408 !!sdata->default_key)
2409 continue;
2410
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002411 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2412 bss->freq != freq)
Jiri Bencf0706e822007-05-05 11:45:53 -07002413 continue;
2414
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002415 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002416 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2417 continue;
2418
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002419 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002420 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2421 continue;
2422
2423 if (!selected || top_rssi < bss->rssi) {
2424 selected = bss;
2425 top_rssi = bss->rssi;
2426 }
2427 }
2428 if (selected)
2429 atomic_inc(&selected->users);
2430 spin_unlock_bh(&local->sta_bss_lock);
2431
2432 if (selected) {
2433 ieee80211_set_channel(local, -1, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002434 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e822007-05-05 11:45:53 -07002435 ieee80211_sta_set_ssid(dev, selected->ssid,
2436 selected->ssid_len);
2437 ieee80211_sta_set_bssid(dev, selected->bssid);
2438 ieee80211_rx_bss_put(dev, selected);
2439 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002440 ieee80211_sta_reset_auth(dev, ifsta);
2441 return 0;
2442 } else {
2443 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002444 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04002445 ieee80211_sta_start_scan(dev, NULL, 0);
2446 else
2447 ieee80211_sta_start_scan(dev, ifsta->ssid,
2448 ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002449 ifsta->state = IEEE80211_AUTHENTICATE;
2450 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2451 } else
2452 ifsta->state = IEEE80211_DISABLED;
2453 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002454 return -1;
2455}
2456
2457static int ieee80211_sta_join_ibss(struct net_device *dev,
2458 struct ieee80211_if_sta *ifsta,
2459 struct ieee80211_sta_bss *bss)
2460{
2461 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2462 int res, rates, i, j;
2463 struct sk_buff *skb;
2464 struct ieee80211_mgmt *mgmt;
2465 struct ieee80211_tx_control control;
2466 struct ieee80211_rate *rate;
2467 struct ieee80211_hw_mode *mode;
2468 struct rate_control_extra extra;
2469 u8 *pos;
2470 struct ieee80211_sub_if_data *sdata;
2471
2472 /* Remove possible STA entries from other IBSS networks. */
2473 sta_info_flush(local, NULL);
2474
2475 if (local->ops->reset_tsf) {
2476 /* Reset own TSF to allow time synchronization work. */
2477 local->ops->reset_tsf(local_to_hw(local));
2478 }
2479 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2480 res = ieee80211_if_config(dev);
2481 if (res)
2482 return res;
2483
2484 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2485
2486 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2487 sdata->drop_unencrypted = bss->capability &
2488 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2489
2490 res = ieee80211_set_channel(local, -1, bss->freq);
2491
2492 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2493 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2494 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2495 local->hw.conf.freq);
2496 return -1;
2497 }
2498
2499 /* Set beacon template based on scan results */
2500 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2501 do {
2502 if (!skb)
2503 break;
2504
2505 skb_reserve(skb, local->hw.extra_tx_headroom);
2506
2507 mgmt = (struct ieee80211_mgmt *)
2508 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2509 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2510 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2511 IEEE80211_STYPE_BEACON);
2512 memset(mgmt->da, 0xff, ETH_ALEN);
2513 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2514 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2515 mgmt->u.beacon.beacon_int =
2516 cpu_to_le16(local->hw.conf.beacon_int);
2517 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2518
2519 pos = skb_put(skb, 2 + ifsta->ssid_len);
2520 *pos++ = WLAN_EID_SSID;
2521 *pos++ = ifsta->ssid_len;
2522 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2523
2524 rates = bss->supp_rates_len;
2525 if (rates > 8)
2526 rates = 8;
2527 pos = skb_put(skb, 2 + rates);
2528 *pos++ = WLAN_EID_SUPP_RATES;
2529 *pos++ = rates;
2530 memcpy(pos, bss->supp_rates, rates);
2531
2532 pos = skb_put(skb, 2 + 1);
2533 *pos++ = WLAN_EID_DS_PARAMS;
2534 *pos++ = 1;
2535 *pos++ = bss->channel;
2536
2537 pos = skb_put(skb, 2 + 2);
2538 *pos++ = WLAN_EID_IBSS_PARAMS;
2539 *pos++ = 2;
2540 /* FIX: set ATIM window based on scan results */
2541 *pos++ = 0;
2542 *pos++ = 0;
2543
2544 if (bss->supp_rates_len > 8) {
2545 rates = bss->supp_rates_len - 8;
2546 pos = skb_put(skb, 2 + rates);
2547 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2548 *pos++ = rates;
2549 memcpy(pos, &bss->supp_rates[8], rates);
2550 }
2551
2552 memset(&control, 0, sizeof(control));
2553 memset(&extra, 0, sizeof(extra));
2554 extra.mode = local->oper_hw_mode;
2555 rate = rate_control_get_rate(local, dev, skb, &extra);
2556 if (!rate) {
2557 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2558 "for IBSS beacon\n", dev->name);
2559 break;
2560 }
Jiri Slaby13262ff2007-08-28 17:01:54 -04002561 control.tx_rate =
2562 ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
2563 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
Jiri Bencf0706e822007-05-05 11:45:53 -07002564 rate->val2 : rate->val;
2565 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2566 control.power_level = local->hw.conf.power_level;
2567 control.flags |= IEEE80211_TXCTL_NO_ACK;
2568 control.retry_limit = 1;
2569
2570 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2571 if (ifsta->probe_resp) {
2572 mgmt = (struct ieee80211_mgmt *)
2573 ifsta->probe_resp->data;
2574 mgmt->frame_control =
2575 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2576 IEEE80211_STYPE_PROBE_RESP);
2577 } else {
2578 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2579 "template for IBSS\n", dev->name);
2580 }
2581
2582 if (local->ops->beacon_update &&
2583 local->ops->beacon_update(local_to_hw(local),
2584 skb, &control) == 0) {
2585 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2586 "template based on scan results\n", dev->name);
2587 skb = NULL;
2588 }
2589
2590 rates = 0;
2591 mode = local->oper_hw_mode;
2592 for (i = 0; i < bss->supp_rates_len; i++) {
2593 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
Jiri Bencf0706e822007-05-05 11:45:53 -07002594 for (j = 0; j < mode->num_rates; j++)
2595 if (mode->rates[j].rate == bitrate)
2596 rates |= BIT(j);
2597 }
2598 ifsta->supp_rates_bits = rates;
2599 } while (0);
2600
2601 if (skb) {
2602 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2603 "template\n", dev->name);
2604 dev_kfree_skb(skb);
2605 }
2606
2607 ifsta->state = IEEE80211_IBSS_JOINED;
2608 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2609
2610 ieee80211_rx_bss_put(dev, bss);
2611
2612 return res;
2613}
2614
2615
2616static int ieee80211_sta_create_ibss(struct net_device *dev,
2617 struct ieee80211_if_sta *ifsta)
2618{
2619 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2620 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04002621 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -07002622 struct ieee80211_hw_mode *mode;
2623 u8 bssid[ETH_ALEN], *pos;
2624 int i;
Joe Perches0795af52007-10-03 17:59:30 -07002625 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07002626
2627#if 0
2628 /* Easier testing, use fixed BSSID. */
2629 memset(bssid, 0xfe, ETH_ALEN);
2630#else
2631 /* Generate random, not broadcast, locally administered BSSID. Mix in
2632 * own MAC address to make sure that devices that do not have proper
2633 * random number generator get different BSSID. */
2634 get_random_bytes(bssid, ETH_ALEN);
2635 for (i = 0; i < ETH_ALEN; i++)
2636 bssid[i] ^= dev->dev_addr[i];
2637 bssid[0] &= ~0x01;
2638 bssid[0] |= 0x02;
2639#endif
2640
Joe Perches0795af52007-10-03 17:59:30 -07002641 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2642 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002643
John W. Linvillecffdd302007-10-05 14:23:27 -04002644 bss = ieee80211_rx_bss_add(dev, bssid, local->hw.conf.channel,
2645 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002646 if (!bss)
2647 return -ENOMEM;
2648
Jiri Bencf0706e822007-05-05 11:45:53 -07002649 mode = local->oper_hw_mode;
2650
2651 if (local->hw.conf.beacon_int == 0)
2652 local->hw.conf.beacon_int = 100;
2653 bss->beacon_int = local->hw.conf.beacon_int;
2654 bss->hw_mode = local->hw.conf.phymode;
Jiri Bencf0706e822007-05-05 11:45:53 -07002655 bss->freq = local->hw.conf.freq;
2656 bss->last_update = jiffies;
2657 bss->capability = WLAN_CAPABILITY_IBSS;
2658 if (sdata->default_key) {
2659 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2660 } else
2661 sdata->drop_unencrypted = 0;
2662 bss->supp_rates_len = mode->num_rates;
2663 pos = bss->supp_rates;
2664 for (i = 0; i < mode->num_rates; i++) {
2665 int rate = mode->rates[i].rate;
Jiri Bencf0706e822007-05-05 11:45:53 -07002666 *pos++ = (u8) (rate / 5);
2667 }
2668
2669 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2670}
2671
2672
2673static int ieee80211_sta_find_ibss(struct net_device *dev,
2674 struct ieee80211_if_sta *ifsta)
2675{
2676 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2677 struct ieee80211_sta_bss *bss;
2678 int found = 0;
2679 u8 bssid[ETH_ALEN];
2680 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07002681 DECLARE_MAC_BUF(mac);
2682 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07002683
2684 if (ifsta->ssid_len == 0)
2685 return -EINVAL;
2686
2687 active_ibss = ieee80211_sta_active_ibss(dev);
2688#ifdef CONFIG_MAC80211_IBSS_DEBUG
2689 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2690 dev->name, active_ibss);
2691#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2692 spin_lock_bh(&local->sta_bss_lock);
2693 list_for_each_entry(bss, &local->sta_bss_list, list) {
2694 if (ifsta->ssid_len != bss->ssid_len ||
2695 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2696 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2697 continue;
2698#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002699 printk(KERN_DEBUG " bssid=%s found\n",
2700 print_mac(mac, bss->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002701#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2702 memcpy(bssid, bss->bssid, ETH_ALEN);
2703 found = 1;
2704 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2705 break;
2706 }
2707 spin_unlock_bh(&local->sta_bss_lock);
2708
2709#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002710 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
2711 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002712#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2713 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002714 (bss = ieee80211_rx_bss_get(dev, bssid, local->hw.conf.channel,
2715 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07002716 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07002717 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07002718 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002719 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2720 }
2721#ifdef CONFIG_MAC80211_IBSS_DEBUG
2722 printk(KERN_DEBUG " did not try to join ibss\n");
2723#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2724
2725 /* Selected IBSS not found in current scan results - try to scan */
2726 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2727 !ieee80211_sta_active_ibss(dev)) {
2728 mod_timer(&ifsta->timer, jiffies +
2729 IEEE80211_IBSS_MERGE_INTERVAL);
2730 } else if (time_after(jiffies, local->last_scan_completed +
2731 IEEE80211_SCAN_INTERVAL)) {
2732 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2733 "join\n", dev->name);
2734 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2735 ifsta->ssid_len);
2736 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2737 int interval = IEEE80211_SCAN_INTERVAL;
2738
2739 if (time_after(jiffies, ifsta->ibss_join_req +
2740 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002741 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002742 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2743 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002744 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002745 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2746 " configured channel %d (%d MHz)\n",
2747 dev->name, local->hw.conf.channel,
2748 local->hw.conf.freq);
2749 }
2750
2751 /* No IBSS found - decrease scan interval and continue
2752 * scanning. */
2753 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2754 }
2755
2756 ifsta->state = IEEE80211_IBSS_SEARCH;
2757 mod_timer(&ifsta->timer, jiffies + interval);
2758 return 0;
2759 }
2760
2761 return 0;
2762}
2763
2764
2765int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2766{
2767 struct ieee80211_sub_if_data *sdata;
2768 struct ieee80211_if_sta *ifsta;
2769 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2770
2771 if (len > IEEE80211_MAX_SSID_LEN)
2772 return -EINVAL;
2773
2774 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2775 * not defined. */
2776 if (local->ops->conf_tx) {
2777 struct ieee80211_tx_queue_params qparam;
2778 int i;
2779
2780 memset(&qparam, 0, sizeof(qparam));
2781 /* TODO: are these ok defaults for all hw_modes? */
2782 qparam.aifs = 2;
2783 qparam.cw_min =
2784 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2785 qparam.cw_max = 1023;
2786 qparam.burst_time = 0;
2787 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2788 {
2789 local->ops->conf_tx(local_to_hw(local),
2790 i + IEEE80211_TX_QUEUE_DATA0,
2791 &qparam);
2792 }
2793 /* IBSS uses different parameters for Beacon sending */
2794 qparam.cw_min++;
2795 qparam.cw_min *= 2;
2796 qparam.cw_min--;
2797 local->ops->conf_tx(local_to_hw(local),
2798 IEEE80211_TX_QUEUE_BEACON, &qparam);
2799 }
2800
2801 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2802 ifsta = &sdata->u.sta;
2803
2804 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002805 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07002806 memcpy(ifsta->ssid, ssid, len);
2807 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2808 ifsta->ssid_len = len;
2809
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002810 if (len)
2811 ifsta->flags |= IEEE80211_STA_SSID_SET;
2812 else
2813 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2814 if (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2815 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002816 ifsta->ibss_join_req = jiffies;
2817 ifsta->state = IEEE80211_IBSS_SEARCH;
2818 return ieee80211_sta_find_ibss(dev, ifsta);
2819 }
2820 return 0;
2821}
2822
2823
2824int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2825{
2826 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2827 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2828 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2829 *len = ifsta->ssid_len;
2830 return 0;
2831}
2832
2833
2834int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2835{
2836 struct ieee80211_sub_if_data *sdata;
2837 struct ieee80211_if_sta *ifsta;
2838 int res;
2839
2840 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2841 ifsta = &sdata->u.sta;
2842
2843 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2844 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2845 res = ieee80211_if_config(dev);
2846 if (res) {
2847 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2848 "the low-level driver\n", dev->name);
2849 return res;
2850 }
2851 }
2852
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002853 if (is_valid_ether_addr(bssid))
2854 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07002855 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002856 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2857
Jiri Bencf0706e822007-05-05 11:45:53 -07002858 return 0;
2859}
2860
2861
2862static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2863 struct ieee80211_sub_if_data *sdata,
2864 int powersave)
2865{
2866 struct sk_buff *skb;
2867 struct ieee80211_hdr *nullfunc;
2868 u16 fc;
2869
2870 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2871 if (!skb) {
2872 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2873 "frame\n", sdata->dev->name);
2874 return;
2875 }
2876 skb_reserve(skb, local->hw.extra_tx_headroom);
2877
2878 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2879 memset(nullfunc, 0, 24);
2880 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2881 IEEE80211_FCTL_TODS;
2882 if (powersave)
2883 fc |= IEEE80211_FCTL_PM;
2884 nullfunc->frame_control = cpu_to_le16(fc);
2885 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2886 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2887 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2888
2889 ieee80211_sta_tx(sdata->dev, skb, 0);
2890}
2891
2892
2893void ieee80211_scan_completed(struct ieee80211_hw *hw)
2894{
2895 struct ieee80211_local *local = hw_to_local(hw);
2896 struct net_device *dev = local->scan_dev;
2897 struct ieee80211_sub_if_data *sdata;
2898 union iwreq_data wrqu;
2899
2900 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08002901 memset(&wrqu, 0, sizeof(wrqu));
2902 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e822007-05-05 11:45:53 -07002903
Zhu Yiece8edd2007-11-22 10:53:21 +08002904 if (local->sta_hw_scanning) {
2905 local->sta_hw_scanning = 0;
2906 goto done;
2907 }
2908
2909 local->sta_sw_scanning = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07002910 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09002911 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e822007-05-05 11:45:53 -07002912 "channel after scan\n", dev->name);
2913
Johannes Berg4150c572007-09-17 01:29:23 -04002914
2915 netif_tx_lock_bh(local->mdev);
2916 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
2917 local->ops->configure_filter(local_to_hw(local),
2918 FIF_BCN_PRBRESP_PROMISC,
2919 &local->filter_flags,
2920 local->mdev->mc_count,
2921 local->mdev->mc_list);
2922
2923 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07002924
Johannes Berg79010422007-09-18 17:29:21 -04002925 rcu_read_lock();
2926 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02002927
2928 /* No need to wake the master device. */
2929 if (sdata->dev == local->mdev)
2930 continue;
2931
Jiri Bencf0706e822007-05-05 11:45:53 -07002932 if (sdata->type == IEEE80211_IF_TYPE_STA) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002933 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07002934 ieee80211_send_nullfunc(local, sdata, 0);
2935 ieee80211_sta_timer((unsigned long)sdata);
2936 }
Mattias Nissler14042cb2007-06-08 15:31:13 +02002937
Jiri Bencf0706e822007-05-05 11:45:53 -07002938 netif_wake_queue(sdata->dev);
2939 }
Johannes Berg79010422007-09-18 17:29:21 -04002940 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002941
Zhu Yiece8edd2007-11-22 10:53:21 +08002942done:
Jiri Bencf0706e822007-05-05 11:45:53 -07002943 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2944 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2945 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002946 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002947 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2948 !ieee80211_sta_active_ibss(dev)))
2949 ieee80211_sta_find_ibss(dev, ifsta);
2950 }
2951}
2952EXPORT_SYMBOL(ieee80211_scan_completed);
2953
2954void ieee80211_sta_scan_work(struct work_struct *work)
2955{
2956 struct ieee80211_local *local =
2957 container_of(work, struct ieee80211_local, scan_work.work);
2958 struct net_device *dev = local->scan_dev;
2959 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2960 struct ieee80211_hw_mode *mode;
2961 struct ieee80211_channel *chan;
2962 int skip;
2963 unsigned long next_delay = 0;
2964
Zhu Yiece8edd2007-11-22 10:53:21 +08002965 if (!local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07002966 return;
2967
2968 switch (local->scan_state) {
2969 case SCAN_SET_CHANNEL:
2970 mode = local->scan_hw_mode;
2971 if (local->scan_hw_mode->list.next == &local->modes_list &&
2972 local->scan_channel_idx >= mode->num_channels) {
2973 ieee80211_scan_completed(local_to_hw(local));
2974 return;
2975 }
2976 skip = !(local->enabled_modes & (1 << mode->mode));
2977 chan = &mode->channels[local->scan_channel_idx];
2978 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2979 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2980 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2981 (local->hw_modes & local->enabled_modes &
2982 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2983 skip = 1;
2984
2985 if (!skip) {
2986#if 0
2987 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2988 dev->name, chan->chan, chan->freq);
2989#endif
2990
2991 local->scan_channel = chan;
2992 if (ieee80211_hw_config(local)) {
2993 printk(KERN_DEBUG "%s: failed to set channel "
2994 "%d (%d MHz) for scan\n", dev->name,
2995 chan->chan, chan->freq);
2996 skip = 1;
2997 }
2998 }
2999
3000 local->scan_channel_idx++;
3001 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
3002 if (local->scan_hw_mode->list.next != &local->modes_list) {
3003 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
3004 struct ieee80211_hw_mode,
3005 list);
3006 local->scan_channel_idx = 0;
3007 }
3008 }
3009
3010 if (skip)
3011 break;
3012
3013 next_delay = IEEE80211_PROBE_DELAY +
3014 usecs_to_jiffies(local->hw.channel_change_time);
3015 local->scan_state = SCAN_SEND_PROBE;
3016 break;
3017 case SCAN_SEND_PROBE:
3018 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
3019 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3020 local->scan_ssid_len);
3021 next_delay = IEEE80211_CHANNEL_TIME;
3022 } else
3023 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3024 local->scan_state = SCAN_SET_CHANNEL;
3025 break;
3026 }
3027
Zhu Yiece8edd2007-11-22 10:53:21 +08003028 if (local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003029 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3030 next_delay);
3031}
3032
3033
3034static int ieee80211_sta_start_scan(struct net_device *dev,
3035 u8 *ssid, size_t ssid_len)
3036{
3037 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3038 struct ieee80211_sub_if_data *sdata;
3039
3040 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3041 return -EINVAL;
3042
3043 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3044 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3045 * BSSID: MACAddress
3046 * SSID
3047 * ScanType: ACTIVE, PASSIVE
3048 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3049 * a Probe frame during active scanning
3050 * ChannelList
3051 * MinChannelTime (>= ProbeDelay), in TU
3052 * MaxChannelTime: (>= MinChannelTime), in TU
3053 */
3054
3055 /* MLME-SCAN.confirm
3056 * BSSDescriptionSet
3057 * ResultCode: SUCCESS, INVALID_PARAMETERS
3058 */
3059
Zhu Yiece8edd2007-11-22 10:53:21 +08003060 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003061 if (local->scan_dev == dev)
3062 return 0;
3063 return -EBUSY;
3064 }
3065
3066 if (local->ops->hw_scan) {
3067 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003068 ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003069 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003070 local->sta_hw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07003071 local->scan_dev = dev;
3072 }
3073 return rc;
3074 }
3075
Zhu Yiece8edd2007-11-22 10:53:21 +08003076 local->sta_sw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07003077
Johannes Berg79010422007-09-18 17:29:21 -04003078 rcu_read_lock();
3079 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02003080
3081 /* Don't stop the master interface, otherwise we can't transmit
3082 * probes! */
3083 if (sdata->dev == local->mdev)
3084 continue;
3085
Jiri Bencf0706e822007-05-05 11:45:53 -07003086 netif_stop_queue(sdata->dev);
3087 if (sdata->type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003088 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07003089 ieee80211_send_nullfunc(local, sdata, 1);
3090 }
Johannes Berg79010422007-09-18 17:29:21 -04003091 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003092
3093 if (ssid) {
3094 local->scan_ssid_len = ssid_len;
3095 memcpy(local->scan_ssid, ssid, ssid_len);
3096 } else
3097 local->scan_ssid_len = 0;
3098 local->scan_state = SCAN_SET_CHANNEL;
3099 local->scan_hw_mode = list_entry(local->modes_list.next,
3100 struct ieee80211_hw_mode,
3101 list);
3102 local->scan_channel_idx = 0;
3103 local->scan_dev = dev;
3104
Johannes Berg4150c572007-09-17 01:29:23 -04003105 netif_tx_lock_bh(local->mdev);
3106 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3107 local->ops->configure_filter(local_to_hw(local),
3108 FIF_BCN_PRBRESP_PROMISC,
3109 &local->filter_flags,
3110 local->mdev->mc_count,
3111 local->mdev->mc_list);
3112 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003113
3114 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3115 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3116 IEEE80211_CHANNEL_TIME);
3117
3118 return 0;
3119}
3120
3121
3122int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3123{
3124 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3125 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3126 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3127
3128 if (sdata->type != IEEE80211_IF_TYPE_STA)
3129 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3130
Zhu Yiece8edd2007-11-22 10:53:21 +08003131 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003132 if (local->scan_dev == dev)
3133 return 0;
3134 return -EBUSY;
3135 }
3136
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003137 ifsta->scan_ssid_len = ssid_len;
3138 if (ssid_len)
3139 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003140 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3141 queue_work(local->hw.workqueue, &ifsta->work);
3142 return 0;
3143}
3144
3145static char *
3146ieee80211_sta_scan_result(struct net_device *dev,
3147 struct ieee80211_sta_bss *bss,
3148 char *current_ev, char *end_buf)
3149{
3150 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3151 struct iw_event iwe;
3152
3153 if (time_after(jiffies,
3154 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3155 return current_ev;
3156
3157 if (!(local->enabled_modes & (1 << bss->hw_mode)))
3158 return current_ev;
3159
Jiri Bencf0706e822007-05-05 11:45:53 -07003160 memset(&iwe, 0, sizeof(iwe));
3161 iwe.cmd = SIOCGIWAP;
3162 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3163 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3164 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3165 IW_EV_ADDR_LEN);
3166
3167 memset(&iwe, 0, sizeof(iwe));
3168 iwe.cmd = SIOCGIWESSID;
3169 iwe.u.data.length = bss->ssid_len;
3170 iwe.u.data.flags = 1;
3171 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3172 bss->ssid);
3173
3174 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
3175 memset(&iwe, 0, sizeof(iwe));
3176 iwe.cmd = SIOCGIWMODE;
3177 if (bss->capability & WLAN_CAPABILITY_ESS)
3178 iwe.u.mode = IW_MODE_MASTER;
3179 else
3180 iwe.u.mode = IW_MODE_ADHOC;
3181 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3182 IW_EV_UINT_LEN);
3183 }
3184
3185 memset(&iwe, 0, sizeof(iwe));
3186 iwe.cmd = SIOCGIWFREQ;
3187 iwe.u.freq.m = bss->channel;
3188 iwe.u.freq.e = 0;
3189 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3190 IW_EV_FREQ_LEN);
3191 iwe.u.freq.m = bss->freq * 100000;
3192 iwe.u.freq.e = 1;
3193 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3194 IW_EV_FREQ_LEN);
3195
3196 memset(&iwe, 0, sizeof(iwe));
3197 iwe.cmd = IWEVQUAL;
3198 iwe.u.qual.qual = bss->signal;
3199 iwe.u.qual.level = bss->rssi;
3200 iwe.u.qual.noise = bss->noise;
3201 iwe.u.qual.updated = local->wstats_flags;
3202 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3203 IW_EV_QUAL_LEN);
3204
3205 memset(&iwe, 0, sizeof(iwe));
3206 iwe.cmd = SIOCGIWENCODE;
3207 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3208 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3209 else
3210 iwe.u.data.flags = IW_ENCODE_DISABLED;
3211 iwe.u.data.length = 0;
3212 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3213
3214 if (bss && bss->wpa_ie) {
3215 memset(&iwe, 0, sizeof(iwe));
3216 iwe.cmd = IWEVGENIE;
3217 iwe.u.data.length = bss->wpa_ie_len;
3218 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3219 bss->wpa_ie);
3220 }
3221
3222 if (bss && bss->rsn_ie) {
3223 memset(&iwe, 0, sizeof(iwe));
3224 iwe.cmd = IWEVGENIE;
3225 iwe.u.data.length = bss->rsn_ie_len;
3226 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3227 bss->rsn_ie);
3228 }
3229
3230 if (bss && bss->supp_rates_len > 0) {
3231 /* display all supported rates in readable format */
3232 char *p = current_ev + IW_EV_LCP_LEN;
3233 int i;
3234
3235 memset(&iwe, 0, sizeof(iwe));
3236 iwe.cmd = SIOCGIWRATE;
3237 /* Those two flags are ignored... */
3238 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3239
3240 for (i = 0; i < bss->supp_rates_len; i++) {
3241 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3242 0x7f) * 500000);
3243 p = iwe_stream_add_value(current_ev, p,
3244 end_buf, &iwe, IW_EV_PARAM_LEN);
3245 }
3246 current_ev = p;
3247 }
3248
3249 if (bss) {
3250 char *buf;
3251 buf = kmalloc(30, GFP_ATOMIC);
3252 if (buf) {
3253 memset(&iwe, 0, sizeof(iwe));
3254 iwe.cmd = IWEVCUSTOM;
3255 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3256 iwe.u.data.length = strlen(buf);
3257 current_ev = iwe_stream_add_point(current_ev, end_buf,
3258 &iwe, buf);
3259 kfree(buf);
3260 }
3261 }
3262
3263 do {
3264 char *buf;
3265
Jiri Bencf0706e822007-05-05 11:45:53 -07003266 buf = kmalloc(100, GFP_ATOMIC);
3267 if (!buf)
3268 break;
3269
3270 memset(&iwe, 0, sizeof(iwe));
3271 iwe.cmd = IWEVCUSTOM;
3272 sprintf(buf, "bcn_int=%d", bss->beacon_int);
3273 iwe.u.data.length = strlen(buf);
3274 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3275 buf);
3276
3277 memset(&iwe, 0, sizeof(iwe));
3278 iwe.cmd = IWEVCUSTOM;
3279 sprintf(buf, "capab=0x%04x", bss->capability);
3280 iwe.u.data.length = strlen(buf);
3281 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3282 buf);
3283
3284 kfree(buf);
3285 break;
3286 } while (0);
3287
3288 return current_ev;
3289}
3290
3291
3292int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3293{
3294 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3295 char *current_ev = buf;
3296 char *end_buf = buf + len;
3297 struct ieee80211_sta_bss *bss;
3298
3299 spin_lock_bh(&local->sta_bss_lock);
3300 list_for_each_entry(bss, &local->sta_bss_list, list) {
3301 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3302 spin_unlock_bh(&local->sta_bss_lock);
3303 return -E2BIG;
3304 }
3305 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3306 end_buf);
3307 }
3308 spin_unlock_bh(&local->sta_bss_lock);
3309 return current_ev - buf;
3310}
3311
3312
3313int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3314{
3315 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3316 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3317 kfree(ifsta->extra_ie);
3318 if (len == 0) {
3319 ifsta->extra_ie = NULL;
3320 ifsta->extra_ie_len = 0;
3321 return 0;
3322 }
3323 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3324 if (!ifsta->extra_ie) {
3325 ifsta->extra_ie_len = 0;
3326 return -ENOMEM;
3327 }
3328 memcpy(ifsta->extra_ie, ie, len);
3329 ifsta->extra_ie_len = len;
3330 return 0;
3331}
3332
3333
3334struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3335 struct sk_buff *skb, u8 *bssid,
3336 u8 *addr)
3337{
3338 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3339 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04003340 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07003341 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07003342
3343 /* TODO: Could consider removing the least recently used entry and
3344 * allow new one to be added. */
3345 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3346 if (net_ratelimit()) {
3347 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07003348 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e822007-05-05 11:45:53 -07003349 }
3350 return NULL;
3351 }
3352
Joe Perches0795af52007-10-03 17:59:30 -07003353 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04003354 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e822007-05-05 11:45:53 -07003355
3356 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3357 if (!sta)
3358 return NULL;
3359
3360 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3361
3362 rate_control_rate_init(sta, local);
3363
3364 return sta; /* caller will call sta_info_put() */
3365}
3366
3367
3368int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3369{
3370 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3371 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3372
3373 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3374 dev->name, reason);
3375
3376 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3377 sdata->type != IEEE80211_IF_TYPE_IBSS)
3378 return -EINVAL;
3379
3380 ieee80211_send_deauth(dev, ifsta, reason);
3381 ieee80211_set_disassoc(dev, ifsta, 1);
3382 return 0;
3383}
3384
3385
3386int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3387{
3388 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3389 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3390
3391 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3392 dev->name, reason);
3393
3394 if (sdata->type != IEEE80211_IF_TYPE_STA)
3395 return -EINVAL;
3396
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003397 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07003398 return -1;
3399
3400 ieee80211_send_disassoc(dev, ifsta, reason);
3401 ieee80211_set_disassoc(dev, ifsta, 0);
3402 return 0;
3403}