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 {
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);
1158 if (status_code == WLAN_STATUS_REASSOC_NO_ASSOC)
1159 ifsta->prev_bssid_set = 0;
1163 pos = mgmt->u.assoc_resp.variable;
1164 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1166 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1171 if (!elems.supp_rates) {
1172 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1177 printk(KERN_DEBUG "%s: associated\n", dev->name);
1179 ifsta->ap_capab = capab_info;
1181 kfree(ifsta->assocresp_ies);
1182 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1183 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_ATOMIC);
1184 if (ifsta->assocresp_ies)
1185 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1187 ieee80211_set_associated(dev, ifsta, 1);
1189 /* Add STA entry for the AP */
1190 sta = sta_info_get(local, ifsta->bssid);
1192 struct ieee80211_sta_bss *bss;
1193 sta = sta_info_add(local, dev, ifsta->bssid, GFP_ATOMIC);
1195 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1196 " AP\n", dev->name);
1199 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1201 sta->last_rssi = bss->rssi;
1202 sta->last_signal = bss->signal;
1203 sta->last_noise = bss->noise;
1204 ieee80211_rx_bss_put(dev, bss);
1209 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
1213 mode = local->oper_hw_mode;
1214 for (i = 0; i < elems.supp_rates_len; i++) {
1215 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1216 if (mode->mode == MODE_ATHEROS_TURBO)
1218 for (j = 0; j < mode->num_rates; j++)
1219 if (mode->rates[j].rate == rate)
1222 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1223 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1224 if (mode->mode == MODE_ATHEROS_TURBO)
1226 for (j = 0; j < mode->num_rates; j++)
1227 if (mode->rates[j].rate == rate)
1230 sta->supp_rates = rates;
1232 rate_control_rate_init(sta, local);
1234 if (elems.wmm_param && ifsta->wmm_enabled) {
1235 sta->flags |= WLAN_STA_WME;
1236 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1237 elems.wmm_param_len);
1243 ieee80211_associated(dev, ifsta);
1247 /* Caller must hold local->sta_bss_lock */
1248 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1249 struct ieee80211_sta_bss *bss)
1251 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1252 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1253 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1257 /* Caller must hold local->sta_bss_lock */
1258 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1259 struct ieee80211_sta_bss *bss)
1261 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1262 struct ieee80211_sta_bss *b, *prev = NULL;
1263 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1267 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1270 prev->hnext = bss->hnext;
1279 static struct ieee80211_sta_bss *
1280 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1282 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1283 struct ieee80211_sta_bss *bss;
1285 bss = kmalloc(sizeof(*bss), GFP_ATOMIC);
1288 memset(bss, 0, sizeof(*bss));
1289 atomic_inc(&bss->users);
1290 atomic_inc(&bss->users);
1291 memcpy(bss->bssid, bssid, ETH_ALEN);
1293 spin_lock_bh(&local->sta_bss_lock);
1294 /* TODO: order by RSSI? */
1295 list_add_tail(&bss->list, &local->sta_bss_list);
1296 __ieee80211_rx_bss_hash_add(dev, bss);
1297 spin_unlock_bh(&local->sta_bss_lock);
1302 static struct ieee80211_sta_bss *
1303 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1305 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1306 struct ieee80211_sta_bss *bss;
1308 spin_lock_bh(&local->sta_bss_lock);
1309 bss = local->sta_bss_hash[STA_HASH(bssid)];
1311 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1312 atomic_inc(&bss->users);
1317 spin_unlock_bh(&local->sta_bss_lock);
1322 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1331 static void ieee80211_rx_bss_put(struct net_device *dev,
1332 struct ieee80211_sta_bss *bss)
1334 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1335 if (!atomic_dec_and_test(&bss->users))
1338 spin_lock_bh(&local->sta_bss_lock);
1339 __ieee80211_rx_bss_hash_del(dev, bss);
1340 list_del(&bss->list);
1341 spin_unlock_bh(&local->sta_bss_lock);
1342 ieee80211_rx_bss_free(bss);
1346 void ieee80211_rx_bss_list_init(struct net_device *dev)
1348 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1349 spin_lock_init(&local->sta_bss_lock);
1350 INIT_LIST_HEAD(&local->sta_bss_list);
1354 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1356 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1357 struct ieee80211_sta_bss *bss, *tmp;
1359 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1360 ieee80211_rx_bss_put(dev, bss);
1364 static void ieee80211_rx_bss_info(struct net_device *dev,
1365 struct ieee80211_mgmt *mgmt,
1367 struct ieee80211_rx_status *rx_status,
1370 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1371 struct ieee802_11_elems elems;
1373 int channel, invalid = 0, clen;
1374 struct ieee80211_sta_bss *bss;
1375 struct sta_info *sta;
1376 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1379 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1380 return; /* ignore ProbeResp to foreign address */
1383 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1384 dev->name, beacon ? "Beacon" : "Probe Response",
1385 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1388 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1392 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1394 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1395 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1396 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1397 static unsigned long last_tsf_debug = 0;
1399 if (local->ops->get_tsf)
1400 tsf = local->ops->get_tsf(local_to_hw(local));
1403 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1404 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1405 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1407 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1408 (unsigned long long)tsf,
1409 (unsigned long long)timestamp,
1410 (unsigned long long)(tsf - timestamp),
1412 last_tsf_debug = jiffies;
1414 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1417 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1418 &elems) == ParseFailed)
1421 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1422 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1423 (sta = sta_info_get(local, mgmt->sa))) {
1424 struct ieee80211_hw_mode *mode;
1425 struct ieee80211_rate *rates;
1427 u32 supp_rates, prev_rates;
1430 mode = local->sta_scanning ?
1431 local->scan_hw_mode : local->oper_hw_mode;
1432 rates = mode->rates;
1433 num_rates = mode->num_rates;
1436 for (i = 0; i < elems.supp_rates_len +
1437 elems.ext_supp_rates_len; i++) {
1440 if (i < elems.supp_rates_len)
1441 rate = elems.supp_rates[i];
1442 else if (elems.ext_supp_rates)
1443 rate = elems.ext_supp_rates
1444 [i - elems.supp_rates_len];
1445 own_rate = 5 * (rate & 0x7f);
1446 if (mode->mode == MODE_ATHEROS_TURBO)
1448 for (j = 0; j < num_rates; j++)
1449 if (rates[j].rate == own_rate)
1450 supp_rates |= BIT(j);
1453 prev_rates = sta->supp_rates;
1454 sta->supp_rates &= supp_rates;
1455 if (sta->supp_rates == 0) {
1456 /* No matching rates - this should not really happen.
1457 * Make sure that at least one rate is marked
1458 * supported to avoid issues with TX rate ctrl. */
1459 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1461 if (sta->supp_rates != prev_rates) {
1462 printk(KERN_DEBUG "%s: updated supp_rates set for "
1463 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1465 dev->name, MAC_ARG(sta->addr), prev_rates,
1466 supp_rates, sta->supp_rates);
1474 if (elems.ds_params && elems.ds_params_len == 1)
1475 channel = elems.ds_params[0];
1477 channel = rx_status->channel;
1479 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1481 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1486 /* TODO: order by RSSI? */
1487 spin_lock_bh(&local->sta_bss_lock);
1488 list_move_tail(&bss->list, &local->sta_bss_list);
1489 spin_unlock_bh(&local->sta_bss_lock);
1493 if (bss->probe_resp && beacon) {
1494 /* Do not allow beacon to override data from Probe Response. */
1495 ieee80211_rx_bss_put(dev, bss);
1499 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1500 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1501 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1502 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1503 bss->ssid_len = elems.ssid_len;
1506 bss->supp_rates_len = 0;
1507 if (elems.supp_rates) {
1508 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1509 if (clen > elems.supp_rates_len)
1510 clen = elems.supp_rates_len;
1511 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1513 bss->supp_rates_len += clen;
1515 if (elems.ext_supp_rates) {
1516 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1517 if (clen > elems.ext_supp_rates_len)
1518 clen = elems.ext_supp_rates_len;
1519 memcpy(&bss->supp_rates[bss->supp_rates_len],
1520 elems.ext_supp_rates, clen);
1521 bss->supp_rates_len += clen;
1525 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1526 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1528 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1530 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1531 bss->wpa_ie_len = elems.wpa_len + 2;
1533 bss->wpa_ie_len = 0;
1534 } else if (!elems.wpa && bss->wpa_ie) {
1537 bss->wpa_ie_len = 0;
1541 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1542 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1544 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1546 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1547 bss->rsn_ie_len = elems.rsn_len + 2;
1549 bss->rsn_ie_len = 0;
1550 } else if (!elems.rsn && bss->rsn_ie) {
1553 bss->rsn_ie_len = 0;
1556 if (elems.wmm_param &&
1557 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1558 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1560 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1562 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1563 elems.wmm_param_len + 2);
1564 bss->wmm_ie_len = elems.wmm_param_len + 2;
1566 bss->wmm_ie_len = 0;
1567 } else if (!elems.wmm_param && bss->wmm_ie) {
1570 bss->wmm_ie_len = 0;
1574 bss->hw_mode = rx_status->phymode;
1575 bss->channel = channel;
1576 bss->freq = rx_status->freq;
1577 if (channel != rx_status->channel &&
1578 (bss->hw_mode == MODE_IEEE80211G ||
1579 bss->hw_mode == MODE_IEEE80211B) &&
1580 channel >= 1 && channel <= 14) {
1581 static const int freq_list[] = {
1582 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1583 2447, 2452, 2457, 2462, 2467, 2472, 2484
1585 /* IEEE 802.11g/b mode can receive packets from neighboring
1586 * channels, so map the channel into frequency. */
1587 bss->freq = freq_list[channel - 1];
1589 bss->timestamp = timestamp;
1590 bss->last_update = jiffies;
1591 bss->rssi = rx_status->ssi;
1592 bss->signal = rx_status->signal;
1593 bss->noise = rx_status->noise;
1596 ieee80211_rx_bss_put(dev, bss);
1600 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1601 struct ieee80211_mgmt *mgmt,
1603 struct ieee80211_rx_status *rx_status)
1605 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1609 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1610 struct ieee80211_mgmt *mgmt,
1612 struct ieee80211_rx_status *rx_status)
1614 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1615 struct ieee80211_sub_if_data *sdata;
1616 struct ieee80211_if_sta *ifsta;
1619 struct ieee802_11_elems elems;
1621 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1623 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1624 if (sdata->type != IEEE80211_IF_TYPE_STA)
1626 ifsta = &sdata->u.sta;
1628 if (!ifsta->associated ||
1629 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1632 /* Process beacon from the current BSS */
1633 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1637 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1638 &elems) == ParseFailed)
1642 if (elems.erp_info && elems.erp_info_len >= 1) {
1644 (elems.erp_info[0] & ERP_INFO_USE_PROTECTION) != 0;
1647 if (use_protection != !!ifsta->use_protection) {
1648 if (net_ratelimit()) {
1649 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
1652 use_protection ? "enabled" : "disabled",
1653 MAC_ARG(ifsta->bssid));
1655 ifsta->use_protection = use_protection ? 1 : 0;
1656 local->cts_protect_erp_frames = use_protection;
1659 if (elems.wmm_param && ifsta->wmm_enabled) {
1660 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1661 elems.wmm_param_len);
1666 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1667 struct ieee80211_if_sta *ifsta,
1668 struct ieee80211_mgmt *mgmt,
1670 struct ieee80211_rx_status *rx_status)
1672 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1673 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1675 struct sk_buff *skb;
1676 struct ieee80211_mgmt *resp;
1679 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1680 ifsta->state != IEEE80211_IBSS_JOINED ||
1681 len < 24 + 2 || !ifsta->probe_resp)
1684 if (local->ops->tx_last_beacon)
1685 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1689 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1690 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1691 MAC_FMT " (tx_last_beacon=%d)\n",
1692 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1693 MAC_ARG(mgmt->bssid), tx_last_beacon);
1694 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1696 if (!tx_last_beacon)
1699 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1700 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1703 end = ((u8 *) mgmt) + len;
1704 pos = mgmt->u.probe_req.variable;
1705 if (pos[0] != WLAN_EID_SSID ||
1706 pos + 2 + pos[1] > end) {
1707 if (net_ratelimit()) {
1708 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1709 "from " MAC_FMT "\n",
1710 dev->name, MAC_ARG(mgmt->sa));
1715 (pos[1] != ifsta->ssid_len ||
1716 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1717 /* Ignore ProbeReq for foreign SSID */
1721 /* Reply with ProbeResp */
1722 skb = skb_copy(ifsta->probe_resp, GFP_ATOMIC);
1726 resp = (struct ieee80211_mgmt *) skb->data;
1727 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1728 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1729 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1730 dev->name, MAC_ARG(resp->da));
1731 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1732 ieee80211_sta_tx(dev, skb, 0);
1736 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1737 struct ieee80211_rx_status *rx_status)
1739 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1740 struct ieee80211_sub_if_data *sdata;
1741 struct ieee80211_if_sta *ifsta;
1742 struct ieee80211_mgmt *mgmt;
1748 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1749 ifsta = &sdata->u.sta;
1751 mgmt = (struct ieee80211_mgmt *) skb->data;
1752 fc = le16_to_cpu(mgmt->frame_control);
1754 switch (fc & IEEE80211_FCTL_STYPE) {
1755 case IEEE80211_STYPE_PROBE_REQ:
1756 case IEEE80211_STYPE_PROBE_RESP:
1757 case IEEE80211_STYPE_BEACON:
1758 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1759 case IEEE80211_STYPE_AUTH:
1760 case IEEE80211_STYPE_ASSOC_RESP:
1761 case IEEE80211_STYPE_REASSOC_RESP:
1762 case IEEE80211_STYPE_DEAUTH:
1763 case IEEE80211_STYPE_DISASSOC:
1764 skb_queue_tail(&ifsta->skb_queue, skb);
1765 queue_work(local->hw.workqueue, &ifsta->work);
1768 printk(KERN_DEBUG "%s: received unknown management frame - "
1769 "stype=%d\n", dev->name,
1770 (fc & IEEE80211_FCTL_STYPE) >> 4);
1779 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1780 struct sk_buff *skb)
1782 struct ieee80211_rx_status *rx_status;
1783 struct ieee80211_sub_if_data *sdata;
1784 struct ieee80211_if_sta *ifsta;
1785 struct ieee80211_mgmt *mgmt;
1788 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1789 ifsta = &sdata->u.sta;
1791 rx_status = (struct ieee80211_rx_status *) skb->cb;
1792 mgmt = (struct ieee80211_mgmt *) skb->data;
1793 fc = le16_to_cpu(mgmt->frame_control);
1795 switch (fc & IEEE80211_FCTL_STYPE) {
1796 case IEEE80211_STYPE_PROBE_REQ:
1797 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1800 case IEEE80211_STYPE_PROBE_RESP:
1801 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1803 case IEEE80211_STYPE_BEACON:
1804 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1806 case IEEE80211_STYPE_AUTH:
1807 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1809 case IEEE80211_STYPE_ASSOC_RESP:
1810 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1812 case IEEE80211_STYPE_REASSOC_RESP:
1813 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1815 case IEEE80211_STYPE_DEAUTH:
1816 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1818 case IEEE80211_STYPE_DISASSOC:
1819 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1827 void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1828 struct ieee80211_rx_status *rx_status)
1830 struct ieee80211_mgmt *mgmt;
1833 if (skb->len < 24) {
1838 mgmt = (struct ieee80211_mgmt *) skb->data;
1839 fc = le16_to_cpu(mgmt->frame_control);
1841 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1842 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1843 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1844 skb->len, rx_status);
1845 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1846 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1855 static int ieee80211_sta_active_ibss(struct net_device *dev)
1857 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1859 struct sta_info *sta;
1861 spin_lock_bh(&local->sta_lock);
1862 list_for_each_entry(sta, &local->sta_list, list) {
1863 if (sta->dev == dev &&
1864 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1870 spin_unlock_bh(&local->sta_lock);
1876 static void ieee80211_sta_expire(struct net_device *dev)
1878 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1879 struct sta_info *sta, *tmp;
1881 spin_lock_bh(&local->sta_lock);
1882 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1883 if (time_after(jiffies, sta->last_rx +
1884 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1885 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1886 "\n", dev->name, MAC_ARG(sta->addr));
1887 sta_info_free(sta, 1);
1889 spin_unlock_bh(&local->sta_lock);
1893 static void ieee80211_sta_merge_ibss(struct net_device *dev,
1894 struct ieee80211_if_sta *ifsta)
1896 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1898 ieee80211_sta_expire(dev);
1899 if (ieee80211_sta_active_ibss(dev))
1902 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1903 "IBSS networks with same SSID (merge)\n", dev->name);
1904 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1908 void ieee80211_sta_timer(unsigned long data)
1910 struct ieee80211_sub_if_data *sdata =
1911 (struct ieee80211_sub_if_data *) data;
1912 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1913 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1915 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1916 queue_work(local->hw.workqueue, &ifsta->work);
1920 void ieee80211_sta_work(struct work_struct *work)
1922 struct ieee80211_sub_if_data *sdata =
1923 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1924 struct net_device *dev = sdata->dev;
1925 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1926 struct ieee80211_if_sta *ifsta;
1927 struct sk_buff *skb;
1929 if (!netif_running(dev))
1932 if (local->sta_scanning)
1935 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1936 sdata->type != IEEE80211_IF_TYPE_IBSS) {
1937 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
1938 "(type=%d)\n", dev->name, sdata->type);
1941 ifsta = &sdata->u.sta;
1943 while ((skb = skb_dequeue(&ifsta->skb_queue)))
1944 ieee80211_sta_rx_queued_mgmt(dev, skb);
1946 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1947 ifsta->state != IEEE80211_ASSOCIATE &&
1948 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
1949 ieee80211_sta_start_scan(dev, NULL, 0);
1953 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
1954 if (ieee80211_sta_config_auth(dev, ifsta))
1956 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1957 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
1960 switch (ifsta->state) {
1961 case IEEE80211_DISABLED:
1963 case IEEE80211_AUTHENTICATE:
1964 ieee80211_authenticate(dev, ifsta);
1966 case IEEE80211_ASSOCIATE:
1967 ieee80211_associate(dev, ifsta);
1969 case IEEE80211_ASSOCIATED:
1970 ieee80211_associated(dev, ifsta);
1972 case IEEE80211_IBSS_SEARCH:
1973 ieee80211_sta_find_ibss(dev, ifsta);
1975 case IEEE80211_IBSS_JOINED:
1976 ieee80211_sta_merge_ibss(dev, ifsta);
1979 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
1984 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1985 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
1986 "mixed-cell disabled - disassociate\n", dev->name);
1988 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
1989 ieee80211_set_disassoc(dev, ifsta, 0);
1994 static void ieee80211_sta_reset_auth(struct net_device *dev,
1995 struct ieee80211_if_sta *ifsta)
1997 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1999 if (local->ops->reset_tsf) {
2000 /* Reset own TSF to allow time synchronization work. */
2001 local->ops->reset_tsf(local_to_hw(local));
2004 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2007 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2008 ifsta->auth_alg = WLAN_AUTH_OPEN;
2009 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2010 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2011 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2012 ifsta->auth_alg = WLAN_AUTH_LEAP;
2014 ifsta->auth_alg = WLAN_AUTH_OPEN;
2015 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2017 ifsta->auth_transaction = -1;
2018 ifsta->associated = ifsta->auth_tries = ifsta->assoc_tries = 0;
2019 netif_carrier_off(dev);
2023 void ieee80211_sta_req_auth(struct net_device *dev,
2024 struct ieee80211_if_sta *ifsta)
2026 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2027 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2029 if (sdata->type != IEEE80211_IF_TYPE_STA)
2032 if ((ifsta->bssid_set || ifsta->auto_bssid_sel) &&
2033 (ifsta->ssid_set || ifsta->auto_ssid_sel)) {
2034 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2035 queue_work(local->hw.workqueue, &ifsta->work);
2039 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2040 const char *ssid, int ssid_len)
2042 int tmp, hidden_ssid;
2044 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2047 if (ifsta->auto_bssid_sel)
2053 if (ssid[tmp] != '\0') {
2059 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2062 if (ssid_len == 1 && ssid[0] == ' ')
2068 static int ieee80211_sta_config_auth(struct net_device *dev,
2069 struct ieee80211_if_sta *ifsta)
2071 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2072 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2073 struct ieee80211_sta_bss *bss, *selected = NULL;
2074 int top_rssi = 0, freq;
2078 if (!ifsta->auto_channel_sel && !ifsta->auto_bssid_sel &&
2079 !ifsta->auto_ssid_sel) {
2080 ifsta->state = IEEE80211_AUTHENTICATE;
2082 ieee80211_sta_reset_auth(dev, ifsta);
2086 spin_lock_bh(&local->sta_bss_lock);
2087 freq = local->oper_channel->freq;
2088 list_for_each_entry(bss, &local->sta_bss_list, list) {
2089 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2092 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2093 !!sdata->default_key)
2096 if (!ifsta->auto_channel_sel && bss->freq != freq)
2099 if (!ifsta->auto_bssid_sel &&
2100 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2103 if (!ifsta->auto_ssid_sel &&
2104 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2107 if (!selected || top_rssi < bss->rssi) {
2109 top_rssi = bss->rssi;
2113 atomic_inc(&selected->users);
2114 spin_unlock_bh(&local->sta_bss_lock);
2117 ieee80211_set_channel(local, -1, selected->freq);
2118 if (!ifsta->ssid_set)
2119 ieee80211_sta_set_ssid(dev, selected->ssid,
2120 selected->ssid_len);
2121 ieee80211_sta_set_bssid(dev, selected->bssid);
2122 ieee80211_rx_bss_put(dev, selected);
2123 ifsta->state = IEEE80211_AUTHENTICATE;
2125 ieee80211_sta_reset_auth(dev, ifsta);
2128 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2129 ieee80211_sta_start_scan(dev, NULL, 0);
2130 ifsta->state = IEEE80211_AUTHENTICATE;
2131 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2133 ifsta->state = IEEE80211_DISABLED;
2139 static int ieee80211_sta_join_ibss(struct net_device *dev,
2140 struct ieee80211_if_sta *ifsta,
2141 struct ieee80211_sta_bss *bss)
2143 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2144 int res, rates, i, j;
2145 struct sk_buff *skb;
2146 struct ieee80211_mgmt *mgmt;
2147 struct ieee80211_tx_control control;
2148 struct ieee80211_rate *rate;
2149 struct ieee80211_hw_mode *mode;
2150 struct rate_control_extra extra;
2152 struct ieee80211_sub_if_data *sdata;
2154 /* Remove possible STA entries from other IBSS networks. */
2155 sta_info_flush(local, NULL);
2157 if (local->ops->reset_tsf) {
2158 /* Reset own TSF to allow time synchronization work. */
2159 local->ops->reset_tsf(local_to_hw(local));
2161 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2162 res = ieee80211_if_config(dev);
2166 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2168 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2169 sdata->drop_unencrypted = bss->capability &
2170 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2172 res = ieee80211_set_channel(local, -1, bss->freq);
2174 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2175 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2176 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2177 local->hw.conf.freq);
2181 /* Set beacon template based on scan results */
2182 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2187 skb_reserve(skb, local->hw.extra_tx_headroom);
2189 mgmt = (struct ieee80211_mgmt *)
2190 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2191 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2192 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2193 IEEE80211_STYPE_BEACON);
2194 memset(mgmt->da, 0xff, ETH_ALEN);
2195 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2196 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2197 mgmt->u.beacon.beacon_int =
2198 cpu_to_le16(local->hw.conf.beacon_int);
2199 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2201 pos = skb_put(skb, 2 + ifsta->ssid_len);
2202 *pos++ = WLAN_EID_SSID;
2203 *pos++ = ifsta->ssid_len;
2204 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2206 rates = bss->supp_rates_len;
2209 pos = skb_put(skb, 2 + rates);
2210 *pos++ = WLAN_EID_SUPP_RATES;
2212 memcpy(pos, bss->supp_rates, rates);
2214 pos = skb_put(skb, 2 + 1);
2215 *pos++ = WLAN_EID_DS_PARAMS;
2217 *pos++ = bss->channel;
2219 pos = skb_put(skb, 2 + 2);
2220 *pos++ = WLAN_EID_IBSS_PARAMS;
2222 /* FIX: set ATIM window based on scan results */
2226 if (bss->supp_rates_len > 8) {
2227 rates = bss->supp_rates_len - 8;
2228 pos = skb_put(skb, 2 + rates);
2229 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2231 memcpy(pos, &bss->supp_rates[8], rates);
2234 memset(&control, 0, sizeof(control));
2235 memset(&extra, 0, sizeof(extra));
2236 extra.mode = local->oper_hw_mode;
2237 rate = rate_control_get_rate(local, dev, skb, &extra);
2239 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2240 "for IBSS beacon\n", dev->name);
2243 control.tx_rate = (local->short_preamble &&
2244 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2245 rate->val2 : rate->val;
2246 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2247 control.power_level = local->hw.conf.power_level;
2248 control.flags |= IEEE80211_TXCTL_NO_ACK;
2249 control.retry_limit = 1;
2251 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2252 if (ifsta->probe_resp) {
2253 mgmt = (struct ieee80211_mgmt *)
2254 ifsta->probe_resp->data;
2255 mgmt->frame_control =
2256 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2257 IEEE80211_STYPE_PROBE_RESP);
2259 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2260 "template for IBSS\n", dev->name);
2263 if (local->ops->beacon_update &&
2264 local->ops->beacon_update(local_to_hw(local),
2265 skb, &control) == 0) {
2266 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2267 "template based on scan results\n", dev->name);
2272 mode = local->oper_hw_mode;
2273 for (i = 0; i < bss->supp_rates_len; i++) {
2274 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2275 if (mode->mode == MODE_ATHEROS_TURBO)
2277 for (j = 0; j < mode->num_rates; j++)
2278 if (mode->rates[j].rate == bitrate)
2281 ifsta->supp_rates_bits = rates;
2285 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2286 "template\n", dev->name);
2290 ifsta->state = IEEE80211_IBSS_JOINED;
2291 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2293 ieee80211_rx_bss_put(dev, bss);
2299 static int ieee80211_sta_create_ibss(struct net_device *dev,
2300 struct ieee80211_if_sta *ifsta)
2302 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2303 struct ieee80211_sta_bss *bss;
2304 struct ieee80211_sub_if_data *sdata;
2305 struct ieee80211_hw_mode *mode;
2306 u8 bssid[ETH_ALEN], *pos;
2310 /* Easier testing, use fixed BSSID. */
2311 memset(bssid, 0xfe, ETH_ALEN);
2313 /* Generate random, not broadcast, locally administered BSSID. Mix in
2314 * own MAC address to make sure that devices that do not have proper
2315 * random number generator get different BSSID. */
2316 get_random_bytes(bssid, ETH_ALEN);
2317 for (i = 0; i < ETH_ALEN; i++)
2318 bssid[i] ^= dev->dev_addr[i];
2323 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2324 dev->name, MAC_ARG(bssid));
2326 bss = ieee80211_rx_bss_add(dev, bssid);
2330 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2331 mode = local->oper_hw_mode;
2333 if (local->hw.conf.beacon_int == 0)
2334 local->hw.conf.beacon_int = 100;
2335 bss->beacon_int = local->hw.conf.beacon_int;
2336 bss->hw_mode = local->hw.conf.phymode;
2337 bss->channel = local->hw.conf.channel;
2338 bss->freq = local->hw.conf.freq;
2339 bss->last_update = jiffies;
2340 bss->capability = WLAN_CAPABILITY_IBSS;
2341 if (sdata->default_key) {
2342 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2344 sdata->drop_unencrypted = 0;
2345 bss->supp_rates_len = mode->num_rates;
2346 pos = bss->supp_rates;
2347 for (i = 0; i < mode->num_rates; i++) {
2348 int rate = mode->rates[i].rate;
2349 if (mode->mode == MODE_ATHEROS_TURBO)
2351 *pos++ = (u8) (rate / 5);
2354 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2358 static int ieee80211_sta_find_ibss(struct net_device *dev,
2359 struct ieee80211_if_sta *ifsta)
2361 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2362 struct ieee80211_sta_bss *bss;
2367 if (ifsta->ssid_len == 0)
2370 active_ibss = ieee80211_sta_active_ibss(dev);
2371 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2372 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2373 dev->name, active_ibss);
2374 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2375 spin_lock_bh(&local->sta_bss_lock);
2376 list_for_each_entry(bss, &local->sta_bss_list, list) {
2377 if (ifsta->ssid_len != bss->ssid_len ||
2378 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2379 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2381 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2382 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2383 MAC_ARG(bss->bssid));
2384 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2385 memcpy(bssid, bss->bssid, ETH_ALEN);
2387 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2390 spin_unlock_bh(&local->sta_bss_lock);
2392 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2393 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2394 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2395 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2396 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2397 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2398 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2399 " based on configured SSID\n",
2400 dev->name, MAC_ARG(bssid));
2401 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2403 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2404 printk(KERN_DEBUG " did not try to join ibss\n");
2405 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2407 /* Selected IBSS not found in current scan results - try to scan */
2408 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2409 !ieee80211_sta_active_ibss(dev)) {
2410 mod_timer(&ifsta->timer, jiffies +
2411 IEEE80211_IBSS_MERGE_INTERVAL);
2412 } else if (time_after(jiffies, local->last_scan_completed +
2413 IEEE80211_SCAN_INTERVAL)) {
2414 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2415 "join\n", dev->name);
2416 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2418 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2419 int interval = IEEE80211_SCAN_INTERVAL;
2421 if (time_after(jiffies, ifsta->ibss_join_req +
2422 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2423 if (ifsta->create_ibss &&
2424 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2425 return ieee80211_sta_create_ibss(dev, ifsta);
2426 if (ifsta->create_ibss) {
2427 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2428 " configured channel %d (%d MHz)\n",
2429 dev->name, local->hw.conf.channel,
2430 local->hw.conf.freq);
2433 /* No IBSS found - decrease scan interval and continue
2435 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2438 ifsta->state = IEEE80211_IBSS_SEARCH;
2439 mod_timer(&ifsta->timer, jiffies + interval);
2447 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2449 struct ieee80211_sub_if_data *sdata;
2450 struct ieee80211_if_sta *ifsta;
2451 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2453 if (len > IEEE80211_MAX_SSID_LEN)
2456 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2458 if (local->ops->conf_tx) {
2459 struct ieee80211_tx_queue_params qparam;
2462 memset(&qparam, 0, sizeof(qparam));
2463 /* TODO: are these ok defaults for all hw_modes? */
2466 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2467 qparam.cw_max = 1023;
2468 qparam.burst_time = 0;
2469 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2471 local->ops->conf_tx(local_to_hw(local),
2472 i + IEEE80211_TX_QUEUE_DATA0,
2475 /* IBSS uses different parameters for Beacon sending */
2479 local->ops->conf_tx(local_to_hw(local),
2480 IEEE80211_TX_QUEUE_BEACON, &qparam);
2483 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2484 ifsta = &sdata->u.sta;
2486 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2487 ifsta->prev_bssid_set = 0;
2488 memcpy(ifsta->ssid, ssid, len);
2489 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2490 ifsta->ssid_len = len;
2492 ifsta->ssid_set = len ? 1 : 0;
2493 if (sdata->type == IEEE80211_IF_TYPE_IBSS && !ifsta->bssid_set) {
2494 ifsta->ibss_join_req = jiffies;
2495 ifsta->state = IEEE80211_IBSS_SEARCH;
2496 return ieee80211_sta_find_ibss(dev, ifsta);
2502 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2504 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2505 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2506 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2507 *len = ifsta->ssid_len;
2512 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2514 struct ieee80211_sub_if_data *sdata;
2515 struct ieee80211_if_sta *ifsta;
2518 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2519 ifsta = &sdata->u.sta;
2521 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2522 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2523 res = ieee80211_if_config(dev);
2525 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2526 "the low-level driver\n", dev->name);
2531 if (!is_valid_ether_addr(bssid))
2532 ifsta->bssid_set = 0;
2534 ifsta->bssid_set = 1;
2539 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2540 struct ieee80211_sub_if_data *sdata,
2543 struct sk_buff *skb;
2544 struct ieee80211_hdr *nullfunc;
2547 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2549 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2550 "frame\n", sdata->dev->name);
2553 skb_reserve(skb, local->hw.extra_tx_headroom);
2555 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2556 memset(nullfunc, 0, 24);
2557 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2558 IEEE80211_FCTL_TODS;
2560 fc |= IEEE80211_FCTL_PM;
2561 nullfunc->frame_control = cpu_to_le16(fc);
2562 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2563 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2564 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2566 ieee80211_sta_tx(sdata->dev, skb, 0);
2570 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2572 struct ieee80211_local *local = hw_to_local(hw);
2573 struct net_device *dev = local->scan_dev;
2574 struct ieee80211_sub_if_data *sdata;
2575 union iwreq_data wrqu;
2577 local->last_scan_completed = jiffies;
2579 local->sta_scanning = 0;
2581 if (ieee80211_hw_config(local))
2582 printk(KERN_DEBUG "%s: failed to restore operational"
2583 "channel after scan\n", dev->name);
2585 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2586 ieee80211_if_config(dev))
2587 printk(KERN_DEBUG "%s: failed to restore operational"
2588 "BSSID after scan\n", dev->name);
2590 memset(&wrqu, 0, sizeof(wrqu));
2591 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2593 read_lock(&local->sub_if_lock);
2594 list_for_each_entry(sdata, &local->sub_if_list, list) {
2596 /* No need to wake the master device. */
2597 if (sdata->dev == local->mdev)
2600 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2601 if (sdata->u.sta.associated)
2602 ieee80211_send_nullfunc(local, sdata, 0);
2603 ieee80211_sta_timer((unsigned long)sdata);
2606 netif_wake_queue(sdata->dev);
2608 read_unlock(&local->sub_if_lock);
2610 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2611 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2612 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2613 if (!ifsta->bssid_set ||
2614 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2615 !ieee80211_sta_active_ibss(dev)))
2616 ieee80211_sta_find_ibss(dev, ifsta);
2619 EXPORT_SYMBOL(ieee80211_scan_completed);
2621 void ieee80211_sta_scan_work(struct work_struct *work)
2623 struct ieee80211_local *local =
2624 container_of(work, struct ieee80211_local, scan_work.work);
2625 struct net_device *dev = local->scan_dev;
2626 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2627 struct ieee80211_hw_mode *mode;
2628 struct ieee80211_channel *chan;
2630 unsigned long next_delay = 0;
2632 if (!local->sta_scanning)
2635 switch (local->scan_state) {
2636 case SCAN_SET_CHANNEL:
2637 mode = local->scan_hw_mode;
2638 if (local->scan_hw_mode->list.next == &local->modes_list &&
2639 local->scan_channel_idx >= mode->num_channels) {
2640 ieee80211_scan_completed(local_to_hw(local));
2643 skip = !(local->enabled_modes & (1 << mode->mode));
2644 chan = &mode->channels[local->scan_channel_idx];
2645 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2646 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2647 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2648 (local->hw_modes & local->enabled_modes &
2649 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2654 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2655 dev->name, chan->chan, chan->freq);
2658 local->scan_channel = chan;
2659 if (ieee80211_hw_config(local)) {
2660 printk(KERN_DEBUG "%s: failed to set channel "
2661 "%d (%d MHz) for scan\n", dev->name,
2662 chan->chan, chan->freq);
2667 local->scan_channel_idx++;
2668 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2669 if (local->scan_hw_mode->list.next != &local->modes_list) {
2670 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2671 struct ieee80211_hw_mode,
2673 local->scan_channel_idx = 0;
2680 next_delay = IEEE80211_PROBE_DELAY +
2681 usecs_to_jiffies(local->hw.channel_change_time);
2682 local->scan_state = SCAN_SEND_PROBE;
2684 case SCAN_SEND_PROBE:
2685 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2686 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2687 local->scan_ssid_len);
2688 next_delay = IEEE80211_CHANNEL_TIME;
2690 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2691 local->scan_state = SCAN_SET_CHANNEL;
2695 if (local->sta_scanning)
2696 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2701 static int ieee80211_sta_start_scan(struct net_device *dev,
2702 u8 *ssid, size_t ssid_len)
2704 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2705 struct ieee80211_sub_if_data *sdata;
2707 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2710 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2711 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2714 * ScanType: ACTIVE, PASSIVE
2715 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2716 * a Probe frame during active scanning
2718 * MinChannelTime (>= ProbeDelay), in TU
2719 * MaxChannelTime: (>= MinChannelTime), in TU
2722 /* MLME-SCAN.confirm
2724 * ResultCode: SUCCESS, INVALID_PARAMETERS
2727 if (local->sta_scanning) {
2728 if (local->scan_dev == dev)
2733 if (local->ops->hw_scan) {
2734 int rc = local->ops->hw_scan(local_to_hw(local),
2737 local->sta_scanning = 1;
2738 local->scan_dev = dev;
2743 local->sta_scanning = 1;
2745 read_lock(&local->sub_if_lock);
2746 list_for_each_entry(sdata, &local->sub_if_list, list) {
2748 /* Don't stop the master interface, otherwise we can't transmit
2750 if (sdata->dev == local->mdev)
2753 netif_stop_queue(sdata->dev);
2754 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2755 sdata->u.sta.associated)
2756 ieee80211_send_nullfunc(local, sdata, 1);
2758 read_unlock(&local->sub_if_lock);
2761 local->scan_ssid_len = ssid_len;
2762 memcpy(local->scan_ssid, ssid, ssid_len);
2764 local->scan_ssid_len = 0;
2765 local->scan_state = SCAN_SET_CHANNEL;
2766 local->scan_hw_mode = list_entry(local->modes_list.next,
2767 struct ieee80211_hw_mode,
2769 local->scan_channel_idx = 0;
2770 local->scan_dev = dev;
2772 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2773 ieee80211_if_config(dev))
2774 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2777 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2778 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2779 IEEE80211_CHANNEL_TIME);
2785 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2787 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2788 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2789 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2791 if (sdata->type != IEEE80211_IF_TYPE_STA)
2792 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2794 if (local->sta_scanning) {
2795 if (local->scan_dev == dev)
2800 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2801 queue_work(local->hw.workqueue, &ifsta->work);
2806 ieee80211_sta_scan_result(struct net_device *dev,
2807 struct ieee80211_sta_bss *bss,
2808 char *current_ev, char *end_buf)
2810 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2811 struct iw_event iwe;
2813 if (time_after(jiffies,
2814 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2817 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2820 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2821 !bss->wpa_ie && !bss->rsn_ie)
2824 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2825 (local->scan_ssid_len != bss->ssid_len ||
2826 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2829 memset(&iwe, 0, sizeof(iwe));
2830 iwe.cmd = SIOCGIWAP;
2831 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2832 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2833 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2836 memset(&iwe, 0, sizeof(iwe));
2837 iwe.cmd = SIOCGIWESSID;
2838 iwe.u.data.length = bss->ssid_len;
2839 iwe.u.data.flags = 1;
2840 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2843 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2844 memset(&iwe, 0, sizeof(iwe));
2845 iwe.cmd = SIOCGIWMODE;
2846 if (bss->capability & WLAN_CAPABILITY_ESS)
2847 iwe.u.mode = IW_MODE_MASTER;
2849 iwe.u.mode = IW_MODE_ADHOC;
2850 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2854 memset(&iwe, 0, sizeof(iwe));
2855 iwe.cmd = SIOCGIWFREQ;
2856 iwe.u.freq.m = bss->channel;
2858 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2860 iwe.u.freq.m = bss->freq * 100000;
2862 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2865 memset(&iwe, 0, sizeof(iwe));
2867 iwe.u.qual.qual = bss->signal;
2868 iwe.u.qual.level = bss->rssi;
2869 iwe.u.qual.noise = bss->noise;
2870 iwe.u.qual.updated = local->wstats_flags;
2871 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2874 memset(&iwe, 0, sizeof(iwe));
2875 iwe.cmd = SIOCGIWENCODE;
2876 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2877 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2879 iwe.u.data.flags = IW_ENCODE_DISABLED;
2880 iwe.u.data.length = 0;
2881 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2883 if (bss && bss->wpa_ie) {
2884 memset(&iwe, 0, sizeof(iwe));
2885 iwe.cmd = IWEVGENIE;
2886 iwe.u.data.length = bss->wpa_ie_len;
2887 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2891 if (bss && bss->rsn_ie) {
2892 memset(&iwe, 0, sizeof(iwe));
2893 iwe.cmd = IWEVGENIE;
2894 iwe.u.data.length = bss->rsn_ie_len;
2895 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2899 if (bss && bss->supp_rates_len > 0) {
2900 /* display all supported rates in readable format */
2901 char *p = current_ev + IW_EV_LCP_LEN;
2904 memset(&iwe, 0, sizeof(iwe));
2905 iwe.cmd = SIOCGIWRATE;
2906 /* Those two flags are ignored... */
2907 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2909 for (i = 0; i < bss->supp_rates_len; i++) {
2910 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2912 p = iwe_stream_add_value(current_ev, p,
2913 end_buf, &iwe, IW_EV_PARAM_LEN);
2920 buf = kmalloc(30, GFP_ATOMIC);
2922 memset(&iwe, 0, sizeof(iwe));
2923 iwe.cmd = IWEVCUSTOM;
2924 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
2925 iwe.u.data.length = strlen(buf);
2926 current_ev = iwe_stream_add_point(current_ev, end_buf,
2935 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
2938 buf = kmalloc(100, GFP_ATOMIC);
2942 memset(&iwe, 0, sizeof(iwe));
2943 iwe.cmd = IWEVCUSTOM;
2944 sprintf(buf, "bcn_int=%d", bss->beacon_int);
2945 iwe.u.data.length = strlen(buf);
2946 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2949 memset(&iwe, 0, sizeof(iwe));
2950 iwe.cmd = IWEVCUSTOM;
2951 sprintf(buf, "capab=0x%04x", bss->capability);
2952 iwe.u.data.length = strlen(buf);
2953 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2964 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
2966 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2967 char *current_ev = buf;
2968 char *end_buf = buf + len;
2969 struct ieee80211_sta_bss *bss;
2971 spin_lock_bh(&local->sta_bss_lock);
2972 list_for_each_entry(bss, &local->sta_bss_list, list) {
2973 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
2974 spin_unlock_bh(&local->sta_bss_lock);
2977 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
2980 spin_unlock_bh(&local->sta_bss_lock);
2981 return current_ev - buf;
2985 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
2987 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2988 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2989 kfree(ifsta->extra_ie);
2991 ifsta->extra_ie = NULL;
2992 ifsta->extra_ie_len = 0;
2995 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2996 if (!ifsta->extra_ie) {
2997 ifsta->extra_ie_len = 0;
3000 memcpy(ifsta->extra_ie, ie, len);
3001 ifsta->extra_ie_len = len;
3006 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3007 struct sk_buff *skb, u8 *bssid,
3010 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3011 struct sta_info *sta;
3012 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3014 /* TODO: Could consider removing the least recently used entry and
3015 * allow new one to be added. */
3016 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3017 if (net_ratelimit()) {
3018 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3019 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3024 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3025 local->mdev->name, MAC_ARG(addr), dev->name);
3027 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3031 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3033 rate_control_rate_init(sta, local);
3035 return sta; /* caller will call sta_info_put() */
3039 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3041 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3042 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3044 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3047 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3048 sdata->type != IEEE80211_IF_TYPE_IBSS)
3051 ieee80211_send_deauth(dev, ifsta, reason);
3052 ieee80211_set_disassoc(dev, ifsta, 1);
3057 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3059 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3060 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3062 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3065 if (sdata->type != IEEE80211_IF_TYPE_STA)
3068 if (!ifsta->associated)
3071 ieee80211_send_disassoc(dev, ifsta, reason);
3072 ieee80211_set_disassoc(dev, ifsta, 0);