2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
4 * Copyright 2013-2014 Intel Mobile Communications GmbH
5 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
6 * Copyright (C) 2018-2020 Intel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/etherdevice.h>
16 #include <linux/netdevice.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_arp.h>
21 #include <linux/timer.h>
22 #include <linux/rtnetlink.h>
24 #include <net/codel.h>
25 #include <net/mac80211.h>
26 #include "ieee80211_i.h"
27 #include "driver-ops.h"
30 #include "debugfs_sta.h"
35 * DOC: STA information lifetime rules
37 * STA info structures (&struct sta_info) are managed in a hash table
38 * for faster lookup and a list for iteration. They are managed using
39 * RCU, i.e. access to the list and hash table is protected by RCU.
41 * Upon allocating a STA info structure with sta_info_alloc(), the caller
42 * owns that structure. It must then insert it into the hash table using
43 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
44 * case (which acquires an rcu read section but must not be called from
45 * within one) will the pointer still be valid after the call. Note that
46 * the caller may not do much with the STA info before inserting it, in
47 * particular, it may not start any mesh peer link management or add
50 * When the insertion fails (sta_info_insert()) returns non-zero), the
51 * structure will have been freed by sta_info_insert()!
53 * Station entries are added by mac80211 when you establish a link with a
54 * peer. This means different things for the different type of interfaces
55 * we support. For a regular station this mean we add the AP sta when we
56 * receive an association response from the AP. For IBSS this occurs when
57 * get to know about a peer on the same IBSS. For WDS we add the sta for
58 * the peer immediately upon device open. When using AP mode we add stations
59 * for each respective station upon request from userspace through nl80211.
61 * In order to remove a STA info structure, various sta_info_destroy_*()
62 * calls are available.
64 * There is no concept of ownership on a STA entry, each structure is
65 * owned by the global hash table/list until it is removed. All users of
66 * the structure need to be RCU protected so that the structure won't be
67 * freed before they are done using it.
70 static const struct rhashtable_params sta_rht_params = {
71 .nelem_hint = 3, /* start small */
72 .automatic_shrinking = true,
73 .head_offset = offsetof(struct sta_info, hash_node),
74 .key_offset = offsetof(struct sta_info, addr),
76 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
79 /* Caller must hold local->sta_mtx */
80 static int sta_info_hash_del(struct ieee80211_local *local,
83 return rhltable_remove(&local->sta_hash, &sta->hash_node,
87 static void __cleanup_single_sta(struct sta_info *sta)
90 struct tid_ampdu_tx *tid_tx;
91 struct ieee80211_sub_if_data *sdata = sta->sdata;
92 struct ieee80211_local *local = sdata->local;
93 struct fq *fq = &local->fq;
96 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
97 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
98 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
99 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
100 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
101 ps = &sdata->bss->ps;
102 else if (ieee80211_vif_is_mesh(&sdata->vif))
103 ps = &sdata->u.mesh.ps;
107 clear_sta_flag(sta, WLAN_STA_PS_STA);
108 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
109 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
111 atomic_dec(&ps->num_sta_ps);
114 if (sta->sta.txq[0]) {
115 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
116 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
118 spin_lock_bh(&fq->lock);
119 ieee80211_txq_purge(local, txqi);
120 spin_unlock_bh(&fq->lock);
124 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
125 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
127 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
130 if (ieee80211_vif_is_mesh(&sdata->vif))
131 mesh_sta_cleanup(sta);
133 cancel_work_sync(&sta->drv_deliver_wk);
136 * Destroy aggregation state here. It would be nice to wait for the
137 * driver to finish aggregation stop and then clean up, but for now
138 * drivers have to handle aggregation stop being requested, followed
139 * directly by station destruction.
141 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
142 kfree(sta->ampdu_mlme.tid_start_tx[i]);
143 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
146 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
151 static void cleanup_single_sta(struct sta_info *sta)
153 struct ieee80211_sub_if_data *sdata = sta->sdata;
154 struct ieee80211_local *local = sdata->local;
156 __cleanup_single_sta(sta);
157 sta_info_free(local, sta);
160 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
163 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
166 /* protected by RCU */
167 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
170 struct ieee80211_local *local = sdata->local;
171 struct rhlist_head *tmp;
172 struct sta_info *sta;
175 for_each_sta_info(local, addr, sta, tmp) {
176 if (sta->sdata == sdata) {
178 /* this is safe as the caller must already hold
179 * another rcu read section or the mutex
189 * Get sta info either from the specified interface
190 * or from one of its vlans
192 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
195 struct ieee80211_local *local = sdata->local;
196 struct rhlist_head *tmp;
197 struct sta_info *sta;
200 for_each_sta_info(local, addr, sta, tmp) {
201 if (sta->sdata == sdata ||
202 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
204 /* this is safe as the caller must already hold
205 * another rcu read section or the mutex
214 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
217 struct ieee80211_local *local = sdata->local;
218 struct sta_info *sta;
221 list_for_each_entry_rcu(sta, &local->sta_list, list) {
222 if (sdata != sta->sdata)
235 * sta_info_free - free STA
237 * @local: pointer to the global information
238 * @sta: STA info to free
240 * This function must undo everything done by sta_info_alloc()
241 * that may happen before sta_info_insert(). It may only be
242 * called when sta_info_insert() has not been attempted (and
243 * if that fails, the station is freed anyway.)
245 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
248 * If we had used sta_info_pre_move_state() then we might not
249 * have gone through the state transitions down again, so do
250 * it here now (and warn if it's inserted).
252 * This will clear state such as fast TX/RX that may have been
253 * allocated during state transitions.
255 while (sta->sta_state > IEEE80211_STA_NONE) {
258 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
260 ret = sta_info_move_state(sta, sta->sta_state - 1);
261 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
266 rate_control_free_sta(sta);
268 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
271 kfree(to_txq_info(sta->sta.txq[0]));
272 kfree(rcu_dereference_raw(sta->sta.rates));
273 #ifdef CONFIG_MAC80211_MESH
276 free_percpu(sta->pcpu_rx_stats);
280 /* Caller must hold local->sta_mtx */
281 static int sta_info_hash_add(struct ieee80211_local *local,
282 struct sta_info *sta)
284 return rhltable_insert(&local->sta_hash, &sta->hash_node,
288 static void sta_deliver_ps_frames(struct work_struct *wk)
290 struct sta_info *sta;
292 sta = container_of(wk, struct sta_info, drv_deliver_wk);
298 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
299 ieee80211_sta_ps_deliver_wakeup(sta);
300 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
301 ieee80211_sta_ps_deliver_poll_response(sta);
302 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
303 ieee80211_sta_ps_deliver_uapsd(sta);
307 static int sta_prepare_rate_control(struct ieee80211_local *local,
308 struct sta_info *sta, gfp_t gfp)
310 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
313 sta->rate_ctrl = local->rate_ctrl;
314 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
316 if (!sta->rate_ctrl_priv)
322 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
323 const u8 *addr, gfp_t gfp)
325 struct ieee80211_local *local = sdata->local;
326 struct ieee80211_hw *hw = &local->hw;
327 struct sta_info *sta;
330 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
334 if (ieee80211_hw_check(hw, USES_RSS)) {
336 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
337 if (!sta->pcpu_rx_stats)
341 spin_lock_init(&sta->lock);
342 spin_lock_init(&sta->ps_lock);
343 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
344 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
345 mutex_init(&sta->ampdu_mlme.mtx);
346 #ifdef CONFIG_MAC80211_MESH
347 if (ieee80211_vif_is_mesh(&sdata->vif)) {
348 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
351 spin_lock_init(&sta->mesh->plink_lock);
352 if (ieee80211_vif_is_mesh(&sdata->vif) &&
353 !sdata->u.mesh.user_mpm)
354 init_timer(&sta->mesh->plink_timer);
355 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
359 memcpy(sta->addr, addr, ETH_ALEN);
360 memcpy(sta->sta.addr, addr, ETH_ALEN);
361 sta->sta.max_rx_aggregation_subframes =
362 local->hw.max_rx_aggregation_subframes;
366 sta->rx_stats.last_rx = jiffies;
368 u64_stats_init(&sta->rx_stats.syncp);
370 ieee80211_init_frag_cache(&sta->frags);
372 sta->sta_state = IEEE80211_STA_NONE;
374 /* Mark TID as unreserved */
375 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
377 sta->last_connected = ktime_get_seconds();
378 ewma_signal_init(&sta->rx_stats_avg.signal);
379 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
380 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
382 if (local->ops->wake_tx_queue) {
384 int size = sizeof(struct txq_info) +
385 ALIGN(hw->txq_data_size, sizeof(void *));
387 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
391 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
392 struct txq_info *txq = txq_data + i * size;
394 ieee80211_txq_init(sdata, sta, txq, i);
398 if (sta_prepare_rate_control(local, sta, gfp))
401 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
403 * timer_to_tid must be initialized with identity mapping
404 * to enable session_timer's data differentiation. See
405 * sta_rx_agg_session_timer_expired for usage.
407 sta->timer_to_tid[i] = i;
409 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
410 skb_queue_head_init(&sta->ps_tx_buf[i]);
411 skb_queue_head_init(&sta->tx_filtered[i]);
414 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
415 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
417 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
418 if (sdata->vif.type == NL80211_IFTYPE_AP ||
419 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
420 struct ieee80211_supported_band *sband;
423 sband = ieee80211_get_sband(sdata);
427 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
428 IEEE80211_HT_CAP_SM_PS_SHIFT;
430 * Assume that hostapd advertises our caps in the beacon and
431 * this is the known_smps_mode for a station that just assciated
434 case WLAN_HT_SMPS_CONTROL_DISABLED:
435 sta->known_smps_mode = IEEE80211_SMPS_OFF;
437 case WLAN_HT_SMPS_CONTROL_STATIC:
438 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
440 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
441 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
448 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
450 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
451 sta->cparams.target = MS2TIME(20);
452 sta->cparams.interval = MS2TIME(100);
453 sta->cparams.ecn = true;
455 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
461 kfree(to_txq_info(sta->sta.txq[0]));
463 free_percpu(sta->pcpu_rx_stats);
464 #ifdef CONFIG_MAC80211_MESH
471 static int sta_info_insert_check(struct sta_info *sta)
473 struct ieee80211_sub_if_data *sdata = sta->sdata;
476 * Can't be a WARN_ON because it can be triggered through a race:
477 * something inserts a STA (on one CPU) without holding the RTNL
478 * and another CPU turns off the net device.
480 if (unlikely(!ieee80211_sdata_running(sdata)))
483 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
484 is_multicast_ether_addr(sta->sta.addr)))
487 /* The RCU read lock is required by rhashtable due to
488 * asynchronous resize/rehash. We also require the mutex
492 lockdep_assert_held(&sdata->local->sta_mtx);
493 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
494 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
503 static int sta_info_insert_drv_state(struct ieee80211_local *local,
504 struct ieee80211_sub_if_data *sdata,
505 struct sta_info *sta)
507 enum ieee80211_sta_state state;
510 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
511 err = drv_sta_state(local, sdata, sta, state, state + 1);
518 * Drivers using legacy sta_add/sta_remove callbacks only
519 * get uploaded set to true after sta_add is called.
521 if (!local->ops->sta_add)
522 sta->uploaded = true;
526 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
528 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
529 sta->sta.addr, state + 1, err);
533 /* unwind on error */
534 for (; state > IEEE80211_STA_NOTEXIST; state--)
535 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
541 * should be called with sta_mtx locked
542 * this function replaces the mutex lock
545 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
547 struct ieee80211_local *local = sta->local;
548 struct ieee80211_sub_if_data *sdata = sta->sdata;
549 struct station_info *sinfo = NULL;
552 lockdep_assert_held(&local->sta_mtx);
554 /* check if STA exists already */
555 if (sta_info_get_bss(sdata, sta->sta.addr)) {
560 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
567 local->sta_generation++;
570 /* simplify things and don't accept BA sessions yet */
571 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
573 /* make the station visible */
574 err = sta_info_hash_add(local, sta);
578 list_add_tail_rcu(&sta->list, &local->sta_list);
581 err = sta_info_insert_drv_state(local, sdata, sta);
585 set_sta_flag(sta, WLAN_STA_INSERTED);
586 /* accept BA sessions now */
587 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
589 ieee80211_sta_debugfs_add(sta);
590 rate_control_add_sta_debugfs(sta);
592 sinfo->generation = local->sta_generation;
593 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
596 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
598 /* move reference to rcu-protected */
600 mutex_unlock(&local->sta_mtx);
602 if (ieee80211_vif_is_mesh(&sdata->vif))
603 mesh_accept_plinks_update(sdata);
607 sta_info_hash_del(local, sta);
608 list_del_rcu(&sta->list);
612 cleanup_single_sta(sta);
614 mutex_unlock(&local->sta_mtx);
620 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
622 struct ieee80211_local *local = sta->local;
627 mutex_lock(&local->sta_mtx);
629 err = sta_info_insert_check(sta);
631 sta_info_free(local, sta);
632 mutex_unlock(&local->sta_mtx);
637 return sta_info_insert_finish(sta);
640 int sta_info_insert(struct sta_info *sta)
642 int err = sta_info_insert_rcu(sta);
649 static inline void __bss_tim_set(u8 *tim, u16 id)
652 * This format has been mandated by the IEEE specifications,
653 * so this line may not be changed to use the __set_bit() format.
655 tim[id / 8] |= (1 << (id % 8));
658 static inline void __bss_tim_clear(u8 *tim, u16 id)
661 * This format has been mandated by the IEEE specifications,
662 * so this line may not be changed to use the __clear_bit() format.
664 tim[id / 8] &= ~(1 << (id % 8));
667 static inline bool __bss_tim_get(u8 *tim, u16 id)
670 * This format has been mandated by the IEEE specifications,
671 * so this line may not be changed to use the test_bit() format.
673 return tim[id / 8] & (1 << (id % 8));
676 static unsigned long ieee80211_tids_for_ac(int ac)
678 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
680 case IEEE80211_AC_VO:
681 return BIT(6) | BIT(7);
682 case IEEE80211_AC_VI:
683 return BIT(4) | BIT(5);
684 case IEEE80211_AC_BE:
685 return BIT(0) | BIT(3);
686 case IEEE80211_AC_BK:
687 return BIT(1) | BIT(2);
694 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
696 struct ieee80211_local *local = sta->local;
698 bool indicate_tim = false;
699 u8 ignore_for_tim = sta->sta.uapsd_queues;
701 u16 id = sta->sta.aid;
703 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
704 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
705 if (WARN_ON_ONCE(!sta->sdata->bss))
708 ps = &sta->sdata->bss->ps;
709 #ifdef CONFIG_MAC80211_MESH
710 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
711 ps = &sta->sdata->u.mesh.ps;
717 /* No need to do anything if the driver does all */
718 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
725 * If all ACs are delivery-enabled then we should build
726 * the TIM bit for all ACs anyway; if only some are then
727 * we ignore those and build the TIM bit using only the
730 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
734 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
736 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
739 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
742 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
743 !skb_queue_empty(&sta->ps_tx_buf[ac]);
747 tids = ieee80211_tids_for_ac(ac);
750 sta->driver_buffered_tids & tids;
752 sta->txq_buffered_tids & tids;
756 spin_lock_bh(&local->tim_lock);
758 if (indicate_tim == __bss_tim_get(ps->tim, id))
762 __bss_tim_set(ps->tim, id);
764 __bss_tim_clear(ps->tim, id);
766 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
767 local->tim_in_locked_section = true;
768 drv_set_tim(local, &sta->sta, indicate_tim);
769 local->tim_in_locked_section = false;
773 spin_unlock_bh(&local->tim_lock);
776 void sta_info_recalc_tim(struct sta_info *sta)
778 __sta_info_recalc_tim(sta, false);
781 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
783 struct ieee80211_tx_info *info;
789 info = IEEE80211_SKB_CB(skb);
791 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
792 timeout = (sta->listen_interval *
793 sta->sdata->vif.bss_conf.beacon_int *
795 if (timeout < STA_TX_BUFFER_EXPIRE)
796 timeout = STA_TX_BUFFER_EXPIRE;
797 return time_after(jiffies, info->control.jiffies + timeout);
801 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
802 struct sta_info *sta, int ac)
808 * First check for frames that should expire on the filtered
809 * queue. Frames here were rejected by the driver and are on
810 * a separate queue to avoid reordering with normal PS-buffered
811 * frames. They also aren't accounted for right now in the
812 * total_ps_buffered counter.
815 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
816 skb = skb_peek(&sta->tx_filtered[ac]);
817 if (sta_info_buffer_expired(sta, skb))
818 skb = __skb_dequeue(&sta->tx_filtered[ac]);
821 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
824 * Frames are queued in order, so if this one
825 * hasn't expired yet we can stop testing. If
826 * we actually reached the end of the queue we
827 * also need to stop, of course.
831 ieee80211_free_txskb(&local->hw, skb);
835 * Now also check the normal PS-buffered queue, this will
836 * only find something if the filtered queue was emptied
837 * since the filtered frames are all before the normal PS
841 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
842 skb = skb_peek(&sta->ps_tx_buf[ac]);
843 if (sta_info_buffer_expired(sta, skb))
844 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
847 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
850 * frames are queued in order, so if this one
851 * hasn't expired yet (or we reached the end of
852 * the queue) we can stop testing
857 local->total_ps_buffered--;
858 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
860 ieee80211_free_txskb(&local->hw, skb);
864 * Finally, recalculate the TIM bit for this station -- it might
865 * now be clear because the station was too slow to retrieve its
868 sta_info_recalc_tim(sta);
871 * Return whether there are any frames still buffered, this is
872 * used to check whether the cleanup timer still needs to run,
873 * if there are no frames we don't need to rearm the timer.
875 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
876 skb_queue_empty(&sta->tx_filtered[ac]));
879 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
880 struct sta_info *sta)
882 bool have_buffered = false;
885 /* This is only necessary for stations on BSS/MBSS interfaces */
886 if (!sta->sdata->bss &&
887 !ieee80211_vif_is_mesh(&sta->sdata->vif))
890 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
892 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
894 return have_buffered;
897 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
899 struct ieee80211_local *local;
900 struct ieee80211_sub_if_data *sdata;
911 lockdep_assert_held(&local->sta_mtx);
914 * Before removing the station from the driver and
915 * rate control, it might still start new aggregation
916 * sessions -- block that to make sure the tear-down
917 * will be sufficient.
919 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
920 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
923 * Before removing the station from the driver there might be pending
924 * rx frames on RSS queues sent prior to the disassociation - wait for
925 * all such frames to be processed.
927 drv_sync_rx_queues(local, sta);
929 ret = sta_info_hash_del(local, sta);
934 * for TDLS peers, make sure to return to the base channel before
937 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
938 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
939 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
942 list_del_rcu(&sta->list);
945 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
947 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
948 rcu_access_pointer(sdata->u.vlan.sta) == sta)
949 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
954 static void __sta_info_destroy_part2(struct sta_info *sta)
956 struct ieee80211_local *local = sta->local;
957 struct ieee80211_sub_if_data *sdata = sta->sdata;
958 struct station_info *sinfo;
962 * NOTE: This assumes at least synchronize_net() was done
963 * after _part1 and before _part2!
967 lockdep_assert_held(&local->sta_mtx);
969 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
970 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
974 /* now keys can no longer be reached */
975 ieee80211_free_sta_keys(local, sta);
977 /* disable TIM bit - last chance to tell driver */
978 __sta_info_recalc_tim(sta, true);
983 local->sta_generation++;
985 while (sta->sta_state > IEEE80211_STA_NONE) {
986 ret = sta_info_move_state(sta, sta->sta_state - 1);
994 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
995 IEEE80211_STA_NOTEXIST);
996 WARN_ON_ONCE(ret != 0);
999 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1001 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1003 sta_set_sinfo(sta, sinfo);
1004 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1007 rate_control_remove_sta_debugfs(sta);
1008 ieee80211_sta_debugfs_remove(sta);
1010 ieee80211_destroy_frag_cache(&sta->frags);
1012 cleanup_single_sta(sta);
1015 int __must_check __sta_info_destroy(struct sta_info *sta)
1017 int err = __sta_info_destroy_part1(sta);
1024 __sta_info_destroy_part2(sta);
1029 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1031 struct sta_info *sta;
1034 mutex_lock(&sdata->local->sta_mtx);
1035 sta = sta_info_get(sdata, addr);
1036 ret = __sta_info_destroy(sta);
1037 mutex_unlock(&sdata->local->sta_mtx);
1042 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1045 struct sta_info *sta;
1048 mutex_lock(&sdata->local->sta_mtx);
1049 sta = sta_info_get_bss(sdata, addr);
1050 ret = __sta_info_destroy(sta);
1051 mutex_unlock(&sdata->local->sta_mtx);
1056 static void sta_info_cleanup(unsigned long data)
1058 struct ieee80211_local *local = (struct ieee80211_local *) data;
1059 struct sta_info *sta;
1060 bool timer_needed = false;
1063 list_for_each_entry_rcu(sta, &local->sta_list, list)
1064 if (sta_info_cleanup_expire_buffered(local, sta))
1065 timer_needed = true;
1068 if (local->quiescing)
1074 mod_timer(&local->sta_cleanup,
1075 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1078 int sta_info_init(struct ieee80211_local *local)
1082 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1086 spin_lock_init(&local->tim_lock);
1087 mutex_init(&local->sta_mtx);
1088 INIT_LIST_HEAD(&local->sta_list);
1090 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1091 (unsigned long)local);
1095 void sta_info_stop(struct ieee80211_local *local)
1097 del_timer_sync(&local->sta_cleanup);
1098 rhltable_destroy(&local->sta_hash);
1102 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1104 struct ieee80211_local *local = sdata->local;
1105 struct sta_info *sta, *tmp;
1106 LIST_HEAD(free_list);
1111 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1112 WARN_ON(vlans && !sdata->bss);
1114 mutex_lock(&local->sta_mtx);
1115 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1116 if (sdata == sta->sdata ||
1117 (vlans && sdata->bss == sta->sdata->bss)) {
1118 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1119 list_add(&sta->free_list, &free_list);
1124 if (!list_empty(&free_list)) {
1126 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1127 __sta_info_destroy_part2(sta);
1129 mutex_unlock(&local->sta_mtx);
1134 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1135 unsigned long exp_time)
1137 struct ieee80211_local *local = sdata->local;
1138 struct sta_info *sta, *tmp;
1140 mutex_lock(&local->sta_mtx);
1142 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1143 unsigned long last_active = ieee80211_sta_last_active(sta);
1145 if (sdata != sta->sdata)
1148 if (time_is_before_jiffies(last_active + exp_time)) {
1149 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1152 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1153 test_sta_flag(sta, WLAN_STA_PS_STA))
1154 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1156 WARN_ON(__sta_info_destroy(sta));
1160 mutex_unlock(&local->sta_mtx);
1163 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1165 const u8 *localaddr)
1167 struct ieee80211_local *local = hw_to_local(hw);
1168 struct rhlist_head *tmp;
1169 struct sta_info *sta;
1172 * Just return a random station if localaddr is NULL
1173 * ... first in list.
1175 for_each_sta_info(local, addr, sta, tmp) {
1177 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1186 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1188 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1191 struct sta_info *sta;
1196 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1205 EXPORT_SYMBOL(ieee80211_find_sta);
1207 /* powersave support code */
1208 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1210 struct ieee80211_sub_if_data *sdata = sta->sdata;
1211 struct ieee80211_local *local = sdata->local;
1212 struct sk_buff_head pending;
1213 int filtered = 0, buffered = 0, ac, i;
1214 unsigned long flags;
1217 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1218 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1221 if (sdata->vif.type == NL80211_IFTYPE_AP)
1222 ps = &sdata->bss->ps;
1223 else if (ieee80211_vif_is_mesh(&sdata->vif))
1224 ps = &sdata->u.mesh.ps;
1228 clear_sta_flag(sta, WLAN_STA_SP);
1230 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1231 sta->driver_buffered_tids = 0;
1232 sta->txq_buffered_tids = 0;
1234 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1235 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1237 if (sta->sta.txq[0]) {
1238 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1239 if (!txq_has_queue(sta->sta.txq[i]))
1242 drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1246 skb_queue_head_init(&pending);
1248 /* sync with ieee80211_tx_h_unicast_ps_buf */
1249 spin_lock(&sta->ps_lock);
1250 /* Send all buffered frames to the station */
1251 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1252 int count = skb_queue_len(&pending), tmp;
1254 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1255 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1256 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1257 tmp = skb_queue_len(&pending);
1258 filtered += tmp - count;
1261 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1262 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1263 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1264 tmp = skb_queue_len(&pending);
1265 buffered += tmp - count;
1268 ieee80211_add_pending_skbs(local, &pending);
1270 /* now we're no longer in the deliver code */
1271 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1273 /* The station might have polled and then woken up before we responded,
1274 * so clear these flags now to avoid them sticking around.
1276 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1277 clear_sta_flag(sta, WLAN_STA_UAPSD);
1278 spin_unlock(&sta->ps_lock);
1280 atomic_dec(&ps->num_sta_ps);
1282 /* This station just woke up and isn't aware of our SMPS state */
1283 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1284 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1285 sdata->smps_mode) &&
1286 sta->known_smps_mode != sdata->bss->req_smps &&
1287 sta_info_tx_streams(sta) != 1) {
1289 "%pM just woke up and MIMO capable - update SMPS\n",
1291 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1293 sdata->vif.bss_conf.bssid);
1296 local->total_ps_buffered -= buffered;
1298 sta_info_recalc_tim(sta);
1301 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1302 sta->sta.addr, sta->sta.aid, filtered, buffered);
1304 ieee80211_check_fast_xmit(sta);
1307 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1308 enum ieee80211_frame_release_type reason,
1309 bool call_driver, bool more_data)
1311 struct ieee80211_sub_if_data *sdata = sta->sdata;
1312 struct ieee80211_local *local = sdata->local;
1313 struct ieee80211_qos_hdr *nullfunc;
1314 struct sk_buff *skb;
1315 int size = sizeof(*nullfunc);
1317 bool qos = sta->sta.wme;
1318 struct ieee80211_tx_info *info;
1319 struct ieee80211_chanctx_conf *chanctx_conf;
1322 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1323 IEEE80211_STYPE_QOS_NULLFUNC |
1324 IEEE80211_FCTL_FROMDS);
1327 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1328 IEEE80211_STYPE_NULLFUNC |
1329 IEEE80211_FCTL_FROMDS);
1332 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1336 skb_reserve(skb, local->hw.extra_tx_headroom);
1338 nullfunc = skb_put(skb, size);
1339 nullfunc->frame_control = fc;
1340 nullfunc->duration_id = 0;
1341 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1342 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1343 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1344 nullfunc->seq_ctrl = 0;
1346 skb->priority = tid;
1347 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1349 nullfunc->qos_ctrl = cpu_to_le16(tid);
1351 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1352 nullfunc->qos_ctrl |=
1353 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1355 nullfunc->frame_control |=
1356 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1360 info = IEEE80211_SKB_CB(skb);
1363 * Tell TX path to send this frame even though the
1364 * STA may still remain is PS mode after this frame
1365 * exchange. Also set EOSP to indicate this packet
1366 * ends the poll/service period.
1368 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1369 IEEE80211_TX_STATUS_EOSP |
1370 IEEE80211_TX_CTL_REQ_TX_STATUS;
1372 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1375 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1378 skb->dev = sdata->dev;
1381 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1382 if (WARN_ON(!chanctx_conf)) {
1388 info->band = chanctx_conf->def.chan->band;
1389 ieee80211_xmit(sdata, sta, skb);
1393 static int find_highest_prio_tid(unsigned long tids)
1395 /* lower 3 TIDs aren't ordered perfectly */
1397 return fls(tids) - 1;
1398 /* TID 0 is BE just like TID 3 */
1401 return fls(tids) - 1;
1404 /* Indicates if the MORE_DATA bit should be set in the last
1405 * frame obtained by ieee80211_sta_ps_get_frames.
1406 * Note that driver_release_tids is relevant only if
1407 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1410 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1411 enum ieee80211_frame_release_type reason,
1412 unsigned long driver_release_tids)
1416 /* If the driver has data on more than one TID then
1417 * certainly there's more data if we release just a
1418 * single frame now (from a single TID). This will
1419 * only happen for PS-Poll.
1421 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1422 hweight16(driver_release_tids) > 1)
1425 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1426 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1429 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1430 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1438 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1439 enum ieee80211_frame_release_type reason,
1440 struct sk_buff_head *frames,
1441 unsigned long *driver_release_tids)
1443 struct ieee80211_sub_if_data *sdata = sta->sdata;
1444 struct ieee80211_local *local = sdata->local;
1447 /* Get response frame(s) and more data bit for the last one. */
1448 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1451 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1454 tids = ieee80211_tids_for_ac(ac);
1456 /* if we already have frames from software, then we can't also
1457 * release from hardware queues
1459 if (skb_queue_empty(frames)) {
1460 *driver_release_tids |=
1461 sta->driver_buffered_tids & tids;
1462 *driver_release_tids |= sta->txq_buffered_tids & tids;
1465 if (!*driver_release_tids) {
1466 struct sk_buff *skb;
1468 while (n_frames > 0) {
1469 skb = skb_dequeue(&sta->tx_filtered[ac]);
1472 &sta->ps_tx_buf[ac]);
1474 local->total_ps_buffered--;
1479 __skb_queue_tail(frames, skb);
1483 /* If we have more frames buffered on this AC, then abort the
1484 * loop since we can't send more data from other ACs before
1485 * the buffered frames from this.
1487 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1488 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1494 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1495 int n_frames, u8 ignored_acs,
1496 enum ieee80211_frame_release_type reason)
1498 struct ieee80211_sub_if_data *sdata = sta->sdata;
1499 struct ieee80211_local *local = sdata->local;
1500 unsigned long driver_release_tids = 0;
1501 struct sk_buff_head frames;
1504 /* Service or PS-Poll period starts */
1505 set_sta_flag(sta, WLAN_STA_SP);
1507 __skb_queue_head_init(&frames);
1509 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1510 &frames, &driver_release_tids);
1512 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1514 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1515 driver_release_tids =
1516 BIT(find_highest_prio_tid(driver_release_tids));
1518 if (skb_queue_empty(&frames) && !driver_release_tids) {
1522 * For PS-Poll, this can only happen due to a race condition
1523 * when we set the TIM bit and the station notices it, but
1524 * before it can poll for the frame we expire it.
1526 * For uAPSD, this is said in the standard (11.2.1.5 h):
1527 * At each unscheduled SP for a non-AP STA, the AP shall
1528 * attempt to transmit at least one MSDU or MMPDU, but no
1529 * more than the value specified in the Max SP Length field
1530 * in the QoS Capability element from delivery-enabled ACs,
1531 * that are destined for the non-AP STA.
1533 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1536 /* This will evaluate to 1, 3, 5 or 7. */
1537 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1538 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1542 ieee80211_send_null_response(sta, tid, reason, true, false);
1543 } else if (!driver_release_tids) {
1544 struct sk_buff_head pending;
1545 struct sk_buff *skb;
1548 bool need_null = false;
1550 skb_queue_head_init(&pending);
1552 while ((skb = __skb_dequeue(&frames))) {
1553 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1554 struct ieee80211_hdr *hdr = (void *) skb->data;
1560 * Tell TX path to send this frame even though the
1561 * STA may still remain is PS mode after this frame
1564 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1565 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1568 * Use MoreData flag to indicate whether there are
1569 * more buffered frames for this STA
1571 if (more_data || !skb_queue_empty(&frames))
1572 hdr->frame_control |=
1573 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1575 hdr->frame_control &=
1576 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1578 if (ieee80211_is_data_qos(hdr->frame_control) ||
1579 ieee80211_is_qos_nullfunc(hdr->frame_control))
1580 qoshdr = ieee80211_get_qos_ctl(hdr);
1582 tids |= BIT(skb->priority);
1584 __skb_queue_tail(&pending, skb);
1586 /* end service period after last frame or add one */
1587 if (!skb_queue_empty(&frames))
1590 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1591 /* for PS-Poll, there's only one frame */
1592 info->flags |= IEEE80211_TX_STATUS_EOSP |
1593 IEEE80211_TX_CTL_REQ_TX_STATUS;
1597 /* For uAPSD, things are a bit more complicated. If the
1598 * last frame has a QoS header (i.e. is a QoS-data or
1599 * QoS-nulldata frame) then just set the EOSP bit there
1601 * If the frame doesn't have a QoS header (which means
1602 * it should be a bufferable MMPDU) then we can't set
1603 * the EOSP bit in the QoS header; add a QoS-nulldata
1604 * frame to the list to send it after the MMPDU.
1606 * Note that this code is only in the mac80211-release
1607 * code path, we assume that the driver will not buffer
1608 * anything but QoS-data frames, or if it does, will
1609 * create the QoS-nulldata frame by itself if needed.
1611 * Cf. 802.11-2012 10.2.1.10 (c).
1614 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1616 info->flags |= IEEE80211_TX_STATUS_EOSP |
1617 IEEE80211_TX_CTL_REQ_TX_STATUS;
1619 /* The standard isn't completely clear on this
1620 * as it says the more-data bit should be set
1621 * if there are more BUs. The QoS-Null frame
1622 * we're about to send isn't buffered yet, we
1623 * only create it below, but let's pretend it
1624 * was buffered just in case some clients only
1625 * expect more-data=0 when eosp=1.
1627 hdr->frame_control |=
1628 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1635 drv_allow_buffered_frames(local, sta, tids, num,
1638 ieee80211_add_pending_skbs(local, &pending);
1641 ieee80211_send_null_response(
1642 sta, find_highest_prio_tid(tids),
1643 reason, false, false);
1645 sta_info_recalc_tim(sta);
1650 * We need to release a frame that is buffered somewhere in the
1651 * driver ... it'll have to handle that.
1652 * Note that the driver also has to check the number of frames
1653 * on the TIDs we're releasing from - if there are more than
1654 * n_frames it has to set the more-data bit (if we didn't ask
1655 * it to set it anyway due to other buffered frames); if there
1656 * are fewer than n_frames it has to make sure to adjust that
1657 * to allow the service period to end properly.
1659 drv_release_buffered_frames(local, sta, driver_release_tids,
1660 n_frames, reason, more_data);
1663 * Note that we don't recalculate the TIM bit here as it would
1664 * most likely have no effect at all unless the driver told us
1665 * that the TID(s) became empty before returning here from the
1667 * Either way, however, when the driver tells us that the TID(s)
1668 * became empty or we find that a txq became empty, we'll do the
1669 * TIM recalculation.
1672 if (!sta->sta.txq[0])
1675 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1676 if (!(driver_release_tids & BIT(tid)) ||
1677 txq_has_queue(sta->sta.txq[tid]))
1680 sta_info_recalc_tim(sta);
1686 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1688 u8 ignore_for_response = sta->sta.uapsd_queues;
1691 * If all ACs are delivery-enabled then we should reply
1692 * from any of them, if only some are enabled we reply
1693 * only from the non-enabled ones.
1695 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1696 ignore_for_response = 0;
1698 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1699 IEEE80211_FRAME_RELEASE_PSPOLL);
1702 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1704 int n_frames = sta->sta.max_sp;
1705 u8 delivery_enabled = sta->sta.uapsd_queues;
1708 * If we ever grow support for TSPEC this might happen if
1709 * the TSPEC update from hostapd comes in between a trigger
1710 * frame setting WLAN_STA_UAPSD in the RX path and this
1711 * actually getting called.
1713 if (!delivery_enabled)
1716 switch (sta->sta.max_sp) {
1727 /* XXX: what is a good value? */
1732 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1733 IEEE80211_FRAME_RELEASE_UAPSD);
1736 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1737 struct ieee80211_sta *pubsta, bool block)
1739 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1741 trace_api_sta_block_awake(sta->local, pubsta, block);
1744 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1745 ieee80211_clear_fast_xmit(sta);
1749 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1752 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1753 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1754 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1755 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1756 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1757 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1758 /* must be asleep in this case */
1759 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1760 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1762 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1763 ieee80211_check_fast_xmit(sta);
1766 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1768 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1770 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1771 struct ieee80211_local *local = sta->local;
1773 trace_api_eosp(local, pubsta);
1775 clear_sta_flag(sta, WLAN_STA_SP);
1777 EXPORT_SYMBOL(ieee80211_sta_eosp);
1779 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1781 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1782 enum ieee80211_frame_release_type reason;
1785 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1787 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1788 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1791 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1793 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1795 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1796 u8 tid, bool buffered)
1798 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1800 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1803 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1806 set_bit(tid, &sta->driver_buffered_tids);
1808 clear_bit(tid, &sta->driver_buffered_tids);
1810 sta_info_recalc_tim(sta);
1812 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1815 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
1817 struct ieee80211_local *local = sdata->local;
1818 bool allow_p2p_go_ps = sdata->vif.p2p;
1819 struct sta_info *sta;
1822 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1823 if (sdata != sta->sdata ||
1824 !test_sta_flag(sta, WLAN_STA_ASSOC))
1826 if (!sta->sta.support_p2p_ps) {
1827 allow_p2p_go_ps = false;
1833 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
1834 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
1835 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
1839 int sta_info_move_state(struct sta_info *sta,
1840 enum ieee80211_sta_state new_state)
1844 if (sta->sta_state == new_state)
1847 /* check allowed transitions first */
1849 switch (new_state) {
1850 case IEEE80211_STA_NONE:
1851 if (sta->sta_state != IEEE80211_STA_AUTH)
1854 case IEEE80211_STA_AUTH:
1855 if (sta->sta_state != IEEE80211_STA_NONE &&
1856 sta->sta_state != IEEE80211_STA_ASSOC)
1859 case IEEE80211_STA_ASSOC:
1860 if (sta->sta_state != IEEE80211_STA_AUTH &&
1861 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1864 case IEEE80211_STA_AUTHORIZED:
1865 if (sta->sta_state != IEEE80211_STA_ASSOC)
1869 WARN(1, "invalid state %d", new_state);
1873 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1874 sta->sta.addr, new_state);
1877 * notify the driver before the actual changes so it can
1878 * fail the transition
1880 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1881 int err = drv_sta_state(sta->local, sta->sdata, sta,
1882 sta->sta_state, new_state);
1887 /* reflect the change in all state variables */
1889 switch (new_state) {
1890 case IEEE80211_STA_NONE:
1891 if (sta->sta_state == IEEE80211_STA_AUTH)
1892 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1894 case IEEE80211_STA_AUTH:
1895 if (sta->sta_state == IEEE80211_STA_NONE) {
1896 set_bit(WLAN_STA_AUTH, &sta->_flags);
1897 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1898 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1899 ieee80211_recalc_min_chandef(sta->sdata);
1900 if (!sta->sta.support_p2p_ps)
1901 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1904 case IEEE80211_STA_ASSOC:
1905 if (sta->sta_state == IEEE80211_STA_AUTH) {
1906 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1907 ieee80211_recalc_min_chandef(sta->sdata);
1908 if (!sta->sta.support_p2p_ps)
1909 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1910 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1911 ieee80211_vif_dec_num_mcast(sta->sdata);
1912 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1913 ieee80211_clear_fast_xmit(sta);
1914 ieee80211_clear_fast_rx(sta);
1917 case IEEE80211_STA_AUTHORIZED:
1918 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1919 ieee80211_vif_inc_num_mcast(sta->sdata);
1920 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1921 ieee80211_check_fast_xmit(sta);
1922 ieee80211_check_fast_rx(sta);
1924 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1925 sta->sdata->vif.type == NL80211_IFTYPE_AP)
1926 cfg80211_send_layer2_update(sta->sdata->dev,
1933 sta->sta_state = new_state;
1938 u8 sta_info_tx_streams(struct sta_info *sta)
1940 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1943 if (!sta->sta.ht_cap.ht_supported)
1946 if (sta->sta.vht_cap.vht_supported) {
1949 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1951 for (i = 7; i >= 0; i--)
1952 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1953 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1957 if (ht_cap->mcs.rx_mask[3])
1959 else if (ht_cap->mcs.rx_mask[2])
1961 else if (ht_cap->mcs.rx_mask[1])
1966 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1969 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1970 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1973 static struct ieee80211_sta_rx_stats *
1974 sta_get_last_rx_stats(struct sta_info *sta)
1976 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1977 struct ieee80211_local *local = sta->local;
1980 if (!ieee80211_hw_check(&local->hw, USES_RSS))
1983 for_each_possible_cpu(cpu) {
1984 struct ieee80211_sta_rx_stats *cpustats;
1986 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1988 if (time_after(cpustats->last_rx, stats->last_rx))
1995 static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
1996 struct rate_info *rinfo)
1998 rinfo->bw = STA_STATS_GET(BW, rate);
2000 switch (STA_STATS_GET(TYPE, rate)) {
2001 case STA_STATS_RATE_TYPE_VHT:
2002 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2003 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2004 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2005 if (STA_STATS_GET(SGI, rate))
2006 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2008 case STA_STATS_RATE_TYPE_HT:
2009 rinfo->flags = RATE_INFO_FLAGS_MCS;
2010 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2011 if (STA_STATS_GET(SGI, rate))
2012 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2014 case STA_STATS_RATE_TYPE_LEGACY: {
2015 struct ieee80211_supported_band *sband;
2018 int band = STA_STATS_GET(LEGACY_BAND, rate);
2019 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2022 sband = local->hw.wiphy->bands[band];
2024 if (WARN_ON_ONCE(!sband->bitrates))
2027 brate = sband->bitrates[rate_idx].bitrate;
2028 if (rinfo->bw == RATE_INFO_BW_5)
2030 else if (rinfo->bw == RATE_INFO_BW_10)
2034 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2040 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2042 u16 rate = ACCESS_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2044 if (rate == STA_STATS_RATE_INVALID)
2047 sta_stats_decode_rate(sta->local, rate, rinfo);
2051 static void sta_set_tidstats(struct sta_info *sta,
2052 struct cfg80211_tid_stats *tidstats,
2055 struct ieee80211_local *local = sta->local;
2057 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2061 start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2062 tidstats->rx_msdu = sta->rx_stats.msdu[tid];
2063 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2065 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2068 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2069 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2070 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2073 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2074 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2075 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2076 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2079 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2080 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2081 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2082 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2086 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2092 start = u64_stats_fetch_begin(&rxstats->syncp);
2093 value = rxstats->bytes;
2094 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2099 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2101 struct ieee80211_sub_if_data *sdata = sta->sdata;
2102 struct ieee80211_local *local = sdata->local;
2105 struct ieee80211_sta_rx_stats *last_rxstats;
2107 last_rxstats = sta_get_last_rx_stats(sta);
2109 sinfo->generation = sdata->local->sta_generation;
2111 /* do before driver, so beacon filtering drivers have a
2112 * chance to e.g. just add the number of filtered beacons
2113 * (or just modify the value entirely, of course)
2115 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2116 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2118 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2120 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2121 BIT(NL80211_STA_INFO_STA_FLAGS) |
2122 BIT(NL80211_STA_INFO_BSS_PARAM) |
2123 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
2124 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2126 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2127 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2128 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2131 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2132 sinfo->inactive_time =
2133 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2135 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2136 BIT(NL80211_STA_INFO_TX_BYTES)))) {
2137 sinfo->tx_bytes = 0;
2138 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2139 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2140 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2143 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
2144 sinfo->tx_packets = 0;
2145 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2146 sinfo->tx_packets += sta->tx_stats.packets[ac];
2147 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
2150 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2151 BIT(NL80211_STA_INFO_RX_BYTES)))) {
2152 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
2154 if (sta->pcpu_rx_stats) {
2155 for_each_possible_cpu(cpu) {
2156 struct ieee80211_sta_rx_stats *cpurxs;
2158 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2159 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2163 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
2166 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2167 sinfo->rx_packets = sta->rx_stats.packets;
2168 if (sta->pcpu_rx_stats) {
2169 for_each_possible_cpu(cpu) {
2170 struct ieee80211_sta_rx_stats *cpurxs;
2172 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2173 sinfo->rx_packets += cpurxs->packets;
2176 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
2179 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2180 sinfo->tx_retries = sta->status_stats.retry_count;
2181 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
2184 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2185 sinfo->tx_failed = sta->status_stats.retry_failed;
2186 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
2189 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2190 if (sta->pcpu_rx_stats) {
2191 for_each_possible_cpu(cpu) {
2192 struct ieee80211_sta_rx_stats *cpurxs;
2194 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2195 sinfo->rx_dropped_misc += cpurxs->dropped;
2199 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2200 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2201 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2202 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2203 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2206 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2207 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2208 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2209 sinfo->signal = (s8)last_rxstats->last_signal;
2210 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2213 if (!sta->pcpu_rx_stats &&
2214 !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
2216 -ewma_signal_read(&sta->rx_stats_avg.signal);
2217 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2221 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2222 * the sta->rx_stats struct, so the check here is fine with and without
2225 if (last_rxstats->chains &&
2226 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2227 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2228 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
2229 if (!sta->pcpu_rx_stats)
2230 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2232 sinfo->chains = last_rxstats->chains;
2234 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2235 sinfo->chain_signal[i] =
2236 last_rxstats->chain_signal_last[i];
2237 sinfo->chain_signal_avg[i] =
2238 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2242 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2243 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2245 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2248 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2249 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2250 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2253 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2254 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2255 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2257 sta_set_tidstats(sta, tidstats, i);
2260 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2261 #ifdef CONFIG_MAC80211_MESH
2262 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2263 BIT(NL80211_STA_INFO_PLID) |
2264 BIT(NL80211_STA_INFO_PLINK_STATE) |
2265 BIT(NL80211_STA_INFO_LOCAL_PM) |
2266 BIT(NL80211_STA_INFO_PEER_PM) |
2267 BIT(NL80211_STA_INFO_NONPEER_PM);
2269 sinfo->llid = sta->mesh->llid;
2270 sinfo->plid = sta->mesh->plid;
2271 sinfo->plink_state = sta->mesh->plink_state;
2272 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2273 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2274 sinfo->t_offset = sta->mesh->t_offset;
2276 sinfo->local_pm = sta->mesh->local_pm;
2277 sinfo->peer_pm = sta->mesh->peer_pm;
2278 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2282 sinfo->bss_param.flags = 0;
2283 if (sdata->vif.bss_conf.use_cts_prot)
2284 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2285 if (sdata->vif.bss_conf.use_short_preamble)
2286 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2287 if (sdata->vif.bss_conf.use_short_slot)
2288 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2289 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2290 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2292 sinfo->sta_flags.set = 0;
2293 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2294 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2295 BIT(NL80211_STA_FLAG_WME) |
2296 BIT(NL80211_STA_FLAG_MFP) |
2297 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2298 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2299 BIT(NL80211_STA_FLAG_TDLS_PEER);
2300 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2301 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2302 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2303 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2305 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2306 if (test_sta_flag(sta, WLAN_STA_MFP))
2307 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2308 if (test_sta_flag(sta, WLAN_STA_AUTH))
2309 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2310 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2311 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2312 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2313 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2315 thr = sta_get_expected_throughput(sta);
2318 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2319 sinfo->expected_throughput = thr;
2323 u32 sta_get_expected_throughput(struct sta_info *sta)
2325 struct ieee80211_sub_if_data *sdata = sta->sdata;
2326 struct ieee80211_local *local = sdata->local;
2327 struct rate_control_ref *ref = NULL;
2330 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2331 ref = local->rate_ctrl;
2333 /* check if the driver has a SW RC implementation */
2334 if (ref && ref->ops->get_expected_throughput)
2335 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2337 thr = drv_get_expected_throughput(local, sta);
2342 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2344 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2346 if (!sta->status_stats.last_ack ||
2347 time_after(stats->last_rx, sta->status_stats.last_ack))
2348 return stats->last_rx;
2349 return sta->status_stats.last_ack;
2352 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2354 if (!sta->sdata->local->ops->wake_tx_queue)
2357 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2358 sta->cparams.target = MS2TIME(50);
2359 sta->cparams.interval = MS2TIME(300);
2360 sta->cparams.ecn = false;
2362 sta->cparams.target = MS2TIME(20);
2363 sta->cparams.interval = MS2TIME(100);
2364 sta->cparams.ecn = true;
2368 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2371 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2373 sta_update_codel_params(sta, thr);