2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
4 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/etherdevice.h>
14 #include <linux/netdevice.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/if_arp.h>
19 #include <linux/timer.h>
20 #include <linux/rtnetlink.h>
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "driver-ops.h"
27 #include "debugfs_sta.h"
32 * DOC: STA information lifetime rules
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
38 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it, in
44 * particular, it may not start any mesh peer link management or add
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
50 * Station entries are added by mac80211 when you establish a link with a
51 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
53 * receive an association response from the AP. For IBSS this occurs when
54 * get to know about a peer on the same IBSS. For WDS we add the sta for
55 * the peer immediately upon device open. When using AP mode we add stations
56 * for each respective station upon request from userspace through nl80211.
58 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
61 * There is no concept of ownership on a STA entry, each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
67 /* Caller must hold local->sta_mtx */
68 static int sta_info_hash_del(struct ieee80211_local *local,
73 s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
74 lockdep_is_held(&local->sta_mtx));
78 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
83 while (rcu_access_pointer(s->hnext) &&
84 rcu_access_pointer(s->hnext) != sta)
85 s = rcu_dereference_protected(s->hnext,
86 lockdep_is_held(&local->sta_mtx));
87 if (rcu_access_pointer(s->hnext)) {
88 rcu_assign_pointer(s->hnext, sta->hnext);
95 static void __cleanup_single_sta(struct sta_info *sta)
98 struct tid_ampdu_tx *tid_tx;
99 struct ieee80211_sub_if_data *sdata = sta->sdata;
100 struct ieee80211_local *local = sdata->local;
103 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
104 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
105 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
106 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
107 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
108 ps = &sdata->bss->ps;
109 else if (ieee80211_vif_is_mesh(&sdata->vif))
110 ps = &sdata->u.mesh.ps;
114 clear_sta_flag(sta, WLAN_STA_PS_STA);
115 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
116 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
118 atomic_dec(&ps->num_sta_ps);
121 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
123 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
124 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
127 if (ieee80211_vif_is_mesh(&sdata->vif))
128 mesh_sta_cleanup(sta);
130 cancel_work_sync(&sta->drv_deliver_wk);
133 * Destroy aggregation state here. It would be nice to wait for the
134 * driver to finish aggregation stop and then clean up, but for now
135 * drivers have to handle aggregation stop being requested, followed
136 * directly by station destruction.
138 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
139 kfree(sta->ampdu_mlme.tid_start_tx[i]);
140 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
148 static void cleanup_single_sta(struct sta_info *sta)
150 struct ieee80211_sub_if_data *sdata = sta->sdata;
151 struct ieee80211_local *local = sdata->local;
153 __cleanup_single_sta(sta);
154 sta_info_free(local, sta);
157 /* protected by RCU */
158 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
161 struct ieee80211_local *local = sdata->local;
162 struct sta_info *sta;
164 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
165 lockdep_is_held(&local->sta_mtx));
167 if (sta->sdata == sdata &&
168 ether_addr_equal(sta->sta.addr, addr))
170 sta = rcu_dereference_check(sta->hnext,
171 lockdep_is_held(&local->sta_mtx));
177 * Get sta info either from the specified interface
178 * or from one of its vlans
180 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
183 struct ieee80211_local *local = sdata->local;
184 struct sta_info *sta;
186 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
187 lockdep_is_held(&local->sta_mtx));
189 if ((sta->sdata == sdata ||
190 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
191 ether_addr_equal(sta->sta.addr, addr))
193 sta = rcu_dereference_check(sta->hnext,
194 lockdep_is_held(&local->sta_mtx));
199 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
202 struct ieee80211_local *local = sdata->local;
203 struct sta_info *sta;
206 list_for_each_entry_rcu(sta, &local->sta_list, list) {
207 if (sdata != sta->sdata)
220 * sta_info_free - free STA
222 * @local: pointer to the global information
223 * @sta: STA info to free
225 * This function must undo everything done by sta_info_alloc()
226 * that may happen before sta_info_insert(). It may only be
227 * called when sta_info_insert() has not been attempted (and
228 * if that fails, the station is freed anyway.)
230 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
233 rate_control_free_sta(sta);
235 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
237 kfree(rcu_dereference_raw(sta->sta.rates));
241 /* Caller must hold local->sta_mtx */
242 static void sta_info_hash_add(struct ieee80211_local *local,
243 struct sta_info *sta)
245 lockdep_assert_held(&local->sta_mtx);
246 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
247 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
250 static void sta_deliver_ps_frames(struct work_struct *wk)
252 struct sta_info *sta;
254 sta = container_of(wk, struct sta_info, drv_deliver_wk);
260 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
261 ieee80211_sta_ps_deliver_wakeup(sta);
262 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
263 ieee80211_sta_ps_deliver_poll_response(sta);
264 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
265 ieee80211_sta_ps_deliver_uapsd(sta);
269 static int sta_prepare_rate_control(struct ieee80211_local *local,
270 struct sta_info *sta, gfp_t gfp)
272 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
275 sta->rate_ctrl = local->rate_ctrl;
276 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
278 if (!sta->rate_ctrl_priv)
284 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
285 const u8 *addr, gfp_t gfp)
287 struct ieee80211_local *local = sdata->local;
288 struct sta_info *sta;
289 struct timespec uptime;
292 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
296 spin_lock_init(&sta->lock);
297 spin_lock_init(&sta->ps_lock);
298 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
299 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
300 mutex_init(&sta->ampdu_mlme.mtx);
301 #ifdef CONFIG_MAC80211_MESH
302 if (ieee80211_vif_is_mesh(&sdata->vif) &&
303 !sdata->u.mesh.user_mpm)
304 init_timer(&sta->plink_timer);
305 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
308 memcpy(sta->sta.addr, addr, ETH_ALEN);
311 sta->last_rx = jiffies;
313 sta->sta_state = IEEE80211_STA_NONE;
315 /* Mark TID as unreserved */
316 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
318 ktime_get_ts(&uptime);
319 sta->last_connected = uptime.tv_sec;
320 ewma_init(&sta->avg_signal, 1024, 8);
321 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
322 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
324 if (sta_prepare_rate_control(local, sta, gfp)) {
329 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
331 * timer_to_tid must be initialized with identity mapping
332 * to enable session_timer's data differentiation. See
333 * sta_rx_agg_session_timer_expired for usage.
335 sta->timer_to_tid[i] = i;
337 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
338 skb_queue_head_init(&sta->ps_tx_buf[i]);
339 skb_queue_head_init(&sta->tx_filtered[i]);
342 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
343 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
345 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
346 if (sdata->vif.type == NL80211_IFTYPE_AP ||
347 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
348 struct ieee80211_supported_band *sband =
349 local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
350 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
351 IEEE80211_HT_CAP_SM_PS_SHIFT;
353 * Assume that hostapd advertises our caps in the beacon and
354 * this is the known_smps_mode for a station that just assciated
357 case WLAN_HT_SMPS_CONTROL_DISABLED:
358 sta->known_smps_mode = IEEE80211_SMPS_OFF;
360 case WLAN_HT_SMPS_CONTROL_STATIC:
361 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
363 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
364 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
371 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
376 static int sta_info_insert_check(struct sta_info *sta)
378 struct ieee80211_sub_if_data *sdata = sta->sdata;
381 * Can't be a WARN_ON because it can be triggered through a race:
382 * something inserts a STA (on one CPU) without holding the RTNL
383 * and another CPU turns off the net device.
385 if (unlikely(!ieee80211_sdata_running(sdata)))
388 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
389 is_multicast_ether_addr(sta->sta.addr)))
395 static int sta_info_insert_drv_state(struct ieee80211_local *local,
396 struct ieee80211_sub_if_data *sdata,
397 struct sta_info *sta)
399 enum ieee80211_sta_state state;
402 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
403 err = drv_sta_state(local, sdata, sta, state, state + 1);
410 * Drivers using legacy sta_add/sta_remove callbacks only
411 * get uploaded set to true after sta_add is called.
413 if (!local->ops->sta_add)
414 sta->uploaded = true;
418 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
420 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
421 sta->sta.addr, state + 1, err);
425 /* unwind on error */
426 for (; state > IEEE80211_STA_NOTEXIST; state--)
427 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
433 * should be called with sta_mtx locked
434 * this function replaces the mutex lock
437 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
439 struct ieee80211_local *local = sta->local;
440 struct ieee80211_sub_if_data *sdata = sta->sdata;
441 struct station_info sinfo;
444 lockdep_assert_held(&local->sta_mtx);
446 /* check if STA exists already */
447 if (sta_info_get_bss(sdata, sta->sta.addr)) {
453 local->sta_generation++;
456 /* simplify things and don't accept BA sessions yet */
457 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
459 /* make the station visible */
460 sta_info_hash_add(local, sta);
462 list_add_tail_rcu(&sta->list, &local->sta_list);
465 err = sta_info_insert_drv_state(local, sdata, sta);
469 set_sta_flag(sta, WLAN_STA_INSERTED);
470 /* accept BA sessions now */
471 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
473 ieee80211_recalc_min_chandef(sdata);
474 ieee80211_sta_debugfs_add(sta);
475 rate_control_add_sta_debugfs(sta);
477 memset(&sinfo, 0, sizeof(sinfo));
479 sinfo.generation = local->sta_generation;
480 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
482 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
484 /* move reference to rcu-protected */
486 mutex_unlock(&local->sta_mtx);
488 if (ieee80211_vif_is_mesh(&sdata->vif))
489 mesh_accept_plinks_update(sdata);
493 sta_info_hash_del(local, sta);
494 list_del_rcu(&sta->list);
497 __cleanup_single_sta(sta);
499 mutex_unlock(&local->sta_mtx);
504 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
506 struct ieee80211_local *local = sta->local;
511 err = sta_info_insert_check(sta);
517 mutex_lock(&local->sta_mtx);
519 err = sta_info_insert_finish(sta);
525 sta_info_free(local, sta);
529 int sta_info_insert(struct sta_info *sta)
531 int err = sta_info_insert_rcu(sta);
538 static inline void __bss_tim_set(u8 *tim, u16 id)
541 * This format has been mandated by the IEEE specifications,
542 * so this line may not be changed to use the __set_bit() format.
544 tim[id / 8] |= (1 << (id % 8));
547 static inline void __bss_tim_clear(u8 *tim, u16 id)
550 * This format has been mandated by the IEEE specifications,
551 * so this line may not be changed to use the __clear_bit() format.
553 tim[id / 8] &= ~(1 << (id % 8));
556 static inline bool __bss_tim_get(u8 *tim, u16 id)
559 * This format has been mandated by the IEEE specifications,
560 * so this line may not be changed to use the test_bit() format.
562 return tim[id / 8] & (1 << (id % 8));
565 static unsigned long ieee80211_tids_for_ac(int ac)
567 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
569 case IEEE80211_AC_VO:
570 return BIT(6) | BIT(7);
571 case IEEE80211_AC_VI:
572 return BIT(4) | BIT(5);
573 case IEEE80211_AC_BE:
574 return BIT(0) | BIT(3);
575 case IEEE80211_AC_BK:
576 return BIT(1) | BIT(2);
583 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
585 struct ieee80211_local *local = sta->local;
587 bool indicate_tim = false;
588 u8 ignore_for_tim = sta->sta.uapsd_queues;
592 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
593 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
594 if (WARN_ON_ONCE(!sta->sdata->bss))
597 ps = &sta->sdata->bss->ps;
599 #ifdef CONFIG_MAC80211_MESH
600 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
601 ps = &sta->sdata->u.mesh.ps;
602 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
603 id = sta->plid % (IEEE80211_MAX_AID + 1);
609 /* No need to do anything if the driver does all */
610 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
617 * If all ACs are delivery-enabled then we should build
618 * the TIM bit for all ACs anyway; if only some are then
619 * we ignore those and build the TIM bit using only the
622 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
626 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
628 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
631 if (ignore_for_tim & BIT(ac))
634 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
635 !skb_queue_empty(&sta->ps_tx_buf[ac]);
639 tids = ieee80211_tids_for_ac(ac);
642 sta->driver_buffered_tids & tids;
646 spin_lock_bh(&local->tim_lock);
648 if (indicate_tim == __bss_tim_get(ps->tim, id))
652 __bss_tim_set(ps->tim, id);
654 __bss_tim_clear(ps->tim, id);
656 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
657 local->tim_in_locked_section = true;
658 drv_set_tim(local, &sta->sta, indicate_tim);
659 local->tim_in_locked_section = false;
663 spin_unlock_bh(&local->tim_lock);
666 void sta_info_recalc_tim(struct sta_info *sta)
668 __sta_info_recalc_tim(sta, false);
671 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
673 struct ieee80211_tx_info *info;
679 info = IEEE80211_SKB_CB(skb);
681 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
682 timeout = (sta->listen_interval *
683 sta->sdata->vif.bss_conf.beacon_int *
685 if (timeout < STA_TX_BUFFER_EXPIRE)
686 timeout = STA_TX_BUFFER_EXPIRE;
687 return time_after(jiffies, info->control.jiffies + timeout);
691 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
692 struct sta_info *sta, int ac)
698 * First check for frames that should expire on the filtered
699 * queue. Frames here were rejected by the driver and are on
700 * a separate queue to avoid reordering with normal PS-buffered
701 * frames. They also aren't accounted for right now in the
702 * total_ps_buffered counter.
705 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
706 skb = skb_peek(&sta->tx_filtered[ac]);
707 if (sta_info_buffer_expired(sta, skb))
708 skb = __skb_dequeue(&sta->tx_filtered[ac]);
711 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
714 * Frames are queued in order, so if this one
715 * hasn't expired yet we can stop testing. If
716 * we actually reached the end of the queue we
717 * also need to stop, of course.
721 ieee80211_free_txskb(&local->hw, skb);
725 * Now also check the normal PS-buffered queue, this will
726 * only find something if the filtered queue was emptied
727 * since the filtered frames are all before the normal PS
731 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
732 skb = skb_peek(&sta->ps_tx_buf[ac]);
733 if (sta_info_buffer_expired(sta, skb))
734 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
737 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
740 * frames are queued in order, so if this one
741 * hasn't expired yet (or we reached the end of
742 * the queue) we can stop testing
747 local->total_ps_buffered--;
748 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
750 ieee80211_free_txskb(&local->hw, skb);
754 * Finally, recalculate the TIM bit for this station -- it might
755 * now be clear because the station was too slow to retrieve its
758 sta_info_recalc_tim(sta);
761 * Return whether there are any frames still buffered, this is
762 * used to check whether the cleanup timer still needs to run,
763 * if there are no frames we don't need to rearm the timer.
765 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
766 skb_queue_empty(&sta->tx_filtered[ac]));
769 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
770 struct sta_info *sta)
772 bool have_buffered = false;
775 /* This is only necessary for stations on BSS/MBSS interfaces */
776 if (!sta->sdata->bss &&
777 !ieee80211_vif_is_mesh(&sta->sdata->vif))
780 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
782 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
784 return have_buffered;
787 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
789 struct ieee80211_local *local;
790 struct ieee80211_sub_if_data *sdata;
801 lockdep_assert_held(&local->sta_mtx);
804 * Before removing the station from the driver and
805 * rate control, it might still start new aggregation
806 * sessions -- block that to make sure the tear-down
807 * will be sufficient.
809 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
810 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
812 ret = sta_info_hash_del(local, sta);
817 * for TDLS peers, make sure to return to the base channel before
820 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
821 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
822 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
825 list_del_rcu(&sta->list);
827 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
829 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
830 rcu_access_pointer(sdata->u.vlan.sta) == sta)
831 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
836 static void __sta_info_destroy_part2(struct sta_info *sta)
838 struct ieee80211_local *local = sta->local;
839 struct ieee80211_sub_if_data *sdata = sta->sdata;
840 struct station_info sinfo = {};
844 * NOTE: This assumes at least synchronize_net() was done
845 * after _part1 and before _part2!
849 lockdep_assert_held(&local->sta_mtx);
851 /* now keys can no longer be reached */
852 ieee80211_free_sta_keys(local, sta);
854 /* disable TIM bit - last chance to tell driver */
855 __sta_info_recalc_tim(sta, true);
860 local->sta_generation++;
862 while (sta->sta_state > IEEE80211_STA_NONE) {
863 ret = sta_info_move_state(sta, sta->sta_state - 1);
871 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
872 IEEE80211_STA_NOTEXIST);
873 WARN_ON_ONCE(ret != 0);
876 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
878 sta_set_sinfo(sta, &sinfo);
879 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
881 rate_control_remove_sta_debugfs(sta);
882 ieee80211_sta_debugfs_remove(sta);
883 ieee80211_recalc_min_chandef(sdata);
885 cleanup_single_sta(sta);
888 int __must_check __sta_info_destroy(struct sta_info *sta)
890 int err = __sta_info_destroy_part1(sta);
897 __sta_info_destroy_part2(sta);
902 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
904 struct sta_info *sta;
907 mutex_lock(&sdata->local->sta_mtx);
908 sta = sta_info_get(sdata, addr);
909 ret = __sta_info_destroy(sta);
910 mutex_unlock(&sdata->local->sta_mtx);
915 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
918 struct sta_info *sta;
921 mutex_lock(&sdata->local->sta_mtx);
922 sta = sta_info_get_bss(sdata, addr);
923 ret = __sta_info_destroy(sta);
924 mutex_unlock(&sdata->local->sta_mtx);
929 static void sta_info_cleanup(unsigned long data)
931 struct ieee80211_local *local = (struct ieee80211_local *) data;
932 struct sta_info *sta;
933 bool timer_needed = false;
936 list_for_each_entry_rcu(sta, &local->sta_list, list)
937 if (sta_info_cleanup_expire_buffered(local, sta))
941 if (local->quiescing)
947 mod_timer(&local->sta_cleanup,
948 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
951 void sta_info_init(struct ieee80211_local *local)
953 spin_lock_init(&local->tim_lock);
954 mutex_init(&local->sta_mtx);
955 INIT_LIST_HEAD(&local->sta_list);
957 setup_timer(&local->sta_cleanup, sta_info_cleanup,
958 (unsigned long)local);
961 void sta_info_stop(struct ieee80211_local *local)
963 del_timer_sync(&local->sta_cleanup);
967 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
969 struct ieee80211_local *local = sdata->local;
970 struct sta_info *sta, *tmp;
971 LIST_HEAD(free_list);
976 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
977 WARN_ON(vlans && !sdata->bss);
979 mutex_lock(&local->sta_mtx);
980 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
981 if (sdata == sta->sdata ||
982 (vlans && sdata->bss == sta->sdata->bss)) {
983 if (!WARN_ON(__sta_info_destroy_part1(sta)))
984 list_add(&sta->free_list, &free_list);
989 if (!list_empty(&free_list)) {
991 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
992 __sta_info_destroy_part2(sta);
994 mutex_unlock(&local->sta_mtx);
999 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1000 unsigned long exp_time)
1002 struct ieee80211_local *local = sdata->local;
1003 struct sta_info *sta, *tmp;
1005 mutex_lock(&local->sta_mtx);
1007 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1008 if (sdata != sta->sdata)
1011 if (time_after(jiffies, sta->last_rx + exp_time)) {
1012 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1015 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1016 test_sta_flag(sta, WLAN_STA_PS_STA))
1017 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1019 WARN_ON(__sta_info_destroy(sta));
1023 mutex_unlock(&local->sta_mtx);
1026 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1028 const u8 *localaddr)
1030 struct sta_info *sta, *nxt;
1033 * Just return a random station if localaddr is NULL
1034 * ... first in list.
1036 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1038 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1047 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1049 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1052 struct sta_info *sta;
1057 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1066 EXPORT_SYMBOL(ieee80211_find_sta);
1068 /* powersave support code */
1069 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1071 struct ieee80211_sub_if_data *sdata = sta->sdata;
1072 struct ieee80211_local *local = sdata->local;
1073 struct sk_buff_head pending;
1074 int filtered = 0, buffered = 0, ac;
1075 unsigned long flags;
1078 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1079 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1082 if (sdata->vif.type == NL80211_IFTYPE_AP)
1083 ps = &sdata->bss->ps;
1084 else if (ieee80211_vif_is_mesh(&sdata->vif))
1085 ps = &sdata->u.mesh.ps;
1089 clear_sta_flag(sta, WLAN_STA_SP);
1091 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1092 sta->driver_buffered_tids = 0;
1094 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1095 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1097 skb_queue_head_init(&pending);
1099 /* sync with ieee80211_tx_h_unicast_ps_buf */
1100 spin_lock(&sta->ps_lock);
1101 /* Send all buffered frames to the station */
1102 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1103 int count = skb_queue_len(&pending), tmp;
1105 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1106 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1107 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1108 tmp = skb_queue_len(&pending);
1109 filtered += tmp - count;
1112 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1113 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1114 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1115 tmp = skb_queue_len(&pending);
1116 buffered += tmp - count;
1119 ieee80211_add_pending_skbs(local, &pending);
1121 /* now we're no longer in the deliver code */
1122 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1124 /* The station might have polled and then woken up before we responded,
1125 * so clear these flags now to avoid them sticking around.
1127 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1128 clear_sta_flag(sta, WLAN_STA_UAPSD);
1129 spin_unlock(&sta->ps_lock);
1131 atomic_dec(&ps->num_sta_ps);
1133 /* This station just woke up and isn't aware of our SMPS state */
1134 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1135 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1136 sdata->smps_mode) &&
1137 sta->known_smps_mode != sdata->bss->req_smps &&
1138 sta_info_tx_streams(sta) != 1) {
1140 "%pM just woke up and MIMO capable - update SMPS\n",
1142 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1144 sdata->vif.bss_conf.bssid);
1147 local->total_ps_buffered -= buffered;
1149 sta_info_recalc_tim(sta);
1152 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1153 sta->sta.addr, sta->sta.aid, filtered, buffered);
1156 static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1157 struct sta_info *sta, int tid,
1158 enum ieee80211_frame_release_type reason,
1161 struct ieee80211_local *local = sdata->local;
1162 struct ieee80211_qos_hdr *nullfunc;
1163 struct sk_buff *skb;
1164 int size = sizeof(*nullfunc);
1166 bool qos = sta->sta.wme;
1167 struct ieee80211_tx_info *info;
1168 struct ieee80211_chanctx_conf *chanctx_conf;
1171 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1172 IEEE80211_STYPE_QOS_NULLFUNC |
1173 IEEE80211_FCTL_FROMDS);
1176 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1177 IEEE80211_STYPE_NULLFUNC |
1178 IEEE80211_FCTL_FROMDS);
1181 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1185 skb_reserve(skb, local->hw.extra_tx_headroom);
1187 nullfunc = (void *) skb_put(skb, size);
1188 nullfunc->frame_control = fc;
1189 nullfunc->duration_id = 0;
1190 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1191 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1192 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1193 nullfunc->seq_ctrl = 0;
1195 skb->priority = tid;
1196 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1198 nullfunc->qos_ctrl = cpu_to_le16(tid);
1200 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1201 nullfunc->qos_ctrl |=
1202 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1205 info = IEEE80211_SKB_CB(skb);
1208 * Tell TX path to send this frame even though the
1209 * STA may still remain is PS mode after this frame
1210 * exchange. Also set EOSP to indicate this packet
1211 * ends the poll/service period.
1213 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1214 IEEE80211_TX_STATUS_EOSP |
1215 IEEE80211_TX_CTL_REQ_TX_STATUS;
1217 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1220 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1223 skb->dev = sdata->dev;
1226 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1227 if (WARN_ON(!chanctx_conf)) {
1233 info->band = chanctx_conf->def.chan->band;
1234 ieee80211_xmit(sdata, sta, skb);
1238 static int find_highest_prio_tid(unsigned long tids)
1240 /* lower 3 TIDs aren't ordered perfectly */
1242 return fls(tids) - 1;
1243 /* TID 0 is BE just like TID 3 */
1246 return fls(tids) - 1;
1250 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1251 int n_frames, u8 ignored_acs,
1252 enum ieee80211_frame_release_type reason)
1254 struct ieee80211_sub_if_data *sdata = sta->sdata;
1255 struct ieee80211_local *local = sdata->local;
1256 bool more_data = false;
1258 unsigned long driver_release_tids = 0;
1259 struct sk_buff_head frames;
1261 /* Service or PS-Poll period starts */
1262 set_sta_flag(sta, WLAN_STA_SP);
1264 __skb_queue_head_init(&frames);
1266 /* Get response frame(s) and more data bit for the last one. */
1267 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1270 if (ignored_acs & BIT(ac))
1273 tids = ieee80211_tids_for_ac(ac);
1275 /* if we already have frames from software, then we can't also
1276 * release from hardware queues
1278 if (skb_queue_empty(&frames))
1279 driver_release_tids |= sta->driver_buffered_tids & tids;
1281 if (driver_release_tids) {
1282 /* If the driver has data on more than one TID then
1283 * certainly there's more data if we release just a
1284 * single frame now (from a single TID). This will
1285 * only happen for PS-Poll.
1287 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1288 hweight16(driver_release_tids) > 1) {
1290 driver_release_tids =
1291 BIT(find_highest_prio_tid(
1292 driver_release_tids));
1296 struct sk_buff *skb;
1298 while (n_frames > 0) {
1299 skb = skb_dequeue(&sta->tx_filtered[ac]);
1302 &sta->ps_tx_buf[ac]);
1304 local->total_ps_buffered--;
1309 __skb_queue_tail(&frames, skb);
1313 /* If we have more frames buffered on this AC, then set the
1314 * more-data bit and abort the loop since we can't send more
1315 * data from other ACs before the buffered frames from this.
1317 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1318 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1324 if (skb_queue_empty(&frames) && !driver_release_tids) {
1328 * For PS-Poll, this can only happen due to a race condition
1329 * when we set the TIM bit and the station notices it, but
1330 * before it can poll for the frame we expire it.
1332 * For uAPSD, this is said in the standard (11.2.1.5 h):
1333 * At each unscheduled SP for a non-AP STA, the AP shall
1334 * attempt to transmit at least one MSDU or MMPDU, but no
1335 * more than the value specified in the Max SP Length field
1336 * in the QoS Capability element from delivery-enabled ACs,
1337 * that are destined for the non-AP STA.
1339 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1342 /* This will evaluate to 1, 3, 5 or 7. */
1343 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1345 ieee80211_send_null_response(sdata, sta, tid, reason, true);
1346 } else if (!driver_release_tids) {
1347 struct sk_buff_head pending;
1348 struct sk_buff *skb;
1351 bool need_null = false;
1353 skb_queue_head_init(&pending);
1355 while ((skb = __skb_dequeue(&frames))) {
1356 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1357 struct ieee80211_hdr *hdr = (void *) skb->data;
1363 * Tell TX path to send this frame even though the
1364 * STA may still remain is PS mode after this frame
1367 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1368 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1371 * Use MoreData flag to indicate whether there are
1372 * more buffered frames for this STA
1374 if (more_data || !skb_queue_empty(&frames))
1375 hdr->frame_control |=
1376 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1378 hdr->frame_control &=
1379 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1381 if (ieee80211_is_data_qos(hdr->frame_control) ||
1382 ieee80211_is_qos_nullfunc(hdr->frame_control))
1383 qoshdr = ieee80211_get_qos_ctl(hdr);
1385 tids |= BIT(skb->priority);
1387 __skb_queue_tail(&pending, skb);
1389 /* end service period after last frame or add one */
1390 if (!skb_queue_empty(&frames))
1393 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1394 /* for PS-Poll, there's only one frame */
1395 info->flags |= IEEE80211_TX_STATUS_EOSP |
1396 IEEE80211_TX_CTL_REQ_TX_STATUS;
1400 /* For uAPSD, things are a bit more complicated. If the
1401 * last frame has a QoS header (i.e. is a QoS-data or
1402 * QoS-nulldata frame) then just set the EOSP bit there
1404 * If the frame doesn't have a QoS header (which means
1405 * it should be a bufferable MMPDU) then we can't set
1406 * the EOSP bit in the QoS header; add a QoS-nulldata
1407 * frame to the list to send it after the MMPDU.
1409 * Note that this code is only in the mac80211-release
1410 * code path, we assume that the driver will not buffer
1411 * anything but QoS-data frames, or if it does, will
1412 * create the QoS-nulldata frame by itself if needed.
1414 * Cf. 802.11-2012 10.2.1.10 (c).
1417 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1419 info->flags |= IEEE80211_TX_STATUS_EOSP |
1420 IEEE80211_TX_CTL_REQ_TX_STATUS;
1422 /* The standard isn't completely clear on this
1423 * as it says the more-data bit should be set
1424 * if there are more BUs. The QoS-Null frame
1425 * we're about to send isn't buffered yet, we
1426 * only create it below, but let's pretend it
1427 * was buffered just in case some clients only
1428 * expect more-data=0 when eosp=1.
1430 hdr->frame_control |=
1431 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1438 drv_allow_buffered_frames(local, sta, tids, num,
1441 ieee80211_add_pending_skbs(local, &pending);
1444 ieee80211_send_null_response(
1445 sdata, sta, find_highest_prio_tid(tids),
1448 sta_info_recalc_tim(sta);
1451 * We need to release a frame that is buffered somewhere in the
1452 * driver ... it'll have to handle that.
1453 * Note that the driver also has to check the number of frames
1454 * on the TIDs we're releasing from - if there are more than
1455 * n_frames it has to set the more-data bit (if we didn't ask
1456 * it to set it anyway due to other buffered frames); if there
1457 * are fewer than n_frames it has to make sure to adjust that
1458 * to allow the service period to end properly.
1460 drv_release_buffered_frames(local, sta, driver_release_tids,
1461 n_frames, reason, more_data);
1464 * Note that we don't recalculate the TIM bit here as it would
1465 * most likely have no effect at all unless the driver told us
1466 * that the TID(s) became empty before returning here from the
1468 * Either way, however, when the driver tells us that the TID(s)
1469 * became empty we'll do the TIM recalculation.
1474 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1476 u8 ignore_for_response = sta->sta.uapsd_queues;
1479 * If all ACs are delivery-enabled then we should reply
1480 * from any of them, if only some are enabled we reply
1481 * only from the non-enabled ones.
1483 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1484 ignore_for_response = 0;
1486 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1487 IEEE80211_FRAME_RELEASE_PSPOLL);
1490 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1492 int n_frames = sta->sta.max_sp;
1493 u8 delivery_enabled = sta->sta.uapsd_queues;
1496 * If we ever grow support for TSPEC this might happen if
1497 * the TSPEC update from hostapd comes in between a trigger
1498 * frame setting WLAN_STA_UAPSD in the RX path and this
1499 * actually getting called.
1501 if (!delivery_enabled)
1504 switch (sta->sta.max_sp) {
1515 /* XXX: what is a good value? */
1520 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1521 IEEE80211_FRAME_RELEASE_UAPSD);
1524 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1525 struct ieee80211_sta *pubsta, bool block)
1527 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1529 trace_api_sta_block_awake(sta->local, pubsta, block);
1532 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1536 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1539 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1540 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1541 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1542 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1543 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1544 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1545 /* must be asleep in this case */
1546 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1547 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1549 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1552 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1554 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1556 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1557 struct ieee80211_local *local = sta->local;
1559 trace_api_eosp(local, pubsta);
1561 clear_sta_flag(sta, WLAN_STA_SP);
1563 EXPORT_SYMBOL(ieee80211_sta_eosp);
1565 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1566 u8 tid, bool buffered)
1568 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1570 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1573 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1576 set_bit(tid, &sta->driver_buffered_tids);
1578 clear_bit(tid, &sta->driver_buffered_tids);
1580 sta_info_recalc_tim(sta);
1582 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1584 int sta_info_move_state(struct sta_info *sta,
1585 enum ieee80211_sta_state new_state)
1589 if (sta->sta_state == new_state)
1592 /* check allowed transitions first */
1594 switch (new_state) {
1595 case IEEE80211_STA_NONE:
1596 if (sta->sta_state != IEEE80211_STA_AUTH)
1599 case IEEE80211_STA_AUTH:
1600 if (sta->sta_state != IEEE80211_STA_NONE &&
1601 sta->sta_state != IEEE80211_STA_ASSOC)
1604 case IEEE80211_STA_ASSOC:
1605 if (sta->sta_state != IEEE80211_STA_AUTH &&
1606 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1609 case IEEE80211_STA_AUTHORIZED:
1610 if (sta->sta_state != IEEE80211_STA_ASSOC)
1614 WARN(1, "invalid state %d", new_state);
1618 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1619 sta->sta.addr, new_state);
1622 * notify the driver before the actual changes so it can
1623 * fail the transition
1625 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1626 int err = drv_sta_state(sta->local, sta->sdata, sta,
1627 sta->sta_state, new_state);
1632 /* reflect the change in all state variables */
1634 switch (new_state) {
1635 case IEEE80211_STA_NONE:
1636 if (sta->sta_state == IEEE80211_STA_AUTH)
1637 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1639 case IEEE80211_STA_AUTH:
1640 if (sta->sta_state == IEEE80211_STA_NONE)
1641 set_bit(WLAN_STA_AUTH, &sta->_flags);
1642 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1643 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1645 case IEEE80211_STA_ASSOC:
1646 if (sta->sta_state == IEEE80211_STA_AUTH) {
1647 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1648 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1649 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1650 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1651 !sta->sdata->u.vlan.sta))
1652 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1653 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1656 case IEEE80211_STA_AUTHORIZED:
1657 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1658 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1659 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1660 !sta->sdata->u.vlan.sta))
1661 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1662 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1669 sta->sta_state = new_state;
1674 u8 sta_info_tx_streams(struct sta_info *sta)
1676 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1679 if (!sta->sta.ht_cap.ht_supported)
1682 if (sta->sta.vht_cap.vht_supported) {
1685 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1687 for (i = 7; i >= 0; i--)
1688 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1689 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1693 if (ht_cap->mcs.rx_mask[3])
1695 else if (ht_cap->mcs.rx_mask[2])
1697 else if (ht_cap->mcs.rx_mask[1])
1702 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1705 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1706 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1709 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1711 struct ieee80211_sub_if_data *sdata = sta->sdata;
1712 struct ieee80211_local *local = sdata->local;
1713 struct rate_control_ref *ref = NULL;
1714 struct timespec uptime;
1718 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1719 ref = local->rate_ctrl;
1721 sinfo->generation = sdata->local->sta_generation;
1723 /* do before driver, so beacon filtering drivers have a
1724 * chance to e.g. just add the number of filtered beacons
1725 * (or just modify the value entirely, of course)
1727 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1728 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1730 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1732 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1733 BIT(NL80211_STA_INFO_STA_FLAGS) |
1734 BIT(NL80211_STA_INFO_BSS_PARAM) |
1735 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1736 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1737 BIT(NL80211_STA_INFO_BEACON_LOSS);
1739 ktime_get_ts(&uptime);
1740 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1741 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
1743 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1744 BIT(NL80211_STA_INFO_TX_BYTES)))) {
1745 sinfo->tx_bytes = 0;
1746 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1747 sinfo->tx_bytes += sta->tx_bytes[ac];
1748 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1751 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
1752 sinfo->tx_packets = 0;
1753 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1754 sinfo->tx_packets += sta->tx_packets[ac];
1755 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
1758 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1759 BIT(NL80211_STA_INFO_RX_BYTES)))) {
1760 sinfo->rx_bytes = sta->rx_bytes;
1761 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
1764 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1765 sinfo->rx_packets = sta->rx_packets;
1766 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
1769 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1770 sinfo->tx_retries = sta->tx_retry_count;
1771 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
1774 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1775 sinfo->tx_failed = sta->tx_retry_failed;
1776 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
1779 sinfo->rx_dropped_misc = sta->rx_dropped;
1780 sinfo->beacon_loss_count = sta->beacon_loss_count;
1782 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1783 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1784 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1785 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1786 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1789 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1790 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1791 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1792 sinfo->signal = (s8)sta->last_signal;
1793 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1796 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
1797 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1798 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
1803 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1804 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1805 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1806 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1808 sinfo->chains = sta->chains;
1809 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1810 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1811 sinfo->chain_signal_avg[i] =
1812 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1816 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1817 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1818 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
1821 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1822 sta_set_rate_info_rx(sta, &sinfo->rxrate);
1823 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
1826 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1827 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1828 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1830 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1831 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1832 tidstats->rx_msdu = sta->rx_msdu[i];
1835 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1836 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1837 tidstats->tx_msdu = sta->tx_msdu[i];
1840 if (!(tidstats->filled &
1841 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
1842 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1844 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1845 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1848 if (!(tidstats->filled &
1849 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
1850 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1852 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1853 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1857 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1858 #ifdef CONFIG_MAC80211_MESH
1859 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1860 BIT(NL80211_STA_INFO_PLID) |
1861 BIT(NL80211_STA_INFO_PLINK_STATE) |
1862 BIT(NL80211_STA_INFO_LOCAL_PM) |
1863 BIT(NL80211_STA_INFO_PEER_PM) |
1864 BIT(NL80211_STA_INFO_NONPEER_PM);
1866 sinfo->llid = sta->llid;
1867 sinfo->plid = sta->plid;
1868 sinfo->plink_state = sta->plink_state;
1869 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1870 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1871 sinfo->t_offset = sta->t_offset;
1873 sinfo->local_pm = sta->local_pm;
1874 sinfo->peer_pm = sta->peer_pm;
1875 sinfo->nonpeer_pm = sta->nonpeer_pm;
1879 sinfo->bss_param.flags = 0;
1880 if (sdata->vif.bss_conf.use_cts_prot)
1881 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1882 if (sdata->vif.bss_conf.use_short_preamble)
1883 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1884 if (sdata->vif.bss_conf.use_short_slot)
1885 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1886 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1887 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1889 sinfo->sta_flags.set = 0;
1890 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1891 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1892 BIT(NL80211_STA_FLAG_WME) |
1893 BIT(NL80211_STA_FLAG_MFP) |
1894 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1895 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1896 BIT(NL80211_STA_FLAG_TDLS_PEER);
1897 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1898 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1899 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1900 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1902 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1903 if (test_sta_flag(sta, WLAN_STA_MFP))
1904 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1905 if (test_sta_flag(sta, WLAN_STA_AUTH))
1906 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1907 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1908 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1909 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1910 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1912 /* check if the driver has a SW RC implementation */
1913 if (ref && ref->ops->get_expected_throughput)
1914 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1916 thr = drv_get_expected_throughput(local, &sta->sta);
1919 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
1920 sinfo->expected_throughput = thr;