e1000: Add device IDs of blade version of the 82571 quad port
[pv_ops_mirror.git] / net / mac80211 / ieee80211_sta.c
blob0d99b685df5f6cf46ea3254b92bc3f103e539d17
1 /*
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>
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.
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)
20 #include <linux/delay.h>
21 #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 <net/iw_handler.h>
29 #include <asm/types.h>
31 #include <net/mac80211.h>
32 #include "ieee80211_i.h"
33 #include "ieee80211_rate.h"
34 #include "hostapd_ioctl.h"
36 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
37 #define IEEE80211_AUTH_MAX_TRIES 3
38 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
39 #define IEEE80211_ASSOC_MAX_TRIES 3
40 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
41 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
42 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
43 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
44 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
45 #define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
47 #define IEEE80211_PROBE_DELAY (HZ / 33)
48 #define IEEE80211_CHANNEL_TIME (HZ / 33)
49 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
50 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
51 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
52 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
54 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
57 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
59 #define ERP_INFO_USE_PROTECTION BIT(1)
61 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
62 u8 *ssid, size_t ssid_len);
63 static struct ieee80211_sta_bss *
64 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid);
65 static void ieee80211_rx_bss_put(struct net_device *dev,
66 struct ieee80211_sta_bss *bss);
67 static int ieee80211_sta_find_ibss(struct net_device *dev,
68 struct ieee80211_if_sta *ifsta);
69 static int ieee80211_sta_wep_configured(struct net_device *dev);
70 static int ieee80211_sta_start_scan(struct net_device *dev,
71 u8 *ssid, size_t ssid_len);
72 static int ieee80211_sta_config_auth(struct net_device *dev,
73 struct ieee80211_if_sta *ifsta);
76 /* Parsed Information Elements */
77 struct ieee802_11_elems {
78 /* pointers to IEs */
79 u8 *ssid;
80 u8 *supp_rates;
81 u8 *fh_params;
82 u8 *ds_params;
83 u8 *cf_params;
84 u8 *tim;
85 u8 *ibss_params;
86 u8 *challenge;
87 u8 *wpa;
88 u8 *rsn;
89 u8 *erp_info;
90 u8 *ext_supp_rates;
91 u8 *wmm_info;
92 u8 *wmm_param;
94 /* length of them, respectively */
95 u8 ssid_len;
96 u8 supp_rates_len;
97 u8 fh_params_len;
98 u8 ds_params_len;
99 u8 cf_params_len;
100 u8 tim_len;
101 u8 ibss_params_len;
102 u8 challenge_len;
103 u8 wpa_len;
104 u8 rsn_len;
105 u8 erp_info_len;
106 u8 ext_supp_rates_len;
107 u8 wmm_info_len;
108 u8 wmm_param_len;
111 typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
114 static ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
115 struct ieee802_11_elems *elems)
117 size_t left = len;
118 u8 *pos = start;
119 int unknown = 0;
121 memset(elems, 0, sizeof(*elems));
123 while (left >= 2) {
124 u8 id, elen;
126 id = *pos++;
127 elen = *pos++;
128 left -= 2;
130 if (elen > left) {
131 #if 0
132 if (net_ratelimit())
133 printk(KERN_DEBUG "IEEE 802.11 element parse "
134 "failed (id=%d elen=%d left=%d)\n",
135 id, elen, left);
136 #endif
137 return ParseFailed;
140 switch (id) {
141 case WLAN_EID_SSID:
142 elems->ssid = pos;
143 elems->ssid_len = elen;
144 break;
145 case WLAN_EID_SUPP_RATES:
146 elems->supp_rates = pos;
147 elems->supp_rates_len = elen;
148 break;
149 case WLAN_EID_FH_PARAMS:
150 elems->fh_params = pos;
151 elems->fh_params_len = elen;
152 break;
153 case WLAN_EID_DS_PARAMS:
154 elems->ds_params = pos;
155 elems->ds_params_len = elen;
156 break;
157 case WLAN_EID_CF_PARAMS:
158 elems->cf_params = pos;
159 elems->cf_params_len = elen;
160 break;
161 case WLAN_EID_TIM:
162 elems->tim = pos;
163 elems->tim_len = elen;
164 break;
165 case WLAN_EID_IBSS_PARAMS:
166 elems->ibss_params = pos;
167 elems->ibss_params_len = elen;
168 break;
169 case WLAN_EID_CHALLENGE:
170 elems->challenge = pos;
171 elems->challenge_len = elen;
172 break;
173 case WLAN_EID_WPA:
174 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
175 pos[2] == 0xf2) {
176 /* Microsoft OUI (00:50:F2) */
177 if (pos[3] == 1) {
178 /* OUI Type 1 - WPA IE */
179 elems->wpa = pos;
180 elems->wpa_len = elen;
181 } else if (elen >= 5 && pos[3] == 2) {
182 if (pos[4] == 0) {
183 elems->wmm_info = pos;
184 elems->wmm_info_len = elen;
185 } else if (pos[4] == 1) {
186 elems->wmm_param = pos;
187 elems->wmm_param_len = elen;
191 break;
192 case WLAN_EID_RSN:
193 elems->rsn = pos;
194 elems->rsn_len = elen;
195 break;
196 case WLAN_EID_ERP_INFO:
197 elems->erp_info = pos;
198 elems->erp_info_len = elen;
199 break;
200 case WLAN_EID_EXT_SUPP_RATES:
201 elems->ext_supp_rates = pos;
202 elems->ext_supp_rates_len = elen;
203 break;
204 default:
205 #if 0
206 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
207 "unknown element (id=%d elen=%d)\n",
208 id, elen);
209 #endif
210 unknown++;
211 break;
214 left -= elen;
215 pos += elen;
218 /* Do not trigger error if left == 1 as Apple Airport base stations
219 * send AssocResps that are one spurious byte too long. */
221 return unknown ? ParseUnknown : ParseOK;
227 static int ecw2cw(int ecw)
229 int cw = 1;
230 while (ecw > 0) {
231 cw <<= 1;
232 ecw--;
234 return cw - 1;
238 static void ieee80211_sta_wmm_params(struct net_device *dev,
239 struct ieee80211_if_sta *ifsta,
240 u8 *wmm_param, size_t wmm_param_len)
242 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
243 struct ieee80211_tx_queue_params params;
244 size_t left;
245 int count;
246 u8 *pos;
248 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
249 return;
250 count = wmm_param[6] & 0x0f;
251 if (count == ifsta->wmm_last_param_set)
252 return;
253 ifsta->wmm_last_param_set = count;
255 pos = wmm_param + 8;
256 left = wmm_param_len - 8;
258 memset(&params, 0, sizeof(params));
260 if (!local->ops->conf_tx)
261 return;
263 local->wmm_acm = 0;
264 for (; left >= 4; left -= 4, pos += 4) {
265 int aci = (pos[0] >> 5) & 0x03;
266 int acm = (pos[0] >> 4) & 0x01;
267 int queue;
269 switch (aci) {
270 case 1:
271 queue = IEEE80211_TX_QUEUE_DATA3;
272 if (acm) {
273 local->wmm_acm |= BIT(0) | BIT(3);
275 break;
276 case 2:
277 queue = IEEE80211_TX_QUEUE_DATA1;
278 if (acm) {
279 local->wmm_acm |= BIT(4) | BIT(5);
281 break;
282 case 3:
283 queue = IEEE80211_TX_QUEUE_DATA0;
284 if (acm) {
285 local->wmm_acm |= BIT(6) | BIT(7);
287 break;
288 case 0:
289 default:
290 queue = IEEE80211_TX_QUEUE_DATA2;
291 if (acm) {
292 local->wmm_acm |= BIT(1) | BIT(2);
294 break;
297 params.aifs = pos[0] & 0x0f;
298 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
299 params.cw_min = ecw2cw(pos[1] & 0x0f);
300 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
301 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
302 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
303 "cWmin=%d cWmax=%d burst=%d\n",
304 dev->name, queue, aci, acm, params.aifs, params.cw_min,
305 params.cw_max, params.burst_time);
306 /* TODO: handle ACM (block TX, fallback to next lowest allowed
307 * AC for now) */
308 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
309 printk(KERN_DEBUG "%s: failed to set TX queue "
310 "parameters for queue %d\n", dev->name, queue);
316 static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
318 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
319 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
320 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
322 if (use_protection != sdata->use_protection) {
323 if (net_ratelimit()) {
324 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
325 MAC_FMT ")\n",
326 dev->name,
327 use_protection ? "enabled" : "disabled",
328 MAC_ARG(ifsta->bssid));
330 sdata->use_protection = use_protection;
335 static void ieee80211_sta_send_associnfo(struct net_device *dev,
336 struct ieee80211_if_sta *ifsta)
338 char *buf;
339 size_t len;
340 int i;
341 union iwreq_data wrqu;
343 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
344 return;
346 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
347 ifsta->assocresp_ies_len), GFP_ATOMIC);
348 if (!buf)
349 return;
351 len = sprintf(buf, "ASSOCINFO(");
352 if (ifsta->assocreq_ies) {
353 len += sprintf(buf + len, "ReqIEs=");
354 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
355 len += sprintf(buf + len, "%02x",
356 ifsta->assocreq_ies[i]);
359 if (ifsta->assocresp_ies) {
360 if (ifsta->assocreq_ies)
361 len += sprintf(buf + len, " ");
362 len += sprintf(buf + len, "RespIEs=");
363 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
364 len += sprintf(buf + len, "%02x",
365 ifsta->assocresp_ies[i]);
368 len += sprintf(buf + len, ")");
370 if (len > IW_CUSTOM_MAX) {
371 len = sprintf(buf, "ASSOCRESPIE=");
372 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
373 len += sprintf(buf + len, "%02x",
374 ifsta->assocresp_ies[i]);
378 memset(&wrqu, 0, sizeof(wrqu));
379 wrqu.data.length = len;
380 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
382 kfree(buf);
386 static void ieee80211_set_associated(struct net_device *dev,
387 struct ieee80211_if_sta *ifsta, int assoc)
389 union iwreq_data wrqu;
390 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
392 if (ifsta->associated == assoc)
393 return;
395 ifsta->associated = assoc;
397 if (assoc) {
398 struct ieee80211_sub_if_data *sdata;
399 struct ieee80211_sta_bss *bss;
400 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
401 if (sdata->type != IEEE80211_IF_TYPE_STA)
402 return;
404 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
405 if (bss) {
406 if (bss->has_erp_value)
407 ieee80211_handle_erp_ie(dev, bss->erp_value);
408 ieee80211_rx_bss_put(dev, bss);
411 netif_carrier_on(dev);
412 ifsta->prev_bssid_set = 1;
413 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
414 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
415 ieee80211_sta_send_associnfo(dev, ifsta);
416 } else {
417 netif_carrier_off(dev);
418 sdata->use_protection = 0;
419 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
421 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
422 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
423 ifsta->last_probe = jiffies;
426 static void ieee80211_set_disassoc(struct net_device *dev,
427 struct ieee80211_if_sta *ifsta, int deauth)
429 if (deauth)
430 ifsta->auth_tries = 0;
431 ifsta->assoc_tries = 0;
432 ieee80211_set_associated(dev, ifsta, 0);
435 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
436 int encrypt)
438 struct ieee80211_sub_if_data *sdata;
439 struct ieee80211_tx_packet_data *pkt_data;
441 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
442 skb->dev = sdata->local->mdev;
443 skb_set_mac_header(skb, 0);
444 skb_set_network_header(skb, 0);
445 skb_set_transport_header(skb, 0);
447 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
448 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
449 pkt_data->ifindex = sdata->dev->ifindex;
450 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
451 pkt_data->do_not_encrypt = !encrypt;
453 dev_queue_xmit(skb);
457 static void ieee80211_send_auth(struct net_device *dev,
458 struct ieee80211_if_sta *ifsta,
459 int transaction, u8 *extra, size_t extra_len,
460 int encrypt)
462 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
463 struct sk_buff *skb;
464 struct ieee80211_mgmt *mgmt;
466 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
467 sizeof(*mgmt) + 6 + extra_len);
468 if (!skb) {
469 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
470 "frame\n", dev->name);
471 return;
473 skb_reserve(skb, local->hw.extra_tx_headroom);
475 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
476 memset(mgmt, 0, 24 + 6);
477 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
478 IEEE80211_STYPE_AUTH);
479 if (encrypt)
480 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
481 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
482 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
483 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
484 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
485 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
486 ifsta->auth_transaction = transaction + 1;
487 mgmt->u.auth.status_code = cpu_to_le16(0);
488 if (extra)
489 memcpy(skb_put(skb, extra_len), extra, extra_len);
491 ieee80211_sta_tx(dev, skb, encrypt);
495 static void ieee80211_authenticate(struct net_device *dev,
496 struct ieee80211_if_sta *ifsta)
498 ifsta->auth_tries++;
499 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
500 printk(KERN_DEBUG "%s: authentication with AP " MAC_FMT
501 " timed out\n",
502 dev->name, MAC_ARG(ifsta->bssid));
503 ifsta->state = IEEE80211_DISABLED;
504 return;
507 ifsta->state = IEEE80211_AUTHENTICATE;
508 printk(KERN_DEBUG "%s: authenticate with AP " MAC_FMT "\n",
509 dev->name, MAC_ARG(ifsta->bssid));
511 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
513 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
517 static void ieee80211_send_assoc(struct net_device *dev,
518 struct ieee80211_if_sta *ifsta)
520 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
521 struct ieee80211_hw_mode *mode;
522 struct sk_buff *skb;
523 struct ieee80211_mgmt *mgmt;
524 u8 *pos, *ies;
525 int i, len;
526 u16 capab;
527 struct ieee80211_sta_bss *bss;
528 int wmm = 0;
530 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
531 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
532 ifsta->ssid_len);
533 if (!skb) {
534 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
535 "frame\n", dev->name);
536 return;
538 skb_reserve(skb, local->hw.extra_tx_headroom);
540 mode = local->oper_hw_mode;
541 capab = ifsta->capab;
542 if (mode->mode == MODE_IEEE80211G) {
543 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
544 WLAN_CAPABILITY_SHORT_PREAMBLE;
546 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
547 if (bss) {
548 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
549 capab |= WLAN_CAPABILITY_PRIVACY;
550 if (bss->wmm_ie) {
551 wmm = 1;
553 ieee80211_rx_bss_put(dev, bss);
556 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
557 memset(mgmt, 0, 24);
558 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
559 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
560 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
562 if (ifsta->prev_bssid_set) {
563 skb_put(skb, 10);
564 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
565 IEEE80211_STYPE_REASSOC_REQ);
566 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
567 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
568 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
569 ETH_ALEN);
570 } else {
571 skb_put(skb, 4);
572 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
573 IEEE80211_STYPE_ASSOC_REQ);
574 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
575 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
578 /* SSID */
579 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
580 *pos++ = WLAN_EID_SSID;
581 *pos++ = ifsta->ssid_len;
582 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
584 len = mode->num_rates;
585 if (len > 8)
586 len = 8;
587 pos = skb_put(skb, len + 2);
588 *pos++ = WLAN_EID_SUPP_RATES;
589 *pos++ = len;
590 for (i = 0; i < len; i++) {
591 int rate = mode->rates[i].rate;
592 if (mode->mode == MODE_ATHEROS_TURBO)
593 rate /= 2;
594 *pos++ = (u8) (rate / 5);
597 if (mode->num_rates > len) {
598 pos = skb_put(skb, mode->num_rates - len + 2);
599 *pos++ = WLAN_EID_EXT_SUPP_RATES;
600 *pos++ = mode->num_rates - len;
601 for (i = len; i < mode->num_rates; i++) {
602 int rate = mode->rates[i].rate;
603 if (mode->mode == MODE_ATHEROS_TURBO)
604 rate /= 2;
605 *pos++ = (u8) (rate / 5);
609 if (ifsta->extra_ie) {
610 pos = skb_put(skb, ifsta->extra_ie_len);
611 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
614 if (wmm && ifsta->wmm_enabled) {
615 pos = skb_put(skb, 9);
616 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
617 *pos++ = 7; /* len */
618 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
619 *pos++ = 0x50;
620 *pos++ = 0xf2;
621 *pos++ = 2; /* WME */
622 *pos++ = 0; /* WME info */
623 *pos++ = 1; /* WME ver */
624 *pos++ = 0;
627 kfree(ifsta->assocreq_ies);
628 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
629 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_ATOMIC);
630 if (ifsta->assocreq_ies)
631 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
633 ieee80211_sta_tx(dev, skb, 0);
637 static void ieee80211_send_deauth(struct net_device *dev,
638 struct ieee80211_if_sta *ifsta, u16 reason)
640 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
641 struct sk_buff *skb;
642 struct ieee80211_mgmt *mgmt;
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 deauth "
647 "frame\n", dev->name);
648 return;
650 skb_reserve(skb, local->hw.extra_tx_headroom);
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_DEAUTH);
659 skb_put(skb, 2);
660 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
662 ieee80211_sta_tx(dev, skb, 0);
666 static void ieee80211_send_disassoc(struct net_device *dev,
667 struct ieee80211_if_sta *ifsta, u16 reason)
669 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
670 struct sk_buff *skb;
671 struct ieee80211_mgmt *mgmt;
673 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
674 if (!skb) {
675 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
676 "frame\n", dev->name);
677 return;
679 skb_reserve(skb, local->hw.extra_tx_headroom);
681 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
682 memset(mgmt, 0, 24);
683 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
684 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
685 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
686 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
687 IEEE80211_STYPE_DISASSOC);
688 skb_put(skb, 2);
689 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
691 ieee80211_sta_tx(dev, skb, 0);
695 static int ieee80211_privacy_mismatch(struct net_device *dev,
696 struct ieee80211_if_sta *ifsta)
698 struct ieee80211_sta_bss *bss;
699 int res = 0;
701 if (!ifsta || ifsta->mixed_cell ||
702 ifsta->key_mgmt != IEEE80211_KEY_MGMT_NONE)
703 return 0;
705 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
706 if (!bss)
707 return 0;
709 if (ieee80211_sta_wep_configured(dev) !=
710 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
711 res = 1;
713 ieee80211_rx_bss_put(dev, bss);
715 return res;
719 static void ieee80211_associate(struct net_device *dev,
720 struct ieee80211_if_sta *ifsta)
722 ifsta->assoc_tries++;
723 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
724 printk(KERN_DEBUG "%s: association with AP " MAC_FMT
725 " timed out\n",
726 dev->name, MAC_ARG(ifsta->bssid));
727 ifsta->state = IEEE80211_DISABLED;
728 return;
731 ifsta->state = IEEE80211_ASSOCIATE;
732 printk(KERN_DEBUG "%s: associate with AP " MAC_FMT "\n",
733 dev->name, MAC_ARG(ifsta->bssid));
734 if (ieee80211_privacy_mismatch(dev, ifsta)) {
735 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
736 "mixed-cell disabled - abort association\n", dev->name);
737 ifsta->state = IEEE80211_DISABLED;
738 return;
741 ieee80211_send_assoc(dev, ifsta);
743 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
747 static void ieee80211_associated(struct net_device *dev,
748 struct ieee80211_if_sta *ifsta)
750 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
751 struct sta_info *sta;
752 int disassoc;
754 /* TODO: start monitoring current AP signal quality and number of
755 * missed beacons. Scan other channels every now and then and search
756 * for better APs. */
757 /* TODO: remove expired BSSes */
759 ifsta->state = IEEE80211_ASSOCIATED;
761 sta = sta_info_get(local, ifsta->bssid);
762 if (!sta) {
763 printk(KERN_DEBUG "%s: No STA entry for own AP " MAC_FMT "\n",
764 dev->name, MAC_ARG(ifsta->bssid));
765 disassoc = 1;
766 } else {
767 disassoc = 0;
768 if (time_after(jiffies,
769 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
770 if (ifsta->probereq_poll) {
771 printk(KERN_DEBUG "%s: No ProbeResp from "
772 "current AP " MAC_FMT " - assume out of "
773 "range\n",
774 dev->name, MAC_ARG(ifsta->bssid));
775 disassoc = 1;
776 sta_info_free(sta, 0);
777 ifsta->probereq_poll = 0;
778 } else {
779 ieee80211_send_probe_req(dev, ifsta->bssid,
780 local->scan_ssid,
781 local->scan_ssid_len);
782 ifsta->probereq_poll = 1;
784 } else {
785 ifsta->probereq_poll = 0;
786 if (time_after(jiffies, ifsta->last_probe +
787 IEEE80211_PROBE_INTERVAL)) {
788 ifsta->last_probe = jiffies;
789 ieee80211_send_probe_req(dev, ifsta->bssid,
790 ifsta->ssid,
791 ifsta->ssid_len);
794 sta_info_put(sta);
796 if (disassoc) {
797 union iwreq_data wrqu;
798 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
799 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
800 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
801 mod_timer(&ifsta->timer, jiffies +
802 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
803 } else {
804 mod_timer(&ifsta->timer, jiffies +
805 IEEE80211_MONITORING_INTERVAL);
810 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
811 u8 *ssid, size_t ssid_len)
813 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
814 struct ieee80211_hw_mode *mode;
815 struct sk_buff *skb;
816 struct ieee80211_mgmt *mgmt;
817 u8 *pos, *supp_rates, *esupp_rates = NULL;
818 int i;
820 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
821 if (!skb) {
822 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
823 "request\n", dev->name);
824 return;
826 skb_reserve(skb, local->hw.extra_tx_headroom);
828 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
829 memset(mgmt, 0, 24);
830 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
831 IEEE80211_STYPE_PROBE_REQ);
832 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
833 if (dst) {
834 memcpy(mgmt->da, dst, ETH_ALEN);
835 memcpy(mgmt->bssid, dst, ETH_ALEN);
836 } else {
837 memset(mgmt->da, 0xff, ETH_ALEN);
838 memset(mgmt->bssid, 0xff, ETH_ALEN);
840 pos = skb_put(skb, 2 + ssid_len);
841 *pos++ = WLAN_EID_SSID;
842 *pos++ = ssid_len;
843 memcpy(pos, ssid, ssid_len);
845 supp_rates = skb_put(skb, 2);
846 supp_rates[0] = WLAN_EID_SUPP_RATES;
847 supp_rates[1] = 0;
848 mode = local->oper_hw_mode;
849 for (i = 0; i < mode->num_rates; i++) {
850 struct ieee80211_rate *rate = &mode->rates[i];
851 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
852 continue;
853 if (esupp_rates) {
854 pos = skb_put(skb, 1);
855 esupp_rates[1]++;
856 } else if (supp_rates[1] == 8) {
857 esupp_rates = skb_put(skb, 3);
858 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
859 esupp_rates[1] = 1;
860 pos = &esupp_rates[2];
861 } else {
862 pos = skb_put(skb, 1);
863 supp_rates[1]++;
865 if (mode->mode == MODE_ATHEROS_TURBO)
866 *pos = rate->rate / 10;
867 else
868 *pos = rate->rate / 5;
871 ieee80211_sta_tx(dev, skb, 0);
875 static int ieee80211_sta_wep_configured(struct net_device *dev)
877 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
878 if (!sdata || !sdata->default_key ||
879 sdata->default_key->alg != ALG_WEP)
880 return 0;
881 return 1;
885 static void ieee80211_auth_completed(struct net_device *dev,
886 struct ieee80211_if_sta *ifsta)
888 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
889 ifsta->authenticated = 1;
890 ieee80211_associate(dev, ifsta);
894 static void ieee80211_auth_challenge(struct net_device *dev,
895 struct ieee80211_if_sta *ifsta,
896 struct ieee80211_mgmt *mgmt,
897 size_t len)
899 u8 *pos;
900 struct ieee802_11_elems elems;
902 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
903 pos = mgmt->u.auth.variable;
904 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
905 == ParseFailed) {
906 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
907 dev->name);
908 return;
910 if (!elems.challenge) {
911 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
912 "frame\n", dev->name);
913 return;
915 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
916 elems.challenge_len + 2, 1);
920 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
921 struct ieee80211_if_sta *ifsta,
922 struct ieee80211_mgmt *mgmt,
923 size_t len)
925 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
926 u16 auth_alg, auth_transaction, status_code;
928 if (ifsta->state != IEEE80211_AUTHENTICATE &&
929 sdata->type != IEEE80211_IF_TYPE_IBSS) {
930 printk(KERN_DEBUG "%s: authentication frame received from "
931 MAC_FMT ", but not in authenticate state - ignored\n",
932 dev->name, MAC_ARG(mgmt->sa));
933 return;
936 if (len < 24 + 6) {
937 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
938 "received from " MAC_FMT " - ignored\n",
939 dev->name, len, MAC_ARG(mgmt->sa));
940 return;
943 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
944 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
945 printk(KERN_DEBUG "%s: authentication frame received from "
946 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
947 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
948 MAC_ARG(mgmt->bssid));
949 return;
952 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
953 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
954 printk(KERN_DEBUG "%s: authentication frame received from "
955 "unknown BSSID (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
956 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
957 MAC_ARG(mgmt->bssid));
958 return;
961 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
962 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
963 status_code = le16_to_cpu(mgmt->u.auth.status_code);
965 printk(KERN_DEBUG "%s: RX authentication from " MAC_FMT " (alg=%d "
966 "transaction=%d status=%d)\n",
967 dev->name, MAC_ARG(mgmt->sa), auth_alg,
968 auth_transaction, status_code);
970 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
971 /* IEEE 802.11 standard does not require authentication in IBSS
972 * networks and most implementations do not seem to use it.
973 * However, try to reply to authentication attempts if someone
974 * has actually implemented this.
975 * TODO: Could implement shared key authentication. */
976 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
977 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
978 "frame (alg=%d transaction=%d)\n",
979 dev->name, auth_alg, auth_transaction);
980 return;
982 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
985 if (auth_alg != ifsta->auth_alg ||
986 auth_transaction != ifsta->auth_transaction) {
987 printk(KERN_DEBUG "%s: unexpected authentication frame "
988 "(alg=%d transaction=%d)\n",
989 dev->name, auth_alg, auth_transaction);
990 return;
993 if (status_code != WLAN_STATUS_SUCCESS) {
994 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
995 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
996 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
997 u8 algs[3];
998 const int num_algs = ARRAY_SIZE(algs);
999 int i, pos;
1000 algs[0] = algs[1] = algs[2] = 0xff;
1001 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1002 algs[0] = WLAN_AUTH_OPEN;
1003 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1004 algs[1] = WLAN_AUTH_SHARED_KEY;
1005 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1006 algs[2] = WLAN_AUTH_LEAP;
1007 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1008 pos = 0;
1009 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1010 pos = 1;
1011 else
1012 pos = 2;
1013 for (i = 0; i < num_algs; i++) {
1014 pos++;
1015 if (pos >= num_algs)
1016 pos = 0;
1017 if (algs[pos] == ifsta->auth_alg ||
1018 algs[pos] == 0xff)
1019 continue;
1020 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1021 !ieee80211_sta_wep_configured(dev))
1022 continue;
1023 ifsta->auth_alg = algs[pos];
1024 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1025 "next try\n",
1026 dev->name, ifsta->auth_alg);
1027 break;
1030 return;
1033 switch (ifsta->auth_alg) {
1034 case WLAN_AUTH_OPEN:
1035 case WLAN_AUTH_LEAP:
1036 ieee80211_auth_completed(dev, ifsta);
1037 break;
1038 case WLAN_AUTH_SHARED_KEY:
1039 if (ifsta->auth_transaction == 4)
1040 ieee80211_auth_completed(dev, ifsta);
1041 else
1042 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1043 break;
1048 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1049 struct ieee80211_if_sta *ifsta,
1050 struct ieee80211_mgmt *mgmt,
1051 size_t len)
1053 u16 reason_code;
1055 if (len < 24 + 2) {
1056 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1057 "received from " MAC_FMT " - ignored\n",
1058 dev->name, len, MAC_ARG(mgmt->sa));
1059 return;
1062 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1063 printk(KERN_DEBUG "%s: deauthentication frame received from "
1064 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1065 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1066 MAC_ARG(mgmt->bssid));
1067 return;
1070 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1072 printk(KERN_DEBUG "%s: RX deauthentication from " MAC_FMT
1073 " (reason=%d)\n",
1074 dev->name, MAC_ARG(mgmt->sa), reason_code);
1076 if (ifsta->authenticated) {
1077 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1080 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1081 ifsta->state == IEEE80211_ASSOCIATE ||
1082 ifsta->state == IEEE80211_ASSOCIATED) {
1083 ifsta->state = IEEE80211_AUTHENTICATE;
1084 mod_timer(&ifsta->timer, jiffies +
1085 IEEE80211_RETRY_AUTH_INTERVAL);
1088 ieee80211_set_disassoc(dev, ifsta, 1);
1089 ifsta->authenticated = 0;
1093 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1094 struct ieee80211_if_sta *ifsta,
1095 struct ieee80211_mgmt *mgmt,
1096 size_t len)
1098 u16 reason_code;
1100 if (len < 24 + 2) {
1101 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1102 "received from " MAC_FMT " - ignored\n",
1103 dev->name, len, MAC_ARG(mgmt->sa));
1104 return;
1107 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1108 printk(KERN_DEBUG "%s: disassociation frame received from "
1109 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1110 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1111 MAC_ARG(mgmt->bssid));
1112 return;
1115 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1117 printk(KERN_DEBUG "%s: RX disassociation from " MAC_FMT
1118 " (reason=%d)\n",
1119 dev->name, MAC_ARG(mgmt->sa), reason_code);
1121 if (ifsta->associated)
1122 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1124 if (ifsta->state == IEEE80211_ASSOCIATED) {
1125 ifsta->state = IEEE80211_ASSOCIATE;
1126 mod_timer(&ifsta->timer, jiffies +
1127 IEEE80211_RETRY_AUTH_INTERVAL);
1130 ieee80211_set_disassoc(dev, ifsta, 0);
1134 static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1135 struct ieee80211_if_sta *ifsta,
1136 struct ieee80211_mgmt *mgmt,
1137 size_t len,
1138 int reassoc)
1140 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1141 struct ieee80211_hw_mode *mode;
1142 struct sta_info *sta;
1143 u32 rates;
1144 u16 capab_info, status_code, aid;
1145 struct ieee802_11_elems elems;
1146 u8 *pos;
1147 int i, j;
1149 /* AssocResp and ReassocResp have identical structure, so process both
1150 * of them in this function. */
1152 if (ifsta->state != IEEE80211_ASSOCIATE) {
1153 printk(KERN_DEBUG "%s: association frame received from "
1154 MAC_FMT ", but not in associate state - ignored\n",
1155 dev->name, MAC_ARG(mgmt->sa));
1156 return;
1159 if (len < 24 + 6) {
1160 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1161 "received from " MAC_FMT " - ignored\n",
1162 dev->name, len, MAC_ARG(mgmt->sa));
1163 return;
1166 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1167 printk(KERN_DEBUG "%s: association frame received from "
1168 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1169 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1170 MAC_ARG(mgmt->bssid));
1171 return;
1174 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1175 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1176 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1177 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1178 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1179 "set\n", dev->name, aid);
1180 aid &= ~(BIT(15) | BIT(14));
1182 printk(KERN_DEBUG "%s: RX %sssocResp from " MAC_FMT " (capab=0x%x "
1183 "status=%d aid=%d)\n",
1184 dev->name, reassoc ? "Rea" : "A", MAC_ARG(mgmt->sa),
1185 capab_info, status_code, aid);
1187 if (status_code != WLAN_STATUS_SUCCESS) {
1188 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1189 dev->name, status_code);
1190 if (status_code == WLAN_STATUS_REASSOC_NO_ASSOC)
1191 ifsta->prev_bssid_set = 0;
1192 return;
1195 pos = mgmt->u.assoc_resp.variable;
1196 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1197 == ParseFailed) {
1198 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1199 dev->name);
1200 return;
1203 if (!elems.supp_rates) {
1204 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1205 dev->name);
1206 return;
1209 /* it probably doesn't, but if the frame includes an ERP value then
1210 * update our stored copy */
1211 if (elems.erp_info && elems.erp_info_len >= 1) {
1212 struct ieee80211_sta_bss *bss
1213 = ieee80211_rx_bss_get(dev, ifsta->bssid);
1214 if (bss) {
1215 bss->erp_value = elems.erp_info[0];
1216 bss->has_erp_value = 1;
1217 ieee80211_rx_bss_put(dev, bss);
1221 printk(KERN_DEBUG "%s: associated\n", dev->name);
1222 ifsta->aid = aid;
1223 ifsta->ap_capab = capab_info;
1225 kfree(ifsta->assocresp_ies);
1226 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1227 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_ATOMIC);
1228 if (ifsta->assocresp_ies)
1229 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1231 ieee80211_set_associated(dev, ifsta, 1);
1233 /* Add STA entry for the AP */
1234 sta = sta_info_get(local, ifsta->bssid);
1235 if (!sta) {
1236 struct ieee80211_sta_bss *bss;
1237 sta = sta_info_add(local, dev, ifsta->bssid, GFP_ATOMIC);
1238 if (!sta) {
1239 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1240 " AP\n", dev->name);
1241 return;
1243 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1244 if (bss) {
1245 sta->last_rssi = bss->rssi;
1246 sta->last_signal = bss->signal;
1247 sta->last_noise = bss->noise;
1248 ieee80211_rx_bss_put(dev, bss);
1252 sta->dev = dev;
1253 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
1254 sta->assoc_ap = 1;
1256 rates = 0;
1257 mode = local->oper_hw_mode;
1258 for (i = 0; i < elems.supp_rates_len; i++) {
1259 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1260 if (mode->mode == MODE_ATHEROS_TURBO)
1261 rate *= 2;
1262 for (j = 0; j < mode->num_rates; j++)
1263 if (mode->rates[j].rate == rate)
1264 rates |= BIT(j);
1266 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1267 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1268 if (mode->mode == MODE_ATHEROS_TURBO)
1269 rate *= 2;
1270 for (j = 0; j < mode->num_rates; j++)
1271 if (mode->rates[j].rate == rate)
1272 rates |= BIT(j);
1274 sta->supp_rates = rates;
1276 rate_control_rate_init(sta, local);
1278 if (elems.wmm_param && ifsta->wmm_enabled) {
1279 sta->flags |= WLAN_STA_WME;
1280 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1281 elems.wmm_param_len);
1285 sta_info_put(sta);
1287 ieee80211_associated(dev, ifsta);
1291 /* Caller must hold local->sta_bss_lock */
1292 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1293 struct ieee80211_sta_bss *bss)
1295 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1296 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1297 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1301 /* Caller must hold local->sta_bss_lock */
1302 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1303 struct ieee80211_sta_bss *bss)
1305 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1306 struct ieee80211_sta_bss *b, *prev = NULL;
1307 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1308 while (b) {
1309 if (b == bss) {
1310 if (!prev)
1311 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1312 bss->hnext;
1313 else
1314 prev->hnext = bss->hnext;
1315 break;
1317 prev = b;
1318 b = b->hnext;
1323 static struct ieee80211_sta_bss *
1324 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1326 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1327 struct ieee80211_sta_bss *bss;
1329 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1330 if (!bss)
1331 return NULL;
1332 atomic_inc(&bss->users);
1333 atomic_inc(&bss->users);
1334 memcpy(bss->bssid, bssid, ETH_ALEN);
1336 spin_lock_bh(&local->sta_bss_lock);
1337 /* TODO: order by RSSI? */
1338 list_add_tail(&bss->list, &local->sta_bss_list);
1339 __ieee80211_rx_bss_hash_add(dev, bss);
1340 spin_unlock_bh(&local->sta_bss_lock);
1341 return bss;
1345 static struct ieee80211_sta_bss *
1346 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1348 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1349 struct ieee80211_sta_bss *bss;
1351 spin_lock_bh(&local->sta_bss_lock);
1352 bss = local->sta_bss_hash[STA_HASH(bssid)];
1353 while (bss) {
1354 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1355 atomic_inc(&bss->users);
1356 break;
1358 bss = bss->hnext;
1360 spin_unlock_bh(&local->sta_bss_lock);
1361 return bss;
1365 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1367 kfree(bss->wpa_ie);
1368 kfree(bss->rsn_ie);
1369 kfree(bss->wmm_ie);
1370 kfree(bss);
1374 static void ieee80211_rx_bss_put(struct net_device *dev,
1375 struct ieee80211_sta_bss *bss)
1377 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1378 if (!atomic_dec_and_test(&bss->users))
1379 return;
1381 spin_lock_bh(&local->sta_bss_lock);
1382 __ieee80211_rx_bss_hash_del(dev, bss);
1383 list_del(&bss->list);
1384 spin_unlock_bh(&local->sta_bss_lock);
1385 ieee80211_rx_bss_free(bss);
1389 void ieee80211_rx_bss_list_init(struct net_device *dev)
1391 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1392 spin_lock_init(&local->sta_bss_lock);
1393 INIT_LIST_HEAD(&local->sta_bss_list);
1397 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1399 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1400 struct ieee80211_sta_bss *bss, *tmp;
1402 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1403 ieee80211_rx_bss_put(dev, bss);
1407 static void ieee80211_rx_bss_info(struct net_device *dev,
1408 struct ieee80211_mgmt *mgmt,
1409 size_t len,
1410 struct ieee80211_rx_status *rx_status,
1411 int beacon)
1413 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1414 struct ieee802_11_elems elems;
1415 size_t baselen;
1416 int channel, invalid = 0, clen;
1417 struct ieee80211_sta_bss *bss;
1418 struct sta_info *sta;
1419 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1420 u64 timestamp;
1422 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1423 return; /* ignore ProbeResp to foreign address */
1425 #if 0
1426 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1427 dev->name, beacon ? "Beacon" : "Probe Response",
1428 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1429 #endif
1431 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1432 if (baselen > len)
1433 return;
1435 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1437 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1438 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1439 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1440 static unsigned long last_tsf_debug = 0;
1441 u64 tsf;
1442 if (local->ops->get_tsf)
1443 tsf = local->ops->get_tsf(local_to_hw(local));
1444 else
1445 tsf = -1LLU;
1446 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1447 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1448 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1449 "@%lu\n",
1450 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1451 (unsigned long long)tsf,
1452 (unsigned long long)timestamp,
1453 (unsigned long long)(tsf - timestamp),
1454 jiffies);
1455 last_tsf_debug = jiffies;
1457 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1460 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1461 &elems) == ParseFailed)
1462 invalid = 1;
1464 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1465 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1466 (sta = sta_info_get(local, mgmt->sa))) {
1467 struct ieee80211_hw_mode *mode;
1468 struct ieee80211_rate *rates;
1469 size_t num_rates;
1470 u32 supp_rates, prev_rates;
1471 int i, j;
1473 mode = local->sta_scanning ?
1474 local->scan_hw_mode : local->oper_hw_mode;
1475 rates = mode->rates;
1476 num_rates = mode->num_rates;
1478 supp_rates = 0;
1479 for (i = 0; i < elems.supp_rates_len +
1480 elems.ext_supp_rates_len; i++) {
1481 u8 rate = 0;
1482 int own_rate;
1483 if (i < elems.supp_rates_len)
1484 rate = elems.supp_rates[i];
1485 else if (elems.ext_supp_rates)
1486 rate = elems.ext_supp_rates
1487 [i - elems.supp_rates_len];
1488 own_rate = 5 * (rate & 0x7f);
1489 if (mode->mode == MODE_ATHEROS_TURBO)
1490 own_rate *= 2;
1491 for (j = 0; j < num_rates; j++)
1492 if (rates[j].rate == own_rate)
1493 supp_rates |= BIT(j);
1496 prev_rates = sta->supp_rates;
1497 sta->supp_rates &= supp_rates;
1498 if (sta->supp_rates == 0) {
1499 /* No matching rates - this should not really happen.
1500 * Make sure that at least one rate is marked
1501 * supported to avoid issues with TX rate ctrl. */
1502 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1504 if (sta->supp_rates != prev_rates) {
1505 printk(KERN_DEBUG "%s: updated supp_rates set for "
1506 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1507 "0x%x)\n",
1508 dev->name, MAC_ARG(sta->addr), prev_rates,
1509 supp_rates, sta->supp_rates);
1511 sta_info_put(sta);
1514 if (!elems.ssid)
1515 return;
1517 if (elems.ds_params && elems.ds_params_len == 1)
1518 channel = elems.ds_params[0];
1519 else
1520 channel = rx_status->channel;
1522 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1523 if (!bss) {
1524 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1525 if (!bss)
1526 return;
1527 } else {
1528 #if 0
1529 /* TODO: order by RSSI? */
1530 spin_lock_bh(&local->sta_bss_lock);
1531 list_move_tail(&bss->list, &local->sta_bss_list);
1532 spin_unlock_bh(&local->sta_bss_lock);
1533 #endif
1536 if (bss->probe_resp && beacon) {
1537 /* Do not allow beacon to override data from Probe Response. */
1538 ieee80211_rx_bss_put(dev, bss);
1539 return;
1542 /* save the ERP value so that it is available at association time */
1543 if (elems.erp_info && elems.erp_info_len >= 1) {
1544 bss->erp_value = elems.erp_info[0];
1545 bss->has_erp_value = 1;
1548 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1549 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1550 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1551 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1552 bss->ssid_len = elems.ssid_len;
1555 bss->supp_rates_len = 0;
1556 if (elems.supp_rates) {
1557 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1558 if (clen > elems.supp_rates_len)
1559 clen = elems.supp_rates_len;
1560 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1561 clen);
1562 bss->supp_rates_len += clen;
1564 if (elems.ext_supp_rates) {
1565 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1566 if (clen > elems.ext_supp_rates_len)
1567 clen = elems.ext_supp_rates_len;
1568 memcpy(&bss->supp_rates[bss->supp_rates_len],
1569 elems.ext_supp_rates, clen);
1570 bss->supp_rates_len += clen;
1573 if (elems.wpa &&
1574 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1575 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1576 kfree(bss->wpa_ie);
1577 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1578 if (bss->wpa_ie) {
1579 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1580 bss->wpa_ie_len = elems.wpa_len + 2;
1581 } else
1582 bss->wpa_ie_len = 0;
1583 } else if (!elems.wpa && bss->wpa_ie) {
1584 kfree(bss->wpa_ie);
1585 bss->wpa_ie = NULL;
1586 bss->wpa_ie_len = 0;
1589 if (elems.rsn &&
1590 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1591 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1592 kfree(bss->rsn_ie);
1593 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1594 if (bss->rsn_ie) {
1595 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1596 bss->rsn_ie_len = elems.rsn_len + 2;
1597 } else
1598 bss->rsn_ie_len = 0;
1599 } else if (!elems.rsn && bss->rsn_ie) {
1600 kfree(bss->rsn_ie);
1601 bss->rsn_ie = NULL;
1602 bss->rsn_ie_len = 0;
1605 if (elems.wmm_param &&
1606 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1607 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1608 kfree(bss->wmm_ie);
1609 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1610 if (bss->wmm_ie) {
1611 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1612 elems.wmm_param_len + 2);
1613 bss->wmm_ie_len = elems.wmm_param_len + 2;
1614 } else
1615 bss->wmm_ie_len = 0;
1616 } else if (!elems.wmm_param && bss->wmm_ie) {
1617 kfree(bss->wmm_ie);
1618 bss->wmm_ie = NULL;
1619 bss->wmm_ie_len = 0;
1623 bss->hw_mode = rx_status->phymode;
1624 bss->channel = channel;
1625 bss->freq = rx_status->freq;
1626 if (channel != rx_status->channel &&
1627 (bss->hw_mode == MODE_IEEE80211G ||
1628 bss->hw_mode == MODE_IEEE80211B) &&
1629 channel >= 1 && channel <= 14) {
1630 static const int freq_list[] = {
1631 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1632 2447, 2452, 2457, 2462, 2467, 2472, 2484
1634 /* IEEE 802.11g/b mode can receive packets from neighboring
1635 * channels, so map the channel into frequency. */
1636 bss->freq = freq_list[channel - 1];
1638 bss->timestamp = timestamp;
1639 bss->last_update = jiffies;
1640 bss->rssi = rx_status->ssi;
1641 bss->signal = rx_status->signal;
1642 bss->noise = rx_status->noise;
1643 if (!beacon)
1644 bss->probe_resp++;
1645 ieee80211_rx_bss_put(dev, bss);
1649 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1650 struct ieee80211_mgmt *mgmt,
1651 size_t len,
1652 struct ieee80211_rx_status *rx_status)
1654 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1658 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1659 struct ieee80211_mgmt *mgmt,
1660 size_t len,
1661 struct ieee80211_rx_status *rx_status)
1663 struct ieee80211_sub_if_data *sdata;
1664 struct ieee80211_if_sta *ifsta;
1665 size_t baselen;
1666 struct ieee802_11_elems elems;
1668 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1670 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1671 if (sdata->type != IEEE80211_IF_TYPE_STA)
1672 return;
1673 ifsta = &sdata->u.sta;
1675 if (!ifsta->associated ||
1676 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1677 return;
1679 /* Process beacon from the current BSS */
1680 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1681 if (baselen > len)
1682 return;
1684 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1685 &elems) == ParseFailed)
1686 return;
1688 if (elems.erp_info && elems.erp_info_len >= 1)
1689 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
1691 if (elems.wmm_param && ifsta->wmm_enabled) {
1692 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1693 elems.wmm_param_len);
1698 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1699 struct ieee80211_if_sta *ifsta,
1700 struct ieee80211_mgmt *mgmt,
1701 size_t len,
1702 struct ieee80211_rx_status *rx_status)
1704 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1705 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1706 int tx_last_beacon;
1707 struct sk_buff *skb;
1708 struct ieee80211_mgmt *resp;
1709 u8 *pos, *end;
1711 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1712 ifsta->state != IEEE80211_IBSS_JOINED ||
1713 len < 24 + 2 || !ifsta->probe_resp)
1714 return;
1716 if (local->ops->tx_last_beacon)
1717 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1718 else
1719 tx_last_beacon = 1;
1721 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1722 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1723 MAC_FMT " (tx_last_beacon=%d)\n",
1724 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1725 MAC_ARG(mgmt->bssid), tx_last_beacon);
1726 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1728 if (!tx_last_beacon)
1729 return;
1731 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1732 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1733 return;
1735 end = ((u8 *) mgmt) + len;
1736 pos = mgmt->u.probe_req.variable;
1737 if (pos[0] != WLAN_EID_SSID ||
1738 pos + 2 + pos[1] > end) {
1739 if (net_ratelimit()) {
1740 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1741 "from " MAC_FMT "\n",
1742 dev->name, MAC_ARG(mgmt->sa));
1744 return;
1746 if (pos[1] != 0 &&
1747 (pos[1] != ifsta->ssid_len ||
1748 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1749 /* Ignore ProbeReq for foreign SSID */
1750 return;
1753 /* Reply with ProbeResp */
1754 skb = skb_copy(ifsta->probe_resp, GFP_ATOMIC);
1755 if (!skb)
1756 return;
1758 resp = (struct ieee80211_mgmt *) skb->data;
1759 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1760 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1761 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1762 dev->name, MAC_ARG(resp->da));
1763 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1764 ieee80211_sta_tx(dev, skb, 0);
1768 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1769 struct ieee80211_rx_status *rx_status)
1771 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1772 struct ieee80211_sub_if_data *sdata;
1773 struct ieee80211_if_sta *ifsta;
1774 struct ieee80211_mgmt *mgmt;
1775 u16 fc;
1777 if (skb->len < 24)
1778 goto fail;
1780 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1781 ifsta = &sdata->u.sta;
1783 mgmt = (struct ieee80211_mgmt *) skb->data;
1784 fc = le16_to_cpu(mgmt->frame_control);
1786 switch (fc & IEEE80211_FCTL_STYPE) {
1787 case IEEE80211_STYPE_PROBE_REQ:
1788 case IEEE80211_STYPE_PROBE_RESP:
1789 case IEEE80211_STYPE_BEACON:
1790 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1791 case IEEE80211_STYPE_AUTH:
1792 case IEEE80211_STYPE_ASSOC_RESP:
1793 case IEEE80211_STYPE_REASSOC_RESP:
1794 case IEEE80211_STYPE_DEAUTH:
1795 case IEEE80211_STYPE_DISASSOC:
1796 skb_queue_tail(&ifsta->skb_queue, skb);
1797 queue_work(local->hw.workqueue, &ifsta->work);
1798 return;
1799 default:
1800 printk(KERN_DEBUG "%s: received unknown management frame - "
1801 "stype=%d\n", dev->name,
1802 (fc & IEEE80211_FCTL_STYPE) >> 4);
1803 break;
1806 fail:
1807 kfree_skb(skb);
1811 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1812 struct sk_buff *skb)
1814 struct ieee80211_rx_status *rx_status;
1815 struct ieee80211_sub_if_data *sdata;
1816 struct ieee80211_if_sta *ifsta;
1817 struct ieee80211_mgmt *mgmt;
1818 u16 fc;
1820 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1821 ifsta = &sdata->u.sta;
1823 rx_status = (struct ieee80211_rx_status *) skb->cb;
1824 mgmt = (struct ieee80211_mgmt *) skb->data;
1825 fc = le16_to_cpu(mgmt->frame_control);
1827 switch (fc & IEEE80211_FCTL_STYPE) {
1828 case IEEE80211_STYPE_PROBE_REQ:
1829 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1830 rx_status);
1831 break;
1832 case IEEE80211_STYPE_PROBE_RESP:
1833 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1834 break;
1835 case IEEE80211_STYPE_BEACON:
1836 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1837 break;
1838 case IEEE80211_STYPE_AUTH:
1839 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1840 break;
1841 case IEEE80211_STYPE_ASSOC_RESP:
1842 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1843 break;
1844 case IEEE80211_STYPE_REASSOC_RESP:
1845 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1846 break;
1847 case IEEE80211_STYPE_DEAUTH:
1848 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1849 break;
1850 case IEEE80211_STYPE_DISASSOC:
1851 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1852 break;
1855 kfree_skb(skb);
1859 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1860 struct ieee80211_rx_status *rx_status)
1862 struct ieee80211_mgmt *mgmt;
1863 u16 fc;
1865 if (skb->len < 24) {
1866 dev_kfree_skb(skb);
1867 return;
1870 mgmt = (struct ieee80211_mgmt *) skb->data;
1871 fc = le16_to_cpu(mgmt->frame_control);
1873 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1874 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1875 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1876 skb->len, rx_status);
1877 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1878 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1879 rx_status);
1883 dev_kfree_skb(skb);
1887 static int ieee80211_sta_active_ibss(struct net_device *dev)
1889 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1890 int active = 0;
1891 struct sta_info *sta;
1893 spin_lock_bh(&local->sta_lock);
1894 list_for_each_entry(sta, &local->sta_list, list) {
1895 if (sta->dev == dev &&
1896 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1897 jiffies)) {
1898 active++;
1899 break;
1902 spin_unlock_bh(&local->sta_lock);
1904 return active;
1908 static void ieee80211_sta_expire(struct net_device *dev)
1910 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1911 struct sta_info *sta, *tmp;
1913 spin_lock_bh(&local->sta_lock);
1914 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1915 if (time_after(jiffies, sta->last_rx +
1916 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1917 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1918 "\n", dev->name, MAC_ARG(sta->addr));
1919 sta_info_free(sta, 1);
1921 spin_unlock_bh(&local->sta_lock);
1925 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1926 struct ieee80211_if_sta *ifsta)
1928 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1930 ieee80211_sta_expire(dev);
1931 if (ieee80211_sta_active_ibss(dev))
1932 return;
1934 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1935 "IBSS networks with same SSID (merge)\n", dev->name);
1936 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1940 void ieee80211_sta_timer(unsigned long data)
1942 struct ieee80211_sub_if_data *sdata =
1943 (struct ieee80211_sub_if_data *) data;
1944 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1945 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1947 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1948 queue_work(local->hw.workqueue, &ifsta->work);
1952 void ieee80211_sta_work(struct work_struct *work)
1954 struct ieee80211_sub_if_data *sdata =
1955 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1956 struct net_device *dev = sdata->dev;
1957 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1958 struct ieee80211_if_sta *ifsta;
1959 struct sk_buff *skb;
1961 if (!netif_running(dev))
1962 return;
1964 if (local->sta_scanning)
1965 return;
1967 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1968 sdata->type != IEEE80211_IF_TYPE_IBSS) {
1969 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
1970 "(type=%d)\n", dev->name, sdata->type);
1971 return;
1973 ifsta = &sdata->u.sta;
1975 while ((skb = skb_dequeue(&ifsta->skb_queue)))
1976 ieee80211_sta_rx_queued_mgmt(dev, skb);
1978 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1979 ifsta->state != IEEE80211_ASSOCIATE &&
1980 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
1981 ieee80211_sta_start_scan(dev, NULL, 0);
1982 return;
1985 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
1986 if (ieee80211_sta_config_auth(dev, ifsta))
1987 return;
1988 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1989 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
1990 return;
1992 switch (ifsta->state) {
1993 case IEEE80211_DISABLED:
1994 break;
1995 case IEEE80211_AUTHENTICATE:
1996 ieee80211_authenticate(dev, ifsta);
1997 break;
1998 case IEEE80211_ASSOCIATE:
1999 ieee80211_associate(dev, ifsta);
2000 break;
2001 case IEEE80211_ASSOCIATED:
2002 ieee80211_associated(dev, ifsta);
2003 break;
2004 case IEEE80211_IBSS_SEARCH:
2005 ieee80211_sta_find_ibss(dev, ifsta);
2006 break;
2007 case IEEE80211_IBSS_JOINED:
2008 ieee80211_sta_merge_ibss(dev, ifsta);
2009 break;
2010 default:
2011 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2012 ifsta->state);
2013 break;
2016 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2017 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2018 "mixed-cell disabled - disassociate\n", dev->name);
2020 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2021 ieee80211_set_disassoc(dev, ifsta, 0);
2026 static void ieee80211_sta_reset_auth(struct net_device *dev,
2027 struct ieee80211_if_sta *ifsta)
2029 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2031 if (local->ops->reset_tsf) {
2032 /* Reset own TSF to allow time synchronization work. */
2033 local->ops->reset_tsf(local_to_hw(local));
2036 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2039 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2040 ifsta->auth_alg = WLAN_AUTH_OPEN;
2041 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2042 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2043 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2044 ifsta->auth_alg = WLAN_AUTH_LEAP;
2045 else
2046 ifsta->auth_alg = WLAN_AUTH_OPEN;
2047 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2048 ifsta->auth_alg);
2049 ifsta->auth_transaction = -1;
2050 ifsta->associated = ifsta->auth_tries = ifsta->assoc_tries = 0;
2051 netif_carrier_off(dev);
2055 void ieee80211_sta_req_auth(struct net_device *dev,
2056 struct ieee80211_if_sta *ifsta)
2058 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2059 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2061 if (sdata->type != IEEE80211_IF_TYPE_STA)
2062 return;
2064 if ((ifsta->bssid_set || ifsta->auto_bssid_sel) &&
2065 (ifsta->ssid_set || ifsta->auto_ssid_sel)) {
2066 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2067 queue_work(local->hw.workqueue, &ifsta->work);
2071 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2072 const char *ssid, int ssid_len)
2074 int tmp, hidden_ssid;
2076 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2077 return 1;
2079 if (ifsta->auto_bssid_sel)
2080 return 0;
2082 hidden_ssid = 1;
2083 tmp = ssid_len;
2084 while (tmp--) {
2085 if (ssid[tmp] != '\0') {
2086 hidden_ssid = 0;
2087 break;
2091 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2092 return 1;
2094 if (ssid_len == 1 && ssid[0] == ' ')
2095 return 1;
2097 return 0;
2100 static int ieee80211_sta_config_auth(struct net_device *dev,
2101 struct ieee80211_if_sta *ifsta)
2103 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2104 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2105 struct ieee80211_sta_bss *bss, *selected = NULL;
2106 int top_rssi = 0, freq;
2108 if (!ifsta->auto_channel_sel && !ifsta->auto_bssid_sel &&
2109 !ifsta->auto_ssid_sel) {
2110 ifsta->state = IEEE80211_AUTHENTICATE;
2111 ieee80211_sta_reset_auth(dev, ifsta);
2112 return 0;
2115 spin_lock_bh(&local->sta_bss_lock);
2116 freq = local->oper_channel->freq;
2117 list_for_each_entry(bss, &local->sta_bss_list, list) {
2118 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2119 continue;
2121 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2122 !!sdata->default_key)
2123 continue;
2125 if (!ifsta->auto_channel_sel && bss->freq != freq)
2126 continue;
2128 if (!ifsta->auto_bssid_sel &&
2129 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2130 continue;
2132 if (!ifsta->auto_ssid_sel &&
2133 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2134 continue;
2136 if (!selected || top_rssi < bss->rssi) {
2137 selected = bss;
2138 top_rssi = bss->rssi;
2141 if (selected)
2142 atomic_inc(&selected->users);
2143 spin_unlock_bh(&local->sta_bss_lock);
2145 if (selected) {
2146 ieee80211_set_channel(local, -1, selected->freq);
2147 if (!ifsta->ssid_set)
2148 ieee80211_sta_set_ssid(dev, selected->ssid,
2149 selected->ssid_len);
2150 ieee80211_sta_set_bssid(dev, selected->bssid);
2151 ieee80211_rx_bss_put(dev, selected);
2152 ifsta->state = IEEE80211_AUTHENTICATE;
2153 ieee80211_sta_reset_auth(dev, ifsta);
2154 return 0;
2155 } else {
2156 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2157 if (ifsta->auto_ssid_sel)
2158 ieee80211_sta_start_scan(dev, NULL, 0);
2159 else
2160 ieee80211_sta_start_scan(dev, ifsta->ssid,
2161 ifsta->ssid_len);
2162 ifsta->state = IEEE80211_AUTHENTICATE;
2163 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2164 } else
2165 ifsta->state = IEEE80211_DISABLED;
2167 return -1;
2170 static int ieee80211_sta_join_ibss(struct net_device *dev,
2171 struct ieee80211_if_sta *ifsta,
2172 struct ieee80211_sta_bss *bss)
2174 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2175 int res, rates, i, j;
2176 struct sk_buff *skb;
2177 struct ieee80211_mgmt *mgmt;
2178 struct ieee80211_tx_control control;
2179 struct ieee80211_rate *rate;
2180 struct ieee80211_hw_mode *mode;
2181 struct rate_control_extra extra;
2182 u8 *pos;
2183 struct ieee80211_sub_if_data *sdata;
2185 /* Remove possible STA entries from other IBSS networks. */
2186 sta_info_flush(local, NULL);
2188 if (local->ops->reset_tsf) {
2189 /* Reset own TSF to allow time synchronization work. */
2190 local->ops->reset_tsf(local_to_hw(local));
2192 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2193 res = ieee80211_if_config(dev);
2194 if (res)
2195 return res;
2197 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2199 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2200 sdata->drop_unencrypted = bss->capability &
2201 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2203 res = ieee80211_set_channel(local, -1, bss->freq);
2205 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2206 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2207 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2208 local->hw.conf.freq);
2209 return -1;
2212 /* Set beacon template based on scan results */
2213 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2214 do {
2215 if (!skb)
2216 break;
2218 skb_reserve(skb, local->hw.extra_tx_headroom);
2220 mgmt = (struct ieee80211_mgmt *)
2221 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2222 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2223 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2224 IEEE80211_STYPE_BEACON);
2225 memset(mgmt->da, 0xff, ETH_ALEN);
2226 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2227 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2228 mgmt->u.beacon.beacon_int =
2229 cpu_to_le16(local->hw.conf.beacon_int);
2230 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2232 pos = skb_put(skb, 2 + ifsta->ssid_len);
2233 *pos++ = WLAN_EID_SSID;
2234 *pos++ = ifsta->ssid_len;
2235 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2237 rates = bss->supp_rates_len;
2238 if (rates > 8)
2239 rates = 8;
2240 pos = skb_put(skb, 2 + rates);
2241 *pos++ = WLAN_EID_SUPP_RATES;
2242 *pos++ = rates;
2243 memcpy(pos, bss->supp_rates, rates);
2245 pos = skb_put(skb, 2 + 1);
2246 *pos++ = WLAN_EID_DS_PARAMS;
2247 *pos++ = 1;
2248 *pos++ = bss->channel;
2250 pos = skb_put(skb, 2 + 2);
2251 *pos++ = WLAN_EID_IBSS_PARAMS;
2252 *pos++ = 2;
2253 /* FIX: set ATIM window based on scan results */
2254 *pos++ = 0;
2255 *pos++ = 0;
2257 if (bss->supp_rates_len > 8) {
2258 rates = bss->supp_rates_len - 8;
2259 pos = skb_put(skb, 2 + rates);
2260 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2261 *pos++ = rates;
2262 memcpy(pos, &bss->supp_rates[8], rates);
2265 memset(&control, 0, sizeof(control));
2266 memset(&extra, 0, sizeof(extra));
2267 extra.mode = local->oper_hw_mode;
2268 rate = rate_control_get_rate(local, dev, skb, &extra);
2269 if (!rate) {
2270 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2271 "for IBSS beacon\n", dev->name);
2272 break;
2274 control.tx_rate = (local->short_preamble &&
2275 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2276 rate->val2 : rate->val;
2277 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2278 control.power_level = local->hw.conf.power_level;
2279 control.flags |= IEEE80211_TXCTL_NO_ACK;
2280 control.retry_limit = 1;
2282 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2283 if (ifsta->probe_resp) {
2284 mgmt = (struct ieee80211_mgmt *)
2285 ifsta->probe_resp->data;
2286 mgmt->frame_control =
2287 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2288 IEEE80211_STYPE_PROBE_RESP);
2289 } else {
2290 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2291 "template for IBSS\n", dev->name);
2294 if (local->ops->beacon_update &&
2295 local->ops->beacon_update(local_to_hw(local),
2296 skb, &control) == 0) {
2297 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2298 "template based on scan results\n", dev->name);
2299 skb = NULL;
2302 rates = 0;
2303 mode = local->oper_hw_mode;
2304 for (i = 0; i < bss->supp_rates_len; i++) {
2305 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2306 if (mode->mode == MODE_ATHEROS_TURBO)
2307 bitrate *= 2;
2308 for (j = 0; j < mode->num_rates; j++)
2309 if (mode->rates[j].rate == bitrate)
2310 rates |= BIT(j);
2312 ifsta->supp_rates_bits = rates;
2313 } while (0);
2315 if (skb) {
2316 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2317 "template\n", dev->name);
2318 dev_kfree_skb(skb);
2321 ifsta->state = IEEE80211_IBSS_JOINED;
2322 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2324 ieee80211_rx_bss_put(dev, bss);
2326 return res;
2330 static int ieee80211_sta_create_ibss(struct net_device *dev,
2331 struct ieee80211_if_sta *ifsta)
2333 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2334 struct ieee80211_sta_bss *bss;
2335 struct ieee80211_sub_if_data *sdata;
2336 struct ieee80211_hw_mode *mode;
2337 u8 bssid[ETH_ALEN], *pos;
2338 int i;
2340 #if 0
2341 /* Easier testing, use fixed BSSID. */
2342 memset(bssid, 0xfe, ETH_ALEN);
2343 #else
2344 /* Generate random, not broadcast, locally administered BSSID. Mix in
2345 * own MAC address to make sure that devices that do not have proper
2346 * random number generator get different BSSID. */
2347 get_random_bytes(bssid, ETH_ALEN);
2348 for (i = 0; i < ETH_ALEN; i++)
2349 bssid[i] ^= dev->dev_addr[i];
2350 bssid[0] &= ~0x01;
2351 bssid[0] |= 0x02;
2352 #endif
2354 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2355 dev->name, MAC_ARG(bssid));
2357 bss = ieee80211_rx_bss_add(dev, bssid);
2358 if (!bss)
2359 return -ENOMEM;
2361 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2362 mode = local->oper_hw_mode;
2364 if (local->hw.conf.beacon_int == 0)
2365 local->hw.conf.beacon_int = 100;
2366 bss->beacon_int = local->hw.conf.beacon_int;
2367 bss->hw_mode = local->hw.conf.phymode;
2368 bss->channel = local->hw.conf.channel;
2369 bss->freq = local->hw.conf.freq;
2370 bss->last_update = jiffies;
2371 bss->capability = WLAN_CAPABILITY_IBSS;
2372 if (sdata->default_key) {
2373 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2374 } else
2375 sdata->drop_unencrypted = 0;
2376 bss->supp_rates_len = mode->num_rates;
2377 pos = bss->supp_rates;
2378 for (i = 0; i < mode->num_rates; i++) {
2379 int rate = mode->rates[i].rate;
2380 if (mode->mode == MODE_ATHEROS_TURBO)
2381 rate /= 2;
2382 *pos++ = (u8) (rate / 5);
2385 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2389 static int ieee80211_sta_find_ibss(struct net_device *dev,
2390 struct ieee80211_if_sta *ifsta)
2392 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2393 struct ieee80211_sta_bss *bss;
2394 int found = 0;
2395 u8 bssid[ETH_ALEN];
2396 int active_ibss;
2398 if (ifsta->ssid_len == 0)
2399 return -EINVAL;
2401 active_ibss = ieee80211_sta_active_ibss(dev);
2402 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2403 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2404 dev->name, active_ibss);
2405 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2406 spin_lock_bh(&local->sta_bss_lock);
2407 list_for_each_entry(bss, &local->sta_bss_list, list) {
2408 if (ifsta->ssid_len != bss->ssid_len ||
2409 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2410 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2411 continue;
2412 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2413 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2414 MAC_ARG(bss->bssid));
2415 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2416 memcpy(bssid, bss->bssid, ETH_ALEN);
2417 found = 1;
2418 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2419 break;
2421 spin_unlock_bh(&local->sta_bss_lock);
2423 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2424 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2425 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2426 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2427 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2428 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2429 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2430 " based on configured SSID\n",
2431 dev->name, MAC_ARG(bssid));
2432 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2434 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2435 printk(KERN_DEBUG " did not try to join ibss\n");
2436 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2438 /* Selected IBSS not found in current scan results - try to scan */
2439 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2440 !ieee80211_sta_active_ibss(dev)) {
2441 mod_timer(&ifsta->timer, jiffies +
2442 IEEE80211_IBSS_MERGE_INTERVAL);
2443 } else if (time_after(jiffies, local->last_scan_completed +
2444 IEEE80211_SCAN_INTERVAL)) {
2445 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2446 "join\n", dev->name);
2447 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2448 ifsta->ssid_len);
2449 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2450 int interval = IEEE80211_SCAN_INTERVAL;
2452 if (time_after(jiffies, ifsta->ibss_join_req +
2453 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2454 if (ifsta->create_ibss &&
2455 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2456 return ieee80211_sta_create_ibss(dev, ifsta);
2457 if (ifsta->create_ibss) {
2458 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2459 " configured channel %d (%d MHz)\n",
2460 dev->name, local->hw.conf.channel,
2461 local->hw.conf.freq);
2464 /* No IBSS found - decrease scan interval and continue
2465 * scanning. */
2466 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2469 ifsta->state = IEEE80211_IBSS_SEARCH;
2470 mod_timer(&ifsta->timer, jiffies + interval);
2471 return 0;
2474 return 0;
2478 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2480 struct ieee80211_sub_if_data *sdata;
2481 struct ieee80211_if_sta *ifsta;
2482 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2484 if (len > IEEE80211_MAX_SSID_LEN)
2485 return -EINVAL;
2487 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2488 * not defined. */
2489 if (local->ops->conf_tx) {
2490 struct ieee80211_tx_queue_params qparam;
2491 int i;
2493 memset(&qparam, 0, sizeof(qparam));
2494 /* TODO: are these ok defaults for all hw_modes? */
2495 qparam.aifs = 2;
2496 qparam.cw_min =
2497 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2498 qparam.cw_max = 1023;
2499 qparam.burst_time = 0;
2500 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2502 local->ops->conf_tx(local_to_hw(local),
2503 i + IEEE80211_TX_QUEUE_DATA0,
2504 &qparam);
2506 /* IBSS uses different parameters for Beacon sending */
2507 qparam.cw_min++;
2508 qparam.cw_min *= 2;
2509 qparam.cw_min--;
2510 local->ops->conf_tx(local_to_hw(local),
2511 IEEE80211_TX_QUEUE_BEACON, &qparam);
2514 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2515 ifsta = &sdata->u.sta;
2517 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2518 ifsta->prev_bssid_set = 0;
2519 memcpy(ifsta->ssid, ssid, len);
2520 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2521 ifsta->ssid_len = len;
2523 ifsta->ssid_set = len ? 1 : 0;
2524 if (sdata->type == IEEE80211_IF_TYPE_IBSS && !ifsta->bssid_set) {
2525 ifsta->ibss_join_req = jiffies;
2526 ifsta->state = IEEE80211_IBSS_SEARCH;
2527 return ieee80211_sta_find_ibss(dev, ifsta);
2529 return 0;
2533 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2535 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2536 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2537 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2538 *len = ifsta->ssid_len;
2539 return 0;
2543 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2545 struct ieee80211_sub_if_data *sdata;
2546 struct ieee80211_if_sta *ifsta;
2547 int res;
2549 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2550 ifsta = &sdata->u.sta;
2552 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2553 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2554 res = ieee80211_if_config(dev);
2555 if (res) {
2556 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2557 "the low-level driver\n", dev->name);
2558 return res;
2562 if (!is_valid_ether_addr(bssid))
2563 ifsta->bssid_set = 0;
2564 else
2565 ifsta->bssid_set = 1;
2566 return 0;
2570 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2571 struct ieee80211_sub_if_data *sdata,
2572 int powersave)
2574 struct sk_buff *skb;
2575 struct ieee80211_hdr *nullfunc;
2576 u16 fc;
2578 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2579 if (!skb) {
2580 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2581 "frame\n", sdata->dev->name);
2582 return;
2584 skb_reserve(skb, local->hw.extra_tx_headroom);
2586 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2587 memset(nullfunc, 0, 24);
2588 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2589 IEEE80211_FCTL_TODS;
2590 if (powersave)
2591 fc |= IEEE80211_FCTL_PM;
2592 nullfunc->frame_control = cpu_to_le16(fc);
2593 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2594 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2595 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2597 ieee80211_sta_tx(sdata->dev, skb, 0);
2601 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2603 struct ieee80211_local *local = hw_to_local(hw);
2604 struct net_device *dev = local->scan_dev;
2605 struct ieee80211_sub_if_data *sdata;
2606 union iwreq_data wrqu;
2608 local->last_scan_completed = jiffies;
2609 wmb();
2610 local->sta_scanning = 0;
2612 if (ieee80211_hw_config(local))
2613 printk(KERN_DEBUG "%s: failed to restore operational"
2614 "channel after scan\n", dev->name);
2616 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2617 ieee80211_if_config(dev))
2618 printk(KERN_DEBUG "%s: failed to restore operational"
2619 "BSSID after scan\n", dev->name);
2621 memset(&wrqu, 0, sizeof(wrqu));
2622 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2624 read_lock(&local->sub_if_lock);
2625 list_for_each_entry(sdata, &local->sub_if_list, list) {
2627 /* No need to wake the master device. */
2628 if (sdata->dev == local->mdev)
2629 continue;
2631 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2632 if (sdata->u.sta.associated)
2633 ieee80211_send_nullfunc(local, sdata, 0);
2634 ieee80211_sta_timer((unsigned long)sdata);
2637 netif_wake_queue(sdata->dev);
2639 read_unlock(&local->sub_if_lock);
2641 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2642 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2643 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2644 if (!ifsta->bssid_set ||
2645 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2646 !ieee80211_sta_active_ibss(dev)))
2647 ieee80211_sta_find_ibss(dev, ifsta);
2650 EXPORT_SYMBOL(ieee80211_scan_completed);
2652 void ieee80211_sta_scan_work(struct work_struct *work)
2654 struct ieee80211_local *local =
2655 container_of(work, struct ieee80211_local, scan_work.work);
2656 struct net_device *dev = local->scan_dev;
2657 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2658 struct ieee80211_hw_mode *mode;
2659 struct ieee80211_channel *chan;
2660 int skip;
2661 unsigned long next_delay = 0;
2663 if (!local->sta_scanning)
2664 return;
2666 switch (local->scan_state) {
2667 case SCAN_SET_CHANNEL:
2668 mode = local->scan_hw_mode;
2669 if (local->scan_hw_mode->list.next == &local->modes_list &&
2670 local->scan_channel_idx >= mode->num_channels) {
2671 ieee80211_scan_completed(local_to_hw(local));
2672 return;
2674 skip = !(local->enabled_modes & (1 << mode->mode));
2675 chan = &mode->channels[local->scan_channel_idx];
2676 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2677 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2678 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2679 (local->hw_modes & local->enabled_modes &
2680 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2681 skip = 1;
2683 if (!skip) {
2684 #if 0
2685 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2686 dev->name, chan->chan, chan->freq);
2687 #endif
2689 local->scan_channel = chan;
2690 if (ieee80211_hw_config(local)) {
2691 printk(KERN_DEBUG "%s: failed to set channel "
2692 "%d (%d MHz) for scan\n", dev->name,
2693 chan->chan, chan->freq);
2694 skip = 1;
2698 local->scan_channel_idx++;
2699 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2700 if (local->scan_hw_mode->list.next != &local->modes_list) {
2701 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2702 struct ieee80211_hw_mode,
2703 list);
2704 local->scan_channel_idx = 0;
2708 if (skip)
2709 break;
2711 next_delay = IEEE80211_PROBE_DELAY +
2712 usecs_to_jiffies(local->hw.channel_change_time);
2713 local->scan_state = SCAN_SEND_PROBE;
2714 break;
2715 case SCAN_SEND_PROBE:
2716 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2717 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2718 local->scan_ssid_len);
2719 next_delay = IEEE80211_CHANNEL_TIME;
2720 } else
2721 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2722 local->scan_state = SCAN_SET_CHANNEL;
2723 break;
2726 if (local->sta_scanning)
2727 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2728 next_delay);
2732 static int ieee80211_sta_start_scan(struct net_device *dev,
2733 u8 *ssid, size_t ssid_len)
2735 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2736 struct ieee80211_sub_if_data *sdata;
2738 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2739 return -EINVAL;
2741 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2742 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2743 * BSSID: MACAddress
2744 * SSID
2745 * ScanType: ACTIVE, PASSIVE
2746 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2747 * a Probe frame during active scanning
2748 * ChannelList
2749 * MinChannelTime (>= ProbeDelay), in TU
2750 * MaxChannelTime: (>= MinChannelTime), in TU
2753 /* MLME-SCAN.confirm
2754 * BSSDescriptionSet
2755 * ResultCode: SUCCESS, INVALID_PARAMETERS
2758 if (local->sta_scanning) {
2759 if (local->scan_dev == dev)
2760 return 0;
2761 return -EBUSY;
2764 if (local->ops->hw_scan) {
2765 int rc = local->ops->hw_scan(local_to_hw(local),
2766 ssid, ssid_len);
2767 if (!rc) {
2768 local->sta_scanning = 1;
2769 local->scan_dev = dev;
2771 return rc;
2774 local->sta_scanning = 1;
2776 read_lock(&local->sub_if_lock);
2777 list_for_each_entry(sdata, &local->sub_if_list, list) {
2779 /* Don't stop the master interface, otherwise we can't transmit
2780 * probes! */
2781 if (sdata->dev == local->mdev)
2782 continue;
2784 netif_stop_queue(sdata->dev);
2785 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2786 sdata->u.sta.associated)
2787 ieee80211_send_nullfunc(local, sdata, 1);
2789 read_unlock(&local->sub_if_lock);
2791 if (ssid) {
2792 local->scan_ssid_len = ssid_len;
2793 memcpy(local->scan_ssid, ssid, ssid_len);
2794 } else
2795 local->scan_ssid_len = 0;
2796 local->scan_state = SCAN_SET_CHANNEL;
2797 local->scan_hw_mode = list_entry(local->modes_list.next,
2798 struct ieee80211_hw_mode,
2799 list);
2800 local->scan_channel_idx = 0;
2801 local->scan_dev = dev;
2803 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2804 ieee80211_if_config(dev))
2805 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2806 dev->name);
2808 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2809 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2810 IEEE80211_CHANNEL_TIME);
2812 return 0;
2816 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2818 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2819 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2820 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2822 if (sdata->type != IEEE80211_IF_TYPE_STA)
2823 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2825 if (local->sta_scanning) {
2826 if (local->scan_dev == dev)
2827 return 0;
2828 return -EBUSY;
2831 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2832 queue_work(local->hw.workqueue, &ifsta->work);
2833 return 0;
2836 static char *
2837 ieee80211_sta_scan_result(struct net_device *dev,
2838 struct ieee80211_sta_bss *bss,
2839 char *current_ev, char *end_buf)
2841 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2842 struct iw_event iwe;
2844 if (time_after(jiffies,
2845 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2846 return current_ev;
2848 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2849 return current_ev;
2851 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2852 !bss->wpa_ie && !bss->rsn_ie)
2853 return current_ev;
2855 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2856 (local->scan_ssid_len != bss->ssid_len ||
2857 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2858 return current_ev;
2860 memset(&iwe, 0, sizeof(iwe));
2861 iwe.cmd = SIOCGIWAP;
2862 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2863 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2864 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2865 IW_EV_ADDR_LEN);
2867 memset(&iwe, 0, sizeof(iwe));
2868 iwe.cmd = SIOCGIWESSID;
2869 iwe.u.data.length = bss->ssid_len;
2870 iwe.u.data.flags = 1;
2871 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2872 bss->ssid);
2874 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2875 memset(&iwe, 0, sizeof(iwe));
2876 iwe.cmd = SIOCGIWMODE;
2877 if (bss->capability & WLAN_CAPABILITY_ESS)
2878 iwe.u.mode = IW_MODE_MASTER;
2879 else
2880 iwe.u.mode = IW_MODE_ADHOC;
2881 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2882 IW_EV_UINT_LEN);
2885 memset(&iwe, 0, sizeof(iwe));
2886 iwe.cmd = SIOCGIWFREQ;
2887 iwe.u.freq.m = bss->channel;
2888 iwe.u.freq.e = 0;
2889 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2890 IW_EV_FREQ_LEN);
2891 iwe.u.freq.m = bss->freq * 100000;
2892 iwe.u.freq.e = 1;
2893 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2894 IW_EV_FREQ_LEN);
2896 memset(&iwe, 0, sizeof(iwe));
2897 iwe.cmd = IWEVQUAL;
2898 iwe.u.qual.qual = bss->signal;
2899 iwe.u.qual.level = bss->rssi;
2900 iwe.u.qual.noise = bss->noise;
2901 iwe.u.qual.updated = local->wstats_flags;
2902 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2903 IW_EV_QUAL_LEN);
2905 memset(&iwe, 0, sizeof(iwe));
2906 iwe.cmd = SIOCGIWENCODE;
2907 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2908 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2909 else
2910 iwe.u.data.flags = IW_ENCODE_DISABLED;
2911 iwe.u.data.length = 0;
2912 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2914 if (bss && bss->wpa_ie) {
2915 memset(&iwe, 0, sizeof(iwe));
2916 iwe.cmd = IWEVGENIE;
2917 iwe.u.data.length = bss->wpa_ie_len;
2918 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2919 bss->wpa_ie);
2922 if (bss && bss->rsn_ie) {
2923 memset(&iwe, 0, sizeof(iwe));
2924 iwe.cmd = IWEVGENIE;
2925 iwe.u.data.length = bss->rsn_ie_len;
2926 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2927 bss->rsn_ie);
2930 if (bss && bss->supp_rates_len > 0) {
2931 /* display all supported rates in readable format */
2932 char *p = current_ev + IW_EV_LCP_LEN;
2933 int i;
2935 memset(&iwe, 0, sizeof(iwe));
2936 iwe.cmd = SIOCGIWRATE;
2937 /* Those two flags are ignored... */
2938 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2940 for (i = 0; i < bss->supp_rates_len; i++) {
2941 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2942 0x7f) * 500000);
2943 p = iwe_stream_add_value(current_ev, p,
2944 end_buf, &iwe, IW_EV_PARAM_LEN);
2946 current_ev = p;
2949 if (bss) {
2950 char *buf;
2951 buf = kmalloc(30, GFP_ATOMIC);
2952 if (buf) {
2953 memset(&iwe, 0, sizeof(iwe));
2954 iwe.cmd = IWEVCUSTOM;
2955 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
2956 iwe.u.data.length = strlen(buf);
2957 current_ev = iwe_stream_add_point(current_ev, end_buf,
2958 &iwe, buf);
2959 kfree(buf);
2963 do {
2964 char *buf;
2966 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
2967 break;
2969 buf = kmalloc(100, GFP_ATOMIC);
2970 if (!buf)
2971 break;
2973 memset(&iwe, 0, sizeof(iwe));
2974 iwe.cmd = IWEVCUSTOM;
2975 sprintf(buf, "bcn_int=%d", bss->beacon_int);
2976 iwe.u.data.length = strlen(buf);
2977 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2978 buf);
2980 memset(&iwe, 0, sizeof(iwe));
2981 iwe.cmd = IWEVCUSTOM;
2982 sprintf(buf, "capab=0x%04x", bss->capability);
2983 iwe.u.data.length = strlen(buf);
2984 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2985 buf);
2987 kfree(buf);
2988 break;
2989 } while (0);
2991 return current_ev;
2995 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
2997 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2998 char *current_ev = buf;
2999 char *end_buf = buf + len;
3000 struct ieee80211_sta_bss *bss;
3002 spin_lock_bh(&local->sta_bss_lock);
3003 list_for_each_entry(bss, &local->sta_bss_list, list) {
3004 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3005 spin_unlock_bh(&local->sta_bss_lock);
3006 return -E2BIG;
3008 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3009 end_buf);
3011 spin_unlock_bh(&local->sta_bss_lock);
3012 return current_ev - buf;
3016 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3018 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3019 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3020 kfree(ifsta->extra_ie);
3021 if (len == 0) {
3022 ifsta->extra_ie = NULL;
3023 ifsta->extra_ie_len = 0;
3024 return 0;
3026 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3027 if (!ifsta->extra_ie) {
3028 ifsta->extra_ie_len = 0;
3029 return -ENOMEM;
3031 memcpy(ifsta->extra_ie, ie, len);
3032 ifsta->extra_ie_len = len;
3033 return 0;
3037 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3038 struct sk_buff *skb, u8 *bssid,
3039 u8 *addr)
3041 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3042 struct sta_info *sta;
3043 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3045 /* TODO: Could consider removing the least recently used entry and
3046 * allow new one to be added. */
3047 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3048 if (net_ratelimit()) {
3049 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3050 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3052 return NULL;
3055 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3056 local->mdev->name, MAC_ARG(addr), dev->name);
3058 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3059 if (!sta)
3060 return NULL;
3062 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3064 rate_control_rate_init(sta, local);
3066 return sta; /* caller will call sta_info_put() */
3070 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3072 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3073 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3075 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3076 dev->name, reason);
3078 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3079 sdata->type != IEEE80211_IF_TYPE_IBSS)
3080 return -EINVAL;
3082 ieee80211_send_deauth(dev, ifsta, reason);
3083 ieee80211_set_disassoc(dev, ifsta, 1);
3084 return 0;
3088 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3090 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3091 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3093 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3094 dev->name, reason);
3096 if (sdata->type != IEEE80211_IF_TYPE_STA)
3097 return -EINVAL;
3099 if (!ifsta->associated)
3100 return -1;
3102 ieee80211_send_disassoc(dev, ifsta, reason);
3103 ieee80211_set_disassoc(dev, ifsta, 0);
3104 return 0;