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.
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_arp.h>
19 #include <linux/wireless.h>
20 #include <linux/random.h>
21 #include <linux/etherdevice.h>
22 #include <linux/rtnetlink.h>
23 #include <net/iw_handler.h>
24 #include <net/mac80211.h>
26 #include "ieee80211_i.h"
31 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
32 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
33 #define IEEE80211_AUTH_MAX_TRIES 3
34 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
35 #define IEEE80211_ASSOC_MAX_TRIES 3
36 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
37 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
38 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
39 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
40 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
41 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
42 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
44 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
45 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
46 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
48 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
52 static int ecw2cw(int ecw)
54 return (1 << ecw) - 1;
57 static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
64 end = pos + bss->ies_len;
66 while (pos + 1 < end) {
67 if (pos + 2 + pos[1] > end)
77 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
78 struct ieee80211_supported_band *sband,
84 for (i = 0; i < bss->supp_rates_len; i++) {
85 int rate = (bss->supp_rates[i] & 0x7F) * 5;
87 for (j = 0; j < sband->n_bitrates; j++)
88 if (sband->bitrates[j].bitrate == rate) {
98 /* frame sending functions */
99 void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
102 skb->dev = sdata->local->mdev;
103 skb_set_mac_header(skb, 0);
104 skb_set_network_header(skb, 0);
105 skb_set_transport_header(skb, 0);
107 skb->iif = sdata->dev->ifindex;
108 skb->do_not_encrypt = !encrypt;
113 static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
114 struct ieee80211_if_sta *ifsta,
115 int transaction, u8 *extra, size_t extra_len,
118 struct ieee80211_local *local = sdata->local;
120 struct ieee80211_mgmt *mgmt;
122 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
123 sizeof(*mgmt) + 6 + extra_len);
125 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
126 "frame\n", sdata->dev->name);
129 skb_reserve(skb, local->hw.extra_tx_headroom);
131 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
132 memset(mgmt, 0, 24 + 6);
133 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
134 IEEE80211_STYPE_AUTH);
136 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
137 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
138 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
139 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
140 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
141 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
142 ifsta->auth_transaction = transaction + 1;
143 mgmt->u.auth.status_code = cpu_to_le16(0);
145 memcpy(skb_put(skb, extra_len), extra, extra_len);
147 ieee80211_sta_tx(sdata, skb, encrypt);
150 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
151 u8 *ssid, size_t ssid_len)
153 struct ieee80211_local *local = sdata->local;
154 struct ieee80211_supported_band *sband;
156 struct ieee80211_mgmt *mgmt;
157 u8 *pos, *supp_rates, *esupp_rates = NULL;
160 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
162 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
163 "request\n", sdata->dev->name);
166 skb_reserve(skb, local->hw.extra_tx_headroom);
168 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
170 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
171 IEEE80211_STYPE_PROBE_REQ);
172 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
174 memcpy(mgmt->da, dst, ETH_ALEN);
175 memcpy(mgmt->bssid, dst, ETH_ALEN);
177 memset(mgmt->da, 0xff, ETH_ALEN);
178 memset(mgmt->bssid, 0xff, ETH_ALEN);
180 pos = skb_put(skb, 2 + ssid_len);
181 *pos++ = WLAN_EID_SSID;
183 memcpy(pos, ssid, ssid_len);
185 supp_rates = skb_put(skb, 2);
186 supp_rates[0] = WLAN_EID_SUPP_RATES;
188 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
190 for (i = 0; i < sband->n_bitrates; i++) {
191 struct ieee80211_rate *rate = &sband->bitrates[i];
193 pos = skb_put(skb, 1);
195 } else if (supp_rates[1] == 8) {
196 esupp_rates = skb_put(skb, 3);
197 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
199 pos = &esupp_rates[2];
201 pos = skb_put(skb, 1);
204 *pos = rate->bitrate / 5;
207 ieee80211_sta_tx(sdata, skb, 0);
210 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
211 struct ieee80211_if_sta *ifsta)
213 struct ieee80211_local *local = sdata->local;
215 struct ieee80211_mgmt *mgmt;
216 u8 *pos, *ies, *ht_add_ie;
217 int i, len, count, rates_len, supp_rates_len;
219 struct ieee80211_sta_bss *bss;
221 struct ieee80211_supported_band *sband;
224 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
225 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
228 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
229 "frame\n", sdata->dev->name);
232 skb_reserve(skb, local->hw.extra_tx_headroom);
234 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
236 capab = ifsta->capab;
238 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
239 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
240 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
241 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
242 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
245 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
246 local->hw.conf.channel->center_freq,
247 ifsta->ssid, ifsta->ssid_len);
249 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
250 capab |= WLAN_CAPABILITY_PRIVACY;
254 /* get all rates supported by the device and the AP as
255 * some APs don't like getting a superset of their rates
256 * in the association request (e.g. D-Link DAP 1353 in
258 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
260 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
261 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
262 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
264 ieee80211_rx_bss_put(local, bss);
267 rates_len = sband->n_bitrates;
270 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
272 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
273 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
274 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
276 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
278 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
279 IEEE80211_STYPE_REASSOC_REQ);
280 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
281 mgmt->u.reassoc_req.listen_interval =
282 cpu_to_le16(local->hw.conf.listen_interval);
283 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
287 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
288 IEEE80211_STYPE_ASSOC_REQ);
289 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
290 mgmt->u.reassoc_req.listen_interval =
291 cpu_to_le16(local->hw.conf.listen_interval);
295 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
296 *pos++ = WLAN_EID_SSID;
297 *pos++ = ifsta->ssid_len;
298 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
300 /* add all rates which were marked to be used above */
301 supp_rates_len = rates_len;
302 if (supp_rates_len > 8)
305 len = sband->n_bitrates;
306 pos = skb_put(skb, supp_rates_len + 2);
307 *pos++ = WLAN_EID_SUPP_RATES;
308 *pos++ = supp_rates_len;
311 for (i = 0; i < sband->n_bitrates; i++) {
312 if (BIT(i) & rates) {
313 int rate = sband->bitrates[i].bitrate;
314 *pos++ = (u8) (rate / 5);
320 if (rates_len > count) {
321 pos = skb_put(skb, rates_len - count + 2);
322 *pos++ = WLAN_EID_EXT_SUPP_RATES;
323 *pos++ = rates_len - count;
325 for (i++; i < sband->n_bitrates; i++) {
326 if (BIT(i) & rates) {
327 int rate = sband->bitrates[i].bitrate;
328 *pos++ = (u8) (rate / 5);
333 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
334 /* 1. power capabilities */
335 pos = skb_put(skb, 4);
336 *pos++ = WLAN_EID_PWR_CAPABILITY;
338 *pos++ = 0; /* min tx power */
339 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
341 /* 2. supported channels */
342 /* TODO: get this in reg domain format */
343 pos = skb_put(skb, 2 * sband->n_channels + 2);
344 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
345 *pos++ = 2 * sband->n_channels;
346 for (i = 0; i < sband->n_channels; i++) {
347 *pos++ = ieee80211_frequency_to_channel(
348 sband->channels[i].center_freq);
349 *pos++ = 1; /* one channel in the subband*/
353 if (ifsta->extra_ie) {
354 pos = skb_put(skb, ifsta->extra_ie_len);
355 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
358 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
359 pos = skb_put(skb, 9);
360 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
361 *pos++ = 7; /* len */
362 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
365 *pos++ = 2; /* WME */
366 *pos++ = 0; /* WME info */
367 *pos++ = 1; /* WME ver */
371 /* wmm support is a must to HT */
372 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
373 sband->ht_info.ht_supported &&
374 (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
375 struct ieee80211_ht_addt_info *ht_add_info =
376 (struct ieee80211_ht_addt_info *)ht_add_ie;
377 u16 cap = sband->ht_info.cap;
379 u32 flags = local->hw.conf.channel->flags;
381 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
382 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
383 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
384 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
385 cap &= ~IEEE80211_HT_CAP_SGI_40;
388 case IEEE80211_HT_IE_CHA_SEC_BELOW:
389 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
390 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
391 cap &= ~IEEE80211_HT_CAP_SGI_40;
396 tmp = cpu_to_le16(cap);
397 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
398 *pos++ = WLAN_EID_HT_CAPABILITY;
399 *pos++ = sizeof(struct ieee80211_ht_cap);
400 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
401 memcpy(pos, &tmp, sizeof(u16));
403 /* TODO: needs a define here for << 2 */
404 *pos++ = sband->ht_info.ampdu_factor |
405 (sband->ht_info.ampdu_density << 2);
406 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
409 kfree(ifsta->assocreq_ies);
410 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
411 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
412 if (ifsta->assocreq_ies)
413 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
415 ieee80211_sta_tx(sdata, skb, 0);
419 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
420 u16 stype, u16 reason)
422 struct ieee80211_local *local = sdata->local;
423 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
425 struct ieee80211_mgmt *mgmt;
427 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
429 printk(KERN_DEBUG "%s: failed to allocate buffer for "
430 "deauth/disassoc frame\n", sdata->dev->name);
433 skb_reserve(skb, local->hw.extra_tx_headroom);
435 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
437 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
438 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
439 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
440 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
442 /* u.deauth.reason_code == u.disassoc.reason_code */
443 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
445 ieee80211_sta_tx(sdata, skb, 0);
449 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
450 struct ieee80211_sta_bss *bss)
452 struct ieee80211_local *local = sdata->local;
453 int i, have_higher_than_11mbit = 0;
455 /* cf. IEEE 802.11 9.2.12 */
456 for (i = 0; i < bss->supp_rates_len; i++)
457 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
458 have_higher_than_11mbit = 1;
460 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
461 have_higher_than_11mbit)
462 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
464 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
466 ieee80211_set_wmm_default(sdata);
469 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
470 struct ieee80211_if_sta *ifsta,
471 u8 *wmm_param, size_t wmm_param_len)
473 struct ieee80211_tx_queue_params params;
478 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
484 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
486 count = wmm_param[6] & 0x0f;
487 if (count == ifsta->wmm_last_param_set)
489 ifsta->wmm_last_param_set = count;
492 left = wmm_param_len - 8;
494 memset(¶ms, 0, sizeof(params));
496 if (!local->ops->conf_tx)
500 for (; left >= 4; left -= 4, pos += 4) {
501 int aci = (pos[0] >> 5) & 0x03;
502 int acm = (pos[0] >> 4) & 0x01;
509 local->wmm_acm |= BIT(0) | BIT(3);
514 local->wmm_acm |= BIT(4) | BIT(5);
519 local->wmm_acm |= BIT(6) | BIT(7);
525 local->wmm_acm |= BIT(1) | BIT(2);
529 params.aifs = pos[0] & 0x0f;
530 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
531 params.cw_min = ecw2cw(pos[1] & 0x0f);
532 params.txop = get_unaligned_le16(pos + 2);
533 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
534 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
535 "cWmin=%d cWmax=%d txop=%d\n",
536 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
537 params.cw_max, params.txop);
539 /* TODO: handle ACM (block TX, fallback to next lowest allowed
541 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
542 printk(KERN_DEBUG "%s: failed to set TX queue "
543 "parameters for queue %d\n", local->mdev->name, queue);
548 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
550 bool use_short_preamble)
552 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
553 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
554 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
555 DECLARE_MAC_BUF(mac);
559 if (use_protection != bss_conf->use_cts_prot) {
560 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
561 if (net_ratelimit()) {
562 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
565 use_protection ? "enabled" : "disabled",
566 print_mac(mac, ifsta->bssid));
569 bss_conf->use_cts_prot = use_protection;
570 changed |= BSS_CHANGED_ERP_CTS_PROT;
573 if (use_short_preamble != bss_conf->use_short_preamble) {
574 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
575 if (net_ratelimit()) {
576 printk(KERN_DEBUG "%s: switched to %s barker preamble"
579 use_short_preamble ? "short" : "long",
580 print_mac(mac, ifsta->bssid));
583 bss_conf->use_short_preamble = use_short_preamble;
584 changed |= BSS_CHANGED_ERP_PREAMBLE;
590 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
593 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
594 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
596 return ieee80211_handle_protect_preamb(sdata,
597 use_protection, use_short_preamble);
600 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
601 struct ieee80211_sta_bss *bss)
605 if (bss->has_erp_value)
606 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
608 u16 capab = bss->capability;
609 changed |= ieee80211_handle_protect_preamb(sdata, false,
610 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
616 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
617 struct ieee80211_if_sta *ifsta)
619 union iwreq_data wrqu;
620 memset(&wrqu, 0, sizeof(wrqu));
621 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
622 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
623 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
624 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
627 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
628 struct ieee80211_if_sta *ifsta)
630 union iwreq_data wrqu;
632 if (ifsta->assocreq_ies) {
633 memset(&wrqu, 0, sizeof(wrqu));
634 wrqu.data.length = ifsta->assocreq_ies_len;
635 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
636 ifsta->assocreq_ies);
638 if (ifsta->assocresp_ies) {
639 memset(&wrqu, 0, sizeof(wrqu));
640 wrqu.data.length = ifsta->assocresp_ies_len;
641 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
642 ifsta->assocresp_ies);
647 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
648 struct ieee80211_if_sta *ifsta)
650 struct ieee80211_local *local = sdata->local;
651 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
652 u32 changed = BSS_CHANGED_ASSOC;
654 struct ieee80211_sta_bss *bss;
656 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
658 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
661 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
662 conf->channel->center_freq,
663 ifsta->ssid, ifsta->ssid_len);
665 /* set timing information */
666 sdata->bss_conf.beacon_int = bss->beacon_int;
667 sdata->bss_conf.timestamp = bss->timestamp;
668 sdata->bss_conf.dtim_period = bss->dtim_period;
670 changed |= ieee80211_handle_bss_capability(sdata, bss);
672 ieee80211_rx_bss_put(local, bss);
675 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
676 changed |= BSS_CHANGED_HT;
677 sdata->bss_conf.assoc_ht = 1;
678 sdata->bss_conf.ht_conf = &conf->ht_conf;
679 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
682 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
683 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
684 ieee80211_sta_send_associnfo(sdata, ifsta);
686 ifsta->last_probe = jiffies;
687 ieee80211_led_assoc(local, 1);
689 sdata->bss_conf.assoc = 1;
690 ieee80211_bss_info_change_notify(sdata, changed);
692 netif_tx_start_all_queues(sdata->dev);
693 netif_carrier_on(sdata->dev);
695 ieee80211_sta_send_apinfo(sdata, ifsta);
698 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
699 struct ieee80211_if_sta *ifsta)
701 DECLARE_MAC_BUF(mac);
703 ifsta->direct_probe_tries++;
704 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
705 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
706 sdata->dev->name, print_mac(mac, ifsta->bssid));
707 ifsta->state = IEEE80211_STA_MLME_DISABLED;
711 printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
712 sdata->dev->name, print_mac(mac, ifsta->bssid),
713 ifsta->direct_probe_tries);
715 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
717 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
719 /* Direct probe is sent to broadcast address as some APs
720 * will not answer to direct packet in unassociated state.
722 ieee80211_send_probe_req(sdata, NULL,
723 ifsta->ssid, ifsta->ssid_len);
725 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
729 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
730 struct ieee80211_if_sta *ifsta)
732 DECLARE_MAC_BUF(mac);
735 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
736 printk(KERN_DEBUG "%s: authentication with AP %s"
738 sdata->dev->name, print_mac(mac, ifsta->bssid));
739 ifsta->state = IEEE80211_STA_MLME_DISABLED;
743 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
744 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
745 sdata->dev->name, print_mac(mac, ifsta->bssid));
747 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
749 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
752 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
753 struct ieee80211_if_sta *ifsta, bool deauth,
754 bool self_disconnected, u16 reason)
756 struct ieee80211_local *local = sdata->local;
757 struct sta_info *sta;
758 u32 changed = BSS_CHANGED_ASSOC;
762 sta = sta_info_get(local, ifsta->bssid);
769 ifsta->direct_probe_tries = 0;
770 ifsta->auth_tries = 0;
772 ifsta->assoc_scan_tries = 0;
773 ifsta->assoc_tries = 0;
775 netif_tx_stop_all_queues(sdata->dev);
776 netif_carrier_off(sdata->dev);
778 ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
780 if (self_disconnected) {
782 ieee80211_send_deauth_disassoc(sdata,
783 IEEE80211_STYPE_DEAUTH, reason);
785 ieee80211_send_deauth_disassoc(sdata,
786 IEEE80211_STYPE_DISASSOC, reason);
789 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
790 changed |= ieee80211_reset_erp_info(sdata);
792 if (sdata->bss_conf.assoc_ht)
793 changed |= BSS_CHANGED_HT;
795 sdata->bss_conf.assoc_ht = 0;
796 sdata->bss_conf.ht_conf = NULL;
797 sdata->bss_conf.ht_bss_conf = NULL;
799 ieee80211_led_assoc(local, 0);
800 sdata->bss_conf.assoc = 0;
802 ieee80211_sta_send_apinfo(sdata, ifsta);
804 if (self_disconnected)
805 ifsta->state = IEEE80211_STA_MLME_DISABLED;
807 sta_info_unlink(&sta);
811 sta_info_destroy(sta);
814 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
816 if (!sdata || !sdata->default_key ||
817 sdata->default_key->conf.alg != ALG_WEP)
822 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
823 struct ieee80211_if_sta *ifsta)
825 struct ieee80211_local *local = sdata->local;
826 struct ieee80211_sta_bss *bss;
831 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
834 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
835 local->hw.conf.channel->center_freq,
836 ifsta->ssid, ifsta->ssid_len);
840 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
841 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
842 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
844 ieee80211_rx_bss_put(local, bss);
846 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
852 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
853 struct ieee80211_if_sta *ifsta)
855 DECLARE_MAC_BUF(mac);
857 ifsta->assoc_tries++;
858 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
859 printk(KERN_DEBUG "%s: association with AP %s"
861 sdata->dev->name, print_mac(mac, ifsta->bssid));
862 ifsta->state = IEEE80211_STA_MLME_DISABLED;
866 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
867 printk(KERN_DEBUG "%s: associate with AP %s\n",
868 sdata->dev->name, print_mac(mac, ifsta->bssid));
869 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
870 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
871 "mixed-cell disabled - abort association\n", sdata->dev->name);
872 ifsta->state = IEEE80211_STA_MLME_DISABLED;
876 ieee80211_send_assoc(sdata, ifsta);
878 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
882 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
883 struct ieee80211_if_sta *ifsta)
885 struct ieee80211_local *local = sdata->local;
886 struct sta_info *sta;
888 DECLARE_MAC_BUF(mac);
890 /* TODO: start monitoring current AP signal quality and number of
891 * missed beacons. Scan other channels every now and then and search
893 /* TODO: remove expired BSSes */
895 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
899 sta = sta_info_get(local, ifsta->bssid);
901 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
902 sdata->dev->name, print_mac(mac, ifsta->bssid));
906 if (time_after(jiffies,
907 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
908 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
909 printk(KERN_DEBUG "%s: No ProbeResp from "
910 "current AP %s - assume out of "
912 sdata->dev->name, print_mac(mac, ifsta->bssid));
915 ieee80211_send_probe_req(sdata, ifsta->bssid,
917 local->scan_ssid_len);
918 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
920 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
921 if (time_after(jiffies, ifsta->last_probe +
922 IEEE80211_PROBE_INTERVAL)) {
923 ifsta->last_probe = jiffies;
924 ieee80211_send_probe_req(sdata, ifsta->bssid,
934 ieee80211_set_disassoc(sdata, ifsta, true, true,
935 WLAN_REASON_PREV_AUTH_NOT_VALID);
937 mod_timer(&ifsta->timer, jiffies +
938 IEEE80211_MONITORING_INTERVAL);
942 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
943 struct ieee80211_if_sta *ifsta)
945 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
946 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
947 ieee80211_associate(sdata, ifsta);
951 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
952 struct ieee80211_if_sta *ifsta,
953 struct ieee80211_mgmt *mgmt,
957 struct ieee802_11_elems elems;
959 pos = mgmt->u.auth.variable;
960 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
961 if (!elems.challenge)
963 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
964 elems.challenge_len + 2, 1);
967 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
968 struct ieee80211_if_sta *ifsta,
969 struct ieee80211_mgmt *mgmt,
972 u16 auth_alg, auth_transaction, status_code;
973 DECLARE_MAC_BUF(mac);
975 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
976 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
982 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
983 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
986 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
987 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
990 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
991 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
992 status_code = le16_to_cpu(mgmt->u.auth.status_code);
994 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
996 * IEEE 802.11 standard does not require authentication in IBSS
997 * networks and most implementations do not seem to use it.
998 * However, try to reply to authentication attempts if someone
999 * has actually implemented this.
1001 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1003 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
1006 if (auth_alg != ifsta->auth_alg ||
1007 auth_transaction != ifsta->auth_transaction)
1010 if (status_code != WLAN_STATUS_SUCCESS) {
1011 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1013 const int num_algs = ARRAY_SIZE(algs);
1015 algs[0] = algs[1] = algs[2] = 0xff;
1016 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1017 algs[0] = WLAN_AUTH_OPEN;
1018 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1019 algs[1] = WLAN_AUTH_SHARED_KEY;
1020 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1021 algs[2] = WLAN_AUTH_LEAP;
1022 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1024 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1028 for (i = 0; i < num_algs; i++) {
1030 if (pos >= num_algs)
1032 if (algs[pos] == ifsta->auth_alg ||
1035 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1036 !ieee80211_sta_wep_configured(sdata))
1038 ifsta->auth_alg = algs[pos];
1045 switch (ifsta->auth_alg) {
1046 case WLAN_AUTH_OPEN:
1047 case WLAN_AUTH_LEAP:
1048 ieee80211_auth_completed(sdata, ifsta);
1050 case WLAN_AUTH_SHARED_KEY:
1051 if (ifsta->auth_transaction == 4)
1052 ieee80211_auth_completed(sdata, ifsta);
1054 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
1060 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1061 struct ieee80211_if_sta *ifsta,
1062 struct ieee80211_mgmt *mgmt,
1066 DECLARE_MAC_BUF(mac);
1071 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1074 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1076 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1077 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
1079 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1080 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1081 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1082 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1083 mod_timer(&ifsta->timer, jiffies +
1084 IEEE80211_RETRY_AUTH_INTERVAL);
1087 ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
1088 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1092 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1093 struct ieee80211_if_sta *ifsta,
1094 struct ieee80211_mgmt *mgmt,
1098 DECLARE_MAC_BUF(mac);
1103 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1106 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1108 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1109 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
1111 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1112 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1113 mod_timer(&ifsta->timer, jiffies +
1114 IEEE80211_RETRY_AUTH_INTERVAL);
1117 ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
1121 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1122 struct ieee80211_if_sta *ifsta,
1123 struct ieee80211_mgmt *mgmt,
1127 struct ieee80211_local *local = sdata->local;
1128 struct ieee80211_supported_band *sband;
1129 struct sta_info *sta;
1130 u64 rates, basic_rates;
1131 u16 capab_info, status_code, aid;
1132 struct ieee802_11_elems elems;
1133 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1136 DECLARE_MAC_BUF(mac);
1137 bool have_higher_than_11mbit = false;
1139 /* AssocResp and ReassocResp have identical structure, so process both
1140 * of them in this function. */
1142 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
1148 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1151 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1152 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1153 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1155 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1156 "status=%d aid=%d)\n",
1157 sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1158 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1160 if (status_code != WLAN_STATUS_SUCCESS) {
1161 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1162 sdata->dev->name, status_code);
1163 /* if this was a reassociation, ensure we try a "full"
1164 * association next time. This works around some broken APs
1165 * which do not correctly reject reassociation requests. */
1166 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1170 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1171 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1172 "set\n", sdata->dev->name, aid);
1173 aid &= ~(BIT(15) | BIT(14));
1175 pos = mgmt->u.assoc_resp.variable;
1176 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1178 if (!elems.supp_rates) {
1179 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1184 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1186 ifsta->ap_capab = capab_info;
1188 kfree(ifsta->assocresp_ies);
1189 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1190 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1191 if (ifsta->assocresp_ies)
1192 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1196 /* Add STA entry for the AP */
1197 sta = sta_info_get(local, ifsta->bssid);
1199 struct ieee80211_sta_bss *bss;
1202 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1204 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1205 " the AP\n", sdata->dev->name);
1209 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1210 local->hw.conf.channel->center_freq,
1211 ifsta->ssid, ifsta->ssid_len);
1213 sta->last_signal = bss->signal;
1214 sta->last_qual = bss->qual;
1215 sta->last_noise = bss->noise;
1216 ieee80211_rx_bss_put(local, bss);
1219 err = sta_info_insert(sta);
1221 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1222 " the AP (error %d)\n", sdata->dev->name, err);
1226 /* update new sta with its last rx activity */
1227 sta->last_rx = jiffies;
1231 * FIXME: Do we really need to update the sta_info's information here?
1232 * We already know about the AP (we found it in our list) so it
1233 * should already be filled with the right info, no?
1234 * As is stands, all this is racy because typically we assume
1235 * the information that is filled in here (except flags) doesn't
1236 * change while a STA structure is alive. As such, it should move
1237 * to between the sta_info_alloc() and sta_info_insert() above.
1240 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1241 WLAN_STA_AUTHORIZED);
1245 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1247 for (i = 0; i < elems.supp_rates_len; i++) {
1248 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1251 have_higher_than_11mbit = true;
1253 for (j = 0; j < sband->n_bitrates; j++) {
1254 if (sband->bitrates[j].bitrate == rate)
1256 if (elems.supp_rates[i] & 0x80)
1257 basic_rates |= BIT(j);
1261 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1262 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1265 have_higher_than_11mbit = true;
1267 for (j = 0; j < sband->n_bitrates; j++) {
1268 if (sband->bitrates[j].bitrate == rate)
1270 if (elems.ext_supp_rates[i] & 0x80)
1271 basic_rates |= BIT(j);
1275 sta->supp_rates[local->hw.conf.channel->band] = rates;
1276 sdata->basic_rates = basic_rates;
1278 /* cf. IEEE 802.11 9.2.12 */
1279 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1280 have_higher_than_11mbit)
1281 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1283 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1285 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1286 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1287 struct ieee80211_ht_bss_info bss_info;
1288 ieee80211_ht_cap_ie_to_ht_info(
1289 (struct ieee80211_ht_cap *)
1290 elems.ht_cap_elem, &sta->ht_info);
1291 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1292 (struct ieee80211_ht_addt_info *)
1293 elems.ht_info_elem, &bss_info);
1294 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
1297 rate_control_rate_init(sta, local);
1299 if (elems.wmm_param) {
1300 set_sta_flags(sta, WLAN_STA_WME);
1302 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1303 elems.wmm_param_len);
1307 /* set AID and assoc capability,
1308 * ieee80211_set_associated() will tell the driver */
1309 bss_conf->aid = aid;
1310 bss_conf->assoc_capability = capab_info;
1311 ieee80211_set_associated(sdata, ifsta);
1313 ieee80211_associated(sdata, ifsta);
1317 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
1318 struct ieee80211_if_sta *ifsta,
1319 struct ieee80211_sta_bss *bss)
1321 struct ieee80211_local *local = sdata->local;
1322 int res, rates, i, j;
1323 struct sk_buff *skb;
1324 struct ieee80211_mgmt *mgmt;
1326 struct ieee80211_supported_band *sband;
1327 union iwreq_data wrqu;
1329 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1331 /* Remove possible STA entries from other IBSS networks. */
1332 sta_info_flush_delayed(sdata);
1334 if (local->ops->reset_tsf) {
1335 /* Reset own TSF to allow time synchronization work. */
1336 local->ops->reset_tsf(local_to_hw(local));
1338 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
1339 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
1343 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
1345 sdata->drop_unencrypted = bss->capability &
1346 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
1348 res = ieee80211_set_freq(sdata, bss->freq);
1353 /* Build IBSS probe response */
1354 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
1356 skb_reserve(skb, local->hw.extra_tx_headroom);
1358 mgmt = (struct ieee80211_mgmt *)
1359 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1360 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1361 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1362 IEEE80211_STYPE_PROBE_RESP);
1363 memset(mgmt->da, 0xff, ETH_ALEN);
1364 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1365 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1366 mgmt->u.beacon.beacon_int =
1367 cpu_to_le16(local->hw.conf.beacon_int);
1368 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
1369 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
1371 pos = skb_put(skb, 2 + ifsta->ssid_len);
1372 *pos++ = WLAN_EID_SSID;
1373 *pos++ = ifsta->ssid_len;
1374 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
1376 rates = bss->supp_rates_len;
1379 pos = skb_put(skb, 2 + rates);
1380 *pos++ = WLAN_EID_SUPP_RATES;
1382 memcpy(pos, bss->supp_rates, rates);
1384 if (bss->band == IEEE80211_BAND_2GHZ) {
1385 pos = skb_put(skb, 2 + 1);
1386 *pos++ = WLAN_EID_DS_PARAMS;
1388 *pos++ = ieee80211_frequency_to_channel(bss->freq);
1391 pos = skb_put(skb, 2 + 2);
1392 *pos++ = WLAN_EID_IBSS_PARAMS;
1394 /* FIX: set ATIM window based on scan results */
1398 if (bss->supp_rates_len > 8) {
1399 rates = bss->supp_rates_len - 8;
1400 pos = skb_put(skb, 2 + rates);
1401 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1403 memcpy(pos, &bss->supp_rates[8], rates);
1406 ifsta->probe_resp = skb;
1408 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1412 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1413 for (i = 0; i < bss->supp_rates_len; i++) {
1414 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
1415 for (j = 0; j < sband->n_bitrates; j++)
1416 if (sband->bitrates[j].bitrate == bitrate)
1419 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
1421 ieee80211_sta_def_wmm_params(sdata, bss);
1423 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
1424 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1426 memset(&wrqu, 0, sizeof(wrqu));
1427 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
1428 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
1433 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
1434 struct ieee802_11_elems *elems,
1435 enum ieee80211_band band)
1437 struct ieee80211_supported_band *sband;
1438 struct ieee80211_rate *bitrates;
1442 sband = local->hw.wiphy->bands[band];
1446 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1449 bitrates = sband->bitrates;
1450 num_rates = sband->n_bitrates;
1452 for (i = 0; i < elems->supp_rates_len +
1453 elems->ext_supp_rates_len; i++) {
1456 if (i < elems->supp_rates_len)
1457 rate = elems->supp_rates[i];
1458 else if (elems->ext_supp_rates)
1459 rate = elems->ext_supp_rates
1460 [i - elems->supp_rates_len];
1461 own_rate = 5 * (rate & 0x7f);
1462 for (j = 0; j < num_rates; j++)
1463 if (bitrates[j].bitrate == own_rate)
1464 supp_rates |= BIT(j);
1469 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
1470 enum ieee80211_band band)
1472 struct ieee80211_supported_band *sband;
1473 struct ieee80211_rate *bitrates;
1474 u64 mandatory_rates;
1475 enum ieee80211_rate_flags mandatory_flag;
1478 sband = local->hw.wiphy->bands[band];
1481 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1484 if (band == IEEE80211_BAND_2GHZ)
1485 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
1487 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
1489 bitrates = sband->bitrates;
1490 mandatory_rates = 0;
1491 for (i = 0; i < sband->n_bitrates; i++)
1492 if (bitrates[i].flags & mandatory_flag)
1493 mandatory_rates |= BIT(i);
1494 return mandatory_rates;
1497 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1498 struct ieee80211_mgmt *mgmt,
1500 struct ieee80211_rx_status *rx_status,
1501 struct ieee802_11_elems *elems,
1504 struct ieee80211_local *local = sdata->local;
1506 struct ieee80211_sta_bss *bss;
1507 struct sta_info *sta;
1508 struct ieee80211_channel *channel;
1509 u64 beacon_timestamp, rx_timestamp;
1511 enum ieee80211_band band = rx_status->band;
1512 DECLARE_MAC_BUF(mac);
1513 DECLARE_MAC_BUF(mac2);
1515 if (elems->ds_params && elems->ds_params_len == 1)
1516 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1518 freq = rx_status->freq;
1520 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1522 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1525 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
1526 elems->mesh_config && mesh_matches_local(elems, sdata)) {
1527 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1529 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
1530 mesh_peer_accepts_plinks(elems));
1533 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
1534 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1535 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1539 sta = sta_info_get(local, mgmt->sa);
1543 prev_rates = sta->supp_rates[band];
1544 /* make sure mandatory rates are always added */
1545 sta->supp_rates[band] = supp_rates |
1546 ieee80211_sta_get_mandatory_rates(local, band);
1548 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1549 if (sta->supp_rates[band] != prev_rates)
1550 printk(KERN_DEBUG "%s: updated supp_rates set "
1551 "for %s based on beacon info (0x%llx | "
1552 "0x%llx -> 0x%llx)\n",
1553 sdata->dev->name, print_mac(mac, sta->addr),
1554 (unsigned long long) prev_rates,
1555 (unsigned long long) supp_rates,
1556 (unsigned long long) sta->supp_rates[band]);
1559 ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
1560 mgmt->sa, supp_rates);
1566 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1571 /* was just updated in ieee80211_bss_info_update */
1572 beacon_timestamp = bss->timestamp;
1575 * In STA mode, the remaining parameters should not be overridden
1576 * by beacons because they're not necessarily accurate there.
1578 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1579 bss->last_probe_resp && beacon) {
1580 ieee80211_rx_bss_put(local, bss);
1584 /* check if we need to merge IBSS */
1585 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
1586 bss->capability & WLAN_CAPABILITY_IBSS &&
1587 bss->freq == local->oper_channel->center_freq &&
1588 elems->ssid_len == sdata->u.sta.ssid_len &&
1589 memcmp(elems->ssid, sdata->u.sta.ssid,
1590 sdata->u.sta.ssid_len) == 0) {
1591 if (rx_status->flag & RX_FLAG_TSFT) {
1592 /* in order for correct IBSS merging we need mactime
1594 * since mactime is defined as the time the first data
1595 * symbol of the frame hits the PHY, and the timestamp
1596 * of the beacon is defined as "the time that the data
1597 * symbol containing the first bit of the timestamp is
1598 * transmitted to the PHY plus the transmitting STA’s
1599 * delays through its local PHY from the MAC-PHY
1600 * interface to its interface with the WM"
1601 * (802.11 11.1.2) - equals the time this bit arrives at
1602 * the receiver - we have to take into account the
1603 * offset between the two.
1604 * e.g: at 1 MBit that means mactime is 192 usec earlier
1605 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
1607 int rate = local->hw.wiphy->bands[band]->
1608 bitrates[rx_status->rate_idx].bitrate;
1609 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
1610 } else if (local && local->ops && local->ops->get_tsf)
1611 /* second best option: get current TSF */
1612 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
1614 /* can't merge without knowing the TSF */
1615 rx_timestamp = -1LLU;
1616 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1617 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1618 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
1619 print_mac(mac, mgmt->sa),
1620 print_mac(mac2, mgmt->bssid),
1621 (unsigned long long)rx_timestamp,
1622 (unsigned long long)beacon_timestamp,
1623 (unsigned long long)(rx_timestamp - beacon_timestamp),
1625 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1626 if (beacon_timestamp > rx_timestamp) {
1627 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1628 printk(KERN_DEBUG "%s: beacon TSF higher than "
1629 "local TSF - IBSS merge with BSSID %s\n",
1630 sdata->dev->name, print_mac(mac, mgmt->bssid));
1632 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
1633 ieee80211_ibss_add_sta(sdata, NULL,
1634 mgmt->bssid, mgmt->sa,
1639 ieee80211_rx_bss_put(local, bss);
1643 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1644 struct ieee80211_mgmt *mgmt,
1646 struct ieee80211_rx_status *rx_status)
1649 struct ieee802_11_elems elems;
1650 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1652 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1653 return; /* ignore ProbeResp to foreign address */
1655 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1659 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1662 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1664 /* direct probe may be part of the association flow */
1665 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
1667 printk(KERN_DEBUG "%s direct probe responded\n",
1669 ieee80211_authenticate(sdata, ifsta);
1674 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1675 struct ieee80211_mgmt *mgmt,
1677 struct ieee80211_rx_status *rx_status)
1679 struct ieee80211_if_sta *ifsta;
1681 struct ieee802_11_elems elems;
1682 struct ieee80211_local *local = sdata->local;
1683 struct ieee80211_conf *conf = &local->hw.conf;
1686 /* Process beacon from the current BSS */
1687 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1691 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1693 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
1695 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
1697 ifsta = &sdata->u.sta;
1699 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1700 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1703 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1704 elems.wmm_param_len);
1706 if (elems.erp_info && elems.erp_info_len >= 1)
1707 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
1709 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
1710 changed |= ieee80211_handle_protect_preamb(sdata, false,
1711 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
1714 if (elems.ht_cap_elem && elems.ht_info_elem &&
1715 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
1716 struct ieee80211_ht_bss_info bss_info;
1718 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1719 (struct ieee80211_ht_addt_info *)
1720 elems.ht_info_elem, &bss_info);
1721 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
1725 ieee80211_bss_info_change_notify(sdata, changed);
1729 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
1730 struct ieee80211_if_sta *ifsta,
1731 struct ieee80211_mgmt *mgmt,
1733 struct ieee80211_rx_status *rx_status)
1735 struct ieee80211_local *local = sdata->local;
1737 struct sk_buff *skb;
1738 struct ieee80211_mgmt *resp;
1740 DECLARE_MAC_BUF(mac);
1741 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1742 DECLARE_MAC_BUF(mac2);
1743 DECLARE_MAC_BUF(mac3);
1746 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
1747 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
1748 len < 24 + 2 || !ifsta->probe_resp)
1751 if (local->ops->tx_last_beacon)
1752 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1756 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1757 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1758 "%s (tx_last_beacon=%d)\n",
1759 sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1760 print_mac(mac3, mgmt->bssid), tx_last_beacon);
1761 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1763 if (!tx_last_beacon)
1766 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1767 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1770 end = ((u8 *) mgmt) + len;
1771 pos = mgmt->u.probe_req.variable;
1772 if (pos[0] != WLAN_EID_SSID ||
1773 pos + 2 + pos[1] > end) {
1774 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1775 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1777 sdata->dev->name, print_mac(mac, mgmt->sa));
1782 (pos[1] != ifsta->ssid_len ||
1783 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1784 /* Ignore ProbeReq for foreign SSID */
1788 /* Reply with ProbeResp */
1789 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1793 resp = (struct ieee80211_mgmt *) skb->data;
1794 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1795 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1796 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1797 sdata->dev->name, print_mac(mac, resp->da));
1798 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1799 ieee80211_sta_tx(sdata, skb, 0);
1802 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1803 struct ieee80211_if_sta *ifsta,
1804 struct ieee80211_mgmt *mgmt,
1806 struct ieee80211_rx_status *rx_status)
1808 /* currently we only handle mesh interface action frames here */
1809 if (!ieee80211_vif_is_mesh(&sdata->vif))
1812 switch (mgmt->u.action.category) {
1813 case PLINK_CATEGORY:
1814 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1816 case MESH_PATH_SEL_CATEGORY:
1817 mesh_rx_path_sel_frame(sdata, mgmt, len);
1822 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
1823 struct ieee80211_rx_status *rx_status)
1825 struct ieee80211_local *local = sdata->local;
1826 struct ieee80211_if_sta *ifsta;
1827 struct ieee80211_mgmt *mgmt;
1833 ifsta = &sdata->u.sta;
1835 mgmt = (struct ieee80211_mgmt *) skb->data;
1836 fc = le16_to_cpu(mgmt->frame_control);
1838 switch (fc & IEEE80211_FCTL_STYPE) {
1839 case IEEE80211_STYPE_PROBE_REQ:
1840 case IEEE80211_STYPE_PROBE_RESP:
1841 case IEEE80211_STYPE_BEACON:
1842 case IEEE80211_STYPE_ACTION:
1843 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1844 case IEEE80211_STYPE_AUTH:
1845 case IEEE80211_STYPE_ASSOC_RESP:
1846 case IEEE80211_STYPE_REASSOC_RESP:
1847 case IEEE80211_STYPE_DEAUTH:
1848 case IEEE80211_STYPE_DISASSOC:
1849 skb_queue_tail(&ifsta->skb_queue, skb);
1850 queue_work(local->hw.workqueue, &ifsta->work);
1858 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1859 struct sk_buff *skb)
1861 struct ieee80211_rx_status *rx_status;
1862 struct ieee80211_if_sta *ifsta;
1863 struct ieee80211_mgmt *mgmt;
1866 ifsta = &sdata->u.sta;
1868 rx_status = (struct ieee80211_rx_status *) skb->cb;
1869 mgmt = (struct ieee80211_mgmt *) skb->data;
1870 fc = le16_to_cpu(mgmt->frame_control);
1872 switch (fc & IEEE80211_FCTL_STYPE) {
1873 case IEEE80211_STYPE_PROBE_REQ:
1874 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
1877 case IEEE80211_STYPE_PROBE_RESP:
1878 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
1880 case IEEE80211_STYPE_BEACON:
1881 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
1883 case IEEE80211_STYPE_AUTH:
1884 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
1886 case IEEE80211_STYPE_ASSOC_RESP:
1887 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
1889 case IEEE80211_STYPE_REASSOC_RESP:
1890 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
1892 case IEEE80211_STYPE_DEAUTH:
1893 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
1895 case IEEE80211_STYPE_DISASSOC:
1896 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
1898 case IEEE80211_STYPE_ACTION:
1899 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
1907 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
1909 struct ieee80211_local *local = sdata->local;
1911 struct sta_info *sta;
1915 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1916 if (sta->sdata == sdata &&
1917 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1930 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
1932 struct ieee80211_local *local = sdata->local;
1933 struct sta_info *sta, *tmp;
1934 LIST_HEAD(tmp_list);
1935 DECLARE_MAC_BUF(mac);
1936 unsigned long flags;
1938 spin_lock_irqsave(&local->sta_lock, flags);
1939 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1940 if (time_after(jiffies, sta->last_rx + exp_time)) {
1941 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1942 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
1943 sdata->dev->name, print_mac(mac, sta->addr));
1945 __sta_info_unlink(&sta);
1947 list_add(&sta->list, &tmp_list);
1949 spin_unlock_irqrestore(&local->sta_lock, flags);
1951 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
1952 sta_info_destroy(sta);
1956 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
1957 struct ieee80211_if_sta *ifsta)
1959 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1961 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
1962 if (ieee80211_sta_active_ibss(sdata))
1965 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1966 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
1967 ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
1971 #ifdef CONFIG_MAC80211_MESH
1972 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
1973 struct ieee80211_if_sta *ifsta)
1977 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
1978 mesh_path_expire(sdata);
1980 free_plinks = mesh_plink_availables(sdata);
1981 if (free_plinks != sdata->u.sta.accepting_plinks)
1982 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1984 mod_timer(&ifsta->timer, jiffies +
1985 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
1989 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
1991 struct ieee80211_if_sta *ifsta;
1992 ifsta = &sdata->u.sta;
1993 ifsta->state = IEEE80211_STA_MLME_MESH_UP;
1994 ieee80211_sta_timer((unsigned long)sdata);
1995 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2000 void ieee80211_sta_timer(unsigned long data)
2002 struct ieee80211_sub_if_data *sdata =
2003 (struct ieee80211_sub_if_data *) data;
2004 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2005 struct ieee80211_local *local = sdata->local;
2007 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2008 queue_work(local->hw.workqueue, &ifsta->work);
2011 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
2012 struct ieee80211_if_sta *ifsta)
2014 struct ieee80211_local *local = sdata->local;
2016 if (local->ops->reset_tsf) {
2017 /* Reset own TSF to allow time synchronization work. */
2018 local->ops->reset_tsf(local_to_hw(local));
2021 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2024 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2025 ifsta->auth_alg = WLAN_AUTH_OPEN;
2026 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2027 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2028 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2029 ifsta->auth_alg = WLAN_AUTH_LEAP;
2031 ifsta->auth_alg = WLAN_AUTH_OPEN;
2032 ifsta->auth_transaction = -1;
2033 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2034 ifsta->assoc_scan_tries = 0;
2035 ifsta->direct_probe_tries = 0;
2036 ifsta->auth_tries = 0;
2037 ifsta->assoc_tries = 0;
2038 netif_tx_stop_all_queues(sdata->dev);
2039 netif_carrier_off(sdata->dev);
2043 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
2044 struct ieee80211_if_sta *ifsta)
2046 struct ieee80211_local *local = sdata->local;
2048 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2051 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2052 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2053 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2054 IEEE80211_STA_AUTO_SSID_SEL))) {
2056 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
2057 ieee80211_set_disassoc(sdata, ifsta, true, true,
2058 WLAN_REASON_DEAUTH_LEAVING);
2060 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2061 queue_work(local->hw.workqueue, &ifsta->work);
2065 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2066 const char *ssid, int ssid_len)
2068 int tmp, hidden_ssid;
2070 if (ssid_len == ifsta->ssid_len &&
2071 !memcmp(ifsta->ssid, ssid, ssid_len))
2074 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2080 if (ssid[tmp] != '\0') {
2086 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2089 if (ssid_len == 1 && ssid[0] == ' ')
2095 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
2096 struct ieee80211_if_sta *ifsta)
2098 struct ieee80211_local *local = sdata->local;
2099 struct ieee80211_sta_bss *bss;
2100 struct ieee80211_supported_band *sband;
2101 u8 bssid[ETH_ALEN], *pos;
2104 DECLARE_MAC_BUF(mac);
2107 /* Easier testing, use fixed BSSID. */
2108 memset(bssid, 0xfe, ETH_ALEN);
2110 /* Generate random, not broadcast, locally administered BSSID. Mix in
2111 * own MAC address to make sure that devices that do not have proper
2112 * random number generator get different BSSID. */
2113 get_random_bytes(bssid, ETH_ALEN);
2114 for (i = 0; i < ETH_ALEN; i++)
2115 bssid[i] ^= sdata->dev->dev_addr[i];
2120 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2121 sdata->dev->name, print_mac(mac, bssid));
2123 bss = ieee80211_rx_bss_add(local, bssid,
2124 local->hw.conf.channel->center_freq,
2125 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2129 bss->band = local->hw.conf.channel->band;
2130 sband = local->hw.wiphy->bands[bss->band];
2132 if (local->hw.conf.beacon_int == 0)
2133 local->hw.conf.beacon_int = 100;
2134 bss->beacon_int = local->hw.conf.beacon_int;
2135 bss->last_update = jiffies;
2136 bss->capability = WLAN_CAPABILITY_IBSS;
2138 if (sdata->default_key)
2139 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2141 sdata->drop_unencrypted = 0;
2143 bss->supp_rates_len = sband->n_bitrates;
2144 pos = bss->supp_rates;
2145 for (i = 0; i < sband->n_bitrates; i++) {
2146 int rate = sband->bitrates[i].bitrate;
2147 *pos++ = (u8) (rate / 5);
2150 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2151 ieee80211_rx_bss_put(local, bss);
2156 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
2157 struct ieee80211_if_sta *ifsta)
2159 struct ieee80211_local *local = sdata->local;
2160 struct ieee80211_sta_bss *bss;
2164 DECLARE_MAC_BUF(mac);
2165 DECLARE_MAC_BUF(mac2);
2167 if (ifsta->ssid_len == 0)
2170 active_ibss = ieee80211_sta_active_ibss(sdata);
2171 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2172 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2173 sdata->dev->name, active_ibss);
2174 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2175 spin_lock_bh(&local->sta_bss_lock);
2176 list_for_each_entry(bss, &local->sta_bss_list, list) {
2177 if (ifsta->ssid_len != bss->ssid_len ||
2178 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2179 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2181 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2182 printk(KERN_DEBUG " bssid=%s found\n",
2183 print_mac(mac, bss->bssid));
2184 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2185 memcpy(bssid, bss->bssid, ETH_ALEN);
2187 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2190 spin_unlock_bh(&local->sta_bss_lock);
2192 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2194 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
2195 "%s\n", print_mac(mac, bssid),
2196 print_mac(mac2, ifsta->bssid));
2197 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2199 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2203 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2204 search_freq = bss->freq;
2206 search_freq = local->hw.conf.channel->center_freq;
2208 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
2209 ifsta->ssid, ifsta->ssid_len);
2213 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2214 " based on configured SSID\n",
2215 sdata->dev->name, print_mac(mac, bssid));
2216 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2217 ieee80211_rx_bss_put(local, bss);
2222 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2223 printk(KERN_DEBUG " did not try to join ibss\n");
2224 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2226 /* Selected IBSS not found in current scan results - try to scan */
2227 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
2228 !ieee80211_sta_active_ibss(sdata)) {
2229 mod_timer(&ifsta->timer, jiffies +
2230 IEEE80211_IBSS_MERGE_INTERVAL);
2231 } else if (time_after(jiffies, local->last_scan_completed +
2232 IEEE80211_SCAN_INTERVAL)) {
2233 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2234 "join\n", sdata->dev->name);
2235 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
2237 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
2238 int interval = IEEE80211_SCAN_INTERVAL;
2240 if (time_after(jiffies, ifsta->ibss_join_req +
2241 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2242 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2243 (!(local->oper_channel->flags &
2244 IEEE80211_CHAN_NO_IBSS)))
2245 return ieee80211_sta_create_ibss(sdata, ifsta);
2246 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2247 printk(KERN_DEBUG "%s: IBSS not allowed on"
2248 " %d MHz\n", sdata->dev->name,
2249 local->hw.conf.channel->center_freq);
2252 /* No IBSS found - decrease scan interval and continue
2254 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2257 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2258 mod_timer(&ifsta->timer, jiffies + interval);
2266 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
2268 struct ieee80211_if_sta *ifsta;
2271 if (len > IEEE80211_MAX_SSID_LEN)
2274 ifsta = &sdata->u.sta;
2276 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
2277 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
2278 memcpy(ifsta->ssid, ssid, len);
2279 ifsta->ssid_len = len;
2280 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2284 * Hack! MLME code needs to be cleaned up to have different
2285 * entry points for configuration and internal selection change
2287 if (netif_running(sdata->dev))
2288 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
2290 printk(KERN_DEBUG "%s: Failed to config new SSID to "
2291 "the low-level driver\n", sdata->dev->name);
2297 ifsta->flags |= IEEE80211_STA_SSID_SET;
2299 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2301 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
2302 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2303 ifsta->ibss_join_req = jiffies;
2304 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2305 return ieee80211_sta_find_ibss(sdata, ifsta);
2312 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2314 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2315 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2316 *len = ifsta->ssid_len;
2321 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2323 struct ieee80211_if_sta *ifsta;
2326 ifsta = &sdata->u.sta;
2328 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2329 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2332 * Hack! See also ieee80211_sta_set_ssid.
2334 if (netif_running(sdata->dev))
2335 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2337 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2338 "the low-level driver\n", sdata->dev->name);
2343 if (is_valid_ether_addr(bssid))
2344 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2346 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2352 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
2354 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2356 kfree(ifsta->extra_ie);
2358 ifsta->extra_ie = NULL;
2359 ifsta->extra_ie_len = 0;
2362 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2363 if (!ifsta->extra_ie) {
2364 ifsta->extra_ie_len = 0;
2367 memcpy(ifsta->extra_ie, ie, len);
2368 ifsta->extra_ie_len = len;
2373 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
2374 struct sk_buff *skb, u8 *bssid,
2375 u8 *addr, u64 supp_rates)
2377 struct ieee80211_local *local = sdata->local;
2378 struct sta_info *sta;
2379 DECLARE_MAC_BUF(mac);
2380 int band = local->hw.conf.channel->band;
2382 /* TODO: Could consider removing the least recently used entry and
2383 * allow new one to be added. */
2384 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2385 if (net_ratelimit()) {
2386 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
2387 "entry %s\n", sdata->dev->name, print_mac(mac, addr));
2392 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
2395 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2396 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
2397 wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
2400 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
2404 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
2406 /* make sure mandatory rates are always added */
2407 sta->supp_rates[band] = supp_rates |
2408 ieee80211_sta_get_mandatory_rates(local, band);
2410 rate_control_rate_init(sta, local);
2412 if (sta_info_insert(sta))
2419 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
2420 struct ieee80211_if_sta *ifsta)
2422 struct ieee80211_local *local = sdata->local;
2423 struct ieee80211_sta_bss *bss, *selected = NULL;
2424 int top_rssi = 0, freq;
2426 spin_lock_bh(&local->sta_bss_lock);
2427 freq = local->oper_channel->center_freq;
2428 list_for_each_entry(bss, &local->sta_bss_list, list) {
2429 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2432 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2433 IEEE80211_STA_AUTO_BSSID_SEL |
2434 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
2435 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2436 !!sdata->default_key))
2439 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2443 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2444 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2447 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2448 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2451 if (!selected || top_rssi < bss->signal) {
2453 top_rssi = bss->signal;
2457 atomic_inc(&selected->users);
2458 spin_unlock_bh(&local->sta_bss_lock);
2461 ieee80211_set_freq(sdata, selected->freq);
2462 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2463 ieee80211_sta_set_ssid(sdata, selected->ssid,
2464 selected->ssid_len);
2465 ieee80211_sta_set_bssid(sdata, selected->bssid);
2466 ieee80211_sta_def_wmm_params(sdata, selected);
2468 /* Send out direct probe if no probe resp was received or
2469 * the one we have is outdated
2471 if (!selected->last_probe_resp ||
2472 time_after(jiffies, selected->last_probe_resp
2473 + IEEE80211_SCAN_RESULT_EXPIRE))
2474 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2476 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2478 ieee80211_rx_bss_put(local, selected);
2479 ieee80211_sta_reset_auth(sdata, ifsta);
2482 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2483 ifsta->assoc_scan_tries++;
2484 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2485 ieee80211_sta_start_scan(sdata, NULL, 0);
2487 ieee80211_sta_start_scan(sdata, ifsta->ssid,
2489 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2490 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2492 ifsta->state = IEEE80211_STA_MLME_DISABLED;
2498 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2500 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2502 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2503 sdata->dev->name, reason);
2505 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2506 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
2509 ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
2514 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2516 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2518 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2519 sdata->dev->name, reason);
2521 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2524 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
2527 ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
2531 void ieee80211_notify_mac(struct ieee80211_hw *hw,
2532 enum ieee80211_notification_types notif_type)
2534 struct ieee80211_local *local = hw_to_local(hw);
2535 struct ieee80211_sub_if_data *sdata;
2537 switch (notif_type) {
2538 case IEEE80211_NOTIFY_RE_ASSOC:
2540 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2541 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2544 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
2550 EXPORT_SYMBOL(ieee80211_notify_mac);
2552 void ieee80211_sta_work(struct work_struct *work)
2554 struct ieee80211_sub_if_data *sdata =
2555 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2556 struct ieee80211_local *local = sdata->local;
2557 struct ieee80211_if_sta *ifsta;
2558 struct sk_buff *skb;
2560 if (!netif_running(sdata->dev))
2563 if (local->sta_sw_scanning || local->sta_hw_scanning)
2566 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2567 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2568 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
2570 ifsta = &sdata->u.sta;
2572 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2573 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2575 #ifdef CONFIG_MAC80211_MESH
2576 if (ifsta->preq_queue_len &&
2578 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
2579 mesh_path_start_discovery(sdata);
2582 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
2583 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
2584 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
2585 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2586 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
2590 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2591 if (ieee80211_sta_config_auth(sdata, ifsta))
2593 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2594 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2597 switch (ifsta->state) {
2598 case IEEE80211_STA_MLME_DISABLED:
2600 case IEEE80211_STA_MLME_DIRECT_PROBE:
2601 ieee80211_direct_probe(sdata, ifsta);
2603 case IEEE80211_STA_MLME_AUTHENTICATE:
2604 ieee80211_authenticate(sdata, ifsta);
2606 case IEEE80211_STA_MLME_ASSOCIATE:
2607 ieee80211_associate(sdata, ifsta);
2609 case IEEE80211_STA_MLME_ASSOCIATED:
2610 ieee80211_associated(sdata, ifsta);
2612 case IEEE80211_STA_MLME_IBSS_SEARCH:
2613 ieee80211_sta_find_ibss(sdata, ifsta);
2615 case IEEE80211_STA_MLME_IBSS_JOINED:
2616 ieee80211_sta_merge_ibss(sdata, ifsta);
2618 #ifdef CONFIG_MAC80211_MESH
2619 case IEEE80211_STA_MLME_MESH_UP:
2620 ieee80211_mesh_housekeeping(sdata, ifsta);
2628 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
2629 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2630 "mixed-cell disabled - disassociate\n", sdata->dev->name);
2632 ieee80211_set_disassoc(sdata, ifsta, false, true,
2633 WLAN_REASON_UNSPECIFIED);
2637 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2639 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2640 struct ieee80211_if_sta *ifsta;
2642 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
2643 ifsta = &sdata->u.sta;
2644 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2645 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
2646 !ieee80211_sta_active_ibss(sdata)))
2647 ieee80211_sta_find_ibss(sdata, ifsta);