xref: /openbmc/linux/net/mac80211/sta_info.c (revision a4ec45a421b80bc36fd37578accf081f32527a7f)
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>
12f0706e82SJiri Benc #include <linux/netdevice.h>
13f0706e82SJiri Benc #include <linux/types.h>
14f0706e82SJiri Benc #include <linux/slab.h>
15f0706e82SJiri Benc #include <linux/skbuff.h>
16f0706e82SJiri Benc #include <linux/if_arp.h>
170d174406SJohannes Berg #include <linux/timer.h>
18d0709a65SJohannes Berg #include <linux/rtnetlink.h>
19f0706e82SJiri Benc 
20f0706e82SJiri Benc #include <net/mac80211.h>
21f0706e82SJiri Benc #include "ieee80211_i.h"
2224487981SJohannes Berg #include "driver-ops.h"
232c8dccc7SJohannes Berg #include "rate.h"
24f0706e82SJiri Benc #include "sta_info.h"
25e9f207f0SJiri Benc #include "debugfs_sta.h"
26ee385855SLuis Carlos Cobo #include "mesh.h"
27ce662b44SJohannes Berg #include "wme.h"
28f0706e82SJiri Benc 
29d0709a65SJohannes Berg /**
30d0709a65SJohannes Berg  * DOC: STA information lifetime rules
31d0709a65SJohannes Berg  *
32d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
33d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
34d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
35d0709a65SJohannes Berg  *
3634e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3734e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3834e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
3934e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4034e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4134e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4234e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4334e89507SJohannes Berg  * encryption keys.
4493e5deb1SJohannes Berg  *
4593e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4693e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
47d0709a65SJohannes Berg  *
4834e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
497e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
507e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5125985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5234e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5325985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5434e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
557e189a12SLuis R. Rodriguez  *
5634e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5734e89507SJohannes Berg  * calls are available.
58d0709a65SJohannes Berg  *
5934e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6034e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6134e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6234e89507SJohannes Berg  * freed before they are done using it.
63d0709a65SJohannes Berg  */
64f0706e82SJiri Benc 
654d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
66be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
67f0706e82SJiri Benc 			     struct sta_info *sta)
68f0706e82SJiri Benc {
69f0706e82SJiri Benc 	struct sta_info *s;
70f0706e82SJiri Benc 
7140b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
724d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
73f0706e82SJiri Benc 	if (!s)
74be8755e1SMichael Wu 		return -ENOENT;
75be8755e1SMichael Wu 	if (s == sta) {
76cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
77d0709a65SJohannes Berg 				   s->hnext);
78be8755e1SMichael Wu 		return 0;
79f0706e82SJiri Benc 	}
80f0706e82SJiri Benc 
8140b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8240b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8340b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
844d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8540b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
86cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
87be8755e1SMichael Wu 		return 0;
88f0706e82SJiri Benc 	}
89f0706e82SJiri Benc 
90be8755e1SMichael Wu 	return -ENOENT;
91f0706e82SJiri Benc }
92f0706e82SJiri Benc 
93d0709a65SJohannes Berg /* protected by RCU */
94abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
95abe60632SJohannes Berg 			      const u8 *addr)
9643ba7e95SJohannes Berg {
97abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
9843ba7e95SJohannes Berg 	struct sta_info *sta;
9943ba7e95SJohannes Berg 
1000379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1010379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
10243ba7e95SJohannes Berg 	while (sta) {
1032a33bee2SGuy Eilam 		if (sta->sdata == sdata && !sta->dummy &&
1042a33bee2SGuy Eilam 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1052a33bee2SGuy Eilam 			break;
1062a33bee2SGuy Eilam 		sta = rcu_dereference_check(sta->hnext,
1072a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_mtx));
1082a33bee2SGuy Eilam 	}
1092a33bee2SGuy Eilam 	return sta;
1102a33bee2SGuy Eilam }
1112a33bee2SGuy Eilam 
1122a33bee2SGuy Eilam /* get a station info entry even if it is a dummy station*/
1132a33bee2SGuy Eilam struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
1142a33bee2SGuy Eilam 			      const u8 *addr)
1152a33bee2SGuy Eilam {
1162a33bee2SGuy Eilam 	struct ieee80211_local *local = sdata->local;
1172a33bee2SGuy Eilam 	struct sta_info *sta;
1182a33bee2SGuy Eilam 
1192a33bee2SGuy Eilam 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1202a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_mtx));
1212a33bee2SGuy Eilam 	while (sta) {
122abe60632SJohannes Berg 		if (sta->sdata == sdata &&
123abe60632SJohannes Berg 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
12443ba7e95SJohannes Berg 			break;
1250379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1260379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
12743ba7e95SJohannes Berg 	}
12843ba7e95SJohannes Berg 	return sta;
12943ba7e95SJohannes Berg }
13043ba7e95SJohannes Berg 
1310e5ded5aSFelix Fietkau /*
1320e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1330e5ded5aSFelix Fietkau  * or from one of its vlans
1340e5ded5aSFelix Fietkau  */
1350e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1360e5ded5aSFelix Fietkau 				  const u8 *addr)
1370e5ded5aSFelix Fietkau {
1380e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1390e5ded5aSFelix Fietkau 	struct sta_info *sta;
1400e5ded5aSFelix Fietkau 
1410379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1420379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1430e5ded5aSFelix Fietkau 	while (sta) {
1440e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
145a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
1462a33bee2SGuy Eilam 		    !sta->dummy &&
1472a33bee2SGuy Eilam 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1482a33bee2SGuy Eilam 			break;
1492a33bee2SGuy Eilam 		sta = rcu_dereference_check(sta->hnext,
1502a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_mtx));
1512a33bee2SGuy Eilam 	}
1522a33bee2SGuy Eilam 	return sta;
1532a33bee2SGuy Eilam }
1542a33bee2SGuy Eilam 
1552a33bee2SGuy Eilam /*
1562a33bee2SGuy Eilam  * Get sta info either from the specified interface
1572a33bee2SGuy Eilam  * or from one of its vlans (including dummy stations)
1582a33bee2SGuy Eilam  */
1592a33bee2SGuy Eilam struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
1602a33bee2SGuy Eilam 				  const u8 *addr)
1612a33bee2SGuy Eilam {
1622a33bee2SGuy Eilam 	struct ieee80211_local *local = sdata->local;
1632a33bee2SGuy Eilam 	struct sta_info *sta;
1642a33bee2SGuy Eilam 
1652a33bee2SGuy Eilam 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1662a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_mtx));
1672a33bee2SGuy Eilam 	while (sta) {
1682a33bee2SGuy Eilam 		if ((sta->sdata == sdata ||
1692a33bee2SGuy Eilam 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
1700e5ded5aSFelix Fietkau 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1710e5ded5aSFelix Fietkau 			break;
1720379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1730379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1740e5ded5aSFelix Fietkau 	}
1750e5ded5aSFelix Fietkau 	return sta;
1760e5ded5aSFelix Fietkau }
1770e5ded5aSFelix Fietkau 
1783b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1793b53fde8SJohannes Berg 				     int idx)
180ee385855SLuis Carlos Cobo {
1813b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
182ee385855SLuis Carlos Cobo 	struct sta_info *sta;
183ee385855SLuis Carlos Cobo 	int i = 0;
184ee385855SLuis Carlos Cobo 
185d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1863b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1872a8ca29aSLuis Carlos Cobo 			continue;
188ee385855SLuis Carlos Cobo 		if (i < idx) {
189ee385855SLuis Carlos Cobo 			++i;
190ee385855SLuis Carlos Cobo 			continue;
191ee385855SLuis Carlos Cobo 		}
1922a8ca29aSLuis Carlos Cobo 		return sta;
193ee385855SLuis Carlos Cobo 	}
194ee385855SLuis Carlos Cobo 
195ee385855SLuis Carlos Cobo 	return NULL;
196ee385855SLuis Carlos Cobo }
197f0706e82SJiri Benc 
19893e5deb1SJohannes Berg /**
199d9a7ddb0SJohannes Berg  * sta_info_free - free STA
20093e5deb1SJohannes Berg  *
2016ef307bcSRandy Dunlap  * @local: pointer to the global information
20293e5deb1SJohannes Berg  * @sta: STA info to free
20393e5deb1SJohannes Berg  *
20493e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
205d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
206d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
207d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
20893e5deb1SJohannes Berg  */
209d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
21093e5deb1SJohannes Berg {
211889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2124b7679a5SJohannes Berg 		rate_control_free_sta(sta);
21393e5deb1SJohannes Berg 
21493e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2150fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
21693e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
21793e5deb1SJohannes Berg 
21893e5deb1SJohannes Berg 	kfree(sta);
21993e5deb1SJohannes Berg }
22093e5deb1SJohannes Berg 
2214d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
222d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
223d0709a65SJohannes Berg 			      struct sta_info *sta)
224f0706e82SJiri Benc {
2254d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
22617741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
227cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
228f0706e82SJiri Benc }
229f0706e82SJiri Benc 
230af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
231af818581SJohannes Berg {
232af818581SJohannes Berg 	struct sta_info *sta;
233af818581SJohannes Berg 
234af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
235af818581SJohannes Berg 
236af818581SJohannes Berg 	if (sta->dead)
237af818581SJohannes Berg 		return;
238af818581SJohannes Berg 
23954420473SHelmut Schaa 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
24054420473SHelmut Schaa 		local_bh_disable();
241af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
24254420473SHelmut Schaa 		local_bh_enable();
24354420473SHelmut Schaa 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
244c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
245ce662b44SJohannes Berg 
246ce662b44SJohannes Berg 		local_bh_disable();
247af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
248ce662b44SJohannes Berg 		local_bh_enable();
249c2c98fdeSJohannes Berg 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
250c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
251ce662b44SJohannes Berg 
252ce662b44SJohannes Berg 		local_bh_disable();
25347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
254ce662b44SJohannes Berg 		local_bh_enable();
25550a9432dSJohannes Berg 	} else
256c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
257af818581SJohannes Berg }
258af818581SJohannes Berg 
259af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
260af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
261af65cd96SJohannes Berg {
262af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
263af65cd96SJohannes Berg 		return 0;
264af65cd96SJohannes Berg 
265889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
266af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
267af65cd96SJohannes Berg 						     &sta->sta, gfp);
268889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
269af65cd96SJohannes Berg 		return -ENOMEM;
270af65cd96SJohannes Berg 
271af65cd96SJohannes Berg 	return 0;
272af65cd96SJohannes Berg }
273af65cd96SJohannes Berg 
27473651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
27556544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
276f0706e82SJiri Benc {
277d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
278f0706e82SJiri Benc 	struct sta_info *sta;
279ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
28016c5f15cSRon Rindjunsky 	int i;
281f0706e82SJiri Benc 
28217741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
283f0706e82SJiri Benc 	if (!sta)
28473651ee6SJohannes Berg 		return NULL;
285f0706e82SJiri Benc 
28607346f81SJohannes Berg 	spin_lock_init(&sta->lock);
287af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
28867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
289a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
29007346f81SJohannes Berg 
29117741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
292d0709a65SJohannes Berg 	sta->local = local;
293d0709a65SJohannes Berg 	sta->sdata = sdata;
2948bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
295f0706e82SJiri Benc 
29671ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
29771ec375cSJohannes Berg 
298ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
299ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
300541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
301541a45a1SBruno Randolf 
302af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
303f0706e82SJiri Benc 		kfree(sta);
30473651ee6SJohannes Berg 		return NULL;
305f0706e82SJiri Benc 	}
306f0706e82SJiri Benc 
30716c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
308a622ab72SJohannes Berg 		/*
309a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
310a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
311a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
312a622ab72SJohannes Berg 		 */
31316c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
31416c5f15cSRon Rindjunsky 	}
315948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
316948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
317948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
318948d887dSJohannes Berg 	}
31973651ee6SJohannes Berg 
320cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
3214be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
322cccaec98SSenthil Balasubramanian 
32373651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3240fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
32573651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
32673651ee6SJohannes Berg 
32703e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
32857cf8043SJavier Cardona 	sta->plink_state = NL80211_PLINK_LISTEN;
32903e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
33003e4497eSJohannes Berg #endif
33103e4497eSJohannes Berg 
33273651ee6SJohannes Berg 	return sta;
33373651ee6SJohannes Berg }
33473651ee6SJohannes Berg 
3358c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
33634e89507SJohannes Berg {
33734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
33834e89507SJohannes Berg 
33903e4497eSJohannes Berg 	/*
34003e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
34103e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
34203e4497eSJohannes Berg 	 * and another CPU turns off the net device.
34303e4497eSJohannes Berg 	 */
3448c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
3458c71df7aSGuy Eilam 		return -ENETDOWN;
34603e4497eSJohannes Berg 
34747846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
3488c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
3498c71df7aSGuy Eilam 		return -EINVAL;
3508c71df7aSGuy Eilam 
3518c71df7aSGuy Eilam 	return 0;
35293e5deb1SJohannes Berg }
35344213b5eSJohannes Berg 
354f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
355f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
356f09603a2SJohannes Berg 				     struct sta_info *sta)
357f09603a2SJohannes Berg {
358f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
359f09603a2SJohannes Berg 	int err = 0;
360f09603a2SJohannes Berg 
361f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
362f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
363f09603a2SJohannes Berg 		if (err)
364f09603a2SJohannes Berg 			break;
365f09603a2SJohannes Berg 	}
366f09603a2SJohannes Berg 
367f09603a2SJohannes Berg 	if (!err) {
368*a4ec45a4SJohannes Berg 		/*
369*a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
370*a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
371*a4ec45a4SJohannes Berg 		 */
372*a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
373f09603a2SJohannes Berg 			sta->uploaded = true;
374f09603a2SJohannes Berg 		return 0;
375f09603a2SJohannes Berg 	}
376f09603a2SJohannes Berg 
377f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
378f09603a2SJohannes Berg 		printk(KERN_DEBUG
379f09603a2SJohannes Berg 		       "%s: failed to move IBSS STA %pM to state %d (%d) - keeping it anyway.\n",
380f09603a2SJohannes Berg 		       sdata->name, sta->sta.addr, state + 1, err);
381f09603a2SJohannes Berg 		err = 0;
382f09603a2SJohannes Berg 	}
383f09603a2SJohannes Berg 
384f09603a2SJohannes Berg 	/* unwind on error */
385f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
386f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
387f09603a2SJohannes Berg 
388f09603a2SJohannes Berg 	return err;
389f09603a2SJohannes Berg }
390f09603a2SJohannes Berg 
39134e89507SJohannes Berg /*
3928c71df7aSGuy Eilam  * should be called with sta_mtx locked
3938c71df7aSGuy Eilam  * this function replaces the mutex lock
3948c71df7aSGuy Eilam  * with a RCU lock
3958c71df7aSGuy Eilam  */
3964d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
3978c71df7aSGuy Eilam {
3988c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
3998c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4002a33bee2SGuy Eilam 	struct sta_info *exist_sta;
4012a33bee2SGuy Eilam 	bool dummy_reinsert = false;
4028c71df7aSGuy Eilam 	int err = 0;
4038c71df7aSGuy Eilam 
4048c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4058c71df7aSGuy Eilam 
4068c71df7aSGuy Eilam 	/*
4072a33bee2SGuy Eilam 	 * check if STA exists already.
4084d33960bSJohannes Berg 	 * only accept a scenario of a second call to sta_info_insert_finish
4092a33bee2SGuy Eilam 	 * with a dummy station entry that was inserted earlier
4102a33bee2SGuy Eilam 	 * in that case - assume that the dummy station flag should
4112a33bee2SGuy Eilam 	 * be removed.
4122a33bee2SGuy Eilam 	 */
4132a33bee2SGuy Eilam 	exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
4142a33bee2SGuy Eilam 	if (exist_sta) {
4152a33bee2SGuy Eilam 		if (exist_sta == sta && sta->dummy) {
4162a33bee2SGuy Eilam 			dummy_reinsert = true;
4172a33bee2SGuy Eilam 		} else {
4184d33960bSJohannes Berg 			err = -EEXIST;
4194d33960bSJohannes Berg 			goto out_err;
42034e89507SJohannes Berg 		}
4212a33bee2SGuy Eilam 	}
42234e89507SJohannes Berg 
4234d33960bSJohannes Berg 	if (!sta->dummy || dummy_reinsert) {
4244d33960bSJohannes Berg 		/* notify driver */
425f09603a2SJohannes Berg 		err = sta_info_insert_drv_state(local, sdata, sta);
426f09603a2SJohannes Berg 		if (err)
427f09603a2SJohannes Berg 			goto out_err;
428f09603a2SJohannes Berg 	}
4294d33960bSJohannes Berg 
4304d33960bSJohannes Berg 	if (!dummy_reinsert) {
4314d33960bSJohannes Berg 		local->num_sta++;
4324d33960bSJohannes Berg 		local->sta_generation++;
4334d33960bSJohannes Berg 		smp_mb();
4344d33960bSJohannes Berg 
4354d33960bSJohannes Berg 		/* make the station visible */
4364d33960bSJohannes Berg 		sta_info_hash_add(local, sta);
4374d33960bSJohannes Berg 
4384d33960bSJohannes Berg 		list_add(&sta->list, &local->sta_list);
43983d5cc01SJohannes Berg 
44083d5cc01SJohannes Berg 		set_sta_flag(sta, WLAN_STA_INSERTED);
4414d33960bSJohannes Berg 	} else {
4424d33960bSJohannes Berg 		sta->dummy = false;
4434d33960bSJohannes Berg 	}
4444d33960bSJohannes Berg 
4454d33960bSJohannes Berg 	if (!sta->dummy) {
4464d33960bSJohannes Berg 		struct station_info sinfo;
4474d33960bSJohannes Berg 
4484d33960bSJohannes Berg 		ieee80211_sta_debugfs_add(sta);
4494d33960bSJohannes Berg 		rate_control_add_sta_debugfs(sta);
4504d33960bSJohannes Berg 
4514d33960bSJohannes Berg 		memset(&sinfo, 0, sizeof(sinfo));
4524d33960bSJohannes Berg 		sinfo.filled = 0;
4534d33960bSJohannes Berg 		sinfo.generation = local->sta_generation;
4544d33960bSJohannes Berg 		cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
45532bfd35dSJohannes Berg 	}
456d0709a65SJohannes Berg 
457f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4582a33bee2SGuy Eilam 	wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
4592a33bee2SGuy Eilam 			sta->dummy ? "dummy " : "", sta->sta.addr);
460f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
461f0706e82SJiri Benc 
46234e89507SJohannes Berg 	/* move reference to rcu-protected */
46334e89507SJohannes Berg 	rcu_read_lock();
46434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
465e9f207f0SJiri Benc 
46673651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
46773651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
46873651ee6SJohannes Berg 
46973651ee6SJohannes Berg 	return 0;
4704d33960bSJohannes Berg  out_err:
4714d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
4724d33960bSJohannes Berg 	rcu_read_lock();
4734d33960bSJohannes Berg 	return err;
4748c71df7aSGuy Eilam }
4758c71df7aSGuy Eilam 
4768c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
4778c71df7aSGuy Eilam {
4788c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4798c71df7aSGuy Eilam 	int err = 0;
4808c71df7aSGuy Eilam 
4814d33960bSJohannes Berg 	might_sleep();
4824d33960bSJohannes Berg 
4838c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
4848c71df7aSGuy Eilam 	if (err) {
4858c71df7aSGuy Eilam 		rcu_read_lock();
4868c71df7aSGuy Eilam 		goto out_free;
4878c71df7aSGuy Eilam 	}
4888c71df7aSGuy Eilam 
489004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
490004c872eSJohannes Berg 
4914d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
4928c71df7aSGuy Eilam 	if (err)
493004c872eSJohannes Berg 		goto out_free;
494d0709a65SJohannes Berg 
495004c872eSJohannes Berg 	return 0;
49693e5deb1SJohannes Berg  out_free:
49793e5deb1SJohannes Berg 	BUG_ON(!err);
498d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
49993e5deb1SJohannes Berg 	return err;
500f0706e82SJiri Benc }
501f0706e82SJiri Benc 
50234e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
50334e89507SJohannes Berg {
50434e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
50534e89507SJohannes Berg 
50634e89507SJohannes Berg 	rcu_read_unlock();
50734e89507SJohannes Berg 
50834e89507SJohannes Berg 	return err;
50934e89507SJohannes Berg }
51034e89507SJohannes Berg 
5112a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */
5122a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta)
5132a33bee2SGuy Eilam {
5142a33bee2SGuy Eilam 	struct ieee80211_local *local = sta->local;
5152a33bee2SGuy Eilam 	int err = 0;
5162a33bee2SGuy Eilam 
5172a33bee2SGuy Eilam 	err = sta_info_insert_check(sta);
5182a33bee2SGuy Eilam 	if (err) {
5192a33bee2SGuy Eilam 		mutex_unlock(&local->sta_mtx);
5202a33bee2SGuy Eilam 		return err;
5212a33bee2SGuy Eilam 	}
5222a33bee2SGuy Eilam 
5232a33bee2SGuy Eilam 	might_sleep();
5242a33bee2SGuy Eilam 
5254d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5262a33bee2SGuy Eilam 	rcu_read_unlock();
5272a33bee2SGuy Eilam 	return err;
5282a33bee2SGuy Eilam }
5292a33bee2SGuy Eilam 
530f0706e82SJiri Benc static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
531004c872eSJohannes Berg {
532004c872eSJohannes Berg 	/*
533004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
534004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
535004c872eSJohannes Berg 	 */
536004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
537004c872eSJohannes Berg }
538004c872eSJohannes Berg 
539004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
540004c872eSJohannes Berg {
541004c872eSJohannes Berg 	/*
542004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
543004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
544004c872eSJohannes Berg 	 */
545004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
546004c872eSJohannes Berg }
547004c872eSJohannes Berg 
548948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
549004c872eSJohannes Berg {
550948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
551948d887dSJohannes Berg 	switch (ac) {
552948d887dSJohannes Berg 	case IEEE80211_AC_VO:
553948d887dSJohannes Berg 		return BIT(6) | BIT(7);
554948d887dSJohannes Berg 	case IEEE80211_AC_VI:
555948d887dSJohannes Berg 		return BIT(4) | BIT(5);
556948d887dSJohannes Berg 	case IEEE80211_AC_BE:
557948d887dSJohannes Berg 		return BIT(0) | BIT(3);
558948d887dSJohannes Berg 	case IEEE80211_AC_BK:
559948d887dSJohannes Berg 		return BIT(1) | BIT(2);
560948d887dSJohannes Berg 	default:
561948d887dSJohannes Berg 		WARN_ON(1);
562948d887dSJohannes Berg 		return 0;
563d0709a65SJohannes Berg 	}
564004c872eSJohannes Berg }
565004c872eSJohannes Berg 
566c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
567004c872eSJohannes Berg {
568c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
569c868cb35SJohannes Berg 	struct ieee80211_if_ap *bss = sta->sdata->bss;
570d0709a65SJohannes Berg 	unsigned long flags;
571948d887dSJohannes Berg 	bool indicate_tim = false;
572948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
573948d887dSJohannes Berg 	int ac;
574004c872eSJohannes Berg 
575c868cb35SJohannes Berg 	if (WARN_ON_ONCE(!sta->sdata->bss))
576c868cb35SJohannes Berg 		return;
5773e122be0SJohannes Berg 
578c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
579c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
580c868cb35SJohannes Berg 		return;
581004c872eSJohannes Berg 
582c868cb35SJohannes Berg 	if (sta->dead)
583c868cb35SJohannes Berg 		goto done;
5843e122be0SJohannes Berg 
585948d887dSJohannes Berg 	/*
586948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
587948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
588948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
589948d887dSJohannes Berg 	 * non-enabled ones.
590948d887dSJohannes Berg 	 */
591948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
592948d887dSJohannes Berg 		ignore_for_tim = 0;
593948d887dSJohannes Berg 
594948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
595948d887dSJohannes Berg 		unsigned long tids;
596948d887dSJohannes Berg 
597948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
598948d887dSJohannes Berg 			continue;
599948d887dSJohannes Berg 
600948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
601948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
602948d887dSJohannes Berg 		if (indicate_tim)
603948d887dSJohannes Berg 			break;
604948d887dSJohannes Berg 
605948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
606948d887dSJohannes Berg 
607948d887dSJohannes Berg 		indicate_tim |=
608948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
609004c872eSJohannes Berg 	}
610004c872eSJohannes Berg 
611c868cb35SJohannes Berg  done:
6124d33960bSJohannes Berg 	spin_lock_irqsave(&local->tim_lock, flags);
613004c872eSJohannes Berg 
614948d887dSJohannes Berg 	if (indicate_tim)
615c868cb35SJohannes Berg 		__bss_tim_set(bss, sta->sta.aid);
616c868cb35SJohannes Berg 	else
61717741cdcSJohannes Berg 		__bss_tim_clear(bss, sta->sta.aid);
6183e122be0SJohannes Berg 
619c868cb35SJohannes Berg 	if (local->ops->set_tim) {
620c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
621948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
622c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
623004c872eSJohannes Berg 	}
624004c872eSJohannes Berg 
6254d33960bSJohannes Berg 	spin_unlock_irqrestore(&local->tim_lock, flags);
626004c872eSJohannes Berg }
627004c872eSJohannes Berg 
628cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
629f0706e82SJiri Benc {
630e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
631f0706e82SJiri Benc 	int timeout;
632f0706e82SJiri Benc 
633f0706e82SJiri Benc 	if (!skb)
634cd0b8d89SJohannes Berg 		return false;
635f0706e82SJiri Benc 
636e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
637f0706e82SJiri Benc 
638f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
63957c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
64057c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
64157c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
642f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
643f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
644e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
645f0706e82SJiri Benc }
646f0706e82SJiri Benc 
647f0706e82SJiri Benc 
648948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
649948d887dSJohannes Berg 						struct sta_info *sta, int ac)
650f0706e82SJiri Benc {
651f0706e82SJiri Benc 	unsigned long flags;
652f0706e82SJiri Benc 	struct sk_buff *skb;
653f0706e82SJiri Benc 
65460750397SJohannes Berg 	/*
65560750397SJohannes Berg 	 * First check for frames that should expire on the filtered
65660750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
65760750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
65860750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
65960750397SJohannes Berg 	 * total_ps_buffered counter.
66060750397SJohannes Berg 	 */
661f0706e82SJiri Benc 	for (;;) {
662948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
663948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
66457c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
665948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
666836341a7SJohannes Berg 		else
667f0706e82SJiri Benc 			skb = NULL;
668948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
669f0706e82SJiri Benc 
67060750397SJohannes Berg 		/*
67160750397SJohannes Berg 		 * Frames are queued in order, so if this one
67260750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
67360750397SJohannes Berg 		 * we actually reached the end of the queue we
67460750397SJohannes Berg 		 * also need to stop, of course.
67560750397SJohannes Berg 		 */
67660750397SJohannes Berg 		if (!skb)
67760750397SJohannes Berg 			break;
67860750397SJohannes Berg 		dev_kfree_skb(skb);
67960750397SJohannes Berg 	}
68060750397SJohannes Berg 
68160750397SJohannes Berg 	/*
68260750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
68360750397SJohannes Berg 	 * only find something if the filtered queue was emptied
68460750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
68560750397SJohannes Berg 	 * buffered frames.
68660750397SJohannes Berg 	 */
687f0706e82SJiri Benc 	for (;;) {
688948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
689948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
690f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
691948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
692f0706e82SJiri Benc 		else
693f0706e82SJiri Benc 			skb = NULL;
694948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
695f0706e82SJiri Benc 
69660750397SJohannes Berg 		/*
69760750397SJohannes Berg 		 * frames are queued in order, so if this one
69860750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
69960750397SJohannes Berg 		 * the queue) we can stop testing
70060750397SJohannes Berg 		 */
701836341a7SJohannes Berg 		if (!skb)
702836341a7SJohannes Berg 			break;
703836341a7SJohannes Berg 
704f0706e82SJiri Benc 		local->total_ps_buffered--;
705f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
7060c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
7070c68ae26SJohannes Berg 		       sta->sta.addr);
708f4ea83ddSJohannes Berg #endif
709f0706e82SJiri Benc 		dev_kfree_skb(skb);
710f0706e82SJiri Benc 	}
7113393a608SJuuso Oikarinen 
71260750397SJohannes Berg 	/*
71360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
71460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
71560750397SJohannes Berg 	 * frames.
71660750397SJohannes Berg 	 */
71760750397SJohannes Berg 	sta_info_recalc_tim(sta);
71860750397SJohannes Berg 
71960750397SJohannes Berg 	/*
72060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
72160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
72260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
72360750397SJohannes Berg 	 */
724948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
725948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
726948d887dSJohannes Berg }
727948d887dSJohannes Berg 
728948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
729948d887dSJohannes Berg 					     struct sta_info *sta)
730948d887dSJohannes Berg {
731948d887dSJohannes Berg 	bool have_buffered = false;
732948d887dSJohannes Berg 	int ac;
733948d887dSJohannes Berg 
734948d887dSJohannes Berg 	/* This is only necessary for stations on BSS interfaces */
735948d887dSJohannes Berg 	if (!sta->sdata->bss)
736948d887dSJohannes Berg 		return false;
737948d887dSJohannes Berg 
738948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
739948d887dSJohannes Berg 		have_buffered |=
740948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
741948d887dSJohannes Berg 
742948d887dSJohannes Berg 	return have_buffered;
743f0706e82SJiri Benc }
744f0706e82SJiri Benc 
74583d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
74634e89507SJohannes Berg {
74734e89507SJohannes Berg 	struct ieee80211_local *local;
74834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
749948d887dSJohannes Berg 	int ret, i, ac;
75042624d49SYogesh Ashok Powar 	struct tid_ampdu_tx *tid_tx;
75134e89507SJohannes Berg 
75234e89507SJohannes Berg 	might_sleep();
75334e89507SJohannes Berg 
75434e89507SJohannes Berg 	if (!sta)
75534e89507SJohannes Berg 		return -ENOENT;
75634e89507SJohannes Berg 
75734e89507SJohannes Berg 	local = sta->local;
75834e89507SJohannes Berg 	sdata = sta->sdata;
75934e89507SJohannes Berg 
76083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
76183d5cc01SJohannes Berg 
762098a6070SJohannes Berg 	/*
763098a6070SJohannes Berg 	 * Before removing the station from the driver and
764098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
765098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
766098a6070SJohannes Berg 	 * will be sufficient.
767098a6070SJohannes Berg 	 */
768c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
76953f73c09SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, true);
770098a6070SJohannes Berg 
77134e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
77234e89507SJohannes Berg 	if (ret)
77334e89507SJohannes Berg 		return ret;
77434e89507SJohannes Berg 
7754d33960bSJohannes Berg 	list_del(&sta->list);
7764d33960bSJohannes Berg 
7778cb23153SJohannes Berg 	mutex_lock(&local->key_mtx);
778e31b8213SJohannes Berg 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
77940b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
780e31b8213SJohannes Berg 	if (sta->ptk)
78140b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
7828cb23153SJohannes Berg 	mutex_unlock(&local->key_mtx);
78334e89507SJohannes Berg 
78434e89507SJohannes Berg 	sta->dead = true;
78534e89507SJohannes Berg 
78634e89507SJohannes Berg 	local->num_sta--;
78734e89507SJohannes Berg 	local->sta_generation++;
78834e89507SJohannes Berg 
78934e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
790a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
79134e89507SJohannes Berg 
79283d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
793f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
794f09603a2SJohannes Berg 		if (ret) {
79583d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
79683d5cc01SJohannes Berg 			break;
79783d5cc01SJohannes Berg 		}
79883d5cc01SJohannes Berg 	}
799d9a7ddb0SJohannes Berg 
800f09603a2SJohannes Berg 	if (sta->uploaded) {
801f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
802f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
803f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
804f09603a2SJohannes Berg 	}
80534e89507SJohannes Berg 
806e64b3795SJohannes Berg 	/*
807e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
808e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
809e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
810e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
811e64b3795SJohannes Berg 	 */
812e64b3795SJohannes Berg 	synchronize_rcu();
813e64b3795SJohannes Berg 
8144f3eb0baSHelmut Schaa 	if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
8154f3eb0baSHelmut Schaa 		BUG_ON(!sdata->bss);
8164f3eb0baSHelmut Schaa 
8174f3eb0baSHelmut Schaa 		clear_sta_flag(sta, WLAN_STA_PS_STA);
8184f3eb0baSHelmut Schaa 
8194f3eb0baSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
8204f3eb0baSHelmut Schaa 		sta_info_recalc_tim(sta);
8214f3eb0baSHelmut Schaa 	}
8224f3eb0baSHelmut Schaa 
823948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
824948d887dSJohannes Berg 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
825948d887dSJohannes Berg 		__skb_queue_purge(&sta->ps_tx_buf[ac]);
826948d887dSJohannes Berg 		__skb_queue_purge(&sta->tx_filtered[ac]);
827948d887dSJohannes Berg 	}
828c868cb35SJohannes Berg 
82934e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
830e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
83134e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
83234e89507SJohannes Berg #endif
83334e89507SJohannes Berg 
83434e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
8350fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
83634e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
83734e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
83834e89507SJohannes Berg 
839ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
840ec15e68bSJouni Malinen 
84134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
84234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
84334e89507SJohannes Berg 
84434e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
84534e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
84634e89507SJohannes Berg 		mesh_plink_deactivate(sta);
84734e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
84834e89507SJohannes Berg 	}
84934e89507SJohannes Berg #endif
85034e89507SJohannes Berg 
851151a02f6SJohannes Berg 	/*
852151a02f6SJohannes Berg 	 * Destroy aggregation state here. It would be nice to wait for the
853151a02f6SJohannes Berg 	 * driver to finish aggregation stop and then clean up, but for now
854151a02f6SJohannes Berg 	 * drivers have to handle aggregation stop being requested, followed
855151a02f6SJohannes Berg 	 * directly by station destruction.
85642624d49SYogesh Ashok Powar 	 */
85742624d49SYogesh Ashok Powar 	for (i = 0; i < STA_TID_NUM; i++) {
858151a02f6SJohannes Berg 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
85927bf8882SYogesh Ashok Powar 		if (!tid_tx)
86042624d49SYogesh Ashok Powar 			continue;
86142624d49SYogesh Ashok Powar 		__skb_queue_purge(&tid_tx->pending);
862151a02f6SJohannes Berg 		kfree(tid_tx);
86342624d49SYogesh Ashok Powar 	}
86427bf8882SYogesh Ashok Powar 
865d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
86634e89507SJohannes Berg 
86734e89507SJohannes Berg 	return 0;
86834e89507SJohannes Berg }
86934e89507SJohannes Berg 
87034e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
87134e89507SJohannes Berg {
87234e89507SJohannes Berg 	struct sta_info *sta;
87334e89507SJohannes Berg 	int ret;
87434e89507SJohannes Berg 
87534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8762a33bee2SGuy Eilam 	sta = sta_info_get_rx(sdata, addr);
87734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
87834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
87934e89507SJohannes Berg 
88034e89507SJohannes Berg 	return ret;
88134e89507SJohannes Berg }
88234e89507SJohannes Berg 
88334e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
88434e89507SJohannes Berg 			      const u8 *addr)
88534e89507SJohannes Berg {
88634e89507SJohannes Berg 	struct sta_info *sta;
88734e89507SJohannes Berg 	int ret;
88834e89507SJohannes Berg 
88934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8902a33bee2SGuy Eilam 	sta = sta_info_get_bss_rx(sdata, addr);
89134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
89234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
89334e89507SJohannes Berg 
89434e89507SJohannes Berg 	return ret;
89534e89507SJohannes Berg }
896f0706e82SJiri Benc 
897f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
898f0706e82SJiri Benc {
899f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
900f0706e82SJiri Benc 	struct sta_info *sta;
9013393a608SJuuso Oikarinen 	bool timer_needed = false;
902f0706e82SJiri Benc 
903d0709a65SJohannes Berg 	rcu_read_lock();
904d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9053393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9063393a608SJuuso Oikarinen 			timer_needed = true;
907d0709a65SJohannes Berg 	rcu_read_unlock();
908f0706e82SJiri Benc 
9095bb644a0SJohannes Berg 	if (local->quiescing)
9105bb644a0SJohannes Berg 		return;
9115bb644a0SJohannes Berg 
9123393a608SJuuso Oikarinen 	if (!timer_needed)
9133393a608SJuuso Oikarinen 		return;
9143393a608SJuuso Oikarinen 
91526d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
91626d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
917f0706e82SJiri Benc }
918f0706e82SJiri Benc 
919f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
920f0706e82SJiri Benc {
9214d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
92234e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
923f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
924f0706e82SJiri Benc 
925b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
926b24b8a24SPavel Emelyanov 		    (unsigned long)local);
927f0706e82SJiri Benc }
928f0706e82SJiri Benc 
929f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
930f0706e82SJiri Benc {
931f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
932be8755e1SMichael Wu 	sta_info_flush(local, NULL);
933f0706e82SJiri Benc }
934f0706e82SJiri Benc 
935f0706e82SJiri Benc /**
936f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
93744213b5eSJohannes Berg  *
93844213b5eSJohannes Berg  * Returns the number of removed STA entries.
93944213b5eSJohannes Berg  *
940f0706e82SJiri Benc  * @local: local interface data
941d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
942f0706e82SJiri Benc  */
94344213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
944d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
945f0706e82SJiri Benc {
946f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
94744213b5eSJohannes Berg 	int ret = 0;
948f0706e82SJiri Benc 
949d0709a65SJohannes Berg 	might_sleep();
950d0709a65SJohannes Berg 
95134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
95234e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
95334e89507SJohannes Berg 		if (!sdata || sdata == sta->sdata)
95434e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
95534e89507SJohannes Berg 	}
95634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
95744213b5eSJohannes Berg 
95844213b5eSJohannes Berg 	return ret;
959f0706e82SJiri Benc }
960dc6676b7SJohannes Berg 
96124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
96224723d1bSJohannes Berg 			  unsigned long exp_time)
96324723d1bSJohannes Berg {
96424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
96524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
96624723d1bSJohannes Berg 
96734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
968e46a2cf9SMohammed Shafi Shajakhan 
969e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
970ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
971ec2b774eSMarek Lindner 			continue;
972ec2b774eSMarek Lindner 
97324723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
97424723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
9750c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
97647846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
97724723d1bSJohannes Berg #endif
97834e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
97924723d1bSJohannes Berg 		}
980e46a2cf9SMohammed Shafi Shajakhan 	}
981e46a2cf9SMohammed Shafi Shajakhan 
98234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
98324723d1bSJohannes Berg }
98417741cdcSJohannes Berg 
985686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
986686b9cb9SBen Greear 					       const u8 *addr,
987686b9cb9SBen Greear 					       const u8 *localaddr)
98817741cdcSJohannes Berg {
989abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
99017741cdcSJohannes Berg 
991686b9cb9SBen Greear 	/*
992686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
993686b9cb9SBen Greear 	 * ... first in list.
994686b9cb9SBen Greear 	 */
995f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
996686b9cb9SBen Greear 		if (localaddr &&
997686b9cb9SBen Greear 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
998686b9cb9SBen Greear 			continue;
999f7c65594SJohannes Berg 		if (!sta->uploaded)
1000f7c65594SJohannes Berg 			return NULL;
100117741cdcSJohannes Berg 		return &sta->sta;
1002f7c65594SJohannes Berg 	}
1003f7c65594SJohannes Berg 
1004abe60632SJohannes Berg 	return NULL;
100517741cdcSJohannes Berg }
1006686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10075ed176e1SJohannes Berg 
10085ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10095ed176e1SJohannes Berg 					 const u8 *addr)
10105ed176e1SJohannes Berg {
1011f7c65594SJohannes Berg 	struct sta_info *sta;
10125ed176e1SJohannes Berg 
10135ed176e1SJohannes Berg 	if (!vif)
10145ed176e1SJohannes Berg 		return NULL;
10155ed176e1SJohannes Berg 
1016f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1017f7c65594SJohannes Berg 	if (!sta)
1018f7c65594SJohannes Berg 		return NULL;
10195ed176e1SJohannes Berg 
1020f7c65594SJohannes Berg 	if (!sta->uploaded)
1021f7c65594SJohannes Berg 		return NULL;
1022f7c65594SJohannes Berg 
1023f7c65594SJohannes Berg 	return &sta->sta;
10245ed176e1SJohannes Berg }
102517741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1026af818581SJohannes Berg 
102750a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
102850a9432dSJohannes Berg {
102950a9432dSJohannes Berg 	struct sta_info *sta = _sta;
1030608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
103150a9432dSJohannes Berg 
1032c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1033608383bfSHelmut Schaa 	if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
1034608383bfSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
103550a9432dSJohannes Berg }
103650a9432dSJohannes Berg 
1037af818581SJohannes Berg /* powersave support code */
1038af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1039af818581SJohannes Berg {
1040af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1041af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1042948d887dSJohannes Berg 	struct sk_buff_head pending;
1043948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
1044af818581SJohannes Berg 
1045c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
104647086fc5SJohannes Berg 
1047948d887dSJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
1048948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1049948d887dSJohannes Berg 
1050d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
105112375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1052af818581SJohannes Berg 
1053948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1054af818581SJohannes Berg 
1055af818581SJohannes Berg 	/* Send all buffered frames to the station */
1056948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1057948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1058948d887dSJohannes Berg 
1059948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1060948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1061948d887dSJohannes Berg 		filtered += tmp - count;
1062948d887dSJohannes Berg 		count = tmp;
1063948d887dSJohannes Berg 
1064948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1065948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1066948d887dSJohannes Berg 		buffered += tmp - count;
1067948d887dSJohannes Berg 	}
1068948d887dSJohannes Berg 
1069948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1070948d887dSJohannes Berg 
1071af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1072af818581SJohannes Berg 
1073c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1074c868cb35SJohannes Berg 
1075af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1076af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
107747846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
1078948d887dSJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1079af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1080af818581SJohannes Berg }
1081af818581SJohannes Berg 
1082ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1083ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
108440b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1085ce662b44SJohannes Berg {
1086ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1087ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1088ce662b44SJohannes Berg 	struct sk_buff *skb;
1089ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1090ce662b44SJohannes Berg 	__le16 fc;
1091c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1092ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
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 	 */
1136deeaee19SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
1137deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1138ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1139ce662b44SJohannes Berg 
114040b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
114140b96408SJohannes Berg 
1142ce662b44SJohannes Berg 	ieee80211_xmit(sdata, skb);
1143ce662b44SJohannes Berg }
1144ce662b44SJohannes Berg 
114547086fc5SJohannes Berg static void
114647086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
114747086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
114847086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1149af818581SJohannes Berg {
1150af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1151af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
11524049e09aSJohannes Berg 	bool found = false;
1153948d887dSJohannes Berg 	bool more_data = false;
1154948d887dSJohannes Berg 	int ac;
11554049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
115647086fc5SJohannes Berg 	struct sk_buff_head frames;
115747086fc5SJohannes Berg 
1158deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1159c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1160deeaee19SJohannes Berg 
116147086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1162af818581SJohannes Berg 
1163948d887dSJohannes Berg 	/*
116447086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1165948d887dSJohannes Berg 	 */
1166948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
11674049e09aSJohannes Berg 		unsigned long tids;
11684049e09aSJohannes Berg 
116947086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1170948d887dSJohannes Berg 			continue;
1171948d887dSJohannes Berg 
11724049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
11734049e09aSJohannes Berg 
11744049e09aSJohannes Berg 		if (!found) {
11754049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
11764049e09aSJohannes Berg 			if (driver_release_tids) {
11774049e09aSJohannes Berg 				found = true;
11784049e09aSJohannes Berg 			} else {
117947086fc5SJohannes Berg 				struct sk_buff *skb;
118047086fc5SJohannes Berg 
118147086fc5SJohannes Berg 				while (n_frames > 0) {
1182948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1183948d887dSJohannes Berg 					if (!skb) {
118447086fc5SJohannes Berg 						skb = skb_dequeue(
118547086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1186af818581SJohannes Berg 						if (skb)
1187af818581SJohannes Berg 							local->total_ps_buffered--;
1188af818581SJohannes Berg 					}
118947086fc5SJohannes Berg 					if (!skb)
119047086fc5SJohannes Berg 						break;
119147086fc5SJohannes Berg 					n_frames--;
11924049e09aSJohannes Berg 					found = true;
119347086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
119447086fc5SJohannes Berg 				}
1195948d887dSJohannes Berg 			}
1196948d887dSJohannes Berg 
11974049e09aSJohannes Berg 			/*
11984049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
11994049e09aSJohannes Berg 			 * certainly there's more data if we release just a
12004049e09aSJohannes Berg 			 * single frame now (from a single TID).
12014049e09aSJohannes Berg 			 */
120247086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
120347086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
12044049e09aSJohannes Berg 				more_data = true;
12054049e09aSJohannes Berg 				driver_release_tids =
12064049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
12074049e09aSJohannes Berg 				break;
12084049e09aSJohannes Berg 			}
12094049e09aSJohannes Berg 		}
1210948d887dSJohannes Berg 
1211948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1212948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1213948d887dSJohannes Berg 			more_data = true;
1214948d887dSJohannes Berg 			break;
1215948d887dSJohannes Berg 		}
1216948d887dSJohannes Berg 	}
1217af818581SJohannes Berg 
12184049e09aSJohannes Berg 	if (!found) {
1219ce662b44SJohannes Berg 		int tid;
12204049e09aSJohannes Berg 
1221ce662b44SJohannes Berg 		/*
1222ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1223ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1224ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1225ce662b44SJohannes Berg 		 *
1226ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1227ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1228ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1229ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1230ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1231ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1232ce662b44SJohannes Berg 		 *
1233ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1234ce662b44SJohannes Berg 		 */
1235ce662b44SJohannes Berg 
1236ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1237ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1238ce662b44SJohannes Berg 
123940b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
12404049e09aSJohannes Berg 		return;
12414049e09aSJohannes Berg 	}
12424049e09aSJohannes Berg 
124347086fc5SJohannes Berg 	if (!driver_release_tids) {
124447086fc5SJohannes Berg 		struct sk_buff_head pending;
124547086fc5SJohannes Berg 		struct sk_buff *skb;
124640b96408SJohannes Berg 		int num = 0;
124740b96408SJohannes Berg 		u16 tids = 0;
124847086fc5SJohannes Berg 
124947086fc5SJohannes Berg 		skb_queue_head_init(&pending);
125047086fc5SJohannes Berg 
125147086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1252af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
125347086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
125440b96408SJohannes Berg 			u8 *qoshdr = NULL;
125540b96408SJohannes Berg 
125640b96408SJohannes Berg 			num++;
1257af818581SJohannes Berg 
1258af818581SJohannes Berg 			/*
125947086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
126047086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
126147086fc5SJohannes Berg 			 * exchange.
1262af818581SJohannes Berg 			 */
126347086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
1264af818581SJohannes Berg 
126547086fc5SJohannes Berg 			/*
126647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
126747086fc5SJohannes Berg 			 * more buffered frames for this STA
126847086fc5SJohannes Berg 			 */
126924b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
127047086fc5SJohannes Berg 				hdr->frame_control |=
127147086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
127224b9c373SJanusz.Dziedzic@tieto.com 			else
127324b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
127424b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1275af818581SJohannes Berg 
127640b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
127740b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
127840b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
127940b96408SJohannes Berg 
128047086fc5SJohannes Berg 			/* set EOSP for the frame */
128140b96408SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
128240b96408SJohannes Berg 			    qoshdr && skb_queue_empty(&frames))
128340b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1284deeaee19SJohannes Berg 
128547086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_STATUS_EOSP |
128647086fc5SJohannes Berg 				       IEEE80211_TX_CTL_REQ_TX_STATUS;
128747086fc5SJohannes Berg 
128840b96408SJohannes Berg 			if (qoshdr)
128940b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
129040b96408SJohannes Berg 			else
129140b96408SJohannes Berg 				tids |= BIT(0);
129240b96408SJohannes Berg 
129347086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
129447086fc5SJohannes Berg 		}
129547086fc5SJohannes Berg 
129640b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
129740b96408SJohannes Berg 					  reason, more_data);
129840b96408SJohannes Berg 
129947086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1300af818581SJohannes Berg 
1301c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1302af818581SJohannes Berg 	} else {
1303af818581SJohannes Berg 		/*
13044049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
13054049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
13064049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
13074049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
13084049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
13094049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
13104049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
13114049e09aSJohannes Berg 		 * needs to be set anyway.
1312af818581SJohannes Berg 		 */
13134049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
131447086fc5SJohannes Berg 					    n_frames, reason, more_data);
13154049e09aSJohannes Berg 
13164049e09aSJohannes Berg 		/*
13174049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
13184049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
13194049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
13204049e09aSJohannes Berg 		 * release function.
13214049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
13224049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
13234049e09aSJohannes Berg 		 */
1324af818581SJohannes Berg 	}
1325af818581SJohannes Berg }
1326af818581SJohannes Berg 
1327af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1328af818581SJohannes Berg {
132947086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1330af818581SJohannes Berg 
1331af818581SJohannes Berg 	/*
133247086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
133347086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
133447086fc5SJohannes Berg 	 * only from the non-enabled ones.
1335af818581SJohannes Berg 	 */
133647086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
133747086fc5SJohannes Berg 		ignore_for_response = 0;
1338af818581SJohannes Berg 
133947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
134047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1341af818581SJohannes Berg }
134247086fc5SJohannes Berg 
134347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
134447086fc5SJohannes Berg {
134547086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
134647086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
134747086fc5SJohannes Berg 
134847086fc5SJohannes Berg 	/*
134947086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
135047086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
135147086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
135247086fc5SJohannes Berg 	 * actually getting called.
135347086fc5SJohannes Berg 	 */
135447086fc5SJohannes Berg 	if (!delivery_enabled)
135547086fc5SJohannes Berg 		return;
135647086fc5SJohannes Berg 
135747086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
135847086fc5SJohannes Berg 	case 1:
135947086fc5SJohannes Berg 		n_frames = 2;
136047086fc5SJohannes Berg 		break;
136147086fc5SJohannes Berg 	case 2:
136247086fc5SJohannes Berg 		n_frames = 4;
136347086fc5SJohannes Berg 		break;
136447086fc5SJohannes Berg 	case 3:
136547086fc5SJohannes Berg 		n_frames = 6;
136647086fc5SJohannes Berg 		break;
136747086fc5SJohannes Berg 	case 0:
136847086fc5SJohannes Berg 		/* XXX: what is a good value? */
136947086fc5SJohannes Berg 		n_frames = 8;
137047086fc5SJohannes Berg 		break;
137147086fc5SJohannes Berg 	}
137247086fc5SJohannes Berg 
137347086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
137447086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1375af818581SJohannes Berg }
1376af818581SJohannes Berg 
1377af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1378af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1379af818581SJohannes Berg {
1380af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1381af818581SJohannes Berg 
1382b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1383b5878a2dSJohannes Berg 
1384af818581SJohannes Berg 	if (block)
1385c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1386c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1387af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1388af818581SJohannes Berg }
1389af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1390dcf55fb5SFelix Fietkau 
139137fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
139237fbd908SJohannes Berg {
139337fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
139437fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
139537fbd908SJohannes Berg 	struct sk_buff *skb;
139637fbd908SJohannes Berg 	struct skb_eosp_msg_data *data;
139737fbd908SJohannes Berg 
139837fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
139937fbd908SJohannes Berg 
140037fbd908SJohannes Berg 	skb = alloc_skb(0, GFP_ATOMIC);
140137fbd908SJohannes Berg 	if (!skb) {
140237fbd908SJohannes Berg 		/* too bad ... but race is better than loss */
140337fbd908SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_SP);
140437fbd908SJohannes Berg 		return;
140537fbd908SJohannes Berg 	}
140637fbd908SJohannes Berg 
140737fbd908SJohannes Berg 	data = (void *)skb->cb;
140837fbd908SJohannes Berg 	memcpy(data->sta, pubsta->addr, ETH_ALEN);
140937fbd908SJohannes Berg 	memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
141037fbd908SJohannes Berg 	skb->pkt_type = IEEE80211_EOSP_MSG;
141137fbd908SJohannes Berg 	skb_queue_tail(&local->skb_queue, skb);
141237fbd908SJohannes Berg 	tasklet_schedule(&local->tasklet);
141337fbd908SJohannes Berg }
141437fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
141537fbd908SJohannes Berg 
1416042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1417042ec453SJohannes Berg 				u8 tid, bool buffered)
1418dcf55fb5SFelix Fietkau {
1419dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1420dcf55fb5SFelix Fietkau 
1421948d887dSJohannes Berg 	if (WARN_ON(tid >= STA_TID_NUM))
1422042ec453SJohannes Berg 		return;
1423042ec453SJohannes Berg 
1424948d887dSJohannes Berg 	if (buffered)
1425948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1426948d887dSJohannes Berg 	else
1427948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1428948d887dSJohannes Berg 
1429c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1430dcf55fb5SFelix Fietkau }
1431042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1432d9a7ddb0SJohannes Berg 
143383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1434d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1435d9a7ddb0SJohannes Berg {
14368bf11d8dSJohannes Berg 	might_sleep();
1437d9a7ddb0SJohannes Berg 
1438d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1439d9a7ddb0SJohannes Berg 		return 0;
1440d9a7ddb0SJohannes Berg 
1441f09603a2SJohannes Berg 	/* check allowed transitions first */
1442f09603a2SJohannes Berg 
1443d9a7ddb0SJohannes Berg 	switch (new_state) {
1444d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1445f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1446d9a7ddb0SJohannes Berg 			return -EINVAL;
1447d9a7ddb0SJohannes Berg 		break;
1448d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1449f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1450f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1451d9a7ddb0SJohannes Berg 			return -EINVAL;
1452d9a7ddb0SJohannes Berg 		break;
1453d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1454f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1455f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1456d9a7ddb0SJohannes Berg 			return -EINVAL;
1457d9a7ddb0SJohannes Berg 		break;
1458d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1459f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1460d9a7ddb0SJohannes Berg 			return -EINVAL;
1461d9a7ddb0SJohannes Berg 		break;
1462d9a7ddb0SJohannes Berg 	default:
1463d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1464d9a7ddb0SJohannes Berg 		return -EINVAL;
1465d9a7ddb0SJohannes Berg 	}
1466d9a7ddb0SJohannes Berg 
1467d9a7ddb0SJohannes Berg 	printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
1468d9a7ddb0SJohannes Berg 		sta->sdata->name, sta->sta.addr, new_state);
1469f09603a2SJohannes Berg 
1470f09603a2SJohannes Berg 	/*
1471f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1472f09603a2SJohannes Berg 	 * fail the transition
1473f09603a2SJohannes Berg 	 */
1474f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1475f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1476f09603a2SJohannes Berg 					sta->sta_state, new_state);
1477f09603a2SJohannes Berg 		if (err)
1478f09603a2SJohannes Berg 			return err;
1479f09603a2SJohannes Berg 	}
1480f09603a2SJohannes Berg 
1481f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1482f09603a2SJohannes Berg 
1483f09603a2SJohannes Berg 	switch (new_state) {
1484f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1485f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1486f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1487f09603a2SJohannes Berg 		break;
1488f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1489f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1490f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1491f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1492f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1493f09603a2SJohannes Berg 		break;
1494f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1495f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1496f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1497f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1498f09603a2SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1499f09603a2SJohannes Berg 				atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
1500f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1501f09603a2SJohannes Berg 		}
1502f09603a2SJohannes Berg 		break;
1503f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1504f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
1505f09603a2SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1506f09603a2SJohannes Berg 				atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
1507f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1508f09603a2SJohannes Berg 		}
1509f09603a2SJohannes Berg 		break;
1510f09603a2SJohannes Berg 	default:
1511f09603a2SJohannes Berg 		break;
1512f09603a2SJohannes Berg 	}
1513f09603a2SJohannes Berg 
1514d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1515d9a7ddb0SJohannes Berg 
1516d9a7ddb0SJohannes Berg 	return 0;
1517d9a7ddb0SJohannes Berg }
1518