xref: /openbmc/linux/net/mac80211/sta_info.c (revision 17741cdc264e4d768167766a252210e201c1519a)
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 
76*17741cdcSJohannes Berg 	s = local->sta_hash[STA_HASH(sta->sta.addr)];
77f0706e82SJiri Benc 	if (!s)
78be8755e1SMichael Wu 		return -ENOENT;
79be8755e1SMichael Wu 	if (s == sta) {
80*17741cdcSJohannes 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 */
9643ba7e95SJohannes Berg static struct sta_info *__sta_info_find(struct ieee80211_local *local,
97*17741cdcSJohannes Berg 					const u8 *addr)
9843ba7e95SJohannes Berg {
9943ba7e95SJohannes Berg 	struct sta_info *sta;
10043ba7e95SJohannes Berg 
101d0709a65SJohannes Berg 	sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
10243ba7e95SJohannes Berg 	while (sta) {
103*17741cdcSJohannes Berg 		if (compare_ether_addr(sta->sta.addr, addr) == 0)
10443ba7e95SJohannes Berg 			break;
105d0709a65SJohannes Berg 		sta = rcu_dereference(sta->hnext);
10643ba7e95SJohannes Berg 	}
10743ba7e95SJohannes Berg 	return sta;
10843ba7e95SJohannes Berg }
10943ba7e95SJohannes Berg 
110f0706e82SJiri Benc struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
111f0706e82SJiri Benc {
112d0709a65SJohannes Berg 	return __sta_info_find(local, addr);
113f0706e82SJiri Benc }
114f0706e82SJiri Benc EXPORT_SYMBOL(sta_info_get);
115f0706e82SJiri Benc 
116ee385855SLuis Carlos Cobo struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
117ee385855SLuis Carlos Cobo 				     struct net_device *dev)
118ee385855SLuis Carlos Cobo {
119ee385855SLuis Carlos Cobo 	struct sta_info *sta;
120ee385855SLuis Carlos Cobo 	int i = 0;
121ee385855SLuis Carlos Cobo 
122d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1232a8ca29aSLuis Carlos Cobo 		if (dev && dev != sta->sdata->dev)
1242a8ca29aSLuis Carlos Cobo 			continue;
125ee385855SLuis Carlos Cobo 		if (i < idx) {
126ee385855SLuis Carlos Cobo 			++i;
127ee385855SLuis Carlos Cobo 			continue;
128ee385855SLuis Carlos Cobo 		}
1292a8ca29aSLuis Carlos Cobo 		return sta;
130ee385855SLuis Carlos Cobo 	}
131ee385855SLuis Carlos Cobo 
132ee385855SLuis Carlos Cobo 	return NULL;
133ee385855SLuis Carlos Cobo }
134f0706e82SJiri Benc 
13593e5deb1SJohannes Berg /**
13693e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
13793e5deb1SJohannes Berg  *
1386ef307bcSRandy Dunlap  * @local: pointer to the global information
13993e5deb1SJohannes Berg  * @sta: STA info to free
14093e5deb1SJohannes Berg  *
14193e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
14293e5deb1SJohannes Berg  * that may happen before sta_info_insert().
14393e5deb1SJohannes Berg  */
14493e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
14593e5deb1SJohannes Berg 			    struct sta_info *sta)
14693e5deb1SJohannes Berg {
14793e5deb1SJohannes Berg 	DECLARE_MAC_BUF(mbuf);
14893e5deb1SJohannes Berg 
14993e5deb1SJohannes Berg 	rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
15093e5deb1SJohannes Berg 	rate_control_put(sta->rate_ctrl);
15193e5deb1SJohannes Berg 
15293e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
15393e5deb1SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %s\n",
154*17741cdcSJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mbuf, sta->sta.addr));
15593e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
15693e5deb1SJohannes Berg 
15793e5deb1SJohannes Berg 	kfree(sta);
15893e5deb1SJohannes Berg }
15993e5deb1SJohannes Berg 
160d0709a65SJohannes Berg void sta_info_destroy(struct sta_info *sta)
161f0706e82SJiri Benc {
16297bff8ecSJohannes Berg 	struct ieee80211_local *local;
163f0706e82SJiri Benc 	struct sk_buff *skb;
16407db2183SRon Rindjunsky 	int i;
16573651ee6SJohannes Berg 
16697bff8ecSJohannes Berg 	might_sleep();
16797bff8ecSJohannes Berg 
16873651ee6SJohannes Berg 	if (!sta)
16973651ee6SJohannes Berg 		return;
170f0706e82SJiri Benc 
17197bff8ecSJohannes Berg 	local = sta->local;
172d0709a65SJohannes Berg 
173d0709a65SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
174d0709a65SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
175d0709a65SJohannes Berg 
176d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
177d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
178d0709a65SJohannes Berg 		mesh_plink_deactivate(sta);
179d0709a65SJohannes Berg #endif
180d0709a65SJohannes Berg 
181d0709a65SJohannes Berg 	/*
1823b96766fSJohannes Berg 	 * We have only unlinked the key, and actually destroying it
1833b96766fSJohannes Berg 	 * may mean it is removed from hardware which requires that
1843b96766fSJohannes Berg 	 * the key->sta pointer is still valid, so flush the key todo
1853b96766fSJohannes Berg 	 * list here.
1863b96766fSJohannes Berg 	 *
1873b96766fSJohannes Berg 	 * ieee80211_key_todo() will synchronize_rcu() so after this
1883b96766fSJohannes Berg 	 * nothing can reference this sta struct any more.
189d0709a65SJohannes Berg 	 */
1903b96766fSJohannes Berg 	ieee80211_key_todo();
191d0709a65SJohannes Berg 
192d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
193d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
194d0709a65SJohannes Berg 		del_timer_sync(&sta->plink_timer);
195d0709a65SJohannes Berg #endif
196d0709a65SJohannes Berg 
197f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
198f0706e82SJiri Benc 		local->total_ps_buffered--;
199f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
200f0706e82SJiri Benc 	}
201d0709a65SJohannes Berg 
202d0709a65SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
203f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
204d0709a65SJohannes Berg 
205fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
20607346f81SJohannes Berg 		spin_lock_bh(&sta->lock);
207cee24a3eSRon Rindjunsky 		if (sta->ampdu_mlme.tid_rx[i])
208cee24a3eSRon Rindjunsky 		  del_timer_sync(&sta->ampdu_mlme.tid_rx[i]->session_timer);
209cee24a3eSRon Rindjunsky 		if (sta->ampdu_mlme.tid_tx[i])
210cee24a3eSRon Rindjunsky 		  del_timer_sync(&sta->ampdu_mlme.tid_tx[i]->addba_resp_timer);
21107346f81SJohannes Berg 		spin_unlock_bh(&sta->lock);
212fe3bf0f5SRon Rindjunsky 	}
213cee24a3eSRon Rindjunsky 
21493e5deb1SJohannes Berg 	__sta_info_free(local, sta);
215f0706e82SJiri Benc }
216f0706e82SJiri Benc 
217f0706e82SJiri Benc 
218d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
219d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
220d0709a65SJohannes Berg 			      struct sta_info *sta)
221f0706e82SJiri Benc {
222*17741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
223*17741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
224f0706e82SJiri Benc }
225f0706e82SJiri Benc 
22673651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
22773651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
228f0706e82SJiri Benc {
229d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
230f0706e82SJiri Benc 	struct sta_info *sta;
23116c5f15cSRon Rindjunsky 	int i;
23273651ee6SJohannes Berg 	DECLARE_MAC_BUF(mbuf);
233f0706e82SJiri Benc 
234*17741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
235f0706e82SJiri Benc 	if (!sta)
23673651ee6SJohannes Berg 		return NULL;
237f0706e82SJiri Benc 
23807346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2395a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
24007346f81SJohannes Berg 
241*17741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
242d0709a65SJohannes Berg 	sta->local = local;
243d0709a65SJohannes Berg 	sta->sdata = sdata;
244f0706e82SJiri Benc 
245f0706e82SJiri Benc 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
246d0709a65SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
24773651ee6SJohannes Berg 						     gfp);
248f0706e82SJiri Benc 	if (!sta->rate_ctrl_priv) {
249f0706e82SJiri Benc 		rate_control_put(sta->rate_ctrl);
250f0706e82SJiri Benc 		kfree(sta);
25173651ee6SJohannes Berg 		return NULL;
252f0706e82SJiri Benc 	}
253f0706e82SJiri Benc 
25416c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
25516c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
25616c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
25716c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
25816c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
259fe3bf0f5SRon Rindjunsky 		/* tid to tx queue: initialize according to HW (0 is valid) */
260e2530083SJohannes Berg 		sta->tid_to_tx_q[i] = ieee80211_num_queues(&local->hw);
261cee24a3eSRon Rindjunsky 		/* rx */
262cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
263cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
264cee24a3eSRon Rindjunsky 		/* tx */
265cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
266cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
267cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
26816c5f15cSRon Rindjunsky 	}
269f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
270f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
27173651ee6SJohannes Berg 
27273651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
27373651ee6SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %s\n",
274*17741cdcSJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mbuf, sta->sta.addr));
27573651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
27673651ee6SJohannes Berg 
27703e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
278b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
27903e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
28003e4497eSJohannes Berg #endif
28103e4497eSJohannes Berg 
28273651ee6SJohannes Berg 	return sta;
28373651ee6SJohannes Berg }
28473651ee6SJohannes Berg 
28573651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta)
28673651ee6SJohannes Berg {
28773651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
28873651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
28973651ee6SJohannes Berg 	unsigned long flags;
29093e5deb1SJohannes Berg 	int err = 0;
29173651ee6SJohannes Berg 	DECLARE_MAC_BUF(mac);
29273651ee6SJohannes Berg 
29303e4497eSJohannes Berg 	/*
29403e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
29503e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
29603e4497eSJohannes Berg 	 * and another CPU turns off the net device.
29703e4497eSJohannes Berg 	 */
29893e5deb1SJohannes Berg 	if (unlikely(!netif_running(sdata->dev))) {
29993e5deb1SJohannes Berg 		err = -ENETDOWN;
30093e5deb1SJohannes Berg 		goto out_free;
30193e5deb1SJohannes Berg 	}
30203e4497eSJohannes Berg 
303*17741cdcSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
304*17741cdcSJohannes Berg 	            is_multicast_ether_addr(sta->sta.addr))) {
30593e5deb1SJohannes Berg 		err = -EINVAL;
30693e5deb1SJohannes Berg 		goto out_free;
30793e5deb1SJohannes Berg 	}
30844213b5eSJohannes Berg 
309d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
31043ba7e95SJohannes Berg 	/* check if STA exists already */
311*17741cdcSJohannes Berg 	if (__sta_info_find(local, sta->sta.addr)) {
312d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
31393e5deb1SJohannes Berg 		err = -EEXIST;
31493e5deb1SJohannes Berg 		goto out_free;
31543ba7e95SJohannes Berg 	}
316f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
317f0706e82SJiri Benc 	local->num_sta++;
318f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
31932bfd35dSJohannes Berg 
320d0709a65SJohannes Berg 	/* notify driver */
321d0709a65SJohannes Berg 	if (local->ops->sta_notify) {
32205c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3233e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
3243e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
3253e122be0SJohannes Berg 					     u.ap);
32632bfd35dSJohannes Berg 
32732bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
328*17741cdcSJohannes Berg 				       STA_NOTIFY_ADD, &sta->sta);
32932bfd35dSJohannes Berg 	}
330d0709a65SJohannes Berg 
331f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
33273651ee6SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %s\n",
333*17741cdcSJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mac, sta->sta.addr));
334f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
335f0706e82SJiri Benc 
33673651ee6SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
33773651ee6SJohannes Berg 
338e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
33993e5deb1SJohannes Berg 	/*
34093e5deb1SJohannes Berg 	 * Debugfs entry adding might sleep, so schedule process
341e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
34293e5deb1SJohannes Berg 	 * have one.
34393e5deb1SJohannes Berg 	 * NOTE: due to auto-freeing semantics this may only be done
34493e5deb1SJohannes Berg 	 *       if the insertion is successful!
34593e5deb1SJohannes Berg 	 */
34649ec6fa2SJohannes Berg 	schedule_work(&local->sta_debugfs_add);
347e9f207f0SJiri Benc #endif
348e9f207f0SJiri Benc 
34973651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
35073651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
35173651ee6SJohannes Berg 
35273651ee6SJohannes Berg 	return 0;
35393e5deb1SJohannes Berg  out_free:
35493e5deb1SJohannes Berg 	BUG_ON(!err);
35593e5deb1SJohannes Berg 	__sta_info_free(local, sta);
35693e5deb1SJohannes Berg 	return err;
357f0706e82SJiri Benc }
358f0706e82SJiri Benc 
359004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
360004c872eSJohannes Berg {
361004c872eSJohannes Berg 	/*
362004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
363004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
364004c872eSJohannes Berg 	 */
365004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
366004c872eSJohannes Berg }
367004c872eSJohannes Berg 
368004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
369004c872eSJohannes Berg {
370004c872eSJohannes Berg 	/*
371004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
372004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
373004c872eSJohannes Berg 	 */
374004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
375004c872eSJohannes Berg }
376004c872eSJohannes Berg 
377004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
378004c872eSJohannes Berg 				   struct sta_info *sta)
379004c872eSJohannes Berg {
3803e122be0SJohannes Berg 	BUG_ON(!bss);
3813e122be0SJohannes Berg 
382*17741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
3833e122be0SJohannes Berg 
384d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
385d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
386*17741cdcSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local),
387*17741cdcSJohannes Berg 					 &sta->sta, true);
388d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
389d0709a65SJohannes Berg 	}
390004c872eSJohannes Berg }
391004c872eSJohannes Berg 
392004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
393004c872eSJohannes Berg {
394d0709a65SJohannes Berg 	unsigned long flags;
395004c872eSJohannes Berg 
3963e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
3973e122be0SJohannes Berg 
398d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
399d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
400d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
401004c872eSJohannes Berg }
402004c872eSJohannes Berg 
403004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
404004c872eSJohannes Berg 				     struct sta_info *sta)
405004c872eSJohannes Berg {
4063e122be0SJohannes Berg 	BUG_ON(!bss);
4073e122be0SJohannes Berg 
408*17741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
4093e122be0SJohannes Berg 
410d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
411d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
412*17741cdcSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local),
413*17741cdcSJohannes Berg 					 &sta->sta, false);
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 
4223e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4233e122be0SJohannes Berg 
424d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
425d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
426d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
427004c872eSJohannes Berg }
428004c872eSJohannes Berg 
42924723d1bSJohannes Berg static void __sta_info_unlink(struct sta_info **sta)
430d0709a65SJohannes Berg {
431d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
432d0709a65SJohannes Berg 	struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
433d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
434d0709a65SJohannes Berg 	DECLARE_MAC_BUF(mbuf);
435d0709a65SJohannes Berg #endif
436d0709a65SJohannes Berg 	/*
437d0709a65SJohannes Berg 	 * pull caller's reference if we're already gone.
438d0709a65SJohannes Berg 	 */
439d0709a65SJohannes Berg 	if (sta_info_hash_del(local, *sta)) {
440d0709a65SJohannes Berg 		*sta = NULL;
441be8755e1SMichael Wu 		return;
442d0709a65SJohannes Berg 	}
443be8755e1SMichael Wu 
4443b96766fSJohannes Berg 	if ((*sta)->key) {
4453b96766fSJohannes Berg 		ieee80211_key_free((*sta)->key);
4463b96766fSJohannes Berg 		WARN_ON((*sta)->key);
4473b96766fSJohannes Berg 	}
4483b96766fSJohannes Berg 
4497d1559f1SJohannes Berg 	list_del(&(*sta)->list);
4507d1559f1SJohannes Berg 
45107346f81SJohannes Berg 	if (test_and_clear_sta_flags(*sta, WLAN_STA_PS)) {
4523e122be0SJohannes Berg 		BUG_ON(!sdata->bss);
4533e122be0SJohannes Berg 
4547d1559f1SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
4557d1559f1SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, *sta);
4567d1559f1SJohannes Berg 	}
4577d1559f1SJohannes Berg 
4587d1559f1SJohannes Berg 	local->num_sta--;
4597d1559f1SJohannes Berg 
4607d1559f1SJohannes Berg 	if (local->ops->sta_notify) {
46105c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4623e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
4633e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
4643e122be0SJohannes Berg 					     u.ap);
4657d1559f1SJohannes Berg 
4667d1559f1SJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
467*17741cdcSJohannes Berg 				       STA_NOTIFY_REMOVE, &(*sta)->sta);
4687d1559f1SJohannes Berg 	}
4697d1559f1SJohannes Berg 
4707d1559f1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
4717d1559f1SJohannes Berg 		mesh_accept_plinks_update(sdata);
4727d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
4737d1559f1SJohannes Berg 		del_timer(&(*sta)->plink_timer);
4747d1559f1SJohannes Berg #endif
4757d1559f1SJohannes Berg 	}
4767d1559f1SJohannes Berg 
4777d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4787d1559f1SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %s\n",
479*17741cdcSJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mbuf, (*sta)->sta.addr));
4807d1559f1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
4817d1559f1SJohannes Berg 
482d0709a65SJohannes Berg 	/*
4837d1559f1SJohannes Berg 	 * Finally, pull caller's reference if the STA is pinned by the
484d0709a65SJohannes Berg 	 * task that is adding the debugfs entries. In that case, we
485d0709a65SJohannes Berg 	 * leave the STA "to be freed".
486d0709a65SJohannes Berg 	 *
487d0709a65SJohannes Berg 	 * The rules are not trivial, but not too complex either:
488d0709a65SJohannes Berg 	 *  (1) pin_status is only modified under the sta_lock
48949ec6fa2SJohannes Berg 	 *  (2) STAs may only be pinned under the RTNL so that
49049ec6fa2SJohannes Berg 	 *	sta_info_flush() is guaranteed to actually destroy
49149ec6fa2SJohannes Berg 	 *	all STAs that are active for a given interface, this
49249ec6fa2SJohannes Berg 	 *	is required for correctness because otherwise we
49349ec6fa2SJohannes Berg 	 *	could notify a driver that an interface is going
49449ec6fa2SJohannes Berg 	 *	away and only after that (!) notify it about a STA
49549ec6fa2SJohannes Berg 	 *	on that interface going away.
49649ec6fa2SJohannes Berg 	 *  (3) sta_info_debugfs_add_work() will set the status
497d0709a65SJohannes Berg 	 *	to PINNED when it found an item that needs a new
498d0709a65SJohannes Berg 	 *	debugfs directory created. In that case, that item
499d0709a65SJohannes Berg 	 *	must not be freed although all *RCU* users are done
500d0709a65SJohannes Berg 	 *	with it. Hence, we tell the caller of _unlink()
501d0709a65SJohannes Berg 	 *	that the item is already gone (as can happen when
502d0709a65SJohannes Berg 	 *	two tasks try to unlink/destroy at the same time)
50349ec6fa2SJohannes Berg 	 *  (4) We set the pin_status to DESTROY here when we
504d0709a65SJohannes Berg 	 *	find such an item.
50549ec6fa2SJohannes Berg 	 *  (5) sta_info_debugfs_add_work() will reset the pin_status
506d0709a65SJohannes Berg 	 *	from PINNED to NORMAL when it is done with the item,
507d0709a65SJohannes Berg 	 *	but will check for DESTROY before resetting it in
508d0709a65SJohannes Berg 	 *	which case it will free the item.
509d0709a65SJohannes Berg 	 */
510d0709a65SJohannes Berg 	if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
511d0709a65SJohannes Berg 		(*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
512d0709a65SJohannes Berg 		*sta = NULL;
513d0709a65SJohannes Berg 		return;
514d0709a65SJohannes Berg 	}
515d0709a65SJohannes Berg }
516d0709a65SJohannes Berg 
517d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta)
518d0709a65SJohannes Berg {
519d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
520d0709a65SJohannes Berg 	unsigned long flags;
521d0709a65SJohannes Berg 
522d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
523d0709a65SJohannes Berg 	__sta_info_unlink(sta);
524d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
525d0709a65SJohannes Berg }
526f0706e82SJiri Benc 
527f0706e82SJiri Benc static inline int sta_info_buffer_expired(struct ieee80211_local *local,
528f0706e82SJiri Benc 					  struct sta_info *sta,
529f0706e82SJiri Benc 					  struct sk_buff *skb)
530f0706e82SJiri Benc {
531e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
532f0706e82SJiri Benc 	int timeout;
533f0706e82SJiri Benc 
534f0706e82SJiri Benc 	if (!skb)
535f0706e82SJiri Benc 		return 0;
536f0706e82SJiri Benc 
537e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
538f0706e82SJiri Benc 
539f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
540f0706e82SJiri Benc 	timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
541f0706e82SJiri Benc 		   15625) * HZ;
542f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
543f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
544e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
545f0706e82SJiri Benc }
546f0706e82SJiri Benc 
547f0706e82SJiri Benc 
548f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
549f0706e82SJiri Benc 					     struct sta_info *sta)
550f0706e82SJiri Benc {
551f0706e82SJiri Benc 	unsigned long flags;
552f0706e82SJiri Benc 	struct sk_buff *skb;
553836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
5540795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
555f0706e82SJiri Benc 
556f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
557f0706e82SJiri Benc 		return;
558f0706e82SJiri Benc 
559f0706e82SJiri Benc 	for (;;) {
560f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
561f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
562836341a7SJohannes Berg 		if (sta_info_buffer_expired(local, sta, skb))
563f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
564836341a7SJohannes Berg 		else
565f0706e82SJiri Benc 			skb = NULL;
566f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
567f0706e82SJiri Benc 
568836341a7SJohannes Berg 		if (!skb)
569836341a7SJohannes Berg 			break;
570836341a7SJohannes Berg 
571d0709a65SJohannes Berg 		sdata = sta->sdata;
572f0706e82SJiri Benc 		local->total_ps_buffered--;
573f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
574f0706e82SJiri Benc 		printk(KERN_DEBUG "Buffered frame expired (STA "
575*17741cdcSJohannes Berg 		       "%s)\n", print_mac(mac, sta->sta.addr));
576f4ea83ddSJohannes Berg #endif
577f0706e82SJiri Benc 		dev_kfree_skb(skb);
578836341a7SJohannes Berg 
579004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
580004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
581f0706e82SJiri Benc 	}
582f0706e82SJiri Benc }
583f0706e82SJiri Benc 
584f0706e82SJiri Benc 
585f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
586f0706e82SJiri Benc {
587f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
588f0706e82SJiri Benc 	struct sta_info *sta;
589f0706e82SJiri Benc 
590d0709a65SJohannes Berg 	rcu_read_lock();
591d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
592f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
593d0709a65SJohannes Berg 	rcu_read_unlock();
594f0706e82SJiri Benc 
5950d174406SJohannes Berg 	local->sta_cleanup.expires =
5960d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
597f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
598f0706e82SJiri Benc }
599f0706e82SJiri Benc 
600e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
6014d6141c3SJiri Slaby /*
6024d6141c3SJiri Slaby  * See comment in __sta_info_unlink,
6034d6141c3SJiri Slaby  * caller must hold local->sta_lock.
6044d6141c3SJiri Slaby  */
6054d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta)
6064d6141c3SJiri Slaby {
6074d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
6084d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_PINNED;
6094d6141c3SJiri Slaby }
6104d6141c3SJiri Slaby 
6114d6141c3SJiri Slaby /*
6124d6141c3SJiri Slaby  * See comment in __sta_info_unlink, returns sta if it
6134d6141c3SJiri Slaby  * needs to be destroyed.
6144d6141c3SJiri Slaby  */
6154d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta)
6164d6141c3SJiri Slaby {
6174d6141c3SJiri Slaby 	struct sta_info *ret = NULL;
6184d6141c3SJiri Slaby 	unsigned long flags;
6194d6141c3SJiri Slaby 
6204d6141c3SJiri Slaby 	spin_lock_irqsave(&sta->local->sta_lock, flags);
6214d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
6224d6141c3SJiri Slaby 		sta->pin_status != STA_INFO_PIN_STAT_PINNED);
6234d6141c3SJiri Slaby 	if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
6244d6141c3SJiri Slaby 		ret = sta;
6254d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
6264d6141c3SJiri Slaby 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
6274d6141c3SJiri Slaby 
6284d6141c3SJiri Slaby 	return ret;
6294d6141c3SJiri Slaby }
6304d6141c3SJiri Slaby 
631d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work)
632e9f207f0SJiri Benc {
633e9f207f0SJiri Benc 	struct ieee80211_local *local =
634e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
635e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
636d0709a65SJohannes Berg 	unsigned long flags;
637e9f207f0SJiri Benc 
63849ec6fa2SJohannes Berg 	/* We need to keep the RTNL across the whole pinned status. */
63949ec6fa2SJohannes Berg 	rtnl_lock();
640e9f207f0SJiri Benc 	while (1) {
641e9f207f0SJiri Benc 		sta = NULL;
642d0709a65SJohannes Berg 
643d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
644e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
645be8755e1SMichael Wu 			if (!tmp->debugfs.dir) {
646e9f207f0SJiri Benc 				sta = tmp;
647d0709a65SJohannes Berg 				__sta_info_pin(sta);
648e9f207f0SJiri Benc 				break;
649e9f207f0SJiri Benc 			}
650e9f207f0SJiri Benc 		}
651d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
652e9f207f0SJiri Benc 
653e9f207f0SJiri Benc 		if (!sta)
654e9f207f0SJiri Benc 			break;
655e9f207f0SJiri Benc 
656e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
657e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
658d0709a65SJohannes Berg 
659d0709a65SJohannes Berg 		sta = __sta_info_unpin(sta);
660d0709a65SJohannes Berg 		sta_info_destroy(sta);
661e9f207f0SJiri Benc 	}
66249ec6fa2SJohannes Berg 	rtnl_unlock();
663e9f207f0SJiri Benc }
664e9f207f0SJiri Benc #endif
665e9f207f0SJiri Benc 
6663b96766fSJohannes Berg static void __ieee80211_run_pending_flush(struct ieee80211_local *local)
667dc6676b7SJohannes Berg {
668dc6676b7SJohannes Berg 	struct sta_info *sta;
669dc6676b7SJohannes Berg 	unsigned long flags;
670dc6676b7SJohannes Berg 
671dc6676b7SJohannes Berg 	ASSERT_RTNL();
672dc6676b7SJohannes Berg 
673dc6676b7SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
674dc6676b7SJohannes Berg 	while (!list_empty(&local->sta_flush_list)) {
675dc6676b7SJohannes Berg 		sta = list_first_entry(&local->sta_flush_list,
676dc6676b7SJohannes Berg 				       struct sta_info, list);
677dc6676b7SJohannes Berg 		list_del(&sta->list);
678dc6676b7SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
679dc6676b7SJohannes Berg 		sta_info_destroy(sta);
680dc6676b7SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
681dc6676b7SJohannes Berg 	}
682dc6676b7SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
683dc6676b7SJohannes Berg }
684dc6676b7SJohannes Berg 
685dc6676b7SJohannes Berg static void ieee80211_sta_flush_work(struct work_struct *work)
686dc6676b7SJohannes Berg {
687dc6676b7SJohannes Berg 	struct ieee80211_local *local =
688dc6676b7SJohannes Berg 		container_of(work, struct ieee80211_local, sta_flush_work);
689dc6676b7SJohannes Berg 
690dc6676b7SJohannes Berg 	rtnl_lock();
691dc6676b7SJohannes Berg 	__ieee80211_run_pending_flush(local);
692dc6676b7SJohannes Berg 	rtnl_unlock();
693dc6676b7SJohannes Berg }
694dc6676b7SJohannes Berg 
695f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
696f0706e82SJiri Benc {
697d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
698f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
699dc6676b7SJohannes Berg 	INIT_LIST_HEAD(&local->sta_flush_list);
700dc6676b7SJohannes Berg 	INIT_WORK(&local->sta_flush_work, ieee80211_sta_flush_work);
701f0706e82SJiri Benc 
702b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
703b24b8a24SPavel Emelyanov 		    (unsigned long)local);
7040d174406SJohannes Berg 	local->sta_cleanup.expires =
7050d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
706e9f207f0SJiri Benc 
707e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
708d0709a65SJohannes Berg 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
709e9f207f0SJiri Benc #endif
710f0706e82SJiri Benc }
711f0706e82SJiri Benc 
712f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
713f0706e82SJiri Benc {
714f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
715f0706e82SJiri Benc 	return 0;
716f0706e82SJiri Benc }
717f0706e82SJiri Benc 
718f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
719f0706e82SJiri Benc {
720f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
721dc6676b7SJohannes Berg 	cancel_work_sync(&local->sta_flush_work);
72249ec6fa2SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS
72349ec6fa2SJohannes Berg 	/*
72449ec6fa2SJohannes Berg 	 * Make sure the debugfs adding work isn't pending after this
72549ec6fa2SJohannes Berg 	 * because we're about to be destroyed. It doesn't matter
72649ec6fa2SJohannes Berg 	 * whether it ran or not since we're going to flush all STAs
72749ec6fa2SJohannes Berg 	 * anyway.
72849ec6fa2SJohannes Berg 	 */
72949ec6fa2SJohannes Berg 	cancel_work_sync(&local->sta_debugfs_add);
73049ec6fa2SJohannes Berg #endif
731dc6676b7SJohannes Berg 
732dc6676b7SJohannes Berg 	rtnl_lock();
733be8755e1SMichael Wu 	sta_info_flush(local, NULL);
734dc6676b7SJohannes Berg 	__ieee80211_run_pending_flush(local);
735dc6676b7SJohannes Berg 	rtnl_unlock();
736f0706e82SJiri Benc }
737f0706e82SJiri Benc 
738f0706e82SJiri Benc /**
739f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
74044213b5eSJohannes Berg  *
74144213b5eSJohannes Berg  * Returns the number of removed STA entries.
74244213b5eSJohannes Berg  *
743f0706e82SJiri Benc  * @local: local interface data
744d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
745f0706e82SJiri Benc  */
74644213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
747d0709a65SJohannes Berg 		    struct ieee80211_sub_if_data *sdata)
748f0706e82SJiri Benc {
749f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
750be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
75144213b5eSJohannes Berg 	int ret = 0;
752d0709a65SJohannes Berg 	unsigned long flags;
753f0706e82SJiri Benc 
754d0709a65SJohannes Berg 	might_sleep();
755dc6676b7SJohannes Berg 	ASSERT_RTNL();
756d0709a65SJohannes Berg 
757d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
758d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
759d0709a65SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
760d0709a65SJohannes Berg 			__sta_info_unlink(&sta);
76144213b5eSJohannes Berg 			if (sta) {
762be8755e1SMichael Wu 				list_add_tail(&sta->list, &tmp_list);
76344213b5eSJohannes Berg 				ret++;
76444213b5eSJohannes Berg 			}
765be8755e1SMichael Wu 		}
766be8755e1SMichael Wu 	}
767d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
768d0709a65SJohannes Berg 
769d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
770d0709a65SJohannes Berg 		sta_info_destroy(sta);
77144213b5eSJohannes Berg 
77244213b5eSJohannes Berg 	return ret;
773f0706e82SJiri Benc }
774dc6676b7SJohannes Berg 
775dc6676b7SJohannes Berg /**
776dc6676b7SJohannes Berg  * sta_info_flush_delayed - flush matching STA entries from the STA table
777dc6676b7SJohannes Berg  *
778dc6676b7SJohannes Berg  * This function unlinks all stations for a given interface and queues
779dc6676b7SJohannes Berg  * them for freeing. Note that the workqueue function scheduled here has
780dc6676b7SJohannes Berg  * to run before any new keys can be added to the system to avoid set_key()
781dc6676b7SJohannes Berg  * callback ordering issues.
782dc6676b7SJohannes Berg  *
783dc6676b7SJohannes Berg  * @sdata: the interface
784dc6676b7SJohannes Berg  */
785dc6676b7SJohannes Berg void sta_info_flush_delayed(struct ieee80211_sub_if_data *sdata)
786dc6676b7SJohannes Berg {
787dc6676b7SJohannes Berg 	struct ieee80211_local *local = sdata->local;
788dc6676b7SJohannes Berg 	struct sta_info *sta, *tmp;
789dc6676b7SJohannes Berg 	unsigned long flags;
790dc6676b7SJohannes Berg 	bool work = false;
791dc6676b7SJohannes Berg 
792dc6676b7SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
793dc6676b7SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
794dc6676b7SJohannes Berg 		if (sdata == sta->sdata) {
795dc6676b7SJohannes Berg 			__sta_info_unlink(&sta);
796dc6676b7SJohannes Berg 			if (sta) {
797dc6676b7SJohannes Berg 				list_add_tail(&sta->list,
798dc6676b7SJohannes Berg 					      &local->sta_flush_list);
799dc6676b7SJohannes Berg 				work = true;
800dc6676b7SJohannes Berg 			}
801dc6676b7SJohannes Berg 		}
802dc6676b7SJohannes Berg 	}
803dc6676b7SJohannes Berg 	if (work)
804dc6676b7SJohannes Berg 		schedule_work(&local->sta_flush_work);
805dc6676b7SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
806dc6676b7SJohannes Berg }
80724723d1bSJohannes Berg 
80824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
80924723d1bSJohannes Berg 			  unsigned long exp_time)
81024723d1bSJohannes Berg {
81124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
81224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
81324723d1bSJohannes Berg 	LIST_HEAD(tmp_list);
81424723d1bSJohannes Berg 	DECLARE_MAC_BUF(mac);
81524723d1bSJohannes Berg 	unsigned long flags;
81624723d1bSJohannes Berg 
81724723d1bSJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
81824723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
81924723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
82024723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
82124723d1bSJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
822*17741cdcSJohannes Berg 			       sdata->dev->name, print_mac(mac, sta->sta.addr));
82324723d1bSJohannes Berg #endif
82424723d1bSJohannes Berg 			__sta_info_unlink(&sta);
82524723d1bSJohannes Berg 			if (sta)
82624723d1bSJohannes Berg 				list_add(&sta->list, &tmp_list);
82724723d1bSJohannes Berg 		}
82824723d1bSJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
82924723d1bSJohannes Berg 
83024723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
83124723d1bSJohannes Berg 		sta_info_destroy(sta);
83224723d1bSJohannes Berg }
833*17741cdcSJohannes Berg 
834*17741cdcSJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_hw *hw,
835*17741cdcSJohannes Berg                                          const u8 *addr)
836*17741cdcSJohannes Berg {
837*17741cdcSJohannes Berg 	struct sta_info *sta = __sta_info_find(hw_to_local(hw), addr);
838*17741cdcSJohannes Berg 
839*17741cdcSJohannes Berg 	if (!sta)
840*17741cdcSJohannes Berg 		return NULL;
841*17741cdcSJohannes Berg 	return &sta->sta;
842*17741cdcSJohannes Berg }
843*17741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
844