1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
7 * Copyright (C) 2018-2021 Intel Corporation
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/timer.h>
19 #include <linux/rtnetlink.h>
21 #include <net/codel.h>
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "driver-ops.h"
27 #include "debugfs_sta.h"
32 * DOC: STA information lifetime rules
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
38 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it, in
44 * particular, it may not start any mesh peer link management or add
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
50 * Station entries are added by mac80211 when you establish a link with a
51 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
53 * receive an association response from the AP. For IBSS this occurs when
54 * get to know about a peer on the same IBSS. For WDS we add the sta for
55 * the peer immediately upon device open. When using AP mode we add stations
56 * for each respective station upon request from userspace through nl80211.
58 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
61 * There is no concept of ownership on a STA entry, each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
67 static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
69 .automatic_shrinking = true,
70 .head_offset = offsetof(struct sta_info, hash_node),
71 .key_offset = offsetof(struct sta_info, addr),
73 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
76 /* Caller must hold local->sta_mtx */
77 static int sta_info_hash_del(struct ieee80211_local *local,
80 return rhltable_remove(&local->sta_hash, &sta->hash_node,
84 static void __cleanup_single_sta(struct sta_info *sta)
87 struct tid_ampdu_tx *tid_tx;
88 struct ieee80211_sub_if_data *sdata = sta->sdata;
89 struct ieee80211_local *local = sdata->local;
92 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
93 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
94 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
95 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
98 else if (ieee80211_vif_is_mesh(&sdata->vif))
99 ps = &sdata->u.mesh.ps;
103 clear_sta_flag(sta, WLAN_STA_PS_STA);
104 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
105 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
107 atomic_dec(&ps->num_sta_ps);
110 if (sta->sta.txq[0]) {
111 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112 struct txq_info *txqi;
114 if (!sta->sta.txq[i])
117 txqi = to_txq_info(sta->sta.txq[i]);
119 ieee80211_txq_purge(local, txqi);
123 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
125 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
129 if (ieee80211_vif_is_mesh(&sdata->vif))
130 mesh_sta_cleanup(sta);
132 cancel_work_sync(&sta->drv_deliver_wk);
135 * Destroy aggregation state here. It would be nice to wait for the
136 * driver to finish aggregation stop and then clean up, but for now
137 * drivers have to handle aggregation stop being requested, followed
138 * directly by station destruction.
140 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141 kfree(sta->ampdu_mlme.tid_start_tx[i]);
142 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
145 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
150 static void cleanup_single_sta(struct sta_info *sta)
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
153 struct ieee80211_local *local = sdata->local;
155 __cleanup_single_sta(sta);
156 sta_info_free(local, sta);
159 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
162 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
165 /* protected by RCU */
166 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
169 struct ieee80211_local *local = sdata->local;
170 struct rhlist_head *tmp;
171 struct sta_info *sta;
174 for_each_sta_info(local, addr, sta, tmp) {
175 if (sta->sdata == sdata) {
177 /* this is safe as the caller must already hold
178 * another rcu read section or the mutex
188 * Get sta info either from the specified interface
189 * or from one of its vlans
191 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
194 struct ieee80211_local *local = sdata->local;
195 struct rhlist_head *tmp;
196 struct sta_info *sta;
199 for_each_sta_info(local, addr, sta, tmp) {
200 if (sta->sdata == sdata ||
201 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
203 /* this is safe as the caller must already hold
204 * another rcu read section or the mutex
213 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
214 const u8 *sta_addr, const u8 *vif_addr)
216 struct rhlist_head *tmp;
217 struct sta_info *sta;
219 for_each_sta_info(local, sta_addr, sta, tmp) {
220 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
227 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
230 struct ieee80211_local *local = sdata->local;
231 struct sta_info *sta;
234 list_for_each_entry_rcu(sta, &local->sta_list, list,
235 lockdep_is_held(&local->sta_mtx)) {
236 if (sdata != sta->sdata)
249 * sta_info_free - free STA
251 * @local: pointer to the global information
252 * @sta: STA info to free
254 * This function must undo everything done by sta_info_alloc()
255 * that may happen before sta_info_insert(). It may only be
256 * called when sta_info_insert() has not been attempted (and
257 * if that fails, the station is freed anyway.)
259 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
262 * If we had used sta_info_pre_move_state() then we might not
263 * have gone through the state transitions down again, so do
264 * it here now (and warn if it's inserted).
266 * This will clear state such as fast TX/RX that may have been
267 * allocated during state transitions.
269 while (sta->sta_state > IEEE80211_STA_NONE) {
272 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
274 ret = sta_info_move_state(sta, sta->sta_state - 1);
275 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
280 rate_control_free_sta(sta);
282 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
285 kfree(to_txq_info(sta->sta.txq[0]));
286 kfree(rcu_dereference_raw(sta->sta.rates));
287 #ifdef CONFIG_MAC80211_MESH
290 free_percpu(sta->deflink.pcpu_rx_stats);
294 /* Caller must hold local->sta_mtx */
295 static int sta_info_hash_add(struct ieee80211_local *local,
296 struct sta_info *sta)
298 return rhltable_insert(&local->sta_hash, &sta->hash_node,
302 static void sta_deliver_ps_frames(struct work_struct *wk)
304 struct sta_info *sta;
306 sta = container_of(wk, struct sta_info, drv_deliver_wk);
312 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
313 ieee80211_sta_ps_deliver_wakeup(sta);
314 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
315 ieee80211_sta_ps_deliver_poll_response(sta);
316 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
317 ieee80211_sta_ps_deliver_uapsd(sta);
321 static int sta_prepare_rate_control(struct ieee80211_local *local,
322 struct sta_info *sta, gfp_t gfp)
324 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
327 sta->rate_ctrl = local->rate_ctrl;
328 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
330 if (!sta->rate_ctrl_priv)
336 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
337 const u8 *addr, gfp_t gfp)
339 struct ieee80211_local *local = sdata->local;
340 struct ieee80211_hw *hw = &local->hw;
341 struct sta_info *sta;
344 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
348 if (ieee80211_hw_check(hw, USES_RSS)) {
349 sta->deflink.pcpu_rx_stats =
350 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
351 if (!sta->deflink.pcpu_rx_stats)
355 spin_lock_init(&sta->lock);
356 spin_lock_init(&sta->ps_lock);
357 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
358 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
359 mutex_init(&sta->ampdu_mlme.mtx);
360 #ifdef CONFIG_MAC80211_MESH
361 if (ieee80211_vif_is_mesh(&sdata->vif)) {
362 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
365 sta->mesh->plink_sta = sta;
366 spin_lock_init(&sta->mesh->plink_lock);
367 if (!sdata->u.mesh.user_mpm)
368 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
370 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
374 memcpy(sta->addr, addr, ETH_ALEN);
375 memcpy(sta->sta.addr, addr, ETH_ALEN);
376 sta->sta.max_rx_aggregation_subframes =
377 local->hw.max_rx_aggregation_subframes;
379 /* TODO link specific alloc and assignments for MLO Link STA */
381 /* For non MLO STA, link info can be accessed either via deflink
384 sta->link[0] = &sta->deflink;
385 sta->sta.link[0] = &sta->sta.deflink;
387 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
388 * The Tx path starts to use a key as soon as the key slot ptk_idx
389 * references to is not NULL. To not use the initial Rx-only key
390 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
391 * which always will refer to a NULL key.
393 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
394 sta->ptk_idx = INVALID_PTK_KEYIDX;
398 sta->deflink.rx_stats.last_rx = jiffies;
400 u64_stats_init(&sta->deflink.rx_stats.syncp);
402 ieee80211_init_frag_cache(&sta->frags);
404 sta->sta_state = IEEE80211_STA_NONE;
406 /* Mark TID as unreserved */
407 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
409 sta->last_connected = ktime_get_seconds();
410 ewma_signal_init(&sta->deflink.rx_stats_avg.signal);
411 ewma_avg_signal_init(&sta->deflink.status_stats.avg_ack_signal);
412 for (i = 0; i < ARRAY_SIZE(sta->deflink.rx_stats_avg.chain_signal); i++)
413 ewma_signal_init(&sta->deflink.rx_stats_avg.chain_signal[i]);
415 if (local->ops->wake_tx_queue) {
417 int size = sizeof(struct txq_info) +
418 ALIGN(hw->txq_data_size, sizeof(void *));
420 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
424 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
425 struct txq_info *txq = txq_data + i * size;
427 /* might not do anything for the bufferable MMPDU TXQ */
428 ieee80211_txq_init(sdata, sta, txq, i);
432 if (sta_prepare_rate_control(local, sta, gfp))
436 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
437 skb_queue_head_init(&sta->ps_tx_buf[i]);
438 skb_queue_head_init(&sta->tx_filtered[i]);
439 init_airtime_info(&sta->airtime[i], &local->airtime[i]);
442 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
443 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
445 for (i = 0; i < NUM_NL80211_BANDS; i++) {
449 if (!hw->wiphy->bands[i])
453 case NL80211_BAND_2GHZ:
454 case NL80211_BAND_LC:
456 * We use both here, even if we cannot really know for
457 * sure the station will support both, but the only use
458 * for this is when we don't know anything yet and send
459 * management frames, and then we'll pick the lowest
460 * possible rate anyway.
461 * If we don't include _G here, we cannot find a rate
462 * in P2P, and thus trigger the WARN_ONCE() in rate.c
464 mandatory = IEEE80211_RATE_MANDATORY_B |
465 IEEE80211_RATE_MANDATORY_G;
467 case NL80211_BAND_5GHZ:
468 mandatory = IEEE80211_RATE_MANDATORY_A;
470 case NL80211_BAND_60GHZ:
476 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
477 struct ieee80211_rate *rate;
479 rate = &hw->wiphy->bands[i]->bitrates[r];
481 if (!(rate->flags & mandatory))
483 sta->sta.deflink.supp_rates[i] |= BIT(r);
487 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
488 if (sdata->vif.type == NL80211_IFTYPE_AP ||
489 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
490 struct ieee80211_supported_band *sband;
493 sband = ieee80211_get_sband(sdata);
497 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
498 IEEE80211_HT_CAP_SM_PS_SHIFT;
500 * Assume that hostapd advertises our caps in the beacon and
501 * this is the known_smps_mode for a station that just assciated
504 case WLAN_HT_SMPS_CONTROL_DISABLED:
505 sta->known_smps_mode = IEEE80211_SMPS_OFF;
507 case WLAN_HT_SMPS_CONTROL_STATIC:
508 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
510 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
511 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
518 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
520 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
521 sta->cparams.target = MS2TIME(20);
522 sta->cparams.interval = MS2TIME(100);
523 sta->cparams.ecn = true;
524 sta->cparams.ce_threshold_selector = 0;
525 sta->cparams.ce_threshold_mask = 0;
527 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
533 kfree(to_txq_info(sta->sta.txq[0]));
535 free_percpu(sta->deflink.pcpu_rx_stats);
536 #ifdef CONFIG_MAC80211_MESH
543 static int sta_info_insert_check(struct sta_info *sta)
545 struct ieee80211_sub_if_data *sdata = sta->sdata;
548 * Can't be a WARN_ON because it can be triggered through a race:
549 * something inserts a STA (on one CPU) without holding the RTNL
550 * and another CPU turns off the net device.
552 if (unlikely(!ieee80211_sdata_running(sdata)))
555 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
556 !is_valid_ether_addr(sta->sta.addr)))
559 /* The RCU read lock is required by rhashtable due to
560 * asynchronous resize/rehash. We also require the mutex
564 lockdep_assert_held(&sdata->local->sta_mtx);
565 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
566 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
575 static int sta_info_insert_drv_state(struct ieee80211_local *local,
576 struct ieee80211_sub_if_data *sdata,
577 struct sta_info *sta)
579 enum ieee80211_sta_state state;
582 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
583 err = drv_sta_state(local, sdata, sta, state, state + 1);
590 * Drivers using legacy sta_add/sta_remove callbacks only
591 * get uploaded set to true after sta_add is called.
593 if (!local->ops->sta_add)
594 sta->uploaded = true;
598 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
600 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
601 sta->sta.addr, state + 1, err);
605 /* unwind on error */
606 for (; state > IEEE80211_STA_NOTEXIST; state--)
607 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
613 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
615 struct ieee80211_local *local = sdata->local;
616 bool allow_p2p_go_ps = sdata->vif.p2p;
617 struct sta_info *sta;
620 list_for_each_entry_rcu(sta, &local->sta_list, list) {
621 if (sdata != sta->sdata ||
622 !test_sta_flag(sta, WLAN_STA_ASSOC))
624 if (!sta->sta.support_p2p_ps) {
625 allow_p2p_go_ps = false;
631 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
632 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
633 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
638 * should be called with sta_mtx locked
639 * this function replaces the mutex lock
642 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
644 struct ieee80211_local *local = sta->local;
645 struct ieee80211_sub_if_data *sdata = sta->sdata;
646 struct station_info *sinfo = NULL;
649 lockdep_assert_held(&local->sta_mtx);
651 /* check if STA exists already */
652 if (sta_info_get_bss(sdata, sta->sta.addr)) {
657 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
664 local->sta_generation++;
667 /* simplify things and don't accept BA sessions yet */
668 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
670 /* make the station visible */
671 err = sta_info_hash_add(local, sta);
675 list_add_tail_rcu(&sta->list, &local->sta_list);
677 /* update channel context before notifying the driver about state
678 * change, this enables driver using the updated channel context right away.
680 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
681 ieee80211_recalc_min_chandef(sta->sdata);
682 if (!sta->sta.support_p2p_ps)
683 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
687 err = sta_info_insert_drv_state(local, sdata, sta);
691 set_sta_flag(sta, WLAN_STA_INSERTED);
693 /* accept BA sessions now */
694 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
696 ieee80211_sta_debugfs_add(sta);
697 rate_control_add_sta_debugfs(sta);
699 sinfo->generation = local->sta_generation;
700 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
703 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
705 /* move reference to rcu-protected */
707 mutex_unlock(&local->sta_mtx);
709 if (ieee80211_vif_is_mesh(&sdata->vif))
710 mesh_accept_plinks_update(sdata);
714 sta_info_hash_del(local, sta);
715 list_del_rcu(&sta->list);
720 cleanup_single_sta(sta);
721 mutex_unlock(&local->sta_mtx);
727 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
729 struct ieee80211_local *local = sta->local;
734 mutex_lock(&local->sta_mtx);
736 err = sta_info_insert_check(sta);
738 sta_info_free(local, sta);
739 mutex_unlock(&local->sta_mtx);
744 return sta_info_insert_finish(sta);
747 int sta_info_insert(struct sta_info *sta)
749 int err = sta_info_insert_rcu(sta);
756 static inline void __bss_tim_set(u8 *tim, u16 id)
759 * This format has been mandated by the IEEE specifications,
760 * so this line may not be changed to use the __set_bit() format.
762 tim[id / 8] |= (1 << (id % 8));
765 static inline void __bss_tim_clear(u8 *tim, u16 id)
768 * This format has been mandated by the IEEE specifications,
769 * so this line may not be changed to use the __clear_bit() format.
771 tim[id / 8] &= ~(1 << (id % 8));
774 static inline bool __bss_tim_get(u8 *tim, u16 id)
777 * This format has been mandated by the IEEE specifications,
778 * so this line may not be changed to use the test_bit() format.
780 return tim[id / 8] & (1 << (id % 8));
783 static unsigned long ieee80211_tids_for_ac(int ac)
785 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
787 case IEEE80211_AC_VO:
788 return BIT(6) | BIT(7);
789 case IEEE80211_AC_VI:
790 return BIT(4) | BIT(5);
791 case IEEE80211_AC_BE:
792 return BIT(0) | BIT(3);
793 case IEEE80211_AC_BK:
794 return BIT(1) | BIT(2);
801 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
803 struct ieee80211_local *local = sta->local;
805 bool indicate_tim = false;
806 u8 ignore_for_tim = sta->sta.uapsd_queues;
808 u16 id = sta->sta.aid;
810 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
811 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
812 if (WARN_ON_ONCE(!sta->sdata->bss))
815 ps = &sta->sdata->bss->ps;
816 #ifdef CONFIG_MAC80211_MESH
817 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
818 ps = &sta->sdata->u.mesh.ps;
824 /* No need to do anything if the driver does all */
825 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
832 * If all ACs are delivery-enabled then we should build
833 * the TIM bit for all ACs anyway; if only some are then
834 * we ignore those and build the TIM bit using only the
837 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
841 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
843 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
846 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
849 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
850 !skb_queue_empty(&sta->ps_tx_buf[ac]);
854 tids = ieee80211_tids_for_ac(ac);
857 sta->driver_buffered_tids & tids;
859 sta->txq_buffered_tids & tids;
863 spin_lock_bh(&local->tim_lock);
865 if (indicate_tim == __bss_tim_get(ps->tim, id))
869 __bss_tim_set(ps->tim, id);
871 __bss_tim_clear(ps->tim, id);
873 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
874 local->tim_in_locked_section = true;
875 drv_set_tim(local, &sta->sta, indicate_tim);
876 local->tim_in_locked_section = false;
880 spin_unlock_bh(&local->tim_lock);
883 void sta_info_recalc_tim(struct sta_info *sta)
885 __sta_info_recalc_tim(sta, false);
888 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
890 struct ieee80211_tx_info *info;
896 info = IEEE80211_SKB_CB(skb);
898 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
899 timeout = (sta->listen_interval *
900 sta->sdata->vif.bss_conf.beacon_int *
902 if (timeout < STA_TX_BUFFER_EXPIRE)
903 timeout = STA_TX_BUFFER_EXPIRE;
904 return time_after(jiffies, info->control.jiffies + timeout);
908 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
909 struct sta_info *sta, int ac)
915 * First check for frames that should expire on the filtered
916 * queue. Frames here were rejected by the driver and are on
917 * a separate queue to avoid reordering with normal PS-buffered
918 * frames. They also aren't accounted for right now in the
919 * total_ps_buffered counter.
922 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
923 skb = skb_peek(&sta->tx_filtered[ac]);
924 if (sta_info_buffer_expired(sta, skb))
925 skb = __skb_dequeue(&sta->tx_filtered[ac]);
928 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
931 * Frames are queued in order, so if this one
932 * hasn't expired yet we can stop testing. If
933 * we actually reached the end of the queue we
934 * also need to stop, of course.
938 ieee80211_free_txskb(&local->hw, skb);
942 * Now also check the normal PS-buffered queue, this will
943 * only find something if the filtered queue was emptied
944 * since the filtered frames are all before the normal PS
948 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
949 skb = skb_peek(&sta->ps_tx_buf[ac]);
950 if (sta_info_buffer_expired(sta, skb))
951 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
954 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
957 * frames are queued in order, so if this one
958 * hasn't expired yet (or we reached the end of
959 * the queue) we can stop testing
964 local->total_ps_buffered--;
965 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
967 ieee80211_free_txskb(&local->hw, skb);
971 * Finally, recalculate the TIM bit for this station -- it might
972 * now be clear because the station was too slow to retrieve its
975 sta_info_recalc_tim(sta);
978 * Return whether there are any frames still buffered, this is
979 * used to check whether the cleanup timer still needs to run,
980 * if there are no frames we don't need to rearm the timer.
982 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
983 skb_queue_empty(&sta->tx_filtered[ac]));
986 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
987 struct sta_info *sta)
989 bool have_buffered = false;
992 /* This is only necessary for stations on BSS/MBSS interfaces */
993 if (!sta->sdata->bss &&
994 !ieee80211_vif_is_mesh(&sta->sdata->vif))
997 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
999 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1001 return have_buffered;
1004 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1006 struct ieee80211_local *local;
1007 struct ieee80211_sub_if_data *sdata;
1018 lockdep_assert_held(&local->sta_mtx);
1021 * Before removing the station from the driver and
1022 * rate control, it might still start new aggregation
1023 * sessions -- block that to make sure the tear-down
1024 * will be sufficient.
1026 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1027 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1030 * Before removing the station from the driver there might be pending
1031 * rx frames on RSS queues sent prior to the disassociation - wait for
1032 * all such frames to be processed.
1034 drv_sync_rx_queues(local, sta);
1036 ret = sta_info_hash_del(local, sta);
1041 * for TDLS peers, make sure to return to the base channel before
1044 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1045 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1046 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1049 list_del_rcu(&sta->list);
1050 sta->removed = true;
1052 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1054 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1055 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1056 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1061 static void __sta_info_destroy_part2(struct sta_info *sta)
1063 struct ieee80211_local *local = sta->local;
1064 struct ieee80211_sub_if_data *sdata = sta->sdata;
1065 struct station_info *sinfo;
1069 * NOTE: This assumes at least synchronize_net() was done
1070 * after _part1 and before _part2!
1074 lockdep_assert_held(&local->sta_mtx);
1076 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1077 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1081 /* now keys can no longer be reached */
1082 ieee80211_free_sta_keys(local, sta);
1084 /* disable TIM bit - last chance to tell driver */
1085 __sta_info_recalc_tim(sta, true);
1090 local->sta_generation++;
1092 while (sta->sta_state > IEEE80211_STA_NONE) {
1093 ret = sta_info_move_state(sta, sta->sta_state - 1);
1100 if (sta->uploaded) {
1101 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1102 IEEE80211_STA_NOTEXIST);
1103 WARN_ON_ONCE(ret != 0);
1106 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1108 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1110 sta_set_sinfo(sta, sinfo, true);
1111 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1114 ieee80211_sta_debugfs_remove(sta);
1116 ieee80211_destroy_frag_cache(&sta->frags);
1118 cleanup_single_sta(sta);
1121 int __must_check __sta_info_destroy(struct sta_info *sta)
1123 int err = __sta_info_destroy_part1(sta);
1130 __sta_info_destroy_part2(sta);
1135 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1137 struct sta_info *sta;
1140 mutex_lock(&sdata->local->sta_mtx);
1141 sta = sta_info_get(sdata, addr);
1142 ret = __sta_info_destroy(sta);
1143 mutex_unlock(&sdata->local->sta_mtx);
1148 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1151 struct sta_info *sta;
1154 mutex_lock(&sdata->local->sta_mtx);
1155 sta = sta_info_get_bss(sdata, addr);
1156 ret = __sta_info_destroy(sta);
1157 mutex_unlock(&sdata->local->sta_mtx);
1162 static void sta_info_cleanup(struct timer_list *t)
1164 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1165 struct sta_info *sta;
1166 bool timer_needed = false;
1169 list_for_each_entry_rcu(sta, &local->sta_list, list)
1170 if (sta_info_cleanup_expire_buffered(local, sta))
1171 timer_needed = true;
1174 if (local->quiescing)
1180 mod_timer(&local->sta_cleanup,
1181 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1184 int sta_info_init(struct ieee80211_local *local)
1188 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1192 spin_lock_init(&local->tim_lock);
1193 mutex_init(&local->sta_mtx);
1194 INIT_LIST_HEAD(&local->sta_list);
1196 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1200 void sta_info_stop(struct ieee80211_local *local)
1202 del_timer_sync(&local->sta_cleanup);
1203 rhltable_destroy(&local->sta_hash);
1207 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1209 struct ieee80211_local *local = sdata->local;
1210 struct sta_info *sta, *tmp;
1211 LIST_HEAD(free_list);
1216 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1217 WARN_ON(vlans && !sdata->bss);
1219 mutex_lock(&local->sta_mtx);
1220 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1221 if (sdata == sta->sdata ||
1222 (vlans && sdata->bss == sta->sdata->bss)) {
1223 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1224 list_add(&sta->free_list, &free_list);
1229 if (!list_empty(&free_list)) {
1231 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1232 __sta_info_destroy_part2(sta);
1234 mutex_unlock(&local->sta_mtx);
1239 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1240 unsigned long exp_time)
1242 struct ieee80211_local *local = sdata->local;
1243 struct sta_info *sta, *tmp;
1245 mutex_lock(&local->sta_mtx);
1247 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1248 unsigned long last_active = ieee80211_sta_last_active(sta);
1250 if (sdata != sta->sdata)
1253 if (time_is_before_jiffies(last_active + exp_time)) {
1254 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1257 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1258 test_sta_flag(sta, WLAN_STA_PS_STA))
1259 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1261 WARN_ON(__sta_info_destroy(sta));
1265 mutex_unlock(&local->sta_mtx);
1268 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1270 const u8 *localaddr)
1272 struct ieee80211_local *local = hw_to_local(hw);
1273 struct rhlist_head *tmp;
1274 struct sta_info *sta;
1277 * Just return a random station if localaddr is NULL
1278 * ... first in list.
1280 for_each_sta_info(local, addr, sta, tmp) {
1282 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1291 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1293 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1296 struct sta_info *sta;
1301 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1310 EXPORT_SYMBOL(ieee80211_find_sta);
1312 /* powersave support code */
1313 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1315 struct ieee80211_sub_if_data *sdata = sta->sdata;
1316 struct ieee80211_local *local = sdata->local;
1317 struct sk_buff_head pending;
1318 int filtered = 0, buffered = 0, ac, i;
1319 unsigned long flags;
1322 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1323 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1326 if (sdata->vif.type == NL80211_IFTYPE_AP)
1327 ps = &sdata->bss->ps;
1328 else if (ieee80211_vif_is_mesh(&sdata->vif))
1329 ps = &sdata->u.mesh.ps;
1333 clear_sta_flag(sta, WLAN_STA_SP);
1335 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1336 sta->driver_buffered_tids = 0;
1337 sta->txq_buffered_tids = 0;
1339 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1340 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1342 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1343 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1346 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1349 skb_queue_head_init(&pending);
1351 /* sync with ieee80211_tx_h_unicast_ps_buf */
1352 spin_lock(&sta->ps_lock);
1353 /* Send all buffered frames to the station */
1354 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1355 int count = skb_queue_len(&pending), tmp;
1357 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1358 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1359 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1360 tmp = skb_queue_len(&pending);
1361 filtered += tmp - count;
1364 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1365 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1366 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1367 tmp = skb_queue_len(&pending);
1368 buffered += tmp - count;
1371 ieee80211_add_pending_skbs(local, &pending);
1373 /* now we're no longer in the deliver code */
1374 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1376 /* The station might have polled and then woken up before we responded,
1377 * so clear these flags now to avoid them sticking around.
1379 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1380 clear_sta_flag(sta, WLAN_STA_UAPSD);
1381 spin_unlock(&sta->ps_lock);
1383 atomic_dec(&ps->num_sta_ps);
1385 local->total_ps_buffered -= buffered;
1387 sta_info_recalc_tim(sta);
1390 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1391 sta->sta.addr, sta->sta.aid, filtered, buffered);
1393 ieee80211_check_fast_xmit(sta);
1396 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1397 enum ieee80211_frame_release_type reason,
1398 bool call_driver, bool more_data)
1400 struct ieee80211_sub_if_data *sdata = sta->sdata;
1401 struct ieee80211_local *local = sdata->local;
1402 struct ieee80211_qos_hdr *nullfunc;
1403 struct sk_buff *skb;
1404 int size = sizeof(*nullfunc);
1406 bool qos = sta->sta.wme;
1407 struct ieee80211_tx_info *info;
1408 struct ieee80211_chanctx_conf *chanctx_conf;
1411 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1412 IEEE80211_STYPE_QOS_NULLFUNC |
1413 IEEE80211_FCTL_FROMDS);
1416 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1417 IEEE80211_STYPE_NULLFUNC |
1418 IEEE80211_FCTL_FROMDS);
1421 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1425 skb_reserve(skb, local->hw.extra_tx_headroom);
1427 nullfunc = skb_put(skb, size);
1428 nullfunc->frame_control = fc;
1429 nullfunc->duration_id = 0;
1430 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1431 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1432 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1433 nullfunc->seq_ctrl = 0;
1435 skb->priority = tid;
1436 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1438 nullfunc->qos_ctrl = cpu_to_le16(tid);
1440 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1441 nullfunc->qos_ctrl |=
1442 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1444 nullfunc->frame_control |=
1445 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1449 info = IEEE80211_SKB_CB(skb);
1452 * Tell TX path to send this frame even though the
1453 * STA may still remain is PS mode after this frame
1454 * exchange. Also set EOSP to indicate this packet
1455 * ends the poll/service period.
1457 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1458 IEEE80211_TX_STATUS_EOSP |
1459 IEEE80211_TX_CTL_REQ_TX_STATUS;
1461 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1464 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1467 skb->dev = sdata->dev;
1470 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1471 if (WARN_ON(!chanctx_conf)) {
1477 info->band = chanctx_conf->def.chan->band;
1478 ieee80211_xmit(sdata, sta, skb);
1482 static int find_highest_prio_tid(unsigned long tids)
1484 /* lower 3 TIDs aren't ordered perfectly */
1486 return fls(tids) - 1;
1487 /* TID 0 is BE just like TID 3 */
1490 return fls(tids) - 1;
1493 /* Indicates if the MORE_DATA bit should be set in the last
1494 * frame obtained by ieee80211_sta_ps_get_frames.
1495 * Note that driver_release_tids is relevant only if
1496 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1499 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1500 enum ieee80211_frame_release_type reason,
1501 unsigned long driver_release_tids)
1505 /* If the driver has data on more than one TID then
1506 * certainly there's more data if we release just a
1507 * single frame now (from a single TID). This will
1508 * only happen for PS-Poll.
1510 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1511 hweight16(driver_release_tids) > 1)
1514 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1515 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1518 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1519 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1527 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1528 enum ieee80211_frame_release_type reason,
1529 struct sk_buff_head *frames,
1530 unsigned long *driver_release_tids)
1532 struct ieee80211_sub_if_data *sdata = sta->sdata;
1533 struct ieee80211_local *local = sdata->local;
1536 /* Get response frame(s) and more data bit for the last one. */
1537 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1540 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1543 tids = ieee80211_tids_for_ac(ac);
1545 /* if we already have frames from software, then we can't also
1546 * release from hardware queues
1548 if (skb_queue_empty(frames)) {
1549 *driver_release_tids |=
1550 sta->driver_buffered_tids & tids;
1551 *driver_release_tids |= sta->txq_buffered_tids & tids;
1554 if (!*driver_release_tids) {
1555 struct sk_buff *skb;
1557 while (n_frames > 0) {
1558 skb = skb_dequeue(&sta->tx_filtered[ac]);
1561 &sta->ps_tx_buf[ac]);
1563 local->total_ps_buffered--;
1568 __skb_queue_tail(frames, skb);
1572 /* If we have more frames buffered on this AC, then abort the
1573 * loop since we can't send more data from other ACs before
1574 * the buffered frames from this.
1576 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1577 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1583 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1584 int n_frames, u8 ignored_acs,
1585 enum ieee80211_frame_release_type reason)
1587 struct ieee80211_sub_if_data *sdata = sta->sdata;
1588 struct ieee80211_local *local = sdata->local;
1589 unsigned long driver_release_tids = 0;
1590 struct sk_buff_head frames;
1593 /* Service or PS-Poll period starts */
1594 set_sta_flag(sta, WLAN_STA_SP);
1596 __skb_queue_head_init(&frames);
1598 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1599 &frames, &driver_release_tids);
1601 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1603 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1604 driver_release_tids =
1605 BIT(find_highest_prio_tid(driver_release_tids));
1607 if (skb_queue_empty(&frames) && !driver_release_tids) {
1611 * For PS-Poll, this can only happen due to a race condition
1612 * when we set the TIM bit and the station notices it, but
1613 * before it can poll for the frame we expire it.
1615 * For uAPSD, this is said in the standard (11.2.1.5 h):
1616 * At each unscheduled SP for a non-AP STA, the AP shall
1617 * attempt to transmit at least one MSDU or MMPDU, but no
1618 * more than the value specified in the Max SP Length field
1619 * in the QoS Capability element from delivery-enabled ACs,
1620 * that are destined for the non-AP STA.
1622 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1625 /* This will evaluate to 1, 3, 5 or 7. */
1626 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1627 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1631 ieee80211_send_null_response(sta, tid, reason, true, false);
1632 } else if (!driver_release_tids) {
1633 struct sk_buff_head pending;
1634 struct sk_buff *skb;
1637 bool need_null = false;
1639 skb_queue_head_init(&pending);
1641 while ((skb = __skb_dequeue(&frames))) {
1642 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1643 struct ieee80211_hdr *hdr = (void *) skb->data;
1649 * Tell TX path to send this frame even though the
1650 * STA may still remain is PS mode after this frame
1653 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1654 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1657 * Use MoreData flag to indicate whether there are
1658 * more buffered frames for this STA
1660 if (more_data || !skb_queue_empty(&frames))
1661 hdr->frame_control |=
1662 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1664 hdr->frame_control &=
1665 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1667 if (ieee80211_is_data_qos(hdr->frame_control) ||
1668 ieee80211_is_qos_nullfunc(hdr->frame_control))
1669 qoshdr = ieee80211_get_qos_ctl(hdr);
1671 tids |= BIT(skb->priority);
1673 __skb_queue_tail(&pending, skb);
1675 /* end service period after last frame or add one */
1676 if (!skb_queue_empty(&frames))
1679 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1680 /* for PS-Poll, there's only one frame */
1681 info->flags |= IEEE80211_TX_STATUS_EOSP |
1682 IEEE80211_TX_CTL_REQ_TX_STATUS;
1686 /* For uAPSD, things are a bit more complicated. If the
1687 * last frame has a QoS header (i.e. is a QoS-data or
1688 * QoS-nulldata frame) then just set the EOSP bit there
1690 * If the frame doesn't have a QoS header (which means
1691 * it should be a bufferable MMPDU) then we can't set
1692 * the EOSP bit in the QoS header; add a QoS-nulldata
1693 * frame to the list to send it after the MMPDU.
1695 * Note that this code is only in the mac80211-release
1696 * code path, we assume that the driver will not buffer
1697 * anything but QoS-data frames, or if it does, will
1698 * create the QoS-nulldata frame by itself if needed.
1700 * Cf. 802.11-2012 10.2.1.10 (c).
1703 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1705 info->flags |= IEEE80211_TX_STATUS_EOSP |
1706 IEEE80211_TX_CTL_REQ_TX_STATUS;
1708 /* The standard isn't completely clear on this
1709 * as it says the more-data bit should be set
1710 * if there are more BUs. The QoS-Null frame
1711 * we're about to send isn't buffered yet, we
1712 * only create it below, but let's pretend it
1713 * was buffered just in case some clients only
1714 * expect more-data=0 when eosp=1.
1716 hdr->frame_control |=
1717 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1724 drv_allow_buffered_frames(local, sta, tids, num,
1727 ieee80211_add_pending_skbs(local, &pending);
1730 ieee80211_send_null_response(
1731 sta, find_highest_prio_tid(tids),
1732 reason, false, false);
1734 sta_info_recalc_tim(sta);
1739 * We need to release a frame that is buffered somewhere in the
1740 * driver ... it'll have to handle that.
1741 * Note that the driver also has to check the number of frames
1742 * on the TIDs we're releasing from - if there are more than
1743 * n_frames it has to set the more-data bit (if we didn't ask
1744 * it to set it anyway due to other buffered frames); if there
1745 * are fewer than n_frames it has to make sure to adjust that
1746 * to allow the service period to end properly.
1748 drv_release_buffered_frames(local, sta, driver_release_tids,
1749 n_frames, reason, more_data);
1752 * Note that we don't recalculate the TIM bit here as it would
1753 * most likely have no effect at all unless the driver told us
1754 * that the TID(s) became empty before returning here from the
1756 * Either way, however, when the driver tells us that the TID(s)
1757 * became empty or we find that a txq became empty, we'll do the
1758 * TIM recalculation.
1761 if (!sta->sta.txq[0])
1764 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1765 if (!sta->sta.txq[tid] ||
1766 !(driver_release_tids & BIT(tid)) ||
1767 txq_has_queue(sta->sta.txq[tid]))
1770 sta_info_recalc_tim(sta);
1776 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1778 u8 ignore_for_response = sta->sta.uapsd_queues;
1781 * If all ACs are delivery-enabled then we should reply
1782 * from any of them, if only some are enabled we reply
1783 * only from the non-enabled ones.
1785 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1786 ignore_for_response = 0;
1788 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1789 IEEE80211_FRAME_RELEASE_PSPOLL);
1792 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1794 int n_frames = sta->sta.max_sp;
1795 u8 delivery_enabled = sta->sta.uapsd_queues;
1798 * If we ever grow support for TSPEC this might happen if
1799 * the TSPEC update from hostapd comes in between a trigger
1800 * frame setting WLAN_STA_UAPSD in the RX path and this
1801 * actually getting called.
1803 if (!delivery_enabled)
1806 switch (sta->sta.max_sp) {
1817 /* XXX: what is a good value? */
1822 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1823 IEEE80211_FRAME_RELEASE_UAPSD);
1826 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1827 struct ieee80211_sta *pubsta, bool block)
1829 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1831 trace_api_sta_block_awake(sta->local, pubsta, block);
1834 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1835 ieee80211_clear_fast_xmit(sta);
1839 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1842 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1843 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1844 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1845 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1846 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1847 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1848 /* must be asleep in this case */
1849 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1850 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1852 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1853 ieee80211_check_fast_xmit(sta);
1856 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1858 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1860 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1861 struct ieee80211_local *local = sta->local;
1863 trace_api_eosp(local, pubsta);
1865 clear_sta_flag(sta, WLAN_STA_SP);
1867 EXPORT_SYMBOL(ieee80211_sta_eosp);
1869 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1871 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1872 enum ieee80211_frame_release_type reason;
1875 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1877 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1878 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1881 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1883 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1885 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1886 u8 tid, bool buffered)
1888 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1890 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1893 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1896 set_bit(tid, &sta->driver_buffered_tids);
1898 clear_bit(tid, &sta->driver_buffered_tids);
1900 sta_info_recalc_tim(sta);
1902 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1904 void ieee80211_register_airtime(struct ieee80211_txq *txq,
1905 u32 tx_airtime, u32 rx_airtime)
1907 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
1908 struct ieee80211_local *local = sdata->local;
1909 u64 weight_sum, weight_sum_reciprocal;
1910 struct airtime_sched_info *air_sched;
1911 struct airtime_info *air_info;
1914 air_sched = &local->airtime[txq->ac];
1915 air_info = to_airtime_info(txq);
1917 if (local->airtime_flags & AIRTIME_USE_TX)
1918 airtime += tx_airtime;
1919 if (local->airtime_flags & AIRTIME_USE_RX)
1920 airtime += rx_airtime;
1922 /* Weights scale so the unit weight is 256 */
1925 spin_lock_bh(&air_sched->lock);
1927 air_info->tx_airtime += tx_airtime;
1928 air_info->rx_airtime += rx_airtime;
1930 if (air_sched->weight_sum) {
1931 weight_sum = air_sched->weight_sum;
1932 weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
1934 weight_sum = air_info->weight;
1935 weight_sum_reciprocal = air_info->weight_reciprocal;
1938 /* Round the calculation of global vt */
1939 air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
1940 weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
1941 air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
1942 air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
1943 ieee80211_resort_txq(&local->hw, txq);
1945 spin_unlock_bh(&air_sched->lock);
1948 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1949 u32 tx_airtime, u32 rx_airtime)
1951 struct ieee80211_txq *txq = pubsta->txq[tid];
1956 ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
1958 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1960 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
1961 struct sta_info *sta, u8 ac,
1962 u16 tx_airtime, bool tx_completed)
1966 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
1969 if (!tx_completed) {
1971 atomic_add(tx_airtime,
1972 &sta->airtime[ac].aql_tx_pending);
1974 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
1979 tx_pending = atomic_sub_return(tx_airtime,
1980 &sta->airtime[ac].aql_tx_pending);
1982 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
1986 tx_pending = atomic_sub_return(tx_airtime,
1987 &local->aql_total_pending_airtime);
1988 if (WARN_ONCE(tx_pending < 0,
1989 "Device %s AC %d pending airtime underflow: %u, %u",
1990 wiphy_name(local->hw.wiphy), ac, tx_pending,
1992 atomic_cmpxchg(&local->aql_total_pending_airtime,
1996 int sta_info_move_state(struct sta_info *sta,
1997 enum ieee80211_sta_state new_state)
2001 if (sta->sta_state == new_state)
2004 /* check allowed transitions first */
2006 switch (new_state) {
2007 case IEEE80211_STA_NONE:
2008 if (sta->sta_state != IEEE80211_STA_AUTH)
2011 case IEEE80211_STA_AUTH:
2012 if (sta->sta_state != IEEE80211_STA_NONE &&
2013 sta->sta_state != IEEE80211_STA_ASSOC)
2016 case IEEE80211_STA_ASSOC:
2017 if (sta->sta_state != IEEE80211_STA_AUTH &&
2018 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2021 case IEEE80211_STA_AUTHORIZED:
2022 if (sta->sta_state != IEEE80211_STA_ASSOC)
2026 WARN(1, "invalid state %d", new_state);
2030 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2031 sta->sta.addr, new_state);
2034 * notify the driver before the actual changes so it can
2035 * fail the transition
2037 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2038 int err = drv_sta_state(sta->local, sta->sdata, sta,
2039 sta->sta_state, new_state);
2044 /* reflect the change in all state variables */
2046 switch (new_state) {
2047 case IEEE80211_STA_NONE:
2048 if (sta->sta_state == IEEE80211_STA_AUTH)
2049 clear_bit(WLAN_STA_AUTH, &sta->_flags);
2051 case IEEE80211_STA_AUTH:
2052 if (sta->sta_state == IEEE80211_STA_NONE) {
2053 set_bit(WLAN_STA_AUTH, &sta->_flags);
2054 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2055 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2056 ieee80211_recalc_min_chandef(sta->sdata);
2057 if (!sta->sta.support_p2p_ps)
2058 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2061 case IEEE80211_STA_ASSOC:
2062 if (sta->sta_state == IEEE80211_STA_AUTH) {
2063 set_bit(WLAN_STA_ASSOC, &sta->_flags);
2064 sta->assoc_at = ktime_get_boottime_ns();
2065 ieee80211_recalc_min_chandef(sta->sdata);
2066 if (!sta->sta.support_p2p_ps)
2067 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2068 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2069 ieee80211_vif_dec_num_mcast(sta->sdata);
2070 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2071 ieee80211_clear_fast_xmit(sta);
2072 ieee80211_clear_fast_rx(sta);
2075 case IEEE80211_STA_AUTHORIZED:
2076 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2077 ieee80211_vif_inc_num_mcast(sta->sdata);
2078 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2079 ieee80211_check_fast_xmit(sta);
2080 ieee80211_check_fast_rx(sta);
2082 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2083 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2084 cfg80211_send_layer2_update(sta->sdata->dev,
2091 sta->sta_state = new_state;
2096 u8 sta_info_tx_streams(struct sta_info *sta)
2098 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.deflink.ht_cap;
2101 if (!sta->sta.deflink.ht_cap.ht_supported)
2104 if (sta->sta.deflink.vht_cap.vht_supported) {
2107 le16_to_cpu(sta->sta.deflink.vht_cap.vht_mcs.tx_mcs_map);
2109 for (i = 7; i >= 0; i--)
2110 if ((tx_mcs_map & (0x3 << (i * 2))) !=
2111 IEEE80211_VHT_MCS_NOT_SUPPORTED)
2115 if (ht_cap->mcs.rx_mask[3])
2117 else if (ht_cap->mcs.rx_mask[2])
2119 else if (ht_cap->mcs.rx_mask[1])
2124 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2127 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2128 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2131 static struct ieee80211_sta_rx_stats *
2132 sta_get_last_rx_stats(struct sta_info *sta)
2134 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2137 if (!sta->deflink.pcpu_rx_stats)
2140 for_each_possible_cpu(cpu) {
2141 struct ieee80211_sta_rx_stats *cpustats;
2143 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2145 if (time_after(cpustats->last_rx, stats->last_rx))
2152 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2153 struct rate_info *rinfo)
2155 rinfo->bw = STA_STATS_GET(BW, rate);
2157 switch (STA_STATS_GET(TYPE, rate)) {
2158 case STA_STATS_RATE_TYPE_VHT:
2159 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2160 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2161 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2162 if (STA_STATS_GET(SGI, rate))
2163 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2165 case STA_STATS_RATE_TYPE_HT:
2166 rinfo->flags = RATE_INFO_FLAGS_MCS;
2167 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2168 if (STA_STATS_GET(SGI, rate))
2169 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2171 case STA_STATS_RATE_TYPE_LEGACY: {
2172 struct ieee80211_supported_band *sband;
2175 int band = STA_STATS_GET(LEGACY_BAND, rate);
2176 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2178 sband = local->hw.wiphy->bands[band];
2180 if (WARN_ON_ONCE(!sband->bitrates))
2183 brate = sband->bitrates[rate_idx].bitrate;
2184 if (rinfo->bw == RATE_INFO_BW_5)
2186 else if (rinfo->bw == RATE_INFO_BW_10)
2190 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2193 case STA_STATS_RATE_TYPE_HE:
2194 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2195 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2196 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2197 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2198 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2199 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2204 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2206 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2208 if (rate == STA_STATS_RATE_INVALID)
2211 sta_stats_decode_rate(sta->local, rate, rinfo);
2215 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2222 start = u64_stats_fetch_begin(&rxstats->syncp);
2223 value = rxstats->msdu[tid];
2224 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2229 static void sta_set_tidstats(struct sta_info *sta,
2230 struct cfg80211_tid_stats *tidstats,
2233 struct ieee80211_local *local = sta->local;
2236 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2237 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2240 if (sta->deflink.pcpu_rx_stats) {
2241 for_each_possible_cpu(cpu) {
2242 struct ieee80211_sta_rx_stats *cpurxs;
2244 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2246 tidstats->rx_msdu +=
2247 sta_get_tidstats_msdu(cpurxs, tid);
2251 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2254 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2255 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2256 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2259 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2260 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2261 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2262 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2265 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2266 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2267 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2268 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2271 if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2272 spin_lock_bh(&local->fq.lock);
2275 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2276 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2277 to_txq_info(sta->sta.txq[tid]));
2280 spin_unlock_bh(&local->fq.lock);
2284 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2290 start = u64_stats_fetch_begin(&rxstats->syncp);
2291 value = rxstats->bytes;
2292 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2297 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2300 struct ieee80211_sub_if_data *sdata = sta->sdata;
2301 struct ieee80211_local *local = sdata->local;
2304 struct ieee80211_sta_rx_stats *last_rxstats;
2306 last_rxstats = sta_get_last_rx_stats(sta);
2308 sinfo->generation = sdata->local->sta_generation;
2310 /* do before driver, so beacon filtering drivers have a
2311 * chance to e.g. just add the number of filtered beacons
2312 * (or just modify the value entirely, of course)
2314 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2315 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2317 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2318 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2319 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2320 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2321 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2322 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2323 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2325 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2326 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2327 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2330 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2331 sinfo->assoc_at = sta->assoc_at;
2332 sinfo->inactive_time =
2333 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2335 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2336 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2337 sinfo->tx_bytes = 0;
2338 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2339 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2340 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2343 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2344 sinfo->tx_packets = 0;
2345 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2346 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2347 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2350 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2351 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2352 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2354 if (sta->deflink.pcpu_rx_stats) {
2355 for_each_possible_cpu(cpu) {
2356 struct ieee80211_sta_rx_stats *cpurxs;
2358 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2360 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2364 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2367 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2368 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2369 if (sta->deflink.pcpu_rx_stats) {
2370 for_each_possible_cpu(cpu) {
2371 struct ieee80211_sta_rx_stats *cpurxs;
2373 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2375 sinfo->rx_packets += cpurxs->packets;
2378 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2381 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2382 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2383 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2386 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2387 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2388 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2391 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2392 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2393 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2394 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2397 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2398 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2399 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2400 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2403 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2404 sinfo->airtime_weight = sta->airtime[0].weight;
2405 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2408 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2409 if (sta->deflink.pcpu_rx_stats) {
2410 for_each_possible_cpu(cpu) {
2411 struct ieee80211_sta_rx_stats *cpurxs;
2413 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2414 sinfo->rx_dropped_misc += cpurxs->dropped;
2418 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2419 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2420 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2421 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2422 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2425 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2426 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2427 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2428 sinfo->signal = (s8)last_rxstats->last_signal;
2429 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2432 if (!sta->deflink.pcpu_rx_stats &&
2433 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2435 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2436 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2440 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2441 * the sta->rx_stats struct, so the check here is fine with and without
2444 if (last_rxstats->chains &&
2445 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2446 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2447 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2448 if (!sta->deflink.pcpu_rx_stats)
2449 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2451 sinfo->chains = last_rxstats->chains;
2453 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2454 sinfo->chain_signal[i] =
2455 last_rxstats->chain_signal_last[i];
2456 sinfo->chain_signal_avg[i] =
2457 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2461 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2462 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2464 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2467 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2468 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2469 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2472 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2473 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2474 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2477 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2478 #ifdef CONFIG_MAC80211_MESH
2479 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2480 BIT_ULL(NL80211_STA_INFO_PLID) |
2481 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2482 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2483 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2484 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2485 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2486 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2488 sinfo->llid = sta->mesh->llid;
2489 sinfo->plid = sta->mesh->plid;
2490 sinfo->plink_state = sta->mesh->plink_state;
2491 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2492 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2493 sinfo->t_offset = sta->mesh->t_offset;
2495 sinfo->local_pm = sta->mesh->local_pm;
2496 sinfo->peer_pm = sta->mesh->peer_pm;
2497 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2498 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2499 sinfo->connected_to_as = sta->mesh->connected_to_as;
2503 sinfo->bss_param.flags = 0;
2504 if (sdata->vif.bss_conf.use_cts_prot)
2505 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2506 if (sdata->vif.bss_conf.use_short_preamble)
2507 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2508 if (sdata->vif.bss_conf.use_short_slot)
2509 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2510 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2511 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2513 sinfo->sta_flags.set = 0;
2514 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2515 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2516 BIT(NL80211_STA_FLAG_WME) |
2517 BIT(NL80211_STA_FLAG_MFP) |
2518 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2519 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2520 BIT(NL80211_STA_FLAG_TDLS_PEER);
2521 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2522 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2523 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2524 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2526 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2527 if (test_sta_flag(sta, WLAN_STA_MFP))
2528 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2529 if (test_sta_flag(sta, WLAN_STA_AUTH))
2530 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2531 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2532 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2533 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2534 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2536 thr = sta_get_expected_throughput(sta);
2539 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2540 sinfo->expected_throughput = thr;
2543 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2544 sta->deflink.status_stats.ack_signal_filled) {
2545 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2546 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2549 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2550 sta->deflink.status_stats.ack_signal_filled) {
2551 sinfo->avg_ack_signal =
2552 -(s8)ewma_avg_signal_read(
2553 &sta->deflink.status_stats.avg_ack_signal);
2555 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2558 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2559 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2560 sinfo->airtime_link_metric =
2561 airtime_link_metric_get(local, sta);
2565 u32 sta_get_expected_throughput(struct sta_info *sta)
2567 struct ieee80211_sub_if_data *sdata = sta->sdata;
2568 struct ieee80211_local *local = sdata->local;
2569 struct rate_control_ref *ref = NULL;
2572 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2573 ref = local->rate_ctrl;
2575 /* check if the driver has a SW RC implementation */
2576 if (ref && ref->ops->get_expected_throughput)
2577 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2579 thr = drv_get_expected_throughput(local, sta);
2584 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2586 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2588 if (!sta->deflink.status_stats.last_ack ||
2589 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2590 return stats->last_rx;
2591 return sta->deflink.status_stats.last_ack;
2594 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2596 if (!sta->sdata->local->ops->wake_tx_queue)
2599 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2600 sta->cparams.target = MS2TIME(50);
2601 sta->cparams.interval = MS2TIME(300);
2602 sta->cparams.ecn = false;
2604 sta->cparams.target = MS2TIME(20);
2605 sta->cparams.interval = MS2TIME(100);
2606 sta->cparams.ecn = true;
2610 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2613 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2615 sta_update_codel_params(sta, thr);