xref: /openbmc/linux/net/mac80211/sta_info.c (revision 888d04dfbe7e09f930fdaafb257cce2c54c9c3f3)
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>
12*888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21f0706e82SJiri Benc #include <net/mac80211.h>
22f0706e82SJiri Benc #include "ieee80211_i.h"
2324487981SJohannes Berg #include "driver-ops.h"
242c8dccc7SJohannes Berg #include "rate.h"
25f0706e82SJiri Benc #include "sta_info.h"
26e9f207f0SJiri Benc #include "debugfs_sta.h"
27ee385855SLuis Carlos Cobo #include "mesh.h"
28ce662b44SJohannes Berg #include "wme.h"
29f0706e82SJiri Benc 
30d0709a65SJohannes Berg /**
31d0709a65SJohannes Berg  * DOC: STA information lifetime rules
32d0709a65SJohannes Berg  *
33d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
34d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
35d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
36d0709a65SJohannes Berg  *
3734e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3834e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3934e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4034e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4134e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4234e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4334e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4434e89507SJohannes Berg  * encryption keys.
4593e5deb1SJohannes Berg  *
4693e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4793e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
48d0709a65SJohannes Berg  *
4934e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
507e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
517e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5225985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5334e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5425985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5534e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
567e189a12SLuis R. Rodriguez  *
5734e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5834e89507SJohannes Berg  * calls are available.
59d0709a65SJohannes Berg  *
6034e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6134e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6234e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6334e89507SJohannes Berg  * freed before they are done using it.
64d0709a65SJohannes Berg  */
65f0706e82SJiri Benc 
664d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
67be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
68f0706e82SJiri Benc 			     struct sta_info *sta)
69f0706e82SJiri Benc {
70f0706e82SJiri Benc 	struct sta_info *s;
71f0706e82SJiri Benc 
7240b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
734d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
74f0706e82SJiri Benc 	if (!s)
75be8755e1SMichael Wu 		return -ENOENT;
76be8755e1SMichael Wu 	if (s == sta) {
77cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
78d0709a65SJohannes Berg 				   s->hnext);
79be8755e1SMichael Wu 		return 0;
80f0706e82SJiri Benc 	}
81f0706e82SJiri Benc 
8240b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8340b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8440b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
854d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8640b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
87cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
88be8755e1SMichael Wu 		return 0;
89f0706e82SJiri Benc 	}
90f0706e82SJiri Benc 
91be8755e1SMichael Wu 	return -ENOENT;
92f0706e82SJiri Benc }
93f0706e82SJiri Benc 
94d0709a65SJohannes Berg /* protected by RCU */
95abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
96abe60632SJohannes Berg 			      const u8 *addr)
9743ba7e95SJohannes Berg {
98abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
9943ba7e95SJohannes Berg 	struct sta_info *sta;
10043ba7e95SJohannes Berg 
1010379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1020379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
10343ba7e95SJohannes Berg 	while (sta) {
104abe60632SJohannes Berg 		if (sta->sdata == sdata &&
105*888d04dfSFelix Fietkau 		    compare_ether_addr(sta->sta.addr, addr) == 0)
10643ba7e95SJohannes Berg 			break;
1070379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1080379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
10943ba7e95SJohannes Berg 	}
11043ba7e95SJohannes Berg 	return sta;
11143ba7e95SJohannes Berg }
11243ba7e95SJohannes Berg 
1130e5ded5aSFelix Fietkau /*
1140e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1150e5ded5aSFelix Fietkau  * or from one of its vlans
1160e5ded5aSFelix Fietkau  */
1170e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1180e5ded5aSFelix Fietkau 				  const u8 *addr)
1190e5ded5aSFelix Fietkau {
1200e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1210e5ded5aSFelix Fietkau 	struct sta_info *sta;
1220e5ded5aSFelix Fietkau 
1230379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1240379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1250e5ded5aSFelix Fietkau 	while (sta) {
1260e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
127a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
128*888d04dfSFelix Fietkau 		    compare_ether_addr(sta->sta.addr, addr) == 0)
1290e5ded5aSFelix Fietkau 			break;
1300379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1310379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1320e5ded5aSFelix Fietkau 	}
1330e5ded5aSFelix Fietkau 	return sta;
1340e5ded5aSFelix Fietkau }
1350e5ded5aSFelix Fietkau 
1363b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1373b53fde8SJohannes Berg 				     int idx)
138ee385855SLuis Carlos Cobo {
1393b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
140ee385855SLuis Carlos Cobo 	struct sta_info *sta;
141ee385855SLuis Carlos Cobo 	int i = 0;
142ee385855SLuis Carlos Cobo 
143d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1443b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1452a8ca29aSLuis Carlos Cobo 			continue;
146ee385855SLuis Carlos Cobo 		if (i < idx) {
147ee385855SLuis Carlos Cobo 			++i;
148ee385855SLuis Carlos Cobo 			continue;
149ee385855SLuis Carlos Cobo 		}
1502a8ca29aSLuis Carlos Cobo 		return sta;
151ee385855SLuis Carlos Cobo 	}
152ee385855SLuis Carlos Cobo 
153ee385855SLuis Carlos Cobo 	return NULL;
154ee385855SLuis Carlos Cobo }
155f0706e82SJiri Benc 
15693e5deb1SJohannes Berg /**
157d9a7ddb0SJohannes Berg  * sta_info_free - free STA
15893e5deb1SJohannes Berg  *
1596ef307bcSRandy Dunlap  * @local: pointer to the global information
16093e5deb1SJohannes Berg  * @sta: STA info to free
16193e5deb1SJohannes Berg  *
16293e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
163d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
164d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
165d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
16693e5deb1SJohannes Berg  */
167d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
16893e5deb1SJohannes Berg {
169889cbb91SJohannes Berg 	if (sta->rate_ctrl)
1704b7679a5SJohannes Berg 		rate_control_free_sta(sta);
17193e5deb1SJohannes Berg 
17293e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1730fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
17493e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
17593e5deb1SJohannes Berg 
17693e5deb1SJohannes Berg 	kfree(sta);
17793e5deb1SJohannes Berg }
17893e5deb1SJohannes Berg 
1794d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
180d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
181d0709a65SJohannes Berg 			      struct sta_info *sta)
182f0706e82SJiri Benc {
1834d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
18417741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
185cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
186f0706e82SJiri Benc }
187f0706e82SJiri Benc 
188af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
189af818581SJohannes Berg {
190af818581SJohannes Berg 	struct sta_info *sta;
191af818581SJohannes Berg 
192af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
193af818581SJohannes Berg 
194af818581SJohannes Berg 	if (sta->dead)
195af818581SJohannes Berg 		return;
196af818581SJohannes Berg 
19754420473SHelmut Schaa 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
19854420473SHelmut Schaa 		local_bh_disable();
199af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
20054420473SHelmut Schaa 		local_bh_enable();
20154420473SHelmut Schaa 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
202c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
203ce662b44SJohannes Berg 
204ce662b44SJohannes Berg 		local_bh_disable();
205af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
206ce662b44SJohannes Berg 		local_bh_enable();
207c2c98fdeSJohannes Berg 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
208c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
209ce662b44SJohannes Berg 
210ce662b44SJohannes Berg 		local_bh_disable();
21147086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
212ce662b44SJohannes Berg 		local_bh_enable();
21350a9432dSJohannes Berg 	} else
214c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
215af818581SJohannes Berg }
216af818581SJohannes Berg 
217af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
218af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
219af65cd96SJohannes Berg {
220af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
221af65cd96SJohannes Berg 		return 0;
222af65cd96SJohannes Berg 
223889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
224af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
225af65cd96SJohannes Berg 						     &sta->sta, gfp);
226889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
227af65cd96SJohannes Berg 		return -ENOMEM;
228af65cd96SJohannes Berg 
229af65cd96SJohannes Berg 	return 0;
230af65cd96SJohannes Berg }
231af65cd96SJohannes Berg 
23273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
23356544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
234f0706e82SJiri Benc {
235d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
236f0706e82SJiri Benc 	struct sta_info *sta;
237ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
23816c5f15cSRon Rindjunsky 	int i;
239f0706e82SJiri Benc 
24017741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
241f0706e82SJiri Benc 	if (!sta)
24273651ee6SJohannes Berg 		return NULL;
243f0706e82SJiri Benc 
24407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
245af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
24667c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
247a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
24807346f81SJohannes Berg 
24917741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
250d0709a65SJohannes Berg 	sta->local = local;
251d0709a65SJohannes Berg 	sta->sdata = sdata;
2528bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
253f0706e82SJiri Benc 
25471ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
25571ec375cSJohannes Berg 
256ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
257ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
258541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
259541a45a1SBruno Randolf 
260af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
261f0706e82SJiri Benc 		kfree(sta);
26273651ee6SJohannes Berg 		return NULL;
263f0706e82SJiri Benc 	}
264f0706e82SJiri Benc 
26516c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
266a622ab72SJohannes Berg 		/*
267a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
268a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
269a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
270a622ab72SJohannes Berg 		 */
27116c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
27216c5f15cSRon Rindjunsky 	}
273948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
274948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
275948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
276948d887dSJohannes Berg 	}
27773651ee6SJohannes Berg 
278cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
2794be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
280cccaec98SSenthil Balasubramanian 
28173651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2820fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
28373651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
28473651ee6SJohannes Berg 
28503e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
28657cf8043SJavier Cardona 	sta->plink_state = NL80211_PLINK_LISTEN;
28703e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
28803e4497eSJohannes Berg #endif
28903e4497eSJohannes Berg 
29073651ee6SJohannes Berg 	return sta;
29173651ee6SJohannes Berg }
29273651ee6SJohannes Berg 
2938c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
29434e89507SJohannes Berg {
29534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
29634e89507SJohannes Berg 
29703e4497eSJohannes Berg 	/*
29803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
29903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
30003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
30103e4497eSJohannes Berg 	 */
3028c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
3038c71df7aSGuy Eilam 		return -ENETDOWN;
30403e4497eSJohannes Berg 
30547846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
3068c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
3078c71df7aSGuy Eilam 		return -EINVAL;
3088c71df7aSGuy Eilam 
3098c71df7aSGuy Eilam 	return 0;
31093e5deb1SJohannes Berg }
31144213b5eSJohannes Berg 
312f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
313f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
314f09603a2SJohannes Berg 				     struct sta_info *sta)
315f09603a2SJohannes Berg {
316f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
317f09603a2SJohannes Berg 	int err = 0;
318f09603a2SJohannes Berg 
319f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
320f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
321f09603a2SJohannes Berg 		if (err)
322f09603a2SJohannes Berg 			break;
323f09603a2SJohannes Berg 	}
324f09603a2SJohannes Berg 
325f09603a2SJohannes Berg 	if (!err) {
326a4ec45a4SJohannes Berg 		/*
327a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
328a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
329a4ec45a4SJohannes Berg 		 */
330a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
331f09603a2SJohannes Berg 			sta->uploaded = true;
332f09603a2SJohannes Berg 		return 0;
333f09603a2SJohannes Berg 	}
334f09603a2SJohannes Berg 
335f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
336f09603a2SJohannes Berg 		printk(KERN_DEBUG
337f09603a2SJohannes Berg 		       "%s: failed to move IBSS STA %pM to state %d (%d) - keeping it anyway.\n",
338f09603a2SJohannes Berg 		       sdata->name, sta->sta.addr, state + 1, err);
339f09603a2SJohannes Berg 		err = 0;
340f09603a2SJohannes Berg 	}
341f09603a2SJohannes Berg 
342f09603a2SJohannes Berg 	/* unwind on error */
343f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
344f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
345f09603a2SJohannes Berg 
346f09603a2SJohannes Berg 	return err;
347f09603a2SJohannes Berg }
348f09603a2SJohannes Berg 
34934e89507SJohannes Berg /*
3508c71df7aSGuy Eilam  * should be called with sta_mtx locked
3518c71df7aSGuy Eilam  * this function replaces the mutex lock
3528c71df7aSGuy Eilam  * with a RCU lock
3538c71df7aSGuy Eilam  */
3544d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
3558c71df7aSGuy Eilam {
3568c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
3578c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3587852e361SJohannes Berg 	struct station_info sinfo;
3598c71df7aSGuy Eilam 	int err = 0;
3608c71df7aSGuy Eilam 
3618c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
3628c71df7aSGuy Eilam 
3637852e361SJohannes Berg 	/* check if STA exists already */
3647852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
3654d33960bSJohannes Berg 		err = -EEXIST;
3664d33960bSJohannes Berg 		goto out_err;
36734e89507SJohannes Berg 	}
36834e89507SJohannes Berg 
3694d33960bSJohannes Berg 	/* notify driver */
370f09603a2SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
371f09603a2SJohannes Berg 	if (err)
372f09603a2SJohannes Berg 		goto out_err;
3734d33960bSJohannes Berg 
3744d33960bSJohannes Berg 	local->num_sta++;
3754d33960bSJohannes Berg 	local->sta_generation++;
3764d33960bSJohannes Berg 	smp_mb();
3774d33960bSJohannes Berg 
3784d33960bSJohannes Berg 	/* make the station visible */
3794d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
3804d33960bSJohannes Berg 
3814d33960bSJohannes Berg 	list_add(&sta->list, &local->sta_list);
38283d5cc01SJohannes Berg 
38383d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
3844d33960bSJohannes Berg 
3854d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
3864d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
3874d33960bSJohannes Berg 
3884d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
3894d33960bSJohannes Berg 	sinfo.filled = 0;
3904d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
3914d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
392d0709a65SJohannes Berg 
393f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3947852e361SJohannes Berg 	wiphy_debug(local->hw.wiphy, "Inserted STA %pM\n", sta->sta.addr);
395f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
396f0706e82SJiri Benc 
39734e89507SJohannes Berg 	/* move reference to rcu-protected */
39834e89507SJohannes Berg 	rcu_read_lock();
39934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
400e9f207f0SJiri Benc 
40173651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
40273651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
40373651ee6SJohannes Berg 
40473651ee6SJohannes Berg 	return 0;
4054d33960bSJohannes Berg  out_err:
4064d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
4074d33960bSJohannes Berg 	rcu_read_lock();
4084d33960bSJohannes Berg 	return err;
4098c71df7aSGuy Eilam }
4108c71df7aSGuy Eilam 
4118c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
4128c71df7aSGuy Eilam {
4138c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4148c71df7aSGuy Eilam 	int err = 0;
4158c71df7aSGuy Eilam 
4164d33960bSJohannes Berg 	might_sleep();
4174d33960bSJohannes Berg 
4188c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
4198c71df7aSGuy Eilam 	if (err) {
4208c71df7aSGuy Eilam 		rcu_read_lock();
4218c71df7aSGuy Eilam 		goto out_free;
4228c71df7aSGuy Eilam 	}
4238c71df7aSGuy Eilam 
424004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
425004c872eSJohannes Berg 
4264d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
4278c71df7aSGuy Eilam 	if (err)
428004c872eSJohannes Berg 		goto out_free;
429d0709a65SJohannes Berg 
430004c872eSJohannes Berg 	return 0;
43193e5deb1SJohannes Berg  out_free:
43293e5deb1SJohannes Berg 	BUG_ON(!err);
433d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
43493e5deb1SJohannes Berg 	return err;
435f0706e82SJiri Benc }
436f0706e82SJiri Benc 
43734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
43834e89507SJohannes Berg {
43934e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
44034e89507SJohannes Berg 
44134e89507SJohannes Berg 	rcu_read_unlock();
44234e89507SJohannes Berg 
44334e89507SJohannes Berg 	return err;
44434e89507SJohannes Berg }
44534e89507SJohannes Berg 
446f0706e82SJiri Benc static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
447004c872eSJohannes Berg {
448004c872eSJohannes Berg 	/*
449004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
450004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
451004c872eSJohannes Berg 	 */
452004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
453004c872eSJohannes Berg }
454004c872eSJohannes Berg 
455004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
456004c872eSJohannes Berg {
457004c872eSJohannes Berg 	/*
458004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
459004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
460004c872eSJohannes Berg 	 */
461004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
462004c872eSJohannes Berg }
463004c872eSJohannes Berg 
464948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
465004c872eSJohannes Berg {
466948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
467948d887dSJohannes Berg 	switch (ac) {
468948d887dSJohannes Berg 	case IEEE80211_AC_VO:
469948d887dSJohannes Berg 		return BIT(6) | BIT(7);
470948d887dSJohannes Berg 	case IEEE80211_AC_VI:
471948d887dSJohannes Berg 		return BIT(4) | BIT(5);
472948d887dSJohannes Berg 	case IEEE80211_AC_BE:
473948d887dSJohannes Berg 		return BIT(0) | BIT(3);
474948d887dSJohannes Berg 	case IEEE80211_AC_BK:
475948d887dSJohannes Berg 		return BIT(1) | BIT(2);
476948d887dSJohannes Berg 	default:
477948d887dSJohannes Berg 		WARN_ON(1);
478948d887dSJohannes Berg 		return 0;
479d0709a65SJohannes Berg 	}
480004c872eSJohannes Berg }
481004c872eSJohannes Berg 
482c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
483004c872eSJohannes Berg {
484c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
485c868cb35SJohannes Berg 	struct ieee80211_if_ap *bss = sta->sdata->bss;
486d0709a65SJohannes Berg 	unsigned long flags;
487948d887dSJohannes Berg 	bool indicate_tim = false;
488948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
489948d887dSJohannes Berg 	int ac;
490004c872eSJohannes Berg 
491c868cb35SJohannes Berg 	if (WARN_ON_ONCE(!sta->sdata->bss))
492c868cb35SJohannes Berg 		return;
4933e122be0SJohannes Berg 
494c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
495c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
496c868cb35SJohannes Berg 		return;
497004c872eSJohannes Berg 
498c868cb35SJohannes Berg 	if (sta->dead)
499c868cb35SJohannes Berg 		goto done;
5003e122be0SJohannes Berg 
501948d887dSJohannes Berg 	/*
502948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
503948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
504948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
505948d887dSJohannes Berg 	 * non-enabled ones.
506948d887dSJohannes Berg 	 */
507948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
508948d887dSJohannes Berg 		ignore_for_tim = 0;
509948d887dSJohannes Berg 
510948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
511948d887dSJohannes Berg 		unsigned long tids;
512948d887dSJohannes Berg 
513948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
514948d887dSJohannes Berg 			continue;
515948d887dSJohannes Berg 
516948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
517948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
518948d887dSJohannes Berg 		if (indicate_tim)
519948d887dSJohannes Berg 			break;
520948d887dSJohannes Berg 
521948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
522948d887dSJohannes Berg 
523948d887dSJohannes Berg 		indicate_tim |=
524948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
525004c872eSJohannes Berg 	}
526004c872eSJohannes Berg 
527c868cb35SJohannes Berg  done:
5284d33960bSJohannes Berg 	spin_lock_irqsave(&local->tim_lock, flags);
529004c872eSJohannes Berg 
530948d887dSJohannes Berg 	if (indicate_tim)
531c868cb35SJohannes Berg 		__bss_tim_set(bss, sta->sta.aid);
532c868cb35SJohannes Berg 	else
53317741cdcSJohannes Berg 		__bss_tim_clear(bss, sta->sta.aid);
5343e122be0SJohannes Berg 
535c868cb35SJohannes Berg 	if (local->ops->set_tim) {
536c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
537948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
538c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
539004c872eSJohannes Berg 	}
540004c872eSJohannes Berg 
5414d33960bSJohannes Berg 	spin_unlock_irqrestore(&local->tim_lock, flags);
542004c872eSJohannes Berg }
543004c872eSJohannes Berg 
544cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
545f0706e82SJiri Benc {
546e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
547f0706e82SJiri Benc 	int timeout;
548f0706e82SJiri Benc 
549f0706e82SJiri Benc 	if (!skb)
550cd0b8d89SJohannes Berg 		return false;
551f0706e82SJiri Benc 
552e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
553f0706e82SJiri Benc 
554f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
55557c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
55657c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
55757c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
558f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
559f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
560e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
561f0706e82SJiri Benc }
562f0706e82SJiri Benc 
563f0706e82SJiri Benc 
564948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
565948d887dSJohannes Berg 						struct sta_info *sta, int ac)
566f0706e82SJiri Benc {
567f0706e82SJiri Benc 	unsigned long flags;
568f0706e82SJiri Benc 	struct sk_buff *skb;
569f0706e82SJiri Benc 
57060750397SJohannes Berg 	/*
57160750397SJohannes Berg 	 * First check for frames that should expire on the filtered
57260750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
57360750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
57460750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
57560750397SJohannes Berg 	 * total_ps_buffered counter.
57660750397SJohannes Berg 	 */
577f0706e82SJiri Benc 	for (;;) {
578948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
579948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
58057c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
581948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
582836341a7SJohannes Berg 		else
583f0706e82SJiri Benc 			skb = NULL;
584948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
585f0706e82SJiri Benc 
58660750397SJohannes Berg 		/*
58760750397SJohannes Berg 		 * Frames are queued in order, so if this one
58860750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
58960750397SJohannes Berg 		 * we actually reached the end of the queue we
59060750397SJohannes Berg 		 * also need to stop, of course.
59160750397SJohannes Berg 		 */
59260750397SJohannes Berg 		if (!skb)
59360750397SJohannes Berg 			break;
59460750397SJohannes Berg 		dev_kfree_skb(skb);
59560750397SJohannes Berg 	}
59660750397SJohannes Berg 
59760750397SJohannes Berg 	/*
59860750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
59960750397SJohannes Berg 	 * only find something if the filtered queue was emptied
60060750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
60160750397SJohannes Berg 	 * buffered frames.
60260750397SJohannes Berg 	 */
603f0706e82SJiri Benc 	for (;;) {
604948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
605948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
606f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
607948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
608f0706e82SJiri Benc 		else
609f0706e82SJiri Benc 			skb = NULL;
610948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
611f0706e82SJiri Benc 
61260750397SJohannes Berg 		/*
61360750397SJohannes Berg 		 * frames are queued in order, so if this one
61460750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
61560750397SJohannes Berg 		 * the queue) we can stop testing
61660750397SJohannes Berg 		 */
617836341a7SJohannes Berg 		if (!skb)
618836341a7SJohannes Berg 			break;
619836341a7SJohannes Berg 
620f0706e82SJiri Benc 		local->total_ps_buffered--;
621f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
6220c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
6230c68ae26SJohannes Berg 		       sta->sta.addr);
624f4ea83ddSJohannes Berg #endif
625f0706e82SJiri Benc 		dev_kfree_skb(skb);
626f0706e82SJiri Benc 	}
6273393a608SJuuso Oikarinen 
62860750397SJohannes Berg 	/*
62960750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
63060750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
63160750397SJohannes Berg 	 * frames.
63260750397SJohannes Berg 	 */
63360750397SJohannes Berg 	sta_info_recalc_tim(sta);
63460750397SJohannes Berg 
63560750397SJohannes Berg 	/*
63660750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
63760750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
63860750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
63960750397SJohannes Berg 	 */
640948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
641948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
642948d887dSJohannes Berg }
643948d887dSJohannes Berg 
644948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
645948d887dSJohannes Berg 					     struct sta_info *sta)
646948d887dSJohannes Berg {
647948d887dSJohannes Berg 	bool have_buffered = false;
648948d887dSJohannes Berg 	int ac;
649948d887dSJohannes Berg 
650948d887dSJohannes Berg 	/* This is only necessary for stations on BSS interfaces */
651948d887dSJohannes Berg 	if (!sta->sdata->bss)
652948d887dSJohannes Berg 		return false;
653948d887dSJohannes Berg 
654948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
655948d887dSJohannes Berg 		have_buffered |=
656948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
657948d887dSJohannes Berg 
658948d887dSJohannes Berg 	return have_buffered;
659f0706e82SJiri Benc }
660f0706e82SJiri Benc 
66183d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
66234e89507SJohannes Berg {
66334e89507SJohannes Berg 	struct ieee80211_local *local;
66434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
665948d887dSJohannes Berg 	int ret, i, ac;
66642624d49SYogesh Ashok Powar 	struct tid_ampdu_tx *tid_tx;
66734e89507SJohannes Berg 
66834e89507SJohannes Berg 	might_sleep();
66934e89507SJohannes Berg 
67034e89507SJohannes Berg 	if (!sta)
67134e89507SJohannes Berg 		return -ENOENT;
67234e89507SJohannes Berg 
67334e89507SJohannes Berg 	local = sta->local;
67434e89507SJohannes Berg 	sdata = sta->sdata;
67534e89507SJohannes Berg 
67683d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
67783d5cc01SJohannes Berg 
678098a6070SJohannes Berg 	/*
679098a6070SJohannes Berg 	 * Before removing the station from the driver and
680098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
681098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
682098a6070SJohannes Berg 	 * will be sufficient.
683098a6070SJohannes Berg 	 */
684c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
68553f73c09SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, true);
686098a6070SJohannes Berg 
68734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
68834e89507SJohannes Berg 	if (ret)
68934e89507SJohannes Berg 		return ret;
69034e89507SJohannes Berg 
6914d33960bSJohannes Berg 	list_del(&sta->list);
6924d33960bSJohannes Berg 
6938cb23153SJohannes Berg 	mutex_lock(&local->key_mtx);
694e31b8213SJohannes Berg 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
69540b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
696e31b8213SJohannes Berg 	if (sta->ptk)
69740b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
6988cb23153SJohannes Berg 	mutex_unlock(&local->key_mtx);
69934e89507SJohannes Berg 
70034e89507SJohannes Berg 	sta->dead = true;
70134e89507SJohannes Berg 
70234e89507SJohannes Berg 	local->num_sta--;
70334e89507SJohannes Berg 	local->sta_generation++;
70434e89507SJohannes Berg 
70534e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
706a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
70734e89507SJohannes Berg 
70883d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
709f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
710f09603a2SJohannes Berg 		if (ret) {
71183d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
71283d5cc01SJohannes Berg 			break;
71383d5cc01SJohannes Berg 		}
71483d5cc01SJohannes Berg 	}
715d9a7ddb0SJohannes Berg 
716f09603a2SJohannes Berg 	if (sta->uploaded) {
717f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
718f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
719f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
720f09603a2SJohannes Berg 	}
72134e89507SJohannes Berg 
722e64b3795SJohannes Berg 	/*
723e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
724e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
725e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
726e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
727e64b3795SJohannes Berg 	 */
728e64b3795SJohannes Berg 	synchronize_rcu();
729e64b3795SJohannes Berg 
7304f3eb0baSHelmut Schaa 	if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
7314f3eb0baSHelmut Schaa 		BUG_ON(!sdata->bss);
7324f3eb0baSHelmut Schaa 
7334f3eb0baSHelmut Schaa 		clear_sta_flag(sta, WLAN_STA_PS_STA);
7344f3eb0baSHelmut Schaa 
7354f3eb0baSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
7364f3eb0baSHelmut Schaa 		sta_info_recalc_tim(sta);
7374f3eb0baSHelmut Schaa 	}
7384f3eb0baSHelmut Schaa 
739948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
740948d887dSJohannes Berg 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
741948d887dSJohannes Berg 		__skb_queue_purge(&sta->ps_tx_buf[ac]);
742948d887dSJohannes Berg 		__skb_queue_purge(&sta->tx_filtered[ac]);
743948d887dSJohannes Berg 	}
744c868cb35SJohannes Berg 
74534e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
746e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
74734e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
74834e89507SJohannes Berg #endif
74934e89507SJohannes Berg 
75034e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
7510fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
75234e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
75334e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
75434e89507SJohannes Berg 
755ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
756ec15e68bSJouni Malinen 
75734e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
75834e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
75934e89507SJohannes Berg 
76034e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
76134e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
76234e89507SJohannes Berg 		mesh_plink_deactivate(sta);
76334e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
76434e89507SJohannes Berg 	}
76534e89507SJohannes Berg #endif
76634e89507SJohannes Berg 
767151a02f6SJohannes Berg 	/*
768151a02f6SJohannes Berg 	 * Destroy aggregation state here. It would be nice to wait for the
769151a02f6SJohannes Berg 	 * driver to finish aggregation stop and then clean up, but for now
770151a02f6SJohannes Berg 	 * drivers have to handle aggregation stop being requested, followed
771151a02f6SJohannes Berg 	 * directly by station destruction.
77242624d49SYogesh Ashok Powar 	 */
77342624d49SYogesh Ashok Powar 	for (i = 0; i < STA_TID_NUM; i++) {
774151a02f6SJohannes Berg 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
77527bf8882SYogesh Ashok Powar 		if (!tid_tx)
77642624d49SYogesh Ashok Powar 			continue;
77742624d49SYogesh Ashok Powar 		__skb_queue_purge(&tid_tx->pending);
778151a02f6SJohannes Berg 		kfree(tid_tx);
77942624d49SYogesh Ashok Powar 	}
78027bf8882SYogesh Ashok Powar 
781d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
78234e89507SJohannes Berg 
78334e89507SJohannes Berg 	return 0;
78434e89507SJohannes Berg }
78534e89507SJohannes Berg 
78634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
78734e89507SJohannes Berg {
78834e89507SJohannes Berg 	struct sta_info *sta;
78934e89507SJohannes Berg 	int ret;
79034e89507SJohannes Berg 
79134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
7927852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
79334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
79434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
79534e89507SJohannes Berg 
79634e89507SJohannes Berg 	return ret;
79734e89507SJohannes Berg }
79834e89507SJohannes Berg 
79934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
80034e89507SJohannes Berg 			      const u8 *addr)
80134e89507SJohannes Berg {
80234e89507SJohannes Berg 	struct sta_info *sta;
80334e89507SJohannes Berg 	int ret;
80434e89507SJohannes Berg 
80534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8067852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
80734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
80834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
80934e89507SJohannes Berg 
81034e89507SJohannes Berg 	return ret;
81134e89507SJohannes Berg }
812f0706e82SJiri Benc 
813f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
814f0706e82SJiri Benc {
815f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
816f0706e82SJiri Benc 	struct sta_info *sta;
8173393a608SJuuso Oikarinen 	bool timer_needed = false;
818f0706e82SJiri Benc 
819d0709a65SJohannes Berg 	rcu_read_lock();
820d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
8213393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
8223393a608SJuuso Oikarinen 			timer_needed = true;
823d0709a65SJohannes Berg 	rcu_read_unlock();
824f0706e82SJiri Benc 
8255bb644a0SJohannes Berg 	if (local->quiescing)
8265bb644a0SJohannes Berg 		return;
8275bb644a0SJohannes Berg 
8283393a608SJuuso Oikarinen 	if (!timer_needed)
8293393a608SJuuso Oikarinen 		return;
8303393a608SJuuso Oikarinen 
83126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
83226d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
833f0706e82SJiri Benc }
834f0706e82SJiri Benc 
835f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
836f0706e82SJiri Benc {
8374d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
83834e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
839f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
840f0706e82SJiri Benc 
841b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
842b24b8a24SPavel Emelyanov 		    (unsigned long)local);
843f0706e82SJiri Benc }
844f0706e82SJiri Benc 
845f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
846f0706e82SJiri Benc {
847f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
848be8755e1SMichael Wu 	sta_info_flush(local, NULL);
849f0706e82SJiri Benc }
850f0706e82SJiri Benc 
851f0706e82SJiri Benc /**
852f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
85344213b5eSJohannes Berg  *
85444213b5eSJohannes Berg  * Returns the number of removed STA entries.
85544213b5eSJohannes Berg  *
856f0706e82SJiri Benc  * @local: local interface data
857d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
858f0706e82SJiri Benc  */
85944213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
860d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
861f0706e82SJiri Benc {
862f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
86344213b5eSJohannes Berg 	int ret = 0;
864f0706e82SJiri Benc 
865d0709a65SJohannes Berg 	might_sleep();
866d0709a65SJohannes Berg 
86734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
86834e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
86934316837SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
87034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
87134316837SJohannes Berg 			ret++;
87234316837SJohannes Berg 		}
87334e89507SJohannes Berg 	}
87434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
87544213b5eSJohannes Berg 
87644213b5eSJohannes Berg 	return ret;
877f0706e82SJiri Benc }
878dc6676b7SJohannes Berg 
87924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
88024723d1bSJohannes Berg 			  unsigned long exp_time)
88124723d1bSJohannes Berg {
88224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
88324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
88424723d1bSJohannes Berg 
88534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
886e46a2cf9SMohammed Shafi Shajakhan 
887e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
888ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
889ec2b774eSMarek Lindner 			continue;
890ec2b774eSMarek Lindner 
89124723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
89224723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
8930c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
89447846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
89524723d1bSJohannes Berg #endif
89634e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
89724723d1bSJohannes Berg 		}
898e46a2cf9SMohammed Shafi Shajakhan 	}
899e46a2cf9SMohammed Shafi Shajakhan 
90034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
90124723d1bSJohannes Berg }
90217741cdcSJohannes Berg 
903686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
904686b9cb9SBen Greear 					       const u8 *addr,
905686b9cb9SBen Greear 					       const u8 *localaddr)
90617741cdcSJohannes Berg {
907abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
90817741cdcSJohannes Berg 
909686b9cb9SBen Greear 	/*
910686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
911686b9cb9SBen Greear 	 * ... first in list.
912686b9cb9SBen Greear 	 */
913f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
914686b9cb9SBen Greear 		if (localaddr &&
915686b9cb9SBen Greear 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
916686b9cb9SBen Greear 			continue;
917f7c65594SJohannes Berg 		if (!sta->uploaded)
918f7c65594SJohannes Berg 			return NULL;
91917741cdcSJohannes Berg 		return &sta->sta;
920f7c65594SJohannes Berg 	}
921f7c65594SJohannes Berg 
922abe60632SJohannes Berg 	return NULL;
92317741cdcSJohannes Berg }
924686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
9255ed176e1SJohannes Berg 
9265ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
9275ed176e1SJohannes Berg 					 const u8 *addr)
9285ed176e1SJohannes Berg {
929f7c65594SJohannes Berg 	struct sta_info *sta;
9305ed176e1SJohannes Berg 
9315ed176e1SJohannes Berg 	if (!vif)
9325ed176e1SJohannes Berg 		return NULL;
9335ed176e1SJohannes Berg 
934f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
935f7c65594SJohannes Berg 	if (!sta)
936f7c65594SJohannes Berg 		return NULL;
9375ed176e1SJohannes Berg 
938f7c65594SJohannes Berg 	if (!sta->uploaded)
939f7c65594SJohannes Berg 		return NULL;
940f7c65594SJohannes Berg 
941f7c65594SJohannes Berg 	return &sta->sta;
9425ed176e1SJohannes Berg }
94317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
944af818581SJohannes Berg 
94550a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
94650a9432dSJohannes Berg {
94750a9432dSJohannes Berg 	struct sta_info *sta = _sta;
948608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
94950a9432dSJohannes Berg 
950c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
951608383bfSHelmut Schaa 	if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
952608383bfSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
95350a9432dSJohannes Berg }
95450a9432dSJohannes Berg 
955af818581SJohannes Berg /* powersave support code */
956af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
957af818581SJohannes Berg {
958af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
959af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
960948d887dSJohannes Berg 	struct sk_buff_head pending;
961948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
962af818581SJohannes Berg 
963c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
96447086fc5SJohannes Berg 
965948d887dSJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
966948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
967948d887dSJohannes Berg 
968d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
96912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
970af818581SJohannes Berg 
971948d887dSJohannes Berg 	skb_queue_head_init(&pending);
972af818581SJohannes Berg 
973af818581SJohannes Berg 	/* Send all buffered frames to the station */
974948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
975948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
976948d887dSJohannes Berg 
977948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
978948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
979948d887dSJohannes Berg 		filtered += tmp - count;
980948d887dSJohannes Berg 		count = tmp;
981948d887dSJohannes Berg 
982948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
983948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
984948d887dSJohannes Berg 		buffered += tmp - count;
985948d887dSJohannes Berg 	}
986948d887dSJohannes Berg 
987948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
988948d887dSJohannes Berg 
989af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
990af818581SJohannes Berg 
991c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
992c868cb35SJohannes Berg 
993af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
994af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
99547846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
996948d887dSJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
997af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
998af818581SJohannes Berg }
999af818581SJohannes Berg 
1000ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1001ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
100240b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1003ce662b44SJohannes Berg {
1004ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1005ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1006ce662b44SJohannes Berg 	struct sk_buff *skb;
1007ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1008ce662b44SJohannes Berg 	__le16 fc;
1009c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1010ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
1011ce662b44SJohannes Berg 
1012ce662b44SJohannes Berg 	if (qos) {
1013ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1014ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1015ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1016ce662b44SJohannes Berg 	} else {
1017ce662b44SJohannes Berg 		size -= 2;
1018ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1019ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1020ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1021ce662b44SJohannes Berg 	}
1022ce662b44SJohannes Berg 
1023ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1024ce662b44SJohannes Berg 	if (!skb)
1025ce662b44SJohannes Berg 		return;
1026ce662b44SJohannes Berg 
1027ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1028ce662b44SJohannes Berg 
1029ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1030ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1031ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1032ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1033ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1034ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1035ce662b44SJohannes Berg 
1036ce662b44SJohannes Berg 	skb->priority = tid;
1037ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
103859b66255SJohannes Berg 	if (qos) {
1039ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1040ce662b44SJohannes Berg 
104140b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1042ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1043ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1044ce662b44SJohannes Berg 	}
1045ce662b44SJohannes Berg 
1046ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1047ce662b44SJohannes Berg 
1048ce662b44SJohannes Berg 	/*
1049ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1050ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1051deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1052deeaee19SJohannes Berg 	 * ends the poll/service period.
1053ce662b44SJohannes Berg 	 */
105402f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1055deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1056ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1057ce662b44SJohannes Berg 
105840b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
105940b96408SJohannes Berg 
1060ce662b44SJohannes Berg 	ieee80211_xmit(sdata, skb);
1061ce662b44SJohannes Berg }
1062ce662b44SJohannes Berg 
106347086fc5SJohannes Berg static void
106447086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
106547086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
106647086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1067af818581SJohannes Berg {
1068af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1069af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
10704049e09aSJohannes Berg 	bool found = false;
1071948d887dSJohannes Berg 	bool more_data = false;
1072948d887dSJohannes Berg 	int ac;
10734049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
107447086fc5SJohannes Berg 	struct sk_buff_head frames;
107547086fc5SJohannes Berg 
1076deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1077c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1078deeaee19SJohannes Berg 
107947086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1080af818581SJohannes Berg 
1081948d887dSJohannes Berg 	/*
108247086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1083948d887dSJohannes Berg 	 */
1084948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
10854049e09aSJohannes Berg 		unsigned long tids;
10864049e09aSJohannes Berg 
108747086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1088948d887dSJohannes Berg 			continue;
1089948d887dSJohannes Berg 
10904049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
10914049e09aSJohannes Berg 
10924049e09aSJohannes Berg 		if (!found) {
10934049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
10944049e09aSJohannes Berg 			if (driver_release_tids) {
10954049e09aSJohannes Berg 				found = true;
10964049e09aSJohannes Berg 			} else {
109747086fc5SJohannes Berg 				struct sk_buff *skb;
109847086fc5SJohannes Berg 
109947086fc5SJohannes Berg 				while (n_frames > 0) {
1100948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1101948d887dSJohannes Berg 					if (!skb) {
110247086fc5SJohannes Berg 						skb = skb_dequeue(
110347086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1104af818581SJohannes Berg 						if (skb)
1105af818581SJohannes Berg 							local->total_ps_buffered--;
1106af818581SJohannes Berg 					}
110747086fc5SJohannes Berg 					if (!skb)
110847086fc5SJohannes Berg 						break;
110947086fc5SJohannes Berg 					n_frames--;
11104049e09aSJohannes Berg 					found = true;
111147086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
111247086fc5SJohannes Berg 				}
1113948d887dSJohannes Berg 			}
1114948d887dSJohannes Berg 
11154049e09aSJohannes Berg 			/*
11164049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
11174049e09aSJohannes Berg 			 * certainly there's more data if we release just a
11184049e09aSJohannes Berg 			 * single frame now (from a single TID).
11194049e09aSJohannes Berg 			 */
112047086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
112147086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
11224049e09aSJohannes Berg 				more_data = true;
11234049e09aSJohannes Berg 				driver_release_tids =
11244049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
11254049e09aSJohannes Berg 				break;
11264049e09aSJohannes Berg 			}
11274049e09aSJohannes Berg 		}
1128948d887dSJohannes Berg 
1129948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1130948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1131948d887dSJohannes Berg 			more_data = true;
1132948d887dSJohannes Berg 			break;
1133948d887dSJohannes Berg 		}
1134948d887dSJohannes Berg 	}
1135af818581SJohannes Berg 
11364049e09aSJohannes Berg 	if (!found) {
1137ce662b44SJohannes Berg 		int tid;
11384049e09aSJohannes Berg 
1139ce662b44SJohannes Berg 		/*
1140ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1141ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1142ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1143ce662b44SJohannes Berg 		 *
1144ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1145ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1146ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1147ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1148ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1149ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1150ce662b44SJohannes Berg 		 *
1151ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1152ce662b44SJohannes Berg 		 */
1153ce662b44SJohannes Berg 
1154ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1155ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1156ce662b44SJohannes Berg 
115740b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
11584049e09aSJohannes Berg 		return;
11594049e09aSJohannes Berg 	}
11604049e09aSJohannes Berg 
116147086fc5SJohannes Berg 	if (!driver_release_tids) {
116247086fc5SJohannes Berg 		struct sk_buff_head pending;
116347086fc5SJohannes Berg 		struct sk_buff *skb;
116440b96408SJohannes Berg 		int num = 0;
116540b96408SJohannes Berg 		u16 tids = 0;
116647086fc5SJohannes Berg 
116747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
116847086fc5SJohannes Berg 
116947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1170af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
117147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
117240b96408SJohannes Berg 			u8 *qoshdr = NULL;
117340b96408SJohannes Berg 
117440b96408SJohannes Berg 			num++;
1175af818581SJohannes Berg 
1176af818581SJohannes Berg 			/*
117747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
117847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
117947086fc5SJohannes Berg 			 * exchange.
1180af818581SJohannes Berg 			 */
118102f2f1a9SJohannes Berg 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1182af818581SJohannes Berg 
118347086fc5SJohannes Berg 			/*
118447086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
118547086fc5SJohannes Berg 			 * more buffered frames for this STA
118647086fc5SJohannes Berg 			 */
118724b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
118847086fc5SJohannes Berg 				hdr->frame_control |=
118947086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
119024b9c373SJanusz.Dziedzic@tieto.com 			else
119124b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
119224b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1193af818581SJohannes Berg 
119440b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
119540b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
119640b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
119740b96408SJohannes Berg 
119847086fc5SJohannes Berg 			/* set EOSP for the frame */
119940b96408SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
120040b96408SJohannes Berg 			    qoshdr && skb_queue_empty(&frames))
120140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1202deeaee19SJohannes Berg 
120347086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_STATUS_EOSP |
120447086fc5SJohannes Berg 				       IEEE80211_TX_CTL_REQ_TX_STATUS;
120547086fc5SJohannes Berg 
120640b96408SJohannes Berg 			if (qoshdr)
120740b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
120840b96408SJohannes Berg 			else
120940b96408SJohannes Berg 				tids |= BIT(0);
121040b96408SJohannes Berg 
121147086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
121247086fc5SJohannes Berg 		}
121347086fc5SJohannes Berg 
121440b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
121540b96408SJohannes Berg 					  reason, more_data);
121640b96408SJohannes Berg 
121747086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1218af818581SJohannes Berg 
1219c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1220af818581SJohannes Berg 	} else {
1221af818581SJohannes Berg 		/*
12224049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
12234049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
12244049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
12254049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
12264049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
12274049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
12284049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
12294049e09aSJohannes Berg 		 * needs to be set anyway.
1230af818581SJohannes Berg 		 */
12314049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
123247086fc5SJohannes Berg 					    n_frames, reason, more_data);
12334049e09aSJohannes Berg 
12344049e09aSJohannes Berg 		/*
12354049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
12364049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
12374049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
12384049e09aSJohannes Berg 		 * release function.
12394049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
12404049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
12414049e09aSJohannes Berg 		 */
1242af818581SJohannes Berg 	}
1243af818581SJohannes Berg }
1244af818581SJohannes Berg 
1245af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1246af818581SJohannes Berg {
124747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1248af818581SJohannes Berg 
1249af818581SJohannes Berg 	/*
125047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
125147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
125247086fc5SJohannes Berg 	 * only from the non-enabled ones.
1253af818581SJohannes Berg 	 */
125447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
125547086fc5SJohannes Berg 		ignore_for_response = 0;
1256af818581SJohannes Berg 
125747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
125847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1259af818581SJohannes Berg }
126047086fc5SJohannes Berg 
126147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
126247086fc5SJohannes Berg {
126347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
126447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
126547086fc5SJohannes Berg 
126647086fc5SJohannes Berg 	/*
126747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
126847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
126947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
127047086fc5SJohannes Berg 	 * actually getting called.
127147086fc5SJohannes Berg 	 */
127247086fc5SJohannes Berg 	if (!delivery_enabled)
127347086fc5SJohannes Berg 		return;
127447086fc5SJohannes Berg 
127547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
127647086fc5SJohannes Berg 	case 1:
127747086fc5SJohannes Berg 		n_frames = 2;
127847086fc5SJohannes Berg 		break;
127947086fc5SJohannes Berg 	case 2:
128047086fc5SJohannes Berg 		n_frames = 4;
128147086fc5SJohannes Berg 		break;
128247086fc5SJohannes Berg 	case 3:
128347086fc5SJohannes Berg 		n_frames = 6;
128447086fc5SJohannes Berg 		break;
128547086fc5SJohannes Berg 	case 0:
128647086fc5SJohannes Berg 		/* XXX: what is a good value? */
128747086fc5SJohannes Berg 		n_frames = 8;
128847086fc5SJohannes Berg 		break;
128947086fc5SJohannes Berg 	}
129047086fc5SJohannes Berg 
129147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
129247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1293af818581SJohannes Berg }
1294af818581SJohannes Berg 
1295af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1296af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1297af818581SJohannes Berg {
1298af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1299af818581SJohannes Berg 
1300b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1301b5878a2dSJohannes Berg 
1302af818581SJohannes Berg 	if (block)
1303c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1304c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1305af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1306af818581SJohannes Berg }
1307af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1308dcf55fb5SFelix Fietkau 
130937fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
131037fbd908SJohannes Berg {
131137fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
131237fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
131337fbd908SJohannes Berg 	struct sk_buff *skb;
131437fbd908SJohannes Berg 	struct skb_eosp_msg_data *data;
131537fbd908SJohannes Berg 
131637fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
131737fbd908SJohannes Berg 
131837fbd908SJohannes Berg 	skb = alloc_skb(0, GFP_ATOMIC);
131937fbd908SJohannes Berg 	if (!skb) {
132037fbd908SJohannes Berg 		/* too bad ... but race is better than loss */
132137fbd908SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_SP);
132237fbd908SJohannes Berg 		return;
132337fbd908SJohannes Berg 	}
132437fbd908SJohannes Berg 
132537fbd908SJohannes Berg 	data = (void *)skb->cb;
132637fbd908SJohannes Berg 	memcpy(data->sta, pubsta->addr, ETH_ALEN);
132737fbd908SJohannes Berg 	memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
132837fbd908SJohannes Berg 	skb->pkt_type = IEEE80211_EOSP_MSG;
132937fbd908SJohannes Berg 	skb_queue_tail(&local->skb_queue, skb);
133037fbd908SJohannes Berg 	tasklet_schedule(&local->tasklet);
133137fbd908SJohannes Berg }
133237fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
133337fbd908SJohannes Berg 
1334042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1335042ec453SJohannes Berg 				u8 tid, bool buffered)
1336dcf55fb5SFelix Fietkau {
1337dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1338dcf55fb5SFelix Fietkau 
1339948d887dSJohannes Berg 	if (WARN_ON(tid >= STA_TID_NUM))
1340042ec453SJohannes Berg 		return;
1341042ec453SJohannes Berg 
1342948d887dSJohannes Berg 	if (buffered)
1343948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1344948d887dSJohannes Berg 	else
1345948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1346948d887dSJohannes Berg 
1347c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1348dcf55fb5SFelix Fietkau }
1349042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1350d9a7ddb0SJohannes Berg 
135183d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1352d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1353d9a7ddb0SJohannes Berg {
13548bf11d8dSJohannes Berg 	might_sleep();
1355d9a7ddb0SJohannes Berg 
1356d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1357d9a7ddb0SJohannes Berg 		return 0;
1358d9a7ddb0SJohannes Berg 
1359f09603a2SJohannes Berg 	/* check allowed transitions first */
1360f09603a2SJohannes Berg 
1361d9a7ddb0SJohannes Berg 	switch (new_state) {
1362d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1363f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1364d9a7ddb0SJohannes Berg 			return -EINVAL;
1365d9a7ddb0SJohannes Berg 		break;
1366d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1367f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1368f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1369d9a7ddb0SJohannes Berg 			return -EINVAL;
1370d9a7ddb0SJohannes Berg 		break;
1371d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1372f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1373f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1374d9a7ddb0SJohannes Berg 			return -EINVAL;
1375d9a7ddb0SJohannes Berg 		break;
1376d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1377f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1378d9a7ddb0SJohannes Berg 			return -EINVAL;
1379d9a7ddb0SJohannes Berg 		break;
1380d9a7ddb0SJohannes Berg 	default:
1381d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1382d9a7ddb0SJohannes Berg 		return -EINVAL;
1383d9a7ddb0SJohannes Berg 	}
1384d9a7ddb0SJohannes Berg 
138579027596SFelix Fietkau #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1386d9a7ddb0SJohannes Berg 	printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
1387d9a7ddb0SJohannes Berg 		sta->sdata->name, sta->sta.addr, new_state);
138879027596SFelix Fietkau #endif
1389f09603a2SJohannes Berg 
1390f09603a2SJohannes Berg 	/*
1391f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1392f09603a2SJohannes Berg 	 * fail the transition
1393f09603a2SJohannes Berg 	 */
1394f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1395f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1396f09603a2SJohannes Berg 					sta->sta_state, new_state);
1397f09603a2SJohannes Berg 		if (err)
1398f09603a2SJohannes Berg 			return err;
1399f09603a2SJohannes Berg 	}
1400f09603a2SJohannes Berg 
1401f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1402f09603a2SJohannes Berg 
1403f09603a2SJohannes Berg 	switch (new_state) {
1404f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1405f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1406f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1407f09603a2SJohannes Berg 		break;
1408f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1409f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1410f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1411f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1412f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1413f09603a2SJohannes Berg 		break;
1414f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1415f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1416f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1417f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1418f09603a2SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1419f09603a2SJohannes Berg 				atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
1420f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1421f09603a2SJohannes Berg 		}
1422f09603a2SJohannes Berg 		break;
1423f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1424f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
1425f09603a2SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1426f09603a2SJohannes Berg 				atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
1427f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1428f09603a2SJohannes Berg 		}
1429f09603a2SJohannes Berg 		break;
1430f09603a2SJohannes Berg 	default:
1431f09603a2SJohannes Berg 		break;
1432f09603a2SJohannes Berg 	}
1433f09603a2SJohannes Berg 
1434d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1435d9a7ddb0SJohannes Berg 
1436d9a7ddb0SJohannes Berg 	return 0;
1437d9a7ddb0SJohannes Berg }
1438