xref: /openbmc/linux/net/mac80211/sta_info.c (revision 71ec375c75095002f36f083ceb32bbb8725734ae)
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 
296*71ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
297*71ec375cSJohannes 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 
35434e89507SJohannes Berg /*
3558c71df7aSGuy Eilam  * should be called with sta_mtx locked
3568c71df7aSGuy Eilam  * this function replaces the mutex lock
3578c71df7aSGuy Eilam  * with a RCU lock
3588c71df7aSGuy Eilam  */
3594d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
3608c71df7aSGuy Eilam {
3618c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
3628c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3632a33bee2SGuy Eilam 	struct sta_info *exist_sta;
3642a33bee2SGuy Eilam 	bool dummy_reinsert = false;
3658c71df7aSGuy Eilam 	int err = 0;
3668c71df7aSGuy Eilam 
3678c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
3688c71df7aSGuy Eilam 
3698c71df7aSGuy Eilam 	/*
3702a33bee2SGuy Eilam 	 * check if STA exists already.
3714d33960bSJohannes Berg 	 * only accept a scenario of a second call to sta_info_insert_finish
3722a33bee2SGuy Eilam 	 * with a dummy station entry that was inserted earlier
3732a33bee2SGuy Eilam 	 * in that case - assume that the dummy station flag should
3742a33bee2SGuy Eilam 	 * be removed.
3752a33bee2SGuy Eilam 	 */
3762a33bee2SGuy Eilam 	exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
3772a33bee2SGuy Eilam 	if (exist_sta) {
3782a33bee2SGuy Eilam 		if (exist_sta == sta && sta->dummy) {
3792a33bee2SGuy Eilam 			dummy_reinsert = true;
3802a33bee2SGuy Eilam 		} else {
3814d33960bSJohannes Berg 			err = -EEXIST;
3824d33960bSJohannes Berg 			goto out_err;
38334e89507SJohannes Berg 		}
3842a33bee2SGuy Eilam 	}
38534e89507SJohannes Berg 
3864d33960bSJohannes Berg 	if (!sta->dummy || dummy_reinsert) {
3874d33960bSJohannes Berg 		/* notify driver */
3884d33960bSJohannes Berg 		err = drv_sta_add(local, sdata, &sta->sta);
38934e89507SJohannes Berg 		if (err) {
3904d33960bSJohannes Berg 			if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
3914d33960bSJohannes Berg 				goto out_err;
3924d33960bSJohannes Berg 			printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
3934d33960bSJohannes Berg 					  "driver (%d) - keeping it anyway.\n",
3944d33960bSJohannes Berg 			       sdata->name, sta->sta.addr, err);
3954d33960bSJohannes Berg 		} else
3964d33960bSJohannes Berg 			sta->uploaded = true;
3974d33960bSJohannes Berg 	}
3984d33960bSJohannes Berg 
3994d33960bSJohannes Berg 	if (!dummy_reinsert) {
4004d33960bSJohannes Berg 		local->num_sta++;
4014d33960bSJohannes Berg 		local->sta_generation++;
4024d33960bSJohannes Berg 		smp_mb();
4034d33960bSJohannes Berg 
4044d33960bSJohannes Berg 		/* make the station visible */
4054d33960bSJohannes Berg 		sta_info_hash_add(local, sta);
4064d33960bSJohannes Berg 
4074d33960bSJohannes Berg 		list_add(&sta->list, &local->sta_list);
40883d5cc01SJohannes Berg 
40983d5cc01SJohannes Berg 		set_sta_flag(sta, WLAN_STA_INSERTED);
4104d33960bSJohannes Berg 	} else {
4114d33960bSJohannes Berg 		sta->dummy = false;
4124d33960bSJohannes Berg 	}
4134d33960bSJohannes Berg 
4144d33960bSJohannes Berg 	if (!sta->dummy) {
4154d33960bSJohannes Berg 		struct station_info sinfo;
4164d33960bSJohannes Berg 
4174d33960bSJohannes Berg 		ieee80211_sta_debugfs_add(sta);
4184d33960bSJohannes Berg 		rate_control_add_sta_debugfs(sta);
4194d33960bSJohannes Berg 
4204d33960bSJohannes Berg 		memset(&sinfo, 0, sizeof(sinfo));
4214d33960bSJohannes Berg 		sinfo.filled = 0;
4224d33960bSJohannes Berg 		sinfo.generation = local->sta_generation;
4234d33960bSJohannes Berg 		cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
42432bfd35dSJohannes Berg 	}
425d0709a65SJohannes Berg 
426f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4272a33bee2SGuy Eilam 	wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
4282a33bee2SGuy Eilam 			sta->dummy ? "dummy " : "", sta->sta.addr);
429f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
430f0706e82SJiri Benc 
43134e89507SJohannes Berg 	/* move reference to rcu-protected */
43234e89507SJohannes Berg 	rcu_read_lock();
43334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
434e9f207f0SJiri Benc 
43573651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
43673651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
43773651ee6SJohannes Berg 
43873651ee6SJohannes Berg 	return 0;
4394d33960bSJohannes Berg  out_err:
4404d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
4414d33960bSJohannes Berg 	rcu_read_lock();
4424d33960bSJohannes Berg 	return err;
4438c71df7aSGuy Eilam }
4448c71df7aSGuy Eilam 
4458c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
4468c71df7aSGuy Eilam {
4478c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4488c71df7aSGuy Eilam 	int err = 0;
4498c71df7aSGuy Eilam 
4504d33960bSJohannes Berg 	might_sleep();
4514d33960bSJohannes Berg 
4528c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
4538c71df7aSGuy Eilam 	if (err) {
4548c71df7aSGuy Eilam 		rcu_read_lock();
4558c71df7aSGuy Eilam 		goto out_free;
4568c71df7aSGuy Eilam 	}
4578c71df7aSGuy Eilam 
458004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
459004c872eSJohannes Berg 
4604d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
4618c71df7aSGuy Eilam 	if (err)
462004c872eSJohannes Berg 		goto out_free;
463d0709a65SJohannes Berg 
464004c872eSJohannes Berg 	return 0;
46593e5deb1SJohannes Berg  out_free:
46693e5deb1SJohannes Berg 	BUG_ON(!err);
467d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
46893e5deb1SJohannes Berg 	return err;
469f0706e82SJiri Benc }
470f0706e82SJiri Benc 
47134e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
47234e89507SJohannes Berg {
47334e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
47434e89507SJohannes Berg 
47534e89507SJohannes Berg 	rcu_read_unlock();
47634e89507SJohannes Berg 
47734e89507SJohannes Berg 	return err;
47834e89507SJohannes Berg }
47934e89507SJohannes Berg 
4802a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */
4812a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta)
4822a33bee2SGuy Eilam {
4832a33bee2SGuy Eilam 	struct ieee80211_local *local = sta->local;
4842a33bee2SGuy Eilam 	int err = 0;
4852a33bee2SGuy Eilam 
4862a33bee2SGuy Eilam 	err = sta_info_insert_check(sta);
4872a33bee2SGuy Eilam 	if (err) {
4882a33bee2SGuy Eilam 		mutex_unlock(&local->sta_mtx);
4892a33bee2SGuy Eilam 		return err;
4902a33bee2SGuy Eilam 	}
4912a33bee2SGuy Eilam 
4922a33bee2SGuy Eilam 	might_sleep();
4932a33bee2SGuy Eilam 
4944d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
4952a33bee2SGuy Eilam 	rcu_read_unlock();
4962a33bee2SGuy Eilam 	return err;
4972a33bee2SGuy Eilam }
4982a33bee2SGuy Eilam 
499f0706e82SJiri Benc static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
500004c872eSJohannes Berg {
501004c872eSJohannes Berg 	/*
502004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
503004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
504004c872eSJohannes Berg 	 */
505004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
506004c872eSJohannes Berg }
507004c872eSJohannes Berg 
508004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
509004c872eSJohannes Berg {
510004c872eSJohannes Berg 	/*
511004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
512004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
513004c872eSJohannes Berg 	 */
514004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
515004c872eSJohannes Berg }
516004c872eSJohannes Berg 
517948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
518004c872eSJohannes Berg {
519948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
520948d887dSJohannes Berg 	switch (ac) {
521948d887dSJohannes Berg 	case IEEE80211_AC_VO:
522948d887dSJohannes Berg 		return BIT(6) | BIT(7);
523948d887dSJohannes Berg 	case IEEE80211_AC_VI:
524948d887dSJohannes Berg 		return BIT(4) | BIT(5);
525948d887dSJohannes Berg 	case IEEE80211_AC_BE:
526948d887dSJohannes Berg 		return BIT(0) | BIT(3);
527948d887dSJohannes Berg 	case IEEE80211_AC_BK:
528948d887dSJohannes Berg 		return BIT(1) | BIT(2);
529948d887dSJohannes Berg 	default:
530948d887dSJohannes Berg 		WARN_ON(1);
531948d887dSJohannes Berg 		return 0;
532d0709a65SJohannes Berg 	}
533004c872eSJohannes Berg }
534004c872eSJohannes Berg 
535c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
536004c872eSJohannes Berg {
537c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
538c868cb35SJohannes Berg 	struct ieee80211_if_ap *bss = sta->sdata->bss;
539d0709a65SJohannes Berg 	unsigned long flags;
540948d887dSJohannes Berg 	bool indicate_tim = false;
541948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
542948d887dSJohannes Berg 	int ac;
543004c872eSJohannes Berg 
544c868cb35SJohannes Berg 	if (WARN_ON_ONCE(!sta->sdata->bss))
545c868cb35SJohannes Berg 		return;
5463e122be0SJohannes Berg 
547c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
548c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
549c868cb35SJohannes Berg 		return;
550004c872eSJohannes Berg 
551c868cb35SJohannes Berg 	if (sta->dead)
552c868cb35SJohannes Berg 		goto done;
5533e122be0SJohannes Berg 
554948d887dSJohannes Berg 	/*
555948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
556948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
557948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
558948d887dSJohannes Berg 	 * non-enabled ones.
559948d887dSJohannes Berg 	 */
560948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
561948d887dSJohannes Berg 		ignore_for_tim = 0;
562948d887dSJohannes Berg 
563948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
564948d887dSJohannes Berg 		unsigned long tids;
565948d887dSJohannes Berg 
566948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
567948d887dSJohannes Berg 			continue;
568948d887dSJohannes Berg 
569948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
570948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
571948d887dSJohannes Berg 		if (indicate_tim)
572948d887dSJohannes Berg 			break;
573948d887dSJohannes Berg 
574948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
575948d887dSJohannes Berg 
576948d887dSJohannes Berg 		indicate_tim |=
577948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
578004c872eSJohannes Berg 	}
579004c872eSJohannes Berg 
580c868cb35SJohannes Berg  done:
5814d33960bSJohannes Berg 	spin_lock_irqsave(&local->tim_lock, flags);
582004c872eSJohannes Berg 
583948d887dSJohannes Berg 	if (indicate_tim)
584c868cb35SJohannes Berg 		__bss_tim_set(bss, sta->sta.aid);
585c868cb35SJohannes Berg 	else
58617741cdcSJohannes Berg 		__bss_tim_clear(bss, sta->sta.aid);
5873e122be0SJohannes Berg 
588c868cb35SJohannes Berg 	if (local->ops->set_tim) {
589c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
590948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
591c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
592004c872eSJohannes Berg 	}
593004c872eSJohannes Berg 
5944d33960bSJohannes Berg 	spin_unlock_irqrestore(&local->tim_lock, flags);
595004c872eSJohannes Berg }
596004c872eSJohannes Berg 
597cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
598f0706e82SJiri Benc {
599e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
600f0706e82SJiri Benc 	int timeout;
601f0706e82SJiri Benc 
602f0706e82SJiri Benc 	if (!skb)
603cd0b8d89SJohannes Berg 		return false;
604f0706e82SJiri Benc 
605e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
606f0706e82SJiri Benc 
607f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
60857c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
60957c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
61057c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
611f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
612f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
613e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
614f0706e82SJiri Benc }
615f0706e82SJiri Benc 
616f0706e82SJiri Benc 
617948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
618948d887dSJohannes Berg 						struct sta_info *sta, int ac)
619f0706e82SJiri Benc {
620f0706e82SJiri Benc 	unsigned long flags;
621f0706e82SJiri Benc 	struct sk_buff *skb;
622f0706e82SJiri Benc 
62360750397SJohannes Berg 	/*
62460750397SJohannes Berg 	 * First check for frames that should expire on the filtered
62560750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
62660750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
62760750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
62860750397SJohannes Berg 	 * total_ps_buffered counter.
62960750397SJohannes Berg 	 */
630f0706e82SJiri Benc 	for (;;) {
631948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
632948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
63357c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
634948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
635836341a7SJohannes Berg 		else
636f0706e82SJiri Benc 			skb = NULL;
637948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
638f0706e82SJiri Benc 
63960750397SJohannes Berg 		/*
64060750397SJohannes Berg 		 * Frames are queued in order, so if this one
64160750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
64260750397SJohannes Berg 		 * we actually reached the end of the queue we
64360750397SJohannes Berg 		 * also need to stop, of course.
64460750397SJohannes Berg 		 */
64560750397SJohannes Berg 		if (!skb)
64660750397SJohannes Berg 			break;
64760750397SJohannes Berg 		dev_kfree_skb(skb);
64860750397SJohannes Berg 	}
64960750397SJohannes Berg 
65060750397SJohannes Berg 	/*
65160750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
65260750397SJohannes Berg 	 * only find something if the filtered queue was emptied
65360750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
65460750397SJohannes Berg 	 * buffered frames.
65560750397SJohannes Berg 	 */
656f0706e82SJiri Benc 	for (;;) {
657948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
658948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
659f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
660948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
661f0706e82SJiri Benc 		else
662f0706e82SJiri Benc 			skb = NULL;
663948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
664f0706e82SJiri Benc 
66560750397SJohannes Berg 		/*
66660750397SJohannes Berg 		 * frames are queued in order, so if this one
66760750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
66860750397SJohannes Berg 		 * the queue) we can stop testing
66960750397SJohannes Berg 		 */
670836341a7SJohannes Berg 		if (!skb)
671836341a7SJohannes Berg 			break;
672836341a7SJohannes Berg 
673f0706e82SJiri Benc 		local->total_ps_buffered--;
674f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
6750c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
6760c68ae26SJohannes Berg 		       sta->sta.addr);
677f4ea83ddSJohannes Berg #endif
678f0706e82SJiri Benc 		dev_kfree_skb(skb);
679f0706e82SJiri Benc 	}
6803393a608SJuuso Oikarinen 
68160750397SJohannes Berg 	/*
68260750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
68360750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
68460750397SJohannes Berg 	 * frames.
68560750397SJohannes Berg 	 */
68660750397SJohannes Berg 	sta_info_recalc_tim(sta);
68760750397SJohannes Berg 
68860750397SJohannes Berg 	/*
68960750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
69060750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
69160750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
69260750397SJohannes Berg 	 */
693948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
694948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
695948d887dSJohannes Berg }
696948d887dSJohannes Berg 
697948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
698948d887dSJohannes Berg 					     struct sta_info *sta)
699948d887dSJohannes Berg {
700948d887dSJohannes Berg 	bool have_buffered = false;
701948d887dSJohannes Berg 	int ac;
702948d887dSJohannes Berg 
703948d887dSJohannes Berg 	/* This is only necessary for stations on BSS interfaces */
704948d887dSJohannes Berg 	if (!sta->sdata->bss)
705948d887dSJohannes Berg 		return false;
706948d887dSJohannes Berg 
707948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
708948d887dSJohannes Berg 		have_buffered |=
709948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
710948d887dSJohannes Berg 
711948d887dSJohannes Berg 	return have_buffered;
712f0706e82SJiri Benc }
713f0706e82SJiri Benc 
71483d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
71534e89507SJohannes Berg {
71634e89507SJohannes Berg 	struct ieee80211_local *local;
71734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
718948d887dSJohannes Berg 	int ret, i, ac;
71942624d49SYogesh Ashok Powar 	struct tid_ampdu_tx *tid_tx;
72034e89507SJohannes Berg 
72134e89507SJohannes Berg 	might_sleep();
72234e89507SJohannes Berg 
72334e89507SJohannes Berg 	if (!sta)
72434e89507SJohannes Berg 		return -ENOENT;
72534e89507SJohannes Berg 
72634e89507SJohannes Berg 	local = sta->local;
72734e89507SJohannes Berg 	sdata = sta->sdata;
72834e89507SJohannes Berg 
72983d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
73083d5cc01SJohannes Berg 
731098a6070SJohannes Berg 	/*
732098a6070SJohannes Berg 	 * Before removing the station from the driver and
733098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
734098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
735098a6070SJohannes Berg 	 * will be sufficient.
736098a6070SJohannes Berg 	 */
737c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
73853f73c09SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, true);
739098a6070SJohannes Berg 
74034e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
74134e89507SJohannes Berg 	if (ret)
74234e89507SJohannes Berg 		return ret;
74334e89507SJohannes Berg 
7444d33960bSJohannes Berg 	list_del(&sta->list);
7454d33960bSJohannes Berg 
7468cb23153SJohannes Berg 	mutex_lock(&local->key_mtx);
747e31b8213SJohannes Berg 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
74840b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
749e31b8213SJohannes Berg 	if (sta->ptk)
75040b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
7518cb23153SJohannes Berg 	mutex_unlock(&local->key_mtx);
75234e89507SJohannes Berg 
75334e89507SJohannes Berg 	sta->dead = true;
75434e89507SJohannes Berg 
75534e89507SJohannes Berg 	local->num_sta--;
75634e89507SJohannes Berg 	local->sta_generation++;
75734e89507SJohannes Berg 
75834e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
759a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
76034e89507SJohannes Berg 
76183d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
76283d5cc01SJohannes Berg 		int err = sta_info_move_state(sta, sta->sta_state - 1);
76383d5cc01SJohannes Berg 		if (err) {
76483d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
76583d5cc01SJohannes Berg 			break;
76683d5cc01SJohannes Berg 		}
76783d5cc01SJohannes Berg 	}
768d9a7ddb0SJohannes Berg 
769077f4939SJohannes Berg 	if (sta->uploaded)
77034e89507SJohannes Berg 		drv_sta_remove(local, sdata, &sta->sta);
77134e89507SJohannes Berg 
772e64b3795SJohannes Berg 	/*
773e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
774e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
775e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
776e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
777e64b3795SJohannes Berg 	 */
778e64b3795SJohannes Berg 	synchronize_rcu();
779e64b3795SJohannes Berg 
7804f3eb0baSHelmut Schaa 	if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
7814f3eb0baSHelmut Schaa 		BUG_ON(!sdata->bss);
7824f3eb0baSHelmut Schaa 
7834f3eb0baSHelmut Schaa 		clear_sta_flag(sta, WLAN_STA_PS_STA);
7844f3eb0baSHelmut Schaa 
7854f3eb0baSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
7864f3eb0baSHelmut Schaa 		sta_info_recalc_tim(sta);
7874f3eb0baSHelmut Schaa 	}
7884f3eb0baSHelmut Schaa 
789948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
790948d887dSJohannes Berg 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
791948d887dSJohannes Berg 		__skb_queue_purge(&sta->ps_tx_buf[ac]);
792948d887dSJohannes Berg 		__skb_queue_purge(&sta->tx_filtered[ac]);
793948d887dSJohannes Berg 	}
794c868cb35SJohannes Berg 
79534e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
796e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
79734e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
79834e89507SJohannes Berg #endif
79934e89507SJohannes Berg 
80034e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
8010fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
80234e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
80334e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
80434e89507SJohannes Berg 
805ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
806ec15e68bSJouni Malinen 
80734e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
80834e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
80934e89507SJohannes Berg 
81034e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
81134e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
81234e89507SJohannes Berg 		mesh_plink_deactivate(sta);
81334e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
81434e89507SJohannes Berg 	}
81534e89507SJohannes Berg #endif
81634e89507SJohannes Berg 
817151a02f6SJohannes Berg 	/*
818151a02f6SJohannes Berg 	 * Destroy aggregation state here. It would be nice to wait for the
819151a02f6SJohannes Berg 	 * driver to finish aggregation stop and then clean up, but for now
820151a02f6SJohannes Berg 	 * drivers have to handle aggregation stop being requested, followed
821151a02f6SJohannes Berg 	 * directly by station destruction.
82242624d49SYogesh Ashok Powar 	 */
82342624d49SYogesh Ashok Powar 	for (i = 0; i < STA_TID_NUM; i++) {
824151a02f6SJohannes Berg 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
82527bf8882SYogesh Ashok Powar 		if (!tid_tx)
82642624d49SYogesh Ashok Powar 			continue;
82742624d49SYogesh Ashok Powar 		__skb_queue_purge(&tid_tx->pending);
828151a02f6SJohannes Berg 		kfree(tid_tx);
82942624d49SYogesh Ashok Powar 	}
83027bf8882SYogesh Ashok Powar 
831d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
83234e89507SJohannes Berg 
83334e89507SJohannes Berg 	return 0;
83434e89507SJohannes Berg }
83534e89507SJohannes Berg 
83634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
83734e89507SJohannes Berg {
83834e89507SJohannes Berg 	struct sta_info *sta;
83934e89507SJohannes Berg 	int ret;
84034e89507SJohannes Berg 
84134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8422a33bee2SGuy Eilam 	sta = sta_info_get_rx(sdata, addr);
84334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
84434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
84534e89507SJohannes Berg 
84634e89507SJohannes Berg 	return ret;
84734e89507SJohannes Berg }
84834e89507SJohannes Berg 
84934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
85034e89507SJohannes Berg 			      const u8 *addr)
85134e89507SJohannes Berg {
85234e89507SJohannes Berg 	struct sta_info *sta;
85334e89507SJohannes Berg 	int ret;
85434e89507SJohannes Berg 
85534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8562a33bee2SGuy Eilam 	sta = sta_info_get_bss_rx(sdata, addr);
85734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
85834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
85934e89507SJohannes Berg 
86034e89507SJohannes Berg 	return ret;
86134e89507SJohannes Berg }
862f0706e82SJiri Benc 
863f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
864f0706e82SJiri Benc {
865f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
866f0706e82SJiri Benc 	struct sta_info *sta;
8673393a608SJuuso Oikarinen 	bool timer_needed = false;
868f0706e82SJiri Benc 
869d0709a65SJohannes Berg 	rcu_read_lock();
870d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
8713393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
8723393a608SJuuso Oikarinen 			timer_needed = true;
873d0709a65SJohannes Berg 	rcu_read_unlock();
874f0706e82SJiri Benc 
8755bb644a0SJohannes Berg 	if (local->quiescing)
8765bb644a0SJohannes Berg 		return;
8775bb644a0SJohannes Berg 
8783393a608SJuuso Oikarinen 	if (!timer_needed)
8793393a608SJuuso Oikarinen 		return;
8803393a608SJuuso Oikarinen 
88126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
88226d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
883f0706e82SJiri Benc }
884f0706e82SJiri Benc 
885f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
886f0706e82SJiri Benc {
8874d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
88834e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
889f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
890f0706e82SJiri Benc 
891b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
892b24b8a24SPavel Emelyanov 		    (unsigned long)local);
893f0706e82SJiri Benc }
894f0706e82SJiri Benc 
895f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
896f0706e82SJiri Benc {
897f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
898be8755e1SMichael Wu 	sta_info_flush(local, NULL);
899f0706e82SJiri Benc }
900f0706e82SJiri Benc 
901f0706e82SJiri Benc /**
902f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
90344213b5eSJohannes Berg  *
90444213b5eSJohannes Berg  * Returns the number of removed STA entries.
90544213b5eSJohannes Berg  *
906f0706e82SJiri Benc  * @local: local interface data
907d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
908f0706e82SJiri Benc  */
90944213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
910d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
911f0706e82SJiri Benc {
912f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
91344213b5eSJohannes Berg 	int ret = 0;
914f0706e82SJiri Benc 
915d0709a65SJohannes Berg 	might_sleep();
916d0709a65SJohannes Berg 
91734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
91834e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
91934e89507SJohannes Berg 		if (!sdata || sdata == sta->sdata)
92034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
92134e89507SJohannes Berg 	}
92234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
92344213b5eSJohannes Berg 
92444213b5eSJohannes Berg 	return ret;
925f0706e82SJiri Benc }
926dc6676b7SJohannes Berg 
92724723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
92824723d1bSJohannes Berg 			  unsigned long exp_time)
92924723d1bSJohannes Berg {
93024723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
93124723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
93224723d1bSJohannes Berg 
93334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
934e46a2cf9SMohammed Shafi Shajakhan 
935e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
936ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
937ec2b774eSMarek Lindner 			continue;
938ec2b774eSMarek Lindner 
93924723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
94024723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
9410c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
94247846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
94324723d1bSJohannes Berg #endif
94434e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
94524723d1bSJohannes Berg 		}
946e46a2cf9SMohammed Shafi Shajakhan 	}
947e46a2cf9SMohammed Shafi Shajakhan 
94834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
94924723d1bSJohannes Berg }
95017741cdcSJohannes Berg 
951686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
952686b9cb9SBen Greear 					       const u8 *addr,
953686b9cb9SBen Greear 					       const u8 *localaddr)
95417741cdcSJohannes Berg {
955abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
95617741cdcSJohannes Berg 
957686b9cb9SBen Greear 	/*
958686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
959686b9cb9SBen Greear 	 * ... first in list.
960686b9cb9SBen Greear 	 */
961f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
962686b9cb9SBen Greear 		if (localaddr &&
963686b9cb9SBen Greear 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
964686b9cb9SBen Greear 			continue;
965f7c65594SJohannes Berg 		if (!sta->uploaded)
966f7c65594SJohannes Berg 			return NULL;
96717741cdcSJohannes Berg 		return &sta->sta;
968f7c65594SJohannes Berg 	}
969f7c65594SJohannes Berg 
970abe60632SJohannes Berg 	return NULL;
97117741cdcSJohannes Berg }
972686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
9735ed176e1SJohannes Berg 
9745ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
9755ed176e1SJohannes Berg 					 const u8 *addr)
9765ed176e1SJohannes Berg {
977f7c65594SJohannes Berg 	struct sta_info *sta;
9785ed176e1SJohannes Berg 
9795ed176e1SJohannes Berg 	if (!vif)
9805ed176e1SJohannes Berg 		return NULL;
9815ed176e1SJohannes Berg 
982f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
983f7c65594SJohannes Berg 	if (!sta)
984f7c65594SJohannes Berg 		return NULL;
9855ed176e1SJohannes Berg 
986f7c65594SJohannes Berg 	if (!sta->uploaded)
987f7c65594SJohannes Berg 		return NULL;
988f7c65594SJohannes Berg 
989f7c65594SJohannes Berg 	return &sta->sta;
9905ed176e1SJohannes Berg }
99117741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
992af818581SJohannes Berg 
99350a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
99450a9432dSJohannes Berg {
99550a9432dSJohannes Berg 	struct sta_info *sta = _sta;
996608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
99750a9432dSJohannes Berg 
998c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
999608383bfSHelmut Schaa 	if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
1000608383bfSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
100150a9432dSJohannes Berg }
100250a9432dSJohannes Berg 
1003af818581SJohannes Berg /* powersave support code */
1004af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1005af818581SJohannes Berg {
1006af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1007af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1008948d887dSJohannes Berg 	struct sk_buff_head pending;
1009948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
1010af818581SJohannes Berg 
1011c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
101247086fc5SJohannes Berg 
1013948d887dSJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
1014948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1015948d887dSJohannes Berg 
1016d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
101712375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1018af818581SJohannes Berg 
1019948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1020af818581SJohannes Berg 
1021af818581SJohannes Berg 	/* Send all buffered frames to the station */
1022948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1023948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1024948d887dSJohannes Berg 
1025948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1026948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1027948d887dSJohannes Berg 		filtered += tmp - count;
1028948d887dSJohannes Berg 		count = tmp;
1029948d887dSJohannes Berg 
1030948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1031948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1032948d887dSJohannes Berg 		buffered += tmp - count;
1033948d887dSJohannes Berg 	}
1034948d887dSJohannes Berg 
1035948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1036948d887dSJohannes Berg 
1037af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1038af818581SJohannes Berg 
1039c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1040c868cb35SJohannes Berg 
1041af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1042af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
104347846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
1044948d887dSJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1045af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1046af818581SJohannes Berg }
1047af818581SJohannes Berg 
1048ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1049ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
105040b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1051ce662b44SJohannes Berg {
1052ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1053ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1054ce662b44SJohannes Berg 	struct sk_buff *skb;
1055ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1056ce662b44SJohannes Berg 	__le16 fc;
1057c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1058ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
1059ce662b44SJohannes Berg 
1060ce662b44SJohannes Berg 	if (qos) {
1061ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1062ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1063ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1064ce662b44SJohannes Berg 	} else {
1065ce662b44SJohannes Berg 		size -= 2;
1066ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1067ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1068ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1069ce662b44SJohannes Berg 	}
1070ce662b44SJohannes Berg 
1071ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1072ce662b44SJohannes Berg 	if (!skb)
1073ce662b44SJohannes Berg 		return;
1074ce662b44SJohannes Berg 
1075ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1076ce662b44SJohannes Berg 
1077ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1078ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1079ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1080ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1081ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1082ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1083ce662b44SJohannes Berg 
1084ce662b44SJohannes Berg 	skb->priority = tid;
1085ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
108659b66255SJohannes Berg 	if (qos) {
1087ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1088ce662b44SJohannes Berg 
108940b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1090ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1091ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1092ce662b44SJohannes Berg 	}
1093ce662b44SJohannes Berg 
1094ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1095ce662b44SJohannes Berg 
1096ce662b44SJohannes Berg 	/*
1097ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1098ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1099deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1100deeaee19SJohannes Berg 	 * ends the poll/service period.
1101ce662b44SJohannes Berg 	 */
1102deeaee19SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
1103deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1104ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1105ce662b44SJohannes Berg 
110640b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
110740b96408SJohannes Berg 
1108ce662b44SJohannes Berg 	ieee80211_xmit(sdata, skb);
1109ce662b44SJohannes Berg }
1110ce662b44SJohannes Berg 
111147086fc5SJohannes Berg static void
111247086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
111347086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
111447086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1115af818581SJohannes Berg {
1116af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1117af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
11184049e09aSJohannes Berg 	bool found = false;
1119948d887dSJohannes Berg 	bool more_data = false;
1120948d887dSJohannes Berg 	int ac;
11214049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
112247086fc5SJohannes Berg 	struct sk_buff_head frames;
112347086fc5SJohannes Berg 
1124deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1125c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1126deeaee19SJohannes Berg 
112747086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1128af818581SJohannes Berg 
1129948d887dSJohannes Berg 	/*
113047086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1131948d887dSJohannes Berg 	 */
1132948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
11334049e09aSJohannes Berg 		unsigned long tids;
11344049e09aSJohannes Berg 
113547086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1136948d887dSJohannes Berg 			continue;
1137948d887dSJohannes Berg 
11384049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
11394049e09aSJohannes Berg 
11404049e09aSJohannes Berg 		if (!found) {
11414049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
11424049e09aSJohannes Berg 			if (driver_release_tids) {
11434049e09aSJohannes Berg 				found = true;
11444049e09aSJohannes Berg 			} else {
114547086fc5SJohannes Berg 				struct sk_buff *skb;
114647086fc5SJohannes Berg 
114747086fc5SJohannes Berg 				while (n_frames > 0) {
1148948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1149948d887dSJohannes Berg 					if (!skb) {
115047086fc5SJohannes Berg 						skb = skb_dequeue(
115147086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1152af818581SJohannes Berg 						if (skb)
1153af818581SJohannes Berg 							local->total_ps_buffered--;
1154af818581SJohannes Berg 					}
115547086fc5SJohannes Berg 					if (!skb)
115647086fc5SJohannes Berg 						break;
115747086fc5SJohannes Berg 					n_frames--;
11584049e09aSJohannes Berg 					found = true;
115947086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
116047086fc5SJohannes Berg 				}
1161948d887dSJohannes Berg 			}
1162948d887dSJohannes Berg 
11634049e09aSJohannes Berg 			/*
11644049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
11654049e09aSJohannes Berg 			 * certainly there's more data if we release just a
11664049e09aSJohannes Berg 			 * single frame now (from a single TID).
11674049e09aSJohannes Berg 			 */
116847086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
116947086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
11704049e09aSJohannes Berg 				more_data = true;
11714049e09aSJohannes Berg 				driver_release_tids =
11724049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
11734049e09aSJohannes Berg 				break;
11744049e09aSJohannes Berg 			}
11754049e09aSJohannes Berg 		}
1176948d887dSJohannes Berg 
1177948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1178948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1179948d887dSJohannes Berg 			more_data = true;
1180948d887dSJohannes Berg 			break;
1181948d887dSJohannes Berg 		}
1182948d887dSJohannes Berg 	}
1183af818581SJohannes Berg 
11844049e09aSJohannes Berg 	if (!found) {
1185ce662b44SJohannes Berg 		int tid;
11864049e09aSJohannes Berg 
1187ce662b44SJohannes Berg 		/*
1188ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1189ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1190ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1191ce662b44SJohannes Berg 		 *
1192ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1193ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1194ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1195ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1196ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1197ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1198ce662b44SJohannes Berg 		 *
1199ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1200ce662b44SJohannes Berg 		 */
1201ce662b44SJohannes Berg 
1202ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1203ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1204ce662b44SJohannes Berg 
120540b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
12064049e09aSJohannes Berg 		return;
12074049e09aSJohannes Berg 	}
12084049e09aSJohannes Berg 
120947086fc5SJohannes Berg 	if (!driver_release_tids) {
121047086fc5SJohannes Berg 		struct sk_buff_head pending;
121147086fc5SJohannes Berg 		struct sk_buff *skb;
121240b96408SJohannes Berg 		int num = 0;
121340b96408SJohannes Berg 		u16 tids = 0;
121447086fc5SJohannes Berg 
121547086fc5SJohannes Berg 		skb_queue_head_init(&pending);
121647086fc5SJohannes Berg 
121747086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1218af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
121947086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
122040b96408SJohannes Berg 			u8 *qoshdr = NULL;
122140b96408SJohannes Berg 
122240b96408SJohannes Berg 			num++;
1223af818581SJohannes Berg 
1224af818581SJohannes Berg 			/*
122547086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
122647086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
122747086fc5SJohannes Berg 			 * exchange.
1228af818581SJohannes Berg 			 */
122947086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
1230af818581SJohannes Berg 
123147086fc5SJohannes Berg 			/*
123247086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
123347086fc5SJohannes Berg 			 * more buffered frames for this STA
123447086fc5SJohannes Berg 			 */
123524b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
123647086fc5SJohannes Berg 				hdr->frame_control |=
123747086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
123824b9c373SJanusz.Dziedzic@tieto.com 			else
123924b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
124024b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1241af818581SJohannes Berg 
124240b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
124340b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
124440b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
124540b96408SJohannes Berg 
124647086fc5SJohannes Berg 			/* set EOSP for the frame */
124740b96408SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
124840b96408SJohannes Berg 			    qoshdr && skb_queue_empty(&frames))
124940b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1250deeaee19SJohannes Berg 
125147086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_STATUS_EOSP |
125247086fc5SJohannes Berg 				       IEEE80211_TX_CTL_REQ_TX_STATUS;
125347086fc5SJohannes Berg 
125440b96408SJohannes Berg 			if (qoshdr)
125540b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
125640b96408SJohannes Berg 			else
125740b96408SJohannes Berg 				tids |= BIT(0);
125840b96408SJohannes Berg 
125947086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
126047086fc5SJohannes Berg 		}
126147086fc5SJohannes Berg 
126240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
126340b96408SJohannes Berg 					  reason, more_data);
126440b96408SJohannes Berg 
126547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1266af818581SJohannes Berg 
1267c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1268af818581SJohannes Berg 	} else {
1269af818581SJohannes Berg 		/*
12704049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
12714049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
12724049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
12734049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
12744049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
12754049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
12764049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
12774049e09aSJohannes Berg 		 * needs to be set anyway.
1278af818581SJohannes Berg 		 */
12794049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
128047086fc5SJohannes Berg 					    n_frames, reason, more_data);
12814049e09aSJohannes Berg 
12824049e09aSJohannes Berg 		/*
12834049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
12844049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
12854049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
12864049e09aSJohannes Berg 		 * release function.
12874049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
12884049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
12894049e09aSJohannes Berg 		 */
1290af818581SJohannes Berg 	}
1291af818581SJohannes Berg }
1292af818581SJohannes Berg 
1293af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1294af818581SJohannes Berg {
129547086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1296af818581SJohannes Berg 
1297af818581SJohannes Berg 	/*
129847086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
129947086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
130047086fc5SJohannes Berg 	 * only from the non-enabled ones.
1301af818581SJohannes Berg 	 */
130247086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
130347086fc5SJohannes Berg 		ignore_for_response = 0;
1304af818581SJohannes Berg 
130547086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
130647086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1307af818581SJohannes Berg }
130847086fc5SJohannes Berg 
130947086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
131047086fc5SJohannes Berg {
131147086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
131247086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
131347086fc5SJohannes Berg 
131447086fc5SJohannes Berg 	/*
131547086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
131647086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
131747086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
131847086fc5SJohannes Berg 	 * actually getting called.
131947086fc5SJohannes Berg 	 */
132047086fc5SJohannes Berg 	if (!delivery_enabled)
132147086fc5SJohannes Berg 		return;
132247086fc5SJohannes Berg 
132347086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
132447086fc5SJohannes Berg 	case 1:
132547086fc5SJohannes Berg 		n_frames = 2;
132647086fc5SJohannes Berg 		break;
132747086fc5SJohannes Berg 	case 2:
132847086fc5SJohannes Berg 		n_frames = 4;
132947086fc5SJohannes Berg 		break;
133047086fc5SJohannes Berg 	case 3:
133147086fc5SJohannes Berg 		n_frames = 6;
133247086fc5SJohannes Berg 		break;
133347086fc5SJohannes Berg 	case 0:
133447086fc5SJohannes Berg 		/* XXX: what is a good value? */
133547086fc5SJohannes Berg 		n_frames = 8;
133647086fc5SJohannes Berg 		break;
133747086fc5SJohannes Berg 	}
133847086fc5SJohannes Berg 
133947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
134047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1341af818581SJohannes Berg }
1342af818581SJohannes Berg 
1343af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1344af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1345af818581SJohannes Berg {
1346af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1347af818581SJohannes Berg 
1348b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1349b5878a2dSJohannes Berg 
1350af818581SJohannes Berg 	if (block)
1351c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1352c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1353af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1354af818581SJohannes Berg }
1355af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1356dcf55fb5SFelix Fietkau 
135737fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
135837fbd908SJohannes Berg {
135937fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
136037fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
136137fbd908SJohannes Berg 	struct sk_buff *skb;
136237fbd908SJohannes Berg 	struct skb_eosp_msg_data *data;
136337fbd908SJohannes Berg 
136437fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
136537fbd908SJohannes Berg 
136637fbd908SJohannes Berg 	skb = alloc_skb(0, GFP_ATOMIC);
136737fbd908SJohannes Berg 	if (!skb) {
136837fbd908SJohannes Berg 		/* too bad ... but race is better than loss */
136937fbd908SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_SP);
137037fbd908SJohannes Berg 		return;
137137fbd908SJohannes Berg 	}
137237fbd908SJohannes Berg 
137337fbd908SJohannes Berg 	data = (void *)skb->cb;
137437fbd908SJohannes Berg 	memcpy(data->sta, pubsta->addr, ETH_ALEN);
137537fbd908SJohannes Berg 	memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
137637fbd908SJohannes Berg 	skb->pkt_type = IEEE80211_EOSP_MSG;
137737fbd908SJohannes Berg 	skb_queue_tail(&local->skb_queue, skb);
137837fbd908SJohannes Berg 	tasklet_schedule(&local->tasklet);
137937fbd908SJohannes Berg }
138037fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
138137fbd908SJohannes Berg 
1382042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1383042ec453SJohannes Berg 				u8 tid, bool buffered)
1384dcf55fb5SFelix Fietkau {
1385dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1386dcf55fb5SFelix Fietkau 
1387948d887dSJohannes Berg 	if (WARN_ON(tid >= STA_TID_NUM))
1388042ec453SJohannes Berg 		return;
1389042ec453SJohannes Berg 
1390948d887dSJohannes Berg 	if (buffered)
1391948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1392948d887dSJohannes Berg 	else
1393948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1394948d887dSJohannes Berg 
1395c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1396dcf55fb5SFelix Fietkau }
1397042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1398d9a7ddb0SJohannes Berg 
139983d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1400d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1401d9a7ddb0SJohannes Berg {
14028bf11d8dSJohannes Berg 	might_sleep();
1403d9a7ddb0SJohannes Berg 
1404d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1405d9a7ddb0SJohannes Berg 		return 0;
1406d9a7ddb0SJohannes Berg 
1407d9a7ddb0SJohannes Berg 	switch (new_state) {
1408d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1409d9a7ddb0SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1410d9a7ddb0SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1411d9a7ddb0SJohannes Berg 		else
1412d9a7ddb0SJohannes Berg 			return -EINVAL;
1413d9a7ddb0SJohannes Berg 		break;
1414d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1415d9a7ddb0SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1416d9a7ddb0SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1417d9a7ddb0SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1418d9a7ddb0SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1419d9a7ddb0SJohannes Berg 		else
1420d9a7ddb0SJohannes Berg 			return -EINVAL;
1421d9a7ddb0SJohannes Berg 		break;
1422d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
142329623892SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1424d9a7ddb0SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
142529623892SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
142629623892SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
142729623892SJohannes Berg 				atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
1428d9a7ddb0SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
142929623892SJohannes Berg 		} else
1430d9a7ddb0SJohannes Berg 			return -EINVAL;
1431d9a7ddb0SJohannes Berg 		break;
1432d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
143329623892SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
143429623892SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
143529623892SJohannes Berg 				atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
1436d9a7ddb0SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
143729623892SJohannes Berg 		} else
1438d9a7ddb0SJohannes Berg 			return -EINVAL;
1439d9a7ddb0SJohannes Berg 		break;
1440d9a7ddb0SJohannes Berg 	default:
1441d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1442d9a7ddb0SJohannes Berg 		return -EINVAL;
1443d9a7ddb0SJohannes Berg 	}
1444d9a7ddb0SJohannes Berg 
1445d9a7ddb0SJohannes Berg 	printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
1446d9a7ddb0SJohannes Berg 		sta->sdata->name, sta->sta.addr, new_state);
1447d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1448d9a7ddb0SJohannes Berg 
1449d9a7ddb0SJohannes Berg 	return 0;
1450d9a7ddb0SJohannes Berg }
1451