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 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
19 #include <linux/delay.h>
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 <net/iw_handler.h>
28 #include <asm/types.h>
30 #include <net/mac80211.h>
31 #include "ieee80211_i.h"
32 #include "ieee80211_rate.h"
33 #include "ieee80211_led.h"
35 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
36 #define IEEE80211_AUTH_MAX_TRIES 3
37 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38 #define IEEE80211_ASSOC_MAX_TRIES 3
39 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
40 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
41 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
42 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
43 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
44 #define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
46 #define IEEE80211_PROBE_DELAY (HZ / 33)
47 #define IEEE80211_CHANNEL_TIME (HZ / 33)
48 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
49 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
50 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
51 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
53 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
56 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
58 #define ERP_INFO_USE_PROTECTION BIT(1)
60 /* mgmt header + 1 byte action code */
61 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
63 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
64 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
65 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
66 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
67 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
69 /* next values represent the buffer size for A-MPDU frame.
70 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
71 #define IEEE80211_MIN_AMPDU_BUF 0x8
72 #define IEEE80211_MAX_AMPDU_BUF 0x40
74 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
75 u8 *ssid, size_t ssid_len);
76 static struct ieee80211_sta_bss *
77 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
78 u8 *ssid, u8 ssid_len);
79 static void ieee80211_rx_bss_put(struct net_device *dev,
80 struct ieee80211_sta_bss *bss);
81 static int ieee80211_sta_find_ibss(struct net_device *dev,
82 struct ieee80211_if_sta *ifsta);
83 static int ieee80211_sta_wep_configured(struct net_device *dev);
84 static int ieee80211_sta_start_scan(struct net_device *dev,
85 u8 *ssid, size_t ssid_len);
86 static int ieee80211_sta_config_auth(struct net_device *dev,
87 struct ieee80211_if_sta *ifsta);
90 /* Parsed Information Elements */
91 struct ieee802_11_elems {
109 /* length of them, respectively */
121 u8 ext_supp_rates_len;
128 static void ieee802_11_parse_elems(u8 *start, size_t len,
129 struct ieee802_11_elems *elems)
134 memset(elems, 0, sizeof(*elems));
149 elems->ssid_len = elen;
151 case WLAN_EID_SUPP_RATES:
152 elems->supp_rates = pos;
153 elems->supp_rates_len = elen;
155 case WLAN_EID_FH_PARAMS:
156 elems->fh_params = pos;
157 elems->fh_params_len = elen;
159 case WLAN_EID_DS_PARAMS:
160 elems->ds_params = pos;
161 elems->ds_params_len = elen;
163 case WLAN_EID_CF_PARAMS:
164 elems->cf_params = pos;
165 elems->cf_params_len = elen;
169 elems->tim_len = elen;
171 case WLAN_EID_IBSS_PARAMS:
172 elems->ibss_params = pos;
173 elems->ibss_params_len = elen;
175 case WLAN_EID_CHALLENGE:
176 elems->challenge = pos;
177 elems->challenge_len = elen;
180 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
182 /* Microsoft OUI (00:50:F2) */
184 /* OUI Type 1 - WPA IE */
186 elems->wpa_len = elen;
187 } else if (elen >= 5 && pos[3] == 2) {
189 elems->wmm_info = pos;
190 elems->wmm_info_len = elen;
191 } else if (pos[4] == 1) {
192 elems->wmm_param = pos;
193 elems->wmm_param_len = elen;
200 elems->rsn_len = elen;
202 case WLAN_EID_ERP_INFO:
203 elems->erp_info = pos;
204 elems->erp_info_len = elen;
206 case WLAN_EID_EXT_SUPP_RATES:
207 elems->ext_supp_rates = pos;
208 elems->ext_supp_rates_len = elen;
210 case WLAN_EID_HT_CAPABILITY:
211 elems->ht_cap_elem = pos;
212 elems->ht_cap_elem_len = elen;
214 case WLAN_EID_HT_EXTRA_INFO:
215 elems->ht_info_elem = pos;
216 elems->ht_info_elem_len = elen;
228 static int ecw2cw(int ecw)
238 static void ieee80211_sta_wmm_params(struct net_device *dev,
239 struct ieee80211_if_sta *ifsta,
240 u8 *wmm_param, size_t wmm_param_len)
242 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
243 struct ieee80211_tx_queue_params params;
248 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
250 count = wmm_param[6] & 0x0f;
251 if (count == ifsta->wmm_last_param_set)
253 ifsta->wmm_last_param_set = count;
256 left = wmm_param_len - 8;
258 memset(¶ms, 0, sizeof(params));
260 if (!local->ops->conf_tx)
264 for (; left >= 4; left -= 4, pos += 4) {
265 int aci = (pos[0] >> 5) & 0x03;
266 int acm = (pos[0] >> 4) & 0x01;
271 queue = IEEE80211_TX_QUEUE_DATA3;
273 local->wmm_acm |= BIT(0) | BIT(3);
277 queue = IEEE80211_TX_QUEUE_DATA1;
279 local->wmm_acm |= BIT(4) | BIT(5);
283 queue = IEEE80211_TX_QUEUE_DATA0;
285 local->wmm_acm |= BIT(6) | BIT(7);
290 queue = IEEE80211_TX_QUEUE_DATA2;
292 local->wmm_acm |= BIT(1) | BIT(2);
297 params.aifs = pos[0] & 0x0f;
298 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
299 params.cw_min = ecw2cw(pos[1] & 0x0f);
300 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
301 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
302 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
303 "cWmin=%d cWmax=%d burst=%d\n",
304 dev->name, queue, aci, acm, params.aifs, params.cw_min,
305 params.cw_max, params.burst_time);
306 /* TODO: handle ACM (block TX, fallback to next lowest allowed
308 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
309 printk(KERN_DEBUG "%s: failed to set TX queue "
310 "parameters for queue %d\n", dev->name, queue);
316 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
319 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
320 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
321 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
322 bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
323 DECLARE_MAC_BUF(mac);
326 if (use_protection != bss_conf->use_cts_prot) {
327 if (net_ratelimit()) {
328 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
331 use_protection ? "enabled" : "disabled",
332 print_mac(mac, ifsta->bssid));
334 bss_conf->use_cts_prot = use_protection;
335 changed |= BSS_CHANGED_ERP_CTS_PROT;
338 if (preamble_mode != bss_conf->use_short_preamble) {
339 if (net_ratelimit()) {
340 printk(KERN_DEBUG "%s: switched to %s barker preamble"
343 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
345 print_mac(mac, ifsta->bssid));
347 bss_conf->use_short_preamble = preamble_mode;
348 changed |= BSS_CHANGED_ERP_PREAMBLE;
354 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
355 struct ieee80211_ht_info *ht_info)
361 memset(ht_info, 0, sizeof(*ht_info));
364 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
366 ht_info->ht_supported = 1;
367 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
368 ht_info->ampdu_factor =
369 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
370 ht_info->ampdu_density =
371 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
372 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
374 ht_info->ht_supported = 0;
379 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
380 struct ieee80211_ht_addt_info *ht_add_info_ie,
381 struct ieee80211_ht_bss_info *bss_info)
383 if (bss_info == NULL)
386 memset(bss_info, 0, sizeof(*bss_info));
388 if (ht_add_info_ie) {
390 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
392 bss_info->primary_channel = ht_add_info_ie->control_chan;
393 bss_info->bss_cap = ht_add_info_ie->ht_param;
394 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
400 static void ieee80211_sta_send_associnfo(struct net_device *dev,
401 struct ieee80211_if_sta *ifsta)
406 union iwreq_data wrqu;
408 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
411 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
412 ifsta->assocresp_ies_len), GFP_KERNEL);
416 len = sprintf(buf, "ASSOCINFO(");
417 if (ifsta->assocreq_ies) {
418 len += sprintf(buf + len, "ReqIEs=");
419 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
420 len += sprintf(buf + len, "%02x",
421 ifsta->assocreq_ies[i]);
424 if (ifsta->assocresp_ies) {
425 if (ifsta->assocreq_ies)
426 len += sprintf(buf + len, " ");
427 len += sprintf(buf + len, "RespIEs=");
428 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
429 len += sprintf(buf + len, "%02x",
430 ifsta->assocresp_ies[i]);
433 len += sprintf(buf + len, ")");
435 if (len > IW_CUSTOM_MAX) {
436 len = sprintf(buf, "ASSOCRESPIE=");
437 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
438 len += sprintf(buf + len, "%02x",
439 ifsta->assocresp_ies[i]);
443 memset(&wrqu, 0, sizeof(wrqu));
444 wrqu.data.length = len;
445 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
451 static void ieee80211_set_associated(struct net_device *dev,
452 struct ieee80211_if_sta *ifsta,
455 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
456 struct ieee80211_local *local = sdata->local;
457 union iwreq_data wrqu;
458 u32 changed = BSS_CHANGED_ASSOC;
461 struct ieee80211_sta_bss *bss;
463 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
465 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
468 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
469 local->hw.conf.channel->center_freq,
470 ifsta->ssid, ifsta->ssid_len);
472 if (bss->has_erp_value)
473 changed |= ieee80211_handle_erp_ie(
474 sdata, bss->erp_value);
475 ieee80211_rx_bss_put(dev, bss);
478 netif_carrier_on(dev);
479 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
480 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
481 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
482 ieee80211_sta_send_associnfo(dev, ifsta);
484 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
486 netif_carrier_off(dev);
487 ieee80211_reset_erp_info(dev);
488 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
490 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
491 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
492 ifsta->last_probe = jiffies;
493 ieee80211_led_assoc(local, assoc);
495 sdata->bss_conf.assoc = assoc;
496 ieee80211_bss_info_change_notify(sdata, changed);
499 static void ieee80211_set_disassoc(struct net_device *dev,
500 struct ieee80211_if_sta *ifsta, int deauth)
503 ifsta->auth_tries = 0;
504 ifsta->assoc_tries = 0;
505 ieee80211_set_associated(dev, ifsta, 0);
508 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
511 struct ieee80211_sub_if_data *sdata;
512 struct ieee80211_tx_packet_data *pkt_data;
514 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
515 skb->dev = sdata->local->mdev;
516 skb_set_mac_header(skb, 0);
517 skb_set_network_header(skb, 0);
518 skb_set_transport_header(skb, 0);
520 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
521 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
522 pkt_data->ifindex = sdata->dev->ifindex;
524 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
530 static void ieee80211_send_auth(struct net_device *dev,
531 struct ieee80211_if_sta *ifsta,
532 int transaction, u8 *extra, size_t extra_len,
535 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
537 struct ieee80211_mgmt *mgmt;
539 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
540 sizeof(*mgmt) + 6 + extra_len);
542 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
543 "frame\n", dev->name);
546 skb_reserve(skb, local->hw.extra_tx_headroom);
548 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
549 memset(mgmt, 0, 24 + 6);
550 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
551 IEEE80211_STYPE_AUTH);
553 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
554 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
555 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
556 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
557 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
558 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
559 ifsta->auth_transaction = transaction + 1;
560 mgmt->u.auth.status_code = cpu_to_le16(0);
562 memcpy(skb_put(skb, extra_len), extra, extra_len);
564 ieee80211_sta_tx(dev, skb, encrypt);
568 static void ieee80211_authenticate(struct net_device *dev,
569 struct ieee80211_if_sta *ifsta)
571 DECLARE_MAC_BUF(mac);
574 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
575 printk(KERN_DEBUG "%s: authentication with AP %s"
577 dev->name, print_mac(mac, ifsta->bssid));
578 ifsta->state = IEEE80211_DISABLED;
582 ifsta->state = IEEE80211_AUTHENTICATE;
583 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
584 dev->name, print_mac(mac, ifsta->bssid));
586 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
588 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
592 static void ieee80211_send_assoc(struct net_device *dev,
593 struct ieee80211_if_sta *ifsta)
595 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
597 struct ieee80211_mgmt *mgmt;
601 struct ieee80211_sta_bss *bss;
603 struct ieee80211_supported_band *sband;
605 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
606 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
609 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
610 "frame\n", dev->name);
613 skb_reserve(skb, local->hw.extra_tx_headroom);
615 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
617 capab = ifsta->capab;
619 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
620 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
621 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
622 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
623 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
626 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
627 local->hw.conf.channel->center_freq,
628 ifsta->ssid, ifsta->ssid_len);
630 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
631 capab |= WLAN_CAPABILITY_PRIVACY;
635 ieee80211_rx_bss_put(dev, bss);
638 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
640 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
641 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
642 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
644 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
646 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
647 IEEE80211_STYPE_REASSOC_REQ);
648 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
649 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
650 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
654 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
655 IEEE80211_STYPE_ASSOC_REQ);
656 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
657 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
661 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
662 *pos++ = WLAN_EID_SSID;
663 *pos++ = ifsta->ssid_len;
664 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
666 len = sband->n_bitrates;
669 pos = skb_put(skb, len + 2);
670 *pos++ = WLAN_EID_SUPP_RATES;
672 for (i = 0; i < len; i++) {
673 int rate = sband->bitrates[i].bitrate;
674 *pos++ = (u8) (rate / 5);
677 if (sband->n_bitrates > len) {
678 pos = skb_put(skb, sband->n_bitrates - len + 2);
679 *pos++ = WLAN_EID_EXT_SUPP_RATES;
680 *pos++ = sband->n_bitrates - len;
681 for (i = len; i < sband->n_bitrates; i++) {
682 int rate = sband->bitrates[i].bitrate;
683 *pos++ = (u8) (rate / 5);
687 if (ifsta->extra_ie) {
688 pos = skb_put(skb, ifsta->extra_ie_len);
689 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
692 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
693 pos = skb_put(skb, 9);
694 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
695 *pos++ = 7; /* len */
696 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
699 *pos++ = 2; /* WME */
700 *pos++ = 0; /* WME info */
701 *pos++ = 1; /* WME ver */
704 /* wmm support is a must to HT */
705 if (wmm && sband->ht_info.ht_supported) {
706 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
707 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
708 *pos++ = WLAN_EID_HT_CAPABILITY;
709 *pos++ = sizeof(struct ieee80211_ht_cap);
710 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
711 memcpy(pos, &tmp, sizeof(u16));
713 /* TODO: needs a define here for << 2 */
714 *pos++ = sband->ht_info.ampdu_factor |
715 (sband->ht_info.ampdu_density << 2);
716 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
719 kfree(ifsta->assocreq_ies);
720 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
721 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
722 if (ifsta->assocreq_ies)
723 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
725 ieee80211_sta_tx(dev, skb, 0);
729 static void ieee80211_send_deauth(struct net_device *dev,
730 struct ieee80211_if_sta *ifsta, u16 reason)
732 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
734 struct ieee80211_mgmt *mgmt;
736 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
738 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
739 "frame\n", dev->name);
742 skb_reserve(skb, local->hw.extra_tx_headroom);
744 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
746 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
747 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
748 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
749 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
750 IEEE80211_STYPE_DEAUTH);
752 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
754 ieee80211_sta_tx(dev, skb, 0);
758 static void ieee80211_send_disassoc(struct net_device *dev,
759 struct ieee80211_if_sta *ifsta, u16 reason)
761 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
763 struct ieee80211_mgmt *mgmt;
765 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
767 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
768 "frame\n", dev->name);
771 skb_reserve(skb, local->hw.extra_tx_headroom);
773 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
775 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
776 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
777 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
778 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
779 IEEE80211_STYPE_DISASSOC);
781 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
783 ieee80211_sta_tx(dev, skb, 0);
787 static int ieee80211_privacy_mismatch(struct net_device *dev,
788 struct ieee80211_if_sta *ifsta)
790 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
791 struct ieee80211_sta_bss *bss;
796 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
799 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
800 local->hw.conf.channel->center_freq,
801 ifsta->ssid, ifsta->ssid_len);
805 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
806 wep_privacy = !!ieee80211_sta_wep_configured(dev);
807 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
809 ieee80211_rx_bss_put(dev, bss);
811 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
818 static void ieee80211_associate(struct net_device *dev,
819 struct ieee80211_if_sta *ifsta)
821 DECLARE_MAC_BUF(mac);
823 ifsta->assoc_tries++;
824 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
825 printk(KERN_DEBUG "%s: association with AP %s"
827 dev->name, print_mac(mac, ifsta->bssid));
828 ifsta->state = IEEE80211_DISABLED;
832 ifsta->state = IEEE80211_ASSOCIATE;
833 printk(KERN_DEBUG "%s: associate with AP %s\n",
834 dev->name, print_mac(mac, ifsta->bssid));
835 if (ieee80211_privacy_mismatch(dev, ifsta)) {
836 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
837 "mixed-cell disabled - abort association\n", dev->name);
838 ifsta->state = IEEE80211_DISABLED;
842 ieee80211_send_assoc(dev, ifsta);
844 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
848 static void ieee80211_associated(struct net_device *dev,
849 struct ieee80211_if_sta *ifsta)
851 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
852 struct sta_info *sta;
854 DECLARE_MAC_BUF(mac);
856 /* TODO: start monitoring current AP signal quality and number of
857 * missed beacons. Scan other channels every now and then and search
859 /* TODO: remove expired BSSes */
861 ifsta->state = IEEE80211_ASSOCIATED;
863 sta = sta_info_get(local, ifsta->bssid);
865 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
866 dev->name, print_mac(mac, ifsta->bssid));
870 if (time_after(jiffies,
871 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
872 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
873 printk(KERN_DEBUG "%s: No ProbeResp from "
874 "current AP %s - assume out of "
876 dev->name, print_mac(mac, ifsta->bssid));
880 ieee80211_send_probe_req(dev, ifsta->bssid,
882 local->scan_ssid_len);
883 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
885 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
886 if (time_after(jiffies, ifsta->last_probe +
887 IEEE80211_PROBE_INTERVAL)) {
888 ifsta->last_probe = jiffies;
889 ieee80211_send_probe_req(dev, ifsta->bssid,
897 ifsta->state = IEEE80211_DISABLED;
898 ieee80211_set_associated(dev, ifsta, 0);
900 mod_timer(&ifsta->timer, jiffies +
901 IEEE80211_MONITORING_INTERVAL);
906 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
907 u8 *ssid, size_t ssid_len)
909 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
910 struct ieee80211_supported_band *sband;
912 struct ieee80211_mgmt *mgmt;
913 u8 *pos, *supp_rates, *esupp_rates = NULL;
916 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
918 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
919 "request\n", dev->name);
922 skb_reserve(skb, local->hw.extra_tx_headroom);
924 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
926 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
927 IEEE80211_STYPE_PROBE_REQ);
928 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
930 memcpy(mgmt->da, dst, ETH_ALEN);
931 memcpy(mgmt->bssid, dst, ETH_ALEN);
933 memset(mgmt->da, 0xff, ETH_ALEN);
934 memset(mgmt->bssid, 0xff, ETH_ALEN);
936 pos = skb_put(skb, 2 + ssid_len);
937 *pos++ = WLAN_EID_SSID;
939 memcpy(pos, ssid, ssid_len);
941 supp_rates = skb_put(skb, 2);
942 supp_rates[0] = WLAN_EID_SUPP_RATES;
944 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
946 for (i = 0; i < sband->n_bitrates; i++) {
947 struct ieee80211_rate *rate = &sband->bitrates[i];
949 pos = skb_put(skb, 1);
951 } else if (supp_rates[1] == 8) {
952 esupp_rates = skb_put(skb, 3);
953 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
955 pos = &esupp_rates[2];
957 pos = skb_put(skb, 1);
960 *pos = rate->bitrate / 5;
963 ieee80211_sta_tx(dev, skb, 0);
967 static int ieee80211_sta_wep_configured(struct net_device *dev)
969 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
970 if (!sdata || !sdata->default_key ||
971 sdata->default_key->conf.alg != ALG_WEP)
977 static void ieee80211_auth_completed(struct net_device *dev,
978 struct ieee80211_if_sta *ifsta)
980 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
981 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
982 ieee80211_associate(dev, ifsta);
986 static void ieee80211_auth_challenge(struct net_device *dev,
987 struct ieee80211_if_sta *ifsta,
988 struct ieee80211_mgmt *mgmt,
992 struct ieee802_11_elems elems;
994 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
995 pos = mgmt->u.auth.variable;
996 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
997 if (!elems.challenge) {
998 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
999 "frame\n", dev->name);
1002 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1003 elems.challenge_len + 2, 1);
1006 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1007 u8 dialog_token, u16 status, u16 policy,
1008 u16 buf_size, u16 timeout)
1010 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1011 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1012 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1013 struct sk_buff *skb;
1014 struct ieee80211_mgmt *mgmt;
1017 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1018 sizeof(mgmt->u.action.u.addba_resp));
1020 printk(KERN_DEBUG "%s: failed to allocate buffer "
1021 "for addba resp frame\n", dev->name);
1025 skb_reserve(skb, local->hw.extra_tx_headroom);
1026 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1027 memset(mgmt, 0, 24);
1028 memcpy(mgmt->da, da, ETH_ALEN);
1029 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1030 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1031 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1033 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1034 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1035 IEEE80211_STYPE_ACTION);
1037 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1038 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1039 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1040 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1042 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1043 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1044 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1046 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1047 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1048 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1050 ieee80211_sta_tx(dev, skb, 0);
1055 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1056 u16 tid, u8 dialog_token, u16 start_seq_num,
1057 u16 agg_size, u16 timeout)
1059 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1060 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1061 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1062 struct sk_buff *skb;
1063 struct ieee80211_mgmt *mgmt;
1066 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1067 sizeof(mgmt->u.action.u.addba_req));
1071 printk(KERN_ERR "%s: failed to allocate buffer "
1072 "for addba request frame\n", dev->name);
1075 skb_reserve(skb, local->hw.extra_tx_headroom);
1076 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1077 memset(mgmt, 0, 24);
1078 memcpy(mgmt->da, da, ETH_ALEN);
1079 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1080 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1081 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1083 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1085 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1086 IEEE80211_STYPE_ACTION);
1088 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1090 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1091 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1093 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1094 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1095 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1096 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1098 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1100 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1101 mgmt->u.action.u.addba_req.start_seq_num =
1102 cpu_to_le16(start_seq_num << 4);
1104 ieee80211_sta_tx(dev, skb, 0);
1107 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1108 struct ieee80211_mgmt *mgmt,
1111 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1112 struct ieee80211_hw *hw = &local->hw;
1113 struct ieee80211_conf *conf = &hw->conf;
1114 struct sta_info *sta;
1115 struct tid_ampdu_rx *tid_agg_rx;
1116 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1118 int ret = -EOPNOTSUPP;
1119 DECLARE_MAC_BUF(mac);
1121 sta = sta_info_get(local, mgmt->sa);
1125 /* extract session parameters from addba request frame */
1126 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1127 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1129 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1131 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1132 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1133 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1134 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1136 status = WLAN_STATUS_REQUEST_DECLINED;
1138 /* sanity check for incoming parameters:
1139 * check if configuration can support the BA policy
1140 * and if buffer size does not exceeds max value */
1141 if (((ba_policy != 1)
1142 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1143 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1144 status = WLAN_STATUS_INVALID_QOS_PARAM;
1145 #ifdef CONFIG_MAC80211_HT_DEBUG
1146 if (net_ratelimit())
1147 printk(KERN_DEBUG "Block Ack Req with bad params from "
1148 "%s on tid %u. policy %d, buffer size %d\n",
1149 print_mac(mac, mgmt->sa), tid, ba_policy,
1151 #endif /* CONFIG_MAC80211_HT_DEBUG */
1154 /* determine default buffer size */
1155 if (buf_size == 0) {
1156 struct ieee80211_supported_band *sband;
1158 sband = local->hw.wiphy->bands[conf->channel->band];
1159 buf_size = IEEE80211_MIN_AMPDU_BUF;
1160 buf_size = buf_size << sband->ht_info.ampdu_factor;
1163 tid_agg_rx = &sta->ampdu_mlme.tid_rx[tid];
1165 /* examine state machine */
1166 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1168 if (tid_agg_rx->state != HT_AGG_STATE_IDLE) {
1169 #ifdef CONFIG_MAC80211_HT_DEBUG
1170 if (net_ratelimit())
1171 printk(KERN_DEBUG "unexpected Block Ack Req from "
1173 print_mac(mac, mgmt->sa), tid);
1174 #endif /* CONFIG_MAC80211_HT_DEBUG */
1178 /* prepare reordering buffer */
1179 tid_agg_rx->reorder_buf =
1180 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
1181 if (!tid_agg_rx->reorder_buf) {
1182 if (net_ratelimit())
1183 printk(KERN_ERR "can not allocate reordering buffer "
1184 "to tid %d\n", tid);
1187 memset(tid_agg_rx->reorder_buf, 0,
1188 buf_size * sizeof(struct sk_buf *));
1190 if (local->ops->ampdu_action)
1191 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1192 sta->addr, tid, &start_seq_num);
1193 #ifdef CONFIG_MAC80211_HT_DEBUG
1194 printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1195 #endif /* CONFIG_MAC80211_HT_DEBUG */
1198 kfree(tid_agg_rx->reorder_buf);
1202 /* change state and send addba resp */
1203 tid_agg_rx->state = HT_AGG_STATE_OPERATIONAL;
1204 tid_agg_rx->dialog_token = dialog_token;
1205 tid_agg_rx->ssn = start_seq_num;
1206 tid_agg_rx->head_seq_num = start_seq_num;
1207 tid_agg_rx->buf_size = buf_size;
1208 tid_agg_rx->timeout = timeout;
1209 tid_agg_rx->stored_mpdu_num = 0;
1210 status = WLAN_STATUS_SUCCESS;
1212 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1215 ieee80211_send_addba_resp(sta->dev, sta->addr, tid, dialog_token,
1216 status, 1, buf_size, timeout);
1220 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1221 struct ieee80211_mgmt *mgmt,
1224 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1225 struct ieee80211_hw *hw = &local->hw;
1226 struct sta_info *sta;
1231 sta = sta_info_get(local, mgmt->sa);
1235 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1236 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1238 state = &sta->ampdu_mlme.tid_tx[tid].state;
1240 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1242 if (mgmt->u.action.u.addba_resp.dialog_token !=
1243 sta->ampdu_mlme.tid_tx[tid].dialog_token) {
1244 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1245 #ifdef CONFIG_MAC80211_HT_DEBUG
1246 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1247 #endif /* CONFIG_MAC80211_HT_DEBUG */
1252 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
1253 #ifdef CONFIG_MAC80211_HT_DEBUG
1254 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1255 #endif /* CONFIG_MAC80211_HT_DEBUG */
1256 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1257 == WLAN_STATUS_SUCCESS) {
1258 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1259 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1260 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1266 if (*state & HT_ADDBA_RECEIVED_MSK)
1267 printk(KERN_DEBUG "double addBA response\n");
1269 *state |= HT_ADDBA_RECEIVED_MSK;
1270 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
1272 if (*state == HT_AGG_STATE_OPERATIONAL) {
1273 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1274 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1277 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1278 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1280 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1282 sta->ampdu_mlme.tid_tx[tid].addba_req_num++;
1283 /* this will allow the state check in stop_BA_session */
1284 *state = HT_AGG_STATE_OPERATIONAL;
1285 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1286 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1287 WLAN_BACK_INITIATOR);
1292 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1293 u16 initiator, u16 reason_code)
1295 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1296 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1297 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1298 struct sk_buff *skb;
1299 struct ieee80211_mgmt *mgmt;
1302 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1303 sizeof(mgmt->u.action.u.delba));
1306 printk(KERN_ERR "%s: failed to allocate buffer "
1307 "for delba frame\n", dev->name);
1311 skb_reserve(skb, local->hw.extra_tx_headroom);
1312 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1313 memset(mgmt, 0, 24);
1314 memcpy(mgmt->da, da, ETH_ALEN);
1315 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1316 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1317 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1319 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1320 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1321 IEEE80211_STYPE_ACTION);
1323 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1325 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1326 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1327 params = (u16)(initiator << 11); /* bit 11 initiator */
1328 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1330 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1331 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1333 ieee80211_sta_tx(dev, skb, 0);
1336 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1337 u16 initiator, u16 reason)
1339 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1340 struct ieee80211_hw *hw = &local->hw;
1341 struct sta_info *sta;
1344 sta = sta_info_get(local, ra);
1348 /* check if TID is in operational state */
1349 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1350 if (sta->ampdu_mlme.tid_rx[tid].state
1351 != HT_AGG_STATE_OPERATIONAL) {
1352 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1356 sta->ampdu_mlme.tid_rx[tid].state =
1357 HT_AGG_STATE_REQ_STOP_BA_MSK |
1358 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1359 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1361 /* stop HW Rx aggregation. ampdu_action existence
1362 * already verified in session init so we add the BUG_ON */
1363 BUG_ON(!local->ops->ampdu_action);
1365 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1368 printk(KERN_DEBUG "HW problem - can not stop rx "
1369 "aggergation for tid %d\n", tid);
1371 /* shutdown timer has not expired */
1372 if (initiator != WLAN_BACK_TIMER)
1373 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid].
1376 /* check if this is a self generated aggregation halt */
1377 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1378 ieee80211_send_delba(dev, ra, tid, 0, reason);
1380 /* free the reordering buffer */
1381 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid].buf_size; i++) {
1382 if (sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]) {
1383 /* release the reordered frames */
1384 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]);
1385 sta->ampdu_mlme.tid_rx[tid].stored_mpdu_num--;
1386 sta->ampdu_mlme.tid_rx[tid].reorder_buf[i] = NULL;
1389 kfree(sta->ampdu_mlme.tid_rx[tid].reorder_buf);
1391 sta->ampdu_mlme.tid_rx[tid].state = HT_AGG_STATE_IDLE;
1396 static void ieee80211_sta_process_delba(struct net_device *dev,
1397 struct ieee80211_mgmt *mgmt, size_t len)
1399 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1400 struct sta_info *sta;
1403 DECLARE_MAC_BUF(mac);
1405 sta = sta_info_get(local, mgmt->sa);
1409 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1410 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1411 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1413 #ifdef CONFIG_MAC80211_HT_DEBUG
1414 if (net_ratelimit())
1415 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1416 print_mac(mac, mgmt->sa),
1417 initiator ? "recipient" : "initiator", tid,
1418 mgmt->u.action.u.delba.reason_code);
1419 #endif /* CONFIG_MAC80211_HT_DEBUG */
1421 if (initiator == WLAN_BACK_INITIATOR)
1422 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1423 WLAN_BACK_INITIATOR, 0);
1424 else { /* WLAN_BACK_RECIPIENT */
1425 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1426 sta->ampdu_mlme.tid_tx[tid].state =
1427 HT_AGG_STATE_OPERATIONAL;
1428 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1429 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1430 WLAN_BACK_RECIPIENT);
1436 * After sending add Block Ack request we activated a timer until
1437 * add Block Ack response will arrive from the recipient.
1438 * If this timer expires sta_addba_resp_timer_expired will be executed.
1440 void sta_addba_resp_timer_expired(unsigned long data)
1442 /* not an elegant detour, but there is no choice as the timer passes
1443 * only one argument, and both sta_info and TID are needed, so init
1444 * flow in sta_info_add gives the TID as data, while the timer_to_id
1445 * array gives the sta through container_of */
1446 u16 tid = *(int *)data;
1447 struct sta_info *temp_sta = container_of((void *)data,
1448 struct sta_info, timer_to_tid[tid]);
1450 struct ieee80211_local *local = temp_sta->local;
1451 struct ieee80211_hw *hw = &local->hw;
1452 struct sta_info *sta;
1455 sta = sta_info_get(local, temp_sta->addr);
1459 state = &sta->ampdu_mlme.tid_tx[tid].state;
1460 /* check if the TID waits for addBA response */
1461 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1462 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1463 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1464 *state = HT_AGG_STATE_IDLE;
1465 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1466 "expecting addBA response there", tid);
1467 goto timer_expired_exit;
1470 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1472 /* go through the state check in stop_BA_session */
1473 *state = HT_AGG_STATE_OPERATIONAL;
1474 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1475 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1476 WLAN_BACK_INITIATOR);
1483 * After receiving Block Ack Request (BAR) we activated a
1484 * timer after each frame arrives from the originator.
1485 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1487 void sta_rx_agg_session_timer_expired(unsigned long data)
1489 /* not an elegant detour, but there is no choice as the timer passes
1490 * only one argument, and verious sta_info are needed here, so init
1491 * flow in sta_info_add gives the TID as data, while the timer_to_id
1492 * array gives the sta through container_of */
1493 u8 *ptid = (u8 *)data;
1494 u8 *timer_to_id = ptid - *ptid;
1495 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1498 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1499 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr, (u16)*ptid,
1501 WLAN_REASON_QSTA_TIMEOUT);
1505 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1506 struct ieee80211_if_sta *ifsta,
1507 struct ieee80211_mgmt *mgmt,
1510 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1511 u16 auth_alg, auth_transaction, status_code;
1512 DECLARE_MAC_BUF(mac);
1514 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1515 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
1516 printk(KERN_DEBUG "%s: authentication frame received from "
1517 "%s, but not in authenticate state - ignored\n",
1518 dev->name, print_mac(mac, mgmt->sa));
1523 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
1524 "received from %s - ignored\n",
1525 dev->name, len, print_mac(mac, mgmt->sa));
1529 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1530 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1531 printk(KERN_DEBUG "%s: authentication frame received from "
1532 "unknown AP (SA=%s BSSID=%s) - "
1533 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1534 print_mac(mac, mgmt->bssid));
1538 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1539 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1540 printk(KERN_DEBUG "%s: authentication frame received from "
1541 "unknown BSSID (SA=%s BSSID=%s) - "
1542 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1543 print_mac(mac, mgmt->bssid));
1547 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1548 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1549 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1551 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
1552 "transaction=%d status=%d)\n",
1553 dev->name, print_mac(mac, mgmt->sa), auth_alg,
1554 auth_transaction, status_code);
1556 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1557 /* IEEE 802.11 standard does not require authentication in IBSS
1558 * networks and most implementations do not seem to use it.
1559 * However, try to reply to authentication attempts if someone
1560 * has actually implemented this.
1561 * TODO: Could implement shared key authentication. */
1562 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1563 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1564 "frame (alg=%d transaction=%d)\n",
1565 dev->name, auth_alg, auth_transaction);
1568 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1571 if (auth_alg != ifsta->auth_alg ||
1572 auth_transaction != ifsta->auth_transaction) {
1573 printk(KERN_DEBUG "%s: unexpected authentication frame "
1574 "(alg=%d transaction=%d)\n",
1575 dev->name, auth_alg, auth_transaction);
1579 if (status_code != WLAN_STATUS_SUCCESS) {
1580 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1581 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1582 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1584 const int num_algs = ARRAY_SIZE(algs);
1586 algs[0] = algs[1] = algs[2] = 0xff;
1587 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1588 algs[0] = WLAN_AUTH_OPEN;
1589 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1590 algs[1] = WLAN_AUTH_SHARED_KEY;
1591 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1592 algs[2] = WLAN_AUTH_LEAP;
1593 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1595 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1599 for (i = 0; i < num_algs; i++) {
1601 if (pos >= num_algs)
1603 if (algs[pos] == ifsta->auth_alg ||
1606 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1607 !ieee80211_sta_wep_configured(dev))
1609 ifsta->auth_alg = algs[pos];
1610 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1612 dev->name, ifsta->auth_alg);
1619 switch (ifsta->auth_alg) {
1620 case WLAN_AUTH_OPEN:
1621 case WLAN_AUTH_LEAP:
1622 ieee80211_auth_completed(dev, ifsta);
1624 case WLAN_AUTH_SHARED_KEY:
1625 if (ifsta->auth_transaction == 4)
1626 ieee80211_auth_completed(dev, ifsta);
1628 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1634 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1635 struct ieee80211_if_sta *ifsta,
1636 struct ieee80211_mgmt *mgmt,
1640 DECLARE_MAC_BUF(mac);
1643 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1644 "received from %s - ignored\n",
1645 dev->name, len, print_mac(mac, mgmt->sa));
1649 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1650 printk(KERN_DEBUG "%s: deauthentication frame received from "
1651 "unknown AP (SA=%s BSSID=%s) - "
1652 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1653 print_mac(mac, mgmt->bssid));
1657 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1659 printk(KERN_DEBUG "%s: RX deauthentication from %s"
1661 dev->name, print_mac(mac, mgmt->sa), reason_code);
1663 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1664 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1667 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1668 ifsta->state == IEEE80211_ASSOCIATE ||
1669 ifsta->state == IEEE80211_ASSOCIATED) {
1670 ifsta->state = IEEE80211_AUTHENTICATE;
1671 mod_timer(&ifsta->timer, jiffies +
1672 IEEE80211_RETRY_AUTH_INTERVAL);
1675 ieee80211_set_disassoc(dev, ifsta, 1);
1676 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1680 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1681 struct ieee80211_if_sta *ifsta,
1682 struct ieee80211_mgmt *mgmt,
1686 DECLARE_MAC_BUF(mac);
1689 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1690 "received from %s - ignored\n",
1691 dev->name, len, print_mac(mac, mgmt->sa));
1695 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1696 printk(KERN_DEBUG "%s: disassociation frame received from "
1697 "unknown AP (SA=%s BSSID=%s) - "
1698 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1699 print_mac(mac, mgmt->bssid));
1703 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1705 printk(KERN_DEBUG "%s: RX disassociation from %s"
1707 dev->name, print_mac(mac, mgmt->sa), reason_code);
1709 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1710 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1712 if (ifsta->state == IEEE80211_ASSOCIATED) {
1713 ifsta->state = IEEE80211_ASSOCIATE;
1714 mod_timer(&ifsta->timer, jiffies +
1715 IEEE80211_RETRY_AUTH_INTERVAL);
1718 ieee80211_set_disassoc(dev, ifsta, 0);
1722 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1723 struct ieee80211_if_sta *ifsta,
1724 struct ieee80211_mgmt *mgmt,
1728 struct ieee80211_local *local = sdata->local;
1729 struct net_device *dev = sdata->dev;
1730 struct ieee80211_supported_band *sband;
1731 struct sta_info *sta;
1732 u64 rates, basic_rates;
1733 u16 capab_info, status_code, aid;
1734 struct ieee802_11_elems elems;
1735 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1738 DECLARE_MAC_BUF(mac);
1739 bool have_higher_than_11mbit = false;
1741 /* AssocResp and ReassocResp have identical structure, so process both
1742 * of them in this function. */
1744 if (ifsta->state != IEEE80211_ASSOCIATE) {
1745 printk(KERN_DEBUG "%s: association frame received from "
1746 "%s, but not in associate state - ignored\n",
1747 dev->name, print_mac(mac, mgmt->sa));
1752 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1753 "received from %s - ignored\n",
1754 dev->name, len, print_mac(mac, mgmt->sa));
1758 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1759 printk(KERN_DEBUG "%s: association frame received from "
1760 "unknown AP (SA=%s BSSID=%s) - "
1761 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1762 print_mac(mac, mgmt->bssid));
1766 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1767 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1768 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1770 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1771 "status=%d aid=%d)\n",
1772 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1773 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1775 if (status_code != WLAN_STATUS_SUCCESS) {
1776 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1777 dev->name, status_code);
1778 /* if this was a reassociation, ensure we try a "full"
1779 * association next time. This works around some broken APs
1780 * which do not correctly reject reassociation requests. */
1781 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1785 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1786 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1787 "set\n", dev->name, aid);
1788 aid &= ~(BIT(15) | BIT(14));
1790 pos = mgmt->u.assoc_resp.variable;
1791 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1793 if (!elems.supp_rates) {
1794 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1799 printk(KERN_DEBUG "%s: associated\n", dev->name);
1801 ifsta->ap_capab = capab_info;
1803 kfree(ifsta->assocresp_ies);
1804 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1805 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1806 if (ifsta->assocresp_ies)
1807 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1809 /* Add STA entry for the AP */
1810 sta = sta_info_get(local, ifsta->bssid);
1812 struct ieee80211_sta_bss *bss;
1813 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
1815 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1816 " AP\n", dev->name);
1819 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
1820 local->hw.conf.channel->center_freq,
1821 ifsta->ssid, ifsta->ssid_len);
1823 sta->last_rssi = bss->rssi;
1824 sta->last_signal = bss->signal;
1825 sta->last_noise = bss->noise;
1826 ieee80211_rx_bss_put(dev, bss);
1831 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
1835 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1837 for (i = 0; i < elems.supp_rates_len; i++) {
1838 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1841 have_higher_than_11mbit = true;
1843 for (j = 0; j < sband->n_bitrates; j++) {
1844 if (sband->bitrates[j].bitrate == rate)
1846 if (elems.supp_rates[i] & 0x80)
1847 basic_rates |= BIT(j);
1851 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1852 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1855 have_higher_than_11mbit = true;
1857 for (j = 0; j < sband->n_bitrates; j++) {
1858 if (sband->bitrates[j].bitrate == rate)
1860 if (elems.ext_supp_rates[i] & 0x80)
1861 basic_rates |= BIT(j);
1865 sta->supp_rates[local->hw.conf.channel->band] = rates;
1866 sdata->basic_rates = basic_rates;
1868 /* cf. IEEE 802.11 9.2.12 */
1869 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1870 have_higher_than_11mbit)
1871 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1873 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1875 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1876 local->ops->conf_ht) {
1877 struct ieee80211_ht_bss_info bss_info;
1879 ieee80211_ht_cap_ie_to_ht_info(
1880 (struct ieee80211_ht_cap *)
1881 elems.ht_cap_elem, &sta->ht_info);
1882 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1883 (struct ieee80211_ht_addt_info *)
1884 elems.ht_info_elem, &bss_info);
1885 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1888 rate_control_rate_init(sta, local);
1890 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1891 sta->flags |= WLAN_STA_WME;
1892 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1893 elems.wmm_param_len);
1896 /* set AID, ieee80211_set_associated() will tell the driver */
1897 bss_conf->aid = aid;
1898 ieee80211_set_associated(dev, ifsta, 1);
1902 ieee80211_associated(dev, ifsta);
1906 /* Caller must hold local->sta_bss_lock */
1907 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1908 struct ieee80211_sta_bss *bss)
1910 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1911 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1912 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1916 /* Caller must hold local->sta_bss_lock */
1917 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1918 struct ieee80211_sta_bss *bss)
1920 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1921 struct ieee80211_sta_bss *b, *prev = NULL;
1922 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1926 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1929 prev->hnext = bss->hnext;
1938 static struct ieee80211_sta_bss *
1939 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
1940 u8 *ssid, u8 ssid_len)
1942 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1943 struct ieee80211_sta_bss *bss;
1945 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1948 atomic_inc(&bss->users);
1949 atomic_inc(&bss->users);
1950 memcpy(bss->bssid, bssid, ETH_ALEN);
1952 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1953 memcpy(bss->ssid, ssid, ssid_len);
1954 bss->ssid_len = ssid_len;
1957 spin_lock_bh(&local->sta_bss_lock);
1958 /* TODO: order by RSSI? */
1959 list_add_tail(&bss->list, &local->sta_bss_list);
1960 __ieee80211_rx_bss_hash_add(dev, bss);
1961 spin_unlock_bh(&local->sta_bss_lock);
1966 static struct ieee80211_sta_bss *
1967 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
1968 u8 *ssid, u8 ssid_len)
1970 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1971 struct ieee80211_sta_bss *bss;
1973 spin_lock_bh(&local->sta_bss_lock);
1974 bss = local->sta_bss_hash[STA_HASH(bssid)];
1976 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
1977 bss->freq == freq &&
1978 bss->ssid_len == ssid_len &&
1979 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
1980 atomic_inc(&bss->users);
1985 spin_unlock_bh(&local->sta_bss_lock);
1990 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2000 static void ieee80211_rx_bss_put(struct net_device *dev,
2001 struct ieee80211_sta_bss *bss)
2003 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2004 if (!atomic_dec_and_test(&bss->users))
2007 spin_lock_bh(&local->sta_bss_lock);
2008 __ieee80211_rx_bss_hash_del(dev, bss);
2009 list_del(&bss->list);
2010 spin_unlock_bh(&local->sta_bss_lock);
2011 ieee80211_rx_bss_free(bss);
2015 void ieee80211_rx_bss_list_init(struct net_device *dev)
2017 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2018 spin_lock_init(&local->sta_bss_lock);
2019 INIT_LIST_HEAD(&local->sta_bss_list);
2023 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2025 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2026 struct ieee80211_sta_bss *bss, *tmp;
2028 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2029 ieee80211_rx_bss_put(dev, bss);
2033 static void ieee80211_rx_bss_info(struct net_device *dev,
2034 struct ieee80211_mgmt *mgmt,
2036 struct ieee80211_rx_status *rx_status,
2039 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2040 struct ieee802_11_elems elems;
2043 struct ieee80211_sta_bss *bss;
2044 struct sta_info *sta;
2045 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2047 DECLARE_MAC_BUF(mac);
2048 DECLARE_MAC_BUF(mac2);
2050 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2051 return; /* ignore ProbeResp to foreign address */
2054 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
2055 dev->name, beacon ? "Beacon" : "Probe Response",
2056 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
2059 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2063 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2065 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2066 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2067 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2068 static unsigned long last_tsf_debug = 0;
2070 if (local->ops->get_tsf)
2071 tsf = local->ops->get_tsf(local_to_hw(local));
2074 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
2075 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2076 "%s TSF=0x%llx BCN=0x%llx diff=%lld "
2078 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
2079 (unsigned long long)tsf,
2080 (unsigned long long)timestamp,
2081 (unsigned long long)(tsf - timestamp),
2083 last_tsf_debug = jiffies;
2085 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2088 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2090 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
2091 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2092 (sta = sta_info_get(local, mgmt->sa))) {
2093 struct ieee80211_supported_band *sband;
2094 struct ieee80211_rate *bitrates;
2096 u64 supp_rates, prev_rates;
2099 sband = local->hw.wiphy->bands[rx_status->band];
2103 sband = local->hw.wiphy->bands[
2104 local->hw.conf.channel->band];
2107 bitrates = sband->bitrates;
2108 num_rates = sband->n_bitrates;
2111 for (i = 0; i < elems.supp_rates_len +
2112 elems.ext_supp_rates_len; i++) {
2115 if (i < elems.supp_rates_len)
2116 rate = elems.supp_rates[i];
2117 else if (elems.ext_supp_rates)
2118 rate = elems.ext_supp_rates
2119 [i - elems.supp_rates_len];
2120 own_rate = 5 * (rate & 0x7f);
2121 for (j = 0; j < num_rates; j++)
2122 if (bitrates[j].bitrate == own_rate)
2123 supp_rates |= BIT(j);
2126 prev_rates = sta->supp_rates[rx_status->band];
2127 sta->supp_rates[rx_status->band] &= supp_rates;
2128 if (sta->supp_rates[rx_status->band] == 0) {
2129 /* No matching rates - this should not really happen.
2130 * Make sure that at least one rate is marked
2131 * supported to avoid issues with TX rate ctrl. */
2132 sta->supp_rates[rx_status->band] =
2133 sdata->u.sta.supp_rates_bits[rx_status->band];
2135 if (sta->supp_rates[rx_status->band] != prev_rates) {
2136 printk(KERN_DEBUG "%s: updated supp_rates set for "
2137 "%s based on beacon info (0x%llx & 0x%llx -> "
2139 dev->name, print_mac(mac, sta->addr),
2140 (unsigned long long) prev_rates,
2141 (unsigned long long) supp_rates,
2142 (unsigned long long) sta->supp_rates[rx_status->band]);
2150 if (elems.ds_params && elems.ds_params_len == 1)
2151 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
2153 freq = rx_status->freq;
2155 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2156 elems.ssid, elems.ssid_len);
2158 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2159 elems.ssid, elems.ssid_len);
2164 /* TODO: order by RSSI? */
2165 spin_lock_bh(&local->sta_bss_lock);
2166 list_move_tail(&bss->list, &local->sta_bss_list);
2167 spin_unlock_bh(&local->sta_bss_lock);
2171 bss->band = rx_status->band;
2173 if (bss->probe_resp && beacon) {
2174 /* Do not allow beacon to override data from Probe Response. */
2175 ieee80211_rx_bss_put(dev, bss);
2179 /* save the ERP value so that it is available at association time */
2180 if (elems.erp_info && elems.erp_info_len >= 1) {
2181 bss->erp_value = elems.erp_info[0];
2182 bss->has_erp_value = 1;
2185 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2186 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2188 bss->supp_rates_len = 0;
2189 if (elems.supp_rates) {
2190 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2191 if (clen > elems.supp_rates_len)
2192 clen = elems.supp_rates_len;
2193 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2195 bss->supp_rates_len += clen;
2197 if (elems.ext_supp_rates) {
2198 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2199 if (clen > elems.ext_supp_rates_len)
2200 clen = elems.ext_supp_rates_len;
2201 memcpy(&bss->supp_rates[bss->supp_rates_len],
2202 elems.ext_supp_rates, clen);
2203 bss->supp_rates_len += clen;
2207 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2208 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2210 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2212 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2213 bss->wpa_ie_len = elems.wpa_len + 2;
2215 bss->wpa_ie_len = 0;
2216 } else if (!elems.wpa && bss->wpa_ie) {
2219 bss->wpa_ie_len = 0;
2223 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2224 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2226 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2228 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2229 bss->rsn_ie_len = elems.rsn_len + 2;
2231 bss->rsn_ie_len = 0;
2232 } else if (!elems.rsn && bss->rsn_ie) {
2235 bss->rsn_ie_len = 0;
2238 if (elems.wmm_param &&
2239 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2240 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2242 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2244 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2245 elems.wmm_param_len + 2);
2246 bss->wmm_ie_len = elems.wmm_param_len + 2;
2248 bss->wmm_ie_len = 0;
2249 } else if (!elems.wmm_param && bss->wmm_ie) {
2252 bss->wmm_ie_len = 0;
2254 if (elems.ht_cap_elem &&
2255 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2256 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2258 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2260 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2261 elems.ht_cap_elem_len + 2);
2262 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2265 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2271 bss->timestamp = timestamp;
2272 bss->last_update = jiffies;
2273 bss->rssi = rx_status->ssi;
2274 bss->signal = rx_status->signal;
2275 bss->noise = rx_status->noise;
2278 ieee80211_rx_bss_put(dev, bss);
2282 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2283 struct ieee80211_mgmt *mgmt,
2285 struct ieee80211_rx_status *rx_status)
2287 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2291 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2292 struct ieee80211_mgmt *mgmt,
2294 struct ieee80211_rx_status *rx_status)
2296 struct ieee80211_sub_if_data *sdata;
2297 struct ieee80211_if_sta *ifsta;
2299 struct ieee802_11_elems elems;
2300 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2301 struct ieee80211_conf *conf = &local->hw.conf;
2304 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2306 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2307 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2309 ifsta = &sdata->u.sta;
2311 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2312 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2315 /* Process beacon from the current BSS */
2316 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2320 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2322 if (elems.erp_info && elems.erp_info_len >= 1)
2323 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2325 if (elems.ht_cap_elem && elems.ht_info_elem &&
2326 elems.wmm_param && local->ops->conf_ht &&
2327 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2328 struct ieee80211_ht_bss_info bss_info;
2330 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2331 (struct ieee80211_ht_addt_info *)
2332 elems.ht_info_elem, &bss_info);
2333 /* check if AP changed bss inforamation */
2334 if ((conf->ht_bss_conf.primary_channel !=
2335 bss_info.primary_channel) ||
2336 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2337 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2338 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2342 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2343 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2344 elems.wmm_param_len);
2347 ieee80211_bss_info_change_notify(sdata, changed);
2351 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2352 struct ieee80211_if_sta *ifsta,
2353 struct ieee80211_mgmt *mgmt,
2355 struct ieee80211_rx_status *rx_status)
2357 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2358 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2360 struct sk_buff *skb;
2361 struct ieee80211_mgmt *resp;
2363 DECLARE_MAC_BUF(mac);
2364 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2365 DECLARE_MAC_BUF(mac2);
2366 DECLARE_MAC_BUF(mac3);
2369 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2370 ifsta->state != IEEE80211_IBSS_JOINED ||
2371 len < 24 + 2 || !ifsta->probe_resp)
2374 if (local->ops->tx_last_beacon)
2375 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2379 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2380 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2381 "%s (tx_last_beacon=%d)\n",
2382 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2383 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2384 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2386 if (!tx_last_beacon)
2389 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2390 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2393 end = ((u8 *) mgmt) + len;
2394 pos = mgmt->u.probe_req.variable;
2395 if (pos[0] != WLAN_EID_SSID ||
2396 pos + 2 + pos[1] > end) {
2397 if (net_ratelimit()) {
2398 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2400 dev->name, print_mac(mac, mgmt->sa));
2405 (pos[1] != ifsta->ssid_len ||
2406 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2407 /* Ignore ProbeReq for foreign SSID */
2411 /* Reply with ProbeResp */
2412 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2416 resp = (struct ieee80211_mgmt *) skb->data;
2417 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2418 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2419 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2420 dev->name, print_mac(mac, resp->da));
2421 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2422 ieee80211_sta_tx(dev, skb, 0);
2425 static void ieee80211_rx_mgmt_action(struct net_device *dev,
2426 struct ieee80211_if_sta *ifsta,
2427 struct ieee80211_mgmt *mgmt,
2430 if (len < IEEE80211_MIN_ACTION_SIZE)
2433 switch (mgmt->u.action.category) {
2434 case WLAN_CATEGORY_BACK:
2435 switch (mgmt->u.action.u.addba_req.action_code) {
2436 case WLAN_ACTION_ADDBA_REQ:
2437 if (len < (IEEE80211_MIN_ACTION_SIZE +
2438 sizeof(mgmt->u.action.u.addba_req)))
2440 ieee80211_sta_process_addba_request(dev, mgmt, len);
2442 case WLAN_ACTION_ADDBA_RESP:
2443 if (len < (IEEE80211_MIN_ACTION_SIZE +
2444 sizeof(mgmt->u.action.u.addba_resp)))
2446 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2448 case WLAN_ACTION_DELBA:
2449 if (len < (IEEE80211_MIN_ACTION_SIZE +
2450 sizeof(mgmt->u.action.u.delba)))
2452 ieee80211_sta_process_delba(dev, mgmt, len);
2455 if (net_ratelimit())
2456 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
2466 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2467 struct ieee80211_rx_status *rx_status)
2469 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2470 struct ieee80211_sub_if_data *sdata;
2471 struct ieee80211_if_sta *ifsta;
2472 struct ieee80211_mgmt *mgmt;
2478 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2479 ifsta = &sdata->u.sta;
2481 mgmt = (struct ieee80211_mgmt *) skb->data;
2482 fc = le16_to_cpu(mgmt->frame_control);
2484 switch (fc & IEEE80211_FCTL_STYPE) {
2485 case IEEE80211_STYPE_PROBE_REQ:
2486 case IEEE80211_STYPE_PROBE_RESP:
2487 case IEEE80211_STYPE_BEACON:
2488 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2489 case IEEE80211_STYPE_AUTH:
2490 case IEEE80211_STYPE_ASSOC_RESP:
2491 case IEEE80211_STYPE_REASSOC_RESP:
2492 case IEEE80211_STYPE_DEAUTH:
2493 case IEEE80211_STYPE_DISASSOC:
2494 case IEEE80211_STYPE_ACTION:
2495 skb_queue_tail(&ifsta->skb_queue, skb);
2496 queue_work(local->hw.workqueue, &ifsta->work);
2499 printk(KERN_DEBUG "%s: received unknown management frame - "
2500 "stype=%d\n", dev->name,
2501 (fc & IEEE80211_FCTL_STYPE) >> 4);
2510 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2511 struct sk_buff *skb)
2513 struct ieee80211_rx_status *rx_status;
2514 struct ieee80211_sub_if_data *sdata;
2515 struct ieee80211_if_sta *ifsta;
2516 struct ieee80211_mgmt *mgmt;
2519 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2520 ifsta = &sdata->u.sta;
2522 rx_status = (struct ieee80211_rx_status *) skb->cb;
2523 mgmt = (struct ieee80211_mgmt *) skb->data;
2524 fc = le16_to_cpu(mgmt->frame_control);
2526 switch (fc & IEEE80211_FCTL_STYPE) {
2527 case IEEE80211_STYPE_PROBE_REQ:
2528 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2531 case IEEE80211_STYPE_PROBE_RESP:
2532 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2534 case IEEE80211_STYPE_BEACON:
2535 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2537 case IEEE80211_STYPE_AUTH:
2538 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2540 case IEEE80211_STYPE_ASSOC_RESP:
2541 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2543 case IEEE80211_STYPE_REASSOC_RESP:
2544 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
2546 case IEEE80211_STYPE_DEAUTH:
2547 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2549 case IEEE80211_STYPE_DISASSOC:
2550 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2552 case IEEE80211_STYPE_ACTION:
2553 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len);
2561 ieee80211_txrx_result
2562 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2563 struct ieee80211_rx_status *rx_status)
2565 struct ieee80211_mgmt *mgmt;
2571 mgmt = (struct ieee80211_mgmt *) skb->data;
2572 fc = le16_to_cpu(mgmt->frame_control);
2574 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
2575 return TXRX_CONTINUE;
2580 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2581 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2582 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2583 skb->len, rx_status);
2586 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2587 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2593 return TXRX_CONTINUE;
2597 static int ieee80211_sta_active_ibss(struct net_device *dev)
2599 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2601 struct sta_info *sta;
2603 read_lock_bh(&local->sta_lock);
2604 list_for_each_entry(sta, &local->sta_list, list) {
2605 if (sta->dev == dev &&
2606 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2612 read_unlock_bh(&local->sta_lock);
2618 static void ieee80211_sta_expire(struct net_device *dev)
2620 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2621 struct sta_info *sta, *tmp;
2622 LIST_HEAD(tmp_list);
2623 DECLARE_MAC_BUF(mac);
2625 write_lock_bh(&local->sta_lock);
2626 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2627 if (time_after(jiffies, sta->last_rx +
2628 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
2629 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2630 dev->name, print_mac(mac, sta->addr));
2631 __sta_info_get(sta);
2632 sta_info_remove(sta);
2633 list_add(&sta->list, &tmp_list);
2635 write_unlock_bh(&local->sta_lock);
2637 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
2644 static void ieee80211_sta_merge_ibss(struct net_device *dev,
2645 struct ieee80211_if_sta *ifsta)
2647 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2649 ieee80211_sta_expire(dev);
2650 if (ieee80211_sta_active_ibss(dev))
2653 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2654 "IBSS networks with same SSID (merge)\n", dev->name);
2655 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2659 void ieee80211_sta_timer(unsigned long data)
2661 struct ieee80211_sub_if_data *sdata =
2662 (struct ieee80211_sub_if_data *) data;
2663 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2664 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2666 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2667 queue_work(local->hw.workqueue, &ifsta->work);
2671 void ieee80211_sta_work(struct work_struct *work)
2673 struct ieee80211_sub_if_data *sdata =
2674 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2675 struct net_device *dev = sdata->dev;
2676 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2677 struct ieee80211_if_sta *ifsta;
2678 struct sk_buff *skb;
2680 if (!netif_running(dev))
2683 if (local->sta_sw_scanning || local->sta_hw_scanning)
2686 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2687 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
2688 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2689 "(type=%d)\n", dev->name, sdata->vif.type);
2692 ifsta = &sdata->u.sta;
2694 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2695 ieee80211_sta_rx_queued_mgmt(dev, skb);
2697 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2698 ifsta->state != IEEE80211_ASSOCIATE &&
2699 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2700 if (ifsta->scan_ssid_len)
2701 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
2703 ieee80211_sta_start_scan(dev, NULL, 0);
2707 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2708 if (ieee80211_sta_config_auth(dev, ifsta))
2710 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2711 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2714 switch (ifsta->state) {
2715 case IEEE80211_DISABLED:
2717 case IEEE80211_AUTHENTICATE:
2718 ieee80211_authenticate(dev, ifsta);
2720 case IEEE80211_ASSOCIATE:
2721 ieee80211_associate(dev, ifsta);
2723 case IEEE80211_ASSOCIATED:
2724 ieee80211_associated(dev, ifsta);
2726 case IEEE80211_IBSS_SEARCH:
2727 ieee80211_sta_find_ibss(dev, ifsta);
2729 case IEEE80211_IBSS_JOINED:
2730 ieee80211_sta_merge_ibss(dev, ifsta);
2733 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2738 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2739 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2740 "mixed-cell disabled - disassociate\n", dev->name);
2742 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2743 ieee80211_set_disassoc(dev, ifsta, 0);
2748 static void ieee80211_sta_reset_auth(struct net_device *dev,
2749 struct ieee80211_if_sta *ifsta)
2751 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2753 if (local->ops->reset_tsf) {
2754 /* Reset own TSF to allow time synchronization work. */
2755 local->ops->reset_tsf(local_to_hw(local));
2758 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2761 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2762 ifsta->auth_alg = WLAN_AUTH_OPEN;
2763 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2764 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2765 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2766 ifsta->auth_alg = WLAN_AUTH_LEAP;
2768 ifsta->auth_alg = WLAN_AUTH_OPEN;
2769 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2771 ifsta->auth_transaction = -1;
2772 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2773 ifsta->auth_tries = ifsta->assoc_tries = 0;
2774 netif_carrier_off(dev);
2778 void ieee80211_sta_req_auth(struct net_device *dev,
2779 struct ieee80211_if_sta *ifsta)
2781 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2782 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2784 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2787 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2788 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2789 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2790 IEEE80211_STA_AUTO_SSID_SEL))) {
2791 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2792 queue_work(local->hw.workqueue, &ifsta->work);
2796 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2797 const char *ssid, int ssid_len)
2799 int tmp, hidden_ssid;
2801 if (ssid_len == ifsta->ssid_len &&
2802 !memcmp(ifsta->ssid, ssid, ssid_len))
2805 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2811 if (ssid[tmp] != '\0') {
2817 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2820 if (ssid_len == 1 && ssid[0] == ' ')
2826 static int ieee80211_sta_config_auth(struct net_device *dev,
2827 struct ieee80211_if_sta *ifsta)
2829 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2830 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2831 struct ieee80211_sta_bss *bss, *selected = NULL;
2832 int top_rssi = 0, freq;
2834 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2835 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
2836 ifsta->state = IEEE80211_AUTHENTICATE;
2837 ieee80211_sta_reset_auth(dev, ifsta);
2841 spin_lock_bh(&local->sta_bss_lock);
2842 freq = local->oper_channel->center_freq;
2843 list_for_each_entry(bss, &local->sta_bss_list, list) {
2844 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2847 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2848 !!sdata->default_key)
2851 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2855 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2856 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2859 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2860 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2863 if (!selected || top_rssi < bss->rssi) {
2865 top_rssi = bss->rssi;
2869 atomic_inc(&selected->users);
2870 spin_unlock_bh(&local->sta_bss_lock);
2873 ieee80211_set_freq(local, selected->freq);
2874 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2875 ieee80211_sta_set_ssid(dev, selected->ssid,
2876 selected->ssid_len);
2877 ieee80211_sta_set_bssid(dev, selected->bssid);
2878 ieee80211_rx_bss_put(dev, selected);
2879 ifsta->state = IEEE80211_AUTHENTICATE;
2880 ieee80211_sta_reset_auth(dev, ifsta);
2883 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2884 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2885 ieee80211_sta_start_scan(dev, NULL, 0);
2887 ieee80211_sta_start_scan(dev, ifsta->ssid,
2889 ifsta->state = IEEE80211_AUTHENTICATE;
2890 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2892 ifsta->state = IEEE80211_DISABLED;
2897 static int ieee80211_sta_join_ibss(struct net_device *dev,
2898 struct ieee80211_if_sta *ifsta,
2899 struct ieee80211_sta_bss *bss)
2901 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2902 int res, rates, i, j;
2903 struct sk_buff *skb;
2904 struct ieee80211_mgmt *mgmt;
2905 struct ieee80211_tx_control control;
2906 struct rate_selection ratesel;
2908 struct ieee80211_sub_if_data *sdata;
2909 struct ieee80211_supported_band *sband;
2911 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2913 /* Remove possible STA entries from other IBSS networks. */
2914 sta_info_flush(local, NULL);
2916 if (local->ops->reset_tsf) {
2917 /* Reset own TSF to allow time synchronization work. */
2918 local->ops->reset_tsf(local_to_hw(local));
2920 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2921 res = ieee80211_if_config(dev);
2925 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2927 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2928 sdata->drop_unencrypted = bss->capability &
2929 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2931 res = ieee80211_set_freq(local, bss->freq);
2933 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2934 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2935 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2939 /* Set beacon template based on scan results */
2940 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2945 skb_reserve(skb, local->hw.extra_tx_headroom);
2947 mgmt = (struct ieee80211_mgmt *)
2948 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2949 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2950 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2951 IEEE80211_STYPE_BEACON);
2952 memset(mgmt->da, 0xff, ETH_ALEN);
2953 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2954 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2955 mgmt->u.beacon.beacon_int =
2956 cpu_to_le16(local->hw.conf.beacon_int);
2957 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2959 pos = skb_put(skb, 2 + ifsta->ssid_len);
2960 *pos++ = WLAN_EID_SSID;
2961 *pos++ = ifsta->ssid_len;
2962 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2964 rates = bss->supp_rates_len;
2967 pos = skb_put(skb, 2 + rates);
2968 *pos++ = WLAN_EID_SUPP_RATES;
2970 memcpy(pos, bss->supp_rates, rates);
2972 if (bss->band == IEEE80211_BAND_2GHZ) {
2973 pos = skb_put(skb, 2 + 1);
2974 *pos++ = WLAN_EID_DS_PARAMS;
2976 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2979 pos = skb_put(skb, 2 + 2);
2980 *pos++ = WLAN_EID_IBSS_PARAMS;
2982 /* FIX: set ATIM window based on scan results */
2986 if (bss->supp_rates_len > 8) {
2987 rates = bss->supp_rates_len - 8;
2988 pos = skb_put(skb, 2 + rates);
2989 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2991 memcpy(pos, &bss->supp_rates[8], rates);
2994 memset(&control, 0, sizeof(control));
2995 rate_control_get_rate(dev, sband, skb, &ratesel);
2996 if (!ratesel.rate) {
2997 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2998 "for IBSS beacon\n", dev->name);
3001 control.vif = &sdata->vif;
3002 control.tx_rate = ratesel.rate;
3003 if (sdata->bss_conf.use_short_preamble &&
3004 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
3005 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
3006 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
3007 control.flags |= IEEE80211_TXCTL_NO_ACK;
3008 control.retry_limit = 1;
3010 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
3011 if (ifsta->probe_resp) {
3012 mgmt = (struct ieee80211_mgmt *)
3013 ifsta->probe_resp->data;
3014 mgmt->frame_control =
3015 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
3016 IEEE80211_STYPE_PROBE_RESP);
3018 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
3019 "template for IBSS\n", dev->name);
3022 if (local->ops->beacon_update &&
3023 local->ops->beacon_update(local_to_hw(local),
3024 skb, &control) == 0) {
3025 printk(KERN_DEBUG "%s: Configured IBSS beacon "
3026 "template based on scan results\n", dev->name);
3031 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
3032 for (i = 0; i < bss->supp_rates_len; i++) {
3033 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
3034 for (j = 0; j < sband->n_bitrates; j++)
3035 if (sband->bitrates[j].bitrate == bitrate)
3038 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
3042 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
3043 "template\n", dev->name);
3047 ifsta->state = IEEE80211_IBSS_JOINED;
3048 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3050 ieee80211_rx_bss_put(dev, bss);
3056 static int ieee80211_sta_create_ibss(struct net_device *dev,
3057 struct ieee80211_if_sta *ifsta)
3059 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3060 struct ieee80211_sta_bss *bss;
3061 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3062 struct ieee80211_supported_band *sband;
3063 u8 bssid[ETH_ALEN], *pos;
3065 DECLARE_MAC_BUF(mac);
3068 /* Easier testing, use fixed BSSID. */
3069 memset(bssid, 0xfe, ETH_ALEN);
3071 /* Generate random, not broadcast, locally administered BSSID. Mix in
3072 * own MAC address to make sure that devices that do not have proper
3073 * random number generator get different BSSID. */
3074 get_random_bytes(bssid, ETH_ALEN);
3075 for (i = 0; i < ETH_ALEN; i++)
3076 bssid[i] ^= dev->dev_addr[i];
3081 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3082 dev->name, print_mac(mac, bssid));
3084 bss = ieee80211_rx_bss_add(dev, bssid,
3085 local->hw.conf.channel->center_freq,
3086 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3090 bss->band = local->hw.conf.channel->band;
3091 sband = local->hw.wiphy->bands[bss->band];
3093 if (local->hw.conf.beacon_int == 0)
3094 local->hw.conf.beacon_int = 100;
3095 bss->beacon_int = local->hw.conf.beacon_int;
3096 bss->last_update = jiffies;
3097 bss->capability = WLAN_CAPABILITY_IBSS;
3098 if (sdata->default_key) {
3099 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3101 sdata->drop_unencrypted = 0;
3102 bss->supp_rates_len = sband->n_bitrates;
3103 pos = bss->supp_rates;
3104 for (i = 0; i < sband->n_bitrates; i++) {
3105 int rate = sband->bitrates[i].bitrate;
3106 *pos++ = (u8) (rate / 5);
3109 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3113 static int ieee80211_sta_find_ibss(struct net_device *dev,
3114 struct ieee80211_if_sta *ifsta)
3116 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3117 struct ieee80211_sta_bss *bss;
3121 DECLARE_MAC_BUF(mac);
3122 DECLARE_MAC_BUF(mac2);
3124 if (ifsta->ssid_len == 0)
3127 active_ibss = ieee80211_sta_active_ibss(dev);
3128 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3129 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3130 dev->name, active_ibss);
3131 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3132 spin_lock_bh(&local->sta_bss_lock);
3133 list_for_each_entry(bss, &local->sta_bss_list, list) {
3134 if (ifsta->ssid_len != bss->ssid_len ||
3135 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3136 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3138 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3139 printk(KERN_DEBUG " bssid=%s found\n",
3140 print_mac(mac, bss->bssid));
3141 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3142 memcpy(bssid, bss->bssid, ETH_ALEN);
3144 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3147 spin_unlock_bh(&local->sta_bss_lock);
3149 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3150 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3151 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
3152 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3153 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
3154 (bss = ieee80211_rx_bss_get(dev, bssid,
3155 local->hw.conf.channel->center_freq,
3156 ifsta->ssid, ifsta->ssid_len))) {
3157 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3158 " based on configured SSID\n",
3159 dev->name, print_mac(mac, bssid));
3160 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3162 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3163 printk(KERN_DEBUG " did not try to join ibss\n");
3164 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3166 /* Selected IBSS not found in current scan results - try to scan */
3167 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3168 !ieee80211_sta_active_ibss(dev)) {
3169 mod_timer(&ifsta->timer, jiffies +
3170 IEEE80211_IBSS_MERGE_INTERVAL);
3171 } else if (time_after(jiffies, local->last_scan_completed +
3172 IEEE80211_SCAN_INTERVAL)) {
3173 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3174 "join\n", dev->name);
3175 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3177 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3178 int interval = IEEE80211_SCAN_INTERVAL;
3180 if (time_after(jiffies, ifsta->ibss_join_req +
3181 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3182 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3183 (!(local->oper_channel->flags &
3184 IEEE80211_CHAN_NO_IBSS)))
3185 return ieee80211_sta_create_ibss(dev, ifsta);
3186 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3187 printk(KERN_DEBUG "%s: IBSS not allowed on"
3188 " %d MHz\n", dev->name,
3189 local->hw.conf.channel->center_freq);
3192 /* No IBSS found - decrease scan interval and continue
3194 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3197 ifsta->state = IEEE80211_IBSS_SEARCH;
3198 mod_timer(&ifsta->timer, jiffies + interval);
3206 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3208 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3209 struct ieee80211_if_sta *ifsta;
3210 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3212 if (len > IEEE80211_MAX_SSID_LEN)
3215 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
3217 if (local->ops->conf_tx) {
3218 struct ieee80211_tx_queue_params qparam;
3221 memset(&qparam, 0, sizeof(qparam));
3225 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
3226 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
3231 qparam.cw_max = 1023;
3232 qparam.burst_time = 0;
3234 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
3235 local->ops->conf_tx(local_to_hw(local),
3236 i + IEEE80211_TX_QUEUE_DATA0,
3239 /* IBSS uses different parameters for Beacon sending */
3243 local->ops->conf_tx(local_to_hw(local),
3244 IEEE80211_TX_QUEUE_BEACON, &qparam);
3247 ifsta = &sdata->u.sta;
3249 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
3250 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3251 memcpy(ifsta->ssid, ssid, len);
3252 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3253 ifsta->ssid_len = len;
3256 ifsta->flags |= IEEE80211_STA_SSID_SET;
3258 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3259 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3260 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3261 ifsta->ibss_join_req = jiffies;
3262 ifsta->state = IEEE80211_IBSS_SEARCH;
3263 return ieee80211_sta_find_ibss(dev, ifsta);
3269 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3271 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3272 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3273 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3274 *len = ifsta->ssid_len;
3279 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3281 struct ieee80211_sub_if_data *sdata;
3282 struct ieee80211_if_sta *ifsta;
3285 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3286 ifsta = &sdata->u.sta;
3288 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3289 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3290 res = ieee80211_if_config(dev);
3292 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3293 "the low-level driver\n", dev->name);
3298 if (is_valid_ether_addr(bssid))
3299 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3301 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3307 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3308 struct ieee80211_sub_if_data *sdata,
3311 struct sk_buff *skb;
3312 struct ieee80211_hdr *nullfunc;
3315 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3317 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3318 "frame\n", sdata->dev->name);
3321 skb_reserve(skb, local->hw.extra_tx_headroom);
3323 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3324 memset(nullfunc, 0, 24);
3325 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3326 IEEE80211_FCTL_TODS;
3328 fc |= IEEE80211_FCTL_PM;
3329 nullfunc->frame_control = cpu_to_le16(fc);
3330 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3331 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3332 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3334 ieee80211_sta_tx(sdata->dev, skb, 0);
3338 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3340 struct ieee80211_local *local = hw_to_local(hw);
3341 struct net_device *dev = local->scan_dev;
3342 struct ieee80211_sub_if_data *sdata;
3343 union iwreq_data wrqu;
3345 local->last_scan_completed = jiffies;
3346 memset(&wrqu, 0, sizeof(wrqu));
3347 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3349 if (local->sta_hw_scanning) {
3350 local->sta_hw_scanning = 0;
3354 local->sta_sw_scanning = 0;
3355 if (ieee80211_hw_config(local))
3356 printk(KERN_DEBUG "%s: failed to restore operational "
3357 "channel after scan\n", dev->name);
3360 netif_tx_lock_bh(local->mdev);
3361 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3362 local->ops->configure_filter(local_to_hw(local),
3363 FIF_BCN_PRBRESP_PROMISC,
3364 &local->filter_flags,
3365 local->mdev->mc_count,
3366 local->mdev->mc_list);
3368 netif_tx_unlock_bh(local->mdev);
3371 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3373 /* No need to wake the master device. */
3374 if (sdata->dev == local->mdev)
3377 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3378 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3379 ieee80211_send_nullfunc(local, sdata, 0);
3380 ieee80211_sta_timer((unsigned long)sdata);
3383 netif_wake_queue(sdata->dev);
3388 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3389 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3390 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3391 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3392 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3393 !ieee80211_sta_active_ibss(dev)))
3394 ieee80211_sta_find_ibss(dev, ifsta);
3397 EXPORT_SYMBOL(ieee80211_scan_completed);
3399 void ieee80211_sta_scan_work(struct work_struct *work)
3401 struct ieee80211_local *local =
3402 container_of(work, struct ieee80211_local, scan_work.work);
3403 struct net_device *dev = local->scan_dev;
3404 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3405 struct ieee80211_supported_band *sband;
3406 struct ieee80211_channel *chan;
3408 unsigned long next_delay = 0;
3410 if (!local->sta_sw_scanning)
3413 switch (local->scan_state) {
3414 case SCAN_SET_CHANNEL:
3415 /* get current scan band */
3416 if (local->scan_band < IEEE80211_NUM_BANDS)
3417 sband = local->hw.wiphy->bands[local->scan_band];
3421 /* if we started at an unsupported one, advance */
3422 while (!sband && local->scan_band < IEEE80211_NUM_BANDS) {
3424 sband = local->hw.wiphy->bands[local->scan_band];
3425 local->scan_channel_idx = 0;
3429 (local->scan_channel_idx >= sband->n_channels &&
3430 local->scan_band >= IEEE80211_NUM_BANDS)) {
3431 ieee80211_scan_completed(local_to_hw(local));
3435 chan = &sband->channels[local->scan_channel_idx];
3437 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3438 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3439 chan->flags & IEEE80211_CHAN_NO_IBSS))
3443 local->scan_channel = chan;
3444 if (ieee80211_hw_config(local)) {
3445 printk(KERN_DEBUG "%s: failed to set freq to "
3446 "%d MHz for scan\n", dev->name,
3452 local->scan_channel_idx++;
3453 if (local->scan_channel_idx >= sband->n_channels) {
3455 local->scan_channel_idx = 0;
3461 next_delay = IEEE80211_PROBE_DELAY +
3462 usecs_to_jiffies(local->hw.channel_change_time);
3463 local->scan_state = SCAN_SEND_PROBE;
3465 case SCAN_SEND_PROBE:
3466 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3467 local->scan_state = SCAN_SET_CHANNEL;
3469 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3471 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3472 local->scan_ssid_len);
3473 next_delay = IEEE80211_CHANNEL_TIME;
3477 if (local->sta_sw_scanning)
3478 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3483 static int ieee80211_sta_start_scan(struct net_device *dev,
3484 u8 *ssid, size_t ssid_len)
3486 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3487 struct ieee80211_sub_if_data *sdata;
3489 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3492 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3493 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3496 * ScanType: ACTIVE, PASSIVE
3497 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3498 * a Probe frame during active scanning
3500 * MinChannelTime (>= ProbeDelay), in TU
3501 * MaxChannelTime: (>= MinChannelTime), in TU
3504 /* MLME-SCAN.confirm
3506 * ResultCode: SUCCESS, INVALID_PARAMETERS
3509 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3510 if (local->scan_dev == dev)
3515 if (local->ops->hw_scan) {
3516 int rc = local->ops->hw_scan(local_to_hw(local),
3519 local->sta_hw_scanning = 1;
3520 local->scan_dev = dev;
3525 local->sta_sw_scanning = 1;
3528 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3530 /* Don't stop the master interface, otherwise we can't transmit
3532 if (sdata->dev == local->mdev)
3535 netif_stop_queue(sdata->dev);
3536 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3537 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
3538 ieee80211_send_nullfunc(local, sdata, 1);
3543 local->scan_ssid_len = ssid_len;
3544 memcpy(local->scan_ssid, ssid, ssid_len);
3546 local->scan_ssid_len = 0;
3547 local->scan_state = SCAN_SET_CHANNEL;
3548 local->scan_channel_idx = 0;
3549 local->scan_band = IEEE80211_BAND_2GHZ;
3550 local->scan_dev = dev;
3552 netif_tx_lock_bh(local->mdev);
3553 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3554 local->ops->configure_filter(local_to_hw(local),
3555 FIF_BCN_PRBRESP_PROMISC,
3556 &local->filter_flags,
3557 local->mdev->mc_count,
3558 local->mdev->mc_list);
3559 netif_tx_unlock_bh(local->mdev);
3561 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3562 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3563 IEEE80211_CHANNEL_TIME);
3569 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3571 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3572 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3573 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3575 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3576 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3578 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3579 if (local->scan_dev == dev)
3584 ifsta->scan_ssid_len = ssid_len;
3586 memcpy(ifsta->scan_ssid, ssid, ssid_len);
3587 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3588 queue_work(local->hw.workqueue, &ifsta->work);
3593 ieee80211_sta_scan_result(struct net_device *dev,
3594 struct ieee80211_sta_bss *bss,
3595 char *current_ev, char *end_buf)
3597 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3598 struct iw_event iwe;
3600 if (time_after(jiffies,
3601 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3604 memset(&iwe, 0, sizeof(iwe));
3605 iwe.cmd = SIOCGIWAP;
3606 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3607 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3608 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3611 memset(&iwe, 0, sizeof(iwe));
3612 iwe.cmd = SIOCGIWESSID;
3613 iwe.u.data.length = bss->ssid_len;
3614 iwe.u.data.flags = 1;
3615 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3618 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
3619 memset(&iwe, 0, sizeof(iwe));
3620 iwe.cmd = SIOCGIWMODE;
3621 if (bss->capability & WLAN_CAPABILITY_ESS)
3622 iwe.u.mode = IW_MODE_MASTER;
3624 iwe.u.mode = IW_MODE_ADHOC;
3625 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3629 memset(&iwe, 0, sizeof(iwe));
3630 iwe.cmd = SIOCGIWFREQ;
3631 iwe.u.freq.m = bss->freq;
3633 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3636 memset(&iwe, 0, sizeof(iwe));
3637 iwe.cmd = SIOCGIWFREQ;
3638 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3640 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3643 memset(&iwe, 0, sizeof(iwe));
3645 iwe.u.qual.qual = bss->signal;
3646 iwe.u.qual.level = bss->rssi;
3647 iwe.u.qual.noise = bss->noise;
3648 iwe.u.qual.updated = local->wstats_flags;
3649 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3652 memset(&iwe, 0, sizeof(iwe));
3653 iwe.cmd = SIOCGIWENCODE;
3654 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3655 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3657 iwe.u.data.flags = IW_ENCODE_DISABLED;
3658 iwe.u.data.length = 0;
3659 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3661 if (bss && bss->wpa_ie) {
3662 memset(&iwe, 0, sizeof(iwe));
3663 iwe.cmd = IWEVGENIE;
3664 iwe.u.data.length = bss->wpa_ie_len;
3665 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3669 if (bss && bss->rsn_ie) {
3670 memset(&iwe, 0, sizeof(iwe));
3671 iwe.cmd = IWEVGENIE;
3672 iwe.u.data.length = bss->rsn_ie_len;
3673 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3677 if (bss && bss->supp_rates_len > 0) {
3678 /* display all supported rates in readable format */
3679 char *p = current_ev + IW_EV_LCP_LEN;
3682 memset(&iwe, 0, sizeof(iwe));
3683 iwe.cmd = SIOCGIWRATE;
3684 /* Those two flags are ignored... */
3685 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3687 for (i = 0; i < bss->supp_rates_len; i++) {
3688 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3690 p = iwe_stream_add_value(current_ev, p,
3691 end_buf, &iwe, IW_EV_PARAM_LEN);
3698 buf = kmalloc(30, GFP_ATOMIC);
3700 memset(&iwe, 0, sizeof(iwe));
3701 iwe.cmd = IWEVCUSTOM;
3702 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3703 iwe.u.data.length = strlen(buf);
3704 current_ev = iwe_stream_add_point(current_ev, end_buf,
3714 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3716 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3717 char *current_ev = buf;
3718 char *end_buf = buf + len;
3719 struct ieee80211_sta_bss *bss;
3721 spin_lock_bh(&local->sta_bss_lock);
3722 list_for_each_entry(bss, &local->sta_bss_list, list) {
3723 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3724 spin_unlock_bh(&local->sta_bss_lock);
3727 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3730 spin_unlock_bh(&local->sta_bss_lock);
3731 return current_ev - buf;
3735 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3737 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3738 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3739 kfree(ifsta->extra_ie);
3741 ifsta->extra_ie = NULL;
3742 ifsta->extra_ie_len = 0;
3745 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3746 if (!ifsta->extra_ie) {
3747 ifsta->extra_ie_len = 0;
3750 memcpy(ifsta->extra_ie, ie, len);
3751 ifsta->extra_ie_len = len;
3756 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3757 struct sk_buff *skb, u8 *bssid,
3760 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3761 struct sta_info *sta;
3762 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3763 DECLARE_MAC_BUF(mac);
3765 /* TODO: Could consider removing the least recently used entry and
3766 * allow new one to be added. */
3767 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3768 if (net_ratelimit()) {
3769 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3770 "entry %s\n", dev->name, print_mac(mac, addr));
3775 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
3776 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
3778 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3782 sta->supp_rates[local->hw.conf.channel->band] =
3783 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
3785 rate_control_rate_init(sta, local);
3787 return sta; /* caller will call sta_info_put() */
3791 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3793 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3794 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3796 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3799 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3800 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
3803 ieee80211_send_deauth(dev, ifsta, reason);
3804 ieee80211_set_disassoc(dev, ifsta, 1);
3809 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3811 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3812 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3814 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3817 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3820 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
3823 ieee80211_send_disassoc(dev, ifsta, reason);
3824 ieee80211_set_disassoc(dev, ifsta, 0);