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 sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
278 const u8 *sta_addr, const u8 *vif_addr)
280 struct rhlist_head *tmp;
281 struct sta_info *sta;
283 for_each_sta_info(local, sta_addr, sta, tmp) {
284 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
291 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
294 struct ieee80211_local *local = sdata->local;
295 struct sta_info *sta;
298 list_for_each_entry_rcu(sta, &local->sta_list, list,
299 lockdep_is_held(&local->sta_mtx)) {
300 if (sdata != sta->sdata)
312 static void sta_info_free_link(struct link_sta_info *link_sta)
314 free_percpu(link_sta->pcpu_rx_stats);
317 static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
320 struct sta_link_alloc *alloc = NULL;
321 struct link_sta_info *link_sta;
323 link_sta = rcu_dereference_protected(sta->link[link_id],
324 lockdep_is_held(&sta->local->sta_mtx));
326 if (WARN_ON(!link_sta))
330 link_sta_info_hash_del(sta->local, link_sta);
332 if (link_sta != &sta->deflink)
333 alloc = container_of(link_sta, typeof(*alloc), info);
335 sta->sta.valid_links &= ~BIT(link_id);
336 RCU_INIT_POINTER(sta->link[link_id], NULL);
337 RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
339 sta_info_free_link(&alloc->info);
340 kfree_rcu(alloc, rcu_head);
345 * sta_info_free - free STA
347 * @local: pointer to the global information
348 * @sta: STA info to free
350 * This function must undo everything done by sta_info_alloc()
351 * that may happen before sta_info_insert(). It may only be
352 * called when sta_info_insert() has not been attempted (and
353 * if that fails, the station is freed anyway.)
355 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
359 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
360 if (!(sta->sta.valid_links & BIT(i)))
363 sta_remove_link(sta, i, false);
367 * If we had used sta_info_pre_move_state() then we might not
368 * have gone through the state transitions down again, so do
369 * it here now (and warn if it's inserted).
371 * This will clear state such as fast TX/RX that may have been
372 * allocated during state transitions.
374 while (sta->sta_state > IEEE80211_STA_NONE) {
377 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
379 ret = sta_info_move_state(sta, sta->sta_state - 1);
380 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
385 rate_control_free_sta(sta);
387 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
390 kfree(to_txq_info(sta->sta.txq[0]));
391 kfree(rcu_dereference_raw(sta->sta.rates));
392 #ifdef CONFIG_MAC80211_MESH
396 sta_info_free_link(&sta->deflink);
400 /* Caller must hold local->sta_mtx */
401 static int sta_info_hash_add(struct ieee80211_local *local,
402 struct sta_info *sta)
404 return rhltable_insert(&local->sta_hash, &sta->hash_node,
408 static void sta_deliver_ps_frames(struct work_struct *wk)
410 struct sta_info *sta;
412 sta = container_of(wk, struct sta_info, drv_deliver_wk);
418 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
419 ieee80211_sta_ps_deliver_wakeup(sta);
420 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
421 ieee80211_sta_ps_deliver_poll_response(sta);
422 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
423 ieee80211_sta_ps_deliver_uapsd(sta);
427 static int sta_prepare_rate_control(struct ieee80211_local *local,
428 struct sta_info *sta, gfp_t gfp)
430 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
433 sta->rate_ctrl = local->rate_ctrl;
434 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
436 if (!sta->rate_ctrl_priv)
442 static int sta_info_alloc_link(struct ieee80211_local *local,
443 struct link_sta_info *link_info,
446 struct ieee80211_hw *hw = &local->hw;
449 if (ieee80211_hw_check(hw, USES_RSS)) {
450 link_info->pcpu_rx_stats =
451 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
452 if (!link_info->pcpu_rx_stats)
456 link_info->rx_stats.last_rx = jiffies;
457 u64_stats_init(&link_info->rx_stats.syncp);
459 ewma_signal_init(&link_info->rx_stats_avg.signal);
460 ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
461 for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
462 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
467 static void sta_info_add_link(struct sta_info *sta,
468 unsigned int link_id,
469 struct link_sta_info *link_info,
470 struct ieee80211_link_sta *link_sta)
472 link_info->sta = sta;
473 link_info->link_id = link_id;
474 link_info->pub = link_sta;
475 rcu_assign_pointer(sta->link[link_id], link_info);
476 rcu_assign_pointer(sta->sta.link[link_id], link_sta);
479 static struct sta_info *
480 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
481 const u8 *addr, int link_id, const u8 *link_addr,
484 struct ieee80211_local *local = sdata->local;
485 struct ieee80211_hw *hw = &local->hw;
486 struct sta_info *sta;
489 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
496 if (sta_info_alloc_link(local, &sta->deflink, gfp))
500 sta_info_add_link(sta, link_id, &sta->deflink,
502 sta->sta.valid_links = BIT(link_id);
504 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
507 spin_lock_init(&sta->lock);
508 spin_lock_init(&sta->ps_lock);
509 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
510 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
511 mutex_init(&sta->ampdu_mlme.mtx);
512 #ifdef CONFIG_MAC80211_MESH
513 if (ieee80211_vif_is_mesh(&sdata->vif)) {
514 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
517 sta->mesh->plink_sta = sta;
518 spin_lock_init(&sta->mesh->plink_lock);
519 if (!sdata->u.mesh.user_mpm)
520 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
522 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
526 memcpy(sta->addr, addr, ETH_ALEN);
527 memcpy(sta->sta.addr, addr, ETH_ALEN);
528 memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
529 memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
530 sta->sta.max_rx_aggregation_subframes =
531 local->hw.max_rx_aggregation_subframes;
533 /* TODO link specific alloc and assignments for MLO Link STA */
535 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
536 * The Tx path starts to use a key as soon as the key slot ptk_idx
537 * references to is not NULL. To not use the initial Rx-only key
538 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
539 * which always will refer to a NULL key.
541 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
542 sta->ptk_idx = INVALID_PTK_KEYIDX;
545 ieee80211_init_frag_cache(&sta->frags);
547 sta->sta_state = IEEE80211_STA_NONE;
549 /* Mark TID as unreserved */
550 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
552 sta->last_connected = ktime_get_seconds();
554 if (local->ops->wake_tx_queue) {
556 int size = sizeof(struct txq_info) +
557 ALIGN(hw->txq_data_size, sizeof(void *));
559 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
563 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
564 struct txq_info *txq = txq_data + i * size;
566 /* might not do anything for the bufferable MMPDU TXQ */
567 ieee80211_txq_init(sdata, sta, txq, i);
571 if (sta_prepare_rate_control(local, sta, gfp))
574 sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
576 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
577 skb_queue_head_init(&sta->ps_tx_buf[i]);
578 skb_queue_head_init(&sta->tx_filtered[i]);
579 sta->airtime[i].deficit = sta->airtime_weight;
580 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
581 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
582 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
585 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
586 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
588 for (i = 0; i < NUM_NL80211_BANDS; i++) {
592 if (!hw->wiphy->bands[i])
596 case NL80211_BAND_2GHZ:
597 case NL80211_BAND_LC:
599 * We use both here, even if we cannot really know for
600 * sure the station will support both, but the only use
601 * for this is when we don't know anything yet and send
602 * management frames, and then we'll pick the lowest
603 * possible rate anyway.
604 * If we don't include _G here, we cannot find a rate
605 * in P2P, and thus trigger the WARN_ONCE() in rate.c
607 mandatory = IEEE80211_RATE_MANDATORY_B |
608 IEEE80211_RATE_MANDATORY_G;
610 case NL80211_BAND_5GHZ:
611 mandatory = IEEE80211_RATE_MANDATORY_A;
613 case NL80211_BAND_60GHZ:
619 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
620 struct ieee80211_rate *rate;
622 rate = &hw->wiphy->bands[i]->bitrates[r];
624 if (!(rate->flags & mandatory))
626 sta->sta.deflink.supp_rates[i] |= BIT(r);
630 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
631 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
633 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
634 sta->cparams.target = MS2TIME(20);
635 sta->cparams.interval = MS2TIME(100);
636 sta->cparams.ecn = true;
637 sta->cparams.ce_threshold_selector = 0;
638 sta->cparams.ce_threshold_mask = 0;
640 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
646 kfree(to_txq_info(sta->sta.txq[0]));
648 sta_info_free_link(&sta->deflink);
649 #ifdef CONFIG_MAC80211_MESH
656 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
657 const u8 *addr, gfp_t gfp)
659 return __sta_info_alloc(sdata, addr, -1, addr, gfp);
662 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
664 unsigned int link_id,
668 return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
671 static int sta_info_insert_check(struct sta_info *sta)
673 struct ieee80211_sub_if_data *sdata = sta->sdata;
676 * Can't be a WARN_ON because it can be triggered through a race:
677 * something inserts a STA (on one CPU) without holding the RTNL
678 * and another CPU turns off the net device.
680 if (unlikely(!ieee80211_sdata_running(sdata)))
683 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
684 !is_valid_ether_addr(sta->sta.addr)))
687 /* The RCU read lock is required by rhashtable due to
688 * asynchronous resize/rehash. We also require the mutex
692 lockdep_assert_held(&sdata->local->sta_mtx);
693 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
694 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
703 static int sta_info_insert_drv_state(struct ieee80211_local *local,
704 struct ieee80211_sub_if_data *sdata,
705 struct sta_info *sta)
707 enum ieee80211_sta_state state;
710 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
711 err = drv_sta_state(local, sdata, sta, state, state + 1);
718 * Drivers using legacy sta_add/sta_remove callbacks only
719 * get uploaded set to true after sta_add is called.
721 if (!local->ops->sta_add)
722 sta->uploaded = true;
726 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
728 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
729 sta->sta.addr, state + 1, err);
733 /* unwind on error */
734 for (; state > IEEE80211_STA_NOTEXIST; state--)
735 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
741 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
743 struct ieee80211_local *local = sdata->local;
744 bool allow_p2p_go_ps = sdata->vif.p2p;
745 struct sta_info *sta;
748 list_for_each_entry_rcu(sta, &local->sta_list, list) {
749 if (sdata != sta->sdata ||
750 !test_sta_flag(sta, WLAN_STA_ASSOC))
752 if (!sta->sta.support_p2p_ps) {
753 allow_p2p_go_ps = false;
759 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
760 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
761 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
767 * should be called with sta_mtx locked
768 * this function replaces the mutex lock
771 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
773 struct ieee80211_local *local = sta->local;
774 struct ieee80211_sub_if_data *sdata = sta->sdata;
775 struct station_info *sinfo = NULL;
778 lockdep_assert_held(&local->sta_mtx);
780 /* check if STA exists already */
781 if (sta_info_get_bss(sdata, sta->sta.addr)) {
786 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
793 local->sta_generation++;
796 /* simplify things and don't accept BA sessions yet */
797 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
799 /* make the station visible */
800 err = sta_info_hash_add(local, sta);
804 if (sta->sta.valid_links) {
805 err = link_sta_info_hash_add(local, &sta->deflink);
807 sta_info_hash_del(local, sta);
812 list_add_tail_rcu(&sta->list, &local->sta_list);
814 /* update channel context before notifying the driver about state
815 * change, this enables driver using the updated channel context right away.
817 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
818 ieee80211_recalc_min_chandef(sta->sdata, -1);
819 if (!sta->sta.support_p2p_ps)
820 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
824 err = sta_info_insert_drv_state(local, sdata, sta);
828 set_sta_flag(sta, WLAN_STA_INSERTED);
830 /* accept BA sessions now */
831 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
833 ieee80211_sta_debugfs_add(sta);
834 rate_control_add_sta_debugfs(sta);
836 sinfo->generation = local->sta_generation;
837 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
840 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
842 /* move reference to rcu-protected */
844 mutex_unlock(&local->sta_mtx);
846 if (ieee80211_vif_is_mesh(&sdata->vif))
847 mesh_accept_plinks_update(sdata);
851 if (sta->sta.valid_links)
852 link_sta_info_hash_del(local, &sta->deflink);
853 sta_info_hash_del(local, sta);
854 list_del_rcu(&sta->list);
859 cleanup_single_sta(sta);
860 mutex_unlock(&local->sta_mtx);
866 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
868 struct ieee80211_local *local = sta->local;
873 mutex_lock(&local->sta_mtx);
875 err = sta_info_insert_check(sta);
877 sta_info_free(local, sta);
878 mutex_unlock(&local->sta_mtx);
883 return sta_info_insert_finish(sta);
886 int sta_info_insert(struct sta_info *sta)
888 int err = sta_info_insert_rcu(sta);
895 static inline void __bss_tim_set(u8 *tim, u16 id)
898 * This format has been mandated by the IEEE specifications,
899 * so this line may not be changed to use the __set_bit() format.
901 tim[id / 8] |= (1 << (id % 8));
904 static inline void __bss_tim_clear(u8 *tim, u16 id)
907 * This format has been mandated by the IEEE specifications,
908 * so this line may not be changed to use the __clear_bit() format.
910 tim[id / 8] &= ~(1 << (id % 8));
913 static inline bool __bss_tim_get(u8 *tim, u16 id)
916 * This format has been mandated by the IEEE specifications,
917 * so this line may not be changed to use the test_bit() format.
919 return tim[id / 8] & (1 << (id % 8));
922 static unsigned long ieee80211_tids_for_ac(int ac)
924 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
926 case IEEE80211_AC_VO:
927 return BIT(6) | BIT(7);
928 case IEEE80211_AC_VI:
929 return BIT(4) | BIT(5);
930 case IEEE80211_AC_BE:
931 return BIT(0) | BIT(3);
932 case IEEE80211_AC_BK:
933 return BIT(1) | BIT(2);
940 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
942 struct ieee80211_local *local = sta->local;
944 bool indicate_tim = false;
945 u8 ignore_for_tim = sta->sta.uapsd_queues;
947 u16 id = sta->sta.aid;
949 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
950 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
951 if (WARN_ON_ONCE(!sta->sdata->bss))
954 ps = &sta->sdata->bss->ps;
955 #ifdef CONFIG_MAC80211_MESH
956 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
957 ps = &sta->sdata->u.mesh.ps;
963 /* No need to do anything if the driver does all */
964 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
971 * If all ACs are delivery-enabled then we should build
972 * the TIM bit for all ACs anyway; if only some are then
973 * we ignore those and build the TIM bit using only the
976 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
980 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
982 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
985 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
988 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
989 !skb_queue_empty(&sta->ps_tx_buf[ac]);
993 tids = ieee80211_tids_for_ac(ac);
996 sta->driver_buffered_tids & tids;
998 sta->txq_buffered_tids & tids;
1002 spin_lock_bh(&local->tim_lock);
1004 if (indicate_tim == __bss_tim_get(ps->tim, id))
1008 __bss_tim_set(ps->tim, id);
1010 __bss_tim_clear(ps->tim, id);
1012 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1013 local->tim_in_locked_section = true;
1014 drv_set_tim(local, &sta->sta, indicate_tim);
1015 local->tim_in_locked_section = false;
1019 spin_unlock_bh(&local->tim_lock);
1022 void sta_info_recalc_tim(struct sta_info *sta)
1024 __sta_info_recalc_tim(sta, false);
1027 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1029 struct ieee80211_tx_info *info;
1035 info = IEEE80211_SKB_CB(skb);
1037 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1038 timeout = (sta->listen_interval *
1039 sta->sdata->vif.bss_conf.beacon_int *
1041 if (timeout < STA_TX_BUFFER_EXPIRE)
1042 timeout = STA_TX_BUFFER_EXPIRE;
1043 return time_after(jiffies, info->control.jiffies + timeout);
1047 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1048 struct sta_info *sta, int ac)
1050 unsigned long flags;
1051 struct sk_buff *skb;
1054 * First check for frames that should expire on the filtered
1055 * queue. Frames here were rejected by the driver and are on
1056 * a separate queue to avoid reordering with normal PS-buffered
1057 * frames. They also aren't accounted for right now in the
1058 * total_ps_buffered counter.
1061 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1062 skb = skb_peek(&sta->tx_filtered[ac]);
1063 if (sta_info_buffer_expired(sta, skb))
1064 skb = __skb_dequeue(&sta->tx_filtered[ac]);
1067 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1070 * Frames are queued in order, so if this one
1071 * hasn't expired yet we can stop testing. If
1072 * we actually reached the end of the queue we
1073 * also need to stop, of course.
1077 ieee80211_free_txskb(&local->hw, skb);
1081 * Now also check the normal PS-buffered queue, this will
1082 * only find something if the filtered queue was emptied
1083 * since the filtered frames are all before the normal PS
1087 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1088 skb = skb_peek(&sta->ps_tx_buf[ac]);
1089 if (sta_info_buffer_expired(sta, skb))
1090 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1093 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1096 * frames are queued in order, so if this one
1097 * hasn't expired yet (or we reached the end of
1098 * the queue) we can stop testing
1103 local->total_ps_buffered--;
1104 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1106 ieee80211_free_txskb(&local->hw, skb);
1110 * Finally, recalculate the TIM bit for this station -- it might
1111 * now be clear because the station was too slow to retrieve its
1114 sta_info_recalc_tim(sta);
1117 * Return whether there are any frames still buffered, this is
1118 * used to check whether the cleanup timer still needs to run,
1119 * if there are no frames we don't need to rearm the timer.
1121 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1122 skb_queue_empty(&sta->tx_filtered[ac]));
1125 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1126 struct sta_info *sta)
1128 bool have_buffered = false;
1131 /* This is only necessary for stations on BSS/MBSS interfaces */
1132 if (!sta->sdata->bss &&
1133 !ieee80211_vif_is_mesh(&sta->sdata->vif))
1136 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1138 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1140 return have_buffered;
1143 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1145 struct ieee80211_local *local;
1146 struct ieee80211_sub_if_data *sdata;
1157 lockdep_assert_held(&local->sta_mtx);
1160 * Before removing the station from the driver and
1161 * rate control, it might still start new aggregation
1162 * sessions -- block that to make sure the tear-down
1163 * will be sufficient.
1165 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1166 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1169 * Before removing the station from the driver there might be pending
1170 * rx frames on RSS queues sent prior to the disassociation - wait for
1171 * all such frames to be processed.
1173 drv_sync_rx_queues(local, sta);
1175 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1176 struct link_sta_info *link_sta;
1178 if (!(sta->sta.valid_links & BIT(i)))
1181 link_sta = rcu_dereference_protected(sta->link[i],
1182 lockdep_is_held(&local->sta_mtx));
1184 link_sta_info_hash_del(local, link_sta);
1187 ret = sta_info_hash_del(local, sta);
1192 * for TDLS peers, make sure to return to the base channel before
1195 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1196 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1197 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1200 list_del_rcu(&sta->list);
1201 sta->removed = true;
1203 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1205 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1206 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1207 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1212 static void __sta_info_destroy_part2(struct sta_info *sta)
1214 struct ieee80211_local *local = sta->local;
1215 struct ieee80211_sub_if_data *sdata = sta->sdata;
1216 struct station_info *sinfo;
1220 * NOTE: This assumes at least synchronize_net() was done
1221 * after _part1 and before _part2!
1225 lockdep_assert_held(&local->sta_mtx);
1227 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1228 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1232 /* now keys can no longer be reached */
1233 ieee80211_free_sta_keys(local, sta);
1235 /* disable TIM bit - last chance to tell driver */
1236 __sta_info_recalc_tim(sta, true);
1241 local->sta_generation++;
1243 while (sta->sta_state > IEEE80211_STA_NONE) {
1244 ret = sta_info_move_state(sta, sta->sta_state - 1);
1251 if (sta->uploaded) {
1252 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1253 IEEE80211_STA_NOTEXIST);
1254 WARN_ON_ONCE(ret != 0);
1257 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1259 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1261 sta_set_sinfo(sta, sinfo, true);
1262 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1265 ieee80211_sta_debugfs_remove(sta);
1267 ieee80211_destroy_frag_cache(&sta->frags);
1269 cleanup_single_sta(sta);
1272 int __must_check __sta_info_destroy(struct sta_info *sta)
1274 int err = __sta_info_destroy_part1(sta);
1281 __sta_info_destroy_part2(sta);
1286 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1288 struct sta_info *sta;
1291 mutex_lock(&sdata->local->sta_mtx);
1292 sta = sta_info_get(sdata, addr);
1293 ret = __sta_info_destroy(sta);
1294 mutex_unlock(&sdata->local->sta_mtx);
1299 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1302 struct sta_info *sta;
1305 mutex_lock(&sdata->local->sta_mtx);
1306 sta = sta_info_get_bss(sdata, addr);
1307 ret = __sta_info_destroy(sta);
1308 mutex_unlock(&sdata->local->sta_mtx);
1313 static void sta_info_cleanup(struct timer_list *t)
1315 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1316 struct sta_info *sta;
1317 bool timer_needed = false;
1320 list_for_each_entry_rcu(sta, &local->sta_list, list)
1321 if (sta_info_cleanup_expire_buffered(local, sta))
1322 timer_needed = true;
1325 if (local->quiescing)
1331 mod_timer(&local->sta_cleanup,
1332 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1335 int sta_info_init(struct ieee80211_local *local)
1339 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1343 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1345 rhltable_destroy(&local->sta_hash);
1349 spin_lock_init(&local->tim_lock);
1350 mutex_init(&local->sta_mtx);
1351 INIT_LIST_HEAD(&local->sta_list);
1353 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1357 void sta_info_stop(struct ieee80211_local *local)
1359 del_timer_sync(&local->sta_cleanup);
1360 rhltable_destroy(&local->sta_hash);
1361 rhltable_destroy(&local->link_sta_hash);
1365 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1367 struct ieee80211_local *local = sdata->local;
1368 struct sta_info *sta, *tmp;
1369 LIST_HEAD(free_list);
1374 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1375 WARN_ON(vlans && !sdata->bss);
1377 mutex_lock(&local->sta_mtx);
1378 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1379 if (sdata == sta->sdata ||
1380 (vlans && sdata->bss == sta->sdata->bss)) {
1381 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1382 list_add(&sta->free_list, &free_list);
1387 if (!list_empty(&free_list)) {
1389 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1390 __sta_info_destroy_part2(sta);
1392 mutex_unlock(&local->sta_mtx);
1397 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1398 unsigned long exp_time)
1400 struct ieee80211_local *local = sdata->local;
1401 struct sta_info *sta, *tmp;
1403 mutex_lock(&local->sta_mtx);
1405 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1406 unsigned long last_active = ieee80211_sta_last_active(sta);
1408 if (sdata != sta->sdata)
1411 if (time_is_before_jiffies(last_active + exp_time)) {
1412 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1415 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1416 test_sta_flag(sta, WLAN_STA_PS_STA))
1417 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1419 WARN_ON(__sta_info_destroy(sta));
1423 mutex_unlock(&local->sta_mtx);
1426 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1428 const u8 *localaddr)
1430 struct ieee80211_local *local = hw_to_local(hw);
1431 struct rhlist_head *tmp;
1432 struct sta_info *sta;
1435 * Just return a random station if localaddr is NULL
1436 * ... first in list.
1438 for_each_sta_info(local, addr, sta, tmp) {
1440 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1449 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1451 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1454 struct sta_info *sta;
1459 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1468 EXPORT_SYMBOL(ieee80211_find_sta);
1470 /* powersave support code */
1471 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1473 struct ieee80211_sub_if_data *sdata = sta->sdata;
1474 struct ieee80211_local *local = sdata->local;
1475 struct sk_buff_head pending;
1476 int filtered = 0, buffered = 0, ac, i;
1477 unsigned long flags;
1480 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1481 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1484 if (sdata->vif.type == NL80211_IFTYPE_AP)
1485 ps = &sdata->bss->ps;
1486 else if (ieee80211_vif_is_mesh(&sdata->vif))
1487 ps = &sdata->u.mesh.ps;
1491 clear_sta_flag(sta, WLAN_STA_SP);
1493 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1494 sta->driver_buffered_tids = 0;
1495 sta->txq_buffered_tids = 0;
1497 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1498 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1500 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1501 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1504 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1507 skb_queue_head_init(&pending);
1509 /* sync with ieee80211_tx_h_unicast_ps_buf */
1510 spin_lock(&sta->ps_lock);
1511 /* Send all buffered frames to the station */
1512 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1513 int count = skb_queue_len(&pending), tmp;
1515 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1516 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1517 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1518 tmp = skb_queue_len(&pending);
1519 filtered += tmp - count;
1522 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1523 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1524 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1525 tmp = skb_queue_len(&pending);
1526 buffered += tmp - count;
1529 ieee80211_add_pending_skbs(local, &pending);
1531 /* now we're no longer in the deliver code */
1532 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1534 /* The station might have polled and then woken up before we responded,
1535 * so clear these flags now to avoid them sticking around.
1537 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1538 clear_sta_flag(sta, WLAN_STA_UAPSD);
1539 spin_unlock(&sta->ps_lock);
1541 atomic_dec(&ps->num_sta_ps);
1543 local->total_ps_buffered -= buffered;
1545 sta_info_recalc_tim(sta);
1548 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1549 sta->sta.addr, sta->sta.aid, filtered, buffered);
1551 ieee80211_check_fast_xmit(sta);
1554 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1555 enum ieee80211_frame_release_type reason,
1556 bool call_driver, bool more_data)
1558 struct ieee80211_sub_if_data *sdata = sta->sdata;
1559 struct ieee80211_local *local = sdata->local;
1560 struct ieee80211_qos_hdr *nullfunc;
1561 struct sk_buff *skb;
1562 int size = sizeof(*nullfunc);
1564 bool qos = sta->sta.wme;
1565 struct ieee80211_tx_info *info;
1566 struct ieee80211_chanctx_conf *chanctx_conf;
1569 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1570 IEEE80211_STYPE_QOS_NULLFUNC |
1571 IEEE80211_FCTL_FROMDS);
1574 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1575 IEEE80211_STYPE_NULLFUNC |
1576 IEEE80211_FCTL_FROMDS);
1579 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1583 skb_reserve(skb, local->hw.extra_tx_headroom);
1585 nullfunc = skb_put(skb, size);
1586 nullfunc->frame_control = fc;
1587 nullfunc->duration_id = 0;
1588 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1589 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1590 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1591 nullfunc->seq_ctrl = 0;
1593 skb->priority = tid;
1594 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1596 nullfunc->qos_ctrl = cpu_to_le16(tid);
1598 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1599 nullfunc->qos_ctrl |=
1600 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1602 nullfunc->frame_control |=
1603 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1607 info = IEEE80211_SKB_CB(skb);
1610 * Tell TX path to send this frame even though the
1611 * STA may still remain is PS mode after this frame
1612 * exchange. Also set EOSP to indicate this packet
1613 * ends the poll/service period.
1615 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1616 IEEE80211_TX_STATUS_EOSP |
1617 IEEE80211_TX_CTL_REQ_TX_STATUS;
1619 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1622 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1625 skb->dev = sdata->dev;
1628 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1629 if (WARN_ON(!chanctx_conf)) {
1635 info->band = chanctx_conf->def.chan->band;
1636 ieee80211_xmit(sdata, sta, skb);
1640 static int find_highest_prio_tid(unsigned long tids)
1642 /* lower 3 TIDs aren't ordered perfectly */
1644 return fls(tids) - 1;
1645 /* TID 0 is BE just like TID 3 */
1648 return fls(tids) - 1;
1651 /* Indicates if the MORE_DATA bit should be set in the last
1652 * frame obtained by ieee80211_sta_ps_get_frames.
1653 * Note that driver_release_tids is relevant only if
1654 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1657 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1658 enum ieee80211_frame_release_type reason,
1659 unsigned long driver_release_tids)
1663 /* If the driver has data on more than one TID then
1664 * certainly there's more data if we release just a
1665 * single frame now (from a single TID). This will
1666 * only happen for PS-Poll.
1668 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1669 hweight16(driver_release_tids) > 1)
1672 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1673 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1676 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1677 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1685 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1686 enum ieee80211_frame_release_type reason,
1687 struct sk_buff_head *frames,
1688 unsigned long *driver_release_tids)
1690 struct ieee80211_sub_if_data *sdata = sta->sdata;
1691 struct ieee80211_local *local = sdata->local;
1694 /* Get response frame(s) and more data bit for the last one. */
1695 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1698 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1701 tids = ieee80211_tids_for_ac(ac);
1703 /* if we already have frames from software, then we can't also
1704 * release from hardware queues
1706 if (skb_queue_empty(frames)) {
1707 *driver_release_tids |=
1708 sta->driver_buffered_tids & tids;
1709 *driver_release_tids |= sta->txq_buffered_tids & tids;
1712 if (!*driver_release_tids) {
1713 struct sk_buff *skb;
1715 while (n_frames > 0) {
1716 skb = skb_dequeue(&sta->tx_filtered[ac]);
1719 &sta->ps_tx_buf[ac]);
1721 local->total_ps_buffered--;
1726 __skb_queue_tail(frames, skb);
1730 /* If we have more frames buffered on this AC, then abort the
1731 * loop since we can't send more data from other ACs before
1732 * the buffered frames from this.
1734 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1735 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1741 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1742 int n_frames, u8 ignored_acs,
1743 enum ieee80211_frame_release_type reason)
1745 struct ieee80211_sub_if_data *sdata = sta->sdata;
1746 struct ieee80211_local *local = sdata->local;
1747 unsigned long driver_release_tids = 0;
1748 struct sk_buff_head frames;
1751 /* Service or PS-Poll period starts */
1752 set_sta_flag(sta, WLAN_STA_SP);
1754 __skb_queue_head_init(&frames);
1756 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1757 &frames, &driver_release_tids);
1759 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1761 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1762 driver_release_tids =
1763 BIT(find_highest_prio_tid(driver_release_tids));
1765 if (skb_queue_empty(&frames) && !driver_release_tids) {
1769 * For PS-Poll, this can only happen due to a race condition
1770 * when we set the TIM bit and the station notices it, but
1771 * before it can poll for the frame we expire it.
1773 * For uAPSD, this is said in the standard (11.2.1.5 h):
1774 * At each unscheduled SP for a non-AP STA, the AP shall
1775 * attempt to transmit at least one MSDU or MMPDU, but no
1776 * more than the value specified in the Max SP Length field
1777 * in the QoS Capability element from delivery-enabled ACs,
1778 * that are destined for the non-AP STA.
1780 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1783 /* This will evaluate to 1, 3, 5 or 7. */
1784 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1785 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1789 ieee80211_send_null_response(sta, tid, reason, true, false);
1790 } else if (!driver_release_tids) {
1791 struct sk_buff_head pending;
1792 struct sk_buff *skb;
1795 bool need_null = false;
1797 skb_queue_head_init(&pending);
1799 while ((skb = __skb_dequeue(&frames))) {
1800 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1801 struct ieee80211_hdr *hdr = (void *) skb->data;
1807 * Tell TX path to send this frame even though the
1808 * STA may still remain is PS mode after this frame
1811 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1812 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1815 * Use MoreData flag to indicate whether there are
1816 * more buffered frames for this STA
1818 if (more_data || !skb_queue_empty(&frames))
1819 hdr->frame_control |=
1820 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1822 hdr->frame_control &=
1823 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1825 if (ieee80211_is_data_qos(hdr->frame_control) ||
1826 ieee80211_is_qos_nullfunc(hdr->frame_control))
1827 qoshdr = ieee80211_get_qos_ctl(hdr);
1829 tids |= BIT(skb->priority);
1831 __skb_queue_tail(&pending, skb);
1833 /* end service period after last frame or add one */
1834 if (!skb_queue_empty(&frames))
1837 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1838 /* for PS-Poll, there's only one frame */
1839 info->flags |= IEEE80211_TX_STATUS_EOSP |
1840 IEEE80211_TX_CTL_REQ_TX_STATUS;
1844 /* For uAPSD, things are a bit more complicated. If the
1845 * last frame has a QoS header (i.e. is a QoS-data or
1846 * QoS-nulldata frame) then just set the EOSP bit there
1848 * If the frame doesn't have a QoS header (which means
1849 * it should be a bufferable MMPDU) then we can't set
1850 * the EOSP bit in the QoS header; add a QoS-nulldata
1851 * frame to the list to send it after the MMPDU.
1853 * Note that this code is only in the mac80211-release
1854 * code path, we assume that the driver will not buffer
1855 * anything but QoS-data frames, or if it does, will
1856 * create the QoS-nulldata frame by itself if needed.
1858 * Cf. 802.11-2012 10.2.1.10 (c).
1861 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1863 info->flags |= IEEE80211_TX_STATUS_EOSP |
1864 IEEE80211_TX_CTL_REQ_TX_STATUS;
1866 /* The standard isn't completely clear on this
1867 * as it says the more-data bit should be set
1868 * if there are more BUs. The QoS-Null frame
1869 * we're about to send isn't buffered yet, we
1870 * only create it below, but let's pretend it
1871 * was buffered just in case some clients only
1872 * expect more-data=0 when eosp=1.
1874 hdr->frame_control |=
1875 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1882 drv_allow_buffered_frames(local, sta, tids, num,
1885 ieee80211_add_pending_skbs(local, &pending);
1888 ieee80211_send_null_response(
1889 sta, find_highest_prio_tid(tids),
1890 reason, false, false);
1892 sta_info_recalc_tim(sta);
1897 * We need to release a frame that is buffered somewhere in the
1898 * driver ... it'll have to handle that.
1899 * Note that the driver also has to check the number of frames
1900 * on the TIDs we're releasing from - if there are more than
1901 * n_frames it has to set the more-data bit (if we didn't ask
1902 * it to set it anyway due to other buffered frames); if there
1903 * are fewer than n_frames it has to make sure to adjust that
1904 * to allow the service period to end properly.
1906 drv_release_buffered_frames(local, sta, driver_release_tids,
1907 n_frames, reason, more_data);
1910 * Note that we don't recalculate the TIM bit here as it would
1911 * most likely have no effect at all unless the driver told us
1912 * that the TID(s) became empty before returning here from the
1914 * Either way, however, when the driver tells us that the TID(s)
1915 * became empty or we find that a txq became empty, we'll do the
1916 * TIM recalculation.
1919 if (!sta->sta.txq[0])
1922 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1923 if (!sta->sta.txq[tid] ||
1924 !(driver_release_tids & BIT(tid)) ||
1925 txq_has_queue(sta->sta.txq[tid]))
1928 sta_info_recalc_tim(sta);
1934 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1936 u8 ignore_for_response = sta->sta.uapsd_queues;
1939 * If all ACs are delivery-enabled then we should reply
1940 * from any of them, if only some are enabled we reply
1941 * only from the non-enabled ones.
1943 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1944 ignore_for_response = 0;
1946 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1947 IEEE80211_FRAME_RELEASE_PSPOLL);
1950 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1952 int n_frames = sta->sta.max_sp;
1953 u8 delivery_enabled = sta->sta.uapsd_queues;
1956 * If we ever grow support for TSPEC this might happen if
1957 * the TSPEC update from hostapd comes in between a trigger
1958 * frame setting WLAN_STA_UAPSD in the RX path and this
1959 * actually getting called.
1961 if (!delivery_enabled)
1964 switch (sta->sta.max_sp) {
1975 /* XXX: what is a good value? */
1980 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1981 IEEE80211_FRAME_RELEASE_UAPSD);
1984 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1985 struct ieee80211_sta *pubsta, bool block)
1987 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1989 trace_api_sta_block_awake(sta->local, pubsta, block);
1992 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1993 ieee80211_clear_fast_xmit(sta);
1997 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2000 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2001 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2002 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2003 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2004 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2005 test_sta_flag(sta, WLAN_STA_UAPSD)) {
2006 /* must be asleep in this case */
2007 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2008 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2010 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2011 ieee80211_check_fast_xmit(sta);
2014 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2016 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2018 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2019 struct ieee80211_local *local = sta->local;
2021 trace_api_eosp(local, pubsta);
2023 clear_sta_flag(sta, WLAN_STA_SP);
2025 EXPORT_SYMBOL(ieee80211_sta_eosp);
2027 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2029 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2030 enum ieee80211_frame_release_type reason;
2033 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2035 reason = IEEE80211_FRAME_RELEASE_UAPSD;
2036 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2039 ieee80211_send_null_response(sta, tid, reason, false, more_data);
2041 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2043 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2044 u8 tid, bool buffered)
2046 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2048 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2051 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2054 set_bit(tid, &sta->driver_buffered_tids);
2056 clear_bit(tid, &sta->driver_buffered_tids);
2058 sta_info_recalc_tim(sta);
2060 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2062 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2063 u32 tx_airtime, u32 rx_airtime)
2065 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2066 struct ieee80211_local *local = sta->sdata->local;
2067 u8 ac = ieee80211_ac_from_tid(tid);
2071 if (sta->local->airtime_flags & AIRTIME_USE_TX)
2072 airtime += tx_airtime;
2073 if (sta->local->airtime_flags & AIRTIME_USE_RX)
2074 airtime += rx_airtime;
2076 spin_lock_bh(&local->active_txq_lock[ac]);
2077 sta->airtime[ac].tx_airtime += tx_airtime;
2078 sta->airtime[ac].rx_airtime += rx_airtime;
2080 diff = (u32)jiffies - sta->airtime[ac].last_active;
2081 if (diff <= AIRTIME_ACTIVE_DURATION)
2082 sta->airtime[ac].deficit -= airtime;
2084 spin_unlock_bh(&local->active_txq_lock[ac]);
2086 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2088 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2089 struct sta_info *sta, u8 ac,
2090 u16 tx_airtime, bool tx_completed)
2094 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2097 if (!tx_completed) {
2099 atomic_add(tx_airtime,
2100 &sta->airtime[ac].aql_tx_pending);
2102 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2103 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2108 tx_pending = atomic_sub_return(tx_airtime,
2109 &sta->airtime[ac].aql_tx_pending);
2111 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2115 atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2116 tx_pending = atomic_sub_return(tx_airtime,
2117 &local->aql_ac_pending_airtime[ac]);
2118 if (WARN_ONCE(tx_pending < 0,
2119 "Device %s AC %d pending airtime underflow: %u, %u",
2120 wiphy_name(local->hw.wiphy), ac, tx_pending,
2122 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2124 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2128 int sta_info_move_state(struct sta_info *sta,
2129 enum ieee80211_sta_state new_state)
2133 if (sta->sta_state == new_state)
2136 /* check allowed transitions first */
2138 switch (new_state) {
2139 case IEEE80211_STA_NONE:
2140 if (sta->sta_state != IEEE80211_STA_AUTH)
2143 case IEEE80211_STA_AUTH:
2144 if (sta->sta_state != IEEE80211_STA_NONE &&
2145 sta->sta_state != IEEE80211_STA_ASSOC)
2148 case IEEE80211_STA_ASSOC:
2149 if (sta->sta_state != IEEE80211_STA_AUTH &&
2150 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2153 case IEEE80211_STA_AUTHORIZED:
2154 if (sta->sta_state != IEEE80211_STA_ASSOC)
2158 WARN(1, "invalid state %d", new_state);
2162 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2163 sta->sta.addr, new_state);
2166 * notify the driver before the actual changes so it can
2167 * fail the transition
2169 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2170 int err = drv_sta_state(sta->local, sta->sdata, sta,
2171 sta->sta_state, new_state);
2176 /* reflect the change in all state variables */
2178 switch (new_state) {
2179 case IEEE80211_STA_NONE:
2180 if (sta->sta_state == IEEE80211_STA_AUTH)
2181 clear_bit(WLAN_STA_AUTH, &sta->_flags);
2183 case IEEE80211_STA_AUTH:
2184 if (sta->sta_state == IEEE80211_STA_NONE) {
2185 set_bit(WLAN_STA_AUTH, &sta->_flags);
2186 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2187 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2188 ieee80211_recalc_min_chandef(sta->sdata, -1);
2189 if (!sta->sta.support_p2p_ps)
2190 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2193 case IEEE80211_STA_ASSOC:
2194 if (sta->sta_state == IEEE80211_STA_AUTH) {
2195 set_bit(WLAN_STA_ASSOC, &sta->_flags);
2196 sta->assoc_at = ktime_get_boottime_ns();
2197 ieee80211_recalc_min_chandef(sta->sdata, -1);
2198 if (!sta->sta.support_p2p_ps)
2199 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2200 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2201 ieee80211_vif_dec_num_mcast(sta->sdata);
2202 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2203 ieee80211_clear_fast_xmit(sta);
2204 ieee80211_clear_fast_rx(sta);
2207 case IEEE80211_STA_AUTHORIZED:
2208 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2209 ieee80211_vif_inc_num_mcast(sta->sdata);
2210 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2211 ieee80211_check_fast_xmit(sta);
2212 ieee80211_check_fast_rx(sta);
2214 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2215 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2216 cfg80211_send_layer2_update(sta->sdata->dev,
2223 sta->sta_state = new_state;
2228 static struct ieee80211_sta_rx_stats *
2229 sta_get_last_rx_stats(struct sta_info *sta)
2231 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2234 if (!sta->deflink.pcpu_rx_stats)
2237 for_each_possible_cpu(cpu) {
2238 struct ieee80211_sta_rx_stats *cpustats;
2240 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2242 if (time_after(cpustats->last_rx, stats->last_rx))
2249 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2250 struct rate_info *rinfo)
2252 rinfo->bw = STA_STATS_GET(BW, rate);
2254 switch (STA_STATS_GET(TYPE, rate)) {
2255 case STA_STATS_RATE_TYPE_VHT:
2256 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2257 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2258 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2259 if (STA_STATS_GET(SGI, rate))
2260 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2262 case STA_STATS_RATE_TYPE_HT:
2263 rinfo->flags = RATE_INFO_FLAGS_MCS;
2264 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2265 if (STA_STATS_GET(SGI, rate))
2266 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2268 case STA_STATS_RATE_TYPE_LEGACY: {
2269 struct ieee80211_supported_band *sband;
2272 int band = STA_STATS_GET(LEGACY_BAND, rate);
2273 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2275 sband = local->hw.wiphy->bands[band];
2277 if (WARN_ON_ONCE(!sband->bitrates))
2280 brate = sband->bitrates[rate_idx].bitrate;
2281 if (rinfo->bw == RATE_INFO_BW_5)
2283 else if (rinfo->bw == RATE_INFO_BW_10)
2287 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2290 case STA_STATS_RATE_TYPE_HE:
2291 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2292 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2293 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2294 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2295 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2296 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2301 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2303 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2305 if (rate == STA_STATS_RATE_INVALID)
2308 sta_stats_decode_rate(sta->local, rate, rinfo);
2312 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2319 start = u64_stats_fetch_begin(&rxstats->syncp);
2320 value = rxstats->msdu[tid];
2321 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2326 static void sta_set_tidstats(struct sta_info *sta,
2327 struct cfg80211_tid_stats *tidstats,
2330 struct ieee80211_local *local = sta->local;
2333 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2334 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2337 if (sta->deflink.pcpu_rx_stats) {
2338 for_each_possible_cpu(cpu) {
2339 struct ieee80211_sta_rx_stats *cpurxs;
2341 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2343 tidstats->rx_msdu +=
2344 sta_get_tidstats_msdu(cpurxs, tid);
2348 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2351 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2352 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2353 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2356 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2357 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2358 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2359 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2362 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2363 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2364 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2365 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2368 if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2369 spin_lock_bh(&local->fq.lock);
2372 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2373 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2374 to_txq_info(sta->sta.txq[tid]));
2377 spin_unlock_bh(&local->fq.lock);
2381 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2387 start = u64_stats_fetch_begin(&rxstats->syncp);
2388 value = rxstats->bytes;
2389 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2394 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2397 struct ieee80211_sub_if_data *sdata = sta->sdata;
2398 struct ieee80211_local *local = sdata->local;
2401 struct ieee80211_sta_rx_stats *last_rxstats;
2403 last_rxstats = sta_get_last_rx_stats(sta);
2405 sinfo->generation = sdata->local->sta_generation;
2407 /* do before driver, so beacon filtering drivers have a
2408 * chance to e.g. just add the number of filtered beacons
2409 * (or just modify the value entirely, of course)
2411 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2412 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2414 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2415 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2416 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2417 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2418 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2419 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2420 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2422 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2423 sinfo->beacon_loss_count =
2424 sdata->deflink.u.mgd.beacon_loss_count;
2425 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2428 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2429 sinfo->assoc_at = sta->assoc_at;
2430 sinfo->inactive_time =
2431 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2433 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2434 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2435 sinfo->tx_bytes = 0;
2436 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2437 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2438 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2441 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2442 sinfo->tx_packets = 0;
2443 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2444 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2445 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2448 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2449 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2450 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2452 if (sta->deflink.pcpu_rx_stats) {
2453 for_each_possible_cpu(cpu) {
2454 struct ieee80211_sta_rx_stats *cpurxs;
2456 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2458 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2462 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2465 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2466 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2467 if (sta->deflink.pcpu_rx_stats) {
2468 for_each_possible_cpu(cpu) {
2469 struct ieee80211_sta_rx_stats *cpurxs;
2471 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2473 sinfo->rx_packets += cpurxs->packets;
2476 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2479 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2480 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2481 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2484 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2485 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2486 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2489 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2490 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2491 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2492 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2495 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2496 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2497 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2498 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2501 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2502 sinfo->airtime_weight = sta->airtime_weight;
2503 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2506 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2507 if (sta->deflink.pcpu_rx_stats) {
2508 for_each_possible_cpu(cpu) {
2509 struct ieee80211_sta_rx_stats *cpurxs;
2511 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2512 sinfo->rx_dropped_misc += cpurxs->dropped;
2516 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2517 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2518 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2519 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2520 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2523 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2524 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2525 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2526 sinfo->signal = (s8)last_rxstats->last_signal;
2527 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2530 if (!sta->deflink.pcpu_rx_stats &&
2531 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2533 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2534 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2538 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2539 * the sta->rx_stats struct, so the check here is fine with and without
2542 if (last_rxstats->chains &&
2543 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2544 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2545 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2546 if (!sta->deflink.pcpu_rx_stats)
2547 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2549 sinfo->chains = last_rxstats->chains;
2551 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2552 sinfo->chain_signal[i] =
2553 last_rxstats->chain_signal_last[i];
2554 sinfo->chain_signal_avg[i] =
2555 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2559 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2560 !sta->sta.valid_links) {
2561 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2563 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2566 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2567 !sta->sta.valid_links) {
2568 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2569 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2572 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2573 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2574 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2577 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2578 #ifdef CONFIG_MAC80211_MESH
2579 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2580 BIT_ULL(NL80211_STA_INFO_PLID) |
2581 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2582 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2583 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2584 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2585 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2586 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2588 sinfo->llid = sta->mesh->llid;
2589 sinfo->plid = sta->mesh->plid;
2590 sinfo->plink_state = sta->mesh->plink_state;
2591 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2592 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2593 sinfo->t_offset = sta->mesh->t_offset;
2595 sinfo->local_pm = sta->mesh->local_pm;
2596 sinfo->peer_pm = sta->mesh->peer_pm;
2597 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2598 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2599 sinfo->connected_to_as = sta->mesh->connected_to_as;
2603 sinfo->bss_param.flags = 0;
2604 if (sdata->vif.bss_conf.use_cts_prot)
2605 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2606 if (sdata->vif.bss_conf.use_short_preamble)
2607 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2608 if (sdata->vif.bss_conf.use_short_slot)
2609 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2610 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2611 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2613 sinfo->sta_flags.set = 0;
2614 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2615 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2616 BIT(NL80211_STA_FLAG_WME) |
2617 BIT(NL80211_STA_FLAG_MFP) |
2618 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2619 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2620 BIT(NL80211_STA_FLAG_TDLS_PEER);
2621 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2622 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2623 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2624 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2626 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2627 if (test_sta_flag(sta, WLAN_STA_MFP))
2628 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2629 if (test_sta_flag(sta, WLAN_STA_AUTH))
2630 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2631 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2632 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2633 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2634 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2636 thr = sta_get_expected_throughput(sta);
2639 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2640 sinfo->expected_throughput = thr;
2643 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2644 sta->deflink.status_stats.ack_signal_filled) {
2645 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2646 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2649 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2650 sta->deflink.status_stats.ack_signal_filled) {
2651 sinfo->avg_ack_signal =
2652 -(s8)ewma_avg_signal_read(
2653 &sta->deflink.status_stats.avg_ack_signal);
2655 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2658 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2659 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2660 sinfo->airtime_link_metric =
2661 airtime_link_metric_get(local, sta);
2665 u32 sta_get_expected_throughput(struct sta_info *sta)
2667 struct ieee80211_sub_if_data *sdata = sta->sdata;
2668 struct ieee80211_local *local = sdata->local;
2669 struct rate_control_ref *ref = NULL;
2672 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2673 ref = local->rate_ctrl;
2675 /* check if the driver has a SW RC implementation */
2676 if (ref && ref->ops->get_expected_throughput)
2677 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2679 thr = drv_get_expected_throughput(local, sta);
2684 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2686 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2688 if (!sta->deflink.status_stats.last_ack ||
2689 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2690 return stats->last_rx;
2691 return sta->deflink.status_stats.last_ack;
2694 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2696 if (!sta->sdata->local->ops->wake_tx_queue)
2699 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2700 sta->cparams.target = MS2TIME(50);
2701 sta->cparams.interval = MS2TIME(300);
2702 sta->cparams.ecn = false;
2704 sta->cparams.target = MS2TIME(20);
2705 sta->cparams.interval = MS2TIME(100);
2706 sta->cparams.ecn = true;
2710 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2713 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2715 sta_update_codel_params(sta, thr);
2718 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2720 struct ieee80211_sub_if_data *sdata = sta->sdata;
2721 struct sta_link_alloc *alloc;
2724 lockdep_assert_held(&sdata->local->sta_mtx);
2726 /* must represent an MLD from the start */
2727 if (WARN_ON(!sta->sta.valid_links))
2730 if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2731 sta->link[link_id]))
2734 alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2738 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2744 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2749 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2751 lockdep_assert_held(&sta->sdata->local->sta_mtx);
2753 sta_remove_link(sta, link_id, false);
2756 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2758 struct ieee80211_sub_if_data *sdata = sta->sdata;
2759 struct link_sta_info *link_sta;
2760 u16 old_links = sta->sta.valid_links;
2761 u16 new_links = old_links | BIT(link_id);
2764 link_sta = rcu_dereference_protected(sta->link[link_id],
2765 lockdep_is_held(&sdata->local->sta_mtx));
2767 if (WARN_ON(old_links == new_links || !link_sta))
2771 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2775 /* we only modify under the mutex so this is fine */
2778 sta->sta.valid_links = new_links;
2780 if (!test_sta_flag(sta, WLAN_STA_INSERTED)) {
2785 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2786 old_links, new_links);
2788 sta->sta.valid_links = old_links;
2789 sta_remove_link(sta, link_id, false);
2794 ret = link_sta_info_hash_add(sdata->local, link_sta);
2799 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2801 struct ieee80211_sub_if_data *sdata = sta->sdata;
2803 lockdep_assert_held(&sdata->local->sta_mtx);
2805 sta->sta.valid_links &= ~BIT(link_id);
2807 if (test_sta_flag(sta, WLAN_STA_INSERTED))
2808 drv_change_sta_links(sdata->local, sdata, &sta->sta,
2809 sta->sta.valid_links,
2810 sta->sta.valid_links & ~BIT(link_id));
2812 sta_remove_link(sta, link_id, true);
2815 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2816 const u8 *ext_capab,
2817 unsigned int ext_capab_len)
2821 sta->sta.max_amsdu_subframes = 0;
2823 if (ext_capab_len < 8)
2826 /* The sender might not have sent the last bit, consider it to be 0 */
2827 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2829 /* we did get all the bits, take the MSB as well */
2830 if (ext_capab_len >= 9)
2831 val |= u8_get_bits(ext_capab[8],
2832 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2835 sta->sta.max_amsdu_subframes = 4 << val;