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 <asm/types.h>
31 #include <net/mac80211.h>
32 #include "ieee80211_i.h"
37 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
38 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
39 #define IEEE80211_AUTH_MAX_TRIES 3
40 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
41 #define IEEE80211_ASSOC_MAX_TRIES 3
42 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
43 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
44 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
45 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
46 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
47 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
48 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
50 #define IEEE80211_PROBE_DELAY (HZ / 33)
51 #define IEEE80211_CHANNEL_TIME (HZ / 33)
52 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
53 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
54 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
55 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
56 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
58 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
61 #define ERP_INFO_USE_PROTECTION BIT(1)
63 /* mgmt header + 1 byte action code */
64 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
66 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
67 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
68 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
69 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
70 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
72 /* next values represent the buffer size for A-MPDU frame.
73 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
74 #define IEEE80211_MIN_AMPDU_BUF 0x8
75 #define IEEE80211_MAX_AMPDU_BUF 0x40
77 static void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
78 u8 *ssid, size_t ssid_len);
79 static struct ieee80211_sta_bss *
80 ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
81 u8 *ssid, u8 ssid_len);
82 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
83 struct ieee80211_sta_bss *bss);
84 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
85 struct ieee80211_if_sta *ifsta);
86 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata);
87 static int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *sdata,
88 u8 *ssid, size_t ssid_len);
89 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
90 struct ieee80211_if_sta *ifsta);
91 static void sta_rx_agg_session_timer_expired(unsigned long data);
94 void ieee802_11_parse_elems(u8 *start, size_t len,
95 struct ieee802_11_elems *elems)
100 memset(elems, 0, sizeof(*elems));
101 elems->ie_start = start;
102 elems->total_len = len;
117 elems->ssid_len = elen;
119 case WLAN_EID_SUPP_RATES:
120 elems->supp_rates = pos;
121 elems->supp_rates_len = elen;
123 case WLAN_EID_FH_PARAMS:
124 elems->fh_params = pos;
125 elems->fh_params_len = elen;
127 case WLAN_EID_DS_PARAMS:
128 elems->ds_params = pos;
129 elems->ds_params_len = elen;
131 case WLAN_EID_CF_PARAMS:
132 elems->cf_params = pos;
133 elems->cf_params_len = elen;
137 elems->tim_len = elen;
139 case WLAN_EID_IBSS_PARAMS:
140 elems->ibss_params = pos;
141 elems->ibss_params_len = elen;
143 case WLAN_EID_CHALLENGE:
144 elems->challenge = pos;
145 elems->challenge_len = elen;
148 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
150 /* Microsoft OUI (00:50:F2) */
152 /* OUI Type 1 - WPA IE */
154 elems->wpa_len = elen;
155 } else if (elen >= 5 && pos[3] == 2) {
157 elems->wmm_info = pos;
158 elems->wmm_info_len = elen;
159 } else if (pos[4] == 1) {
160 elems->wmm_param = pos;
161 elems->wmm_param_len = elen;
168 elems->rsn_len = elen;
170 case WLAN_EID_ERP_INFO:
171 elems->erp_info = pos;
172 elems->erp_info_len = elen;
174 case WLAN_EID_EXT_SUPP_RATES:
175 elems->ext_supp_rates = pos;
176 elems->ext_supp_rates_len = elen;
178 case WLAN_EID_HT_CAPABILITY:
179 elems->ht_cap_elem = pos;
180 elems->ht_cap_elem_len = elen;
182 case WLAN_EID_HT_EXTRA_INFO:
183 elems->ht_info_elem = pos;
184 elems->ht_info_elem_len = elen;
186 case WLAN_EID_MESH_ID:
187 elems->mesh_id = pos;
188 elems->mesh_id_len = elen;
190 case WLAN_EID_MESH_CONFIG:
191 elems->mesh_config = pos;
192 elems->mesh_config_len = elen;
194 case WLAN_EID_PEER_LINK:
195 elems->peer_link = pos;
196 elems->peer_link_len = elen;
200 elems->preq_len = elen;
204 elems->prep_len = elen;
208 elems->perr_len = elen;
210 case WLAN_EID_CHANNEL_SWITCH:
211 elems->ch_switch_elem = pos;
212 elems->ch_switch_elem_len = elen;
215 if (!elems->quiet_elem) {
216 elems->quiet_elem = pos;
217 elems->quiet_elem_len = elen;
219 elems->num_of_quiet_elem++;
221 case WLAN_EID_COUNTRY:
222 elems->country_elem = pos;
223 elems->country_elem_len = elen;
225 case WLAN_EID_PWR_CONSTRAINT:
226 elems->pwr_constr_elem = pos;
227 elems->pwr_constr_elem_len = elen;
239 static u8 * ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
246 end = pos + bss->ies_len;
248 while (pos + 1 < end) {
249 if (pos + 2 + pos[1] > end)
260 static int ecw2cw(int ecw)
262 return (1 << ecw) - 1;
266 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
267 struct ieee80211_sta_bss *bss,
270 struct ieee80211_local *local = sdata->local;
271 int i, have_higher_than_11mbit = 0;
274 /* cf. IEEE 802.11 9.2.12 */
275 for (i = 0; i < bss->supp_rates_len; i++)
276 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
277 have_higher_than_11mbit = 1;
279 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
280 have_higher_than_11mbit)
281 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
283 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
286 if (local->ops->conf_tx) {
287 struct ieee80211_tx_queue_params qparam;
289 memset(&qparam, 0, sizeof(qparam));
293 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
294 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
299 qparam.cw_max = 1023;
302 for (i = 0; i < local_to_hw(local)->queues; i++)
303 local->ops->conf_tx(local_to_hw(local), i, &qparam);
307 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
308 struct ieee80211_if_sta *ifsta,
309 u8 *wmm_param, size_t wmm_param_len)
311 struct ieee80211_tx_queue_params params;
316 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
322 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
324 count = wmm_param[6] & 0x0f;
325 if (count == ifsta->wmm_last_param_set)
327 ifsta->wmm_last_param_set = count;
330 left = wmm_param_len - 8;
332 memset(¶ms, 0, sizeof(params));
334 if (!local->ops->conf_tx)
338 for (; left >= 4; left -= 4, pos += 4) {
339 int aci = (pos[0] >> 5) & 0x03;
340 int acm = (pos[0] >> 4) & 0x01;
347 local->wmm_acm |= BIT(0) | BIT(3);
352 local->wmm_acm |= BIT(4) | BIT(5);
357 local->wmm_acm |= BIT(6) | BIT(7);
363 local->wmm_acm |= BIT(1) | BIT(2);
367 params.aifs = pos[0] & 0x0f;
368 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
369 params.cw_min = ecw2cw(pos[1] & 0x0f);
370 params.txop = get_unaligned_le16(pos + 2);
371 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
372 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
373 "cWmin=%d cWmax=%d txop=%d\n",
374 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
375 params.cw_max, params.txop);
377 /* TODO: handle ACM (block TX, fallback to next lowest allowed
379 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
380 printk(KERN_DEBUG "%s: failed to set TX queue "
381 "parameters for queue %d\n", local->mdev->name, queue);
386 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
388 bool use_short_preamble)
390 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
391 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
392 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
393 DECLARE_MAC_BUF(mac);
397 if (use_protection != bss_conf->use_cts_prot) {
398 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
399 if (net_ratelimit()) {
400 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
403 use_protection ? "enabled" : "disabled",
404 print_mac(mac, ifsta->bssid));
407 bss_conf->use_cts_prot = use_protection;
408 changed |= BSS_CHANGED_ERP_CTS_PROT;
411 if (use_short_preamble != bss_conf->use_short_preamble) {
412 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
413 if (net_ratelimit()) {
414 printk(KERN_DEBUG "%s: switched to %s barker preamble"
417 use_short_preamble ? "short" : "long",
418 print_mac(mac, ifsta->bssid));
421 bss_conf->use_short_preamble = use_short_preamble;
422 changed |= BSS_CHANGED_ERP_PREAMBLE;
428 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
431 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
432 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
434 return ieee80211_handle_protect_preamb(sdata,
435 use_protection, use_short_preamble);
438 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
439 struct ieee80211_sta_bss *bss)
443 if (bss->has_erp_value)
444 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
446 u16 capab = bss->capability;
447 changed |= ieee80211_handle_protect_preamb(sdata, false,
448 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
454 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
455 struct ieee80211_ht_info *ht_info)
461 memset(ht_info, 0, sizeof(*ht_info));
464 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
466 ht_info->ht_supported = 1;
467 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
468 ht_info->ampdu_factor =
469 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
470 ht_info->ampdu_density =
471 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
472 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
474 ht_info->ht_supported = 0;
479 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
480 struct ieee80211_ht_addt_info *ht_add_info_ie,
481 struct ieee80211_ht_bss_info *bss_info)
483 if (bss_info == NULL)
486 memset(bss_info, 0, sizeof(*bss_info));
488 if (ht_add_info_ie) {
490 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
492 bss_info->primary_channel = ht_add_info_ie->control_chan;
493 bss_info->bss_cap = ht_add_info_ie->ht_param;
494 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
500 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
501 struct ieee80211_if_sta *ifsta)
503 union iwreq_data wrqu;
505 if (ifsta->assocreq_ies) {
506 memset(&wrqu, 0, sizeof(wrqu));
507 wrqu.data.length = ifsta->assocreq_ies_len;
508 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
509 ifsta->assocreq_ies);
512 if (ifsta->assocresp_ies) {
513 memset(&wrqu, 0, sizeof(wrqu));
514 wrqu.data.length = ifsta->assocresp_ies_len;
515 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
516 ifsta->assocresp_ies);
521 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
522 struct ieee80211_if_sta *ifsta,
525 struct ieee80211_local *local = sdata->local;
526 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
527 union iwreq_data wrqu;
528 u32 changed = BSS_CHANGED_ASSOC;
531 struct ieee80211_sta_bss *bss;
533 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
535 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
538 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
539 conf->channel->center_freq,
540 ifsta->ssid, ifsta->ssid_len);
542 /* set timing information */
543 sdata->bss_conf.beacon_int = bss->beacon_int;
544 sdata->bss_conf.timestamp = bss->timestamp;
545 sdata->bss_conf.dtim_period = bss->dtim_period;
547 changed |= ieee80211_handle_bss_capability(sdata, bss);
549 ieee80211_rx_bss_put(local, bss);
552 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
553 changed |= BSS_CHANGED_HT;
554 sdata->bss_conf.assoc_ht = 1;
555 sdata->bss_conf.ht_conf = &conf->ht_conf;
556 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
559 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
560 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
561 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
562 ieee80211_sta_send_associnfo(sdata, ifsta);
564 netif_carrier_off(sdata->dev);
565 ieee80211_sta_tear_down_BA_sessions(sdata, ifsta->bssid);
566 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
567 changed |= ieee80211_reset_erp_info(sdata);
569 sdata->bss_conf.assoc_ht = 0;
570 sdata->bss_conf.ht_conf = NULL;
571 sdata->bss_conf.ht_bss_conf = NULL;
573 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
575 ifsta->last_probe = jiffies;
576 ieee80211_led_assoc(local, assoc);
578 sdata->bss_conf.assoc = assoc;
579 ieee80211_bss_info_change_notify(sdata, changed);
582 netif_carrier_on(sdata->dev);
584 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
585 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
588 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
589 struct ieee80211_if_sta *ifsta, int deauth)
592 ifsta->direct_probe_tries = 0;
593 ifsta->auth_tries = 0;
595 ifsta->assoc_scan_tries = 0;
596 ifsta->assoc_tries = 0;
597 ieee80211_set_associated(sdata, ifsta, 0);
600 void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
603 skb->dev = sdata->local->mdev;
604 skb_set_mac_header(skb, 0);
605 skb_set_network_header(skb, 0);
606 skb_set_transport_header(skb, 0);
608 skb->iif = sdata->dev->ifindex;
609 skb->do_not_encrypt = !encrypt;
615 static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
616 struct ieee80211_if_sta *ifsta,
617 int transaction, u8 *extra, size_t extra_len,
620 struct ieee80211_local *local = sdata->local;
622 struct ieee80211_mgmt *mgmt;
624 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
625 sizeof(*mgmt) + 6 + extra_len);
627 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
628 "frame\n", sdata->dev->name);
631 skb_reserve(skb, local->hw.extra_tx_headroom);
633 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
634 memset(mgmt, 0, 24 + 6);
635 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
636 IEEE80211_STYPE_AUTH);
638 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
639 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
640 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
641 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
642 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
643 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
644 ifsta->auth_transaction = transaction + 1;
645 mgmt->u.auth.status_code = cpu_to_le16(0);
647 memcpy(skb_put(skb, extra_len), extra, extra_len);
649 ieee80211_sta_tx(sdata, skb, encrypt);
652 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
653 struct ieee80211_if_sta *ifsta)
655 DECLARE_MAC_BUF(mac);
657 ifsta->direct_probe_tries++;
658 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
659 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
660 sdata->dev->name, print_mac(mac, ifsta->bssid));
661 ifsta->state = IEEE80211_STA_MLME_DISABLED;
665 printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
666 sdata->dev->name, print_mac(mac, ifsta->bssid),
667 ifsta->direct_probe_tries);
669 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
671 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
673 /* Direct probe is sent to broadcast address as some APs
674 * will not answer to direct packet in unassociated state.
676 ieee80211_send_probe_req(sdata, NULL,
677 ifsta->ssid, ifsta->ssid_len);
679 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
683 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
684 struct ieee80211_if_sta *ifsta)
686 DECLARE_MAC_BUF(mac);
689 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
690 printk(KERN_DEBUG "%s: authentication with AP %s"
692 sdata->dev->name, print_mac(mac, ifsta->bssid));
693 ifsta->state = IEEE80211_STA_MLME_DISABLED;
697 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
698 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
699 sdata->dev->name, print_mac(mac, ifsta->bssid));
701 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
703 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
706 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
707 struct ieee80211_supported_band *sband,
713 for (i = 0; i < bss->supp_rates_len; i++) {
714 int rate = (bss->supp_rates[i] & 0x7F) * 5;
716 for (j = 0; j < sband->n_bitrates; j++)
717 if (sband->bitrates[j].bitrate == rate) {
727 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
728 struct ieee80211_if_sta *ifsta)
730 struct ieee80211_local *local = sdata->local;
732 struct ieee80211_mgmt *mgmt;
733 u8 *pos, *ies, *ht_add_ie;
734 int i, len, count, rates_len, supp_rates_len;
736 struct ieee80211_sta_bss *bss;
738 struct ieee80211_supported_band *sband;
741 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
742 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
745 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
746 "frame\n", sdata->dev->name);
749 skb_reserve(skb, local->hw.extra_tx_headroom);
751 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
753 capab = ifsta->capab;
755 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
756 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
757 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
758 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
759 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
762 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
763 local->hw.conf.channel->center_freq,
764 ifsta->ssid, ifsta->ssid_len);
766 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
767 capab |= WLAN_CAPABILITY_PRIVACY;
771 /* get all rates supported by the device and the AP as
772 * some APs don't like getting a superset of their rates
773 * in the association request (e.g. D-Link DAP 1353 in
775 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
777 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
778 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
779 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
781 ieee80211_rx_bss_put(local, bss);
784 rates_len = sband->n_bitrates;
787 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
789 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
790 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
791 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
793 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
795 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
796 IEEE80211_STYPE_REASSOC_REQ);
797 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
798 mgmt->u.reassoc_req.listen_interval =
799 cpu_to_le16(local->hw.conf.listen_interval);
800 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
804 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
805 IEEE80211_STYPE_ASSOC_REQ);
806 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
807 mgmt->u.reassoc_req.listen_interval =
808 cpu_to_le16(local->hw.conf.listen_interval);
812 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
813 *pos++ = WLAN_EID_SSID;
814 *pos++ = ifsta->ssid_len;
815 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
817 /* add all rates which were marked to be used above */
818 supp_rates_len = rates_len;
819 if (supp_rates_len > 8)
822 len = sband->n_bitrates;
823 pos = skb_put(skb, supp_rates_len + 2);
824 *pos++ = WLAN_EID_SUPP_RATES;
825 *pos++ = supp_rates_len;
828 for (i = 0; i < sband->n_bitrates; i++) {
829 if (BIT(i) & rates) {
830 int rate = sband->bitrates[i].bitrate;
831 *pos++ = (u8) (rate / 5);
837 if (rates_len > count) {
838 pos = skb_put(skb, rates_len - count + 2);
839 *pos++ = WLAN_EID_EXT_SUPP_RATES;
840 *pos++ = rates_len - count;
842 for (i++; i < sband->n_bitrates; i++) {
843 if (BIT(i) & rates) {
844 int rate = sband->bitrates[i].bitrate;
845 *pos++ = (u8) (rate / 5);
850 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
851 /* 1. power capabilities */
852 pos = skb_put(skb, 4);
853 *pos++ = WLAN_EID_PWR_CAPABILITY;
855 *pos++ = 0; /* min tx power */
856 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
858 /* 2. supported channels */
859 /* TODO: get this in reg domain format */
860 pos = skb_put(skb, 2 * sband->n_channels + 2);
861 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
862 *pos++ = 2 * sband->n_channels;
863 for (i = 0; i < sband->n_channels; i++) {
864 *pos++ = ieee80211_frequency_to_channel(
865 sband->channels[i].center_freq);
866 *pos++ = 1; /* one channel in the subband*/
870 if (ifsta->extra_ie) {
871 pos = skb_put(skb, ifsta->extra_ie_len);
872 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
875 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
876 pos = skb_put(skb, 9);
877 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
878 *pos++ = 7; /* len */
879 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
882 *pos++ = 2; /* WME */
883 *pos++ = 0; /* WME info */
884 *pos++ = 1; /* WME ver */
888 /* wmm support is a must to HT */
889 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
890 sband->ht_info.ht_supported &&
891 (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
892 struct ieee80211_ht_addt_info *ht_add_info =
893 (struct ieee80211_ht_addt_info *)ht_add_ie;
894 u16 cap = sband->ht_info.cap;
896 u32 flags = local->hw.conf.channel->flags;
898 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
899 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
900 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
901 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
902 cap &= ~IEEE80211_HT_CAP_SGI_40;
905 case IEEE80211_HT_IE_CHA_SEC_BELOW:
906 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
907 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
908 cap &= ~IEEE80211_HT_CAP_SGI_40;
913 tmp = cpu_to_le16(cap);
914 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
915 *pos++ = WLAN_EID_HT_CAPABILITY;
916 *pos++ = sizeof(struct ieee80211_ht_cap);
917 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
918 memcpy(pos, &tmp, sizeof(u16));
920 /* TODO: needs a define here for << 2 */
921 *pos++ = sband->ht_info.ampdu_factor |
922 (sband->ht_info.ampdu_density << 2);
923 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
926 kfree(ifsta->assocreq_ies);
927 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
928 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
929 if (ifsta->assocreq_ies)
930 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
932 ieee80211_sta_tx(sdata, skb, 0);
936 static void ieee80211_send_deauth(struct ieee80211_sub_if_data *sdata,
937 struct ieee80211_if_sta *ifsta, u16 reason)
939 struct ieee80211_local *local = sdata->local;
941 struct ieee80211_mgmt *mgmt;
943 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
945 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
946 "frame\n", sdata->dev->name);
949 skb_reserve(skb, local->hw.extra_tx_headroom);
951 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
953 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
954 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
955 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
956 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
957 IEEE80211_STYPE_DEAUTH);
959 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
961 ieee80211_sta_tx(sdata, skb, 0);
965 static void ieee80211_send_disassoc(struct ieee80211_sub_if_data *sdata,
966 struct ieee80211_if_sta *ifsta, u16 reason)
968 struct ieee80211_local *local = sdata->local;
970 struct ieee80211_mgmt *mgmt;
972 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
974 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
975 "frame\n", sdata->dev->name);
978 skb_reserve(skb, local->hw.extra_tx_headroom);
980 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
982 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
983 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
984 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
985 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
986 IEEE80211_STYPE_DISASSOC);
988 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
990 ieee80211_sta_tx(sdata, skb, 0);
994 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
995 struct ieee80211_if_sta *ifsta)
997 struct ieee80211_local *local = sdata->local;
998 struct ieee80211_sta_bss *bss;
1001 int privacy_invoked;
1003 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
1006 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1007 local->hw.conf.channel->center_freq,
1008 ifsta->ssid, ifsta->ssid_len);
1012 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
1013 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
1014 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
1016 ieee80211_rx_bss_put(local, bss);
1018 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
1025 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
1026 struct ieee80211_if_sta *ifsta)
1028 DECLARE_MAC_BUF(mac);
1030 ifsta->assoc_tries++;
1031 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
1032 printk(KERN_DEBUG "%s: association with AP %s"
1034 sdata->dev->name, print_mac(mac, ifsta->bssid));
1035 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1039 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1040 printk(KERN_DEBUG "%s: associate with AP %s\n",
1041 sdata->dev->name, print_mac(mac, ifsta->bssid));
1042 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
1043 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1044 "mixed-cell disabled - abort association\n", sdata->dev->name);
1045 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1049 ieee80211_send_assoc(sdata, ifsta);
1051 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1055 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
1056 struct ieee80211_if_sta *ifsta)
1058 struct ieee80211_local *local = sdata->local;
1059 struct sta_info *sta;
1061 DECLARE_MAC_BUF(mac);
1063 /* TODO: start monitoring current AP signal quality and number of
1064 * missed beacons. Scan other channels every now and then and search
1065 * for better APs. */
1066 /* TODO: remove expired BSSes */
1068 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
1072 sta = sta_info_get(local, ifsta->bssid);
1074 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1075 sdata->dev->name, print_mac(mac, ifsta->bssid));
1079 if (time_after(jiffies,
1080 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1081 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
1082 printk(KERN_DEBUG "%s: No ProbeResp from "
1083 "current AP %s - assume out of "
1085 sdata->dev->name, print_mac(mac, ifsta->bssid));
1087 sta_info_unlink(&sta);
1089 ieee80211_send_probe_req(sdata, ifsta->bssid,
1091 local->scan_ssid_len);
1092 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1094 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1095 if (time_after(jiffies, ifsta->last_probe +
1096 IEEE80211_PROBE_INTERVAL)) {
1097 ifsta->last_probe = jiffies;
1098 ieee80211_send_probe_req(sdata, ifsta->bssid,
1107 if (disassoc && sta)
1108 sta_info_destroy(sta);
1111 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1112 ieee80211_set_associated(sdata, ifsta, 0);
1114 mod_timer(&ifsta->timer, jiffies +
1115 IEEE80211_MONITORING_INTERVAL);
1120 static void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1121 u8 *ssid, size_t ssid_len)
1123 struct ieee80211_local *local = sdata->local;
1124 struct ieee80211_supported_band *sband;
1125 struct sk_buff *skb;
1126 struct ieee80211_mgmt *mgmt;
1127 u8 *pos, *supp_rates, *esupp_rates = NULL;
1130 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1132 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1133 "request\n", sdata->dev->name);
1136 skb_reserve(skb, local->hw.extra_tx_headroom);
1138 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1139 memset(mgmt, 0, 24);
1140 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1141 IEEE80211_STYPE_PROBE_REQ);
1142 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1144 memcpy(mgmt->da, dst, ETH_ALEN);
1145 memcpy(mgmt->bssid, dst, ETH_ALEN);
1147 memset(mgmt->da, 0xff, ETH_ALEN);
1148 memset(mgmt->bssid, 0xff, ETH_ALEN);
1150 pos = skb_put(skb, 2 + ssid_len);
1151 *pos++ = WLAN_EID_SSID;
1153 memcpy(pos, ssid, ssid_len);
1155 supp_rates = skb_put(skb, 2);
1156 supp_rates[0] = WLAN_EID_SUPP_RATES;
1158 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1160 for (i = 0; i < sband->n_bitrates; i++) {
1161 struct ieee80211_rate *rate = &sband->bitrates[i];
1163 pos = skb_put(skb, 1);
1165 } else if (supp_rates[1] == 8) {
1166 esupp_rates = skb_put(skb, 3);
1167 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1169 pos = &esupp_rates[2];
1171 pos = skb_put(skb, 1);
1174 *pos = rate->bitrate / 5;
1177 ieee80211_sta_tx(sdata, skb, 0);
1181 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
1183 if (!sdata || !sdata->default_key ||
1184 sdata->default_key->conf.alg != ALG_WEP)
1190 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
1191 struct ieee80211_if_sta *ifsta)
1193 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
1194 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1195 ieee80211_associate(sdata, ifsta);
1199 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1200 struct ieee80211_if_sta *ifsta,
1201 struct ieee80211_mgmt *mgmt,
1205 struct ieee802_11_elems elems;
1207 pos = mgmt->u.auth.variable;
1208 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1209 if (!elems.challenge)
1211 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
1212 elems.challenge_len + 2, 1);
1215 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
1216 u8 dialog_token, u16 status, u16 policy,
1217 u16 buf_size, u16 timeout)
1219 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1220 struct ieee80211_local *local = sdata->local;
1221 struct sk_buff *skb;
1222 struct ieee80211_mgmt *mgmt;
1225 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1228 printk(KERN_DEBUG "%s: failed to allocate buffer "
1229 "for addba resp frame\n", sdata->dev->name);
1233 skb_reserve(skb, local->hw.extra_tx_headroom);
1234 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1235 memset(mgmt, 0, 24);
1236 memcpy(mgmt->da, da, ETH_ALEN);
1237 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1238 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1239 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1241 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1242 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1243 IEEE80211_STYPE_ACTION);
1245 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1246 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1247 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1248 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1250 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1251 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1252 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1254 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1255 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1256 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1258 ieee80211_sta_tx(sdata, skb, 0);
1263 void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata, const u8 *da,
1264 u16 tid, u8 dialog_token, u16 start_seq_num,
1265 u16 agg_size, u16 timeout)
1267 struct ieee80211_local *local = sdata->local;
1268 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1269 struct sk_buff *skb;
1270 struct ieee80211_mgmt *mgmt;
1273 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1276 printk(KERN_ERR "%s: failed to allocate buffer "
1277 "for addba request frame\n", sdata->dev->name);
1280 skb_reserve(skb, local->hw.extra_tx_headroom);
1281 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1282 memset(mgmt, 0, 24);
1283 memcpy(mgmt->da, da, ETH_ALEN);
1284 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1285 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1286 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1288 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1290 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1291 IEEE80211_STYPE_ACTION);
1293 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1295 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1296 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1298 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1299 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1300 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1301 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1303 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1305 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1306 mgmt->u.action.u.addba_req.start_seq_num =
1307 cpu_to_le16(start_seq_num << 4);
1309 ieee80211_sta_tx(sdata, skb, 0);
1312 static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
1313 struct ieee80211_mgmt *mgmt,
1316 struct ieee80211_hw *hw = &local->hw;
1317 struct ieee80211_conf *conf = &hw->conf;
1318 struct sta_info *sta;
1319 struct tid_ampdu_rx *tid_agg_rx;
1320 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1322 int ret = -EOPNOTSUPP;
1323 DECLARE_MAC_BUF(mac);
1327 sta = sta_info_get(local, mgmt->sa);
1333 /* extract session parameters from addba request frame */
1334 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1335 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1337 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1339 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1340 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1341 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1342 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1344 status = WLAN_STATUS_REQUEST_DECLINED;
1346 /* sanity check for incoming parameters:
1347 * check if configuration can support the BA policy
1348 * and if buffer size does not exceeds max value */
1349 if (((ba_policy != 1)
1350 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1351 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1352 status = WLAN_STATUS_INVALID_QOS_PARAM;
1353 #ifdef CONFIG_MAC80211_HT_DEBUG
1354 if (net_ratelimit())
1355 printk(KERN_DEBUG "AddBA Req with bad params from "
1356 "%s on tid %u. policy %d, buffer size %d\n",
1357 print_mac(mac, mgmt->sa), tid, ba_policy,
1359 #endif /* CONFIG_MAC80211_HT_DEBUG */
1362 /* determine default buffer size */
1363 if (buf_size == 0) {
1364 struct ieee80211_supported_band *sband;
1366 sband = local->hw.wiphy->bands[conf->channel->band];
1367 buf_size = IEEE80211_MIN_AMPDU_BUF;
1368 buf_size = buf_size << sband->ht_info.ampdu_factor;
1372 /* examine state machine */
1373 spin_lock_bh(&sta->lock);
1375 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1376 #ifdef CONFIG_MAC80211_HT_DEBUG
1377 if (net_ratelimit())
1378 printk(KERN_DEBUG "unexpected AddBA Req from "
1380 print_mac(mac, mgmt->sa), tid);
1381 #endif /* CONFIG_MAC80211_HT_DEBUG */
1385 /* prepare A-MPDU MLME for Rx aggregation */
1386 sta->ampdu_mlme.tid_rx[tid] =
1387 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1388 if (!sta->ampdu_mlme.tid_rx[tid]) {
1389 #ifdef CONFIG_MAC80211_HT_DEBUG
1390 if (net_ratelimit())
1391 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1397 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1398 sta_rx_agg_session_timer_expired;
1399 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1400 (unsigned long)&sta->timer_to_tid[tid];
1401 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1403 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1405 /* prepare reordering buffer */
1406 tid_agg_rx->reorder_buf =
1407 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1408 if (!tid_agg_rx->reorder_buf) {
1409 #ifdef CONFIG_MAC80211_HT_DEBUG
1410 if (net_ratelimit())
1411 printk(KERN_ERR "can not allocate reordering buffer "
1412 "to tid %d\n", tid);
1414 kfree(sta->ampdu_mlme.tid_rx[tid]);
1417 memset(tid_agg_rx->reorder_buf, 0,
1418 buf_size * sizeof(struct sk_buff *));
1420 if (local->ops->ampdu_action)
1421 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1422 sta->addr, tid, &start_seq_num);
1423 #ifdef CONFIG_MAC80211_HT_DEBUG
1424 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1425 #endif /* CONFIG_MAC80211_HT_DEBUG */
1428 kfree(tid_agg_rx->reorder_buf);
1430 sta->ampdu_mlme.tid_rx[tid] = NULL;
1434 /* change state and send addba resp */
1435 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1436 tid_agg_rx->dialog_token = dialog_token;
1437 tid_agg_rx->ssn = start_seq_num;
1438 tid_agg_rx->head_seq_num = start_seq_num;
1439 tid_agg_rx->buf_size = buf_size;
1440 tid_agg_rx->timeout = timeout;
1441 tid_agg_rx->stored_mpdu_num = 0;
1442 status = WLAN_STATUS_SUCCESS;
1444 spin_unlock_bh(&sta->lock);
1447 ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
1448 dialog_token, status, 1, buf_size, timeout);
1452 static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
1453 struct ieee80211_mgmt *mgmt,
1456 struct ieee80211_hw *hw = &local->hw;
1457 struct sta_info *sta;
1464 sta = sta_info_get(local, mgmt->sa);
1470 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1471 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1473 state = &sta->ampdu_mlme.tid_state_tx[tid];
1475 spin_lock_bh(&sta->lock);
1477 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1478 spin_unlock_bh(&sta->lock);
1479 goto addba_resp_exit;
1482 if (mgmt->u.action.u.addba_resp.dialog_token !=
1483 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1484 spin_unlock_bh(&sta->lock);
1485 #ifdef CONFIG_MAC80211_HT_DEBUG
1486 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1487 #endif /* CONFIG_MAC80211_HT_DEBUG */
1488 goto addba_resp_exit;
1491 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1492 #ifdef CONFIG_MAC80211_HT_DEBUG
1493 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1494 #endif /* CONFIG_MAC80211_HT_DEBUG */
1495 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1496 == WLAN_STATUS_SUCCESS) {
1497 *state |= HT_ADDBA_RECEIVED_MSK;
1498 sta->ampdu_mlme.addba_req_num[tid] = 0;
1500 if (*state == HT_AGG_STATE_OPERATIONAL)
1501 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1503 spin_unlock_bh(&sta->lock);
1505 sta->ampdu_mlme.addba_req_num[tid]++;
1506 /* this will allow the state check in stop_BA_session */
1507 *state = HT_AGG_STATE_OPERATIONAL;
1508 spin_unlock_bh(&sta->lock);
1509 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1510 WLAN_BACK_INITIATOR);
1517 void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata, const u8 *da, u16 tid,
1518 u16 initiator, u16 reason_code)
1520 struct ieee80211_local *local = sdata->local;
1521 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1522 struct sk_buff *skb;
1523 struct ieee80211_mgmt *mgmt;
1526 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1529 printk(KERN_ERR "%s: failed to allocate buffer "
1530 "for delba frame\n", sdata->dev->name);
1534 skb_reserve(skb, local->hw.extra_tx_headroom);
1535 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1536 memset(mgmt, 0, 24);
1537 memcpy(mgmt->da, da, ETH_ALEN);
1538 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1539 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1540 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1542 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1543 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1544 IEEE80211_STYPE_ACTION);
1546 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1548 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1549 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1550 params = (u16)(initiator << 11); /* bit 11 initiator */
1551 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1553 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1554 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1556 ieee80211_sta_tx(sdata, skb, 0);
1559 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
1561 struct ieee80211_local *local = sdata->local;
1562 struct sk_buff *skb;
1563 struct ieee80211_bar *bar;
1564 u16 bar_control = 0;
1566 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1568 printk(KERN_ERR "%s: failed to allocate buffer for "
1569 "bar frame\n", sdata->dev->name);
1572 skb_reserve(skb, local->hw.extra_tx_headroom);
1573 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1574 memset(bar, 0, sizeof(*bar));
1575 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1576 IEEE80211_STYPE_BACK_REQ);
1577 memcpy(bar->ra, ra, ETH_ALEN);
1578 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
1579 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1580 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1581 bar_control |= (u16)(tid << 12);
1582 bar->control = cpu_to_le16(bar_control);
1583 bar->start_seq_num = cpu_to_le16(ssn);
1585 ieee80211_sta_tx(sdata, skb, 0);
1588 void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
1589 u16 initiator, u16 reason)
1591 struct ieee80211_local *local = sdata->local;
1592 struct ieee80211_hw *hw = &local->hw;
1593 struct sta_info *sta;
1595 DECLARE_MAC_BUF(mac);
1599 sta = sta_info_get(local, ra);
1605 /* check if TID is in operational state */
1606 spin_lock_bh(&sta->lock);
1607 if (sta->ampdu_mlme.tid_state_rx[tid]
1608 != HT_AGG_STATE_OPERATIONAL) {
1609 spin_unlock_bh(&sta->lock);
1613 sta->ampdu_mlme.tid_state_rx[tid] =
1614 HT_AGG_STATE_REQ_STOP_BA_MSK |
1615 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1616 spin_unlock_bh(&sta->lock);
1618 /* stop HW Rx aggregation. ampdu_action existence
1619 * already verified in session init so we add the BUG_ON */
1620 BUG_ON(!local->ops->ampdu_action);
1622 #ifdef CONFIG_MAC80211_HT_DEBUG
1623 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1624 print_mac(mac, ra), tid);
1625 #endif /* CONFIG_MAC80211_HT_DEBUG */
1627 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1630 printk(KERN_DEBUG "HW problem - can not stop rx "
1631 "aggregation for tid %d\n", tid);
1633 /* shutdown timer has not expired */
1634 if (initiator != WLAN_BACK_TIMER)
1635 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1637 /* check if this is a self generated aggregation halt */
1638 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1639 ieee80211_send_delba(sdata, ra, tid, 0, reason);
1641 /* free the reordering buffer */
1642 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1643 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1644 /* release the reordered frames */
1645 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1646 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1647 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1650 /* free resources */
1651 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1652 kfree(sta->ampdu_mlme.tid_rx[tid]);
1653 sta->ampdu_mlme.tid_rx[tid] = NULL;
1654 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1660 static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
1661 struct ieee80211_mgmt *mgmt, size_t len)
1663 struct ieee80211_local *local = sdata->local;
1664 struct sta_info *sta;
1667 DECLARE_MAC_BUF(mac);
1671 sta = sta_info_get(local, mgmt->sa);
1677 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1678 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1679 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1681 #ifdef CONFIG_MAC80211_HT_DEBUG
1682 if (net_ratelimit())
1683 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1684 print_mac(mac, mgmt->sa),
1685 initiator ? "initiator" : "recipient", tid,
1686 mgmt->u.action.u.delba.reason_code);
1687 #endif /* CONFIG_MAC80211_HT_DEBUG */
1689 if (initiator == WLAN_BACK_INITIATOR)
1690 ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
1691 WLAN_BACK_INITIATOR, 0);
1692 else { /* WLAN_BACK_RECIPIENT */
1693 spin_lock_bh(&sta->lock);
1694 sta->ampdu_mlme.tid_state_tx[tid] =
1695 HT_AGG_STATE_OPERATIONAL;
1696 spin_unlock_bh(&sta->lock);
1697 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1698 WLAN_BACK_RECIPIENT);
1704 * After sending add Block Ack request we activated a timer until
1705 * add Block Ack response will arrive from the recipient.
1706 * If this timer expires sta_addba_resp_timer_expired will be executed.
1708 void sta_addba_resp_timer_expired(unsigned long data)
1710 /* not an elegant detour, but there is no choice as the timer passes
1711 * only one argument, and both sta_info and TID are needed, so init
1712 * flow in sta_info_create gives the TID as data, while the timer_to_id
1713 * array gives the sta through container_of */
1714 u16 tid = *(u8 *)data;
1715 struct sta_info *temp_sta = container_of((void *)data,
1716 struct sta_info, timer_to_tid[tid]);
1718 struct ieee80211_local *local = temp_sta->local;
1719 struct ieee80211_hw *hw = &local->hw;
1720 struct sta_info *sta;
1725 sta = sta_info_get(local, temp_sta->addr);
1731 state = &sta->ampdu_mlme.tid_state_tx[tid];
1732 /* check if the TID waits for addBA response */
1733 spin_lock_bh(&sta->lock);
1734 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1735 spin_unlock_bh(&sta->lock);
1736 *state = HT_AGG_STATE_IDLE;
1737 #ifdef CONFIG_MAC80211_HT_DEBUG
1738 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1739 "expecting addBA response there", tid);
1741 goto timer_expired_exit;
1744 #ifdef CONFIG_MAC80211_HT_DEBUG
1745 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1748 /* go through the state check in stop_BA_session */
1749 *state = HT_AGG_STATE_OPERATIONAL;
1750 spin_unlock_bh(&sta->lock);
1751 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1752 WLAN_BACK_INITIATOR);
1759 * After accepting the AddBA Request we activated a timer,
1760 * resetting it after each frame that arrives from the originator.
1761 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1763 static void sta_rx_agg_session_timer_expired(unsigned long data)
1765 /* not an elegant detour, but there is no choice as the timer passes
1766 * only one argument, and various sta_info are needed here, so init
1767 * flow in sta_info_create gives the TID as data, while the timer_to_id
1768 * array gives the sta through container_of */
1769 u8 *ptid = (u8 *)data;
1770 u8 *timer_to_id = ptid - *ptid;
1771 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1774 #ifdef CONFIG_MAC80211_HT_DEBUG
1775 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1777 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
1778 (u16)*ptid, WLAN_BACK_TIMER,
1779 WLAN_REASON_QSTA_TIMEOUT);
1782 void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
1784 struct ieee80211_local *local = sdata->local;
1787 for (i = 0; i < STA_TID_NUM; i++) {
1788 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1789 WLAN_BACK_INITIATOR);
1790 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
1791 WLAN_BACK_RECIPIENT,
1792 WLAN_REASON_QSTA_LEAVE_QBSS);
1796 static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
1797 struct ieee80211_msrment_ie *request_ie,
1798 const u8 *da, const u8 *bssid,
1801 struct ieee80211_local *local = sdata->local;
1802 struct sk_buff *skb;
1803 struct ieee80211_mgmt *msr_report;
1805 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1806 sizeof(struct ieee80211_msrment_ie));
1809 printk(KERN_ERR "%s: failed to allocate buffer for "
1810 "measurement report frame\n", sdata->dev->name);
1814 skb_reserve(skb, local->hw.extra_tx_headroom);
1815 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1816 memset(msr_report, 0, 24);
1817 memcpy(msr_report->da, da, ETH_ALEN);
1818 memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
1819 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1820 msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1821 IEEE80211_STYPE_ACTION);
1823 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1824 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1825 msr_report->u.action.u.measurement.action_code =
1826 WLAN_ACTION_SPCT_MSR_RPRT;
1827 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1829 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1830 msr_report->u.action.u.measurement.length =
1831 sizeof(struct ieee80211_msrment_ie);
1833 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1834 sizeof(struct ieee80211_msrment_ie));
1835 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1836 msr_report->u.action.u.measurement.msr_elem.mode |=
1837 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1838 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1840 ieee80211_sta_tx(sdata, skb, 0);
1843 static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
1844 struct ieee80211_mgmt *mgmt,
1848 * Ignoring measurement request is spec violation.
1849 * Mandatory measurements must be reported optional
1850 * measurements might be refused or reported incapable
1851 * For now just refuse
1852 * TODO: Answer basic measurement as unmeasured
1854 ieee80211_send_refuse_measurement_request(sdata,
1855 &mgmt->u.action.u.measurement.msr_elem,
1856 mgmt->sa, mgmt->bssid,
1857 mgmt->u.action.u.measurement.dialog_token);
1861 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1862 struct ieee80211_if_sta *ifsta,
1863 struct ieee80211_mgmt *mgmt,
1866 u16 auth_alg, auth_transaction, status_code;
1867 DECLARE_MAC_BUF(mac);
1869 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
1870 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
1876 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1877 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1880 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1881 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1884 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1885 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1886 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1888 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1890 * IEEE 802.11 standard does not require authentication in IBSS
1891 * networks and most implementations do not seem to use it.
1892 * However, try to reply to authentication attempts if someone
1893 * has actually implemented this.
1895 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1897 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
1900 if (auth_alg != ifsta->auth_alg ||
1901 auth_transaction != ifsta->auth_transaction)
1904 if (status_code != WLAN_STATUS_SUCCESS) {
1905 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1907 const int num_algs = ARRAY_SIZE(algs);
1909 algs[0] = algs[1] = algs[2] = 0xff;
1910 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1911 algs[0] = WLAN_AUTH_OPEN;
1912 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1913 algs[1] = WLAN_AUTH_SHARED_KEY;
1914 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1915 algs[2] = WLAN_AUTH_LEAP;
1916 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1918 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1922 for (i = 0; i < num_algs; i++) {
1924 if (pos >= num_algs)
1926 if (algs[pos] == ifsta->auth_alg ||
1929 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1930 !ieee80211_sta_wep_configured(sdata))
1932 ifsta->auth_alg = algs[pos];
1939 switch (ifsta->auth_alg) {
1940 case WLAN_AUTH_OPEN:
1941 case WLAN_AUTH_LEAP:
1942 ieee80211_auth_completed(sdata, ifsta);
1944 case WLAN_AUTH_SHARED_KEY:
1945 if (ifsta->auth_transaction == 4)
1946 ieee80211_auth_completed(sdata, ifsta);
1948 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
1954 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1955 struct ieee80211_if_sta *ifsta,
1956 struct ieee80211_mgmt *mgmt,
1960 DECLARE_MAC_BUF(mac);
1965 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1968 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1970 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1971 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
1973 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1974 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1975 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1976 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1977 mod_timer(&ifsta->timer, jiffies +
1978 IEEE80211_RETRY_AUTH_INTERVAL);
1981 ieee80211_set_disassoc(sdata, ifsta, 1);
1982 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1986 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1987 struct ieee80211_if_sta *ifsta,
1988 struct ieee80211_mgmt *mgmt,
1992 DECLARE_MAC_BUF(mac);
1997 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
2000 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2002 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
2003 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
2005 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
2006 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
2007 mod_timer(&ifsta->timer, jiffies +
2008 IEEE80211_RETRY_AUTH_INTERVAL);
2011 ieee80211_set_disassoc(sdata, ifsta, 0);
2015 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2016 struct ieee80211_if_sta *ifsta,
2017 struct ieee80211_mgmt *mgmt,
2021 struct ieee80211_local *local = sdata->local;
2022 struct ieee80211_supported_band *sband;
2023 struct sta_info *sta;
2024 u64 rates, basic_rates;
2025 u16 capab_info, status_code, aid;
2026 struct ieee802_11_elems elems;
2027 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
2030 DECLARE_MAC_BUF(mac);
2031 bool have_higher_than_11mbit = false;
2033 /* AssocResp and ReassocResp have identical structure, so process both
2034 * of them in this function. */
2036 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
2042 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
2045 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2046 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2047 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2049 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
2050 "status=%d aid=%d)\n",
2051 sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
2052 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2054 if (status_code != WLAN_STATUS_SUCCESS) {
2055 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2056 sdata->dev->name, status_code);
2057 /* if this was a reassociation, ensure we try a "full"
2058 * association next time. This works around some broken APs
2059 * which do not correctly reject reassociation requests. */
2060 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2064 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2065 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2066 "set\n", sdata->dev->name, aid);
2067 aid &= ~(BIT(15) | BIT(14));
2069 pos = mgmt->u.assoc_resp.variable;
2070 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2072 if (!elems.supp_rates) {
2073 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2078 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
2080 ifsta->ap_capab = capab_info;
2082 kfree(ifsta->assocresp_ies);
2083 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
2084 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
2085 if (ifsta->assocresp_ies)
2086 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2090 /* Add STA entry for the AP */
2091 sta = sta_info_get(local, ifsta->bssid);
2093 struct ieee80211_sta_bss *bss;
2096 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2098 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2099 " the AP\n", sdata->dev->name);
2103 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
2104 local->hw.conf.channel->center_freq,
2105 ifsta->ssid, ifsta->ssid_len);
2107 sta->last_signal = bss->signal;
2108 sta->last_qual = bss->qual;
2109 sta->last_noise = bss->noise;
2110 ieee80211_rx_bss_put(local, bss);
2113 err = sta_info_insert(sta);
2115 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2116 " the AP (error %d)\n", sdata->dev->name, err);
2120 /* update new sta with its last rx activity */
2121 sta->last_rx = jiffies;
2125 * FIXME: Do we really need to update the sta_info's information here?
2126 * We already know about the AP (we found it in our list) so it
2127 * should already be filled with the right info, no?
2128 * As is stands, all this is racy because typically we assume
2129 * the information that is filled in here (except flags) doesn't
2130 * change while a STA structure is alive. As such, it should move
2131 * to between the sta_info_alloc() and sta_info_insert() above.
2134 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2135 WLAN_STA_AUTHORIZED);
2139 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2141 for (i = 0; i < elems.supp_rates_len; i++) {
2142 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2145 have_higher_than_11mbit = true;
2147 for (j = 0; j < sband->n_bitrates; j++) {
2148 if (sband->bitrates[j].bitrate == rate)
2150 if (elems.supp_rates[i] & 0x80)
2151 basic_rates |= BIT(j);
2155 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2156 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2159 have_higher_than_11mbit = true;
2161 for (j = 0; j < sband->n_bitrates; j++) {
2162 if (sband->bitrates[j].bitrate == rate)
2164 if (elems.ext_supp_rates[i] & 0x80)
2165 basic_rates |= BIT(j);
2169 sta->supp_rates[local->hw.conf.channel->band] = rates;
2170 sdata->basic_rates = basic_rates;
2172 /* cf. IEEE 802.11 9.2.12 */
2173 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2174 have_higher_than_11mbit)
2175 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2177 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2179 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2180 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2181 struct ieee80211_ht_bss_info bss_info;
2182 ieee80211_ht_cap_ie_to_ht_info(
2183 (struct ieee80211_ht_cap *)
2184 elems.ht_cap_elem, &sta->ht_info);
2185 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2186 (struct ieee80211_ht_addt_info *)
2187 elems.ht_info_elem, &bss_info);
2188 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2191 rate_control_rate_init(sta, local);
2193 if (elems.wmm_param) {
2194 set_sta_flags(sta, WLAN_STA_WME);
2196 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2197 elems.wmm_param_len);
2201 /* set AID and assoc capability,
2202 * ieee80211_set_associated() will tell the driver */
2203 bss_conf->aid = aid;
2204 bss_conf->assoc_capability = capab_info;
2205 ieee80211_set_associated(sdata, ifsta, 1);
2207 ieee80211_associated(sdata, ifsta);
2211 /* Caller must hold local->sta_bss_lock */
2212 static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
2213 struct ieee80211_sta_bss *bss)
2217 if (bss_mesh_cfg(bss))
2218 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2219 bss_mesh_id_len(bss));
2221 hash_idx = STA_HASH(bss->bssid);
2223 bss->hnext = local->sta_bss_hash[hash_idx];
2224 local->sta_bss_hash[hash_idx] = bss;
2228 /* Caller must hold local->sta_bss_lock */
2229 static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
2230 struct ieee80211_sta_bss *bss)
2232 struct ieee80211_sta_bss *b, *prev = NULL;
2233 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2237 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2240 prev->hnext = bss->hnext;
2249 static struct ieee80211_sta_bss *
2250 ieee80211_rx_bss_add(struct ieee80211_sub_if_data *sdata, u8 *bssid, int freq,
2251 u8 *ssid, u8 ssid_len)
2253 struct ieee80211_local *local = sdata->local;
2254 struct ieee80211_sta_bss *bss;
2256 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2259 atomic_inc(&bss->users);
2260 atomic_inc(&bss->users);
2261 memcpy(bss->bssid, bssid, ETH_ALEN);
2263 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2264 memcpy(bss->ssid, ssid, ssid_len);
2265 bss->ssid_len = ssid_len;
2268 spin_lock_bh(&local->sta_bss_lock);
2269 /* TODO: order by RSSI? */
2270 list_add_tail(&bss->list, &local->sta_bss_list);
2271 __ieee80211_rx_bss_hash_add(local, bss);
2272 spin_unlock_bh(&local->sta_bss_lock);
2276 static struct ieee80211_sta_bss *
2277 ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
2278 u8 *ssid, u8 ssid_len)
2280 struct ieee80211_sta_bss *bss;
2282 spin_lock_bh(&local->sta_bss_lock);
2283 bss = local->sta_bss_hash[STA_HASH(bssid)];
2285 if (!bss_mesh_cfg(bss) &&
2286 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2287 bss->freq == freq &&
2288 bss->ssid_len == ssid_len &&
2289 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2290 atomic_inc(&bss->users);
2295 spin_unlock_bh(&local->sta_bss_lock);
2299 #ifdef CONFIG_MAC80211_MESH
2300 static struct ieee80211_sta_bss *
2301 ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
2302 u8 *mesh_cfg, int freq)
2304 struct ieee80211_sta_bss *bss;
2306 spin_lock_bh(&local->sta_bss_lock);
2307 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2309 if (bss_mesh_cfg(bss) &&
2310 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2311 bss->freq == freq &&
2312 mesh_id_len == bss->mesh_id_len &&
2313 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2315 atomic_inc(&bss->users);
2320 spin_unlock_bh(&local->sta_bss_lock);
2324 static struct ieee80211_sta_bss *
2325 ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
2326 u8 *mesh_cfg, int mesh_config_len, int freq)
2328 struct ieee80211_sta_bss *bss;
2330 if (mesh_config_len != MESH_CFG_LEN)
2333 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2337 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2338 if (!bss->mesh_cfg) {
2343 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2344 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2345 if (!bss->mesh_id) {
2346 kfree(bss->mesh_cfg);
2350 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2353 atomic_inc(&bss->users);
2354 atomic_inc(&bss->users);
2355 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2356 bss->mesh_id_len = mesh_id_len;
2358 spin_lock_bh(&local->sta_bss_lock);
2359 /* TODO: order by RSSI? */
2360 list_add_tail(&bss->list, &local->sta_bss_list);
2361 __ieee80211_rx_bss_hash_add(local, bss);
2362 spin_unlock_bh(&local->sta_bss_lock);
2367 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2370 kfree(bss_mesh_id(bss));
2371 kfree(bss_mesh_cfg(bss));
2376 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
2377 struct ieee80211_sta_bss *bss)
2380 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2385 __ieee80211_rx_bss_hash_del(local, bss);
2386 list_del(&bss->list);
2387 spin_unlock_bh(&local->sta_bss_lock);
2388 ieee80211_rx_bss_free(bss);
2392 void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
2394 spin_lock_init(&local->sta_bss_lock);
2395 INIT_LIST_HEAD(&local->sta_bss_list);
2399 void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
2401 struct ieee80211_sta_bss *bss, *tmp;
2403 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2404 ieee80211_rx_bss_put(local, bss);
2408 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
2409 struct ieee80211_if_sta *ifsta,
2410 struct ieee80211_sta_bss *bss)
2412 struct ieee80211_local *local = sdata->local;
2413 int res, rates, i, j;
2414 struct sk_buff *skb;
2415 struct ieee80211_mgmt *mgmt;
2417 struct ieee80211_supported_band *sband;
2418 union iwreq_data wrqu;
2420 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2422 /* Remove possible STA entries from other IBSS networks. */
2423 sta_info_flush_delayed(sdata);
2425 if (local->ops->reset_tsf) {
2426 /* Reset own TSF to allow time synchronization work. */
2427 local->ops->reset_tsf(local_to_hw(local));
2429 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2430 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2434 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2436 sdata->drop_unencrypted = bss->capability &
2437 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2439 res = ieee80211_set_freq(sdata, bss->freq);
2444 /* Build IBSS probe response */
2445 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2447 skb_reserve(skb, local->hw.extra_tx_headroom);
2449 mgmt = (struct ieee80211_mgmt *)
2450 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2451 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2452 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2453 IEEE80211_STYPE_PROBE_RESP);
2454 memset(mgmt->da, 0xff, ETH_ALEN);
2455 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
2456 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2457 mgmt->u.beacon.beacon_int =
2458 cpu_to_le16(local->hw.conf.beacon_int);
2459 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
2460 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2462 pos = skb_put(skb, 2 + ifsta->ssid_len);
2463 *pos++ = WLAN_EID_SSID;
2464 *pos++ = ifsta->ssid_len;
2465 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2467 rates = bss->supp_rates_len;
2470 pos = skb_put(skb, 2 + rates);
2471 *pos++ = WLAN_EID_SUPP_RATES;
2473 memcpy(pos, bss->supp_rates, rates);
2475 if (bss->band == IEEE80211_BAND_2GHZ) {
2476 pos = skb_put(skb, 2 + 1);
2477 *pos++ = WLAN_EID_DS_PARAMS;
2479 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2482 pos = skb_put(skb, 2 + 2);
2483 *pos++ = WLAN_EID_IBSS_PARAMS;
2485 /* FIX: set ATIM window based on scan results */
2489 if (bss->supp_rates_len > 8) {
2490 rates = bss->supp_rates_len - 8;
2491 pos = skb_put(skb, 2 + rates);
2492 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2494 memcpy(pos, &bss->supp_rates[8], rates);
2497 ifsta->probe_resp = skb;
2499 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2503 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2504 for (i = 0; i < bss->supp_rates_len; i++) {
2505 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2506 for (j = 0; j < sband->n_bitrates; j++)
2507 if (sband->bitrates[j].bitrate == bitrate)
2510 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2512 ieee80211_sta_def_wmm_params(sdata, bss, 1);
2514 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
2515 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2517 memset(&wrqu, 0, sizeof(wrqu));
2518 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2519 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
2524 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2525 struct ieee802_11_elems *elems,
2526 enum ieee80211_band band)
2528 struct ieee80211_supported_band *sband;
2529 struct ieee80211_rate *bitrates;
2533 sband = local->hw.wiphy->bands[band];
2537 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2540 bitrates = sband->bitrates;
2541 num_rates = sband->n_bitrates;
2543 for (i = 0; i < elems->supp_rates_len +
2544 elems->ext_supp_rates_len; i++) {
2547 if (i < elems->supp_rates_len)
2548 rate = elems->supp_rates[i];
2549 else if (elems->ext_supp_rates)
2550 rate = elems->ext_supp_rates
2551 [i - elems->supp_rates_len];
2552 own_rate = 5 * (rate & 0x7f);
2553 for (j = 0; j < num_rates; j++)
2554 if (bitrates[j].bitrate == own_rate)
2555 supp_rates |= BIT(j);
2560 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
2561 enum ieee80211_band band)
2563 struct ieee80211_supported_band *sband;
2564 struct ieee80211_rate *bitrates;
2565 u64 mandatory_rates;
2566 enum ieee80211_rate_flags mandatory_flag;
2569 sband = local->hw.wiphy->bands[band];
2572 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2575 if (band == IEEE80211_BAND_2GHZ)
2576 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
2578 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
2580 bitrates = sband->bitrates;
2581 mandatory_rates = 0;
2582 for (i = 0; i < sband->n_bitrates; i++)
2583 if (bitrates[i].flags & mandatory_flag)
2584 mandatory_rates |= BIT(i);
2585 return mandatory_rates;
2588 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2589 struct ieee80211_mgmt *mgmt,
2591 struct ieee80211_rx_status *rx_status,
2592 struct ieee802_11_elems *elems)
2594 struct ieee80211_local *local = sdata->local;
2596 struct ieee80211_sta_bss *bss;
2597 struct sta_info *sta;
2598 struct ieee80211_channel *channel;
2599 u64 beacon_timestamp, rx_timestamp;
2601 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
2602 enum ieee80211_band band = rx_status->band;
2603 DECLARE_MAC_BUF(mac);
2604 DECLARE_MAC_BUF(mac2);
2606 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2608 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2609 elems->mesh_config && mesh_matches_local(elems, sdata)) {
2610 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2612 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
2613 mesh_peer_accepts_plinks(elems));
2618 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
2619 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2621 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2623 sta = sta_info_get(local, mgmt->sa);
2627 prev_rates = sta->supp_rates[band];
2628 /* make sure mandatory rates are always added */
2629 sta->supp_rates[band] = supp_rates |
2630 ieee80211_sta_get_mandatory_rates(local, band);
2632 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2633 if (sta->supp_rates[band] != prev_rates)
2634 printk(KERN_DEBUG "%s: updated supp_rates set "
2635 "for %s based on beacon info (0x%llx | "
2636 "0x%llx -> 0x%llx)\n",
2637 sdata->dev->name, print_mac(mac, sta->addr),
2638 (unsigned long long) prev_rates,
2639 (unsigned long long) supp_rates,
2640 (unsigned long long) sta->supp_rates[band]);
2643 ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
2644 mgmt->sa, supp_rates);
2650 if (elems->ds_params && elems->ds_params_len == 1)
2651 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
2653 freq = rx_status->freq;
2655 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2657 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2660 #ifdef CONFIG_MAC80211_MESH
2661 if (elems->mesh_config)
2662 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
2663 elems->mesh_id_len, elems->mesh_config, freq);
2666 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
2667 elems->ssid, elems->ssid_len);
2669 #ifdef CONFIG_MAC80211_MESH
2670 if (elems->mesh_config)
2671 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
2672 elems->mesh_id_len, elems->mesh_config,
2673 elems->mesh_config_len, freq);
2676 bss = ieee80211_rx_bss_add(sdata, mgmt->bssid, freq,
2677 elems->ssid, elems->ssid_len);
2682 /* TODO: order by RSSI? */
2683 spin_lock_bh(&local->sta_bss_lock);
2684 list_move_tail(&bss->list, &local->sta_bss_list);
2685 spin_unlock_bh(&local->sta_bss_lock);
2689 /* save the ERP value so that it is available at association time */
2690 if (elems->erp_info && elems->erp_info_len >= 1) {
2691 bss->erp_value = elems->erp_info[0];
2692 bss->has_erp_value = 1;
2695 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2696 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2699 struct ieee80211_tim_ie *tim_ie =
2700 (struct ieee80211_tim_ie *)elems->tim;
2701 bss->dtim_period = tim_ie->dtim_period;
2704 /* set default value for buggy APs */
2705 if (!elems->tim || bss->dtim_period == 0)
2706 bss->dtim_period = 1;
2708 bss->supp_rates_len = 0;
2709 if (elems->supp_rates) {
2710 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2711 if (clen > elems->supp_rates_len)
2712 clen = elems->supp_rates_len;
2713 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
2715 bss->supp_rates_len += clen;
2717 if (elems->ext_supp_rates) {
2718 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2719 if (clen > elems->ext_supp_rates_len)
2720 clen = elems->ext_supp_rates_len;
2721 memcpy(&bss->supp_rates[bss->supp_rates_len],
2722 elems->ext_supp_rates, clen);
2723 bss->supp_rates_len += clen;
2728 bss->timestamp = beacon_timestamp;
2729 bss->last_update = jiffies;
2730 bss->signal = rx_status->signal;
2731 bss->noise = rx_status->noise;
2732 bss->qual = rx_status->qual;
2734 bss->last_probe_resp = jiffies;
2736 * In STA mode, the remaining parameters should not be overridden
2737 * by beacons because they're not necessarily accurate there.
2739 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2740 bss->last_probe_resp && beacon) {
2741 ieee80211_rx_bss_put(local, bss);
2745 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
2747 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
2750 memcpy(bss->ies, elems->ie_start, elems->total_len);
2751 bss->ies_len = elems->total_len;
2755 bss->wmm_used = elems->wmm_param || elems->wmm_info;
2757 /* check if we need to merge IBSS */
2758 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2759 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2760 bss->capability & WLAN_CAPABILITY_IBSS &&
2761 bss->freq == local->oper_channel->center_freq &&
2762 elems->ssid_len == sdata->u.sta.ssid_len &&
2763 memcmp(elems->ssid, sdata->u.sta.ssid,
2764 sdata->u.sta.ssid_len) == 0) {
2765 if (rx_status->flag & RX_FLAG_TSFT) {
2766 /* in order for correct IBSS merging we need mactime
2768 * since mactime is defined as the time the first data
2769 * symbol of the frame hits the PHY, and the timestamp
2770 * of the beacon is defined as "the time that the data
2771 * symbol containing the first bit of the timestamp is
2772 * transmitted to the PHY plus the transmitting STA’s
2773 * delays through its local PHY from the MAC-PHY
2774 * interface to its interface with the WM"
2775 * (802.11 11.1.2) - equals the time this bit arrives at
2776 * the receiver - we have to take into account the
2777 * offset between the two.
2778 * e.g: at 1 MBit that means mactime is 192 usec earlier
2779 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2781 int rate = local->hw.wiphy->bands[band]->
2782 bitrates[rx_status->rate_idx].bitrate;
2783 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2784 } else if (local && local->ops && local->ops->get_tsf)
2785 /* second best option: get current TSF */
2786 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2788 /* can't merge without knowing the TSF */
2789 rx_timestamp = -1LLU;
2790 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2791 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2792 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2793 print_mac(mac, mgmt->sa),
2794 print_mac(mac2, mgmt->bssid),
2795 (unsigned long long)rx_timestamp,
2796 (unsigned long long)beacon_timestamp,
2797 (unsigned long long)(rx_timestamp - beacon_timestamp),
2799 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2800 if (beacon_timestamp > rx_timestamp) {
2801 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2802 printk(KERN_DEBUG "%s: beacon TSF higher than "
2803 "local TSF - IBSS merge with BSSID %s\n",
2804 sdata->dev->name, print_mac(mac, mgmt->bssid));
2806 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
2807 ieee80211_ibss_add_sta(sdata, NULL,
2808 mgmt->bssid, mgmt->sa,
2813 ieee80211_rx_bss_put(local, bss);
2817 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2818 struct ieee80211_mgmt *mgmt,
2820 struct ieee80211_rx_status *rx_status)
2823 struct ieee802_11_elems elems;
2824 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2826 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
2827 return; /* ignore ProbeResp to foreign address */
2829 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2833 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2836 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2838 /* direct probe may be part of the association flow */
2839 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
2841 printk(KERN_DEBUG "%s direct probe responded\n",
2843 ieee80211_authenticate(sdata, ifsta);
2848 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2849 struct ieee80211_mgmt *mgmt,
2851 struct ieee80211_rx_status *rx_status)
2853 struct ieee80211_if_sta *ifsta;
2855 struct ieee802_11_elems elems;
2856 struct ieee80211_local *local = sdata->local;
2857 struct ieee80211_conf *conf = &local->hw.conf;
2860 /* Process beacon from the current BSS */
2861 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2865 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2867 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2869 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2871 ifsta = &sdata->u.sta;
2873 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2874 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2877 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2878 elems.wmm_param_len);
2880 /* Do not send changes to driver if we are scanning. This removes
2881 * requirement that driver's bss_info_changed function needs to be
2883 if (local->sta_sw_scanning || local->sta_hw_scanning)
2886 if (elems.erp_info && elems.erp_info_len >= 1)
2887 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2889 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2890 changed |= ieee80211_handle_protect_preamb(sdata, false,
2891 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2894 if (elems.ht_cap_elem && elems.ht_info_elem &&
2895 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2896 struct ieee80211_ht_bss_info bss_info;
2898 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2899 (struct ieee80211_ht_addt_info *)
2900 elems.ht_info_elem, &bss_info);
2901 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2905 ieee80211_bss_info_change_notify(sdata, changed);
2909 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
2910 struct ieee80211_if_sta *ifsta,
2911 struct ieee80211_mgmt *mgmt,
2913 struct ieee80211_rx_status *rx_status)
2915 struct ieee80211_local *local = sdata->local;
2917 struct sk_buff *skb;
2918 struct ieee80211_mgmt *resp;
2920 DECLARE_MAC_BUF(mac);
2921 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2922 DECLARE_MAC_BUF(mac2);
2923 DECLARE_MAC_BUF(mac3);
2926 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2927 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
2928 len < 24 + 2 || !ifsta->probe_resp)
2931 if (local->ops->tx_last_beacon)
2932 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2936 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2937 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2938 "%s (tx_last_beacon=%d)\n",
2939 sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2940 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2941 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2943 if (!tx_last_beacon)
2946 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2947 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2950 end = ((u8 *) mgmt) + len;
2951 pos = mgmt->u.probe_req.variable;
2952 if (pos[0] != WLAN_EID_SSID ||
2953 pos + 2 + pos[1] > end) {
2954 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2955 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2957 sdata->dev->name, print_mac(mac, mgmt->sa));
2962 (pos[1] != ifsta->ssid_len ||
2963 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2964 /* Ignore ProbeReq for foreign SSID */
2968 /* Reply with ProbeResp */
2969 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2973 resp = (struct ieee80211_mgmt *) skb->data;
2974 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2975 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2976 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2977 sdata->dev->name, print_mac(mac, resp->da));
2978 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2979 ieee80211_sta_tx(sdata, skb, 0);
2982 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
2983 struct ieee80211_if_sta *ifsta,
2984 struct ieee80211_mgmt *mgmt,
2986 struct ieee80211_rx_status *rx_status)
2988 struct ieee80211_local *local = sdata->local;
2990 if (len < IEEE80211_MIN_ACTION_SIZE)
2993 switch (mgmt->u.action.category) {
2994 case WLAN_CATEGORY_SPECTRUM_MGMT:
2995 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2997 switch (mgmt->u.action.u.chan_switch.action_code) {
2998 case WLAN_ACTION_SPCT_MSR_REQ:
2999 if (len < (IEEE80211_MIN_ACTION_SIZE +
3000 sizeof(mgmt->u.action.u.measurement)))
3002 ieee80211_sta_process_measurement_req(sdata, mgmt, len);
3006 case WLAN_CATEGORY_BACK:
3007 switch (mgmt->u.action.u.addba_req.action_code) {
3008 case WLAN_ACTION_ADDBA_REQ:
3009 if (len < (IEEE80211_MIN_ACTION_SIZE +
3010 sizeof(mgmt->u.action.u.addba_req)))
3012 ieee80211_sta_process_addba_request(local, mgmt, len);
3014 case WLAN_ACTION_ADDBA_RESP:
3015 if (len < (IEEE80211_MIN_ACTION_SIZE +
3016 sizeof(mgmt->u.action.u.addba_resp)))
3018 ieee80211_sta_process_addba_resp(local, mgmt, len);
3020 case WLAN_ACTION_DELBA:
3021 if (len < (IEEE80211_MIN_ACTION_SIZE +
3022 sizeof(mgmt->u.action.u.delba)))
3024 ieee80211_sta_process_delba(sdata, mgmt, len);
3028 case PLINK_CATEGORY:
3029 if (ieee80211_vif_is_mesh(&sdata->vif))
3030 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
3032 case MESH_PATH_SEL_CATEGORY:
3033 if (ieee80211_vif_is_mesh(&sdata->vif))
3034 mesh_rx_path_sel_frame(sdata, mgmt, len);
3039 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
3040 struct ieee80211_rx_status *rx_status)
3042 struct ieee80211_local *local = sdata->local;
3043 struct ieee80211_if_sta *ifsta;
3044 struct ieee80211_mgmt *mgmt;
3050 ifsta = &sdata->u.sta;
3052 mgmt = (struct ieee80211_mgmt *) skb->data;
3053 fc = le16_to_cpu(mgmt->frame_control);
3055 switch (fc & IEEE80211_FCTL_STYPE) {
3056 case IEEE80211_STYPE_PROBE_REQ:
3057 case IEEE80211_STYPE_PROBE_RESP:
3058 case IEEE80211_STYPE_BEACON:
3059 case IEEE80211_STYPE_ACTION:
3060 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3061 case IEEE80211_STYPE_AUTH:
3062 case IEEE80211_STYPE_ASSOC_RESP:
3063 case IEEE80211_STYPE_REASSOC_RESP:
3064 case IEEE80211_STYPE_DEAUTH:
3065 case IEEE80211_STYPE_DISASSOC:
3066 skb_queue_tail(&ifsta->skb_queue, skb);
3067 queue_work(local->hw.workqueue, &ifsta->work);
3075 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3076 struct sk_buff *skb)
3078 struct ieee80211_rx_status *rx_status;
3079 struct ieee80211_if_sta *ifsta;
3080 struct ieee80211_mgmt *mgmt;
3083 ifsta = &sdata->u.sta;
3085 rx_status = (struct ieee80211_rx_status *) skb->cb;
3086 mgmt = (struct ieee80211_mgmt *) skb->data;
3087 fc = le16_to_cpu(mgmt->frame_control);
3089 switch (fc & IEEE80211_FCTL_STYPE) {
3090 case IEEE80211_STYPE_PROBE_REQ:
3091 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
3094 case IEEE80211_STYPE_PROBE_RESP:
3095 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
3097 case IEEE80211_STYPE_BEACON:
3098 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
3100 case IEEE80211_STYPE_AUTH:
3101 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
3103 case IEEE80211_STYPE_ASSOC_RESP:
3104 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
3106 case IEEE80211_STYPE_REASSOC_RESP:
3107 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
3109 case IEEE80211_STYPE_DEAUTH:
3110 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
3112 case IEEE80211_STYPE_DISASSOC:
3113 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
3115 case IEEE80211_STYPE_ACTION:
3116 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
3125 ieee80211_sta_rx_scan(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
3126 struct ieee80211_rx_status *rx_status)
3128 struct ieee80211_mgmt *mgmt;
3132 return RX_DROP_UNUSABLE;
3134 mgmt = (struct ieee80211_mgmt *) skb->data;
3135 fc = mgmt->frame_control;
3137 if (ieee80211_is_ctl(fc))
3141 return RX_DROP_MONITOR;
3143 if (ieee80211_is_probe_resp(fc)) {
3144 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
3149 if (ieee80211_is_beacon(fc)) {
3150 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
3159 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
3161 struct ieee80211_local *local = sdata->local;
3163 struct sta_info *sta;
3167 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3168 if (sta->sdata == sdata &&
3169 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3182 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
3184 struct ieee80211_local *local = sdata->local;
3185 struct sta_info *sta, *tmp;
3186 LIST_HEAD(tmp_list);
3187 DECLARE_MAC_BUF(mac);
3188 unsigned long flags;
3190 spin_lock_irqsave(&local->sta_lock, flags);
3191 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3192 if (time_after(jiffies, sta->last_rx + exp_time)) {
3193 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3194 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3195 sdata->dev->name, print_mac(mac, sta->addr));
3197 __sta_info_unlink(&sta);
3199 list_add(&sta->list, &tmp_list);
3201 spin_unlock_irqrestore(&local->sta_lock, flags);
3203 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3204 sta_info_destroy(sta);
3208 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
3209 struct ieee80211_if_sta *ifsta)
3211 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3213 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
3214 if (ieee80211_sta_active_ibss(sdata))
3217 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3218 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
3219 ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
3223 #ifdef CONFIG_MAC80211_MESH
3224 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
3225 struct ieee80211_if_sta *ifsta)
3229 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3230 mesh_path_expire(sdata);
3232 free_plinks = mesh_plink_availables(sdata);
3233 if (free_plinks != sdata->u.sta.accepting_plinks)
3234 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3236 mod_timer(&ifsta->timer, jiffies +
3237 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3241 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
3243 struct ieee80211_if_sta *ifsta;
3244 ifsta = &sdata->u.sta;
3245 ifsta->state = IEEE80211_STA_MLME_MESH_UP;
3246 ieee80211_sta_timer((unsigned long)sdata);
3247 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3252 void ieee80211_sta_timer(unsigned long data)
3254 struct ieee80211_sub_if_data *sdata =
3255 (struct ieee80211_sub_if_data *) data;
3256 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3257 struct ieee80211_local *local = sdata->local;
3259 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3260 queue_work(local->hw.workqueue, &ifsta->work);
3263 void ieee80211_sta_work(struct work_struct *work)
3265 struct ieee80211_sub_if_data *sdata =
3266 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3267 struct ieee80211_local *local = sdata->local;
3268 struct ieee80211_if_sta *ifsta;
3269 struct sk_buff *skb;
3271 if (!netif_running(sdata->dev))
3274 if (local->sta_sw_scanning || local->sta_hw_scanning)
3277 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3278 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3279 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
3281 ifsta = &sdata->u.sta;
3283 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3284 ieee80211_sta_rx_queued_mgmt(sdata, skb);
3286 #ifdef CONFIG_MAC80211_MESH
3287 if (ifsta->preq_queue_len &&
3289 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3290 mesh_path_start_discovery(sdata);
3293 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
3294 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
3295 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
3296 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3297 if (ifsta->scan_ssid_len)
3298 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
3300 ieee80211_sta_start_scan(sdata, NULL, 0);
3304 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3305 if (ieee80211_sta_config_auth(sdata, ifsta))
3307 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3308 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3311 switch (ifsta->state) {
3312 case IEEE80211_STA_MLME_DISABLED:
3314 case IEEE80211_STA_MLME_DIRECT_PROBE:
3315 ieee80211_direct_probe(sdata, ifsta);
3317 case IEEE80211_STA_MLME_AUTHENTICATE:
3318 ieee80211_authenticate(sdata, ifsta);
3320 case IEEE80211_STA_MLME_ASSOCIATE:
3321 ieee80211_associate(sdata, ifsta);
3323 case IEEE80211_STA_MLME_ASSOCIATED:
3324 ieee80211_associated(sdata, ifsta);
3326 case IEEE80211_STA_MLME_IBSS_SEARCH:
3327 ieee80211_sta_find_ibss(sdata, ifsta);
3329 case IEEE80211_STA_MLME_IBSS_JOINED:
3330 ieee80211_sta_merge_ibss(sdata, ifsta);
3332 #ifdef CONFIG_MAC80211_MESH
3333 case IEEE80211_STA_MLME_MESH_UP:
3334 ieee80211_mesh_housekeeping(sdata, ifsta);
3342 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
3343 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3344 "mixed-cell disabled - disassociate\n", sdata->dev->name);
3346 ieee80211_send_disassoc(sdata, ifsta, WLAN_REASON_UNSPECIFIED);
3347 ieee80211_set_disassoc(sdata, ifsta, 0);
3352 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
3353 struct ieee80211_if_sta *ifsta)
3355 struct ieee80211_local *local = sdata->local;
3357 if (local->ops->reset_tsf) {
3358 /* Reset own TSF to allow time synchronization work. */
3359 local->ops->reset_tsf(local_to_hw(local));
3362 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3365 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3366 ifsta->auth_alg = WLAN_AUTH_OPEN;
3367 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3368 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3369 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3370 ifsta->auth_alg = WLAN_AUTH_LEAP;
3372 ifsta->auth_alg = WLAN_AUTH_OPEN;
3373 ifsta->auth_transaction = -1;
3374 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3375 ifsta->assoc_scan_tries = 0;
3376 ifsta->direct_probe_tries = 0;
3377 ifsta->auth_tries = 0;
3378 ifsta->assoc_tries = 0;
3379 netif_carrier_off(sdata->dev);
3383 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
3384 struct ieee80211_if_sta *ifsta)
3386 struct ieee80211_local *local = sdata->local;
3388 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3391 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3392 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3393 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3394 IEEE80211_STA_AUTO_SSID_SEL))) {
3395 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3396 queue_work(local->hw.workqueue, &ifsta->work);
3400 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3401 const char *ssid, int ssid_len)
3403 int tmp, hidden_ssid;
3405 if (ssid_len == ifsta->ssid_len &&
3406 !memcmp(ifsta->ssid, ssid, ssid_len))
3409 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3415 if (ssid[tmp] != '\0') {
3421 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3424 if (ssid_len == 1 && ssid[0] == ' ')
3430 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
3431 struct ieee80211_if_sta *ifsta)
3433 struct ieee80211_local *local = sdata->local;
3434 struct ieee80211_sta_bss *bss, *selected = NULL;
3435 int top_rssi = 0, freq;
3437 spin_lock_bh(&local->sta_bss_lock);
3438 freq = local->oper_channel->center_freq;
3439 list_for_each_entry(bss, &local->sta_bss_list, list) {
3440 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3443 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3444 IEEE80211_STA_AUTO_BSSID_SEL |
3445 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3446 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3447 !!sdata->default_key))
3450 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3454 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3455 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3458 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3459 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3462 if (!selected || top_rssi < bss->signal) {
3464 top_rssi = bss->signal;
3468 atomic_inc(&selected->users);
3469 spin_unlock_bh(&local->sta_bss_lock);
3472 ieee80211_set_freq(sdata, selected->freq);
3473 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3474 ieee80211_sta_set_ssid(sdata, selected->ssid,
3475 selected->ssid_len);
3476 ieee80211_sta_set_bssid(sdata, selected->bssid);
3477 ieee80211_sta_def_wmm_params(sdata, selected, 0);
3479 /* Send out direct probe if no probe resp was received or
3480 * the one we have is outdated
3482 if (!selected->last_probe_resp ||
3483 time_after(jiffies, selected->last_probe_resp
3484 + IEEE80211_SCAN_RESULT_EXPIRE))
3485 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
3487 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
3489 ieee80211_rx_bss_put(local, selected);
3490 ieee80211_sta_reset_auth(sdata, ifsta);
3493 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
3494 ifsta->assoc_scan_tries++;
3495 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3496 ieee80211_sta_start_scan(sdata, NULL, 0);
3498 ieee80211_sta_start_scan(sdata, ifsta->ssid,
3500 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
3501 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3503 ifsta->state = IEEE80211_STA_MLME_DISABLED;
3509 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
3510 struct ieee80211_if_sta *ifsta)
3512 struct ieee80211_local *local = sdata->local;
3513 struct ieee80211_sta_bss *bss;
3514 struct ieee80211_supported_band *sband;
3515 u8 bssid[ETH_ALEN], *pos;
3518 DECLARE_MAC_BUF(mac);
3521 /* Easier testing, use fixed BSSID. */
3522 memset(bssid, 0xfe, ETH_ALEN);
3524 /* Generate random, not broadcast, locally administered BSSID. Mix in
3525 * own MAC address to make sure that devices that do not have proper
3526 * random number generator get different BSSID. */
3527 get_random_bytes(bssid, ETH_ALEN);
3528 for (i = 0; i < ETH_ALEN; i++)
3529 bssid[i] ^= sdata->dev->dev_addr[i];
3534 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3535 sdata->dev->name, print_mac(mac, bssid));
3537 bss = ieee80211_rx_bss_add(sdata, bssid,
3538 local->hw.conf.channel->center_freq,
3539 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3543 bss->band = local->hw.conf.channel->band;
3544 sband = local->hw.wiphy->bands[bss->band];
3546 if (local->hw.conf.beacon_int == 0)
3547 local->hw.conf.beacon_int = 100;
3548 bss->beacon_int = local->hw.conf.beacon_int;
3549 bss->last_update = jiffies;
3550 bss->capability = WLAN_CAPABILITY_IBSS;
3552 if (sdata->default_key)
3553 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3555 sdata->drop_unencrypted = 0;
3557 bss->supp_rates_len = sband->n_bitrates;
3558 pos = bss->supp_rates;
3559 for (i = 0; i < sband->n_bitrates; i++) {
3560 int rate = sband->bitrates[i].bitrate;
3561 *pos++ = (u8) (rate / 5);
3564 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3565 ieee80211_rx_bss_put(local, bss);
3570 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
3571 struct ieee80211_if_sta *ifsta)
3573 struct ieee80211_local *local = sdata->local;
3574 struct ieee80211_sta_bss *bss;
3578 DECLARE_MAC_BUF(mac);
3579 DECLARE_MAC_BUF(mac2);
3581 if (ifsta->ssid_len == 0)
3584 active_ibss = ieee80211_sta_active_ibss(sdata);
3585 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3586 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3587 sdata->dev->name, active_ibss);
3588 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3589 spin_lock_bh(&local->sta_bss_lock);
3590 list_for_each_entry(bss, &local->sta_bss_list, list) {
3591 if (ifsta->ssid_len != bss->ssid_len ||
3592 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3593 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3595 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3596 printk(KERN_DEBUG " bssid=%s found\n",
3597 print_mac(mac, bss->bssid));
3598 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3599 memcpy(bssid, bss->bssid, ETH_ALEN);
3601 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3604 spin_unlock_bh(&local->sta_bss_lock);
3606 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3608 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3609 "%s\n", print_mac(mac, bssid),
3610 print_mac(mac2, ifsta->bssid));
3611 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3613 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3617 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3618 search_freq = bss->freq;
3620 search_freq = local->hw.conf.channel->center_freq;
3622 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
3623 ifsta->ssid, ifsta->ssid_len);
3627 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3628 " based on configured SSID\n",
3629 sdata->dev->name, print_mac(mac, bssid));
3630 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3631 ieee80211_rx_bss_put(local, bss);
3636 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3637 printk(KERN_DEBUG " did not try to join ibss\n");
3638 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3640 /* Selected IBSS not found in current scan results - try to scan */
3641 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
3642 !ieee80211_sta_active_ibss(sdata)) {
3643 mod_timer(&ifsta->timer, jiffies +
3644 IEEE80211_IBSS_MERGE_INTERVAL);
3645 } else if (time_after(jiffies, local->last_scan_completed +
3646 IEEE80211_SCAN_INTERVAL)) {
3647 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3648 "join\n", sdata->dev->name);
3649 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
3651 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
3652 int interval = IEEE80211_SCAN_INTERVAL;
3654 if (time_after(jiffies, ifsta->ibss_join_req +
3655 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3656 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3657 (!(local->oper_channel->flags &
3658 IEEE80211_CHAN_NO_IBSS)))
3659 return ieee80211_sta_create_ibss(sdata, ifsta);
3660 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3661 printk(KERN_DEBUG "%s: IBSS not allowed on"
3662 " %d MHz\n", sdata->dev->name,
3663 local->hw.conf.channel->center_freq);
3666 /* No IBSS found - decrease scan interval and continue
3668 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3671 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
3672 mod_timer(&ifsta->timer, jiffies + interval);
3680 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
3682 struct ieee80211_if_sta *ifsta;
3685 if (len > IEEE80211_MAX_SSID_LEN)
3688 ifsta = &sdata->u.sta;
3690 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3691 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3692 memcpy(ifsta->ssid, ssid, len);
3693 ifsta->ssid_len = len;
3694 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3698 * Hack! MLME code needs to be cleaned up to have different
3699 * entry points for configuration and internal selection change
3701 if (netif_running(sdata->dev))
3702 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3704 printk(KERN_DEBUG "%s: Failed to config new SSID to "
3705 "the low-level driver\n", sdata->dev->name);
3711 ifsta->flags |= IEEE80211_STA_SSID_SET;
3713 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3715 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3716 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3717 ifsta->ibss_join_req = jiffies;
3718 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
3719 return ieee80211_sta_find_ibss(sdata, ifsta);
3726 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
3728 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3729 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3730 *len = ifsta->ssid_len;
3735 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
3737 struct ieee80211_if_sta *ifsta;
3740 ifsta = &sdata->u.sta;
3742 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3743 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3746 * Hack! See also ieee80211_sta_set_ssid.
3748 if (netif_running(sdata->dev))
3749 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
3751 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3752 "the low-level driver\n", sdata->dev->name);
3757 if (is_valid_ether_addr(bssid))
3758 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3760 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3766 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3767 struct ieee80211_sub_if_data *sdata,
3770 struct sk_buff *skb;
3771 struct ieee80211_hdr *nullfunc;
3774 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3776 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3777 "frame\n", sdata->dev->name);
3780 skb_reserve(skb, local->hw.extra_tx_headroom);
3782 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3783 memset(nullfunc, 0, 24);
3784 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3785 IEEE80211_FCTL_TODS);
3787 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3788 nullfunc->frame_control = fc;
3789 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3790 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3791 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3793 ieee80211_sta_tx(sdata, skb, 0);
3797 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3799 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3800 ieee80211_vif_is_mesh(&sdata->vif))
3801 ieee80211_sta_timer((unsigned long)sdata);
3804 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3806 struct ieee80211_local *local = hw_to_local(hw);
3807 struct net_device *dev = local->scan_dev;
3808 struct ieee80211_sub_if_data *sdata;
3809 union iwreq_data wrqu;
3811 local->last_scan_completed = jiffies;
3812 memset(&wrqu, 0, sizeof(wrqu));
3813 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3815 if (local->sta_hw_scanning) {
3816 local->sta_hw_scanning = 0;
3817 if (ieee80211_hw_config(local))
3818 printk(KERN_DEBUG "%s: failed to restore operational "
3819 "channel after scan\n", dev->name);
3820 /* Restart STA timer for HW scan case */
3822 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3823 ieee80211_restart_sta_timer(sdata);
3829 local->sta_sw_scanning = 0;
3830 if (ieee80211_hw_config(local))
3831 printk(KERN_DEBUG "%s: failed to restore operational "
3832 "channel after scan\n", dev->name);
3835 netif_tx_lock_bh(local->mdev);
3836 netif_addr_lock(local->mdev);
3837 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3838 local->ops->configure_filter(local_to_hw(local),
3839 FIF_BCN_PRBRESP_PROMISC,
3840 &local->filter_flags,
3841 local->mdev->mc_count,
3842 local->mdev->mc_list);
3844 netif_addr_unlock(local->mdev);
3845 netif_tx_unlock_bh(local->mdev);
3848 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3849 /* Tell AP we're back */
3850 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3851 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3852 ieee80211_send_nullfunc(local, sdata, 0);
3854 ieee80211_restart_sta_timer(sdata);
3856 netif_wake_queue(sdata->dev);
3861 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3862 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3863 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3864 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3865 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
3866 !ieee80211_sta_active_ibss(sdata)))
3867 ieee80211_sta_find_ibss(sdata, ifsta);
3870 EXPORT_SYMBOL(ieee80211_scan_completed);
3872 void ieee80211_sta_scan_work(struct work_struct *work)
3874 struct ieee80211_local *local =
3875 container_of(work, struct ieee80211_local, scan_work.work);
3876 struct net_device *dev = local->scan_dev;
3877 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3878 struct ieee80211_supported_band *sband;
3879 struct ieee80211_channel *chan;
3881 unsigned long next_delay = 0;
3883 if (!local->sta_sw_scanning)
3886 switch (local->scan_state) {
3887 case SCAN_SET_CHANNEL:
3889 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3890 * after we successfully scanned the last channel of the last
3891 * band (and the last band is supported by the hw)
3893 if (local->scan_band < IEEE80211_NUM_BANDS)
3894 sband = local->hw.wiphy->bands[local->scan_band];
3899 * If we are at an unsupported band and have more bands
3900 * left to scan, advance to the next supported one.
3902 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3904 sband = local->hw.wiphy->bands[local->scan_band];
3905 local->scan_channel_idx = 0;
3908 /* if no more bands/channels left, complete scan */
3909 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3910 ieee80211_scan_completed(local_to_hw(local));
3914 chan = &sband->channels[local->scan_channel_idx];
3916 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3917 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3918 chan->flags & IEEE80211_CHAN_NO_IBSS))
3922 local->scan_channel = chan;
3923 if (ieee80211_hw_config(local)) {
3924 printk(KERN_DEBUG "%s: failed to set freq to "
3925 "%d MHz for scan\n", dev->name,
3931 /* advance state machine to next channel/band */
3932 local->scan_channel_idx++;
3933 if (local->scan_channel_idx >= sband->n_channels) {
3935 * scan_band may end up == IEEE80211_NUM_BANDS, but
3936 * we'll catch that case above and complete the scan
3937 * if that is the case.
3940 local->scan_channel_idx = 0;
3946 next_delay = IEEE80211_PROBE_DELAY +
3947 usecs_to_jiffies(local->hw.channel_change_time);
3948 local->scan_state = SCAN_SEND_PROBE;
3950 case SCAN_SEND_PROBE:
3951 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3952 local->scan_state = SCAN_SET_CHANNEL;
3954 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3956 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
3957 local->scan_ssid_len);
3958 next_delay = IEEE80211_CHANNEL_TIME;
3962 if (local->sta_sw_scanning)
3963 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3968 static int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *scan_sdata,
3969 u8 *ssid, size_t ssid_len)
3971 struct ieee80211_local *local = scan_sdata->local;
3972 struct ieee80211_sub_if_data *sdata;
3974 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3977 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3978 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3981 * ScanType: ACTIVE, PASSIVE
3982 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3983 * a Probe frame during active scanning
3985 * MinChannelTime (>= ProbeDelay), in TU
3986 * MaxChannelTime: (>= MinChannelTime), in TU
3989 /* MLME-SCAN.confirm
3991 * ResultCode: SUCCESS, INVALID_PARAMETERS
3994 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3995 if (local->scan_dev == scan_sdata->dev)
4000 if (local->ops->hw_scan) {
4001 int rc = local->ops->hw_scan(local_to_hw(local),
4004 local->sta_hw_scanning = 1;
4005 local->scan_dev = scan_sdata->dev;
4010 local->sta_sw_scanning = 1;
4013 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4014 netif_stop_queue(sdata->dev);
4015 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
4016 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
4017 ieee80211_send_nullfunc(local, sdata, 1);
4022 local->scan_ssid_len = ssid_len;
4023 memcpy(local->scan_ssid, ssid, ssid_len);
4025 local->scan_ssid_len = 0;
4026 local->scan_state = SCAN_SET_CHANNEL;
4027 local->scan_channel_idx = 0;
4028 local->scan_band = IEEE80211_BAND_2GHZ;
4029 local->scan_dev = scan_sdata->dev;
4031 netif_addr_lock_bh(local->mdev);
4032 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4033 local->ops->configure_filter(local_to_hw(local),
4034 FIF_BCN_PRBRESP_PROMISC,
4035 &local->filter_flags,
4036 local->mdev->mc_count,
4037 local->mdev->mc_list);
4038 netif_addr_unlock_bh(local->mdev);
4040 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4041 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4042 IEEE80211_CHANNEL_TIME);
4048 int ieee80211_sta_req_scan(struct ieee80211_sub_if_data *sdata, u8 *ssid, size_t ssid_len)
4050 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4051 struct ieee80211_local *local = sdata->local;
4053 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4054 return ieee80211_sta_start_scan(sdata, ssid, ssid_len);
4056 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4057 if (local->scan_dev == sdata->dev)
4062 ifsta->scan_ssid_len = ssid_len;
4064 memcpy(ifsta->scan_ssid, ssid, ssid_len);
4065 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4066 queue_work(local->hw.workqueue, &ifsta->work);
4071 static void ieee80211_sta_add_scan_ies(struct iw_request_info *info,
4072 struct ieee80211_sta_bss *bss,
4073 char **current_ev, char *end_buf)
4075 u8 *pos, *end, *next;
4076 struct iw_event iwe;
4078 if (bss == NULL || bss->ies == NULL)
4082 * If needed, fragment the IEs buffer (at IE boundaries) into short
4083 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
4086 end = pos + bss->ies_len;
4088 while (end - pos > IW_GENERIC_IE_MAX) {
4089 next = pos + 2 + pos[1];
4090 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
4091 next = next + 2 + next[1];
4093 memset(&iwe, 0, sizeof(iwe));
4094 iwe.cmd = IWEVGENIE;
4095 iwe.u.data.length = next - pos;
4096 *current_ev = iwe_stream_add_point(info, *current_ev,
4097 end_buf, &iwe, pos);
4103 memset(&iwe, 0, sizeof(iwe));
4104 iwe.cmd = IWEVGENIE;
4105 iwe.u.data.length = end - pos;
4106 *current_ev = iwe_stream_add_point(info, *current_ev,
4107 end_buf, &iwe, pos);
4113 ieee80211_sta_scan_result(struct ieee80211_local *local,
4114 struct iw_request_info *info,
4115 struct ieee80211_sta_bss *bss,
4116 char *current_ev, char *end_buf)
4118 struct iw_event iwe;
4120 if (time_after(jiffies,
4121 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4124 memset(&iwe, 0, sizeof(iwe));
4125 iwe.cmd = SIOCGIWAP;
4126 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4127 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4128 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4131 memset(&iwe, 0, sizeof(iwe));
4132 iwe.cmd = SIOCGIWESSID;
4133 if (bss_mesh_cfg(bss)) {
4134 iwe.u.data.length = bss_mesh_id_len(bss);
4135 iwe.u.data.flags = 1;
4136 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4137 &iwe, bss_mesh_id(bss));
4139 iwe.u.data.length = bss->ssid_len;
4140 iwe.u.data.flags = 1;
4141 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4145 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4146 || bss_mesh_cfg(bss)) {
4147 memset(&iwe, 0, sizeof(iwe));
4148 iwe.cmd = SIOCGIWMODE;
4149 if (bss_mesh_cfg(bss))
4150 iwe.u.mode = IW_MODE_MESH;
4151 else if (bss->capability & WLAN_CAPABILITY_ESS)
4152 iwe.u.mode = IW_MODE_MASTER;
4154 iwe.u.mode = IW_MODE_ADHOC;
4155 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4156 &iwe, IW_EV_UINT_LEN);
4159 memset(&iwe, 0, sizeof(iwe));
4160 iwe.cmd = SIOCGIWFREQ;
4161 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4163 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4166 memset(&iwe, 0, sizeof(iwe));
4167 iwe.cmd = SIOCGIWFREQ;
4168 iwe.u.freq.m = bss->freq;
4170 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4172 memset(&iwe, 0, sizeof(iwe));
4174 iwe.u.qual.qual = bss->qual;
4175 iwe.u.qual.level = bss->signal;
4176 iwe.u.qual.noise = bss->noise;
4177 iwe.u.qual.updated = local->wstats_flags;
4178 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4181 memset(&iwe, 0, sizeof(iwe));
4182 iwe.cmd = SIOCGIWENCODE;
4183 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4184 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4186 iwe.u.data.flags = IW_ENCODE_DISABLED;
4187 iwe.u.data.length = 0;
4188 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4191 ieee80211_sta_add_scan_ies(info, bss, ¤t_ev, end_buf);
4193 if (bss && bss->supp_rates_len > 0) {
4194 /* display all supported rates in readable format */
4195 char *p = current_ev + iwe_stream_lcp_len(info);
4198 memset(&iwe, 0, sizeof(iwe));
4199 iwe.cmd = SIOCGIWRATE;
4200 /* Those two flags are ignored... */
4201 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4203 for (i = 0; i < bss->supp_rates_len; i++) {
4204 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4206 p = iwe_stream_add_value(info, current_ev, p,
4207 end_buf, &iwe, IW_EV_PARAM_LEN);
4214 buf = kmalloc(30, GFP_ATOMIC);
4216 memset(&iwe, 0, sizeof(iwe));
4217 iwe.cmd = IWEVCUSTOM;
4218 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4219 iwe.u.data.length = strlen(buf);
4220 current_ev = iwe_stream_add_point(info, current_ev,
4223 memset(&iwe, 0, sizeof(iwe));
4224 iwe.cmd = IWEVCUSTOM;
4225 sprintf(buf, " Last beacon: %dms ago",
4226 jiffies_to_msecs(jiffies - bss->last_update));
4227 iwe.u.data.length = strlen(buf);
4228 current_ev = iwe_stream_add_point(info, current_ev,
4229 end_buf, &iwe, buf);
4234 if (bss_mesh_cfg(bss)) {
4236 u8 *cfg = bss_mesh_cfg(bss);
4237 buf = kmalloc(50, GFP_ATOMIC);
4239 memset(&iwe, 0, sizeof(iwe));
4240 iwe.cmd = IWEVCUSTOM;
4241 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4242 iwe.u.data.length = strlen(buf);
4243 current_ev = iwe_stream_add_point(info, current_ev,
4246 sprintf(buf, "Path Selection Protocol ID: "
4247 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4249 iwe.u.data.length = strlen(buf);
4250 current_ev = iwe_stream_add_point(info, current_ev,
4253 sprintf(buf, "Path Selection Metric ID: "
4254 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4256 iwe.u.data.length = strlen(buf);
4257 current_ev = iwe_stream_add_point(info, current_ev,
4260 sprintf(buf, "Congestion Control Mode ID: "
4261 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4263 iwe.u.data.length = strlen(buf);
4264 current_ev = iwe_stream_add_point(info, current_ev,
4267 sprintf(buf, "Channel Precedence: "
4268 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4270 iwe.u.data.length = strlen(buf);
4271 current_ev = iwe_stream_add_point(info, current_ev,
4282 int ieee80211_sta_scan_results(struct ieee80211_local *local,
4283 struct iw_request_info *info,
4284 char *buf, size_t len)
4286 char *current_ev = buf;
4287 char *end_buf = buf + len;
4288 struct ieee80211_sta_bss *bss;
4290 spin_lock_bh(&local->sta_bss_lock);
4291 list_for_each_entry(bss, &local->sta_bss_list, list) {
4292 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4293 spin_unlock_bh(&local->sta_bss_lock);
4296 current_ev = ieee80211_sta_scan_result(local, info, bss,
4297 current_ev, end_buf);
4299 spin_unlock_bh(&local->sta_bss_lock);
4300 return current_ev - buf;
4304 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
4306 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4308 kfree(ifsta->extra_ie);
4310 ifsta->extra_ie = NULL;
4311 ifsta->extra_ie_len = 0;
4314 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4315 if (!ifsta->extra_ie) {
4316 ifsta->extra_ie_len = 0;
4319 memcpy(ifsta->extra_ie, ie, len);
4320 ifsta->extra_ie_len = len;
4325 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
4326 struct sk_buff *skb, u8 *bssid,
4327 u8 *addr, u64 supp_rates)
4329 struct ieee80211_local *local = sdata->local;
4330 struct sta_info *sta;
4331 DECLARE_MAC_BUF(mac);
4332 int band = local->hw.conf.channel->band;
4334 /* TODO: Could consider removing the least recently used entry and
4335 * allow new one to be added. */
4336 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4337 if (net_ratelimit()) {
4338 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4339 "entry %s\n", sdata->dev->name, print_mac(mac, addr));
4344 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
4347 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4348 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4349 wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
4352 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4356 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
4358 /* make sure mandatory rates are always added */
4359 sta->supp_rates[band] = supp_rates |
4360 ieee80211_sta_get_mandatory_rates(local, band);
4362 rate_control_rate_init(sta, local);
4364 if (sta_info_insert(sta))
4371 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
4373 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4375 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
4376 sdata->dev->name, reason);
4378 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4379 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4382 ieee80211_send_deauth(sdata, ifsta, reason);
4383 ieee80211_set_disassoc(sdata, ifsta, 1);
4388 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
4390 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4392 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
4393 sdata->dev->name, reason);
4395 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4398 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4401 ieee80211_send_disassoc(sdata, ifsta, reason);
4402 ieee80211_set_disassoc(sdata, ifsta, 0);
4406 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4407 enum ieee80211_notification_types notif_type)
4409 struct ieee80211_local *local = hw_to_local(hw);
4410 struct ieee80211_sub_if_data *sdata;
4412 switch (notif_type) {
4413 case IEEE80211_NOTIFY_RE_ASSOC:
4415 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4416 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4419 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
4425 EXPORT_SYMBOL(ieee80211_notify_mac);