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.
15 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
19 #include <linux/delay.h>
20 #include <linux/if_ether.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/wireless.h>
25 #include <linux/random.h>
26 #include <linux/etherdevice.h>
27 #include <net/iw_handler.h>
28 #include <asm/types.h>
30 #include <net/mac80211.h>
31 #include "ieee80211_i.h"
32 #include "ieee80211_rate.h"
33 #include "ieee80211_led.h"
35 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
36 #define IEEE80211_AUTH_MAX_TRIES 3
37 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38 #define IEEE80211_ASSOC_MAX_TRIES 3
39 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
40 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
41 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
42 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
43 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
44 #define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
46 #define IEEE80211_PROBE_DELAY (HZ / 33)
47 #define IEEE80211_CHANNEL_TIME (HZ / 33)
48 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
49 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
50 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
51 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
53 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
56 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
58 #define ERP_INFO_USE_PROTECTION BIT(1)
60 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
61 u8 *ssid, size_t ssid_len);
62 static struct ieee80211_sta_bss *
63 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel,
64 u8 *ssid, u8 ssid_len);
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 {
94 /* length of them, respectively */
106 u8 ext_supp_rates_len;
111 enum ParseRes { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 };
113 static enum ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
114 struct ieee802_11_elems *elems)
120 memset(elems, 0, sizeof(*elems));
132 printk(KERN_DEBUG "IEEE 802.11 element parse "
133 "failed (id=%d elen=%d left=%d)\n",
142 elems->ssid_len = elen;
144 case WLAN_EID_SUPP_RATES:
145 elems->supp_rates = pos;
146 elems->supp_rates_len = elen;
148 case WLAN_EID_FH_PARAMS:
149 elems->fh_params = pos;
150 elems->fh_params_len = elen;
152 case WLAN_EID_DS_PARAMS:
153 elems->ds_params = pos;
154 elems->ds_params_len = elen;
156 case WLAN_EID_CF_PARAMS:
157 elems->cf_params = pos;
158 elems->cf_params_len = elen;
162 elems->tim_len = elen;
164 case WLAN_EID_IBSS_PARAMS:
165 elems->ibss_params = pos;
166 elems->ibss_params_len = elen;
168 case WLAN_EID_CHALLENGE:
169 elems->challenge = pos;
170 elems->challenge_len = elen;
173 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
175 /* Microsoft OUI (00:50:F2) */
177 /* OUI Type 1 - WPA IE */
179 elems->wpa_len = elen;
180 } else if (elen >= 5 && pos[3] == 2) {
182 elems->wmm_info = pos;
183 elems->wmm_info_len = elen;
184 } else if (pos[4] == 1) {
185 elems->wmm_param = pos;
186 elems->wmm_param_len = elen;
193 elems->rsn_len = elen;
195 case WLAN_EID_ERP_INFO:
196 elems->erp_info = pos;
197 elems->erp_info_len = elen;
199 case WLAN_EID_EXT_SUPP_RATES:
200 elems->ext_supp_rates = pos;
201 elems->ext_supp_rates_len = elen;
205 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
206 "unknown element (id=%d elen=%d)\n",
217 /* Do not trigger error if left == 1 as Apple Airport base stations
218 * send AssocResps that are one spurious byte too long. */
220 return unknown ? ParseUnknown : ParseOK;
226 static int ecw2cw(int ecw)
236 static void ieee80211_sta_wmm_params(struct net_device *dev,
237 struct ieee80211_if_sta *ifsta,
238 u8 *wmm_param, size_t wmm_param_len)
240 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
241 struct ieee80211_tx_queue_params params;
246 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
248 count = wmm_param[6] & 0x0f;
249 if (count == ifsta->wmm_last_param_set)
251 ifsta->wmm_last_param_set = count;
254 left = wmm_param_len - 8;
256 memset(¶ms, 0, sizeof(params));
258 if (!local->ops->conf_tx)
262 for (; left >= 4; left -= 4, pos += 4) {
263 int aci = (pos[0] >> 5) & 0x03;
264 int acm = (pos[0] >> 4) & 0x01;
269 queue = IEEE80211_TX_QUEUE_DATA3;
271 local->wmm_acm |= BIT(0) | BIT(3);
275 queue = IEEE80211_TX_QUEUE_DATA1;
277 local->wmm_acm |= BIT(4) | BIT(5);
281 queue = IEEE80211_TX_QUEUE_DATA0;
283 local->wmm_acm |= BIT(6) | BIT(7);
288 queue = IEEE80211_TX_QUEUE_DATA2;
290 local->wmm_acm |= BIT(1) | BIT(2);
295 params.aifs = pos[0] & 0x0f;
296 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
297 params.cw_min = ecw2cw(pos[1] & 0x0f);
298 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
299 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
300 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
301 "cWmin=%d cWmax=%d burst=%d\n",
302 dev->name, queue, aci, acm, params.aifs, params.cw_min,
303 params.cw_max, params.burst_time);
304 /* TODO: handle ACM (block TX, fallback to next lowest allowed
306 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
307 printk(KERN_DEBUG "%s: failed to set TX queue "
308 "parameters for queue %d\n", dev->name, queue);
314 static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
316 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
317 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
318 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
319 int preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
321 DECLARE_MAC_BUF(mac);
323 if (use_protection != !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION)) {
324 if (net_ratelimit()) {
325 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
328 use_protection ? "enabled" : "disabled",
329 print_mac(mac, ifsta->bssid));
332 sdata->flags |= IEEE80211_SDATA_USE_PROTECTION;
334 sdata->flags &= ~IEEE80211_SDATA_USE_PROTECTION;
335 changes |= IEEE80211_ERP_CHANGE_PROTECTION;
338 if (preamble_mode != !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE)) {
339 if (net_ratelimit()) {
340 printk(KERN_DEBUG "%s: switched to %s barker preamble"
343 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
345 print_mac(mac, ifsta->bssid));
348 sdata->flags &= ~IEEE80211_SDATA_SHORT_PREAMBLE;
350 sdata->flags |= IEEE80211_SDATA_SHORT_PREAMBLE;
351 changes |= IEEE80211_ERP_CHANGE_PREAMBLE;
355 ieee80211_erp_info_change_notify(dev, changes);
359 static void ieee80211_sta_send_associnfo(struct net_device *dev,
360 struct ieee80211_if_sta *ifsta)
365 union iwreq_data wrqu;
367 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
370 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
371 ifsta->assocresp_ies_len), GFP_KERNEL);
375 len = sprintf(buf, "ASSOCINFO(");
376 if (ifsta->assocreq_ies) {
377 len += sprintf(buf + len, "ReqIEs=");
378 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
379 len += sprintf(buf + len, "%02x",
380 ifsta->assocreq_ies[i]);
383 if (ifsta->assocresp_ies) {
384 if (ifsta->assocreq_ies)
385 len += sprintf(buf + len, " ");
386 len += sprintf(buf + len, "RespIEs=");
387 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
388 len += sprintf(buf + len, "%02x",
389 ifsta->assocresp_ies[i]);
392 len += sprintf(buf + len, ")");
394 if (len > IW_CUSTOM_MAX) {
395 len = sprintf(buf, "ASSOCRESPIE=");
396 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
397 len += sprintf(buf + len, "%02x",
398 ifsta->assocresp_ies[i]);
402 memset(&wrqu, 0, sizeof(wrqu));
403 wrqu.data.length = len;
404 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
410 static void ieee80211_set_associated(struct net_device *dev,
411 struct ieee80211_if_sta *ifsta,
414 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
415 union iwreq_data wrqu;
417 if (!!(ifsta->flags & IEEE80211_STA_ASSOCIATED) == assoc)
421 struct ieee80211_sub_if_data *sdata;
422 struct ieee80211_sta_bss *bss;
424 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
426 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
427 if (sdata->type != IEEE80211_IF_TYPE_STA)
430 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
431 local->hw.conf.channel,
432 ifsta->ssid, ifsta->ssid_len);
434 if (bss->has_erp_value)
435 ieee80211_handle_erp_ie(dev, bss->erp_value);
436 ieee80211_rx_bss_put(dev, bss);
439 netif_carrier_on(dev);
440 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
441 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
442 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
443 ieee80211_sta_send_associnfo(dev, ifsta);
445 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
447 netif_carrier_off(dev);
448 ieee80211_reset_erp_info(dev);
449 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
451 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
452 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
453 ifsta->last_probe = jiffies;
454 ieee80211_led_assoc(local, assoc);
457 static void ieee80211_set_disassoc(struct net_device *dev,
458 struct ieee80211_if_sta *ifsta, int deauth)
461 ifsta->auth_tries = 0;
462 ifsta->assoc_tries = 0;
463 ieee80211_set_associated(dev, ifsta, 0);
466 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
469 struct ieee80211_sub_if_data *sdata;
470 struct ieee80211_tx_packet_data *pkt_data;
472 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
473 skb->dev = sdata->local->mdev;
474 skb_set_mac_header(skb, 0);
475 skb_set_network_header(skb, 0);
476 skb_set_transport_header(skb, 0);
478 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
479 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
480 pkt_data->ifindex = sdata->dev->ifindex;
482 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
488 static void ieee80211_send_auth(struct net_device *dev,
489 struct ieee80211_if_sta *ifsta,
490 int transaction, u8 *extra, size_t extra_len,
493 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
495 struct ieee80211_mgmt *mgmt;
497 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
498 sizeof(*mgmt) + 6 + extra_len);
500 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
501 "frame\n", dev->name);
504 skb_reserve(skb, local->hw.extra_tx_headroom);
506 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
507 memset(mgmt, 0, 24 + 6);
508 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
509 IEEE80211_STYPE_AUTH);
511 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
512 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
513 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
514 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
515 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
516 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
517 ifsta->auth_transaction = transaction + 1;
518 mgmt->u.auth.status_code = cpu_to_le16(0);
520 memcpy(skb_put(skb, extra_len), extra, extra_len);
522 ieee80211_sta_tx(dev, skb, encrypt);
526 static void ieee80211_authenticate(struct net_device *dev,
527 struct ieee80211_if_sta *ifsta)
529 DECLARE_MAC_BUF(mac);
532 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
533 printk(KERN_DEBUG "%s: authentication with AP %s"
535 dev->name, print_mac(mac, ifsta->bssid));
536 ifsta->state = IEEE80211_DISABLED;
540 ifsta->state = IEEE80211_AUTHENTICATE;
541 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
542 dev->name, print_mac(mac, ifsta->bssid));
544 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
546 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
550 static void ieee80211_send_assoc(struct net_device *dev,
551 struct ieee80211_if_sta *ifsta)
553 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
554 struct ieee80211_hw_mode *mode;
556 struct ieee80211_mgmt *mgmt;
560 struct ieee80211_sta_bss *bss;
563 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
564 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
567 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
568 "frame\n", dev->name);
571 skb_reserve(skb, local->hw.extra_tx_headroom);
573 mode = local->oper_hw_mode;
574 capab = ifsta->capab;
575 if (mode->mode == MODE_IEEE80211G) {
576 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
577 WLAN_CAPABILITY_SHORT_PREAMBLE;
579 bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel,
580 ifsta->ssid, ifsta->ssid_len);
582 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
583 capab |= WLAN_CAPABILITY_PRIVACY;
587 ieee80211_rx_bss_put(dev, bss);
590 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
592 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
593 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
594 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
596 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
598 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
599 IEEE80211_STYPE_REASSOC_REQ);
600 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
601 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
602 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
606 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
607 IEEE80211_STYPE_ASSOC_REQ);
608 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
609 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
613 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
614 *pos++ = WLAN_EID_SSID;
615 *pos++ = ifsta->ssid_len;
616 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
618 len = mode->num_rates;
621 pos = skb_put(skb, len + 2);
622 *pos++ = WLAN_EID_SUPP_RATES;
624 for (i = 0; i < len; i++) {
625 int rate = mode->rates[i].rate;
626 *pos++ = (u8) (rate / 5);
629 if (mode->num_rates > len) {
630 pos = skb_put(skb, mode->num_rates - len + 2);
631 *pos++ = WLAN_EID_EXT_SUPP_RATES;
632 *pos++ = mode->num_rates - len;
633 for (i = len; i < mode->num_rates; i++) {
634 int rate = mode->rates[i].rate;
635 *pos++ = (u8) (rate / 5);
639 if (ifsta->extra_ie) {
640 pos = skb_put(skb, ifsta->extra_ie_len);
641 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
644 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
645 pos = skb_put(skb, 9);
646 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
647 *pos++ = 7; /* len */
648 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
651 *pos++ = 2; /* WME */
652 *pos++ = 0; /* WME info */
653 *pos++ = 1; /* WME ver */
657 kfree(ifsta->assocreq_ies);
658 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
659 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
660 if (ifsta->assocreq_ies)
661 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
663 ieee80211_sta_tx(dev, skb, 0);
667 static void ieee80211_send_deauth(struct net_device *dev,
668 struct ieee80211_if_sta *ifsta, u16 reason)
670 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
672 struct ieee80211_mgmt *mgmt;
674 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
676 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
677 "frame\n", dev->name);
680 skb_reserve(skb, local->hw.extra_tx_headroom);
682 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
684 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
685 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
686 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
687 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
688 IEEE80211_STYPE_DEAUTH);
690 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
692 ieee80211_sta_tx(dev, skb, 0);
696 static void ieee80211_send_disassoc(struct net_device *dev,
697 struct ieee80211_if_sta *ifsta, u16 reason)
699 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
701 struct ieee80211_mgmt *mgmt;
703 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
705 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
706 "frame\n", dev->name);
709 skb_reserve(skb, local->hw.extra_tx_headroom);
711 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
713 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
714 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
715 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
716 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
717 IEEE80211_STYPE_DISASSOC);
719 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
721 ieee80211_sta_tx(dev, skb, 0);
725 static int ieee80211_privacy_mismatch(struct net_device *dev,
726 struct ieee80211_if_sta *ifsta)
728 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
729 struct ieee80211_sta_bss *bss;
732 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL) ||
733 ifsta->key_management_enabled)
736 bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel,
737 ifsta->ssid, ifsta->ssid_len);
741 if (ieee80211_sta_wep_configured(dev) !=
742 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
745 ieee80211_rx_bss_put(dev, bss);
751 static void ieee80211_associate(struct net_device *dev,
752 struct ieee80211_if_sta *ifsta)
754 DECLARE_MAC_BUF(mac);
756 ifsta->assoc_tries++;
757 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
758 printk(KERN_DEBUG "%s: association with AP %s"
760 dev->name, print_mac(mac, ifsta->bssid));
761 ifsta->state = IEEE80211_DISABLED;
765 ifsta->state = IEEE80211_ASSOCIATE;
766 printk(KERN_DEBUG "%s: associate with AP %s\n",
767 dev->name, print_mac(mac, ifsta->bssid));
768 if (ieee80211_privacy_mismatch(dev, ifsta)) {
769 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
770 "mixed-cell disabled - abort association\n", dev->name);
771 ifsta->state = IEEE80211_DISABLED;
775 ieee80211_send_assoc(dev, ifsta);
777 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
781 static void ieee80211_associated(struct net_device *dev,
782 struct ieee80211_if_sta *ifsta)
784 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
785 struct sta_info *sta;
787 DECLARE_MAC_BUF(mac);
789 /* TODO: start monitoring current AP signal quality and number of
790 * missed beacons. Scan other channels every now and then and search
792 /* TODO: remove expired BSSes */
794 ifsta->state = IEEE80211_ASSOCIATED;
796 sta = sta_info_get(local, ifsta->bssid);
798 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
799 dev->name, print_mac(mac, ifsta->bssid));
803 if (time_after(jiffies,
804 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
805 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
806 printk(KERN_DEBUG "%s: No ProbeResp from "
807 "current AP %s - assume out of "
809 dev->name, print_mac(mac, ifsta->bssid));
813 ieee80211_send_probe_req(dev, ifsta->bssid,
815 local->scan_ssid_len);
816 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
818 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
819 if (time_after(jiffies, ifsta->last_probe +
820 IEEE80211_PROBE_INTERVAL)) {
821 ifsta->last_probe = jiffies;
822 ieee80211_send_probe_req(dev, ifsta->bssid,
830 union iwreq_data wrqu;
831 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
832 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
833 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
834 mod_timer(&ifsta->timer, jiffies +
835 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
837 mod_timer(&ifsta->timer, jiffies +
838 IEEE80211_MONITORING_INTERVAL);
843 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
844 u8 *ssid, size_t ssid_len)
846 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
847 struct ieee80211_hw_mode *mode;
849 struct ieee80211_mgmt *mgmt;
850 u8 *pos, *supp_rates, *esupp_rates = NULL;
853 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
855 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
856 "request\n", dev->name);
859 skb_reserve(skb, local->hw.extra_tx_headroom);
861 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
863 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
864 IEEE80211_STYPE_PROBE_REQ);
865 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
867 memcpy(mgmt->da, dst, ETH_ALEN);
868 memcpy(mgmt->bssid, dst, ETH_ALEN);
870 memset(mgmt->da, 0xff, ETH_ALEN);
871 memset(mgmt->bssid, 0xff, ETH_ALEN);
873 pos = skb_put(skb, 2 + ssid_len);
874 *pos++ = WLAN_EID_SSID;
876 memcpy(pos, ssid, ssid_len);
878 supp_rates = skb_put(skb, 2);
879 supp_rates[0] = WLAN_EID_SUPP_RATES;
881 mode = local->oper_hw_mode;
882 for (i = 0; i < mode->num_rates; i++) {
883 struct ieee80211_rate *rate = &mode->rates[i];
884 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
887 pos = skb_put(skb, 1);
889 } else if (supp_rates[1] == 8) {
890 esupp_rates = skb_put(skb, 3);
891 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
893 pos = &esupp_rates[2];
895 pos = skb_put(skb, 1);
898 *pos = rate->rate / 5;
901 ieee80211_sta_tx(dev, skb, 0);
905 static int ieee80211_sta_wep_configured(struct net_device *dev)
907 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
908 if (!sdata || !sdata->default_key ||
909 sdata->default_key->conf.alg != ALG_WEP)
915 static void ieee80211_auth_completed(struct net_device *dev,
916 struct ieee80211_if_sta *ifsta)
918 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
919 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
920 ieee80211_associate(dev, ifsta);
924 static void ieee80211_auth_challenge(struct net_device *dev,
925 struct ieee80211_if_sta *ifsta,
926 struct ieee80211_mgmt *mgmt,
930 struct ieee802_11_elems elems;
932 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
933 pos = mgmt->u.auth.variable;
934 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
936 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
940 if (!elems.challenge) {
941 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
942 "frame\n", dev->name);
945 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
946 elems.challenge_len + 2, 1);
950 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
951 struct ieee80211_if_sta *ifsta,
952 struct ieee80211_mgmt *mgmt,
955 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
956 u16 auth_alg, auth_transaction, status_code;
957 DECLARE_MAC_BUF(mac);
959 if (ifsta->state != IEEE80211_AUTHENTICATE &&
960 sdata->type != IEEE80211_IF_TYPE_IBSS) {
961 printk(KERN_DEBUG "%s: authentication frame received from "
962 "%s, but not in authenticate state - ignored\n",
963 dev->name, print_mac(mac, mgmt->sa));
968 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
969 "received from %s - ignored\n",
970 dev->name, len, print_mac(mac, mgmt->sa));
974 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
975 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
976 printk(KERN_DEBUG "%s: authentication frame received from "
977 "unknown AP (SA=%s BSSID=%s) - "
978 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
979 print_mac(mac, mgmt->bssid));
983 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
984 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
985 printk(KERN_DEBUG "%s: authentication frame received from "
986 "unknown BSSID (SA=%s BSSID=%s) - "
987 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
988 print_mac(mac, mgmt->bssid));
992 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
993 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
994 status_code = le16_to_cpu(mgmt->u.auth.status_code);
996 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
997 "transaction=%d status=%d)\n",
998 dev->name, print_mac(mac, mgmt->sa), auth_alg,
999 auth_transaction, status_code);
1001 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
1002 /* IEEE 802.11 standard does not require authentication in IBSS
1003 * networks and most implementations do not seem to use it.
1004 * However, try to reply to authentication attempts if someone
1005 * has actually implemented this.
1006 * TODO: Could implement shared key authentication. */
1007 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1008 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1009 "frame (alg=%d transaction=%d)\n",
1010 dev->name, auth_alg, auth_transaction);
1013 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1016 if (auth_alg != ifsta->auth_alg ||
1017 auth_transaction != ifsta->auth_transaction) {
1018 printk(KERN_DEBUG "%s: unexpected authentication frame "
1019 "(alg=%d transaction=%d)\n",
1020 dev->name, auth_alg, auth_transaction);
1024 if (status_code != WLAN_STATUS_SUCCESS) {
1025 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1026 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1027 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1029 const int num_algs = ARRAY_SIZE(algs);
1031 algs[0] = algs[1] = algs[2] = 0xff;
1032 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1033 algs[0] = WLAN_AUTH_OPEN;
1034 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1035 algs[1] = WLAN_AUTH_SHARED_KEY;
1036 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1037 algs[2] = WLAN_AUTH_LEAP;
1038 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1040 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1044 for (i = 0; i < num_algs; i++) {
1046 if (pos >= num_algs)
1048 if (algs[pos] == ifsta->auth_alg ||
1051 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1052 !ieee80211_sta_wep_configured(dev))
1054 ifsta->auth_alg = algs[pos];
1055 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1057 dev->name, ifsta->auth_alg);
1064 switch (ifsta->auth_alg) {
1065 case WLAN_AUTH_OPEN:
1066 case WLAN_AUTH_LEAP:
1067 ieee80211_auth_completed(dev, ifsta);
1069 case WLAN_AUTH_SHARED_KEY:
1070 if (ifsta->auth_transaction == 4)
1071 ieee80211_auth_completed(dev, ifsta);
1073 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1079 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1080 struct ieee80211_if_sta *ifsta,
1081 struct ieee80211_mgmt *mgmt,
1085 DECLARE_MAC_BUF(mac);
1088 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1089 "received from %s - ignored\n",
1090 dev->name, len, print_mac(mac, mgmt->sa));
1094 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1095 printk(KERN_DEBUG "%s: deauthentication frame received from "
1096 "unknown AP (SA=%s BSSID=%s) - "
1097 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1098 print_mac(mac, mgmt->bssid));
1102 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1104 printk(KERN_DEBUG "%s: RX deauthentication from %s"
1106 dev->name, print_mac(mac, mgmt->sa), reason_code);
1108 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1109 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1112 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1113 ifsta->state == IEEE80211_ASSOCIATE ||
1114 ifsta->state == IEEE80211_ASSOCIATED) {
1115 ifsta->state = IEEE80211_AUTHENTICATE;
1116 mod_timer(&ifsta->timer, jiffies +
1117 IEEE80211_RETRY_AUTH_INTERVAL);
1120 ieee80211_set_disassoc(dev, ifsta, 1);
1121 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1125 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1126 struct ieee80211_if_sta *ifsta,
1127 struct ieee80211_mgmt *mgmt,
1131 DECLARE_MAC_BUF(mac);
1134 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1135 "received from %s - ignored\n",
1136 dev->name, len, print_mac(mac, mgmt->sa));
1140 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1141 printk(KERN_DEBUG "%s: disassociation frame received from "
1142 "unknown AP (SA=%s BSSID=%s) - "
1143 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1144 print_mac(mac, mgmt->bssid));
1148 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1150 printk(KERN_DEBUG "%s: RX disassociation from %s"
1152 dev->name, print_mac(mac, mgmt->sa), reason_code);
1154 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1155 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1157 if (ifsta->state == IEEE80211_ASSOCIATED) {
1158 ifsta->state = IEEE80211_ASSOCIATE;
1159 mod_timer(&ifsta->timer, jiffies +
1160 IEEE80211_RETRY_AUTH_INTERVAL);
1163 ieee80211_set_disassoc(dev, ifsta, 0);
1167 static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1168 struct ieee80211_if_sta *ifsta,
1169 struct ieee80211_mgmt *mgmt,
1173 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1174 struct ieee80211_hw_mode *mode;
1175 struct sta_info *sta;
1177 u16 capab_info, status_code, aid;
1178 struct ieee802_11_elems elems;
1181 DECLARE_MAC_BUF(mac);
1183 /* AssocResp and ReassocResp have identical structure, so process both
1184 * of them in this function. */
1186 if (ifsta->state != IEEE80211_ASSOCIATE) {
1187 printk(KERN_DEBUG "%s: association frame received from "
1188 "%s, but not in associate state - ignored\n",
1189 dev->name, print_mac(mac, mgmt->sa));
1194 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1195 "received from %s - ignored\n",
1196 dev->name, len, print_mac(mac, mgmt->sa));
1200 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1201 printk(KERN_DEBUG "%s: association frame received from "
1202 "unknown AP (SA=%s BSSID=%s) - "
1203 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1204 print_mac(mac, mgmt->bssid));
1208 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1209 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1210 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1212 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1213 "status=%d aid=%d)\n",
1214 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1215 capab_info, status_code, aid & ~(BIT(15) | BIT(14)));
1217 if (status_code != WLAN_STATUS_SUCCESS) {
1218 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1219 dev->name, status_code);
1220 /* if this was a reassociation, ensure we try a "full"
1221 * association next time. This works around some broken APs
1222 * which do not correctly reject reassociation requests. */
1223 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1227 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1228 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1229 "set\n", dev->name, aid);
1230 aid &= ~(BIT(15) | BIT(14));
1232 pos = mgmt->u.assoc_resp.variable;
1233 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1235 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1240 if (!elems.supp_rates) {
1241 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1246 /* it probably doesn't, but if the frame includes an ERP value then
1247 * update our stored copy */
1248 if (elems.erp_info && elems.erp_info_len >= 1) {
1249 struct ieee80211_sta_bss *bss
1250 = ieee80211_rx_bss_get(dev, ifsta->bssid,
1251 local->hw.conf.channel,
1252 ifsta->ssid, ifsta->ssid_len);
1254 bss->erp_value = elems.erp_info[0];
1255 bss->has_erp_value = 1;
1256 ieee80211_rx_bss_put(dev, bss);
1260 printk(KERN_DEBUG "%s: associated\n", dev->name);
1262 ifsta->ap_capab = capab_info;
1264 kfree(ifsta->assocresp_ies);
1265 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1266 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1267 if (ifsta->assocresp_ies)
1268 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1270 ieee80211_set_associated(dev, ifsta, 1);
1272 /* Add STA entry for the AP */
1273 sta = sta_info_get(local, ifsta->bssid);
1275 struct ieee80211_sta_bss *bss;
1276 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
1278 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1279 " AP\n", dev->name);
1282 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
1283 local->hw.conf.channel,
1284 ifsta->ssid, ifsta->ssid_len);
1286 sta->last_rssi = bss->rssi;
1287 sta->last_signal = bss->signal;
1288 sta->last_noise = bss->noise;
1289 ieee80211_rx_bss_put(dev, bss);
1294 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
1297 mode = local->oper_hw_mode;
1298 for (i = 0; i < elems.supp_rates_len; i++) {
1299 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1300 for (j = 0; j < mode->num_rates; j++)
1301 if (mode->rates[j].rate == rate)
1304 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1305 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1306 for (j = 0; j < mode->num_rates; j++)
1307 if (mode->rates[j].rate == rate)
1310 sta->supp_rates = rates;
1312 rate_control_rate_init(sta, local);
1314 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1315 sta->flags |= WLAN_STA_WME;
1316 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1317 elems.wmm_param_len);
1323 ieee80211_associated(dev, ifsta);
1327 /* Caller must hold local->sta_bss_lock */
1328 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1329 struct ieee80211_sta_bss *bss)
1331 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1332 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1333 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1337 /* Caller must hold local->sta_bss_lock */
1338 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1339 struct ieee80211_sta_bss *bss)
1341 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1342 struct ieee80211_sta_bss *b, *prev = NULL;
1343 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1347 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1350 prev->hnext = bss->hnext;
1359 static struct ieee80211_sta_bss *
1360 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int channel,
1361 u8 *ssid, u8 ssid_len)
1363 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1364 struct ieee80211_sta_bss *bss;
1366 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1369 atomic_inc(&bss->users);
1370 atomic_inc(&bss->users);
1371 memcpy(bss->bssid, bssid, ETH_ALEN);
1372 bss->channel = channel;
1373 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1374 memcpy(bss->ssid, ssid, ssid_len);
1375 bss->ssid_len = ssid_len;
1378 spin_lock_bh(&local->sta_bss_lock);
1379 /* TODO: order by RSSI? */
1380 list_add_tail(&bss->list, &local->sta_bss_list);
1381 __ieee80211_rx_bss_hash_add(dev, bss);
1382 spin_unlock_bh(&local->sta_bss_lock);
1387 static struct ieee80211_sta_bss *
1388 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel,
1389 u8 *ssid, u8 ssid_len)
1391 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1392 struct ieee80211_sta_bss *bss;
1394 spin_lock_bh(&local->sta_bss_lock);
1395 bss = local->sta_bss_hash[STA_HASH(bssid)];
1397 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
1398 bss->channel == channel &&
1399 bss->ssid_len == ssid_len &&
1400 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
1401 atomic_inc(&bss->users);
1406 spin_unlock_bh(&local->sta_bss_lock);
1411 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1420 static void ieee80211_rx_bss_put(struct net_device *dev,
1421 struct ieee80211_sta_bss *bss)
1423 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1424 if (!atomic_dec_and_test(&bss->users))
1427 spin_lock_bh(&local->sta_bss_lock);
1428 __ieee80211_rx_bss_hash_del(dev, bss);
1429 list_del(&bss->list);
1430 spin_unlock_bh(&local->sta_bss_lock);
1431 ieee80211_rx_bss_free(bss);
1435 void ieee80211_rx_bss_list_init(struct net_device *dev)
1437 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1438 spin_lock_init(&local->sta_bss_lock);
1439 INIT_LIST_HEAD(&local->sta_bss_list);
1443 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1445 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1446 struct ieee80211_sta_bss *bss, *tmp;
1448 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1449 ieee80211_rx_bss_put(dev, bss);
1453 static void ieee80211_rx_bss_info(struct net_device *dev,
1454 struct ieee80211_mgmt *mgmt,
1456 struct ieee80211_rx_status *rx_status,
1459 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1460 struct ieee802_11_elems elems;
1462 int channel, invalid = 0, clen;
1463 struct ieee80211_sta_bss *bss;
1464 struct sta_info *sta;
1465 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1467 DECLARE_MAC_BUF(mac);
1468 DECLARE_MAC_BUF(mac2);
1470 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1471 return; /* ignore ProbeResp to foreign address */
1474 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
1475 dev->name, beacon ? "Beacon" : "Probe Response",
1476 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
1479 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1483 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1485 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1486 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1487 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1488 static unsigned long last_tsf_debug = 0;
1490 if (local->ops->get_tsf)
1491 tsf = local->ops->get_tsf(local_to_hw(local));
1494 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1495 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1496 "%s TSF=0x%llx BCN=0x%llx diff=%lld "
1498 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
1499 (unsigned long long)tsf,
1500 (unsigned long long)timestamp,
1501 (unsigned long long)(tsf - timestamp),
1503 last_tsf_debug = jiffies;
1505 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1508 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1509 &elems) == ParseFailed)
1512 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1513 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1514 (sta = sta_info_get(local, mgmt->sa))) {
1515 struct ieee80211_hw_mode *mode;
1516 struct ieee80211_rate *rates;
1518 u32 supp_rates, prev_rates;
1521 mode = local->sta_scanning ?
1522 local->scan_hw_mode : local->oper_hw_mode;
1523 rates = mode->rates;
1524 num_rates = mode->num_rates;
1527 for (i = 0; i < elems.supp_rates_len +
1528 elems.ext_supp_rates_len; i++) {
1531 if (i < elems.supp_rates_len)
1532 rate = elems.supp_rates[i];
1533 else if (elems.ext_supp_rates)
1534 rate = elems.ext_supp_rates
1535 [i - elems.supp_rates_len];
1536 own_rate = 5 * (rate & 0x7f);
1537 for (j = 0; j < num_rates; j++)
1538 if (rates[j].rate == own_rate)
1539 supp_rates |= BIT(j);
1542 prev_rates = sta->supp_rates;
1543 sta->supp_rates &= supp_rates;
1544 if (sta->supp_rates == 0) {
1545 /* No matching rates - this should not really happen.
1546 * Make sure that at least one rate is marked
1547 * supported to avoid issues with TX rate ctrl. */
1548 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1550 if (sta->supp_rates != prev_rates) {
1551 printk(KERN_DEBUG "%s: updated supp_rates set for "
1552 "%s based on beacon info (0x%x & 0x%x -> "
1554 dev->name, print_mac(mac, sta->addr), prev_rates,
1555 supp_rates, sta->supp_rates);
1563 if (elems.ds_params && elems.ds_params_len == 1)
1564 channel = elems.ds_params[0];
1566 channel = rx_status->channel;
1568 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, channel,
1569 elems.ssid, elems.ssid_len);
1571 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, channel,
1572 elems.ssid, elems.ssid_len);
1577 /* TODO: order by RSSI? */
1578 spin_lock_bh(&local->sta_bss_lock);
1579 list_move_tail(&bss->list, &local->sta_bss_list);
1580 spin_unlock_bh(&local->sta_bss_lock);
1584 if (bss->probe_resp && beacon) {
1585 /* Do not allow beacon to override data from Probe Response. */
1586 ieee80211_rx_bss_put(dev, bss);
1590 /* save the ERP value so that it is available at association time */
1591 if (elems.erp_info && elems.erp_info_len >= 1) {
1592 bss->erp_value = elems.erp_info[0];
1593 bss->has_erp_value = 1;
1596 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1597 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1599 bss->supp_rates_len = 0;
1600 if (elems.supp_rates) {
1601 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1602 if (clen > elems.supp_rates_len)
1603 clen = elems.supp_rates_len;
1604 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1606 bss->supp_rates_len += clen;
1608 if (elems.ext_supp_rates) {
1609 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1610 if (clen > elems.ext_supp_rates_len)
1611 clen = elems.ext_supp_rates_len;
1612 memcpy(&bss->supp_rates[bss->supp_rates_len],
1613 elems.ext_supp_rates, clen);
1614 bss->supp_rates_len += clen;
1618 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1619 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1621 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1623 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1624 bss->wpa_ie_len = elems.wpa_len + 2;
1626 bss->wpa_ie_len = 0;
1627 } else if (!elems.wpa && bss->wpa_ie) {
1630 bss->wpa_ie_len = 0;
1634 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1635 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1637 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1639 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1640 bss->rsn_ie_len = elems.rsn_len + 2;
1642 bss->rsn_ie_len = 0;
1643 } else if (!elems.rsn && bss->rsn_ie) {
1646 bss->rsn_ie_len = 0;
1649 if (elems.wmm_param &&
1650 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1651 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1653 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1655 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1656 elems.wmm_param_len + 2);
1657 bss->wmm_ie_len = elems.wmm_param_len + 2;
1659 bss->wmm_ie_len = 0;
1660 } else if (!elems.wmm_param && bss->wmm_ie) {
1663 bss->wmm_ie_len = 0;
1667 bss->hw_mode = rx_status->phymode;
1668 bss->freq = rx_status->freq;
1669 if (channel != rx_status->channel &&
1670 (bss->hw_mode == MODE_IEEE80211G ||
1671 bss->hw_mode == MODE_IEEE80211B) &&
1672 channel >= 1 && channel <= 14) {
1673 static const int freq_list[] = {
1674 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1675 2447, 2452, 2457, 2462, 2467, 2472, 2484
1677 /* IEEE 802.11g/b mode can receive packets from neighboring
1678 * channels, so map the channel into frequency. */
1679 bss->freq = freq_list[channel - 1];
1681 bss->timestamp = timestamp;
1682 bss->last_update = jiffies;
1683 bss->rssi = rx_status->ssi;
1684 bss->signal = rx_status->signal;
1685 bss->noise = rx_status->noise;
1688 ieee80211_rx_bss_put(dev, bss);
1692 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1693 struct ieee80211_mgmt *mgmt,
1695 struct ieee80211_rx_status *rx_status)
1697 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1701 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1702 struct ieee80211_mgmt *mgmt,
1704 struct ieee80211_rx_status *rx_status)
1706 struct ieee80211_sub_if_data *sdata;
1707 struct ieee80211_if_sta *ifsta;
1709 struct ieee802_11_elems elems;
1711 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1713 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1714 if (sdata->type != IEEE80211_IF_TYPE_STA)
1716 ifsta = &sdata->u.sta;
1718 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1719 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1722 /* Process beacon from the current BSS */
1723 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1727 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1728 &elems) == ParseFailed)
1731 if (elems.erp_info && elems.erp_info_len >= 1)
1732 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
1734 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1735 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1736 elems.wmm_param_len);
1741 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1742 struct ieee80211_if_sta *ifsta,
1743 struct ieee80211_mgmt *mgmt,
1745 struct ieee80211_rx_status *rx_status)
1747 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1748 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1750 struct sk_buff *skb;
1751 struct ieee80211_mgmt *resp;
1753 DECLARE_MAC_BUF(mac);
1754 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1755 DECLARE_MAC_BUF(mac2);
1756 DECLARE_MAC_BUF(mac3);
1759 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1760 ifsta->state != IEEE80211_IBSS_JOINED ||
1761 len < 24 + 2 || !ifsta->probe_resp)
1764 if (local->ops->tx_last_beacon)
1765 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1769 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1770 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1771 "%s (tx_last_beacon=%d)\n",
1772 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1773 print_mac(mac3, mgmt->bssid), tx_last_beacon);
1774 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1776 if (!tx_last_beacon)
1779 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1780 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1783 end = ((u8 *) mgmt) + len;
1784 pos = mgmt->u.probe_req.variable;
1785 if (pos[0] != WLAN_EID_SSID ||
1786 pos + 2 + pos[1] > end) {
1787 if (net_ratelimit()) {
1788 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1790 dev->name, print_mac(mac, mgmt->sa));
1795 (pos[1] != ifsta->ssid_len ||
1796 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1797 /* Ignore ProbeReq for foreign SSID */
1801 /* Reply with ProbeResp */
1802 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1806 resp = (struct ieee80211_mgmt *) skb->data;
1807 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1808 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1809 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1810 dev->name, print_mac(mac, resp->da));
1811 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1812 ieee80211_sta_tx(dev, skb, 0);
1816 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1817 struct ieee80211_rx_status *rx_status)
1819 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1820 struct ieee80211_sub_if_data *sdata;
1821 struct ieee80211_if_sta *ifsta;
1822 struct ieee80211_mgmt *mgmt;
1828 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1829 ifsta = &sdata->u.sta;
1831 mgmt = (struct ieee80211_mgmt *) skb->data;
1832 fc = le16_to_cpu(mgmt->frame_control);
1834 switch (fc & IEEE80211_FCTL_STYPE) {
1835 case IEEE80211_STYPE_PROBE_REQ:
1836 case IEEE80211_STYPE_PROBE_RESP:
1837 case IEEE80211_STYPE_BEACON:
1838 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1839 case IEEE80211_STYPE_AUTH:
1840 case IEEE80211_STYPE_ASSOC_RESP:
1841 case IEEE80211_STYPE_REASSOC_RESP:
1842 case IEEE80211_STYPE_DEAUTH:
1843 case IEEE80211_STYPE_DISASSOC:
1844 skb_queue_tail(&ifsta->skb_queue, skb);
1845 queue_work(local->hw.workqueue, &ifsta->work);
1848 printk(KERN_DEBUG "%s: received unknown management frame - "
1849 "stype=%d\n", dev->name,
1850 (fc & IEEE80211_FCTL_STYPE) >> 4);
1859 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1860 struct sk_buff *skb)
1862 struct ieee80211_rx_status *rx_status;
1863 struct ieee80211_sub_if_data *sdata;
1864 struct ieee80211_if_sta *ifsta;
1865 struct ieee80211_mgmt *mgmt;
1868 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1869 ifsta = &sdata->u.sta;
1871 rx_status = (struct ieee80211_rx_status *) skb->cb;
1872 mgmt = (struct ieee80211_mgmt *) skb->data;
1873 fc = le16_to_cpu(mgmt->frame_control);
1875 switch (fc & IEEE80211_FCTL_STYPE) {
1876 case IEEE80211_STYPE_PROBE_REQ:
1877 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1880 case IEEE80211_STYPE_PROBE_RESP:
1881 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1883 case IEEE80211_STYPE_BEACON:
1884 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1886 case IEEE80211_STYPE_AUTH:
1887 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1889 case IEEE80211_STYPE_ASSOC_RESP:
1890 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1892 case IEEE80211_STYPE_REASSOC_RESP:
1893 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1895 case IEEE80211_STYPE_DEAUTH:
1896 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1898 case IEEE80211_STYPE_DISASSOC:
1899 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1907 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1908 struct ieee80211_rx_status *rx_status)
1910 struct ieee80211_mgmt *mgmt;
1913 if (skb->len < 24) {
1918 mgmt = (struct ieee80211_mgmt *) skb->data;
1919 fc = le16_to_cpu(mgmt->frame_control);
1921 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1922 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1923 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1924 skb->len, rx_status);
1925 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1926 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1935 static int ieee80211_sta_active_ibss(struct net_device *dev)
1937 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1939 struct sta_info *sta;
1941 read_lock_bh(&local->sta_lock);
1942 list_for_each_entry(sta, &local->sta_list, list) {
1943 if (sta->dev == dev &&
1944 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1950 read_unlock_bh(&local->sta_lock);
1956 static void ieee80211_sta_expire(struct net_device *dev)
1958 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1959 struct sta_info *sta, *tmp;
1960 LIST_HEAD(tmp_list);
1961 DECLARE_MAC_BUF(mac);
1963 write_lock_bh(&local->sta_lock);
1964 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1965 if (time_after(jiffies, sta->last_rx +
1966 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1967 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
1968 dev->name, print_mac(mac, sta->addr));
1969 __sta_info_get(sta);
1970 sta_info_remove(sta);
1971 list_add(&sta->list, &tmp_list);
1973 write_unlock_bh(&local->sta_lock);
1975 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
1982 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1983 struct ieee80211_if_sta *ifsta)
1985 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1987 ieee80211_sta_expire(dev);
1988 if (ieee80211_sta_active_ibss(dev))
1991 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1992 "IBSS networks with same SSID (merge)\n", dev->name);
1993 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1997 void ieee80211_sta_timer(unsigned long data)
1999 struct ieee80211_sub_if_data *sdata =
2000 (struct ieee80211_sub_if_data *) data;
2001 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2002 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2004 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2005 queue_work(local->hw.workqueue, &ifsta->work);
2009 void ieee80211_sta_work(struct work_struct *work)
2011 struct ieee80211_sub_if_data *sdata =
2012 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2013 struct net_device *dev = sdata->dev;
2014 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2015 struct ieee80211_if_sta *ifsta;
2016 struct sk_buff *skb;
2018 if (!netif_running(dev))
2021 if (local->sta_scanning)
2024 if (sdata->type != IEEE80211_IF_TYPE_STA &&
2025 sdata->type != IEEE80211_IF_TYPE_IBSS) {
2026 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2027 "(type=%d)\n", dev->name, sdata->type);
2030 ifsta = &sdata->u.sta;
2032 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2033 ieee80211_sta_rx_queued_mgmt(dev, skb);
2035 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2036 ifsta->state != IEEE80211_ASSOCIATE &&
2037 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2038 ieee80211_sta_start_scan(dev, NULL, 0);
2042 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2043 if (ieee80211_sta_config_auth(dev, ifsta))
2045 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2046 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2049 switch (ifsta->state) {
2050 case IEEE80211_DISABLED:
2052 case IEEE80211_AUTHENTICATE:
2053 ieee80211_authenticate(dev, ifsta);
2055 case IEEE80211_ASSOCIATE:
2056 ieee80211_associate(dev, ifsta);
2058 case IEEE80211_ASSOCIATED:
2059 ieee80211_associated(dev, ifsta);
2061 case IEEE80211_IBSS_SEARCH:
2062 ieee80211_sta_find_ibss(dev, ifsta);
2064 case IEEE80211_IBSS_JOINED:
2065 ieee80211_sta_merge_ibss(dev, ifsta);
2068 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2073 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2074 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2075 "mixed-cell disabled - disassociate\n", dev->name);
2077 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2078 ieee80211_set_disassoc(dev, ifsta, 0);
2083 static void ieee80211_sta_reset_auth(struct net_device *dev,
2084 struct ieee80211_if_sta *ifsta)
2086 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2088 if (local->ops->reset_tsf) {
2089 /* Reset own TSF to allow time synchronization work. */
2090 local->ops->reset_tsf(local_to_hw(local));
2093 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2096 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2097 ifsta->auth_alg = WLAN_AUTH_OPEN;
2098 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2099 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2100 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2101 ifsta->auth_alg = WLAN_AUTH_LEAP;
2103 ifsta->auth_alg = WLAN_AUTH_OPEN;
2104 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2106 ifsta->auth_transaction = -1;
2107 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2108 ifsta->auth_tries = ifsta->assoc_tries = 0;
2109 netif_carrier_off(dev);
2113 void ieee80211_sta_req_auth(struct net_device *dev,
2114 struct ieee80211_if_sta *ifsta)
2116 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2117 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2119 if (sdata->type != IEEE80211_IF_TYPE_STA)
2122 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2123 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2124 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2125 IEEE80211_STA_AUTO_SSID_SEL))) {
2126 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2127 queue_work(local->hw.workqueue, &ifsta->work);
2131 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2132 const char *ssid, int ssid_len)
2134 int tmp, hidden_ssid;
2136 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2139 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2145 if (ssid[tmp] != '\0') {
2151 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2154 if (ssid_len == 1 && ssid[0] == ' ')
2160 static int ieee80211_sta_config_auth(struct net_device *dev,
2161 struct ieee80211_if_sta *ifsta)
2163 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2164 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2165 struct ieee80211_sta_bss *bss, *selected = NULL;
2166 int top_rssi = 0, freq;
2168 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2169 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
2170 ifsta->state = IEEE80211_AUTHENTICATE;
2171 ieee80211_sta_reset_auth(dev, ifsta);
2175 spin_lock_bh(&local->sta_bss_lock);
2176 freq = local->oper_channel->freq;
2177 list_for_each_entry(bss, &local->sta_bss_list, list) {
2178 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2181 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2182 !!sdata->default_key)
2185 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2189 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2190 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2193 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2194 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2197 if (!selected || top_rssi < bss->rssi) {
2199 top_rssi = bss->rssi;
2203 atomic_inc(&selected->users);
2204 spin_unlock_bh(&local->sta_bss_lock);
2207 ieee80211_set_channel(local, -1, selected->freq);
2208 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2209 ieee80211_sta_set_ssid(dev, selected->ssid,
2210 selected->ssid_len);
2211 ieee80211_sta_set_bssid(dev, selected->bssid);
2212 ieee80211_rx_bss_put(dev, selected);
2213 ifsta->state = IEEE80211_AUTHENTICATE;
2214 ieee80211_sta_reset_auth(dev, ifsta);
2217 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2218 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2219 ieee80211_sta_start_scan(dev, NULL, 0);
2221 ieee80211_sta_start_scan(dev, ifsta->ssid,
2223 ifsta->state = IEEE80211_AUTHENTICATE;
2224 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2226 ifsta->state = IEEE80211_DISABLED;
2231 static int ieee80211_sta_join_ibss(struct net_device *dev,
2232 struct ieee80211_if_sta *ifsta,
2233 struct ieee80211_sta_bss *bss)
2235 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2236 int res, rates, i, j;
2237 struct sk_buff *skb;
2238 struct ieee80211_mgmt *mgmt;
2239 struct ieee80211_tx_control control;
2240 struct ieee80211_rate *rate;
2241 struct ieee80211_hw_mode *mode;
2242 struct rate_control_extra extra;
2244 struct ieee80211_sub_if_data *sdata;
2246 /* Remove possible STA entries from other IBSS networks. */
2247 sta_info_flush(local, NULL);
2249 if (local->ops->reset_tsf) {
2250 /* Reset own TSF to allow time synchronization work. */
2251 local->ops->reset_tsf(local_to_hw(local));
2253 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2254 res = ieee80211_if_config(dev);
2258 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2260 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2261 sdata->drop_unencrypted = bss->capability &
2262 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2264 res = ieee80211_set_channel(local, -1, bss->freq);
2266 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2267 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2268 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2269 local->hw.conf.freq);
2273 /* Set beacon template based on scan results */
2274 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2279 skb_reserve(skb, local->hw.extra_tx_headroom);
2281 mgmt = (struct ieee80211_mgmt *)
2282 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2283 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2284 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2285 IEEE80211_STYPE_BEACON);
2286 memset(mgmt->da, 0xff, ETH_ALEN);
2287 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2288 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2289 mgmt->u.beacon.beacon_int =
2290 cpu_to_le16(local->hw.conf.beacon_int);
2291 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2293 pos = skb_put(skb, 2 + ifsta->ssid_len);
2294 *pos++ = WLAN_EID_SSID;
2295 *pos++ = ifsta->ssid_len;
2296 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2298 rates = bss->supp_rates_len;
2301 pos = skb_put(skb, 2 + rates);
2302 *pos++ = WLAN_EID_SUPP_RATES;
2304 memcpy(pos, bss->supp_rates, rates);
2306 pos = skb_put(skb, 2 + 1);
2307 *pos++ = WLAN_EID_DS_PARAMS;
2309 *pos++ = bss->channel;
2311 pos = skb_put(skb, 2 + 2);
2312 *pos++ = WLAN_EID_IBSS_PARAMS;
2314 /* FIX: set ATIM window based on scan results */
2318 if (bss->supp_rates_len > 8) {
2319 rates = bss->supp_rates_len - 8;
2320 pos = skb_put(skb, 2 + rates);
2321 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2323 memcpy(pos, &bss->supp_rates[8], rates);
2326 memset(&control, 0, sizeof(control));
2327 memset(&extra, 0, sizeof(extra));
2328 extra.mode = local->oper_hw_mode;
2329 rate = rate_control_get_rate(local, dev, skb, &extra);
2331 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2332 "for IBSS beacon\n", dev->name);
2336 ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
2337 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2338 rate->val2 : rate->val;
2339 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2340 control.power_level = local->hw.conf.power_level;
2341 control.flags |= IEEE80211_TXCTL_NO_ACK;
2342 control.retry_limit = 1;
2344 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2345 if (ifsta->probe_resp) {
2346 mgmt = (struct ieee80211_mgmt *)
2347 ifsta->probe_resp->data;
2348 mgmt->frame_control =
2349 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2350 IEEE80211_STYPE_PROBE_RESP);
2352 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2353 "template for IBSS\n", dev->name);
2356 if (local->ops->beacon_update &&
2357 local->ops->beacon_update(local_to_hw(local),
2358 skb, &control) == 0) {
2359 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2360 "template based on scan results\n", dev->name);
2365 mode = local->oper_hw_mode;
2366 for (i = 0; i < bss->supp_rates_len; i++) {
2367 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2368 for (j = 0; j < mode->num_rates; j++)
2369 if (mode->rates[j].rate == bitrate)
2372 ifsta->supp_rates_bits = rates;
2376 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2377 "template\n", dev->name);
2381 ifsta->state = IEEE80211_IBSS_JOINED;
2382 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2384 ieee80211_rx_bss_put(dev, bss);
2390 static int ieee80211_sta_create_ibss(struct net_device *dev,
2391 struct ieee80211_if_sta *ifsta)
2393 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2394 struct ieee80211_sta_bss *bss;
2395 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2396 struct ieee80211_hw_mode *mode;
2397 u8 bssid[ETH_ALEN], *pos;
2399 DECLARE_MAC_BUF(mac);
2402 /* Easier testing, use fixed BSSID. */
2403 memset(bssid, 0xfe, ETH_ALEN);
2405 /* Generate random, not broadcast, locally administered BSSID. Mix in
2406 * own MAC address to make sure that devices that do not have proper
2407 * random number generator get different BSSID. */
2408 get_random_bytes(bssid, ETH_ALEN);
2409 for (i = 0; i < ETH_ALEN; i++)
2410 bssid[i] ^= dev->dev_addr[i];
2415 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2416 dev->name, print_mac(mac, bssid));
2418 bss = ieee80211_rx_bss_add(dev, bssid, local->hw.conf.channel,
2419 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2423 mode = local->oper_hw_mode;
2425 if (local->hw.conf.beacon_int == 0)
2426 local->hw.conf.beacon_int = 100;
2427 bss->beacon_int = local->hw.conf.beacon_int;
2428 bss->hw_mode = local->hw.conf.phymode;
2429 bss->freq = local->hw.conf.freq;
2430 bss->last_update = jiffies;
2431 bss->capability = WLAN_CAPABILITY_IBSS;
2432 if (sdata->default_key) {
2433 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2435 sdata->drop_unencrypted = 0;
2436 bss->supp_rates_len = mode->num_rates;
2437 pos = bss->supp_rates;
2438 for (i = 0; i < mode->num_rates; i++) {
2439 int rate = mode->rates[i].rate;
2440 *pos++ = (u8) (rate / 5);
2443 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2447 static int ieee80211_sta_find_ibss(struct net_device *dev,
2448 struct ieee80211_if_sta *ifsta)
2450 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2451 struct ieee80211_sta_bss *bss;
2455 DECLARE_MAC_BUF(mac);
2456 DECLARE_MAC_BUF(mac2);
2458 if (ifsta->ssid_len == 0)
2461 active_ibss = ieee80211_sta_active_ibss(dev);
2462 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2463 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2464 dev->name, active_ibss);
2465 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2466 spin_lock_bh(&local->sta_bss_lock);
2467 list_for_each_entry(bss, &local->sta_bss_list, list) {
2468 if (ifsta->ssid_len != bss->ssid_len ||
2469 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2470 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2472 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2473 printk(KERN_DEBUG " bssid=%s found\n",
2474 print_mac(mac, bss->bssid));
2475 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2476 memcpy(bssid, bss->bssid, ETH_ALEN);
2478 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2481 spin_unlock_bh(&local->sta_bss_lock);
2483 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2484 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
2485 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
2486 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2487 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2488 (bss = ieee80211_rx_bss_get(dev, bssid, local->hw.conf.channel,
2489 ifsta->ssid, ifsta->ssid_len))) {
2490 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2491 " based on configured SSID\n",
2492 dev->name, print_mac(mac, bssid));
2493 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2495 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2496 printk(KERN_DEBUG " did not try to join ibss\n");
2497 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2499 /* Selected IBSS not found in current scan results - try to scan */
2500 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2501 !ieee80211_sta_active_ibss(dev)) {
2502 mod_timer(&ifsta->timer, jiffies +
2503 IEEE80211_IBSS_MERGE_INTERVAL);
2504 } else if (time_after(jiffies, local->last_scan_completed +
2505 IEEE80211_SCAN_INTERVAL)) {
2506 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2507 "join\n", dev->name);
2508 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2510 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2511 int interval = IEEE80211_SCAN_INTERVAL;
2513 if (time_after(jiffies, ifsta->ibss_join_req +
2514 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2515 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2516 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2517 return ieee80211_sta_create_ibss(dev, ifsta);
2518 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2519 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2520 " configured channel %d (%d MHz)\n",
2521 dev->name, local->hw.conf.channel,
2522 local->hw.conf.freq);
2525 /* No IBSS found - decrease scan interval and continue
2527 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2530 ifsta->state = IEEE80211_IBSS_SEARCH;
2531 mod_timer(&ifsta->timer, jiffies + interval);
2539 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2541 struct ieee80211_sub_if_data *sdata;
2542 struct ieee80211_if_sta *ifsta;
2543 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2545 if (len > IEEE80211_MAX_SSID_LEN)
2548 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2550 if (local->ops->conf_tx) {
2551 struct ieee80211_tx_queue_params qparam;
2554 memset(&qparam, 0, sizeof(qparam));
2555 /* TODO: are these ok defaults for all hw_modes? */
2558 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2559 qparam.cw_max = 1023;
2560 qparam.burst_time = 0;
2561 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2563 local->ops->conf_tx(local_to_hw(local),
2564 i + IEEE80211_TX_QUEUE_DATA0,
2567 /* IBSS uses different parameters for Beacon sending */
2571 local->ops->conf_tx(local_to_hw(local),
2572 IEEE80211_TX_QUEUE_BEACON, &qparam);
2575 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2576 ifsta = &sdata->u.sta;
2578 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2579 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2580 memcpy(ifsta->ssid, ssid, len);
2581 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2582 ifsta->ssid_len = len;
2585 ifsta->flags |= IEEE80211_STA_SSID_SET;
2587 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2588 if (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2589 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2590 ifsta->ibss_join_req = jiffies;
2591 ifsta->state = IEEE80211_IBSS_SEARCH;
2592 return ieee80211_sta_find_ibss(dev, ifsta);
2598 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2600 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2601 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2602 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2603 *len = ifsta->ssid_len;
2608 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2610 struct ieee80211_sub_if_data *sdata;
2611 struct ieee80211_if_sta *ifsta;
2614 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2615 ifsta = &sdata->u.sta;
2617 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2618 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2619 res = ieee80211_if_config(dev);
2621 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2622 "the low-level driver\n", dev->name);
2627 if (is_valid_ether_addr(bssid))
2628 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2630 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2636 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2637 struct ieee80211_sub_if_data *sdata,
2640 struct sk_buff *skb;
2641 struct ieee80211_hdr *nullfunc;
2644 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2646 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2647 "frame\n", sdata->dev->name);
2650 skb_reserve(skb, local->hw.extra_tx_headroom);
2652 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2653 memset(nullfunc, 0, 24);
2654 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2655 IEEE80211_FCTL_TODS;
2657 fc |= IEEE80211_FCTL_PM;
2658 nullfunc->frame_control = cpu_to_le16(fc);
2659 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2660 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2661 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2663 ieee80211_sta_tx(sdata->dev, skb, 0);
2667 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2669 struct ieee80211_local *local = hw_to_local(hw);
2670 struct net_device *dev = local->scan_dev;
2671 struct ieee80211_sub_if_data *sdata;
2672 union iwreq_data wrqu;
2674 local->last_scan_completed = jiffies;
2676 local->sta_scanning = 0;
2678 if (ieee80211_hw_config(local))
2679 printk(KERN_DEBUG "%s: failed to restore operational"
2680 "channel after scan\n", dev->name);
2683 netif_tx_lock_bh(local->mdev);
2684 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
2685 local->ops->configure_filter(local_to_hw(local),
2686 FIF_BCN_PRBRESP_PROMISC,
2687 &local->filter_flags,
2688 local->mdev->mc_count,
2689 local->mdev->mc_list);
2691 netif_tx_unlock_bh(local->mdev);
2693 memset(&wrqu, 0, sizeof(wrqu));
2694 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2697 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2699 /* No need to wake the master device. */
2700 if (sdata->dev == local->mdev)
2703 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2704 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
2705 ieee80211_send_nullfunc(local, sdata, 0);
2706 ieee80211_sta_timer((unsigned long)sdata);
2709 netif_wake_queue(sdata->dev);
2713 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2714 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2715 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2716 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2717 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2718 !ieee80211_sta_active_ibss(dev)))
2719 ieee80211_sta_find_ibss(dev, ifsta);
2722 EXPORT_SYMBOL(ieee80211_scan_completed);
2724 void ieee80211_sta_scan_work(struct work_struct *work)
2726 struct ieee80211_local *local =
2727 container_of(work, struct ieee80211_local, scan_work.work);
2728 struct net_device *dev = local->scan_dev;
2729 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2730 struct ieee80211_hw_mode *mode;
2731 struct ieee80211_channel *chan;
2733 unsigned long next_delay = 0;
2735 if (!local->sta_scanning)
2738 switch (local->scan_state) {
2739 case SCAN_SET_CHANNEL:
2740 mode = local->scan_hw_mode;
2741 if (local->scan_hw_mode->list.next == &local->modes_list &&
2742 local->scan_channel_idx >= mode->num_channels) {
2743 ieee80211_scan_completed(local_to_hw(local));
2746 skip = !(local->enabled_modes & (1 << mode->mode));
2747 chan = &mode->channels[local->scan_channel_idx];
2748 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2749 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2750 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2751 (local->hw_modes & local->enabled_modes &
2752 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2757 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2758 dev->name, chan->chan, chan->freq);
2761 local->scan_channel = chan;
2762 if (ieee80211_hw_config(local)) {
2763 printk(KERN_DEBUG "%s: failed to set channel "
2764 "%d (%d MHz) for scan\n", dev->name,
2765 chan->chan, chan->freq);
2770 local->scan_channel_idx++;
2771 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2772 if (local->scan_hw_mode->list.next != &local->modes_list) {
2773 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2774 struct ieee80211_hw_mode,
2776 local->scan_channel_idx = 0;
2783 next_delay = IEEE80211_PROBE_DELAY +
2784 usecs_to_jiffies(local->hw.channel_change_time);
2785 local->scan_state = SCAN_SEND_PROBE;
2787 case SCAN_SEND_PROBE:
2788 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2789 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2790 local->scan_ssid_len);
2791 next_delay = IEEE80211_CHANNEL_TIME;
2793 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2794 local->scan_state = SCAN_SET_CHANNEL;
2798 if (local->sta_scanning)
2799 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2804 static int ieee80211_sta_start_scan(struct net_device *dev,
2805 u8 *ssid, size_t ssid_len)
2807 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2808 struct ieee80211_sub_if_data *sdata;
2810 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2813 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2814 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2817 * ScanType: ACTIVE, PASSIVE
2818 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2819 * a Probe frame during active scanning
2821 * MinChannelTime (>= ProbeDelay), in TU
2822 * MaxChannelTime: (>= MinChannelTime), in TU
2825 /* MLME-SCAN.confirm
2827 * ResultCode: SUCCESS, INVALID_PARAMETERS
2830 if (local->sta_scanning) {
2831 if (local->scan_dev == dev)
2836 if (local->ops->hw_scan) {
2837 int rc = local->ops->hw_scan(local_to_hw(local),
2840 local->sta_scanning = 1;
2841 local->scan_dev = dev;
2846 local->sta_scanning = 1;
2849 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2851 /* Don't stop the master interface, otherwise we can't transmit
2853 if (sdata->dev == local->mdev)
2856 netif_stop_queue(sdata->dev);
2857 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2858 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
2859 ieee80211_send_nullfunc(local, sdata, 1);
2864 local->scan_ssid_len = ssid_len;
2865 memcpy(local->scan_ssid, ssid, ssid_len);
2867 local->scan_ssid_len = 0;
2868 local->scan_state = SCAN_SET_CHANNEL;
2869 local->scan_hw_mode = list_entry(local->modes_list.next,
2870 struct ieee80211_hw_mode,
2872 local->scan_channel_idx = 0;
2873 local->scan_dev = dev;
2875 netif_tx_lock_bh(local->mdev);
2876 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
2877 local->ops->configure_filter(local_to_hw(local),
2878 FIF_BCN_PRBRESP_PROMISC,
2879 &local->filter_flags,
2880 local->mdev->mc_count,
2881 local->mdev->mc_list);
2882 netif_tx_unlock_bh(local->mdev);
2884 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2885 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2886 IEEE80211_CHANNEL_TIME);
2892 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2894 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2895 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2896 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2898 if (sdata->type != IEEE80211_IF_TYPE_STA)
2899 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2901 if (local->sta_scanning) {
2902 if (local->scan_dev == dev)
2907 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2908 queue_work(local->hw.workqueue, &ifsta->work);
2913 ieee80211_sta_scan_result(struct net_device *dev,
2914 struct ieee80211_sta_bss *bss,
2915 char *current_ev, char *end_buf)
2917 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2918 struct iw_event iwe;
2920 if (time_after(jiffies,
2921 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2924 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2927 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2928 !bss->wpa_ie && !bss->rsn_ie)
2931 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2932 (local->scan_ssid_len != bss->ssid_len ||
2933 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2936 memset(&iwe, 0, sizeof(iwe));
2937 iwe.cmd = SIOCGIWAP;
2938 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2939 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2940 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2943 memset(&iwe, 0, sizeof(iwe));
2944 iwe.cmd = SIOCGIWESSID;
2945 iwe.u.data.length = bss->ssid_len;
2946 iwe.u.data.flags = 1;
2947 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2950 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2951 memset(&iwe, 0, sizeof(iwe));
2952 iwe.cmd = SIOCGIWMODE;
2953 if (bss->capability & WLAN_CAPABILITY_ESS)
2954 iwe.u.mode = IW_MODE_MASTER;
2956 iwe.u.mode = IW_MODE_ADHOC;
2957 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2961 memset(&iwe, 0, sizeof(iwe));
2962 iwe.cmd = SIOCGIWFREQ;
2963 iwe.u.freq.m = bss->channel;
2965 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2967 iwe.u.freq.m = bss->freq * 100000;
2969 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2972 memset(&iwe, 0, sizeof(iwe));
2974 iwe.u.qual.qual = bss->signal;
2975 iwe.u.qual.level = bss->rssi;
2976 iwe.u.qual.noise = bss->noise;
2977 iwe.u.qual.updated = local->wstats_flags;
2978 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2981 memset(&iwe, 0, sizeof(iwe));
2982 iwe.cmd = SIOCGIWENCODE;
2983 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2984 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2986 iwe.u.data.flags = IW_ENCODE_DISABLED;
2987 iwe.u.data.length = 0;
2988 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2990 if (bss && bss->wpa_ie) {
2991 memset(&iwe, 0, sizeof(iwe));
2992 iwe.cmd = IWEVGENIE;
2993 iwe.u.data.length = bss->wpa_ie_len;
2994 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2998 if (bss && bss->rsn_ie) {
2999 memset(&iwe, 0, sizeof(iwe));
3000 iwe.cmd = IWEVGENIE;
3001 iwe.u.data.length = bss->rsn_ie_len;
3002 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3006 if (bss && bss->supp_rates_len > 0) {
3007 /* display all supported rates in readable format */
3008 char *p = current_ev + IW_EV_LCP_LEN;
3011 memset(&iwe, 0, sizeof(iwe));
3012 iwe.cmd = SIOCGIWRATE;
3013 /* Those two flags are ignored... */
3014 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3016 for (i = 0; i < bss->supp_rates_len; i++) {
3017 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3019 p = iwe_stream_add_value(current_ev, p,
3020 end_buf, &iwe, IW_EV_PARAM_LEN);
3027 buf = kmalloc(30, GFP_ATOMIC);
3029 memset(&iwe, 0, sizeof(iwe));
3030 iwe.cmd = IWEVCUSTOM;
3031 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3032 iwe.u.data.length = strlen(buf);
3033 current_ev = iwe_stream_add_point(current_ev, end_buf,
3042 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
3045 buf = kmalloc(100, GFP_ATOMIC);
3049 memset(&iwe, 0, sizeof(iwe));
3050 iwe.cmd = IWEVCUSTOM;
3051 sprintf(buf, "bcn_int=%d", bss->beacon_int);
3052 iwe.u.data.length = strlen(buf);
3053 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3056 memset(&iwe, 0, sizeof(iwe));
3057 iwe.cmd = IWEVCUSTOM;
3058 sprintf(buf, "capab=0x%04x", bss->capability);
3059 iwe.u.data.length = strlen(buf);
3060 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3071 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3073 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3074 char *current_ev = buf;
3075 char *end_buf = buf + len;
3076 struct ieee80211_sta_bss *bss;
3078 spin_lock_bh(&local->sta_bss_lock);
3079 list_for_each_entry(bss, &local->sta_bss_list, list) {
3080 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3081 spin_unlock_bh(&local->sta_bss_lock);
3084 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3087 spin_unlock_bh(&local->sta_bss_lock);
3088 return current_ev - buf;
3092 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3094 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3095 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3096 kfree(ifsta->extra_ie);
3098 ifsta->extra_ie = NULL;
3099 ifsta->extra_ie_len = 0;
3102 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3103 if (!ifsta->extra_ie) {
3104 ifsta->extra_ie_len = 0;
3107 memcpy(ifsta->extra_ie, ie, len);
3108 ifsta->extra_ie_len = len;
3113 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3114 struct sk_buff *skb, u8 *bssid,
3117 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3118 struct sta_info *sta;
3119 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3120 DECLARE_MAC_BUF(mac);
3122 /* TODO: Could consider removing the least recently used entry and
3123 * allow new one to be added. */
3124 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3125 if (net_ratelimit()) {
3126 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3127 "entry %s\n", dev->name, print_mac(mac, addr));
3132 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
3133 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
3135 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3139 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3141 rate_control_rate_init(sta, local);
3143 return sta; /* caller will call sta_info_put() */
3147 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3149 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3150 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3152 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3155 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3156 sdata->type != IEEE80211_IF_TYPE_IBSS)
3159 ieee80211_send_deauth(dev, ifsta, reason);
3160 ieee80211_set_disassoc(dev, ifsta, 1);
3165 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3167 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3168 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3170 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3173 if (sdata->type != IEEE80211_IF_TYPE_STA)
3176 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
3179 ieee80211_send_disassoc(dev, ifsta, reason);
3180 ieee80211_set_disassoc(dev, ifsta, 0);