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