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 | WLAN_STA_ASSOC_AP;
1285 mode = local->oper_hw_mode;
1286 for (i = 0; i < elems.supp_rates_len; i++) {
1287 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1288 if (mode->mode == MODE_ATHEROS_TURBO)
1290 for (j = 0; j < mode->num_rates; j++)
1291 if (mode->rates[j].rate == rate)
1294 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1295 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1296 if (mode->mode == MODE_ATHEROS_TURBO)
1298 for (j = 0; j < mode->num_rates; j++)
1299 if (mode->rates[j].rate == rate)
1302 sta->supp_rates = rates;
1304 rate_control_rate_init(sta, local);
1306 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1307 sta->flags |= WLAN_STA_WME;
1308 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1309 elems.wmm_param_len);
1315 ieee80211_associated(dev, ifsta);
1319 /* Caller must hold local->sta_bss_lock */
1320 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1321 struct ieee80211_sta_bss *bss)
1323 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1324 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1325 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1329 /* Caller must hold local->sta_bss_lock */
1330 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1331 struct ieee80211_sta_bss *bss)
1333 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1334 struct ieee80211_sta_bss *b, *prev = NULL;
1335 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1339 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1342 prev->hnext = bss->hnext;
1351 static struct ieee80211_sta_bss *
1352 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1354 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1355 struct ieee80211_sta_bss *bss;
1357 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1360 atomic_inc(&bss->users);
1361 atomic_inc(&bss->users);
1362 memcpy(bss->bssid, bssid, ETH_ALEN);
1364 spin_lock_bh(&local->sta_bss_lock);
1365 /* TODO: order by RSSI? */
1366 list_add_tail(&bss->list, &local->sta_bss_list);
1367 __ieee80211_rx_bss_hash_add(dev, bss);
1368 spin_unlock_bh(&local->sta_bss_lock);
1373 static struct ieee80211_sta_bss *
1374 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1376 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1377 struct ieee80211_sta_bss *bss;
1379 spin_lock_bh(&local->sta_bss_lock);
1380 bss = local->sta_bss_hash[STA_HASH(bssid)];
1382 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1383 atomic_inc(&bss->users);
1388 spin_unlock_bh(&local->sta_bss_lock);
1393 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1402 static void ieee80211_rx_bss_put(struct net_device *dev,
1403 struct ieee80211_sta_bss *bss)
1405 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1406 if (!atomic_dec_and_test(&bss->users))
1409 spin_lock_bh(&local->sta_bss_lock);
1410 __ieee80211_rx_bss_hash_del(dev, bss);
1411 list_del(&bss->list);
1412 spin_unlock_bh(&local->sta_bss_lock);
1413 ieee80211_rx_bss_free(bss);
1417 void ieee80211_rx_bss_list_init(struct net_device *dev)
1419 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1420 spin_lock_init(&local->sta_bss_lock);
1421 INIT_LIST_HEAD(&local->sta_bss_list);
1425 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1427 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1428 struct ieee80211_sta_bss *bss, *tmp;
1430 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1431 ieee80211_rx_bss_put(dev, bss);
1435 static void ieee80211_rx_bss_info(struct net_device *dev,
1436 struct ieee80211_mgmt *mgmt,
1438 struct ieee80211_rx_status *rx_status,
1441 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1442 struct ieee802_11_elems elems;
1444 int channel, invalid = 0, clen;
1445 struct ieee80211_sta_bss *bss;
1446 struct sta_info *sta;
1447 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1450 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1451 return; /* ignore ProbeResp to foreign address */
1454 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1455 dev->name, beacon ? "Beacon" : "Probe Response",
1456 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1459 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1463 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1465 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1466 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1467 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1468 static unsigned long last_tsf_debug = 0;
1470 if (local->ops->get_tsf)
1471 tsf = local->ops->get_tsf(local_to_hw(local));
1474 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1475 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1476 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1478 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1479 (unsigned long long)tsf,
1480 (unsigned long long)timestamp,
1481 (unsigned long long)(tsf - timestamp),
1483 last_tsf_debug = jiffies;
1485 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1488 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1489 &elems) == ParseFailed)
1492 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1493 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1494 (sta = sta_info_get(local, mgmt->sa))) {
1495 struct ieee80211_hw_mode *mode;
1496 struct ieee80211_rate *rates;
1498 u32 supp_rates, prev_rates;
1501 mode = local->sta_scanning ?
1502 local->scan_hw_mode : local->oper_hw_mode;
1503 rates = mode->rates;
1504 num_rates = mode->num_rates;
1507 for (i = 0; i < elems.supp_rates_len +
1508 elems.ext_supp_rates_len; i++) {
1511 if (i < elems.supp_rates_len)
1512 rate = elems.supp_rates[i];
1513 else if (elems.ext_supp_rates)
1514 rate = elems.ext_supp_rates
1515 [i - elems.supp_rates_len];
1516 own_rate = 5 * (rate & 0x7f);
1517 if (mode->mode == MODE_ATHEROS_TURBO)
1519 for (j = 0; j < num_rates; j++)
1520 if (rates[j].rate == own_rate)
1521 supp_rates |= BIT(j);
1524 prev_rates = sta->supp_rates;
1525 sta->supp_rates &= supp_rates;
1526 if (sta->supp_rates == 0) {
1527 /* No matching rates - this should not really happen.
1528 * Make sure that at least one rate is marked
1529 * supported to avoid issues with TX rate ctrl. */
1530 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1532 if (sta->supp_rates != prev_rates) {
1533 printk(KERN_DEBUG "%s: updated supp_rates set for "
1534 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1536 dev->name, MAC_ARG(sta->addr), prev_rates,
1537 supp_rates, sta->supp_rates);
1545 if (elems.ds_params && elems.ds_params_len == 1)
1546 channel = elems.ds_params[0];
1548 channel = rx_status->channel;
1550 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1552 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1557 /* TODO: order by RSSI? */
1558 spin_lock_bh(&local->sta_bss_lock);
1559 list_move_tail(&bss->list, &local->sta_bss_list);
1560 spin_unlock_bh(&local->sta_bss_lock);
1564 if (bss->probe_resp && beacon) {
1565 /* Do not allow beacon to override data from Probe Response. */
1566 ieee80211_rx_bss_put(dev, bss);
1570 /* save the ERP value so that it is available at association time */
1571 if (elems.erp_info && elems.erp_info_len >= 1) {
1572 bss->erp_value = elems.erp_info[0];
1573 bss->has_erp_value = 1;
1576 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1577 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1578 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1579 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1580 bss->ssid_len = elems.ssid_len;
1583 bss->supp_rates_len = 0;
1584 if (elems.supp_rates) {
1585 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1586 if (clen > elems.supp_rates_len)
1587 clen = elems.supp_rates_len;
1588 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1590 bss->supp_rates_len += clen;
1592 if (elems.ext_supp_rates) {
1593 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1594 if (clen > elems.ext_supp_rates_len)
1595 clen = elems.ext_supp_rates_len;
1596 memcpy(&bss->supp_rates[bss->supp_rates_len],
1597 elems.ext_supp_rates, clen);
1598 bss->supp_rates_len += clen;
1602 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1603 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1605 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1607 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1608 bss->wpa_ie_len = elems.wpa_len + 2;
1610 bss->wpa_ie_len = 0;
1611 } else if (!elems.wpa && bss->wpa_ie) {
1614 bss->wpa_ie_len = 0;
1618 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1619 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1621 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1623 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1624 bss->rsn_ie_len = elems.rsn_len + 2;
1626 bss->rsn_ie_len = 0;
1627 } else if (!elems.rsn && bss->rsn_ie) {
1630 bss->rsn_ie_len = 0;
1633 if (elems.wmm_param &&
1634 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1635 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1637 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1639 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1640 elems.wmm_param_len + 2);
1641 bss->wmm_ie_len = elems.wmm_param_len + 2;
1643 bss->wmm_ie_len = 0;
1644 } else if (!elems.wmm_param && bss->wmm_ie) {
1647 bss->wmm_ie_len = 0;
1651 bss->hw_mode = rx_status->phymode;
1652 bss->channel = channel;
1653 bss->freq = rx_status->freq;
1654 if (channel != rx_status->channel &&
1655 (bss->hw_mode == MODE_IEEE80211G ||
1656 bss->hw_mode == MODE_IEEE80211B) &&
1657 channel >= 1 && channel <= 14) {
1658 static const int freq_list[] = {
1659 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1660 2447, 2452, 2457, 2462, 2467, 2472, 2484
1662 /* IEEE 802.11g/b mode can receive packets from neighboring
1663 * channels, so map the channel into frequency. */
1664 bss->freq = freq_list[channel - 1];
1666 bss->timestamp = timestamp;
1667 bss->last_update = jiffies;
1668 bss->rssi = rx_status->ssi;
1669 bss->signal = rx_status->signal;
1670 bss->noise = rx_status->noise;
1673 ieee80211_rx_bss_put(dev, bss);
1677 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1678 struct ieee80211_mgmt *mgmt,
1680 struct ieee80211_rx_status *rx_status)
1682 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1686 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1687 struct ieee80211_mgmt *mgmt,
1689 struct ieee80211_rx_status *rx_status)
1691 struct ieee80211_sub_if_data *sdata;
1692 struct ieee80211_if_sta *ifsta;
1694 struct ieee802_11_elems elems;
1696 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1698 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1699 if (sdata->type != IEEE80211_IF_TYPE_STA)
1701 ifsta = &sdata->u.sta;
1703 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1704 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1707 /* Process beacon from the current BSS */
1708 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1712 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1713 &elems) == ParseFailed)
1716 if (elems.erp_info && elems.erp_info_len >= 1)
1717 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
1719 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1720 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1721 elems.wmm_param_len);
1726 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1727 struct ieee80211_if_sta *ifsta,
1728 struct ieee80211_mgmt *mgmt,
1730 struct ieee80211_rx_status *rx_status)
1732 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1733 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1735 struct sk_buff *skb;
1736 struct ieee80211_mgmt *resp;
1739 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1740 ifsta->state != IEEE80211_IBSS_JOINED ||
1741 len < 24 + 2 || !ifsta->probe_resp)
1744 if (local->ops->tx_last_beacon)
1745 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1749 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1750 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1751 MAC_FMT " (tx_last_beacon=%d)\n",
1752 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1753 MAC_ARG(mgmt->bssid), tx_last_beacon);
1754 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1756 if (!tx_last_beacon)
1759 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1760 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1763 end = ((u8 *) mgmt) + len;
1764 pos = mgmt->u.probe_req.variable;
1765 if (pos[0] != WLAN_EID_SSID ||
1766 pos + 2 + pos[1] > end) {
1767 if (net_ratelimit()) {
1768 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1769 "from " MAC_FMT "\n",
1770 dev->name, MAC_ARG(mgmt->sa));
1775 (pos[1] != ifsta->ssid_len ||
1776 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1777 /* Ignore ProbeReq for foreign SSID */
1781 /* Reply with ProbeResp */
1782 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1786 resp = (struct ieee80211_mgmt *) skb->data;
1787 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1788 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1789 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1790 dev->name, MAC_ARG(resp->da));
1791 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1792 ieee80211_sta_tx(dev, skb, 0);
1796 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1797 struct ieee80211_rx_status *rx_status)
1799 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1800 struct ieee80211_sub_if_data *sdata;
1801 struct ieee80211_if_sta *ifsta;
1802 struct ieee80211_mgmt *mgmt;
1808 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1809 ifsta = &sdata->u.sta;
1811 mgmt = (struct ieee80211_mgmt *) skb->data;
1812 fc = le16_to_cpu(mgmt->frame_control);
1814 switch (fc & IEEE80211_FCTL_STYPE) {
1815 case IEEE80211_STYPE_PROBE_REQ:
1816 case IEEE80211_STYPE_PROBE_RESP:
1817 case IEEE80211_STYPE_BEACON:
1818 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1819 case IEEE80211_STYPE_AUTH:
1820 case IEEE80211_STYPE_ASSOC_RESP:
1821 case IEEE80211_STYPE_REASSOC_RESP:
1822 case IEEE80211_STYPE_DEAUTH:
1823 case IEEE80211_STYPE_DISASSOC:
1824 skb_queue_tail(&ifsta->skb_queue, skb);
1825 queue_work(local->hw.workqueue, &ifsta->work);
1828 printk(KERN_DEBUG "%s: received unknown management frame - "
1829 "stype=%d\n", dev->name,
1830 (fc & IEEE80211_FCTL_STYPE) >> 4);
1839 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1840 struct sk_buff *skb)
1842 struct ieee80211_rx_status *rx_status;
1843 struct ieee80211_sub_if_data *sdata;
1844 struct ieee80211_if_sta *ifsta;
1845 struct ieee80211_mgmt *mgmt;
1848 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1849 ifsta = &sdata->u.sta;
1851 rx_status = (struct ieee80211_rx_status *) skb->cb;
1852 mgmt = (struct ieee80211_mgmt *) skb->data;
1853 fc = le16_to_cpu(mgmt->frame_control);
1855 switch (fc & IEEE80211_FCTL_STYPE) {
1856 case IEEE80211_STYPE_PROBE_REQ:
1857 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1860 case IEEE80211_STYPE_PROBE_RESP:
1861 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1863 case IEEE80211_STYPE_BEACON:
1864 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1866 case IEEE80211_STYPE_AUTH:
1867 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1869 case IEEE80211_STYPE_ASSOC_RESP:
1870 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1872 case IEEE80211_STYPE_REASSOC_RESP:
1873 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1875 case IEEE80211_STYPE_DEAUTH:
1876 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1878 case IEEE80211_STYPE_DISASSOC:
1879 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1887 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1888 struct ieee80211_rx_status *rx_status)
1890 struct ieee80211_mgmt *mgmt;
1893 if (skb->len < 24) {
1898 mgmt = (struct ieee80211_mgmt *) skb->data;
1899 fc = le16_to_cpu(mgmt->frame_control);
1901 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1902 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1903 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1904 skb->len, rx_status);
1905 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1906 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1915 static int ieee80211_sta_active_ibss(struct net_device *dev)
1917 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1919 struct sta_info *sta;
1921 read_lock_bh(&local->sta_lock);
1922 list_for_each_entry(sta, &local->sta_list, list) {
1923 if (sta->dev == dev &&
1924 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1930 read_unlock_bh(&local->sta_lock);
1936 static void ieee80211_sta_expire(struct net_device *dev)
1938 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1939 struct sta_info *sta, *tmp;
1940 LIST_HEAD(tmp_list);
1942 write_lock_bh(&local->sta_lock);
1943 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1944 if (time_after(jiffies, sta->last_rx +
1945 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1946 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1947 "\n", dev->name, MAC_ARG(sta->addr));
1948 __sta_info_get(sta);
1949 sta_info_remove(sta);
1950 list_add(&sta->list, &tmp_list);
1952 write_unlock_bh(&local->sta_lock);
1954 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
1961 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1962 struct ieee80211_if_sta *ifsta)
1964 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1966 ieee80211_sta_expire(dev);
1967 if (ieee80211_sta_active_ibss(dev))
1970 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1971 "IBSS networks with same SSID (merge)\n", dev->name);
1972 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1976 void ieee80211_sta_timer(unsigned long data)
1978 struct ieee80211_sub_if_data *sdata =
1979 (struct ieee80211_sub_if_data *) data;
1980 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1981 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1983 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1984 queue_work(local->hw.workqueue, &ifsta->work);
1988 void ieee80211_sta_work(struct work_struct *work)
1990 struct ieee80211_sub_if_data *sdata =
1991 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1992 struct net_device *dev = sdata->dev;
1993 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1994 struct ieee80211_if_sta *ifsta;
1995 struct sk_buff *skb;
1997 if (!netif_running(dev))
2000 if (local->sta_scanning)
2003 if (sdata->type != IEEE80211_IF_TYPE_STA &&
2004 sdata->type != IEEE80211_IF_TYPE_IBSS) {
2005 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2006 "(type=%d)\n", dev->name, sdata->type);
2009 ifsta = &sdata->u.sta;
2011 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2012 ieee80211_sta_rx_queued_mgmt(dev, skb);
2014 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2015 ifsta->state != IEEE80211_ASSOCIATE &&
2016 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2017 ieee80211_sta_start_scan(dev, NULL, 0);
2021 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2022 if (ieee80211_sta_config_auth(dev, ifsta))
2024 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2025 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2028 switch (ifsta->state) {
2029 case IEEE80211_DISABLED:
2031 case IEEE80211_AUTHENTICATE:
2032 ieee80211_authenticate(dev, ifsta);
2034 case IEEE80211_ASSOCIATE:
2035 ieee80211_associate(dev, ifsta);
2037 case IEEE80211_ASSOCIATED:
2038 ieee80211_associated(dev, ifsta);
2040 case IEEE80211_IBSS_SEARCH:
2041 ieee80211_sta_find_ibss(dev, ifsta);
2043 case IEEE80211_IBSS_JOINED:
2044 ieee80211_sta_merge_ibss(dev, ifsta);
2047 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2052 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2053 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2054 "mixed-cell disabled - disassociate\n", dev->name);
2056 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2057 ieee80211_set_disassoc(dev, ifsta, 0);
2062 static void ieee80211_sta_reset_auth(struct net_device *dev,
2063 struct ieee80211_if_sta *ifsta)
2065 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2067 if (local->ops->reset_tsf) {
2068 /* Reset own TSF to allow time synchronization work. */
2069 local->ops->reset_tsf(local_to_hw(local));
2072 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2075 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2076 ifsta->auth_alg = WLAN_AUTH_OPEN;
2077 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2078 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2079 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2080 ifsta->auth_alg = WLAN_AUTH_LEAP;
2082 ifsta->auth_alg = WLAN_AUTH_OPEN;
2083 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2085 ifsta->auth_transaction = -1;
2086 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2087 ifsta->auth_tries = ifsta->assoc_tries = 0;
2088 netif_carrier_off(dev);
2092 void ieee80211_sta_req_auth(struct net_device *dev,
2093 struct ieee80211_if_sta *ifsta)
2095 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2096 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2098 if (sdata->type != IEEE80211_IF_TYPE_STA)
2101 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2102 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2103 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2104 IEEE80211_STA_AUTO_SSID_SEL))) {
2105 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2106 queue_work(local->hw.workqueue, &ifsta->work);
2110 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2111 const char *ssid, int ssid_len)
2113 int tmp, hidden_ssid;
2115 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2118 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2124 if (ssid[tmp] != '\0') {
2130 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2133 if (ssid_len == 1 && ssid[0] == ' ')
2139 static int ieee80211_sta_config_auth(struct net_device *dev,
2140 struct ieee80211_if_sta *ifsta)
2142 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2143 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2144 struct ieee80211_sta_bss *bss, *selected = NULL;
2145 int top_rssi = 0, freq;
2147 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2148 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
2149 ifsta->state = IEEE80211_AUTHENTICATE;
2150 ieee80211_sta_reset_auth(dev, ifsta);
2154 spin_lock_bh(&local->sta_bss_lock);
2155 freq = local->oper_channel->freq;
2156 list_for_each_entry(bss, &local->sta_bss_list, list) {
2157 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2160 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2161 !!sdata->default_key)
2164 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2168 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2169 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2172 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2173 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2176 if (!selected || top_rssi < bss->rssi) {
2178 top_rssi = bss->rssi;
2182 atomic_inc(&selected->users);
2183 spin_unlock_bh(&local->sta_bss_lock);
2186 ieee80211_set_channel(local, -1, selected->freq);
2187 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2188 ieee80211_sta_set_ssid(dev, selected->ssid,
2189 selected->ssid_len);
2190 ieee80211_sta_set_bssid(dev, selected->bssid);
2191 ieee80211_rx_bss_put(dev, selected);
2192 ifsta->state = IEEE80211_AUTHENTICATE;
2193 ieee80211_sta_reset_auth(dev, ifsta);
2196 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2197 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2198 ieee80211_sta_start_scan(dev, NULL, 0);
2200 ieee80211_sta_start_scan(dev, ifsta->ssid,
2202 ifsta->state = IEEE80211_AUTHENTICATE;
2203 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2205 ifsta->state = IEEE80211_DISABLED;
2210 static int ieee80211_sta_join_ibss(struct net_device *dev,
2211 struct ieee80211_if_sta *ifsta,
2212 struct ieee80211_sta_bss *bss)
2214 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2215 int res, rates, i, j;
2216 struct sk_buff *skb;
2217 struct ieee80211_mgmt *mgmt;
2218 struct ieee80211_tx_control control;
2219 struct ieee80211_rate *rate;
2220 struct ieee80211_hw_mode *mode;
2221 struct rate_control_extra extra;
2223 struct ieee80211_sub_if_data *sdata;
2225 /* Remove possible STA entries from other IBSS networks. */
2226 sta_info_flush(local, NULL);
2228 if (local->ops->reset_tsf) {
2229 /* Reset own TSF to allow time synchronization work. */
2230 local->ops->reset_tsf(local_to_hw(local));
2232 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2233 res = ieee80211_if_config(dev);
2237 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2239 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2240 sdata->drop_unencrypted = bss->capability &
2241 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2243 res = ieee80211_set_channel(local, -1, bss->freq);
2245 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2246 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2247 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2248 local->hw.conf.freq);
2252 /* Set beacon template based on scan results */
2253 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2258 skb_reserve(skb, local->hw.extra_tx_headroom);
2260 mgmt = (struct ieee80211_mgmt *)
2261 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2262 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2263 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2264 IEEE80211_STYPE_BEACON);
2265 memset(mgmt->da, 0xff, ETH_ALEN);
2266 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2267 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2268 mgmt->u.beacon.beacon_int =
2269 cpu_to_le16(local->hw.conf.beacon_int);
2270 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2272 pos = skb_put(skb, 2 + ifsta->ssid_len);
2273 *pos++ = WLAN_EID_SSID;
2274 *pos++ = ifsta->ssid_len;
2275 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2277 rates = bss->supp_rates_len;
2280 pos = skb_put(skb, 2 + rates);
2281 *pos++ = WLAN_EID_SUPP_RATES;
2283 memcpy(pos, bss->supp_rates, rates);
2285 pos = skb_put(skb, 2 + 1);
2286 *pos++ = WLAN_EID_DS_PARAMS;
2288 *pos++ = bss->channel;
2290 pos = skb_put(skb, 2 + 2);
2291 *pos++ = WLAN_EID_IBSS_PARAMS;
2293 /* FIX: set ATIM window based on scan results */
2297 if (bss->supp_rates_len > 8) {
2298 rates = bss->supp_rates_len - 8;
2299 pos = skb_put(skb, 2 + rates);
2300 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2302 memcpy(pos, &bss->supp_rates[8], rates);
2305 memset(&control, 0, sizeof(control));
2306 memset(&extra, 0, sizeof(extra));
2307 extra.mode = local->oper_hw_mode;
2308 rate = rate_control_get_rate(local, dev, skb, &extra);
2310 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2311 "for IBSS beacon\n", dev->name);
2315 ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
2316 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2317 rate->val2 : rate->val;
2318 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2319 control.power_level = local->hw.conf.power_level;
2320 control.flags |= IEEE80211_TXCTL_NO_ACK;
2321 control.retry_limit = 1;
2323 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2324 if (ifsta->probe_resp) {
2325 mgmt = (struct ieee80211_mgmt *)
2326 ifsta->probe_resp->data;
2327 mgmt->frame_control =
2328 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2329 IEEE80211_STYPE_PROBE_RESP);
2331 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2332 "template for IBSS\n", dev->name);
2335 if (local->ops->beacon_update &&
2336 local->ops->beacon_update(local_to_hw(local),
2337 skb, &control) == 0) {
2338 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2339 "template based on scan results\n", dev->name);
2344 mode = local->oper_hw_mode;
2345 for (i = 0; i < bss->supp_rates_len; i++) {
2346 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2347 if (mode->mode == MODE_ATHEROS_TURBO)
2349 for (j = 0; j < mode->num_rates; j++)
2350 if (mode->rates[j].rate == bitrate)
2353 ifsta->supp_rates_bits = rates;
2357 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2358 "template\n", dev->name);
2362 ifsta->state = IEEE80211_IBSS_JOINED;
2363 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2365 ieee80211_rx_bss_put(dev, bss);
2371 static int ieee80211_sta_create_ibss(struct net_device *dev,
2372 struct ieee80211_if_sta *ifsta)
2374 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2375 struct ieee80211_sta_bss *bss;
2376 struct ieee80211_sub_if_data *sdata;
2377 struct ieee80211_hw_mode *mode;
2378 u8 bssid[ETH_ALEN], *pos;
2382 /* Easier testing, use fixed BSSID. */
2383 memset(bssid, 0xfe, ETH_ALEN);
2385 /* Generate random, not broadcast, locally administered BSSID. Mix in
2386 * own MAC address to make sure that devices that do not have proper
2387 * random number generator get different BSSID. */
2388 get_random_bytes(bssid, ETH_ALEN);
2389 for (i = 0; i < ETH_ALEN; i++)
2390 bssid[i] ^= dev->dev_addr[i];
2395 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2396 dev->name, MAC_ARG(bssid));
2398 bss = ieee80211_rx_bss_add(dev, bssid);
2402 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2403 mode = local->oper_hw_mode;
2405 if (local->hw.conf.beacon_int == 0)
2406 local->hw.conf.beacon_int = 100;
2407 bss->beacon_int = local->hw.conf.beacon_int;
2408 bss->hw_mode = local->hw.conf.phymode;
2409 bss->channel = local->hw.conf.channel;
2410 bss->freq = local->hw.conf.freq;
2411 bss->last_update = jiffies;
2412 bss->capability = WLAN_CAPABILITY_IBSS;
2413 if (sdata->default_key) {
2414 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2416 sdata->drop_unencrypted = 0;
2417 bss->supp_rates_len = mode->num_rates;
2418 pos = bss->supp_rates;
2419 for (i = 0; i < mode->num_rates; i++) {
2420 int rate = mode->rates[i].rate;
2421 if (mode->mode == MODE_ATHEROS_TURBO)
2423 *pos++ = (u8) (rate / 5);
2426 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2430 static int ieee80211_sta_find_ibss(struct net_device *dev,
2431 struct ieee80211_if_sta *ifsta)
2433 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2434 struct ieee80211_sta_bss *bss;
2439 if (ifsta->ssid_len == 0)
2442 active_ibss = ieee80211_sta_active_ibss(dev);
2443 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2444 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2445 dev->name, active_ibss);
2446 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2447 spin_lock_bh(&local->sta_bss_lock);
2448 list_for_each_entry(bss, &local->sta_bss_list, list) {
2449 if (ifsta->ssid_len != bss->ssid_len ||
2450 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2451 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2453 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2454 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2455 MAC_ARG(bss->bssid));
2456 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2457 memcpy(bssid, bss->bssid, ETH_ALEN);
2459 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2462 spin_unlock_bh(&local->sta_bss_lock);
2464 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2465 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2466 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2467 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2468 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2469 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2470 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2471 " based on configured SSID\n",
2472 dev->name, MAC_ARG(bssid));
2473 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2475 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2476 printk(KERN_DEBUG " did not try to join ibss\n");
2477 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2479 /* Selected IBSS not found in current scan results - try to scan */
2480 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2481 !ieee80211_sta_active_ibss(dev)) {
2482 mod_timer(&ifsta->timer, jiffies +
2483 IEEE80211_IBSS_MERGE_INTERVAL);
2484 } else if (time_after(jiffies, local->last_scan_completed +
2485 IEEE80211_SCAN_INTERVAL)) {
2486 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2487 "join\n", dev->name);
2488 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2490 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2491 int interval = IEEE80211_SCAN_INTERVAL;
2493 if (time_after(jiffies, ifsta->ibss_join_req +
2494 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2495 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2496 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2497 return ieee80211_sta_create_ibss(dev, ifsta);
2498 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2499 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2500 " configured channel %d (%d MHz)\n",
2501 dev->name, local->hw.conf.channel,
2502 local->hw.conf.freq);
2505 /* No IBSS found - decrease scan interval and continue
2507 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2510 ifsta->state = IEEE80211_IBSS_SEARCH;
2511 mod_timer(&ifsta->timer, jiffies + interval);
2519 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2521 struct ieee80211_sub_if_data *sdata;
2522 struct ieee80211_if_sta *ifsta;
2523 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2525 if (len > IEEE80211_MAX_SSID_LEN)
2528 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2530 if (local->ops->conf_tx) {
2531 struct ieee80211_tx_queue_params qparam;
2534 memset(&qparam, 0, sizeof(qparam));
2535 /* TODO: are these ok defaults for all hw_modes? */
2538 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2539 qparam.cw_max = 1023;
2540 qparam.burst_time = 0;
2541 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2543 local->ops->conf_tx(local_to_hw(local),
2544 i + IEEE80211_TX_QUEUE_DATA0,
2547 /* IBSS uses different parameters for Beacon sending */
2551 local->ops->conf_tx(local_to_hw(local),
2552 IEEE80211_TX_QUEUE_BEACON, &qparam);
2555 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2556 ifsta = &sdata->u.sta;
2558 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2559 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2560 memcpy(ifsta->ssid, ssid, len);
2561 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2562 ifsta->ssid_len = len;
2565 ifsta->flags |= IEEE80211_STA_SSID_SET;
2567 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2568 if (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2569 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2570 ifsta->ibss_join_req = jiffies;
2571 ifsta->state = IEEE80211_IBSS_SEARCH;
2572 return ieee80211_sta_find_ibss(dev, ifsta);
2578 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2580 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2581 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2582 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2583 *len = ifsta->ssid_len;
2588 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2590 struct ieee80211_sub_if_data *sdata;
2591 struct ieee80211_if_sta *ifsta;
2594 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2595 ifsta = &sdata->u.sta;
2597 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2598 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2599 res = ieee80211_if_config(dev);
2601 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2602 "the low-level driver\n", dev->name);
2607 if (is_valid_ether_addr(bssid))
2608 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2610 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2616 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2617 struct ieee80211_sub_if_data *sdata,
2620 struct sk_buff *skb;
2621 struct ieee80211_hdr *nullfunc;
2624 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2626 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2627 "frame\n", sdata->dev->name);
2630 skb_reserve(skb, local->hw.extra_tx_headroom);
2632 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2633 memset(nullfunc, 0, 24);
2634 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2635 IEEE80211_FCTL_TODS;
2637 fc |= IEEE80211_FCTL_PM;
2638 nullfunc->frame_control = cpu_to_le16(fc);
2639 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2640 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2641 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2643 ieee80211_sta_tx(sdata->dev, skb, 0);
2647 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2649 struct ieee80211_local *local = hw_to_local(hw);
2650 struct net_device *dev = local->scan_dev;
2651 struct ieee80211_sub_if_data *sdata;
2652 union iwreq_data wrqu;
2654 local->last_scan_completed = jiffies;
2656 local->sta_scanning = 0;
2658 if (ieee80211_hw_config(local))
2659 printk(KERN_DEBUG "%s: failed to restore operational"
2660 "channel after scan\n", dev->name);
2662 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2663 ieee80211_if_config(dev))
2664 printk(KERN_DEBUG "%s: failed to restore operational"
2665 "BSSID after scan\n", dev->name);
2667 memset(&wrqu, 0, sizeof(wrqu));
2668 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2670 read_lock(&local->sub_if_lock);
2671 list_for_each_entry(sdata, &local->sub_if_list, list) {
2673 /* No need to wake the master device. */
2674 if (sdata->dev == local->mdev)
2677 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2678 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
2679 ieee80211_send_nullfunc(local, sdata, 0);
2680 ieee80211_sta_timer((unsigned long)sdata);
2683 netif_wake_queue(sdata->dev);
2685 read_unlock(&local->sub_if_lock);
2687 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2688 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2689 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2690 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2691 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2692 !ieee80211_sta_active_ibss(dev)))
2693 ieee80211_sta_find_ibss(dev, ifsta);
2696 EXPORT_SYMBOL(ieee80211_scan_completed);
2698 void ieee80211_sta_scan_work(struct work_struct *work)
2700 struct ieee80211_local *local =
2701 container_of(work, struct ieee80211_local, scan_work.work);
2702 struct net_device *dev = local->scan_dev;
2703 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2704 struct ieee80211_hw_mode *mode;
2705 struct ieee80211_channel *chan;
2707 unsigned long next_delay = 0;
2709 if (!local->sta_scanning)
2712 switch (local->scan_state) {
2713 case SCAN_SET_CHANNEL:
2714 mode = local->scan_hw_mode;
2715 if (local->scan_hw_mode->list.next == &local->modes_list &&
2716 local->scan_channel_idx >= mode->num_channels) {
2717 ieee80211_scan_completed(local_to_hw(local));
2720 skip = !(local->enabled_modes & (1 << mode->mode));
2721 chan = &mode->channels[local->scan_channel_idx];
2722 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2723 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2724 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2725 (local->hw_modes & local->enabled_modes &
2726 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2731 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2732 dev->name, chan->chan, chan->freq);
2735 local->scan_channel = chan;
2736 if (ieee80211_hw_config(local)) {
2737 printk(KERN_DEBUG "%s: failed to set channel "
2738 "%d (%d MHz) for scan\n", dev->name,
2739 chan->chan, chan->freq);
2744 local->scan_channel_idx++;
2745 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2746 if (local->scan_hw_mode->list.next != &local->modes_list) {
2747 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2748 struct ieee80211_hw_mode,
2750 local->scan_channel_idx = 0;
2757 next_delay = IEEE80211_PROBE_DELAY +
2758 usecs_to_jiffies(local->hw.channel_change_time);
2759 local->scan_state = SCAN_SEND_PROBE;
2761 case SCAN_SEND_PROBE:
2762 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2763 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2764 local->scan_ssid_len);
2765 next_delay = IEEE80211_CHANNEL_TIME;
2767 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2768 local->scan_state = SCAN_SET_CHANNEL;
2772 if (local->sta_scanning)
2773 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2778 static int ieee80211_sta_start_scan(struct net_device *dev,
2779 u8 *ssid, size_t ssid_len)
2781 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2782 struct ieee80211_sub_if_data *sdata;
2784 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2787 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2788 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2791 * ScanType: ACTIVE, PASSIVE
2792 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2793 * a Probe frame during active scanning
2795 * MinChannelTime (>= ProbeDelay), in TU
2796 * MaxChannelTime: (>= MinChannelTime), in TU
2799 /* MLME-SCAN.confirm
2801 * ResultCode: SUCCESS, INVALID_PARAMETERS
2804 if (local->sta_scanning) {
2805 if (local->scan_dev == dev)
2810 if (local->ops->hw_scan) {
2811 int rc = local->ops->hw_scan(local_to_hw(local),
2814 local->sta_scanning = 1;
2815 local->scan_dev = dev;
2820 local->sta_scanning = 1;
2822 read_lock(&local->sub_if_lock);
2823 list_for_each_entry(sdata, &local->sub_if_list, list) {
2825 /* Don't stop the master interface, otherwise we can't transmit
2827 if (sdata->dev == local->mdev)
2830 netif_stop_queue(sdata->dev);
2831 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2832 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
2833 ieee80211_send_nullfunc(local, sdata, 1);
2835 read_unlock(&local->sub_if_lock);
2838 local->scan_ssid_len = ssid_len;
2839 memcpy(local->scan_ssid, ssid, ssid_len);
2841 local->scan_ssid_len = 0;
2842 local->scan_state = SCAN_SET_CHANNEL;
2843 local->scan_hw_mode = list_entry(local->modes_list.next,
2844 struct ieee80211_hw_mode,
2846 local->scan_channel_idx = 0;
2847 local->scan_dev = dev;
2849 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2850 ieee80211_if_config(dev))
2851 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2854 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2855 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2856 IEEE80211_CHANNEL_TIME);
2862 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2864 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2865 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2866 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2868 if (sdata->type != IEEE80211_IF_TYPE_STA)
2869 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2871 if (local->sta_scanning) {
2872 if (local->scan_dev == dev)
2877 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2878 queue_work(local->hw.workqueue, &ifsta->work);
2883 ieee80211_sta_scan_result(struct net_device *dev,
2884 struct ieee80211_sta_bss *bss,
2885 char *current_ev, char *end_buf)
2887 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2888 struct iw_event iwe;
2890 if (time_after(jiffies,
2891 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2894 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2897 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2898 !bss->wpa_ie && !bss->rsn_ie)
2901 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2902 (local->scan_ssid_len != bss->ssid_len ||
2903 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2906 memset(&iwe, 0, sizeof(iwe));
2907 iwe.cmd = SIOCGIWAP;
2908 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2909 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2910 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2913 memset(&iwe, 0, sizeof(iwe));
2914 iwe.cmd = SIOCGIWESSID;
2915 iwe.u.data.length = bss->ssid_len;
2916 iwe.u.data.flags = 1;
2917 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2920 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2921 memset(&iwe, 0, sizeof(iwe));
2922 iwe.cmd = SIOCGIWMODE;
2923 if (bss->capability & WLAN_CAPABILITY_ESS)
2924 iwe.u.mode = IW_MODE_MASTER;
2926 iwe.u.mode = IW_MODE_ADHOC;
2927 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2931 memset(&iwe, 0, sizeof(iwe));
2932 iwe.cmd = SIOCGIWFREQ;
2933 iwe.u.freq.m = bss->channel;
2935 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2937 iwe.u.freq.m = bss->freq * 100000;
2939 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2942 memset(&iwe, 0, sizeof(iwe));
2944 iwe.u.qual.qual = bss->signal;
2945 iwe.u.qual.level = bss->rssi;
2946 iwe.u.qual.noise = bss->noise;
2947 iwe.u.qual.updated = local->wstats_flags;
2948 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2951 memset(&iwe, 0, sizeof(iwe));
2952 iwe.cmd = SIOCGIWENCODE;
2953 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2954 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2956 iwe.u.data.flags = IW_ENCODE_DISABLED;
2957 iwe.u.data.length = 0;
2958 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2960 if (bss && bss->wpa_ie) {
2961 memset(&iwe, 0, sizeof(iwe));
2962 iwe.cmd = IWEVGENIE;
2963 iwe.u.data.length = bss->wpa_ie_len;
2964 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2968 if (bss && bss->rsn_ie) {
2969 memset(&iwe, 0, sizeof(iwe));
2970 iwe.cmd = IWEVGENIE;
2971 iwe.u.data.length = bss->rsn_ie_len;
2972 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2976 if (bss && bss->supp_rates_len > 0) {
2977 /* display all supported rates in readable format */
2978 char *p = current_ev + IW_EV_LCP_LEN;
2981 memset(&iwe, 0, sizeof(iwe));
2982 iwe.cmd = SIOCGIWRATE;
2983 /* Those two flags are ignored... */
2984 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2986 for (i = 0; i < bss->supp_rates_len; i++) {
2987 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2989 p = iwe_stream_add_value(current_ev, p,
2990 end_buf, &iwe, IW_EV_PARAM_LEN);
2997 buf = kmalloc(30, GFP_ATOMIC);
2999 memset(&iwe, 0, sizeof(iwe));
3000 iwe.cmd = IWEVCUSTOM;
3001 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3002 iwe.u.data.length = strlen(buf);
3003 current_ev = iwe_stream_add_point(current_ev, end_buf,
3012 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
3015 buf = kmalloc(100, GFP_ATOMIC);
3019 memset(&iwe, 0, sizeof(iwe));
3020 iwe.cmd = IWEVCUSTOM;
3021 sprintf(buf, "bcn_int=%d", bss->beacon_int);
3022 iwe.u.data.length = strlen(buf);
3023 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3026 memset(&iwe, 0, sizeof(iwe));
3027 iwe.cmd = IWEVCUSTOM;
3028 sprintf(buf, "capab=0x%04x", bss->capability);
3029 iwe.u.data.length = strlen(buf);
3030 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3041 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3043 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3044 char *current_ev = buf;
3045 char *end_buf = buf + len;
3046 struct ieee80211_sta_bss *bss;
3048 spin_lock_bh(&local->sta_bss_lock);
3049 list_for_each_entry(bss, &local->sta_bss_list, list) {
3050 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3051 spin_unlock_bh(&local->sta_bss_lock);
3054 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3057 spin_unlock_bh(&local->sta_bss_lock);
3058 return current_ev - buf;
3062 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3064 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3065 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3066 kfree(ifsta->extra_ie);
3068 ifsta->extra_ie = NULL;
3069 ifsta->extra_ie_len = 0;
3072 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3073 if (!ifsta->extra_ie) {
3074 ifsta->extra_ie_len = 0;
3077 memcpy(ifsta->extra_ie, ie, len);
3078 ifsta->extra_ie_len = len;
3083 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3084 struct sk_buff *skb, u8 *bssid,
3087 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3088 struct sta_info *sta;
3089 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3091 /* TODO: Could consider removing the least recently used entry and
3092 * allow new one to be added. */
3093 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3094 if (net_ratelimit()) {
3095 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3096 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3101 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3102 local->mdev->name, MAC_ARG(addr), dev->name);
3104 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3108 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3110 rate_control_rate_init(sta, local);
3112 return sta; /* caller will call sta_info_put() */
3116 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3118 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3119 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3121 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3124 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3125 sdata->type != IEEE80211_IF_TYPE_IBSS)
3128 ieee80211_send_deauth(dev, ifsta, reason);
3129 ieee80211_set_disassoc(dev, ifsta, 1);
3134 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3136 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3137 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3139 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3142 if (sdata->type != IEEE80211_IF_TYPE_STA)
3145 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
3148 ieee80211_send_disassoc(dev, ifsta, reason);
3149 ieee80211_set_disassoc(dev, ifsta, 0);