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