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