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 <linux/slab.h>
23 #include <net/mac80211.h>
24 #include <asm/unaligned.h>
26 #include "ieee80211_i.h"
27 #include "driver-ops.h"
31 #define IEEE80211_MAX_PROBE_TRIES 5
34 * Beacon loss timeout is calculated as N frames times the
35 * advertised beacon interval. This may need to be somewhat
36 * higher than what hardware might detect to account for
37 * delays in the host processing frames. But since we also
38 * probe on beacon miss before declaring the connection lost
39 * default to what we want.
41 #define IEEE80211_BEACON_LOSS_COUNT 7
44 * Time the connection can be idle before we probe
45 * it to see if we can still talk to the AP.
47 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
49 * Time we wait for a probe response after sending
50 * a probe request because of beacon loss or for
51 * checking the connection still works.
53 #define IEEE80211_PROBE_WAIT (HZ / 2)
56 * Weight given to the latest Beacon frame when calculating average signal
57 * strength for Beacon frames received in the current BSS. This must be
60 #define IEEE80211_SIGNAL_AVE_WEIGHT 3
63 * How many Beacon frames need to have been used in average signal strength
64 * before starting to indicate signal change events.
66 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
68 #define TMR_RUNNING_TIMER 0
69 #define TMR_RUNNING_CHANSW 1
72 * All cfg80211 functions have to be called outside a locked
73 * section so that they can acquire a lock themselves... This
74 * is much simpler than queuing up things in cfg80211, but we
75 * do need some indirection for that here.
78 /* no action required */
81 /* caller must call cfg80211_send_rx_auth() */
82 RX_MGMT_CFG80211_AUTH,
84 /* caller must call cfg80211_send_rx_assoc() */
85 RX_MGMT_CFG80211_ASSOC,
87 /* caller must call cfg80211_send_deauth() */
88 RX_MGMT_CFG80211_DEAUTH,
90 /* caller must call cfg80211_send_disassoc() */
91 RX_MGMT_CFG80211_DISASSOC,
93 /* caller must tell cfg80211 about internal error */
94 RX_MGMT_CFG80211_ASSOC_ERROR,
98 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
100 lockdep_assert_held(&ifmgd->mtx);
104 * We can have multiple work items (and connection probing)
105 * scheduling this timer, but we need to take care to only
106 * reschedule it when it should fire _earlier_ than it was
107 * asked for before, or if it's not pending right now. This
108 * function ensures that. Note that it then is required to
109 * run this function for all timeouts after the first one
110 * has happened -- the work that runs from this timer will
113 static void run_again(struct ieee80211_if_managed *ifmgd,
114 unsigned long timeout)
116 ASSERT_MGD_MTX(ifmgd);
118 if (!timer_pending(&ifmgd->timer) ||
119 time_before(timeout, ifmgd->timer.expires))
120 mod_timer(&ifmgd->timer, timeout);
123 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
125 if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
128 mod_timer(&sdata->u.mgd.bcn_mon_timer,
129 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
132 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
134 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
136 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
139 mod_timer(&sdata->u.mgd.conn_mon_timer,
140 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
142 ifmgd->probe_send_count = 0;
145 static int ecw2cw(int ecw)
147 return (1 << ecw) - 1;
151 * ieee80211_enable_ht should be called only after the operating band
152 * has been determined as ht configuration depends on the hw's
153 * HT abilities for a specific band.
155 static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
156 struct ieee80211_ht_info *hti,
157 const u8 *bssid, u16 ap_ht_cap_flags)
159 struct ieee80211_local *local = sdata->local;
160 struct ieee80211_supported_band *sband;
161 struct sta_info *sta;
164 bool enable_ht = true;
165 enum nl80211_channel_type prev_chantype;
166 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
168 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
170 prev_chantype = sdata->vif.bss_conf.channel_type;
172 /* HT is not supported */
173 if (!sband->ht_cap.ht_supported)
176 /* check that channel matches the right operating channel */
177 if (local->hw.conf.channel->center_freq !=
178 ieee80211_channel_to_frequency(hti->control_chan))
182 channel_type = NL80211_CHAN_HT20;
184 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
185 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
186 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
187 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
188 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
189 if (!(local->hw.conf.channel->flags &
190 IEEE80211_CHAN_NO_HT40PLUS))
191 channel_type = NL80211_CHAN_HT40PLUS;
193 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
194 if (!(local->hw.conf.channel->flags &
195 IEEE80211_CHAN_NO_HT40MINUS))
196 channel_type = NL80211_CHAN_HT40MINUS;
202 if (local->tmp_channel)
203 local->tmp_channel_type = channel_type;
205 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
206 /* can only fail due to HT40+/- mismatch */
207 channel_type = NL80211_CHAN_HT20;
208 WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
211 /* channel_type change automatically detected */
212 ieee80211_hw_config(local, 0);
214 if (prev_chantype != channel_type) {
216 sta = sta_info_get(sdata, bssid);
218 rate_control_rate_update(local, sband, sta,
219 IEEE80211_RC_HT_CHANGED,
224 ht_opmode = le16_to_cpu(hti->operation_mode);
226 /* if bss configuration changed store the new one */
227 if (sdata->ht_opmode_valid != enable_ht ||
228 sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
229 prev_chantype != channel_type) {
230 changed |= BSS_CHANGED_HT;
231 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
232 sdata->ht_opmode_valid = enable_ht;
238 /* frame sending functions */
240 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
241 const u8 *bssid, u16 stype, u16 reason,
242 void *cookie, bool send_frame)
244 struct ieee80211_local *local = sdata->local;
245 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
247 struct ieee80211_mgmt *mgmt;
249 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
251 printk(KERN_DEBUG "%s: failed to allocate buffer for "
252 "deauth/disassoc frame\n", sdata->name);
255 skb_reserve(skb, local->hw.extra_tx_headroom);
257 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
259 memcpy(mgmt->da, bssid, ETH_ALEN);
260 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
261 memcpy(mgmt->bssid, bssid, ETH_ALEN);
262 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
264 /* u.deauth.reason_code == u.disassoc.reason_code */
265 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
267 if (stype == IEEE80211_STYPE_DEAUTH)
269 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
271 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
274 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
276 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
277 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
278 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
281 ieee80211_tx_skb(sdata, skb);
286 void ieee80211_send_pspoll(struct ieee80211_local *local,
287 struct ieee80211_sub_if_data *sdata)
289 struct ieee80211_pspoll *pspoll;
292 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
296 pspoll = (struct ieee80211_pspoll *) skb->data;
297 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
299 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
300 ieee80211_tx_skb(sdata, skb);
303 void ieee80211_send_nullfunc(struct ieee80211_local *local,
304 struct ieee80211_sub_if_data *sdata,
308 struct ieee80211_hdr_3addr *nullfunc;
310 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
314 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
316 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
318 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
319 ieee80211_tx_skb(sdata, skb);
322 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
323 struct ieee80211_sub_if_data *sdata)
326 struct ieee80211_hdr *nullfunc;
329 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
332 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
334 printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
335 "nullfunc frame\n", sdata->name);
338 skb_reserve(skb, local->hw.extra_tx_headroom);
340 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
341 memset(nullfunc, 0, 30);
342 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
343 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
344 nullfunc->frame_control = fc;
345 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
346 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
347 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
348 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
350 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
351 ieee80211_tx_skb(sdata, skb);
354 /* spectrum management related things */
355 static void ieee80211_chswitch_work(struct work_struct *work)
357 struct ieee80211_sub_if_data *sdata =
358 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
359 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
361 if (!ieee80211_sdata_running(sdata))
364 mutex_lock(&ifmgd->mtx);
365 if (!ifmgd->associated)
368 sdata->local->oper_channel = sdata->local->csa_channel;
369 if (!sdata->local->ops->channel_switch) {
370 /* call "hw_config" only if doing sw channel switch */
371 ieee80211_hw_config(sdata->local,
372 IEEE80211_CONF_CHANGE_CHANNEL);
375 /* XXX: shouldn't really modify cfg80211-owned data! */
376 ifmgd->associated->channel = sdata->local->oper_channel;
378 ieee80211_wake_queues_by_reason(&sdata->local->hw,
379 IEEE80211_QUEUE_STOP_REASON_CSA);
381 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
382 mutex_unlock(&ifmgd->mtx);
385 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
387 struct ieee80211_sub_if_data *sdata;
388 struct ieee80211_if_managed *ifmgd;
390 sdata = vif_to_sdata(vif);
391 ifmgd = &sdata->u.mgd;
393 trace_api_chswitch_done(sdata, success);
396 * If the channel switch was not successful, stay
397 * around on the old channel. We currently lack
398 * good handling of this situation, possibly we
399 * should just drop the association.
401 sdata->local->csa_channel = sdata->local->oper_channel;
404 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
406 EXPORT_SYMBOL(ieee80211_chswitch_done);
408 static void ieee80211_chswitch_timer(unsigned long data)
410 struct ieee80211_sub_if_data *sdata =
411 (struct ieee80211_sub_if_data *) data;
412 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
414 if (sdata->local->quiescing) {
415 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
419 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
422 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
423 struct ieee80211_channel_sw_ie *sw_elem,
424 struct ieee80211_bss *bss,
427 struct cfg80211_bss *cbss =
428 container_of((void *)bss, struct cfg80211_bss, priv);
429 struct ieee80211_channel *new_ch;
430 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
431 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
433 ASSERT_MGD_MTX(ifmgd);
435 if (!ifmgd->associated)
438 if (sdata->local->scanning)
441 /* Disregard subsequent beacons if we are already running a timer
444 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
447 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
448 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
451 sdata->local->csa_channel = new_ch;
453 if (sdata->local->ops->channel_switch) {
454 /* use driver's channel switch callback */
455 struct ieee80211_channel_switch ch_switch;
456 memset(&ch_switch, 0, sizeof(ch_switch));
457 ch_switch.timestamp = timestamp;
459 ch_switch.block_tx = true;
460 ieee80211_stop_queues_by_reason(&sdata->local->hw,
461 IEEE80211_QUEUE_STOP_REASON_CSA);
463 ch_switch.channel = new_ch;
464 ch_switch.count = sw_elem->count;
465 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
466 drv_channel_switch(sdata->local, &ch_switch);
470 /* channel switch handled in software */
471 if (sw_elem->count <= 1) {
472 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
475 ieee80211_stop_queues_by_reason(&sdata->local->hw,
476 IEEE80211_QUEUE_STOP_REASON_CSA);
477 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
478 mod_timer(&ifmgd->chswitch_timer,
480 msecs_to_jiffies(sw_elem->count *
481 cbss->beacon_interval));
485 static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
486 u16 capab_info, u8 *pwr_constr_elem,
487 u8 pwr_constr_elem_len)
489 struct ieee80211_conf *conf = &sdata->local->hw.conf;
491 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
494 /* Power constraint IE length should be 1 octet */
495 if (pwr_constr_elem_len != 1)
498 if ((*pwr_constr_elem <= conf->channel->max_power) &&
499 (*pwr_constr_elem != sdata->local->power_constr_level)) {
500 sdata->local->power_constr_level = *pwr_constr_elem;
501 ieee80211_hw_config(sdata->local, 0);
505 void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
507 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
508 struct ieee80211_local *local = sdata->local;
509 struct ieee80211_conf *conf = &local->hw.conf;
511 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
512 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
513 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
515 local->disable_dynamic_ps = false;
516 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
518 EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
520 void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
522 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
523 struct ieee80211_local *local = sdata->local;
524 struct ieee80211_conf *conf = &local->hw.conf;
526 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
527 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
528 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
530 local->disable_dynamic_ps = true;
531 conf->dynamic_ps_timeout = 0;
532 del_timer_sync(&local->dynamic_ps_timer);
533 ieee80211_queue_work(&local->hw,
534 &local->dynamic_ps_enable_work);
536 EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
539 static void ieee80211_enable_ps(struct ieee80211_local *local,
540 struct ieee80211_sub_if_data *sdata)
542 struct ieee80211_conf *conf = &local->hw.conf;
545 * If we are scanning right now then the parameters will
546 * take effect when scan finishes.
551 if (conf->dynamic_ps_timeout > 0 &&
552 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
553 mod_timer(&local->dynamic_ps_timer, jiffies +
554 msecs_to_jiffies(conf->dynamic_ps_timeout));
556 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
557 ieee80211_send_nullfunc(local, sdata, 1);
559 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
560 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
563 conf->flags |= IEEE80211_CONF_PS;
564 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
568 static void ieee80211_change_ps(struct ieee80211_local *local)
570 struct ieee80211_conf *conf = &local->hw.conf;
572 if (local->ps_sdata) {
573 ieee80211_enable_ps(local, local->ps_sdata);
574 } else if (conf->flags & IEEE80211_CONF_PS) {
575 conf->flags &= ~IEEE80211_CONF_PS;
576 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
577 del_timer_sync(&local->dynamic_ps_timer);
578 cancel_work_sync(&local->dynamic_ps_enable_work);
582 /* need to hold RTNL or interface lock */
583 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
585 struct ieee80211_sub_if_data *sdata, *found = NULL;
589 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
590 local->ps_sdata = NULL;
594 if (!list_empty(&local->work_list)) {
595 local->ps_sdata = NULL;
599 list_for_each_entry(sdata, &local->interfaces, list) {
600 if (!ieee80211_sdata_running(sdata))
602 if (sdata->vif.type != NL80211_IFTYPE_STATION)
608 if (count == 1 && found->u.mgd.powersave &&
609 found->u.mgd.associated &&
610 found->u.mgd.associated->beacon_ies &&
611 !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
612 IEEE80211_STA_CONNECTION_POLL))) {
613 struct ieee80211_conf *conf = &local->hw.conf;
617 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
619 beaconint_us = ieee80211_tu_to_usec(
620 found->vif.bss_conf.beacon_int);
622 timeout = local->dynamic_ps_forced_timeout;
625 * Go to full PSM if the user configures a very low
626 * latency requirement.
627 * The 2 second value is there for compatibility until
628 * the PM_QOS_NETWORK_LATENCY is configured with real
631 if (latency > 1900000000 && latency != 2000000000)
636 local->dynamic_ps_user_timeout = timeout;
637 if (!local->disable_dynamic_ps)
638 conf->dynamic_ps_timeout =
639 local->dynamic_ps_user_timeout;
641 if (beaconint_us > latency) {
642 local->ps_sdata = NULL;
644 struct ieee80211_bss *bss;
648 bss = (void *)found->u.mgd.associated->priv;
649 dtimper = bss->dtim_period;
651 /* If the TIM IE is invalid, pretend the value is 1 */
654 else if (dtimper > 1)
655 maxslp = min_t(int, dtimper,
656 latency / beaconint_us);
658 local->hw.conf.max_sleep_period = maxslp;
659 local->hw.conf.ps_dtim_period = dtimper;
660 local->ps_sdata = found;
663 local->ps_sdata = NULL;
667 ieee80211_change_ps(local);
670 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
672 struct ieee80211_local *local =
673 container_of(work, struct ieee80211_local,
674 dynamic_ps_disable_work);
676 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
677 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
678 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
681 ieee80211_wake_queues_by_reason(&local->hw,
682 IEEE80211_QUEUE_STOP_REASON_PS);
685 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
687 struct ieee80211_local *local =
688 container_of(work, struct ieee80211_local,
689 dynamic_ps_enable_work);
690 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
691 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
693 /* can only happen when PS was just disabled anyway */
697 if (local->hw.conf.flags & IEEE80211_CONF_PS)
700 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
701 (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
702 ieee80211_send_nullfunc(local, sdata, 1);
704 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
705 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
706 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
707 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
708 local->hw.conf.flags |= IEEE80211_CONF_PS;
709 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
713 void ieee80211_dynamic_ps_timer(unsigned long data)
715 struct ieee80211_local *local = (void *) data;
717 if (local->quiescing || local->suspended)
720 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
724 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
725 struct ieee80211_sub_if_data *sdata,
726 u8 *wmm_param, size_t wmm_param_len)
728 struct ieee80211_tx_queue_params params;
729 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
732 u8 *pos, uapsd_queues = 0;
734 if (!local->ops->conf_tx)
737 if (local->hw.queues < 4)
743 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
746 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
747 uapsd_queues = local->uapsd_queues;
749 count = wmm_param[6] & 0x0f;
750 if (count == ifmgd->wmm_last_param_set)
752 ifmgd->wmm_last_param_set = count;
755 left = wmm_param_len - 8;
757 memset(¶ms, 0, sizeof(params));
760 for (; left >= 4; left -= 4, pos += 4) {
761 int aci = (pos[0] >> 5) & 0x03;
762 int acm = (pos[0] >> 4) & 0x01;
770 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
771 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
777 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
778 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
784 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
785 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
792 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
793 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
798 params.aifs = pos[0] & 0x0f;
799 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
800 params.cw_min = ecw2cw(pos[1] & 0x0f);
801 params.txop = get_unaligned_le16(pos + 2);
802 params.uapsd = uapsd;
804 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
805 wiphy_debug(local->hw.wiphy,
806 "WMM queue=%d aci=%d acm=%d aifs=%d "
807 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
809 params.aifs, params.cw_min, params.cw_max,
810 params.txop, params.uapsd);
812 if (drv_conf_tx(local, queue, ¶ms))
813 wiphy_debug(local->hw.wiphy,
814 "failed to set TX queue parameters for queue %d\n",
818 /* enable WMM or activate new settings */
819 sdata->vif.bss_conf.qos = true;
820 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
823 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
824 u16 capab, bool erp_valid, u8 erp)
826 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
829 bool use_short_preamble;
833 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
834 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
836 use_protection = false;
837 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
840 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
841 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
842 use_short_slot = true;
844 if (use_protection != bss_conf->use_cts_prot) {
845 bss_conf->use_cts_prot = use_protection;
846 changed |= BSS_CHANGED_ERP_CTS_PROT;
849 if (use_short_preamble != bss_conf->use_short_preamble) {
850 bss_conf->use_short_preamble = use_short_preamble;
851 changed |= BSS_CHANGED_ERP_PREAMBLE;
854 if (use_short_slot != bss_conf->use_short_slot) {
855 bss_conf->use_short_slot = use_short_slot;
856 changed |= BSS_CHANGED_ERP_SLOT;
862 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
863 struct cfg80211_bss *cbss,
864 u32 bss_info_changed)
866 struct ieee80211_bss *bss = (void *)cbss->priv;
867 struct ieee80211_local *local = sdata->local;
868 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
870 bss_info_changed |= BSS_CHANGED_ASSOC;
871 /* set timing information */
872 bss_conf->beacon_int = cbss->beacon_interval;
873 bss_conf->timestamp = cbss->tsf;
875 bss_info_changed |= BSS_CHANGED_BEACON_INT;
876 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
877 cbss->capability, bss->has_erp_value, bss->erp_value);
879 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
880 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
882 sdata->u.mgd.associated = cbss;
883 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
885 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
887 /* just to be sure */
888 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
889 IEEE80211_STA_BEACON_POLL);
891 ieee80211_led_assoc(local, 1);
893 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
894 bss_conf->dtim_period = bss->dtim_period;
896 bss_conf->dtim_period = 0;
900 * For now just always ask the driver to update the basic rateset
901 * when we have associated, we aren't checking whether it actually
904 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
906 /* And the BSSID changed - we're associated now */
907 bss_info_changed |= BSS_CHANGED_BSSID;
909 /* Tell the driver to monitor connection quality (if supported) */
910 if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
911 bss_conf->cqm_rssi_thold)
912 bss_info_changed |= BSS_CHANGED_CQM;
914 /* Enable ARP filtering */
915 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
916 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
917 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
920 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
922 mutex_lock(&local->iflist_mtx);
923 ieee80211_recalc_ps(local, -1);
924 ieee80211_recalc_smps(local);
925 mutex_unlock(&local->iflist_mtx);
927 netif_tx_start_all_queues(sdata->dev);
928 netif_carrier_on(sdata->dev);
931 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
932 bool remove_sta, bool tx)
934 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
935 struct ieee80211_local *local = sdata->local;
936 struct sta_info *sta;
937 u32 changed = 0, config_changed = 0;
940 ASSERT_MGD_MTX(ifmgd);
942 if (WARN_ON(!ifmgd->associated))
945 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
947 ifmgd->associated = NULL;
948 memset(ifmgd->bssid, 0, ETH_ALEN);
951 * we need to commit the associated = NULL change because the
952 * scan code uses that to determine whether this iface should
953 * go to/wake up from powersave or not -- and could otherwise
954 * wake the queues erroneously.
959 * Thus, we can only afterwards stop the queues -- to account
960 * for the case where another CPU is finishing a scan at this
961 * time -- we don't want the scan code to enable queues.
964 netif_tx_stop_all_queues(sdata->dev);
965 netif_carrier_off(sdata->dev);
967 mutex_lock(&local->sta_mtx);
968 sta = sta_info_get(sdata, bssid);
970 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
971 ieee80211_sta_tear_down_BA_sessions(sta, tx);
973 mutex_unlock(&local->sta_mtx);
975 changed |= ieee80211_reset_erp_info(sdata);
977 ieee80211_led_assoc(local, 0);
978 changed |= BSS_CHANGED_ASSOC;
979 sdata->vif.bss_conf.assoc = false;
981 ieee80211_set_wmm_default(sdata);
983 /* channel(_type) changes are handled by ieee80211_hw_config */
984 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
986 /* on the next assoc, re-program HT parameters */
987 sdata->ht_opmode_valid = false;
989 local->power_constr_level = 0;
991 del_timer_sync(&local->dynamic_ps_timer);
992 cancel_work_sync(&local->dynamic_ps_enable_work);
994 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
995 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
996 config_changed |= IEEE80211_CONF_CHANGE_PS;
999 ieee80211_hw_config(local, config_changed);
1001 /* Disable ARP filtering */
1002 if (sdata->vif.bss_conf.arp_filter_enabled) {
1003 sdata->vif.bss_conf.arp_filter_enabled = false;
1004 changed |= BSS_CHANGED_ARP_FILTER;
1007 /* The BSSID (not really interesting) and HT changed */
1008 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1009 ieee80211_bss_info_change_notify(sdata, changed);
1012 sta_info_destroy_addr(sdata, bssid);
1014 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1015 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1016 del_timer_sync(&sdata->u.mgd.timer);
1017 del_timer_sync(&sdata->u.mgd.chswitch_timer);
1020 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1021 struct ieee80211_hdr *hdr)
1024 * We can postpone the mgd.timer whenever receiving unicast frames
1025 * from AP because we know that the connection is working both ways
1026 * at that time. But multicast frames (and hence also beacons) must
1027 * be ignored here, because we need to trigger the timer during
1028 * data idle periods for sending the periodic probe request to the
1029 * AP we're connected to.
1031 if (is_multicast_ether_addr(hdr->addr1))
1034 ieee80211_sta_reset_conn_monitor(sdata);
1037 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1039 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1041 u8 *dst = ifmgd->associated->bssid;
1042 u8 unicast_limit = max(1, IEEE80211_MAX_PROBE_TRIES - 3);
1045 * Try sending broadcast probe requests for the last three
1046 * probe requests after the first ones failed since some
1047 * buggy APs only support broadcast probe requests.
1049 if (ifmgd->probe_send_count >= unicast_limit)
1052 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1053 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
1055 ifmgd->probe_send_count++;
1056 ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1057 run_again(ifmgd, ifmgd->probe_timeout);
1060 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1063 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1064 bool already = false;
1066 if (!ieee80211_sdata_running(sdata))
1069 if (sdata->local->scanning)
1072 if (sdata->local->tmp_channel)
1075 mutex_lock(&ifmgd->mtx);
1077 if (!ifmgd->associated)
1080 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1081 if (beacon && net_ratelimit())
1082 printk(KERN_DEBUG "%s: detected beacon loss from AP "
1083 "- sending probe request\n", sdata->name);
1087 * The driver/our work has already reported this event or the
1088 * connection monitoring has kicked in and we have already sent
1089 * a probe request. Or maybe the AP died and the driver keeps
1090 * reporting until we disassociate...
1092 * In either case we have to ignore the current call to this
1093 * function (except for setting the correct probe reason bit)
1094 * because otherwise we would reset the timer every time and
1095 * never check whether we received a probe response!
1097 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1098 IEEE80211_STA_CONNECTION_POLL))
1102 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1104 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1109 mutex_lock(&sdata->local->iflist_mtx);
1110 ieee80211_recalc_ps(sdata->local, -1);
1111 mutex_unlock(&sdata->local->iflist_mtx);
1113 ifmgd->probe_send_count = 0;
1114 ieee80211_mgd_probe_ap_send(sdata);
1116 mutex_unlock(&ifmgd->mtx);
1119 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1120 struct ieee80211_vif *vif)
1122 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1123 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1124 struct sk_buff *skb;
1127 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1130 ASSERT_MGD_MTX(ifmgd);
1132 if (!ifmgd->associated)
1135 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1136 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
1137 ssid + 2, ssid[1], NULL, 0);
1141 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1143 static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1145 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1146 struct ieee80211_local *local = sdata->local;
1149 mutex_lock(&ifmgd->mtx);
1150 if (!ifmgd->associated) {
1151 mutex_unlock(&ifmgd->mtx);
1155 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1157 printk(KERN_DEBUG "Connection to AP %pM lost.\n", bssid);
1159 ieee80211_set_disassoc(sdata, true, true);
1160 mutex_unlock(&ifmgd->mtx);
1162 mutex_lock(&local->mtx);
1163 ieee80211_recalc_idle(local);
1164 mutex_unlock(&local->mtx);
1166 * must be outside lock due to cfg80211,
1167 * but that's not a problem.
1169 ieee80211_send_deauth_disassoc(sdata, bssid,
1170 IEEE80211_STYPE_DEAUTH,
1171 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1175 void ieee80211_beacon_connection_loss_work(struct work_struct *work)
1177 struct ieee80211_sub_if_data *sdata =
1178 container_of(work, struct ieee80211_sub_if_data,
1179 u.mgd.beacon_connection_loss_work);
1181 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1182 __ieee80211_connection_loss(sdata);
1184 ieee80211_mgd_probe_ap(sdata, true);
1187 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1189 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1190 struct ieee80211_hw *hw = &sdata->local->hw;
1192 trace_api_beacon_loss(sdata);
1194 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1195 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1197 EXPORT_SYMBOL(ieee80211_beacon_loss);
1199 void ieee80211_connection_loss(struct ieee80211_vif *vif)
1201 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1202 struct ieee80211_hw *hw = &sdata->local->hw;
1204 trace_api_connection_loss(sdata);
1206 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1207 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1209 EXPORT_SYMBOL(ieee80211_connection_loss);
1212 static enum rx_mgmt_action __must_check
1213 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1214 struct ieee80211_mgmt *mgmt, size_t len)
1216 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1217 const u8 *bssid = NULL;
1221 return RX_MGMT_NONE;
1223 ASSERT_MGD_MTX(ifmgd);
1225 bssid = ifmgd->associated->bssid;
1227 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1229 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
1230 sdata->name, bssid, reason_code);
1232 ieee80211_set_disassoc(sdata, true, false);
1233 mutex_lock(&sdata->local->mtx);
1234 ieee80211_recalc_idle(sdata->local);
1235 mutex_unlock(&sdata->local->mtx);
1237 return RX_MGMT_CFG80211_DEAUTH;
1241 static enum rx_mgmt_action __must_check
1242 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1243 struct ieee80211_mgmt *mgmt, size_t len)
1245 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1249 return RX_MGMT_NONE;
1251 ASSERT_MGD_MTX(ifmgd);
1253 if (WARN_ON(!ifmgd->associated))
1254 return RX_MGMT_NONE;
1256 if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
1257 return RX_MGMT_NONE;
1259 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1261 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
1262 sdata->name, mgmt->sa, reason_code);
1264 ieee80211_set_disassoc(sdata, true, false);
1265 mutex_lock(&sdata->local->mtx);
1266 ieee80211_recalc_idle(sdata->local);
1267 mutex_unlock(&sdata->local->mtx);
1268 return RX_MGMT_CFG80211_DISASSOC;
1272 static bool ieee80211_assoc_success(struct ieee80211_work *wk,
1273 struct ieee80211_mgmt *mgmt, size_t len)
1275 struct ieee80211_sub_if_data *sdata = wk->sdata;
1276 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1277 struct ieee80211_local *local = sdata->local;
1278 struct ieee80211_supported_band *sband;
1279 struct sta_info *sta;
1280 struct cfg80211_bss *cbss = wk->assoc.bss;
1282 u32 rates, basic_rates;
1283 u16 capab_info, aid;
1284 struct ieee802_11_elems elems;
1285 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1288 bool have_higher_than_11mbit = false;
1289 u16 ap_ht_cap_flags;
1291 /* AssocResp and ReassocResp have identical structure */
1293 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1294 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1296 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1297 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1298 "set\n", sdata->name, aid);
1299 aid &= ~(BIT(15) | BIT(14));
1301 pos = mgmt->u.assoc_resp.variable;
1302 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1304 if (!elems.supp_rates) {
1305 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1312 sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
1314 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1315 " the AP\n", sdata->name);
1319 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1321 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1322 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1326 sband = local->hw.wiphy->bands[wk->chan->band];
1328 for (i = 0; i < elems.supp_rates_len; i++) {
1329 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1330 bool is_basic = !!(elems.supp_rates[i] & 0x80);
1333 have_higher_than_11mbit = true;
1335 for (j = 0; j < sband->n_bitrates; j++) {
1336 if (sband->bitrates[j].bitrate == rate) {
1339 basic_rates |= BIT(j);
1345 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1346 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1347 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
1350 have_higher_than_11mbit = true;
1352 for (j = 0; j < sband->n_bitrates; j++) {
1353 if (sband->bitrates[j].bitrate == rate) {
1356 basic_rates |= BIT(j);
1362 sta->sta.supp_rates[wk->chan->band] = rates;
1363 sdata->vif.bss_conf.basic_rates = basic_rates;
1365 /* cf. IEEE 802.11 9.2.12 */
1366 if (wk->chan->band == IEEE80211_BAND_2GHZ &&
1367 have_higher_than_11mbit)
1368 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1370 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1372 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1373 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1374 elems.ht_cap_elem, &sta->sta.ht_cap);
1376 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1378 rate_control_rate_init(sta);
1380 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
1381 set_sta_flags(sta, WLAN_STA_MFP);
1383 if (elems.wmm_param)
1384 set_sta_flags(sta, WLAN_STA_WME);
1386 err = sta_info_insert(sta);
1389 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1390 " the AP (error %d)\n", sdata->name, err);
1395 * Always handle WMM once after association regardless
1396 * of the first value the AP uses. Setting -1 here has
1397 * that effect because the AP values is an unsigned
1400 ifmgd->wmm_last_param_set = -1;
1402 if (elems.wmm_param)
1403 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
1404 elems.wmm_param_len);
1406 ieee80211_set_wmm_default(sdata);
1408 local->oper_channel = wk->chan;
1410 if (elems.ht_info_elem && elems.wmm_param &&
1411 (sdata->local->hw.queues >= 4) &&
1412 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1413 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1414 cbss->bssid, ap_ht_cap_flags);
1416 /* set AID and assoc capability,
1417 * ieee80211_set_associated() will tell the driver */
1418 bss_conf->aid = aid;
1419 bss_conf->assoc_capability = capab_info;
1420 ieee80211_set_associated(sdata, cbss, changed);
1423 * If we're using 4-addr mode, let the AP know that we're
1424 * doing so, so that it can create the STA VLAN on its side
1426 if (ifmgd->use_4addr)
1427 ieee80211_send_4addr_nullfunc(local, sdata);
1430 * Start timer to probe the connection to the AP now.
1431 * Also start the timer that will detect beacon loss.
1433 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
1434 ieee80211_sta_reset_beacon_monitor(sdata);
1440 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1441 struct ieee80211_mgmt *mgmt,
1443 struct ieee80211_rx_status *rx_status,
1444 struct ieee802_11_elems *elems,
1447 struct ieee80211_local *local = sdata->local;
1449 struct ieee80211_bss *bss;
1450 struct ieee80211_channel *channel;
1451 bool need_ps = false;
1453 if (sdata->u.mgd.associated) {
1454 bss = (void *)sdata->u.mgd.associated->priv;
1455 /* not previously set so we may need to recalc */
1456 need_ps = !bss->dtim_period;
1459 if (elems->ds_params && elems->ds_params_len == 1)
1460 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1462 freq = rx_status->freq;
1464 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1466 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1469 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1472 ieee80211_rx_bss_put(local, bss);
1474 if (!sdata->u.mgd.associated)
1478 mutex_lock(&local->iflist_mtx);
1479 ieee80211_recalc_ps(local, -1);
1480 mutex_unlock(&local->iflist_mtx);
1483 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
1484 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
1486 struct ieee80211_channel_sw_ie *sw_elem =
1487 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
1488 ieee80211_sta_process_chanswitch(sdata, sw_elem,
1489 bss, rx_status->mactime);
1494 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1495 struct sk_buff *skb)
1497 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1498 struct ieee80211_if_managed *ifmgd;
1499 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
1500 size_t baselen, len = skb->len;
1501 struct ieee802_11_elems elems;
1503 ifmgd = &sdata->u.mgd;
1505 ASSERT_MGD_MTX(ifmgd);
1507 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
1508 return; /* ignore ProbeResp to foreign address */
1510 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1514 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1517 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1519 if (ifmgd->associated &&
1520 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0 &&
1521 ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1522 IEEE80211_STA_CONNECTION_POLL)) {
1523 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1524 IEEE80211_STA_BEACON_POLL);
1525 mutex_lock(&sdata->local->iflist_mtx);
1526 ieee80211_recalc_ps(sdata->local, -1);
1527 mutex_unlock(&sdata->local->iflist_mtx);
1529 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1533 * We've received a probe response, but are not sure whether
1534 * we have or will be receiving any beacons or data, so let's
1535 * schedule the timers again, just in case.
1537 ieee80211_sta_reset_beacon_monitor(sdata);
1539 mod_timer(&ifmgd->conn_mon_timer,
1540 round_jiffies_up(jiffies +
1541 IEEE80211_CONNECTION_IDLE_TIME));
1546 * This is the canonical list of information elements we care about,
1547 * the filter code also gives us all changes to the Microsoft OUI
1548 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1550 * We implement beacon filtering in software since that means we can
1551 * avoid processing the frame here and in cfg80211, and userspace
1552 * will not be able to tell whether the hardware supports it or not.
1554 * XXX: This list needs to be dynamic -- userspace needs to be able to
1555 * add items it requires. It also needs to be able to tell us to
1556 * look out for other vendor IEs.
1558 static const u64 care_about_ies =
1559 (1ULL << WLAN_EID_COUNTRY) |
1560 (1ULL << WLAN_EID_ERP_INFO) |
1561 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1562 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1563 (1ULL << WLAN_EID_HT_CAPABILITY) |
1564 (1ULL << WLAN_EID_HT_INFORMATION);
1566 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1567 struct ieee80211_mgmt *mgmt,
1569 struct ieee80211_rx_status *rx_status)
1571 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1572 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1574 struct ieee802_11_elems elems;
1575 struct ieee80211_local *local = sdata->local;
1577 bool erp_valid, directed_tim = false;
1582 ASSERT_MGD_MTX(ifmgd);
1584 /* Process beacon from the current BSS */
1585 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1589 if (rx_status->freq != local->hw.conf.channel->center_freq)
1593 * We might have received a number of frames, among them a
1594 * disassoc frame and a beacon...
1596 if (!ifmgd->associated)
1599 bssid = ifmgd->associated->bssid;
1602 * And in theory even frames from a different AP we were just
1603 * associated to a split-second ago!
1605 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
1608 /* Track average RSSI from the Beacon frames of the current AP */
1609 ifmgd->last_beacon_signal = rx_status->signal;
1610 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
1611 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
1612 ifmgd->ave_beacon_signal = rx_status->signal * 16;
1613 ifmgd->last_cqm_event_signal = 0;
1614 ifmgd->count_beacon_signal = 1;
1616 ifmgd->ave_beacon_signal =
1617 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
1618 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
1619 ifmgd->ave_beacon_signal) / 16;
1620 ifmgd->count_beacon_signal++;
1622 if (bss_conf->cqm_rssi_thold &&
1623 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
1624 !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1625 int sig = ifmgd->ave_beacon_signal / 16;
1626 int last_event = ifmgd->last_cqm_event_signal;
1627 int thold = bss_conf->cqm_rssi_thold;
1628 int hyst = bss_conf->cqm_rssi_hyst;
1630 (last_event == 0 || sig < last_event - hyst)) {
1631 ifmgd->last_cqm_event_signal = sig;
1632 ieee80211_cqm_rssi_notify(
1634 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
1636 } else if (sig > thold &&
1637 (last_event == 0 || sig > last_event + hyst)) {
1638 ifmgd->last_cqm_event_signal = sig;
1639 ieee80211_cqm_rssi_notify(
1641 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
1646 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
1647 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1648 if (net_ratelimit()) {
1649 printk(KERN_DEBUG "%s: cancelling probereq poll due "
1650 "to a received beacon\n", sdata->name);
1653 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
1654 mutex_lock(&local->iflist_mtx);
1655 ieee80211_recalc_ps(local, -1);
1656 mutex_unlock(&local->iflist_mtx);
1660 * Push the beacon loss detection into the future since
1661 * we are processing a beacon from the AP just now.
1663 ieee80211_sta_reset_beacon_monitor(sdata);
1665 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1666 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1667 len - baselen, &elems,
1668 care_about_ies, ncrc);
1670 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1671 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1674 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
1675 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1678 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
1679 elems.wmm_param_len);
1682 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1684 if (local->hw.conf.dynamic_ps_timeout > 0) {
1685 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1686 ieee80211_hw_config(local,
1687 IEEE80211_CONF_CHANGE_PS);
1688 ieee80211_send_nullfunc(local, sdata, 0);
1690 local->pspolling = true;
1693 * Here is assumed that the driver will be
1694 * able to send ps-poll frame and receive a
1695 * response even though power save mode is
1696 * enabled, but some drivers might require
1697 * to disable power save here. This needs
1698 * to be investigated.
1700 ieee80211_send_pspoll(local, sdata);
1705 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
1707 ifmgd->beacon_crc = ncrc;
1708 ifmgd->beacon_crc_valid = true;
1710 if (elems.erp_info && elems.erp_info_len >= 1) {
1712 erp_value = elems.erp_info[0];
1716 changed |= ieee80211_handle_bss_capability(sdata,
1717 le16_to_cpu(mgmt->u.beacon.capab_info),
1718 erp_valid, erp_value);
1721 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1722 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
1723 struct sta_info *sta;
1724 struct ieee80211_supported_band *sband;
1725 u16 ap_ht_cap_flags;
1729 sta = sta_info_get(sdata, bssid);
1730 if (WARN_ON(!sta)) {
1735 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1737 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1738 elems.ht_cap_elem, &sta->sta.ht_cap);
1740 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1744 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1745 bssid, ap_ht_cap_flags);
1748 /* Note: country IE parsing is done for us by cfg80211 */
1749 if (elems.country_elem) {
1750 /* TODO: IBSS also needs this */
1751 if (elems.pwr_constr_elem)
1752 ieee80211_handle_pwr_constr(sdata,
1753 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1754 elems.pwr_constr_elem,
1755 elems.pwr_constr_elem_len);
1758 ieee80211_bss_info_change_notify(sdata, changed);
1761 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1762 struct sk_buff *skb)
1764 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1765 struct ieee80211_rx_status *rx_status;
1766 struct ieee80211_mgmt *mgmt;
1767 enum rx_mgmt_action rma = RX_MGMT_NONE;
1770 rx_status = (struct ieee80211_rx_status *) skb->cb;
1771 mgmt = (struct ieee80211_mgmt *) skb->data;
1772 fc = le16_to_cpu(mgmt->frame_control);
1774 mutex_lock(&ifmgd->mtx);
1776 if (ifmgd->associated &&
1777 memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
1778 switch (fc & IEEE80211_FCTL_STYPE) {
1779 case IEEE80211_STYPE_BEACON:
1780 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1783 case IEEE80211_STYPE_PROBE_RESP:
1784 ieee80211_rx_mgmt_probe_resp(sdata, skb);
1786 case IEEE80211_STYPE_DEAUTH:
1787 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
1789 case IEEE80211_STYPE_DISASSOC:
1790 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1792 case IEEE80211_STYPE_ACTION:
1793 switch (mgmt->u.action.category) {
1794 case WLAN_CATEGORY_SPECTRUM_MGMT:
1795 ieee80211_sta_process_chanswitch(sdata,
1796 &mgmt->u.action.u.chan_switch.sw_elem,
1797 (void *)ifmgd->associated->priv,
1798 rx_status->mactime);
1802 mutex_unlock(&ifmgd->mtx);
1808 case RX_MGMT_CFG80211_DEAUTH:
1809 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
1811 case RX_MGMT_CFG80211_DISASSOC:
1812 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
1815 WARN(1, "unexpected: %d", rma);
1820 mutex_unlock(&ifmgd->mtx);
1822 if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
1823 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
1824 struct ieee80211_local *local = sdata->local;
1825 struct ieee80211_work *wk;
1827 mutex_lock(&local->mtx);
1828 list_for_each_entry(wk, &local->work_list, list) {
1829 if (wk->sdata != sdata)
1832 if (wk->type != IEEE80211_WORK_ASSOC &&
1833 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
1836 if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
1838 if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
1842 * Printing the message only here means we can't
1843 * spuriously print it, but it also means that it
1844 * won't be printed when the frame comes in before
1845 * we even tried to associate or in similar cases.
1847 * Ultimately, I suspect cfg80211 should print the
1851 "%s: deauthenticated from %pM (Reason: %u)\n",
1852 sdata->name, mgmt->bssid,
1853 le16_to_cpu(mgmt->u.deauth.reason_code));
1855 list_del_rcu(&wk->list);
1859 mutex_unlock(&local->mtx);
1861 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
1865 static void ieee80211_sta_timer(unsigned long data)
1867 struct ieee80211_sub_if_data *sdata =
1868 (struct ieee80211_sub_if_data *) data;
1869 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1870 struct ieee80211_local *local = sdata->local;
1872 if (local->quiescing) {
1873 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
1877 ieee80211_queue_work(&local->hw, &sdata->work);
1880 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
1882 struct ieee80211_local *local = sdata->local;
1883 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1885 /* then process the rest of the work */
1886 mutex_lock(&ifmgd->mtx);
1888 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1889 IEEE80211_STA_CONNECTION_POLL) &&
1890 ifmgd->associated) {
1893 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1894 if (time_is_after_jiffies(ifmgd->probe_timeout))
1895 run_again(ifmgd, ifmgd->probe_timeout);
1897 else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
1898 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1899 wiphy_debug(local->hw.wiphy,
1900 "%s: No probe response from AP %pM"
1901 " after %dms, try %d\n",
1903 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ,
1904 ifmgd->probe_send_count);
1906 ieee80211_mgd_probe_ap_send(sdata);
1909 * We actually lost the connection ... or did we?
1912 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1913 IEEE80211_STA_BEACON_POLL);
1914 wiphy_debug(local->hw.wiphy,
1915 "%s: No probe response from AP %pM"
1916 " after %dms, disconnecting.\n",
1918 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
1919 ieee80211_set_disassoc(sdata, true, true);
1920 mutex_unlock(&ifmgd->mtx);
1921 mutex_lock(&local->mtx);
1922 ieee80211_recalc_idle(local);
1923 mutex_unlock(&local->mtx);
1925 * must be outside lock due to cfg80211,
1926 * but that's not a problem.
1928 ieee80211_send_deauth_disassoc(sdata, bssid,
1929 IEEE80211_STYPE_DEAUTH,
1930 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1932 mutex_lock(&ifmgd->mtx);
1936 mutex_unlock(&ifmgd->mtx);
1939 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
1941 struct ieee80211_sub_if_data *sdata =
1942 (struct ieee80211_sub_if_data *) data;
1943 struct ieee80211_local *local = sdata->local;
1945 if (local->quiescing)
1948 ieee80211_queue_work(&sdata->local->hw,
1949 &sdata->u.mgd.beacon_connection_loss_work);
1952 static void ieee80211_sta_conn_mon_timer(unsigned long data)
1954 struct ieee80211_sub_if_data *sdata =
1955 (struct ieee80211_sub_if_data *) data;
1956 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1957 struct ieee80211_local *local = sdata->local;
1959 if (local->quiescing)
1962 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
1965 static void ieee80211_sta_monitor_work(struct work_struct *work)
1967 struct ieee80211_sub_if_data *sdata =
1968 container_of(work, struct ieee80211_sub_if_data,
1969 u.mgd.monitor_work);
1971 ieee80211_mgd_probe_ap(sdata, false);
1974 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
1976 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1977 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
1978 IEEE80211_STA_CONNECTION_POLL);
1980 /* let's probe the connection once */
1981 ieee80211_queue_work(&sdata->local->hw,
1982 &sdata->u.mgd.monitor_work);
1983 /* and do all the other regular work too */
1984 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1989 void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
1991 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1994 * we need to use atomic bitops for the running bits
1995 * only because both timers might fire at the same
1996 * time -- the code here is properly synchronised.
1999 cancel_work_sync(&ifmgd->request_smps_work);
2001 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
2002 if (del_timer_sync(&ifmgd->timer))
2003 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2005 cancel_work_sync(&ifmgd->chswitch_work);
2006 if (del_timer_sync(&ifmgd->chswitch_timer))
2007 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
2009 cancel_work_sync(&ifmgd->monitor_work);
2010 /* these will just be re-established on connection */
2011 del_timer_sync(&ifmgd->conn_mon_timer);
2012 del_timer_sync(&ifmgd->bcn_mon_timer);
2015 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2017 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2019 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2020 add_timer(&ifmgd->timer);
2021 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2022 add_timer(&ifmgd->chswitch_timer);
2023 ieee80211_sta_reset_beacon_monitor(sdata);
2024 ieee80211_restart_sta_timer(sdata);
2028 /* interface setup */
2029 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2031 struct ieee80211_if_managed *ifmgd;
2033 ifmgd = &sdata->u.mgd;
2034 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
2035 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
2036 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2037 ieee80211_beacon_connection_loss_work);
2038 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
2039 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
2040 (unsigned long) sdata);
2041 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2042 (unsigned long) sdata);
2043 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2044 (unsigned long) sdata);
2045 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
2046 (unsigned long) sdata);
2050 mutex_init(&ifmgd->mtx);
2052 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2053 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2055 ifmgd->req_smps = IEEE80211_SMPS_OFF;
2058 /* scan finished notification */
2059 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2061 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2063 /* Restart STA timers */
2065 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2066 ieee80211_restart_sta_timer(sdata);
2070 int ieee80211_max_network_latency(struct notifier_block *nb,
2071 unsigned long data, void *dummy)
2073 s32 latency_usec = (s32) data;
2074 struct ieee80211_local *local =
2075 container_of(nb, struct ieee80211_local,
2076 network_latency_notifier);
2078 mutex_lock(&local->iflist_mtx);
2079 ieee80211_recalc_ps(local, latency_usec);
2080 mutex_unlock(&local->iflist_mtx);
2086 static enum work_done_result
2087 ieee80211_probe_auth_done(struct ieee80211_work *wk,
2088 struct sk_buff *skb)
2091 cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
2092 return WORK_DONE_DESTROY;
2095 if (wk->type == IEEE80211_WORK_AUTH) {
2096 cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
2097 return WORK_DONE_DESTROY;
2100 mutex_lock(&wk->sdata->u.mgd.mtx);
2101 ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
2102 mutex_unlock(&wk->sdata->u.mgd.mtx);
2104 wk->type = IEEE80211_WORK_AUTH;
2105 wk->probe_auth.tries = 0;
2106 return WORK_DONE_REQUEUE;
2109 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2110 struct cfg80211_auth_request *req)
2113 struct ieee80211_work *wk;
2116 if (req->local_state_change)
2117 return 0; /* no need to update mac80211 state */
2119 switch (req->auth_type) {
2120 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2121 auth_alg = WLAN_AUTH_OPEN;
2123 case NL80211_AUTHTYPE_SHARED_KEY:
2124 if (IS_ERR(sdata->local->wep_tx_tfm))
2126 auth_alg = WLAN_AUTH_SHARED_KEY;
2128 case NL80211_AUTHTYPE_FT:
2129 auth_alg = WLAN_AUTH_FT;
2131 case NL80211_AUTHTYPE_NETWORK_EAP:
2132 auth_alg = WLAN_AUTH_LEAP;
2138 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2142 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
2144 if (req->ie && req->ie_len) {
2145 memcpy(wk->ie, req->ie, req->ie_len);
2146 wk->ie_len = req->ie_len;
2149 if (req->key && req->key_len) {
2150 wk->probe_auth.key_len = req->key_len;
2151 wk->probe_auth.key_idx = req->key_idx;
2152 memcpy(wk->probe_auth.key, req->key, req->key_len);
2155 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2156 memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
2157 wk->probe_auth.ssid_len = ssid[1];
2159 wk->probe_auth.algorithm = auth_alg;
2160 wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
2162 /* if we already have a probe, don't probe again */
2163 if (req->bss->proberesp_ies)
2164 wk->type = IEEE80211_WORK_AUTH;
2166 wk->type = IEEE80211_WORK_DIRECT_PROBE;
2167 wk->chan = req->bss->channel;
2169 wk->done = ieee80211_probe_auth_done;
2171 ieee80211_add_work(wk);
2175 static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
2176 struct sk_buff *skb)
2178 struct ieee80211_mgmt *mgmt;
2179 struct ieee80211_rx_status *rx_status;
2180 struct ieee802_11_elems elems;
2184 cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
2185 return WORK_DONE_DESTROY;
2188 if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
2189 mutex_lock(&wk->sdata->u.mgd.mtx);
2190 rx_status = (void *) skb->cb;
2191 ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
2192 ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
2194 mutex_unlock(&wk->sdata->u.mgd.mtx);
2196 wk->type = IEEE80211_WORK_ASSOC;
2197 /* not really done yet */
2198 return WORK_DONE_REQUEUE;
2201 mgmt = (void *)skb->data;
2202 status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2204 if (status == WLAN_STATUS_SUCCESS) {
2205 mutex_lock(&wk->sdata->u.mgd.mtx);
2206 if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
2207 mutex_unlock(&wk->sdata->u.mgd.mtx);
2208 /* oops -- internal error -- send timeout for now */
2209 cfg80211_send_assoc_timeout(wk->sdata->dev,
2211 return WORK_DONE_DESTROY;
2214 mutex_unlock(&wk->sdata->u.mgd.mtx);
2217 cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
2218 return WORK_DONE_DESTROY;
2221 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2222 struct cfg80211_assoc_request *req)
2224 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2225 struct ieee80211_bss *bss = (void *)req->bss->priv;
2226 struct ieee80211_work *wk;
2230 mutex_lock(&ifmgd->mtx);
2231 if (ifmgd->associated) {
2232 if (!req->prev_bssid ||
2233 memcmp(req->prev_bssid, ifmgd->associated->bssid,
2236 * We are already associated and the request was not a
2237 * reassociation request from the current BSS, so
2240 mutex_unlock(&ifmgd->mtx);
2244 /* Trying to reassociate - clear previous association state */
2245 ieee80211_set_disassoc(sdata, true, false);
2247 mutex_unlock(&ifmgd->mtx);
2249 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2253 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
2254 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
2256 ifmgd->beacon_crc_valid = false;
2258 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2259 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2260 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2261 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2262 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2265 if (req->ie && req->ie_len) {
2266 memcpy(wk->ie, req->ie, req->ie_len);
2267 wk->ie_len = req->ie_len;
2271 wk->assoc.bss = req->bss;
2273 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
2275 /* new association always uses requested smps mode */
2276 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
2277 if (ifmgd->powersave)
2278 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
2280 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
2282 ifmgd->ap_smps = ifmgd->req_smps;
2284 wk->assoc.smps = ifmgd->ap_smps;
2286 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
2287 * We still associate in non-HT mode (11a/b/g) if any one of these
2288 * ciphers is configured as pairwise.
2289 * We can set this to true for non-11n hardware, that'll be checked
2290 * separately along with the peer capabilities.
2292 wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
2293 wk->assoc.capability = req->bss->capability;
2294 wk->assoc.wmm_used = bss->wmm_used;
2295 wk->assoc.supp_rates = bss->supp_rates;
2296 wk->assoc.supp_rates_len = bss->supp_rates_len;
2297 wk->assoc.ht_information_ie =
2298 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
2300 if (bss->wmm_used && bss->uapsd_supported &&
2301 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
2302 wk->assoc.uapsd_used = true;
2303 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
2305 wk->assoc.uapsd_used = false;
2306 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
2309 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2310 memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
2311 wk->assoc.ssid_len = ssid[1];
2313 if (req->prev_bssid)
2314 memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
2316 wk->chan = req->bss->channel;
2318 wk->done = ieee80211_assoc_done;
2319 if (!bss->dtim_period &&
2320 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
2321 wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
2323 wk->type = IEEE80211_WORK_ASSOC;
2326 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2327 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2329 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2330 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2333 if (req->crypto.control_port)
2334 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2336 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2338 sdata->control_port_protocol = req->crypto.control_port_ethertype;
2339 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
2341 ieee80211_add_work(wk);
2345 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
2346 struct cfg80211_deauth_request *req,
2349 struct ieee80211_local *local = sdata->local;
2350 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2351 struct ieee80211_work *wk;
2353 bool assoc_bss = false;
2355 mutex_lock(&ifmgd->mtx);
2357 memcpy(bssid, req->bss->bssid, ETH_ALEN);
2358 if (ifmgd->associated == req->bss) {
2359 ieee80211_set_disassoc(sdata, false, true);
2360 mutex_unlock(&ifmgd->mtx);
2363 bool not_auth_yet = false;
2365 mutex_unlock(&ifmgd->mtx);
2367 mutex_lock(&local->mtx);
2368 list_for_each_entry(wk, &local->work_list, list) {
2369 if (wk->sdata != sdata)
2372 if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
2373 wk->type != IEEE80211_WORK_AUTH &&
2374 wk->type != IEEE80211_WORK_ASSOC &&
2375 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
2378 if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
2381 not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
2382 list_del_rcu(&wk->list);
2386 mutex_unlock(&local->mtx);
2389 * If somebody requests authentication and we haven't
2390 * sent out an auth frame yet there's no need to send
2391 * out a deauth frame either. If the state was PROBE,
2392 * then this is the case. If it's AUTH we have sent a
2393 * frame, and if it's IDLE we have completed the auth
2397 __cfg80211_auth_canceled(sdata->dev, bssid);
2402 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
2403 sdata->name, bssid, req->reason_code);
2405 ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
2406 req->reason_code, cookie,
2407 !req->local_state_change);
2409 sta_info_destroy_addr(sdata, bssid);
2411 mutex_lock(&sdata->local->mtx);
2412 ieee80211_recalc_idle(sdata->local);
2413 mutex_unlock(&sdata->local->mtx);
2418 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
2419 struct cfg80211_disassoc_request *req,
2422 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2425 mutex_lock(&ifmgd->mtx);
2428 * cfg80211 should catch this ... but it's racy since
2429 * we can receive a disassoc frame, process it, hand it
2430 * to cfg80211 while that's in a locked section already
2431 * trying to tell us that the user wants to disconnect.
2433 if (ifmgd->associated != req->bss) {
2434 mutex_unlock(&ifmgd->mtx);
2438 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
2439 sdata->name, req->bss->bssid, req->reason_code);
2441 memcpy(bssid, req->bss->bssid, ETH_ALEN);
2442 ieee80211_set_disassoc(sdata, false, true);
2444 mutex_unlock(&ifmgd->mtx);
2446 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
2447 IEEE80211_STYPE_DISASSOC, req->reason_code,
2448 cookie, !req->local_state_change);
2449 sta_info_destroy_addr(sdata, bssid);
2451 mutex_lock(&sdata->local->mtx);
2452 ieee80211_recalc_idle(sdata->local);
2453 mutex_unlock(&sdata->local->mtx);
2458 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
2459 enum nl80211_cqm_rssi_threshold_event rssi_event,
2462 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2464 trace_api_cqm_rssi_notify(sdata, rssi_event);
2466 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
2468 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);