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 .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 rate_control_free_sta(sta);
248 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
251 kfree(to_txq_info(sta->sta.txq[0]));
252 kfree(rcu_dereference_raw(sta->sta.rates));
253 #ifdef CONFIG_MAC80211_MESH
259 /* Caller must hold local->sta_mtx */
260 static void sta_info_hash_add(struct ieee80211_local *local,
261 struct sta_info *sta)
263 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
267 static void sta_deliver_ps_frames(struct work_struct *wk)
269 struct sta_info *sta;
271 sta = container_of(wk, struct sta_info, drv_deliver_wk);
277 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
278 ieee80211_sta_ps_deliver_wakeup(sta);
279 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
280 ieee80211_sta_ps_deliver_poll_response(sta);
281 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
282 ieee80211_sta_ps_deliver_uapsd(sta);
286 static int sta_prepare_rate_control(struct ieee80211_local *local,
287 struct sta_info *sta, gfp_t gfp)
289 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
292 sta->rate_ctrl = local->rate_ctrl;
293 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
295 if (!sta->rate_ctrl_priv)
301 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
302 const u8 *addr, gfp_t gfp)
304 struct ieee80211_local *local = sdata->local;
305 struct ieee80211_hw *hw = &local->hw;
306 struct sta_info *sta;
309 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
313 spin_lock_init(&sta->lock);
314 spin_lock_init(&sta->ps_lock);
315 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
316 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
317 mutex_init(&sta->ampdu_mlme.mtx);
318 #ifdef CONFIG_MAC80211_MESH
319 if (ieee80211_vif_is_mesh(&sdata->vif)) {
320 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
323 spin_lock_init(&sta->mesh->plink_lock);
324 if (ieee80211_vif_is_mesh(&sdata->vif) &&
325 !sdata->u.mesh.user_mpm)
326 init_timer(&sta->mesh->plink_timer);
327 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
331 memcpy(sta->addr, addr, ETH_ALEN);
332 memcpy(sta->sta.addr, addr, ETH_ALEN);
335 sta->rx_stats.last_rx = jiffies;
337 sta->sta_state = IEEE80211_STA_NONE;
339 /* Mark TID as unreserved */
340 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
342 sta->last_connected = ktime_get_seconds();
343 ewma_signal_init(&sta->rx_stats.avg_signal);
344 for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
345 ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
347 if (local->ops->wake_tx_queue) {
349 int size = sizeof(struct txq_info) +
350 ALIGN(hw->txq_data_size, sizeof(void *));
352 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
356 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
357 struct txq_info *txq = txq_data + i * size;
359 ieee80211_init_tx_queue(sdata, sta, txq, i);
363 if (sta_prepare_rate_control(local, sta, gfp))
366 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
368 * timer_to_tid must be initialized with identity mapping
369 * to enable session_timer's data differentiation. See
370 * sta_rx_agg_session_timer_expired for usage.
372 sta->timer_to_tid[i] = i;
374 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
375 skb_queue_head_init(&sta->ps_tx_buf[i]);
376 skb_queue_head_init(&sta->tx_filtered[i]);
379 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
380 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
382 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
383 if (sdata->vif.type == NL80211_IFTYPE_AP ||
384 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
385 struct ieee80211_supported_band *sband =
386 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
387 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
388 IEEE80211_HT_CAP_SM_PS_SHIFT;
390 * Assume that hostapd advertises our caps in the beacon and
391 * this is the known_smps_mode for a station that just assciated
394 case WLAN_HT_SMPS_CONTROL_DISABLED:
395 sta->known_smps_mode = IEEE80211_SMPS_OFF;
397 case WLAN_HT_SMPS_CONTROL_STATIC:
398 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
400 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
401 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
408 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
414 kfree(to_txq_info(sta->sta.txq[0]));
416 #ifdef CONFIG_MAC80211_MESH
423 static int sta_info_insert_check(struct sta_info *sta)
425 struct ieee80211_sub_if_data *sdata = sta->sdata;
428 * Can't be a WARN_ON because it can be triggered through a race:
429 * something inserts a STA (on one CPU) without holding the RTNL
430 * and another CPU turns off the net device.
432 if (unlikely(!ieee80211_sdata_running(sdata)))
435 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
436 is_multicast_ether_addr(sta->sta.addr)))
439 /* Strictly speaking this isn't necessary as we hold the mutex, but
440 * the rhashtable code can't really deal with that distinction. We
441 * do require the mutex for correctness though.
444 lockdep_assert_held(&sdata->local->sta_mtx);
445 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
446 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
455 static int sta_info_insert_drv_state(struct ieee80211_local *local,
456 struct ieee80211_sub_if_data *sdata,
457 struct sta_info *sta)
459 enum ieee80211_sta_state state;
462 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
463 err = drv_sta_state(local, sdata, sta, state, state + 1);
470 * Drivers using legacy sta_add/sta_remove callbacks only
471 * get uploaded set to true after sta_add is called.
473 if (!local->ops->sta_add)
474 sta->uploaded = true;
478 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
480 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
481 sta->sta.addr, state + 1, err);
485 /* unwind on error */
486 for (; state > IEEE80211_STA_NOTEXIST; state--)
487 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
493 * should be called with sta_mtx locked
494 * this function replaces the mutex lock
497 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
499 struct ieee80211_local *local = sta->local;
500 struct ieee80211_sub_if_data *sdata = sta->sdata;
501 struct station_info sinfo;
504 lockdep_assert_held(&local->sta_mtx);
506 /* check if STA exists already */
507 if (sta_info_get_bss(sdata, sta->sta.addr)) {
513 local->sta_generation++;
516 /* simplify things and don't accept BA sessions yet */
517 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
519 /* make the station visible */
520 sta_info_hash_add(local, sta);
522 list_add_tail_rcu(&sta->list, &local->sta_list);
525 err = sta_info_insert_drv_state(local, sdata, sta);
529 set_sta_flag(sta, WLAN_STA_INSERTED);
530 /* accept BA sessions now */
531 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
533 ieee80211_recalc_min_chandef(sdata);
534 ieee80211_sta_debugfs_add(sta);
535 rate_control_add_sta_debugfs(sta);
537 memset(&sinfo, 0, sizeof(sinfo));
539 sinfo.generation = local->sta_generation;
540 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
542 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
544 /* move reference to rcu-protected */
546 mutex_unlock(&local->sta_mtx);
548 if (ieee80211_vif_is_mesh(&sdata->vif))
549 mesh_accept_plinks_update(sdata);
553 sta_info_hash_del(local, sta);
554 list_del_rcu(&sta->list);
557 __cleanup_single_sta(sta);
559 mutex_unlock(&local->sta_mtx);
564 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
566 struct ieee80211_local *local = sta->local;
571 mutex_lock(&local->sta_mtx);
573 err = sta_info_insert_check(sta);
575 mutex_unlock(&local->sta_mtx);
580 err = sta_info_insert_finish(sta);
586 sta_info_free(local, sta);
590 int sta_info_insert(struct sta_info *sta)
592 int err = sta_info_insert_rcu(sta);
599 static inline void __bss_tim_set(u8 *tim, u16 id)
602 * This format has been mandated by the IEEE specifications,
603 * so this line may not be changed to use the __set_bit() format.
605 tim[id / 8] |= (1 << (id % 8));
608 static inline void __bss_tim_clear(u8 *tim, u16 id)
611 * This format has been mandated by the IEEE specifications,
612 * so this line may not be changed to use the __clear_bit() format.
614 tim[id / 8] &= ~(1 << (id % 8));
617 static inline bool __bss_tim_get(u8 *tim, u16 id)
620 * This format has been mandated by the IEEE specifications,
621 * so this line may not be changed to use the test_bit() format.
623 return tim[id / 8] & (1 << (id % 8));
626 static unsigned long ieee80211_tids_for_ac(int ac)
628 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
630 case IEEE80211_AC_VO:
631 return BIT(6) | BIT(7);
632 case IEEE80211_AC_VI:
633 return BIT(4) | BIT(5);
634 case IEEE80211_AC_BE:
635 return BIT(0) | BIT(3);
636 case IEEE80211_AC_BK:
637 return BIT(1) | BIT(2);
644 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
646 struct ieee80211_local *local = sta->local;
648 bool indicate_tim = false;
649 u8 ignore_for_tim = sta->sta.uapsd_queues;
651 u16 id = sta->sta.aid;
653 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
654 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
655 if (WARN_ON_ONCE(!sta->sdata->bss))
658 ps = &sta->sdata->bss->ps;
659 #ifdef CONFIG_MAC80211_MESH
660 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
661 ps = &sta->sdata->u.mesh.ps;
667 /* No need to do anything if the driver does all */
668 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
675 * If all ACs are delivery-enabled then we should build
676 * the TIM bit for all ACs anyway; if only some are then
677 * we ignore those and build the TIM bit using only the
680 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
684 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
686 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
689 if (ignore_for_tim & BIT(ac))
692 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
693 !skb_queue_empty(&sta->ps_tx_buf[ac]);
697 tids = ieee80211_tids_for_ac(ac);
700 sta->driver_buffered_tids & tids;
702 sta->txq_buffered_tids & tids;
706 spin_lock_bh(&local->tim_lock);
708 if (indicate_tim == __bss_tim_get(ps->tim, id))
712 __bss_tim_set(ps->tim, id);
714 __bss_tim_clear(ps->tim, id);
716 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
717 local->tim_in_locked_section = true;
718 drv_set_tim(local, &sta->sta, indicate_tim);
719 local->tim_in_locked_section = false;
723 spin_unlock_bh(&local->tim_lock);
726 void sta_info_recalc_tim(struct sta_info *sta)
728 __sta_info_recalc_tim(sta, false);
731 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
733 struct ieee80211_tx_info *info;
739 info = IEEE80211_SKB_CB(skb);
741 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
742 timeout = (sta->listen_interval *
743 sta->sdata->vif.bss_conf.beacon_int *
745 if (timeout < STA_TX_BUFFER_EXPIRE)
746 timeout = STA_TX_BUFFER_EXPIRE;
747 return time_after(jiffies, info->control.jiffies + timeout);
751 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
752 struct sta_info *sta, int ac)
758 * First check for frames that should expire on the filtered
759 * queue. Frames here were rejected by the driver and are on
760 * a separate queue to avoid reordering with normal PS-buffered
761 * frames. They also aren't accounted for right now in the
762 * total_ps_buffered counter.
765 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
766 skb = skb_peek(&sta->tx_filtered[ac]);
767 if (sta_info_buffer_expired(sta, skb))
768 skb = __skb_dequeue(&sta->tx_filtered[ac]);
771 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
774 * Frames are queued in order, so if this one
775 * hasn't expired yet we can stop testing. If
776 * we actually reached the end of the queue we
777 * also need to stop, of course.
781 ieee80211_free_txskb(&local->hw, skb);
785 * Now also check the normal PS-buffered queue, this will
786 * only find something if the filtered queue was emptied
787 * since the filtered frames are all before the normal PS
791 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
792 skb = skb_peek(&sta->ps_tx_buf[ac]);
793 if (sta_info_buffer_expired(sta, skb))
794 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
797 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
800 * frames are queued in order, so if this one
801 * hasn't expired yet (or we reached the end of
802 * the queue) we can stop testing
807 local->total_ps_buffered--;
808 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
810 ieee80211_free_txskb(&local->hw, skb);
814 * Finally, recalculate the TIM bit for this station -- it might
815 * now be clear because the station was too slow to retrieve its
818 sta_info_recalc_tim(sta);
821 * Return whether there are any frames still buffered, this is
822 * used to check whether the cleanup timer still needs to run,
823 * if there are no frames we don't need to rearm the timer.
825 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
826 skb_queue_empty(&sta->tx_filtered[ac]));
829 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
830 struct sta_info *sta)
832 bool have_buffered = false;
835 /* This is only necessary for stations on BSS/MBSS interfaces */
836 if (!sta->sdata->bss &&
837 !ieee80211_vif_is_mesh(&sta->sdata->vif))
840 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
842 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
844 return have_buffered;
847 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
849 struct ieee80211_local *local;
850 struct ieee80211_sub_if_data *sdata;
861 lockdep_assert_held(&local->sta_mtx);
864 * Before removing the station from the driver and
865 * rate control, it might still start new aggregation
866 * sessions -- block that to make sure the tear-down
867 * will be sufficient.
869 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
870 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
872 ret = sta_info_hash_del(local, sta);
877 * for TDLS peers, make sure to return to the base channel before
880 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
881 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
882 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
885 list_del_rcu(&sta->list);
888 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
890 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
891 rcu_access_pointer(sdata->u.vlan.sta) == sta)
892 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
897 static void __sta_info_destroy_part2(struct sta_info *sta)
899 struct ieee80211_local *local = sta->local;
900 struct ieee80211_sub_if_data *sdata = sta->sdata;
901 struct station_info sinfo = {};
905 * NOTE: This assumes at least synchronize_net() was done
906 * after _part1 and before _part2!
910 lockdep_assert_held(&local->sta_mtx);
912 /* now keys can no longer be reached */
913 ieee80211_free_sta_keys(local, sta);
915 /* disable TIM bit - last chance to tell driver */
916 __sta_info_recalc_tim(sta, true);
921 local->sta_generation++;
923 while (sta->sta_state > IEEE80211_STA_NONE) {
924 ret = sta_info_move_state(sta, sta->sta_state - 1);
932 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
933 IEEE80211_STA_NOTEXIST);
934 WARN_ON_ONCE(ret != 0);
937 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
939 sta_set_sinfo(sta, &sinfo);
940 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
942 rate_control_remove_sta_debugfs(sta);
943 ieee80211_sta_debugfs_remove(sta);
944 ieee80211_recalc_min_chandef(sdata);
946 cleanup_single_sta(sta);
949 int __must_check __sta_info_destroy(struct sta_info *sta)
951 int err = __sta_info_destroy_part1(sta);
958 __sta_info_destroy_part2(sta);
963 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
965 struct sta_info *sta;
968 mutex_lock(&sdata->local->sta_mtx);
969 sta = sta_info_get(sdata, addr);
970 ret = __sta_info_destroy(sta);
971 mutex_unlock(&sdata->local->sta_mtx);
976 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
979 struct sta_info *sta;
982 mutex_lock(&sdata->local->sta_mtx);
983 sta = sta_info_get_bss(sdata, addr);
984 ret = __sta_info_destroy(sta);
985 mutex_unlock(&sdata->local->sta_mtx);
990 static void sta_info_cleanup(unsigned long data)
992 struct ieee80211_local *local = (struct ieee80211_local *) data;
993 struct sta_info *sta;
994 bool timer_needed = false;
997 list_for_each_entry_rcu(sta, &local->sta_list, list)
998 if (sta_info_cleanup_expire_buffered(local, sta))
1002 if (local->quiescing)
1008 mod_timer(&local->sta_cleanup,
1009 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1012 u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1014 return jhash(key, ETH_ALEN, seed);
1017 int sta_info_init(struct ieee80211_local *local)
1021 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1025 spin_lock_init(&local->tim_lock);
1026 mutex_init(&local->sta_mtx);
1027 INIT_LIST_HEAD(&local->sta_list);
1029 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1030 (unsigned long)local);
1034 void sta_info_stop(struct ieee80211_local *local)
1036 del_timer_sync(&local->sta_cleanup);
1037 rhashtable_destroy(&local->sta_hash);
1041 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1043 struct ieee80211_local *local = sdata->local;
1044 struct sta_info *sta, *tmp;
1045 LIST_HEAD(free_list);
1050 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1051 WARN_ON(vlans && !sdata->bss);
1053 mutex_lock(&local->sta_mtx);
1054 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1055 if (sdata == sta->sdata ||
1056 (vlans && sdata->bss == sta->sdata->bss)) {
1057 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1058 list_add(&sta->free_list, &free_list);
1063 if (!list_empty(&free_list)) {
1065 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1066 __sta_info_destroy_part2(sta);
1068 mutex_unlock(&local->sta_mtx);
1073 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1074 unsigned long exp_time)
1076 struct ieee80211_local *local = sdata->local;
1077 struct sta_info *sta, *tmp;
1079 mutex_lock(&local->sta_mtx);
1081 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1082 if (sdata != sta->sdata)
1085 if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
1086 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1089 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1090 test_sta_flag(sta, WLAN_STA_PS_STA))
1091 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1093 WARN_ON(__sta_info_destroy(sta));
1097 mutex_unlock(&local->sta_mtx);
1100 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1102 const u8 *localaddr)
1104 struct ieee80211_local *local = hw_to_local(hw);
1105 struct sta_info *sta;
1106 struct rhash_head *tmp;
1107 const struct bucket_table *tbl;
1109 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1112 * Just return a random station if localaddr is NULL
1113 * ... first in list.
1115 for_each_sta_info(local, tbl, addr, sta, tmp) {
1117 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1126 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1128 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1131 struct sta_info *sta;
1136 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1145 EXPORT_SYMBOL(ieee80211_find_sta);
1147 /* powersave support code */
1148 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1150 struct ieee80211_sub_if_data *sdata = sta->sdata;
1151 struct ieee80211_local *local = sdata->local;
1152 struct sk_buff_head pending;
1153 int filtered = 0, buffered = 0, ac, i;
1154 unsigned long flags;
1157 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1158 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1161 if (sdata->vif.type == NL80211_IFTYPE_AP)
1162 ps = &sdata->bss->ps;
1163 else if (ieee80211_vif_is_mesh(&sdata->vif))
1164 ps = &sdata->u.mesh.ps;
1168 clear_sta_flag(sta, WLAN_STA_SP);
1170 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1171 sta->driver_buffered_tids = 0;
1172 sta->txq_buffered_tids = 0;
1174 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1175 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1177 if (sta->sta.txq[0]) {
1178 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1179 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1181 if (!skb_queue_len(&txqi->queue))
1184 drv_wake_tx_queue(local, txqi);
1188 skb_queue_head_init(&pending);
1190 /* sync with ieee80211_tx_h_unicast_ps_buf */
1191 spin_lock(&sta->ps_lock);
1192 /* Send all buffered frames to the station */
1193 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1194 int count = skb_queue_len(&pending), tmp;
1196 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1197 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1198 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1199 tmp = skb_queue_len(&pending);
1200 filtered += tmp - count;
1203 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1204 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1205 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1206 tmp = skb_queue_len(&pending);
1207 buffered += tmp - count;
1210 ieee80211_add_pending_skbs(local, &pending);
1212 /* now we're no longer in the deliver code */
1213 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1215 /* The station might have polled and then woken up before we responded,
1216 * so clear these flags now to avoid them sticking around.
1218 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1219 clear_sta_flag(sta, WLAN_STA_UAPSD);
1220 spin_unlock(&sta->ps_lock);
1222 atomic_dec(&ps->num_sta_ps);
1224 /* This station just woke up and isn't aware of our SMPS state */
1225 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1226 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1227 sdata->smps_mode) &&
1228 sta->known_smps_mode != sdata->bss->req_smps &&
1229 sta_info_tx_streams(sta) != 1) {
1231 "%pM just woke up and MIMO capable - update SMPS\n",
1233 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1235 sdata->vif.bss_conf.bssid);
1238 local->total_ps_buffered -= buffered;
1240 sta_info_recalc_tim(sta);
1243 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1244 sta->sta.addr, sta->sta.aid, filtered, buffered);
1246 ieee80211_check_fast_xmit(sta);
1249 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1250 enum ieee80211_frame_release_type reason,
1251 bool call_driver, bool more_data)
1253 struct ieee80211_sub_if_data *sdata = sta->sdata;
1254 struct ieee80211_local *local = sdata->local;
1255 struct ieee80211_qos_hdr *nullfunc;
1256 struct sk_buff *skb;
1257 int size = sizeof(*nullfunc);
1259 bool qos = sta->sta.wme;
1260 struct ieee80211_tx_info *info;
1261 struct ieee80211_chanctx_conf *chanctx_conf;
1264 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1265 IEEE80211_STYPE_QOS_NULLFUNC |
1266 IEEE80211_FCTL_FROMDS);
1269 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1270 IEEE80211_STYPE_NULLFUNC |
1271 IEEE80211_FCTL_FROMDS);
1274 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1278 skb_reserve(skb, local->hw.extra_tx_headroom);
1280 nullfunc = (void *) skb_put(skb, size);
1281 nullfunc->frame_control = fc;
1282 nullfunc->duration_id = 0;
1283 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1284 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1285 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1286 nullfunc->seq_ctrl = 0;
1288 skb->priority = tid;
1289 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1291 nullfunc->qos_ctrl = cpu_to_le16(tid);
1293 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1294 nullfunc->qos_ctrl |=
1295 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1297 nullfunc->frame_control |=
1298 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1302 info = IEEE80211_SKB_CB(skb);
1305 * Tell TX path to send this frame even though the
1306 * STA may still remain is PS mode after this frame
1307 * exchange. Also set EOSP to indicate this packet
1308 * ends the poll/service period.
1310 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1311 IEEE80211_TX_STATUS_EOSP |
1312 IEEE80211_TX_CTL_REQ_TX_STATUS;
1314 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1317 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1320 skb->dev = sdata->dev;
1323 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1324 if (WARN_ON(!chanctx_conf)) {
1330 info->band = chanctx_conf->def.chan->band;
1331 ieee80211_xmit(sdata, sta, skb);
1335 static int find_highest_prio_tid(unsigned long tids)
1337 /* lower 3 TIDs aren't ordered perfectly */
1339 return fls(tids) - 1;
1340 /* TID 0 is BE just like TID 3 */
1343 return fls(tids) - 1;
1346 /* Indicates if the MORE_DATA bit should be set in the last
1347 * frame obtained by ieee80211_sta_ps_get_frames.
1348 * Note that driver_release_tids is relevant only if
1349 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1352 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1353 enum ieee80211_frame_release_type reason,
1354 unsigned long driver_release_tids)
1358 /* If the driver has data on more than one TID then
1359 * certainly there's more data if we release just a
1360 * single frame now (from a single TID). This will
1361 * only happen for PS-Poll.
1363 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1364 hweight16(driver_release_tids) > 1)
1367 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1368 if (ignored_acs & BIT(ac))
1371 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1372 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1380 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1381 enum ieee80211_frame_release_type reason,
1382 struct sk_buff_head *frames,
1383 unsigned long *driver_release_tids)
1385 struct ieee80211_sub_if_data *sdata = sta->sdata;
1386 struct ieee80211_local *local = sdata->local;
1389 /* Get response frame(s) and more data bit for the last one. */
1390 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1393 if (ignored_acs & BIT(ac))
1396 tids = ieee80211_tids_for_ac(ac);
1398 /* if we already have frames from software, then we can't also
1399 * release from hardware queues
1401 if (skb_queue_empty(frames)) {
1402 *driver_release_tids |=
1403 sta->driver_buffered_tids & tids;
1404 *driver_release_tids |= sta->txq_buffered_tids & tids;
1407 if (!*driver_release_tids) {
1408 struct sk_buff *skb;
1410 while (n_frames > 0) {
1411 skb = skb_dequeue(&sta->tx_filtered[ac]);
1414 &sta->ps_tx_buf[ac]);
1416 local->total_ps_buffered--;
1421 __skb_queue_tail(frames, skb);
1425 /* If we have more frames buffered on this AC, then abort the
1426 * loop since we can't send more data from other ACs before
1427 * the buffered frames from this.
1429 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1430 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1436 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1437 int n_frames, u8 ignored_acs,
1438 enum ieee80211_frame_release_type reason)
1440 struct ieee80211_sub_if_data *sdata = sta->sdata;
1441 struct ieee80211_local *local = sdata->local;
1442 unsigned long driver_release_tids = 0;
1443 struct sk_buff_head frames;
1446 /* Service or PS-Poll period starts */
1447 set_sta_flag(sta, WLAN_STA_SP);
1449 __skb_queue_head_init(&frames);
1451 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1452 &frames, &driver_release_tids);
1454 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1456 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1457 driver_release_tids =
1458 BIT(find_highest_prio_tid(driver_release_tids));
1460 if (skb_queue_empty(&frames) && !driver_release_tids) {
1464 * For PS-Poll, this can only happen due to a race condition
1465 * when we set the TIM bit and the station notices it, but
1466 * before it can poll for the frame we expire it.
1468 * For uAPSD, this is said in the standard (11.2.1.5 h):
1469 * At each unscheduled SP for a non-AP STA, the AP shall
1470 * attempt to transmit at least one MSDU or MMPDU, but no
1471 * more than the value specified in the Max SP Length field
1472 * in the QoS Capability element from delivery-enabled ACs,
1473 * that are destined for the non-AP STA.
1475 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1478 /* This will evaluate to 1, 3, 5 or 7. */
1479 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1481 ieee80211_send_null_response(sta, tid, reason, true, false);
1482 } else if (!driver_release_tids) {
1483 struct sk_buff_head pending;
1484 struct sk_buff *skb;
1487 bool need_null = false;
1489 skb_queue_head_init(&pending);
1491 while ((skb = __skb_dequeue(&frames))) {
1492 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1493 struct ieee80211_hdr *hdr = (void *) skb->data;
1499 * Tell TX path to send this frame even though the
1500 * STA may still remain is PS mode after this frame
1503 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1504 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1507 * Use MoreData flag to indicate whether there are
1508 * more buffered frames for this STA
1510 if (more_data || !skb_queue_empty(&frames))
1511 hdr->frame_control |=
1512 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1514 hdr->frame_control &=
1515 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1517 if (ieee80211_is_data_qos(hdr->frame_control) ||
1518 ieee80211_is_qos_nullfunc(hdr->frame_control))
1519 qoshdr = ieee80211_get_qos_ctl(hdr);
1521 tids |= BIT(skb->priority);
1523 __skb_queue_tail(&pending, skb);
1525 /* end service period after last frame or add one */
1526 if (!skb_queue_empty(&frames))
1529 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1530 /* for PS-Poll, there's only one frame */
1531 info->flags |= IEEE80211_TX_STATUS_EOSP |
1532 IEEE80211_TX_CTL_REQ_TX_STATUS;
1536 /* For uAPSD, things are a bit more complicated. If the
1537 * last frame has a QoS header (i.e. is a QoS-data or
1538 * QoS-nulldata frame) then just set the EOSP bit there
1540 * If the frame doesn't have a QoS header (which means
1541 * it should be a bufferable MMPDU) then we can't set
1542 * the EOSP bit in the QoS header; add a QoS-nulldata
1543 * frame to the list to send it after the MMPDU.
1545 * Note that this code is only in the mac80211-release
1546 * code path, we assume that the driver will not buffer
1547 * anything but QoS-data frames, or if it does, will
1548 * create the QoS-nulldata frame by itself if needed.
1550 * Cf. 802.11-2012 10.2.1.10 (c).
1553 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1555 info->flags |= IEEE80211_TX_STATUS_EOSP |
1556 IEEE80211_TX_CTL_REQ_TX_STATUS;
1558 /* The standard isn't completely clear on this
1559 * as it says the more-data bit should be set
1560 * if there are more BUs. The QoS-Null frame
1561 * we're about to send isn't buffered yet, we
1562 * only create it below, but let's pretend it
1563 * was buffered just in case some clients only
1564 * expect more-data=0 when eosp=1.
1566 hdr->frame_control |=
1567 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1574 drv_allow_buffered_frames(local, sta, tids, num,
1577 ieee80211_add_pending_skbs(local, &pending);
1580 ieee80211_send_null_response(
1581 sta, find_highest_prio_tid(tids),
1582 reason, false, false);
1584 sta_info_recalc_tim(sta);
1586 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1590 * We need to release a frame that is buffered somewhere in the
1591 * driver ... it'll have to handle that.
1592 * Note that the driver also has to check the number of frames
1593 * on the TIDs we're releasing from - if there are more than
1594 * n_frames it has to set the more-data bit (if we didn't ask
1595 * it to set it anyway due to other buffered frames); if there
1596 * are fewer than n_frames it has to make sure to adjust that
1597 * to allow the service period to end properly.
1599 drv_release_buffered_frames(local, sta, driver_release_tids,
1600 n_frames, reason, more_data);
1603 * Note that we don't recalculate the TIM bit here as it would
1604 * most likely have no effect at all unless the driver told us
1605 * that the TID(s) became empty before returning here from the
1607 * Either way, however, when the driver tells us that the TID(s)
1608 * became empty or we find that a txq became empty, we'll do the
1609 * TIM recalculation.
1612 if (!sta->sta.txq[0])
1615 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1616 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1618 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1621 sta_info_recalc_tim(sta);
1627 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1629 u8 ignore_for_response = sta->sta.uapsd_queues;
1632 * If all ACs are delivery-enabled then we should reply
1633 * from any of them, if only some are enabled we reply
1634 * only from the non-enabled ones.
1636 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1637 ignore_for_response = 0;
1639 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1640 IEEE80211_FRAME_RELEASE_PSPOLL);
1643 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1645 int n_frames = sta->sta.max_sp;
1646 u8 delivery_enabled = sta->sta.uapsd_queues;
1649 * If we ever grow support for TSPEC this might happen if
1650 * the TSPEC update from hostapd comes in between a trigger
1651 * frame setting WLAN_STA_UAPSD in the RX path and this
1652 * actually getting called.
1654 if (!delivery_enabled)
1657 switch (sta->sta.max_sp) {
1668 /* XXX: what is a good value? */
1673 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1674 IEEE80211_FRAME_RELEASE_UAPSD);
1677 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1678 struct ieee80211_sta *pubsta, bool block)
1680 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1682 trace_api_sta_block_awake(sta->local, pubsta, block);
1685 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1686 ieee80211_clear_fast_xmit(sta);
1690 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1693 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1694 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1695 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1696 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1697 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1698 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1699 /* must be asleep in this case */
1700 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1701 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1703 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1704 ieee80211_check_fast_xmit(sta);
1707 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1709 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1711 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1712 struct ieee80211_local *local = sta->local;
1714 trace_api_eosp(local, pubsta);
1716 clear_sta_flag(sta, WLAN_STA_SP);
1718 EXPORT_SYMBOL(ieee80211_sta_eosp);
1720 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1722 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1723 enum ieee80211_frame_release_type reason;
1726 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1728 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1729 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1732 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1734 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1736 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1737 u8 tid, bool buffered)
1739 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1741 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1744 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1747 set_bit(tid, &sta->driver_buffered_tids);
1749 clear_bit(tid, &sta->driver_buffered_tids);
1751 sta_info_recalc_tim(sta);
1753 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1755 int sta_info_move_state(struct sta_info *sta,
1756 enum ieee80211_sta_state new_state)
1760 if (sta->sta_state == new_state)
1763 /* check allowed transitions first */
1765 switch (new_state) {
1766 case IEEE80211_STA_NONE:
1767 if (sta->sta_state != IEEE80211_STA_AUTH)
1770 case IEEE80211_STA_AUTH:
1771 if (sta->sta_state != IEEE80211_STA_NONE &&
1772 sta->sta_state != IEEE80211_STA_ASSOC)
1775 case IEEE80211_STA_ASSOC:
1776 if (sta->sta_state != IEEE80211_STA_AUTH &&
1777 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1780 case IEEE80211_STA_AUTHORIZED:
1781 if (sta->sta_state != IEEE80211_STA_ASSOC)
1785 WARN(1, "invalid state %d", new_state);
1789 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1790 sta->sta.addr, new_state);
1793 * notify the driver before the actual changes so it can
1794 * fail the transition
1796 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1797 int err = drv_sta_state(sta->local, sta->sdata, sta,
1798 sta->sta_state, new_state);
1803 /* reflect the change in all state variables */
1805 switch (new_state) {
1806 case IEEE80211_STA_NONE:
1807 if (sta->sta_state == IEEE80211_STA_AUTH)
1808 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1810 case IEEE80211_STA_AUTH:
1811 if (sta->sta_state == IEEE80211_STA_NONE)
1812 set_bit(WLAN_STA_AUTH, &sta->_flags);
1813 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1814 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1816 case IEEE80211_STA_ASSOC:
1817 if (sta->sta_state == IEEE80211_STA_AUTH) {
1818 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1819 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1820 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1821 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1822 !sta->sdata->u.vlan.sta))
1823 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1824 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1825 ieee80211_clear_fast_xmit(sta);
1828 case IEEE80211_STA_AUTHORIZED:
1829 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1830 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1831 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1832 !sta->sdata->u.vlan.sta))
1833 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1834 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1835 ieee80211_check_fast_xmit(sta);
1842 sta->sta_state = new_state;
1847 u8 sta_info_tx_streams(struct sta_info *sta)
1849 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1852 if (!sta->sta.ht_cap.ht_supported)
1855 if (sta->sta.vht_cap.vht_supported) {
1858 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1860 for (i = 7; i >= 0; i--)
1861 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1862 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1866 if (ht_cap->mcs.rx_mask[3])
1868 else if (ht_cap->mcs.rx_mask[2])
1870 else if (ht_cap->mcs.rx_mask[1])
1875 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1878 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1879 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1882 static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1886 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
1887 rinfo->flags |= RATE_INFO_FLAGS_MCS;
1888 rinfo->mcs = sta->rx_stats.last_rate_idx;
1889 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
1890 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1891 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1892 rinfo->mcs = sta->rx_stats.last_rate_idx;
1894 struct ieee80211_supported_band *sband;
1895 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1898 sband = sta->local->hw.wiphy->bands[
1899 ieee80211_get_sdata_band(sta->sdata)];
1900 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
1901 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1904 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
1905 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1907 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
1908 rinfo->bw = RATE_INFO_BW_5;
1909 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
1910 rinfo->bw = RATE_INFO_BW_10;
1911 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
1912 rinfo->bw = RATE_INFO_BW_40;
1913 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1914 rinfo->bw = RATE_INFO_BW_80;
1915 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1916 rinfo->bw = RATE_INFO_BW_160;
1918 rinfo->bw = RATE_INFO_BW_20;
1921 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1923 struct ieee80211_sub_if_data *sdata = sta->sdata;
1924 struct ieee80211_local *local = sdata->local;
1925 struct rate_control_ref *ref = NULL;
1929 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1930 ref = local->rate_ctrl;
1932 sinfo->generation = sdata->local->sta_generation;
1934 /* do before driver, so beacon filtering drivers have a
1935 * chance to e.g. just add the number of filtered beacons
1936 * (or just modify the value entirely, of course)
1938 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1939 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1941 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1943 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1944 BIT(NL80211_STA_INFO_STA_FLAGS) |
1945 BIT(NL80211_STA_INFO_BSS_PARAM) |
1946 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1947 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1949 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1950 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1951 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1954 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
1955 sinfo->inactive_time =
1956 jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
1958 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1959 BIT(NL80211_STA_INFO_TX_BYTES)))) {
1960 sinfo->tx_bytes = 0;
1961 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1962 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
1963 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1966 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
1967 sinfo->tx_packets = 0;
1968 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1969 sinfo->tx_packets += sta->tx_stats.packets[ac];
1970 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
1973 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1974 BIT(NL80211_STA_INFO_RX_BYTES)))) {
1975 sinfo->rx_bytes = sta->rx_stats.bytes;
1976 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
1979 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1980 sinfo->rx_packets = sta->rx_stats.packets;
1981 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
1984 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1985 sinfo->tx_retries = sta->status_stats.retry_count;
1986 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
1989 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1990 sinfo->tx_failed = sta->status_stats.retry_failed;
1991 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
1994 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
1996 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1997 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1998 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1999 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2000 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2003 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2004 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2005 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2006 sinfo->signal = (s8)sta->rx_stats.last_signal;
2007 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2010 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
2012 -ewma_signal_read(&sta->rx_stats.avg_signal);
2013 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2017 if (sta->rx_stats.chains &&
2018 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2019 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2020 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2021 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2023 sinfo->chains = sta->rx_stats.chains;
2024 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2025 sinfo->chain_signal[i] =
2026 sta->rx_stats.chain_signal_last[i];
2027 sinfo->chain_signal_avg[i] =
2028 -ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
2032 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2033 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2035 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2038 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2039 sta_set_rate_info_rx(sta, &sinfo->rxrate);
2040 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2043 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2044 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2045 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2047 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2048 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2049 tidstats->rx_msdu = sta->rx_stats.msdu[i];
2052 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2053 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2054 tidstats->tx_msdu = sta->tx_stats.msdu[i];
2057 if (!(tidstats->filled &
2058 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2059 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2061 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2062 tidstats->tx_msdu_retries =
2063 sta->status_stats.msdu_retries[i];
2066 if (!(tidstats->filled &
2067 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2068 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2070 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2071 tidstats->tx_msdu_failed =
2072 sta->status_stats.msdu_failed[i];
2076 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2077 #ifdef CONFIG_MAC80211_MESH
2078 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2079 BIT(NL80211_STA_INFO_PLID) |
2080 BIT(NL80211_STA_INFO_PLINK_STATE) |
2081 BIT(NL80211_STA_INFO_LOCAL_PM) |
2082 BIT(NL80211_STA_INFO_PEER_PM) |
2083 BIT(NL80211_STA_INFO_NONPEER_PM);
2085 sinfo->llid = sta->mesh->llid;
2086 sinfo->plid = sta->mesh->plid;
2087 sinfo->plink_state = sta->mesh->plink_state;
2088 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2089 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2090 sinfo->t_offset = sta->mesh->t_offset;
2092 sinfo->local_pm = sta->mesh->local_pm;
2093 sinfo->peer_pm = sta->mesh->peer_pm;
2094 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2098 sinfo->bss_param.flags = 0;
2099 if (sdata->vif.bss_conf.use_cts_prot)
2100 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2101 if (sdata->vif.bss_conf.use_short_preamble)
2102 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2103 if (sdata->vif.bss_conf.use_short_slot)
2104 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2105 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2106 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2108 sinfo->sta_flags.set = 0;
2109 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2110 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2111 BIT(NL80211_STA_FLAG_WME) |
2112 BIT(NL80211_STA_FLAG_MFP) |
2113 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2114 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2115 BIT(NL80211_STA_FLAG_TDLS_PEER);
2116 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2117 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2118 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2119 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2121 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2122 if (test_sta_flag(sta, WLAN_STA_MFP))
2123 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2124 if (test_sta_flag(sta, WLAN_STA_AUTH))
2125 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2126 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2127 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2128 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2129 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2131 /* check if the driver has a SW RC implementation */
2132 if (ref && ref->ops->get_expected_throughput)
2133 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2135 thr = drv_get_expected_throughput(local, &sta->sta);
2138 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2139 sinfo->expected_throughput = thr;