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_NULLFUNC_TRIES 2
32 #define IEEE80211_MAX_PROBE_TRIES 5
35 * Beacon loss timeout is calculated as N frames times the
36 * advertised beacon interval. This may need to be somewhat
37 * higher than what hardware might detect to account for
38 * delays in the host processing frames. But since we also
39 * probe on beacon miss before declaring the connection lost
40 * default to what we want.
42 #define IEEE80211_BEACON_LOSS_COUNT 7
45 * Time the connection can be idle before we probe
46 * it to see if we can still talk to the AP.
48 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
50 * Time we wait for a probe response after sending
51 * a probe request because of beacon loss or for
52 * checking the connection still works.
54 #define IEEE80211_PROBE_WAIT (HZ / 2)
57 * Weight given to the latest Beacon frame when calculating average signal
58 * strength for Beacon frames received in the current BSS. This must be
61 #define IEEE80211_SIGNAL_AVE_WEIGHT 3
64 * How many Beacon frames need to have been used in average signal strength
65 * before starting to indicate signal change events.
67 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
69 #define TMR_RUNNING_TIMER 0
70 #define TMR_RUNNING_CHANSW 1
73 * All cfg80211 functions have to be called outside a locked
74 * section so that they can acquire a lock themselves... This
75 * is much simpler than queuing up things in cfg80211, but we
76 * do need some indirection for that here.
79 /* no action required */
82 /* caller must call cfg80211_send_rx_auth() */
83 RX_MGMT_CFG80211_AUTH,
85 /* caller must call cfg80211_send_rx_assoc() */
86 RX_MGMT_CFG80211_ASSOC,
88 /* caller must call cfg80211_send_deauth() */
89 RX_MGMT_CFG80211_DEAUTH,
91 /* caller must call cfg80211_send_disassoc() */
92 RX_MGMT_CFG80211_DISASSOC,
94 /* caller must tell cfg80211 about internal error */
95 RX_MGMT_CFG80211_ASSOC_ERROR,
99 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
101 lockdep_assert_held(&ifmgd->mtx);
105 * We can have multiple work items (and connection probing)
106 * scheduling this timer, but we need to take care to only
107 * reschedule it when it should fire _earlier_ than it was
108 * asked for before, or if it's not pending right now. This
109 * function ensures that. Note that it then is required to
110 * run this function for all timeouts after the first one
111 * has happened -- the work that runs from this timer will
114 static void run_again(struct ieee80211_if_managed *ifmgd,
115 unsigned long timeout)
117 ASSERT_MGD_MTX(ifmgd);
119 if (!timer_pending(&ifmgd->timer) ||
120 time_before(timeout, ifmgd->timer.expires))
121 mod_timer(&ifmgd->timer, timeout);
124 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
126 if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
129 mod_timer(&sdata->u.mgd.bcn_mon_timer,
130 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
133 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
135 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
137 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
140 mod_timer(&sdata->u.mgd.conn_mon_timer,
141 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
143 ifmgd->probe_send_count = 0;
146 static int ecw2cw(int ecw)
148 return (1 << ecw) - 1;
152 * ieee80211_enable_ht should be called only after the operating band
153 * has been determined as ht configuration depends on the hw's
154 * HT abilities for a specific band.
156 static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
157 struct ieee80211_ht_info *hti,
158 const u8 *bssid, u16 ap_ht_cap_flags)
160 struct ieee80211_local *local = sdata->local;
161 struct ieee80211_supported_band *sband;
162 struct sta_info *sta;
165 bool enable_ht = true;
166 enum nl80211_channel_type prev_chantype;
167 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
169 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
171 prev_chantype = sdata->vif.bss_conf.channel_type;
173 /* HT is not supported */
174 if (!sband->ht_cap.ht_supported)
177 /* check that channel matches the right operating channel */
178 if (local->hw.conf.channel->center_freq !=
179 ieee80211_channel_to_frequency(hti->control_chan, sband->band))
183 channel_type = NL80211_CHAN_HT20;
185 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
186 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
187 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
188 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
189 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
190 if (!(local->hw.conf.channel->flags &
191 IEEE80211_CHAN_NO_HT40PLUS))
192 channel_type = NL80211_CHAN_HT40PLUS;
194 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
195 if (!(local->hw.conf.channel->flags &
196 IEEE80211_CHAN_NO_HT40MINUS))
197 channel_type = NL80211_CHAN_HT40MINUS;
203 if (local->tmp_channel)
204 local->tmp_channel_type = channel_type;
206 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
207 /* can only fail due to HT40+/- mismatch */
208 channel_type = NL80211_CHAN_HT20;
209 WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
212 /* channel_type change automatically detected */
213 ieee80211_hw_config(local, 0);
215 if (prev_chantype != channel_type) {
217 sta = sta_info_get(sdata, bssid);
219 rate_control_rate_update(local, sband, sta,
220 IEEE80211_RC_HT_CHANGED,
225 ht_opmode = le16_to_cpu(hti->operation_mode);
227 /* if bss configuration changed store the new one */
228 if (sdata->ht_opmode_valid != enable_ht ||
229 sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
230 prev_chantype != channel_type) {
231 changed |= BSS_CHANGED_HT;
232 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
233 sdata->ht_opmode_valid = enable_ht;
239 /* frame sending functions */
241 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
242 const u8 *bssid, u16 stype, u16 reason,
243 void *cookie, bool send_frame)
245 struct ieee80211_local *local = sdata->local;
246 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
248 struct ieee80211_mgmt *mgmt;
250 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
252 printk(KERN_DEBUG "%s: failed to allocate buffer for "
253 "deauth/disassoc frame\n", sdata->name);
256 skb_reserve(skb, local->hw.extra_tx_headroom);
258 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
260 memcpy(mgmt->da, bssid, ETH_ALEN);
261 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
262 memcpy(mgmt->bssid, bssid, ETH_ALEN);
263 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
265 /* u.deauth.reason_code == u.disassoc.reason_code */
266 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
268 if (stype == IEEE80211_STYPE_DEAUTH)
270 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
272 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
275 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
277 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
278 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
279 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
282 ieee80211_tx_skb(sdata, skb);
287 void ieee80211_send_pspoll(struct ieee80211_local *local,
288 struct ieee80211_sub_if_data *sdata)
290 struct ieee80211_pspoll *pspoll;
293 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
297 pspoll = (struct ieee80211_pspoll *) skb->data;
298 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
300 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
301 ieee80211_tx_skb(sdata, skb);
304 void ieee80211_send_nullfunc(struct ieee80211_local *local,
305 struct ieee80211_sub_if_data *sdata,
309 struct ieee80211_hdr_3addr *nullfunc;
311 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
315 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
317 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
319 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
320 ieee80211_tx_skb(sdata, skb);
323 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
324 struct ieee80211_sub_if_data *sdata)
327 struct ieee80211_hdr *nullfunc;
330 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
333 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
335 printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
336 "nullfunc frame\n", sdata->name);
339 skb_reserve(skb, local->hw.extra_tx_headroom);
341 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
342 memset(nullfunc, 0, 30);
343 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
344 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
345 nullfunc->frame_control = fc;
346 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
347 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
348 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
349 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
351 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
352 ieee80211_tx_skb(sdata, skb);
355 /* spectrum management related things */
356 static void ieee80211_chswitch_work(struct work_struct *work)
358 struct ieee80211_sub_if_data *sdata =
359 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
360 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
362 if (!ieee80211_sdata_running(sdata))
365 mutex_lock(&ifmgd->mtx);
366 if (!ifmgd->associated)
369 sdata->local->oper_channel = sdata->local->csa_channel;
370 if (!sdata->local->ops->channel_switch) {
371 /* call "hw_config" only if doing sw channel switch */
372 ieee80211_hw_config(sdata->local,
373 IEEE80211_CONF_CHANGE_CHANNEL);
376 /* XXX: shouldn't really modify cfg80211-owned data! */
377 ifmgd->associated->channel = sdata->local->oper_channel;
379 ieee80211_wake_queues_by_reason(&sdata->local->hw,
380 IEEE80211_QUEUE_STOP_REASON_CSA);
382 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
383 mutex_unlock(&ifmgd->mtx);
386 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
388 struct ieee80211_sub_if_data *sdata;
389 struct ieee80211_if_managed *ifmgd;
391 sdata = vif_to_sdata(vif);
392 ifmgd = &sdata->u.mgd;
394 trace_api_chswitch_done(sdata, success);
397 * If the channel switch was not successful, stay
398 * around on the old channel. We currently lack
399 * good handling of this situation, possibly we
400 * should just drop the association.
402 sdata->local->csa_channel = sdata->local->oper_channel;
405 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
407 EXPORT_SYMBOL(ieee80211_chswitch_done);
409 static void ieee80211_chswitch_timer(unsigned long data)
411 struct ieee80211_sub_if_data *sdata =
412 (struct ieee80211_sub_if_data *) data;
413 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
415 if (sdata->local->quiescing) {
416 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
420 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
423 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
424 struct ieee80211_channel_sw_ie *sw_elem,
425 struct ieee80211_bss *bss,
428 struct cfg80211_bss *cbss =
429 container_of((void *)bss, struct cfg80211_bss, priv);
430 struct ieee80211_channel *new_ch;
431 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
432 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
433 cbss->channel->band);
435 ASSERT_MGD_MTX(ifmgd);
437 if (!ifmgd->associated)
440 if (sdata->local->scanning)
443 /* Disregard subsequent beacons if we are already running a timer
446 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
449 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
450 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
453 sdata->local->csa_channel = new_ch;
455 if (sdata->local->ops->channel_switch) {
456 /* use driver's channel switch callback */
457 struct ieee80211_channel_switch ch_switch;
458 memset(&ch_switch, 0, sizeof(ch_switch));
459 ch_switch.timestamp = timestamp;
461 ch_switch.block_tx = true;
462 ieee80211_stop_queues_by_reason(&sdata->local->hw,
463 IEEE80211_QUEUE_STOP_REASON_CSA);
465 ch_switch.channel = new_ch;
466 ch_switch.count = sw_elem->count;
467 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
468 drv_channel_switch(sdata->local, &ch_switch);
472 /* channel switch handled in software */
473 if (sw_elem->count <= 1) {
474 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
477 ieee80211_stop_queues_by_reason(&sdata->local->hw,
478 IEEE80211_QUEUE_STOP_REASON_CSA);
479 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
480 mod_timer(&ifmgd->chswitch_timer,
482 msecs_to_jiffies(sw_elem->count *
483 cbss->beacon_interval));
487 static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
488 u16 capab_info, u8 *pwr_constr_elem,
489 u8 pwr_constr_elem_len)
491 struct ieee80211_conf *conf = &sdata->local->hw.conf;
493 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
496 /* Power constraint IE length should be 1 octet */
497 if (pwr_constr_elem_len != 1)
500 if ((*pwr_constr_elem <= conf->channel->max_power) &&
501 (*pwr_constr_elem != sdata->local->power_constr_level)) {
502 sdata->local->power_constr_level = *pwr_constr_elem;
503 ieee80211_hw_config(sdata->local, 0);
507 void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
509 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
510 struct ieee80211_local *local = sdata->local;
511 struct ieee80211_conf *conf = &local->hw.conf;
513 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
514 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
515 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
517 local->disable_dynamic_ps = false;
518 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
520 EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
522 void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
524 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
525 struct ieee80211_local *local = sdata->local;
526 struct ieee80211_conf *conf = &local->hw.conf;
528 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
529 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
530 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
532 local->disable_dynamic_ps = true;
533 conf->dynamic_ps_timeout = 0;
534 del_timer_sync(&local->dynamic_ps_timer);
535 ieee80211_queue_work(&local->hw,
536 &local->dynamic_ps_enable_work);
538 EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
541 static void ieee80211_enable_ps(struct ieee80211_local *local,
542 struct ieee80211_sub_if_data *sdata)
544 struct ieee80211_conf *conf = &local->hw.conf;
547 * If we are scanning right now then the parameters will
548 * take effect when scan finishes.
553 if (conf->dynamic_ps_timeout > 0 &&
554 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
555 mod_timer(&local->dynamic_ps_timer, jiffies +
556 msecs_to_jiffies(conf->dynamic_ps_timeout));
558 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
559 ieee80211_send_nullfunc(local, sdata, 1);
561 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
562 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
565 conf->flags |= IEEE80211_CONF_PS;
566 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
570 static void ieee80211_change_ps(struct ieee80211_local *local)
572 struct ieee80211_conf *conf = &local->hw.conf;
574 if (local->ps_sdata) {
575 ieee80211_enable_ps(local, local->ps_sdata);
576 } else if (conf->flags & IEEE80211_CONF_PS) {
577 conf->flags &= ~IEEE80211_CONF_PS;
578 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
579 del_timer_sync(&local->dynamic_ps_timer);
580 cancel_work_sync(&local->dynamic_ps_enable_work);
584 /* need to hold RTNL or interface lock */
585 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
587 struct ieee80211_sub_if_data *sdata, *found = NULL;
591 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
592 local->ps_sdata = NULL;
596 if (!list_empty(&local->work_list)) {
597 local->ps_sdata = NULL;
601 list_for_each_entry(sdata, &local->interfaces, list) {
602 if (!ieee80211_sdata_running(sdata))
604 if (sdata->vif.type == NL80211_IFTYPE_AP) {
605 /* If an AP vif is found, then disable PS
606 * by setting the count to zero thereby setting
612 if (sdata->vif.type != NL80211_IFTYPE_STATION)
618 if (count == 1 && found->u.mgd.powersave &&
619 found->u.mgd.associated &&
620 found->u.mgd.associated->beacon_ies &&
621 !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
622 IEEE80211_STA_CONNECTION_POLL))) {
623 struct ieee80211_conf *conf = &local->hw.conf;
627 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
629 beaconint_us = ieee80211_tu_to_usec(
630 found->vif.bss_conf.beacon_int);
632 timeout = local->dynamic_ps_forced_timeout;
635 * Go to full PSM if the user configures a very low
636 * latency requirement.
637 * The 2000 second value is there for compatibility
638 * until the PM_QOS_NETWORK_LATENCY is configured
641 if (latency > (1900 * USEC_PER_MSEC) &&
642 latency != (2000 * USEC_PER_SEC))
647 local->dynamic_ps_user_timeout = timeout;
648 if (!local->disable_dynamic_ps)
649 conf->dynamic_ps_timeout =
650 local->dynamic_ps_user_timeout;
652 if (beaconint_us > latency) {
653 local->ps_sdata = NULL;
655 struct ieee80211_bss *bss;
659 bss = (void *)found->u.mgd.associated->priv;
660 dtimper = bss->dtim_period;
662 /* If the TIM IE is invalid, pretend the value is 1 */
665 else if (dtimper > 1)
666 maxslp = min_t(int, dtimper,
667 latency / beaconint_us);
669 local->hw.conf.max_sleep_period = maxslp;
670 local->hw.conf.ps_dtim_period = dtimper;
671 local->ps_sdata = found;
674 local->ps_sdata = NULL;
678 ieee80211_change_ps(local);
681 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
683 struct ieee80211_local *local =
684 container_of(work, struct ieee80211_local,
685 dynamic_ps_disable_work);
687 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
688 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
689 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
692 ieee80211_wake_queues_by_reason(&local->hw,
693 IEEE80211_QUEUE_STOP_REASON_PS);
696 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
698 struct ieee80211_local *local =
699 container_of(work, struct ieee80211_local,
700 dynamic_ps_enable_work);
701 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
702 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
704 /* can only happen when PS was just disabled anyway */
708 if (local->hw.conf.flags & IEEE80211_CONF_PS)
711 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
712 (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
713 ieee80211_send_nullfunc(local, sdata, 1);
715 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
716 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
717 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
718 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
719 local->hw.conf.flags |= IEEE80211_CONF_PS;
720 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
724 void ieee80211_dynamic_ps_timer(unsigned long data)
726 struct ieee80211_local *local = (void *) data;
728 if (local->quiescing || local->suspended)
731 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
735 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
736 struct ieee80211_sub_if_data *sdata,
737 u8 *wmm_param, size_t wmm_param_len)
739 struct ieee80211_tx_queue_params params;
740 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
743 u8 *pos, uapsd_queues = 0;
745 if (!local->ops->conf_tx)
748 if (local->hw.queues < 4)
754 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
757 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
758 uapsd_queues = local->uapsd_queues;
760 count = wmm_param[6] & 0x0f;
761 if (count == ifmgd->wmm_last_param_set)
763 ifmgd->wmm_last_param_set = count;
766 left = wmm_param_len - 8;
768 memset(¶ms, 0, sizeof(params));
771 for (; left >= 4; left -= 4, pos += 4) {
772 int aci = (pos[0] >> 5) & 0x03;
773 int acm = (pos[0] >> 4) & 0x01;
781 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
782 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
788 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
789 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
795 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
796 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
803 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
804 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
809 params.aifs = pos[0] & 0x0f;
810 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
811 params.cw_min = ecw2cw(pos[1] & 0x0f);
812 params.txop = get_unaligned_le16(pos + 2);
813 params.uapsd = uapsd;
815 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
816 wiphy_debug(local->hw.wiphy,
817 "WMM queue=%d aci=%d acm=%d aifs=%d "
818 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
820 params.aifs, params.cw_min, params.cw_max,
821 params.txop, params.uapsd);
823 if (drv_conf_tx(local, queue, ¶ms))
824 wiphy_debug(local->hw.wiphy,
825 "failed to set TX queue parameters for queue %d\n",
829 /* enable WMM or activate new settings */
830 sdata->vif.bss_conf.qos = true;
831 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
834 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
835 u16 capab, bool erp_valid, u8 erp)
837 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
840 bool use_short_preamble;
844 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
845 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
847 use_protection = false;
848 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
851 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
852 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
853 use_short_slot = true;
855 if (use_protection != bss_conf->use_cts_prot) {
856 bss_conf->use_cts_prot = use_protection;
857 changed |= BSS_CHANGED_ERP_CTS_PROT;
860 if (use_short_preamble != bss_conf->use_short_preamble) {
861 bss_conf->use_short_preamble = use_short_preamble;
862 changed |= BSS_CHANGED_ERP_PREAMBLE;
865 if (use_short_slot != bss_conf->use_short_slot) {
866 bss_conf->use_short_slot = use_short_slot;
867 changed |= BSS_CHANGED_ERP_SLOT;
873 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
874 struct cfg80211_bss *cbss,
875 u32 bss_info_changed)
877 struct ieee80211_bss *bss = (void *)cbss->priv;
878 struct ieee80211_local *local = sdata->local;
879 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
881 bss_info_changed |= BSS_CHANGED_ASSOC;
882 /* set timing information */
883 bss_conf->beacon_int = cbss->beacon_interval;
884 bss_conf->timestamp = cbss->tsf;
886 bss_info_changed |= BSS_CHANGED_BEACON_INT;
887 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
888 cbss->capability, bss->has_erp_value, bss->erp_value);
890 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
891 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
893 sdata->u.mgd.associated = cbss;
894 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
896 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
898 /* just to be sure */
899 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
900 IEEE80211_STA_BEACON_POLL);
902 ieee80211_led_assoc(local, 1);
904 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
905 bss_conf->dtim_period = bss->dtim_period;
907 bss_conf->dtim_period = 0;
911 * For now just always ask the driver to update the basic rateset
912 * when we have associated, we aren't checking whether it actually
915 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
917 /* And the BSSID changed - we're associated now */
918 bss_info_changed |= BSS_CHANGED_BSSID;
920 /* Tell the driver to monitor connection quality (if supported) */
921 if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
922 bss_conf->cqm_rssi_thold)
923 bss_info_changed |= BSS_CHANGED_CQM;
925 /* Enable ARP filtering */
926 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
927 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
928 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
931 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
933 mutex_lock(&local->iflist_mtx);
934 ieee80211_recalc_ps(local, -1);
935 ieee80211_recalc_smps(local);
936 mutex_unlock(&local->iflist_mtx);
938 netif_tx_start_all_queues(sdata->dev);
939 netif_carrier_on(sdata->dev);
942 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
943 bool remove_sta, bool tx)
945 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
946 struct ieee80211_local *local = sdata->local;
947 struct sta_info *sta;
948 u32 changed = 0, config_changed = 0;
951 ASSERT_MGD_MTX(ifmgd);
953 if (WARN_ON(!ifmgd->associated))
956 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
958 ifmgd->associated = NULL;
959 memset(ifmgd->bssid, 0, ETH_ALEN);
962 * we need to commit the associated = NULL change because the
963 * scan code uses that to determine whether this iface should
964 * go to/wake up from powersave or not -- and could otherwise
965 * wake the queues erroneously.
970 * Thus, we can only afterwards stop the queues -- to account
971 * for the case where another CPU is finishing a scan at this
972 * time -- we don't want the scan code to enable queues.
975 netif_tx_stop_all_queues(sdata->dev);
976 netif_carrier_off(sdata->dev);
978 mutex_lock(&local->sta_mtx);
979 sta = sta_info_get(sdata, bssid);
981 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
982 ieee80211_sta_tear_down_BA_sessions(sta, tx);
984 mutex_unlock(&local->sta_mtx);
986 changed |= ieee80211_reset_erp_info(sdata);
988 ieee80211_led_assoc(local, 0);
989 changed |= BSS_CHANGED_ASSOC;
990 sdata->vif.bss_conf.assoc = false;
992 ieee80211_set_wmm_default(sdata);
994 /* channel(_type) changes are handled by ieee80211_hw_config */
995 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
997 /* on the next assoc, re-program HT parameters */
998 sdata->ht_opmode_valid = false;
1000 local->power_constr_level = 0;
1002 del_timer_sync(&local->dynamic_ps_timer);
1003 cancel_work_sync(&local->dynamic_ps_enable_work);
1005 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1006 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1007 config_changed |= IEEE80211_CONF_CHANGE_PS;
1010 ieee80211_hw_config(local, config_changed);
1012 /* Disable ARP filtering */
1013 if (sdata->vif.bss_conf.arp_filter_enabled) {
1014 sdata->vif.bss_conf.arp_filter_enabled = false;
1015 changed |= BSS_CHANGED_ARP_FILTER;
1018 /* The BSSID (not really interesting) and HT changed */
1019 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1020 ieee80211_bss_info_change_notify(sdata, changed);
1023 sta_info_destroy_addr(sdata, bssid);
1025 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1026 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1027 del_timer_sync(&sdata->u.mgd.timer);
1028 del_timer_sync(&sdata->u.mgd.chswitch_timer);
1031 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1032 struct ieee80211_hdr *hdr)
1035 * We can postpone the mgd.timer whenever receiving unicast frames
1036 * from AP because we know that the connection is working both ways
1037 * at that time. But multicast frames (and hence also beacons) must
1038 * be ignored here, because we need to trigger the timer during
1039 * data idle periods for sending the periodic probe request to the
1040 * AP we're connected to.
1042 if (is_multicast_ether_addr(hdr->addr1))
1045 ieee80211_sta_reset_conn_monitor(sdata);
1048 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1050 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1052 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1053 IEEE80211_STA_CONNECTION_POLL)))
1056 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1057 IEEE80211_STA_BEACON_POLL);
1058 mutex_lock(&sdata->local->iflist_mtx);
1059 ieee80211_recalc_ps(sdata->local, -1);
1060 mutex_unlock(&sdata->local->iflist_mtx);
1062 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1066 * We've received a probe response, but are not sure whether
1067 * we have or will be receiving any beacons or data, so let's
1068 * schedule the timers again, just in case.
1070 ieee80211_sta_reset_beacon_monitor(sdata);
1072 mod_timer(&ifmgd->conn_mon_timer,
1073 round_jiffies_up(jiffies +
1074 IEEE80211_CONNECTION_IDLE_TIME));
1077 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
1078 struct ieee80211_hdr *hdr, bool ack)
1080 if (!ieee80211_is_data(hdr->frame_control))
1084 ieee80211_sta_reset_conn_monitor(sdata);
1086 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1087 sdata->u.mgd.probe_send_count > 0) {
1089 sdata->u.mgd.probe_send_count = 0;
1091 sdata->u.mgd.nullfunc_failed = true;
1092 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1096 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1098 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1100 u8 *dst = ifmgd->associated->bssid;
1101 u8 unicast_limit = max(1, IEEE80211_MAX_PROBE_TRIES - 3);
1104 * Try sending broadcast probe requests for the last three
1105 * probe requests after the first ones failed since some
1106 * buggy APs only support broadcast probe requests.
1108 if (ifmgd->probe_send_count >= unicast_limit)
1112 * When the hardware reports an accurate Tx ACK status, it's
1113 * better to send a nullfunc frame instead of a probe request,
1114 * as it will kick us off the AP quickly if we aren't associated
1115 * anymore. The timeout will be reset if the frame is ACKed by
1118 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1119 ifmgd->nullfunc_failed = false;
1120 ieee80211_send_nullfunc(sdata->local, sdata, 0);
1122 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1123 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
1126 ifmgd->probe_send_count++;
1127 ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1128 run_again(ifmgd, ifmgd->probe_timeout);
1131 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1134 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1135 bool already = false;
1137 if (!ieee80211_sdata_running(sdata))
1140 if (sdata->local->scanning)
1143 if (sdata->local->tmp_channel)
1146 mutex_lock(&ifmgd->mtx);
1148 if (!ifmgd->associated)
1151 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1152 if (beacon && net_ratelimit())
1153 printk(KERN_DEBUG "%s: detected beacon loss from AP "
1154 "- sending probe request\n", sdata->name);
1158 * The driver/our work has already reported this event or the
1159 * connection monitoring has kicked in and we have already sent
1160 * a probe request. Or maybe the AP died and the driver keeps
1161 * reporting until we disassociate...
1163 * In either case we have to ignore the current call to this
1164 * function (except for setting the correct probe reason bit)
1165 * because otherwise we would reset the timer every time and
1166 * never check whether we received a probe response!
1168 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1169 IEEE80211_STA_CONNECTION_POLL))
1173 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1175 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1180 mutex_lock(&sdata->local->iflist_mtx);
1181 ieee80211_recalc_ps(sdata->local, -1);
1182 mutex_unlock(&sdata->local->iflist_mtx);
1184 ifmgd->probe_send_count = 0;
1185 ieee80211_mgd_probe_ap_send(sdata);
1187 mutex_unlock(&ifmgd->mtx);
1190 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1191 struct ieee80211_vif *vif)
1193 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1194 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1195 struct sk_buff *skb;
1198 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1201 ASSERT_MGD_MTX(ifmgd);
1203 if (!ifmgd->associated)
1206 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1207 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
1208 ssid + 2, ssid[1], NULL, 0);
1212 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1214 static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1216 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1217 struct ieee80211_local *local = sdata->local;
1220 mutex_lock(&ifmgd->mtx);
1221 if (!ifmgd->associated) {
1222 mutex_unlock(&ifmgd->mtx);
1226 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1228 printk(KERN_DEBUG "Connection to AP %pM lost.\n", bssid);
1230 ieee80211_set_disassoc(sdata, true, true);
1231 mutex_unlock(&ifmgd->mtx);
1233 mutex_lock(&local->mtx);
1234 ieee80211_recalc_idle(local);
1235 mutex_unlock(&local->mtx);
1237 * must be outside lock due to cfg80211,
1238 * but that's not a problem.
1240 ieee80211_send_deauth_disassoc(sdata, bssid,
1241 IEEE80211_STYPE_DEAUTH,
1242 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1246 void ieee80211_beacon_connection_loss_work(struct work_struct *work)
1248 struct ieee80211_sub_if_data *sdata =
1249 container_of(work, struct ieee80211_sub_if_data,
1250 u.mgd.beacon_connection_loss_work);
1252 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1253 __ieee80211_connection_loss(sdata);
1255 ieee80211_mgd_probe_ap(sdata, true);
1258 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1260 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1261 struct ieee80211_hw *hw = &sdata->local->hw;
1263 trace_api_beacon_loss(sdata);
1265 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1266 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1268 EXPORT_SYMBOL(ieee80211_beacon_loss);
1270 void ieee80211_connection_loss(struct ieee80211_vif *vif)
1272 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1273 struct ieee80211_hw *hw = &sdata->local->hw;
1275 trace_api_connection_loss(sdata);
1277 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1278 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1280 EXPORT_SYMBOL(ieee80211_connection_loss);
1283 static enum rx_mgmt_action __must_check
1284 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1285 struct ieee80211_mgmt *mgmt, size_t len)
1287 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1288 const u8 *bssid = NULL;
1292 return RX_MGMT_NONE;
1294 ASSERT_MGD_MTX(ifmgd);
1296 bssid = ifmgd->associated->bssid;
1298 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1300 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
1301 sdata->name, bssid, reason_code);
1303 ieee80211_set_disassoc(sdata, true, false);
1304 mutex_lock(&sdata->local->mtx);
1305 ieee80211_recalc_idle(sdata->local);
1306 mutex_unlock(&sdata->local->mtx);
1308 return RX_MGMT_CFG80211_DEAUTH;
1312 static enum rx_mgmt_action __must_check
1313 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1314 struct ieee80211_mgmt *mgmt, size_t len)
1316 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1320 return RX_MGMT_NONE;
1322 ASSERT_MGD_MTX(ifmgd);
1324 if (WARN_ON(!ifmgd->associated))
1325 return RX_MGMT_NONE;
1327 if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
1328 return RX_MGMT_NONE;
1330 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1332 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
1333 sdata->name, mgmt->sa, reason_code);
1335 ieee80211_set_disassoc(sdata, true, false);
1336 mutex_lock(&sdata->local->mtx);
1337 ieee80211_recalc_idle(sdata->local);
1338 mutex_unlock(&sdata->local->mtx);
1339 return RX_MGMT_CFG80211_DISASSOC;
1343 static bool ieee80211_assoc_success(struct ieee80211_work *wk,
1344 struct ieee80211_mgmt *mgmt, size_t len)
1346 struct ieee80211_sub_if_data *sdata = wk->sdata;
1347 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1348 struct ieee80211_local *local = sdata->local;
1349 struct ieee80211_supported_band *sband;
1350 struct sta_info *sta;
1351 struct cfg80211_bss *cbss = wk->assoc.bss;
1353 u32 rates, basic_rates;
1354 u16 capab_info, aid;
1355 struct ieee802_11_elems elems;
1356 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1359 bool have_higher_than_11mbit = false;
1360 u16 ap_ht_cap_flags;
1362 /* AssocResp and ReassocResp have identical structure */
1364 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1365 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1367 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1368 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1369 "set\n", sdata->name, aid);
1370 aid &= ~(BIT(15) | BIT(14));
1372 pos = mgmt->u.assoc_resp.variable;
1373 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1375 if (!elems.supp_rates) {
1376 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1383 sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
1385 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1386 " the AP\n", sdata->name);
1390 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1392 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1393 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1397 sband = local->hw.wiphy->bands[wk->chan->band];
1399 for (i = 0; i < elems.supp_rates_len; i++) {
1400 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1401 bool is_basic = !!(elems.supp_rates[i] & 0x80);
1404 have_higher_than_11mbit = true;
1406 for (j = 0; j < sband->n_bitrates; j++) {
1407 if (sband->bitrates[j].bitrate == rate) {
1410 basic_rates |= BIT(j);
1416 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1417 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1418 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
1421 have_higher_than_11mbit = true;
1423 for (j = 0; j < sband->n_bitrates; j++) {
1424 if (sband->bitrates[j].bitrate == rate) {
1427 basic_rates |= BIT(j);
1433 sta->sta.supp_rates[wk->chan->band] = rates;
1434 sdata->vif.bss_conf.basic_rates = basic_rates;
1436 /* cf. IEEE 802.11 9.2.12 */
1437 if (wk->chan->band == IEEE80211_BAND_2GHZ &&
1438 have_higher_than_11mbit)
1439 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1441 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1443 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1444 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1445 elems.ht_cap_elem, &sta->sta.ht_cap);
1447 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1449 rate_control_rate_init(sta);
1451 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
1452 set_sta_flags(sta, WLAN_STA_MFP);
1454 if (elems.wmm_param)
1455 set_sta_flags(sta, WLAN_STA_WME);
1457 err = sta_info_insert(sta);
1460 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1461 " the AP (error %d)\n", sdata->name, err);
1466 * Always handle WMM once after association regardless
1467 * of the first value the AP uses. Setting -1 here has
1468 * that effect because the AP values is an unsigned
1471 ifmgd->wmm_last_param_set = -1;
1473 if (elems.wmm_param)
1474 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
1475 elems.wmm_param_len);
1477 ieee80211_set_wmm_default(sdata);
1479 local->oper_channel = wk->chan;
1481 if (elems.ht_info_elem && elems.wmm_param &&
1482 (sdata->local->hw.queues >= 4) &&
1483 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1484 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1485 cbss->bssid, ap_ht_cap_flags);
1487 /* set AID and assoc capability,
1488 * ieee80211_set_associated() will tell the driver */
1489 bss_conf->aid = aid;
1490 bss_conf->assoc_capability = capab_info;
1491 ieee80211_set_associated(sdata, cbss, changed);
1494 * If we're using 4-addr mode, let the AP know that we're
1495 * doing so, so that it can create the STA VLAN on its side
1497 if (ifmgd->use_4addr)
1498 ieee80211_send_4addr_nullfunc(local, sdata);
1501 * Start timer to probe the connection to the AP now.
1502 * Also start the timer that will detect beacon loss.
1504 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
1505 ieee80211_sta_reset_beacon_monitor(sdata);
1511 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1512 struct ieee80211_mgmt *mgmt,
1514 struct ieee80211_rx_status *rx_status,
1515 struct ieee802_11_elems *elems,
1518 struct ieee80211_local *local = sdata->local;
1520 struct ieee80211_bss *bss;
1521 struct ieee80211_channel *channel;
1522 bool need_ps = false;
1524 if (sdata->u.mgd.associated) {
1525 bss = (void *)sdata->u.mgd.associated->priv;
1526 /* not previously set so we may need to recalc */
1527 need_ps = !bss->dtim_period;
1530 if (elems->ds_params && elems->ds_params_len == 1)
1531 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
1534 freq = rx_status->freq;
1536 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1538 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1541 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1544 ieee80211_rx_bss_put(local, bss);
1546 if (!sdata->u.mgd.associated)
1550 mutex_lock(&local->iflist_mtx);
1551 ieee80211_recalc_ps(local, -1);
1552 mutex_unlock(&local->iflist_mtx);
1555 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
1556 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
1558 struct ieee80211_channel_sw_ie *sw_elem =
1559 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
1560 ieee80211_sta_process_chanswitch(sdata, sw_elem,
1561 bss, rx_status->mactime);
1566 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1567 struct sk_buff *skb)
1569 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1570 struct ieee80211_if_managed *ifmgd;
1571 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
1572 size_t baselen, len = skb->len;
1573 struct ieee802_11_elems elems;
1575 ifmgd = &sdata->u.mgd;
1577 ASSERT_MGD_MTX(ifmgd);
1579 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
1580 return; /* ignore ProbeResp to foreign address */
1582 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1586 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1589 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1591 if (ifmgd->associated &&
1592 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
1593 ieee80211_reset_ap_probe(sdata);
1597 * This is the canonical list of information elements we care about,
1598 * the filter code also gives us all changes to the Microsoft OUI
1599 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1601 * We implement beacon filtering in software since that means we can
1602 * avoid processing the frame here and in cfg80211, and userspace
1603 * will not be able to tell whether the hardware supports it or not.
1605 * XXX: This list needs to be dynamic -- userspace needs to be able to
1606 * add items it requires. It also needs to be able to tell us to
1607 * look out for other vendor IEs.
1609 static const u64 care_about_ies =
1610 (1ULL << WLAN_EID_COUNTRY) |
1611 (1ULL << WLAN_EID_ERP_INFO) |
1612 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1613 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1614 (1ULL << WLAN_EID_HT_CAPABILITY) |
1615 (1ULL << WLAN_EID_HT_INFORMATION);
1617 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1618 struct ieee80211_mgmt *mgmt,
1620 struct ieee80211_rx_status *rx_status)
1622 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1623 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1625 struct ieee802_11_elems elems;
1626 struct ieee80211_local *local = sdata->local;
1628 bool erp_valid, directed_tim = false;
1633 ASSERT_MGD_MTX(ifmgd);
1635 /* Process beacon from the current BSS */
1636 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1640 if (rx_status->freq != local->hw.conf.channel->center_freq)
1644 * We might have received a number of frames, among them a
1645 * disassoc frame and a beacon...
1647 if (!ifmgd->associated)
1650 bssid = ifmgd->associated->bssid;
1653 * And in theory even frames from a different AP we were just
1654 * associated to a split-second ago!
1656 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
1659 /* Track average RSSI from the Beacon frames of the current AP */
1660 ifmgd->last_beacon_signal = rx_status->signal;
1661 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
1662 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
1663 ifmgd->ave_beacon_signal = rx_status->signal * 16;
1664 ifmgd->last_cqm_event_signal = 0;
1665 ifmgd->count_beacon_signal = 1;
1667 ifmgd->ave_beacon_signal =
1668 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
1669 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
1670 ifmgd->ave_beacon_signal) / 16;
1671 ifmgd->count_beacon_signal++;
1673 if (bss_conf->cqm_rssi_thold &&
1674 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
1675 !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1676 int sig = ifmgd->ave_beacon_signal / 16;
1677 int last_event = ifmgd->last_cqm_event_signal;
1678 int thold = bss_conf->cqm_rssi_thold;
1679 int hyst = bss_conf->cqm_rssi_hyst;
1681 (last_event == 0 || sig < last_event - hyst)) {
1682 ifmgd->last_cqm_event_signal = sig;
1683 ieee80211_cqm_rssi_notify(
1685 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
1687 } else if (sig > thold &&
1688 (last_event == 0 || sig > last_event + hyst)) {
1689 ifmgd->last_cqm_event_signal = sig;
1690 ieee80211_cqm_rssi_notify(
1692 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
1697 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
1698 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1699 if (net_ratelimit()) {
1700 printk(KERN_DEBUG "%s: cancelling probereq poll due "
1701 "to a received beacon\n", sdata->name);
1704 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
1705 mutex_lock(&local->iflist_mtx);
1706 ieee80211_recalc_ps(local, -1);
1707 mutex_unlock(&local->iflist_mtx);
1711 * Push the beacon loss detection into the future since
1712 * we are processing a beacon from the AP just now.
1714 ieee80211_sta_reset_beacon_monitor(sdata);
1716 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1717 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1718 len - baselen, &elems,
1719 care_about_ies, ncrc);
1721 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1722 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1725 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
1726 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1729 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
1730 elems.wmm_param_len);
1733 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1735 if (local->hw.conf.dynamic_ps_timeout > 0) {
1736 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1737 ieee80211_hw_config(local,
1738 IEEE80211_CONF_CHANGE_PS);
1739 ieee80211_send_nullfunc(local, sdata, 0);
1741 local->pspolling = true;
1744 * Here is assumed that the driver will be
1745 * able to send ps-poll frame and receive a
1746 * response even though power save mode is
1747 * enabled, but some drivers might require
1748 * to disable power save here. This needs
1749 * to be investigated.
1751 ieee80211_send_pspoll(local, sdata);
1756 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
1758 ifmgd->beacon_crc = ncrc;
1759 ifmgd->beacon_crc_valid = true;
1761 if (elems.erp_info && elems.erp_info_len >= 1) {
1763 erp_value = elems.erp_info[0];
1767 changed |= ieee80211_handle_bss_capability(sdata,
1768 le16_to_cpu(mgmt->u.beacon.capab_info),
1769 erp_valid, erp_value);
1772 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1773 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
1774 struct sta_info *sta;
1775 struct ieee80211_supported_band *sband;
1776 u16 ap_ht_cap_flags;
1780 sta = sta_info_get(sdata, bssid);
1781 if (WARN_ON(!sta)) {
1786 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1788 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1789 elems.ht_cap_elem, &sta->sta.ht_cap);
1791 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1795 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1796 bssid, ap_ht_cap_flags);
1799 /* Note: country IE parsing is done for us by cfg80211 */
1800 if (elems.country_elem) {
1801 /* TODO: IBSS also needs this */
1802 if (elems.pwr_constr_elem)
1803 ieee80211_handle_pwr_constr(sdata,
1804 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1805 elems.pwr_constr_elem,
1806 elems.pwr_constr_elem_len);
1809 ieee80211_bss_info_change_notify(sdata, changed);
1812 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1813 struct sk_buff *skb)
1815 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1816 struct ieee80211_rx_status *rx_status;
1817 struct ieee80211_mgmt *mgmt;
1818 enum rx_mgmt_action rma = RX_MGMT_NONE;
1821 rx_status = (struct ieee80211_rx_status *) skb->cb;
1822 mgmt = (struct ieee80211_mgmt *) skb->data;
1823 fc = le16_to_cpu(mgmt->frame_control);
1825 mutex_lock(&ifmgd->mtx);
1827 if (ifmgd->associated &&
1828 memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
1829 switch (fc & IEEE80211_FCTL_STYPE) {
1830 case IEEE80211_STYPE_BEACON:
1831 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1834 case IEEE80211_STYPE_PROBE_RESP:
1835 ieee80211_rx_mgmt_probe_resp(sdata, skb);
1837 case IEEE80211_STYPE_DEAUTH:
1838 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
1840 case IEEE80211_STYPE_DISASSOC:
1841 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1843 case IEEE80211_STYPE_ACTION:
1844 switch (mgmt->u.action.category) {
1845 case WLAN_CATEGORY_SPECTRUM_MGMT:
1846 ieee80211_sta_process_chanswitch(sdata,
1847 &mgmt->u.action.u.chan_switch.sw_elem,
1848 (void *)ifmgd->associated->priv,
1849 rx_status->mactime);
1853 mutex_unlock(&ifmgd->mtx);
1859 case RX_MGMT_CFG80211_DEAUTH:
1860 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
1862 case RX_MGMT_CFG80211_DISASSOC:
1863 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
1866 WARN(1, "unexpected: %d", rma);
1871 mutex_unlock(&ifmgd->mtx);
1873 if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
1874 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
1875 struct ieee80211_local *local = sdata->local;
1876 struct ieee80211_work *wk;
1878 mutex_lock(&local->mtx);
1879 list_for_each_entry(wk, &local->work_list, list) {
1880 if (wk->sdata != sdata)
1883 if (wk->type != IEEE80211_WORK_ASSOC &&
1884 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
1887 if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
1889 if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
1893 * Printing the message only here means we can't
1894 * spuriously print it, but it also means that it
1895 * won't be printed when the frame comes in before
1896 * we even tried to associate or in similar cases.
1898 * Ultimately, I suspect cfg80211 should print the
1902 "%s: deauthenticated from %pM (Reason: %u)\n",
1903 sdata->name, mgmt->bssid,
1904 le16_to_cpu(mgmt->u.deauth.reason_code));
1906 list_del_rcu(&wk->list);
1910 mutex_unlock(&local->mtx);
1912 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
1916 static void ieee80211_sta_timer(unsigned long data)
1918 struct ieee80211_sub_if_data *sdata =
1919 (struct ieee80211_sub_if_data *) data;
1920 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1921 struct ieee80211_local *local = sdata->local;
1923 if (local->quiescing) {
1924 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
1928 ieee80211_queue_work(&local->hw, &sdata->work);
1931 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
1934 struct ieee80211_local *local = sdata->local;
1935 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1937 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1938 IEEE80211_STA_BEACON_POLL);
1940 ieee80211_set_disassoc(sdata, true, true);
1941 mutex_unlock(&ifmgd->mtx);
1942 mutex_lock(&local->mtx);
1943 ieee80211_recalc_idle(local);
1944 mutex_unlock(&local->mtx);
1946 * must be outside lock due to cfg80211,
1947 * but that's not a problem.
1949 ieee80211_send_deauth_disassoc(sdata, bssid,
1950 IEEE80211_STYPE_DEAUTH,
1951 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1953 mutex_lock(&ifmgd->mtx);
1956 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
1958 struct ieee80211_local *local = sdata->local;
1959 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1961 /* then process the rest of the work */
1962 mutex_lock(&ifmgd->mtx);
1964 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1965 IEEE80211_STA_CONNECTION_POLL) &&
1966 ifmgd->associated) {
1970 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1972 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1973 max_tries = IEEE80211_MAX_NULLFUNC_TRIES;
1975 max_tries = IEEE80211_MAX_PROBE_TRIES;
1977 /* ACK received for nullfunc probing frame */
1978 if (!ifmgd->probe_send_count)
1979 ieee80211_reset_ap_probe(sdata);
1980 else if (ifmgd->nullfunc_failed) {
1981 if (ifmgd->probe_send_count < max_tries) {
1982 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1983 wiphy_debug(local->hw.wiphy,
1984 "%s: No ack for nullfunc frame to"
1985 " AP %pM, try %d/%i\n",
1987 ifmgd->probe_send_count, max_tries);
1989 ieee80211_mgd_probe_ap_send(sdata);
1991 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1992 wiphy_debug(local->hw.wiphy,
1993 "%s: No ack for nullfunc frame to"
1994 " AP %pM, disconnecting.\n",
1995 sdata->name, bssid);
1997 ieee80211_sta_connection_lost(sdata, bssid);
1999 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
2000 run_again(ifmgd, ifmgd->probe_timeout);
2001 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2002 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2003 wiphy_debug(local->hw.wiphy,
2004 "%s: Failed to send nullfunc to AP %pM"
2005 " after %dms, disconnecting.\n",
2007 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
2009 ieee80211_sta_connection_lost(sdata, bssid);
2010 } else if (ifmgd->probe_send_count < max_tries) {
2011 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2012 wiphy_debug(local->hw.wiphy,
2013 "%s: No probe response from AP %pM"
2014 " after %dms, try %d/%i\n",
2016 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ,
2017 ifmgd->probe_send_count, max_tries);
2019 ieee80211_mgd_probe_ap_send(sdata);
2022 * We actually lost the connection ... or did we?
2025 wiphy_debug(local->hw.wiphy,
2026 "%s: No probe response from AP %pM"
2027 " after %dms, disconnecting.\n",
2029 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
2031 ieee80211_sta_connection_lost(sdata, bssid);
2035 mutex_unlock(&ifmgd->mtx);
2038 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2040 struct ieee80211_sub_if_data *sdata =
2041 (struct ieee80211_sub_if_data *) data;
2042 struct ieee80211_local *local = sdata->local;
2044 if (local->quiescing)
2047 ieee80211_queue_work(&sdata->local->hw,
2048 &sdata->u.mgd.beacon_connection_loss_work);
2051 static void ieee80211_sta_conn_mon_timer(unsigned long data)
2053 struct ieee80211_sub_if_data *sdata =
2054 (struct ieee80211_sub_if_data *) data;
2055 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2056 struct ieee80211_local *local = sdata->local;
2058 if (local->quiescing)
2061 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
2064 static void ieee80211_sta_monitor_work(struct work_struct *work)
2066 struct ieee80211_sub_if_data *sdata =
2067 container_of(work, struct ieee80211_sub_if_data,
2068 u.mgd.monitor_work);
2070 ieee80211_mgd_probe_ap(sdata, false);
2073 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2075 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2076 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2077 IEEE80211_STA_CONNECTION_POLL);
2079 /* let's probe the connection once */
2080 ieee80211_queue_work(&sdata->local->hw,
2081 &sdata->u.mgd.monitor_work);
2082 /* and do all the other regular work too */
2083 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2088 void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2090 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2093 * we need to use atomic bitops for the running bits
2094 * only because both timers might fire at the same
2095 * time -- the code here is properly synchronised.
2098 cancel_work_sync(&ifmgd->request_smps_work);
2100 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
2101 if (del_timer_sync(&ifmgd->timer))
2102 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2104 cancel_work_sync(&ifmgd->chswitch_work);
2105 if (del_timer_sync(&ifmgd->chswitch_timer))
2106 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
2108 cancel_work_sync(&ifmgd->monitor_work);
2109 /* these will just be re-established on connection */
2110 del_timer_sync(&ifmgd->conn_mon_timer);
2111 del_timer_sync(&ifmgd->bcn_mon_timer);
2114 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2116 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2118 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2119 add_timer(&ifmgd->timer);
2120 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2121 add_timer(&ifmgd->chswitch_timer);
2122 ieee80211_sta_reset_beacon_monitor(sdata);
2123 ieee80211_restart_sta_timer(sdata);
2127 /* interface setup */
2128 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2130 struct ieee80211_if_managed *ifmgd;
2132 ifmgd = &sdata->u.mgd;
2133 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
2134 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
2135 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2136 ieee80211_beacon_connection_loss_work);
2137 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
2138 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
2139 (unsigned long) sdata);
2140 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2141 (unsigned long) sdata);
2142 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2143 (unsigned long) sdata);
2144 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
2145 (unsigned long) sdata);
2149 mutex_init(&ifmgd->mtx);
2151 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2152 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2154 ifmgd->req_smps = IEEE80211_SMPS_OFF;
2157 /* scan finished notification */
2158 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2160 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2162 /* Restart STA timers */
2164 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2165 ieee80211_restart_sta_timer(sdata);
2169 int ieee80211_max_network_latency(struct notifier_block *nb,
2170 unsigned long data, void *dummy)
2172 s32 latency_usec = (s32) data;
2173 struct ieee80211_local *local =
2174 container_of(nb, struct ieee80211_local,
2175 network_latency_notifier);
2177 mutex_lock(&local->iflist_mtx);
2178 ieee80211_recalc_ps(local, latency_usec);
2179 mutex_unlock(&local->iflist_mtx);
2185 static enum work_done_result
2186 ieee80211_probe_auth_done(struct ieee80211_work *wk,
2187 struct sk_buff *skb)
2190 cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
2191 return WORK_DONE_DESTROY;
2194 if (wk->type == IEEE80211_WORK_AUTH) {
2195 cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
2196 return WORK_DONE_DESTROY;
2199 mutex_lock(&wk->sdata->u.mgd.mtx);
2200 ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
2201 mutex_unlock(&wk->sdata->u.mgd.mtx);
2203 wk->type = IEEE80211_WORK_AUTH;
2204 wk->probe_auth.tries = 0;
2205 return WORK_DONE_REQUEUE;
2208 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2209 struct cfg80211_auth_request *req)
2212 struct ieee80211_work *wk;
2215 if (req->local_state_change)
2216 return 0; /* no need to update mac80211 state */
2218 switch (req->auth_type) {
2219 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2220 auth_alg = WLAN_AUTH_OPEN;
2222 case NL80211_AUTHTYPE_SHARED_KEY:
2223 if (IS_ERR(sdata->local->wep_tx_tfm))
2225 auth_alg = WLAN_AUTH_SHARED_KEY;
2227 case NL80211_AUTHTYPE_FT:
2228 auth_alg = WLAN_AUTH_FT;
2230 case NL80211_AUTHTYPE_NETWORK_EAP:
2231 auth_alg = WLAN_AUTH_LEAP;
2237 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2241 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
2243 if (req->ie && req->ie_len) {
2244 memcpy(wk->ie, req->ie, req->ie_len);
2245 wk->ie_len = req->ie_len;
2248 if (req->key && req->key_len) {
2249 wk->probe_auth.key_len = req->key_len;
2250 wk->probe_auth.key_idx = req->key_idx;
2251 memcpy(wk->probe_auth.key, req->key, req->key_len);
2254 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2255 memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
2256 wk->probe_auth.ssid_len = ssid[1];
2258 wk->probe_auth.algorithm = auth_alg;
2259 wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
2261 /* if we already have a probe, don't probe again */
2262 if (req->bss->proberesp_ies)
2263 wk->type = IEEE80211_WORK_AUTH;
2265 wk->type = IEEE80211_WORK_DIRECT_PROBE;
2266 wk->chan = req->bss->channel;
2268 wk->done = ieee80211_probe_auth_done;
2270 ieee80211_add_work(wk);
2274 static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
2275 struct sk_buff *skb)
2277 struct ieee80211_mgmt *mgmt;
2278 struct ieee80211_rx_status *rx_status;
2279 struct ieee802_11_elems elems;
2283 cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
2284 return WORK_DONE_DESTROY;
2287 if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
2288 mutex_lock(&wk->sdata->u.mgd.mtx);
2289 rx_status = (void *) skb->cb;
2290 ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
2291 ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
2293 mutex_unlock(&wk->sdata->u.mgd.mtx);
2295 wk->type = IEEE80211_WORK_ASSOC;
2296 /* not really done yet */
2297 return WORK_DONE_REQUEUE;
2300 mgmt = (void *)skb->data;
2301 status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2303 if (status == WLAN_STATUS_SUCCESS) {
2304 mutex_lock(&wk->sdata->u.mgd.mtx);
2305 if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
2306 mutex_unlock(&wk->sdata->u.mgd.mtx);
2307 /* oops -- internal error -- send timeout for now */
2308 cfg80211_send_assoc_timeout(wk->sdata->dev,
2310 return WORK_DONE_DESTROY;
2313 mutex_unlock(&wk->sdata->u.mgd.mtx);
2316 cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
2317 return WORK_DONE_DESTROY;
2320 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2321 struct cfg80211_assoc_request *req)
2323 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2324 struct ieee80211_bss *bss = (void *)req->bss->priv;
2325 struct ieee80211_work *wk;
2329 mutex_lock(&ifmgd->mtx);
2330 if (ifmgd->associated) {
2331 if (!req->prev_bssid ||
2332 memcmp(req->prev_bssid, ifmgd->associated->bssid,
2335 * We are already associated and the request was not a
2336 * reassociation request from the current BSS, so
2339 mutex_unlock(&ifmgd->mtx);
2343 /* Trying to reassociate - clear previous association state */
2344 ieee80211_set_disassoc(sdata, true, false);
2346 mutex_unlock(&ifmgd->mtx);
2348 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2352 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
2353 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
2355 ifmgd->beacon_crc_valid = false;
2357 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2358 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2359 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2360 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2361 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2364 if (req->ie && req->ie_len) {
2365 memcpy(wk->ie, req->ie, req->ie_len);
2366 wk->ie_len = req->ie_len;
2370 wk->assoc.bss = req->bss;
2372 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
2374 /* new association always uses requested smps mode */
2375 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
2376 if (ifmgd->powersave)
2377 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
2379 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
2381 ifmgd->ap_smps = ifmgd->req_smps;
2383 wk->assoc.smps = ifmgd->ap_smps;
2385 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
2386 * We still associate in non-HT mode (11a/b/g) if any one of these
2387 * ciphers is configured as pairwise.
2388 * We can set this to true for non-11n hardware, that'll be checked
2389 * separately along with the peer capabilities.
2391 wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
2392 wk->assoc.capability = req->bss->capability;
2393 wk->assoc.wmm_used = bss->wmm_used;
2394 wk->assoc.supp_rates = bss->supp_rates;
2395 wk->assoc.supp_rates_len = bss->supp_rates_len;
2396 wk->assoc.ht_information_ie =
2397 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
2399 if (bss->wmm_used && bss->uapsd_supported &&
2400 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
2401 wk->assoc.uapsd_used = true;
2402 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
2404 wk->assoc.uapsd_used = false;
2405 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
2408 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2409 memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
2410 wk->assoc.ssid_len = ssid[1];
2412 if (req->prev_bssid)
2413 memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
2415 wk->chan = req->bss->channel;
2417 wk->done = ieee80211_assoc_done;
2418 if (!bss->dtim_period &&
2419 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
2420 wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
2422 wk->type = IEEE80211_WORK_ASSOC;
2425 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2426 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2428 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2429 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2432 if (req->crypto.control_port)
2433 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2435 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2437 sdata->control_port_protocol = req->crypto.control_port_ethertype;
2438 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
2440 ieee80211_add_work(wk);
2444 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
2445 struct cfg80211_deauth_request *req,
2448 struct ieee80211_local *local = sdata->local;
2449 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2450 struct ieee80211_work *wk;
2452 bool assoc_bss = false;
2454 mutex_lock(&ifmgd->mtx);
2456 memcpy(bssid, req->bss->bssid, ETH_ALEN);
2457 if (ifmgd->associated == req->bss) {
2458 ieee80211_set_disassoc(sdata, false, true);
2459 mutex_unlock(&ifmgd->mtx);
2462 bool not_auth_yet = false;
2464 mutex_unlock(&ifmgd->mtx);
2466 mutex_lock(&local->mtx);
2467 list_for_each_entry(wk, &local->work_list, list) {
2468 if (wk->sdata != sdata)
2471 if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
2472 wk->type != IEEE80211_WORK_AUTH &&
2473 wk->type != IEEE80211_WORK_ASSOC &&
2474 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
2477 if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
2480 not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
2481 list_del_rcu(&wk->list);
2485 mutex_unlock(&local->mtx);
2488 * If somebody requests authentication and we haven't
2489 * sent out an auth frame yet there's no need to send
2490 * out a deauth frame either. If the state was PROBE,
2491 * then this is the case. If it's AUTH we have sent a
2492 * frame, and if it's IDLE we have completed the auth
2496 __cfg80211_auth_canceled(sdata->dev, bssid);
2501 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
2502 sdata->name, bssid, req->reason_code);
2504 ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
2505 req->reason_code, cookie,
2506 !req->local_state_change);
2508 sta_info_destroy_addr(sdata, bssid);
2510 mutex_lock(&sdata->local->mtx);
2511 ieee80211_recalc_idle(sdata->local);
2512 mutex_unlock(&sdata->local->mtx);
2517 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
2518 struct cfg80211_disassoc_request *req,
2521 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2524 mutex_lock(&ifmgd->mtx);
2527 * cfg80211 should catch this ... but it's racy since
2528 * we can receive a disassoc frame, process it, hand it
2529 * to cfg80211 while that's in a locked section already
2530 * trying to tell us that the user wants to disconnect.
2532 if (ifmgd->associated != req->bss) {
2533 mutex_unlock(&ifmgd->mtx);
2537 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
2538 sdata->name, req->bss->bssid, req->reason_code);
2540 memcpy(bssid, req->bss->bssid, ETH_ALEN);
2541 ieee80211_set_disassoc(sdata, false, true);
2543 mutex_unlock(&ifmgd->mtx);
2545 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
2546 IEEE80211_STYPE_DISASSOC, req->reason_code,
2547 cookie, !req->local_state_change);
2548 sta_info_destroy_addr(sdata, bssid);
2550 mutex_lock(&sdata->local->mtx);
2551 ieee80211_recalc_idle(sdata->local);
2552 mutex_unlock(&sdata->local->mtx);
2557 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
2558 enum nl80211_cqm_rssi_threshold_event rssi_event,
2561 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2563 trace_api_cqm_rssi_notify(sdata, rssi_event);
2565 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
2567 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);