xref: /openbmc/linux/net/mac80211/sta_info.c (revision 456bbb8a)
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-2023 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 	might_sleep();
1288 
1289 	if (sta->sta_state == new_state)
1290 		return 0;
1291 
1292 	/* check allowed transitions first */
1293 
1294 	switch (new_state) {
1295 	case IEEE80211_STA_NONE:
1296 		if (sta->sta_state != IEEE80211_STA_AUTH)
1297 			return -EINVAL;
1298 		break;
1299 	case IEEE80211_STA_AUTH:
1300 		if (sta->sta_state != IEEE80211_STA_NONE &&
1301 		    sta->sta_state != IEEE80211_STA_ASSOC)
1302 			return -EINVAL;
1303 		break;
1304 	case IEEE80211_STA_ASSOC:
1305 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1306 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1307 			return -EINVAL;
1308 		break;
1309 	case IEEE80211_STA_AUTHORIZED:
1310 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1311 			return -EINVAL;
1312 		break;
1313 	default:
1314 		WARN(1, "invalid state %d", new_state);
1315 		return -EINVAL;
1316 	}
1317 
1318 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1319 		sta->sta.addr, new_state);
1320 
1321 	/* notify the driver before the actual changes so it can
1322 	 * fail the transition
1323 	 */
1324 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1325 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1326 					sta->sta_state, new_state);
1327 		if (err)
1328 			return err;
1329 	}
1330 
1331 	/* reflect the change in all state variables */
1332 
1333 	switch (new_state) {
1334 	case IEEE80211_STA_NONE:
1335 		if (sta->sta_state == IEEE80211_STA_AUTH)
1336 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1337 		break;
1338 	case IEEE80211_STA_AUTH:
1339 		if (sta->sta_state == IEEE80211_STA_NONE) {
1340 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1341 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1342 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1343 			if (recalc) {
1344 				ieee80211_recalc_min_chandef(sta->sdata, -1);
1345 				if (!sta->sta.support_p2p_ps)
1346 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1347 			}
1348 		}
1349 		break;
1350 	case IEEE80211_STA_ASSOC:
1351 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1352 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1353 			sta->assoc_at = ktime_get_boottime_ns();
1354 			if (recalc) {
1355 				ieee80211_recalc_min_chandef(sta->sdata, -1);
1356 				if (!sta->sta.support_p2p_ps)
1357 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1358 			}
1359 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1360 			ieee80211_vif_dec_num_mcast(sta->sdata);
1361 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1362 			ieee80211_clear_fast_xmit(sta);
1363 			ieee80211_clear_fast_rx(sta);
1364 		}
1365 		break;
1366 	case IEEE80211_STA_AUTHORIZED:
1367 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
1368 			ieee80211_vif_inc_num_mcast(sta->sdata);
1369 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1370 			ieee80211_check_fast_xmit(sta);
1371 			ieee80211_check_fast_rx(sta);
1372 		}
1373 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1374 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
1375 			cfg80211_send_layer2_update(sta->sdata->dev,
1376 						    sta->sta.addr);
1377 		break;
1378 	default:
1379 		break;
1380 	}
1381 
1382 	sta->sta_state = new_state;
1383 
1384 	return 0;
1385 }
1386 
sta_info_move_state(struct sta_info * sta,enum ieee80211_sta_state new_state)1387 int sta_info_move_state(struct sta_info *sta,
1388 			enum ieee80211_sta_state new_state)
1389 {
1390 	return _sta_info_move_state(sta, new_state, true);
1391 }
1392 
__sta_info_destroy_part2(struct sta_info * sta,bool recalc)1393 static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1394 {
1395 	struct ieee80211_local *local = sta->local;
1396 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1397 	struct station_info *sinfo;
1398 	int ret;
1399 
1400 	/*
1401 	 * NOTE: This assumes at least synchronize_net() was done
1402 	 *	 after _part1 and before _part2!
1403 	 */
1404 
1405 	might_sleep();
1406 	lockdep_assert_held(&local->sta_mtx);
1407 
1408 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1409 		ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1410 		WARN_ON_ONCE(ret);
1411 	}
1412 
1413 	/* Flush queues before removing keys, as that might remove them
1414 	 * from hardware, and then depending on the offload method, any
1415 	 * frames sitting on hardware queues might be sent out without
1416 	 * any encryption at all.
1417 	 */
1418 	if (local->ops->set_key) {
1419 		if (local->ops->flush_sta)
1420 			drv_flush_sta(local, sta->sdata, sta);
1421 		else
1422 			ieee80211_flush_queues(local, sta->sdata, false);
1423 	}
1424 
1425 	/* now keys can no longer be reached */
1426 	ieee80211_free_sta_keys(local, sta);
1427 
1428 	/* disable TIM bit - last chance to tell driver */
1429 	__sta_info_recalc_tim(sta, true);
1430 
1431 	sta->dead = true;
1432 
1433 	local->num_sta--;
1434 	local->sta_generation++;
1435 
1436 	while (sta->sta_state > IEEE80211_STA_NONE) {
1437 		ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1438 		if (ret) {
1439 			WARN_ON_ONCE(1);
1440 			break;
1441 		}
1442 	}
1443 
1444 	if (sta->uploaded) {
1445 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1446 				    IEEE80211_STA_NOTEXIST);
1447 		WARN_ON_ONCE(ret != 0);
1448 	}
1449 
1450 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1451 
1452 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1453 	if (sinfo)
1454 		sta_set_sinfo(sta, sinfo, true);
1455 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1456 	kfree(sinfo);
1457 
1458 	ieee80211_sta_debugfs_remove(sta);
1459 
1460 	ieee80211_destroy_frag_cache(&sta->frags);
1461 
1462 	cleanup_single_sta(sta);
1463 }
1464 
__sta_info_destroy(struct sta_info * sta)1465 int __must_check __sta_info_destroy(struct sta_info *sta)
1466 {
1467 	int err = __sta_info_destroy_part1(sta);
1468 
1469 	if (err)
1470 		return err;
1471 
1472 	synchronize_net();
1473 
1474 	__sta_info_destroy_part2(sta, true);
1475 
1476 	return 0;
1477 }
1478 
sta_info_destroy_addr(struct ieee80211_sub_if_data * sdata,const u8 * addr)1479 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1480 {
1481 	struct sta_info *sta;
1482 	int ret;
1483 
1484 	mutex_lock(&sdata->local->sta_mtx);
1485 	sta = sta_info_get(sdata, addr);
1486 	ret = __sta_info_destroy(sta);
1487 	mutex_unlock(&sdata->local->sta_mtx);
1488 
1489 	return ret;
1490 }
1491 
sta_info_destroy_addr_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)1492 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1493 			      const u8 *addr)
1494 {
1495 	struct sta_info *sta;
1496 	int ret;
1497 
1498 	mutex_lock(&sdata->local->sta_mtx);
1499 	sta = sta_info_get_bss(sdata, addr);
1500 	ret = __sta_info_destroy(sta);
1501 	mutex_unlock(&sdata->local->sta_mtx);
1502 
1503 	return ret;
1504 }
1505 
sta_info_cleanup(struct timer_list * t)1506 static void sta_info_cleanup(struct timer_list *t)
1507 {
1508 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1509 	struct sta_info *sta;
1510 	bool timer_needed = false;
1511 
1512 	rcu_read_lock();
1513 	list_for_each_entry_rcu(sta, &local->sta_list, list)
1514 		if (sta_info_cleanup_expire_buffered(local, sta))
1515 			timer_needed = true;
1516 	rcu_read_unlock();
1517 
1518 	if (local->quiescing)
1519 		return;
1520 
1521 	if (!timer_needed)
1522 		return;
1523 
1524 	mod_timer(&local->sta_cleanup,
1525 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1526 }
1527 
sta_info_init(struct ieee80211_local * local)1528 int sta_info_init(struct ieee80211_local *local)
1529 {
1530 	int err;
1531 
1532 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
1533 	if (err)
1534 		return err;
1535 
1536 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1537 	if (err) {
1538 		rhltable_destroy(&local->sta_hash);
1539 		return err;
1540 	}
1541 
1542 	spin_lock_init(&local->tim_lock);
1543 	mutex_init(&local->sta_mtx);
1544 	INIT_LIST_HEAD(&local->sta_list);
1545 
1546 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1547 	return 0;
1548 }
1549 
sta_info_stop(struct ieee80211_local * local)1550 void sta_info_stop(struct ieee80211_local *local)
1551 {
1552 	del_timer_sync(&local->sta_cleanup);
1553 	rhltable_destroy(&local->sta_hash);
1554 	rhltable_destroy(&local->link_sta_hash);
1555 }
1556 
1557 
__sta_info_flush(struct ieee80211_sub_if_data * sdata,bool vlans)1558 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1559 {
1560 	struct ieee80211_local *local = sdata->local;
1561 	struct sta_info *sta, *tmp;
1562 	LIST_HEAD(free_list);
1563 	int ret = 0;
1564 
1565 	might_sleep();
1566 
1567 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1568 	WARN_ON(vlans && !sdata->bss);
1569 
1570 	mutex_lock(&local->sta_mtx);
1571 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1572 		if (sdata == sta->sdata ||
1573 		    (vlans && sdata->bss == sta->sdata->bss)) {
1574 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1575 				list_add(&sta->free_list, &free_list);
1576 			ret++;
1577 		}
1578 	}
1579 
1580 	if (!list_empty(&free_list)) {
1581 		bool support_p2p_ps = true;
1582 
1583 		synchronize_net();
1584 		list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
1585 			if (!sta->sta.support_p2p_ps)
1586 				support_p2p_ps = false;
1587 			__sta_info_destroy_part2(sta, false);
1588 		}
1589 
1590 		ieee80211_recalc_min_chandef(sdata, -1);
1591 		if (!support_p2p_ps)
1592 			ieee80211_recalc_p2p_go_ps_allowed(sdata);
1593 	}
1594 	mutex_unlock(&local->sta_mtx);
1595 
1596 	return ret;
1597 }
1598 
ieee80211_sta_expire(struct ieee80211_sub_if_data * sdata,unsigned long exp_time)1599 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1600 			  unsigned long exp_time)
1601 {
1602 	struct ieee80211_local *local = sdata->local;
1603 	struct sta_info *sta, *tmp;
1604 
1605 	mutex_lock(&local->sta_mtx);
1606 
1607 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1608 		unsigned long last_active = ieee80211_sta_last_active(sta);
1609 
1610 		if (sdata != sta->sdata)
1611 			continue;
1612 
1613 		if (time_is_before_jiffies(last_active + exp_time)) {
1614 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1615 				sta->sta.addr);
1616 
1617 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
1618 			    test_sta_flag(sta, WLAN_STA_PS_STA))
1619 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1620 
1621 			WARN_ON(__sta_info_destroy(sta));
1622 		}
1623 	}
1624 
1625 	mutex_unlock(&local->sta_mtx);
1626 }
1627 
ieee80211_find_sta_by_ifaddr(struct ieee80211_hw * hw,const u8 * addr,const u8 * localaddr)1628 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1629 						   const u8 *addr,
1630 						   const u8 *localaddr)
1631 {
1632 	struct ieee80211_local *local = hw_to_local(hw);
1633 	struct rhlist_head *tmp;
1634 	struct sta_info *sta;
1635 
1636 	/*
1637 	 * Just return a random station if localaddr is NULL
1638 	 * ... first in list.
1639 	 */
1640 	for_each_sta_info(local, addr, sta, tmp) {
1641 		if (localaddr &&
1642 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1643 			continue;
1644 		if (!sta->uploaded)
1645 			return NULL;
1646 		return &sta->sta;
1647 	}
1648 
1649 	return NULL;
1650 }
1651 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1652 
ieee80211_find_sta(struct ieee80211_vif * vif,const u8 * addr)1653 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1654 					 const u8 *addr)
1655 {
1656 	struct sta_info *sta;
1657 
1658 	if (!vif)
1659 		return NULL;
1660 
1661 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1662 	if (!sta)
1663 		return NULL;
1664 
1665 	if (!sta->uploaded)
1666 		return NULL;
1667 
1668 	return &sta->sta;
1669 }
1670 EXPORT_SYMBOL(ieee80211_find_sta);
1671 
1672 /* powersave support code */
ieee80211_sta_ps_deliver_wakeup(struct sta_info * sta)1673 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1674 {
1675 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1676 	struct ieee80211_local *local = sdata->local;
1677 	struct sk_buff_head pending;
1678 	int filtered = 0, buffered = 0, ac, i;
1679 	unsigned long flags;
1680 	struct ps_data *ps;
1681 
1682 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1683 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1684 				     u.ap);
1685 
1686 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1687 		ps = &sdata->bss->ps;
1688 	else if (ieee80211_vif_is_mesh(&sdata->vif))
1689 		ps = &sdata->u.mesh.ps;
1690 	else
1691 		return;
1692 
1693 	clear_sta_flag(sta, WLAN_STA_SP);
1694 
1695 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1696 	sta->driver_buffered_tids = 0;
1697 	sta->txq_buffered_tids = 0;
1698 
1699 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1700 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1701 
1702 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1703 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1704 			continue;
1705 
1706 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1707 	}
1708 
1709 	skb_queue_head_init(&pending);
1710 
1711 	/* sync with ieee80211_tx_h_unicast_ps_buf */
1712 	spin_lock_bh(&sta->ps_lock);
1713 	/* Send all buffered frames to the station */
1714 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1715 		int count = skb_queue_len(&pending), tmp;
1716 
1717 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1718 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1719 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1720 		tmp = skb_queue_len(&pending);
1721 		filtered += tmp - count;
1722 		count = tmp;
1723 
1724 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1725 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1726 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1727 		tmp = skb_queue_len(&pending);
1728 		buffered += tmp - count;
1729 	}
1730 
1731 	ieee80211_add_pending_skbs(local, &pending);
1732 
1733 	/* now we're no longer in the deliver code */
1734 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1735 
1736 	/* The station might have polled and then woken up before we responded,
1737 	 * so clear these flags now to avoid them sticking around.
1738 	 */
1739 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
1740 	clear_sta_flag(sta, WLAN_STA_UAPSD);
1741 	spin_unlock_bh(&sta->ps_lock);
1742 
1743 	atomic_dec(&ps->num_sta_ps);
1744 
1745 	local->total_ps_buffered -= buffered;
1746 
1747 	sta_info_recalc_tim(sta);
1748 
1749 	ps_dbg(sdata,
1750 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1751 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1752 
1753 	ieee80211_check_fast_xmit(sta);
1754 }
1755 
ieee80211_send_null_response(struct sta_info * sta,int tid,enum ieee80211_frame_release_type reason,bool call_driver,bool more_data)1756 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1757 					 enum ieee80211_frame_release_type reason,
1758 					 bool call_driver, bool more_data)
1759 {
1760 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1761 	struct ieee80211_local *local = sdata->local;
1762 	struct ieee80211_qos_hdr *nullfunc;
1763 	struct sk_buff *skb;
1764 	int size = sizeof(*nullfunc);
1765 	__le16 fc;
1766 	bool qos = sta->sta.wme;
1767 	struct ieee80211_tx_info *info;
1768 	struct ieee80211_chanctx_conf *chanctx_conf;
1769 
1770 	if (qos) {
1771 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1772 				 IEEE80211_STYPE_QOS_NULLFUNC |
1773 				 IEEE80211_FCTL_FROMDS);
1774 	} else {
1775 		size -= 2;
1776 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1777 				 IEEE80211_STYPE_NULLFUNC |
1778 				 IEEE80211_FCTL_FROMDS);
1779 	}
1780 
1781 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1782 	if (!skb)
1783 		return;
1784 
1785 	skb_reserve(skb, local->hw.extra_tx_headroom);
1786 
1787 	nullfunc = skb_put(skb, size);
1788 	nullfunc->frame_control = fc;
1789 	nullfunc->duration_id = 0;
1790 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1791 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1792 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1793 	nullfunc->seq_ctrl = 0;
1794 
1795 	skb->priority = tid;
1796 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1797 	if (qos) {
1798 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1799 
1800 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1801 			nullfunc->qos_ctrl |=
1802 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1803 			if (more_data)
1804 				nullfunc->frame_control |=
1805 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1806 		}
1807 	}
1808 
1809 	info = IEEE80211_SKB_CB(skb);
1810 
1811 	/*
1812 	 * Tell TX path to send this frame even though the
1813 	 * STA may still remain is PS mode after this frame
1814 	 * exchange. Also set EOSP to indicate this packet
1815 	 * ends the poll/service period.
1816 	 */
1817 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1818 		       IEEE80211_TX_STATUS_EOSP |
1819 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1820 
1821 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1822 
1823 	if (call_driver)
1824 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1825 					  reason, false);
1826 
1827 	skb->dev = sdata->dev;
1828 
1829 	rcu_read_lock();
1830 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1831 	if (WARN_ON(!chanctx_conf)) {
1832 		rcu_read_unlock();
1833 		kfree_skb(skb);
1834 		return;
1835 	}
1836 
1837 	info->band = chanctx_conf->def.chan->band;
1838 	ieee80211_xmit(sdata, sta, skb);
1839 	rcu_read_unlock();
1840 }
1841 
find_highest_prio_tid(unsigned long tids)1842 static int find_highest_prio_tid(unsigned long tids)
1843 {
1844 	/* lower 3 TIDs aren't ordered perfectly */
1845 	if (tids & 0xF8)
1846 		return fls(tids) - 1;
1847 	/* TID 0 is BE just like TID 3 */
1848 	if (tids & BIT(0))
1849 		return 0;
1850 	return fls(tids) - 1;
1851 }
1852 
1853 /* Indicates if the MORE_DATA bit should be set in the last
1854  * frame obtained by ieee80211_sta_ps_get_frames.
1855  * Note that driver_release_tids is relevant only if
1856  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1857  */
1858 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)1859 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1860 			   enum ieee80211_frame_release_type reason,
1861 			   unsigned long driver_release_tids)
1862 {
1863 	int ac;
1864 
1865 	/* If the driver has data on more than one TID then
1866 	 * certainly there's more data if we release just a
1867 	 * single frame now (from a single TID). This will
1868 	 * only happen for PS-Poll.
1869 	 */
1870 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1871 	    hweight16(driver_release_tids) > 1)
1872 		return true;
1873 
1874 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1875 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1876 			continue;
1877 
1878 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1879 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1880 			return true;
1881 	}
1882 
1883 	return false;
1884 }
1885 
1886 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)1887 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1888 			    enum ieee80211_frame_release_type reason,
1889 			    struct sk_buff_head *frames,
1890 			    unsigned long *driver_release_tids)
1891 {
1892 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1893 	struct ieee80211_local *local = sdata->local;
1894 	int ac;
1895 
1896 	/* Get response frame(s) and more data bit for the last one. */
1897 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1898 		unsigned long tids;
1899 
1900 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1901 			continue;
1902 
1903 		tids = ieee80211_tids_for_ac(ac);
1904 
1905 		/* if we already have frames from software, then we can't also
1906 		 * release from hardware queues
1907 		 */
1908 		if (skb_queue_empty(frames)) {
1909 			*driver_release_tids |=
1910 				sta->driver_buffered_tids & tids;
1911 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1912 		}
1913 
1914 		if (!*driver_release_tids) {
1915 			struct sk_buff *skb;
1916 
1917 			while (n_frames > 0) {
1918 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1919 				if (!skb) {
1920 					skb = skb_dequeue(
1921 						&sta->ps_tx_buf[ac]);
1922 					if (skb)
1923 						local->total_ps_buffered--;
1924 				}
1925 				if (!skb)
1926 					break;
1927 				n_frames--;
1928 				__skb_queue_tail(frames, skb);
1929 			}
1930 		}
1931 
1932 		/* If we have more frames buffered on this AC, then abort the
1933 		 * loop since we can't send more data from other ACs before
1934 		 * the buffered frames from this.
1935 		 */
1936 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1937 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1938 			break;
1939 	}
1940 }
1941 
1942 static void
ieee80211_sta_ps_deliver_response(struct sta_info * sta,int n_frames,u8 ignored_acs,enum ieee80211_frame_release_type reason)1943 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1944 				  int n_frames, u8 ignored_acs,
1945 				  enum ieee80211_frame_release_type reason)
1946 {
1947 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1948 	struct ieee80211_local *local = sdata->local;
1949 	unsigned long driver_release_tids = 0;
1950 	struct sk_buff_head frames;
1951 	bool more_data;
1952 
1953 	/* Service or PS-Poll period starts */
1954 	set_sta_flag(sta, WLAN_STA_SP);
1955 
1956 	__skb_queue_head_init(&frames);
1957 
1958 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1959 				    &frames, &driver_release_tids);
1960 
1961 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1962 
1963 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1964 		driver_release_tids =
1965 			BIT(find_highest_prio_tid(driver_release_tids));
1966 
1967 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1968 		int tid, ac;
1969 
1970 		/*
1971 		 * For PS-Poll, this can only happen due to a race condition
1972 		 * when we set the TIM bit and the station notices it, but
1973 		 * before it can poll for the frame we expire it.
1974 		 *
1975 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1976 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1977 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1978 		 *	more than the value specified in the Max SP Length field
1979 		 *	in the QoS Capability element from delivery-enabled ACs,
1980 		 *	that are destined for the non-AP STA.
1981 		 *
1982 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1983 		 */
1984 
1985 		/* This will evaluate to 1, 3, 5 or 7. */
1986 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1987 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1988 				break;
1989 		tid = 7 - 2 * ac;
1990 
1991 		ieee80211_send_null_response(sta, tid, reason, true, false);
1992 	} else if (!driver_release_tids) {
1993 		struct sk_buff_head pending;
1994 		struct sk_buff *skb;
1995 		int num = 0;
1996 		u16 tids = 0;
1997 		bool need_null = false;
1998 
1999 		skb_queue_head_init(&pending);
2000 
2001 		while ((skb = __skb_dequeue(&frames))) {
2002 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2003 			struct ieee80211_hdr *hdr = (void *) skb->data;
2004 			u8 *qoshdr = NULL;
2005 
2006 			num++;
2007 
2008 			/*
2009 			 * Tell TX path to send this frame even though the
2010 			 * STA may still remain is PS mode after this frame
2011 			 * exchange.
2012 			 */
2013 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
2014 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2015 
2016 			/*
2017 			 * Use MoreData flag to indicate whether there are
2018 			 * more buffered frames for this STA
2019 			 */
2020 			if (more_data || !skb_queue_empty(&frames))
2021 				hdr->frame_control |=
2022 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2023 			else
2024 				hdr->frame_control &=
2025 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2026 
2027 			if (ieee80211_is_data_qos(hdr->frame_control) ||
2028 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
2029 				qoshdr = ieee80211_get_qos_ctl(hdr);
2030 
2031 			tids |= BIT(skb->priority);
2032 
2033 			__skb_queue_tail(&pending, skb);
2034 
2035 			/* end service period after last frame or add one */
2036 			if (!skb_queue_empty(&frames))
2037 				continue;
2038 
2039 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2040 				/* for PS-Poll, there's only one frame */
2041 				info->flags |= IEEE80211_TX_STATUS_EOSP |
2042 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2043 				break;
2044 			}
2045 
2046 			/* For uAPSD, things are a bit more complicated. If the
2047 			 * last frame has a QoS header (i.e. is a QoS-data or
2048 			 * QoS-nulldata frame) then just set the EOSP bit there
2049 			 * and be done.
2050 			 * If the frame doesn't have a QoS header (which means
2051 			 * it should be a bufferable MMPDU) then we can't set
2052 			 * the EOSP bit in the QoS header; add a QoS-nulldata
2053 			 * frame to the list to send it after the MMPDU.
2054 			 *
2055 			 * Note that this code is only in the mac80211-release
2056 			 * code path, we assume that the driver will not buffer
2057 			 * anything but QoS-data frames, or if it does, will
2058 			 * create the QoS-nulldata frame by itself if needed.
2059 			 *
2060 			 * Cf. 802.11-2012 10.2.1.10 (c).
2061 			 */
2062 			if (qoshdr) {
2063 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
2064 
2065 				info->flags |= IEEE80211_TX_STATUS_EOSP |
2066 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2067 			} else {
2068 				/* The standard isn't completely clear on this
2069 				 * as it says the more-data bit should be set
2070 				 * if there are more BUs. The QoS-Null frame
2071 				 * we're about to send isn't buffered yet, we
2072 				 * only create it below, but let's pretend it
2073 				 * was buffered just in case some clients only
2074 				 * expect more-data=0 when eosp=1.
2075 				 */
2076 				hdr->frame_control |=
2077 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2078 				need_null = true;
2079 				num++;
2080 			}
2081 			break;
2082 		}
2083 
2084 		drv_allow_buffered_frames(local, sta, tids, num,
2085 					  reason, more_data);
2086 
2087 		ieee80211_add_pending_skbs(local, &pending);
2088 
2089 		if (need_null)
2090 			ieee80211_send_null_response(
2091 				sta, find_highest_prio_tid(tids),
2092 				reason, false, false);
2093 
2094 		sta_info_recalc_tim(sta);
2095 	} else {
2096 		int tid;
2097 
2098 		/*
2099 		 * We need to release a frame that is buffered somewhere in the
2100 		 * driver ... it'll have to handle that.
2101 		 * Note that the driver also has to check the number of frames
2102 		 * on the TIDs we're releasing from - if there are more than
2103 		 * n_frames it has to set the more-data bit (if we didn't ask
2104 		 * it to set it anyway due to other buffered frames); if there
2105 		 * are fewer than n_frames it has to make sure to adjust that
2106 		 * to allow the service period to end properly.
2107 		 */
2108 		drv_release_buffered_frames(local, sta, driver_release_tids,
2109 					    n_frames, reason, more_data);
2110 
2111 		/*
2112 		 * Note that we don't recalculate the TIM bit here as it would
2113 		 * most likely have no effect at all unless the driver told us
2114 		 * that the TID(s) became empty before returning here from the
2115 		 * release function.
2116 		 * Either way, however, when the driver tells us that the TID(s)
2117 		 * became empty or we find that a txq became empty, we'll do the
2118 		 * TIM recalculation.
2119 		 */
2120 
2121 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2122 			if (!sta->sta.txq[tid] ||
2123 			    !(driver_release_tids & BIT(tid)) ||
2124 			    txq_has_queue(sta->sta.txq[tid]))
2125 				continue;
2126 
2127 			sta_info_recalc_tim(sta);
2128 			break;
2129 		}
2130 	}
2131 }
2132 
ieee80211_sta_ps_deliver_poll_response(struct sta_info * sta)2133 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2134 {
2135 	u8 ignore_for_response = sta->sta.uapsd_queues;
2136 
2137 	/*
2138 	 * If all ACs are delivery-enabled then we should reply
2139 	 * from any of them, if only some are enabled we reply
2140 	 * only from the non-enabled ones.
2141 	 */
2142 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
2143 		ignore_for_response = 0;
2144 
2145 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
2146 					  IEEE80211_FRAME_RELEASE_PSPOLL);
2147 }
2148 
ieee80211_sta_ps_deliver_uapsd(struct sta_info * sta)2149 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2150 {
2151 	int n_frames = sta->sta.max_sp;
2152 	u8 delivery_enabled = sta->sta.uapsd_queues;
2153 
2154 	/*
2155 	 * If we ever grow support for TSPEC this might happen if
2156 	 * the TSPEC update from hostapd comes in between a trigger
2157 	 * frame setting WLAN_STA_UAPSD in the RX path and this
2158 	 * actually getting called.
2159 	 */
2160 	if (!delivery_enabled)
2161 		return;
2162 
2163 	switch (sta->sta.max_sp) {
2164 	case 1:
2165 		n_frames = 2;
2166 		break;
2167 	case 2:
2168 		n_frames = 4;
2169 		break;
2170 	case 3:
2171 		n_frames = 6;
2172 		break;
2173 	case 0:
2174 		/* XXX: what is a good value? */
2175 		n_frames = 128;
2176 		break;
2177 	}
2178 
2179 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2180 					  IEEE80211_FRAME_RELEASE_UAPSD);
2181 }
2182 
ieee80211_sta_block_awake(struct ieee80211_hw * hw,struct ieee80211_sta * pubsta,bool block)2183 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2184 			       struct ieee80211_sta *pubsta, bool block)
2185 {
2186 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2187 
2188 	trace_api_sta_block_awake(sta->local, pubsta, block);
2189 
2190 	if (block) {
2191 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2192 		ieee80211_clear_fast_xmit(sta);
2193 		return;
2194 	}
2195 
2196 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2197 		return;
2198 
2199 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2200 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2201 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2202 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2203 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2204 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
2205 		/* must be asleep in this case */
2206 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2207 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2208 	} else {
2209 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2210 		ieee80211_check_fast_xmit(sta);
2211 	}
2212 }
2213 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2214 
ieee80211_sta_eosp(struct ieee80211_sta * pubsta)2215 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2216 {
2217 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2218 	struct ieee80211_local *local = sta->local;
2219 
2220 	trace_api_eosp(local, pubsta);
2221 
2222 	clear_sta_flag(sta, WLAN_STA_SP);
2223 }
2224 EXPORT_SYMBOL(ieee80211_sta_eosp);
2225 
ieee80211_send_eosp_nullfunc(struct ieee80211_sta * pubsta,int tid)2226 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2227 {
2228 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2229 	enum ieee80211_frame_release_type reason;
2230 	bool more_data;
2231 
2232 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2233 
2234 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
2235 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2236 					       reason, 0);
2237 
2238 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
2239 }
2240 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2241 
ieee80211_sta_set_buffered(struct ieee80211_sta * pubsta,u8 tid,bool buffered)2242 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2243 				u8 tid, bool buffered)
2244 {
2245 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2246 
2247 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2248 		return;
2249 
2250 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2251 
2252 	if (buffered)
2253 		set_bit(tid, &sta->driver_buffered_tids);
2254 	else
2255 		clear_bit(tid, &sta->driver_buffered_tids);
2256 
2257 	sta_info_recalc_tim(sta);
2258 }
2259 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2260 
ieee80211_sta_register_airtime(struct ieee80211_sta * pubsta,u8 tid,u32 tx_airtime,u32 rx_airtime)2261 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2262 				    u32 tx_airtime, u32 rx_airtime)
2263 {
2264 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2265 	struct ieee80211_local *local = sta->sdata->local;
2266 	u8 ac = ieee80211_ac_from_tid(tid);
2267 	u32 airtime = 0;
2268 	u32 diff;
2269 
2270 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2271 		airtime += tx_airtime;
2272 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2273 		airtime += rx_airtime;
2274 
2275 	spin_lock_bh(&local->active_txq_lock[ac]);
2276 	sta->airtime[ac].tx_airtime += tx_airtime;
2277 	sta->airtime[ac].rx_airtime += rx_airtime;
2278 
2279 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2280 	if (diff <= AIRTIME_ACTIVE_DURATION)
2281 		sta->airtime[ac].deficit -= airtime;
2282 
2283 	spin_unlock_bh(&local->active_txq_lock[ac]);
2284 }
2285 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2286 
__ieee80211_sta_recalc_aggregates(struct sta_info * sta,u16 active_links)2287 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
2288 {
2289 	bool first = true;
2290 	int link_id;
2291 
2292 	if (!sta->sta.valid_links || !sta->sta.mlo) {
2293 		sta->sta.cur = &sta->sta.deflink.agg;
2294 		return;
2295 	}
2296 
2297 	rcu_read_lock();
2298 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
2299 		struct ieee80211_link_sta *link_sta;
2300 		int i;
2301 
2302 		if (!(active_links & BIT(link_id)))
2303 			continue;
2304 
2305 		link_sta = rcu_dereference(sta->sta.link[link_id]);
2306 		if (!link_sta)
2307 			continue;
2308 
2309 		if (first) {
2310 			sta->cur = sta->sta.deflink.agg;
2311 			first = false;
2312 			continue;
2313 		}
2314 
2315 		sta->cur.max_amsdu_len =
2316 			min(sta->cur.max_amsdu_len,
2317 			    link_sta->agg.max_amsdu_len);
2318 		sta->cur.max_rc_amsdu_len =
2319 			min(sta->cur.max_rc_amsdu_len,
2320 			    link_sta->agg.max_rc_amsdu_len);
2321 
2322 		for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2323 			sta->cur.max_tid_amsdu_len[i] =
2324 				min(sta->cur.max_tid_amsdu_len[i],
2325 				    link_sta->agg.max_tid_amsdu_len[i]);
2326 	}
2327 	rcu_read_unlock();
2328 
2329 	sta->sta.cur = &sta->cur;
2330 }
2331 
ieee80211_sta_recalc_aggregates(struct ieee80211_sta * pubsta)2332 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2333 {
2334 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2335 
2336 	__ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
2337 }
2338 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2339 
ieee80211_sta_update_pending_airtime(struct ieee80211_local * local,struct sta_info * sta,u8 ac,u16 tx_airtime,bool tx_completed)2340 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2341 					  struct sta_info *sta, u8 ac,
2342 					  u16 tx_airtime, bool tx_completed)
2343 {
2344 	int tx_pending;
2345 
2346 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2347 		return;
2348 
2349 	if (!tx_completed) {
2350 		if (sta)
2351 			atomic_add(tx_airtime,
2352 				   &sta->airtime[ac].aql_tx_pending);
2353 
2354 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2355 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2356 		return;
2357 	}
2358 
2359 	if (sta) {
2360 		tx_pending = atomic_sub_return(tx_airtime,
2361 					       &sta->airtime[ac].aql_tx_pending);
2362 		if (tx_pending < 0)
2363 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2364 				       tx_pending, 0);
2365 	}
2366 
2367 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2368 	tx_pending = atomic_sub_return(tx_airtime,
2369 				       &local->aql_ac_pending_airtime[ac]);
2370 	if (WARN_ONCE(tx_pending < 0,
2371 		      "Device %s AC %d pending airtime underflow: %u, %u",
2372 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
2373 		      tx_airtime)) {
2374 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2375 			       tx_pending, 0);
2376 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2377 	}
2378 }
2379 
2380 static struct ieee80211_sta_rx_stats *
sta_get_last_rx_stats(struct sta_info * sta)2381 sta_get_last_rx_stats(struct sta_info *sta)
2382 {
2383 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2384 	int cpu;
2385 
2386 	if (!sta->deflink.pcpu_rx_stats)
2387 		return stats;
2388 
2389 	for_each_possible_cpu(cpu) {
2390 		struct ieee80211_sta_rx_stats *cpustats;
2391 
2392 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2393 
2394 		if (time_after(cpustats->last_rx, stats->last_rx))
2395 			stats = cpustats;
2396 	}
2397 
2398 	return stats;
2399 }
2400 
sta_stats_decode_rate(struct ieee80211_local * local,u32 rate,struct rate_info * rinfo)2401 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2402 				  struct rate_info *rinfo)
2403 {
2404 	rinfo->bw = STA_STATS_GET(BW, rate);
2405 
2406 	switch (STA_STATS_GET(TYPE, rate)) {
2407 	case STA_STATS_RATE_TYPE_VHT:
2408 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2409 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2410 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2411 		if (STA_STATS_GET(SGI, rate))
2412 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2413 		break;
2414 	case STA_STATS_RATE_TYPE_HT:
2415 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2416 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2417 		if (STA_STATS_GET(SGI, rate))
2418 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2419 		break;
2420 	case STA_STATS_RATE_TYPE_LEGACY: {
2421 		struct ieee80211_supported_band *sband;
2422 		u16 brate;
2423 		unsigned int shift;
2424 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2425 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2426 
2427 		sband = local->hw.wiphy->bands[band];
2428 
2429 		if (WARN_ON_ONCE(!sband->bitrates))
2430 			break;
2431 
2432 		brate = sband->bitrates[rate_idx].bitrate;
2433 		if (rinfo->bw == RATE_INFO_BW_5)
2434 			shift = 2;
2435 		else if (rinfo->bw == RATE_INFO_BW_10)
2436 			shift = 1;
2437 		else
2438 			shift = 0;
2439 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2440 		break;
2441 		}
2442 	case STA_STATS_RATE_TYPE_HE:
2443 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2444 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2445 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2446 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2447 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2448 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2449 		break;
2450 	case STA_STATS_RATE_TYPE_EHT:
2451 		rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2452 		rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2453 		rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2454 		rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2455 		rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2456 		break;
2457 	}
2458 }
2459 
sta_set_rate_info_rx(struct sta_info * sta,struct rate_info * rinfo)2460 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2461 {
2462 	u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2463 
2464 	if (rate == STA_STATS_RATE_INVALID)
2465 		return -EINVAL;
2466 
2467 	sta_stats_decode_rate(sta->local, rate, rinfo);
2468 	return 0;
2469 }
2470 
sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats * rxstats,int tid)2471 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2472 					int tid)
2473 {
2474 	unsigned int start;
2475 	u64 value;
2476 
2477 	do {
2478 		start = u64_stats_fetch_begin(&rxstats->syncp);
2479 		value = rxstats->msdu[tid];
2480 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2481 
2482 	return value;
2483 }
2484 
sta_set_tidstats(struct sta_info * sta,struct cfg80211_tid_stats * tidstats,int tid)2485 static void sta_set_tidstats(struct sta_info *sta,
2486 			     struct cfg80211_tid_stats *tidstats,
2487 			     int tid)
2488 {
2489 	struct ieee80211_local *local = sta->local;
2490 	int cpu;
2491 
2492 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2493 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2494 							   tid);
2495 
2496 		if (sta->deflink.pcpu_rx_stats) {
2497 			for_each_possible_cpu(cpu) {
2498 				struct ieee80211_sta_rx_stats *cpurxs;
2499 
2500 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2501 						     cpu);
2502 				tidstats->rx_msdu +=
2503 					sta_get_tidstats_msdu(cpurxs, tid);
2504 			}
2505 		}
2506 
2507 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2508 	}
2509 
2510 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2511 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2512 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2513 	}
2514 
2515 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2516 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2517 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2518 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2519 	}
2520 
2521 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2522 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2523 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2524 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2525 	}
2526 
2527 	if (tid < IEEE80211_NUM_TIDS) {
2528 		spin_lock_bh(&local->fq.lock);
2529 		rcu_read_lock();
2530 
2531 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2532 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
2533 					 to_txq_info(sta->sta.txq[tid]));
2534 
2535 		rcu_read_unlock();
2536 		spin_unlock_bh(&local->fq.lock);
2537 	}
2538 }
2539 
sta_get_stats_bytes(struct ieee80211_sta_rx_stats * rxstats)2540 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2541 {
2542 	unsigned int start;
2543 	u64 value;
2544 
2545 	do {
2546 		start = u64_stats_fetch_begin(&rxstats->syncp);
2547 		value = rxstats->bytes;
2548 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2549 
2550 	return value;
2551 }
2552 
sta_set_sinfo(struct sta_info * sta,struct station_info * sinfo,bool tidstats)2553 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2554 		   bool tidstats)
2555 {
2556 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2557 	struct ieee80211_local *local = sdata->local;
2558 	u32 thr = 0;
2559 	int i, ac, cpu;
2560 	struct ieee80211_sta_rx_stats *last_rxstats;
2561 
2562 	last_rxstats = sta_get_last_rx_stats(sta);
2563 
2564 	sinfo->generation = sdata->local->sta_generation;
2565 
2566 	/* do before driver, so beacon filtering drivers have a
2567 	 * chance to e.g. just add the number of filtered beacons
2568 	 * (or just modify the value entirely, of course)
2569 	 */
2570 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2571 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2572 
2573 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2574 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2575 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2576 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2577 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2578 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2579 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2580 
2581 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2582 		sinfo->beacon_loss_count =
2583 			sdata->deflink.u.mgd.beacon_loss_count;
2584 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2585 	}
2586 
2587 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2588 	sinfo->assoc_at = sta->assoc_at;
2589 	sinfo->inactive_time =
2590 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2591 
2592 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2593 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2594 		sinfo->tx_bytes = 0;
2595 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2596 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2597 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2598 	}
2599 
2600 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2601 		sinfo->tx_packets = 0;
2602 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2603 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2604 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2605 	}
2606 
2607 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2608 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2609 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2610 
2611 		if (sta->deflink.pcpu_rx_stats) {
2612 			for_each_possible_cpu(cpu) {
2613 				struct ieee80211_sta_rx_stats *cpurxs;
2614 
2615 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2616 						     cpu);
2617 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2618 			}
2619 		}
2620 
2621 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2622 	}
2623 
2624 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2625 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2626 		if (sta->deflink.pcpu_rx_stats) {
2627 			for_each_possible_cpu(cpu) {
2628 				struct ieee80211_sta_rx_stats *cpurxs;
2629 
2630 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2631 						     cpu);
2632 				sinfo->rx_packets += cpurxs->packets;
2633 			}
2634 		}
2635 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2636 	}
2637 
2638 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2639 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2640 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2641 	}
2642 
2643 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2644 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2645 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2646 	}
2647 
2648 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2649 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2650 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2651 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2652 	}
2653 
2654 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2655 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2656 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2657 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2658 	}
2659 
2660 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2661 		sinfo->airtime_weight = sta->airtime_weight;
2662 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2663 	}
2664 
2665 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2666 	if (sta->deflink.pcpu_rx_stats) {
2667 		for_each_possible_cpu(cpu) {
2668 			struct ieee80211_sta_rx_stats *cpurxs;
2669 
2670 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2671 			sinfo->rx_dropped_misc += cpurxs->dropped;
2672 		}
2673 	}
2674 
2675 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2676 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2677 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2678 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2679 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2680 	}
2681 
2682 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2683 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2684 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2685 			sinfo->signal = (s8)last_rxstats->last_signal;
2686 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2687 		}
2688 
2689 		if (!sta->deflink.pcpu_rx_stats &&
2690 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2691 			sinfo->signal_avg =
2692 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2693 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2694 		}
2695 	}
2696 
2697 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2698 	 * the sta->rx_stats struct, so the check here is fine with and without
2699 	 * pcpu statistics
2700 	 */
2701 	if (last_rxstats->chains &&
2702 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2703 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2704 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2705 		if (!sta->deflink.pcpu_rx_stats)
2706 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2707 
2708 		sinfo->chains = last_rxstats->chains;
2709 
2710 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2711 			sinfo->chain_signal[i] =
2712 				last_rxstats->chain_signal_last[i];
2713 			sinfo->chain_signal_avg[i] =
2714 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2715 		}
2716 	}
2717 
2718 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2719 	    !sta->sta.valid_links) {
2720 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2721 				     &sinfo->txrate);
2722 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2723 	}
2724 
2725 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2726 	    !sta->sta.valid_links) {
2727 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2728 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2729 	}
2730 
2731 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2732 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2733 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
2734 	}
2735 
2736 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2737 #ifdef CONFIG_MAC80211_MESH
2738 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2739 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2740 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2741 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2742 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2743 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2744 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2745 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2746 
2747 		sinfo->llid = sta->mesh->llid;
2748 		sinfo->plid = sta->mesh->plid;
2749 		sinfo->plink_state = sta->mesh->plink_state;
2750 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2751 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2752 			sinfo->t_offset = sta->mesh->t_offset;
2753 		}
2754 		sinfo->local_pm = sta->mesh->local_pm;
2755 		sinfo->peer_pm = sta->mesh->peer_pm;
2756 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2757 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2758 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2759 #endif
2760 	}
2761 
2762 	sinfo->bss_param.flags = 0;
2763 	if (sdata->vif.bss_conf.use_cts_prot)
2764 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2765 	if (sdata->vif.bss_conf.use_short_preamble)
2766 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2767 	if (sdata->vif.bss_conf.use_short_slot)
2768 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2769 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2770 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2771 
2772 	sinfo->sta_flags.set = 0;
2773 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2774 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2775 				BIT(NL80211_STA_FLAG_WME) |
2776 				BIT(NL80211_STA_FLAG_MFP) |
2777 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2778 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2779 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2780 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2781 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2782 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2783 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2784 	if (sta->sta.wme)
2785 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2786 	if (test_sta_flag(sta, WLAN_STA_MFP))
2787 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2788 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2789 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2790 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2791 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2792 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2793 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2794 
2795 	thr = sta_get_expected_throughput(sta);
2796 
2797 	if (thr != 0) {
2798 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2799 		sinfo->expected_throughput = thr;
2800 	}
2801 
2802 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2803 	    sta->deflink.status_stats.ack_signal_filled) {
2804 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2805 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2806 	}
2807 
2808 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2809 	    sta->deflink.status_stats.ack_signal_filled) {
2810 		sinfo->avg_ack_signal =
2811 			-(s8)ewma_avg_signal_read(
2812 				&sta->deflink.status_stats.avg_ack_signal);
2813 		sinfo->filled |=
2814 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2815 	}
2816 
2817 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2818 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2819 		sinfo->airtime_link_metric =
2820 			airtime_link_metric_get(local, sta);
2821 	}
2822 }
2823 
sta_get_expected_throughput(struct sta_info * sta)2824 u32 sta_get_expected_throughput(struct sta_info *sta)
2825 {
2826 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2827 	struct ieee80211_local *local = sdata->local;
2828 	struct rate_control_ref *ref = NULL;
2829 	u32 thr = 0;
2830 
2831 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2832 		ref = local->rate_ctrl;
2833 
2834 	/* check if the driver has a SW RC implementation */
2835 	if (ref && ref->ops->get_expected_throughput)
2836 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2837 	else
2838 		thr = drv_get_expected_throughput(local, sta);
2839 
2840 	return thr;
2841 }
2842 
ieee80211_sta_last_active(struct sta_info * sta)2843 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2844 {
2845 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2846 
2847 	if (!sta->deflink.status_stats.last_ack ||
2848 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2849 		return stats->last_rx;
2850 	return sta->deflink.status_stats.last_ack;
2851 }
2852 
sta_update_codel_params(struct sta_info * sta,u32 thr)2853 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2854 {
2855 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2856 		sta->cparams.target = MS2TIME(50);
2857 		sta->cparams.interval = MS2TIME(300);
2858 		sta->cparams.ecn = false;
2859 	} else {
2860 		sta->cparams.target = MS2TIME(20);
2861 		sta->cparams.interval = MS2TIME(100);
2862 		sta->cparams.ecn = true;
2863 	}
2864 }
2865 
ieee80211_sta_set_expected_throughput(struct ieee80211_sta * pubsta,u32 thr)2866 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2867 					   u32 thr)
2868 {
2869 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2870 
2871 	sta_update_codel_params(sta, thr);
2872 }
2873 
ieee80211_sta_allocate_link(struct sta_info * sta,unsigned int link_id)2874 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2875 {
2876 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2877 	struct sta_link_alloc *alloc;
2878 	int ret;
2879 
2880 	lockdep_assert_held(&sdata->local->sta_mtx);
2881 
2882 	/* must represent an MLD from the start */
2883 	if (WARN_ON(!sta->sta.valid_links))
2884 		return -EINVAL;
2885 
2886 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2887 		    sta->link[link_id]))
2888 		return -EBUSY;
2889 
2890 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2891 	if (!alloc)
2892 		return -ENOMEM;
2893 
2894 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2895 	if (ret) {
2896 		kfree(alloc);
2897 		return ret;
2898 	}
2899 
2900 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2901 
2902 	ieee80211_link_sta_debugfs_add(&alloc->info);
2903 
2904 	return 0;
2905 }
2906 
ieee80211_sta_free_link(struct sta_info * sta,unsigned int link_id)2907 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2908 {
2909 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
2910 
2911 	sta_remove_link(sta, link_id, false);
2912 }
2913 
ieee80211_sta_activate_link(struct sta_info * sta,unsigned int link_id)2914 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2915 {
2916 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2917 	struct link_sta_info *link_sta;
2918 	u16 old_links = sta->sta.valid_links;
2919 	u16 new_links = old_links | BIT(link_id);
2920 	int ret;
2921 
2922 	link_sta = rcu_dereference_protected(sta->link[link_id],
2923 					     lockdep_is_held(&sdata->local->sta_mtx));
2924 
2925 	if (WARN_ON(old_links == new_links || !link_sta))
2926 		return -EINVAL;
2927 
2928 	rcu_read_lock();
2929 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2930 		rcu_read_unlock();
2931 		return -EALREADY;
2932 	}
2933 	/* we only modify under the mutex so this is fine */
2934 	rcu_read_unlock();
2935 
2936 	sta->sta.valid_links = new_links;
2937 
2938 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2939 		goto hash;
2940 
2941 	ieee80211_recalc_min_chandef(sdata, link_id);
2942 
2943 	/* Ensure the values are updated for the driver,
2944 	 * redone by sta_remove_link on failure.
2945 	 */
2946 	ieee80211_sta_recalc_aggregates(&sta->sta);
2947 
2948 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2949 				   old_links, new_links);
2950 	if (ret) {
2951 		sta->sta.valid_links = old_links;
2952 		sta_remove_link(sta, link_id, false);
2953 		return ret;
2954 	}
2955 
2956 hash:
2957 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2958 	WARN_ON(ret);
2959 	return 0;
2960 }
2961 
ieee80211_sta_remove_link(struct sta_info * sta,unsigned int link_id)2962 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2963 {
2964 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2965 	u16 old_links = sta->sta.valid_links;
2966 
2967 	lockdep_assert_held(&sdata->local->sta_mtx);
2968 
2969 	sta->sta.valid_links &= ~BIT(link_id);
2970 
2971 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2972 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2973 				     old_links, sta->sta.valid_links);
2974 
2975 	sta_remove_link(sta, link_id, true);
2976 }
2977 
ieee80211_sta_set_max_amsdu_subframes(struct sta_info * sta,const u8 * ext_capab,unsigned int ext_capab_len)2978 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2979 					   const u8 *ext_capab,
2980 					   unsigned int ext_capab_len)
2981 {
2982 	u8 val;
2983 
2984 	sta->sta.max_amsdu_subframes = 0;
2985 
2986 	if (ext_capab_len < 8)
2987 		return;
2988 
2989 	/* The sender might not have sent the last bit, consider it to be 0 */
2990 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2991 
2992 	/* we did get all the bits, take the MSB as well */
2993 	if (ext_capab_len >= 9)
2994 		val |= u8_get_bits(ext_capab[8],
2995 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2996 
2997 	if (val)
2998 		sta->sta.max_amsdu_subframes = 4 << (4 - val);
2999 }
3000 
3001 #ifdef CONFIG_LOCKDEP
lockdep_sta_mutex_held(struct ieee80211_sta * pubsta)3002 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
3003 {
3004 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3005 
3006 	return lockdep_is_held(&sta->local->sta_mtx);
3007 }
3008 EXPORT_SYMBOL(lockdep_sta_mutex_held);
3009 #endif
3010