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 struct sta_link_alloc {
68 struct link_sta_info info;
69 struct ieee80211_link_sta sta;
70 struct rcu_head rcu_head;
73 static const struct rhashtable_params sta_rht_params = {
74 .nelem_hint = 3, /* start small */
75 .automatic_shrinking = true,
76 .head_offset = offsetof(struct sta_info, hash_node),
77 .key_offset = offsetof(struct sta_info, addr),
79 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
82 static const struct rhashtable_params link_sta_rht_params = {
83 .nelem_hint = 3, /* start small */
84 .automatic_shrinking = true,
85 .head_offset = offsetof(struct link_sta_info, link_hash_node),
86 .key_offset = offsetof(struct link_sta_info, addr),
88 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
91 /* Caller must hold local->sta_mtx */
92 static int sta_info_hash_del(struct ieee80211_local *local,
95 return rhltable_remove(&local->sta_hash, &sta->hash_node,
99 static int link_sta_info_hash_add(struct ieee80211_local *local,
100 struct link_sta_info *link_sta)
102 lockdep_assert_held(&local->sta_mtx);
103 return rhltable_insert(&local->link_sta_hash,
104 &link_sta->link_hash_node,
105 link_sta_rht_params);
108 static int link_sta_info_hash_del(struct ieee80211_local *local,
109 struct link_sta_info *link_sta)
111 lockdep_assert_held(&local->sta_mtx);
112 return rhltable_remove(&local->link_sta_hash,
113 &link_sta->link_hash_node,
114 link_sta_rht_params);
117 static void __cleanup_single_sta(struct sta_info *sta)
120 struct tid_ampdu_tx *tid_tx;
121 struct ieee80211_sub_if_data *sdata = sta->sdata;
122 struct ieee80211_local *local = sdata->local;
125 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
126 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
127 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
128 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
129 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130 ps = &sdata->bss->ps;
131 else if (ieee80211_vif_is_mesh(&sdata->vif))
132 ps = &sdata->u.mesh.ps;
136 clear_sta_flag(sta, WLAN_STA_PS_STA);
137 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
138 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
140 atomic_dec(&ps->num_sta_ps);
143 if (sta->sta.txq[0]) {
144 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
145 struct txq_info *txqi;
147 if (!sta->sta.txq[i])
150 txqi = to_txq_info(sta->sta.txq[i]);
152 ieee80211_txq_purge(local, txqi);
156 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
157 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
158 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
159 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
162 if (ieee80211_vif_is_mesh(&sdata->vif))
163 mesh_sta_cleanup(sta);
165 cancel_work_sync(&sta->drv_deliver_wk);
168 * Destroy aggregation state here. It would be nice to wait for the
169 * driver to finish aggregation stop and then clean up, but for now
170 * drivers have to handle aggregation stop being requested, followed
171 * directly by station destruction.
173 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
174 kfree(sta->ampdu_mlme.tid_start_tx[i]);
175 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
178 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
183 static void cleanup_single_sta(struct sta_info *sta)
185 struct ieee80211_sub_if_data *sdata = sta->sdata;
186 struct ieee80211_local *local = sdata->local;
188 __cleanup_single_sta(sta);
189 sta_info_free(local, sta);
192 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
195 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
198 /* protected by RCU */
199 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
202 struct ieee80211_local *local = sdata->local;
203 struct rhlist_head *tmp;
204 struct sta_info *sta;
207 for_each_sta_info(local, addr, sta, tmp) {
208 if (sta->sdata == sdata) {
210 /* this is safe as the caller must already hold
211 * another rcu read section or the mutex
221 * Get sta info either from the specified interface
222 * or from one of its vlans
224 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
227 struct ieee80211_local *local = sdata->local;
228 struct rhlist_head *tmp;
229 struct sta_info *sta;
232 for_each_sta_info(local, addr, sta, tmp) {
233 if (sta->sdata == sdata ||
234 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
236 /* this is safe as the caller must already hold
237 * another rcu read section or the mutex
246 struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
249 return rhltable_lookup(&local->link_sta_hash, addr,
250 link_sta_rht_params);
253 struct link_sta_info *
254 link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
256 struct ieee80211_local *local = sdata->local;
257 struct rhlist_head *tmp;
258 struct link_sta_info *link_sta;
261 for_each_link_sta_info(local, addr, link_sta, tmp) {
262 struct sta_info *sta = link_sta->sta;
264 if (sta->sdata == sdata ||
265 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
267 /* this is safe as the caller must already hold
268 * another rcu read section or the mutex
277 struct ieee80211_sta *
278 ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
281 unsigned int *link_id)
283 struct ieee80211_local *local = hw_to_local(hw);
284 struct link_sta_info *link_sta;
285 struct rhlist_head *tmp;
287 for_each_link_sta_info(local, addr, link_sta, tmp) {
288 struct sta_info *sta = link_sta->sta;
289 struct ieee80211_link_data *link;
290 u8 _link_id = link_sta->link_id;
298 link = rcu_dereference(sta->sdata->link[_link_id]);
302 if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
312 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
314 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
315 const u8 *sta_addr, const u8 *vif_addr)
317 struct rhlist_head *tmp;
318 struct sta_info *sta;
320 for_each_sta_info(local, sta_addr, sta, tmp) {
321 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
328 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
331 struct ieee80211_local *local = sdata->local;
332 struct sta_info *sta;
335 list_for_each_entry_rcu(sta, &local->sta_list, list,
336 lockdep_is_held(&local->sta_mtx)) {
337 if (sdata != sta->sdata)
349 static void sta_info_free_link(struct link_sta_info *link_sta)
351 free_percpu(link_sta->pcpu_rx_stats);
354 static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
357 struct sta_link_alloc *alloc = NULL;
358 struct link_sta_info *link_sta;
360 link_sta = rcu_dereference_protected(sta->link[link_id],
361 lockdep_is_held(&sta->local->sta_mtx));
363 if (WARN_ON(!link_sta))
367 link_sta_info_hash_del(sta->local, link_sta);
369 if (link_sta != &sta->deflink)
370 alloc = container_of(link_sta, typeof(*alloc), info);
372 sta->sta.valid_links &= ~BIT(link_id);
373 RCU_INIT_POINTER(sta->link[link_id], NULL);
374 RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
376 sta_info_free_link(&alloc->info);
377 kfree_rcu(alloc, rcu_head);
380 ieee80211_sta_recalc_aggregates(&sta->sta);
384 * sta_info_free - free STA
386 * @local: pointer to the global information
387 * @sta: STA info to free
389 * This function must undo everything done by sta_info_alloc()
390 * that may happen before sta_info_insert(). It may only be
391 * called when sta_info_insert() has not been attempted (and
392 * if that fails, the station is freed anyway.)
394 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
398 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
399 struct link_sta_info *link_sta;
401 link_sta = rcu_access_pointer(sta->link[i]);
405 sta_remove_link(sta, i, false);
409 * If we had used sta_info_pre_move_state() then we might not
410 * have gone through the state transitions down again, so do
411 * it here now (and warn if it's inserted).
413 * This will clear state such as fast TX/RX that may have been
414 * allocated during state transitions.
416 while (sta->sta_state > IEEE80211_STA_NONE) {
419 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
421 ret = sta_info_move_state(sta, sta->sta_state - 1);
422 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
427 rate_control_free_sta(sta);
429 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
432 kfree(to_txq_info(sta->sta.txq[0]));
433 kfree(rcu_dereference_raw(sta->sta.rates));
434 #ifdef CONFIG_MAC80211_MESH
438 sta_info_free_link(&sta->deflink);
442 /* Caller must hold local->sta_mtx */
443 static int sta_info_hash_add(struct ieee80211_local *local,
444 struct sta_info *sta)
446 return rhltable_insert(&local->sta_hash, &sta->hash_node,
450 static void sta_deliver_ps_frames(struct work_struct *wk)
452 struct sta_info *sta;
454 sta = container_of(wk, struct sta_info, drv_deliver_wk);
460 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
461 ieee80211_sta_ps_deliver_wakeup(sta);
462 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
463 ieee80211_sta_ps_deliver_poll_response(sta);
464 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
465 ieee80211_sta_ps_deliver_uapsd(sta);
469 static int sta_prepare_rate_control(struct ieee80211_local *local,
470 struct sta_info *sta, gfp_t gfp)
472 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
475 sta->rate_ctrl = local->rate_ctrl;
476 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
478 if (!sta->rate_ctrl_priv)
484 static int sta_info_alloc_link(struct ieee80211_local *local,
485 struct link_sta_info *link_info,
488 struct ieee80211_hw *hw = &local->hw;
491 if (ieee80211_hw_check(hw, USES_RSS)) {
492 link_info->pcpu_rx_stats =
493 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
494 if (!link_info->pcpu_rx_stats)
498 link_info->rx_stats.last_rx = jiffies;
499 u64_stats_init(&link_info->rx_stats.syncp);
501 ewma_signal_init(&link_info->rx_stats_avg.signal);
502 ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
503 for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
504 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
509 static void sta_info_add_link(struct sta_info *sta,
510 unsigned int link_id,
511 struct link_sta_info *link_info,
512 struct ieee80211_link_sta *link_sta)
514 link_info->sta = sta;
515 link_info->link_id = link_id;
516 link_info->pub = link_sta;
517 link_sta->link_id = link_id;
518 rcu_assign_pointer(sta->link[link_id], link_info);
519 rcu_assign_pointer(sta->sta.link[link_id], link_sta);
521 link_sta->smps_mode = IEEE80211_SMPS_OFF;
522 link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
525 static struct sta_info *
526 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
527 const u8 *addr, int link_id, const u8 *link_addr,
530 struct ieee80211_local *local = sdata->local;
531 struct ieee80211_hw *hw = &local->hw;
532 struct sta_info *sta;
535 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
542 if (sta_info_alloc_link(local, &sta->deflink, gfp))
546 sta_info_add_link(sta, link_id, &sta->deflink,
548 sta->sta.valid_links = BIT(link_id);
550 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
553 sta->sta.cur = &sta->sta.deflink.agg;
555 spin_lock_init(&sta->lock);
556 spin_lock_init(&sta->ps_lock);
557 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
558 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
559 mutex_init(&sta->ampdu_mlme.mtx);
560 #ifdef CONFIG_MAC80211_MESH
561 if (ieee80211_vif_is_mesh(&sdata->vif)) {
562 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
565 sta->mesh->plink_sta = sta;
566 spin_lock_init(&sta->mesh->plink_lock);
567 if (!sdata->u.mesh.user_mpm)
568 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
570 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
574 memcpy(sta->addr, addr, ETH_ALEN);
575 memcpy(sta->sta.addr, addr, ETH_ALEN);
576 memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
577 memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
578 sta->sta.max_rx_aggregation_subframes =
579 local->hw.max_rx_aggregation_subframes;
581 /* TODO link specific alloc and assignments for MLO Link STA */
583 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
584 * The Tx path starts to use a key as soon as the key slot ptk_idx
585 * references to is not NULL. To not use the initial Rx-only key
586 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
587 * which always will refer to a NULL key.
589 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
590 sta->ptk_idx = INVALID_PTK_KEYIDX;
593 ieee80211_init_frag_cache(&sta->frags);
595 sta->sta_state = IEEE80211_STA_NONE;
597 /* Mark TID as unreserved */
598 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
600 sta->last_connected = ktime_get_seconds();
602 if (local->ops->wake_tx_queue) {
604 int size = sizeof(struct txq_info) +
605 ALIGN(hw->txq_data_size, sizeof(void *));
607 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
611 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
612 struct txq_info *txq = txq_data + i * size;
614 /* might not do anything for the bufferable MMPDU TXQ */
615 ieee80211_txq_init(sdata, sta, txq, i);
619 if (sta_prepare_rate_control(local, sta, gfp))
622 sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
624 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
625 skb_queue_head_init(&sta->ps_tx_buf[i]);
626 skb_queue_head_init(&sta->tx_filtered[i]);
627 sta->airtime[i].deficit = sta->airtime_weight;
628 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
629 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
630 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
633 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
634 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
636 for (i = 0; i < NUM_NL80211_BANDS; i++) {
640 if (!hw->wiphy->bands[i])
644 case NL80211_BAND_2GHZ:
645 case NL80211_BAND_LC:
647 * We use both here, even if we cannot really know for
648 * sure the station will support both, but the only use
649 * for this is when we don't know anything yet and send
650 * management frames, and then we'll pick the lowest
651 * possible rate anyway.
652 * If we don't include _G here, we cannot find a rate
653 * in P2P, and thus trigger the WARN_ONCE() in rate.c
655 mandatory = IEEE80211_RATE_MANDATORY_B |
656 IEEE80211_RATE_MANDATORY_G;
658 case NL80211_BAND_5GHZ:
659 mandatory = IEEE80211_RATE_MANDATORY_A;
661 case NL80211_BAND_60GHZ:
667 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
668 struct ieee80211_rate *rate;
670 rate = &hw->wiphy->bands[i]->bitrates[r];
672 if (!(rate->flags & mandatory))
674 sta->sta.deflink.supp_rates[i] |= BIT(r);
678 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
679 sta->cparams.target = MS2TIME(20);
680 sta->cparams.interval = MS2TIME(100);
681 sta->cparams.ecn = true;
682 sta->cparams.ce_threshold_selector = 0;
683 sta->cparams.ce_threshold_mask = 0;
685 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
691 kfree(to_txq_info(sta->sta.txq[0]));
693 sta_info_free_link(&sta->deflink);
694 #ifdef CONFIG_MAC80211_MESH
701 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
702 const u8 *addr, gfp_t gfp)
704 return __sta_info_alloc(sdata, addr, -1, addr, gfp);
707 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
709 unsigned int link_id,
713 return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
716 static int sta_info_insert_check(struct sta_info *sta)
718 struct ieee80211_sub_if_data *sdata = sta->sdata;
721 * Can't be a WARN_ON because it can be triggered through a race:
722 * something inserts a STA (on one CPU) without holding the RTNL
723 * and another CPU turns off the net device.
725 if (unlikely(!ieee80211_sdata_running(sdata)))
728 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
729 !is_valid_ether_addr(sta->sta.addr)))
732 /* The RCU read lock is required by rhashtable due to
733 * asynchronous resize/rehash. We also require the mutex
737 lockdep_assert_held(&sdata->local->sta_mtx);
738 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
739 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
748 static int sta_info_insert_drv_state(struct ieee80211_local *local,
749 struct ieee80211_sub_if_data *sdata,
750 struct sta_info *sta)
752 enum ieee80211_sta_state state;
755 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
756 err = drv_sta_state(local, sdata, sta, state, state + 1);
763 * Drivers using legacy sta_add/sta_remove callbacks only
764 * get uploaded set to true after sta_add is called.
766 if (!local->ops->sta_add)
767 sta->uploaded = true;
771 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
773 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
774 sta->sta.addr, state + 1, err);
778 /* unwind on error */
779 for (; state > IEEE80211_STA_NOTEXIST; state--)
780 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
786 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
788 struct ieee80211_local *local = sdata->local;
789 bool allow_p2p_go_ps = sdata->vif.p2p;
790 struct sta_info *sta;
793 list_for_each_entry_rcu(sta, &local->sta_list, list) {
794 if (sdata != sta->sdata ||
795 !test_sta_flag(sta, WLAN_STA_ASSOC))
797 if (!sta->sta.support_p2p_ps) {
798 allow_p2p_go_ps = false;
804 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
805 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
806 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
812 * should be called with sta_mtx locked
813 * this function replaces the mutex lock
816 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
818 struct ieee80211_local *local = sta->local;
819 struct ieee80211_sub_if_data *sdata = sta->sdata;
820 struct station_info *sinfo = NULL;
823 lockdep_assert_held(&local->sta_mtx);
825 /* check if STA exists already */
826 if (sta_info_get_bss(sdata, sta->sta.addr)) {
831 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
838 local->sta_generation++;
841 /* simplify things and don't accept BA sessions yet */
842 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
844 /* make the station visible */
845 err = sta_info_hash_add(local, sta);
849 if (sta->sta.valid_links) {
850 err = link_sta_info_hash_add(local, &sta->deflink);
852 sta_info_hash_del(local, sta);
857 list_add_tail_rcu(&sta->list, &local->sta_list);
859 /* update channel context before notifying the driver about state
860 * change, this enables driver using the updated channel context right away.
862 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
863 ieee80211_recalc_min_chandef(sta->sdata, -1);
864 if (!sta->sta.support_p2p_ps)
865 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
869 err = sta_info_insert_drv_state(local, sdata, sta);
873 set_sta_flag(sta, WLAN_STA_INSERTED);
875 /* accept BA sessions now */
876 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
878 ieee80211_sta_debugfs_add(sta);
879 rate_control_add_sta_debugfs(sta);
881 sinfo->generation = local->sta_generation;
882 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
885 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
887 /* move reference to rcu-protected */
889 mutex_unlock(&local->sta_mtx);
891 if (ieee80211_vif_is_mesh(&sdata->vif))
892 mesh_accept_plinks_update(sdata);
894 ieee80211_check_fast_xmit(sta);
898 if (sta->sta.valid_links)
899 link_sta_info_hash_del(local, &sta->deflink);
900 sta_info_hash_del(local, sta);
901 list_del_rcu(&sta->list);
906 cleanup_single_sta(sta);
907 mutex_unlock(&local->sta_mtx);
913 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
915 struct ieee80211_local *local = sta->local;
920 mutex_lock(&local->sta_mtx);
922 err = sta_info_insert_check(sta);
924 sta_info_free(local, sta);
925 mutex_unlock(&local->sta_mtx);
930 return sta_info_insert_finish(sta);
933 int sta_info_insert(struct sta_info *sta)
935 int err = sta_info_insert_rcu(sta);
942 static inline void __bss_tim_set(u8 *tim, u16 id)
945 * This format has been mandated by the IEEE specifications,
946 * so this line may not be changed to use the __set_bit() format.
948 tim[id / 8] |= (1 << (id % 8));
951 static inline void __bss_tim_clear(u8 *tim, u16 id)
954 * This format has been mandated by the IEEE specifications,
955 * so this line may not be changed to use the __clear_bit() format.
957 tim[id / 8] &= ~(1 << (id % 8));
960 static inline bool __bss_tim_get(u8 *tim, u16 id)
963 * This format has been mandated by the IEEE specifications,
964 * so this line may not be changed to use the test_bit() format.
966 return tim[id / 8] & (1 << (id % 8));
969 static unsigned long ieee80211_tids_for_ac(int ac)
971 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
973 case IEEE80211_AC_VO:
974 return BIT(6) | BIT(7);
975 case IEEE80211_AC_VI:
976 return BIT(4) | BIT(5);
977 case IEEE80211_AC_BE:
978 return BIT(0) | BIT(3);
979 case IEEE80211_AC_BK:
980 return BIT(1) | BIT(2);
987 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
989 struct ieee80211_local *local = sta->local;
991 bool indicate_tim = false;
992 u8 ignore_for_tim = sta->sta.uapsd_queues;
994 u16 id = sta->sta.aid;
996 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
997 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
998 if (WARN_ON_ONCE(!sta->sdata->bss))
1001 ps = &sta->sdata->bss->ps;
1002 #ifdef CONFIG_MAC80211_MESH
1003 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1004 ps = &sta->sdata->u.mesh.ps;
1010 /* No need to do anything if the driver does all */
1011 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1018 * If all ACs are delivery-enabled then we should build
1019 * the TIM bit for all ACs anyway; if only some are then
1020 * we ignore those and build the TIM bit using only the
1023 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1027 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1029 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1032 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1035 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1036 !skb_queue_empty(&sta->ps_tx_buf[ac]);
1040 tids = ieee80211_tids_for_ac(ac);
1043 sta->driver_buffered_tids & tids;
1045 sta->txq_buffered_tids & tids;
1049 spin_lock_bh(&local->tim_lock);
1051 if (indicate_tim == __bss_tim_get(ps->tim, id))
1055 __bss_tim_set(ps->tim, id);
1057 __bss_tim_clear(ps->tim, id);
1059 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1060 local->tim_in_locked_section = true;
1061 drv_set_tim(local, &sta->sta, indicate_tim);
1062 local->tim_in_locked_section = false;
1066 spin_unlock_bh(&local->tim_lock);
1069 void sta_info_recalc_tim(struct sta_info *sta)
1071 __sta_info_recalc_tim(sta, false);
1074 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1076 struct ieee80211_tx_info *info;
1082 info = IEEE80211_SKB_CB(skb);
1084 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1085 timeout = (sta->listen_interval *
1086 sta->sdata->vif.bss_conf.beacon_int *
1088 if (timeout < STA_TX_BUFFER_EXPIRE)
1089 timeout = STA_TX_BUFFER_EXPIRE;
1090 return time_after(jiffies, info->control.jiffies + timeout);
1094 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1095 struct sta_info *sta, int ac)
1097 unsigned long flags;
1098 struct sk_buff *skb;
1101 * First check for frames that should expire on the filtered
1102 * queue. Frames here were rejected by the driver and are on
1103 * a separate queue to avoid reordering with normal PS-buffered
1104 * frames. They also aren't accounted for right now in the
1105 * total_ps_buffered counter.
1108 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1109 skb = skb_peek(&sta->tx_filtered[ac]);
1110 if (sta_info_buffer_expired(sta, skb))
1111 skb = __skb_dequeue(&sta->tx_filtered[ac]);
1114 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1117 * Frames are queued in order, so if this one
1118 * hasn't expired yet we can stop testing. If
1119 * we actually reached the end of the queue we
1120 * also need to stop, of course.
1124 ieee80211_free_txskb(&local->hw, skb);
1128 * Now also check the normal PS-buffered queue, this will
1129 * only find something if the filtered queue was emptied
1130 * since the filtered frames are all before the normal PS
1134 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1135 skb = skb_peek(&sta->ps_tx_buf[ac]);
1136 if (sta_info_buffer_expired(sta, skb))
1137 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1140 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1143 * frames are queued in order, so if this one
1144 * hasn't expired yet (or we reached the end of
1145 * the queue) we can stop testing
1150 local->total_ps_buffered--;
1151 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1153 ieee80211_free_txskb(&local->hw, skb);
1157 * Finally, recalculate the TIM bit for this station -- it might
1158 * now be clear because the station was too slow to retrieve its
1161 sta_info_recalc_tim(sta);
1164 * Return whether there are any frames still buffered, this is
1165 * used to check whether the cleanup timer still needs to run,
1166 * if there are no frames we don't need to rearm the timer.
1168 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1169 skb_queue_empty(&sta->tx_filtered[ac]));
1172 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1173 struct sta_info *sta)
1175 bool have_buffered = false;
1178 /* This is only necessary for stations on BSS/MBSS interfaces */
1179 if (!sta->sdata->bss &&
1180 !ieee80211_vif_is_mesh(&sta->sdata->vif))
1183 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1185 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1187 return have_buffered;
1190 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1192 struct ieee80211_local *local;
1193 struct ieee80211_sub_if_data *sdata;
1204 lockdep_assert_held(&local->sta_mtx);
1207 * Before removing the station from the driver and
1208 * rate control, it might still start new aggregation
1209 * sessions -- block that to make sure the tear-down
1210 * will be sufficient.
1212 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1213 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1216 * Before removing the station from the driver there might be pending
1217 * rx frames on RSS queues sent prior to the disassociation - wait for
1218 * all such frames to be processed.
1220 drv_sync_rx_queues(local, sta);
1222 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1223 struct link_sta_info *link_sta;
1225 if (!(sta->sta.valid_links & BIT(i)))
1228 link_sta = rcu_dereference_protected(sta->link[i],
1229 lockdep_is_held(&local->sta_mtx));
1231 link_sta_info_hash_del(local, link_sta);
1234 ret = sta_info_hash_del(local, sta);
1239 * for TDLS peers, make sure to return to the base channel before
1242 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1243 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1244 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1247 list_del_rcu(&sta->list);
1248 sta->removed = true;
1251 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1253 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1254 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1255 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1260 static void __sta_info_destroy_part2(struct sta_info *sta)
1262 struct ieee80211_local *local = sta->local;
1263 struct ieee80211_sub_if_data *sdata = sta->sdata;
1264 struct station_info *sinfo;
1268 * NOTE: This assumes at least synchronize_net() was done
1269 * after _part1 and before _part2!
1273 lockdep_assert_held(&local->sta_mtx);
1275 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1276 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1280 /* now keys can no longer be reached */
1281 ieee80211_free_sta_keys(local, sta);
1283 /* disable TIM bit - last chance to tell driver */
1284 __sta_info_recalc_tim(sta, true);
1289 local->sta_generation++;
1291 while (sta->sta_state > IEEE80211_STA_NONE) {
1292 ret = sta_info_move_state(sta, sta->sta_state - 1);
1299 if (sta->uploaded) {
1300 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1301 IEEE80211_STA_NOTEXIST);
1302 WARN_ON_ONCE(ret != 0);
1305 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1307 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1309 sta_set_sinfo(sta, sinfo, true);
1310 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1313 ieee80211_sta_debugfs_remove(sta);
1315 ieee80211_destroy_frag_cache(&sta->frags);
1317 cleanup_single_sta(sta);
1320 int __must_check __sta_info_destroy(struct sta_info *sta)
1322 int err = __sta_info_destroy_part1(sta);
1329 __sta_info_destroy_part2(sta);
1334 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1336 struct sta_info *sta;
1339 mutex_lock(&sdata->local->sta_mtx);
1340 sta = sta_info_get(sdata, addr);
1341 ret = __sta_info_destroy(sta);
1342 mutex_unlock(&sdata->local->sta_mtx);
1347 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1350 struct sta_info *sta;
1353 mutex_lock(&sdata->local->sta_mtx);
1354 sta = sta_info_get_bss(sdata, addr);
1355 ret = __sta_info_destroy(sta);
1356 mutex_unlock(&sdata->local->sta_mtx);
1361 static void sta_info_cleanup(struct timer_list *t)
1363 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1364 struct sta_info *sta;
1365 bool timer_needed = false;
1368 list_for_each_entry_rcu(sta, &local->sta_list, list)
1369 if (sta_info_cleanup_expire_buffered(local, sta))
1370 timer_needed = true;
1373 if (local->quiescing)
1379 mod_timer(&local->sta_cleanup,
1380 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1383 int sta_info_init(struct ieee80211_local *local)
1387 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1391 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1393 rhltable_destroy(&local->sta_hash);
1397 spin_lock_init(&local->tim_lock);
1398 mutex_init(&local->sta_mtx);
1399 INIT_LIST_HEAD(&local->sta_list);
1401 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1405 void sta_info_stop(struct ieee80211_local *local)
1407 del_timer_sync(&local->sta_cleanup);
1408 rhltable_destroy(&local->sta_hash);
1409 rhltable_destroy(&local->link_sta_hash);
1413 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1415 struct ieee80211_local *local = sdata->local;
1416 struct sta_info *sta, *tmp;
1417 LIST_HEAD(free_list);
1422 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1423 WARN_ON(vlans && !sdata->bss);
1425 mutex_lock(&local->sta_mtx);
1426 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1427 if (sdata == sta->sdata ||
1428 (vlans && sdata->bss == sta->sdata->bss)) {
1429 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1430 list_add(&sta->free_list, &free_list);
1435 if (!list_empty(&free_list)) {
1437 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1438 __sta_info_destroy_part2(sta);
1440 mutex_unlock(&local->sta_mtx);
1445 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1446 unsigned long exp_time)
1448 struct ieee80211_local *local = sdata->local;
1449 struct sta_info *sta, *tmp;
1451 mutex_lock(&local->sta_mtx);
1453 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1454 unsigned long last_active = ieee80211_sta_last_active(sta);
1456 if (sdata != sta->sdata)
1459 if (time_is_before_jiffies(last_active + exp_time)) {
1460 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1463 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1464 test_sta_flag(sta, WLAN_STA_PS_STA))
1465 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1467 WARN_ON(__sta_info_destroy(sta));
1471 mutex_unlock(&local->sta_mtx);
1474 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1476 const u8 *localaddr)
1478 struct ieee80211_local *local = hw_to_local(hw);
1479 struct rhlist_head *tmp;
1480 struct sta_info *sta;
1483 * Just return a random station if localaddr is NULL
1484 * ... first in list.
1486 for_each_sta_info(local, addr, sta, tmp) {
1488 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1497 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1499 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1502 struct sta_info *sta;
1507 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1516 EXPORT_SYMBOL(ieee80211_find_sta);
1518 /* powersave support code */
1519 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1521 struct ieee80211_sub_if_data *sdata = sta->sdata;
1522 struct ieee80211_local *local = sdata->local;
1523 struct sk_buff_head pending;
1524 int filtered = 0, buffered = 0, ac, i;
1525 unsigned long flags;
1528 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1529 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1532 if (sdata->vif.type == NL80211_IFTYPE_AP)
1533 ps = &sdata->bss->ps;
1534 else if (ieee80211_vif_is_mesh(&sdata->vif))
1535 ps = &sdata->u.mesh.ps;
1539 clear_sta_flag(sta, WLAN_STA_SP);
1541 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1542 sta->driver_buffered_tids = 0;
1543 sta->txq_buffered_tids = 0;
1545 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1546 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1548 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1549 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1552 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1555 skb_queue_head_init(&pending);
1557 /* sync with ieee80211_tx_h_unicast_ps_buf */
1558 spin_lock(&sta->ps_lock);
1559 /* Send all buffered frames to the station */
1560 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1561 int count = skb_queue_len(&pending), tmp;
1563 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1564 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1565 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1566 tmp = skb_queue_len(&pending);
1567 filtered += tmp - count;
1570 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1571 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1572 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1573 tmp = skb_queue_len(&pending);
1574 buffered += tmp - count;
1577 ieee80211_add_pending_skbs(local, &pending);
1579 /* now we're no longer in the deliver code */
1580 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1582 /* The station might have polled and then woken up before we responded,
1583 * so clear these flags now to avoid them sticking around.
1585 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1586 clear_sta_flag(sta, WLAN_STA_UAPSD);
1587 spin_unlock(&sta->ps_lock);
1589 atomic_dec(&ps->num_sta_ps);
1591 local->total_ps_buffered -= buffered;
1593 sta_info_recalc_tim(sta);
1596 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1597 sta->sta.addr, sta->sta.aid, filtered, buffered);
1599 ieee80211_check_fast_xmit(sta);
1602 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1603 enum ieee80211_frame_release_type reason,
1604 bool call_driver, bool more_data)
1606 struct ieee80211_sub_if_data *sdata = sta->sdata;
1607 struct ieee80211_local *local = sdata->local;
1608 struct ieee80211_qos_hdr *nullfunc;
1609 struct sk_buff *skb;
1610 int size = sizeof(*nullfunc);
1612 bool qos = sta->sta.wme;
1613 struct ieee80211_tx_info *info;
1614 struct ieee80211_chanctx_conf *chanctx_conf;
1617 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1618 IEEE80211_STYPE_QOS_NULLFUNC |
1619 IEEE80211_FCTL_FROMDS);
1622 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1623 IEEE80211_STYPE_NULLFUNC |
1624 IEEE80211_FCTL_FROMDS);
1627 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1631 skb_reserve(skb, local->hw.extra_tx_headroom);
1633 nullfunc = skb_put(skb, size);
1634 nullfunc->frame_control = fc;
1635 nullfunc->duration_id = 0;
1636 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1637 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1638 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1639 nullfunc->seq_ctrl = 0;
1641 skb->priority = tid;
1642 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1644 nullfunc->qos_ctrl = cpu_to_le16(tid);
1646 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1647 nullfunc->qos_ctrl |=
1648 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1650 nullfunc->frame_control |=
1651 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1655 info = IEEE80211_SKB_CB(skb);
1658 * Tell TX path to send this frame even though the
1659 * STA may still remain is PS mode after this frame
1660 * exchange. Also set EOSP to indicate this packet
1661 * ends the poll/service period.
1663 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1664 IEEE80211_TX_STATUS_EOSP |
1665 IEEE80211_TX_CTL_REQ_TX_STATUS;
1667 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1670 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1673 skb->dev = sdata->dev;
1676 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1677 if (WARN_ON(!chanctx_conf)) {
1683 info->band = chanctx_conf->def.chan->band;
1684 ieee80211_xmit(sdata, sta, skb);
1688 static int find_highest_prio_tid(unsigned long tids)
1690 /* lower 3 TIDs aren't ordered perfectly */
1692 return fls(tids) - 1;
1693 /* TID 0 is BE just like TID 3 */
1696 return fls(tids) - 1;
1699 /* Indicates if the MORE_DATA bit should be set in the last
1700 * frame obtained by ieee80211_sta_ps_get_frames.
1701 * Note that driver_release_tids is relevant only if
1702 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1705 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1706 enum ieee80211_frame_release_type reason,
1707 unsigned long driver_release_tids)
1711 /* If the driver has data on more than one TID then
1712 * certainly there's more data if we release just a
1713 * single frame now (from a single TID). This will
1714 * only happen for PS-Poll.
1716 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1717 hweight16(driver_release_tids) > 1)
1720 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1721 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1724 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1725 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1733 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1734 enum ieee80211_frame_release_type reason,
1735 struct sk_buff_head *frames,
1736 unsigned long *driver_release_tids)
1738 struct ieee80211_sub_if_data *sdata = sta->sdata;
1739 struct ieee80211_local *local = sdata->local;
1742 /* Get response frame(s) and more data bit for the last one. */
1743 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1746 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1749 tids = ieee80211_tids_for_ac(ac);
1751 /* if we already have frames from software, then we can't also
1752 * release from hardware queues
1754 if (skb_queue_empty(frames)) {
1755 *driver_release_tids |=
1756 sta->driver_buffered_tids & tids;
1757 *driver_release_tids |= sta->txq_buffered_tids & tids;
1760 if (!*driver_release_tids) {
1761 struct sk_buff *skb;
1763 while (n_frames > 0) {
1764 skb = skb_dequeue(&sta->tx_filtered[ac]);
1767 &sta->ps_tx_buf[ac]);
1769 local->total_ps_buffered--;
1774 __skb_queue_tail(frames, skb);
1778 /* If we have more frames buffered on this AC, then abort the
1779 * loop since we can't send more data from other ACs before
1780 * the buffered frames from this.
1782 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1783 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1789 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1790 int n_frames, u8 ignored_acs,
1791 enum ieee80211_frame_release_type reason)
1793 struct ieee80211_sub_if_data *sdata = sta->sdata;
1794 struct ieee80211_local *local = sdata->local;
1795 unsigned long driver_release_tids = 0;
1796 struct sk_buff_head frames;
1799 /* Service or PS-Poll period starts */
1800 set_sta_flag(sta, WLAN_STA_SP);
1802 __skb_queue_head_init(&frames);
1804 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1805 &frames, &driver_release_tids);
1807 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1809 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1810 driver_release_tids =
1811 BIT(find_highest_prio_tid(driver_release_tids));
1813 if (skb_queue_empty(&frames) && !driver_release_tids) {
1817 * For PS-Poll, this can only happen due to a race condition
1818 * when we set the TIM bit and the station notices it, but
1819 * before it can poll for the frame we expire it.
1821 * For uAPSD, this is said in the standard (11.2.1.5 h):
1822 * At each unscheduled SP for a non-AP STA, the AP shall
1823 * attempt to transmit at least one MSDU or MMPDU, but no
1824 * more than the value specified in the Max SP Length field
1825 * in the QoS Capability element from delivery-enabled ACs,
1826 * that are destined for the non-AP STA.
1828 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1831 /* This will evaluate to 1, 3, 5 or 7. */
1832 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1833 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1837 ieee80211_send_null_response(sta, tid, reason, true, false);
1838 } else if (!driver_release_tids) {
1839 struct sk_buff_head pending;
1840 struct sk_buff *skb;
1843 bool need_null = false;
1845 skb_queue_head_init(&pending);
1847 while ((skb = __skb_dequeue(&frames))) {
1848 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1849 struct ieee80211_hdr *hdr = (void *) skb->data;
1855 * Tell TX path to send this frame even though the
1856 * STA may still remain is PS mode after this frame
1859 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1860 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1863 * Use MoreData flag to indicate whether there are
1864 * more buffered frames for this STA
1866 if (more_data || !skb_queue_empty(&frames))
1867 hdr->frame_control |=
1868 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1870 hdr->frame_control &=
1871 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1873 if (ieee80211_is_data_qos(hdr->frame_control) ||
1874 ieee80211_is_qos_nullfunc(hdr->frame_control))
1875 qoshdr = ieee80211_get_qos_ctl(hdr);
1877 tids |= BIT(skb->priority);
1879 __skb_queue_tail(&pending, skb);
1881 /* end service period after last frame or add one */
1882 if (!skb_queue_empty(&frames))
1885 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1886 /* for PS-Poll, there's only one frame */
1887 info->flags |= IEEE80211_TX_STATUS_EOSP |
1888 IEEE80211_TX_CTL_REQ_TX_STATUS;
1892 /* For uAPSD, things are a bit more complicated. If the
1893 * last frame has a QoS header (i.e. is a QoS-data or
1894 * QoS-nulldata frame) then just set the EOSP bit there
1896 * If the frame doesn't have a QoS header (which means
1897 * it should be a bufferable MMPDU) then we can't set
1898 * the EOSP bit in the QoS header; add a QoS-nulldata
1899 * frame to the list to send it after the MMPDU.
1901 * Note that this code is only in the mac80211-release
1902 * code path, we assume that the driver will not buffer
1903 * anything but QoS-data frames, or if it does, will
1904 * create the QoS-nulldata frame by itself if needed.
1906 * Cf. 802.11-2012 10.2.1.10 (c).
1909 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1911 info->flags |= IEEE80211_TX_STATUS_EOSP |
1912 IEEE80211_TX_CTL_REQ_TX_STATUS;
1914 /* The standard isn't completely clear on this
1915 * as it says the more-data bit should be set
1916 * if there are more BUs. The QoS-Null frame
1917 * we're about to send isn't buffered yet, we
1918 * only create it below, but let's pretend it
1919 * was buffered just in case some clients only
1920 * expect more-data=0 when eosp=1.
1922 hdr->frame_control |=
1923 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1930 drv_allow_buffered_frames(local, sta, tids, num,
1933 ieee80211_add_pending_skbs(local, &pending);
1936 ieee80211_send_null_response(
1937 sta, find_highest_prio_tid(tids),
1938 reason, false, false);
1940 sta_info_recalc_tim(sta);
1945 * We need to release a frame that is buffered somewhere in the
1946 * driver ... it'll have to handle that.
1947 * Note that the driver also has to check the number of frames
1948 * on the TIDs we're releasing from - if there are more than
1949 * n_frames it has to set the more-data bit (if we didn't ask
1950 * it to set it anyway due to other buffered frames); if there
1951 * are fewer than n_frames it has to make sure to adjust that
1952 * to allow the service period to end properly.
1954 drv_release_buffered_frames(local, sta, driver_release_tids,
1955 n_frames, reason, more_data);
1958 * Note that we don't recalculate the TIM bit here as it would
1959 * most likely have no effect at all unless the driver told us
1960 * that the TID(s) became empty before returning here from the
1962 * Either way, however, when the driver tells us that the TID(s)
1963 * became empty or we find that a txq became empty, we'll do the
1964 * TIM recalculation.
1967 if (!sta->sta.txq[0])
1970 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1971 if (!sta->sta.txq[tid] ||
1972 !(driver_release_tids & BIT(tid)) ||
1973 txq_has_queue(sta->sta.txq[tid]))
1976 sta_info_recalc_tim(sta);
1982 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1984 u8 ignore_for_response = sta->sta.uapsd_queues;
1987 * If all ACs are delivery-enabled then we should reply
1988 * from any of them, if only some are enabled we reply
1989 * only from the non-enabled ones.
1991 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1992 ignore_for_response = 0;
1994 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1995 IEEE80211_FRAME_RELEASE_PSPOLL);
1998 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2000 int n_frames = sta->sta.max_sp;
2001 u8 delivery_enabled = sta->sta.uapsd_queues;
2004 * If we ever grow support for TSPEC this might happen if
2005 * the TSPEC update from hostapd comes in between a trigger
2006 * frame setting WLAN_STA_UAPSD in the RX path and this
2007 * actually getting called.
2009 if (!delivery_enabled)
2012 switch (sta->sta.max_sp) {
2023 /* XXX: what is a good value? */
2028 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2029 IEEE80211_FRAME_RELEASE_UAPSD);
2032 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2033 struct ieee80211_sta *pubsta, bool block)
2035 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2037 trace_api_sta_block_awake(sta->local, pubsta, block);
2040 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2041 ieee80211_clear_fast_xmit(sta);
2045 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2048 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2049 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2050 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2051 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2052 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2053 test_sta_flag(sta, WLAN_STA_UAPSD)) {
2054 /* must be asleep in this case */
2055 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2056 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2058 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2059 ieee80211_check_fast_xmit(sta);
2062 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2064 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2066 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2067 struct ieee80211_local *local = sta->local;
2069 trace_api_eosp(local, pubsta);
2071 clear_sta_flag(sta, WLAN_STA_SP);
2073 EXPORT_SYMBOL(ieee80211_sta_eosp);
2075 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2077 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2078 enum ieee80211_frame_release_type reason;
2081 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2083 reason = IEEE80211_FRAME_RELEASE_UAPSD;
2084 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2087 ieee80211_send_null_response(sta, tid, reason, false, more_data);
2089 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2091 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2092 u8 tid, bool buffered)
2094 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2096 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2099 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2102 set_bit(tid, &sta->driver_buffered_tids);
2104 clear_bit(tid, &sta->driver_buffered_tids);
2106 sta_info_recalc_tim(sta);
2108 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2110 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2111 u32 tx_airtime, u32 rx_airtime)
2113 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2114 struct ieee80211_local *local = sta->sdata->local;
2115 u8 ac = ieee80211_ac_from_tid(tid);
2119 if (sta->local->airtime_flags & AIRTIME_USE_TX)
2120 airtime += tx_airtime;
2121 if (sta->local->airtime_flags & AIRTIME_USE_RX)
2122 airtime += rx_airtime;
2124 spin_lock_bh(&local->active_txq_lock[ac]);
2125 sta->airtime[ac].tx_airtime += tx_airtime;
2126 sta->airtime[ac].rx_airtime += rx_airtime;
2128 diff = (u32)jiffies - sta->airtime[ac].last_active;
2129 if (diff <= AIRTIME_ACTIVE_DURATION)
2130 sta->airtime[ac].deficit -= airtime;
2132 spin_unlock_bh(&local->active_txq_lock[ac]);
2134 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2136 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2138 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2139 struct ieee80211_link_sta *link_sta;
2143 if (!pubsta->valid_links || !pubsta->mlo) {
2144 pubsta->cur = &pubsta->deflink.agg;
2149 for_each_sta_active_link(&sta->sdata->vif, pubsta, link_sta, link_id) {
2151 sta->cur = pubsta->deflink.agg;
2156 sta->cur.max_amsdu_len =
2157 min(sta->cur.max_amsdu_len,
2158 link_sta->agg.max_amsdu_len);
2159 sta->cur.max_rc_amsdu_len =
2160 min(sta->cur.max_rc_amsdu_len,
2161 link_sta->agg.max_rc_amsdu_len);
2163 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2164 sta->cur.max_tid_amsdu_len[i] =
2165 min(sta->cur.max_tid_amsdu_len[i],
2166 link_sta->agg.max_tid_amsdu_len[i]);
2170 pubsta->cur = &sta->cur;
2172 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2174 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2175 struct sta_info *sta, u8 ac,
2176 u16 tx_airtime, bool tx_completed)
2180 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2183 if (!tx_completed) {
2185 atomic_add(tx_airtime,
2186 &sta->airtime[ac].aql_tx_pending);
2188 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2189 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2194 tx_pending = atomic_sub_return(tx_airtime,
2195 &sta->airtime[ac].aql_tx_pending);
2197 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2201 atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2202 tx_pending = atomic_sub_return(tx_airtime,
2203 &local->aql_ac_pending_airtime[ac]);
2204 if (WARN_ONCE(tx_pending < 0,
2205 "Device %s AC %d pending airtime underflow: %u, %u",
2206 wiphy_name(local->hw.wiphy), ac, tx_pending,
2208 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2210 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2214 int sta_info_move_state(struct sta_info *sta,
2215 enum ieee80211_sta_state new_state)
2219 if (sta->sta_state == new_state)
2222 /* check allowed transitions first */
2224 switch (new_state) {
2225 case IEEE80211_STA_NONE:
2226 if (sta->sta_state != IEEE80211_STA_AUTH)
2229 case IEEE80211_STA_AUTH:
2230 if (sta->sta_state != IEEE80211_STA_NONE &&
2231 sta->sta_state != IEEE80211_STA_ASSOC)
2234 case IEEE80211_STA_ASSOC:
2235 if (sta->sta_state != IEEE80211_STA_AUTH &&
2236 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2239 case IEEE80211_STA_AUTHORIZED:
2240 if (sta->sta_state != IEEE80211_STA_ASSOC)
2244 WARN(1, "invalid state %d", new_state);
2248 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2249 sta->sta.addr, new_state);
2252 * notify the driver before the actual changes so it can
2253 * fail the transition
2255 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2256 int err = drv_sta_state(sta->local, sta->sdata, sta,
2257 sta->sta_state, new_state);
2262 /* reflect the change in all state variables */
2264 switch (new_state) {
2265 case IEEE80211_STA_NONE:
2266 if (sta->sta_state == IEEE80211_STA_AUTH)
2267 clear_bit(WLAN_STA_AUTH, &sta->_flags);
2269 case IEEE80211_STA_AUTH:
2270 if (sta->sta_state == IEEE80211_STA_NONE) {
2271 set_bit(WLAN_STA_AUTH, &sta->_flags);
2272 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2273 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2274 ieee80211_recalc_min_chandef(sta->sdata, -1);
2275 if (!sta->sta.support_p2p_ps)
2276 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2279 case IEEE80211_STA_ASSOC:
2280 if (sta->sta_state == IEEE80211_STA_AUTH) {
2281 set_bit(WLAN_STA_ASSOC, &sta->_flags);
2282 sta->assoc_at = ktime_get_boottime_ns();
2283 ieee80211_recalc_min_chandef(sta->sdata, -1);
2284 if (!sta->sta.support_p2p_ps)
2285 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2286 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2287 ieee80211_vif_dec_num_mcast(sta->sdata);
2288 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2289 ieee80211_clear_fast_xmit(sta);
2290 ieee80211_clear_fast_rx(sta);
2293 case IEEE80211_STA_AUTHORIZED:
2294 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2295 ieee80211_vif_inc_num_mcast(sta->sdata);
2296 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2297 ieee80211_check_fast_xmit(sta);
2298 ieee80211_check_fast_rx(sta);
2300 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2301 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2302 cfg80211_send_layer2_update(sta->sdata->dev,
2309 sta->sta_state = new_state;
2314 static struct ieee80211_sta_rx_stats *
2315 sta_get_last_rx_stats(struct sta_info *sta)
2317 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2320 if (!sta->deflink.pcpu_rx_stats)
2323 for_each_possible_cpu(cpu) {
2324 struct ieee80211_sta_rx_stats *cpustats;
2326 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2328 if (time_after(cpustats->last_rx, stats->last_rx))
2335 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2336 struct rate_info *rinfo)
2338 rinfo->bw = STA_STATS_GET(BW, rate);
2340 switch (STA_STATS_GET(TYPE, rate)) {
2341 case STA_STATS_RATE_TYPE_VHT:
2342 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2343 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2344 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2345 if (STA_STATS_GET(SGI, rate))
2346 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2348 case STA_STATS_RATE_TYPE_HT:
2349 rinfo->flags = RATE_INFO_FLAGS_MCS;
2350 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2351 if (STA_STATS_GET(SGI, rate))
2352 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2354 case STA_STATS_RATE_TYPE_LEGACY: {
2355 struct ieee80211_supported_band *sband;
2358 int band = STA_STATS_GET(LEGACY_BAND, rate);
2359 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2361 sband = local->hw.wiphy->bands[band];
2363 if (WARN_ON_ONCE(!sband->bitrates))
2366 brate = sband->bitrates[rate_idx].bitrate;
2367 if (rinfo->bw == RATE_INFO_BW_5)
2369 else if (rinfo->bw == RATE_INFO_BW_10)
2373 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2376 case STA_STATS_RATE_TYPE_HE:
2377 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2378 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2379 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2380 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2381 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2382 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2387 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2389 u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2391 if (rate == STA_STATS_RATE_INVALID)
2394 sta_stats_decode_rate(sta->local, rate, rinfo);
2398 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2405 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2406 value = rxstats->msdu[tid];
2407 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2412 static void sta_set_tidstats(struct sta_info *sta,
2413 struct cfg80211_tid_stats *tidstats,
2416 struct ieee80211_local *local = sta->local;
2419 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2420 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2423 if (sta->deflink.pcpu_rx_stats) {
2424 for_each_possible_cpu(cpu) {
2425 struct ieee80211_sta_rx_stats *cpurxs;
2427 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2429 tidstats->rx_msdu +=
2430 sta_get_tidstats_msdu(cpurxs, tid);
2434 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2437 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2438 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2439 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2442 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2443 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2444 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2445 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2448 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2449 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2450 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2451 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2454 if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2455 spin_lock_bh(&local->fq.lock);
2458 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2459 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2460 to_txq_info(sta->sta.txq[tid]));
2463 spin_unlock_bh(&local->fq.lock);
2467 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2473 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2474 value = rxstats->bytes;
2475 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2480 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2483 struct ieee80211_sub_if_data *sdata = sta->sdata;
2484 struct ieee80211_local *local = sdata->local;
2487 struct ieee80211_sta_rx_stats *last_rxstats;
2489 last_rxstats = sta_get_last_rx_stats(sta);
2491 sinfo->generation = sdata->local->sta_generation;
2493 /* do before driver, so beacon filtering drivers have a
2494 * chance to e.g. just add the number of filtered beacons
2495 * (or just modify the value entirely, of course)
2497 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2498 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2500 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2501 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2502 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2503 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2504 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2505 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2506 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2508 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2509 sinfo->beacon_loss_count =
2510 sdata->deflink.u.mgd.beacon_loss_count;
2511 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2514 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2515 sinfo->assoc_at = sta->assoc_at;
2516 sinfo->inactive_time =
2517 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2519 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2520 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2521 sinfo->tx_bytes = 0;
2522 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2523 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2524 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2527 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2528 sinfo->tx_packets = 0;
2529 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2530 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2531 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2534 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2535 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2536 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2538 if (sta->deflink.pcpu_rx_stats) {
2539 for_each_possible_cpu(cpu) {
2540 struct ieee80211_sta_rx_stats *cpurxs;
2542 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2544 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2548 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2551 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2552 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2553 if (sta->deflink.pcpu_rx_stats) {
2554 for_each_possible_cpu(cpu) {
2555 struct ieee80211_sta_rx_stats *cpurxs;
2557 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2559 sinfo->rx_packets += cpurxs->packets;
2562 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2565 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2566 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2567 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2570 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2571 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2572 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2575 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2576 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2577 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2578 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2581 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2582 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2583 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2584 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2587 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2588 sinfo->airtime_weight = sta->airtime_weight;
2589 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2592 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2593 if (sta->deflink.pcpu_rx_stats) {
2594 for_each_possible_cpu(cpu) {
2595 struct ieee80211_sta_rx_stats *cpurxs;
2597 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2598 sinfo->rx_dropped_misc += cpurxs->dropped;
2602 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2603 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2604 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2605 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2606 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2609 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2610 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2611 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2612 sinfo->signal = (s8)last_rxstats->last_signal;
2613 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2616 if (!sta->deflink.pcpu_rx_stats &&
2617 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2619 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2620 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2624 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2625 * the sta->rx_stats struct, so the check here is fine with and without
2628 if (last_rxstats->chains &&
2629 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2630 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2631 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2632 if (!sta->deflink.pcpu_rx_stats)
2633 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2635 sinfo->chains = last_rxstats->chains;
2637 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2638 sinfo->chain_signal[i] =
2639 last_rxstats->chain_signal_last[i];
2640 sinfo->chain_signal_avg[i] =
2641 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2645 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2646 !sta->sta.valid_links) {
2647 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2649 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2652 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2653 !sta->sta.valid_links) {
2654 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2655 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2658 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2659 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2660 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2663 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2664 #ifdef CONFIG_MAC80211_MESH
2665 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2666 BIT_ULL(NL80211_STA_INFO_PLID) |
2667 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2668 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2669 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2670 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2671 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2672 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2674 sinfo->llid = sta->mesh->llid;
2675 sinfo->plid = sta->mesh->plid;
2676 sinfo->plink_state = sta->mesh->plink_state;
2677 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2678 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2679 sinfo->t_offset = sta->mesh->t_offset;
2681 sinfo->local_pm = sta->mesh->local_pm;
2682 sinfo->peer_pm = sta->mesh->peer_pm;
2683 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2684 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2685 sinfo->connected_to_as = sta->mesh->connected_to_as;
2689 sinfo->bss_param.flags = 0;
2690 if (sdata->vif.bss_conf.use_cts_prot)
2691 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2692 if (sdata->vif.bss_conf.use_short_preamble)
2693 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2694 if (sdata->vif.bss_conf.use_short_slot)
2695 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2696 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2697 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2699 sinfo->sta_flags.set = 0;
2700 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2701 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2702 BIT(NL80211_STA_FLAG_WME) |
2703 BIT(NL80211_STA_FLAG_MFP) |
2704 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2705 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2706 BIT(NL80211_STA_FLAG_TDLS_PEER);
2707 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2708 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2709 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2710 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2712 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2713 if (test_sta_flag(sta, WLAN_STA_MFP))
2714 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2715 if (test_sta_flag(sta, WLAN_STA_AUTH))
2716 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2717 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2718 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2719 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2720 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2722 thr = sta_get_expected_throughput(sta);
2725 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2726 sinfo->expected_throughput = thr;
2729 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2730 sta->deflink.status_stats.ack_signal_filled) {
2731 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2732 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2735 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2736 sta->deflink.status_stats.ack_signal_filled) {
2737 sinfo->avg_ack_signal =
2738 -(s8)ewma_avg_signal_read(
2739 &sta->deflink.status_stats.avg_ack_signal);
2741 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2744 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2745 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2746 sinfo->airtime_link_metric =
2747 airtime_link_metric_get(local, sta);
2751 u32 sta_get_expected_throughput(struct sta_info *sta)
2753 struct ieee80211_sub_if_data *sdata = sta->sdata;
2754 struct ieee80211_local *local = sdata->local;
2755 struct rate_control_ref *ref = NULL;
2758 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2759 ref = local->rate_ctrl;
2761 /* check if the driver has a SW RC implementation */
2762 if (ref && ref->ops->get_expected_throughput)
2763 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2765 thr = drv_get_expected_throughput(local, sta);
2770 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2772 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2774 if (!sta->deflink.status_stats.last_ack ||
2775 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2776 return stats->last_rx;
2777 return sta->deflink.status_stats.last_ack;
2780 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2782 if (!sta->sdata->local->ops->wake_tx_queue)
2785 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2786 sta->cparams.target = MS2TIME(50);
2787 sta->cparams.interval = MS2TIME(300);
2788 sta->cparams.ecn = false;
2790 sta->cparams.target = MS2TIME(20);
2791 sta->cparams.interval = MS2TIME(100);
2792 sta->cparams.ecn = true;
2796 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2799 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2801 sta_update_codel_params(sta, thr);
2804 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2806 struct ieee80211_sub_if_data *sdata = sta->sdata;
2807 struct sta_link_alloc *alloc;
2810 lockdep_assert_held(&sdata->local->sta_mtx);
2812 /* must represent an MLD from the start */
2813 if (WARN_ON(!sta->sta.valid_links))
2816 if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2817 sta->link[link_id]))
2820 alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2824 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2830 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2835 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2837 lockdep_assert_held(&sta->sdata->local->sta_mtx);
2839 sta_remove_link(sta, link_id, false);
2842 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2844 struct ieee80211_sub_if_data *sdata = sta->sdata;
2845 struct link_sta_info *link_sta;
2846 u16 old_links = sta->sta.valid_links;
2847 u16 new_links = old_links | BIT(link_id);
2850 link_sta = rcu_dereference_protected(sta->link[link_id],
2851 lockdep_is_held(&sdata->local->sta_mtx));
2853 if (WARN_ON(old_links == new_links || !link_sta))
2857 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2861 /* we only modify under the mutex so this is fine */
2864 sta->sta.valid_links = new_links;
2866 if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2869 ieee80211_recalc_min_chandef(sdata, link_id);
2871 /* Ensure the values are updated for the driver,
2872 * redone by sta_remove_link on failure.
2874 ieee80211_sta_recalc_aggregates(&sta->sta);
2876 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2877 old_links, new_links);
2879 sta->sta.valid_links = old_links;
2880 sta_remove_link(sta, link_id, false);
2885 ret = link_sta_info_hash_add(sdata->local, link_sta);
2890 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2892 struct ieee80211_sub_if_data *sdata = sta->sdata;
2893 u16 old_links = sta->sta.valid_links;
2895 lockdep_assert_held(&sdata->local->sta_mtx);
2897 sta->sta.valid_links &= ~BIT(link_id);
2899 if (test_sta_flag(sta, WLAN_STA_INSERTED))
2900 drv_change_sta_links(sdata->local, sdata, &sta->sta,
2901 old_links, sta->sta.valid_links);
2903 sta_remove_link(sta, link_id, true);
2906 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2907 const u8 *ext_capab,
2908 unsigned int ext_capab_len)
2912 sta->sta.max_amsdu_subframes = 0;
2914 if (ext_capab_len < 8)
2917 /* The sender might not have sent the last bit, consider it to be 0 */
2918 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2920 /* we did get all the bits, take the MSB as well */
2921 if (ext_capab_len >= 9)
2922 val |= u8_get_bits(ext_capab[8],
2923 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2926 sta->sta.max_amsdu_subframes = 4 << (4 - val);
2929 #ifdef CONFIG_LOCKDEP
2930 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
2932 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2934 return lockdep_is_held(&sta->local->sta_mtx);
2936 EXPORT_SYMBOL(lockdep_sta_mutex_held);