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 <linux/rtnetlink.h>
29 #include <net/iw_handler.h>
30 #include <asm/types.h>
32 #include <net/mac80211.h>
33 #include "ieee80211_i.h"
34 #include "ieee80211_rate.h"
35 #include "hostapd_ioctl.h"
37 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38 #define IEEE80211_AUTH_MAX_TRIES 3
39 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40 #define IEEE80211_ASSOC_MAX_TRIES 3
41 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
42 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
43 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
44 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
45 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
46 #define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48 #define IEEE80211_PROBE_DELAY (HZ / 33)
49 #define IEEE80211_CHANNEL_TIME (HZ / 33)
50 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
51 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
52 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
53 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
55 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
60 #define ERP_INFO_USE_PROTECTION BIT(1)
62 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
63 u8 *ssid, size_t ssid_len);
64 static struct ieee80211_sta_bss *
65 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid);
66 static void ieee80211_rx_bss_put(struct net_device *dev,
67 struct ieee80211_sta_bss *bss);
68 static int ieee80211_sta_find_ibss(struct net_device *dev,
69 struct ieee80211_if_sta *ifsta);
70 static int ieee80211_sta_wep_configured(struct net_device *dev);
71 static int ieee80211_sta_start_scan(struct net_device *dev,
72 u8 *ssid, size_t ssid_len);
73 static int ieee80211_sta_config_auth(struct net_device *dev,
74 struct ieee80211_if_sta *ifsta);
77 /* Parsed Information Elements */
78 struct ieee802_11_elems {
95 /* length of them, respectively */
107 u8 ext_supp_rates_len;
112 typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
115 static ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
116 struct ieee802_11_elems *elems)
122 memset(elems, 0, sizeof(*elems));
134 printk(KERN_DEBUG "IEEE 802.11 element parse "
135 "failed (id=%d elen=%d left=%d)\n",
144 elems->ssid_len = elen;
146 case WLAN_EID_SUPP_RATES:
147 elems->supp_rates = pos;
148 elems->supp_rates_len = elen;
150 case WLAN_EID_FH_PARAMS:
151 elems->fh_params = pos;
152 elems->fh_params_len = elen;
154 case WLAN_EID_DS_PARAMS:
155 elems->ds_params = pos;
156 elems->ds_params_len = elen;
158 case WLAN_EID_CF_PARAMS:
159 elems->cf_params = pos;
160 elems->cf_params_len = elen;
164 elems->tim_len = elen;
166 case WLAN_EID_IBSS_PARAMS:
167 elems->ibss_params = pos;
168 elems->ibss_params_len = elen;
170 case WLAN_EID_CHALLENGE:
171 elems->challenge = pos;
172 elems->challenge_len = elen;
175 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
177 /* Microsoft OUI (00:50:F2) */
179 /* OUI Type 1 - WPA IE */
181 elems->wpa_len = elen;
182 } else if (elen >= 5 && pos[3] == 2) {
184 elems->wmm_info = pos;
185 elems->wmm_info_len = elen;
186 } else if (pos[4] == 1) {
187 elems->wmm_param = pos;
188 elems->wmm_param_len = elen;
195 elems->rsn_len = elen;
197 case WLAN_EID_ERP_INFO:
198 elems->erp_info = pos;
199 elems->erp_info_len = elen;
201 case WLAN_EID_EXT_SUPP_RATES:
202 elems->ext_supp_rates = pos;
203 elems->ext_supp_rates_len = elen;
207 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
208 "unknown element (id=%d elen=%d)\n",
219 /* Do not trigger error if left == 1 as Apple Airport base stations
220 * send AssocResps that are one spurious byte too long. */
222 return unknown ? ParseUnknown : ParseOK;
228 static int ecw2cw(int ecw)
239 static void ieee80211_sta_wmm_params(struct net_device *dev,
240 struct ieee80211_if_sta *ifsta,
241 u8 *wmm_param, size_t wmm_param_len)
243 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
244 struct ieee80211_tx_queue_params params;
249 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
251 count = wmm_param[6] & 0x0f;
252 if (count == ifsta->wmm_last_param_set)
254 ifsta->wmm_last_param_set = count;
257 left = wmm_param_len - 8;
259 memset(¶ms, 0, sizeof(params));
261 if (!local->ops->conf_tx)
265 for (; left >= 4; left -= 4, pos += 4) {
266 int aci = (pos[0] >> 5) & 0x03;
267 int acm = (pos[0] >> 4) & 0x01;
272 queue = IEEE80211_TX_QUEUE_DATA3;
274 local->wmm_acm |= BIT(0) | BIT(3);
278 queue = IEEE80211_TX_QUEUE_DATA1;
280 local->wmm_acm |= BIT(4) | BIT(5);
284 queue = IEEE80211_TX_QUEUE_DATA0;
286 local->wmm_acm |= BIT(6) | BIT(7);
291 queue = IEEE80211_TX_QUEUE_DATA2;
293 local->wmm_acm |= BIT(1) | BIT(2);
298 params.aifs = pos[0] & 0x0f;
299 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
300 params.cw_min = ecw2cw(pos[1] & 0x0f);
301 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
302 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
303 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
304 "cWmin=%d cWmax=%d burst=%d\n",
305 dev->name, queue, aci, acm, params.aifs, params.cw_min,
306 params.cw_max, params.burst_time);
307 /* TODO: handle ACM (block TX, fallback to next lowest allowed
309 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
310 printk(KERN_DEBUG "%s: failed to set TX queue "
311 "parameters for queue %d\n", dev->name, queue);
317 static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
319 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
320 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
321 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
323 if (use_protection != 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));
331 sdata->use_protection = use_protection;
336 static void ieee80211_sta_send_associnfo(struct net_device *dev,
337 struct ieee80211_if_sta *ifsta)
342 union iwreq_data wrqu;
344 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
347 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
348 ifsta->assocresp_ies_len), GFP_ATOMIC);
352 len = sprintf(buf, "ASSOCINFO(");
353 if (ifsta->assocreq_ies) {
354 len += sprintf(buf + len, "ReqIEs=");
355 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
356 len += sprintf(buf + len, "%02x",
357 ifsta->assocreq_ies[i]);
360 if (ifsta->assocresp_ies) {
361 if (ifsta->assocreq_ies)
362 len += sprintf(buf + len, " ");
363 len += sprintf(buf + len, "RespIEs=");
364 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
365 len += sprintf(buf + len, "%02x",
366 ifsta->assocresp_ies[i]);
369 len += sprintf(buf + len, ")");
371 if (len > IW_CUSTOM_MAX) {
372 len = sprintf(buf, "ASSOCRESPIE=");
373 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
374 len += sprintf(buf + len, "%02x",
375 ifsta->assocresp_ies[i]);
379 memset(&wrqu, 0, sizeof(wrqu));
380 wrqu.data.length = len;
381 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
387 static void ieee80211_set_associated(struct net_device *dev,
388 struct ieee80211_if_sta *ifsta, int assoc)
390 union iwreq_data wrqu;
391 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
393 if (ifsta->associated == assoc)
396 ifsta->associated = assoc;
399 struct ieee80211_sub_if_data *sdata;
400 struct ieee80211_sta_bss *bss;
401 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
402 if (sdata->type != IEEE80211_IF_TYPE_STA)
405 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
407 if (bss->has_erp_value)
408 ieee80211_handle_erp_ie(dev, bss->erp_value);
409 ieee80211_rx_bss_put(dev, bss);
412 netif_carrier_on(dev);
413 ifsta->prev_bssid_set = 1;
414 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
415 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
416 ieee80211_sta_send_associnfo(dev, ifsta);
418 netif_carrier_off(dev);
419 sdata->use_protection = 0;
420 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
422 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
423 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
424 ifsta->last_probe = jiffies;
427 static void ieee80211_set_disassoc(struct net_device *dev,
428 struct ieee80211_if_sta *ifsta, int deauth)
431 ifsta->auth_tries = 0;
432 ifsta->assoc_tries = 0;
433 ieee80211_set_associated(dev, ifsta, 0);
436 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
439 struct ieee80211_sub_if_data *sdata;
440 struct ieee80211_tx_packet_data *pkt_data;
442 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
443 skb->dev = sdata->local->mdev;
444 skb_set_mac_header(skb, 0);
445 skb_set_network_header(skb, 0);
446 skb_set_transport_header(skb, 0);
448 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
449 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
450 pkt_data->ifindex = sdata->dev->ifindex;
451 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
452 pkt_data->do_not_encrypt = !encrypt;
458 static void ieee80211_send_auth(struct net_device *dev,
459 struct ieee80211_if_sta *ifsta,
460 int transaction, u8 *extra, size_t extra_len,
463 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
465 struct ieee80211_mgmt *mgmt;
467 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
468 sizeof(*mgmt) + 6 + extra_len);
470 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
471 "frame\n", dev->name);
474 skb_reserve(skb, local->hw.extra_tx_headroom);
476 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
477 memset(mgmt, 0, 24 + 6);
478 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
479 IEEE80211_STYPE_AUTH);
481 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
482 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
483 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
484 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
485 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
486 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
487 ifsta->auth_transaction = transaction + 1;
488 mgmt->u.auth.status_code = cpu_to_le16(0);
490 memcpy(skb_put(skb, extra_len), extra, extra_len);
492 ieee80211_sta_tx(dev, skb, encrypt);
496 static void ieee80211_authenticate(struct net_device *dev,
497 struct ieee80211_if_sta *ifsta)
500 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
501 printk(KERN_DEBUG "%s: authentication with AP " MAC_FMT
503 dev->name, MAC_ARG(ifsta->bssid));
504 ifsta->state = IEEE80211_DISABLED;
508 ifsta->state = IEEE80211_AUTHENTICATE;
509 printk(KERN_DEBUG "%s: authenticate with AP " MAC_FMT "\n",
510 dev->name, MAC_ARG(ifsta->bssid));
512 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
514 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
518 static void ieee80211_send_assoc(struct net_device *dev,
519 struct ieee80211_if_sta *ifsta)
521 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
522 struct ieee80211_hw_mode *mode;
524 struct ieee80211_mgmt *mgmt;
528 struct ieee80211_sta_bss *bss;
531 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
532 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
535 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
536 "frame\n", dev->name);
539 skb_reserve(skb, local->hw.extra_tx_headroom);
541 mode = local->oper_hw_mode;
542 capab = ifsta->capab;
543 if (mode->mode == MODE_IEEE80211G) {
544 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
545 WLAN_CAPABILITY_SHORT_PREAMBLE;
547 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
549 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
550 capab |= WLAN_CAPABILITY_PRIVACY;
554 ieee80211_rx_bss_put(dev, bss);
557 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
559 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
560 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
561 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
563 if (ifsta->prev_bssid_set) {
565 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
566 IEEE80211_STYPE_REASSOC_REQ);
567 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
568 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
569 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
573 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
574 IEEE80211_STYPE_ASSOC_REQ);
575 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
576 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
580 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
581 *pos++ = WLAN_EID_SSID;
582 *pos++ = ifsta->ssid_len;
583 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
585 len = mode->num_rates;
588 pos = skb_put(skb, len + 2);
589 *pos++ = WLAN_EID_SUPP_RATES;
591 for (i = 0; i < len; i++) {
592 int rate = mode->rates[i].rate;
593 if (mode->mode == MODE_ATHEROS_TURBO)
595 *pos++ = (u8) (rate / 5);
598 if (mode->num_rates > len) {
599 pos = skb_put(skb, mode->num_rates - len + 2);
600 *pos++ = WLAN_EID_EXT_SUPP_RATES;
601 *pos++ = mode->num_rates - len;
602 for (i = len; i < mode->num_rates; i++) {
603 int rate = mode->rates[i].rate;
604 if (mode->mode == MODE_ATHEROS_TURBO)
606 *pos++ = (u8) (rate / 5);
610 if (ifsta->extra_ie) {
611 pos = skb_put(skb, ifsta->extra_ie_len);
612 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
615 if (wmm && ifsta->wmm_enabled) {
616 pos = skb_put(skb, 9);
617 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
618 *pos++ = 7; /* len */
619 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
622 *pos++ = 2; /* WME */
623 *pos++ = 0; /* WME info */
624 *pos++ = 1; /* WME ver */
628 kfree(ifsta->assocreq_ies);
629 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
630 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_ATOMIC);
631 if (ifsta->assocreq_ies)
632 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
634 ieee80211_sta_tx(dev, skb, 0);
638 static void ieee80211_send_deauth(struct net_device *dev,
639 struct ieee80211_if_sta *ifsta, u16 reason)
641 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
643 struct ieee80211_mgmt *mgmt;
645 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
647 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
648 "frame\n", dev->name);
651 skb_reserve(skb, local->hw.extra_tx_headroom);
653 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
655 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
656 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
657 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
658 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
659 IEEE80211_STYPE_DEAUTH);
661 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
663 ieee80211_sta_tx(dev, skb, 0);
667 static void ieee80211_send_disassoc(struct net_device *dev,
668 struct ieee80211_if_sta *ifsta, u16 reason)
670 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
672 struct ieee80211_mgmt *mgmt;
674 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
676 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
677 "frame\n", dev->name);
680 skb_reserve(skb, local->hw.extra_tx_headroom);
682 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
684 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
685 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
686 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
687 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
688 IEEE80211_STYPE_DISASSOC);
690 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
692 ieee80211_sta_tx(dev, skb, 0);
696 static int ieee80211_privacy_mismatch(struct net_device *dev,
697 struct ieee80211_if_sta *ifsta)
699 struct ieee80211_sta_bss *bss;
702 if (!ifsta || ifsta->mixed_cell ||
703 ifsta->key_mgmt != IEEE80211_KEY_MGMT_NONE)
706 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
710 if (ieee80211_sta_wep_configured(dev) !=
711 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
714 ieee80211_rx_bss_put(dev, bss);
720 static void ieee80211_associate(struct net_device *dev,
721 struct ieee80211_if_sta *ifsta)
723 ifsta->assoc_tries++;
724 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
725 printk(KERN_DEBUG "%s: association with AP " MAC_FMT
727 dev->name, MAC_ARG(ifsta->bssid));
728 ifsta->state = IEEE80211_DISABLED;
732 ifsta->state = IEEE80211_ASSOCIATE;
733 printk(KERN_DEBUG "%s: associate with AP " MAC_FMT "\n",
734 dev->name, MAC_ARG(ifsta->bssid));
735 if (ieee80211_privacy_mismatch(dev, ifsta)) {
736 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
737 "mixed-cell disabled - abort association\n", dev->name);
738 ifsta->state = IEEE80211_DISABLED;
742 ieee80211_send_assoc(dev, ifsta);
744 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
748 static void ieee80211_associated(struct net_device *dev,
749 struct ieee80211_if_sta *ifsta)
751 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
752 struct sta_info *sta;
755 /* TODO: start monitoring current AP signal quality and number of
756 * missed beacons. Scan other channels every now and then and search
758 /* TODO: remove expired BSSes */
760 ifsta->state = IEEE80211_ASSOCIATED;
762 sta = sta_info_get(local, ifsta->bssid);
764 printk(KERN_DEBUG "%s: No STA entry for own AP " MAC_FMT "\n",
765 dev->name, MAC_ARG(ifsta->bssid));
769 if (time_after(jiffies,
770 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
771 if (ifsta->probereq_poll) {
772 printk(KERN_DEBUG "%s: No ProbeResp from "
773 "current AP " MAC_FMT " - assume out of "
775 dev->name, MAC_ARG(ifsta->bssid));
777 sta_info_free(sta, 0);
778 ifsta->probereq_poll = 0;
780 ieee80211_send_probe_req(dev, ifsta->bssid,
782 local->scan_ssid_len);
783 ifsta->probereq_poll = 1;
786 ifsta->probereq_poll = 0;
787 if (time_after(jiffies, ifsta->last_probe +
788 IEEE80211_PROBE_INTERVAL)) {
789 ifsta->last_probe = jiffies;
790 ieee80211_send_probe_req(dev, ifsta->bssid,
798 union iwreq_data wrqu;
799 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
800 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
801 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
802 mod_timer(&ifsta->timer, jiffies +
803 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
805 mod_timer(&ifsta->timer, jiffies +
806 IEEE80211_MONITORING_INTERVAL);
811 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
812 u8 *ssid, size_t ssid_len)
814 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
815 struct ieee80211_hw_mode *mode;
817 struct ieee80211_mgmt *mgmt;
818 u8 *pos, *supp_rates, *esupp_rates = NULL;
821 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
823 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
824 "request\n", dev->name);
827 skb_reserve(skb, local->hw.extra_tx_headroom);
829 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
831 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
832 IEEE80211_STYPE_PROBE_REQ);
833 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
835 memcpy(mgmt->da, dst, ETH_ALEN);
836 memcpy(mgmt->bssid, dst, ETH_ALEN);
838 memset(mgmt->da, 0xff, ETH_ALEN);
839 memset(mgmt->bssid, 0xff, ETH_ALEN);
841 pos = skb_put(skb, 2 + ssid_len);
842 *pos++ = WLAN_EID_SSID;
844 memcpy(pos, ssid, ssid_len);
846 supp_rates = skb_put(skb, 2);
847 supp_rates[0] = WLAN_EID_SUPP_RATES;
849 mode = local->oper_hw_mode;
850 for (i = 0; i < mode->num_rates; i++) {
851 struct ieee80211_rate *rate = &mode->rates[i];
852 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
855 pos = skb_put(skb, 1);
857 } else if (supp_rates[1] == 8) {
858 esupp_rates = skb_put(skb, 3);
859 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
861 pos = &esupp_rates[2];
863 pos = skb_put(skb, 1);
866 if (mode->mode == MODE_ATHEROS_TURBO)
867 *pos = rate->rate / 10;
869 *pos = rate->rate / 5;
872 ieee80211_sta_tx(dev, skb, 0);
876 static int ieee80211_sta_wep_configured(struct net_device *dev)
878 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
879 if (!sdata || !sdata->default_key ||
880 sdata->default_key->alg != ALG_WEP)
886 static void ieee80211_auth_completed(struct net_device *dev,
887 struct ieee80211_if_sta *ifsta)
889 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
890 ifsta->authenticated = 1;
891 ieee80211_associate(dev, ifsta);
895 static void ieee80211_auth_challenge(struct net_device *dev,
896 struct ieee80211_if_sta *ifsta,
897 struct ieee80211_mgmt *mgmt,
901 struct ieee802_11_elems elems;
903 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
904 pos = mgmt->u.auth.variable;
905 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
907 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
911 if (!elems.challenge) {
912 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
913 "frame\n", dev->name);
916 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
917 elems.challenge_len + 2, 1);
921 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
922 struct ieee80211_if_sta *ifsta,
923 struct ieee80211_mgmt *mgmt,
926 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
927 u16 auth_alg, auth_transaction, status_code;
929 if (ifsta->state != IEEE80211_AUTHENTICATE &&
930 sdata->type != IEEE80211_IF_TYPE_IBSS) {
931 printk(KERN_DEBUG "%s: authentication frame received from "
932 MAC_FMT ", but not in authenticate state - ignored\n",
933 dev->name, MAC_ARG(mgmt->sa));
938 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
939 "received from " MAC_FMT " - ignored\n",
940 dev->name, len, MAC_ARG(mgmt->sa));
944 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
945 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
946 printk(KERN_DEBUG "%s: authentication frame received from "
947 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
948 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
949 MAC_ARG(mgmt->bssid));
953 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
954 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
955 printk(KERN_DEBUG "%s: authentication frame received from "
956 "unknown BSSID (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
957 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
958 MAC_ARG(mgmt->bssid));
962 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
963 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
964 status_code = le16_to_cpu(mgmt->u.auth.status_code);
966 printk(KERN_DEBUG "%s: RX authentication from " MAC_FMT " (alg=%d "
967 "transaction=%d status=%d)\n",
968 dev->name, MAC_ARG(mgmt->sa), auth_alg,
969 auth_transaction, status_code);
971 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
972 /* IEEE 802.11 standard does not require authentication in IBSS
973 * networks and most implementations do not seem to use it.
974 * However, try to reply to authentication attempts if someone
975 * has actually implemented this.
976 * TODO: Could implement shared key authentication. */
977 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
978 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
979 "frame (alg=%d transaction=%d)\n",
980 dev->name, auth_alg, auth_transaction);
983 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
986 if (auth_alg != ifsta->auth_alg ||
987 auth_transaction != ifsta->auth_transaction) {
988 printk(KERN_DEBUG "%s: unexpected authentication frame "
989 "(alg=%d transaction=%d)\n",
990 dev->name, auth_alg, auth_transaction);
994 if (status_code != WLAN_STATUS_SUCCESS) {
995 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
996 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
997 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
999 const int num_algs = ARRAY_SIZE(algs);
1001 algs[0] = algs[1] = algs[2] = 0xff;
1002 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1003 algs[0] = WLAN_AUTH_OPEN;
1004 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1005 algs[1] = WLAN_AUTH_SHARED_KEY;
1006 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1007 algs[2] = WLAN_AUTH_LEAP;
1008 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1010 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1014 for (i = 0; i < num_algs; i++) {
1016 if (pos >= num_algs)
1018 if (algs[pos] == ifsta->auth_alg ||
1021 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1022 !ieee80211_sta_wep_configured(dev))
1024 ifsta->auth_alg = algs[pos];
1025 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1027 dev->name, ifsta->auth_alg);
1034 switch (ifsta->auth_alg) {
1035 case WLAN_AUTH_OPEN:
1036 case WLAN_AUTH_LEAP:
1037 ieee80211_auth_completed(dev, ifsta);
1039 case WLAN_AUTH_SHARED_KEY:
1040 if (ifsta->auth_transaction == 4)
1041 ieee80211_auth_completed(dev, ifsta);
1043 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1049 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1050 struct ieee80211_if_sta *ifsta,
1051 struct ieee80211_mgmt *mgmt,
1057 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1058 "received from " MAC_FMT " - ignored\n",
1059 dev->name, len, MAC_ARG(mgmt->sa));
1063 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1064 printk(KERN_DEBUG "%s: deauthentication frame received from "
1065 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1066 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1067 MAC_ARG(mgmt->bssid));
1071 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1073 printk(KERN_DEBUG "%s: RX deauthentication from " MAC_FMT
1075 dev->name, MAC_ARG(mgmt->sa), reason_code);
1077 if (ifsta->authenticated) {
1078 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1081 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1082 ifsta->state == IEEE80211_ASSOCIATE ||
1083 ifsta->state == IEEE80211_ASSOCIATED) {
1084 ifsta->state = IEEE80211_AUTHENTICATE;
1085 mod_timer(&ifsta->timer, jiffies +
1086 IEEE80211_RETRY_AUTH_INTERVAL);
1089 ieee80211_set_disassoc(dev, ifsta, 1);
1090 ifsta->authenticated = 0;
1094 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1095 struct ieee80211_if_sta *ifsta,
1096 struct ieee80211_mgmt *mgmt,
1102 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1103 "received from " MAC_FMT " - ignored\n",
1104 dev->name, len, MAC_ARG(mgmt->sa));
1108 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1109 printk(KERN_DEBUG "%s: disassociation frame received from "
1110 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1111 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1112 MAC_ARG(mgmt->bssid));
1116 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1118 printk(KERN_DEBUG "%s: RX disassociation from " MAC_FMT
1120 dev->name, MAC_ARG(mgmt->sa), reason_code);
1122 if (ifsta->associated)
1123 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1125 if (ifsta->state == IEEE80211_ASSOCIATED) {
1126 ifsta->state = IEEE80211_ASSOCIATE;
1127 mod_timer(&ifsta->timer, jiffies +
1128 IEEE80211_RETRY_AUTH_INTERVAL);
1131 ieee80211_set_disassoc(dev, ifsta, 0);
1135 static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1136 struct ieee80211_if_sta *ifsta,
1137 struct ieee80211_mgmt *mgmt,
1141 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1142 struct ieee80211_hw_mode *mode;
1143 struct sta_info *sta;
1145 u16 capab_info, status_code, aid;
1146 struct ieee802_11_elems elems;
1150 /* AssocResp and ReassocResp have identical structure, so process both
1151 * of them in this function. */
1153 if (ifsta->state != IEEE80211_ASSOCIATE) {
1154 printk(KERN_DEBUG "%s: association frame received from "
1155 MAC_FMT ", but not in associate state - ignored\n",
1156 dev->name, MAC_ARG(mgmt->sa));
1161 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1162 "received from " MAC_FMT " - ignored\n",
1163 dev->name, len, MAC_ARG(mgmt->sa));
1167 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1168 printk(KERN_DEBUG "%s: association frame received from "
1169 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1170 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1171 MAC_ARG(mgmt->bssid));
1175 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1176 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1177 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1178 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1179 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1180 "set\n", dev->name, aid);
1181 aid &= ~(BIT(15) | BIT(14));
1183 printk(KERN_DEBUG "%s: RX %sssocResp from " MAC_FMT " (capab=0x%x "
1184 "status=%d aid=%d)\n",
1185 dev->name, reassoc ? "Rea" : "A", MAC_ARG(mgmt->sa),
1186 capab_info, status_code, aid);
1188 if (status_code != WLAN_STATUS_SUCCESS) {
1189 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1190 dev->name, status_code);
1191 if (status_code == WLAN_STATUS_REASSOC_NO_ASSOC)
1192 ifsta->prev_bssid_set = 0;
1196 pos = mgmt->u.assoc_resp.variable;
1197 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1199 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1204 if (!elems.supp_rates) {
1205 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1210 /* it probably doesn't, but if the frame includes an ERP value then
1211 * update our stored copy */
1212 if (elems.erp_info && elems.erp_info_len >= 1) {
1213 struct ieee80211_sta_bss *bss
1214 = ieee80211_rx_bss_get(dev, ifsta->bssid);
1216 bss->erp_value = elems.erp_info[0];
1217 bss->has_erp_value = 1;
1218 ieee80211_rx_bss_put(dev, bss);
1222 printk(KERN_DEBUG "%s: associated\n", dev->name);
1224 ifsta->ap_capab = capab_info;
1226 kfree(ifsta->assocresp_ies);
1227 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1228 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_ATOMIC);
1229 if (ifsta->assocresp_ies)
1230 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1232 ieee80211_set_associated(dev, ifsta, 1);
1234 /* Add STA entry for the AP */
1235 sta = sta_info_get(local, ifsta->bssid);
1237 struct ieee80211_sta_bss *bss;
1238 sta = sta_info_add(local, dev, ifsta->bssid, GFP_ATOMIC);
1240 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1241 " AP\n", dev->name);
1244 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1246 sta->last_rssi = bss->rssi;
1247 sta->last_signal = bss->signal;
1248 sta->last_noise = bss->noise;
1249 ieee80211_rx_bss_put(dev, bss);
1254 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
1258 mode = local->oper_hw_mode;
1259 for (i = 0; i < elems.supp_rates_len; i++) {
1260 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1261 if (mode->mode == MODE_ATHEROS_TURBO)
1263 for (j = 0; j < mode->num_rates; j++)
1264 if (mode->rates[j].rate == rate)
1267 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1268 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1269 if (mode->mode == MODE_ATHEROS_TURBO)
1271 for (j = 0; j < mode->num_rates; j++)
1272 if (mode->rates[j].rate == rate)
1275 sta->supp_rates = rates;
1277 rate_control_rate_init(sta, local);
1279 if (elems.wmm_param && ifsta->wmm_enabled) {
1280 sta->flags |= WLAN_STA_WME;
1281 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1282 elems.wmm_param_len);
1288 ieee80211_associated(dev, ifsta);
1292 /* Caller must hold local->sta_bss_lock */
1293 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1294 struct ieee80211_sta_bss *bss)
1296 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1297 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1298 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1302 /* Caller must hold local->sta_bss_lock */
1303 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1304 struct ieee80211_sta_bss *bss)
1306 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1307 struct ieee80211_sta_bss *b, *prev = NULL;
1308 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1312 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1315 prev->hnext = bss->hnext;
1324 static struct ieee80211_sta_bss *
1325 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1327 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1328 struct ieee80211_sta_bss *bss;
1330 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1333 memset(bss, 0, sizeof(*bss));
1334 atomic_inc(&bss->users);
1335 atomic_inc(&bss->users);
1336 memcpy(bss->bssid, bssid, ETH_ALEN);
1338 spin_lock_bh(&local->sta_bss_lock);
1339 /* TODO: order by RSSI? */
1340 list_add_tail(&bss->list, &local->sta_bss_list);
1341 __ieee80211_rx_bss_hash_add(dev, bss);
1342 spin_unlock_bh(&local->sta_bss_lock);
1347 static struct ieee80211_sta_bss *
1348 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1350 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1351 struct ieee80211_sta_bss *bss;
1353 spin_lock_bh(&local->sta_bss_lock);
1354 bss = local->sta_bss_hash[STA_HASH(bssid)];
1356 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1357 atomic_inc(&bss->users);
1362 spin_unlock_bh(&local->sta_bss_lock);
1367 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1376 static void ieee80211_rx_bss_put(struct net_device *dev,
1377 struct ieee80211_sta_bss *bss)
1379 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1380 if (!atomic_dec_and_test(&bss->users))
1383 spin_lock_bh(&local->sta_bss_lock);
1384 __ieee80211_rx_bss_hash_del(dev, bss);
1385 list_del(&bss->list);
1386 spin_unlock_bh(&local->sta_bss_lock);
1387 ieee80211_rx_bss_free(bss);
1391 void ieee80211_rx_bss_list_init(struct net_device *dev)
1393 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1394 spin_lock_init(&local->sta_bss_lock);
1395 INIT_LIST_HEAD(&local->sta_bss_list);
1399 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1401 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1402 struct ieee80211_sta_bss *bss, *tmp;
1404 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1405 ieee80211_rx_bss_put(dev, bss);
1409 static void ieee80211_rx_bss_info(struct net_device *dev,
1410 struct ieee80211_mgmt *mgmt,
1412 struct ieee80211_rx_status *rx_status,
1415 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1416 struct ieee802_11_elems elems;
1418 int channel, invalid = 0, clen;
1419 struct ieee80211_sta_bss *bss;
1420 struct sta_info *sta;
1421 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1424 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1425 return; /* ignore ProbeResp to foreign address */
1428 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1429 dev->name, beacon ? "Beacon" : "Probe Response",
1430 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1433 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1437 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1439 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1440 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1441 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1442 static unsigned long last_tsf_debug = 0;
1444 if (local->ops->get_tsf)
1445 tsf = local->ops->get_tsf(local_to_hw(local));
1448 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1449 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1450 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1452 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1453 (unsigned long long)tsf,
1454 (unsigned long long)timestamp,
1455 (unsigned long long)(tsf - timestamp),
1457 last_tsf_debug = jiffies;
1459 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1462 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1463 &elems) == ParseFailed)
1466 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1467 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1468 (sta = sta_info_get(local, mgmt->sa))) {
1469 struct ieee80211_hw_mode *mode;
1470 struct ieee80211_rate *rates;
1472 u32 supp_rates, prev_rates;
1475 mode = local->sta_scanning ?
1476 local->scan_hw_mode : local->oper_hw_mode;
1477 rates = mode->rates;
1478 num_rates = mode->num_rates;
1481 for (i = 0; i < elems.supp_rates_len +
1482 elems.ext_supp_rates_len; i++) {
1485 if (i < elems.supp_rates_len)
1486 rate = elems.supp_rates[i];
1487 else if (elems.ext_supp_rates)
1488 rate = elems.ext_supp_rates
1489 [i - elems.supp_rates_len];
1490 own_rate = 5 * (rate & 0x7f);
1491 if (mode->mode == MODE_ATHEROS_TURBO)
1493 for (j = 0; j < num_rates; j++)
1494 if (rates[j].rate == own_rate)
1495 supp_rates |= BIT(j);
1498 prev_rates = sta->supp_rates;
1499 sta->supp_rates &= supp_rates;
1500 if (sta->supp_rates == 0) {
1501 /* No matching rates - this should not really happen.
1502 * Make sure that at least one rate is marked
1503 * supported to avoid issues with TX rate ctrl. */
1504 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1506 if (sta->supp_rates != prev_rates) {
1507 printk(KERN_DEBUG "%s: updated supp_rates set for "
1508 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1510 dev->name, MAC_ARG(sta->addr), prev_rates,
1511 supp_rates, sta->supp_rates);
1519 if (elems.ds_params && elems.ds_params_len == 1)
1520 channel = elems.ds_params[0];
1522 channel = rx_status->channel;
1524 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1526 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1531 /* TODO: order by RSSI? */
1532 spin_lock_bh(&local->sta_bss_lock);
1533 list_move_tail(&bss->list, &local->sta_bss_list);
1534 spin_unlock_bh(&local->sta_bss_lock);
1538 if (bss->probe_resp && beacon) {
1539 /* Do not allow beacon to override data from Probe Response. */
1540 ieee80211_rx_bss_put(dev, bss);
1544 /* save the ERP value so that it is available at association time */
1545 if (elems.erp_info && elems.erp_info_len >= 1) {
1546 bss->erp_value = elems.erp_info[0];
1547 bss->has_erp_value = 1;
1550 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1551 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1552 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1553 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1554 bss->ssid_len = elems.ssid_len;
1557 bss->supp_rates_len = 0;
1558 if (elems.supp_rates) {
1559 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1560 if (clen > elems.supp_rates_len)
1561 clen = elems.supp_rates_len;
1562 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1564 bss->supp_rates_len += clen;
1566 if (elems.ext_supp_rates) {
1567 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1568 if (clen > elems.ext_supp_rates_len)
1569 clen = elems.ext_supp_rates_len;
1570 memcpy(&bss->supp_rates[bss->supp_rates_len],
1571 elems.ext_supp_rates, clen);
1572 bss->supp_rates_len += clen;
1576 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1577 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1579 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1581 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1582 bss->wpa_ie_len = elems.wpa_len + 2;
1584 bss->wpa_ie_len = 0;
1585 } else if (!elems.wpa && bss->wpa_ie) {
1588 bss->wpa_ie_len = 0;
1592 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1593 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1595 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1597 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1598 bss->rsn_ie_len = elems.rsn_len + 2;
1600 bss->rsn_ie_len = 0;
1601 } else if (!elems.rsn && bss->rsn_ie) {
1604 bss->rsn_ie_len = 0;
1607 if (elems.wmm_param &&
1608 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1609 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1611 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1613 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1614 elems.wmm_param_len + 2);
1615 bss->wmm_ie_len = elems.wmm_param_len + 2;
1617 bss->wmm_ie_len = 0;
1618 } else if (!elems.wmm_param && bss->wmm_ie) {
1621 bss->wmm_ie_len = 0;
1625 bss->hw_mode = rx_status->phymode;
1626 bss->channel = channel;
1627 bss->freq = rx_status->freq;
1628 if (channel != rx_status->channel &&
1629 (bss->hw_mode == MODE_IEEE80211G ||
1630 bss->hw_mode == MODE_IEEE80211B) &&
1631 channel >= 1 && channel <= 14) {
1632 static const int freq_list[] = {
1633 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1634 2447, 2452, 2457, 2462, 2467, 2472, 2484
1636 /* IEEE 802.11g/b mode can receive packets from neighboring
1637 * channels, so map the channel into frequency. */
1638 bss->freq = freq_list[channel - 1];
1640 bss->timestamp = timestamp;
1641 bss->last_update = jiffies;
1642 bss->rssi = rx_status->ssi;
1643 bss->signal = rx_status->signal;
1644 bss->noise = rx_status->noise;
1647 ieee80211_rx_bss_put(dev, bss);
1651 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1652 struct ieee80211_mgmt *mgmt,
1654 struct ieee80211_rx_status *rx_status)
1656 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1660 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1661 struct ieee80211_mgmt *mgmt,
1663 struct ieee80211_rx_status *rx_status)
1665 struct ieee80211_sub_if_data *sdata;
1666 struct ieee80211_if_sta *ifsta;
1668 struct ieee802_11_elems elems;
1670 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1672 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1673 if (sdata->type != IEEE80211_IF_TYPE_STA)
1675 ifsta = &sdata->u.sta;
1677 if (!ifsta->associated ||
1678 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1681 /* Process beacon from the current BSS */
1682 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1686 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1687 &elems) == ParseFailed)
1690 if (elems.erp_info && elems.erp_info_len >= 1)
1691 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
1693 if (elems.wmm_param && ifsta->wmm_enabled) {
1694 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1695 elems.wmm_param_len);
1700 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1701 struct ieee80211_if_sta *ifsta,
1702 struct ieee80211_mgmt *mgmt,
1704 struct ieee80211_rx_status *rx_status)
1706 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1707 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1709 struct sk_buff *skb;
1710 struct ieee80211_mgmt *resp;
1713 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1714 ifsta->state != IEEE80211_IBSS_JOINED ||
1715 len < 24 + 2 || !ifsta->probe_resp)
1718 if (local->ops->tx_last_beacon)
1719 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1723 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1724 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1725 MAC_FMT " (tx_last_beacon=%d)\n",
1726 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1727 MAC_ARG(mgmt->bssid), tx_last_beacon);
1728 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1730 if (!tx_last_beacon)
1733 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1734 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1737 end = ((u8 *) mgmt) + len;
1738 pos = mgmt->u.probe_req.variable;
1739 if (pos[0] != WLAN_EID_SSID ||
1740 pos + 2 + pos[1] > end) {
1741 if (net_ratelimit()) {
1742 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1743 "from " MAC_FMT "\n",
1744 dev->name, MAC_ARG(mgmt->sa));
1749 (pos[1] != ifsta->ssid_len ||
1750 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1751 /* Ignore ProbeReq for foreign SSID */
1755 /* Reply with ProbeResp */
1756 skb = skb_copy(ifsta->probe_resp, GFP_ATOMIC);
1760 resp = (struct ieee80211_mgmt *) skb->data;
1761 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1762 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1763 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1764 dev->name, MAC_ARG(resp->da));
1765 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1766 ieee80211_sta_tx(dev, skb, 0);
1770 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1771 struct ieee80211_rx_status *rx_status)
1773 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1774 struct ieee80211_sub_if_data *sdata;
1775 struct ieee80211_if_sta *ifsta;
1776 struct ieee80211_mgmt *mgmt;
1782 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1783 ifsta = &sdata->u.sta;
1785 mgmt = (struct ieee80211_mgmt *) skb->data;
1786 fc = le16_to_cpu(mgmt->frame_control);
1788 switch (fc & IEEE80211_FCTL_STYPE) {
1789 case IEEE80211_STYPE_PROBE_REQ:
1790 case IEEE80211_STYPE_PROBE_RESP:
1791 case IEEE80211_STYPE_BEACON:
1792 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1793 case IEEE80211_STYPE_AUTH:
1794 case IEEE80211_STYPE_ASSOC_RESP:
1795 case IEEE80211_STYPE_REASSOC_RESP:
1796 case IEEE80211_STYPE_DEAUTH:
1797 case IEEE80211_STYPE_DISASSOC:
1798 skb_queue_tail(&ifsta->skb_queue, skb);
1799 queue_work(local->hw.workqueue, &ifsta->work);
1802 printk(KERN_DEBUG "%s: received unknown management frame - "
1803 "stype=%d\n", dev->name,
1804 (fc & IEEE80211_FCTL_STYPE) >> 4);
1813 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1814 struct sk_buff *skb)
1816 struct ieee80211_rx_status *rx_status;
1817 struct ieee80211_sub_if_data *sdata;
1818 struct ieee80211_if_sta *ifsta;
1819 struct ieee80211_mgmt *mgmt;
1822 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1823 ifsta = &sdata->u.sta;
1825 rx_status = (struct ieee80211_rx_status *) skb->cb;
1826 mgmt = (struct ieee80211_mgmt *) skb->data;
1827 fc = le16_to_cpu(mgmt->frame_control);
1829 switch (fc & IEEE80211_FCTL_STYPE) {
1830 case IEEE80211_STYPE_PROBE_REQ:
1831 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1834 case IEEE80211_STYPE_PROBE_RESP:
1835 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1837 case IEEE80211_STYPE_BEACON:
1838 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1840 case IEEE80211_STYPE_AUTH:
1841 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1843 case IEEE80211_STYPE_ASSOC_RESP:
1844 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1846 case IEEE80211_STYPE_REASSOC_RESP:
1847 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1849 case IEEE80211_STYPE_DEAUTH:
1850 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1852 case IEEE80211_STYPE_DISASSOC:
1853 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1861 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1862 struct ieee80211_rx_status *rx_status)
1864 struct ieee80211_mgmt *mgmt;
1867 if (skb->len < 24) {
1872 mgmt = (struct ieee80211_mgmt *) skb->data;
1873 fc = le16_to_cpu(mgmt->frame_control);
1875 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1876 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1877 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1878 skb->len, rx_status);
1879 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1880 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1889 static int ieee80211_sta_active_ibss(struct net_device *dev)
1891 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1893 struct sta_info *sta;
1895 spin_lock_bh(&local->sta_lock);
1896 list_for_each_entry(sta, &local->sta_list, list) {
1897 if (sta->dev == dev &&
1898 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1904 spin_unlock_bh(&local->sta_lock);
1910 static void ieee80211_sta_expire(struct net_device *dev)
1912 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1913 struct sta_info *sta, *tmp;
1915 spin_lock_bh(&local->sta_lock);
1916 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1917 if (time_after(jiffies, sta->last_rx +
1918 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1919 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1920 "\n", dev->name, MAC_ARG(sta->addr));
1921 sta_info_free(sta, 1);
1923 spin_unlock_bh(&local->sta_lock);
1927 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1928 struct ieee80211_if_sta *ifsta)
1930 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1932 ieee80211_sta_expire(dev);
1933 if (ieee80211_sta_active_ibss(dev))
1936 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1937 "IBSS networks with same SSID (merge)\n", dev->name);
1938 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1942 void ieee80211_sta_timer(unsigned long data)
1944 struct ieee80211_sub_if_data *sdata =
1945 (struct ieee80211_sub_if_data *) data;
1946 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1947 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1949 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1950 queue_work(local->hw.workqueue, &ifsta->work);
1954 void ieee80211_sta_work(struct work_struct *work)
1956 struct ieee80211_sub_if_data *sdata =
1957 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1958 struct net_device *dev = sdata->dev;
1959 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1960 struct ieee80211_if_sta *ifsta;
1961 struct sk_buff *skb;
1963 if (!netif_running(dev))
1966 if (local->sta_scanning)
1969 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1970 sdata->type != IEEE80211_IF_TYPE_IBSS) {
1971 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
1972 "(type=%d)\n", dev->name, sdata->type);
1975 ifsta = &sdata->u.sta;
1977 while ((skb = skb_dequeue(&ifsta->skb_queue)))
1978 ieee80211_sta_rx_queued_mgmt(dev, skb);
1980 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1981 ifsta->state != IEEE80211_ASSOCIATE &&
1982 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
1983 ieee80211_sta_start_scan(dev, NULL, 0);
1987 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
1988 if (ieee80211_sta_config_auth(dev, ifsta))
1990 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1991 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
1994 switch (ifsta->state) {
1995 case IEEE80211_DISABLED:
1997 case IEEE80211_AUTHENTICATE:
1998 ieee80211_authenticate(dev, ifsta);
2000 case IEEE80211_ASSOCIATE:
2001 ieee80211_associate(dev, ifsta);
2003 case IEEE80211_ASSOCIATED:
2004 ieee80211_associated(dev, ifsta);
2006 case IEEE80211_IBSS_SEARCH:
2007 ieee80211_sta_find_ibss(dev, ifsta);
2009 case IEEE80211_IBSS_JOINED:
2010 ieee80211_sta_merge_ibss(dev, ifsta);
2013 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2018 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2019 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2020 "mixed-cell disabled - disassociate\n", dev->name);
2022 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2023 ieee80211_set_disassoc(dev, ifsta, 0);
2028 static void ieee80211_sta_reset_auth(struct net_device *dev,
2029 struct ieee80211_if_sta *ifsta)
2031 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2033 if (local->ops->reset_tsf) {
2034 /* Reset own TSF to allow time synchronization work. */
2035 local->ops->reset_tsf(local_to_hw(local));
2038 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2041 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2042 ifsta->auth_alg = WLAN_AUTH_OPEN;
2043 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2044 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2045 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2046 ifsta->auth_alg = WLAN_AUTH_LEAP;
2048 ifsta->auth_alg = WLAN_AUTH_OPEN;
2049 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2051 ifsta->auth_transaction = -1;
2052 ifsta->associated = ifsta->auth_tries = ifsta->assoc_tries = 0;
2053 netif_carrier_off(dev);
2057 void ieee80211_sta_req_auth(struct net_device *dev,
2058 struct ieee80211_if_sta *ifsta)
2060 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2061 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2063 if (sdata->type != IEEE80211_IF_TYPE_STA)
2066 if ((ifsta->bssid_set || ifsta->auto_bssid_sel) &&
2067 (ifsta->ssid_set || ifsta->auto_ssid_sel)) {
2068 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2069 queue_work(local->hw.workqueue, &ifsta->work);
2073 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2074 const char *ssid, int ssid_len)
2076 int tmp, hidden_ssid;
2078 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2081 if (ifsta->auto_bssid_sel)
2087 if (ssid[tmp] != '\0') {
2093 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2096 if (ssid_len == 1 && ssid[0] == ' ')
2102 static int ieee80211_sta_config_auth(struct net_device *dev,
2103 struct ieee80211_if_sta *ifsta)
2105 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2106 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2107 struct ieee80211_sta_bss *bss, *selected = NULL;
2108 int top_rssi = 0, freq;
2112 if (!ifsta->auto_channel_sel && !ifsta->auto_bssid_sel &&
2113 !ifsta->auto_ssid_sel) {
2114 ifsta->state = IEEE80211_AUTHENTICATE;
2116 ieee80211_sta_reset_auth(dev, ifsta);
2120 spin_lock_bh(&local->sta_bss_lock);
2121 freq = local->oper_channel->freq;
2122 list_for_each_entry(bss, &local->sta_bss_list, list) {
2123 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2126 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2127 !!sdata->default_key)
2130 if (!ifsta->auto_channel_sel && bss->freq != freq)
2133 if (!ifsta->auto_bssid_sel &&
2134 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2137 if (!ifsta->auto_ssid_sel &&
2138 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2141 if (!selected || top_rssi < bss->rssi) {
2143 top_rssi = bss->rssi;
2147 atomic_inc(&selected->users);
2148 spin_unlock_bh(&local->sta_bss_lock);
2151 ieee80211_set_channel(local, -1, selected->freq);
2152 if (!ifsta->ssid_set)
2153 ieee80211_sta_set_ssid(dev, selected->ssid,
2154 selected->ssid_len);
2155 ieee80211_sta_set_bssid(dev, selected->bssid);
2156 ieee80211_rx_bss_put(dev, selected);
2157 ifsta->state = IEEE80211_AUTHENTICATE;
2159 ieee80211_sta_reset_auth(dev, ifsta);
2162 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2163 ieee80211_sta_start_scan(dev, NULL, 0);
2164 ifsta->state = IEEE80211_AUTHENTICATE;
2165 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2167 ifsta->state = IEEE80211_DISABLED;
2173 static int ieee80211_sta_join_ibss(struct net_device *dev,
2174 struct ieee80211_if_sta *ifsta,
2175 struct ieee80211_sta_bss *bss)
2177 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2178 int res, rates, i, j;
2179 struct sk_buff *skb;
2180 struct ieee80211_mgmt *mgmt;
2181 struct ieee80211_tx_control control;
2182 struct ieee80211_rate *rate;
2183 struct ieee80211_hw_mode *mode;
2184 struct rate_control_extra extra;
2186 struct ieee80211_sub_if_data *sdata;
2188 /* Remove possible STA entries from other IBSS networks. */
2189 sta_info_flush(local, NULL);
2191 if (local->ops->reset_tsf) {
2192 /* Reset own TSF to allow time synchronization work. */
2193 local->ops->reset_tsf(local_to_hw(local));
2195 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2196 res = ieee80211_if_config(dev);
2200 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2202 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2203 sdata->drop_unencrypted = bss->capability &
2204 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2206 res = ieee80211_set_channel(local, -1, bss->freq);
2208 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2209 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2210 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2211 local->hw.conf.freq);
2215 /* Set beacon template based on scan results */
2216 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2221 skb_reserve(skb, local->hw.extra_tx_headroom);
2223 mgmt = (struct ieee80211_mgmt *)
2224 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2225 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2226 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2227 IEEE80211_STYPE_BEACON);
2228 memset(mgmt->da, 0xff, ETH_ALEN);
2229 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2230 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2231 mgmt->u.beacon.beacon_int =
2232 cpu_to_le16(local->hw.conf.beacon_int);
2233 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2235 pos = skb_put(skb, 2 + ifsta->ssid_len);
2236 *pos++ = WLAN_EID_SSID;
2237 *pos++ = ifsta->ssid_len;
2238 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2240 rates = bss->supp_rates_len;
2243 pos = skb_put(skb, 2 + rates);
2244 *pos++ = WLAN_EID_SUPP_RATES;
2246 memcpy(pos, bss->supp_rates, rates);
2248 pos = skb_put(skb, 2 + 1);
2249 *pos++ = WLAN_EID_DS_PARAMS;
2251 *pos++ = bss->channel;
2253 pos = skb_put(skb, 2 + 2);
2254 *pos++ = WLAN_EID_IBSS_PARAMS;
2256 /* FIX: set ATIM window based on scan results */
2260 if (bss->supp_rates_len > 8) {
2261 rates = bss->supp_rates_len - 8;
2262 pos = skb_put(skb, 2 + rates);
2263 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2265 memcpy(pos, &bss->supp_rates[8], rates);
2268 memset(&control, 0, sizeof(control));
2269 memset(&extra, 0, sizeof(extra));
2270 extra.mode = local->oper_hw_mode;
2271 rate = rate_control_get_rate(local, dev, skb, &extra);
2273 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2274 "for IBSS beacon\n", dev->name);
2277 control.tx_rate = (local->short_preamble &&
2278 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2279 rate->val2 : rate->val;
2280 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2281 control.power_level = local->hw.conf.power_level;
2282 control.flags |= IEEE80211_TXCTL_NO_ACK;
2283 control.retry_limit = 1;
2285 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2286 if (ifsta->probe_resp) {
2287 mgmt = (struct ieee80211_mgmt *)
2288 ifsta->probe_resp->data;
2289 mgmt->frame_control =
2290 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2291 IEEE80211_STYPE_PROBE_RESP);
2293 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2294 "template for IBSS\n", dev->name);
2297 if (local->ops->beacon_update &&
2298 local->ops->beacon_update(local_to_hw(local),
2299 skb, &control) == 0) {
2300 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2301 "template based on scan results\n", dev->name);
2306 mode = local->oper_hw_mode;
2307 for (i = 0; i < bss->supp_rates_len; i++) {
2308 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2309 if (mode->mode == MODE_ATHEROS_TURBO)
2311 for (j = 0; j < mode->num_rates; j++)
2312 if (mode->rates[j].rate == bitrate)
2315 ifsta->supp_rates_bits = rates;
2319 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2320 "template\n", dev->name);
2324 ifsta->state = IEEE80211_IBSS_JOINED;
2325 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2327 ieee80211_rx_bss_put(dev, bss);
2333 static int ieee80211_sta_create_ibss(struct net_device *dev,
2334 struct ieee80211_if_sta *ifsta)
2336 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2337 struct ieee80211_sta_bss *bss;
2338 struct ieee80211_sub_if_data *sdata;
2339 struct ieee80211_hw_mode *mode;
2340 u8 bssid[ETH_ALEN], *pos;
2344 /* Easier testing, use fixed BSSID. */
2345 memset(bssid, 0xfe, ETH_ALEN);
2347 /* Generate random, not broadcast, locally administered BSSID. Mix in
2348 * own MAC address to make sure that devices that do not have proper
2349 * random number generator get different BSSID. */
2350 get_random_bytes(bssid, ETH_ALEN);
2351 for (i = 0; i < ETH_ALEN; i++)
2352 bssid[i] ^= dev->dev_addr[i];
2357 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2358 dev->name, MAC_ARG(bssid));
2360 bss = ieee80211_rx_bss_add(dev, bssid);
2364 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2365 mode = local->oper_hw_mode;
2367 if (local->hw.conf.beacon_int == 0)
2368 local->hw.conf.beacon_int = 100;
2369 bss->beacon_int = local->hw.conf.beacon_int;
2370 bss->hw_mode = local->hw.conf.phymode;
2371 bss->channel = local->hw.conf.channel;
2372 bss->freq = local->hw.conf.freq;
2373 bss->last_update = jiffies;
2374 bss->capability = WLAN_CAPABILITY_IBSS;
2375 if (sdata->default_key) {
2376 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2378 sdata->drop_unencrypted = 0;
2379 bss->supp_rates_len = mode->num_rates;
2380 pos = bss->supp_rates;
2381 for (i = 0; i < mode->num_rates; i++) {
2382 int rate = mode->rates[i].rate;
2383 if (mode->mode == MODE_ATHEROS_TURBO)
2385 *pos++ = (u8) (rate / 5);
2388 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2392 static int ieee80211_sta_find_ibss(struct net_device *dev,
2393 struct ieee80211_if_sta *ifsta)
2395 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2396 struct ieee80211_sta_bss *bss;
2401 if (ifsta->ssid_len == 0)
2404 active_ibss = ieee80211_sta_active_ibss(dev);
2405 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2406 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2407 dev->name, active_ibss);
2408 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2409 spin_lock_bh(&local->sta_bss_lock);
2410 list_for_each_entry(bss, &local->sta_bss_list, list) {
2411 if (ifsta->ssid_len != bss->ssid_len ||
2412 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2413 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2415 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2416 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2417 MAC_ARG(bss->bssid));
2418 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2419 memcpy(bssid, bss->bssid, ETH_ALEN);
2421 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2424 spin_unlock_bh(&local->sta_bss_lock);
2426 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2427 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2428 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2429 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2430 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2431 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2432 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2433 " based on configured SSID\n",
2434 dev->name, MAC_ARG(bssid));
2435 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2437 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2438 printk(KERN_DEBUG " did not try to join ibss\n");
2439 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2441 /* Selected IBSS not found in current scan results - try to scan */
2442 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2443 !ieee80211_sta_active_ibss(dev)) {
2444 mod_timer(&ifsta->timer, jiffies +
2445 IEEE80211_IBSS_MERGE_INTERVAL);
2446 } else if (time_after(jiffies, local->last_scan_completed +
2447 IEEE80211_SCAN_INTERVAL)) {
2448 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2449 "join\n", dev->name);
2450 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2452 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2453 int interval = IEEE80211_SCAN_INTERVAL;
2455 if (time_after(jiffies, ifsta->ibss_join_req +
2456 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2457 if (ifsta->create_ibss &&
2458 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2459 return ieee80211_sta_create_ibss(dev, ifsta);
2460 if (ifsta->create_ibss) {
2461 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2462 " configured channel %d (%d MHz)\n",
2463 dev->name, local->hw.conf.channel,
2464 local->hw.conf.freq);
2467 /* No IBSS found - decrease scan interval and continue
2469 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2472 ifsta->state = IEEE80211_IBSS_SEARCH;
2473 mod_timer(&ifsta->timer, jiffies + interval);
2481 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2483 struct ieee80211_sub_if_data *sdata;
2484 struct ieee80211_if_sta *ifsta;
2485 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2487 if (len > IEEE80211_MAX_SSID_LEN)
2490 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2492 if (local->ops->conf_tx) {
2493 struct ieee80211_tx_queue_params qparam;
2496 memset(&qparam, 0, sizeof(qparam));
2497 /* TODO: are these ok defaults for all hw_modes? */
2500 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2501 qparam.cw_max = 1023;
2502 qparam.burst_time = 0;
2503 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2505 local->ops->conf_tx(local_to_hw(local),
2506 i + IEEE80211_TX_QUEUE_DATA0,
2509 /* IBSS uses different parameters for Beacon sending */
2513 local->ops->conf_tx(local_to_hw(local),
2514 IEEE80211_TX_QUEUE_BEACON, &qparam);
2517 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2518 ifsta = &sdata->u.sta;
2520 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2521 ifsta->prev_bssid_set = 0;
2522 memcpy(ifsta->ssid, ssid, len);
2523 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2524 ifsta->ssid_len = len;
2526 ifsta->ssid_set = len ? 1 : 0;
2527 if (sdata->type == IEEE80211_IF_TYPE_IBSS && !ifsta->bssid_set) {
2528 ifsta->ibss_join_req = jiffies;
2529 ifsta->state = IEEE80211_IBSS_SEARCH;
2530 return ieee80211_sta_find_ibss(dev, ifsta);
2536 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2538 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2539 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2540 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2541 *len = ifsta->ssid_len;
2546 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2548 struct ieee80211_sub_if_data *sdata;
2549 struct ieee80211_if_sta *ifsta;
2552 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2553 ifsta = &sdata->u.sta;
2555 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2556 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2557 res = ieee80211_if_config(dev);
2559 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2560 "the low-level driver\n", dev->name);
2565 if (!is_valid_ether_addr(bssid))
2566 ifsta->bssid_set = 0;
2568 ifsta->bssid_set = 1;
2573 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2574 struct ieee80211_sub_if_data *sdata,
2577 struct sk_buff *skb;
2578 struct ieee80211_hdr *nullfunc;
2581 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2583 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2584 "frame\n", sdata->dev->name);
2587 skb_reserve(skb, local->hw.extra_tx_headroom);
2589 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2590 memset(nullfunc, 0, 24);
2591 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2592 IEEE80211_FCTL_TODS;
2594 fc |= IEEE80211_FCTL_PM;
2595 nullfunc->frame_control = cpu_to_le16(fc);
2596 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2597 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2598 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2600 ieee80211_sta_tx(sdata->dev, skb, 0);
2604 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2606 struct ieee80211_local *local = hw_to_local(hw);
2607 struct net_device *dev = local->scan_dev;
2608 struct ieee80211_sub_if_data *sdata;
2609 union iwreq_data wrqu;
2611 local->last_scan_completed = jiffies;
2613 local->sta_scanning = 0;
2615 if (ieee80211_hw_config(local))
2616 printk(KERN_DEBUG "%s: failed to restore operational"
2617 "channel after scan\n", dev->name);
2619 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2620 ieee80211_if_config(dev))
2621 printk(KERN_DEBUG "%s: failed to restore operational"
2622 "BSSID after scan\n", dev->name);
2624 memset(&wrqu, 0, sizeof(wrqu));
2625 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2627 read_lock(&local->sub_if_lock);
2628 list_for_each_entry(sdata, &local->sub_if_list, list) {
2630 /* No need to wake the master device. */
2631 if (sdata->dev == local->mdev)
2634 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2635 if (sdata->u.sta.associated)
2636 ieee80211_send_nullfunc(local, sdata, 0);
2637 ieee80211_sta_timer((unsigned long)sdata);
2640 netif_wake_queue(sdata->dev);
2642 read_unlock(&local->sub_if_lock);
2644 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2645 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2646 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2647 if (!ifsta->bssid_set ||
2648 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2649 !ieee80211_sta_active_ibss(dev)))
2650 ieee80211_sta_find_ibss(dev, ifsta);
2653 EXPORT_SYMBOL(ieee80211_scan_completed);
2655 void ieee80211_sta_scan_work(struct work_struct *work)
2657 struct ieee80211_local *local =
2658 container_of(work, struct ieee80211_local, scan_work.work);
2659 struct net_device *dev = local->scan_dev;
2660 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2661 struct ieee80211_hw_mode *mode;
2662 struct ieee80211_channel *chan;
2664 unsigned long next_delay = 0;
2666 if (!local->sta_scanning)
2669 switch (local->scan_state) {
2670 case SCAN_SET_CHANNEL:
2671 mode = local->scan_hw_mode;
2672 if (local->scan_hw_mode->list.next == &local->modes_list &&
2673 local->scan_channel_idx >= mode->num_channels) {
2674 ieee80211_scan_completed(local_to_hw(local));
2677 skip = !(local->enabled_modes & (1 << mode->mode));
2678 chan = &mode->channels[local->scan_channel_idx];
2679 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2680 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2681 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2682 (local->hw_modes & local->enabled_modes &
2683 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2688 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2689 dev->name, chan->chan, chan->freq);
2692 local->scan_channel = chan;
2693 if (ieee80211_hw_config(local)) {
2694 printk(KERN_DEBUG "%s: failed to set channel "
2695 "%d (%d MHz) for scan\n", dev->name,
2696 chan->chan, chan->freq);
2701 local->scan_channel_idx++;
2702 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2703 if (local->scan_hw_mode->list.next != &local->modes_list) {
2704 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2705 struct ieee80211_hw_mode,
2707 local->scan_channel_idx = 0;
2714 next_delay = IEEE80211_PROBE_DELAY +
2715 usecs_to_jiffies(local->hw.channel_change_time);
2716 local->scan_state = SCAN_SEND_PROBE;
2718 case SCAN_SEND_PROBE:
2719 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2720 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2721 local->scan_ssid_len);
2722 next_delay = IEEE80211_CHANNEL_TIME;
2724 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2725 local->scan_state = SCAN_SET_CHANNEL;
2729 if (local->sta_scanning)
2730 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2735 static int ieee80211_sta_start_scan(struct net_device *dev,
2736 u8 *ssid, size_t ssid_len)
2738 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2739 struct ieee80211_sub_if_data *sdata;
2741 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2744 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2745 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2748 * ScanType: ACTIVE, PASSIVE
2749 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2750 * a Probe frame during active scanning
2752 * MinChannelTime (>= ProbeDelay), in TU
2753 * MaxChannelTime: (>= MinChannelTime), in TU
2756 /* MLME-SCAN.confirm
2758 * ResultCode: SUCCESS, INVALID_PARAMETERS
2761 if (local->sta_scanning) {
2762 if (local->scan_dev == dev)
2767 if (local->ops->hw_scan) {
2768 int rc = local->ops->hw_scan(local_to_hw(local),
2771 local->sta_scanning = 1;
2772 local->scan_dev = dev;
2777 local->sta_scanning = 1;
2779 read_lock(&local->sub_if_lock);
2780 list_for_each_entry(sdata, &local->sub_if_list, list) {
2782 /* Don't stop the master interface, otherwise we can't transmit
2784 if (sdata->dev == local->mdev)
2787 netif_stop_queue(sdata->dev);
2788 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2789 sdata->u.sta.associated)
2790 ieee80211_send_nullfunc(local, sdata, 1);
2792 read_unlock(&local->sub_if_lock);
2795 local->scan_ssid_len = ssid_len;
2796 memcpy(local->scan_ssid, ssid, ssid_len);
2798 local->scan_ssid_len = 0;
2799 local->scan_state = SCAN_SET_CHANNEL;
2800 local->scan_hw_mode = list_entry(local->modes_list.next,
2801 struct ieee80211_hw_mode,
2803 local->scan_channel_idx = 0;
2804 local->scan_dev = dev;
2806 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2807 ieee80211_if_config(dev))
2808 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2811 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2812 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2813 IEEE80211_CHANNEL_TIME);
2819 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2821 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2822 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2823 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2825 if (sdata->type != IEEE80211_IF_TYPE_STA)
2826 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2828 if (local->sta_scanning) {
2829 if (local->scan_dev == dev)
2834 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2835 queue_work(local->hw.workqueue, &ifsta->work);
2840 ieee80211_sta_scan_result(struct net_device *dev,
2841 struct ieee80211_sta_bss *bss,
2842 char *current_ev, char *end_buf)
2844 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2845 struct iw_event iwe;
2847 if (time_after(jiffies,
2848 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2851 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2854 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2855 !bss->wpa_ie && !bss->rsn_ie)
2858 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2859 (local->scan_ssid_len != bss->ssid_len ||
2860 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2863 memset(&iwe, 0, sizeof(iwe));
2864 iwe.cmd = SIOCGIWAP;
2865 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2866 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2867 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2870 memset(&iwe, 0, sizeof(iwe));
2871 iwe.cmd = SIOCGIWESSID;
2872 iwe.u.data.length = bss->ssid_len;
2873 iwe.u.data.flags = 1;
2874 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2877 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2878 memset(&iwe, 0, sizeof(iwe));
2879 iwe.cmd = SIOCGIWMODE;
2880 if (bss->capability & WLAN_CAPABILITY_ESS)
2881 iwe.u.mode = IW_MODE_MASTER;
2883 iwe.u.mode = IW_MODE_ADHOC;
2884 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2888 memset(&iwe, 0, sizeof(iwe));
2889 iwe.cmd = SIOCGIWFREQ;
2890 iwe.u.freq.m = bss->channel;
2892 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2894 iwe.u.freq.m = bss->freq * 100000;
2896 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2899 memset(&iwe, 0, sizeof(iwe));
2901 iwe.u.qual.qual = bss->signal;
2902 iwe.u.qual.level = bss->rssi;
2903 iwe.u.qual.noise = bss->noise;
2904 iwe.u.qual.updated = local->wstats_flags;
2905 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2908 memset(&iwe, 0, sizeof(iwe));
2909 iwe.cmd = SIOCGIWENCODE;
2910 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2911 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2913 iwe.u.data.flags = IW_ENCODE_DISABLED;
2914 iwe.u.data.length = 0;
2915 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2917 if (bss && bss->wpa_ie) {
2918 memset(&iwe, 0, sizeof(iwe));
2919 iwe.cmd = IWEVGENIE;
2920 iwe.u.data.length = bss->wpa_ie_len;
2921 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2925 if (bss && bss->rsn_ie) {
2926 memset(&iwe, 0, sizeof(iwe));
2927 iwe.cmd = IWEVGENIE;
2928 iwe.u.data.length = bss->rsn_ie_len;
2929 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2933 if (bss && bss->supp_rates_len > 0) {
2934 /* display all supported rates in readable format */
2935 char *p = current_ev + IW_EV_LCP_LEN;
2938 memset(&iwe, 0, sizeof(iwe));
2939 iwe.cmd = SIOCGIWRATE;
2940 /* Those two flags are ignored... */
2941 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2943 for (i = 0; i < bss->supp_rates_len; i++) {
2944 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2946 p = iwe_stream_add_value(current_ev, p,
2947 end_buf, &iwe, IW_EV_PARAM_LEN);
2954 buf = kmalloc(30, GFP_ATOMIC);
2956 memset(&iwe, 0, sizeof(iwe));
2957 iwe.cmd = IWEVCUSTOM;
2958 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
2959 iwe.u.data.length = strlen(buf);
2960 current_ev = iwe_stream_add_point(current_ev, end_buf,
2969 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
2972 buf = kmalloc(100, GFP_ATOMIC);
2976 memset(&iwe, 0, sizeof(iwe));
2977 iwe.cmd = IWEVCUSTOM;
2978 sprintf(buf, "bcn_int=%d", bss->beacon_int);
2979 iwe.u.data.length = strlen(buf);
2980 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2983 memset(&iwe, 0, sizeof(iwe));
2984 iwe.cmd = IWEVCUSTOM;
2985 sprintf(buf, "capab=0x%04x", bss->capability);
2986 iwe.u.data.length = strlen(buf);
2987 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2998 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3000 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3001 char *current_ev = buf;
3002 char *end_buf = buf + len;
3003 struct ieee80211_sta_bss *bss;
3005 spin_lock_bh(&local->sta_bss_lock);
3006 list_for_each_entry(bss, &local->sta_bss_list, list) {
3007 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3008 spin_unlock_bh(&local->sta_bss_lock);
3011 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3014 spin_unlock_bh(&local->sta_bss_lock);
3015 return current_ev - buf;
3019 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3021 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3022 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3023 kfree(ifsta->extra_ie);
3025 ifsta->extra_ie = NULL;
3026 ifsta->extra_ie_len = 0;
3029 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3030 if (!ifsta->extra_ie) {
3031 ifsta->extra_ie_len = 0;
3034 memcpy(ifsta->extra_ie, ie, len);
3035 ifsta->extra_ie_len = len;
3040 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3041 struct sk_buff *skb, u8 *bssid,
3044 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3045 struct sta_info *sta;
3046 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3048 /* TODO: Could consider removing the least recently used entry and
3049 * allow new one to be added. */
3050 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3051 if (net_ratelimit()) {
3052 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3053 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3058 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3059 local->mdev->name, MAC_ARG(addr), dev->name);
3061 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3065 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3067 rate_control_rate_init(sta, local);
3069 return sta; /* caller will call sta_info_put() */
3073 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3075 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3076 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3078 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3081 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3082 sdata->type != IEEE80211_IF_TYPE_IBSS)
3085 ieee80211_send_deauth(dev, ifsta, reason);
3086 ieee80211_set_disassoc(dev, ifsta, 1);
3091 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3093 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3094 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3096 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3099 if (sdata->type != IEEE80211_IF_TYPE_STA)
3102 if (!ifsta->associated)
3105 ieee80211_send_disassoc(dev, ifsta, reason);
3106 ieee80211_set_disassoc(dev, ifsta, 0);