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