2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
19 #include <linux/delay.h>
20 #include <linux/if_ether.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/wireless.h>
25 #include <linux/random.h>
26 #include <linux/etherdevice.h>
27 #include <linux/rtnetlink.h>
28 #include <net/iw_handler.h>
29 #include <asm/types.h>
31 #include <net/mac80211.h>
32 #include "ieee80211_i.h"
37 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38 #define IEEE80211_AUTH_MAX_TRIES 3
39 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40 #define IEEE80211_ASSOC_MAX_TRIES 3
41 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
42 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
43 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
44 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
46 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
49 #define IEEE80211_PROBE_DELAY (HZ / 33)
50 #define IEEE80211_CHANNEL_TIME (HZ / 33)
51 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
55 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
57 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
60 #define ERP_INFO_USE_PROTECTION BIT(1)
62 /* mgmt header + 1 byte action code */
63 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
65 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
68 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
71 /* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73 #define IEEE80211_MIN_AMPDU_BUF 0x8
74 #define IEEE80211_MAX_AMPDU_BUF 0x40
76 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78 static struct ieee80211_sta_bss *
79 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
80 u8 *ssid, u8 ssid_len);
81 static void ieee80211_rx_bss_put(struct net_device *dev,
82 struct ieee80211_sta_bss *bss);
83 static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85 static int ieee80211_sta_wep_configured(struct net_device *dev);
86 static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88 static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
92 void ieee802_11_parse_elems(u8 *start, size_t len,
93 struct ieee802_11_elems *elems)
98 memset(elems, 0, sizeof(*elems));
113 elems->ssid_len = elen;
115 case WLAN_EID_SUPP_RATES:
116 elems->supp_rates = pos;
117 elems->supp_rates_len = elen;
119 case WLAN_EID_FH_PARAMS:
120 elems->fh_params = pos;
121 elems->fh_params_len = elen;
123 case WLAN_EID_DS_PARAMS:
124 elems->ds_params = pos;
125 elems->ds_params_len = elen;
127 case WLAN_EID_CF_PARAMS:
128 elems->cf_params = pos;
129 elems->cf_params_len = elen;
133 elems->tim_len = elen;
135 case WLAN_EID_IBSS_PARAMS:
136 elems->ibss_params = pos;
137 elems->ibss_params_len = elen;
139 case WLAN_EID_CHALLENGE:
140 elems->challenge = pos;
141 elems->challenge_len = elen;
144 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
146 /* Microsoft OUI (00:50:F2) */
148 /* OUI Type 1 - WPA IE */
150 elems->wpa_len = elen;
151 } else if (elen >= 5 && pos[3] == 2) {
153 elems->wmm_info = pos;
154 elems->wmm_info_len = elen;
155 } else if (pos[4] == 1) {
156 elems->wmm_param = pos;
157 elems->wmm_param_len = elen;
164 elems->rsn_len = elen;
166 case WLAN_EID_ERP_INFO:
167 elems->erp_info = pos;
168 elems->erp_info_len = elen;
170 case WLAN_EID_EXT_SUPP_RATES:
171 elems->ext_supp_rates = pos;
172 elems->ext_supp_rates_len = elen;
174 case WLAN_EID_HT_CAPABILITY:
175 elems->ht_cap_elem = pos;
176 elems->ht_cap_elem_len = elen;
178 case WLAN_EID_HT_EXTRA_INFO:
179 elems->ht_info_elem = pos;
180 elems->ht_info_elem_len = elen;
182 case WLAN_EID_MESH_ID:
183 elems->mesh_id = pos;
184 elems->mesh_id_len = elen;
186 case WLAN_EID_MESH_CONFIG:
187 elems->mesh_config = pos;
188 elems->mesh_config_len = elen;
190 case WLAN_EID_PEER_LINK:
191 elems->peer_link = pos;
192 elems->peer_link_len = elen;
196 elems->preq_len = elen;
200 elems->prep_len = elen;
204 elems->perr_len = elen;
216 static int ecw2cw(int ecw)
218 return (1 << ecw) - 1;
222 static void ieee80211_sta_def_wmm_params(struct net_device *dev,
223 struct ieee80211_sta_bss *bss,
226 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
227 struct ieee80211_local *local = sdata->local;
228 int i, have_higher_than_11mbit = 0;
231 /* cf. IEEE 802.11 9.2.12 */
232 for (i = 0; i < bss->supp_rates_len; i++)
233 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
234 have_higher_than_11mbit = 1;
236 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
237 have_higher_than_11mbit)
238 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
240 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
243 if (local->ops->conf_tx) {
244 struct ieee80211_tx_queue_params qparam;
246 memset(&qparam, 0, sizeof(qparam));
250 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
251 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
256 qparam.cw_max = 1023;
259 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
260 local->ops->conf_tx(local_to_hw(local),
261 i + IEEE80211_TX_QUEUE_DATA0,
265 /* IBSS uses different parameters for Beacon sending */
269 local->ops->conf_tx(local_to_hw(local),
270 IEEE80211_TX_QUEUE_BEACON, &qparam);
275 static void ieee80211_sta_wmm_params(struct net_device *dev,
276 struct ieee80211_if_sta *ifsta,
277 u8 *wmm_param, size_t wmm_param_len)
279 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
280 struct ieee80211_tx_queue_params params;
285 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
287 count = wmm_param[6] & 0x0f;
288 if (count == ifsta->wmm_last_param_set)
290 ifsta->wmm_last_param_set = count;
293 left = wmm_param_len - 8;
295 memset(¶ms, 0, sizeof(params));
297 if (!local->ops->conf_tx)
301 for (; left >= 4; left -= 4, pos += 4) {
302 int aci = (pos[0] >> 5) & 0x03;
303 int acm = (pos[0] >> 4) & 0x01;
308 queue = IEEE80211_TX_QUEUE_DATA3;
310 local->wmm_acm |= BIT(0) | BIT(3);
314 queue = IEEE80211_TX_QUEUE_DATA1;
316 local->wmm_acm |= BIT(4) | BIT(5);
320 queue = IEEE80211_TX_QUEUE_DATA0;
322 local->wmm_acm |= BIT(6) | BIT(7);
327 queue = IEEE80211_TX_QUEUE_DATA2;
329 local->wmm_acm |= BIT(1) | BIT(2);
334 params.aifs = pos[0] & 0x0f;
335 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
336 params.cw_min = ecw2cw(pos[1] & 0x0f);
337 params.txop = pos[2] | (pos[3] << 8);
338 #ifdef CONFIG_MAC80211_DEBUG
339 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
340 "cWmin=%d cWmax=%d txop=%d\n",
341 dev->name, queue, aci, acm, params.aifs, params.cw_min,
342 params.cw_max, params.txop);
344 /* TODO: handle ACM (block TX, fallback to next lowest allowed
346 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
347 printk(KERN_DEBUG "%s: failed to set TX queue "
348 "parameters for queue %d\n", dev->name, queue);
353 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
355 bool use_short_preamble)
357 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
358 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
359 DECLARE_MAC_BUF(mac);
362 if (use_protection != bss_conf->use_cts_prot) {
363 if (net_ratelimit()) {
364 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
367 use_protection ? "enabled" : "disabled",
368 print_mac(mac, ifsta->bssid));
370 bss_conf->use_cts_prot = use_protection;
371 changed |= BSS_CHANGED_ERP_CTS_PROT;
374 if (use_short_preamble != bss_conf->use_short_preamble) {
375 if (net_ratelimit()) {
376 printk(KERN_DEBUG "%s: switched to %s barker preamble"
379 use_short_preamble ? "short" : "long",
380 print_mac(mac, ifsta->bssid));
382 bss_conf->use_short_preamble = use_short_preamble;
383 changed |= BSS_CHANGED_ERP_PREAMBLE;
389 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
392 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
393 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
395 return ieee80211_handle_protect_preamb(sdata,
396 use_protection, use_short_preamble);
399 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
400 struct ieee80211_sta_bss *bss)
404 if (bss->has_erp_value)
405 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
407 u16 capab = bss->capability;
408 changed |= ieee80211_handle_protect_preamb(sdata, false,
409 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
415 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
416 struct ieee80211_ht_info *ht_info)
422 memset(ht_info, 0, sizeof(*ht_info));
425 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
427 ht_info->ht_supported = 1;
428 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
429 ht_info->ampdu_factor =
430 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
431 ht_info->ampdu_density =
432 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
433 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
435 ht_info->ht_supported = 0;
440 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
441 struct ieee80211_ht_addt_info *ht_add_info_ie,
442 struct ieee80211_ht_bss_info *bss_info)
444 if (bss_info == NULL)
447 memset(bss_info, 0, sizeof(*bss_info));
449 if (ht_add_info_ie) {
451 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
453 bss_info->primary_channel = ht_add_info_ie->control_chan;
454 bss_info->bss_cap = ht_add_info_ie->ht_param;
455 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
461 static void ieee80211_sta_send_associnfo(struct net_device *dev,
462 struct ieee80211_if_sta *ifsta)
467 union iwreq_data wrqu;
469 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
472 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
473 ifsta->assocresp_ies_len), GFP_KERNEL);
477 len = sprintf(buf, "ASSOCINFO(");
478 if (ifsta->assocreq_ies) {
479 len += sprintf(buf + len, "ReqIEs=");
480 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
481 len += sprintf(buf + len, "%02x",
482 ifsta->assocreq_ies[i]);
485 if (ifsta->assocresp_ies) {
486 if (ifsta->assocreq_ies)
487 len += sprintf(buf + len, " ");
488 len += sprintf(buf + len, "RespIEs=");
489 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
490 len += sprintf(buf + len, "%02x",
491 ifsta->assocresp_ies[i]);
494 len += sprintf(buf + len, ")");
496 if (len > IW_CUSTOM_MAX) {
497 len = sprintf(buf, "ASSOCRESPIE=");
498 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
499 len += sprintf(buf + len, "%02x",
500 ifsta->assocresp_ies[i]);
504 memset(&wrqu, 0, sizeof(wrqu));
505 wrqu.data.length = len;
506 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
512 static void ieee80211_set_associated(struct net_device *dev,
513 struct ieee80211_if_sta *ifsta,
516 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
517 struct ieee80211_local *local = sdata->local;
518 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
519 union iwreq_data wrqu;
520 u32 changed = BSS_CHANGED_ASSOC;
523 struct ieee80211_sta_bss *bss;
525 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
527 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
530 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
531 conf->channel->center_freq,
532 ifsta->ssid, ifsta->ssid_len);
534 /* set timing information */
535 sdata->bss_conf.beacon_int = bss->beacon_int;
536 sdata->bss_conf.timestamp = bss->timestamp;
538 changed |= ieee80211_handle_bss_capability(sdata, bss);
540 ieee80211_rx_bss_put(dev, bss);
543 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
544 changed |= BSS_CHANGED_HT;
545 sdata->bss_conf.assoc_ht = 1;
546 sdata->bss_conf.ht_conf = &conf->ht_conf;
547 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
550 netif_carrier_on(dev);
551 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
552 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
553 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
554 ieee80211_sta_send_associnfo(dev, ifsta);
556 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
557 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
558 netif_carrier_off(dev);
559 ieee80211_reset_erp_info(dev);
561 sdata->bss_conf.assoc_ht = 0;
562 sdata->bss_conf.ht_conf = NULL;
563 sdata->bss_conf.ht_bss_conf = NULL;
565 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
567 ifsta->last_probe = jiffies;
568 ieee80211_led_assoc(local, assoc);
570 sdata->bss_conf.assoc = assoc;
571 ieee80211_bss_info_change_notify(sdata, changed);
572 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
573 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
576 static void ieee80211_set_disassoc(struct net_device *dev,
577 struct ieee80211_if_sta *ifsta, int deauth)
580 ifsta->auth_tries = 0;
581 ifsta->assoc_tries = 0;
582 ieee80211_set_associated(dev, ifsta, 0);
585 void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
588 struct ieee80211_sub_if_data *sdata;
589 struct ieee80211_tx_packet_data *pkt_data;
591 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
592 skb->dev = sdata->local->mdev;
593 skb_set_mac_header(skb, 0);
594 skb_set_network_header(skb, 0);
595 skb_set_transport_header(skb, 0);
597 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
598 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
599 pkt_data->ifindex = sdata->dev->ifindex;
601 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
607 static void ieee80211_send_auth(struct net_device *dev,
608 struct ieee80211_if_sta *ifsta,
609 int transaction, u8 *extra, size_t extra_len,
612 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
614 struct ieee80211_mgmt *mgmt;
616 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
617 sizeof(*mgmt) + 6 + extra_len);
619 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
620 "frame\n", dev->name);
623 skb_reserve(skb, local->hw.extra_tx_headroom);
625 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
626 memset(mgmt, 0, 24 + 6);
627 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
628 IEEE80211_STYPE_AUTH);
630 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
631 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
632 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
633 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
634 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
635 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
636 ifsta->auth_transaction = transaction + 1;
637 mgmt->u.auth.status_code = cpu_to_le16(0);
639 memcpy(skb_put(skb, extra_len), extra, extra_len);
641 ieee80211_sta_tx(dev, skb, encrypt);
645 static void ieee80211_authenticate(struct net_device *dev,
646 struct ieee80211_if_sta *ifsta)
648 DECLARE_MAC_BUF(mac);
651 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
652 printk(KERN_DEBUG "%s: authentication with AP %s"
654 dev->name, print_mac(mac, ifsta->bssid));
655 ifsta->state = IEEE80211_DISABLED;
659 ifsta->state = IEEE80211_AUTHENTICATE;
660 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
661 dev->name, print_mac(mac, ifsta->bssid));
663 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
665 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
668 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
669 struct ieee80211_supported_band *sband,
675 for (i = 0; i < bss->supp_rates_len; i++) {
676 int rate = (bss->supp_rates[i] & 0x7F) * 5;
678 for (j = 0; j < sband->n_bitrates; j++)
679 if (sband->bitrates[j].bitrate == rate) {
689 static void ieee80211_send_assoc(struct net_device *dev,
690 struct ieee80211_if_sta *ifsta)
692 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
694 struct ieee80211_mgmt *mgmt;
696 int i, len, count, rates_len, supp_rates_len;
698 struct ieee80211_sta_bss *bss;
700 struct ieee80211_supported_band *sband;
703 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
704 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
707 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
708 "frame\n", dev->name);
711 skb_reserve(skb, local->hw.extra_tx_headroom);
713 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
715 capab = ifsta->capab;
717 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
718 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
719 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
720 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
721 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
724 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
725 local->hw.conf.channel->center_freq,
726 ifsta->ssid, ifsta->ssid_len);
728 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
729 capab |= WLAN_CAPABILITY_PRIVACY;
734 /* get all rates supported by the device and the AP as
735 * some APs don't like getting a superset of their rates
736 * in the association request (e.g. D-Link DAP 1353 in
738 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
740 ieee80211_rx_bss_put(dev, bss);
743 rates_len = sband->n_bitrates;
746 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
748 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
749 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
750 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
752 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
754 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
755 IEEE80211_STYPE_REASSOC_REQ);
756 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
757 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
758 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
762 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
763 IEEE80211_STYPE_ASSOC_REQ);
764 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
765 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
769 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
770 *pos++ = WLAN_EID_SSID;
771 *pos++ = ifsta->ssid_len;
772 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
774 /* add all rates which were marked to be used above */
775 supp_rates_len = rates_len;
776 if (supp_rates_len > 8)
779 len = sband->n_bitrates;
780 pos = skb_put(skb, supp_rates_len + 2);
781 *pos++ = WLAN_EID_SUPP_RATES;
782 *pos++ = supp_rates_len;
785 for (i = 0; i < sband->n_bitrates; i++) {
786 if (BIT(i) & rates) {
787 int rate = sband->bitrates[i].bitrate;
788 *pos++ = (u8) (rate / 5);
795 pos = skb_put(skb, rates_len - count + 2);
796 *pos++ = WLAN_EID_EXT_SUPP_RATES;
797 *pos++ = rates_len - count;
799 for (i++; i < sband->n_bitrates; i++) {
800 if (BIT(i) & rates) {
801 int rate = sband->bitrates[i].bitrate;
802 *pos++ = (u8) (rate / 5);
807 if (ifsta->extra_ie) {
808 pos = skb_put(skb, ifsta->extra_ie_len);
809 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
812 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
813 pos = skb_put(skb, 9);
814 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
815 *pos++ = 7; /* len */
816 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
819 *pos++ = 2; /* WME */
820 *pos++ = 0; /* WME info */
821 *pos++ = 1; /* WME ver */
824 /* wmm support is a must to HT */
825 if (wmm && sband->ht_info.ht_supported) {
826 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
827 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
828 *pos++ = WLAN_EID_HT_CAPABILITY;
829 *pos++ = sizeof(struct ieee80211_ht_cap);
830 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
831 memcpy(pos, &tmp, sizeof(u16));
833 /* TODO: needs a define here for << 2 */
834 *pos++ = sband->ht_info.ampdu_factor |
835 (sband->ht_info.ampdu_density << 2);
836 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
839 kfree(ifsta->assocreq_ies);
840 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
841 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
842 if (ifsta->assocreq_ies)
843 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
845 ieee80211_sta_tx(dev, skb, 0);
849 static void ieee80211_send_deauth(struct net_device *dev,
850 struct ieee80211_if_sta *ifsta, u16 reason)
852 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
854 struct ieee80211_mgmt *mgmt;
856 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
858 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
859 "frame\n", dev->name);
862 skb_reserve(skb, local->hw.extra_tx_headroom);
864 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
866 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
867 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
868 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
869 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
870 IEEE80211_STYPE_DEAUTH);
872 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
874 ieee80211_sta_tx(dev, skb, 0);
878 static void ieee80211_send_disassoc(struct net_device *dev,
879 struct ieee80211_if_sta *ifsta, u16 reason)
881 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
883 struct ieee80211_mgmt *mgmt;
885 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
887 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
888 "frame\n", dev->name);
891 skb_reserve(skb, local->hw.extra_tx_headroom);
893 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
895 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
896 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
897 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
898 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
899 IEEE80211_STYPE_DISASSOC);
901 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
903 ieee80211_sta_tx(dev, skb, 0);
907 static int ieee80211_privacy_mismatch(struct net_device *dev,
908 struct ieee80211_if_sta *ifsta)
910 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
911 struct ieee80211_sta_bss *bss;
916 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
919 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
920 local->hw.conf.channel->center_freq,
921 ifsta->ssid, ifsta->ssid_len);
925 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
926 wep_privacy = !!ieee80211_sta_wep_configured(dev);
927 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
929 ieee80211_rx_bss_put(dev, bss);
931 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
938 static void ieee80211_associate(struct net_device *dev,
939 struct ieee80211_if_sta *ifsta)
941 DECLARE_MAC_BUF(mac);
943 ifsta->assoc_tries++;
944 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
945 printk(KERN_DEBUG "%s: association with AP %s"
947 dev->name, print_mac(mac, ifsta->bssid));
948 ifsta->state = IEEE80211_DISABLED;
952 ifsta->state = IEEE80211_ASSOCIATE;
953 printk(KERN_DEBUG "%s: associate with AP %s\n",
954 dev->name, print_mac(mac, ifsta->bssid));
955 if (ieee80211_privacy_mismatch(dev, ifsta)) {
956 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
957 "mixed-cell disabled - abort association\n", dev->name);
958 ifsta->state = IEEE80211_DISABLED;
962 ieee80211_send_assoc(dev, ifsta);
964 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
968 static void ieee80211_associated(struct net_device *dev,
969 struct ieee80211_if_sta *ifsta)
971 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
972 struct sta_info *sta;
974 DECLARE_MAC_BUF(mac);
976 /* TODO: start monitoring current AP signal quality and number of
977 * missed beacons. Scan other channels every now and then and search
979 /* TODO: remove expired BSSes */
981 ifsta->state = IEEE80211_ASSOCIATED;
985 sta = sta_info_get(local, ifsta->bssid);
987 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
988 dev->name, print_mac(mac, ifsta->bssid));
992 if (time_after(jiffies,
993 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
994 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
995 printk(KERN_DEBUG "%s: No ProbeResp from "
996 "current AP %s - assume out of "
998 dev->name, print_mac(mac, ifsta->bssid));
1000 sta_info_unlink(&sta);
1002 ieee80211_send_probe_req(dev, ifsta->bssid,
1004 local->scan_ssid_len);
1005 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1007 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1008 if (time_after(jiffies, ifsta->last_probe +
1009 IEEE80211_PROBE_INTERVAL)) {
1010 ifsta->last_probe = jiffies;
1011 ieee80211_send_probe_req(dev, ifsta->bssid,
1020 if (disassoc && sta)
1021 sta_info_destroy(sta);
1024 ifsta->state = IEEE80211_DISABLED;
1025 ieee80211_set_associated(dev, ifsta, 0);
1027 mod_timer(&ifsta->timer, jiffies +
1028 IEEE80211_MONITORING_INTERVAL);
1033 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1034 u8 *ssid, size_t ssid_len)
1036 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1037 struct ieee80211_supported_band *sband;
1038 struct sk_buff *skb;
1039 struct ieee80211_mgmt *mgmt;
1040 u8 *pos, *supp_rates, *esupp_rates = NULL;
1043 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1045 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1046 "request\n", dev->name);
1049 skb_reserve(skb, local->hw.extra_tx_headroom);
1051 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1052 memset(mgmt, 0, 24);
1053 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1054 IEEE80211_STYPE_PROBE_REQ);
1055 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1057 memcpy(mgmt->da, dst, ETH_ALEN);
1058 memcpy(mgmt->bssid, dst, ETH_ALEN);
1060 memset(mgmt->da, 0xff, ETH_ALEN);
1061 memset(mgmt->bssid, 0xff, ETH_ALEN);
1063 pos = skb_put(skb, 2 + ssid_len);
1064 *pos++ = WLAN_EID_SSID;
1066 memcpy(pos, ssid, ssid_len);
1068 supp_rates = skb_put(skb, 2);
1069 supp_rates[0] = WLAN_EID_SUPP_RATES;
1071 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1073 for (i = 0; i < sband->n_bitrates; i++) {
1074 struct ieee80211_rate *rate = &sband->bitrates[i];
1076 pos = skb_put(skb, 1);
1078 } else if (supp_rates[1] == 8) {
1079 esupp_rates = skb_put(skb, 3);
1080 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1082 pos = &esupp_rates[2];
1084 pos = skb_put(skb, 1);
1087 *pos = rate->bitrate / 5;
1090 ieee80211_sta_tx(dev, skb, 0);
1094 static int ieee80211_sta_wep_configured(struct net_device *dev)
1096 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1097 if (!sdata || !sdata->default_key ||
1098 sdata->default_key->conf.alg != ALG_WEP)
1104 static void ieee80211_auth_completed(struct net_device *dev,
1105 struct ieee80211_if_sta *ifsta)
1107 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
1108 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1109 ieee80211_associate(dev, ifsta);
1113 static void ieee80211_auth_challenge(struct net_device *dev,
1114 struct ieee80211_if_sta *ifsta,
1115 struct ieee80211_mgmt *mgmt,
1119 struct ieee802_11_elems elems;
1121 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1122 pos = mgmt->u.auth.variable;
1123 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1124 if (!elems.challenge) {
1125 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1126 "frame\n", dev->name);
1129 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1130 elems.challenge_len + 2, 1);
1133 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1134 u8 dialog_token, u16 status, u16 policy,
1135 u16 buf_size, u16 timeout)
1137 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1138 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1139 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1140 struct sk_buff *skb;
1141 struct ieee80211_mgmt *mgmt;
1144 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1145 sizeof(mgmt->u.action.u.addba_resp));
1147 printk(KERN_DEBUG "%s: failed to allocate buffer "
1148 "for addba resp frame\n", dev->name);
1152 skb_reserve(skb, local->hw.extra_tx_headroom);
1153 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1154 memset(mgmt, 0, 24);
1155 memcpy(mgmt->da, da, ETH_ALEN);
1156 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1157 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1158 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1160 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1161 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1162 IEEE80211_STYPE_ACTION);
1164 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1165 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1166 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1167 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1169 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1170 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1171 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1173 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1174 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1175 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1177 ieee80211_sta_tx(dev, skb, 0);
1182 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1183 u16 tid, u8 dialog_token, u16 start_seq_num,
1184 u16 agg_size, u16 timeout)
1186 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1187 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1188 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1189 struct sk_buff *skb;
1190 struct ieee80211_mgmt *mgmt;
1193 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1194 sizeof(mgmt->u.action.u.addba_req));
1198 printk(KERN_ERR "%s: failed to allocate buffer "
1199 "for addba request frame\n", dev->name);
1202 skb_reserve(skb, local->hw.extra_tx_headroom);
1203 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1204 memset(mgmt, 0, 24);
1205 memcpy(mgmt->da, da, ETH_ALEN);
1206 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1207 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1208 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1210 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1212 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1213 IEEE80211_STYPE_ACTION);
1215 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1217 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1218 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1220 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1221 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1222 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1223 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1225 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1227 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1228 mgmt->u.action.u.addba_req.start_seq_num =
1229 cpu_to_le16(start_seq_num << 4);
1231 ieee80211_sta_tx(dev, skb, 0);
1234 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1235 struct ieee80211_mgmt *mgmt,
1238 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1239 struct ieee80211_hw *hw = &local->hw;
1240 struct ieee80211_conf *conf = &hw->conf;
1241 struct sta_info *sta;
1242 struct tid_ampdu_rx *tid_agg_rx;
1243 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1245 int ret = -EOPNOTSUPP;
1246 DECLARE_MAC_BUF(mac);
1250 sta = sta_info_get(local, mgmt->sa);
1256 /* extract session parameters from addba request frame */
1257 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1258 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1260 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1262 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1263 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1264 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1265 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1267 status = WLAN_STATUS_REQUEST_DECLINED;
1269 /* sanity check for incoming parameters:
1270 * check if configuration can support the BA policy
1271 * and if buffer size does not exceeds max value */
1272 if (((ba_policy != 1)
1273 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1274 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1275 status = WLAN_STATUS_INVALID_QOS_PARAM;
1276 #ifdef CONFIG_MAC80211_HT_DEBUG
1277 if (net_ratelimit())
1278 printk(KERN_DEBUG "AddBA Req with bad params from "
1279 "%s on tid %u. policy %d, buffer size %d\n",
1280 print_mac(mac, mgmt->sa), tid, ba_policy,
1282 #endif /* CONFIG_MAC80211_HT_DEBUG */
1285 /* determine default buffer size */
1286 if (buf_size == 0) {
1287 struct ieee80211_supported_band *sband;
1289 sband = local->hw.wiphy->bands[conf->channel->band];
1290 buf_size = IEEE80211_MIN_AMPDU_BUF;
1291 buf_size = buf_size << sband->ht_info.ampdu_factor;
1295 /* examine state machine */
1296 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1298 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1299 #ifdef CONFIG_MAC80211_HT_DEBUG
1300 if (net_ratelimit())
1301 printk(KERN_DEBUG "unexpected AddBA Req from "
1303 print_mac(mac, mgmt->sa), tid);
1304 #endif /* CONFIG_MAC80211_HT_DEBUG */
1308 /* prepare A-MPDU MLME for Rx aggregation */
1309 sta->ampdu_mlme.tid_rx[tid] =
1310 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1311 if (!sta->ampdu_mlme.tid_rx[tid]) {
1312 if (net_ratelimit())
1313 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1318 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1319 sta_rx_agg_session_timer_expired;
1320 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1321 (unsigned long)&sta->timer_to_tid[tid];
1322 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1324 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1326 /* prepare reordering buffer */
1327 tid_agg_rx->reorder_buf =
1328 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1329 if (!tid_agg_rx->reorder_buf) {
1330 if (net_ratelimit())
1331 printk(KERN_ERR "can not allocate reordering buffer "
1332 "to tid %d\n", tid);
1333 kfree(sta->ampdu_mlme.tid_rx[tid]);
1336 memset(tid_agg_rx->reorder_buf, 0,
1337 buf_size * sizeof(struct sk_buff *));
1339 if (local->ops->ampdu_action)
1340 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1341 sta->addr, tid, &start_seq_num);
1342 #ifdef CONFIG_MAC80211_HT_DEBUG
1343 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1344 #endif /* CONFIG_MAC80211_HT_DEBUG */
1347 kfree(tid_agg_rx->reorder_buf);
1349 sta->ampdu_mlme.tid_rx[tid] = NULL;
1353 /* change state and send addba resp */
1354 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1355 tid_agg_rx->dialog_token = dialog_token;
1356 tid_agg_rx->ssn = start_seq_num;
1357 tid_agg_rx->head_seq_num = start_seq_num;
1358 tid_agg_rx->buf_size = buf_size;
1359 tid_agg_rx->timeout = timeout;
1360 tid_agg_rx->stored_mpdu_num = 0;
1361 status = WLAN_STATUS_SUCCESS;
1363 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1366 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1367 dialog_token, status, 1, buf_size, timeout);
1371 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1372 struct ieee80211_mgmt *mgmt,
1375 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1376 struct ieee80211_hw *hw = &local->hw;
1377 struct sta_info *sta;
1384 sta = sta_info_get(local, mgmt->sa);
1390 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1391 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1393 state = &sta->ampdu_mlme.tid_state_tx[tid];
1395 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1397 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1398 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1399 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1401 goto addba_resp_exit;
1404 if (mgmt->u.action.u.addba_resp.dialog_token !=
1405 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1406 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1407 #ifdef CONFIG_MAC80211_HT_DEBUG
1408 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1409 #endif /* CONFIG_MAC80211_HT_DEBUG */
1410 goto addba_resp_exit;
1413 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1414 #ifdef CONFIG_MAC80211_HT_DEBUG
1415 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1416 #endif /* CONFIG_MAC80211_HT_DEBUG */
1417 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1418 == WLAN_STATUS_SUCCESS) {
1419 if (*state & HT_ADDBA_RECEIVED_MSK)
1420 printk(KERN_DEBUG "double addBA response\n");
1422 *state |= HT_ADDBA_RECEIVED_MSK;
1423 sta->ampdu_mlme.addba_req_num[tid] = 0;
1425 if (*state == HT_AGG_STATE_OPERATIONAL) {
1426 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1427 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1430 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1431 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1433 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1435 sta->ampdu_mlme.addba_req_num[tid]++;
1436 /* this will allow the state check in stop_BA_session */
1437 *state = HT_AGG_STATE_OPERATIONAL;
1438 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1439 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1440 WLAN_BACK_INITIATOR);
1447 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1448 u16 initiator, u16 reason_code)
1450 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1451 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1452 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1453 struct sk_buff *skb;
1454 struct ieee80211_mgmt *mgmt;
1457 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1458 sizeof(mgmt->u.action.u.delba));
1461 printk(KERN_ERR "%s: failed to allocate buffer "
1462 "for delba frame\n", dev->name);
1466 skb_reserve(skb, local->hw.extra_tx_headroom);
1467 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1468 memset(mgmt, 0, 24);
1469 memcpy(mgmt->da, da, ETH_ALEN);
1470 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1471 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1472 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1474 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1475 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1476 IEEE80211_STYPE_ACTION);
1478 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1480 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1481 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1482 params = (u16)(initiator << 11); /* bit 11 initiator */
1483 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1485 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1486 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1488 ieee80211_sta_tx(dev, skb, 0);
1491 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1492 u16 initiator, u16 reason)
1494 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1495 struct ieee80211_hw *hw = &local->hw;
1496 struct sta_info *sta;
1498 DECLARE_MAC_BUF(mac);
1502 sta = sta_info_get(local, ra);
1508 /* check if TID is in operational state */
1509 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1510 if (sta->ampdu_mlme.tid_state_rx[tid]
1511 != HT_AGG_STATE_OPERATIONAL) {
1512 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1516 sta->ampdu_mlme.tid_state_rx[tid] =
1517 HT_AGG_STATE_REQ_STOP_BA_MSK |
1518 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1519 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1521 /* stop HW Rx aggregation. ampdu_action existence
1522 * already verified in session init so we add the BUG_ON */
1523 BUG_ON(!local->ops->ampdu_action);
1525 #ifdef CONFIG_MAC80211_HT_DEBUG
1526 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1527 print_mac(mac, ra), tid);
1528 #endif /* CONFIG_MAC80211_HT_DEBUG */
1530 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1533 printk(KERN_DEBUG "HW problem - can not stop rx "
1534 "aggergation for tid %d\n", tid);
1536 /* shutdown timer has not expired */
1537 if (initiator != WLAN_BACK_TIMER)
1538 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1540 /* check if this is a self generated aggregation halt */
1541 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1542 ieee80211_send_delba(dev, ra, tid, 0, reason);
1544 /* free the reordering buffer */
1545 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1546 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1547 /* release the reordered frames */
1548 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1549 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1550 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1553 /* free resources */
1554 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1555 kfree(sta->ampdu_mlme.tid_rx[tid]);
1556 sta->ampdu_mlme.tid_rx[tid] = NULL;
1557 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1563 static void ieee80211_sta_process_delba(struct net_device *dev,
1564 struct ieee80211_mgmt *mgmt, size_t len)
1566 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1567 struct sta_info *sta;
1570 DECLARE_MAC_BUF(mac);
1574 sta = sta_info_get(local, mgmt->sa);
1580 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1581 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1582 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1584 #ifdef CONFIG_MAC80211_HT_DEBUG
1585 if (net_ratelimit())
1586 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1587 print_mac(mac, mgmt->sa),
1588 initiator ? "initiator" : "recipient", tid,
1589 mgmt->u.action.u.delba.reason_code);
1590 #endif /* CONFIG_MAC80211_HT_DEBUG */
1592 if (initiator == WLAN_BACK_INITIATOR)
1593 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1594 WLAN_BACK_INITIATOR, 0);
1595 else { /* WLAN_BACK_RECIPIENT */
1596 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1597 sta->ampdu_mlme.tid_state_tx[tid] =
1598 HT_AGG_STATE_OPERATIONAL;
1599 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1600 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1601 WLAN_BACK_RECIPIENT);
1607 * After sending add Block Ack request we activated a timer until
1608 * add Block Ack response will arrive from the recipient.
1609 * If this timer expires sta_addba_resp_timer_expired will be executed.
1611 void sta_addba_resp_timer_expired(unsigned long data)
1613 /* not an elegant detour, but there is no choice as the timer passes
1614 * only one argument, and both sta_info and TID are needed, so init
1615 * flow in sta_info_create gives the TID as data, while the timer_to_id
1616 * array gives the sta through container_of */
1617 u16 tid = *(u8 *)data;
1618 struct sta_info *temp_sta = container_of((void *)data,
1619 struct sta_info, timer_to_tid[tid]);
1621 struct ieee80211_local *local = temp_sta->local;
1622 struct ieee80211_hw *hw = &local->hw;
1623 struct sta_info *sta;
1628 sta = sta_info_get(local, temp_sta->addr);
1634 state = &sta->ampdu_mlme.tid_state_tx[tid];
1635 /* check if the TID waits for addBA response */
1636 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1637 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1638 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1639 *state = HT_AGG_STATE_IDLE;
1640 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1641 "expecting addBA response there", tid);
1642 goto timer_expired_exit;
1645 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1647 /* go through the state check in stop_BA_session */
1648 *state = HT_AGG_STATE_OPERATIONAL;
1649 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1650 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1651 WLAN_BACK_INITIATOR);
1658 * After accepting the AddBA Request we activated a timer,
1659 * resetting it after each frame that arrives from the originator.
1660 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1662 void sta_rx_agg_session_timer_expired(unsigned long data)
1664 /* not an elegant detour, but there is no choice as the timer passes
1665 * only one argument, and various sta_info are needed here, so init
1666 * flow in sta_info_create gives the TID as data, while the timer_to_id
1667 * array gives the sta through container_of */
1668 u8 *ptid = (u8 *)data;
1669 u8 *timer_to_id = ptid - *ptid;
1670 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1673 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1674 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1675 (u16)*ptid, WLAN_BACK_TIMER,
1676 WLAN_REASON_QSTA_TIMEOUT);
1679 void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1681 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1684 for (i = 0; i < STA_TID_NUM; i++) {
1685 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1686 WLAN_BACK_INITIATOR);
1687 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1688 WLAN_BACK_RECIPIENT,
1689 WLAN_REASON_QSTA_LEAVE_QBSS);
1693 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1694 struct ieee80211_if_sta *ifsta,
1695 struct ieee80211_mgmt *mgmt,
1698 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1699 u16 auth_alg, auth_transaction, status_code;
1700 DECLARE_MAC_BUF(mac);
1702 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1703 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
1704 printk(KERN_DEBUG "%s: authentication frame received from "
1705 "%s, but not in authenticate state - ignored\n",
1706 dev->name, print_mac(mac, mgmt->sa));
1711 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
1712 "received from %s - ignored\n",
1713 dev->name, len, print_mac(mac, mgmt->sa));
1717 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1718 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1719 printk(KERN_DEBUG "%s: authentication frame received from "
1720 "unknown AP (SA=%s BSSID=%s) - "
1721 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1722 print_mac(mac, mgmt->bssid));
1726 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1727 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1728 printk(KERN_DEBUG "%s: authentication frame received from "
1729 "unknown BSSID (SA=%s BSSID=%s) - "
1730 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1731 print_mac(mac, mgmt->bssid));
1735 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1736 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1737 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1739 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
1740 "transaction=%d status=%d)\n",
1741 dev->name, print_mac(mac, mgmt->sa), auth_alg,
1742 auth_transaction, status_code);
1744 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1745 /* IEEE 802.11 standard does not require authentication in IBSS
1746 * networks and most implementations do not seem to use it.
1747 * However, try to reply to authentication attempts if someone
1748 * has actually implemented this.
1749 * TODO: Could implement shared key authentication. */
1750 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1751 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1752 "frame (alg=%d transaction=%d)\n",
1753 dev->name, auth_alg, auth_transaction);
1756 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1759 if (auth_alg != ifsta->auth_alg ||
1760 auth_transaction != ifsta->auth_transaction) {
1761 printk(KERN_DEBUG "%s: unexpected authentication frame "
1762 "(alg=%d transaction=%d)\n",
1763 dev->name, auth_alg, auth_transaction);
1767 if (status_code != WLAN_STATUS_SUCCESS) {
1768 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1769 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1770 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1772 const int num_algs = ARRAY_SIZE(algs);
1774 algs[0] = algs[1] = algs[2] = 0xff;
1775 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1776 algs[0] = WLAN_AUTH_OPEN;
1777 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1778 algs[1] = WLAN_AUTH_SHARED_KEY;
1779 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1780 algs[2] = WLAN_AUTH_LEAP;
1781 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1783 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1787 for (i = 0; i < num_algs; i++) {
1789 if (pos >= num_algs)
1791 if (algs[pos] == ifsta->auth_alg ||
1794 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1795 !ieee80211_sta_wep_configured(dev))
1797 ifsta->auth_alg = algs[pos];
1798 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1800 dev->name, ifsta->auth_alg);
1807 switch (ifsta->auth_alg) {
1808 case WLAN_AUTH_OPEN:
1809 case WLAN_AUTH_LEAP:
1810 ieee80211_auth_completed(dev, ifsta);
1812 case WLAN_AUTH_SHARED_KEY:
1813 if (ifsta->auth_transaction == 4)
1814 ieee80211_auth_completed(dev, ifsta);
1816 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1822 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1823 struct ieee80211_if_sta *ifsta,
1824 struct ieee80211_mgmt *mgmt,
1828 DECLARE_MAC_BUF(mac);
1831 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1832 "received from %s - ignored\n",
1833 dev->name, len, print_mac(mac, mgmt->sa));
1837 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1838 printk(KERN_DEBUG "%s: deauthentication frame received from "
1839 "unknown AP (SA=%s BSSID=%s) - "
1840 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1841 print_mac(mac, mgmt->bssid));
1845 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1847 printk(KERN_DEBUG "%s: RX deauthentication from %s"
1849 dev->name, print_mac(mac, mgmt->sa), reason_code);
1851 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1852 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1855 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1856 ifsta->state == IEEE80211_ASSOCIATE ||
1857 ifsta->state == IEEE80211_ASSOCIATED) {
1858 ifsta->state = IEEE80211_AUTHENTICATE;
1859 mod_timer(&ifsta->timer, jiffies +
1860 IEEE80211_RETRY_AUTH_INTERVAL);
1863 ieee80211_set_disassoc(dev, ifsta, 1);
1864 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1868 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1869 struct ieee80211_if_sta *ifsta,
1870 struct ieee80211_mgmt *mgmt,
1874 DECLARE_MAC_BUF(mac);
1877 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1878 "received from %s - ignored\n",
1879 dev->name, len, print_mac(mac, mgmt->sa));
1883 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1884 printk(KERN_DEBUG "%s: disassociation frame received from "
1885 "unknown AP (SA=%s BSSID=%s) - "
1886 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1887 print_mac(mac, mgmt->bssid));
1891 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1893 printk(KERN_DEBUG "%s: RX disassociation from %s"
1895 dev->name, print_mac(mac, mgmt->sa), reason_code);
1897 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1898 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1900 if (ifsta->state == IEEE80211_ASSOCIATED) {
1901 ifsta->state = IEEE80211_ASSOCIATE;
1902 mod_timer(&ifsta->timer, jiffies +
1903 IEEE80211_RETRY_AUTH_INTERVAL);
1906 ieee80211_set_disassoc(dev, ifsta, 0);
1910 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1911 struct ieee80211_if_sta *ifsta,
1912 struct ieee80211_mgmt *mgmt,
1916 struct ieee80211_local *local = sdata->local;
1917 struct net_device *dev = sdata->dev;
1918 struct ieee80211_supported_band *sband;
1919 struct sta_info *sta;
1920 u64 rates, basic_rates;
1921 u16 capab_info, status_code, aid;
1922 struct ieee802_11_elems elems;
1923 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1926 DECLARE_MAC_BUF(mac);
1927 bool have_higher_than_11mbit = false;
1929 /* AssocResp and ReassocResp have identical structure, so process both
1930 * of them in this function. */
1932 if (ifsta->state != IEEE80211_ASSOCIATE) {
1933 printk(KERN_DEBUG "%s: association frame received from "
1934 "%s, but not in associate state - ignored\n",
1935 dev->name, print_mac(mac, mgmt->sa));
1940 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1941 "received from %s - ignored\n",
1942 dev->name, len, print_mac(mac, mgmt->sa));
1946 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1947 printk(KERN_DEBUG "%s: association frame received from "
1948 "unknown AP (SA=%s BSSID=%s) - "
1949 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1950 print_mac(mac, mgmt->bssid));
1954 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1955 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1956 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1958 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1959 "status=%d aid=%d)\n",
1960 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1961 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1963 if (status_code != WLAN_STATUS_SUCCESS) {
1964 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1965 dev->name, status_code);
1966 /* if this was a reassociation, ensure we try a "full"
1967 * association next time. This works around some broken APs
1968 * which do not correctly reject reassociation requests. */
1969 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1973 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1974 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1975 "set\n", dev->name, aid);
1976 aid &= ~(BIT(15) | BIT(14));
1978 pos = mgmt->u.assoc_resp.variable;
1979 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1981 if (!elems.supp_rates) {
1982 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1987 printk(KERN_DEBUG "%s: associated\n", dev->name);
1989 ifsta->ap_capab = capab_info;
1991 kfree(ifsta->assocresp_ies);
1992 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1993 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1994 if (ifsta->assocresp_ies)
1995 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1999 /* Add STA entry for the AP */
2000 sta = sta_info_get(local, ifsta->bssid);
2002 struct ieee80211_sta_bss *bss;
2005 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2007 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2008 " the AP\n", dev->name);
2012 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
2013 local->hw.conf.channel->center_freq,
2014 ifsta->ssid, ifsta->ssid_len);
2016 sta->last_rssi = bss->rssi;
2017 sta->last_signal = bss->signal;
2018 sta->last_noise = bss->noise;
2019 ieee80211_rx_bss_put(dev, bss);
2022 err = sta_info_insert(sta);
2024 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2025 " the AP (error %d)\n", dev->name, err);
2032 * FIXME: Do we really need to update the sta_info's information here?
2033 * We already know about the AP (we found it in our list) so it
2034 * should already be filled with the right info, no?
2035 * As is stands, all this is racy because typically we assume
2036 * the information that is filled in here (except flags) doesn't
2037 * change while a STA structure is alive. As such, it should move
2038 * to between the sta_info_alloc() and sta_info_insert() above.
2041 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2042 WLAN_STA_AUTHORIZED;
2046 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2048 for (i = 0; i < elems.supp_rates_len; i++) {
2049 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2052 have_higher_than_11mbit = true;
2054 for (j = 0; j < sband->n_bitrates; j++) {
2055 if (sband->bitrates[j].bitrate == rate)
2057 if (elems.supp_rates[i] & 0x80)
2058 basic_rates |= BIT(j);
2062 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2063 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2066 have_higher_than_11mbit = true;
2068 for (j = 0; j < sband->n_bitrates; j++) {
2069 if (sband->bitrates[j].bitrate == rate)
2071 if (elems.ext_supp_rates[i] & 0x80)
2072 basic_rates |= BIT(j);
2076 sta->supp_rates[local->hw.conf.channel->band] = rates;
2077 sdata->basic_rates = basic_rates;
2079 /* cf. IEEE 802.11 9.2.12 */
2080 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2081 have_higher_than_11mbit)
2082 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2084 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2086 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
2087 struct ieee80211_ht_bss_info bss_info;
2088 ieee80211_ht_cap_ie_to_ht_info(
2089 (struct ieee80211_ht_cap *)
2090 elems.ht_cap_elem, &sta->ht_info);
2091 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2092 (struct ieee80211_ht_addt_info *)
2093 elems.ht_info_elem, &bss_info);
2094 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2097 rate_control_rate_init(sta, local);
2099 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2100 sta->flags |= WLAN_STA_WME;
2102 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2103 elems.wmm_param_len);
2107 /* set AID and assoc capability,
2108 * ieee80211_set_associated() will tell the driver */
2109 bss_conf->aid = aid;
2110 bss_conf->assoc_capability = capab_info;
2111 ieee80211_set_associated(dev, ifsta, 1);
2113 ieee80211_associated(dev, ifsta);
2117 /* Caller must hold local->sta_bss_lock */
2118 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2119 struct ieee80211_sta_bss *bss)
2121 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2124 if (bss_mesh_cfg(bss))
2125 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2126 bss_mesh_id_len(bss));
2128 hash_idx = STA_HASH(bss->bssid);
2130 bss->hnext = local->sta_bss_hash[hash_idx];
2131 local->sta_bss_hash[hash_idx] = bss;
2135 /* Caller must hold local->sta_bss_lock */
2136 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2137 struct ieee80211_sta_bss *bss)
2139 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2140 struct ieee80211_sta_bss *b, *prev = NULL;
2141 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2145 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2148 prev->hnext = bss->hnext;
2157 static struct ieee80211_sta_bss *
2158 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
2159 u8 *ssid, u8 ssid_len)
2161 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2162 struct ieee80211_sta_bss *bss;
2164 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2167 atomic_inc(&bss->users);
2168 atomic_inc(&bss->users);
2169 memcpy(bss->bssid, bssid, ETH_ALEN);
2171 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2172 memcpy(bss->ssid, ssid, ssid_len);
2173 bss->ssid_len = ssid_len;
2176 spin_lock_bh(&local->sta_bss_lock);
2177 /* TODO: order by RSSI? */
2178 list_add_tail(&bss->list, &local->sta_bss_list);
2179 __ieee80211_rx_bss_hash_add(dev, bss);
2180 spin_unlock_bh(&local->sta_bss_lock);
2184 static struct ieee80211_sta_bss *
2185 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
2186 u8 *ssid, u8 ssid_len)
2188 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2189 struct ieee80211_sta_bss *bss;
2191 spin_lock_bh(&local->sta_bss_lock);
2192 bss = local->sta_bss_hash[STA_HASH(bssid)];
2194 if (!bss_mesh_cfg(bss) &&
2195 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2196 bss->freq == freq &&
2197 bss->ssid_len == ssid_len &&
2198 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2199 atomic_inc(&bss->users);
2204 spin_unlock_bh(&local->sta_bss_lock);
2208 #ifdef CONFIG_MAC80211_MESH
2209 static struct ieee80211_sta_bss *
2210 ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2211 u8 *mesh_cfg, int freq)
2213 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2214 struct ieee80211_sta_bss *bss;
2216 spin_lock_bh(&local->sta_bss_lock);
2217 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2219 if (bss_mesh_cfg(bss) &&
2220 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2221 bss->freq == freq &&
2222 mesh_id_len == bss->mesh_id_len &&
2223 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2225 atomic_inc(&bss->users);
2230 spin_unlock_bh(&local->sta_bss_lock);
2234 static struct ieee80211_sta_bss *
2235 ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2236 u8 *mesh_cfg, int mesh_config_len, int freq)
2238 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2239 struct ieee80211_sta_bss *bss;
2241 if (mesh_config_len != MESH_CFG_LEN)
2244 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2248 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2249 if (!bss->mesh_cfg) {
2254 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2255 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2256 if (!bss->mesh_id) {
2257 kfree(bss->mesh_cfg);
2261 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2264 atomic_inc(&bss->users);
2265 atomic_inc(&bss->users);
2266 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2267 bss->mesh_id_len = mesh_id_len;
2269 spin_lock_bh(&local->sta_bss_lock);
2270 /* TODO: order by RSSI? */
2271 list_add_tail(&bss->list, &local->sta_bss_list);
2272 __ieee80211_rx_bss_hash_add(dev, bss);
2273 spin_unlock_bh(&local->sta_bss_lock);
2278 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2284 kfree(bss_mesh_id(bss));
2285 kfree(bss_mesh_cfg(bss));
2290 static void ieee80211_rx_bss_put(struct net_device *dev,
2291 struct ieee80211_sta_bss *bss)
2293 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2296 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2301 __ieee80211_rx_bss_hash_del(dev, bss);
2302 list_del(&bss->list);
2303 spin_unlock_bh(&local->sta_bss_lock);
2304 ieee80211_rx_bss_free(bss);
2308 void ieee80211_rx_bss_list_init(struct net_device *dev)
2310 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2311 spin_lock_init(&local->sta_bss_lock);
2312 INIT_LIST_HEAD(&local->sta_bss_list);
2316 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2318 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2319 struct ieee80211_sta_bss *bss, *tmp;
2321 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2322 ieee80211_rx_bss_put(dev, bss);
2326 static int ieee80211_sta_join_ibss(struct net_device *dev,
2327 struct ieee80211_if_sta *ifsta,
2328 struct ieee80211_sta_bss *bss)
2330 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2331 int res, rates, i, j;
2332 struct sk_buff *skb;
2333 struct ieee80211_mgmt *mgmt;
2334 struct ieee80211_tx_control control;
2335 struct rate_selection ratesel;
2337 struct ieee80211_sub_if_data *sdata;
2338 struct ieee80211_supported_band *sband;
2339 union iwreq_data wrqu;
2341 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2343 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2345 /* Remove possible STA entries from other IBSS networks. */
2346 sta_info_flush_delayed(sdata);
2348 if (local->ops->reset_tsf) {
2349 /* Reset own TSF to allow time synchronization work. */
2350 local->ops->reset_tsf(local_to_hw(local));
2352 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2353 res = ieee80211_if_config(dev);
2357 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2359 sdata->drop_unencrypted = bss->capability &
2360 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2362 res = ieee80211_set_freq(dev, bss->freq);
2367 /* Set beacon template */
2368 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2373 skb_reserve(skb, local->hw.extra_tx_headroom);
2375 mgmt = (struct ieee80211_mgmt *)
2376 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2377 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2378 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2379 IEEE80211_STYPE_BEACON);
2380 memset(mgmt->da, 0xff, ETH_ALEN);
2381 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2382 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2383 mgmt->u.beacon.beacon_int =
2384 cpu_to_le16(local->hw.conf.beacon_int);
2385 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2387 pos = skb_put(skb, 2 + ifsta->ssid_len);
2388 *pos++ = WLAN_EID_SSID;
2389 *pos++ = ifsta->ssid_len;
2390 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2392 rates = bss->supp_rates_len;
2395 pos = skb_put(skb, 2 + rates);
2396 *pos++ = WLAN_EID_SUPP_RATES;
2398 memcpy(pos, bss->supp_rates, rates);
2400 if (bss->band == IEEE80211_BAND_2GHZ) {
2401 pos = skb_put(skb, 2 + 1);
2402 *pos++ = WLAN_EID_DS_PARAMS;
2404 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2407 pos = skb_put(skb, 2 + 2);
2408 *pos++ = WLAN_EID_IBSS_PARAMS;
2410 /* FIX: set ATIM window based on scan results */
2414 if (bss->supp_rates_len > 8) {
2415 rates = bss->supp_rates_len - 8;
2416 pos = skb_put(skb, 2 + rates);
2417 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2419 memcpy(pos, &bss->supp_rates[8], rates);
2422 memset(&control, 0, sizeof(control));
2423 rate_control_get_rate(dev, sband, skb, &ratesel);
2424 if (!ratesel.rate) {
2425 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2426 "for IBSS beacon\n", dev->name);
2429 control.vif = &sdata->vif;
2430 control.tx_rate = ratesel.rate;
2431 if (sdata->bss_conf.use_short_preamble &&
2432 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2433 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2434 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2435 control.flags |= IEEE80211_TXCTL_NO_ACK;
2436 control.retry_limit = 1;
2438 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2439 if (ifsta->probe_resp) {
2440 mgmt = (struct ieee80211_mgmt *)
2441 ifsta->probe_resp->data;
2442 mgmt->frame_control =
2443 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2444 IEEE80211_STYPE_PROBE_RESP);
2446 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2447 "template for IBSS\n", dev->name);
2450 if (local->ops->beacon_update &&
2451 local->ops->beacon_update(local_to_hw(local),
2452 skb, &control) == 0) {
2453 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2454 "template\n", dev->name);
2459 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2460 for (i = 0; i < bss->supp_rates_len; i++) {
2461 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2462 for (j = 0; j < sband->n_bitrates; j++)
2463 if (sband->bitrates[j].bitrate == bitrate)
2466 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2468 ieee80211_sta_def_wmm_params(dev, bss, 1);
2472 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2473 "template\n", dev->name);
2477 ifsta->state = IEEE80211_IBSS_JOINED;
2478 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2480 memset(&wrqu, 0, sizeof(wrqu));
2481 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2482 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
2487 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2488 struct ieee802_11_elems *elems,
2489 enum ieee80211_band band)
2491 struct ieee80211_supported_band *sband;
2492 struct ieee80211_rate *bitrates;
2496 sband = local->hw.wiphy->bands[band];
2500 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2503 bitrates = sband->bitrates;
2504 num_rates = sband->n_bitrates;
2506 for (i = 0; i < elems->supp_rates_len +
2507 elems->ext_supp_rates_len; i++) {
2510 if (i < elems->supp_rates_len)
2511 rate = elems->supp_rates[i];
2512 else if (elems->ext_supp_rates)
2513 rate = elems->ext_supp_rates
2514 [i - elems->supp_rates_len];
2515 own_rate = 5 * (rate & 0x7f);
2516 for (j = 0; j < num_rates; j++)
2517 if (bitrates[j].bitrate == own_rate)
2518 supp_rates |= BIT(j);
2524 static void ieee80211_rx_bss_info(struct net_device *dev,
2525 struct ieee80211_mgmt *mgmt,
2527 struct ieee80211_rx_status *rx_status,
2530 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2531 struct ieee802_11_elems elems;
2534 struct ieee80211_sta_bss *bss;
2535 struct sta_info *sta;
2536 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2537 u64 beacon_timestamp, rx_timestamp;
2538 struct ieee80211_channel *channel;
2539 DECLARE_MAC_BUF(mac);
2540 DECLARE_MAC_BUF(mac2);
2542 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2543 return; /* ignore ProbeResp to foreign address */
2546 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
2547 dev->name, beacon ? "Beacon" : "Probe Response",
2548 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
2551 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2555 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2556 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2558 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2559 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2560 u64 rates = ieee80211_sta_get_rates(local, &elems,
2563 mesh_neighbour_update(mgmt->sa, rates, dev,
2564 mesh_peer_accepts_plinks(&elems, dev));
2569 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
2570 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2571 (sta = sta_info_get(local, mgmt->sa))) {
2573 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2576 prev_rates = sta->supp_rates[rx_status->band];
2577 sta->supp_rates[rx_status->band] &= supp_rates;
2578 if (sta->supp_rates[rx_status->band] == 0) {
2579 /* No matching rates - this should not really happen.
2580 * Make sure that at least one rate is marked
2581 * supported to avoid issues with TX rate ctrl. */
2582 sta->supp_rates[rx_status->band] =
2583 sdata->u.sta.supp_rates_bits[rx_status->band];
2585 if (sta->supp_rates[rx_status->band] != prev_rates) {
2586 printk(KERN_DEBUG "%s: updated supp_rates set for "
2587 "%s based on beacon info (0x%llx & 0x%llx -> "
2589 dev->name, print_mac(mac, sta->addr),
2590 (unsigned long long) prev_rates,
2591 (unsigned long long) supp_rates,
2592 (unsigned long long) sta->supp_rates[rx_status->band]);
2598 if (elems.ds_params && elems.ds_params_len == 1)
2599 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
2601 freq = rx_status->freq;
2603 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2605 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2608 #ifdef CONFIG_MAC80211_MESH
2609 if (elems.mesh_config)
2610 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2611 elems.mesh_id_len, elems.mesh_config, freq);
2614 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2615 elems.ssid, elems.ssid_len);
2617 #ifdef CONFIG_MAC80211_MESH
2618 if (elems.mesh_config)
2619 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2620 elems.mesh_id_len, elems.mesh_config,
2621 elems.mesh_config_len, freq);
2624 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2625 elems.ssid, elems.ssid_len);
2630 /* TODO: order by RSSI? */
2631 spin_lock_bh(&local->sta_bss_lock);
2632 list_move_tail(&bss->list, &local->sta_bss_list);
2633 spin_unlock_bh(&local->sta_bss_lock);
2637 /* save the ERP value so that it is available at association time */
2638 if (elems.erp_info && elems.erp_info_len >= 1) {
2639 bss->erp_value = elems.erp_info[0];
2640 bss->has_erp_value = 1;
2643 if (elems.ht_cap_elem &&
2644 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2645 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2647 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2649 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2650 elems.ht_cap_elem_len + 2);
2651 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2654 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2660 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2661 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2663 bss->supp_rates_len = 0;
2664 if (elems.supp_rates) {
2665 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2666 if (clen > elems.supp_rates_len)
2667 clen = elems.supp_rates_len;
2668 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2670 bss->supp_rates_len += clen;
2672 if (elems.ext_supp_rates) {
2673 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2674 if (clen > elems.ext_supp_rates_len)
2675 clen = elems.ext_supp_rates_len;
2676 memcpy(&bss->supp_rates[bss->supp_rates_len],
2677 elems.ext_supp_rates, clen);
2678 bss->supp_rates_len += clen;
2681 bss->band = rx_status->band;
2683 bss->timestamp = beacon_timestamp;
2684 bss->last_update = jiffies;
2685 bss->rssi = rx_status->ssi;
2686 bss->signal = rx_status->signal;
2687 bss->noise = rx_status->noise;
2688 if (!beacon && !bss->probe_resp)
2689 bss->probe_resp = true;
2692 * In STA mode, the remaining parameters should not be overridden
2693 * by beacons because they're not necessarily accurate there.
2695 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2696 bss->probe_resp && beacon) {
2697 ieee80211_rx_bss_put(dev, bss);
2702 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2703 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2705 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2707 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2708 bss->wpa_ie_len = elems.wpa_len + 2;
2710 bss->wpa_ie_len = 0;
2711 } else if (!elems.wpa && bss->wpa_ie) {
2714 bss->wpa_ie_len = 0;
2718 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2719 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2721 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2723 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2724 bss->rsn_ie_len = elems.rsn_len + 2;
2726 bss->rsn_ie_len = 0;
2727 } else if (!elems.rsn && bss->rsn_ie) {
2730 bss->rsn_ie_len = 0;
2735 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2739 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2740 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2741 * Alliance (September 1, 2004) is incorporated by reference herein.
2742 * The inclusion of the WMM Parameters in probe responses and
2743 * association responses is mandatory for WMM enabled networks. The
2744 * inclusion of the WMM Parameters in beacons, however, is optional.
2747 if (elems.wmm_param &&
2748 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2749 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2751 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2753 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2754 elems.wmm_param_len + 2);
2755 bss->wmm_ie_len = elems.wmm_param_len + 2;
2757 bss->wmm_ie_len = 0;
2758 } else if (elems.wmm_info &&
2759 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_info_len ||
2760 memcmp(bss->wmm_ie, elems.wmm_info, elems.wmm_info_len))) {
2761 /* As for certain AP's Fifth bit is not set in WMM IE in
2762 * beacon frames.So while parsing the beacon frame the
2763 * wmm_info structure is used instead of wmm_param.
2764 * wmm_info structure was never used to set bss->wmm_ie.
2765 * This code fixes this problem by copying the WME
2766 * information from wmm_info to bss->wmm_ie and enabling
2767 * n-band association.
2770 bss->wmm_ie = kmalloc(elems.wmm_info_len + 2, GFP_ATOMIC);
2772 memcpy(bss->wmm_ie, elems.wmm_info - 2,
2773 elems.wmm_info_len + 2);
2774 bss->wmm_ie_len = elems.wmm_info_len + 2;
2776 bss->wmm_ie_len = 0;
2777 } else if (!elems.wmm_param && !elems.wmm_info && bss->wmm_ie) {
2780 bss->wmm_ie_len = 0;
2783 /* check if we need to merge IBSS */
2784 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2785 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2786 bss->capability & WLAN_CAPABILITY_IBSS &&
2787 bss->freq == local->oper_channel->center_freq &&
2788 elems.ssid_len == sdata->u.sta.ssid_len &&
2789 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2790 if (rx_status->flag & RX_FLAG_TSFT) {
2791 /* in order for correct IBSS merging we need mactime
2793 * since mactime is defined as the time the first data
2794 * symbol of the frame hits the PHY, and the timestamp
2795 * of the beacon is defined as "the time that the data
2796 * symbol containing the first bit of the timestamp is
2797 * transmitted to the PHY plus the transmitting STA’s
2798 * delays through its local PHY from the MAC-PHY
2799 * interface to its interface with the WM"
2800 * (802.11 11.1.2) - equals the time this bit arrives at
2801 * the receiver - we have to take into account the
2802 * offset between the two.
2803 * e.g: at 1 MBit that means mactime is 192 usec earlier
2804 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2806 int rate = local->hw.wiphy->bands[rx_status->band]->
2807 bitrates[rx_status->rate_idx].bitrate;
2808 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2809 } else if (local && local->ops && local->ops->get_tsf)
2810 /* second best option: get current TSF */
2811 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2813 /* can't merge without knowing the TSF */
2814 rx_timestamp = -1LLU;
2815 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2816 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2817 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2818 print_mac(mac, mgmt->sa),
2819 print_mac(mac2, mgmt->bssid),
2820 (unsigned long long)rx_timestamp,
2821 (unsigned long long)beacon_timestamp,
2822 (unsigned long long)(rx_timestamp - beacon_timestamp),
2824 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2825 if (beacon_timestamp > rx_timestamp) {
2826 #ifndef CONFIG_MAC80211_IBSS_DEBUG
2827 if (net_ratelimit())
2829 printk(KERN_DEBUG "%s: beacon TSF higher than "
2830 "local TSF - IBSS merge with BSSID %s\n",
2831 dev->name, print_mac(mac, mgmt->bssid));
2832 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2833 ieee80211_ibss_add_sta(dev, NULL,
2834 mgmt->bssid, mgmt->sa);
2838 ieee80211_rx_bss_put(dev, bss);
2842 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2843 struct ieee80211_mgmt *mgmt,
2845 struct ieee80211_rx_status *rx_status)
2847 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2851 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2852 struct ieee80211_mgmt *mgmt,
2854 struct ieee80211_rx_status *rx_status)
2856 struct ieee80211_sub_if_data *sdata;
2857 struct ieee80211_if_sta *ifsta;
2859 struct ieee802_11_elems elems;
2860 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2861 struct ieee80211_conf *conf = &local->hw.conf;
2864 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2866 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2867 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2869 ifsta = &sdata->u.sta;
2871 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2872 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2875 /* Process beacon from the current BSS */
2876 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2880 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2882 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2883 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2884 elems.wmm_param_len);
2887 /* Do not send changes to driver if we are scanning. This removes
2888 * requirement that driver's bss_info_changed function needs to be
2890 if (local->sta_sw_scanning || local->sta_hw_scanning)
2893 if (elems.erp_info && elems.erp_info_len >= 1)
2894 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2896 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2897 changed |= ieee80211_handle_protect_preamb(sdata, false,
2898 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2901 if (elems.ht_cap_elem && elems.ht_info_elem &&
2902 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2903 struct ieee80211_ht_bss_info bss_info;
2905 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2906 (struct ieee80211_ht_addt_info *)
2907 elems.ht_info_elem, &bss_info);
2908 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2912 ieee80211_bss_info_change_notify(sdata, changed);
2916 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2917 struct ieee80211_if_sta *ifsta,
2918 struct ieee80211_mgmt *mgmt,
2920 struct ieee80211_rx_status *rx_status)
2922 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2923 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2925 struct sk_buff *skb;
2926 struct ieee80211_mgmt *resp;
2928 DECLARE_MAC_BUF(mac);
2929 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2930 DECLARE_MAC_BUF(mac2);
2931 DECLARE_MAC_BUF(mac3);
2934 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2935 ifsta->state != IEEE80211_IBSS_JOINED ||
2936 len < 24 + 2 || !ifsta->probe_resp)
2939 if (local->ops->tx_last_beacon)
2940 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2944 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2945 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2946 "%s (tx_last_beacon=%d)\n",
2947 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2948 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2949 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2951 if (!tx_last_beacon)
2954 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2955 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2958 end = ((u8 *) mgmt) + len;
2959 pos = mgmt->u.probe_req.variable;
2960 if (pos[0] != WLAN_EID_SSID ||
2961 pos + 2 + pos[1] > end) {
2962 if (net_ratelimit()) {
2963 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2965 dev->name, print_mac(mac, mgmt->sa));
2970 (pos[1] != ifsta->ssid_len ||
2971 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2972 /* Ignore ProbeReq for foreign SSID */
2976 /* Reply with ProbeResp */
2977 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2981 resp = (struct ieee80211_mgmt *) skb->data;
2982 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2983 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2984 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2985 dev->name, print_mac(mac, resp->da));
2986 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2987 ieee80211_sta_tx(dev, skb, 0);
2990 static void ieee80211_rx_mgmt_action(struct net_device *dev,
2991 struct ieee80211_if_sta *ifsta,
2992 struct ieee80211_mgmt *mgmt,
2994 struct ieee80211_rx_status *rx_status)
2996 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2998 if (len < IEEE80211_MIN_ACTION_SIZE)
3001 switch (mgmt->u.action.category) {
3002 case WLAN_CATEGORY_BACK:
3003 switch (mgmt->u.action.u.addba_req.action_code) {
3004 case WLAN_ACTION_ADDBA_REQ:
3005 if (len < (IEEE80211_MIN_ACTION_SIZE +
3006 sizeof(mgmt->u.action.u.addba_req)))
3008 ieee80211_sta_process_addba_request(dev, mgmt, len);
3010 case WLAN_ACTION_ADDBA_RESP:
3011 if (len < (IEEE80211_MIN_ACTION_SIZE +
3012 sizeof(mgmt->u.action.u.addba_resp)))
3014 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3016 case WLAN_ACTION_DELBA:
3017 if (len < (IEEE80211_MIN_ACTION_SIZE +
3018 sizeof(mgmt->u.action.u.delba)))
3020 ieee80211_sta_process_delba(dev, mgmt, len);
3023 if (net_ratelimit())
3024 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
3029 case PLINK_CATEGORY:
3030 if (ieee80211_vif_is_mesh(&sdata->vif))
3031 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3033 case MESH_PATH_SEL_CATEGORY:
3034 if (ieee80211_vif_is_mesh(&sdata->vif))
3035 mesh_rx_path_sel_frame(dev, mgmt, len);
3038 if (net_ratelimit())
3039 printk(KERN_DEBUG "%s: Rx unknown action frame - "
3040 "category=%d\n", dev->name, mgmt->u.action.category);
3045 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3046 struct ieee80211_rx_status *rx_status)
3048 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3049 struct ieee80211_sub_if_data *sdata;
3050 struct ieee80211_if_sta *ifsta;
3051 struct ieee80211_mgmt *mgmt;
3057 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3058 ifsta = &sdata->u.sta;
3060 mgmt = (struct ieee80211_mgmt *) skb->data;
3061 fc = le16_to_cpu(mgmt->frame_control);
3063 switch (fc & IEEE80211_FCTL_STYPE) {
3064 case IEEE80211_STYPE_PROBE_REQ:
3065 case IEEE80211_STYPE_PROBE_RESP:
3066 case IEEE80211_STYPE_BEACON:
3067 case IEEE80211_STYPE_ACTION:
3068 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3069 case IEEE80211_STYPE_AUTH:
3070 case IEEE80211_STYPE_ASSOC_RESP:
3071 case IEEE80211_STYPE_REASSOC_RESP:
3072 case IEEE80211_STYPE_DEAUTH:
3073 case IEEE80211_STYPE_DISASSOC:
3074 skb_queue_tail(&ifsta->skb_queue, skb);
3075 queue_work(local->hw.workqueue, &ifsta->work);
3078 printk(KERN_DEBUG "%s: received unknown management frame - "
3079 "stype=%d\n", dev->name,
3080 (fc & IEEE80211_FCTL_STYPE) >> 4);
3089 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3090 struct sk_buff *skb)
3092 struct ieee80211_rx_status *rx_status;
3093 struct ieee80211_sub_if_data *sdata;
3094 struct ieee80211_if_sta *ifsta;
3095 struct ieee80211_mgmt *mgmt;
3098 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3099 ifsta = &sdata->u.sta;
3101 rx_status = (struct ieee80211_rx_status *) skb->cb;
3102 mgmt = (struct ieee80211_mgmt *) skb->data;
3103 fc = le16_to_cpu(mgmt->frame_control);
3105 switch (fc & IEEE80211_FCTL_STYPE) {
3106 case IEEE80211_STYPE_PROBE_REQ:
3107 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3110 case IEEE80211_STYPE_PROBE_RESP:
3111 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3113 case IEEE80211_STYPE_BEACON:
3114 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3116 case IEEE80211_STYPE_AUTH:
3117 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3119 case IEEE80211_STYPE_ASSOC_RESP:
3120 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
3122 case IEEE80211_STYPE_REASSOC_RESP:
3123 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
3125 case IEEE80211_STYPE_DEAUTH:
3126 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3128 case IEEE80211_STYPE_DISASSOC:
3129 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3131 case IEEE80211_STYPE_ACTION:
3132 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
3141 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3142 struct ieee80211_rx_status *rx_status)
3144 struct ieee80211_mgmt *mgmt;
3148 return RX_DROP_UNUSABLE;
3150 mgmt = (struct ieee80211_mgmt *) skb->data;
3151 fc = le16_to_cpu(mgmt->frame_control);
3153 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
3157 return RX_DROP_MONITOR;
3159 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3160 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3161 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3162 skb->len, rx_status);
3165 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3166 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3176 static int ieee80211_sta_active_ibss(struct net_device *dev)
3178 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3180 struct sta_info *sta;
3181 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3185 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3186 if (sta->sdata == sdata &&
3187 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3200 static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
3202 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3203 struct sta_info *sta, *tmp;
3204 LIST_HEAD(tmp_list);
3205 DECLARE_MAC_BUF(mac);
3206 unsigned long flags;
3208 spin_lock_irqsave(&local->sta_lock, flags);
3209 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3210 if (time_after(jiffies, sta->last_rx + exp_time)) {
3211 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3212 dev->name, print_mac(mac, sta->addr));
3213 __sta_info_unlink(&sta);
3215 list_add(&sta->list, &tmp_list);
3217 spin_unlock_irqrestore(&local->sta_lock, flags);
3219 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3220 sta_info_destroy(sta);
3224 static void ieee80211_sta_merge_ibss(struct net_device *dev,
3225 struct ieee80211_if_sta *ifsta)
3227 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3229 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
3230 if (ieee80211_sta_active_ibss(dev))
3233 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3234 "IBSS networks with same SSID (merge)\n", dev->name);
3235 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3239 #ifdef CONFIG_MAC80211_MESH
3240 static void ieee80211_mesh_housekeeping(struct net_device *dev,
3241 struct ieee80211_if_sta *ifsta)
3243 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3246 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3247 mesh_path_expire(dev);
3249 free_plinks = mesh_plink_availables(sdata);
3250 if (free_plinks != sdata->u.sta.accepting_plinks)
3251 ieee80211_if_config_beacon(dev);
3253 mod_timer(&ifsta->timer, jiffies +
3254 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3258 void ieee80211_start_mesh(struct net_device *dev)
3260 struct ieee80211_if_sta *ifsta;
3261 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3262 ifsta = &sdata->u.sta;
3263 ifsta->state = IEEE80211_MESH_UP;
3264 ieee80211_sta_timer((unsigned long)sdata);
3269 void ieee80211_sta_timer(unsigned long data)
3271 struct ieee80211_sub_if_data *sdata =
3272 (struct ieee80211_sub_if_data *) data;
3273 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3274 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3276 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3277 queue_work(local->hw.workqueue, &ifsta->work);
3280 void ieee80211_sta_work(struct work_struct *work)
3282 struct ieee80211_sub_if_data *sdata =
3283 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3284 struct net_device *dev = sdata->dev;
3285 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3286 struct ieee80211_if_sta *ifsta;
3287 struct sk_buff *skb;
3289 if (!netif_running(dev))
3292 if (local->sta_sw_scanning || local->sta_hw_scanning)
3295 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3296 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3297 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
3298 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
3299 "(type=%d)\n", dev->name, sdata->vif.type);
3302 ifsta = &sdata->u.sta;
3304 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3305 ieee80211_sta_rx_queued_mgmt(dev, skb);
3307 #ifdef CONFIG_MAC80211_MESH
3308 if (ifsta->preq_queue_len &&
3310 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3311 mesh_path_start_discovery(dev);
3314 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3315 ifsta->state != IEEE80211_ASSOCIATE &&
3316 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3317 if (ifsta->scan_ssid_len)
3318 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3320 ieee80211_sta_start_scan(dev, NULL, 0);
3324 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3325 if (ieee80211_sta_config_auth(dev, ifsta))
3327 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3328 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3331 switch (ifsta->state) {
3332 case IEEE80211_DISABLED:
3334 case IEEE80211_AUTHENTICATE:
3335 ieee80211_authenticate(dev, ifsta);
3337 case IEEE80211_ASSOCIATE:
3338 ieee80211_associate(dev, ifsta);
3340 case IEEE80211_ASSOCIATED:
3341 ieee80211_associated(dev, ifsta);
3343 case IEEE80211_IBSS_SEARCH:
3344 ieee80211_sta_find_ibss(dev, ifsta);
3346 case IEEE80211_IBSS_JOINED:
3347 ieee80211_sta_merge_ibss(dev, ifsta);
3349 #ifdef CONFIG_MAC80211_MESH
3350 case IEEE80211_MESH_UP:
3351 ieee80211_mesh_housekeeping(dev, ifsta);
3355 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3360 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3361 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3362 "mixed-cell disabled - disassociate\n", dev->name);
3364 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3365 ieee80211_set_disassoc(dev, ifsta, 0);
3370 static void ieee80211_sta_reset_auth(struct net_device *dev,
3371 struct ieee80211_if_sta *ifsta)
3373 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3375 if (local->ops->reset_tsf) {
3376 /* Reset own TSF to allow time synchronization work. */
3377 local->ops->reset_tsf(local_to_hw(local));
3380 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3383 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3384 ifsta->auth_alg = WLAN_AUTH_OPEN;
3385 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3386 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3387 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3388 ifsta->auth_alg = WLAN_AUTH_LEAP;
3390 ifsta->auth_alg = WLAN_AUTH_OPEN;
3391 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3393 ifsta->auth_transaction = -1;
3394 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3395 ifsta->auth_tries = ifsta->assoc_tries = 0;
3396 netif_carrier_off(dev);
3400 void ieee80211_sta_req_auth(struct net_device *dev,
3401 struct ieee80211_if_sta *ifsta)
3403 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3404 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3406 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3409 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3410 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3411 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3412 IEEE80211_STA_AUTO_SSID_SEL))) {
3413 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3414 queue_work(local->hw.workqueue, &ifsta->work);
3418 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3419 const char *ssid, int ssid_len)
3421 int tmp, hidden_ssid;
3423 if (ssid_len == ifsta->ssid_len &&
3424 !memcmp(ifsta->ssid, ssid, ssid_len))
3427 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3433 if (ssid[tmp] != '\0') {
3439 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3442 if (ssid_len == 1 && ssid[0] == ' ')
3448 static int ieee80211_sta_config_auth(struct net_device *dev,
3449 struct ieee80211_if_sta *ifsta)
3451 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3452 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3453 struct ieee80211_sta_bss *bss, *selected = NULL;
3454 int top_rssi = 0, freq;
3456 spin_lock_bh(&local->sta_bss_lock);
3457 freq = local->oper_channel->center_freq;
3458 list_for_each_entry(bss, &local->sta_bss_list, list) {
3459 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3462 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3463 IEEE80211_STA_AUTO_BSSID_SEL |
3464 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3465 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3466 !!sdata->default_key))
3469 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3473 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3474 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3477 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3478 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3481 if (!selected || top_rssi < bss->rssi) {
3483 top_rssi = bss->rssi;
3487 atomic_inc(&selected->users);
3488 spin_unlock_bh(&local->sta_bss_lock);
3491 ieee80211_set_freq(dev, selected->freq);
3492 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3493 ieee80211_sta_set_ssid(dev, selected->ssid,
3494 selected->ssid_len);
3495 ieee80211_sta_set_bssid(dev, selected->bssid);
3496 ieee80211_sta_def_wmm_params(dev, selected, 0);
3497 ieee80211_rx_bss_put(dev, selected);
3498 ifsta->state = IEEE80211_AUTHENTICATE;
3499 ieee80211_sta_reset_auth(dev, ifsta);
3502 if (ifsta->state != IEEE80211_AUTHENTICATE) {
3503 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3504 ieee80211_sta_start_scan(dev, NULL, 0);
3506 ieee80211_sta_start_scan(dev, ifsta->ssid,
3508 ifsta->state = IEEE80211_AUTHENTICATE;
3509 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3511 ifsta->state = IEEE80211_DISABLED;
3517 static int ieee80211_sta_create_ibss(struct net_device *dev,
3518 struct ieee80211_if_sta *ifsta)
3520 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3521 struct ieee80211_sta_bss *bss;
3522 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3523 struct ieee80211_supported_band *sband;
3524 u8 bssid[ETH_ALEN], *pos;
3527 DECLARE_MAC_BUF(mac);
3530 /* Easier testing, use fixed BSSID. */
3531 memset(bssid, 0xfe, ETH_ALEN);
3533 /* Generate random, not broadcast, locally administered BSSID. Mix in
3534 * own MAC address to make sure that devices that do not have proper
3535 * random number generator get different BSSID. */
3536 get_random_bytes(bssid, ETH_ALEN);
3537 for (i = 0; i < ETH_ALEN; i++)
3538 bssid[i] ^= dev->dev_addr[i];
3543 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3544 dev->name, print_mac(mac, bssid));
3546 bss = ieee80211_rx_bss_add(dev, bssid,
3547 local->hw.conf.channel->center_freq,
3548 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3552 bss->band = local->hw.conf.channel->band;
3553 sband = local->hw.wiphy->bands[bss->band];
3555 if (local->hw.conf.beacon_int == 0)
3556 local->hw.conf.beacon_int = 10000;
3557 bss->beacon_int = local->hw.conf.beacon_int;
3558 bss->last_update = jiffies;
3559 bss->capability = WLAN_CAPABILITY_IBSS;
3560 if (sdata->default_key) {
3561 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3563 sdata->drop_unencrypted = 0;
3564 bss->supp_rates_len = sband->n_bitrates;
3565 pos = bss->supp_rates;
3566 for (i = 0; i < sband->n_bitrates; i++) {
3567 int rate = sband->bitrates[i].bitrate;
3568 *pos++ = (u8) (rate / 5);
3571 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3572 ieee80211_rx_bss_put(dev, bss);
3577 static int ieee80211_sta_find_ibss(struct net_device *dev,
3578 struct ieee80211_if_sta *ifsta)
3580 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3581 struct ieee80211_sta_bss *bss;
3585 DECLARE_MAC_BUF(mac);
3586 DECLARE_MAC_BUF(mac2);
3588 if (ifsta->ssid_len == 0)
3591 active_ibss = ieee80211_sta_active_ibss(dev);
3592 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3593 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3594 dev->name, active_ibss);
3595 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3596 spin_lock_bh(&local->sta_bss_lock);
3597 list_for_each_entry(bss, &local->sta_bss_list, list) {
3598 if (ifsta->ssid_len != bss->ssid_len ||
3599 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3600 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3602 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3603 printk(KERN_DEBUG " bssid=%s found\n",
3604 print_mac(mac, bss->bssid));
3605 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3606 memcpy(bssid, bss->bssid, ETH_ALEN);
3608 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3611 spin_unlock_bh(&local->sta_bss_lock);
3613 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3614 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3615 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
3616 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3617 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
3618 (bss = ieee80211_rx_bss_get(dev, bssid,
3619 local->hw.conf.channel->center_freq,
3620 ifsta->ssid, ifsta->ssid_len))) {
3622 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3623 " based on configured SSID\n",
3624 dev->name, print_mac(mac, bssid));
3625 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3626 ieee80211_rx_bss_put(dev, bss);
3629 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3630 printk(KERN_DEBUG " did not try to join ibss\n");
3631 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3633 /* Selected IBSS not found in current scan results - try to scan */
3634 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3635 !ieee80211_sta_active_ibss(dev)) {
3636 mod_timer(&ifsta->timer, jiffies +
3637 IEEE80211_IBSS_MERGE_INTERVAL);
3638 } else if (time_after(jiffies, local->last_scan_completed +
3639 IEEE80211_SCAN_INTERVAL)) {
3640 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3641 "join\n", dev->name);
3642 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3644 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3645 int interval = IEEE80211_SCAN_INTERVAL;
3647 if (time_after(jiffies, ifsta->ibss_join_req +
3648 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3649 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3650 (!(local->oper_channel->flags &
3651 IEEE80211_CHAN_NO_IBSS)))
3652 return ieee80211_sta_create_ibss(dev, ifsta);
3653 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3654 printk(KERN_DEBUG "%s: IBSS not allowed on"
3655 " %d MHz\n", dev->name,
3656 local->hw.conf.channel->center_freq);
3659 /* No IBSS found - decrease scan interval and continue
3661 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3664 ifsta->state = IEEE80211_IBSS_SEARCH;
3665 mod_timer(&ifsta->timer, jiffies + interval);
3673 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3675 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3676 struct ieee80211_if_sta *ifsta;
3678 if (len > IEEE80211_MAX_SSID_LEN)
3681 ifsta = &sdata->u.sta;
3683 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
3684 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3685 memcpy(ifsta->ssid, ssid, len);
3686 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3687 ifsta->ssid_len = len;
3690 ifsta->flags |= IEEE80211_STA_SSID_SET;
3692 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3693 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3694 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3695 ifsta->ibss_join_req = jiffies;
3696 ifsta->state = IEEE80211_IBSS_SEARCH;
3697 return ieee80211_sta_find_ibss(dev, ifsta);
3703 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3705 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3706 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3707 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3708 *len = ifsta->ssid_len;
3713 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3715 struct ieee80211_sub_if_data *sdata;
3716 struct ieee80211_if_sta *ifsta;
3719 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3720 ifsta = &sdata->u.sta;
3722 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3723 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3724 res = ieee80211_if_config(dev);
3726 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3727 "the low-level driver\n", dev->name);
3732 if (is_valid_ether_addr(bssid))
3733 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3735 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3741 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3742 struct ieee80211_sub_if_data *sdata,
3745 struct sk_buff *skb;
3746 struct ieee80211_hdr *nullfunc;
3749 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3751 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3752 "frame\n", sdata->dev->name);
3755 skb_reserve(skb, local->hw.extra_tx_headroom);
3757 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3758 memset(nullfunc, 0, 24);
3759 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3760 IEEE80211_FCTL_TODS;
3762 fc |= IEEE80211_FCTL_PM;
3763 nullfunc->frame_control = cpu_to_le16(fc);
3764 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3765 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3766 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3768 ieee80211_sta_tx(sdata->dev, skb, 0);
3772 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3774 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3775 ieee80211_vif_is_mesh(&sdata->vif))
3776 ieee80211_sta_timer((unsigned long)sdata);
3779 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3781 struct ieee80211_local *local = hw_to_local(hw);
3782 struct net_device *dev = local->scan_dev;
3783 struct ieee80211_sub_if_data *sdata;
3784 union iwreq_data wrqu;
3786 local->last_scan_completed = jiffies;
3787 memset(&wrqu, 0, sizeof(wrqu));
3788 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3790 if (local->sta_hw_scanning) {
3791 local->sta_hw_scanning = 0;
3792 if (ieee80211_hw_config(local))
3793 printk(KERN_DEBUG "%s: failed to restore operational "
3794 "channel after scan\n", dev->name);
3795 /* Restart STA timer for HW scan case */
3797 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3798 ieee80211_restart_sta_timer(sdata);
3804 local->sta_sw_scanning = 0;
3805 if (ieee80211_hw_config(local))
3806 printk(KERN_DEBUG "%s: failed to restore operational "
3807 "channel after scan\n", dev->name);
3810 netif_tx_lock_bh(local->mdev);
3811 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3812 local->ops->configure_filter(local_to_hw(local),
3813 FIF_BCN_PRBRESP_PROMISC,
3814 &local->filter_flags,
3815 local->mdev->mc_count,
3816 local->mdev->mc_list);
3818 netif_tx_unlock_bh(local->mdev);
3821 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3823 /* No need to wake the master device. */
3824 if (sdata->dev == local->mdev)
3827 /* Tell AP we're back */
3828 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3829 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3830 ieee80211_send_nullfunc(local, sdata, 0);
3832 ieee80211_restart_sta_timer(sdata);
3834 netif_wake_queue(sdata->dev);
3839 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3840 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3841 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3842 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3843 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3844 !ieee80211_sta_active_ibss(dev)))
3845 ieee80211_sta_find_ibss(dev, ifsta);
3848 EXPORT_SYMBOL(ieee80211_scan_completed);
3850 void ieee80211_sta_scan_work(struct work_struct *work)
3852 struct ieee80211_local *local =
3853 container_of(work, struct ieee80211_local, scan_work.work);
3854 struct net_device *dev = local->scan_dev;
3855 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3856 struct ieee80211_supported_band *sband;
3857 struct ieee80211_channel *chan;
3859 unsigned long next_delay = 0;
3861 if (!local->sta_sw_scanning)
3864 switch (local->scan_state) {
3865 case SCAN_SET_CHANNEL:
3867 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3868 * after we successfully scanned the last channel of the last
3869 * band (and the last band is supported by the hw)
3871 if (local->scan_band < IEEE80211_NUM_BANDS)
3872 sband = local->hw.wiphy->bands[local->scan_band];
3877 * If we are at an unsupported band and have more bands
3878 * left to scan, advance to the next supported one.
3880 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3882 sband = local->hw.wiphy->bands[local->scan_band];
3883 local->scan_channel_idx = 0;
3886 /* if no more bands/channels left, complete scan */
3887 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3888 ieee80211_scan_completed(local_to_hw(local));
3892 chan = &sband->channels[local->scan_channel_idx];
3894 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3895 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3896 chan->flags & IEEE80211_CHAN_NO_IBSS))
3900 local->scan_channel = chan;
3901 if (ieee80211_hw_config(local)) {
3902 printk(KERN_DEBUG "%s: failed to set freq to "
3903 "%d MHz for scan\n", dev->name,
3909 /* advance state machine to next channel/band */
3910 local->scan_channel_idx++;
3911 if (local->scan_channel_idx >= sband->n_channels) {
3913 * scan_band may end up == IEEE80211_NUM_BANDS, but
3914 * we'll catch that case above and complete the scan
3915 * if that is the case.
3918 local->scan_channel_idx = 0;
3924 next_delay = IEEE80211_PROBE_DELAY +
3925 usecs_to_jiffies(local->hw.channel_change_time);
3926 local->scan_state = SCAN_SEND_PROBE;
3928 case SCAN_SEND_PROBE:
3929 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3930 local->scan_state = SCAN_SET_CHANNEL;
3932 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3934 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3935 local->scan_ssid_len);
3936 next_delay = IEEE80211_CHANNEL_TIME;
3940 if (local->sta_sw_scanning)
3941 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3946 static int ieee80211_sta_start_scan(struct net_device *dev,
3947 u8 *ssid, size_t ssid_len)
3949 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3950 struct ieee80211_sub_if_data *sdata;
3952 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3955 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3956 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3959 * ScanType: ACTIVE, PASSIVE
3960 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3961 * a Probe frame during active scanning
3963 * MinChannelTime (>= ProbeDelay), in TU
3964 * MaxChannelTime: (>= MinChannelTime), in TU
3967 /* MLME-SCAN.confirm
3969 * ResultCode: SUCCESS, INVALID_PARAMETERS
3972 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3973 if (local->scan_dev == dev)
3978 if (local->ops->hw_scan) {
3979 int rc = local->ops->hw_scan(local_to_hw(local),
3982 local->sta_hw_scanning = 1;
3983 local->scan_dev = dev;
3988 local->sta_sw_scanning = 1;
3991 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3993 /* Don't stop the master interface, otherwise we can't transmit
3995 if (sdata->dev == local->mdev)
3998 netif_stop_queue(sdata->dev);
3999 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
4000 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
4001 ieee80211_send_nullfunc(local, sdata, 1);
4006 local->scan_ssid_len = ssid_len;
4007 memcpy(local->scan_ssid, ssid, ssid_len);
4009 local->scan_ssid_len = 0;
4010 local->scan_state = SCAN_SET_CHANNEL;
4011 local->scan_channel_idx = 0;
4012 local->scan_band = IEEE80211_BAND_2GHZ;
4013 local->scan_dev = dev;
4015 netif_tx_lock_bh(local->mdev);
4016 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4017 local->ops->configure_filter(local_to_hw(local),
4018 FIF_BCN_PRBRESP_PROMISC,
4019 &local->filter_flags,
4020 local->mdev->mc_count,
4021 local->mdev->mc_list);
4022 netif_tx_unlock_bh(local->mdev);
4024 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4025 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4026 IEEE80211_CHANNEL_TIME);
4032 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4034 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4035 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4036 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4038 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4039 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4041 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4042 if (local->scan_dev == dev)
4047 ifsta->scan_ssid_len = ssid_len;
4049 memcpy(ifsta->scan_ssid, ssid, ssid_len);
4050 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4051 queue_work(local->hw.workqueue, &ifsta->work);
4056 ieee80211_sta_scan_result(struct net_device *dev,
4057 struct ieee80211_sta_bss *bss,
4058 char *current_ev, char *end_buf)
4060 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4061 struct iw_event iwe;
4063 if (time_after(jiffies,
4064 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4067 memset(&iwe, 0, sizeof(iwe));
4068 iwe.cmd = SIOCGIWAP;
4069 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4070 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4071 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4074 memset(&iwe, 0, sizeof(iwe));
4075 iwe.cmd = SIOCGIWESSID;
4076 if (bss_mesh_cfg(bss)) {
4077 iwe.u.data.length = bss_mesh_id_len(bss);
4078 iwe.u.data.flags = 1;
4079 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4082 iwe.u.data.length = bss->ssid_len;
4083 iwe.u.data.flags = 1;
4084 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4088 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4089 || bss_mesh_cfg(bss)) {
4090 memset(&iwe, 0, sizeof(iwe));
4091 iwe.cmd = SIOCGIWMODE;
4092 if (bss_mesh_cfg(bss))
4093 iwe.u.mode = IW_MODE_MESH;
4094 else if (bss->capability & WLAN_CAPABILITY_ESS)
4095 iwe.u.mode = IW_MODE_MASTER;
4097 iwe.u.mode = IW_MODE_ADHOC;
4098 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4102 memset(&iwe, 0, sizeof(iwe));
4103 iwe.cmd = SIOCGIWFREQ;
4104 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4106 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4109 memset(&iwe, 0, sizeof(iwe));
4110 iwe.cmd = SIOCGIWFREQ;
4111 iwe.u.freq.m = bss->freq;
4113 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4115 memset(&iwe, 0, sizeof(iwe));
4117 iwe.u.qual.qual = bss->signal;
4118 iwe.u.qual.level = bss->rssi;
4119 iwe.u.qual.noise = bss->noise;
4120 iwe.u.qual.updated = local->wstats_flags;
4121 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4124 memset(&iwe, 0, sizeof(iwe));
4125 iwe.cmd = SIOCGIWENCODE;
4126 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4127 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4129 iwe.u.data.flags = IW_ENCODE_DISABLED;
4130 iwe.u.data.length = 0;
4131 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4133 if (bss && bss->wpa_ie) {
4134 memset(&iwe, 0, sizeof(iwe));
4135 iwe.cmd = IWEVGENIE;
4136 iwe.u.data.length = bss->wpa_ie_len;
4137 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4141 if (bss && bss->rsn_ie) {
4142 memset(&iwe, 0, sizeof(iwe));
4143 iwe.cmd = IWEVGENIE;
4144 iwe.u.data.length = bss->rsn_ie_len;
4145 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4149 if (bss && bss->supp_rates_len > 0) {
4150 /* display all supported rates in readable format */
4151 char *p = current_ev + IW_EV_LCP_LEN;
4154 memset(&iwe, 0, sizeof(iwe));
4155 iwe.cmd = SIOCGIWRATE;
4156 /* Those two flags are ignored... */
4157 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4159 for (i = 0; i < bss->supp_rates_len; i++) {
4160 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4162 p = iwe_stream_add_value(current_ev, p,
4163 end_buf, &iwe, IW_EV_PARAM_LEN);
4170 buf = kmalloc(30, GFP_ATOMIC);
4172 memset(&iwe, 0, sizeof(iwe));
4173 iwe.cmd = IWEVCUSTOM;
4174 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4175 iwe.u.data.length = strlen(buf);
4176 current_ev = iwe_stream_add_point(current_ev, end_buf,
4182 if (bss_mesh_cfg(bss)) {
4184 u8 *cfg = bss_mesh_cfg(bss);
4185 buf = kmalloc(50, GFP_ATOMIC);
4187 memset(&iwe, 0, sizeof(iwe));
4188 iwe.cmd = IWEVCUSTOM;
4189 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4190 iwe.u.data.length = strlen(buf);
4191 current_ev = iwe_stream_add_point(current_ev, end_buf,
4193 sprintf(buf, "Path Selection Protocol ID: "
4194 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4196 iwe.u.data.length = strlen(buf);
4197 current_ev = iwe_stream_add_point(current_ev, end_buf,
4199 sprintf(buf, "Path Selection Metric ID: "
4200 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4202 iwe.u.data.length = strlen(buf);
4203 current_ev = iwe_stream_add_point(current_ev, end_buf,
4205 sprintf(buf, "Congestion Control Mode ID: "
4206 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4208 iwe.u.data.length = strlen(buf);
4209 current_ev = iwe_stream_add_point(current_ev, end_buf,
4211 sprintf(buf, "Channel Precedence: "
4212 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4214 iwe.u.data.length = strlen(buf);
4215 current_ev = iwe_stream_add_point(current_ev, end_buf,
4225 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4227 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4228 char *current_ev = buf;
4229 char *end_buf = buf + len;
4230 struct ieee80211_sta_bss *bss;
4232 spin_lock_bh(&local->sta_bss_lock);
4233 list_for_each_entry(bss, &local->sta_bss_list, list) {
4234 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4235 spin_unlock_bh(&local->sta_bss_lock);
4238 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4241 spin_unlock_bh(&local->sta_bss_lock);
4242 return current_ev - buf;
4246 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4248 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4249 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4250 kfree(ifsta->extra_ie);
4252 ifsta->extra_ie = NULL;
4253 ifsta->extra_ie_len = 0;
4256 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4257 if (!ifsta->extra_ie) {
4258 ifsta->extra_ie_len = 0;
4261 memcpy(ifsta->extra_ie, ie, len);
4262 ifsta->extra_ie_len = len;
4267 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4268 struct sk_buff *skb, u8 *bssid,
4271 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4272 struct sta_info *sta;
4273 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4274 DECLARE_MAC_BUF(mac);
4276 /* TODO: Could consider removing the least recently used entry and
4277 * allow new one to be added. */
4278 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4279 if (net_ratelimit()) {
4280 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4281 "entry %s\n", dev->name, print_mac(mac, addr));
4286 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4287 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
4289 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4293 sta->flags |= WLAN_STA_AUTHORIZED;
4295 sta->supp_rates[local->hw.conf.channel->band] =
4296 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
4298 rate_control_rate_init(sta, local);
4300 if (sta_info_insert(sta))
4307 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4309 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4310 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4312 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4315 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4316 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4319 ieee80211_send_deauth(dev, ifsta, reason);
4320 ieee80211_set_disassoc(dev, ifsta, 1);
4325 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4327 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4328 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4330 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4333 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4336 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4339 ieee80211_send_disassoc(dev, ifsta, reason);
4340 ieee80211_set_disassoc(dev, ifsta, 0);
4344 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4345 enum ieee80211_notification_types notif_type)
4347 struct ieee80211_local *local = hw_to_local(hw);
4348 struct ieee80211_sub_if_data *sdata;
4350 switch (notif_type) {
4351 case IEEE80211_NOTIFY_RE_ASSOC:
4353 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4355 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4356 ieee80211_sta_req_auth(sdata->dev,
4365 EXPORT_SYMBOL(ieee80211_notify_mac);