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