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 static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
100 struct ieee80211_if_sta *ifsta,
101 int transaction, u8 *extra, size_t extra_len,
104 struct ieee80211_local *local = sdata->local;
106 struct ieee80211_mgmt *mgmt;
108 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
109 sizeof(*mgmt) + 6 + extra_len);
111 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
112 "frame\n", sdata->dev->name);
115 skb_reserve(skb, local->hw.extra_tx_headroom);
117 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
118 memset(mgmt, 0, 24 + 6);
119 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
120 IEEE80211_STYPE_AUTH);
122 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
123 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
124 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
125 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
126 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
127 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
128 ifsta->auth_transaction = transaction + 1;
129 mgmt->u.auth.status_code = cpu_to_le16(0);
131 memcpy(skb_put(skb, extra_len), extra, extra_len);
133 ieee80211_tx_skb(sdata, skb, encrypt);
136 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
137 u8 *ssid, size_t ssid_len)
139 struct ieee80211_local *local = sdata->local;
140 struct ieee80211_supported_band *sband;
142 struct ieee80211_mgmt *mgmt;
143 u8 *pos, *supp_rates, *esupp_rates = NULL;
146 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
148 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
149 "request\n", sdata->dev->name);
152 skb_reserve(skb, local->hw.extra_tx_headroom);
154 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
156 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
157 IEEE80211_STYPE_PROBE_REQ);
158 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
160 memcpy(mgmt->da, dst, ETH_ALEN);
161 memcpy(mgmt->bssid, dst, ETH_ALEN);
163 memset(mgmt->da, 0xff, ETH_ALEN);
164 memset(mgmt->bssid, 0xff, ETH_ALEN);
166 pos = skb_put(skb, 2 + ssid_len);
167 *pos++ = WLAN_EID_SSID;
169 memcpy(pos, ssid, ssid_len);
171 supp_rates = skb_put(skb, 2);
172 supp_rates[0] = WLAN_EID_SUPP_RATES;
174 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
176 for (i = 0; i < sband->n_bitrates; i++) {
177 struct ieee80211_rate *rate = &sband->bitrates[i];
179 pos = skb_put(skb, 1);
181 } else if (supp_rates[1] == 8) {
182 esupp_rates = skb_put(skb, 3);
183 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
185 pos = &esupp_rates[2];
187 pos = skb_put(skb, 1);
190 *pos = rate->bitrate / 5;
193 ieee80211_tx_skb(sdata, skb, 0);
196 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
197 struct ieee80211_if_sta *ifsta)
199 struct ieee80211_local *local = sdata->local;
201 struct ieee80211_mgmt *mgmt;
202 u8 *pos, *ies, *ht_add_ie;
203 int i, len, count, rates_len, supp_rates_len;
205 struct ieee80211_sta_bss *bss;
207 struct ieee80211_supported_band *sband;
210 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
211 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
214 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
215 "frame\n", sdata->dev->name);
218 skb_reserve(skb, local->hw.extra_tx_headroom);
220 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
222 capab = ifsta->capab;
224 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
225 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
226 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
227 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
228 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
231 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
232 local->hw.conf.channel->center_freq,
233 ifsta->ssid, ifsta->ssid_len);
235 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
236 capab |= WLAN_CAPABILITY_PRIVACY;
240 /* get all rates supported by the device and the AP as
241 * some APs don't like getting a superset of their rates
242 * in the association request (e.g. D-Link DAP 1353 in
244 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
246 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
247 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
248 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
250 ieee80211_rx_bss_put(local, bss);
253 rates_len = sband->n_bitrates;
256 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
258 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
259 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
260 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
262 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
264 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
265 IEEE80211_STYPE_REASSOC_REQ);
266 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
267 mgmt->u.reassoc_req.listen_interval =
268 cpu_to_le16(local->hw.conf.listen_interval);
269 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
273 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
274 IEEE80211_STYPE_ASSOC_REQ);
275 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
276 mgmt->u.reassoc_req.listen_interval =
277 cpu_to_le16(local->hw.conf.listen_interval);
281 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
282 *pos++ = WLAN_EID_SSID;
283 *pos++ = ifsta->ssid_len;
284 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
286 /* add all rates which were marked to be used above */
287 supp_rates_len = rates_len;
288 if (supp_rates_len > 8)
291 len = sband->n_bitrates;
292 pos = skb_put(skb, supp_rates_len + 2);
293 *pos++ = WLAN_EID_SUPP_RATES;
294 *pos++ = supp_rates_len;
297 for (i = 0; i < sband->n_bitrates; i++) {
298 if (BIT(i) & rates) {
299 int rate = sband->bitrates[i].bitrate;
300 *pos++ = (u8) (rate / 5);
306 if (rates_len > count) {
307 pos = skb_put(skb, rates_len - count + 2);
308 *pos++ = WLAN_EID_EXT_SUPP_RATES;
309 *pos++ = rates_len - count;
311 for (i++; i < sband->n_bitrates; i++) {
312 if (BIT(i) & rates) {
313 int rate = sband->bitrates[i].bitrate;
314 *pos++ = (u8) (rate / 5);
319 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
320 /* 1. power capabilities */
321 pos = skb_put(skb, 4);
322 *pos++ = WLAN_EID_PWR_CAPABILITY;
324 *pos++ = 0; /* min tx power */
325 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
327 /* 2. supported channels */
328 /* TODO: get this in reg domain format */
329 pos = skb_put(skb, 2 * sband->n_channels + 2);
330 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
331 *pos++ = 2 * sband->n_channels;
332 for (i = 0; i < sband->n_channels; i++) {
333 *pos++ = ieee80211_frequency_to_channel(
334 sband->channels[i].center_freq);
335 *pos++ = 1; /* one channel in the subband*/
339 if (ifsta->extra_ie) {
340 pos = skb_put(skb, ifsta->extra_ie_len);
341 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
344 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
345 pos = skb_put(skb, 9);
346 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
347 *pos++ = 7; /* len */
348 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
351 *pos++ = 2; /* WME */
352 *pos++ = 0; /* WME info */
353 *pos++ = 1; /* WME ver */
357 /* wmm support is a must to HT */
358 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
359 sband->ht_info.ht_supported &&
360 (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
361 struct ieee80211_ht_addt_info *ht_add_info =
362 (struct ieee80211_ht_addt_info *)ht_add_ie;
363 u16 cap = sband->ht_info.cap;
365 u32 flags = local->hw.conf.channel->flags;
367 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
368 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
369 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
370 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
371 cap &= ~IEEE80211_HT_CAP_SGI_40;
374 case IEEE80211_HT_IE_CHA_SEC_BELOW:
375 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
376 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
377 cap &= ~IEEE80211_HT_CAP_SGI_40;
382 tmp = cpu_to_le16(cap);
383 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
384 *pos++ = WLAN_EID_HT_CAPABILITY;
385 *pos++ = sizeof(struct ieee80211_ht_cap);
386 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
387 memcpy(pos, &tmp, sizeof(u16));
389 /* TODO: needs a define here for << 2 */
390 *pos++ = sband->ht_info.ampdu_factor |
391 (sband->ht_info.ampdu_density << 2);
392 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
395 kfree(ifsta->assocreq_ies);
396 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
397 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
398 if (ifsta->assocreq_ies)
399 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
401 ieee80211_tx_skb(sdata, skb, 0);
405 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
406 u16 stype, u16 reason)
408 struct ieee80211_local *local = sdata->local;
409 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
411 struct ieee80211_mgmt *mgmt;
413 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
415 printk(KERN_DEBUG "%s: failed to allocate buffer for "
416 "deauth/disassoc frame\n", sdata->dev->name);
419 skb_reserve(skb, local->hw.extra_tx_headroom);
421 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
423 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
424 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
425 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
426 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
428 /* u.deauth.reason_code == u.disassoc.reason_code */
429 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
431 ieee80211_tx_skb(sdata, skb, 0);
435 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
436 struct ieee80211_sta_bss *bss)
438 struct ieee80211_local *local = sdata->local;
439 int i, have_higher_than_11mbit = 0;
441 /* cf. IEEE 802.11 9.2.12 */
442 for (i = 0; i < bss->supp_rates_len; i++)
443 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
444 have_higher_than_11mbit = 1;
446 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
447 have_higher_than_11mbit)
448 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
450 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
452 ieee80211_set_wmm_default(sdata);
455 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
456 struct ieee80211_if_sta *ifsta,
457 u8 *wmm_param, size_t wmm_param_len)
459 struct ieee80211_tx_queue_params params;
464 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
470 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
472 count = wmm_param[6] & 0x0f;
473 if (count == ifsta->wmm_last_param_set)
475 ifsta->wmm_last_param_set = count;
478 left = wmm_param_len - 8;
480 memset(¶ms, 0, sizeof(params));
482 if (!local->ops->conf_tx)
486 for (; left >= 4; left -= 4, pos += 4) {
487 int aci = (pos[0] >> 5) & 0x03;
488 int acm = (pos[0] >> 4) & 0x01;
495 local->wmm_acm |= BIT(0) | BIT(3);
500 local->wmm_acm |= BIT(4) | BIT(5);
505 local->wmm_acm |= BIT(6) | BIT(7);
511 local->wmm_acm |= BIT(1) | BIT(2);
515 params.aifs = pos[0] & 0x0f;
516 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
517 params.cw_min = ecw2cw(pos[1] & 0x0f);
518 params.txop = get_unaligned_le16(pos + 2);
519 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
520 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
521 "cWmin=%d cWmax=%d txop=%d\n",
522 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
523 params.cw_max, params.txop);
525 /* TODO: handle ACM (block TX, fallback to next lowest allowed
527 if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) {
528 printk(KERN_DEBUG "%s: failed to set TX queue "
529 "parameters for queue %d\n", local->mdev->name, queue);
534 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
536 bool use_short_preamble)
538 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
539 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
540 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
541 DECLARE_MAC_BUF(mac);
545 if (use_protection != bss_conf->use_cts_prot) {
546 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
547 if (net_ratelimit()) {
548 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
551 use_protection ? "enabled" : "disabled",
552 print_mac(mac, ifsta->bssid));
555 bss_conf->use_cts_prot = use_protection;
556 changed |= BSS_CHANGED_ERP_CTS_PROT;
559 if (use_short_preamble != bss_conf->use_short_preamble) {
560 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
561 if (net_ratelimit()) {
562 printk(KERN_DEBUG "%s: switched to %s barker preamble"
565 use_short_preamble ? "short" : "long",
566 print_mac(mac, ifsta->bssid));
569 bss_conf->use_short_preamble = use_short_preamble;
570 changed |= BSS_CHANGED_ERP_PREAMBLE;
576 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
579 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
580 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
582 return ieee80211_handle_protect_preamb(sdata,
583 use_protection, use_short_preamble);
586 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
587 struct ieee80211_sta_bss *bss)
591 if (bss->has_erp_value)
592 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
594 u16 capab = bss->capability;
595 changed |= ieee80211_handle_protect_preamb(sdata, false,
596 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
602 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
603 struct ieee80211_if_sta *ifsta)
605 union iwreq_data wrqu;
606 memset(&wrqu, 0, sizeof(wrqu));
607 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
608 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
609 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
610 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
613 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
614 struct ieee80211_if_sta *ifsta)
616 union iwreq_data wrqu;
618 if (ifsta->assocreq_ies) {
619 memset(&wrqu, 0, sizeof(wrqu));
620 wrqu.data.length = ifsta->assocreq_ies_len;
621 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
622 ifsta->assocreq_ies);
624 if (ifsta->assocresp_ies) {
625 memset(&wrqu, 0, sizeof(wrqu));
626 wrqu.data.length = ifsta->assocresp_ies_len;
627 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
628 ifsta->assocresp_ies);
633 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
634 struct ieee80211_if_sta *ifsta)
636 struct ieee80211_local *local = sdata->local;
637 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
638 u32 changed = BSS_CHANGED_ASSOC;
640 struct ieee80211_sta_bss *bss;
642 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
644 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
647 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
648 conf->channel->center_freq,
649 ifsta->ssid, ifsta->ssid_len);
651 /* set timing information */
652 sdata->bss_conf.beacon_int = bss->beacon_int;
653 sdata->bss_conf.timestamp = bss->timestamp;
654 sdata->bss_conf.dtim_period = bss->dtim_period;
656 changed |= ieee80211_handle_bss_capability(sdata, bss);
658 ieee80211_rx_bss_put(local, bss);
661 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
662 changed |= BSS_CHANGED_HT;
663 sdata->bss_conf.assoc_ht = 1;
664 sdata->bss_conf.ht_conf = &conf->ht_conf;
665 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
668 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
669 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
670 ieee80211_sta_send_associnfo(sdata, ifsta);
672 ifsta->last_probe = jiffies;
673 ieee80211_led_assoc(local, 1);
675 sdata->bss_conf.assoc = 1;
676 ieee80211_bss_info_change_notify(sdata, changed);
678 netif_tx_start_all_queues(sdata->dev);
679 netif_carrier_on(sdata->dev);
681 ieee80211_sta_send_apinfo(sdata, ifsta);
684 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
685 struct ieee80211_if_sta *ifsta)
687 DECLARE_MAC_BUF(mac);
689 ifsta->direct_probe_tries++;
690 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
691 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
692 sdata->dev->name, print_mac(mac, ifsta->bssid));
693 ifsta->state = IEEE80211_STA_MLME_DISABLED;
697 printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
698 sdata->dev->name, print_mac(mac, ifsta->bssid),
699 ifsta->direct_probe_tries);
701 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
703 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
705 /* Direct probe is sent to broadcast address as some APs
706 * will not answer to direct packet in unassociated state.
708 ieee80211_send_probe_req(sdata, NULL,
709 ifsta->ssid, ifsta->ssid_len);
711 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
715 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
716 struct ieee80211_if_sta *ifsta)
718 DECLARE_MAC_BUF(mac);
721 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
722 printk(KERN_DEBUG "%s: authentication with AP %s"
724 sdata->dev->name, print_mac(mac, ifsta->bssid));
725 ifsta->state = IEEE80211_STA_MLME_DISABLED;
729 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
730 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
731 sdata->dev->name, print_mac(mac, ifsta->bssid));
733 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
735 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
738 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
739 struct ieee80211_if_sta *ifsta, bool deauth,
740 bool self_disconnected, u16 reason)
742 struct ieee80211_local *local = sdata->local;
743 struct sta_info *sta;
744 u32 changed = BSS_CHANGED_ASSOC;
748 sta = sta_info_get(local, ifsta->bssid);
755 ifsta->direct_probe_tries = 0;
756 ifsta->auth_tries = 0;
758 ifsta->assoc_scan_tries = 0;
759 ifsta->assoc_tries = 0;
761 netif_tx_stop_all_queues(sdata->dev);
762 netif_carrier_off(sdata->dev);
764 ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
766 if (self_disconnected) {
768 ieee80211_send_deauth_disassoc(sdata,
769 IEEE80211_STYPE_DEAUTH, reason);
771 ieee80211_send_deauth_disassoc(sdata,
772 IEEE80211_STYPE_DISASSOC, reason);
775 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
776 changed |= ieee80211_reset_erp_info(sdata);
778 if (sdata->bss_conf.assoc_ht)
779 changed |= BSS_CHANGED_HT;
781 sdata->bss_conf.assoc_ht = 0;
782 sdata->bss_conf.ht_conf = NULL;
783 sdata->bss_conf.ht_bss_conf = NULL;
785 ieee80211_led_assoc(local, 0);
786 sdata->bss_conf.assoc = 0;
788 ieee80211_sta_send_apinfo(sdata, ifsta);
790 if (self_disconnected)
791 ifsta->state = IEEE80211_STA_MLME_DISABLED;
793 sta_info_unlink(&sta);
797 sta_info_destroy(sta);
800 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
802 if (!sdata || !sdata->default_key ||
803 sdata->default_key->conf.alg != ALG_WEP)
808 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
809 struct ieee80211_if_sta *ifsta)
811 struct ieee80211_local *local = sdata->local;
812 struct ieee80211_sta_bss *bss;
817 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
820 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
821 local->hw.conf.channel->center_freq,
822 ifsta->ssid, ifsta->ssid_len);
826 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
827 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
828 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
830 ieee80211_rx_bss_put(local, bss);
832 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
838 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
839 struct ieee80211_if_sta *ifsta)
841 DECLARE_MAC_BUF(mac);
843 ifsta->assoc_tries++;
844 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
845 printk(KERN_DEBUG "%s: association with AP %s"
847 sdata->dev->name, print_mac(mac, ifsta->bssid));
848 ifsta->state = IEEE80211_STA_MLME_DISABLED;
852 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
853 printk(KERN_DEBUG "%s: associate with AP %s\n",
854 sdata->dev->name, print_mac(mac, ifsta->bssid));
855 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
856 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
857 "mixed-cell disabled - abort association\n", sdata->dev->name);
858 ifsta->state = IEEE80211_STA_MLME_DISABLED;
862 ieee80211_send_assoc(sdata, ifsta);
864 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
868 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
869 struct ieee80211_if_sta *ifsta)
871 struct ieee80211_local *local = sdata->local;
872 struct sta_info *sta;
874 DECLARE_MAC_BUF(mac);
876 /* TODO: start monitoring current AP signal quality and number of
877 * missed beacons. Scan other channels every now and then and search
879 /* TODO: remove expired BSSes */
881 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
885 sta = sta_info_get(local, ifsta->bssid);
887 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
888 sdata->dev->name, print_mac(mac, ifsta->bssid));
892 if (time_after(jiffies,
893 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
894 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
895 printk(KERN_DEBUG "%s: No ProbeResp from "
896 "current AP %s - assume out of "
898 sdata->dev->name, print_mac(mac, ifsta->bssid));
901 ieee80211_send_probe_req(sdata, ifsta->bssid,
903 local->scan_ssid_len);
904 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
906 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
907 if (time_after(jiffies, ifsta->last_probe +
908 IEEE80211_PROBE_INTERVAL)) {
909 ifsta->last_probe = jiffies;
910 ieee80211_send_probe_req(sdata, ifsta->bssid,
920 ieee80211_set_disassoc(sdata, ifsta, true, true,
921 WLAN_REASON_PREV_AUTH_NOT_VALID);
923 mod_timer(&ifsta->timer, jiffies +
924 IEEE80211_MONITORING_INTERVAL);
928 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
929 struct ieee80211_if_sta *ifsta)
931 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
932 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
933 ieee80211_associate(sdata, ifsta);
937 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
938 struct ieee80211_if_sta *ifsta,
939 struct ieee80211_mgmt *mgmt,
943 struct ieee802_11_elems elems;
945 pos = mgmt->u.auth.variable;
946 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
947 if (!elems.challenge)
949 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
950 elems.challenge_len + 2, 1);
953 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
954 struct ieee80211_if_sta *ifsta,
955 struct ieee80211_mgmt *mgmt,
958 u16 auth_alg, auth_transaction, status_code;
959 DECLARE_MAC_BUF(mac);
961 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
962 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
968 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
969 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
972 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
973 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
976 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
977 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
978 status_code = le16_to_cpu(mgmt->u.auth.status_code);
980 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
982 * IEEE 802.11 standard does not require authentication in IBSS
983 * networks and most implementations do not seem to use it.
984 * However, try to reply to authentication attempts if someone
985 * has actually implemented this.
987 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
989 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
992 if (auth_alg != ifsta->auth_alg ||
993 auth_transaction != ifsta->auth_transaction)
996 if (status_code != WLAN_STATUS_SUCCESS) {
997 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
999 const int num_algs = ARRAY_SIZE(algs);
1001 algs[0] = algs[1] = algs[2] = 0xff;
1002 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1003 algs[0] = WLAN_AUTH_OPEN;
1004 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1005 algs[1] = WLAN_AUTH_SHARED_KEY;
1006 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1007 algs[2] = WLAN_AUTH_LEAP;
1008 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1010 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1014 for (i = 0; i < num_algs; i++) {
1016 if (pos >= num_algs)
1018 if (algs[pos] == ifsta->auth_alg ||
1021 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1022 !ieee80211_sta_wep_configured(sdata))
1024 ifsta->auth_alg = algs[pos];
1031 switch (ifsta->auth_alg) {
1032 case WLAN_AUTH_OPEN:
1033 case WLAN_AUTH_LEAP:
1034 ieee80211_auth_completed(sdata, ifsta);
1036 case WLAN_AUTH_SHARED_KEY:
1037 if (ifsta->auth_transaction == 4)
1038 ieee80211_auth_completed(sdata, ifsta);
1040 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
1046 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1047 struct ieee80211_if_sta *ifsta,
1048 struct ieee80211_mgmt *mgmt,
1052 DECLARE_MAC_BUF(mac);
1057 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1060 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1062 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1063 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
1065 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1066 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1067 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1068 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1069 mod_timer(&ifsta->timer, jiffies +
1070 IEEE80211_RETRY_AUTH_INTERVAL);
1073 ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
1074 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1078 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1079 struct ieee80211_if_sta *ifsta,
1080 struct ieee80211_mgmt *mgmt,
1084 DECLARE_MAC_BUF(mac);
1089 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1092 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1094 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1095 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
1097 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1098 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1099 mod_timer(&ifsta->timer, jiffies +
1100 IEEE80211_RETRY_AUTH_INTERVAL);
1103 ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
1107 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1108 struct ieee80211_if_sta *ifsta,
1109 struct ieee80211_mgmt *mgmt,
1113 struct ieee80211_local *local = sdata->local;
1114 struct ieee80211_supported_band *sband;
1115 struct sta_info *sta;
1116 u64 rates, basic_rates;
1117 u16 capab_info, status_code, aid;
1118 struct ieee802_11_elems elems;
1119 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1122 DECLARE_MAC_BUF(mac);
1123 bool have_higher_than_11mbit = false;
1125 /* AssocResp and ReassocResp have identical structure, so process both
1126 * of them in this function. */
1128 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
1134 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1137 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1138 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1139 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1141 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1142 "status=%d aid=%d)\n",
1143 sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1144 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1146 if (status_code != WLAN_STATUS_SUCCESS) {
1147 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1148 sdata->dev->name, status_code);
1149 /* if this was a reassociation, ensure we try a "full"
1150 * association next time. This works around some broken APs
1151 * which do not correctly reject reassociation requests. */
1152 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1156 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1157 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1158 "set\n", sdata->dev->name, aid);
1159 aid &= ~(BIT(15) | BIT(14));
1161 pos = mgmt->u.assoc_resp.variable;
1162 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1164 if (!elems.supp_rates) {
1165 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1170 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1172 ifsta->ap_capab = capab_info;
1174 kfree(ifsta->assocresp_ies);
1175 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1176 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1177 if (ifsta->assocresp_ies)
1178 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1182 /* Add STA entry for the AP */
1183 sta = sta_info_get(local, ifsta->bssid);
1185 struct ieee80211_sta_bss *bss;
1188 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1190 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1191 " the AP\n", sdata->dev->name);
1195 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1196 local->hw.conf.channel->center_freq,
1197 ifsta->ssid, ifsta->ssid_len);
1199 sta->last_signal = bss->signal;
1200 sta->last_qual = bss->qual;
1201 sta->last_noise = bss->noise;
1202 ieee80211_rx_bss_put(local, bss);
1205 err = sta_info_insert(sta);
1207 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1208 " the AP (error %d)\n", sdata->dev->name, err);
1212 /* update new sta with its last rx activity */
1213 sta->last_rx = jiffies;
1217 * FIXME: Do we really need to update the sta_info's information here?
1218 * We already know about the AP (we found it in our list) so it
1219 * should already be filled with the right info, no?
1220 * As is stands, all this is racy because typically we assume
1221 * the information that is filled in here (except flags) doesn't
1222 * change while a STA structure is alive. As such, it should move
1223 * to between the sta_info_alloc() and sta_info_insert() above.
1226 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1227 WLAN_STA_AUTHORIZED);
1231 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1233 for (i = 0; i < elems.supp_rates_len; i++) {
1234 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1237 have_higher_than_11mbit = true;
1239 for (j = 0; j < sband->n_bitrates; j++) {
1240 if (sband->bitrates[j].bitrate == rate)
1242 if (elems.supp_rates[i] & 0x80)
1243 basic_rates |= BIT(j);
1247 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1248 int rate = (elems.ext_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.ext_supp_rates[i] & 0x80)
1257 basic_rates |= BIT(j);
1261 sta->supp_rates[local->hw.conf.channel->band] = rates;
1262 sdata->basic_rates = basic_rates;
1264 /* cf. IEEE 802.11 9.2.12 */
1265 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1266 have_higher_than_11mbit)
1267 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1269 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1271 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1272 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1273 struct ieee80211_ht_bss_info bss_info;
1274 ieee80211_ht_cap_ie_to_ht_info(
1275 (struct ieee80211_ht_cap *)
1276 elems.ht_cap_elem, &sta->ht_info);
1277 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1278 (struct ieee80211_ht_addt_info *)
1279 elems.ht_info_elem, &bss_info);
1280 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
1283 rate_control_rate_init(sta, local);
1285 if (elems.wmm_param) {
1286 set_sta_flags(sta, WLAN_STA_WME);
1288 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1289 elems.wmm_param_len);
1293 /* set AID and assoc capability,
1294 * ieee80211_set_associated() will tell the driver */
1295 bss_conf->aid = aid;
1296 bss_conf->assoc_capability = capab_info;
1297 ieee80211_set_associated(sdata, ifsta);
1299 ieee80211_associated(sdata, ifsta);
1303 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
1304 struct ieee80211_if_sta *ifsta,
1305 struct ieee80211_sta_bss *bss)
1307 struct ieee80211_local *local = sdata->local;
1308 int res, rates, i, j;
1309 struct sk_buff *skb;
1310 struct ieee80211_mgmt *mgmt;
1312 struct ieee80211_supported_band *sband;
1313 union iwreq_data wrqu;
1315 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1317 /* Remove possible STA entries from other IBSS networks. */
1318 sta_info_flush_delayed(sdata);
1320 if (local->ops->reset_tsf) {
1321 /* Reset own TSF to allow time synchronization work. */
1322 local->ops->reset_tsf(local_to_hw(local));
1324 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
1325 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
1329 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
1331 sdata->drop_unencrypted = bss->capability &
1332 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
1334 res = ieee80211_set_freq(sdata, bss->freq);
1339 /* Build IBSS probe response */
1340 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
1342 skb_reserve(skb, local->hw.extra_tx_headroom);
1344 mgmt = (struct ieee80211_mgmt *)
1345 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1346 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1347 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1348 IEEE80211_STYPE_PROBE_RESP);
1349 memset(mgmt->da, 0xff, ETH_ALEN);
1350 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1351 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1352 mgmt->u.beacon.beacon_int =
1353 cpu_to_le16(local->hw.conf.beacon_int);
1354 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
1355 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
1357 pos = skb_put(skb, 2 + ifsta->ssid_len);
1358 *pos++ = WLAN_EID_SSID;
1359 *pos++ = ifsta->ssid_len;
1360 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
1362 rates = bss->supp_rates_len;
1365 pos = skb_put(skb, 2 + rates);
1366 *pos++ = WLAN_EID_SUPP_RATES;
1368 memcpy(pos, bss->supp_rates, rates);
1370 if (bss->band == IEEE80211_BAND_2GHZ) {
1371 pos = skb_put(skb, 2 + 1);
1372 *pos++ = WLAN_EID_DS_PARAMS;
1374 *pos++ = ieee80211_frequency_to_channel(bss->freq);
1377 pos = skb_put(skb, 2 + 2);
1378 *pos++ = WLAN_EID_IBSS_PARAMS;
1380 /* FIX: set ATIM window based on scan results */
1384 if (bss->supp_rates_len > 8) {
1385 rates = bss->supp_rates_len - 8;
1386 pos = skb_put(skb, 2 + rates);
1387 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1389 memcpy(pos, &bss->supp_rates[8], rates);
1392 ifsta->probe_resp = skb;
1394 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1398 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1399 for (i = 0; i < bss->supp_rates_len; i++) {
1400 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
1401 for (j = 0; j < sband->n_bitrates; j++)
1402 if (sband->bitrates[j].bitrate == bitrate)
1405 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
1407 ieee80211_sta_def_wmm_params(sdata, bss);
1409 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
1410 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1412 memset(&wrqu, 0, sizeof(wrqu));
1413 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
1414 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
1419 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
1420 struct ieee802_11_elems *elems,
1421 enum ieee80211_band band)
1423 struct ieee80211_supported_band *sband;
1424 struct ieee80211_rate *bitrates;
1428 sband = local->hw.wiphy->bands[band];
1432 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1435 bitrates = sband->bitrates;
1436 num_rates = sband->n_bitrates;
1438 for (i = 0; i < elems->supp_rates_len +
1439 elems->ext_supp_rates_len; i++) {
1442 if (i < elems->supp_rates_len)
1443 rate = elems->supp_rates[i];
1444 else if (elems->ext_supp_rates)
1445 rate = elems->ext_supp_rates
1446 [i - elems->supp_rates_len];
1447 own_rate = 5 * (rate & 0x7f);
1448 for (j = 0; j < num_rates; j++)
1449 if (bitrates[j].bitrate == own_rate)
1450 supp_rates |= BIT(j);
1455 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
1456 enum ieee80211_band band)
1458 struct ieee80211_supported_band *sband;
1459 struct ieee80211_rate *bitrates;
1460 u64 mandatory_rates;
1461 enum ieee80211_rate_flags mandatory_flag;
1464 sband = local->hw.wiphy->bands[band];
1467 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1470 if (band == IEEE80211_BAND_2GHZ)
1471 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
1473 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
1475 bitrates = sband->bitrates;
1476 mandatory_rates = 0;
1477 for (i = 0; i < sband->n_bitrates; i++)
1478 if (bitrates[i].flags & mandatory_flag)
1479 mandatory_rates |= BIT(i);
1480 return mandatory_rates;
1483 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1484 struct ieee80211_mgmt *mgmt,
1486 struct ieee80211_rx_status *rx_status,
1487 struct ieee802_11_elems *elems,
1490 struct ieee80211_local *local = sdata->local;
1492 struct ieee80211_sta_bss *bss;
1493 struct sta_info *sta;
1494 struct ieee80211_channel *channel;
1495 u64 beacon_timestamp, rx_timestamp;
1497 enum ieee80211_band band = rx_status->band;
1498 DECLARE_MAC_BUF(mac);
1499 DECLARE_MAC_BUF(mac2);
1501 if (elems->ds_params && elems->ds_params_len == 1)
1502 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1504 freq = rx_status->freq;
1506 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1508 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1511 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
1512 elems->mesh_config && mesh_matches_local(elems, sdata)) {
1513 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1515 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
1516 mesh_peer_accepts_plinks(elems));
1519 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
1520 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1521 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1525 sta = sta_info_get(local, mgmt->sa);
1529 prev_rates = sta->supp_rates[band];
1530 /* make sure mandatory rates are always added */
1531 sta->supp_rates[band] = supp_rates |
1532 ieee80211_sta_get_mandatory_rates(local, band);
1534 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1535 if (sta->supp_rates[band] != prev_rates)
1536 printk(KERN_DEBUG "%s: updated supp_rates set "
1537 "for %s based on beacon info (0x%llx | "
1538 "0x%llx -> 0x%llx)\n",
1539 sdata->dev->name, print_mac(mac, sta->addr),
1540 (unsigned long long) prev_rates,
1541 (unsigned long long) supp_rates,
1542 (unsigned long long) sta->supp_rates[band]);
1545 ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
1546 mgmt->sa, supp_rates);
1552 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1557 /* was just updated in ieee80211_bss_info_update */
1558 beacon_timestamp = bss->timestamp;
1561 * In STA mode, the remaining parameters should not be overridden
1562 * by beacons because they're not necessarily accurate there.
1564 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1565 bss->last_probe_resp && beacon) {
1566 ieee80211_rx_bss_put(local, bss);
1570 /* check if we need to merge IBSS */
1571 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
1572 bss->capability & WLAN_CAPABILITY_IBSS &&
1573 bss->freq == local->oper_channel->center_freq &&
1574 elems->ssid_len == sdata->u.sta.ssid_len &&
1575 memcmp(elems->ssid, sdata->u.sta.ssid,
1576 sdata->u.sta.ssid_len) == 0) {
1577 if (rx_status->flag & RX_FLAG_TSFT) {
1578 /* in order for correct IBSS merging we need mactime
1580 * since mactime is defined as the time the first data
1581 * symbol of the frame hits the PHY, and the timestamp
1582 * of the beacon is defined as "the time that the data
1583 * symbol containing the first bit of the timestamp is
1584 * transmitted to the PHY plus the transmitting STA’s
1585 * delays through its local PHY from the MAC-PHY
1586 * interface to its interface with the WM"
1587 * (802.11 11.1.2) - equals the time this bit arrives at
1588 * the receiver - we have to take into account the
1589 * offset between the two.
1590 * e.g: at 1 MBit that means mactime is 192 usec earlier
1591 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
1593 int rate = local->hw.wiphy->bands[band]->
1594 bitrates[rx_status->rate_idx].bitrate;
1595 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
1596 } else if (local && local->ops && local->ops->get_tsf)
1597 /* second best option: get current TSF */
1598 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
1600 /* can't merge without knowing the TSF */
1601 rx_timestamp = -1LLU;
1602 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1603 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1604 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
1605 print_mac(mac, mgmt->sa),
1606 print_mac(mac2, mgmt->bssid),
1607 (unsigned long long)rx_timestamp,
1608 (unsigned long long)beacon_timestamp,
1609 (unsigned long long)(rx_timestamp - beacon_timestamp),
1611 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1612 if (beacon_timestamp > rx_timestamp) {
1613 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1614 printk(KERN_DEBUG "%s: beacon TSF higher than "
1615 "local TSF - IBSS merge with BSSID %s\n",
1616 sdata->dev->name, print_mac(mac, mgmt->bssid));
1618 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
1619 ieee80211_ibss_add_sta(sdata, NULL,
1620 mgmt->bssid, mgmt->sa,
1625 ieee80211_rx_bss_put(local, bss);
1629 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1630 struct ieee80211_mgmt *mgmt,
1632 struct ieee80211_rx_status *rx_status)
1635 struct ieee802_11_elems elems;
1636 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1638 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1639 return; /* ignore ProbeResp to foreign address */
1641 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1645 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1648 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1650 /* direct probe may be part of the association flow */
1651 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
1653 printk(KERN_DEBUG "%s direct probe responded\n",
1655 ieee80211_authenticate(sdata, ifsta);
1660 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1661 struct ieee80211_mgmt *mgmt,
1663 struct ieee80211_rx_status *rx_status)
1665 struct ieee80211_if_sta *ifsta;
1667 struct ieee802_11_elems elems;
1668 struct ieee80211_local *local = sdata->local;
1669 struct ieee80211_conf *conf = &local->hw.conf;
1672 /* Process beacon from the current BSS */
1673 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1677 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1679 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
1681 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
1683 ifsta = &sdata->u.sta;
1685 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1686 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1689 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1690 elems.wmm_param_len);
1692 if (elems.erp_info && elems.erp_info_len >= 1)
1693 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
1695 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
1696 changed |= ieee80211_handle_protect_preamb(sdata, false,
1697 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
1700 if (elems.ht_cap_elem && elems.ht_info_elem &&
1701 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
1702 struct ieee80211_ht_bss_info bss_info;
1704 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1705 (struct ieee80211_ht_addt_info *)
1706 elems.ht_info_elem, &bss_info);
1707 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
1711 ieee80211_bss_info_change_notify(sdata, changed);
1715 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
1716 struct ieee80211_if_sta *ifsta,
1717 struct ieee80211_mgmt *mgmt,
1719 struct ieee80211_rx_status *rx_status)
1721 struct ieee80211_local *local = sdata->local;
1723 struct sk_buff *skb;
1724 struct ieee80211_mgmt *resp;
1726 DECLARE_MAC_BUF(mac);
1727 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1728 DECLARE_MAC_BUF(mac2);
1729 DECLARE_MAC_BUF(mac3);
1732 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
1733 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
1734 len < 24 + 2 || !ifsta->probe_resp)
1737 if (local->ops->tx_last_beacon)
1738 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1742 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1743 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1744 "%s (tx_last_beacon=%d)\n",
1745 sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1746 print_mac(mac3, mgmt->bssid), tx_last_beacon);
1747 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1749 if (!tx_last_beacon)
1752 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1753 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1756 end = ((u8 *) mgmt) + len;
1757 pos = mgmt->u.probe_req.variable;
1758 if (pos[0] != WLAN_EID_SSID ||
1759 pos + 2 + pos[1] > end) {
1760 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1761 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1763 sdata->dev->name, print_mac(mac, mgmt->sa));
1768 (pos[1] != ifsta->ssid_len ||
1769 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1770 /* Ignore ProbeReq for foreign SSID */
1774 /* Reply with ProbeResp */
1775 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1779 resp = (struct ieee80211_mgmt *) skb->data;
1780 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1781 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1782 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1783 sdata->dev->name, print_mac(mac, resp->da));
1784 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1785 ieee80211_tx_skb(sdata, skb, 0);
1788 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1789 struct ieee80211_if_sta *ifsta,
1790 struct ieee80211_mgmt *mgmt,
1792 struct ieee80211_rx_status *rx_status)
1794 /* currently we only handle mesh interface action frames here */
1795 if (!ieee80211_vif_is_mesh(&sdata->vif))
1798 switch (mgmt->u.action.category) {
1799 case PLINK_CATEGORY:
1800 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1802 case MESH_PATH_SEL_CATEGORY:
1803 mesh_rx_path_sel_frame(sdata, mgmt, len);
1808 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
1809 struct ieee80211_rx_status *rx_status)
1811 struct ieee80211_local *local = sdata->local;
1812 struct ieee80211_if_sta *ifsta;
1813 struct ieee80211_mgmt *mgmt;
1819 ifsta = &sdata->u.sta;
1821 mgmt = (struct ieee80211_mgmt *) skb->data;
1822 fc = le16_to_cpu(mgmt->frame_control);
1824 switch (fc & IEEE80211_FCTL_STYPE) {
1825 case IEEE80211_STYPE_PROBE_REQ:
1826 case IEEE80211_STYPE_PROBE_RESP:
1827 case IEEE80211_STYPE_BEACON:
1828 case IEEE80211_STYPE_ACTION:
1829 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1830 case IEEE80211_STYPE_AUTH:
1831 case IEEE80211_STYPE_ASSOC_RESP:
1832 case IEEE80211_STYPE_REASSOC_RESP:
1833 case IEEE80211_STYPE_DEAUTH:
1834 case IEEE80211_STYPE_DISASSOC:
1835 skb_queue_tail(&ifsta->skb_queue, skb);
1836 queue_work(local->hw.workqueue, &ifsta->work);
1844 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1845 struct sk_buff *skb)
1847 struct ieee80211_rx_status *rx_status;
1848 struct ieee80211_if_sta *ifsta;
1849 struct ieee80211_mgmt *mgmt;
1852 ifsta = &sdata->u.sta;
1854 rx_status = (struct ieee80211_rx_status *) skb->cb;
1855 mgmt = (struct ieee80211_mgmt *) skb->data;
1856 fc = le16_to_cpu(mgmt->frame_control);
1858 switch (fc & IEEE80211_FCTL_STYPE) {
1859 case IEEE80211_STYPE_PROBE_REQ:
1860 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
1863 case IEEE80211_STYPE_PROBE_RESP:
1864 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
1866 case IEEE80211_STYPE_BEACON:
1867 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
1869 case IEEE80211_STYPE_AUTH:
1870 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
1872 case IEEE80211_STYPE_ASSOC_RESP:
1873 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
1875 case IEEE80211_STYPE_REASSOC_RESP:
1876 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
1878 case IEEE80211_STYPE_DEAUTH:
1879 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
1881 case IEEE80211_STYPE_DISASSOC:
1882 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
1884 case IEEE80211_STYPE_ACTION:
1885 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
1893 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
1895 struct ieee80211_local *local = sdata->local;
1897 struct sta_info *sta;
1901 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1902 if (sta->sdata == sdata &&
1903 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1916 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
1918 struct ieee80211_local *local = sdata->local;
1919 struct sta_info *sta, *tmp;
1920 LIST_HEAD(tmp_list);
1921 DECLARE_MAC_BUF(mac);
1922 unsigned long flags;
1924 spin_lock_irqsave(&local->sta_lock, flags);
1925 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1926 if (time_after(jiffies, sta->last_rx + exp_time)) {
1927 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1928 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
1929 sdata->dev->name, print_mac(mac, sta->addr));
1931 __sta_info_unlink(&sta);
1933 list_add(&sta->list, &tmp_list);
1935 spin_unlock_irqrestore(&local->sta_lock, flags);
1937 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
1938 sta_info_destroy(sta);
1942 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
1943 struct ieee80211_if_sta *ifsta)
1945 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1947 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
1948 if (ieee80211_sta_active_ibss(sdata))
1951 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1952 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
1953 ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
1957 #ifdef CONFIG_MAC80211_MESH
1958 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
1959 struct ieee80211_if_sta *ifsta)
1963 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
1964 mesh_path_expire(sdata);
1966 free_plinks = mesh_plink_availables(sdata);
1967 if (free_plinks != sdata->u.sta.accepting_plinks)
1968 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1970 mod_timer(&ifsta->timer, jiffies +
1971 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
1975 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
1977 struct ieee80211_if_sta *ifsta;
1978 ifsta = &sdata->u.sta;
1979 ifsta->state = IEEE80211_STA_MLME_MESH_UP;
1980 ieee80211_sta_timer((unsigned long)sdata);
1981 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1986 void ieee80211_sta_timer(unsigned long data)
1988 struct ieee80211_sub_if_data *sdata =
1989 (struct ieee80211_sub_if_data *) data;
1990 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1991 struct ieee80211_local *local = sdata->local;
1993 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1994 queue_work(local->hw.workqueue, &ifsta->work);
1997 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
1998 struct ieee80211_if_sta *ifsta)
2000 struct ieee80211_local *local = sdata->local;
2002 if (local->ops->reset_tsf) {
2003 /* Reset own TSF to allow time synchronization work. */
2004 local->ops->reset_tsf(local_to_hw(local));
2007 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2010 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2011 ifsta->auth_alg = WLAN_AUTH_OPEN;
2012 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2013 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2014 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2015 ifsta->auth_alg = WLAN_AUTH_LEAP;
2017 ifsta->auth_alg = WLAN_AUTH_OPEN;
2018 ifsta->auth_transaction = -1;
2019 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2020 ifsta->assoc_scan_tries = 0;
2021 ifsta->direct_probe_tries = 0;
2022 ifsta->auth_tries = 0;
2023 ifsta->assoc_tries = 0;
2024 netif_tx_stop_all_queues(sdata->dev);
2025 netif_carrier_off(sdata->dev);
2029 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
2030 struct ieee80211_if_sta *ifsta)
2032 struct ieee80211_local *local = sdata->local;
2034 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2037 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2038 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2039 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2040 IEEE80211_STA_AUTO_SSID_SEL))) {
2042 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
2043 ieee80211_set_disassoc(sdata, ifsta, true, true,
2044 WLAN_REASON_DEAUTH_LEAVING);
2046 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2047 queue_work(local->hw.workqueue, &ifsta->work);
2051 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2052 const char *ssid, int ssid_len)
2054 int tmp, hidden_ssid;
2056 if (ssid_len == ifsta->ssid_len &&
2057 !memcmp(ifsta->ssid, ssid, ssid_len))
2060 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2066 if (ssid[tmp] != '\0') {
2072 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2075 if (ssid_len == 1 && ssid[0] == ' ')
2081 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
2082 struct ieee80211_if_sta *ifsta)
2084 struct ieee80211_local *local = sdata->local;
2085 struct ieee80211_sta_bss *bss;
2086 struct ieee80211_supported_band *sband;
2087 u8 bssid[ETH_ALEN], *pos;
2090 DECLARE_MAC_BUF(mac);
2093 /* Easier testing, use fixed BSSID. */
2094 memset(bssid, 0xfe, ETH_ALEN);
2096 /* Generate random, not broadcast, locally administered BSSID. Mix in
2097 * own MAC address to make sure that devices that do not have proper
2098 * random number generator get different BSSID. */
2099 get_random_bytes(bssid, ETH_ALEN);
2100 for (i = 0; i < ETH_ALEN; i++)
2101 bssid[i] ^= sdata->dev->dev_addr[i];
2106 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2107 sdata->dev->name, print_mac(mac, bssid));
2109 bss = ieee80211_rx_bss_add(local, bssid,
2110 local->hw.conf.channel->center_freq,
2111 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2115 bss->band = local->hw.conf.channel->band;
2116 sband = local->hw.wiphy->bands[bss->band];
2118 if (local->hw.conf.beacon_int == 0)
2119 local->hw.conf.beacon_int = 100;
2120 bss->beacon_int = local->hw.conf.beacon_int;
2121 bss->last_update = jiffies;
2122 bss->capability = WLAN_CAPABILITY_IBSS;
2124 if (sdata->default_key)
2125 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2127 sdata->drop_unencrypted = 0;
2129 bss->supp_rates_len = sband->n_bitrates;
2130 pos = bss->supp_rates;
2131 for (i = 0; i < sband->n_bitrates; i++) {
2132 int rate = sband->bitrates[i].bitrate;
2133 *pos++ = (u8) (rate / 5);
2136 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2137 ieee80211_rx_bss_put(local, bss);
2142 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
2143 struct ieee80211_if_sta *ifsta)
2145 struct ieee80211_local *local = sdata->local;
2146 struct ieee80211_sta_bss *bss;
2150 DECLARE_MAC_BUF(mac);
2151 DECLARE_MAC_BUF(mac2);
2153 if (ifsta->ssid_len == 0)
2156 active_ibss = ieee80211_sta_active_ibss(sdata);
2157 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2158 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2159 sdata->dev->name, active_ibss);
2160 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2161 spin_lock_bh(&local->sta_bss_lock);
2162 list_for_each_entry(bss, &local->sta_bss_list, list) {
2163 if (ifsta->ssid_len != bss->ssid_len ||
2164 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2165 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2167 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2168 printk(KERN_DEBUG " bssid=%s found\n",
2169 print_mac(mac, bss->bssid));
2170 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2171 memcpy(bssid, bss->bssid, ETH_ALEN);
2173 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2176 spin_unlock_bh(&local->sta_bss_lock);
2178 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2180 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
2181 "%s\n", print_mac(mac, bssid),
2182 print_mac(mac2, ifsta->bssid));
2183 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2185 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2189 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2190 search_freq = bss->freq;
2192 search_freq = local->hw.conf.channel->center_freq;
2194 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
2195 ifsta->ssid, ifsta->ssid_len);
2199 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2200 " based on configured SSID\n",
2201 sdata->dev->name, print_mac(mac, bssid));
2202 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2203 ieee80211_rx_bss_put(local, bss);
2208 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2209 printk(KERN_DEBUG " did not try to join ibss\n");
2210 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2212 /* Selected IBSS not found in current scan results - try to scan */
2213 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
2214 !ieee80211_sta_active_ibss(sdata)) {
2215 mod_timer(&ifsta->timer, jiffies +
2216 IEEE80211_IBSS_MERGE_INTERVAL);
2217 } else if (time_after(jiffies, local->last_scan_completed +
2218 IEEE80211_SCAN_INTERVAL)) {
2219 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2220 "join\n", sdata->dev->name);
2221 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
2223 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
2224 int interval = IEEE80211_SCAN_INTERVAL;
2226 if (time_after(jiffies, ifsta->ibss_join_req +
2227 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2228 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2229 (!(local->oper_channel->flags &
2230 IEEE80211_CHAN_NO_IBSS)))
2231 return ieee80211_sta_create_ibss(sdata, ifsta);
2232 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2233 printk(KERN_DEBUG "%s: IBSS not allowed on"
2234 " %d MHz\n", sdata->dev->name,
2235 local->hw.conf.channel->center_freq);
2238 /* No IBSS found - decrease scan interval and continue
2240 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2243 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2244 mod_timer(&ifsta->timer, jiffies + interval);
2252 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
2254 struct ieee80211_if_sta *ifsta;
2257 if (len > IEEE80211_MAX_SSID_LEN)
2260 ifsta = &sdata->u.sta;
2262 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
2263 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
2264 memcpy(ifsta->ssid, ssid, len);
2265 ifsta->ssid_len = len;
2266 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2270 * Hack! MLME code needs to be cleaned up to have different
2271 * entry points for configuration and internal selection change
2273 if (netif_running(sdata->dev))
2274 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
2276 printk(KERN_DEBUG "%s: Failed to config new SSID to "
2277 "the low-level driver\n", sdata->dev->name);
2283 ifsta->flags |= IEEE80211_STA_SSID_SET;
2285 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2287 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
2288 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2289 ifsta->ibss_join_req = jiffies;
2290 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2291 return ieee80211_sta_find_ibss(sdata, ifsta);
2298 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2300 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2301 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2302 *len = ifsta->ssid_len;
2307 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2309 struct ieee80211_if_sta *ifsta;
2312 ifsta = &sdata->u.sta;
2314 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2315 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2318 * Hack! See also ieee80211_sta_set_ssid.
2320 if (netif_running(sdata->dev))
2321 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2323 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2324 "the low-level driver\n", sdata->dev->name);
2329 if (is_valid_ether_addr(bssid))
2330 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2332 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2338 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
2340 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2342 kfree(ifsta->extra_ie);
2344 ifsta->extra_ie = NULL;
2345 ifsta->extra_ie_len = 0;
2348 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2349 if (!ifsta->extra_ie) {
2350 ifsta->extra_ie_len = 0;
2353 memcpy(ifsta->extra_ie, ie, len);
2354 ifsta->extra_ie_len = len;
2359 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
2360 struct sk_buff *skb, u8 *bssid,
2361 u8 *addr, u64 supp_rates)
2363 struct ieee80211_local *local = sdata->local;
2364 struct sta_info *sta;
2365 DECLARE_MAC_BUF(mac);
2366 int band = local->hw.conf.channel->band;
2368 /* TODO: Could consider removing the least recently used entry and
2369 * allow new one to be added. */
2370 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2371 if (net_ratelimit()) {
2372 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
2373 "entry %s\n", sdata->dev->name, print_mac(mac, addr));
2378 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
2381 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2382 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
2383 wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
2386 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
2390 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
2392 /* make sure mandatory rates are always added */
2393 sta->supp_rates[band] = supp_rates |
2394 ieee80211_sta_get_mandatory_rates(local, band);
2396 rate_control_rate_init(sta, local);
2398 if (sta_info_insert(sta))
2405 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
2406 struct ieee80211_if_sta *ifsta)
2408 struct ieee80211_local *local = sdata->local;
2409 struct ieee80211_sta_bss *bss, *selected = NULL;
2410 int top_rssi = 0, freq;
2412 spin_lock_bh(&local->sta_bss_lock);
2413 freq = local->oper_channel->center_freq;
2414 list_for_each_entry(bss, &local->sta_bss_list, list) {
2415 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2418 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2419 IEEE80211_STA_AUTO_BSSID_SEL |
2420 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
2421 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2422 !!sdata->default_key))
2425 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2429 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2430 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2433 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2434 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2437 if (!selected || top_rssi < bss->signal) {
2439 top_rssi = bss->signal;
2443 atomic_inc(&selected->users);
2444 spin_unlock_bh(&local->sta_bss_lock);
2447 ieee80211_set_freq(sdata, selected->freq);
2448 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2449 ieee80211_sta_set_ssid(sdata, selected->ssid,
2450 selected->ssid_len);
2451 ieee80211_sta_set_bssid(sdata, selected->bssid);
2452 ieee80211_sta_def_wmm_params(sdata, selected);
2454 /* Send out direct probe if no probe resp was received or
2455 * the one we have is outdated
2457 if (!selected->last_probe_resp ||
2458 time_after(jiffies, selected->last_probe_resp
2459 + IEEE80211_SCAN_RESULT_EXPIRE))
2460 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2462 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2464 ieee80211_rx_bss_put(local, selected);
2465 ieee80211_sta_reset_auth(sdata, ifsta);
2468 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2469 ifsta->assoc_scan_tries++;
2470 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2471 ieee80211_sta_start_scan(sdata, NULL, 0);
2473 ieee80211_sta_start_scan(sdata, ifsta->ssid,
2475 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2476 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2478 ifsta->state = IEEE80211_STA_MLME_DISABLED;
2484 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2486 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2488 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2489 sdata->dev->name, reason);
2491 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2492 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
2495 ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
2500 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2502 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2504 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2505 sdata->dev->name, reason);
2507 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2510 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
2513 ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
2517 void ieee80211_notify_mac(struct ieee80211_hw *hw,
2518 enum ieee80211_notification_types notif_type)
2520 struct ieee80211_local *local = hw_to_local(hw);
2521 struct ieee80211_sub_if_data *sdata;
2523 switch (notif_type) {
2524 case IEEE80211_NOTIFY_RE_ASSOC:
2526 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2527 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2530 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
2536 EXPORT_SYMBOL(ieee80211_notify_mac);
2538 void ieee80211_sta_work(struct work_struct *work)
2540 struct ieee80211_sub_if_data *sdata =
2541 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2542 struct ieee80211_local *local = sdata->local;
2543 struct ieee80211_if_sta *ifsta;
2544 struct sk_buff *skb;
2546 if (!netif_running(sdata->dev))
2549 if (local->sta_sw_scanning || local->sta_hw_scanning)
2552 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2553 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2554 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
2556 ifsta = &sdata->u.sta;
2558 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2559 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2561 #ifdef CONFIG_MAC80211_MESH
2562 if (ifsta->preq_queue_len &&
2564 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
2565 mesh_path_start_discovery(sdata);
2568 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
2569 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
2570 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
2571 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2572 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
2576 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2577 if (ieee80211_sta_config_auth(sdata, ifsta))
2579 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2580 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2583 switch (ifsta->state) {
2584 case IEEE80211_STA_MLME_DISABLED:
2586 case IEEE80211_STA_MLME_DIRECT_PROBE:
2587 ieee80211_direct_probe(sdata, ifsta);
2589 case IEEE80211_STA_MLME_AUTHENTICATE:
2590 ieee80211_authenticate(sdata, ifsta);
2592 case IEEE80211_STA_MLME_ASSOCIATE:
2593 ieee80211_associate(sdata, ifsta);
2595 case IEEE80211_STA_MLME_ASSOCIATED:
2596 ieee80211_associated(sdata, ifsta);
2598 case IEEE80211_STA_MLME_IBSS_SEARCH:
2599 ieee80211_sta_find_ibss(sdata, ifsta);
2601 case IEEE80211_STA_MLME_IBSS_JOINED:
2602 ieee80211_sta_merge_ibss(sdata, ifsta);
2604 #ifdef CONFIG_MAC80211_MESH
2605 case IEEE80211_STA_MLME_MESH_UP:
2606 ieee80211_mesh_housekeeping(sdata, ifsta);
2614 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
2615 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2616 "mixed-cell disabled - disassociate\n", sdata->dev->name);
2618 ieee80211_set_disassoc(sdata, ifsta, false, true,
2619 WLAN_REASON_UNSPECIFIED);
2623 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2625 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2626 struct ieee80211_if_sta *ifsta;
2628 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
2629 ifsta = &sdata->u.sta;
2630 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2631 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
2632 !ieee80211_sta_active_ibss(sdata)))
2633 ieee80211_sta_find_ibss(sdata, ifsta);