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 net_device *dev,
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;
217 static int ecw2cw(int ecw)
219 return (1 << ecw) - 1;
223 static void ieee80211_sta_def_wmm_params(struct net_device *dev,
224 struct ieee80211_sta_bss *bss,
227 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
228 struct ieee80211_local *local = sdata->local;
229 int i, have_higher_than_11mbit = 0;
232 /* cf. IEEE 802.11 9.2.12 */
233 for (i = 0; i < bss->supp_rates_len; i++)
234 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
235 have_higher_than_11mbit = 1;
237 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
238 have_higher_than_11mbit)
239 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
241 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
244 if (local->ops->conf_tx) {
245 struct ieee80211_tx_queue_params qparam;
247 memset(&qparam, 0, sizeof(qparam));
251 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
252 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
257 qparam.cw_max = 1023;
260 for (i = 0; i < local_to_hw(local)->queues; i++)
261 local->ops->conf_tx(local_to_hw(local), i, &qparam);
265 static void ieee80211_sta_wmm_params(struct net_device *dev,
266 struct ieee80211_if_sta *ifsta,
267 u8 *wmm_param, size_t wmm_param_len)
269 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
270 struct ieee80211_tx_queue_params params;
275 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
281 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
283 count = wmm_param[6] & 0x0f;
284 if (count == ifsta->wmm_last_param_set)
286 ifsta->wmm_last_param_set = count;
289 left = wmm_param_len - 8;
291 memset(¶ms, 0, sizeof(params));
293 if (!local->ops->conf_tx)
297 for (; left >= 4; left -= 4, pos += 4) {
298 int aci = (pos[0] >> 5) & 0x03;
299 int acm = (pos[0] >> 4) & 0x01;
306 local->wmm_acm |= BIT(0) | BIT(3);
311 local->wmm_acm |= BIT(4) | BIT(5);
316 local->wmm_acm |= BIT(6) | BIT(7);
322 local->wmm_acm |= BIT(1) | BIT(2);
326 params.aifs = pos[0] & 0x0f;
327 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
328 params.cw_min = ecw2cw(pos[1] & 0x0f);
329 params.txop = pos[2] | (pos[3] << 8);
330 #ifdef CONFIG_MAC80211_DEBUG
331 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
332 "cWmin=%d cWmax=%d txop=%d\n",
333 dev->name, queue, aci, acm, params.aifs, params.cw_min,
334 params.cw_max, params.txop);
336 /* TODO: handle ACM (block TX, fallback to next lowest allowed
338 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
339 printk(KERN_DEBUG "%s: failed to set TX queue "
340 "parameters for queue %d\n", dev->name, queue);
345 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
347 bool use_short_preamble)
349 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
350 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
351 DECLARE_MAC_BUF(mac);
354 if (use_protection != bss_conf->use_cts_prot) {
355 if (net_ratelimit()) {
356 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
359 use_protection ? "enabled" : "disabled",
360 print_mac(mac, ifsta->bssid));
362 bss_conf->use_cts_prot = use_protection;
363 changed |= BSS_CHANGED_ERP_CTS_PROT;
366 if (use_short_preamble != bss_conf->use_short_preamble) {
367 if (net_ratelimit()) {
368 printk(KERN_DEBUG "%s: switched to %s barker preamble"
371 use_short_preamble ? "short" : "long",
372 print_mac(mac, ifsta->bssid));
374 bss_conf->use_short_preamble = use_short_preamble;
375 changed |= BSS_CHANGED_ERP_PREAMBLE;
381 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
384 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
385 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
387 return ieee80211_handle_protect_preamb(sdata,
388 use_protection, use_short_preamble);
391 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
392 struct ieee80211_sta_bss *bss)
396 if (bss->has_erp_value)
397 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
399 u16 capab = bss->capability;
400 changed |= ieee80211_handle_protect_preamb(sdata, false,
401 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
407 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
408 struct ieee80211_ht_info *ht_info)
414 memset(ht_info, 0, sizeof(*ht_info));
417 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
419 ht_info->ht_supported = 1;
420 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
421 ht_info->ampdu_factor =
422 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
423 ht_info->ampdu_density =
424 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
425 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
427 ht_info->ht_supported = 0;
432 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
433 struct ieee80211_ht_addt_info *ht_add_info_ie,
434 struct ieee80211_ht_bss_info *bss_info)
436 if (bss_info == NULL)
439 memset(bss_info, 0, sizeof(*bss_info));
441 if (ht_add_info_ie) {
443 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
445 bss_info->primary_channel = ht_add_info_ie->control_chan;
446 bss_info->bss_cap = ht_add_info_ie->ht_param;
447 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
453 static void ieee80211_sta_send_associnfo(struct net_device *dev,
454 struct ieee80211_if_sta *ifsta)
459 union iwreq_data wrqu;
461 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
464 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
465 ifsta->assocresp_ies_len), GFP_KERNEL);
469 len = sprintf(buf, "ASSOCINFO(");
470 if (ifsta->assocreq_ies) {
471 len += sprintf(buf + len, "ReqIEs=");
472 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
473 len += sprintf(buf + len, "%02x",
474 ifsta->assocreq_ies[i]);
477 if (ifsta->assocresp_ies) {
478 if (ifsta->assocreq_ies)
479 len += sprintf(buf + len, " ");
480 len += sprintf(buf + len, "RespIEs=");
481 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
482 len += sprintf(buf + len, "%02x",
483 ifsta->assocresp_ies[i]);
486 len += sprintf(buf + len, ")");
488 if (len > IW_CUSTOM_MAX) {
489 len = sprintf(buf, "ASSOCRESPIE=");
490 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
491 len += sprintf(buf + len, "%02x",
492 ifsta->assocresp_ies[i]);
496 memset(&wrqu, 0, sizeof(wrqu));
497 wrqu.data.length = len;
498 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
504 static void ieee80211_set_associated(struct net_device *dev,
505 struct ieee80211_if_sta *ifsta,
508 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
509 struct ieee80211_local *local = sdata->local;
510 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
511 union iwreq_data wrqu;
512 u32 changed = BSS_CHANGED_ASSOC;
515 struct ieee80211_sta_bss *bss;
517 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
519 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
522 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
523 conf->channel->center_freq,
524 ifsta->ssid, ifsta->ssid_len);
526 /* set timing information */
527 sdata->bss_conf.beacon_int = bss->beacon_int;
528 sdata->bss_conf.timestamp = bss->timestamp;
530 changed |= ieee80211_handle_bss_capability(sdata, bss);
532 ieee80211_rx_bss_put(dev, bss);
535 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
536 changed |= BSS_CHANGED_HT;
537 sdata->bss_conf.assoc_ht = 1;
538 sdata->bss_conf.ht_conf = &conf->ht_conf;
539 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
542 netif_carrier_on(dev);
543 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
544 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
545 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
546 ieee80211_sta_send_associnfo(dev, ifsta);
548 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
549 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
550 netif_carrier_off(dev);
551 ieee80211_reset_erp_info(dev);
553 sdata->bss_conf.assoc_ht = 0;
554 sdata->bss_conf.ht_conf = NULL;
555 sdata->bss_conf.ht_bss_conf = NULL;
557 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
559 ifsta->last_probe = jiffies;
560 ieee80211_led_assoc(local, assoc);
562 sdata->bss_conf.assoc = assoc;
563 ieee80211_bss_info_change_notify(sdata, changed);
564 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
565 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
568 static void ieee80211_set_disassoc(struct net_device *dev,
569 struct ieee80211_if_sta *ifsta, int deauth)
572 ifsta->auth_tries = 0;
573 ifsta->assoc_tries = 0;
574 ieee80211_set_associated(dev, ifsta, 0);
577 void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
580 struct ieee80211_sub_if_data *sdata;
581 struct ieee80211_tx_info *info;
583 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
584 skb->dev = sdata->local->mdev;
585 skb_set_mac_header(skb, 0);
586 skb_set_network_header(skb, 0);
587 skb_set_transport_header(skb, 0);
589 info = IEEE80211_SKB_CB(skb);
590 memset(info, 0, sizeof(struct ieee80211_tx_info));
591 info->control.ifindex = sdata->dev->ifindex;
593 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
599 static void ieee80211_send_auth(struct net_device *dev,
600 struct ieee80211_if_sta *ifsta,
601 int transaction, u8 *extra, size_t extra_len,
604 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
606 struct ieee80211_mgmt *mgmt;
608 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
609 sizeof(*mgmt) + 6 + extra_len);
611 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
612 "frame\n", dev->name);
615 skb_reserve(skb, local->hw.extra_tx_headroom);
617 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
618 memset(mgmt, 0, 24 + 6);
619 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
620 IEEE80211_STYPE_AUTH);
622 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
623 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
624 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
625 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
626 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
627 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
628 ifsta->auth_transaction = transaction + 1;
629 mgmt->u.auth.status_code = cpu_to_le16(0);
631 memcpy(skb_put(skb, extra_len), extra, extra_len);
633 ieee80211_sta_tx(dev, skb, encrypt);
637 static void ieee80211_authenticate(struct net_device *dev,
638 struct ieee80211_if_sta *ifsta)
640 DECLARE_MAC_BUF(mac);
643 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
644 printk(KERN_DEBUG "%s: authentication with AP %s"
646 dev->name, print_mac(mac, ifsta->bssid));
647 ifsta->state = IEEE80211_DISABLED;
651 ifsta->state = IEEE80211_AUTHENTICATE;
652 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
653 dev->name, print_mac(mac, ifsta->bssid));
655 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
657 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
660 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
661 struct ieee80211_supported_band *sband,
667 for (i = 0; i < bss->supp_rates_len; i++) {
668 int rate = (bss->supp_rates[i] & 0x7F) * 5;
670 for (j = 0; j < sband->n_bitrates; j++)
671 if (sband->bitrates[j].bitrate == rate) {
681 static void ieee80211_send_assoc(struct net_device *dev,
682 struct ieee80211_if_sta *ifsta)
684 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
686 struct ieee80211_mgmt *mgmt;
688 int i, len, count, rates_len, supp_rates_len;
690 struct ieee80211_sta_bss *bss;
692 struct ieee80211_supported_band *sband;
695 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
696 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
699 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
700 "frame\n", dev->name);
703 skb_reserve(skb, local->hw.extra_tx_headroom);
705 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
707 capab = ifsta->capab;
709 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
710 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
711 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
712 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
713 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
716 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
717 local->hw.conf.channel->center_freq,
718 ifsta->ssid, ifsta->ssid_len);
720 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
721 capab |= WLAN_CAPABILITY_PRIVACY;
725 /* get all rates supported by the device and the AP as
726 * some APs don't like getting a superset of their rates
727 * in the association request (e.g. D-Link DAP 1353 in
729 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
731 ieee80211_rx_bss_put(dev, bss);
734 rates_len = sband->n_bitrates;
737 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
739 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
740 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
741 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
743 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
745 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
746 IEEE80211_STYPE_REASSOC_REQ);
747 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
748 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
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.assoc_req.listen_interval = cpu_to_le16(1);
760 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
761 *pos++ = WLAN_EID_SSID;
762 *pos++ = ifsta->ssid_len;
763 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
765 /* add all rates which were marked to be used above */
766 supp_rates_len = rates_len;
767 if (supp_rates_len > 8)
770 len = sband->n_bitrates;
771 pos = skb_put(skb, supp_rates_len + 2);
772 *pos++ = WLAN_EID_SUPP_RATES;
773 *pos++ = supp_rates_len;
776 for (i = 0; i < sband->n_bitrates; i++) {
777 if (BIT(i) & rates) {
778 int rate = sband->bitrates[i].bitrate;
779 *pos++ = (u8) (rate / 5);
786 pos = skb_put(skb, rates_len - count + 2);
787 *pos++ = WLAN_EID_EXT_SUPP_RATES;
788 *pos++ = rates_len - count;
790 for (i++; i < sband->n_bitrates; i++) {
791 if (BIT(i) & rates) {
792 int rate = sband->bitrates[i].bitrate;
793 *pos++ = (u8) (rate / 5);
798 if (ifsta->extra_ie) {
799 pos = skb_put(skb, ifsta->extra_ie_len);
800 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
803 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
804 pos = skb_put(skb, 9);
805 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
806 *pos++ = 7; /* len */
807 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
810 *pos++ = 2; /* WME */
811 *pos++ = 0; /* WME info */
812 *pos++ = 1; /* WME ver */
816 /* wmm support is a must to HT */
817 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
818 sband->ht_info.ht_supported && bss->ht_add_ie) {
819 struct ieee80211_ht_addt_info *ht_add_info =
820 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
821 u16 cap = sband->ht_info.cap;
823 u32 flags = local->hw.conf.channel->flags;
825 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
826 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
827 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
828 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
829 cap &= ~IEEE80211_HT_CAP_SGI_40;
832 case IEEE80211_HT_IE_CHA_SEC_BELOW:
833 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
834 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
835 cap &= ~IEEE80211_HT_CAP_SGI_40;
840 tmp = cpu_to_le16(cap);
841 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
842 *pos++ = WLAN_EID_HT_CAPABILITY;
843 *pos++ = sizeof(struct ieee80211_ht_cap);
844 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
845 memcpy(pos, &tmp, sizeof(u16));
847 /* TODO: needs a define here for << 2 */
848 *pos++ = sband->ht_info.ampdu_factor |
849 (sband->ht_info.ampdu_density << 2);
850 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
853 kfree(ifsta->assocreq_ies);
854 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
855 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
856 if (ifsta->assocreq_ies)
857 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
859 ieee80211_sta_tx(dev, skb, 0);
863 static void ieee80211_send_deauth(struct net_device *dev,
864 struct ieee80211_if_sta *ifsta, u16 reason)
866 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
868 struct ieee80211_mgmt *mgmt;
870 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
872 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
873 "frame\n", dev->name);
876 skb_reserve(skb, local->hw.extra_tx_headroom);
878 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
880 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
881 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
882 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
883 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
884 IEEE80211_STYPE_DEAUTH);
886 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
888 ieee80211_sta_tx(dev, skb, 0);
892 static void ieee80211_send_disassoc(struct net_device *dev,
893 struct ieee80211_if_sta *ifsta, u16 reason)
895 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
897 struct ieee80211_mgmt *mgmt;
899 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
901 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
902 "frame\n", dev->name);
905 skb_reserve(skb, local->hw.extra_tx_headroom);
907 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
909 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
910 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
911 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
912 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
913 IEEE80211_STYPE_DISASSOC);
915 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
917 ieee80211_sta_tx(dev, skb, 0);
921 static int ieee80211_privacy_mismatch(struct net_device *dev,
922 struct ieee80211_if_sta *ifsta)
924 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
925 struct ieee80211_sta_bss *bss;
930 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
933 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
934 local->hw.conf.channel->center_freq,
935 ifsta->ssid, ifsta->ssid_len);
939 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
940 wep_privacy = !!ieee80211_sta_wep_configured(dev);
941 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
943 ieee80211_rx_bss_put(dev, bss);
945 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
952 static void ieee80211_associate(struct net_device *dev,
953 struct ieee80211_if_sta *ifsta)
955 DECLARE_MAC_BUF(mac);
957 ifsta->assoc_tries++;
958 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
959 printk(KERN_DEBUG "%s: association with AP %s"
961 dev->name, print_mac(mac, ifsta->bssid));
962 ifsta->state = IEEE80211_DISABLED;
966 ifsta->state = IEEE80211_ASSOCIATE;
967 printk(KERN_DEBUG "%s: associate with AP %s\n",
968 dev->name, print_mac(mac, ifsta->bssid));
969 if (ieee80211_privacy_mismatch(dev, ifsta)) {
970 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
971 "mixed-cell disabled - abort association\n", dev->name);
972 ifsta->state = IEEE80211_DISABLED;
976 ieee80211_send_assoc(dev, ifsta);
978 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
982 static void ieee80211_associated(struct net_device *dev,
983 struct ieee80211_if_sta *ifsta)
985 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
986 struct sta_info *sta;
988 DECLARE_MAC_BUF(mac);
990 /* TODO: start monitoring current AP signal quality and number of
991 * missed beacons. Scan other channels every now and then and search
993 /* TODO: remove expired BSSes */
995 ifsta->state = IEEE80211_ASSOCIATED;
999 sta = sta_info_get(local, ifsta->bssid);
1001 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1002 dev->name, print_mac(mac, ifsta->bssid));
1006 if (time_after(jiffies,
1007 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1008 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
1009 printk(KERN_DEBUG "%s: No ProbeResp from "
1010 "current AP %s - assume out of "
1012 dev->name, print_mac(mac, ifsta->bssid));
1014 sta_info_unlink(&sta);
1016 ieee80211_send_probe_req(dev, ifsta->bssid,
1018 local->scan_ssid_len);
1019 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1021 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1022 if (time_after(jiffies, ifsta->last_probe +
1023 IEEE80211_PROBE_INTERVAL)) {
1024 ifsta->last_probe = jiffies;
1025 ieee80211_send_probe_req(dev, ifsta->bssid,
1034 if (disassoc && sta)
1035 sta_info_destroy(sta);
1038 ifsta->state = IEEE80211_DISABLED;
1039 ieee80211_set_associated(dev, ifsta, 0);
1041 mod_timer(&ifsta->timer, jiffies +
1042 IEEE80211_MONITORING_INTERVAL);
1047 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1048 u8 *ssid, size_t ssid_len)
1050 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1051 struct ieee80211_supported_band *sband;
1052 struct sk_buff *skb;
1053 struct ieee80211_mgmt *mgmt;
1054 u8 *pos, *supp_rates, *esupp_rates = NULL;
1057 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1059 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1060 "request\n", dev->name);
1063 skb_reserve(skb, local->hw.extra_tx_headroom);
1065 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1066 memset(mgmt, 0, 24);
1067 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1068 IEEE80211_STYPE_PROBE_REQ);
1069 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1071 memcpy(mgmt->da, dst, ETH_ALEN);
1072 memcpy(mgmt->bssid, dst, ETH_ALEN);
1074 memset(mgmt->da, 0xff, ETH_ALEN);
1075 memset(mgmt->bssid, 0xff, ETH_ALEN);
1077 pos = skb_put(skb, 2 + ssid_len);
1078 *pos++ = WLAN_EID_SSID;
1080 memcpy(pos, ssid, ssid_len);
1082 supp_rates = skb_put(skb, 2);
1083 supp_rates[0] = WLAN_EID_SUPP_RATES;
1085 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1087 for (i = 0; i < sband->n_bitrates; i++) {
1088 struct ieee80211_rate *rate = &sband->bitrates[i];
1090 pos = skb_put(skb, 1);
1092 } else if (supp_rates[1] == 8) {
1093 esupp_rates = skb_put(skb, 3);
1094 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1096 pos = &esupp_rates[2];
1098 pos = skb_put(skb, 1);
1101 *pos = rate->bitrate / 5;
1104 ieee80211_sta_tx(dev, skb, 0);
1108 static int ieee80211_sta_wep_configured(struct net_device *dev)
1110 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1111 if (!sdata || !sdata->default_key ||
1112 sdata->default_key->conf.alg != ALG_WEP)
1118 static void ieee80211_auth_completed(struct net_device *dev,
1119 struct ieee80211_if_sta *ifsta)
1121 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
1122 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1123 ieee80211_associate(dev, ifsta);
1127 static void ieee80211_auth_challenge(struct net_device *dev,
1128 struct ieee80211_if_sta *ifsta,
1129 struct ieee80211_mgmt *mgmt,
1133 struct ieee802_11_elems elems;
1135 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1136 pos = mgmt->u.auth.variable;
1137 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1138 if (!elems.challenge) {
1139 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1140 "frame\n", dev->name);
1143 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1144 elems.challenge_len + 2, 1);
1147 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1148 u8 dialog_token, u16 status, u16 policy,
1149 u16 buf_size, u16 timeout)
1151 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1152 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1153 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1154 struct sk_buff *skb;
1155 struct ieee80211_mgmt *mgmt;
1158 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1161 printk(KERN_DEBUG "%s: failed to allocate buffer "
1162 "for addba resp frame\n", dev->name);
1166 skb_reserve(skb, local->hw.extra_tx_headroom);
1167 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1168 memset(mgmt, 0, 24);
1169 memcpy(mgmt->da, da, ETH_ALEN);
1170 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1171 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1172 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1174 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1175 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1176 IEEE80211_STYPE_ACTION);
1178 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1179 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1180 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1181 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1183 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1184 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1185 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1187 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1188 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1189 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1191 ieee80211_sta_tx(dev, skb, 0);
1196 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1197 u16 tid, u8 dialog_token, u16 start_seq_num,
1198 u16 agg_size, u16 timeout)
1200 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1201 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1202 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1203 struct sk_buff *skb;
1204 struct ieee80211_mgmt *mgmt;
1207 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1210 printk(KERN_ERR "%s: failed to allocate buffer "
1211 "for addba request frame\n", dev->name);
1214 skb_reserve(skb, local->hw.extra_tx_headroom);
1215 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1216 memset(mgmt, 0, 24);
1217 memcpy(mgmt->da, da, ETH_ALEN);
1218 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1219 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1220 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1222 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1224 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1225 IEEE80211_STYPE_ACTION);
1227 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1229 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1230 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1232 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1233 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1234 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1235 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1237 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1239 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1240 mgmt->u.action.u.addba_req.start_seq_num =
1241 cpu_to_le16(start_seq_num << 4);
1243 ieee80211_sta_tx(dev, skb, 0);
1246 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1247 struct ieee80211_mgmt *mgmt,
1250 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1251 struct ieee80211_hw *hw = &local->hw;
1252 struct ieee80211_conf *conf = &hw->conf;
1253 struct sta_info *sta;
1254 struct tid_ampdu_rx *tid_agg_rx;
1255 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1257 int ret = -EOPNOTSUPP;
1258 DECLARE_MAC_BUF(mac);
1262 sta = sta_info_get(local, mgmt->sa);
1268 /* extract session parameters from addba request frame */
1269 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1270 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1272 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1274 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1275 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1276 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1277 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1279 status = WLAN_STATUS_REQUEST_DECLINED;
1281 /* sanity check for incoming parameters:
1282 * check if configuration can support the BA policy
1283 * and if buffer size does not exceeds max value */
1284 if (((ba_policy != 1)
1285 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1286 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1287 status = WLAN_STATUS_INVALID_QOS_PARAM;
1288 #ifdef CONFIG_MAC80211_HT_DEBUG
1289 if (net_ratelimit())
1290 printk(KERN_DEBUG "AddBA Req with bad params from "
1291 "%s on tid %u. policy %d, buffer size %d\n",
1292 print_mac(mac, mgmt->sa), tid, ba_policy,
1294 #endif /* CONFIG_MAC80211_HT_DEBUG */
1297 /* determine default buffer size */
1298 if (buf_size == 0) {
1299 struct ieee80211_supported_band *sband;
1301 sband = local->hw.wiphy->bands[conf->channel->band];
1302 buf_size = IEEE80211_MIN_AMPDU_BUF;
1303 buf_size = buf_size << sband->ht_info.ampdu_factor;
1307 /* examine state machine */
1308 spin_lock_bh(&sta->lock);
1310 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1311 #ifdef CONFIG_MAC80211_HT_DEBUG
1312 if (net_ratelimit())
1313 printk(KERN_DEBUG "unexpected AddBA Req from "
1315 print_mac(mac, mgmt->sa), tid);
1316 #endif /* CONFIG_MAC80211_HT_DEBUG */
1320 /* prepare A-MPDU MLME for Rx aggregation */
1321 sta->ampdu_mlme.tid_rx[tid] =
1322 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1323 if (!sta->ampdu_mlme.tid_rx[tid]) {
1324 if (net_ratelimit())
1325 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1330 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1331 sta_rx_agg_session_timer_expired;
1332 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1333 (unsigned long)&sta->timer_to_tid[tid];
1334 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1336 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1338 /* prepare reordering buffer */
1339 tid_agg_rx->reorder_buf =
1340 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1341 if (!tid_agg_rx->reorder_buf) {
1342 if (net_ratelimit())
1343 printk(KERN_ERR "can not allocate reordering buffer "
1344 "to tid %d\n", tid);
1345 kfree(sta->ampdu_mlme.tid_rx[tid]);
1348 memset(tid_agg_rx->reorder_buf, 0,
1349 buf_size * sizeof(struct sk_buff *));
1351 if (local->ops->ampdu_action)
1352 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1353 sta->addr, tid, &start_seq_num);
1354 #ifdef CONFIG_MAC80211_HT_DEBUG
1355 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1356 #endif /* CONFIG_MAC80211_HT_DEBUG */
1359 kfree(tid_agg_rx->reorder_buf);
1361 sta->ampdu_mlme.tid_rx[tid] = NULL;
1365 /* change state and send addba resp */
1366 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1367 tid_agg_rx->dialog_token = dialog_token;
1368 tid_agg_rx->ssn = start_seq_num;
1369 tid_agg_rx->head_seq_num = start_seq_num;
1370 tid_agg_rx->buf_size = buf_size;
1371 tid_agg_rx->timeout = timeout;
1372 tid_agg_rx->stored_mpdu_num = 0;
1373 status = WLAN_STATUS_SUCCESS;
1375 spin_unlock_bh(&sta->lock);
1378 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1379 dialog_token, status, 1, buf_size, timeout);
1383 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1384 struct ieee80211_mgmt *mgmt,
1387 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1388 struct ieee80211_hw *hw = &local->hw;
1389 struct sta_info *sta;
1396 sta = sta_info_get(local, mgmt->sa);
1402 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1403 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1405 state = &sta->ampdu_mlme.tid_state_tx[tid];
1407 spin_lock_bh(&sta->lock);
1409 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1410 spin_unlock_bh(&sta->lock);
1411 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1413 goto addba_resp_exit;
1416 if (mgmt->u.action.u.addba_resp.dialog_token !=
1417 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1418 spin_unlock_bh(&sta->lock);
1419 #ifdef CONFIG_MAC80211_HT_DEBUG
1420 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1421 #endif /* CONFIG_MAC80211_HT_DEBUG */
1422 goto addba_resp_exit;
1425 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1426 #ifdef CONFIG_MAC80211_HT_DEBUG
1427 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1428 #endif /* CONFIG_MAC80211_HT_DEBUG */
1429 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1430 == WLAN_STATUS_SUCCESS) {
1431 if (*state & HT_ADDBA_RECEIVED_MSK)
1432 printk(KERN_DEBUG "double addBA response\n");
1434 *state |= HT_ADDBA_RECEIVED_MSK;
1435 sta->ampdu_mlme.addba_req_num[tid] = 0;
1437 if (*state == HT_AGG_STATE_OPERATIONAL) {
1438 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1439 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1442 spin_unlock_bh(&sta->lock);
1443 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1445 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1447 sta->ampdu_mlme.addba_req_num[tid]++;
1448 /* this will allow the state check in stop_BA_session */
1449 *state = HT_AGG_STATE_OPERATIONAL;
1450 spin_unlock_bh(&sta->lock);
1451 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1452 WLAN_BACK_INITIATOR);
1459 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1460 u16 initiator, u16 reason_code)
1462 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1463 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1464 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1465 struct sk_buff *skb;
1466 struct ieee80211_mgmt *mgmt;
1469 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1472 printk(KERN_ERR "%s: failed to allocate buffer "
1473 "for delba frame\n", dev->name);
1477 skb_reserve(skb, local->hw.extra_tx_headroom);
1478 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1479 memset(mgmt, 0, 24);
1480 memcpy(mgmt->da, da, ETH_ALEN);
1481 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1482 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1483 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1485 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1486 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1487 IEEE80211_STYPE_ACTION);
1489 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1491 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1492 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1493 params = (u16)(initiator << 11); /* bit 11 initiator */
1494 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1496 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1497 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1499 ieee80211_sta_tx(dev, skb, 0);
1502 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1503 u16 initiator, u16 reason)
1505 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1506 struct ieee80211_hw *hw = &local->hw;
1507 struct sta_info *sta;
1509 DECLARE_MAC_BUF(mac);
1513 sta = sta_info_get(local, ra);
1519 /* check if TID is in operational state */
1520 spin_lock_bh(&sta->lock);
1521 if (sta->ampdu_mlme.tid_state_rx[tid]
1522 != HT_AGG_STATE_OPERATIONAL) {
1523 spin_unlock_bh(&sta->lock);
1527 sta->ampdu_mlme.tid_state_rx[tid] =
1528 HT_AGG_STATE_REQ_STOP_BA_MSK |
1529 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1530 spin_unlock_bh(&sta->lock);
1532 /* stop HW Rx aggregation. ampdu_action existence
1533 * already verified in session init so we add the BUG_ON */
1534 BUG_ON(!local->ops->ampdu_action);
1536 #ifdef CONFIG_MAC80211_HT_DEBUG
1537 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1538 print_mac(mac, ra), tid);
1539 #endif /* CONFIG_MAC80211_HT_DEBUG */
1541 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1544 printk(KERN_DEBUG "HW problem - can not stop rx "
1545 "aggergation for tid %d\n", tid);
1547 /* shutdown timer has not expired */
1548 if (initiator != WLAN_BACK_TIMER)
1549 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1551 /* check if this is a self generated aggregation halt */
1552 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1553 ieee80211_send_delba(dev, ra, tid, 0, reason);
1555 /* free the reordering buffer */
1556 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1557 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1558 /* release the reordered frames */
1559 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1560 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1561 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1564 /* free resources */
1565 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1566 kfree(sta->ampdu_mlme.tid_rx[tid]);
1567 sta->ampdu_mlme.tid_rx[tid] = NULL;
1568 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1574 static void ieee80211_sta_process_delba(struct net_device *dev,
1575 struct ieee80211_mgmt *mgmt, size_t len)
1577 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1578 struct sta_info *sta;
1581 DECLARE_MAC_BUF(mac);
1585 sta = sta_info_get(local, mgmt->sa);
1591 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1592 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1593 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1595 #ifdef CONFIG_MAC80211_HT_DEBUG
1596 if (net_ratelimit())
1597 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1598 print_mac(mac, mgmt->sa),
1599 initiator ? "initiator" : "recipient", tid,
1600 mgmt->u.action.u.delba.reason_code);
1601 #endif /* CONFIG_MAC80211_HT_DEBUG */
1603 if (initiator == WLAN_BACK_INITIATOR)
1604 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1605 WLAN_BACK_INITIATOR, 0);
1606 else { /* WLAN_BACK_RECIPIENT */
1607 spin_lock_bh(&sta->lock);
1608 sta->ampdu_mlme.tid_state_tx[tid] =
1609 HT_AGG_STATE_OPERATIONAL;
1610 spin_unlock_bh(&sta->lock);
1611 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1612 WLAN_BACK_RECIPIENT);
1618 * After sending add Block Ack request we activated a timer until
1619 * add Block Ack response will arrive from the recipient.
1620 * If this timer expires sta_addba_resp_timer_expired will be executed.
1622 void sta_addba_resp_timer_expired(unsigned long data)
1624 /* not an elegant detour, but there is no choice as the timer passes
1625 * only one argument, and both sta_info and TID are needed, so init
1626 * flow in sta_info_create gives the TID as data, while the timer_to_id
1627 * array gives the sta through container_of */
1628 u16 tid = *(u8 *)data;
1629 struct sta_info *temp_sta = container_of((void *)data,
1630 struct sta_info, timer_to_tid[tid]);
1632 struct ieee80211_local *local = temp_sta->local;
1633 struct ieee80211_hw *hw = &local->hw;
1634 struct sta_info *sta;
1639 sta = sta_info_get(local, temp_sta->addr);
1645 state = &sta->ampdu_mlme.tid_state_tx[tid];
1646 /* check if the TID waits for addBA response */
1647 spin_lock_bh(&sta->lock);
1648 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1649 spin_unlock_bh(&sta->lock);
1650 *state = HT_AGG_STATE_IDLE;
1651 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1652 "expecting addBA response there", tid);
1653 goto timer_expired_exit;
1656 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1658 /* go through the state check in stop_BA_session */
1659 *state = HT_AGG_STATE_OPERATIONAL;
1660 spin_unlock_bh(&sta->lock);
1661 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1662 WLAN_BACK_INITIATOR);
1669 * After accepting the AddBA Request we activated a timer,
1670 * resetting it after each frame that arrives from the originator.
1671 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1673 static void sta_rx_agg_session_timer_expired(unsigned long data)
1675 /* not an elegant detour, but there is no choice as the timer passes
1676 * only one argument, and various sta_info are needed here, so init
1677 * flow in sta_info_create gives the TID as data, while the timer_to_id
1678 * array gives the sta through container_of */
1679 u8 *ptid = (u8 *)data;
1680 u8 *timer_to_id = ptid - *ptid;
1681 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1684 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1685 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1686 (u16)*ptid, WLAN_BACK_TIMER,
1687 WLAN_REASON_QSTA_TIMEOUT);
1690 void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1692 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1695 for (i = 0; i < STA_TID_NUM; i++) {
1696 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1697 WLAN_BACK_INITIATOR);
1698 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1699 WLAN_BACK_RECIPIENT,
1700 WLAN_REASON_QSTA_LEAVE_QBSS);
1704 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1705 struct ieee80211_if_sta *ifsta,
1706 struct ieee80211_mgmt *mgmt,
1709 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1710 u16 auth_alg, auth_transaction, status_code;
1711 DECLARE_MAC_BUF(mac);
1713 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1714 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
1715 printk(KERN_DEBUG "%s: authentication frame received from "
1716 "%s, but not in authenticate state - ignored\n",
1717 dev->name, print_mac(mac, mgmt->sa));
1722 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
1723 "received from %s - ignored\n",
1724 dev->name, len, print_mac(mac, mgmt->sa));
1728 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1729 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1730 printk(KERN_DEBUG "%s: authentication frame received from "
1731 "unknown AP (SA=%s BSSID=%s) - "
1732 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1733 print_mac(mac, mgmt->bssid));
1737 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1738 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1739 printk(KERN_DEBUG "%s: authentication frame received from "
1740 "unknown BSSID (SA=%s BSSID=%s) - "
1741 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1742 print_mac(mac, mgmt->bssid));
1746 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1747 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1748 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1750 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
1751 "transaction=%d status=%d)\n",
1752 dev->name, print_mac(mac, mgmt->sa), auth_alg,
1753 auth_transaction, status_code);
1755 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1756 /* IEEE 802.11 standard does not require authentication in IBSS
1757 * networks and most implementations do not seem to use it.
1758 * However, try to reply to authentication attempts if someone
1759 * has actually implemented this.
1760 * TODO: Could implement shared key authentication. */
1761 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1762 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1763 "frame (alg=%d transaction=%d)\n",
1764 dev->name, auth_alg, auth_transaction);
1767 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1770 if (auth_alg != ifsta->auth_alg ||
1771 auth_transaction != ifsta->auth_transaction) {
1772 printk(KERN_DEBUG "%s: unexpected authentication frame "
1773 "(alg=%d transaction=%d)\n",
1774 dev->name, auth_alg, auth_transaction);
1778 if (status_code != WLAN_STATUS_SUCCESS) {
1779 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1780 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1781 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1783 const int num_algs = ARRAY_SIZE(algs);
1785 algs[0] = algs[1] = algs[2] = 0xff;
1786 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1787 algs[0] = WLAN_AUTH_OPEN;
1788 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1789 algs[1] = WLAN_AUTH_SHARED_KEY;
1790 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1791 algs[2] = WLAN_AUTH_LEAP;
1792 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1794 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1798 for (i = 0; i < num_algs; i++) {
1800 if (pos >= num_algs)
1802 if (algs[pos] == ifsta->auth_alg ||
1805 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1806 !ieee80211_sta_wep_configured(dev))
1808 ifsta->auth_alg = algs[pos];
1809 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1811 dev->name, ifsta->auth_alg);
1818 switch (ifsta->auth_alg) {
1819 case WLAN_AUTH_OPEN:
1820 case WLAN_AUTH_LEAP:
1821 ieee80211_auth_completed(dev, ifsta);
1823 case WLAN_AUTH_SHARED_KEY:
1824 if (ifsta->auth_transaction == 4)
1825 ieee80211_auth_completed(dev, ifsta);
1827 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1833 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1834 struct ieee80211_if_sta *ifsta,
1835 struct ieee80211_mgmt *mgmt,
1839 DECLARE_MAC_BUF(mac);
1842 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1843 "received from %s - ignored\n",
1844 dev->name, len, print_mac(mac, mgmt->sa));
1848 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1849 printk(KERN_DEBUG "%s: deauthentication frame received from "
1850 "unknown AP (SA=%s BSSID=%s) - "
1851 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1852 print_mac(mac, mgmt->bssid));
1856 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1858 printk(KERN_DEBUG "%s: RX deauthentication from %s"
1860 dev->name, print_mac(mac, mgmt->sa), reason_code);
1862 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1863 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1865 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1866 ifsta->state == IEEE80211_ASSOCIATE ||
1867 ifsta->state == IEEE80211_ASSOCIATED) {
1868 ifsta->state = IEEE80211_AUTHENTICATE;
1869 mod_timer(&ifsta->timer, jiffies +
1870 IEEE80211_RETRY_AUTH_INTERVAL);
1873 ieee80211_set_disassoc(dev, ifsta, 1);
1874 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1878 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1879 struct ieee80211_if_sta *ifsta,
1880 struct ieee80211_mgmt *mgmt,
1884 DECLARE_MAC_BUF(mac);
1887 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1888 "received from %s - ignored\n",
1889 dev->name, len, print_mac(mac, mgmt->sa));
1893 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1894 printk(KERN_DEBUG "%s: disassociation frame received from "
1895 "unknown AP (SA=%s BSSID=%s) - "
1896 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1897 print_mac(mac, mgmt->bssid));
1901 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1903 printk(KERN_DEBUG "%s: RX disassociation from %s"
1905 dev->name, print_mac(mac, mgmt->sa), reason_code);
1907 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1908 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1910 if (ifsta->state == IEEE80211_ASSOCIATED) {
1911 ifsta->state = IEEE80211_ASSOCIATE;
1912 mod_timer(&ifsta->timer, jiffies +
1913 IEEE80211_RETRY_AUTH_INTERVAL);
1916 ieee80211_set_disassoc(dev, ifsta, 0);
1920 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1921 struct ieee80211_if_sta *ifsta,
1922 struct ieee80211_mgmt *mgmt,
1926 struct ieee80211_local *local = sdata->local;
1927 struct net_device *dev = sdata->dev;
1928 struct ieee80211_supported_band *sband;
1929 struct sta_info *sta;
1930 u64 rates, basic_rates;
1931 u16 capab_info, status_code, aid;
1932 struct ieee802_11_elems elems;
1933 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1936 DECLARE_MAC_BUF(mac);
1937 bool have_higher_than_11mbit = false;
1939 /* AssocResp and ReassocResp have identical structure, so process both
1940 * of them in this function. */
1942 if (ifsta->state != IEEE80211_ASSOCIATE) {
1943 printk(KERN_DEBUG "%s: association frame received from "
1944 "%s, but not in associate state - ignored\n",
1945 dev->name, print_mac(mac, mgmt->sa));
1950 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1951 "received from %s - ignored\n",
1952 dev->name, len, print_mac(mac, mgmt->sa));
1956 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1957 printk(KERN_DEBUG "%s: association frame received from "
1958 "unknown AP (SA=%s BSSID=%s) - "
1959 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1960 print_mac(mac, mgmt->bssid));
1964 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1965 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1966 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1968 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1969 "status=%d aid=%d)\n",
1970 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1971 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1973 if (status_code != WLAN_STATUS_SUCCESS) {
1974 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1975 dev->name, status_code);
1976 /* if this was a reassociation, ensure we try a "full"
1977 * association next time. This works around some broken APs
1978 * which do not correctly reject reassociation requests. */
1979 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1983 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1984 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1985 "set\n", dev->name, aid);
1986 aid &= ~(BIT(15) | BIT(14));
1988 pos = mgmt->u.assoc_resp.variable;
1989 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1991 if (!elems.supp_rates) {
1992 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1997 printk(KERN_DEBUG "%s: associated\n", dev->name);
1999 ifsta->ap_capab = capab_info;
2001 kfree(ifsta->assocresp_ies);
2002 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
2003 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
2004 if (ifsta->assocresp_ies)
2005 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2009 /* Add STA entry for the AP */
2010 sta = sta_info_get(local, ifsta->bssid);
2012 struct ieee80211_sta_bss *bss;
2015 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2017 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2018 " the AP\n", dev->name);
2022 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
2023 local->hw.conf.channel->center_freq,
2024 ifsta->ssid, ifsta->ssid_len);
2026 sta->last_signal = bss->signal;
2027 sta->last_qual = bss->qual;
2028 sta->last_noise = bss->noise;
2029 ieee80211_rx_bss_put(dev, bss);
2032 err = sta_info_insert(sta);
2034 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2035 " the AP (error %d)\n", dev->name, err);
2042 * FIXME: Do we really need to update the sta_info's information here?
2043 * We already know about the AP (we found it in our list) so it
2044 * should already be filled with the right info, no?
2045 * As is stands, all this is racy because typically we assume
2046 * the information that is filled in here (except flags) doesn't
2047 * change while a STA structure is alive. As such, it should move
2048 * to between the sta_info_alloc() and sta_info_insert() above.
2051 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2052 WLAN_STA_AUTHORIZED);
2056 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2058 for (i = 0; i < elems.supp_rates_len; i++) {
2059 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2062 have_higher_than_11mbit = true;
2064 for (j = 0; j < sband->n_bitrates; j++) {
2065 if (sband->bitrates[j].bitrate == rate)
2067 if (elems.supp_rates[i] & 0x80)
2068 basic_rates |= BIT(j);
2072 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2073 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2076 have_higher_than_11mbit = true;
2078 for (j = 0; j < sband->n_bitrates; j++) {
2079 if (sband->bitrates[j].bitrate == rate)
2081 if (elems.ext_supp_rates[i] & 0x80)
2082 basic_rates |= BIT(j);
2086 sta->supp_rates[local->hw.conf.channel->band] = rates;
2087 sdata->basic_rates = basic_rates;
2089 /* cf. IEEE 802.11 9.2.12 */
2090 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2091 have_higher_than_11mbit)
2092 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2094 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2096 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2097 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2098 struct ieee80211_ht_bss_info bss_info;
2099 ieee80211_ht_cap_ie_to_ht_info(
2100 (struct ieee80211_ht_cap *)
2101 elems.ht_cap_elem, &sta->ht_info);
2102 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2103 (struct ieee80211_ht_addt_info *)
2104 elems.ht_info_elem, &bss_info);
2105 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2108 rate_control_rate_init(sta, local);
2110 if (elems.wmm_param) {
2111 set_sta_flags(sta, WLAN_STA_WME);
2113 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2114 elems.wmm_param_len);
2118 /* set AID and assoc capability,
2119 * ieee80211_set_associated() will tell the driver */
2120 bss_conf->aid = aid;
2121 bss_conf->assoc_capability = capab_info;
2122 ieee80211_set_associated(dev, ifsta, 1);
2124 ieee80211_associated(dev, ifsta);
2128 /* Caller must hold local->sta_bss_lock */
2129 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2130 struct ieee80211_sta_bss *bss)
2132 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2135 if (bss_mesh_cfg(bss))
2136 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2137 bss_mesh_id_len(bss));
2139 hash_idx = STA_HASH(bss->bssid);
2141 bss->hnext = local->sta_bss_hash[hash_idx];
2142 local->sta_bss_hash[hash_idx] = bss;
2146 /* Caller must hold local->sta_bss_lock */
2147 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2148 struct ieee80211_sta_bss *bss)
2150 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2151 struct ieee80211_sta_bss *b, *prev = NULL;
2152 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2156 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2159 prev->hnext = bss->hnext;
2168 static struct ieee80211_sta_bss *
2169 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
2170 u8 *ssid, u8 ssid_len)
2172 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2173 struct ieee80211_sta_bss *bss;
2175 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2178 atomic_inc(&bss->users);
2179 atomic_inc(&bss->users);
2180 memcpy(bss->bssid, bssid, ETH_ALEN);
2182 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2183 memcpy(bss->ssid, ssid, ssid_len);
2184 bss->ssid_len = ssid_len;
2187 spin_lock_bh(&local->sta_bss_lock);
2188 /* TODO: order by RSSI? */
2189 list_add_tail(&bss->list, &local->sta_bss_list);
2190 __ieee80211_rx_bss_hash_add(dev, bss);
2191 spin_unlock_bh(&local->sta_bss_lock);
2195 static struct ieee80211_sta_bss *
2196 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
2197 u8 *ssid, u8 ssid_len)
2199 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2200 struct ieee80211_sta_bss *bss;
2202 spin_lock_bh(&local->sta_bss_lock);
2203 bss = local->sta_bss_hash[STA_HASH(bssid)];
2205 if (!bss_mesh_cfg(bss) &&
2206 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2207 bss->freq == freq &&
2208 bss->ssid_len == ssid_len &&
2209 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2210 atomic_inc(&bss->users);
2215 spin_unlock_bh(&local->sta_bss_lock);
2219 #ifdef CONFIG_MAC80211_MESH
2220 static struct ieee80211_sta_bss *
2221 ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2222 u8 *mesh_cfg, int freq)
2224 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2225 struct ieee80211_sta_bss *bss;
2227 spin_lock_bh(&local->sta_bss_lock);
2228 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2230 if (bss_mesh_cfg(bss) &&
2231 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2232 bss->freq == freq &&
2233 mesh_id_len == bss->mesh_id_len &&
2234 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2236 atomic_inc(&bss->users);
2241 spin_unlock_bh(&local->sta_bss_lock);
2245 static struct ieee80211_sta_bss *
2246 ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2247 u8 *mesh_cfg, int mesh_config_len, int freq)
2249 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2250 struct ieee80211_sta_bss *bss;
2252 if (mesh_config_len != MESH_CFG_LEN)
2255 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2259 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2260 if (!bss->mesh_cfg) {
2265 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2266 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2267 if (!bss->mesh_id) {
2268 kfree(bss->mesh_cfg);
2272 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2275 atomic_inc(&bss->users);
2276 atomic_inc(&bss->users);
2277 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2278 bss->mesh_id_len = mesh_id_len;
2280 spin_lock_bh(&local->sta_bss_lock);
2281 /* TODO: order by RSSI? */
2282 list_add_tail(&bss->list, &local->sta_bss_list);
2283 __ieee80211_rx_bss_hash_add(dev, bss);
2284 spin_unlock_bh(&local->sta_bss_lock);
2289 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2295 kfree(bss->ht_add_ie);
2296 kfree(bss_mesh_id(bss));
2297 kfree(bss_mesh_cfg(bss));
2302 static void ieee80211_rx_bss_put(struct net_device *dev,
2303 struct ieee80211_sta_bss *bss)
2305 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2308 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2313 __ieee80211_rx_bss_hash_del(dev, bss);
2314 list_del(&bss->list);
2315 spin_unlock_bh(&local->sta_bss_lock);
2316 ieee80211_rx_bss_free(bss);
2320 void ieee80211_rx_bss_list_init(struct net_device *dev)
2322 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2323 spin_lock_init(&local->sta_bss_lock);
2324 INIT_LIST_HEAD(&local->sta_bss_list);
2328 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2330 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2331 struct ieee80211_sta_bss *bss, *tmp;
2333 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2334 ieee80211_rx_bss_put(dev, bss);
2338 static int ieee80211_sta_join_ibss(struct net_device *dev,
2339 struct ieee80211_if_sta *ifsta,
2340 struct ieee80211_sta_bss *bss)
2342 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2343 int res, rates, i, j;
2344 struct sk_buff *skb;
2345 struct ieee80211_mgmt *mgmt;
2346 struct ieee80211_tx_info *control;
2347 struct rate_selection ratesel;
2349 struct ieee80211_sub_if_data *sdata;
2350 struct ieee80211_supported_band *sband;
2351 union iwreq_data wrqu;
2353 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2355 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2357 /* Remove possible STA entries from other IBSS networks. */
2358 sta_info_flush_delayed(sdata);
2360 if (local->ops->reset_tsf) {
2361 /* Reset own TSF to allow time synchronization work. */
2362 local->ops->reset_tsf(local_to_hw(local));
2364 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2365 res = ieee80211_if_config(dev);
2369 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2371 sdata->drop_unencrypted = bss->capability &
2372 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2374 res = ieee80211_set_freq(dev, bss->freq);
2379 /* Set beacon template */
2380 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2385 skb_reserve(skb, local->hw.extra_tx_headroom);
2387 mgmt = (struct ieee80211_mgmt *)
2388 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2389 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2390 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2391 IEEE80211_STYPE_BEACON);
2392 memset(mgmt->da, 0xff, ETH_ALEN);
2393 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2394 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2395 mgmt->u.beacon.beacon_int =
2396 cpu_to_le16(local->hw.conf.beacon_int);
2397 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2399 pos = skb_put(skb, 2 + ifsta->ssid_len);
2400 *pos++ = WLAN_EID_SSID;
2401 *pos++ = ifsta->ssid_len;
2402 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2404 rates = bss->supp_rates_len;
2407 pos = skb_put(skb, 2 + rates);
2408 *pos++ = WLAN_EID_SUPP_RATES;
2410 memcpy(pos, bss->supp_rates, rates);
2412 if (bss->band == IEEE80211_BAND_2GHZ) {
2413 pos = skb_put(skb, 2 + 1);
2414 *pos++ = WLAN_EID_DS_PARAMS;
2416 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2419 pos = skb_put(skb, 2 + 2);
2420 *pos++ = WLAN_EID_IBSS_PARAMS;
2422 /* FIX: set ATIM window based on scan results */
2426 if (bss->supp_rates_len > 8) {
2427 rates = bss->supp_rates_len - 8;
2428 pos = skb_put(skb, 2 + rates);
2429 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2431 memcpy(pos, &bss->supp_rates[8], rates);
2434 control = IEEE80211_SKB_CB(skb);
2436 rate_control_get_rate(dev, sband, skb, &ratesel);
2437 if (ratesel.rate_idx < 0) {
2438 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2439 "for IBSS beacon\n", dev->name);
2442 control->control.vif = &sdata->vif;
2443 control->tx_rate_idx = ratesel.rate_idx;
2444 if (sdata->bss_conf.use_short_preamble &&
2445 sband->bitrates[ratesel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
2446 control->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
2447 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2448 control->flags |= IEEE80211_TX_CTL_NO_ACK;
2449 control->control.retry_limit = 1;
2451 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2452 if (ifsta->probe_resp) {
2453 mgmt = (struct ieee80211_mgmt *)
2454 ifsta->probe_resp->data;
2455 mgmt->frame_control =
2456 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2457 IEEE80211_STYPE_PROBE_RESP);
2459 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2460 "template for IBSS\n", dev->name);
2463 if (local->ops->beacon_update &&
2464 local->ops->beacon_update(local_to_hw(local), skb) == 0) {
2465 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2466 "template\n", dev->name);
2471 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2472 for (i = 0; i < bss->supp_rates_len; i++) {
2473 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2474 for (j = 0; j < sband->n_bitrates; j++)
2475 if (sband->bitrates[j].bitrate == bitrate)
2478 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2480 ieee80211_sta_def_wmm_params(dev, bss, 1);
2484 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2485 "template\n", dev->name);
2489 ifsta->state = IEEE80211_IBSS_JOINED;
2490 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2492 memset(&wrqu, 0, sizeof(wrqu));
2493 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2494 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
2499 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2500 struct ieee802_11_elems *elems,
2501 enum ieee80211_band band)
2503 struct ieee80211_supported_band *sband;
2504 struct ieee80211_rate *bitrates;
2508 sband = local->hw.wiphy->bands[band];
2512 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2515 bitrates = sband->bitrates;
2516 num_rates = sband->n_bitrates;
2518 for (i = 0; i < elems->supp_rates_len +
2519 elems->ext_supp_rates_len; i++) {
2522 if (i < elems->supp_rates_len)
2523 rate = elems->supp_rates[i];
2524 else if (elems->ext_supp_rates)
2525 rate = elems->ext_supp_rates
2526 [i - elems->supp_rates_len];
2527 own_rate = 5 * (rate & 0x7f);
2528 for (j = 0; j < num_rates; j++)
2529 if (bitrates[j].bitrate == own_rate)
2530 supp_rates |= BIT(j);
2536 static void ieee80211_rx_bss_info(struct net_device *dev,
2537 struct ieee80211_mgmt *mgmt,
2539 struct ieee80211_rx_status *rx_status,
2542 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2543 struct ieee802_11_elems elems;
2546 struct ieee80211_sta_bss *bss;
2547 struct sta_info *sta;
2548 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2549 u64 beacon_timestamp, rx_timestamp;
2550 struct ieee80211_channel *channel;
2551 DECLARE_MAC_BUF(mac);
2552 DECLARE_MAC_BUF(mac2);
2554 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2555 return; /* ignore ProbeResp to foreign address */
2558 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
2559 dev->name, beacon ? "Beacon" : "Probe Response",
2560 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
2563 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2567 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2568 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2570 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2571 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2572 u64 rates = ieee80211_sta_get_rates(local, &elems,
2575 mesh_neighbour_update(mgmt->sa, rates, dev,
2576 mesh_peer_accepts_plinks(&elems, dev));
2581 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
2582 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2583 (sta = sta_info_get(local, mgmt->sa))) {
2585 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2588 prev_rates = sta->supp_rates[rx_status->band];
2589 sta->supp_rates[rx_status->band] &= supp_rates;
2590 if (sta->supp_rates[rx_status->band] == 0) {
2591 /* No matching rates - this should not really happen.
2592 * Make sure that at least one rate is marked
2593 * supported to avoid issues with TX rate ctrl. */
2594 sta->supp_rates[rx_status->band] =
2595 sdata->u.sta.supp_rates_bits[rx_status->band];
2597 if (sta->supp_rates[rx_status->band] != prev_rates) {
2598 printk(KERN_DEBUG "%s: updated supp_rates set for "
2599 "%s based on beacon info (0x%llx & 0x%llx -> "
2601 dev->name, print_mac(mac, sta->addr),
2602 (unsigned long long) prev_rates,
2603 (unsigned long long) supp_rates,
2604 (unsigned long long) sta->supp_rates[rx_status->band]);
2610 if (elems.ds_params && elems.ds_params_len == 1)
2611 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
2613 freq = rx_status->freq;
2615 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2617 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2620 #ifdef CONFIG_MAC80211_MESH
2621 if (elems.mesh_config)
2622 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2623 elems.mesh_id_len, elems.mesh_config, freq);
2626 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2627 elems.ssid, elems.ssid_len);
2629 #ifdef CONFIG_MAC80211_MESH
2630 if (elems.mesh_config)
2631 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2632 elems.mesh_id_len, elems.mesh_config,
2633 elems.mesh_config_len, freq);
2636 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2637 elems.ssid, elems.ssid_len);
2642 /* TODO: order by RSSI? */
2643 spin_lock_bh(&local->sta_bss_lock);
2644 list_move_tail(&bss->list, &local->sta_bss_list);
2645 spin_unlock_bh(&local->sta_bss_lock);
2649 /* save the ERP value so that it is available at association time */
2650 if (elems.erp_info && elems.erp_info_len >= 1) {
2651 bss->erp_value = elems.erp_info[0];
2652 bss->has_erp_value = 1;
2655 if (elems.ht_cap_elem &&
2656 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2657 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2659 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2661 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2662 elems.ht_cap_elem_len + 2);
2663 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2666 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2672 if (elems.ht_info_elem &&
2674 bss->ht_add_ie_len != elems.ht_info_elem_len ||
2675 memcmp(bss->ht_add_ie, elems.ht_info_elem,
2676 elems.ht_info_elem_len))) {
2677 kfree(bss->ht_add_ie);
2679 kmalloc(elems.ht_info_elem_len + 2, GFP_ATOMIC);
2680 if (bss->ht_add_ie) {
2681 memcpy(bss->ht_add_ie, elems.ht_info_elem - 2,
2682 elems.ht_info_elem_len + 2);
2683 bss->ht_add_ie_len = elems.ht_info_elem_len + 2;
2685 bss->ht_add_ie_len = 0;
2686 } else if (!elems.ht_info_elem && bss->ht_add_ie) {
2687 kfree(bss->ht_add_ie);
2688 bss->ht_add_ie = NULL;
2689 bss->ht_add_ie_len = 0;
2692 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2693 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2695 bss->supp_rates_len = 0;
2696 if (elems.supp_rates) {
2697 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2698 if (clen > elems.supp_rates_len)
2699 clen = elems.supp_rates_len;
2700 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2702 bss->supp_rates_len += clen;
2704 if (elems.ext_supp_rates) {
2705 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2706 if (clen > elems.ext_supp_rates_len)
2707 clen = elems.ext_supp_rates_len;
2708 memcpy(&bss->supp_rates[bss->supp_rates_len],
2709 elems.ext_supp_rates, clen);
2710 bss->supp_rates_len += clen;
2713 bss->band = rx_status->band;
2715 bss->timestamp = beacon_timestamp;
2716 bss->last_update = jiffies;
2717 bss->signal = rx_status->signal;
2718 bss->noise = rx_status->noise;
2719 bss->qual = rx_status->qual;
2720 if (!beacon && !bss->probe_resp)
2721 bss->probe_resp = true;
2724 * In STA mode, the remaining parameters should not be overridden
2725 * by beacons because they're not necessarily accurate there.
2727 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2728 bss->probe_resp && beacon) {
2729 ieee80211_rx_bss_put(dev, bss);
2734 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2735 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2737 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2739 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2740 bss->wpa_ie_len = elems.wpa_len + 2;
2742 bss->wpa_ie_len = 0;
2743 } else if (!elems.wpa && bss->wpa_ie) {
2746 bss->wpa_ie_len = 0;
2750 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2751 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2753 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2755 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2756 bss->rsn_ie_len = elems.rsn_len + 2;
2758 bss->rsn_ie_len = 0;
2759 } else if (!elems.rsn && bss->rsn_ie) {
2762 bss->rsn_ie_len = 0;
2767 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2771 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2772 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2773 * Alliance (September 1, 2004) is incorporated by reference herein.
2774 * The inclusion of the WMM Parameters in probe responses and
2775 * association responses is mandatory for WMM enabled networks. The
2776 * inclusion of the WMM Parameters in beacons, however, is optional.
2779 if (elems.wmm_param &&
2780 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2781 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2783 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2785 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2786 elems.wmm_param_len + 2);
2787 bss->wmm_ie_len = elems.wmm_param_len + 2;
2789 bss->wmm_ie_len = 0;
2790 } else if (elems.wmm_info &&
2791 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_info_len ||
2792 memcmp(bss->wmm_ie, elems.wmm_info, elems.wmm_info_len))) {
2793 /* As for certain AP's Fifth bit is not set in WMM IE in
2794 * beacon frames.So while parsing the beacon frame the
2795 * wmm_info structure is used instead of wmm_param.
2796 * wmm_info structure was never used to set bss->wmm_ie.
2797 * This code fixes this problem by copying the WME
2798 * information from wmm_info to bss->wmm_ie and enabling
2799 * n-band association.
2802 bss->wmm_ie = kmalloc(elems.wmm_info_len + 2, GFP_ATOMIC);
2804 memcpy(bss->wmm_ie, elems.wmm_info - 2,
2805 elems.wmm_info_len + 2);
2806 bss->wmm_ie_len = elems.wmm_info_len + 2;
2808 bss->wmm_ie_len = 0;
2809 } else if (!elems.wmm_param && !elems.wmm_info && bss->wmm_ie) {
2812 bss->wmm_ie_len = 0;
2815 /* check if we need to merge IBSS */
2816 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2817 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2818 bss->capability & WLAN_CAPABILITY_IBSS &&
2819 bss->freq == local->oper_channel->center_freq &&
2820 elems.ssid_len == sdata->u.sta.ssid_len &&
2821 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2822 if (rx_status->flag & RX_FLAG_TSFT) {
2823 /* in order for correct IBSS merging we need mactime
2825 * since mactime is defined as the time the first data
2826 * symbol of the frame hits the PHY, and the timestamp
2827 * of the beacon is defined as "the time that the data
2828 * symbol containing the first bit of the timestamp is
2829 * transmitted to the PHY plus the transmitting STA’s
2830 * delays through its local PHY from the MAC-PHY
2831 * interface to its interface with the WM"
2832 * (802.11 11.1.2) - equals the time this bit arrives at
2833 * the receiver - we have to take into account the
2834 * offset between the two.
2835 * e.g: at 1 MBit that means mactime is 192 usec earlier
2836 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2838 int rate = local->hw.wiphy->bands[rx_status->band]->
2839 bitrates[rx_status->rate_idx].bitrate;
2840 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2841 } else if (local && local->ops && local->ops->get_tsf)
2842 /* second best option: get current TSF */
2843 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2845 /* can't merge without knowing the TSF */
2846 rx_timestamp = -1LLU;
2847 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2848 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2849 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2850 print_mac(mac, mgmt->sa),
2851 print_mac(mac2, mgmt->bssid),
2852 (unsigned long long)rx_timestamp,
2853 (unsigned long long)beacon_timestamp,
2854 (unsigned long long)(rx_timestamp - beacon_timestamp),
2856 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2857 if (beacon_timestamp > rx_timestamp) {
2858 #ifndef CONFIG_MAC80211_IBSS_DEBUG
2859 if (net_ratelimit())
2861 printk(KERN_DEBUG "%s: beacon TSF higher than "
2862 "local TSF - IBSS merge with BSSID %s\n",
2863 dev->name, print_mac(mac, mgmt->bssid));
2864 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2865 ieee80211_ibss_add_sta(dev, NULL,
2866 mgmt->bssid, mgmt->sa);
2870 ieee80211_rx_bss_put(dev, bss);
2874 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2875 struct ieee80211_mgmt *mgmt,
2877 struct ieee80211_rx_status *rx_status)
2879 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2883 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2884 struct ieee80211_mgmt *mgmt,
2886 struct ieee80211_rx_status *rx_status)
2888 struct ieee80211_sub_if_data *sdata;
2889 struct ieee80211_if_sta *ifsta;
2891 struct ieee802_11_elems elems;
2892 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2893 struct ieee80211_conf *conf = &local->hw.conf;
2896 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 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 /* Process beacon from the current BSS */
2908 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2912 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2914 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2915 elems.wmm_param_len);
2917 /* Do not send changes to driver if we are scanning. This removes
2918 * requirement that driver's bss_info_changed function needs to be
2920 if (local->sta_sw_scanning || local->sta_hw_scanning)
2923 if (elems.erp_info && elems.erp_info_len >= 1)
2924 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2926 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2927 changed |= ieee80211_handle_protect_preamb(sdata, false,
2928 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2931 if (elems.ht_cap_elem && elems.ht_info_elem &&
2932 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2933 struct ieee80211_ht_bss_info bss_info;
2935 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2936 (struct ieee80211_ht_addt_info *)
2937 elems.ht_info_elem, &bss_info);
2938 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2942 ieee80211_bss_info_change_notify(sdata, changed);
2946 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2947 struct ieee80211_if_sta *ifsta,
2948 struct ieee80211_mgmt *mgmt,
2950 struct ieee80211_rx_status *rx_status)
2952 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2953 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2955 struct sk_buff *skb;
2956 struct ieee80211_mgmt *resp;
2958 DECLARE_MAC_BUF(mac);
2959 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2960 DECLARE_MAC_BUF(mac2);
2961 DECLARE_MAC_BUF(mac3);
2964 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2965 ifsta->state != IEEE80211_IBSS_JOINED ||
2966 len < 24 + 2 || !ifsta->probe_resp)
2969 if (local->ops->tx_last_beacon)
2970 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2974 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2975 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2976 "%s (tx_last_beacon=%d)\n",
2977 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2978 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2979 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2981 if (!tx_last_beacon)
2984 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2985 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2988 end = ((u8 *) mgmt) + len;
2989 pos = mgmt->u.probe_req.variable;
2990 if (pos[0] != WLAN_EID_SSID ||
2991 pos + 2 + pos[1] > end) {
2992 if (net_ratelimit()) {
2993 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2995 dev->name, print_mac(mac, mgmt->sa));
3000 (pos[1] != ifsta->ssid_len ||
3001 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
3002 /* Ignore ProbeReq for foreign SSID */
3006 /* Reply with ProbeResp */
3007 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
3011 resp = (struct ieee80211_mgmt *) skb->data;
3012 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3013 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3014 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3015 dev->name, print_mac(mac, resp->da));
3016 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3017 ieee80211_sta_tx(dev, skb, 0);
3020 static void ieee80211_rx_mgmt_action(struct net_device *dev,
3021 struct ieee80211_if_sta *ifsta,
3022 struct ieee80211_mgmt *mgmt,
3024 struct ieee80211_rx_status *rx_status)
3026 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3028 if (len < IEEE80211_MIN_ACTION_SIZE)
3031 switch (mgmt->u.action.category) {
3032 case WLAN_CATEGORY_BACK:
3033 switch (mgmt->u.action.u.addba_req.action_code) {
3034 case WLAN_ACTION_ADDBA_REQ:
3035 if (len < (IEEE80211_MIN_ACTION_SIZE +
3036 sizeof(mgmt->u.action.u.addba_req)))
3038 ieee80211_sta_process_addba_request(dev, mgmt, len);
3040 case WLAN_ACTION_ADDBA_RESP:
3041 if (len < (IEEE80211_MIN_ACTION_SIZE +
3042 sizeof(mgmt->u.action.u.addba_resp)))
3044 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3046 case WLAN_ACTION_DELBA:
3047 if (len < (IEEE80211_MIN_ACTION_SIZE +
3048 sizeof(mgmt->u.action.u.delba)))
3050 ieee80211_sta_process_delba(dev, mgmt, len);
3053 if (net_ratelimit())
3054 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
3059 case PLINK_CATEGORY:
3060 if (ieee80211_vif_is_mesh(&sdata->vif))
3061 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3063 case MESH_PATH_SEL_CATEGORY:
3064 if (ieee80211_vif_is_mesh(&sdata->vif))
3065 mesh_rx_path_sel_frame(dev, mgmt, len);
3068 if (net_ratelimit())
3069 printk(KERN_DEBUG "%s: Rx unknown action frame - "
3070 "category=%d\n", dev->name, mgmt->u.action.category);
3075 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3076 struct ieee80211_rx_status *rx_status)
3078 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3079 struct ieee80211_sub_if_data *sdata;
3080 struct ieee80211_if_sta *ifsta;
3081 struct ieee80211_mgmt *mgmt;
3087 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3088 ifsta = &sdata->u.sta;
3090 mgmt = (struct ieee80211_mgmt *) skb->data;
3091 fc = le16_to_cpu(mgmt->frame_control);
3093 switch (fc & IEEE80211_FCTL_STYPE) {
3094 case IEEE80211_STYPE_PROBE_REQ:
3095 case IEEE80211_STYPE_PROBE_RESP:
3096 case IEEE80211_STYPE_BEACON:
3097 case IEEE80211_STYPE_ACTION:
3098 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3099 case IEEE80211_STYPE_AUTH:
3100 case IEEE80211_STYPE_ASSOC_RESP:
3101 case IEEE80211_STYPE_REASSOC_RESP:
3102 case IEEE80211_STYPE_DEAUTH:
3103 case IEEE80211_STYPE_DISASSOC:
3104 skb_queue_tail(&ifsta->skb_queue, skb);
3105 queue_work(local->hw.workqueue, &ifsta->work);
3108 printk(KERN_DEBUG "%s: received unknown management frame - "
3109 "stype=%d\n", dev->name,
3110 (fc & IEEE80211_FCTL_STYPE) >> 4);
3119 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3120 struct sk_buff *skb)
3122 struct ieee80211_rx_status *rx_status;
3123 struct ieee80211_sub_if_data *sdata;
3124 struct ieee80211_if_sta *ifsta;
3125 struct ieee80211_mgmt *mgmt;
3128 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3129 ifsta = &sdata->u.sta;
3131 rx_status = (struct ieee80211_rx_status *) skb->cb;
3132 mgmt = (struct ieee80211_mgmt *) skb->data;
3133 fc = le16_to_cpu(mgmt->frame_control);
3135 switch (fc & IEEE80211_FCTL_STYPE) {
3136 case IEEE80211_STYPE_PROBE_REQ:
3137 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3140 case IEEE80211_STYPE_PROBE_RESP:
3141 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3143 case IEEE80211_STYPE_BEACON:
3144 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3146 case IEEE80211_STYPE_AUTH:
3147 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3149 case IEEE80211_STYPE_ASSOC_RESP:
3150 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
3152 case IEEE80211_STYPE_REASSOC_RESP:
3153 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
3155 case IEEE80211_STYPE_DEAUTH:
3156 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3158 case IEEE80211_STYPE_DISASSOC:
3159 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3161 case IEEE80211_STYPE_ACTION:
3162 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
3171 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3172 struct ieee80211_rx_status *rx_status)
3174 struct ieee80211_mgmt *mgmt;
3178 return RX_DROP_UNUSABLE;
3180 mgmt = (struct ieee80211_mgmt *) skb->data;
3181 fc = le16_to_cpu(mgmt->frame_control);
3183 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
3187 return RX_DROP_MONITOR;
3189 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3190 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3191 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3192 skb->len, rx_status);
3195 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3196 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3206 static int ieee80211_sta_active_ibss(struct net_device *dev)
3208 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3210 struct sta_info *sta;
3211 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3215 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3216 if (sta->sdata == sdata &&
3217 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3230 static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
3232 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3233 struct sta_info *sta, *tmp;
3234 LIST_HEAD(tmp_list);
3235 DECLARE_MAC_BUF(mac);
3236 unsigned long flags;
3238 spin_lock_irqsave(&local->sta_lock, flags);
3239 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3240 if (time_after(jiffies, sta->last_rx + exp_time)) {
3241 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3242 dev->name, print_mac(mac, sta->addr));
3243 __sta_info_unlink(&sta);
3245 list_add(&sta->list, &tmp_list);
3247 spin_unlock_irqrestore(&local->sta_lock, flags);
3249 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3250 sta_info_destroy(sta);
3254 static void ieee80211_sta_merge_ibss(struct net_device *dev,
3255 struct ieee80211_if_sta *ifsta)
3257 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3259 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
3260 if (ieee80211_sta_active_ibss(dev))
3263 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3264 "IBSS networks with same SSID (merge)\n", dev->name);
3265 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3269 #ifdef CONFIG_MAC80211_MESH
3270 static void ieee80211_mesh_housekeeping(struct net_device *dev,
3271 struct ieee80211_if_sta *ifsta)
3273 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3276 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3277 mesh_path_expire(dev);
3279 free_plinks = mesh_plink_availables(sdata);
3280 if (free_plinks != sdata->u.sta.accepting_plinks)
3281 ieee80211_if_config_beacon(dev);
3283 mod_timer(&ifsta->timer, jiffies +
3284 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3288 void ieee80211_start_mesh(struct net_device *dev)
3290 struct ieee80211_if_sta *ifsta;
3291 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3292 ifsta = &sdata->u.sta;
3293 ifsta->state = IEEE80211_MESH_UP;
3294 ieee80211_sta_timer((unsigned long)sdata);
3299 void ieee80211_sta_timer(unsigned long data)
3301 struct ieee80211_sub_if_data *sdata =
3302 (struct ieee80211_sub_if_data *) data;
3303 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3304 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3306 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3307 queue_work(local->hw.workqueue, &ifsta->work);
3310 void ieee80211_sta_work(struct work_struct *work)
3312 struct ieee80211_sub_if_data *sdata =
3313 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3314 struct net_device *dev = sdata->dev;
3315 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3316 struct ieee80211_if_sta *ifsta;
3317 struct sk_buff *skb;
3319 if (!netif_running(dev))
3322 if (local->sta_sw_scanning || local->sta_hw_scanning)
3325 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3326 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3327 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
3328 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
3329 "(type=%d)\n", dev->name, sdata->vif.type);
3332 ifsta = &sdata->u.sta;
3334 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3335 ieee80211_sta_rx_queued_mgmt(dev, skb);
3337 #ifdef CONFIG_MAC80211_MESH
3338 if (ifsta->preq_queue_len &&
3340 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3341 mesh_path_start_discovery(dev);
3344 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3345 ifsta->state != IEEE80211_ASSOCIATE &&
3346 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3347 if (ifsta->scan_ssid_len)
3348 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3350 ieee80211_sta_start_scan(dev, NULL, 0);
3354 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3355 if (ieee80211_sta_config_auth(dev, ifsta))
3357 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3358 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3361 switch (ifsta->state) {
3362 case IEEE80211_DISABLED:
3364 case IEEE80211_AUTHENTICATE:
3365 ieee80211_authenticate(dev, ifsta);
3367 case IEEE80211_ASSOCIATE:
3368 ieee80211_associate(dev, ifsta);
3370 case IEEE80211_ASSOCIATED:
3371 ieee80211_associated(dev, ifsta);
3373 case IEEE80211_IBSS_SEARCH:
3374 ieee80211_sta_find_ibss(dev, ifsta);
3376 case IEEE80211_IBSS_JOINED:
3377 ieee80211_sta_merge_ibss(dev, ifsta);
3379 #ifdef CONFIG_MAC80211_MESH
3380 case IEEE80211_MESH_UP:
3381 ieee80211_mesh_housekeeping(dev, ifsta);
3385 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3390 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3391 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3392 "mixed-cell disabled - disassociate\n", dev->name);
3394 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3395 ieee80211_set_disassoc(dev, ifsta, 0);
3400 static void ieee80211_sta_reset_auth(struct net_device *dev,
3401 struct ieee80211_if_sta *ifsta)
3403 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3405 if (local->ops->reset_tsf) {
3406 /* Reset own TSF to allow time synchronization work. */
3407 local->ops->reset_tsf(local_to_hw(local));
3410 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3413 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3414 ifsta->auth_alg = WLAN_AUTH_OPEN;
3415 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3416 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3417 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3418 ifsta->auth_alg = WLAN_AUTH_LEAP;
3420 ifsta->auth_alg = WLAN_AUTH_OPEN;
3421 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3423 ifsta->auth_transaction = -1;
3424 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3425 ifsta->auth_tries = ifsta->assoc_tries = 0;
3426 netif_carrier_off(dev);
3430 void ieee80211_sta_req_auth(struct net_device *dev,
3431 struct ieee80211_if_sta *ifsta)
3433 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3434 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3436 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3439 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3440 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3441 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3442 IEEE80211_STA_AUTO_SSID_SEL))) {
3443 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3444 queue_work(local->hw.workqueue, &ifsta->work);
3448 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3449 const char *ssid, int ssid_len)
3451 int tmp, hidden_ssid;
3453 if (ssid_len == ifsta->ssid_len &&
3454 !memcmp(ifsta->ssid, ssid, ssid_len))
3457 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3463 if (ssid[tmp] != '\0') {
3469 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3472 if (ssid_len == 1 && ssid[0] == ' ')
3478 static int ieee80211_sta_config_auth(struct net_device *dev,
3479 struct ieee80211_if_sta *ifsta)
3481 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3482 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3483 struct ieee80211_sta_bss *bss, *selected = NULL;
3484 int top_rssi = 0, freq;
3486 spin_lock_bh(&local->sta_bss_lock);
3487 freq = local->oper_channel->center_freq;
3488 list_for_each_entry(bss, &local->sta_bss_list, list) {
3489 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3492 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3493 IEEE80211_STA_AUTO_BSSID_SEL |
3494 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3495 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3496 !!sdata->default_key))
3499 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3503 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3504 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3507 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3508 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3511 if (!selected || top_rssi < bss->signal) {
3513 top_rssi = bss->signal;
3517 atomic_inc(&selected->users);
3518 spin_unlock_bh(&local->sta_bss_lock);
3521 ieee80211_set_freq(dev, selected->freq);
3522 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3523 ieee80211_sta_set_ssid(dev, selected->ssid,
3524 selected->ssid_len);
3525 ieee80211_sta_set_bssid(dev, selected->bssid);
3526 ieee80211_sta_def_wmm_params(dev, selected, 0);
3527 ieee80211_rx_bss_put(dev, selected);
3528 ifsta->state = IEEE80211_AUTHENTICATE;
3529 ieee80211_sta_reset_auth(dev, ifsta);
3532 if (ifsta->state != IEEE80211_AUTHENTICATE) {
3533 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3534 ieee80211_sta_start_scan(dev, NULL, 0);
3536 ieee80211_sta_start_scan(dev, ifsta->ssid,
3538 ifsta->state = IEEE80211_AUTHENTICATE;
3539 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3541 ifsta->state = IEEE80211_DISABLED;
3547 static int ieee80211_sta_create_ibss(struct net_device *dev,
3548 struct ieee80211_if_sta *ifsta)
3550 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3551 struct ieee80211_sta_bss *bss;
3552 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3553 struct ieee80211_supported_band *sband;
3554 u8 bssid[ETH_ALEN], *pos;
3557 DECLARE_MAC_BUF(mac);
3560 /* Easier testing, use fixed BSSID. */
3561 memset(bssid, 0xfe, ETH_ALEN);
3563 /* Generate random, not broadcast, locally administered BSSID. Mix in
3564 * own MAC address to make sure that devices that do not have proper
3565 * random number generator get different BSSID. */
3566 get_random_bytes(bssid, ETH_ALEN);
3567 for (i = 0; i < ETH_ALEN; i++)
3568 bssid[i] ^= dev->dev_addr[i];
3573 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3574 dev->name, print_mac(mac, bssid));
3576 bss = ieee80211_rx_bss_add(dev, bssid,
3577 local->hw.conf.channel->center_freq,
3578 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3582 bss->band = local->hw.conf.channel->band;
3583 sband = local->hw.wiphy->bands[bss->band];
3585 if (local->hw.conf.beacon_int == 0)
3586 local->hw.conf.beacon_int = 10000;
3587 bss->beacon_int = local->hw.conf.beacon_int;
3588 bss->last_update = jiffies;
3589 bss->capability = WLAN_CAPABILITY_IBSS;
3591 if (sdata->default_key)
3592 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3594 sdata->drop_unencrypted = 0;
3596 bss->supp_rates_len = sband->n_bitrates;
3597 pos = bss->supp_rates;
3598 for (i = 0; i < sband->n_bitrates; i++) {
3599 int rate = sband->bitrates[i].bitrate;
3600 *pos++ = (u8) (rate / 5);
3603 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3604 ieee80211_rx_bss_put(dev, bss);
3609 static int ieee80211_sta_find_ibss(struct net_device *dev,
3610 struct ieee80211_if_sta *ifsta)
3612 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3613 struct ieee80211_sta_bss *bss;
3617 DECLARE_MAC_BUF(mac);
3618 DECLARE_MAC_BUF(mac2);
3620 if (ifsta->ssid_len == 0)
3623 active_ibss = ieee80211_sta_active_ibss(dev);
3624 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3625 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3626 dev->name, active_ibss);
3627 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3628 spin_lock_bh(&local->sta_bss_lock);
3629 list_for_each_entry(bss, &local->sta_bss_list, list) {
3630 if (ifsta->ssid_len != bss->ssid_len ||
3631 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3632 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3634 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3635 printk(KERN_DEBUG " bssid=%s found\n",
3636 print_mac(mac, bss->bssid));
3637 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3638 memcpy(bssid, bss->bssid, ETH_ALEN);
3640 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3643 spin_unlock_bh(&local->sta_bss_lock);
3645 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3646 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3647 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
3648 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3649 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
3650 (bss = ieee80211_rx_bss_get(dev, bssid,
3651 local->hw.conf.channel->center_freq,
3652 ifsta->ssid, ifsta->ssid_len))) {
3654 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3655 " based on configured SSID\n",
3656 dev->name, print_mac(mac, bssid));
3657 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3658 ieee80211_rx_bss_put(dev, bss);
3661 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3662 printk(KERN_DEBUG " did not try to join ibss\n");
3663 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3665 /* Selected IBSS not found in current scan results - try to scan */
3666 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3667 !ieee80211_sta_active_ibss(dev)) {
3668 mod_timer(&ifsta->timer, jiffies +
3669 IEEE80211_IBSS_MERGE_INTERVAL);
3670 } else if (time_after(jiffies, local->last_scan_completed +
3671 IEEE80211_SCAN_INTERVAL)) {
3672 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3673 "join\n", dev->name);
3674 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3676 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3677 int interval = IEEE80211_SCAN_INTERVAL;
3679 if (time_after(jiffies, ifsta->ibss_join_req +
3680 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3681 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3682 (!(local->oper_channel->flags &
3683 IEEE80211_CHAN_NO_IBSS)))
3684 return ieee80211_sta_create_ibss(dev, ifsta);
3685 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3686 printk(KERN_DEBUG "%s: IBSS not allowed on"
3687 " %d MHz\n", dev->name,
3688 local->hw.conf.channel->center_freq);
3691 /* No IBSS found - decrease scan interval and continue
3693 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3696 ifsta->state = IEEE80211_IBSS_SEARCH;
3697 mod_timer(&ifsta->timer, jiffies + interval);
3705 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3707 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3708 struct ieee80211_if_sta *ifsta;
3710 if (len > IEEE80211_MAX_SSID_LEN)
3713 ifsta = &sdata->u.sta;
3715 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
3716 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3717 memcpy(ifsta->ssid, ssid, len);
3718 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3719 ifsta->ssid_len = len;
3722 ifsta->flags |= IEEE80211_STA_SSID_SET;
3724 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3725 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3726 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3727 ifsta->ibss_join_req = jiffies;
3728 ifsta->state = IEEE80211_IBSS_SEARCH;
3729 return ieee80211_sta_find_ibss(dev, ifsta);
3735 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3737 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3738 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3739 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3740 *len = ifsta->ssid_len;
3745 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3747 struct ieee80211_sub_if_data *sdata;
3748 struct ieee80211_if_sta *ifsta;
3751 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3752 ifsta = &sdata->u.sta;
3754 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3755 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3756 res = ieee80211_if_config(dev);
3758 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3759 "the low-level driver\n", dev->name);
3764 if (is_valid_ether_addr(bssid))
3765 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3767 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3773 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3774 struct ieee80211_sub_if_data *sdata,
3777 struct sk_buff *skb;
3778 struct ieee80211_hdr *nullfunc;
3781 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3783 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3784 "frame\n", sdata->dev->name);
3787 skb_reserve(skb, local->hw.extra_tx_headroom);
3789 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3790 memset(nullfunc, 0, 24);
3791 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3792 IEEE80211_FCTL_TODS;
3794 fc |= IEEE80211_FCTL_PM;
3795 nullfunc->frame_control = cpu_to_le16(fc);
3796 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3797 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3798 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3800 ieee80211_sta_tx(sdata->dev, skb, 0);
3804 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3806 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3807 ieee80211_vif_is_mesh(&sdata->vif))
3808 ieee80211_sta_timer((unsigned long)sdata);
3811 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3813 struct ieee80211_local *local = hw_to_local(hw);
3814 struct net_device *dev = local->scan_dev;
3815 struct ieee80211_sub_if_data *sdata;
3816 union iwreq_data wrqu;
3818 local->last_scan_completed = jiffies;
3819 memset(&wrqu, 0, sizeof(wrqu));
3820 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3822 if (local->sta_hw_scanning) {
3823 local->sta_hw_scanning = 0;
3824 if (ieee80211_hw_config(local))
3825 printk(KERN_DEBUG "%s: failed to restore operational "
3826 "channel after scan\n", dev->name);
3827 /* Restart STA timer for HW scan case */
3829 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3830 ieee80211_restart_sta_timer(sdata);
3836 local->sta_sw_scanning = 0;
3837 if (ieee80211_hw_config(local))
3838 printk(KERN_DEBUG "%s: failed to restore operational "
3839 "channel after scan\n", dev->name);
3842 netif_tx_lock_bh(local->mdev);
3843 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3844 local->ops->configure_filter(local_to_hw(local),
3845 FIF_BCN_PRBRESP_PROMISC,
3846 &local->filter_flags,
3847 local->mdev->mc_count,
3848 local->mdev->mc_list);
3850 netif_tx_unlock_bh(local->mdev);
3853 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3855 /* No need to wake the master device. */
3856 if (sdata->dev == local->mdev)
3859 /* Tell AP we're back */
3860 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3861 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3862 ieee80211_send_nullfunc(local, sdata, 0);
3864 ieee80211_restart_sta_timer(sdata);
3866 netif_wake_queue(sdata->dev);
3871 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3872 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3873 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3874 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3875 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3876 !ieee80211_sta_active_ibss(dev)))
3877 ieee80211_sta_find_ibss(dev, ifsta);
3880 EXPORT_SYMBOL(ieee80211_scan_completed);
3882 void ieee80211_sta_scan_work(struct work_struct *work)
3884 struct ieee80211_local *local =
3885 container_of(work, struct ieee80211_local, scan_work.work);
3886 struct net_device *dev = local->scan_dev;
3887 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3888 struct ieee80211_supported_band *sband;
3889 struct ieee80211_channel *chan;
3891 unsigned long next_delay = 0;
3893 if (!local->sta_sw_scanning)
3896 switch (local->scan_state) {
3897 case SCAN_SET_CHANNEL:
3899 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3900 * after we successfully scanned the last channel of the last
3901 * band (and the last band is supported by the hw)
3903 if (local->scan_band < IEEE80211_NUM_BANDS)
3904 sband = local->hw.wiphy->bands[local->scan_band];
3909 * If we are at an unsupported band and have more bands
3910 * left to scan, advance to the next supported one.
3912 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3914 sband = local->hw.wiphy->bands[local->scan_band];
3915 local->scan_channel_idx = 0;
3918 /* if no more bands/channels left, complete scan */
3919 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3920 ieee80211_scan_completed(local_to_hw(local));
3924 chan = &sband->channels[local->scan_channel_idx];
3926 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3927 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3928 chan->flags & IEEE80211_CHAN_NO_IBSS))
3932 local->scan_channel = chan;
3933 if (ieee80211_hw_config(local)) {
3934 printk(KERN_DEBUG "%s: failed to set freq to "
3935 "%d MHz for scan\n", dev->name,
3941 /* advance state machine to next channel/band */
3942 local->scan_channel_idx++;
3943 if (local->scan_channel_idx >= sband->n_channels) {
3945 * scan_band may end up == IEEE80211_NUM_BANDS, but
3946 * we'll catch that case above and complete the scan
3947 * if that is the case.
3950 local->scan_channel_idx = 0;
3956 next_delay = IEEE80211_PROBE_DELAY +
3957 usecs_to_jiffies(local->hw.channel_change_time);
3958 local->scan_state = SCAN_SEND_PROBE;
3960 case SCAN_SEND_PROBE:
3961 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3962 local->scan_state = SCAN_SET_CHANNEL;
3964 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3966 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3967 local->scan_ssid_len);
3968 next_delay = IEEE80211_CHANNEL_TIME;
3972 if (local->sta_sw_scanning)
3973 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3978 static int ieee80211_sta_start_scan(struct net_device *dev,
3979 u8 *ssid, size_t ssid_len)
3981 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3982 struct ieee80211_sub_if_data *sdata;
3984 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3987 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3988 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3991 * ScanType: ACTIVE, PASSIVE
3992 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3993 * a Probe frame during active scanning
3995 * MinChannelTime (>= ProbeDelay), in TU
3996 * MaxChannelTime: (>= MinChannelTime), in TU
3999 /* MLME-SCAN.confirm
4001 * ResultCode: SUCCESS, INVALID_PARAMETERS
4004 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4005 if (local->scan_dev == dev)
4010 if (local->ops->hw_scan) {
4011 int rc = local->ops->hw_scan(local_to_hw(local),
4014 local->sta_hw_scanning = 1;
4015 local->scan_dev = dev;
4020 local->sta_sw_scanning = 1;
4023 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4025 /* Don't stop the master interface, otherwise we can't transmit
4027 if (sdata->dev == local->mdev)
4030 netif_stop_queue(sdata->dev);
4031 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
4032 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
4033 ieee80211_send_nullfunc(local, sdata, 1);
4038 local->scan_ssid_len = ssid_len;
4039 memcpy(local->scan_ssid, ssid, ssid_len);
4041 local->scan_ssid_len = 0;
4042 local->scan_state = SCAN_SET_CHANNEL;
4043 local->scan_channel_idx = 0;
4044 local->scan_band = IEEE80211_BAND_2GHZ;
4045 local->scan_dev = dev;
4047 netif_tx_lock_bh(local->mdev);
4048 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4049 local->ops->configure_filter(local_to_hw(local),
4050 FIF_BCN_PRBRESP_PROMISC,
4051 &local->filter_flags,
4052 local->mdev->mc_count,
4053 local->mdev->mc_list);
4054 netif_tx_unlock_bh(local->mdev);
4056 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4057 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4058 IEEE80211_CHANNEL_TIME);
4064 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4066 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4067 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4068 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4070 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4071 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4073 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4074 if (local->scan_dev == dev)
4079 ifsta->scan_ssid_len = ssid_len;
4081 memcpy(ifsta->scan_ssid, ssid, ssid_len);
4082 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4083 queue_work(local->hw.workqueue, &ifsta->work);
4088 ieee80211_sta_scan_result(struct net_device *dev,
4089 struct ieee80211_sta_bss *bss,
4090 char *current_ev, char *end_buf)
4092 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4093 struct iw_event iwe;
4095 if (time_after(jiffies,
4096 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4099 memset(&iwe, 0, sizeof(iwe));
4100 iwe.cmd = SIOCGIWAP;
4101 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4102 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4103 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4106 memset(&iwe, 0, sizeof(iwe));
4107 iwe.cmd = SIOCGIWESSID;
4108 if (bss_mesh_cfg(bss)) {
4109 iwe.u.data.length = bss_mesh_id_len(bss);
4110 iwe.u.data.flags = 1;
4111 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4114 iwe.u.data.length = bss->ssid_len;
4115 iwe.u.data.flags = 1;
4116 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4120 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4121 || bss_mesh_cfg(bss)) {
4122 memset(&iwe, 0, sizeof(iwe));
4123 iwe.cmd = SIOCGIWMODE;
4124 if (bss_mesh_cfg(bss))
4125 iwe.u.mode = IW_MODE_MESH;
4126 else if (bss->capability & WLAN_CAPABILITY_ESS)
4127 iwe.u.mode = IW_MODE_MASTER;
4129 iwe.u.mode = IW_MODE_ADHOC;
4130 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4134 memset(&iwe, 0, sizeof(iwe));
4135 iwe.cmd = SIOCGIWFREQ;
4136 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4138 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4141 memset(&iwe, 0, sizeof(iwe));
4142 iwe.cmd = SIOCGIWFREQ;
4143 iwe.u.freq.m = bss->freq;
4145 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4147 memset(&iwe, 0, sizeof(iwe));
4149 iwe.u.qual.qual = bss->qual;
4150 iwe.u.qual.level = bss->signal;
4151 iwe.u.qual.noise = bss->noise;
4152 iwe.u.qual.updated = local->wstats_flags;
4153 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4156 memset(&iwe, 0, sizeof(iwe));
4157 iwe.cmd = SIOCGIWENCODE;
4158 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4159 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4161 iwe.u.data.flags = IW_ENCODE_DISABLED;
4162 iwe.u.data.length = 0;
4163 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4165 if (bss && bss->wpa_ie) {
4166 memset(&iwe, 0, sizeof(iwe));
4167 iwe.cmd = IWEVGENIE;
4168 iwe.u.data.length = bss->wpa_ie_len;
4169 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4173 if (bss && bss->rsn_ie) {
4174 memset(&iwe, 0, sizeof(iwe));
4175 iwe.cmd = IWEVGENIE;
4176 iwe.u.data.length = bss->rsn_ie_len;
4177 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4181 if (bss && bss->ht_ie) {
4182 memset(&iwe, 0, sizeof(iwe));
4183 iwe.cmd = IWEVGENIE;
4184 iwe.u.data.length = bss->ht_ie_len;
4185 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4189 if (bss && bss->supp_rates_len > 0) {
4190 /* display all supported rates in readable format */
4191 char *p = current_ev + IW_EV_LCP_LEN;
4194 memset(&iwe, 0, sizeof(iwe));
4195 iwe.cmd = SIOCGIWRATE;
4196 /* Those two flags are ignored... */
4197 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4199 for (i = 0; i < bss->supp_rates_len; i++) {
4200 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4202 p = iwe_stream_add_value(current_ev, p,
4203 end_buf, &iwe, IW_EV_PARAM_LEN);
4210 buf = kmalloc(30, GFP_ATOMIC);
4212 memset(&iwe, 0, sizeof(iwe));
4213 iwe.cmd = IWEVCUSTOM;
4214 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4215 iwe.u.data.length = strlen(buf);
4216 current_ev = iwe_stream_add_point(current_ev, end_buf,
4222 if (bss_mesh_cfg(bss)) {
4224 u8 *cfg = bss_mesh_cfg(bss);
4225 buf = kmalloc(50, GFP_ATOMIC);
4227 memset(&iwe, 0, sizeof(iwe));
4228 iwe.cmd = IWEVCUSTOM;
4229 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4230 iwe.u.data.length = strlen(buf);
4231 current_ev = iwe_stream_add_point(current_ev, end_buf,
4233 sprintf(buf, "Path Selection Protocol ID: "
4234 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4236 iwe.u.data.length = strlen(buf);
4237 current_ev = iwe_stream_add_point(current_ev, end_buf,
4239 sprintf(buf, "Path Selection Metric ID: "
4240 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4242 iwe.u.data.length = strlen(buf);
4243 current_ev = iwe_stream_add_point(current_ev, end_buf,
4245 sprintf(buf, "Congestion Control Mode ID: "
4246 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4248 iwe.u.data.length = strlen(buf);
4249 current_ev = iwe_stream_add_point(current_ev, end_buf,
4251 sprintf(buf, "Channel Precedence: "
4252 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4254 iwe.u.data.length = strlen(buf);
4255 current_ev = iwe_stream_add_point(current_ev, end_buf,
4265 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4267 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4268 char *current_ev = buf;
4269 char *end_buf = buf + len;
4270 struct ieee80211_sta_bss *bss;
4272 spin_lock_bh(&local->sta_bss_lock);
4273 list_for_each_entry(bss, &local->sta_bss_list, list) {
4274 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4275 spin_unlock_bh(&local->sta_bss_lock);
4278 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4281 spin_unlock_bh(&local->sta_bss_lock);
4282 return current_ev - buf;
4286 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4288 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4289 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4291 kfree(ifsta->extra_ie);
4293 ifsta->extra_ie = NULL;
4294 ifsta->extra_ie_len = 0;
4297 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4298 if (!ifsta->extra_ie) {
4299 ifsta->extra_ie_len = 0;
4302 memcpy(ifsta->extra_ie, ie, len);
4303 ifsta->extra_ie_len = len;
4308 struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4309 struct sk_buff *skb, u8 *bssid,
4312 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4313 struct sta_info *sta;
4314 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4315 DECLARE_MAC_BUF(mac);
4317 /* TODO: Could consider removing the least recently used entry and
4318 * allow new one to be added. */
4319 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4320 if (net_ratelimit()) {
4321 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4322 "entry %s\n", dev->name, print_mac(mac, addr));
4327 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4328 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
4330 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4334 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
4336 sta->supp_rates[local->hw.conf.channel->band] =
4337 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
4339 rate_control_rate_init(sta, local);
4341 if (sta_info_insert(sta))
4348 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4350 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4351 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4353 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4356 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4357 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4360 ieee80211_send_deauth(dev, ifsta, reason);
4361 ieee80211_set_disassoc(dev, ifsta, 1);
4366 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4368 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4369 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4371 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4374 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4377 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4380 ieee80211_send_disassoc(dev, ifsta, reason);
4381 ieee80211_set_disassoc(dev, ifsta, 0);
4385 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4386 enum ieee80211_notification_types notif_type)
4388 struct ieee80211_local *local = hw_to_local(hw);
4389 struct ieee80211_sub_if_data *sdata;
4391 switch (notif_type) {
4392 case IEEE80211_NOTIFY_RE_ASSOC:
4394 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4396 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4397 ieee80211_sta_req_auth(sdata->dev,
4406 EXPORT_SYMBOL(ieee80211_notify_mac);