xref: /openbmc/linux/net/mac80211/sta_info.c (revision 2448798133d747ad339e57099e32a1d1e68aca1c)
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"
22*24487981SJohannes Berg #include "driver-ops.h"
232c8dccc7SJohannes Berg #include "rate.h"
24f0706e82SJiri Benc #include "sta_info.h"
25e9f207f0SJiri Benc #include "debugfs_sta.h"
26ee385855SLuis Carlos Cobo #include "mesh.h"
27f0706e82SJiri Benc 
28d0709a65SJohannes Berg /**
29d0709a65SJohannes Berg  * DOC: STA information lifetime rules
30d0709a65SJohannes Berg  *
31d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
32d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
33d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
34d0709a65SJohannes Berg  *
3503e4497eSJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
3603e4497eSJohannes Berg  * that structure. It must then either destroy it using sta_info_destroy()
3703e4497eSJohannes Berg  * (which is pretty useless) or insert it into the hash table using
3803e4497eSJohannes Berg  * sta_info_insert() which demotes the reference from ownership to a regular
3903e4497eSJohannes Berg  * RCU-protected reference; if the function is called without protection by an
4093e5deb1SJohannes Berg  * RCU critical section the reference is instantly invalidated. Note that the
4193e5deb1SJohannes Berg  * caller may not do much with the STA info before inserting it, in particular,
4293e5deb1SJohannes Berg  * it may not start any mesh peer link management or add encryption keys.
4393e5deb1SJohannes Berg  *
4493e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4593e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
46d0709a65SJohannes Berg  *
47d0709a65SJohannes Berg  * Because there are debugfs entries for each station, and adding those
48d0709a65SJohannes Berg  * must be able to sleep, it is also possible to "pin" a station entry,
49d0709a65SJohannes Berg  * that means it can be removed from the hash table but not be freed.
5093e5deb1SJohannes Berg  * See the comment in __sta_info_unlink() for more information, this is
5193e5deb1SJohannes Berg  * an internal capability only.
52d0709a65SJohannes Berg  *
53d0709a65SJohannes Berg  * In order to remove a STA info structure, the caller needs to first
54dbbea671SJohannes Berg  * unlink it (sta_info_unlink()) from the list and hash tables and
553b96766fSJohannes Berg  * then destroy it; sta_info_destroy() will wait for an RCU grace period
563b96766fSJohannes Berg  * to elapse before actually freeing it. Due to the pinning and the
573b96766fSJohannes Berg  * possibility of multiple callers trying to remove the same STA info at
583b96766fSJohannes Berg  * the same time, sta_info_unlink() can clear the STA info pointer it is
593b96766fSJohannes Berg  * passed to indicate that the STA info is owned 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
633b96766fSJohannes Berg  * a call to sta_info_destroy().
64d0709a65SJohannes Berg  *
65d0709a65SJohannes Berg  * In all other cases, there is no concept of ownership on a STA entry,
66d0709a65SJohannes Berg  * each structure is owned by the global hash table/list until it is
67d0709a65SJohannes Berg  * removed. All users of the structure need to be RCU protected so that
68d0709a65SJohannes Berg  * the structure won't be freed before they are done using it.
69d0709a65SJohannes Berg  */
70f0706e82SJiri Benc 
71f0706e82SJiri Benc /* Caller must hold local->sta_lock */
72be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
73f0706e82SJiri Benc 			     struct sta_info *sta)
74f0706e82SJiri Benc {
75f0706e82SJiri Benc 	struct sta_info *s;
76f0706e82SJiri Benc 
7717741cdcSJohannes Berg 	s = local->sta_hash[STA_HASH(sta->sta.addr)];
78f0706e82SJiri Benc 	if (!s)
79be8755e1SMichael Wu 		return -ENOENT;
80be8755e1SMichael Wu 	if (s == sta) {
8117741cdcSJohannes Berg 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
82d0709a65SJohannes Berg 				   s->hnext);
83be8755e1SMichael Wu 		return 0;
84f0706e82SJiri Benc 	}
85f0706e82SJiri Benc 
86be8755e1SMichael Wu 	while (s->hnext && s->hnext != sta)
87f0706e82SJiri Benc 		s = s->hnext;
88be8755e1SMichael Wu 	if (s->hnext) {
89d0709a65SJohannes Berg 		rcu_assign_pointer(s->hnext, sta->hnext);
90be8755e1SMichael Wu 		return 0;
91f0706e82SJiri Benc 	}
92f0706e82SJiri Benc 
93be8755e1SMichael Wu 	return -ENOENT;
94f0706e82SJiri Benc }
95f0706e82SJiri Benc 
96d0709a65SJohannes Berg /* protected by RCU */
974b7679a5SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_local *local, 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) {
1035cf12e8dSShaddy Baddah 		if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
10443ba7e95SJohannes Berg 			break;
105d0709a65SJohannes Berg 		sta = rcu_dereference(sta->hnext);
10643ba7e95SJohannes Berg 	}
10743ba7e95SJohannes Berg 	return sta;
10843ba7e95SJohannes Berg }
10943ba7e95SJohannes Berg 
110ee385855SLuis Carlos Cobo struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
111ee385855SLuis Carlos Cobo 				     struct net_device *dev)
112ee385855SLuis Carlos Cobo {
113ee385855SLuis Carlos Cobo 	struct sta_info *sta;
114ee385855SLuis Carlos Cobo 	int i = 0;
115ee385855SLuis Carlos Cobo 
116d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1172a8ca29aSLuis Carlos Cobo 		if (dev && dev != sta->sdata->dev)
1182a8ca29aSLuis Carlos Cobo 			continue;
119ee385855SLuis Carlos Cobo 		if (i < idx) {
120ee385855SLuis Carlos Cobo 			++i;
121ee385855SLuis Carlos Cobo 			continue;
122ee385855SLuis Carlos Cobo 		}
1232a8ca29aSLuis Carlos Cobo 		return sta;
124ee385855SLuis Carlos Cobo 	}
125ee385855SLuis Carlos Cobo 
126ee385855SLuis Carlos Cobo 	return NULL;
127ee385855SLuis Carlos Cobo }
128f0706e82SJiri Benc 
12993e5deb1SJohannes Berg /**
13093e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
13193e5deb1SJohannes Berg  *
1326ef307bcSRandy Dunlap  * @local: pointer to the global information
13393e5deb1SJohannes Berg  * @sta: STA info to free
13493e5deb1SJohannes Berg  *
13593e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
13693e5deb1SJohannes Berg  * that may happen before sta_info_insert().
13793e5deb1SJohannes Berg  */
13893e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
13993e5deb1SJohannes Berg 			    struct sta_info *sta)
14093e5deb1SJohannes Berg {
1414b7679a5SJohannes Berg 	rate_control_free_sta(sta);
14293e5deb1SJohannes Berg 	rate_control_put(sta->rate_ctrl);
14393e5deb1SJohannes Berg 
14493e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1450c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
1460c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
14793e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
14893e5deb1SJohannes Berg 
14993e5deb1SJohannes Berg 	kfree(sta);
15093e5deb1SJohannes Berg }
15193e5deb1SJohannes Berg 
152d0709a65SJohannes Berg void sta_info_destroy(struct sta_info *sta)
153f0706e82SJiri Benc {
15497bff8ecSJohannes Berg 	struct ieee80211_local *local;
155f0706e82SJiri Benc 	struct sk_buff *skb;
15607db2183SRon Rindjunsky 	int i;
15773651ee6SJohannes Berg 
15897bff8ecSJohannes Berg 	might_sleep();
15997bff8ecSJohannes Berg 
16073651ee6SJohannes Berg 	if (!sta)
16173651ee6SJohannes Berg 		return;
162f0706e82SJiri Benc 
16397bff8ecSJohannes Berg 	local = sta->local;
164d0709a65SJohannes Berg 
165d0709a65SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
166d0709a65SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
167d0709a65SJohannes Berg 
168d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
169d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
170d0709a65SJohannes Berg 		mesh_plink_deactivate(sta);
171d0709a65SJohannes Berg #endif
172d0709a65SJohannes Berg 
173d0709a65SJohannes Berg 	/*
1743b96766fSJohannes Berg 	 * We have only unlinked the key, and actually destroying it
1753b96766fSJohannes Berg 	 * may mean it is removed from hardware which requires that
1763b96766fSJohannes Berg 	 * the key->sta pointer is still valid, so flush the key todo
1773b96766fSJohannes Berg 	 * list here.
1783b96766fSJohannes Berg 	 *
1793b96766fSJohannes Berg 	 * ieee80211_key_todo() will synchronize_rcu() so after this
1803b96766fSJohannes Berg 	 * nothing can reference this sta struct any more.
181d0709a65SJohannes Berg 	 */
1823b96766fSJohannes Berg 	ieee80211_key_todo();
183d0709a65SJohannes Berg 
184d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH
185d0709a65SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif))
186d0709a65SJohannes Berg 		del_timer_sync(&sta->plink_timer);
187d0709a65SJohannes Berg #endif
188d0709a65SJohannes Berg 
189f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
190f0706e82SJiri Benc 		local->total_ps_buffered--;
191f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
192f0706e82SJiri Benc 	}
193d0709a65SJohannes Berg 
194d0709a65SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
195f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
196d0709a65SJohannes Berg 
197fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
19855687e38SJohannes Berg 		struct tid_ampdu_rx *tid_rx;
19955687e38SJohannes Berg 		struct tid_ampdu_tx *tid_tx;
20055687e38SJohannes Berg 
20107346f81SJohannes Berg 		spin_lock_bh(&sta->lock);
20255687e38SJohannes Berg 		tid_rx = sta->ampdu_mlme.tid_rx[i];
20355687e38SJohannes Berg 		/* Make sure timer won't free the tid_rx struct, see below */
20455687e38SJohannes Berg 		if (tid_rx)
20555687e38SJohannes Berg 			tid_rx->shutdown = true;
20696f5e66eSJohannes Berg 
20707346f81SJohannes Berg 		spin_unlock_bh(&sta->lock);
20855687e38SJohannes Berg 
20955687e38SJohannes Berg 		/*
21055687e38SJohannes Berg 		 * Outside spinlock - shutdown is true now so that the timer
21155687e38SJohannes Berg 		 * won't free tid_rx, we have to do that now. Can't let the
21255687e38SJohannes Berg 		 * timer do it because we have to sync the timer outside the
21355687e38SJohannes Berg 		 * lock that it takes itself.
21455687e38SJohannes Berg 		 */
21555687e38SJohannes Berg 		if (tid_rx) {
21655687e38SJohannes Berg 			del_timer_sync(&tid_rx->session_timer);
21755687e38SJohannes Berg 			kfree(tid_rx);
21855687e38SJohannes Berg 		}
21955687e38SJohannes Berg 
22055687e38SJohannes Berg 		/*
22155687e38SJohannes Berg 		 * No need to do such complications for TX agg sessions, the
22255687e38SJohannes Berg 		 * path leading to freeing the tid_tx struct goes via a call
22355687e38SJohannes Berg 		 * from the driver, and thus needs to look up the sta struct
22455687e38SJohannes Berg 		 * again, which cannot be found when we get here. Hence, we
22555687e38SJohannes Berg 		 * just need to delete the timer and free the aggregation
22655687e38SJohannes Berg 		 * info; we won't be telling the peer about it then but that
22755687e38SJohannes Berg 		 * doesn't matter if we're not talking to it again anyway.
22855687e38SJohannes Berg 		 */
22955687e38SJohannes Berg 		tid_tx = sta->ampdu_mlme.tid_tx[i];
23055687e38SJohannes Berg 		if (tid_tx) {
23155687e38SJohannes Berg 			del_timer_sync(&tid_tx->addba_resp_timer);
232cd8ffc80SJohannes Berg 			/*
233cd8ffc80SJohannes Berg 			 * STA removed while aggregation session being
234cd8ffc80SJohannes Berg 			 * started? Bit odd, but purge frames anyway.
235cd8ffc80SJohannes Berg 			 */
236cd8ffc80SJohannes Berg 			skb_queue_purge(&tid_tx->pending);
23755687e38SJohannes Berg 			kfree(tid_tx);
23855687e38SJohannes Berg 		}
239fe3bf0f5SRon Rindjunsky 	}
240cee24a3eSRon Rindjunsky 
24193e5deb1SJohannes Berg 	__sta_info_free(local, sta);
242f0706e82SJiri Benc }
243f0706e82SJiri Benc 
244f0706e82SJiri Benc 
245d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
246d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
247d0709a65SJohannes Berg 			      struct sta_info *sta)
248f0706e82SJiri Benc {
24917741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
25017741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
251f0706e82SJiri Benc }
252f0706e82SJiri Benc 
25373651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
25473651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
255f0706e82SJiri Benc {
256d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
257f0706e82SJiri Benc 	struct sta_info *sta;
25816c5f15cSRon Rindjunsky 	int i;
259f0706e82SJiri Benc 
26017741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
261f0706e82SJiri Benc 	if (!sta)
26273651ee6SJohannes Berg 		return NULL;
263f0706e82SJiri Benc 
26407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2655a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
26607346f81SJohannes Berg 
26717741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
268d0709a65SJohannes Berg 	sta->local = local;
269d0709a65SJohannes Berg 	sta->sdata = sdata;
270f0706e82SJiri Benc 
271f0706e82SJiri Benc 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
272d0709a65SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
2734b7679a5SJohannes Berg 						     &sta->sta, gfp);
274f0706e82SJiri Benc 	if (!sta->rate_ctrl_priv) {
275f0706e82SJiri Benc 		rate_control_put(sta->rate_ctrl);
276f0706e82SJiri Benc 		kfree(sta);
27773651ee6SJohannes Berg 		return NULL;
278f0706e82SJiri Benc 	}
279f0706e82SJiri Benc 
28016c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
28116c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
28216c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
28316c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
28416c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
285cee24a3eSRon Rindjunsky 		/* rx */
286cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
287cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
288cee24a3eSRon Rindjunsky 		/* tx */
289cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
290cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
291cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
29216c5f15cSRon Rindjunsky 	}
293f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
294f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
29573651ee6SJohannes Berg 
29673651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2970c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %pM\n",
2980c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
29973651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
30073651ee6SJohannes Berg 
30103e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
302b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
30303e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
30403e4497eSJohannes Berg #endif
30503e4497eSJohannes Berg 
30673651ee6SJohannes Berg 	return sta;
30773651ee6SJohannes Berg }
30873651ee6SJohannes Berg 
30973651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta)
31073651ee6SJohannes Berg {
31173651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
31273651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
31373651ee6SJohannes Berg 	unsigned long flags;
31493e5deb1SJohannes Berg 	int err = 0;
31573651ee6SJohannes Berg 
31603e4497eSJohannes Berg 	/*
31703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
31803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
31903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
32003e4497eSJohannes Berg 	 */
32193e5deb1SJohannes Berg 	if (unlikely(!netif_running(sdata->dev))) {
32293e5deb1SJohannes Berg 		err = -ENETDOWN;
32393e5deb1SJohannes Berg 		goto out_free;
32493e5deb1SJohannes Berg 	}
32503e4497eSJohannes Berg 
32617741cdcSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
32717741cdcSJohannes Berg 		    is_multicast_ether_addr(sta->sta.addr))) {
32893e5deb1SJohannes Berg 		err = -EINVAL;
32993e5deb1SJohannes Berg 		goto out_free;
33093e5deb1SJohannes Berg 	}
33144213b5eSJohannes Berg 
332d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
33343ba7e95SJohannes Berg 	/* check if STA exists already */
3344b7679a5SJohannes Berg 	if (sta_info_get(local, sta->sta.addr)) {
335d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
33693e5deb1SJohannes Berg 		err = -EEXIST;
33793e5deb1SJohannes Berg 		goto out_free;
33843ba7e95SJohannes Berg 	}
339f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
340f0706e82SJiri Benc 	local->num_sta++;
341f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
34232bfd35dSJohannes Berg 
343d0709a65SJohannes Berg 	/* notify driver */
344d0709a65SJohannes Berg 	if (local->ops->sta_notify) {
34505c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3463e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
3473e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
3483e122be0SJohannes Berg 					     u.ap);
34932bfd35dSJohannes Berg 
350*24487981SJohannes Berg 		drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
35132bfd35dSJohannes Berg 	}
352d0709a65SJohannes Berg 
353f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3540c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %pM\n",
3550c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
356f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
357f0706e82SJiri Benc 
35873651ee6SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
35973651ee6SJohannes Berg 
360e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
36193e5deb1SJohannes Berg 	/*
36293e5deb1SJohannes Berg 	 * Debugfs entry adding might sleep, so schedule process
363e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
36493e5deb1SJohannes Berg 	 * have one.
36593e5deb1SJohannes Berg 	 * NOTE: due to auto-freeing semantics this may only be done
36693e5deb1SJohannes Berg 	 *       if the insertion is successful!
36793e5deb1SJohannes Berg 	 */
36849ec6fa2SJohannes Berg 	schedule_work(&local->sta_debugfs_add);
369e9f207f0SJiri Benc #endif
370e9f207f0SJiri Benc 
37173651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
37273651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
37373651ee6SJohannes Berg 
37473651ee6SJohannes Berg 	return 0;
37593e5deb1SJohannes Berg  out_free:
37693e5deb1SJohannes Berg 	BUG_ON(!err);
37793e5deb1SJohannes Berg 	__sta_info_free(local, sta);
37893e5deb1SJohannes Berg 	return err;
379f0706e82SJiri Benc }
380f0706e82SJiri Benc 
381004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
382004c872eSJohannes Berg {
383004c872eSJohannes Berg 	/*
384004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
385004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
386004c872eSJohannes Berg 	 */
387004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
388004c872eSJohannes Berg }
389004c872eSJohannes Berg 
390004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
391004c872eSJohannes Berg {
392004c872eSJohannes Berg 	/*
393004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
394004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
395004c872eSJohannes Berg 	 */
396004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
397004c872eSJohannes Berg }
398004c872eSJohannes Berg 
399004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
400004c872eSJohannes Berg 				   struct sta_info *sta)
401004c872eSJohannes Berg {
4023e122be0SJohannes Berg 	BUG_ON(!bss);
4033e122be0SJohannes Berg 
40417741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
4053e122be0SJohannes Berg 
406d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
407d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
408*24487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, true);
409d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
410d0709a65SJohannes Berg 	}
411004c872eSJohannes Berg }
412004c872eSJohannes Berg 
413004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
414004c872eSJohannes Berg {
415d0709a65SJohannes Berg 	unsigned long flags;
416004c872eSJohannes Berg 
4173e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4183e122be0SJohannes Berg 
419d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
420d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
421d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
422004c872eSJohannes Berg }
423004c872eSJohannes Berg 
424004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
425004c872eSJohannes Berg 				     struct sta_info *sta)
426004c872eSJohannes Berg {
4273e122be0SJohannes Berg 	BUG_ON(!bss);
4283e122be0SJohannes Berg 
42917741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
4303e122be0SJohannes Berg 
431d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
432d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
433*24487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, false);
434d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
435d0709a65SJohannes Berg 	}
436004c872eSJohannes Berg }
437004c872eSJohannes Berg 
438004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
439004c872eSJohannes Berg {
440d0709a65SJohannes Berg 	unsigned long flags;
441004c872eSJohannes Berg 
4423e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
4433e122be0SJohannes Berg 
444d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
445d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
446d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
447004c872eSJohannes Berg }
448004c872eSJohannes Berg 
44924723d1bSJohannes Berg static void __sta_info_unlink(struct sta_info **sta)
450d0709a65SJohannes Berg {
451d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
452d0709a65SJohannes Berg 	struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
453d0709a65SJohannes Berg 	/*
454d0709a65SJohannes Berg 	 * pull caller's reference if we're already gone.
455d0709a65SJohannes Berg 	 */
456d0709a65SJohannes Berg 	if (sta_info_hash_del(local, *sta)) {
457d0709a65SJohannes Berg 		*sta = NULL;
458be8755e1SMichael Wu 		return;
459d0709a65SJohannes Berg 	}
460be8755e1SMichael Wu 
4613b96766fSJohannes Berg 	if ((*sta)->key) {
4623b96766fSJohannes Berg 		ieee80211_key_free((*sta)->key);
4633b96766fSJohannes Berg 		WARN_ON((*sta)->key);
4643b96766fSJohannes Berg 	}
4653b96766fSJohannes Berg 
4667d1559f1SJohannes Berg 	list_del(&(*sta)->list);
4677d1559f1SJohannes Berg 
46807346f81SJohannes Berg 	if (test_and_clear_sta_flags(*sta, WLAN_STA_PS)) {
4693e122be0SJohannes Berg 		BUG_ON(!sdata->bss);
4703e122be0SJohannes Berg 
4717d1559f1SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
4727d1559f1SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, *sta);
4737d1559f1SJohannes Berg 	}
4747d1559f1SJohannes Berg 
4757d1559f1SJohannes Berg 	local->num_sta--;
4767d1559f1SJohannes Berg 
4777d1559f1SJohannes Berg 	if (local->ops->sta_notify) {
47805c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4793e122be0SJohannes Berg 			sdata = container_of(sdata->bss,
4803e122be0SJohannes Berg 					     struct ieee80211_sub_if_data,
4813e122be0SJohannes Berg 					     u.ap);
4827d1559f1SJohannes Berg 
483*24487981SJohannes Berg 		drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
484*24487981SJohannes Berg 			       &(*sta)->sta);
4857d1559f1SJohannes Berg 	}
4867d1559f1SJohannes Berg 
4877d1559f1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
4887d1559f1SJohannes Berg 		mesh_accept_plinks_update(sdata);
4897d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
4907d1559f1SJohannes Berg 		del_timer(&(*sta)->plink_timer);
4917d1559f1SJohannes Berg #endif
4927d1559f1SJohannes Berg 	}
4937d1559f1SJohannes Berg 
4947d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4950c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %pM\n",
4960c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
4977d1559f1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
4987d1559f1SJohannes Berg 
499d0709a65SJohannes Berg 	/*
5007d1559f1SJohannes Berg 	 * Finally, pull caller's reference if the STA is pinned by the
501d0709a65SJohannes Berg 	 * task that is adding the debugfs entries. In that case, we
502d0709a65SJohannes Berg 	 * leave the STA "to be freed".
503d0709a65SJohannes Berg 	 *
504d0709a65SJohannes Berg 	 * The rules are not trivial, but not too complex either:
505d0709a65SJohannes Berg 	 *  (1) pin_status is only modified under the sta_lock
50649ec6fa2SJohannes Berg 	 *  (2) STAs may only be pinned under the RTNL so that
50749ec6fa2SJohannes Berg 	 *	sta_info_flush() is guaranteed to actually destroy
50849ec6fa2SJohannes Berg 	 *	all STAs that are active for a given interface, this
50949ec6fa2SJohannes Berg 	 *	is required for correctness because otherwise we
51049ec6fa2SJohannes Berg 	 *	could notify a driver that an interface is going
51149ec6fa2SJohannes Berg 	 *	away and only after that (!) notify it about a STA
51249ec6fa2SJohannes Berg 	 *	on that interface going away.
51349ec6fa2SJohannes Berg 	 *  (3) sta_info_debugfs_add_work() will set the status
514d0709a65SJohannes Berg 	 *	to PINNED when it found an item that needs a new
515d0709a65SJohannes Berg 	 *	debugfs directory created. In that case, that item
516d0709a65SJohannes Berg 	 *	must not be freed although all *RCU* users are done
517d0709a65SJohannes Berg 	 *	with it. Hence, we tell the caller of _unlink()
518d0709a65SJohannes Berg 	 *	that the item is already gone (as can happen when
519d0709a65SJohannes Berg 	 *	two tasks try to unlink/destroy at the same time)
52049ec6fa2SJohannes Berg 	 *  (4) We set the pin_status to DESTROY here when we
521d0709a65SJohannes Berg 	 *	find such an item.
52249ec6fa2SJohannes Berg 	 *  (5) sta_info_debugfs_add_work() will reset the pin_status
523d0709a65SJohannes Berg 	 *	from PINNED to NORMAL when it is done with the item,
524d0709a65SJohannes Berg 	 *	but will check for DESTROY before resetting it in
525d0709a65SJohannes Berg 	 *	which case it will free the item.
526d0709a65SJohannes Berg 	 */
527d0709a65SJohannes Berg 	if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
528d0709a65SJohannes Berg 		(*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
529d0709a65SJohannes Berg 		*sta = NULL;
530d0709a65SJohannes Berg 		return;
531d0709a65SJohannes Berg 	}
532d0709a65SJohannes Berg }
533d0709a65SJohannes Berg 
534d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta)
535d0709a65SJohannes Berg {
536d0709a65SJohannes Berg 	struct ieee80211_local *local = (*sta)->local;
537d0709a65SJohannes Berg 	unsigned long flags;
538d0709a65SJohannes Berg 
539d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
540d0709a65SJohannes Berg 	__sta_info_unlink(sta);
541d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
542d0709a65SJohannes Berg }
543f0706e82SJiri Benc 
54457c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta,
545f0706e82SJiri Benc 				   struct sk_buff *skb)
546f0706e82SJiri Benc {
547e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
548f0706e82SJiri Benc 	int timeout;
549f0706e82SJiri Benc 
550f0706e82SJiri Benc 	if (!skb)
551f0706e82SJiri Benc 		return 0;
552f0706e82SJiri Benc 
553e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
554f0706e82SJiri Benc 
555f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
55657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
55757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
55857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
559f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
560f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
561e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
562f0706e82SJiri Benc }
563f0706e82SJiri Benc 
564f0706e82SJiri Benc 
565f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
566f0706e82SJiri Benc 					     struct sta_info *sta)
567f0706e82SJiri Benc {
568f0706e82SJiri Benc 	unsigned long flags;
569f0706e82SJiri Benc 	struct sk_buff *skb;
570836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
571f0706e82SJiri Benc 
572f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
573f0706e82SJiri Benc 		return;
574f0706e82SJiri Benc 
575f0706e82SJiri Benc 	for (;;) {
576f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
577f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
57857c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
579f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
580836341a7SJohannes Berg 		else
581f0706e82SJiri Benc 			skb = NULL;
582f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
583f0706e82SJiri Benc 
584836341a7SJohannes Berg 		if (!skb)
585836341a7SJohannes Berg 			break;
586836341a7SJohannes Berg 
587d0709a65SJohannes Berg 		sdata = sta->sdata;
588f0706e82SJiri Benc 		local->total_ps_buffered--;
589f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
5900c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
5910c68ae26SJohannes Berg 		       sta->sta.addr);
592f4ea83ddSJohannes Berg #endif
593f0706e82SJiri Benc 		dev_kfree_skb(skb);
594836341a7SJohannes Berg 
595004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
596004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
597f0706e82SJiri Benc 	}
598f0706e82SJiri Benc }
599f0706e82SJiri Benc 
600f0706e82SJiri Benc 
601f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
602f0706e82SJiri Benc {
603f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
604f0706e82SJiri Benc 	struct sta_info *sta;
605f0706e82SJiri Benc 
606d0709a65SJohannes Berg 	rcu_read_lock();
607d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
608f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
609d0709a65SJohannes Berg 	rcu_read_unlock();
610f0706e82SJiri Benc 
6110d174406SJohannes Berg 	local->sta_cleanup.expires =
6120d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
613f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
614f0706e82SJiri Benc }
615f0706e82SJiri Benc 
616e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
6174d6141c3SJiri Slaby /*
6184d6141c3SJiri Slaby  * See comment in __sta_info_unlink,
6194d6141c3SJiri Slaby  * caller must hold local->sta_lock.
6204d6141c3SJiri Slaby  */
6214d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta)
6224d6141c3SJiri Slaby {
6234d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
6244d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_PINNED;
6254d6141c3SJiri Slaby }
6264d6141c3SJiri Slaby 
6274d6141c3SJiri Slaby /*
6284d6141c3SJiri Slaby  * See comment in __sta_info_unlink, returns sta if it
6294d6141c3SJiri Slaby  * needs to be destroyed.
6304d6141c3SJiri Slaby  */
6314d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta)
6324d6141c3SJiri Slaby {
6334d6141c3SJiri Slaby 	struct sta_info *ret = NULL;
6344d6141c3SJiri Slaby 	unsigned long flags;
6354d6141c3SJiri Slaby 
6364d6141c3SJiri Slaby 	spin_lock_irqsave(&sta->local->sta_lock, flags);
6374d6141c3SJiri Slaby 	WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
6384d6141c3SJiri Slaby 		sta->pin_status != STA_INFO_PIN_STAT_PINNED);
6394d6141c3SJiri Slaby 	if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
6404d6141c3SJiri Slaby 		ret = sta;
6414d6141c3SJiri Slaby 	sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
6424d6141c3SJiri Slaby 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
6434d6141c3SJiri Slaby 
6444d6141c3SJiri Slaby 	return ret;
6454d6141c3SJiri Slaby }
6464d6141c3SJiri Slaby 
647d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work)
648e9f207f0SJiri Benc {
649e9f207f0SJiri Benc 	struct ieee80211_local *local =
650e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
651e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
652d0709a65SJohannes Berg 	unsigned long flags;
653e9f207f0SJiri Benc 
65449ec6fa2SJohannes Berg 	/* We need to keep the RTNL across the whole pinned status. */
65549ec6fa2SJohannes Berg 	rtnl_lock();
656e9f207f0SJiri Benc 	while (1) {
657e9f207f0SJiri Benc 		sta = NULL;
658d0709a65SJohannes Berg 
659d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
660e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
66163044e9fSJohannes Berg 			/*
66263044e9fSJohannes Berg 			 * debugfs.add_has_run will be set by
66363044e9fSJohannes Berg 			 * ieee80211_sta_debugfs_add regardless
66463044e9fSJohannes Berg 			 * of what else it does.
66563044e9fSJohannes Berg 			 */
66663044e9fSJohannes Berg 			if (!tmp->debugfs.add_has_run) {
667e9f207f0SJiri Benc 				sta = tmp;
668d0709a65SJohannes Berg 				__sta_info_pin(sta);
669e9f207f0SJiri Benc 				break;
670e9f207f0SJiri Benc 			}
671e9f207f0SJiri Benc 		}
672d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
673e9f207f0SJiri Benc 
674e9f207f0SJiri Benc 		if (!sta)
675e9f207f0SJiri Benc 			break;
676e9f207f0SJiri Benc 
677e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
678e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
679d0709a65SJohannes Berg 
680d0709a65SJohannes Berg 		sta = __sta_info_unpin(sta);
681d0709a65SJohannes Berg 		sta_info_destroy(sta);
682e9f207f0SJiri Benc 	}
68349ec6fa2SJohannes Berg 	rtnl_unlock();
684e9f207f0SJiri Benc }
685e9f207f0SJiri Benc #endif
686e9f207f0SJiri Benc 
687f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
688f0706e82SJiri Benc {
689d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
690f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
691f0706e82SJiri Benc 
692b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
693b24b8a24SPavel Emelyanov 		    (unsigned long)local);
6940d174406SJohannes Berg 	local->sta_cleanup.expires =
6950d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
696e9f207f0SJiri Benc 
697e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
698d0709a65SJohannes Berg 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
699e9f207f0SJiri Benc #endif
700f0706e82SJiri Benc }
701f0706e82SJiri Benc 
702f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
703f0706e82SJiri Benc {
704f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
705f0706e82SJiri Benc 	return 0;
706f0706e82SJiri Benc }
707f0706e82SJiri Benc 
708f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
709f0706e82SJiri Benc {
710f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
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 
721be8755e1SMichael Wu 	sta_info_flush(local, NULL);
722f0706e82SJiri Benc }
723f0706e82SJiri Benc 
724f0706e82SJiri Benc /**
725f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
72644213b5eSJohannes Berg  *
72744213b5eSJohannes Berg  * Returns the number of removed STA entries.
72844213b5eSJohannes Berg  *
729f0706e82SJiri Benc  * @local: local interface data
730d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
731f0706e82SJiri Benc  */
73244213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
733d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
734f0706e82SJiri Benc {
735f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
736be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
73744213b5eSJohannes Berg 	int ret = 0;
738d0709a65SJohannes Berg 	unsigned long flags;
739f0706e82SJiri Benc 
740d0709a65SJohannes Berg 	might_sleep();
741d0709a65SJohannes Berg 
742d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
743d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
744d0709a65SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
745d0709a65SJohannes Berg 			__sta_info_unlink(&sta);
74644213b5eSJohannes Berg 			if (sta) {
747be8755e1SMichael Wu 				list_add_tail(&sta->list, &tmp_list);
74844213b5eSJohannes Berg 				ret++;
74944213b5eSJohannes Berg 			}
750be8755e1SMichael Wu 		}
751be8755e1SMichael Wu 	}
752d0709a65SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
753d0709a65SJohannes Berg 
754d0709a65SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
755d0709a65SJohannes Berg 		sta_info_destroy(sta);
75644213b5eSJohannes Berg 
75744213b5eSJohannes Berg 	return ret;
758f0706e82SJiri Benc }
759dc6676b7SJohannes Berg 
76024723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
76124723d1bSJohannes Berg 			  unsigned long exp_time)
76224723d1bSJohannes Berg {
76324723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
76424723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
76524723d1bSJohannes Berg 	LIST_HEAD(tmp_list);
76624723d1bSJohannes Berg 	unsigned long flags;
76724723d1bSJohannes Berg 
76824723d1bSJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
76924723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
77024723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
77124723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
7720c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
7730c68ae26SJohannes Berg 			       sdata->dev->name, sta->sta.addr);
77424723d1bSJohannes Berg #endif
77524723d1bSJohannes Berg 			__sta_info_unlink(&sta);
77624723d1bSJohannes Berg 			if (sta)
77724723d1bSJohannes Berg 				list_add(&sta->list, &tmp_list);
77824723d1bSJohannes Berg 		}
77924723d1bSJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
78024723d1bSJohannes Berg 
78124723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &tmp_list, list)
78224723d1bSJohannes Berg 		sta_info_destroy(sta);
78324723d1bSJohannes Berg }
78417741cdcSJohannes Berg 
78517741cdcSJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_hw *hw,
78617741cdcSJohannes Berg 					 const u8 *addr)
78717741cdcSJohannes Berg {
7884b7679a5SJohannes Berg 	struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
78917741cdcSJohannes Berg 
79017741cdcSJohannes Berg 	if (!sta)
79117741cdcSJohannes Berg 		return NULL;
79217741cdcSJohannes Berg 	return &sta->sta;
79317741cdcSJohannes Berg }
79417741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
795