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 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
144 struct txq_info *txqi;
146 if (!sta->sta.txq[i])
149 txqi = to_txq_info(sta->sta.txq[i]);
151 ieee80211_txq_purge(local, txqi);
154 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
155 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
156 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
157 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
160 if (ieee80211_vif_is_mesh(&sdata->vif))
161 mesh_sta_cleanup(sta);
163 cancel_work_sync(&sta->drv_deliver_wk);
166 * Destroy aggregation state here. It would be nice to wait for the
167 * driver to finish aggregation stop and then clean up, but for now
168 * drivers have to handle aggregation stop being requested, followed
169 * directly by station destruction.
171 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
172 kfree(sta->ampdu_mlme.tid_start_tx[i]);
173 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
176 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
181 static void cleanup_single_sta(struct sta_info *sta)
183 struct ieee80211_sub_if_data *sdata = sta->sdata;
184 struct ieee80211_local *local = sdata->local;
186 __cleanup_single_sta(sta);
187 sta_info_free(local, sta);
190 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
193 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
196 /* protected by RCU */
197 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
200 struct ieee80211_local *local = sdata->local;
201 struct rhlist_head *tmp;
202 struct sta_info *sta;
205 for_each_sta_info(local, addr, sta, tmp) {
206 if (sta->sdata == sdata) {
208 /* this is safe as the caller must already hold
209 * another rcu read section or the mutex
219 * Get sta info either from the specified interface
220 * or from one of its vlans
222 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
225 struct ieee80211_local *local = sdata->local;
226 struct rhlist_head *tmp;
227 struct sta_info *sta;
230 for_each_sta_info(local, addr, sta, tmp) {
231 if (sta->sdata == sdata ||
232 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
234 /* this is safe as the caller must already hold
235 * another rcu read section or the mutex
244 struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
247 return rhltable_lookup(&local->link_sta_hash, addr,
248 link_sta_rht_params);
251 struct link_sta_info *
252 link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
254 struct ieee80211_local *local = sdata->local;
255 struct rhlist_head *tmp;
256 struct link_sta_info *link_sta;
259 for_each_link_sta_info(local, addr, link_sta, tmp) {
260 struct sta_info *sta = link_sta->sta;
262 if (sta->sdata == sdata ||
263 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
265 /* this is safe as the caller must already hold
266 * another rcu read section or the mutex
275 struct ieee80211_sta *
276 ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
279 unsigned int *link_id)
281 struct ieee80211_local *local = hw_to_local(hw);
282 struct link_sta_info *link_sta;
283 struct rhlist_head *tmp;
285 for_each_link_sta_info(local, addr, link_sta, tmp) {
286 struct sta_info *sta = link_sta->sta;
287 struct ieee80211_link_data *link;
288 u8 _link_id = link_sta->link_id;
296 link = rcu_dereference(sta->sdata->link[_link_id]);
300 if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
310 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
312 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
313 const u8 *sta_addr, const u8 *vif_addr)
315 struct rhlist_head *tmp;
316 struct sta_info *sta;
318 for_each_sta_info(local, sta_addr, sta, tmp) {
319 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
326 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
329 struct ieee80211_local *local = sdata->local;
330 struct sta_info *sta;
333 list_for_each_entry_rcu(sta, &local->sta_list, list,
334 lockdep_is_held(&local->sta_mtx)) {
335 if (sdata != sta->sdata)
347 static void sta_info_free_link(struct link_sta_info *link_sta)
349 free_percpu(link_sta->pcpu_rx_stats);
352 static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
355 struct sta_link_alloc *alloc = NULL;
356 struct link_sta_info *link_sta;
358 link_sta = rcu_dereference_protected(sta->link[link_id],
359 lockdep_is_held(&sta->local->sta_mtx));
361 if (WARN_ON(!link_sta))
365 link_sta_info_hash_del(sta->local, link_sta);
367 if (test_sta_flag(sta, WLAN_STA_INSERTED))
368 ieee80211_link_sta_debugfs_remove(link_sta);
370 if (link_sta != &sta->deflink)
371 alloc = container_of(link_sta, typeof(*alloc), info);
373 sta->sta.valid_links &= ~BIT(link_id);
374 RCU_INIT_POINTER(sta->link[link_id], NULL);
375 RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
377 sta_info_free_link(&alloc->info);
378 kfree_rcu(alloc, rcu_head);
381 ieee80211_sta_recalc_aggregates(&sta->sta);
385 * sta_info_free - free STA
387 * @local: pointer to the global information
388 * @sta: STA info to free
390 * This function must undo everything done by sta_info_alloc()
391 * that may happen before sta_info_insert(). It may only be
392 * called when sta_info_insert() has not been attempted (and
393 * if that fails, the station is freed anyway.)
395 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
399 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
400 if (!(sta->sta.valid_links & BIT(i)))
403 sta_remove_link(sta, i, false);
407 * If we had used sta_info_pre_move_state() then we might not
408 * have gone through the state transitions down again, so do
409 * it here now (and warn if it's inserted).
411 * This will clear state such as fast TX/RX that may have been
412 * allocated during state transitions.
414 while (sta->sta_state > IEEE80211_STA_NONE) {
417 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
419 ret = sta_info_move_state(sta, sta->sta_state - 1);
420 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
425 rate_control_free_sta(sta);
427 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
429 kfree(to_txq_info(sta->sta.txq[0]));
430 kfree(rcu_dereference_raw(sta->sta.rates));
431 #ifdef CONFIG_MAC80211_MESH
435 sta_info_free_link(&sta->deflink);
439 /* Caller must hold local->sta_mtx */
440 static int sta_info_hash_add(struct ieee80211_local *local,
441 struct sta_info *sta)
443 return rhltable_insert(&local->sta_hash, &sta->hash_node,
447 static void sta_deliver_ps_frames(struct work_struct *wk)
449 struct sta_info *sta;
451 sta = container_of(wk, struct sta_info, drv_deliver_wk);
457 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
458 ieee80211_sta_ps_deliver_wakeup(sta);
459 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
460 ieee80211_sta_ps_deliver_poll_response(sta);
461 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
462 ieee80211_sta_ps_deliver_uapsd(sta);
466 static int sta_prepare_rate_control(struct ieee80211_local *local,
467 struct sta_info *sta, gfp_t gfp)
469 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
472 sta->rate_ctrl = local->rate_ctrl;
473 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
475 if (!sta->rate_ctrl_priv)
481 static int sta_info_alloc_link(struct ieee80211_local *local,
482 struct link_sta_info *link_info,
485 struct ieee80211_hw *hw = &local->hw;
488 if (ieee80211_hw_check(hw, USES_RSS)) {
489 link_info->pcpu_rx_stats =
490 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
491 if (!link_info->pcpu_rx_stats)
495 link_info->rx_stats.last_rx = jiffies;
496 u64_stats_init(&link_info->rx_stats.syncp);
498 ewma_signal_init(&link_info->rx_stats_avg.signal);
499 ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
500 for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
501 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
506 static void sta_info_add_link(struct sta_info *sta,
507 unsigned int link_id,
508 struct link_sta_info *link_info,
509 struct ieee80211_link_sta *link_sta)
511 link_info->sta = sta;
512 link_info->link_id = link_id;
513 link_info->pub = link_sta;
514 link_info->pub->sta = &sta->sta;
515 link_sta->link_id = link_id;
516 rcu_assign_pointer(sta->link[link_id], link_info);
517 rcu_assign_pointer(sta->sta.link[link_id], link_sta);
519 link_sta->smps_mode = IEEE80211_SMPS_OFF;
520 link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
523 static struct sta_info *
524 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
525 const u8 *addr, int link_id, const u8 *link_addr,
528 struct ieee80211_local *local = sdata->local;
529 struct ieee80211_hw *hw = &local->hw;
530 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 size = sizeof(struct txq_info) +
603 ALIGN(hw->txq_data_size, sizeof(void *));
605 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
609 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
610 struct txq_info *txq = txq_data + i * size;
612 /* might not do anything for the (bufferable) MMPDU TXQ */
613 ieee80211_txq_init(sdata, sta, txq, i);
616 if (sta_prepare_rate_control(local, sta, gfp))
619 sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
621 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
622 skb_queue_head_init(&sta->ps_tx_buf[i]);
623 skb_queue_head_init(&sta->tx_filtered[i]);
624 sta->airtime[i].deficit = sta->airtime_weight;
625 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
626 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
627 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
630 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
631 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
633 for (i = 0; i < NUM_NL80211_BANDS; i++) {
637 if (!hw->wiphy->bands[i])
641 case NL80211_BAND_2GHZ:
642 case NL80211_BAND_LC:
644 * We use both here, even if we cannot really know for
645 * sure the station will support both, but the only use
646 * for this is when we don't know anything yet and send
647 * management frames, and then we'll pick the lowest
648 * possible rate anyway.
649 * If we don't include _G here, we cannot find a rate
650 * in P2P, and thus trigger the WARN_ONCE() in rate.c
652 mandatory = IEEE80211_RATE_MANDATORY_B |
653 IEEE80211_RATE_MANDATORY_G;
655 case NL80211_BAND_5GHZ:
656 mandatory = IEEE80211_RATE_MANDATORY_A;
658 case NL80211_BAND_60GHZ:
664 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
665 struct ieee80211_rate *rate;
667 rate = &hw->wiphy->bands[i]->bitrates[r];
669 if (!(rate->flags & mandatory))
671 sta->sta.deflink.supp_rates[i] |= BIT(r);
675 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
676 sta->cparams.target = MS2TIME(20);
677 sta->cparams.interval = MS2TIME(100);
678 sta->cparams.ecn = true;
679 sta->cparams.ce_threshold_selector = 0;
680 sta->cparams.ce_threshold_mask = 0;
682 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
687 kfree(to_txq_info(sta->sta.txq[0]));
689 sta_info_free_link(&sta->deflink);
690 #ifdef CONFIG_MAC80211_MESH
697 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
698 const u8 *addr, gfp_t gfp)
700 return __sta_info_alloc(sdata, addr, -1, addr, gfp);
703 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
705 unsigned int link_id,
709 return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
712 static int sta_info_insert_check(struct sta_info *sta)
714 struct ieee80211_sub_if_data *sdata = sta->sdata;
717 * Can't be a WARN_ON because it can be triggered through a race:
718 * something inserts a STA (on one CPU) without holding the RTNL
719 * and another CPU turns off the net device.
721 if (unlikely(!ieee80211_sdata_running(sdata)))
724 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
725 !is_valid_ether_addr(sta->sta.addr)))
728 /* The RCU read lock is required by rhashtable due to
729 * asynchronous resize/rehash. We also require the mutex
733 lockdep_assert_held(&sdata->local->sta_mtx);
734 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
735 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
744 static int sta_info_insert_drv_state(struct ieee80211_local *local,
745 struct ieee80211_sub_if_data *sdata,
746 struct sta_info *sta)
748 enum ieee80211_sta_state state;
751 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
752 err = drv_sta_state(local, sdata, sta, state, state + 1);
759 * Drivers using legacy sta_add/sta_remove callbacks only
760 * get uploaded set to true after sta_add is called.
762 if (!local->ops->sta_add)
763 sta->uploaded = true;
767 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
769 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
770 sta->sta.addr, state + 1, err);
774 /* unwind on error */
775 for (; state > IEEE80211_STA_NOTEXIST; state--)
776 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
782 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
784 struct ieee80211_local *local = sdata->local;
785 bool allow_p2p_go_ps = sdata->vif.p2p;
786 struct sta_info *sta;
789 list_for_each_entry_rcu(sta, &local->sta_list, list) {
790 if (sdata != sta->sdata ||
791 !test_sta_flag(sta, WLAN_STA_ASSOC))
793 if (!sta->sta.support_p2p_ps) {
794 allow_p2p_go_ps = false;
800 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
801 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
802 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
808 * should be called with sta_mtx locked
809 * this function replaces the mutex lock
812 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
814 struct ieee80211_local *local = sta->local;
815 struct ieee80211_sub_if_data *sdata = sta->sdata;
816 struct station_info *sinfo = NULL;
819 lockdep_assert_held(&local->sta_mtx);
821 /* check if STA exists already */
822 if (sta_info_get_bss(sdata, sta->sta.addr)) {
827 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
834 local->sta_generation++;
837 /* simplify things and don't accept BA sessions yet */
838 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
840 /* make the station visible */
841 err = sta_info_hash_add(local, sta);
845 if (sta->sta.valid_links) {
846 err = link_sta_info_hash_add(local, &sta->deflink);
848 sta_info_hash_del(local, sta);
853 list_add_tail_rcu(&sta->list, &local->sta_list);
855 /* update channel context before notifying the driver about state
856 * change, this enables driver using the updated channel context right away.
858 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
859 ieee80211_recalc_min_chandef(sta->sdata, -1);
860 if (!sta->sta.support_p2p_ps)
861 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
865 err = sta_info_insert_drv_state(local, sdata, sta);
869 set_sta_flag(sta, WLAN_STA_INSERTED);
871 /* accept BA sessions now */
872 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
874 ieee80211_sta_debugfs_add(sta);
875 rate_control_add_sta_debugfs(sta);
876 if (sta->sta.valid_links) {
879 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
880 struct link_sta_info *link_sta;
882 link_sta = rcu_dereference_protected(sta->link[i],
883 lockdep_is_held(&local->sta_mtx));
888 ieee80211_link_sta_debugfs_add(link_sta);
889 if (sdata->vif.active_links & BIT(i))
890 ieee80211_link_sta_debugfs_drv_add(link_sta);
893 ieee80211_link_sta_debugfs_add(&sta->deflink);
894 ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
897 sinfo->generation = local->sta_generation;
898 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
901 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
903 /* move reference to rcu-protected */
905 mutex_unlock(&local->sta_mtx);
907 if (ieee80211_vif_is_mesh(&sdata->vif))
908 mesh_accept_plinks_update(sdata);
912 if (sta->sta.valid_links)
913 link_sta_info_hash_del(local, &sta->deflink);
914 sta_info_hash_del(local, sta);
915 list_del_rcu(&sta->list);
920 cleanup_single_sta(sta);
921 mutex_unlock(&local->sta_mtx);
927 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
929 struct ieee80211_local *local = sta->local;
934 mutex_lock(&local->sta_mtx);
936 err = sta_info_insert_check(sta);
938 sta_info_free(local, sta);
939 mutex_unlock(&local->sta_mtx);
944 return sta_info_insert_finish(sta);
947 int sta_info_insert(struct sta_info *sta)
949 int err = sta_info_insert_rcu(sta);
956 static inline void __bss_tim_set(u8 *tim, u16 id)
959 * This format has been mandated by the IEEE specifications,
960 * so this line may not be changed to use the __set_bit() format.
962 tim[id / 8] |= (1 << (id % 8));
965 static inline void __bss_tim_clear(u8 *tim, u16 id)
968 * This format has been mandated by the IEEE specifications,
969 * so this line may not be changed to use the __clear_bit() format.
971 tim[id / 8] &= ~(1 << (id % 8));
974 static inline bool __bss_tim_get(u8 *tim, u16 id)
977 * This format has been mandated by the IEEE specifications,
978 * so this line may not be changed to use the test_bit() format.
980 return tim[id / 8] & (1 << (id % 8));
983 static unsigned long ieee80211_tids_for_ac(int ac)
985 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
987 case IEEE80211_AC_VO:
988 return BIT(6) | BIT(7);
989 case IEEE80211_AC_VI:
990 return BIT(4) | BIT(5);
991 case IEEE80211_AC_BE:
992 return BIT(0) | BIT(3);
993 case IEEE80211_AC_BK:
994 return BIT(1) | BIT(2);
1001 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1003 struct ieee80211_local *local = sta->local;
1005 bool indicate_tim = false;
1006 u8 ignore_for_tim = sta->sta.uapsd_queues;
1008 u16 id = sta->sta.aid;
1010 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1011 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1012 if (WARN_ON_ONCE(!sta->sdata->bss))
1015 ps = &sta->sdata->bss->ps;
1016 #ifdef CONFIG_MAC80211_MESH
1017 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1018 ps = &sta->sdata->u.mesh.ps;
1024 /* No need to do anything if the driver does all */
1025 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1032 * If all ACs are delivery-enabled then we should build
1033 * the TIM bit for all ACs anyway; if only some are then
1034 * we ignore those and build the TIM bit using only the
1037 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1041 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1043 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1046 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1049 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1050 !skb_queue_empty(&sta->ps_tx_buf[ac]);
1054 tids = ieee80211_tids_for_ac(ac);
1057 sta->driver_buffered_tids & tids;
1059 sta->txq_buffered_tids & tids;
1063 spin_lock_bh(&local->tim_lock);
1065 if (indicate_tim == __bss_tim_get(ps->tim, id))
1069 __bss_tim_set(ps->tim, id);
1071 __bss_tim_clear(ps->tim, id);
1073 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1074 local->tim_in_locked_section = true;
1075 drv_set_tim(local, &sta->sta, indicate_tim);
1076 local->tim_in_locked_section = false;
1080 spin_unlock_bh(&local->tim_lock);
1083 void sta_info_recalc_tim(struct sta_info *sta)
1085 __sta_info_recalc_tim(sta, false);
1088 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1090 struct ieee80211_tx_info *info;
1096 info = IEEE80211_SKB_CB(skb);
1098 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1099 timeout = (sta->listen_interval *
1100 sta->sdata->vif.bss_conf.beacon_int *
1102 if (timeout < STA_TX_BUFFER_EXPIRE)
1103 timeout = STA_TX_BUFFER_EXPIRE;
1104 return time_after(jiffies, info->control.jiffies + timeout);
1108 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1109 struct sta_info *sta, int ac)
1111 unsigned long flags;
1112 struct sk_buff *skb;
1115 * First check for frames that should expire on the filtered
1116 * queue. Frames here were rejected by the driver and are on
1117 * a separate queue to avoid reordering with normal PS-buffered
1118 * frames. They also aren't accounted for right now in the
1119 * total_ps_buffered counter.
1122 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1123 skb = skb_peek(&sta->tx_filtered[ac]);
1124 if (sta_info_buffer_expired(sta, skb))
1125 skb = __skb_dequeue(&sta->tx_filtered[ac]);
1128 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1131 * Frames are queued in order, so if this one
1132 * hasn't expired yet we can stop testing. If
1133 * we actually reached the end of the queue we
1134 * also need to stop, of course.
1138 ieee80211_free_txskb(&local->hw, skb);
1142 * Now also check the normal PS-buffered queue, this will
1143 * only find something if the filtered queue was emptied
1144 * since the filtered frames are all before the normal PS
1148 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1149 skb = skb_peek(&sta->ps_tx_buf[ac]);
1150 if (sta_info_buffer_expired(sta, skb))
1151 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1154 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1157 * frames are queued in order, so if this one
1158 * hasn't expired yet (or we reached the end of
1159 * the queue) we can stop testing
1164 local->total_ps_buffered--;
1165 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1167 ieee80211_free_txskb(&local->hw, skb);
1171 * Finally, recalculate the TIM bit for this station -- it might
1172 * now be clear because the station was too slow to retrieve its
1175 sta_info_recalc_tim(sta);
1178 * Return whether there are any frames still buffered, this is
1179 * used to check whether the cleanup timer still needs to run,
1180 * if there are no frames we don't need to rearm the timer.
1182 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1183 skb_queue_empty(&sta->tx_filtered[ac]));
1186 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1187 struct sta_info *sta)
1189 bool have_buffered = false;
1192 /* This is only necessary for stations on BSS/MBSS interfaces */
1193 if (!sta->sdata->bss &&
1194 !ieee80211_vif_is_mesh(&sta->sdata->vif))
1197 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1199 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1201 return have_buffered;
1204 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1206 struct ieee80211_local *local;
1207 struct ieee80211_sub_if_data *sdata;
1218 lockdep_assert_held(&local->sta_mtx);
1221 * Before removing the station from the driver and
1222 * rate control, it might still start new aggregation
1223 * sessions -- block that to make sure the tear-down
1224 * will be sufficient.
1226 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1227 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1230 * Before removing the station from the driver there might be pending
1231 * rx frames on RSS queues sent prior to the disassociation - wait for
1232 * all such frames to be processed.
1234 drv_sync_rx_queues(local, sta);
1236 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1237 struct link_sta_info *link_sta;
1239 if (!(sta->sta.valid_links & BIT(i)))
1242 link_sta = rcu_dereference_protected(sta->link[i],
1243 lockdep_is_held(&local->sta_mtx));
1245 link_sta_info_hash_del(local, link_sta);
1248 ret = sta_info_hash_del(local, sta);
1253 * for TDLS peers, make sure to return to the base channel before
1256 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1257 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1258 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1261 list_del_rcu(&sta->list);
1262 sta->removed = true;
1264 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1266 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1267 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1268 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1273 static void __sta_info_destroy_part2(struct sta_info *sta)
1275 struct ieee80211_local *local = sta->local;
1276 struct ieee80211_sub_if_data *sdata = sta->sdata;
1277 struct station_info *sinfo;
1281 * NOTE: This assumes at least synchronize_net() was done
1282 * after _part1 and before _part2!
1286 lockdep_assert_held(&local->sta_mtx);
1288 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1289 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1293 /* now keys can no longer be reached */
1294 ieee80211_free_sta_keys(local, sta);
1296 /* disable TIM bit - last chance to tell driver */
1297 __sta_info_recalc_tim(sta, true);
1302 local->sta_generation++;
1304 while (sta->sta_state > IEEE80211_STA_NONE) {
1305 ret = sta_info_move_state(sta, sta->sta_state - 1);
1312 if (sta->uploaded) {
1313 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1314 IEEE80211_STA_NOTEXIST);
1315 WARN_ON_ONCE(ret != 0);
1318 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1320 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1322 sta_set_sinfo(sta, sinfo, true);
1323 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1326 ieee80211_sta_debugfs_remove(sta);
1328 ieee80211_destroy_frag_cache(&sta->frags);
1330 cleanup_single_sta(sta);
1333 int __must_check __sta_info_destroy(struct sta_info *sta)
1335 int err = __sta_info_destroy_part1(sta);
1342 __sta_info_destroy_part2(sta);
1347 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1349 struct sta_info *sta;
1352 mutex_lock(&sdata->local->sta_mtx);
1353 sta = sta_info_get(sdata, addr);
1354 ret = __sta_info_destroy(sta);
1355 mutex_unlock(&sdata->local->sta_mtx);
1360 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1363 struct sta_info *sta;
1366 mutex_lock(&sdata->local->sta_mtx);
1367 sta = sta_info_get_bss(sdata, addr);
1368 ret = __sta_info_destroy(sta);
1369 mutex_unlock(&sdata->local->sta_mtx);
1374 static void sta_info_cleanup(struct timer_list *t)
1376 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1377 struct sta_info *sta;
1378 bool timer_needed = false;
1381 list_for_each_entry_rcu(sta, &local->sta_list, list)
1382 if (sta_info_cleanup_expire_buffered(local, sta))
1383 timer_needed = true;
1386 if (local->quiescing)
1392 mod_timer(&local->sta_cleanup,
1393 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1396 int sta_info_init(struct ieee80211_local *local)
1400 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1404 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1406 rhltable_destroy(&local->sta_hash);
1410 spin_lock_init(&local->tim_lock);
1411 mutex_init(&local->sta_mtx);
1412 INIT_LIST_HEAD(&local->sta_list);
1414 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1418 void sta_info_stop(struct ieee80211_local *local)
1420 del_timer_sync(&local->sta_cleanup);
1421 rhltable_destroy(&local->sta_hash);
1422 rhltable_destroy(&local->link_sta_hash);
1426 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1428 struct ieee80211_local *local = sdata->local;
1429 struct sta_info *sta, *tmp;
1430 LIST_HEAD(free_list);
1435 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1436 WARN_ON(vlans && !sdata->bss);
1438 mutex_lock(&local->sta_mtx);
1439 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1440 if (sdata == sta->sdata ||
1441 (vlans && sdata->bss == sta->sdata->bss)) {
1442 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1443 list_add(&sta->free_list, &free_list);
1448 if (!list_empty(&free_list)) {
1450 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1451 __sta_info_destroy_part2(sta);
1453 mutex_unlock(&local->sta_mtx);
1458 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1459 unsigned long exp_time)
1461 struct ieee80211_local *local = sdata->local;
1462 struct sta_info *sta, *tmp;
1464 mutex_lock(&local->sta_mtx);
1466 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1467 unsigned long last_active = ieee80211_sta_last_active(sta);
1469 if (sdata != sta->sdata)
1472 if (time_is_before_jiffies(last_active + exp_time)) {
1473 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1476 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1477 test_sta_flag(sta, WLAN_STA_PS_STA))
1478 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1480 WARN_ON(__sta_info_destroy(sta));
1484 mutex_unlock(&local->sta_mtx);
1487 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1489 const u8 *localaddr)
1491 struct ieee80211_local *local = hw_to_local(hw);
1492 struct rhlist_head *tmp;
1493 struct sta_info *sta;
1496 * Just return a random station if localaddr is NULL
1497 * ... first in list.
1499 for_each_sta_info(local, addr, sta, tmp) {
1501 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1510 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1512 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1515 struct sta_info *sta;
1520 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1529 EXPORT_SYMBOL(ieee80211_find_sta);
1531 /* powersave support code */
1532 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1534 struct ieee80211_sub_if_data *sdata = sta->sdata;
1535 struct ieee80211_local *local = sdata->local;
1536 struct sk_buff_head pending;
1537 int filtered = 0, buffered = 0, ac, i;
1538 unsigned long flags;
1541 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1542 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1545 if (sdata->vif.type == NL80211_IFTYPE_AP)
1546 ps = &sdata->bss->ps;
1547 else if (ieee80211_vif_is_mesh(&sdata->vif))
1548 ps = &sdata->u.mesh.ps;
1552 clear_sta_flag(sta, WLAN_STA_SP);
1554 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1555 sta->driver_buffered_tids = 0;
1556 sta->txq_buffered_tids = 0;
1558 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1559 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1561 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1562 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1565 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1568 skb_queue_head_init(&pending);
1570 /* sync with ieee80211_tx_h_unicast_ps_buf */
1571 spin_lock(&sta->ps_lock);
1572 /* Send all buffered frames to the station */
1573 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1574 int count = skb_queue_len(&pending), tmp;
1576 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1577 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1578 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1579 tmp = skb_queue_len(&pending);
1580 filtered += tmp - count;
1583 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1584 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1585 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1586 tmp = skb_queue_len(&pending);
1587 buffered += tmp - count;
1590 ieee80211_add_pending_skbs(local, &pending);
1592 /* now we're no longer in the deliver code */
1593 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1595 /* The station might have polled and then woken up before we responded,
1596 * so clear these flags now to avoid them sticking around.
1598 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1599 clear_sta_flag(sta, WLAN_STA_UAPSD);
1600 spin_unlock(&sta->ps_lock);
1602 atomic_dec(&ps->num_sta_ps);
1604 local->total_ps_buffered -= buffered;
1606 sta_info_recalc_tim(sta);
1609 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1610 sta->sta.addr, sta->sta.aid, filtered, buffered);
1612 ieee80211_check_fast_xmit(sta);
1615 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1616 enum ieee80211_frame_release_type reason,
1617 bool call_driver, bool more_data)
1619 struct ieee80211_sub_if_data *sdata = sta->sdata;
1620 struct ieee80211_local *local = sdata->local;
1621 struct ieee80211_qos_hdr *nullfunc;
1622 struct sk_buff *skb;
1623 int size = sizeof(*nullfunc);
1625 bool qos = sta->sta.wme;
1626 struct ieee80211_tx_info *info;
1627 struct ieee80211_chanctx_conf *chanctx_conf;
1630 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1631 IEEE80211_STYPE_QOS_NULLFUNC |
1632 IEEE80211_FCTL_FROMDS);
1635 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1636 IEEE80211_STYPE_NULLFUNC |
1637 IEEE80211_FCTL_FROMDS);
1640 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1644 skb_reserve(skb, local->hw.extra_tx_headroom);
1646 nullfunc = skb_put(skb, size);
1647 nullfunc->frame_control = fc;
1648 nullfunc->duration_id = 0;
1649 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1650 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1651 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1652 nullfunc->seq_ctrl = 0;
1654 skb->priority = tid;
1655 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1657 nullfunc->qos_ctrl = cpu_to_le16(tid);
1659 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1660 nullfunc->qos_ctrl |=
1661 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1663 nullfunc->frame_control |=
1664 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1668 info = IEEE80211_SKB_CB(skb);
1671 * Tell TX path to send this frame even though the
1672 * STA may still remain is PS mode after this frame
1673 * exchange. Also set EOSP to indicate this packet
1674 * ends the poll/service period.
1676 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1677 IEEE80211_TX_STATUS_EOSP |
1678 IEEE80211_TX_CTL_REQ_TX_STATUS;
1680 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1683 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1686 skb->dev = sdata->dev;
1689 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1690 if (WARN_ON(!chanctx_conf)) {
1696 info->band = chanctx_conf->def.chan->band;
1697 ieee80211_xmit(sdata, sta, skb);
1701 static int find_highest_prio_tid(unsigned long tids)
1703 /* lower 3 TIDs aren't ordered perfectly */
1705 return fls(tids) - 1;
1706 /* TID 0 is BE just like TID 3 */
1709 return fls(tids) - 1;
1712 /* Indicates if the MORE_DATA bit should be set in the last
1713 * frame obtained by ieee80211_sta_ps_get_frames.
1714 * Note that driver_release_tids is relevant only if
1715 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1718 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1719 enum ieee80211_frame_release_type reason,
1720 unsigned long driver_release_tids)
1724 /* If the driver has data on more than one TID then
1725 * certainly there's more data if we release just a
1726 * single frame now (from a single TID). This will
1727 * only happen for PS-Poll.
1729 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1730 hweight16(driver_release_tids) > 1)
1733 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1734 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1737 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1738 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1746 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1747 enum ieee80211_frame_release_type reason,
1748 struct sk_buff_head *frames,
1749 unsigned long *driver_release_tids)
1751 struct ieee80211_sub_if_data *sdata = sta->sdata;
1752 struct ieee80211_local *local = sdata->local;
1755 /* Get response frame(s) and more data bit for the last one. */
1756 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1759 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1762 tids = ieee80211_tids_for_ac(ac);
1764 /* if we already have frames from software, then we can't also
1765 * release from hardware queues
1767 if (skb_queue_empty(frames)) {
1768 *driver_release_tids |=
1769 sta->driver_buffered_tids & tids;
1770 *driver_release_tids |= sta->txq_buffered_tids & tids;
1773 if (!*driver_release_tids) {
1774 struct sk_buff *skb;
1776 while (n_frames > 0) {
1777 skb = skb_dequeue(&sta->tx_filtered[ac]);
1780 &sta->ps_tx_buf[ac]);
1782 local->total_ps_buffered--;
1787 __skb_queue_tail(frames, skb);
1791 /* If we have more frames buffered on this AC, then abort the
1792 * loop since we can't send more data from other ACs before
1793 * the buffered frames from this.
1795 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1796 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1802 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1803 int n_frames, u8 ignored_acs,
1804 enum ieee80211_frame_release_type reason)
1806 struct ieee80211_sub_if_data *sdata = sta->sdata;
1807 struct ieee80211_local *local = sdata->local;
1808 unsigned long driver_release_tids = 0;
1809 struct sk_buff_head frames;
1812 /* Service or PS-Poll period starts */
1813 set_sta_flag(sta, WLAN_STA_SP);
1815 __skb_queue_head_init(&frames);
1817 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1818 &frames, &driver_release_tids);
1820 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1822 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1823 driver_release_tids =
1824 BIT(find_highest_prio_tid(driver_release_tids));
1826 if (skb_queue_empty(&frames) && !driver_release_tids) {
1830 * For PS-Poll, this can only happen due to a race condition
1831 * when we set the TIM bit and the station notices it, but
1832 * before it can poll for the frame we expire it.
1834 * For uAPSD, this is said in the standard (11.2.1.5 h):
1835 * At each unscheduled SP for a non-AP STA, the AP shall
1836 * attempt to transmit at least one MSDU or MMPDU, but no
1837 * more than the value specified in the Max SP Length field
1838 * in the QoS Capability element from delivery-enabled ACs,
1839 * that are destined for the non-AP STA.
1841 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1844 /* This will evaluate to 1, 3, 5 or 7. */
1845 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1846 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1850 ieee80211_send_null_response(sta, tid, reason, true, false);
1851 } else if (!driver_release_tids) {
1852 struct sk_buff_head pending;
1853 struct sk_buff *skb;
1856 bool need_null = false;
1858 skb_queue_head_init(&pending);
1860 while ((skb = __skb_dequeue(&frames))) {
1861 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1862 struct ieee80211_hdr *hdr = (void *) skb->data;
1868 * Tell TX path to send this frame even though the
1869 * STA may still remain is PS mode after this frame
1872 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1873 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1876 * Use MoreData flag to indicate whether there are
1877 * more buffered frames for this STA
1879 if (more_data || !skb_queue_empty(&frames))
1880 hdr->frame_control |=
1881 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1883 hdr->frame_control &=
1884 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1886 if (ieee80211_is_data_qos(hdr->frame_control) ||
1887 ieee80211_is_qos_nullfunc(hdr->frame_control))
1888 qoshdr = ieee80211_get_qos_ctl(hdr);
1890 tids |= BIT(skb->priority);
1892 __skb_queue_tail(&pending, skb);
1894 /* end service period after last frame or add one */
1895 if (!skb_queue_empty(&frames))
1898 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1899 /* for PS-Poll, there's only one frame */
1900 info->flags |= IEEE80211_TX_STATUS_EOSP |
1901 IEEE80211_TX_CTL_REQ_TX_STATUS;
1905 /* For uAPSD, things are a bit more complicated. If the
1906 * last frame has a QoS header (i.e. is a QoS-data or
1907 * QoS-nulldata frame) then just set the EOSP bit there
1909 * If the frame doesn't have a QoS header (which means
1910 * it should be a bufferable MMPDU) then we can't set
1911 * the EOSP bit in the QoS header; add a QoS-nulldata
1912 * frame to the list to send it after the MMPDU.
1914 * Note that this code is only in the mac80211-release
1915 * code path, we assume that the driver will not buffer
1916 * anything but QoS-data frames, or if it does, will
1917 * create the QoS-nulldata frame by itself if needed.
1919 * Cf. 802.11-2012 10.2.1.10 (c).
1922 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1924 info->flags |= IEEE80211_TX_STATUS_EOSP |
1925 IEEE80211_TX_CTL_REQ_TX_STATUS;
1927 /* The standard isn't completely clear on this
1928 * as it says the more-data bit should be set
1929 * if there are more BUs. The QoS-Null frame
1930 * we're about to send isn't buffered yet, we
1931 * only create it below, but let's pretend it
1932 * was buffered just in case some clients only
1933 * expect more-data=0 when eosp=1.
1935 hdr->frame_control |=
1936 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1943 drv_allow_buffered_frames(local, sta, tids, num,
1946 ieee80211_add_pending_skbs(local, &pending);
1949 ieee80211_send_null_response(
1950 sta, find_highest_prio_tid(tids),
1951 reason, false, false);
1953 sta_info_recalc_tim(sta);
1958 * We need to release a frame that is buffered somewhere in the
1959 * driver ... it'll have to handle that.
1960 * Note that the driver also has to check the number of frames
1961 * on the TIDs we're releasing from - if there are more than
1962 * n_frames it has to set the more-data bit (if we didn't ask
1963 * it to set it anyway due to other buffered frames); if there
1964 * are fewer than n_frames it has to make sure to adjust that
1965 * to allow the service period to end properly.
1967 drv_release_buffered_frames(local, sta, driver_release_tids,
1968 n_frames, reason, more_data);
1971 * Note that we don't recalculate the TIM bit here as it would
1972 * most likely have no effect at all unless the driver told us
1973 * that the TID(s) became empty before returning here from the
1975 * Either way, however, when the driver tells us that the TID(s)
1976 * became empty or we find that a txq became empty, we'll do the
1977 * TIM recalculation.
1980 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1981 if (!sta->sta.txq[tid] ||
1982 !(driver_release_tids & BIT(tid)) ||
1983 txq_has_queue(sta->sta.txq[tid]))
1986 sta_info_recalc_tim(sta);
1992 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1994 u8 ignore_for_response = sta->sta.uapsd_queues;
1997 * If all ACs are delivery-enabled then we should reply
1998 * from any of them, if only some are enabled we reply
1999 * only from the non-enabled ones.
2001 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
2002 ignore_for_response = 0;
2004 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
2005 IEEE80211_FRAME_RELEASE_PSPOLL);
2008 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2010 int n_frames = sta->sta.max_sp;
2011 u8 delivery_enabled = sta->sta.uapsd_queues;
2014 * If we ever grow support for TSPEC this might happen if
2015 * the TSPEC update from hostapd comes in between a trigger
2016 * frame setting WLAN_STA_UAPSD in the RX path and this
2017 * actually getting called.
2019 if (!delivery_enabled)
2022 switch (sta->sta.max_sp) {
2033 /* XXX: what is a good value? */
2038 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2039 IEEE80211_FRAME_RELEASE_UAPSD);
2042 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2043 struct ieee80211_sta *pubsta, bool block)
2045 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2047 trace_api_sta_block_awake(sta->local, pubsta, block);
2050 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2051 ieee80211_clear_fast_xmit(sta);
2055 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2058 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2059 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2060 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2061 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2062 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2063 test_sta_flag(sta, WLAN_STA_UAPSD)) {
2064 /* must be asleep in this case */
2065 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2066 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2068 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2069 ieee80211_check_fast_xmit(sta);
2072 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2074 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2076 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2077 struct ieee80211_local *local = sta->local;
2079 trace_api_eosp(local, pubsta);
2081 clear_sta_flag(sta, WLAN_STA_SP);
2083 EXPORT_SYMBOL(ieee80211_sta_eosp);
2085 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2087 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2088 enum ieee80211_frame_release_type reason;
2091 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2093 reason = IEEE80211_FRAME_RELEASE_UAPSD;
2094 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2097 ieee80211_send_null_response(sta, tid, reason, false, more_data);
2099 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2101 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2102 u8 tid, bool buffered)
2104 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2106 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2109 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2112 set_bit(tid, &sta->driver_buffered_tids);
2114 clear_bit(tid, &sta->driver_buffered_tids);
2116 sta_info_recalc_tim(sta);
2118 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2120 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2121 u32 tx_airtime, u32 rx_airtime)
2123 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2124 struct ieee80211_local *local = sta->sdata->local;
2125 u8 ac = ieee80211_ac_from_tid(tid);
2129 if (sta->local->airtime_flags & AIRTIME_USE_TX)
2130 airtime += tx_airtime;
2131 if (sta->local->airtime_flags & AIRTIME_USE_RX)
2132 airtime += rx_airtime;
2134 spin_lock_bh(&local->active_txq_lock[ac]);
2135 sta->airtime[ac].tx_airtime += tx_airtime;
2136 sta->airtime[ac].rx_airtime += rx_airtime;
2138 diff = (u32)jiffies - sta->airtime[ac].last_active;
2139 if (diff <= AIRTIME_ACTIVE_DURATION)
2140 sta->airtime[ac].deficit -= airtime;
2142 spin_unlock_bh(&local->active_txq_lock[ac]);
2144 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2146 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
2151 if (!sta->sta.valid_links || !sta->sta.mlo) {
2152 sta->sta.cur = &sta->sta.deflink.agg;
2157 for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
2158 struct ieee80211_link_sta *link_sta;
2161 if (!(active_links & BIT(link_id)))
2164 link_sta = rcu_dereference(sta->sta.link[link_id]);
2169 sta->cur = sta->sta.deflink.agg;
2174 sta->cur.max_amsdu_len =
2175 min(sta->cur.max_amsdu_len,
2176 link_sta->agg.max_amsdu_len);
2177 sta->cur.max_rc_amsdu_len =
2178 min(sta->cur.max_rc_amsdu_len,
2179 link_sta->agg.max_rc_amsdu_len);
2181 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2182 sta->cur.max_tid_amsdu_len[i] =
2183 min(sta->cur.max_tid_amsdu_len[i],
2184 link_sta->agg.max_tid_amsdu_len[i]);
2188 sta->sta.cur = &sta->cur;
2191 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2193 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2195 __ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
2197 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2199 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2200 struct sta_info *sta, u8 ac,
2201 u16 tx_airtime, bool tx_completed)
2205 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2208 if (!tx_completed) {
2210 atomic_add(tx_airtime,
2211 &sta->airtime[ac].aql_tx_pending);
2213 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2214 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2219 tx_pending = atomic_sub_return(tx_airtime,
2220 &sta->airtime[ac].aql_tx_pending);
2222 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2226 atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2227 tx_pending = atomic_sub_return(tx_airtime,
2228 &local->aql_ac_pending_airtime[ac]);
2229 if (WARN_ONCE(tx_pending < 0,
2230 "Device %s AC %d pending airtime underflow: %u, %u",
2231 wiphy_name(local->hw.wiphy), ac, tx_pending,
2233 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2235 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2239 int sta_info_move_state(struct sta_info *sta,
2240 enum ieee80211_sta_state new_state)
2244 if (sta->sta_state == new_state)
2247 /* check allowed transitions first */
2249 switch (new_state) {
2250 case IEEE80211_STA_NONE:
2251 if (sta->sta_state != IEEE80211_STA_AUTH)
2254 case IEEE80211_STA_AUTH:
2255 if (sta->sta_state != IEEE80211_STA_NONE &&
2256 sta->sta_state != IEEE80211_STA_ASSOC)
2259 case IEEE80211_STA_ASSOC:
2260 if (sta->sta_state != IEEE80211_STA_AUTH &&
2261 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2264 case IEEE80211_STA_AUTHORIZED:
2265 if (sta->sta_state != IEEE80211_STA_ASSOC)
2269 WARN(1, "invalid state %d", new_state);
2273 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2274 sta->sta.addr, new_state);
2277 * notify the driver before the actual changes so it can
2278 * fail the transition
2280 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2281 int err = drv_sta_state(sta->local, sta->sdata, sta,
2282 sta->sta_state, new_state);
2287 /* reflect the change in all state variables */
2289 switch (new_state) {
2290 case IEEE80211_STA_NONE:
2291 if (sta->sta_state == IEEE80211_STA_AUTH)
2292 clear_bit(WLAN_STA_AUTH, &sta->_flags);
2294 case IEEE80211_STA_AUTH:
2295 if (sta->sta_state == IEEE80211_STA_NONE) {
2296 set_bit(WLAN_STA_AUTH, &sta->_flags);
2297 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2298 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2299 ieee80211_recalc_min_chandef(sta->sdata, -1);
2300 if (!sta->sta.support_p2p_ps)
2301 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2304 case IEEE80211_STA_ASSOC:
2305 if (sta->sta_state == IEEE80211_STA_AUTH) {
2306 set_bit(WLAN_STA_ASSOC, &sta->_flags);
2307 sta->assoc_at = ktime_get_boottime_ns();
2308 ieee80211_recalc_min_chandef(sta->sdata, -1);
2309 if (!sta->sta.support_p2p_ps)
2310 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2311 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2312 ieee80211_vif_dec_num_mcast(sta->sdata);
2313 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2314 ieee80211_clear_fast_xmit(sta);
2315 ieee80211_clear_fast_rx(sta);
2318 case IEEE80211_STA_AUTHORIZED:
2319 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2320 ieee80211_vif_inc_num_mcast(sta->sdata);
2321 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2322 ieee80211_check_fast_xmit(sta);
2323 ieee80211_check_fast_rx(sta);
2325 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2326 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2327 cfg80211_send_layer2_update(sta->sdata->dev,
2334 sta->sta_state = new_state;
2339 static struct ieee80211_sta_rx_stats *
2340 sta_get_last_rx_stats(struct sta_info *sta)
2342 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2345 if (!sta->deflink.pcpu_rx_stats)
2348 for_each_possible_cpu(cpu) {
2349 struct ieee80211_sta_rx_stats *cpustats;
2351 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2353 if (time_after(cpustats->last_rx, stats->last_rx))
2360 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2361 struct rate_info *rinfo)
2363 rinfo->bw = STA_STATS_GET(BW, rate);
2365 switch (STA_STATS_GET(TYPE, rate)) {
2366 case STA_STATS_RATE_TYPE_VHT:
2367 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2368 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2369 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2370 if (STA_STATS_GET(SGI, rate))
2371 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2373 case STA_STATS_RATE_TYPE_HT:
2374 rinfo->flags = RATE_INFO_FLAGS_MCS;
2375 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2376 if (STA_STATS_GET(SGI, rate))
2377 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2379 case STA_STATS_RATE_TYPE_LEGACY: {
2380 struct ieee80211_supported_band *sband;
2383 int band = STA_STATS_GET(LEGACY_BAND, rate);
2384 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2386 sband = local->hw.wiphy->bands[band];
2388 if (WARN_ON_ONCE(!sband->bitrates))
2391 brate = sband->bitrates[rate_idx].bitrate;
2392 if (rinfo->bw == RATE_INFO_BW_5)
2394 else if (rinfo->bw == RATE_INFO_BW_10)
2398 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2401 case STA_STATS_RATE_TYPE_HE:
2402 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2403 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2404 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2405 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2406 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2407 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2412 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2414 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2416 if (rate == STA_STATS_RATE_INVALID)
2419 sta_stats_decode_rate(sta->local, rate, rinfo);
2423 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2430 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2431 value = rxstats->msdu[tid];
2432 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2437 static void sta_set_tidstats(struct sta_info *sta,
2438 struct cfg80211_tid_stats *tidstats,
2441 struct ieee80211_local *local = sta->local;
2444 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2445 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2448 if (sta->deflink.pcpu_rx_stats) {
2449 for_each_possible_cpu(cpu) {
2450 struct ieee80211_sta_rx_stats *cpurxs;
2452 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2454 tidstats->rx_msdu +=
2455 sta_get_tidstats_msdu(cpurxs, tid);
2459 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2462 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2463 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2464 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2467 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2468 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2469 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2470 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2473 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2474 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2475 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2476 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2479 if (tid < IEEE80211_NUM_TIDS) {
2480 spin_lock_bh(&local->fq.lock);
2483 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2484 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2485 to_txq_info(sta->sta.txq[tid]));
2488 spin_unlock_bh(&local->fq.lock);
2492 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2498 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2499 value = rxstats->bytes;
2500 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2505 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2508 struct ieee80211_sub_if_data *sdata = sta->sdata;
2509 struct ieee80211_local *local = sdata->local;
2512 struct ieee80211_sta_rx_stats *last_rxstats;
2514 last_rxstats = sta_get_last_rx_stats(sta);
2516 sinfo->generation = sdata->local->sta_generation;
2518 /* do before driver, so beacon filtering drivers have a
2519 * chance to e.g. just add the number of filtered beacons
2520 * (or just modify the value entirely, of course)
2522 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2523 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2525 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2526 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2527 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2528 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2529 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2530 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2531 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2533 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2534 sinfo->beacon_loss_count =
2535 sdata->deflink.u.mgd.beacon_loss_count;
2536 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2539 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2540 sinfo->assoc_at = sta->assoc_at;
2541 sinfo->inactive_time =
2542 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2544 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2545 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2546 sinfo->tx_bytes = 0;
2547 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2548 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2549 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2552 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2553 sinfo->tx_packets = 0;
2554 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2555 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2556 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2559 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2560 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2561 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2563 if (sta->deflink.pcpu_rx_stats) {
2564 for_each_possible_cpu(cpu) {
2565 struct ieee80211_sta_rx_stats *cpurxs;
2567 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2569 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2573 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2576 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2577 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2578 if (sta->deflink.pcpu_rx_stats) {
2579 for_each_possible_cpu(cpu) {
2580 struct ieee80211_sta_rx_stats *cpurxs;
2582 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2584 sinfo->rx_packets += cpurxs->packets;
2587 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2590 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2591 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2592 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2595 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2596 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2597 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2600 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2601 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2602 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2603 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2606 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2607 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2608 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2609 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2612 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2613 sinfo->airtime_weight = sta->airtime_weight;
2614 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2617 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2618 if (sta->deflink.pcpu_rx_stats) {
2619 for_each_possible_cpu(cpu) {
2620 struct ieee80211_sta_rx_stats *cpurxs;
2622 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2623 sinfo->rx_dropped_misc += cpurxs->dropped;
2627 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2628 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2629 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2630 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2631 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2634 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2635 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2636 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2637 sinfo->signal = (s8)last_rxstats->last_signal;
2638 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2641 if (!sta->deflink.pcpu_rx_stats &&
2642 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2644 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2645 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2649 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2650 * the sta->rx_stats struct, so the check here is fine with and without
2653 if (last_rxstats->chains &&
2654 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2655 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2656 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2657 if (!sta->deflink.pcpu_rx_stats)
2658 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2660 sinfo->chains = last_rxstats->chains;
2662 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2663 sinfo->chain_signal[i] =
2664 last_rxstats->chain_signal_last[i];
2665 sinfo->chain_signal_avg[i] =
2666 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2670 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2671 !sta->sta.valid_links) {
2672 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2674 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2677 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2678 !sta->sta.valid_links) {
2679 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2680 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2683 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2684 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2685 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2688 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2689 #ifdef CONFIG_MAC80211_MESH
2690 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2691 BIT_ULL(NL80211_STA_INFO_PLID) |
2692 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2693 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2694 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2695 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2696 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2697 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2699 sinfo->llid = sta->mesh->llid;
2700 sinfo->plid = sta->mesh->plid;
2701 sinfo->plink_state = sta->mesh->plink_state;
2702 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2703 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2704 sinfo->t_offset = sta->mesh->t_offset;
2706 sinfo->local_pm = sta->mesh->local_pm;
2707 sinfo->peer_pm = sta->mesh->peer_pm;
2708 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2709 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2710 sinfo->connected_to_as = sta->mesh->connected_to_as;
2714 sinfo->bss_param.flags = 0;
2715 if (sdata->vif.bss_conf.use_cts_prot)
2716 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2717 if (sdata->vif.bss_conf.use_short_preamble)
2718 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2719 if (sdata->vif.bss_conf.use_short_slot)
2720 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2721 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2722 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2724 sinfo->sta_flags.set = 0;
2725 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2726 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2727 BIT(NL80211_STA_FLAG_WME) |
2728 BIT(NL80211_STA_FLAG_MFP) |
2729 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2730 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2731 BIT(NL80211_STA_FLAG_TDLS_PEER);
2732 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2733 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2734 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2735 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2737 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2738 if (test_sta_flag(sta, WLAN_STA_MFP))
2739 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2740 if (test_sta_flag(sta, WLAN_STA_AUTH))
2741 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2742 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2743 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2744 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2745 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2747 thr = sta_get_expected_throughput(sta);
2750 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2751 sinfo->expected_throughput = thr;
2754 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2755 sta->deflink.status_stats.ack_signal_filled) {
2756 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2757 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2760 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2761 sta->deflink.status_stats.ack_signal_filled) {
2762 sinfo->avg_ack_signal =
2763 -(s8)ewma_avg_signal_read(
2764 &sta->deflink.status_stats.avg_ack_signal);
2766 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2769 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2770 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2771 sinfo->airtime_link_metric =
2772 airtime_link_metric_get(local, sta);
2776 u32 sta_get_expected_throughput(struct sta_info *sta)
2778 struct ieee80211_sub_if_data *sdata = sta->sdata;
2779 struct ieee80211_local *local = sdata->local;
2780 struct rate_control_ref *ref = NULL;
2783 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2784 ref = local->rate_ctrl;
2786 /* check if the driver has a SW RC implementation */
2787 if (ref && ref->ops->get_expected_throughput)
2788 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2790 thr = drv_get_expected_throughput(local, sta);
2795 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2797 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2799 if (!sta->deflink.status_stats.last_ack ||
2800 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2801 return stats->last_rx;
2802 return sta->deflink.status_stats.last_ack;
2805 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2807 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2808 sta->cparams.target = MS2TIME(50);
2809 sta->cparams.interval = MS2TIME(300);
2810 sta->cparams.ecn = false;
2812 sta->cparams.target = MS2TIME(20);
2813 sta->cparams.interval = MS2TIME(100);
2814 sta->cparams.ecn = true;
2818 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2821 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2823 sta_update_codel_params(sta, thr);
2826 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2828 struct ieee80211_sub_if_data *sdata = sta->sdata;
2829 struct sta_link_alloc *alloc;
2832 lockdep_assert_held(&sdata->local->sta_mtx);
2834 /* must represent an MLD from the start */
2835 if (WARN_ON(!sta->sta.valid_links))
2838 if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2839 sta->link[link_id]))
2842 alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2846 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2852 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2854 ieee80211_link_sta_debugfs_add(&alloc->info);
2859 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2861 lockdep_assert_held(&sta->sdata->local->sta_mtx);
2863 sta_remove_link(sta, link_id, false);
2866 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2868 struct ieee80211_sub_if_data *sdata = sta->sdata;
2869 struct link_sta_info *link_sta;
2870 u16 old_links = sta->sta.valid_links;
2871 u16 new_links = old_links | BIT(link_id);
2874 link_sta = rcu_dereference_protected(sta->link[link_id],
2875 lockdep_is_held(&sdata->local->sta_mtx));
2877 if (WARN_ON(old_links == new_links || !link_sta))
2881 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2885 /* we only modify under the mutex so this is fine */
2888 sta->sta.valid_links = new_links;
2890 if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2893 /* Ensure the values are updated for the driver,
2894 * redone by sta_remove_link on failure.
2896 ieee80211_sta_recalc_aggregates(&sta->sta);
2898 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2899 old_links, new_links);
2901 sta->sta.valid_links = old_links;
2902 sta_remove_link(sta, link_id, false);
2907 ret = link_sta_info_hash_add(sdata->local, link_sta);
2912 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2914 struct ieee80211_sub_if_data *sdata = sta->sdata;
2915 u16 old_links = sta->sta.valid_links;
2917 lockdep_assert_held(&sdata->local->sta_mtx);
2919 sta->sta.valid_links &= ~BIT(link_id);
2921 if (test_sta_flag(sta, WLAN_STA_INSERTED))
2922 drv_change_sta_links(sdata->local, sdata, &sta->sta,
2923 old_links, sta->sta.valid_links);
2925 sta_remove_link(sta, link_id, true);
2928 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2929 const u8 *ext_capab,
2930 unsigned int ext_capab_len)
2934 sta->sta.max_amsdu_subframes = 0;
2936 if (ext_capab_len < 8)
2939 /* The sender might not have sent the last bit, consider it to be 0 */
2940 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2942 /* we did get all the bits, take the MSB as well */
2943 if (ext_capab_len >= 9)
2944 val |= u8_get_bits(ext_capab[8],
2945 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2948 sta->sta.max_amsdu_subframes = 4 << val;
2951 #ifdef CONFIG_LOCKDEP
2952 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
2954 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2956 return lockdep_is_held(&sta->local->sta_mtx);
2958 EXPORT_SYMBOL(lockdep_sta_mutex_held);