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
67 static 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 */
sta_info_hash_del(struct ieee80211_local * local,struct sta_info * sta)77 static 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
__cleanup_single_sta(struct sta_info * sta)84 static 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
cleanup_single_sta(struct sta_info * sta)150 static 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
sta_info_hash_lookup(struct ieee80211_local * local,const u8 * addr)159 struct 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 */
sta_info_get(struct ieee80211_sub_if_data * sdata,const u8 * addr)166 struct 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 */
sta_info_get_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)191 struct 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
sta_info_get_by_addrs(struct ieee80211_local * local,const u8 * sta_addr,const u8 * vif_addr)213 struct 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
sta_info_get_by_idx(struct ieee80211_sub_if_data * sdata,int idx)227 struct 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 */
sta_info_free(struct ieee80211_local * local,struct sta_info * sta)259 void 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 */
sta_info_hash_add(struct ieee80211_local * local,struct sta_info * sta)295 static 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
sta_deliver_ps_frames(struct work_struct * wk)302 static 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
sta_prepare_rate_control(struct ieee80211_local * local,struct sta_info * sta,gfp_t gfp)321 static 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
sta_info_alloc(struct ieee80211_sub_if_data * sdata,const u8 * addr,gfp_t gfp)336 struct 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 (ieee80211_vif_is_mesh(&sdata->vif) &&
368 !sdata->u.mesh.user_mpm)
369 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
370 0);
371 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
372 }
373 #endif
374
375 memcpy(sta->addr, addr, ETH_ALEN);
376 memcpy(sta->sta.addr, addr, ETH_ALEN);
377 memcpy(sta->deflink.addr, addr, ETH_ALEN);
378 memcpy(sta->sta.deflink.addr, addr, ETH_ALEN);
379 sta->sta.max_rx_aggregation_subframes =
380 local->hw.max_rx_aggregation_subframes;
381
382 /* TODO link specific alloc and assignments for MLO Link STA */
383
384 /* For non MLO STA, link info can be accessed either via deflink
385 * or link[0]
386 */
387 sta->link[0] = &sta->deflink;
388 sta->sta.link[0] = &sta->sta.deflink;
389
390 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
391 * The Tx path starts to use a key as soon as the key slot ptk_idx
392 * references to is not NULL. To not use the initial Rx-only key
393 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
394 * which always will refer to a NULL key.
395 */
396 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
397 sta->ptk_idx = INVALID_PTK_KEYIDX;
398
399 sta->local = local;
400 sta->sdata = sdata;
401 sta->deflink.rx_stats.last_rx = jiffies;
402
403 u64_stats_init(&sta->deflink.rx_stats.syncp);
404
405 ieee80211_init_frag_cache(&sta->frags);
406
407 sta->sta_state = IEEE80211_STA_NONE;
408
409 /* Mark TID as unreserved */
410 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
411
412 sta->last_connected = ktime_get_seconds();
413 ewma_signal_init(&sta->deflink.rx_stats_avg.signal);
414 ewma_avg_signal_init(&sta->deflink.status_stats.avg_ack_signal);
415 for (i = 0; i < ARRAY_SIZE(sta->deflink.rx_stats_avg.chain_signal); i++)
416 ewma_signal_init(&sta->deflink.rx_stats_avg.chain_signal[i]);
417
418 if (local->ops->wake_tx_queue) {
419 void *txq_data;
420 int size = sizeof(struct txq_info) +
421 ALIGN(hw->txq_data_size, sizeof(void *));
422
423 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
424 if (!txq_data)
425 goto free;
426
427 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
428 struct txq_info *txq = txq_data + i * size;
429
430 /* might not do anything for the bufferable MMPDU TXQ */
431 ieee80211_txq_init(sdata, sta, txq, i);
432 }
433 }
434
435 if (sta_prepare_rate_control(local, sta, gfp))
436 goto free_txq;
437
438
439 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
440 skb_queue_head_init(&sta->ps_tx_buf[i]);
441 skb_queue_head_init(&sta->tx_filtered[i]);
442 init_airtime_info(&sta->airtime[i], &local->airtime[i]);
443 }
444
445 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
446 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
447
448 for (i = 0; i < NUM_NL80211_BANDS; i++) {
449 u32 mandatory = 0;
450 int r;
451
452 if (!hw->wiphy->bands[i])
453 continue;
454
455 switch (i) {
456 case NL80211_BAND_2GHZ:
457 case NL80211_BAND_LC:
458 /*
459 * We use both here, even if we cannot really know for
460 * sure the station will support both, but the only use
461 * for this is when we don't know anything yet and send
462 * management frames, and then we'll pick the lowest
463 * possible rate anyway.
464 * If we don't include _G here, we cannot find a rate
465 * in P2P, and thus trigger the WARN_ONCE() in rate.c
466 */
467 mandatory = IEEE80211_RATE_MANDATORY_B |
468 IEEE80211_RATE_MANDATORY_G;
469 break;
470 case NL80211_BAND_5GHZ:
471 mandatory = IEEE80211_RATE_MANDATORY_A;
472 break;
473 case NL80211_BAND_60GHZ:
474 WARN_ON(1);
475 mandatory = 0;
476 break;
477 }
478
479 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
480 struct ieee80211_rate *rate;
481
482 rate = &hw->wiphy->bands[i]->bitrates[r];
483
484 if (!(rate->flags & mandatory))
485 continue;
486 sta->sta.deflink.supp_rates[i] |= BIT(r);
487 }
488 }
489
490 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
491 if (sdata->vif.type == NL80211_IFTYPE_AP ||
492 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
493 struct ieee80211_supported_band *sband;
494 u8 smps;
495
496 sband = ieee80211_get_sband(sdata);
497 if (!sband)
498 goto free_txq;
499
500 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
501 IEEE80211_HT_CAP_SM_PS_SHIFT;
502 /*
503 * Assume that hostapd advertises our caps in the beacon and
504 * this is the known_smps_mode for a station that just assciated
505 */
506 switch (smps) {
507 case WLAN_HT_SMPS_CONTROL_DISABLED:
508 sta->known_smps_mode = IEEE80211_SMPS_OFF;
509 break;
510 case WLAN_HT_SMPS_CONTROL_STATIC:
511 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
512 break;
513 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
514 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
515 break;
516 default:
517 WARN_ON(1);
518 }
519 }
520
521 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
522
523 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
524 sta->cparams.target = MS2TIME(20);
525 sta->cparams.interval = MS2TIME(100);
526 sta->cparams.ecn = true;
527
528 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
529
530 return sta;
531
532 free_txq:
533 if (sta->sta.txq[0])
534 kfree(to_txq_info(sta->sta.txq[0]));
535 free:
536 free_percpu(sta->deflink.pcpu_rx_stats);
537 #ifdef CONFIG_MAC80211_MESH
538 kfree(sta->mesh);
539 #endif
540 kfree(sta);
541 return NULL;
542 }
543
sta_info_insert_check(struct sta_info * sta)544 static int sta_info_insert_check(struct sta_info *sta)
545 {
546 struct ieee80211_sub_if_data *sdata = sta->sdata;
547
548 /*
549 * Can't be a WARN_ON because it can be triggered through a race:
550 * something inserts a STA (on one CPU) without holding the RTNL
551 * and another CPU turns off the net device.
552 */
553 if (unlikely(!ieee80211_sdata_running(sdata)))
554 return -ENETDOWN;
555
556 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
557 !is_valid_ether_addr(sta->sta.addr)))
558 return -EINVAL;
559
560 /* The RCU read lock is required by rhashtable due to
561 * asynchronous resize/rehash. We also require the mutex
562 * for correctness.
563 */
564 rcu_read_lock();
565 lockdep_assert_held(&sdata->local->sta_mtx);
566 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
567 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
568 rcu_read_unlock();
569 return -ENOTUNIQ;
570 }
571 rcu_read_unlock();
572
573 return 0;
574 }
575
sta_info_insert_drv_state(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct sta_info * sta)576 static int sta_info_insert_drv_state(struct ieee80211_local *local,
577 struct ieee80211_sub_if_data *sdata,
578 struct sta_info *sta)
579 {
580 enum ieee80211_sta_state state;
581 int err = 0;
582
583 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
584 err = drv_sta_state(local, sdata, sta, state, state + 1);
585 if (err)
586 break;
587 }
588
589 if (!err) {
590 /*
591 * Drivers using legacy sta_add/sta_remove callbacks only
592 * get uploaded set to true after sta_add is called.
593 */
594 if (!local->ops->sta_add)
595 sta->uploaded = true;
596 return 0;
597 }
598
599 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
600 sdata_info(sdata,
601 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
602 sta->sta.addr, state + 1, err);
603 err = 0;
604 }
605
606 /* unwind on error */
607 for (; state > IEEE80211_STA_NOTEXIST; state--)
608 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
609
610 return err;
611 }
612
613 static void
ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data * sdata)614 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
615 {
616 struct ieee80211_local *local = sdata->local;
617 bool allow_p2p_go_ps = sdata->vif.p2p;
618 struct sta_info *sta;
619
620 rcu_read_lock();
621 list_for_each_entry_rcu(sta, &local->sta_list, list) {
622 if (sdata != sta->sdata ||
623 !test_sta_flag(sta, WLAN_STA_ASSOC))
624 continue;
625 if (!sta->sta.support_p2p_ps) {
626 allow_p2p_go_ps = false;
627 break;
628 }
629 }
630 rcu_read_unlock();
631
632 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
633 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
634 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
635 }
636 }
637
638 /*
639 * should be called with sta_mtx locked
640 * this function replaces the mutex lock
641 * with a RCU lock
642 */
sta_info_insert_finish(struct sta_info * sta)643 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
644 {
645 struct ieee80211_local *local = sta->local;
646 struct ieee80211_sub_if_data *sdata = sta->sdata;
647 struct station_info *sinfo = NULL;
648 int err = 0;
649
650 lockdep_assert_held(&local->sta_mtx);
651
652 /* check if STA exists already */
653 if (sta_info_get_bss(sdata, sta->sta.addr)) {
654 err = -EEXIST;
655 goto out_cleanup;
656 }
657
658 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
659 if (!sinfo) {
660 err = -ENOMEM;
661 goto out_cleanup;
662 }
663
664 local->num_sta++;
665 local->sta_generation++;
666 smp_mb();
667
668 /* simplify things and don't accept BA sessions yet */
669 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
670
671 /* make the station visible */
672 err = sta_info_hash_add(local, sta);
673 if (err)
674 goto out_drop_sta;
675
676 list_add_tail_rcu(&sta->list, &local->sta_list);
677
678 /* notify driver */
679 err = sta_info_insert_drv_state(local, sdata, sta);
680 if (err)
681 goto out_remove;
682
683 set_sta_flag(sta, WLAN_STA_INSERTED);
684
685 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
686 ieee80211_recalc_min_chandef(sta->sdata);
687 if (!sta->sta.support_p2p_ps)
688 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
689 }
690
691 /* accept BA sessions now */
692 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
693
694 ieee80211_sta_debugfs_add(sta);
695 rate_control_add_sta_debugfs(sta);
696
697 sinfo->generation = local->sta_generation;
698 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
699 kfree(sinfo);
700
701 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
702
703 /* move reference to rcu-protected */
704 rcu_read_lock();
705 mutex_unlock(&local->sta_mtx);
706
707 if (ieee80211_vif_is_mesh(&sdata->vif))
708 mesh_accept_plinks_update(sdata);
709
710 ieee80211_check_fast_xmit(sta);
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
sta_info_insert_rcu(struct sta_info * sta)727 int 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
sta_info_insert(struct sta_info * sta)747 int 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
__bss_tim_set(u8 * tim,u16 id)756 static 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
__bss_tim_clear(u8 * tim,u16 id)765 static 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
__bss_tim_get(u8 * tim,u16 id)774 static 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
ieee80211_tids_for_ac(int ac)783 static 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
__sta_info_recalc_tim(struct sta_info * sta,bool ignore_pending)801 static 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
879 out_unlock:
880 spin_unlock_bh(&local->tim_lock);
881 }
882
sta_info_recalc_tim(struct sta_info * sta)883 void sta_info_recalc_tim(struct sta_info *sta)
884 {
885 __sta_info_recalc_tim(sta, false);
886 }
887
sta_info_buffer_expired(struct sta_info * sta,struct sk_buff * skb)888 static 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
sta_info_cleanup_expire_buffered_ac(struct ieee80211_local * local,struct sta_info * sta,int ac)908 static 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
sta_info_cleanup_expire_buffered(struct ieee80211_local * local,struct sta_info * sta)986 static 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
__sta_info_destroy_part1(struct sta_info * sta)1004 static 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 if (sta->uploaded)
1053 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1054
1055 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1056 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1057 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1058
1059 return 0;
1060 }
1061
__sta_info_destroy_part2(struct sta_info * sta)1062 static void __sta_info_destroy_part2(struct sta_info *sta)
1063 {
1064 struct ieee80211_local *local = sta->local;
1065 struct ieee80211_sub_if_data *sdata = sta->sdata;
1066 struct station_info *sinfo;
1067 int ret;
1068
1069 /*
1070 * NOTE: This assumes at least synchronize_net() was done
1071 * after _part1 and before _part2!
1072 */
1073
1074 might_sleep();
1075 lockdep_assert_held(&local->sta_mtx);
1076
1077 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1078 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1079 WARN_ON_ONCE(ret);
1080 }
1081
1082 /* now keys can no longer be reached */
1083 ieee80211_free_sta_keys(local, sta);
1084
1085 /* disable TIM bit - last chance to tell driver */
1086 __sta_info_recalc_tim(sta, true);
1087
1088 sta->dead = true;
1089
1090 local->num_sta--;
1091 local->sta_generation++;
1092
1093 while (sta->sta_state > IEEE80211_STA_NONE) {
1094 ret = sta_info_move_state(sta, sta->sta_state - 1);
1095 if (ret) {
1096 WARN_ON_ONCE(1);
1097 break;
1098 }
1099 }
1100
1101 if (sta->uploaded) {
1102 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1103 IEEE80211_STA_NOTEXIST);
1104 WARN_ON_ONCE(ret != 0);
1105 }
1106
1107 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1108
1109 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1110 if (sinfo)
1111 sta_set_sinfo(sta, sinfo, true);
1112 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1113 kfree(sinfo);
1114
1115 ieee80211_sta_debugfs_remove(sta);
1116
1117 ieee80211_destroy_frag_cache(&sta->frags);
1118
1119 cleanup_single_sta(sta);
1120 }
1121
__sta_info_destroy(struct sta_info * sta)1122 int __must_check __sta_info_destroy(struct sta_info *sta)
1123 {
1124 int err = __sta_info_destroy_part1(sta);
1125
1126 if (err)
1127 return err;
1128
1129 synchronize_net();
1130
1131 __sta_info_destroy_part2(sta);
1132
1133 return 0;
1134 }
1135
sta_info_destroy_addr(struct ieee80211_sub_if_data * sdata,const u8 * addr)1136 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1137 {
1138 struct sta_info *sta;
1139 int ret;
1140
1141 mutex_lock(&sdata->local->sta_mtx);
1142 sta = sta_info_get(sdata, addr);
1143 ret = __sta_info_destroy(sta);
1144 mutex_unlock(&sdata->local->sta_mtx);
1145
1146 return ret;
1147 }
1148
sta_info_destroy_addr_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)1149 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1150 const u8 *addr)
1151 {
1152 struct sta_info *sta;
1153 int ret;
1154
1155 mutex_lock(&sdata->local->sta_mtx);
1156 sta = sta_info_get_bss(sdata, addr);
1157 ret = __sta_info_destroy(sta);
1158 mutex_unlock(&sdata->local->sta_mtx);
1159
1160 return ret;
1161 }
1162
sta_info_cleanup(struct timer_list * t)1163 static void sta_info_cleanup(struct timer_list *t)
1164 {
1165 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1166 struct sta_info *sta;
1167 bool timer_needed = false;
1168
1169 rcu_read_lock();
1170 list_for_each_entry_rcu(sta, &local->sta_list, list)
1171 if (sta_info_cleanup_expire_buffered(local, sta))
1172 timer_needed = true;
1173 rcu_read_unlock();
1174
1175 if (local->quiescing)
1176 return;
1177
1178 if (!timer_needed)
1179 return;
1180
1181 mod_timer(&local->sta_cleanup,
1182 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1183 }
1184
sta_info_init(struct ieee80211_local * local)1185 int sta_info_init(struct ieee80211_local *local)
1186 {
1187 int err;
1188
1189 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1190 if (err)
1191 return err;
1192
1193 spin_lock_init(&local->tim_lock);
1194 mutex_init(&local->sta_mtx);
1195 INIT_LIST_HEAD(&local->sta_list);
1196
1197 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1198 return 0;
1199 }
1200
sta_info_stop(struct ieee80211_local * local)1201 void sta_info_stop(struct ieee80211_local *local)
1202 {
1203 del_timer_sync(&local->sta_cleanup);
1204 rhltable_destroy(&local->sta_hash);
1205 }
1206
1207
__sta_info_flush(struct ieee80211_sub_if_data * sdata,bool vlans)1208 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1209 {
1210 struct ieee80211_local *local = sdata->local;
1211 struct sta_info *sta, *tmp;
1212 LIST_HEAD(free_list);
1213 int ret = 0;
1214
1215 might_sleep();
1216
1217 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1218 WARN_ON(vlans && !sdata->bss);
1219
1220 mutex_lock(&local->sta_mtx);
1221 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1222 if (sdata == sta->sdata ||
1223 (vlans && sdata->bss == sta->sdata->bss)) {
1224 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1225 list_add(&sta->free_list, &free_list);
1226 ret++;
1227 }
1228 }
1229
1230 if (!list_empty(&free_list)) {
1231 synchronize_net();
1232 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1233 __sta_info_destroy_part2(sta);
1234 }
1235 mutex_unlock(&local->sta_mtx);
1236
1237 return ret;
1238 }
1239
ieee80211_sta_expire(struct ieee80211_sub_if_data * sdata,unsigned long exp_time)1240 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1241 unsigned long exp_time)
1242 {
1243 struct ieee80211_local *local = sdata->local;
1244 struct sta_info *sta, *tmp;
1245
1246 mutex_lock(&local->sta_mtx);
1247
1248 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1249 unsigned long last_active = ieee80211_sta_last_active(sta);
1250
1251 if (sdata != sta->sdata)
1252 continue;
1253
1254 if (time_is_before_jiffies(last_active + exp_time)) {
1255 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1256 sta->sta.addr);
1257
1258 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1259 test_sta_flag(sta, WLAN_STA_PS_STA))
1260 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1261
1262 WARN_ON(__sta_info_destroy(sta));
1263 }
1264 }
1265
1266 mutex_unlock(&local->sta_mtx);
1267 }
1268
ieee80211_find_sta_by_ifaddr(struct ieee80211_hw * hw,const u8 * addr,const u8 * localaddr)1269 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1270 const u8 *addr,
1271 const u8 *localaddr)
1272 {
1273 struct ieee80211_local *local = hw_to_local(hw);
1274 struct rhlist_head *tmp;
1275 struct sta_info *sta;
1276
1277 /*
1278 * Just return a random station if localaddr is NULL
1279 * ... first in list.
1280 */
1281 for_each_sta_info(local, addr, sta, tmp) {
1282 if (localaddr &&
1283 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1284 continue;
1285 if (!sta->uploaded)
1286 return NULL;
1287 return &sta->sta;
1288 }
1289
1290 return NULL;
1291 }
1292 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1293
ieee80211_find_sta(struct ieee80211_vif * vif,const u8 * addr)1294 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1295 const u8 *addr)
1296 {
1297 struct sta_info *sta;
1298
1299 if (!vif)
1300 return NULL;
1301
1302 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1303 if (!sta)
1304 return NULL;
1305
1306 if (!sta->uploaded)
1307 return NULL;
1308
1309 return &sta->sta;
1310 }
1311 EXPORT_SYMBOL(ieee80211_find_sta);
1312
1313 /* powersave support code */
ieee80211_sta_ps_deliver_wakeup(struct sta_info * sta)1314 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1315 {
1316 struct ieee80211_sub_if_data *sdata = sta->sdata;
1317 struct ieee80211_local *local = sdata->local;
1318 struct sk_buff_head pending;
1319 int filtered = 0, buffered = 0, ac, i;
1320 unsigned long flags;
1321 struct ps_data *ps;
1322
1323 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1324 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1325 u.ap);
1326
1327 if (sdata->vif.type == NL80211_IFTYPE_AP)
1328 ps = &sdata->bss->ps;
1329 else if (ieee80211_vif_is_mesh(&sdata->vif))
1330 ps = &sdata->u.mesh.ps;
1331 else
1332 return;
1333
1334 clear_sta_flag(sta, WLAN_STA_SP);
1335
1336 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1337 sta->driver_buffered_tids = 0;
1338 sta->txq_buffered_tids = 0;
1339
1340 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1341 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1342
1343 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1344 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1345 continue;
1346
1347 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1348 }
1349
1350 skb_queue_head_init(&pending);
1351
1352 /* sync with ieee80211_tx_h_unicast_ps_buf */
1353 spin_lock(&sta->ps_lock);
1354 /* Send all buffered frames to the station */
1355 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1356 int count = skb_queue_len(&pending), tmp;
1357
1358 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1359 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1360 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1361 tmp = skb_queue_len(&pending);
1362 filtered += tmp - count;
1363 count = tmp;
1364
1365 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1366 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1367 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1368 tmp = skb_queue_len(&pending);
1369 buffered += tmp - count;
1370 }
1371
1372 ieee80211_add_pending_skbs(local, &pending);
1373
1374 /* now we're no longer in the deliver code */
1375 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1376
1377 /* The station might have polled and then woken up before we responded,
1378 * so clear these flags now to avoid them sticking around.
1379 */
1380 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1381 clear_sta_flag(sta, WLAN_STA_UAPSD);
1382 spin_unlock(&sta->ps_lock);
1383
1384 atomic_dec(&ps->num_sta_ps);
1385
1386 local->total_ps_buffered -= buffered;
1387
1388 sta_info_recalc_tim(sta);
1389
1390 ps_dbg(sdata,
1391 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1392 sta->sta.addr, sta->sta.aid, filtered, buffered);
1393
1394 ieee80211_check_fast_xmit(sta);
1395 }
1396
ieee80211_send_null_response(struct sta_info * sta,int tid,enum ieee80211_frame_release_type reason,bool call_driver,bool more_data)1397 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1398 enum ieee80211_frame_release_type reason,
1399 bool call_driver, bool more_data)
1400 {
1401 struct ieee80211_sub_if_data *sdata = sta->sdata;
1402 struct ieee80211_local *local = sdata->local;
1403 struct ieee80211_qos_hdr *nullfunc;
1404 struct sk_buff *skb;
1405 int size = sizeof(*nullfunc);
1406 __le16 fc;
1407 bool qos = sta->sta.wme;
1408 struct ieee80211_tx_info *info;
1409 struct ieee80211_chanctx_conf *chanctx_conf;
1410
1411 if (qos) {
1412 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1413 IEEE80211_STYPE_QOS_NULLFUNC |
1414 IEEE80211_FCTL_FROMDS);
1415 } else {
1416 size -= 2;
1417 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1418 IEEE80211_STYPE_NULLFUNC |
1419 IEEE80211_FCTL_FROMDS);
1420 }
1421
1422 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1423 if (!skb)
1424 return;
1425
1426 skb_reserve(skb, local->hw.extra_tx_headroom);
1427
1428 nullfunc = skb_put(skb, size);
1429 nullfunc->frame_control = fc;
1430 nullfunc->duration_id = 0;
1431 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1432 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1433 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1434 nullfunc->seq_ctrl = 0;
1435
1436 skb->priority = tid;
1437 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1438 if (qos) {
1439 nullfunc->qos_ctrl = cpu_to_le16(tid);
1440
1441 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1442 nullfunc->qos_ctrl |=
1443 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1444 if (more_data)
1445 nullfunc->frame_control |=
1446 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1447 }
1448 }
1449
1450 info = IEEE80211_SKB_CB(skb);
1451
1452 /*
1453 * Tell TX path to send this frame even though the
1454 * STA may still remain is PS mode after this frame
1455 * exchange. Also set EOSP to indicate this packet
1456 * ends the poll/service period.
1457 */
1458 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1459 IEEE80211_TX_STATUS_EOSP |
1460 IEEE80211_TX_CTL_REQ_TX_STATUS;
1461
1462 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1463
1464 if (call_driver)
1465 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1466 reason, false);
1467
1468 skb->dev = sdata->dev;
1469
1470 rcu_read_lock();
1471 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1472 if (WARN_ON(!chanctx_conf)) {
1473 rcu_read_unlock();
1474 kfree_skb(skb);
1475 return;
1476 }
1477
1478 info->band = chanctx_conf->def.chan->band;
1479 ieee80211_xmit(sdata, sta, skb);
1480 rcu_read_unlock();
1481 }
1482
find_highest_prio_tid(unsigned long tids)1483 static int find_highest_prio_tid(unsigned long tids)
1484 {
1485 /* lower 3 TIDs aren't ordered perfectly */
1486 if (tids & 0xF8)
1487 return fls(tids) - 1;
1488 /* TID 0 is BE just like TID 3 */
1489 if (tids & BIT(0))
1490 return 0;
1491 return fls(tids) - 1;
1492 }
1493
1494 /* Indicates if the MORE_DATA bit should be set in the last
1495 * frame obtained by ieee80211_sta_ps_get_frames.
1496 * Note that driver_release_tids is relevant only if
1497 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1498 */
1499 static bool
ieee80211_sta_ps_more_data(struct sta_info * sta,u8 ignored_acs,enum ieee80211_frame_release_type reason,unsigned long driver_release_tids)1500 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1501 enum ieee80211_frame_release_type reason,
1502 unsigned long driver_release_tids)
1503 {
1504 int ac;
1505
1506 /* If the driver has data on more than one TID then
1507 * certainly there's more data if we release just a
1508 * single frame now (from a single TID). This will
1509 * only happen for PS-Poll.
1510 */
1511 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1512 hweight16(driver_release_tids) > 1)
1513 return true;
1514
1515 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1516 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1517 continue;
1518
1519 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1520 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1521 return true;
1522 }
1523
1524 return false;
1525 }
1526
1527 static void
ieee80211_sta_ps_get_frames(struct sta_info * sta,int n_frames,u8 ignored_acs,enum ieee80211_frame_release_type reason,struct sk_buff_head * frames,unsigned long * driver_release_tids)1528 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1529 enum ieee80211_frame_release_type reason,
1530 struct sk_buff_head *frames,
1531 unsigned long *driver_release_tids)
1532 {
1533 struct ieee80211_sub_if_data *sdata = sta->sdata;
1534 struct ieee80211_local *local = sdata->local;
1535 int ac;
1536
1537 /* Get response frame(s) and more data bit for the last one. */
1538 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1539 unsigned long tids;
1540
1541 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1542 continue;
1543
1544 tids = ieee80211_tids_for_ac(ac);
1545
1546 /* if we already have frames from software, then we can't also
1547 * release from hardware queues
1548 */
1549 if (skb_queue_empty(frames)) {
1550 *driver_release_tids |=
1551 sta->driver_buffered_tids & tids;
1552 *driver_release_tids |= sta->txq_buffered_tids & tids;
1553 }
1554
1555 if (!*driver_release_tids) {
1556 struct sk_buff *skb;
1557
1558 while (n_frames > 0) {
1559 skb = skb_dequeue(&sta->tx_filtered[ac]);
1560 if (!skb) {
1561 skb = skb_dequeue(
1562 &sta->ps_tx_buf[ac]);
1563 if (skb)
1564 local->total_ps_buffered--;
1565 }
1566 if (!skb)
1567 break;
1568 n_frames--;
1569 __skb_queue_tail(frames, skb);
1570 }
1571 }
1572
1573 /* If we have more frames buffered on this AC, then abort the
1574 * loop since we can't send more data from other ACs before
1575 * the buffered frames from this.
1576 */
1577 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1578 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1579 break;
1580 }
1581 }
1582
1583 static void
ieee80211_sta_ps_deliver_response(struct sta_info * sta,int n_frames,u8 ignored_acs,enum ieee80211_frame_release_type reason)1584 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1585 int n_frames, u8 ignored_acs,
1586 enum ieee80211_frame_release_type reason)
1587 {
1588 struct ieee80211_sub_if_data *sdata = sta->sdata;
1589 struct ieee80211_local *local = sdata->local;
1590 unsigned long driver_release_tids = 0;
1591 struct sk_buff_head frames;
1592 bool more_data;
1593
1594 /* Service or PS-Poll period starts */
1595 set_sta_flag(sta, WLAN_STA_SP);
1596
1597 __skb_queue_head_init(&frames);
1598
1599 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1600 &frames, &driver_release_tids);
1601
1602 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1603
1604 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1605 driver_release_tids =
1606 BIT(find_highest_prio_tid(driver_release_tids));
1607
1608 if (skb_queue_empty(&frames) && !driver_release_tids) {
1609 int tid, ac;
1610
1611 /*
1612 * For PS-Poll, this can only happen due to a race condition
1613 * when we set the TIM bit and the station notices it, but
1614 * before it can poll for the frame we expire it.
1615 *
1616 * For uAPSD, this is said in the standard (11.2.1.5 h):
1617 * At each unscheduled SP for a non-AP STA, the AP shall
1618 * attempt to transmit at least one MSDU or MMPDU, but no
1619 * more than the value specified in the Max SP Length field
1620 * in the QoS Capability element from delivery-enabled ACs,
1621 * that are destined for the non-AP STA.
1622 *
1623 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1624 */
1625
1626 /* This will evaluate to 1, 3, 5 or 7. */
1627 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1628 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1629 break;
1630 tid = 7 - 2 * ac;
1631
1632 ieee80211_send_null_response(sta, tid, reason, true, false);
1633 } else if (!driver_release_tids) {
1634 struct sk_buff_head pending;
1635 struct sk_buff *skb;
1636 int num = 0;
1637 u16 tids = 0;
1638 bool need_null = false;
1639
1640 skb_queue_head_init(&pending);
1641
1642 while ((skb = __skb_dequeue(&frames))) {
1643 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1644 struct ieee80211_hdr *hdr = (void *) skb->data;
1645 u8 *qoshdr = NULL;
1646
1647 num++;
1648
1649 /*
1650 * Tell TX path to send this frame even though the
1651 * STA may still remain is PS mode after this frame
1652 * exchange.
1653 */
1654 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1655 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1656
1657 /*
1658 * Use MoreData flag to indicate whether there are
1659 * more buffered frames for this STA
1660 */
1661 if (more_data || !skb_queue_empty(&frames))
1662 hdr->frame_control |=
1663 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1664 else
1665 hdr->frame_control &=
1666 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1667
1668 if (ieee80211_is_data_qos(hdr->frame_control) ||
1669 ieee80211_is_qos_nullfunc(hdr->frame_control))
1670 qoshdr = ieee80211_get_qos_ctl(hdr);
1671
1672 tids |= BIT(skb->priority);
1673
1674 __skb_queue_tail(&pending, skb);
1675
1676 /* end service period after last frame or add one */
1677 if (!skb_queue_empty(&frames))
1678 continue;
1679
1680 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1681 /* for PS-Poll, there's only one frame */
1682 info->flags |= IEEE80211_TX_STATUS_EOSP |
1683 IEEE80211_TX_CTL_REQ_TX_STATUS;
1684 break;
1685 }
1686
1687 /* For uAPSD, things are a bit more complicated. If the
1688 * last frame has a QoS header (i.e. is a QoS-data or
1689 * QoS-nulldata frame) then just set the EOSP bit there
1690 * and be done.
1691 * If the frame doesn't have a QoS header (which means
1692 * it should be a bufferable MMPDU) then we can't set
1693 * the EOSP bit in the QoS header; add a QoS-nulldata
1694 * frame to the list to send it after the MMPDU.
1695 *
1696 * Note that this code is only in the mac80211-release
1697 * code path, we assume that the driver will not buffer
1698 * anything but QoS-data frames, or if it does, will
1699 * create the QoS-nulldata frame by itself if needed.
1700 *
1701 * Cf. 802.11-2012 10.2.1.10 (c).
1702 */
1703 if (qoshdr) {
1704 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1705
1706 info->flags |= IEEE80211_TX_STATUS_EOSP |
1707 IEEE80211_TX_CTL_REQ_TX_STATUS;
1708 } else {
1709 /* The standard isn't completely clear on this
1710 * as it says the more-data bit should be set
1711 * if there are more BUs. The QoS-Null frame
1712 * we're about to send isn't buffered yet, we
1713 * only create it below, but let's pretend it
1714 * was buffered just in case some clients only
1715 * expect more-data=0 when eosp=1.
1716 */
1717 hdr->frame_control |=
1718 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1719 need_null = true;
1720 num++;
1721 }
1722 break;
1723 }
1724
1725 drv_allow_buffered_frames(local, sta, tids, num,
1726 reason, more_data);
1727
1728 ieee80211_add_pending_skbs(local, &pending);
1729
1730 if (need_null)
1731 ieee80211_send_null_response(
1732 sta, find_highest_prio_tid(tids),
1733 reason, false, false);
1734
1735 sta_info_recalc_tim(sta);
1736 } else {
1737 int tid;
1738
1739 /*
1740 * We need to release a frame that is buffered somewhere in the
1741 * driver ... it'll have to handle that.
1742 * Note that the driver also has to check the number of frames
1743 * on the TIDs we're releasing from - if there are more than
1744 * n_frames it has to set the more-data bit (if we didn't ask
1745 * it to set it anyway due to other buffered frames); if there
1746 * are fewer than n_frames it has to make sure to adjust that
1747 * to allow the service period to end properly.
1748 */
1749 drv_release_buffered_frames(local, sta, driver_release_tids,
1750 n_frames, reason, more_data);
1751
1752 /*
1753 * Note that we don't recalculate the TIM bit here as it would
1754 * most likely have no effect at all unless the driver told us
1755 * that the TID(s) became empty before returning here from the
1756 * release function.
1757 * Either way, however, when the driver tells us that the TID(s)
1758 * became empty or we find that a txq became empty, we'll do the
1759 * TIM recalculation.
1760 */
1761
1762 if (!sta->sta.txq[0])
1763 return;
1764
1765 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1766 if (!sta->sta.txq[tid] ||
1767 !(driver_release_tids & BIT(tid)) ||
1768 txq_has_queue(sta->sta.txq[tid]))
1769 continue;
1770
1771 sta_info_recalc_tim(sta);
1772 break;
1773 }
1774 }
1775 }
1776
ieee80211_sta_ps_deliver_poll_response(struct sta_info * sta)1777 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1778 {
1779 u8 ignore_for_response = sta->sta.uapsd_queues;
1780
1781 /*
1782 * If all ACs are delivery-enabled then we should reply
1783 * from any of them, if only some are enabled we reply
1784 * only from the non-enabled ones.
1785 */
1786 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1787 ignore_for_response = 0;
1788
1789 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1790 IEEE80211_FRAME_RELEASE_PSPOLL);
1791 }
1792
ieee80211_sta_ps_deliver_uapsd(struct sta_info * sta)1793 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1794 {
1795 int n_frames = sta->sta.max_sp;
1796 u8 delivery_enabled = sta->sta.uapsd_queues;
1797
1798 /*
1799 * If we ever grow support for TSPEC this might happen if
1800 * the TSPEC update from hostapd comes in between a trigger
1801 * frame setting WLAN_STA_UAPSD in the RX path and this
1802 * actually getting called.
1803 */
1804 if (!delivery_enabled)
1805 return;
1806
1807 switch (sta->sta.max_sp) {
1808 case 1:
1809 n_frames = 2;
1810 break;
1811 case 2:
1812 n_frames = 4;
1813 break;
1814 case 3:
1815 n_frames = 6;
1816 break;
1817 case 0:
1818 /* XXX: what is a good value? */
1819 n_frames = 128;
1820 break;
1821 }
1822
1823 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1824 IEEE80211_FRAME_RELEASE_UAPSD);
1825 }
1826
ieee80211_sta_block_awake(struct ieee80211_hw * hw,struct ieee80211_sta * pubsta,bool block)1827 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1828 struct ieee80211_sta *pubsta, bool block)
1829 {
1830 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1831
1832 trace_api_sta_block_awake(sta->local, pubsta, block);
1833
1834 if (block) {
1835 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1836 ieee80211_clear_fast_xmit(sta);
1837 return;
1838 }
1839
1840 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1841 return;
1842
1843 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1844 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1845 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1846 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1847 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1848 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1849 /* must be asleep in this case */
1850 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1851 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1852 } else {
1853 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1854 ieee80211_check_fast_xmit(sta);
1855 }
1856 }
1857 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1858
ieee80211_sta_eosp(struct ieee80211_sta * pubsta)1859 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1860 {
1861 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1862 struct ieee80211_local *local = sta->local;
1863
1864 trace_api_eosp(local, pubsta);
1865
1866 clear_sta_flag(sta, WLAN_STA_SP);
1867 }
1868 EXPORT_SYMBOL(ieee80211_sta_eosp);
1869
ieee80211_send_eosp_nullfunc(struct ieee80211_sta * pubsta,int tid)1870 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1871 {
1872 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1873 enum ieee80211_frame_release_type reason;
1874 bool more_data;
1875
1876 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1877
1878 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1879 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1880 reason, 0);
1881
1882 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1883 }
1884 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1885
ieee80211_sta_set_buffered(struct ieee80211_sta * pubsta,u8 tid,bool buffered)1886 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1887 u8 tid, bool buffered)
1888 {
1889 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1890
1891 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1892 return;
1893
1894 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1895
1896 if (buffered)
1897 set_bit(tid, &sta->driver_buffered_tids);
1898 else
1899 clear_bit(tid, &sta->driver_buffered_tids);
1900
1901 sta_info_recalc_tim(sta);
1902 }
1903 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1904
ieee80211_register_airtime(struct ieee80211_txq * txq,u32 tx_airtime,u32 rx_airtime)1905 void ieee80211_register_airtime(struct ieee80211_txq *txq,
1906 u32 tx_airtime, u32 rx_airtime)
1907 {
1908 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
1909 struct ieee80211_local *local = sdata->local;
1910 u64 weight_sum, weight_sum_reciprocal;
1911 struct airtime_sched_info *air_sched;
1912 struct airtime_info *air_info;
1913 u32 airtime = 0;
1914
1915 air_sched = &local->airtime[txq->ac];
1916 air_info = to_airtime_info(txq);
1917
1918 if (local->airtime_flags & AIRTIME_USE_TX)
1919 airtime += tx_airtime;
1920 if (local->airtime_flags & AIRTIME_USE_RX)
1921 airtime += rx_airtime;
1922
1923 /* Weights scale so the unit weight is 256 */
1924 airtime <<= 8;
1925
1926 spin_lock_bh(&air_sched->lock);
1927
1928 air_info->tx_airtime += tx_airtime;
1929 air_info->rx_airtime += rx_airtime;
1930
1931 if (air_sched->weight_sum) {
1932 weight_sum = air_sched->weight_sum;
1933 weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
1934 } else {
1935 weight_sum = air_info->weight;
1936 weight_sum_reciprocal = air_info->weight_reciprocal;
1937 }
1938
1939 /* Round the calculation of global vt */
1940 air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
1941 weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
1942 air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
1943 air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
1944 ieee80211_resort_txq(&local->hw, txq);
1945
1946 spin_unlock_bh(&air_sched->lock);
1947 }
1948
ieee80211_sta_register_airtime(struct ieee80211_sta * pubsta,u8 tid,u32 tx_airtime,u32 rx_airtime)1949 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1950 u32 tx_airtime, u32 rx_airtime)
1951 {
1952 struct ieee80211_txq *txq = pubsta->txq[tid];
1953
1954 if (!txq)
1955 return;
1956
1957 ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
1958 }
1959 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1960
ieee80211_sta_update_pending_airtime(struct ieee80211_local * local,struct sta_info * sta,u8 ac,u16 tx_airtime,bool tx_completed)1961 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
1962 struct sta_info *sta, u8 ac,
1963 u16 tx_airtime, bool tx_completed)
1964 {
1965 int tx_pending;
1966
1967 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
1968 return;
1969
1970 if (!tx_completed) {
1971 if (sta)
1972 atomic_add(tx_airtime,
1973 &sta->airtime[ac].aql_tx_pending);
1974
1975 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
1976 return;
1977 }
1978
1979 if (sta) {
1980 tx_pending = atomic_sub_return(tx_airtime,
1981 &sta->airtime[ac].aql_tx_pending);
1982 if (tx_pending < 0)
1983 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
1984 tx_pending, 0);
1985 }
1986
1987 tx_pending = atomic_sub_return(tx_airtime,
1988 &local->aql_total_pending_airtime);
1989 if (WARN_ONCE(tx_pending < 0,
1990 "Device %s AC %d pending airtime underflow: %u, %u",
1991 wiphy_name(local->hw.wiphy), ac, tx_pending,
1992 tx_airtime))
1993 atomic_cmpxchg(&local->aql_total_pending_airtime,
1994 tx_pending, 0);
1995 }
1996
sta_info_move_state(struct sta_info * sta,enum ieee80211_sta_state new_state)1997 int sta_info_move_state(struct sta_info *sta,
1998 enum ieee80211_sta_state new_state)
1999 {
2000 might_sleep();
2001
2002 if (sta->sta_state == new_state)
2003 return 0;
2004
2005 /* check allowed transitions first */
2006
2007 switch (new_state) {
2008 case IEEE80211_STA_NONE:
2009 if (sta->sta_state != IEEE80211_STA_AUTH)
2010 return -EINVAL;
2011 break;
2012 case IEEE80211_STA_AUTH:
2013 if (sta->sta_state != IEEE80211_STA_NONE &&
2014 sta->sta_state != IEEE80211_STA_ASSOC)
2015 return -EINVAL;
2016 break;
2017 case IEEE80211_STA_ASSOC:
2018 if (sta->sta_state != IEEE80211_STA_AUTH &&
2019 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2020 return -EINVAL;
2021 break;
2022 case IEEE80211_STA_AUTHORIZED:
2023 if (sta->sta_state != IEEE80211_STA_ASSOC)
2024 return -EINVAL;
2025 break;
2026 default:
2027 WARN(1, "invalid state %d", new_state);
2028 return -EINVAL;
2029 }
2030
2031 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2032 sta->sta.addr, new_state);
2033
2034 /*
2035 * notify the driver before the actual changes so it can
2036 * fail the transition
2037 */
2038 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2039 int err = drv_sta_state(sta->local, sta->sdata, sta,
2040 sta->sta_state, new_state);
2041 if (err)
2042 return err;
2043 }
2044
2045 /* reflect the change in all state variables */
2046
2047 switch (new_state) {
2048 case IEEE80211_STA_NONE:
2049 if (sta->sta_state == IEEE80211_STA_AUTH)
2050 clear_bit(WLAN_STA_AUTH, &sta->_flags);
2051 break;
2052 case IEEE80211_STA_AUTH:
2053 if (sta->sta_state == IEEE80211_STA_NONE) {
2054 set_bit(WLAN_STA_AUTH, &sta->_flags);
2055 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2056 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2057 ieee80211_recalc_min_chandef(sta->sdata);
2058 if (!sta->sta.support_p2p_ps)
2059 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2060 }
2061 break;
2062 case IEEE80211_STA_ASSOC:
2063 if (sta->sta_state == IEEE80211_STA_AUTH) {
2064 set_bit(WLAN_STA_ASSOC, &sta->_flags);
2065 sta->assoc_at = ktime_get_boottime_ns();
2066 ieee80211_recalc_min_chandef(sta->sdata);
2067 if (!sta->sta.support_p2p_ps)
2068 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2069 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2070 ieee80211_vif_dec_num_mcast(sta->sdata);
2071 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2072 ieee80211_clear_fast_xmit(sta);
2073 ieee80211_clear_fast_rx(sta);
2074 }
2075 break;
2076 case IEEE80211_STA_AUTHORIZED:
2077 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2078 ieee80211_vif_inc_num_mcast(sta->sdata);
2079 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2080 ieee80211_check_fast_xmit(sta);
2081 ieee80211_check_fast_rx(sta);
2082 }
2083 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2084 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2085 cfg80211_send_layer2_update(sta->sdata->dev,
2086 sta->sta.addr);
2087 break;
2088 default:
2089 break;
2090 }
2091
2092 sta->sta_state = new_state;
2093
2094 return 0;
2095 }
2096
sta_info_tx_streams(struct sta_info * sta)2097 u8 sta_info_tx_streams(struct sta_info *sta)
2098 {
2099 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.deflink.ht_cap;
2100 u8 rx_streams;
2101
2102 if (!sta->sta.deflink.ht_cap.ht_supported)
2103 return 1;
2104
2105 if (sta->sta.deflink.vht_cap.vht_supported) {
2106 int i;
2107 u16 tx_mcs_map =
2108 le16_to_cpu(sta->sta.deflink.vht_cap.vht_mcs.tx_mcs_map);
2109
2110 for (i = 7; i >= 0; i--)
2111 if ((tx_mcs_map & (0x3 << (i * 2))) !=
2112 IEEE80211_VHT_MCS_NOT_SUPPORTED)
2113 return i + 1;
2114 }
2115
2116 if (ht_cap->mcs.rx_mask[3])
2117 rx_streams = 4;
2118 else if (ht_cap->mcs.rx_mask[2])
2119 rx_streams = 3;
2120 else if (ht_cap->mcs.rx_mask[1])
2121 rx_streams = 2;
2122 else
2123 rx_streams = 1;
2124
2125 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2126 return rx_streams;
2127
2128 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2129 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2130 }
2131
2132 static struct ieee80211_sta_rx_stats *
sta_get_last_rx_stats(struct sta_info * sta)2133 sta_get_last_rx_stats(struct sta_info *sta)
2134 {
2135 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2136 int cpu;
2137
2138 if (!sta->deflink.pcpu_rx_stats)
2139 return stats;
2140
2141 for_each_possible_cpu(cpu) {
2142 struct ieee80211_sta_rx_stats *cpustats;
2143
2144 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2145
2146 if (time_after(cpustats->last_rx, stats->last_rx))
2147 stats = cpustats;
2148 }
2149
2150 return stats;
2151 }
2152
sta_stats_decode_rate(struct ieee80211_local * local,u32 rate,struct rate_info * rinfo)2153 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2154 struct rate_info *rinfo)
2155 {
2156 rinfo->bw = STA_STATS_GET(BW, rate);
2157
2158 switch (STA_STATS_GET(TYPE, rate)) {
2159 case STA_STATS_RATE_TYPE_VHT:
2160 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2161 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2162 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2163 if (STA_STATS_GET(SGI, rate))
2164 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2165 break;
2166 case STA_STATS_RATE_TYPE_HT:
2167 rinfo->flags = RATE_INFO_FLAGS_MCS;
2168 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2169 if (STA_STATS_GET(SGI, rate))
2170 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2171 break;
2172 case STA_STATS_RATE_TYPE_LEGACY: {
2173 struct ieee80211_supported_band *sband;
2174 u16 brate;
2175 unsigned int shift;
2176 int band = STA_STATS_GET(LEGACY_BAND, rate);
2177 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2178
2179 sband = local->hw.wiphy->bands[band];
2180
2181 if (WARN_ON_ONCE(!sband->bitrates))
2182 break;
2183
2184 brate = sband->bitrates[rate_idx].bitrate;
2185 if (rinfo->bw == RATE_INFO_BW_5)
2186 shift = 2;
2187 else if (rinfo->bw == RATE_INFO_BW_10)
2188 shift = 1;
2189 else
2190 shift = 0;
2191 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2192 break;
2193 }
2194 case STA_STATS_RATE_TYPE_HE:
2195 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2196 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2197 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2198 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2199 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2200 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2201 break;
2202 }
2203 }
2204
sta_set_rate_info_rx(struct sta_info * sta,struct rate_info * rinfo)2205 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2206 {
2207 u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2208
2209 if (rate == STA_STATS_RATE_INVALID)
2210 return -EINVAL;
2211
2212 sta_stats_decode_rate(sta->local, rate, rinfo);
2213 return 0;
2214 }
2215
sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats * rxstats,int tid)2216 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2217 int tid)
2218 {
2219 unsigned int start;
2220 u64 value;
2221
2222 do {
2223 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2224 value = rxstats->msdu[tid];
2225 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2226
2227 return value;
2228 }
2229
sta_set_tidstats(struct sta_info * sta,struct cfg80211_tid_stats * tidstats,int tid)2230 static void sta_set_tidstats(struct sta_info *sta,
2231 struct cfg80211_tid_stats *tidstats,
2232 int tid)
2233 {
2234 struct ieee80211_local *local = sta->local;
2235 int cpu;
2236
2237 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2238 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2239 tid);
2240
2241 if (sta->deflink.pcpu_rx_stats) {
2242 for_each_possible_cpu(cpu) {
2243 struct ieee80211_sta_rx_stats *cpurxs;
2244
2245 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2246 cpu);
2247 tidstats->rx_msdu +=
2248 sta_get_tidstats_msdu(cpurxs, tid);
2249 }
2250 }
2251
2252 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2253 }
2254
2255 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2256 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2257 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2258 }
2259
2260 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2261 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2262 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2263 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2264 }
2265
2266 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2267 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2268 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2269 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2270 }
2271
2272 if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2273 spin_lock_bh(&local->fq.lock);
2274 rcu_read_lock();
2275
2276 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2277 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2278 to_txq_info(sta->sta.txq[tid]));
2279
2280 rcu_read_unlock();
2281 spin_unlock_bh(&local->fq.lock);
2282 }
2283 }
2284
sta_get_stats_bytes(struct ieee80211_sta_rx_stats * rxstats)2285 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2286 {
2287 unsigned int start;
2288 u64 value;
2289
2290 do {
2291 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2292 value = rxstats->bytes;
2293 } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2294
2295 return value;
2296 }
2297
sta_set_sinfo(struct sta_info * sta,struct station_info * sinfo,bool tidstats)2298 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2299 bool tidstats)
2300 {
2301 struct ieee80211_sub_if_data *sdata = sta->sdata;
2302 struct ieee80211_local *local = sdata->local;
2303 u32 thr = 0;
2304 int i, ac, cpu;
2305 struct ieee80211_sta_rx_stats *last_rxstats;
2306
2307 last_rxstats = sta_get_last_rx_stats(sta);
2308
2309 sinfo->generation = sdata->local->sta_generation;
2310
2311 /* do before driver, so beacon filtering drivers have a
2312 * chance to e.g. just add the number of filtered beacons
2313 * (or just modify the value entirely, of course)
2314 */
2315 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2316 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2317
2318 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2319 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2320 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2321 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2322 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2323 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2324 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2325
2326 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2327 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2328 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2329 }
2330
2331 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2332 sinfo->assoc_at = sta->assoc_at;
2333 sinfo->inactive_time =
2334 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2335
2336 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2337 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2338 sinfo->tx_bytes = 0;
2339 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2340 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2341 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2342 }
2343
2344 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2345 sinfo->tx_packets = 0;
2346 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2347 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2348 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2349 }
2350
2351 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2352 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2353 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2354
2355 if (sta->deflink.pcpu_rx_stats) {
2356 for_each_possible_cpu(cpu) {
2357 struct ieee80211_sta_rx_stats *cpurxs;
2358
2359 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2360 cpu);
2361 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2362 }
2363 }
2364
2365 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2366 }
2367
2368 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2369 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2370 if (sta->deflink.pcpu_rx_stats) {
2371 for_each_possible_cpu(cpu) {
2372 struct ieee80211_sta_rx_stats *cpurxs;
2373
2374 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2375 cpu);
2376 sinfo->rx_packets += cpurxs->packets;
2377 }
2378 }
2379 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2380 }
2381
2382 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2383 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2384 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2385 }
2386
2387 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2388 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2389 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2390 }
2391
2392 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2393 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2394 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2395 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2396 }
2397
2398 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2399 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2400 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2401 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2402 }
2403
2404 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2405 sinfo->airtime_weight = sta->airtime[0].weight;
2406 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2407 }
2408
2409 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2410 if (sta->deflink.pcpu_rx_stats) {
2411 for_each_possible_cpu(cpu) {
2412 struct ieee80211_sta_rx_stats *cpurxs;
2413
2414 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2415 sinfo->rx_dropped_misc += cpurxs->dropped;
2416 }
2417 }
2418
2419 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2420 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2421 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2422 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2423 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2424 }
2425
2426 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2427 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2428 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2429 sinfo->signal = (s8)last_rxstats->last_signal;
2430 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2431 }
2432
2433 if (!sta->deflink.pcpu_rx_stats &&
2434 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2435 sinfo->signal_avg =
2436 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2437 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2438 }
2439 }
2440
2441 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2442 * the sta->rx_stats struct, so the check here is fine with and without
2443 * pcpu statistics
2444 */
2445 if (last_rxstats->chains &&
2446 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2447 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2448 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2449 if (!sta->deflink.pcpu_rx_stats)
2450 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2451
2452 sinfo->chains = last_rxstats->chains;
2453
2454 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2455 sinfo->chain_signal[i] =
2456 last_rxstats->chain_signal_last[i];
2457 sinfo->chain_signal_avg[i] =
2458 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2459 }
2460 }
2461
2462 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2463 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2464 &sinfo->txrate);
2465 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2466 }
2467
2468 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2469 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2470 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2471 }
2472
2473 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2474 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2475 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2476 }
2477
2478 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2479 #ifdef CONFIG_MAC80211_MESH
2480 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2481 BIT_ULL(NL80211_STA_INFO_PLID) |
2482 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2483 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2484 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2485 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2486 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2487 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2488
2489 sinfo->llid = sta->mesh->llid;
2490 sinfo->plid = sta->mesh->plid;
2491 sinfo->plink_state = sta->mesh->plink_state;
2492 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2493 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2494 sinfo->t_offset = sta->mesh->t_offset;
2495 }
2496 sinfo->local_pm = sta->mesh->local_pm;
2497 sinfo->peer_pm = sta->mesh->peer_pm;
2498 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2499 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2500 sinfo->connected_to_as = sta->mesh->connected_to_as;
2501 #endif
2502 }
2503
2504 sinfo->bss_param.flags = 0;
2505 if (sdata->vif.bss_conf.use_cts_prot)
2506 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2507 if (sdata->vif.bss_conf.use_short_preamble)
2508 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2509 if (sdata->vif.bss_conf.use_short_slot)
2510 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2511 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2512 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2513
2514 sinfo->sta_flags.set = 0;
2515 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2516 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2517 BIT(NL80211_STA_FLAG_WME) |
2518 BIT(NL80211_STA_FLAG_MFP) |
2519 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2520 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2521 BIT(NL80211_STA_FLAG_TDLS_PEER);
2522 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2523 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2524 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2525 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2526 if (sta->sta.wme)
2527 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2528 if (test_sta_flag(sta, WLAN_STA_MFP))
2529 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2530 if (test_sta_flag(sta, WLAN_STA_AUTH))
2531 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2532 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2533 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2534 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2535 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2536
2537 thr = sta_get_expected_throughput(sta);
2538
2539 if (thr != 0) {
2540 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2541 sinfo->expected_throughput = thr;
2542 }
2543
2544 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2545 sta->deflink.status_stats.ack_signal_filled) {
2546 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2547 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2548 }
2549
2550 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2551 sta->deflink.status_stats.ack_signal_filled) {
2552 sinfo->avg_ack_signal =
2553 -(s8)ewma_avg_signal_read(
2554 &sta->deflink.status_stats.avg_ack_signal);
2555 sinfo->filled |=
2556 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2557 }
2558
2559 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2560 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2561 sinfo->airtime_link_metric =
2562 airtime_link_metric_get(local, sta);
2563 }
2564 }
2565
sta_get_expected_throughput(struct sta_info * sta)2566 u32 sta_get_expected_throughput(struct sta_info *sta)
2567 {
2568 struct ieee80211_sub_if_data *sdata = sta->sdata;
2569 struct ieee80211_local *local = sdata->local;
2570 struct rate_control_ref *ref = NULL;
2571 u32 thr = 0;
2572
2573 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2574 ref = local->rate_ctrl;
2575
2576 /* check if the driver has a SW RC implementation */
2577 if (ref && ref->ops->get_expected_throughput)
2578 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2579 else
2580 thr = drv_get_expected_throughput(local, sta);
2581
2582 return thr;
2583 }
2584
ieee80211_sta_last_active(struct sta_info * sta)2585 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2586 {
2587 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2588
2589 if (!sta->deflink.status_stats.last_ack ||
2590 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2591 return stats->last_rx;
2592 return sta->deflink.status_stats.last_ack;
2593 }
2594
sta_update_codel_params(struct sta_info * sta,u32 thr)2595 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2596 {
2597 if (!sta->sdata->local->ops->wake_tx_queue)
2598 return;
2599
2600 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2601 sta->cparams.target = MS2TIME(50);
2602 sta->cparams.interval = MS2TIME(300);
2603 sta->cparams.ecn = false;
2604 } else {
2605 sta->cparams.target = MS2TIME(20);
2606 sta->cparams.interval = MS2TIME(100);
2607 sta->cparams.ecn = true;
2608 }
2609 }
2610
ieee80211_sta_set_expected_throughput(struct ieee80211_sta * pubsta,u32 thr)2611 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2612 u32 thr)
2613 {
2614 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2615
2616 sta_update_codel_params(sta, thr);
2617 }
2618