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 <linux/rtnetlink.h>
28 #include <net/iw_handler.h>
29 #include <net/mac80211.h>
31 #include "ieee80211_i.h"
36 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
37 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38 #define IEEE80211_AUTH_MAX_TRIES 3
39 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40 #define IEEE80211_ASSOC_MAX_TRIES 3
41 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
42 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
43 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
44 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
46 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
49 #define IEEE80211_PROBE_DELAY (HZ / 33)
50 #define IEEE80211_CHANNEL_TIME (HZ / 33)
51 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
55 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
57 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
60 /* mgmt header + 1 byte category 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
75 static struct ieee80211_sta_bss *
76 ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
77 u8 *ssid, u8 ssid_len)
79 struct ieee80211_sta_bss *bss;
81 spin_lock_bh(&local->sta_bss_lock);
82 bss = local->sta_bss_hash[STA_HASH(bssid)];
84 if (!bss_mesh_cfg(bss) &&
85 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
87 bss->ssid_len == ssid_len &&
88 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
89 atomic_inc(&bss->users);
94 spin_unlock_bh(&local->sta_bss_lock);
98 /* Caller must hold local->sta_bss_lock */
99 static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
100 struct ieee80211_sta_bss *bss)
104 if (bss_mesh_cfg(bss))
105 hash_idx = mesh_id_hash(bss_mesh_id(bss),
106 bss_mesh_id_len(bss));
108 hash_idx = STA_HASH(bss->bssid);
110 bss->hnext = local->sta_bss_hash[hash_idx];
111 local->sta_bss_hash[hash_idx] = bss;
114 /* Caller must hold local->sta_bss_lock */
115 static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
116 struct ieee80211_sta_bss *bss)
118 struct ieee80211_sta_bss *b, *prev = NULL;
119 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
123 local->sta_bss_hash[STA_HASH(bss->bssid)] =
126 prev->hnext = bss->hnext;
134 static struct ieee80211_sta_bss *
135 ieee80211_rx_bss_add(struct ieee80211_sub_if_data *sdata, u8 *bssid, int freq,
136 u8 *ssid, u8 ssid_len)
138 struct ieee80211_local *local = sdata->local;
139 struct ieee80211_sta_bss *bss;
141 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
144 atomic_inc(&bss->users);
145 atomic_inc(&bss->users);
146 memcpy(bss->bssid, bssid, ETH_ALEN);
148 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
149 memcpy(bss->ssid, ssid, ssid_len);
150 bss->ssid_len = ssid_len;
153 spin_lock_bh(&local->sta_bss_lock);
154 /* TODO: order by RSSI? */
155 list_add_tail(&bss->list, &local->sta_bss_list);
156 __ieee80211_rx_bss_hash_add(local, bss);
157 spin_unlock_bh(&local->sta_bss_lock);
161 #ifdef CONFIG_MAC80211_MESH
162 static struct ieee80211_sta_bss *
163 ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
164 u8 *mesh_cfg, int freq)
166 struct ieee80211_sta_bss *bss;
168 spin_lock_bh(&local->sta_bss_lock);
169 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
171 if (bss_mesh_cfg(bss) &&
172 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
174 mesh_id_len == bss->mesh_id_len &&
175 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
177 atomic_inc(&bss->users);
182 spin_unlock_bh(&local->sta_bss_lock);
186 static struct ieee80211_sta_bss *
187 ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
188 u8 *mesh_cfg, int mesh_config_len, int freq)
190 struct ieee80211_sta_bss *bss;
192 if (mesh_config_len != MESH_CFG_LEN)
195 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
199 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
200 if (!bss->mesh_cfg) {
205 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
206 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
208 kfree(bss->mesh_cfg);
212 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
215 atomic_inc(&bss->users);
216 atomic_inc(&bss->users);
217 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
218 bss->mesh_id_len = mesh_id_len;
220 spin_lock_bh(&local->sta_bss_lock);
221 /* TODO: order by RSSI? */
222 list_add_tail(&bss->list, &local->sta_bss_list);
223 __ieee80211_rx_bss_hash_add(local, bss);
224 spin_unlock_bh(&local->sta_bss_lock);
229 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
232 kfree(bss_mesh_id(bss));
233 kfree(bss_mesh_cfg(bss));
237 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
238 struct ieee80211_sta_bss *bss)
241 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
246 __ieee80211_rx_bss_hash_del(local, bss);
247 list_del(&bss->list);
248 spin_unlock_bh(&local->sta_bss_lock);
249 ieee80211_rx_bss_free(bss);
252 void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
254 spin_lock_init(&local->sta_bss_lock);
255 INIT_LIST_HEAD(&local->sta_bss_list);
258 void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
260 struct ieee80211_sta_bss *bss, *tmp;
262 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
263 ieee80211_rx_bss_put(local, bss);
266 static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
273 end = pos + bss->ies_len;
275 while (pos + 1 < end) {
276 if (pos + 2 + pos[1] > end)
287 static int ecw2cw(int ecw)
289 return (1 << ecw) - 1;
292 /* frame sending functions */
293 void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
296 skb->dev = sdata->local->mdev;
297 skb_set_mac_header(skb, 0);
298 skb_set_network_header(skb, 0);
299 skb_set_transport_header(skb, 0);
301 skb->iif = sdata->dev->ifindex;
302 skb->do_not_encrypt = !encrypt;
307 static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
308 struct ieee80211_if_sta *ifsta,
309 int transaction, u8 *extra, size_t extra_len,
312 struct ieee80211_local *local = sdata->local;
314 struct ieee80211_mgmt *mgmt;
316 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
317 sizeof(*mgmt) + 6 + extra_len);
319 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
320 "frame\n", sdata->dev->name);
323 skb_reserve(skb, local->hw.extra_tx_headroom);
325 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
326 memset(mgmt, 0, 24 + 6);
327 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
328 IEEE80211_STYPE_AUTH);
330 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
331 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
332 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
333 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
334 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
335 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
336 ifsta->auth_transaction = transaction + 1;
337 mgmt->u.auth.status_code = cpu_to_le16(0);
339 memcpy(skb_put(skb, extra_len), extra, extra_len);
341 ieee80211_sta_tx(sdata, skb, encrypt);
344 static void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
345 u8 *ssid, size_t ssid_len)
347 struct ieee80211_local *local = sdata->local;
348 struct ieee80211_supported_band *sband;
350 struct ieee80211_mgmt *mgmt;
351 u8 *pos, *supp_rates, *esupp_rates = NULL;
354 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
356 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
357 "request\n", sdata->dev->name);
360 skb_reserve(skb, local->hw.extra_tx_headroom);
362 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
364 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
365 IEEE80211_STYPE_PROBE_REQ);
366 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
368 memcpy(mgmt->da, dst, ETH_ALEN);
369 memcpy(mgmt->bssid, dst, ETH_ALEN);
371 memset(mgmt->da, 0xff, ETH_ALEN);
372 memset(mgmt->bssid, 0xff, ETH_ALEN);
374 pos = skb_put(skb, 2 + ssid_len);
375 *pos++ = WLAN_EID_SSID;
377 memcpy(pos, ssid, ssid_len);
379 supp_rates = skb_put(skb, 2);
380 supp_rates[0] = WLAN_EID_SUPP_RATES;
382 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
384 for (i = 0; i < sband->n_bitrates; i++) {
385 struct ieee80211_rate *rate = &sband->bitrates[i];
387 pos = skb_put(skb, 1);
389 } else if (supp_rates[1] == 8) {
390 esupp_rates = skb_put(skb, 3);
391 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
393 pos = &esupp_rates[2];
395 pos = skb_put(skb, 1);
398 *pos = rate->bitrate / 5;
401 ieee80211_sta_tx(sdata, skb, 0);
405 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
406 struct ieee80211_sta_bss *bss,
409 struct ieee80211_local *local = sdata->local;
410 int i, have_higher_than_11mbit = 0;
413 /* cf. IEEE 802.11 9.2.12 */
414 for (i = 0; i < bss->supp_rates_len; i++)
415 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
416 have_higher_than_11mbit = 1;
418 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
419 have_higher_than_11mbit)
420 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
422 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
425 if (local->ops->conf_tx) {
426 struct ieee80211_tx_queue_params qparam;
428 memset(&qparam, 0, sizeof(qparam));
432 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
433 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
438 qparam.cw_max = 1023;
441 for (i = 0; i < local_to_hw(local)->queues; i++)
442 local->ops->conf_tx(local_to_hw(local), i, &qparam);
446 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
447 struct ieee80211_if_sta *ifsta,
448 u8 *wmm_param, size_t wmm_param_len)
450 struct ieee80211_tx_queue_params params;
455 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
461 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
463 count = wmm_param[6] & 0x0f;
464 if (count == ifsta->wmm_last_param_set)
466 ifsta->wmm_last_param_set = count;
469 left = wmm_param_len - 8;
471 memset(¶ms, 0, sizeof(params));
473 if (!local->ops->conf_tx)
477 for (; left >= 4; left -= 4, pos += 4) {
478 int aci = (pos[0] >> 5) & 0x03;
479 int acm = (pos[0] >> 4) & 0x01;
486 local->wmm_acm |= BIT(0) | BIT(3);
491 local->wmm_acm |= BIT(4) | BIT(5);
496 local->wmm_acm |= BIT(6) | BIT(7);
502 local->wmm_acm |= BIT(1) | BIT(2);
506 params.aifs = pos[0] & 0x0f;
507 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
508 params.cw_min = ecw2cw(pos[1] & 0x0f);
509 params.txop = get_unaligned_le16(pos + 2);
510 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
511 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
512 "cWmin=%d cWmax=%d txop=%d\n",
513 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
514 params.cw_max, params.txop);
516 /* TODO: handle ACM (block TX, fallback to next lowest allowed
518 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
519 printk(KERN_DEBUG "%s: failed to set TX queue "
520 "parameters for queue %d\n", local->mdev->name, queue);
525 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
527 bool use_short_preamble)
529 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
530 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
531 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
532 DECLARE_MAC_BUF(mac);
536 if (use_protection != bss_conf->use_cts_prot) {
537 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
538 if (net_ratelimit()) {
539 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
542 use_protection ? "enabled" : "disabled",
543 print_mac(mac, ifsta->bssid));
546 bss_conf->use_cts_prot = use_protection;
547 changed |= BSS_CHANGED_ERP_CTS_PROT;
550 if (use_short_preamble != bss_conf->use_short_preamble) {
551 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
552 if (net_ratelimit()) {
553 printk(KERN_DEBUG "%s: switched to %s barker preamble"
556 use_short_preamble ? "short" : "long",
557 print_mac(mac, ifsta->bssid));
560 bss_conf->use_short_preamble = use_short_preamble;
561 changed |= BSS_CHANGED_ERP_PREAMBLE;
567 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
570 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
571 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
573 return ieee80211_handle_protect_preamb(sdata,
574 use_protection, use_short_preamble);
577 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
578 struct ieee80211_sta_bss *bss)
582 if (bss->has_erp_value)
583 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
585 u16 capab = bss->capability;
586 changed |= ieee80211_handle_protect_preamb(sdata, false,
587 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
593 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
594 struct ieee80211_ht_info *ht_info)
600 memset(ht_info, 0, sizeof(*ht_info));
603 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
605 ht_info->ht_supported = 1;
606 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
607 ht_info->ampdu_factor =
608 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
609 ht_info->ampdu_density =
610 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
611 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
613 ht_info->ht_supported = 0;
618 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
619 struct ieee80211_ht_addt_info *ht_add_info_ie,
620 struct ieee80211_ht_bss_info *bss_info)
622 if (bss_info == NULL)
625 memset(bss_info, 0, sizeof(*bss_info));
627 if (ht_add_info_ie) {
629 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
631 bss_info->primary_channel = ht_add_info_ie->control_chan;
632 bss_info->bss_cap = ht_add_info_ie->ht_param;
633 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
639 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
640 struct ieee80211_if_sta *ifsta)
642 union iwreq_data wrqu;
643 memset(&wrqu, 0, sizeof(wrqu));
644 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
645 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
646 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
647 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
650 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
651 struct ieee80211_if_sta *ifsta)
653 union iwreq_data wrqu;
655 if (ifsta->assocreq_ies) {
656 memset(&wrqu, 0, sizeof(wrqu));
657 wrqu.data.length = ifsta->assocreq_ies_len;
658 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
659 ifsta->assocreq_ies);
661 if (ifsta->assocresp_ies) {
662 memset(&wrqu, 0, sizeof(wrqu));
663 wrqu.data.length = ifsta->assocresp_ies_len;
664 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
665 ifsta->assocresp_ies);
670 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
671 struct ieee80211_if_sta *ifsta)
673 struct ieee80211_local *local = sdata->local;
674 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
675 u32 changed = BSS_CHANGED_ASSOC;
677 struct ieee80211_sta_bss *bss;
679 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
681 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
684 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
685 conf->channel->center_freq,
686 ifsta->ssid, ifsta->ssid_len);
688 /* set timing information */
689 sdata->bss_conf.beacon_int = bss->beacon_int;
690 sdata->bss_conf.timestamp = bss->timestamp;
691 sdata->bss_conf.dtim_period = bss->dtim_period;
693 changed |= ieee80211_handle_bss_capability(sdata, bss);
695 ieee80211_rx_bss_put(local, bss);
698 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
699 changed |= BSS_CHANGED_HT;
700 sdata->bss_conf.assoc_ht = 1;
701 sdata->bss_conf.ht_conf = &conf->ht_conf;
702 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
705 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
706 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
707 ieee80211_sta_send_associnfo(sdata, ifsta);
709 ifsta->last_probe = jiffies;
710 ieee80211_led_assoc(local, 1);
712 sdata->bss_conf.assoc = 1;
713 ieee80211_bss_info_change_notify(sdata, changed);
715 netif_tx_start_all_queues(sdata->dev);
716 netif_carrier_on(sdata->dev);
718 ieee80211_sta_send_apinfo(sdata, ifsta);
721 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
722 struct ieee80211_if_sta *ifsta)
724 DECLARE_MAC_BUF(mac);
726 ifsta->direct_probe_tries++;
727 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
728 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
729 sdata->dev->name, print_mac(mac, ifsta->bssid));
730 ifsta->state = IEEE80211_STA_MLME_DISABLED;
734 printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
735 sdata->dev->name, print_mac(mac, ifsta->bssid),
736 ifsta->direct_probe_tries);
738 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
740 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
742 /* Direct probe is sent to broadcast address as some APs
743 * will not answer to direct packet in unassociated state.
745 ieee80211_send_probe_req(sdata, NULL,
746 ifsta->ssid, ifsta->ssid_len);
748 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
752 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
753 struct ieee80211_if_sta *ifsta)
755 DECLARE_MAC_BUF(mac);
758 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
759 printk(KERN_DEBUG "%s: authentication with AP %s"
761 sdata->dev->name, print_mac(mac, ifsta->bssid));
762 ifsta->state = IEEE80211_STA_MLME_DISABLED;
766 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
767 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
768 sdata->dev->name, print_mac(mac, ifsta->bssid));
770 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
772 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
775 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
776 struct ieee80211_supported_band *sband,
782 for (i = 0; i < bss->supp_rates_len; i++) {
783 int rate = (bss->supp_rates[i] & 0x7F) * 5;
785 for (j = 0; j < sband->n_bitrates; j++)
786 if (sband->bitrates[j].bitrate == rate) {
796 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
797 struct ieee80211_if_sta *ifsta)
799 struct ieee80211_local *local = sdata->local;
801 struct ieee80211_mgmt *mgmt;
802 u8 *pos, *ies, *ht_add_ie;
803 int i, len, count, rates_len, supp_rates_len;
805 struct ieee80211_sta_bss *bss;
807 struct ieee80211_supported_band *sband;
810 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
811 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
814 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
815 "frame\n", sdata->dev->name);
818 skb_reserve(skb, local->hw.extra_tx_headroom);
820 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
822 capab = ifsta->capab;
824 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
825 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
826 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
827 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
828 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
831 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
832 local->hw.conf.channel->center_freq,
833 ifsta->ssid, ifsta->ssid_len);
835 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
836 capab |= WLAN_CAPABILITY_PRIVACY;
840 /* get all rates supported by the device and the AP as
841 * some APs don't like getting a superset of their rates
842 * in the association request (e.g. D-Link DAP 1353 in
844 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
846 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
847 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
848 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
850 ieee80211_rx_bss_put(local, bss);
853 rates_len = sband->n_bitrates;
856 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
858 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
859 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
860 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
862 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
864 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
865 IEEE80211_STYPE_REASSOC_REQ);
866 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
867 mgmt->u.reassoc_req.listen_interval =
868 cpu_to_le16(local->hw.conf.listen_interval);
869 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
873 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
874 IEEE80211_STYPE_ASSOC_REQ);
875 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
876 mgmt->u.reassoc_req.listen_interval =
877 cpu_to_le16(local->hw.conf.listen_interval);
881 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
882 *pos++ = WLAN_EID_SSID;
883 *pos++ = ifsta->ssid_len;
884 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
886 /* add all rates which were marked to be used above */
887 supp_rates_len = rates_len;
888 if (supp_rates_len > 8)
891 len = sband->n_bitrates;
892 pos = skb_put(skb, supp_rates_len + 2);
893 *pos++ = WLAN_EID_SUPP_RATES;
894 *pos++ = supp_rates_len;
897 for (i = 0; i < sband->n_bitrates; i++) {
898 if (BIT(i) & rates) {
899 int rate = sband->bitrates[i].bitrate;
900 *pos++ = (u8) (rate / 5);
906 if (rates_len > count) {
907 pos = skb_put(skb, rates_len - count + 2);
908 *pos++ = WLAN_EID_EXT_SUPP_RATES;
909 *pos++ = rates_len - count;
911 for (i++; i < sband->n_bitrates; i++) {
912 if (BIT(i) & rates) {
913 int rate = sband->bitrates[i].bitrate;
914 *pos++ = (u8) (rate / 5);
919 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
920 /* 1. power capabilities */
921 pos = skb_put(skb, 4);
922 *pos++ = WLAN_EID_PWR_CAPABILITY;
924 *pos++ = 0; /* min tx power */
925 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
927 /* 2. supported channels */
928 /* TODO: get this in reg domain format */
929 pos = skb_put(skb, 2 * sband->n_channels + 2);
930 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
931 *pos++ = 2 * sband->n_channels;
932 for (i = 0; i < sband->n_channels; i++) {
933 *pos++ = ieee80211_frequency_to_channel(
934 sband->channels[i].center_freq);
935 *pos++ = 1; /* one channel in the subband*/
939 if (ifsta->extra_ie) {
940 pos = skb_put(skb, ifsta->extra_ie_len);
941 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
944 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
945 pos = skb_put(skb, 9);
946 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
947 *pos++ = 7; /* len */
948 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
951 *pos++ = 2; /* WME */
952 *pos++ = 0; /* WME info */
953 *pos++ = 1; /* WME ver */
957 /* wmm support is a must to HT */
958 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
959 sband->ht_info.ht_supported &&
960 (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
961 struct ieee80211_ht_addt_info *ht_add_info =
962 (struct ieee80211_ht_addt_info *)ht_add_ie;
963 u16 cap = sband->ht_info.cap;
965 u32 flags = local->hw.conf.channel->flags;
967 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
968 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
969 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
970 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
971 cap &= ~IEEE80211_HT_CAP_SGI_40;
974 case IEEE80211_HT_IE_CHA_SEC_BELOW:
975 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
976 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
977 cap &= ~IEEE80211_HT_CAP_SGI_40;
982 tmp = cpu_to_le16(cap);
983 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
984 *pos++ = WLAN_EID_HT_CAPABILITY;
985 *pos++ = sizeof(struct ieee80211_ht_cap);
986 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
987 memcpy(pos, &tmp, sizeof(u16));
989 /* TODO: needs a define here for << 2 */
990 *pos++ = sband->ht_info.ampdu_factor |
991 (sband->ht_info.ampdu_density << 2);
992 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
995 kfree(ifsta->assocreq_ies);
996 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
997 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
998 if (ifsta->assocreq_ies)
999 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
1001 ieee80211_sta_tx(sdata, skb, 0);
1005 static void ieee80211_send_deauth(struct ieee80211_sub_if_data *sdata,
1006 struct ieee80211_if_sta *ifsta, u16 reason)
1008 struct ieee80211_local *local = sdata->local;
1009 struct sk_buff *skb;
1010 struct ieee80211_mgmt *mgmt;
1012 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
1014 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
1015 "frame\n", sdata->dev->name);
1018 skb_reserve(skb, local->hw.extra_tx_headroom);
1020 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1021 memset(mgmt, 0, 24);
1022 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
1023 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1024 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1025 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1026 IEEE80211_STYPE_DEAUTH);
1028 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1030 ieee80211_sta_tx(sdata, skb, 0);
1033 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
1035 if (!sdata || !sdata->default_key ||
1036 sdata->default_key->conf.alg != ALG_WEP)
1041 static void ieee80211_send_disassoc(struct ieee80211_sub_if_data *sdata,
1042 struct ieee80211_if_sta *ifsta, u16 reason)
1044 struct ieee80211_local *local = sdata->local;
1045 struct sk_buff *skb;
1046 struct ieee80211_mgmt *mgmt;
1048 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
1050 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
1051 "frame\n", sdata->dev->name);
1054 skb_reserve(skb, local->hw.extra_tx_headroom);
1056 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1057 memset(mgmt, 0, 24);
1058 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
1059 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1060 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1061 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1062 IEEE80211_STYPE_DISASSOC);
1064 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
1066 ieee80211_sta_tx(sdata, skb, 0);
1069 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1070 struct ieee80211_if_sta *ifsta, bool deauth,
1071 bool self_disconnected, u16 reason)
1073 struct ieee80211_local *local = sdata->local;
1074 struct sta_info *sta;
1075 u32 changed = BSS_CHANGED_ASSOC;
1079 sta = sta_info_get(local, ifsta->bssid);
1086 ifsta->direct_probe_tries = 0;
1087 ifsta->auth_tries = 0;
1089 ifsta->assoc_scan_tries = 0;
1090 ifsta->assoc_tries = 0;
1092 netif_tx_stop_all_queues(sdata->dev);
1093 netif_carrier_off(sdata->dev);
1095 ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
1097 if (self_disconnected) {
1099 ieee80211_send_deauth(sdata, ifsta, reason);
1101 ieee80211_send_disassoc(sdata, ifsta, reason);
1104 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
1105 changed |= ieee80211_reset_erp_info(sdata);
1107 if (sdata->bss_conf.assoc_ht)
1108 changed |= BSS_CHANGED_HT;
1110 sdata->bss_conf.assoc_ht = 0;
1111 sdata->bss_conf.ht_conf = NULL;
1112 sdata->bss_conf.ht_bss_conf = NULL;
1114 ieee80211_led_assoc(local, 0);
1115 sdata->bss_conf.assoc = 0;
1117 ieee80211_sta_send_apinfo(sdata, ifsta);
1119 if (self_disconnected)
1120 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1122 sta_info_unlink(&sta);
1126 sta_info_destroy(sta);
1129 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
1130 struct ieee80211_if_sta *ifsta)
1132 struct ieee80211_local *local = sdata->local;
1133 struct ieee80211_sta_bss *bss;
1136 int privacy_invoked;
1138 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
1141 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1142 local->hw.conf.channel->center_freq,
1143 ifsta->ssid, ifsta->ssid_len);
1147 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
1148 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
1149 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
1151 ieee80211_rx_bss_put(local, bss);
1153 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
1159 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
1160 struct ieee80211_if_sta *ifsta)
1162 DECLARE_MAC_BUF(mac);
1164 ifsta->assoc_tries++;
1165 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
1166 printk(KERN_DEBUG "%s: association with AP %s"
1168 sdata->dev->name, print_mac(mac, ifsta->bssid));
1169 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1173 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1174 printk(KERN_DEBUG "%s: associate with AP %s\n",
1175 sdata->dev->name, print_mac(mac, ifsta->bssid));
1176 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
1177 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1178 "mixed-cell disabled - abort association\n", sdata->dev->name);
1179 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1183 ieee80211_send_assoc(sdata, ifsta);
1185 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1189 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
1190 struct ieee80211_if_sta *ifsta)
1192 struct ieee80211_local *local = sdata->local;
1193 struct sta_info *sta;
1195 DECLARE_MAC_BUF(mac);
1197 /* TODO: start monitoring current AP signal quality and number of
1198 * missed beacons. Scan other channels every now and then and search
1199 * for better APs. */
1200 /* TODO: remove expired BSSes */
1202 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
1206 sta = sta_info_get(local, ifsta->bssid);
1208 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1209 sdata->dev->name, print_mac(mac, ifsta->bssid));
1213 if (time_after(jiffies,
1214 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1215 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
1216 printk(KERN_DEBUG "%s: No ProbeResp from "
1217 "current AP %s - assume out of "
1219 sdata->dev->name, print_mac(mac, ifsta->bssid));
1222 ieee80211_send_probe_req(sdata, ifsta->bssid,
1224 local->scan_ssid_len);
1225 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1227 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1228 if (time_after(jiffies, ifsta->last_probe +
1229 IEEE80211_PROBE_INTERVAL)) {
1230 ifsta->last_probe = jiffies;
1231 ieee80211_send_probe_req(sdata, ifsta->bssid,
1241 ieee80211_set_disassoc(sdata, ifsta, true, true,
1242 WLAN_REASON_PREV_AUTH_NOT_VALID);
1244 mod_timer(&ifsta->timer, jiffies +
1245 IEEE80211_MONITORING_INTERVAL);
1249 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
1250 struct ieee80211_if_sta *ifsta)
1252 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
1253 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1254 ieee80211_associate(sdata, ifsta);
1258 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1259 struct ieee80211_if_sta *ifsta,
1260 struct ieee80211_mgmt *mgmt,
1264 struct ieee802_11_elems elems;
1266 pos = mgmt->u.auth.variable;
1267 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1268 if (!elems.challenge)
1270 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
1271 elems.challenge_len + 2, 1);
1274 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
1275 u8 dialog_token, u16 status, u16 policy,
1276 u16 buf_size, u16 timeout)
1278 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1279 struct ieee80211_local *local = sdata->local;
1280 struct sk_buff *skb;
1281 struct ieee80211_mgmt *mgmt;
1284 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1287 printk(KERN_DEBUG "%s: failed to allocate buffer "
1288 "for addba resp frame\n", sdata->dev->name);
1292 skb_reserve(skb, local->hw.extra_tx_headroom);
1293 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1294 memset(mgmt, 0, 24);
1295 memcpy(mgmt->da, da, ETH_ALEN);
1296 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1297 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1298 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1300 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1301 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1302 IEEE80211_STYPE_ACTION);
1304 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1305 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1306 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1307 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1309 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1310 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1311 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1313 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1314 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1315 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1317 ieee80211_sta_tx(sdata, skb, 0);
1322 void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata, const u8 *da,
1323 u16 tid, u8 dialog_token, u16 start_seq_num,
1324 u16 agg_size, u16 timeout)
1326 struct ieee80211_local *local = sdata->local;
1327 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1328 struct sk_buff *skb;
1329 struct ieee80211_mgmt *mgmt;
1332 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1335 printk(KERN_ERR "%s: failed to allocate buffer "
1336 "for addba request frame\n", sdata->dev->name);
1339 skb_reserve(skb, local->hw.extra_tx_headroom);
1340 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1341 memset(mgmt, 0, 24);
1342 memcpy(mgmt->da, da, ETH_ALEN);
1343 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1344 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1345 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1347 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1349 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1350 IEEE80211_STYPE_ACTION);
1352 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1354 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1355 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1357 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1358 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1359 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1360 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1362 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1364 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1365 mgmt->u.action.u.addba_req.start_seq_num =
1366 cpu_to_le16(start_seq_num << 4);
1368 ieee80211_sta_tx(sdata, skb, 0);
1372 * After accepting the AddBA Request we activated a timer,
1373 * resetting it after each frame that arrives from the originator.
1374 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1376 static void sta_rx_agg_session_timer_expired(unsigned long data)
1378 /* not an elegant detour, but there is no choice as the timer passes
1379 * only one argument, and various sta_info are needed here, so init
1380 * flow in sta_info_create gives the TID as data, while the timer_to_id
1381 * array gives the sta through container_of */
1382 u8 *ptid = (u8 *)data;
1383 u8 *timer_to_id = ptid - *ptid;
1384 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1387 #ifdef CONFIG_MAC80211_HT_DEBUG
1388 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1390 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
1391 (u16)*ptid, WLAN_BACK_TIMER,
1392 WLAN_REASON_QSTA_TIMEOUT);
1395 static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
1396 struct ieee80211_mgmt *mgmt,
1399 struct ieee80211_hw *hw = &local->hw;
1400 struct ieee80211_conf *conf = &hw->conf;
1401 struct sta_info *sta;
1402 struct tid_ampdu_rx *tid_agg_rx;
1403 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1405 int ret = -EOPNOTSUPP;
1406 DECLARE_MAC_BUF(mac);
1410 sta = sta_info_get(local, mgmt->sa);
1416 /* extract session parameters from addba request frame */
1417 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1418 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1420 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1422 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1423 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1424 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1425 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1427 status = WLAN_STATUS_REQUEST_DECLINED;
1429 /* sanity check for incoming parameters:
1430 * check if configuration can support the BA policy
1431 * and if buffer size does not exceeds max value */
1432 if (((ba_policy != 1)
1433 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1434 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1435 status = WLAN_STATUS_INVALID_QOS_PARAM;
1436 #ifdef CONFIG_MAC80211_HT_DEBUG
1437 if (net_ratelimit())
1438 printk(KERN_DEBUG "AddBA Req with bad params from "
1439 "%s on tid %u. policy %d, buffer size %d\n",
1440 print_mac(mac, mgmt->sa), tid, ba_policy,
1442 #endif /* CONFIG_MAC80211_HT_DEBUG */
1445 /* determine default buffer size */
1446 if (buf_size == 0) {
1447 struct ieee80211_supported_band *sband;
1449 sband = local->hw.wiphy->bands[conf->channel->band];
1450 buf_size = IEEE80211_MIN_AMPDU_BUF;
1451 buf_size = buf_size << sband->ht_info.ampdu_factor;
1455 /* examine state machine */
1456 spin_lock_bh(&sta->lock);
1458 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1459 #ifdef CONFIG_MAC80211_HT_DEBUG
1460 if (net_ratelimit())
1461 printk(KERN_DEBUG "unexpected AddBA Req from "
1463 print_mac(mac, mgmt->sa), tid);
1464 #endif /* CONFIG_MAC80211_HT_DEBUG */
1468 /* prepare A-MPDU MLME for Rx aggregation */
1469 sta->ampdu_mlme.tid_rx[tid] =
1470 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1471 if (!sta->ampdu_mlme.tid_rx[tid]) {
1472 #ifdef CONFIG_MAC80211_HT_DEBUG
1473 if (net_ratelimit())
1474 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1480 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1481 sta_rx_agg_session_timer_expired;
1482 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1483 (unsigned long)&sta->timer_to_tid[tid];
1484 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1486 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1488 /* prepare reordering buffer */
1489 tid_agg_rx->reorder_buf =
1490 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1491 if (!tid_agg_rx->reorder_buf) {
1492 #ifdef CONFIG_MAC80211_HT_DEBUG
1493 if (net_ratelimit())
1494 printk(KERN_ERR "can not allocate reordering buffer "
1495 "to tid %d\n", tid);
1497 kfree(sta->ampdu_mlme.tid_rx[tid]);
1500 memset(tid_agg_rx->reorder_buf, 0,
1501 buf_size * sizeof(struct sk_buff *));
1503 if (local->ops->ampdu_action)
1504 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1505 sta->addr, tid, &start_seq_num);
1506 #ifdef CONFIG_MAC80211_HT_DEBUG
1507 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1508 #endif /* CONFIG_MAC80211_HT_DEBUG */
1511 kfree(tid_agg_rx->reorder_buf);
1513 sta->ampdu_mlme.tid_rx[tid] = NULL;
1517 /* change state and send addba resp */
1518 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1519 tid_agg_rx->dialog_token = dialog_token;
1520 tid_agg_rx->ssn = start_seq_num;
1521 tid_agg_rx->head_seq_num = start_seq_num;
1522 tid_agg_rx->buf_size = buf_size;
1523 tid_agg_rx->timeout = timeout;
1524 tid_agg_rx->stored_mpdu_num = 0;
1525 status = WLAN_STATUS_SUCCESS;
1527 spin_unlock_bh(&sta->lock);
1530 ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
1531 dialog_token, status, 1, buf_size, timeout);
1535 static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
1536 struct ieee80211_mgmt *mgmt,
1539 struct ieee80211_hw *hw = &local->hw;
1540 struct sta_info *sta;
1547 sta = sta_info_get(local, mgmt->sa);
1553 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1554 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1556 state = &sta->ampdu_mlme.tid_state_tx[tid];
1558 spin_lock_bh(&sta->lock);
1560 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1561 spin_unlock_bh(&sta->lock);
1562 goto addba_resp_exit;
1565 if (mgmt->u.action.u.addba_resp.dialog_token !=
1566 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1567 spin_unlock_bh(&sta->lock);
1568 #ifdef CONFIG_MAC80211_HT_DEBUG
1569 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1570 #endif /* CONFIG_MAC80211_HT_DEBUG */
1571 goto addba_resp_exit;
1574 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1575 #ifdef CONFIG_MAC80211_HT_DEBUG
1576 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1577 #endif /* CONFIG_MAC80211_HT_DEBUG */
1578 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1579 == WLAN_STATUS_SUCCESS) {
1580 *state |= HT_ADDBA_RECEIVED_MSK;
1581 sta->ampdu_mlme.addba_req_num[tid] = 0;
1583 if (*state == HT_AGG_STATE_OPERATIONAL)
1584 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1586 spin_unlock_bh(&sta->lock);
1588 sta->ampdu_mlme.addba_req_num[tid]++;
1589 /* this will allow the state check in stop_BA_session */
1590 *state = HT_AGG_STATE_OPERATIONAL;
1591 spin_unlock_bh(&sta->lock);
1592 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1593 WLAN_BACK_INITIATOR);
1600 void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata, const u8 *da, u16 tid,
1601 u16 initiator, u16 reason_code)
1603 struct ieee80211_local *local = sdata->local;
1604 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1605 struct sk_buff *skb;
1606 struct ieee80211_mgmt *mgmt;
1609 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1612 printk(KERN_ERR "%s: failed to allocate buffer "
1613 "for delba frame\n", sdata->dev->name);
1617 skb_reserve(skb, local->hw.extra_tx_headroom);
1618 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1619 memset(mgmt, 0, 24);
1620 memcpy(mgmt->da, da, ETH_ALEN);
1621 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1622 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1623 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1625 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1626 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1627 IEEE80211_STYPE_ACTION);
1629 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1631 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1632 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1633 params = (u16)(initiator << 11); /* bit 11 initiator */
1634 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1636 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1637 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1639 ieee80211_sta_tx(sdata, skb, 0);
1642 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
1644 struct ieee80211_local *local = sdata->local;
1645 struct sk_buff *skb;
1646 struct ieee80211_bar *bar;
1647 u16 bar_control = 0;
1649 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1651 printk(KERN_ERR "%s: failed to allocate buffer for "
1652 "bar frame\n", sdata->dev->name);
1655 skb_reserve(skb, local->hw.extra_tx_headroom);
1656 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1657 memset(bar, 0, sizeof(*bar));
1658 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1659 IEEE80211_STYPE_BACK_REQ);
1660 memcpy(bar->ra, ra, ETH_ALEN);
1661 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
1662 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1663 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1664 bar_control |= (u16)(tid << 12);
1665 bar->control = cpu_to_le16(bar_control);
1666 bar->start_seq_num = cpu_to_le16(ssn);
1668 ieee80211_sta_tx(sdata, skb, 0);
1671 void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
1672 u16 initiator, u16 reason)
1674 struct ieee80211_local *local = sdata->local;
1675 struct ieee80211_hw *hw = &local->hw;
1676 struct sta_info *sta;
1678 DECLARE_MAC_BUF(mac);
1682 sta = sta_info_get(local, ra);
1688 /* check if TID is in operational state */
1689 spin_lock_bh(&sta->lock);
1690 if (sta->ampdu_mlme.tid_state_rx[tid]
1691 != HT_AGG_STATE_OPERATIONAL) {
1692 spin_unlock_bh(&sta->lock);
1696 sta->ampdu_mlme.tid_state_rx[tid] =
1697 HT_AGG_STATE_REQ_STOP_BA_MSK |
1698 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1699 spin_unlock_bh(&sta->lock);
1701 /* stop HW Rx aggregation. ampdu_action existence
1702 * already verified in session init so we add the BUG_ON */
1703 BUG_ON(!local->ops->ampdu_action);
1705 #ifdef CONFIG_MAC80211_HT_DEBUG
1706 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1707 print_mac(mac, ra), tid);
1708 #endif /* CONFIG_MAC80211_HT_DEBUG */
1710 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1713 printk(KERN_DEBUG "HW problem - can not stop rx "
1714 "aggregation for tid %d\n", tid);
1716 /* shutdown timer has not expired */
1717 if (initiator != WLAN_BACK_TIMER)
1718 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1720 /* check if this is a self generated aggregation halt */
1721 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1722 ieee80211_send_delba(sdata, ra, tid, 0, reason);
1724 /* free the reordering buffer */
1725 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1726 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1727 /* release the reordered frames */
1728 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1729 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1730 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1733 /* free resources */
1734 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1735 kfree(sta->ampdu_mlme.tid_rx[tid]);
1736 sta->ampdu_mlme.tid_rx[tid] = NULL;
1737 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1743 static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
1744 struct ieee80211_mgmt *mgmt, size_t len)
1746 struct ieee80211_local *local = sdata->local;
1747 struct sta_info *sta;
1750 DECLARE_MAC_BUF(mac);
1754 sta = sta_info_get(local, mgmt->sa);
1760 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1761 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1762 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1764 #ifdef CONFIG_MAC80211_HT_DEBUG
1765 if (net_ratelimit())
1766 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1767 print_mac(mac, mgmt->sa),
1768 initiator ? "initiator" : "recipient", tid,
1769 mgmt->u.action.u.delba.reason_code);
1770 #endif /* CONFIG_MAC80211_HT_DEBUG */
1772 if (initiator == WLAN_BACK_INITIATOR)
1773 ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
1774 WLAN_BACK_INITIATOR, 0);
1775 else { /* WLAN_BACK_RECIPIENT */
1776 spin_lock_bh(&sta->lock);
1777 sta->ampdu_mlme.tid_state_tx[tid] =
1778 HT_AGG_STATE_OPERATIONAL;
1779 spin_unlock_bh(&sta->lock);
1780 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1781 WLAN_BACK_RECIPIENT);
1787 * After sending add Block Ack request we activated a timer until
1788 * add Block Ack response will arrive from the recipient.
1789 * If this timer expires sta_addba_resp_timer_expired will be executed.
1791 void sta_addba_resp_timer_expired(unsigned long data)
1793 /* not an elegant detour, but there is no choice as the timer passes
1794 * only one argument, and both sta_info and TID are needed, so init
1795 * flow in sta_info_create gives the TID as data, while the timer_to_id
1796 * array gives the sta through container_of */
1797 u16 tid = *(u8 *)data;
1798 struct sta_info *temp_sta = container_of((void *)data,
1799 struct sta_info, timer_to_tid[tid]);
1801 struct ieee80211_local *local = temp_sta->local;
1802 struct ieee80211_hw *hw = &local->hw;
1803 struct sta_info *sta;
1808 sta = sta_info_get(local, temp_sta->addr);
1814 state = &sta->ampdu_mlme.tid_state_tx[tid];
1815 /* check if the TID waits for addBA response */
1816 spin_lock_bh(&sta->lock);
1817 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1818 spin_unlock_bh(&sta->lock);
1819 *state = HT_AGG_STATE_IDLE;
1820 #ifdef CONFIG_MAC80211_HT_DEBUG
1821 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1822 "expecting addBA response there", tid);
1824 goto timer_expired_exit;
1827 #ifdef CONFIG_MAC80211_HT_DEBUG
1828 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1831 /* go through the state check in stop_BA_session */
1832 *state = HT_AGG_STATE_OPERATIONAL;
1833 spin_unlock_bh(&sta->lock);
1834 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1835 WLAN_BACK_INITIATOR);
1841 void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
1843 struct ieee80211_local *local = sdata->local;
1846 for (i = 0; i < STA_TID_NUM; i++) {
1847 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1848 WLAN_BACK_INITIATOR);
1849 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
1850 WLAN_BACK_RECIPIENT,
1851 WLAN_REASON_QSTA_LEAVE_QBSS);
1855 static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
1856 struct ieee80211_msrment_ie *request_ie,
1857 const u8 *da, const u8 *bssid,
1860 struct ieee80211_local *local = sdata->local;
1861 struct sk_buff *skb;
1862 struct ieee80211_mgmt *msr_report;
1864 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1865 sizeof(struct ieee80211_msrment_ie));
1868 printk(KERN_ERR "%s: failed to allocate buffer for "
1869 "measurement report frame\n", sdata->dev->name);
1873 skb_reserve(skb, local->hw.extra_tx_headroom);
1874 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1875 memset(msr_report, 0, 24);
1876 memcpy(msr_report->da, da, ETH_ALEN);
1877 memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
1878 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1879 msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1880 IEEE80211_STYPE_ACTION);
1882 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1883 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1884 msr_report->u.action.u.measurement.action_code =
1885 WLAN_ACTION_SPCT_MSR_RPRT;
1886 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1888 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1889 msr_report->u.action.u.measurement.length =
1890 sizeof(struct ieee80211_msrment_ie);
1892 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1893 sizeof(struct ieee80211_msrment_ie));
1894 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1895 msr_report->u.action.u.measurement.msr_elem.mode |=
1896 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1897 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1899 ieee80211_sta_tx(sdata, skb, 0);
1902 static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
1903 struct ieee80211_mgmt *mgmt,
1907 * Ignoring measurement request is spec violation.
1908 * Mandatory measurements must be reported optional
1909 * measurements might be refused or reported incapable
1910 * For now just refuse
1911 * TODO: Answer basic measurement as unmeasured
1913 ieee80211_send_refuse_measurement_request(sdata,
1914 &mgmt->u.action.u.measurement.msr_elem,
1915 mgmt->sa, mgmt->bssid,
1916 mgmt->u.action.u.measurement.dialog_token);
1920 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1921 struct ieee80211_if_sta *ifsta,
1922 struct ieee80211_mgmt *mgmt,
1925 u16 auth_alg, auth_transaction, status_code;
1926 DECLARE_MAC_BUF(mac);
1928 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
1929 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
1935 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1936 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1939 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1940 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1943 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1944 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1945 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1947 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1949 * IEEE 802.11 standard does not require authentication in IBSS
1950 * networks and most implementations do not seem to use it.
1951 * However, try to reply to authentication attempts if someone
1952 * has actually implemented this.
1954 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1956 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
1959 if (auth_alg != ifsta->auth_alg ||
1960 auth_transaction != ifsta->auth_transaction)
1963 if (status_code != WLAN_STATUS_SUCCESS) {
1964 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1966 const int num_algs = ARRAY_SIZE(algs);
1968 algs[0] = algs[1] = algs[2] = 0xff;
1969 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1970 algs[0] = WLAN_AUTH_OPEN;
1971 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1972 algs[1] = WLAN_AUTH_SHARED_KEY;
1973 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1974 algs[2] = WLAN_AUTH_LEAP;
1975 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1977 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1981 for (i = 0; i < num_algs; i++) {
1983 if (pos >= num_algs)
1985 if (algs[pos] == ifsta->auth_alg ||
1988 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1989 !ieee80211_sta_wep_configured(sdata))
1991 ifsta->auth_alg = algs[pos];
1998 switch (ifsta->auth_alg) {
1999 case WLAN_AUTH_OPEN:
2000 case WLAN_AUTH_LEAP:
2001 ieee80211_auth_completed(sdata, ifsta);
2003 case WLAN_AUTH_SHARED_KEY:
2004 if (ifsta->auth_transaction == 4)
2005 ieee80211_auth_completed(sdata, ifsta);
2007 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
2013 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2014 struct ieee80211_if_sta *ifsta,
2015 struct ieee80211_mgmt *mgmt,
2019 DECLARE_MAC_BUF(mac);
2024 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
2027 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2029 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
2030 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
2032 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
2033 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
2034 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
2035 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2036 mod_timer(&ifsta->timer, jiffies +
2037 IEEE80211_RETRY_AUTH_INTERVAL);
2040 ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
2041 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
2045 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2046 struct ieee80211_if_sta *ifsta,
2047 struct ieee80211_mgmt *mgmt,
2051 DECLARE_MAC_BUF(mac);
2056 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
2059 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2061 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
2062 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
2064 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
2065 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
2066 mod_timer(&ifsta->timer, jiffies +
2067 IEEE80211_RETRY_AUTH_INTERVAL);
2070 ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
2074 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2075 struct ieee80211_if_sta *ifsta,
2076 struct ieee80211_mgmt *mgmt,
2080 struct ieee80211_local *local = sdata->local;
2081 struct ieee80211_supported_band *sband;
2082 struct sta_info *sta;
2083 u64 rates, basic_rates;
2084 u16 capab_info, status_code, aid;
2085 struct ieee802_11_elems elems;
2086 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
2089 DECLARE_MAC_BUF(mac);
2090 bool have_higher_than_11mbit = false;
2092 /* AssocResp and ReassocResp have identical structure, so process both
2093 * of them in this function. */
2095 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
2101 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
2104 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2105 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2106 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2108 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
2109 "status=%d aid=%d)\n",
2110 sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
2111 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2113 if (status_code != WLAN_STATUS_SUCCESS) {
2114 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2115 sdata->dev->name, status_code);
2116 /* if this was a reassociation, ensure we try a "full"
2117 * association next time. This works around some broken APs
2118 * which do not correctly reject reassociation requests. */
2119 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2123 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2124 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2125 "set\n", sdata->dev->name, aid);
2126 aid &= ~(BIT(15) | BIT(14));
2128 pos = mgmt->u.assoc_resp.variable;
2129 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2131 if (!elems.supp_rates) {
2132 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2137 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
2139 ifsta->ap_capab = capab_info;
2141 kfree(ifsta->assocresp_ies);
2142 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
2143 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
2144 if (ifsta->assocresp_ies)
2145 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2149 /* Add STA entry for the AP */
2150 sta = sta_info_get(local, ifsta->bssid);
2152 struct ieee80211_sta_bss *bss;
2155 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2157 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2158 " the AP\n", sdata->dev->name);
2162 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
2163 local->hw.conf.channel->center_freq,
2164 ifsta->ssid, ifsta->ssid_len);
2166 sta->last_signal = bss->signal;
2167 sta->last_qual = bss->qual;
2168 sta->last_noise = bss->noise;
2169 ieee80211_rx_bss_put(local, bss);
2172 err = sta_info_insert(sta);
2174 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2175 " the AP (error %d)\n", sdata->dev->name, err);
2179 /* update new sta with its last rx activity */
2180 sta->last_rx = jiffies;
2184 * FIXME: Do we really need to update the sta_info's information here?
2185 * We already know about the AP (we found it in our list) so it
2186 * should already be filled with the right info, no?
2187 * As is stands, all this is racy because typically we assume
2188 * the information that is filled in here (except flags) doesn't
2189 * change while a STA structure is alive. As such, it should move
2190 * to between the sta_info_alloc() and sta_info_insert() above.
2193 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2194 WLAN_STA_AUTHORIZED);
2198 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2200 for (i = 0; i < elems.supp_rates_len; i++) {
2201 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2204 have_higher_than_11mbit = true;
2206 for (j = 0; j < sband->n_bitrates; j++) {
2207 if (sband->bitrates[j].bitrate == rate)
2209 if (elems.supp_rates[i] & 0x80)
2210 basic_rates |= BIT(j);
2214 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2215 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2218 have_higher_than_11mbit = true;
2220 for (j = 0; j < sband->n_bitrates; j++) {
2221 if (sband->bitrates[j].bitrate == rate)
2223 if (elems.ext_supp_rates[i] & 0x80)
2224 basic_rates |= BIT(j);
2228 sta->supp_rates[local->hw.conf.channel->band] = rates;
2229 sdata->basic_rates = basic_rates;
2231 /* cf. IEEE 802.11 9.2.12 */
2232 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2233 have_higher_than_11mbit)
2234 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2236 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2238 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2239 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2240 struct ieee80211_ht_bss_info bss_info;
2241 ieee80211_ht_cap_ie_to_ht_info(
2242 (struct ieee80211_ht_cap *)
2243 elems.ht_cap_elem, &sta->ht_info);
2244 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2245 (struct ieee80211_ht_addt_info *)
2246 elems.ht_info_elem, &bss_info);
2247 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2250 rate_control_rate_init(sta, local);
2252 if (elems.wmm_param) {
2253 set_sta_flags(sta, WLAN_STA_WME);
2255 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2256 elems.wmm_param_len);
2260 /* set AID and assoc capability,
2261 * ieee80211_set_associated() will tell the driver */
2262 bss_conf->aid = aid;
2263 bss_conf->assoc_capability = capab_info;
2264 ieee80211_set_associated(sdata, ifsta);
2266 ieee80211_associated(sdata, ifsta);
2270 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
2271 struct ieee80211_if_sta *ifsta,
2272 struct ieee80211_sta_bss *bss)
2274 struct ieee80211_local *local = sdata->local;
2275 int res, rates, i, j;
2276 struct sk_buff *skb;
2277 struct ieee80211_mgmt *mgmt;
2279 struct ieee80211_supported_band *sband;
2280 union iwreq_data wrqu;
2282 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2284 /* Remove possible STA entries from other IBSS networks. */
2285 sta_info_flush_delayed(sdata);
2287 if (local->ops->reset_tsf) {
2288 /* Reset own TSF to allow time synchronization work. */
2289 local->ops->reset_tsf(local_to_hw(local));
2291 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2292 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2296 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2298 sdata->drop_unencrypted = bss->capability &
2299 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2301 res = ieee80211_set_freq(sdata, bss->freq);
2306 /* Build IBSS probe response */
2307 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2309 skb_reserve(skb, local->hw.extra_tx_headroom);
2311 mgmt = (struct ieee80211_mgmt *)
2312 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2313 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2314 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2315 IEEE80211_STYPE_PROBE_RESP);
2316 memset(mgmt->da, 0xff, ETH_ALEN);
2317 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
2318 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2319 mgmt->u.beacon.beacon_int =
2320 cpu_to_le16(local->hw.conf.beacon_int);
2321 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
2322 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2324 pos = skb_put(skb, 2 + ifsta->ssid_len);
2325 *pos++ = WLAN_EID_SSID;
2326 *pos++ = ifsta->ssid_len;
2327 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2329 rates = bss->supp_rates_len;
2332 pos = skb_put(skb, 2 + rates);
2333 *pos++ = WLAN_EID_SUPP_RATES;
2335 memcpy(pos, bss->supp_rates, rates);
2337 if (bss->band == IEEE80211_BAND_2GHZ) {
2338 pos = skb_put(skb, 2 + 1);
2339 *pos++ = WLAN_EID_DS_PARAMS;
2341 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2344 pos = skb_put(skb, 2 + 2);
2345 *pos++ = WLAN_EID_IBSS_PARAMS;
2347 /* FIX: set ATIM window based on scan results */
2351 if (bss->supp_rates_len > 8) {
2352 rates = bss->supp_rates_len - 8;
2353 pos = skb_put(skb, 2 + rates);
2354 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2356 memcpy(pos, &bss->supp_rates[8], rates);
2359 ifsta->probe_resp = skb;
2361 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2365 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2366 for (i = 0; i < bss->supp_rates_len; i++) {
2367 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2368 for (j = 0; j < sband->n_bitrates; j++)
2369 if (sband->bitrates[j].bitrate == bitrate)
2372 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2374 ieee80211_sta_def_wmm_params(sdata, bss, 1);
2376 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
2377 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2379 memset(&wrqu, 0, sizeof(wrqu));
2380 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2381 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
2386 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2387 struct ieee802_11_elems *elems,
2388 enum ieee80211_band band)
2390 struct ieee80211_supported_band *sband;
2391 struct ieee80211_rate *bitrates;
2395 sband = local->hw.wiphy->bands[band];
2399 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2402 bitrates = sband->bitrates;
2403 num_rates = sband->n_bitrates;
2405 for (i = 0; i < elems->supp_rates_len +
2406 elems->ext_supp_rates_len; i++) {
2409 if (i < elems->supp_rates_len)
2410 rate = elems->supp_rates[i];
2411 else if (elems->ext_supp_rates)
2412 rate = elems->ext_supp_rates
2413 [i - elems->supp_rates_len];
2414 own_rate = 5 * (rate & 0x7f);
2415 for (j = 0; j < num_rates; j++)
2416 if (bitrates[j].bitrate == own_rate)
2417 supp_rates |= BIT(j);
2422 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
2423 enum ieee80211_band band)
2425 struct ieee80211_supported_band *sband;
2426 struct ieee80211_rate *bitrates;
2427 u64 mandatory_rates;
2428 enum ieee80211_rate_flags mandatory_flag;
2431 sband = local->hw.wiphy->bands[band];
2434 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2437 if (band == IEEE80211_BAND_2GHZ)
2438 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
2440 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
2442 bitrates = sband->bitrates;
2443 mandatory_rates = 0;
2444 for (i = 0; i < sband->n_bitrates; i++)
2445 if (bitrates[i].flags & mandatory_flag)
2446 mandatory_rates |= BIT(i);
2447 return mandatory_rates;
2450 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2451 struct ieee80211_mgmt *mgmt,
2453 struct ieee80211_rx_status *rx_status,
2454 struct ieee802_11_elems *elems)
2456 struct ieee80211_local *local = sdata->local;
2458 struct ieee80211_sta_bss *bss;
2459 struct sta_info *sta;
2460 struct ieee80211_channel *channel;
2461 u64 beacon_timestamp, rx_timestamp;
2463 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
2464 enum ieee80211_band band = rx_status->band;
2465 DECLARE_MAC_BUF(mac);
2466 DECLARE_MAC_BUF(mac2);
2468 if (elems->ds_params && elems->ds_params_len == 1)
2469 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
2471 freq = rx_status->freq;
2473 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2475 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2478 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2479 elems->mesh_config && mesh_matches_local(elems, sdata)) {
2480 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2482 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
2483 mesh_peer_accepts_plinks(elems));
2486 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
2487 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2488 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2492 sta = sta_info_get(local, mgmt->sa);
2496 prev_rates = sta->supp_rates[band];
2497 /* make sure mandatory rates are always added */
2498 sta->supp_rates[band] = supp_rates |
2499 ieee80211_sta_get_mandatory_rates(local, band);
2501 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2502 if (sta->supp_rates[band] != prev_rates)
2503 printk(KERN_DEBUG "%s: updated supp_rates set "
2504 "for %s based on beacon info (0x%llx | "
2505 "0x%llx -> 0x%llx)\n",
2506 sdata->dev->name, print_mac(mac, sta->addr),
2507 (unsigned long long) prev_rates,
2508 (unsigned long long) supp_rates,
2509 (unsigned long long) sta->supp_rates[band]);
2512 ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
2513 mgmt->sa, supp_rates);
2519 #ifdef CONFIG_MAC80211_MESH
2520 if (elems->mesh_config)
2521 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
2522 elems->mesh_id_len, elems->mesh_config, freq);
2525 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
2526 elems->ssid, elems->ssid_len);
2528 #ifdef CONFIG_MAC80211_MESH
2529 if (elems->mesh_config)
2530 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
2531 elems->mesh_id_len, elems->mesh_config,
2532 elems->mesh_config_len, freq);
2535 bss = ieee80211_rx_bss_add(sdata, mgmt->bssid, freq,
2536 elems->ssid, elems->ssid_len);
2541 /* TODO: order by RSSI? */
2542 spin_lock_bh(&local->sta_bss_lock);
2543 list_move_tail(&bss->list, &local->sta_bss_list);
2544 spin_unlock_bh(&local->sta_bss_lock);
2548 /* save the ERP value so that it is available at association time */
2549 if (elems->erp_info && elems->erp_info_len >= 1) {
2550 bss->erp_value = elems->erp_info[0];
2551 bss->has_erp_value = 1;
2554 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2555 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2558 struct ieee80211_tim_ie *tim_ie =
2559 (struct ieee80211_tim_ie *)elems->tim;
2560 bss->dtim_period = tim_ie->dtim_period;
2563 /* set default value for buggy APs */
2564 if (!elems->tim || bss->dtim_period == 0)
2565 bss->dtim_period = 1;
2567 bss->supp_rates_len = 0;
2568 if (elems->supp_rates) {
2569 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2570 if (clen > elems->supp_rates_len)
2571 clen = elems->supp_rates_len;
2572 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
2574 bss->supp_rates_len += clen;
2576 if (elems->ext_supp_rates) {
2577 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2578 if (clen > elems->ext_supp_rates_len)
2579 clen = elems->ext_supp_rates_len;
2580 memcpy(&bss->supp_rates[bss->supp_rates_len],
2581 elems->ext_supp_rates, clen);
2582 bss->supp_rates_len += clen;
2587 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2589 bss->timestamp = beacon_timestamp;
2590 bss->last_update = jiffies;
2591 bss->signal = rx_status->signal;
2592 bss->noise = rx_status->noise;
2593 bss->qual = rx_status->qual;
2595 bss->last_probe_resp = jiffies;
2597 * In STA mode, the remaining parameters should not be overridden
2598 * by beacons because they're not necessarily accurate there.
2600 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2601 bss->last_probe_resp && beacon) {
2602 ieee80211_rx_bss_put(local, bss);
2606 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
2608 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
2611 memcpy(bss->ies, elems->ie_start, elems->total_len);
2612 bss->ies_len = elems->total_len;
2616 bss->wmm_used = elems->wmm_param || elems->wmm_info;
2618 /* check if we need to merge IBSS */
2619 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2620 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2621 bss->capability & WLAN_CAPABILITY_IBSS &&
2622 bss->freq == local->oper_channel->center_freq &&
2623 elems->ssid_len == sdata->u.sta.ssid_len &&
2624 memcmp(elems->ssid, sdata->u.sta.ssid,
2625 sdata->u.sta.ssid_len) == 0) {
2626 if (rx_status->flag & RX_FLAG_TSFT) {
2627 /* in order for correct IBSS merging we need mactime
2629 * since mactime is defined as the time the first data
2630 * symbol of the frame hits the PHY, and the timestamp
2631 * of the beacon is defined as "the time that the data
2632 * symbol containing the first bit of the timestamp is
2633 * transmitted to the PHY plus the transmitting STA’s
2634 * delays through its local PHY from the MAC-PHY
2635 * interface to its interface with the WM"
2636 * (802.11 11.1.2) - equals the time this bit arrives at
2637 * the receiver - we have to take into account the
2638 * offset between the two.
2639 * e.g: at 1 MBit that means mactime is 192 usec earlier
2640 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2642 int rate = local->hw.wiphy->bands[band]->
2643 bitrates[rx_status->rate_idx].bitrate;
2644 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2645 } else if (local && local->ops && local->ops->get_tsf)
2646 /* second best option: get current TSF */
2647 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2649 /* can't merge without knowing the TSF */
2650 rx_timestamp = -1LLU;
2651 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2652 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2653 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2654 print_mac(mac, mgmt->sa),
2655 print_mac(mac2, mgmt->bssid),
2656 (unsigned long long)rx_timestamp,
2657 (unsigned long long)beacon_timestamp,
2658 (unsigned long long)(rx_timestamp - beacon_timestamp),
2660 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2661 if (beacon_timestamp > rx_timestamp) {
2662 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2663 printk(KERN_DEBUG "%s: beacon TSF higher than "
2664 "local TSF - IBSS merge with BSSID %s\n",
2665 sdata->dev->name, print_mac(mac, mgmt->bssid));
2667 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
2668 ieee80211_ibss_add_sta(sdata, NULL,
2669 mgmt->bssid, mgmt->sa,
2674 ieee80211_rx_bss_put(local, bss);
2678 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2679 struct ieee80211_mgmt *mgmt,
2681 struct ieee80211_rx_status *rx_status)
2684 struct ieee802_11_elems elems;
2685 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2687 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
2688 return; /* ignore ProbeResp to foreign address */
2690 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2694 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2697 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2699 /* direct probe may be part of the association flow */
2700 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
2702 printk(KERN_DEBUG "%s direct probe responded\n",
2704 ieee80211_authenticate(sdata, ifsta);
2709 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2710 struct ieee80211_mgmt *mgmt,
2712 struct ieee80211_rx_status *rx_status)
2714 struct ieee80211_if_sta *ifsta;
2716 struct ieee802_11_elems elems;
2717 struct ieee80211_local *local = sdata->local;
2718 struct ieee80211_conf *conf = &local->hw.conf;
2721 /* Process beacon from the current BSS */
2722 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2726 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2728 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2730 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2732 ifsta = &sdata->u.sta;
2734 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2735 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2738 /* Do not send changes to driver if we are scanning. This removes
2739 * requirement that a driver's bss_info_changed/conf_tx functions
2740 * need to be atomic.
2741 * This is really ugly code, we should rewrite scanning and make
2742 * all this more understandable for humans.
2744 if (local->sta_sw_scanning || local->sta_hw_scanning)
2747 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2748 elems.wmm_param_len);
2750 if (elems.erp_info && elems.erp_info_len >= 1)
2751 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2753 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2754 changed |= ieee80211_handle_protect_preamb(sdata, false,
2755 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2758 if (elems.ht_cap_elem && elems.ht_info_elem &&
2759 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2760 struct ieee80211_ht_bss_info bss_info;
2762 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2763 (struct ieee80211_ht_addt_info *)
2764 elems.ht_info_elem, &bss_info);
2765 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2769 ieee80211_bss_info_change_notify(sdata, changed);
2773 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
2774 struct ieee80211_if_sta *ifsta,
2775 struct ieee80211_mgmt *mgmt,
2777 struct ieee80211_rx_status *rx_status)
2779 struct ieee80211_local *local = sdata->local;
2781 struct sk_buff *skb;
2782 struct ieee80211_mgmt *resp;
2784 DECLARE_MAC_BUF(mac);
2785 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2786 DECLARE_MAC_BUF(mac2);
2787 DECLARE_MAC_BUF(mac3);
2790 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2791 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
2792 len < 24 + 2 || !ifsta->probe_resp)
2795 if (local->ops->tx_last_beacon)
2796 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2800 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2801 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2802 "%s (tx_last_beacon=%d)\n",
2803 sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2804 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2805 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2807 if (!tx_last_beacon)
2810 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2811 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2814 end = ((u8 *) mgmt) + len;
2815 pos = mgmt->u.probe_req.variable;
2816 if (pos[0] != WLAN_EID_SSID ||
2817 pos + 2 + pos[1] > end) {
2818 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2819 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2821 sdata->dev->name, print_mac(mac, mgmt->sa));
2826 (pos[1] != ifsta->ssid_len ||
2827 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2828 /* Ignore ProbeReq for foreign SSID */
2832 /* Reply with ProbeResp */
2833 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2837 resp = (struct ieee80211_mgmt *) skb->data;
2838 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2839 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2840 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2841 sdata->dev->name, print_mac(mac, resp->da));
2842 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2843 ieee80211_sta_tx(sdata, skb, 0);
2846 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
2847 struct ieee80211_if_sta *ifsta,
2848 struct ieee80211_mgmt *mgmt,
2850 struct ieee80211_rx_status *rx_status)
2852 struct ieee80211_local *local = sdata->local;
2854 /* all categories we currently handle have action_code */
2855 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2858 switch (mgmt->u.action.category) {
2859 case WLAN_CATEGORY_SPECTRUM_MGMT:
2860 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2862 switch (mgmt->u.action.u.measurement.action_code) {
2863 case WLAN_ACTION_SPCT_MSR_REQ:
2864 if (len < (IEEE80211_MIN_ACTION_SIZE +
2865 sizeof(mgmt->u.action.u.measurement)))
2867 ieee80211_sta_process_measurement_req(sdata, mgmt, len);
2871 case WLAN_CATEGORY_BACK:
2872 switch (mgmt->u.action.u.addba_req.action_code) {
2873 case WLAN_ACTION_ADDBA_REQ:
2874 if (len < (IEEE80211_MIN_ACTION_SIZE +
2875 sizeof(mgmt->u.action.u.addba_req)))
2877 ieee80211_sta_process_addba_request(local, mgmt, len);
2879 case WLAN_ACTION_ADDBA_RESP:
2880 if (len < (IEEE80211_MIN_ACTION_SIZE +
2881 sizeof(mgmt->u.action.u.addba_resp)))
2883 ieee80211_sta_process_addba_resp(local, mgmt, len);
2885 case WLAN_ACTION_DELBA:
2886 if (len < (IEEE80211_MIN_ACTION_SIZE +
2887 sizeof(mgmt->u.action.u.delba)))
2889 ieee80211_sta_process_delba(sdata, mgmt, len);
2893 case PLINK_CATEGORY:
2894 if (ieee80211_vif_is_mesh(&sdata->vif))
2895 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
2897 case MESH_PATH_SEL_CATEGORY:
2898 if (ieee80211_vif_is_mesh(&sdata->vif))
2899 mesh_rx_path_sel_frame(sdata, mgmt, len);
2904 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
2905 struct ieee80211_rx_status *rx_status)
2907 struct ieee80211_local *local = sdata->local;
2908 struct ieee80211_if_sta *ifsta;
2909 struct ieee80211_mgmt *mgmt;
2915 ifsta = &sdata->u.sta;
2917 mgmt = (struct ieee80211_mgmt *) skb->data;
2918 fc = le16_to_cpu(mgmt->frame_control);
2920 switch (fc & IEEE80211_FCTL_STYPE) {
2921 case IEEE80211_STYPE_PROBE_REQ:
2922 case IEEE80211_STYPE_PROBE_RESP:
2923 case IEEE80211_STYPE_BEACON:
2924 case IEEE80211_STYPE_ACTION:
2925 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2926 case IEEE80211_STYPE_AUTH:
2927 case IEEE80211_STYPE_ASSOC_RESP:
2928 case IEEE80211_STYPE_REASSOC_RESP:
2929 case IEEE80211_STYPE_DEAUTH:
2930 case IEEE80211_STYPE_DISASSOC:
2931 skb_queue_tail(&ifsta->skb_queue, skb);
2932 queue_work(local->hw.workqueue, &ifsta->work);
2940 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2941 struct sk_buff *skb)
2943 struct ieee80211_rx_status *rx_status;
2944 struct ieee80211_if_sta *ifsta;
2945 struct ieee80211_mgmt *mgmt;
2948 ifsta = &sdata->u.sta;
2950 rx_status = (struct ieee80211_rx_status *) skb->cb;
2951 mgmt = (struct ieee80211_mgmt *) skb->data;
2952 fc = le16_to_cpu(mgmt->frame_control);
2954 switch (fc & IEEE80211_FCTL_STYPE) {
2955 case IEEE80211_STYPE_PROBE_REQ:
2956 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
2959 case IEEE80211_STYPE_PROBE_RESP:
2960 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
2962 case IEEE80211_STYPE_BEACON:
2963 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2965 case IEEE80211_STYPE_AUTH:
2966 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
2968 case IEEE80211_STYPE_ASSOC_RESP:
2969 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2971 case IEEE80211_STYPE_REASSOC_RESP:
2972 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
2974 case IEEE80211_STYPE_DEAUTH:
2975 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
2977 case IEEE80211_STYPE_DISASSOC:
2978 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
2980 case IEEE80211_STYPE_ACTION:
2981 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
2990 ieee80211_sta_rx_scan(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
2991 struct ieee80211_rx_status *rx_status)
2993 struct ieee80211_mgmt *mgmt;
2997 return RX_DROP_UNUSABLE;
2999 mgmt = (struct ieee80211_mgmt *) skb->data;
3000 fc = mgmt->frame_control;
3002 if (ieee80211_is_ctl(fc))
3006 return RX_DROP_MONITOR;
3008 if (ieee80211_is_probe_resp(fc)) {
3009 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
3014 if (ieee80211_is_beacon(fc)) {
3015 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
3024 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
3026 struct ieee80211_local *local = sdata->local;
3028 struct sta_info *sta;
3032 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3033 if (sta->sdata == sdata &&
3034 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3047 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
3049 struct ieee80211_local *local = sdata->local;
3050 struct sta_info *sta, *tmp;
3051 LIST_HEAD(tmp_list);
3052 DECLARE_MAC_BUF(mac);
3053 unsigned long flags;
3055 spin_lock_irqsave(&local->sta_lock, flags);
3056 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3057 if (time_after(jiffies, sta->last_rx + exp_time)) {
3058 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3059 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3060 sdata->dev->name, print_mac(mac, sta->addr));
3062 __sta_info_unlink(&sta);
3064 list_add(&sta->list, &tmp_list);
3066 spin_unlock_irqrestore(&local->sta_lock, flags);
3068 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3069 sta_info_destroy(sta);
3073 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
3074 struct ieee80211_if_sta *ifsta)
3076 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3078 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
3079 if (ieee80211_sta_active_ibss(sdata))
3082 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3083 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
3084 ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
3088 #ifdef CONFIG_MAC80211_MESH
3089 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
3090 struct ieee80211_if_sta *ifsta)
3094 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3095 mesh_path_expire(sdata);
3097 free_plinks = mesh_plink_availables(sdata);
3098 if (free_plinks != sdata->u.sta.accepting_plinks)
3099 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3101 mod_timer(&ifsta->timer, jiffies +
3102 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3106 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
3108 struct ieee80211_if_sta *ifsta;
3109 ifsta = &sdata->u.sta;
3110 ifsta->state = IEEE80211_STA_MLME_MESH_UP;
3111 ieee80211_sta_timer((unsigned long)sdata);
3112 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3117 void ieee80211_sta_timer(unsigned long data)
3119 struct ieee80211_sub_if_data *sdata =
3120 (struct ieee80211_sub_if_data *) data;
3121 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3122 struct ieee80211_local *local = sdata->local;
3124 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3125 queue_work(local->hw.workqueue, &ifsta->work);
3128 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
3129 struct ieee80211_if_sta *ifsta)
3131 struct ieee80211_local *local = sdata->local;
3133 if (local->ops->reset_tsf) {
3134 /* Reset own TSF to allow time synchronization work. */
3135 local->ops->reset_tsf(local_to_hw(local));
3138 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3141 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3142 ifsta->auth_alg = WLAN_AUTH_OPEN;
3143 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3144 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3145 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3146 ifsta->auth_alg = WLAN_AUTH_LEAP;
3148 ifsta->auth_alg = WLAN_AUTH_OPEN;
3149 ifsta->auth_transaction = -1;
3150 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3151 ifsta->assoc_scan_tries = 0;
3152 ifsta->direct_probe_tries = 0;
3153 ifsta->auth_tries = 0;
3154 ifsta->assoc_tries = 0;
3155 netif_tx_stop_all_queues(sdata->dev);
3156 netif_carrier_off(sdata->dev);
3160 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
3161 struct ieee80211_if_sta *ifsta)
3163 struct ieee80211_local *local = sdata->local;
3165 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3168 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3169 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3170 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3171 IEEE80211_STA_AUTO_SSID_SEL))) {
3173 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
3174 ieee80211_set_disassoc(sdata, ifsta, true, true,
3175 WLAN_REASON_DEAUTH_LEAVING);
3177 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3178 queue_work(local->hw.workqueue, &ifsta->work);
3182 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3183 const char *ssid, int ssid_len)
3185 int tmp, hidden_ssid;
3187 if (ssid_len == ifsta->ssid_len &&
3188 !memcmp(ifsta->ssid, ssid, ssid_len))
3191 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3197 if (ssid[tmp] != '\0') {
3203 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3206 if (ssid_len == 1 && ssid[0] == ' ')
3212 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
3213 struct ieee80211_if_sta *ifsta)
3215 struct ieee80211_local *local = sdata->local;
3216 struct ieee80211_sta_bss *bss;
3217 struct ieee80211_supported_band *sband;
3218 u8 bssid[ETH_ALEN], *pos;
3221 DECLARE_MAC_BUF(mac);
3224 /* Easier testing, use fixed BSSID. */
3225 memset(bssid, 0xfe, ETH_ALEN);
3227 /* Generate random, not broadcast, locally administered BSSID. Mix in
3228 * own MAC address to make sure that devices that do not have proper
3229 * random number generator get different BSSID. */
3230 get_random_bytes(bssid, ETH_ALEN);
3231 for (i = 0; i < ETH_ALEN; i++)
3232 bssid[i] ^= sdata->dev->dev_addr[i];
3237 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3238 sdata->dev->name, print_mac(mac, bssid));
3240 bss = ieee80211_rx_bss_add(sdata, bssid,
3241 local->hw.conf.channel->center_freq,
3242 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3246 bss->band = local->hw.conf.channel->band;
3247 sband = local->hw.wiphy->bands[bss->band];
3249 if (local->hw.conf.beacon_int == 0)
3250 local->hw.conf.beacon_int = 100;
3251 bss->beacon_int = local->hw.conf.beacon_int;
3252 bss->last_update = jiffies;
3253 bss->capability = WLAN_CAPABILITY_IBSS;
3255 if (sdata->default_key)
3256 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3258 sdata->drop_unencrypted = 0;
3260 bss->supp_rates_len = sband->n_bitrates;
3261 pos = bss->supp_rates;
3262 for (i = 0; i < sband->n_bitrates; i++) {
3263 int rate = sband->bitrates[i].bitrate;
3264 *pos++ = (u8) (rate / 5);
3267 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3268 ieee80211_rx_bss_put(local, bss);
3273 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
3274 struct ieee80211_if_sta *ifsta)
3276 struct ieee80211_local *local = sdata->local;
3277 struct ieee80211_sta_bss *bss;
3281 DECLARE_MAC_BUF(mac);
3282 DECLARE_MAC_BUF(mac2);
3284 if (ifsta->ssid_len == 0)
3287 active_ibss = ieee80211_sta_active_ibss(sdata);
3288 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3289 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3290 sdata->dev->name, active_ibss);
3291 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3292 spin_lock_bh(&local->sta_bss_lock);
3293 list_for_each_entry(bss, &local->sta_bss_list, list) {
3294 if (ifsta->ssid_len != bss->ssid_len ||
3295 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3296 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3298 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3299 printk(KERN_DEBUG " bssid=%s found\n",
3300 print_mac(mac, bss->bssid));
3301 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3302 memcpy(bssid, bss->bssid, ETH_ALEN);
3304 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3307 spin_unlock_bh(&local->sta_bss_lock);
3309 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3311 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3312 "%s\n", print_mac(mac, bssid),
3313 print_mac(mac2, ifsta->bssid));
3314 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3316 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3320 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3321 search_freq = bss->freq;
3323 search_freq = local->hw.conf.channel->center_freq;
3325 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
3326 ifsta->ssid, ifsta->ssid_len);
3330 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3331 " based on configured SSID\n",
3332 sdata->dev->name, print_mac(mac, bssid));
3333 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3334 ieee80211_rx_bss_put(local, bss);
3339 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3340 printk(KERN_DEBUG " did not try to join ibss\n");
3341 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3343 /* Selected IBSS not found in current scan results - try to scan */
3344 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
3345 !ieee80211_sta_active_ibss(sdata)) {
3346 mod_timer(&ifsta->timer, jiffies +
3347 IEEE80211_IBSS_MERGE_INTERVAL);
3348 } else if (time_after(jiffies, local->last_scan_completed +
3349 IEEE80211_SCAN_INTERVAL)) {
3350 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3351 "join\n", sdata->dev->name);
3352 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
3354 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
3355 int interval = IEEE80211_SCAN_INTERVAL;
3357 if (time_after(jiffies, ifsta->ibss_join_req +
3358 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3359 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3360 (!(local->oper_channel->flags &
3361 IEEE80211_CHAN_NO_IBSS)))
3362 return ieee80211_sta_create_ibss(sdata, ifsta);
3363 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3364 printk(KERN_DEBUG "%s: IBSS not allowed on"
3365 " %d MHz\n", sdata->dev->name,
3366 local->hw.conf.channel->center_freq);
3369 /* No IBSS found - decrease scan interval and continue
3371 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3374 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
3375 mod_timer(&ifsta->timer, jiffies + interval);
3383 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
3385 struct ieee80211_if_sta *ifsta;
3388 if (len > IEEE80211_MAX_SSID_LEN)
3391 ifsta = &sdata->u.sta;
3393 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3394 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3395 memcpy(ifsta->ssid, ssid, len);
3396 ifsta->ssid_len = len;
3397 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3401 * Hack! MLME code needs to be cleaned up to have different
3402 * entry points for configuration and internal selection change
3404 if (netif_running(sdata->dev))
3405 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3407 printk(KERN_DEBUG "%s: Failed to config new SSID to "
3408 "the low-level driver\n", sdata->dev->name);
3414 ifsta->flags |= IEEE80211_STA_SSID_SET;
3416 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3418 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3419 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3420 ifsta->ibss_join_req = jiffies;
3421 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
3422 return ieee80211_sta_find_ibss(sdata, ifsta);
3429 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
3431 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3432 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3433 *len = ifsta->ssid_len;
3438 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
3440 struct ieee80211_if_sta *ifsta;
3443 ifsta = &sdata->u.sta;
3445 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3446 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3449 * Hack! See also ieee80211_sta_set_ssid.
3451 if (netif_running(sdata->dev))
3452 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
3454 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3455 "the low-level driver\n", sdata->dev->name);
3460 if (is_valid_ether_addr(bssid))
3461 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3463 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3469 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3470 struct ieee80211_sub_if_data *sdata,
3473 struct sk_buff *skb;
3474 struct ieee80211_hdr *nullfunc;
3477 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3479 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3480 "frame\n", sdata->dev->name);
3483 skb_reserve(skb, local->hw.extra_tx_headroom);
3485 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3486 memset(nullfunc, 0, 24);
3487 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3488 IEEE80211_FCTL_TODS);
3490 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3491 nullfunc->frame_control = fc;
3492 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3493 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3494 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3496 ieee80211_sta_tx(sdata, skb, 0);
3500 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3502 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3503 ieee80211_vif_is_mesh(&sdata->vif))
3504 ieee80211_sta_timer((unsigned long)sdata);
3507 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3509 struct ieee80211_local *local = hw_to_local(hw);
3510 struct ieee80211_sub_if_data *sdata;
3511 union iwreq_data wrqu;
3513 local->last_scan_completed = jiffies;
3514 memset(&wrqu, 0, sizeof(wrqu));
3515 wireless_send_event(local->scan_sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
3517 if (local->sta_hw_scanning) {
3518 local->sta_hw_scanning = 0;
3519 if (ieee80211_hw_config(local))
3520 printk(KERN_DEBUG "%s: failed to restore operational "
3521 "channel after scan\n", wiphy_name(local->hw.wiphy));
3522 /* Restart STA timer for HW scan case */
3524 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3525 ieee80211_restart_sta_timer(sdata);
3531 local->sta_sw_scanning = 0;
3532 if (ieee80211_hw_config(local))
3533 printk(KERN_DEBUG "%s: failed to restore operational "
3534 "channel after scan\n", wiphy_name(local->hw.wiphy));
3537 netif_tx_lock_bh(local->mdev);
3538 netif_addr_lock(local->mdev);
3539 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3540 local->ops->configure_filter(local_to_hw(local),
3541 FIF_BCN_PRBRESP_PROMISC,
3542 &local->filter_flags,
3543 local->mdev->mc_count,
3544 local->mdev->mc_list);
3546 netif_addr_unlock(local->mdev);
3547 netif_tx_unlock_bh(local->mdev);
3550 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3551 /* Tell AP we're back */
3552 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3553 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
3554 ieee80211_send_nullfunc(local, sdata, 0);
3555 netif_tx_wake_all_queues(sdata->dev);
3558 netif_tx_wake_all_queues(sdata->dev);
3560 ieee80211_restart_sta_timer(sdata);
3565 sdata = local->scan_sdata;
3566 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3567 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3568 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3569 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
3570 !ieee80211_sta_active_ibss(sdata)))
3571 ieee80211_sta_find_ibss(sdata, ifsta);
3574 EXPORT_SYMBOL(ieee80211_scan_completed);
3576 void ieee80211_sta_scan_work(struct work_struct *work)
3578 struct ieee80211_local *local =
3579 container_of(work, struct ieee80211_local, scan_work.work);
3580 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
3581 struct ieee80211_supported_band *sband;
3582 struct ieee80211_channel *chan;
3584 unsigned long next_delay = 0;
3586 if (!local->sta_sw_scanning)
3589 switch (local->scan_state) {
3590 case SCAN_SET_CHANNEL:
3592 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3593 * after we successfully scanned the last channel of the last
3594 * band (and the last band is supported by the hw)
3596 if (local->scan_band < IEEE80211_NUM_BANDS)
3597 sband = local->hw.wiphy->bands[local->scan_band];
3602 * If we are at an unsupported band and have more bands
3603 * left to scan, advance to the next supported one.
3605 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3607 sband = local->hw.wiphy->bands[local->scan_band];
3608 local->scan_channel_idx = 0;
3611 /* if no more bands/channels left, complete scan */
3612 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3613 ieee80211_scan_completed(local_to_hw(local));
3617 chan = &sband->channels[local->scan_channel_idx];
3619 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3620 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3621 chan->flags & IEEE80211_CHAN_NO_IBSS))
3625 local->scan_channel = chan;
3626 if (ieee80211_hw_config(local)) {
3627 printk(KERN_DEBUG "%s: failed to set freq to "
3628 "%d MHz for scan\n", wiphy_name(local->hw.wiphy),
3634 /* advance state machine to next channel/band */
3635 local->scan_channel_idx++;
3636 if (local->scan_channel_idx >= sband->n_channels) {
3638 * scan_band may end up == IEEE80211_NUM_BANDS, but
3639 * we'll catch that case above and complete the scan
3640 * if that is the case.
3643 local->scan_channel_idx = 0;
3649 next_delay = IEEE80211_PROBE_DELAY +
3650 usecs_to_jiffies(local->hw.channel_change_time);
3651 local->scan_state = SCAN_SEND_PROBE;
3653 case SCAN_SEND_PROBE:
3654 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3655 local->scan_state = SCAN_SET_CHANNEL;
3657 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3659 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
3660 local->scan_ssid_len);
3661 next_delay = IEEE80211_CHANNEL_TIME;
3665 if (local->sta_sw_scanning)
3666 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3671 static int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *scan_sdata,
3672 u8 *ssid, size_t ssid_len)
3674 struct ieee80211_local *local = scan_sdata->local;
3675 struct ieee80211_sub_if_data *sdata;
3677 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3680 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3681 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3684 * ScanType: ACTIVE, PASSIVE
3685 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3686 * a Probe frame during active scanning
3688 * MinChannelTime (>= ProbeDelay), in TU
3689 * MaxChannelTime: (>= MinChannelTime), in TU
3692 /* MLME-SCAN.confirm
3694 * ResultCode: SUCCESS, INVALID_PARAMETERS
3697 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3698 if (local->scan_sdata == scan_sdata)
3703 if (local->ops->hw_scan) {
3704 int rc = local->ops->hw_scan(local_to_hw(local),
3707 local->sta_hw_scanning = 1;
3708 local->scan_sdata = scan_sdata;
3713 local->sta_sw_scanning = 1;
3716 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3717 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3718 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
3719 netif_tx_stop_all_queues(sdata->dev);
3720 ieee80211_send_nullfunc(local, sdata, 1);
3723 netif_tx_stop_all_queues(sdata->dev);
3728 local->scan_ssid_len = ssid_len;
3729 memcpy(local->scan_ssid, ssid, ssid_len);
3731 local->scan_ssid_len = 0;
3732 local->scan_state = SCAN_SET_CHANNEL;
3733 local->scan_channel_idx = 0;
3734 local->scan_band = IEEE80211_BAND_2GHZ;
3735 local->scan_sdata = scan_sdata;
3737 netif_addr_lock_bh(local->mdev);
3738 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3739 local->ops->configure_filter(local_to_hw(local),
3740 FIF_BCN_PRBRESP_PROMISC,
3741 &local->filter_flags,
3742 local->mdev->mc_count,
3743 local->mdev->mc_list);
3744 netif_addr_unlock_bh(local->mdev);
3746 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3747 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3748 IEEE80211_CHANNEL_TIME);
3754 int ieee80211_sta_req_scan(struct ieee80211_sub_if_data *sdata, u8 *ssid, size_t ssid_len)
3756 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3757 struct ieee80211_local *local = sdata->local;
3759 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3760 return ieee80211_sta_start_scan(sdata, ssid, ssid_len);
3762 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3763 if (local->scan_sdata == sdata)
3768 ifsta->scan_ssid_len = ssid_len;
3770 memcpy(ifsta->scan_ssid, ssid, ssid_len);
3771 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3772 queue_work(local->hw.workqueue, &ifsta->work);
3777 static void ieee80211_sta_add_scan_ies(struct iw_request_info *info,
3778 struct ieee80211_sta_bss *bss,
3779 char **current_ev, char *end_buf)
3781 u8 *pos, *end, *next;
3782 struct iw_event iwe;
3784 if (bss == NULL || bss->ies == NULL)
3788 * If needed, fragment the IEs buffer (at IE boundaries) into short
3789 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
3792 end = pos + bss->ies_len;
3794 while (end - pos > IW_GENERIC_IE_MAX) {
3795 next = pos + 2 + pos[1];
3796 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
3797 next = next + 2 + next[1];
3799 memset(&iwe, 0, sizeof(iwe));
3800 iwe.cmd = IWEVGENIE;
3801 iwe.u.data.length = next - pos;
3802 *current_ev = iwe_stream_add_point(info, *current_ev,
3803 end_buf, &iwe, pos);
3809 memset(&iwe, 0, sizeof(iwe));
3810 iwe.cmd = IWEVGENIE;
3811 iwe.u.data.length = end - pos;
3812 *current_ev = iwe_stream_add_point(info, *current_ev,
3813 end_buf, &iwe, pos);
3819 ieee80211_sta_scan_result(struct ieee80211_local *local,
3820 struct iw_request_info *info,
3821 struct ieee80211_sta_bss *bss,
3822 char *current_ev, char *end_buf)
3824 struct iw_event iwe;
3826 if (time_after(jiffies,
3827 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3830 memset(&iwe, 0, sizeof(iwe));
3831 iwe.cmd = SIOCGIWAP;
3832 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3833 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3834 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3837 memset(&iwe, 0, sizeof(iwe));
3838 iwe.cmd = SIOCGIWESSID;
3839 if (bss_mesh_cfg(bss)) {
3840 iwe.u.data.length = bss_mesh_id_len(bss);
3841 iwe.u.data.flags = 1;
3842 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3843 &iwe, bss_mesh_id(bss));
3845 iwe.u.data.length = bss->ssid_len;
3846 iwe.u.data.flags = 1;
3847 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3851 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3852 || bss_mesh_cfg(bss)) {
3853 memset(&iwe, 0, sizeof(iwe));
3854 iwe.cmd = SIOCGIWMODE;
3855 if (bss_mesh_cfg(bss))
3856 iwe.u.mode = IW_MODE_MESH;
3857 else if (bss->capability & WLAN_CAPABILITY_ESS)
3858 iwe.u.mode = IW_MODE_MASTER;
3860 iwe.u.mode = IW_MODE_ADHOC;
3861 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
3862 &iwe, IW_EV_UINT_LEN);
3865 memset(&iwe, 0, sizeof(iwe));
3866 iwe.cmd = SIOCGIWFREQ;
3867 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3869 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3872 memset(&iwe, 0, sizeof(iwe));
3873 iwe.cmd = SIOCGIWFREQ;
3874 iwe.u.freq.m = bss->freq;
3876 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3878 memset(&iwe, 0, sizeof(iwe));
3880 iwe.u.qual.qual = bss->qual;
3881 iwe.u.qual.level = bss->signal;
3882 iwe.u.qual.noise = bss->noise;
3883 iwe.u.qual.updated = local->wstats_flags;
3884 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3887 memset(&iwe, 0, sizeof(iwe));
3888 iwe.cmd = SIOCGIWENCODE;
3889 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3890 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3892 iwe.u.data.flags = IW_ENCODE_DISABLED;
3893 iwe.u.data.length = 0;
3894 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3897 ieee80211_sta_add_scan_ies(info, bss, ¤t_ev, end_buf);
3899 if (bss && bss->supp_rates_len > 0) {
3900 /* display all supported rates in readable format */
3901 char *p = current_ev + iwe_stream_lcp_len(info);
3904 memset(&iwe, 0, sizeof(iwe));
3905 iwe.cmd = SIOCGIWRATE;
3906 /* Those two flags are ignored... */
3907 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3909 for (i = 0; i < bss->supp_rates_len; i++) {
3910 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3912 p = iwe_stream_add_value(info, current_ev, p,
3913 end_buf, &iwe, IW_EV_PARAM_LEN);
3920 buf = kmalloc(30, GFP_ATOMIC);
3922 memset(&iwe, 0, sizeof(iwe));
3923 iwe.cmd = IWEVCUSTOM;
3924 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3925 iwe.u.data.length = strlen(buf);
3926 current_ev = iwe_stream_add_point(info, current_ev,
3929 memset(&iwe, 0, sizeof(iwe));
3930 iwe.cmd = IWEVCUSTOM;
3931 sprintf(buf, " Last beacon: %dms ago",
3932 jiffies_to_msecs(jiffies - bss->last_update));
3933 iwe.u.data.length = strlen(buf);
3934 current_ev = iwe_stream_add_point(info, current_ev,
3935 end_buf, &iwe, buf);
3940 if (bss_mesh_cfg(bss)) {
3942 u8 *cfg = bss_mesh_cfg(bss);
3943 buf = kmalloc(50, GFP_ATOMIC);
3945 memset(&iwe, 0, sizeof(iwe));
3946 iwe.cmd = IWEVCUSTOM;
3947 sprintf(buf, "Mesh network (version %d)", cfg[0]);
3948 iwe.u.data.length = strlen(buf);
3949 current_ev = iwe_stream_add_point(info, current_ev,
3952 sprintf(buf, "Path Selection Protocol ID: "
3953 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
3955 iwe.u.data.length = strlen(buf);
3956 current_ev = iwe_stream_add_point(info, current_ev,
3959 sprintf(buf, "Path Selection Metric ID: "
3960 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
3962 iwe.u.data.length = strlen(buf);
3963 current_ev = iwe_stream_add_point(info, current_ev,
3966 sprintf(buf, "Congestion Control Mode ID: "
3967 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
3969 iwe.u.data.length = strlen(buf);
3970 current_ev = iwe_stream_add_point(info, current_ev,
3973 sprintf(buf, "Channel Precedence: "
3974 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
3976 iwe.u.data.length = strlen(buf);
3977 current_ev = iwe_stream_add_point(info, current_ev,
3988 int ieee80211_sta_scan_results(struct ieee80211_local *local,
3989 struct iw_request_info *info,
3990 char *buf, size_t len)
3992 char *current_ev = buf;
3993 char *end_buf = buf + len;
3994 struct ieee80211_sta_bss *bss;
3996 spin_lock_bh(&local->sta_bss_lock);
3997 list_for_each_entry(bss, &local->sta_bss_list, list) {
3998 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3999 spin_unlock_bh(&local->sta_bss_lock);
4002 current_ev = ieee80211_sta_scan_result(local, info, bss,
4003 current_ev, end_buf);
4005 spin_unlock_bh(&local->sta_bss_lock);
4006 return current_ev - buf;
4010 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
4012 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4014 kfree(ifsta->extra_ie);
4016 ifsta->extra_ie = NULL;
4017 ifsta->extra_ie_len = 0;
4020 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4021 if (!ifsta->extra_ie) {
4022 ifsta->extra_ie_len = 0;
4025 memcpy(ifsta->extra_ie, ie, len);
4026 ifsta->extra_ie_len = len;
4031 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
4032 struct sk_buff *skb, u8 *bssid,
4033 u8 *addr, u64 supp_rates)
4035 struct ieee80211_local *local = sdata->local;
4036 struct sta_info *sta;
4037 DECLARE_MAC_BUF(mac);
4038 int band = local->hw.conf.channel->band;
4040 /* TODO: Could consider removing the least recently used entry and
4041 * allow new one to be added. */
4042 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4043 if (net_ratelimit()) {
4044 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4045 "entry %s\n", sdata->dev->name, print_mac(mac, addr));
4050 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
4053 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4054 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4055 wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
4058 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4062 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
4064 /* make sure mandatory rates are always added */
4065 sta->supp_rates[band] = supp_rates |
4066 ieee80211_sta_get_mandatory_rates(local, band);
4068 rate_control_rate_init(sta, local);
4070 if (sta_info_insert(sta))
4077 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
4078 struct ieee80211_if_sta *ifsta)
4080 struct ieee80211_local *local = sdata->local;
4081 struct ieee80211_sta_bss *bss, *selected = NULL;
4082 int top_rssi = 0, freq;
4084 spin_lock_bh(&local->sta_bss_lock);
4085 freq = local->oper_channel->center_freq;
4086 list_for_each_entry(bss, &local->sta_bss_list, list) {
4087 if (!(bss->capability & WLAN_CAPABILITY_ESS))
4090 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
4091 IEEE80211_STA_AUTO_BSSID_SEL |
4092 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
4093 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
4094 !!sdata->default_key))
4097 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
4101 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
4102 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
4105 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
4106 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
4109 if (!selected || top_rssi < bss->signal) {
4111 top_rssi = bss->signal;
4115 atomic_inc(&selected->users);
4116 spin_unlock_bh(&local->sta_bss_lock);
4119 ieee80211_set_freq(sdata, selected->freq);
4120 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
4121 ieee80211_sta_set_ssid(sdata, selected->ssid,
4122 selected->ssid_len);
4123 ieee80211_sta_set_bssid(sdata, selected->bssid);
4124 ieee80211_sta_def_wmm_params(sdata, selected, 0);
4126 /* Send out direct probe if no probe resp was received or
4127 * the one we have is outdated
4129 if (!selected->last_probe_resp ||
4130 time_after(jiffies, selected->last_probe_resp
4131 + IEEE80211_SCAN_RESULT_EXPIRE))
4132 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
4134 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
4136 ieee80211_rx_bss_put(local, selected);
4137 ieee80211_sta_reset_auth(sdata, ifsta);
4140 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
4141 ifsta->assoc_scan_tries++;
4142 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
4143 ieee80211_sta_start_scan(sdata, NULL, 0);
4145 ieee80211_sta_start_scan(sdata, ifsta->ssid,
4147 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
4148 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
4150 ifsta->state = IEEE80211_STA_MLME_DISABLED;
4156 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
4158 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4160 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
4161 sdata->dev->name, reason);
4163 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4164 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4167 ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
4172 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
4174 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4176 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
4177 sdata->dev->name, reason);
4179 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4182 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4185 ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
4189 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4190 enum ieee80211_notification_types notif_type)
4192 struct ieee80211_local *local = hw_to_local(hw);
4193 struct ieee80211_sub_if_data *sdata;
4195 switch (notif_type) {
4196 case IEEE80211_NOTIFY_RE_ASSOC:
4198 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4199 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4202 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
4208 EXPORT_SYMBOL(ieee80211_notify_mac);
4210 void ieee80211_sta_work(struct work_struct *work)
4212 struct ieee80211_sub_if_data *sdata =
4213 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
4214 struct ieee80211_local *local = sdata->local;
4215 struct ieee80211_if_sta *ifsta;
4216 struct sk_buff *skb;
4218 if (!netif_running(sdata->dev))
4221 if (local->sta_sw_scanning || local->sta_hw_scanning)
4224 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4225 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
4226 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
4228 ifsta = &sdata->u.sta;
4230 while ((skb = skb_dequeue(&ifsta->skb_queue)))
4231 ieee80211_sta_rx_queued_mgmt(sdata, skb);
4233 #ifdef CONFIG_MAC80211_MESH
4234 if (ifsta->preq_queue_len &&
4236 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
4237 mesh_path_start_discovery(sdata);
4240 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
4241 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
4242 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
4243 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
4244 if (ifsta->scan_ssid_len)
4245 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
4247 ieee80211_sta_start_scan(sdata, NULL, 0);
4251 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
4252 if (ieee80211_sta_config_auth(sdata, ifsta))
4254 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
4255 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
4258 switch (ifsta->state) {
4259 case IEEE80211_STA_MLME_DISABLED:
4261 case IEEE80211_STA_MLME_DIRECT_PROBE:
4262 ieee80211_direct_probe(sdata, ifsta);
4264 case IEEE80211_STA_MLME_AUTHENTICATE:
4265 ieee80211_authenticate(sdata, ifsta);
4267 case IEEE80211_STA_MLME_ASSOCIATE:
4268 ieee80211_associate(sdata, ifsta);
4270 case IEEE80211_STA_MLME_ASSOCIATED:
4271 ieee80211_associated(sdata, ifsta);
4273 case IEEE80211_STA_MLME_IBSS_SEARCH:
4274 ieee80211_sta_find_ibss(sdata, ifsta);
4276 case IEEE80211_STA_MLME_IBSS_JOINED:
4277 ieee80211_sta_merge_ibss(sdata, ifsta);
4279 #ifdef CONFIG_MAC80211_MESH
4280 case IEEE80211_STA_MLME_MESH_UP:
4281 ieee80211_mesh_housekeeping(sdata, ifsta);
4289 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
4290 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
4291 "mixed-cell disabled - disassociate\n", sdata->dev->name);
4293 ieee80211_set_disassoc(sdata, ifsta, false, true,
4294 WLAN_REASON_UNSPECIFIED);