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 * 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,
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 {
94 /* length of them, respectively */
106 u8 ext_supp_rates_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)
121 memset(elems, 0, sizeof(*elems));
133 printk(KERN_DEBUG "IEEE 802.11 element parse "
134 "failed (id=%d elen=%d left=%d)\n",
143 elems->ssid_len = elen;
145 case WLAN_EID_SUPP_RATES:
146 elems->supp_rates = pos;
147 elems->supp_rates_len = elen;
149 case WLAN_EID_FH_PARAMS:
150 elems->fh_params = pos;
151 elems->fh_params_len = elen;
153 case WLAN_EID_DS_PARAMS:
154 elems->ds_params = pos;
155 elems->ds_params_len = elen;
157 case WLAN_EID_CF_PARAMS:
158 elems->cf_params = pos;
159 elems->cf_params_len = elen;
163 elems->tim_len = elen;
165 case WLAN_EID_IBSS_PARAMS:
166 elems->ibss_params = pos;
167 elems->ibss_params_len = elen;
169 case WLAN_EID_CHALLENGE:
170 elems->challenge = pos;
171 elems->challenge_len = elen;
174 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
176 /* Microsoft OUI (00:50:F2) */
178 /* OUI Type 1 - WPA IE */
180 elems->wpa_len = elen;
181 } else if (elen >= 5 && pos[3] == 2) {
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;
194 elems->rsn_len = elen;
196 case WLAN_EID_ERP_INFO:
197 elems->erp_info = pos;
198 elems->erp_info_len = elen;
200 case WLAN_EID_EXT_SUPP_RATES:
201 elems->ext_supp_rates = pos;
202 elems->ext_supp_rates_len = elen;
206 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
207 "unknown element (id=%d elen=%d)\n",
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)
237 static void ieee80211_sta_wmm_params(struct net_device *dev,
238 struct ieee80211_if_sta *ifsta,
239 u8 *wmm_param, size_t wmm_param_len)
241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
242 struct ieee80211_tx_queue_params params;
247 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
249 count = wmm_param[6] & 0x0f;
250 if (count == ifsta->wmm_last_param_set)
252 ifsta->wmm_last_param_set = count;
255 left = wmm_param_len - 8;
257 memset(¶ms, 0, sizeof(params));
259 if (!local->ops->conf_tx)
263 for (; left >= 4; left -= 4, pos += 4) {
264 int aci = (pos[0] >> 5) & 0x03;
265 int acm = (pos[0] >> 4) & 0x01;
270 queue = IEEE80211_TX_QUEUE_DATA3;
272 local->wmm_acm |= BIT(0) | BIT(3);
276 queue = IEEE80211_TX_QUEUE_DATA1;
278 local->wmm_acm |= BIT(4) | BIT(5);
282 queue = IEEE80211_TX_QUEUE_DATA0;
284 local->wmm_acm |= BIT(6) | BIT(7);
289 queue = IEEE80211_TX_QUEUE_DATA2;
291 local->wmm_acm |= BIT(1) | BIT(2);
296 params.aifs = pos[0] & 0x0f;
297 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
298 params.cw_min = ecw2cw(pos[1] & 0x0f);
299 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
300 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
301 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
302 "cWmin=%d cWmax=%d burst=%d\n",
303 dev->name, queue, aci, acm, params.aifs, params.cw_min,
304 params.cw_max, params.burst_time);
305 /* TODO: handle ACM (block TX, fallback to next lowest allowed
307 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
308 printk(KERN_DEBUG "%s: failed to set TX queue "
309 "parameters for queue %d\n", dev->name, queue);
315 static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
317 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
318 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
319 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
320 int preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
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 MAC_ARG(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"
341 " (BSSID=" MAC_FMT ")\n",
343 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
345 MAC_ARG(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 union iwreq_data wrqu;
416 if (!!(ifsta->flags & IEEE80211_STA_ASSOCIATED) == assoc)
420 struct ieee80211_sub_if_data *sdata;
421 struct ieee80211_sta_bss *bss;
423 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
425 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
426 if (sdata->type != IEEE80211_IF_TYPE_STA)
429 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
431 if (bss->has_erp_value)
432 ieee80211_handle_erp_ie(dev, bss->erp_value);
433 ieee80211_rx_bss_put(dev, bss);
436 netif_carrier_on(dev);
437 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
438 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
439 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
440 ieee80211_sta_send_associnfo(dev, ifsta);
442 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
444 netif_carrier_off(dev);
445 ieee80211_reset_erp_info(dev);
446 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
448 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
449 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
450 ifsta->last_probe = jiffies;
453 static void ieee80211_set_disassoc(struct net_device *dev,
454 struct ieee80211_if_sta *ifsta, int deauth)
457 ifsta->auth_tries = 0;
458 ifsta->assoc_tries = 0;
459 ieee80211_set_associated(dev, ifsta, 0);
462 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
465 struct ieee80211_sub_if_data *sdata;
466 struct ieee80211_tx_packet_data *pkt_data;
468 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
469 skb->dev = sdata->local->mdev;
470 skb_set_mac_header(skb, 0);
471 skb_set_network_header(skb, 0);
472 skb_set_transport_header(skb, 0);
474 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
475 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
476 pkt_data->ifindex = sdata->dev->ifindex;
477 if (sdata->type == IEEE80211_IF_TYPE_MGMT)
478 pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
480 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
486 static void ieee80211_send_auth(struct net_device *dev,
487 struct ieee80211_if_sta *ifsta,
488 int transaction, u8 *extra, size_t extra_len,
491 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
493 struct ieee80211_mgmt *mgmt;
495 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
496 sizeof(*mgmt) + 6 + extra_len);
498 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
499 "frame\n", dev->name);
502 skb_reserve(skb, local->hw.extra_tx_headroom);
504 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
505 memset(mgmt, 0, 24 + 6);
506 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
507 IEEE80211_STYPE_AUTH);
509 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
510 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
511 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
512 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
513 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
514 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
515 ifsta->auth_transaction = transaction + 1;
516 mgmt->u.auth.status_code = cpu_to_le16(0);
518 memcpy(skb_put(skb, extra_len), extra, extra_len);
520 ieee80211_sta_tx(dev, skb, encrypt);
524 static void ieee80211_authenticate(struct net_device *dev,
525 struct ieee80211_if_sta *ifsta)
528 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
529 printk(KERN_DEBUG "%s: authentication with AP " MAC_FMT
531 dev->name, MAC_ARG(ifsta->bssid));
532 ifsta->state = IEEE80211_DISABLED;
536 ifsta->state = IEEE80211_AUTHENTICATE;
537 printk(KERN_DEBUG "%s: authenticate with AP " MAC_FMT "\n",
538 dev->name, MAC_ARG(ifsta->bssid));
540 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
542 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
546 static void ieee80211_send_assoc(struct net_device *dev,
547 struct ieee80211_if_sta *ifsta)
549 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
550 struct ieee80211_hw_mode *mode;
552 struct ieee80211_mgmt *mgmt;
556 struct ieee80211_sta_bss *bss;
559 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
560 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
563 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
564 "frame\n", dev->name);
567 skb_reserve(skb, local->hw.extra_tx_headroom);
569 mode = local->oper_hw_mode;
570 capab = ifsta->capab;
571 if (mode->mode == MODE_IEEE80211G) {
572 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
573 WLAN_CAPABILITY_SHORT_PREAMBLE;
575 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
577 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
578 capab |= WLAN_CAPABILITY_PRIVACY;
582 ieee80211_rx_bss_put(dev, bss);
585 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
587 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
588 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
589 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
591 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
593 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
594 IEEE80211_STYPE_REASSOC_REQ);
595 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
596 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
597 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
601 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
602 IEEE80211_STYPE_ASSOC_REQ);
603 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
604 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
608 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
609 *pos++ = WLAN_EID_SSID;
610 *pos++ = ifsta->ssid_len;
611 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
613 len = mode->num_rates;
616 pos = skb_put(skb, len + 2);
617 *pos++ = WLAN_EID_SUPP_RATES;
619 for (i = 0; i < len; i++) {
620 int rate = mode->rates[i].rate;
621 if (mode->mode == MODE_ATHEROS_TURBO)
623 *pos++ = (u8) (rate / 5);
626 if (mode->num_rates > len) {
627 pos = skb_put(skb, mode->num_rates - len + 2);
628 *pos++ = WLAN_EID_EXT_SUPP_RATES;
629 *pos++ = mode->num_rates - len;
630 for (i = len; i < mode->num_rates; i++) {
631 int rate = mode->rates[i].rate;
632 if (mode->mode == MODE_ATHEROS_TURBO)
634 *pos++ = (u8) (rate / 5);
638 if (ifsta->extra_ie) {
639 pos = skb_put(skb, ifsta->extra_ie_len);
640 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
643 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
644 pos = skb_put(skb, 9);
645 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
646 *pos++ = 7; /* len */
647 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
650 *pos++ = 2; /* WME */
651 *pos++ = 0; /* WME info */
652 *pos++ = 1; /* WME ver */
656 kfree(ifsta->assocreq_ies);
657 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
658 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
659 if (ifsta->assocreq_ies)
660 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
662 ieee80211_sta_tx(dev, skb, 0);
666 static void ieee80211_send_deauth(struct net_device *dev,
667 struct ieee80211_if_sta *ifsta, u16 reason)
669 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
671 struct ieee80211_mgmt *mgmt;
673 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
675 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
676 "frame\n", dev->name);
679 skb_reserve(skb, local->hw.extra_tx_headroom);
681 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 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_DEAUTH);
689 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
691 ieee80211_sta_tx(dev, skb, 0);
695 static void ieee80211_send_disassoc(struct net_device *dev,
696 struct ieee80211_if_sta *ifsta, u16 reason)
698 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
700 struct ieee80211_mgmt *mgmt;
702 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
704 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
705 "frame\n", dev->name);
708 skb_reserve(skb, local->hw.extra_tx_headroom);
710 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
712 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
713 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
714 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
715 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
716 IEEE80211_STYPE_DISASSOC);
718 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
720 ieee80211_sta_tx(dev, skb, 0);
724 static int ieee80211_privacy_mismatch(struct net_device *dev,
725 struct ieee80211_if_sta *ifsta)
727 struct ieee80211_sta_bss *bss;
730 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL) ||
731 ifsta->key_management_enabled)
734 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
738 if (ieee80211_sta_wep_configured(dev) !=
739 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
742 ieee80211_rx_bss_put(dev, bss);
748 static void ieee80211_associate(struct net_device *dev,
749 struct ieee80211_if_sta *ifsta)
751 ifsta->assoc_tries++;
752 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
753 printk(KERN_DEBUG "%s: association with AP " MAC_FMT
755 dev->name, MAC_ARG(ifsta->bssid));
756 ifsta->state = IEEE80211_DISABLED;
760 ifsta->state = IEEE80211_ASSOCIATE;
761 printk(KERN_DEBUG "%s: associate with AP " MAC_FMT "\n",
762 dev->name, MAC_ARG(ifsta->bssid));
763 if (ieee80211_privacy_mismatch(dev, ifsta)) {
764 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
765 "mixed-cell disabled - abort association\n", dev->name);
766 ifsta->state = IEEE80211_DISABLED;
770 ieee80211_send_assoc(dev, ifsta);
772 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
776 static void ieee80211_associated(struct net_device *dev,
777 struct ieee80211_if_sta *ifsta)
779 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
780 struct sta_info *sta;
783 /* TODO: start monitoring current AP signal quality and number of
784 * missed beacons. Scan other channels every now and then and search
786 /* TODO: remove expired BSSes */
788 ifsta->state = IEEE80211_ASSOCIATED;
790 sta = sta_info_get(local, ifsta->bssid);
792 printk(KERN_DEBUG "%s: No STA entry for own AP " MAC_FMT "\n",
793 dev->name, MAC_ARG(ifsta->bssid));
797 if (time_after(jiffies,
798 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
799 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
800 printk(KERN_DEBUG "%s: No ProbeResp from "
801 "current AP " MAC_FMT " - assume out of "
803 dev->name, MAC_ARG(ifsta->bssid));
807 ieee80211_send_probe_req(dev, ifsta->bssid,
809 local->scan_ssid_len);
810 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
812 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
813 if (time_after(jiffies, ifsta->last_probe +
814 IEEE80211_PROBE_INTERVAL)) {
815 ifsta->last_probe = jiffies;
816 ieee80211_send_probe_req(dev, ifsta->bssid,
824 union iwreq_data wrqu;
825 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
826 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
827 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
828 mod_timer(&ifsta->timer, jiffies +
829 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
831 mod_timer(&ifsta->timer, jiffies +
832 IEEE80211_MONITORING_INTERVAL);
837 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
838 u8 *ssid, size_t ssid_len)
840 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
841 struct ieee80211_hw_mode *mode;
843 struct ieee80211_mgmt *mgmt;
844 u8 *pos, *supp_rates, *esupp_rates = NULL;
847 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
849 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
850 "request\n", dev->name);
853 skb_reserve(skb, local->hw.extra_tx_headroom);
855 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
857 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
858 IEEE80211_STYPE_PROBE_REQ);
859 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
861 memcpy(mgmt->da, dst, ETH_ALEN);
862 memcpy(mgmt->bssid, dst, ETH_ALEN);
864 memset(mgmt->da, 0xff, ETH_ALEN);
865 memset(mgmt->bssid, 0xff, ETH_ALEN);
867 pos = skb_put(skb, 2 + ssid_len);
868 *pos++ = WLAN_EID_SSID;
870 memcpy(pos, ssid, ssid_len);
872 supp_rates = skb_put(skb, 2);
873 supp_rates[0] = WLAN_EID_SUPP_RATES;
875 mode = local->oper_hw_mode;
876 for (i = 0; i < mode->num_rates; i++) {
877 struct ieee80211_rate *rate = &mode->rates[i];
878 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
881 pos = skb_put(skb, 1);
883 } else if (supp_rates[1] == 8) {
884 esupp_rates = skb_put(skb, 3);
885 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
887 pos = &esupp_rates[2];
889 pos = skb_put(skb, 1);
892 if (mode->mode == MODE_ATHEROS_TURBO)
893 *pos = rate->rate / 10;
895 *pos = rate->rate / 5;
898 ieee80211_sta_tx(dev, skb, 0);
902 static int ieee80211_sta_wep_configured(struct net_device *dev)
904 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
905 if (!sdata || !sdata->default_key ||
906 sdata->default_key->conf.alg != ALG_WEP)
912 static void ieee80211_auth_completed(struct net_device *dev,
913 struct ieee80211_if_sta *ifsta)
915 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
916 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
917 ieee80211_associate(dev, ifsta);
921 static void ieee80211_auth_challenge(struct net_device *dev,
922 struct ieee80211_if_sta *ifsta,
923 struct ieee80211_mgmt *mgmt,
927 struct ieee802_11_elems elems;
929 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
930 pos = mgmt->u.auth.variable;
931 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
933 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
937 if (!elems.challenge) {
938 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
939 "frame\n", dev->name);
942 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
943 elems.challenge_len + 2, 1);
947 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
948 struct ieee80211_if_sta *ifsta,
949 struct ieee80211_mgmt *mgmt,
952 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
953 u16 auth_alg, auth_transaction, status_code;
955 if (ifsta->state != IEEE80211_AUTHENTICATE &&
956 sdata->type != IEEE80211_IF_TYPE_IBSS) {
957 printk(KERN_DEBUG "%s: authentication frame received from "
958 MAC_FMT ", but not in authenticate state - ignored\n",
959 dev->name, MAC_ARG(mgmt->sa));
964 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
965 "received from " MAC_FMT " - ignored\n",
966 dev->name, len, MAC_ARG(mgmt->sa));
970 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
971 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
972 printk(KERN_DEBUG "%s: authentication frame received from "
973 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
974 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
975 MAC_ARG(mgmt->bssid));
979 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
980 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
981 printk(KERN_DEBUG "%s: authentication frame received from "
982 "unknown BSSID (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
983 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
984 MAC_ARG(mgmt->bssid));
988 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
989 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
990 status_code = le16_to_cpu(mgmt->u.auth.status_code);
992 printk(KERN_DEBUG "%s: RX authentication from " MAC_FMT " (alg=%d "
993 "transaction=%d status=%d)\n",
994 dev->name, MAC_ARG(mgmt->sa), auth_alg,
995 auth_transaction, status_code);
997 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
998 /* IEEE 802.11 standard does not require authentication in IBSS
999 * networks and most implementations do not seem to use it.
1000 * However, try to reply to authentication attempts if someone
1001 * has actually implemented this.
1002 * TODO: Could implement shared key authentication. */
1003 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1004 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1005 "frame (alg=%d transaction=%d)\n",
1006 dev->name, auth_alg, auth_transaction);
1009 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1012 if (auth_alg != ifsta->auth_alg ||
1013 auth_transaction != ifsta->auth_transaction) {
1014 printk(KERN_DEBUG "%s: unexpected authentication frame "
1015 "(alg=%d transaction=%d)\n",
1016 dev->name, auth_alg, auth_transaction);
1020 if (status_code != WLAN_STATUS_SUCCESS) {
1021 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1022 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1023 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1025 const int num_algs = ARRAY_SIZE(algs);
1027 algs[0] = algs[1] = algs[2] = 0xff;
1028 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1029 algs[0] = WLAN_AUTH_OPEN;
1030 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1031 algs[1] = WLAN_AUTH_SHARED_KEY;
1032 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1033 algs[2] = WLAN_AUTH_LEAP;
1034 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1036 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1040 for (i = 0; i < num_algs; i++) {
1042 if (pos >= num_algs)
1044 if (algs[pos] == ifsta->auth_alg ||
1047 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1048 !ieee80211_sta_wep_configured(dev))
1050 ifsta->auth_alg = algs[pos];
1051 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1053 dev->name, ifsta->auth_alg);
1060 switch (ifsta->auth_alg) {
1061 case WLAN_AUTH_OPEN:
1062 case WLAN_AUTH_LEAP:
1063 ieee80211_auth_completed(dev, ifsta);
1065 case WLAN_AUTH_SHARED_KEY:
1066 if (ifsta->auth_transaction == 4)
1067 ieee80211_auth_completed(dev, ifsta);
1069 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1075 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1076 struct ieee80211_if_sta *ifsta,
1077 struct ieee80211_mgmt *mgmt,
1083 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1084 "received from " MAC_FMT " - ignored\n",
1085 dev->name, len, MAC_ARG(mgmt->sa));
1089 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1090 printk(KERN_DEBUG "%s: deauthentication frame received from "
1091 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1092 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1093 MAC_ARG(mgmt->bssid));
1097 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1099 printk(KERN_DEBUG "%s: RX deauthentication from " MAC_FMT
1101 dev->name, MAC_ARG(mgmt->sa), reason_code);
1103 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1104 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1107 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1108 ifsta->state == IEEE80211_ASSOCIATE ||
1109 ifsta->state == IEEE80211_ASSOCIATED) {
1110 ifsta->state = IEEE80211_AUTHENTICATE;
1111 mod_timer(&ifsta->timer, jiffies +
1112 IEEE80211_RETRY_AUTH_INTERVAL);
1115 ieee80211_set_disassoc(dev, ifsta, 1);
1116 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1120 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1121 struct ieee80211_if_sta *ifsta,
1122 struct ieee80211_mgmt *mgmt,
1128 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1129 "received from " MAC_FMT " - ignored\n",
1130 dev->name, len, MAC_ARG(mgmt->sa));
1134 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1135 printk(KERN_DEBUG "%s: disassociation frame received from "
1136 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1137 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1138 MAC_ARG(mgmt->bssid));
1142 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1144 printk(KERN_DEBUG "%s: RX disassociation from " MAC_FMT
1146 dev->name, MAC_ARG(mgmt->sa), reason_code);
1148 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1149 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1151 if (ifsta->state == IEEE80211_ASSOCIATED) {
1152 ifsta->state = IEEE80211_ASSOCIATE;
1153 mod_timer(&ifsta->timer, jiffies +
1154 IEEE80211_RETRY_AUTH_INTERVAL);
1157 ieee80211_set_disassoc(dev, ifsta, 0);
1161 static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1162 struct ieee80211_if_sta *ifsta,
1163 struct ieee80211_mgmt *mgmt,
1167 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1168 struct ieee80211_hw_mode *mode;
1169 struct sta_info *sta;
1171 u16 capab_info, status_code, aid;
1172 struct ieee802_11_elems elems;
1176 /* AssocResp and ReassocResp have identical structure, so process both
1177 * of them in this function. */
1179 if (ifsta->state != IEEE80211_ASSOCIATE) {
1180 printk(KERN_DEBUG "%s: association frame received from "
1181 MAC_FMT ", but not in associate state - ignored\n",
1182 dev->name, MAC_ARG(mgmt->sa));
1187 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1188 "received from " MAC_FMT " - ignored\n",
1189 dev->name, len, MAC_ARG(mgmt->sa));
1193 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1194 printk(KERN_DEBUG "%s: association frame received from "
1195 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1196 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1197 MAC_ARG(mgmt->bssid));
1201 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1202 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1203 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1204 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1205 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1206 "set\n", dev->name, aid);
1207 aid &= ~(BIT(15) | BIT(14));
1209 printk(KERN_DEBUG "%s: RX %sssocResp from " MAC_FMT " (capab=0x%x "
1210 "status=%d aid=%d)\n",
1211 dev->name, reassoc ? "Rea" : "A", MAC_ARG(mgmt->sa),
1212 capab_info, status_code, aid);
1214 if (status_code != WLAN_STATUS_SUCCESS) {
1215 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1216 dev->name, status_code);
1217 /* if this was a reassociation, ensure we try a "full"
1218 * association next time. This works around some broken APs
1219 * which do not correctly reject reassociation requests. */
1220 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1224 pos = mgmt->u.assoc_resp.variable;
1225 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1227 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1232 if (!elems.supp_rates) {
1233 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1238 /* it probably doesn't, but if the frame includes an ERP value then
1239 * update our stored copy */
1240 if (elems.erp_info && elems.erp_info_len >= 1) {
1241 struct ieee80211_sta_bss *bss
1242 = ieee80211_rx_bss_get(dev, ifsta->bssid);
1244 bss->erp_value = elems.erp_info[0];
1245 bss->has_erp_value = 1;
1246 ieee80211_rx_bss_put(dev, bss);
1250 printk(KERN_DEBUG "%s: associated\n", dev->name);
1252 ifsta->ap_capab = capab_info;
1254 kfree(ifsta->assocresp_ies);
1255 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1256 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1257 if (ifsta->assocresp_ies)
1258 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1260 ieee80211_set_associated(dev, ifsta, 1);
1262 /* Add STA entry for the AP */
1263 sta = sta_info_get(local, ifsta->bssid);
1265 struct ieee80211_sta_bss *bss;
1266 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
1268 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1269 " AP\n", dev->name);
1272 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1274 sta->last_rssi = bss->rssi;
1275 sta->last_signal = bss->signal;
1276 sta->last_noise = bss->noise;
1277 ieee80211_rx_bss_put(dev, bss);
1282 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
1286 mode = local->oper_hw_mode;
1287 for (i = 0; i < elems.supp_rates_len; i++) {
1288 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1289 if (mode->mode == MODE_ATHEROS_TURBO)
1291 for (j = 0; j < mode->num_rates; j++)
1292 if (mode->rates[j].rate == rate)
1295 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1296 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1297 if (mode->mode == MODE_ATHEROS_TURBO)
1299 for (j = 0; j < mode->num_rates; j++)
1300 if (mode->rates[j].rate == rate)
1303 sta->supp_rates = rates;
1305 rate_control_rate_init(sta, local);
1307 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1308 sta->flags |= WLAN_STA_WME;
1309 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1310 elems.wmm_param_len);
1316 ieee80211_associated(dev, ifsta);
1320 /* Caller must hold local->sta_bss_lock */
1321 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1322 struct ieee80211_sta_bss *bss)
1324 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1325 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1326 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1330 /* Caller must hold local->sta_bss_lock */
1331 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1332 struct ieee80211_sta_bss *bss)
1334 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1335 struct ieee80211_sta_bss *b, *prev = NULL;
1336 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1340 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1343 prev->hnext = bss->hnext;
1352 static struct ieee80211_sta_bss *
1353 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1355 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1356 struct ieee80211_sta_bss *bss;
1358 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1361 atomic_inc(&bss->users);
1362 atomic_inc(&bss->users);
1363 memcpy(bss->bssid, bssid, ETH_ALEN);
1365 spin_lock_bh(&local->sta_bss_lock);
1366 /* TODO: order by RSSI? */
1367 list_add_tail(&bss->list, &local->sta_bss_list);
1368 __ieee80211_rx_bss_hash_add(dev, bss);
1369 spin_unlock_bh(&local->sta_bss_lock);
1374 static struct ieee80211_sta_bss *
1375 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1377 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1378 struct ieee80211_sta_bss *bss;
1380 spin_lock_bh(&local->sta_bss_lock);
1381 bss = local->sta_bss_hash[STA_HASH(bssid)];
1383 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1384 atomic_inc(&bss->users);
1389 spin_unlock_bh(&local->sta_bss_lock);
1394 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1403 static void ieee80211_rx_bss_put(struct net_device *dev,
1404 struct ieee80211_sta_bss *bss)
1406 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1407 if (!atomic_dec_and_test(&bss->users))
1410 spin_lock_bh(&local->sta_bss_lock);
1411 __ieee80211_rx_bss_hash_del(dev, bss);
1412 list_del(&bss->list);
1413 spin_unlock_bh(&local->sta_bss_lock);
1414 ieee80211_rx_bss_free(bss);
1418 void ieee80211_rx_bss_list_init(struct net_device *dev)
1420 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1421 spin_lock_init(&local->sta_bss_lock);
1422 INIT_LIST_HEAD(&local->sta_bss_list);
1426 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1428 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1429 struct ieee80211_sta_bss *bss, *tmp;
1431 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1432 ieee80211_rx_bss_put(dev, bss);
1436 static void ieee80211_rx_bss_info(struct net_device *dev,
1437 struct ieee80211_mgmt *mgmt,
1439 struct ieee80211_rx_status *rx_status,
1442 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1443 struct ieee802_11_elems elems;
1445 int channel, invalid = 0, clen;
1446 struct ieee80211_sta_bss *bss;
1447 struct sta_info *sta;
1448 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1451 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1452 return; /* ignore ProbeResp to foreign address */
1455 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1456 dev->name, beacon ? "Beacon" : "Probe Response",
1457 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1460 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1464 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1466 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1467 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1468 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1469 static unsigned long last_tsf_debug = 0;
1471 if (local->ops->get_tsf)
1472 tsf = local->ops->get_tsf(local_to_hw(local));
1475 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1476 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1477 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1479 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1480 (unsigned long long)tsf,
1481 (unsigned long long)timestamp,
1482 (unsigned long long)(tsf - timestamp),
1484 last_tsf_debug = jiffies;
1486 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1489 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1490 &elems) == ParseFailed)
1493 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1494 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1495 (sta = sta_info_get(local, mgmt->sa))) {
1496 struct ieee80211_hw_mode *mode;
1497 struct ieee80211_rate *rates;
1499 u32 supp_rates, prev_rates;
1502 mode = local->sta_scanning ?
1503 local->scan_hw_mode : local->oper_hw_mode;
1504 rates = mode->rates;
1505 num_rates = mode->num_rates;
1508 for (i = 0; i < elems.supp_rates_len +
1509 elems.ext_supp_rates_len; i++) {
1512 if (i < elems.supp_rates_len)
1513 rate = elems.supp_rates[i];
1514 else if (elems.ext_supp_rates)
1515 rate = elems.ext_supp_rates
1516 [i - elems.supp_rates_len];
1517 own_rate = 5 * (rate & 0x7f);
1518 if (mode->mode == MODE_ATHEROS_TURBO)
1520 for (j = 0; j < num_rates; j++)
1521 if (rates[j].rate == own_rate)
1522 supp_rates |= BIT(j);
1525 prev_rates = sta->supp_rates;
1526 sta->supp_rates &= supp_rates;
1527 if (sta->supp_rates == 0) {
1528 /* No matching rates - this should not really happen.
1529 * Make sure that at least one rate is marked
1530 * supported to avoid issues with TX rate ctrl. */
1531 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1533 if (sta->supp_rates != prev_rates) {
1534 printk(KERN_DEBUG "%s: updated supp_rates set for "
1535 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1537 dev->name, MAC_ARG(sta->addr), prev_rates,
1538 supp_rates, sta->supp_rates);
1546 if (elems.ds_params && elems.ds_params_len == 1)
1547 channel = elems.ds_params[0];
1549 channel = rx_status->channel;
1551 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1553 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1558 /* TODO: order by RSSI? */
1559 spin_lock_bh(&local->sta_bss_lock);
1560 list_move_tail(&bss->list, &local->sta_bss_list);
1561 spin_unlock_bh(&local->sta_bss_lock);
1565 if (bss->probe_resp && beacon) {
1566 /* Do not allow beacon to override data from Probe Response. */
1567 ieee80211_rx_bss_put(dev, bss);
1571 /* save the ERP value so that it is available at association time */
1572 if (elems.erp_info && elems.erp_info_len >= 1) {
1573 bss->erp_value = elems.erp_info[0];
1574 bss->has_erp_value = 1;
1577 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1578 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1579 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1580 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1581 bss->ssid_len = elems.ssid_len;
1584 bss->supp_rates_len = 0;
1585 if (elems.supp_rates) {
1586 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1587 if (clen > elems.supp_rates_len)
1588 clen = elems.supp_rates_len;
1589 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1591 bss->supp_rates_len += clen;
1593 if (elems.ext_supp_rates) {
1594 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1595 if (clen > elems.ext_supp_rates_len)
1596 clen = elems.ext_supp_rates_len;
1597 memcpy(&bss->supp_rates[bss->supp_rates_len],
1598 elems.ext_supp_rates, clen);
1599 bss->supp_rates_len += clen;
1603 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1604 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1606 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1608 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1609 bss->wpa_ie_len = elems.wpa_len + 2;
1611 bss->wpa_ie_len = 0;
1612 } else if (!elems.wpa && bss->wpa_ie) {
1615 bss->wpa_ie_len = 0;
1619 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1620 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1622 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1624 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1625 bss->rsn_ie_len = elems.rsn_len + 2;
1627 bss->rsn_ie_len = 0;
1628 } else if (!elems.rsn && bss->rsn_ie) {
1631 bss->rsn_ie_len = 0;
1634 if (elems.wmm_param &&
1635 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1636 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1638 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1640 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1641 elems.wmm_param_len + 2);
1642 bss->wmm_ie_len = elems.wmm_param_len + 2;
1644 bss->wmm_ie_len = 0;
1645 } else if (!elems.wmm_param && bss->wmm_ie) {
1648 bss->wmm_ie_len = 0;
1652 bss->hw_mode = rx_status->phymode;
1653 bss->channel = channel;
1654 bss->freq = rx_status->freq;
1655 if (channel != rx_status->channel &&
1656 (bss->hw_mode == MODE_IEEE80211G ||
1657 bss->hw_mode == MODE_IEEE80211B) &&
1658 channel >= 1 && channel <= 14) {
1659 static const int freq_list[] = {
1660 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1661 2447, 2452, 2457, 2462, 2467, 2472, 2484
1663 /* IEEE 802.11g/b mode can receive packets from neighboring
1664 * channels, so map the channel into frequency. */
1665 bss->freq = freq_list[channel - 1];
1667 bss->timestamp = timestamp;
1668 bss->last_update = jiffies;
1669 bss->rssi = rx_status->ssi;
1670 bss->signal = rx_status->signal;
1671 bss->noise = rx_status->noise;
1674 ieee80211_rx_bss_put(dev, bss);
1678 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1679 struct ieee80211_mgmt *mgmt,
1681 struct ieee80211_rx_status *rx_status)
1683 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1687 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1688 struct ieee80211_mgmt *mgmt,
1690 struct ieee80211_rx_status *rx_status)
1692 struct ieee80211_sub_if_data *sdata;
1693 struct ieee80211_if_sta *ifsta;
1695 struct ieee802_11_elems elems;
1697 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1699 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1700 if (sdata->type != IEEE80211_IF_TYPE_STA)
1702 ifsta = &sdata->u.sta;
1704 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1705 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1708 /* Process beacon from the current BSS */
1709 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1713 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1714 &elems) == ParseFailed)
1717 if (elems.erp_info && elems.erp_info_len >= 1)
1718 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
1720 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1721 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1722 elems.wmm_param_len);
1727 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1728 struct ieee80211_if_sta *ifsta,
1729 struct ieee80211_mgmt *mgmt,
1731 struct ieee80211_rx_status *rx_status)
1733 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1734 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1736 struct sk_buff *skb;
1737 struct ieee80211_mgmt *resp;
1740 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1741 ifsta->state != IEEE80211_IBSS_JOINED ||
1742 len < 24 + 2 || !ifsta->probe_resp)
1745 if (local->ops->tx_last_beacon)
1746 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1750 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1751 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1752 MAC_FMT " (tx_last_beacon=%d)\n",
1753 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1754 MAC_ARG(mgmt->bssid), tx_last_beacon);
1755 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1757 if (!tx_last_beacon)
1760 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1761 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1764 end = ((u8 *) mgmt) + len;
1765 pos = mgmt->u.probe_req.variable;
1766 if (pos[0] != WLAN_EID_SSID ||
1767 pos + 2 + pos[1] > end) {
1768 if (net_ratelimit()) {
1769 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1770 "from " MAC_FMT "\n",
1771 dev->name, MAC_ARG(mgmt->sa));
1776 (pos[1] != ifsta->ssid_len ||
1777 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1778 /* Ignore ProbeReq for foreign SSID */
1782 /* Reply with ProbeResp */
1783 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1787 resp = (struct ieee80211_mgmt *) skb->data;
1788 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1789 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1790 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1791 dev->name, MAC_ARG(resp->da));
1792 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1793 ieee80211_sta_tx(dev, skb, 0);
1797 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1798 struct ieee80211_rx_status *rx_status)
1800 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1801 struct ieee80211_sub_if_data *sdata;
1802 struct ieee80211_if_sta *ifsta;
1803 struct ieee80211_mgmt *mgmt;
1809 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1810 ifsta = &sdata->u.sta;
1812 mgmt = (struct ieee80211_mgmt *) skb->data;
1813 fc = le16_to_cpu(mgmt->frame_control);
1815 switch (fc & IEEE80211_FCTL_STYPE) {
1816 case IEEE80211_STYPE_PROBE_REQ:
1817 case IEEE80211_STYPE_PROBE_RESP:
1818 case IEEE80211_STYPE_BEACON:
1819 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1820 case IEEE80211_STYPE_AUTH:
1821 case IEEE80211_STYPE_ASSOC_RESP:
1822 case IEEE80211_STYPE_REASSOC_RESP:
1823 case IEEE80211_STYPE_DEAUTH:
1824 case IEEE80211_STYPE_DISASSOC:
1825 skb_queue_tail(&ifsta->skb_queue, skb);
1826 queue_work(local->hw.workqueue, &ifsta->work);
1829 printk(KERN_DEBUG "%s: received unknown management frame - "
1830 "stype=%d\n", dev->name,
1831 (fc & IEEE80211_FCTL_STYPE) >> 4);
1840 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1841 struct sk_buff *skb)
1843 struct ieee80211_rx_status *rx_status;
1844 struct ieee80211_sub_if_data *sdata;
1845 struct ieee80211_if_sta *ifsta;
1846 struct ieee80211_mgmt *mgmt;
1849 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1850 ifsta = &sdata->u.sta;
1852 rx_status = (struct ieee80211_rx_status *) skb->cb;
1853 mgmt = (struct ieee80211_mgmt *) skb->data;
1854 fc = le16_to_cpu(mgmt->frame_control);
1856 switch (fc & IEEE80211_FCTL_STYPE) {
1857 case IEEE80211_STYPE_PROBE_REQ:
1858 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1861 case IEEE80211_STYPE_PROBE_RESP:
1862 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1864 case IEEE80211_STYPE_BEACON:
1865 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1867 case IEEE80211_STYPE_AUTH:
1868 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1870 case IEEE80211_STYPE_ASSOC_RESP:
1871 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1873 case IEEE80211_STYPE_REASSOC_RESP:
1874 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1876 case IEEE80211_STYPE_DEAUTH:
1877 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1879 case IEEE80211_STYPE_DISASSOC:
1880 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1888 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1889 struct ieee80211_rx_status *rx_status)
1891 struct ieee80211_mgmt *mgmt;
1894 if (skb->len < 24) {
1899 mgmt = (struct ieee80211_mgmt *) skb->data;
1900 fc = le16_to_cpu(mgmt->frame_control);
1902 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1903 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1904 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1905 skb->len, rx_status);
1906 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1907 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1916 static int ieee80211_sta_active_ibss(struct net_device *dev)
1918 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1920 struct sta_info *sta;
1922 read_lock_bh(&local->sta_lock);
1923 list_for_each_entry(sta, &local->sta_list, list) {
1924 if (sta->dev == dev &&
1925 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1931 read_unlock_bh(&local->sta_lock);
1937 static void ieee80211_sta_expire(struct net_device *dev)
1939 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1940 struct sta_info *sta, *tmp;
1941 LIST_HEAD(tmp_list);
1943 write_lock_bh(&local->sta_lock);
1944 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1945 if (time_after(jiffies, sta->last_rx +
1946 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1947 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1948 "\n", dev->name, MAC_ARG(sta->addr));
1949 __sta_info_get(sta);
1950 sta_info_remove(sta);
1951 list_add(&sta->list, &tmp_list);
1953 write_unlock_bh(&local->sta_lock);
1955 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
1962 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1963 struct ieee80211_if_sta *ifsta)
1965 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1967 ieee80211_sta_expire(dev);
1968 if (ieee80211_sta_active_ibss(dev))
1971 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1972 "IBSS networks with same SSID (merge)\n", dev->name);
1973 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1977 void ieee80211_sta_timer(unsigned long data)
1979 struct ieee80211_sub_if_data *sdata =
1980 (struct ieee80211_sub_if_data *) data;
1981 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1982 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1984 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1985 queue_work(local->hw.workqueue, &ifsta->work);
1989 void ieee80211_sta_work(struct work_struct *work)
1991 struct ieee80211_sub_if_data *sdata =
1992 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1993 struct net_device *dev = sdata->dev;
1994 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1995 struct ieee80211_if_sta *ifsta;
1996 struct sk_buff *skb;
1998 if (!netif_running(dev))
2001 if (local->sta_scanning)
2004 if (sdata->type != IEEE80211_IF_TYPE_STA &&
2005 sdata->type != IEEE80211_IF_TYPE_IBSS) {
2006 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2007 "(type=%d)\n", dev->name, sdata->type);
2010 ifsta = &sdata->u.sta;
2012 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2013 ieee80211_sta_rx_queued_mgmt(dev, skb);
2015 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2016 ifsta->state != IEEE80211_ASSOCIATE &&
2017 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2018 ieee80211_sta_start_scan(dev, NULL, 0);
2022 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2023 if (ieee80211_sta_config_auth(dev, ifsta))
2025 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2026 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2029 switch (ifsta->state) {
2030 case IEEE80211_DISABLED:
2032 case IEEE80211_AUTHENTICATE:
2033 ieee80211_authenticate(dev, ifsta);
2035 case IEEE80211_ASSOCIATE:
2036 ieee80211_associate(dev, ifsta);
2038 case IEEE80211_ASSOCIATED:
2039 ieee80211_associated(dev, ifsta);
2041 case IEEE80211_IBSS_SEARCH:
2042 ieee80211_sta_find_ibss(dev, ifsta);
2044 case IEEE80211_IBSS_JOINED:
2045 ieee80211_sta_merge_ibss(dev, ifsta);
2048 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2053 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2054 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2055 "mixed-cell disabled - disassociate\n", dev->name);
2057 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2058 ieee80211_set_disassoc(dev, ifsta, 0);
2063 static void ieee80211_sta_reset_auth(struct net_device *dev,
2064 struct ieee80211_if_sta *ifsta)
2066 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2068 if (local->ops->reset_tsf) {
2069 /* Reset own TSF to allow time synchronization work. */
2070 local->ops->reset_tsf(local_to_hw(local));
2073 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2076 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2077 ifsta->auth_alg = WLAN_AUTH_OPEN;
2078 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2079 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2080 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2081 ifsta->auth_alg = WLAN_AUTH_LEAP;
2083 ifsta->auth_alg = WLAN_AUTH_OPEN;
2084 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2086 ifsta->auth_transaction = -1;
2087 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2088 ifsta->auth_tries = ifsta->assoc_tries = 0;
2089 netif_carrier_off(dev);
2093 void ieee80211_sta_req_auth(struct net_device *dev,
2094 struct ieee80211_if_sta *ifsta)
2096 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2097 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2099 if (sdata->type != IEEE80211_IF_TYPE_STA)
2102 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2103 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2104 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2105 IEEE80211_STA_AUTO_SSID_SEL))) {
2106 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2107 queue_work(local->hw.workqueue, &ifsta->work);
2111 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2112 const char *ssid, int ssid_len)
2114 int tmp, hidden_ssid;
2116 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2119 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2125 if (ssid[tmp] != '\0') {
2131 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2134 if (ssid_len == 1 && ssid[0] == ' ')
2140 static int ieee80211_sta_config_auth(struct net_device *dev,
2141 struct ieee80211_if_sta *ifsta)
2143 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2144 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2145 struct ieee80211_sta_bss *bss, *selected = NULL;
2146 int top_rssi = 0, freq;
2148 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2149 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
2150 ifsta->state = IEEE80211_AUTHENTICATE;
2151 ieee80211_sta_reset_auth(dev, ifsta);
2155 spin_lock_bh(&local->sta_bss_lock);
2156 freq = local->oper_channel->freq;
2157 list_for_each_entry(bss, &local->sta_bss_list, list) {
2158 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2161 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2162 !!sdata->default_key)
2165 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2169 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2170 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2173 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2174 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2177 if (!selected || top_rssi < bss->rssi) {
2179 top_rssi = bss->rssi;
2183 atomic_inc(&selected->users);
2184 spin_unlock_bh(&local->sta_bss_lock);
2187 ieee80211_set_channel(local, -1, selected->freq);
2188 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2189 ieee80211_sta_set_ssid(dev, selected->ssid,
2190 selected->ssid_len);
2191 ieee80211_sta_set_bssid(dev, selected->bssid);
2192 ieee80211_rx_bss_put(dev, selected);
2193 ifsta->state = IEEE80211_AUTHENTICATE;
2194 ieee80211_sta_reset_auth(dev, ifsta);
2197 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2198 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2199 ieee80211_sta_start_scan(dev, NULL, 0);
2201 ieee80211_sta_start_scan(dev, ifsta->ssid,
2203 ifsta->state = IEEE80211_AUTHENTICATE;
2204 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2206 ifsta->state = IEEE80211_DISABLED;
2211 static int ieee80211_sta_join_ibss(struct net_device *dev,
2212 struct ieee80211_if_sta *ifsta,
2213 struct ieee80211_sta_bss *bss)
2215 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2216 int res, rates, i, j;
2217 struct sk_buff *skb;
2218 struct ieee80211_mgmt *mgmt;
2219 struct ieee80211_tx_control control;
2220 struct ieee80211_rate *rate;
2221 struct ieee80211_hw_mode *mode;
2222 struct rate_control_extra extra;
2224 struct ieee80211_sub_if_data *sdata;
2226 /* Remove possible STA entries from other IBSS networks. */
2227 sta_info_flush(local, NULL);
2229 if (local->ops->reset_tsf) {
2230 /* Reset own TSF to allow time synchronization work. */
2231 local->ops->reset_tsf(local_to_hw(local));
2233 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2234 res = ieee80211_if_config(dev);
2238 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2240 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2241 sdata->drop_unencrypted = bss->capability &
2242 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2244 res = ieee80211_set_channel(local, -1, bss->freq);
2246 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2247 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2248 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2249 local->hw.conf.freq);
2253 /* Set beacon template based on scan results */
2254 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2259 skb_reserve(skb, local->hw.extra_tx_headroom);
2261 mgmt = (struct ieee80211_mgmt *)
2262 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2263 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2264 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2265 IEEE80211_STYPE_BEACON);
2266 memset(mgmt->da, 0xff, ETH_ALEN);
2267 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2268 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2269 mgmt->u.beacon.beacon_int =
2270 cpu_to_le16(local->hw.conf.beacon_int);
2271 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2273 pos = skb_put(skb, 2 + ifsta->ssid_len);
2274 *pos++ = WLAN_EID_SSID;
2275 *pos++ = ifsta->ssid_len;
2276 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2278 rates = bss->supp_rates_len;
2281 pos = skb_put(skb, 2 + rates);
2282 *pos++ = WLAN_EID_SUPP_RATES;
2284 memcpy(pos, bss->supp_rates, rates);
2286 pos = skb_put(skb, 2 + 1);
2287 *pos++ = WLAN_EID_DS_PARAMS;
2289 *pos++ = bss->channel;
2291 pos = skb_put(skb, 2 + 2);
2292 *pos++ = WLAN_EID_IBSS_PARAMS;
2294 /* FIX: set ATIM window based on scan results */
2298 if (bss->supp_rates_len > 8) {
2299 rates = bss->supp_rates_len - 8;
2300 pos = skb_put(skb, 2 + rates);
2301 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2303 memcpy(pos, &bss->supp_rates[8], rates);
2306 memset(&control, 0, sizeof(control));
2307 memset(&extra, 0, sizeof(extra));
2308 extra.mode = local->oper_hw_mode;
2309 rate = rate_control_get_rate(local, dev, skb, &extra);
2311 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2312 "for IBSS beacon\n", dev->name);
2316 ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
2317 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2318 rate->val2 : rate->val;
2319 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2320 control.power_level = local->hw.conf.power_level;
2321 control.flags |= IEEE80211_TXCTL_NO_ACK;
2322 control.retry_limit = 1;
2324 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2325 if (ifsta->probe_resp) {
2326 mgmt = (struct ieee80211_mgmt *)
2327 ifsta->probe_resp->data;
2328 mgmt->frame_control =
2329 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2330 IEEE80211_STYPE_PROBE_RESP);
2332 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2333 "template for IBSS\n", dev->name);
2336 if (local->ops->beacon_update &&
2337 local->ops->beacon_update(local_to_hw(local),
2338 skb, &control) == 0) {
2339 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2340 "template based on scan results\n", dev->name);
2345 mode = local->oper_hw_mode;
2346 for (i = 0; i < bss->supp_rates_len; i++) {
2347 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2348 if (mode->mode == MODE_ATHEROS_TURBO)
2350 for (j = 0; j < mode->num_rates; j++)
2351 if (mode->rates[j].rate == bitrate)
2354 ifsta->supp_rates_bits = rates;
2358 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2359 "template\n", dev->name);
2363 ifsta->state = IEEE80211_IBSS_JOINED;
2364 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2366 ieee80211_rx_bss_put(dev, bss);
2372 static int ieee80211_sta_create_ibss(struct net_device *dev,
2373 struct ieee80211_if_sta *ifsta)
2375 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2376 struct ieee80211_sta_bss *bss;
2377 struct ieee80211_sub_if_data *sdata;
2378 struct ieee80211_hw_mode *mode;
2379 u8 bssid[ETH_ALEN], *pos;
2383 /* Easier testing, use fixed BSSID. */
2384 memset(bssid, 0xfe, ETH_ALEN);
2386 /* Generate random, not broadcast, locally administered BSSID. Mix in
2387 * own MAC address to make sure that devices that do not have proper
2388 * random number generator get different BSSID. */
2389 get_random_bytes(bssid, ETH_ALEN);
2390 for (i = 0; i < ETH_ALEN; i++)
2391 bssid[i] ^= dev->dev_addr[i];
2396 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2397 dev->name, MAC_ARG(bssid));
2399 bss = ieee80211_rx_bss_add(dev, bssid);
2403 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2404 mode = local->oper_hw_mode;
2406 if (local->hw.conf.beacon_int == 0)
2407 local->hw.conf.beacon_int = 100;
2408 bss->beacon_int = local->hw.conf.beacon_int;
2409 bss->hw_mode = local->hw.conf.phymode;
2410 bss->channel = local->hw.conf.channel;
2411 bss->freq = local->hw.conf.freq;
2412 bss->last_update = jiffies;
2413 bss->capability = WLAN_CAPABILITY_IBSS;
2414 if (sdata->default_key) {
2415 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2417 sdata->drop_unencrypted = 0;
2418 bss->supp_rates_len = mode->num_rates;
2419 pos = bss->supp_rates;
2420 for (i = 0; i < mode->num_rates; i++) {
2421 int rate = mode->rates[i].rate;
2422 if (mode->mode == MODE_ATHEROS_TURBO)
2424 *pos++ = (u8) (rate / 5);
2427 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2431 static int ieee80211_sta_find_ibss(struct net_device *dev,
2432 struct ieee80211_if_sta *ifsta)
2434 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2435 struct ieee80211_sta_bss *bss;
2440 if (ifsta->ssid_len == 0)
2443 active_ibss = ieee80211_sta_active_ibss(dev);
2444 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2445 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2446 dev->name, active_ibss);
2447 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2448 spin_lock_bh(&local->sta_bss_lock);
2449 list_for_each_entry(bss, &local->sta_bss_list, list) {
2450 if (ifsta->ssid_len != bss->ssid_len ||
2451 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2452 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2454 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2455 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2456 MAC_ARG(bss->bssid));
2457 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2458 memcpy(bssid, bss->bssid, ETH_ALEN);
2460 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2463 spin_unlock_bh(&local->sta_bss_lock);
2465 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2466 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2467 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2468 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2469 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2470 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2471 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2472 " based on configured SSID\n",
2473 dev->name, MAC_ARG(bssid));
2474 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2476 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2477 printk(KERN_DEBUG " did not try to join ibss\n");
2478 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2480 /* Selected IBSS not found in current scan results - try to scan */
2481 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2482 !ieee80211_sta_active_ibss(dev)) {
2483 mod_timer(&ifsta->timer, jiffies +
2484 IEEE80211_IBSS_MERGE_INTERVAL);
2485 } else if (time_after(jiffies, local->last_scan_completed +
2486 IEEE80211_SCAN_INTERVAL)) {
2487 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2488 "join\n", dev->name);
2489 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2491 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2492 int interval = IEEE80211_SCAN_INTERVAL;
2494 if (time_after(jiffies, ifsta->ibss_join_req +
2495 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2496 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2497 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2498 return ieee80211_sta_create_ibss(dev, ifsta);
2499 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2500 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2501 " configured channel %d (%d MHz)\n",
2502 dev->name, local->hw.conf.channel,
2503 local->hw.conf.freq);
2506 /* No IBSS found - decrease scan interval and continue
2508 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2511 ifsta->state = IEEE80211_IBSS_SEARCH;
2512 mod_timer(&ifsta->timer, jiffies + interval);
2520 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2522 struct ieee80211_sub_if_data *sdata;
2523 struct ieee80211_if_sta *ifsta;
2524 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2526 if (len > IEEE80211_MAX_SSID_LEN)
2529 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2531 if (local->ops->conf_tx) {
2532 struct ieee80211_tx_queue_params qparam;
2535 memset(&qparam, 0, sizeof(qparam));
2536 /* TODO: are these ok defaults for all hw_modes? */
2539 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2540 qparam.cw_max = 1023;
2541 qparam.burst_time = 0;
2542 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2544 local->ops->conf_tx(local_to_hw(local),
2545 i + IEEE80211_TX_QUEUE_DATA0,
2548 /* IBSS uses different parameters for Beacon sending */
2552 local->ops->conf_tx(local_to_hw(local),
2553 IEEE80211_TX_QUEUE_BEACON, &qparam);
2556 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2557 ifsta = &sdata->u.sta;
2559 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2560 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2561 memcpy(ifsta->ssid, ssid, len);
2562 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2563 ifsta->ssid_len = len;
2566 ifsta->flags |= IEEE80211_STA_SSID_SET;
2568 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2569 if (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2570 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2571 ifsta->ibss_join_req = jiffies;
2572 ifsta->state = IEEE80211_IBSS_SEARCH;
2573 return ieee80211_sta_find_ibss(dev, ifsta);
2579 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2581 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2582 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2583 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2584 *len = ifsta->ssid_len;
2589 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2591 struct ieee80211_sub_if_data *sdata;
2592 struct ieee80211_if_sta *ifsta;
2595 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2596 ifsta = &sdata->u.sta;
2598 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2599 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2600 res = ieee80211_if_config(dev);
2602 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2603 "the low-level driver\n", dev->name);
2608 if (is_valid_ether_addr(bssid))
2609 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2611 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2617 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2618 struct ieee80211_sub_if_data *sdata,
2621 struct sk_buff *skb;
2622 struct ieee80211_hdr *nullfunc;
2625 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2627 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2628 "frame\n", sdata->dev->name);
2631 skb_reserve(skb, local->hw.extra_tx_headroom);
2633 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2634 memset(nullfunc, 0, 24);
2635 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2636 IEEE80211_FCTL_TODS;
2638 fc |= IEEE80211_FCTL_PM;
2639 nullfunc->frame_control = cpu_to_le16(fc);
2640 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2641 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2642 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2644 ieee80211_sta_tx(sdata->dev, skb, 0);
2648 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2650 struct ieee80211_local *local = hw_to_local(hw);
2651 struct net_device *dev = local->scan_dev;
2652 struct ieee80211_sub_if_data *sdata;
2653 union iwreq_data wrqu;
2655 local->last_scan_completed = jiffies;
2657 local->sta_scanning = 0;
2659 if (ieee80211_hw_config(local))
2660 printk(KERN_DEBUG "%s: failed to restore operational"
2661 "channel after scan\n", dev->name);
2663 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2664 ieee80211_if_config(dev))
2665 printk(KERN_DEBUG "%s: failed to restore operational"
2666 "BSSID after scan\n", dev->name);
2668 memset(&wrqu, 0, sizeof(wrqu));
2669 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2671 read_lock(&local->sub_if_lock);
2672 list_for_each_entry(sdata, &local->sub_if_list, list) {
2674 /* No need to wake the master device. */
2675 if (sdata->dev == local->mdev)
2678 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2679 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
2680 ieee80211_send_nullfunc(local, sdata, 0);
2681 ieee80211_sta_timer((unsigned long)sdata);
2684 netif_wake_queue(sdata->dev);
2686 read_unlock(&local->sub_if_lock);
2688 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2689 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2690 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2691 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2692 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2693 !ieee80211_sta_active_ibss(dev)))
2694 ieee80211_sta_find_ibss(dev, ifsta);
2697 EXPORT_SYMBOL(ieee80211_scan_completed);
2699 void ieee80211_sta_scan_work(struct work_struct *work)
2701 struct ieee80211_local *local =
2702 container_of(work, struct ieee80211_local, scan_work.work);
2703 struct net_device *dev = local->scan_dev;
2704 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2705 struct ieee80211_hw_mode *mode;
2706 struct ieee80211_channel *chan;
2708 unsigned long next_delay = 0;
2710 if (!local->sta_scanning)
2713 switch (local->scan_state) {
2714 case SCAN_SET_CHANNEL:
2715 mode = local->scan_hw_mode;
2716 if (local->scan_hw_mode->list.next == &local->modes_list &&
2717 local->scan_channel_idx >= mode->num_channels) {
2718 ieee80211_scan_completed(local_to_hw(local));
2721 skip = !(local->enabled_modes & (1 << mode->mode));
2722 chan = &mode->channels[local->scan_channel_idx];
2723 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2724 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2725 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2726 (local->hw_modes & local->enabled_modes &
2727 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2732 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2733 dev->name, chan->chan, chan->freq);
2736 local->scan_channel = chan;
2737 if (ieee80211_hw_config(local)) {
2738 printk(KERN_DEBUG "%s: failed to set channel "
2739 "%d (%d MHz) for scan\n", dev->name,
2740 chan->chan, chan->freq);
2745 local->scan_channel_idx++;
2746 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2747 if (local->scan_hw_mode->list.next != &local->modes_list) {
2748 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2749 struct ieee80211_hw_mode,
2751 local->scan_channel_idx = 0;
2758 next_delay = IEEE80211_PROBE_DELAY +
2759 usecs_to_jiffies(local->hw.channel_change_time);
2760 local->scan_state = SCAN_SEND_PROBE;
2762 case SCAN_SEND_PROBE:
2763 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2764 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2765 local->scan_ssid_len);
2766 next_delay = IEEE80211_CHANNEL_TIME;
2768 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2769 local->scan_state = SCAN_SET_CHANNEL;
2773 if (local->sta_scanning)
2774 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2779 static int ieee80211_sta_start_scan(struct net_device *dev,
2780 u8 *ssid, size_t ssid_len)
2782 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2783 struct ieee80211_sub_if_data *sdata;
2785 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2788 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2789 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2792 * ScanType: ACTIVE, PASSIVE
2793 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2794 * a Probe frame during active scanning
2796 * MinChannelTime (>= ProbeDelay), in TU
2797 * MaxChannelTime: (>= MinChannelTime), in TU
2800 /* MLME-SCAN.confirm
2802 * ResultCode: SUCCESS, INVALID_PARAMETERS
2805 if (local->sta_scanning) {
2806 if (local->scan_dev == dev)
2811 if (local->ops->hw_scan) {
2812 int rc = local->ops->hw_scan(local_to_hw(local),
2815 local->sta_scanning = 1;
2816 local->scan_dev = dev;
2821 local->sta_scanning = 1;
2823 read_lock(&local->sub_if_lock);
2824 list_for_each_entry(sdata, &local->sub_if_list, list) {
2826 /* Don't stop the master interface, otherwise we can't transmit
2828 if (sdata->dev == local->mdev)
2831 netif_stop_queue(sdata->dev);
2832 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2833 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
2834 ieee80211_send_nullfunc(local, sdata, 1);
2836 read_unlock(&local->sub_if_lock);
2839 local->scan_ssid_len = ssid_len;
2840 memcpy(local->scan_ssid, ssid, ssid_len);
2842 local->scan_ssid_len = 0;
2843 local->scan_state = SCAN_SET_CHANNEL;
2844 local->scan_hw_mode = list_entry(local->modes_list.next,
2845 struct ieee80211_hw_mode,
2847 local->scan_channel_idx = 0;
2848 local->scan_dev = dev;
2850 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2851 ieee80211_if_config(dev))
2852 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2855 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2856 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2857 IEEE80211_CHANNEL_TIME);
2863 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2865 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2866 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2867 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2869 if (sdata->type != IEEE80211_IF_TYPE_STA)
2870 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2872 if (local->sta_scanning) {
2873 if (local->scan_dev == dev)
2878 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2879 queue_work(local->hw.workqueue, &ifsta->work);
2884 ieee80211_sta_scan_result(struct net_device *dev,
2885 struct ieee80211_sta_bss *bss,
2886 char *current_ev, char *end_buf)
2888 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2889 struct iw_event iwe;
2891 if (time_after(jiffies,
2892 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2895 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2898 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2899 !bss->wpa_ie && !bss->rsn_ie)
2902 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2903 (local->scan_ssid_len != bss->ssid_len ||
2904 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2907 memset(&iwe, 0, sizeof(iwe));
2908 iwe.cmd = SIOCGIWAP;
2909 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2910 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2911 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2914 memset(&iwe, 0, sizeof(iwe));
2915 iwe.cmd = SIOCGIWESSID;
2916 iwe.u.data.length = bss->ssid_len;
2917 iwe.u.data.flags = 1;
2918 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2921 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2922 memset(&iwe, 0, sizeof(iwe));
2923 iwe.cmd = SIOCGIWMODE;
2924 if (bss->capability & WLAN_CAPABILITY_ESS)
2925 iwe.u.mode = IW_MODE_MASTER;
2927 iwe.u.mode = IW_MODE_ADHOC;
2928 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2932 memset(&iwe, 0, sizeof(iwe));
2933 iwe.cmd = SIOCGIWFREQ;
2934 iwe.u.freq.m = bss->channel;
2936 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2938 iwe.u.freq.m = bss->freq * 100000;
2940 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2943 memset(&iwe, 0, sizeof(iwe));
2945 iwe.u.qual.qual = bss->signal;
2946 iwe.u.qual.level = bss->rssi;
2947 iwe.u.qual.noise = bss->noise;
2948 iwe.u.qual.updated = local->wstats_flags;
2949 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2952 memset(&iwe, 0, sizeof(iwe));
2953 iwe.cmd = SIOCGIWENCODE;
2954 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2955 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2957 iwe.u.data.flags = IW_ENCODE_DISABLED;
2958 iwe.u.data.length = 0;
2959 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2961 if (bss && bss->wpa_ie) {
2962 memset(&iwe, 0, sizeof(iwe));
2963 iwe.cmd = IWEVGENIE;
2964 iwe.u.data.length = bss->wpa_ie_len;
2965 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2969 if (bss && bss->rsn_ie) {
2970 memset(&iwe, 0, sizeof(iwe));
2971 iwe.cmd = IWEVGENIE;
2972 iwe.u.data.length = bss->rsn_ie_len;
2973 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2977 if (bss && bss->supp_rates_len > 0) {
2978 /* display all supported rates in readable format */
2979 char *p = current_ev + IW_EV_LCP_LEN;
2982 memset(&iwe, 0, sizeof(iwe));
2983 iwe.cmd = SIOCGIWRATE;
2984 /* Those two flags are ignored... */
2985 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2987 for (i = 0; i < bss->supp_rates_len; i++) {
2988 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2990 p = iwe_stream_add_value(current_ev, p,
2991 end_buf, &iwe, IW_EV_PARAM_LEN);
2998 buf = kmalloc(30, GFP_ATOMIC);
3000 memset(&iwe, 0, sizeof(iwe));
3001 iwe.cmd = IWEVCUSTOM;
3002 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3003 iwe.u.data.length = strlen(buf);
3004 current_ev = iwe_stream_add_point(current_ev, end_buf,
3013 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
3016 buf = kmalloc(100, GFP_ATOMIC);
3020 memset(&iwe, 0, sizeof(iwe));
3021 iwe.cmd = IWEVCUSTOM;
3022 sprintf(buf, "bcn_int=%d", bss->beacon_int);
3023 iwe.u.data.length = strlen(buf);
3024 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3027 memset(&iwe, 0, sizeof(iwe));
3028 iwe.cmd = IWEVCUSTOM;
3029 sprintf(buf, "capab=0x%04x", bss->capability);
3030 iwe.u.data.length = strlen(buf);
3031 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3042 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3044 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3045 char *current_ev = buf;
3046 char *end_buf = buf + len;
3047 struct ieee80211_sta_bss *bss;
3049 spin_lock_bh(&local->sta_bss_lock);
3050 list_for_each_entry(bss, &local->sta_bss_list, list) {
3051 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3052 spin_unlock_bh(&local->sta_bss_lock);
3055 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3058 spin_unlock_bh(&local->sta_bss_lock);
3059 return current_ev - buf;
3063 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3065 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3066 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3067 kfree(ifsta->extra_ie);
3069 ifsta->extra_ie = NULL;
3070 ifsta->extra_ie_len = 0;
3073 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3074 if (!ifsta->extra_ie) {
3075 ifsta->extra_ie_len = 0;
3078 memcpy(ifsta->extra_ie, ie, len);
3079 ifsta->extra_ie_len = len;
3084 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3085 struct sk_buff *skb, u8 *bssid,
3088 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3089 struct sta_info *sta;
3090 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3092 /* TODO: Could consider removing the least recently used entry and
3093 * allow new one to be added. */
3094 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3095 if (net_ratelimit()) {
3096 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3097 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3102 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3103 local->mdev->name, MAC_ARG(addr), dev->name);
3105 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3109 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3111 rate_control_rate_init(sta, local);
3113 return sta; /* caller will call sta_info_put() */
3117 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3119 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3120 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3122 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3125 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3126 sdata->type != IEEE80211_IF_TYPE_IBSS)
3129 ieee80211_send_deauth(dev, ifsta, reason);
3130 ieee80211_set_disassoc(dev, ifsta, 1);
3135 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3137 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3138 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3140 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3143 if (sdata->type != IEEE80211_IF_TYPE_STA)
3146 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
3149 ieee80211_send_disassoc(dev, ifsta, reason);
3150 ieee80211_set_disassoc(dev, ifsta, 0);