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