xref: /openbmc/linux/net/mac80211/sta_info.c (revision af65cd96dd4ea8ea5adc6ee850e61a407cd1067a)
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"
27f0706e82SJiri Benc 
28d0709a65SJohannes Berg /**
29d0709a65SJohannes Berg  * DOC: STA information lifetime rules
30d0709a65SJohannes Berg  *
31d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
32d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
33d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
34d0709a65SJohannes Berg  *
3503e4497eSJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
3603e4497eSJohannes Berg  * that structure. It must then either destroy it using sta_info_destroy()
3703e4497eSJohannes Berg  * (which is pretty useless) or insert it into the hash table using
3803e4497eSJohannes Berg  * sta_info_insert() which demotes the reference from ownership to a regular
3903e4497eSJohannes Berg  * RCU-protected reference; if the function is called without protection by an
4093e5deb1SJohannes Berg  * RCU critical section the reference is instantly invalidated. Note that the
4193e5deb1SJohannes Berg  * caller may not do much with the STA info before inserting it, in particular,
4293e5deb1SJohannes Berg  * it may not start any mesh peer link management or add encryption keys.
4393e5deb1SJohannes Berg  *
4493e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4593e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
46d0709a65SJohannes Berg  *
477e189a12SLuis R. Rodriguez  * sta entries are added by mac80211 when you establish a link with a
487e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
497e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
507e189a12SLuis R. Rodriguez  * receive an assocation response from the AP. For IBSS this occurs when
517e189a12SLuis R. Rodriguez  * we receive a probe response or a beacon from target IBSS network. For
527e189a12SLuis R. Rodriguez  * WDS we add the sta for the peer imediately upon device open. When using
537e189a12SLuis R. Rodriguez  * AP mode we add stations for each respective station upon request from
547e189a12SLuis R. Rodriguez  * userspace through nl80211.
557e189a12SLuis R. Rodriguez  *
56d0709a65SJohannes Berg  * Because there are debugfs entries for each station, and adding those
57d0709a65SJohannes Berg  * must be able to sleep, it is also possible to "pin" a station entry,
58d0709a65SJohannes Berg  * that means it can be removed from the hash table but not be freed.
5993e5deb1SJohannes Berg  * See the comment in __sta_info_unlink() for more information, this is
6093e5deb1SJohannes Berg  * an internal capability only.
61d0709a65SJohannes Berg  *
62d0709a65SJohannes Berg  * In order to remove a STA info structure, the caller needs to first
63dbbea671SJohannes Berg  * unlink it (sta_info_unlink()) from the list and hash tables and
643b96766fSJohannes Berg  * then destroy it; sta_info_destroy() will wait for an RCU grace period
653b96766fSJohannes Berg  * to elapse before actually freeing it. Due to the pinning and the
663b96766fSJohannes Berg  * possibility of multiple callers trying to remove the same STA info at
673b96766fSJohannes Berg  * the same time, sta_info_unlink() can clear the STA info pointer it is
683b96766fSJohannes Berg  * passed to indicate that the STA info is owned by somebody else now.
69d0709a65SJohannes Berg  *
70dbbea671SJohannes Berg  * If sta_info_unlink() did not clear the pointer then the caller owns
71d0709a65SJohannes Berg  * the STA info structure now and is responsible of destroying it with
723b96766fSJohannes Berg  * a call to sta_info_destroy().
73d0709a65SJohannes Berg  *
74d0709a65SJohannes Berg  * In all other cases, there is no concept of ownership on a STA entry,
75d0709a65SJohannes Berg  * each structure is owned by the global hash table/list until it is
76d0709a65SJohannes Berg  * removed. All users of the structure need to be RCU protected so that
77d0709a65SJohannes Berg  * the structure won't be freed before they are done using it.
78d0709a65SJohannes Berg  */
79f0706e82SJiri Benc 
80f0706e82SJiri Benc /* Caller must hold local->sta_lock */
81be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
82f0706e82SJiri Benc 			     struct sta_info *sta)
83f0706e82SJiri Benc {
84f0706e82SJiri Benc 	struct sta_info *s;
85f0706e82SJiri Benc 
8617741cdcSJohannes Berg 	s = local->sta_hash[STA_HASH(sta->sta.addr)];
87f0706e82SJiri Benc 	if (!s)
88be8755e1SMichael Wu 		return -ENOENT;
89be8755e1SMichael Wu 	if (s == sta) {
9017741cdcSJohannes Berg 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
91d0709a65SJohannes Berg 				   s->hnext);
92be8755e1SMichael Wu 		return 0;
93f0706e82SJiri Benc 	}
94f0706e82SJiri Benc 
95be8755e1SMichael Wu 	while (s->hnext && s->hnext != sta)
96f0706e82SJiri Benc 		s = s->hnext;
97be8755e1SMichael Wu 	if (s->hnext) {
98d0709a65SJohannes Berg 		rcu_assign_pointer(s->hnext, sta->hnext);
99be8755e1SMichael Wu 		return 0;
100f0706e82SJiri Benc 	}
101f0706e82SJiri Benc 
102be8755e1SMichael Wu 	return -ENOENT;
103f0706e82SJiri Benc }
104f0706e82SJiri Benc 
105d0709a65SJohannes Berg /* protected by RCU */
1064b7679a5SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
10743ba7e95SJohannes Berg {
10843ba7e95SJohannes Berg 	struct sta_info *sta;
10943ba7e95SJohannes Berg 
110d0709a65SJohannes Berg 	sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
11143ba7e95SJohannes Berg 	while (sta) {
1125cf12e8dSShaddy Baddah 		if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
11343ba7e95SJohannes Berg 			break;
114d0709a65SJohannes Berg 		sta = rcu_dereference(sta->hnext);
11543ba7e95SJohannes Berg 	}
11643ba7e95SJohannes Berg 	return sta;
11743ba7e95SJohannes Berg }
11843ba7e95SJohannes Berg 
1193b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1203b53fde8SJohannes Berg 				     int idx)
121ee385855SLuis Carlos Cobo {
1223b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
123ee385855SLuis Carlos Cobo 	struct sta_info *sta;
124ee385855SLuis Carlos Cobo 	int i = 0;
125ee385855SLuis Carlos Cobo 
126d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1273b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1282a8ca29aSLuis Carlos Cobo 			continue;
129ee385855SLuis Carlos Cobo 		if (i < idx) {
130ee385855SLuis Carlos Cobo 			++i;
131ee385855SLuis Carlos Cobo 			continue;
132ee385855SLuis Carlos Cobo 		}
1332a8ca29aSLuis Carlos Cobo 		return sta;
134ee385855SLuis Carlos Cobo 	}
135ee385855SLuis Carlos Cobo 
136ee385855SLuis Carlos Cobo 	return NULL;
137ee385855SLuis Carlos Cobo }
138f0706e82SJiri Benc 
13993e5deb1SJohannes Berg /**
14093e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
14193e5deb1SJohannes Berg  *
1426ef307bcSRandy Dunlap  * @local: pointer to the global information
14393e5deb1SJohannes Berg  * @sta: STA info to free
14493e5deb1SJohannes Berg  *
14593e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
14693e5deb1SJohannes Berg  * that may happen before sta_info_insert().
14793e5deb1SJohannes Berg  */
14893e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
14993e5deb1SJohannes Berg 			    struct sta_info *sta)
15093e5deb1SJohannes Berg {
151*af65cd96SJohannes Berg 	if (sta->rate_ctrl) {
1524b7679a5SJohannes Berg 		rate_control_free_sta(sta);
15393e5deb1SJohannes Berg 		rate_control_put(sta->rate_ctrl);
154*af65cd96SJohannes Berg 	}
15593e5deb1SJohannes Berg 
15693e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1570c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
1580c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
15993e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
16093e5deb1SJohannes Berg 
16193e5deb1SJohannes Berg 	kfree(sta);
16293e5deb1SJohannes Berg }
16393e5deb1SJohannes Berg 
164d0709a65SJohannes Berg void sta_info_destroy(struct sta_info *sta)
165f0706e82SJiri Benc {
16697bff8ecSJohannes Berg 	struct ieee80211_local *local;
167f0706e82SJiri Benc 	struct sk_buff *skb;
16807db2183SRon Rindjunsky 	int i;
16973651ee6SJohannes Berg 
17097bff8ecSJohannes Berg 	might_sleep();
17197bff8ecSJohannes Berg 
17273651ee6SJohannes Berg 	if (!sta)
17373651ee6SJohannes Berg 		return;
174f0706e82SJiri Benc 
17597bff8ecSJohannes Berg 	local = sta->local;
176d0709a65SJohannes Berg 
177af818581SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
178af818581SJohannes Berg 
179d0709a65SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
180d0709a65SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
181d0709a65SJohannes Berg 
182d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
183d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
184d0709a65SJohannes Berg 		mesh_plink_deactivate(sta);
185d0709a65SJohannes Berg #endif
186d0709a65SJohannes Berg 
187d0709a65SJohannes Berg 	/*
1883b96766fSJohannes Berg 	 * We have only unlinked the key, and actually destroying it
1893b96766fSJohannes Berg 	 * may mean it is removed from hardware which requires that
1903b96766fSJohannes Berg 	 * the key->sta pointer is still valid, so flush the key todo
1913b96766fSJohannes Berg 	 * list here.
1923b96766fSJohannes Berg 	 *
1933b96766fSJohannes Berg 	 * ieee80211_key_todo() will synchronize_rcu() so after this
1943b96766fSJohannes Berg 	 * nothing can reference this sta struct any more.
195d0709a65SJohannes Berg 	 */
1963b96766fSJohannes Berg 	ieee80211_key_todo();
197d0709a65SJohannes Berg 
198d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
199d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
200d0709a65SJohannes Berg 		del_timer_sync(&sta->plink_timer);
201d0709a65SJohannes Berg #endif
202d0709a65SJohannes Berg 
203f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
204f0706e82SJiri Benc 		local->total_ps_buffered--;
205f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
206f0706e82SJiri Benc 	}
207d0709a65SJohannes Berg 
208d0709a65SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
209f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
210d0709a65SJohannes Berg 
211fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
21255687e38SJohannes Berg 		struct tid_ampdu_rx *tid_rx;
21355687e38SJohannes Berg 		struct tid_ampdu_tx *tid_tx;
21455687e38SJohannes Berg 
21507346f81SJohannes Berg 		spin_lock_bh(&sta->lock);
21655687e38SJohannes Berg 		tid_rx = sta->ampdu_mlme.tid_rx[i];
21755687e38SJohannes Berg 		/* Make sure timer won't free the tid_rx struct, see below */
21855687e38SJohannes Berg 		if (tid_rx)
21955687e38SJohannes Berg 			tid_rx->shutdown = true;
22096f5e66eSJohannes Berg 
22107346f81SJohannes Berg 		spin_unlock_bh(&sta->lock);
22255687e38SJohannes Berg 
22355687e38SJohannes Berg 		/*
22455687e38SJohannes Berg 		 * Outside spinlock - shutdown is true now so that the timer
22555687e38SJohannes Berg 		 * won't free tid_rx, we have to do that now. Can't let the
22655687e38SJohannes Berg 		 * timer do it because we have to sync the timer outside the
22755687e38SJohannes Berg 		 * lock that it takes itself.
22855687e38SJohannes Berg 		 */
22955687e38SJohannes Berg 		if (tid_rx) {
23055687e38SJohannes Berg 			del_timer_sync(&tid_rx->session_timer);
23155687e38SJohannes Berg 			kfree(tid_rx);
23255687e38SJohannes Berg 		}
23355687e38SJohannes Berg 
23455687e38SJohannes Berg 		/*
23555687e38SJohannes Berg 		 * No need to do such complications for TX agg sessions, the
23655687e38SJohannes Berg 		 * path leading to freeing the tid_tx struct goes via a call
23755687e38SJohannes Berg 		 * from the driver, and thus needs to look up the sta struct
23855687e38SJohannes Berg 		 * again, which cannot be found when we get here. Hence, we
23955687e38SJohannes Berg 		 * just need to delete the timer and free the aggregation
24055687e38SJohannes Berg 		 * info; we won't be telling the peer about it then but that
24155687e38SJohannes Berg 		 * doesn't matter if we're not talking to it again anyway.
24255687e38SJohannes Berg 		 */
24355687e38SJohannes Berg 		tid_tx = sta->ampdu_mlme.tid_tx[i];
24455687e38SJohannes Berg 		if (tid_tx) {
24555687e38SJohannes Berg 			del_timer_sync(&tid_tx->addba_resp_timer);
246cd8ffc80SJohannes Berg 			/*
247cd8ffc80SJohannes Berg 			 * STA removed while aggregation session being
248cd8ffc80SJohannes Berg 			 * started? Bit odd, but purge frames anyway.
249cd8ffc80SJohannes Berg 			 */
250cd8ffc80SJohannes Berg 			skb_queue_purge(&tid_tx->pending);
25155687e38SJohannes Berg 			kfree(tid_tx);
25255687e38SJohannes Berg 		}
253fe3bf0f5SRon Rindjunsky 	}
254cee24a3eSRon Rindjunsky 
25593e5deb1SJohannes Berg 	__sta_info_free(local, sta);
256f0706e82SJiri Benc }
257f0706e82SJiri Benc 
258f0706e82SJiri Benc 
259d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
260d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
261d0709a65SJohannes Berg 			      struct sta_info *sta)
262f0706e82SJiri Benc {
26317741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
26417741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
265f0706e82SJiri Benc }
266f0706e82SJiri Benc 
267af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
268af818581SJohannes Berg {
269af818581SJohannes Berg 	struct sta_info *sta;
270af818581SJohannes Berg 
271af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
272af818581SJohannes Berg 
273af818581SJohannes Berg 	if (sta->dead)
274af818581SJohannes Berg 		return;
275af818581SJohannes Berg 
276af818581SJohannes Berg 	if (!test_sta_flags(sta, WLAN_STA_PS_STA))
277af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
278af818581SJohannes Berg 	else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
279af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
280af818581SJohannes Berg }
281af818581SJohannes Berg 
282*af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
283*af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
284*af65cd96SJohannes Berg {
285*af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
286*af65cd96SJohannes Berg 		return 0;
287*af65cd96SJohannes Berg 
288*af65cd96SJohannes Berg 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
289*af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
290*af65cd96SJohannes Berg 						     &sta->sta, gfp);
291*af65cd96SJohannes Berg 	if (!sta->rate_ctrl_priv) {
292*af65cd96SJohannes Berg 		rate_control_put(sta->rate_ctrl);
293*af65cd96SJohannes Berg 		return -ENOMEM;
294*af65cd96SJohannes Berg 	}
295*af65cd96SJohannes Berg 
296*af65cd96SJohannes Berg 	return 0;
297*af65cd96SJohannes Berg }
298*af65cd96SJohannes Berg 
29973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30073651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
301f0706e82SJiri Benc {
302d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
303f0706e82SJiri Benc 	struct sta_info *sta;
30416c5f15cSRon Rindjunsky 	int i;
305f0706e82SJiri Benc 
30617741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
307f0706e82SJiri Benc 	if (!sta)
30873651ee6SJohannes Berg 		return NULL;
309f0706e82SJiri Benc 
31007346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3115a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
312af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
31307346f81SJohannes Berg 
31417741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
315d0709a65SJohannes Berg 	sta->local = local;
316d0709a65SJohannes Berg 	sta->sdata = sdata;
317f0706e82SJiri Benc 
318*af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
319f0706e82SJiri Benc 		kfree(sta);
32073651ee6SJohannes Berg 		return NULL;
321f0706e82SJiri Benc 	}
322f0706e82SJiri Benc 
32316c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
32416c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
32516c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
32616c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
32716c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
328cee24a3eSRon Rindjunsky 		/* rx */
329cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
330cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
331cee24a3eSRon Rindjunsky 		/* tx */
332cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
333cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
334cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
33516c5f15cSRon Rindjunsky 	}
336f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
337f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
33873651ee6SJohannes Berg 
339cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
340cccaec98SSenthil Balasubramanian 		sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
341cccaec98SSenthil Balasubramanian 
34273651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3430c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %pM\n",
3440c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
34573651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
34673651ee6SJohannes Berg 
34703e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
348b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
34903e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
35003e4497eSJohannes Berg #endif
35103e4497eSJohannes Berg 
35273651ee6SJohannes Berg 	return sta;
35373651ee6SJohannes Berg }
35473651ee6SJohannes Berg 
35573651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta)
35673651ee6SJohannes Berg {
35773651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
35873651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
35973651ee6SJohannes Berg 	unsigned long flags;
36093e5deb1SJohannes Berg 	int err = 0;
36173651ee6SJohannes Berg 
36203e4497eSJohannes Berg 	/*
36303e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
36403e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
36503e4497eSJohannes Berg 	 * and another CPU turns off the net device.
36603e4497eSJohannes Berg 	 */
36793e5deb1SJohannes Berg 	if (unlikely(!netif_running(sdata->dev))) {
36893e5deb1SJohannes Berg 		err = -ENETDOWN;
36993e5deb1SJohannes Berg 		goto out_free;
37093e5deb1SJohannes Berg 	}
37103e4497eSJohannes Berg 
37217741cdcSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
37317741cdcSJohannes Berg 		    is_multicast_ether_addr(sta->sta.addr))) {
37493e5deb1SJohannes Berg 		err = -EINVAL;
37593e5deb1SJohannes Berg 		goto out_free;
37693e5deb1SJohannes Berg 	}
37744213b5eSJohannes Berg 
378d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
37943ba7e95SJohannes Berg 	/* check if STA exists already */
3804b7679a5SJohannes Berg 	if (sta_info_get(local, sta->sta.addr)) {
381d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
38293e5deb1SJohannes Berg 		err = -EEXIST;
38393e5deb1SJohannes Berg 		goto out_free;
38443ba7e95SJohannes Berg 	}
385f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
386f5ea9120SJohannes Berg 	local->sta_generation++;
387f0706e82SJiri Benc 	local->num_sta++;
388f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
38932bfd35dSJohannes Berg 
390d0709a65SJohannes Berg 	/* notify driver */
391d0709a65SJohannes Berg 	if (local->ops->sta_notify) {
39205c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3933e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
3943e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
3953e122be0SJohannes Berg 					     u.ap);
39632bfd35dSJohannes Berg 
39724487981SJohannes Berg 		drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
398fbc44bf7SJohannes Berg 		sdata = sta->sdata;
39932bfd35dSJohannes Berg 	}
400d0709a65SJohannes Berg 
401f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4020c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %pM\n",
4030c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
404f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
405f0706e82SJiri Benc 
40673651ee6SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
40773651ee6SJohannes Berg 
408e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
40993e5deb1SJohannes Berg 	/*
41093e5deb1SJohannes Berg 	 * Debugfs entry adding might sleep, so schedule process
411e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
41293e5deb1SJohannes Berg 	 * have one.
41393e5deb1SJohannes Berg 	 * NOTE: due to auto-freeing semantics this may only be done
41493e5deb1SJohannes Berg 	 *       if the insertion is successful!
41593e5deb1SJohannes Berg 	 */
41649ec6fa2SJohannes Berg 	schedule_work(&local->sta_debugfs_add);
417e9f207f0SJiri Benc #endif
418e9f207f0SJiri Benc 
41973651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
42073651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
42173651ee6SJohannes Berg 
42273651ee6SJohannes Berg 	return 0;
42393e5deb1SJohannes Berg  out_free:
42493e5deb1SJohannes Berg 	BUG_ON(!err);
42593e5deb1SJohannes Berg 	__sta_info_free(local, sta);
42693e5deb1SJohannes Berg 	return err;
427f0706e82SJiri Benc }
428f0706e82SJiri Benc 
429004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
430004c872eSJohannes Berg {
431004c872eSJohannes Berg 	/*
432004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
433004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
434004c872eSJohannes Berg 	 */
435004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
436004c872eSJohannes Berg }
437004c872eSJohannes Berg 
438004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
439004c872eSJohannes Berg {
440004c872eSJohannes Berg 	/*
441004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
442004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
443004c872eSJohannes Berg 	 */
444004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
445004c872eSJohannes Berg }
446004c872eSJohannes Berg 
447004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
448004c872eSJohannes Berg 				   struct sta_info *sta)
449004c872eSJohannes Berg {
4503e122be0SJohannes Berg 	BUG_ON(!bss);
4513e122be0SJohannes Berg 
45217741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
4533e122be0SJohannes Berg 
454d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
455d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
45624487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, true);
457d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
458d0709a65SJohannes Berg 	}
459004c872eSJohannes Berg }
460004c872eSJohannes Berg 
461004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
462004c872eSJohannes Berg {
463d0709a65SJohannes Berg 	unsigned long flags;
464004c872eSJohannes Berg 
4653e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4663e122be0SJohannes Berg 
467d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
468d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
469d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
470004c872eSJohannes Berg }
471004c872eSJohannes Berg 
472004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
473004c872eSJohannes Berg 				     struct sta_info *sta)
474004c872eSJohannes Berg {
4753e122be0SJohannes Berg 	BUG_ON(!bss);
4763e122be0SJohannes Berg 
47717741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
4783e122be0SJohannes Berg 
479d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
480d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
48124487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, false);
482d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
483d0709a65SJohannes Berg 	}
484004c872eSJohannes Berg }
485004c872eSJohannes Berg 
486004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
487004c872eSJohannes Berg {
488d0709a65SJohannes Berg 	unsigned long flags;
489004c872eSJohannes Berg 
4903e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4913e122be0SJohannes Berg 
492d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
493d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
494d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
495004c872eSJohannes Berg }
496004c872eSJohannes Berg 
49724723d1bSJohannes Berg static void __sta_info_unlink(struct sta_info **sta)
498d0709a65SJohannes Berg {
499d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
500d0709a65SJohannes Berg 	struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
501d0709a65SJohannes Berg 	/*
502d0709a65SJohannes Berg 	 * pull caller's reference if we're already gone.
503d0709a65SJohannes Berg 	 */
504d0709a65SJohannes Berg 	if (sta_info_hash_del(local, *sta)) {
505d0709a65SJohannes Berg 		*sta = NULL;
506be8755e1SMichael Wu 		return;
507d0709a65SJohannes Berg 	}
508be8755e1SMichael Wu 
5093b96766fSJohannes Berg 	if ((*sta)->key) {
5103b96766fSJohannes Berg 		ieee80211_key_free((*sta)->key);
5113b96766fSJohannes Berg 		WARN_ON((*sta)->key);
5123b96766fSJohannes Berg 	}
5133b96766fSJohannes Berg 
5147d1559f1SJohannes Berg 	list_del(&(*sta)->list);
515af818581SJohannes Berg 	(*sta)->dead = true;
5167d1559f1SJohannes Berg 
517af818581SJohannes Berg 	if (test_and_clear_sta_flags(*sta,
518af818581SJohannes Berg 				WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
5193e122be0SJohannes Berg 		BUG_ON(!sdata->bss);
5203e122be0SJohannes Berg 
5217d1559f1SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
5227d1559f1SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, *sta);
5237d1559f1SJohannes Berg 	}
5247d1559f1SJohannes Berg 
5257d1559f1SJohannes Berg 	local->num_sta--;
526f5ea9120SJohannes Berg 	local->sta_generation++;
5277d1559f1SJohannes Berg 
528f14543eeSFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
529f14543eeSFelix Fietkau 		rcu_assign_pointer(sdata->u.vlan.sta, NULL);
530f14543eeSFelix Fietkau 
5317d1559f1SJohannes Berg 	if (local->ops->sta_notify) {
53205c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5333e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
5343e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
5353e122be0SJohannes Berg 					     u.ap);
5367d1559f1SJohannes Berg 
53724487981SJohannes Berg 		drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
53824487981SJohannes Berg 			       &(*sta)->sta);
539fbc44bf7SJohannes Berg 		sdata = (*sta)->sdata;
5407d1559f1SJohannes Berg 	}
5417d1559f1SJohannes Berg 
5427d1559f1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
5437d1559f1SJohannes Berg 		mesh_accept_plinks_update(sdata);
5447d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
5457d1559f1SJohannes Berg 		del_timer(&(*sta)->plink_timer);
5467d1559f1SJohannes Berg #endif
5477d1559f1SJohannes Berg 	}
5487d1559f1SJohannes Berg 
5497d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
5500c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %pM\n",
5510c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
5527d1559f1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
5537d1559f1SJohannes Berg 
554d0709a65SJohannes Berg 	/*
5557d1559f1SJohannes Berg 	 * Finally, pull caller's reference if the STA is pinned by the
556d0709a65SJohannes Berg 	 * task that is adding the debugfs entries. In that case, we
557d0709a65SJohannes Berg 	 * leave the STA "to be freed".
558d0709a65SJohannes Berg 	 *
559d0709a65SJohannes Berg 	 * The rules are not trivial, but not too complex either:
560d0709a65SJohannes Berg 	 *  (1) pin_status is only modified under the sta_lock
56149ec6fa2SJohannes Berg 	 *  (2) STAs may only be pinned under the RTNL so that
56249ec6fa2SJohannes Berg 	 *	sta_info_flush() is guaranteed to actually destroy
56349ec6fa2SJohannes Berg 	 *	all STAs that are active for a given interface, this
56449ec6fa2SJohannes Berg 	 *	is required for correctness because otherwise we
56549ec6fa2SJohannes Berg 	 *	could notify a driver that an interface is going
56649ec6fa2SJohannes Berg 	 *	away and only after that (!) notify it about a STA
56749ec6fa2SJohannes Berg 	 *	on that interface going away.
56849ec6fa2SJohannes Berg 	 *  (3) sta_info_debugfs_add_work() will set the status
569d0709a65SJohannes Berg 	 *	to PINNED when it found an item that needs a new
570d0709a65SJohannes Berg 	 *	debugfs directory created. In that case, that item
571d0709a65SJohannes Berg 	 *	must not be freed although all *RCU* users are done
572d0709a65SJohannes Berg 	 *	with it. Hence, we tell the caller of _unlink()
573d0709a65SJohannes Berg 	 *	that the item is already gone (as can happen when
574d0709a65SJohannes Berg 	 *	two tasks try to unlink/destroy at the same time)
57549ec6fa2SJohannes Berg 	 *  (4) We set the pin_status to DESTROY here when we
576d0709a65SJohannes Berg 	 *	find such an item.
57749ec6fa2SJohannes Berg 	 *  (5) sta_info_debugfs_add_work() will reset the pin_status
578d0709a65SJohannes Berg 	 *	from PINNED to NORMAL when it is done with the item,
579d0709a65SJohannes Berg 	 *	but will check for DESTROY before resetting it in
580d0709a65SJohannes Berg 	 *	which case it will free the item.
581d0709a65SJohannes Berg 	 */
582d0709a65SJohannes Berg 	if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
583d0709a65SJohannes Berg 		(*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
584d0709a65SJohannes Berg 		*sta = NULL;
585d0709a65SJohannes Berg 		return;
586d0709a65SJohannes Berg 	}
587d0709a65SJohannes Berg }
588d0709a65SJohannes Berg 
589d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta)
590d0709a65SJohannes Berg {
591d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
592d0709a65SJohannes Berg 	unsigned long flags;
593d0709a65SJohannes Berg 
594d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
595d0709a65SJohannes Berg 	__sta_info_unlink(sta);
596d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
597d0709a65SJohannes Berg }
598f0706e82SJiri Benc 
59957c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta,
600f0706e82SJiri Benc 				   struct sk_buff *skb)
601f0706e82SJiri Benc {
602e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
603f0706e82SJiri Benc 	int timeout;
604f0706e82SJiri Benc 
605f0706e82SJiri Benc 	if (!skb)
606f0706e82SJiri Benc 		return 0;
607f0706e82SJiri Benc 
608e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
609f0706e82SJiri Benc 
610f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
61157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
61257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
61357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
614f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
615f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
616e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
617f0706e82SJiri Benc }
618f0706e82SJiri Benc 
619f0706e82SJiri Benc 
620f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
621f0706e82SJiri Benc 					     struct sta_info *sta)
622f0706e82SJiri Benc {
623f0706e82SJiri Benc 	unsigned long flags;
624f0706e82SJiri Benc 	struct sk_buff *skb;
625836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
626f0706e82SJiri Benc 
627f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
628f0706e82SJiri Benc 		return;
629f0706e82SJiri Benc 
630f0706e82SJiri Benc 	for (;;) {
631f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
632f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
63357c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
634f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
635836341a7SJohannes Berg 		else
636f0706e82SJiri Benc 			skb = NULL;
637f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
638f0706e82SJiri Benc 
639836341a7SJohannes Berg 		if (!skb)
640836341a7SJohannes Berg 			break;
641836341a7SJohannes Berg 
642d0709a65SJohannes Berg 		sdata = sta->sdata;
643f0706e82SJiri Benc 		local->total_ps_buffered--;
644f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
6450c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
6460c68ae26SJohannes Berg 		       sta->sta.addr);
647f4ea83ddSJohannes Berg #endif
648f0706e82SJiri Benc 		dev_kfree_skb(skb);
649836341a7SJohannes Berg 
650004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
651004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
652f0706e82SJiri Benc 	}
653f0706e82SJiri Benc }
654f0706e82SJiri Benc 
655f0706e82SJiri Benc 
656f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
657f0706e82SJiri Benc {
658f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
659f0706e82SJiri Benc 	struct sta_info *sta;
660f0706e82SJiri Benc 
661d0709a65SJohannes Berg 	rcu_read_lock();
662d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
663f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
664d0709a65SJohannes Berg 	rcu_read_unlock();
665f0706e82SJiri Benc 
6665bb644a0SJohannes Berg 	if (local->quiescing)
6675bb644a0SJohannes Berg 		return;
6685bb644a0SJohannes Berg 
6690d174406SJohannes Berg 	local->sta_cleanup.expires =
6700d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
671f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
672f0706e82SJiri Benc }
673f0706e82SJiri Benc 
674e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
6754d6141c3SJiri Slaby /*
6764d6141c3SJiri Slaby  * See comment in __sta_info_unlink,
6774d6141c3SJiri Slaby  * caller must hold local->sta_lock.
6784d6141c3SJiri Slaby  */
6794d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta)
6804d6141c3SJiri Slaby {
6814d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
6824d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_PINNED;
6834d6141c3SJiri Slaby }
6844d6141c3SJiri Slaby 
6854d6141c3SJiri Slaby /*
6864d6141c3SJiri Slaby  * See comment in __sta_info_unlink, returns sta if it
6874d6141c3SJiri Slaby  * needs to be destroyed.
6884d6141c3SJiri Slaby  */
6894d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta)
6904d6141c3SJiri Slaby {
6914d6141c3SJiri Slaby 	struct sta_info *ret = NULL;
6924d6141c3SJiri Slaby 	unsigned long flags;
6934d6141c3SJiri Slaby 
6944d6141c3SJiri Slaby 	spin_lock_irqsave(&sta->local->sta_lock, flags);
6954d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
6964d6141c3SJiri Slaby 		sta->pin_status != STA_INFO_PIN_STAT_PINNED);
6974d6141c3SJiri Slaby 	if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
6984d6141c3SJiri Slaby 		ret = sta;
6994d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
7004d6141c3SJiri Slaby 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
7014d6141c3SJiri Slaby 
7024d6141c3SJiri Slaby 	return ret;
7034d6141c3SJiri Slaby }
7044d6141c3SJiri Slaby 
705d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work)
706e9f207f0SJiri Benc {
707e9f207f0SJiri Benc 	struct ieee80211_local *local =
708e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
709e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
710d0709a65SJohannes Berg 	unsigned long flags;
711e9f207f0SJiri Benc 
71249ec6fa2SJohannes Berg 	/* We need to keep the RTNL across the whole pinned status. */
71349ec6fa2SJohannes Berg 	rtnl_lock();
714e9f207f0SJiri Benc 	while (1) {
715e9f207f0SJiri Benc 		sta = NULL;
716d0709a65SJohannes Berg 
717d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
718e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
71963044e9fSJohannes Berg 			/*
72063044e9fSJohannes Berg 			 * debugfs.add_has_run will be set by
72163044e9fSJohannes Berg 			 * ieee80211_sta_debugfs_add regardless
72263044e9fSJohannes Berg 			 * of what else it does.
72363044e9fSJohannes Berg 			 */
72463044e9fSJohannes Berg 			if (!tmp->debugfs.add_has_run) {
725e9f207f0SJiri Benc 				sta = tmp;
726d0709a65SJohannes Berg 				__sta_info_pin(sta);
727e9f207f0SJiri Benc 				break;
728e9f207f0SJiri Benc 			}
729e9f207f0SJiri Benc 		}
730d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
731e9f207f0SJiri Benc 
732e9f207f0SJiri Benc 		if (!sta)
733e9f207f0SJiri Benc 			break;
734e9f207f0SJiri Benc 
735e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
736e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
737d0709a65SJohannes Berg 
738d0709a65SJohannes Berg 		sta = __sta_info_unpin(sta);
739d0709a65SJohannes Berg 		sta_info_destroy(sta);
740e9f207f0SJiri Benc 	}
74149ec6fa2SJohannes Berg 	rtnl_unlock();
742e9f207f0SJiri Benc }
743e9f207f0SJiri Benc #endif
744e9f207f0SJiri Benc 
745f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
746f0706e82SJiri Benc {
747d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
748f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
749f0706e82SJiri Benc 
750b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
751b24b8a24SPavel Emelyanov 		    (unsigned long)local);
7520d174406SJohannes Berg 	local->sta_cleanup.expires =
7530d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
754e9f207f0SJiri Benc 
755e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
756d0709a65SJohannes Berg 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
757e9f207f0SJiri Benc #endif
758f0706e82SJiri Benc }
759f0706e82SJiri Benc 
760f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
761f0706e82SJiri Benc {
762f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
763f0706e82SJiri Benc 	return 0;
764f0706e82SJiri Benc }
765f0706e82SJiri Benc 
766f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
767f0706e82SJiri Benc {
768f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
76949ec6fa2SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS
77049ec6fa2SJohannes Berg 	/*
77149ec6fa2SJohannes Berg 	 * Make sure the debugfs adding work isn't pending after this
77249ec6fa2SJohannes Berg 	 * because we're about to be destroyed. It doesn't matter
77349ec6fa2SJohannes Berg 	 * whether it ran or not since we're going to flush all STAs
77449ec6fa2SJohannes Berg 	 * anyway.
77549ec6fa2SJohannes Berg 	 */
77649ec6fa2SJohannes Berg 	cancel_work_sync(&local->sta_debugfs_add);
77749ec6fa2SJohannes Berg #endif
778dc6676b7SJohannes Berg 
779be8755e1SMichael Wu 	sta_info_flush(local, NULL);
780f0706e82SJiri Benc }
781f0706e82SJiri Benc 
782f0706e82SJiri Benc /**
783f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
78444213b5eSJohannes Berg  *
78544213b5eSJohannes Berg  * Returns the number of removed STA entries.
78644213b5eSJohannes Berg  *
787f0706e82SJiri Benc  * @local: local interface data
788d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
789f0706e82SJiri Benc  */
79044213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
791d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
792f0706e82SJiri Benc {
793f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
794be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
79544213b5eSJohannes Berg 	int ret = 0;
796d0709a65SJohannes Berg 	unsigned long flags;
797f0706e82SJiri Benc 
798d0709a65SJohannes Berg 	might_sleep();
799d0709a65SJohannes Berg 
800d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
801d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
802d0709a65SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
803d0709a65SJohannes Berg 			__sta_info_unlink(&sta);
80444213b5eSJohannes Berg 			if (sta) {
805be8755e1SMichael Wu 				list_add_tail(&sta->list, &tmp_list);
80644213b5eSJohannes Berg 				ret++;
80744213b5eSJohannes Berg 			}
808be8755e1SMichael Wu 		}
809be8755e1SMichael Wu 	}
810d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
811d0709a65SJohannes Berg 
812d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
813d0709a65SJohannes Berg 		sta_info_destroy(sta);
81444213b5eSJohannes Berg 
81544213b5eSJohannes Berg 	return ret;
816f0706e82SJiri Benc }
817dc6676b7SJohannes Berg 
81824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
81924723d1bSJohannes Berg 			  unsigned long exp_time)
82024723d1bSJohannes Berg {
82124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
82224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
82324723d1bSJohannes Berg 	LIST_HEAD(tmp_list);
82424723d1bSJohannes Berg 	unsigned long flags;
82524723d1bSJohannes Berg 
82624723d1bSJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
82724723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
82824723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
82924723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
8300c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
8310c68ae26SJohannes Berg 			       sdata->dev->name, sta->sta.addr);
83224723d1bSJohannes Berg #endif
83324723d1bSJohannes Berg 			__sta_info_unlink(&sta);
83424723d1bSJohannes Berg 			if (sta)
83524723d1bSJohannes Berg 				list_add(&sta->list, &tmp_list);
83624723d1bSJohannes Berg 		}
83724723d1bSJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
83824723d1bSJohannes Berg 
83924723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
84024723d1bSJohannes Berg 		sta_info_destroy(sta);
84124723d1bSJohannes Berg }
84217741cdcSJohannes Berg 
8435ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
84417741cdcSJohannes Berg 					       const u8 *addr)
84517741cdcSJohannes Berg {
8464b7679a5SJohannes Berg 	struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
84717741cdcSJohannes Berg 
84817741cdcSJohannes Berg 	if (!sta)
84917741cdcSJohannes Berg 		return NULL;
85017741cdcSJohannes Berg 	return &sta->sta;
85117741cdcSJohannes Berg }
8525ed176e1SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
8535ed176e1SJohannes Berg 
8545ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
8555ed176e1SJohannes Berg 					 const u8 *addr)
8565ed176e1SJohannes Berg {
8575ed176e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8585ed176e1SJohannes Berg 
8595ed176e1SJohannes Berg 	if (!vif)
8605ed176e1SJohannes Berg 		return NULL;
8615ed176e1SJohannes Berg 
8625ed176e1SJohannes Berg 	sdata = vif_to_sdata(vif);
8635ed176e1SJohannes Berg 
8645ed176e1SJohannes Berg 	return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
8655ed176e1SJohannes Berg }
86617741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
867af818581SJohannes Berg 
868af818581SJohannes Berg /* powersave support code */
869af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
870af818581SJohannes Berg {
871af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
872af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
873af818581SJohannes Berg 	int sent, buffered;
874af818581SJohannes Berg 
875af818581SJohannes Berg 	drv_sta_notify(local, &sdata->vif, STA_NOTIFY_AWAKE, &sta->sta);
876af818581SJohannes Berg 
877af818581SJohannes Berg 	if (!skb_queue_empty(&sta->ps_tx_buf))
878af818581SJohannes Berg 		sta_info_clear_tim_bit(sta);
879af818581SJohannes Berg 
880af818581SJohannes Berg 	/* Send all buffered frames to the station */
881af818581SJohannes Berg 	sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
882af818581SJohannes Berg 	buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
883af818581SJohannes Berg 	sent += buffered;
884af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
885af818581SJohannes Berg 
886af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
887af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
888af818581SJohannes Berg 	       "since STA not sleeping anymore\n", sdata->dev->name,
889af818581SJohannes Berg 	       sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
890af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
891af818581SJohannes Berg }
892af818581SJohannes Berg 
893af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
894af818581SJohannes Berg {
895af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
896af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
897af818581SJohannes Berg 	struct sk_buff *skb;
898af818581SJohannes Berg 	int no_pending_pkts;
899af818581SJohannes Berg 
900af818581SJohannes Berg 	skb = skb_dequeue(&sta->tx_filtered);
901af818581SJohannes Berg 	if (!skb) {
902af818581SJohannes Berg 		skb = skb_dequeue(&sta->ps_tx_buf);
903af818581SJohannes Berg 		if (skb)
904af818581SJohannes Berg 			local->total_ps_buffered--;
905af818581SJohannes Berg 	}
906af818581SJohannes Berg 	no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
907af818581SJohannes Berg 		skb_queue_empty(&sta->ps_tx_buf);
908af818581SJohannes Berg 
909af818581SJohannes Berg 	if (skb) {
910af818581SJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
911af818581SJohannes Berg 		struct ieee80211_hdr *hdr =
912af818581SJohannes Berg 			(struct ieee80211_hdr *) skb->data;
913af818581SJohannes Berg 
914af818581SJohannes Berg 		/*
915af818581SJohannes Berg 		 * Tell TX path to send this frame even though the STA may
916af818581SJohannes Berg 		 * still remain is PS mode after this frame exchange.
917af818581SJohannes Berg 		 */
918af818581SJohannes Berg 		info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
919af818581SJohannes Berg 
920af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
921af818581SJohannes Berg 		printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
922af818581SJohannes Berg 		       sta->sta.addr, sta->sta.aid,
923af818581SJohannes Berg 		       skb_queue_len(&sta->ps_tx_buf));
924af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
925af818581SJohannes Berg 
926af818581SJohannes Berg 		/* Use MoreData flag to indicate whether there are more
927af818581SJohannes Berg 		 * buffered frames for this STA */
928af818581SJohannes Berg 		if (no_pending_pkts)
929af818581SJohannes Berg 			hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
930af818581SJohannes Berg 		else
931af818581SJohannes Berg 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
932af818581SJohannes Berg 
933af818581SJohannes Berg 		ieee80211_add_pending_skb(local, skb);
934af818581SJohannes Berg 
935af818581SJohannes Berg 		if (no_pending_pkts)
936af818581SJohannes Berg 			sta_info_clear_tim_bit(sta);
937af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
938af818581SJohannes Berg 	} else {
939af818581SJohannes Berg 		/*
940af818581SJohannes Berg 		 * FIXME: This can be the result of a race condition between
941af818581SJohannes Berg 		 *	  us expiring a frame and the station polling for it.
942af818581SJohannes Berg 		 *	  Should we send it a null-func frame indicating we
943af818581SJohannes Berg 		 *	  have nothing buffered for it?
944af818581SJohannes Berg 		 */
945af818581SJohannes Berg 		printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
946af818581SJohannes Berg 		       "though there are no buffered frames for it\n",
947af818581SJohannes Berg 		       sdata->dev->name, sta->sta.addr);
948af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
949af818581SJohannes Berg 	}
950af818581SJohannes Berg }
951af818581SJohannes Berg 
952af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
953af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
954af818581SJohannes Berg {
955af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
956af818581SJohannes Berg 
957af818581SJohannes Berg 	if (block)
958af818581SJohannes Berg 		set_sta_flags(sta, WLAN_STA_PS_DRIVER);
959af818581SJohannes Berg 	else
960af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
961af818581SJohannes Berg }
962af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
963