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/if_ether.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/wireless.h>
25 #include <linux/random.h>
26 #include <linux/etherdevice.h>
27 #include <linux/rtnetlink.h>
28 #include <net/iw_handler.h>
29 #include <asm/types.h>
30 #include <asm/delay.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 {
102 u8 ext_supp_rates_len;
109 typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
112 static ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
113 struct ieee802_11_elems *elems)
119 memset(elems, 0, sizeof(*elems));
131 printk(KERN_DEBUG "IEEE 802.11 element parse "
132 "failed (id=%d elen=%d left=%d)\n",
141 elems->ssid_len = elen;
143 case WLAN_EID_SUPP_RATES:
144 elems->supp_rates = pos;
145 elems->supp_rates_len = elen;
147 case WLAN_EID_FH_PARAMS:
148 elems->fh_params = pos;
149 elems->fh_params_len = elen;
151 case WLAN_EID_DS_PARAMS:
152 elems->ds_params = pos;
153 elems->ds_params_len = elen;
155 case WLAN_EID_CF_PARAMS:
156 elems->cf_params = pos;
157 elems->cf_params_len = elen;
161 elems->tim_len = elen;
163 case WLAN_EID_IBSS_PARAMS:
164 elems->ibss_params = pos;
165 elems->ibss_params_len = elen;
167 case WLAN_EID_CHALLENGE:
168 elems->challenge = pos;
169 elems->challenge_len = elen;
172 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
174 /* Microsoft OUI (00:50:F2) */
176 /* OUI Type 1 - WPA IE */
178 elems->wpa_len = elen;
179 } else if (elen >= 5 && pos[3] == 2) {
181 elems->wmm_info = pos;
182 elems->wmm_info_len = elen;
183 } else if (pos[4] == 1) {
184 elems->wmm_param = pos;
185 elems->wmm_param_len = elen;
192 elems->rsn_len = elen;
194 case WLAN_EID_ERP_INFO:
195 elems->erp_info = pos;
196 elems->erp_info_len = elen;
198 case WLAN_EID_EXT_SUPP_RATES:
199 elems->ext_supp_rates = pos;
200 elems->ext_supp_rates_len = elen;
204 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
205 "unknown element (id=%d elen=%d)\n",
216 /* Do not trigger error if left == 1 as Apple Airport base stations
217 * send AssocResps that are one spurious byte too long. */
219 return unknown ? ParseUnknown : ParseOK;
225 static int ecw2cw(int ecw)
236 static void ieee80211_sta_wmm_params(struct net_device *dev,
237 struct ieee80211_if_sta *ifsta,
238 u8 *wmm_param, size_t wmm_param_len)
240 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
241 struct ieee80211_tx_queue_params params;
246 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
248 count = wmm_param[6] & 0x0f;
249 if (count == ifsta->wmm_last_param_set)
251 ifsta->wmm_last_param_set = count;
254 left = wmm_param_len - 8;
256 memset(¶ms, 0, sizeof(params));
258 if (!local->ops->conf_tx)
262 for (; left >= 4; left -= 4, pos += 4) {
263 int aci = (pos[0] >> 5) & 0x03;
264 int acm = (pos[0] >> 4) & 0x01;
269 queue = IEEE80211_TX_QUEUE_DATA3;
271 local->wmm_acm |= BIT(0) | BIT(3);
275 queue = IEEE80211_TX_QUEUE_DATA1;
277 local->wmm_acm |= BIT(4) | BIT(5);
281 queue = IEEE80211_TX_QUEUE_DATA0;
283 local->wmm_acm |= BIT(6) | BIT(7);
288 queue = IEEE80211_TX_QUEUE_DATA2;
290 local->wmm_acm |= BIT(1) | BIT(2);
295 params.aifs = pos[0] & 0x0f;
296 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
297 params.cw_min = ecw2cw(pos[1] & 0x0f);
298 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
299 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
300 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
301 "cWmin=%d cWmax=%d burst=%d\n",
302 dev->name, queue, aci, acm, params.aifs, params.cw_min,
303 params.cw_max, params.burst_time);
304 /* TODO: handle ACM (block TX, fallback to next lowest allowed
306 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
307 printk(KERN_DEBUG "%s: failed to set TX queue "
308 "parameters for queue %d\n", dev->name, queue);
314 static void ieee80211_sta_send_associnfo(struct net_device *dev,
315 struct ieee80211_if_sta *ifsta)
320 union iwreq_data wrqu;
322 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
325 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
326 ifsta->assocresp_ies_len), GFP_ATOMIC);
330 len = sprintf(buf, "ASSOCINFO(");
331 if (ifsta->assocreq_ies) {
332 len += sprintf(buf + len, "ReqIEs=");
333 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
334 len += sprintf(buf + len, "%02x",
335 ifsta->assocreq_ies[i]);
338 if (ifsta->assocresp_ies) {
339 if (ifsta->assocreq_ies)
340 len += sprintf(buf + len, " ");
341 len += sprintf(buf + len, "RespIEs=");
342 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
343 len += sprintf(buf + len, "%02x",
344 ifsta->assocresp_ies[i]);
347 len += sprintf(buf + len, ")");
349 if (len > IW_CUSTOM_MAX) {
350 len = sprintf(buf, "ASSOCRESPIE=");
351 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
352 len += sprintf(buf + len, "%02x",
353 ifsta->assocresp_ies[i]);
357 memset(&wrqu, 0, sizeof(wrqu));
358 wrqu.data.length = len;
359 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
365 static void ieee80211_set_associated(struct net_device *dev,
366 struct ieee80211_if_sta *ifsta, int assoc)
368 union iwreq_data wrqu;
370 if (ifsta->associated == assoc)
373 ifsta->associated = assoc;
376 struct ieee80211_sub_if_data *sdata;
377 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
378 if (sdata->type != IEEE80211_IF_TYPE_STA)
380 netif_carrier_on(dev);
381 ifsta->prev_bssid_set = 1;
382 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
383 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
384 ieee80211_sta_send_associnfo(dev, ifsta);
386 netif_carrier_off(dev);
387 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
389 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
390 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
391 ifsta->last_probe = jiffies;
394 static void ieee80211_set_disassoc(struct net_device *dev,
395 struct ieee80211_if_sta *ifsta, int deauth)
398 ifsta->auth_tries = 0;
399 ifsta->assoc_tries = 0;
400 ieee80211_set_associated(dev, ifsta, 0);
403 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
406 struct ieee80211_sub_if_data *sdata;
407 struct ieee80211_tx_packet_data *pkt_data;
409 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
410 skb->dev = sdata->local->mdev;
411 skb_set_mac_header(skb, 0);
412 skb_set_network_header(skb, 0);
413 skb_set_transport_header(skb, 0);
415 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
416 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
417 pkt_data->ifindex = sdata->dev->ifindex;
418 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
419 pkt_data->do_not_encrypt = !encrypt;
425 static void ieee80211_send_auth(struct net_device *dev,
426 struct ieee80211_if_sta *ifsta,
427 int transaction, u8 *extra, size_t extra_len,
430 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
432 struct ieee80211_mgmt *mgmt;
434 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
435 sizeof(*mgmt) + 6 + extra_len);
437 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
438 "frame\n", dev->name);
441 skb_reserve(skb, local->hw.extra_tx_headroom);
443 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
444 memset(mgmt, 0, 24 + 6);
445 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
446 IEEE80211_STYPE_AUTH);
448 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
449 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
450 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
451 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
452 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
453 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
454 ifsta->auth_transaction = transaction + 1;
455 mgmt->u.auth.status_code = cpu_to_le16(0);
457 memcpy(skb_put(skb, extra_len), extra, extra_len);
459 ieee80211_sta_tx(dev, skb, encrypt);
463 static void ieee80211_authenticate(struct net_device *dev,
464 struct ieee80211_if_sta *ifsta)
467 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
468 printk(KERN_DEBUG "%s: authentication with AP " MAC_FMT
470 dev->name, MAC_ARG(ifsta->bssid));
471 ifsta->state = IEEE80211_DISABLED;
475 ifsta->state = IEEE80211_AUTHENTICATE;
476 printk(KERN_DEBUG "%s: authenticate with AP " MAC_FMT "\n",
477 dev->name, MAC_ARG(ifsta->bssid));
479 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
481 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
485 static void ieee80211_send_assoc(struct net_device *dev,
486 struct ieee80211_if_sta *ifsta)
488 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
489 struct ieee80211_hw_mode *mode;
491 struct ieee80211_mgmt *mgmt;
495 struct ieee80211_sta_bss *bss;
498 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
499 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
502 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
503 "frame\n", dev->name);
506 skb_reserve(skb, local->hw.extra_tx_headroom);
508 mode = local->oper_hw_mode;
509 capab = ifsta->capab;
510 if (mode->mode == MODE_IEEE80211G) {
511 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
512 WLAN_CAPABILITY_SHORT_PREAMBLE;
514 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
516 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
517 capab |= WLAN_CAPABILITY_PRIVACY;
521 ieee80211_rx_bss_put(dev, bss);
524 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
526 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
527 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
528 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
530 if (ifsta->prev_bssid_set) {
532 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
533 IEEE80211_STYPE_REASSOC_REQ);
534 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
535 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
536 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
540 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
541 IEEE80211_STYPE_ASSOC_REQ);
542 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
543 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
547 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
548 *pos++ = WLAN_EID_SSID;
549 *pos++ = ifsta->ssid_len;
550 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
552 len = mode->num_rates;
555 pos = skb_put(skb, len + 2);
556 *pos++ = WLAN_EID_SUPP_RATES;
558 for (i = 0; i < len; i++) {
559 int rate = mode->rates[i].rate;
560 if (mode->mode == MODE_ATHEROS_TURBO)
562 *pos++ = (u8) (rate / 5);
565 if (mode->num_rates > len) {
566 pos = skb_put(skb, mode->num_rates - len + 2);
567 *pos++ = WLAN_EID_EXT_SUPP_RATES;
568 *pos++ = mode->num_rates - len;
569 for (i = len; i < mode->num_rates; i++) {
570 int rate = mode->rates[i].rate;
571 if (mode->mode == MODE_ATHEROS_TURBO)
573 *pos++ = (u8) (rate / 5);
577 if (ifsta->extra_ie) {
578 pos = skb_put(skb, ifsta->extra_ie_len);
579 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
582 if (wmm && ifsta->wmm_enabled) {
583 pos = skb_put(skb, 9);
584 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
585 *pos++ = 7; /* len */
586 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
589 *pos++ = 2; /* WME */
590 *pos++ = 0; /* WME info */
591 *pos++ = 1; /* WME ver */
595 kfree(ifsta->assocreq_ies);
596 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
597 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_ATOMIC);
598 if (ifsta->assocreq_ies)
599 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
601 ieee80211_sta_tx(dev, skb, 0);
605 static void ieee80211_send_deauth(struct net_device *dev,
606 struct ieee80211_if_sta *ifsta, u16 reason)
608 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
610 struct ieee80211_mgmt *mgmt;
612 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
614 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
615 "frame\n", dev->name);
618 skb_reserve(skb, local->hw.extra_tx_headroom);
620 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
622 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
623 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
624 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
625 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
626 IEEE80211_STYPE_DEAUTH);
628 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
630 ieee80211_sta_tx(dev, skb, 0);
634 static void ieee80211_send_disassoc(struct net_device *dev,
635 struct ieee80211_if_sta *ifsta, u16 reason)
637 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
639 struct ieee80211_mgmt *mgmt;
641 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
643 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
644 "frame\n", dev->name);
647 skb_reserve(skb, local->hw.extra_tx_headroom);
649 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
651 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
652 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
653 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
654 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
655 IEEE80211_STYPE_DISASSOC);
657 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
659 ieee80211_sta_tx(dev, skb, 0);
663 static int ieee80211_privacy_mismatch(struct net_device *dev,
664 struct ieee80211_if_sta *ifsta)
666 struct ieee80211_sta_bss *bss;
669 if (!ifsta || ifsta->mixed_cell ||
670 ifsta->key_mgmt != IEEE80211_KEY_MGMT_NONE)
673 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
677 if (ieee80211_sta_wep_configured(dev) !=
678 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
681 ieee80211_rx_bss_put(dev, bss);
687 static void ieee80211_associate(struct net_device *dev,
688 struct ieee80211_if_sta *ifsta)
690 ifsta->assoc_tries++;
691 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
692 printk(KERN_DEBUG "%s: association with AP " MAC_FMT
694 dev->name, MAC_ARG(ifsta->bssid));
695 ifsta->state = IEEE80211_DISABLED;
699 ifsta->state = IEEE80211_ASSOCIATE;
700 printk(KERN_DEBUG "%s: associate with AP " MAC_FMT "\n",
701 dev->name, MAC_ARG(ifsta->bssid));
702 if (ieee80211_privacy_mismatch(dev, ifsta)) {
703 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
704 "mixed-cell disabled - abort association\n", dev->name);
705 ifsta->state = IEEE80211_DISABLED;
709 ieee80211_send_assoc(dev, ifsta);
711 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
715 static void ieee80211_associated(struct net_device *dev,
716 struct ieee80211_if_sta *ifsta)
718 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
719 struct sta_info *sta;
722 /* TODO: start monitoring current AP signal quality and number of
723 * missed beacons. Scan other channels every now and then and search
725 /* TODO: remove expired BSSes */
727 ifsta->state = IEEE80211_ASSOCIATED;
729 sta = sta_info_get(local, ifsta->bssid);
731 printk(KERN_DEBUG "%s: No STA entry for own AP " MAC_FMT "\n",
732 dev->name, MAC_ARG(ifsta->bssid));
736 if (time_after(jiffies,
737 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
738 if (ifsta->probereq_poll) {
739 printk(KERN_DEBUG "%s: No ProbeResp from "
740 "current AP " MAC_FMT " - assume out of "
742 dev->name, MAC_ARG(ifsta->bssid));
744 sta_info_free(sta, 0);
745 ifsta->probereq_poll = 0;
747 ieee80211_send_probe_req(dev, ifsta->bssid,
749 local->scan_ssid_len);
750 ifsta->probereq_poll = 1;
753 ifsta->probereq_poll = 0;
754 if (time_after(jiffies, ifsta->last_probe +
755 IEEE80211_PROBE_INTERVAL)) {
756 ifsta->last_probe = jiffies;
757 ieee80211_send_probe_req(dev, ifsta->bssid,
765 union iwreq_data wrqu;
766 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
767 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
768 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
769 mod_timer(&ifsta->timer, jiffies +
770 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
772 mod_timer(&ifsta->timer, jiffies +
773 IEEE80211_MONITORING_INTERVAL);
778 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
779 u8 *ssid, size_t ssid_len)
781 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
782 struct ieee80211_hw_mode *mode;
784 struct ieee80211_mgmt *mgmt;
785 u8 *pos, *supp_rates, *esupp_rates = NULL;
788 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
790 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
791 "request\n", dev->name);
794 skb_reserve(skb, local->hw.extra_tx_headroom);
796 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
798 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
799 IEEE80211_STYPE_PROBE_REQ);
800 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
802 memcpy(mgmt->da, dst, ETH_ALEN);
803 memcpy(mgmt->bssid, dst, ETH_ALEN);
805 memset(mgmt->da, 0xff, ETH_ALEN);
806 memset(mgmt->bssid, 0xff, ETH_ALEN);
808 pos = skb_put(skb, 2 + ssid_len);
809 *pos++ = WLAN_EID_SSID;
811 memcpy(pos, ssid, ssid_len);
813 supp_rates = skb_put(skb, 2);
814 supp_rates[0] = WLAN_EID_SUPP_RATES;
816 mode = local->oper_hw_mode;
817 for (i = 0; i < mode->num_rates; i++) {
818 struct ieee80211_rate *rate = &mode->rates[i];
819 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
822 pos = skb_put(skb, 1);
824 } else if (supp_rates[1] == 8) {
825 esupp_rates = skb_put(skb, 3);
826 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
828 pos = &esupp_rates[2];
830 pos = skb_put(skb, 1);
833 if (mode->mode == MODE_ATHEROS_TURBO)
834 *pos = rate->rate / 10;
836 *pos = rate->rate / 5;
839 ieee80211_sta_tx(dev, skb, 0);
843 static int ieee80211_sta_wep_configured(struct net_device *dev)
845 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
846 if (!sdata || !sdata->default_key ||
847 sdata->default_key->alg != ALG_WEP)
853 static void ieee80211_auth_completed(struct net_device *dev,
854 struct ieee80211_if_sta *ifsta)
856 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
857 ifsta->authenticated = 1;
858 ieee80211_associate(dev, ifsta);
862 static void ieee80211_auth_challenge(struct net_device *dev,
863 struct ieee80211_if_sta *ifsta,
864 struct ieee80211_mgmt *mgmt,
868 struct ieee802_11_elems elems;
870 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
871 pos = mgmt->u.auth.variable;
872 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
874 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
878 if (!elems.challenge) {
879 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
880 "frame\n", dev->name);
883 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
884 elems.challenge_len + 2, 1);
888 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
889 struct ieee80211_if_sta *ifsta,
890 struct ieee80211_mgmt *mgmt,
893 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
894 u16 auth_alg, auth_transaction, status_code;
896 if (ifsta->state != IEEE80211_AUTHENTICATE &&
897 sdata->type != IEEE80211_IF_TYPE_IBSS) {
898 printk(KERN_DEBUG "%s: authentication frame received from "
899 MAC_FMT ", but not in authenticate state - ignored\n",
900 dev->name, MAC_ARG(mgmt->sa));
905 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
906 "received from " MAC_FMT " - ignored\n",
907 dev->name, len, MAC_ARG(mgmt->sa));
911 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
912 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
913 printk(KERN_DEBUG "%s: authentication frame received from "
914 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
915 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
916 MAC_ARG(mgmt->bssid));
920 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
921 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
922 printk(KERN_DEBUG "%s: authentication frame received from "
923 "unknown BSSID (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
924 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
925 MAC_ARG(mgmt->bssid));
929 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
930 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
931 status_code = le16_to_cpu(mgmt->u.auth.status_code);
933 printk(KERN_DEBUG "%s: RX authentication from " MAC_FMT " (alg=%d "
934 "transaction=%d status=%d)\n",
935 dev->name, MAC_ARG(mgmt->sa), auth_alg,
936 auth_transaction, status_code);
938 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
939 /* IEEE 802.11 standard does not require authentication in IBSS
940 * networks and most implementations do not seem to use it.
941 * However, try to reply to authentication attempts if someone
942 * has actually implemented this.
943 * TODO: Could implement shared key authentication. */
944 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
945 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
946 "frame (alg=%d transaction=%d)\n",
947 dev->name, auth_alg, auth_transaction);
950 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
953 if (auth_alg != ifsta->auth_alg ||
954 auth_transaction != ifsta->auth_transaction) {
955 printk(KERN_DEBUG "%s: unexpected authentication frame "
956 "(alg=%d transaction=%d)\n",
957 dev->name, auth_alg, auth_transaction);
961 if (status_code != WLAN_STATUS_SUCCESS) {
962 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
963 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
964 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
966 const int num_algs = ARRAY_SIZE(algs);
968 algs[0] = algs[1] = algs[2] = 0xff;
969 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
970 algs[0] = WLAN_AUTH_OPEN;
971 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
972 algs[1] = WLAN_AUTH_SHARED_KEY;
973 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
974 algs[2] = WLAN_AUTH_LEAP;
975 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
977 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
981 for (i = 0; i < num_algs; i++) {
985 if (algs[pos] == ifsta->auth_alg ||
988 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
989 !ieee80211_sta_wep_configured(dev))
991 ifsta->auth_alg = algs[pos];
992 printk(KERN_DEBUG "%s: set auth_alg=%d for "
994 dev->name, ifsta->auth_alg);
1001 switch (ifsta->auth_alg) {
1002 case WLAN_AUTH_OPEN:
1003 case WLAN_AUTH_LEAP:
1004 ieee80211_auth_completed(dev, ifsta);
1006 case WLAN_AUTH_SHARED_KEY:
1007 if (ifsta->auth_transaction == 4)
1008 ieee80211_auth_completed(dev, ifsta);
1010 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1016 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1017 struct ieee80211_if_sta *ifsta,
1018 struct ieee80211_mgmt *mgmt,
1024 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1025 "received from " MAC_FMT " - ignored\n",
1026 dev->name, len, MAC_ARG(mgmt->sa));
1030 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1031 printk(KERN_DEBUG "%s: deauthentication frame received from "
1032 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1033 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1034 MAC_ARG(mgmt->bssid));
1038 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1040 printk(KERN_DEBUG "%s: RX deauthentication from " MAC_FMT
1042 dev->name, MAC_ARG(mgmt->sa), reason_code);
1044 if (ifsta->authenticated) {
1045 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1048 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1049 ifsta->state == IEEE80211_ASSOCIATE ||
1050 ifsta->state == IEEE80211_ASSOCIATED) {
1051 ifsta->state = IEEE80211_AUTHENTICATE;
1052 mod_timer(&ifsta->timer, jiffies +
1053 IEEE80211_RETRY_AUTH_INTERVAL);
1056 ieee80211_set_disassoc(dev, ifsta, 1);
1057 ifsta->authenticated = 0;
1061 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1062 struct ieee80211_if_sta *ifsta,
1063 struct ieee80211_mgmt *mgmt,
1069 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1070 "received from " MAC_FMT " - ignored\n",
1071 dev->name, len, MAC_ARG(mgmt->sa));
1075 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1076 printk(KERN_DEBUG "%s: disassociation frame received from "
1077 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1078 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1079 MAC_ARG(mgmt->bssid));
1083 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1085 printk(KERN_DEBUG "%s: RX disassociation from " MAC_FMT
1087 dev->name, MAC_ARG(mgmt->sa), reason_code);
1089 if (ifsta->associated)
1090 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1092 if (ifsta->state == IEEE80211_ASSOCIATED) {
1093 ifsta->state = IEEE80211_ASSOCIATE;
1094 mod_timer(&ifsta->timer, jiffies +
1095 IEEE80211_RETRY_AUTH_INTERVAL);
1098 ieee80211_set_disassoc(dev, ifsta, 0);
1102 static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1103 struct ieee80211_if_sta *ifsta,
1104 struct ieee80211_mgmt *mgmt,
1108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1109 struct ieee80211_hw_mode *mode;
1110 struct sta_info *sta;
1112 u16 capab_info, status_code, aid;
1113 struct ieee802_11_elems elems;
1117 /* AssocResp and ReassocResp have identical structure, so process both
1118 * of them in this function. */
1120 if (ifsta->state != IEEE80211_ASSOCIATE) {
1121 printk(KERN_DEBUG "%s: association frame received from "
1122 MAC_FMT ", but not in associate state - ignored\n",
1123 dev->name, MAC_ARG(mgmt->sa));
1128 printk(KERN_DEBUG "%s: too short (%zd) association 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: association 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 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1143 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1144 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1145 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1146 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1147 "set\n", dev->name, aid);
1148 aid &= ~(BIT(15) | BIT(14));
1150 printk(KERN_DEBUG "%s: RX %sssocResp from " MAC_FMT " (capab=0x%x "
1151 "status=%d aid=%d)\n",
1152 dev->name, reassoc ? "Rea" : "A", MAC_ARG(mgmt->sa),
1153 capab_info, status_code, aid);
1155 if (status_code != WLAN_STATUS_SUCCESS) {
1156 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1157 dev->name, status_code);
1161 pos = mgmt->u.assoc_resp.variable;
1162 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1164 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1169 if (!elems.supp_rates) {
1170 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1175 printk(KERN_DEBUG "%s: associated\n", dev->name);
1177 ifsta->ap_capab = capab_info;
1179 kfree(ifsta->assocresp_ies);
1180 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1181 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_ATOMIC);
1182 if (ifsta->assocresp_ies)
1183 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1185 ieee80211_set_associated(dev, ifsta, 1);
1187 /* Add STA entry for the AP */
1188 sta = sta_info_get(local, ifsta->bssid);
1190 struct ieee80211_sta_bss *bss;
1191 sta = sta_info_add(local, dev, ifsta->bssid, GFP_ATOMIC);
1193 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1194 " AP\n", dev->name);
1197 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1199 sta->last_rssi = bss->rssi;
1200 sta->last_signal = bss->signal;
1201 sta->last_noise = bss->noise;
1202 ieee80211_rx_bss_put(dev, bss);
1207 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
1211 mode = local->oper_hw_mode;
1212 for (i = 0; i < elems.supp_rates_len; i++) {
1213 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1214 if (mode->mode == MODE_ATHEROS_TURBO)
1216 for (j = 0; j < mode->num_rates; j++)
1217 if (mode->rates[j].rate == rate)
1220 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1221 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1222 if (mode->mode == MODE_ATHEROS_TURBO)
1224 for (j = 0; j < mode->num_rates; j++)
1225 if (mode->rates[j].rate == rate)
1228 sta->supp_rates = rates;
1230 rate_control_rate_init(sta, local);
1232 if (elems.wmm_param && ifsta->wmm_enabled) {
1233 sta->flags |= WLAN_STA_WME;
1234 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1235 elems.wmm_param_len);
1241 ieee80211_associated(dev, ifsta);
1245 /* Caller must hold local->sta_bss_lock */
1246 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1247 struct ieee80211_sta_bss *bss)
1249 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1250 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1251 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1255 /* Caller must hold local->sta_bss_lock */
1256 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1257 struct ieee80211_sta_bss *bss)
1259 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1260 struct ieee80211_sta_bss *b, *prev = NULL;
1261 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1265 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1268 prev->hnext = bss->hnext;
1277 static struct ieee80211_sta_bss *
1278 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1280 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1281 struct ieee80211_sta_bss *bss;
1283 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1286 memset(bss, 0, sizeof(*bss));
1287 atomic_inc(&bss->users);
1288 atomic_inc(&bss->users);
1289 memcpy(bss->bssid, bssid, ETH_ALEN);
1291 spin_lock_bh(&local->sta_bss_lock);
1292 /* TODO: order by RSSI? */
1293 list_add_tail(&bss->list, &local->sta_bss_list);
1294 __ieee80211_rx_bss_hash_add(dev, bss);
1295 spin_unlock_bh(&local->sta_bss_lock);
1300 static struct ieee80211_sta_bss *
1301 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1303 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1304 struct ieee80211_sta_bss *bss;
1306 spin_lock_bh(&local->sta_bss_lock);
1307 bss = local->sta_bss_hash[STA_HASH(bssid)];
1309 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1310 atomic_inc(&bss->users);
1315 spin_unlock_bh(&local->sta_bss_lock);
1320 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1329 static void ieee80211_rx_bss_put(struct net_device *dev,
1330 struct ieee80211_sta_bss *bss)
1332 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1333 if (!atomic_dec_and_test(&bss->users))
1336 spin_lock_bh(&local->sta_bss_lock);
1337 __ieee80211_rx_bss_hash_del(dev, bss);
1338 list_del(&bss->list);
1339 spin_unlock_bh(&local->sta_bss_lock);
1340 ieee80211_rx_bss_free(bss);
1344 void ieee80211_rx_bss_list_init(struct net_device *dev)
1346 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1347 spin_lock_init(&local->sta_bss_lock);
1348 INIT_LIST_HEAD(&local->sta_bss_list);
1352 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1354 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1355 struct ieee80211_sta_bss *bss, *tmp;
1357 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1358 ieee80211_rx_bss_put(dev, bss);
1362 static void ieee80211_rx_bss_info(struct net_device *dev,
1363 struct ieee80211_mgmt *mgmt,
1365 struct ieee80211_rx_status *rx_status,
1368 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1369 struct ieee802_11_elems elems;
1371 int channel, invalid = 0, clen;
1372 struct ieee80211_sta_bss *bss;
1373 struct sta_info *sta;
1374 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1377 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1378 return; /* ignore ProbeResp to foreign address */
1381 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1382 dev->name, beacon ? "Beacon" : "Probe Response",
1383 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1386 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1390 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1392 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1393 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1394 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1395 static unsigned long last_tsf_debug = 0;
1397 if (local->ops->get_tsf)
1398 tsf = local->ops->get_tsf(local_to_hw(local));
1401 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1402 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1403 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1405 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1406 (unsigned long long)tsf,
1407 (unsigned long long)timestamp,
1408 (unsigned long long)(tsf - timestamp),
1410 last_tsf_debug = jiffies;
1412 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1415 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1416 &elems) == ParseFailed)
1419 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1420 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1421 (sta = sta_info_get(local, mgmt->sa))) {
1422 struct ieee80211_hw_mode *mode;
1423 struct ieee80211_rate *rates;
1425 u32 supp_rates, prev_rates;
1428 mode = local->sta_scanning ?
1429 local->scan_hw_mode : local->oper_hw_mode;
1430 rates = mode->rates;
1431 num_rates = mode->num_rates;
1434 for (i = 0; i < elems.supp_rates_len +
1435 elems.ext_supp_rates_len; i++) {
1438 if (i < elems.supp_rates_len)
1439 rate = elems.supp_rates[i];
1440 else if (elems.ext_supp_rates)
1441 rate = elems.ext_supp_rates
1442 [i - elems.supp_rates_len];
1443 own_rate = 5 * (rate & 0x7f);
1444 if (mode->mode == MODE_ATHEROS_TURBO)
1446 for (j = 0; j < num_rates; j++)
1447 if (rates[j].rate == own_rate)
1448 supp_rates |= BIT(j);
1451 prev_rates = sta->supp_rates;
1452 sta->supp_rates &= supp_rates;
1453 if (sta->supp_rates == 0) {
1454 /* No matching rates - this should not really happen.
1455 * Make sure that at least one rate is marked
1456 * supported to avoid issues with TX rate ctrl. */
1457 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1459 if (sta->supp_rates != prev_rates) {
1460 printk(KERN_DEBUG "%s: updated supp_rates set for "
1461 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1463 dev->name, MAC_ARG(sta->addr), prev_rates,
1464 supp_rates, sta->supp_rates);
1472 if (elems.ds_params && elems.ds_params_len == 1)
1473 channel = elems.ds_params[0];
1475 channel = rx_status->channel;
1477 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1479 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1484 /* TODO: order by RSSI? */
1485 spin_lock_bh(&local->sta_bss_lock);
1486 list_move_tail(&bss->list, &local->sta_bss_list);
1487 spin_unlock_bh(&local->sta_bss_lock);
1491 if (bss->probe_resp && beacon) {
1492 /* Do not allow beacon to override data from Probe Response. */
1493 ieee80211_rx_bss_put(dev, bss);
1497 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1498 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1499 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1500 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1501 bss->ssid_len = elems.ssid_len;
1504 bss->supp_rates_len = 0;
1505 if (elems.supp_rates) {
1506 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1507 if (clen > elems.supp_rates_len)
1508 clen = elems.supp_rates_len;
1509 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1511 bss->supp_rates_len += clen;
1513 if (elems.ext_supp_rates) {
1514 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1515 if (clen > elems.ext_supp_rates_len)
1516 clen = elems.ext_supp_rates_len;
1517 memcpy(&bss->supp_rates[bss->supp_rates_len],
1518 elems.ext_supp_rates, clen);
1519 bss->supp_rates_len += clen;
1523 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1524 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1526 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1528 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1529 bss->wpa_ie_len = elems.wpa_len + 2;
1531 bss->wpa_ie_len = 0;
1532 } else if (!elems.wpa && bss->wpa_ie) {
1535 bss->wpa_ie_len = 0;
1539 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1540 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1542 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1544 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1545 bss->rsn_ie_len = elems.rsn_len + 2;
1547 bss->rsn_ie_len = 0;
1548 } else if (!elems.rsn && bss->rsn_ie) {
1551 bss->rsn_ie_len = 0;
1554 if (elems.wmm_param &&
1555 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1556 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1558 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1560 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1561 elems.wmm_param_len + 2);
1562 bss->wmm_ie_len = elems.wmm_param_len + 2;
1564 bss->wmm_ie_len = 0;
1565 } else if (!elems.wmm_param && bss->wmm_ie) {
1568 bss->wmm_ie_len = 0;
1572 bss->hw_mode = rx_status->phymode;
1573 bss->channel = channel;
1574 bss->freq = rx_status->freq;
1575 if (channel != rx_status->channel &&
1576 (bss->hw_mode == MODE_IEEE80211G ||
1577 bss->hw_mode == MODE_IEEE80211B) &&
1578 channel >= 1 && channel <= 14) {
1579 static const int freq_list[] = {
1580 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1581 2447, 2452, 2457, 2462, 2467, 2472, 2484
1583 /* IEEE 802.11g/b mode can receive packets from neighboring
1584 * channels, so map the channel into frequency. */
1585 bss->freq = freq_list[channel - 1];
1587 bss->timestamp = timestamp;
1588 bss->last_update = jiffies;
1589 bss->rssi = rx_status->ssi;
1590 bss->signal = rx_status->signal;
1591 bss->noise = rx_status->noise;
1594 ieee80211_rx_bss_put(dev, bss);
1598 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1599 struct ieee80211_mgmt *mgmt,
1601 struct ieee80211_rx_status *rx_status)
1603 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1607 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1608 struct ieee80211_mgmt *mgmt,
1610 struct ieee80211_rx_status *rx_status)
1612 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1613 struct ieee80211_sub_if_data *sdata;
1614 struct ieee80211_if_sta *ifsta;
1617 struct ieee802_11_elems elems;
1619 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1621 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1622 if (sdata->type != IEEE80211_IF_TYPE_STA)
1624 ifsta = &sdata->u.sta;
1626 if (!ifsta->associated ||
1627 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1630 /* Process beacon from the current BSS */
1631 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1635 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1636 &elems) == ParseFailed)
1640 if (elems.erp_info && elems.erp_info_len >= 1) {
1642 (elems.erp_info[0] & ERP_INFO_USE_PROTECTION) != 0;
1645 if (use_protection != !!ifsta->use_protection) {
1646 if (net_ratelimit()) {
1647 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
1650 use_protection ? "enabled" : "disabled",
1651 MAC_ARG(ifsta->bssid));
1653 ifsta->use_protection = use_protection ? 1 : 0;
1654 local->cts_protect_erp_frames = use_protection;
1657 if (elems.wmm_param && ifsta->wmm_enabled) {
1658 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1659 elems.wmm_param_len);
1664 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1665 struct ieee80211_if_sta *ifsta,
1666 struct ieee80211_mgmt *mgmt,
1668 struct ieee80211_rx_status *rx_status)
1670 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1671 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1673 struct sk_buff *skb;
1674 struct ieee80211_mgmt *resp;
1677 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1678 ifsta->state != IEEE80211_IBSS_JOINED ||
1679 len < 24 + 2 || !ifsta->probe_resp)
1682 if (local->ops->tx_last_beacon)
1683 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1687 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1688 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1689 MAC_FMT " (tx_last_beacon=%d)\n",
1690 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1691 MAC_ARG(mgmt->bssid), tx_last_beacon);
1692 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1694 if (!tx_last_beacon)
1697 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1698 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1701 end = ((u8 *) mgmt) + len;
1702 pos = mgmt->u.probe_req.variable;
1703 if (pos[0] != WLAN_EID_SSID ||
1704 pos + 2 + pos[1] > end) {
1705 if (net_ratelimit()) {
1706 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1707 "from " MAC_FMT "\n",
1708 dev->name, MAC_ARG(mgmt->sa));
1713 (pos[1] != ifsta->ssid_len ||
1714 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1715 /* Ignore ProbeReq for foreign SSID */
1719 /* Reply with ProbeResp */
1720 skb = skb_copy(ifsta->probe_resp, GFP_ATOMIC);
1724 resp = (struct ieee80211_mgmt *) skb->data;
1725 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1726 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1727 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1728 dev->name, MAC_ARG(resp->da));
1729 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1730 ieee80211_sta_tx(dev, skb, 0);
1734 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1735 struct ieee80211_rx_status *rx_status)
1737 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1738 struct ieee80211_sub_if_data *sdata;
1739 struct ieee80211_if_sta *ifsta;
1740 struct ieee80211_mgmt *mgmt;
1746 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1747 ifsta = &sdata->u.sta;
1749 mgmt = (struct ieee80211_mgmt *) skb->data;
1750 fc = le16_to_cpu(mgmt->frame_control);
1752 switch (fc & IEEE80211_FCTL_STYPE) {
1753 case IEEE80211_STYPE_PROBE_REQ:
1754 case IEEE80211_STYPE_PROBE_RESP:
1755 case IEEE80211_STYPE_BEACON:
1756 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1757 case IEEE80211_STYPE_AUTH:
1758 case IEEE80211_STYPE_ASSOC_RESP:
1759 case IEEE80211_STYPE_REASSOC_RESP:
1760 case IEEE80211_STYPE_DEAUTH:
1761 case IEEE80211_STYPE_DISASSOC:
1762 skb_queue_tail(&ifsta->skb_queue, skb);
1763 queue_work(local->hw.workqueue, &ifsta->work);
1766 printk(KERN_DEBUG "%s: received unknown management frame - "
1767 "stype=%d\n", dev->name,
1768 (fc & IEEE80211_FCTL_STYPE) >> 4);
1777 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1778 struct sk_buff *skb)
1780 struct ieee80211_rx_status *rx_status;
1781 struct ieee80211_sub_if_data *sdata;
1782 struct ieee80211_if_sta *ifsta;
1783 struct ieee80211_mgmt *mgmt;
1786 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1787 ifsta = &sdata->u.sta;
1789 rx_status = (struct ieee80211_rx_status *) skb->cb;
1790 mgmt = (struct ieee80211_mgmt *) skb->data;
1791 fc = le16_to_cpu(mgmt->frame_control);
1793 switch (fc & IEEE80211_FCTL_STYPE) {
1794 case IEEE80211_STYPE_PROBE_REQ:
1795 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1798 case IEEE80211_STYPE_PROBE_RESP:
1799 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1801 case IEEE80211_STYPE_BEACON:
1802 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1804 case IEEE80211_STYPE_AUTH:
1805 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1807 case IEEE80211_STYPE_ASSOC_RESP:
1808 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1810 case IEEE80211_STYPE_REASSOC_RESP:
1811 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1813 case IEEE80211_STYPE_DEAUTH:
1814 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1816 case IEEE80211_STYPE_DISASSOC:
1817 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1825 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1826 struct ieee80211_rx_status *rx_status)
1828 struct ieee80211_mgmt *mgmt;
1831 if (skb->len < 24) {
1836 mgmt = (struct ieee80211_mgmt *) skb->data;
1837 fc = le16_to_cpu(mgmt->frame_control);
1839 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1840 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1841 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1842 skb->len, rx_status);
1843 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1844 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1853 static int ieee80211_sta_active_ibss(struct net_device *dev)
1855 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1857 struct sta_info *sta;
1859 spin_lock_bh(&local->sta_lock);
1860 list_for_each_entry(sta, &local->sta_list, list) {
1861 if (sta->dev == dev &&
1862 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1868 spin_unlock_bh(&local->sta_lock);
1874 static void ieee80211_sta_expire(struct net_device *dev)
1876 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1877 struct sta_info *sta, *tmp;
1879 spin_lock_bh(&local->sta_lock);
1880 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1881 if (time_after(jiffies, sta->last_rx +
1882 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1883 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1884 "\n", dev->name, MAC_ARG(sta->addr));
1885 sta_info_free(sta, 1);
1887 spin_unlock_bh(&local->sta_lock);
1891 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1892 struct ieee80211_if_sta *ifsta)
1894 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1896 ieee80211_sta_expire(dev);
1897 if (ieee80211_sta_active_ibss(dev))
1900 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1901 "IBSS networks with same SSID (merge)\n", dev->name);
1902 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1906 void ieee80211_sta_timer(unsigned long data)
1908 struct ieee80211_sub_if_data *sdata =
1909 (struct ieee80211_sub_if_data *) data;
1910 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1911 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1913 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1914 queue_work(local->hw.workqueue, &ifsta->work);
1918 void ieee80211_sta_work(struct work_struct *work)
1920 struct ieee80211_sub_if_data *sdata =
1921 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1922 struct net_device *dev = sdata->dev;
1923 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1924 struct ieee80211_if_sta *ifsta;
1925 struct sk_buff *skb;
1927 if (!netif_running(dev))
1930 if (local->sta_scanning)
1933 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1934 sdata->type != IEEE80211_IF_TYPE_IBSS) {
1935 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
1936 "(type=%d)\n", dev->name, sdata->type);
1939 ifsta = &sdata->u.sta;
1941 while ((skb = skb_dequeue(&ifsta->skb_queue)))
1942 ieee80211_sta_rx_queued_mgmt(dev, skb);
1944 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1945 ifsta->state != IEEE80211_ASSOCIATE &&
1946 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
1947 ieee80211_sta_start_scan(dev, NULL, 0);
1951 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
1952 if (ieee80211_sta_config_auth(dev, ifsta))
1954 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1955 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
1958 switch (ifsta->state) {
1959 case IEEE80211_DISABLED:
1961 case IEEE80211_AUTHENTICATE:
1962 ieee80211_authenticate(dev, ifsta);
1964 case IEEE80211_ASSOCIATE:
1965 ieee80211_associate(dev, ifsta);
1967 case IEEE80211_ASSOCIATED:
1968 ieee80211_associated(dev, ifsta);
1970 case IEEE80211_IBSS_SEARCH:
1971 ieee80211_sta_find_ibss(dev, ifsta);
1973 case IEEE80211_IBSS_JOINED:
1974 ieee80211_sta_merge_ibss(dev, ifsta);
1977 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
1982 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1983 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
1984 "mixed-cell disabled - disassociate\n", dev->name);
1986 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
1987 ieee80211_set_disassoc(dev, ifsta, 0);
1992 static void ieee80211_sta_reset_auth(struct net_device *dev,
1993 struct ieee80211_if_sta *ifsta)
1995 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1997 if (local->ops->reset_tsf) {
1998 /* Reset own TSF to allow time synchronization work. */
1999 local->ops->reset_tsf(local_to_hw(local));
2002 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2005 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2006 ifsta->auth_alg = WLAN_AUTH_OPEN;
2007 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2008 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2009 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2010 ifsta->auth_alg = WLAN_AUTH_LEAP;
2012 ifsta->auth_alg = WLAN_AUTH_OPEN;
2013 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2015 ifsta->auth_transaction = -1;
2016 ifsta->associated = ifsta->auth_tries = ifsta->assoc_tries = 0;
2017 netif_carrier_off(dev);
2021 void ieee80211_sta_req_auth(struct net_device *dev,
2022 struct ieee80211_if_sta *ifsta)
2024 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2025 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2027 if (sdata->type != IEEE80211_IF_TYPE_STA)
2030 if ((ifsta->bssid_set || ifsta->auto_bssid_sel) &&
2031 (ifsta->ssid_set || ifsta->auto_ssid_sel)) {
2032 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2033 queue_work(local->hw.workqueue, &ifsta->work);
2037 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2038 const char *ssid, int ssid_len)
2040 int tmp, hidden_ssid;
2042 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2045 if (ifsta->auto_bssid_sel)
2051 if (ssid[tmp] != '\0') {
2057 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2060 if (ssid_len == 1 && ssid[0] == ' ')
2066 static int ieee80211_sta_config_auth(struct net_device *dev,
2067 struct ieee80211_if_sta *ifsta)
2069 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2070 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2071 struct ieee80211_sta_bss *bss, *selected = NULL;
2072 int top_rssi = 0, freq;
2076 if (!ifsta->auto_channel_sel && !ifsta->auto_bssid_sel &&
2077 !ifsta->auto_ssid_sel) {
2078 ifsta->state = IEEE80211_AUTHENTICATE;
2080 ieee80211_sta_reset_auth(dev, ifsta);
2084 spin_lock_bh(&local->sta_bss_lock);
2085 freq = local->oper_channel->freq;
2086 list_for_each_entry(bss, &local->sta_bss_list, list) {
2087 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2090 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2091 !!sdata->default_key)
2094 if (!ifsta->auto_channel_sel && bss->freq != freq)
2097 if (!ifsta->auto_bssid_sel &&
2098 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2101 if (!ifsta->auto_ssid_sel &&
2102 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2105 if (!selected || top_rssi < bss->rssi) {
2107 top_rssi = bss->rssi;
2111 atomic_inc(&selected->users);
2112 spin_unlock_bh(&local->sta_bss_lock);
2115 ieee80211_set_channel(local, -1, selected->freq);
2116 if (!ifsta->ssid_set)
2117 ieee80211_sta_set_ssid(dev, selected->ssid,
2118 selected->ssid_len);
2119 ieee80211_sta_set_bssid(dev, selected->bssid);
2120 ieee80211_rx_bss_put(dev, selected);
2121 ifsta->state = IEEE80211_AUTHENTICATE;
2123 ieee80211_sta_reset_auth(dev, ifsta);
2126 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2127 ieee80211_sta_start_scan(dev, NULL, 0);
2128 ifsta->state = IEEE80211_AUTHENTICATE;
2129 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2131 ifsta->state = IEEE80211_DISABLED;
2137 static int ieee80211_sta_join_ibss(struct net_device *dev,
2138 struct ieee80211_if_sta *ifsta,
2139 struct ieee80211_sta_bss *bss)
2141 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2142 int res, rates, i, j;
2143 struct sk_buff *skb;
2144 struct ieee80211_mgmt *mgmt;
2145 struct ieee80211_tx_control control;
2146 struct ieee80211_rate *rate;
2147 struct ieee80211_hw_mode *mode;
2148 struct rate_control_extra extra;
2150 struct ieee80211_sub_if_data *sdata;
2152 /* Remove possible STA entries from other IBSS networks. */
2153 sta_info_flush(local, NULL);
2155 if (local->ops->reset_tsf) {
2156 /* Reset own TSF to allow time synchronization work. */
2157 local->ops->reset_tsf(local_to_hw(local));
2159 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2160 res = ieee80211_if_config(dev);
2164 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2166 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2167 sdata->drop_unencrypted = bss->capability &
2168 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2170 res = ieee80211_set_channel(local, -1, bss->freq);
2172 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2173 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2174 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2175 local->hw.conf.freq);
2179 /* Set beacon template based on scan results */
2180 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2185 skb_reserve(skb, local->hw.extra_tx_headroom);
2187 mgmt = (struct ieee80211_mgmt *)
2188 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2189 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2190 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2191 IEEE80211_STYPE_BEACON);
2192 memset(mgmt->da, 0xff, ETH_ALEN);
2193 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2194 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2195 mgmt->u.beacon.beacon_int =
2196 cpu_to_le16(local->hw.conf.beacon_int);
2197 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2199 pos = skb_put(skb, 2 + ifsta->ssid_len);
2200 *pos++ = WLAN_EID_SSID;
2201 *pos++ = ifsta->ssid_len;
2202 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2204 rates = bss->supp_rates_len;
2207 pos = skb_put(skb, 2 + rates);
2208 *pos++ = WLAN_EID_SUPP_RATES;
2210 memcpy(pos, bss->supp_rates, rates);
2212 pos = skb_put(skb, 2 + 1);
2213 *pos++ = WLAN_EID_DS_PARAMS;
2215 *pos++ = bss->channel;
2217 pos = skb_put(skb, 2 + 2);
2218 *pos++ = WLAN_EID_IBSS_PARAMS;
2220 /* FIX: set ATIM window based on scan results */
2224 if (bss->supp_rates_len > 8) {
2225 rates = bss->supp_rates_len - 8;
2226 pos = skb_put(skb, 2 + rates);
2227 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2229 memcpy(pos, &bss->supp_rates[8], rates);
2232 memset(&control, 0, sizeof(control));
2233 memset(&extra, 0, sizeof(extra));
2234 extra.mode = local->oper_hw_mode;
2235 rate = rate_control_get_rate(local, dev, skb, &extra);
2237 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2238 "for IBSS beacon\n", dev->name);
2241 control.tx_rate = (local->short_preamble &&
2242 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2243 rate->val2 : rate->val;
2244 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2245 control.power_level = local->hw.conf.power_level;
2246 control.flags |= IEEE80211_TXCTL_NO_ACK;
2247 control.retry_limit = 1;
2249 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2250 if (ifsta->probe_resp) {
2251 mgmt = (struct ieee80211_mgmt *)
2252 ifsta->probe_resp->data;
2253 mgmt->frame_control =
2254 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2255 IEEE80211_STYPE_PROBE_RESP);
2257 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2258 "template for IBSS\n", dev->name);
2261 if (local->ops->beacon_update &&
2262 local->ops->beacon_update(local_to_hw(local),
2263 skb, &control) == 0) {
2264 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2265 "template based on scan results\n", dev->name);
2270 mode = local->oper_hw_mode;
2271 for (i = 0; i < bss->supp_rates_len; i++) {
2272 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2273 if (mode->mode == MODE_ATHEROS_TURBO)
2275 for (j = 0; j < mode->num_rates; j++)
2276 if (mode->rates[j].rate == bitrate)
2279 ifsta->supp_rates_bits = rates;
2283 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2284 "template\n", dev->name);
2288 ifsta->state = IEEE80211_IBSS_JOINED;
2289 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2291 ieee80211_rx_bss_put(dev, bss);
2297 static int ieee80211_sta_create_ibss(struct net_device *dev,
2298 struct ieee80211_if_sta *ifsta)
2300 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2301 struct ieee80211_sta_bss *bss;
2302 struct ieee80211_sub_if_data *sdata;
2303 struct ieee80211_hw_mode *mode;
2304 u8 bssid[ETH_ALEN], *pos;
2308 /* Easier testing, use fixed BSSID. */
2309 memset(bssid, 0xfe, ETH_ALEN);
2311 /* Generate random, not broadcast, locally administered BSSID. Mix in
2312 * own MAC address to make sure that devices that do not have proper
2313 * random number generator get different BSSID. */
2314 get_random_bytes(bssid, ETH_ALEN);
2315 for (i = 0; i < ETH_ALEN; i++)
2316 bssid[i] ^= dev->dev_addr[i];
2321 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2322 dev->name, MAC_ARG(bssid));
2324 bss = ieee80211_rx_bss_add(dev, bssid);
2328 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2329 mode = local->oper_hw_mode;
2331 if (local->hw.conf.beacon_int == 0)
2332 local->hw.conf.beacon_int = 100;
2333 bss->beacon_int = local->hw.conf.beacon_int;
2334 bss->hw_mode = local->hw.conf.phymode;
2335 bss->channel = local->hw.conf.channel;
2336 bss->freq = local->hw.conf.freq;
2337 bss->last_update = jiffies;
2338 bss->capability = WLAN_CAPABILITY_IBSS;
2339 if (sdata->default_key) {
2340 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2342 sdata->drop_unencrypted = 0;
2343 bss->supp_rates_len = mode->num_rates;
2344 pos = bss->supp_rates;
2345 for (i = 0; i < mode->num_rates; i++) {
2346 int rate = mode->rates[i].rate;
2347 if (mode->mode == MODE_ATHEROS_TURBO)
2349 *pos++ = (u8) (rate / 5);
2352 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2356 static int ieee80211_sta_find_ibss(struct net_device *dev,
2357 struct ieee80211_if_sta *ifsta)
2359 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2360 struct ieee80211_sta_bss *bss;
2365 if (ifsta->ssid_len == 0)
2368 active_ibss = ieee80211_sta_active_ibss(dev);
2369 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2370 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2371 dev->name, active_ibss);
2372 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2373 spin_lock_bh(&local->sta_bss_lock);
2374 list_for_each_entry(bss, &local->sta_bss_list, list) {
2375 if (ifsta->ssid_len != bss->ssid_len ||
2376 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2377 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2379 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2380 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2381 MAC_ARG(bss->bssid));
2382 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2383 memcpy(bssid, bss->bssid, ETH_ALEN);
2385 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2388 spin_unlock_bh(&local->sta_bss_lock);
2390 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2391 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2392 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2393 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2394 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2395 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2396 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2397 " based on configured SSID\n",
2398 dev->name, MAC_ARG(bssid));
2399 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2401 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2402 printk(KERN_DEBUG " did not try to join ibss\n");
2403 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2405 /* Selected IBSS not found in current scan results - try to scan */
2406 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2407 !ieee80211_sta_active_ibss(dev)) {
2408 mod_timer(&ifsta->timer, jiffies +
2409 IEEE80211_IBSS_MERGE_INTERVAL);
2410 } else if (time_after(jiffies, local->last_scan_completed +
2411 IEEE80211_SCAN_INTERVAL)) {
2412 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2413 "join\n", dev->name);
2414 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2416 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2417 int interval = IEEE80211_SCAN_INTERVAL;
2419 if (time_after(jiffies, ifsta->ibss_join_req +
2420 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2421 if (ifsta->create_ibss &&
2422 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2423 return ieee80211_sta_create_ibss(dev, ifsta);
2424 if (ifsta->create_ibss) {
2425 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2426 " configured channel %d (%d MHz)\n",
2427 dev->name, local->hw.conf.channel,
2428 local->hw.conf.freq);
2431 /* No IBSS found - decrease scan interval and continue
2433 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2436 ifsta->state = IEEE80211_IBSS_SEARCH;
2437 mod_timer(&ifsta->timer, jiffies + interval);
2445 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2447 struct ieee80211_sub_if_data *sdata;
2448 struct ieee80211_if_sta *ifsta;
2449 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2451 if (len > IEEE80211_MAX_SSID_LEN)
2454 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2456 if (local->ops->conf_tx) {
2457 struct ieee80211_tx_queue_params qparam;
2460 memset(&qparam, 0, sizeof(qparam));
2461 /* TODO: are these ok defaults for all hw_modes? */
2464 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2465 qparam.cw_max = 1023;
2466 qparam.burst_time = 0;
2467 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2469 local->ops->conf_tx(local_to_hw(local),
2470 i + IEEE80211_TX_QUEUE_DATA0,
2473 /* IBSS uses different parameters for Beacon sending */
2477 local->ops->conf_tx(local_to_hw(local),
2478 IEEE80211_TX_QUEUE_BEACON, &qparam);
2481 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2482 ifsta = &sdata->u.sta;
2484 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2485 ifsta->prev_bssid_set = 0;
2486 memcpy(ifsta->ssid, ssid, len);
2487 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2488 ifsta->ssid_len = len;
2490 ifsta->ssid_set = len ? 1 : 0;
2491 if (sdata->type == IEEE80211_IF_TYPE_IBSS && !ifsta->bssid_set) {
2492 ifsta->ibss_join_req = jiffies;
2493 ifsta->state = IEEE80211_IBSS_SEARCH;
2494 return ieee80211_sta_find_ibss(dev, ifsta);
2500 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2502 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2503 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2504 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2505 *len = ifsta->ssid_len;
2510 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2512 struct ieee80211_sub_if_data *sdata;
2513 struct ieee80211_if_sta *ifsta;
2516 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2517 ifsta = &sdata->u.sta;
2519 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2520 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2521 res = ieee80211_if_config(dev);
2523 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2524 "the low-level driver\n", dev->name);
2529 if (!is_valid_ether_addr(bssid))
2530 ifsta->bssid_set = 0;
2532 ifsta->bssid_set = 1;
2537 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2538 struct ieee80211_sub_if_data *sdata,
2541 struct sk_buff *skb;
2542 struct ieee80211_hdr *nullfunc;
2545 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2547 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2548 "frame\n", sdata->dev->name);
2551 skb_reserve(skb, local->hw.extra_tx_headroom);
2553 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2554 memset(nullfunc, 0, 24);
2555 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2556 IEEE80211_FCTL_TODS;
2558 fc |= IEEE80211_FCTL_PM;
2559 nullfunc->frame_control = cpu_to_le16(fc);
2560 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2561 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2562 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2564 ieee80211_sta_tx(sdata->dev, skb, 0);
2568 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2570 struct ieee80211_local *local = hw_to_local(hw);
2571 struct net_device *dev = local->scan_dev;
2572 struct ieee80211_sub_if_data *sdata;
2573 union iwreq_data wrqu;
2575 local->last_scan_completed = jiffies;
2577 local->sta_scanning = 0;
2579 if (ieee80211_hw_config(local))
2580 printk(KERN_DEBUG "%s: failed to restore operational"
2581 "channel after scan\n", dev->name);
2583 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2584 ieee80211_if_config(dev))
2585 printk(KERN_DEBUG "%s: failed to restore operational"
2586 "BSSID after scan\n", dev->name);
2588 memset(&wrqu, 0, sizeof(wrqu));
2589 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2591 read_lock(&local->sub_if_lock);
2592 list_for_each_entry(sdata, &local->sub_if_list, list) {
2593 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2594 if (sdata->u.sta.associated)
2595 ieee80211_send_nullfunc(local, sdata, 0);
2596 ieee80211_sta_timer((unsigned long)sdata);
2598 netif_wake_queue(sdata->dev);
2600 read_unlock(&local->sub_if_lock);
2602 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2603 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2604 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2605 if (!ifsta->bssid_set ||
2606 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2607 !ieee80211_sta_active_ibss(dev)))
2608 ieee80211_sta_find_ibss(dev, ifsta);
2611 EXPORT_SYMBOL(ieee80211_scan_completed);
2613 void ieee80211_sta_scan_work(struct work_struct *work)
2615 struct ieee80211_local *local =
2616 container_of(work, struct ieee80211_local, scan_work.work);
2617 struct net_device *dev = local->scan_dev;
2618 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2619 struct ieee80211_hw_mode *mode;
2620 struct ieee80211_channel *chan;
2622 unsigned long next_delay = 0;
2624 if (!local->sta_scanning)
2627 switch (local->scan_state) {
2628 case SCAN_SET_CHANNEL:
2629 mode = local->scan_hw_mode;
2630 if (local->scan_hw_mode->list.next == &local->modes_list &&
2631 local->scan_channel_idx >= mode->num_channels) {
2632 ieee80211_scan_completed(local_to_hw(local));
2635 skip = !(local->enabled_modes & (1 << mode->mode));
2636 chan = &mode->channels[local->scan_channel_idx];
2637 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2638 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2639 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2640 (local->hw_modes & local->enabled_modes &
2641 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2646 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2647 dev->name, chan->chan, chan->freq);
2650 local->scan_channel = chan;
2651 if (ieee80211_hw_config(local)) {
2652 printk(KERN_DEBUG "%s: failed to set channel "
2653 "%d (%d MHz) for scan\n", dev->name,
2654 chan->chan, chan->freq);
2659 local->scan_channel_idx++;
2660 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2661 if (local->scan_hw_mode->list.next != &local->modes_list) {
2662 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2663 struct ieee80211_hw_mode,
2665 local->scan_channel_idx = 0;
2672 next_delay = IEEE80211_PROBE_DELAY +
2673 usecs_to_jiffies(local->hw.channel_change_time);
2674 local->scan_state = SCAN_SEND_PROBE;
2676 case SCAN_SEND_PROBE:
2677 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2678 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2679 local->scan_ssid_len);
2680 next_delay = IEEE80211_CHANNEL_TIME;
2682 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2683 local->scan_state = SCAN_SET_CHANNEL;
2687 if (local->sta_scanning)
2688 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2693 static int ieee80211_sta_start_scan(struct net_device *dev,
2694 u8 *ssid, size_t ssid_len)
2696 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2697 struct ieee80211_sub_if_data *sdata;
2699 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2702 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2703 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2706 * ScanType: ACTIVE, PASSIVE
2707 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2708 * a Probe frame during active scanning
2710 * MinChannelTime (>= ProbeDelay), in TU
2711 * MaxChannelTime: (>= MinChannelTime), in TU
2714 /* MLME-SCAN.confirm
2716 * ResultCode: SUCCESS, INVALID_PARAMETERS
2719 if (local->sta_scanning) {
2720 if (local->scan_dev == dev)
2725 if (local->ops->hw_scan) {
2726 int rc = local->ops->hw_scan(local_to_hw(local),
2729 local->sta_scanning = 1;
2730 local->scan_dev = dev;
2735 local->sta_scanning = 1;
2737 read_lock(&local->sub_if_lock);
2738 list_for_each_entry(sdata, &local->sub_if_list, list) {
2739 netif_stop_queue(sdata->dev);
2740 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2741 sdata->u.sta.associated)
2742 ieee80211_send_nullfunc(local, sdata, 1);
2744 read_unlock(&local->sub_if_lock);
2747 local->scan_ssid_len = ssid_len;
2748 memcpy(local->scan_ssid, ssid, ssid_len);
2750 local->scan_ssid_len = 0;
2751 local->scan_state = SCAN_SET_CHANNEL;
2752 local->scan_hw_mode = list_entry(local->modes_list.next,
2753 struct ieee80211_hw_mode,
2755 local->scan_channel_idx = 0;
2756 local->scan_dev = dev;
2758 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2759 ieee80211_if_config(dev))
2760 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2763 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2764 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2765 IEEE80211_CHANNEL_TIME);
2771 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2773 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2774 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2775 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2777 if (sdata->type != IEEE80211_IF_TYPE_STA)
2778 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2780 if (local->sta_scanning) {
2781 if (local->scan_dev == dev)
2786 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2787 queue_work(local->hw.workqueue, &ifsta->work);
2792 ieee80211_sta_scan_result(struct net_device *dev,
2793 struct ieee80211_sta_bss *bss,
2794 char *current_ev, char *end_buf)
2796 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2797 struct iw_event iwe;
2799 if (time_after(jiffies,
2800 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2803 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2806 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2807 !bss->wpa_ie && !bss->rsn_ie)
2810 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2811 (local->scan_ssid_len != bss->ssid_len ||
2812 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2815 memset(&iwe, 0, sizeof(iwe));
2816 iwe.cmd = SIOCGIWAP;
2817 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2818 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2819 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2822 memset(&iwe, 0, sizeof(iwe));
2823 iwe.cmd = SIOCGIWESSID;
2824 iwe.u.data.length = bss->ssid_len;
2825 iwe.u.data.flags = 1;
2826 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2829 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2830 memset(&iwe, 0, sizeof(iwe));
2831 iwe.cmd = SIOCGIWMODE;
2832 if (bss->capability & WLAN_CAPABILITY_ESS)
2833 iwe.u.mode = IW_MODE_MASTER;
2835 iwe.u.mode = IW_MODE_ADHOC;
2836 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2840 memset(&iwe, 0, sizeof(iwe));
2841 iwe.cmd = SIOCGIWFREQ;
2842 iwe.u.freq.m = bss->channel;
2844 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2846 iwe.u.freq.m = bss->freq * 100000;
2848 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2851 memset(&iwe, 0, sizeof(iwe));
2853 iwe.u.qual.qual = bss->signal;
2854 iwe.u.qual.level = bss->rssi;
2855 iwe.u.qual.noise = bss->noise;
2856 iwe.u.qual.updated = local->wstats_flags;
2857 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2860 memset(&iwe, 0, sizeof(iwe));
2861 iwe.cmd = SIOCGIWENCODE;
2862 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2863 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2865 iwe.u.data.flags = IW_ENCODE_DISABLED;
2866 iwe.u.data.length = 0;
2867 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2869 if (bss && bss->wpa_ie) {
2870 memset(&iwe, 0, sizeof(iwe));
2871 iwe.cmd = IWEVGENIE;
2872 iwe.u.data.length = bss->wpa_ie_len;
2873 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2877 if (bss && bss->rsn_ie) {
2878 memset(&iwe, 0, sizeof(iwe));
2879 iwe.cmd = IWEVGENIE;
2880 iwe.u.data.length = bss->rsn_ie_len;
2881 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2885 if (bss && bss->supp_rates_len > 0) {
2886 /* display all supported rates in readable format */
2887 char *p = current_ev + IW_EV_LCP_LEN;
2890 memset(&iwe, 0, sizeof(iwe));
2891 iwe.cmd = SIOCGIWRATE;
2892 /* Those two flags are ignored... */
2893 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2895 for (i = 0; i < bss->supp_rates_len; i++) {
2896 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2898 p = iwe_stream_add_value(current_ev, p,
2899 end_buf, &iwe, IW_EV_PARAM_LEN);
2906 buf = kmalloc(30, GFP_ATOMIC);
2908 memset(&iwe, 0, sizeof(iwe));
2909 iwe.cmd = IWEVCUSTOM;
2910 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
2911 iwe.u.data.length = strlen(buf);
2912 current_ev = iwe_stream_add_point(current_ev, end_buf,
2921 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
2924 buf = kmalloc(100, GFP_ATOMIC);
2928 memset(&iwe, 0, sizeof(iwe));
2929 iwe.cmd = IWEVCUSTOM;
2930 sprintf(buf, "bcn_int=%d", bss->beacon_int);
2931 iwe.u.data.length = strlen(buf);
2932 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2935 memset(&iwe, 0, sizeof(iwe));
2936 iwe.cmd = IWEVCUSTOM;
2937 sprintf(buf, "capab=0x%04x", bss->capability);
2938 iwe.u.data.length = strlen(buf);
2939 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2950 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
2952 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2953 char *current_ev = buf;
2954 char *end_buf = buf + len;
2955 struct ieee80211_sta_bss *bss;
2957 spin_lock_bh(&local->sta_bss_lock);
2958 list_for_each_entry(bss, &local->sta_bss_list, list) {
2959 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
2960 spin_unlock_bh(&local->sta_bss_lock);
2963 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
2966 spin_unlock_bh(&local->sta_bss_lock);
2967 return current_ev - buf;
2971 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
2973 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2974 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2975 kfree(ifsta->extra_ie);
2977 ifsta->extra_ie = NULL;
2978 ifsta->extra_ie_len = 0;
2981 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2982 if (!ifsta->extra_ie) {
2983 ifsta->extra_ie_len = 0;
2986 memcpy(ifsta->extra_ie, ie, len);
2987 ifsta->extra_ie_len = len;
2992 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
2993 struct sk_buff *skb, u8 *bssid,
2996 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2997 struct sta_info *sta;
2998 struct ieee80211_sub_if_data *sdata = NULL;
3000 /* TODO: Could consider removing the least recently used entry and
3001 * allow new one to be added. */
3002 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3003 if (net_ratelimit()) {
3004 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3005 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3010 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3011 local->mdev->name, MAC_ARG(addr), dev->name);
3013 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3017 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3019 rate_control_rate_init(sta, local);
3021 return sta; /* caller will call sta_info_put() */
3025 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3027 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3028 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3030 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3033 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3034 sdata->type != IEEE80211_IF_TYPE_IBSS)
3037 ieee80211_send_deauth(dev, ifsta, reason);
3038 ieee80211_set_disassoc(dev, ifsta, 1);
3043 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3045 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3046 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3048 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3051 if (sdata->type != IEEE80211_IF_TYPE_STA)
3054 if (!ifsta->associated)
3057 ieee80211_send_disassoc(dev, ifsta, reason);
3058 ieee80211_set_disassoc(dev, ifsta, 0);