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_AUTH_TIMEOUT (HZ / 5)
38 #define IEEE80211_AUTH_MAX_TRIES 3
39 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40 #define IEEE80211_ASSOC_MAX_TRIES 3
41 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
42 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
43 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
44 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
46 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
49 #define IEEE80211_PROBE_DELAY (HZ / 33)
50 #define IEEE80211_CHANNEL_TIME (HZ / 33)
51 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
55 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
57 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
60 #define ERP_INFO_USE_PROTECTION BIT(1)
62 /* mgmt header + 1 byte action code */
63 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
65 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
68 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
71 /* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73 #define IEEE80211_MIN_AMPDU_BUF 0x8
74 #define IEEE80211_MAX_AMPDU_BUF 0x40
76 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78 static struct ieee80211_sta_bss *
79 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
80 u8 *ssid, u8 ssid_len);
81 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
82 struct ieee80211_sta_bss *bss);
83 static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85 static int ieee80211_sta_wep_configured(struct net_device *dev);
86 static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88 static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
90 static void sta_rx_agg_session_timer_expired(unsigned long data);
93 void ieee802_11_parse_elems(u8 *start, size_t len,
94 struct ieee802_11_elems *elems)
99 memset(elems, 0, sizeof(*elems));
114 elems->ssid_len = elen;
116 case WLAN_EID_SUPP_RATES:
117 elems->supp_rates = pos;
118 elems->supp_rates_len = elen;
120 case WLAN_EID_FH_PARAMS:
121 elems->fh_params = pos;
122 elems->fh_params_len = elen;
124 case WLAN_EID_DS_PARAMS:
125 elems->ds_params = pos;
126 elems->ds_params_len = elen;
128 case WLAN_EID_CF_PARAMS:
129 elems->cf_params = pos;
130 elems->cf_params_len = elen;
134 elems->tim_len = elen;
136 case WLAN_EID_IBSS_PARAMS:
137 elems->ibss_params = pos;
138 elems->ibss_params_len = elen;
140 case WLAN_EID_CHALLENGE:
141 elems->challenge = pos;
142 elems->challenge_len = elen;
145 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
147 /* Microsoft OUI (00:50:F2) */
149 /* OUI Type 1 - WPA IE */
151 elems->wpa_len = elen;
152 } else if (elen >= 5 && pos[3] == 2) {
154 elems->wmm_info = pos;
155 elems->wmm_info_len = elen;
156 } else if (pos[4] == 1) {
157 elems->wmm_param = pos;
158 elems->wmm_param_len = elen;
165 elems->rsn_len = elen;
167 case WLAN_EID_ERP_INFO:
168 elems->erp_info = pos;
169 elems->erp_info_len = elen;
171 case WLAN_EID_EXT_SUPP_RATES:
172 elems->ext_supp_rates = pos;
173 elems->ext_supp_rates_len = elen;
175 case WLAN_EID_HT_CAPABILITY:
176 elems->ht_cap_elem = pos;
177 elems->ht_cap_elem_len = elen;
179 case WLAN_EID_HT_EXTRA_INFO:
180 elems->ht_info_elem = pos;
181 elems->ht_info_elem_len = elen;
183 case WLAN_EID_MESH_ID:
184 elems->mesh_id = pos;
185 elems->mesh_id_len = elen;
187 case WLAN_EID_MESH_CONFIG:
188 elems->mesh_config = pos;
189 elems->mesh_config_len = elen;
191 case WLAN_EID_PEER_LINK:
192 elems->peer_link = pos;
193 elems->peer_link_len = elen;
197 elems->preq_len = elen;
201 elems->prep_len = elen;
205 elems->perr_len = elen;
207 case WLAN_EID_CHANNEL_SWITCH:
208 elems->ch_switch_elem = pos;
209 elems->ch_switch_elem_len = elen;
212 if (!elems->quiet_elem) {
213 elems->quiet_elem = pos;
214 elems->quiet_elem_len = elen;
216 elems->num_of_quiet_elem++;
218 case WLAN_EID_COUNTRY:
219 elems->country_elem = pos;
220 elems->country_elem_len = elen;
222 case WLAN_EID_PWR_CONSTRAINT:
223 elems->pwr_constr_elem = pos;
224 elems->pwr_constr_elem_len = elen;
236 static int ecw2cw(int ecw)
238 return (1 << ecw) - 1;
242 static void ieee80211_sta_def_wmm_params(struct net_device *dev,
243 struct ieee80211_sta_bss *bss,
246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
247 struct ieee80211_local *local = sdata->local;
248 int i, have_higher_than_11mbit = 0;
251 /* cf. IEEE 802.11 9.2.12 */
252 for (i = 0; i < bss->supp_rates_len; i++)
253 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
254 have_higher_than_11mbit = 1;
256 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
257 have_higher_than_11mbit)
258 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
260 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
263 if (local->ops->conf_tx) {
264 struct ieee80211_tx_queue_params qparam;
266 memset(&qparam, 0, sizeof(qparam));
270 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
271 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
276 qparam.cw_max = 1023;
279 for (i = 0; i < local_to_hw(local)->queues; i++)
280 local->ops->conf_tx(local_to_hw(local), i, &qparam);
284 static void ieee80211_sta_wmm_params(struct net_device *dev,
285 struct ieee80211_if_sta *ifsta,
286 u8 *wmm_param, size_t wmm_param_len)
288 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
289 struct ieee80211_tx_queue_params params;
294 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
300 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
302 count = wmm_param[6] & 0x0f;
303 if (count == ifsta->wmm_last_param_set)
305 ifsta->wmm_last_param_set = count;
308 left = wmm_param_len - 8;
310 memset(¶ms, 0, sizeof(params));
312 if (!local->ops->conf_tx)
316 for (; left >= 4; left -= 4, pos += 4) {
317 int aci = (pos[0] >> 5) & 0x03;
318 int acm = (pos[0] >> 4) & 0x01;
325 local->wmm_acm |= BIT(0) | BIT(3);
330 local->wmm_acm |= BIT(4) | BIT(5);
335 local->wmm_acm |= BIT(6) | BIT(7);
341 local->wmm_acm |= BIT(1) | BIT(2);
345 params.aifs = pos[0] & 0x0f;
346 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
347 params.cw_min = ecw2cw(pos[1] & 0x0f);
348 params.txop = get_unaligned_le16(pos + 2);
349 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
350 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
351 "cWmin=%d cWmax=%d txop=%d\n",
352 dev->name, queue, aci, acm, params.aifs, params.cw_min,
353 params.cw_max, params.txop);
355 /* TODO: handle ACM (block TX, fallback to next lowest allowed
357 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
358 printk(KERN_DEBUG "%s: failed to set TX queue "
359 "parameters for queue %d\n", dev->name, queue);
364 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
366 bool use_short_preamble)
368 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
369 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
370 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
371 DECLARE_MAC_BUF(mac);
375 if (use_protection != bss_conf->use_cts_prot) {
376 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
377 if (net_ratelimit()) {
378 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
381 use_protection ? "enabled" : "disabled",
382 print_mac(mac, ifsta->bssid));
385 bss_conf->use_cts_prot = use_protection;
386 changed |= BSS_CHANGED_ERP_CTS_PROT;
389 if (use_short_preamble != bss_conf->use_short_preamble) {
390 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
391 if (net_ratelimit()) {
392 printk(KERN_DEBUG "%s: switched to %s barker preamble"
395 use_short_preamble ? "short" : "long",
396 print_mac(mac, ifsta->bssid));
399 bss_conf->use_short_preamble = use_short_preamble;
400 changed |= BSS_CHANGED_ERP_PREAMBLE;
406 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
409 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
410 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
412 return ieee80211_handle_protect_preamb(sdata,
413 use_protection, use_short_preamble);
416 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
417 struct ieee80211_sta_bss *bss)
421 if (bss->has_erp_value)
422 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
424 u16 capab = bss->capability;
425 changed |= ieee80211_handle_protect_preamb(sdata, false,
426 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
432 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
433 struct ieee80211_ht_info *ht_info)
439 memset(ht_info, 0, sizeof(*ht_info));
442 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
444 ht_info->ht_supported = 1;
445 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
446 ht_info->ampdu_factor =
447 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
448 ht_info->ampdu_density =
449 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
450 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
452 ht_info->ht_supported = 0;
457 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
458 struct ieee80211_ht_addt_info *ht_add_info_ie,
459 struct ieee80211_ht_bss_info *bss_info)
461 if (bss_info == NULL)
464 memset(bss_info, 0, sizeof(*bss_info));
466 if (ht_add_info_ie) {
468 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
470 bss_info->primary_channel = ht_add_info_ie->control_chan;
471 bss_info->bss_cap = ht_add_info_ie->ht_param;
472 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
478 static void ieee80211_sta_send_associnfo(struct net_device *dev,
479 struct ieee80211_if_sta *ifsta)
481 union iwreq_data wrqu;
483 if (ifsta->assocreq_ies) {
484 memset(&wrqu, 0, sizeof(wrqu));
485 wrqu.data.length = ifsta->assocreq_ies_len;
486 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
487 ifsta->assocreq_ies);
490 if (ifsta->assocresp_ies) {
491 memset(&wrqu, 0, sizeof(wrqu));
492 wrqu.data.length = ifsta->assocresp_ies_len;
493 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
494 ifsta->assocresp_ies);
499 static void ieee80211_set_associated(struct net_device *dev,
500 struct ieee80211_if_sta *ifsta,
503 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
504 struct ieee80211_local *local = sdata->local;
505 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
506 union iwreq_data wrqu;
507 u32 changed = BSS_CHANGED_ASSOC;
510 struct ieee80211_sta_bss *bss;
512 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
514 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
517 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
518 conf->channel->center_freq,
519 ifsta->ssid, ifsta->ssid_len);
521 /* set timing information */
522 sdata->bss_conf.beacon_int = bss->beacon_int;
523 sdata->bss_conf.timestamp = bss->timestamp;
524 sdata->bss_conf.dtim_period = bss->dtim_period;
526 changed |= ieee80211_handle_bss_capability(sdata, bss);
528 ieee80211_rx_bss_put(local, bss);
531 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
532 changed |= BSS_CHANGED_HT;
533 sdata->bss_conf.assoc_ht = 1;
534 sdata->bss_conf.ht_conf = &conf->ht_conf;
535 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
538 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
539 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
540 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
541 ieee80211_sta_send_associnfo(dev, ifsta);
543 netif_carrier_off(dev);
544 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
545 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
546 changed |= ieee80211_reset_erp_info(dev);
548 sdata->bss_conf.assoc_ht = 0;
549 sdata->bss_conf.ht_conf = NULL;
550 sdata->bss_conf.ht_bss_conf = NULL;
552 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
554 ifsta->last_probe = jiffies;
555 ieee80211_led_assoc(local, assoc);
557 sdata->bss_conf.assoc = assoc;
558 ieee80211_bss_info_change_notify(sdata, changed);
561 netif_carrier_on(dev);
563 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
564 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
567 static void ieee80211_set_disassoc(struct net_device *dev,
568 struct ieee80211_if_sta *ifsta, int deauth)
571 ifsta->auth_tries = 0;
572 ifsta->assoc_tries = 0;
573 ieee80211_set_associated(dev, ifsta, 0);
576 void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
579 struct ieee80211_sub_if_data *sdata;
581 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
582 skb->dev = sdata->local->mdev;
583 skb_set_mac_header(skb, 0);
584 skb_set_network_header(skb, 0);
585 skb_set_transport_header(skb, 0);
587 skb->iif = sdata->dev->ifindex;
588 skb->do_not_encrypt = !encrypt;
594 static void ieee80211_send_auth(struct net_device *dev,
595 struct ieee80211_if_sta *ifsta,
596 int transaction, u8 *extra, size_t extra_len,
599 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
601 struct ieee80211_mgmt *mgmt;
603 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
604 sizeof(*mgmt) + 6 + extra_len);
606 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
607 "frame\n", dev->name);
610 skb_reserve(skb, local->hw.extra_tx_headroom);
612 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
613 memset(mgmt, 0, 24 + 6);
614 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
615 IEEE80211_STYPE_AUTH);
617 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
618 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
619 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
620 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
621 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
622 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
623 ifsta->auth_transaction = transaction + 1;
624 mgmt->u.auth.status_code = cpu_to_le16(0);
626 memcpy(skb_put(skb, extra_len), extra, extra_len);
628 ieee80211_sta_tx(dev, skb, encrypt);
632 static void ieee80211_authenticate(struct net_device *dev,
633 struct ieee80211_if_sta *ifsta)
635 DECLARE_MAC_BUF(mac);
638 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
639 printk(KERN_DEBUG "%s: authentication with AP %s"
641 dev->name, print_mac(mac, ifsta->bssid));
642 ifsta->state = IEEE80211_DISABLED;
646 ifsta->state = IEEE80211_AUTHENTICATE;
647 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
648 dev->name, print_mac(mac, ifsta->bssid));
650 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
652 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
655 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
656 struct ieee80211_supported_band *sband,
662 for (i = 0; i < bss->supp_rates_len; i++) {
663 int rate = (bss->supp_rates[i] & 0x7F) * 5;
665 for (j = 0; j < sband->n_bitrates; j++)
666 if (sband->bitrates[j].bitrate == rate) {
676 static void ieee80211_send_assoc(struct net_device *dev,
677 struct ieee80211_if_sta *ifsta)
679 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
681 struct ieee80211_mgmt *mgmt;
683 int i, len, count, rates_len, supp_rates_len;
685 struct ieee80211_sta_bss *bss;
687 struct ieee80211_supported_band *sband;
690 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
691 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
694 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
695 "frame\n", dev->name);
698 skb_reserve(skb, local->hw.extra_tx_headroom);
700 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
702 capab = ifsta->capab;
704 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
705 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
706 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
707 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
708 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
711 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
712 local->hw.conf.channel->center_freq,
713 ifsta->ssid, ifsta->ssid_len);
715 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
716 capab |= WLAN_CAPABILITY_PRIVACY;
720 /* get all rates supported by the device and the AP as
721 * some APs don't like getting a superset of their rates
722 * in the association request (e.g. D-Link DAP 1353 in
724 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
726 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
727 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
728 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
730 ieee80211_rx_bss_put(local, bss);
733 rates_len = sband->n_bitrates;
736 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
738 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
739 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
740 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
742 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
744 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
745 IEEE80211_STYPE_REASSOC_REQ);
746 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
747 mgmt->u.reassoc_req.listen_interval =
748 cpu_to_le16(local->hw.conf.listen_interval);
749 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
753 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
754 IEEE80211_STYPE_ASSOC_REQ);
755 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
756 mgmt->u.reassoc_req.listen_interval =
757 cpu_to_le16(local->hw.conf.listen_interval);
761 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
762 *pos++ = WLAN_EID_SSID;
763 *pos++ = ifsta->ssid_len;
764 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
766 /* add all rates which were marked to be used above */
767 supp_rates_len = rates_len;
768 if (supp_rates_len > 8)
771 len = sband->n_bitrates;
772 pos = skb_put(skb, supp_rates_len + 2);
773 *pos++ = WLAN_EID_SUPP_RATES;
774 *pos++ = supp_rates_len;
777 for (i = 0; i < sband->n_bitrates; i++) {
778 if (BIT(i) & rates) {
779 int rate = sband->bitrates[i].bitrate;
780 *pos++ = (u8) (rate / 5);
786 if (rates_len > count) {
787 pos = skb_put(skb, rates_len - count + 2);
788 *pos++ = WLAN_EID_EXT_SUPP_RATES;
789 *pos++ = rates_len - count;
791 for (i++; i < sband->n_bitrates; i++) {
792 if (BIT(i) & rates) {
793 int rate = sband->bitrates[i].bitrate;
794 *pos++ = (u8) (rate / 5);
799 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
800 /* 1. power capabilities */
801 pos = skb_put(skb, 4);
802 *pos++ = WLAN_EID_PWR_CAPABILITY;
804 *pos++ = 0; /* min tx power */
805 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
807 /* 2. supported channels */
808 /* TODO: get this in reg domain format */
809 pos = skb_put(skb, 2 * sband->n_channels + 2);
810 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
811 *pos++ = 2 * sband->n_channels;
812 for (i = 0; i < sband->n_channels; i++) {
813 *pos++ = ieee80211_frequency_to_channel(
814 sband->channels[i].center_freq);
815 *pos++ = 1; /* one channel in the subband*/
819 if (ifsta->extra_ie) {
820 pos = skb_put(skb, ifsta->extra_ie_len);
821 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
824 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
825 pos = skb_put(skb, 9);
826 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
827 *pos++ = 7; /* len */
828 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
831 *pos++ = 2; /* WME */
832 *pos++ = 0; /* WME info */
833 *pos++ = 1; /* WME ver */
837 /* wmm support is a must to HT */
838 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
839 sband->ht_info.ht_supported && bss->ht_add_ie) {
840 struct ieee80211_ht_addt_info *ht_add_info =
841 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
842 u16 cap = sband->ht_info.cap;
844 u32 flags = local->hw.conf.channel->flags;
846 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
847 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
848 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
849 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
850 cap &= ~IEEE80211_HT_CAP_SGI_40;
853 case IEEE80211_HT_IE_CHA_SEC_BELOW:
854 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
855 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
856 cap &= ~IEEE80211_HT_CAP_SGI_40;
861 tmp = cpu_to_le16(cap);
862 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
863 *pos++ = WLAN_EID_HT_CAPABILITY;
864 *pos++ = sizeof(struct ieee80211_ht_cap);
865 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
866 memcpy(pos, &tmp, sizeof(u16));
868 /* TODO: needs a define here for << 2 */
869 *pos++ = sband->ht_info.ampdu_factor |
870 (sband->ht_info.ampdu_density << 2);
871 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
874 kfree(ifsta->assocreq_ies);
875 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
876 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
877 if (ifsta->assocreq_ies)
878 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
880 ieee80211_sta_tx(dev, skb, 0);
884 static void ieee80211_send_deauth(struct net_device *dev,
885 struct ieee80211_if_sta *ifsta, u16 reason)
887 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
889 struct ieee80211_mgmt *mgmt;
891 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
893 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
894 "frame\n", dev->name);
897 skb_reserve(skb, local->hw.extra_tx_headroom);
899 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
901 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
902 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
903 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
904 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
905 IEEE80211_STYPE_DEAUTH);
907 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
909 ieee80211_sta_tx(dev, skb, 0);
913 static void ieee80211_send_disassoc(struct net_device *dev,
914 struct ieee80211_if_sta *ifsta, u16 reason)
916 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
918 struct ieee80211_mgmt *mgmt;
920 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
922 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
923 "frame\n", dev->name);
926 skb_reserve(skb, local->hw.extra_tx_headroom);
928 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
930 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
931 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
932 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
933 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
934 IEEE80211_STYPE_DISASSOC);
936 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
938 ieee80211_sta_tx(dev, skb, 0);
942 static int ieee80211_privacy_mismatch(struct net_device *dev,
943 struct ieee80211_if_sta *ifsta)
945 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
946 struct ieee80211_sta_bss *bss;
951 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
954 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
955 local->hw.conf.channel->center_freq,
956 ifsta->ssid, ifsta->ssid_len);
960 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
961 wep_privacy = !!ieee80211_sta_wep_configured(dev);
962 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
964 ieee80211_rx_bss_put(local, bss);
966 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
973 static void ieee80211_associate(struct net_device *dev,
974 struct ieee80211_if_sta *ifsta)
976 DECLARE_MAC_BUF(mac);
978 ifsta->assoc_tries++;
979 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
980 printk(KERN_DEBUG "%s: association with AP %s"
982 dev->name, print_mac(mac, ifsta->bssid));
983 ifsta->state = IEEE80211_DISABLED;
987 ifsta->state = IEEE80211_ASSOCIATE;
988 printk(KERN_DEBUG "%s: associate with AP %s\n",
989 dev->name, print_mac(mac, ifsta->bssid));
990 if (ieee80211_privacy_mismatch(dev, ifsta)) {
991 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
992 "mixed-cell disabled - abort association\n", dev->name);
993 ifsta->state = IEEE80211_DISABLED;
997 ieee80211_send_assoc(dev, ifsta);
999 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1003 static void ieee80211_associated(struct net_device *dev,
1004 struct ieee80211_if_sta *ifsta)
1006 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1007 struct sta_info *sta;
1009 DECLARE_MAC_BUF(mac);
1011 /* TODO: start monitoring current AP signal quality and number of
1012 * missed beacons. Scan other channels every now and then and search
1013 * for better APs. */
1014 /* TODO: remove expired BSSes */
1016 ifsta->state = IEEE80211_ASSOCIATED;
1020 sta = sta_info_get(local, ifsta->bssid);
1022 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1023 dev->name, print_mac(mac, ifsta->bssid));
1027 if (time_after(jiffies,
1028 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1029 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
1030 printk(KERN_DEBUG "%s: No ProbeResp from "
1031 "current AP %s - assume out of "
1033 dev->name, print_mac(mac, ifsta->bssid));
1035 sta_info_unlink(&sta);
1037 ieee80211_send_probe_req(dev, ifsta->bssid,
1039 local->scan_ssid_len);
1040 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1042 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1043 if (time_after(jiffies, ifsta->last_probe +
1044 IEEE80211_PROBE_INTERVAL)) {
1045 ifsta->last_probe = jiffies;
1046 ieee80211_send_probe_req(dev, ifsta->bssid,
1055 if (disassoc && sta)
1056 sta_info_destroy(sta);
1059 ifsta->state = IEEE80211_DISABLED;
1060 ieee80211_set_associated(dev, ifsta, 0);
1062 mod_timer(&ifsta->timer, jiffies +
1063 IEEE80211_MONITORING_INTERVAL);
1068 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1069 u8 *ssid, size_t ssid_len)
1071 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1072 struct ieee80211_supported_band *sband;
1073 struct sk_buff *skb;
1074 struct ieee80211_mgmt *mgmt;
1075 u8 *pos, *supp_rates, *esupp_rates = NULL;
1078 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1080 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1081 "request\n", dev->name);
1084 skb_reserve(skb, local->hw.extra_tx_headroom);
1086 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1087 memset(mgmt, 0, 24);
1088 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1089 IEEE80211_STYPE_PROBE_REQ);
1090 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1092 memcpy(mgmt->da, dst, ETH_ALEN);
1093 memcpy(mgmt->bssid, dst, ETH_ALEN);
1095 memset(mgmt->da, 0xff, ETH_ALEN);
1096 memset(mgmt->bssid, 0xff, ETH_ALEN);
1098 pos = skb_put(skb, 2 + ssid_len);
1099 *pos++ = WLAN_EID_SSID;
1101 memcpy(pos, ssid, ssid_len);
1103 supp_rates = skb_put(skb, 2);
1104 supp_rates[0] = WLAN_EID_SUPP_RATES;
1106 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1108 for (i = 0; i < sband->n_bitrates; i++) {
1109 struct ieee80211_rate *rate = &sband->bitrates[i];
1111 pos = skb_put(skb, 1);
1113 } else if (supp_rates[1] == 8) {
1114 esupp_rates = skb_put(skb, 3);
1115 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1117 pos = &esupp_rates[2];
1119 pos = skb_put(skb, 1);
1122 *pos = rate->bitrate / 5;
1125 ieee80211_sta_tx(dev, skb, 0);
1129 static int ieee80211_sta_wep_configured(struct net_device *dev)
1131 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1132 if (!sdata || !sdata->default_key ||
1133 sdata->default_key->conf.alg != ALG_WEP)
1139 static void ieee80211_auth_completed(struct net_device *dev,
1140 struct ieee80211_if_sta *ifsta)
1142 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
1143 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1144 ieee80211_associate(dev, ifsta);
1148 static void ieee80211_auth_challenge(struct net_device *dev,
1149 struct ieee80211_if_sta *ifsta,
1150 struct ieee80211_mgmt *mgmt,
1154 struct ieee802_11_elems elems;
1156 pos = mgmt->u.auth.variable;
1157 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1158 if (!elems.challenge)
1160 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1161 elems.challenge_len + 2, 1);
1164 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1165 u8 dialog_token, u16 status, u16 policy,
1166 u16 buf_size, u16 timeout)
1168 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1169 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1170 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1171 struct sk_buff *skb;
1172 struct ieee80211_mgmt *mgmt;
1175 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1178 printk(KERN_DEBUG "%s: failed to allocate buffer "
1179 "for addba resp frame\n", dev->name);
1183 skb_reserve(skb, local->hw.extra_tx_headroom);
1184 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1185 memset(mgmt, 0, 24);
1186 memcpy(mgmt->da, da, ETH_ALEN);
1187 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1188 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1189 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1191 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1192 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1193 IEEE80211_STYPE_ACTION);
1195 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1196 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1197 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1198 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1200 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1201 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1202 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1204 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1205 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1206 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1208 ieee80211_sta_tx(dev, skb, 0);
1213 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1214 u16 tid, u8 dialog_token, u16 start_seq_num,
1215 u16 agg_size, u16 timeout)
1217 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1218 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1219 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1220 struct sk_buff *skb;
1221 struct ieee80211_mgmt *mgmt;
1224 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1227 printk(KERN_ERR "%s: failed to allocate buffer "
1228 "for addba request frame\n", dev->name);
1231 skb_reserve(skb, local->hw.extra_tx_headroom);
1232 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1233 memset(mgmt, 0, 24);
1234 memcpy(mgmt->da, da, ETH_ALEN);
1235 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1236 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1237 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1239 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1241 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1242 IEEE80211_STYPE_ACTION);
1244 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1246 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1247 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1249 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1250 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1251 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1252 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1254 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1256 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1257 mgmt->u.action.u.addba_req.start_seq_num =
1258 cpu_to_le16(start_seq_num << 4);
1260 ieee80211_sta_tx(dev, skb, 0);
1263 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1264 struct ieee80211_mgmt *mgmt,
1267 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1268 struct ieee80211_hw *hw = &local->hw;
1269 struct ieee80211_conf *conf = &hw->conf;
1270 struct sta_info *sta;
1271 struct tid_ampdu_rx *tid_agg_rx;
1272 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1274 int ret = -EOPNOTSUPP;
1275 DECLARE_MAC_BUF(mac);
1279 sta = sta_info_get(local, mgmt->sa);
1285 /* extract session parameters from addba request frame */
1286 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1287 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1289 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1291 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1292 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1293 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1294 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1296 status = WLAN_STATUS_REQUEST_DECLINED;
1298 /* sanity check for incoming parameters:
1299 * check if configuration can support the BA policy
1300 * and if buffer size does not exceeds max value */
1301 if (((ba_policy != 1)
1302 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1303 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1304 status = WLAN_STATUS_INVALID_QOS_PARAM;
1305 #ifdef CONFIG_MAC80211_HT_DEBUG
1306 if (net_ratelimit())
1307 printk(KERN_DEBUG "AddBA Req with bad params from "
1308 "%s on tid %u. policy %d, buffer size %d\n",
1309 print_mac(mac, mgmt->sa), tid, ba_policy,
1311 #endif /* CONFIG_MAC80211_HT_DEBUG */
1314 /* determine default buffer size */
1315 if (buf_size == 0) {
1316 struct ieee80211_supported_band *sband;
1318 sband = local->hw.wiphy->bands[conf->channel->band];
1319 buf_size = IEEE80211_MIN_AMPDU_BUF;
1320 buf_size = buf_size << sband->ht_info.ampdu_factor;
1324 /* examine state machine */
1325 spin_lock_bh(&sta->lock);
1327 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1328 #ifdef CONFIG_MAC80211_HT_DEBUG
1329 if (net_ratelimit())
1330 printk(KERN_DEBUG "unexpected AddBA Req from "
1332 print_mac(mac, mgmt->sa), tid);
1333 #endif /* CONFIG_MAC80211_HT_DEBUG */
1337 /* prepare A-MPDU MLME for Rx aggregation */
1338 sta->ampdu_mlme.tid_rx[tid] =
1339 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1340 if (!sta->ampdu_mlme.tid_rx[tid]) {
1341 #ifdef CONFIG_MAC80211_HT_DEBUG
1342 if (net_ratelimit())
1343 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1349 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1350 sta_rx_agg_session_timer_expired;
1351 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1352 (unsigned long)&sta->timer_to_tid[tid];
1353 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1355 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1357 /* prepare reordering buffer */
1358 tid_agg_rx->reorder_buf =
1359 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1360 if (!tid_agg_rx->reorder_buf) {
1361 #ifdef CONFIG_MAC80211_HT_DEBUG
1362 if (net_ratelimit())
1363 printk(KERN_ERR "can not allocate reordering buffer "
1364 "to tid %d\n", tid);
1366 kfree(sta->ampdu_mlme.tid_rx[tid]);
1369 memset(tid_agg_rx->reorder_buf, 0,
1370 buf_size * sizeof(struct sk_buff *));
1372 if (local->ops->ampdu_action)
1373 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1374 sta->addr, tid, &start_seq_num);
1375 #ifdef CONFIG_MAC80211_HT_DEBUG
1376 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1377 #endif /* CONFIG_MAC80211_HT_DEBUG */
1380 kfree(tid_agg_rx->reorder_buf);
1382 sta->ampdu_mlme.tid_rx[tid] = NULL;
1386 /* change state and send addba resp */
1387 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1388 tid_agg_rx->dialog_token = dialog_token;
1389 tid_agg_rx->ssn = start_seq_num;
1390 tid_agg_rx->head_seq_num = start_seq_num;
1391 tid_agg_rx->buf_size = buf_size;
1392 tid_agg_rx->timeout = timeout;
1393 tid_agg_rx->stored_mpdu_num = 0;
1394 status = WLAN_STATUS_SUCCESS;
1396 spin_unlock_bh(&sta->lock);
1399 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1400 dialog_token, status, 1, buf_size, timeout);
1404 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1405 struct ieee80211_mgmt *mgmt,
1408 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1409 struct ieee80211_hw *hw = &local->hw;
1410 struct sta_info *sta;
1417 sta = sta_info_get(local, mgmt->sa);
1423 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1424 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1426 state = &sta->ampdu_mlme.tid_state_tx[tid];
1428 spin_lock_bh(&sta->lock);
1430 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1431 spin_unlock_bh(&sta->lock);
1432 goto addba_resp_exit;
1435 if (mgmt->u.action.u.addba_resp.dialog_token !=
1436 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1437 spin_unlock_bh(&sta->lock);
1438 #ifdef CONFIG_MAC80211_HT_DEBUG
1439 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1440 #endif /* CONFIG_MAC80211_HT_DEBUG */
1441 goto addba_resp_exit;
1444 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1445 #ifdef CONFIG_MAC80211_HT_DEBUG
1446 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1447 #endif /* CONFIG_MAC80211_HT_DEBUG */
1448 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1449 == WLAN_STATUS_SUCCESS) {
1450 *state |= HT_ADDBA_RECEIVED_MSK;
1451 sta->ampdu_mlme.addba_req_num[tid] = 0;
1453 if (*state == HT_AGG_STATE_OPERATIONAL)
1454 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1456 spin_unlock_bh(&sta->lock);
1458 sta->ampdu_mlme.addba_req_num[tid]++;
1459 /* this will allow the state check in stop_BA_session */
1460 *state = HT_AGG_STATE_OPERATIONAL;
1461 spin_unlock_bh(&sta->lock);
1462 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1463 WLAN_BACK_INITIATOR);
1470 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1471 u16 initiator, u16 reason_code)
1473 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1474 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1475 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1476 struct sk_buff *skb;
1477 struct ieee80211_mgmt *mgmt;
1480 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1483 printk(KERN_ERR "%s: failed to allocate buffer "
1484 "for delba frame\n", dev->name);
1488 skb_reserve(skb, local->hw.extra_tx_headroom);
1489 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1490 memset(mgmt, 0, 24);
1491 memcpy(mgmt->da, da, ETH_ALEN);
1492 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1493 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1494 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1496 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1497 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1498 IEEE80211_STYPE_ACTION);
1500 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1502 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1503 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1504 params = (u16)(initiator << 11); /* bit 11 initiator */
1505 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1507 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1508 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1510 ieee80211_sta_tx(dev, skb, 0);
1513 void ieee80211_send_bar(struct net_device *dev, u8 *ra, u16 tid, u16 ssn)
1515 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1516 struct sk_buff *skb;
1517 struct ieee80211_bar *bar;
1518 u16 bar_control = 0;
1520 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1522 printk(KERN_ERR "%s: failed to allocate buffer for "
1523 "bar frame\n", dev->name);
1526 skb_reserve(skb, local->hw.extra_tx_headroom);
1527 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1528 memset(bar, 0, sizeof(*bar));
1529 bar->frame_control = IEEE80211_FC(IEEE80211_FTYPE_CTL,
1530 IEEE80211_STYPE_BACK_REQ);
1531 memcpy(bar->ra, ra, ETH_ALEN);
1532 memcpy(bar->ta, dev->dev_addr, ETH_ALEN);
1533 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1534 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1535 bar_control |= (u16)(tid << 12);
1536 bar->control = cpu_to_le16(bar_control);
1537 bar->start_seq_num = cpu_to_le16(ssn);
1539 ieee80211_sta_tx(dev, skb, 0);
1542 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1543 u16 initiator, u16 reason)
1545 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1546 struct ieee80211_hw *hw = &local->hw;
1547 struct sta_info *sta;
1549 DECLARE_MAC_BUF(mac);
1553 sta = sta_info_get(local, ra);
1559 /* check if TID is in operational state */
1560 spin_lock_bh(&sta->lock);
1561 if (sta->ampdu_mlme.tid_state_rx[tid]
1562 != HT_AGG_STATE_OPERATIONAL) {
1563 spin_unlock_bh(&sta->lock);
1567 sta->ampdu_mlme.tid_state_rx[tid] =
1568 HT_AGG_STATE_REQ_STOP_BA_MSK |
1569 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1570 spin_unlock_bh(&sta->lock);
1572 /* stop HW Rx aggregation. ampdu_action existence
1573 * already verified in session init so we add the BUG_ON */
1574 BUG_ON(!local->ops->ampdu_action);
1576 #ifdef CONFIG_MAC80211_HT_DEBUG
1577 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1578 print_mac(mac, ra), tid);
1579 #endif /* CONFIG_MAC80211_HT_DEBUG */
1581 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1584 printk(KERN_DEBUG "HW problem - can not stop rx "
1585 "aggregation for tid %d\n", tid);
1587 /* shutdown timer has not expired */
1588 if (initiator != WLAN_BACK_TIMER)
1589 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1591 /* check if this is a self generated aggregation halt */
1592 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1593 ieee80211_send_delba(dev, ra, tid, 0, reason);
1595 /* free the reordering buffer */
1596 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1597 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1598 /* release the reordered frames */
1599 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1600 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1601 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1604 /* free resources */
1605 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1606 kfree(sta->ampdu_mlme.tid_rx[tid]);
1607 sta->ampdu_mlme.tid_rx[tid] = NULL;
1608 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1614 static void ieee80211_sta_process_delba(struct net_device *dev,
1615 struct ieee80211_mgmt *mgmt, size_t len)
1617 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1618 struct sta_info *sta;
1621 DECLARE_MAC_BUF(mac);
1625 sta = sta_info_get(local, mgmt->sa);
1631 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1632 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1633 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1635 #ifdef CONFIG_MAC80211_HT_DEBUG
1636 if (net_ratelimit())
1637 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1638 print_mac(mac, mgmt->sa),
1639 initiator ? "initiator" : "recipient", tid,
1640 mgmt->u.action.u.delba.reason_code);
1641 #endif /* CONFIG_MAC80211_HT_DEBUG */
1643 if (initiator == WLAN_BACK_INITIATOR)
1644 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1645 WLAN_BACK_INITIATOR, 0);
1646 else { /* WLAN_BACK_RECIPIENT */
1647 spin_lock_bh(&sta->lock);
1648 sta->ampdu_mlme.tid_state_tx[tid] =
1649 HT_AGG_STATE_OPERATIONAL;
1650 spin_unlock_bh(&sta->lock);
1651 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1652 WLAN_BACK_RECIPIENT);
1658 * After sending add Block Ack request we activated a timer until
1659 * add Block Ack response will arrive from the recipient.
1660 * If this timer expires sta_addba_resp_timer_expired will be executed.
1662 void sta_addba_resp_timer_expired(unsigned long data)
1664 /* not an elegant detour, but there is no choice as the timer passes
1665 * only one argument, and both sta_info and TID are needed, so init
1666 * flow in sta_info_create gives the TID as data, while the timer_to_id
1667 * array gives the sta through container_of */
1668 u16 tid = *(u8 *)data;
1669 struct sta_info *temp_sta = container_of((void *)data,
1670 struct sta_info, timer_to_tid[tid]);
1672 struct ieee80211_local *local = temp_sta->local;
1673 struct ieee80211_hw *hw = &local->hw;
1674 struct sta_info *sta;
1679 sta = sta_info_get(local, temp_sta->addr);
1685 state = &sta->ampdu_mlme.tid_state_tx[tid];
1686 /* check if the TID waits for addBA response */
1687 spin_lock_bh(&sta->lock);
1688 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1689 spin_unlock_bh(&sta->lock);
1690 *state = HT_AGG_STATE_IDLE;
1691 #ifdef CONFIG_MAC80211_HT_DEBUG
1692 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1693 "expecting addBA response there", tid);
1695 goto timer_expired_exit;
1698 #ifdef CONFIG_MAC80211_HT_DEBUG
1699 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1702 /* go through the state check in stop_BA_session */
1703 *state = HT_AGG_STATE_OPERATIONAL;
1704 spin_unlock_bh(&sta->lock);
1705 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1706 WLAN_BACK_INITIATOR);
1713 * After accepting the AddBA Request we activated a timer,
1714 * resetting it after each frame that arrives from the originator.
1715 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1717 static void sta_rx_agg_session_timer_expired(unsigned long data)
1719 /* not an elegant detour, but there is no choice as the timer passes
1720 * only one argument, and various sta_info are needed here, so init
1721 * flow in sta_info_create gives the TID as data, while the timer_to_id
1722 * array gives the sta through container_of */
1723 u8 *ptid = (u8 *)data;
1724 u8 *timer_to_id = ptid - *ptid;
1725 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1728 #ifdef CONFIG_MAC80211_HT_DEBUG
1729 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1731 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1732 (u16)*ptid, WLAN_BACK_TIMER,
1733 WLAN_REASON_QSTA_TIMEOUT);
1736 void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1738 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1741 for (i = 0; i < STA_TID_NUM; i++) {
1742 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1743 WLAN_BACK_INITIATOR);
1744 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1745 WLAN_BACK_RECIPIENT,
1746 WLAN_REASON_QSTA_LEAVE_QBSS);
1750 static void ieee80211_send_refuse_measurement_request(struct net_device *dev,
1751 struct ieee80211_msrment_ie *request_ie,
1752 const u8 *da, const u8 *bssid,
1755 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1756 struct sk_buff *skb;
1757 struct ieee80211_mgmt *msr_report;
1759 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1760 sizeof(struct ieee80211_msrment_ie));
1763 printk(KERN_ERR "%s: failed to allocate buffer for "
1764 "measurement report frame\n", dev->name);
1768 skb_reserve(skb, local->hw.extra_tx_headroom);
1769 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1770 memset(msr_report, 0, 24);
1771 memcpy(msr_report->da, da, ETH_ALEN);
1772 memcpy(msr_report->sa, dev->dev_addr, ETH_ALEN);
1773 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1774 msr_report->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1775 IEEE80211_STYPE_ACTION);
1777 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1778 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1779 msr_report->u.action.u.measurement.action_code =
1780 WLAN_ACTION_SPCT_MSR_RPRT;
1781 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1783 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1784 msr_report->u.action.u.measurement.length =
1785 sizeof(struct ieee80211_msrment_ie);
1787 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1788 sizeof(struct ieee80211_msrment_ie));
1789 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1790 msr_report->u.action.u.measurement.msr_elem.mode |=
1791 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1792 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1794 ieee80211_sta_tx(dev, skb, 0);
1797 static void ieee80211_sta_process_measurement_req(struct net_device *dev,
1798 struct ieee80211_mgmt *mgmt,
1802 * Ignoring measurement request is spec violation.
1803 * Mandatory measurements must be reported optional
1804 * measurements might be refused or reported incapable
1805 * For now just refuse
1806 * TODO: Answer basic measurement as unmeasured
1808 ieee80211_send_refuse_measurement_request(dev,
1809 &mgmt->u.action.u.measurement.msr_elem,
1810 mgmt->sa, mgmt->bssid,
1811 mgmt->u.action.u.measurement.dialog_token);
1815 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1816 struct ieee80211_if_sta *ifsta,
1817 struct ieee80211_mgmt *mgmt,
1820 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1821 u16 auth_alg, auth_transaction, status_code;
1822 DECLARE_MAC_BUF(mac);
1824 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1825 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
1831 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1832 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1835 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1836 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1839 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1840 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1841 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1843 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1845 * IEEE 802.11 standard does not require authentication in IBSS
1846 * networks and most implementations do not seem to use it.
1847 * However, try to reply to authentication attempts if someone
1848 * has actually implemented this.
1850 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1852 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1855 if (auth_alg != ifsta->auth_alg ||
1856 auth_transaction != ifsta->auth_transaction)
1859 if (status_code != WLAN_STATUS_SUCCESS) {
1860 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1862 const int num_algs = ARRAY_SIZE(algs);
1864 algs[0] = algs[1] = algs[2] = 0xff;
1865 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1866 algs[0] = WLAN_AUTH_OPEN;
1867 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1868 algs[1] = WLAN_AUTH_SHARED_KEY;
1869 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1870 algs[2] = WLAN_AUTH_LEAP;
1871 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1873 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1877 for (i = 0; i < num_algs; i++) {
1879 if (pos >= num_algs)
1881 if (algs[pos] == ifsta->auth_alg ||
1884 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1885 !ieee80211_sta_wep_configured(dev))
1887 ifsta->auth_alg = algs[pos];
1894 switch (ifsta->auth_alg) {
1895 case WLAN_AUTH_OPEN:
1896 case WLAN_AUTH_LEAP:
1897 ieee80211_auth_completed(dev, ifsta);
1899 case WLAN_AUTH_SHARED_KEY:
1900 if (ifsta->auth_transaction == 4)
1901 ieee80211_auth_completed(dev, ifsta);
1903 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1909 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1910 struct ieee80211_if_sta *ifsta,
1911 struct ieee80211_mgmt *mgmt,
1915 DECLARE_MAC_BUF(mac);
1920 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1923 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1925 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1926 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1928 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1929 ifsta->state == IEEE80211_ASSOCIATE ||
1930 ifsta->state == IEEE80211_ASSOCIATED) {
1931 ifsta->state = IEEE80211_AUTHENTICATE;
1932 mod_timer(&ifsta->timer, jiffies +
1933 IEEE80211_RETRY_AUTH_INTERVAL);
1936 ieee80211_set_disassoc(dev, ifsta, 1);
1937 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1941 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1942 struct ieee80211_if_sta *ifsta,
1943 struct ieee80211_mgmt *mgmt,
1947 DECLARE_MAC_BUF(mac);
1952 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1955 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1957 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1958 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1960 if (ifsta->state == IEEE80211_ASSOCIATED) {
1961 ifsta->state = IEEE80211_ASSOCIATE;
1962 mod_timer(&ifsta->timer, jiffies +
1963 IEEE80211_RETRY_AUTH_INTERVAL);
1966 ieee80211_set_disassoc(dev, ifsta, 0);
1970 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1971 struct ieee80211_if_sta *ifsta,
1972 struct ieee80211_mgmt *mgmt,
1976 struct ieee80211_local *local = sdata->local;
1977 struct net_device *dev = sdata->dev;
1978 struct ieee80211_supported_band *sband;
1979 struct sta_info *sta;
1980 u64 rates, basic_rates;
1981 u16 capab_info, status_code, aid;
1982 struct ieee802_11_elems elems;
1983 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1986 DECLARE_MAC_BUF(mac);
1987 bool have_higher_than_11mbit = false;
1989 /* AssocResp and ReassocResp have identical structure, so process both
1990 * of them in this function. */
1992 if (ifsta->state != IEEE80211_ASSOCIATE)
1998 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
2001 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2002 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2003 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2005 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
2006 "status=%d aid=%d)\n",
2007 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
2008 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2010 if (status_code != WLAN_STATUS_SUCCESS) {
2011 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2012 dev->name, status_code);
2013 /* if this was a reassociation, ensure we try a "full"
2014 * association next time. This works around some broken APs
2015 * which do not correctly reject reassociation requests. */
2016 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2020 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2021 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2022 "set\n", dev->name, aid);
2023 aid &= ~(BIT(15) | BIT(14));
2025 pos = mgmt->u.assoc_resp.variable;
2026 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2028 if (!elems.supp_rates) {
2029 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2034 printk(KERN_DEBUG "%s: associated\n", dev->name);
2036 ifsta->ap_capab = capab_info;
2038 kfree(ifsta->assocresp_ies);
2039 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
2040 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
2041 if (ifsta->assocresp_ies)
2042 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2046 /* Add STA entry for the AP */
2047 sta = sta_info_get(local, ifsta->bssid);
2049 struct ieee80211_sta_bss *bss;
2052 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2054 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2055 " the AP\n", dev->name);
2059 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
2060 local->hw.conf.channel->center_freq,
2061 ifsta->ssid, ifsta->ssid_len);
2063 sta->last_signal = bss->signal;
2064 sta->last_qual = bss->qual;
2065 sta->last_noise = bss->noise;
2066 ieee80211_rx_bss_put(local, bss);
2069 err = sta_info_insert(sta);
2071 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2072 " the AP (error %d)\n", dev->name, err);
2076 /* update new sta with its last rx activity */
2077 sta->last_rx = jiffies;
2081 * FIXME: Do we really need to update the sta_info's information here?
2082 * We already know about the AP (we found it in our list) so it
2083 * should already be filled with the right info, no?
2084 * As is stands, all this is racy because typically we assume
2085 * the information that is filled in here (except flags) doesn't
2086 * change while a STA structure is alive. As such, it should move
2087 * to between the sta_info_alloc() and sta_info_insert() above.
2090 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2091 WLAN_STA_AUTHORIZED);
2095 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2097 for (i = 0; i < elems.supp_rates_len; i++) {
2098 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2101 have_higher_than_11mbit = true;
2103 for (j = 0; j < sband->n_bitrates; j++) {
2104 if (sband->bitrates[j].bitrate == rate)
2106 if (elems.supp_rates[i] & 0x80)
2107 basic_rates |= BIT(j);
2111 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2112 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2115 have_higher_than_11mbit = true;
2117 for (j = 0; j < sband->n_bitrates; j++) {
2118 if (sband->bitrates[j].bitrate == rate)
2120 if (elems.ext_supp_rates[i] & 0x80)
2121 basic_rates |= BIT(j);
2125 sta->supp_rates[local->hw.conf.channel->band] = rates;
2126 sdata->basic_rates = basic_rates;
2128 /* cf. IEEE 802.11 9.2.12 */
2129 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2130 have_higher_than_11mbit)
2131 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2133 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2135 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2136 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2137 struct ieee80211_ht_bss_info bss_info;
2138 ieee80211_ht_cap_ie_to_ht_info(
2139 (struct ieee80211_ht_cap *)
2140 elems.ht_cap_elem, &sta->ht_info);
2141 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2142 (struct ieee80211_ht_addt_info *)
2143 elems.ht_info_elem, &bss_info);
2144 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2147 rate_control_rate_init(sta, local);
2149 if (elems.wmm_param) {
2150 set_sta_flags(sta, WLAN_STA_WME);
2152 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2153 elems.wmm_param_len);
2157 /* set AID and assoc capability,
2158 * ieee80211_set_associated() will tell the driver */
2159 bss_conf->aid = aid;
2160 bss_conf->assoc_capability = capab_info;
2161 ieee80211_set_associated(dev, ifsta, 1);
2163 ieee80211_associated(dev, ifsta);
2167 /* Caller must hold local->sta_bss_lock */
2168 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2169 struct ieee80211_sta_bss *bss)
2171 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2174 if (bss_mesh_cfg(bss))
2175 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2176 bss_mesh_id_len(bss));
2178 hash_idx = STA_HASH(bss->bssid);
2180 bss->hnext = local->sta_bss_hash[hash_idx];
2181 local->sta_bss_hash[hash_idx] = bss;
2185 /* Caller must hold local->sta_bss_lock */
2186 static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
2187 struct ieee80211_sta_bss *bss)
2189 struct ieee80211_sta_bss *b, *prev = NULL;
2190 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2194 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2197 prev->hnext = bss->hnext;
2206 static struct ieee80211_sta_bss *
2207 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
2208 u8 *ssid, u8 ssid_len)
2210 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2211 struct ieee80211_sta_bss *bss;
2213 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2216 atomic_inc(&bss->users);
2217 atomic_inc(&bss->users);
2218 memcpy(bss->bssid, bssid, ETH_ALEN);
2220 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2221 memcpy(bss->ssid, ssid, ssid_len);
2222 bss->ssid_len = ssid_len;
2225 spin_lock_bh(&local->sta_bss_lock);
2226 /* TODO: order by RSSI? */
2227 list_add_tail(&bss->list, &local->sta_bss_list);
2228 __ieee80211_rx_bss_hash_add(dev, bss);
2229 spin_unlock_bh(&local->sta_bss_lock);
2233 static struct ieee80211_sta_bss *
2234 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
2235 u8 *ssid, u8 ssid_len)
2237 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2238 struct ieee80211_sta_bss *bss;
2240 spin_lock_bh(&local->sta_bss_lock);
2241 bss = local->sta_bss_hash[STA_HASH(bssid)];
2243 if (!bss_mesh_cfg(bss) &&
2244 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2245 bss->freq == freq &&
2246 bss->ssid_len == ssid_len &&
2247 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2248 atomic_inc(&bss->users);
2253 spin_unlock_bh(&local->sta_bss_lock);
2257 #ifdef CONFIG_MAC80211_MESH
2258 static struct ieee80211_sta_bss *
2259 ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2260 u8 *mesh_cfg, int freq)
2262 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2263 struct ieee80211_sta_bss *bss;
2265 spin_lock_bh(&local->sta_bss_lock);
2266 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2268 if (bss_mesh_cfg(bss) &&
2269 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2270 bss->freq == freq &&
2271 mesh_id_len == bss->mesh_id_len &&
2272 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2274 atomic_inc(&bss->users);
2279 spin_unlock_bh(&local->sta_bss_lock);
2283 static struct ieee80211_sta_bss *
2284 ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2285 u8 *mesh_cfg, int mesh_config_len, int freq)
2287 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2288 struct ieee80211_sta_bss *bss;
2290 if (mesh_config_len != MESH_CFG_LEN)
2293 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2297 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2298 if (!bss->mesh_cfg) {
2303 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2304 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2305 if (!bss->mesh_id) {
2306 kfree(bss->mesh_cfg);
2310 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2313 atomic_inc(&bss->users);
2314 atomic_inc(&bss->users);
2315 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2316 bss->mesh_id_len = mesh_id_len;
2318 spin_lock_bh(&local->sta_bss_lock);
2319 /* TODO: order by RSSI? */
2320 list_add_tail(&bss->list, &local->sta_bss_list);
2321 __ieee80211_rx_bss_hash_add(dev, bss);
2322 spin_unlock_bh(&local->sta_bss_lock);
2327 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2333 kfree(bss->ht_add_ie);
2334 kfree(bss_mesh_id(bss));
2335 kfree(bss_mesh_cfg(bss));
2340 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
2341 struct ieee80211_sta_bss *bss)
2344 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2349 __ieee80211_rx_bss_hash_del(local, bss);
2350 list_del(&bss->list);
2351 spin_unlock_bh(&local->sta_bss_lock);
2352 ieee80211_rx_bss_free(bss);
2356 void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
2358 spin_lock_init(&local->sta_bss_lock);
2359 INIT_LIST_HEAD(&local->sta_bss_list);
2363 void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
2365 struct ieee80211_sta_bss *bss, *tmp;
2367 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2368 ieee80211_rx_bss_put(local, bss);
2372 static int ieee80211_sta_join_ibss(struct net_device *dev,
2373 struct ieee80211_if_sta *ifsta,
2374 struct ieee80211_sta_bss *bss)
2376 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2377 int res, rates, i, j;
2378 struct sk_buff *skb;
2379 struct ieee80211_mgmt *mgmt;
2381 struct ieee80211_sub_if_data *sdata;
2382 struct ieee80211_supported_band *sband;
2383 union iwreq_data wrqu;
2385 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2387 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2389 /* Remove possible STA entries from other IBSS networks. */
2390 sta_info_flush_delayed(sdata);
2392 if (local->ops->reset_tsf) {
2393 /* Reset own TSF to allow time synchronization work. */
2394 local->ops->reset_tsf(local_to_hw(local));
2396 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2397 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2401 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2403 sdata->drop_unencrypted = bss->capability &
2404 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2406 res = ieee80211_set_freq(dev, bss->freq);
2411 /* Build IBSS probe response */
2412 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2414 skb_reserve(skb, local->hw.extra_tx_headroom);
2416 mgmt = (struct ieee80211_mgmt *)
2417 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2418 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2419 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2420 IEEE80211_STYPE_PROBE_RESP);
2421 memset(mgmt->da, 0xff, ETH_ALEN);
2422 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2423 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2424 mgmt->u.beacon.beacon_int =
2425 cpu_to_le16(local->hw.conf.beacon_int);
2426 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
2427 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2429 pos = skb_put(skb, 2 + ifsta->ssid_len);
2430 *pos++ = WLAN_EID_SSID;
2431 *pos++ = ifsta->ssid_len;
2432 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2434 rates = bss->supp_rates_len;
2437 pos = skb_put(skb, 2 + rates);
2438 *pos++ = WLAN_EID_SUPP_RATES;
2440 memcpy(pos, bss->supp_rates, rates);
2442 if (bss->band == IEEE80211_BAND_2GHZ) {
2443 pos = skb_put(skb, 2 + 1);
2444 *pos++ = WLAN_EID_DS_PARAMS;
2446 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2449 pos = skb_put(skb, 2 + 2);
2450 *pos++ = WLAN_EID_IBSS_PARAMS;
2452 /* FIX: set ATIM window based on scan results */
2456 if (bss->supp_rates_len > 8) {
2457 rates = bss->supp_rates_len - 8;
2458 pos = skb_put(skb, 2 + rates);
2459 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2461 memcpy(pos, &bss->supp_rates[8], rates);
2464 ifsta->probe_resp = skb;
2466 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2470 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2471 for (i = 0; i < bss->supp_rates_len; i++) {
2472 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2473 for (j = 0; j < sband->n_bitrates; j++)
2474 if (sband->bitrates[j].bitrate == bitrate)
2477 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2479 ieee80211_sta_def_wmm_params(dev, bss, 1);
2481 ifsta->state = IEEE80211_IBSS_JOINED;
2482 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2484 memset(&wrqu, 0, sizeof(wrqu));
2485 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2486 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
2491 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2492 struct ieee802_11_elems *elems,
2493 enum ieee80211_band band)
2495 struct ieee80211_supported_band *sband;
2496 struct ieee80211_rate *bitrates;
2500 sband = local->hw.wiphy->bands[band];
2504 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2507 bitrates = sband->bitrates;
2508 num_rates = sband->n_bitrates;
2510 for (i = 0; i < elems->supp_rates_len +
2511 elems->ext_supp_rates_len; i++) {
2514 if (i < elems->supp_rates_len)
2515 rate = elems->supp_rates[i];
2516 else if (elems->ext_supp_rates)
2517 rate = elems->ext_supp_rates
2518 [i - elems->supp_rates_len];
2519 own_rate = 5 * (rate & 0x7f);
2520 for (j = 0; j < num_rates; j++)
2521 if (bitrates[j].bitrate == own_rate)
2522 supp_rates |= BIT(j);
2528 static void ieee80211_rx_bss_info(struct net_device *dev,
2529 struct ieee80211_mgmt *mgmt,
2531 struct ieee80211_rx_status *rx_status,
2532 struct ieee802_11_elems *elems,
2535 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2537 struct ieee80211_sta_bss *bss;
2538 struct sta_info *sta;
2539 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2540 u64 beacon_timestamp, rx_timestamp;
2541 struct ieee80211_channel *channel;
2542 DECLARE_MAC_BUF(mac);
2543 DECLARE_MAC_BUF(mac2);
2545 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2546 return; /* ignore ProbeResp to foreign address */
2548 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2550 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2551 elems->mesh_config && mesh_matches_local(elems, dev)) {
2552 u64 rates = ieee80211_sta_get_rates(local, elems,
2555 mesh_neighbour_update(mgmt->sa, rates, dev,
2556 mesh_peer_accepts_plinks(elems, dev));
2561 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
2562 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2563 (sta = sta_info_get(local, mgmt->sa))) {
2565 u64 supp_rates = ieee80211_sta_get_rates(local, elems,
2568 prev_rates = sta->supp_rates[rx_status->band];
2569 sta->supp_rates[rx_status->band] &= supp_rates;
2570 if (sta->supp_rates[rx_status->band] == 0) {
2571 /* No matching rates - this should not really happen.
2572 * Make sure that at least one rate is marked
2573 * supported to avoid issues with TX rate ctrl. */
2574 sta->supp_rates[rx_status->band] =
2575 sdata->u.sta.supp_rates_bits[rx_status->band];
2581 if (elems->ds_params && elems->ds_params_len == 1)
2582 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
2584 freq = rx_status->freq;
2586 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2588 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2591 #ifdef CONFIG_MAC80211_MESH
2592 if (elems->mesh_config)
2593 bss = ieee80211_rx_mesh_bss_get(dev, elems->mesh_id,
2594 elems->mesh_id_len, elems->mesh_config, freq);
2597 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2598 elems->ssid, elems->ssid_len);
2600 #ifdef CONFIG_MAC80211_MESH
2601 if (elems->mesh_config)
2602 bss = ieee80211_rx_mesh_bss_add(dev, elems->mesh_id,
2603 elems->mesh_id_len, elems->mesh_config,
2604 elems->mesh_config_len, freq);
2607 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2608 elems->ssid, elems->ssid_len);
2613 /* TODO: order by RSSI? */
2614 spin_lock_bh(&local->sta_bss_lock);
2615 list_move_tail(&bss->list, &local->sta_bss_list);
2616 spin_unlock_bh(&local->sta_bss_lock);
2620 /* save the ERP value so that it is available at association time */
2621 if (elems->erp_info && elems->erp_info_len >= 1) {
2622 bss->erp_value = elems->erp_info[0];
2623 bss->has_erp_value = 1;
2626 if (elems->ht_cap_elem &&
2627 (!bss->ht_ie || bss->ht_ie_len != elems->ht_cap_elem_len ||
2628 memcmp(bss->ht_ie, elems->ht_cap_elem, elems->ht_cap_elem_len))) {
2630 bss->ht_ie = kmalloc(elems->ht_cap_elem_len + 2, GFP_ATOMIC);
2632 memcpy(bss->ht_ie, elems->ht_cap_elem - 2,
2633 elems->ht_cap_elem_len + 2);
2634 bss->ht_ie_len = elems->ht_cap_elem_len + 2;
2637 } else if (!elems->ht_cap_elem && bss->ht_ie) {
2643 if (elems->ht_info_elem &&
2645 bss->ht_add_ie_len != elems->ht_info_elem_len ||
2646 memcmp(bss->ht_add_ie, elems->ht_info_elem,
2647 elems->ht_info_elem_len))) {
2648 kfree(bss->ht_add_ie);
2650 kmalloc(elems->ht_info_elem_len + 2, GFP_ATOMIC);
2651 if (bss->ht_add_ie) {
2652 memcpy(bss->ht_add_ie, elems->ht_info_elem - 2,
2653 elems->ht_info_elem_len + 2);
2654 bss->ht_add_ie_len = elems->ht_info_elem_len + 2;
2656 bss->ht_add_ie_len = 0;
2657 } else if (!elems->ht_info_elem && bss->ht_add_ie) {
2658 kfree(bss->ht_add_ie);
2659 bss->ht_add_ie = NULL;
2660 bss->ht_add_ie_len = 0;
2663 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2664 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2667 struct ieee80211_tim_ie *tim_ie =
2668 (struct ieee80211_tim_ie *)elems->tim;
2669 bss->dtim_period = tim_ie->dtim_period;
2672 /* set default value for buggy APs */
2673 if (!elems->tim || bss->dtim_period == 0)
2674 bss->dtim_period = 1;
2676 bss->supp_rates_len = 0;
2677 if (elems->supp_rates) {
2678 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2679 if (clen > elems->supp_rates_len)
2680 clen = elems->supp_rates_len;
2681 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
2683 bss->supp_rates_len += clen;
2685 if (elems->ext_supp_rates) {
2686 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2687 if (clen > elems->ext_supp_rates_len)
2688 clen = elems->ext_supp_rates_len;
2689 memcpy(&bss->supp_rates[bss->supp_rates_len],
2690 elems->ext_supp_rates, clen);
2691 bss->supp_rates_len += clen;
2694 bss->band = rx_status->band;
2696 bss->timestamp = beacon_timestamp;
2697 bss->last_update = jiffies;
2698 bss->signal = rx_status->signal;
2699 bss->noise = rx_status->noise;
2700 bss->qual = rx_status->qual;
2701 if (!beacon && !bss->probe_resp)
2702 bss->probe_resp = true;
2705 * In STA mode, the remaining parameters should not be overridden
2706 * by beacons because they're not necessarily accurate there.
2708 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2709 bss->probe_resp && beacon) {
2710 ieee80211_rx_bss_put(local, bss);
2715 (!bss->wpa_ie || bss->wpa_ie_len != elems->wpa_len ||
2716 memcmp(bss->wpa_ie, elems->wpa, elems->wpa_len))) {
2718 bss->wpa_ie = kmalloc(elems->wpa_len + 2, GFP_ATOMIC);
2720 memcpy(bss->wpa_ie, elems->wpa - 2, elems->wpa_len + 2);
2721 bss->wpa_ie_len = elems->wpa_len + 2;
2723 bss->wpa_ie_len = 0;
2724 } else if (!elems->wpa && bss->wpa_ie) {
2727 bss->wpa_ie_len = 0;
2731 (!bss->rsn_ie || bss->rsn_ie_len != elems->rsn_len ||
2732 memcmp(bss->rsn_ie, elems->rsn, elems->rsn_len))) {
2734 bss->rsn_ie = kmalloc(elems->rsn_len + 2, GFP_ATOMIC);
2736 memcpy(bss->rsn_ie, elems->rsn - 2, elems->rsn_len + 2);
2737 bss->rsn_ie_len = elems->rsn_len + 2;
2739 bss->rsn_ie_len = 0;
2740 } else if (!elems->rsn && bss->rsn_ie) {
2743 bss->rsn_ie_len = 0;
2748 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2752 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2753 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2754 * Alliance (September 1, 2004) is incorporated by reference herein.
2755 * The inclusion of the WMM Parameters in probe responses and
2756 * association responses is mandatory for WMM enabled networks. The
2757 * inclusion of the WMM Parameters in beacons, however, is optional.
2760 if (elems->wmm_param &&
2761 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_param_len ||
2762 memcmp(bss->wmm_ie, elems->wmm_param, elems->wmm_param_len))) {
2764 bss->wmm_ie = kmalloc(elems->wmm_param_len + 2, GFP_ATOMIC);
2766 memcpy(bss->wmm_ie, elems->wmm_param - 2,
2767 elems->wmm_param_len + 2);
2768 bss->wmm_ie_len = elems->wmm_param_len + 2;
2770 bss->wmm_ie_len = 0;
2771 } else if (elems->wmm_info &&
2772 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_info_len ||
2773 memcmp(bss->wmm_ie, elems->wmm_info,
2774 elems->wmm_info_len))) {
2775 /* As for certain AP's Fifth bit is not set in WMM IE in
2776 * beacon frames.So while parsing the beacon frame the
2777 * wmm_info structure is used instead of wmm_param.
2778 * wmm_info structure was never used to set bss->wmm_ie.
2779 * This code fixes this problem by copying the WME
2780 * information from wmm_info to bss->wmm_ie and enabling
2781 * n-band association.
2784 bss->wmm_ie = kmalloc(elems->wmm_info_len + 2, GFP_ATOMIC);
2786 memcpy(bss->wmm_ie, elems->wmm_info - 2,
2787 elems->wmm_info_len + 2);
2788 bss->wmm_ie_len = elems->wmm_info_len + 2;
2790 bss->wmm_ie_len = 0;
2791 } else if (!elems->wmm_param && !elems->wmm_info && bss->wmm_ie) {
2794 bss->wmm_ie_len = 0;
2797 /* check if we need to merge IBSS */
2798 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2799 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2800 bss->capability & WLAN_CAPABILITY_IBSS &&
2801 bss->freq == local->oper_channel->center_freq &&
2802 elems->ssid_len == sdata->u.sta.ssid_len &&
2803 memcmp(elems->ssid, sdata->u.sta.ssid,
2804 sdata->u.sta.ssid_len) == 0) {
2805 if (rx_status->flag & RX_FLAG_TSFT) {
2806 /* in order for correct IBSS merging we need mactime
2808 * since mactime is defined as the time the first data
2809 * symbol of the frame hits the PHY, and the timestamp
2810 * of the beacon is defined as "the time that the data
2811 * symbol containing the first bit of the timestamp is
2812 * transmitted to the PHY plus the transmitting STA’s
2813 * delays through its local PHY from the MAC-PHY
2814 * interface to its interface with the WM"
2815 * (802.11 11.1.2) - equals the time this bit arrives at
2816 * the receiver - we have to take into account the
2817 * offset between the two.
2818 * e.g: at 1 MBit that means mactime is 192 usec earlier
2819 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2821 int rate = local->hw.wiphy->bands[rx_status->band]->
2822 bitrates[rx_status->rate_idx].bitrate;
2823 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2824 } else if (local && local->ops && local->ops->get_tsf)
2825 /* second best option: get current TSF */
2826 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2828 /* can't merge without knowing the TSF */
2829 rx_timestamp = -1LLU;
2830 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2831 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2832 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2833 print_mac(mac, mgmt->sa),
2834 print_mac(mac2, mgmt->bssid),
2835 (unsigned long long)rx_timestamp,
2836 (unsigned long long)beacon_timestamp,
2837 (unsigned long long)(rx_timestamp - beacon_timestamp),
2839 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2840 if (beacon_timestamp > rx_timestamp) {
2841 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2842 printk(KERN_DEBUG "%s: beacon TSF higher than "
2843 "local TSF - IBSS merge with BSSID %s\n",
2844 dev->name, print_mac(mac, mgmt->bssid));
2846 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2847 ieee80211_ibss_add_sta(dev, NULL,
2848 mgmt->bssid, mgmt->sa,
2849 BIT(rx_status->rate_idx));
2853 ieee80211_rx_bss_put(local, bss);
2857 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2858 struct ieee80211_mgmt *mgmt,
2860 struct ieee80211_rx_status *rx_status)
2863 struct ieee802_11_elems elems;
2865 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2869 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2872 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 0);
2876 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2877 struct ieee80211_mgmt *mgmt,
2879 struct ieee80211_rx_status *rx_status)
2881 struct ieee80211_sub_if_data *sdata;
2882 struct ieee80211_if_sta *ifsta;
2884 struct ieee802_11_elems elems;
2885 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2886 struct ieee80211_conf *conf = &local->hw.conf;
2889 /* Process beacon from the current BSS */
2890 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2894 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2896 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 1);
2898 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2899 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2901 ifsta = &sdata->u.sta;
2903 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2904 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2907 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2908 elems.wmm_param_len);
2910 /* Do not send changes to driver if we are scanning. This removes
2911 * requirement that driver's bss_info_changed function needs to be
2913 if (local->sta_sw_scanning || local->sta_hw_scanning)
2916 if (elems.erp_info && elems.erp_info_len >= 1)
2917 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2919 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2920 changed |= ieee80211_handle_protect_preamb(sdata, false,
2921 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2924 if (elems.ht_cap_elem && elems.ht_info_elem &&
2925 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2926 struct ieee80211_ht_bss_info bss_info;
2928 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2929 (struct ieee80211_ht_addt_info *)
2930 elems.ht_info_elem, &bss_info);
2931 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2935 ieee80211_bss_info_change_notify(sdata, changed);
2939 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2940 struct ieee80211_if_sta *ifsta,
2941 struct ieee80211_mgmt *mgmt,
2943 struct ieee80211_rx_status *rx_status)
2945 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2946 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2948 struct sk_buff *skb;
2949 struct ieee80211_mgmt *resp;
2951 DECLARE_MAC_BUF(mac);
2952 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2953 DECLARE_MAC_BUF(mac2);
2954 DECLARE_MAC_BUF(mac3);
2957 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2958 ifsta->state != IEEE80211_IBSS_JOINED ||
2959 len < 24 + 2 || !ifsta->probe_resp)
2962 if (local->ops->tx_last_beacon)
2963 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2967 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2968 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2969 "%s (tx_last_beacon=%d)\n",
2970 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2971 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2972 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2974 if (!tx_last_beacon)
2977 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2978 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2981 end = ((u8 *) mgmt) + len;
2982 pos = mgmt->u.probe_req.variable;
2983 if (pos[0] != WLAN_EID_SSID ||
2984 pos + 2 + pos[1] > end) {
2985 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2986 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2988 dev->name, print_mac(mac, mgmt->sa));
2993 (pos[1] != ifsta->ssid_len ||
2994 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2995 /* Ignore ProbeReq for foreign SSID */
2999 /* Reply with ProbeResp */
3000 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
3004 resp = (struct ieee80211_mgmt *) skb->data;
3005 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3006 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3007 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3008 dev->name, print_mac(mac, resp->da));
3009 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3010 ieee80211_sta_tx(dev, skb, 0);
3013 static void ieee80211_rx_mgmt_action(struct net_device *dev,
3014 struct ieee80211_if_sta *ifsta,
3015 struct ieee80211_mgmt *mgmt,
3017 struct ieee80211_rx_status *rx_status)
3019 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3020 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3022 if (len < IEEE80211_MIN_ACTION_SIZE)
3025 switch (mgmt->u.action.category) {
3026 case WLAN_CATEGORY_SPECTRUM_MGMT:
3027 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
3029 switch (mgmt->u.action.u.chan_switch.action_code) {
3030 case WLAN_ACTION_SPCT_MSR_REQ:
3031 if (len < (IEEE80211_MIN_ACTION_SIZE +
3032 sizeof(mgmt->u.action.u.measurement)))
3034 ieee80211_sta_process_measurement_req(dev, mgmt, len);
3038 case WLAN_CATEGORY_BACK:
3039 switch (mgmt->u.action.u.addba_req.action_code) {
3040 case WLAN_ACTION_ADDBA_REQ:
3041 if (len < (IEEE80211_MIN_ACTION_SIZE +
3042 sizeof(mgmt->u.action.u.addba_req)))
3044 ieee80211_sta_process_addba_request(dev, mgmt, len);
3046 case WLAN_ACTION_ADDBA_RESP:
3047 if (len < (IEEE80211_MIN_ACTION_SIZE +
3048 sizeof(mgmt->u.action.u.addba_resp)))
3050 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3052 case WLAN_ACTION_DELBA:
3053 if (len < (IEEE80211_MIN_ACTION_SIZE +
3054 sizeof(mgmt->u.action.u.delba)))
3056 ieee80211_sta_process_delba(dev, mgmt, len);
3060 case PLINK_CATEGORY:
3061 if (ieee80211_vif_is_mesh(&sdata->vif))
3062 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3064 case MESH_PATH_SEL_CATEGORY:
3065 if (ieee80211_vif_is_mesh(&sdata->vif))
3066 mesh_rx_path_sel_frame(dev, mgmt, len);
3071 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3072 struct ieee80211_rx_status *rx_status)
3074 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3075 struct ieee80211_sub_if_data *sdata;
3076 struct ieee80211_if_sta *ifsta;
3077 struct ieee80211_mgmt *mgmt;
3083 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3084 ifsta = &sdata->u.sta;
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 case IEEE80211_STYPE_PROBE_RESP:
3092 case IEEE80211_STYPE_BEACON:
3093 case IEEE80211_STYPE_ACTION:
3094 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3095 case IEEE80211_STYPE_AUTH:
3096 case IEEE80211_STYPE_ASSOC_RESP:
3097 case IEEE80211_STYPE_REASSOC_RESP:
3098 case IEEE80211_STYPE_DEAUTH:
3099 case IEEE80211_STYPE_DISASSOC:
3100 skb_queue_tail(&ifsta->skb_queue, skb);
3101 queue_work(local->hw.workqueue, &ifsta->work);
3110 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3111 struct sk_buff *skb)
3113 struct ieee80211_rx_status *rx_status;
3114 struct ieee80211_sub_if_data *sdata;
3115 struct ieee80211_if_sta *ifsta;
3116 struct ieee80211_mgmt *mgmt;
3119 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3120 ifsta = &sdata->u.sta;
3122 rx_status = (struct ieee80211_rx_status *) skb->cb;
3123 mgmt = (struct ieee80211_mgmt *) skb->data;
3124 fc = le16_to_cpu(mgmt->frame_control);
3126 switch (fc & IEEE80211_FCTL_STYPE) {
3127 case IEEE80211_STYPE_PROBE_REQ:
3128 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3131 case IEEE80211_STYPE_PROBE_RESP:
3132 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3134 case IEEE80211_STYPE_BEACON:
3135 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3137 case IEEE80211_STYPE_AUTH:
3138 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3140 case IEEE80211_STYPE_ASSOC_RESP:
3141 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
3143 case IEEE80211_STYPE_REASSOC_RESP:
3144 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
3146 case IEEE80211_STYPE_DEAUTH:
3147 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3149 case IEEE80211_STYPE_DISASSOC:
3150 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3152 case IEEE80211_STYPE_ACTION:
3153 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
3162 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3163 struct ieee80211_rx_status *rx_status)
3165 struct ieee80211_mgmt *mgmt;
3169 return RX_DROP_UNUSABLE;
3171 mgmt = (struct ieee80211_mgmt *) skb->data;
3172 fc = mgmt->frame_control;
3174 if (ieee80211_is_ctl(fc))
3178 return RX_DROP_MONITOR;
3180 if (ieee80211_is_probe_resp(fc)) {
3181 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3186 if (ieee80211_is_beacon(fc)) {
3187 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3196 static int ieee80211_sta_active_ibss(struct net_device *dev)
3198 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3200 struct sta_info *sta;
3201 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3205 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3206 if (sta->sdata == sdata &&
3207 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3220 static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
3222 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3223 struct sta_info *sta, *tmp;
3224 LIST_HEAD(tmp_list);
3225 DECLARE_MAC_BUF(mac);
3226 unsigned long flags;
3228 spin_lock_irqsave(&local->sta_lock, flags);
3229 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3230 if (time_after(jiffies, sta->last_rx + exp_time)) {
3231 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3232 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3233 dev->name, print_mac(mac, sta->addr));
3235 __sta_info_unlink(&sta);
3237 list_add(&sta->list, &tmp_list);
3239 spin_unlock_irqrestore(&local->sta_lock, flags);
3241 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3242 sta_info_destroy(sta);
3246 static void ieee80211_sta_merge_ibss(struct net_device *dev,
3247 struct ieee80211_if_sta *ifsta)
3249 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3251 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
3252 if (ieee80211_sta_active_ibss(dev))
3255 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3256 "IBSS networks with same SSID (merge)\n", dev->name);
3257 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3261 #ifdef CONFIG_MAC80211_MESH
3262 static void ieee80211_mesh_housekeeping(struct net_device *dev,
3263 struct ieee80211_if_sta *ifsta)
3265 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3268 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3269 mesh_path_expire(dev);
3271 free_plinks = mesh_plink_availables(sdata);
3272 if (free_plinks != sdata->u.sta.accepting_plinks)
3273 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3275 mod_timer(&ifsta->timer, jiffies +
3276 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3280 void ieee80211_start_mesh(struct net_device *dev)
3282 struct ieee80211_if_sta *ifsta;
3283 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3284 ifsta = &sdata->u.sta;
3285 ifsta->state = IEEE80211_MESH_UP;
3286 ieee80211_sta_timer((unsigned long)sdata);
3287 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3292 void ieee80211_sta_timer(unsigned long data)
3294 struct ieee80211_sub_if_data *sdata =
3295 (struct ieee80211_sub_if_data *) data;
3296 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3297 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3299 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3300 queue_work(local->hw.workqueue, &ifsta->work);
3303 void ieee80211_sta_work(struct work_struct *work)
3305 struct ieee80211_sub_if_data *sdata =
3306 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3307 struct net_device *dev = sdata->dev;
3308 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3309 struct ieee80211_if_sta *ifsta;
3310 struct sk_buff *skb;
3312 if (!netif_running(dev))
3315 if (local->sta_sw_scanning || local->sta_hw_scanning)
3318 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3319 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3320 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
3322 ifsta = &sdata->u.sta;
3324 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3325 ieee80211_sta_rx_queued_mgmt(dev, skb);
3327 #ifdef CONFIG_MAC80211_MESH
3328 if (ifsta->preq_queue_len &&
3330 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3331 mesh_path_start_discovery(dev);
3334 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3335 ifsta->state != IEEE80211_ASSOCIATE &&
3336 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3337 if (ifsta->scan_ssid_len)
3338 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3340 ieee80211_sta_start_scan(dev, NULL, 0);
3344 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3345 if (ieee80211_sta_config_auth(dev, ifsta))
3347 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3348 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3351 switch (ifsta->state) {
3352 case IEEE80211_DISABLED:
3354 case IEEE80211_AUTHENTICATE:
3355 ieee80211_authenticate(dev, ifsta);
3357 case IEEE80211_ASSOCIATE:
3358 ieee80211_associate(dev, ifsta);
3360 case IEEE80211_ASSOCIATED:
3361 ieee80211_associated(dev, ifsta);
3363 case IEEE80211_IBSS_SEARCH:
3364 ieee80211_sta_find_ibss(dev, ifsta);
3366 case IEEE80211_IBSS_JOINED:
3367 ieee80211_sta_merge_ibss(dev, ifsta);
3369 #ifdef CONFIG_MAC80211_MESH
3370 case IEEE80211_MESH_UP:
3371 ieee80211_mesh_housekeeping(dev, ifsta);
3379 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3380 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3381 "mixed-cell disabled - disassociate\n", dev->name);
3383 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3384 ieee80211_set_disassoc(dev, ifsta, 0);
3389 static void ieee80211_sta_reset_auth(struct net_device *dev,
3390 struct ieee80211_if_sta *ifsta)
3392 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3394 if (local->ops->reset_tsf) {
3395 /* Reset own TSF to allow time synchronization work. */
3396 local->ops->reset_tsf(local_to_hw(local));
3399 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3402 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3403 ifsta->auth_alg = WLAN_AUTH_OPEN;
3404 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3405 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3406 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3407 ifsta->auth_alg = WLAN_AUTH_LEAP;
3409 ifsta->auth_alg = WLAN_AUTH_OPEN;
3410 ifsta->auth_transaction = -1;
3411 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3412 ifsta->auth_tries = ifsta->assoc_tries = 0;
3413 netif_carrier_off(dev);
3417 void ieee80211_sta_req_auth(struct net_device *dev,
3418 struct ieee80211_if_sta *ifsta)
3420 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3421 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3423 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3426 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3427 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3428 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3429 IEEE80211_STA_AUTO_SSID_SEL))) {
3430 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3431 queue_work(local->hw.workqueue, &ifsta->work);
3435 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3436 const char *ssid, int ssid_len)
3438 int tmp, hidden_ssid;
3440 if (ssid_len == ifsta->ssid_len &&
3441 !memcmp(ifsta->ssid, ssid, ssid_len))
3444 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3450 if (ssid[tmp] != '\0') {
3456 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3459 if (ssid_len == 1 && ssid[0] == ' ')
3465 static int ieee80211_sta_config_auth(struct net_device *dev,
3466 struct ieee80211_if_sta *ifsta)
3468 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3469 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3470 struct ieee80211_sta_bss *bss, *selected = NULL;
3471 int top_rssi = 0, freq;
3473 spin_lock_bh(&local->sta_bss_lock);
3474 freq = local->oper_channel->center_freq;
3475 list_for_each_entry(bss, &local->sta_bss_list, list) {
3476 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3479 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3480 IEEE80211_STA_AUTO_BSSID_SEL |
3481 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3482 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3483 !!sdata->default_key))
3486 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3490 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3491 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3494 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3495 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3498 if (!selected || top_rssi < bss->signal) {
3500 top_rssi = bss->signal;
3504 atomic_inc(&selected->users);
3505 spin_unlock_bh(&local->sta_bss_lock);
3508 ieee80211_set_freq(dev, selected->freq);
3509 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3510 ieee80211_sta_set_ssid(dev, selected->ssid,
3511 selected->ssid_len);
3512 ieee80211_sta_set_bssid(dev, selected->bssid);
3513 ieee80211_sta_def_wmm_params(dev, selected, 0);
3514 ieee80211_rx_bss_put(local, selected);
3515 ifsta->state = IEEE80211_AUTHENTICATE;
3516 ieee80211_sta_reset_auth(dev, ifsta);
3519 if (ifsta->state != IEEE80211_AUTHENTICATE) {
3520 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3521 ieee80211_sta_start_scan(dev, NULL, 0);
3523 ieee80211_sta_start_scan(dev, ifsta->ssid,
3525 ifsta->state = IEEE80211_AUTHENTICATE;
3526 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3528 ifsta->state = IEEE80211_DISABLED;
3534 static int ieee80211_sta_create_ibss(struct net_device *dev,
3535 struct ieee80211_if_sta *ifsta)
3537 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3538 struct ieee80211_sta_bss *bss;
3539 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3540 struct ieee80211_supported_band *sband;
3541 u8 bssid[ETH_ALEN], *pos;
3544 DECLARE_MAC_BUF(mac);
3547 /* Easier testing, use fixed BSSID. */
3548 memset(bssid, 0xfe, ETH_ALEN);
3550 /* Generate random, not broadcast, locally administered BSSID. Mix in
3551 * own MAC address to make sure that devices that do not have proper
3552 * random number generator get different BSSID. */
3553 get_random_bytes(bssid, ETH_ALEN);
3554 for (i = 0; i < ETH_ALEN; i++)
3555 bssid[i] ^= dev->dev_addr[i];
3560 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3561 dev->name, print_mac(mac, bssid));
3563 bss = ieee80211_rx_bss_add(dev, bssid,
3564 local->hw.conf.channel->center_freq,
3565 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3569 bss->band = local->hw.conf.channel->band;
3570 sband = local->hw.wiphy->bands[bss->band];
3572 if (local->hw.conf.beacon_int == 0)
3573 local->hw.conf.beacon_int = 100;
3574 bss->beacon_int = local->hw.conf.beacon_int;
3575 bss->last_update = jiffies;
3576 bss->capability = WLAN_CAPABILITY_IBSS;
3578 if (sdata->default_key)
3579 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3581 sdata->drop_unencrypted = 0;
3583 bss->supp_rates_len = sband->n_bitrates;
3584 pos = bss->supp_rates;
3585 for (i = 0; i < sband->n_bitrates; i++) {
3586 int rate = sband->bitrates[i].bitrate;
3587 *pos++ = (u8) (rate / 5);
3590 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3591 ieee80211_rx_bss_put(local, bss);
3596 static int ieee80211_sta_find_ibss(struct net_device *dev,
3597 struct ieee80211_if_sta *ifsta)
3599 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3600 struct ieee80211_sta_bss *bss;
3604 DECLARE_MAC_BUF(mac);
3605 DECLARE_MAC_BUF(mac2);
3607 if (ifsta->ssid_len == 0)
3610 active_ibss = ieee80211_sta_active_ibss(dev);
3611 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3612 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3613 dev->name, active_ibss);
3614 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3615 spin_lock_bh(&local->sta_bss_lock);
3616 list_for_each_entry(bss, &local->sta_bss_list, list) {
3617 if (ifsta->ssid_len != bss->ssid_len ||
3618 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3619 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3621 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3622 printk(KERN_DEBUG " bssid=%s found\n",
3623 print_mac(mac, bss->bssid));
3624 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3625 memcpy(bssid, bss->bssid, ETH_ALEN);
3627 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3630 spin_unlock_bh(&local->sta_bss_lock);
3632 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3634 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3635 "%s\n", print_mac(mac, bssid),
3636 print_mac(mac2, ifsta->bssid));
3637 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3639 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3643 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3644 search_freq = bss->freq;
3646 search_freq = local->hw.conf.channel->center_freq;
3648 bss = ieee80211_rx_bss_get(dev, bssid, search_freq,
3649 ifsta->ssid, ifsta->ssid_len);
3653 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3654 " based on configured SSID\n",
3655 dev->name, print_mac(mac, bssid));
3656 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3657 ieee80211_rx_bss_put(local, bss);
3662 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3663 printk(KERN_DEBUG " did not try to join ibss\n");
3664 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3666 /* Selected IBSS not found in current scan results - try to scan */
3667 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3668 !ieee80211_sta_active_ibss(dev)) {
3669 mod_timer(&ifsta->timer, jiffies +
3670 IEEE80211_IBSS_MERGE_INTERVAL);
3671 } else if (time_after(jiffies, local->last_scan_completed +
3672 IEEE80211_SCAN_INTERVAL)) {
3673 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3674 "join\n", dev->name);
3675 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3677 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3678 int interval = IEEE80211_SCAN_INTERVAL;
3680 if (time_after(jiffies, ifsta->ibss_join_req +
3681 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3682 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3683 (!(local->oper_channel->flags &
3684 IEEE80211_CHAN_NO_IBSS)))
3685 return ieee80211_sta_create_ibss(dev, ifsta);
3686 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3687 printk(KERN_DEBUG "%s: IBSS not allowed on"
3688 " %d MHz\n", dev->name,
3689 local->hw.conf.channel->center_freq);
3692 /* No IBSS found - decrease scan interval and continue
3694 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3697 ifsta->state = IEEE80211_IBSS_SEARCH;
3698 mod_timer(&ifsta->timer, jiffies + interval);
3706 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3708 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3709 struct ieee80211_if_sta *ifsta;
3712 if (len > IEEE80211_MAX_SSID_LEN)
3715 ifsta = &sdata->u.sta;
3717 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3718 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3719 memcpy(ifsta->ssid, ssid, len);
3720 ifsta->ssid_len = len;
3721 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3725 * Hack! MLME code needs to be cleaned up to have different
3726 * entry points for configuration and internal selection change
3728 if (netif_running(sdata->dev))
3729 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3731 printk(KERN_DEBUG "%s: Failed to config new SSID to "
3732 "the low-level driver\n", dev->name);
3738 ifsta->flags |= IEEE80211_STA_SSID_SET;
3740 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3742 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3743 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3744 ifsta->ibss_join_req = jiffies;
3745 ifsta->state = IEEE80211_IBSS_SEARCH;
3746 return ieee80211_sta_find_ibss(dev, ifsta);
3753 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3755 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3756 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3757 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3758 *len = ifsta->ssid_len;
3763 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3765 struct ieee80211_sub_if_data *sdata;
3766 struct ieee80211_if_sta *ifsta;
3769 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3770 ifsta = &sdata->u.sta;
3772 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3773 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3776 * Hack! See also ieee80211_sta_set_ssid.
3778 if (netif_running(sdata->dev))
3779 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
3781 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3782 "the low-level driver\n", dev->name);
3787 if (is_valid_ether_addr(bssid))
3788 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3790 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3796 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3797 struct ieee80211_sub_if_data *sdata,
3800 struct sk_buff *skb;
3801 struct ieee80211_hdr *nullfunc;
3804 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3806 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3807 "frame\n", sdata->dev->name);
3810 skb_reserve(skb, local->hw.extra_tx_headroom);
3812 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3813 memset(nullfunc, 0, 24);
3814 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3815 IEEE80211_FCTL_TODS);
3817 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3818 nullfunc->frame_control = fc;
3819 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3820 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3821 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3823 ieee80211_sta_tx(sdata->dev, skb, 0);
3827 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3829 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3830 ieee80211_vif_is_mesh(&sdata->vif))
3831 ieee80211_sta_timer((unsigned long)sdata);
3834 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3836 struct ieee80211_local *local = hw_to_local(hw);
3837 struct net_device *dev = local->scan_dev;
3838 struct ieee80211_sub_if_data *sdata;
3839 union iwreq_data wrqu;
3841 local->last_scan_completed = jiffies;
3842 memset(&wrqu, 0, sizeof(wrqu));
3843 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3845 if (local->sta_hw_scanning) {
3846 local->sta_hw_scanning = 0;
3847 if (ieee80211_hw_config(local))
3848 printk(KERN_DEBUG "%s: failed to restore operational "
3849 "channel after scan\n", dev->name);
3850 /* Restart STA timer for HW scan case */
3852 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3853 ieee80211_restart_sta_timer(sdata);
3859 local->sta_sw_scanning = 0;
3860 if (ieee80211_hw_config(local))
3861 printk(KERN_DEBUG "%s: failed to restore operational "
3862 "channel after scan\n", dev->name);
3865 netif_tx_lock_bh(local->mdev);
3866 netif_addr_lock(local->mdev);
3867 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3868 local->ops->configure_filter(local_to_hw(local),
3869 FIF_BCN_PRBRESP_PROMISC,
3870 &local->filter_flags,
3871 local->mdev->mc_count,
3872 local->mdev->mc_list);
3874 netif_addr_unlock(local->mdev);
3875 netif_tx_unlock_bh(local->mdev);
3878 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3879 /* Tell AP we're back */
3880 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3881 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3882 ieee80211_send_nullfunc(local, sdata, 0);
3884 ieee80211_restart_sta_timer(sdata);
3886 netif_wake_queue(sdata->dev);
3891 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3892 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3893 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3894 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3895 (!(ifsta->state == IEEE80211_IBSS_JOINED) &&
3896 !ieee80211_sta_active_ibss(dev)))
3897 ieee80211_sta_find_ibss(dev, ifsta);
3900 EXPORT_SYMBOL(ieee80211_scan_completed);
3902 void ieee80211_sta_scan_work(struct work_struct *work)
3904 struct ieee80211_local *local =
3905 container_of(work, struct ieee80211_local, scan_work.work);
3906 struct net_device *dev = local->scan_dev;
3907 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3908 struct ieee80211_supported_band *sband;
3909 struct ieee80211_channel *chan;
3911 unsigned long next_delay = 0;
3913 if (!local->sta_sw_scanning)
3916 switch (local->scan_state) {
3917 case SCAN_SET_CHANNEL:
3919 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3920 * after we successfully scanned the last channel of the last
3921 * band (and the last band is supported by the hw)
3923 if (local->scan_band < IEEE80211_NUM_BANDS)
3924 sband = local->hw.wiphy->bands[local->scan_band];
3929 * If we are at an unsupported band and have more bands
3930 * left to scan, advance to the next supported one.
3932 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3934 sband = local->hw.wiphy->bands[local->scan_band];
3935 local->scan_channel_idx = 0;
3938 /* if no more bands/channels left, complete scan */
3939 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3940 ieee80211_scan_completed(local_to_hw(local));
3944 chan = &sband->channels[local->scan_channel_idx];
3946 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3947 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3948 chan->flags & IEEE80211_CHAN_NO_IBSS))
3952 local->scan_channel = chan;
3953 if (ieee80211_hw_config(local)) {
3954 printk(KERN_DEBUG "%s: failed to set freq to "
3955 "%d MHz for scan\n", dev->name,
3961 /* advance state machine to next channel/band */
3962 local->scan_channel_idx++;
3963 if (local->scan_channel_idx >= sband->n_channels) {
3965 * scan_band may end up == IEEE80211_NUM_BANDS, but
3966 * we'll catch that case above and complete the scan
3967 * if that is the case.
3970 local->scan_channel_idx = 0;
3976 next_delay = IEEE80211_PROBE_DELAY +
3977 usecs_to_jiffies(local->hw.channel_change_time);
3978 local->scan_state = SCAN_SEND_PROBE;
3980 case SCAN_SEND_PROBE:
3981 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3982 local->scan_state = SCAN_SET_CHANNEL;
3984 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3986 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3987 local->scan_ssid_len);
3988 next_delay = IEEE80211_CHANNEL_TIME;
3992 if (local->sta_sw_scanning)
3993 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3998 static int ieee80211_sta_start_scan(struct net_device *dev,
3999 u8 *ssid, size_t ssid_len)
4001 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4002 struct ieee80211_sub_if_data *sdata;
4004 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4007 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
4008 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
4011 * ScanType: ACTIVE, PASSIVE
4012 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
4013 * a Probe frame during active scanning
4015 * MinChannelTime (>= ProbeDelay), in TU
4016 * MaxChannelTime: (>= MinChannelTime), in TU
4019 /* MLME-SCAN.confirm
4021 * ResultCode: SUCCESS, INVALID_PARAMETERS
4024 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4025 if (local->scan_dev == dev)
4030 if (local->ops->hw_scan) {
4031 int rc = local->ops->hw_scan(local_to_hw(local),
4034 local->sta_hw_scanning = 1;
4035 local->scan_dev = dev;
4040 local->sta_sw_scanning = 1;
4043 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4044 netif_stop_queue(sdata->dev);
4045 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
4046 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
4047 ieee80211_send_nullfunc(local, sdata, 1);
4052 local->scan_ssid_len = ssid_len;
4053 memcpy(local->scan_ssid, ssid, ssid_len);
4055 local->scan_ssid_len = 0;
4056 local->scan_state = SCAN_SET_CHANNEL;
4057 local->scan_channel_idx = 0;
4058 local->scan_band = IEEE80211_BAND_2GHZ;
4059 local->scan_dev = dev;
4061 netif_addr_lock_bh(local->mdev);
4062 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4063 local->ops->configure_filter(local_to_hw(local),
4064 FIF_BCN_PRBRESP_PROMISC,
4065 &local->filter_flags,
4066 local->mdev->mc_count,
4067 local->mdev->mc_list);
4068 netif_addr_unlock_bh(local->mdev);
4070 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4071 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4072 IEEE80211_CHANNEL_TIME);
4078 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4080 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4081 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4082 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4084 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4085 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4087 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4088 if (local->scan_dev == dev)
4093 ifsta->scan_ssid_len = ssid_len;
4095 memcpy(ifsta->scan_ssid, ssid, ssid_len);
4096 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4097 queue_work(local->hw.workqueue, &ifsta->work);
4102 ieee80211_sta_scan_result(struct net_device *dev,
4103 struct iw_request_info *info,
4104 struct ieee80211_sta_bss *bss,
4105 char *current_ev, char *end_buf)
4107 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4108 struct iw_event iwe;
4110 if (time_after(jiffies,
4111 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4114 memset(&iwe, 0, sizeof(iwe));
4115 iwe.cmd = SIOCGIWAP;
4116 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4117 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4118 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4121 memset(&iwe, 0, sizeof(iwe));
4122 iwe.cmd = SIOCGIWESSID;
4123 if (bss_mesh_cfg(bss)) {
4124 iwe.u.data.length = bss_mesh_id_len(bss);
4125 iwe.u.data.flags = 1;
4126 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4127 &iwe, bss_mesh_id(bss));
4129 iwe.u.data.length = bss->ssid_len;
4130 iwe.u.data.flags = 1;
4131 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4135 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4136 || bss_mesh_cfg(bss)) {
4137 memset(&iwe, 0, sizeof(iwe));
4138 iwe.cmd = SIOCGIWMODE;
4139 if (bss_mesh_cfg(bss))
4140 iwe.u.mode = IW_MODE_MESH;
4141 else if (bss->capability & WLAN_CAPABILITY_ESS)
4142 iwe.u.mode = IW_MODE_MASTER;
4144 iwe.u.mode = IW_MODE_ADHOC;
4145 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4146 &iwe, IW_EV_UINT_LEN);
4149 memset(&iwe, 0, sizeof(iwe));
4150 iwe.cmd = SIOCGIWFREQ;
4151 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4153 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4156 memset(&iwe, 0, sizeof(iwe));
4157 iwe.cmd = SIOCGIWFREQ;
4158 iwe.u.freq.m = bss->freq;
4160 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4162 memset(&iwe, 0, sizeof(iwe));
4164 iwe.u.qual.qual = bss->qual;
4165 iwe.u.qual.level = bss->signal;
4166 iwe.u.qual.noise = bss->noise;
4167 iwe.u.qual.updated = local->wstats_flags;
4168 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4171 memset(&iwe, 0, sizeof(iwe));
4172 iwe.cmd = SIOCGIWENCODE;
4173 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4174 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4176 iwe.u.data.flags = IW_ENCODE_DISABLED;
4177 iwe.u.data.length = 0;
4178 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4181 if (bss && bss->wpa_ie) {
4182 memset(&iwe, 0, sizeof(iwe));
4183 iwe.cmd = IWEVGENIE;
4184 iwe.u.data.length = bss->wpa_ie_len;
4185 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4189 if (bss && bss->rsn_ie) {
4190 memset(&iwe, 0, sizeof(iwe));
4191 iwe.cmd = IWEVGENIE;
4192 iwe.u.data.length = bss->rsn_ie_len;
4193 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4197 if (bss && bss->ht_ie) {
4198 memset(&iwe, 0, sizeof(iwe));
4199 iwe.cmd = IWEVGENIE;
4200 iwe.u.data.length = bss->ht_ie_len;
4201 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4205 if (bss && bss->supp_rates_len > 0) {
4206 /* display all supported rates in readable format */
4207 char *p = current_ev + iwe_stream_lcp_len(info);
4210 memset(&iwe, 0, sizeof(iwe));
4211 iwe.cmd = SIOCGIWRATE;
4212 /* Those two flags are ignored... */
4213 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4215 for (i = 0; i < bss->supp_rates_len; i++) {
4216 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4218 p = iwe_stream_add_value(info, current_ev, p,
4219 end_buf, &iwe, IW_EV_PARAM_LEN);
4226 buf = kmalloc(30, GFP_ATOMIC);
4228 memset(&iwe, 0, sizeof(iwe));
4229 iwe.cmd = IWEVCUSTOM;
4230 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4231 iwe.u.data.length = strlen(buf);
4232 current_ev = iwe_stream_add_point(info, current_ev,
4235 memset(&iwe, 0, sizeof(iwe));
4236 iwe.cmd = IWEVCUSTOM;
4237 sprintf(buf, " Last beacon: %dms ago",
4238 jiffies_to_msecs(jiffies - bss->last_update));
4239 iwe.u.data.length = strlen(buf);
4240 current_ev = iwe_stream_add_point(info, current_ev,
4241 end_buf, &iwe, buf);
4246 if (bss_mesh_cfg(bss)) {
4248 u8 *cfg = bss_mesh_cfg(bss);
4249 buf = kmalloc(50, GFP_ATOMIC);
4251 memset(&iwe, 0, sizeof(iwe));
4252 iwe.cmd = IWEVCUSTOM;
4253 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4254 iwe.u.data.length = strlen(buf);
4255 current_ev = iwe_stream_add_point(info, current_ev,
4258 sprintf(buf, "Path Selection Protocol ID: "
4259 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4261 iwe.u.data.length = strlen(buf);
4262 current_ev = iwe_stream_add_point(info, current_ev,
4265 sprintf(buf, "Path Selection Metric ID: "
4266 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4268 iwe.u.data.length = strlen(buf);
4269 current_ev = iwe_stream_add_point(info, current_ev,
4272 sprintf(buf, "Congestion Control Mode ID: "
4273 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4275 iwe.u.data.length = strlen(buf);
4276 current_ev = iwe_stream_add_point(info, current_ev,
4279 sprintf(buf, "Channel Precedence: "
4280 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4282 iwe.u.data.length = strlen(buf);
4283 current_ev = iwe_stream_add_point(info, current_ev,
4294 int ieee80211_sta_scan_results(struct net_device *dev,
4295 struct iw_request_info *info,
4296 char *buf, size_t len)
4298 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4299 char *current_ev = buf;
4300 char *end_buf = buf + len;
4301 struct ieee80211_sta_bss *bss;
4303 spin_lock_bh(&local->sta_bss_lock);
4304 list_for_each_entry(bss, &local->sta_bss_list, list) {
4305 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4306 spin_unlock_bh(&local->sta_bss_lock);
4309 current_ev = ieee80211_sta_scan_result(dev, info, bss,
4310 current_ev, end_buf);
4312 spin_unlock_bh(&local->sta_bss_lock);
4313 return current_ev - buf;
4317 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4319 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4320 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4322 kfree(ifsta->extra_ie);
4324 ifsta->extra_ie = NULL;
4325 ifsta->extra_ie_len = 0;
4328 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4329 if (!ifsta->extra_ie) {
4330 ifsta->extra_ie_len = 0;
4333 memcpy(ifsta->extra_ie, ie, len);
4334 ifsta->extra_ie_len = len;
4339 struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4340 struct sk_buff *skb, u8 *bssid,
4341 u8 *addr, u64 supp_rates)
4343 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4344 struct sta_info *sta;
4345 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4346 DECLARE_MAC_BUF(mac);
4347 int band = local->hw.conf.channel->band;
4349 /* TODO: Could consider removing the least recently used entry and
4350 * allow new one to be added. */
4351 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4352 if (net_ratelimit()) {
4353 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4354 "entry %s\n", dev->name, print_mac(mac, addr));
4359 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
4362 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4363 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4364 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
4367 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4371 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
4374 sta->supp_rates[band] = supp_rates;
4376 sta->supp_rates[band] = sdata->u.sta.supp_rates_bits[band];
4378 rate_control_rate_init(sta, local);
4380 if (sta_info_insert(sta))
4387 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4389 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4390 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4392 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
4395 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4396 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4399 ieee80211_send_deauth(dev, ifsta, reason);
4400 ieee80211_set_disassoc(dev, ifsta, 1);
4405 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4407 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4408 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4410 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
4413 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4416 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4419 ieee80211_send_disassoc(dev, ifsta, reason);
4420 ieee80211_set_disassoc(dev, ifsta, 0);
4424 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4425 enum ieee80211_notification_types notif_type)
4427 struct ieee80211_local *local = hw_to_local(hw);
4428 struct ieee80211_sub_if_data *sdata;
4430 switch (notif_type) {
4431 case IEEE80211_NOTIFY_RE_ASSOC:
4433 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4434 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4437 ieee80211_sta_req_auth(sdata->dev, &sdata->u.sta);
4443 EXPORT_SYMBOL(ieee80211_notify_mac);