xref: /openbmc/linux/net/mac80211/sta_info.c (revision 2a33bee2753bf28411de8822e3e3c7501966eb1b)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4f0706e82SJiri Benc  *
5f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
6f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
7f0706e82SJiri Benc  * published by the Free Software Foundation.
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12f0706e82SJiri Benc #include <linux/netdevice.h>
13f0706e82SJiri Benc #include <linux/types.h>
14f0706e82SJiri Benc #include <linux/slab.h>
15f0706e82SJiri Benc #include <linux/skbuff.h>
16f0706e82SJiri Benc #include <linux/if_arp.h>
170d174406SJohannes Berg #include <linux/timer.h>
18d0709a65SJohannes Berg #include <linux/rtnetlink.h>
19f0706e82SJiri Benc 
20f0706e82SJiri Benc #include <net/mac80211.h>
21f0706e82SJiri Benc #include "ieee80211_i.h"
2224487981SJohannes Berg #include "driver-ops.h"
232c8dccc7SJohannes Berg #include "rate.h"
24f0706e82SJiri Benc #include "sta_info.h"
25e9f207f0SJiri Benc #include "debugfs_sta.h"
26ee385855SLuis Carlos Cobo #include "mesh.h"
27f0706e82SJiri Benc 
28d0709a65SJohannes Berg /**
29d0709a65SJohannes Berg  * DOC: STA information lifetime rules
30d0709a65SJohannes Berg  *
31d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
32d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
33d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
34d0709a65SJohannes Berg  *
3534e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3634e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3734e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
3834e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
3934e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4034e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4134e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4234e89507SJohannes Berg  * 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  *
4734e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
487e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
497e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5025985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5134e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5225985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5334e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
547e189a12SLuis R. Rodriguez  *
5534e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5634e89507SJohannes Berg  * calls are available.
57d0709a65SJohannes Berg  *
5834e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
5934e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6034e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6134e89507SJohannes Berg  * freed before they are done using it.
62d0709a65SJohannes Berg  */
63f0706e82SJiri Benc 
64f0706e82SJiri Benc /* Caller must hold local->sta_lock */
65be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
66f0706e82SJiri Benc 			     struct sta_info *sta)
67f0706e82SJiri Benc {
68f0706e82SJiri Benc 	struct sta_info *s;
69f0706e82SJiri Benc 
7040b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
7140b275b6SJohannes Berg 				      lockdep_is_held(&local->sta_lock));
72f0706e82SJiri Benc 	if (!s)
73be8755e1SMichael Wu 		return -ENOENT;
74be8755e1SMichael Wu 	if (s == sta) {
7517741cdcSJohannes Berg 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
76d0709a65SJohannes Berg 				   s->hnext);
77be8755e1SMichael Wu 		return 0;
78f0706e82SJiri Benc 	}
79f0706e82SJiri Benc 
8040b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8140b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8240b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
8340b275b6SJohannes Berg 					lockdep_is_held(&local->sta_lock));
8440b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
85d0709a65SJohannes Berg 		rcu_assign_pointer(s->hnext, sta->hnext);
86be8755e1SMichael Wu 		return 0;
87f0706e82SJiri Benc 	}
88f0706e82SJiri Benc 
89be8755e1SMichael Wu 	return -ENOENT;
90f0706e82SJiri Benc }
91f0706e82SJiri Benc 
92d0709a65SJohannes Berg /* protected by RCU */
93abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
94abe60632SJohannes Berg 			      const u8 *addr)
9543ba7e95SJohannes Berg {
96abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
9743ba7e95SJohannes Berg 	struct sta_info *sta;
9843ba7e95SJohannes Berg 
990379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1000379185bSJohannes Berg 				    lockdep_is_held(&local->sta_lock) ||
1010379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
10243ba7e95SJohannes Berg 	while (sta) {
103*2a33bee2SGuy Eilam 		if (sta->sdata == sdata && !sta->dummy &&
104*2a33bee2SGuy Eilam 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
105*2a33bee2SGuy Eilam 			break;
106*2a33bee2SGuy Eilam 		sta = rcu_dereference_check(sta->hnext,
107*2a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_lock) ||
108*2a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_mtx));
109*2a33bee2SGuy Eilam 	}
110*2a33bee2SGuy Eilam 	return sta;
111*2a33bee2SGuy Eilam }
112*2a33bee2SGuy Eilam 
113*2a33bee2SGuy Eilam /* get a station info entry even if it is a dummy station*/
114*2a33bee2SGuy Eilam struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
115*2a33bee2SGuy Eilam 			      const u8 *addr)
116*2a33bee2SGuy Eilam {
117*2a33bee2SGuy Eilam 	struct ieee80211_local *local = sdata->local;
118*2a33bee2SGuy Eilam 	struct sta_info *sta;
119*2a33bee2SGuy Eilam 
120*2a33bee2SGuy Eilam 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
121*2a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_lock) ||
122*2a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_mtx));
123*2a33bee2SGuy Eilam 	while (sta) {
124abe60632SJohannes Berg 		if (sta->sdata == sdata &&
125abe60632SJohannes Berg 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
12643ba7e95SJohannes Berg 			break;
1270379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1280379185bSJohannes Berg 					    lockdep_is_held(&local->sta_lock) ||
1290379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
13043ba7e95SJohannes Berg 	}
13143ba7e95SJohannes Berg 	return sta;
13243ba7e95SJohannes Berg }
13343ba7e95SJohannes Berg 
1340e5ded5aSFelix Fietkau /*
1350e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1360e5ded5aSFelix Fietkau  * or from one of its vlans
1370e5ded5aSFelix Fietkau  */
1380e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1390e5ded5aSFelix Fietkau 				  const u8 *addr)
1400e5ded5aSFelix Fietkau {
1410e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1420e5ded5aSFelix Fietkau 	struct sta_info *sta;
1430e5ded5aSFelix Fietkau 
1440379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1450379185bSJohannes Berg 				    lockdep_is_held(&local->sta_lock) ||
1460379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1470e5ded5aSFelix Fietkau 	while (sta) {
1480e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
149a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
150*2a33bee2SGuy Eilam 		    !sta->dummy &&
151*2a33bee2SGuy Eilam 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
152*2a33bee2SGuy Eilam 			break;
153*2a33bee2SGuy Eilam 		sta = rcu_dereference_check(sta->hnext,
154*2a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_lock) ||
155*2a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_mtx));
156*2a33bee2SGuy Eilam 	}
157*2a33bee2SGuy Eilam 	return sta;
158*2a33bee2SGuy Eilam }
159*2a33bee2SGuy Eilam 
160*2a33bee2SGuy Eilam /*
161*2a33bee2SGuy Eilam  * Get sta info either from the specified interface
162*2a33bee2SGuy Eilam  * or from one of its vlans (including dummy stations)
163*2a33bee2SGuy Eilam  */
164*2a33bee2SGuy Eilam struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
165*2a33bee2SGuy Eilam 				  const u8 *addr)
166*2a33bee2SGuy Eilam {
167*2a33bee2SGuy Eilam 	struct ieee80211_local *local = sdata->local;
168*2a33bee2SGuy Eilam 	struct sta_info *sta;
169*2a33bee2SGuy Eilam 
170*2a33bee2SGuy Eilam 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
171*2a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_lock) ||
172*2a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_mtx));
173*2a33bee2SGuy Eilam 	while (sta) {
174*2a33bee2SGuy Eilam 		if ((sta->sdata == sdata ||
175*2a33bee2SGuy Eilam 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
1760e5ded5aSFelix Fietkau 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1770e5ded5aSFelix Fietkau 			break;
1780379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1790379185bSJohannes Berg 					    lockdep_is_held(&local->sta_lock) ||
1800379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1810e5ded5aSFelix Fietkau 	}
1820e5ded5aSFelix Fietkau 	return sta;
1830e5ded5aSFelix Fietkau }
1840e5ded5aSFelix Fietkau 
1853b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1863b53fde8SJohannes Berg 				     int idx)
187ee385855SLuis Carlos Cobo {
1883b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
189ee385855SLuis Carlos Cobo 	struct sta_info *sta;
190ee385855SLuis Carlos Cobo 	int i = 0;
191ee385855SLuis Carlos Cobo 
192d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1933b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1942a8ca29aSLuis Carlos Cobo 			continue;
195ee385855SLuis Carlos Cobo 		if (i < idx) {
196ee385855SLuis Carlos Cobo 			++i;
197ee385855SLuis Carlos Cobo 			continue;
198ee385855SLuis Carlos Cobo 		}
1992a8ca29aSLuis Carlos Cobo 		return sta;
200ee385855SLuis Carlos Cobo 	}
201ee385855SLuis Carlos Cobo 
202ee385855SLuis Carlos Cobo 	return NULL;
203ee385855SLuis Carlos Cobo }
204f0706e82SJiri Benc 
20593e5deb1SJohannes Berg /**
20693e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
20793e5deb1SJohannes Berg  *
2086ef307bcSRandy Dunlap  * @local: pointer to the global information
20993e5deb1SJohannes Berg  * @sta: STA info to free
21093e5deb1SJohannes Berg  *
21193e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
21293e5deb1SJohannes Berg  * that may happen before sta_info_insert().
21393e5deb1SJohannes Berg  */
21493e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
21593e5deb1SJohannes Berg 			    struct sta_info *sta)
21693e5deb1SJohannes Berg {
217af65cd96SJohannes Berg 	if (sta->rate_ctrl) {
2184b7679a5SJohannes Berg 		rate_control_free_sta(sta);
21993e5deb1SJohannes Berg 		rate_control_put(sta->rate_ctrl);
220af65cd96SJohannes Berg 	}
22193e5deb1SJohannes Berg 
22293e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2230fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
22493e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
22593e5deb1SJohannes Berg 
22693e5deb1SJohannes Berg 	kfree(sta);
22793e5deb1SJohannes Berg }
22893e5deb1SJohannes Berg 
229d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
230d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
231d0709a65SJohannes Berg 			      struct sta_info *sta)
232f0706e82SJiri Benc {
23317741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
23417741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
235f0706e82SJiri Benc }
236f0706e82SJiri Benc 
237af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
238af818581SJohannes Berg {
239af818581SJohannes Berg 	struct sta_info *sta;
240af818581SJohannes Berg 
241af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
242af818581SJohannes Berg 
243af818581SJohannes Berg 	if (sta->dead)
244af818581SJohannes Berg 		return;
245af818581SJohannes Berg 
246af818581SJohannes Berg 	if (!test_sta_flags(sta, WLAN_STA_PS_STA))
247af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
24850a9432dSJohannes Berg 	else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) {
24950a9432dSJohannes Berg 		clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
250af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
25150a9432dSJohannes Berg 	} else
25250a9432dSJohannes Berg 		clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
253af818581SJohannes Berg }
254af818581SJohannes Berg 
255af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
256af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
257af65cd96SJohannes Berg {
258af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
259af65cd96SJohannes Berg 		return 0;
260af65cd96SJohannes Berg 
261af65cd96SJohannes Berg 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
262af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
263af65cd96SJohannes Berg 						     &sta->sta, gfp);
264af65cd96SJohannes Berg 	if (!sta->rate_ctrl_priv) {
265af65cd96SJohannes Berg 		rate_control_put(sta->rate_ctrl);
266af65cd96SJohannes Berg 		return -ENOMEM;
267af65cd96SJohannes Berg 	}
268af65cd96SJohannes Berg 
269af65cd96SJohannes Berg 	return 0;
270af65cd96SJohannes Berg }
271af65cd96SJohannes Berg 
27273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
27373651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
274f0706e82SJiri Benc {
275d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
276f0706e82SJiri Benc 	struct sta_info *sta;
277ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
27816c5f15cSRon Rindjunsky 	int i;
279f0706e82SJiri Benc 
28017741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
281f0706e82SJiri Benc 	if (!sta)
28273651ee6SJohannes Berg 		return NULL;
283f0706e82SJiri Benc 
28407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2855a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
286af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
28767c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
288a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
28907346f81SJohannes Berg 
29017741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
291d0709a65SJohannes Berg 	sta->local = local;
292d0709a65SJohannes Berg 	sta->sdata = sdata;
2938bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
294f0706e82SJiri Benc 
295ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
296ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
297541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
298541a45a1SBruno Randolf 
299af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
300f0706e82SJiri Benc 		kfree(sta);
30173651ee6SJohannes Berg 		return NULL;
302f0706e82SJiri Benc 	}
303f0706e82SJiri Benc 
30416c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
305a622ab72SJohannes Berg 		/*
306a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
307a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
308a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
309a622ab72SJohannes Berg 		 */
31016c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
31116c5f15cSRon Rindjunsky 	}
312f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
313f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
31473651ee6SJohannes Berg 
315cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
3164be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
317cccaec98SSenthil Balasubramanian 
31873651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3190fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
32073651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
32173651ee6SJohannes Berg 
32203e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
32357cf8043SJavier Cardona 	sta->plink_state = NL80211_PLINK_LISTEN;
32403e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
32503e4497eSJohannes Berg #endif
32603e4497eSJohannes Berg 
32773651ee6SJohannes Berg 	return sta;
32873651ee6SJohannes Berg }
32973651ee6SJohannes Berg 
330*2a33bee2SGuy Eilam static int sta_info_finish_insert(struct sta_info *sta,
331*2a33bee2SGuy Eilam 				bool async, bool dummy_reinsert)
33273651ee6SJohannes Berg {
33373651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
33473651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
33598b62183SJohannes Berg 	struct station_info sinfo;
33673651ee6SJohannes Berg 	unsigned long flags;
33793e5deb1SJohannes Berg 	int err = 0;
33873651ee6SJohannes Berg 
33946a5ebafSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
34034e89507SJohannes Berg 
341*2a33bee2SGuy Eilam 	if (!sta->dummy || dummy_reinsert) {
34234e89507SJohannes Berg 		/* notify driver */
34334e89507SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
34434e89507SJohannes Berg 			sdata = container_of(sdata->bss,
34534e89507SJohannes Berg 					     struct ieee80211_sub_if_data,
34634e89507SJohannes Berg 					     u.ap);
34734e89507SJohannes Berg 		err = drv_sta_add(local, sdata, &sta->sta);
34834e89507SJohannes Berg 		if (err) {
34934e89507SJohannes Berg 			if (!async)
35034e89507SJohannes Berg 				return err;
351*2a33bee2SGuy Eilam 			printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
352*2a33bee2SGuy Eilam 					  "driver (%d) - keeping it anyway.\n",
35334e89507SJohannes Berg 			       sdata->name, sta->sta.addr, err);
35434e89507SJohannes Berg 		} else {
35534e89507SJohannes Berg 			sta->uploaded = true;
35634e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
35734e89507SJohannes Berg 			if (async)
3580fb9a9ecSJoe Perches 				wiphy_debug(local->hw.wiphy,
3590fb9a9ecSJoe Perches 					    "Finished adding IBSS STA %pM\n",
3600fb9a9ecSJoe Perches 					    sta->sta.addr);
36134e89507SJohannes Berg #endif
36234e89507SJohannes Berg 		}
36334e89507SJohannes Berg 
36434e89507SJohannes Berg 		sdata = sta->sdata;
365*2a33bee2SGuy Eilam 	}
36634e89507SJohannes Berg 
367*2a33bee2SGuy Eilam 	if (!dummy_reinsert) {
36834e89507SJohannes Berg 		if (!async) {
36934e89507SJohannes Berg 			local->num_sta++;
37034e89507SJohannes Berg 			local->sta_generation++;
37134e89507SJohannes Berg 			smp_mb();
37234e89507SJohannes Berg 
37334e89507SJohannes Berg 			/* make the station visible */
37434e89507SJohannes Berg 			spin_lock_irqsave(&local->sta_lock, flags);
37534e89507SJohannes Berg 			sta_info_hash_add(local, sta);
37634e89507SJohannes Berg 			spin_unlock_irqrestore(&local->sta_lock, flags);
37734e89507SJohannes Berg 		}
37834e89507SJohannes Berg 
37934e89507SJohannes Berg 		list_add(&sta->list, &local->sta_list);
380*2a33bee2SGuy Eilam 	} else {
381*2a33bee2SGuy Eilam 		sta->dummy = false;
382*2a33bee2SGuy Eilam 	}
38334e89507SJohannes Berg 
384*2a33bee2SGuy Eilam 	if (!sta->dummy) {
38534e89507SJohannes Berg 		ieee80211_sta_debugfs_add(sta);
38634e89507SJohannes Berg 		rate_control_add_sta_debugfs(sta);
38734e89507SJohannes Berg 
388f612cedfSJouni Malinen 		memset(&sinfo, 0, sizeof(sinfo));
38934e89507SJohannes Berg 		sinfo.filled = 0;
39034e89507SJohannes Berg 		sinfo.generation = local->sta_generation;
39134e89507SJohannes Berg 		cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
392*2a33bee2SGuy Eilam 	}
39334e89507SJohannes Berg 
39434e89507SJohannes Berg 	return 0;
39534e89507SJohannes Berg }
39634e89507SJohannes Berg 
39734e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local)
39834e89507SJohannes Berg {
39934e89507SJohannes Berg 	struct sta_info *sta;
40034e89507SJohannes Berg 	unsigned long flags;
40134e89507SJohannes Berg 
40234e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
40334e89507SJohannes Berg 	while (!list_empty(&local->sta_pending_list)) {
40434e89507SJohannes Berg 		sta = list_first_entry(&local->sta_pending_list,
40534e89507SJohannes Berg 				       struct sta_info, list);
40634e89507SJohannes Berg 		list_del(&sta->list);
40734e89507SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
40834e89507SJohannes Berg 
409*2a33bee2SGuy Eilam 		sta_info_finish_insert(sta, true, false);
41034e89507SJohannes Berg 
41134e89507SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
41234e89507SJohannes Berg 	}
41334e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
41434e89507SJohannes Berg }
41534e89507SJohannes Berg 
41634e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work)
41734e89507SJohannes Berg {
41834e89507SJohannes Berg 	struct ieee80211_local *local =
41934e89507SJohannes Berg 		container_of(work, struct ieee80211_local, sta_finish_work);
42034e89507SJohannes Berg 
42134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
42234e89507SJohannes Berg 	sta_info_finish_pending(local);
42334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
42434e89507SJohannes Berg }
42534e89507SJohannes Berg 
4268c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42734e89507SJohannes Berg {
42834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42934e89507SJohannes Berg 
43003e4497eSJohannes Berg 	/*
43103e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
43203e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
43303e4497eSJohannes Berg 	 * and another CPU turns off the net device.
43403e4497eSJohannes Berg 	 */
4358c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4368c71df7aSGuy Eilam 		return -ENETDOWN;
43703e4497eSJohannes Berg 
43847846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
4398c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4408c71df7aSGuy Eilam 		return -EINVAL;
4418c71df7aSGuy Eilam 
4428c71df7aSGuy Eilam 	return 0;
44393e5deb1SJohannes Berg }
44444213b5eSJohannes Berg 
4458c71df7aSGuy Eilam static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU)
4468c71df7aSGuy Eilam {
4478c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4488c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4498c71df7aSGuy Eilam 	unsigned long flags;
4508c71df7aSGuy Eilam 
451d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
45243ba7e95SJohannes Berg 	/* check if STA exists already */
453*2a33bee2SGuy Eilam 	if (sta_info_get_bss_rx(sdata, sta->sta.addr)) {
454d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
45534e89507SJohannes Berg 		rcu_read_lock();
4568c71df7aSGuy Eilam 		return -EEXIST;
45743ba7e95SJohannes Berg 	}
45834e89507SJohannes Berg 
459f0706e82SJiri Benc 	local->num_sta++;
46034e89507SJohannes Berg 	local->sta_generation++;
46134e89507SJohannes Berg 	smp_mb();
462f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
46332bfd35dSJohannes Berg 
46434e89507SJohannes Berg 	list_add_tail(&sta->list, &local->sta_pending_list);
46532bfd35dSJohannes Berg 
46634e89507SJohannes Berg 	rcu_read_lock();
46734e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
46834e89507SJohannes Berg 
46934e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4700fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
4710fb9a9ecSJoe Perches 			sta->sta.addr);
47234e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
47334e89507SJohannes Berg 
47434e89507SJohannes Berg 	ieee80211_queue_work(&local->hw, &local->sta_finish_work);
47534e89507SJohannes Berg 
47634e89507SJohannes Berg 	return 0;
47734e89507SJohannes Berg }
47834e89507SJohannes Berg 
47934e89507SJohannes Berg /*
4808c71df7aSGuy Eilam  * should be called with sta_mtx locked
4818c71df7aSGuy Eilam  * this function replaces the mutex lock
4828c71df7aSGuy Eilam  * with a RCU lock
4838c71df7aSGuy Eilam  */
4848c71df7aSGuy Eilam static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU)
4858c71df7aSGuy Eilam {
4868c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4878c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4888c71df7aSGuy Eilam 	unsigned long flags;
489*2a33bee2SGuy Eilam 	struct sta_info *exist_sta;
490*2a33bee2SGuy Eilam 	bool dummy_reinsert = false;
4918c71df7aSGuy Eilam 	int err = 0;
4928c71df7aSGuy Eilam 
4938c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4948c71df7aSGuy Eilam 
4958c71df7aSGuy Eilam 	/*
49634e89507SJohannes Berg 	 * On first glance, this will look racy, because the code
4978c71df7aSGuy Eilam 	 * in this function, which inserts a station with sleeping,
49834e89507SJohannes Berg 	 * unlocks the sta_lock between checking existence in the
49934e89507SJohannes Berg 	 * hash table and inserting into it.
50034e89507SJohannes Berg 	 *
50134e89507SJohannes Berg 	 * However, it is not racy against itself because it keeps
5028c71df7aSGuy Eilam 	 * the mutex locked.
50334e89507SJohannes Berg 	 */
50434e89507SJohannes Berg 
50534e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
506*2a33bee2SGuy Eilam 	/*
507*2a33bee2SGuy Eilam 	 * check if STA exists already.
508*2a33bee2SGuy Eilam 	 * only accept a scenario of a second call to sta_info_insert_non_ibss
509*2a33bee2SGuy Eilam 	 * with a dummy station entry that was inserted earlier
510*2a33bee2SGuy Eilam 	 * in that case - assume that the dummy station flag should
511*2a33bee2SGuy Eilam 	 * be removed.
512*2a33bee2SGuy Eilam 	 */
513*2a33bee2SGuy Eilam 	exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
514*2a33bee2SGuy Eilam 	if (exist_sta) {
515*2a33bee2SGuy Eilam 		if (exist_sta == sta && sta->dummy) {
516*2a33bee2SGuy Eilam 			dummy_reinsert = true;
517*2a33bee2SGuy Eilam 		} else {
51834e89507SJohannes Berg 			spin_unlock_irqrestore(&local->sta_lock, flags);
51938a679a5SJouni Malinen 			mutex_unlock(&local->sta_mtx);
52034e89507SJohannes Berg 			rcu_read_lock();
5218c71df7aSGuy Eilam 			return -EEXIST;
52234e89507SJohannes Berg 		}
523*2a33bee2SGuy Eilam 	}
52434e89507SJohannes Berg 
52534e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
52634e89507SJohannes Berg 
527*2a33bee2SGuy Eilam 	err = sta_info_finish_insert(sta, false, dummy_reinsert);
52834e89507SJohannes Berg 	if (err) {
52934e89507SJohannes Berg 		mutex_unlock(&local->sta_mtx);
53034e89507SJohannes Berg 		rcu_read_lock();
5318c71df7aSGuy Eilam 		return err;
53232bfd35dSJohannes Berg 	}
533d0709a65SJohannes Berg 
534f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
535*2a33bee2SGuy Eilam 	wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
536*2a33bee2SGuy Eilam 			sta->dummy ? "dummy " : "", sta->sta.addr);
537f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
538f0706e82SJiri Benc 
53934e89507SJohannes Berg 	/* move reference to rcu-protected */
54034e89507SJohannes Berg 	rcu_read_lock();
54134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
542e9f207f0SJiri Benc 
54373651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
54473651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
54573651ee6SJohannes Berg 
54673651ee6SJohannes Berg 	return 0;
5478c71df7aSGuy Eilam }
5488c71df7aSGuy Eilam 
5498c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5508c71df7aSGuy Eilam {
5518c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5528c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5538c71df7aSGuy Eilam 	int err = 0;
5548c71df7aSGuy Eilam 
5558c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5568c71df7aSGuy Eilam 	if (err) {
5578c71df7aSGuy Eilam 		rcu_read_lock();
5588c71df7aSGuy Eilam 		goto out_free;
5598c71df7aSGuy Eilam 	}
5608c71df7aSGuy Eilam 
5618c71df7aSGuy Eilam 	/*
5628c71df7aSGuy Eilam 	 * In ad-hoc mode, we sometimes need to insert stations
5638c71df7aSGuy Eilam 	 * from tasklet context from the RX path. To avoid races,
5648c71df7aSGuy Eilam 	 * always do so in that case -- see the comment below.
5658c71df7aSGuy Eilam 	 */
5668c71df7aSGuy Eilam 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
5678c71df7aSGuy Eilam 		err = sta_info_insert_ibss(sta);
5688c71df7aSGuy Eilam 		if (err)
5698c71df7aSGuy Eilam 			goto out_free;
5708c71df7aSGuy Eilam 
5718c71df7aSGuy Eilam 		return 0;
5728c71df7aSGuy Eilam 	}
5738c71df7aSGuy Eilam 
5748c71df7aSGuy Eilam 	/*
5758c71df7aSGuy Eilam 	 * It might seem that the function called below is in race against
5768c71df7aSGuy Eilam 	 * the function call above that atomically inserts the station... That,
5778c71df7aSGuy Eilam 	 * however, is not true because the above code can only
5788c71df7aSGuy Eilam 	 * be invoked for IBSS interfaces, and the below code will
5798c71df7aSGuy Eilam 	 * not be -- and the two do not race against each other as
5808c71df7aSGuy Eilam 	 * the hash table also keys off the interface.
5818c71df7aSGuy Eilam 	 */
5828c71df7aSGuy Eilam 
5838c71df7aSGuy Eilam 	might_sleep();
5848c71df7aSGuy Eilam 
5858c71df7aSGuy Eilam 	mutex_lock(&local->sta_mtx);
5868c71df7aSGuy Eilam 
5878c71df7aSGuy Eilam 	err = sta_info_insert_non_ibss(sta);
5888c71df7aSGuy Eilam 	if (err)
5898c71df7aSGuy Eilam 		goto out_free;
5908c71df7aSGuy Eilam 
5918c71df7aSGuy Eilam 	return 0;
59293e5deb1SJohannes Berg  out_free:
59393e5deb1SJohannes Berg 	BUG_ON(!err);
59493e5deb1SJohannes Berg 	__sta_info_free(local, sta);
59593e5deb1SJohannes Berg 	return err;
596f0706e82SJiri Benc }
597f0706e82SJiri Benc 
59834e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
59934e89507SJohannes Berg {
60034e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
60134e89507SJohannes Berg 
60234e89507SJohannes Berg 	rcu_read_unlock();
60334e89507SJohannes Berg 
60434e89507SJohannes Berg 	return err;
60534e89507SJohannes Berg }
60634e89507SJohannes Berg 
607*2a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */
608*2a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta)
609*2a33bee2SGuy Eilam {
610*2a33bee2SGuy Eilam 	struct ieee80211_local *local = sta->local;
611*2a33bee2SGuy Eilam 	int err = 0;
612*2a33bee2SGuy Eilam 
613*2a33bee2SGuy Eilam 	err = sta_info_insert_check(sta);
614*2a33bee2SGuy Eilam 	if (err) {
615*2a33bee2SGuy Eilam 		mutex_unlock(&local->sta_mtx);
616*2a33bee2SGuy Eilam 		return err;
617*2a33bee2SGuy Eilam 	}
618*2a33bee2SGuy Eilam 
619*2a33bee2SGuy Eilam 	might_sleep();
620*2a33bee2SGuy Eilam 
621*2a33bee2SGuy Eilam 	err = sta_info_insert_non_ibss(sta);
622*2a33bee2SGuy Eilam 	rcu_read_unlock();
623*2a33bee2SGuy Eilam 	return err;
624*2a33bee2SGuy Eilam }
625*2a33bee2SGuy Eilam 
626004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
627004c872eSJohannes Berg {
628004c872eSJohannes Berg 	/*
629004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
630004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
631004c872eSJohannes Berg 	 */
632004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
633004c872eSJohannes Berg }
634004c872eSJohannes Berg 
635004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
636004c872eSJohannes Berg {
637004c872eSJohannes Berg 	/*
638004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
639004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
640004c872eSJohannes Berg 	 */
641004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
642004c872eSJohannes Berg }
643004c872eSJohannes Berg 
644004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
645004c872eSJohannes Berg 				   struct sta_info *sta)
646004c872eSJohannes Berg {
6473e122be0SJohannes Berg 	BUG_ON(!bss);
6483e122be0SJohannes Berg 
64917741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
6503e122be0SJohannes Berg 
651d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
652d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
65324487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, true);
654d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
655d0709a65SJohannes Berg 	}
656004c872eSJohannes Berg }
657004c872eSJohannes Berg 
658004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
659004c872eSJohannes Berg {
660d0709a65SJohannes Berg 	unsigned long flags;
661004c872eSJohannes Berg 
6623e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
6633e122be0SJohannes Berg 
664d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
665d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
666d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
667004c872eSJohannes Berg }
668004c872eSJohannes Berg 
669004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
670004c872eSJohannes Berg 				     struct sta_info *sta)
671004c872eSJohannes Berg {
6723e122be0SJohannes Berg 	BUG_ON(!bss);
6733e122be0SJohannes Berg 
67417741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
6753e122be0SJohannes Berg 
676d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
677d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
67824487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, false);
679d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
680d0709a65SJohannes Berg 	}
681004c872eSJohannes Berg }
682004c872eSJohannes Berg 
683004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
684004c872eSJohannes Berg {
685d0709a65SJohannes Berg 	unsigned long flags;
686004c872eSJohannes Berg 
6873e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
6883e122be0SJohannes Berg 
689d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
690d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
691d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
692004c872eSJohannes Berg }
693004c872eSJohannes Berg 
69457c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta,
695f0706e82SJiri Benc 				   struct sk_buff *skb)
696f0706e82SJiri Benc {
697e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
698f0706e82SJiri Benc 	int timeout;
699f0706e82SJiri Benc 
700f0706e82SJiri Benc 	if (!skb)
701f0706e82SJiri Benc 		return 0;
702f0706e82SJiri Benc 
703e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
704f0706e82SJiri Benc 
705f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
70657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
70757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
70857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
709f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
710f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
711e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
712f0706e82SJiri Benc }
713f0706e82SJiri Benc 
714f0706e82SJiri Benc 
7153393a608SJuuso Oikarinen static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
716f0706e82SJiri Benc 					     struct sta_info *sta)
717f0706e82SJiri Benc {
718f0706e82SJiri Benc 	unsigned long flags;
719f0706e82SJiri Benc 	struct sk_buff *skb;
720f0706e82SJiri Benc 
721f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
7223393a608SJuuso Oikarinen 		return false;
723f0706e82SJiri Benc 
724f0706e82SJiri Benc 	for (;;) {
725f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
726f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
72757c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
728f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
729836341a7SJohannes Berg 		else
730f0706e82SJiri Benc 			skb = NULL;
731f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
732f0706e82SJiri Benc 
733836341a7SJohannes Berg 		if (!skb)
734836341a7SJohannes Berg 			break;
735836341a7SJohannes Berg 
736f0706e82SJiri Benc 		local->total_ps_buffered--;
737f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
7380c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
7390c68ae26SJohannes Berg 		       sta->sta.addr);
740f4ea83ddSJohannes Berg #endif
741f0706e82SJiri Benc 		dev_kfree_skb(skb);
742836341a7SJohannes Berg 
743dcf55fb5SFelix Fietkau 		if (skb_queue_empty(&sta->ps_tx_buf) &&
744dcf55fb5SFelix Fietkau 		    !test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF))
745004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
746f0706e82SJiri Benc 	}
7473393a608SJuuso Oikarinen 
7483393a608SJuuso Oikarinen 	return true;
749f0706e82SJiri Benc }
750f0706e82SJiri Benc 
75134e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta)
75234e89507SJohannes Berg {
75334e89507SJohannes Berg 	struct ieee80211_local *local;
75434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
75534e89507SJohannes Berg 	struct sk_buff *skb;
75634e89507SJohannes Berg 	unsigned long flags;
757e31b8213SJohannes Berg 	int ret, i;
75834e89507SJohannes Berg 
75934e89507SJohannes Berg 	might_sleep();
76034e89507SJohannes Berg 
76134e89507SJohannes Berg 	if (!sta)
76234e89507SJohannes Berg 		return -ENOENT;
76334e89507SJohannes Berg 
76434e89507SJohannes Berg 	local = sta->local;
76534e89507SJohannes Berg 	sdata = sta->sdata;
76634e89507SJohannes Berg 
767098a6070SJohannes Berg 	/*
768098a6070SJohannes Berg 	 * Before removing the station from the driver and
769098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
770098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
771098a6070SJohannes Berg 	 * will be sufficient.
772098a6070SJohannes Berg 	 */
773098a6070SJohannes Berg 	set_sta_flags(sta, WLAN_STA_BLOCK_BA);
77453f73c09SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, true);
775098a6070SJohannes Berg 
77634e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
77734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
77834e89507SJohannes Berg 	/* this might still be the pending list ... which is fine */
77934e89507SJohannes Berg 	if (!ret)
78034e89507SJohannes Berg 		list_del(&sta->list);
78134e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
78234e89507SJohannes Berg 	if (ret)
78334e89507SJohannes Berg 		return ret;
78434e89507SJohannes Berg 
7858cb23153SJohannes Berg 	mutex_lock(&local->key_mtx);
786e31b8213SJohannes Berg 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
78740b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
788e31b8213SJohannes Berg 	if (sta->ptk)
78940b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
7908cb23153SJohannes Berg 	mutex_unlock(&local->key_mtx);
79134e89507SJohannes Berg 
79234e89507SJohannes Berg 	sta->dead = true;
79334e89507SJohannes Berg 
79434e89507SJohannes Berg 	if (test_and_clear_sta_flags(sta,
79534e89507SJohannes Berg 				WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
79634e89507SJohannes Berg 		BUG_ON(!sdata->bss);
79734e89507SJohannes Berg 
79834e89507SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
79934e89507SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, sta);
80034e89507SJohannes Berg 	}
80134e89507SJohannes Berg 
80234e89507SJohannes Berg 	local->num_sta--;
80334e89507SJohannes Berg 	local->sta_generation++;
80434e89507SJohannes Berg 
80534e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
80634e89507SJohannes Berg 		rcu_assign_pointer(sdata->u.vlan.sta, NULL);
80734e89507SJohannes Berg 
80834e89507SJohannes Berg 	if (sta->uploaded) {
80934e89507SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
81034e89507SJohannes Berg 			sdata = container_of(sdata->bss,
81134e89507SJohannes Berg 					     struct ieee80211_sub_if_data,
81234e89507SJohannes Berg 					     u.ap);
81334e89507SJohannes Berg 		drv_sta_remove(local, sdata, &sta->sta);
81434e89507SJohannes Berg 		sdata = sta->sdata;
81534e89507SJohannes Berg 	}
81634e89507SJohannes Berg 
817e64b3795SJohannes Berg 	/*
818e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
819e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
820e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
821e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
822e64b3795SJohannes Berg 	 */
823e64b3795SJohannes Berg 	synchronize_rcu();
824e64b3795SJohannes Berg 
82534e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
826e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
82734e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
82834e89507SJohannes Berg #endif
82934e89507SJohannes Berg 
83034e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
8310fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
83234e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
83334e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
83434e89507SJohannes Berg 
835ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
836ec15e68bSJouni Malinen 
83734e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
83834e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
83934e89507SJohannes Berg 
84034e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
84134e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
84234e89507SJohannes Berg 		mesh_plink_deactivate(sta);
84334e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
84434e89507SJohannes Berg 	}
84534e89507SJohannes Berg #endif
84634e89507SJohannes Berg 
84734e89507SJohannes Berg 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
84834e89507SJohannes Berg 		local->total_ps_buffered--;
84934e89507SJohannes Berg 		dev_kfree_skb_any(skb);
85034e89507SJohannes Berg 	}
85134e89507SJohannes Berg 
85234e89507SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
85334e89507SJohannes Berg 		dev_kfree_skb_any(skb);
85434e89507SJohannes Berg 
85534e89507SJohannes Berg 	__sta_info_free(local, sta);
85634e89507SJohannes Berg 
85734e89507SJohannes Berg 	return 0;
85834e89507SJohannes Berg }
85934e89507SJohannes Berg 
86034e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
86134e89507SJohannes Berg {
86234e89507SJohannes Berg 	struct sta_info *sta;
86334e89507SJohannes Berg 	int ret;
86434e89507SJohannes Berg 
86534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
866*2a33bee2SGuy Eilam 	sta = sta_info_get_rx(sdata, addr);
86734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
86834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
86934e89507SJohannes Berg 
87034e89507SJohannes Berg 	return ret;
87134e89507SJohannes Berg }
87234e89507SJohannes Berg 
87334e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
87434e89507SJohannes Berg 			      const u8 *addr)
87534e89507SJohannes Berg {
87634e89507SJohannes Berg 	struct sta_info *sta;
87734e89507SJohannes Berg 	int ret;
87834e89507SJohannes Berg 
87934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
880*2a33bee2SGuy Eilam 	sta = sta_info_get_bss_rx(sdata, addr);
88134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
88234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
88334e89507SJohannes Berg 
88434e89507SJohannes Berg 	return ret;
88534e89507SJohannes Berg }
886f0706e82SJiri Benc 
887f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
888f0706e82SJiri Benc {
889f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
890f0706e82SJiri Benc 	struct sta_info *sta;
8913393a608SJuuso Oikarinen 	bool timer_needed = false;
892f0706e82SJiri Benc 
893d0709a65SJohannes Berg 	rcu_read_lock();
894d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
8953393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
8963393a608SJuuso Oikarinen 			timer_needed = true;
897d0709a65SJohannes Berg 	rcu_read_unlock();
898f0706e82SJiri Benc 
8995bb644a0SJohannes Berg 	if (local->quiescing)
9005bb644a0SJohannes Berg 		return;
9015bb644a0SJohannes Berg 
9023393a608SJuuso Oikarinen 	if (!timer_needed)
9033393a608SJuuso Oikarinen 		return;
9043393a608SJuuso Oikarinen 
90526d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
90626d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
907f0706e82SJiri Benc }
908f0706e82SJiri Benc 
909f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
910f0706e82SJiri Benc {
911d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
91234e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
913f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
91434e89507SJohannes Berg 	INIT_LIST_HEAD(&local->sta_pending_list);
91534e89507SJohannes Berg 	INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
916f0706e82SJiri Benc 
917b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
918b24b8a24SPavel Emelyanov 		    (unsigned long)local);
919f0706e82SJiri Benc }
920f0706e82SJiri Benc 
921f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
922f0706e82SJiri Benc {
923f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
924be8755e1SMichael Wu 	sta_info_flush(local, NULL);
925f0706e82SJiri Benc }
926f0706e82SJiri Benc 
927f0706e82SJiri Benc /**
928f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
92944213b5eSJohannes Berg  *
93044213b5eSJohannes Berg  * Returns the number of removed STA entries.
93144213b5eSJohannes Berg  *
932f0706e82SJiri Benc  * @local: local interface data
933d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
934f0706e82SJiri Benc  */
93544213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
936d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
937f0706e82SJiri Benc {
938f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
93944213b5eSJohannes Berg 	int ret = 0;
940f0706e82SJiri Benc 
941d0709a65SJohannes Berg 	might_sleep();
942d0709a65SJohannes Berg 
94334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
944d0709a65SJohannes Berg 
94534e89507SJohannes Berg 	sta_info_finish_pending(local);
94634e89507SJohannes Berg 
94734e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
94834e89507SJohannes Berg 		if (!sdata || sdata == sta->sdata)
94934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
95034e89507SJohannes Berg 	}
95134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
95244213b5eSJohannes Berg 
95344213b5eSJohannes Berg 	return ret;
954f0706e82SJiri Benc }
955dc6676b7SJohannes Berg 
95624723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
95724723d1bSJohannes Berg 			  unsigned long exp_time)
95824723d1bSJohannes Berg {
95924723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
96024723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
96124723d1bSJohannes Berg 
96234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
96324723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
96424723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
96524723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
9660c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
96747846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
96824723d1bSJohannes Berg #endif
96934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
97024723d1bSJohannes Berg 		}
97134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
97224723d1bSJohannes Berg }
97317741cdcSJohannes Berg 
974686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
975686b9cb9SBen Greear 					       const u8 *addr,
976686b9cb9SBen Greear 					       const u8 *localaddr)
97717741cdcSJohannes Berg {
978abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
97917741cdcSJohannes Berg 
980686b9cb9SBen Greear 	/*
981686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
982686b9cb9SBen Greear 	 * ... first in list.
983686b9cb9SBen Greear 	 */
984f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
985686b9cb9SBen Greear 		if (localaddr &&
986686b9cb9SBen Greear 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
987686b9cb9SBen Greear 			continue;
988f7c65594SJohannes Berg 		if (!sta->uploaded)
989f7c65594SJohannes Berg 			return NULL;
99017741cdcSJohannes Berg 		return &sta->sta;
991f7c65594SJohannes Berg 	}
992f7c65594SJohannes Berg 
993abe60632SJohannes Berg 	return NULL;
99417741cdcSJohannes Berg }
995686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
9965ed176e1SJohannes Berg 
9975ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
9985ed176e1SJohannes Berg 					 const u8 *addr)
9995ed176e1SJohannes Berg {
1000f7c65594SJohannes Berg 	struct sta_info *sta;
10015ed176e1SJohannes Berg 
10025ed176e1SJohannes Berg 	if (!vif)
10035ed176e1SJohannes Berg 		return NULL;
10045ed176e1SJohannes Berg 
1005f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1006f7c65594SJohannes Berg 	if (!sta)
1007f7c65594SJohannes Berg 		return NULL;
10085ed176e1SJohannes Berg 
1009f7c65594SJohannes Berg 	if (!sta->uploaded)
1010f7c65594SJohannes Berg 		return NULL;
1011f7c65594SJohannes Berg 
1012f7c65594SJohannes Berg 	return &sta->sta;
10135ed176e1SJohannes Berg }
101417741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1015af818581SJohannes Berg 
101650a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
101750a9432dSJohannes Berg {
101850a9432dSJohannes Berg 	struct sta_info *sta = _sta;
101950a9432dSJohannes Berg 
102050a9432dSJohannes Berg 	clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA);
102150a9432dSJohannes Berg }
102250a9432dSJohannes Berg 
1023af818581SJohannes Berg /* powersave support code */
1024af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1025af818581SJohannes Berg {
1026af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1027af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1028af818581SJohannes Berg 	int sent, buffered;
1029af818581SJohannes Berg 
1030dcf55fb5SFelix Fietkau 	clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
1031d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
103212375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1033af818581SJohannes Berg 
1034af818581SJohannes Berg 	if (!skb_queue_empty(&sta->ps_tx_buf))
1035af818581SJohannes Berg 		sta_info_clear_tim_bit(sta);
1036af818581SJohannes Berg 
1037af818581SJohannes Berg 	/* Send all buffered frames to the station */
1038af818581SJohannes Berg 	sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
103950a9432dSJohannes Berg 	buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf,
104050a9432dSJohannes Berg 						 clear_sta_ps_flags, sta);
1041af818581SJohannes Berg 	sent += buffered;
1042af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1043af818581SJohannes Berg 
1044af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1045af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
104647846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
1047af818581SJohannes Berg 	       sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
1048af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1049af818581SJohannes Berg }
1050af818581SJohannes Berg 
1051af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1052af818581SJohannes Berg {
1053af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1054af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1055af818581SJohannes Berg 	struct sk_buff *skb;
1056af818581SJohannes Berg 	int no_pending_pkts;
1057af818581SJohannes Berg 
1058af818581SJohannes Berg 	skb = skb_dequeue(&sta->tx_filtered);
1059af818581SJohannes Berg 	if (!skb) {
1060af818581SJohannes Berg 		skb = skb_dequeue(&sta->ps_tx_buf);
1061af818581SJohannes Berg 		if (skb)
1062af818581SJohannes Berg 			local->total_ps_buffered--;
1063af818581SJohannes Berg 	}
1064af818581SJohannes Berg 	no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
1065af818581SJohannes Berg 		skb_queue_empty(&sta->ps_tx_buf);
1066af818581SJohannes Berg 
1067af818581SJohannes Berg 	if (skb) {
1068af818581SJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1069af818581SJohannes Berg 		struct ieee80211_hdr *hdr =
1070af818581SJohannes Berg 			(struct ieee80211_hdr *) skb->data;
1071af818581SJohannes Berg 
1072af818581SJohannes Berg 		/*
1073af818581SJohannes Berg 		 * Tell TX path to send this frame even though the STA may
1074af818581SJohannes Berg 		 * still remain is PS mode after this frame exchange.
1075af818581SJohannes Berg 		 */
1076af818581SJohannes Berg 		info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
1077af818581SJohannes Berg 
1078af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1079af818581SJohannes Berg 		printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
1080af818581SJohannes Berg 		       sta->sta.addr, sta->sta.aid,
1081af818581SJohannes Berg 		       skb_queue_len(&sta->ps_tx_buf));
1082af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1083af818581SJohannes Berg 
1084af818581SJohannes Berg 		/* Use MoreData flag to indicate whether there are more
1085af818581SJohannes Berg 		 * buffered frames for this STA */
1086af818581SJohannes Berg 		if (no_pending_pkts)
1087af818581SJohannes Berg 			hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1088af818581SJohannes Berg 		else
1089af818581SJohannes Berg 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1090af818581SJohannes Berg 
1091af818581SJohannes Berg 		ieee80211_add_pending_skb(local, skb);
1092af818581SJohannes Berg 
1093af818581SJohannes Berg 		if (no_pending_pkts)
1094af818581SJohannes Berg 			sta_info_clear_tim_bit(sta);
1095af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1096af818581SJohannes Berg 	} else {
1097af818581SJohannes Berg 		/*
1098af818581SJohannes Berg 		 * FIXME: This can be the result of a race condition between
1099af818581SJohannes Berg 		 *	  us expiring a frame and the station polling for it.
1100af818581SJohannes Berg 		 *	  Should we send it a null-func frame indicating we
1101af818581SJohannes Berg 		 *	  have nothing buffered for it?
1102af818581SJohannes Berg 		 */
1103af818581SJohannes Berg 		printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
1104af818581SJohannes Berg 		       "though there are no buffered frames for it\n",
110547846c9bSJohannes Berg 		       sdata->name, sta->sta.addr);
1106af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1107af818581SJohannes Berg 	}
1108af818581SJohannes Berg }
1109af818581SJohannes Berg 
1110af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1111af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1112af818581SJohannes Berg {
1113af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1114af818581SJohannes Berg 
1115b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1116b5878a2dSJohannes Berg 
1117af818581SJohannes Berg 	if (block)
1118af818581SJohannes Berg 		set_sta_flags(sta, WLAN_STA_PS_DRIVER);
111950a9432dSJohannes Berg 	else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER))
1120af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1121af818581SJohannes Berg }
1122af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1123dcf55fb5SFelix Fietkau 
1124dcf55fb5SFelix Fietkau void ieee80211_sta_set_tim(struct ieee80211_sta *pubsta)
1125dcf55fb5SFelix Fietkau {
1126dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1127dcf55fb5SFelix Fietkau 
1128dcf55fb5SFelix Fietkau 	set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
1129dcf55fb5SFelix Fietkau 	sta_info_set_tim_bit(sta);
1130dcf55fb5SFelix Fietkau }
1131dcf55fb5SFelix Fietkau EXPORT_SYMBOL(ieee80211_sta_set_tim);
1132