2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
19 #include <linux/delay.h>
20 #include <linux/if_ether.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/wireless.h>
25 #include <linux/random.h>
26 #include <linux/etherdevice.h>
27 #include <linux/rtnetlink.h>
28 #include <net/iw_handler.h>
29 #include <asm/types.h>
31 #include <net/mac80211.h>
32 #include "ieee80211_i.h"
37 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38 #define IEEE80211_AUTH_MAX_TRIES 3
39 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40 #define IEEE80211_ASSOC_MAX_TRIES 3
41 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
42 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
43 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
44 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
46 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
49 #define IEEE80211_PROBE_DELAY (HZ / 33)
50 #define IEEE80211_CHANNEL_TIME (HZ / 33)
51 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
55 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
57 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
60 #define ERP_INFO_USE_PROTECTION BIT(1)
62 /* mgmt header + 1 byte action code */
63 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
65 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
68 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
71 /* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73 #define IEEE80211_MIN_AMPDU_BUF 0x8
74 #define IEEE80211_MAX_AMPDU_BUF 0x40
76 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78 static struct ieee80211_sta_bss *
79 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
80 u8 *ssid, u8 ssid_len);
81 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
82 struct ieee80211_sta_bss *bss);
83 static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85 static int ieee80211_sta_wep_configured(struct net_device *dev);
86 static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88 static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
90 static void sta_rx_agg_session_timer_expired(unsigned long data);
93 void ieee802_11_parse_elems(u8 *start, size_t len,
94 struct ieee802_11_elems *elems)
99 memset(elems, 0, sizeof(*elems));
114 elems->ssid_len = elen;
116 case WLAN_EID_SUPP_RATES:
117 elems->supp_rates = pos;
118 elems->supp_rates_len = elen;
120 case WLAN_EID_FH_PARAMS:
121 elems->fh_params = pos;
122 elems->fh_params_len = elen;
124 case WLAN_EID_DS_PARAMS:
125 elems->ds_params = pos;
126 elems->ds_params_len = elen;
128 case WLAN_EID_CF_PARAMS:
129 elems->cf_params = pos;
130 elems->cf_params_len = elen;
134 elems->tim_len = elen;
136 case WLAN_EID_IBSS_PARAMS:
137 elems->ibss_params = pos;
138 elems->ibss_params_len = elen;
140 case WLAN_EID_CHALLENGE:
141 elems->challenge = pos;
142 elems->challenge_len = elen;
145 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
147 /* Microsoft OUI (00:50:F2) */
149 /* OUI Type 1 - WPA IE */
151 elems->wpa_len = elen;
152 } else if (elen >= 5 && pos[3] == 2) {
154 elems->wmm_info = pos;
155 elems->wmm_info_len = elen;
156 } else if (pos[4] == 1) {
157 elems->wmm_param = pos;
158 elems->wmm_param_len = elen;
165 elems->rsn_len = elen;
167 case WLAN_EID_ERP_INFO:
168 elems->erp_info = pos;
169 elems->erp_info_len = elen;
171 case WLAN_EID_EXT_SUPP_RATES:
172 elems->ext_supp_rates = pos;
173 elems->ext_supp_rates_len = elen;
175 case WLAN_EID_HT_CAPABILITY:
176 elems->ht_cap_elem = pos;
177 elems->ht_cap_elem_len = elen;
179 case WLAN_EID_HT_EXTRA_INFO:
180 elems->ht_info_elem = pos;
181 elems->ht_info_elem_len = elen;
183 case WLAN_EID_MESH_ID:
184 elems->mesh_id = pos;
185 elems->mesh_id_len = elen;
187 case WLAN_EID_MESH_CONFIG:
188 elems->mesh_config = pos;
189 elems->mesh_config_len = elen;
191 case WLAN_EID_PEER_LINK:
192 elems->peer_link = pos;
193 elems->peer_link_len = elen;
197 elems->preq_len = elen;
201 elems->prep_len = elen;
205 elems->perr_len = elen;
207 case WLAN_EID_CHANNEL_SWITCH:
208 elems->ch_switch_elem = pos;
209 elems->ch_switch_elem_len = elen;
212 if (!elems->quiet_elem) {
213 elems->quiet_elem = pos;
214 elems->quiet_elem_len = elen;
216 elems->num_of_quiet_elem++;
218 case WLAN_EID_COUNTRY:
219 elems->country_elem = pos;
220 elems->country_elem_len = elen;
222 case WLAN_EID_PWR_CONSTRAINT:
223 elems->pwr_constr_elem = pos;
224 elems->pwr_constr_elem_len = elen;
236 static int ecw2cw(int ecw)
238 return (1 << ecw) - 1;
242 static void ieee80211_sta_def_wmm_params(struct net_device *dev,
243 struct ieee80211_sta_bss *bss,
246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
247 struct ieee80211_local *local = sdata->local;
248 int i, have_higher_than_11mbit = 0;
251 /* cf. IEEE 802.11 9.2.12 */
252 for (i = 0; i < bss->supp_rates_len; i++)
253 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
254 have_higher_than_11mbit = 1;
256 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
257 have_higher_than_11mbit)
258 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
260 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
263 if (local->ops->conf_tx) {
264 struct ieee80211_tx_queue_params qparam;
266 memset(&qparam, 0, sizeof(qparam));
270 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
271 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
276 qparam.cw_max = 1023;
279 for (i = 0; i < local_to_hw(local)->queues; i++)
280 local->ops->conf_tx(local_to_hw(local), i, &qparam);
284 static void ieee80211_sta_wmm_params(struct net_device *dev,
285 struct ieee80211_if_sta *ifsta,
286 u8 *wmm_param, size_t wmm_param_len)
288 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
289 struct ieee80211_tx_queue_params params;
294 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
300 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
302 count = wmm_param[6] & 0x0f;
303 if (count == ifsta->wmm_last_param_set)
305 ifsta->wmm_last_param_set = count;
308 left = wmm_param_len - 8;
310 memset(¶ms, 0, sizeof(params));
312 if (!local->ops->conf_tx)
316 for (; left >= 4; left -= 4, pos += 4) {
317 int aci = (pos[0] >> 5) & 0x03;
318 int acm = (pos[0] >> 4) & 0x01;
325 local->wmm_acm |= BIT(0) | BIT(3);
330 local->wmm_acm |= BIT(4) | BIT(5);
335 local->wmm_acm |= BIT(6) | BIT(7);
341 local->wmm_acm |= BIT(1) | BIT(2);
345 params.aifs = pos[0] & 0x0f;
346 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
347 params.cw_min = ecw2cw(pos[1] & 0x0f);
348 params.txop = get_unaligned_le16(pos + 2);
349 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
350 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
351 "cWmin=%d cWmax=%d txop=%d\n",
352 dev->name, queue, aci, acm, params.aifs, params.cw_min,
353 params.cw_max, params.txop);
355 /* TODO: handle ACM (block TX, fallback to next lowest allowed
357 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
358 printk(KERN_DEBUG "%s: failed to set TX queue "
359 "parameters for queue %d\n", dev->name, queue);
364 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
366 bool use_short_preamble)
368 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
369 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
370 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
371 DECLARE_MAC_BUF(mac);
375 if (use_protection != bss_conf->use_cts_prot) {
376 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
377 if (net_ratelimit()) {
378 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
381 use_protection ? "enabled" : "disabled",
382 print_mac(mac, ifsta->bssid));
385 bss_conf->use_cts_prot = use_protection;
386 changed |= BSS_CHANGED_ERP_CTS_PROT;
389 if (use_short_preamble != bss_conf->use_short_preamble) {
390 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
391 if (net_ratelimit()) {
392 printk(KERN_DEBUG "%s: switched to %s barker preamble"
395 use_short_preamble ? "short" : "long",
396 print_mac(mac, ifsta->bssid));
399 bss_conf->use_short_preamble = use_short_preamble;
400 changed |= BSS_CHANGED_ERP_PREAMBLE;
406 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
409 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
410 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
412 return ieee80211_handle_protect_preamb(sdata,
413 use_protection, use_short_preamble);
416 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
417 struct ieee80211_sta_bss *bss)
421 if (bss->has_erp_value)
422 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
424 u16 capab = bss->capability;
425 changed |= ieee80211_handle_protect_preamb(sdata, false,
426 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
432 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
433 struct ieee80211_ht_info *ht_info)
439 memset(ht_info, 0, sizeof(*ht_info));
442 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
444 ht_info->ht_supported = 1;
445 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
446 ht_info->ampdu_factor =
447 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
448 ht_info->ampdu_density =
449 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
450 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
452 ht_info->ht_supported = 0;
457 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
458 struct ieee80211_ht_addt_info *ht_add_info_ie,
459 struct ieee80211_ht_bss_info *bss_info)
461 if (bss_info == NULL)
464 memset(bss_info, 0, sizeof(*bss_info));
466 if (ht_add_info_ie) {
468 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
470 bss_info->primary_channel = ht_add_info_ie->control_chan;
471 bss_info->bss_cap = ht_add_info_ie->ht_param;
472 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
478 static void ieee80211_sta_send_associnfo(struct net_device *dev,
479 struct ieee80211_if_sta *ifsta)
484 union iwreq_data wrqu;
486 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
489 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
490 ifsta->assocresp_ies_len), GFP_KERNEL);
494 len = sprintf(buf, "ASSOCINFO(");
495 if (ifsta->assocreq_ies) {
496 len += sprintf(buf + len, "ReqIEs=");
497 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
498 len += sprintf(buf + len, "%02x",
499 ifsta->assocreq_ies[i]);
502 if (ifsta->assocresp_ies) {
503 if (ifsta->assocreq_ies)
504 len += sprintf(buf + len, " ");
505 len += sprintf(buf + len, "RespIEs=");
506 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
507 len += sprintf(buf + len, "%02x",
508 ifsta->assocresp_ies[i]);
511 len += sprintf(buf + len, ")");
513 if (len > IW_CUSTOM_MAX) {
514 len = sprintf(buf, "ASSOCRESPIE=");
515 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
516 len += sprintf(buf + len, "%02x",
517 ifsta->assocresp_ies[i]);
521 memset(&wrqu, 0, sizeof(wrqu));
522 wrqu.data.length = len;
523 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
529 static void ieee80211_set_associated(struct net_device *dev,
530 struct ieee80211_if_sta *ifsta,
533 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
534 struct ieee80211_local *local = sdata->local;
535 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
536 union iwreq_data wrqu;
537 u32 changed = BSS_CHANGED_ASSOC;
540 struct ieee80211_sta_bss *bss;
542 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
544 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
547 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
548 conf->channel->center_freq,
549 ifsta->ssid, ifsta->ssid_len);
551 /* set timing information */
552 sdata->bss_conf.beacon_int = bss->beacon_int;
553 sdata->bss_conf.timestamp = bss->timestamp;
554 sdata->bss_conf.dtim_period = bss->dtim_period;
556 changed |= ieee80211_handle_bss_capability(sdata, bss);
558 ieee80211_rx_bss_put(local, bss);
561 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
562 changed |= BSS_CHANGED_HT;
563 sdata->bss_conf.assoc_ht = 1;
564 sdata->bss_conf.ht_conf = &conf->ht_conf;
565 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
568 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
569 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
570 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
571 ieee80211_sta_send_associnfo(dev, ifsta);
573 netif_carrier_off(dev);
574 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
575 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
576 changed |= ieee80211_reset_erp_info(dev);
578 sdata->bss_conf.assoc_ht = 0;
579 sdata->bss_conf.ht_conf = NULL;
580 sdata->bss_conf.ht_bss_conf = NULL;
582 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
584 ifsta->last_probe = jiffies;
585 ieee80211_led_assoc(local, assoc);
587 sdata->bss_conf.assoc = assoc;
588 ieee80211_bss_info_change_notify(sdata, changed);
591 netif_carrier_on(dev);
593 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
594 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
597 static void ieee80211_set_disassoc(struct net_device *dev,
598 struct ieee80211_if_sta *ifsta, int deauth)
601 ifsta->auth_tries = 0;
602 ifsta->assoc_tries = 0;
603 ieee80211_set_associated(dev, ifsta, 0);
606 void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
609 struct ieee80211_sub_if_data *sdata;
611 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
612 skb->dev = sdata->local->mdev;
613 skb_set_mac_header(skb, 0);
614 skb_set_network_header(skb, 0);
615 skb_set_transport_header(skb, 0);
617 skb->iif = sdata->dev->ifindex;
618 skb->do_not_encrypt = !encrypt;
624 static void ieee80211_send_auth(struct net_device *dev,
625 struct ieee80211_if_sta *ifsta,
626 int transaction, u8 *extra, size_t extra_len,
629 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
631 struct ieee80211_mgmt *mgmt;
633 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
634 sizeof(*mgmt) + 6 + extra_len);
636 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
637 "frame\n", dev->name);
640 skb_reserve(skb, local->hw.extra_tx_headroom);
642 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
643 memset(mgmt, 0, 24 + 6);
644 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
645 IEEE80211_STYPE_AUTH);
647 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
648 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
649 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
650 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
651 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
652 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
653 ifsta->auth_transaction = transaction + 1;
654 mgmt->u.auth.status_code = cpu_to_le16(0);
656 memcpy(skb_put(skb, extra_len), extra, extra_len);
658 ieee80211_sta_tx(dev, skb, encrypt);
662 static void ieee80211_authenticate(struct net_device *dev,
663 struct ieee80211_if_sta *ifsta)
665 DECLARE_MAC_BUF(mac);
668 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
669 printk(KERN_DEBUG "%s: authentication with AP %s"
671 dev->name, print_mac(mac, ifsta->bssid));
672 ifsta->state = IEEE80211_DISABLED;
676 ifsta->state = IEEE80211_AUTHENTICATE;
677 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
678 dev->name, print_mac(mac, ifsta->bssid));
680 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
682 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
685 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
686 struct ieee80211_supported_band *sband,
692 for (i = 0; i < bss->supp_rates_len; i++) {
693 int rate = (bss->supp_rates[i] & 0x7F) * 5;
695 for (j = 0; j < sband->n_bitrates; j++)
696 if (sband->bitrates[j].bitrate == rate) {
706 static void ieee80211_send_assoc(struct net_device *dev,
707 struct ieee80211_if_sta *ifsta)
709 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
711 struct ieee80211_mgmt *mgmt;
713 int i, len, count, rates_len, supp_rates_len;
715 struct ieee80211_sta_bss *bss;
717 struct ieee80211_supported_band *sband;
720 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
721 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
724 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
725 "frame\n", dev->name);
728 skb_reserve(skb, local->hw.extra_tx_headroom);
730 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
732 capab = ifsta->capab;
734 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
735 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
736 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
737 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
738 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
741 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
742 local->hw.conf.channel->center_freq,
743 ifsta->ssid, ifsta->ssid_len);
745 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
746 capab |= WLAN_CAPABILITY_PRIVACY;
750 /* get all rates supported by the device and the AP as
751 * some APs don't like getting a superset of their rates
752 * in the association request (e.g. D-Link DAP 1353 in
754 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
756 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
757 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
758 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
760 ieee80211_rx_bss_put(local, bss);
763 rates_len = sband->n_bitrates;
766 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
768 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
769 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
770 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
772 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
774 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
775 IEEE80211_STYPE_REASSOC_REQ);
776 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
777 mgmt->u.reassoc_req.listen_interval =
778 cpu_to_le16(local->hw.conf.listen_interval);
779 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
783 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
784 IEEE80211_STYPE_ASSOC_REQ);
785 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
786 mgmt->u.reassoc_req.listen_interval =
787 cpu_to_le16(local->hw.conf.listen_interval);
791 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
792 *pos++ = WLAN_EID_SSID;
793 *pos++ = ifsta->ssid_len;
794 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
796 /* add all rates which were marked to be used above */
797 supp_rates_len = rates_len;
798 if (supp_rates_len > 8)
801 len = sband->n_bitrates;
802 pos = skb_put(skb, supp_rates_len + 2);
803 *pos++ = WLAN_EID_SUPP_RATES;
804 *pos++ = supp_rates_len;
807 for (i = 0; i < sband->n_bitrates; i++) {
808 if (BIT(i) & rates) {
809 int rate = sband->bitrates[i].bitrate;
810 *pos++ = (u8) (rate / 5);
817 pos = skb_put(skb, rates_len - count + 2);
818 *pos++ = WLAN_EID_EXT_SUPP_RATES;
819 *pos++ = rates_len - count;
821 for (i++; i < sband->n_bitrates; i++) {
822 if (BIT(i) & rates) {
823 int rate = sband->bitrates[i].bitrate;
824 *pos++ = (u8) (rate / 5);
829 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
830 /* 1. power capabilities */
831 pos = skb_put(skb, 4);
832 *pos++ = WLAN_EID_PWR_CAPABILITY;
834 *pos++ = 0; /* min tx power */
835 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
837 /* 2. supported channels */
838 /* TODO: get this in reg domain format */
839 pos = skb_put(skb, 2 * sband->n_channels + 2);
840 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
841 *pos++ = 2 * sband->n_channels;
842 for (i = 0; i < sband->n_channels; i++) {
843 *pos++ = ieee80211_frequency_to_channel(
844 sband->channels[i].center_freq);
845 *pos++ = 1; /* one channel in the subband*/
849 if (ifsta->extra_ie) {
850 pos = skb_put(skb, ifsta->extra_ie_len);
851 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
854 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
855 pos = skb_put(skb, 9);
856 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
857 *pos++ = 7; /* len */
858 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
861 *pos++ = 2; /* WME */
862 *pos++ = 0; /* WME info */
863 *pos++ = 1; /* WME ver */
867 /* wmm support is a must to HT */
868 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
869 sband->ht_info.ht_supported && bss->ht_add_ie) {
870 struct ieee80211_ht_addt_info *ht_add_info =
871 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
872 u16 cap = sband->ht_info.cap;
874 u32 flags = local->hw.conf.channel->flags;
876 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
877 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
878 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
879 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
880 cap &= ~IEEE80211_HT_CAP_SGI_40;
883 case IEEE80211_HT_IE_CHA_SEC_BELOW:
884 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
885 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
886 cap &= ~IEEE80211_HT_CAP_SGI_40;
891 tmp = cpu_to_le16(cap);
892 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
893 *pos++ = WLAN_EID_HT_CAPABILITY;
894 *pos++ = sizeof(struct ieee80211_ht_cap);
895 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
896 memcpy(pos, &tmp, sizeof(u16));
898 /* TODO: needs a define here for << 2 */
899 *pos++ = sband->ht_info.ampdu_factor |
900 (sband->ht_info.ampdu_density << 2);
901 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
904 kfree(ifsta->assocreq_ies);
905 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
906 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
907 if (ifsta->assocreq_ies)
908 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
910 ieee80211_sta_tx(dev, skb, 0);
914 static void ieee80211_send_deauth(struct net_device *dev,
915 struct ieee80211_if_sta *ifsta, u16 reason)
917 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
919 struct ieee80211_mgmt *mgmt;
921 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
923 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
924 "frame\n", dev->name);
927 skb_reserve(skb, local->hw.extra_tx_headroom);
929 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
931 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
932 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
933 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
934 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
935 IEEE80211_STYPE_DEAUTH);
937 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
939 ieee80211_sta_tx(dev, skb, 0);
943 static void ieee80211_send_disassoc(struct net_device *dev,
944 struct ieee80211_if_sta *ifsta, u16 reason)
946 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
948 struct ieee80211_mgmt *mgmt;
950 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
952 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
953 "frame\n", dev->name);
956 skb_reserve(skb, local->hw.extra_tx_headroom);
958 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
960 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
961 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
962 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
963 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
964 IEEE80211_STYPE_DISASSOC);
966 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
968 ieee80211_sta_tx(dev, skb, 0);
972 static int ieee80211_privacy_mismatch(struct net_device *dev,
973 struct ieee80211_if_sta *ifsta)
975 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
976 struct ieee80211_sta_bss *bss;
981 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
984 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
985 local->hw.conf.channel->center_freq,
986 ifsta->ssid, ifsta->ssid_len);
990 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
991 wep_privacy = !!ieee80211_sta_wep_configured(dev);
992 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
994 ieee80211_rx_bss_put(local, bss);
996 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
1003 static void ieee80211_associate(struct net_device *dev,
1004 struct ieee80211_if_sta *ifsta)
1006 DECLARE_MAC_BUF(mac);
1008 ifsta->assoc_tries++;
1009 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
1010 printk(KERN_DEBUG "%s: association with AP %s"
1012 dev->name, print_mac(mac, ifsta->bssid));
1013 ifsta->state = IEEE80211_DISABLED;
1017 ifsta->state = IEEE80211_ASSOCIATE;
1018 printk(KERN_DEBUG "%s: associate with AP %s\n",
1019 dev->name, print_mac(mac, ifsta->bssid));
1020 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1021 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1022 "mixed-cell disabled - abort association\n", dev->name);
1023 ifsta->state = IEEE80211_DISABLED;
1027 ieee80211_send_assoc(dev, ifsta);
1029 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1033 static void ieee80211_associated(struct net_device *dev,
1034 struct ieee80211_if_sta *ifsta)
1036 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1037 struct sta_info *sta;
1039 DECLARE_MAC_BUF(mac);
1041 /* TODO: start monitoring current AP signal quality and number of
1042 * missed beacons. Scan other channels every now and then and search
1043 * for better APs. */
1044 /* TODO: remove expired BSSes */
1046 ifsta->state = IEEE80211_ASSOCIATED;
1050 sta = sta_info_get(local, ifsta->bssid);
1052 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1053 dev->name, print_mac(mac, ifsta->bssid));
1057 if (time_after(jiffies,
1058 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1059 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
1060 printk(KERN_DEBUG "%s: No ProbeResp from "
1061 "current AP %s - assume out of "
1063 dev->name, print_mac(mac, ifsta->bssid));
1065 sta_info_unlink(&sta);
1067 ieee80211_send_probe_req(dev, ifsta->bssid,
1069 local->scan_ssid_len);
1070 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1072 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1073 if (time_after(jiffies, ifsta->last_probe +
1074 IEEE80211_PROBE_INTERVAL)) {
1075 ifsta->last_probe = jiffies;
1076 ieee80211_send_probe_req(dev, ifsta->bssid,
1085 if (disassoc && sta)
1086 sta_info_destroy(sta);
1089 ifsta->state = IEEE80211_DISABLED;
1090 ieee80211_set_associated(dev, ifsta, 0);
1092 mod_timer(&ifsta->timer, jiffies +
1093 IEEE80211_MONITORING_INTERVAL);
1098 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1099 u8 *ssid, size_t ssid_len)
1101 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1102 struct ieee80211_supported_band *sband;
1103 struct sk_buff *skb;
1104 struct ieee80211_mgmt *mgmt;
1105 u8 *pos, *supp_rates, *esupp_rates = NULL;
1108 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1110 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1111 "request\n", dev->name);
1114 skb_reserve(skb, local->hw.extra_tx_headroom);
1116 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1117 memset(mgmt, 0, 24);
1118 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1119 IEEE80211_STYPE_PROBE_REQ);
1120 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1122 memcpy(mgmt->da, dst, ETH_ALEN);
1123 memcpy(mgmt->bssid, dst, ETH_ALEN);
1125 memset(mgmt->da, 0xff, ETH_ALEN);
1126 memset(mgmt->bssid, 0xff, ETH_ALEN);
1128 pos = skb_put(skb, 2 + ssid_len);
1129 *pos++ = WLAN_EID_SSID;
1131 memcpy(pos, ssid, ssid_len);
1133 supp_rates = skb_put(skb, 2);
1134 supp_rates[0] = WLAN_EID_SUPP_RATES;
1136 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1138 for (i = 0; i < sband->n_bitrates; i++) {
1139 struct ieee80211_rate *rate = &sband->bitrates[i];
1141 pos = skb_put(skb, 1);
1143 } else if (supp_rates[1] == 8) {
1144 esupp_rates = skb_put(skb, 3);
1145 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1147 pos = &esupp_rates[2];
1149 pos = skb_put(skb, 1);
1152 *pos = rate->bitrate / 5;
1155 ieee80211_sta_tx(dev, skb, 0);
1159 static int ieee80211_sta_wep_configured(struct net_device *dev)
1161 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1162 if (!sdata || !sdata->default_key ||
1163 sdata->default_key->conf.alg != ALG_WEP)
1169 static void ieee80211_auth_completed(struct net_device *dev,
1170 struct ieee80211_if_sta *ifsta)
1172 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
1173 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1174 ieee80211_associate(dev, ifsta);
1178 static void ieee80211_auth_challenge(struct net_device *dev,
1179 struct ieee80211_if_sta *ifsta,
1180 struct ieee80211_mgmt *mgmt,
1184 struct ieee802_11_elems elems;
1186 pos = mgmt->u.auth.variable;
1187 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1188 if (!elems.challenge)
1190 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1191 elems.challenge_len + 2, 1);
1194 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1195 u8 dialog_token, u16 status, u16 policy,
1196 u16 buf_size, u16 timeout)
1198 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1199 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1200 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1201 struct sk_buff *skb;
1202 struct ieee80211_mgmt *mgmt;
1205 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1208 printk(KERN_DEBUG "%s: failed to allocate buffer "
1209 "for addba resp frame\n", dev->name);
1213 skb_reserve(skb, local->hw.extra_tx_headroom);
1214 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1215 memset(mgmt, 0, 24);
1216 memcpy(mgmt->da, da, ETH_ALEN);
1217 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1218 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1219 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1221 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1222 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1223 IEEE80211_STYPE_ACTION);
1225 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1226 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1227 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1228 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1230 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1231 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1232 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1234 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1235 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1236 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1238 ieee80211_sta_tx(dev, skb, 0);
1243 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1244 u16 tid, u8 dialog_token, u16 start_seq_num,
1245 u16 agg_size, u16 timeout)
1247 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1248 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1249 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1250 struct sk_buff *skb;
1251 struct ieee80211_mgmt *mgmt;
1254 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1257 printk(KERN_ERR "%s: failed to allocate buffer "
1258 "for addba request frame\n", dev->name);
1261 skb_reserve(skb, local->hw.extra_tx_headroom);
1262 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1263 memset(mgmt, 0, 24);
1264 memcpy(mgmt->da, da, ETH_ALEN);
1265 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1266 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1267 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1269 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1271 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1272 IEEE80211_STYPE_ACTION);
1274 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1276 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1277 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1279 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1280 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1281 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1282 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1284 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1286 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1287 mgmt->u.action.u.addba_req.start_seq_num =
1288 cpu_to_le16(start_seq_num << 4);
1290 ieee80211_sta_tx(dev, skb, 0);
1293 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1294 struct ieee80211_mgmt *mgmt,
1297 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1298 struct ieee80211_hw *hw = &local->hw;
1299 struct ieee80211_conf *conf = &hw->conf;
1300 struct sta_info *sta;
1301 struct tid_ampdu_rx *tid_agg_rx;
1302 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1304 int ret = -EOPNOTSUPP;
1305 DECLARE_MAC_BUF(mac);
1309 sta = sta_info_get(local, mgmt->sa);
1315 /* extract session parameters from addba request frame */
1316 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1317 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1319 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1321 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1322 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1323 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1324 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1326 status = WLAN_STATUS_REQUEST_DECLINED;
1328 /* sanity check for incoming parameters:
1329 * check if configuration can support the BA policy
1330 * and if buffer size does not exceeds max value */
1331 if (((ba_policy != 1)
1332 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1333 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1334 status = WLAN_STATUS_INVALID_QOS_PARAM;
1335 #ifdef CONFIG_MAC80211_HT_DEBUG
1336 if (net_ratelimit())
1337 printk(KERN_DEBUG "AddBA Req with bad params from "
1338 "%s on tid %u. policy %d, buffer size %d\n",
1339 print_mac(mac, mgmt->sa), tid, ba_policy,
1341 #endif /* CONFIG_MAC80211_HT_DEBUG */
1344 /* determine default buffer size */
1345 if (buf_size == 0) {
1346 struct ieee80211_supported_band *sband;
1348 sband = local->hw.wiphy->bands[conf->channel->band];
1349 buf_size = IEEE80211_MIN_AMPDU_BUF;
1350 buf_size = buf_size << sband->ht_info.ampdu_factor;
1354 /* examine state machine */
1355 spin_lock_bh(&sta->lock);
1357 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1358 #ifdef CONFIG_MAC80211_HT_DEBUG
1359 if (net_ratelimit())
1360 printk(KERN_DEBUG "unexpected AddBA Req from "
1362 print_mac(mac, mgmt->sa), tid);
1363 #endif /* CONFIG_MAC80211_HT_DEBUG */
1367 /* prepare A-MPDU MLME for Rx aggregation */
1368 sta->ampdu_mlme.tid_rx[tid] =
1369 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1370 if (!sta->ampdu_mlme.tid_rx[tid]) {
1371 #ifdef CONFIG_MAC80211_HT_DEBUG
1372 if (net_ratelimit())
1373 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1379 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1380 sta_rx_agg_session_timer_expired;
1381 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1382 (unsigned long)&sta->timer_to_tid[tid];
1383 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1385 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1387 /* prepare reordering buffer */
1388 tid_agg_rx->reorder_buf =
1389 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1390 if (!tid_agg_rx->reorder_buf) {
1391 #ifdef CONFIG_MAC80211_HT_DEBUG
1392 if (net_ratelimit())
1393 printk(KERN_ERR "can not allocate reordering buffer "
1394 "to tid %d\n", tid);
1396 kfree(sta->ampdu_mlme.tid_rx[tid]);
1399 memset(tid_agg_rx->reorder_buf, 0,
1400 buf_size * sizeof(struct sk_buff *));
1402 if (local->ops->ampdu_action)
1403 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1404 sta->addr, tid, &start_seq_num);
1405 #ifdef CONFIG_MAC80211_HT_DEBUG
1406 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1407 #endif /* CONFIG_MAC80211_HT_DEBUG */
1410 kfree(tid_agg_rx->reorder_buf);
1412 sta->ampdu_mlme.tid_rx[tid] = NULL;
1416 /* change state and send addba resp */
1417 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1418 tid_agg_rx->dialog_token = dialog_token;
1419 tid_agg_rx->ssn = start_seq_num;
1420 tid_agg_rx->head_seq_num = start_seq_num;
1421 tid_agg_rx->buf_size = buf_size;
1422 tid_agg_rx->timeout = timeout;
1423 tid_agg_rx->stored_mpdu_num = 0;
1424 status = WLAN_STATUS_SUCCESS;
1426 spin_unlock_bh(&sta->lock);
1429 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1430 dialog_token, status, 1, buf_size, timeout);
1434 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1435 struct ieee80211_mgmt *mgmt,
1438 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1439 struct ieee80211_hw *hw = &local->hw;
1440 struct sta_info *sta;
1447 sta = sta_info_get(local, mgmt->sa);
1453 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1454 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1456 state = &sta->ampdu_mlme.tid_state_tx[tid];
1458 spin_lock_bh(&sta->lock);
1460 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1461 spin_unlock_bh(&sta->lock);
1462 goto addba_resp_exit;
1465 if (mgmt->u.action.u.addba_resp.dialog_token !=
1466 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1467 spin_unlock_bh(&sta->lock);
1468 #ifdef CONFIG_MAC80211_HT_DEBUG
1469 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1470 #endif /* CONFIG_MAC80211_HT_DEBUG */
1471 goto addba_resp_exit;
1474 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1475 #ifdef CONFIG_MAC80211_HT_DEBUG
1476 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1477 #endif /* CONFIG_MAC80211_HT_DEBUG */
1478 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1479 == WLAN_STATUS_SUCCESS) {
1480 *state |= HT_ADDBA_RECEIVED_MSK;
1481 sta->ampdu_mlme.addba_req_num[tid] = 0;
1483 if (*state == HT_AGG_STATE_OPERATIONAL)
1484 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1486 spin_unlock_bh(&sta->lock);
1488 sta->ampdu_mlme.addba_req_num[tid]++;
1489 /* this will allow the state check in stop_BA_session */
1490 *state = HT_AGG_STATE_OPERATIONAL;
1491 spin_unlock_bh(&sta->lock);
1492 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1493 WLAN_BACK_INITIATOR);
1500 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1501 u16 initiator, u16 reason_code)
1503 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1504 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1505 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1506 struct sk_buff *skb;
1507 struct ieee80211_mgmt *mgmt;
1510 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1513 printk(KERN_ERR "%s: failed to allocate buffer "
1514 "for delba frame\n", dev->name);
1518 skb_reserve(skb, local->hw.extra_tx_headroom);
1519 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1520 memset(mgmt, 0, 24);
1521 memcpy(mgmt->da, da, ETH_ALEN);
1522 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1523 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1524 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1526 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1527 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1528 IEEE80211_STYPE_ACTION);
1530 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1532 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1533 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1534 params = (u16)(initiator << 11); /* bit 11 initiator */
1535 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1537 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1538 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1540 ieee80211_sta_tx(dev, skb, 0);
1543 void ieee80211_send_bar(struct net_device *dev, u8 *ra, u16 tid, u16 ssn)
1545 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1546 struct sk_buff *skb;
1547 struct ieee80211_bar *bar;
1548 u16 bar_control = 0;
1550 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1552 printk(KERN_ERR "%s: failed to allocate buffer for "
1553 "bar frame\n", dev->name);
1556 skb_reserve(skb, local->hw.extra_tx_headroom);
1557 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1558 memset(bar, 0, sizeof(*bar));
1559 bar->frame_control = IEEE80211_FC(IEEE80211_FTYPE_CTL,
1560 IEEE80211_STYPE_BACK_REQ);
1561 memcpy(bar->ra, ra, ETH_ALEN);
1562 memcpy(bar->ta, dev->dev_addr, ETH_ALEN);
1563 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1564 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1565 bar_control |= (u16)(tid << 12);
1566 bar->control = cpu_to_le16(bar_control);
1567 bar->start_seq_num = cpu_to_le16(ssn);
1569 ieee80211_sta_tx(dev, skb, 0);
1572 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1573 u16 initiator, u16 reason)
1575 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1576 struct ieee80211_hw *hw = &local->hw;
1577 struct sta_info *sta;
1579 DECLARE_MAC_BUF(mac);
1583 sta = sta_info_get(local, ra);
1589 /* check if TID is in operational state */
1590 spin_lock_bh(&sta->lock);
1591 if (sta->ampdu_mlme.tid_state_rx[tid]
1592 != HT_AGG_STATE_OPERATIONAL) {
1593 spin_unlock_bh(&sta->lock);
1597 sta->ampdu_mlme.tid_state_rx[tid] =
1598 HT_AGG_STATE_REQ_STOP_BA_MSK |
1599 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1600 spin_unlock_bh(&sta->lock);
1602 /* stop HW Rx aggregation. ampdu_action existence
1603 * already verified in session init so we add the BUG_ON */
1604 BUG_ON(!local->ops->ampdu_action);
1606 #ifdef CONFIG_MAC80211_HT_DEBUG
1607 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1608 print_mac(mac, ra), tid);
1609 #endif /* CONFIG_MAC80211_HT_DEBUG */
1611 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1614 printk(KERN_DEBUG "HW problem - can not stop rx "
1615 "aggregation for tid %d\n", tid);
1617 /* shutdown timer has not expired */
1618 if (initiator != WLAN_BACK_TIMER)
1619 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1621 /* check if this is a self generated aggregation halt */
1622 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1623 ieee80211_send_delba(dev, ra, tid, 0, reason);
1625 /* free the reordering buffer */
1626 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1627 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1628 /* release the reordered frames */
1629 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1630 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1631 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1634 /* free resources */
1635 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1636 kfree(sta->ampdu_mlme.tid_rx[tid]);
1637 sta->ampdu_mlme.tid_rx[tid] = NULL;
1638 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1644 static void ieee80211_sta_process_delba(struct net_device *dev,
1645 struct ieee80211_mgmt *mgmt, size_t len)
1647 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1648 struct sta_info *sta;
1651 DECLARE_MAC_BUF(mac);
1655 sta = sta_info_get(local, mgmt->sa);
1661 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1662 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1663 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1665 #ifdef CONFIG_MAC80211_HT_DEBUG
1666 if (net_ratelimit())
1667 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1668 print_mac(mac, mgmt->sa),
1669 initiator ? "initiator" : "recipient", tid,
1670 mgmt->u.action.u.delba.reason_code);
1671 #endif /* CONFIG_MAC80211_HT_DEBUG */
1673 if (initiator == WLAN_BACK_INITIATOR)
1674 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1675 WLAN_BACK_INITIATOR, 0);
1676 else { /* WLAN_BACK_RECIPIENT */
1677 spin_lock_bh(&sta->lock);
1678 sta->ampdu_mlme.tid_state_tx[tid] =
1679 HT_AGG_STATE_OPERATIONAL;
1680 spin_unlock_bh(&sta->lock);
1681 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1682 WLAN_BACK_RECIPIENT);
1688 * After sending add Block Ack request we activated a timer until
1689 * add Block Ack response will arrive from the recipient.
1690 * If this timer expires sta_addba_resp_timer_expired will be executed.
1692 void sta_addba_resp_timer_expired(unsigned long data)
1694 /* not an elegant detour, but there is no choice as the timer passes
1695 * only one argument, and both sta_info and TID are needed, so init
1696 * flow in sta_info_create gives the TID as data, while the timer_to_id
1697 * array gives the sta through container_of */
1698 u16 tid = *(u8 *)data;
1699 struct sta_info *temp_sta = container_of((void *)data,
1700 struct sta_info, timer_to_tid[tid]);
1702 struct ieee80211_local *local = temp_sta->local;
1703 struct ieee80211_hw *hw = &local->hw;
1704 struct sta_info *sta;
1709 sta = sta_info_get(local, temp_sta->addr);
1715 state = &sta->ampdu_mlme.tid_state_tx[tid];
1716 /* check if the TID waits for addBA response */
1717 spin_lock_bh(&sta->lock);
1718 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1719 spin_unlock_bh(&sta->lock);
1720 *state = HT_AGG_STATE_IDLE;
1721 #ifdef CONFIG_MAC80211_HT_DEBUG
1722 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1723 "expecting addBA response there", tid);
1725 goto timer_expired_exit;
1728 #ifdef CONFIG_MAC80211_HT_DEBUG
1729 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1732 /* go through the state check in stop_BA_session */
1733 *state = HT_AGG_STATE_OPERATIONAL;
1734 spin_unlock_bh(&sta->lock);
1735 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1736 WLAN_BACK_INITIATOR);
1743 * After accepting the AddBA Request we activated a timer,
1744 * resetting it after each frame that arrives from the originator.
1745 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1747 static void sta_rx_agg_session_timer_expired(unsigned long data)
1749 /* not an elegant detour, but there is no choice as the timer passes
1750 * only one argument, and various sta_info are needed here, so init
1751 * flow in sta_info_create gives the TID as data, while the timer_to_id
1752 * array gives the sta through container_of */
1753 u8 *ptid = (u8 *)data;
1754 u8 *timer_to_id = ptid - *ptid;
1755 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1758 #ifdef CONFIG_MAC80211_HT_DEBUG
1759 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1761 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1762 (u16)*ptid, WLAN_BACK_TIMER,
1763 WLAN_REASON_QSTA_TIMEOUT);
1766 void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1768 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1771 for (i = 0; i < STA_TID_NUM; i++) {
1772 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1773 WLAN_BACK_INITIATOR);
1774 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1775 WLAN_BACK_RECIPIENT,
1776 WLAN_REASON_QSTA_LEAVE_QBSS);
1780 static void ieee80211_send_refuse_measurement_request(struct net_device *dev,
1781 struct ieee80211_msrment_ie *request_ie,
1782 const u8 *da, const u8 *bssid,
1785 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1786 struct sk_buff *skb;
1787 struct ieee80211_mgmt *msr_report;
1789 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1790 sizeof(struct ieee80211_msrment_ie));
1793 printk(KERN_ERR "%s: failed to allocate buffer for "
1794 "measurement report frame\n", dev->name);
1798 skb_reserve(skb, local->hw.extra_tx_headroom);
1799 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1800 memset(msr_report, 0, 24);
1801 memcpy(msr_report->da, da, ETH_ALEN);
1802 memcpy(msr_report->sa, dev->dev_addr, ETH_ALEN);
1803 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1804 msr_report->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1805 IEEE80211_STYPE_ACTION);
1807 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1808 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1809 msr_report->u.action.u.measurement.action_code =
1810 WLAN_ACTION_SPCT_MSR_RPRT;
1811 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1813 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1814 msr_report->u.action.u.measurement.length =
1815 sizeof(struct ieee80211_msrment_ie);
1817 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1818 sizeof(struct ieee80211_msrment_ie));
1819 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1820 msr_report->u.action.u.measurement.msr_elem.mode |=
1821 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1822 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1824 ieee80211_sta_tx(dev, skb, 0);
1827 static void ieee80211_sta_process_measurement_req(struct net_device *dev,
1828 struct ieee80211_mgmt *mgmt,
1832 * Ignoring measurement request is spec violation.
1833 * Mandatory measurements must be reported optional
1834 * measurements might be refused or reported incapable
1835 * For now just refuse
1836 * TODO: Answer basic measurement as unmeasured
1838 ieee80211_send_refuse_measurement_request(dev,
1839 &mgmt->u.action.u.measurement.msr_elem,
1840 mgmt->sa, mgmt->bssid,
1841 mgmt->u.action.u.measurement.dialog_token);
1845 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1846 struct ieee80211_if_sta *ifsta,
1847 struct ieee80211_mgmt *mgmt,
1850 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1851 u16 auth_alg, auth_transaction, status_code;
1852 DECLARE_MAC_BUF(mac);
1854 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1855 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
1861 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1862 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1865 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1866 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1869 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1870 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1871 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1873 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1875 * IEEE 802.11 standard does not require authentication in IBSS
1876 * networks and most implementations do not seem to use it.
1877 * However, try to reply to authentication attempts if someone
1878 * has actually implemented this.
1880 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1882 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1885 if (auth_alg != ifsta->auth_alg ||
1886 auth_transaction != ifsta->auth_transaction)
1889 if (status_code != WLAN_STATUS_SUCCESS) {
1890 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1892 const int num_algs = ARRAY_SIZE(algs);
1894 algs[0] = algs[1] = algs[2] = 0xff;
1895 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1896 algs[0] = WLAN_AUTH_OPEN;
1897 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1898 algs[1] = WLAN_AUTH_SHARED_KEY;
1899 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1900 algs[2] = WLAN_AUTH_LEAP;
1901 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1903 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1907 for (i = 0; i < num_algs; i++) {
1909 if (pos >= num_algs)
1911 if (algs[pos] == ifsta->auth_alg ||
1914 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1915 !ieee80211_sta_wep_configured(dev))
1917 ifsta->auth_alg = algs[pos];
1924 switch (ifsta->auth_alg) {
1925 case WLAN_AUTH_OPEN:
1926 case WLAN_AUTH_LEAP:
1927 ieee80211_auth_completed(dev, ifsta);
1929 case WLAN_AUTH_SHARED_KEY:
1930 if (ifsta->auth_transaction == 4)
1931 ieee80211_auth_completed(dev, ifsta);
1933 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1939 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1940 struct ieee80211_if_sta *ifsta,
1941 struct ieee80211_mgmt *mgmt,
1945 DECLARE_MAC_BUF(mac);
1950 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1953 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1955 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1956 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1958 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1959 ifsta->state == IEEE80211_ASSOCIATE ||
1960 ifsta->state == IEEE80211_ASSOCIATED) {
1961 ifsta->state = IEEE80211_AUTHENTICATE;
1962 mod_timer(&ifsta->timer, jiffies +
1963 IEEE80211_RETRY_AUTH_INTERVAL);
1966 ieee80211_set_disassoc(dev, ifsta, 1);
1967 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1971 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1972 struct ieee80211_if_sta *ifsta,
1973 struct ieee80211_mgmt *mgmt,
1977 DECLARE_MAC_BUF(mac);
1982 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1985 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1987 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1988 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1990 if (ifsta->state == IEEE80211_ASSOCIATED) {
1991 ifsta->state = IEEE80211_ASSOCIATE;
1992 mod_timer(&ifsta->timer, jiffies +
1993 IEEE80211_RETRY_AUTH_INTERVAL);
1996 ieee80211_set_disassoc(dev, ifsta, 0);
2000 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2001 struct ieee80211_if_sta *ifsta,
2002 struct ieee80211_mgmt *mgmt,
2006 struct ieee80211_local *local = sdata->local;
2007 struct net_device *dev = sdata->dev;
2008 struct ieee80211_supported_band *sband;
2009 struct sta_info *sta;
2010 u64 rates, basic_rates;
2011 u16 capab_info, status_code, aid;
2012 struct ieee802_11_elems elems;
2013 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
2016 DECLARE_MAC_BUF(mac);
2017 bool have_higher_than_11mbit = false;
2019 /* AssocResp and ReassocResp have identical structure, so process both
2020 * of them in this function. */
2022 if (ifsta->state != IEEE80211_ASSOCIATE)
2028 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
2031 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2032 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2033 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2035 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
2036 "status=%d aid=%d)\n",
2037 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
2038 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2040 if (status_code != WLAN_STATUS_SUCCESS) {
2041 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2042 dev->name, status_code);
2043 /* if this was a reassociation, ensure we try a "full"
2044 * association next time. This works around some broken APs
2045 * which do not correctly reject reassociation requests. */
2046 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2050 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2051 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2052 "set\n", dev->name, aid);
2053 aid &= ~(BIT(15) | BIT(14));
2055 pos = mgmt->u.assoc_resp.variable;
2056 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2058 if (!elems.supp_rates) {
2059 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2064 printk(KERN_DEBUG "%s: associated\n", dev->name);
2066 ifsta->ap_capab = capab_info;
2068 kfree(ifsta->assocresp_ies);
2069 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
2070 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
2071 if (ifsta->assocresp_ies)
2072 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2076 /* Add STA entry for the AP */
2077 sta = sta_info_get(local, ifsta->bssid);
2079 struct ieee80211_sta_bss *bss;
2082 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2084 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2085 " the AP\n", dev->name);
2089 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
2090 local->hw.conf.channel->center_freq,
2091 ifsta->ssid, ifsta->ssid_len);
2093 sta->last_signal = bss->signal;
2094 sta->last_qual = bss->qual;
2095 sta->last_noise = bss->noise;
2096 ieee80211_rx_bss_put(local, bss);
2099 err = sta_info_insert(sta);
2101 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2102 " the AP (error %d)\n", dev->name, err);
2109 * FIXME: Do we really need to update the sta_info's information here?
2110 * We already know about the AP (we found it in our list) so it
2111 * should already be filled with the right info, no?
2112 * As is stands, all this is racy because typically we assume
2113 * the information that is filled in here (except flags) doesn't
2114 * change while a STA structure is alive. As such, it should move
2115 * to between the sta_info_alloc() and sta_info_insert() above.
2118 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2119 WLAN_STA_AUTHORIZED);
2123 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2125 for (i = 0; i < elems.supp_rates_len; i++) {
2126 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2129 have_higher_than_11mbit = true;
2131 for (j = 0; j < sband->n_bitrates; j++) {
2132 if (sband->bitrates[j].bitrate == rate)
2134 if (elems.supp_rates[i] & 0x80)
2135 basic_rates |= BIT(j);
2139 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2140 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2143 have_higher_than_11mbit = true;
2145 for (j = 0; j < sband->n_bitrates; j++) {
2146 if (sband->bitrates[j].bitrate == rate)
2148 if (elems.ext_supp_rates[i] & 0x80)
2149 basic_rates |= BIT(j);
2153 sta->supp_rates[local->hw.conf.channel->band] = rates;
2154 sdata->basic_rates = basic_rates;
2156 /* cf. IEEE 802.11 9.2.12 */
2157 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2158 have_higher_than_11mbit)
2159 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2161 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2163 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2164 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2165 struct ieee80211_ht_bss_info bss_info;
2166 ieee80211_ht_cap_ie_to_ht_info(
2167 (struct ieee80211_ht_cap *)
2168 elems.ht_cap_elem, &sta->ht_info);
2169 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2170 (struct ieee80211_ht_addt_info *)
2171 elems.ht_info_elem, &bss_info);
2172 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2175 rate_control_rate_init(sta, local);
2177 if (elems.wmm_param) {
2178 set_sta_flags(sta, WLAN_STA_WME);
2180 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2181 elems.wmm_param_len);
2185 /* set AID and assoc capability,
2186 * ieee80211_set_associated() will tell the driver */
2187 bss_conf->aid = aid;
2188 bss_conf->assoc_capability = capab_info;
2189 ieee80211_set_associated(dev, ifsta, 1);
2191 ieee80211_associated(dev, ifsta);
2195 /* Caller must hold local->sta_bss_lock */
2196 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2197 struct ieee80211_sta_bss *bss)
2199 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2202 if (bss_mesh_cfg(bss))
2203 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2204 bss_mesh_id_len(bss));
2206 hash_idx = STA_HASH(bss->bssid);
2208 bss->hnext = local->sta_bss_hash[hash_idx];
2209 local->sta_bss_hash[hash_idx] = bss;
2213 /* Caller must hold local->sta_bss_lock */
2214 static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
2215 struct ieee80211_sta_bss *bss)
2217 struct ieee80211_sta_bss *b, *prev = NULL;
2218 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2222 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2225 prev->hnext = bss->hnext;
2234 static struct ieee80211_sta_bss *
2235 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
2236 u8 *ssid, u8 ssid_len)
2238 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2239 struct ieee80211_sta_bss *bss;
2241 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2244 atomic_inc(&bss->users);
2245 atomic_inc(&bss->users);
2246 memcpy(bss->bssid, bssid, ETH_ALEN);
2248 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2249 memcpy(bss->ssid, ssid, ssid_len);
2250 bss->ssid_len = ssid_len;
2253 spin_lock_bh(&local->sta_bss_lock);
2254 /* TODO: order by RSSI? */
2255 list_add_tail(&bss->list, &local->sta_bss_list);
2256 __ieee80211_rx_bss_hash_add(dev, bss);
2257 spin_unlock_bh(&local->sta_bss_lock);
2261 static struct ieee80211_sta_bss *
2262 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
2263 u8 *ssid, u8 ssid_len)
2265 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2266 struct ieee80211_sta_bss *bss;
2268 spin_lock_bh(&local->sta_bss_lock);
2269 bss = local->sta_bss_hash[STA_HASH(bssid)];
2271 if (!bss_mesh_cfg(bss) &&
2272 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2273 bss->freq == freq &&
2274 bss->ssid_len == ssid_len &&
2275 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2276 atomic_inc(&bss->users);
2281 spin_unlock_bh(&local->sta_bss_lock);
2285 #ifdef CONFIG_MAC80211_MESH
2286 static struct ieee80211_sta_bss *
2287 ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2288 u8 *mesh_cfg, int freq)
2290 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2291 struct ieee80211_sta_bss *bss;
2293 spin_lock_bh(&local->sta_bss_lock);
2294 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2296 if (bss_mesh_cfg(bss) &&
2297 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2298 bss->freq == freq &&
2299 mesh_id_len == bss->mesh_id_len &&
2300 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2302 atomic_inc(&bss->users);
2307 spin_unlock_bh(&local->sta_bss_lock);
2311 static struct ieee80211_sta_bss *
2312 ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2313 u8 *mesh_cfg, int mesh_config_len, int freq)
2315 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2316 struct ieee80211_sta_bss *bss;
2318 if (mesh_config_len != MESH_CFG_LEN)
2321 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2325 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2326 if (!bss->mesh_cfg) {
2331 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2332 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2333 if (!bss->mesh_id) {
2334 kfree(bss->mesh_cfg);
2338 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2341 atomic_inc(&bss->users);
2342 atomic_inc(&bss->users);
2343 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2344 bss->mesh_id_len = mesh_id_len;
2346 spin_lock_bh(&local->sta_bss_lock);
2347 /* TODO: order by RSSI? */
2348 list_add_tail(&bss->list, &local->sta_bss_list);
2349 __ieee80211_rx_bss_hash_add(dev, bss);
2350 spin_unlock_bh(&local->sta_bss_lock);
2355 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2361 kfree(bss->ht_add_ie);
2362 kfree(bss_mesh_id(bss));
2363 kfree(bss_mesh_cfg(bss));
2368 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
2369 struct ieee80211_sta_bss *bss)
2372 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2377 __ieee80211_rx_bss_hash_del(local, bss);
2378 list_del(&bss->list);
2379 spin_unlock_bh(&local->sta_bss_lock);
2380 ieee80211_rx_bss_free(bss);
2384 void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
2386 spin_lock_init(&local->sta_bss_lock);
2387 INIT_LIST_HEAD(&local->sta_bss_list);
2391 void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
2393 struct ieee80211_sta_bss *bss, *tmp;
2395 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2396 ieee80211_rx_bss_put(local, bss);
2400 static int ieee80211_sta_join_ibss(struct net_device *dev,
2401 struct ieee80211_if_sta *ifsta,
2402 struct ieee80211_sta_bss *bss)
2404 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2405 int res, rates, i, j;
2406 struct sk_buff *skb;
2407 struct ieee80211_mgmt *mgmt;
2409 struct ieee80211_sub_if_data *sdata;
2410 struct ieee80211_supported_band *sband;
2411 union iwreq_data wrqu;
2413 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2415 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2417 /* Remove possible STA entries from other IBSS networks. */
2418 sta_info_flush_delayed(sdata);
2420 if (local->ops->reset_tsf) {
2421 /* Reset own TSF to allow time synchronization work. */
2422 local->ops->reset_tsf(local_to_hw(local));
2424 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2425 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2429 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2431 sdata->drop_unencrypted = bss->capability &
2432 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2434 res = ieee80211_set_freq(dev, bss->freq);
2439 /* Build IBSS probe response */
2440 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2442 skb_reserve(skb, local->hw.extra_tx_headroom);
2444 mgmt = (struct ieee80211_mgmt *)
2445 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2446 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2447 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2448 IEEE80211_STYPE_PROBE_RESP);
2449 memset(mgmt->da, 0xff, ETH_ALEN);
2450 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2451 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2452 mgmt->u.beacon.beacon_int =
2453 cpu_to_le16(local->hw.conf.beacon_int);
2454 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
2455 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2457 pos = skb_put(skb, 2 + ifsta->ssid_len);
2458 *pos++ = WLAN_EID_SSID;
2459 *pos++ = ifsta->ssid_len;
2460 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2462 rates = bss->supp_rates_len;
2465 pos = skb_put(skb, 2 + rates);
2466 *pos++ = WLAN_EID_SUPP_RATES;
2468 memcpy(pos, bss->supp_rates, rates);
2470 if (bss->band == IEEE80211_BAND_2GHZ) {
2471 pos = skb_put(skb, 2 + 1);
2472 *pos++ = WLAN_EID_DS_PARAMS;
2474 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2477 pos = skb_put(skb, 2 + 2);
2478 *pos++ = WLAN_EID_IBSS_PARAMS;
2480 /* FIX: set ATIM window based on scan results */
2484 if (bss->supp_rates_len > 8) {
2485 rates = bss->supp_rates_len - 8;
2486 pos = skb_put(skb, 2 + rates);
2487 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2489 memcpy(pos, &bss->supp_rates[8], rates);
2492 ifsta->probe_resp = skb;
2494 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2498 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2499 for (i = 0; i < bss->supp_rates_len; i++) {
2500 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2501 for (j = 0; j < sband->n_bitrates; j++)
2502 if (sband->bitrates[j].bitrate == bitrate)
2505 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2507 ieee80211_sta_def_wmm_params(dev, bss, 1);
2509 ifsta->state = IEEE80211_IBSS_JOINED;
2510 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2512 memset(&wrqu, 0, sizeof(wrqu));
2513 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2514 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
2519 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2520 struct ieee802_11_elems *elems,
2521 enum ieee80211_band band)
2523 struct ieee80211_supported_band *sband;
2524 struct ieee80211_rate *bitrates;
2528 sband = local->hw.wiphy->bands[band];
2532 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2535 bitrates = sband->bitrates;
2536 num_rates = sband->n_bitrates;
2538 for (i = 0; i < elems->supp_rates_len +
2539 elems->ext_supp_rates_len; i++) {
2542 if (i < elems->supp_rates_len)
2543 rate = elems->supp_rates[i];
2544 else if (elems->ext_supp_rates)
2545 rate = elems->ext_supp_rates
2546 [i - elems->supp_rates_len];
2547 own_rate = 5 * (rate & 0x7f);
2548 for (j = 0; j < num_rates; j++)
2549 if (bitrates[j].bitrate == own_rate)
2550 supp_rates |= BIT(j);
2556 static void ieee80211_rx_bss_info(struct net_device *dev,
2557 struct ieee80211_mgmt *mgmt,
2559 struct ieee80211_rx_status *rx_status,
2560 struct ieee802_11_elems *elems,
2563 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2565 struct ieee80211_sta_bss *bss;
2566 struct sta_info *sta;
2567 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2568 u64 beacon_timestamp, rx_timestamp;
2569 struct ieee80211_channel *channel;
2570 DECLARE_MAC_BUF(mac);
2571 DECLARE_MAC_BUF(mac2);
2573 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2574 return; /* ignore ProbeResp to foreign address */
2576 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2578 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2579 elems->mesh_config && mesh_matches_local(elems, dev)) {
2580 u64 rates = ieee80211_sta_get_rates(local, elems,
2583 mesh_neighbour_update(mgmt->sa, rates, dev,
2584 mesh_peer_accepts_plinks(elems, dev));
2589 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
2590 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2591 (sta = sta_info_get(local, mgmt->sa))) {
2593 u64 supp_rates = ieee80211_sta_get_rates(local, elems,
2596 prev_rates = sta->supp_rates[rx_status->band];
2597 sta->supp_rates[rx_status->band] &= supp_rates;
2598 if (sta->supp_rates[rx_status->band] == 0) {
2599 /* No matching rates - this should not really happen.
2600 * Make sure that at least one rate is marked
2601 * supported to avoid issues with TX rate ctrl. */
2602 sta->supp_rates[rx_status->band] =
2603 sdata->u.sta.supp_rates_bits[rx_status->band];
2609 if (elems->ds_params && elems->ds_params_len == 1)
2610 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
2612 freq = rx_status->freq;
2614 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2616 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2619 #ifdef CONFIG_MAC80211_MESH
2620 if (elems->mesh_config)
2621 bss = ieee80211_rx_mesh_bss_get(dev, elems->mesh_id,
2622 elems->mesh_id_len, elems->mesh_config, freq);
2625 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2626 elems->ssid, elems->ssid_len);
2628 #ifdef CONFIG_MAC80211_MESH
2629 if (elems->mesh_config)
2630 bss = ieee80211_rx_mesh_bss_add(dev, elems->mesh_id,
2631 elems->mesh_id_len, elems->mesh_config,
2632 elems->mesh_config_len, freq);
2635 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2636 elems->ssid, elems->ssid_len);
2641 /* TODO: order by RSSI? */
2642 spin_lock_bh(&local->sta_bss_lock);
2643 list_move_tail(&bss->list, &local->sta_bss_list);
2644 spin_unlock_bh(&local->sta_bss_lock);
2648 /* save the ERP value so that it is available at association time */
2649 if (elems->erp_info && elems->erp_info_len >= 1) {
2650 bss->erp_value = elems->erp_info[0];
2651 bss->has_erp_value = 1;
2654 if (elems->ht_cap_elem &&
2655 (!bss->ht_ie || bss->ht_ie_len != elems->ht_cap_elem_len ||
2656 memcmp(bss->ht_ie, elems->ht_cap_elem, elems->ht_cap_elem_len))) {
2658 bss->ht_ie = kmalloc(elems->ht_cap_elem_len + 2, GFP_ATOMIC);
2660 memcpy(bss->ht_ie, elems->ht_cap_elem - 2,
2661 elems->ht_cap_elem_len + 2);
2662 bss->ht_ie_len = elems->ht_cap_elem_len + 2;
2665 } else if (!elems->ht_cap_elem && bss->ht_ie) {
2671 if (elems->ht_info_elem &&
2673 bss->ht_add_ie_len != elems->ht_info_elem_len ||
2674 memcmp(bss->ht_add_ie, elems->ht_info_elem,
2675 elems->ht_info_elem_len))) {
2676 kfree(bss->ht_add_ie);
2678 kmalloc(elems->ht_info_elem_len + 2, GFP_ATOMIC);
2679 if (bss->ht_add_ie) {
2680 memcpy(bss->ht_add_ie, elems->ht_info_elem - 2,
2681 elems->ht_info_elem_len + 2);
2682 bss->ht_add_ie_len = elems->ht_info_elem_len + 2;
2684 bss->ht_add_ie_len = 0;
2685 } else if (!elems->ht_info_elem && bss->ht_add_ie) {
2686 kfree(bss->ht_add_ie);
2687 bss->ht_add_ie = NULL;
2688 bss->ht_add_ie_len = 0;
2691 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2692 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2695 struct ieee80211_tim_ie *tim_ie =
2696 (struct ieee80211_tim_ie *)elems->tim;
2697 bss->dtim_period = tim_ie->dtim_period;
2700 /* set default value for buggy APs */
2701 if (!elems->tim || bss->dtim_period == 0)
2702 bss->dtim_period = 1;
2704 bss->supp_rates_len = 0;
2705 if (elems->supp_rates) {
2706 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2707 if (clen > elems->supp_rates_len)
2708 clen = elems->supp_rates_len;
2709 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
2711 bss->supp_rates_len += clen;
2713 if (elems->ext_supp_rates) {
2714 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2715 if (clen > elems->ext_supp_rates_len)
2716 clen = elems->ext_supp_rates_len;
2717 memcpy(&bss->supp_rates[bss->supp_rates_len],
2718 elems->ext_supp_rates, clen);
2719 bss->supp_rates_len += clen;
2722 bss->band = rx_status->band;
2724 bss->timestamp = beacon_timestamp;
2725 bss->last_update = jiffies;
2726 bss->signal = rx_status->signal;
2727 bss->noise = rx_status->noise;
2728 bss->qual = rx_status->qual;
2729 if (!beacon && !bss->probe_resp)
2730 bss->probe_resp = true;
2733 * In STA mode, the remaining parameters should not be overridden
2734 * by beacons because they're not necessarily accurate there.
2736 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2737 bss->probe_resp && beacon) {
2738 ieee80211_rx_bss_put(local, bss);
2743 (!bss->wpa_ie || bss->wpa_ie_len != elems->wpa_len ||
2744 memcmp(bss->wpa_ie, elems->wpa, elems->wpa_len))) {
2746 bss->wpa_ie = kmalloc(elems->wpa_len + 2, GFP_ATOMIC);
2748 memcpy(bss->wpa_ie, elems->wpa - 2, elems->wpa_len + 2);
2749 bss->wpa_ie_len = elems->wpa_len + 2;
2751 bss->wpa_ie_len = 0;
2752 } else if (!elems->wpa && bss->wpa_ie) {
2755 bss->wpa_ie_len = 0;
2759 (!bss->rsn_ie || bss->rsn_ie_len != elems->rsn_len ||
2760 memcmp(bss->rsn_ie, elems->rsn, elems->rsn_len))) {
2762 bss->rsn_ie = kmalloc(elems->rsn_len + 2, GFP_ATOMIC);
2764 memcpy(bss->rsn_ie, elems->rsn - 2, elems->rsn_len + 2);
2765 bss->rsn_ie_len = elems->rsn_len + 2;
2767 bss->rsn_ie_len = 0;
2768 } else if (!elems->rsn && bss->rsn_ie) {
2771 bss->rsn_ie_len = 0;
2776 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2780 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2781 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2782 * Alliance (September 1, 2004) is incorporated by reference herein.
2783 * The inclusion of the WMM Parameters in probe responses and
2784 * association responses is mandatory for WMM enabled networks. The
2785 * inclusion of the WMM Parameters in beacons, however, is optional.
2788 if (elems->wmm_param &&
2789 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_param_len ||
2790 memcmp(bss->wmm_ie, elems->wmm_param, elems->wmm_param_len))) {
2792 bss->wmm_ie = kmalloc(elems->wmm_param_len + 2, GFP_ATOMIC);
2794 memcpy(bss->wmm_ie, elems->wmm_param - 2,
2795 elems->wmm_param_len + 2);
2796 bss->wmm_ie_len = elems->wmm_param_len + 2;
2798 bss->wmm_ie_len = 0;
2799 } else if (elems->wmm_info &&
2800 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_info_len ||
2801 memcmp(bss->wmm_ie, elems->wmm_info,
2802 elems->wmm_info_len))) {
2803 /* As for certain AP's Fifth bit is not set in WMM IE in
2804 * beacon frames.So while parsing the beacon frame the
2805 * wmm_info structure is used instead of wmm_param.
2806 * wmm_info structure was never used to set bss->wmm_ie.
2807 * This code fixes this problem by copying the WME
2808 * information from wmm_info to bss->wmm_ie and enabling
2809 * n-band association.
2812 bss->wmm_ie = kmalloc(elems->wmm_info_len + 2, GFP_ATOMIC);
2814 memcpy(bss->wmm_ie, elems->wmm_info - 2,
2815 elems->wmm_info_len + 2);
2816 bss->wmm_ie_len = elems->wmm_info_len + 2;
2818 bss->wmm_ie_len = 0;
2819 } else if (!elems->wmm_param && !elems->wmm_info && bss->wmm_ie) {
2822 bss->wmm_ie_len = 0;
2825 /* check if we need to merge IBSS */
2826 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2827 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2828 bss->capability & WLAN_CAPABILITY_IBSS &&
2829 bss->freq == local->oper_channel->center_freq &&
2830 elems->ssid_len == sdata->u.sta.ssid_len &&
2831 memcmp(elems->ssid, sdata->u.sta.ssid,
2832 sdata->u.sta.ssid_len) == 0) {
2833 if (rx_status->flag & RX_FLAG_TSFT) {
2834 /* in order for correct IBSS merging we need mactime
2836 * since mactime is defined as the time the first data
2837 * symbol of the frame hits the PHY, and the timestamp
2838 * of the beacon is defined as "the time that the data
2839 * symbol containing the first bit of the timestamp is
2840 * transmitted to the PHY plus the transmitting STA’s
2841 * delays through its local PHY from the MAC-PHY
2842 * interface to its interface with the WM"
2843 * (802.11 11.1.2) - equals the time this bit arrives at
2844 * the receiver - we have to take into account the
2845 * offset between the two.
2846 * e.g: at 1 MBit that means mactime is 192 usec earlier
2847 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2849 int rate = local->hw.wiphy->bands[rx_status->band]->
2850 bitrates[rx_status->rate_idx].bitrate;
2851 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2852 } else if (local && local->ops && local->ops->get_tsf)
2853 /* second best option: get current TSF */
2854 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2856 /* can't merge without knowing the TSF */
2857 rx_timestamp = -1LLU;
2858 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2859 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2860 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2861 print_mac(mac, mgmt->sa),
2862 print_mac(mac2, mgmt->bssid),
2863 (unsigned long long)rx_timestamp,
2864 (unsigned long long)beacon_timestamp,
2865 (unsigned long long)(rx_timestamp - beacon_timestamp),
2867 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2868 if (beacon_timestamp > rx_timestamp) {
2869 #ifndef CONFIG_MAC80211_IBSS_DEBUG
2870 printk(KERN_DEBUG "%s: beacon TSF higher than "
2871 "local TSF - IBSS merge with BSSID %s\n",
2872 dev->name, print_mac(mac, mgmt->bssid));
2874 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2875 ieee80211_ibss_add_sta(dev, NULL,
2876 mgmt->bssid, mgmt->sa,
2877 BIT(rx_status->rate_idx));
2881 ieee80211_rx_bss_put(local, bss);
2885 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2886 struct ieee80211_mgmt *mgmt,
2888 struct ieee80211_rx_status *rx_status)
2891 struct ieee802_11_elems elems;
2893 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2897 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2900 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 0);
2904 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2905 struct ieee80211_mgmt *mgmt,
2907 struct ieee80211_rx_status *rx_status)
2909 struct ieee80211_sub_if_data *sdata;
2910 struct ieee80211_if_sta *ifsta;
2912 struct ieee802_11_elems elems;
2913 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2914 struct ieee80211_conf *conf = &local->hw.conf;
2917 /* Process beacon from the current BSS */
2918 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2922 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2924 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 1);
2926 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2927 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2929 ifsta = &sdata->u.sta;
2931 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2932 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2935 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2936 elems.wmm_param_len);
2938 /* Do not send changes to driver if we are scanning. This removes
2939 * requirement that driver's bss_info_changed function needs to be
2941 if (local->sta_sw_scanning || local->sta_hw_scanning)
2944 if (elems.erp_info && elems.erp_info_len >= 1)
2945 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2947 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2948 changed |= ieee80211_handle_protect_preamb(sdata, false,
2949 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2952 if (elems.ht_cap_elem && elems.ht_info_elem &&
2953 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2954 struct ieee80211_ht_bss_info bss_info;
2956 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2957 (struct ieee80211_ht_addt_info *)
2958 elems.ht_info_elem, &bss_info);
2959 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2963 ieee80211_bss_info_change_notify(sdata, changed);
2967 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2968 struct ieee80211_if_sta *ifsta,
2969 struct ieee80211_mgmt *mgmt,
2971 struct ieee80211_rx_status *rx_status)
2973 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2974 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2976 struct sk_buff *skb;
2977 struct ieee80211_mgmt *resp;
2979 DECLARE_MAC_BUF(mac);
2980 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2981 DECLARE_MAC_BUF(mac2);
2982 DECLARE_MAC_BUF(mac3);
2985 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2986 ifsta->state != IEEE80211_IBSS_JOINED ||
2987 len < 24 + 2 || !ifsta->probe_resp)
2990 if (local->ops->tx_last_beacon)
2991 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2995 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2996 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2997 "%s (tx_last_beacon=%d)\n",
2998 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2999 print_mac(mac3, mgmt->bssid), tx_last_beacon);
3000 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3002 if (!tx_last_beacon)
3005 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
3006 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
3009 end = ((u8 *) mgmt) + len;
3010 pos = mgmt->u.probe_req.variable;
3011 if (pos[0] != WLAN_EID_SSID ||
3012 pos + 2 + pos[1] > end) {
3013 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3014 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
3016 dev->name, print_mac(mac, mgmt->sa));
3021 (pos[1] != ifsta->ssid_len ||
3022 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
3023 /* Ignore ProbeReq for foreign SSID */
3027 /* Reply with ProbeResp */
3028 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
3032 resp = (struct ieee80211_mgmt *) skb->data;
3033 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3034 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3035 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3036 dev->name, print_mac(mac, resp->da));
3037 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3038 ieee80211_sta_tx(dev, skb, 0);
3041 static void ieee80211_rx_mgmt_action(struct net_device *dev,
3042 struct ieee80211_if_sta *ifsta,
3043 struct ieee80211_mgmt *mgmt,
3045 struct ieee80211_rx_status *rx_status)
3047 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3048 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3050 if (len < IEEE80211_MIN_ACTION_SIZE)
3053 switch (mgmt->u.action.category) {
3054 case WLAN_CATEGORY_SPECTRUM_MGMT:
3055 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
3057 switch (mgmt->u.action.u.chan_switch.action_code) {
3058 case WLAN_ACTION_SPCT_MSR_REQ:
3059 if (len < (IEEE80211_MIN_ACTION_SIZE +
3060 sizeof(mgmt->u.action.u.measurement)))
3062 ieee80211_sta_process_measurement_req(dev, mgmt, len);
3066 case WLAN_CATEGORY_BACK:
3067 switch (mgmt->u.action.u.addba_req.action_code) {
3068 case WLAN_ACTION_ADDBA_REQ:
3069 if (len < (IEEE80211_MIN_ACTION_SIZE +
3070 sizeof(mgmt->u.action.u.addba_req)))
3072 ieee80211_sta_process_addba_request(dev, mgmt, len);
3074 case WLAN_ACTION_ADDBA_RESP:
3075 if (len < (IEEE80211_MIN_ACTION_SIZE +
3076 sizeof(mgmt->u.action.u.addba_resp)))
3078 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3080 case WLAN_ACTION_DELBA:
3081 if (len < (IEEE80211_MIN_ACTION_SIZE +
3082 sizeof(mgmt->u.action.u.delba)))
3084 ieee80211_sta_process_delba(dev, mgmt, len);
3088 case PLINK_CATEGORY:
3089 if (ieee80211_vif_is_mesh(&sdata->vif))
3090 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3092 case MESH_PATH_SEL_CATEGORY:
3093 if (ieee80211_vif_is_mesh(&sdata->vif))
3094 mesh_rx_path_sel_frame(dev, mgmt, len);
3099 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3100 struct ieee80211_rx_status *rx_status)
3102 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3103 struct ieee80211_sub_if_data *sdata;
3104 struct ieee80211_if_sta *ifsta;
3105 struct ieee80211_mgmt *mgmt;
3111 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3112 ifsta = &sdata->u.sta;
3114 mgmt = (struct ieee80211_mgmt *) skb->data;
3115 fc = le16_to_cpu(mgmt->frame_control);
3117 switch (fc & IEEE80211_FCTL_STYPE) {
3118 case IEEE80211_STYPE_PROBE_REQ:
3119 case IEEE80211_STYPE_PROBE_RESP:
3120 case IEEE80211_STYPE_BEACON:
3121 case IEEE80211_STYPE_ACTION:
3122 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3123 case IEEE80211_STYPE_AUTH:
3124 case IEEE80211_STYPE_ASSOC_RESP:
3125 case IEEE80211_STYPE_REASSOC_RESP:
3126 case IEEE80211_STYPE_DEAUTH:
3127 case IEEE80211_STYPE_DISASSOC:
3128 skb_queue_tail(&ifsta->skb_queue, skb);
3129 queue_work(local->hw.workqueue, &ifsta->work);
3138 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3139 struct sk_buff *skb)
3141 struct ieee80211_rx_status *rx_status;
3142 struct ieee80211_sub_if_data *sdata;
3143 struct ieee80211_if_sta *ifsta;
3144 struct ieee80211_mgmt *mgmt;
3147 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3148 ifsta = &sdata->u.sta;
3150 rx_status = (struct ieee80211_rx_status *) skb->cb;
3151 mgmt = (struct ieee80211_mgmt *) skb->data;
3152 fc = le16_to_cpu(mgmt->frame_control);
3154 switch (fc & IEEE80211_FCTL_STYPE) {
3155 case IEEE80211_STYPE_PROBE_REQ:
3156 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3159 case IEEE80211_STYPE_PROBE_RESP:
3160 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3162 case IEEE80211_STYPE_BEACON:
3163 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3165 case IEEE80211_STYPE_AUTH:
3166 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3168 case IEEE80211_STYPE_ASSOC_RESP:
3169 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
3171 case IEEE80211_STYPE_REASSOC_RESP:
3172 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
3174 case IEEE80211_STYPE_DEAUTH:
3175 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3177 case IEEE80211_STYPE_DISASSOC:
3178 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3180 case IEEE80211_STYPE_ACTION:
3181 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
3190 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3191 struct ieee80211_rx_status *rx_status)
3193 struct ieee80211_mgmt *mgmt;
3197 return RX_DROP_UNUSABLE;
3199 mgmt = (struct ieee80211_mgmt *) skb->data;
3200 fc = mgmt->frame_control;
3202 if (ieee80211_is_ctl(fc))
3206 return RX_DROP_MONITOR;
3208 if (ieee80211_is_probe_resp(fc)) {
3209 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3214 if (ieee80211_is_beacon(fc)) {
3215 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3224 static int ieee80211_sta_active_ibss(struct net_device *dev)
3226 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3228 struct sta_info *sta;
3229 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3233 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3234 if (sta->sdata == sdata &&
3235 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3248 static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
3250 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3251 struct sta_info *sta, *tmp;
3252 LIST_HEAD(tmp_list);
3253 DECLARE_MAC_BUF(mac);
3254 unsigned long flags;
3256 spin_lock_irqsave(&local->sta_lock, flags);
3257 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3258 if (time_after(jiffies, sta->last_rx + exp_time)) {
3259 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3260 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3261 dev->name, print_mac(mac, sta->addr));
3263 __sta_info_unlink(&sta);
3265 list_add(&sta->list, &tmp_list);
3267 spin_unlock_irqrestore(&local->sta_lock, flags);
3269 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3270 sta_info_destroy(sta);
3274 static void ieee80211_sta_merge_ibss(struct net_device *dev,
3275 struct ieee80211_if_sta *ifsta)
3277 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3279 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
3280 if (ieee80211_sta_active_ibss(dev))
3283 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3284 "IBSS networks with same SSID (merge)\n", dev->name);
3285 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3289 #ifdef CONFIG_MAC80211_MESH
3290 static void ieee80211_mesh_housekeeping(struct net_device *dev,
3291 struct ieee80211_if_sta *ifsta)
3293 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3296 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3297 mesh_path_expire(dev);
3299 free_plinks = mesh_plink_availables(sdata);
3300 if (free_plinks != sdata->u.sta.accepting_plinks)
3301 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3303 mod_timer(&ifsta->timer, jiffies +
3304 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3308 void ieee80211_start_mesh(struct net_device *dev)
3310 struct ieee80211_if_sta *ifsta;
3311 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3312 ifsta = &sdata->u.sta;
3313 ifsta->state = IEEE80211_MESH_UP;
3314 ieee80211_sta_timer((unsigned long)sdata);
3315 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3320 void ieee80211_sta_timer(unsigned long data)
3322 struct ieee80211_sub_if_data *sdata =
3323 (struct ieee80211_sub_if_data *) data;
3324 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3325 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3327 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3328 queue_work(local->hw.workqueue, &ifsta->work);
3331 void ieee80211_sta_work(struct work_struct *work)
3333 struct ieee80211_sub_if_data *sdata =
3334 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3335 struct net_device *dev = sdata->dev;
3336 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3337 struct ieee80211_if_sta *ifsta;
3338 struct sk_buff *skb;
3340 if (!netif_running(dev))
3343 if (local->sta_sw_scanning || local->sta_hw_scanning)
3346 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3347 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3348 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
3350 ifsta = &sdata->u.sta;
3352 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3353 ieee80211_sta_rx_queued_mgmt(dev, skb);
3355 #ifdef CONFIG_MAC80211_MESH
3356 if (ifsta->preq_queue_len &&
3358 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3359 mesh_path_start_discovery(dev);
3362 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3363 ifsta->state != IEEE80211_ASSOCIATE &&
3364 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3365 if (ifsta->scan_ssid_len)
3366 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3368 ieee80211_sta_start_scan(dev, NULL, 0);
3372 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3373 if (ieee80211_sta_config_auth(dev, ifsta))
3375 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3376 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3379 switch (ifsta->state) {
3380 case IEEE80211_DISABLED:
3382 case IEEE80211_AUTHENTICATE:
3383 ieee80211_authenticate(dev, ifsta);
3385 case IEEE80211_ASSOCIATE:
3386 ieee80211_associate(dev, ifsta);
3388 case IEEE80211_ASSOCIATED:
3389 ieee80211_associated(dev, ifsta);
3391 case IEEE80211_IBSS_SEARCH:
3392 ieee80211_sta_find_ibss(dev, ifsta);
3394 case IEEE80211_IBSS_JOINED:
3395 ieee80211_sta_merge_ibss(dev, ifsta);
3397 #ifdef CONFIG_MAC80211_MESH
3398 case IEEE80211_MESH_UP:
3399 ieee80211_mesh_housekeeping(dev, ifsta);
3407 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3408 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3409 "mixed-cell disabled - disassociate\n", dev->name);
3411 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3412 ieee80211_set_disassoc(dev, ifsta, 0);
3417 static void ieee80211_sta_reset_auth(struct net_device *dev,
3418 struct ieee80211_if_sta *ifsta)
3420 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3422 if (local->ops->reset_tsf) {
3423 /* Reset own TSF to allow time synchronization work. */
3424 local->ops->reset_tsf(local_to_hw(local));
3427 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3430 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3431 ifsta->auth_alg = WLAN_AUTH_OPEN;
3432 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3433 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3434 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3435 ifsta->auth_alg = WLAN_AUTH_LEAP;
3437 ifsta->auth_alg = WLAN_AUTH_OPEN;
3438 ifsta->auth_transaction = -1;
3439 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3440 ifsta->auth_tries = ifsta->assoc_tries = 0;
3441 netif_carrier_off(dev);
3445 void ieee80211_sta_req_auth(struct net_device *dev,
3446 struct ieee80211_if_sta *ifsta)
3448 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3449 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3451 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3454 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3455 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3456 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3457 IEEE80211_STA_AUTO_SSID_SEL))) {
3458 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3459 queue_work(local->hw.workqueue, &ifsta->work);
3463 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3464 const char *ssid, int ssid_len)
3466 int tmp, hidden_ssid;
3468 if (ssid_len == ifsta->ssid_len &&
3469 !memcmp(ifsta->ssid, ssid, ssid_len))
3472 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3478 if (ssid[tmp] != '\0') {
3484 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3487 if (ssid_len == 1 && ssid[0] == ' ')
3493 static int ieee80211_sta_config_auth(struct net_device *dev,
3494 struct ieee80211_if_sta *ifsta)
3496 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3497 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3498 struct ieee80211_sta_bss *bss, *selected = NULL;
3499 int top_rssi = 0, freq;
3501 spin_lock_bh(&local->sta_bss_lock);
3502 freq = local->oper_channel->center_freq;
3503 list_for_each_entry(bss, &local->sta_bss_list, list) {
3504 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3507 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3508 IEEE80211_STA_AUTO_BSSID_SEL |
3509 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3510 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3511 !!sdata->default_key))
3514 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3518 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3519 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3522 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3523 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3526 if (!selected || top_rssi < bss->signal) {
3528 top_rssi = bss->signal;
3532 atomic_inc(&selected->users);
3533 spin_unlock_bh(&local->sta_bss_lock);
3536 ieee80211_set_freq(dev, selected->freq);
3537 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3538 ieee80211_sta_set_ssid(dev, selected->ssid,
3539 selected->ssid_len);
3540 ieee80211_sta_set_bssid(dev, selected->bssid);
3541 ieee80211_sta_def_wmm_params(dev, selected, 0);
3542 ieee80211_rx_bss_put(local, selected);
3543 ifsta->state = IEEE80211_AUTHENTICATE;
3544 ieee80211_sta_reset_auth(dev, ifsta);
3547 if (ifsta->state != IEEE80211_AUTHENTICATE) {
3548 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3549 ieee80211_sta_start_scan(dev, NULL, 0);
3551 ieee80211_sta_start_scan(dev, ifsta->ssid,
3553 ifsta->state = IEEE80211_AUTHENTICATE;
3554 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3556 ifsta->state = IEEE80211_DISABLED;
3562 static int ieee80211_sta_create_ibss(struct net_device *dev,
3563 struct ieee80211_if_sta *ifsta)
3565 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3566 struct ieee80211_sta_bss *bss;
3567 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3568 struct ieee80211_supported_band *sband;
3569 u8 bssid[ETH_ALEN], *pos;
3572 DECLARE_MAC_BUF(mac);
3575 /* Easier testing, use fixed BSSID. */
3576 memset(bssid, 0xfe, ETH_ALEN);
3578 /* Generate random, not broadcast, locally administered BSSID. Mix in
3579 * own MAC address to make sure that devices that do not have proper
3580 * random number generator get different BSSID. */
3581 get_random_bytes(bssid, ETH_ALEN);
3582 for (i = 0; i < ETH_ALEN; i++)
3583 bssid[i] ^= dev->dev_addr[i];
3588 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3589 dev->name, print_mac(mac, bssid));
3591 bss = ieee80211_rx_bss_add(dev, bssid,
3592 local->hw.conf.channel->center_freq,
3593 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3597 bss->band = local->hw.conf.channel->band;
3598 sband = local->hw.wiphy->bands[bss->band];
3600 if (local->hw.conf.beacon_int == 0)
3601 local->hw.conf.beacon_int = 100;
3602 bss->beacon_int = local->hw.conf.beacon_int;
3603 bss->last_update = jiffies;
3604 bss->capability = WLAN_CAPABILITY_IBSS;
3606 if (sdata->default_key)
3607 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3609 sdata->drop_unencrypted = 0;
3611 bss->supp_rates_len = sband->n_bitrates;
3612 pos = bss->supp_rates;
3613 for (i = 0; i < sband->n_bitrates; i++) {
3614 int rate = sband->bitrates[i].bitrate;
3615 *pos++ = (u8) (rate / 5);
3618 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3619 ieee80211_rx_bss_put(local, bss);
3624 static int ieee80211_sta_find_ibss(struct net_device *dev,
3625 struct ieee80211_if_sta *ifsta)
3627 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3628 struct ieee80211_sta_bss *bss;
3632 DECLARE_MAC_BUF(mac);
3633 DECLARE_MAC_BUF(mac2);
3635 if (ifsta->ssid_len == 0)
3638 active_ibss = ieee80211_sta_active_ibss(dev);
3639 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3640 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3641 dev->name, active_ibss);
3642 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3643 spin_lock_bh(&local->sta_bss_lock);
3644 list_for_each_entry(bss, &local->sta_bss_list, list) {
3645 if (ifsta->ssid_len != bss->ssid_len ||
3646 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3647 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3649 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3650 printk(KERN_DEBUG " bssid=%s found\n",
3651 print_mac(mac, bss->bssid));
3652 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3653 memcpy(bssid, bss->bssid, ETH_ALEN);
3655 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3658 spin_unlock_bh(&local->sta_bss_lock);
3660 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3662 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3663 "%s\n", print_mac(mac, bssid),
3664 print_mac(mac2, ifsta->bssid));
3665 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3667 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3671 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3672 search_freq = bss->freq;
3674 search_freq = local->hw.conf.channel->center_freq;
3676 bss = ieee80211_rx_bss_get(dev, bssid, search_freq,
3677 ifsta->ssid, ifsta->ssid_len);
3681 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3682 " based on configured SSID\n",
3683 dev->name, print_mac(mac, bssid));
3684 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3685 ieee80211_rx_bss_put(local, bss);
3690 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3691 printk(KERN_DEBUG " did not try to join ibss\n");
3692 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3694 /* Selected IBSS not found in current scan results - try to scan */
3695 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3696 !ieee80211_sta_active_ibss(dev)) {
3697 mod_timer(&ifsta->timer, jiffies +
3698 IEEE80211_IBSS_MERGE_INTERVAL);
3699 } else if (time_after(jiffies, local->last_scan_completed +
3700 IEEE80211_SCAN_INTERVAL)) {
3701 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3702 "join\n", dev->name);
3703 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3705 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3706 int interval = IEEE80211_SCAN_INTERVAL;
3708 if (time_after(jiffies, ifsta->ibss_join_req +
3709 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3710 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3711 (!(local->oper_channel->flags &
3712 IEEE80211_CHAN_NO_IBSS)))
3713 return ieee80211_sta_create_ibss(dev, ifsta);
3714 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3715 printk(KERN_DEBUG "%s: IBSS not allowed on"
3716 " %d MHz\n", dev->name,
3717 local->hw.conf.channel->center_freq);
3720 /* No IBSS found - decrease scan interval and continue
3722 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3725 ifsta->state = IEEE80211_IBSS_SEARCH;
3726 mod_timer(&ifsta->timer, jiffies + interval);
3734 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3736 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3737 struct ieee80211_if_sta *ifsta;
3740 if (len > IEEE80211_MAX_SSID_LEN)
3743 ifsta = &sdata->u.sta;
3745 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3746 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3747 memcpy(ifsta->ssid, ssid, len);
3748 ifsta->ssid_len = len;
3749 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3753 * Hack! MLME code needs to be cleaned up to have different
3754 * entry points for configuration and internal selection change
3756 if (netif_running(sdata->dev))
3757 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3759 printk(KERN_DEBUG "%s: Failed to config new SSID to "
3760 "the low-level driver\n", dev->name);
3766 ifsta->flags |= IEEE80211_STA_SSID_SET;
3768 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3770 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3771 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3772 ifsta->ibss_join_req = jiffies;
3773 ifsta->state = IEEE80211_IBSS_SEARCH;
3774 return ieee80211_sta_find_ibss(dev, ifsta);
3781 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3783 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3784 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3785 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3786 *len = ifsta->ssid_len;
3791 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3793 struct ieee80211_sub_if_data *sdata;
3794 struct ieee80211_if_sta *ifsta;
3797 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3798 ifsta = &sdata->u.sta;
3800 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3801 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3804 * Hack! See also ieee80211_sta_set_ssid.
3806 if (netif_running(sdata->dev))
3807 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
3809 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3810 "the low-level driver\n", dev->name);
3815 if (is_valid_ether_addr(bssid))
3816 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3818 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3824 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3825 struct ieee80211_sub_if_data *sdata,
3828 struct sk_buff *skb;
3829 struct ieee80211_hdr *nullfunc;
3832 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3834 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3835 "frame\n", sdata->dev->name);
3838 skb_reserve(skb, local->hw.extra_tx_headroom);
3840 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3841 memset(nullfunc, 0, 24);
3842 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3843 IEEE80211_FCTL_TODS);
3845 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3846 nullfunc->frame_control = fc;
3847 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3848 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3849 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3851 ieee80211_sta_tx(sdata->dev, skb, 0);
3855 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3857 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3858 ieee80211_vif_is_mesh(&sdata->vif))
3859 ieee80211_sta_timer((unsigned long)sdata);
3862 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3864 struct ieee80211_local *local = hw_to_local(hw);
3865 struct net_device *dev = local->scan_dev;
3866 struct ieee80211_sub_if_data *sdata;
3867 union iwreq_data wrqu;
3869 local->last_scan_completed = jiffies;
3870 memset(&wrqu, 0, sizeof(wrqu));
3871 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3873 if (local->sta_hw_scanning) {
3874 local->sta_hw_scanning = 0;
3875 if (ieee80211_hw_config(local))
3876 printk(KERN_DEBUG "%s: failed to restore operational "
3877 "channel after scan\n", dev->name);
3878 /* Restart STA timer for HW scan case */
3880 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3881 ieee80211_restart_sta_timer(sdata);
3887 local->sta_sw_scanning = 0;
3888 if (ieee80211_hw_config(local))
3889 printk(KERN_DEBUG "%s: failed to restore operational "
3890 "channel after scan\n", dev->name);
3893 netif_tx_lock_bh(local->mdev);
3894 netif_addr_lock(local->mdev);
3895 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3896 local->ops->configure_filter(local_to_hw(local),
3897 FIF_BCN_PRBRESP_PROMISC,
3898 &local->filter_flags,
3899 local->mdev->mc_count,
3900 local->mdev->mc_list);
3902 netif_addr_unlock(local->mdev);
3903 netif_tx_unlock_bh(local->mdev);
3906 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3907 /* Tell AP we're back */
3908 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3909 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3910 ieee80211_send_nullfunc(local, sdata, 0);
3912 ieee80211_restart_sta_timer(sdata);
3914 netif_wake_queue(sdata->dev);
3919 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3920 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3921 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3922 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3923 (!(ifsta->state == IEEE80211_IBSS_JOINED) &&
3924 !ieee80211_sta_active_ibss(dev)))
3925 ieee80211_sta_find_ibss(dev, ifsta);
3928 EXPORT_SYMBOL(ieee80211_scan_completed);
3930 void ieee80211_sta_scan_work(struct work_struct *work)
3932 struct ieee80211_local *local =
3933 container_of(work, struct ieee80211_local, scan_work.work);
3934 struct net_device *dev = local->scan_dev;
3935 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3936 struct ieee80211_supported_band *sband;
3937 struct ieee80211_channel *chan;
3939 unsigned long next_delay = 0;
3941 if (!local->sta_sw_scanning)
3944 switch (local->scan_state) {
3945 case SCAN_SET_CHANNEL:
3947 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3948 * after we successfully scanned the last channel of the last
3949 * band (and the last band is supported by the hw)
3951 if (local->scan_band < IEEE80211_NUM_BANDS)
3952 sband = local->hw.wiphy->bands[local->scan_band];
3957 * If we are at an unsupported band and have more bands
3958 * left to scan, advance to the next supported one.
3960 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3962 sband = local->hw.wiphy->bands[local->scan_band];
3963 local->scan_channel_idx = 0;
3966 /* if no more bands/channels left, complete scan */
3967 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3968 ieee80211_scan_completed(local_to_hw(local));
3972 chan = &sband->channels[local->scan_channel_idx];
3974 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3975 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3976 chan->flags & IEEE80211_CHAN_NO_IBSS))
3980 local->scan_channel = chan;
3981 if (ieee80211_hw_config(local)) {
3982 printk(KERN_DEBUG "%s: failed to set freq to "
3983 "%d MHz for scan\n", dev->name,
3989 /* advance state machine to next channel/band */
3990 local->scan_channel_idx++;
3991 if (local->scan_channel_idx >= sband->n_channels) {
3993 * scan_band may end up == IEEE80211_NUM_BANDS, but
3994 * we'll catch that case above and complete the scan
3995 * if that is the case.
3998 local->scan_channel_idx = 0;
4004 next_delay = IEEE80211_PROBE_DELAY +
4005 usecs_to_jiffies(local->hw.channel_change_time);
4006 local->scan_state = SCAN_SEND_PROBE;
4008 case SCAN_SEND_PROBE:
4009 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
4010 local->scan_state = SCAN_SET_CHANNEL;
4012 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
4014 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
4015 local->scan_ssid_len);
4016 next_delay = IEEE80211_CHANNEL_TIME;
4020 if (local->sta_sw_scanning)
4021 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4026 static int ieee80211_sta_start_scan(struct net_device *dev,
4027 u8 *ssid, size_t ssid_len)
4029 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4030 struct ieee80211_sub_if_data *sdata;
4032 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4035 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
4036 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
4039 * ScanType: ACTIVE, PASSIVE
4040 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
4041 * a Probe frame during active scanning
4043 * MinChannelTime (>= ProbeDelay), in TU
4044 * MaxChannelTime: (>= MinChannelTime), in TU
4047 /* MLME-SCAN.confirm
4049 * ResultCode: SUCCESS, INVALID_PARAMETERS
4052 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4053 if (local->scan_dev == dev)
4058 if (local->ops->hw_scan) {
4059 int rc = local->ops->hw_scan(local_to_hw(local),
4062 local->sta_hw_scanning = 1;
4063 local->scan_dev = dev;
4068 local->sta_sw_scanning = 1;
4071 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4072 netif_stop_queue(sdata->dev);
4073 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
4074 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
4075 ieee80211_send_nullfunc(local, sdata, 1);
4080 local->scan_ssid_len = ssid_len;
4081 memcpy(local->scan_ssid, ssid, ssid_len);
4083 local->scan_ssid_len = 0;
4084 local->scan_state = SCAN_SET_CHANNEL;
4085 local->scan_channel_idx = 0;
4086 local->scan_band = IEEE80211_BAND_2GHZ;
4087 local->scan_dev = dev;
4089 netif_addr_lock_bh(local->mdev);
4090 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4091 local->ops->configure_filter(local_to_hw(local),
4092 FIF_BCN_PRBRESP_PROMISC,
4093 &local->filter_flags,
4094 local->mdev->mc_count,
4095 local->mdev->mc_list);
4096 netif_addr_unlock_bh(local->mdev);
4098 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4099 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4100 IEEE80211_CHANNEL_TIME);
4106 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4108 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4109 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4110 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4112 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4113 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4115 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4116 if (local->scan_dev == dev)
4121 ifsta->scan_ssid_len = ssid_len;
4123 memcpy(ifsta->scan_ssid, ssid, ssid_len);
4124 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4125 queue_work(local->hw.workqueue, &ifsta->work);
4130 ieee80211_sta_scan_result(struct net_device *dev,
4131 struct iw_request_info *info,
4132 struct ieee80211_sta_bss *bss,
4133 char *current_ev, char *end_buf)
4135 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4136 struct iw_event iwe;
4138 if (time_after(jiffies,
4139 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4142 memset(&iwe, 0, sizeof(iwe));
4143 iwe.cmd = SIOCGIWAP;
4144 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4145 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4146 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4149 memset(&iwe, 0, sizeof(iwe));
4150 iwe.cmd = SIOCGIWESSID;
4151 if (bss_mesh_cfg(bss)) {
4152 iwe.u.data.length = bss_mesh_id_len(bss);
4153 iwe.u.data.flags = 1;
4154 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4155 &iwe, bss_mesh_id(bss));
4157 iwe.u.data.length = bss->ssid_len;
4158 iwe.u.data.flags = 1;
4159 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4163 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4164 || bss_mesh_cfg(bss)) {
4165 memset(&iwe, 0, sizeof(iwe));
4166 iwe.cmd = SIOCGIWMODE;
4167 if (bss_mesh_cfg(bss))
4168 iwe.u.mode = IW_MODE_MESH;
4169 else if (bss->capability & WLAN_CAPABILITY_ESS)
4170 iwe.u.mode = IW_MODE_MASTER;
4172 iwe.u.mode = IW_MODE_ADHOC;
4173 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4174 &iwe, IW_EV_UINT_LEN);
4177 memset(&iwe, 0, sizeof(iwe));
4178 iwe.cmd = SIOCGIWFREQ;
4179 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4181 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4184 memset(&iwe, 0, sizeof(iwe));
4185 iwe.cmd = SIOCGIWFREQ;
4186 iwe.u.freq.m = bss->freq;
4188 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4190 memset(&iwe, 0, sizeof(iwe));
4192 iwe.u.qual.qual = bss->qual;
4193 iwe.u.qual.level = bss->signal;
4194 iwe.u.qual.noise = bss->noise;
4195 iwe.u.qual.updated = local->wstats_flags;
4196 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
4199 memset(&iwe, 0, sizeof(iwe));
4200 iwe.cmd = SIOCGIWENCODE;
4201 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4202 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4204 iwe.u.data.flags = IW_ENCODE_DISABLED;
4205 iwe.u.data.length = 0;
4206 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4209 if (bss && bss->wpa_ie) {
4210 memset(&iwe, 0, sizeof(iwe));
4211 iwe.cmd = IWEVGENIE;
4212 iwe.u.data.length = bss->wpa_ie_len;
4213 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4217 if (bss && bss->rsn_ie) {
4218 memset(&iwe, 0, sizeof(iwe));
4219 iwe.cmd = IWEVGENIE;
4220 iwe.u.data.length = bss->rsn_ie_len;
4221 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4225 if (bss && bss->ht_ie) {
4226 memset(&iwe, 0, sizeof(iwe));
4227 iwe.cmd = IWEVGENIE;
4228 iwe.u.data.length = bss->ht_ie_len;
4229 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4233 if (bss && bss->supp_rates_len > 0) {
4234 /* display all supported rates in readable format */
4235 char *p = current_ev + iwe_stream_lcp_len(info);
4238 memset(&iwe, 0, sizeof(iwe));
4239 iwe.cmd = SIOCGIWRATE;
4240 /* Those two flags are ignored... */
4241 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4243 for (i = 0; i < bss->supp_rates_len; i++) {
4244 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4246 p = iwe_stream_add_value(info, current_ev, p,
4247 end_buf, &iwe, IW_EV_PARAM_LEN);
4254 buf = kmalloc(30, GFP_ATOMIC);
4256 memset(&iwe, 0, sizeof(iwe));
4257 iwe.cmd = IWEVCUSTOM;
4258 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4259 iwe.u.data.length = strlen(buf);
4260 current_ev = iwe_stream_add_point(info, current_ev,
4263 memset(&iwe, 0, sizeof(iwe));
4264 iwe.cmd = IWEVCUSTOM;
4265 sprintf(buf, " Last beacon: %dms ago",
4266 jiffies_to_msecs(jiffies - bss->last_update));
4267 iwe.u.data.length = strlen(buf);
4268 current_ev = iwe_stream_add_point(info, current_ev,
4269 end_buf, &iwe, buf);
4274 if (bss_mesh_cfg(bss)) {
4276 u8 *cfg = bss_mesh_cfg(bss);
4277 buf = kmalloc(50, GFP_ATOMIC);
4279 memset(&iwe, 0, sizeof(iwe));
4280 iwe.cmd = IWEVCUSTOM;
4281 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4282 iwe.u.data.length = strlen(buf);
4283 current_ev = iwe_stream_add_point(info, current_ev,
4286 sprintf(buf, "Path Selection Protocol ID: "
4287 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4289 iwe.u.data.length = strlen(buf);
4290 current_ev = iwe_stream_add_point(info, current_ev,
4293 sprintf(buf, "Path Selection Metric ID: "
4294 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4296 iwe.u.data.length = strlen(buf);
4297 current_ev = iwe_stream_add_point(info, current_ev,
4300 sprintf(buf, "Congestion Control Mode ID: "
4301 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4303 iwe.u.data.length = strlen(buf);
4304 current_ev = iwe_stream_add_point(info, current_ev,
4307 sprintf(buf, "Channel Precedence: "
4308 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4310 iwe.u.data.length = strlen(buf);
4311 current_ev = iwe_stream_add_point(info, current_ev,
4322 int ieee80211_sta_scan_results(struct net_device *dev,
4323 struct iw_request_info *info,
4324 char *buf, size_t len)
4326 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4327 char *current_ev = buf;
4328 char *end_buf = buf + len;
4329 struct ieee80211_sta_bss *bss;
4331 spin_lock_bh(&local->sta_bss_lock);
4332 list_for_each_entry(bss, &local->sta_bss_list, list) {
4333 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4334 spin_unlock_bh(&local->sta_bss_lock);
4337 current_ev = ieee80211_sta_scan_result(dev, info, bss,
4338 current_ev, end_buf);
4340 spin_unlock_bh(&local->sta_bss_lock);
4341 return current_ev - buf;
4345 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4347 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4348 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4350 kfree(ifsta->extra_ie);
4352 ifsta->extra_ie = NULL;
4353 ifsta->extra_ie_len = 0;
4356 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4357 if (!ifsta->extra_ie) {
4358 ifsta->extra_ie_len = 0;
4361 memcpy(ifsta->extra_ie, ie, len);
4362 ifsta->extra_ie_len = len;
4367 struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4368 struct sk_buff *skb, u8 *bssid,
4369 u8 *addr, u64 supp_rates)
4371 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4372 struct sta_info *sta;
4373 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4374 DECLARE_MAC_BUF(mac);
4375 int band = local->hw.conf.channel->band;
4377 /* TODO: Could consider removing the least recently used entry and
4378 * allow new one to be added. */
4379 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4380 if (net_ratelimit()) {
4381 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4382 "entry %s\n", dev->name, print_mac(mac, addr));
4387 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
4390 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4391 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4392 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
4395 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4399 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
4402 sta->supp_rates[band] = supp_rates;
4404 sta->supp_rates[band] = sdata->u.sta.supp_rates_bits[band];
4406 rate_control_rate_init(sta, local);
4408 if (sta_info_insert(sta))
4415 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4417 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4418 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4420 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
4423 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4424 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4427 ieee80211_send_deauth(dev, ifsta, reason);
4428 ieee80211_set_disassoc(dev, ifsta, 1);
4433 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4435 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4436 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4438 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
4441 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4444 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4447 ieee80211_send_disassoc(dev, ifsta, reason);
4448 ieee80211_set_disassoc(dev, ifsta, 0);
4452 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4453 enum ieee80211_notification_types notif_type)
4455 struct ieee80211_local *local = hw_to_local(hw);
4456 struct ieee80211_sub_if_data *sdata;
4458 switch (notif_type) {
4459 case IEEE80211_NOTIFY_RE_ASSOC:
4461 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4462 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4465 ieee80211_sta_req_auth(sdata->dev, &sdata->u.sta);
4471 EXPORT_SYMBOL(ieee80211_notify_mac);