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