xref: /openbmc/linux/net/mac80211/sta_info.c (revision 6c7c4cbfd5f59c04a40af67ad72d14e19215ef36)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4f0706e82SJiri Benc  *
5f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
6f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
7f0706e82SJiri Benc  * published by the Free Software Foundation.
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21f0706e82SJiri Benc #include <net/mac80211.h>
22f0706e82SJiri Benc #include "ieee80211_i.h"
2324487981SJohannes Berg #include "driver-ops.h"
242c8dccc7SJohannes Berg #include "rate.h"
25f0706e82SJiri Benc #include "sta_info.h"
26e9f207f0SJiri Benc #include "debugfs_sta.h"
27ee385855SLuis Carlos Cobo #include "mesh.h"
28ce662b44SJohannes Berg #include "wme.h"
29f0706e82SJiri Benc 
30d0709a65SJohannes Berg /**
31d0709a65SJohannes Berg  * DOC: STA information lifetime rules
32d0709a65SJohannes Berg  *
33d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
34d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
35d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
36d0709a65SJohannes Berg  *
3734e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3834e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3934e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4034e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4134e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4234e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4334e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4434e89507SJohannes Berg  * encryption keys.
4593e5deb1SJohannes Berg  *
4693e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4793e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
48d0709a65SJohannes Berg  *
4934e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
507e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
517e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5225985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5334e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5425985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5534e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
567e189a12SLuis R. Rodriguez  *
5734e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5834e89507SJohannes Berg  * calls are available.
59d0709a65SJohannes Berg  *
6034e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6134e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6234e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6334e89507SJohannes Berg  * freed before they are done using it.
64d0709a65SJohannes Berg  */
65f0706e82SJiri Benc 
664d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
67be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
68f0706e82SJiri Benc 			     struct sta_info *sta)
69f0706e82SJiri Benc {
70f0706e82SJiri Benc 	struct sta_info *s;
71f0706e82SJiri Benc 
7240b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
734d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
74f0706e82SJiri Benc 	if (!s)
75be8755e1SMichael Wu 		return -ENOENT;
76be8755e1SMichael Wu 	if (s == sta) {
77cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
78d0709a65SJohannes Berg 				   s->hnext);
79be8755e1SMichael Wu 		return 0;
80f0706e82SJiri Benc 	}
81f0706e82SJiri Benc 
8240b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8340b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8440b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
854d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8640b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
87cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
88be8755e1SMichael Wu 		return 0;
89f0706e82SJiri Benc 	}
90f0706e82SJiri Benc 
91be8755e1SMichael Wu 	return -ENOENT;
92f0706e82SJiri Benc }
93f0706e82SJiri Benc 
9497f97b1fSJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
95b22cfcfcSEliad Peller {
96b22cfcfcSEliad Peller 	int ac, i;
97b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
98b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
99b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
100d012a605SMarco Porsch 	struct ps_data *ps;
101b22cfcfcSEliad Peller 
102b22cfcfcSEliad Peller 	/*
103b22cfcfcSEliad Peller 	 * At this point, when being called as call_rcu callback,
104b22cfcfcSEliad Peller 	 * neither mac80211 nor the driver can reference this
105b22cfcfcSEliad Peller 	 * sta struct any more except by still existing timers
106b22cfcfcSEliad Peller 	 * associated with this station that we clean up below.
107051007d9SJohannes Berg 	 *
108051007d9SJohannes Berg 	 * Note though that this still uses the sdata and even
109051007d9SJohannes Berg 	 * calls the driver in AP and mesh mode, so interfaces
110051007d9SJohannes Berg 	 * of those types mush use call sta_info_flush_cleanup()
111051007d9SJohannes Berg 	 * (typically via sta_info_flush()) before deconfiguring
112051007d9SJohannes Berg 	 * the driver.
113051007d9SJohannes Berg 	 *
114051007d9SJohannes Berg 	 * In station mode, nothing happens here so it doesn't
115051007d9SJohannes Berg 	 * have to (and doesn't) do that, this is intentional to
116051007d9SJohannes Berg 	 * speed up roaming.
117b22cfcfcSEliad Peller 	 */
118b22cfcfcSEliad Peller 
119b22cfcfcSEliad Peller 	if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
120d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
121d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
122d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1233f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1243f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
125d012a605SMarco Porsch 		else
126d012a605SMarco Porsch 			return;
127b22cfcfcSEliad Peller 
128b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
129b22cfcfcSEliad Peller 
130d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
131b22cfcfcSEliad Peller 		sta_info_recalc_tim(sta);
132b22cfcfcSEliad Peller 	}
133b22cfcfcSEliad Peller 
134b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
135b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1361f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1371f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
138b22cfcfcSEliad Peller 	}
139b22cfcfcSEliad Peller 
14045b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
14145b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
142b22cfcfcSEliad Peller 
143b22cfcfcSEliad Peller 	cancel_work_sync(&sta->drv_unblock_wk);
144b22cfcfcSEliad Peller 
145b22cfcfcSEliad Peller 	/*
146b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
147b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
148b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
149b22cfcfcSEliad Peller 	 * directly by station destruction.
150b22cfcfcSEliad Peller 	 */
1515a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
152661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
153b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
154b22cfcfcSEliad Peller 		if (!tid_tx)
155b22cfcfcSEliad Peller 			continue;
1561f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
157b22cfcfcSEliad Peller 		kfree(tid_tx);
158b22cfcfcSEliad Peller 	}
159b22cfcfcSEliad Peller 
160b22cfcfcSEliad Peller 	sta_info_free(local, sta);
161b22cfcfcSEliad Peller }
162b22cfcfcSEliad Peller 
16397f97b1fSJohannes Berg void ieee80211_cleanup_sdata_stas(struct ieee80211_sub_if_data *sdata)
16497f97b1fSJohannes Berg {
16597f97b1fSJohannes Berg 	struct sta_info *sta;
16697f97b1fSJohannes Berg 
16797f97b1fSJohannes Berg 	spin_lock_bh(&sdata->cleanup_stations_lock);
16897f97b1fSJohannes Berg 	while (!list_empty(&sdata->cleanup_stations)) {
16997f97b1fSJohannes Berg 		sta = list_first_entry(&sdata->cleanup_stations,
17097f97b1fSJohannes Berg 				       struct sta_info, list);
17197f97b1fSJohannes Berg 		list_del(&sta->list);
17297f97b1fSJohannes Berg 		spin_unlock_bh(&sdata->cleanup_stations_lock);
17397f97b1fSJohannes Berg 
17497f97b1fSJohannes Berg 		cleanup_single_sta(sta);
17597f97b1fSJohannes Berg 
17697f97b1fSJohannes Berg 		spin_lock_bh(&sdata->cleanup_stations_lock);
17797f97b1fSJohannes Berg 	}
17897f97b1fSJohannes Berg 
17997f97b1fSJohannes Berg 	spin_unlock_bh(&sdata->cleanup_stations_lock);
18097f97b1fSJohannes Berg }
18197f97b1fSJohannes Berg 
182b22cfcfcSEliad Peller static void free_sta_rcu(struct rcu_head *h)
183b22cfcfcSEliad Peller {
184b22cfcfcSEliad Peller 	struct sta_info *sta = container_of(h, struct sta_info, rcu_head);
18597f97b1fSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
186b22cfcfcSEliad Peller 
18797f97b1fSJohannes Berg 	spin_lock(&sdata->cleanup_stations_lock);
18897f97b1fSJohannes Berg 	list_add_tail(&sta->list, &sdata->cleanup_stations);
18997f97b1fSJohannes Berg 	spin_unlock(&sdata->cleanup_stations_lock);
19097f97b1fSJohannes Berg 
19197f97b1fSJohannes Berg 	ieee80211_queue_work(&sdata->local->hw, &sdata->cleanup_stations_wk);
192b22cfcfcSEliad Peller }
193b22cfcfcSEliad Peller 
194d0709a65SJohannes Berg /* protected by RCU */
195abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
196abe60632SJohannes Berg 			      const u8 *addr)
19743ba7e95SJohannes Berg {
198abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
19943ba7e95SJohannes Berg 	struct sta_info *sta;
20043ba7e95SJohannes Berg 
2010379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
2020379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
20343ba7e95SJohannes Berg 	while (sta) {
204abe60632SJohannes Berg 		if (sta->sdata == sdata &&
205b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
20643ba7e95SJohannes Berg 			break;
2070379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
2080379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
20943ba7e95SJohannes Berg 	}
21043ba7e95SJohannes Berg 	return sta;
21143ba7e95SJohannes Berg }
21243ba7e95SJohannes Berg 
2130e5ded5aSFelix Fietkau /*
2140e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
2150e5ded5aSFelix Fietkau  * or from one of its vlans
2160e5ded5aSFelix Fietkau  */
2170e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
2180e5ded5aSFelix Fietkau 				  const u8 *addr)
2190e5ded5aSFelix Fietkau {
2200e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
2210e5ded5aSFelix Fietkau 	struct sta_info *sta;
2220e5ded5aSFelix Fietkau 
2230379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
2240379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
2250e5ded5aSFelix Fietkau 	while (sta) {
2260e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
227a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
228b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
2290e5ded5aSFelix Fietkau 			break;
2300379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
2310379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
2320e5ded5aSFelix Fietkau 	}
2330e5ded5aSFelix Fietkau 	return sta;
2340e5ded5aSFelix Fietkau }
2350e5ded5aSFelix Fietkau 
2363b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2373b53fde8SJohannes Berg 				     int idx)
238ee385855SLuis Carlos Cobo {
2393b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
240ee385855SLuis Carlos Cobo 	struct sta_info *sta;
241ee385855SLuis Carlos Cobo 	int i = 0;
242ee385855SLuis Carlos Cobo 
243d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2443b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2452a8ca29aSLuis Carlos Cobo 			continue;
246ee385855SLuis Carlos Cobo 		if (i < idx) {
247ee385855SLuis Carlos Cobo 			++i;
248ee385855SLuis Carlos Cobo 			continue;
249ee385855SLuis Carlos Cobo 		}
2502a8ca29aSLuis Carlos Cobo 		return sta;
251ee385855SLuis Carlos Cobo 	}
252ee385855SLuis Carlos Cobo 
253ee385855SLuis Carlos Cobo 	return NULL;
254ee385855SLuis Carlos Cobo }
255f0706e82SJiri Benc 
25693e5deb1SJohannes Berg /**
257d9a7ddb0SJohannes Berg  * sta_info_free - free STA
25893e5deb1SJohannes Berg  *
2596ef307bcSRandy Dunlap  * @local: pointer to the global information
26093e5deb1SJohannes Berg  * @sta: STA info to free
26193e5deb1SJohannes Berg  *
26293e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
263d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
264d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
265d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
26693e5deb1SJohannes Berg  */
267d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
26893e5deb1SJohannes Berg {
269889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2704b7679a5SJohannes Berg 		rate_control_free_sta(sta);
27193e5deb1SJohannes Berg 
272bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
27393e5deb1SJohannes Berg 
27493e5deb1SJohannes Berg 	kfree(sta);
27593e5deb1SJohannes Berg }
27693e5deb1SJohannes Berg 
2774d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
278d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
279d0709a65SJohannes Berg 			      struct sta_info *sta)
280f0706e82SJiri Benc {
2814d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
28217741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
283cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
284f0706e82SJiri Benc }
285f0706e82SJiri Benc 
286af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
287af818581SJohannes Berg {
288af818581SJohannes Berg 	struct sta_info *sta;
289af818581SJohannes Berg 
290af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
291af818581SJohannes Berg 
292af818581SJohannes Berg 	if (sta->dead)
293af818581SJohannes Berg 		return;
294af818581SJohannes Berg 
29554420473SHelmut Schaa 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
29654420473SHelmut Schaa 		local_bh_disable();
297af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
29854420473SHelmut Schaa 		local_bh_enable();
29954420473SHelmut Schaa 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
300c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
301ce662b44SJohannes Berg 
302ce662b44SJohannes Berg 		local_bh_disable();
303af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
304ce662b44SJohannes Berg 		local_bh_enable();
305c2c98fdeSJohannes Berg 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
306c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
307ce662b44SJohannes Berg 
308ce662b44SJohannes Berg 		local_bh_disable();
30947086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
310ce662b44SJohannes Berg 		local_bh_enable();
31150a9432dSJohannes Berg 	} else
312c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
313af818581SJohannes Berg }
314af818581SJohannes Berg 
315af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
316af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
317af65cd96SJohannes Berg {
318af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
319af65cd96SJohannes Berg 		return 0;
320af65cd96SJohannes Berg 
321889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
322af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
323af65cd96SJohannes Berg 						     &sta->sta, gfp);
324889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
325af65cd96SJohannes Berg 		return -ENOMEM;
326af65cd96SJohannes Berg 
327af65cd96SJohannes Berg 	return 0;
328af65cd96SJohannes Berg }
329af65cd96SJohannes Berg 
33073651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
33156544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
332f0706e82SJiri Benc {
333d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
334f0706e82SJiri Benc 	struct sta_info *sta;
335ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
33616c5f15cSRon Rindjunsky 	int i;
337f0706e82SJiri Benc 
33817741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
339f0706e82SJiri Benc 	if (!sta)
34073651ee6SJohannes Berg 		return NULL;
341f0706e82SJiri Benc 
34207346f81SJohannes Berg 	spin_lock_init(&sta->lock);
343af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
34467c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
345a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
34687f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
34787f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
34887f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
34987f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
350*6c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
35187f59c70SThomas Pedersen #endif
35207346f81SJohannes Berg 
35317741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
354d0709a65SJohannes Berg 	sta->local = local;
355d0709a65SJohannes Berg 	sta->sdata = sdata;
3568bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
357f0706e82SJiri Benc 
35871ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35971ec375cSJohannes Berg 
360ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
361ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
362541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
363ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
364ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
365541a45a1SBruno Randolf 
366af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
367f0706e82SJiri Benc 		kfree(sta);
36873651ee6SJohannes Berg 		return NULL;
369f0706e82SJiri Benc 	}
370f0706e82SJiri Benc 
3715a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
372a622ab72SJohannes Berg 		/*
373a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
374a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
375a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
376a622ab72SJohannes Berg 		 */
37716c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37816c5f15cSRon Rindjunsky 	}
379948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
380948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
381948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
382948d887dSJohannes Berg 	}
38373651ee6SJohannes Berg 
3845a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3854be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
386cccaec98SSenthil Balasubramanian 
387af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
388af0ed69bSJohannes Berg 
389bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
39073651ee6SJohannes Berg 
39173651ee6SJohannes Berg 	return sta;
39273651ee6SJohannes Berg }
39373651ee6SJohannes Berg 
3948c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
39534e89507SJohannes Berg {
39634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
39734e89507SJohannes Berg 
39803e4497eSJohannes Berg 	/*
39903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
40003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
40103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
40203e4497eSJohannes Berg 	 */
4038c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4048c71df7aSGuy Eilam 		return -ENETDOWN;
40503e4497eSJohannes Berg 
406b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4078c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4088c71df7aSGuy Eilam 		return -EINVAL;
4098c71df7aSGuy Eilam 
4108c71df7aSGuy Eilam 	return 0;
41193e5deb1SJohannes Berg }
41244213b5eSJohannes Berg 
413f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
414f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
415f09603a2SJohannes Berg 				     struct sta_info *sta)
416f09603a2SJohannes Berg {
417f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
418f09603a2SJohannes Berg 	int err = 0;
419f09603a2SJohannes Berg 
420f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
421f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
422f09603a2SJohannes Berg 		if (err)
423f09603a2SJohannes Berg 			break;
424f09603a2SJohannes Berg 	}
425f09603a2SJohannes Berg 
426f09603a2SJohannes Berg 	if (!err) {
427a4ec45a4SJohannes Berg 		/*
428a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
429a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
430a4ec45a4SJohannes Berg 		 */
431a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
432f09603a2SJohannes Berg 			sta->uploaded = true;
433f09603a2SJohannes Berg 		return 0;
434f09603a2SJohannes Berg 	}
435f09603a2SJohannes Berg 
436f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
437bdcbd8e0SJohannes Berg 		sdata_info(sdata,
438bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
439bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
440f09603a2SJohannes Berg 		err = 0;
441f09603a2SJohannes Berg 	}
442f09603a2SJohannes Berg 
443f09603a2SJohannes Berg 	/* unwind on error */
444f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
445f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
446f09603a2SJohannes Berg 
447f09603a2SJohannes Berg 	return err;
448f09603a2SJohannes Berg }
449f09603a2SJohannes Berg 
45034e89507SJohannes Berg /*
4518c71df7aSGuy Eilam  * should be called with sta_mtx locked
4528c71df7aSGuy Eilam  * this function replaces the mutex lock
4538c71df7aSGuy Eilam  * with a RCU lock
4548c71df7aSGuy Eilam  */
4554d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4568c71df7aSGuy Eilam {
4578c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4588c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4597852e361SJohannes Berg 	struct station_info sinfo;
4608c71df7aSGuy Eilam 	int err = 0;
4618c71df7aSGuy Eilam 
4628c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4638c71df7aSGuy Eilam 
4647852e361SJohannes Berg 	/* check if STA exists already */
4657852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4664d33960bSJohannes Berg 		err = -EEXIST;
4674d33960bSJohannes Berg 		goto out_err;
46834e89507SJohannes Berg 	}
46934e89507SJohannes Berg 
4704d33960bSJohannes Berg 	/* notify driver */
471f09603a2SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
472f09603a2SJohannes Berg 	if (err)
473f09603a2SJohannes Berg 		goto out_err;
4744d33960bSJohannes Berg 
4754d33960bSJohannes Berg 	local->num_sta++;
4764d33960bSJohannes Berg 	local->sta_generation++;
4774d33960bSJohannes Berg 	smp_mb();
4784d33960bSJohannes Berg 
4794d33960bSJohannes Berg 	/* make the station visible */
4804d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
4814d33960bSJohannes Berg 
482794454ceSArik Nemtsov 	list_add_rcu(&sta->list, &local->sta_list);
48383d5cc01SJohannes Berg 
48483d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
4854d33960bSJohannes Berg 
4864d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
4874d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
4884d33960bSJohannes Berg 
4894d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
4904d33960bSJohannes Berg 	sinfo.filled = 0;
4914d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
4924d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
493d0709a65SJohannes Berg 
494bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
495f0706e82SJiri Benc 
49634e89507SJohannes Berg 	/* move reference to rcu-protected */
49734e89507SJohannes Berg 	rcu_read_lock();
49834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
499e9f207f0SJiri Benc 
50073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
50173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
50273651ee6SJohannes Berg 
50373651ee6SJohannes Berg 	return 0;
5044d33960bSJohannes Berg  out_err:
5054d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5064d33960bSJohannes Berg 	rcu_read_lock();
5074d33960bSJohannes Berg 	return err;
5088c71df7aSGuy Eilam }
5098c71df7aSGuy Eilam 
5108c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5118c71df7aSGuy Eilam {
5128c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5138c71df7aSGuy Eilam 	int err = 0;
5148c71df7aSGuy Eilam 
5154d33960bSJohannes Berg 	might_sleep();
5164d33960bSJohannes Berg 
5178c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5188c71df7aSGuy Eilam 	if (err) {
5198c71df7aSGuy Eilam 		rcu_read_lock();
5208c71df7aSGuy Eilam 		goto out_free;
5218c71df7aSGuy Eilam 	}
5228c71df7aSGuy Eilam 
523004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
524004c872eSJohannes Berg 
5254d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5268c71df7aSGuy Eilam 	if (err)
527004c872eSJohannes Berg 		goto out_free;
528d0709a65SJohannes Berg 
529004c872eSJohannes Berg 	return 0;
53093e5deb1SJohannes Berg  out_free:
53193e5deb1SJohannes Berg 	BUG_ON(!err);
532d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
53393e5deb1SJohannes Berg 	return err;
534f0706e82SJiri Benc }
535f0706e82SJiri Benc 
53634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
53734e89507SJohannes Berg {
53834e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
53934e89507SJohannes Berg 
54034e89507SJohannes Berg 	rcu_read_unlock();
54134e89507SJohannes Berg 
54234e89507SJohannes Berg 	return err;
54334e89507SJohannes Berg }
54434e89507SJohannes Berg 
545d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
546004c872eSJohannes Berg {
547004c872eSJohannes Berg 	/*
548004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
549004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
550004c872eSJohannes Berg 	 */
551d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
552004c872eSJohannes Berg }
553004c872eSJohannes Berg 
554d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
555004c872eSJohannes Berg {
556004c872eSJohannes Berg 	/*
557004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
558004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
559004c872eSJohannes Berg 	 */
560d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
561004c872eSJohannes Berg }
562004c872eSJohannes Berg 
5633d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5643d5839b6SIlan Peer {
5653d5839b6SIlan Peer 	/*
5663d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
5673d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
5683d5839b6SIlan Peer 	 */
5693d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
5703d5839b6SIlan Peer }
5713d5839b6SIlan Peer 
572948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
573004c872eSJohannes Berg {
574948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
575948d887dSJohannes Berg 	switch (ac) {
576948d887dSJohannes Berg 	case IEEE80211_AC_VO:
577948d887dSJohannes Berg 		return BIT(6) | BIT(7);
578948d887dSJohannes Berg 	case IEEE80211_AC_VI:
579948d887dSJohannes Berg 		return BIT(4) | BIT(5);
580948d887dSJohannes Berg 	case IEEE80211_AC_BE:
581948d887dSJohannes Berg 		return BIT(0) | BIT(3);
582948d887dSJohannes Berg 	case IEEE80211_AC_BK:
583948d887dSJohannes Berg 		return BIT(1) | BIT(2);
584948d887dSJohannes Berg 	default:
585948d887dSJohannes Berg 		WARN_ON(1);
586948d887dSJohannes Berg 		return 0;
587d0709a65SJohannes Berg 	}
588004c872eSJohannes Berg }
589004c872eSJohannes Berg 
590c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
591004c872eSJohannes Berg {
592c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
593d012a605SMarco Porsch 	struct ps_data *ps;
594948d887dSJohannes Berg 	bool indicate_tim = false;
595948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
596948d887dSJohannes Berg 	int ac;
597d012a605SMarco Porsch 	u16 id;
598004c872eSJohannes Berg 
599d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
600d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
601c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
602c868cb35SJohannes Berg 			return;
6033e122be0SJohannes Berg 
604d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
605d012a605SMarco Porsch 		id = sta->sta.aid;
6063f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6073f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6083f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6093f52b7e3SMarco Porsch 		/* TIM map only for PLID <= IEEE80211_MAX_AID */
6103f52b7e3SMarco Porsch 		id = le16_to_cpu(sta->plid) % IEEE80211_MAX_AID;
6113f52b7e3SMarco Porsch #endif
612d012a605SMarco Porsch 	} else {
613d012a605SMarco Porsch 		return;
614d012a605SMarco Porsch 	}
615d012a605SMarco Porsch 
616c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
617c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
618c868cb35SJohannes Berg 		return;
619004c872eSJohannes Berg 
620c868cb35SJohannes Berg 	if (sta->dead)
621c868cb35SJohannes Berg 		goto done;
6223e122be0SJohannes Berg 
623948d887dSJohannes Berg 	/*
624948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
625948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
626948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
627948d887dSJohannes Berg 	 * non-enabled ones.
628948d887dSJohannes Berg 	 */
629948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
630948d887dSJohannes Berg 		ignore_for_tim = 0;
631948d887dSJohannes Berg 
632948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
633948d887dSJohannes Berg 		unsigned long tids;
634948d887dSJohannes Berg 
635948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
636948d887dSJohannes Berg 			continue;
637948d887dSJohannes Berg 
638948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
639948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
640948d887dSJohannes Berg 		if (indicate_tim)
641948d887dSJohannes Berg 			break;
642948d887dSJohannes Berg 
643948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
644948d887dSJohannes Berg 
645948d887dSJohannes Berg 		indicate_tim |=
646948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
647004c872eSJohannes Berg 	}
648004c872eSJohannes Berg 
649c868cb35SJohannes Berg  done:
65065f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
651004c872eSJohannes Berg 
6523d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6533d5839b6SIlan Peer 		goto out_unlock;
6543d5839b6SIlan Peer 
655948d887dSJohannes Berg 	if (indicate_tim)
656d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
657c868cb35SJohannes Berg 	else
658d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6593e122be0SJohannes Berg 
660c868cb35SJohannes Berg 	if (local->ops->set_tim) {
661c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
662948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
663c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
664004c872eSJohannes Berg 	}
665004c872eSJohannes Berg 
6663d5839b6SIlan Peer out_unlock:
66765f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
668004c872eSJohannes Berg }
669004c872eSJohannes Berg 
670cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
671f0706e82SJiri Benc {
672e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
673f0706e82SJiri Benc 	int timeout;
674f0706e82SJiri Benc 
675f0706e82SJiri Benc 	if (!skb)
676cd0b8d89SJohannes Berg 		return false;
677f0706e82SJiri Benc 
678e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
679f0706e82SJiri Benc 
680f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
68157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
68257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
68357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
684f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
685f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
686e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
687f0706e82SJiri Benc }
688f0706e82SJiri Benc 
689f0706e82SJiri Benc 
690948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
691948d887dSJohannes Berg 						struct sta_info *sta, int ac)
692f0706e82SJiri Benc {
693f0706e82SJiri Benc 	unsigned long flags;
694f0706e82SJiri Benc 	struct sk_buff *skb;
695f0706e82SJiri Benc 
69660750397SJohannes Berg 	/*
69760750397SJohannes Berg 	 * First check for frames that should expire on the filtered
69860750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
69960750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
70060750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
70160750397SJohannes Berg 	 * total_ps_buffered counter.
70260750397SJohannes Berg 	 */
703f0706e82SJiri Benc 	for (;;) {
704948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
705948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
70657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
707948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
708836341a7SJohannes Berg 		else
709f0706e82SJiri Benc 			skb = NULL;
710948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
711f0706e82SJiri Benc 
71260750397SJohannes Berg 		/*
71360750397SJohannes Berg 		 * Frames are queued in order, so if this one
71460750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
71560750397SJohannes Berg 		 * we actually reached the end of the queue we
71660750397SJohannes Berg 		 * also need to stop, of course.
71760750397SJohannes Berg 		 */
71860750397SJohannes Berg 		if (!skb)
71960750397SJohannes Berg 			break;
720d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
72160750397SJohannes Berg 	}
72260750397SJohannes Berg 
72360750397SJohannes Berg 	/*
72460750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
72560750397SJohannes Berg 	 * only find something if the filtered queue was emptied
72660750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
72760750397SJohannes Berg 	 * buffered frames.
72860750397SJohannes Berg 	 */
729f0706e82SJiri Benc 	for (;;) {
730948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
731948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
732f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
733948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
734f0706e82SJiri Benc 		else
735f0706e82SJiri Benc 			skb = NULL;
736948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
737f0706e82SJiri Benc 
73860750397SJohannes Berg 		/*
73960750397SJohannes Berg 		 * frames are queued in order, so if this one
74060750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
74160750397SJohannes Berg 		 * the queue) we can stop testing
74260750397SJohannes Berg 		 */
743836341a7SJohannes Berg 		if (!skb)
744836341a7SJohannes Berg 			break;
745836341a7SJohannes Berg 
746f0706e82SJiri Benc 		local->total_ps_buffered--;
747bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
748bdcbd8e0SJohannes Berg 		       sta->sta.addr);
749d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
750f0706e82SJiri Benc 	}
7513393a608SJuuso Oikarinen 
75260750397SJohannes Berg 	/*
75360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
75460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
75560750397SJohannes Berg 	 * frames.
75660750397SJohannes Berg 	 */
75760750397SJohannes Berg 	sta_info_recalc_tim(sta);
75860750397SJohannes Berg 
75960750397SJohannes Berg 	/*
76060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
76160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
76260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
76360750397SJohannes Berg 	 */
764948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
765948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
766948d887dSJohannes Berg }
767948d887dSJohannes Berg 
768948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
769948d887dSJohannes Berg 					     struct sta_info *sta)
770948d887dSJohannes Berg {
771948d887dSJohannes Berg 	bool have_buffered = false;
772948d887dSJohannes Berg 	int ac;
773948d887dSJohannes Berg 
7743f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
7753f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
7763f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
777948d887dSJohannes Berg 		return false;
778948d887dSJohannes Berg 
779948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
780948d887dSJohannes Berg 		have_buffered |=
781948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
782948d887dSJohannes Berg 
783948d887dSJohannes Berg 	return have_buffered;
784f0706e82SJiri Benc }
785f0706e82SJiri Benc 
78683d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
78734e89507SJohannes Berg {
78834e89507SJohannes Berg 	struct ieee80211_local *local;
78934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
7906d10e46bSJohannes Berg 	int ret;
79134e89507SJohannes Berg 
79234e89507SJohannes Berg 	might_sleep();
79334e89507SJohannes Berg 
79434e89507SJohannes Berg 	if (!sta)
79534e89507SJohannes Berg 		return -ENOENT;
79634e89507SJohannes Berg 
79734e89507SJohannes Berg 	local = sta->local;
79834e89507SJohannes Berg 	sdata = sta->sdata;
79934e89507SJohannes Berg 
80083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
80183d5cc01SJohannes Berg 
802098a6070SJohannes Berg 	/*
803098a6070SJohannes Berg 	 * Before removing the station from the driver and
804098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
805098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
806098a6070SJohannes Berg 	 * will be sufficient.
807098a6070SJohannes Berg 	 */
808c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
809c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
810098a6070SJohannes Berg 
81134e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
81234e89507SJohannes Berg 	if (ret)
81334e89507SJohannes Berg 		return ret;
81434e89507SJohannes Berg 
815794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8164d33960bSJohannes Berg 
8176d10e46bSJohannes Berg 	/* this always calls synchronize_net() */
8186d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
81934e89507SJohannes Berg 
82034e89507SJohannes Berg 	sta->dead = true;
82134e89507SJohannes Berg 
82234e89507SJohannes Berg 	local->num_sta--;
82334e89507SJohannes Berg 	local->sta_generation++;
82434e89507SJohannes Berg 
82534e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
826a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
82734e89507SJohannes Berg 
82883d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
829f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
830f09603a2SJohannes Berg 		if (ret) {
83183d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
83283d5cc01SJohannes Berg 			break;
83383d5cc01SJohannes Berg 		}
83483d5cc01SJohannes Berg 	}
835d9a7ddb0SJohannes Berg 
836f09603a2SJohannes Berg 	if (sta->uploaded) {
837f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
838f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
839f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
840f09603a2SJohannes Berg 	}
84134e89507SJohannes Berg 
842bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
843bdcbd8e0SJohannes Berg 
844ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
845ec15e68bSJouni Malinen 
84634e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
84734e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
84834e89507SJohannes Berg 
849b22cfcfcSEliad Peller 	call_rcu(&sta->rcu_head, free_sta_rcu);
85034e89507SJohannes Berg 
85134e89507SJohannes Berg 	return 0;
85234e89507SJohannes Berg }
85334e89507SJohannes Berg 
85434e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
85534e89507SJohannes Berg {
85634e89507SJohannes Berg 	struct sta_info *sta;
85734e89507SJohannes Berg 	int ret;
85834e89507SJohannes Berg 
85934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8607852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
86134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
86234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
86334e89507SJohannes Berg 
86434e89507SJohannes Berg 	return ret;
86534e89507SJohannes Berg }
86634e89507SJohannes Berg 
86734e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
86834e89507SJohannes Berg 			      const u8 *addr)
86934e89507SJohannes Berg {
87034e89507SJohannes Berg 	struct sta_info *sta;
87134e89507SJohannes Berg 	int ret;
87234e89507SJohannes Berg 
87334e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8747852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
87534e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
87634e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
87734e89507SJohannes Berg 
87834e89507SJohannes Berg 	return ret;
87934e89507SJohannes Berg }
880f0706e82SJiri Benc 
881f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
882f0706e82SJiri Benc {
883f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
884f0706e82SJiri Benc 	struct sta_info *sta;
8853393a608SJuuso Oikarinen 	bool timer_needed = false;
886f0706e82SJiri Benc 
887d0709a65SJohannes Berg 	rcu_read_lock();
888d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
8893393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
8903393a608SJuuso Oikarinen 			timer_needed = true;
891d0709a65SJohannes Berg 	rcu_read_unlock();
892f0706e82SJiri Benc 
8935bb644a0SJohannes Berg 	if (local->quiescing)
8945bb644a0SJohannes Berg 		return;
8955bb644a0SJohannes Berg 
8963393a608SJuuso Oikarinen 	if (!timer_needed)
8973393a608SJuuso Oikarinen 		return;
8983393a608SJuuso Oikarinen 
89926d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
90026d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
901f0706e82SJiri Benc }
902f0706e82SJiri Benc 
903f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
904f0706e82SJiri Benc {
9054d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
90634e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
907f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
908f0706e82SJiri Benc 
909b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
910b24b8a24SPavel Emelyanov 		    (unsigned long)local);
911f0706e82SJiri Benc }
912f0706e82SJiri Benc 
913f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
914f0706e82SJiri Benc {
915a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
916f0706e82SJiri Benc }
917f0706e82SJiri Benc 
918051007d9SJohannes Berg 
919051007d9SJohannes Berg int sta_info_flush_defer(struct ieee80211_sub_if_data *sdata)
920f0706e82SJiri Benc {
921b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
922f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
92344213b5eSJohannes Berg 	int ret = 0;
924f0706e82SJiri Benc 
925d0709a65SJohannes Berg 	might_sleep();
926d0709a65SJohannes Berg 
92734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
92834e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
929b998e8bbSJohannes Berg 		if (sdata == sta->sdata) {
93034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
93134316837SJohannes Berg 			ret++;
93234316837SJohannes Berg 		}
93334e89507SJohannes Berg 	}
93434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
93544213b5eSJohannes Berg 
936051007d9SJohannes Berg 	return ret;
937051007d9SJohannes Berg }
938051007d9SJohannes Berg 
939051007d9SJohannes Berg void sta_info_flush_cleanup(struct ieee80211_sub_if_data *sdata)
940051007d9SJohannes Berg {
94197f97b1fSJohannes Berg 	ieee80211_cleanup_sdata_stas(sdata);
94297f97b1fSJohannes Berg 	cancel_work_sync(&sdata->cleanup_stations_wk);
943f0706e82SJiri Benc }
944dc6676b7SJohannes Berg 
94524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
94624723d1bSJohannes Berg 			  unsigned long exp_time)
94724723d1bSJohannes Berg {
94824723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
94924723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
95024723d1bSJohannes Berg 
95134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
952e46a2cf9SMohammed Shafi Shajakhan 
953e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
954ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
955ec2b774eSMarek Lindner 			continue;
956ec2b774eSMarek Lindner 
95724723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
958eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
959bdcbd8e0SJohannes Berg 				sta->sta.addr);
9603f52b7e3SMarco Porsch 
9613f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
9623f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
9633f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
9643f52b7e3SMarco Porsch 
96534e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
96624723d1bSJohannes Berg 		}
967e46a2cf9SMohammed Shafi Shajakhan 	}
968e46a2cf9SMohammed Shafi Shajakhan 
96934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
97024723d1bSJohannes Berg }
97117741cdcSJohannes Berg 
972686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
973686b9cb9SBen Greear 					       const u8 *addr,
974686b9cb9SBen Greear 					       const u8 *localaddr)
97517741cdcSJohannes Berg {
976abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
97717741cdcSJohannes Berg 
978686b9cb9SBen Greear 	/*
979686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
980686b9cb9SBen Greear 	 * ... first in list.
981686b9cb9SBen Greear 	 */
982f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
983686b9cb9SBen Greear 		if (localaddr &&
984b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
985686b9cb9SBen Greear 			continue;
986f7c65594SJohannes Berg 		if (!sta->uploaded)
987f7c65594SJohannes Berg 			return NULL;
98817741cdcSJohannes Berg 		return &sta->sta;
989f7c65594SJohannes Berg 	}
990f7c65594SJohannes Berg 
991abe60632SJohannes Berg 	return NULL;
99217741cdcSJohannes Berg }
993686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
9945ed176e1SJohannes Berg 
9955ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
9965ed176e1SJohannes Berg 					 const u8 *addr)
9975ed176e1SJohannes Berg {
998f7c65594SJohannes Berg 	struct sta_info *sta;
9995ed176e1SJohannes Berg 
10005ed176e1SJohannes Berg 	if (!vif)
10015ed176e1SJohannes Berg 		return NULL;
10025ed176e1SJohannes Berg 
1003f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1004f7c65594SJohannes Berg 	if (!sta)
1005f7c65594SJohannes Berg 		return NULL;
10065ed176e1SJohannes Berg 
1007f7c65594SJohannes Berg 	if (!sta->uploaded)
1008f7c65594SJohannes Berg 		return NULL;
1009f7c65594SJohannes Berg 
1010f7c65594SJohannes Berg 	return &sta->sta;
10115ed176e1SJohannes Berg }
101217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1013af818581SJohannes Berg 
101450a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
101550a9432dSJohannes Berg {
101650a9432dSJohannes Berg 	struct sta_info *sta = _sta;
1017608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1018d012a605SMarco Porsch 	struct ps_data *ps;
1019d012a605SMarco Porsch 
1020d012a605SMarco Porsch 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
1021d012a605SMarco Porsch 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1022d012a605SMarco Porsch 		ps = &sdata->bss->ps;
10233f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
10243f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1025d012a605SMarco Porsch 	else
1026d012a605SMarco Porsch 		return;
102750a9432dSJohannes Berg 
1028c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1029608383bfSHelmut Schaa 	if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
1030d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
103150a9432dSJohannes Berg }
103250a9432dSJohannes Berg 
1033af818581SJohannes Berg /* powersave support code */
1034af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1035af818581SJohannes Berg {
1036af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1037af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1038948d887dSJohannes Berg 	struct sk_buff_head pending;
1039948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
1040987c285cSArik Nemtsov 	unsigned long flags;
1041af818581SJohannes Berg 
1042c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
104347086fc5SJohannes Berg 
10445a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1045948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1046948d887dSJohannes Berg 
1047d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
104812375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1049af818581SJohannes Berg 
1050948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1051af818581SJohannes Berg 
1052af818581SJohannes Berg 	/* Send all buffered frames to the station */
1053948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1054948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1055948d887dSJohannes Berg 
1056987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1057948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1058987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1059948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1060948d887dSJohannes Berg 		filtered += tmp - count;
1061948d887dSJohannes Berg 		count = tmp;
1062948d887dSJohannes Berg 
1063987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1064948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1065987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1066948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1067948d887dSJohannes Berg 		buffered += tmp - count;
1068948d887dSJohannes Berg 	}
1069948d887dSJohannes Berg 
1070948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1071948d887dSJohannes Berg 
1072af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1073af818581SJohannes Berg 
1074c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1075c868cb35SJohannes Berg 
1076bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1077bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1078bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1079af818581SJohannes Berg }
1080af818581SJohannes Berg 
1081ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1082ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
108340b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1084ce662b44SJohannes Berg {
1085ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1086ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1087ce662b44SJohannes Berg 	struct sk_buff *skb;
1088ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1089ce662b44SJohannes Berg 	__le16 fc;
1090c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1091ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
109255de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1093ce662b44SJohannes Berg 
1094ce662b44SJohannes Berg 	if (qos) {
1095ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1096ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1097ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1098ce662b44SJohannes Berg 	} else {
1099ce662b44SJohannes Berg 		size -= 2;
1100ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1101ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1102ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1103ce662b44SJohannes Berg 	}
1104ce662b44SJohannes Berg 
1105ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1106ce662b44SJohannes Berg 	if (!skb)
1107ce662b44SJohannes Berg 		return;
1108ce662b44SJohannes Berg 
1109ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1110ce662b44SJohannes Berg 
1111ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1112ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1113ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1114ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1115ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1116ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1117ce662b44SJohannes Berg 
1118ce662b44SJohannes Berg 	skb->priority = tid;
1119ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
112059b66255SJohannes Berg 	if (qos) {
1121ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1122ce662b44SJohannes Berg 
112340b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1124ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1125ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1126ce662b44SJohannes Berg 	}
1127ce662b44SJohannes Berg 
1128ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1129ce662b44SJohannes Berg 
1130ce662b44SJohannes Berg 	/*
1131ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1132ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1133deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1134deeaee19SJohannes Berg 	 * ends the poll/service period.
1135ce662b44SJohannes Berg 	 */
113602f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1137d6d23de2SFelix Fietkau 		       IEEE80211_TX_CTL_PS_RESPONSE |
1138deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1139ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1140ce662b44SJohannes Berg 
114140b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
114240b96408SJohannes Berg 
114389afe614SJohannes Berg 	skb->dev = sdata->dev;
114489afe614SJohannes Berg 
114555de908aSJohannes Berg 	rcu_read_lock();
114655de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
114755de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
114855de908aSJohannes Berg 		rcu_read_unlock();
114955de908aSJohannes Berg 		kfree_skb(skb);
115055de908aSJohannes Berg 		return;
115155de908aSJohannes Berg 	}
115255de908aSJohannes Berg 
11534bf88530SJohannes Berg 	ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
115455de908aSJohannes Berg 	rcu_read_unlock();
1155ce662b44SJohannes Berg }
1156ce662b44SJohannes Berg 
115747086fc5SJohannes Berg static void
115847086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
115947086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
116047086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1161af818581SJohannes Berg {
1162af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1163af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
11644049e09aSJohannes Berg 	bool found = false;
1165948d887dSJohannes Berg 	bool more_data = false;
1166948d887dSJohannes Berg 	int ac;
11674049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
116847086fc5SJohannes Berg 	struct sk_buff_head frames;
116947086fc5SJohannes Berg 
1170deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1171c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1172deeaee19SJohannes Berg 
117347086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1174af818581SJohannes Berg 
1175948d887dSJohannes Berg 	/*
117647086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1177948d887dSJohannes Berg 	 */
1178948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
11794049e09aSJohannes Berg 		unsigned long tids;
11804049e09aSJohannes Berg 
118147086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1182948d887dSJohannes Berg 			continue;
1183948d887dSJohannes Berg 
11844049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
11854049e09aSJohannes Berg 
11864049e09aSJohannes Berg 		if (!found) {
11874049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
11884049e09aSJohannes Berg 			if (driver_release_tids) {
11894049e09aSJohannes Berg 				found = true;
11904049e09aSJohannes Berg 			} else {
119147086fc5SJohannes Berg 				struct sk_buff *skb;
119247086fc5SJohannes Berg 
119347086fc5SJohannes Berg 				while (n_frames > 0) {
1194948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1195948d887dSJohannes Berg 					if (!skb) {
119647086fc5SJohannes Berg 						skb = skb_dequeue(
119747086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1198af818581SJohannes Berg 						if (skb)
1199af818581SJohannes Berg 							local->total_ps_buffered--;
1200af818581SJohannes Berg 					}
120147086fc5SJohannes Berg 					if (!skb)
120247086fc5SJohannes Berg 						break;
120347086fc5SJohannes Berg 					n_frames--;
12044049e09aSJohannes Berg 					found = true;
120547086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
120647086fc5SJohannes Berg 				}
1207948d887dSJohannes Berg 			}
1208948d887dSJohannes Berg 
12094049e09aSJohannes Berg 			/*
12104049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
12114049e09aSJohannes Berg 			 * certainly there's more data if we release just a
12124049e09aSJohannes Berg 			 * single frame now (from a single TID).
12134049e09aSJohannes Berg 			 */
121447086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
121547086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
12164049e09aSJohannes Berg 				more_data = true;
12174049e09aSJohannes Berg 				driver_release_tids =
12184049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
12194049e09aSJohannes Berg 				break;
12204049e09aSJohannes Berg 			}
12214049e09aSJohannes Berg 		}
1222948d887dSJohannes Berg 
1223948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1224948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1225948d887dSJohannes Berg 			more_data = true;
1226948d887dSJohannes Berg 			break;
1227948d887dSJohannes Berg 		}
1228948d887dSJohannes Berg 	}
1229af818581SJohannes Berg 
12304049e09aSJohannes Berg 	if (!found) {
1231ce662b44SJohannes Berg 		int tid;
12324049e09aSJohannes Berg 
1233ce662b44SJohannes Berg 		/*
1234ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1235ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1236ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1237ce662b44SJohannes Berg 		 *
1238ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1239ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1240ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1241ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1242ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1243ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1244ce662b44SJohannes Berg 		 *
1245ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1246ce662b44SJohannes Berg 		 */
1247ce662b44SJohannes Berg 
1248ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1249ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1250ce662b44SJohannes Berg 
125140b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
12524049e09aSJohannes Berg 		return;
12534049e09aSJohannes Berg 	}
12544049e09aSJohannes Berg 
125547086fc5SJohannes Berg 	if (!driver_release_tids) {
125647086fc5SJohannes Berg 		struct sk_buff_head pending;
125747086fc5SJohannes Berg 		struct sk_buff *skb;
125840b96408SJohannes Berg 		int num = 0;
125940b96408SJohannes Berg 		u16 tids = 0;
126047086fc5SJohannes Berg 
126147086fc5SJohannes Berg 		skb_queue_head_init(&pending);
126247086fc5SJohannes Berg 
126347086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1264af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
126547086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
126640b96408SJohannes Berg 			u8 *qoshdr = NULL;
126740b96408SJohannes Berg 
126840b96408SJohannes Berg 			num++;
1269af818581SJohannes Berg 
1270af818581SJohannes Berg 			/*
127147086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
127247086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
127347086fc5SJohannes Berg 			 * exchange.
1274af818581SJohannes Berg 			 */
1275d6d23de2SFelix Fietkau 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1276d6d23de2SFelix Fietkau 				       IEEE80211_TX_CTL_PS_RESPONSE;
1277af818581SJohannes Berg 
127847086fc5SJohannes Berg 			/*
127947086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
128047086fc5SJohannes Berg 			 * more buffered frames for this STA
128147086fc5SJohannes Berg 			 */
128224b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
128347086fc5SJohannes Berg 				hdr->frame_control |=
128447086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
128524b9c373SJanusz.Dziedzic@tieto.com 			else
128624b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
128724b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1288af818581SJohannes Berg 
128940b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
129040b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
129140b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
129240b96408SJohannes Berg 
129352a3f20cSMarco Porsch 			/* end service period after last frame */
129452a3f20cSMarco Porsch 			if (skb_queue_empty(&frames)) {
129540b96408SJohannes Berg 				if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
129652a3f20cSMarco Porsch 				    qoshdr)
129740b96408SJohannes Berg 					*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1298deeaee19SJohannes Berg 
129947086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
130047086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
130152a3f20cSMarco Porsch 			}
130247086fc5SJohannes Berg 
130340b96408SJohannes Berg 			if (qoshdr)
130440b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
130540b96408SJohannes Berg 			else
130640b96408SJohannes Berg 				tids |= BIT(0);
130740b96408SJohannes Berg 
130847086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
130947086fc5SJohannes Berg 		}
131047086fc5SJohannes Berg 
131140b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
131240b96408SJohannes Berg 					  reason, more_data);
131340b96408SJohannes Berg 
131447086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1315af818581SJohannes Berg 
1316c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1317af818581SJohannes Berg 	} else {
1318af818581SJohannes Berg 		/*
13194049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
13204049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
13214049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
13224049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
13234049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
13244049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
13254049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
13264049e09aSJohannes Berg 		 * needs to be set anyway.
1327af818581SJohannes Berg 		 */
13284049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
132947086fc5SJohannes Berg 					    n_frames, reason, more_data);
13304049e09aSJohannes Berg 
13314049e09aSJohannes Berg 		/*
13324049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
13334049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
13344049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
13354049e09aSJohannes Berg 		 * release function.
13364049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
13374049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
13384049e09aSJohannes Berg 		 */
1339af818581SJohannes Berg 	}
1340af818581SJohannes Berg }
1341af818581SJohannes Berg 
1342af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1343af818581SJohannes Berg {
134447086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1345af818581SJohannes Berg 
1346af818581SJohannes Berg 	/*
134747086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
134847086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
134947086fc5SJohannes Berg 	 * only from the non-enabled ones.
1350af818581SJohannes Berg 	 */
135147086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
135247086fc5SJohannes Berg 		ignore_for_response = 0;
1353af818581SJohannes Berg 
135447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
135547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1356af818581SJohannes Berg }
135747086fc5SJohannes Berg 
135847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
135947086fc5SJohannes Berg {
136047086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
136147086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
136247086fc5SJohannes Berg 
136347086fc5SJohannes Berg 	/*
136447086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
136547086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
136647086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
136747086fc5SJohannes Berg 	 * actually getting called.
136847086fc5SJohannes Berg 	 */
136947086fc5SJohannes Berg 	if (!delivery_enabled)
137047086fc5SJohannes Berg 		return;
137147086fc5SJohannes Berg 
137247086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
137347086fc5SJohannes Berg 	case 1:
137447086fc5SJohannes Berg 		n_frames = 2;
137547086fc5SJohannes Berg 		break;
137647086fc5SJohannes Berg 	case 2:
137747086fc5SJohannes Berg 		n_frames = 4;
137847086fc5SJohannes Berg 		break;
137947086fc5SJohannes Berg 	case 3:
138047086fc5SJohannes Berg 		n_frames = 6;
138147086fc5SJohannes Berg 		break;
138247086fc5SJohannes Berg 	case 0:
138347086fc5SJohannes Berg 		/* XXX: what is a good value? */
138447086fc5SJohannes Berg 		n_frames = 8;
138547086fc5SJohannes Berg 		break;
138647086fc5SJohannes Berg 	}
138747086fc5SJohannes Berg 
138847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
138947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1390af818581SJohannes Berg }
1391af818581SJohannes Berg 
1392af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1393af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1394af818581SJohannes Berg {
1395af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1396af818581SJohannes Berg 
1397b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1398b5878a2dSJohannes Berg 
1399af818581SJohannes Berg 	if (block)
1400c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1401c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1402af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1403af818581SJohannes Berg }
1404af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1405dcf55fb5SFelix Fietkau 
1406e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
140737fbd908SJohannes Berg {
140837fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
140937fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
141037fbd908SJohannes Berg 
141137fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
141237fbd908SJohannes Berg 
141337fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
141437fbd908SJohannes Berg }
1415e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
141637fbd908SJohannes Berg 
1417042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1418042ec453SJohannes Berg 				u8 tid, bool buffered)
1419dcf55fb5SFelix Fietkau {
1420dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1421dcf55fb5SFelix Fietkau 
14225a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1423042ec453SJohannes Berg 		return;
1424042ec453SJohannes Berg 
1425948d887dSJohannes Berg 	if (buffered)
1426948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1427948d887dSJohannes Berg 	else
1428948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1429948d887dSJohannes Berg 
1430c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1431dcf55fb5SFelix Fietkau }
1432042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1433d9a7ddb0SJohannes Berg 
143483d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1435d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1436d9a7ddb0SJohannes Berg {
14378bf11d8dSJohannes Berg 	might_sleep();
1438d9a7ddb0SJohannes Berg 
1439d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1440d9a7ddb0SJohannes Berg 		return 0;
1441d9a7ddb0SJohannes Berg 
1442f09603a2SJohannes Berg 	/* check allowed transitions first */
1443f09603a2SJohannes Berg 
1444d9a7ddb0SJohannes Berg 	switch (new_state) {
1445d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1446f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1447d9a7ddb0SJohannes Berg 			return -EINVAL;
1448d9a7ddb0SJohannes Berg 		break;
1449d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1450f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1451f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1452d9a7ddb0SJohannes Berg 			return -EINVAL;
1453d9a7ddb0SJohannes Berg 		break;
1454d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1455f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1456f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1457d9a7ddb0SJohannes Berg 			return -EINVAL;
1458d9a7ddb0SJohannes Berg 		break;
1459d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1460f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1461d9a7ddb0SJohannes Berg 			return -EINVAL;
1462d9a7ddb0SJohannes Berg 		break;
1463d9a7ddb0SJohannes Berg 	default:
1464d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1465d9a7ddb0SJohannes Berg 		return -EINVAL;
1466d9a7ddb0SJohannes Berg 	}
1467d9a7ddb0SJohannes Berg 
1468bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1469bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1470f09603a2SJohannes Berg 
1471f09603a2SJohannes Berg 	/*
1472f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1473f09603a2SJohannes Berg 	 * fail the transition
1474f09603a2SJohannes Berg 	 */
1475f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1476f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1477f09603a2SJohannes Berg 					sta->sta_state, new_state);
1478f09603a2SJohannes Berg 		if (err)
1479f09603a2SJohannes Berg 			return err;
1480f09603a2SJohannes Berg 	}
1481f09603a2SJohannes Berg 
1482f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1483f09603a2SJohannes Berg 
1484f09603a2SJohannes Berg 	switch (new_state) {
1485f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1486f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1487f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1488f09603a2SJohannes Berg 		break;
1489f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1490f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1491f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1492f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1493f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1494f09603a2SJohannes Berg 		break;
1495f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1496f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1497f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1498f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
14997e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
15007e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
15017e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
15027e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1503f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1504f09603a2SJohannes Berg 		}
1505f09603a2SJohannes Berg 		break;
1506f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1507f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
15087e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
15097e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
15107e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
15117e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1512f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1513f09603a2SJohannes Berg 		}
1514f09603a2SJohannes Berg 		break;
1515f09603a2SJohannes Berg 	default:
1516f09603a2SJohannes Berg 		break;
1517f09603a2SJohannes Berg 	}
1518f09603a2SJohannes Berg 
1519d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1520d9a7ddb0SJohannes Berg 
1521d9a7ddb0SJohannes Berg 	return 0;
1522d9a7ddb0SJohannes Berg }
1523