mac80211: enable spatial multiplexing powersave
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / mlme.c
blob0a762a9ba4df3311eb810325940f2752637cb4e9
1 /*
2 * BSS client mode implementation
3 * Copyright 2003-2008, Jouni Malinen <j@w1.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/if_arp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/pm_qos_params.h>
21 #include <linux/crc32.h>
22 #include <net/mac80211.h>
23 #include <asm/unaligned.h>
25 #include "ieee80211_i.h"
26 #include "driver-ops.h"
27 #include "rate.h"
28 #include "led.h"
30 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
31 #define IEEE80211_AUTH_MAX_TRIES 3
32 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
33 #define IEEE80211_ASSOC_MAX_TRIES 3
34 #define IEEE80211_MAX_PROBE_TRIES 5
37 * beacon loss detection timeout
38 * XXX: should depend on beacon interval
40 #define IEEE80211_BEACON_LOSS_TIME (2 * HZ)
42 * Time the connection can be idle before we probe
43 * it to see if we can still talk to the AP.
45 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
47 * Time we wait for a probe response after sending
48 * a probe request because of beacon loss or for
49 * checking the connection still works.
51 #define IEEE80211_PROBE_WAIT (HZ / 2)
53 #define TMR_RUNNING_TIMER 0
54 #define TMR_RUNNING_CHANSW 1
57 * All cfg80211 functions have to be called outside a locked
58 * section so that they can acquire a lock themselves... This
59 * is much simpler than queuing up things in cfg80211, but we
60 * do need some indirection for that here.
62 enum rx_mgmt_action {
63 /* no action required */
64 RX_MGMT_NONE,
66 /* caller must call cfg80211_send_rx_auth() */
67 RX_MGMT_CFG80211_AUTH,
69 /* caller must call cfg80211_send_rx_assoc() */
70 RX_MGMT_CFG80211_ASSOC,
72 /* caller must call cfg80211_send_deauth() */
73 RX_MGMT_CFG80211_DEAUTH,
75 /* caller must call cfg80211_send_disassoc() */
76 RX_MGMT_CFG80211_DISASSOC,
78 /* caller must call cfg80211_auth_timeout() & free work */
79 RX_MGMT_CFG80211_AUTH_TO,
81 /* caller must call cfg80211_assoc_timeout() & free work */
82 RX_MGMT_CFG80211_ASSOC_TO,
85 /* utils */
86 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
88 WARN_ON(!mutex_is_locked(&ifmgd->mtx));
92 * We can have multiple work items (and connection probing)
93 * scheduling this timer, but we need to take care to only
94 * reschedule it when it should fire _earlier_ than it was
95 * asked for before, or if it's not pending right now. This
96 * function ensures that. Note that it then is required to
97 * run this function for all timeouts after the first one
98 * has happened -- the work that runs from this timer will
99 * do that.
101 static void run_again(struct ieee80211_if_managed *ifmgd,
102 unsigned long timeout)
104 ASSERT_MGD_MTX(ifmgd);
106 if (!timer_pending(&ifmgd->timer) ||
107 time_before(timeout, ifmgd->timer.expires))
108 mod_timer(&ifmgd->timer, timeout);
111 static void mod_beacon_timer(struct ieee80211_sub_if_data *sdata)
113 if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
114 return;
116 mod_timer(&sdata->u.mgd.bcn_mon_timer,
117 round_jiffies_up(jiffies + IEEE80211_BEACON_LOSS_TIME));
120 static int ecw2cw(int ecw)
122 return (1 << ecw) - 1;
125 static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
126 struct ieee80211_supported_band *sband,
127 u32 *rates)
129 int i, j, count;
130 *rates = 0;
131 count = 0;
132 for (i = 0; i < bss->supp_rates_len; i++) {
133 int rate = (bss->supp_rates[i] & 0x7F) * 5;
135 for (j = 0; j < sband->n_bitrates; j++)
136 if (sband->bitrates[j].bitrate == rate) {
137 *rates |= BIT(j);
138 count++;
139 break;
143 return count;
147 * ieee80211_enable_ht should be called only after the operating band
148 * has been determined as ht configuration depends on the hw's
149 * HT abilities for a specific band.
151 static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
152 struct ieee80211_ht_info *hti,
153 const u8 *bssid, u16 ap_ht_cap_flags)
155 struct ieee80211_local *local = sdata->local;
156 struct ieee80211_supported_band *sband;
157 struct sta_info *sta;
158 u32 changed = 0;
159 u16 ht_opmode;
160 bool enable_ht = true, ht_changed;
161 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
163 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
165 /* HT is not supported */
166 if (!sband->ht_cap.ht_supported)
167 enable_ht = false;
169 /* check that channel matches the right operating channel */
170 if (local->hw.conf.channel->center_freq !=
171 ieee80211_channel_to_frequency(hti->control_chan))
172 enable_ht = false;
174 if (enable_ht) {
175 channel_type = NL80211_CHAN_HT20;
177 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
178 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
179 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
180 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
181 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
182 if (!(local->hw.conf.channel->flags &
183 IEEE80211_CHAN_NO_HT40PLUS))
184 channel_type = NL80211_CHAN_HT40PLUS;
185 break;
186 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
187 if (!(local->hw.conf.channel->flags &
188 IEEE80211_CHAN_NO_HT40MINUS))
189 channel_type = NL80211_CHAN_HT40MINUS;
190 break;
195 ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
196 channel_type != local->hw.conf.channel_type;
198 local->oper_channel_type = channel_type;
200 if (ht_changed) {
201 /* channel_type change automatically detected */
202 ieee80211_hw_config(local, 0);
204 rcu_read_lock();
205 sta = sta_info_get(sdata, bssid);
206 if (sta)
207 rate_control_rate_update(local, sband, sta,
208 IEEE80211_RC_HT_CHANGED);
209 rcu_read_unlock();
212 /* disable HT */
213 if (!enable_ht)
214 return 0;
216 ht_opmode = le16_to_cpu(hti->operation_mode);
218 /* if bss configuration changed store the new one */
219 if (!sdata->ht_opmode_valid ||
220 sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
221 changed |= BSS_CHANGED_HT;
222 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
223 sdata->ht_opmode_valid = true;
226 return changed;
229 /* frame sending functions */
231 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
232 struct ieee80211_mgd_work *wk)
234 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
235 struct ieee80211_local *local = sdata->local;
236 struct sk_buff *skb;
237 struct ieee80211_mgmt *mgmt;
238 u8 *pos;
239 const u8 *ies, *ht_ie;
240 int i, len, count, rates_len, supp_rates_len;
241 u16 capab;
242 int wmm = 0;
243 struct ieee80211_supported_band *sband;
244 u32 rates = 0;
246 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
247 sizeof(*mgmt) + 200 + wk->ie_len +
248 wk->ssid_len);
249 if (!skb) {
250 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
251 "frame\n", sdata->name);
252 return;
254 skb_reserve(skb, local->hw.extra_tx_headroom);
256 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
258 capab = ifmgd->capab;
260 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
261 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
262 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
263 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
264 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
267 if (wk->bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
268 capab |= WLAN_CAPABILITY_PRIVACY;
269 if (wk->bss->wmm_used)
270 wmm = 1;
272 /* get all rates supported by the device and the AP as
273 * some APs don't like getting a superset of their rates
274 * in the association request (e.g. D-Link DAP 1353 in
275 * b-only mode) */
276 rates_len = ieee80211_compatible_rates(wk->bss, sband, &rates);
278 if ((wk->bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
279 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
280 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
282 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
283 memset(mgmt, 0, 24);
284 memcpy(mgmt->da, wk->bss->cbss.bssid, ETH_ALEN);
285 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
286 memcpy(mgmt->bssid, wk->bss->cbss.bssid, ETH_ALEN);
288 if (!is_zero_ether_addr(wk->prev_bssid)) {
289 skb_put(skb, 10);
290 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
291 IEEE80211_STYPE_REASSOC_REQ);
292 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
293 mgmt->u.reassoc_req.listen_interval =
294 cpu_to_le16(local->hw.conf.listen_interval);
295 memcpy(mgmt->u.reassoc_req.current_ap, wk->prev_bssid,
296 ETH_ALEN);
297 } else {
298 skb_put(skb, 4);
299 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
300 IEEE80211_STYPE_ASSOC_REQ);
301 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
302 mgmt->u.assoc_req.listen_interval =
303 cpu_to_le16(local->hw.conf.listen_interval);
306 /* SSID */
307 ies = pos = skb_put(skb, 2 + wk->ssid_len);
308 *pos++ = WLAN_EID_SSID;
309 *pos++ = wk->ssid_len;
310 memcpy(pos, wk->ssid, wk->ssid_len);
312 /* add all rates which were marked to be used above */
313 supp_rates_len = rates_len;
314 if (supp_rates_len > 8)
315 supp_rates_len = 8;
317 len = sband->n_bitrates;
318 pos = skb_put(skb, supp_rates_len + 2);
319 *pos++ = WLAN_EID_SUPP_RATES;
320 *pos++ = supp_rates_len;
322 count = 0;
323 for (i = 0; i < sband->n_bitrates; i++) {
324 if (BIT(i) & rates) {
325 int rate = sband->bitrates[i].bitrate;
326 *pos++ = (u8) (rate / 5);
327 if (++count == 8)
328 break;
332 if (rates_len > count) {
333 pos = skb_put(skb, rates_len - count + 2);
334 *pos++ = WLAN_EID_EXT_SUPP_RATES;
335 *pos++ = rates_len - count;
337 for (i++; i < sband->n_bitrates; i++) {
338 if (BIT(i) & rates) {
339 int rate = sband->bitrates[i].bitrate;
340 *pos++ = (u8) (rate / 5);
345 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
346 /* 1. power capabilities */
347 pos = skb_put(skb, 4);
348 *pos++ = WLAN_EID_PWR_CAPABILITY;
349 *pos++ = 2;
350 *pos++ = 0; /* min tx power */
351 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
353 /* 2. supported channels */
354 /* TODO: get this in reg domain format */
355 pos = skb_put(skb, 2 * sband->n_channels + 2);
356 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
357 *pos++ = 2 * sband->n_channels;
358 for (i = 0; i < sband->n_channels; i++) {
359 *pos++ = ieee80211_frequency_to_channel(
360 sband->channels[i].center_freq);
361 *pos++ = 1; /* one channel in the subband*/
365 if (wk->ie_len && wk->ie) {
366 pos = skb_put(skb, wk->ie_len);
367 memcpy(pos, wk->ie, wk->ie_len);
370 if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
371 pos = skb_put(skb, 9);
372 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
373 *pos++ = 7; /* len */
374 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
375 *pos++ = 0x50;
376 *pos++ = 0xf2;
377 *pos++ = 2; /* WME */
378 *pos++ = 0; /* WME info */
379 *pos++ = 1; /* WME ver */
380 *pos++ = 0;
383 /* wmm support is a must to HT */
385 * IEEE802.11n does not allow TKIP/WEP as pairwise
386 * ciphers in HT mode. We still associate in non-ht
387 * mode (11a/b/g) if any one of these ciphers is
388 * configured as pairwise.
390 if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
391 sband->ht_cap.ht_supported &&
392 (ht_ie = ieee80211_bss_get_ie(&wk->bss->cbss, WLAN_EID_HT_INFORMATION)) &&
393 ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
394 (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))) {
395 struct ieee80211_ht_info *ht_info =
396 (struct ieee80211_ht_info *)(ht_ie + 2);
397 u16 cap = sband->ht_cap.cap;
398 __le16 tmp;
399 u32 flags = local->hw.conf.channel->flags;
401 /* determine capability flags */
403 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
404 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
405 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
406 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
407 cap &= ~IEEE80211_HT_CAP_SGI_40;
409 break;
410 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
411 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
412 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
413 cap &= ~IEEE80211_HT_CAP_SGI_40;
415 break;
418 /* set SM PS mode properly */
419 cap &= ~IEEE80211_HT_CAP_SM_PS;
420 /* new association always uses requested smps mode */
421 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
422 if (ifmgd->powersave)
423 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
424 else
425 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
426 } else
427 ifmgd->ap_smps = ifmgd->req_smps;
429 switch (ifmgd->ap_smps) {
430 case IEEE80211_SMPS_AUTOMATIC:
431 case IEEE80211_SMPS_NUM_MODES:
432 WARN_ON(1);
433 case IEEE80211_SMPS_OFF:
434 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
435 IEEE80211_HT_CAP_SM_PS_SHIFT;
436 break;
437 case IEEE80211_SMPS_STATIC:
438 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
439 IEEE80211_HT_CAP_SM_PS_SHIFT;
440 break;
441 case IEEE80211_SMPS_DYNAMIC:
442 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
443 IEEE80211_HT_CAP_SM_PS_SHIFT;
444 break;
447 /* reserve and fill IE */
449 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
450 *pos++ = WLAN_EID_HT_CAPABILITY;
451 *pos++ = sizeof(struct ieee80211_ht_cap);
452 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
454 /* capability flags */
455 tmp = cpu_to_le16(cap);
456 memcpy(pos, &tmp, sizeof(u16));
457 pos += sizeof(u16);
459 /* AMPDU parameters */
460 *pos++ = sband->ht_cap.ampdu_factor |
461 (sband->ht_cap.ampdu_density <<
462 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
464 /* MCS set */
465 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
466 pos += sizeof(sband->ht_cap.mcs);
468 /* extended capabilities */
469 pos += sizeof(__le16);
471 /* BF capabilities */
472 pos += sizeof(__le32);
474 /* antenna selection */
475 pos += sizeof(u8);
478 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
479 ieee80211_tx_skb(sdata, skb);
483 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
484 const u8 *bssid, u16 stype, u16 reason,
485 void *cookie)
487 struct ieee80211_local *local = sdata->local;
488 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
489 struct sk_buff *skb;
490 struct ieee80211_mgmt *mgmt;
492 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
493 if (!skb) {
494 printk(KERN_DEBUG "%s: failed to allocate buffer for "
495 "deauth/disassoc frame\n", sdata->name);
496 return;
498 skb_reserve(skb, local->hw.extra_tx_headroom);
500 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
501 memset(mgmt, 0, 24);
502 memcpy(mgmt->da, bssid, ETH_ALEN);
503 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
504 memcpy(mgmt->bssid, bssid, ETH_ALEN);
505 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
506 skb_put(skb, 2);
507 /* u.deauth.reason_code == u.disassoc.reason_code */
508 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
510 if (stype == IEEE80211_STYPE_DEAUTH)
511 if (cookie)
512 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
513 else
514 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
515 else
516 if (cookie)
517 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
518 else
519 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
520 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
521 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
522 ieee80211_tx_skb(sdata, skb);
525 void ieee80211_send_pspoll(struct ieee80211_local *local,
526 struct ieee80211_sub_if_data *sdata)
528 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
529 struct ieee80211_pspoll *pspoll;
530 struct sk_buff *skb;
531 u16 fc;
533 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
534 if (!skb) {
535 printk(KERN_DEBUG "%s: failed to allocate buffer for "
536 "pspoll frame\n", sdata->name);
537 return;
539 skb_reserve(skb, local->hw.extra_tx_headroom);
541 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
542 memset(pspoll, 0, sizeof(*pspoll));
543 fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
544 pspoll->frame_control = cpu_to_le16(fc);
545 pspoll->aid = cpu_to_le16(ifmgd->aid);
547 /* aid in PS-Poll has its two MSBs each set to 1 */
548 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
550 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
551 memcpy(pspoll->ta, sdata->vif.addr, ETH_ALEN);
553 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
554 ieee80211_tx_skb(sdata, skb);
557 void ieee80211_send_nullfunc(struct ieee80211_local *local,
558 struct ieee80211_sub_if_data *sdata,
559 int powersave)
561 struct sk_buff *skb;
562 struct ieee80211_hdr *nullfunc;
563 __le16 fc;
565 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
566 return;
568 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
569 if (!skb) {
570 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
571 "frame\n", sdata->name);
572 return;
574 skb_reserve(skb, local->hw.extra_tx_headroom);
576 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
577 memset(nullfunc, 0, 24);
578 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
579 IEEE80211_FCTL_TODS);
580 if (powersave)
581 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
582 nullfunc->frame_control = fc;
583 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
584 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
585 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
587 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
588 ieee80211_tx_skb(sdata, skb);
591 /* spectrum management related things */
592 static void ieee80211_chswitch_work(struct work_struct *work)
594 struct ieee80211_sub_if_data *sdata =
595 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
596 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
598 if (!netif_running(sdata->dev))
599 return;
601 mutex_lock(&ifmgd->mtx);
602 if (!ifmgd->associated)
603 goto out;
605 sdata->local->oper_channel = sdata->local->csa_channel;
606 ieee80211_hw_config(sdata->local, IEEE80211_CONF_CHANGE_CHANNEL);
608 /* XXX: shouldn't really modify cfg80211-owned data! */
609 ifmgd->associated->cbss.channel = sdata->local->oper_channel;
611 ieee80211_wake_queues_by_reason(&sdata->local->hw,
612 IEEE80211_QUEUE_STOP_REASON_CSA);
613 out:
614 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
615 mutex_unlock(&ifmgd->mtx);
618 static void ieee80211_chswitch_timer(unsigned long data)
620 struct ieee80211_sub_if_data *sdata =
621 (struct ieee80211_sub_if_data *) data;
622 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
624 if (sdata->local->quiescing) {
625 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
626 return;
629 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
632 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
633 struct ieee80211_channel_sw_ie *sw_elem,
634 struct ieee80211_bss *bss)
636 struct ieee80211_channel *new_ch;
637 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
638 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
640 ASSERT_MGD_MTX(ifmgd);
642 if (!ifmgd->associated)
643 return;
645 if (sdata->local->scanning)
646 return;
648 /* Disregard subsequent beacons if we are already running a timer
649 processing a CSA */
651 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
652 return;
654 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
655 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
656 return;
658 sdata->local->csa_channel = new_ch;
660 if (sw_elem->count <= 1) {
661 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
662 } else {
663 ieee80211_stop_queues_by_reason(&sdata->local->hw,
664 IEEE80211_QUEUE_STOP_REASON_CSA);
665 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
666 mod_timer(&ifmgd->chswitch_timer,
667 jiffies +
668 msecs_to_jiffies(sw_elem->count *
669 bss->cbss.beacon_interval));
673 static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
674 u16 capab_info, u8 *pwr_constr_elem,
675 u8 pwr_constr_elem_len)
677 struct ieee80211_conf *conf = &sdata->local->hw.conf;
679 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
680 return;
682 /* Power constraint IE length should be 1 octet */
683 if (pwr_constr_elem_len != 1)
684 return;
686 if ((*pwr_constr_elem <= conf->channel->max_power) &&
687 (*pwr_constr_elem != sdata->local->power_constr_level)) {
688 sdata->local->power_constr_level = *pwr_constr_elem;
689 ieee80211_hw_config(sdata->local, 0);
693 /* powersave */
694 static void ieee80211_enable_ps(struct ieee80211_local *local,
695 struct ieee80211_sub_if_data *sdata)
697 struct ieee80211_conf *conf = &local->hw.conf;
700 * If we are scanning right now then the parameters will
701 * take effect when scan finishes.
703 if (local->scanning)
704 return;
706 if (conf->dynamic_ps_timeout > 0 &&
707 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
708 mod_timer(&local->dynamic_ps_timer, jiffies +
709 msecs_to_jiffies(conf->dynamic_ps_timeout));
710 } else {
711 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
712 ieee80211_send_nullfunc(local, sdata, 1);
713 conf->flags |= IEEE80211_CONF_PS;
714 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
718 static void ieee80211_change_ps(struct ieee80211_local *local)
720 struct ieee80211_conf *conf = &local->hw.conf;
722 if (local->ps_sdata) {
723 ieee80211_enable_ps(local, local->ps_sdata);
724 } else if (conf->flags & IEEE80211_CONF_PS) {
725 conf->flags &= ~IEEE80211_CONF_PS;
726 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
727 del_timer_sync(&local->dynamic_ps_timer);
728 cancel_work_sync(&local->dynamic_ps_enable_work);
732 /* need to hold RTNL or interface lock */
733 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
735 struct ieee80211_sub_if_data *sdata, *found = NULL;
736 int count = 0;
738 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
739 local->ps_sdata = NULL;
740 return;
743 list_for_each_entry(sdata, &local->interfaces, list) {
744 if (!netif_running(sdata->dev))
745 continue;
746 if (sdata->vif.type != NL80211_IFTYPE_STATION)
747 continue;
748 found = sdata;
749 count++;
752 if (count == 1 && found->u.mgd.powersave &&
753 found->u.mgd.associated && list_empty(&found->u.mgd.work_list) &&
754 !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
755 IEEE80211_STA_CONNECTION_POLL))) {
756 s32 beaconint_us;
758 if (latency < 0)
759 latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
761 beaconint_us = ieee80211_tu_to_usec(
762 found->vif.bss_conf.beacon_int);
764 if (beaconint_us > latency) {
765 local->ps_sdata = NULL;
766 } else {
767 u8 dtimper = found->vif.bss_conf.dtim_period;
768 int maxslp = 1;
770 if (dtimper > 1)
771 maxslp = min_t(int, dtimper,
772 latency / beaconint_us);
774 local->hw.conf.max_sleep_period = maxslp;
775 local->ps_sdata = found;
777 } else {
778 local->ps_sdata = NULL;
781 ieee80211_change_ps(local);
784 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
786 struct ieee80211_local *local =
787 container_of(work, struct ieee80211_local,
788 dynamic_ps_disable_work);
790 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
791 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
792 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
795 ieee80211_wake_queues_by_reason(&local->hw,
796 IEEE80211_QUEUE_STOP_REASON_PS);
799 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
801 struct ieee80211_local *local =
802 container_of(work, struct ieee80211_local,
803 dynamic_ps_enable_work);
804 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
806 /* can only happen when PS was just disabled anyway */
807 if (!sdata)
808 return;
810 if (local->hw.conf.flags & IEEE80211_CONF_PS)
811 return;
813 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
814 ieee80211_send_nullfunc(local, sdata, 1);
816 local->hw.conf.flags |= IEEE80211_CONF_PS;
817 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
820 void ieee80211_dynamic_ps_timer(unsigned long data)
822 struct ieee80211_local *local = (void *) data;
824 if (local->quiescing || local->suspended)
825 return;
827 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
830 /* MLME */
831 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
832 struct ieee80211_if_managed *ifmgd,
833 u8 *wmm_param, size_t wmm_param_len)
835 struct ieee80211_tx_queue_params params;
836 size_t left;
837 int count;
838 u8 *pos;
840 if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
841 return;
843 if (!wmm_param)
844 return;
846 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
847 return;
848 count = wmm_param[6] & 0x0f;
849 if (count == ifmgd->wmm_last_param_set)
850 return;
851 ifmgd->wmm_last_param_set = count;
853 pos = wmm_param + 8;
854 left = wmm_param_len - 8;
856 memset(&params, 0, sizeof(params));
858 local->wmm_acm = 0;
859 for (; left >= 4; left -= 4, pos += 4) {
860 int aci = (pos[0] >> 5) & 0x03;
861 int acm = (pos[0] >> 4) & 0x01;
862 int queue;
864 switch (aci) {
865 case 1: /* AC_BK */
866 queue = 3;
867 if (acm)
868 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
869 break;
870 case 2: /* AC_VI */
871 queue = 1;
872 if (acm)
873 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
874 break;
875 case 3: /* AC_VO */
876 queue = 0;
877 if (acm)
878 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
879 break;
880 case 0: /* AC_BE */
881 default:
882 queue = 2;
883 if (acm)
884 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
885 break;
888 params.aifs = pos[0] & 0x0f;
889 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
890 params.cw_min = ecw2cw(pos[1] & 0x0f);
891 params.txop = get_unaligned_le16(pos + 2);
892 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
893 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
894 "cWmin=%d cWmax=%d txop=%d\n",
895 wiphy_name(local->hw.wiphy), queue, aci, acm,
896 params.aifs, params.cw_min, params.cw_max, params.txop);
897 #endif
898 if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
899 printk(KERN_DEBUG "%s: failed to set TX queue "
900 "parameters for queue %d\n",
901 wiphy_name(local->hw.wiphy), queue);
905 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
906 u16 capab, bool erp_valid, u8 erp)
908 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
909 u32 changed = 0;
910 bool use_protection;
911 bool use_short_preamble;
912 bool use_short_slot;
914 if (erp_valid) {
915 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
916 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
917 } else {
918 use_protection = false;
919 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
922 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
924 if (use_protection != bss_conf->use_cts_prot) {
925 bss_conf->use_cts_prot = use_protection;
926 changed |= BSS_CHANGED_ERP_CTS_PROT;
929 if (use_short_preamble != bss_conf->use_short_preamble) {
930 bss_conf->use_short_preamble = use_short_preamble;
931 changed |= BSS_CHANGED_ERP_PREAMBLE;
934 if (use_short_slot != bss_conf->use_short_slot) {
935 bss_conf->use_short_slot = use_short_slot;
936 changed |= BSS_CHANGED_ERP_SLOT;
939 return changed;
942 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
943 struct ieee80211_mgd_work *wk,
944 u32 bss_info_changed)
946 struct ieee80211_local *local = sdata->local;
947 struct ieee80211_bss *bss = wk->bss;
949 bss_info_changed |= BSS_CHANGED_ASSOC;
950 /* set timing information */
951 sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
952 sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
953 sdata->vif.bss_conf.dtim_period = bss->dtim_period;
955 bss_info_changed |= BSS_CHANGED_BEACON_INT;
956 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
957 bss->cbss.capability, bss->has_erp_value, bss->erp_value);
959 sdata->u.mgd.associated = bss;
960 sdata->u.mgd.old_associate_work = wk;
961 memcpy(sdata->u.mgd.bssid, bss->cbss.bssid, ETH_ALEN);
963 /* just to be sure */
964 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
965 IEEE80211_STA_BEACON_POLL);
967 ieee80211_led_assoc(local, 1);
969 sdata->vif.bss_conf.assoc = 1;
971 * For now just always ask the driver to update the basic rateset
972 * when we have associated, we aren't checking whether it actually
973 * changed or not.
975 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
977 /* And the BSSID changed - we're associated now */
978 bss_info_changed |= BSS_CHANGED_BSSID;
980 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
982 mutex_lock(&local->iflist_mtx);
983 ieee80211_recalc_ps(local, -1);
984 ieee80211_recalc_smps(local, sdata);
985 mutex_unlock(&local->iflist_mtx);
987 netif_start_queue(sdata->dev);
988 netif_carrier_on(sdata->dev);
991 static enum rx_mgmt_action __must_check
992 ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
993 struct ieee80211_mgd_work *wk)
995 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
996 struct ieee80211_local *local = sdata->local;
998 wk->tries++;
999 if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
1000 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
1001 sdata->name, wk->bss->cbss.bssid);
1004 * Most likely AP is not in the range so remove the
1005 * bss struct for that AP.
1007 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
1010 * We might have a pending scan which had no chance to run yet
1011 * due to work needing to be done. Hence, queue the STAs work
1012 * again for that.
1014 ieee80211_queue_work(&local->hw, &ifmgd->work);
1015 return RX_MGMT_CFG80211_AUTH_TO;
1018 printk(KERN_DEBUG "%s: direct probe to AP %pM (try %d)\n",
1019 sdata->name, wk->bss->cbss.bssid,
1020 wk->tries);
1023 * Direct probe is sent to broadcast address as some APs
1024 * will not answer to direct packet in unassociated state.
1026 ieee80211_send_probe_req(sdata, NULL, wk->ssid, wk->ssid_len, NULL, 0);
1028 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
1029 run_again(ifmgd, wk->timeout);
1031 return RX_MGMT_NONE;
1035 static enum rx_mgmt_action __must_check
1036 ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
1037 struct ieee80211_mgd_work *wk)
1039 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1040 struct ieee80211_local *local = sdata->local;
1042 wk->tries++;
1043 if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
1044 printk(KERN_DEBUG "%s: authentication with AP %pM"
1045 " timed out\n",
1046 sdata->name, wk->bss->cbss.bssid);
1049 * Most likely AP is not in the range so remove the
1050 * bss struct for that AP.
1052 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
1055 * We might have a pending scan which had no chance to run yet
1056 * due to work needing to be done. Hence, queue the STAs work
1057 * again for that.
1059 ieee80211_queue_work(&local->hw, &ifmgd->work);
1060 return RX_MGMT_CFG80211_AUTH_TO;
1063 printk(KERN_DEBUG "%s: authenticate with AP %pM (try %d)\n",
1064 sdata->name, wk->bss->cbss.bssid, wk->tries);
1066 ieee80211_send_auth(sdata, 1, wk->auth_alg, wk->ie, wk->ie_len,
1067 wk->bss->cbss.bssid, NULL, 0, 0);
1068 wk->auth_transaction = 2;
1070 wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
1071 run_again(ifmgd, wk->timeout);
1073 return RX_MGMT_NONE;
1076 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1077 bool deauth)
1079 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1080 struct ieee80211_local *local = sdata->local;
1081 struct sta_info *sta;
1082 u32 changed = 0, config_changed = 0;
1083 u8 bssid[ETH_ALEN];
1085 ASSERT_MGD_MTX(ifmgd);
1087 if (WARN_ON(!ifmgd->associated))
1088 return;
1090 memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
1092 ifmgd->associated = NULL;
1093 memset(ifmgd->bssid, 0, ETH_ALEN);
1095 if (deauth) {
1096 kfree(ifmgd->old_associate_work);
1097 ifmgd->old_associate_work = NULL;
1098 } else {
1099 struct ieee80211_mgd_work *wk = ifmgd->old_associate_work;
1101 wk->state = IEEE80211_MGD_STATE_IDLE;
1102 list_add(&wk->list, &ifmgd->work_list);
1106 * we need to commit the associated = NULL change because the
1107 * scan code uses that to determine whether this iface should
1108 * go to/wake up from powersave or not -- and could otherwise
1109 * wake the queues erroneously.
1111 smp_mb();
1114 * Thus, we can only afterwards stop the queues -- to account
1115 * for the case where another CPU is finishing a scan at this
1116 * time -- we don't want the scan code to enable queues.
1119 netif_stop_queue(sdata->dev);
1120 netif_carrier_off(sdata->dev);
1122 rcu_read_lock();
1123 sta = sta_info_get(sdata, bssid);
1124 if (sta)
1125 ieee80211_sta_tear_down_BA_sessions(sta);
1126 rcu_read_unlock();
1128 changed |= ieee80211_reset_erp_info(sdata);
1130 ieee80211_led_assoc(local, 0);
1131 changed |= BSS_CHANGED_ASSOC;
1132 sdata->vif.bss_conf.assoc = false;
1134 ieee80211_set_wmm_default(sdata);
1136 ieee80211_recalc_idle(local);
1138 /* channel(_type) changes are handled by ieee80211_hw_config */
1139 local->oper_channel_type = NL80211_CHAN_NO_HT;
1141 /* on the next assoc, re-program HT parameters */
1142 sdata->ht_opmode_valid = false;
1144 local->power_constr_level = 0;
1146 del_timer_sync(&local->dynamic_ps_timer);
1147 cancel_work_sync(&local->dynamic_ps_enable_work);
1149 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1150 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1151 config_changed |= IEEE80211_CONF_CHANGE_PS;
1154 ieee80211_hw_config(local, config_changed);
1156 /* And the BSSID changed -- not very interesting here */
1157 changed |= BSS_CHANGED_BSSID;
1158 ieee80211_bss_info_change_notify(sdata, changed);
1160 rcu_read_lock();
1162 sta = sta_info_get(sdata, bssid);
1163 if (!sta) {
1164 rcu_read_unlock();
1165 return;
1168 sta_info_unlink(&sta);
1170 rcu_read_unlock();
1172 sta_info_destroy(sta);
1175 static enum rx_mgmt_action __must_check
1176 ieee80211_associate(struct ieee80211_sub_if_data *sdata,
1177 struct ieee80211_mgd_work *wk)
1179 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1180 struct ieee80211_local *local = sdata->local;
1182 wk->tries++;
1183 if (wk->tries > IEEE80211_ASSOC_MAX_TRIES) {
1184 printk(KERN_DEBUG "%s: association with AP %pM"
1185 " timed out\n",
1186 sdata->name, wk->bss->cbss.bssid);
1189 * Most likely AP is not in the range so remove the
1190 * bss struct for that AP.
1192 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
1195 * We might have a pending scan which had no chance to run yet
1196 * due to work needing to be done. Hence, queue the STAs work
1197 * again for that.
1199 ieee80211_queue_work(&local->hw, &ifmgd->work);
1200 return RX_MGMT_CFG80211_ASSOC_TO;
1203 printk(KERN_DEBUG "%s: associate with AP %pM (try %d)\n",
1204 sdata->name, wk->bss->cbss.bssid, wk->tries);
1205 ieee80211_send_assoc(sdata, wk);
1207 wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
1208 run_again(ifmgd, wk->timeout);
1210 return RX_MGMT_NONE;
1213 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1214 struct ieee80211_hdr *hdr)
1217 * We can postpone the mgd.timer whenever receiving unicast frames
1218 * from AP because we know that the connection is working both ways
1219 * at that time. But multicast frames (and hence also beacons) must
1220 * be ignored here, because we need to trigger the timer during
1221 * data idle periods for sending the periodic probe request to the
1222 * AP we're connected to.
1224 if (is_multicast_ether_addr(hdr->addr1))
1225 return;
1227 mod_timer(&sdata->u.mgd.conn_mon_timer,
1228 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
1231 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1233 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1234 const u8 *ssid;
1236 ssid = ieee80211_bss_get_ie(&ifmgd->associated->cbss, WLAN_EID_SSID);
1237 ieee80211_send_probe_req(sdata, ifmgd->associated->cbss.bssid,
1238 ssid + 2, ssid[1], NULL, 0);
1240 ifmgd->probe_send_count++;
1241 ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1242 run_again(ifmgd, ifmgd->probe_timeout);
1245 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1246 bool beacon)
1248 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1249 bool already = false;
1251 if (!netif_running(sdata->dev))
1252 return;
1254 if (sdata->local->scanning)
1255 return;
1257 mutex_lock(&ifmgd->mtx);
1259 if (!ifmgd->associated)
1260 goto out;
1262 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1263 if (beacon && net_ratelimit())
1264 printk(KERN_DEBUG "%s: detected beacon loss from AP "
1265 "- sending probe request\n", sdata->name);
1266 #endif
1269 * The driver/our work has already reported this event or the
1270 * connection monitoring has kicked in and we have already sent
1271 * a probe request. Or maybe the AP died and the driver keeps
1272 * reporting until we disassociate...
1274 * In either case we have to ignore the current call to this
1275 * function (except for setting the correct probe reason bit)
1276 * because otherwise we would reset the timer every time and
1277 * never check whether we received a probe response!
1279 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1280 IEEE80211_STA_CONNECTION_POLL))
1281 already = true;
1283 if (beacon)
1284 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1285 else
1286 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1288 if (already)
1289 goto out;
1291 mutex_lock(&sdata->local->iflist_mtx);
1292 ieee80211_recalc_ps(sdata->local, -1);
1293 mutex_unlock(&sdata->local->iflist_mtx);
1295 ifmgd->probe_send_count = 0;
1296 ieee80211_mgd_probe_ap_send(sdata);
1297 out:
1298 mutex_unlock(&ifmgd->mtx);
1301 void ieee80211_beacon_loss_work(struct work_struct *work)
1303 struct ieee80211_sub_if_data *sdata =
1304 container_of(work, struct ieee80211_sub_if_data,
1305 u.mgd.beacon_loss_work);
1307 ieee80211_mgd_probe_ap(sdata, true);
1310 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1312 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1314 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
1316 EXPORT_SYMBOL(ieee80211_beacon_loss);
1318 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
1319 struct ieee80211_mgd_work *wk)
1321 wk->state = IEEE80211_MGD_STATE_IDLE;
1322 printk(KERN_DEBUG "%s: authenticated\n", sdata->name);
1326 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1327 struct ieee80211_mgd_work *wk,
1328 struct ieee80211_mgmt *mgmt,
1329 size_t len)
1331 u8 *pos;
1332 struct ieee802_11_elems elems;
1334 pos = mgmt->u.auth.variable;
1335 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1336 if (!elems.challenge)
1337 return;
1338 ieee80211_send_auth(sdata, 3, wk->auth_alg,
1339 elems.challenge - 2, elems.challenge_len + 2,
1340 wk->bss->cbss.bssid,
1341 wk->key, wk->key_len, wk->key_idx);
1342 wk->auth_transaction = 4;
1345 static enum rx_mgmt_action __must_check
1346 ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1347 struct ieee80211_mgd_work *wk,
1348 struct ieee80211_mgmt *mgmt, size_t len)
1350 u16 auth_alg, auth_transaction, status_code;
1352 if (wk->state != IEEE80211_MGD_STATE_AUTH)
1353 return RX_MGMT_NONE;
1355 if (len < 24 + 6)
1356 return RX_MGMT_NONE;
1358 if (memcmp(wk->bss->cbss.bssid, mgmt->sa, ETH_ALEN) != 0)
1359 return RX_MGMT_NONE;
1361 if (memcmp(wk->bss->cbss.bssid, mgmt->bssid, ETH_ALEN) != 0)
1362 return RX_MGMT_NONE;
1364 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1365 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1366 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1368 if (auth_alg != wk->auth_alg ||
1369 auth_transaction != wk->auth_transaction)
1370 return RX_MGMT_NONE;
1372 if (status_code != WLAN_STATUS_SUCCESS) {
1373 list_del(&wk->list);
1374 kfree(wk);
1375 return RX_MGMT_CFG80211_AUTH;
1378 switch (wk->auth_alg) {
1379 case WLAN_AUTH_OPEN:
1380 case WLAN_AUTH_LEAP:
1381 case WLAN_AUTH_FT:
1382 ieee80211_auth_completed(sdata, wk);
1383 return RX_MGMT_CFG80211_AUTH;
1384 case WLAN_AUTH_SHARED_KEY:
1385 if (wk->auth_transaction == 4) {
1386 ieee80211_auth_completed(sdata, wk);
1387 return RX_MGMT_CFG80211_AUTH;
1388 } else
1389 ieee80211_auth_challenge(sdata, wk, mgmt, len);
1390 break;
1393 return RX_MGMT_NONE;
1397 static enum rx_mgmt_action __must_check
1398 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1399 struct ieee80211_mgd_work *wk,
1400 struct ieee80211_mgmt *mgmt, size_t len)
1402 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1403 const u8 *bssid = NULL;
1404 u16 reason_code;
1406 if (len < 24 + 2)
1407 return RX_MGMT_NONE;
1409 ASSERT_MGD_MTX(ifmgd);
1411 if (wk)
1412 bssid = wk->bss->cbss.bssid;
1413 else
1414 bssid = ifmgd->associated->cbss.bssid;
1416 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1418 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
1419 sdata->name, bssid, reason_code);
1421 if (!wk) {
1422 ieee80211_set_disassoc(sdata, true);
1423 } else {
1424 list_del(&wk->list);
1425 kfree(wk);
1428 return RX_MGMT_CFG80211_DEAUTH;
1432 static enum rx_mgmt_action __must_check
1433 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1434 struct ieee80211_mgmt *mgmt, size_t len)
1436 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1437 u16 reason_code;
1439 if (len < 24 + 2)
1440 return RX_MGMT_NONE;
1442 ASSERT_MGD_MTX(ifmgd);
1444 if (WARN_ON(!ifmgd->associated))
1445 return RX_MGMT_NONE;
1447 if (WARN_ON(memcmp(ifmgd->associated->cbss.bssid, mgmt->sa, ETH_ALEN)))
1448 return RX_MGMT_NONE;
1450 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1452 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
1453 sdata->name, mgmt->sa, reason_code);
1455 ieee80211_set_disassoc(sdata, false);
1456 return RX_MGMT_CFG80211_DISASSOC;
1460 static enum rx_mgmt_action __must_check
1461 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1462 struct ieee80211_mgd_work *wk,
1463 struct ieee80211_mgmt *mgmt, size_t len,
1464 bool reassoc)
1466 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1467 struct ieee80211_local *local = sdata->local;
1468 struct ieee80211_supported_band *sband;
1469 struct sta_info *sta;
1470 u32 rates, basic_rates;
1471 u16 capab_info, status_code, aid;
1472 struct ieee802_11_elems elems;
1473 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1474 u8 *pos;
1475 u32 changed = 0;
1476 int i, j;
1477 bool have_higher_than_11mbit = false, newsta = false;
1478 u16 ap_ht_cap_flags;
1481 * AssocResp and ReassocResp have identical structure, so process both
1482 * of them in this function.
1485 if (len < 24 + 6)
1486 return RX_MGMT_NONE;
1488 if (memcmp(wk->bss->cbss.bssid, mgmt->sa, ETH_ALEN) != 0)
1489 return RX_MGMT_NONE;
1491 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1492 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1493 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1495 printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
1496 "status=%d aid=%d)\n",
1497 sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
1498 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1500 pos = mgmt->u.assoc_resp.variable;
1501 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1503 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
1504 elems.timeout_int && elems.timeout_int_len == 5 &&
1505 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
1506 u32 tu, ms;
1507 tu = get_unaligned_le32(elems.timeout_int + 1);
1508 ms = tu * 1024 / 1000;
1509 printk(KERN_DEBUG "%s: AP rejected association temporarily; "
1510 "comeback duration %u TU (%u ms)\n",
1511 sdata->name, tu, ms);
1512 wk->timeout = jiffies + msecs_to_jiffies(ms);
1513 if (ms > IEEE80211_ASSOC_TIMEOUT)
1514 run_again(ifmgd, jiffies + msecs_to_jiffies(ms));
1515 return RX_MGMT_NONE;
1518 if (status_code != WLAN_STATUS_SUCCESS) {
1519 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1520 sdata->name, status_code);
1521 wk->state = IEEE80211_MGD_STATE_IDLE;
1522 return RX_MGMT_CFG80211_ASSOC;
1525 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1526 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1527 "set\n", sdata->name, aid);
1528 aid &= ~(BIT(15) | BIT(14));
1530 if (!elems.supp_rates) {
1531 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1532 sdata->name);
1533 return RX_MGMT_NONE;
1536 printk(KERN_DEBUG "%s: associated\n", sdata->name);
1537 ifmgd->aid = aid;
1539 rcu_read_lock();
1541 /* Add STA entry for the AP */
1542 sta = sta_info_get(sdata, wk->bss->cbss.bssid);
1543 if (!sta) {
1544 newsta = true;
1546 rcu_read_unlock();
1548 sta = sta_info_alloc(sdata, wk->bss->cbss.bssid, GFP_KERNEL);
1549 if (!sta) {
1550 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1551 " the AP\n", sdata->name);
1552 return RX_MGMT_NONE;
1555 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1556 WLAN_STA_ASSOC_AP);
1557 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1558 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1560 rcu_read_lock();
1563 rates = 0;
1564 basic_rates = 0;
1565 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1567 for (i = 0; i < elems.supp_rates_len; i++) {
1568 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1569 bool is_basic = !!(elems.supp_rates[i] & 0x80);
1571 if (rate > 110)
1572 have_higher_than_11mbit = true;
1574 for (j = 0; j < sband->n_bitrates; j++) {
1575 if (sband->bitrates[j].bitrate == rate) {
1576 rates |= BIT(j);
1577 if (is_basic)
1578 basic_rates |= BIT(j);
1579 break;
1584 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1585 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1586 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
1588 if (rate > 110)
1589 have_higher_than_11mbit = true;
1591 for (j = 0; j < sband->n_bitrates; j++) {
1592 if (sband->bitrates[j].bitrate == rate) {
1593 rates |= BIT(j);
1594 if (is_basic)
1595 basic_rates |= BIT(j);
1596 break;
1601 sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
1602 sdata->vif.bss_conf.basic_rates = basic_rates;
1604 /* cf. IEEE 802.11 9.2.12 */
1605 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1606 have_higher_than_11mbit)
1607 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1608 else
1609 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1611 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1612 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1613 elems.ht_cap_elem, &sta->sta.ht_cap);
1615 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1617 rate_control_rate_init(sta);
1619 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
1620 set_sta_flags(sta, WLAN_STA_MFP);
1622 if (elems.wmm_param)
1623 set_sta_flags(sta, WLAN_STA_WME);
1625 if (newsta) {
1626 int err = sta_info_insert(sta);
1627 if (err) {
1628 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1629 " the AP (error %d)\n", sdata->name, err);
1630 rcu_read_unlock();
1631 return RX_MGMT_NONE;
1635 rcu_read_unlock();
1637 if (elems.wmm_param)
1638 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1639 elems.wmm_param_len);
1640 else
1641 ieee80211_set_wmm_default(sdata);
1643 if (elems.ht_info_elem && elems.wmm_param &&
1644 (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
1645 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1646 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1647 wk->bss->cbss.bssid,
1648 ap_ht_cap_flags);
1650 /* delete work item -- must be before set_associated for PS */
1651 list_del(&wk->list);
1653 /* set AID and assoc capability,
1654 * ieee80211_set_associated() will tell the driver */
1655 bss_conf->aid = aid;
1656 bss_conf->assoc_capability = capab_info;
1657 /* this will take ownership of wk */
1658 ieee80211_set_associated(sdata, wk, changed);
1661 * Start timer to probe the connection to the AP now.
1662 * Also start the timer that will detect beacon loss.
1664 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
1665 mod_beacon_timer(sdata);
1667 return RX_MGMT_CFG80211_ASSOC;
1671 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1672 struct ieee80211_mgmt *mgmt,
1673 size_t len,
1674 struct ieee80211_rx_status *rx_status,
1675 struct ieee802_11_elems *elems,
1676 bool beacon)
1678 struct ieee80211_local *local = sdata->local;
1679 int freq;
1680 struct ieee80211_bss *bss;
1681 struct ieee80211_channel *channel;
1683 if (elems->ds_params && elems->ds_params_len == 1)
1684 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1685 else
1686 freq = rx_status->freq;
1688 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1690 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1691 return;
1693 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1694 channel, beacon);
1695 if (bss)
1696 ieee80211_rx_bss_put(local, bss);
1698 if (!sdata->u.mgd.associated)
1699 return;
1701 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
1702 (memcmp(mgmt->bssid, sdata->u.mgd.associated->cbss.bssid,
1703 ETH_ALEN) == 0)) {
1704 struct ieee80211_channel_sw_ie *sw_elem =
1705 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
1706 ieee80211_sta_process_chanswitch(sdata, sw_elem, bss);
1711 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1712 struct ieee80211_mgd_work *wk,
1713 struct ieee80211_mgmt *mgmt, size_t len,
1714 struct ieee80211_rx_status *rx_status)
1716 struct ieee80211_if_managed *ifmgd;
1717 size_t baselen;
1718 struct ieee802_11_elems elems;
1720 ifmgd = &sdata->u.mgd;
1722 ASSERT_MGD_MTX(ifmgd);
1724 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
1725 return; /* ignore ProbeResp to foreign address */
1727 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1728 if (baselen > len)
1729 return;
1731 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1732 &elems);
1734 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1736 /* direct probe may be part of the association flow */
1737 if (wk && wk->state == IEEE80211_MGD_STATE_PROBE) {
1738 printk(KERN_DEBUG "%s: direct probe responded\n",
1739 sdata->name);
1740 wk->tries = 0;
1741 wk->state = IEEE80211_MGD_STATE_AUTH;
1742 WARN_ON(ieee80211_authenticate(sdata, wk) != RX_MGMT_NONE);
1745 if (ifmgd->associated &&
1746 memcmp(mgmt->bssid, ifmgd->associated->cbss.bssid, ETH_ALEN) == 0 &&
1747 ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1748 IEEE80211_STA_CONNECTION_POLL)) {
1749 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1750 IEEE80211_STA_BEACON_POLL);
1751 mutex_lock(&sdata->local->iflist_mtx);
1752 ieee80211_recalc_ps(sdata->local, -1);
1753 mutex_unlock(&sdata->local->iflist_mtx);
1755 * We've received a probe response, but are not sure whether
1756 * we have or will be receiving any beacons or data, so let's
1757 * schedule the timers again, just in case.
1759 mod_beacon_timer(sdata);
1760 mod_timer(&ifmgd->conn_mon_timer,
1761 round_jiffies_up(jiffies +
1762 IEEE80211_CONNECTION_IDLE_TIME));
1767 * This is the canonical list of information elements we care about,
1768 * the filter code also gives us all changes to the Microsoft OUI
1769 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1771 * We implement beacon filtering in software since that means we can
1772 * avoid processing the frame here and in cfg80211, and userspace
1773 * will not be able to tell whether the hardware supports it or not.
1775 * XXX: This list needs to be dynamic -- userspace needs to be able to
1776 * add items it requires. It also needs to be able to tell us to
1777 * look out for other vendor IEs.
1779 static const u64 care_about_ies =
1780 (1ULL << WLAN_EID_COUNTRY) |
1781 (1ULL << WLAN_EID_ERP_INFO) |
1782 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1783 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1784 (1ULL << WLAN_EID_HT_CAPABILITY) |
1785 (1ULL << WLAN_EID_HT_INFORMATION);
1787 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1788 struct ieee80211_mgmt *mgmt,
1789 size_t len,
1790 struct ieee80211_rx_status *rx_status)
1792 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1793 size_t baselen;
1794 struct ieee802_11_elems elems;
1795 struct ieee80211_local *local = sdata->local;
1796 u32 changed = 0;
1797 bool erp_valid, directed_tim = false;
1798 u8 erp_value = 0;
1799 u32 ncrc;
1800 u8 *bssid;
1802 ASSERT_MGD_MTX(ifmgd);
1804 /* Process beacon from the current BSS */
1805 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1806 if (baselen > len)
1807 return;
1809 if (rx_status->freq != local->hw.conf.channel->center_freq)
1810 return;
1813 * We might have received a number of frames, among them a
1814 * disassoc frame and a beacon...
1816 if (!ifmgd->associated)
1817 return;
1819 bssid = ifmgd->associated->cbss.bssid;
1822 * And in theory even frames from a different AP we were just
1823 * associated to a split-second ago!
1825 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
1826 return;
1828 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
1829 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1830 if (net_ratelimit()) {
1831 printk(KERN_DEBUG "%s: cancelling probereq poll due "
1832 "to a received beacon\n", sdata->name);
1834 #endif
1835 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
1836 mutex_lock(&local->iflist_mtx);
1837 ieee80211_recalc_ps(local, -1);
1838 mutex_unlock(&local->iflist_mtx);
1842 * Push the beacon loss detection into the future since
1843 * we are processing a beacon from the AP just now.
1845 mod_beacon_timer(sdata);
1847 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1848 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1849 len - baselen, &elems,
1850 care_about_ies, ncrc);
1852 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1853 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1854 ifmgd->aid);
1856 if (ncrc != ifmgd->beacon_crc) {
1857 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1858 true);
1860 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1861 elems.wmm_param_len);
1864 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1865 if (directed_tim) {
1866 if (local->hw.conf.dynamic_ps_timeout > 0) {
1867 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1868 ieee80211_hw_config(local,
1869 IEEE80211_CONF_CHANGE_PS);
1870 ieee80211_send_nullfunc(local, sdata, 0);
1871 } else {
1872 local->pspolling = true;
1875 * Here is assumed that the driver will be
1876 * able to send ps-poll frame and receive a
1877 * response even though power save mode is
1878 * enabled, but some drivers might require
1879 * to disable power save here. This needs
1880 * to be investigated.
1882 ieee80211_send_pspoll(local, sdata);
1887 if (ncrc == ifmgd->beacon_crc)
1888 return;
1889 ifmgd->beacon_crc = ncrc;
1891 if (elems.erp_info && elems.erp_info_len >= 1) {
1892 erp_valid = true;
1893 erp_value = elems.erp_info[0];
1894 } else {
1895 erp_valid = false;
1897 changed |= ieee80211_handle_bss_capability(sdata,
1898 le16_to_cpu(mgmt->u.beacon.capab_info),
1899 erp_valid, erp_value);
1902 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1903 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
1904 struct sta_info *sta;
1905 struct ieee80211_supported_band *sband;
1906 u16 ap_ht_cap_flags;
1908 rcu_read_lock();
1910 sta = sta_info_get(sdata, bssid);
1911 if (WARN_ON(!sta)) {
1912 rcu_read_unlock();
1913 return;
1916 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1918 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1919 elems.ht_cap_elem, &sta->sta.ht_cap);
1921 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1923 rcu_read_unlock();
1925 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1926 bssid, ap_ht_cap_flags);
1929 /* Note: country IE parsing is done for us by cfg80211 */
1930 if (elems.country_elem) {
1931 /* TODO: IBSS also needs this */
1932 if (elems.pwr_constr_elem)
1933 ieee80211_handle_pwr_constr(sdata,
1934 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1935 elems.pwr_constr_elem,
1936 elems.pwr_constr_elem_len);
1939 ieee80211_bss_info_change_notify(sdata, changed);
1942 ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1943 struct sk_buff *skb)
1945 struct ieee80211_local *local = sdata->local;
1946 struct ieee80211_mgmt *mgmt;
1947 u16 fc;
1949 if (skb->len < 24)
1950 return RX_DROP_MONITOR;
1952 mgmt = (struct ieee80211_mgmt *) skb->data;
1953 fc = le16_to_cpu(mgmt->frame_control);
1955 switch (fc & IEEE80211_FCTL_STYPE) {
1956 case IEEE80211_STYPE_PROBE_RESP:
1957 case IEEE80211_STYPE_BEACON:
1958 case IEEE80211_STYPE_AUTH:
1959 case IEEE80211_STYPE_ASSOC_RESP:
1960 case IEEE80211_STYPE_REASSOC_RESP:
1961 case IEEE80211_STYPE_DEAUTH:
1962 case IEEE80211_STYPE_DISASSOC:
1963 case IEEE80211_STYPE_ACTION:
1964 skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
1965 ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
1966 return RX_QUEUED;
1969 return RX_DROP_MONITOR;
1972 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1973 struct sk_buff *skb)
1975 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1976 struct ieee80211_rx_status *rx_status;
1977 struct ieee80211_mgmt *mgmt;
1978 struct ieee80211_mgd_work *wk;
1979 enum rx_mgmt_action rma = RX_MGMT_NONE;
1980 u16 fc;
1982 rx_status = (struct ieee80211_rx_status *) skb->cb;
1983 mgmt = (struct ieee80211_mgmt *) skb->data;
1984 fc = le16_to_cpu(mgmt->frame_control);
1986 mutex_lock(&ifmgd->mtx);
1988 if (ifmgd->associated &&
1989 memcmp(ifmgd->associated->cbss.bssid, mgmt->bssid,
1990 ETH_ALEN) == 0) {
1991 switch (fc & IEEE80211_FCTL_STYPE) {
1992 case IEEE80211_STYPE_BEACON:
1993 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1994 rx_status);
1995 break;
1996 case IEEE80211_STYPE_PROBE_RESP:
1997 ieee80211_rx_mgmt_probe_resp(sdata, NULL, mgmt,
1998 skb->len, rx_status);
1999 break;
2000 case IEEE80211_STYPE_DEAUTH:
2001 rma = ieee80211_rx_mgmt_deauth(sdata, NULL,
2002 mgmt, skb->len);
2003 break;
2004 case IEEE80211_STYPE_DISASSOC:
2005 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2006 break;
2007 case IEEE80211_STYPE_ACTION:
2008 /* XXX: differentiate, can only happen for CSA now! */
2009 ieee80211_sta_process_chanswitch(sdata,
2010 &mgmt->u.action.u.chan_switch.sw_elem,
2011 ifmgd->associated);
2012 break;
2014 mutex_unlock(&ifmgd->mtx);
2016 switch (rma) {
2017 case RX_MGMT_NONE:
2018 /* no action */
2019 break;
2020 case RX_MGMT_CFG80211_DEAUTH:
2021 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
2022 break;
2023 case RX_MGMT_CFG80211_DISASSOC:
2024 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2025 break;
2026 default:
2027 WARN(1, "unexpected: %d", rma);
2029 goto out;
2032 list_for_each_entry(wk, &ifmgd->work_list, list) {
2033 if (memcmp(wk->bss->cbss.bssid, mgmt->bssid, ETH_ALEN) != 0)
2034 continue;
2036 switch (fc & IEEE80211_FCTL_STYPE) {
2037 case IEEE80211_STYPE_PROBE_RESP:
2038 ieee80211_rx_mgmt_probe_resp(sdata, wk, mgmt, skb->len,
2039 rx_status);
2040 break;
2041 case IEEE80211_STYPE_AUTH:
2042 rma = ieee80211_rx_mgmt_auth(sdata, wk, mgmt, skb->len);
2043 break;
2044 case IEEE80211_STYPE_ASSOC_RESP:
2045 rma = ieee80211_rx_mgmt_assoc_resp(sdata, wk, mgmt,
2046 skb->len, false);
2047 break;
2048 case IEEE80211_STYPE_REASSOC_RESP:
2049 rma = ieee80211_rx_mgmt_assoc_resp(sdata, wk, mgmt,
2050 skb->len, true);
2051 break;
2052 case IEEE80211_STYPE_DEAUTH:
2053 rma = ieee80211_rx_mgmt_deauth(sdata, wk, mgmt,
2054 skb->len);
2055 break;
2058 * We've processed this frame for that work, so it can't
2059 * belong to another work struct.
2060 * NB: this is also required for correctness because the
2061 * called functions can free 'wk', and for 'rma'!
2063 break;
2066 mutex_unlock(&ifmgd->mtx);
2068 switch (rma) {
2069 case RX_MGMT_NONE:
2070 /* no action */
2071 break;
2072 case RX_MGMT_CFG80211_AUTH:
2073 cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, skb->len);
2074 break;
2075 case RX_MGMT_CFG80211_ASSOC:
2076 cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, skb->len);
2077 break;
2078 case RX_MGMT_CFG80211_DEAUTH:
2079 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
2080 break;
2081 default:
2082 WARN(1, "unexpected: %d", rma);
2085 out:
2086 kfree_skb(skb);
2089 static void ieee80211_sta_timer(unsigned long data)
2091 struct ieee80211_sub_if_data *sdata =
2092 (struct ieee80211_sub_if_data *) data;
2093 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2094 struct ieee80211_local *local = sdata->local;
2096 if (local->quiescing) {
2097 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2098 return;
2101 ieee80211_queue_work(&local->hw, &ifmgd->work);
2104 static void ieee80211_sta_work(struct work_struct *work)
2106 struct ieee80211_sub_if_data *sdata =
2107 container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
2108 struct ieee80211_local *local = sdata->local;
2109 struct ieee80211_if_managed *ifmgd;
2110 struct sk_buff *skb;
2111 struct ieee80211_mgd_work *wk, *tmp;
2112 LIST_HEAD(free_work);
2113 enum rx_mgmt_action rma;
2114 bool anybusy = false;
2116 if (!netif_running(sdata->dev))
2117 return;
2119 if (local->scanning)
2120 return;
2122 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2123 return;
2126 * ieee80211_queue_work() should have picked up most cases,
2127 * here we'll pick the the rest.
2129 if (WARN(local->suspended, "STA MLME work scheduled while "
2130 "going to suspend\n"))
2131 return;
2133 ifmgd = &sdata->u.mgd;
2135 /* first process frames to avoid timing out while a frame is pending */
2136 while ((skb = skb_dequeue(&ifmgd->skb_queue)))
2137 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2139 /* then process the rest of the work */
2140 mutex_lock(&ifmgd->mtx);
2142 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2143 IEEE80211_STA_CONNECTION_POLL) &&
2144 ifmgd->associated) {
2145 u8 bssid[ETH_ALEN];
2147 memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
2148 if (time_is_after_jiffies(ifmgd->probe_timeout))
2149 run_again(ifmgd, ifmgd->probe_timeout);
2151 else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
2152 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2153 printk(KERN_DEBUG "No probe response from AP %pM"
2154 " after %dms, try %d\n", bssid,
2155 (1000 * IEEE80211_PROBE_WAIT)/HZ,
2156 ifmgd->probe_send_count);
2157 #endif
2158 ieee80211_mgd_probe_ap_send(sdata);
2159 } else {
2161 * We actually lost the connection ... or did we?
2162 * Let's make sure!
2164 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
2165 IEEE80211_STA_BEACON_POLL);
2166 printk(KERN_DEBUG "No probe response from AP %pM"
2167 " after %dms, disconnecting.\n",
2168 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
2169 ieee80211_set_disassoc(sdata, true);
2170 mutex_unlock(&ifmgd->mtx);
2172 * must be outside lock due to cfg80211,
2173 * but that's not a problem.
2175 ieee80211_send_deauth_disassoc(sdata, bssid,
2176 IEEE80211_STYPE_DEAUTH,
2177 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2178 NULL);
2179 mutex_lock(&ifmgd->mtx);
2184 ieee80211_recalc_idle(local);
2186 list_for_each_entry_safe(wk, tmp, &ifmgd->work_list, list) {
2187 if (time_is_after_jiffies(wk->timeout)) {
2189 * This work item isn't supposed to be worked on
2190 * right now, but take care to adjust the timer
2191 * properly.
2193 run_again(ifmgd, wk->timeout);
2194 continue;
2197 switch (wk->state) {
2198 default:
2199 WARN_ON(1);
2200 /* fall through */
2201 case IEEE80211_MGD_STATE_IDLE:
2202 /* nothing */
2203 rma = RX_MGMT_NONE;
2204 break;
2205 case IEEE80211_MGD_STATE_PROBE:
2206 rma = ieee80211_direct_probe(sdata, wk);
2207 break;
2208 case IEEE80211_MGD_STATE_AUTH:
2209 rma = ieee80211_authenticate(sdata, wk);
2210 break;
2211 case IEEE80211_MGD_STATE_ASSOC:
2212 rma = ieee80211_associate(sdata, wk);
2213 break;
2216 switch (rma) {
2217 case RX_MGMT_NONE:
2218 /* no action required */
2219 break;
2220 case RX_MGMT_CFG80211_AUTH_TO:
2221 case RX_MGMT_CFG80211_ASSOC_TO:
2222 list_del(&wk->list);
2223 list_add(&wk->list, &free_work);
2224 wk->tries = rma; /* small abuse but only local */
2225 break;
2226 default:
2227 WARN(1, "unexpected: %d", rma);
2231 list_for_each_entry(wk, &ifmgd->work_list, list) {
2232 if (wk->state != IEEE80211_MGD_STATE_IDLE) {
2233 anybusy = true;
2234 break;
2237 if (!anybusy &&
2238 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
2239 ieee80211_queue_delayed_work(&local->hw,
2240 &local->scan_work,
2241 round_jiffies_relative(0));
2243 mutex_unlock(&ifmgd->mtx);
2245 list_for_each_entry_safe(wk, tmp, &free_work, list) {
2246 switch (wk->tries) {
2247 case RX_MGMT_CFG80211_AUTH_TO:
2248 cfg80211_send_auth_timeout(sdata->dev,
2249 wk->bss->cbss.bssid);
2250 break;
2251 case RX_MGMT_CFG80211_ASSOC_TO:
2252 cfg80211_send_assoc_timeout(sdata->dev,
2253 wk->bss->cbss.bssid);
2254 break;
2255 default:
2256 WARN(1, "unexpected: %d", wk->tries);
2259 list_del(&wk->list);
2260 kfree(wk);
2263 ieee80211_recalc_idle(local);
2266 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2268 struct ieee80211_sub_if_data *sdata =
2269 (struct ieee80211_sub_if_data *) data;
2270 struct ieee80211_local *local = sdata->local;
2272 if (local->quiescing)
2273 return;
2275 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
2278 static void ieee80211_sta_conn_mon_timer(unsigned long data)
2280 struct ieee80211_sub_if_data *sdata =
2281 (struct ieee80211_sub_if_data *) data;
2282 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2283 struct ieee80211_local *local = sdata->local;
2285 if (local->quiescing)
2286 return;
2288 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
2291 static void ieee80211_sta_monitor_work(struct work_struct *work)
2293 struct ieee80211_sub_if_data *sdata =
2294 container_of(work, struct ieee80211_sub_if_data,
2295 u.mgd.monitor_work);
2297 ieee80211_mgd_probe_ap(sdata, false);
2300 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2302 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2303 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2304 IEEE80211_STA_CONNECTION_POLL);
2306 /* let's probe the connection once */
2307 ieee80211_queue_work(&sdata->local->hw,
2308 &sdata->u.mgd.monitor_work);
2309 /* and do all the other regular work too */
2310 ieee80211_queue_work(&sdata->local->hw,
2311 &sdata->u.mgd.work);
2315 #ifdef CONFIG_PM
2316 void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2318 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2321 * we need to use atomic bitops for the running bits
2322 * only because both timers might fire at the same
2323 * time -- the code here is properly synchronised.
2326 cancel_work_sync(&ifmgd->work);
2327 cancel_work_sync(&ifmgd->beacon_loss_work);
2328 if (del_timer_sync(&ifmgd->timer))
2329 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2331 cancel_work_sync(&ifmgd->chswitch_work);
2332 if (del_timer_sync(&ifmgd->chswitch_timer))
2333 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
2335 cancel_work_sync(&ifmgd->monitor_work);
2336 /* these will just be re-established on connection */
2337 del_timer_sync(&ifmgd->conn_mon_timer);
2338 del_timer_sync(&ifmgd->bcn_mon_timer);
2341 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2343 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2345 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2346 add_timer(&ifmgd->timer);
2347 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2348 add_timer(&ifmgd->chswitch_timer);
2350 #endif
2352 /* interface setup */
2353 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2355 struct ieee80211_if_managed *ifmgd;
2357 ifmgd = &sdata->u.mgd;
2358 INIT_WORK(&ifmgd->work, ieee80211_sta_work);
2359 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
2360 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
2361 INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
2362 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
2363 (unsigned long) sdata);
2364 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2365 (unsigned long) sdata);
2366 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2367 (unsigned long) sdata);
2368 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
2369 (unsigned long) sdata);
2370 skb_queue_head_init(&ifmgd->skb_queue);
2372 INIT_LIST_HEAD(&ifmgd->work_list);
2374 ifmgd->capab = WLAN_CAPABILITY_ESS;
2375 ifmgd->flags = 0;
2376 if (sdata->local->hw.queues >= 4)
2377 ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
2379 mutex_init(&ifmgd->mtx);
2381 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2382 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2383 else
2384 ifmgd->req_smps = IEEE80211_SMPS_OFF;
2387 /* scan finished notification */
2388 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2390 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2392 /* Restart STA timers */
2393 rcu_read_lock();
2394 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2395 ieee80211_restart_sta_timer(sdata);
2396 rcu_read_unlock();
2399 int ieee80211_max_network_latency(struct notifier_block *nb,
2400 unsigned long data, void *dummy)
2402 s32 latency_usec = (s32) data;
2403 struct ieee80211_local *local =
2404 container_of(nb, struct ieee80211_local,
2405 network_latency_notifier);
2407 mutex_lock(&local->iflist_mtx);
2408 ieee80211_recalc_ps(local, latency_usec);
2409 mutex_unlock(&local->iflist_mtx);
2411 return 0;
2414 /* config hooks */
2415 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2416 struct cfg80211_auth_request *req)
2418 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2419 const u8 *ssid;
2420 struct ieee80211_mgd_work *wk;
2421 u16 auth_alg;
2423 switch (req->auth_type) {
2424 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2425 auth_alg = WLAN_AUTH_OPEN;
2426 break;
2427 case NL80211_AUTHTYPE_SHARED_KEY:
2428 auth_alg = WLAN_AUTH_SHARED_KEY;
2429 break;
2430 case NL80211_AUTHTYPE_FT:
2431 auth_alg = WLAN_AUTH_FT;
2432 break;
2433 case NL80211_AUTHTYPE_NETWORK_EAP:
2434 auth_alg = WLAN_AUTH_LEAP;
2435 break;
2436 default:
2437 return -EOPNOTSUPP;
2440 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2441 if (!wk)
2442 return -ENOMEM;
2444 wk->bss = (void *)req->bss;
2446 if (req->ie && req->ie_len) {
2447 memcpy(wk->ie, req->ie, req->ie_len);
2448 wk->ie_len = req->ie_len;
2451 if (req->key && req->key_len) {
2452 wk->key_len = req->key_len;
2453 wk->key_idx = req->key_idx;
2454 memcpy(wk->key, req->key, req->key_len);
2457 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2458 memcpy(wk->ssid, ssid + 2, ssid[1]);
2459 wk->ssid_len = ssid[1];
2461 wk->state = IEEE80211_MGD_STATE_PROBE;
2462 wk->auth_alg = auth_alg;
2463 wk->timeout = jiffies; /* run right away */
2466 * XXX: if still associated need to tell AP that we're going
2467 * to sleep and then change channel etc.
2469 sdata->local->oper_channel = req->bss->channel;
2470 ieee80211_hw_config(sdata->local, 0);
2472 mutex_lock(&ifmgd->mtx);
2473 list_add(&wk->list, &sdata->u.mgd.work_list);
2474 mutex_unlock(&ifmgd->mtx);
2476 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
2477 return 0;
2480 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2481 struct cfg80211_assoc_request *req)
2483 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2484 struct ieee80211_mgd_work *wk, *found = NULL;
2485 int i, err;
2487 mutex_lock(&ifmgd->mtx);
2489 list_for_each_entry(wk, &ifmgd->work_list, list) {
2490 if (&wk->bss->cbss == req->bss &&
2491 wk->state == IEEE80211_MGD_STATE_IDLE) {
2492 found = wk;
2493 break;
2497 if (!found) {
2498 err = -ENOLINK;
2499 goto out;
2502 list_del(&found->list);
2504 wk = krealloc(found, sizeof(*wk) + req->ie_len, GFP_KERNEL);
2505 if (!wk) {
2506 list_add(&found->list, &ifmgd->work_list);
2507 err = -ENOMEM;
2508 goto out;
2511 list_add(&wk->list, &ifmgd->work_list);
2513 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
2515 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2516 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2517 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2518 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2519 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2521 sdata->local->oper_channel = req->bss->channel;
2522 ieee80211_hw_config(sdata->local, 0);
2524 if (req->ie && req->ie_len) {
2525 memcpy(wk->ie, req->ie, req->ie_len);
2526 wk->ie_len = req->ie_len;
2527 } else
2528 wk->ie_len = 0;
2530 if (req->prev_bssid)
2531 memcpy(wk->prev_bssid, req->prev_bssid, ETH_ALEN);
2533 wk->state = IEEE80211_MGD_STATE_ASSOC;
2534 wk->tries = 0;
2535 wk->timeout = jiffies; /* run right away */
2537 if (req->use_mfp) {
2538 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2539 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2540 } else {
2541 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2542 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2545 if (req->crypto.control_port)
2546 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2547 else
2548 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2550 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
2552 err = 0;
2554 out:
2555 mutex_unlock(&ifmgd->mtx);
2556 return err;
2559 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
2560 struct cfg80211_deauth_request *req,
2561 void *cookie)
2563 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2564 struct ieee80211_mgd_work *wk;
2565 const u8 *bssid = NULL;
2566 bool not_auth_yet = false;
2568 mutex_lock(&ifmgd->mtx);
2570 if (ifmgd->associated && &ifmgd->associated->cbss == req->bss) {
2571 bssid = req->bss->bssid;
2572 ieee80211_set_disassoc(sdata, true);
2573 } else list_for_each_entry(wk, &ifmgd->work_list, list) {
2574 if (&wk->bss->cbss == req->bss) {
2575 bssid = req->bss->bssid;
2576 if (wk->state == IEEE80211_MGD_STATE_PROBE)
2577 not_auth_yet = true;
2578 list_del(&wk->list);
2579 kfree(wk);
2580 break;
2585 * If somebody requests authentication and we haven't
2586 * sent out an auth frame yet there's no need to send
2587 * out a deauth frame either. If the state was PROBE,
2588 * then this is the case. If it's AUTH we have sent a
2589 * frame, and if it's IDLE we have completed the auth
2590 * process already.
2592 if (not_auth_yet) {
2593 mutex_unlock(&ifmgd->mtx);
2594 __cfg80211_auth_canceled(sdata->dev, bssid);
2595 return 0;
2599 * cfg80211 should catch this ... but it's racy since
2600 * we can receive a deauth frame, process it, hand it
2601 * to cfg80211 while that's in a locked section already
2602 * trying to tell us that the user wants to disconnect.
2604 if (!bssid) {
2605 mutex_unlock(&ifmgd->mtx);
2606 return -ENOLINK;
2609 mutex_unlock(&ifmgd->mtx);
2611 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
2612 sdata->name, bssid, req->reason_code);
2614 ieee80211_send_deauth_disassoc(sdata, bssid,
2615 IEEE80211_STYPE_DEAUTH, req->reason_code,
2616 cookie);
2618 return 0;
2621 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
2622 struct cfg80211_disassoc_request *req,
2623 void *cookie)
2625 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2627 mutex_lock(&ifmgd->mtx);
2630 * cfg80211 should catch this ... but it's racy since
2631 * we can receive a disassoc frame, process it, hand it
2632 * to cfg80211 while that's in a locked section already
2633 * trying to tell us that the user wants to disconnect.
2635 if (&ifmgd->associated->cbss != req->bss) {
2636 mutex_unlock(&ifmgd->mtx);
2637 return -ENOLINK;
2640 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
2641 sdata->name, req->bss->bssid, req->reason_code);
2643 ieee80211_set_disassoc(sdata, false);
2645 mutex_unlock(&ifmgd->mtx);
2647 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
2648 IEEE80211_STYPE_DISASSOC, req->reason_code,
2649 cookie);
2650 return 0;