xref: /openbmc/linux/net/mac80211/sta_info.c (revision 9607e6b66a0d25ca63b70d54a4283fa13d8f7c9d)
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 */
106abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
107abe60632SJohannes Berg 			      const u8 *addr)
10843ba7e95SJohannes Berg {
109abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
11043ba7e95SJohannes Berg 	struct sta_info *sta;
11143ba7e95SJohannes Berg 
112d0709a65SJohannes Berg 	sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
11343ba7e95SJohannes Berg 	while (sta) {
114abe60632SJohannes Berg 		if (sta->sdata == sdata &&
115abe60632SJohannes Berg 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
11643ba7e95SJohannes Berg 			break;
117d0709a65SJohannes Berg 		sta = rcu_dereference(sta->hnext);
11843ba7e95SJohannes Berg 	}
11943ba7e95SJohannes Berg 	return sta;
12043ba7e95SJohannes Berg }
12143ba7e95SJohannes Berg 
1223b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1233b53fde8SJohannes Berg 				     int idx)
124ee385855SLuis Carlos Cobo {
1253b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
126ee385855SLuis Carlos Cobo 	struct sta_info *sta;
127ee385855SLuis Carlos Cobo 	int i = 0;
128ee385855SLuis Carlos Cobo 
129d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1303b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1312a8ca29aSLuis Carlos Cobo 			continue;
132ee385855SLuis Carlos Cobo 		if (i < idx) {
133ee385855SLuis Carlos Cobo 			++i;
134ee385855SLuis Carlos Cobo 			continue;
135ee385855SLuis Carlos Cobo 		}
1362a8ca29aSLuis Carlos Cobo 		return sta;
137ee385855SLuis Carlos Cobo 	}
138ee385855SLuis Carlos Cobo 
139ee385855SLuis Carlos Cobo 	return NULL;
140ee385855SLuis Carlos Cobo }
141f0706e82SJiri Benc 
14293e5deb1SJohannes Berg /**
14393e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
14493e5deb1SJohannes Berg  *
1456ef307bcSRandy Dunlap  * @local: pointer to the global information
14693e5deb1SJohannes Berg  * @sta: STA info to free
14793e5deb1SJohannes Berg  *
14893e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
14993e5deb1SJohannes Berg  * that may happen before sta_info_insert().
15093e5deb1SJohannes Berg  */
15193e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
15293e5deb1SJohannes Berg 			    struct sta_info *sta)
15393e5deb1SJohannes Berg {
154af65cd96SJohannes Berg 	if (sta->rate_ctrl) {
1554b7679a5SJohannes Berg 		rate_control_free_sta(sta);
15693e5deb1SJohannes Berg 		rate_control_put(sta->rate_ctrl);
157af65cd96SJohannes Berg 	}
15893e5deb1SJohannes Berg 
15993e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1600c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
1610c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
16293e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
16393e5deb1SJohannes Berg 
16493e5deb1SJohannes Berg 	kfree(sta);
16593e5deb1SJohannes Berg }
16693e5deb1SJohannes Berg 
167d0709a65SJohannes Berg void sta_info_destroy(struct sta_info *sta)
168f0706e82SJiri Benc {
16997bff8ecSJohannes Berg 	struct ieee80211_local *local;
170f0706e82SJiri Benc 	struct sk_buff *skb;
17107db2183SRon Rindjunsky 	int i;
17273651ee6SJohannes Berg 
17397bff8ecSJohannes Berg 	might_sleep();
17497bff8ecSJohannes Berg 
17573651ee6SJohannes Berg 	if (!sta)
17673651ee6SJohannes Berg 		return;
177f0706e82SJiri Benc 
17897bff8ecSJohannes Berg 	local = sta->local;
179d0709a65SJohannes Berg 
180af818581SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
181af818581SJohannes Berg 
182d0709a65SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
183d0709a65SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
184d0709a65SJohannes Berg 
185d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
186d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
187d0709a65SJohannes Berg 		mesh_plink_deactivate(sta);
188d0709a65SJohannes Berg #endif
189d0709a65SJohannes Berg 
190d0709a65SJohannes Berg 	/*
1913b96766fSJohannes Berg 	 * We have only unlinked the key, and actually destroying it
1923b96766fSJohannes Berg 	 * may mean it is removed from hardware which requires that
1933b96766fSJohannes Berg 	 * the key->sta pointer is still valid, so flush the key todo
1943b96766fSJohannes Berg 	 * list here.
1953b96766fSJohannes Berg 	 *
1963b96766fSJohannes Berg 	 * ieee80211_key_todo() will synchronize_rcu() so after this
1973b96766fSJohannes Berg 	 * nothing can reference this sta struct any more.
198d0709a65SJohannes Berg 	 */
1993b96766fSJohannes Berg 	ieee80211_key_todo();
200d0709a65SJohannes Berg 
201d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
202d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
203d0709a65SJohannes Berg 		del_timer_sync(&sta->plink_timer);
204d0709a65SJohannes Berg #endif
205d0709a65SJohannes Berg 
206f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
207f0706e82SJiri Benc 		local->total_ps_buffered--;
208f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
209f0706e82SJiri Benc 	}
210d0709a65SJohannes Berg 
211d0709a65SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
212f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
213d0709a65SJohannes Berg 
214fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
21555687e38SJohannes Berg 		struct tid_ampdu_rx *tid_rx;
21655687e38SJohannes Berg 		struct tid_ampdu_tx *tid_tx;
21755687e38SJohannes Berg 
21807346f81SJohannes Berg 		spin_lock_bh(&sta->lock);
21955687e38SJohannes Berg 		tid_rx = sta->ampdu_mlme.tid_rx[i];
22055687e38SJohannes Berg 		/* Make sure timer won't free the tid_rx struct, see below */
22155687e38SJohannes Berg 		if (tid_rx)
22255687e38SJohannes Berg 			tid_rx->shutdown = true;
22396f5e66eSJohannes Berg 
22407346f81SJohannes Berg 		spin_unlock_bh(&sta->lock);
22555687e38SJohannes Berg 
22655687e38SJohannes Berg 		/*
22755687e38SJohannes Berg 		 * Outside spinlock - shutdown is true now so that the timer
22855687e38SJohannes Berg 		 * won't free tid_rx, we have to do that now. Can't let the
22955687e38SJohannes Berg 		 * timer do it because we have to sync the timer outside the
23055687e38SJohannes Berg 		 * lock that it takes itself.
23155687e38SJohannes Berg 		 */
23255687e38SJohannes Berg 		if (tid_rx) {
23355687e38SJohannes Berg 			del_timer_sync(&tid_rx->session_timer);
23455687e38SJohannes Berg 			kfree(tid_rx);
23555687e38SJohannes Berg 		}
23655687e38SJohannes Berg 
23755687e38SJohannes Berg 		/*
23855687e38SJohannes Berg 		 * No need to do such complications for TX agg sessions, the
23955687e38SJohannes Berg 		 * path leading to freeing the tid_tx struct goes via a call
24055687e38SJohannes Berg 		 * from the driver, and thus needs to look up the sta struct
24155687e38SJohannes Berg 		 * again, which cannot be found when we get here. Hence, we
24255687e38SJohannes Berg 		 * just need to delete the timer and free the aggregation
24355687e38SJohannes Berg 		 * info; we won't be telling the peer about it then but that
24455687e38SJohannes Berg 		 * doesn't matter if we're not talking to it again anyway.
24555687e38SJohannes Berg 		 */
24655687e38SJohannes Berg 		tid_tx = sta->ampdu_mlme.tid_tx[i];
24755687e38SJohannes Berg 		if (tid_tx) {
24855687e38SJohannes Berg 			del_timer_sync(&tid_tx->addba_resp_timer);
249cd8ffc80SJohannes Berg 			/*
250cd8ffc80SJohannes Berg 			 * STA removed while aggregation session being
251cd8ffc80SJohannes Berg 			 * started? Bit odd, but purge frames anyway.
252cd8ffc80SJohannes Berg 			 */
253cd8ffc80SJohannes Berg 			skb_queue_purge(&tid_tx->pending);
25455687e38SJohannes Berg 			kfree(tid_tx);
25555687e38SJohannes Berg 		}
256fe3bf0f5SRon Rindjunsky 	}
257cee24a3eSRon Rindjunsky 
25893e5deb1SJohannes Berg 	__sta_info_free(local, sta);
259f0706e82SJiri Benc }
260f0706e82SJiri Benc 
261f0706e82SJiri Benc 
262d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
263d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
264d0709a65SJohannes Berg 			      struct sta_info *sta)
265f0706e82SJiri Benc {
26617741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
26717741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
268f0706e82SJiri Benc }
269f0706e82SJiri Benc 
270af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
271af818581SJohannes Berg {
272af818581SJohannes Berg 	struct sta_info *sta;
273af818581SJohannes Berg 
274af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
275af818581SJohannes Berg 
276af818581SJohannes Berg 	if (sta->dead)
277af818581SJohannes Berg 		return;
278af818581SJohannes Berg 
279af818581SJohannes Berg 	if (!test_sta_flags(sta, WLAN_STA_PS_STA))
280af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
281af818581SJohannes Berg 	else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
282af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
283af818581SJohannes Berg }
284af818581SJohannes Berg 
285af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
286af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
287af65cd96SJohannes Berg {
288af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
289af65cd96SJohannes Berg 		return 0;
290af65cd96SJohannes Berg 
291af65cd96SJohannes Berg 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
292af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
293af65cd96SJohannes Berg 						     &sta->sta, gfp);
294af65cd96SJohannes Berg 	if (!sta->rate_ctrl_priv) {
295af65cd96SJohannes Berg 		rate_control_put(sta->rate_ctrl);
296af65cd96SJohannes Berg 		return -ENOMEM;
297af65cd96SJohannes Berg 	}
298af65cd96SJohannes Berg 
299af65cd96SJohannes Berg 	return 0;
300af65cd96SJohannes Berg }
301af65cd96SJohannes Berg 
30273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30373651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
304f0706e82SJiri Benc {
305d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
306f0706e82SJiri Benc 	struct sta_info *sta;
30716c5f15cSRon Rindjunsky 	int i;
308f0706e82SJiri Benc 
30917741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
310f0706e82SJiri Benc 	if (!sta)
31173651ee6SJohannes Berg 		return NULL;
312f0706e82SJiri Benc 
31307346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3145a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
315af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
31607346f81SJohannes Berg 
31717741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
318d0709a65SJohannes Berg 	sta->local = local;
319d0709a65SJohannes Berg 	sta->sdata = sdata;
320f0706e82SJiri Benc 
321af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
322f0706e82SJiri Benc 		kfree(sta);
32373651ee6SJohannes Berg 		return NULL;
324f0706e82SJiri Benc 	}
325f0706e82SJiri Benc 
32616c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
32716c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
32816c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
32916c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
33016c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
331cee24a3eSRon Rindjunsky 		/* rx */
332cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
333cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
334cee24a3eSRon Rindjunsky 		/* tx */
335cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
336cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
337cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
33816c5f15cSRon Rindjunsky 	}
339f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
340f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
34173651ee6SJohannes Berg 
342cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
343cccaec98SSenthil Balasubramanian 		sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
344cccaec98SSenthil Balasubramanian 
34573651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3460c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %pM\n",
3470c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
34873651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
34973651ee6SJohannes Berg 
35003e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
351b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
35203e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
35303e4497eSJohannes Berg #endif
35403e4497eSJohannes Berg 
35573651ee6SJohannes Berg 	return sta;
35673651ee6SJohannes Berg }
35773651ee6SJohannes Berg 
35873651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta)
35973651ee6SJohannes Berg {
36073651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
36173651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
36273651ee6SJohannes Berg 	unsigned long flags;
36393e5deb1SJohannes Berg 	int err = 0;
36473651ee6SJohannes Berg 
36503e4497eSJohannes Berg 	/*
36603e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
36703e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
36803e4497eSJohannes Berg 	 * and another CPU turns off the net device.
36903e4497eSJohannes Berg 	 */
370*9607e6b6SJohannes Berg 	if (unlikely(!ieee80211_sdata_running(sdata))) {
37193e5deb1SJohannes Berg 		err = -ENETDOWN;
37293e5deb1SJohannes Berg 		goto out_free;
37393e5deb1SJohannes Berg 	}
37403e4497eSJohannes Berg 
37547846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
37617741cdcSJohannes Berg 		    is_multicast_ether_addr(sta->sta.addr))) {
37793e5deb1SJohannes Berg 		err = -EINVAL;
37893e5deb1SJohannes Berg 		goto out_free;
37993e5deb1SJohannes Berg 	}
38044213b5eSJohannes Berg 
381d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
38243ba7e95SJohannes Berg 	/* check if STA exists already */
383abe60632SJohannes Berg 	if (sta_info_get(sdata, sta->sta.addr)) {
384d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
38593e5deb1SJohannes Berg 		err = -EEXIST;
38693e5deb1SJohannes Berg 		goto out_free;
38743ba7e95SJohannes Berg 	}
388f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
389f5ea9120SJohannes Berg 	local->sta_generation++;
390f0706e82SJiri Benc 	local->num_sta++;
391f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
39232bfd35dSJohannes Berg 
393d0709a65SJohannes Berg 	/* notify driver */
394d0709a65SJohannes Berg 	if (local->ops->sta_notify) {
39505c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3963e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
3973e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
3983e122be0SJohannes Berg 					     u.ap);
39932bfd35dSJohannes Berg 
40012375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_ADD, &sta->sta);
401fbc44bf7SJohannes Berg 		sdata = sta->sdata;
40232bfd35dSJohannes Berg 	}
403d0709a65SJohannes Berg 
404f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4050c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %pM\n",
4060c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
407f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
408f0706e82SJiri Benc 
40973651ee6SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
41073651ee6SJohannes Berg 
411e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
41293e5deb1SJohannes Berg 	/*
41393e5deb1SJohannes Berg 	 * Debugfs entry adding might sleep, so schedule process
414e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
41593e5deb1SJohannes Berg 	 * have one.
41693e5deb1SJohannes Berg 	 * NOTE: due to auto-freeing semantics this may only be done
41793e5deb1SJohannes Berg 	 *       if the insertion is successful!
41893e5deb1SJohannes Berg 	 */
41949ec6fa2SJohannes Berg 	schedule_work(&local->sta_debugfs_add);
420e9f207f0SJiri Benc #endif
421e9f207f0SJiri Benc 
42273651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
42373651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
42473651ee6SJohannes Berg 
42573651ee6SJohannes Berg 	return 0;
42693e5deb1SJohannes Berg  out_free:
42793e5deb1SJohannes Berg 	BUG_ON(!err);
42893e5deb1SJohannes Berg 	__sta_info_free(local, sta);
42993e5deb1SJohannes Berg 	return err;
430f0706e82SJiri Benc }
431f0706e82SJiri Benc 
432004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
433004c872eSJohannes Berg {
434004c872eSJohannes Berg 	/*
435004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
436004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
437004c872eSJohannes Berg 	 */
438004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
439004c872eSJohannes Berg }
440004c872eSJohannes Berg 
441004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
442004c872eSJohannes Berg {
443004c872eSJohannes Berg 	/*
444004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
445004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
446004c872eSJohannes Berg 	 */
447004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
448004c872eSJohannes Berg }
449004c872eSJohannes Berg 
450004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
451004c872eSJohannes Berg 				   struct sta_info *sta)
452004c872eSJohannes Berg {
4533e122be0SJohannes Berg 	BUG_ON(!bss);
4543e122be0SJohannes Berg 
45517741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
4563e122be0SJohannes Berg 
457d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
458d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
45924487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, true);
460d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
461d0709a65SJohannes Berg 	}
462004c872eSJohannes Berg }
463004c872eSJohannes Berg 
464004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
465004c872eSJohannes Berg {
466d0709a65SJohannes Berg 	unsigned long flags;
467004c872eSJohannes Berg 
4683e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4693e122be0SJohannes Berg 
470d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
471d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
472d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
473004c872eSJohannes Berg }
474004c872eSJohannes Berg 
475004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
476004c872eSJohannes Berg 				     struct sta_info *sta)
477004c872eSJohannes Berg {
4783e122be0SJohannes Berg 	BUG_ON(!bss);
4793e122be0SJohannes Berg 
48017741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
4813e122be0SJohannes Berg 
482d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
483d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
48424487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, false);
485d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
486d0709a65SJohannes Berg 	}
487004c872eSJohannes Berg }
488004c872eSJohannes Berg 
489004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
490004c872eSJohannes Berg {
491d0709a65SJohannes Berg 	unsigned long flags;
492004c872eSJohannes Berg 
4933e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4943e122be0SJohannes Berg 
495d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
496d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
497d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
498004c872eSJohannes Berg }
499004c872eSJohannes Berg 
50024723d1bSJohannes Berg static void __sta_info_unlink(struct sta_info **sta)
501d0709a65SJohannes Berg {
502d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
503d0709a65SJohannes Berg 	struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
504d0709a65SJohannes Berg 	/*
505d0709a65SJohannes Berg 	 * pull caller's reference if we're already gone.
506d0709a65SJohannes Berg 	 */
507d0709a65SJohannes Berg 	if (sta_info_hash_del(local, *sta)) {
508d0709a65SJohannes Berg 		*sta = NULL;
509be8755e1SMichael Wu 		return;
510d0709a65SJohannes Berg 	}
511be8755e1SMichael Wu 
5123b96766fSJohannes Berg 	if ((*sta)->key) {
5133b96766fSJohannes Berg 		ieee80211_key_free((*sta)->key);
5143b96766fSJohannes Berg 		WARN_ON((*sta)->key);
5153b96766fSJohannes Berg 	}
5163b96766fSJohannes Berg 
5177d1559f1SJohannes Berg 	list_del(&(*sta)->list);
518af818581SJohannes Berg 	(*sta)->dead = true;
5197d1559f1SJohannes Berg 
520af818581SJohannes Berg 	if (test_and_clear_sta_flags(*sta,
521af818581SJohannes Berg 				WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
5223e122be0SJohannes Berg 		BUG_ON(!sdata->bss);
5233e122be0SJohannes Berg 
5247d1559f1SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
5257d1559f1SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, *sta);
5267d1559f1SJohannes Berg 	}
5277d1559f1SJohannes Berg 
5287d1559f1SJohannes Berg 	local->num_sta--;
529f5ea9120SJohannes Berg 	local->sta_generation++;
5307d1559f1SJohannes Berg 
531f14543eeSFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
532f14543eeSFelix Fietkau 		rcu_assign_pointer(sdata->u.vlan.sta, NULL);
533f14543eeSFelix Fietkau 
5347d1559f1SJohannes Berg 	if (local->ops->sta_notify) {
53505c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5363e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
5373e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
5383e122be0SJohannes Berg 					     u.ap);
5397d1559f1SJohannes Berg 
54012375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_REMOVE,
54124487981SJohannes Berg 			       &(*sta)->sta);
542fbc44bf7SJohannes Berg 		sdata = (*sta)->sdata;
5437d1559f1SJohannes Berg 	}
5447d1559f1SJohannes Berg 
5457d1559f1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
5467d1559f1SJohannes Berg 		mesh_accept_plinks_update(sdata);
5477d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
5487d1559f1SJohannes Berg 		del_timer(&(*sta)->plink_timer);
5497d1559f1SJohannes Berg #endif
5507d1559f1SJohannes Berg 	}
5517d1559f1SJohannes Berg 
5527d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
5530c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %pM\n",
5540c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
5557d1559f1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
5567d1559f1SJohannes Berg 
557d0709a65SJohannes Berg 	/*
5587d1559f1SJohannes Berg 	 * Finally, pull caller's reference if the STA is pinned by the
559d0709a65SJohannes Berg 	 * task that is adding the debugfs entries. In that case, we
560d0709a65SJohannes Berg 	 * leave the STA "to be freed".
561d0709a65SJohannes Berg 	 *
562d0709a65SJohannes Berg 	 * The rules are not trivial, but not too complex either:
563d0709a65SJohannes Berg 	 *  (1) pin_status is only modified under the sta_lock
56449ec6fa2SJohannes Berg 	 *  (2) STAs may only be pinned under the RTNL so that
56549ec6fa2SJohannes Berg 	 *	sta_info_flush() is guaranteed to actually destroy
56649ec6fa2SJohannes Berg 	 *	all STAs that are active for a given interface, this
56749ec6fa2SJohannes Berg 	 *	is required for correctness because otherwise we
56849ec6fa2SJohannes Berg 	 *	could notify a driver that an interface is going
56949ec6fa2SJohannes Berg 	 *	away and only after that (!) notify it about a STA
57049ec6fa2SJohannes Berg 	 *	on that interface going away.
57149ec6fa2SJohannes Berg 	 *  (3) sta_info_debugfs_add_work() will set the status
572d0709a65SJohannes Berg 	 *	to PINNED when it found an item that needs a new
573d0709a65SJohannes Berg 	 *	debugfs directory created. In that case, that item
574d0709a65SJohannes Berg 	 *	must not be freed although all *RCU* users are done
575d0709a65SJohannes Berg 	 *	with it. Hence, we tell the caller of _unlink()
576d0709a65SJohannes Berg 	 *	that the item is already gone (as can happen when
577d0709a65SJohannes Berg 	 *	two tasks try to unlink/destroy at the same time)
57849ec6fa2SJohannes Berg 	 *  (4) We set the pin_status to DESTROY here when we
579d0709a65SJohannes Berg 	 *	find such an item.
58049ec6fa2SJohannes Berg 	 *  (5) sta_info_debugfs_add_work() will reset the pin_status
581d0709a65SJohannes Berg 	 *	from PINNED to NORMAL when it is done with the item,
582d0709a65SJohannes Berg 	 *	but will check for DESTROY before resetting it in
583d0709a65SJohannes Berg 	 *	which case it will free the item.
584d0709a65SJohannes Berg 	 */
585d0709a65SJohannes Berg 	if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
586d0709a65SJohannes Berg 		(*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
587d0709a65SJohannes Berg 		*sta = NULL;
588d0709a65SJohannes Berg 		return;
589d0709a65SJohannes Berg 	}
590d0709a65SJohannes Berg }
591d0709a65SJohannes Berg 
592d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta)
593d0709a65SJohannes Berg {
594d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
595d0709a65SJohannes Berg 	unsigned long flags;
596d0709a65SJohannes Berg 
597d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
598d0709a65SJohannes Berg 	__sta_info_unlink(sta);
599d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
600d0709a65SJohannes Berg }
601f0706e82SJiri Benc 
60257c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta,
603f0706e82SJiri Benc 				   struct sk_buff *skb)
604f0706e82SJiri Benc {
605e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
606f0706e82SJiri Benc 	int timeout;
607f0706e82SJiri Benc 
608f0706e82SJiri Benc 	if (!skb)
609f0706e82SJiri Benc 		return 0;
610f0706e82SJiri Benc 
611e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
612f0706e82SJiri Benc 
613f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
61457c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
61557c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
61657c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
617f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
618f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
619e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
620f0706e82SJiri Benc }
621f0706e82SJiri Benc 
622f0706e82SJiri Benc 
623f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
624f0706e82SJiri Benc 					     struct sta_info *sta)
625f0706e82SJiri Benc {
626f0706e82SJiri Benc 	unsigned long flags;
627f0706e82SJiri Benc 	struct sk_buff *skb;
628836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
629f0706e82SJiri Benc 
630f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
631f0706e82SJiri Benc 		return;
632f0706e82SJiri Benc 
633f0706e82SJiri Benc 	for (;;) {
634f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
635f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
63657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
637f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
638836341a7SJohannes Berg 		else
639f0706e82SJiri Benc 			skb = NULL;
640f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
641f0706e82SJiri Benc 
642836341a7SJohannes Berg 		if (!skb)
643836341a7SJohannes Berg 			break;
644836341a7SJohannes Berg 
645d0709a65SJohannes Berg 		sdata = sta->sdata;
646f0706e82SJiri Benc 		local->total_ps_buffered--;
647f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
6480c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
6490c68ae26SJohannes Berg 		       sta->sta.addr);
650f4ea83ddSJohannes Berg #endif
651f0706e82SJiri Benc 		dev_kfree_skb(skb);
652836341a7SJohannes Berg 
653004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
654004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
655f0706e82SJiri Benc 	}
656f0706e82SJiri Benc }
657f0706e82SJiri Benc 
658f0706e82SJiri Benc 
659f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
660f0706e82SJiri Benc {
661f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
662f0706e82SJiri Benc 	struct sta_info *sta;
663f0706e82SJiri Benc 
664d0709a65SJohannes Berg 	rcu_read_lock();
665d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
666f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
667d0709a65SJohannes Berg 	rcu_read_unlock();
668f0706e82SJiri Benc 
6695bb644a0SJohannes Berg 	if (local->quiescing)
6705bb644a0SJohannes Berg 		return;
6715bb644a0SJohannes Berg 
6720d174406SJohannes Berg 	local->sta_cleanup.expires =
6730d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
674f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
675f0706e82SJiri Benc }
676f0706e82SJiri Benc 
677e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
6784d6141c3SJiri Slaby /*
6794d6141c3SJiri Slaby  * See comment in __sta_info_unlink,
6804d6141c3SJiri Slaby  * caller must hold local->sta_lock.
6814d6141c3SJiri Slaby  */
6824d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta)
6834d6141c3SJiri Slaby {
6844d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
6854d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_PINNED;
6864d6141c3SJiri Slaby }
6874d6141c3SJiri Slaby 
6884d6141c3SJiri Slaby /*
6894d6141c3SJiri Slaby  * See comment in __sta_info_unlink, returns sta if it
6904d6141c3SJiri Slaby  * needs to be destroyed.
6914d6141c3SJiri Slaby  */
6924d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta)
6934d6141c3SJiri Slaby {
6944d6141c3SJiri Slaby 	struct sta_info *ret = NULL;
6954d6141c3SJiri Slaby 	unsigned long flags;
6964d6141c3SJiri Slaby 
6974d6141c3SJiri Slaby 	spin_lock_irqsave(&sta->local->sta_lock, flags);
6984d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
6994d6141c3SJiri Slaby 		sta->pin_status != STA_INFO_PIN_STAT_PINNED);
7004d6141c3SJiri Slaby 	if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
7014d6141c3SJiri Slaby 		ret = sta;
7024d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
7034d6141c3SJiri Slaby 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
7044d6141c3SJiri Slaby 
7054d6141c3SJiri Slaby 	return ret;
7064d6141c3SJiri Slaby }
7074d6141c3SJiri Slaby 
708d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work)
709e9f207f0SJiri Benc {
710e9f207f0SJiri Benc 	struct ieee80211_local *local =
711e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
712e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
713d0709a65SJohannes Berg 	unsigned long flags;
714e9f207f0SJiri Benc 
71549ec6fa2SJohannes Berg 	/* We need to keep the RTNL across the whole pinned status. */
71649ec6fa2SJohannes Berg 	rtnl_lock();
717e9f207f0SJiri Benc 	while (1) {
718e9f207f0SJiri Benc 		sta = NULL;
719d0709a65SJohannes Berg 
720d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
721e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
72263044e9fSJohannes Berg 			/*
72363044e9fSJohannes Berg 			 * debugfs.add_has_run will be set by
72463044e9fSJohannes Berg 			 * ieee80211_sta_debugfs_add regardless
72563044e9fSJohannes Berg 			 * of what else it does.
72663044e9fSJohannes Berg 			 */
72763044e9fSJohannes Berg 			if (!tmp->debugfs.add_has_run) {
728e9f207f0SJiri Benc 				sta = tmp;
729d0709a65SJohannes Berg 				__sta_info_pin(sta);
730e9f207f0SJiri Benc 				break;
731e9f207f0SJiri Benc 			}
732e9f207f0SJiri Benc 		}
733d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
734e9f207f0SJiri Benc 
735e9f207f0SJiri Benc 		if (!sta)
736e9f207f0SJiri Benc 			break;
737e9f207f0SJiri Benc 
738e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
739e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
740d0709a65SJohannes Berg 
741d0709a65SJohannes Berg 		sta = __sta_info_unpin(sta);
742d0709a65SJohannes Berg 		sta_info_destroy(sta);
743e9f207f0SJiri Benc 	}
74449ec6fa2SJohannes Berg 	rtnl_unlock();
745e9f207f0SJiri Benc }
746e9f207f0SJiri Benc #endif
747e9f207f0SJiri Benc 
748f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
749f0706e82SJiri Benc {
750d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
751f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
752f0706e82SJiri Benc 
753b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
754b24b8a24SPavel Emelyanov 		    (unsigned long)local);
7550d174406SJohannes Berg 	local->sta_cleanup.expires =
7560d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
757e9f207f0SJiri Benc 
758e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
759d0709a65SJohannes Berg 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
760e9f207f0SJiri Benc #endif
761f0706e82SJiri Benc }
762f0706e82SJiri Benc 
763f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
764f0706e82SJiri Benc {
765f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
766f0706e82SJiri Benc 	return 0;
767f0706e82SJiri Benc }
768f0706e82SJiri Benc 
769f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
770f0706e82SJiri Benc {
771f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
77249ec6fa2SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS
77349ec6fa2SJohannes Berg 	/*
77449ec6fa2SJohannes Berg 	 * Make sure the debugfs adding work isn't pending after this
77549ec6fa2SJohannes Berg 	 * because we're about to be destroyed. It doesn't matter
77649ec6fa2SJohannes Berg 	 * whether it ran or not since we're going to flush all STAs
77749ec6fa2SJohannes Berg 	 * anyway.
77849ec6fa2SJohannes Berg 	 */
77949ec6fa2SJohannes Berg 	cancel_work_sync(&local->sta_debugfs_add);
78049ec6fa2SJohannes Berg #endif
781dc6676b7SJohannes Berg 
782be8755e1SMichael Wu 	sta_info_flush(local, NULL);
783f0706e82SJiri Benc }
784f0706e82SJiri Benc 
785f0706e82SJiri Benc /**
786f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
78744213b5eSJohannes Berg  *
78844213b5eSJohannes Berg  * Returns the number of removed STA entries.
78944213b5eSJohannes Berg  *
790f0706e82SJiri Benc  * @local: local interface data
791d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
792f0706e82SJiri Benc  */
79344213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
794d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
795f0706e82SJiri Benc {
796f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
797be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
79844213b5eSJohannes Berg 	int ret = 0;
799d0709a65SJohannes Berg 	unsigned long flags;
800f0706e82SJiri Benc 
801d0709a65SJohannes Berg 	might_sleep();
802d0709a65SJohannes Berg 
803d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
804d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
805d0709a65SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
806d0709a65SJohannes Berg 			__sta_info_unlink(&sta);
80744213b5eSJohannes Berg 			if (sta) {
808be8755e1SMichael Wu 				list_add_tail(&sta->list, &tmp_list);
80944213b5eSJohannes Berg 				ret++;
81044213b5eSJohannes Berg 			}
811be8755e1SMichael Wu 		}
812be8755e1SMichael Wu 	}
813d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
814d0709a65SJohannes Berg 
815d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
816d0709a65SJohannes Berg 		sta_info_destroy(sta);
81744213b5eSJohannes Berg 
81844213b5eSJohannes Berg 	return ret;
819f0706e82SJiri Benc }
820dc6676b7SJohannes Berg 
82124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
82224723d1bSJohannes Berg 			  unsigned long exp_time)
82324723d1bSJohannes Berg {
82424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
82524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
82624723d1bSJohannes Berg 	LIST_HEAD(tmp_list);
82724723d1bSJohannes Berg 	unsigned long flags;
82824723d1bSJohannes Berg 
82924723d1bSJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
83024723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
83124723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
83224723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
8330c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
83447846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
83524723d1bSJohannes Berg #endif
83624723d1bSJohannes Berg 			__sta_info_unlink(&sta);
83724723d1bSJohannes Berg 			if (sta)
83824723d1bSJohannes Berg 				list_add(&sta->list, &tmp_list);
83924723d1bSJohannes Berg 		}
84024723d1bSJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
84124723d1bSJohannes Berg 
84224723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
84324723d1bSJohannes Berg 		sta_info_destroy(sta);
84424723d1bSJohannes Berg }
84517741cdcSJohannes Berg 
8465ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
84717741cdcSJohannes Berg 					       const u8 *addr)
84817741cdcSJohannes Berg {
849abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
85017741cdcSJohannes Berg 
851abe60632SJohannes Berg 	/* Just return a random station ... first in list ... */
852abe60632SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt)
85317741cdcSJohannes Berg 		return &sta->sta;
854abe60632SJohannes Berg 	return NULL;
85517741cdcSJohannes Berg }
8565ed176e1SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
8575ed176e1SJohannes Berg 
8585ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
8595ed176e1SJohannes Berg 					 const u8 *addr)
8605ed176e1SJohannes Berg {
8615ed176e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8625ed176e1SJohannes Berg 
8635ed176e1SJohannes Berg 	if (!vif)
8645ed176e1SJohannes Berg 		return NULL;
8655ed176e1SJohannes Berg 
8665ed176e1SJohannes Berg 	sdata = vif_to_sdata(vif);
8675ed176e1SJohannes Berg 
8685ed176e1SJohannes Berg 	return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
8695ed176e1SJohannes Berg }
87017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
871af818581SJohannes Berg 
872af818581SJohannes Berg /* powersave support code */
873af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
874af818581SJohannes Berg {
875af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
876af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
877af818581SJohannes Berg 	int sent, buffered;
878af818581SJohannes Berg 
87912375ef9SJohannes Berg 	drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
880af818581SJohannes Berg 
881af818581SJohannes Berg 	if (!skb_queue_empty(&sta->ps_tx_buf))
882af818581SJohannes Berg 		sta_info_clear_tim_bit(sta);
883af818581SJohannes Berg 
884af818581SJohannes Berg 	/* Send all buffered frames to the station */
885af818581SJohannes Berg 	sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
886af818581SJohannes Berg 	buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
887af818581SJohannes Berg 	sent += buffered;
888af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
889af818581SJohannes Berg 
890af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
891af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
89247846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
893af818581SJohannes Berg 	       sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
894af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
895af818581SJohannes Berg }
896af818581SJohannes Berg 
897af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
898af818581SJohannes Berg {
899af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
900af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
901af818581SJohannes Berg 	struct sk_buff *skb;
902af818581SJohannes Berg 	int no_pending_pkts;
903af818581SJohannes Berg 
904af818581SJohannes Berg 	skb = skb_dequeue(&sta->tx_filtered);
905af818581SJohannes Berg 	if (!skb) {
906af818581SJohannes Berg 		skb = skb_dequeue(&sta->ps_tx_buf);
907af818581SJohannes Berg 		if (skb)
908af818581SJohannes Berg 			local->total_ps_buffered--;
909af818581SJohannes Berg 	}
910af818581SJohannes Berg 	no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
911af818581SJohannes Berg 		skb_queue_empty(&sta->ps_tx_buf);
912af818581SJohannes Berg 
913af818581SJohannes Berg 	if (skb) {
914af818581SJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
915af818581SJohannes Berg 		struct ieee80211_hdr *hdr =
916af818581SJohannes Berg 			(struct ieee80211_hdr *) skb->data;
917af818581SJohannes Berg 
918af818581SJohannes Berg 		/*
919af818581SJohannes Berg 		 * Tell TX path to send this frame even though the STA may
920af818581SJohannes Berg 		 * still remain is PS mode after this frame exchange.
921af818581SJohannes Berg 		 */
922af818581SJohannes Berg 		info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
923af818581SJohannes Berg 
924af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
925af818581SJohannes Berg 		printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
926af818581SJohannes Berg 		       sta->sta.addr, sta->sta.aid,
927af818581SJohannes Berg 		       skb_queue_len(&sta->ps_tx_buf));
928af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
929af818581SJohannes Berg 
930af818581SJohannes Berg 		/* Use MoreData flag to indicate whether there are more
931af818581SJohannes Berg 		 * buffered frames for this STA */
932af818581SJohannes Berg 		if (no_pending_pkts)
933af818581SJohannes Berg 			hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
934af818581SJohannes Berg 		else
935af818581SJohannes Berg 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
936af818581SJohannes Berg 
937af818581SJohannes Berg 		ieee80211_add_pending_skb(local, skb);
938af818581SJohannes Berg 
939af818581SJohannes Berg 		if (no_pending_pkts)
940af818581SJohannes Berg 			sta_info_clear_tim_bit(sta);
941af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
942af818581SJohannes Berg 	} else {
943af818581SJohannes Berg 		/*
944af818581SJohannes Berg 		 * FIXME: This can be the result of a race condition between
945af818581SJohannes Berg 		 *	  us expiring a frame and the station polling for it.
946af818581SJohannes Berg 		 *	  Should we send it a null-func frame indicating we
947af818581SJohannes Berg 		 *	  have nothing buffered for it?
948af818581SJohannes Berg 		 */
949af818581SJohannes Berg 		printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
950af818581SJohannes Berg 		       "though there are no buffered frames for it\n",
95147846c9bSJohannes Berg 		       sdata->name, sta->sta.addr);
952af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
953af818581SJohannes Berg 	}
954af818581SJohannes Berg }
955af818581SJohannes Berg 
956af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
957af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
958af818581SJohannes Berg {
959af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
960af818581SJohannes Berg 
961af818581SJohannes Berg 	if (block)
962af818581SJohannes Berg 		set_sta_flags(sta, WLAN_STA_PS_DRIVER);
963af818581SJohannes Berg 	else
964af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
965af818581SJohannes Berg }
966af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
967