blob: 3757298c5a602fc67051cf5daf99167511941c23 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
15 * BSS table: use <BSSID,SSID> as the key to support multi-SSID APs
16 * order BSS list by RSSI(?) ("quality of AP")
17 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
18 * SSID)
19 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070020#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070021#include <linux/if_ether.h>
22#include <linux/skbuff.h>
23#include <linux/netdevice.h>
24#include <linux/if_arp.h>
25#include <linux/wireless.h>
26#include <linux/random.h>
27#include <linux/etherdevice.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
33#include "ieee80211_rate.h"
34#include "hostapd_ioctl.h"
35
36#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
37#define IEEE80211_AUTH_MAX_TRIES 3
38#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
39#define IEEE80211_ASSOC_MAX_TRIES 3
40#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
41#define IEEE80211_PROBE_INTERVAL (60 * HZ)
42#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
43#define IEEE80211_SCAN_INTERVAL (2 * HZ)
44#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
45#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
46
47#define IEEE80211_PROBE_DELAY (HZ / 33)
48#define IEEE80211_CHANNEL_TIME (HZ / 33)
49#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
50#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
51#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
52#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
53
54#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
55
56
57#define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
58
59#define ERP_INFO_USE_PROTECTION BIT(1)
60
61static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
62 u8 *ssid, size_t ssid_len);
63static struct ieee80211_sta_bss *
64ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid);
65static void ieee80211_rx_bss_put(struct net_device *dev,
66 struct ieee80211_sta_bss *bss);
67static int ieee80211_sta_find_ibss(struct net_device *dev,
68 struct ieee80211_if_sta *ifsta);
69static int ieee80211_sta_wep_configured(struct net_device *dev);
70static int ieee80211_sta_start_scan(struct net_device *dev,
71 u8 *ssid, size_t ssid_len);
72static int ieee80211_sta_config_auth(struct net_device *dev,
73 struct ieee80211_if_sta *ifsta);
74
75
76/* Parsed Information Elements */
77struct ieee802_11_elems {
Johannes Berg55582352007-07-10 19:32:09 +020078 /* pointers to IEs */
Jiri Bencf0706e82007-05-05 11:45:53 -070079 u8 *ssid;
Jiri Bencf0706e82007-05-05 11:45:53 -070080 u8 *supp_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -070081 u8 *fh_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070082 u8 *ds_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070083 u8 *cf_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070084 u8 *tim;
Jiri Bencf0706e82007-05-05 11:45:53 -070085 u8 *ibss_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070086 u8 *challenge;
Jiri Bencf0706e82007-05-05 11:45:53 -070087 u8 *wpa;
Jiri Bencf0706e82007-05-05 11:45:53 -070088 u8 *rsn;
Jiri Bencf0706e82007-05-05 11:45:53 -070089 u8 *erp_info;
Jiri Bencf0706e82007-05-05 11:45:53 -070090 u8 *ext_supp_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -070091 u8 *wmm_info;
Jiri Bencf0706e82007-05-05 11:45:53 -070092 u8 *wmm_param;
Johannes Berg55582352007-07-10 19:32:09 +020093
94 /* length of them, respectively */
95 u8 ssid_len;
96 u8 supp_rates_len;
97 u8 fh_params_len;
98 u8 ds_params_len;
99 u8 cf_params_len;
100 u8 tim_len;
101 u8 ibss_params_len;
102 u8 challenge_len;
103 u8 wpa_len;
104 u8 rsn_len;
105 u8 erp_info_len;
106 u8 ext_supp_rates_len;
107 u8 wmm_info_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700108 u8 wmm_param_len;
109};
110
111typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
112
113
114static ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
115 struct ieee802_11_elems *elems)
116{
117 size_t left = len;
118 u8 *pos = start;
119 int unknown = 0;
120
121 memset(elems, 0, sizeof(*elems));
122
123 while (left >= 2) {
124 u8 id, elen;
125
126 id = *pos++;
127 elen = *pos++;
128 left -= 2;
129
130 if (elen > left) {
131#if 0
132 if (net_ratelimit())
133 printk(KERN_DEBUG "IEEE 802.11 element parse "
134 "failed (id=%d elen=%d left=%d)\n",
135 id, elen, left);
136#endif
137 return ParseFailed;
138 }
139
140 switch (id) {
141 case WLAN_EID_SSID:
142 elems->ssid = pos;
143 elems->ssid_len = elen;
144 break;
145 case WLAN_EID_SUPP_RATES:
146 elems->supp_rates = pos;
147 elems->supp_rates_len = elen;
148 break;
149 case WLAN_EID_FH_PARAMS:
150 elems->fh_params = pos;
151 elems->fh_params_len = elen;
152 break;
153 case WLAN_EID_DS_PARAMS:
154 elems->ds_params = pos;
155 elems->ds_params_len = elen;
156 break;
157 case WLAN_EID_CF_PARAMS:
158 elems->cf_params = pos;
159 elems->cf_params_len = elen;
160 break;
161 case WLAN_EID_TIM:
162 elems->tim = pos;
163 elems->tim_len = elen;
164 break;
165 case WLAN_EID_IBSS_PARAMS:
166 elems->ibss_params = pos;
167 elems->ibss_params_len = elen;
168 break;
169 case WLAN_EID_CHALLENGE:
170 elems->challenge = pos;
171 elems->challenge_len = elen;
172 break;
173 case WLAN_EID_WPA:
174 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
175 pos[2] == 0xf2) {
176 /* Microsoft OUI (00:50:F2) */
177 if (pos[3] == 1) {
178 /* OUI Type 1 - WPA IE */
179 elems->wpa = pos;
180 elems->wpa_len = elen;
181 } else if (elen >= 5 && pos[3] == 2) {
182 if (pos[4] == 0) {
183 elems->wmm_info = pos;
184 elems->wmm_info_len = elen;
185 } else if (pos[4] == 1) {
186 elems->wmm_param = pos;
187 elems->wmm_param_len = elen;
188 }
189 }
190 }
191 break;
192 case WLAN_EID_RSN:
193 elems->rsn = pos;
194 elems->rsn_len = elen;
195 break;
196 case WLAN_EID_ERP_INFO:
197 elems->erp_info = pos;
198 elems->erp_info_len = elen;
199 break;
200 case WLAN_EID_EXT_SUPP_RATES:
201 elems->ext_supp_rates = pos;
202 elems->ext_supp_rates_len = elen;
203 break;
204 default:
205#if 0
206 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
207 "unknown element (id=%d elen=%d)\n",
208 id, elen);
209#endif
210 unknown++;
211 break;
212 }
213
214 left -= elen;
215 pos += elen;
216 }
217
218 /* Do not trigger error if left == 1 as Apple Airport base stations
219 * send AssocResps that are one spurious byte too long. */
220
221 return unknown ? ParseUnknown : ParseOK;
222}
223
224
225
226
227static int ecw2cw(int ecw)
228{
229 int cw = 1;
230 while (ecw > 0) {
231 cw <<= 1;
232 ecw--;
233 }
234 return cw - 1;
235}
236
237
238static void ieee80211_sta_wmm_params(struct net_device *dev,
239 struct ieee80211_if_sta *ifsta,
240 u8 *wmm_param, size_t wmm_param_len)
241{
242 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
243 struct ieee80211_tx_queue_params params;
244 size_t left;
245 int count;
246 u8 *pos;
247
248 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
249 return;
250 count = wmm_param[6] & 0x0f;
251 if (count == ifsta->wmm_last_param_set)
252 return;
253 ifsta->wmm_last_param_set = count;
254
255 pos = wmm_param + 8;
256 left = wmm_param_len - 8;
257
258 memset(&params, 0, sizeof(params));
259
260 if (!local->ops->conf_tx)
261 return;
262
263 local->wmm_acm = 0;
264 for (; left >= 4; left -= 4, pos += 4) {
265 int aci = (pos[0] >> 5) & 0x03;
266 int acm = (pos[0] >> 4) & 0x01;
267 int queue;
268
269 switch (aci) {
270 case 1:
271 queue = IEEE80211_TX_QUEUE_DATA3;
272 if (acm) {
273 local->wmm_acm |= BIT(0) | BIT(3);
274 }
275 break;
276 case 2:
277 queue = IEEE80211_TX_QUEUE_DATA1;
278 if (acm) {
279 local->wmm_acm |= BIT(4) | BIT(5);
280 }
281 break;
282 case 3:
283 queue = IEEE80211_TX_QUEUE_DATA0;
284 if (acm) {
285 local->wmm_acm |= BIT(6) | BIT(7);
286 }
287 break;
288 case 0:
289 default:
290 queue = IEEE80211_TX_QUEUE_DATA2;
291 if (acm) {
292 local->wmm_acm |= BIT(1) | BIT(2);
293 }
294 break;
295 }
296
297 params.aifs = pos[0] & 0x0f;
298 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
299 params.cw_min = ecw2cw(pos[1] & 0x0f);
300 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
301 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
302 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
303 "cWmin=%d cWmax=%d burst=%d\n",
304 dev->name, queue, aci, acm, params.aifs, params.cw_min,
305 params.cw_max, params.burst_time);
306 /* TODO: handle ACM (block TX, fallback to next lowest allowed
307 * AC for now) */
308 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
309 printk(KERN_DEBUG "%s: failed to set TX queue "
310 "parameters for queue %d\n", dev->name, queue);
311 }
312 }
313}
314
315
Daniel Drake56282212007-07-10 19:32:10 +0200316static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
317{
Daniel Drake56282212007-07-10 19:32:10 +0200318 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
319 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
320 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200321 int preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Daniel Draked9430a32007-07-27 15:43:24 +0200322 u8 changes = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200323
Daniel Drake63fc33c2007-07-10 19:32:11 +0200324 if (use_protection != sdata->use_protection) {
Daniel Drake56282212007-07-10 19:32:10 +0200325 if (net_ratelimit()) {
326 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
327 MAC_FMT ")\n",
328 dev->name,
329 use_protection ? "enabled" : "disabled",
330 MAC_ARG(ifsta->bssid));
331 }
Daniel Drake63fc33c2007-07-10 19:32:11 +0200332 sdata->use_protection = use_protection;
Daniel Draked9430a32007-07-27 15:43:24 +0200333 changes |= IEEE80211_ERP_CHANGE_PROTECTION;
Daniel Drake56282212007-07-10 19:32:10 +0200334 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200335
336 if (!preamble_mode != sdata->short_preamble) {
337 if (net_ratelimit()) {
338 printk(KERN_DEBUG "%s: switched to %s barker preamble"
339 " (BSSID=" MAC_FMT ")\n",
340 dev->name,
341 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
342 "short" : "long",
343 MAC_ARG(ifsta->bssid));
344 }
345 sdata->short_preamble = !preamble_mode;
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
353
Jiri Bencf0706e82007-05-05 11:45:53 -0700354static void ieee80211_sta_send_associnfo(struct net_device *dev,
355 struct ieee80211_if_sta *ifsta)
356{
357 char *buf;
358 size_t len;
359 int i;
360 union iwreq_data wrqu;
361
362 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
363 return;
364
365 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200366 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700367 if (!buf)
368 return;
369
370 len = sprintf(buf, "ASSOCINFO(");
371 if (ifsta->assocreq_ies) {
372 len += sprintf(buf + len, "ReqIEs=");
373 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
374 len += sprintf(buf + len, "%02x",
375 ifsta->assocreq_ies[i]);
376 }
377 }
378 if (ifsta->assocresp_ies) {
379 if (ifsta->assocreq_ies)
380 len += sprintf(buf + len, " ");
381 len += sprintf(buf + len, "RespIEs=");
382 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
383 len += sprintf(buf + len, "%02x",
384 ifsta->assocresp_ies[i]);
385 }
386 }
387 len += sprintf(buf + len, ")");
388
389 if (len > IW_CUSTOM_MAX) {
390 len = sprintf(buf, "ASSOCRESPIE=");
391 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
392 len += sprintf(buf + len, "%02x",
393 ifsta->assocresp_ies[i]);
394 }
395 }
396
397 memset(&wrqu, 0, sizeof(wrqu));
398 wrqu.data.length = len;
399 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
400
401 kfree(buf);
402}
403
404
405static void ieee80211_set_associated(struct net_device *dev,
406 struct ieee80211_if_sta *ifsta, int assoc)
407{
408 union iwreq_data wrqu;
409
410 if (ifsta->associated == assoc)
411 return;
412
413 ifsta->associated = assoc;
414
415 if (assoc) {
416 struct ieee80211_sub_if_data *sdata;
Daniel Drake56282212007-07-10 19:32:10 +0200417 struct ieee80211_sta_bss *bss;
Jiri Bencf0706e82007-05-05 11:45:53 -0700418 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
419 if (sdata->type != IEEE80211_IF_TYPE_STA)
420 return;
Daniel Drake56282212007-07-10 19:32:10 +0200421
422 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
423 if (bss) {
424 if (bss->has_erp_value)
425 ieee80211_handle_erp_ie(dev, bss->erp_value);
426 ieee80211_rx_bss_put(dev, bss);
427 }
428
Jiri Bencf0706e82007-05-05 11:45:53 -0700429 netif_carrier_on(dev);
430 ifsta->prev_bssid_set = 1;
431 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
432 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
433 ieee80211_sta_send_associnfo(dev, ifsta);
434 } else {
435 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200436 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700437 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
438 }
439 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
440 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
441 ifsta->last_probe = jiffies;
442}
443
444static void ieee80211_set_disassoc(struct net_device *dev,
445 struct ieee80211_if_sta *ifsta, int deauth)
446{
447 if (deauth)
448 ifsta->auth_tries = 0;
449 ifsta->assoc_tries = 0;
450 ieee80211_set_associated(dev, ifsta, 0);
451}
452
453static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
454 int encrypt)
455{
456 struct ieee80211_sub_if_data *sdata;
457 struct ieee80211_tx_packet_data *pkt_data;
458
459 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
460 skb->dev = sdata->local->mdev;
461 skb_set_mac_header(skb, 0);
462 skb_set_network_header(skb, 0);
463 skb_set_transport_header(skb, 0);
464
465 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
466 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
467 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400468 if (sdata->type == IEEE80211_IF_TYPE_MGMT)
469 pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
470 if (!encrypt)
471 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700472
473 dev_queue_xmit(skb);
474}
475
476
477static void ieee80211_send_auth(struct net_device *dev,
478 struct ieee80211_if_sta *ifsta,
479 int transaction, u8 *extra, size_t extra_len,
480 int encrypt)
481{
482 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
483 struct sk_buff *skb;
484 struct ieee80211_mgmt *mgmt;
485
486 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
487 sizeof(*mgmt) + 6 + extra_len);
488 if (!skb) {
489 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
490 "frame\n", dev->name);
491 return;
492 }
493 skb_reserve(skb, local->hw.extra_tx_headroom);
494
495 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
496 memset(mgmt, 0, 24 + 6);
497 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
498 IEEE80211_STYPE_AUTH);
499 if (encrypt)
500 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
501 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
502 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
503 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
504 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
505 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
506 ifsta->auth_transaction = transaction + 1;
507 mgmt->u.auth.status_code = cpu_to_le16(0);
508 if (extra)
509 memcpy(skb_put(skb, extra_len), extra, extra_len);
510
511 ieee80211_sta_tx(dev, skb, encrypt);
512}
513
514
515static void ieee80211_authenticate(struct net_device *dev,
516 struct ieee80211_if_sta *ifsta)
517{
518 ifsta->auth_tries++;
519 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
520 printk(KERN_DEBUG "%s: authentication with AP " MAC_FMT
521 " timed out\n",
522 dev->name, MAC_ARG(ifsta->bssid));
523 ifsta->state = IEEE80211_DISABLED;
524 return;
525 }
526
527 ifsta->state = IEEE80211_AUTHENTICATE;
528 printk(KERN_DEBUG "%s: authenticate with AP " MAC_FMT "\n",
529 dev->name, MAC_ARG(ifsta->bssid));
530
531 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
532
533 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
534}
535
536
537static void ieee80211_send_assoc(struct net_device *dev,
538 struct ieee80211_if_sta *ifsta)
539{
540 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
541 struct ieee80211_hw_mode *mode;
542 struct sk_buff *skb;
543 struct ieee80211_mgmt *mgmt;
544 u8 *pos, *ies;
545 int i, len;
546 u16 capab;
547 struct ieee80211_sta_bss *bss;
548 int wmm = 0;
549
550 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
551 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
552 ifsta->ssid_len);
553 if (!skb) {
554 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
555 "frame\n", dev->name);
556 return;
557 }
558 skb_reserve(skb, local->hw.extra_tx_headroom);
559
560 mode = local->oper_hw_mode;
561 capab = ifsta->capab;
562 if (mode->mode == MODE_IEEE80211G) {
563 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
564 WLAN_CAPABILITY_SHORT_PREAMBLE;
565 }
566 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
567 if (bss) {
568 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
569 capab |= WLAN_CAPABILITY_PRIVACY;
570 if (bss->wmm_ie) {
571 wmm = 1;
572 }
573 ieee80211_rx_bss_put(dev, bss);
574 }
575
576 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
577 memset(mgmt, 0, 24);
578 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
579 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
580 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
581
582 if (ifsta->prev_bssid_set) {
583 skb_put(skb, 10);
584 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
585 IEEE80211_STYPE_REASSOC_REQ);
586 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
587 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
588 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
589 ETH_ALEN);
590 } else {
591 skb_put(skb, 4);
592 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
593 IEEE80211_STYPE_ASSOC_REQ);
594 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
595 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
596 }
597
598 /* SSID */
599 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
600 *pos++ = WLAN_EID_SSID;
601 *pos++ = ifsta->ssid_len;
602 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
603
604 len = mode->num_rates;
605 if (len > 8)
606 len = 8;
607 pos = skb_put(skb, len + 2);
608 *pos++ = WLAN_EID_SUPP_RATES;
609 *pos++ = len;
610 for (i = 0; i < len; i++) {
611 int rate = mode->rates[i].rate;
612 if (mode->mode == MODE_ATHEROS_TURBO)
613 rate /= 2;
614 *pos++ = (u8) (rate / 5);
615 }
616
617 if (mode->num_rates > len) {
618 pos = skb_put(skb, mode->num_rates - len + 2);
619 *pos++ = WLAN_EID_EXT_SUPP_RATES;
620 *pos++ = mode->num_rates - len;
621 for (i = len; i < mode->num_rates; i++) {
622 int rate = mode->rates[i].rate;
623 if (mode->mode == MODE_ATHEROS_TURBO)
624 rate /= 2;
625 *pos++ = (u8) (rate / 5);
626 }
627 }
628
629 if (ifsta->extra_ie) {
630 pos = skb_put(skb, ifsta->extra_ie_len);
631 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
632 }
633
634 if (wmm && ifsta->wmm_enabled) {
635 pos = skb_put(skb, 9);
636 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
637 *pos++ = 7; /* len */
638 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
639 *pos++ = 0x50;
640 *pos++ = 0xf2;
641 *pos++ = 2; /* WME */
642 *pos++ = 0; /* WME info */
643 *pos++ = 1; /* WME ver */
644 *pos++ = 0;
645 }
646
647 kfree(ifsta->assocreq_ies);
648 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200649 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700650 if (ifsta->assocreq_ies)
651 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
652
653 ieee80211_sta_tx(dev, skb, 0);
654}
655
656
657static void ieee80211_send_deauth(struct net_device *dev,
658 struct ieee80211_if_sta *ifsta, u16 reason)
659{
660 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
661 struct sk_buff *skb;
662 struct ieee80211_mgmt *mgmt;
663
664 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
665 if (!skb) {
666 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
667 "frame\n", dev->name);
668 return;
669 }
670 skb_reserve(skb, local->hw.extra_tx_headroom);
671
672 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
673 memset(mgmt, 0, 24);
674 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
675 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
676 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
677 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
678 IEEE80211_STYPE_DEAUTH);
679 skb_put(skb, 2);
680 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
681
682 ieee80211_sta_tx(dev, skb, 0);
683}
684
685
686static void ieee80211_send_disassoc(struct net_device *dev,
687 struct ieee80211_if_sta *ifsta, u16 reason)
688{
689 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
690 struct sk_buff *skb;
691 struct ieee80211_mgmt *mgmt;
692
693 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
694 if (!skb) {
695 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
696 "frame\n", dev->name);
697 return;
698 }
699 skb_reserve(skb, local->hw.extra_tx_headroom);
700
701 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
702 memset(mgmt, 0, 24);
703 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
704 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
705 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
706 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
707 IEEE80211_STYPE_DISASSOC);
708 skb_put(skb, 2);
709 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
710
711 ieee80211_sta_tx(dev, skb, 0);
712}
713
714
715static int ieee80211_privacy_mismatch(struct net_device *dev,
716 struct ieee80211_if_sta *ifsta)
717{
718 struct ieee80211_sta_bss *bss;
719 int res = 0;
720
Johannes Berg808718c2007-08-28 17:01:53 -0400721 if (!ifsta || ifsta->mixed_cell || ifsta->key_management_enabled)
Jiri Bencf0706e82007-05-05 11:45:53 -0700722 return 0;
723
724 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
725 if (!bss)
726 return 0;
727
728 if (ieee80211_sta_wep_configured(dev) !=
729 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
730 res = 1;
731
732 ieee80211_rx_bss_put(dev, bss);
733
734 return res;
735}
736
737
738static void ieee80211_associate(struct net_device *dev,
739 struct ieee80211_if_sta *ifsta)
740{
741 ifsta->assoc_tries++;
742 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
743 printk(KERN_DEBUG "%s: association with AP " MAC_FMT
744 " timed out\n",
745 dev->name, MAC_ARG(ifsta->bssid));
746 ifsta->state = IEEE80211_DISABLED;
747 return;
748 }
749
750 ifsta->state = IEEE80211_ASSOCIATE;
751 printk(KERN_DEBUG "%s: associate with AP " MAC_FMT "\n",
752 dev->name, MAC_ARG(ifsta->bssid));
753 if (ieee80211_privacy_mismatch(dev, ifsta)) {
754 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
755 "mixed-cell disabled - abort association\n", dev->name);
756 ifsta->state = IEEE80211_DISABLED;
757 return;
758 }
759
760 ieee80211_send_assoc(dev, ifsta);
761
762 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
763}
764
765
766static void ieee80211_associated(struct net_device *dev,
767 struct ieee80211_if_sta *ifsta)
768{
769 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
770 struct sta_info *sta;
771 int disassoc;
772
773 /* TODO: start monitoring current AP signal quality and number of
774 * missed beacons. Scan other channels every now and then and search
775 * for better APs. */
776 /* TODO: remove expired BSSes */
777
778 ifsta->state = IEEE80211_ASSOCIATED;
779
780 sta = sta_info_get(local, ifsta->bssid);
781 if (!sta) {
782 printk(KERN_DEBUG "%s: No STA entry for own AP " MAC_FMT "\n",
783 dev->name, MAC_ARG(ifsta->bssid));
784 disassoc = 1;
785 } else {
786 disassoc = 0;
787 if (time_after(jiffies,
788 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
789 if (ifsta->probereq_poll) {
790 printk(KERN_DEBUG "%s: No ProbeResp from "
791 "current AP " MAC_FMT " - assume out of "
792 "range\n",
793 dev->name, MAC_ARG(ifsta->bssid));
794 disassoc = 1;
Michael Wube8755e2007-07-27 15:43:23 +0200795 sta_info_free(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700796 ifsta->probereq_poll = 0;
797 } else {
798 ieee80211_send_probe_req(dev, ifsta->bssid,
799 local->scan_ssid,
800 local->scan_ssid_len);
801 ifsta->probereq_poll = 1;
802 }
803 } else {
804 ifsta->probereq_poll = 0;
805 if (time_after(jiffies, ifsta->last_probe +
806 IEEE80211_PROBE_INTERVAL)) {
807 ifsta->last_probe = jiffies;
808 ieee80211_send_probe_req(dev, ifsta->bssid,
809 ifsta->ssid,
810 ifsta->ssid_len);
811 }
812 }
813 sta_info_put(sta);
814 }
815 if (disassoc) {
816 union iwreq_data wrqu;
817 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
818 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
819 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
820 mod_timer(&ifsta->timer, jiffies +
821 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
822 } else {
823 mod_timer(&ifsta->timer, jiffies +
824 IEEE80211_MONITORING_INTERVAL);
825 }
826}
827
828
829static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
830 u8 *ssid, size_t ssid_len)
831{
832 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
833 struct ieee80211_hw_mode *mode;
834 struct sk_buff *skb;
835 struct ieee80211_mgmt *mgmt;
836 u8 *pos, *supp_rates, *esupp_rates = NULL;
837 int i;
838
839 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
840 if (!skb) {
841 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
842 "request\n", dev->name);
843 return;
844 }
845 skb_reserve(skb, local->hw.extra_tx_headroom);
846
847 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
848 memset(mgmt, 0, 24);
849 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
850 IEEE80211_STYPE_PROBE_REQ);
851 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
852 if (dst) {
853 memcpy(mgmt->da, dst, ETH_ALEN);
854 memcpy(mgmt->bssid, dst, ETH_ALEN);
855 } else {
856 memset(mgmt->da, 0xff, ETH_ALEN);
857 memset(mgmt->bssid, 0xff, ETH_ALEN);
858 }
859 pos = skb_put(skb, 2 + ssid_len);
860 *pos++ = WLAN_EID_SSID;
861 *pos++ = ssid_len;
862 memcpy(pos, ssid, ssid_len);
863
864 supp_rates = skb_put(skb, 2);
865 supp_rates[0] = WLAN_EID_SUPP_RATES;
866 supp_rates[1] = 0;
867 mode = local->oper_hw_mode;
868 for (i = 0; i < mode->num_rates; i++) {
869 struct ieee80211_rate *rate = &mode->rates[i];
870 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
871 continue;
872 if (esupp_rates) {
873 pos = skb_put(skb, 1);
874 esupp_rates[1]++;
875 } else if (supp_rates[1] == 8) {
876 esupp_rates = skb_put(skb, 3);
877 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
878 esupp_rates[1] = 1;
879 pos = &esupp_rates[2];
880 } else {
881 pos = skb_put(skb, 1);
882 supp_rates[1]++;
883 }
884 if (mode->mode == MODE_ATHEROS_TURBO)
885 *pos = rate->rate / 10;
886 else
887 *pos = rate->rate / 5;
888 }
889
890 ieee80211_sta_tx(dev, skb, 0);
891}
892
893
894static int ieee80211_sta_wep_configured(struct net_device *dev)
895{
896 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
897 if (!sdata || !sdata->default_key ||
898 sdata->default_key->alg != ALG_WEP)
899 return 0;
900 return 1;
901}
902
903
904static void ieee80211_auth_completed(struct net_device *dev,
905 struct ieee80211_if_sta *ifsta)
906{
907 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
908 ifsta->authenticated = 1;
909 ieee80211_associate(dev, ifsta);
910}
911
912
913static void ieee80211_auth_challenge(struct net_device *dev,
914 struct ieee80211_if_sta *ifsta,
915 struct ieee80211_mgmt *mgmt,
916 size_t len)
917{
918 u8 *pos;
919 struct ieee802_11_elems elems;
920
921 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
922 pos = mgmt->u.auth.variable;
923 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
924 == ParseFailed) {
925 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
926 dev->name);
927 return;
928 }
929 if (!elems.challenge) {
930 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
931 "frame\n", dev->name);
932 return;
933 }
934 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
935 elems.challenge_len + 2, 1);
936}
937
938
939static void ieee80211_rx_mgmt_auth(struct net_device *dev,
940 struct ieee80211_if_sta *ifsta,
941 struct ieee80211_mgmt *mgmt,
942 size_t len)
943{
944 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
945 u16 auth_alg, auth_transaction, status_code;
946
947 if (ifsta->state != IEEE80211_AUTHENTICATE &&
948 sdata->type != IEEE80211_IF_TYPE_IBSS) {
949 printk(KERN_DEBUG "%s: authentication frame received from "
950 MAC_FMT ", but not in authenticate state - ignored\n",
951 dev->name, MAC_ARG(mgmt->sa));
952 return;
953 }
954
955 if (len < 24 + 6) {
956 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
957 "received from " MAC_FMT " - ignored\n",
958 dev->name, len, MAC_ARG(mgmt->sa));
959 return;
960 }
961
962 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
963 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
964 printk(KERN_DEBUG "%s: authentication frame received from "
965 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
966 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
967 MAC_ARG(mgmt->bssid));
968 return;
969 }
970
971 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
972 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
973 printk(KERN_DEBUG "%s: authentication frame received from "
974 "unknown BSSID (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
975 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
976 MAC_ARG(mgmt->bssid));
977 return;
978 }
979
980 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
981 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
982 status_code = le16_to_cpu(mgmt->u.auth.status_code);
983
984 printk(KERN_DEBUG "%s: RX authentication from " MAC_FMT " (alg=%d "
985 "transaction=%d status=%d)\n",
986 dev->name, MAC_ARG(mgmt->sa), auth_alg,
987 auth_transaction, status_code);
988
989 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
990 /* IEEE 802.11 standard does not require authentication in IBSS
991 * networks and most implementations do not seem to use it.
992 * However, try to reply to authentication attempts if someone
993 * has actually implemented this.
994 * TODO: Could implement shared key authentication. */
995 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
996 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
997 "frame (alg=%d transaction=%d)\n",
998 dev->name, auth_alg, auth_transaction);
999 return;
1000 }
1001 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1002 }
1003
1004 if (auth_alg != ifsta->auth_alg ||
1005 auth_transaction != ifsta->auth_transaction) {
1006 printk(KERN_DEBUG "%s: unexpected authentication frame "
1007 "(alg=%d transaction=%d)\n",
1008 dev->name, auth_alg, auth_transaction);
1009 return;
1010 }
1011
1012 if (status_code != WLAN_STATUS_SUCCESS) {
1013 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1014 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1015 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1016 u8 algs[3];
1017 const int num_algs = ARRAY_SIZE(algs);
1018 int i, pos;
1019 algs[0] = algs[1] = algs[2] = 0xff;
1020 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1021 algs[0] = WLAN_AUTH_OPEN;
1022 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1023 algs[1] = WLAN_AUTH_SHARED_KEY;
1024 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1025 algs[2] = WLAN_AUTH_LEAP;
1026 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1027 pos = 0;
1028 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1029 pos = 1;
1030 else
1031 pos = 2;
1032 for (i = 0; i < num_algs; i++) {
1033 pos++;
1034 if (pos >= num_algs)
1035 pos = 0;
1036 if (algs[pos] == ifsta->auth_alg ||
1037 algs[pos] == 0xff)
1038 continue;
1039 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1040 !ieee80211_sta_wep_configured(dev))
1041 continue;
1042 ifsta->auth_alg = algs[pos];
1043 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1044 "next try\n",
1045 dev->name, ifsta->auth_alg);
1046 break;
1047 }
1048 }
1049 return;
1050 }
1051
1052 switch (ifsta->auth_alg) {
1053 case WLAN_AUTH_OPEN:
1054 case WLAN_AUTH_LEAP:
1055 ieee80211_auth_completed(dev, ifsta);
1056 break;
1057 case WLAN_AUTH_SHARED_KEY:
1058 if (ifsta->auth_transaction == 4)
1059 ieee80211_auth_completed(dev, ifsta);
1060 else
1061 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1062 break;
1063 }
1064}
1065
1066
1067static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1068 struct ieee80211_if_sta *ifsta,
1069 struct ieee80211_mgmt *mgmt,
1070 size_t len)
1071{
1072 u16 reason_code;
1073
1074 if (len < 24 + 2) {
1075 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1076 "received from " MAC_FMT " - ignored\n",
1077 dev->name, len, MAC_ARG(mgmt->sa));
1078 return;
1079 }
1080
1081 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1082 printk(KERN_DEBUG "%s: deauthentication frame received from "
1083 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1084 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1085 MAC_ARG(mgmt->bssid));
1086 return;
1087 }
1088
1089 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1090
1091 printk(KERN_DEBUG "%s: RX deauthentication from " MAC_FMT
1092 " (reason=%d)\n",
1093 dev->name, MAC_ARG(mgmt->sa), reason_code);
1094
1095 if (ifsta->authenticated) {
1096 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1097 }
1098
1099 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1100 ifsta->state == IEEE80211_ASSOCIATE ||
1101 ifsta->state == IEEE80211_ASSOCIATED) {
1102 ifsta->state = IEEE80211_AUTHENTICATE;
1103 mod_timer(&ifsta->timer, jiffies +
1104 IEEE80211_RETRY_AUTH_INTERVAL);
1105 }
1106
1107 ieee80211_set_disassoc(dev, ifsta, 1);
1108 ifsta->authenticated = 0;
1109}
1110
1111
1112static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1113 struct ieee80211_if_sta *ifsta,
1114 struct ieee80211_mgmt *mgmt,
1115 size_t len)
1116{
1117 u16 reason_code;
1118
1119 if (len < 24 + 2) {
1120 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1121 "received from " MAC_FMT " - ignored\n",
1122 dev->name, len, MAC_ARG(mgmt->sa));
1123 return;
1124 }
1125
1126 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1127 printk(KERN_DEBUG "%s: disassociation frame received from "
1128 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1129 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1130 MAC_ARG(mgmt->bssid));
1131 return;
1132 }
1133
1134 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1135
1136 printk(KERN_DEBUG "%s: RX disassociation from " MAC_FMT
1137 " (reason=%d)\n",
1138 dev->name, MAC_ARG(mgmt->sa), reason_code);
1139
1140 if (ifsta->associated)
1141 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1142
1143 if (ifsta->state == IEEE80211_ASSOCIATED) {
1144 ifsta->state = IEEE80211_ASSOCIATE;
1145 mod_timer(&ifsta->timer, jiffies +
1146 IEEE80211_RETRY_AUTH_INTERVAL);
1147 }
1148
1149 ieee80211_set_disassoc(dev, ifsta, 0);
1150}
1151
1152
1153static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1154 struct ieee80211_if_sta *ifsta,
1155 struct ieee80211_mgmt *mgmt,
1156 size_t len,
1157 int reassoc)
1158{
1159 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1160 struct ieee80211_hw_mode *mode;
1161 struct sta_info *sta;
1162 u32 rates;
1163 u16 capab_info, status_code, aid;
1164 struct ieee802_11_elems elems;
1165 u8 *pos;
1166 int i, j;
1167
1168 /* AssocResp and ReassocResp have identical structure, so process both
1169 * of them in this function. */
1170
1171 if (ifsta->state != IEEE80211_ASSOCIATE) {
1172 printk(KERN_DEBUG "%s: association frame received from "
1173 MAC_FMT ", but not in associate state - ignored\n",
1174 dev->name, MAC_ARG(mgmt->sa));
1175 return;
1176 }
1177
1178 if (len < 24 + 6) {
1179 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1180 "received from " MAC_FMT " - ignored\n",
1181 dev->name, len, MAC_ARG(mgmt->sa));
1182 return;
1183 }
1184
1185 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1186 printk(KERN_DEBUG "%s: association frame received from "
1187 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1188 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1189 MAC_ARG(mgmt->bssid));
1190 return;
1191 }
1192
1193 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1194 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1195 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1196 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1197 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1198 "set\n", dev->name, aid);
1199 aid &= ~(BIT(15) | BIT(14));
1200
1201 printk(KERN_DEBUG "%s: RX %sssocResp from " MAC_FMT " (capab=0x%x "
1202 "status=%d aid=%d)\n",
1203 dev->name, reassoc ? "Rea" : "A", MAC_ARG(mgmt->sa),
1204 capab_info, status_code, aid);
1205
1206 if (status_code != WLAN_STATUS_SUCCESS) {
1207 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1208 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001209 /* if this was a reassociation, ensure we try a "full"
1210 * association next time. This works around some broken APs
1211 * which do not correctly reject reassociation requests. */
1212 ifsta->prev_bssid_set = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001213 return;
1214 }
1215
1216 pos = mgmt->u.assoc_resp.variable;
1217 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1218 == ParseFailed) {
1219 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1220 dev->name);
1221 return;
1222 }
1223
1224 if (!elems.supp_rates) {
1225 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1226 dev->name);
1227 return;
1228 }
1229
Daniel Drake56282212007-07-10 19:32:10 +02001230 /* it probably doesn't, but if the frame includes an ERP value then
1231 * update our stored copy */
1232 if (elems.erp_info && elems.erp_info_len >= 1) {
1233 struct ieee80211_sta_bss *bss
1234 = ieee80211_rx_bss_get(dev, ifsta->bssid);
1235 if (bss) {
1236 bss->erp_value = elems.erp_info[0];
1237 bss->has_erp_value = 1;
1238 ieee80211_rx_bss_put(dev, bss);
1239 }
1240 }
1241
Jiri Bencf0706e82007-05-05 11:45:53 -07001242 printk(KERN_DEBUG "%s: associated\n", dev->name);
1243 ifsta->aid = aid;
1244 ifsta->ap_capab = capab_info;
1245
1246 kfree(ifsta->assocresp_ies);
1247 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001248 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001249 if (ifsta->assocresp_ies)
1250 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1251
1252 ieee80211_set_associated(dev, ifsta, 1);
1253
1254 /* Add STA entry for the AP */
1255 sta = sta_info_get(local, ifsta->bssid);
1256 if (!sta) {
1257 struct ieee80211_sta_bss *bss;
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001258 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001259 if (!sta) {
1260 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1261 " AP\n", dev->name);
1262 return;
1263 }
1264 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1265 if (bss) {
1266 sta->last_rssi = bss->rssi;
1267 sta->last_signal = bss->signal;
1268 sta->last_noise = bss->noise;
1269 ieee80211_rx_bss_put(dev, bss);
1270 }
1271 }
1272
1273 sta->dev = dev;
1274 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
1275 sta->assoc_ap = 1;
1276
1277 rates = 0;
1278 mode = local->oper_hw_mode;
1279 for (i = 0; i < elems.supp_rates_len; i++) {
1280 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1281 if (mode->mode == MODE_ATHEROS_TURBO)
1282 rate *= 2;
1283 for (j = 0; j < mode->num_rates; j++)
1284 if (mode->rates[j].rate == rate)
1285 rates |= BIT(j);
1286 }
1287 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1288 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1289 if (mode->mode == MODE_ATHEROS_TURBO)
1290 rate *= 2;
1291 for (j = 0; j < mode->num_rates; j++)
1292 if (mode->rates[j].rate == rate)
1293 rates |= BIT(j);
1294 }
1295 sta->supp_rates = rates;
1296
1297 rate_control_rate_init(sta, local);
1298
1299 if (elems.wmm_param && ifsta->wmm_enabled) {
1300 sta->flags |= WLAN_STA_WME;
1301 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1302 elems.wmm_param_len);
1303 }
1304
1305
1306 sta_info_put(sta);
1307
1308 ieee80211_associated(dev, ifsta);
1309}
1310
1311
1312/* Caller must hold local->sta_bss_lock */
1313static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1314 struct ieee80211_sta_bss *bss)
1315{
1316 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1317 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1318 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1319}
1320
1321
1322/* Caller must hold local->sta_bss_lock */
1323static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1324 struct ieee80211_sta_bss *bss)
1325{
1326 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1327 struct ieee80211_sta_bss *b, *prev = NULL;
1328 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1329 while (b) {
1330 if (b == bss) {
1331 if (!prev)
1332 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1333 bss->hnext;
1334 else
1335 prev->hnext = bss->hnext;
1336 break;
1337 }
1338 prev = b;
1339 b = b->hnext;
1340 }
1341}
1342
1343
1344static struct ieee80211_sta_bss *
1345ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1346{
1347 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1348 struct ieee80211_sta_bss *bss;
1349
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001350 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07001351 if (!bss)
1352 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001353 atomic_inc(&bss->users);
1354 atomic_inc(&bss->users);
1355 memcpy(bss->bssid, bssid, ETH_ALEN);
1356
1357 spin_lock_bh(&local->sta_bss_lock);
1358 /* TODO: order by RSSI? */
1359 list_add_tail(&bss->list, &local->sta_bss_list);
1360 __ieee80211_rx_bss_hash_add(dev, bss);
1361 spin_unlock_bh(&local->sta_bss_lock);
1362 return bss;
1363}
1364
1365
1366static struct ieee80211_sta_bss *
1367ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1368{
1369 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1370 struct ieee80211_sta_bss *bss;
1371
1372 spin_lock_bh(&local->sta_bss_lock);
1373 bss = local->sta_bss_hash[STA_HASH(bssid)];
1374 while (bss) {
1375 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1376 atomic_inc(&bss->users);
1377 break;
1378 }
1379 bss = bss->hnext;
1380 }
1381 spin_unlock_bh(&local->sta_bss_lock);
1382 return bss;
1383}
1384
1385
1386static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1387{
1388 kfree(bss->wpa_ie);
1389 kfree(bss->rsn_ie);
1390 kfree(bss->wmm_ie);
1391 kfree(bss);
1392}
1393
1394
1395static void ieee80211_rx_bss_put(struct net_device *dev,
1396 struct ieee80211_sta_bss *bss)
1397{
1398 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1399 if (!atomic_dec_and_test(&bss->users))
1400 return;
1401
1402 spin_lock_bh(&local->sta_bss_lock);
1403 __ieee80211_rx_bss_hash_del(dev, bss);
1404 list_del(&bss->list);
1405 spin_unlock_bh(&local->sta_bss_lock);
1406 ieee80211_rx_bss_free(bss);
1407}
1408
1409
1410void ieee80211_rx_bss_list_init(struct net_device *dev)
1411{
1412 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1413 spin_lock_init(&local->sta_bss_lock);
1414 INIT_LIST_HEAD(&local->sta_bss_list);
1415}
1416
1417
1418void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1419{
1420 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1421 struct ieee80211_sta_bss *bss, *tmp;
1422
1423 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1424 ieee80211_rx_bss_put(dev, bss);
1425}
1426
1427
1428static void ieee80211_rx_bss_info(struct net_device *dev,
1429 struct ieee80211_mgmt *mgmt,
1430 size_t len,
1431 struct ieee80211_rx_status *rx_status,
1432 int beacon)
1433{
1434 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1435 struct ieee802_11_elems elems;
1436 size_t baselen;
1437 int channel, invalid = 0, clen;
1438 struct ieee80211_sta_bss *bss;
1439 struct sta_info *sta;
1440 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1441 u64 timestamp;
1442
1443 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1444 return; /* ignore ProbeResp to foreign address */
1445
1446#if 0
1447 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1448 dev->name, beacon ? "Beacon" : "Probe Response",
1449 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1450#endif
1451
1452 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1453 if (baselen > len)
1454 return;
1455
1456 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1457
1458 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1459 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1460#ifdef CONFIG_MAC80211_IBSS_DEBUG
1461 static unsigned long last_tsf_debug = 0;
1462 u64 tsf;
1463 if (local->ops->get_tsf)
1464 tsf = local->ops->get_tsf(local_to_hw(local));
1465 else
1466 tsf = -1LLU;
1467 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1468 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1469 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1470 "@%lu\n",
1471 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1472 (unsigned long long)tsf,
1473 (unsigned long long)timestamp,
1474 (unsigned long long)(tsf - timestamp),
1475 jiffies);
1476 last_tsf_debug = jiffies;
1477 }
1478#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1479 }
1480
1481 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1482 &elems) == ParseFailed)
1483 invalid = 1;
1484
1485 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1486 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1487 (sta = sta_info_get(local, mgmt->sa))) {
1488 struct ieee80211_hw_mode *mode;
1489 struct ieee80211_rate *rates;
1490 size_t num_rates;
1491 u32 supp_rates, prev_rates;
1492 int i, j;
1493
1494 mode = local->sta_scanning ?
1495 local->scan_hw_mode : local->oper_hw_mode;
1496 rates = mode->rates;
1497 num_rates = mode->num_rates;
1498
1499 supp_rates = 0;
1500 for (i = 0; i < elems.supp_rates_len +
1501 elems.ext_supp_rates_len; i++) {
1502 u8 rate = 0;
1503 int own_rate;
1504 if (i < elems.supp_rates_len)
1505 rate = elems.supp_rates[i];
1506 else if (elems.ext_supp_rates)
1507 rate = elems.ext_supp_rates
1508 [i - elems.supp_rates_len];
1509 own_rate = 5 * (rate & 0x7f);
1510 if (mode->mode == MODE_ATHEROS_TURBO)
1511 own_rate *= 2;
1512 for (j = 0; j < num_rates; j++)
1513 if (rates[j].rate == own_rate)
1514 supp_rates |= BIT(j);
1515 }
1516
1517 prev_rates = sta->supp_rates;
1518 sta->supp_rates &= supp_rates;
1519 if (sta->supp_rates == 0) {
1520 /* No matching rates - this should not really happen.
1521 * Make sure that at least one rate is marked
1522 * supported to avoid issues with TX rate ctrl. */
1523 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1524 }
1525 if (sta->supp_rates != prev_rates) {
1526 printk(KERN_DEBUG "%s: updated supp_rates set for "
1527 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1528 "0x%x)\n",
1529 dev->name, MAC_ARG(sta->addr), prev_rates,
1530 supp_rates, sta->supp_rates);
1531 }
1532 sta_info_put(sta);
1533 }
1534
1535 if (!elems.ssid)
1536 return;
1537
1538 if (elems.ds_params && elems.ds_params_len == 1)
1539 channel = elems.ds_params[0];
1540 else
1541 channel = rx_status->channel;
1542
1543 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1544 if (!bss) {
1545 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1546 if (!bss)
1547 return;
1548 } else {
1549#if 0
1550 /* TODO: order by RSSI? */
1551 spin_lock_bh(&local->sta_bss_lock);
1552 list_move_tail(&bss->list, &local->sta_bss_list);
1553 spin_unlock_bh(&local->sta_bss_lock);
1554#endif
1555 }
1556
1557 if (bss->probe_resp && beacon) {
1558 /* Do not allow beacon to override data from Probe Response. */
1559 ieee80211_rx_bss_put(dev, bss);
1560 return;
1561 }
1562
Daniel Drake56282212007-07-10 19:32:10 +02001563 /* save the ERP value so that it is available at association time */
1564 if (elems.erp_info && elems.erp_info_len >= 1) {
1565 bss->erp_value = elems.erp_info[0];
1566 bss->has_erp_value = 1;
1567 }
1568
Jiri Bencf0706e82007-05-05 11:45:53 -07001569 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1570 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1571 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1572 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1573 bss->ssid_len = elems.ssid_len;
1574 }
1575
1576 bss->supp_rates_len = 0;
1577 if (elems.supp_rates) {
1578 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1579 if (clen > elems.supp_rates_len)
1580 clen = elems.supp_rates_len;
1581 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1582 clen);
1583 bss->supp_rates_len += clen;
1584 }
1585 if (elems.ext_supp_rates) {
1586 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1587 if (clen > elems.ext_supp_rates_len)
1588 clen = elems.ext_supp_rates_len;
1589 memcpy(&bss->supp_rates[bss->supp_rates_len],
1590 elems.ext_supp_rates, clen);
1591 bss->supp_rates_len += clen;
1592 }
1593
1594 if (elems.wpa &&
1595 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1596 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1597 kfree(bss->wpa_ie);
1598 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1599 if (bss->wpa_ie) {
1600 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1601 bss->wpa_ie_len = elems.wpa_len + 2;
1602 } else
1603 bss->wpa_ie_len = 0;
1604 } else if (!elems.wpa && bss->wpa_ie) {
1605 kfree(bss->wpa_ie);
1606 bss->wpa_ie = NULL;
1607 bss->wpa_ie_len = 0;
1608 }
1609
1610 if (elems.rsn &&
1611 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1612 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1613 kfree(bss->rsn_ie);
1614 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1615 if (bss->rsn_ie) {
1616 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1617 bss->rsn_ie_len = elems.rsn_len + 2;
1618 } else
1619 bss->rsn_ie_len = 0;
1620 } else if (!elems.rsn && bss->rsn_ie) {
1621 kfree(bss->rsn_ie);
1622 bss->rsn_ie = NULL;
1623 bss->rsn_ie_len = 0;
1624 }
1625
1626 if (elems.wmm_param &&
1627 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1628 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1629 kfree(bss->wmm_ie);
1630 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1631 if (bss->wmm_ie) {
1632 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1633 elems.wmm_param_len + 2);
1634 bss->wmm_ie_len = elems.wmm_param_len + 2;
1635 } else
1636 bss->wmm_ie_len = 0;
1637 } else if (!elems.wmm_param && bss->wmm_ie) {
1638 kfree(bss->wmm_ie);
1639 bss->wmm_ie = NULL;
1640 bss->wmm_ie_len = 0;
1641 }
1642
1643
1644 bss->hw_mode = rx_status->phymode;
1645 bss->channel = channel;
1646 bss->freq = rx_status->freq;
1647 if (channel != rx_status->channel &&
1648 (bss->hw_mode == MODE_IEEE80211G ||
1649 bss->hw_mode == MODE_IEEE80211B) &&
1650 channel >= 1 && channel <= 14) {
1651 static const int freq_list[] = {
1652 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1653 2447, 2452, 2457, 2462, 2467, 2472, 2484
1654 };
1655 /* IEEE 802.11g/b mode can receive packets from neighboring
1656 * channels, so map the channel into frequency. */
1657 bss->freq = freq_list[channel - 1];
1658 }
1659 bss->timestamp = timestamp;
1660 bss->last_update = jiffies;
1661 bss->rssi = rx_status->ssi;
1662 bss->signal = rx_status->signal;
1663 bss->noise = rx_status->noise;
1664 if (!beacon)
1665 bss->probe_resp++;
1666 ieee80211_rx_bss_put(dev, bss);
1667}
1668
1669
1670static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1671 struct ieee80211_mgmt *mgmt,
1672 size_t len,
1673 struct ieee80211_rx_status *rx_status)
1674{
1675 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1676}
1677
1678
1679static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1680 struct ieee80211_mgmt *mgmt,
1681 size_t len,
1682 struct ieee80211_rx_status *rx_status)
1683{
Jiri Bencf0706e82007-05-05 11:45:53 -07001684 struct ieee80211_sub_if_data *sdata;
1685 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07001686 size_t baselen;
1687 struct ieee802_11_elems elems;
1688
1689 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1690
1691 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1692 if (sdata->type != IEEE80211_IF_TYPE_STA)
1693 return;
1694 ifsta = &sdata->u.sta;
1695
1696 if (!ifsta->associated ||
1697 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1698 return;
1699
1700 /* Process beacon from the current BSS */
1701 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1702 if (baselen > len)
1703 return;
1704
1705 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1706 &elems) == ParseFailed)
1707 return;
1708
Daniel Drake56282212007-07-10 19:32:10 +02001709 if (elems.erp_info && elems.erp_info_len >= 1)
1710 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07001711
1712 if (elems.wmm_param && ifsta->wmm_enabled) {
1713 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1714 elems.wmm_param_len);
1715 }
1716}
1717
1718
1719static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1720 struct ieee80211_if_sta *ifsta,
1721 struct ieee80211_mgmt *mgmt,
1722 size_t len,
1723 struct ieee80211_rx_status *rx_status)
1724{
1725 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1726 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1727 int tx_last_beacon;
1728 struct sk_buff *skb;
1729 struct ieee80211_mgmt *resp;
1730 u8 *pos, *end;
1731
1732 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1733 ifsta->state != IEEE80211_IBSS_JOINED ||
1734 len < 24 + 2 || !ifsta->probe_resp)
1735 return;
1736
1737 if (local->ops->tx_last_beacon)
1738 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1739 else
1740 tx_last_beacon = 1;
1741
1742#ifdef CONFIG_MAC80211_IBSS_DEBUG
1743 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1744 MAC_FMT " (tx_last_beacon=%d)\n",
1745 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1746 MAC_ARG(mgmt->bssid), tx_last_beacon);
1747#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1748
1749 if (!tx_last_beacon)
1750 return;
1751
1752 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1753 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1754 return;
1755
1756 end = ((u8 *) mgmt) + len;
1757 pos = mgmt->u.probe_req.variable;
1758 if (pos[0] != WLAN_EID_SSID ||
1759 pos + 2 + pos[1] > end) {
1760 if (net_ratelimit()) {
1761 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1762 "from " MAC_FMT "\n",
1763 dev->name, MAC_ARG(mgmt->sa));
1764 }
1765 return;
1766 }
1767 if (pos[1] != 0 &&
1768 (pos[1] != ifsta->ssid_len ||
1769 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1770 /* Ignore ProbeReq for foreign SSID */
1771 return;
1772 }
1773
1774 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001775 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001776 if (!skb)
1777 return;
1778
1779 resp = (struct ieee80211_mgmt *) skb->data;
1780 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1781#ifdef CONFIG_MAC80211_IBSS_DEBUG
1782 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1783 dev->name, MAC_ARG(resp->da));
1784#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1785 ieee80211_sta_tx(dev, skb, 0);
1786}
1787
1788
1789void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1790 struct ieee80211_rx_status *rx_status)
1791{
1792 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1793 struct ieee80211_sub_if_data *sdata;
1794 struct ieee80211_if_sta *ifsta;
1795 struct ieee80211_mgmt *mgmt;
1796 u16 fc;
1797
1798 if (skb->len < 24)
1799 goto fail;
1800
1801 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1802 ifsta = &sdata->u.sta;
1803
1804 mgmt = (struct ieee80211_mgmt *) skb->data;
1805 fc = le16_to_cpu(mgmt->frame_control);
1806
1807 switch (fc & IEEE80211_FCTL_STYPE) {
1808 case IEEE80211_STYPE_PROBE_REQ:
1809 case IEEE80211_STYPE_PROBE_RESP:
1810 case IEEE80211_STYPE_BEACON:
1811 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1812 case IEEE80211_STYPE_AUTH:
1813 case IEEE80211_STYPE_ASSOC_RESP:
1814 case IEEE80211_STYPE_REASSOC_RESP:
1815 case IEEE80211_STYPE_DEAUTH:
1816 case IEEE80211_STYPE_DISASSOC:
1817 skb_queue_tail(&ifsta->skb_queue, skb);
1818 queue_work(local->hw.workqueue, &ifsta->work);
1819 return;
1820 default:
1821 printk(KERN_DEBUG "%s: received unknown management frame - "
1822 "stype=%d\n", dev->name,
1823 (fc & IEEE80211_FCTL_STYPE) >> 4);
1824 break;
1825 }
1826
1827 fail:
1828 kfree_skb(skb);
1829}
1830
1831
1832static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1833 struct sk_buff *skb)
1834{
1835 struct ieee80211_rx_status *rx_status;
1836 struct ieee80211_sub_if_data *sdata;
1837 struct ieee80211_if_sta *ifsta;
1838 struct ieee80211_mgmt *mgmt;
1839 u16 fc;
1840
1841 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1842 ifsta = &sdata->u.sta;
1843
1844 rx_status = (struct ieee80211_rx_status *) skb->cb;
1845 mgmt = (struct ieee80211_mgmt *) skb->data;
1846 fc = le16_to_cpu(mgmt->frame_control);
1847
1848 switch (fc & IEEE80211_FCTL_STYPE) {
1849 case IEEE80211_STYPE_PROBE_REQ:
1850 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1851 rx_status);
1852 break;
1853 case IEEE80211_STYPE_PROBE_RESP:
1854 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1855 break;
1856 case IEEE80211_STYPE_BEACON:
1857 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1858 break;
1859 case IEEE80211_STYPE_AUTH:
1860 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1861 break;
1862 case IEEE80211_STYPE_ASSOC_RESP:
1863 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1864 break;
1865 case IEEE80211_STYPE_REASSOC_RESP:
1866 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1867 break;
1868 case IEEE80211_STYPE_DEAUTH:
1869 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1870 break;
1871 case IEEE80211_STYPE_DISASSOC:
1872 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1873 break;
1874 }
1875
1876 kfree_skb(skb);
1877}
1878
1879
1880void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1881 struct ieee80211_rx_status *rx_status)
1882{
1883 struct ieee80211_mgmt *mgmt;
1884 u16 fc;
1885
1886 if (skb->len < 24) {
1887 dev_kfree_skb(skb);
1888 return;
1889 }
1890
1891 mgmt = (struct ieee80211_mgmt *) skb->data;
1892 fc = le16_to_cpu(mgmt->frame_control);
1893
1894 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1895 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1896 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1897 skb->len, rx_status);
1898 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1899 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1900 rx_status);
1901 }
1902 }
1903
1904 dev_kfree_skb(skb);
1905}
1906
1907
1908static int ieee80211_sta_active_ibss(struct net_device *dev)
1909{
1910 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1911 int active = 0;
1912 struct sta_info *sta;
1913
Michael Wube8755e2007-07-27 15:43:23 +02001914 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07001915 list_for_each_entry(sta, &local->sta_list, list) {
1916 if (sta->dev == dev &&
1917 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1918 jiffies)) {
1919 active++;
1920 break;
1921 }
1922 }
Michael Wube8755e2007-07-27 15:43:23 +02001923 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07001924
1925 return active;
1926}
1927
1928
1929static void ieee80211_sta_expire(struct net_device *dev)
1930{
1931 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1932 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02001933 LIST_HEAD(tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001934
Michael Wube8755e2007-07-27 15:43:23 +02001935 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07001936 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1937 if (time_after(jiffies, sta->last_rx +
1938 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1939 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1940 "\n", dev->name, MAC_ARG(sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +02001941 __sta_info_get(sta);
1942 sta_info_remove(sta);
1943 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001944 }
Michael Wube8755e2007-07-27 15:43:23 +02001945 write_unlock_bh(&local->sta_lock);
1946
1947 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
1948 sta_info_free(sta);
1949 sta_info_put(sta);
1950 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001951}
1952
1953
1954static void ieee80211_sta_merge_ibss(struct net_device *dev,
1955 struct ieee80211_if_sta *ifsta)
1956{
1957 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1958
1959 ieee80211_sta_expire(dev);
1960 if (ieee80211_sta_active_ibss(dev))
1961 return;
1962
1963 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1964 "IBSS networks with same SSID (merge)\n", dev->name);
1965 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1966}
1967
1968
1969void ieee80211_sta_timer(unsigned long data)
1970{
1971 struct ieee80211_sub_if_data *sdata =
1972 (struct ieee80211_sub_if_data *) data;
1973 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1974 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1975
1976 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1977 queue_work(local->hw.workqueue, &ifsta->work);
1978}
1979
1980
1981void ieee80211_sta_work(struct work_struct *work)
1982{
1983 struct ieee80211_sub_if_data *sdata =
1984 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1985 struct net_device *dev = sdata->dev;
1986 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1987 struct ieee80211_if_sta *ifsta;
1988 struct sk_buff *skb;
1989
1990 if (!netif_running(dev))
1991 return;
1992
1993 if (local->sta_scanning)
1994 return;
1995
1996 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1997 sdata->type != IEEE80211_IF_TYPE_IBSS) {
1998 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
1999 "(type=%d)\n", dev->name, sdata->type);
2000 return;
2001 }
2002 ifsta = &sdata->u.sta;
2003
2004 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2005 ieee80211_sta_rx_queued_mgmt(dev, skb);
2006
2007 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2008 ifsta->state != IEEE80211_ASSOCIATE &&
2009 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2010 ieee80211_sta_start_scan(dev, NULL, 0);
2011 return;
2012 }
2013
2014 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2015 if (ieee80211_sta_config_auth(dev, ifsta))
2016 return;
2017 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2018 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2019 return;
2020
2021 switch (ifsta->state) {
2022 case IEEE80211_DISABLED:
2023 break;
2024 case IEEE80211_AUTHENTICATE:
2025 ieee80211_authenticate(dev, ifsta);
2026 break;
2027 case IEEE80211_ASSOCIATE:
2028 ieee80211_associate(dev, ifsta);
2029 break;
2030 case IEEE80211_ASSOCIATED:
2031 ieee80211_associated(dev, ifsta);
2032 break;
2033 case IEEE80211_IBSS_SEARCH:
2034 ieee80211_sta_find_ibss(dev, ifsta);
2035 break;
2036 case IEEE80211_IBSS_JOINED:
2037 ieee80211_sta_merge_ibss(dev, ifsta);
2038 break;
2039 default:
2040 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2041 ifsta->state);
2042 break;
2043 }
2044
2045 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2046 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2047 "mixed-cell disabled - disassociate\n", dev->name);
2048
2049 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2050 ieee80211_set_disassoc(dev, ifsta, 0);
2051 }
2052}
2053
2054
2055static void ieee80211_sta_reset_auth(struct net_device *dev,
2056 struct ieee80211_if_sta *ifsta)
2057{
2058 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2059
2060 if (local->ops->reset_tsf) {
2061 /* Reset own TSF to allow time synchronization work. */
2062 local->ops->reset_tsf(local_to_hw(local));
2063 }
2064
2065 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2066
2067
2068 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2069 ifsta->auth_alg = WLAN_AUTH_OPEN;
2070 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2071 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2072 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2073 ifsta->auth_alg = WLAN_AUTH_LEAP;
2074 else
2075 ifsta->auth_alg = WLAN_AUTH_OPEN;
2076 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2077 ifsta->auth_alg);
2078 ifsta->auth_transaction = -1;
2079 ifsta->associated = ifsta->auth_tries = ifsta->assoc_tries = 0;
2080 netif_carrier_off(dev);
2081}
2082
2083
2084void ieee80211_sta_req_auth(struct net_device *dev,
2085 struct ieee80211_if_sta *ifsta)
2086{
2087 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2088 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2089
2090 if (sdata->type != IEEE80211_IF_TYPE_STA)
2091 return;
2092
2093 if ((ifsta->bssid_set || ifsta->auto_bssid_sel) &&
2094 (ifsta->ssid_set || ifsta->auto_ssid_sel)) {
2095 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2096 queue_work(local->hw.workqueue, &ifsta->work);
2097 }
2098}
2099
2100static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2101 const char *ssid, int ssid_len)
2102{
2103 int tmp, hidden_ssid;
2104
2105 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2106 return 1;
2107
2108 if (ifsta->auto_bssid_sel)
2109 return 0;
2110
2111 hidden_ssid = 1;
2112 tmp = ssid_len;
2113 while (tmp--) {
2114 if (ssid[tmp] != '\0') {
2115 hidden_ssid = 0;
2116 break;
2117 }
2118 }
2119
2120 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2121 return 1;
2122
2123 if (ssid_len == 1 && ssid[0] == ' ')
2124 return 1;
2125
2126 return 0;
2127}
2128
2129static int ieee80211_sta_config_auth(struct net_device *dev,
2130 struct ieee80211_if_sta *ifsta)
2131{
2132 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2133 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2134 struct ieee80211_sta_bss *bss, *selected = NULL;
2135 int top_rssi = 0, freq;
2136
Jiri Bencf0706e82007-05-05 11:45:53 -07002137 if (!ifsta->auto_channel_sel && !ifsta->auto_bssid_sel &&
2138 !ifsta->auto_ssid_sel) {
2139 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002140 ieee80211_sta_reset_auth(dev, ifsta);
2141 return 0;
2142 }
2143
2144 spin_lock_bh(&local->sta_bss_lock);
2145 freq = local->oper_channel->freq;
2146 list_for_each_entry(bss, &local->sta_bss_list, list) {
2147 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2148 continue;
2149
2150 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2151 !!sdata->default_key)
2152 continue;
2153
2154 if (!ifsta->auto_channel_sel && bss->freq != freq)
2155 continue;
2156
2157 if (!ifsta->auto_bssid_sel &&
2158 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2159 continue;
2160
2161 if (!ifsta->auto_ssid_sel &&
2162 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2163 continue;
2164
2165 if (!selected || top_rssi < bss->rssi) {
2166 selected = bss;
2167 top_rssi = bss->rssi;
2168 }
2169 }
2170 if (selected)
2171 atomic_inc(&selected->users);
2172 spin_unlock_bh(&local->sta_bss_lock);
2173
2174 if (selected) {
2175 ieee80211_set_channel(local, -1, selected->freq);
2176 if (!ifsta->ssid_set)
2177 ieee80211_sta_set_ssid(dev, selected->ssid,
2178 selected->ssid_len);
2179 ieee80211_sta_set_bssid(dev, selected->bssid);
2180 ieee80211_rx_bss_put(dev, selected);
2181 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002182 ieee80211_sta_reset_auth(dev, ifsta);
2183 return 0;
2184 } else {
2185 if (ifsta->state != IEEE80211_AUTHENTICATE) {
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04002186 if (ifsta->auto_ssid_sel)
2187 ieee80211_sta_start_scan(dev, NULL, 0);
2188 else
2189 ieee80211_sta_start_scan(dev, ifsta->ssid,
2190 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002191 ifsta->state = IEEE80211_AUTHENTICATE;
2192 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2193 } else
2194 ifsta->state = IEEE80211_DISABLED;
2195 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002196 return -1;
2197}
2198
2199static int ieee80211_sta_join_ibss(struct net_device *dev,
2200 struct ieee80211_if_sta *ifsta,
2201 struct ieee80211_sta_bss *bss)
2202{
2203 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2204 int res, rates, i, j;
2205 struct sk_buff *skb;
2206 struct ieee80211_mgmt *mgmt;
2207 struct ieee80211_tx_control control;
2208 struct ieee80211_rate *rate;
2209 struct ieee80211_hw_mode *mode;
2210 struct rate_control_extra extra;
2211 u8 *pos;
2212 struct ieee80211_sub_if_data *sdata;
2213
2214 /* Remove possible STA entries from other IBSS networks. */
2215 sta_info_flush(local, NULL);
2216
2217 if (local->ops->reset_tsf) {
2218 /* Reset own TSF to allow time synchronization work. */
2219 local->ops->reset_tsf(local_to_hw(local));
2220 }
2221 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2222 res = ieee80211_if_config(dev);
2223 if (res)
2224 return res;
2225
2226 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2227
2228 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2229 sdata->drop_unencrypted = bss->capability &
2230 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2231
2232 res = ieee80211_set_channel(local, -1, bss->freq);
2233
2234 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2235 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2236 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2237 local->hw.conf.freq);
2238 return -1;
2239 }
2240
2241 /* Set beacon template based on scan results */
2242 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2243 do {
2244 if (!skb)
2245 break;
2246
2247 skb_reserve(skb, local->hw.extra_tx_headroom);
2248
2249 mgmt = (struct ieee80211_mgmt *)
2250 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2251 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2252 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2253 IEEE80211_STYPE_BEACON);
2254 memset(mgmt->da, 0xff, ETH_ALEN);
2255 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2256 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2257 mgmt->u.beacon.beacon_int =
2258 cpu_to_le16(local->hw.conf.beacon_int);
2259 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2260
2261 pos = skb_put(skb, 2 + ifsta->ssid_len);
2262 *pos++ = WLAN_EID_SSID;
2263 *pos++ = ifsta->ssid_len;
2264 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2265
2266 rates = bss->supp_rates_len;
2267 if (rates > 8)
2268 rates = 8;
2269 pos = skb_put(skb, 2 + rates);
2270 *pos++ = WLAN_EID_SUPP_RATES;
2271 *pos++ = rates;
2272 memcpy(pos, bss->supp_rates, rates);
2273
2274 pos = skb_put(skb, 2 + 1);
2275 *pos++ = WLAN_EID_DS_PARAMS;
2276 *pos++ = 1;
2277 *pos++ = bss->channel;
2278
2279 pos = skb_put(skb, 2 + 2);
2280 *pos++ = WLAN_EID_IBSS_PARAMS;
2281 *pos++ = 2;
2282 /* FIX: set ATIM window based on scan results */
2283 *pos++ = 0;
2284 *pos++ = 0;
2285
2286 if (bss->supp_rates_len > 8) {
2287 rates = bss->supp_rates_len - 8;
2288 pos = skb_put(skb, 2 + rates);
2289 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2290 *pos++ = rates;
2291 memcpy(pos, &bss->supp_rates[8], rates);
2292 }
2293
2294 memset(&control, 0, sizeof(control));
2295 memset(&extra, 0, sizeof(extra));
2296 extra.mode = local->oper_hw_mode;
2297 rate = rate_control_get_rate(local, dev, skb, &extra);
2298 if (!rate) {
2299 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2300 "for IBSS beacon\n", dev->name);
2301 break;
2302 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02002303 control.tx_rate = (sdata->short_preamble &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002304 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2305 rate->val2 : rate->val;
2306 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2307 control.power_level = local->hw.conf.power_level;
2308 control.flags |= IEEE80211_TXCTL_NO_ACK;
2309 control.retry_limit = 1;
2310
2311 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2312 if (ifsta->probe_resp) {
2313 mgmt = (struct ieee80211_mgmt *)
2314 ifsta->probe_resp->data;
2315 mgmt->frame_control =
2316 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2317 IEEE80211_STYPE_PROBE_RESP);
2318 } else {
2319 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2320 "template for IBSS\n", dev->name);
2321 }
2322
2323 if (local->ops->beacon_update &&
2324 local->ops->beacon_update(local_to_hw(local),
2325 skb, &control) == 0) {
2326 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2327 "template based on scan results\n", dev->name);
2328 skb = NULL;
2329 }
2330
2331 rates = 0;
2332 mode = local->oper_hw_mode;
2333 for (i = 0; i < bss->supp_rates_len; i++) {
2334 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2335 if (mode->mode == MODE_ATHEROS_TURBO)
2336 bitrate *= 2;
2337 for (j = 0; j < mode->num_rates; j++)
2338 if (mode->rates[j].rate == bitrate)
2339 rates |= BIT(j);
2340 }
2341 ifsta->supp_rates_bits = rates;
2342 } while (0);
2343
2344 if (skb) {
2345 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2346 "template\n", dev->name);
2347 dev_kfree_skb(skb);
2348 }
2349
2350 ifsta->state = IEEE80211_IBSS_JOINED;
2351 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2352
2353 ieee80211_rx_bss_put(dev, bss);
2354
2355 return res;
2356}
2357
2358
2359static int ieee80211_sta_create_ibss(struct net_device *dev,
2360 struct ieee80211_if_sta *ifsta)
2361{
2362 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2363 struct ieee80211_sta_bss *bss;
2364 struct ieee80211_sub_if_data *sdata;
2365 struct ieee80211_hw_mode *mode;
2366 u8 bssid[ETH_ALEN], *pos;
2367 int i;
2368
2369#if 0
2370 /* Easier testing, use fixed BSSID. */
2371 memset(bssid, 0xfe, ETH_ALEN);
2372#else
2373 /* Generate random, not broadcast, locally administered BSSID. Mix in
2374 * own MAC address to make sure that devices that do not have proper
2375 * random number generator get different BSSID. */
2376 get_random_bytes(bssid, ETH_ALEN);
2377 for (i = 0; i < ETH_ALEN; i++)
2378 bssid[i] ^= dev->dev_addr[i];
2379 bssid[0] &= ~0x01;
2380 bssid[0] |= 0x02;
2381#endif
2382
2383 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2384 dev->name, MAC_ARG(bssid));
2385
2386 bss = ieee80211_rx_bss_add(dev, bssid);
2387 if (!bss)
2388 return -ENOMEM;
2389
2390 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2391 mode = local->oper_hw_mode;
2392
2393 if (local->hw.conf.beacon_int == 0)
2394 local->hw.conf.beacon_int = 100;
2395 bss->beacon_int = local->hw.conf.beacon_int;
2396 bss->hw_mode = local->hw.conf.phymode;
2397 bss->channel = local->hw.conf.channel;
2398 bss->freq = local->hw.conf.freq;
2399 bss->last_update = jiffies;
2400 bss->capability = WLAN_CAPABILITY_IBSS;
2401 if (sdata->default_key) {
2402 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2403 } else
2404 sdata->drop_unencrypted = 0;
2405 bss->supp_rates_len = mode->num_rates;
2406 pos = bss->supp_rates;
2407 for (i = 0; i < mode->num_rates; i++) {
2408 int rate = mode->rates[i].rate;
2409 if (mode->mode == MODE_ATHEROS_TURBO)
2410 rate /= 2;
2411 *pos++ = (u8) (rate / 5);
2412 }
2413
2414 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2415}
2416
2417
2418static int ieee80211_sta_find_ibss(struct net_device *dev,
2419 struct ieee80211_if_sta *ifsta)
2420{
2421 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2422 struct ieee80211_sta_bss *bss;
2423 int found = 0;
2424 u8 bssid[ETH_ALEN];
2425 int active_ibss;
2426
2427 if (ifsta->ssid_len == 0)
2428 return -EINVAL;
2429
2430 active_ibss = ieee80211_sta_active_ibss(dev);
2431#ifdef CONFIG_MAC80211_IBSS_DEBUG
2432 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2433 dev->name, active_ibss);
2434#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2435 spin_lock_bh(&local->sta_bss_lock);
2436 list_for_each_entry(bss, &local->sta_bss_list, list) {
2437 if (ifsta->ssid_len != bss->ssid_len ||
2438 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2439 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2440 continue;
2441#ifdef CONFIG_MAC80211_IBSS_DEBUG
2442 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2443 MAC_ARG(bss->bssid));
2444#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2445 memcpy(bssid, bss->bssid, ETH_ALEN);
2446 found = 1;
2447 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2448 break;
2449 }
2450 spin_unlock_bh(&local->sta_bss_lock);
2451
2452#ifdef CONFIG_MAC80211_IBSS_DEBUG
2453 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2454 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2455#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2456 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2457 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2458 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2459 " based on configured SSID\n",
2460 dev->name, MAC_ARG(bssid));
2461 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2462 }
2463#ifdef CONFIG_MAC80211_IBSS_DEBUG
2464 printk(KERN_DEBUG " did not try to join ibss\n");
2465#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2466
2467 /* Selected IBSS not found in current scan results - try to scan */
2468 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2469 !ieee80211_sta_active_ibss(dev)) {
2470 mod_timer(&ifsta->timer, jiffies +
2471 IEEE80211_IBSS_MERGE_INTERVAL);
2472 } else if (time_after(jiffies, local->last_scan_completed +
2473 IEEE80211_SCAN_INTERVAL)) {
2474 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2475 "join\n", dev->name);
2476 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2477 ifsta->ssid_len);
2478 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2479 int interval = IEEE80211_SCAN_INTERVAL;
2480
2481 if (time_after(jiffies, ifsta->ibss_join_req +
2482 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2483 if (ifsta->create_ibss &&
2484 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2485 return ieee80211_sta_create_ibss(dev, ifsta);
2486 if (ifsta->create_ibss) {
2487 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2488 " configured channel %d (%d MHz)\n",
2489 dev->name, local->hw.conf.channel,
2490 local->hw.conf.freq);
2491 }
2492
2493 /* No IBSS found - decrease scan interval and continue
2494 * scanning. */
2495 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2496 }
2497
2498 ifsta->state = IEEE80211_IBSS_SEARCH;
2499 mod_timer(&ifsta->timer, jiffies + interval);
2500 return 0;
2501 }
2502
2503 return 0;
2504}
2505
2506
2507int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2508{
2509 struct ieee80211_sub_if_data *sdata;
2510 struct ieee80211_if_sta *ifsta;
2511 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2512
2513 if (len > IEEE80211_MAX_SSID_LEN)
2514 return -EINVAL;
2515
2516 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2517 * not defined. */
2518 if (local->ops->conf_tx) {
2519 struct ieee80211_tx_queue_params qparam;
2520 int i;
2521
2522 memset(&qparam, 0, sizeof(qparam));
2523 /* TODO: are these ok defaults for all hw_modes? */
2524 qparam.aifs = 2;
2525 qparam.cw_min =
2526 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2527 qparam.cw_max = 1023;
2528 qparam.burst_time = 0;
2529 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2530 {
2531 local->ops->conf_tx(local_to_hw(local),
2532 i + IEEE80211_TX_QUEUE_DATA0,
2533 &qparam);
2534 }
2535 /* IBSS uses different parameters for Beacon sending */
2536 qparam.cw_min++;
2537 qparam.cw_min *= 2;
2538 qparam.cw_min--;
2539 local->ops->conf_tx(local_to_hw(local),
2540 IEEE80211_TX_QUEUE_BEACON, &qparam);
2541 }
2542
2543 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2544 ifsta = &sdata->u.sta;
2545
2546 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2547 ifsta->prev_bssid_set = 0;
2548 memcpy(ifsta->ssid, ssid, len);
2549 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2550 ifsta->ssid_len = len;
2551
2552 ifsta->ssid_set = len ? 1 : 0;
2553 if (sdata->type == IEEE80211_IF_TYPE_IBSS && !ifsta->bssid_set) {
2554 ifsta->ibss_join_req = jiffies;
2555 ifsta->state = IEEE80211_IBSS_SEARCH;
2556 return ieee80211_sta_find_ibss(dev, ifsta);
2557 }
2558 return 0;
2559}
2560
2561
2562int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2563{
2564 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2565 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2566 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2567 *len = ifsta->ssid_len;
2568 return 0;
2569}
2570
2571
2572int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2573{
2574 struct ieee80211_sub_if_data *sdata;
2575 struct ieee80211_if_sta *ifsta;
2576 int res;
2577
2578 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2579 ifsta = &sdata->u.sta;
2580
2581 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2582 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2583 res = ieee80211_if_config(dev);
2584 if (res) {
2585 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2586 "the low-level driver\n", dev->name);
2587 return res;
2588 }
2589 }
2590
2591 if (!is_valid_ether_addr(bssid))
2592 ifsta->bssid_set = 0;
2593 else
2594 ifsta->bssid_set = 1;
2595 return 0;
2596}
2597
2598
2599static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2600 struct ieee80211_sub_if_data *sdata,
2601 int powersave)
2602{
2603 struct sk_buff *skb;
2604 struct ieee80211_hdr *nullfunc;
2605 u16 fc;
2606
2607 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2608 if (!skb) {
2609 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2610 "frame\n", sdata->dev->name);
2611 return;
2612 }
2613 skb_reserve(skb, local->hw.extra_tx_headroom);
2614
2615 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2616 memset(nullfunc, 0, 24);
2617 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2618 IEEE80211_FCTL_TODS;
2619 if (powersave)
2620 fc |= IEEE80211_FCTL_PM;
2621 nullfunc->frame_control = cpu_to_le16(fc);
2622 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2623 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2624 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2625
2626 ieee80211_sta_tx(sdata->dev, skb, 0);
2627}
2628
2629
2630void ieee80211_scan_completed(struct ieee80211_hw *hw)
2631{
2632 struct ieee80211_local *local = hw_to_local(hw);
2633 struct net_device *dev = local->scan_dev;
2634 struct ieee80211_sub_if_data *sdata;
2635 union iwreq_data wrqu;
2636
2637 local->last_scan_completed = jiffies;
2638 wmb();
2639 local->sta_scanning = 0;
2640
2641 if (ieee80211_hw_config(local))
2642 printk(KERN_DEBUG "%s: failed to restore operational"
2643 "channel after scan\n", dev->name);
2644
2645 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2646 ieee80211_if_config(dev))
2647 printk(KERN_DEBUG "%s: failed to restore operational"
2648 "BSSID after scan\n", dev->name);
2649
2650 memset(&wrqu, 0, sizeof(wrqu));
2651 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2652
2653 read_lock(&local->sub_if_lock);
2654 list_for_each_entry(sdata, &local->sub_if_list, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02002655
2656 /* No need to wake the master device. */
2657 if (sdata->dev == local->mdev)
2658 continue;
2659
Jiri Bencf0706e82007-05-05 11:45:53 -07002660 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2661 if (sdata->u.sta.associated)
2662 ieee80211_send_nullfunc(local, sdata, 0);
2663 ieee80211_sta_timer((unsigned long)sdata);
2664 }
Mattias Nissler14042cbe2007-06-08 15:31:13 +02002665
Jiri Bencf0706e82007-05-05 11:45:53 -07002666 netif_wake_queue(sdata->dev);
2667 }
2668 read_unlock(&local->sub_if_lock);
2669
2670 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2671 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2672 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2673 if (!ifsta->bssid_set ||
2674 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2675 !ieee80211_sta_active_ibss(dev)))
2676 ieee80211_sta_find_ibss(dev, ifsta);
2677 }
2678}
2679EXPORT_SYMBOL(ieee80211_scan_completed);
2680
2681void ieee80211_sta_scan_work(struct work_struct *work)
2682{
2683 struct ieee80211_local *local =
2684 container_of(work, struct ieee80211_local, scan_work.work);
2685 struct net_device *dev = local->scan_dev;
2686 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2687 struct ieee80211_hw_mode *mode;
2688 struct ieee80211_channel *chan;
2689 int skip;
2690 unsigned long next_delay = 0;
2691
2692 if (!local->sta_scanning)
2693 return;
2694
2695 switch (local->scan_state) {
2696 case SCAN_SET_CHANNEL:
2697 mode = local->scan_hw_mode;
2698 if (local->scan_hw_mode->list.next == &local->modes_list &&
2699 local->scan_channel_idx >= mode->num_channels) {
2700 ieee80211_scan_completed(local_to_hw(local));
2701 return;
2702 }
2703 skip = !(local->enabled_modes & (1 << mode->mode));
2704 chan = &mode->channels[local->scan_channel_idx];
2705 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2706 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2707 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2708 (local->hw_modes & local->enabled_modes &
2709 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2710 skip = 1;
2711
2712 if (!skip) {
2713#if 0
2714 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2715 dev->name, chan->chan, chan->freq);
2716#endif
2717
2718 local->scan_channel = chan;
2719 if (ieee80211_hw_config(local)) {
2720 printk(KERN_DEBUG "%s: failed to set channel "
2721 "%d (%d MHz) for scan\n", dev->name,
2722 chan->chan, chan->freq);
2723 skip = 1;
2724 }
2725 }
2726
2727 local->scan_channel_idx++;
2728 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2729 if (local->scan_hw_mode->list.next != &local->modes_list) {
2730 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2731 struct ieee80211_hw_mode,
2732 list);
2733 local->scan_channel_idx = 0;
2734 }
2735 }
2736
2737 if (skip)
2738 break;
2739
2740 next_delay = IEEE80211_PROBE_DELAY +
2741 usecs_to_jiffies(local->hw.channel_change_time);
2742 local->scan_state = SCAN_SEND_PROBE;
2743 break;
2744 case SCAN_SEND_PROBE:
2745 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2746 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2747 local->scan_ssid_len);
2748 next_delay = IEEE80211_CHANNEL_TIME;
2749 } else
2750 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2751 local->scan_state = SCAN_SET_CHANNEL;
2752 break;
2753 }
2754
2755 if (local->sta_scanning)
2756 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2757 next_delay);
2758}
2759
2760
2761static int ieee80211_sta_start_scan(struct net_device *dev,
2762 u8 *ssid, size_t ssid_len)
2763{
2764 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2765 struct ieee80211_sub_if_data *sdata;
2766
2767 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2768 return -EINVAL;
2769
2770 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2771 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2772 * BSSID: MACAddress
2773 * SSID
2774 * ScanType: ACTIVE, PASSIVE
2775 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2776 * a Probe frame during active scanning
2777 * ChannelList
2778 * MinChannelTime (>= ProbeDelay), in TU
2779 * MaxChannelTime: (>= MinChannelTime), in TU
2780 */
2781
2782 /* MLME-SCAN.confirm
2783 * BSSDescriptionSet
2784 * ResultCode: SUCCESS, INVALID_PARAMETERS
2785 */
2786
2787 if (local->sta_scanning) {
2788 if (local->scan_dev == dev)
2789 return 0;
2790 return -EBUSY;
2791 }
2792
2793 if (local->ops->hw_scan) {
2794 int rc = local->ops->hw_scan(local_to_hw(local),
2795 ssid, ssid_len);
2796 if (!rc) {
2797 local->sta_scanning = 1;
2798 local->scan_dev = dev;
2799 }
2800 return rc;
2801 }
2802
2803 local->sta_scanning = 1;
2804
2805 read_lock(&local->sub_if_lock);
2806 list_for_each_entry(sdata, &local->sub_if_list, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02002807
2808 /* Don't stop the master interface, otherwise we can't transmit
2809 * probes! */
2810 if (sdata->dev == local->mdev)
2811 continue;
2812
Jiri Bencf0706e82007-05-05 11:45:53 -07002813 netif_stop_queue(sdata->dev);
2814 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2815 sdata->u.sta.associated)
2816 ieee80211_send_nullfunc(local, sdata, 1);
2817 }
2818 read_unlock(&local->sub_if_lock);
2819
2820 if (ssid) {
2821 local->scan_ssid_len = ssid_len;
2822 memcpy(local->scan_ssid, ssid, ssid_len);
2823 } else
2824 local->scan_ssid_len = 0;
2825 local->scan_state = SCAN_SET_CHANNEL;
2826 local->scan_hw_mode = list_entry(local->modes_list.next,
2827 struct ieee80211_hw_mode,
2828 list);
2829 local->scan_channel_idx = 0;
2830 local->scan_dev = dev;
2831
2832 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2833 ieee80211_if_config(dev))
2834 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2835 dev->name);
2836
2837 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2838 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2839 IEEE80211_CHANNEL_TIME);
2840
2841 return 0;
2842}
2843
2844
2845int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2846{
2847 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2848 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2849 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2850
2851 if (sdata->type != IEEE80211_IF_TYPE_STA)
2852 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2853
2854 if (local->sta_scanning) {
2855 if (local->scan_dev == dev)
2856 return 0;
2857 return -EBUSY;
2858 }
2859
2860 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2861 queue_work(local->hw.workqueue, &ifsta->work);
2862 return 0;
2863}
2864
2865static char *
2866ieee80211_sta_scan_result(struct net_device *dev,
2867 struct ieee80211_sta_bss *bss,
2868 char *current_ev, char *end_buf)
2869{
2870 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2871 struct iw_event iwe;
2872
2873 if (time_after(jiffies,
2874 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2875 return current_ev;
2876
2877 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2878 return current_ev;
2879
2880 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2881 !bss->wpa_ie && !bss->rsn_ie)
2882 return current_ev;
2883
2884 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2885 (local->scan_ssid_len != bss->ssid_len ||
2886 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2887 return current_ev;
2888
2889 memset(&iwe, 0, sizeof(iwe));
2890 iwe.cmd = SIOCGIWAP;
2891 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2892 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2893 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2894 IW_EV_ADDR_LEN);
2895
2896 memset(&iwe, 0, sizeof(iwe));
2897 iwe.cmd = SIOCGIWESSID;
2898 iwe.u.data.length = bss->ssid_len;
2899 iwe.u.data.flags = 1;
2900 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2901 bss->ssid);
2902
2903 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2904 memset(&iwe, 0, sizeof(iwe));
2905 iwe.cmd = SIOCGIWMODE;
2906 if (bss->capability & WLAN_CAPABILITY_ESS)
2907 iwe.u.mode = IW_MODE_MASTER;
2908 else
2909 iwe.u.mode = IW_MODE_ADHOC;
2910 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2911 IW_EV_UINT_LEN);
2912 }
2913
2914 memset(&iwe, 0, sizeof(iwe));
2915 iwe.cmd = SIOCGIWFREQ;
2916 iwe.u.freq.m = bss->channel;
2917 iwe.u.freq.e = 0;
2918 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2919 IW_EV_FREQ_LEN);
2920 iwe.u.freq.m = bss->freq * 100000;
2921 iwe.u.freq.e = 1;
2922 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2923 IW_EV_FREQ_LEN);
2924
2925 memset(&iwe, 0, sizeof(iwe));
2926 iwe.cmd = IWEVQUAL;
2927 iwe.u.qual.qual = bss->signal;
2928 iwe.u.qual.level = bss->rssi;
2929 iwe.u.qual.noise = bss->noise;
2930 iwe.u.qual.updated = local->wstats_flags;
2931 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2932 IW_EV_QUAL_LEN);
2933
2934 memset(&iwe, 0, sizeof(iwe));
2935 iwe.cmd = SIOCGIWENCODE;
2936 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2937 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2938 else
2939 iwe.u.data.flags = IW_ENCODE_DISABLED;
2940 iwe.u.data.length = 0;
2941 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2942
2943 if (bss && bss->wpa_ie) {
2944 memset(&iwe, 0, sizeof(iwe));
2945 iwe.cmd = IWEVGENIE;
2946 iwe.u.data.length = bss->wpa_ie_len;
2947 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2948 bss->wpa_ie);
2949 }
2950
2951 if (bss && bss->rsn_ie) {
2952 memset(&iwe, 0, sizeof(iwe));
2953 iwe.cmd = IWEVGENIE;
2954 iwe.u.data.length = bss->rsn_ie_len;
2955 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2956 bss->rsn_ie);
2957 }
2958
2959 if (bss && bss->supp_rates_len > 0) {
2960 /* display all supported rates in readable format */
2961 char *p = current_ev + IW_EV_LCP_LEN;
2962 int i;
2963
2964 memset(&iwe, 0, sizeof(iwe));
2965 iwe.cmd = SIOCGIWRATE;
2966 /* Those two flags are ignored... */
2967 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2968
2969 for (i = 0; i < bss->supp_rates_len; i++) {
2970 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2971 0x7f) * 500000);
2972 p = iwe_stream_add_value(current_ev, p,
2973 end_buf, &iwe, IW_EV_PARAM_LEN);
2974 }
2975 current_ev = p;
2976 }
2977
2978 if (bss) {
2979 char *buf;
2980 buf = kmalloc(30, GFP_ATOMIC);
2981 if (buf) {
2982 memset(&iwe, 0, sizeof(iwe));
2983 iwe.cmd = IWEVCUSTOM;
2984 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
2985 iwe.u.data.length = strlen(buf);
2986 current_ev = iwe_stream_add_point(current_ev, end_buf,
2987 &iwe, buf);
2988 kfree(buf);
2989 }
2990 }
2991
2992 do {
2993 char *buf;
2994
2995 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
2996 break;
2997
2998 buf = kmalloc(100, GFP_ATOMIC);
2999 if (!buf)
3000 break;
3001
3002 memset(&iwe, 0, sizeof(iwe));
3003 iwe.cmd = IWEVCUSTOM;
3004 sprintf(buf, "bcn_int=%d", bss->beacon_int);
3005 iwe.u.data.length = strlen(buf);
3006 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3007 buf);
3008
3009 memset(&iwe, 0, sizeof(iwe));
3010 iwe.cmd = IWEVCUSTOM;
3011 sprintf(buf, "capab=0x%04x", bss->capability);
3012 iwe.u.data.length = strlen(buf);
3013 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3014 buf);
3015
3016 kfree(buf);
3017 break;
3018 } while (0);
3019
3020 return current_ev;
3021}
3022
3023
3024int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3025{
3026 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3027 char *current_ev = buf;
3028 char *end_buf = buf + len;
3029 struct ieee80211_sta_bss *bss;
3030
3031 spin_lock_bh(&local->sta_bss_lock);
3032 list_for_each_entry(bss, &local->sta_bss_list, list) {
3033 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3034 spin_unlock_bh(&local->sta_bss_lock);
3035 return -E2BIG;
3036 }
3037 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3038 end_buf);
3039 }
3040 spin_unlock_bh(&local->sta_bss_lock);
3041 return current_ev - buf;
3042}
3043
3044
3045int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3046{
3047 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3048 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3049 kfree(ifsta->extra_ie);
3050 if (len == 0) {
3051 ifsta->extra_ie = NULL;
3052 ifsta->extra_ie_len = 0;
3053 return 0;
3054 }
3055 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3056 if (!ifsta->extra_ie) {
3057 ifsta->extra_ie_len = 0;
3058 return -ENOMEM;
3059 }
3060 memcpy(ifsta->extra_ie, ie, len);
3061 ifsta->extra_ie_len = len;
3062 return 0;
3063}
3064
3065
3066struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3067 struct sk_buff *skb, u8 *bssid,
3068 u8 *addr)
3069{
3070 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3071 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04003072 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003073
3074 /* TODO: Could consider removing the least recently used entry and
3075 * allow new one to be added. */
3076 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3077 if (net_ratelimit()) {
3078 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3079 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3080 }
3081 return NULL;
3082 }
3083
3084 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3085 local->mdev->name, MAC_ARG(addr), dev->name);
3086
3087 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3088 if (!sta)
3089 return NULL;
3090
3091 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3092
3093 rate_control_rate_init(sta, local);
3094
3095 return sta; /* caller will call sta_info_put() */
3096}
3097
3098
3099int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3100{
3101 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3102 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3103
3104 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3105 dev->name, reason);
3106
3107 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3108 sdata->type != IEEE80211_IF_TYPE_IBSS)
3109 return -EINVAL;
3110
3111 ieee80211_send_deauth(dev, ifsta, reason);
3112 ieee80211_set_disassoc(dev, ifsta, 1);
3113 return 0;
3114}
3115
3116
3117int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3118{
3119 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3120 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3121
3122 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3123 dev->name, reason);
3124
3125 if (sdata->type != IEEE80211_IF_TYPE_STA)
3126 return -EINVAL;
3127
3128 if (!ifsta->associated)
3129 return -1;
3130
3131 ieee80211_send_disassoc(dev, ifsta, reason);
3132 ieee80211_set_disassoc(dev, ifsta, 0);
3133 return 0;
3134}