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) 2015 Intel Deutschland GmbH
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 .insecure_elasticity = true, /* Disable chain-length checks. */
71 .automatic_shrinking = true,
72 .head_offset = offsetof(struct sta_info, hash_node),
73 .key_offset = offsetof(struct sta_info, addr),
75 .hashfn = sta_addr_hash,
76 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
79 /* Caller must hold local->sta_mtx */
80 static int sta_info_hash_del(struct ieee80211_local *local,
83 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
87 static void __cleanup_single_sta(struct sta_info *sta)
90 struct tid_ampdu_tx *tid_tx;
91 struct ieee80211_sub_if_data *sdata = sta->sdata;
92 struct ieee80211_local *local = sdata->local;
95 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
96 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
97 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
98 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
99 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
100 ps = &sdata->bss->ps;
101 else if (ieee80211_vif_is_mesh(&sdata->vif))
102 ps = &sdata->u.mesh.ps;
106 clear_sta_flag(sta, WLAN_STA_PS_STA);
107 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
108 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
110 atomic_dec(&ps->num_sta_ps);
113 if (sta->sta.txq[0]) {
114 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
115 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
116 int n = skb_queue_len(&txqi->queue);
118 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
119 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
124 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
125 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
127 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
130 if (ieee80211_vif_is_mesh(&sdata->vif))
131 mesh_sta_cleanup(sta);
133 cancel_work_sync(&sta->drv_deliver_wk);
136 * Destroy aggregation state here. It would be nice to wait for the
137 * driver to finish aggregation stop and then clean up, but for now
138 * drivers have to handle aggregation stop being requested, followed
139 * directly by station destruction.
141 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
142 kfree(sta->ampdu_mlme.tid_start_tx[i]);
143 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
146 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
151 static void cleanup_single_sta(struct sta_info *sta)
153 struct ieee80211_sub_if_data *sdata = sta->sdata;
154 struct ieee80211_local *local = sdata->local;
156 __cleanup_single_sta(sta);
157 sta_info_free(local, sta);
160 /* protected by RCU */
161 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
164 struct ieee80211_local *local = sdata->local;
165 struct sta_info *sta;
166 struct rhash_head *tmp;
167 const struct bucket_table *tbl;
170 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
172 for_each_sta_info(local, tbl, addr, sta, tmp) {
173 if (sta->sdata == sdata) {
175 /* this is safe as the caller must already hold
176 * another rcu read section or the mutex
186 * Get sta info either from the specified interface
187 * or from one of its vlans
189 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
192 struct ieee80211_local *local = sdata->local;
193 struct sta_info *sta;
194 struct rhash_head *tmp;
195 const struct bucket_table *tbl;
198 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
200 for_each_sta_info(local, tbl, addr, sta, tmp) {
201 if (sta->sdata == sdata ||
202 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
204 /* this is safe as the caller must already hold
205 * another rcu read section or the mutex
214 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
217 struct ieee80211_local *local = sdata->local;
218 struct sta_info *sta;
221 list_for_each_entry_rcu(sta, &local->sta_list, list) {
222 if (sdata != sta->sdata)
235 * sta_info_free - free STA
237 * @local: pointer to the global information
238 * @sta: STA info to free
240 * This function must undo everything done by sta_info_alloc()
241 * that may happen before sta_info_insert(). It may only be
242 * called when sta_info_insert() has not been attempted (and
243 * if that fails, the station is freed anyway.)
245 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
248 rate_control_free_sta(sta);
250 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
253 kfree(to_txq_info(sta->sta.txq[0]));
254 kfree(rcu_dereference_raw(sta->sta.rates));
255 #ifdef CONFIG_MAC80211_MESH
261 /* Caller must hold local->sta_mtx */
262 static int sta_info_hash_add(struct ieee80211_local *local,
263 struct sta_info *sta)
265 return rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
269 static void sta_deliver_ps_frames(struct work_struct *wk)
271 struct sta_info *sta;
273 sta = container_of(wk, struct sta_info, drv_deliver_wk);
279 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
280 ieee80211_sta_ps_deliver_wakeup(sta);
281 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
282 ieee80211_sta_ps_deliver_poll_response(sta);
283 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
284 ieee80211_sta_ps_deliver_uapsd(sta);
288 static int sta_prepare_rate_control(struct ieee80211_local *local,
289 struct sta_info *sta, gfp_t gfp)
291 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
294 sta->rate_ctrl = local->rate_ctrl;
295 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
297 if (!sta->rate_ctrl_priv)
303 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
304 const u8 *addr, gfp_t gfp)
306 struct ieee80211_local *local = sdata->local;
307 struct ieee80211_hw *hw = &local->hw;
308 struct sta_info *sta;
311 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
315 spin_lock_init(&sta->lock);
316 spin_lock_init(&sta->ps_lock);
317 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
318 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
319 mutex_init(&sta->ampdu_mlme.mtx);
320 #ifdef CONFIG_MAC80211_MESH
321 if (ieee80211_vif_is_mesh(&sdata->vif)) {
322 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
325 spin_lock_init(&sta->mesh->plink_lock);
326 if (ieee80211_vif_is_mesh(&sdata->vif) &&
327 !sdata->u.mesh.user_mpm)
328 init_timer(&sta->mesh->plink_timer);
329 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
333 memcpy(sta->addr, addr, ETH_ALEN);
334 memcpy(sta->sta.addr, addr, ETH_ALEN);
337 sta->rx_stats.last_rx = jiffies;
339 sta->sta_state = IEEE80211_STA_NONE;
341 /* Mark TID as unreserved */
342 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
344 sta->last_connected = ktime_get_seconds();
345 ewma_signal_init(&sta->rx_stats.avg_signal);
346 for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
347 ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
349 if (local->ops->wake_tx_queue) {
351 int size = sizeof(struct txq_info) +
352 ALIGN(hw->txq_data_size, sizeof(void *));
354 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
358 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
359 struct txq_info *txq = txq_data + i * size;
361 ieee80211_init_tx_queue(sdata, sta, txq, i);
365 if (sta_prepare_rate_control(local, sta, gfp))
368 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
370 * timer_to_tid must be initialized with identity mapping
371 * to enable session_timer's data differentiation. See
372 * sta_rx_agg_session_timer_expired for usage.
374 sta->timer_to_tid[i] = i;
376 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
377 skb_queue_head_init(&sta->ps_tx_buf[i]);
378 skb_queue_head_init(&sta->tx_filtered[i]);
381 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
382 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
384 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
385 if (sdata->vif.type == NL80211_IFTYPE_AP ||
386 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
387 struct ieee80211_supported_band *sband =
388 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
389 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
390 IEEE80211_HT_CAP_SM_PS_SHIFT;
392 * Assume that hostapd advertises our caps in the beacon and
393 * this is the known_smps_mode for a station that just assciated
396 case WLAN_HT_SMPS_CONTROL_DISABLED:
397 sta->known_smps_mode = IEEE80211_SMPS_OFF;
399 case WLAN_HT_SMPS_CONTROL_STATIC:
400 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
402 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
403 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
410 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
416 kfree(to_txq_info(sta->sta.txq[0]));
418 #ifdef CONFIG_MAC80211_MESH
425 static int sta_info_insert_check(struct sta_info *sta)
427 struct ieee80211_sub_if_data *sdata = sta->sdata;
430 * Can't be a WARN_ON because it can be triggered through a race:
431 * something inserts a STA (on one CPU) without holding the RTNL
432 * and another CPU turns off the net device.
434 if (unlikely(!ieee80211_sdata_running(sdata)))
437 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
438 is_multicast_ether_addr(sta->sta.addr)))
441 /* Strictly speaking this isn't necessary as we hold the mutex, but
442 * the rhashtable code can't really deal with that distinction. We
443 * do require the mutex for correctness though.
446 lockdep_assert_held(&sdata->local->sta_mtx);
447 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
448 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
457 static int sta_info_insert_drv_state(struct ieee80211_local *local,
458 struct ieee80211_sub_if_data *sdata,
459 struct sta_info *sta)
461 enum ieee80211_sta_state state;
464 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
465 err = drv_sta_state(local, sdata, sta, state, state + 1);
472 * Drivers using legacy sta_add/sta_remove callbacks only
473 * get uploaded set to true after sta_add is called.
475 if (!local->ops->sta_add)
476 sta->uploaded = true;
480 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
482 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
483 sta->sta.addr, state + 1, err);
487 /* unwind on error */
488 for (; state > IEEE80211_STA_NOTEXIST; state--)
489 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
495 * should be called with sta_mtx locked
496 * this function replaces the mutex lock
499 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
501 struct ieee80211_local *local = sta->local;
502 struct ieee80211_sub_if_data *sdata = sta->sdata;
503 struct station_info *sinfo;
506 lockdep_assert_held(&local->sta_mtx);
508 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
514 /* check if STA exists already */
515 if (sta_info_get_bss(sdata, sta->sta.addr)) {
521 local->sta_generation++;
524 /* simplify things and don't accept BA sessions yet */
525 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
527 /* make the station visible */
528 err = sta_info_hash_add(local, sta);
532 list_add_tail_rcu(&sta->list, &local->sta_list);
535 err = sta_info_insert_drv_state(local, sdata, sta);
539 set_sta_flag(sta, WLAN_STA_INSERTED);
540 /* accept BA sessions now */
541 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
543 ieee80211_sta_debugfs_add(sta);
544 rate_control_add_sta_debugfs(sta);
546 sinfo->generation = local->sta_generation;
547 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
550 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
552 /* move reference to rcu-protected */
554 mutex_unlock(&local->sta_mtx);
556 if (ieee80211_vif_is_mesh(&sdata->vif))
557 mesh_accept_plinks_update(sdata);
561 sta_info_hash_del(local, sta);
562 list_del_rcu(&sta->list);
566 __cleanup_single_sta(sta);
568 mutex_unlock(&local->sta_mtx);
574 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
576 struct ieee80211_local *local = sta->local;
581 mutex_lock(&local->sta_mtx);
583 err = sta_info_insert_check(sta);
585 mutex_unlock(&local->sta_mtx);
590 err = sta_info_insert_finish(sta);
596 sta_info_free(local, sta);
600 int sta_info_insert(struct sta_info *sta)
602 int err = sta_info_insert_rcu(sta);
609 static inline void __bss_tim_set(u8 *tim, u16 id)
612 * This format has been mandated by the IEEE specifications,
613 * so this line may not be changed to use the __set_bit() format.
615 tim[id / 8] |= (1 << (id % 8));
618 static inline void __bss_tim_clear(u8 *tim, u16 id)
621 * This format has been mandated by the IEEE specifications,
622 * so this line may not be changed to use the __clear_bit() format.
624 tim[id / 8] &= ~(1 << (id % 8));
627 static inline bool __bss_tim_get(u8 *tim, u16 id)
630 * This format has been mandated by the IEEE specifications,
631 * so this line may not be changed to use the test_bit() format.
633 return tim[id / 8] & (1 << (id % 8));
636 static unsigned long ieee80211_tids_for_ac(int ac)
638 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
640 case IEEE80211_AC_VO:
641 return BIT(6) | BIT(7);
642 case IEEE80211_AC_VI:
643 return BIT(4) | BIT(5);
644 case IEEE80211_AC_BE:
645 return BIT(0) | BIT(3);
646 case IEEE80211_AC_BK:
647 return BIT(1) | BIT(2);
654 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
656 struct ieee80211_local *local = sta->local;
658 bool indicate_tim = false;
659 u8 ignore_for_tim = sta->sta.uapsd_queues;
661 u16 id = sta->sta.aid;
663 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
664 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
665 if (WARN_ON_ONCE(!sta->sdata->bss))
668 ps = &sta->sdata->bss->ps;
669 #ifdef CONFIG_MAC80211_MESH
670 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
671 ps = &sta->sdata->u.mesh.ps;
677 /* No need to do anything if the driver does all */
678 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
685 * If all ACs are delivery-enabled then we should build
686 * the TIM bit for all ACs anyway; if only some are then
687 * we ignore those and build the TIM bit using only the
690 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
694 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
696 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
699 if (ignore_for_tim & BIT(ac))
702 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
703 !skb_queue_empty(&sta->ps_tx_buf[ac]);
707 tids = ieee80211_tids_for_ac(ac);
710 sta->driver_buffered_tids & tids;
712 sta->txq_buffered_tids & tids;
716 spin_lock_bh(&local->tim_lock);
718 if (indicate_tim == __bss_tim_get(ps->tim, id))
722 __bss_tim_set(ps->tim, id);
724 __bss_tim_clear(ps->tim, id);
726 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
727 local->tim_in_locked_section = true;
728 drv_set_tim(local, &sta->sta, indicate_tim);
729 local->tim_in_locked_section = false;
733 spin_unlock_bh(&local->tim_lock);
736 void sta_info_recalc_tim(struct sta_info *sta)
738 __sta_info_recalc_tim(sta, false);
741 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
743 struct ieee80211_tx_info *info;
749 info = IEEE80211_SKB_CB(skb);
751 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
752 timeout = (sta->listen_interval *
753 sta->sdata->vif.bss_conf.beacon_int *
755 if (timeout < STA_TX_BUFFER_EXPIRE)
756 timeout = STA_TX_BUFFER_EXPIRE;
757 return time_after(jiffies, info->control.jiffies + timeout);
761 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
762 struct sta_info *sta, int ac)
768 * First check for frames that should expire on the filtered
769 * queue. Frames here were rejected by the driver and are on
770 * a separate queue to avoid reordering with normal PS-buffered
771 * frames. They also aren't accounted for right now in the
772 * total_ps_buffered counter.
775 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
776 skb = skb_peek(&sta->tx_filtered[ac]);
777 if (sta_info_buffer_expired(sta, skb))
778 skb = __skb_dequeue(&sta->tx_filtered[ac]);
781 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
784 * Frames are queued in order, so if this one
785 * hasn't expired yet we can stop testing. If
786 * we actually reached the end of the queue we
787 * also need to stop, of course.
791 ieee80211_free_txskb(&local->hw, skb);
795 * Now also check the normal PS-buffered queue, this will
796 * only find something if the filtered queue was emptied
797 * since the filtered frames are all before the normal PS
801 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
802 skb = skb_peek(&sta->ps_tx_buf[ac]);
803 if (sta_info_buffer_expired(sta, skb))
804 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
807 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
810 * frames are queued in order, so if this one
811 * hasn't expired yet (or we reached the end of
812 * the queue) we can stop testing
817 local->total_ps_buffered--;
818 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
820 ieee80211_free_txskb(&local->hw, skb);
824 * Finally, recalculate the TIM bit for this station -- it might
825 * now be clear because the station was too slow to retrieve its
828 sta_info_recalc_tim(sta);
831 * Return whether there are any frames still buffered, this is
832 * used to check whether the cleanup timer still needs to run,
833 * if there are no frames we don't need to rearm the timer.
835 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
836 skb_queue_empty(&sta->tx_filtered[ac]));
839 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
840 struct sta_info *sta)
842 bool have_buffered = false;
845 /* This is only necessary for stations on BSS/MBSS interfaces */
846 if (!sta->sdata->bss &&
847 !ieee80211_vif_is_mesh(&sta->sdata->vif))
850 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
852 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
854 return have_buffered;
857 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
859 struct ieee80211_local *local;
860 struct ieee80211_sub_if_data *sdata;
871 lockdep_assert_held(&local->sta_mtx);
874 * Before removing the station from the driver and
875 * rate control, it might still start new aggregation
876 * sessions -- block that to make sure the tear-down
877 * will be sufficient.
879 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
880 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
882 ret = sta_info_hash_del(local, sta);
887 * for TDLS peers, make sure to return to the base channel before
890 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
891 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
892 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
895 list_del_rcu(&sta->list);
898 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
900 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
901 rcu_access_pointer(sdata->u.vlan.sta) == sta)
902 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
907 static void __sta_info_destroy_part2(struct sta_info *sta)
909 struct ieee80211_local *local = sta->local;
910 struct ieee80211_sub_if_data *sdata = sta->sdata;
911 struct station_info *sinfo;
915 * NOTE: This assumes at least synchronize_net() was done
916 * after _part1 and before _part2!
920 lockdep_assert_held(&local->sta_mtx);
922 /* now keys can no longer be reached */
923 ieee80211_free_sta_keys(local, sta);
925 /* disable TIM bit - last chance to tell driver */
926 __sta_info_recalc_tim(sta, true);
931 local->sta_generation++;
933 while (sta->sta_state > IEEE80211_STA_NONE) {
934 ret = sta_info_move_state(sta, sta->sta_state - 1);
942 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
943 IEEE80211_STA_NOTEXIST);
944 WARN_ON_ONCE(ret != 0);
947 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
949 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
951 sta_set_sinfo(sta, sinfo);
952 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
955 rate_control_remove_sta_debugfs(sta);
956 ieee80211_sta_debugfs_remove(sta);
958 cleanup_single_sta(sta);
961 int __must_check __sta_info_destroy(struct sta_info *sta)
963 int err = __sta_info_destroy_part1(sta);
970 __sta_info_destroy_part2(sta);
975 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
977 struct sta_info *sta;
980 mutex_lock(&sdata->local->sta_mtx);
981 sta = sta_info_get(sdata, addr);
982 ret = __sta_info_destroy(sta);
983 mutex_unlock(&sdata->local->sta_mtx);
988 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
991 struct sta_info *sta;
994 mutex_lock(&sdata->local->sta_mtx);
995 sta = sta_info_get_bss(sdata, addr);
996 ret = __sta_info_destroy(sta);
997 mutex_unlock(&sdata->local->sta_mtx);
1002 static void sta_info_cleanup(unsigned long data)
1004 struct ieee80211_local *local = (struct ieee80211_local *) data;
1005 struct sta_info *sta;
1006 bool timer_needed = false;
1009 list_for_each_entry_rcu(sta, &local->sta_list, list)
1010 if (sta_info_cleanup_expire_buffered(local, sta))
1011 timer_needed = true;
1014 if (local->quiescing)
1020 mod_timer(&local->sta_cleanup,
1021 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1024 u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1026 return jhash(key, ETH_ALEN, seed);
1029 int sta_info_init(struct ieee80211_local *local)
1033 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1037 spin_lock_init(&local->tim_lock);
1038 mutex_init(&local->sta_mtx);
1039 INIT_LIST_HEAD(&local->sta_list);
1041 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1042 (unsigned long)local);
1046 void sta_info_stop(struct ieee80211_local *local)
1048 del_timer_sync(&local->sta_cleanup);
1049 rhashtable_destroy(&local->sta_hash);
1053 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1055 struct ieee80211_local *local = sdata->local;
1056 struct sta_info *sta, *tmp;
1057 LIST_HEAD(free_list);
1062 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1063 WARN_ON(vlans && !sdata->bss);
1065 mutex_lock(&local->sta_mtx);
1066 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1067 if (sdata == sta->sdata ||
1068 (vlans && sdata->bss == sta->sdata->bss)) {
1069 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1070 list_add(&sta->free_list, &free_list);
1075 if (!list_empty(&free_list)) {
1077 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1078 __sta_info_destroy_part2(sta);
1080 mutex_unlock(&local->sta_mtx);
1085 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1086 unsigned long exp_time)
1088 struct ieee80211_local *local = sdata->local;
1089 struct sta_info *sta, *tmp;
1091 mutex_lock(&local->sta_mtx);
1093 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1094 if (sdata != sta->sdata)
1097 if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
1098 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1101 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1102 test_sta_flag(sta, WLAN_STA_PS_STA))
1103 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1105 WARN_ON(__sta_info_destroy(sta));
1109 mutex_unlock(&local->sta_mtx);
1112 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1114 const u8 *localaddr)
1116 struct ieee80211_local *local = hw_to_local(hw);
1117 struct sta_info *sta;
1118 struct rhash_head *tmp;
1119 const struct bucket_table *tbl;
1121 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1124 * Just return a random station if localaddr is NULL
1125 * ... first in list.
1127 for_each_sta_info(local, tbl, addr, sta, tmp) {
1129 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1138 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1140 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1143 struct sta_info *sta;
1148 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1157 EXPORT_SYMBOL(ieee80211_find_sta);
1159 /* powersave support code */
1160 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1162 struct ieee80211_sub_if_data *sdata = sta->sdata;
1163 struct ieee80211_local *local = sdata->local;
1164 struct sk_buff_head pending;
1165 int filtered = 0, buffered = 0, ac, i;
1166 unsigned long flags;
1169 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1170 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1173 if (sdata->vif.type == NL80211_IFTYPE_AP)
1174 ps = &sdata->bss->ps;
1175 else if (ieee80211_vif_is_mesh(&sdata->vif))
1176 ps = &sdata->u.mesh.ps;
1180 clear_sta_flag(sta, WLAN_STA_SP);
1182 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1183 sta->driver_buffered_tids = 0;
1184 sta->txq_buffered_tids = 0;
1186 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1187 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1189 if (sta->sta.txq[0]) {
1190 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1191 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1193 if (!skb_queue_len(&txqi->queue))
1196 drv_wake_tx_queue(local, txqi);
1200 skb_queue_head_init(&pending);
1202 /* sync with ieee80211_tx_h_unicast_ps_buf */
1203 spin_lock(&sta->ps_lock);
1204 /* Send all buffered frames to the station */
1205 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1206 int count = skb_queue_len(&pending), tmp;
1208 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1209 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1210 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1211 tmp = skb_queue_len(&pending);
1212 filtered += tmp - count;
1215 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1216 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1217 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1218 tmp = skb_queue_len(&pending);
1219 buffered += tmp - count;
1222 ieee80211_add_pending_skbs(local, &pending);
1224 /* now we're no longer in the deliver code */
1225 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1227 /* The station might have polled and then woken up before we responded,
1228 * so clear these flags now to avoid them sticking around.
1230 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1231 clear_sta_flag(sta, WLAN_STA_UAPSD);
1232 spin_unlock(&sta->ps_lock);
1234 atomic_dec(&ps->num_sta_ps);
1236 /* This station just woke up and isn't aware of our SMPS state */
1237 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1238 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1239 sdata->smps_mode) &&
1240 sta->known_smps_mode != sdata->bss->req_smps &&
1241 sta_info_tx_streams(sta) != 1) {
1243 "%pM just woke up and MIMO capable - update SMPS\n",
1245 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1247 sdata->vif.bss_conf.bssid);
1250 local->total_ps_buffered -= buffered;
1252 sta_info_recalc_tim(sta);
1255 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1256 sta->sta.addr, sta->sta.aid, filtered, buffered);
1258 ieee80211_check_fast_xmit(sta);
1261 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1262 enum ieee80211_frame_release_type reason,
1263 bool call_driver, bool more_data)
1265 struct ieee80211_sub_if_data *sdata = sta->sdata;
1266 struct ieee80211_local *local = sdata->local;
1267 struct ieee80211_qos_hdr *nullfunc;
1268 struct sk_buff *skb;
1269 int size = sizeof(*nullfunc);
1271 bool qos = sta->sta.wme;
1272 struct ieee80211_tx_info *info;
1273 struct ieee80211_chanctx_conf *chanctx_conf;
1276 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1277 IEEE80211_STYPE_QOS_NULLFUNC |
1278 IEEE80211_FCTL_FROMDS);
1281 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1282 IEEE80211_STYPE_NULLFUNC |
1283 IEEE80211_FCTL_FROMDS);
1286 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1290 skb_reserve(skb, local->hw.extra_tx_headroom);
1292 nullfunc = (void *) skb_put(skb, size);
1293 nullfunc->frame_control = fc;
1294 nullfunc->duration_id = 0;
1295 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1296 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1297 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1298 nullfunc->seq_ctrl = 0;
1300 skb->priority = tid;
1301 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1303 nullfunc->qos_ctrl = cpu_to_le16(tid);
1305 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1306 nullfunc->qos_ctrl |=
1307 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1309 nullfunc->frame_control |=
1310 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1314 info = IEEE80211_SKB_CB(skb);
1317 * Tell TX path to send this frame even though the
1318 * STA may still remain is PS mode after this frame
1319 * exchange. Also set EOSP to indicate this packet
1320 * ends the poll/service period.
1322 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1323 IEEE80211_TX_STATUS_EOSP |
1324 IEEE80211_TX_CTL_REQ_TX_STATUS;
1326 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1329 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1332 skb->dev = sdata->dev;
1335 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1336 if (WARN_ON(!chanctx_conf)) {
1342 info->band = chanctx_conf->def.chan->band;
1343 ieee80211_xmit(sdata, sta, skb);
1347 static int find_highest_prio_tid(unsigned long tids)
1349 /* lower 3 TIDs aren't ordered perfectly */
1351 return fls(tids) - 1;
1352 /* TID 0 is BE just like TID 3 */
1355 return fls(tids) - 1;
1358 /* Indicates if the MORE_DATA bit should be set in the last
1359 * frame obtained by ieee80211_sta_ps_get_frames.
1360 * Note that driver_release_tids is relevant only if
1361 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1364 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1365 enum ieee80211_frame_release_type reason,
1366 unsigned long driver_release_tids)
1370 /* If the driver has data on more than one TID then
1371 * certainly there's more data if we release just a
1372 * single frame now (from a single TID). This will
1373 * only happen for PS-Poll.
1375 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1376 hweight16(driver_release_tids) > 1)
1379 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1380 if (ignored_acs & BIT(ac))
1383 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1384 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1392 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1393 enum ieee80211_frame_release_type reason,
1394 struct sk_buff_head *frames,
1395 unsigned long *driver_release_tids)
1397 struct ieee80211_sub_if_data *sdata = sta->sdata;
1398 struct ieee80211_local *local = sdata->local;
1401 /* Get response frame(s) and more data bit for the last one. */
1402 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1405 if (ignored_acs & BIT(ac))
1408 tids = ieee80211_tids_for_ac(ac);
1410 /* if we already have frames from software, then we can't also
1411 * release from hardware queues
1413 if (skb_queue_empty(frames)) {
1414 *driver_release_tids |=
1415 sta->driver_buffered_tids & tids;
1416 *driver_release_tids |= sta->txq_buffered_tids & tids;
1419 if (!*driver_release_tids) {
1420 struct sk_buff *skb;
1422 while (n_frames > 0) {
1423 skb = skb_dequeue(&sta->tx_filtered[ac]);
1426 &sta->ps_tx_buf[ac]);
1428 local->total_ps_buffered--;
1433 __skb_queue_tail(frames, skb);
1437 /* If we have more frames buffered on this AC, then abort the
1438 * loop since we can't send more data from other ACs before
1439 * the buffered frames from this.
1441 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1442 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1448 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1449 int n_frames, u8 ignored_acs,
1450 enum ieee80211_frame_release_type reason)
1452 struct ieee80211_sub_if_data *sdata = sta->sdata;
1453 struct ieee80211_local *local = sdata->local;
1454 unsigned long driver_release_tids = 0;
1455 struct sk_buff_head frames;
1458 /* Service or PS-Poll period starts */
1459 set_sta_flag(sta, WLAN_STA_SP);
1461 __skb_queue_head_init(&frames);
1463 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1464 &frames, &driver_release_tids);
1466 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1468 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1469 driver_release_tids =
1470 BIT(find_highest_prio_tid(driver_release_tids));
1472 if (skb_queue_empty(&frames) && !driver_release_tids) {
1476 * For PS-Poll, this can only happen due to a race condition
1477 * when we set the TIM bit and the station notices it, but
1478 * before it can poll for the frame we expire it.
1480 * For uAPSD, this is said in the standard (11.2.1.5 h):
1481 * At each unscheduled SP for a non-AP STA, the AP shall
1482 * attempt to transmit at least one MSDU or MMPDU, but no
1483 * more than the value specified in the Max SP Length field
1484 * in the QoS Capability element from delivery-enabled ACs,
1485 * that are destined for the non-AP STA.
1487 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1490 /* This will evaluate to 1, 3, 5 or 7. */
1491 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1493 ieee80211_send_null_response(sta, tid, reason, true, false);
1494 } else if (!driver_release_tids) {
1495 struct sk_buff_head pending;
1496 struct sk_buff *skb;
1499 bool need_null = false;
1501 skb_queue_head_init(&pending);
1503 while ((skb = __skb_dequeue(&frames))) {
1504 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1505 struct ieee80211_hdr *hdr = (void *) skb->data;
1511 * Tell TX path to send this frame even though the
1512 * STA may still remain is PS mode after this frame
1515 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1516 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1519 * Use MoreData flag to indicate whether there are
1520 * more buffered frames for this STA
1522 if (more_data || !skb_queue_empty(&frames))
1523 hdr->frame_control |=
1524 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1526 hdr->frame_control &=
1527 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1529 if (ieee80211_is_data_qos(hdr->frame_control) ||
1530 ieee80211_is_qos_nullfunc(hdr->frame_control))
1531 qoshdr = ieee80211_get_qos_ctl(hdr);
1533 tids |= BIT(skb->priority);
1535 __skb_queue_tail(&pending, skb);
1537 /* end service period after last frame or add one */
1538 if (!skb_queue_empty(&frames))
1541 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1542 /* for PS-Poll, there's only one frame */
1543 info->flags |= IEEE80211_TX_STATUS_EOSP |
1544 IEEE80211_TX_CTL_REQ_TX_STATUS;
1548 /* For uAPSD, things are a bit more complicated. If the
1549 * last frame has a QoS header (i.e. is a QoS-data or
1550 * QoS-nulldata frame) then just set the EOSP bit there
1552 * If the frame doesn't have a QoS header (which means
1553 * it should be a bufferable MMPDU) then we can't set
1554 * the EOSP bit in the QoS header; add a QoS-nulldata
1555 * frame to the list to send it after the MMPDU.
1557 * Note that this code is only in the mac80211-release
1558 * code path, we assume that the driver will not buffer
1559 * anything but QoS-data frames, or if it does, will
1560 * create the QoS-nulldata frame by itself if needed.
1562 * Cf. 802.11-2012 10.2.1.10 (c).
1565 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1567 info->flags |= IEEE80211_TX_STATUS_EOSP |
1568 IEEE80211_TX_CTL_REQ_TX_STATUS;
1570 /* The standard isn't completely clear on this
1571 * as it says the more-data bit should be set
1572 * if there are more BUs. The QoS-Null frame
1573 * we're about to send isn't buffered yet, we
1574 * only create it below, but let's pretend it
1575 * was buffered just in case some clients only
1576 * expect more-data=0 when eosp=1.
1578 hdr->frame_control |=
1579 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1586 drv_allow_buffered_frames(local, sta, tids, num,
1589 ieee80211_add_pending_skbs(local, &pending);
1592 ieee80211_send_null_response(
1593 sta, find_highest_prio_tid(tids),
1594 reason, false, false);
1596 sta_info_recalc_tim(sta);
1598 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1602 * We need to release a frame that is buffered somewhere in the
1603 * driver ... it'll have to handle that.
1604 * Note that the driver also has to check the number of frames
1605 * on the TIDs we're releasing from - if there are more than
1606 * n_frames it has to set the more-data bit (if we didn't ask
1607 * it to set it anyway due to other buffered frames); if there
1608 * are fewer than n_frames it has to make sure to adjust that
1609 * to allow the service period to end properly.
1611 drv_release_buffered_frames(local, sta, driver_release_tids,
1612 n_frames, reason, more_data);
1615 * Note that we don't recalculate the TIM bit here as it would
1616 * most likely have no effect at all unless the driver told us
1617 * that the TID(s) became empty before returning here from the
1619 * Either way, however, when the driver tells us that the TID(s)
1620 * became empty or we find that a txq became empty, we'll do the
1621 * TIM recalculation.
1624 if (!sta->sta.txq[0])
1627 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1628 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1630 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1633 sta_info_recalc_tim(sta);
1639 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1641 u8 ignore_for_response = sta->sta.uapsd_queues;
1644 * If all ACs are delivery-enabled then we should reply
1645 * from any of them, if only some are enabled we reply
1646 * only from the non-enabled ones.
1648 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1649 ignore_for_response = 0;
1651 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1652 IEEE80211_FRAME_RELEASE_PSPOLL);
1655 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1657 int n_frames = sta->sta.max_sp;
1658 u8 delivery_enabled = sta->sta.uapsd_queues;
1661 * If we ever grow support for TSPEC this might happen if
1662 * the TSPEC update from hostapd comes in between a trigger
1663 * frame setting WLAN_STA_UAPSD in the RX path and this
1664 * actually getting called.
1666 if (!delivery_enabled)
1669 switch (sta->sta.max_sp) {
1680 /* XXX: what is a good value? */
1685 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1686 IEEE80211_FRAME_RELEASE_UAPSD);
1689 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1690 struct ieee80211_sta *pubsta, bool block)
1692 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1694 trace_api_sta_block_awake(sta->local, pubsta, block);
1697 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1698 ieee80211_clear_fast_xmit(sta);
1702 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1705 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1706 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1707 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1708 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1709 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1710 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1711 /* must be asleep in this case */
1712 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1713 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1715 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1716 ieee80211_check_fast_xmit(sta);
1719 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1721 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1723 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1724 struct ieee80211_local *local = sta->local;
1726 trace_api_eosp(local, pubsta);
1728 clear_sta_flag(sta, WLAN_STA_SP);
1730 EXPORT_SYMBOL(ieee80211_sta_eosp);
1732 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1734 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1735 enum ieee80211_frame_release_type reason;
1738 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1740 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1741 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1744 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1746 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1748 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1749 u8 tid, bool buffered)
1751 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1753 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1756 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1759 set_bit(tid, &sta->driver_buffered_tids);
1761 clear_bit(tid, &sta->driver_buffered_tids);
1763 sta_info_recalc_tim(sta);
1765 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1767 int sta_info_move_state(struct sta_info *sta,
1768 enum ieee80211_sta_state new_state)
1772 if (sta->sta_state == new_state)
1775 /* check allowed transitions first */
1777 switch (new_state) {
1778 case IEEE80211_STA_NONE:
1779 if (sta->sta_state != IEEE80211_STA_AUTH)
1782 case IEEE80211_STA_AUTH:
1783 if (sta->sta_state != IEEE80211_STA_NONE &&
1784 sta->sta_state != IEEE80211_STA_ASSOC)
1787 case IEEE80211_STA_ASSOC:
1788 if (sta->sta_state != IEEE80211_STA_AUTH &&
1789 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1792 case IEEE80211_STA_AUTHORIZED:
1793 if (sta->sta_state != IEEE80211_STA_ASSOC)
1797 WARN(1, "invalid state %d", new_state);
1801 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1802 sta->sta.addr, new_state);
1805 * notify the driver before the actual changes so it can
1806 * fail the transition
1808 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1809 int err = drv_sta_state(sta->local, sta->sdata, sta,
1810 sta->sta_state, new_state);
1815 /* reflect the change in all state variables */
1817 switch (new_state) {
1818 case IEEE80211_STA_NONE:
1819 if (sta->sta_state == IEEE80211_STA_AUTH)
1820 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1822 case IEEE80211_STA_AUTH:
1823 if (sta->sta_state == IEEE80211_STA_NONE) {
1824 set_bit(WLAN_STA_AUTH, &sta->_flags);
1825 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1826 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1827 ieee80211_recalc_min_chandef(sta->sdata);
1830 case IEEE80211_STA_ASSOC:
1831 if (sta->sta_state == IEEE80211_STA_AUTH) {
1832 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1833 ieee80211_recalc_min_chandef(sta->sdata);
1834 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1835 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1836 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1837 !sta->sdata->u.vlan.sta))
1838 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1839 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1840 ieee80211_clear_fast_xmit(sta);
1843 case IEEE80211_STA_AUTHORIZED:
1844 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1845 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1846 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1847 !sta->sdata->u.vlan.sta))
1848 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1849 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1850 ieee80211_check_fast_xmit(sta);
1857 sta->sta_state = new_state;
1862 u8 sta_info_tx_streams(struct sta_info *sta)
1864 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1867 if (!sta->sta.ht_cap.ht_supported)
1870 if (sta->sta.vht_cap.vht_supported) {
1873 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1875 for (i = 7; i >= 0; i--)
1876 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1877 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1881 if (ht_cap->mcs.rx_mask[3])
1883 else if (ht_cap->mcs.rx_mask[2])
1885 else if (ht_cap->mcs.rx_mask[1])
1890 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1893 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1894 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1897 static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1901 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
1902 rinfo->flags |= RATE_INFO_FLAGS_MCS;
1903 rinfo->mcs = sta->rx_stats.last_rate_idx;
1904 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
1905 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1906 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1907 rinfo->mcs = sta->rx_stats.last_rate_idx;
1909 struct ieee80211_supported_band *sband;
1910 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1913 sband = sta->local->hw.wiphy->bands[
1914 ieee80211_get_sdata_band(sta->sdata)];
1915 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
1916 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1919 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
1920 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1922 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
1923 rinfo->bw = RATE_INFO_BW_5;
1924 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
1925 rinfo->bw = RATE_INFO_BW_10;
1926 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
1927 rinfo->bw = RATE_INFO_BW_40;
1928 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1929 rinfo->bw = RATE_INFO_BW_80;
1930 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1931 rinfo->bw = RATE_INFO_BW_160;
1933 rinfo->bw = RATE_INFO_BW_20;
1936 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1938 struct ieee80211_sub_if_data *sdata = sta->sdata;
1939 struct ieee80211_local *local = sdata->local;
1940 struct rate_control_ref *ref = NULL;
1944 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1945 ref = local->rate_ctrl;
1947 sinfo->generation = sdata->local->sta_generation;
1949 /* do before driver, so beacon filtering drivers have a
1950 * chance to e.g. just add the number of filtered beacons
1951 * (or just modify the value entirely, of course)
1953 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1954 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1956 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1958 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1959 BIT(NL80211_STA_INFO_STA_FLAGS) |
1960 BIT(NL80211_STA_INFO_BSS_PARAM) |
1961 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1962 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1964 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1965 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1966 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1969 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
1970 sinfo->inactive_time =
1971 jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
1973 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1974 BIT(NL80211_STA_INFO_TX_BYTES)))) {
1975 sinfo->tx_bytes = 0;
1976 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1977 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
1978 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1981 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
1982 sinfo->tx_packets = 0;
1983 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1984 sinfo->tx_packets += sta->tx_stats.packets[ac];
1985 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
1988 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1989 BIT(NL80211_STA_INFO_RX_BYTES)))) {
1990 sinfo->rx_bytes = sta->rx_stats.bytes;
1991 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
1994 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1995 sinfo->rx_packets = sta->rx_stats.packets;
1996 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
1999 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2000 sinfo->tx_retries = sta->status_stats.retry_count;
2001 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
2004 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2005 sinfo->tx_failed = sta->status_stats.retry_failed;
2006 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
2009 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2011 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2012 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2013 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2014 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2015 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2018 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2019 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2020 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2021 sinfo->signal = (s8)sta->rx_stats.last_signal;
2022 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2025 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
2027 -ewma_signal_read(&sta->rx_stats.avg_signal);
2028 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2032 if (sta->rx_stats.chains &&
2033 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2034 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2035 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2036 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2038 sinfo->chains = sta->rx_stats.chains;
2039 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2040 sinfo->chain_signal[i] =
2041 sta->rx_stats.chain_signal_last[i];
2042 sinfo->chain_signal_avg[i] =
2043 -ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
2047 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2048 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2050 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2053 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2054 sta_set_rate_info_rx(sta, &sinfo->rxrate);
2055 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2058 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2059 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2060 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2062 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2063 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2064 tidstats->rx_msdu = sta->rx_stats.msdu[i];
2067 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2068 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2069 tidstats->tx_msdu = sta->tx_stats.msdu[i];
2072 if (!(tidstats->filled &
2073 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2074 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2076 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2077 tidstats->tx_msdu_retries =
2078 sta->status_stats.msdu_retries[i];
2081 if (!(tidstats->filled &
2082 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2083 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2085 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2086 tidstats->tx_msdu_failed =
2087 sta->status_stats.msdu_failed[i];
2091 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2092 #ifdef CONFIG_MAC80211_MESH
2093 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2094 BIT(NL80211_STA_INFO_PLID) |
2095 BIT(NL80211_STA_INFO_PLINK_STATE) |
2096 BIT(NL80211_STA_INFO_LOCAL_PM) |
2097 BIT(NL80211_STA_INFO_PEER_PM) |
2098 BIT(NL80211_STA_INFO_NONPEER_PM);
2100 sinfo->llid = sta->mesh->llid;
2101 sinfo->plid = sta->mesh->plid;
2102 sinfo->plink_state = sta->mesh->plink_state;
2103 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2104 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2105 sinfo->t_offset = sta->mesh->t_offset;
2107 sinfo->local_pm = sta->mesh->local_pm;
2108 sinfo->peer_pm = sta->mesh->peer_pm;
2109 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2113 sinfo->bss_param.flags = 0;
2114 if (sdata->vif.bss_conf.use_cts_prot)
2115 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2116 if (sdata->vif.bss_conf.use_short_preamble)
2117 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2118 if (sdata->vif.bss_conf.use_short_slot)
2119 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2120 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2121 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2123 sinfo->sta_flags.set = 0;
2124 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2125 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2126 BIT(NL80211_STA_FLAG_WME) |
2127 BIT(NL80211_STA_FLAG_MFP) |
2128 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2129 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2130 BIT(NL80211_STA_FLAG_TDLS_PEER);
2131 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2132 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2133 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2134 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2136 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2137 if (test_sta_flag(sta, WLAN_STA_MFP))
2138 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2139 if (test_sta_flag(sta, WLAN_STA_AUTH))
2140 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2141 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2142 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2143 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2144 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2146 /* check if the driver has a SW RC implementation */
2147 if (ref && ref->ops->get_expected_throughput)
2148 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2150 thr = drv_get_expected_throughput(local, &sta->sta);
2153 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2154 sinfo->expected_throughput = thr;