xref: /openbmc/linux/net/mac80211/sta_info.c (revision 83d5cc012441531ab0bf6f99881958e964e9cf11)
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 
296ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
297ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
298541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
299541a45a1SBruno Randolf 
300af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
301f0706e82SJiri Benc 		kfree(sta);
30273651ee6SJohannes Berg 		return NULL;
303f0706e82SJiri Benc 	}
304f0706e82SJiri Benc 
30516c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
306a622ab72SJohannes Berg 		/*
307a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
308a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
309a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
310a622ab72SJohannes Berg 		 */
31116c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
31216c5f15cSRon Rindjunsky 	}
313948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
314948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
315948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
316948d887dSJohannes Berg 	}
31773651ee6SJohannes Berg 
318cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
3194be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
320cccaec98SSenthil Balasubramanian 
32173651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3220fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
32373651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
32473651ee6SJohannes Berg 
32503e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
32657cf8043SJavier Cardona 	sta->plink_state = NL80211_PLINK_LISTEN;
32703e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
32803e4497eSJohannes Berg #endif
32903e4497eSJohannes Berg 
33073651ee6SJohannes Berg 	return sta;
33173651ee6SJohannes Berg }
33273651ee6SJohannes Berg 
3338c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
33434e89507SJohannes Berg {
33534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
33634e89507SJohannes Berg 
33703e4497eSJohannes Berg 	/*
33803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
33903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
34003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
34103e4497eSJohannes Berg 	 */
3428c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
3438c71df7aSGuy Eilam 		return -ENETDOWN;
34403e4497eSJohannes Berg 
34547846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
3468c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
3478c71df7aSGuy Eilam 		return -EINVAL;
3488c71df7aSGuy Eilam 
3498c71df7aSGuy Eilam 	return 0;
35093e5deb1SJohannes Berg }
35144213b5eSJohannes Berg 
35234e89507SJohannes Berg /*
3538c71df7aSGuy Eilam  * should be called with sta_mtx locked
3548c71df7aSGuy Eilam  * this function replaces the mutex lock
3558c71df7aSGuy Eilam  * with a RCU lock
3568c71df7aSGuy Eilam  */
3574d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
3588c71df7aSGuy Eilam {
3598c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
3608c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3612a33bee2SGuy Eilam 	struct sta_info *exist_sta;
3622a33bee2SGuy Eilam 	bool dummy_reinsert = false;
3638c71df7aSGuy Eilam 	int err = 0;
3648c71df7aSGuy Eilam 
3658c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
3668c71df7aSGuy Eilam 
3678c71df7aSGuy Eilam 	/*
3682a33bee2SGuy Eilam 	 * check if STA exists already.
3694d33960bSJohannes Berg 	 * only accept a scenario of a second call to sta_info_insert_finish
3702a33bee2SGuy Eilam 	 * with a dummy station entry that was inserted earlier
3712a33bee2SGuy Eilam 	 * in that case - assume that the dummy station flag should
3722a33bee2SGuy Eilam 	 * be removed.
3732a33bee2SGuy Eilam 	 */
3742a33bee2SGuy Eilam 	exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
3752a33bee2SGuy Eilam 	if (exist_sta) {
3762a33bee2SGuy Eilam 		if (exist_sta == sta && sta->dummy) {
3772a33bee2SGuy Eilam 			dummy_reinsert = true;
3782a33bee2SGuy Eilam 		} else {
3794d33960bSJohannes Berg 			err = -EEXIST;
3804d33960bSJohannes Berg 			goto out_err;
38134e89507SJohannes Berg 		}
3822a33bee2SGuy Eilam 	}
38334e89507SJohannes Berg 
3844d33960bSJohannes Berg 	if (!sta->dummy || dummy_reinsert) {
3854d33960bSJohannes Berg 		/* notify driver */
3864d33960bSJohannes Berg 		err = drv_sta_add(local, sdata, &sta->sta);
38734e89507SJohannes Berg 		if (err) {
3884d33960bSJohannes Berg 			if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
3894d33960bSJohannes Berg 				goto out_err;
3904d33960bSJohannes Berg 			printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
3914d33960bSJohannes Berg 					  "driver (%d) - keeping it anyway.\n",
3924d33960bSJohannes Berg 			       sdata->name, sta->sta.addr, err);
3934d33960bSJohannes Berg 		} else
3944d33960bSJohannes Berg 			sta->uploaded = true;
3954d33960bSJohannes Berg 	}
3964d33960bSJohannes Berg 
3974d33960bSJohannes Berg 	if (!dummy_reinsert) {
3984d33960bSJohannes Berg 		local->num_sta++;
3994d33960bSJohannes Berg 		local->sta_generation++;
4004d33960bSJohannes Berg 		smp_mb();
4014d33960bSJohannes Berg 
4024d33960bSJohannes Berg 		/* make the station visible */
4034d33960bSJohannes Berg 		sta_info_hash_add(local, sta);
4044d33960bSJohannes Berg 
4054d33960bSJohannes Berg 		list_add(&sta->list, &local->sta_list);
406*83d5cc01SJohannes Berg 
407*83d5cc01SJohannes Berg 		set_sta_flag(sta, WLAN_STA_INSERTED);
4084d33960bSJohannes Berg 	} else {
4094d33960bSJohannes Berg 		sta->dummy = false;
4104d33960bSJohannes Berg 	}
4114d33960bSJohannes Berg 
4124d33960bSJohannes Berg 	if (!sta->dummy) {
4134d33960bSJohannes Berg 		struct station_info sinfo;
4144d33960bSJohannes Berg 
4154d33960bSJohannes Berg 		ieee80211_sta_debugfs_add(sta);
4164d33960bSJohannes Berg 		rate_control_add_sta_debugfs(sta);
4174d33960bSJohannes Berg 
4184d33960bSJohannes Berg 		memset(&sinfo, 0, sizeof(sinfo));
4194d33960bSJohannes Berg 		sinfo.filled = 0;
4204d33960bSJohannes Berg 		sinfo.generation = local->sta_generation;
4214d33960bSJohannes Berg 		cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
42232bfd35dSJohannes Berg 	}
423d0709a65SJohannes Berg 
424f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4252a33bee2SGuy Eilam 	wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
4262a33bee2SGuy Eilam 			sta->dummy ? "dummy " : "", sta->sta.addr);
427f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
428f0706e82SJiri Benc 
42934e89507SJohannes Berg 	/* move reference to rcu-protected */
43034e89507SJohannes Berg 	rcu_read_lock();
43134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
432e9f207f0SJiri Benc 
43373651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
43473651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
43573651ee6SJohannes Berg 
43673651ee6SJohannes Berg 	return 0;
4374d33960bSJohannes Berg  out_err:
4384d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
4394d33960bSJohannes Berg 	rcu_read_lock();
4404d33960bSJohannes Berg 	return err;
4418c71df7aSGuy Eilam }
4428c71df7aSGuy Eilam 
4438c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
4448c71df7aSGuy Eilam {
4458c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4468c71df7aSGuy Eilam 	int err = 0;
4478c71df7aSGuy Eilam 
4484d33960bSJohannes Berg 	might_sleep();
4494d33960bSJohannes Berg 
4508c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
4518c71df7aSGuy Eilam 	if (err) {
4528c71df7aSGuy Eilam 		rcu_read_lock();
4538c71df7aSGuy Eilam 		goto out_free;
4548c71df7aSGuy Eilam 	}
4558c71df7aSGuy Eilam 
456004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
457004c872eSJohannes Berg 
4584d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
4598c71df7aSGuy Eilam 	if (err)
460004c872eSJohannes Berg 		goto out_free;
461d0709a65SJohannes Berg 
462004c872eSJohannes Berg 	return 0;
46393e5deb1SJohannes Berg  out_free:
46493e5deb1SJohannes Berg 	BUG_ON(!err);
465d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
46693e5deb1SJohannes Berg 	return err;
467f0706e82SJiri Benc }
468f0706e82SJiri Benc 
46934e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
47034e89507SJohannes Berg {
47134e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
47234e89507SJohannes Berg 
47334e89507SJohannes Berg 	rcu_read_unlock();
47434e89507SJohannes Berg 
47534e89507SJohannes Berg 	return err;
47634e89507SJohannes Berg }
47734e89507SJohannes Berg 
4782a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */
4792a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta)
4802a33bee2SGuy Eilam {
4812a33bee2SGuy Eilam 	struct ieee80211_local *local = sta->local;
4822a33bee2SGuy Eilam 	int err = 0;
4832a33bee2SGuy Eilam 
4842a33bee2SGuy Eilam 	err = sta_info_insert_check(sta);
4852a33bee2SGuy Eilam 	if (err) {
4862a33bee2SGuy Eilam 		mutex_unlock(&local->sta_mtx);
4872a33bee2SGuy Eilam 		return err;
4882a33bee2SGuy Eilam 	}
4892a33bee2SGuy Eilam 
4902a33bee2SGuy Eilam 	might_sleep();
4912a33bee2SGuy Eilam 
4924d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
4932a33bee2SGuy Eilam 	rcu_read_unlock();
4942a33bee2SGuy Eilam 	return err;
4952a33bee2SGuy Eilam }
4962a33bee2SGuy Eilam 
497f0706e82SJiri Benc static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
498004c872eSJohannes Berg {
499004c872eSJohannes Berg 	/*
500004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
501004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
502004c872eSJohannes Berg 	 */
503004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
504004c872eSJohannes Berg }
505004c872eSJohannes Berg 
506004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
507004c872eSJohannes Berg {
508004c872eSJohannes Berg 	/*
509004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
510004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
511004c872eSJohannes Berg 	 */
512004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
513004c872eSJohannes Berg }
514004c872eSJohannes Berg 
515948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
516004c872eSJohannes Berg {
517948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
518948d887dSJohannes Berg 	switch (ac) {
519948d887dSJohannes Berg 	case IEEE80211_AC_VO:
520948d887dSJohannes Berg 		return BIT(6) | BIT(7);
521948d887dSJohannes Berg 	case IEEE80211_AC_VI:
522948d887dSJohannes Berg 		return BIT(4) | BIT(5);
523948d887dSJohannes Berg 	case IEEE80211_AC_BE:
524948d887dSJohannes Berg 		return BIT(0) | BIT(3);
525948d887dSJohannes Berg 	case IEEE80211_AC_BK:
526948d887dSJohannes Berg 		return BIT(1) | BIT(2);
527948d887dSJohannes Berg 	default:
528948d887dSJohannes Berg 		WARN_ON(1);
529948d887dSJohannes Berg 		return 0;
530d0709a65SJohannes Berg 	}
531004c872eSJohannes Berg }
532004c872eSJohannes Berg 
533c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
534004c872eSJohannes Berg {
535c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
536c868cb35SJohannes Berg 	struct ieee80211_if_ap *bss = sta->sdata->bss;
537d0709a65SJohannes Berg 	unsigned long flags;
538948d887dSJohannes Berg 	bool indicate_tim = false;
539948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
540948d887dSJohannes Berg 	int ac;
541004c872eSJohannes Berg 
542c868cb35SJohannes Berg 	if (WARN_ON_ONCE(!sta->sdata->bss))
543c868cb35SJohannes Berg 		return;
5443e122be0SJohannes Berg 
545c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
546c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
547c868cb35SJohannes Berg 		return;
548004c872eSJohannes Berg 
549c868cb35SJohannes Berg 	if (sta->dead)
550c868cb35SJohannes Berg 		goto done;
5513e122be0SJohannes Berg 
552948d887dSJohannes Berg 	/*
553948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
554948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
555948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
556948d887dSJohannes Berg 	 * non-enabled ones.
557948d887dSJohannes Berg 	 */
558948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
559948d887dSJohannes Berg 		ignore_for_tim = 0;
560948d887dSJohannes Berg 
561948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
562948d887dSJohannes Berg 		unsigned long tids;
563948d887dSJohannes Berg 
564948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
565948d887dSJohannes Berg 			continue;
566948d887dSJohannes Berg 
567948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
568948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
569948d887dSJohannes Berg 		if (indicate_tim)
570948d887dSJohannes Berg 			break;
571948d887dSJohannes Berg 
572948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
573948d887dSJohannes Berg 
574948d887dSJohannes Berg 		indicate_tim |=
575948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
576004c872eSJohannes Berg 	}
577004c872eSJohannes Berg 
578c868cb35SJohannes Berg  done:
5794d33960bSJohannes Berg 	spin_lock_irqsave(&local->tim_lock, flags);
580004c872eSJohannes Berg 
581948d887dSJohannes Berg 	if (indicate_tim)
582c868cb35SJohannes Berg 		__bss_tim_set(bss, sta->sta.aid);
583c868cb35SJohannes Berg 	else
58417741cdcSJohannes Berg 		__bss_tim_clear(bss, sta->sta.aid);
5853e122be0SJohannes Berg 
586c868cb35SJohannes Berg 	if (local->ops->set_tim) {
587c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
588948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
589c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
590004c872eSJohannes Berg 	}
591004c872eSJohannes Berg 
5924d33960bSJohannes Berg 	spin_unlock_irqrestore(&local->tim_lock, flags);
593004c872eSJohannes Berg }
594004c872eSJohannes Berg 
595cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
596f0706e82SJiri Benc {
597e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
598f0706e82SJiri Benc 	int timeout;
599f0706e82SJiri Benc 
600f0706e82SJiri Benc 	if (!skb)
601cd0b8d89SJohannes Berg 		return false;
602f0706e82SJiri Benc 
603e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
604f0706e82SJiri Benc 
605f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
60657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
60757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
60857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
609f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
610f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
611e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
612f0706e82SJiri Benc }
613f0706e82SJiri Benc 
614f0706e82SJiri Benc 
615948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
616948d887dSJohannes Berg 						struct sta_info *sta, int ac)
617f0706e82SJiri Benc {
618f0706e82SJiri Benc 	unsigned long flags;
619f0706e82SJiri Benc 	struct sk_buff *skb;
620f0706e82SJiri Benc 
62160750397SJohannes Berg 	/*
62260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
62360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
62460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
62560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
62660750397SJohannes Berg 	 * total_ps_buffered counter.
62760750397SJohannes Berg 	 */
628f0706e82SJiri Benc 	for (;;) {
629948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
630948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
63157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
632948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
633836341a7SJohannes Berg 		else
634f0706e82SJiri Benc 			skb = NULL;
635948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
636f0706e82SJiri Benc 
63760750397SJohannes Berg 		/*
63860750397SJohannes Berg 		 * Frames are queued in order, so if this one
63960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
64060750397SJohannes Berg 		 * we actually reached the end of the queue we
64160750397SJohannes Berg 		 * also need to stop, of course.
64260750397SJohannes Berg 		 */
64360750397SJohannes Berg 		if (!skb)
64460750397SJohannes Berg 			break;
64560750397SJohannes Berg 		dev_kfree_skb(skb);
64660750397SJohannes Berg 	}
64760750397SJohannes Berg 
64860750397SJohannes Berg 	/*
64960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
65060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
65160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
65260750397SJohannes Berg 	 * buffered frames.
65360750397SJohannes Berg 	 */
654f0706e82SJiri Benc 	for (;;) {
655948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
656948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
657f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
658948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
659f0706e82SJiri Benc 		else
660f0706e82SJiri Benc 			skb = NULL;
661948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
662f0706e82SJiri Benc 
66360750397SJohannes Berg 		/*
66460750397SJohannes Berg 		 * frames are queued in order, so if this one
66560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
66660750397SJohannes Berg 		 * the queue) we can stop testing
66760750397SJohannes Berg 		 */
668836341a7SJohannes Berg 		if (!skb)
669836341a7SJohannes Berg 			break;
670836341a7SJohannes Berg 
671f0706e82SJiri Benc 		local->total_ps_buffered--;
672f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
6730c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
6740c68ae26SJohannes Berg 		       sta->sta.addr);
675f4ea83ddSJohannes Berg #endif
676f0706e82SJiri Benc 		dev_kfree_skb(skb);
677f0706e82SJiri Benc 	}
6783393a608SJuuso Oikarinen 
67960750397SJohannes Berg 	/*
68060750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
68160750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
68260750397SJohannes Berg 	 * frames.
68360750397SJohannes Berg 	 */
68460750397SJohannes Berg 	sta_info_recalc_tim(sta);
68560750397SJohannes Berg 
68660750397SJohannes Berg 	/*
68760750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
68860750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
68960750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
69060750397SJohannes Berg 	 */
691948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
692948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
693948d887dSJohannes Berg }
694948d887dSJohannes Berg 
695948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
696948d887dSJohannes Berg 					     struct sta_info *sta)
697948d887dSJohannes Berg {
698948d887dSJohannes Berg 	bool have_buffered = false;
699948d887dSJohannes Berg 	int ac;
700948d887dSJohannes Berg 
701948d887dSJohannes Berg 	/* This is only necessary for stations on BSS interfaces */
702948d887dSJohannes Berg 	if (!sta->sdata->bss)
703948d887dSJohannes Berg 		return false;
704948d887dSJohannes Berg 
705948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
706948d887dSJohannes Berg 		have_buffered |=
707948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
708948d887dSJohannes Berg 
709948d887dSJohannes Berg 	return have_buffered;
710f0706e82SJiri Benc }
711f0706e82SJiri Benc 
712*83d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
71334e89507SJohannes Berg {
71434e89507SJohannes Berg 	struct ieee80211_local *local;
71534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
716948d887dSJohannes Berg 	int ret, i, ac;
71742624d49SYogesh Ashok Powar 	struct tid_ampdu_tx *tid_tx;
71834e89507SJohannes Berg 
71934e89507SJohannes Berg 	might_sleep();
72034e89507SJohannes Berg 
72134e89507SJohannes Berg 	if (!sta)
72234e89507SJohannes Berg 		return -ENOENT;
72334e89507SJohannes Berg 
72434e89507SJohannes Berg 	local = sta->local;
72534e89507SJohannes Berg 	sdata = sta->sdata;
72634e89507SJohannes Berg 
727*83d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
728*83d5cc01SJohannes Berg 
729098a6070SJohannes Berg 	/*
730098a6070SJohannes Berg 	 * Before removing the station from the driver and
731098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
732098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
733098a6070SJohannes Berg 	 * will be sufficient.
734098a6070SJohannes Berg 	 */
735c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
73653f73c09SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, true);
737098a6070SJohannes Berg 
73834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
73934e89507SJohannes Berg 	if (ret)
74034e89507SJohannes Berg 		return ret;
74134e89507SJohannes Berg 
7424d33960bSJohannes Berg 	list_del(&sta->list);
7434d33960bSJohannes Berg 
7448cb23153SJohannes Berg 	mutex_lock(&local->key_mtx);
745e31b8213SJohannes Berg 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
74640b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
747e31b8213SJohannes Berg 	if (sta->ptk)
74840b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
7498cb23153SJohannes Berg 	mutex_unlock(&local->key_mtx);
75034e89507SJohannes Berg 
75134e89507SJohannes Berg 	sta->dead = true;
75234e89507SJohannes Berg 
753c2c98fdeSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
754c2c98fdeSJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
75534e89507SJohannes Berg 		BUG_ON(!sdata->bss);
75634e89507SJohannes Berg 
757c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_STA);
758c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
759c2c98fdeSJohannes Berg 
76034e89507SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
761c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
76234e89507SJohannes Berg 	}
76334e89507SJohannes Berg 
76434e89507SJohannes Berg 	local->num_sta--;
76534e89507SJohannes Berg 	local->sta_generation++;
76634e89507SJohannes Berg 
76734e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
768a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
76934e89507SJohannes Berg 
770*83d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
771*83d5cc01SJohannes Berg 		int err = sta_info_move_state(sta, sta->sta_state - 1);
772*83d5cc01SJohannes Berg 		if (err) {
773*83d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
774*83d5cc01SJohannes Berg 			break;
775*83d5cc01SJohannes Berg 		}
776*83d5cc01SJohannes Berg 	}
777d9a7ddb0SJohannes Berg 
77834e89507SJohannes Berg 	if (sta->uploaded) {
77934e89507SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
78034e89507SJohannes Berg 			sdata = container_of(sdata->bss,
78134e89507SJohannes Berg 					     struct ieee80211_sub_if_data,
78234e89507SJohannes Berg 					     u.ap);
78334e89507SJohannes Berg 		drv_sta_remove(local, sdata, &sta->sta);
78434e89507SJohannes Berg 		sdata = sta->sdata;
78534e89507SJohannes Berg 	}
78634e89507SJohannes Berg 
787e64b3795SJohannes Berg 	/*
788e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
789e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
790e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
791e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
792e64b3795SJohannes Berg 	 */
793e64b3795SJohannes Berg 	synchronize_rcu();
794e64b3795SJohannes Berg 
795948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
796948d887dSJohannes Berg 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
797948d887dSJohannes Berg 		__skb_queue_purge(&sta->ps_tx_buf[ac]);
798948d887dSJohannes Berg 		__skb_queue_purge(&sta->tx_filtered[ac]);
799948d887dSJohannes Berg 	}
800c868cb35SJohannes Berg 
80134e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
802e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
80334e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
80434e89507SJohannes Berg #endif
80534e89507SJohannes Berg 
80634e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
8070fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
80834e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
80934e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
81034e89507SJohannes Berg 
811ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
812ec15e68bSJouni Malinen 
81334e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
81434e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
81534e89507SJohannes Berg 
81634e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
81734e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
81834e89507SJohannes Berg 		mesh_plink_deactivate(sta);
81934e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
82034e89507SJohannes Berg 	}
82134e89507SJohannes Berg #endif
82234e89507SJohannes Berg 
823151a02f6SJohannes Berg 	/*
824151a02f6SJohannes Berg 	 * Destroy aggregation state here. It would be nice to wait for the
825151a02f6SJohannes Berg 	 * driver to finish aggregation stop and then clean up, but for now
826151a02f6SJohannes Berg 	 * drivers have to handle aggregation stop being requested, followed
827151a02f6SJohannes Berg 	 * directly by station destruction.
82842624d49SYogesh Ashok Powar 	 */
82942624d49SYogesh Ashok Powar 	for (i = 0; i < STA_TID_NUM; i++) {
830151a02f6SJohannes Berg 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
83127bf8882SYogesh Ashok Powar 		if (!tid_tx)
83242624d49SYogesh Ashok Powar 			continue;
83342624d49SYogesh Ashok Powar 		__skb_queue_purge(&tid_tx->pending);
834151a02f6SJohannes Berg 		kfree(tid_tx);
83542624d49SYogesh Ashok Powar 	}
83627bf8882SYogesh Ashok Powar 
837d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
83834e89507SJohannes Berg 
83934e89507SJohannes Berg 	return 0;
84034e89507SJohannes Berg }
84134e89507SJohannes Berg 
84234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
84334e89507SJohannes Berg {
84434e89507SJohannes Berg 	struct sta_info *sta;
84534e89507SJohannes Berg 	int ret;
84634e89507SJohannes Berg 
84734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8482a33bee2SGuy Eilam 	sta = sta_info_get_rx(sdata, addr);
84934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
85034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
85134e89507SJohannes Berg 
85234e89507SJohannes Berg 	return ret;
85334e89507SJohannes Berg }
85434e89507SJohannes Berg 
85534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
85634e89507SJohannes Berg 			      const u8 *addr)
85734e89507SJohannes Berg {
85834e89507SJohannes Berg 	struct sta_info *sta;
85934e89507SJohannes Berg 	int ret;
86034e89507SJohannes Berg 
86134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8622a33bee2SGuy Eilam 	sta = sta_info_get_bss_rx(sdata, addr);
86334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
86434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
86534e89507SJohannes Berg 
86634e89507SJohannes Berg 	return ret;
86734e89507SJohannes Berg }
868f0706e82SJiri Benc 
869f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
870f0706e82SJiri Benc {
871f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
872f0706e82SJiri Benc 	struct sta_info *sta;
8733393a608SJuuso Oikarinen 	bool timer_needed = false;
874f0706e82SJiri Benc 
875d0709a65SJohannes Berg 	rcu_read_lock();
876d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
8773393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
8783393a608SJuuso Oikarinen 			timer_needed = true;
879d0709a65SJohannes Berg 	rcu_read_unlock();
880f0706e82SJiri Benc 
8815bb644a0SJohannes Berg 	if (local->quiescing)
8825bb644a0SJohannes Berg 		return;
8835bb644a0SJohannes Berg 
8843393a608SJuuso Oikarinen 	if (!timer_needed)
8853393a608SJuuso Oikarinen 		return;
8863393a608SJuuso Oikarinen 
88726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
88826d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
889f0706e82SJiri Benc }
890f0706e82SJiri Benc 
891f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
892f0706e82SJiri Benc {
8934d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
89434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
895f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
896f0706e82SJiri Benc 
897b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
898b24b8a24SPavel Emelyanov 		    (unsigned long)local);
899f0706e82SJiri Benc }
900f0706e82SJiri Benc 
901f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
902f0706e82SJiri Benc {
903f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
904be8755e1SMichael Wu 	sta_info_flush(local, NULL);
905f0706e82SJiri Benc }
906f0706e82SJiri Benc 
907f0706e82SJiri Benc /**
908f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
90944213b5eSJohannes Berg  *
91044213b5eSJohannes Berg  * Returns the number of removed STA entries.
91144213b5eSJohannes Berg  *
912f0706e82SJiri Benc  * @local: local interface data
913d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
914f0706e82SJiri Benc  */
91544213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
916d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
917f0706e82SJiri Benc {
918f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
91944213b5eSJohannes Berg 	int ret = 0;
920f0706e82SJiri Benc 
921d0709a65SJohannes Berg 	might_sleep();
922d0709a65SJohannes Berg 
92334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
92434e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
92534e89507SJohannes Berg 		if (!sdata || sdata == sta->sdata)
92634e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
92734e89507SJohannes Berg 	}
92834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
92944213b5eSJohannes Berg 
93044213b5eSJohannes Berg 	return ret;
931f0706e82SJiri Benc }
932dc6676b7SJohannes Berg 
93324723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
93424723d1bSJohannes Berg 			  unsigned long exp_time)
93524723d1bSJohannes Berg {
93624723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
93724723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
93824723d1bSJohannes Berg 
93934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
940e46a2cf9SMohammed Shafi Shajakhan 
941e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
942ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
943ec2b774eSMarek Lindner 			continue;
944ec2b774eSMarek Lindner 
94524723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
94624723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
9470c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
94847846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
94924723d1bSJohannes Berg #endif
95034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
95124723d1bSJohannes Berg 		}
952e46a2cf9SMohammed Shafi Shajakhan 	}
953e46a2cf9SMohammed Shafi Shajakhan 
95434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
95524723d1bSJohannes Berg }
95617741cdcSJohannes Berg 
957686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
958686b9cb9SBen Greear 					       const u8 *addr,
959686b9cb9SBen Greear 					       const u8 *localaddr)
96017741cdcSJohannes Berg {
961abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
96217741cdcSJohannes Berg 
963686b9cb9SBen Greear 	/*
964686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
965686b9cb9SBen Greear 	 * ... first in list.
966686b9cb9SBen Greear 	 */
967f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
968686b9cb9SBen Greear 		if (localaddr &&
969686b9cb9SBen Greear 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
970686b9cb9SBen Greear 			continue;
971f7c65594SJohannes Berg 		if (!sta->uploaded)
972f7c65594SJohannes Berg 			return NULL;
97317741cdcSJohannes Berg 		return &sta->sta;
974f7c65594SJohannes Berg 	}
975f7c65594SJohannes Berg 
976abe60632SJohannes Berg 	return NULL;
97717741cdcSJohannes Berg }
978686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
9795ed176e1SJohannes Berg 
9805ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
9815ed176e1SJohannes Berg 					 const u8 *addr)
9825ed176e1SJohannes Berg {
983f7c65594SJohannes Berg 	struct sta_info *sta;
9845ed176e1SJohannes Berg 
9855ed176e1SJohannes Berg 	if (!vif)
9865ed176e1SJohannes Berg 		return NULL;
9875ed176e1SJohannes Berg 
988f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
989f7c65594SJohannes Berg 	if (!sta)
990f7c65594SJohannes Berg 		return NULL;
9915ed176e1SJohannes Berg 
992f7c65594SJohannes Berg 	if (!sta->uploaded)
993f7c65594SJohannes Berg 		return NULL;
994f7c65594SJohannes Berg 
995f7c65594SJohannes Berg 	return &sta->sta;
9965ed176e1SJohannes Berg }
99717741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
998af818581SJohannes Berg 
99950a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
100050a9432dSJohannes Berg {
100150a9432dSJohannes Berg 	struct sta_info *sta = _sta;
100250a9432dSJohannes Berg 
1003c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1004c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_STA);
100550a9432dSJohannes Berg }
100650a9432dSJohannes Berg 
1007af818581SJohannes Berg /* powersave support code */
1008af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1009af818581SJohannes Berg {
1010af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1011af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1012948d887dSJohannes Berg 	struct sk_buff_head pending;
1013948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
1014af818581SJohannes Berg 
1015c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
101647086fc5SJohannes Berg 
1017948d887dSJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
1018948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1019948d887dSJohannes Berg 
1020d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
102112375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1022af818581SJohannes Berg 
1023948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1024af818581SJohannes Berg 
1025af818581SJohannes Berg 	/* Send all buffered frames to the station */
1026948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1027948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1028948d887dSJohannes Berg 
1029948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1030948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1031948d887dSJohannes Berg 		filtered += tmp - count;
1032948d887dSJohannes Berg 		count = tmp;
1033948d887dSJohannes Berg 
1034948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1035948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1036948d887dSJohannes Berg 		buffered += tmp - count;
1037948d887dSJohannes Berg 	}
1038948d887dSJohannes Berg 
1039948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1040948d887dSJohannes Berg 
1041af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1042af818581SJohannes Berg 
1043c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1044c868cb35SJohannes Berg 
1045af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1046af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
104747846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
1048948d887dSJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1049af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1050af818581SJohannes Berg }
1051af818581SJohannes Berg 
1052ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1053ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
105440b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1055ce662b44SJohannes Berg {
1056ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1057ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1058ce662b44SJohannes Berg 	struct sk_buff *skb;
1059ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1060ce662b44SJohannes Berg 	__le16 fc;
1061c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1062ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
1063ce662b44SJohannes Berg 
1064ce662b44SJohannes Berg 	if (qos) {
1065ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1066ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1067ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1068ce662b44SJohannes Berg 	} else {
1069ce662b44SJohannes Berg 		size -= 2;
1070ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1071ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1072ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1073ce662b44SJohannes Berg 	}
1074ce662b44SJohannes Berg 
1075ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1076ce662b44SJohannes Berg 	if (!skb)
1077ce662b44SJohannes Berg 		return;
1078ce662b44SJohannes Berg 
1079ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1080ce662b44SJohannes Berg 
1081ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1082ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1083ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1084ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1085ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1086ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1087ce662b44SJohannes Berg 
1088ce662b44SJohannes Berg 	skb->priority = tid;
1089ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
109059b66255SJohannes Berg 	if (qos) {
1091ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1092ce662b44SJohannes Berg 
109340b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1094ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1095ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1096ce662b44SJohannes Berg 	}
1097ce662b44SJohannes Berg 
1098ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1099ce662b44SJohannes Berg 
1100ce662b44SJohannes Berg 	/*
1101ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1102ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1103deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1104deeaee19SJohannes Berg 	 * ends the poll/service period.
1105ce662b44SJohannes Berg 	 */
1106deeaee19SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
1107deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1108ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1109ce662b44SJohannes Berg 
111040b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
111140b96408SJohannes Berg 
1112ce662b44SJohannes Berg 	ieee80211_xmit(sdata, skb);
1113ce662b44SJohannes Berg }
1114ce662b44SJohannes Berg 
111547086fc5SJohannes Berg static void
111647086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
111747086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
111847086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1119af818581SJohannes Berg {
1120af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1121af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
11224049e09aSJohannes Berg 	bool found = false;
1123948d887dSJohannes Berg 	bool more_data = false;
1124948d887dSJohannes Berg 	int ac;
11254049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
112647086fc5SJohannes Berg 	struct sk_buff_head frames;
112747086fc5SJohannes Berg 
1128deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1129c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1130deeaee19SJohannes Berg 
113147086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1132af818581SJohannes Berg 
1133948d887dSJohannes Berg 	/*
113447086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1135948d887dSJohannes Berg 	 */
1136948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
11374049e09aSJohannes Berg 		unsigned long tids;
11384049e09aSJohannes Berg 
113947086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1140948d887dSJohannes Berg 			continue;
1141948d887dSJohannes Berg 
11424049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
11434049e09aSJohannes Berg 
11444049e09aSJohannes Berg 		if (!found) {
11454049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
11464049e09aSJohannes Berg 			if (driver_release_tids) {
11474049e09aSJohannes Berg 				found = true;
11484049e09aSJohannes Berg 			} else {
114947086fc5SJohannes Berg 				struct sk_buff *skb;
115047086fc5SJohannes Berg 
115147086fc5SJohannes Berg 				while (n_frames > 0) {
1152948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1153948d887dSJohannes Berg 					if (!skb) {
115447086fc5SJohannes Berg 						skb = skb_dequeue(
115547086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1156af818581SJohannes Berg 						if (skb)
1157af818581SJohannes Berg 							local->total_ps_buffered--;
1158af818581SJohannes Berg 					}
115947086fc5SJohannes Berg 					if (!skb)
116047086fc5SJohannes Berg 						break;
116147086fc5SJohannes Berg 					n_frames--;
11624049e09aSJohannes Berg 					found = true;
116347086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
116447086fc5SJohannes Berg 				}
1165948d887dSJohannes Berg 			}
1166948d887dSJohannes Berg 
11674049e09aSJohannes Berg 			/*
11684049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
11694049e09aSJohannes Berg 			 * certainly there's more data if we release just a
11704049e09aSJohannes Berg 			 * single frame now (from a single TID).
11714049e09aSJohannes Berg 			 */
117247086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
117347086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
11744049e09aSJohannes Berg 				more_data = true;
11754049e09aSJohannes Berg 				driver_release_tids =
11764049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
11774049e09aSJohannes Berg 				break;
11784049e09aSJohannes Berg 			}
11794049e09aSJohannes Berg 		}
1180948d887dSJohannes Berg 
1181948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1182948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1183948d887dSJohannes Berg 			more_data = true;
1184948d887dSJohannes Berg 			break;
1185948d887dSJohannes Berg 		}
1186948d887dSJohannes Berg 	}
1187af818581SJohannes Berg 
11884049e09aSJohannes Berg 	if (!found) {
1189ce662b44SJohannes Berg 		int tid;
11904049e09aSJohannes Berg 
1191ce662b44SJohannes Berg 		/*
1192ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1193ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1194ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1195ce662b44SJohannes Berg 		 *
1196ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1197ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1198ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1199ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1200ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1201ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1202ce662b44SJohannes Berg 		 *
1203ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1204ce662b44SJohannes Berg 		 */
1205ce662b44SJohannes Berg 
1206ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1207ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1208ce662b44SJohannes Berg 
120940b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
12104049e09aSJohannes Berg 		return;
12114049e09aSJohannes Berg 	}
12124049e09aSJohannes Berg 
121347086fc5SJohannes Berg 	if (!driver_release_tids) {
121447086fc5SJohannes Berg 		struct sk_buff_head pending;
121547086fc5SJohannes Berg 		struct sk_buff *skb;
121640b96408SJohannes Berg 		int num = 0;
121740b96408SJohannes Berg 		u16 tids = 0;
121847086fc5SJohannes Berg 
121947086fc5SJohannes Berg 		skb_queue_head_init(&pending);
122047086fc5SJohannes Berg 
122147086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1222af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
122347086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
122440b96408SJohannes Berg 			u8 *qoshdr = NULL;
122540b96408SJohannes Berg 
122640b96408SJohannes Berg 			num++;
1227af818581SJohannes Berg 
1228af818581SJohannes Berg 			/*
122947086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
123047086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
123147086fc5SJohannes Berg 			 * exchange.
1232af818581SJohannes Berg 			 */
123347086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
1234af818581SJohannes Berg 
123547086fc5SJohannes Berg 			/*
123647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
123747086fc5SJohannes Berg 			 * more buffered frames for this STA
123847086fc5SJohannes Berg 			 */
123924b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
124047086fc5SJohannes Berg 				hdr->frame_control |=
124147086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
124224b9c373SJanusz.Dziedzic@tieto.com 			else
124324b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
124424b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1245af818581SJohannes Berg 
124640b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
124740b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
124840b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
124940b96408SJohannes Berg 
125047086fc5SJohannes Berg 			/* set EOSP for the frame */
125140b96408SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
125240b96408SJohannes Berg 			    qoshdr && skb_queue_empty(&frames))
125340b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1254deeaee19SJohannes Berg 
125547086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_STATUS_EOSP |
125647086fc5SJohannes Berg 				       IEEE80211_TX_CTL_REQ_TX_STATUS;
125747086fc5SJohannes Berg 
125840b96408SJohannes Berg 			if (qoshdr)
125940b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
126040b96408SJohannes Berg 			else
126140b96408SJohannes Berg 				tids |= BIT(0);
126240b96408SJohannes Berg 
126347086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
126447086fc5SJohannes Berg 		}
126547086fc5SJohannes Berg 
126640b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
126740b96408SJohannes Berg 					  reason, more_data);
126840b96408SJohannes Berg 
126947086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1270af818581SJohannes Berg 
1271c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1272af818581SJohannes Berg 	} else {
1273af818581SJohannes Berg 		/*
12744049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
12754049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
12764049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
12774049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
12784049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
12794049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
12804049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
12814049e09aSJohannes Berg 		 * needs to be set anyway.
1282af818581SJohannes Berg 		 */
12834049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
128447086fc5SJohannes Berg 					    n_frames, reason, more_data);
12854049e09aSJohannes Berg 
12864049e09aSJohannes Berg 		/*
12874049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
12884049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
12894049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
12904049e09aSJohannes Berg 		 * release function.
12914049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
12924049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
12934049e09aSJohannes Berg 		 */
1294af818581SJohannes Berg 	}
1295af818581SJohannes Berg }
1296af818581SJohannes Berg 
1297af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1298af818581SJohannes Berg {
129947086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1300af818581SJohannes Berg 
1301af818581SJohannes Berg 	/*
130247086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
130347086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
130447086fc5SJohannes Berg 	 * only from the non-enabled ones.
1305af818581SJohannes Berg 	 */
130647086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
130747086fc5SJohannes Berg 		ignore_for_response = 0;
1308af818581SJohannes Berg 
130947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
131047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1311af818581SJohannes Berg }
131247086fc5SJohannes Berg 
131347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
131447086fc5SJohannes Berg {
131547086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
131647086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
131747086fc5SJohannes Berg 
131847086fc5SJohannes Berg 	/*
131947086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
132047086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
132147086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
132247086fc5SJohannes Berg 	 * actually getting called.
132347086fc5SJohannes Berg 	 */
132447086fc5SJohannes Berg 	if (!delivery_enabled)
132547086fc5SJohannes Berg 		return;
132647086fc5SJohannes Berg 
132747086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
132847086fc5SJohannes Berg 	case 1:
132947086fc5SJohannes Berg 		n_frames = 2;
133047086fc5SJohannes Berg 		break;
133147086fc5SJohannes Berg 	case 2:
133247086fc5SJohannes Berg 		n_frames = 4;
133347086fc5SJohannes Berg 		break;
133447086fc5SJohannes Berg 	case 3:
133547086fc5SJohannes Berg 		n_frames = 6;
133647086fc5SJohannes Berg 		break;
133747086fc5SJohannes Berg 	case 0:
133847086fc5SJohannes Berg 		/* XXX: what is a good value? */
133947086fc5SJohannes Berg 		n_frames = 8;
134047086fc5SJohannes Berg 		break;
134147086fc5SJohannes Berg 	}
134247086fc5SJohannes Berg 
134347086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
134447086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1345af818581SJohannes Berg }
1346af818581SJohannes Berg 
1347af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1348af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1349af818581SJohannes Berg {
1350af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1351af818581SJohannes Berg 
1352b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1353b5878a2dSJohannes Berg 
1354af818581SJohannes Berg 	if (block)
1355c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1356c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1357af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1358af818581SJohannes Berg }
1359af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1360dcf55fb5SFelix Fietkau 
136137fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
136237fbd908SJohannes Berg {
136337fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
136437fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
136537fbd908SJohannes Berg 	struct sk_buff *skb;
136637fbd908SJohannes Berg 	struct skb_eosp_msg_data *data;
136737fbd908SJohannes Berg 
136837fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
136937fbd908SJohannes Berg 
137037fbd908SJohannes Berg 	skb = alloc_skb(0, GFP_ATOMIC);
137137fbd908SJohannes Berg 	if (!skb) {
137237fbd908SJohannes Berg 		/* too bad ... but race is better than loss */
137337fbd908SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_SP);
137437fbd908SJohannes Berg 		return;
137537fbd908SJohannes Berg 	}
137637fbd908SJohannes Berg 
137737fbd908SJohannes Berg 	data = (void *)skb->cb;
137837fbd908SJohannes Berg 	memcpy(data->sta, pubsta->addr, ETH_ALEN);
137937fbd908SJohannes Berg 	memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
138037fbd908SJohannes Berg 	skb->pkt_type = IEEE80211_EOSP_MSG;
138137fbd908SJohannes Berg 	skb_queue_tail(&local->skb_queue, skb);
138237fbd908SJohannes Berg 	tasklet_schedule(&local->tasklet);
138337fbd908SJohannes Berg }
138437fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
138537fbd908SJohannes Berg 
1386042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1387042ec453SJohannes Berg 				u8 tid, bool buffered)
1388dcf55fb5SFelix Fietkau {
1389dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1390dcf55fb5SFelix Fietkau 
1391948d887dSJohannes Berg 	if (WARN_ON(tid >= STA_TID_NUM))
1392042ec453SJohannes Berg 		return;
1393042ec453SJohannes Berg 
1394948d887dSJohannes Berg 	if (buffered)
1395948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1396948d887dSJohannes Berg 	else
1397948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1398948d887dSJohannes Berg 
1399c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1400dcf55fb5SFelix Fietkau }
1401042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1402d9a7ddb0SJohannes Berg 
1403*83d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1404d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1405d9a7ddb0SJohannes Berg {
14068bf11d8dSJohannes Berg 	might_sleep();
1407d9a7ddb0SJohannes Berg 
1408d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1409d9a7ddb0SJohannes Berg 		return 0;
1410d9a7ddb0SJohannes Berg 
1411d9a7ddb0SJohannes Berg 	switch (new_state) {
1412d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1413d9a7ddb0SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1414d9a7ddb0SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1415d9a7ddb0SJohannes Berg 		else
1416d9a7ddb0SJohannes Berg 			return -EINVAL;
1417d9a7ddb0SJohannes Berg 		break;
1418d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1419d9a7ddb0SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1420d9a7ddb0SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1421d9a7ddb0SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1422d9a7ddb0SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1423d9a7ddb0SJohannes Berg 		else
1424d9a7ddb0SJohannes Berg 			return -EINVAL;
1425d9a7ddb0SJohannes Berg 		break;
1426d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
142729623892SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1428d9a7ddb0SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
142929623892SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
143029623892SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
143129623892SJohannes Berg 				atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
1432d9a7ddb0SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
143329623892SJohannes Berg 		} else
1434d9a7ddb0SJohannes Berg 			return -EINVAL;
1435d9a7ddb0SJohannes Berg 		break;
1436d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
143729623892SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
143829623892SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
143929623892SJohannes Berg 				atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
1440d9a7ddb0SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
144129623892SJohannes Berg 		} else
1442d9a7ddb0SJohannes Berg 			return -EINVAL;
1443d9a7ddb0SJohannes Berg 		break;
1444d9a7ddb0SJohannes Berg 	default:
1445d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1446d9a7ddb0SJohannes Berg 		return -EINVAL;
1447d9a7ddb0SJohannes Berg 	}
1448d9a7ddb0SJohannes Berg 
1449d9a7ddb0SJohannes Berg 	printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
1450d9a7ddb0SJohannes Berg 		sta->sdata->name, sta->sta.addr, new_state);
1451d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1452d9a7ddb0SJohannes Berg 
1453d9a7ddb0SJohannes Berg 	return 0;
1454d9a7ddb0SJohannes Berg }
1455