xref: /openbmc/linux/net/mac80211/sta_info.c (revision 55687e380a3965ac448e03281e027553a6ae6dac)
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"
222c8dccc7SJohannes Berg #include "rate.h"
23f0706e82SJiri Benc #include "sta_info.h"
24e9f207f0SJiri Benc #include "debugfs_sta.h"
25ee385855SLuis Carlos Cobo #include "mesh.h"
26f0706e82SJiri Benc 
27d0709a65SJohannes Berg /**
28d0709a65SJohannes Berg  * DOC: STA information lifetime rules
29d0709a65SJohannes Berg  *
30d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
31d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
32d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
33d0709a65SJohannes Berg  *
3403e4497eSJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
3503e4497eSJohannes Berg  * that structure. It must then either destroy it using sta_info_destroy()
3603e4497eSJohannes Berg  * (which is pretty useless) or insert it into the hash table using
3703e4497eSJohannes Berg  * sta_info_insert() which demotes the reference from ownership to a regular
3803e4497eSJohannes Berg  * RCU-protected reference; if the function is called without protection by an
3993e5deb1SJohannes Berg  * RCU critical section the reference is instantly invalidated. Note that the
4093e5deb1SJohannes Berg  * caller may not do much with the STA info before inserting it, in particular,
4193e5deb1SJohannes Berg  * it may not start any mesh peer link management or add encryption keys.
4293e5deb1SJohannes Berg  *
4393e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4493e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
45d0709a65SJohannes Berg  *
46d0709a65SJohannes Berg  * Because there are debugfs entries for each station, and adding those
47d0709a65SJohannes Berg  * must be able to sleep, it is also possible to "pin" a station entry,
48d0709a65SJohannes Berg  * that means it can be removed from the hash table but not be freed.
4993e5deb1SJohannes Berg  * See the comment in __sta_info_unlink() for more information, this is
5093e5deb1SJohannes Berg  * an internal capability only.
51d0709a65SJohannes Berg  *
52d0709a65SJohannes Berg  * In order to remove a STA info structure, the caller needs to first
53dbbea671SJohannes Berg  * unlink it (sta_info_unlink()) from the list and hash tables and
543b96766fSJohannes Berg  * then destroy it; sta_info_destroy() will wait for an RCU grace period
553b96766fSJohannes Berg  * to elapse before actually freeing it. Due to the pinning and the
563b96766fSJohannes Berg  * possibility of multiple callers trying to remove the same STA info at
573b96766fSJohannes Berg  * the same time, sta_info_unlink() can clear the STA info pointer it is
583b96766fSJohannes Berg  * passed to indicate that the STA info is owned by somebody else now.
59d0709a65SJohannes Berg  *
60dbbea671SJohannes Berg  * If sta_info_unlink() did not clear the pointer then the caller owns
61d0709a65SJohannes Berg  * the STA info structure now and is responsible of destroying it with
623b96766fSJohannes Berg  * a call to sta_info_destroy().
63d0709a65SJohannes Berg  *
64d0709a65SJohannes Berg  * In all other cases, there is no concept of ownership on a STA entry,
65d0709a65SJohannes Berg  * each structure is owned by the global hash table/list until it is
66d0709a65SJohannes Berg  * removed. All users of the structure need to be RCU protected so that
67d0709a65SJohannes Berg  * the structure won't be freed before they are done using it.
68d0709a65SJohannes Berg  */
69f0706e82SJiri Benc 
70f0706e82SJiri Benc /* Caller must hold local->sta_lock */
71be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
72f0706e82SJiri Benc 			     struct sta_info *sta)
73f0706e82SJiri Benc {
74f0706e82SJiri Benc 	struct sta_info *s;
75f0706e82SJiri Benc 
7617741cdcSJohannes Berg 	s = local->sta_hash[STA_HASH(sta->sta.addr)];
77f0706e82SJiri Benc 	if (!s)
78be8755e1SMichael Wu 		return -ENOENT;
79be8755e1SMichael Wu 	if (s == sta) {
8017741cdcSJohannes Berg 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
81d0709a65SJohannes Berg 				   s->hnext);
82be8755e1SMichael Wu 		return 0;
83f0706e82SJiri Benc 	}
84f0706e82SJiri Benc 
85be8755e1SMichael Wu 	while (s->hnext && s->hnext != sta)
86f0706e82SJiri Benc 		s = s->hnext;
87be8755e1SMichael Wu 	if (s->hnext) {
88d0709a65SJohannes Berg 		rcu_assign_pointer(s->hnext, sta->hnext);
89be8755e1SMichael Wu 		return 0;
90f0706e82SJiri Benc 	}
91f0706e82SJiri Benc 
92be8755e1SMichael Wu 	return -ENOENT;
93f0706e82SJiri Benc }
94f0706e82SJiri Benc 
95d0709a65SJohannes Berg /* protected by RCU */
964b7679a5SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
9743ba7e95SJohannes Berg {
9843ba7e95SJohannes Berg 	struct sta_info *sta;
9943ba7e95SJohannes Berg 
100d0709a65SJohannes Berg 	sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
10143ba7e95SJohannes Berg 	while (sta) {
1025cf12e8dSShaddy Baddah 		if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
10343ba7e95SJohannes Berg 			break;
104d0709a65SJohannes Berg 		sta = rcu_dereference(sta->hnext);
10543ba7e95SJohannes Berg 	}
10643ba7e95SJohannes Berg 	return sta;
10743ba7e95SJohannes Berg }
10843ba7e95SJohannes Berg 
109ee385855SLuis Carlos Cobo struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
110ee385855SLuis Carlos Cobo 				     struct net_device *dev)
111ee385855SLuis Carlos Cobo {
112ee385855SLuis Carlos Cobo 	struct sta_info *sta;
113ee385855SLuis Carlos Cobo 	int i = 0;
114ee385855SLuis Carlos Cobo 
115d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1162a8ca29aSLuis Carlos Cobo 		if (dev && dev != sta->sdata->dev)
1172a8ca29aSLuis Carlos Cobo 			continue;
118ee385855SLuis Carlos Cobo 		if (i < idx) {
119ee385855SLuis Carlos Cobo 			++i;
120ee385855SLuis Carlos Cobo 			continue;
121ee385855SLuis Carlos Cobo 		}
1222a8ca29aSLuis Carlos Cobo 		return sta;
123ee385855SLuis Carlos Cobo 	}
124ee385855SLuis Carlos Cobo 
125ee385855SLuis Carlos Cobo 	return NULL;
126ee385855SLuis Carlos Cobo }
127f0706e82SJiri Benc 
12893e5deb1SJohannes Berg /**
12993e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
13093e5deb1SJohannes Berg  *
1316ef307bcSRandy Dunlap  * @local: pointer to the global information
13293e5deb1SJohannes Berg  * @sta: STA info to free
13393e5deb1SJohannes Berg  *
13493e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
13593e5deb1SJohannes Berg  * that may happen before sta_info_insert().
13693e5deb1SJohannes Berg  */
13793e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
13893e5deb1SJohannes Berg 			    struct sta_info *sta)
13993e5deb1SJohannes Berg {
1404b7679a5SJohannes Berg 	rate_control_free_sta(sta);
14193e5deb1SJohannes Berg 	rate_control_put(sta->rate_ctrl);
14293e5deb1SJohannes Berg 
14393e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1440c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
1450c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
14693e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
14793e5deb1SJohannes Berg 
14893e5deb1SJohannes Berg 	kfree(sta);
14993e5deb1SJohannes Berg }
15093e5deb1SJohannes Berg 
151d0709a65SJohannes Berg void sta_info_destroy(struct sta_info *sta)
152f0706e82SJiri Benc {
15397bff8ecSJohannes Berg 	struct ieee80211_local *local;
154f0706e82SJiri Benc 	struct sk_buff *skb;
15507db2183SRon Rindjunsky 	int i;
15673651ee6SJohannes Berg 
15797bff8ecSJohannes Berg 	might_sleep();
15897bff8ecSJohannes Berg 
15973651ee6SJohannes Berg 	if (!sta)
16073651ee6SJohannes Berg 		return;
161f0706e82SJiri Benc 
16297bff8ecSJohannes Berg 	local = sta->local;
163d0709a65SJohannes Berg 
164d0709a65SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
165d0709a65SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
166d0709a65SJohannes Berg 
167d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
168d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
169d0709a65SJohannes Berg 		mesh_plink_deactivate(sta);
170d0709a65SJohannes Berg #endif
171d0709a65SJohannes Berg 
172d0709a65SJohannes Berg 	/*
1733b96766fSJohannes Berg 	 * We have only unlinked the key, and actually destroying it
1743b96766fSJohannes Berg 	 * may mean it is removed from hardware which requires that
1753b96766fSJohannes Berg 	 * the key->sta pointer is still valid, so flush the key todo
1763b96766fSJohannes Berg 	 * list here.
1773b96766fSJohannes Berg 	 *
1783b96766fSJohannes Berg 	 * ieee80211_key_todo() will synchronize_rcu() so after this
1793b96766fSJohannes Berg 	 * nothing can reference this sta struct any more.
180d0709a65SJohannes Berg 	 */
1813b96766fSJohannes Berg 	ieee80211_key_todo();
182d0709a65SJohannes Berg 
183d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
184d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
185d0709a65SJohannes Berg 		del_timer_sync(&sta->plink_timer);
186d0709a65SJohannes Berg #endif
187d0709a65SJohannes Berg 
188f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
189f0706e82SJiri Benc 		local->total_ps_buffered--;
190f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
191f0706e82SJiri Benc 	}
192d0709a65SJohannes Berg 
193d0709a65SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
194f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
195d0709a65SJohannes Berg 
196fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
197*55687e38SJohannes Berg 		struct tid_ampdu_rx *tid_rx;
198*55687e38SJohannes Berg 		struct tid_ampdu_tx *tid_tx;
199*55687e38SJohannes Berg 
20007346f81SJohannes Berg 		spin_lock_bh(&sta->lock);
201*55687e38SJohannes Berg 		tid_rx = sta->ampdu_mlme.tid_rx[i];
202*55687e38SJohannes Berg 		/* Make sure timer won't free the tid_rx struct, see below */
203*55687e38SJohannes Berg 		if (tid_rx)
204*55687e38SJohannes Berg 			tid_rx->shutdown = true;
20507346f81SJohannes Berg 		spin_unlock_bh(&sta->lock);
206*55687e38SJohannes Berg 
207*55687e38SJohannes Berg 		/*
208*55687e38SJohannes Berg 		 * Outside spinlock - shutdown is true now so that the timer
209*55687e38SJohannes Berg 		 * won't free tid_rx, we have to do that now. Can't let the
210*55687e38SJohannes Berg 		 * timer do it because we have to sync the timer outside the
211*55687e38SJohannes Berg 		 * lock that it takes itself.
212*55687e38SJohannes Berg 		 */
213*55687e38SJohannes Berg 		if (tid_rx) {
214*55687e38SJohannes Berg 			del_timer_sync(&tid_rx->session_timer);
215*55687e38SJohannes Berg 			kfree(tid_rx);
216*55687e38SJohannes Berg 		}
217*55687e38SJohannes Berg 
218*55687e38SJohannes Berg 		/*
219*55687e38SJohannes Berg 		 * No need to do such complications for TX agg sessions, the
220*55687e38SJohannes Berg 		 * path leading to freeing the tid_tx struct goes via a call
221*55687e38SJohannes Berg 		 * from the driver, and thus needs to look up the sta struct
222*55687e38SJohannes Berg 		 * again, which cannot be found when we get here. Hence, we
223*55687e38SJohannes Berg 		 * just need to delete the timer and free the aggregation
224*55687e38SJohannes Berg 		 * info; we won't be telling the peer about it then but that
225*55687e38SJohannes Berg 		 * doesn't matter if we're not talking to it again anyway.
226*55687e38SJohannes Berg 		 */
227*55687e38SJohannes Berg 		tid_tx = sta->ampdu_mlme.tid_tx[i];
228*55687e38SJohannes Berg 		if (tid_tx) {
229*55687e38SJohannes Berg 			del_timer_sync(&tid_tx->addba_resp_timer);
230*55687e38SJohannes Berg 			kfree(tid_tx);
231*55687e38SJohannes Berg 		}
232fe3bf0f5SRon Rindjunsky 	}
233cee24a3eSRon Rindjunsky 
23493e5deb1SJohannes Berg 	__sta_info_free(local, sta);
235f0706e82SJiri Benc }
236f0706e82SJiri Benc 
237f0706e82SJiri Benc 
238d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
239d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
240d0709a65SJohannes Berg 			      struct sta_info *sta)
241f0706e82SJiri Benc {
24217741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
24317741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
244f0706e82SJiri Benc }
245f0706e82SJiri Benc 
24673651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
24773651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
248f0706e82SJiri Benc {
249d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
250f0706e82SJiri Benc 	struct sta_info *sta;
25116c5f15cSRon Rindjunsky 	int i;
252f0706e82SJiri Benc 
25317741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
254f0706e82SJiri Benc 	if (!sta)
25573651ee6SJohannes Berg 		return NULL;
256f0706e82SJiri Benc 
25707346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2585a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
25907346f81SJohannes Berg 
26017741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
261d0709a65SJohannes Berg 	sta->local = local;
262d0709a65SJohannes Berg 	sta->sdata = sdata;
263f0706e82SJiri Benc 
264f0706e82SJiri Benc 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
265d0709a65SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
2664b7679a5SJohannes Berg 						     &sta->sta, gfp);
267f0706e82SJiri Benc 	if (!sta->rate_ctrl_priv) {
268f0706e82SJiri Benc 		rate_control_put(sta->rate_ctrl);
269f0706e82SJiri Benc 		kfree(sta);
27073651ee6SJohannes Berg 		return NULL;
271f0706e82SJiri Benc 	}
272f0706e82SJiri Benc 
27316c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
27416c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
27516c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
27616c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
27716c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
278fe3bf0f5SRon Rindjunsky 		/* tid to tx queue: initialize according to HW (0 is valid) */
279e2530083SJohannes Berg 		sta->tid_to_tx_q[i] = ieee80211_num_queues(&local->hw);
280cee24a3eSRon Rindjunsky 		/* rx */
281cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
282cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
283cee24a3eSRon Rindjunsky 		/* tx */
284cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
285cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
286cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
28716c5f15cSRon Rindjunsky 	}
288f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
289f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
29073651ee6SJohannes Berg 
29173651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2920c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %pM\n",
2930c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
29473651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
29573651ee6SJohannes Berg 
29603e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
297b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
29803e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
29903e4497eSJohannes Berg #endif
30003e4497eSJohannes Berg 
30173651ee6SJohannes Berg 	return sta;
30273651ee6SJohannes Berg }
30373651ee6SJohannes Berg 
30473651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta)
30573651ee6SJohannes Berg {
30673651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
30773651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
30873651ee6SJohannes Berg 	unsigned long flags;
30993e5deb1SJohannes Berg 	int err = 0;
31073651ee6SJohannes Berg 
31103e4497eSJohannes Berg 	/*
31203e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
31303e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
31403e4497eSJohannes Berg 	 * and another CPU turns off the net device.
31503e4497eSJohannes Berg 	 */
31693e5deb1SJohannes Berg 	if (unlikely(!netif_running(sdata->dev))) {
31793e5deb1SJohannes Berg 		err = -ENETDOWN;
31893e5deb1SJohannes Berg 		goto out_free;
31993e5deb1SJohannes Berg 	}
32003e4497eSJohannes Berg 
32117741cdcSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
32217741cdcSJohannes Berg 		    is_multicast_ether_addr(sta->sta.addr))) {
32393e5deb1SJohannes Berg 		err = -EINVAL;
32493e5deb1SJohannes Berg 		goto out_free;
32593e5deb1SJohannes Berg 	}
32644213b5eSJohannes Berg 
327d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
32843ba7e95SJohannes Berg 	/* check if STA exists already */
3294b7679a5SJohannes Berg 	if (sta_info_get(local, sta->sta.addr)) {
330d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
33193e5deb1SJohannes Berg 		err = -EEXIST;
33293e5deb1SJohannes Berg 		goto out_free;
33343ba7e95SJohannes Berg 	}
334f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
335f0706e82SJiri Benc 	local->num_sta++;
336f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
33732bfd35dSJohannes Berg 
338d0709a65SJohannes Berg 	/* notify driver */
339d0709a65SJohannes Berg 	if (local->ops->sta_notify) {
34005c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3413e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
3423e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
3433e122be0SJohannes Berg 					     u.ap);
34432bfd35dSJohannes Berg 
34532bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
34617741cdcSJohannes Berg 				       STA_NOTIFY_ADD, &sta->sta);
34732bfd35dSJohannes Berg 	}
348d0709a65SJohannes Berg 
349f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3500c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %pM\n",
3510c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
352f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
353f0706e82SJiri Benc 
35473651ee6SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
35573651ee6SJohannes Berg 
356e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
35793e5deb1SJohannes Berg 	/*
35893e5deb1SJohannes Berg 	 * Debugfs entry adding might sleep, so schedule process
359e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
36093e5deb1SJohannes Berg 	 * have one.
36193e5deb1SJohannes Berg 	 * NOTE: due to auto-freeing semantics this may only be done
36293e5deb1SJohannes Berg 	 *       if the insertion is successful!
36393e5deb1SJohannes Berg 	 */
36449ec6fa2SJohannes Berg 	schedule_work(&local->sta_debugfs_add);
365e9f207f0SJiri Benc #endif
366e9f207f0SJiri Benc 
36773651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
36873651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
36973651ee6SJohannes Berg 
37073651ee6SJohannes Berg 	return 0;
37193e5deb1SJohannes Berg  out_free:
37293e5deb1SJohannes Berg 	BUG_ON(!err);
37393e5deb1SJohannes Berg 	__sta_info_free(local, sta);
37493e5deb1SJohannes Berg 	return err;
375f0706e82SJiri Benc }
376f0706e82SJiri Benc 
377004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
378004c872eSJohannes Berg {
379004c872eSJohannes Berg 	/*
380004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
381004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
382004c872eSJohannes Berg 	 */
383004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
384004c872eSJohannes Berg }
385004c872eSJohannes Berg 
386004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
387004c872eSJohannes Berg {
388004c872eSJohannes Berg 	/*
389004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
390004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
391004c872eSJohannes Berg 	 */
392004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
393004c872eSJohannes Berg }
394004c872eSJohannes Berg 
395004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
396004c872eSJohannes Berg 				   struct sta_info *sta)
397004c872eSJohannes Berg {
3983e122be0SJohannes Berg 	BUG_ON(!bss);
3993e122be0SJohannes Berg 
40017741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
4013e122be0SJohannes Berg 
402d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
403d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
40417741cdcSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local),
40517741cdcSJohannes Berg 					 &sta->sta, true);
406d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
407d0709a65SJohannes Berg 	}
408004c872eSJohannes Berg }
409004c872eSJohannes Berg 
410004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
411004c872eSJohannes Berg {
412d0709a65SJohannes Berg 	unsigned long flags;
413004c872eSJohannes Berg 
4143e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4153e122be0SJohannes Berg 
416d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
417d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
418d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
419004c872eSJohannes Berg }
420004c872eSJohannes Berg 
421004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
422004c872eSJohannes Berg 				     struct sta_info *sta)
423004c872eSJohannes Berg {
4243e122be0SJohannes Berg 	BUG_ON(!bss);
4253e122be0SJohannes Berg 
42617741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
4273e122be0SJohannes Berg 
428d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
429d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
43017741cdcSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local),
43117741cdcSJohannes Berg 					 &sta->sta, false);
432d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
433d0709a65SJohannes Berg 	}
434004c872eSJohannes Berg }
435004c872eSJohannes Berg 
436004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
437004c872eSJohannes Berg {
438d0709a65SJohannes Berg 	unsigned long flags;
439004c872eSJohannes Berg 
4403e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4413e122be0SJohannes Berg 
442d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
443d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
444d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
445004c872eSJohannes Berg }
446004c872eSJohannes Berg 
44724723d1bSJohannes Berg static void __sta_info_unlink(struct sta_info **sta)
448d0709a65SJohannes Berg {
449d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
450d0709a65SJohannes Berg 	struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
451d0709a65SJohannes Berg 	/*
452d0709a65SJohannes Berg 	 * pull caller's reference if we're already gone.
453d0709a65SJohannes Berg 	 */
454d0709a65SJohannes Berg 	if (sta_info_hash_del(local, *sta)) {
455d0709a65SJohannes Berg 		*sta = NULL;
456be8755e1SMichael Wu 		return;
457d0709a65SJohannes Berg 	}
458be8755e1SMichael Wu 
4593b96766fSJohannes Berg 	if ((*sta)->key) {
4603b96766fSJohannes Berg 		ieee80211_key_free((*sta)->key);
4613b96766fSJohannes Berg 		WARN_ON((*sta)->key);
4623b96766fSJohannes Berg 	}
4633b96766fSJohannes Berg 
4647d1559f1SJohannes Berg 	list_del(&(*sta)->list);
4657d1559f1SJohannes Berg 
46607346f81SJohannes Berg 	if (test_and_clear_sta_flags(*sta, WLAN_STA_PS)) {
4673e122be0SJohannes Berg 		BUG_ON(!sdata->bss);
4683e122be0SJohannes Berg 
4697d1559f1SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
4707d1559f1SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, *sta);
4717d1559f1SJohannes Berg 	}
4727d1559f1SJohannes Berg 
4737d1559f1SJohannes Berg 	local->num_sta--;
4747d1559f1SJohannes Berg 
4757d1559f1SJohannes Berg 	if (local->ops->sta_notify) {
47605c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4773e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
4783e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
4793e122be0SJohannes Berg 					     u.ap);
4807d1559f1SJohannes Berg 
4817d1559f1SJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
48217741cdcSJohannes Berg 				       STA_NOTIFY_REMOVE, &(*sta)->sta);
4837d1559f1SJohannes Berg 	}
4847d1559f1SJohannes Berg 
4857d1559f1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
4867d1559f1SJohannes Berg 		mesh_accept_plinks_update(sdata);
4877d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
4887d1559f1SJohannes Berg 		del_timer(&(*sta)->plink_timer);
4897d1559f1SJohannes Berg #endif
4907d1559f1SJohannes Berg 	}
4917d1559f1SJohannes Berg 
4927d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4930c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %pM\n",
4940c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
4957d1559f1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
4967d1559f1SJohannes Berg 
497d0709a65SJohannes Berg 	/*
4987d1559f1SJohannes Berg 	 * Finally, pull caller's reference if the STA is pinned by the
499d0709a65SJohannes Berg 	 * task that is adding the debugfs entries. In that case, we
500d0709a65SJohannes Berg 	 * leave the STA "to be freed".
501d0709a65SJohannes Berg 	 *
502d0709a65SJohannes Berg 	 * The rules are not trivial, but not too complex either:
503d0709a65SJohannes Berg 	 *  (1) pin_status is only modified under the sta_lock
50449ec6fa2SJohannes Berg 	 *  (2) STAs may only be pinned under the RTNL so that
50549ec6fa2SJohannes Berg 	 *	sta_info_flush() is guaranteed to actually destroy
50649ec6fa2SJohannes Berg 	 *	all STAs that are active for a given interface, this
50749ec6fa2SJohannes Berg 	 *	is required for correctness because otherwise we
50849ec6fa2SJohannes Berg 	 *	could notify a driver that an interface is going
50949ec6fa2SJohannes Berg 	 *	away and only after that (!) notify it about a STA
51049ec6fa2SJohannes Berg 	 *	on that interface going away.
51149ec6fa2SJohannes Berg 	 *  (3) sta_info_debugfs_add_work() will set the status
512d0709a65SJohannes Berg 	 *	to PINNED when it found an item that needs a new
513d0709a65SJohannes Berg 	 *	debugfs directory created. In that case, that item
514d0709a65SJohannes Berg 	 *	must not be freed although all *RCU* users are done
515d0709a65SJohannes Berg 	 *	with it. Hence, we tell the caller of _unlink()
516d0709a65SJohannes Berg 	 *	that the item is already gone (as can happen when
517d0709a65SJohannes Berg 	 *	two tasks try to unlink/destroy at the same time)
51849ec6fa2SJohannes Berg 	 *  (4) We set the pin_status to DESTROY here when we
519d0709a65SJohannes Berg 	 *	find such an item.
52049ec6fa2SJohannes Berg 	 *  (5) sta_info_debugfs_add_work() will reset the pin_status
521d0709a65SJohannes Berg 	 *	from PINNED to NORMAL when it is done with the item,
522d0709a65SJohannes Berg 	 *	but will check for DESTROY before resetting it in
523d0709a65SJohannes Berg 	 *	which case it will free the item.
524d0709a65SJohannes Berg 	 */
525d0709a65SJohannes Berg 	if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
526d0709a65SJohannes Berg 		(*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
527d0709a65SJohannes Berg 		*sta = NULL;
528d0709a65SJohannes Berg 		return;
529d0709a65SJohannes Berg 	}
530d0709a65SJohannes Berg }
531d0709a65SJohannes Berg 
532d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta)
533d0709a65SJohannes Berg {
534d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
535d0709a65SJohannes Berg 	unsigned long flags;
536d0709a65SJohannes Berg 
537d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
538d0709a65SJohannes Berg 	__sta_info_unlink(sta);
539d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
540d0709a65SJohannes Berg }
541f0706e82SJiri Benc 
542f0706e82SJiri Benc static inline int sta_info_buffer_expired(struct ieee80211_local *local,
543f0706e82SJiri Benc 					  struct sta_info *sta,
544f0706e82SJiri Benc 					  struct sk_buff *skb)
545f0706e82SJiri Benc {
546e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
547f0706e82SJiri Benc 	int timeout;
548f0706e82SJiri Benc 
549f0706e82SJiri Benc 	if (!skb)
550f0706e82SJiri Benc 		return 0;
551f0706e82SJiri Benc 
552e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
553f0706e82SJiri Benc 
554f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
555f0706e82SJiri Benc 	timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
556f0706e82SJiri Benc 		   15625) * HZ;
557f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
558f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
559e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
560f0706e82SJiri Benc }
561f0706e82SJiri Benc 
562f0706e82SJiri Benc 
563f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
564f0706e82SJiri Benc 					     struct sta_info *sta)
565f0706e82SJiri Benc {
566f0706e82SJiri Benc 	unsigned long flags;
567f0706e82SJiri Benc 	struct sk_buff *skb;
568836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
569f0706e82SJiri Benc 
570f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
571f0706e82SJiri Benc 		return;
572f0706e82SJiri Benc 
573f0706e82SJiri Benc 	for (;;) {
574f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
575f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
576836341a7SJohannes Berg 		if (sta_info_buffer_expired(local, sta, skb))
577f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
578836341a7SJohannes Berg 		else
579f0706e82SJiri Benc 			skb = NULL;
580f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
581f0706e82SJiri Benc 
582836341a7SJohannes Berg 		if (!skb)
583836341a7SJohannes Berg 			break;
584836341a7SJohannes Berg 
585d0709a65SJohannes Berg 		sdata = sta->sdata;
586f0706e82SJiri Benc 		local->total_ps_buffered--;
587f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
5880c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
5890c68ae26SJohannes Berg 		       sta->sta.addr);
590f4ea83ddSJohannes Berg #endif
591f0706e82SJiri Benc 		dev_kfree_skb(skb);
592836341a7SJohannes Berg 
593004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
594004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
595f0706e82SJiri Benc 	}
596f0706e82SJiri Benc }
597f0706e82SJiri Benc 
598f0706e82SJiri Benc 
599f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
600f0706e82SJiri Benc {
601f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
602f0706e82SJiri Benc 	struct sta_info *sta;
603f0706e82SJiri Benc 
604d0709a65SJohannes Berg 	rcu_read_lock();
605d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
606f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
607d0709a65SJohannes Berg 	rcu_read_unlock();
608f0706e82SJiri Benc 
6090d174406SJohannes Berg 	local->sta_cleanup.expires =
6100d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
611f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
612f0706e82SJiri Benc }
613f0706e82SJiri Benc 
614e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
6154d6141c3SJiri Slaby /*
6164d6141c3SJiri Slaby  * See comment in __sta_info_unlink,
6174d6141c3SJiri Slaby  * caller must hold local->sta_lock.
6184d6141c3SJiri Slaby  */
6194d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta)
6204d6141c3SJiri Slaby {
6214d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
6224d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_PINNED;
6234d6141c3SJiri Slaby }
6244d6141c3SJiri Slaby 
6254d6141c3SJiri Slaby /*
6264d6141c3SJiri Slaby  * See comment in __sta_info_unlink, returns sta if it
6274d6141c3SJiri Slaby  * needs to be destroyed.
6284d6141c3SJiri Slaby  */
6294d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta)
6304d6141c3SJiri Slaby {
6314d6141c3SJiri Slaby 	struct sta_info *ret = NULL;
6324d6141c3SJiri Slaby 	unsigned long flags;
6334d6141c3SJiri Slaby 
6344d6141c3SJiri Slaby 	spin_lock_irqsave(&sta->local->sta_lock, flags);
6354d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
6364d6141c3SJiri Slaby 		sta->pin_status != STA_INFO_PIN_STAT_PINNED);
6374d6141c3SJiri Slaby 	if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
6384d6141c3SJiri Slaby 		ret = sta;
6394d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
6404d6141c3SJiri Slaby 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
6414d6141c3SJiri Slaby 
6424d6141c3SJiri Slaby 	return ret;
6434d6141c3SJiri Slaby }
6444d6141c3SJiri Slaby 
645d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work)
646e9f207f0SJiri Benc {
647e9f207f0SJiri Benc 	struct ieee80211_local *local =
648e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
649e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
650d0709a65SJohannes Berg 	unsigned long flags;
651e9f207f0SJiri Benc 
65249ec6fa2SJohannes Berg 	/* We need to keep the RTNL across the whole pinned status. */
65349ec6fa2SJohannes Berg 	rtnl_lock();
654e9f207f0SJiri Benc 	while (1) {
655e9f207f0SJiri Benc 		sta = NULL;
656d0709a65SJohannes Berg 
657d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
658e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
65963044e9fSJohannes Berg 			/*
66063044e9fSJohannes Berg 			 * debugfs.add_has_run will be set by
66163044e9fSJohannes Berg 			 * ieee80211_sta_debugfs_add regardless
66263044e9fSJohannes Berg 			 * of what else it does.
66363044e9fSJohannes Berg 			 */
66463044e9fSJohannes Berg 			if (!tmp->debugfs.add_has_run) {
665e9f207f0SJiri Benc 				sta = tmp;
666d0709a65SJohannes Berg 				__sta_info_pin(sta);
667e9f207f0SJiri Benc 				break;
668e9f207f0SJiri Benc 			}
669e9f207f0SJiri Benc 		}
670d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
671e9f207f0SJiri Benc 
672e9f207f0SJiri Benc 		if (!sta)
673e9f207f0SJiri Benc 			break;
674e9f207f0SJiri Benc 
675e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
676e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
677d0709a65SJohannes Berg 
678d0709a65SJohannes Berg 		sta = __sta_info_unpin(sta);
679d0709a65SJohannes Berg 		sta_info_destroy(sta);
680e9f207f0SJiri Benc 	}
68149ec6fa2SJohannes Berg 	rtnl_unlock();
682e9f207f0SJiri Benc }
683e9f207f0SJiri Benc #endif
684e9f207f0SJiri Benc 
6853b96766fSJohannes Berg static void __ieee80211_run_pending_flush(struct ieee80211_local *local)
686dc6676b7SJohannes Berg {
687dc6676b7SJohannes Berg 	struct sta_info *sta;
688dc6676b7SJohannes Berg 	unsigned long flags;
689dc6676b7SJohannes Berg 
690dc6676b7SJohannes Berg 	ASSERT_RTNL();
691dc6676b7SJohannes Berg 
692dc6676b7SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
693dc6676b7SJohannes Berg 	while (!list_empty(&local->sta_flush_list)) {
694dc6676b7SJohannes Berg 		sta = list_first_entry(&local->sta_flush_list,
695dc6676b7SJohannes Berg 				       struct sta_info, list);
696dc6676b7SJohannes Berg 		list_del(&sta->list);
697dc6676b7SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
698dc6676b7SJohannes Berg 		sta_info_destroy(sta);
699dc6676b7SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
700dc6676b7SJohannes Berg 	}
701dc6676b7SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
702dc6676b7SJohannes Berg }
703dc6676b7SJohannes Berg 
704dc6676b7SJohannes Berg static void ieee80211_sta_flush_work(struct work_struct *work)
705dc6676b7SJohannes Berg {
706dc6676b7SJohannes Berg 	struct ieee80211_local *local =
707dc6676b7SJohannes Berg 		container_of(work, struct ieee80211_local, sta_flush_work);
708dc6676b7SJohannes Berg 
709dc6676b7SJohannes Berg 	rtnl_lock();
710dc6676b7SJohannes Berg 	__ieee80211_run_pending_flush(local);
711dc6676b7SJohannes Berg 	rtnl_unlock();
712dc6676b7SJohannes Berg }
713dc6676b7SJohannes Berg 
714f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
715f0706e82SJiri Benc {
716d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
717f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
718dc6676b7SJohannes Berg 	INIT_LIST_HEAD(&local->sta_flush_list);
719dc6676b7SJohannes Berg 	INIT_WORK(&local->sta_flush_work, ieee80211_sta_flush_work);
720f0706e82SJiri Benc 
721b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
722b24b8a24SPavel Emelyanov 		    (unsigned long)local);
7230d174406SJohannes Berg 	local->sta_cleanup.expires =
7240d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
725e9f207f0SJiri Benc 
726e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
727d0709a65SJohannes Berg 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
728e9f207f0SJiri Benc #endif
729f0706e82SJiri Benc }
730f0706e82SJiri Benc 
731f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
732f0706e82SJiri Benc {
733f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
734f0706e82SJiri Benc 	return 0;
735f0706e82SJiri Benc }
736f0706e82SJiri Benc 
737f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
738f0706e82SJiri Benc {
739f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
740dc6676b7SJohannes Berg 	cancel_work_sync(&local->sta_flush_work);
74149ec6fa2SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS
74249ec6fa2SJohannes Berg 	/*
74349ec6fa2SJohannes Berg 	 * Make sure the debugfs adding work isn't pending after this
74449ec6fa2SJohannes Berg 	 * because we're about to be destroyed. It doesn't matter
74549ec6fa2SJohannes Berg 	 * whether it ran or not since we're going to flush all STAs
74649ec6fa2SJohannes Berg 	 * anyway.
74749ec6fa2SJohannes Berg 	 */
74849ec6fa2SJohannes Berg 	cancel_work_sync(&local->sta_debugfs_add);
74949ec6fa2SJohannes Berg #endif
750dc6676b7SJohannes Berg 
751dc6676b7SJohannes Berg 	rtnl_lock();
752be8755e1SMichael Wu 	sta_info_flush(local, NULL);
753dc6676b7SJohannes Berg 	__ieee80211_run_pending_flush(local);
754dc6676b7SJohannes Berg 	rtnl_unlock();
755f0706e82SJiri Benc }
756f0706e82SJiri Benc 
757f0706e82SJiri Benc /**
758f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
75944213b5eSJohannes Berg  *
76044213b5eSJohannes Berg  * Returns the number of removed STA entries.
76144213b5eSJohannes Berg  *
762f0706e82SJiri Benc  * @local: local interface data
763d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
764f0706e82SJiri Benc  */
76544213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
766d0709a65SJohannes Berg 		    struct ieee80211_sub_if_data *sdata)
767f0706e82SJiri Benc {
768f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
769be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
77044213b5eSJohannes Berg 	int ret = 0;
771d0709a65SJohannes Berg 	unsigned long flags;
772f0706e82SJiri Benc 
773d0709a65SJohannes Berg 	might_sleep();
774dc6676b7SJohannes Berg 	ASSERT_RTNL();
775d0709a65SJohannes Berg 
776d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
777d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
778d0709a65SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
779d0709a65SJohannes Berg 			__sta_info_unlink(&sta);
78044213b5eSJohannes Berg 			if (sta) {
781be8755e1SMichael Wu 				list_add_tail(&sta->list, &tmp_list);
78244213b5eSJohannes Berg 				ret++;
78344213b5eSJohannes Berg 			}
784be8755e1SMichael Wu 		}
785be8755e1SMichael Wu 	}
786d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
787d0709a65SJohannes Berg 
788d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
789d0709a65SJohannes Berg 		sta_info_destroy(sta);
79044213b5eSJohannes Berg 
79144213b5eSJohannes Berg 	return ret;
792f0706e82SJiri Benc }
793dc6676b7SJohannes Berg 
794dc6676b7SJohannes Berg /**
795dc6676b7SJohannes Berg  * sta_info_flush_delayed - flush matching STA entries from the STA table
796dc6676b7SJohannes Berg  *
797dc6676b7SJohannes Berg  * This function unlinks all stations for a given interface and queues
798dc6676b7SJohannes Berg  * them for freeing. Note that the workqueue function scheduled here has
799dc6676b7SJohannes Berg  * to run before any new keys can be added to the system to avoid set_key()
800dc6676b7SJohannes Berg  * callback ordering issues.
801dc6676b7SJohannes Berg  *
802dc6676b7SJohannes Berg  * @sdata: the interface
803dc6676b7SJohannes Berg  */
804dc6676b7SJohannes Berg void sta_info_flush_delayed(struct ieee80211_sub_if_data *sdata)
805dc6676b7SJohannes Berg {
806dc6676b7SJohannes Berg 	struct ieee80211_local *local = sdata->local;
807dc6676b7SJohannes Berg 	struct sta_info *sta, *tmp;
808dc6676b7SJohannes Berg 	unsigned long flags;
809dc6676b7SJohannes Berg 	bool work = false;
810dc6676b7SJohannes Berg 
811dc6676b7SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
812dc6676b7SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
813dc6676b7SJohannes Berg 		if (sdata == sta->sdata) {
814dc6676b7SJohannes Berg 			__sta_info_unlink(&sta);
815dc6676b7SJohannes Berg 			if (sta) {
816dc6676b7SJohannes Berg 				list_add_tail(&sta->list,
817dc6676b7SJohannes Berg 					      &local->sta_flush_list);
818dc6676b7SJohannes Berg 				work = true;
819dc6676b7SJohannes Berg 			}
820dc6676b7SJohannes Berg 		}
821dc6676b7SJohannes Berg 	}
822dc6676b7SJohannes Berg 	if (work)
823dc6676b7SJohannes Berg 		schedule_work(&local->sta_flush_work);
824dc6676b7SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
825dc6676b7SJohannes Berg }
82624723d1bSJohannes Berg 
82724723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
82824723d1bSJohannes Berg 			  unsigned long exp_time)
82924723d1bSJohannes Berg {
83024723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
83124723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
83224723d1bSJohannes Berg 	LIST_HEAD(tmp_list);
83324723d1bSJohannes Berg 	unsigned long flags;
83424723d1bSJohannes Berg 
83524723d1bSJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
83624723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
83724723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
83824723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
8390c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
8400c68ae26SJohannes Berg 			       sdata->dev->name, sta->sta.addr);
84124723d1bSJohannes Berg #endif
84224723d1bSJohannes Berg 			__sta_info_unlink(&sta);
84324723d1bSJohannes Berg 			if (sta)
84424723d1bSJohannes Berg 				list_add(&sta->list, &tmp_list);
84524723d1bSJohannes Berg 		}
84624723d1bSJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
84724723d1bSJohannes Berg 
84824723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
84924723d1bSJohannes Berg 		sta_info_destroy(sta);
85024723d1bSJohannes Berg }
85117741cdcSJohannes Berg 
85217741cdcSJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_hw *hw,
85317741cdcSJohannes Berg 					 const u8 *addr)
85417741cdcSJohannes Berg {
8554b7679a5SJohannes Berg 	struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
85617741cdcSJohannes Berg 
85717741cdcSJohannes Berg 	if (!sta)
85817741cdcSJohannes Berg 		return NULL;
85917741cdcSJohannes Berg 	return &sta->sta;
86017741cdcSJohannes Berg }
86117741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
862