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 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
551 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
552 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
553 ieee80211_sta_send_associnfo(dev, ifsta);
555 netif_carrier_off(dev);
556 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
557 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
558 ieee80211_reset_erp_info(dev);
560 sdata->bss_conf.assoc_ht = 0;
561 sdata->bss_conf.ht_conf = NULL;
562 sdata->bss_conf.ht_bss_conf = NULL;
564 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
566 ifsta->last_probe = jiffies;
567 ieee80211_led_assoc(local, assoc);
569 sdata->bss_conf.assoc = assoc;
570 ieee80211_bss_info_change_notify(sdata, changed);
573 netif_carrier_on(dev);
575 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
576 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
579 static void ieee80211_set_disassoc(struct net_device *dev,
580 struct ieee80211_if_sta *ifsta, int deauth)
583 ifsta->auth_tries = 0;
584 ifsta->assoc_tries = 0;
585 ieee80211_set_associated(dev, ifsta, 0);
588 void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
591 struct ieee80211_sub_if_data *sdata;
592 struct ieee80211_tx_packet_data *pkt_data;
594 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
595 skb->dev = sdata->local->mdev;
596 skb_set_mac_header(skb, 0);
597 skb_set_network_header(skb, 0);
598 skb_set_transport_header(skb, 0);
600 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
601 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
602 pkt_data->ifindex = sdata->dev->ifindex;
604 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
610 static void ieee80211_send_auth(struct net_device *dev,
611 struct ieee80211_if_sta *ifsta,
612 int transaction, u8 *extra, size_t extra_len,
615 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
617 struct ieee80211_mgmt *mgmt;
619 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
620 sizeof(*mgmt) + 6 + extra_len);
622 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
623 "frame\n", dev->name);
626 skb_reserve(skb, local->hw.extra_tx_headroom);
628 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
629 memset(mgmt, 0, 24 + 6);
630 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
631 IEEE80211_STYPE_AUTH);
633 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
634 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
635 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
636 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
637 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
638 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
639 ifsta->auth_transaction = transaction + 1;
640 mgmt->u.auth.status_code = cpu_to_le16(0);
642 memcpy(skb_put(skb, extra_len), extra, extra_len);
644 ieee80211_sta_tx(dev, skb, encrypt);
648 static void ieee80211_authenticate(struct net_device *dev,
649 struct ieee80211_if_sta *ifsta)
651 DECLARE_MAC_BUF(mac);
654 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
655 printk(KERN_DEBUG "%s: authentication with AP %s"
657 dev->name, print_mac(mac, ifsta->bssid));
658 ifsta->state = IEEE80211_DISABLED;
662 ifsta->state = IEEE80211_AUTHENTICATE;
663 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
664 dev->name, print_mac(mac, ifsta->bssid));
666 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
668 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
671 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
672 struct ieee80211_supported_band *sband,
678 for (i = 0; i < bss->supp_rates_len; i++) {
679 int rate = (bss->supp_rates[i] & 0x7F) * 5;
681 for (j = 0; j < sband->n_bitrates; j++)
682 if (sband->bitrates[j].bitrate == rate) {
692 static void ieee80211_send_assoc(struct net_device *dev,
693 struct ieee80211_if_sta *ifsta)
695 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
697 struct ieee80211_mgmt *mgmt;
699 int i, len, count, rates_len, supp_rates_len;
701 struct ieee80211_sta_bss *bss;
703 struct ieee80211_supported_band *sband;
706 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
707 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
710 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
711 "frame\n", dev->name);
714 skb_reserve(skb, local->hw.extra_tx_headroom);
716 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
718 capab = ifsta->capab;
720 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
721 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
722 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
723 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
724 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
727 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
728 local->hw.conf.channel->center_freq,
729 ifsta->ssid, ifsta->ssid_len);
731 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
732 capab |= WLAN_CAPABILITY_PRIVACY;
737 /* get all rates supported by the device and the AP as
738 * some APs don't like getting a superset of their rates
739 * in the association request (e.g. D-Link DAP 1353 in
741 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
743 ieee80211_rx_bss_put(dev, bss);
746 rates_len = sband->n_bitrates;
749 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
751 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
752 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
753 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
755 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
757 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
758 IEEE80211_STYPE_REASSOC_REQ);
759 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
760 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
761 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
765 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
766 IEEE80211_STYPE_ASSOC_REQ);
767 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
768 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
772 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
773 *pos++ = WLAN_EID_SSID;
774 *pos++ = ifsta->ssid_len;
775 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
777 /* add all rates which were marked to be used above */
778 supp_rates_len = rates_len;
779 if (supp_rates_len > 8)
782 len = sband->n_bitrates;
783 pos = skb_put(skb, supp_rates_len + 2);
784 *pos++ = WLAN_EID_SUPP_RATES;
785 *pos++ = supp_rates_len;
788 for (i = 0; i < sband->n_bitrates; i++) {
789 if (BIT(i) & rates) {
790 int rate = sband->bitrates[i].bitrate;
791 *pos++ = (u8) (rate / 5);
798 pos = skb_put(skb, rates_len - count + 2);
799 *pos++ = WLAN_EID_EXT_SUPP_RATES;
800 *pos++ = rates_len - count;
802 for (i++; i < sband->n_bitrates; i++) {
803 if (BIT(i) & rates) {
804 int rate = sband->bitrates[i].bitrate;
805 *pos++ = (u8) (rate / 5);
810 if (ifsta->extra_ie) {
811 pos = skb_put(skb, ifsta->extra_ie_len);
812 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
815 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
816 pos = skb_put(skb, 9);
817 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
818 *pos++ = 7; /* len */
819 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
822 *pos++ = 2; /* WME */
823 *pos++ = 0; /* WME info */
824 *pos++ = 1; /* WME ver */
827 /* wmm support is a must to HT */
828 if (wmm && sband->ht_info.ht_supported) {
829 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
830 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
831 *pos++ = WLAN_EID_HT_CAPABILITY;
832 *pos++ = sizeof(struct ieee80211_ht_cap);
833 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
834 memcpy(pos, &tmp, sizeof(u16));
836 /* TODO: needs a define here for << 2 */
837 *pos++ = sband->ht_info.ampdu_factor |
838 (sband->ht_info.ampdu_density << 2);
839 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
842 kfree(ifsta->assocreq_ies);
843 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
844 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
845 if (ifsta->assocreq_ies)
846 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
848 ieee80211_sta_tx(dev, skb, 0);
852 static void ieee80211_send_deauth(struct net_device *dev,
853 struct ieee80211_if_sta *ifsta, u16 reason)
855 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
857 struct ieee80211_mgmt *mgmt;
859 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
861 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
862 "frame\n", dev->name);
865 skb_reserve(skb, local->hw.extra_tx_headroom);
867 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
869 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
870 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
871 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
872 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
873 IEEE80211_STYPE_DEAUTH);
875 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
877 ieee80211_sta_tx(dev, skb, 0);
881 static void ieee80211_send_disassoc(struct net_device *dev,
882 struct ieee80211_if_sta *ifsta, u16 reason)
884 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
886 struct ieee80211_mgmt *mgmt;
888 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
890 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
891 "frame\n", dev->name);
894 skb_reserve(skb, local->hw.extra_tx_headroom);
896 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
898 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
899 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
900 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
901 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
902 IEEE80211_STYPE_DISASSOC);
904 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
906 ieee80211_sta_tx(dev, skb, 0);
910 static int ieee80211_privacy_mismatch(struct net_device *dev,
911 struct ieee80211_if_sta *ifsta)
913 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
914 struct ieee80211_sta_bss *bss;
919 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
922 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
923 local->hw.conf.channel->center_freq,
924 ifsta->ssid, ifsta->ssid_len);
928 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
929 wep_privacy = !!ieee80211_sta_wep_configured(dev);
930 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
932 ieee80211_rx_bss_put(dev, bss);
934 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
941 static void ieee80211_associate(struct net_device *dev,
942 struct ieee80211_if_sta *ifsta)
944 DECLARE_MAC_BUF(mac);
946 ifsta->assoc_tries++;
947 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
948 printk(KERN_DEBUG "%s: association with AP %s"
950 dev->name, print_mac(mac, ifsta->bssid));
951 ifsta->state = IEEE80211_DISABLED;
955 ifsta->state = IEEE80211_ASSOCIATE;
956 printk(KERN_DEBUG "%s: associate with AP %s\n",
957 dev->name, print_mac(mac, ifsta->bssid));
958 if (ieee80211_privacy_mismatch(dev, ifsta)) {
959 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
960 "mixed-cell disabled - abort association\n", dev->name);
961 ifsta->state = IEEE80211_DISABLED;
965 ieee80211_send_assoc(dev, ifsta);
967 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
971 static void ieee80211_associated(struct net_device *dev,
972 struct ieee80211_if_sta *ifsta)
974 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
975 struct sta_info *sta;
977 DECLARE_MAC_BUF(mac);
979 /* TODO: start monitoring current AP signal quality and number of
980 * missed beacons. Scan other channels every now and then and search
982 /* TODO: remove expired BSSes */
984 ifsta->state = IEEE80211_ASSOCIATED;
988 sta = sta_info_get(local, ifsta->bssid);
990 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
991 dev->name, print_mac(mac, ifsta->bssid));
995 if (time_after(jiffies,
996 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
997 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
998 printk(KERN_DEBUG "%s: No ProbeResp from "
999 "current AP %s - assume out of "
1001 dev->name, print_mac(mac, ifsta->bssid));
1003 sta_info_unlink(&sta);
1005 ieee80211_send_probe_req(dev, ifsta->bssid,
1007 local->scan_ssid_len);
1008 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1010 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1011 if (time_after(jiffies, ifsta->last_probe +
1012 IEEE80211_PROBE_INTERVAL)) {
1013 ifsta->last_probe = jiffies;
1014 ieee80211_send_probe_req(dev, ifsta->bssid,
1023 if (disassoc && sta)
1024 sta_info_destroy(sta);
1027 ifsta->state = IEEE80211_DISABLED;
1028 ieee80211_set_associated(dev, ifsta, 0);
1030 mod_timer(&ifsta->timer, jiffies +
1031 IEEE80211_MONITORING_INTERVAL);
1036 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1037 u8 *ssid, size_t ssid_len)
1039 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1040 struct ieee80211_supported_band *sband;
1041 struct sk_buff *skb;
1042 struct ieee80211_mgmt *mgmt;
1043 u8 *pos, *supp_rates, *esupp_rates = NULL;
1046 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1048 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1049 "request\n", dev->name);
1052 skb_reserve(skb, local->hw.extra_tx_headroom);
1054 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1055 memset(mgmt, 0, 24);
1056 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1057 IEEE80211_STYPE_PROBE_REQ);
1058 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1060 memcpy(mgmt->da, dst, ETH_ALEN);
1061 memcpy(mgmt->bssid, dst, ETH_ALEN);
1063 memset(mgmt->da, 0xff, ETH_ALEN);
1064 memset(mgmt->bssid, 0xff, ETH_ALEN);
1066 pos = skb_put(skb, 2 + ssid_len);
1067 *pos++ = WLAN_EID_SSID;
1069 memcpy(pos, ssid, ssid_len);
1071 supp_rates = skb_put(skb, 2);
1072 supp_rates[0] = WLAN_EID_SUPP_RATES;
1074 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1076 for (i = 0; i < sband->n_bitrates; i++) {
1077 struct ieee80211_rate *rate = &sband->bitrates[i];
1079 pos = skb_put(skb, 1);
1081 } else if (supp_rates[1] == 8) {
1082 esupp_rates = skb_put(skb, 3);
1083 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1085 pos = &esupp_rates[2];
1087 pos = skb_put(skb, 1);
1090 *pos = rate->bitrate / 5;
1093 ieee80211_sta_tx(dev, skb, 0);
1097 static int ieee80211_sta_wep_configured(struct net_device *dev)
1099 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1100 if (!sdata || !sdata->default_key ||
1101 sdata->default_key->conf.alg != ALG_WEP)
1107 static void ieee80211_auth_completed(struct net_device *dev,
1108 struct ieee80211_if_sta *ifsta)
1110 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
1111 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1112 ieee80211_associate(dev, ifsta);
1116 static void ieee80211_auth_challenge(struct net_device *dev,
1117 struct ieee80211_if_sta *ifsta,
1118 struct ieee80211_mgmt *mgmt,
1122 struct ieee802_11_elems elems;
1124 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1125 pos = mgmt->u.auth.variable;
1126 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1127 if (!elems.challenge) {
1128 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1129 "frame\n", dev->name);
1132 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1133 elems.challenge_len + 2, 1);
1136 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1137 u8 dialog_token, u16 status, u16 policy,
1138 u16 buf_size, u16 timeout)
1140 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1141 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1142 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1143 struct sk_buff *skb;
1144 struct ieee80211_mgmt *mgmt;
1147 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1148 sizeof(mgmt->u.action.u.addba_resp));
1150 printk(KERN_DEBUG "%s: failed to allocate buffer "
1151 "for addba resp frame\n", dev->name);
1155 skb_reserve(skb, local->hw.extra_tx_headroom);
1156 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1157 memset(mgmt, 0, 24);
1158 memcpy(mgmt->da, da, ETH_ALEN);
1159 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1160 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1161 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1163 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1164 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1165 IEEE80211_STYPE_ACTION);
1167 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1168 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1169 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1170 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1172 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1173 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1174 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1176 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1177 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1178 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1180 ieee80211_sta_tx(dev, skb, 0);
1185 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1186 u16 tid, u8 dialog_token, u16 start_seq_num,
1187 u16 agg_size, u16 timeout)
1189 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1190 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1191 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1192 struct sk_buff *skb;
1193 struct ieee80211_mgmt *mgmt;
1196 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1197 sizeof(mgmt->u.action.u.addba_req));
1201 printk(KERN_ERR "%s: failed to allocate buffer "
1202 "for addba request frame\n", dev->name);
1205 skb_reserve(skb, local->hw.extra_tx_headroom);
1206 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1207 memset(mgmt, 0, 24);
1208 memcpy(mgmt->da, da, ETH_ALEN);
1209 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1210 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1211 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1213 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1215 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1216 IEEE80211_STYPE_ACTION);
1218 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1220 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1221 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1223 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1224 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1225 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1226 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1228 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1230 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1231 mgmt->u.action.u.addba_req.start_seq_num =
1232 cpu_to_le16(start_seq_num << 4);
1234 ieee80211_sta_tx(dev, skb, 0);
1237 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1238 struct ieee80211_mgmt *mgmt,
1241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1242 struct ieee80211_hw *hw = &local->hw;
1243 struct ieee80211_conf *conf = &hw->conf;
1244 struct sta_info *sta;
1245 struct tid_ampdu_rx *tid_agg_rx;
1246 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1248 int ret = -EOPNOTSUPP;
1249 DECLARE_MAC_BUF(mac);
1253 sta = sta_info_get(local, mgmt->sa);
1259 /* extract session parameters from addba request frame */
1260 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1261 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1263 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1265 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1266 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1267 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1268 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1270 status = WLAN_STATUS_REQUEST_DECLINED;
1272 /* sanity check for incoming parameters:
1273 * check if configuration can support the BA policy
1274 * and if buffer size does not exceeds max value */
1275 if (((ba_policy != 1)
1276 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1277 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1278 status = WLAN_STATUS_INVALID_QOS_PARAM;
1279 #ifdef CONFIG_MAC80211_HT_DEBUG
1280 if (net_ratelimit())
1281 printk(KERN_DEBUG "AddBA Req with bad params from "
1282 "%s on tid %u. policy %d, buffer size %d\n",
1283 print_mac(mac, mgmt->sa), tid, ba_policy,
1285 #endif /* CONFIG_MAC80211_HT_DEBUG */
1288 /* determine default buffer size */
1289 if (buf_size == 0) {
1290 struct ieee80211_supported_band *sband;
1292 sband = local->hw.wiphy->bands[conf->channel->band];
1293 buf_size = IEEE80211_MIN_AMPDU_BUF;
1294 buf_size = buf_size << sband->ht_info.ampdu_factor;
1298 /* examine state machine */
1299 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1301 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1302 #ifdef CONFIG_MAC80211_HT_DEBUG
1303 if (net_ratelimit())
1304 printk(KERN_DEBUG "unexpected AddBA Req from "
1306 print_mac(mac, mgmt->sa), tid);
1307 #endif /* CONFIG_MAC80211_HT_DEBUG */
1311 /* prepare A-MPDU MLME for Rx aggregation */
1312 sta->ampdu_mlme.tid_rx[tid] =
1313 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1314 if (!sta->ampdu_mlme.tid_rx[tid]) {
1315 if (net_ratelimit())
1316 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1321 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1322 sta_rx_agg_session_timer_expired;
1323 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1324 (unsigned long)&sta->timer_to_tid[tid];
1325 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1327 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1329 /* prepare reordering buffer */
1330 tid_agg_rx->reorder_buf =
1331 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1332 if (!tid_agg_rx->reorder_buf) {
1333 if (net_ratelimit())
1334 printk(KERN_ERR "can not allocate reordering buffer "
1335 "to tid %d\n", tid);
1336 kfree(sta->ampdu_mlme.tid_rx[tid]);
1339 memset(tid_agg_rx->reorder_buf, 0,
1340 buf_size * sizeof(struct sk_buff *));
1342 if (local->ops->ampdu_action)
1343 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1344 sta->addr, tid, &start_seq_num);
1345 #ifdef CONFIG_MAC80211_HT_DEBUG
1346 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1347 #endif /* CONFIG_MAC80211_HT_DEBUG */
1350 kfree(tid_agg_rx->reorder_buf);
1352 sta->ampdu_mlme.tid_rx[tid] = NULL;
1356 /* change state and send addba resp */
1357 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1358 tid_agg_rx->dialog_token = dialog_token;
1359 tid_agg_rx->ssn = start_seq_num;
1360 tid_agg_rx->head_seq_num = start_seq_num;
1361 tid_agg_rx->buf_size = buf_size;
1362 tid_agg_rx->timeout = timeout;
1363 tid_agg_rx->stored_mpdu_num = 0;
1364 status = WLAN_STATUS_SUCCESS;
1366 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1369 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1370 dialog_token, status, 1, buf_size, timeout);
1374 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1375 struct ieee80211_mgmt *mgmt,
1378 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1379 struct ieee80211_hw *hw = &local->hw;
1380 struct sta_info *sta;
1387 sta = sta_info_get(local, mgmt->sa);
1393 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1394 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1396 state = &sta->ampdu_mlme.tid_state_tx[tid];
1398 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1400 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1401 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1402 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1404 goto addba_resp_exit;
1407 if (mgmt->u.action.u.addba_resp.dialog_token !=
1408 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1409 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1410 #ifdef CONFIG_MAC80211_HT_DEBUG
1411 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1412 #endif /* CONFIG_MAC80211_HT_DEBUG */
1413 goto addba_resp_exit;
1416 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1417 #ifdef CONFIG_MAC80211_HT_DEBUG
1418 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1419 #endif /* CONFIG_MAC80211_HT_DEBUG */
1420 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1421 == WLAN_STATUS_SUCCESS) {
1422 if (*state & HT_ADDBA_RECEIVED_MSK)
1423 printk(KERN_DEBUG "double addBA response\n");
1425 *state |= HT_ADDBA_RECEIVED_MSK;
1426 sta->ampdu_mlme.addba_req_num[tid] = 0;
1428 if (*state == HT_AGG_STATE_OPERATIONAL) {
1429 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1430 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1433 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1434 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1436 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1438 sta->ampdu_mlme.addba_req_num[tid]++;
1439 /* this will allow the state check in stop_BA_session */
1440 *state = HT_AGG_STATE_OPERATIONAL;
1441 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1442 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1443 WLAN_BACK_INITIATOR);
1450 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1451 u16 initiator, u16 reason_code)
1453 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1454 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1455 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1456 struct sk_buff *skb;
1457 struct ieee80211_mgmt *mgmt;
1460 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1461 sizeof(mgmt->u.action.u.delba));
1464 printk(KERN_ERR "%s: failed to allocate buffer "
1465 "for delba frame\n", dev->name);
1469 skb_reserve(skb, local->hw.extra_tx_headroom);
1470 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1471 memset(mgmt, 0, 24);
1472 memcpy(mgmt->da, da, ETH_ALEN);
1473 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1474 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1475 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1477 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1478 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1479 IEEE80211_STYPE_ACTION);
1481 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1483 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1484 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1485 params = (u16)(initiator << 11); /* bit 11 initiator */
1486 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1488 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1489 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1491 ieee80211_sta_tx(dev, skb, 0);
1494 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1495 u16 initiator, u16 reason)
1497 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1498 struct ieee80211_hw *hw = &local->hw;
1499 struct sta_info *sta;
1501 DECLARE_MAC_BUF(mac);
1505 sta = sta_info_get(local, ra);
1511 /* check if TID is in operational state */
1512 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1513 if (sta->ampdu_mlme.tid_state_rx[tid]
1514 != HT_AGG_STATE_OPERATIONAL) {
1515 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1519 sta->ampdu_mlme.tid_state_rx[tid] =
1520 HT_AGG_STATE_REQ_STOP_BA_MSK |
1521 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1522 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1524 /* stop HW Rx aggregation. ampdu_action existence
1525 * already verified in session init so we add the BUG_ON */
1526 BUG_ON(!local->ops->ampdu_action);
1528 #ifdef CONFIG_MAC80211_HT_DEBUG
1529 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1530 print_mac(mac, ra), tid);
1531 #endif /* CONFIG_MAC80211_HT_DEBUG */
1533 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1536 printk(KERN_DEBUG "HW problem - can not stop rx "
1537 "aggergation for tid %d\n", tid);
1539 /* shutdown timer has not expired */
1540 if (initiator != WLAN_BACK_TIMER)
1541 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1543 /* check if this is a self generated aggregation halt */
1544 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1545 ieee80211_send_delba(dev, ra, tid, 0, reason);
1547 /* free the reordering buffer */
1548 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1549 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1550 /* release the reordered frames */
1551 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1552 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1553 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1556 /* free resources */
1557 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1558 kfree(sta->ampdu_mlme.tid_rx[tid]);
1559 sta->ampdu_mlme.tid_rx[tid] = NULL;
1560 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1566 static void ieee80211_sta_process_delba(struct net_device *dev,
1567 struct ieee80211_mgmt *mgmt, size_t len)
1569 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1570 struct sta_info *sta;
1573 DECLARE_MAC_BUF(mac);
1577 sta = sta_info_get(local, mgmt->sa);
1583 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1584 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1585 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1587 #ifdef CONFIG_MAC80211_HT_DEBUG
1588 if (net_ratelimit())
1589 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1590 print_mac(mac, mgmt->sa),
1591 initiator ? "initiator" : "recipient", tid,
1592 mgmt->u.action.u.delba.reason_code);
1593 #endif /* CONFIG_MAC80211_HT_DEBUG */
1595 if (initiator == WLAN_BACK_INITIATOR)
1596 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1597 WLAN_BACK_INITIATOR, 0);
1598 else { /* WLAN_BACK_RECIPIENT */
1599 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1600 sta->ampdu_mlme.tid_state_tx[tid] =
1601 HT_AGG_STATE_OPERATIONAL;
1602 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1603 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1604 WLAN_BACK_RECIPIENT);
1610 * After sending add Block Ack request we activated a timer until
1611 * add Block Ack response will arrive from the recipient.
1612 * If this timer expires sta_addba_resp_timer_expired will be executed.
1614 void sta_addba_resp_timer_expired(unsigned long data)
1616 /* not an elegant detour, but there is no choice as the timer passes
1617 * only one argument, and both sta_info and TID are needed, so init
1618 * flow in sta_info_create gives the TID as data, while the timer_to_id
1619 * array gives the sta through container_of */
1620 u16 tid = *(u8 *)data;
1621 struct sta_info *temp_sta = container_of((void *)data,
1622 struct sta_info, timer_to_tid[tid]);
1624 struct ieee80211_local *local = temp_sta->local;
1625 struct ieee80211_hw *hw = &local->hw;
1626 struct sta_info *sta;
1631 sta = sta_info_get(local, temp_sta->addr);
1637 state = &sta->ampdu_mlme.tid_state_tx[tid];
1638 /* check if the TID waits for addBA response */
1639 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1640 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1641 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1642 *state = HT_AGG_STATE_IDLE;
1643 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1644 "expecting addBA response there", tid);
1645 goto timer_expired_exit;
1648 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1650 /* go through the state check in stop_BA_session */
1651 *state = HT_AGG_STATE_OPERATIONAL;
1652 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1653 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1654 WLAN_BACK_INITIATOR);
1661 * After accepting the AddBA Request we activated a timer,
1662 * resetting it after each frame that arrives from the originator.
1663 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1665 void sta_rx_agg_session_timer_expired(unsigned long data)
1667 /* not an elegant detour, but there is no choice as the timer passes
1668 * only one argument, and various sta_info are needed here, so init
1669 * flow in sta_info_create gives the TID as data, while the timer_to_id
1670 * array gives the sta through container_of */
1671 u8 *ptid = (u8 *)data;
1672 u8 *timer_to_id = ptid - *ptid;
1673 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1676 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1677 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1678 (u16)*ptid, WLAN_BACK_TIMER,
1679 WLAN_REASON_QSTA_TIMEOUT);
1682 void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1684 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1687 for (i = 0; i < STA_TID_NUM; i++) {
1688 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1689 WLAN_BACK_INITIATOR);
1690 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1691 WLAN_BACK_RECIPIENT,
1692 WLAN_REASON_QSTA_LEAVE_QBSS);
1696 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1697 struct ieee80211_if_sta *ifsta,
1698 struct ieee80211_mgmt *mgmt,
1701 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1702 u16 auth_alg, auth_transaction, status_code;
1703 DECLARE_MAC_BUF(mac);
1705 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1706 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
1707 printk(KERN_DEBUG "%s: authentication frame received from "
1708 "%s, but not in authenticate state - ignored\n",
1709 dev->name, print_mac(mac, mgmt->sa));
1714 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
1715 "received from %s - ignored\n",
1716 dev->name, len, print_mac(mac, mgmt->sa));
1720 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1721 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1722 printk(KERN_DEBUG "%s: authentication frame received from "
1723 "unknown AP (SA=%s BSSID=%s) - "
1724 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1725 print_mac(mac, mgmt->bssid));
1729 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1730 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1731 printk(KERN_DEBUG "%s: authentication frame received from "
1732 "unknown BSSID (SA=%s BSSID=%s) - "
1733 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1734 print_mac(mac, mgmt->bssid));
1738 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1739 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1740 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1742 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
1743 "transaction=%d status=%d)\n",
1744 dev->name, print_mac(mac, mgmt->sa), auth_alg,
1745 auth_transaction, status_code);
1747 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1748 /* IEEE 802.11 standard does not require authentication in IBSS
1749 * networks and most implementations do not seem to use it.
1750 * However, try to reply to authentication attempts if someone
1751 * has actually implemented this.
1752 * TODO: Could implement shared key authentication. */
1753 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1754 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1755 "frame (alg=%d transaction=%d)\n",
1756 dev->name, auth_alg, auth_transaction);
1759 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1762 if (auth_alg != ifsta->auth_alg ||
1763 auth_transaction != ifsta->auth_transaction) {
1764 printk(KERN_DEBUG "%s: unexpected authentication frame "
1765 "(alg=%d transaction=%d)\n",
1766 dev->name, auth_alg, auth_transaction);
1770 if (status_code != WLAN_STATUS_SUCCESS) {
1771 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1772 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1773 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1775 const int num_algs = ARRAY_SIZE(algs);
1777 algs[0] = algs[1] = algs[2] = 0xff;
1778 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1779 algs[0] = WLAN_AUTH_OPEN;
1780 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1781 algs[1] = WLAN_AUTH_SHARED_KEY;
1782 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1783 algs[2] = WLAN_AUTH_LEAP;
1784 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1786 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1790 for (i = 0; i < num_algs; i++) {
1792 if (pos >= num_algs)
1794 if (algs[pos] == ifsta->auth_alg ||
1797 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1798 !ieee80211_sta_wep_configured(dev))
1800 ifsta->auth_alg = algs[pos];
1801 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1803 dev->name, ifsta->auth_alg);
1810 switch (ifsta->auth_alg) {
1811 case WLAN_AUTH_OPEN:
1812 case WLAN_AUTH_LEAP:
1813 ieee80211_auth_completed(dev, ifsta);
1815 case WLAN_AUTH_SHARED_KEY:
1816 if (ifsta->auth_transaction == 4)
1817 ieee80211_auth_completed(dev, ifsta);
1819 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1825 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1826 struct ieee80211_if_sta *ifsta,
1827 struct ieee80211_mgmt *mgmt,
1831 DECLARE_MAC_BUF(mac);
1834 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1835 "received from %s - ignored\n",
1836 dev->name, len, print_mac(mac, mgmt->sa));
1840 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1841 printk(KERN_DEBUG "%s: deauthentication frame received from "
1842 "unknown AP (SA=%s BSSID=%s) - "
1843 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1844 print_mac(mac, mgmt->bssid));
1848 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1850 printk(KERN_DEBUG "%s: RX deauthentication from %s"
1852 dev->name, print_mac(mac, mgmt->sa), reason_code);
1854 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1855 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1858 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1859 ifsta->state == IEEE80211_ASSOCIATE ||
1860 ifsta->state == IEEE80211_ASSOCIATED) {
1861 ifsta->state = IEEE80211_AUTHENTICATE;
1862 mod_timer(&ifsta->timer, jiffies +
1863 IEEE80211_RETRY_AUTH_INTERVAL);
1866 ieee80211_set_disassoc(dev, ifsta, 1);
1867 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1871 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1872 struct ieee80211_if_sta *ifsta,
1873 struct ieee80211_mgmt *mgmt,
1877 DECLARE_MAC_BUF(mac);
1880 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1881 "received from %s - ignored\n",
1882 dev->name, len, print_mac(mac, mgmt->sa));
1886 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1887 printk(KERN_DEBUG "%s: disassociation frame received from "
1888 "unknown AP (SA=%s BSSID=%s) - "
1889 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1890 print_mac(mac, mgmt->bssid));
1894 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1896 printk(KERN_DEBUG "%s: RX disassociation from %s"
1898 dev->name, print_mac(mac, mgmt->sa), reason_code);
1900 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1901 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1903 if (ifsta->state == IEEE80211_ASSOCIATED) {
1904 ifsta->state = IEEE80211_ASSOCIATE;
1905 mod_timer(&ifsta->timer, jiffies +
1906 IEEE80211_RETRY_AUTH_INTERVAL);
1909 ieee80211_set_disassoc(dev, ifsta, 0);
1913 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1914 struct ieee80211_if_sta *ifsta,
1915 struct ieee80211_mgmt *mgmt,
1919 struct ieee80211_local *local = sdata->local;
1920 struct net_device *dev = sdata->dev;
1921 struct ieee80211_supported_band *sband;
1922 struct sta_info *sta;
1923 u64 rates, basic_rates;
1924 u16 capab_info, status_code, aid;
1925 struct ieee802_11_elems elems;
1926 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1929 DECLARE_MAC_BUF(mac);
1930 bool have_higher_than_11mbit = false;
1932 /* AssocResp and ReassocResp have identical structure, so process both
1933 * of them in this function. */
1935 if (ifsta->state != IEEE80211_ASSOCIATE) {
1936 printk(KERN_DEBUG "%s: association frame received from "
1937 "%s, but not in associate state - ignored\n",
1938 dev->name, print_mac(mac, mgmt->sa));
1943 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1944 "received from %s - ignored\n",
1945 dev->name, len, print_mac(mac, mgmt->sa));
1949 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1950 printk(KERN_DEBUG "%s: association frame received from "
1951 "unknown AP (SA=%s BSSID=%s) - "
1952 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1953 print_mac(mac, mgmt->bssid));
1957 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1958 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1959 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1961 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1962 "status=%d aid=%d)\n",
1963 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1964 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1966 if (status_code != WLAN_STATUS_SUCCESS) {
1967 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1968 dev->name, status_code);
1969 /* if this was a reassociation, ensure we try a "full"
1970 * association next time. This works around some broken APs
1971 * which do not correctly reject reassociation requests. */
1972 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1976 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1977 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1978 "set\n", dev->name, aid);
1979 aid &= ~(BIT(15) | BIT(14));
1981 pos = mgmt->u.assoc_resp.variable;
1982 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1984 if (!elems.supp_rates) {
1985 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1990 printk(KERN_DEBUG "%s: associated\n", dev->name);
1992 ifsta->ap_capab = capab_info;
1994 kfree(ifsta->assocresp_ies);
1995 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1996 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1997 if (ifsta->assocresp_ies)
1998 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2002 /* Add STA entry for the AP */
2003 sta = sta_info_get(local, ifsta->bssid);
2005 struct ieee80211_sta_bss *bss;
2008 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2010 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2011 " the AP\n", dev->name);
2015 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
2016 local->hw.conf.channel->center_freq,
2017 ifsta->ssid, ifsta->ssid_len);
2019 sta->last_rssi = bss->rssi;
2020 sta->last_signal = bss->signal;
2021 sta->last_noise = bss->noise;
2022 ieee80211_rx_bss_put(dev, bss);
2025 err = sta_info_insert(sta);
2027 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2028 " the AP (error %d)\n", dev->name, err);
2035 * FIXME: Do we really need to update the sta_info's information here?
2036 * We already know about the AP (we found it in our list) so it
2037 * should already be filled with the right info, no?
2038 * As is stands, all this is racy because typically we assume
2039 * the information that is filled in here (except flags) doesn't
2040 * change while a STA structure is alive. As such, it should move
2041 * to between the sta_info_alloc() and sta_info_insert() above.
2044 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2045 WLAN_STA_AUTHORIZED;
2049 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2051 for (i = 0; i < elems.supp_rates_len; i++) {
2052 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2055 have_higher_than_11mbit = true;
2057 for (j = 0; j < sband->n_bitrates; j++) {
2058 if (sband->bitrates[j].bitrate == rate)
2060 if (elems.supp_rates[i] & 0x80)
2061 basic_rates |= BIT(j);
2065 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2066 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2069 have_higher_than_11mbit = true;
2071 for (j = 0; j < sband->n_bitrates; j++) {
2072 if (sband->bitrates[j].bitrate == rate)
2074 if (elems.ext_supp_rates[i] & 0x80)
2075 basic_rates |= BIT(j);
2079 sta->supp_rates[local->hw.conf.channel->band] = rates;
2080 sdata->basic_rates = basic_rates;
2082 /* cf. IEEE 802.11 9.2.12 */
2083 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2084 have_higher_than_11mbit)
2085 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2087 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2089 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
2090 struct ieee80211_ht_bss_info bss_info;
2091 ieee80211_ht_cap_ie_to_ht_info(
2092 (struct ieee80211_ht_cap *)
2093 elems.ht_cap_elem, &sta->ht_info);
2094 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2095 (struct ieee80211_ht_addt_info *)
2096 elems.ht_info_elem, &bss_info);
2097 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2100 rate_control_rate_init(sta, local);
2102 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2103 sta->flags |= WLAN_STA_WME;
2105 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2106 elems.wmm_param_len);
2110 /* set AID and assoc capability,
2111 * ieee80211_set_associated() will tell the driver */
2112 bss_conf->aid = aid;
2113 bss_conf->assoc_capability = capab_info;
2114 ieee80211_set_associated(dev, ifsta, 1);
2116 ieee80211_associated(dev, ifsta);
2120 /* Caller must hold local->sta_bss_lock */
2121 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2122 struct ieee80211_sta_bss *bss)
2124 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2127 if (bss_mesh_cfg(bss))
2128 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2129 bss_mesh_id_len(bss));
2131 hash_idx = STA_HASH(bss->bssid);
2133 bss->hnext = local->sta_bss_hash[hash_idx];
2134 local->sta_bss_hash[hash_idx] = bss;
2138 /* Caller must hold local->sta_bss_lock */
2139 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2140 struct ieee80211_sta_bss *bss)
2142 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2143 struct ieee80211_sta_bss *b, *prev = NULL;
2144 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2148 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2151 prev->hnext = bss->hnext;
2160 static struct ieee80211_sta_bss *
2161 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
2162 u8 *ssid, u8 ssid_len)
2164 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2165 struct ieee80211_sta_bss *bss;
2167 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2170 atomic_inc(&bss->users);
2171 atomic_inc(&bss->users);
2172 memcpy(bss->bssid, bssid, ETH_ALEN);
2174 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2175 memcpy(bss->ssid, ssid, ssid_len);
2176 bss->ssid_len = ssid_len;
2179 spin_lock_bh(&local->sta_bss_lock);
2180 /* TODO: order by RSSI? */
2181 list_add_tail(&bss->list, &local->sta_bss_list);
2182 __ieee80211_rx_bss_hash_add(dev, bss);
2183 spin_unlock_bh(&local->sta_bss_lock);
2187 static struct ieee80211_sta_bss *
2188 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
2189 u8 *ssid, u8 ssid_len)
2191 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2192 struct ieee80211_sta_bss *bss;
2194 spin_lock_bh(&local->sta_bss_lock);
2195 bss = local->sta_bss_hash[STA_HASH(bssid)];
2197 if (!bss_mesh_cfg(bss) &&
2198 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2199 bss->freq == freq &&
2200 bss->ssid_len == ssid_len &&
2201 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2202 atomic_inc(&bss->users);
2207 spin_unlock_bh(&local->sta_bss_lock);
2211 #ifdef CONFIG_MAC80211_MESH
2212 static struct ieee80211_sta_bss *
2213 ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2214 u8 *mesh_cfg, int freq)
2216 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2217 struct ieee80211_sta_bss *bss;
2219 spin_lock_bh(&local->sta_bss_lock);
2220 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2222 if (bss_mesh_cfg(bss) &&
2223 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2224 bss->freq == freq &&
2225 mesh_id_len == bss->mesh_id_len &&
2226 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2228 atomic_inc(&bss->users);
2233 spin_unlock_bh(&local->sta_bss_lock);
2237 static struct ieee80211_sta_bss *
2238 ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2239 u8 *mesh_cfg, int mesh_config_len, int freq)
2241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2242 struct ieee80211_sta_bss *bss;
2244 if (mesh_config_len != MESH_CFG_LEN)
2247 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2251 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2252 if (!bss->mesh_cfg) {
2257 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2258 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2259 if (!bss->mesh_id) {
2260 kfree(bss->mesh_cfg);
2264 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2267 atomic_inc(&bss->users);
2268 atomic_inc(&bss->users);
2269 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2270 bss->mesh_id_len = mesh_id_len;
2272 spin_lock_bh(&local->sta_bss_lock);
2273 /* TODO: order by RSSI? */
2274 list_add_tail(&bss->list, &local->sta_bss_list);
2275 __ieee80211_rx_bss_hash_add(dev, bss);
2276 spin_unlock_bh(&local->sta_bss_lock);
2281 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2287 kfree(bss_mesh_id(bss));
2288 kfree(bss_mesh_cfg(bss));
2293 static void ieee80211_rx_bss_put(struct net_device *dev,
2294 struct ieee80211_sta_bss *bss)
2296 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2299 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2304 __ieee80211_rx_bss_hash_del(dev, bss);
2305 list_del(&bss->list);
2306 spin_unlock_bh(&local->sta_bss_lock);
2307 ieee80211_rx_bss_free(bss);
2311 void ieee80211_rx_bss_list_init(struct net_device *dev)
2313 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2314 spin_lock_init(&local->sta_bss_lock);
2315 INIT_LIST_HEAD(&local->sta_bss_list);
2319 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2321 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2322 struct ieee80211_sta_bss *bss, *tmp;
2324 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2325 ieee80211_rx_bss_put(dev, bss);
2329 static int ieee80211_sta_join_ibss(struct net_device *dev,
2330 struct ieee80211_if_sta *ifsta,
2331 struct ieee80211_sta_bss *bss)
2333 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2334 int res, rates, i, j;
2335 struct sk_buff *skb;
2336 struct ieee80211_mgmt *mgmt;
2337 struct ieee80211_tx_control control;
2338 struct rate_selection ratesel;
2340 struct ieee80211_sub_if_data *sdata;
2341 struct ieee80211_supported_band *sband;
2342 union iwreq_data wrqu;
2344 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2346 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2348 /* Remove possible STA entries from other IBSS networks. */
2349 sta_info_flush_delayed(sdata);
2351 if (local->ops->reset_tsf) {
2352 /* Reset own TSF to allow time synchronization work. */
2353 local->ops->reset_tsf(local_to_hw(local));
2355 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2356 res = ieee80211_if_config(dev);
2360 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2362 sdata->drop_unencrypted = bss->capability &
2363 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2365 res = ieee80211_set_freq(dev, bss->freq);
2370 /* Set beacon template */
2371 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2376 skb_reserve(skb, local->hw.extra_tx_headroom);
2378 mgmt = (struct ieee80211_mgmt *)
2379 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2380 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2381 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2382 IEEE80211_STYPE_BEACON);
2383 memset(mgmt->da, 0xff, ETH_ALEN);
2384 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2385 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2386 mgmt->u.beacon.beacon_int =
2387 cpu_to_le16(local->hw.conf.beacon_int);
2388 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2390 pos = skb_put(skb, 2 + ifsta->ssid_len);
2391 *pos++ = WLAN_EID_SSID;
2392 *pos++ = ifsta->ssid_len;
2393 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2395 rates = bss->supp_rates_len;
2398 pos = skb_put(skb, 2 + rates);
2399 *pos++ = WLAN_EID_SUPP_RATES;
2401 memcpy(pos, bss->supp_rates, rates);
2403 if (bss->band == IEEE80211_BAND_2GHZ) {
2404 pos = skb_put(skb, 2 + 1);
2405 *pos++ = WLAN_EID_DS_PARAMS;
2407 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2410 pos = skb_put(skb, 2 + 2);
2411 *pos++ = WLAN_EID_IBSS_PARAMS;
2413 /* FIX: set ATIM window based on scan results */
2417 if (bss->supp_rates_len > 8) {
2418 rates = bss->supp_rates_len - 8;
2419 pos = skb_put(skb, 2 + rates);
2420 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2422 memcpy(pos, &bss->supp_rates[8], rates);
2425 memset(&control, 0, sizeof(control));
2426 rate_control_get_rate(dev, sband, skb, &ratesel);
2427 if (!ratesel.rate) {
2428 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2429 "for IBSS beacon\n", dev->name);
2432 control.vif = &sdata->vif;
2433 control.tx_rate = ratesel.rate;
2434 if (sdata->bss_conf.use_short_preamble &&
2435 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2436 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2437 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2438 control.flags |= IEEE80211_TXCTL_NO_ACK;
2439 control.retry_limit = 1;
2441 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2442 if (ifsta->probe_resp) {
2443 mgmt = (struct ieee80211_mgmt *)
2444 ifsta->probe_resp->data;
2445 mgmt->frame_control =
2446 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2447 IEEE80211_STYPE_PROBE_RESP);
2449 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2450 "template for IBSS\n", dev->name);
2453 if (local->ops->beacon_update &&
2454 local->ops->beacon_update(local_to_hw(local),
2455 skb, &control) == 0) {
2456 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2457 "template\n", dev->name);
2462 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2463 for (i = 0; i < bss->supp_rates_len; i++) {
2464 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2465 for (j = 0; j < sband->n_bitrates; j++)
2466 if (sband->bitrates[j].bitrate == bitrate)
2469 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2471 ieee80211_sta_def_wmm_params(dev, bss, 1);
2475 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2476 "template\n", dev->name);
2480 ifsta->state = IEEE80211_IBSS_JOINED;
2481 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2483 memset(&wrqu, 0, sizeof(wrqu));
2484 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2485 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
2490 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2491 struct ieee802_11_elems *elems,
2492 enum ieee80211_band band)
2494 struct ieee80211_supported_band *sband;
2495 struct ieee80211_rate *bitrates;
2499 sband = local->hw.wiphy->bands[band];
2503 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2506 bitrates = sband->bitrates;
2507 num_rates = sband->n_bitrates;
2509 for (i = 0; i < elems->supp_rates_len +
2510 elems->ext_supp_rates_len; i++) {
2513 if (i < elems->supp_rates_len)
2514 rate = elems->supp_rates[i];
2515 else if (elems->ext_supp_rates)
2516 rate = elems->ext_supp_rates
2517 [i - elems->supp_rates_len];
2518 own_rate = 5 * (rate & 0x7f);
2519 for (j = 0; j < num_rates; j++)
2520 if (bitrates[j].bitrate == own_rate)
2521 supp_rates |= BIT(j);
2527 static void ieee80211_rx_bss_info(struct net_device *dev,
2528 struct ieee80211_mgmt *mgmt,
2530 struct ieee80211_rx_status *rx_status,
2533 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2534 struct ieee802_11_elems elems;
2537 struct ieee80211_sta_bss *bss;
2538 struct sta_info *sta;
2539 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2540 u64 beacon_timestamp, rx_timestamp;
2541 struct ieee80211_channel *channel;
2542 DECLARE_MAC_BUF(mac);
2543 DECLARE_MAC_BUF(mac2);
2545 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2546 return; /* ignore ProbeResp to foreign address */
2549 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
2550 dev->name, beacon ? "Beacon" : "Probe Response",
2551 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
2554 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2558 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2559 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2561 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2562 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2563 u64 rates = ieee80211_sta_get_rates(local, &elems,
2566 mesh_neighbour_update(mgmt->sa, rates, dev,
2567 mesh_peer_accepts_plinks(&elems, dev));
2572 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
2573 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2574 (sta = sta_info_get(local, mgmt->sa))) {
2576 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2579 prev_rates = sta->supp_rates[rx_status->band];
2580 sta->supp_rates[rx_status->band] &= supp_rates;
2581 if (sta->supp_rates[rx_status->band] == 0) {
2582 /* No matching rates - this should not really happen.
2583 * Make sure that at least one rate is marked
2584 * supported to avoid issues with TX rate ctrl. */
2585 sta->supp_rates[rx_status->band] =
2586 sdata->u.sta.supp_rates_bits[rx_status->band];
2588 if (sta->supp_rates[rx_status->band] != prev_rates) {
2589 printk(KERN_DEBUG "%s: updated supp_rates set for "
2590 "%s based on beacon info (0x%llx & 0x%llx -> "
2592 dev->name, print_mac(mac, sta->addr),
2593 (unsigned long long) prev_rates,
2594 (unsigned long long) supp_rates,
2595 (unsigned long long) sta->supp_rates[rx_status->band]);
2601 if (elems.ds_params && elems.ds_params_len == 1)
2602 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
2604 freq = rx_status->freq;
2606 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2608 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2611 #ifdef CONFIG_MAC80211_MESH
2612 if (elems.mesh_config)
2613 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2614 elems.mesh_id_len, elems.mesh_config, freq);
2617 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2618 elems.ssid, elems.ssid_len);
2620 #ifdef CONFIG_MAC80211_MESH
2621 if (elems.mesh_config)
2622 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2623 elems.mesh_id_len, elems.mesh_config,
2624 elems.mesh_config_len, freq);
2627 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2628 elems.ssid, elems.ssid_len);
2633 /* TODO: order by RSSI? */
2634 spin_lock_bh(&local->sta_bss_lock);
2635 list_move_tail(&bss->list, &local->sta_bss_list);
2636 spin_unlock_bh(&local->sta_bss_lock);
2640 /* save the ERP value so that it is available at association time */
2641 if (elems.erp_info && elems.erp_info_len >= 1) {
2642 bss->erp_value = elems.erp_info[0];
2643 bss->has_erp_value = 1;
2646 if (elems.ht_cap_elem &&
2647 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2648 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2650 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2652 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2653 elems.ht_cap_elem_len + 2);
2654 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2657 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2663 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2664 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2666 bss->supp_rates_len = 0;
2667 if (elems.supp_rates) {
2668 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2669 if (clen > elems.supp_rates_len)
2670 clen = elems.supp_rates_len;
2671 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2673 bss->supp_rates_len += clen;
2675 if (elems.ext_supp_rates) {
2676 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2677 if (clen > elems.ext_supp_rates_len)
2678 clen = elems.ext_supp_rates_len;
2679 memcpy(&bss->supp_rates[bss->supp_rates_len],
2680 elems.ext_supp_rates, clen);
2681 bss->supp_rates_len += clen;
2684 bss->band = rx_status->band;
2686 bss->timestamp = beacon_timestamp;
2687 bss->last_update = jiffies;
2688 bss->rssi = rx_status->ssi;
2689 bss->signal = rx_status->signal;
2690 bss->noise = rx_status->noise;
2691 if (!beacon && !bss->probe_resp)
2692 bss->probe_resp = true;
2695 * In STA mode, the remaining parameters should not be overridden
2696 * by beacons because they're not necessarily accurate there.
2698 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2699 bss->probe_resp && beacon) {
2700 ieee80211_rx_bss_put(dev, bss);
2705 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2706 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2708 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2710 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2711 bss->wpa_ie_len = elems.wpa_len + 2;
2713 bss->wpa_ie_len = 0;
2714 } else if (!elems.wpa && bss->wpa_ie) {
2717 bss->wpa_ie_len = 0;
2721 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2722 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2724 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2726 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2727 bss->rsn_ie_len = elems.rsn_len + 2;
2729 bss->rsn_ie_len = 0;
2730 } else if (!elems.rsn && bss->rsn_ie) {
2733 bss->rsn_ie_len = 0;
2738 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2742 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2743 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2744 * Alliance (September 1, 2004) is incorporated by reference herein.
2745 * The inclusion of the WMM Parameters in probe responses and
2746 * association responses is mandatory for WMM enabled networks. The
2747 * inclusion of the WMM Parameters in beacons, however, is optional.
2750 if (elems.wmm_param &&
2751 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2752 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2754 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2756 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2757 elems.wmm_param_len + 2);
2758 bss->wmm_ie_len = elems.wmm_param_len + 2;
2760 bss->wmm_ie_len = 0;
2761 } else if (elems.wmm_info &&
2762 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_info_len ||
2763 memcmp(bss->wmm_ie, elems.wmm_info, elems.wmm_info_len))) {
2764 /* As for certain AP's Fifth bit is not set in WMM IE in
2765 * beacon frames.So while parsing the beacon frame the
2766 * wmm_info structure is used instead of wmm_param.
2767 * wmm_info structure was never used to set bss->wmm_ie.
2768 * This code fixes this problem by copying the WME
2769 * information from wmm_info to bss->wmm_ie and enabling
2770 * n-band association.
2773 bss->wmm_ie = kmalloc(elems.wmm_info_len + 2, GFP_ATOMIC);
2775 memcpy(bss->wmm_ie, elems.wmm_info - 2,
2776 elems.wmm_info_len + 2);
2777 bss->wmm_ie_len = elems.wmm_info_len + 2;
2779 bss->wmm_ie_len = 0;
2780 } else if (!elems.wmm_param && !elems.wmm_info && bss->wmm_ie) {
2783 bss->wmm_ie_len = 0;
2786 /* check if we need to merge IBSS */
2787 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2788 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2789 bss->capability & WLAN_CAPABILITY_IBSS &&
2790 bss->freq == local->oper_channel->center_freq &&
2791 elems.ssid_len == sdata->u.sta.ssid_len &&
2792 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2793 if (rx_status->flag & RX_FLAG_TSFT) {
2794 /* in order for correct IBSS merging we need mactime
2796 * since mactime is defined as the time the first data
2797 * symbol of the frame hits the PHY, and the timestamp
2798 * of the beacon is defined as "the time that the data
2799 * symbol containing the first bit of the timestamp is
2800 * transmitted to the PHY plus the transmitting STA’s
2801 * delays through its local PHY from the MAC-PHY
2802 * interface to its interface with the WM"
2803 * (802.11 11.1.2) - equals the time this bit arrives at
2804 * the receiver - we have to take into account the
2805 * offset between the two.
2806 * e.g: at 1 MBit that means mactime is 192 usec earlier
2807 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2809 int rate = local->hw.wiphy->bands[rx_status->band]->
2810 bitrates[rx_status->rate_idx].bitrate;
2811 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2812 } else if (local && local->ops && local->ops->get_tsf)
2813 /* second best option: get current TSF */
2814 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2816 /* can't merge without knowing the TSF */
2817 rx_timestamp = -1LLU;
2818 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2819 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2820 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2821 print_mac(mac, mgmt->sa),
2822 print_mac(mac2, mgmt->bssid),
2823 (unsigned long long)rx_timestamp,
2824 (unsigned long long)beacon_timestamp,
2825 (unsigned long long)(rx_timestamp - beacon_timestamp),
2827 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2828 if (beacon_timestamp > rx_timestamp) {
2829 #ifndef CONFIG_MAC80211_IBSS_DEBUG
2830 if (net_ratelimit())
2832 printk(KERN_DEBUG "%s: beacon TSF higher than "
2833 "local TSF - IBSS merge with BSSID %s\n",
2834 dev->name, print_mac(mac, mgmt->bssid));
2835 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2836 ieee80211_ibss_add_sta(dev, NULL,
2837 mgmt->bssid, mgmt->sa);
2841 ieee80211_rx_bss_put(dev, bss);
2845 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2846 struct ieee80211_mgmt *mgmt,
2848 struct ieee80211_rx_status *rx_status)
2850 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2854 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2855 struct ieee80211_mgmt *mgmt,
2857 struct ieee80211_rx_status *rx_status)
2859 struct ieee80211_sub_if_data *sdata;
2860 struct ieee80211_if_sta *ifsta;
2862 struct ieee802_11_elems elems;
2863 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2864 struct ieee80211_conf *conf = &local->hw.conf;
2867 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2869 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2870 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2872 ifsta = &sdata->u.sta;
2874 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2875 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2878 /* Process beacon from the current BSS */
2879 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2883 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2885 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2886 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2887 elems.wmm_param_len);
2890 /* Do not send changes to driver if we are scanning. This removes
2891 * requirement that driver's bss_info_changed function needs to be
2893 if (local->sta_sw_scanning || local->sta_hw_scanning)
2896 if (elems.erp_info && elems.erp_info_len >= 1)
2897 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2899 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2900 changed |= ieee80211_handle_protect_preamb(sdata, false,
2901 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2904 if (elems.ht_cap_elem && elems.ht_info_elem &&
2905 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2906 struct ieee80211_ht_bss_info bss_info;
2908 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2909 (struct ieee80211_ht_addt_info *)
2910 elems.ht_info_elem, &bss_info);
2911 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2915 ieee80211_bss_info_change_notify(sdata, changed);
2919 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2920 struct ieee80211_if_sta *ifsta,
2921 struct ieee80211_mgmt *mgmt,
2923 struct ieee80211_rx_status *rx_status)
2925 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2926 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2928 struct sk_buff *skb;
2929 struct ieee80211_mgmt *resp;
2931 DECLARE_MAC_BUF(mac);
2932 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2933 DECLARE_MAC_BUF(mac2);
2934 DECLARE_MAC_BUF(mac3);
2937 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2938 ifsta->state != IEEE80211_IBSS_JOINED ||
2939 len < 24 + 2 || !ifsta->probe_resp)
2942 if (local->ops->tx_last_beacon)
2943 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2947 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2948 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2949 "%s (tx_last_beacon=%d)\n",
2950 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2951 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2952 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2954 if (!tx_last_beacon)
2957 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2958 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2961 end = ((u8 *) mgmt) + len;
2962 pos = mgmt->u.probe_req.variable;
2963 if (pos[0] != WLAN_EID_SSID ||
2964 pos + 2 + pos[1] > end) {
2965 if (net_ratelimit()) {
2966 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2968 dev->name, print_mac(mac, mgmt->sa));
2973 (pos[1] != ifsta->ssid_len ||
2974 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2975 /* Ignore ProbeReq for foreign SSID */
2979 /* Reply with ProbeResp */
2980 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2984 resp = (struct ieee80211_mgmt *) skb->data;
2985 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2986 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2987 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2988 dev->name, print_mac(mac, resp->da));
2989 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2990 ieee80211_sta_tx(dev, skb, 0);
2993 static void ieee80211_rx_mgmt_action(struct net_device *dev,
2994 struct ieee80211_if_sta *ifsta,
2995 struct ieee80211_mgmt *mgmt,
2997 struct ieee80211_rx_status *rx_status)
2999 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3001 if (len < IEEE80211_MIN_ACTION_SIZE)
3004 switch (mgmt->u.action.category) {
3005 case WLAN_CATEGORY_BACK:
3006 switch (mgmt->u.action.u.addba_req.action_code) {
3007 case WLAN_ACTION_ADDBA_REQ:
3008 if (len < (IEEE80211_MIN_ACTION_SIZE +
3009 sizeof(mgmt->u.action.u.addba_req)))
3011 ieee80211_sta_process_addba_request(dev, mgmt, len);
3013 case WLAN_ACTION_ADDBA_RESP:
3014 if (len < (IEEE80211_MIN_ACTION_SIZE +
3015 sizeof(mgmt->u.action.u.addba_resp)))
3017 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3019 case WLAN_ACTION_DELBA:
3020 if (len < (IEEE80211_MIN_ACTION_SIZE +
3021 sizeof(mgmt->u.action.u.delba)))
3023 ieee80211_sta_process_delba(dev, mgmt, len);
3026 if (net_ratelimit())
3027 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
3032 case PLINK_CATEGORY:
3033 if (ieee80211_vif_is_mesh(&sdata->vif))
3034 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3036 case MESH_PATH_SEL_CATEGORY:
3037 if (ieee80211_vif_is_mesh(&sdata->vif))
3038 mesh_rx_path_sel_frame(dev, mgmt, len);
3041 if (net_ratelimit())
3042 printk(KERN_DEBUG "%s: Rx unknown action frame - "
3043 "category=%d\n", dev->name, mgmt->u.action.category);
3048 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3049 struct ieee80211_rx_status *rx_status)
3051 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3052 struct ieee80211_sub_if_data *sdata;
3053 struct ieee80211_if_sta *ifsta;
3054 struct ieee80211_mgmt *mgmt;
3060 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3061 ifsta = &sdata->u.sta;
3063 mgmt = (struct ieee80211_mgmt *) skb->data;
3064 fc = le16_to_cpu(mgmt->frame_control);
3066 switch (fc & IEEE80211_FCTL_STYPE) {
3067 case IEEE80211_STYPE_PROBE_REQ:
3068 case IEEE80211_STYPE_PROBE_RESP:
3069 case IEEE80211_STYPE_BEACON:
3070 case IEEE80211_STYPE_ACTION:
3071 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3072 case IEEE80211_STYPE_AUTH:
3073 case IEEE80211_STYPE_ASSOC_RESP:
3074 case IEEE80211_STYPE_REASSOC_RESP:
3075 case IEEE80211_STYPE_DEAUTH:
3076 case IEEE80211_STYPE_DISASSOC:
3077 skb_queue_tail(&ifsta->skb_queue, skb);
3078 queue_work(local->hw.workqueue, &ifsta->work);
3081 printk(KERN_DEBUG "%s: received unknown management frame - "
3082 "stype=%d\n", dev->name,
3083 (fc & IEEE80211_FCTL_STYPE) >> 4);
3092 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3093 struct sk_buff *skb)
3095 struct ieee80211_rx_status *rx_status;
3096 struct ieee80211_sub_if_data *sdata;
3097 struct ieee80211_if_sta *ifsta;
3098 struct ieee80211_mgmt *mgmt;
3101 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3102 ifsta = &sdata->u.sta;
3104 rx_status = (struct ieee80211_rx_status *) skb->cb;
3105 mgmt = (struct ieee80211_mgmt *) skb->data;
3106 fc = le16_to_cpu(mgmt->frame_control);
3108 switch (fc & IEEE80211_FCTL_STYPE) {
3109 case IEEE80211_STYPE_PROBE_REQ:
3110 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3113 case IEEE80211_STYPE_PROBE_RESP:
3114 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3116 case IEEE80211_STYPE_BEACON:
3117 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3119 case IEEE80211_STYPE_AUTH:
3120 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3122 case IEEE80211_STYPE_ASSOC_RESP:
3123 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
3125 case IEEE80211_STYPE_REASSOC_RESP:
3126 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
3128 case IEEE80211_STYPE_DEAUTH:
3129 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3131 case IEEE80211_STYPE_DISASSOC:
3132 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3134 case IEEE80211_STYPE_ACTION:
3135 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
3144 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3145 struct ieee80211_rx_status *rx_status)
3147 struct ieee80211_mgmt *mgmt;
3151 return RX_DROP_UNUSABLE;
3153 mgmt = (struct ieee80211_mgmt *) skb->data;
3154 fc = le16_to_cpu(mgmt->frame_control);
3156 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
3160 return RX_DROP_MONITOR;
3162 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3163 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3164 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3165 skb->len, rx_status);
3168 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3169 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3179 static int ieee80211_sta_active_ibss(struct net_device *dev)
3181 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3183 struct sta_info *sta;
3184 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3188 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3189 if (sta->sdata == sdata &&
3190 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3203 static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
3205 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3206 struct sta_info *sta, *tmp;
3207 LIST_HEAD(tmp_list);
3208 DECLARE_MAC_BUF(mac);
3209 unsigned long flags;
3211 spin_lock_irqsave(&local->sta_lock, flags);
3212 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3213 if (time_after(jiffies, sta->last_rx + exp_time)) {
3214 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3215 dev->name, print_mac(mac, sta->addr));
3216 __sta_info_unlink(&sta);
3218 list_add(&sta->list, &tmp_list);
3220 spin_unlock_irqrestore(&local->sta_lock, flags);
3222 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3223 sta_info_destroy(sta);
3227 static void ieee80211_sta_merge_ibss(struct net_device *dev,
3228 struct ieee80211_if_sta *ifsta)
3230 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3232 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
3233 if (ieee80211_sta_active_ibss(dev))
3236 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3237 "IBSS networks with same SSID (merge)\n", dev->name);
3238 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3242 #ifdef CONFIG_MAC80211_MESH
3243 static void ieee80211_mesh_housekeeping(struct net_device *dev,
3244 struct ieee80211_if_sta *ifsta)
3246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3249 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3250 mesh_path_expire(dev);
3252 free_plinks = mesh_plink_availables(sdata);
3253 if (free_plinks != sdata->u.sta.accepting_plinks)
3254 ieee80211_if_config_beacon(dev);
3256 mod_timer(&ifsta->timer, jiffies +
3257 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3261 void ieee80211_start_mesh(struct net_device *dev)
3263 struct ieee80211_if_sta *ifsta;
3264 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3265 ifsta = &sdata->u.sta;
3266 ifsta->state = IEEE80211_MESH_UP;
3267 ieee80211_sta_timer((unsigned long)sdata);
3272 void ieee80211_sta_timer(unsigned long data)
3274 struct ieee80211_sub_if_data *sdata =
3275 (struct ieee80211_sub_if_data *) data;
3276 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3277 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3279 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3280 queue_work(local->hw.workqueue, &ifsta->work);
3283 void ieee80211_sta_work(struct work_struct *work)
3285 struct ieee80211_sub_if_data *sdata =
3286 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3287 struct net_device *dev = sdata->dev;
3288 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3289 struct ieee80211_if_sta *ifsta;
3290 struct sk_buff *skb;
3292 if (!netif_running(dev))
3295 if (local->sta_sw_scanning || local->sta_hw_scanning)
3298 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3299 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3300 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
3301 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
3302 "(type=%d)\n", dev->name, sdata->vif.type);
3305 ifsta = &sdata->u.sta;
3307 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3308 ieee80211_sta_rx_queued_mgmt(dev, skb);
3310 #ifdef CONFIG_MAC80211_MESH
3311 if (ifsta->preq_queue_len &&
3313 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3314 mesh_path_start_discovery(dev);
3317 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3318 ifsta->state != IEEE80211_ASSOCIATE &&
3319 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3320 if (ifsta->scan_ssid_len)
3321 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3323 ieee80211_sta_start_scan(dev, NULL, 0);
3327 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3328 if (ieee80211_sta_config_auth(dev, ifsta))
3330 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3331 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3334 switch (ifsta->state) {
3335 case IEEE80211_DISABLED:
3337 case IEEE80211_AUTHENTICATE:
3338 ieee80211_authenticate(dev, ifsta);
3340 case IEEE80211_ASSOCIATE:
3341 ieee80211_associate(dev, ifsta);
3343 case IEEE80211_ASSOCIATED:
3344 ieee80211_associated(dev, ifsta);
3346 case IEEE80211_IBSS_SEARCH:
3347 ieee80211_sta_find_ibss(dev, ifsta);
3349 case IEEE80211_IBSS_JOINED:
3350 ieee80211_sta_merge_ibss(dev, ifsta);
3352 #ifdef CONFIG_MAC80211_MESH
3353 case IEEE80211_MESH_UP:
3354 ieee80211_mesh_housekeeping(dev, ifsta);
3358 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3363 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3364 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3365 "mixed-cell disabled - disassociate\n", dev->name);
3367 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3368 ieee80211_set_disassoc(dev, ifsta, 0);
3373 static void ieee80211_sta_reset_auth(struct net_device *dev,
3374 struct ieee80211_if_sta *ifsta)
3376 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3378 if (local->ops->reset_tsf) {
3379 /* Reset own TSF to allow time synchronization work. */
3380 local->ops->reset_tsf(local_to_hw(local));
3383 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3386 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3387 ifsta->auth_alg = WLAN_AUTH_OPEN;
3388 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3389 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3390 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3391 ifsta->auth_alg = WLAN_AUTH_LEAP;
3393 ifsta->auth_alg = WLAN_AUTH_OPEN;
3394 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3396 ifsta->auth_transaction = -1;
3397 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3398 ifsta->auth_tries = ifsta->assoc_tries = 0;
3399 netif_carrier_off(dev);
3403 void ieee80211_sta_req_auth(struct net_device *dev,
3404 struct ieee80211_if_sta *ifsta)
3406 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3407 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3409 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3412 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3413 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3414 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3415 IEEE80211_STA_AUTO_SSID_SEL))) {
3416 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3417 queue_work(local->hw.workqueue, &ifsta->work);
3421 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3422 const char *ssid, int ssid_len)
3424 int tmp, hidden_ssid;
3426 if (ssid_len == ifsta->ssid_len &&
3427 !memcmp(ifsta->ssid, ssid, ssid_len))
3430 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3436 if (ssid[tmp] != '\0') {
3442 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3445 if (ssid_len == 1 && ssid[0] == ' ')
3451 static int ieee80211_sta_config_auth(struct net_device *dev,
3452 struct ieee80211_if_sta *ifsta)
3454 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3455 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3456 struct ieee80211_sta_bss *bss, *selected = NULL;
3457 int top_rssi = 0, freq;
3459 spin_lock_bh(&local->sta_bss_lock);
3460 freq = local->oper_channel->center_freq;
3461 list_for_each_entry(bss, &local->sta_bss_list, list) {
3462 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3465 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3466 IEEE80211_STA_AUTO_BSSID_SEL |
3467 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3468 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3469 !!sdata->default_key))
3472 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3476 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3477 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3480 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3481 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3484 if (!selected || top_rssi < bss->rssi) {
3486 top_rssi = bss->rssi;
3490 atomic_inc(&selected->users);
3491 spin_unlock_bh(&local->sta_bss_lock);
3494 ieee80211_set_freq(dev, selected->freq);
3495 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3496 ieee80211_sta_set_ssid(dev, selected->ssid,
3497 selected->ssid_len);
3498 ieee80211_sta_set_bssid(dev, selected->bssid);
3499 ieee80211_sta_def_wmm_params(dev, selected, 0);
3500 ieee80211_rx_bss_put(dev, selected);
3501 ifsta->state = IEEE80211_AUTHENTICATE;
3502 ieee80211_sta_reset_auth(dev, ifsta);
3505 if (ifsta->state != IEEE80211_AUTHENTICATE) {
3506 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3507 ieee80211_sta_start_scan(dev, NULL, 0);
3509 ieee80211_sta_start_scan(dev, ifsta->ssid,
3511 ifsta->state = IEEE80211_AUTHENTICATE;
3512 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3514 ifsta->state = IEEE80211_DISABLED;
3520 static int ieee80211_sta_create_ibss(struct net_device *dev,
3521 struct ieee80211_if_sta *ifsta)
3523 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3524 struct ieee80211_sta_bss *bss;
3525 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3526 struct ieee80211_supported_band *sband;
3527 u8 bssid[ETH_ALEN], *pos;
3530 DECLARE_MAC_BUF(mac);
3533 /* Easier testing, use fixed BSSID. */
3534 memset(bssid, 0xfe, ETH_ALEN);
3536 /* Generate random, not broadcast, locally administered BSSID. Mix in
3537 * own MAC address to make sure that devices that do not have proper
3538 * random number generator get different BSSID. */
3539 get_random_bytes(bssid, ETH_ALEN);
3540 for (i = 0; i < ETH_ALEN; i++)
3541 bssid[i] ^= dev->dev_addr[i];
3546 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3547 dev->name, print_mac(mac, bssid));
3549 bss = ieee80211_rx_bss_add(dev, bssid,
3550 local->hw.conf.channel->center_freq,
3551 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3555 bss->band = local->hw.conf.channel->band;
3556 sband = local->hw.wiphy->bands[bss->band];
3558 if (local->hw.conf.beacon_int == 0)
3559 local->hw.conf.beacon_int = 10000;
3560 bss->beacon_int = local->hw.conf.beacon_int;
3561 bss->last_update = jiffies;
3562 bss->capability = WLAN_CAPABILITY_IBSS;
3563 if (sdata->default_key) {
3564 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3566 sdata->drop_unencrypted = 0;
3567 bss->supp_rates_len = sband->n_bitrates;
3568 pos = bss->supp_rates;
3569 for (i = 0; i < sband->n_bitrates; i++) {
3570 int rate = sband->bitrates[i].bitrate;
3571 *pos++ = (u8) (rate / 5);
3574 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3575 ieee80211_rx_bss_put(dev, bss);
3580 static int ieee80211_sta_find_ibss(struct net_device *dev,
3581 struct ieee80211_if_sta *ifsta)
3583 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3584 struct ieee80211_sta_bss *bss;
3588 DECLARE_MAC_BUF(mac);
3589 DECLARE_MAC_BUF(mac2);
3591 if (ifsta->ssid_len == 0)
3594 active_ibss = ieee80211_sta_active_ibss(dev);
3595 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3596 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3597 dev->name, active_ibss);
3598 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3599 spin_lock_bh(&local->sta_bss_lock);
3600 list_for_each_entry(bss, &local->sta_bss_list, list) {
3601 if (ifsta->ssid_len != bss->ssid_len ||
3602 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3603 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3605 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3606 printk(KERN_DEBUG " bssid=%s found\n",
3607 print_mac(mac, bss->bssid));
3608 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3609 memcpy(bssid, bss->bssid, ETH_ALEN);
3611 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3614 spin_unlock_bh(&local->sta_bss_lock);
3616 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3618 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3619 "%s\n", print_mac(mac, bssid),
3620 print_mac(mac2, ifsta->bssid));
3621 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3622 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
3623 (bss = ieee80211_rx_bss_get(dev, bssid,
3624 local->hw.conf.channel->center_freq,
3625 ifsta->ssid, ifsta->ssid_len))) {
3627 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3628 " based on configured SSID\n",
3629 dev->name, print_mac(mac, bssid));
3630 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3631 ieee80211_rx_bss_put(dev, bss);
3634 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3635 printk(KERN_DEBUG " did not try to join ibss\n");
3636 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3638 /* Selected IBSS not found in current scan results - try to scan */
3639 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3640 !ieee80211_sta_active_ibss(dev)) {
3641 mod_timer(&ifsta->timer, jiffies +
3642 IEEE80211_IBSS_MERGE_INTERVAL);
3643 } else if (time_after(jiffies, local->last_scan_completed +
3644 IEEE80211_SCAN_INTERVAL)) {
3645 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3646 "join\n", dev->name);
3647 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3649 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3650 int interval = IEEE80211_SCAN_INTERVAL;
3652 if (time_after(jiffies, ifsta->ibss_join_req +
3653 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3654 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3655 (!(local->oper_channel->flags &
3656 IEEE80211_CHAN_NO_IBSS)))
3657 return ieee80211_sta_create_ibss(dev, ifsta);
3658 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3659 printk(KERN_DEBUG "%s: IBSS not allowed on"
3660 " %d MHz\n", dev->name,
3661 local->hw.conf.channel->center_freq);
3664 /* No IBSS found - decrease scan interval and continue
3666 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3669 ifsta->state = IEEE80211_IBSS_SEARCH;
3670 mod_timer(&ifsta->timer, jiffies + interval);
3678 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3680 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3681 struct ieee80211_if_sta *ifsta;
3683 if (len > IEEE80211_MAX_SSID_LEN)
3686 ifsta = &sdata->u.sta;
3688 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
3689 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3690 memcpy(ifsta->ssid, ssid, len);
3691 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3692 ifsta->ssid_len = len;
3695 ifsta->flags |= IEEE80211_STA_SSID_SET;
3697 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3698 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3699 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3700 ifsta->ibss_join_req = jiffies;
3701 ifsta->state = IEEE80211_IBSS_SEARCH;
3702 return ieee80211_sta_find_ibss(dev, ifsta);
3708 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3710 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3711 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3712 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3713 *len = ifsta->ssid_len;
3718 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3720 struct ieee80211_sub_if_data *sdata;
3721 struct ieee80211_if_sta *ifsta;
3724 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3725 ifsta = &sdata->u.sta;
3727 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3728 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3729 res = ieee80211_if_config(dev);
3731 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3732 "the low-level driver\n", dev->name);
3737 if (is_valid_ether_addr(bssid))
3738 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3740 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3746 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3747 struct ieee80211_sub_if_data *sdata,
3750 struct sk_buff *skb;
3751 struct ieee80211_hdr *nullfunc;
3754 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3756 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3757 "frame\n", sdata->dev->name);
3760 skb_reserve(skb, local->hw.extra_tx_headroom);
3762 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3763 memset(nullfunc, 0, 24);
3764 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3765 IEEE80211_FCTL_TODS;
3767 fc |= IEEE80211_FCTL_PM;
3768 nullfunc->frame_control = cpu_to_le16(fc);
3769 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3770 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3771 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3773 ieee80211_sta_tx(sdata->dev, skb, 0);
3777 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3779 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3780 ieee80211_vif_is_mesh(&sdata->vif))
3781 ieee80211_sta_timer((unsigned long)sdata);
3784 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3786 struct ieee80211_local *local = hw_to_local(hw);
3787 struct net_device *dev = local->scan_dev;
3788 struct ieee80211_sub_if_data *sdata;
3789 union iwreq_data wrqu;
3791 local->last_scan_completed = jiffies;
3792 memset(&wrqu, 0, sizeof(wrqu));
3793 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3795 if (local->sta_hw_scanning) {
3796 local->sta_hw_scanning = 0;
3797 if (ieee80211_hw_config(local))
3798 printk(KERN_DEBUG "%s: failed to restore operational "
3799 "channel after scan\n", dev->name);
3800 /* Restart STA timer for HW scan case */
3802 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3803 ieee80211_restart_sta_timer(sdata);
3809 local->sta_sw_scanning = 0;
3810 if (ieee80211_hw_config(local))
3811 printk(KERN_DEBUG "%s: failed to restore operational "
3812 "channel after scan\n", dev->name);
3815 netif_tx_lock_bh(local->mdev);
3816 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3817 local->ops->configure_filter(local_to_hw(local),
3818 FIF_BCN_PRBRESP_PROMISC,
3819 &local->filter_flags,
3820 local->mdev->mc_count,
3821 local->mdev->mc_list);
3823 netif_tx_unlock_bh(local->mdev);
3826 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3828 /* No need to wake the master device. */
3829 if (sdata->dev == local->mdev)
3832 /* Tell AP we're back */
3833 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3834 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3835 ieee80211_send_nullfunc(local, sdata, 0);
3837 ieee80211_restart_sta_timer(sdata);
3839 netif_wake_queue(sdata->dev);
3844 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3845 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3846 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3847 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3848 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3849 !ieee80211_sta_active_ibss(dev)))
3850 ieee80211_sta_find_ibss(dev, ifsta);
3853 EXPORT_SYMBOL(ieee80211_scan_completed);
3855 void ieee80211_sta_scan_work(struct work_struct *work)
3857 struct ieee80211_local *local =
3858 container_of(work, struct ieee80211_local, scan_work.work);
3859 struct net_device *dev = local->scan_dev;
3860 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3861 struct ieee80211_supported_band *sband;
3862 struct ieee80211_channel *chan;
3864 unsigned long next_delay = 0;
3866 if (!local->sta_sw_scanning)
3869 switch (local->scan_state) {
3870 case SCAN_SET_CHANNEL:
3872 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3873 * after we successfully scanned the last channel of the last
3874 * band (and the last band is supported by the hw)
3876 if (local->scan_band < IEEE80211_NUM_BANDS)
3877 sband = local->hw.wiphy->bands[local->scan_band];
3882 * If we are at an unsupported band and have more bands
3883 * left to scan, advance to the next supported one.
3885 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3887 sband = local->hw.wiphy->bands[local->scan_band];
3888 local->scan_channel_idx = 0;
3891 /* if no more bands/channels left, complete scan */
3892 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3893 ieee80211_scan_completed(local_to_hw(local));
3897 chan = &sband->channels[local->scan_channel_idx];
3899 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3900 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3901 chan->flags & IEEE80211_CHAN_NO_IBSS))
3905 local->scan_channel = chan;
3906 if (ieee80211_hw_config(local)) {
3907 printk(KERN_DEBUG "%s: failed to set freq to "
3908 "%d MHz for scan\n", dev->name,
3914 /* advance state machine to next channel/band */
3915 local->scan_channel_idx++;
3916 if (local->scan_channel_idx >= sband->n_channels) {
3918 * scan_band may end up == IEEE80211_NUM_BANDS, but
3919 * we'll catch that case above and complete the scan
3920 * if that is the case.
3923 local->scan_channel_idx = 0;
3929 next_delay = IEEE80211_PROBE_DELAY +
3930 usecs_to_jiffies(local->hw.channel_change_time);
3931 local->scan_state = SCAN_SEND_PROBE;
3933 case SCAN_SEND_PROBE:
3934 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3935 local->scan_state = SCAN_SET_CHANNEL;
3937 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3939 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3940 local->scan_ssid_len);
3941 next_delay = IEEE80211_CHANNEL_TIME;
3945 if (local->sta_sw_scanning)
3946 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3951 static int ieee80211_sta_start_scan(struct net_device *dev,
3952 u8 *ssid, size_t ssid_len)
3954 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3955 struct ieee80211_sub_if_data *sdata;
3957 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3960 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3961 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3964 * ScanType: ACTIVE, PASSIVE
3965 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3966 * a Probe frame during active scanning
3968 * MinChannelTime (>= ProbeDelay), in TU
3969 * MaxChannelTime: (>= MinChannelTime), in TU
3972 /* MLME-SCAN.confirm
3974 * ResultCode: SUCCESS, INVALID_PARAMETERS
3977 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3978 if (local->scan_dev == dev)
3983 if (local->ops->hw_scan) {
3984 int rc = local->ops->hw_scan(local_to_hw(local),
3987 local->sta_hw_scanning = 1;
3988 local->scan_dev = dev;
3993 local->sta_sw_scanning = 1;
3996 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3998 /* Don't stop the master interface, otherwise we can't transmit
4000 if (sdata->dev == local->mdev)
4003 netif_stop_queue(sdata->dev);
4004 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
4005 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
4006 ieee80211_send_nullfunc(local, sdata, 1);
4011 local->scan_ssid_len = ssid_len;
4012 memcpy(local->scan_ssid, ssid, ssid_len);
4014 local->scan_ssid_len = 0;
4015 local->scan_state = SCAN_SET_CHANNEL;
4016 local->scan_channel_idx = 0;
4017 local->scan_band = IEEE80211_BAND_2GHZ;
4018 local->scan_dev = dev;
4020 netif_tx_lock_bh(local->mdev);
4021 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4022 local->ops->configure_filter(local_to_hw(local),
4023 FIF_BCN_PRBRESP_PROMISC,
4024 &local->filter_flags,
4025 local->mdev->mc_count,
4026 local->mdev->mc_list);
4027 netif_tx_unlock_bh(local->mdev);
4029 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4030 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4031 IEEE80211_CHANNEL_TIME);
4037 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4039 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4040 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4041 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4043 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4044 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4046 if (local->sta_sw_scanning || local->sta_hw_scanning) {
4047 if (local->scan_dev == dev)
4052 ifsta->scan_ssid_len = ssid_len;
4054 memcpy(ifsta->scan_ssid, ssid, ssid_len);
4055 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4056 queue_work(local->hw.workqueue, &ifsta->work);
4061 ieee80211_sta_scan_result(struct net_device *dev,
4062 struct ieee80211_sta_bss *bss,
4063 char *current_ev, char *end_buf)
4065 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4066 struct iw_event iwe;
4068 if (time_after(jiffies,
4069 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4072 memset(&iwe, 0, sizeof(iwe));
4073 iwe.cmd = SIOCGIWAP;
4074 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4075 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4076 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4079 memset(&iwe, 0, sizeof(iwe));
4080 iwe.cmd = SIOCGIWESSID;
4081 if (bss_mesh_cfg(bss)) {
4082 iwe.u.data.length = bss_mesh_id_len(bss);
4083 iwe.u.data.flags = 1;
4084 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4087 iwe.u.data.length = bss->ssid_len;
4088 iwe.u.data.flags = 1;
4089 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4093 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4094 || bss_mesh_cfg(bss)) {
4095 memset(&iwe, 0, sizeof(iwe));
4096 iwe.cmd = SIOCGIWMODE;
4097 if (bss_mesh_cfg(bss))
4098 iwe.u.mode = IW_MODE_MESH;
4099 else if (bss->capability & WLAN_CAPABILITY_ESS)
4100 iwe.u.mode = IW_MODE_MASTER;
4102 iwe.u.mode = IW_MODE_ADHOC;
4103 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4107 memset(&iwe, 0, sizeof(iwe));
4108 iwe.cmd = SIOCGIWFREQ;
4109 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4111 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4114 memset(&iwe, 0, sizeof(iwe));
4115 iwe.cmd = SIOCGIWFREQ;
4116 iwe.u.freq.m = bss->freq;
4118 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4120 memset(&iwe, 0, sizeof(iwe));
4122 iwe.u.qual.qual = bss->signal;
4123 iwe.u.qual.level = bss->rssi;
4124 iwe.u.qual.noise = bss->noise;
4125 iwe.u.qual.updated = local->wstats_flags;
4126 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4129 memset(&iwe, 0, sizeof(iwe));
4130 iwe.cmd = SIOCGIWENCODE;
4131 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4132 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4134 iwe.u.data.flags = IW_ENCODE_DISABLED;
4135 iwe.u.data.length = 0;
4136 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4138 if (bss && bss->wpa_ie) {
4139 memset(&iwe, 0, sizeof(iwe));
4140 iwe.cmd = IWEVGENIE;
4141 iwe.u.data.length = bss->wpa_ie_len;
4142 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4146 if (bss && bss->rsn_ie) {
4147 memset(&iwe, 0, sizeof(iwe));
4148 iwe.cmd = IWEVGENIE;
4149 iwe.u.data.length = bss->rsn_ie_len;
4150 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4154 if (bss && bss->supp_rates_len > 0) {
4155 /* display all supported rates in readable format */
4156 char *p = current_ev + IW_EV_LCP_LEN;
4159 memset(&iwe, 0, sizeof(iwe));
4160 iwe.cmd = SIOCGIWRATE;
4161 /* Those two flags are ignored... */
4162 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4164 for (i = 0; i < bss->supp_rates_len; i++) {
4165 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4167 p = iwe_stream_add_value(current_ev, p,
4168 end_buf, &iwe, IW_EV_PARAM_LEN);
4175 buf = kmalloc(30, GFP_ATOMIC);
4177 memset(&iwe, 0, sizeof(iwe));
4178 iwe.cmd = IWEVCUSTOM;
4179 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4180 iwe.u.data.length = strlen(buf);
4181 current_ev = iwe_stream_add_point(current_ev, end_buf,
4187 if (bss_mesh_cfg(bss)) {
4189 u8 *cfg = bss_mesh_cfg(bss);
4190 buf = kmalloc(50, GFP_ATOMIC);
4192 memset(&iwe, 0, sizeof(iwe));
4193 iwe.cmd = IWEVCUSTOM;
4194 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4195 iwe.u.data.length = strlen(buf);
4196 current_ev = iwe_stream_add_point(current_ev, end_buf,
4198 sprintf(buf, "Path Selection Protocol ID: "
4199 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4201 iwe.u.data.length = strlen(buf);
4202 current_ev = iwe_stream_add_point(current_ev, end_buf,
4204 sprintf(buf, "Path Selection Metric ID: "
4205 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4207 iwe.u.data.length = strlen(buf);
4208 current_ev = iwe_stream_add_point(current_ev, end_buf,
4210 sprintf(buf, "Congestion Control Mode ID: "
4211 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4213 iwe.u.data.length = strlen(buf);
4214 current_ev = iwe_stream_add_point(current_ev, end_buf,
4216 sprintf(buf, "Channel Precedence: "
4217 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4219 iwe.u.data.length = strlen(buf);
4220 current_ev = iwe_stream_add_point(current_ev, end_buf,
4230 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4232 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4233 char *current_ev = buf;
4234 char *end_buf = buf + len;
4235 struct ieee80211_sta_bss *bss;
4237 spin_lock_bh(&local->sta_bss_lock);
4238 list_for_each_entry(bss, &local->sta_bss_list, list) {
4239 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4240 spin_unlock_bh(&local->sta_bss_lock);
4243 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4246 spin_unlock_bh(&local->sta_bss_lock);
4247 return current_ev - buf;
4251 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4253 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4254 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4255 kfree(ifsta->extra_ie);
4257 ifsta->extra_ie = NULL;
4258 ifsta->extra_ie_len = 0;
4261 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4262 if (!ifsta->extra_ie) {
4263 ifsta->extra_ie_len = 0;
4266 memcpy(ifsta->extra_ie, ie, len);
4267 ifsta->extra_ie_len = len;
4272 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4273 struct sk_buff *skb, u8 *bssid,
4276 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4277 struct sta_info *sta;
4278 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4279 DECLARE_MAC_BUF(mac);
4281 /* TODO: Could consider removing the least recently used entry and
4282 * allow new one to be added. */
4283 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4284 if (net_ratelimit()) {
4285 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4286 "entry %s\n", dev->name, print_mac(mac, addr));
4291 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4292 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
4294 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4298 sta->flags |= WLAN_STA_AUTHORIZED;
4300 sta->supp_rates[local->hw.conf.channel->band] =
4301 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
4303 rate_control_rate_init(sta, local);
4305 if (sta_info_insert(sta))
4312 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4314 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4315 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4317 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4320 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4321 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4324 ieee80211_send_deauth(dev, ifsta, reason);
4325 ieee80211_set_disassoc(dev, ifsta, 1);
4330 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4332 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4333 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4335 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4338 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4341 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4344 ieee80211_send_disassoc(dev, ifsta, reason);
4345 ieee80211_set_disassoc(dev, ifsta, 0);
4349 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4350 enum ieee80211_notification_types notif_type)
4352 struct ieee80211_local *local = hw_to_local(hw);
4353 struct ieee80211_sub_if_data *sdata;
4355 switch (notif_type) {
4356 case IEEE80211_NOTIFY_RE_ASSOC:
4358 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4360 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4361 ieee80211_sta_req_auth(sdata->dev,
4370 EXPORT_SYMBOL(ieee80211_notify_mac);