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-2021 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 sta->mesh->plink_sta = sta;
352 spin_lock_init(&sta->mesh->plink_lock);
353 if (ieee80211_vif_is_mesh(&sdata->vif) &&
354 !sdata->u.mesh.user_mpm)
355 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
357 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
361 memcpy(sta->addr, addr, ETH_ALEN);
362 memcpy(sta->sta.addr, addr, ETH_ALEN);
363 sta->sta.max_rx_aggregation_subframes =
364 local->hw.max_rx_aggregation_subframes;
368 sta->rx_stats.last_rx = jiffies;
370 u64_stats_init(&sta->rx_stats.syncp);
372 ieee80211_init_frag_cache(&sta->frags);
374 sta->sta_state = IEEE80211_STA_NONE;
376 /* Mark TID as unreserved */
377 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
379 sta->last_connected = ktime_get_seconds();
380 ewma_signal_init(&sta->rx_stats_avg.signal);
381 ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
382 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
383 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
385 if (local->ops->wake_tx_queue) {
387 int size = sizeof(struct txq_info) +
388 ALIGN(hw->txq_data_size, sizeof(void *));
390 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
394 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
395 struct txq_info *txq = txq_data + i * size;
397 ieee80211_txq_init(sdata, sta, txq, i);
401 if (sta_prepare_rate_control(local, sta, gfp))
404 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
405 skb_queue_head_init(&sta->ps_tx_buf[i]);
406 skb_queue_head_init(&sta->tx_filtered[i]);
409 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
410 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
412 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
413 if (sdata->vif.type == NL80211_IFTYPE_AP ||
414 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
415 struct ieee80211_supported_band *sband;
418 sband = ieee80211_get_sband(sdata);
422 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
423 IEEE80211_HT_CAP_SM_PS_SHIFT;
425 * Assume that hostapd advertises our caps in the beacon and
426 * this is the known_smps_mode for a station that just assciated
429 case WLAN_HT_SMPS_CONTROL_DISABLED:
430 sta->known_smps_mode = IEEE80211_SMPS_OFF;
432 case WLAN_HT_SMPS_CONTROL_STATIC:
433 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
435 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
436 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
443 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
445 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
446 sta->cparams.target = MS2TIME(20);
447 sta->cparams.interval = MS2TIME(100);
448 sta->cparams.ecn = true;
450 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
456 kfree(to_txq_info(sta->sta.txq[0]));
458 free_percpu(sta->pcpu_rx_stats);
459 #ifdef CONFIG_MAC80211_MESH
466 static int sta_info_insert_check(struct sta_info *sta)
468 struct ieee80211_sub_if_data *sdata = sta->sdata;
471 * Can't be a WARN_ON because it can be triggered through a race:
472 * something inserts a STA (on one CPU) without holding the RTNL
473 * and another CPU turns off the net device.
475 if (unlikely(!ieee80211_sdata_running(sdata)))
478 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
479 is_multicast_ether_addr(sta->sta.addr)))
482 /* The RCU read lock is required by rhashtable due to
483 * asynchronous resize/rehash. We also require the mutex
487 lockdep_assert_held(&sdata->local->sta_mtx);
488 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
489 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
498 static int sta_info_insert_drv_state(struct ieee80211_local *local,
499 struct ieee80211_sub_if_data *sdata,
500 struct sta_info *sta)
502 enum ieee80211_sta_state state;
505 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
506 err = drv_sta_state(local, sdata, sta, state, state + 1);
513 * Drivers using legacy sta_add/sta_remove callbacks only
514 * get uploaded set to true after sta_add is called.
516 if (!local->ops->sta_add)
517 sta->uploaded = true;
521 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
523 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
524 sta->sta.addr, state + 1, err);
528 /* unwind on error */
529 for (; state > IEEE80211_STA_NOTEXIST; state--)
530 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
536 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
538 struct ieee80211_local *local = sdata->local;
539 bool allow_p2p_go_ps = sdata->vif.p2p;
540 struct sta_info *sta;
543 list_for_each_entry_rcu(sta, &local->sta_list, list) {
544 if (sdata != sta->sdata ||
545 !test_sta_flag(sta, WLAN_STA_ASSOC))
547 if (!sta->sta.support_p2p_ps) {
548 allow_p2p_go_ps = false;
554 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
555 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
556 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
561 * should be called with sta_mtx locked
562 * this function replaces the mutex lock
565 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
567 struct ieee80211_local *local = sta->local;
568 struct ieee80211_sub_if_data *sdata = sta->sdata;
569 struct station_info *sinfo = NULL;
572 lockdep_assert_held(&local->sta_mtx);
574 /* check if STA exists already */
575 if (sta_info_get_bss(sdata, sta->sta.addr)) {
580 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
587 local->sta_generation++;
590 /* simplify things and don't accept BA sessions yet */
591 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
593 /* make the station visible */
594 err = sta_info_hash_add(local, sta);
598 list_add_tail_rcu(&sta->list, &local->sta_list);
601 err = sta_info_insert_drv_state(local, sdata, sta);
605 set_sta_flag(sta, WLAN_STA_INSERTED);
607 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
608 ieee80211_recalc_min_chandef(sta->sdata);
609 if (!sta->sta.support_p2p_ps)
610 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
613 /* accept BA sessions now */
614 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
616 ieee80211_sta_debugfs_add(sta);
617 rate_control_add_sta_debugfs(sta);
619 sinfo->generation = local->sta_generation;
620 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
623 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
625 /* move reference to rcu-protected */
627 mutex_unlock(&local->sta_mtx);
629 if (ieee80211_vif_is_mesh(&sdata->vif))
630 mesh_accept_plinks_update(sdata);
634 sta_info_hash_del(local, sta);
635 list_del_rcu(&sta->list);
639 cleanup_single_sta(sta);
641 mutex_unlock(&local->sta_mtx);
647 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
649 struct ieee80211_local *local = sta->local;
654 mutex_lock(&local->sta_mtx);
656 err = sta_info_insert_check(sta);
658 sta_info_free(local, sta);
659 mutex_unlock(&local->sta_mtx);
664 return sta_info_insert_finish(sta);
667 int sta_info_insert(struct sta_info *sta)
669 int err = sta_info_insert_rcu(sta);
676 static inline void __bss_tim_set(u8 *tim, u16 id)
679 * This format has been mandated by the IEEE specifications,
680 * so this line may not be changed to use the __set_bit() format.
682 tim[id / 8] |= (1 << (id % 8));
685 static inline void __bss_tim_clear(u8 *tim, u16 id)
688 * This format has been mandated by the IEEE specifications,
689 * so this line may not be changed to use the __clear_bit() format.
691 tim[id / 8] &= ~(1 << (id % 8));
694 static inline bool __bss_tim_get(u8 *tim, u16 id)
697 * This format has been mandated by the IEEE specifications,
698 * so this line may not be changed to use the test_bit() format.
700 return tim[id / 8] & (1 << (id % 8));
703 static unsigned long ieee80211_tids_for_ac(int ac)
705 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
707 case IEEE80211_AC_VO:
708 return BIT(6) | BIT(7);
709 case IEEE80211_AC_VI:
710 return BIT(4) | BIT(5);
711 case IEEE80211_AC_BE:
712 return BIT(0) | BIT(3);
713 case IEEE80211_AC_BK:
714 return BIT(1) | BIT(2);
721 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
723 struct ieee80211_local *local = sta->local;
725 bool indicate_tim = false;
726 u8 ignore_for_tim = sta->sta.uapsd_queues;
728 u16 id = sta->sta.aid;
730 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
731 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
732 if (WARN_ON_ONCE(!sta->sdata->bss))
735 ps = &sta->sdata->bss->ps;
736 #ifdef CONFIG_MAC80211_MESH
737 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
738 ps = &sta->sdata->u.mesh.ps;
744 /* No need to do anything if the driver does all */
745 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
752 * If all ACs are delivery-enabled then we should build
753 * the TIM bit for all ACs anyway; if only some are then
754 * we ignore those and build the TIM bit using only the
757 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
761 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
763 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
766 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
769 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
770 !skb_queue_empty(&sta->ps_tx_buf[ac]);
774 tids = ieee80211_tids_for_ac(ac);
777 sta->driver_buffered_tids & tids;
779 sta->txq_buffered_tids & tids;
783 spin_lock_bh(&local->tim_lock);
785 if (indicate_tim == __bss_tim_get(ps->tim, id))
789 __bss_tim_set(ps->tim, id);
791 __bss_tim_clear(ps->tim, id);
793 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
794 local->tim_in_locked_section = true;
795 drv_set_tim(local, &sta->sta, indicate_tim);
796 local->tim_in_locked_section = false;
800 spin_unlock_bh(&local->tim_lock);
803 void sta_info_recalc_tim(struct sta_info *sta)
805 __sta_info_recalc_tim(sta, false);
808 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
810 struct ieee80211_tx_info *info;
816 info = IEEE80211_SKB_CB(skb);
818 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
819 timeout = (sta->listen_interval *
820 sta->sdata->vif.bss_conf.beacon_int *
822 if (timeout < STA_TX_BUFFER_EXPIRE)
823 timeout = STA_TX_BUFFER_EXPIRE;
824 return time_after(jiffies, info->control.jiffies + timeout);
828 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
829 struct sta_info *sta, int ac)
835 * First check for frames that should expire on the filtered
836 * queue. Frames here were rejected by the driver and are on
837 * a separate queue to avoid reordering with normal PS-buffered
838 * frames. They also aren't accounted for right now in the
839 * total_ps_buffered counter.
842 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
843 skb = skb_peek(&sta->tx_filtered[ac]);
844 if (sta_info_buffer_expired(sta, skb))
845 skb = __skb_dequeue(&sta->tx_filtered[ac]);
848 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
851 * Frames are queued in order, so if this one
852 * hasn't expired yet we can stop testing. If
853 * we actually reached the end of the queue we
854 * also need to stop, of course.
858 ieee80211_free_txskb(&local->hw, skb);
862 * Now also check the normal PS-buffered queue, this will
863 * only find something if the filtered queue was emptied
864 * since the filtered frames are all before the normal PS
868 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
869 skb = skb_peek(&sta->ps_tx_buf[ac]);
870 if (sta_info_buffer_expired(sta, skb))
871 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
874 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
877 * frames are queued in order, so if this one
878 * hasn't expired yet (or we reached the end of
879 * the queue) we can stop testing
884 local->total_ps_buffered--;
885 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
887 ieee80211_free_txskb(&local->hw, skb);
891 * Finally, recalculate the TIM bit for this station -- it might
892 * now be clear because the station was too slow to retrieve its
895 sta_info_recalc_tim(sta);
898 * Return whether there are any frames still buffered, this is
899 * used to check whether the cleanup timer still needs to run,
900 * if there are no frames we don't need to rearm the timer.
902 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
903 skb_queue_empty(&sta->tx_filtered[ac]));
906 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
907 struct sta_info *sta)
909 bool have_buffered = false;
912 /* This is only necessary for stations on BSS/MBSS interfaces */
913 if (!sta->sdata->bss &&
914 !ieee80211_vif_is_mesh(&sta->sdata->vif))
917 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
919 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
921 return have_buffered;
924 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
926 struct ieee80211_local *local;
927 struct ieee80211_sub_if_data *sdata;
938 lockdep_assert_held(&local->sta_mtx);
941 * Before removing the station from the driver and
942 * rate control, it might still start new aggregation
943 * sessions -- block that to make sure the tear-down
944 * will be sufficient.
946 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
947 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
950 * Before removing the station from the driver there might be pending
951 * rx frames on RSS queues sent prior to the disassociation - wait for
952 * all such frames to be processed.
954 drv_sync_rx_queues(local, sta);
956 ret = sta_info_hash_del(local, sta);
961 * for TDLS peers, make sure to return to the base channel before
964 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
965 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
966 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
969 list_del_rcu(&sta->list);
972 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
974 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
975 rcu_access_pointer(sdata->u.vlan.sta) == sta)
976 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
981 static void __sta_info_destroy_part2(struct sta_info *sta)
983 struct ieee80211_local *local = sta->local;
984 struct ieee80211_sub_if_data *sdata = sta->sdata;
985 struct station_info *sinfo;
989 * NOTE: This assumes at least synchronize_net() was done
990 * after _part1 and before _part2!
994 lockdep_assert_held(&local->sta_mtx);
996 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
997 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1001 /* now keys can no longer be reached */
1002 ieee80211_free_sta_keys(local, sta);
1004 /* disable TIM bit - last chance to tell driver */
1005 __sta_info_recalc_tim(sta, true);
1010 local->sta_generation++;
1012 while (sta->sta_state > IEEE80211_STA_NONE) {
1013 ret = sta_info_move_state(sta, sta->sta_state - 1);
1020 if (sta->uploaded) {
1021 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1022 IEEE80211_STA_NOTEXIST);
1023 WARN_ON_ONCE(ret != 0);
1026 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1028 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1030 sta_set_sinfo(sta, sinfo, true);
1031 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1034 rate_control_remove_sta_debugfs(sta);
1035 ieee80211_sta_debugfs_remove(sta);
1037 ieee80211_destroy_frag_cache(&sta->frags);
1039 cleanup_single_sta(sta);
1042 int __must_check __sta_info_destroy(struct sta_info *sta)
1044 int err = __sta_info_destroy_part1(sta);
1051 __sta_info_destroy_part2(sta);
1056 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1058 struct sta_info *sta;
1061 mutex_lock(&sdata->local->sta_mtx);
1062 sta = sta_info_get(sdata, addr);
1063 ret = __sta_info_destroy(sta);
1064 mutex_unlock(&sdata->local->sta_mtx);
1069 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1072 struct sta_info *sta;
1075 mutex_lock(&sdata->local->sta_mtx);
1076 sta = sta_info_get_bss(sdata, addr);
1077 ret = __sta_info_destroy(sta);
1078 mutex_unlock(&sdata->local->sta_mtx);
1083 static void sta_info_cleanup(struct timer_list *t)
1085 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1086 struct sta_info *sta;
1087 bool timer_needed = false;
1090 list_for_each_entry_rcu(sta, &local->sta_list, list)
1091 if (sta_info_cleanup_expire_buffered(local, sta))
1092 timer_needed = true;
1095 if (local->quiescing)
1101 mod_timer(&local->sta_cleanup,
1102 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1105 int sta_info_init(struct ieee80211_local *local)
1109 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1113 spin_lock_init(&local->tim_lock);
1114 mutex_init(&local->sta_mtx);
1115 INIT_LIST_HEAD(&local->sta_list);
1117 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1121 void sta_info_stop(struct ieee80211_local *local)
1123 del_timer_sync(&local->sta_cleanup);
1124 rhltable_destroy(&local->sta_hash);
1128 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1130 struct ieee80211_local *local = sdata->local;
1131 struct sta_info *sta, *tmp;
1132 LIST_HEAD(free_list);
1137 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1138 WARN_ON(vlans && !sdata->bss);
1140 mutex_lock(&local->sta_mtx);
1141 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1142 if (sdata == sta->sdata ||
1143 (vlans && sdata->bss == sta->sdata->bss)) {
1144 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1145 list_add(&sta->free_list, &free_list);
1150 if (!list_empty(&free_list)) {
1152 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1153 __sta_info_destroy_part2(sta);
1155 mutex_unlock(&local->sta_mtx);
1160 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1161 unsigned long exp_time)
1163 struct ieee80211_local *local = sdata->local;
1164 struct sta_info *sta, *tmp;
1166 mutex_lock(&local->sta_mtx);
1168 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1169 unsigned long last_active = ieee80211_sta_last_active(sta);
1171 if (sdata != sta->sdata)
1174 if (time_is_before_jiffies(last_active + exp_time)) {
1175 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1178 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1179 test_sta_flag(sta, WLAN_STA_PS_STA))
1180 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1182 WARN_ON(__sta_info_destroy(sta));
1186 mutex_unlock(&local->sta_mtx);
1189 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1191 const u8 *localaddr)
1193 struct ieee80211_local *local = hw_to_local(hw);
1194 struct rhlist_head *tmp;
1195 struct sta_info *sta;
1198 * Just return a random station if localaddr is NULL
1199 * ... first in list.
1201 for_each_sta_info(local, addr, sta, tmp) {
1203 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1212 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1214 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1217 struct sta_info *sta;
1222 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1231 EXPORT_SYMBOL(ieee80211_find_sta);
1233 /* powersave support code */
1234 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1236 struct ieee80211_sub_if_data *sdata = sta->sdata;
1237 struct ieee80211_local *local = sdata->local;
1238 struct sk_buff_head pending;
1239 int filtered = 0, buffered = 0, ac, i;
1240 unsigned long flags;
1243 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1244 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1247 if (sdata->vif.type == NL80211_IFTYPE_AP)
1248 ps = &sdata->bss->ps;
1249 else if (ieee80211_vif_is_mesh(&sdata->vif))
1250 ps = &sdata->u.mesh.ps;
1254 clear_sta_flag(sta, WLAN_STA_SP);
1256 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1257 sta->driver_buffered_tids = 0;
1258 sta->txq_buffered_tids = 0;
1260 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1261 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1263 if (sta->sta.txq[0]) {
1264 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1265 if (!txq_has_queue(sta->sta.txq[i]))
1268 drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1272 skb_queue_head_init(&pending);
1274 /* sync with ieee80211_tx_h_unicast_ps_buf */
1275 spin_lock(&sta->ps_lock);
1276 /* Send all buffered frames to the station */
1277 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1278 int count = skb_queue_len(&pending), tmp;
1280 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1281 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1282 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1283 tmp = skb_queue_len(&pending);
1284 filtered += tmp - count;
1287 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1288 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1289 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1290 tmp = skb_queue_len(&pending);
1291 buffered += tmp - count;
1294 ieee80211_add_pending_skbs(local, &pending);
1296 /* now we're no longer in the deliver code */
1297 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1299 /* The station might have polled and then woken up before we responded,
1300 * so clear these flags now to avoid them sticking around.
1302 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1303 clear_sta_flag(sta, WLAN_STA_UAPSD);
1304 spin_unlock(&sta->ps_lock);
1306 atomic_dec(&ps->num_sta_ps);
1308 /* This station just woke up and isn't aware of our SMPS state */
1309 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1310 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1311 sdata->smps_mode) &&
1312 sta->known_smps_mode != sdata->bss->req_smps &&
1313 sta_info_tx_streams(sta) != 1) {
1315 "%pM just woke up and MIMO capable - update SMPS\n",
1317 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1319 sdata->vif.bss_conf.bssid);
1322 local->total_ps_buffered -= buffered;
1324 sta_info_recalc_tim(sta);
1327 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1328 sta->sta.addr, sta->sta.aid, filtered, buffered);
1330 ieee80211_check_fast_xmit(sta);
1333 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1334 enum ieee80211_frame_release_type reason,
1335 bool call_driver, bool more_data)
1337 struct ieee80211_sub_if_data *sdata = sta->sdata;
1338 struct ieee80211_local *local = sdata->local;
1339 struct ieee80211_qos_hdr *nullfunc;
1340 struct sk_buff *skb;
1341 int size = sizeof(*nullfunc);
1343 bool qos = sta->sta.wme;
1344 struct ieee80211_tx_info *info;
1345 struct ieee80211_chanctx_conf *chanctx_conf;
1348 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1349 IEEE80211_STYPE_QOS_NULLFUNC |
1350 IEEE80211_FCTL_FROMDS);
1353 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1354 IEEE80211_STYPE_NULLFUNC |
1355 IEEE80211_FCTL_FROMDS);
1358 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1362 skb_reserve(skb, local->hw.extra_tx_headroom);
1364 nullfunc = skb_put(skb, size);
1365 nullfunc->frame_control = fc;
1366 nullfunc->duration_id = 0;
1367 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1368 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1369 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1370 nullfunc->seq_ctrl = 0;
1372 skb->priority = tid;
1373 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1375 nullfunc->qos_ctrl = cpu_to_le16(tid);
1377 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1378 nullfunc->qos_ctrl |=
1379 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1381 nullfunc->frame_control |=
1382 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1386 info = IEEE80211_SKB_CB(skb);
1389 * Tell TX path to send this frame even though the
1390 * STA may still remain is PS mode after this frame
1391 * exchange. Also set EOSP to indicate this packet
1392 * ends the poll/service period.
1394 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1395 IEEE80211_TX_STATUS_EOSP |
1396 IEEE80211_TX_CTL_REQ_TX_STATUS;
1398 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1401 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1404 skb->dev = sdata->dev;
1407 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1408 if (WARN_ON(!chanctx_conf)) {
1414 info->band = chanctx_conf->def.chan->band;
1415 ieee80211_xmit(sdata, sta, skb, 0);
1419 static int find_highest_prio_tid(unsigned long tids)
1421 /* lower 3 TIDs aren't ordered perfectly */
1423 return fls(tids) - 1;
1424 /* TID 0 is BE just like TID 3 */
1427 return fls(tids) - 1;
1430 /* Indicates if the MORE_DATA bit should be set in the last
1431 * frame obtained by ieee80211_sta_ps_get_frames.
1432 * Note that driver_release_tids is relevant only if
1433 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1436 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1437 enum ieee80211_frame_release_type reason,
1438 unsigned long driver_release_tids)
1442 /* If the driver has data on more than one TID then
1443 * certainly there's more data if we release just a
1444 * single frame now (from a single TID). This will
1445 * only happen for PS-Poll.
1447 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1448 hweight16(driver_release_tids) > 1)
1451 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1452 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1455 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1456 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1464 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1465 enum ieee80211_frame_release_type reason,
1466 struct sk_buff_head *frames,
1467 unsigned long *driver_release_tids)
1469 struct ieee80211_sub_if_data *sdata = sta->sdata;
1470 struct ieee80211_local *local = sdata->local;
1473 /* Get response frame(s) and more data bit for the last one. */
1474 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1477 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1480 tids = ieee80211_tids_for_ac(ac);
1482 /* if we already have frames from software, then we can't also
1483 * release from hardware queues
1485 if (skb_queue_empty(frames)) {
1486 *driver_release_tids |=
1487 sta->driver_buffered_tids & tids;
1488 *driver_release_tids |= sta->txq_buffered_tids & tids;
1491 if (!*driver_release_tids) {
1492 struct sk_buff *skb;
1494 while (n_frames > 0) {
1495 skb = skb_dequeue(&sta->tx_filtered[ac]);
1498 &sta->ps_tx_buf[ac]);
1500 local->total_ps_buffered--;
1505 __skb_queue_tail(frames, skb);
1509 /* If we have more frames buffered on this AC, then abort the
1510 * loop since we can't send more data from other ACs before
1511 * the buffered frames from this.
1513 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1514 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1520 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1521 int n_frames, u8 ignored_acs,
1522 enum ieee80211_frame_release_type reason)
1524 struct ieee80211_sub_if_data *sdata = sta->sdata;
1525 struct ieee80211_local *local = sdata->local;
1526 unsigned long driver_release_tids = 0;
1527 struct sk_buff_head frames;
1530 /* Service or PS-Poll period starts */
1531 set_sta_flag(sta, WLAN_STA_SP);
1533 __skb_queue_head_init(&frames);
1535 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1536 &frames, &driver_release_tids);
1538 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1540 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1541 driver_release_tids =
1542 BIT(find_highest_prio_tid(driver_release_tids));
1544 if (skb_queue_empty(&frames) && !driver_release_tids) {
1548 * For PS-Poll, this can only happen due to a race condition
1549 * when we set the TIM bit and the station notices it, but
1550 * before it can poll for the frame we expire it.
1552 * For uAPSD, this is said in the standard (11.2.1.5 h):
1553 * At each unscheduled SP for a non-AP STA, the AP shall
1554 * attempt to transmit at least one MSDU or MMPDU, but no
1555 * more than the value specified in the Max SP Length field
1556 * in the QoS Capability element from delivery-enabled ACs,
1557 * that are destined for the non-AP STA.
1559 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1562 /* This will evaluate to 1, 3, 5 or 7. */
1563 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1564 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1568 ieee80211_send_null_response(sta, tid, reason, true, false);
1569 } else if (!driver_release_tids) {
1570 struct sk_buff_head pending;
1571 struct sk_buff *skb;
1574 bool need_null = false;
1576 skb_queue_head_init(&pending);
1578 while ((skb = __skb_dequeue(&frames))) {
1579 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1580 struct ieee80211_hdr *hdr = (void *) skb->data;
1586 * Tell TX path to send this frame even though the
1587 * STA may still remain is PS mode after this frame
1590 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1591 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1594 * Use MoreData flag to indicate whether there are
1595 * more buffered frames for this STA
1597 if (more_data || !skb_queue_empty(&frames))
1598 hdr->frame_control |=
1599 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1601 hdr->frame_control &=
1602 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1604 if (ieee80211_is_data_qos(hdr->frame_control) ||
1605 ieee80211_is_qos_nullfunc(hdr->frame_control))
1606 qoshdr = ieee80211_get_qos_ctl(hdr);
1608 tids |= BIT(skb->priority);
1610 __skb_queue_tail(&pending, skb);
1612 /* end service period after last frame or add one */
1613 if (!skb_queue_empty(&frames))
1616 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1617 /* for PS-Poll, there's only one frame */
1618 info->flags |= IEEE80211_TX_STATUS_EOSP |
1619 IEEE80211_TX_CTL_REQ_TX_STATUS;
1623 /* For uAPSD, things are a bit more complicated. If the
1624 * last frame has a QoS header (i.e. is a QoS-data or
1625 * QoS-nulldata frame) then just set the EOSP bit there
1627 * If the frame doesn't have a QoS header (which means
1628 * it should be a bufferable MMPDU) then we can't set
1629 * the EOSP bit in the QoS header; add a QoS-nulldata
1630 * frame to the list to send it after the MMPDU.
1632 * Note that this code is only in the mac80211-release
1633 * code path, we assume that the driver will not buffer
1634 * anything but QoS-data frames, or if it does, will
1635 * create the QoS-nulldata frame by itself if needed.
1637 * Cf. 802.11-2012 10.2.1.10 (c).
1640 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1642 info->flags |= IEEE80211_TX_STATUS_EOSP |
1643 IEEE80211_TX_CTL_REQ_TX_STATUS;
1645 /* The standard isn't completely clear on this
1646 * as it says the more-data bit should be set
1647 * if there are more BUs. The QoS-Null frame
1648 * we're about to send isn't buffered yet, we
1649 * only create it below, but let's pretend it
1650 * was buffered just in case some clients only
1651 * expect more-data=0 when eosp=1.
1653 hdr->frame_control |=
1654 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1661 drv_allow_buffered_frames(local, sta, tids, num,
1664 ieee80211_add_pending_skbs(local, &pending);
1667 ieee80211_send_null_response(
1668 sta, find_highest_prio_tid(tids),
1669 reason, false, false);
1671 sta_info_recalc_tim(sta);
1676 * We need to release a frame that is buffered somewhere in the
1677 * driver ... it'll have to handle that.
1678 * Note that the driver also has to check the number of frames
1679 * on the TIDs we're releasing from - if there are more than
1680 * n_frames it has to set the more-data bit (if we didn't ask
1681 * it to set it anyway due to other buffered frames); if there
1682 * are fewer than n_frames it has to make sure to adjust that
1683 * to allow the service period to end properly.
1685 drv_release_buffered_frames(local, sta, driver_release_tids,
1686 n_frames, reason, more_data);
1689 * Note that we don't recalculate the TIM bit here as it would
1690 * most likely have no effect at all unless the driver told us
1691 * that the TID(s) became empty before returning here from the
1693 * Either way, however, when the driver tells us that the TID(s)
1694 * became empty or we find that a txq became empty, we'll do the
1695 * TIM recalculation.
1698 if (!sta->sta.txq[0])
1701 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1702 if (!(driver_release_tids & BIT(tid)) ||
1703 txq_has_queue(sta->sta.txq[tid]))
1706 sta_info_recalc_tim(sta);
1712 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1714 u8 ignore_for_response = sta->sta.uapsd_queues;
1717 * If all ACs are delivery-enabled then we should reply
1718 * from any of them, if only some are enabled we reply
1719 * only from the non-enabled ones.
1721 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1722 ignore_for_response = 0;
1724 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1725 IEEE80211_FRAME_RELEASE_PSPOLL);
1728 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1730 int n_frames = sta->sta.max_sp;
1731 u8 delivery_enabled = sta->sta.uapsd_queues;
1734 * If we ever grow support for TSPEC this might happen if
1735 * the TSPEC update from hostapd comes in between a trigger
1736 * frame setting WLAN_STA_UAPSD in the RX path and this
1737 * actually getting called.
1739 if (!delivery_enabled)
1742 switch (sta->sta.max_sp) {
1753 /* XXX: what is a good value? */
1758 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1759 IEEE80211_FRAME_RELEASE_UAPSD);
1762 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1763 struct ieee80211_sta *pubsta, bool block)
1765 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1767 trace_api_sta_block_awake(sta->local, pubsta, block);
1770 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1771 ieee80211_clear_fast_xmit(sta);
1775 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1778 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1779 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1780 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1781 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1782 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1783 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1784 /* must be asleep in this case */
1785 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1786 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1788 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1789 ieee80211_check_fast_xmit(sta);
1792 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1794 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1796 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1797 struct ieee80211_local *local = sta->local;
1799 trace_api_eosp(local, pubsta);
1801 clear_sta_flag(sta, WLAN_STA_SP);
1803 EXPORT_SYMBOL(ieee80211_sta_eosp);
1805 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1807 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1808 enum ieee80211_frame_release_type reason;
1811 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1813 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1814 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1817 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1819 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1821 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1822 u8 tid, bool buffered)
1824 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1826 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1829 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1832 set_bit(tid, &sta->driver_buffered_tids);
1834 clear_bit(tid, &sta->driver_buffered_tids);
1836 sta_info_recalc_tim(sta);
1838 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1840 int sta_info_move_state(struct sta_info *sta,
1841 enum ieee80211_sta_state new_state)
1845 if (sta->sta_state == new_state)
1848 /* check allowed transitions first */
1850 switch (new_state) {
1851 case IEEE80211_STA_NONE:
1852 if (sta->sta_state != IEEE80211_STA_AUTH)
1855 case IEEE80211_STA_AUTH:
1856 if (sta->sta_state != IEEE80211_STA_NONE &&
1857 sta->sta_state != IEEE80211_STA_ASSOC)
1860 case IEEE80211_STA_ASSOC:
1861 if (sta->sta_state != IEEE80211_STA_AUTH &&
1862 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1865 case IEEE80211_STA_AUTHORIZED:
1866 if (sta->sta_state != IEEE80211_STA_ASSOC)
1870 WARN(1, "invalid state %d", new_state);
1874 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1875 sta->sta.addr, new_state);
1878 * notify the driver before the actual changes so it can
1879 * fail the transition
1881 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1882 int err = drv_sta_state(sta->local, sta->sdata, sta,
1883 sta->sta_state, new_state);
1888 /* reflect the change in all state variables */
1890 switch (new_state) {
1891 case IEEE80211_STA_NONE:
1892 if (sta->sta_state == IEEE80211_STA_AUTH)
1893 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1895 case IEEE80211_STA_AUTH:
1896 if (sta->sta_state == IEEE80211_STA_NONE) {
1897 set_bit(WLAN_STA_AUTH, &sta->_flags);
1898 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1899 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1900 ieee80211_recalc_min_chandef(sta->sdata);
1901 if (!sta->sta.support_p2p_ps)
1902 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1905 case IEEE80211_STA_ASSOC:
1906 if (sta->sta_state == IEEE80211_STA_AUTH) {
1907 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1908 ieee80211_recalc_min_chandef(sta->sdata);
1909 if (!sta->sta.support_p2p_ps)
1910 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1911 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1912 ieee80211_vif_dec_num_mcast(sta->sdata);
1913 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1914 ieee80211_clear_fast_xmit(sta);
1915 ieee80211_clear_fast_rx(sta);
1918 case IEEE80211_STA_AUTHORIZED:
1919 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1920 ieee80211_vif_inc_num_mcast(sta->sdata);
1921 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1922 ieee80211_check_fast_xmit(sta);
1923 ieee80211_check_fast_rx(sta);
1925 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1926 sta->sdata->vif.type == NL80211_IFTYPE_AP)
1927 cfg80211_send_layer2_update(sta->sdata->dev,
1934 sta->sta_state = new_state;
1939 u8 sta_info_tx_streams(struct sta_info *sta)
1941 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1944 if (!sta->sta.ht_cap.ht_supported)
1947 if (sta->sta.vht_cap.vht_supported) {
1950 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1952 for (i = 7; i >= 0; i--)
1953 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1954 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1958 if (ht_cap->mcs.rx_mask[3])
1960 else if (ht_cap->mcs.rx_mask[2])
1962 else if (ht_cap->mcs.rx_mask[1])
1967 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1970 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1971 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1974 static struct ieee80211_sta_rx_stats *
1975 sta_get_last_rx_stats(struct sta_info *sta)
1977 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1978 struct ieee80211_local *local = sta->local;
1981 if (!ieee80211_hw_check(&local->hw, USES_RSS))
1984 for_each_possible_cpu(cpu) {
1985 struct ieee80211_sta_rx_stats *cpustats;
1987 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1989 if (time_after(cpustats->last_rx, stats->last_rx))
1996 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
1997 struct rate_info *rinfo)
1999 rinfo->bw = STA_STATS_GET(BW, rate);
2001 switch (STA_STATS_GET(TYPE, rate)) {
2002 case STA_STATS_RATE_TYPE_VHT:
2003 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2004 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2005 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2006 if (STA_STATS_GET(SGI, rate))
2007 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2009 case STA_STATS_RATE_TYPE_HT:
2010 rinfo->flags = RATE_INFO_FLAGS_MCS;
2011 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2012 if (STA_STATS_GET(SGI, rate))
2013 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2015 case STA_STATS_RATE_TYPE_LEGACY: {
2016 struct ieee80211_supported_band *sband;
2019 int band = STA_STATS_GET(LEGACY_BAND, rate);
2020 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);
2037 case STA_STATS_RATE_TYPE_HE:
2038 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2039 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2040 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2041 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2042 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2043 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2048 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2050 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2052 if (rate == STA_STATS_RATE_INVALID)
2055 sta_stats_decode_rate(sta->local, rate, rinfo);
2059 static void sta_set_tidstats(struct sta_info *sta,
2060 struct cfg80211_tid_stats *tidstats,
2063 struct ieee80211_local *local = sta->local;
2065 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2069 start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2070 tidstats->rx_msdu = sta->rx_stats.msdu[tid];
2071 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2073 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2076 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2077 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2078 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2081 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2082 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2083 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2084 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2087 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2088 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2089 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2090 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2093 if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2094 spin_lock_bh(&local->fq.lock);
2097 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2098 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2099 to_txq_info(sta->sta.txq[tid]));
2102 spin_unlock_bh(&local->fq.lock);
2106 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2112 start = u64_stats_fetch_begin(&rxstats->syncp);
2113 value = rxstats->bytes;
2114 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2119 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2122 struct ieee80211_sub_if_data *sdata = sta->sdata;
2123 struct ieee80211_local *local = sdata->local;
2126 struct ieee80211_sta_rx_stats *last_rxstats;
2128 last_rxstats = sta_get_last_rx_stats(sta);
2130 sinfo->generation = sdata->local->sta_generation;
2132 /* do before driver, so beacon filtering drivers have a
2133 * chance to e.g. just add the number of filtered beacons
2134 * (or just modify the value entirely, of course)
2136 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2137 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2139 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2141 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2142 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2143 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2144 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2145 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2147 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2148 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2149 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2152 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2153 sinfo->inactive_time =
2154 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2156 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2157 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2158 sinfo->tx_bytes = 0;
2159 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2160 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2161 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2164 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2165 sinfo->tx_packets = 0;
2166 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2167 sinfo->tx_packets += sta->tx_stats.packets[ac];
2168 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2171 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2172 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2173 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
2175 if (sta->pcpu_rx_stats) {
2176 for_each_possible_cpu(cpu) {
2177 struct ieee80211_sta_rx_stats *cpurxs;
2179 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2180 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2184 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2187 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2188 sinfo->rx_packets = sta->rx_stats.packets;
2189 if (sta->pcpu_rx_stats) {
2190 for_each_possible_cpu(cpu) {
2191 struct ieee80211_sta_rx_stats *cpurxs;
2193 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2194 sinfo->rx_packets += cpurxs->packets;
2197 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2200 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2201 sinfo->tx_retries = sta->status_stats.retry_count;
2202 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2205 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2206 sinfo->tx_failed = sta->status_stats.retry_failed;
2207 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2210 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2211 if (sta->pcpu_rx_stats) {
2212 for_each_possible_cpu(cpu) {
2213 struct ieee80211_sta_rx_stats *cpurxs;
2215 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2216 sinfo->rx_dropped_misc += cpurxs->dropped;
2220 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2221 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2222 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2223 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2224 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2227 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2228 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2229 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2230 sinfo->signal = (s8)last_rxstats->last_signal;
2231 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2234 if (!sta->pcpu_rx_stats &&
2235 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2237 -ewma_signal_read(&sta->rx_stats_avg.signal);
2238 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2242 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2243 * the sta->rx_stats struct, so the check here is fine with and without
2246 if (last_rxstats->chains &&
2247 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2248 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2249 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2250 if (!sta->pcpu_rx_stats)
2251 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2253 sinfo->chains = last_rxstats->chains;
2255 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2256 sinfo->chain_signal[i] =
2257 last_rxstats->chain_signal_last[i];
2258 sinfo->chain_signal_avg[i] =
2259 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2263 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2264 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2266 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2269 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2270 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2271 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2274 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2275 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2276 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2278 sta_set_tidstats(sta, tidstats, i);
2282 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2283 #ifdef CONFIG_MAC80211_MESH
2284 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2285 BIT_ULL(NL80211_STA_INFO_PLID) |
2286 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2287 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2288 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2289 BIT_ULL(NL80211_STA_INFO_NONPEER_PM);
2291 sinfo->llid = sta->mesh->llid;
2292 sinfo->plid = sta->mesh->plid;
2293 sinfo->plink_state = sta->mesh->plink_state;
2294 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2295 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2296 sinfo->t_offset = sta->mesh->t_offset;
2298 sinfo->local_pm = sta->mesh->local_pm;
2299 sinfo->peer_pm = sta->mesh->peer_pm;
2300 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2304 sinfo->bss_param.flags = 0;
2305 if (sdata->vif.bss_conf.use_cts_prot)
2306 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2307 if (sdata->vif.bss_conf.use_short_preamble)
2308 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2309 if (sdata->vif.bss_conf.use_short_slot)
2310 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2311 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2312 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2314 sinfo->sta_flags.set = 0;
2315 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2316 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2317 BIT(NL80211_STA_FLAG_WME) |
2318 BIT(NL80211_STA_FLAG_MFP) |
2319 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2320 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2321 BIT(NL80211_STA_FLAG_TDLS_PEER);
2322 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2323 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2324 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2325 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2327 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2328 if (test_sta_flag(sta, WLAN_STA_MFP))
2329 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2330 if (test_sta_flag(sta, WLAN_STA_AUTH))
2331 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2332 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2333 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2334 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2335 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2337 thr = sta_get_expected_throughput(sta);
2340 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2341 sinfo->expected_throughput = thr;
2344 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2345 sta->status_stats.ack_signal_filled) {
2346 sinfo->ack_signal = sta->status_stats.last_ack_signal;
2347 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2350 if (ieee80211_hw_check(&sta->local->hw, REPORTS_TX_ACK_STATUS) &&
2351 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG))) {
2352 sinfo->avg_ack_signal =
2353 -(s8)ewma_avg_signal_read(
2354 &sta->status_stats.avg_ack_signal);
2356 BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG);
2360 u32 sta_get_expected_throughput(struct sta_info *sta)
2362 struct ieee80211_sub_if_data *sdata = sta->sdata;
2363 struct ieee80211_local *local = sdata->local;
2364 struct rate_control_ref *ref = NULL;
2367 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2368 ref = local->rate_ctrl;
2370 /* check if the driver has a SW RC implementation */
2371 if (ref && ref->ops->get_expected_throughput)
2372 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2374 thr = drv_get_expected_throughput(local, sta);
2379 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2381 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2383 if (!sta->status_stats.last_ack ||
2384 time_after(stats->last_rx, sta->status_stats.last_ack))
2385 return stats->last_rx;
2386 return sta->status_stats.last_ack;
2389 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2391 if (!sta->sdata->local->ops->wake_tx_queue)
2394 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2395 sta->cparams.target = MS2TIME(50);
2396 sta->cparams.interval = MS2TIME(300);
2397 sta->cparams.ecn = false;
2399 sta->cparams.target = MS2TIME(20);
2400 sta->cparams.interval = MS2TIME(100);
2401 sta->cparams.ecn = true;
2405 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2408 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2410 sta_update_codel_params(sta, thr);