Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
7 * Copyright (C) 2018-2021 Intel Corporation
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/etherdevice.h>
13#include <linux/netdevice.h>
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <linux/skbuff.h>
17#include <linux/if_arp.h>
18#include <linux/timer.h>
19#include <linux/rtnetlink.h>
20
21#include <net/codel.h>
22#include <net/mac80211.h>
23#include "ieee80211_i.h"
24#include "driver-ops.h"
25#include "rate.h"
26#include "sta_info.h"
27#include "debugfs_sta.h"
28#include "mesh.h"
29#include "wme.h"
30
31/**
32 * DOC: STA information lifetime rules
33 *
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
37 *
38 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it, in
44 * particular, it may not start any mesh peer link management or add
45 * encryption keys.
46 *
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
49 *
50 * Station entries are added by mac80211 when you establish a link with a
51 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
53 * receive an association response from the AP. For IBSS this occurs when
54 * get to know about a peer on the same IBSS. For WDS we add the sta for
55 * the peer immediately upon device open. When using AP mode we add stations
56 * for each respective station upon request from userspace through nl80211.
57 *
58 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
60 *
61 * There is no concept of ownership on a STA entry, each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
65 */
66
67static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
69 .automatic_shrinking = true,
70 .head_offset = offsetof(struct sta_info, hash_node),
71 .key_offset = offsetof(struct sta_info, addr),
72 .key_len = ETH_ALEN,
73 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
74};
75
76/* Caller must hold local->sta_mtx */
77static int sta_info_hash_del(struct ieee80211_local *local,
78 struct sta_info *sta)
79{
80 return rhltable_remove(&local->sta_hash, &sta->hash_node,
81 sta_rht_params);
82}
83
84static void __cleanup_single_sta(struct sta_info *sta)
85{
86 int ac, i;
87 struct tid_ampdu_tx *tid_tx;
88 struct ieee80211_sub_if_data *sdata = sta->sdata;
89 struct ieee80211_local *local = sdata->local;
90 struct ps_data *ps;
91
92 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
93 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
94 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
95 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
97 ps = &sdata->bss->ps;
98 else if (ieee80211_vif_is_mesh(&sdata->vif))
99 ps = &sdata->u.mesh.ps;
100 else
101 return;
102
103 clear_sta_flag(sta, WLAN_STA_PS_STA);
104 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
105 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
106
107 atomic_dec(&ps->num_sta_ps);
108 }
109
110 if (sta->sta.txq[0]) {
111 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112 struct txq_info *txqi;
113
114 if (!sta->sta.txq[i])
115 continue;
116
117 txqi = to_txq_info(sta->sta.txq[i]);
118
119 ieee80211_txq_purge(local, txqi);
120 }
121 }
122
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]);
127 }
128
129 if (ieee80211_vif_is_mesh(&sdata->vif))
130 mesh_sta_cleanup(sta);
131
132 cancel_work_sync(&sta->drv_deliver_wk);
133
134 /*
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.
139 */
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]);
143 if (!tid_tx)
144 continue;
145 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
146 kfree(tid_tx);
147 }
148}
149
150static void cleanup_single_sta(struct sta_info *sta)
151{
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
153 struct ieee80211_local *local = sdata->local;
154
155 __cleanup_single_sta(sta);
156 sta_info_free(local, sta);
157}
158
159struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
160 const u8 *addr)
161{
162 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
163}
164
165/* protected by RCU */
166struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
167 const u8 *addr)
168{
169 struct ieee80211_local *local = sdata->local;
170 struct rhlist_head *tmp;
171 struct sta_info *sta;
172
173 rcu_read_lock();
174 for_each_sta_info(local, addr, sta, tmp) {
175 if (sta->sdata == sdata) {
176 rcu_read_unlock();
177 /* this is safe as the caller must already hold
178 * another rcu read section or the mutex
179 */
180 return sta;
181 }
182 }
183 rcu_read_unlock();
184 return NULL;
185}
186
187/*
188 * Get sta info either from the specified interface
189 * or from one of its vlans
190 */
191struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
192 const u8 *addr)
193{
194 struct ieee80211_local *local = sdata->local;
195 struct rhlist_head *tmp;
196 struct sta_info *sta;
197
198 rcu_read_lock();
199 for_each_sta_info(local, addr, sta, tmp) {
200 if (sta->sdata == sdata ||
201 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
202 rcu_read_unlock();
203 /* this is safe as the caller must already hold
204 * another rcu read section or the mutex
205 */
206 return sta;
207 }
208 }
209 rcu_read_unlock();
210 return NULL;
211}
212
213struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
214 const u8 *sta_addr, const u8 *vif_addr)
215{
216 struct rhlist_head *tmp;
217 struct sta_info *sta;
218
219 for_each_sta_info(local, sta_addr, sta, tmp) {
220 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
221 return sta;
222 }
223
224 return NULL;
225}
226
227struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
228 int idx)
229{
230 struct ieee80211_local *local = sdata->local;
231 struct sta_info *sta;
232 int i = 0;
233
234 list_for_each_entry_rcu(sta, &local->sta_list, list,
235 lockdep_is_held(&local->sta_mtx)) {
236 if (sdata != sta->sdata)
237 continue;
238 if (i < idx) {
239 ++i;
240 continue;
241 }
242 return sta;
243 }
244
245 return NULL;
246}
247
248/**
249 * sta_info_free - free STA
250 *
251 * @local: pointer to the global information
252 * @sta: STA info to free
253 *
254 * This function must undo everything done by sta_info_alloc()
255 * that may happen before sta_info_insert(). It may only be
256 * called when sta_info_insert() has not been attempted (and
257 * if that fails, the station is freed anyway.)
258 */
259void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
260{
261 /*
262 * If we had used sta_info_pre_move_state() then we might not
263 * have gone through the state transitions down again, so do
264 * it here now (and warn if it's inserted).
265 *
266 * This will clear state such as fast TX/RX that may have been
267 * allocated during state transitions.
268 */
269 while (sta->sta_state > IEEE80211_STA_NONE) {
270 int ret;
271
272 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
273
274 ret = sta_info_move_state(sta, sta->sta_state - 1);
275 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
276 break;
277 }
278
279 if (sta->rate_ctrl)
280 rate_control_free_sta(sta);
281
282 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
283
284 if (sta->sta.txq[0])
285 kfree(to_txq_info(sta->sta.txq[0]));
286 kfree(rcu_dereference_raw(sta->sta.rates));
287#ifdef CONFIG_MAC80211_MESH
288 kfree(sta->mesh);
289#endif
290 free_percpu(sta->deflink.pcpu_rx_stats);
291 kfree(sta);
292}
293
294/* Caller must hold local->sta_mtx */
295static int sta_info_hash_add(struct ieee80211_local *local,
296 struct sta_info *sta)
297{
298 return rhltable_insert(&local->sta_hash, &sta->hash_node,
299 sta_rht_params);
300}
301
302static void sta_deliver_ps_frames(struct work_struct *wk)
303{
304 struct sta_info *sta;
305
306 sta = container_of(wk, struct sta_info, drv_deliver_wk);
307
308 if (sta->dead)
309 return;
310
311 local_bh_disable();
312 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
313 ieee80211_sta_ps_deliver_wakeup(sta);
314 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
315 ieee80211_sta_ps_deliver_poll_response(sta);
316 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
317 ieee80211_sta_ps_deliver_uapsd(sta);
318 local_bh_enable();
319}
320
321static int sta_prepare_rate_control(struct ieee80211_local *local,
322 struct sta_info *sta, gfp_t gfp)
323{
324 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
325 return 0;
326
327 sta->rate_ctrl = local->rate_ctrl;
328 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
329 sta, gfp);
330 if (!sta->rate_ctrl_priv)
331 return -ENOMEM;
332
333 return 0;
334}
335
336struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
337 const u8 *addr, gfp_t gfp)
338{
339 struct ieee80211_local *local = sdata->local;
340 struct ieee80211_hw *hw = &local->hw;
341 struct sta_info *sta;
342 int i;
343
344 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
345 if (!sta)
346 return NULL;
347
348 if (ieee80211_hw_check(hw, USES_RSS)) {
349 sta->deflink.pcpu_rx_stats =
350 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
351 if (!sta->deflink.pcpu_rx_stats)
352 goto free;
353 }
354
355 spin_lock_init(&sta->lock);
356 spin_lock_init(&sta->ps_lock);
357 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
358 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
359 mutex_init(&sta->ampdu_mlme.mtx);
360#ifdef CONFIG_MAC80211_MESH
361 if (ieee80211_vif_is_mesh(&sdata->vif)) {
362 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
363 if (!sta->mesh)
364 goto free;
365 sta->mesh->plink_sta = sta;
366 spin_lock_init(&sta->mesh->plink_lock);
367 if (!sdata->u.mesh.user_mpm)
368 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
369 0);
370 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
371 }
372#endif
373
374 memcpy(sta->addr, addr, ETH_ALEN);
375 memcpy(sta->sta.addr, addr, ETH_ALEN);
376 sta->sta.max_rx_aggregation_subframes =
377 local->hw.max_rx_aggregation_subframes;
378
379 /* TODO link specific alloc and assignments for MLO Link STA */
380
381 /* For non MLO STA, link info can be accessed either via deflink
382 * or link[0]
383 */
384 sta->link[0] = &sta->deflink;
385 sta->sta.link[0] = &sta->sta.deflink;
386
387 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
388 * The Tx path starts to use a key as soon as the key slot ptk_idx
389 * references to is not NULL. To not use the initial Rx-only key
390 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
391 * which always will refer to a NULL key.
392 */
393 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
394 sta->ptk_idx = INVALID_PTK_KEYIDX;
395
396 sta->local = local;
397 sta->sdata = sdata;
398 sta->deflink.rx_stats.last_rx = jiffies;
399
400 u64_stats_init(&sta->deflink.rx_stats.syncp);
401
402 ieee80211_init_frag_cache(&sta->frags);
403
404 sta->sta_state = IEEE80211_STA_NONE;
405
406 /* Mark TID as unreserved */
407 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
408
409 sta->last_connected = ktime_get_seconds();
410 ewma_signal_init(&sta->deflink.rx_stats_avg.signal);
411 ewma_avg_signal_init(&sta->deflink.status_stats.avg_ack_signal);
412 for (i = 0; i < ARRAY_SIZE(sta->deflink.rx_stats_avg.chain_signal); i++)
413 ewma_signal_init(&sta->deflink.rx_stats_avg.chain_signal[i]);
414
415 if (local->ops->wake_tx_queue) {
416 void *txq_data;
417 int size = sizeof(struct txq_info) +
418 ALIGN(hw->txq_data_size, sizeof(void *));
419
420 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
421 if (!txq_data)
422 goto free;
423
424 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
425 struct txq_info *txq = txq_data + i * size;
426
427 /* might not do anything for the bufferable MMPDU TXQ */
428 ieee80211_txq_init(sdata, sta, txq, i);
429 }
430 }
431
432 if (sta_prepare_rate_control(local, sta, gfp))
433 goto free_txq;
434
435
436 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
437 skb_queue_head_init(&sta->ps_tx_buf[i]);
438 skb_queue_head_init(&sta->tx_filtered[i]);
439 init_airtime_info(&sta->airtime[i], &local->airtime[i]);
440 }
441
442 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
443 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
444
445 for (i = 0; i < NUM_NL80211_BANDS; i++) {
446 u32 mandatory = 0;
447 int r;
448
449 if (!hw->wiphy->bands[i])
450 continue;
451
452 switch (i) {
453 case NL80211_BAND_2GHZ:
454 case NL80211_BAND_LC:
455 /*
456 * We use both here, even if we cannot really know for
457 * sure the station will support both, but the only use
458 * for this is when we don't know anything yet and send
459 * management frames, and then we'll pick the lowest
460 * possible rate anyway.
461 * If we don't include _G here, we cannot find a rate
462 * in P2P, and thus trigger the WARN_ONCE() in rate.c
463 */
464 mandatory = IEEE80211_RATE_MANDATORY_B |
465 IEEE80211_RATE_MANDATORY_G;
466 break;
467 case NL80211_BAND_5GHZ:
468 mandatory = IEEE80211_RATE_MANDATORY_A;
469 break;
470 case NL80211_BAND_60GHZ:
471 WARN_ON(1);
472 mandatory = 0;
473 break;
474 }
475
476 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
477 struct ieee80211_rate *rate;
478
479 rate = &hw->wiphy->bands[i]->bitrates[r];
480
481 if (!(rate->flags & mandatory))
482 continue;
483 sta->sta.deflink.supp_rates[i] |= BIT(r);
484 }
485 }
486
487 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
488 if (sdata->vif.type == NL80211_IFTYPE_AP ||
489 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
490 struct ieee80211_supported_band *sband;
491 u8 smps;
492
493 sband = ieee80211_get_sband(sdata);
494 if (!sband)
495 goto free_txq;
496
497 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
498 IEEE80211_HT_CAP_SM_PS_SHIFT;
499 /*
500 * Assume that hostapd advertises our caps in the beacon and
501 * this is the known_smps_mode for a station that just assciated
502 */
503 switch (smps) {
504 case WLAN_HT_SMPS_CONTROL_DISABLED:
505 sta->known_smps_mode = IEEE80211_SMPS_OFF;
506 break;
507 case WLAN_HT_SMPS_CONTROL_STATIC:
508 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
509 break;
510 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
511 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
512 break;
513 default:
514 WARN_ON(1);
515 }
516 }
517
518 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
519
520 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
521 sta->cparams.target = MS2TIME(20);
522 sta->cparams.interval = MS2TIME(100);
523 sta->cparams.ecn = true;
524 sta->cparams.ce_threshold_selector = 0;
525 sta->cparams.ce_threshold_mask = 0;
526
527 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
528
529 return sta;
530
531free_txq:
532 if (sta->sta.txq[0])
533 kfree(to_txq_info(sta->sta.txq[0]));
534free:
535 free_percpu(sta->deflink.pcpu_rx_stats);
536#ifdef CONFIG_MAC80211_MESH
537 kfree(sta->mesh);
538#endif
539 kfree(sta);
540 return NULL;
541}
542
543static int sta_info_insert_check(struct sta_info *sta)
544{
545 struct ieee80211_sub_if_data *sdata = sta->sdata;
546
547 /*
548 * Can't be a WARN_ON because it can be triggered through a race:
549 * something inserts a STA (on one CPU) without holding the RTNL
550 * and another CPU turns off the net device.
551 */
552 if (unlikely(!ieee80211_sdata_running(sdata)))
553 return -ENETDOWN;
554
555 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
556 !is_valid_ether_addr(sta->sta.addr)))
557 return -EINVAL;
558
559 /* The RCU read lock is required by rhashtable due to
560 * asynchronous resize/rehash. We also require the mutex
561 * for correctness.
562 */
563 rcu_read_lock();
564 lockdep_assert_held(&sdata->local->sta_mtx);
565 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
566 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
567 rcu_read_unlock();
568 return -ENOTUNIQ;
569 }
570 rcu_read_unlock();
571
572 return 0;
573}
574
575static int sta_info_insert_drv_state(struct ieee80211_local *local,
576 struct ieee80211_sub_if_data *sdata,
577 struct sta_info *sta)
578{
579 enum ieee80211_sta_state state;
580 int err = 0;
581
582 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
583 err = drv_sta_state(local, sdata, sta, state, state + 1);
584 if (err)
585 break;
586 }
587
588 if (!err) {
589 /*
590 * Drivers using legacy sta_add/sta_remove callbacks only
591 * get uploaded set to true after sta_add is called.
592 */
593 if (!local->ops->sta_add)
594 sta->uploaded = true;
595 return 0;
596 }
597
598 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
599 sdata_info(sdata,
600 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
601 sta->sta.addr, state + 1, err);
602 err = 0;
603 }
604
605 /* unwind on error */
606 for (; state > IEEE80211_STA_NOTEXIST; state--)
607 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
608
609 return err;
610}
611
612static void
613ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
614{
615 struct ieee80211_local *local = sdata->local;
616 bool allow_p2p_go_ps = sdata->vif.p2p;
617 struct sta_info *sta;
618
619 rcu_read_lock();
620 list_for_each_entry_rcu(sta, &local->sta_list, list) {
621 if (sdata != sta->sdata ||
622 !test_sta_flag(sta, WLAN_STA_ASSOC))
623 continue;
624 if (!sta->sta.support_p2p_ps) {
625 allow_p2p_go_ps = false;
626 break;
627 }
628 }
629 rcu_read_unlock();
630
631 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
632 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
633 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
634 }
635}
636
637/*
638 * should be called with sta_mtx locked
639 * this function replaces the mutex lock
640 * with a RCU lock
641 */
642static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
643{
644 struct ieee80211_local *local = sta->local;
645 struct ieee80211_sub_if_data *sdata = sta->sdata;
646 struct station_info *sinfo = NULL;
647 int err = 0;
648
649 lockdep_assert_held(&local->sta_mtx);
650
651 /* check if STA exists already */
652 if (sta_info_get_bss(sdata, sta->sta.addr)) {
653 err = -EEXIST;
654 goto out_cleanup;
655 }
656
657 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
658 if (!sinfo) {
659 err = -ENOMEM;
660 goto out_cleanup;
661 }
662
663 local->num_sta++;
664 local->sta_generation++;
665 smp_mb();
666
667 /* simplify things and don't accept BA sessions yet */
668 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
669
670 /* make the station visible */
671 err = sta_info_hash_add(local, sta);
672 if (err)
673 goto out_drop_sta;
674
675 list_add_tail_rcu(&sta->list, &local->sta_list);
676
677 /* update channel context before notifying the driver about state
678 * change, this enables driver using the updated channel context right away.
679 */
680 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
681 ieee80211_recalc_min_chandef(sta->sdata);
682 if (!sta->sta.support_p2p_ps)
683 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
684 }
685
686 /* notify driver */
687 err = sta_info_insert_drv_state(local, sdata, sta);
688 if (err)
689 goto out_remove;
690
691 set_sta_flag(sta, WLAN_STA_INSERTED);
692
693 /* accept BA sessions now */
694 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
695
696 ieee80211_sta_debugfs_add(sta);
697 rate_control_add_sta_debugfs(sta);
698
699 sinfo->generation = local->sta_generation;
700 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
701 kfree(sinfo);
702
703 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
704
705 /* move reference to rcu-protected */
706 rcu_read_lock();
707 mutex_unlock(&local->sta_mtx);
708
709 if (ieee80211_vif_is_mesh(&sdata->vif))
710 mesh_accept_plinks_update(sdata);
711
712 return 0;
713 out_remove:
714 sta_info_hash_del(local, sta);
715 list_del_rcu(&sta->list);
716 out_drop_sta:
717 local->num_sta--;
718 synchronize_net();
719 out_cleanup:
720 cleanup_single_sta(sta);
721 mutex_unlock(&local->sta_mtx);
722 kfree(sinfo);
723 rcu_read_lock();
724 return err;
725}
726
727int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
728{
729 struct ieee80211_local *local = sta->local;
730 int err;
731
732 might_sleep();
733
734 mutex_lock(&local->sta_mtx);
735
736 err = sta_info_insert_check(sta);
737 if (err) {
738 sta_info_free(local, sta);
739 mutex_unlock(&local->sta_mtx);
740 rcu_read_lock();
741 return err;
742 }
743
744 return sta_info_insert_finish(sta);
745}
746
747int sta_info_insert(struct sta_info *sta)
748{
749 int err = sta_info_insert_rcu(sta);
750
751 rcu_read_unlock();
752
753 return err;
754}
755
756static inline void __bss_tim_set(u8 *tim, u16 id)
757{
758 /*
759 * This format has been mandated by the IEEE specifications,
760 * so this line may not be changed to use the __set_bit() format.
761 */
762 tim[id / 8] |= (1 << (id % 8));
763}
764
765static inline void __bss_tim_clear(u8 *tim, u16 id)
766{
767 /*
768 * This format has been mandated by the IEEE specifications,
769 * so this line may not be changed to use the __clear_bit() format.
770 */
771 tim[id / 8] &= ~(1 << (id % 8));
772}
773
774static inline bool __bss_tim_get(u8 *tim, u16 id)
775{
776 /*
777 * This format has been mandated by the IEEE specifications,
778 * so this line may not be changed to use the test_bit() format.
779 */
780 return tim[id / 8] & (1 << (id % 8));
781}
782
783static unsigned long ieee80211_tids_for_ac(int ac)
784{
785 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
786 switch (ac) {
787 case IEEE80211_AC_VO:
788 return BIT(6) | BIT(7);
789 case IEEE80211_AC_VI:
790 return BIT(4) | BIT(5);
791 case IEEE80211_AC_BE:
792 return BIT(0) | BIT(3);
793 case IEEE80211_AC_BK:
794 return BIT(1) | BIT(2);
795 default:
796 WARN_ON(1);
797 return 0;
798 }
799}
800
801static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
802{
803 struct ieee80211_local *local = sta->local;
804 struct ps_data *ps;
805 bool indicate_tim = false;
806 u8 ignore_for_tim = sta->sta.uapsd_queues;
807 int ac;
808 u16 id = sta->sta.aid;
809
810 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
811 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
812 if (WARN_ON_ONCE(!sta->sdata->bss))
813 return;
814
815 ps = &sta->sdata->bss->ps;
816#ifdef CONFIG_MAC80211_MESH
817 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
818 ps = &sta->sdata->u.mesh.ps;
819#endif
820 } else {
821 return;
822 }
823
824 /* No need to do anything if the driver does all */
825 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
826 return;
827
828 if (sta->dead)
829 goto done;
830
831 /*
832 * If all ACs are delivery-enabled then we should build
833 * the TIM bit for all ACs anyway; if only some are then
834 * we ignore those and build the TIM bit using only the
835 * non-enabled ones.
836 */
837 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
838 ignore_for_tim = 0;
839
840 if (ignore_pending)
841 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
842
843 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
844 unsigned long tids;
845
846 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
847 continue;
848
849 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
850 !skb_queue_empty(&sta->ps_tx_buf[ac]);
851 if (indicate_tim)
852 break;
853
854 tids = ieee80211_tids_for_ac(ac);
855
856 indicate_tim |=
857 sta->driver_buffered_tids & tids;
858 indicate_tim |=
859 sta->txq_buffered_tids & tids;
860 }
861
862 done:
863 spin_lock_bh(&local->tim_lock);
864
865 if (indicate_tim == __bss_tim_get(ps->tim, id))
866 goto out_unlock;
867
868 if (indicate_tim)
869 __bss_tim_set(ps->tim, id);
870 else
871 __bss_tim_clear(ps->tim, id);
872
873 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
874 local->tim_in_locked_section = true;
875 drv_set_tim(local, &sta->sta, indicate_tim);
876 local->tim_in_locked_section = false;
877 }
878
879out_unlock:
880 spin_unlock_bh(&local->tim_lock);
881}
882
883void sta_info_recalc_tim(struct sta_info *sta)
884{
885 __sta_info_recalc_tim(sta, false);
886}
887
888static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
889{
890 struct ieee80211_tx_info *info;
891 int timeout;
892
893 if (!skb)
894 return false;
895
896 info = IEEE80211_SKB_CB(skb);
897
898 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
899 timeout = (sta->listen_interval *
900 sta->sdata->vif.bss_conf.beacon_int *
901 32 / 15625) * HZ;
902 if (timeout < STA_TX_BUFFER_EXPIRE)
903 timeout = STA_TX_BUFFER_EXPIRE;
904 return time_after(jiffies, info->control.jiffies + timeout);
905}
906
907
908static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
909 struct sta_info *sta, int ac)
910{
911 unsigned long flags;
912 struct sk_buff *skb;
913
914 /*
915 * First check for frames that should expire on the filtered
916 * queue. Frames here were rejected by the driver and are on
917 * a separate queue to avoid reordering with normal PS-buffered
918 * frames. They also aren't accounted for right now in the
919 * total_ps_buffered counter.
920 */
921 for (;;) {
922 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
923 skb = skb_peek(&sta->tx_filtered[ac]);
924 if (sta_info_buffer_expired(sta, skb))
925 skb = __skb_dequeue(&sta->tx_filtered[ac]);
926 else
927 skb = NULL;
928 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
929
930 /*
931 * Frames are queued in order, so if this one
932 * hasn't expired yet we can stop testing. If
933 * we actually reached the end of the queue we
934 * also need to stop, of course.
935 */
936 if (!skb)
937 break;
938 ieee80211_free_txskb(&local->hw, skb);
939 }
940
941 /*
942 * Now also check the normal PS-buffered queue, this will
943 * only find something if the filtered queue was emptied
944 * since the filtered frames are all before the normal PS
945 * buffered frames.
946 */
947 for (;;) {
948 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
949 skb = skb_peek(&sta->ps_tx_buf[ac]);
950 if (sta_info_buffer_expired(sta, skb))
951 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
952 else
953 skb = NULL;
954 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
955
956 /*
957 * frames are queued in order, so if this one
958 * hasn't expired yet (or we reached the end of
959 * the queue) we can stop testing
960 */
961 if (!skb)
962 break;
963
964 local->total_ps_buffered--;
965 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
966 sta->sta.addr);
967 ieee80211_free_txskb(&local->hw, skb);
968 }
969
970 /*
971 * Finally, recalculate the TIM bit for this station -- it might
972 * now be clear because the station was too slow to retrieve its
973 * frames.
974 */
975 sta_info_recalc_tim(sta);
976
977 /*
978 * Return whether there are any frames still buffered, this is
979 * used to check whether the cleanup timer still needs to run,
980 * if there are no frames we don't need to rearm the timer.
981 */
982 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
983 skb_queue_empty(&sta->tx_filtered[ac]));
984}
985
986static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
987 struct sta_info *sta)
988{
989 bool have_buffered = false;
990 int ac;
991
992 /* This is only necessary for stations on BSS/MBSS interfaces */
993 if (!sta->sdata->bss &&
994 !ieee80211_vif_is_mesh(&sta->sdata->vif))
995 return false;
996
997 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
998 have_buffered |=
999 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1000
1001 return have_buffered;
1002}
1003
1004static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1005{
1006 struct ieee80211_local *local;
1007 struct ieee80211_sub_if_data *sdata;
1008 int ret;
1009
1010 might_sleep();
1011
1012 if (!sta)
1013 return -ENOENT;
1014
1015 local = sta->local;
1016 sdata = sta->sdata;
1017
1018 lockdep_assert_held(&local->sta_mtx);
1019
1020 /*
1021 * Before removing the station from the driver and
1022 * rate control, it might still start new aggregation
1023 * sessions -- block that to make sure the tear-down
1024 * will be sufficient.
1025 */
1026 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1027 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1028
1029 /*
1030 * Before removing the station from the driver there might be pending
1031 * rx frames on RSS queues sent prior to the disassociation - wait for
1032 * all such frames to be processed.
1033 */
1034 drv_sync_rx_queues(local, sta);
1035
1036 ret = sta_info_hash_del(local, sta);
1037 if (WARN_ON(ret))
1038 return ret;
1039
1040 /*
1041 * for TDLS peers, make sure to return to the base channel before
1042 * removal.
1043 */
1044 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1045 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1046 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1047 }
1048
1049 list_del_rcu(&sta->list);
1050 sta->removed = true;
1051
1052 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1053
1054 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1055 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1056 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1057
1058 return 0;
1059}
1060
1061static void __sta_info_destroy_part2(struct sta_info *sta)
1062{
1063 struct ieee80211_local *local = sta->local;
1064 struct ieee80211_sub_if_data *sdata = sta->sdata;
1065 struct station_info *sinfo;
1066 int ret;
1067
1068 /*
1069 * NOTE: This assumes at least synchronize_net() was done
1070 * after _part1 and before _part2!
1071 */
1072
1073 might_sleep();
1074 lockdep_assert_held(&local->sta_mtx);
1075
1076 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1077 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1078 WARN_ON_ONCE(ret);
1079 }
1080
1081 /* now keys can no longer be reached */
1082 ieee80211_free_sta_keys(local, sta);
1083
1084 /* disable TIM bit - last chance to tell driver */
1085 __sta_info_recalc_tim(sta, true);
1086
1087 sta->dead = true;
1088
1089 local->num_sta--;
1090 local->sta_generation++;
1091
1092 while (sta->sta_state > IEEE80211_STA_NONE) {
1093 ret = sta_info_move_state(sta, sta->sta_state - 1);
1094 if (ret) {
1095 WARN_ON_ONCE(1);
1096 break;
1097 }
1098 }
1099
1100 if (sta->uploaded) {
1101 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1102 IEEE80211_STA_NOTEXIST);
1103 WARN_ON_ONCE(ret != 0);
1104 }
1105
1106 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1107
1108 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1109 if (sinfo)
1110 sta_set_sinfo(sta, sinfo, true);
1111 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1112 kfree(sinfo);
1113
1114 ieee80211_sta_debugfs_remove(sta);
1115
1116 ieee80211_destroy_frag_cache(&sta->frags);
1117
1118 cleanup_single_sta(sta);
1119}
1120
1121int __must_check __sta_info_destroy(struct sta_info *sta)
1122{
1123 int err = __sta_info_destroy_part1(sta);
1124
1125 if (err)
1126 return err;
1127
1128 synchronize_net();
1129
1130 __sta_info_destroy_part2(sta);
1131
1132 return 0;
1133}
1134
1135int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1136{
1137 struct sta_info *sta;
1138 int ret;
1139
1140 mutex_lock(&sdata->local->sta_mtx);
1141 sta = sta_info_get(sdata, addr);
1142 ret = __sta_info_destroy(sta);
1143 mutex_unlock(&sdata->local->sta_mtx);
1144
1145 return ret;
1146}
1147
1148int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1149 const u8 *addr)
1150{
1151 struct sta_info *sta;
1152 int ret;
1153
1154 mutex_lock(&sdata->local->sta_mtx);
1155 sta = sta_info_get_bss(sdata, addr);
1156 ret = __sta_info_destroy(sta);
1157 mutex_unlock(&sdata->local->sta_mtx);
1158
1159 return ret;
1160}
1161
1162static void sta_info_cleanup(struct timer_list *t)
1163{
1164 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1165 struct sta_info *sta;
1166 bool timer_needed = false;
1167
1168 rcu_read_lock();
1169 list_for_each_entry_rcu(sta, &local->sta_list, list)
1170 if (sta_info_cleanup_expire_buffered(local, sta))
1171 timer_needed = true;
1172 rcu_read_unlock();
1173
1174 if (local->quiescing)
1175 return;
1176
1177 if (!timer_needed)
1178 return;
1179
1180 mod_timer(&local->sta_cleanup,
1181 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1182}
1183
1184int sta_info_init(struct ieee80211_local *local)
1185{
1186 int err;
1187
1188 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1189 if (err)
1190 return err;
1191
1192 spin_lock_init(&local->tim_lock);
1193 mutex_init(&local->sta_mtx);
1194 INIT_LIST_HEAD(&local->sta_list);
1195
1196 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1197 return 0;
1198}
1199
1200void sta_info_stop(struct ieee80211_local *local)
1201{
1202 del_timer_sync(&local->sta_cleanup);
1203 rhltable_destroy(&local->sta_hash);
1204}
1205
1206
1207int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1208{
1209 struct ieee80211_local *local = sdata->local;
1210 struct sta_info *sta, *tmp;
1211 LIST_HEAD(free_list);
1212 int ret = 0;
1213
1214 might_sleep();
1215
1216 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1217 WARN_ON(vlans && !sdata->bss);
1218
1219 mutex_lock(&local->sta_mtx);
1220 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1221 if (sdata == sta->sdata ||
1222 (vlans && sdata->bss == sta->sdata->bss)) {
1223 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1224 list_add(&sta->free_list, &free_list);
1225 ret++;
1226 }
1227 }
1228
1229 if (!list_empty(&free_list)) {
1230 synchronize_net();
1231 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1232 __sta_info_destroy_part2(sta);
1233 }
1234 mutex_unlock(&local->sta_mtx);
1235
1236 return ret;
1237}
1238
1239void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1240 unsigned long exp_time)
1241{
1242 struct ieee80211_local *local = sdata->local;
1243 struct sta_info *sta, *tmp;
1244
1245 mutex_lock(&local->sta_mtx);
1246
1247 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1248 unsigned long last_active = ieee80211_sta_last_active(sta);
1249
1250 if (sdata != sta->sdata)
1251 continue;
1252
1253 if (time_is_before_jiffies(last_active + exp_time)) {
1254 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1255 sta->sta.addr);
1256
1257 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1258 test_sta_flag(sta, WLAN_STA_PS_STA))
1259 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1260
1261 WARN_ON(__sta_info_destroy(sta));
1262 }
1263 }
1264
1265 mutex_unlock(&local->sta_mtx);
1266}
1267
1268struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1269 const u8 *addr,
1270 const u8 *localaddr)
1271{
1272 struct ieee80211_local *local = hw_to_local(hw);
1273 struct rhlist_head *tmp;
1274 struct sta_info *sta;
1275
1276 /*
1277 * Just return a random station if localaddr is NULL
1278 * ... first in list.
1279 */
1280 for_each_sta_info(local, addr, sta, tmp) {
1281 if (localaddr &&
1282 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1283 continue;
1284 if (!sta->uploaded)
1285 return NULL;
1286 return &sta->sta;
1287 }
1288
1289 return NULL;
1290}
1291EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1292
1293struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1294 const u8 *addr)
1295{
1296 struct sta_info *sta;
1297
1298 if (!vif)
1299 return NULL;
1300
1301 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1302 if (!sta)
1303 return NULL;
1304
1305 if (!sta->uploaded)
1306 return NULL;
1307
1308 return &sta->sta;
1309}
1310EXPORT_SYMBOL(ieee80211_find_sta);
1311
1312/* powersave support code */
1313void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1314{
1315 struct ieee80211_sub_if_data *sdata = sta->sdata;
1316 struct ieee80211_local *local = sdata->local;
1317 struct sk_buff_head pending;
1318 int filtered = 0, buffered = 0, ac, i;
1319 unsigned long flags;
1320 struct ps_data *ps;
1321
1322 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1323 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1324 u.ap);
1325
1326 if (sdata->vif.type == NL80211_IFTYPE_AP)
1327 ps = &sdata->bss->ps;
1328 else if (ieee80211_vif_is_mesh(&sdata->vif))
1329 ps = &sdata->u.mesh.ps;
1330 else
1331 return;
1332
1333 clear_sta_flag(sta, WLAN_STA_SP);
1334
1335 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1336 sta->driver_buffered_tids = 0;
1337 sta->txq_buffered_tids = 0;
1338
1339 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1340 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1341
1342 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1343 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1344 continue;
1345
1346 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1347 }
1348
1349 skb_queue_head_init(&pending);
1350
1351 /* sync with ieee80211_tx_h_unicast_ps_buf */
1352 spin_lock(&sta->ps_lock);
1353 /* Send all buffered frames to the station */
1354 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1355 int count = skb_queue_len(&pending), tmp;
1356
1357 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1358 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1359 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1360 tmp = skb_queue_len(&pending);
1361 filtered += tmp - count;
1362 count = tmp;
1363
1364 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1365 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1366 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1367 tmp = skb_queue_len(&pending);
1368 buffered += tmp - count;
1369 }
1370
1371 ieee80211_add_pending_skbs(local, &pending);
1372
1373 /* now we're no longer in the deliver code */
1374 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1375
1376 /* The station might have polled and then woken up before we responded,
1377 * so clear these flags now to avoid them sticking around.
1378 */
1379 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1380 clear_sta_flag(sta, WLAN_STA_UAPSD);
1381 spin_unlock(&sta->ps_lock);
1382
1383 atomic_dec(&ps->num_sta_ps);
1384
1385 local->total_ps_buffered -= buffered;
1386
1387 sta_info_recalc_tim(sta);
1388
1389 ps_dbg(sdata,
1390 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1391 sta->sta.addr, sta->sta.aid, filtered, buffered);
1392
1393 ieee80211_check_fast_xmit(sta);
1394}
1395
1396static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1397 enum ieee80211_frame_release_type reason,
1398 bool call_driver, bool more_data)
1399{
1400 struct ieee80211_sub_if_data *sdata = sta->sdata;
1401 struct ieee80211_local *local = sdata->local;
1402 struct ieee80211_qos_hdr *nullfunc;
1403 struct sk_buff *skb;
1404 int size = sizeof(*nullfunc);
1405 __le16 fc;
1406 bool qos = sta->sta.wme;
1407 struct ieee80211_tx_info *info;
1408 struct ieee80211_chanctx_conf *chanctx_conf;
1409
1410 if (qos) {
1411 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1412 IEEE80211_STYPE_QOS_NULLFUNC |
1413 IEEE80211_FCTL_FROMDS);
1414 } else {
1415 size -= 2;
1416 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1417 IEEE80211_STYPE_NULLFUNC |
1418 IEEE80211_FCTL_FROMDS);
1419 }
1420
1421 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1422 if (!skb)
1423 return;
1424
1425 skb_reserve(skb, local->hw.extra_tx_headroom);
1426
1427 nullfunc = skb_put(skb, size);
1428 nullfunc->frame_control = fc;
1429 nullfunc->duration_id = 0;
1430 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1431 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1432 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1433 nullfunc->seq_ctrl = 0;
1434
1435 skb->priority = tid;
1436 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1437 if (qos) {
1438 nullfunc->qos_ctrl = cpu_to_le16(tid);
1439
1440 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1441 nullfunc->qos_ctrl |=
1442 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1443 if (more_data)
1444 nullfunc->frame_control |=
1445 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1446 }
1447 }
1448
1449 info = IEEE80211_SKB_CB(skb);
1450
1451 /*
1452 * Tell TX path to send this frame even though the
1453 * STA may still remain is PS mode after this frame
1454 * exchange. Also set EOSP to indicate this packet
1455 * ends the poll/service period.
1456 */
1457 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1458 IEEE80211_TX_STATUS_EOSP |
1459 IEEE80211_TX_CTL_REQ_TX_STATUS;
1460
1461 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1462
1463 if (call_driver)
1464 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1465 reason, false);
1466
1467 skb->dev = sdata->dev;
1468
1469 rcu_read_lock();
1470 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1471 if (WARN_ON(!chanctx_conf)) {
1472 rcu_read_unlock();
1473 kfree_skb(skb);
1474 return;
1475 }
1476
1477 info->band = chanctx_conf->def.chan->band;
1478 ieee80211_xmit(sdata, sta, skb);
1479 rcu_read_unlock();
1480}
1481
1482static int find_highest_prio_tid(unsigned long tids)
1483{
1484 /* lower 3 TIDs aren't ordered perfectly */
1485 if (tids & 0xF8)
1486 return fls(tids) - 1;
1487 /* TID 0 is BE just like TID 3 */
1488 if (tids & BIT(0))
1489 return 0;
1490 return fls(tids) - 1;
1491}
1492
1493/* Indicates if the MORE_DATA bit should be set in the last
1494 * frame obtained by ieee80211_sta_ps_get_frames.
1495 * Note that driver_release_tids is relevant only if
1496 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1497 */
1498static bool
1499ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1500 enum ieee80211_frame_release_type reason,
1501 unsigned long driver_release_tids)
1502{
1503 int ac;
1504
1505 /* If the driver has data on more than one TID then
1506 * certainly there's more data if we release just a
1507 * single frame now (from a single TID). This will
1508 * only happen for PS-Poll.
1509 */
1510 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1511 hweight16(driver_release_tids) > 1)
1512 return true;
1513
1514 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1515 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1516 continue;
1517
1518 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1519 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1520 return true;
1521 }
1522
1523 return false;
1524}
1525
1526static void
1527ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1528 enum ieee80211_frame_release_type reason,
1529 struct sk_buff_head *frames,
1530 unsigned long *driver_release_tids)
1531{
1532 struct ieee80211_sub_if_data *sdata = sta->sdata;
1533 struct ieee80211_local *local = sdata->local;
1534 int ac;
1535
1536 /* Get response frame(s) and more data bit for the last one. */
1537 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1538 unsigned long tids;
1539
1540 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1541 continue;
1542
1543 tids = ieee80211_tids_for_ac(ac);
1544
1545 /* if we already have frames from software, then we can't also
1546 * release from hardware queues
1547 */
1548 if (skb_queue_empty(frames)) {
1549 *driver_release_tids |=
1550 sta->driver_buffered_tids & tids;
1551 *driver_release_tids |= sta->txq_buffered_tids & tids;
1552 }
1553
1554 if (!*driver_release_tids) {
1555 struct sk_buff *skb;
1556
1557 while (n_frames > 0) {
1558 skb = skb_dequeue(&sta->tx_filtered[ac]);
1559 if (!skb) {
1560 skb = skb_dequeue(
1561 &sta->ps_tx_buf[ac]);
1562 if (skb)
1563 local->total_ps_buffered--;
1564 }
1565 if (!skb)
1566 break;
1567 n_frames--;
1568 __skb_queue_tail(frames, skb);
1569 }
1570 }
1571
1572 /* If we have more frames buffered on this AC, then abort the
1573 * loop since we can't send more data from other ACs before
1574 * the buffered frames from this.
1575 */
1576 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1577 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1578 break;
1579 }
1580}
1581
1582static void
1583ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1584 int n_frames, u8 ignored_acs,
1585 enum ieee80211_frame_release_type reason)
1586{
1587 struct ieee80211_sub_if_data *sdata = sta->sdata;
1588 struct ieee80211_local *local = sdata->local;
1589 unsigned long driver_release_tids = 0;
1590 struct sk_buff_head frames;
1591 bool more_data;
1592
1593 /* Service or PS-Poll period starts */
1594 set_sta_flag(sta, WLAN_STA_SP);
1595
1596 __skb_queue_head_init(&frames);
1597
1598 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1599 &frames, &driver_release_tids);
1600
1601 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1602
1603 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1604 driver_release_tids =
1605 BIT(find_highest_prio_tid(driver_release_tids));
1606
1607 if (skb_queue_empty(&frames) && !driver_release_tids) {
1608 int tid, ac;
1609
1610 /*
1611 * For PS-Poll, this can only happen due to a race condition
1612 * when we set the TIM bit and the station notices it, but
1613 * before it can poll for the frame we expire it.
1614 *
1615 * For uAPSD, this is said in the standard (11.2.1.5 h):
1616 * At each unscheduled SP for a non-AP STA, the AP shall
1617 * attempt to transmit at least one MSDU or MMPDU, but no
1618 * more than the value specified in the Max SP Length field
1619 * in the QoS Capability element from delivery-enabled ACs,
1620 * that are destined for the non-AP STA.
1621 *
1622 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1623 */
1624
1625 /* This will evaluate to 1, 3, 5 or 7. */
1626 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1627 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1628 break;
1629 tid = 7 - 2 * ac;
1630
1631 ieee80211_send_null_response(sta, tid, reason, true, false);
1632 } else if (!driver_release_tids) {
1633 struct sk_buff_head pending;
1634 struct sk_buff *skb;
1635 int num = 0;
1636 u16 tids = 0;
1637 bool need_null = false;
1638
1639 skb_queue_head_init(&pending);
1640
1641 while ((skb = __skb_dequeue(&frames))) {
1642 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1643 struct ieee80211_hdr *hdr = (void *) skb->data;
1644 u8 *qoshdr = NULL;
1645
1646 num++;
1647
1648 /*
1649 * Tell TX path to send this frame even though the
1650 * STA may still remain is PS mode after this frame
1651 * exchange.
1652 */
1653 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1654 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1655
1656 /*
1657 * Use MoreData flag to indicate whether there are
1658 * more buffered frames for this STA
1659 */
1660 if (more_data || !skb_queue_empty(&frames))
1661 hdr->frame_control |=
1662 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1663 else
1664 hdr->frame_control &=
1665 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1666
1667 if (ieee80211_is_data_qos(hdr->frame_control) ||
1668 ieee80211_is_qos_nullfunc(hdr->frame_control))
1669 qoshdr = ieee80211_get_qos_ctl(hdr);
1670
1671 tids |= BIT(skb->priority);
1672
1673 __skb_queue_tail(&pending, skb);
1674
1675 /* end service period after last frame or add one */
1676 if (!skb_queue_empty(&frames))
1677 continue;
1678
1679 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1680 /* for PS-Poll, there's only one frame */
1681 info->flags |= IEEE80211_TX_STATUS_EOSP |
1682 IEEE80211_TX_CTL_REQ_TX_STATUS;
1683 break;
1684 }
1685
1686 /* For uAPSD, things are a bit more complicated. If the
1687 * last frame has a QoS header (i.e. is a QoS-data or
1688 * QoS-nulldata frame) then just set the EOSP bit there
1689 * and be done.
1690 * If the frame doesn't have a QoS header (which means
1691 * it should be a bufferable MMPDU) then we can't set
1692 * the EOSP bit in the QoS header; add a QoS-nulldata
1693 * frame to the list to send it after the MMPDU.
1694 *
1695 * Note that this code is only in the mac80211-release
1696 * code path, we assume that the driver will not buffer
1697 * anything but QoS-data frames, or if it does, will
1698 * create the QoS-nulldata frame by itself if needed.
1699 *
1700 * Cf. 802.11-2012 10.2.1.10 (c).
1701 */
1702 if (qoshdr) {
1703 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1704
1705 info->flags |= IEEE80211_TX_STATUS_EOSP |
1706 IEEE80211_TX_CTL_REQ_TX_STATUS;
1707 } else {
1708 /* The standard isn't completely clear on this
1709 * as it says the more-data bit should be set
1710 * if there are more BUs. The QoS-Null frame
1711 * we're about to send isn't buffered yet, we
1712 * only create it below, but let's pretend it
1713 * was buffered just in case some clients only
1714 * expect more-data=0 when eosp=1.
1715 */
1716 hdr->frame_control |=
1717 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1718 need_null = true;
1719 num++;
1720 }
1721 break;
1722 }
1723
1724 drv_allow_buffered_frames(local, sta, tids, num,
1725 reason, more_data);
1726
1727 ieee80211_add_pending_skbs(local, &pending);
1728
1729 if (need_null)
1730 ieee80211_send_null_response(
1731 sta, find_highest_prio_tid(tids),
1732 reason, false, false);
1733
1734 sta_info_recalc_tim(sta);
1735 } else {
1736 int tid;
1737
1738 /*
1739 * We need to release a frame that is buffered somewhere in the
1740 * driver ... it'll have to handle that.
1741 * Note that the driver also has to check the number of frames
1742 * on the TIDs we're releasing from - if there are more than
1743 * n_frames it has to set the more-data bit (if we didn't ask
1744 * it to set it anyway due to other buffered frames); if there
1745 * are fewer than n_frames it has to make sure to adjust that
1746 * to allow the service period to end properly.
1747 */
1748 drv_release_buffered_frames(local, sta, driver_release_tids,
1749 n_frames, reason, more_data);
1750
1751 /*
1752 * Note that we don't recalculate the TIM bit here as it would
1753 * most likely have no effect at all unless the driver told us
1754 * that the TID(s) became empty before returning here from the
1755 * release function.
1756 * Either way, however, when the driver tells us that the TID(s)
1757 * became empty or we find that a txq became empty, we'll do the
1758 * TIM recalculation.
1759 */
1760
1761 if (!sta->sta.txq[0])
1762 return;
1763
1764 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1765 if (!sta->sta.txq[tid] ||
1766 !(driver_release_tids & BIT(tid)) ||
1767 txq_has_queue(sta->sta.txq[tid]))
1768 continue;
1769
1770 sta_info_recalc_tim(sta);
1771 break;
1772 }
1773 }
1774}
1775
1776void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1777{
1778 u8 ignore_for_response = sta->sta.uapsd_queues;
1779
1780 /*
1781 * If all ACs are delivery-enabled then we should reply
1782 * from any of them, if only some are enabled we reply
1783 * only from the non-enabled ones.
1784 */
1785 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1786 ignore_for_response = 0;
1787
1788 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1789 IEEE80211_FRAME_RELEASE_PSPOLL);
1790}
1791
1792void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1793{
1794 int n_frames = sta->sta.max_sp;
1795 u8 delivery_enabled = sta->sta.uapsd_queues;
1796
1797 /*
1798 * If we ever grow support for TSPEC this might happen if
1799 * the TSPEC update from hostapd comes in between a trigger
1800 * frame setting WLAN_STA_UAPSD in the RX path and this
1801 * actually getting called.
1802 */
1803 if (!delivery_enabled)
1804 return;
1805
1806 switch (sta->sta.max_sp) {
1807 case 1:
1808 n_frames = 2;
1809 break;
1810 case 2:
1811 n_frames = 4;
1812 break;
1813 case 3:
1814 n_frames = 6;
1815 break;
1816 case 0:
1817 /* XXX: what is a good value? */
1818 n_frames = 128;
1819 break;
1820 }
1821
1822 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1823 IEEE80211_FRAME_RELEASE_UAPSD);
1824}
1825
1826void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1827 struct ieee80211_sta *pubsta, bool block)
1828{
1829 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1830
1831 trace_api_sta_block_awake(sta->local, pubsta, block);
1832
1833 if (block) {
1834 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1835 ieee80211_clear_fast_xmit(sta);
1836 return;
1837 }
1838
1839 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1840 return;
1841
1842 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1843 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1844 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1845 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1846 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1847 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1848 /* must be asleep in this case */
1849 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1850 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1851 } else {
1852 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1853 ieee80211_check_fast_xmit(sta);
1854 }
1855}
1856EXPORT_SYMBOL(ieee80211_sta_block_awake);
1857
1858void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1859{
1860 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1861 struct ieee80211_local *local = sta->local;
1862
1863 trace_api_eosp(local, pubsta);
1864
1865 clear_sta_flag(sta, WLAN_STA_SP);
1866}
1867EXPORT_SYMBOL(ieee80211_sta_eosp);
1868
1869void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1870{
1871 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1872 enum ieee80211_frame_release_type reason;
1873 bool more_data;
1874
1875 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1876
1877 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1878 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1879 reason, 0);
1880
1881 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1882}
1883EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1884
1885void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1886 u8 tid, bool buffered)
1887{
1888 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1889
1890 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1891 return;
1892
1893 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1894
1895 if (buffered)
1896 set_bit(tid, &sta->driver_buffered_tids);
1897 else
1898 clear_bit(tid, &sta->driver_buffered_tids);
1899
1900 sta_info_recalc_tim(sta);
1901}
1902EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1903
1904void ieee80211_register_airtime(struct ieee80211_txq *txq,
1905 u32 tx_airtime, u32 rx_airtime)
1906{
1907 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
1908 struct ieee80211_local *local = sdata->local;
1909 u64 weight_sum, weight_sum_reciprocal;
1910 struct airtime_sched_info *air_sched;
1911 struct airtime_info *air_info;
1912 u32 airtime = 0;
1913
1914 air_sched = &local->airtime[txq->ac];
1915 air_info = to_airtime_info(txq);
1916
1917 if (local->airtime_flags & AIRTIME_USE_TX)
1918 airtime += tx_airtime;
1919 if (local->airtime_flags & AIRTIME_USE_RX)
1920 airtime += rx_airtime;
1921
1922 /* Weights scale so the unit weight is 256 */
1923 airtime <<= 8;
1924
1925 spin_lock_bh(&air_sched->lock);
1926
1927 air_info->tx_airtime += tx_airtime;
1928 air_info->rx_airtime += rx_airtime;
1929
1930 if (air_sched->weight_sum) {
1931 weight_sum = air_sched->weight_sum;
1932 weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
1933 } else {
1934 weight_sum = air_info->weight;
1935 weight_sum_reciprocal = air_info->weight_reciprocal;
1936 }
1937
1938 /* Round the calculation of global vt */
1939 air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
1940 weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
1941 air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
1942 air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
1943 ieee80211_resort_txq(&local->hw, txq);
1944
1945 spin_unlock_bh(&air_sched->lock);
1946}
1947
1948void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1949 u32 tx_airtime, u32 rx_airtime)
1950{
1951 struct ieee80211_txq *txq = pubsta->txq[tid];
1952
1953 if (!txq)
1954 return;
1955
1956 ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
1957}
1958EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1959
1960void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
1961 struct sta_info *sta, u8 ac,
1962 u16 tx_airtime, bool tx_completed)
1963{
1964 int tx_pending;
1965
1966 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
1967 return;
1968
1969 if (!tx_completed) {
1970 if (sta)
1971 atomic_add(tx_airtime,
1972 &sta->airtime[ac].aql_tx_pending);
1973
1974 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
1975 return;
1976 }
1977
1978 if (sta) {
1979 tx_pending = atomic_sub_return(tx_airtime,
1980 &sta->airtime[ac].aql_tx_pending);
1981 if (tx_pending < 0)
1982 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
1983 tx_pending, 0);
1984 }
1985
1986 tx_pending = atomic_sub_return(tx_airtime,
1987 &local->aql_total_pending_airtime);
1988 if (WARN_ONCE(tx_pending < 0,
1989 "Device %s AC %d pending airtime underflow: %u, %u",
1990 wiphy_name(local->hw.wiphy), ac, tx_pending,
1991 tx_airtime))
1992 atomic_cmpxchg(&local->aql_total_pending_airtime,
1993 tx_pending, 0);
1994}
1995
1996int sta_info_move_state(struct sta_info *sta,
1997 enum ieee80211_sta_state new_state)
1998{
1999 might_sleep();
2000
2001 if (sta->sta_state == new_state)
2002 return 0;
2003
2004 /* check allowed transitions first */
2005
2006 switch (new_state) {
2007 case IEEE80211_STA_NONE:
2008 if (sta->sta_state != IEEE80211_STA_AUTH)
2009 return -EINVAL;
2010 break;
2011 case IEEE80211_STA_AUTH:
2012 if (sta->sta_state != IEEE80211_STA_NONE &&
2013 sta->sta_state != IEEE80211_STA_ASSOC)
2014 return -EINVAL;
2015 break;
2016 case IEEE80211_STA_ASSOC:
2017 if (sta->sta_state != IEEE80211_STA_AUTH &&
2018 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2019 return -EINVAL;
2020 break;
2021 case IEEE80211_STA_AUTHORIZED:
2022 if (sta->sta_state != IEEE80211_STA_ASSOC)
2023 return -EINVAL;
2024 break;
2025 default:
2026 WARN(1, "invalid state %d", new_state);
2027 return -EINVAL;
2028 }
2029
2030 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2031 sta->sta.addr, new_state);
2032
2033 /*
2034 * notify the driver before the actual changes so it can
2035 * fail the transition
2036 */
2037 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2038 int err = drv_sta_state(sta->local, sta->sdata, sta,
2039 sta->sta_state, new_state);
2040 if (err)
2041 return err;
2042 }
2043
2044 /* reflect the change in all state variables */
2045
2046 switch (new_state) {
2047 case IEEE80211_STA_NONE:
2048 if (sta->sta_state == IEEE80211_STA_AUTH)
2049 clear_bit(WLAN_STA_AUTH, &sta->_flags);
2050 break;
2051 case IEEE80211_STA_AUTH:
2052 if (sta->sta_state == IEEE80211_STA_NONE) {
2053 set_bit(WLAN_STA_AUTH, &sta->_flags);
2054 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2055 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2056 ieee80211_recalc_min_chandef(sta->sdata);
2057 if (!sta->sta.support_p2p_ps)
2058 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2059 }
2060 break;
2061 case IEEE80211_STA_ASSOC:
2062 if (sta->sta_state == IEEE80211_STA_AUTH) {
2063 set_bit(WLAN_STA_ASSOC, &sta->_flags);
2064 sta->assoc_at = ktime_get_boottime_ns();
2065 ieee80211_recalc_min_chandef(sta->sdata);
2066 if (!sta->sta.support_p2p_ps)
2067 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2068 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2069 ieee80211_vif_dec_num_mcast(sta->sdata);
2070 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2071 ieee80211_clear_fast_xmit(sta);
2072 ieee80211_clear_fast_rx(sta);
2073 }
2074 break;
2075 case IEEE80211_STA_AUTHORIZED:
2076 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2077 ieee80211_vif_inc_num_mcast(sta->sdata);
2078 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2079 ieee80211_check_fast_xmit(sta);
2080 ieee80211_check_fast_rx(sta);
2081 }
2082 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2083 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2084 cfg80211_send_layer2_update(sta->sdata->dev,
2085 sta->sta.addr);
2086 break;
2087 default:
2088 break;
2089 }
2090
2091 sta->sta_state = new_state;
2092
2093 return 0;
2094}
2095
2096u8 sta_info_tx_streams(struct sta_info *sta)
2097{
2098 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.deflink.ht_cap;
2099 u8 rx_streams;
2100
2101 if (!sta->sta.deflink.ht_cap.ht_supported)
2102 return 1;
2103
2104 if (sta->sta.deflink.vht_cap.vht_supported) {
2105 int i;
2106 u16 tx_mcs_map =
2107 le16_to_cpu(sta->sta.deflink.vht_cap.vht_mcs.tx_mcs_map);
2108
2109 for (i = 7; i >= 0; i--)
2110 if ((tx_mcs_map & (0x3 << (i * 2))) !=
2111 IEEE80211_VHT_MCS_NOT_SUPPORTED)
2112 return i + 1;
2113 }
2114
2115 if (ht_cap->mcs.rx_mask[3])
2116 rx_streams = 4;
2117 else if (ht_cap->mcs.rx_mask[2])
2118 rx_streams = 3;
2119 else if (ht_cap->mcs.rx_mask[1])
2120 rx_streams = 2;
2121 else
2122 rx_streams = 1;
2123
2124 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2125 return rx_streams;
2126
2127 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2128 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2129}
2130
2131static struct ieee80211_sta_rx_stats *
2132sta_get_last_rx_stats(struct sta_info *sta)
2133{
2134 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2135 int cpu;
2136
2137 if (!sta->deflink.pcpu_rx_stats)
2138 return stats;
2139
2140 for_each_possible_cpu(cpu) {
2141 struct ieee80211_sta_rx_stats *cpustats;
2142
2143 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2144
2145 if (time_after(cpustats->last_rx, stats->last_rx))
2146 stats = cpustats;
2147 }
2148
2149 return stats;
2150}
2151
2152static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2153 struct rate_info *rinfo)
2154{
2155 rinfo->bw = STA_STATS_GET(BW, rate);
2156
2157 switch (STA_STATS_GET(TYPE, rate)) {
2158 case STA_STATS_RATE_TYPE_VHT:
2159 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2160 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2161 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2162 if (STA_STATS_GET(SGI, rate))
2163 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2164 break;
2165 case STA_STATS_RATE_TYPE_HT:
2166 rinfo->flags = RATE_INFO_FLAGS_MCS;
2167 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2168 if (STA_STATS_GET(SGI, rate))
2169 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2170 break;
2171 case STA_STATS_RATE_TYPE_LEGACY: {
2172 struct ieee80211_supported_band *sband;
2173 u16 brate;
2174 unsigned int shift;
2175 int band = STA_STATS_GET(LEGACY_BAND, rate);
2176 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2177
2178 sband = local->hw.wiphy->bands[band];
2179
2180 if (WARN_ON_ONCE(!sband->bitrates))
2181 break;
2182
2183 brate = sband->bitrates[rate_idx].bitrate;
2184 if (rinfo->bw == RATE_INFO_BW_5)
2185 shift = 2;
2186 else if (rinfo->bw == RATE_INFO_BW_10)
2187 shift = 1;
2188 else
2189 shift = 0;
2190 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2191 break;
2192 }
2193 case STA_STATS_RATE_TYPE_HE:
2194 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2195 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2196 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2197 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2198 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2199 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2200 break;
2201 }
2202}
2203
2204static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2205{
2206 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2207
2208 if (rate == STA_STATS_RATE_INVALID)
2209 return -EINVAL;
2210
2211 sta_stats_decode_rate(sta->local, rate, rinfo);
2212 return 0;
2213}
2214
2215static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2216 int tid)
2217{
2218 unsigned int start;
2219 u64 value;
2220
2221 do {
2222 start = u64_stats_fetch_begin(&rxstats->syncp);
2223 value = rxstats->msdu[tid];
2224 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2225
2226 return value;
2227}
2228
2229static void sta_set_tidstats(struct sta_info *sta,
2230 struct cfg80211_tid_stats *tidstats,
2231 int tid)
2232{
2233 struct ieee80211_local *local = sta->local;
2234 int cpu;
2235
2236 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2237 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2238 tid);
2239
2240 if (sta->deflink.pcpu_rx_stats) {
2241 for_each_possible_cpu(cpu) {
2242 struct ieee80211_sta_rx_stats *cpurxs;
2243
2244 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2245 cpu);
2246 tidstats->rx_msdu +=
2247 sta_get_tidstats_msdu(cpurxs, tid);
2248 }
2249 }
2250
2251 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2252 }
2253
2254 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2255 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2256 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2257 }
2258
2259 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2260 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2261 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2262 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2263 }
2264
2265 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2266 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2267 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2268 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2269 }
2270
2271 if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2272 spin_lock_bh(&local->fq.lock);
2273 rcu_read_lock();
2274
2275 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2276 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2277 to_txq_info(sta->sta.txq[tid]));
2278
2279 rcu_read_unlock();
2280 spin_unlock_bh(&local->fq.lock);
2281 }
2282}
2283
2284static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2285{
2286 unsigned int start;
2287 u64 value;
2288
2289 do {
2290 start = u64_stats_fetch_begin(&rxstats->syncp);
2291 value = rxstats->bytes;
2292 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2293
2294 return value;
2295}
2296
2297void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2298 bool tidstats)
2299{
2300 struct ieee80211_sub_if_data *sdata = sta->sdata;
2301 struct ieee80211_local *local = sdata->local;
2302 u32 thr = 0;
2303 int i, ac, cpu;
2304 struct ieee80211_sta_rx_stats *last_rxstats;
2305
2306 last_rxstats = sta_get_last_rx_stats(sta);
2307
2308 sinfo->generation = sdata->local->sta_generation;
2309
2310 /* do before driver, so beacon filtering drivers have a
2311 * chance to e.g. just add the number of filtered beacons
2312 * (or just modify the value entirely, of course)
2313 */
2314 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2315 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2316
2317 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2318 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2319 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2320 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2321 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2322 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2323 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2324
2325 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2326 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2327 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2328 }
2329
2330 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2331 sinfo->assoc_at = sta->assoc_at;
2332 sinfo->inactive_time =
2333 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2334
2335 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2336 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2337 sinfo->tx_bytes = 0;
2338 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2339 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2340 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2341 }
2342
2343 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2344 sinfo->tx_packets = 0;
2345 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2346 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2347 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2348 }
2349
2350 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2351 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2352 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2353
2354 if (sta->deflink.pcpu_rx_stats) {
2355 for_each_possible_cpu(cpu) {
2356 struct ieee80211_sta_rx_stats *cpurxs;
2357
2358 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2359 cpu);
2360 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2361 }
2362 }
2363
2364 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2365 }
2366
2367 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2368 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2369 if (sta->deflink.pcpu_rx_stats) {
2370 for_each_possible_cpu(cpu) {
2371 struct ieee80211_sta_rx_stats *cpurxs;
2372
2373 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2374 cpu);
2375 sinfo->rx_packets += cpurxs->packets;
2376 }
2377 }
2378 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2379 }
2380
2381 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2382 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2383 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2384 }
2385
2386 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2387 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2388 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2389 }
2390
2391 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2392 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2393 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2394 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2395 }
2396
2397 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2398 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2399 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2400 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2401 }
2402
2403 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2404 sinfo->airtime_weight = sta->airtime[0].weight;
2405 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2406 }
2407
2408 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2409 if (sta->deflink.pcpu_rx_stats) {
2410 for_each_possible_cpu(cpu) {
2411 struct ieee80211_sta_rx_stats *cpurxs;
2412
2413 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2414 sinfo->rx_dropped_misc += cpurxs->dropped;
2415 }
2416 }
2417
2418 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2419 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2420 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2421 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2422 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2423 }
2424
2425 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2426 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2427 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2428 sinfo->signal = (s8)last_rxstats->last_signal;
2429 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2430 }
2431
2432 if (!sta->deflink.pcpu_rx_stats &&
2433 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2434 sinfo->signal_avg =
2435 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2436 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2437 }
2438 }
2439
2440 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2441 * the sta->rx_stats struct, so the check here is fine with and without
2442 * pcpu statistics
2443 */
2444 if (last_rxstats->chains &&
2445 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2446 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2447 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2448 if (!sta->deflink.pcpu_rx_stats)
2449 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2450
2451 sinfo->chains = last_rxstats->chains;
2452
2453 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2454 sinfo->chain_signal[i] =
2455 last_rxstats->chain_signal_last[i];
2456 sinfo->chain_signal_avg[i] =
2457 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2458 }
2459 }
2460
2461 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2462 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2463 &sinfo->txrate);
2464 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2465 }
2466
2467 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2468 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2469 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2470 }
2471
2472 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2473 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2474 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2475 }
2476
2477 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2478#ifdef CONFIG_MAC80211_MESH
2479 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2480 BIT_ULL(NL80211_STA_INFO_PLID) |
2481 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2482 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2483 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2484 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2485 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2486 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2487
2488 sinfo->llid = sta->mesh->llid;
2489 sinfo->plid = sta->mesh->plid;
2490 sinfo->plink_state = sta->mesh->plink_state;
2491 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2492 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2493 sinfo->t_offset = sta->mesh->t_offset;
2494 }
2495 sinfo->local_pm = sta->mesh->local_pm;
2496 sinfo->peer_pm = sta->mesh->peer_pm;
2497 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2498 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2499 sinfo->connected_to_as = sta->mesh->connected_to_as;
2500#endif
2501 }
2502
2503 sinfo->bss_param.flags = 0;
2504 if (sdata->vif.bss_conf.use_cts_prot)
2505 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2506 if (sdata->vif.bss_conf.use_short_preamble)
2507 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2508 if (sdata->vif.bss_conf.use_short_slot)
2509 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2510 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2511 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2512
2513 sinfo->sta_flags.set = 0;
2514 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2515 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2516 BIT(NL80211_STA_FLAG_WME) |
2517 BIT(NL80211_STA_FLAG_MFP) |
2518 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2519 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2520 BIT(NL80211_STA_FLAG_TDLS_PEER);
2521 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2522 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2523 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2524 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2525 if (sta->sta.wme)
2526 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2527 if (test_sta_flag(sta, WLAN_STA_MFP))
2528 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2529 if (test_sta_flag(sta, WLAN_STA_AUTH))
2530 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2531 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2532 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2533 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2534 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2535
2536 thr = sta_get_expected_throughput(sta);
2537
2538 if (thr != 0) {
2539 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2540 sinfo->expected_throughput = thr;
2541 }
2542
2543 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2544 sta->deflink.status_stats.ack_signal_filled) {
2545 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2546 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2547 }
2548
2549 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2550 sta->deflink.status_stats.ack_signal_filled) {
2551 sinfo->avg_ack_signal =
2552 -(s8)ewma_avg_signal_read(
2553 &sta->deflink.status_stats.avg_ack_signal);
2554 sinfo->filled |=
2555 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2556 }
2557
2558 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2559 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2560 sinfo->airtime_link_metric =
2561 airtime_link_metric_get(local, sta);
2562 }
2563}
2564
2565u32 sta_get_expected_throughput(struct sta_info *sta)
2566{
2567 struct ieee80211_sub_if_data *sdata = sta->sdata;
2568 struct ieee80211_local *local = sdata->local;
2569 struct rate_control_ref *ref = NULL;
2570 u32 thr = 0;
2571
2572 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2573 ref = local->rate_ctrl;
2574
2575 /* check if the driver has a SW RC implementation */
2576 if (ref && ref->ops->get_expected_throughput)
2577 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2578 else
2579 thr = drv_get_expected_throughput(local, sta);
2580
2581 return thr;
2582}
2583
2584unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2585{
2586 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2587
2588 if (!sta->deflink.status_stats.last_ack ||
2589 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2590 return stats->last_rx;
2591 return sta->deflink.status_stats.last_ack;
2592}
2593
2594static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2595{
2596 if (!sta->sdata->local->ops->wake_tx_queue)
2597 return;
2598
2599 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2600 sta->cparams.target = MS2TIME(50);
2601 sta->cparams.interval = MS2TIME(300);
2602 sta->cparams.ecn = false;
2603 } else {
2604 sta->cparams.target = MS2TIME(20);
2605 sta->cparams.interval = MS2TIME(100);
2606 sta->cparams.ecn = true;
2607 }
2608}
2609
2610void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2611 u32 thr)
2612{
2613 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2614
2615 sta_update_codel_params(sta, thr);
2616}