xref: /openbmc/linux/net/mac80211/sta_info.c (revision af81858172cc0f3da81946aab919c26e4b364efc)
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 
119ee385855SLuis Carlos Cobo struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
120ee385855SLuis Carlos Cobo 				     struct net_device *dev)
121ee385855SLuis Carlos Cobo {
122ee385855SLuis Carlos Cobo 	struct sta_info *sta;
123ee385855SLuis Carlos Cobo 	int i = 0;
124ee385855SLuis Carlos Cobo 
125d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1262a8ca29aSLuis Carlos Cobo 		if (dev && dev != sta->sdata->dev)
1272a8ca29aSLuis Carlos Cobo 			continue;
128ee385855SLuis Carlos Cobo 		if (i < idx) {
129ee385855SLuis Carlos Cobo 			++i;
130ee385855SLuis Carlos Cobo 			continue;
131ee385855SLuis Carlos Cobo 		}
1322a8ca29aSLuis Carlos Cobo 		return sta;
133ee385855SLuis Carlos Cobo 	}
134ee385855SLuis Carlos Cobo 
135ee385855SLuis Carlos Cobo 	return NULL;
136ee385855SLuis Carlos Cobo }
137f0706e82SJiri Benc 
13893e5deb1SJohannes Berg /**
13993e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
14093e5deb1SJohannes Berg  *
1416ef307bcSRandy Dunlap  * @local: pointer to the global information
14293e5deb1SJohannes Berg  * @sta: STA info to free
14393e5deb1SJohannes Berg  *
14493e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
14593e5deb1SJohannes Berg  * that may happen before sta_info_insert().
14693e5deb1SJohannes Berg  */
14793e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
14893e5deb1SJohannes Berg 			    struct sta_info *sta)
14993e5deb1SJohannes Berg {
1504b7679a5SJohannes Berg 	rate_control_free_sta(sta);
15193e5deb1SJohannes Berg 	rate_control_put(sta->rate_ctrl);
15293e5deb1SJohannes Berg 
15393e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1540c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
1550c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
15693e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
15793e5deb1SJohannes Berg 
15893e5deb1SJohannes Berg 	kfree(sta);
15993e5deb1SJohannes Berg }
16093e5deb1SJohannes Berg 
161d0709a65SJohannes Berg void sta_info_destroy(struct sta_info *sta)
162f0706e82SJiri Benc {
16397bff8ecSJohannes Berg 	struct ieee80211_local *local;
164f0706e82SJiri Benc 	struct sk_buff *skb;
16507db2183SRon Rindjunsky 	int i;
16673651ee6SJohannes Berg 
16797bff8ecSJohannes Berg 	might_sleep();
16897bff8ecSJohannes Berg 
16973651ee6SJohannes Berg 	if (!sta)
17073651ee6SJohannes Berg 		return;
171f0706e82SJiri Benc 
17297bff8ecSJohannes Berg 	local = sta->local;
173d0709a65SJohannes Berg 
174*af818581SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
175*af818581SJohannes Berg 
176d0709a65SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
177d0709a65SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
178d0709a65SJohannes Berg 
179d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
180d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
181d0709a65SJohannes Berg 		mesh_plink_deactivate(sta);
182d0709a65SJohannes Berg #endif
183d0709a65SJohannes Berg 
184d0709a65SJohannes Berg 	/*
1853b96766fSJohannes Berg 	 * We have only unlinked the key, and actually destroying it
1863b96766fSJohannes Berg 	 * may mean it is removed from hardware which requires that
1873b96766fSJohannes Berg 	 * the key->sta pointer is still valid, so flush the key todo
1883b96766fSJohannes Berg 	 * list here.
1893b96766fSJohannes Berg 	 *
1903b96766fSJohannes Berg 	 * ieee80211_key_todo() will synchronize_rcu() so after this
1913b96766fSJohannes Berg 	 * nothing can reference this sta struct any more.
192d0709a65SJohannes Berg 	 */
1933b96766fSJohannes Berg 	ieee80211_key_todo();
194d0709a65SJohannes Berg 
195d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
196d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
197d0709a65SJohannes Berg 		del_timer_sync(&sta->plink_timer);
198d0709a65SJohannes Berg #endif
199d0709a65SJohannes Berg 
200f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
201f0706e82SJiri Benc 		local->total_ps_buffered--;
202f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
203f0706e82SJiri Benc 	}
204d0709a65SJohannes Berg 
205d0709a65SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
206f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
207d0709a65SJohannes Berg 
208fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
20955687e38SJohannes Berg 		struct tid_ampdu_rx *tid_rx;
21055687e38SJohannes Berg 		struct tid_ampdu_tx *tid_tx;
21155687e38SJohannes Berg 
21207346f81SJohannes Berg 		spin_lock_bh(&sta->lock);
21355687e38SJohannes Berg 		tid_rx = sta->ampdu_mlme.tid_rx[i];
21455687e38SJohannes Berg 		/* Make sure timer won't free the tid_rx struct, see below */
21555687e38SJohannes Berg 		if (tid_rx)
21655687e38SJohannes Berg 			tid_rx->shutdown = true;
21796f5e66eSJohannes Berg 
21807346f81SJohannes Berg 		spin_unlock_bh(&sta->lock);
21955687e38SJohannes Berg 
22055687e38SJohannes Berg 		/*
22155687e38SJohannes Berg 		 * Outside spinlock - shutdown is true now so that the timer
22255687e38SJohannes Berg 		 * won't free tid_rx, we have to do that now. Can't let the
22355687e38SJohannes Berg 		 * timer do it because we have to sync the timer outside the
22455687e38SJohannes Berg 		 * lock that it takes itself.
22555687e38SJohannes Berg 		 */
22655687e38SJohannes Berg 		if (tid_rx) {
22755687e38SJohannes Berg 			del_timer_sync(&tid_rx->session_timer);
22855687e38SJohannes Berg 			kfree(tid_rx);
22955687e38SJohannes Berg 		}
23055687e38SJohannes Berg 
23155687e38SJohannes Berg 		/*
23255687e38SJohannes Berg 		 * No need to do such complications for TX agg sessions, the
23355687e38SJohannes Berg 		 * path leading to freeing the tid_tx struct goes via a call
23455687e38SJohannes Berg 		 * from the driver, and thus needs to look up the sta struct
23555687e38SJohannes Berg 		 * again, which cannot be found when we get here. Hence, we
23655687e38SJohannes Berg 		 * just need to delete the timer and free the aggregation
23755687e38SJohannes Berg 		 * info; we won't be telling the peer about it then but that
23855687e38SJohannes Berg 		 * doesn't matter if we're not talking to it again anyway.
23955687e38SJohannes Berg 		 */
24055687e38SJohannes Berg 		tid_tx = sta->ampdu_mlme.tid_tx[i];
24155687e38SJohannes Berg 		if (tid_tx) {
24255687e38SJohannes Berg 			del_timer_sync(&tid_tx->addba_resp_timer);
243cd8ffc80SJohannes Berg 			/*
244cd8ffc80SJohannes Berg 			 * STA removed while aggregation session being
245cd8ffc80SJohannes Berg 			 * started? Bit odd, but purge frames anyway.
246cd8ffc80SJohannes Berg 			 */
247cd8ffc80SJohannes Berg 			skb_queue_purge(&tid_tx->pending);
24855687e38SJohannes Berg 			kfree(tid_tx);
24955687e38SJohannes Berg 		}
250fe3bf0f5SRon Rindjunsky 	}
251cee24a3eSRon Rindjunsky 
25293e5deb1SJohannes Berg 	__sta_info_free(local, sta);
253f0706e82SJiri Benc }
254f0706e82SJiri Benc 
255f0706e82SJiri Benc 
256d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
257d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
258d0709a65SJohannes Berg 			      struct sta_info *sta)
259f0706e82SJiri Benc {
26017741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
26117741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
262f0706e82SJiri Benc }
263f0706e82SJiri Benc 
264*af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
265*af818581SJohannes Berg {
266*af818581SJohannes Berg 	struct sta_info *sta;
267*af818581SJohannes Berg 
268*af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
269*af818581SJohannes Berg 
270*af818581SJohannes Berg 	if (sta->dead)
271*af818581SJohannes Berg 		return;
272*af818581SJohannes Berg 
273*af818581SJohannes Berg 	if (!test_sta_flags(sta, WLAN_STA_PS_STA))
274*af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
275*af818581SJohannes Berg 	else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
276*af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
277*af818581SJohannes Berg }
278*af818581SJohannes Berg 
27973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
28073651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
281f0706e82SJiri Benc {
282d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
283f0706e82SJiri Benc 	struct sta_info *sta;
28416c5f15cSRon Rindjunsky 	int i;
285f0706e82SJiri Benc 
28617741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
287f0706e82SJiri Benc 	if (!sta)
28873651ee6SJohannes Berg 		return NULL;
289f0706e82SJiri Benc 
29007346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2915a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
292*af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
29307346f81SJohannes Berg 
29417741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
295d0709a65SJohannes Berg 	sta->local = local;
296d0709a65SJohannes Berg 	sta->sdata = sdata;
297f0706e82SJiri Benc 
298f0706e82SJiri Benc 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
299d0709a65SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
3004b7679a5SJohannes Berg 						     &sta->sta, gfp);
301f0706e82SJiri Benc 	if (!sta->rate_ctrl_priv) {
302f0706e82SJiri Benc 		rate_control_put(sta->rate_ctrl);
303f0706e82SJiri Benc 		kfree(sta);
30473651ee6SJohannes Berg 		return NULL;
305f0706e82SJiri Benc 	}
306f0706e82SJiri Benc 
30716c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
30816c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
30916c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
31016c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
31116c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
312cee24a3eSRon Rindjunsky 		/* rx */
313cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
314cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
315cee24a3eSRon Rindjunsky 		/* tx */
316cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
317cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
318cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
31916c5f15cSRon Rindjunsky 	}
320f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
321f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
32273651ee6SJohannes Berg 
323cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
324cccaec98SSenthil Balasubramanian 		sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
325cccaec98SSenthil Balasubramanian 
32673651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3270c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %pM\n",
3280c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
32973651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
33073651ee6SJohannes Berg 
33103e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
332b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
33303e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
33403e4497eSJohannes Berg #endif
33503e4497eSJohannes Berg 
33673651ee6SJohannes Berg 	return sta;
33773651ee6SJohannes Berg }
33873651ee6SJohannes Berg 
33973651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta)
34073651ee6SJohannes Berg {
34173651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
34273651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
34373651ee6SJohannes Berg 	unsigned long flags;
34493e5deb1SJohannes Berg 	int err = 0;
34573651ee6SJohannes Berg 
34603e4497eSJohannes Berg 	/*
34703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
34803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
34903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
35003e4497eSJohannes Berg 	 */
35193e5deb1SJohannes Berg 	if (unlikely(!netif_running(sdata->dev))) {
35293e5deb1SJohannes Berg 		err = -ENETDOWN;
35393e5deb1SJohannes Berg 		goto out_free;
35493e5deb1SJohannes Berg 	}
35503e4497eSJohannes Berg 
35617741cdcSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
35717741cdcSJohannes Berg 		    is_multicast_ether_addr(sta->sta.addr))) {
35893e5deb1SJohannes Berg 		err = -EINVAL;
35993e5deb1SJohannes Berg 		goto out_free;
36093e5deb1SJohannes Berg 	}
36144213b5eSJohannes Berg 
362d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
36343ba7e95SJohannes Berg 	/* check if STA exists already */
3644b7679a5SJohannes Berg 	if (sta_info_get(local, sta->sta.addr)) {
365d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
36693e5deb1SJohannes Berg 		err = -EEXIST;
36793e5deb1SJohannes Berg 		goto out_free;
36843ba7e95SJohannes Berg 	}
369f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
370f5ea9120SJohannes Berg 	local->sta_generation++;
371f0706e82SJiri Benc 	local->num_sta++;
372f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
37332bfd35dSJohannes Berg 
374d0709a65SJohannes Berg 	/* notify driver */
375d0709a65SJohannes Berg 	if (local->ops->sta_notify) {
37605c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3773e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
3783e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
3793e122be0SJohannes Berg 					     u.ap);
38032bfd35dSJohannes Berg 
38124487981SJohannes Berg 		drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
382fbc44bf7SJohannes Berg 		sdata = sta->sdata;
38332bfd35dSJohannes Berg 	}
384d0709a65SJohannes Berg 
385f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3860c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %pM\n",
3870c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
388f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
389f0706e82SJiri Benc 
39073651ee6SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
39173651ee6SJohannes Berg 
392e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
39393e5deb1SJohannes Berg 	/*
39493e5deb1SJohannes Berg 	 * Debugfs entry adding might sleep, so schedule process
395e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
39693e5deb1SJohannes Berg 	 * have one.
39793e5deb1SJohannes Berg 	 * NOTE: due to auto-freeing semantics this may only be done
39893e5deb1SJohannes Berg 	 *       if the insertion is successful!
39993e5deb1SJohannes Berg 	 */
40049ec6fa2SJohannes Berg 	schedule_work(&local->sta_debugfs_add);
401e9f207f0SJiri Benc #endif
402e9f207f0SJiri Benc 
40373651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
40473651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
40573651ee6SJohannes Berg 
40673651ee6SJohannes Berg 	return 0;
40793e5deb1SJohannes Berg  out_free:
40893e5deb1SJohannes Berg 	BUG_ON(!err);
40993e5deb1SJohannes Berg 	__sta_info_free(local, sta);
41093e5deb1SJohannes Berg 	return err;
411f0706e82SJiri Benc }
412f0706e82SJiri Benc 
413004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
414004c872eSJohannes Berg {
415004c872eSJohannes Berg 	/*
416004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
417004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
418004c872eSJohannes Berg 	 */
419004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
420004c872eSJohannes Berg }
421004c872eSJohannes Berg 
422004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
423004c872eSJohannes Berg {
424004c872eSJohannes Berg 	/*
425004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
426004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
427004c872eSJohannes Berg 	 */
428004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
429004c872eSJohannes Berg }
430004c872eSJohannes Berg 
431004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
432004c872eSJohannes Berg 				   struct sta_info *sta)
433004c872eSJohannes Berg {
4343e122be0SJohannes Berg 	BUG_ON(!bss);
4353e122be0SJohannes Berg 
43617741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
4373e122be0SJohannes Berg 
438d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
439d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
44024487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, true);
441d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
442d0709a65SJohannes Berg 	}
443004c872eSJohannes Berg }
444004c872eSJohannes Berg 
445004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
446004c872eSJohannes Berg {
447d0709a65SJohannes Berg 	unsigned long flags;
448004c872eSJohannes Berg 
4493e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4503e122be0SJohannes Berg 
451d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
452d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
453d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
454004c872eSJohannes Berg }
455004c872eSJohannes Berg 
456004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
457004c872eSJohannes Berg 				     struct sta_info *sta)
458004c872eSJohannes Berg {
4593e122be0SJohannes Berg 	BUG_ON(!bss);
4603e122be0SJohannes Berg 
46117741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
4623e122be0SJohannes Berg 
463d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
464d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
46524487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, false);
466d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
467d0709a65SJohannes Berg 	}
468004c872eSJohannes Berg }
469004c872eSJohannes Berg 
470004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
471004c872eSJohannes Berg {
472d0709a65SJohannes Berg 	unsigned long flags;
473004c872eSJohannes Berg 
4743e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4753e122be0SJohannes Berg 
476d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
477d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
478d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
479004c872eSJohannes Berg }
480004c872eSJohannes Berg 
48124723d1bSJohannes Berg static void __sta_info_unlink(struct sta_info **sta)
482d0709a65SJohannes Berg {
483d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
484d0709a65SJohannes Berg 	struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
485d0709a65SJohannes Berg 	/*
486d0709a65SJohannes Berg 	 * pull caller's reference if we're already gone.
487d0709a65SJohannes Berg 	 */
488d0709a65SJohannes Berg 	if (sta_info_hash_del(local, *sta)) {
489d0709a65SJohannes Berg 		*sta = NULL;
490be8755e1SMichael Wu 		return;
491d0709a65SJohannes Berg 	}
492be8755e1SMichael Wu 
4933b96766fSJohannes Berg 	if ((*sta)->key) {
4943b96766fSJohannes Berg 		ieee80211_key_free((*sta)->key);
4953b96766fSJohannes Berg 		WARN_ON((*sta)->key);
4963b96766fSJohannes Berg 	}
4973b96766fSJohannes Berg 
4987d1559f1SJohannes Berg 	list_del(&(*sta)->list);
499*af818581SJohannes Berg 	(*sta)->dead = true;
5007d1559f1SJohannes Berg 
501*af818581SJohannes Berg 	if (test_and_clear_sta_flags(*sta,
502*af818581SJohannes Berg 				WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
5033e122be0SJohannes Berg 		BUG_ON(!sdata->bss);
5043e122be0SJohannes Berg 
5057d1559f1SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
5067d1559f1SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, *sta);
5077d1559f1SJohannes Berg 	}
5087d1559f1SJohannes Berg 
5097d1559f1SJohannes Berg 	local->num_sta--;
510f5ea9120SJohannes Berg 	local->sta_generation++;
5117d1559f1SJohannes Berg 
5127d1559f1SJohannes Berg 	if (local->ops->sta_notify) {
51305c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5143e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
5153e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
5163e122be0SJohannes Berg 					     u.ap);
5177d1559f1SJohannes Berg 
51824487981SJohannes Berg 		drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
51924487981SJohannes Berg 			       &(*sta)->sta);
520fbc44bf7SJohannes Berg 		sdata = (*sta)->sdata;
5217d1559f1SJohannes Berg 	}
5227d1559f1SJohannes Berg 
5237d1559f1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
5247d1559f1SJohannes Berg 		mesh_accept_plinks_update(sdata);
5257d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
5267d1559f1SJohannes Berg 		del_timer(&(*sta)->plink_timer);
5277d1559f1SJohannes Berg #endif
5287d1559f1SJohannes Berg 	}
5297d1559f1SJohannes Berg 
5307d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
5310c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %pM\n",
5320c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
5337d1559f1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
5347d1559f1SJohannes Berg 
535d0709a65SJohannes Berg 	/*
5367d1559f1SJohannes Berg 	 * Finally, pull caller's reference if the STA is pinned by the
537d0709a65SJohannes Berg 	 * task that is adding the debugfs entries. In that case, we
538d0709a65SJohannes Berg 	 * leave the STA "to be freed".
539d0709a65SJohannes Berg 	 *
540d0709a65SJohannes Berg 	 * The rules are not trivial, but not too complex either:
541d0709a65SJohannes Berg 	 *  (1) pin_status is only modified under the sta_lock
54249ec6fa2SJohannes Berg 	 *  (2) STAs may only be pinned under the RTNL so that
54349ec6fa2SJohannes Berg 	 *	sta_info_flush() is guaranteed to actually destroy
54449ec6fa2SJohannes Berg 	 *	all STAs that are active for a given interface, this
54549ec6fa2SJohannes Berg 	 *	is required for correctness because otherwise we
54649ec6fa2SJohannes Berg 	 *	could notify a driver that an interface is going
54749ec6fa2SJohannes Berg 	 *	away and only after that (!) notify it about a STA
54849ec6fa2SJohannes Berg 	 *	on that interface going away.
54949ec6fa2SJohannes Berg 	 *  (3) sta_info_debugfs_add_work() will set the status
550d0709a65SJohannes Berg 	 *	to PINNED when it found an item that needs a new
551d0709a65SJohannes Berg 	 *	debugfs directory created. In that case, that item
552d0709a65SJohannes Berg 	 *	must not be freed although all *RCU* users are done
553d0709a65SJohannes Berg 	 *	with it. Hence, we tell the caller of _unlink()
554d0709a65SJohannes Berg 	 *	that the item is already gone (as can happen when
555d0709a65SJohannes Berg 	 *	two tasks try to unlink/destroy at the same time)
55649ec6fa2SJohannes Berg 	 *  (4) We set the pin_status to DESTROY here when we
557d0709a65SJohannes Berg 	 *	find such an item.
55849ec6fa2SJohannes Berg 	 *  (5) sta_info_debugfs_add_work() will reset the pin_status
559d0709a65SJohannes Berg 	 *	from PINNED to NORMAL when it is done with the item,
560d0709a65SJohannes Berg 	 *	but will check for DESTROY before resetting it in
561d0709a65SJohannes Berg 	 *	which case it will free the item.
562d0709a65SJohannes Berg 	 */
563d0709a65SJohannes Berg 	if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
564d0709a65SJohannes Berg 		(*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
565d0709a65SJohannes Berg 		*sta = NULL;
566d0709a65SJohannes Berg 		return;
567d0709a65SJohannes Berg 	}
568d0709a65SJohannes Berg }
569d0709a65SJohannes Berg 
570d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta)
571d0709a65SJohannes Berg {
572d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
573d0709a65SJohannes Berg 	unsigned long flags;
574d0709a65SJohannes Berg 
575d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
576d0709a65SJohannes Berg 	__sta_info_unlink(sta);
577d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
578d0709a65SJohannes Berg }
579f0706e82SJiri Benc 
58057c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta,
581f0706e82SJiri Benc 				   struct sk_buff *skb)
582f0706e82SJiri Benc {
583e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
584f0706e82SJiri Benc 	int timeout;
585f0706e82SJiri Benc 
586f0706e82SJiri Benc 	if (!skb)
587f0706e82SJiri Benc 		return 0;
588f0706e82SJiri Benc 
589e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
590f0706e82SJiri Benc 
591f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
59257c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
59357c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
59457c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
595f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
596f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
597e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
598f0706e82SJiri Benc }
599f0706e82SJiri Benc 
600f0706e82SJiri Benc 
601f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
602f0706e82SJiri Benc 					     struct sta_info *sta)
603f0706e82SJiri Benc {
604f0706e82SJiri Benc 	unsigned long flags;
605f0706e82SJiri Benc 	struct sk_buff *skb;
606836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
607f0706e82SJiri Benc 
608f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
609f0706e82SJiri Benc 		return;
610f0706e82SJiri Benc 
611f0706e82SJiri Benc 	for (;;) {
612f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
613f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
61457c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
615f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
616836341a7SJohannes Berg 		else
617f0706e82SJiri Benc 			skb = NULL;
618f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
619f0706e82SJiri Benc 
620836341a7SJohannes Berg 		if (!skb)
621836341a7SJohannes Berg 			break;
622836341a7SJohannes Berg 
623d0709a65SJohannes Berg 		sdata = sta->sdata;
624f0706e82SJiri Benc 		local->total_ps_buffered--;
625f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
6260c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
6270c68ae26SJohannes Berg 		       sta->sta.addr);
628f4ea83ddSJohannes Berg #endif
629f0706e82SJiri Benc 		dev_kfree_skb(skb);
630836341a7SJohannes Berg 
631004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
632004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
633f0706e82SJiri Benc 	}
634f0706e82SJiri Benc }
635f0706e82SJiri Benc 
636f0706e82SJiri Benc 
637f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
638f0706e82SJiri Benc {
639f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
640f0706e82SJiri Benc 	struct sta_info *sta;
641f0706e82SJiri Benc 
642d0709a65SJohannes Berg 	rcu_read_lock();
643d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
644f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
645d0709a65SJohannes Berg 	rcu_read_unlock();
646f0706e82SJiri Benc 
6475bb644a0SJohannes Berg 	if (local->quiescing)
6485bb644a0SJohannes Berg 		return;
6495bb644a0SJohannes Berg 
6500d174406SJohannes Berg 	local->sta_cleanup.expires =
6510d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
652f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
653f0706e82SJiri Benc }
654f0706e82SJiri Benc 
655e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
6564d6141c3SJiri Slaby /*
6574d6141c3SJiri Slaby  * See comment in __sta_info_unlink,
6584d6141c3SJiri Slaby  * caller must hold local->sta_lock.
6594d6141c3SJiri Slaby  */
6604d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta)
6614d6141c3SJiri Slaby {
6624d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
6634d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_PINNED;
6644d6141c3SJiri Slaby }
6654d6141c3SJiri Slaby 
6664d6141c3SJiri Slaby /*
6674d6141c3SJiri Slaby  * See comment in __sta_info_unlink, returns sta if it
6684d6141c3SJiri Slaby  * needs to be destroyed.
6694d6141c3SJiri Slaby  */
6704d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta)
6714d6141c3SJiri Slaby {
6724d6141c3SJiri Slaby 	struct sta_info *ret = NULL;
6734d6141c3SJiri Slaby 	unsigned long flags;
6744d6141c3SJiri Slaby 
6754d6141c3SJiri Slaby 	spin_lock_irqsave(&sta->local->sta_lock, flags);
6764d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
6774d6141c3SJiri Slaby 		sta->pin_status != STA_INFO_PIN_STAT_PINNED);
6784d6141c3SJiri Slaby 	if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
6794d6141c3SJiri Slaby 		ret = sta;
6804d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
6814d6141c3SJiri Slaby 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
6824d6141c3SJiri Slaby 
6834d6141c3SJiri Slaby 	return ret;
6844d6141c3SJiri Slaby }
6854d6141c3SJiri Slaby 
686d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work)
687e9f207f0SJiri Benc {
688e9f207f0SJiri Benc 	struct ieee80211_local *local =
689e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
690e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
691d0709a65SJohannes Berg 	unsigned long flags;
692e9f207f0SJiri Benc 
69349ec6fa2SJohannes Berg 	/* We need to keep the RTNL across the whole pinned status. */
69449ec6fa2SJohannes Berg 	rtnl_lock();
695e9f207f0SJiri Benc 	while (1) {
696e9f207f0SJiri Benc 		sta = NULL;
697d0709a65SJohannes Berg 
698d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
699e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
70063044e9fSJohannes Berg 			/*
70163044e9fSJohannes Berg 			 * debugfs.add_has_run will be set by
70263044e9fSJohannes Berg 			 * ieee80211_sta_debugfs_add regardless
70363044e9fSJohannes Berg 			 * of what else it does.
70463044e9fSJohannes Berg 			 */
70563044e9fSJohannes Berg 			if (!tmp->debugfs.add_has_run) {
706e9f207f0SJiri Benc 				sta = tmp;
707d0709a65SJohannes Berg 				__sta_info_pin(sta);
708e9f207f0SJiri Benc 				break;
709e9f207f0SJiri Benc 			}
710e9f207f0SJiri Benc 		}
711d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
712e9f207f0SJiri Benc 
713e9f207f0SJiri Benc 		if (!sta)
714e9f207f0SJiri Benc 			break;
715e9f207f0SJiri Benc 
716e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
717e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
718d0709a65SJohannes Berg 
719d0709a65SJohannes Berg 		sta = __sta_info_unpin(sta);
720d0709a65SJohannes Berg 		sta_info_destroy(sta);
721e9f207f0SJiri Benc 	}
72249ec6fa2SJohannes Berg 	rtnl_unlock();
723e9f207f0SJiri Benc }
724e9f207f0SJiri Benc #endif
725e9f207f0SJiri Benc 
726f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
727f0706e82SJiri Benc {
728d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
729f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
730f0706e82SJiri Benc 
731b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
732b24b8a24SPavel Emelyanov 		    (unsigned long)local);
7330d174406SJohannes Berg 	local->sta_cleanup.expires =
7340d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
735e9f207f0SJiri Benc 
736e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
737d0709a65SJohannes Berg 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
738e9f207f0SJiri Benc #endif
739f0706e82SJiri Benc }
740f0706e82SJiri Benc 
741f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
742f0706e82SJiri Benc {
743f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
744f0706e82SJiri Benc 	return 0;
745f0706e82SJiri Benc }
746f0706e82SJiri Benc 
747f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
748f0706e82SJiri Benc {
749f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
75049ec6fa2SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS
75149ec6fa2SJohannes Berg 	/*
75249ec6fa2SJohannes Berg 	 * Make sure the debugfs adding work isn't pending after this
75349ec6fa2SJohannes Berg 	 * because we're about to be destroyed. It doesn't matter
75449ec6fa2SJohannes Berg 	 * whether it ran or not since we're going to flush all STAs
75549ec6fa2SJohannes Berg 	 * anyway.
75649ec6fa2SJohannes Berg 	 */
75749ec6fa2SJohannes Berg 	cancel_work_sync(&local->sta_debugfs_add);
75849ec6fa2SJohannes Berg #endif
759dc6676b7SJohannes Berg 
760be8755e1SMichael Wu 	sta_info_flush(local, NULL);
761f0706e82SJiri Benc }
762f0706e82SJiri Benc 
763f0706e82SJiri Benc /**
764f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
76544213b5eSJohannes Berg  *
76644213b5eSJohannes Berg  * Returns the number of removed STA entries.
76744213b5eSJohannes Berg  *
768f0706e82SJiri Benc  * @local: local interface data
769d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
770f0706e82SJiri Benc  */
77144213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
772d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
773f0706e82SJiri Benc {
774f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
775be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
77644213b5eSJohannes Berg 	int ret = 0;
777d0709a65SJohannes Berg 	unsigned long flags;
778f0706e82SJiri Benc 
779d0709a65SJohannes Berg 	might_sleep();
780d0709a65SJohannes Berg 
781d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
782d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
783d0709a65SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
784d0709a65SJohannes Berg 			__sta_info_unlink(&sta);
78544213b5eSJohannes Berg 			if (sta) {
786be8755e1SMichael Wu 				list_add_tail(&sta->list, &tmp_list);
78744213b5eSJohannes Berg 				ret++;
78844213b5eSJohannes Berg 			}
789be8755e1SMichael Wu 		}
790be8755e1SMichael Wu 	}
791d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
792d0709a65SJohannes Berg 
793d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
794d0709a65SJohannes Berg 		sta_info_destroy(sta);
79544213b5eSJohannes Berg 
79644213b5eSJohannes Berg 	return ret;
797f0706e82SJiri Benc }
798dc6676b7SJohannes Berg 
79924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
80024723d1bSJohannes Berg 			  unsigned long exp_time)
80124723d1bSJohannes Berg {
80224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
80324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
80424723d1bSJohannes Berg 	LIST_HEAD(tmp_list);
80524723d1bSJohannes Berg 	unsigned long flags;
80624723d1bSJohannes Berg 
80724723d1bSJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
80824723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
80924723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
81024723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
8110c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
8120c68ae26SJohannes Berg 			       sdata->dev->name, sta->sta.addr);
81324723d1bSJohannes Berg #endif
81424723d1bSJohannes Berg 			__sta_info_unlink(&sta);
81524723d1bSJohannes Berg 			if (sta)
81624723d1bSJohannes Berg 				list_add(&sta->list, &tmp_list);
81724723d1bSJohannes Berg 		}
81824723d1bSJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
81924723d1bSJohannes Berg 
82024723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
82124723d1bSJohannes Berg 		sta_info_destroy(sta);
82224723d1bSJohannes Berg }
82317741cdcSJohannes Berg 
8245ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
82517741cdcSJohannes Berg 					       const u8 *addr)
82617741cdcSJohannes Berg {
8274b7679a5SJohannes Berg 	struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
82817741cdcSJohannes Berg 
82917741cdcSJohannes Berg 	if (!sta)
83017741cdcSJohannes Berg 		return NULL;
83117741cdcSJohannes Berg 	return &sta->sta;
83217741cdcSJohannes Berg }
8335ed176e1SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
8345ed176e1SJohannes Berg 
8355ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
8365ed176e1SJohannes Berg 					 const u8 *addr)
8375ed176e1SJohannes Berg {
8385ed176e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8395ed176e1SJohannes Berg 
8405ed176e1SJohannes Berg 	if (!vif)
8415ed176e1SJohannes Berg 		return NULL;
8425ed176e1SJohannes Berg 
8435ed176e1SJohannes Berg 	sdata = vif_to_sdata(vif);
8445ed176e1SJohannes Berg 
8455ed176e1SJohannes Berg 	return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
8465ed176e1SJohannes Berg }
84717741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
848*af818581SJohannes Berg 
849*af818581SJohannes Berg /* powersave support code */
850*af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
851*af818581SJohannes Berg {
852*af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
853*af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
854*af818581SJohannes Berg 	int sent, buffered;
855*af818581SJohannes Berg 
856*af818581SJohannes Berg 	drv_sta_notify(local, &sdata->vif, STA_NOTIFY_AWAKE, &sta->sta);
857*af818581SJohannes Berg 
858*af818581SJohannes Berg 	if (!skb_queue_empty(&sta->ps_tx_buf))
859*af818581SJohannes Berg 		sta_info_clear_tim_bit(sta);
860*af818581SJohannes Berg 
861*af818581SJohannes Berg 	/* Send all buffered frames to the station */
862*af818581SJohannes Berg 	sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
863*af818581SJohannes Berg 	buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
864*af818581SJohannes Berg 	sent += buffered;
865*af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
866*af818581SJohannes Berg 
867*af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
868*af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
869*af818581SJohannes Berg 	       "since STA not sleeping anymore\n", sdata->dev->name,
870*af818581SJohannes Berg 	       sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
871*af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
872*af818581SJohannes Berg }
873*af818581SJohannes Berg 
874*af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
875*af818581SJohannes Berg {
876*af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
877*af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
878*af818581SJohannes Berg 	struct sk_buff *skb;
879*af818581SJohannes Berg 	int no_pending_pkts;
880*af818581SJohannes Berg 
881*af818581SJohannes Berg 	skb = skb_dequeue(&sta->tx_filtered);
882*af818581SJohannes Berg 	if (!skb) {
883*af818581SJohannes Berg 		skb = skb_dequeue(&sta->ps_tx_buf);
884*af818581SJohannes Berg 		if (skb)
885*af818581SJohannes Berg 			local->total_ps_buffered--;
886*af818581SJohannes Berg 	}
887*af818581SJohannes Berg 	no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
888*af818581SJohannes Berg 		skb_queue_empty(&sta->ps_tx_buf);
889*af818581SJohannes Berg 
890*af818581SJohannes Berg 	if (skb) {
891*af818581SJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
892*af818581SJohannes Berg 		struct ieee80211_hdr *hdr =
893*af818581SJohannes Berg 			(struct ieee80211_hdr *) skb->data;
894*af818581SJohannes Berg 
895*af818581SJohannes Berg 		/*
896*af818581SJohannes Berg 		 * Tell TX path to send this frame even though the STA may
897*af818581SJohannes Berg 		 * still remain is PS mode after this frame exchange.
898*af818581SJohannes Berg 		 */
899*af818581SJohannes Berg 		info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
900*af818581SJohannes Berg 
901*af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
902*af818581SJohannes Berg 		printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
903*af818581SJohannes Berg 		       sta->sta.addr, sta->sta.aid,
904*af818581SJohannes Berg 		       skb_queue_len(&sta->ps_tx_buf));
905*af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
906*af818581SJohannes Berg 
907*af818581SJohannes Berg 		/* Use MoreData flag to indicate whether there are more
908*af818581SJohannes Berg 		 * buffered frames for this STA */
909*af818581SJohannes Berg 		if (no_pending_pkts)
910*af818581SJohannes Berg 			hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
911*af818581SJohannes Berg 		else
912*af818581SJohannes Berg 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
913*af818581SJohannes Berg 
914*af818581SJohannes Berg 		ieee80211_add_pending_skb(local, skb);
915*af818581SJohannes Berg 
916*af818581SJohannes Berg 		if (no_pending_pkts)
917*af818581SJohannes Berg 			sta_info_clear_tim_bit(sta);
918*af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
919*af818581SJohannes Berg 	} else {
920*af818581SJohannes Berg 		/*
921*af818581SJohannes Berg 		 * FIXME: This can be the result of a race condition between
922*af818581SJohannes Berg 		 *	  us expiring a frame and the station polling for it.
923*af818581SJohannes Berg 		 *	  Should we send it a null-func frame indicating we
924*af818581SJohannes Berg 		 *	  have nothing buffered for it?
925*af818581SJohannes Berg 		 */
926*af818581SJohannes Berg 		printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
927*af818581SJohannes Berg 		       "though there are no buffered frames for it\n",
928*af818581SJohannes Berg 		       sdata->dev->name, sta->sta.addr);
929*af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
930*af818581SJohannes Berg 	}
931*af818581SJohannes Berg }
932*af818581SJohannes Berg 
933*af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
934*af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
935*af818581SJohannes Berg {
936*af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
937*af818581SJohannes Berg 
938*af818581SJohannes Berg 	if (block)
939*af818581SJohannes Berg 		set_sta_flags(sta, WLAN_STA_PS_DRIVER);
940*af818581SJohannes Berg 	else
941*af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
942*af818581SJohannes Berg }
943*af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
944