xref: /openbmc/linux/net/mac80211/sta_info.c (revision 4d6141c30a2567a85d869d55f579438b3365d719)
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"
22f0706e82SJiri Benc #include "ieee80211_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
5493e5deb1SJohannes Berg  * then destroy it while holding the RTNL; sta_info_destroy() will wait
5593e5deb1SJohannes Berg  * for an RCU grace period to elapse before actually freeing it. Due to
56d0709a65SJohannes Berg  * the pinning and the possibility of multiple callers trying to remove
57dbbea671SJohannes Berg  * the same STA info at the same time, sta_info_unlink() can clear the
58d0709a65SJohannes Berg  * STA info pointer it is passed to indicate that the STA info is owned
59d0709a65SJohannes Berg  * by somebody else now.
60d0709a65SJohannes Berg  *
61dbbea671SJohannes Berg  * If sta_info_unlink() did not clear the pointer then the caller owns
62d0709a65SJohannes Berg  * the STA info structure now and is responsible of destroying it with
63dbbea671SJohannes Berg  * a call to sta_info_destroy(), not before RCU synchronisation, of
64d0709a65SJohannes Berg  * course. Note that sta_info_destroy() must be protected by the RTNL.
65d0709a65SJohannes Berg  *
66d0709a65SJohannes Berg  * In all other cases, there is no concept of ownership on a STA entry,
67d0709a65SJohannes Berg  * each structure is owned by the global hash table/list until it is
68d0709a65SJohannes Berg  * removed. All users of the structure need to be RCU protected so that
69d0709a65SJohannes Berg  * the structure won't be freed before they are done using it.
70d0709a65SJohannes Berg  */
71f0706e82SJiri Benc 
72f0706e82SJiri Benc /* Caller must hold local->sta_lock */
73be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
74f0706e82SJiri Benc 			     struct sta_info *sta)
75f0706e82SJiri Benc {
76f0706e82SJiri Benc 	struct sta_info *s;
77f0706e82SJiri Benc 
78f0706e82SJiri Benc 	s = local->sta_hash[STA_HASH(sta->addr)];
79f0706e82SJiri Benc 	if (!s)
80be8755e1SMichael Wu 		return -ENOENT;
81be8755e1SMichael Wu 	if (s == sta) {
82d0709a65SJohannes Berg 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)],
83d0709a65SJohannes Berg 				   s->hnext);
84be8755e1SMichael Wu 		return 0;
85f0706e82SJiri Benc 	}
86f0706e82SJiri Benc 
87be8755e1SMichael Wu 	while (s->hnext && s->hnext != sta)
88f0706e82SJiri Benc 		s = s->hnext;
89be8755e1SMichael Wu 	if (s->hnext) {
90d0709a65SJohannes Berg 		rcu_assign_pointer(s->hnext, sta->hnext);
91be8755e1SMichael Wu 		return 0;
92f0706e82SJiri Benc 	}
93f0706e82SJiri Benc 
94be8755e1SMichael Wu 	return -ENOENT;
95f0706e82SJiri Benc }
96f0706e82SJiri Benc 
97d0709a65SJohannes Berg /* protected by RCU */
9843ba7e95SJohannes Berg static struct sta_info *__sta_info_find(struct ieee80211_local *local,
9943ba7e95SJohannes Berg 					u8 *addr)
10043ba7e95SJohannes Berg {
10143ba7e95SJohannes Berg 	struct sta_info *sta;
10243ba7e95SJohannes Berg 
103d0709a65SJohannes Berg 	sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
10443ba7e95SJohannes Berg 	while (sta) {
10543ba7e95SJohannes Berg 		if (compare_ether_addr(sta->addr, addr) == 0)
10643ba7e95SJohannes Berg 			break;
107d0709a65SJohannes Berg 		sta = rcu_dereference(sta->hnext);
10843ba7e95SJohannes Berg 	}
10943ba7e95SJohannes Berg 	return sta;
11043ba7e95SJohannes Berg }
11143ba7e95SJohannes Berg 
112f0706e82SJiri Benc struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
113f0706e82SJiri Benc {
114d0709a65SJohannes Berg 	return __sta_info_find(local, addr);
115f0706e82SJiri Benc }
116f0706e82SJiri Benc EXPORT_SYMBOL(sta_info_get);
117f0706e82SJiri Benc 
118ee385855SLuis Carlos Cobo struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
119ee385855SLuis Carlos Cobo 				     struct net_device *dev)
120ee385855SLuis Carlos Cobo {
121ee385855SLuis Carlos Cobo 	struct sta_info *sta;
122ee385855SLuis Carlos Cobo 	int i = 0;
123ee385855SLuis Carlos Cobo 
124d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1252a8ca29aSLuis Carlos Cobo 		if (dev && dev != sta->sdata->dev)
1262a8ca29aSLuis Carlos Cobo 			continue;
127ee385855SLuis Carlos Cobo 		if (i < idx) {
128ee385855SLuis Carlos Cobo 			++i;
129ee385855SLuis Carlos Cobo 			continue;
130ee385855SLuis Carlos Cobo 		}
1312a8ca29aSLuis Carlos Cobo 		return sta;
132ee385855SLuis Carlos Cobo 	}
133ee385855SLuis Carlos Cobo 
134ee385855SLuis Carlos Cobo 	return NULL;
135ee385855SLuis Carlos Cobo }
136f0706e82SJiri Benc 
13793e5deb1SJohannes Berg /**
13893e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
13993e5deb1SJohannes Berg  *
14093e5deb1SJohannes Berg  * @sta: STA info to free
14193e5deb1SJohannes Berg  *
14293e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
14393e5deb1SJohannes Berg  * that may happen before sta_info_insert().
14493e5deb1SJohannes Berg  */
14593e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
14693e5deb1SJohannes Berg 			    struct sta_info *sta)
14793e5deb1SJohannes Berg {
14893e5deb1SJohannes Berg 	DECLARE_MAC_BUF(mbuf);
14993e5deb1SJohannes Berg 
15093e5deb1SJohannes Berg 	rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
15193e5deb1SJohannes Berg 	rate_control_put(sta->rate_ctrl);
15293e5deb1SJohannes Berg 
15393e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
15493e5deb1SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %s\n",
15593e5deb1SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mbuf, 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 	ASSERT_RTNL();
16897bff8ecSJohannes Berg 	might_sleep();
16997bff8ecSJohannes Berg 
17073651ee6SJohannes Berg 	if (!sta)
17173651ee6SJohannes Berg 		return;
172f0706e82SJiri Benc 
17397bff8ecSJohannes Berg 	local = sta->local;
174d0709a65SJohannes Berg 
175d0709a65SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
176d0709a65SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
177d0709a65SJohannes Berg 
178d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
179d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
180d0709a65SJohannes Berg 		mesh_plink_deactivate(sta);
181d0709a65SJohannes Berg #endif
182d0709a65SJohannes Berg 
1834f6fab47SJohannes Berg 	if (sta->key) {
184d0709a65SJohannes Berg 		/*
185d0709a65SJohannes Berg 		 * NOTE: This will call synchronize_rcu() internally to
186d0709a65SJohannes Berg 		 * make sure no key references can be in use. We rely on
1874f6fab47SJohannes Berg 		 * that when we take this branch to make sure nobody can
1884f6fab47SJohannes Berg 		 * reference this STA struct any longer!
189d0709a65SJohannes Berg 		 */
190d0709a65SJohannes Berg 		ieee80211_key_free(sta->key);
191d0709a65SJohannes Berg 		WARN_ON(sta->key);
1924f6fab47SJohannes Berg 	} else {
1934f6fab47SJohannes Berg 		/*
1944f6fab47SJohannes Berg 		 * Make sure that nobody can reference this STA struct
1954f6fab47SJohannes Berg 		 * any longer.
1964f6fab47SJohannes Berg 		 */
1974f6fab47SJohannes Berg 		synchronize_rcu();
1984f6fab47SJohannes Berg 	}
199d0709a65SJohannes Berg 
200d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
201d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
202d0709a65SJohannes Berg 		del_timer_sync(&sta->plink_timer);
203d0709a65SJohannes Berg #endif
204d0709a65SJohannes Berg 
205f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
206f0706e82SJiri Benc 		local->total_ps_buffered--;
207f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
208f0706e82SJiri Benc 	}
209d0709a65SJohannes Berg 
210d0709a65SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
211f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
212d0709a65SJohannes Berg 
213fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
214cee24a3eSRon Rindjunsky 		spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
215cee24a3eSRon Rindjunsky 		if (sta->ampdu_mlme.tid_rx[i])
216cee24a3eSRon Rindjunsky 		  del_timer_sync(&sta->ampdu_mlme.tid_rx[i]->session_timer);
217cee24a3eSRon Rindjunsky 		spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
218cee24a3eSRon Rindjunsky 		spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
219cee24a3eSRon Rindjunsky 		if (sta->ampdu_mlme.tid_tx[i])
220cee24a3eSRon Rindjunsky 		  del_timer_sync(&sta->ampdu_mlme.tid_tx[i]->addba_resp_timer);
221cee24a3eSRon Rindjunsky 		spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
222fe3bf0f5SRon Rindjunsky 	}
223cee24a3eSRon Rindjunsky 
22493e5deb1SJohannes Berg 	__sta_info_free(local, sta);
225f0706e82SJiri Benc }
226f0706e82SJiri Benc 
227f0706e82SJiri Benc 
228d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
229d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
230d0709a65SJohannes Berg 			      struct sta_info *sta)
231f0706e82SJiri Benc {
232d0709a65SJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
233d0709a65SJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)], sta);
234f0706e82SJiri Benc }
235f0706e82SJiri Benc 
23673651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
23773651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
238f0706e82SJiri Benc {
239d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
240f0706e82SJiri Benc 	struct sta_info *sta;
24116c5f15cSRon Rindjunsky 	int i;
24273651ee6SJohannes Berg 	DECLARE_MAC_BUF(mbuf);
243f0706e82SJiri Benc 
24473651ee6SJohannes Berg 	sta = kzalloc(sizeof(*sta), gfp);
245f0706e82SJiri Benc 	if (!sta)
24673651ee6SJohannes Berg 		return NULL;
247f0706e82SJiri Benc 
248d0709a65SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
249d0709a65SJohannes Berg 	sta->local = local;
250d0709a65SJohannes Berg 	sta->sdata = sdata;
251f0706e82SJiri Benc 
252f0706e82SJiri Benc 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
253d0709a65SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
25473651ee6SJohannes Berg 						     gfp);
255f0706e82SJiri Benc 	if (!sta->rate_ctrl_priv) {
256f0706e82SJiri Benc 		rate_control_put(sta->rate_ctrl);
257f0706e82SJiri Benc 		kfree(sta);
25873651ee6SJohannes Berg 		return NULL;
259f0706e82SJiri Benc 	}
260f0706e82SJiri Benc 
26116c5f15cSRon Rindjunsky 	spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
262fe3bf0f5SRon Rindjunsky 	spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
26316c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
26416c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
26516c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
26616c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
26716c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
268fe3bf0f5SRon Rindjunsky 		/* tid to tx queue: initialize according to HW (0 is valid) */
269fe3bf0f5SRon Rindjunsky 		sta->tid_to_tx_q[i] = local->hw.queues;
270cee24a3eSRon Rindjunsky 		/* rx */
271cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
272cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
273cee24a3eSRon Rindjunsky 		/* tx */
274cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
275cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
276cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
27716c5f15cSRon Rindjunsky 	}
278f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
279f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
28073651ee6SJohannes Berg 
28173651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
28273651ee6SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %s\n",
28373651ee6SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mbuf, sta->addr));
28473651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
28573651ee6SJohannes Berg 
28603e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
287b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
28803e4497eSJohannes Berg 	spin_lock_init(&sta->plink_lock);
28903e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
29003e4497eSJohannes Berg #endif
29103e4497eSJohannes Berg 
29273651ee6SJohannes Berg 	return sta;
29373651ee6SJohannes Berg }
29473651ee6SJohannes Berg 
29573651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta)
29673651ee6SJohannes Berg {
29773651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
29873651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
29973651ee6SJohannes Berg 	unsigned long flags;
30093e5deb1SJohannes Berg 	int err = 0;
30173651ee6SJohannes Berg 	DECLARE_MAC_BUF(mac);
30273651ee6SJohannes Berg 
30303e4497eSJohannes Berg 	/*
30403e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
30503e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
30603e4497eSJohannes Berg 	 * and another CPU turns off the net device.
30703e4497eSJohannes Berg 	 */
30893e5deb1SJohannes Berg 	if (unlikely(!netif_running(sdata->dev))) {
30993e5deb1SJohannes Berg 		err = -ENETDOWN;
31093e5deb1SJohannes Berg 		goto out_free;
31193e5deb1SJohannes Berg 	}
31203e4497eSJohannes Berg 
31393e5deb1SJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->addr, sdata->dev->dev_addr) == 0 ||
31493e5deb1SJohannes Berg 	            is_multicast_ether_addr(sta->addr))) {
31593e5deb1SJohannes Berg 		err = -EINVAL;
31693e5deb1SJohannes Berg 		goto out_free;
31793e5deb1SJohannes Berg 	}
31844213b5eSJohannes Berg 
319d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
32043ba7e95SJohannes Berg 	/* check if STA exists already */
32173651ee6SJohannes Berg 	if (__sta_info_find(local, sta->addr)) {
322d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
32393e5deb1SJohannes Berg 		err = -EEXIST;
32493e5deb1SJohannes Berg 		goto out_free;
32543ba7e95SJohannes Berg 	}
326f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
327f0706e82SJiri Benc 	local->num_sta++;
328f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
32932bfd35dSJohannes Berg 
330d0709a65SJohannes Berg 	/* notify driver */
331d0709a65SJohannes Berg 	if (local->ops->sta_notify) {
33251fb61e7SJohannes Berg 		if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
33332bfd35dSJohannes Berg 			sdata = sdata->u.vlan.ap;
33432bfd35dSJohannes Berg 
33532bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
33673651ee6SJohannes Berg 				       STA_NOTIFY_ADD, sta->addr);
33732bfd35dSJohannes Berg 	}
338d0709a65SJohannes Berg 
339f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
34073651ee6SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %s\n",
34173651ee6SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
342f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
343f0706e82SJiri Benc 
34473651ee6SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
34573651ee6SJohannes Berg 
346e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
34793e5deb1SJohannes Berg 	/*
34893e5deb1SJohannes Berg 	 * Debugfs entry adding might sleep, so schedule process
349e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
35093e5deb1SJohannes Berg 	 * have one.
35193e5deb1SJohannes Berg 	 * NOTE: due to auto-freeing semantics this may only be done
35293e5deb1SJohannes Berg 	 *       if the insertion is successful!
35393e5deb1SJohannes Berg 	 */
35449ec6fa2SJohannes Berg 	schedule_work(&local->sta_debugfs_add);
355e9f207f0SJiri Benc #endif
356e9f207f0SJiri Benc 
35773651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
35873651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
35973651ee6SJohannes Berg 
36073651ee6SJohannes Berg 	return 0;
36193e5deb1SJohannes Berg  out_free:
36293e5deb1SJohannes Berg 	BUG_ON(!err);
36393e5deb1SJohannes Berg 	__sta_info_free(local, sta);
36493e5deb1SJohannes Berg 	return err;
365f0706e82SJiri Benc }
366f0706e82SJiri Benc 
367004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
368004c872eSJohannes Berg {
369004c872eSJohannes Berg 	/*
370004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
371004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
372004c872eSJohannes Berg 	 */
373004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
374004c872eSJohannes Berg }
375004c872eSJohannes Berg 
376004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
377004c872eSJohannes Berg {
378004c872eSJohannes Berg 	/*
379004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
380004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
381004c872eSJohannes Berg 	 */
382004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
383004c872eSJohannes Berg }
384004c872eSJohannes Berg 
385004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
386004c872eSJohannes Berg 				   struct sta_info *sta)
387004c872eSJohannes Berg {
388004c872eSJohannes Berg 	if (bss)
389004c872eSJohannes Berg 		__bss_tim_set(bss, sta->aid);
390d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
391d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
392004c872eSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1);
393d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
394d0709a65SJohannes Berg 	}
395004c872eSJohannes Berg }
396004c872eSJohannes Berg 
397004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
398004c872eSJohannes Berg {
399d0709a65SJohannes Berg 	unsigned long flags;
400004c872eSJohannes Berg 
401d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
402d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
403d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
404004c872eSJohannes Berg }
405004c872eSJohannes Berg 
406004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
407004c872eSJohannes Berg 				     struct sta_info *sta)
408004c872eSJohannes Berg {
409004c872eSJohannes Berg 	if (bss)
410004c872eSJohannes Berg 		__bss_tim_clear(bss, sta->aid);
411d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
412d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
413004c872eSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0);
414d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
415d0709a65SJohannes Berg 	}
416004c872eSJohannes Berg }
417004c872eSJohannes Berg 
418004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
419004c872eSJohannes Berg {
420d0709a65SJohannes Berg 	unsigned long flags;
421004c872eSJohannes Berg 
422d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
423d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
424d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
425004c872eSJohannes Berg }
426004c872eSJohannes Berg 
427cb585bccSLuis Carlos Cobo void __sta_info_unlink(struct sta_info **sta)
428d0709a65SJohannes Berg {
429d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
430d0709a65SJohannes Berg 	struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
431d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
432d0709a65SJohannes Berg 	DECLARE_MAC_BUF(mbuf);
433d0709a65SJohannes Berg #endif
434d0709a65SJohannes Berg 	/*
435d0709a65SJohannes Berg 	 * pull caller's reference if we're already gone.
436d0709a65SJohannes Berg 	 */
437d0709a65SJohannes Berg 	if (sta_info_hash_del(local, *sta)) {
438d0709a65SJohannes Berg 		*sta = NULL;
439be8755e1SMichael Wu 		return;
440d0709a65SJohannes Berg 	}
441be8755e1SMichael Wu 
442d0709a65SJohannes Berg 	/*
443d0709a65SJohannes Berg 	 * Also pull caller's reference if the STA is pinned by the
444d0709a65SJohannes Berg 	 * task that is adding the debugfs entries. In that case, we
445d0709a65SJohannes Berg 	 * leave the STA "to be freed".
446d0709a65SJohannes Berg 	 *
447d0709a65SJohannes Berg 	 * The rules are not trivial, but not too complex either:
448d0709a65SJohannes Berg 	 *  (1) pin_status is only modified under the sta_lock
44949ec6fa2SJohannes Berg 	 *  (2) STAs may only be pinned under the RTNL so that
45049ec6fa2SJohannes Berg 	 *	sta_info_flush() is guaranteed to actually destroy
45149ec6fa2SJohannes Berg 	 *	all STAs that are active for a given interface, this
45249ec6fa2SJohannes Berg 	 *	is required for correctness because otherwise we
45349ec6fa2SJohannes Berg 	 *	could notify a driver that an interface is going
45449ec6fa2SJohannes Berg 	 *	away and only after that (!) notify it about a STA
45549ec6fa2SJohannes Berg 	 *	on that interface going away.
45649ec6fa2SJohannes Berg 	 *  (3) sta_info_debugfs_add_work() will set the status
457d0709a65SJohannes Berg 	 *	to PINNED when it found an item that needs a new
458d0709a65SJohannes Berg 	 *	debugfs directory created. In that case, that item
459d0709a65SJohannes Berg 	 *	must not be freed although all *RCU* users are done
460d0709a65SJohannes Berg 	 *	with it. Hence, we tell the caller of _unlink()
461d0709a65SJohannes Berg 	 *	that the item is already gone (as can happen when
462d0709a65SJohannes Berg 	 *	two tasks try to unlink/destroy at the same time)
46349ec6fa2SJohannes Berg 	 *  (4) We set the pin_status to DESTROY here when we
464d0709a65SJohannes Berg 	 *	find such an item.
46549ec6fa2SJohannes Berg 	 *  (5) sta_info_debugfs_add_work() will reset the pin_status
466d0709a65SJohannes Berg 	 *	from PINNED to NORMAL when it is done with the item,
467d0709a65SJohannes Berg 	 *	but will check for DESTROY before resetting it in
468d0709a65SJohannes Berg 	 *	which case it will free the item.
469d0709a65SJohannes Berg 	 */
470d0709a65SJohannes Berg 	if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
471d0709a65SJohannes Berg 		(*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
472d0709a65SJohannes Berg 		*sta = NULL;
473d0709a65SJohannes Berg 		return;
474d0709a65SJohannes Berg 	}
475d0709a65SJohannes Berg 
476d0709a65SJohannes Berg 	list_del(&(*sta)->list);
477d0709a65SJohannes Berg 
478d0709a65SJohannes Berg 	if ((*sta)->flags & WLAN_STA_PS) {
479d0709a65SJohannes Berg 		(*sta)->flags &= ~WLAN_STA_PS;
480f0706e82SJiri Benc 		if (sdata->bss)
481f0706e82SJiri Benc 			atomic_dec(&sdata->bss->num_sta_ps);
482d0709a65SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, *sta);
483f0706e82SJiri Benc 	}
484d0709a65SJohannes Berg 
485f0706e82SJiri Benc 	local->num_sta--;
486ee385855SLuis Carlos Cobo 
48732bfd35dSJohannes Berg 	if (local->ops->sta_notify) {
48851fb61e7SJohannes Berg 		if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
48932bfd35dSJohannes Berg 			sdata = sdata->u.vlan.ap;
49032bfd35dSJohannes Berg 
49132bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
492d0709a65SJohannes Berg 				       STA_NOTIFY_REMOVE, (*sta)->addr);
49332bfd35dSJohannes Berg 	}
494478f8d2bSTomas Winkler 
495d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
496d0709a65SJohannes Berg 		mesh_accept_plinks_update(sdata);
497d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
498d0709a65SJohannes Berg 		del_timer(&(*sta)->plink_timer);
499d0709a65SJohannes Berg #endif
500f0706e82SJiri Benc 	}
501f0706e82SJiri Benc 
502d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
503d0709a65SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %s\n",
504d0709a65SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mbuf, (*sta)->addr));
505d0709a65SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
506d0709a65SJohannes Berg }
507d0709a65SJohannes Berg 
508d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta)
509d0709a65SJohannes Berg {
510d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
511d0709a65SJohannes Berg 	unsigned long flags;
512d0709a65SJohannes Berg 
513d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
514d0709a65SJohannes Berg 	__sta_info_unlink(sta);
515d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
516d0709a65SJohannes Berg }
517f0706e82SJiri Benc 
518f0706e82SJiri Benc static inline int sta_info_buffer_expired(struct ieee80211_local *local,
519f0706e82SJiri Benc 					  struct sta_info *sta,
520f0706e82SJiri Benc 					  struct sk_buff *skb)
521f0706e82SJiri Benc {
522f0706e82SJiri Benc 	struct ieee80211_tx_packet_data *pkt_data;
523f0706e82SJiri Benc 	int timeout;
524f0706e82SJiri Benc 
525f0706e82SJiri Benc 	if (!skb)
526f0706e82SJiri Benc 		return 0;
527f0706e82SJiri Benc 
528f0706e82SJiri Benc 	pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
529f0706e82SJiri Benc 
530f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
531f0706e82SJiri Benc 	timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
532f0706e82SJiri Benc 		   15625) * HZ;
533f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
534f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
535f0706e82SJiri Benc 	return time_after(jiffies, pkt_data->jiffies + timeout);
536f0706e82SJiri Benc }
537f0706e82SJiri Benc 
538f0706e82SJiri Benc 
539f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
540f0706e82SJiri Benc 					     struct sta_info *sta)
541f0706e82SJiri Benc {
542f0706e82SJiri Benc 	unsigned long flags;
543f0706e82SJiri Benc 	struct sk_buff *skb;
544836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
5450795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
546f0706e82SJiri Benc 
547f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
548f0706e82SJiri Benc 		return;
549f0706e82SJiri Benc 
550f0706e82SJiri Benc 	for (;;) {
551f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
552f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
553836341a7SJohannes Berg 		if (sta_info_buffer_expired(local, sta, skb))
554f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
555836341a7SJohannes Berg 		else
556f0706e82SJiri Benc 			skb = NULL;
557f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
558f0706e82SJiri Benc 
559836341a7SJohannes Berg 		if (!skb)
560836341a7SJohannes Berg 			break;
561836341a7SJohannes Berg 
562d0709a65SJohannes Berg 		sdata = sta->sdata;
563f0706e82SJiri Benc 		local->total_ps_buffered--;
564f0706e82SJiri Benc 		printk(KERN_DEBUG "Buffered frame expired (STA "
5650795af57SJoe Perches 		       "%s)\n", print_mac(mac, sta->addr));
566f0706e82SJiri Benc 		dev_kfree_skb(skb);
567836341a7SJohannes Berg 
568004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
569004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
570f0706e82SJiri Benc 	}
571f0706e82SJiri Benc }
572f0706e82SJiri Benc 
573f0706e82SJiri Benc 
574f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
575f0706e82SJiri Benc {
576f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
577f0706e82SJiri Benc 	struct sta_info *sta;
578f0706e82SJiri Benc 
579d0709a65SJohannes Berg 	rcu_read_lock();
580d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
581f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
582d0709a65SJohannes Berg 	rcu_read_unlock();
583f0706e82SJiri Benc 
5840d174406SJohannes Berg 	local->sta_cleanup.expires =
5850d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
586f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
587f0706e82SJiri Benc }
588f0706e82SJiri Benc 
589e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
590*4d6141c3SJiri Slaby /*
591*4d6141c3SJiri Slaby  * See comment in __sta_info_unlink,
592*4d6141c3SJiri Slaby  * caller must hold local->sta_lock.
593*4d6141c3SJiri Slaby  */
594*4d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta)
595*4d6141c3SJiri Slaby {
596*4d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
597*4d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_PINNED;
598*4d6141c3SJiri Slaby }
599*4d6141c3SJiri Slaby 
600*4d6141c3SJiri Slaby /*
601*4d6141c3SJiri Slaby  * See comment in __sta_info_unlink, returns sta if it
602*4d6141c3SJiri Slaby  * needs to be destroyed.
603*4d6141c3SJiri Slaby  */
604*4d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta)
605*4d6141c3SJiri Slaby {
606*4d6141c3SJiri Slaby 	struct sta_info *ret = NULL;
607*4d6141c3SJiri Slaby 	unsigned long flags;
608*4d6141c3SJiri Slaby 
609*4d6141c3SJiri Slaby 	spin_lock_irqsave(&sta->local->sta_lock, flags);
610*4d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
611*4d6141c3SJiri Slaby 		sta->pin_status != STA_INFO_PIN_STAT_PINNED);
612*4d6141c3SJiri Slaby 	if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
613*4d6141c3SJiri Slaby 		ret = sta;
614*4d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
615*4d6141c3SJiri Slaby 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
616*4d6141c3SJiri Slaby 
617*4d6141c3SJiri Slaby 	return ret;
618*4d6141c3SJiri Slaby }
619*4d6141c3SJiri Slaby 
620d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work)
621e9f207f0SJiri Benc {
622e9f207f0SJiri Benc 	struct ieee80211_local *local =
623e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
624e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
625d0709a65SJohannes Berg 	unsigned long flags;
626e9f207f0SJiri Benc 
62749ec6fa2SJohannes Berg 	/* We need to keep the RTNL across the whole pinned status. */
62849ec6fa2SJohannes Berg 	rtnl_lock();
629e9f207f0SJiri Benc 	while (1) {
630e9f207f0SJiri Benc 		sta = NULL;
631d0709a65SJohannes Berg 
632d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
633e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
634be8755e1SMichael Wu 			if (!tmp->debugfs.dir) {
635e9f207f0SJiri Benc 				sta = tmp;
636d0709a65SJohannes Berg 				__sta_info_pin(sta);
637e9f207f0SJiri Benc 				break;
638e9f207f0SJiri Benc 			}
639e9f207f0SJiri Benc 		}
640d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
641e9f207f0SJiri Benc 
642e9f207f0SJiri Benc 		if (!sta)
643e9f207f0SJiri Benc 			break;
644e9f207f0SJiri Benc 
645e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
646e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
647d0709a65SJohannes Berg 
648d0709a65SJohannes Berg 		sta = __sta_info_unpin(sta);
649d0709a65SJohannes Berg 		sta_info_destroy(sta);
650e9f207f0SJiri Benc 	}
65149ec6fa2SJohannes Berg 	rtnl_unlock();
652e9f207f0SJiri Benc }
653e9f207f0SJiri Benc #endif
654e9f207f0SJiri Benc 
655dc6676b7SJohannes Berg void __ieee80211_run_pending_flush(struct ieee80211_local *local)
656dc6676b7SJohannes Berg {
657dc6676b7SJohannes Berg 	struct sta_info *sta;
658dc6676b7SJohannes Berg 	unsigned long flags;
659dc6676b7SJohannes Berg 
660dc6676b7SJohannes Berg 	ASSERT_RTNL();
661dc6676b7SJohannes Berg 
662dc6676b7SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
663dc6676b7SJohannes Berg 	while (!list_empty(&local->sta_flush_list)) {
664dc6676b7SJohannes Berg 		sta = list_first_entry(&local->sta_flush_list,
665dc6676b7SJohannes Berg 				       struct sta_info, list);
666dc6676b7SJohannes Berg 		list_del(&sta->list);
667dc6676b7SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
668dc6676b7SJohannes Berg 		sta_info_destroy(sta);
669dc6676b7SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
670dc6676b7SJohannes Berg 	}
671dc6676b7SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
672dc6676b7SJohannes Berg }
673dc6676b7SJohannes Berg 
674dc6676b7SJohannes Berg static void ieee80211_sta_flush_work(struct work_struct *work)
675dc6676b7SJohannes Berg {
676dc6676b7SJohannes Berg 	struct ieee80211_local *local =
677dc6676b7SJohannes Berg 		container_of(work, struct ieee80211_local, sta_flush_work);
678dc6676b7SJohannes Berg 
679dc6676b7SJohannes Berg 	rtnl_lock();
680dc6676b7SJohannes Berg 	__ieee80211_run_pending_flush(local);
681dc6676b7SJohannes Berg 	rtnl_unlock();
682dc6676b7SJohannes Berg }
683dc6676b7SJohannes Berg 
684f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
685f0706e82SJiri Benc {
686d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
687f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
688dc6676b7SJohannes Berg 	INIT_LIST_HEAD(&local->sta_flush_list);
689dc6676b7SJohannes Berg 	INIT_WORK(&local->sta_flush_work, ieee80211_sta_flush_work);
690f0706e82SJiri Benc 
691b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
692b24b8a24SPavel Emelyanov 		    (unsigned long)local);
6930d174406SJohannes Berg 	local->sta_cleanup.expires =
6940d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
695e9f207f0SJiri Benc 
696e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
697d0709a65SJohannes Berg 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
698e9f207f0SJiri Benc #endif
699f0706e82SJiri Benc }
700f0706e82SJiri Benc 
701f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
702f0706e82SJiri Benc {
703f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
704f0706e82SJiri Benc 	return 0;
705f0706e82SJiri Benc }
706f0706e82SJiri Benc 
707f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
708f0706e82SJiri Benc {
709f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
710dc6676b7SJohannes Berg 	cancel_work_sync(&local->sta_flush_work);
71149ec6fa2SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS
71249ec6fa2SJohannes Berg 	/*
71349ec6fa2SJohannes Berg 	 * Make sure the debugfs adding work isn't pending after this
71449ec6fa2SJohannes Berg 	 * because we're about to be destroyed. It doesn't matter
71549ec6fa2SJohannes Berg 	 * whether it ran or not since we're going to flush all STAs
71649ec6fa2SJohannes Berg 	 * anyway.
71749ec6fa2SJohannes Berg 	 */
71849ec6fa2SJohannes Berg 	cancel_work_sync(&local->sta_debugfs_add);
71949ec6fa2SJohannes Berg #endif
720dc6676b7SJohannes Berg 
721dc6676b7SJohannes Berg 	rtnl_lock();
722be8755e1SMichael Wu 	sta_info_flush(local, NULL);
723dc6676b7SJohannes Berg 	__ieee80211_run_pending_flush(local);
724dc6676b7SJohannes Berg 	rtnl_unlock();
725f0706e82SJiri Benc }
726f0706e82SJiri Benc 
727f0706e82SJiri Benc /**
728f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
72944213b5eSJohannes Berg  *
73044213b5eSJohannes Berg  * Returns the number of removed STA entries.
73144213b5eSJohannes Berg  *
732f0706e82SJiri Benc  * @local: local interface data
733d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
734f0706e82SJiri Benc  */
73544213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
736d0709a65SJohannes Berg 		    struct ieee80211_sub_if_data *sdata)
737f0706e82SJiri Benc {
738f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
739be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
74044213b5eSJohannes Berg 	int ret = 0;
741d0709a65SJohannes Berg 	unsigned long flags;
742f0706e82SJiri Benc 
743d0709a65SJohannes Berg 	might_sleep();
744dc6676b7SJohannes Berg 	ASSERT_RTNL();
745d0709a65SJohannes Berg 
746d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
747d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
748d0709a65SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
749d0709a65SJohannes Berg 			__sta_info_unlink(&sta);
75044213b5eSJohannes Berg 			if (sta) {
751be8755e1SMichael Wu 				list_add_tail(&sta->list, &tmp_list);
75244213b5eSJohannes Berg 				ret++;
75344213b5eSJohannes Berg 			}
754be8755e1SMichael Wu 		}
755be8755e1SMichael Wu 	}
756d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
757d0709a65SJohannes Berg 
758d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
759d0709a65SJohannes Berg 		sta_info_destroy(sta);
76044213b5eSJohannes Berg 
76144213b5eSJohannes Berg 	return ret;
762f0706e82SJiri Benc }
763dc6676b7SJohannes Berg 
764dc6676b7SJohannes Berg /**
765dc6676b7SJohannes Berg  * sta_info_flush_delayed - flush matching STA entries from the STA table
766dc6676b7SJohannes Berg  *
767dc6676b7SJohannes Berg  * This function unlinks all stations for a given interface and queues
768dc6676b7SJohannes Berg  * them for freeing. Note that the workqueue function scheduled here has
769dc6676b7SJohannes Berg  * to run before any new keys can be added to the system to avoid set_key()
770dc6676b7SJohannes Berg  * callback ordering issues.
771dc6676b7SJohannes Berg  *
772dc6676b7SJohannes Berg  * @sdata: the interface
773dc6676b7SJohannes Berg  */
774dc6676b7SJohannes Berg void sta_info_flush_delayed(struct ieee80211_sub_if_data *sdata)
775dc6676b7SJohannes Berg {
776dc6676b7SJohannes Berg 	struct ieee80211_local *local = sdata->local;
777dc6676b7SJohannes Berg 	struct sta_info *sta, *tmp;
778dc6676b7SJohannes Berg 	unsigned long flags;
779dc6676b7SJohannes Berg 	bool work = false;
780dc6676b7SJohannes Berg 
781dc6676b7SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
782dc6676b7SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
783dc6676b7SJohannes Berg 		if (sdata == sta->sdata) {
784dc6676b7SJohannes Berg 			__sta_info_unlink(&sta);
785dc6676b7SJohannes Berg 			if (sta) {
786dc6676b7SJohannes Berg 				list_add_tail(&sta->list,
787dc6676b7SJohannes Berg 					      &local->sta_flush_list);
788dc6676b7SJohannes Berg 				work = true;
789dc6676b7SJohannes Berg 			}
790dc6676b7SJohannes Berg 		}
791dc6676b7SJohannes Berg 	}
792dc6676b7SJohannes Berg 	if (work)
793dc6676b7SJohannes Berg 		schedule_work(&local->sta_flush_work);
794dc6676b7SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
795dc6676b7SJohannes Berg }
796