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