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"
33 #include "ieee80211_rate.h"
34 #include "ieee80211_led.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 (20 * 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 IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
62 #define ERP_INFO_USE_PROTECTION BIT(1)
64 /* mgmt header + 1 byte action code */
65 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
67 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
68 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
69 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
70 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
71 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
73 /* next values represent the buffer size for A-MPDU frame.
74 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
75 #define IEEE80211_MIN_AMPDU_BUF 0x8
76 #define IEEE80211_MAX_AMPDU_BUF 0x40
78 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
79 u8 *ssid, size_t ssid_len);
80 static struct ieee80211_sta_bss *
81 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
82 u8 *ssid, u8 ssid_len);
83 static void ieee80211_rx_bss_put(struct net_device *dev,
84 struct ieee80211_sta_bss *bss);
85 static int ieee80211_sta_find_ibss(struct net_device *dev,
86 struct ieee80211_if_sta *ifsta);
87 static int ieee80211_sta_wep_configured(struct net_device *dev);
88 static int ieee80211_sta_start_scan(struct net_device *dev,
89 u8 *ssid, size_t ssid_len);
90 static int ieee80211_sta_config_auth(struct net_device *dev,
91 struct ieee80211_if_sta *ifsta);
94 void ieee802_11_parse_elems(u8 *start, size_t len,
95 struct ieee802_11_elems *elems)
100 memset(elems, 0, sizeof(*elems));
115 elems->ssid_len = elen;
117 case WLAN_EID_SUPP_RATES:
118 elems->supp_rates = pos;
119 elems->supp_rates_len = elen;
121 case WLAN_EID_FH_PARAMS:
122 elems->fh_params = pos;
123 elems->fh_params_len = elen;
125 case WLAN_EID_DS_PARAMS:
126 elems->ds_params = pos;
127 elems->ds_params_len = elen;
129 case WLAN_EID_CF_PARAMS:
130 elems->cf_params = pos;
131 elems->cf_params_len = elen;
135 elems->tim_len = elen;
137 case WLAN_EID_IBSS_PARAMS:
138 elems->ibss_params = pos;
139 elems->ibss_params_len = elen;
141 case WLAN_EID_CHALLENGE:
142 elems->challenge = pos;
143 elems->challenge_len = elen;
146 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
148 /* Microsoft OUI (00:50:F2) */
150 /* OUI Type 1 - WPA IE */
152 elems->wpa_len = elen;
153 } else if (elen >= 5 && pos[3] == 2) {
155 elems->wmm_info = pos;
156 elems->wmm_info_len = elen;
157 } else if (pos[4] == 1) {
158 elems->wmm_param = pos;
159 elems->wmm_param_len = elen;
166 elems->rsn_len = elen;
168 case WLAN_EID_ERP_INFO:
169 elems->erp_info = pos;
170 elems->erp_info_len = elen;
172 case WLAN_EID_EXT_SUPP_RATES:
173 elems->ext_supp_rates = pos;
174 elems->ext_supp_rates_len = elen;
176 case WLAN_EID_HT_CAPABILITY:
177 elems->ht_cap_elem = pos;
178 elems->ht_cap_elem_len = elen;
180 case WLAN_EID_HT_EXTRA_INFO:
181 elems->ht_info_elem = pos;
182 elems->ht_info_elem_len = elen;
184 case WLAN_EID_MESH_ID:
185 elems->mesh_id = pos;
186 elems->mesh_id_len = elen;
188 case WLAN_EID_MESH_CONFIG:
189 elems->mesh_config = pos;
190 elems->mesh_config_len = elen;
192 case WLAN_EID_PEER_LINK:
193 elems->peer_link = pos;
194 elems->peer_link_len = elen;
198 elems->preq_len = elen;
202 elems->prep_len = elen;
206 elems->perr_len = elen;
218 static int ecw2cw(int ecw)
220 return (1 << ecw) - 1;
224 static void ieee80211_sta_def_wmm_params(struct net_device *dev,
225 struct ieee80211_sta_bss *bss,
228 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
229 struct ieee80211_local *local = sdata->local;
230 int i, have_higher_than_11mbit = 0;
233 /* cf. IEEE 802.11 9.2.12 */
234 for (i = 0; i < bss->supp_rates_len; i++)
235 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
236 have_higher_than_11mbit = 1;
238 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
239 have_higher_than_11mbit)
240 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
242 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
245 if (local->ops->conf_tx) {
246 struct ieee80211_tx_queue_params qparam;
248 memset(&qparam, 0, sizeof(qparam));
252 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
253 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
258 qparam.cw_max = 1023;
261 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
262 local->ops->conf_tx(local_to_hw(local),
263 i + IEEE80211_TX_QUEUE_DATA0,
267 /* IBSS uses different parameters for Beacon sending */
271 local->ops->conf_tx(local_to_hw(local),
272 IEEE80211_TX_QUEUE_BEACON, &qparam);
277 static void ieee80211_sta_wmm_params(struct net_device *dev,
278 struct ieee80211_if_sta *ifsta,
279 u8 *wmm_param, size_t wmm_param_len)
281 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
282 struct ieee80211_tx_queue_params params;
287 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
289 count = wmm_param[6] & 0x0f;
290 if (count == ifsta->wmm_last_param_set)
292 ifsta->wmm_last_param_set = count;
295 left = wmm_param_len - 8;
297 memset(¶ms, 0, sizeof(params));
299 if (!local->ops->conf_tx)
303 for (; left >= 4; left -= 4, pos += 4) {
304 int aci = (pos[0] >> 5) & 0x03;
305 int acm = (pos[0] >> 4) & 0x01;
310 queue = IEEE80211_TX_QUEUE_DATA3;
312 local->wmm_acm |= BIT(0) | BIT(3);
316 queue = IEEE80211_TX_QUEUE_DATA1;
318 local->wmm_acm |= BIT(4) | BIT(5);
322 queue = IEEE80211_TX_QUEUE_DATA0;
324 local->wmm_acm |= BIT(6) | BIT(7);
329 queue = IEEE80211_TX_QUEUE_DATA2;
331 local->wmm_acm |= BIT(1) | BIT(2);
336 params.aifs = pos[0] & 0x0f;
337 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
338 params.cw_min = ecw2cw(pos[1] & 0x0f);
339 params.txop = pos[2] | (pos[3] << 8);
340 #ifdef CONFIG_MAC80211_DEBUG
341 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
342 "cWmin=%d cWmax=%d txop=%d\n",
343 dev->name, queue, aci, acm, params.aifs, params.cw_min,
344 params.cw_max, params.txop);
346 /* TODO: handle ACM (block TX, fallback to next lowest allowed
348 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
349 printk(KERN_DEBUG "%s: failed to set TX queue "
350 "parameters for queue %d\n", dev->name, queue);
356 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
359 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
360 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
361 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
362 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
363 DECLARE_MAC_BUF(mac);
366 if (use_protection != bss_conf->use_cts_prot) {
367 if (net_ratelimit()) {
368 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
371 use_protection ? "enabled" : "disabled",
372 print_mac(mac, ifsta->bssid));
374 bss_conf->use_cts_prot = use_protection;
375 changed |= BSS_CHANGED_ERP_CTS_PROT;
378 if (use_short_preamble != bss_conf->use_short_preamble) {
379 if (net_ratelimit()) {
380 printk(KERN_DEBUG "%s: switched to %s barker preamble"
383 use_short_preamble ? "short" : "long",
384 print_mac(mac, ifsta->bssid));
386 bss_conf->use_short_preamble = use_short_preamble;
387 changed |= BSS_CHANGED_ERP_PREAMBLE;
393 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
394 struct ieee80211_ht_info *ht_info)
400 memset(ht_info, 0, sizeof(*ht_info));
403 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
405 ht_info->ht_supported = 1;
406 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
407 ht_info->ampdu_factor =
408 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
409 ht_info->ampdu_density =
410 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
411 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
413 ht_info->ht_supported = 0;
418 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
419 struct ieee80211_ht_addt_info *ht_add_info_ie,
420 struct ieee80211_ht_bss_info *bss_info)
422 if (bss_info == NULL)
425 memset(bss_info, 0, sizeof(*bss_info));
427 if (ht_add_info_ie) {
429 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
431 bss_info->primary_channel = ht_add_info_ie->control_chan;
432 bss_info->bss_cap = ht_add_info_ie->ht_param;
433 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
439 static void ieee80211_sta_send_associnfo(struct net_device *dev,
440 struct ieee80211_if_sta *ifsta)
445 union iwreq_data wrqu;
447 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
450 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
451 ifsta->assocresp_ies_len), GFP_KERNEL);
455 len = sprintf(buf, "ASSOCINFO(");
456 if (ifsta->assocreq_ies) {
457 len += sprintf(buf + len, "ReqIEs=");
458 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
459 len += sprintf(buf + len, "%02x",
460 ifsta->assocreq_ies[i]);
463 if (ifsta->assocresp_ies) {
464 if (ifsta->assocreq_ies)
465 len += sprintf(buf + len, " ");
466 len += sprintf(buf + len, "RespIEs=");
467 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
468 len += sprintf(buf + len, "%02x",
469 ifsta->assocresp_ies[i]);
472 len += sprintf(buf + len, ")");
474 if (len > IW_CUSTOM_MAX) {
475 len = sprintf(buf, "ASSOCRESPIE=");
476 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
477 len += sprintf(buf + len, "%02x",
478 ifsta->assocresp_ies[i]);
482 memset(&wrqu, 0, sizeof(wrqu));
483 wrqu.data.length = len;
484 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
490 static void ieee80211_set_associated(struct net_device *dev,
491 struct ieee80211_if_sta *ifsta,
494 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
495 struct ieee80211_local *local = sdata->local;
496 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
497 union iwreq_data wrqu;
498 u32 changed = BSS_CHANGED_ASSOC;
501 struct ieee80211_sta_bss *bss;
503 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
505 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
508 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
509 conf->channel->center_freq,
510 ifsta->ssid, ifsta->ssid_len);
512 if (bss->has_erp_value)
513 changed |= ieee80211_handle_erp_ie(
514 sdata, bss->erp_value);
515 ieee80211_rx_bss_put(dev, bss);
518 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
519 changed |= BSS_CHANGED_HT;
520 sdata->bss_conf.assoc_ht = 1;
521 sdata->bss_conf.ht_conf = &conf->ht_conf;
522 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
525 netif_carrier_on(dev);
526 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
527 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
528 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
529 ieee80211_sta_send_associnfo(dev, ifsta);
531 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
532 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
533 netif_carrier_off(dev);
534 ieee80211_reset_erp_info(dev);
536 sdata->bss_conf.assoc_ht = 0;
537 sdata->bss_conf.ht_conf = NULL;
538 sdata->bss_conf.ht_bss_conf = NULL;
540 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
542 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
543 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
544 ifsta->last_probe = jiffies;
545 ieee80211_led_assoc(local, assoc);
547 sdata->bss_conf.assoc = assoc;
548 ieee80211_bss_info_change_notify(sdata, changed);
551 static void ieee80211_set_disassoc(struct net_device *dev,
552 struct ieee80211_if_sta *ifsta, int deauth)
555 ifsta->auth_tries = 0;
556 ifsta->assoc_tries = 0;
557 ieee80211_set_associated(dev, ifsta, 0);
560 void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
563 struct ieee80211_sub_if_data *sdata;
564 struct ieee80211_tx_packet_data *pkt_data;
566 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
567 skb->dev = sdata->local->mdev;
568 skb_set_mac_header(skb, 0);
569 skb_set_network_header(skb, 0);
570 skb_set_transport_header(skb, 0);
572 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
573 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
574 pkt_data->ifindex = sdata->dev->ifindex;
576 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
582 static void ieee80211_send_auth(struct net_device *dev,
583 struct ieee80211_if_sta *ifsta,
584 int transaction, u8 *extra, size_t extra_len,
587 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
589 struct ieee80211_mgmt *mgmt;
591 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
592 sizeof(*mgmt) + 6 + extra_len);
594 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
595 "frame\n", dev->name);
598 skb_reserve(skb, local->hw.extra_tx_headroom);
600 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
601 memset(mgmt, 0, 24 + 6);
602 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
603 IEEE80211_STYPE_AUTH);
605 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
606 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
607 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
608 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
609 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
610 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
611 ifsta->auth_transaction = transaction + 1;
612 mgmt->u.auth.status_code = cpu_to_le16(0);
614 memcpy(skb_put(skb, extra_len), extra, extra_len);
616 ieee80211_sta_tx(dev, skb, encrypt);
620 static void ieee80211_authenticate(struct net_device *dev,
621 struct ieee80211_if_sta *ifsta)
623 DECLARE_MAC_BUF(mac);
626 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
627 printk(KERN_DEBUG "%s: authentication with AP %s"
629 dev->name, print_mac(mac, ifsta->bssid));
630 ifsta->state = IEEE80211_DISABLED;
634 ifsta->state = IEEE80211_AUTHENTICATE;
635 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
636 dev->name, print_mac(mac, ifsta->bssid));
638 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
640 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
644 static void ieee80211_send_assoc(struct net_device *dev,
645 struct ieee80211_if_sta *ifsta)
647 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
649 struct ieee80211_mgmt *mgmt;
653 struct ieee80211_sta_bss *bss;
655 struct ieee80211_supported_band *sband;
657 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
658 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
661 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
662 "frame\n", dev->name);
665 skb_reserve(skb, local->hw.extra_tx_headroom);
667 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
669 capab = ifsta->capab;
671 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
672 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
673 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
674 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
675 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
678 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
679 local->hw.conf.channel->center_freq,
680 ifsta->ssid, ifsta->ssid_len);
682 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
683 capab |= WLAN_CAPABILITY_PRIVACY;
687 ieee80211_rx_bss_put(dev, bss);
690 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
692 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
693 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
694 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
696 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
698 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
699 IEEE80211_STYPE_REASSOC_REQ);
700 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
701 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
702 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
706 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
707 IEEE80211_STYPE_ASSOC_REQ);
708 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
709 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
713 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
714 *pos++ = WLAN_EID_SSID;
715 *pos++ = ifsta->ssid_len;
716 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
718 len = sband->n_bitrates;
721 pos = skb_put(skb, len + 2);
722 *pos++ = WLAN_EID_SUPP_RATES;
724 for (i = 0; i < len; i++) {
725 int rate = sband->bitrates[i].bitrate;
726 *pos++ = (u8) (rate / 5);
729 if (sband->n_bitrates > len) {
730 pos = skb_put(skb, sband->n_bitrates - len + 2);
731 *pos++ = WLAN_EID_EXT_SUPP_RATES;
732 *pos++ = sband->n_bitrates - len;
733 for (i = len; i < sband->n_bitrates; i++) {
734 int rate = sband->bitrates[i].bitrate;
735 *pos++ = (u8) (rate / 5);
739 if (ifsta->extra_ie) {
740 pos = skb_put(skb, ifsta->extra_ie_len);
741 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
744 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
745 pos = skb_put(skb, 9);
746 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
747 *pos++ = 7; /* len */
748 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
751 *pos++ = 2; /* WME */
752 *pos++ = 0; /* WME info */
753 *pos++ = 1; /* WME ver */
756 /* wmm support is a must to HT */
757 if (wmm && sband->ht_info.ht_supported) {
758 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
759 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
760 *pos++ = WLAN_EID_HT_CAPABILITY;
761 *pos++ = sizeof(struct ieee80211_ht_cap);
762 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
763 memcpy(pos, &tmp, sizeof(u16));
765 /* TODO: needs a define here for << 2 */
766 *pos++ = sband->ht_info.ampdu_factor |
767 (sband->ht_info.ampdu_density << 2);
768 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
771 kfree(ifsta->assocreq_ies);
772 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
773 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
774 if (ifsta->assocreq_ies)
775 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
777 ieee80211_sta_tx(dev, skb, 0);
781 static void ieee80211_send_deauth(struct net_device *dev,
782 struct ieee80211_if_sta *ifsta, u16 reason)
784 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
786 struct ieee80211_mgmt *mgmt;
788 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
790 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
791 "frame\n", dev->name);
794 skb_reserve(skb, local->hw.extra_tx_headroom);
796 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
798 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
799 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
800 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
801 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
802 IEEE80211_STYPE_DEAUTH);
804 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
806 ieee80211_sta_tx(dev, skb, 0);
810 static void ieee80211_send_disassoc(struct net_device *dev,
811 struct ieee80211_if_sta *ifsta, u16 reason)
813 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
815 struct ieee80211_mgmt *mgmt;
817 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
819 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
820 "frame\n", dev->name);
823 skb_reserve(skb, local->hw.extra_tx_headroom);
825 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
827 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
828 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
829 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
830 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
831 IEEE80211_STYPE_DISASSOC);
833 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
835 ieee80211_sta_tx(dev, skb, 0);
839 static int ieee80211_privacy_mismatch(struct net_device *dev,
840 struct ieee80211_if_sta *ifsta)
842 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
843 struct ieee80211_sta_bss *bss;
848 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
851 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
852 local->hw.conf.channel->center_freq,
853 ifsta->ssid, ifsta->ssid_len);
857 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
858 wep_privacy = !!ieee80211_sta_wep_configured(dev);
859 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
861 ieee80211_rx_bss_put(dev, bss);
863 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
870 static void ieee80211_associate(struct net_device *dev,
871 struct ieee80211_if_sta *ifsta)
873 DECLARE_MAC_BUF(mac);
875 ifsta->assoc_tries++;
876 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
877 printk(KERN_DEBUG "%s: association with AP %s"
879 dev->name, print_mac(mac, ifsta->bssid));
880 ifsta->state = IEEE80211_DISABLED;
884 ifsta->state = IEEE80211_ASSOCIATE;
885 printk(KERN_DEBUG "%s: associate with AP %s\n",
886 dev->name, print_mac(mac, ifsta->bssid));
887 if (ieee80211_privacy_mismatch(dev, ifsta)) {
888 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
889 "mixed-cell disabled - abort association\n", dev->name);
890 ifsta->state = IEEE80211_DISABLED;
894 ieee80211_send_assoc(dev, ifsta);
896 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
900 static void ieee80211_associated(struct net_device *dev,
901 struct ieee80211_if_sta *ifsta)
903 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
904 struct sta_info *sta;
906 DECLARE_MAC_BUF(mac);
908 /* TODO: start monitoring current AP signal quality and number of
909 * missed beacons. Scan other channels every now and then and search
911 /* TODO: remove expired BSSes */
913 ifsta->state = IEEE80211_ASSOCIATED;
917 sta = sta_info_get(local, ifsta->bssid);
919 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
920 dev->name, print_mac(mac, ifsta->bssid));
924 if (time_after(jiffies,
925 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
926 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
927 printk(KERN_DEBUG "%s: No ProbeResp from "
928 "current AP %s - assume out of "
930 dev->name, print_mac(mac, ifsta->bssid));
932 sta_info_unlink(&sta);
934 ieee80211_send_probe_req(dev, ifsta->bssid,
936 local->scan_ssid_len);
937 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
939 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
940 if (time_after(jiffies, ifsta->last_probe +
941 IEEE80211_PROBE_INTERVAL)) {
942 ifsta->last_probe = jiffies;
943 ieee80211_send_probe_req(dev, ifsta->bssid,
952 if (disassoc && sta) {
954 sta_info_destroy(sta);
959 ifsta->state = IEEE80211_DISABLED;
960 ieee80211_set_associated(dev, ifsta, 0);
962 mod_timer(&ifsta->timer, jiffies +
963 IEEE80211_MONITORING_INTERVAL);
968 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
969 u8 *ssid, size_t ssid_len)
971 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
972 struct ieee80211_supported_band *sband;
974 struct ieee80211_mgmt *mgmt;
975 u8 *pos, *supp_rates, *esupp_rates = NULL;
978 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
980 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
981 "request\n", dev->name);
984 skb_reserve(skb, local->hw.extra_tx_headroom);
986 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
988 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
989 IEEE80211_STYPE_PROBE_REQ);
990 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
992 memcpy(mgmt->da, dst, ETH_ALEN);
993 memcpy(mgmt->bssid, dst, ETH_ALEN);
995 memset(mgmt->da, 0xff, ETH_ALEN);
996 memset(mgmt->bssid, 0xff, ETH_ALEN);
998 pos = skb_put(skb, 2 + ssid_len);
999 *pos++ = WLAN_EID_SSID;
1001 memcpy(pos, ssid, ssid_len);
1003 supp_rates = skb_put(skb, 2);
1004 supp_rates[0] = WLAN_EID_SUPP_RATES;
1006 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1008 for (i = 0; i < sband->n_bitrates; i++) {
1009 struct ieee80211_rate *rate = &sband->bitrates[i];
1011 pos = skb_put(skb, 1);
1013 } else if (supp_rates[1] == 8) {
1014 esupp_rates = skb_put(skb, 3);
1015 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1017 pos = &esupp_rates[2];
1019 pos = skb_put(skb, 1);
1022 *pos = rate->bitrate / 5;
1025 ieee80211_sta_tx(dev, skb, 0);
1029 static int ieee80211_sta_wep_configured(struct net_device *dev)
1031 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1032 if (!sdata || !sdata->default_key ||
1033 sdata->default_key->conf.alg != ALG_WEP)
1039 static void ieee80211_auth_completed(struct net_device *dev,
1040 struct ieee80211_if_sta *ifsta)
1042 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
1043 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1044 ieee80211_associate(dev, ifsta);
1048 static void ieee80211_auth_challenge(struct net_device *dev,
1049 struct ieee80211_if_sta *ifsta,
1050 struct ieee80211_mgmt *mgmt,
1054 struct ieee802_11_elems elems;
1056 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1057 pos = mgmt->u.auth.variable;
1058 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1059 if (!elems.challenge) {
1060 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1061 "frame\n", dev->name);
1064 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1065 elems.challenge_len + 2, 1);
1068 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1069 u8 dialog_token, u16 status, u16 policy,
1070 u16 buf_size, u16 timeout)
1072 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1073 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1074 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1075 struct sk_buff *skb;
1076 struct ieee80211_mgmt *mgmt;
1079 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1080 sizeof(mgmt->u.action.u.addba_resp));
1082 printk(KERN_DEBUG "%s: failed to allocate buffer "
1083 "for addba resp frame\n", dev->name);
1087 skb_reserve(skb, local->hw.extra_tx_headroom);
1088 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1089 memset(mgmt, 0, 24);
1090 memcpy(mgmt->da, da, ETH_ALEN);
1091 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1092 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1093 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1095 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1096 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1097 IEEE80211_STYPE_ACTION);
1099 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1100 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1101 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1102 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1104 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1105 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1106 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1108 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1109 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1110 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1112 ieee80211_sta_tx(dev, skb, 0);
1117 void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1118 u16 tid, u8 dialog_token, u16 start_seq_num,
1119 u16 agg_size, u16 timeout)
1121 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1122 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1123 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1124 struct sk_buff *skb;
1125 struct ieee80211_mgmt *mgmt;
1128 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1129 sizeof(mgmt->u.action.u.addba_req));
1133 printk(KERN_ERR "%s: failed to allocate buffer "
1134 "for addba request frame\n", dev->name);
1137 skb_reserve(skb, local->hw.extra_tx_headroom);
1138 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1139 memset(mgmt, 0, 24);
1140 memcpy(mgmt->da, da, ETH_ALEN);
1141 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1142 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1143 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1145 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1147 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1148 IEEE80211_STYPE_ACTION);
1150 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1152 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1153 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1155 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1156 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1157 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1158 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1160 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1162 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1163 mgmt->u.action.u.addba_req.start_seq_num =
1164 cpu_to_le16(start_seq_num << 4);
1166 ieee80211_sta_tx(dev, skb, 0);
1169 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1170 struct ieee80211_mgmt *mgmt,
1173 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1174 struct ieee80211_hw *hw = &local->hw;
1175 struct ieee80211_conf *conf = &hw->conf;
1176 struct sta_info *sta;
1177 struct tid_ampdu_rx *tid_agg_rx;
1178 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1180 int ret = -EOPNOTSUPP;
1181 DECLARE_MAC_BUF(mac);
1185 sta = sta_info_get(local, mgmt->sa);
1191 /* extract session parameters from addba request frame */
1192 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1193 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1195 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1197 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1198 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1199 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1200 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1202 status = WLAN_STATUS_REQUEST_DECLINED;
1204 /* sanity check for incoming parameters:
1205 * check if configuration can support the BA policy
1206 * and if buffer size does not exceeds max value */
1207 if (((ba_policy != 1)
1208 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1209 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1210 status = WLAN_STATUS_INVALID_QOS_PARAM;
1211 #ifdef CONFIG_MAC80211_HT_DEBUG
1212 if (net_ratelimit())
1213 printk(KERN_DEBUG "AddBA Req with bad params from "
1214 "%s on tid %u. policy %d, buffer size %d\n",
1215 print_mac(mac, mgmt->sa), tid, ba_policy,
1217 #endif /* CONFIG_MAC80211_HT_DEBUG */
1220 /* determine default buffer size */
1221 if (buf_size == 0) {
1222 struct ieee80211_supported_band *sband;
1224 sband = local->hw.wiphy->bands[conf->channel->band];
1225 buf_size = IEEE80211_MIN_AMPDU_BUF;
1226 buf_size = buf_size << sband->ht_info.ampdu_factor;
1230 /* examine state machine */
1231 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1233 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1234 #ifdef CONFIG_MAC80211_HT_DEBUG
1235 if (net_ratelimit())
1236 printk(KERN_DEBUG "unexpected AddBA Req from "
1238 print_mac(mac, mgmt->sa), tid);
1239 #endif /* CONFIG_MAC80211_HT_DEBUG */
1243 /* prepare A-MPDU MLME for Rx aggregation */
1244 sta->ampdu_mlme.tid_rx[tid] =
1245 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1246 if (!sta->ampdu_mlme.tid_rx[tid]) {
1247 if (net_ratelimit())
1248 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1253 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1254 sta_rx_agg_session_timer_expired;
1255 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1256 (unsigned long)&sta->timer_to_tid[tid];
1257 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1259 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1261 /* prepare reordering buffer */
1262 tid_agg_rx->reorder_buf =
1263 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
1264 if (!tid_agg_rx->reorder_buf) {
1265 if (net_ratelimit())
1266 printk(KERN_ERR "can not allocate reordering buffer "
1267 "to tid %d\n", tid);
1268 kfree(sta->ampdu_mlme.tid_rx[tid]);
1271 memset(tid_agg_rx->reorder_buf, 0,
1272 buf_size * sizeof(struct sk_buf *));
1274 if (local->ops->ampdu_action)
1275 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1276 sta->addr, tid, &start_seq_num);
1277 #ifdef CONFIG_MAC80211_HT_DEBUG
1278 printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1279 #endif /* CONFIG_MAC80211_HT_DEBUG */
1282 kfree(tid_agg_rx->reorder_buf);
1284 sta->ampdu_mlme.tid_rx[tid] = NULL;
1288 /* change state and send addba resp */
1289 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1290 tid_agg_rx->dialog_token = dialog_token;
1291 tid_agg_rx->ssn = start_seq_num;
1292 tid_agg_rx->head_seq_num = start_seq_num;
1293 tid_agg_rx->buf_size = buf_size;
1294 tid_agg_rx->timeout = timeout;
1295 tid_agg_rx->stored_mpdu_num = 0;
1296 status = WLAN_STATUS_SUCCESS;
1298 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1301 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1302 dialog_token, status, 1, buf_size, timeout);
1306 static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1307 struct ieee80211_mgmt *mgmt,
1310 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1311 struct ieee80211_hw *hw = &local->hw;
1312 struct sta_info *sta;
1319 sta = sta_info_get(local, mgmt->sa);
1325 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1326 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1328 state = &sta->ampdu_mlme.tid_state_tx[tid];
1330 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1332 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1333 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1334 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1336 goto addba_resp_exit;
1339 if (mgmt->u.action.u.addba_resp.dialog_token !=
1340 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1341 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1342 #ifdef CONFIG_MAC80211_HT_DEBUG
1343 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1344 #endif /* CONFIG_MAC80211_HT_DEBUG */
1345 goto addba_resp_exit;
1348 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1349 #ifdef CONFIG_MAC80211_HT_DEBUG
1350 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1351 #endif /* CONFIG_MAC80211_HT_DEBUG */
1352 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1353 == WLAN_STATUS_SUCCESS) {
1354 if (*state & HT_ADDBA_RECEIVED_MSK)
1355 printk(KERN_DEBUG "double addBA response\n");
1357 *state |= HT_ADDBA_RECEIVED_MSK;
1358 sta->ampdu_mlme.addba_req_num[tid] = 0;
1360 if (*state == HT_AGG_STATE_OPERATIONAL) {
1361 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1362 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1365 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1366 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1368 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1370 sta->ampdu_mlme.addba_req_num[tid]++;
1371 /* this will allow the state check in stop_BA_session */
1372 *state = HT_AGG_STATE_OPERATIONAL;
1373 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1374 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1375 WLAN_BACK_INITIATOR);
1382 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1383 u16 initiator, u16 reason_code)
1385 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1386 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1387 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1388 struct sk_buff *skb;
1389 struct ieee80211_mgmt *mgmt;
1392 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1393 sizeof(mgmt->u.action.u.delba));
1396 printk(KERN_ERR "%s: failed to allocate buffer "
1397 "for delba frame\n", dev->name);
1401 skb_reserve(skb, local->hw.extra_tx_headroom);
1402 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1403 memset(mgmt, 0, 24);
1404 memcpy(mgmt->da, da, ETH_ALEN);
1405 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1406 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1407 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1409 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1410 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1411 IEEE80211_STYPE_ACTION);
1413 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1415 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1416 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1417 params = (u16)(initiator << 11); /* bit 11 initiator */
1418 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1420 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1421 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1423 ieee80211_sta_tx(dev, skb, 0);
1426 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1427 u16 initiator, u16 reason)
1429 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1430 struct ieee80211_hw *hw = &local->hw;
1431 struct sta_info *sta;
1436 sta = sta_info_get(local, ra);
1442 /* check if TID is in operational state */
1443 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1444 if (sta->ampdu_mlme.tid_state_rx[tid]
1445 != HT_AGG_STATE_OPERATIONAL) {
1446 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1450 sta->ampdu_mlme.tid_state_rx[tid] =
1451 HT_AGG_STATE_REQ_STOP_BA_MSK |
1452 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1453 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1455 /* stop HW Rx aggregation. ampdu_action existence
1456 * already verified in session init so we add the BUG_ON */
1457 BUG_ON(!local->ops->ampdu_action);
1459 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1462 printk(KERN_DEBUG "HW problem - can not stop rx "
1463 "aggergation for tid %d\n", tid);
1465 /* shutdown timer has not expired */
1466 if (initiator != WLAN_BACK_TIMER)
1467 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1469 /* check if this is a self generated aggregation halt */
1470 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1471 ieee80211_send_delba(dev, ra, tid, 0, reason);
1473 /* free the reordering buffer */
1474 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1475 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1476 /* release the reordered frames */
1477 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1478 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1479 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1482 /* free resources */
1483 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1484 kfree(sta->ampdu_mlme.tid_rx[tid]);
1485 sta->ampdu_mlme.tid_rx[tid] = NULL;
1486 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1492 static void ieee80211_sta_process_delba(struct net_device *dev,
1493 struct ieee80211_mgmt *mgmt, size_t len)
1495 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1496 struct sta_info *sta;
1499 DECLARE_MAC_BUF(mac);
1503 sta = sta_info_get(local, mgmt->sa);
1509 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1510 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1511 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1513 #ifdef CONFIG_MAC80211_HT_DEBUG
1514 if (net_ratelimit())
1515 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1516 print_mac(mac, mgmt->sa),
1517 initiator ? "initiator" : "recipient", tid,
1518 mgmt->u.action.u.delba.reason_code);
1519 #endif /* CONFIG_MAC80211_HT_DEBUG */
1521 if (initiator == WLAN_BACK_INITIATOR)
1522 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1523 WLAN_BACK_INITIATOR, 0);
1524 else { /* WLAN_BACK_RECIPIENT */
1525 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1526 sta->ampdu_mlme.tid_state_tx[tid] =
1527 HT_AGG_STATE_OPERATIONAL;
1528 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1529 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1530 WLAN_BACK_RECIPIENT);
1536 * After sending add Block Ack request we activated a timer until
1537 * add Block Ack response will arrive from the recipient.
1538 * If this timer expires sta_addba_resp_timer_expired will be executed.
1540 void sta_addba_resp_timer_expired(unsigned long data)
1542 /* not an elegant detour, but there is no choice as the timer passes
1543 * only one argument, and both sta_info and TID are needed, so init
1544 * flow in sta_info_create gives the TID as data, while the timer_to_id
1545 * array gives the sta through container_of */
1546 u16 tid = *(int *)data;
1547 struct sta_info *temp_sta = container_of((void *)data,
1548 struct sta_info, timer_to_tid[tid]);
1550 struct ieee80211_local *local = temp_sta->local;
1551 struct ieee80211_hw *hw = &local->hw;
1552 struct sta_info *sta;
1557 sta = sta_info_get(local, temp_sta->addr);
1563 state = &sta->ampdu_mlme.tid_state_tx[tid];
1564 /* check if the TID waits for addBA response */
1565 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1566 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1567 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1568 *state = HT_AGG_STATE_IDLE;
1569 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1570 "expecting addBA response there", tid);
1571 goto timer_expired_exit;
1574 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1576 /* go through the state check in stop_BA_session */
1577 *state = HT_AGG_STATE_OPERATIONAL;
1578 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1579 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1580 WLAN_BACK_INITIATOR);
1587 * After accepting the AddBA Request we activated a timer,
1588 * resetting it after each frame that arrives from the originator.
1589 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1591 void sta_rx_agg_session_timer_expired(unsigned long data)
1593 /* not an elegant detour, but there is no choice as the timer passes
1594 * only one argument, and verious sta_info are needed here, so init
1595 * flow in sta_info_create gives the TID as data, while the timer_to_id
1596 * array gives the sta through container_of */
1597 u8 *ptid = (u8 *)data;
1598 u8 *timer_to_id = ptid - *ptid;
1599 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1602 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1603 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1604 (u16)*ptid, WLAN_BACK_TIMER,
1605 WLAN_REASON_QSTA_TIMEOUT);
1608 void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1613 for (i = 0; i < STA_TID_NUM; i++) {
1614 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1615 WLAN_BACK_INITIATOR);
1616 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1617 WLAN_BACK_RECIPIENT,
1618 WLAN_REASON_QSTA_LEAVE_QBSS);
1622 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1623 struct ieee80211_if_sta *ifsta,
1624 struct ieee80211_mgmt *mgmt,
1627 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1628 u16 auth_alg, auth_transaction, status_code;
1629 DECLARE_MAC_BUF(mac);
1631 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1632 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
1633 printk(KERN_DEBUG "%s: authentication frame received from "
1634 "%s, but not in authenticate state - ignored\n",
1635 dev->name, print_mac(mac, mgmt->sa));
1640 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
1641 "received from %s - ignored\n",
1642 dev->name, len, print_mac(mac, mgmt->sa));
1646 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1647 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1648 printk(KERN_DEBUG "%s: authentication frame received from "
1649 "unknown AP (SA=%s BSSID=%s) - "
1650 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1651 print_mac(mac, mgmt->bssid));
1655 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1656 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1657 printk(KERN_DEBUG "%s: authentication frame received from "
1658 "unknown BSSID (SA=%s BSSID=%s) - "
1659 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1660 print_mac(mac, mgmt->bssid));
1664 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1665 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1666 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1668 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
1669 "transaction=%d status=%d)\n",
1670 dev->name, print_mac(mac, mgmt->sa), auth_alg,
1671 auth_transaction, status_code);
1673 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1674 /* IEEE 802.11 standard does not require authentication in IBSS
1675 * networks and most implementations do not seem to use it.
1676 * However, try to reply to authentication attempts if someone
1677 * has actually implemented this.
1678 * TODO: Could implement shared key authentication. */
1679 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1680 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1681 "frame (alg=%d transaction=%d)\n",
1682 dev->name, auth_alg, auth_transaction);
1685 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1688 if (auth_alg != ifsta->auth_alg ||
1689 auth_transaction != ifsta->auth_transaction) {
1690 printk(KERN_DEBUG "%s: unexpected authentication frame "
1691 "(alg=%d transaction=%d)\n",
1692 dev->name, auth_alg, auth_transaction);
1696 if (status_code != WLAN_STATUS_SUCCESS) {
1697 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1698 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1699 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1701 const int num_algs = ARRAY_SIZE(algs);
1703 algs[0] = algs[1] = algs[2] = 0xff;
1704 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1705 algs[0] = WLAN_AUTH_OPEN;
1706 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1707 algs[1] = WLAN_AUTH_SHARED_KEY;
1708 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1709 algs[2] = WLAN_AUTH_LEAP;
1710 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1712 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1716 for (i = 0; i < num_algs; i++) {
1718 if (pos >= num_algs)
1720 if (algs[pos] == ifsta->auth_alg ||
1723 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1724 !ieee80211_sta_wep_configured(dev))
1726 ifsta->auth_alg = algs[pos];
1727 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1729 dev->name, ifsta->auth_alg);
1736 switch (ifsta->auth_alg) {
1737 case WLAN_AUTH_OPEN:
1738 case WLAN_AUTH_LEAP:
1739 ieee80211_auth_completed(dev, ifsta);
1741 case WLAN_AUTH_SHARED_KEY:
1742 if (ifsta->auth_transaction == 4)
1743 ieee80211_auth_completed(dev, ifsta);
1745 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1751 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1752 struct ieee80211_if_sta *ifsta,
1753 struct ieee80211_mgmt *mgmt,
1757 DECLARE_MAC_BUF(mac);
1760 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1761 "received from %s - ignored\n",
1762 dev->name, len, print_mac(mac, mgmt->sa));
1766 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1767 printk(KERN_DEBUG "%s: deauthentication frame received from "
1768 "unknown AP (SA=%s BSSID=%s) - "
1769 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1770 print_mac(mac, mgmt->bssid));
1774 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1776 printk(KERN_DEBUG "%s: RX deauthentication from %s"
1778 dev->name, print_mac(mac, mgmt->sa), reason_code);
1780 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1781 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1784 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1785 ifsta->state == IEEE80211_ASSOCIATE ||
1786 ifsta->state == IEEE80211_ASSOCIATED) {
1787 ifsta->state = IEEE80211_AUTHENTICATE;
1788 mod_timer(&ifsta->timer, jiffies +
1789 IEEE80211_RETRY_AUTH_INTERVAL);
1792 ieee80211_set_disassoc(dev, ifsta, 1);
1793 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1797 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1798 struct ieee80211_if_sta *ifsta,
1799 struct ieee80211_mgmt *mgmt,
1803 DECLARE_MAC_BUF(mac);
1806 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1807 "received from %s - ignored\n",
1808 dev->name, len, print_mac(mac, mgmt->sa));
1812 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1813 printk(KERN_DEBUG "%s: disassociation frame received from "
1814 "unknown AP (SA=%s BSSID=%s) - "
1815 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1816 print_mac(mac, mgmt->bssid));
1820 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1822 printk(KERN_DEBUG "%s: RX disassociation from %s"
1824 dev->name, print_mac(mac, mgmt->sa), reason_code);
1826 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1827 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1829 if (ifsta->state == IEEE80211_ASSOCIATED) {
1830 ifsta->state = IEEE80211_ASSOCIATE;
1831 mod_timer(&ifsta->timer, jiffies +
1832 IEEE80211_RETRY_AUTH_INTERVAL);
1835 ieee80211_set_disassoc(dev, ifsta, 0);
1839 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1840 struct ieee80211_if_sta *ifsta,
1841 struct ieee80211_mgmt *mgmt,
1845 struct ieee80211_local *local = sdata->local;
1846 struct net_device *dev = sdata->dev;
1847 struct ieee80211_supported_band *sband;
1848 struct sta_info *sta;
1849 u64 rates, basic_rates;
1850 u16 capab_info, status_code, aid;
1851 struct ieee802_11_elems elems;
1852 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1855 DECLARE_MAC_BUF(mac);
1856 bool have_higher_than_11mbit = false;
1858 /* AssocResp and ReassocResp have identical structure, so process both
1859 * of them in this function. */
1861 if (ifsta->state != IEEE80211_ASSOCIATE) {
1862 printk(KERN_DEBUG "%s: association frame received from "
1863 "%s, but not in associate state - ignored\n",
1864 dev->name, print_mac(mac, mgmt->sa));
1869 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1870 "received from %s - ignored\n",
1871 dev->name, len, print_mac(mac, mgmt->sa));
1875 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1876 printk(KERN_DEBUG "%s: association frame received from "
1877 "unknown AP (SA=%s BSSID=%s) - "
1878 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1879 print_mac(mac, mgmt->bssid));
1883 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1884 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1885 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1887 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1888 "status=%d aid=%d)\n",
1889 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1890 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1892 if (status_code != WLAN_STATUS_SUCCESS) {
1893 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1894 dev->name, status_code);
1895 /* if this was a reassociation, ensure we try a "full"
1896 * association next time. This works around some broken APs
1897 * which do not correctly reject reassociation requests. */
1898 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1902 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1903 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1904 "set\n", dev->name, aid);
1905 aid &= ~(BIT(15) | BIT(14));
1907 pos = mgmt->u.assoc_resp.variable;
1908 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1910 if (!elems.supp_rates) {
1911 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1916 printk(KERN_DEBUG "%s: associated\n", dev->name);
1918 ifsta->ap_capab = capab_info;
1920 kfree(ifsta->assocresp_ies);
1921 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1922 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1923 if (ifsta->assocresp_ies)
1924 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1928 /* Add STA entry for the AP */
1929 sta = sta_info_get(local, ifsta->bssid);
1931 struct ieee80211_sta_bss *bss;
1934 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1936 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1937 " the AP\n", dev->name);
1941 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
1942 local->hw.conf.channel->center_freq,
1943 ifsta->ssid, ifsta->ssid_len);
1945 sta->last_rssi = bss->rssi;
1946 sta->last_signal = bss->signal;
1947 sta->last_noise = bss->noise;
1948 ieee80211_rx_bss_put(dev, bss);
1951 err = sta_info_insert(sta);
1953 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1954 " the AP (error %d)\n", dev->name, err);
1961 * FIXME: Do we really need to update the sta_info's information here?
1962 * We already know about the AP (we found it in our list) so it
1963 * should already be filled with the right info, no?
1964 * As is stands, all this is racy because typically we assume
1965 * the information that is filled in here (except flags) doesn't
1966 * change while a STA structure is alive. As such, it should move
1967 * to between the sta_info_alloc() and sta_info_insert() above.
1970 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1971 WLAN_STA_AUTHORIZED;
1975 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1977 for (i = 0; i < elems.supp_rates_len; i++) {
1978 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1981 have_higher_than_11mbit = true;
1983 for (j = 0; j < sband->n_bitrates; j++) {
1984 if (sband->bitrates[j].bitrate == rate)
1986 if (elems.supp_rates[i] & 0x80)
1987 basic_rates |= BIT(j);
1991 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1992 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1995 have_higher_than_11mbit = true;
1997 for (j = 0; j < sband->n_bitrates; j++) {
1998 if (sband->bitrates[j].bitrate == rate)
2000 if (elems.ext_supp_rates[i] & 0x80)
2001 basic_rates |= BIT(j);
2005 sta->supp_rates[local->hw.conf.channel->band] = rates;
2006 sdata->basic_rates = basic_rates;
2008 /* cf. IEEE 802.11 9.2.12 */
2009 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2010 have_higher_than_11mbit)
2011 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2013 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2015 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
2016 struct ieee80211_ht_bss_info bss_info;
2017 ieee80211_ht_cap_ie_to_ht_info(
2018 (struct ieee80211_ht_cap *)
2019 elems.ht_cap_elem, &sta->ht_info);
2020 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2021 (struct ieee80211_ht_addt_info *)
2022 elems.ht_info_elem, &bss_info);
2023 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2026 rate_control_rate_init(sta, local);
2028 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2029 sta->flags |= WLAN_STA_WME;
2031 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2032 elems.wmm_param_len);
2036 /* set AID, ieee80211_set_associated() will tell the driver */
2037 bss_conf->aid = aid;
2038 ieee80211_set_associated(dev, ifsta, 1);
2040 ieee80211_associated(dev, ifsta);
2044 /* Caller must hold local->sta_bss_lock */
2045 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2046 struct ieee80211_sta_bss *bss)
2048 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2051 if (bss_mesh_cfg(bss))
2052 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2053 bss_mesh_id_len(bss));
2055 hash_idx = STA_HASH(bss->bssid);
2057 bss->hnext = local->sta_bss_hash[hash_idx];
2058 local->sta_bss_hash[hash_idx] = bss;
2062 /* Caller must hold local->sta_bss_lock */
2063 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2064 struct ieee80211_sta_bss *bss)
2066 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2067 struct ieee80211_sta_bss *b, *prev = NULL;
2068 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2072 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2075 prev->hnext = bss->hnext;
2084 static struct ieee80211_sta_bss *
2085 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
2086 u8 *ssid, u8 ssid_len)
2088 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2089 struct ieee80211_sta_bss *bss;
2091 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2094 atomic_inc(&bss->users);
2095 atomic_inc(&bss->users);
2096 memcpy(bss->bssid, bssid, ETH_ALEN);
2098 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2099 memcpy(bss->ssid, ssid, ssid_len);
2100 bss->ssid_len = ssid_len;
2103 spin_lock_bh(&local->sta_bss_lock);
2104 /* TODO: order by RSSI? */
2105 list_add_tail(&bss->list, &local->sta_bss_list);
2106 __ieee80211_rx_bss_hash_add(dev, bss);
2107 spin_unlock_bh(&local->sta_bss_lock);
2111 static struct ieee80211_sta_bss *
2112 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
2113 u8 *ssid, u8 ssid_len)
2115 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2116 struct ieee80211_sta_bss *bss;
2118 spin_lock_bh(&local->sta_bss_lock);
2119 bss = local->sta_bss_hash[STA_HASH(bssid)];
2121 if (!bss_mesh_cfg(bss) &&
2122 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
2123 bss->freq == freq &&
2124 bss->ssid_len == ssid_len &&
2125 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
2126 atomic_inc(&bss->users);
2131 spin_unlock_bh(&local->sta_bss_lock);
2135 #ifdef CONFIG_MAC80211_MESH
2136 static struct ieee80211_sta_bss *
2137 ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2138 u8 *mesh_cfg, int freq)
2140 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2141 struct ieee80211_sta_bss *bss;
2143 spin_lock_bh(&local->sta_bss_lock);
2144 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2146 if (bss_mesh_cfg(bss) &&
2147 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
2148 bss->freq == freq &&
2149 mesh_id_len == bss->mesh_id_len &&
2150 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2152 atomic_inc(&bss->users);
2157 spin_unlock_bh(&local->sta_bss_lock);
2161 static struct ieee80211_sta_bss *
2162 ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2163 u8 *mesh_cfg, int mesh_config_len, int freq)
2165 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2166 struct ieee80211_sta_bss *bss;
2168 if (mesh_config_len != MESH_CFG_LEN)
2171 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2175 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2176 if (!bss->mesh_cfg) {
2181 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2182 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2183 if (!bss->mesh_id) {
2184 kfree(bss->mesh_cfg);
2188 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2191 atomic_inc(&bss->users);
2192 atomic_inc(&bss->users);
2193 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2194 bss->mesh_id_len = mesh_id_len;
2196 spin_lock_bh(&local->sta_bss_lock);
2197 /* TODO: order by RSSI? */
2198 list_add_tail(&bss->list, &local->sta_bss_list);
2199 __ieee80211_rx_bss_hash_add(dev, bss);
2200 spin_unlock_bh(&local->sta_bss_lock);
2205 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2211 kfree(bss_mesh_id(bss));
2212 kfree(bss_mesh_cfg(bss));
2217 static void ieee80211_rx_bss_put(struct net_device *dev,
2218 struct ieee80211_sta_bss *bss)
2220 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2221 if (!atomic_dec_and_test(&bss->users))
2224 spin_lock_bh(&local->sta_bss_lock);
2225 __ieee80211_rx_bss_hash_del(dev, bss);
2226 list_del(&bss->list);
2227 spin_unlock_bh(&local->sta_bss_lock);
2228 ieee80211_rx_bss_free(bss);
2232 void ieee80211_rx_bss_list_init(struct net_device *dev)
2234 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2235 spin_lock_init(&local->sta_bss_lock);
2236 INIT_LIST_HEAD(&local->sta_bss_list);
2240 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2242 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2243 struct ieee80211_sta_bss *bss, *tmp;
2245 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2246 ieee80211_rx_bss_put(dev, bss);
2250 static int ieee80211_sta_join_ibss(struct net_device *dev,
2251 struct ieee80211_if_sta *ifsta,
2252 struct ieee80211_sta_bss *bss)
2254 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2255 int res, rates, i, j;
2256 struct sk_buff *skb;
2257 struct ieee80211_mgmt *mgmt;
2258 struct ieee80211_tx_control control;
2259 struct rate_selection ratesel;
2261 struct ieee80211_sub_if_data *sdata;
2262 struct ieee80211_supported_band *sband;
2264 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2266 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2268 /* Remove possible STA entries from other IBSS networks. */
2269 sta_info_flush_delayed(sdata);
2271 if (local->ops->reset_tsf) {
2272 /* Reset own TSF to allow time synchronization work. */
2273 local->ops->reset_tsf(local_to_hw(local));
2275 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2276 res = ieee80211_if_config(dev);
2280 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2282 sdata->drop_unencrypted = bss->capability &
2283 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2285 res = ieee80211_set_freq(local, bss->freq);
2287 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2288 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2289 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2293 /* Set beacon template */
2294 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2299 skb_reserve(skb, local->hw.extra_tx_headroom);
2301 mgmt = (struct ieee80211_mgmt *)
2302 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2303 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2304 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2305 IEEE80211_STYPE_BEACON);
2306 memset(mgmt->da, 0xff, ETH_ALEN);
2307 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2308 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2309 mgmt->u.beacon.beacon_int =
2310 cpu_to_le16(local->hw.conf.beacon_int);
2311 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2313 pos = skb_put(skb, 2 + ifsta->ssid_len);
2314 *pos++ = WLAN_EID_SSID;
2315 *pos++ = ifsta->ssid_len;
2316 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2318 rates = bss->supp_rates_len;
2321 pos = skb_put(skb, 2 + rates);
2322 *pos++ = WLAN_EID_SUPP_RATES;
2324 memcpy(pos, bss->supp_rates, rates);
2326 if (bss->band == IEEE80211_BAND_2GHZ) {
2327 pos = skb_put(skb, 2 + 1);
2328 *pos++ = WLAN_EID_DS_PARAMS;
2330 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2333 pos = skb_put(skb, 2 + 2);
2334 *pos++ = WLAN_EID_IBSS_PARAMS;
2336 /* FIX: set ATIM window based on scan results */
2340 if (bss->supp_rates_len > 8) {
2341 rates = bss->supp_rates_len - 8;
2342 pos = skb_put(skb, 2 + rates);
2343 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2345 memcpy(pos, &bss->supp_rates[8], rates);
2348 memset(&control, 0, sizeof(control));
2349 rate_control_get_rate(dev, sband, skb, &ratesel);
2350 if (!ratesel.rate) {
2351 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2352 "for IBSS beacon\n", dev->name);
2355 control.vif = &sdata->vif;
2356 control.tx_rate = ratesel.rate;
2357 if (sdata->bss_conf.use_short_preamble &&
2358 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2359 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2360 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2361 control.flags |= IEEE80211_TXCTL_NO_ACK;
2362 control.retry_limit = 1;
2364 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2365 if (ifsta->probe_resp) {
2366 mgmt = (struct ieee80211_mgmt *)
2367 ifsta->probe_resp->data;
2368 mgmt->frame_control =
2369 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2370 IEEE80211_STYPE_PROBE_RESP);
2372 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2373 "template for IBSS\n", dev->name);
2376 if (local->ops->beacon_update &&
2377 local->ops->beacon_update(local_to_hw(local),
2378 skb, &control) == 0) {
2379 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2380 "template\n", dev->name);
2385 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2386 for (i = 0; i < bss->supp_rates_len; i++) {
2387 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2388 for (j = 0; j < sband->n_bitrates; j++)
2389 if (sband->bitrates[j].bitrate == bitrate)
2392 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2394 ieee80211_sta_def_wmm_params(dev, bss, 1);
2398 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2399 "template\n", dev->name);
2403 ifsta->state = IEEE80211_IBSS_JOINED;
2404 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2406 ieee80211_rx_bss_put(dev, bss);
2411 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2412 struct ieee802_11_elems *elems,
2413 enum ieee80211_band band)
2415 struct ieee80211_supported_band *sband;
2416 struct ieee80211_rate *bitrates;
2420 sband = local->hw.wiphy->bands[band];
2424 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2427 bitrates = sband->bitrates;
2428 num_rates = sband->n_bitrates;
2430 for (i = 0; i < elems->supp_rates_len +
2431 elems->ext_supp_rates_len; i++) {
2434 if (i < elems->supp_rates_len)
2435 rate = elems->supp_rates[i];
2436 else if (elems->ext_supp_rates)
2437 rate = elems->ext_supp_rates
2438 [i - elems->supp_rates_len];
2439 own_rate = 5 * (rate & 0x7f);
2440 for (j = 0; j < num_rates; j++)
2441 if (bitrates[j].bitrate == own_rate)
2442 supp_rates |= BIT(j);
2448 static void ieee80211_rx_bss_info(struct net_device *dev,
2449 struct ieee80211_mgmt *mgmt,
2451 struct ieee80211_rx_status *rx_status,
2454 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2455 struct ieee802_11_elems elems;
2458 struct ieee80211_sta_bss *bss;
2459 struct sta_info *sta;
2460 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2461 u64 beacon_timestamp, rx_timestamp;
2462 struct ieee80211_channel *channel;
2463 DECLARE_MAC_BUF(mac);
2464 DECLARE_MAC_BUF(mac2);
2466 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2467 return; /* ignore ProbeResp to foreign address */
2470 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
2471 dev->name, beacon ? "Beacon" : "Probe Response",
2472 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
2475 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2479 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2480 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2482 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2483 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2484 u64 rates = ieee80211_sta_get_rates(local, &elems,
2487 mesh_neighbour_update(mgmt->sa, rates, dev,
2488 mesh_peer_accepts_plinks(&elems, dev));
2493 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
2494 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2495 (sta = sta_info_get(local, mgmt->sa))) {
2497 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2500 prev_rates = sta->supp_rates[rx_status->band];
2501 sta->supp_rates[rx_status->band] &= supp_rates;
2502 if (sta->supp_rates[rx_status->band] == 0) {
2503 /* No matching rates - this should not really happen.
2504 * Make sure that at least one rate is marked
2505 * supported to avoid issues with TX rate ctrl. */
2506 sta->supp_rates[rx_status->band] =
2507 sdata->u.sta.supp_rates_bits[rx_status->band];
2509 if (sta->supp_rates[rx_status->band] != prev_rates) {
2510 printk(KERN_DEBUG "%s: updated supp_rates set for "
2511 "%s based on beacon info (0x%llx & 0x%llx -> "
2513 dev->name, print_mac(mac, sta->addr),
2514 (unsigned long long) prev_rates,
2515 (unsigned long long) supp_rates,
2516 (unsigned long long) sta->supp_rates[rx_status->band]);
2522 if (elems.ds_params && elems.ds_params_len == 1)
2523 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
2525 freq = rx_status->freq;
2527 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2529 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2532 #ifdef CONFIG_MAC80211_MESH
2533 if (elems.mesh_config)
2534 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2535 elems.mesh_id_len, elems.mesh_config, freq);
2538 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
2539 elems.ssid, elems.ssid_len);
2541 #ifdef CONFIG_MAC80211_MESH
2542 if (elems.mesh_config)
2543 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2544 elems.mesh_id_len, elems.mesh_config,
2545 elems.mesh_config_len, freq);
2548 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2549 elems.ssid, elems.ssid_len);
2554 /* TODO: order by RSSI? */
2555 spin_lock_bh(&local->sta_bss_lock);
2556 list_move_tail(&bss->list, &local->sta_bss_list);
2557 spin_unlock_bh(&local->sta_bss_lock);
2561 bss->band = rx_status->band;
2563 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2564 bss->probe_resp && beacon) {
2566 * Do not allow beacon to override data from Probe Response. */
2567 ieee80211_rx_bss_put(dev, bss);
2571 /* save the ERP value so that it is available at association time */
2572 if (elems.erp_info && elems.erp_info_len >= 1) {
2573 bss->erp_value = elems.erp_info[0];
2574 bss->has_erp_value = 1;
2577 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2578 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2580 bss->supp_rates_len = 0;
2581 if (elems.supp_rates) {
2582 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2583 if (clen > elems.supp_rates_len)
2584 clen = elems.supp_rates_len;
2585 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2587 bss->supp_rates_len += clen;
2589 if (elems.ext_supp_rates) {
2590 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2591 if (clen > elems.ext_supp_rates_len)
2592 clen = elems.ext_supp_rates_len;
2593 memcpy(&bss->supp_rates[bss->supp_rates_len],
2594 elems.ext_supp_rates, clen);
2595 bss->supp_rates_len += clen;
2599 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2600 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2602 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2604 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2605 bss->wpa_ie_len = elems.wpa_len + 2;
2607 bss->wpa_ie_len = 0;
2608 } else if (!elems.wpa && bss->wpa_ie) {
2611 bss->wpa_ie_len = 0;
2615 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2616 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2618 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2620 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2621 bss->rsn_ie_len = elems.rsn_len + 2;
2623 bss->rsn_ie_len = 0;
2624 } else if (!elems.rsn && bss->rsn_ie) {
2627 bss->rsn_ie_len = 0;
2630 if (elems.wmm_param &&
2631 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2632 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2634 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2636 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2637 elems.wmm_param_len + 2);
2638 bss->wmm_ie_len = elems.wmm_param_len + 2;
2640 bss->wmm_ie_len = 0;
2641 } else if (!elems.wmm_param && bss->wmm_ie) {
2644 bss->wmm_ie_len = 0;
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->timestamp = beacon_timestamp;
2664 bss->last_update = jiffies;
2665 bss->rssi = rx_status->ssi;
2666 bss->signal = rx_status->signal;
2667 bss->noise = rx_status->noise;
2671 /* check if we need to merge IBSS */
2672 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2673 !local->sta_sw_scanning && !local->sta_hw_scanning &&
2674 bss->capability & WLAN_CAPABILITY_IBSS &&
2675 bss->freq == local->oper_channel->center_freq &&
2676 elems.ssid_len == sdata->u.sta.ssid_len &&
2677 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2678 if (rx_status->flag & RX_FLAG_TSFT) {
2679 /* in order for correct IBSS merging we need mactime
2681 * since mactime is defined as the time the first data
2682 * symbol of the frame hits the PHY, and the timestamp
2683 * of the beacon is defined as "the time that the data
2684 * symbol containing the first bit of the timestamp is
2685 * transmitted to the PHY plus the transmitting STA’s
2686 * delays through its local PHY from the MAC-PHY
2687 * interface to its interface with the WM"
2688 * (802.11 11.1.2) - equals the time this bit arrives at
2689 * the receiver - we have to take into account the
2690 * offset between the two.
2691 * e.g: at 1 MBit that means mactime is 192 usec earlier
2692 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2694 int rate = local->hw.wiphy->bands[rx_status->band]->
2695 bitrates[rx_status->rate_idx].bitrate;
2696 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2697 } else if (local && local->ops && local->ops->get_tsf)
2698 /* second best option: get current TSF */
2699 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2701 /* can't merge without knowing the TSF */
2702 rx_timestamp = -1LLU;
2703 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2704 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2705 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2706 print_mac(mac, mgmt->sa),
2707 print_mac(mac2, mgmt->bssid),
2708 (unsigned long long)rx_timestamp,
2709 (unsigned long long)beacon_timestamp,
2710 (unsigned long long)(rx_timestamp - beacon_timestamp),
2712 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2713 if (beacon_timestamp > rx_timestamp) {
2714 #ifndef CONFIG_MAC80211_IBSS_DEBUG
2715 if (net_ratelimit())
2717 printk(KERN_DEBUG "%s: beacon TSF higher than "
2718 "local TSF - IBSS merge with BSSID %s\n",
2719 dev->name, print_mac(mac, mgmt->bssid));
2720 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2721 ieee80211_ibss_add_sta(dev, NULL,
2722 mgmt->bssid, mgmt->sa);
2726 ieee80211_rx_bss_put(dev, bss);
2730 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2731 struct ieee80211_mgmt *mgmt,
2733 struct ieee80211_rx_status *rx_status)
2735 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2739 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2740 struct ieee80211_mgmt *mgmt,
2742 struct ieee80211_rx_status *rx_status)
2744 struct ieee80211_sub_if_data *sdata;
2745 struct ieee80211_if_sta *ifsta;
2747 struct ieee802_11_elems elems;
2748 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2749 struct ieee80211_conf *conf = &local->hw.conf;
2752 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2754 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2755 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2757 ifsta = &sdata->u.sta;
2759 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2760 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2763 /* Process beacon from the current BSS */
2764 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2768 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2770 if (elems.erp_info && elems.erp_info_len >= 1)
2771 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2773 if (elems.ht_cap_elem && elems.ht_info_elem &&
2774 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2775 struct ieee80211_ht_bss_info bss_info;
2777 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2778 (struct ieee80211_ht_addt_info *)
2779 elems.ht_info_elem, &bss_info);
2780 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2784 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2785 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2786 elems.wmm_param_len);
2789 ieee80211_bss_info_change_notify(sdata, changed);
2793 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2794 struct ieee80211_if_sta *ifsta,
2795 struct ieee80211_mgmt *mgmt,
2797 struct ieee80211_rx_status *rx_status)
2799 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2800 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2802 struct sk_buff *skb;
2803 struct ieee80211_mgmt *resp;
2805 DECLARE_MAC_BUF(mac);
2806 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2807 DECLARE_MAC_BUF(mac2);
2808 DECLARE_MAC_BUF(mac3);
2811 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2812 ifsta->state != IEEE80211_IBSS_JOINED ||
2813 len < 24 + 2 || !ifsta->probe_resp)
2816 if (local->ops->tx_last_beacon)
2817 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2821 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2822 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2823 "%s (tx_last_beacon=%d)\n",
2824 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2825 print_mac(mac3, mgmt->bssid), tx_last_beacon);
2826 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2828 if (!tx_last_beacon)
2831 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2832 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2835 end = ((u8 *) mgmt) + len;
2836 pos = mgmt->u.probe_req.variable;
2837 if (pos[0] != WLAN_EID_SSID ||
2838 pos + 2 + pos[1] > end) {
2839 if (net_ratelimit()) {
2840 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2842 dev->name, print_mac(mac, mgmt->sa));
2847 (pos[1] != ifsta->ssid_len ||
2848 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2849 /* Ignore ProbeReq for foreign SSID */
2853 /* Reply with ProbeResp */
2854 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2858 resp = (struct ieee80211_mgmt *) skb->data;
2859 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2860 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2861 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2862 dev->name, print_mac(mac, resp->da));
2863 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2864 ieee80211_sta_tx(dev, skb, 0);
2867 static void ieee80211_rx_mgmt_action(struct net_device *dev,
2868 struct ieee80211_if_sta *ifsta,
2869 struct ieee80211_mgmt *mgmt,
2871 struct ieee80211_rx_status *rx_status)
2873 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2875 if (len < IEEE80211_MIN_ACTION_SIZE)
2878 switch (mgmt->u.action.category) {
2879 case WLAN_CATEGORY_BACK:
2880 switch (mgmt->u.action.u.addba_req.action_code) {
2881 case WLAN_ACTION_ADDBA_REQ:
2882 if (len < (IEEE80211_MIN_ACTION_SIZE +
2883 sizeof(mgmt->u.action.u.addba_req)))
2885 ieee80211_sta_process_addba_request(dev, mgmt, len);
2887 case WLAN_ACTION_ADDBA_RESP:
2888 if (len < (IEEE80211_MIN_ACTION_SIZE +
2889 sizeof(mgmt->u.action.u.addba_resp)))
2891 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2893 case WLAN_ACTION_DELBA:
2894 if (len < (IEEE80211_MIN_ACTION_SIZE +
2895 sizeof(mgmt->u.action.u.delba)))
2897 ieee80211_sta_process_delba(dev, mgmt, len);
2900 if (net_ratelimit())
2901 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
2906 case PLINK_CATEGORY:
2907 if (ieee80211_vif_is_mesh(&sdata->vif))
2908 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2910 case MESH_PATH_SEL_CATEGORY:
2911 if (ieee80211_vif_is_mesh(&sdata->vif))
2912 mesh_rx_path_sel_frame(dev, mgmt, len);
2915 if (net_ratelimit())
2916 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2917 "category=%d\n", dev->name, mgmt->u.action.category);
2922 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2923 struct ieee80211_rx_status *rx_status)
2925 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2926 struct ieee80211_sub_if_data *sdata;
2927 struct ieee80211_if_sta *ifsta;
2928 struct ieee80211_mgmt *mgmt;
2934 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2935 ifsta = &sdata->u.sta;
2937 mgmt = (struct ieee80211_mgmt *) skb->data;
2938 fc = le16_to_cpu(mgmt->frame_control);
2940 switch (fc & IEEE80211_FCTL_STYPE) {
2941 case IEEE80211_STYPE_PROBE_REQ:
2942 case IEEE80211_STYPE_PROBE_RESP:
2943 case IEEE80211_STYPE_BEACON:
2944 case IEEE80211_STYPE_ACTION:
2945 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2946 case IEEE80211_STYPE_AUTH:
2947 case IEEE80211_STYPE_ASSOC_RESP:
2948 case IEEE80211_STYPE_REASSOC_RESP:
2949 case IEEE80211_STYPE_DEAUTH:
2950 case IEEE80211_STYPE_DISASSOC:
2951 skb_queue_tail(&ifsta->skb_queue, skb);
2952 queue_work(local->hw.workqueue, &ifsta->work);
2955 printk(KERN_DEBUG "%s: received unknown management frame - "
2956 "stype=%d\n", dev->name,
2957 (fc & IEEE80211_FCTL_STYPE) >> 4);
2966 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2967 struct sk_buff *skb)
2969 struct ieee80211_rx_status *rx_status;
2970 struct ieee80211_sub_if_data *sdata;
2971 struct ieee80211_if_sta *ifsta;
2972 struct ieee80211_mgmt *mgmt;
2975 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2976 ifsta = &sdata->u.sta;
2978 rx_status = (struct ieee80211_rx_status *) skb->cb;
2979 mgmt = (struct ieee80211_mgmt *) skb->data;
2980 fc = le16_to_cpu(mgmt->frame_control);
2982 switch (fc & IEEE80211_FCTL_STYPE) {
2983 case IEEE80211_STYPE_PROBE_REQ:
2984 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2987 case IEEE80211_STYPE_PROBE_RESP:
2988 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2990 case IEEE80211_STYPE_BEACON:
2991 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2993 case IEEE80211_STYPE_AUTH:
2994 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2996 case IEEE80211_STYPE_ASSOC_RESP:
2997 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2999 case IEEE80211_STYPE_REASSOC_RESP:
3000 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
3002 case IEEE80211_STYPE_DEAUTH:
3003 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3005 case IEEE80211_STYPE_DISASSOC:
3006 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3008 case IEEE80211_STYPE_ACTION:
3009 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
3018 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3019 struct ieee80211_rx_status *rx_status)
3021 struct ieee80211_mgmt *mgmt;
3025 return RX_DROP_UNUSABLE;
3027 mgmt = (struct ieee80211_mgmt *) skb->data;
3028 fc = le16_to_cpu(mgmt->frame_control);
3030 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
3034 return RX_DROP_MONITOR;
3036 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3037 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3038 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3039 skb->len, rx_status);
3042 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3043 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3053 static int ieee80211_sta_active_ibss(struct net_device *dev)
3055 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3057 struct sta_info *sta;
3058 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3062 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3063 if (sta->sdata == sdata &&
3064 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3077 static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
3079 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3080 struct sta_info *sta, *tmp;
3081 LIST_HEAD(tmp_list);
3082 DECLARE_MAC_BUF(mac);
3083 unsigned long flags;
3085 spin_lock_irqsave(&local->sta_lock, flags);
3086 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3087 if (time_after(jiffies, sta->last_rx + exp_time)) {
3088 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3089 dev->name, print_mac(mac, sta->addr));
3090 __sta_info_unlink(&sta);
3092 list_add(&sta->list, &tmp_list);
3094 spin_unlock_irqrestore(&local->sta_lock, flags);
3099 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3100 sta_info_destroy(sta);
3105 static void ieee80211_sta_merge_ibss(struct net_device *dev,
3106 struct ieee80211_if_sta *ifsta)
3108 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3110 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
3111 if (ieee80211_sta_active_ibss(dev))
3114 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3115 "IBSS networks with same SSID (merge)\n", dev->name);
3116 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3120 #ifdef CONFIG_MAC80211_MESH
3121 static void ieee80211_mesh_housekeeping(struct net_device *dev,
3122 struct ieee80211_if_sta *ifsta)
3124 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3127 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3128 mesh_path_expire(dev);
3130 free_plinks = mesh_plink_availables(sdata);
3131 if (free_plinks != sdata->u.sta.accepting_plinks)
3132 ieee80211_if_config_beacon(dev);
3134 mod_timer(&ifsta->timer, jiffies +
3135 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3139 void ieee80211_start_mesh(struct net_device *dev)
3141 struct ieee80211_if_sta *ifsta;
3142 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3143 ifsta = &sdata->u.sta;
3144 ifsta->state = IEEE80211_MESH_UP;
3145 ieee80211_sta_timer((unsigned long)sdata);
3150 void ieee80211_sta_timer(unsigned long data)
3152 struct ieee80211_sub_if_data *sdata =
3153 (struct ieee80211_sub_if_data *) data;
3154 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3155 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3157 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3158 queue_work(local->hw.workqueue, &ifsta->work);
3161 void ieee80211_sta_work(struct work_struct *work)
3163 struct ieee80211_sub_if_data *sdata =
3164 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3165 struct net_device *dev = sdata->dev;
3166 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3167 struct ieee80211_if_sta *ifsta;
3168 struct sk_buff *skb;
3170 if (!netif_running(dev))
3173 if (local->sta_sw_scanning || local->sta_hw_scanning)
3176 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3177 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3178 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
3179 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
3180 "(type=%d)\n", dev->name, sdata->vif.type);
3183 ifsta = &sdata->u.sta;
3185 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3186 ieee80211_sta_rx_queued_mgmt(dev, skb);
3188 #ifdef CONFIG_MAC80211_MESH
3189 if (ifsta->preq_queue_len &&
3191 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3192 mesh_path_start_discovery(dev);
3195 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3196 ifsta->state != IEEE80211_ASSOCIATE &&
3197 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3198 if (ifsta->scan_ssid_len)
3199 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3201 ieee80211_sta_start_scan(dev, NULL, 0);
3205 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3206 if (ieee80211_sta_config_auth(dev, ifsta))
3208 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3209 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3212 switch (ifsta->state) {
3213 case IEEE80211_DISABLED:
3215 case IEEE80211_AUTHENTICATE:
3216 ieee80211_authenticate(dev, ifsta);
3218 case IEEE80211_ASSOCIATE:
3219 ieee80211_associate(dev, ifsta);
3221 case IEEE80211_ASSOCIATED:
3222 ieee80211_associated(dev, ifsta);
3224 case IEEE80211_IBSS_SEARCH:
3225 ieee80211_sta_find_ibss(dev, ifsta);
3227 case IEEE80211_IBSS_JOINED:
3228 ieee80211_sta_merge_ibss(dev, ifsta);
3230 #ifdef CONFIG_MAC80211_MESH
3231 case IEEE80211_MESH_UP:
3232 ieee80211_mesh_housekeeping(dev, ifsta);
3236 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3241 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3242 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3243 "mixed-cell disabled - disassociate\n", dev->name);
3245 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3246 ieee80211_set_disassoc(dev, ifsta, 0);
3251 static void ieee80211_sta_reset_auth(struct net_device *dev,
3252 struct ieee80211_if_sta *ifsta)
3254 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3256 if (local->ops->reset_tsf) {
3257 /* Reset own TSF to allow time synchronization work. */
3258 local->ops->reset_tsf(local_to_hw(local));
3261 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3264 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3265 ifsta->auth_alg = WLAN_AUTH_OPEN;
3266 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3267 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3268 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3269 ifsta->auth_alg = WLAN_AUTH_LEAP;
3271 ifsta->auth_alg = WLAN_AUTH_OPEN;
3272 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3274 ifsta->auth_transaction = -1;
3275 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3276 ifsta->auth_tries = ifsta->assoc_tries = 0;
3277 netif_carrier_off(dev);
3281 void ieee80211_sta_req_auth(struct net_device *dev,
3282 struct ieee80211_if_sta *ifsta)
3284 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3285 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3287 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3290 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3291 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3292 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3293 IEEE80211_STA_AUTO_SSID_SEL))) {
3294 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3295 queue_work(local->hw.workqueue, &ifsta->work);
3299 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3300 const char *ssid, int ssid_len)
3302 int tmp, hidden_ssid;
3304 if (ssid_len == ifsta->ssid_len &&
3305 !memcmp(ifsta->ssid, ssid, ssid_len))
3308 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3314 if (ssid[tmp] != '\0') {
3320 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3323 if (ssid_len == 1 && ssid[0] == ' ')
3329 static int ieee80211_sta_config_auth(struct net_device *dev,
3330 struct ieee80211_if_sta *ifsta)
3332 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3333 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3334 struct ieee80211_sta_bss *bss, *selected = NULL;
3335 int top_rssi = 0, freq;
3337 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3338 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
3339 ifsta->state = IEEE80211_AUTHENTICATE;
3340 ieee80211_sta_reset_auth(dev, ifsta);
3344 spin_lock_bh(&local->sta_bss_lock);
3345 freq = local->oper_channel->center_freq;
3346 list_for_each_entry(bss, &local->sta_bss_list, list) {
3347 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3350 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3351 !!sdata->default_key)
3354 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3358 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
3359 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3362 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
3363 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3366 if (!selected || top_rssi < bss->rssi) {
3368 top_rssi = bss->rssi;
3372 atomic_inc(&selected->users);
3373 spin_unlock_bh(&local->sta_bss_lock);
3376 ieee80211_set_freq(local, selected->freq);
3377 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
3378 ieee80211_sta_set_ssid(dev, selected->ssid,
3379 selected->ssid_len);
3380 ieee80211_sta_set_bssid(dev, selected->bssid);
3381 ieee80211_sta_def_wmm_params(dev, selected, 0);
3382 ieee80211_rx_bss_put(dev, selected);
3383 ifsta->state = IEEE80211_AUTHENTICATE;
3384 ieee80211_sta_reset_auth(dev, ifsta);
3387 if (ifsta->state != IEEE80211_AUTHENTICATE) {
3388 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
3389 ieee80211_sta_start_scan(dev, NULL, 0);
3391 ieee80211_sta_start_scan(dev, ifsta->ssid,
3393 ifsta->state = IEEE80211_AUTHENTICATE;
3394 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3396 ifsta->state = IEEE80211_DISABLED;
3402 static int ieee80211_sta_create_ibss(struct net_device *dev,
3403 struct ieee80211_if_sta *ifsta)
3405 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3406 struct ieee80211_sta_bss *bss;
3407 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3408 struct ieee80211_supported_band *sband;
3409 u8 bssid[ETH_ALEN], *pos;
3411 DECLARE_MAC_BUF(mac);
3414 /* Easier testing, use fixed BSSID. */
3415 memset(bssid, 0xfe, ETH_ALEN);
3417 /* Generate random, not broadcast, locally administered BSSID. Mix in
3418 * own MAC address to make sure that devices that do not have proper
3419 * random number generator get different BSSID. */
3420 get_random_bytes(bssid, ETH_ALEN);
3421 for (i = 0; i < ETH_ALEN; i++)
3422 bssid[i] ^= dev->dev_addr[i];
3427 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3428 dev->name, print_mac(mac, bssid));
3430 bss = ieee80211_rx_bss_add(dev, bssid,
3431 local->hw.conf.channel->center_freq,
3432 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3436 bss->band = local->hw.conf.channel->band;
3437 sband = local->hw.wiphy->bands[bss->band];
3439 if (local->hw.conf.beacon_int == 0)
3440 local->hw.conf.beacon_int = 10000;
3441 bss->beacon_int = local->hw.conf.beacon_int;
3442 bss->last_update = jiffies;
3443 bss->capability = WLAN_CAPABILITY_IBSS;
3444 if (sdata->default_key) {
3445 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3447 sdata->drop_unencrypted = 0;
3448 bss->supp_rates_len = sband->n_bitrates;
3449 pos = bss->supp_rates;
3450 for (i = 0; i < sband->n_bitrates; i++) {
3451 int rate = sband->bitrates[i].bitrate;
3452 *pos++ = (u8) (rate / 5);
3455 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3459 static int ieee80211_sta_find_ibss(struct net_device *dev,
3460 struct ieee80211_if_sta *ifsta)
3462 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3463 struct ieee80211_sta_bss *bss;
3467 DECLARE_MAC_BUF(mac);
3468 DECLARE_MAC_BUF(mac2);
3470 if (ifsta->ssid_len == 0)
3473 active_ibss = ieee80211_sta_active_ibss(dev);
3474 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3475 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3476 dev->name, active_ibss);
3477 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3478 spin_lock_bh(&local->sta_bss_lock);
3479 list_for_each_entry(bss, &local->sta_bss_list, list) {
3480 if (ifsta->ssid_len != bss->ssid_len ||
3481 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3482 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3484 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3485 printk(KERN_DEBUG " bssid=%s found\n",
3486 print_mac(mac, bss->bssid));
3487 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3488 memcpy(bssid, bss->bssid, ETH_ALEN);
3490 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3493 spin_unlock_bh(&local->sta_bss_lock);
3495 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3496 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3497 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
3498 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3499 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
3500 (bss = ieee80211_rx_bss_get(dev, bssid,
3501 local->hw.conf.channel->center_freq,
3502 ifsta->ssid, ifsta->ssid_len))) {
3503 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3504 " based on configured SSID\n",
3505 dev->name, print_mac(mac, bssid));
3506 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3508 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3509 printk(KERN_DEBUG " did not try to join ibss\n");
3510 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3512 /* Selected IBSS not found in current scan results - try to scan */
3513 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3514 !ieee80211_sta_active_ibss(dev)) {
3515 mod_timer(&ifsta->timer, jiffies +
3516 IEEE80211_IBSS_MERGE_INTERVAL);
3517 } else if (time_after(jiffies, local->last_scan_completed +
3518 IEEE80211_SCAN_INTERVAL)) {
3519 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3520 "join\n", dev->name);
3521 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3523 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3524 int interval = IEEE80211_SCAN_INTERVAL;
3526 if (time_after(jiffies, ifsta->ibss_join_req +
3527 IEEE80211_IBSS_JOIN_TIMEOUT)) {
3528 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3529 (!(local->oper_channel->flags &
3530 IEEE80211_CHAN_NO_IBSS)))
3531 return ieee80211_sta_create_ibss(dev, ifsta);
3532 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3533 printk(KERN_DEBUG "%s: IBSS not allowed on"
3534 " %d MHz\n", dev->name,
3535 local->hw.conf.channel->center_freq);
3538 /* No IBSS found - decrease scan interval and continue
3540 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3543 ifsta->state = IEEE80211_IBSS_SEARCH;
3544 mod_timer(&ifsta->timer, jiffies + interval);
3552 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3554 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3555 struct ieee80211_if_sta *ifsta;
3557 if (len > IEEE80211_MAX_SSID_LEN)
3560 ifsta = &sdata->u.sta;
3562 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
3563 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3564 memcpy(ifsta->ssid, ssid, len);
3565 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3566 ifsta->ssid_len = len;
3569 ifsta->flags |= IEEE80211_STA_SSID_SET;
3571 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3572 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3573 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3574 ifsta->ibss_join_req = jiffies;
3575 ifsta->state = IEEE80211_IBSS_SEARCH;
3576 return ieee80211_sta_find_ibss(dev, ifsta);
3582 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3584 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3585 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3586 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3587 *len = ifsta->ssid_len;
3592 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3594 struct ieee80211_sub_if_data *sdata;
3595 struct ieee80211_if_sta *ifsta;
3598 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3599 ifsta = &sdata->u.sta;
3601 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3602 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3603 res = ieee80211_if_config(dev);
3605 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3606 "the low-level driver\n", dev->name);
3611 if (is_valid_ether_addr(bssid))
3612 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3614 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3620 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3621 struct ieee80211_sub_if_data *sdata,
3624 struct sk_buff *skb;
3625 struct ieee80211_hdr *nullfunc;
3628 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3630 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3631 "frame\n", sdata->dev->name);
3634 skb_reserve(skb, local->hw.extra_tx_headroom);
3636 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3637 memset(nullfunc, 0, 24);
3638 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3639 IEEE80211_FCTL_TODS;
3641 fc |= IEEE80211_FCTL_PM;
3642 nullfunc->frame_control = cpu_to_le16(fc);
3643 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3644 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3645 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3647 ieee80211_sta_tx(sdata->dev, skb, 0);
3651 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3653 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3654 ieee80211_vif_is_mesh(&sdata->vif))
3655 ieee80211_sta_timer((unsigned long)sdata);
3658 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3660 struct ieee80211_local *local = hw_to_local(hw);
3661 struct net_device *dev = local->scan_dev;
3662 struct ieee80211_sub_if_data *sdata;
3663 union iwreq_data wrqu;
3665 local->last_scan_completed = jiffies;
3666 memset(&wrqu, 0, sizeof(wrqu));
3667 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3669 if (local->sta_hw_scanning) {
3670 local->sta_hw_scanning = 0;
3671 if (ieee80211_hw_config(local))
3672 printk(KERN_DEBUG "%s: failed to restore operational "
3673 "channel after scan\n", dev->name);
3674 /* Restart STA timer for HW scan case */
3676 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3677 ieee80211_restart_sta_timer(sdata);
3683 local->sta_sw_scanning = 0;
3684 if (ieee80211_hw_config(local))
3685 printk(KERN_DEBUG "%s: failed to restore operational "
3686 "channel after scan\n", dev->name);
3689 netif_tx_lock_bh(local->mdev);
3690 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3691 local->ops->configure_filter(local_to_hw(local),
3692 FIF_BCN_PRBRESP_PROMISC,
3693 &local->filter_flags,
3694 local->mdev->mc_count,
3695 local->mdev->mc_list);
3697 netif_tx_unlock_bh(local->mdev);
3700 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3702 /* No need to wake the master device. */
3703 if (sdata->dev == local->mdev)
3706 /* Tell AP we're back */
3707 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3708 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3709 ieee80211_send_nullfunc(local, sdata, 0);
3711 ieee80211_restart_sta_timer(sdata);
3713 netif_wake_queue(sdata->dev);
3718 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3719 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3720 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3721 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3722 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3723 !ieee80211_sta_active_ibss(dev)))
3724 ieee80211_sta_find_ibss(dev, ifsta);
3727 EXPORT_SYMBOL(ieee80211_scan_completed);
3729 void ieee80211_sta_scan_work(struct work_struct *work)
3731 struct ieee80211_local *local =
3732 container_of(work, struct ieee80211_local, scan_work.work);
3733 struct net_device *dev = local->scan_dev;
3734 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3735 struct ieee80211_supported_band *sband;
3736 struct ieee80211_channel *chan;
3738 unsigned long next_delay = 0;
3740 if (!local->sta_sw_scanning)
3743 switch (local->scan_state) {
3744 case SCAN_SET_CHANNEL:
3746 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3747 * after we successfully scanned the last channel of the last
3748 * band (and the last band is supported by the hw)
3750 if (local->scan_band < IEEE80211_NUM_BANDS)
3751 sband = local->hw.wiphy->bands[local->scan_band];
3756 * If we are at an unsupported band and have more bands
3757 * left to scan, advance to the next supported one.
3759 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3761 sband = local->hw.wiphy->bands[local->scan_band];
3762 local->scan_channel_idx = 0;
3765 /* if no more bands/channels left, complete scan */
3766 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3767 ieee80211_scan_completed(local_to_hw(local));
3771 chan = &sband->channels[local->scan_channel_idx];
3773 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3774 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3775 chan->flags & IEEE80211_CHAN_NO_IBSS))
3779 local->scan_channel = chan;
3780 if (ieee80211_hw_config(local)) {
3781 printk(KERN_DEBUG "%s: failed to set freq to "
3782 "%d MHz for scan\n", dev->name,
3788 /* advance state machine to next channel/band */
3789 local->scan_channel_idx++;
3790 if (local->scan_channel_idx >= sband->n_channels) {
3792 * scan_band may end up == IEEE80211_NUM_BANDS, but
3793 * we'll catch that case above and complete the scan
3794 * if that is the case.
3797 local->scan_channel_idx = 0;
3803 next_delay = IEEE80211_PROBE_DELAY +
3804 usecs_to_jiffies(local->hw.channel_change_time);
3805 local->scan_state = SCAN_SEND_PROBE;
3807 case SCAN_SEND_PROBE:
3808 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3809 local->scan_state = SCAN_SET_CHANNEL;
3811 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3813 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3814 local->scan_ssid_len);
3815 next_delay = IEEE80211_CHANNEL_TIME;
3819 if (local->sta_sw_scanning)
3820 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3825 static int ieee80211_sta_start_scan(struct net_device *dev,
3826 u8 *ssid, size_t ssid_len)
3828 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3829 struct ieee80211_sub_if_data *sdata;
3831 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3834 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3835 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3838 * ScanType: ACTIVE, PASSIVE
3839 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3840 * a Probe frame during active scanning
3842 * MinChannelTime (>= ProbeDelay), in TU
3843 * MaxChannelTime: (>= MinChannelTime), in TU
3846 /* MLME-SCAN.confirm
3848 * ResultCode: SUCCESS, INVALID_PARAMETERS
3851 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3852 if (local->scan_dev == dev)
3857 if (local->ops->hw_scan) {
3858 int rc = local->ops->hw_scan(local_to_hw(local),
3861 local->sta_hw_scanning = 1;
3862 local->scan_dev = dev;
3867 local->sta_sw_scanning = 1;
3870 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3872 /* Don't stop the master interface, otherwise we can't transmit
3874 if (sdata->dev == local->mdev)
3877 netif_stop_queue(sdata->dev);
3878 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3879 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
3880 ieee80211_send_nullfunc(local, sdata, 1);
3885 local->scan_ssid_len = ssid_len;
3886 memcpy(local->scan_ssid, ssid, ssid_len);
3888 local->scan_ssid_len = 0;
3889 local->scan_state = SCAN_SET_CHANNEL;
3890 local->scan_channel_idx = 0;
3891 local->scan_band = IEEE80211_BAND_2GHZ;
3892 local->scan_dev = dev;
3894 netif_tx_lock_bh(local->mdev);
3895 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3896 local->ops->configure_filter(local_to_hw(local),
3897 FIF_BCN_PRBRESP_PROMISC,
3898 &local->filter_flags,
3899 local->mdev->mc_count,
3900 local->mdev->mc_list);
3901 netif_tx_unlock_bh(local->mdev);
3903 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3904 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3905 IEEE80211_CHANNEL_TIME);
3911 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3913 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3914 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3915 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3917 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3918 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3920 if (local->sta_sw_scanning || local->sta_hw_scanning) {
3921 if (local->scan_dev == dev)
3926 ifsta->scan_ssid_len = ssid_len;
3928 memcpy(ifsta->scan_ssid, ssid, ssid_len);
3929 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3930 queue_work(local->hw.workqueue, &ifsta->work);
3935 ieee80211_sta_scan_result(struct net_device *dev,
3936 struct ieee80211_sta_bss *bss,
3937 char *current_ev, char *end_buf)
3939 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3940 struct iw_event iwe;
3942 if (time_after(jiffies,
3943 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3946 memset(&iwe, 0, sizeof(iwe));
3947 iwe.cmd = SIOCGIWAP;
3948 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3949 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3950 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3953 memset(&iwe, 0, sizeof(iwe));
3954 iwe.cmd = SIOCGIWESSID;
3955 if (bss_mesh_cfg(bss)) {
3956 iwe.u.data.length = bss_mesh_id_len(bss);
3957 iwe.u.data.flags = 1;
3958 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3961 iwe.u.data.length = bss->ssid_len;
3962 iwe.u.data.flags = 1;
3963 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3967 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3968 || bss_mesh_cfg(bss)) {
3969 memset(&iwe, 0, sizeof(iwe));
3970 iwe.cmd = SIOCGIWMODE;
3971 if (bss_mesh_cfg(bss))
3972 iwe.u.mode = IW_MODE_MESH;
3973 else if (bss->capability & WLAN_CAPABILITY_ESS)
3974 iwe.u.mode = IW_MODE_MASTER;
3976 iwe.u.mode = IW_MODE_ADHOC;
3977 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3981 memset(&iwe, 0, sizeof(iwe));
3982 iwe.cmd = SIOCGIWFREQ;
3983 iwe.u.freq.m = bss->freq;
3985 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3988 memset(&iwe, 0, sizeof(iwe));
3989 iwe.cmd = SIOCGIWFREQ;
3990 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3992 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3995 memset(&iwe, 0, sizeof(iwe));
3997 iwe.u.qual.qual = bss->signal;
3998 iwe.u.qual.level = bss->rssi;
3999 iwe.u.qual.noise = bss->noise;
4000 iwe.u.qual.updated = local->wstats_flags;
4001 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4004 memset(&iwe, 0, sizeof(iwe));
4005 iwe.cmd = SIOCGIWENCODE;
4006 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4007 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4009 iwe.u.data.flags = IW_ENCODE_DISABLED;
4010 iwe.u.data.length = 0;
4011 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4013 if (bss && bss->wpa_ie) {
4014 memset(&iwe, 0, sizeof(iwe));
4015 iwe.cmd = IWEVGENIE;
4016 iwe.u.data.length = bss->wpa_ie_len;
4017 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4021 if (bss && bss->rsn_ie) {
4022 memset(&iwe, 0, sizeof(iwe));
4023 iwe.cmd = IWEVGENIE;
4024 iwe.u.data.length = bss->rsn_ie_len;
4025 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4029 if (bss && bss->supp_rates_len > 0) {
4030 /* display all supported rates in readable format */
4031 char *p = current_ev + IW_EV_LCP_LEN;
4034 memset(&iwe, 0, sizeof(iwe));
4035 iwe.cmd = SIOCGIWRATE;
4036 /* Those two flags are ignored... */
4037 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4039 for (i = 0; i < bss->supp_rates_len; i++) {
4040 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4042 p = iwe_stream_add_value(current_ev, p,
4043 end_buf, &iwe, IW_EV_PARAM_LEN);
4050 buf = kmalloc(30, GFP_ATOMIC);
4052 memset(&iwe, 0, sizeof(iwe));
4053 iwe.cmd = IWEVCUSTOM;
4054 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4055 iwe.u.data.length = strlen(buf);
4056 current_ev = iwe_stream_add_point(current_ev, end_buf,
4062 if (bss_mesh_cfg(bss)) {
4064 u8 *cfg = bss_mesh_cfg(bss);
4065 buf = kmalloc(50, GFP_ATOMIC);
4067 memset(&iwe, 0, sizeof(iwe));
4068 iwe.cmd = IWEVCUSTOM;
4069 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4070 iwe.u.data.length = strlen(buf);
4071 current_ev = iwe_stream_add_point(current_ev, end_buf,
4073 sprintf(buf, "Path Selection Protocol ID: "
4074 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4076 iwe.u.data.length = strlen(buf);
4077 current_ev = iwe_stream_add_point(current_ev, end_buf,
4079 sprintf(buf, "Path Selection Metric ID: "
4080 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4082 iwe.u.data.length = strlen(buf);
4083 current_ev = iwe_stream_add_point(current_ev, end_buf,
4085 sprintf(buf, "Congestion Control Mode ID: "
4086 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4088 iwe.u.data.length = strlen(buf);
4089 current_ev = iwe_stream_add_point(current_ev, end_buf,
4091 sprintf(buf, "Channel Precedence: "
4092 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4094 iwe.u.data.length = strlen(buf);
4095 current_ev = iwe_stream_add_point(current_ev, end_buf,
4105 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4107 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4108 char *current_ev = buf;
4109 char *end_buf = buf + len;
4110 struct ieee80211_sta_bss *bss;
4112 spin_lock_bh(&local->sta_bss_lock);
4113 list_for_each_entry(bss, &local->sta_bss_list, list) {
4114 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4115 spin_unlock_bh(&local->sta_bss_lock);
4118 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4121 spin_unlock_bh(&local->sta_bss_lock);
4122 return current_ev - buf;
4126 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4128 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4129 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4130 kfree(ifsta->extra_ie);
4132 ifsta->extra_ie = NULL;
4133 ifsta->extra_ie_len = 0;
4136 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4137 if (!ifsta->extra_ie) {
4138 ifsta->extra_ie_len = 0;
4141 memcpy(ifsta->extra_ie, ie, len);
4142 ifsta->extra_ie_len = len;
4147 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4148 struct sk_buff *skb, u8 *bssid,
4151 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4152 struct sta_info *sta;
4153 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4154 DECLARE_MAC_BUF(mac);
4156 /* TODO: Could consider removing the least recently used entry and
4157 * allow new one to be added. */
4158 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4159 if (net_ratelimit()) {
4160 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4161 "entry %s\n", dev->name, print_mac(mac, addr));
4166 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4167 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
4169 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4173 sta->flags |= WLAN_STA_AUTHORIZED;
4175 sta->supp_rates[local->hw.conf.channel->band] =
4176 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
4178 rate_control_rate_init(sta, local);
4180 if (sta_info_insert(sta))
4187 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4189 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4190 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4192 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4195 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4196 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4199 ieee80211_send_deauth(dev, ifsta, reason);
4200 ieee80211_set_disassoc(dev, ifsta, 1);
4205 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4207 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4208 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4210 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4213 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4216 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4219 ieee80211_send_disassoc(dev, ifsta, reason);
4220 ieee80211_set_disassoc(dev, ifsta, 0);