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 - 2016 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]);
123 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
125 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
129 if (ieee80211_vif_is_mesh(&sdata->vif))
130 mesh_sta_cleanup(sta);
132 cancel_work_sync(&sta->drv_deliver_wk);
135 * Destroy aggregation state here. It would be nice to wait for the
136 * driver to finish aggregation stop and then clean up, but for now
137 * drivers have to handle aggregation stop being requested, followed
138 * directly by station destruction.
140 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141 kfree(sta->ampdu_mlme.tid_start_tx[i]);
142 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
145 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
150 static void cleanup_single_sta(struct sta_info *sta)
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
153 struct ieee80211_local *local = sdata->local;
155 __cleanup_single_sta(sta);
156 sta_info_free(local, sta);
159 /* protected by RCU */
160 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
163 struct ieee80211_local *local = sdata->local;
164 struct sta_info *sta;
165 struct rhash_head *tmp;
166 const struct bucket_table *tbl;
169 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
171 for_each_sta_info(local, tbl, addr, sta, tmp) {
172 if (sta->sdata == sdata) {
174 /* this is safe as the caller must already hold
175 * another rcu read section or the mutex
185 * Get sta info either from the specified interface
186 * or from one of its vlans
188 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
191 struct ieee80211_local *local = sdata->local;
192 struct sta_info *sta;
193 struct rhash_head *tmp;
194 const struct bucket_table *tbl;
197 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
199 for_each_sta_info(local, tbl, addr, sta, tmp) {
200 if (sta->sdata == sdata ||
201 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
203 /* this is safe as the caller must already hold
204 * another rcu read section or the mutex
213 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
216 struct ieee80211_local *local = sdata->local;
217 struct sta_info *sta;
220 list_for_each_entry_rcu(sta, &local->sta_list, list) {
221 if (sdata != sta->sdata)
234 * sta_info_free - free STA
236 * @local: pointer to the global information
237 * @sta: STA info to free
239 * This function must undo everything done by sta_info_alloc()
240 * that may happen before sta_info_insert(). It may only be
241 * called when sta_info_insert() has not been attempted (and
242 * if that fails, the station is freed anyway.)
244 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
247 rate_control_free_sta(sta);
249 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
252 kfree(to_txq_info(sta->sta.txq[0]));
253 kfree(rcu_dereference_raw(sta->sta.rates));
254 #ifdef CONFIG_MAC80211_MESH
260 /* Caller must hold local->sta_mtx */
261 static void sta_info_hash_add(struct ieee80211_local *local,
262 struct sta_info *sta)
264 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
268 static void sta_deliver_ps_frames(struct work_struct *wk)
270 struct sta_info *sta;
272 sta = container_of(wk, struct sta_info, drv_deliver_wk);
278 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
279 ieee80211_sta_ps_deliver_wakeup(sta);
280 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
281 ieee80211_sta_ps_deliver_poll_response(sta);
282 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
283 ieee80211_sta_ps_deliver_uapsd(sta);
287 static int sta_prepare_rate_control(struct ieee80211_local *local,
288 struct sta_info *sta, gfp_t gfp)
290 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
293 sta->rate_ctrl = local->rate_ctrl;
294 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
296 if (!sta->rate_ctrl_priv)
302 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
303 const u8 *addr, gfp_t gfp)
305 struct ieee80211_local *local = sdata->local;
306 struct ieee80211_hw *hw = &local->hw;
307 struct sta_info *sta;
310 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
314 spin_lock_init(&sta->lock);
315 spin_lock_init(&sta->ps_lock);
316 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
317 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
318 mutex_init(&sta->ampdu_mlme.mtx);
319 #ifdef CONFIG_MAC80211_MESH
320 if (ieee80211_vif_is_mesh(&sdata->vif)) {
321 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
324 spin_lock_init(&sta->mesh->plink_lock);
325 if (ieee80211_vif_is_mesh(&sdata->vif) &&
326 !sdata->u.mesh.user_mpm)
327 init_timer(&sta->mesh->plink_timer);
328 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
332 memcpy(sta->addr, addr, ETH_ALEN);
333 memcpy(sta->sta.addr, addr, ETH_ALEN);
336 sta->rx_stats.last_rx = jiffies;
338 sta->sta_state = IEEE80211_STA_NONE;
340 /* Mark TID as unreserved */
341 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
343 sta->last_connected = ktime_get_seconds();
344 ewma_signal_init(&sta->rx_stats_avg.signal);
345 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
346 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
348 if (local->ops->wake_tx_queue) {
350 int size = sizeof(struct txq_info) +
351 ALIGN(hw->txq_data_size, sizeof(void *));
353 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
357 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
358 struct txq_info *txq = txq_data + i * size;
360 ieee80211_init_tx_queue(sdata, sta, txq, i);
364 if (sta_prepare_rate_control(local, sta, gfp))
367 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
369 * timer_to_tid must be initialized with identity mapping
370 * to enable session_timer's data differentiation. See
371 * sta_rx_agg_session_timer_expired for usage.
373 sta->timer_to_tid[i] = i;
375 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
376 skb_queue_head_init(&sta->ps_tx_buf[i]);
377 skb_queue_head_init(&sta->tx_filtered[i]);
380 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
381 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
383 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
384 if (sdata->vif.type == NL80211_IFTYPE_AP ||
385 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
386 struct ieee80211_supported_band *sband =
387 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
388 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
389 IEEE80211_HT_CAP_SM_PS_SHIFT;
391 * Assume that hostapd advertises our caps in the beacon and
392 * this is the known_smps_mode for a station that just assciated
395 case WLAN_HT_SMPS_CONTROL_DISABLED:
396 sta->known_smps_mode = IEEE80211_SMPS_OFF;
398 case WLAN_HT_SMPS_CONTROL_STATIC:
399 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
401 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
402 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
409 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
415 kfree(to_txq_info(sta->sta.txq[0]));
417 #ifdef CONFIG_MAC80211_MESH
424 static int sta_info_insert_check(struct sta_info *sta)
426 struct ieee80211_sub_if_data *sdata = sta->sdata;
429 * Can't be a WARN_ON because it can be triggered through a race:
430 * something inserts a STA (on one CPU) without holding the RTNL
431 * and another CPU turns off the net device.
433 if (unlikely(!ieee80211_sdata_running(sdata)))
436 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
437 is_multicast_ether_addr(sta->sta.addr)))
440 /* Strictly speaking this isn't necessary as we hold the mutex, but
441 * the rhashtable code can't really deal with that distinction. We
442 * do require the mutex for correctness though.
445 lockdep_assert_held(&sdata->local->sta_mtx);
446 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
447 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
456 static int sta_info_insert_drv_state(struct ieee80211_local *local,
457 struct ieee80211_sub_if_data *sdata,
458 struct sta_info *sta)
460 enum ieee80211_sta_state state;
463 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
464 err = drv_sta_state(local, sdata, sta, state, state + 1);
471 * Drivers using legacy sta_add/sta_remove callbacks only
472 * get uploaded set to true after sta_add is called.
474 if (!local->ops->sta_add)
475 sta->uploaded = true;
479 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
481 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
482 sta->sta.addr, state + 1, err);
486 /* unwind on error */
487 for (; state > IEEE80211_STA_NOTEXIST; state--)
488 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
494 * should be called with sta_mtx locked
495 * this function replaces the mutex lock
498 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
500 struct ieee80211_local *local = sta->local;
501 struct ieee80211_sub_if_data *sdata = sta->sdata;
502 struct station_info *sinfo;
505 lockdep_assert_held(&local->sta_mtx);
507 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
513 /* check if STA exists already */
514 if (sta_info_get_bss(sdata, sta->sta.addr)) {
520 local->sta_generation++;
523 /* simplify things and don't accept BA sessions yet */
524 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
526 /* make the station visible */
527 sta_info_hash_add(local, sta);
529 list_add_tail_rcu(&sta->list, &local->sta_list);
532 err = sta_info_insert_drv_state(local, sdata, sta);
536 set_sta_flag(sta, WLAN_STA_INSERTED);
537 /* accept BA sessions now */
538 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
540 ieee80211_sta_debugfs_add(sta);
541 rate_control_add_sta_debugfs(sta);
543 sinfo->generation = local->sta_generation;
544 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
547 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
549 /* move reference to rcu-protected */
551 mutex_unlock(&local->sta_mtx);
553 if (ieee80211_vif_is_mesh(&sdata->vif))
554 mesh_accept_plinks_update(sdata);
558 sta_info_hash_del(local, sta);
559 list_del_rcu(&sta->list);
562 __cleanup_single_sta(sta);
564 mutex_unlock(&local->sta_mtx);
570 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
572 struct ieee80211_local *local = sta->local;
577 mutex_lock(&local->sta_mtx);
579 err = sta_info_insert_check(sta);
581 mutex_unlock(&local->sta_mtx);
586 err = sta_info_insert_finish(sta);
592 sta_info_free(local, sta);
596 int sta_info_insert(struct sta_info *sta)
598 int err = sta_info_insert_rcu(sta);
605 static inline void __bss_tim_set(u8 *tim, u16 id)
608 * This format has been mandated by the IEEE specifications,
609 * so this line may not be changed to use the __set_bit() format.
611 tim[id / 8] |= (1 << (id % 8));
614 static inline void __bss_tim_clear(u8 *tim, u16 id)
617 * This format has been mandated by the IEEE specifications,
618 * so this line may not be changed to use the __clear_bit() format.
620 tim[id / 8] &= ~(1 << (id % 8));
623 static inline bool __bss_tim_get(u8 *tim, u16 id)
626 * This format has been mandated by the IEEE specifications,
627 * so this line may not be changed to use the test_bit() format.
629 return tim[id / 8] & (1 << (id % 8));
632 static unsigned long ieee80211_tids_for_ac(int ac)
634 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
636 case IEEE80211_AC_VO:
637 return BIT(6) | BIT(7);
638 case IEEE80211_AC_VI:
639 return BIT(4) | BIT(5);
640 case IEEE80211_AC_BE:
641 return BIT(0) | BIT(3);
642 case IEEE80211_AC_BK:
643 return BIT(1) | BIT(2);
650 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
652 struct ieee80211_local *local = sta->local;
654 bool indicate_tim = false;
655 u8 ignore_for_tim = sta->sta.uapsd_queues;
657 u16 id = sta->sta.aid;
659 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
660 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
661 if (WARN_ON_ONCE(!sta->sdata->bss))
664 ps = &sta->sdata->bss->ps;
665 #ifdef CONFIG_MAC80211_MESH
666 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
667 ps = &sta->sdata->u.mesh.ps;
673 /* No need to do anything if the driver does all */
674 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
681 * If all ACs are delivery-enabled then we should build
682 * the TIM bit for all ACs anyway; if only some are then
683 * we ignore those and build the TIM bit using only the
686 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
690 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
692 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
695 if (ignore_for_tim & BIT(ac))
698 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
699 !skb_queue_empty(&sta->ps_tx_buf[ac]);
703 tids = ieee80211_tids_for_ac(ac);
706 sta->driver_buffered_tids & tids;
708 sta->txq_buffered_tids & tids;
712 spin_lock_bh(&local->tim_lock);
714 if (indicate_tim == __bss_tim_get(ps->tim, id))
718 __bss_tim_set(ps->tim, id);
720 __bss_tim_clear(ps->tim, id);
722 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
723 local->tim_in_locked_section = true;
724 drv_set_tim(local, &sta->sta, indicate_tim);
725 local->tim_in_locked_section = false;
729 spin_unlock_bh(&local->tim_lock);
732 void sta_info_recalc_tim(struct sta_info *sta)
734 __sta_info_recalc_tim(sta, false);
737 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
739 struct ieee80211_tx_info *info;
745 info = IEEE80211_SKB_CB(skb);
747 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
748 timeout = (sta->listen_interval *
749 sta->sdata->vif.bss_conf.beacon_int *
751 if (timeout < STA_TX_BUFFER_EXPIRE)
752 timeout = STA_TX_BUFFER_EXPIRE;
753 return time_after(jiffies, info->control.jiffies + timeout);
757 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
758 struct sta_info *sta, int ac)
764 * First check for frames that should expire on the filtered
765 * queue. Frames here were rejected by the driver and are on
766 * a separate queue to avoid reordering with normal PS-buffered
767 * frames. They also aren't accounted for right now in the
768 * total_ps_buffered counter.
771 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
772 skb = skb_peek(&sta->tx_filtered[ac]);
773 if (sta_info_buffer_expired(sta, skb))
774 skb = __skb_dequeue(&sta->tx_filtered[ac]);
777 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
780 * Frames are queued in order, so if this one
781 * hasn't expired yet we can stop testing. If
782 * we actually reached the end of the queue we
783 * also need to stop, of course.
787 ieee80211_free_txskb(&local->hw, skb);
791 * Now also check the normal PS-buffered queue, this will
792 * only find something if the filtered queue was emptied
793 * since the filtered frames are all before the normal PS
797 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
798 skb = skb_peek(&sta->ps_tx_buf[ac]);
799 if (sta_info_buffer_expired(sta, skb))
800 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
803 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
806 * frames are queued in order, so if this one
807 * hasn't expired yet (or we reached the end of
808 * the queue) we can stop testing
813 local->total_ps_buffered--;
814 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
816 ieee80211_free_txskb(&local->hw, skb);
820 * Finally, recalculate the TIM bit for this station -- it might
821 * now be clear because the station was too slow to retrieve its
824 sta_info_recalc_tim(sta);
827 * Return whether there are any frames still buffered, this is
828 * used to check whether the cleanup timer still needs to run,
829 * if there are no frames we don't need to rearm the timer.
831 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
832 skb_queue_empty(&sta->tx_filtered[ac]));
835 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
836 struct sta_info *sta)
838 bool have_buffered = false;
841 /* This is only necessary for stations on BSS/MBSS interfaces */
842 if (!sta->sdata->bss &&
843 !ieee80211_vif_is_mesh(&sta->sdata->vif))
846 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
848 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
850 return have_buffered;
853 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
855 struct ieee80211_local *local;
856 struct ieee80211_sub_if_data *sdata;
867 lockdep_assert_held(&local->sta_mtx);
870 * Before removing the station from the driver and
871 * rate control, it might still start new aggregation
872 * sessions -- block that to make sure the tear-down
873 * will be sufficient.
875 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
876 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
879 * Before removing the station from the driver there might be pending
880 * rx frames on RSS queues sent prior to the disassociation - wait for
881 * all such frames to be processed.
883 drv_sync_rx_queues(local, sta);
885 ret = sta_info_hash_del(local, sta);
890 * for TDLS peers, make sure to return to the base channel before
893 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
894 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
895 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
898 list_del_rcu(&sta->list);
901 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
903 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
904 rcu_access_pointer(sdata->u.vlan.sta) == sta)
905 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
910 static void __sta_info_destroy_part2(struct sta_info *sta)
912 struct ieee80211_local *local = sta->local;
913 struct ieee80211_sub_if_data *sdata = sta->sdata;
914 struct station_info *sinfo;
918 * NOTE: This assumes at least synchronize_net() was done
919 * after _part1 and before _part2!
923 lockdep_assert_held(&local->sta_mtx);
925 /* now keys can no longer be reached */
926 ieee80211_free_sta_keys(local, sta);
928 /* disable TIM bit - last chance to tell driver */
929 __sta_info_recalc_tim(sta, true);
934 local->sta_generation++;
936 while (sta->sta_state > IEEE80211_STA_NONE) {
937 ret = sta_info_move_state(sta, sta->sta_state - 1);
945 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
946 IEEE80211_STA_NOTEXIST);
947 WARN_ON_ONCE(ret != 0);
950 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
952 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
954 sta_set_sinfo(sta, sinfo);
955 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
958 rate_control_remove_sta_debugfs(sta);
959 ieee80211_sta_debugfs_remove(sta);
961 cleanup_single_sta(sta);
964 int __must_check __sta_info_destroy(struct sta_info *sta)
966 int err = __sta_info_destroy_part1(sta);
973 __sta_info_destroy_part2(sta);
978 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
980 struct sta_info *sta;
983 mutex_lock(&sdata->local->sta_mtx);
984 sta = sta_info_get(sdata, addr);
985 ret = __sta_info_destroy(sta);
986 mutex_unlock(&sdata->local->sta_mtx);
991 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
994 struct sta_info *sta;
997 mutex_lock(&sdata->local->sta_mtx);
998 sta = sta_info_get_bss(sdata, addr);
999 ret = __sta_info_destroy(sta);
1000 mutex_unlock(&sdata->local->sta_mtx);
1005 static void sta_info_cleanup(unsigned long data)
1007 struct ieee80211_local *local = (struct ieee80211_local *) data;
1008 struct sta_info *sta;
1009 bool timer_needed = false;
1012 list_for_each_entry_rcu(sta, &local->sta_list, list)
1013 if (sta_info_cleanup_expire_buffered(local, sta))
1014 timer_needed = true;
1017 if (local->quiescing)
1023 mod_timer(&local->sta_cleanup,
1024 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1027 u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1029 return jhash(key, ETH_ALEN, seed);
1032 int sta_info_init(struct ieee80211_local *local)
1036 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1040 spin_lock_init(&local->tim_lock);
1041 mutex_init(&local->sta_mtx);
1042 INIT_LIST_HEAD(&local->sta_list);
1044 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1045 (unsigned long)local);
1049 void sta_info_stop(struct ieee80211_local *local)
1051 del_timer_sync(&local->sta_cleanup);
1052 rhashtable_destroy(&local->sta_hash);
1056 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1058 struct ieee80211_local *local = sdata->local;
1059 struct sta_info *sta, *tmp;
1060 LIST_HEAD(free_list);
1065 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1066 WARN_ON(vlans && !sdata->bss);
1068 mutex_lock(&local->sta_mtx);
1069 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1070 if (sdata == sta->sdata ||
1071 (vlans && sdata->bss == sta->sdata->bss)) {
1072 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1073 list_add(&sta->free_list, &free_list);
1078 if (!list_empty(&free_list)) {
1080 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1081 __sta_info_destroy_part2(sta);
1083 mutex_unlock(&local->sta_mtx);
1088 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1089 unsigned long exp_time)
1091 struct ieee80211_local *local = sdata->local;
1092 struct sta_info *sta, *tmp;
1094 mutex_lock(&local->sta_mtx);
1096 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1097 if (sdata != sta->sdata)
1100 if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
1101 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1104 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1105 test_sta_flag(sta, WLAN_STA_PS_STA))
1106 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1108 WARN_ON(__sta_info_destroy(sta));
1112 mutex_unlock(&local->sta_mtx);
1115 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1117 const u8 *localaddr)
1119 struct ieee80211_local *local = hw_to_local(hw);
1120 struct sta_info *sta;
1121 struct rhash_head *tmp;
1122 const struct bucket_table *tbl;
1124 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1127 * Just return a random station if localaddr is NULL
1128 * ... first in list.
1130 for_each_sta_info(local, tbl, addr, sta, tmp) {
1132 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1141 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1143 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1146 struct sta_info *sta;
1151 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1160 EXPORT_SYMBOL(ieee80211_find_sta);
1162 /* powersave support code */
1163 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1165 struct ieee80211_sub_if_data *sdata = sta->sdata;
1166 struct ieee80211_local *local = sdata->local;
1167 struct sk_buff_head pending;
1168 int filtered = 0, buffered = 0, ac, i;
1169 unsigned long flags;
1172 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1173 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1176 if (sdata->vif.type == NL80211_IFTYPE_AP)
1177 ps = &sdata->bss->ps;
1178 else if (ieee80211_vif_is_mesh(&sdata->vif))
1179 ps = &sdata->u.mesh.ps;
1183 clear_sta_flag(sta, WLAN_STA_SP);
1185 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1186 sta->driver_buffered_tids = 0;
1187 sta->txq_buffered_tids = 0;
1189 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1190 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1192 if (sta->sta.txq[0]) {
1193 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1194 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1196 if (!skb_queue_len(&txqi->queue))
1199 drv_wake_tx_queue(local, txqi);
1203 skb_queue_head_init(&pending);
1205 /* sync with ieee80211_tx_h_unicast_ps_buf */
1206 spin_lock(&sta->ps_lock);
1207 /* Send all buffered frames to the station */
1208 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1209 int count = skb_queue_len(&pending), tmp;
1211 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1212 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1213 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1214 tmp = skb_queue_len(&pending);
1215 filtered += tmp - count;
1218 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1219 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1220 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1221 tmp = skb_queue_len(&pending);
1222 buffered += tmp - count;
1225 ieee80211_add_pending_skbs(local, &pending);
1227 /* now we're no longer in the deliver code */
1228 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1230 /* The station might have polled and then woken up before we responded,
1231 * so clear these flags now to avoid them sticking around.
1233 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1234 clear_sta_flag(sta, WLAN_STA_UAPSD);
1235 spin_unlock(&sta->ps_lock);
1237 atomic_dec(&ps->num_sta_ps);
1239 /* This station just woke up and isn't aware of our SMPS state */
1240 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1241 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1242 sdata->smps_mode) &&
1243 sta->known_smps_mode != sdata->bss->req_smps &&
1244 sta_info_tx_streams(sta) != 1) {
1246 "%pM just woke up and MIMO capable - update SMPS\n",
1248 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1250 sdata->vif.bss_conf.bssid);
1253 local->total_ps_buffered -= buffered;
1255 sta_info_recalc_tim(sta);
1258 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1259 sta->sta.addr, sta->sta.aid, filtered, buffered);
1261 ieee80211_check_fast_xmit(sta);
1264 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1265 enum ieee80211_frame_release_type reason,
1266 bool call_driver, bool more_data)
1268 struct ieee80211_sub_if_data *sdata = sta->sdata;
1269 struct ieee80211_local *local = sdata->local;
1270 struct ieee80211_qos_hdr *nullfunc;
1271 struct sk_buff *skb;
1272 int size = sizeof(*nullfunc);
1274 bool qos = sta->sta.wme;
1275 struct ieee80211_tx_info *info;
1276 struct ieee80211_chanctx_conf *chanctx_conf;
1279 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1280 IEEE80211_STYPE_QOS_NULLFUNC |
1281 IEEE80211_FCTL_FROMDS);
1284 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1285 IEEE80211_STYPE_NULLFUNC |
1286 IEEE80211_FCTL_FROMDS);
1289 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1293 skb_reserve(skb, local->hw.extra_tx_headroom);
1295 nullfunc = (void *) skb_put(skb, size);
1296 nullfunc->frame_control = fc;
1297 nullfunc->duration_id = 0;
1298 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1299 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1300 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1301 nullfunc->seq_ctrl = 0;
1303 skb->priority = tid;
1304 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1306 nullfunc->qos_ctrl = cpu_to_le16(tid);
1308 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1309 nullfunc->qos_ctrl |=
1310 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1312 nullfunc->frame_control |=
1313 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1317 info = IEEE80211_SKB_CB(skb);
1320 * Tell TX path to send this frame even though the
1321 * STA may still remain is PS mode after this frame
1322 * exchange. Also set EOSP to indicate this packet
1323 * ends the poll/service period.
1325 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1326 IEEE80211_TX_STATUS_EOSP |
1327 IEEE80211_TX_CTL_REQ_TX_STATUS;
1329 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1332 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1335 skb->dev = sdata->dev;
1338 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1339 if (WARN_ON(!chanctx_conf)) {
1345 info->band = chanctx_conf->def.chan->band;
1346 ieee80211_xmit(sdata, sta, skb);
1350 static int find_highest_prio_tid(unsigned long tids)
1352 /* lower 3 TIDs aren't ordered perfectly */
1354 return fls(tids) - 1;
1355 /* TID 0 is BE just like TID 3 */
1358 return fls(tids) - 1;
1361 /* Indicates if the MORE_DATA bit should be set in the last
1362 * frame obtained by ieee80211_sta_ps_get_frames.
1363 * Note that driver_release_tids is relevant only if
1364 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1367 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1368 enum ieee80211_frame_release_type reason,
1369 unsigned long driver_release_tids)
1373 /* If the driver has data on more than one TID then
1374 * certainly there's more data if we release just a
1375 * single frame now (from a single TID). This will
1376 * only happen for PS-Poll.
1378 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1379 hweight16(driver_release_tids) > 1)
1382 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1383 if (ignored_acs & BIT(ac))
1386 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1387 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1395 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1396 enum ieee80211_frame_release_type reason,
1397 struct sk_buff_head *frames,
1398 unsigned long *driver_release_tids)
1400 struct ieee80211_sub_if_data *sdata = sta->sdata;
1401 struct ieee80211_local *local = sdata->local;
1404 /* Get response frame(s) and more data bit for the last one. */
1405 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1408 if (ignored_acs & BIT(ac))
1411 tids = ieee80211_tids_for_ac(ac);
1413 /* if we already have frames from software, then we can't also
1414 * release from hardware queues
1416 if (skb_queue_empty(frames)) {
1417 *driver_release_tids |=
1418 sta->driver_buffered_tids & tids;
1419 *driver_release_tids |= sta->txq_buffered_tids & tids;
1422 if (!*driver_release_tids) {
1423 struct sk_buff *skb;
1425 while (n_frames > 0) {
1426 skb = skb_dequeue(&sta->tx_filtered[ac]);
1429 &sta->ps_tx_buf[ac]);
1431 local->total_ps_buffered--;
1436 __skb_queue_tail(frames, skb);
1440 /* If we have more frames buffered on this AC, then abort the
1441 * loop since we can't send more data from other ACs before
1442 * the buffered frames from this.
1444 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1445 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1451 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1452 int n_frames, u8 ignored_acs,
1453 enum ieee80211_frame_release_type reason)
1455 struct ieee80211_sub_if_data *sdata = sta->sdata;
1456 struct ieee80211_local *local = sdata->local;
1457 unsigned long driver_release_tids = 0;
1458 struct sk_buff_head frames;
1461 /* Service or PS-Poll period starts */
1462 set_sta_flag(sta, WLAN_STA_SP);
1464 __skb_queue_head_init(&frames);
1466 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1467 &frames, &driver_release_tids);
1469 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1471 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1472 driver_release_tids =
1473 BIT(find_highest_prio_tid(driver_release_tids));
1475 if (skb_queue_empty(&frames) && !driver_release_tids) {
1479 * For PS-Poll, this can only happen due to a race condition
1480 * when we set the TIM bit and the station notices it, but
1481 * before it can poll for the frame we expire it.
1483 * For uAPSD, this is said in the standard (11.2.1.5 h):
1484 * At each unscheduled SP for a non-AP STA, the AP shall
1485 * attempt to transmit at least one MSDU or MMPDU, but no
1486 * more than the value specified in the Max SP Length field
1487 * in the QoS Capability element from delivery-enabled ACs,
1488 * that are destined for the non-AP STA.
1490 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1493 /* This will evaluate to 1, 3, 5 or 7. */
1494 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1496 ieee80211_send_null_response(sta, tid, reason, true, false);
1497 } else if (!driver_release_tids) {
1498 struct sk_buff_head pending;
1499 struct sk_buff *skb;
1502 bool need_null = false;
1504 skb_queue_head_init(&pending);
1506 while ((skb = __skb_dequeue(&frames))) {
1507 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1508 struct ieee80211_hdr *hdr = (void *) skb->data;
1514 * Tell TX path to send this frame even though the
1515 * STA may still remain is PS mode after this frame
1518 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1519 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1522 * Use MoreData flag to indicate whether there are
1523 * more buffered frames for this STA
1525 if (more_data || !skb_queue_empty(&frames))
1526 hdr->frame_control |=
1527 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1529 hdr->frame_control &=
1530 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1532 if (ieee80211_is_data_qos(hdr->frame_control) ||
1533 ieee80211_is_qos_nullfunc(hdr->frame_control))
1534 qoshdr = ieee80211_get_qos_ctl(hdr);
1536 tids |= BIT(skb->priority);
1538 __skb_queue_tail(&pending, skb);
1540 /* end service period after last frame or add one */
1541 if (!skb_queue_empty(&frames))
1544 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1545 /* for PS-Poll, there's only one frame */
1546 info->flags |= IEEE80211_TX_STATUS_EOSP |
1547 IEEE80211_TX_CTL_REQ_TX_STATUS;
1551 /* For uAPSD, things are a bit more complicated. If the
1552 * last frame has a QoS header (i.e. is a QoS-data or
1553 * QoS-nulldata frame) then just set the EOSP bit there
1555 * If the frame doesn't have a QoS header (which means
1556 * it should be a bufferable MMPDU) then we can't set
1557 * the EOSP bit in the QoS header; add a QoS-nulldata
1558 * frame to the list to send it after the MMPDU.
1560 * Note that this code is only in the mac80211-release
1561 * code path, we assume that the driver will not buffer
1562 * anything but QoS-data frames, or if it does, will
1563 * create the QoS-nulldata frame by itself if needed.
1565 * Cf. 802.11-2012 10.2.1.10 (c).
1568 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1570 info->flags |= IEEE80211_TX_STATUS_EOSP |
1571 IEEE80211_TX_CTL_REQ_TX_STATUS;
1573 /* The standard isn't completely clear on this
1574 * as it says the more-data bit should be set
1575 * if there are more BUs. The QoS-Null frame
1576 * we're about to send isn't buffered yet, we
1577 * only create it below, but let's pretend it
1578 * was buffered just in case some clients only
1579 * expect more-data=0 when eosp=1.
1581 hdr->frame_control |=
1582 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1589 drv_allow_buffered_frames(local, sta, tids, num,
1592 ieee80211_add_pending_skbs(local, &pending);
1595 ieee80211_send_null_response(
1596 sta, find_highest_prio_tid(tids),
1597 reason, false, false);
1599 sta_info_recalc_tim(sta);
1601 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1605 * We need to release a frame that is buffered somewhere in the
1606 * driver ... it'll have to handle that.
1607 * Note that the driver also has to check the number of frames
1608 * on the TIDs we're releasing from - if there are more than
1609 * n_frames it has to set the more-data bit (if we didn't ask
1610 * it to set it anyway due to other buffered frames); if there
1611 * are fewer than n_frames it has to make sure to adjust that
1612 * to allow the service period to end properly.
1614 drv_release_buffered_frames(local, sta, driver_release_tids,
1615 n_frames, reason, more_data);
1618 * Note that we don't recalculate the TIM bit here as it would
1619 * most likely have no effect at all unless the driver told us
1620 * that the TID(s) became empty before returning here from the
1622 * Either way, however, when the driver tells us that the TID(s)
1623 * became empty or we find that a txq became empty, we'll do the
1624 * TIM recalculation.
1627 if (!sta->sta.txq[0])
1630 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1631 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1633 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1636 sta_info_recalc_tim(sta);
1642 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1644 u8 ignore_for_response = sta->sta.uapsd_queues;
1647 * If all ACs are delivery-enabled then we should reply
1648 * from any of them, if only some are enabled we reply
1649 * only from the non-enabled ones.
1651 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1652 ignore_for_response = 0;
1654 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1655 IEEE80211_FRAME_RELEASE_PSPOLL);
1658 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1660 int n_frames = sta->sta.max_sp;
1661 u8 delivery_enabled = sta->sta.uapsd_queues;
1664 * If we ever grow support for TSPEC this might happen if
1665 * the TSPEC update from hostapd comes in between a trigger
1666 * frame setting WLAN_STA_UAPSD in the RX path and this
1667 * actually getting called.
1669 if (!delivery_enabled)
1672 switch (sta->sta.max_sp) {
1683 /* XXX: what is a good value? */
1688 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1689 IEEE80211_FRAME_RELEASE_UAPSD);
1692 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1693 struct ieee80211_sta *pubsta, bool block)
1695 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1697 trace_api_sta_block_awake(sta->local, pubsta, block);
1700 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1701 ieee80211_clear_fast_xmit(sta);
1705 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1708 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1709 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1710 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1711 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1712 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1713 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1714 /* must be asleep in this case */
1715 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1716 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1718 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1719 ieee80211_check_fast_xmit(sta);
1722 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1724 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1726 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1727 struct ieee80211_local *local = sta->local;
1729 trace_api_eosp(local, pubsta);
1731 clear_sta_flag(sta, WLAN_STA_SP);
1733 EXPORT_SYMBOL(ieee80211_sta_eosp);
1735 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1737 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1738 enum ieee80211_frame_release_type reason;
1741 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1743 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1744 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1747 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1749 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1751 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1752 u8 tid, bool buffered)
1754 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1756 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1759 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1762 set_bit(tid, &sta->driver_buffered_tids);
1764 clear_bit(tid, &sta->driver_buffered_tids);
1766 sta_info_recalc_tim(sta);
1768 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1771 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
1773 struct ieee80211_local *local = sdata->local;
1774 bool allow_p2p_go_ps = sdata->vif.p2p;
1775 struct sta_info *sta;
1778 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1779 if (sdata != sta->sdata ||
1780 !test_sta_flag(sta, WLAN_STA_ASSOC))
1782 if (!sta->sta.support_p2p_ps) {
1783 allow_p2p_go_ps = false;
1789 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
1790 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
1791 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
1795 int sta_info_move_state(struct sta_info *sta,
1796 enum ieee80211_sta_state new_state)
1800 if (sta->sta_state == new_state)
1803 /* check allowed transitions first */
1805 switch (new_state) {
1806 case IEEE80211_STA_NONE:
1807 if (sta->sta_state != IEEE80211_STA_AUTH)
1810 case IEEE80211_STA_AUTH:
1811 if (sta->sta_state != IEEE80211_STA_NONE &&
1812 sta->sta_state != IEEE80211_STA_ASSOC)
1815 case IEEE80211_STA_ASSOC:
1816 if (sta->sta_state != IEEE80211_STA_AUTH &&
1817 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1820 case IEEE80211_STA_AUTHORIZED:
1821 if (sta->sta_state != IEEE80211_STA_ASSOC)
1825 WARN(1, "invalid state %d", new_state);
1829 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1830 sta->sta.addr, new_state);
1833 * notify the driver before the actual changes so it can
1834 * fail the transition
1836 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1837 int err = drv_sta_state(sta->local, sta->sdata, sta,
1838 sta->sta_state, new_state);
1843 /* reflect the change in all state variables */
1845 switch (new_state) {
1846 case IEEE80211_STA_NONE:
1847 if (sta->sta_state == IEEE80211_STA_AUTH)
1848 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1850 case IEEE80211_STA_AUTH:
1851 if (sta->sta_state == IEEE80211_STA_NONE) {
1852 set_bit(WLAN_STA_AUTH, &sta->_flags);
1853 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1854 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1855 ieee80211_recalc_min_chandef(sta->sdata);
1856 if (!sta->sta.support_p2p_ps)
1857 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1860 case IEEE80211_STA_ASSOC:
1861 if (sta->sta_state == IEEE80211_STA_AUTH) {
1862 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1863 ieee80211_recalc_min_chandef(sta->sdata);
1864 if (!sta->sta.support_p2p_ps)
1865 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1866 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1867 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1868 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1869 !sta->sdata->u.vlan.sta))
1870 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1871 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1872 ieee80211_clear_fast_xmit(sta);
1875 case IEEE80211_STA_AUTHORIZED:
1876 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1877 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1878 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1879 !sta->sdata->u.vlan.sta))
1880 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1881 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1882 ieee80211_check_fast_xmit(sta);
1889 sta->sta_state = new_state;
1894 u8 sta_info_tx_streams(struct sta_info *sta)
1896 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1899 if (!sta->sta.ht_cap.ht_supported)
1902 if (sta->sta.vht_cap.vht_supported) {
1905 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1907 for (i = 7; i >= 0; i--)
1908 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1909 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1913 if (ht_cap->mcs.rx_mask[3])
1915 else if (ht_cap->mcs.rx_mask[2])
1917 else if (ht_cap->mcs.rx_mask[1])
1922 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1925 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1926 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1929 static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1933 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
1934 rinfo->flags |= RATE_INFO_FLAGS_MCS;
1935 rinfo->mcs = sta->rx_stats.last_rate_idx;
1936 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
1937 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1938 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1939 rinfo->mcs = sta->rx_stats.last_rate_idx;
1941 struct ieee80211_supported_band *sband;
1942 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1945 sband = sta->local->hw.wiphy->bands[
1946 ieee80211_get_sdata_band(sta->sdata)];
1947 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
1948 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1951 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
1952 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1954 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
1955 rinfo->bw = RATE_INFO_BW_5;
1956 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
1957 rinfo->bw = RATE_INFO_BW_10;
1958 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
1959 rinfo->bw = RATE_INFO_BW_40;
1960 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1961 rinfo->bw = RATE_INFO_BW_80;
1962 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1963 rinfo->bw = RATE_INFO_BW_160;
1965 rinfo->bw = RATE_INFO_BW_20;
1968 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1970 struct ieee80211_sub_if_data *sdata = sta->sdata;
1971 struct ieee80211_local *local = sdata->local;
1972 struct rate_control_ref *ref = NULL;
1976 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1977 ref = local->rate_ctrl;
1979 sinfo->generation = sdata->local->sta_generation;
1981 /* do before driver, so beacon filtering drivers have a
1982 * chance to e.g. just add the number of filtered beacons
1983 * (or just modify the value entirely, of course)
1985 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1986 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1988 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1990 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1991 BIT(NL80211_STA_INFO_STA_FLAGS) |
1992 BIT(NL80211_STA_INFO_BSS_PARAM) |
1993 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1994 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1996 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1997 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1998 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2001 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2002 sinfo->inactive_time =
2003 jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
2005 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2006 BIT(NL80211_STA_INFO_TX_BYTES)))) {
2007 sinfo->tx_bytes = 0;
2008 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2009 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2010 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2013 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
2014 sinfo->tx_packets = 0;
2015 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2016 sinfo->tx_packets += sta->tx_stats.packets[ac];
2017 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
2020 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2021 BIT(NL80211_STA_INFO_RX_BYTES)))) {
2022 sinfo->rx_bytes = sta->rx_stats.bytes;
2023 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
2026 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2027 sinfo->rx_packets = sta->rx_stats.packets;
2028 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
2031 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2032 sinfo->tx_retries = sta->status_stats.retry_count;
2033 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
2036 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2037 sinfo->tx_failed = sta->status_stats.retry_failed;
2038 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
2041 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2043 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2044 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2045 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2046 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2047 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2050 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2051 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2052 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2053 sinfo->signal = (s8)sta->rx_stats.last_signal;
2054 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2057 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
2059 -ewma_signal_read(&sta->rx_stats_avg.signal);
2060 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2064 if (sta->rx_stats.chains &&
2065 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2066 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2067 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2068 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2070 sinfo->chains = sta->rx_stats.chains;
2071 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2072 sinfo->chain_signal[i] =
2073 sta->rx_stats.chain_signal_last[i];
2074 sinfo->chain_signal_avg[i] =
2075 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2079 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2080 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2082 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2085 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2086 sta_set_rate_info_rx(sta, &sinfo->rxrate);
2087 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2090 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2091 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2092 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2094 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2095 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2096 tidstats->rx_msdu = sta->rx_stats.msdu[i];
2099 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2100 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2101 tidstats->tx_msdu = sta->tx_stats.msdu[i];
2104 if (!(tidstats->filled &
2105 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2106 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2108 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2109 tidstats->tx_msdu_retries =
2110 sta->status_stats.msdu_retries[i];
2113 if (!(tidstats->filled &
2114 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2115 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2117 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2118 tidstats->tx_msdu_failed =
2119 sta->status_stats.msdu_failed[i];
2123 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2124 #ifdef CONFIG_MAC80211_MESH
2125 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2126 BIT(NL80211_STA_INFO_PLID) |
2127 BIT(NL80211_STA_INFO_PLINK_STATE) |
2128 BIT(NL80211_STA_INFO_LOCAL_PM) |
2129 BIT(NL80211_STA_INFO_PEER_PM) |
2130 BIT(NL80211_STA_INFO_NONPEER_PM);
2132 sinfo->llid = sta->mesh->llid;
2133 sinfo->plid = sta->mesh->plid;
2134 sinfo->plink_state = sta->mesh->plink_state;
2135 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2136 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2137 sinfo->t_offset = sta->mesh->t_offset;
2139 sinfo->local_pm = sta->mesh->local_pm;
2140 sinfo->peer_pm = sta->mesh->peer_pm;
2141 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2145 sinfo->bss_param.flags = 0;
2146 if (sdata->vif.bss_conf.use_cts_prot)
2147 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2148 if (sdata->vif.bss_conf.use_short_preamble)
2149 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2150 if (sdata->vif.bss_conf.use_short_slot)
2151 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2152 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2153 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2155 sinfo->sta_flags.set = 0;
2156 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2157 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2158 BIT(NL80211_STA_FLAG_WME) |
2159 BIT(NL80211_STA_FLAG_MFP) |
2160 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2161 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2162 BIT(NL80211_STA_FLAG_TDLS_PEER);
2163 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2164 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2165 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2166 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2168 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2169 if (test_sta_flag(sta, WLAN_STA_MFP))
2170 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2171 if (test_sta_flag(sta, WLAN_STA_AUTH))
2172 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2173 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2174 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2175 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2176 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2178 /* check if the driver has a SW RC implementation */
2179 if (ref && ref->ops->get_expected_throughput)
2180 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2182 thr = drv_get_expected_throughput(local, &sta->sta);
2185 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2186 sinfo->expected_throughput = thr;