2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
4 * Copyright 2013-2014 Intel Mobile Communications GmbH
5 * Copyright (C) 2018-2020 Intel Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/etherdevice.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/timer.h>
21 #include <linux/rtnetlink.h>
23 #include <net/mac80211.h>
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
28 #include "debugfs_sta.h"
33 * DOC: STA information lifetime rules
35 * STA info structures (&struct sta_info) are managed in a hash table
36 * for faster lookup and a list for iteration. They are managed using
37 * RCU, i.e. access to the list and hash table is protected by RCU.
39 * Upon allocating a STA info structure with sta_info_alloc(), the caller
40 * owns that structure. It must then insert it into the hash table using
41 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
42 * case (which acquires an rcu read section but must not be called from
43 * within one) will the pointer still be valid after the call. Note that
44 * the caller may not do much with the STA info before inserting it, in
45 * particular, it may not start any mesh peer link management or add
48 * When the insertion fails (sta_info_insert()) returns non-zero), the
49 * structure will have been freed by sta_info_insert()!
51 * Station entries are added by mac80211 when you establish a link with a
52 * peer. This means different things for the different type of interfaces
53 * we support. For a regular station this mean we add the AP sta when we
54 * receive an association response from the AP. For IBSS this occurs when
55 * get to know about a peer on the same IBSS. For WDS we add the sta for
56 * the peer immediately upon device open. When using AP mode we add stations
57 * for each respective station upon request from userspace through nl80211.
59 * In order to remove a STA info structure, various sta_info_destroy_*()
60 * calls are available.
62 * There is no concept of ownership on a STA entry, each structure is
63 * owned by the global hash table/list until it is removed. All users of
64 * the structure need to be RCU protected so that the structure won't be
65 * freed before they are done using it.
68 static const struct rhashtable_params sta_rht_params = {
69 .nelem_hint = 3, /* start small */
70 .automatic_shrinking = true,
71 .head_offset = offsetof(struct sta_info, hash_node),
72 .key_offset = offsetof(struct sta_info, addr),
74 .hashfn = sta_addr_hash,
75 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
78 /* Caller must hold local->sta_mtx */
79 static int sta_info_hash_del(struct ieee80211_local *local,
82 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
86 static void __cleanup_single_sta(struct sta_info *sta)
89 struct tid_ampdu_tx *tid_tx;
90 struct ieee80211_sub_if_data *sdata = sta->sdata;
91 struct ieee80211_local *local = sdata->local;
94 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
95 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
96 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
97 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
100 else if (ieee80211_vif_is_mesh(&sdata->vif))
101 ps = &sdata->u.mesh.ps;
105 clear_sta_flag(sta, WLAN_STA_PS_STA);
106 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
107 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
109 atomic_dec(&ps->num_sta_ps);
112 if (sta->sta.txq[0]) {
113 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115 int n = skb_queue_len(&txqi->queue);
117 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
122 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
123 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
124 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
125 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
128 if (ieee80211_vif_is_mesh(&sdata->vif))
129 mesh_sta_cleanup(sta);
131 cancel_work_sync(&sta->drv_deliver_wk);
134 * Destroy aggregation state here. It would be nice to wait for the
135 * driver to finish aggregation stop and then clean up, but for now
136 * drivers have to handle aggregation stop being requested, followed
137 * directly by station destruction.
139 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
140 kfree(sta->ampdu_mlme.tid_start_tx[i]);
141 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
144 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
149 static void cleanup_single_sta(struct sta_info *sta)
151 struct ieee80211_sub_if_data *sdata = sta->sdata;
152 struct ieee80211_local *local = sdata->local;
154 __cleanup_single_sta(sta);
155 sta_info_free(local, sta);
158 /* protected by RCU */
159 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
162 struct ieee80211_local *local = sdata->local;
163 struct sta_info *sta;
164 struct rhash_head *tmp;
165 const struct bucket_table *tbl;
168 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
170 for_each_sta_info(local, tbl, addr, sta, tmp) {
171 if (sta->sdata == sdata) {
173 /* this is safe as the caller must already hold
174 * another rcu read section or the mutex
184 * Get sta info either from the specified interface
185 * or from one of its vlans
187 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
190 struct ieee80211_local *local = sdata->local;
191 struct sta_info *sta;
192 struct rhash_head *tmp;
193 const struct bucket_table *tbl;
196 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
198 for_each_sta_info(local, tbl, addr, sta, tmp) {
199 if (sta->sdata == sdata ||
200 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
202 /* this is safe as the caller must already hold
203 * another rcu read section or the mutex
212 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
215 struct ieee80211_local *local = sdata->local;
216 struct sta_info *sta;
219 list_for_each_entry_rcu(sta, &local->sta_list, list) {
220 if (sdata != sta->sdata)
233 * sta_info_free - free STA
235 * @local: pointer to the global information
236 * @sta: STA info to free
238 * This function must undo everything done by sta_info_alloc()
239 * that may happen before sta_info_insert(). It may only be
240 * called when sta_info_insert() has not been attempted (and
241 * if that fails, the station is freed anyway.)
243 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
246 * If we had used sta_info_pre_move_state() then we might not
247 * have gone through the state transitions down again, so do
248 * it here now (and warn if it's inserted).
250 * This will clear state such as fast TX/RX that may have been
251 * allocated during state transitions.
253 while (sta->sta_state > IEEE80211_STA_NONE) {
256 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
258 ret = sta_info_move_state(sta, sta->sta_state - 1);
259 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
264 rate_control_free_sta(sta);
266 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
269 kfree(to_txq_info(sta->sta.txq[0]));
270 kfree(rcu_dereference_raw(sta->sta.rates));
271 #ifdef CONFIG_MAC80211_MESH
277 /* Caller must hold local->sta_mtx */
278 static int sta_info_hash_add(struct ieee80211_local *local,
279 struct sta_info *sta)
281 return rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
285 static void sta_deliver_ps_frames(struct work_struct *wk)
287 struct sta_info *sta;
289 sta = container_of(wk, struct sta_info, drv_deliver_wk);
295 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
296 ieee80211_sta_ps_deliver_wakeup(sta);
297 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
298 ieee80211_sta_ps_deliver_poll_response(sta);
299 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
300 ieee80211_sta_ps_deliver_uapsd(sta);
304 static int sta_prepare_rate_control(struct ieee80211_local *local,
305 struct sta_info *sta, gfp_t gfp)
307 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
310 sta->rate_ctrl = local->rate_ctrl;
311 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
313 if (!sta->rate_ctrl_priv)
319 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
320 const u8 *addr, gfp_t gfp)
322 struct ieee80211_local *local = sdata->local;
323 struct ieee80211_hw *hw = &local->hw;
324 struct sta_info *sta;
327 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
331 spin_lock_init(&sta->lock);
332 spin_lock_init(&sta->ps_lock);
333 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
334 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
335 mutex_init(&sta->ampdu_mlme.mtx);
336 #ifdef CONFIG_MAC80211_MESH
337 if (ieee80211_vif_is_mesh(&sdata->vif)) {
338 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
341 spin_lock_init(&sta->mesh->plink_lock);
342 if (ieee80211_vif_is_mesh(&sdata->vif) &&
343 !sdata->u.mesh.user_mpm)
344 init_timer(&sta->mesh->plink_timer);
345 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
349 memcpy(sta->addr, addr, ETH_ALEN);
350 memcpy(sta->sta.addr, addr, ETH_ALEN);
351 sta->sta.max_rx_aggregation_subframes =
352 local->hw.max_rx_aggregation_subframes;
356 sta->rx_stats.last_rx = jiffies;
358 ieee80211_init_frag_cache(&sta->frags);
360 sta->sta_state = IEEE80211_STA_NONE;
362 /* Mark TID as unreserved */
363 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
365 sta->last_connected = ktime_get_seconds();
366 ewma_signal_init(&sta->rx_stats.avg_signal);
367 for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
368 ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
370 if (local->ops->wake_tx_queue) {
372 int size = sizeof(struct txq_info) +
373 ALIGN(hw->txq_data_size, sizeof(void *));
375 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
379 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
380 struct txq_info *txq = txq_data + i * size;
382 ieee80211_init_tx_queue(sdata, sta, txq, i);
386 if (sta_prepare_rate_control(local, sta, gfp))
389 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
391 * timer_to_tid must be initialized with identity mapping
392 * to enable session_timer's data differentiation. See
393 * sta_rx_agg_session_timer_expired for usage.
395 sta->timer_to_tid[i] = i;
397 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
398 skb_queue_head_init(&sta->ps_tx_buf[i]);
399 skb_queue_head_init(&sta->tx_filtered[i]);
402 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
403 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
405 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
406 if (sdata->vif.type == NL80211_IFTYPE_AP ||
407 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
408 struct ieee80211_supported_band *sband =
409 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
410 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
411 IEEE80211_HT_CAP_SM_PS_SHIFT;
413 * Assume that hostapd advertises our caps in the beacon and
414 * this is the known_smps_mode for a station that just assciated
417 case WLAN_HT_SMPS_CONTROL_DISABLED:
418 sta->known_smps_mode = IEEE80211_SMPS_OFF;
420 case WLAN_HT_SMPS_CONTROL_STATIC:
421 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
423 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
424 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
431 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
437 kfree(to_txq_info(sta->sta.txq[0]));
439 #ifdef CONFIG_MAC80211_MESH
446 static int sta_info_insert_check(struct sta_info *sta)
448 struct ieee80211_sub_if_data *sdata = sta->sdata;
451 * Can't be a WARN_ON because it can be triggered through a race:
452 * something inserts a STA (on one CPU) without holding the RTNL
453 * and another CPU turns off the net device.
455 if (unlikely(!ieee80211_sdata_running(sdata)))
458 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
459 is_multicast_ether_addr(sta->sta.addr)))
462 /* Strictly speaking this isn't necessary as we hold the mutex, but
463 * the rhashtable code can't really deal with that distinction. We
464 * do require the mutex for correctness though.
467 lockdep_assert_held(&sdata->local->sta_mtx);
468 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
469 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
478 static int sta_info_insert_drv_state(struct ieee80211_local *local,
479 struct ieee80211_sub_if_data *sdata,
480 struct sta_info *sta)
482 enum ieee80211_sta_state state;
485 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
486 err = drv_sta_state(local, sdata, sta, state, state + 1);
493 * Drivers using legacy sta_add/sta_remove callbacks only
494 * get uploaded set to true after sta_add is called.
496 if (!local->ops->sta_add)
497 sta->uploaded = true;
501 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
503 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
504 sta->sta.addr, state + 1, err);
508 /* unwind on error */
509 for (; state > IEEE80211_STA_NOTEXIST; state--)
510 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
516 * should be called with sta_mtx locked
517 * this function replaces the mutex lock
520 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
522 struct ieee80211_local *local = sta->local;
523 struct ieee80211_sub_if_data *sdata = sta->sdata;
524 struct station_info *sinfo;
527 lockdep_assert_held(&local->sta_mtx);
529 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
535 /* check if STA exists already */
536 if (sta_info_get_bss(sdata, sta->sta.addr)) {
542 local->sta_generation++;
545 /* simplify things and don't accept BA sessions yet */
546 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
548 /* make the station visible */
549 err = sta_info_hash_add(local, sta);
553 list_add_tail_rcu(&sta->list, &local->sta_list);
556 err = sta_info_insert_drv_state(local, sdata, sta);
560 set_sta_flag(sta, WLAN_STA_INSERTED);
561 /* accept BA sessions now */
562 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
564 ieee80211_recalc_min_chandef(sdata);
565 ieee80211_sta_debugfs_add(sta);
566 rate_control_add_sta_debugfs(sta);
568 sinfo->generation = local->sta_generation;
569 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
572 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
574 /* move reference to rcu-protected */
576 mutex_unlock(&local->sta_mtx);
578 if (ieee80211_vif_is_mesh(&sdata->vif))
579 mesh_accept_plinks_update(sdata);
583 sta_info_hash_del(local, sta);
584 list_del_rcu(&sta->list);
588 cleanup_single_sta(sta);
590 mutex_unlock(&local->sta_mtx);
596 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
598 struct ieee80211_local *local = sta->local;
603 mutex_lock(&local->sta_mtx);
605 err = sta_info_insert_check(sta);
607 sta_info_free(local, sta);
608 mutex_unlock(&local->sta_mtx);
613 return sta_info_insert_finish(sta);
616 int sta_info_insert(struct sta_info *sta)
618 int err = sta_info_insert_rcu(sta);
625 static inline void __bss_tim_set(u8 *tim, u16 id)
628 * This format has been mandated by the IEEE specifications,
629 * so this line may not be changed to use the __set_bit() format.
631 tim[id / 8] |= (1 << (id % 8));
634 static inline void __bss_tim_clear(u8 *tim, u16 id)
637 * This format has been mandated by the IEEE specifications,
638 * so this line may not be changed to use the __clear_bit() format.
640 tim[id / 8] &= ~(1 << (id % 8));
643 static inline bool __bss_tim_get(u8 *tim, u16 id)
646 * This format has been mandated by the IEEE specifications,
647 * so this line may not be changed to use the test_bit() format.
649 return tim[id / 8] & (1 << (id % 8));
652 static unsigned long ieee80211_tids_for_ac(int ac)
654 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
656 case IEEE80211_AC_VO:
657 return BIT(6) | BIT(7);
658 case IEEE80211_AC_VI:
659 return BIT(4) | BIT(5);
660 case IEEE80211_AC_BE:
661 return BIT(0) | BIT(3);
662 case IEEE80211_AC_BK:
663 return BIT(1) | BIT(2);
670 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
672 struct ieee80211_local *local = sta->local;
674 bool indicate_tim = false;
675 u8 ignore_for_tim = sta->sta.uapsd_queues;
677 u16 id = sta->sta.aid;
679 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
680 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
681 if (WARN_ON_ONCE(!sta->sdata->bss))
684 ps = &sta->sdata->bss->ps;
685 #ifdef CONFIG_MAC80211_MESH
686 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
687 ps = &sta->sdata->u.mesh.ps;
693 /* No need to do anything if the driver does all */
694 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
701 * If all ACs are delivery-enabled then we should build
702 * the TIM bit for all ACs anyway; if only some are then
703 * we ignore those and build the TIM bit using only the
706 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
710 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
712 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
715 if (ignore_for_tim & BIT(ac))
718 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
719 !skb_queue_empty(&sta->ps_tx_buf[ac]);
723 tids = ieee80211_tids_for_ac(ac);
726 sta->driver_buffered_tids & tids;
728 sta->txq_buffered_tids & tids;
732 spin_lock_bh(&local->tim_lock);
734 if (indicate_tim == __bss_tim_get(ps->tim, id))
738 __bss_tim_set(ps->tim, id);
740 __bss_tim_clear(ps->tim, id);
742 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
743 local->tim_in_locked_section = true;
744 drv_set_tim(local, &sta->sta, indicate_tim);
745 local->tim_in_locked_section = false;
749 spin_unlock_bh(&local->tim_lock);
752 void sta_info_recalc_tim(struct sta_info *sta)
754 __sta_info_recalc_tim(sta, false);
757 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
759 struct ieee80211_tx_info *info;
765 info = IEEE80211_SKB_CB(skb);
767 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
768 timeout = (sta->listen_interval *
769 sta->sdata->vif.bss_conf.beacon_int *
771 if (timeout < STA_TX_BUFFER_EXPIRE)
772 timeout = STA_TX_BUFFER_EXPIRE;
773 return time_after(jiffies, info->control.jiffies + timeout);
777 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
778 struct sta_info *sta, int ac)
784 * First check for frames that should expire on the filtered
785 * queue. Frames here were rejected by the driver and are on
786 * a separate queue to avoid reordering with normal PS-buffered
787 * frames. They also aren't accounted for right now in the
788 * total_ps_buffered counter.
791 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
792 skb = skb_peek(&sta->tx_filtered[ac]);
793 if (sta_info_buffer_expired(sta, skb))
794 skb = __skb_dequeue(&sta->tx_filtered[ac]);
797 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
800 * Frames are queued in order, so if this one
801 * hasn't expired yet we can stop testing. If
802 * we actually reached the end of the queue we
803 * also need to stop, of course.
807 ieee80211_free_txskb(&local->hw, skb);
811 * Now also check the normal PS-buffered queue, this will
812 * only find something if the filtered queue was emptied
813 * since the filtered frames are all before the normal PS
817 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
818 skb = skb_peek(&sta->ps_tx_buf[ac]);
819 if (sta_info_buffer_expired(sta, skb))
820 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
823 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
826 * frames are queued in order, so if this one
827 * hasn't expired yet (or we reached the end of
828 * the queue) we can stop testing
833 local->total_ps_buffered--;
834 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
836 ieee80211_free_txskb(&local->hw, skb);
840 * Finally, recalculate the TIM bit for this station -- it might
841 * now be clear because the station was too slow to retrieve its
844 sta_info_recalc_tim(sta);
847 * Return whether there are any frames still buffered, this is
848 * used to check whether the cleanup timer still needs to run,
849 * if there are no frames we don't need to rearm the timer.
851 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
852 skb_queue_empty(&sta->tx_filtered[ac]));
855 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
856 struct sta_info *sta)
858 bool have_buffered = false;
861 /* This is only necessary for stations on BSS/MBSS interfaces */
862 if (!sta->sdata->bss &&
863 !ieee80211_vif_is_mesh(&sta->sdata->vif))
866 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
868 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
870 return have_buffered;
873 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
875 struct ieee80211_local *local;
876 struct ieee80211_sub_if_data *sdata;
887 lockdep_assert_held(&local->sta_mtx);
890 * Before removing the station from the driver and
891 * rate control, it might still start new aggregation
892 * sessions -- block that to make sure the tear-down
893 * will be sufficient.
895 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
896 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
898 ret = sta_info_hash_del(local, sta);
903 * for TDLS peers, make sure to return to the base channel before
906 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
907 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
908 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
911 list_del_rcu(&sta->list);
913 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
915 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
916 rcu_access_pointer(sdata->u.vlan.sta) == sta)
917 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
922 static void __sta_info_destroy_part2(struct sta_info *sta)
924 struct ieee80211_local *local = sta->local;
925 struct ieee80211_sub_if_data *sdata = sta->sdata;
926 struct station_info *sinfo;
930 * NOTE: This assumes at least synchronize_net() was done
931 * after _part1 and before _part2!
935 lockdep_assert_held(&local->sta_mtx);
937 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
938 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
942 /* now keys can no longer be reached */
943 ieee80211_free_sta_keys(local, sta);
945 /* disable TIM bit - last chance to tell driver */
946 __sta_info_recalc_tim(sta, true);
951 local->sta_generation++;
953 while (sta->sta_state > IEEE80211_STA_NONE) {
954 ret = sta_info_move_state(sta, sta->sta_state - 1);
962 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
963 IEEE80211_STA_NOTEXIST);
964 WARN_ON_ONCE(ret != 0);
967 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
969 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
971 sta_set_sinfo(sta, sinfo);
972 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
975 rate_control_remove_sta_debugfs(sta);
976 ieee80211_sta_debugfs_remove(sta);
977 ieee80211_recalc_min_chandef(sdata);
979 ieee80211_destroy_frag_cache(&sta->frags);
981 cleanup_single_sta(sta);
984 int __must_check __sta_info_destroy(struct sta_info *sta)
986 int err = __sta_info_destroy_part1(sta);
993 __sta_info_destroy_part2(sta);
998 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1000 struct sta_info *sta;
1003 mutex_lock(&sdata->local->sta_mtx);
1004 sta = sta_info_get(sdata, addr);
1005 ret = __sta_info_destroy(sta);
1006 mutex_unlock(&sdata->local->sta_mtx);
1011 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1014 struct sta_info *sta;
1017 mutex_lock(&sdata->local->sta_mtx);
1018 sta = sta_info_get_bss(sdata, addr);
1019 ret = __sta_info_destroy(sta);
1020 mutex_unlock(&sdata->local->sta_mtx);
1025 static void sta_info_cleanup(unsigned long data)
1027 struct ieee80211_local *local = (struct ieee80211_local *) data;
1028 struct sta_info *sta;
1029 bool timer_needed = false;
1032 list_for_each_entry_rcu(sta, &local->sta_list, list)
1033 if (sta_info_cleanup_expire_buffered(local, sta))
1034 timer_needed = true;
1037 if (local->quiescing)
1043 mod_timer(&local->sta_cleanup,
1044 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1047 u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1049 return jhash(key, ETH_ALEN, seed);
1052 int sta_info_init(struct ieee80211_local *local)
1056 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1060 spin_lock_init(&local->tim_lock);
1061 mutex_init(&local->sta_mtx);
1062 INIT_LIST_HEAD(&local->sta_list);
1064 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1065 (unsigned long)local);
1069 void sta_info_stop(struct ieee80211_local *local)
1071 del_timer_sync(&local->sta_cleanup);
1072 rhashtable_destroy(&local->sta_hash);
1076 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1078 struct ieee80211_local *local = sdata->local;
1079 struct sta_info *sta, *tmp;
1080 LIST_HEAD(free_list);
1085 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1086 WARN_ON(vlans && !sdata->bss);
1088 mutex_lock(&local->sta_mtx);
1089 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1090 if (sdata == sta->sdata ||
1091 (vlans && sdata->bss == sta->sdata->bss)) {
1092 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1093 list_add(&sta->free_list, &free_list);
1098 if (!list_empty(&free_list)) {
1100 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1101 __sta_info_destroy_part2(sta);
1103 mutex_unlock(&local->sta_mtx);
1108 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1109 unsigned long exp_time)
1111 struct ieee80211_local *local = sdata->local;
1112 struct sta_info *sta, *tmp;
1114 mutex_lock(&local->sta_mtx);
1116 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1117 if (sdata != sta->sdata)
1120 if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
1121 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1124 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1125 test_sta_flag(sta, WLAN_STA_PS_STA))
1126 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1128 WARN_ON(__sta_info_destroy(sta));
1132 mutex_unlock(&local->sta_mtx);
1135 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1137 const u8 *localaddr)
1139 struct ieee80211_local *local = hw_to_local(hw);
1140 struct sta_info *sta;
1141 struct rhash_head *tmp;
1142 const struct bucket_table *tbl;
1144 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1147 * Just return a random station if localaddr is NULL
1148 * ... first in list.
1150 for_each_sta_info(local, tbl, addr, sta, tmp) {
1152 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1161 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1163 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1166 struct sta_info *sta;
1171 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1180 EXPORT_SYMBOL(ieee80211_find_sta);
1182 /* powersave support code */
1183 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1185 struct ieee80211_sub_if_data *sdata = sta->sdata;
1186 struct ieee80211_local *local = sdata->local;
1187 struct sk_buff_head pending;
1188 int filtered = 0, buffered = 0, ac, i;
1189 unsigned long flags;
1192 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1193 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1196 if (sdata->vif.type == NL80211_IFTYPE_AP)
1197 ps = &sdata->bss->ps;
1198 else if (ieee80211_vif_is_mesh(&sdata->vif))
1199 ps = &sdata->u.mesh.ps;
1203 clear_sta_flag(sta, WLAN_STA_SP);
1205 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1206 sta->driver_buffered_tids = 0;
1207 sta->txq_buffered_tids = 0;
1209 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1210 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1212 if (sta->sta.txq[0]) {
1213 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1214 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1216 if (!skb_queue_len(&txqi->queue))
1219 drv_wake_tx_queue(local, txqi);
1223 skb_queue_head_init(&pending);
1225 /* sync with ieee80211_tx_h_unicast_ps_buf */
1226 spin_lock(&sta->ps_lock);
1227 /* Send all buffered frames to the station */
1228 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1229 int count = skb_queue_len(&pending), tmp;
1231 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1232 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1233 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1234 tmp = skb_queue_len(&pending);
1235 filtered += tmp - count;
1238 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1239 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1240 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1241 tmp = skb_queue_len(&pending);
1242 buffered += tmp - count;
1245 ieee80211_add_pending_skbs(local, &pending);
1247 /* now we're no longer in the deliver code */
1248 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1250 /* The station might have polled and then woken up before we responded,
1251 * so clear these flags now to avoid them sticking around.
1253 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1254 clear_sta_flag(sta, WLAN_STA_UAPSD);
1255 spin_unlock(&sta->ps_lock);
1257 atomic_dec(&ps->num_sta_ps);
1259 /* This station just woke up and isn't aware of our SMPS state */
1260 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1261 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1262 sdata->smps_mode) &&
1263 sta->known_smps_mode != sdata->bss->req_smps &&
1264 sta_info_tx_streams(sta) != 1) {
1266 "%pM just woke up and MIMO capable - update SMPS\n",
1268 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1270 sdata->vif.bss_conf.bssid);
1273 local->total_ps_buffered -= buffered;
1275 sta_info_recalc_tim(sta);
1278 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1279 sta->sta.addr, sta->sta.aid, filtered, buffered);
1281 ieee80211_check_fast_xmit(sta);
1284 static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1285 struct sta_info *sta, int tid,
1286 enum ieee80211_frame_release_type reason,
1289 struct ieee80211_local *local = sdata->local;
1290 struct ieee80211_qos_hdr *nullfunc;
1291 struct sk_buff *skb;
1292 int size = sizeof(*nullfunc);
1294 bool qos = sta->sta.wme;
1295 struct ieee80211_tx_info *info;
1296 struct ieee80211_chanctx_conf *chanctx_conf;
1299 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1300 IEEE80211_STYPE_QOS_NULLFUNC |
1301 IEEE80211_FCTL_FROMDS);
1304 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1305 IEEE80211_STYPE_NULLFUNC |
1306 IEEE80211_FCTL_FROMDS);
1309 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1313 skb_reserve(skb, local->hw.extra_tx_headroom);
1315 nullfunc = (void *) skb_put(skb, size);
1316 nullfunc->frame_control = fc;
1317 nullfunc->duration_id = 0;
1318 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1319 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1320 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1321 nullfunc->seq_ctrl = 0;
1323 skb->priority = tid;
1324 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1326 nullfunc->qos_ctrl = cpu_to_le16(tid);
1328 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1329 nullfunc->qos_ctrl |=
1330 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1333 info = IEEE80211_SKB_CB(skb);
1336 * Tell TX path to send this frame even though the
1337 * STA may still remain is PS mode after this frame
1338 * exchange. Also set EOSP to indicate this packet
1339 * ends the poll/service period.
1341 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1342 IEEE80211_TX_STATUS_EOSP |
1343 IEEE80211_TX_CTL_REQ_TX_STATUS;
1345 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1348 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1351 skb->dev = sdata->dev;
1354 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1355 if (WARN_ON(!chanctx_conf)) {
1361 info->band = chanctx_conf->def.chan->band;
1362 ieee80211_xmit(sdata, sta, skb);
1366 static int find_highest_prio_tid(unsigned long tids)
1368 /* lower 3 TIDs aren't ordered perfectly */
1370 return fls(tids) - 1;
1371 /* TID 0 is BE just like TID 3 */
1374 return fls(tids) - 1;
1378 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1379 int n_frames, u8 ignored_acs,
1380 enum ieee80211_frame_release_type reason)
1382 struct ieee80211_sub_if_data *sdata = sta->sdata;
1383 struct ieee80211_local *local = sdata->local;
1384 bool more_data = false;
1386 unsigned long driver_release_tids = 0;
1387 struct sk_buff_head frames;
1389 /* Service or PS-Poll period starts */
1390 set_sta_flag(sta, WLAN_STA_SP);
1392 __skb_queue_head_init(&frames);
1394 /* Get response frame(s) and more data bit for the last one. */
1395 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1398 if (ignored_acs & BIT(ac))
1401 tids = ieee80211_tids_for_ac(ac);
1403 /* if we already have frames from software, then we can't also
1404 * release from hardware queues
1406 if (skb_queue_empty(&frames)) {
1407 driver_release_tids |= sta->driver_buffered_tids & tids;
1408 driver_release_tids |= sta->txq_buffered_tids & tids;
1411 if (driver_release_tids) {
1412 /* If the driver has data on more than one TID then
1413 * certainly there's more data if we release just a
1414 * single frame now (from a single TID). This will
1415 * only happen for PS-Poll.
1417 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1418 hweight16(driver_release_tids) > 1) {
1420 driver_release_tids =
1421 BIT(find_highest_prio_tid(
1422 driver_release_tids));
1426 struct sk_buff *skb;
1428 while (n_frames > 0) {
1429 skb = skb_dequeue(&sta->tx_filtered[ac]);
1432 &sta->ps_tx_buf[ac]);
1434 local->total_ps_buffered--;
1439 __skb_queue_tail(&frames, skb);
1443 /* If we have more frames buffered on this AC, then set the
1444 * more-data bit and abort the loop since we can't send more
1445 * data from other ACs before the buffered frames from this.
1447 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1448 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1454 if (skb_queue_empty(&frames) && !driver_release_tids) {
1458 * For PS-Poll, this can only happen due to a race condition
1459 * when we set the TIM bit and the station notices it, but
1460 * before it can poll for the frame we expire it.
1462 * For uAPSD, this is said in the standard (11.2.1.5 h):
1463 * At each unscheduled SP for a non-AP STA, the AP shall
1464 * attempt to transmit at least one MSDU or MMPDU, but no
1465 * more than the value specified in the Max SP Length field
1466 * in the QoS Capability element from delivery-enabled ACs,
1467 * that are destined for the non-AP STA.
1469 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1472 /* This will evaluate to 1, 3, 5 or 7. */
1473 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1475 ieee80211_send_null_response(sdata, sta, tid, reason, true);
1476 } else if (!driver_release_tids) {
1477 struct sk_buff_head pending;
1478 struct sk_buff *skb;
1481 bool need_null = false;
1483 skb_queue_head_init(&pending);
1485 while ((skb = __skb_dequeue(&frames))) {
1486 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1487 struct ieee80211_hdr *hdr = (void *) skb->data;
1493 * Tell TX path to send this frame even though the
1494 * STA may still remain is PS mode after this frame
1497 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1498 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1501 * Use MoreData flag to indicate whether there are
1502 * more buffered frames for this STA
1504 if (more_data || !skb_queue_empty(&frames))
1505 hdr->frame_control |=
1506 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1508 hdr->frame_control &=
1509 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1511 if (ieee80211_is_data_qos(hdr->frame_control) ||
1512 ieee80211_is_qos_nullfunc(hdr->frame_control))
1513 qoshdr = ieee80211_get_qos_ctl(hdr);
1515 tids |= BIT(skb->priority);
1517 __skb_queue_tail(&pending, skb);
1519 /* end service period after last frame or add one */
1520 if (!skb_queue_empty(&frames))
1523 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1524 /* for PS-Poll, there's only one frame */
1525 info->flags |= IEEE80211_TX_STATUS_EOSP |
1526 IEEE80211_TX_CTL_REQ_TX_STATUS;
1530 /* For uAPSD, things are a bit more complicated. If the
1531 * last frame has a QoS header (i.e. is a QoS-data or
1532 * QoS-nulldata frame) then just set the EOSP bit there
1534 * If the frame doesn't have a QoS header (which means
1535 * it should be a bufferable MMPDU) then we can't set
1536 * the EOSP bit in the QoS header; add a QoS-nulldata
1537 * frame to the list to send it after the MMPDU.
1539 * Note that this code is only in the mac80211-release
1540 * code path, we assume that the driver will not buffer
1541 * anything but QoS-data frames, or if it does, will
1542 * create the QoS-nulldata frame by itself if needed.
1544 * Cf. 802.11-2012 10.2.1.10 (c).
1547 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1549 info->flags |= IEEE80211_TX_STATUS_EOSP |
1550 IEEE80211_TX_CTL_REQ_TX_STATUS;
1552 /* The standard isn't completely clear on this
1553 * as it says the more-data bit should be set
1554 * if there are more BUs. The QoS-Null frame
1555 * we're about to send isn't buffered yet, we
1556 * only create it below, but let's pretend it
1557 * was buffered just in case some clients only
1558 * expect more-data=0 when eosp=1.
1560 hdr->frame_control |=
1561 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1568 drv_allow_buffered_frames(local, sta, tids, num,
1571 ieee80211_add_pending_skbs(local, &pending);
1574 ieee80211_send_null_response(
1575 sdata, sta, find_highest_prio_tid(tids),
1578 sta_info_recalc_tim(sta);
1580 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1584 * We need to release a frame that is buffered somewhere in the
1585 * driver ... it'll have to handle that.
1586 * Note that the driver also has to check the number of frames
1587 * on the TIDs we're releasing from - if there are more than
1588 * n_frames it has to set the more-data bit (if we didn't ask
1589 * it to set it anyway due to other buffered frames); if there
1590 * are fewer than n_frames it has to make sure to adjust that
1591 * to allow the service period to end properly.
1593 drv_release_buffered_frames(local, sta, driver_release_tids,
1594 n_frames, reason, more_data);
1597 * Note that we don't recalculate the TIM bit here as it would
1598 * most likely have no effect at all unless the driver told us
1599 * that the TID(s) became empty before returning here from the
1601 * Either way, however, when the driver tells us that the TID(s)
1602 * became empty or we find that a txq became empty, we'll do the
1603 * TIM recalculation.
1606 if (!sta->sta.txq[0])
1609 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1610 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1612 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1615 sta_info_recalc_tim(sta);
1621 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1623 u8 ignore_for_response = sta->sta.uapsd_queues;
1626 * If all ACs are delivery-enabled then we should reply
1627 * from any of them, if only some are enabled we reply
1628 * only from the non-enabled ones.
1630 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1631 ignore_for_response = 0;
1633 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1634 IEEE80211_FRAME_RELEASE_PSPOLL);
1637 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1639 int n_frames = sta->sta.max_sp;
1640 u8 delivery_enabled = sta->sta.uapsd_queues;
1643 * If we ever grow support for TSPEC this might happen if
1644 * the TSPEC update from hostapd comes in between a trigger
1645 * frame setting WLAN_STA_UAPSD in the RX path and this
1646 * actually getting called.
1648 if (!delivery_enabled)
1651 switch (sta->sta.max_sp) {
1662 /* XXX: what is a good value? */
1667 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1668 IEEE80211_FRAME_RELEASE_UAPSD);
1671 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1672 struct ieee80211_sta *pubsta, bool block)
1674 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1676 trace_api_sta_block_awake(sta->local, pubsta, block);
1679 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1680 ieee80211_clear_fast_xmit(sta);
1684 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1687 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1688 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1689 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1690 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1691 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1692 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1693 /* must be asleep in this case */
1694 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1695 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1697 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1698 ieee80211_check_fast_xmit(sta);
1701 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1703 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1705 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1706 struct ieee80211_local *local = sta->local;
1708 trace_api_eosp(local, pubsta);
1710 clear_sta_flag(sta, WLAN_STA_SP);
1712 EXPORT_SYMBOL(ieee80211_sta_eosp);
1714 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1715 u8 tid, bool buffered)
1717 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1719 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1722 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1725 set_bit(tid, &sta->driver_buffered_tids);
1727 clear_bit(tid, &sta->driver_buffered_tids);
1729 sta_info_recalc_tim(sta);
1731 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1733 int sta_info_move_state(struct sta_info *sta,
1734 enum ieee80211_sta_state new_state)
1738 if (sta->sta_state == new_state)
1741 /* check allowed transitions first */
1743 switch (new_state) {
1744 case IEEE80211_STA_NONE:
1745 if (sta->sta_state != IEEE80211_STA_AUTH)
1748 case IEEE80211_STA_AUTH:
1749 if (sta->sta_state != IEEE80211_STA_NONE &&
1750 sta->sta_state != IEEE80211_STA_ASSOC)
1753 case IEEE80211_STA_ASSOC:
1754 if (sta->sta_state != IEEE80211_STA_AUTH &&
1755 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1758 case IEEE80211_STA_AUTHORIZED:
1759 if (sta->sta_state != IEEE80211_STA_ASSOC)
1763 WARN(1, "invalid state %d", new_state);
1767 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1768 sta->sta.addr, new_state);
1771 * notify the driver before the actual changes so it can
1772 * fail the transition
1774 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1775 int err = drv_sta_state(sta->local, sta->sdata, sta,
1776 sta->sta_state, new_state);
1781 /* reflect the change in all state variables */
1783 switch (new_state) {
1784 case IEEE80211_STA_NONE:
1785 if (sta->sta_state == IEEE80211_STA_AUTH)
1786 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1788 case IEEE80211_STA_AUTH:
1789 if (sta->sta_state == IEEE80211_STA_NONE)
1790 set_bit(WLAN_STA_AUTH, &sta->_flags);
1791 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1792 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1794 case IEEE80211_STA_ASSOC:
1795 if (sta->sta_state == IEEE80211_STA_AUTH) {
1796 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1797 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1798 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1799 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1800 !sta->sdata->u.vlan.sta))
1801 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1802 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1803 ieee80211_clear_fast_xmit(sta);
1806 case IEEE80211_STA_AUTHORIZED:
1807 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1808 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1809 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1810 !sta->sdata->u.vlan.sta))
1811 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1812 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1813 ieee80211_check_fast_xmit(sta);
1815 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1816 sta->sdata->vif.type == NL80211_IFTYPE_AP)
1817 cfg80211_send_layer2_update(sta->sdata->dev,
1824 sta->sta_state = new_state;
1829 u8 sta_info_tx_streams(struct sta_info *sta)
1831 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1834 if (!sta->sta.ht_cap.ht_supported)
1837 if (sta->sta.vht_cap.vht_supported) {
1840 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1842 for (i = 7; i >= 0; i--)
1843 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1844 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1848 if (ht_cap->mcs.rx_mask[3])
1850 else if (ht_cap->mcs.rx_mask[2])
1852 else if (ht_cap->mcs.rx_mask[1])
1857 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1860 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1861 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1864 static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1868 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
1869 rinfo->flags |= RATE_INFO_FLAGS_MCS;
1870 rinfo->mcs = sta->rx_stats.last_rate_idx;
1871 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
1872 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1873 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1874 rinfo->mcs = sta->rx_stats.last_rate_idx;
1876 struct ieee80211_supported_band *sband;
1877 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1880 sband = sta->local->hw.wiphy->bands[
1881 ieee80211_get_sdata_band(sta->sdata)];
1882 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
1883 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1886 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
1887 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1889 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
1890 rinfo->bw = RATE_INFO_BW_5;
1891 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
1892 rinfo->bw = RATE_INFO_BW_10;
1893 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
1894 rinfo->bw = RATE_INFO_BW_40;
1895 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1896 rinfo->bw = RATE_INFO_BW_80;
1897 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1898 rinfo->bw = RATE_INFO_BW_160;
1900 rinfo->bw = RATE_INFO_BW_20;
1903 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1905 struct ieee80211_sub_if_data *sdata = sta->sdata;
1906 struct ieee80211_local *local = sdata->local;
1907 struct rate_control_ref *ref = NULL;
1911 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1912 ref = local->rate_ctrl;
1914 sinfo->generation = sdata->local->sta_generation;
1916 /* do before driver, so beacon filtering drivers have a
1917 * chance to e.g. just add the number of filtered beacons
1918 * (or just modify the value entirely, of course)
1920 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1921 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1923 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1925 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1926 BIT(NL80211_STA_INFO_STA_FLAGS) |
1927 BIT(NL80211_STA_INFO_BSS_PARAM) |
1928 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1929 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1931 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1932 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1933 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1936 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
1937 sinfo->inactive_time =
1938 jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
1940 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1941 BIT(NL80211_STA_INFO_TX_BYTES)))) {
1942 sinfo->tx_bytes = 0;
1943 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1944 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
1945 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1948 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
1949 sinfo->tx_packets = 0;
1950 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1951 sinfo->tx_packets += sta->tx_stats.packets[ac];
1952 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
1955 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1956 BIT(NL80211_STA_INFO_RX_BYTES)))) {
1957 sinfo->rx_bytes = sta->rx_stats.bytes;
1958 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
1961 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1962 sinfo->rx_packets = sta->rx_stats.packets;
1963 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
1966 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1967 sinfo->tx_retries = sta->status_stats.retry_count;
1968 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
1971 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1972 sinfo->tx_failed = sta->status_stats.retry_failed;
1973 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
1976 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
1978 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1979 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1980 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1981 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1982 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1985 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
1986 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
1987 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1988 sinfo->signal = (s8)sta->rx_stats.last_signal;
1989 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1992 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
1994 -ewma_signal_read(&sta->rx_stats.avg_signal);
1995 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
1999 if (sta->rx_stats.chains &&
2000 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2001 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2002 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2003 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2005 sinfo->chains = sta->rx_stats.chains;
2006 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2007 sinfo->chain_signal[i] =
2008 sta->rx_stats.chain_signal_last[i];
2009 sinfo->chain_signal_avg[i] =
2010 -ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
2014 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2015 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2017 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2020 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2021 sta_set_rate_info_rx(sta, &sinfo->rxrate);
2022 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2025 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2026 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2027 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2029 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2030 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2031 tidstats->rx_msdu = sta->rx_stats.msdu[i];
2034 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2035 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2036 tidstats->tx_msdu = sta->tx_stats.msdu[i];
2039 if (!(tidstats->filled &
2040 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2041 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2043 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2044 tidstats->tx_msdu_retries =
2045 sta->status_stats.msdu_retries[i];
2048 if (!(tidstats->filled &
2049 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2050 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2052 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2053 tidstats->tx_msdu_failed =
2054 sta->status_stats.msdu_failed[i];
2058 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2059 #ifdef CONFIG_MAC80211_MESH
2060 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2061 BIT(NL80211_STA_INFO_PLID) |
2062 BIT(NL80211_STA_INFO_PLINK_STATE) |
2063 BIT(NL80211_STA_INFO_LOCAL_PM) |
2064 BIT(NL80211_STA_INFO_PEER_PM) |
2065 BIT(NL80211_STA_INFO_NONPEER_PM);
2067 sinfo->llid = sta->mesh->llid;
2068 sinfo->plid = sta->mesh->plid;
2069 sinfo->plink_state = sta->mesh->plink_state;
2070 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2071 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2072 sinfo->t_offset = sta->mesh->t_offset;
2074 sinfo->local_pm = sta->mesh->local_pm;
2075 sinfo->peer_pm = sta->mesh->peer_pm;
2076 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2080 sinfo->bss_param.flags = 0;
2081 if (sdata->vif.bss_conf.use_cts_prot)
2082 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2083 if (sdata->vif.bss_conf.use_short_preamble)
2084 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2085 if (sdata->vif.bss_conf.use_short_slot)
2086 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2087 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2088 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2090 sinfo->sta_flags.set = 0;
2091 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2092 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2093 BIT(NL80211_STA_FLAG_WME) |
2094 BIT(NL80211_STA_FLAG_MFP) |
2095 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2096 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2097 BIT(NL80211_STA_FLAG_TDLS_PEER);
2098 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2099 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2100 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2101 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2103 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2104 if (test_sta_flag(sta, WLAN_STA_MFP))
2105 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2106 if (test_sta_flag(sta, WLAN_STA_AUTH))
2107 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2108 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2109 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2110 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2111 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2113 /* check if the driver has a SW RC implementation */
2114 if (ref && ref->ops->get_expected_throughput)
2115 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2117 thr = drv_get_expected_throughput(local, &sta->sta);
2120 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2121 sinfo->expected_throughput = thr;