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