xref: /openbmc/linux/net/mac80211/sta_info.c (revision e64b379574d6c92c15b4239ee0a5173317176547)
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
507e189a12SLuis R. Rodriguez  * receive an assocation 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
5234e89507SJohannes Berg  * the peer imediately 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 
7017741cdcSJohannes Berg 	s = local->sta_hash[STA_HASH(sta->sta.addr)];
71f0706e82SJiri Benc 	if (!s)
72be8755e1SMichael Wu 		return -ENOENT;
73be8755e1SMichael Wu 	if (s == sta) {
7417741cdcSJohannes Berg 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
75d0709a65SJohannes Berg 				   s->hnext);
76be8755e1SMichael Wu 		return 0;
77f0706e82SJiri Benc 	}
78f0706e82SJiri Benc 
79be8755e1SMichael Wu 	while (s->hnext && s->hnext != sta)
80f0706e82SJiri Benc 		s = s->hnext;
81be8755e1SMichael Wu 	if (s->hnext) {
82d0709a65SJohannes Berg 		rcu_assign_pointer(s->hnext, sta->hnext);
83be8755e1SMichael Wu 		return 0;
84f0706e82SJiri Benc 	}
85f0706e82SJiri Benc 
86be8755e1SMichael Wu 	return -ENOENT;
87f0706e82SJiri Benc }
88f0706e82SJiri Benc 
89d0709a65SJohannes Berg /* protected by RCU */
90abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
91abe60632SJohannes Berg 			      const u8 *addr)
9243ba7e95SJohannes Berg {
93abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
9443ba7e95SJohannes Berg 	struct sta_info *sta;
9543ba7e95SJohannes Berg 
96d0709a65SJohannes Berg 	sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
9743ba7e95SJohannes Berg 	while (sta) {
98abe60632SJohannes Berg 		if (sta->sdata == sdata &&
99abe60632SJohannes Berg 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
10043ba7e95SJohannes Berg 			break;
101d0709a65SJohannes Berg 		sta = rcu_dereference(sta->hnext);
10243ba7e95SJohannes Berg 	}
10343ba7e95SJohannes Berg 	return sta;
10443ba7e95SJohannes Berg }
10543ba7e95SJohannes Berg 
1060e5ded5aSFelix Fietkau /*
1070e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1080e5ded5aSFelix Fietkau  * or from one of its vlans
1090e5ded5aSFelix Fietkau  */
1100e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1110e5ded5aSFelix Fietkau 				  const u8 *addr)
1120e5ded5aSFelix Fietkau {
1130e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1140e5ded5aSFelix Fietkau 	struct sta_info *sta;
1150e5ded5aSFelix Fietkau 
1160e5ded5aSFelix Fietkau 	sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
1170e5ded5aSFelix Fietkau 	while (sta) {
1180e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
1190e5ded5aSFelix Fietkau 		     sta->sdata->bss == sdata->bss) &&
1200e5ded5aSFelix Fietkau 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1210e5ded5aSFelix Fietkau 			break;
1220e5ded5aSFelix Fietkau 		sta = rcu_dereference(sta->hnext);
1230e5ded5aSFelix Fietkau 	}
1240e5ded5aSFelix Fietkau 	return sta;
1250e5ded5aSFelix Fietkau }
1260e5ded5aSFelix Fietkau 
1273b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1283b53fde8SJohannes Berg 				     int idx)
129ee385855SLuis Carlos Cobo {
1303b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
131ee385855SLuis Carlos Cobo 	struct sta_info *sta;
132ee385855SLuis Carlos Cobo 	int i = 0;
133ee385855SLuis Carlos Cobo 
134d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1353b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1362a8ca29aSLuis Carlos Cobo 			continue;
137ee385855SLuis Carlos Cobo 		if (i < idx) {
138ee385855SLuis Carlos Cobo 			++i;
139ee385855SLuis Carlos Cobo 			continue;
140ee385855SLuis Carlos Cobo 		}
1412a8ca29aSLuis Carlos Cobo 		return sta;
142ee385855SLuis Carlos Cobo 	}
143ee385855SLuis Carlos Cobo 
144ee385855SLuis Carlos Cobo 	return NULL;
145ee385855SLuis Carlos Cobo }
146f0706e82SJiri Benc 
14793e5deb1SJohannes Berg /**
14893e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
14993e5deb1SJohannes Berg  *
1506ef307bcSRandy Dunlap  * @local: pointer to the global information
15193e5deb1SJohannes Berg  * @sta: STA info to free
15293e5deb1SJohannes Berg  *
15393e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
15493e5deb1SJohannes Berg  * that may happen before sta_info_insert().
15593e5deb1SJohannes Berg  */
15693e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
15793e5deb1SJohannes Berg 			    struct sta_info *sta)
15893e5deb1SJohannes Berg {
159af65cd96SJohannes Berg 	if (sta->rate_ctrl) {
1604b7679a5SJohannes Berg 		rate_control_free_sta(sta);
16193e5deb1SJohannes Berg 		rate_control_put(sta->rate_ctrl);
162af65cd96SJohannes Berg 	}
16393e5deb1SJohannes Berg 
16493e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1650c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
1660c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
16793e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
16893e5deb1SJohannes Berg 
16993e5deb1SJohannes Berg 	kfree(sta);
17093e5deb1SJohannes Berg }
17193e5deb1SJohannes Berg 
172d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
173d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
174d0709a65SJohannes Berg 			      struct sta_info *sta)
175f0706e82SJiri Benc {
17617741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
17717741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
178f0706e82SJiri Benc }
179f0706e82SJiri Benc 
180af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
181af818581SJohannes Berg {
182af818581SJohannes Berg 	struct sta_info *sta;
183af818581SJohannes Berg 
184af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
185af818581SJohannes Berg 
186af818581SJohannes Berg 	if (sta->dead)
187af818581SJohannes Berg 		return;
188af818581SJohannes Berg 
189af818581SJohannes Berg 	if (!test_sta_flags(sta, WLAN_STA_PS_STA))
190af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
191af818581SJohannes Berg 	else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
192af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
193af818581SJohannes Berg }
194af818581SJohannes Berg 
195af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
196af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
197af65cd96SJohannes Berg {
198af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
199af65cd96SJohannes Berg 		return 0;
200af65cd96SJohannes Berg 
201af65cd96SJohannes Berg 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
202af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
203af65cd96SJohannes Berg 						     &sta->sta, gfp);
204af65cd96SJohannes Berg 	if (!sta->rate_ctrl_priv) {
205af65cd96SJohannes Berg 		rate_control_put(sta->rate_ctrl);
206af65cd96SJohannes Berg 		return -ENOMEM;
207af65cd96SJohannes Berg 	}
208af65cd96SJohannes Berg 
209af65cd96SJohannes Berg 	return 0;
210af65cd96SJohannes Berg }
211af65cd96SJohannes Berg 
21273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
21373651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
214f0706e82SJiri Benc {
215d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
216f0706e82SJiri Benc 	struct sta_info *sta;
21716c5f15cSRon Rindjunsky 	int i;
218f0706e82SJiri Benc 
21917741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
220f0706e82SJiri Benc 	if (!sta)
22173651ee6SJohannes Berg 		return NULL;
222f0706e82SJiri Benc 
22307346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2245a9f7b04SJohannes Berg 	spin_lock_init(&sta->flaglock);
225af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
22607346f81SJohannes Berg 
22717741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
228d0709a65SJohannes Berg 	sta->local = local;
229d0709a65SJohannes Berg 	sta->sdata = sdata;
230f0706e82SJiri Benc 
231af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
232f0706e82SJiri Benc 		kfree(sta);
23373651ee6SJohannes Berg 		return NULL;
234f0706e82SJiri Benc 	}
235f0706e82SJiri Benc 
23616c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
23716c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
23816c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
23916c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
24016c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
241cee24a3eSRon Rindjunsky 		/* rx */
242cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
243cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i] = NULL;
244cee24a3eSRon Rindjunsky 		/* tx */
245cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
246cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i] = NULL;
247cee24a3eSRon Rindjunsky 		sta->ampdu_mlme.addba_req_num[i] = 0;
24816c5f15cSRon Rindjunsky 	}
249f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
250f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
25173651ee6SJohannes Berg 
252cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
253cccaec98SSenthil Balasubramanian 		sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
254cccaec98SSenthil Balasubramanian 
25573651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2560c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Allocated STA %pM\n",
2570c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
25873651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
25973651ee6SJohannes Berg 
26003e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
261b4e08ea1SLuis Carlos Cobo 	sta->plink_state = PLINK_LISTEN;
26203e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
26303e4497eSJohannes Berg #endif
26403e4497eSJohannes Berg 
26573651ee6SJohannes Berg 	return sta;
26673651ee6SJohannes Berg }
26773651ee6SJohannes Berg 
26834e89507SJohannes Berg static int sta_info_finish_insert(struct sta_info *sta, bool async)
26973651ee6SJohannes Berg {
27073651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
27173651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
27298b62183SJohannes Berg 	struct station_info sinfo;
27373651ee6SJohannes Berg 	unsigned long flags;
27493e5deb1SJohannes Berg 	int err = 0;
27573651ee6SJohannes Berg 
27634e89507SJohannes Berg 	WARN_ON(!mutex_is_locked(&local->sta_mtx));
27734e89507SJohannes Berg 
27834e89507SJohannes Berg 	/* notify driver */
27934e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
28034e89507SJohannes Berg 		sdata = container_of(sdata->bss,
28134e89507SJohannes Berg 				     struct ieee80211_sub_if_data,
28234e89507SJohannes Berg 				     u.ap);
28334e89507SJohannes Berg 	err = drv_sta_add(local, sdata, &sta->sta);
28434e89507SJohannes Berg 	if (err) {
28534e89507SJohannes Berg 		if (!async)
28634e89507SJohannes Berg 			return err;
28734e89507SJohannes Berg 		printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to driver (%d)"
28834e89507SJohannes Berg 				  " - keeping it anyway.\n",
28934e89507SJohannes Berg 		       sdata->name, sta->sta.addr, err);
29034e89507SJohannes Berg 	} else {
29134e89507SJohannes Berg 		sta->uploaded = true;
29234e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
29334e89507SJohannes Berg 		if (async)
29434e89507SJohannes Berg 			printk(KERN_DEBUG "%s: Finished adding IBSS STA %pM\n",
29534e89507SJohannes Berg 			       wiphy_name(local->hw.wiphy), sta->sta.addr);
29634e89507SJohannes Berg #endif
29734e89507SJohannes Berg 	}
29834e89507SJohannes Berg 
29934e89507SJohannes Berg 	sdata = sta->sdata;
30034e89507SJohannes Berg 
30134e89507SJohannes Berg 	if (!async) {
30234e89507SJohannes Berg 		local->num_sta++;
30334e89507SJohannes Berg 		local->sta_generation++;
30434e89507SJohannes Berg 		smp_mb();
30534e89507SJohannes Berg 
30634e89507SJohannes Berg 		/* make the station visible */
30734e89507SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
30834e89507SJohannes Berg 		sta_info_hash_add(local, sta);
30934e89507SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
31034e89507SJohannes Berg 	}
31134e89507SJohannes Berg 
31234e89507SJohannes Berg 	list_add(&sta->list, &local->sta_list);
31334e89507SJohannes Berg 
31434e89507SJohannes Berg 	ieee80211_sta_debugfs_add(sta);
31534e89507SJohannes Berg 	rate_control_add_sta_debugfs(sta);
31634e89507SJohannes Berg 
31734e89507SJohannes Berg 	sinfo.filled = 0;
31834e89507SJohannes Berg 	sinfo.generation = local->sta_generation;
31934e89507SJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
32034e89507SJohannes Berg 
32134e89507SJohannes Berg 
32234e89507SJohannes Berg 	return 0;
32334e89507SJohannes Berg }
32434e89507SJohannes Berg 
32534e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local)
32634e89507SJohannes Berg {
32734e89507SJohannes Berg 	struct sta_info *sta;
32834e89507SJohannes Berg 	unsigned long flags;
32934e89507SJohannes Berg 
33034e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
33134e89507SJohannes Berg 	while (!list_empty(&local->sta_pending_list)) {
33234e89507SJohannes Berg 		sta = list_first_entry(&local->sta_pending_list,
33334e89507SJohannes Berg 				       struct sta_info, list);
33434e89507SJohannes Berg 		list_del(&sta->list);
33534e89507SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
33634e89507SJohannes Berg 
33734e89507SJohannes Berg 		sta_info_finish_insert(sta, true);
33834e89507SJohannes Berg 
33934e89507SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
34034e89507SJohannes Berg 	}
34134e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
34234e89507SJohannes Berg }
34334e89507SJohannes Berg 
34434e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work)
34534e89507SJohannes Berg {
34634e89507SJohannes Berg 	struct ieee80211_local *local =
34734e89507SJohannes Berg 		container_of(work, struct ieee80211_local, sta_finish_work);
34834e89507SJohannes Berg 
34934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
35034e89507SJohannes Berg 	sta_info_finish_pending(local);
35134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
35234e89507SJohannes Berg }
35334e89507SJohannes Berg 
35434e89507SJohannes Berg int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
35534e89507SJohannes Berg {
35634e89507SJohannes Berg 	struct ieee80211_local *local = sta->local;
35734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
35834e89507SJohannes Berg 	unsigned long flags;
35934e89507SJohannes Berg 	int err = 0;
36034e89507SJohannes Berg 
36103e4497eSJohannes Berg 	/*
36203e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
36303e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
36403e4497eSJohannes Berg 	 * and another CPU turns off the net device.
36503e4497eSJohannes Berg 	 */
3669607e6b6SJohannes Berg 	if (unlikely(!ieee80211_sdata_running(sdata))) {
36793e5deb1SJohannes Berg 		err = -ENETDOWN;
36834e89507SJohannes Berg 		rcu_read_lock();
36993e5deb1SJohannes Berg 		goto out_free;
37093e5deb1SJohannes Berg 	}
37103e4497eSJohannes Berg 
37247846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
37317741cdcSJohannes Berg 		    is_multicast_ether_addr(sta->sta.addr))) {
37493e5deb1SJohannes Berg 		err = -EINVAL;
37534e89507SJohannes Berg 		rcu_read_lock();
37693e5deb1SJohannes Berg 		goto out_free;
37793e5deb1SJohannes Berg 	}
37844213b5eSJohannes Berg 
37934e89507SJohannes Berg 	/*
38034e89507SJohannes Berg 	 * In ad-hoc mode, we sometimes need to insert stations
38134e89507SJohannes Berg 	 * from tasklet context from the RX path. To avoid races,
38234e89507SJohannes Berg 	 * always do so in that case -- see the comment below.
38334e89507SJohannes Berg 	 */
38434e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
385d0709a65SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
38643ba7e95SJohannes Berg 		/* check if STA exists already */
38734e89507SJohannes Berg 		if (sta_info_get_bss(sdata, sta->sta.addr)) {
388d0709a65SJohannes Berg 			spin_unlock_irqrestore(&local->sta_lock, flags);
38934e89507SJohannes Berg 			rcu_read_lock();
39093e5deb1SJohannes Berg 			err = -EEXIST;
39193e5deb1SJohannes Berg 			goto out_free;
39243ba7e95SJohannes Berg 		}
39334e89507SJohannes Berg 
394f0706e82SJiri Benc 		local->num_sta++;
39534e89507SJohannes Berg 		local->sta_generation++;
39634e89507SJohannes Berg 		smp_mb();
397f0706e82SJiri Benc 		sta_info_hash_add(local, sta);
39832bfd35dSJohannes Berg 
39934e89507SJohannes Berg 		list_add_tail(&sta->list, &local->sta_pending_list);
40032bfd35dSJohannes Berg 
40134e89507SJohannes Berg 		rcu_read_lock();
40234e89507SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
40334e89507SJohannes Berg 
40434e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
40534e89507SJohannes Berg 		printk(KERN_DEBUG "%s: Added IBSS STA %pM\n",
40634e89507SJohannes Berg 		       wiphy_name(local->hw.wiphy), sta->sta.addr);
40734e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
40834e89507SJohannes Berg 
40934e89507SJohannes Berg 		ieee80211_queue_work(&local->hw, &local->sta_finish_work);
41034e89507SJohannes Berg 
41134e89507SJohannes Berg 		return 0;
41234e89507SJohannes Berg 	}
41334e89507SJohannes Berg 
41434e89507SJohannes Berg 	/*
41534e89507SJohannes Berg 	 * On first glance, this will look racy, because the code
41634e89507SJohannes Berg 	 * below this point, which inserts a station with sleeping,
41734e89507SJohannes Berg 	 * unlocks the sta_lock between checking existence in the
41834e89507SJohannes Berg 	 * hash table and inserting into it.
41934e89507SJohannes Berg 	 *
42034e89507SJohannes Berg 	 * However, it is not racy against itself because it keeps
42134e89507SJohannes Berg 	 * the mutex locked. It still seems to race against the
42234e89507SJohannes Berg 	 * above code that atomically inserts the station... That,
42334e89507SJohannes Berg 	 * however, is not true because the above code can only
42434e89507SJohannes Berg 	 * be invoked for IBSS interfaces, and the below code will
42534e89507SJohannes Berg 	 * not be -- and the two do not race against each other as
42634e89507SJohannes Berg 	 * the hash table also keys off the interface.
42734e89507SJohannes Berg 	 */
42834e89507SJohannes Berg 
42934e89507SJohannes Berg 	might_sleep();
43034e89507SJohannes Berg 
43134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
43234e89507SJohannes Berg 
43334e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
43434e89507SJohannes Berg 	/* check if STA exists already */
43534e89507SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
43634e89507SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
43734e89507SJohannes Berg 		rcu_read_lock();
43834e89507SJohannes Berg 		err = -EEXIST;
43934e89507SJohannes Berg 		goto out_free;
44034e89507SJohannes Berg 	}
44134e89507SJohannes Berg 
44234e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
44334e89507SJohannes Berg 
44434e89507SJohannes Berg 	err = sta_info_finish_insert(sta, false);
44534e89507SJohannes Berg 	if (err) {
44634e89507SJohannes Berg 		mutex_unlock(&local->sta_mtx);
44734e89507SJohannes Berg 		rcu_read_lock();
44834e89507SJohannes Berg 		goto out_free;
44932bfd35dSJohannes Berg 	}
450d0709a65SJohannes Berg 
451f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4520c68ae26SJohannes Berg 	printk(KERN_DEBUG "%s: Inserted STA %pM\n",
4530c68ae26SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
454f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
455f0706e82SJiri Benc 
45634e89507SJohannes Berg 	/* move reference to rcu-protected */
45734e89507SJohannes Berg 	rcu_read_lock();
45834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
459e9f207f0SJiri Benc 
46073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
46173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
46273651ee6SJohannes Berg 
46373651ee6SJohannes Berg 	return 0;
46493e5deb1SJohannes Berg  out_free:
46593e5deb1SJohannes Berg 	BUG_ON(!err);
46693e5deb1SJohannes Berg 	__sta_info_free(local, sta);
46793e5deb1SJohannes Berg 	return err;
468f0706e82SJiri Benc }
469f0706e82SJiri Benc 
47034e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
47134e89507SJohannes Berg {
47234e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
47334e89507SJohannes Berg 
47434e89507SJohannes Berg 	rcu_read_unlock();
47534e89507SJohannes Berg 
47634e89507SJohannes Berg 	return err;
47734e89507SJohannes Berg }
47834e89507SJohannes Berg 
479004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
480004c872eSJohannes Berg {
481004c872eSJohannes Berg 	/*
482004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
483004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
484004c872eSJohannes Berg 	 */
485004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
486004c872eSJohannes Berg }
487004c872eSJohannes Berg 
488004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
489004c872eSJohannes Berg {
490004c872eSJohannes Berg 	/*
491004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
492004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
493004c872eSJohannes Berg 	 */
494004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
495004c872eSJohannes Berg }
496004c872eSJohannes Berg 
497004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
498004c872eSJohannes Berg 				   struct sta_info *sta)
499004c872eSJohannes Berg {
5003e122be0SJohannes Berg 	BUG_ON(!bss);
5013e122be0SJohannes Berg 
50217741cdcSJohannes Berg 	__bss_tim_set(bss, sta->sta.aid);
5033e122be0SJohannes Berg 
504d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
505d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
50624487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, true);
507d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
508d0709a65SJohannes Berg 	}
509004c872eSJohannes Berg }
510004c872eSJohannes Berg 
511004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
512004c872eSJohannes Berg {
513d0709a65SJohannes Berg 	unsigned long flags;
514004c872eSJohannes Berg 
5153e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
5163e122be0SJohannes Berg 
517d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
518d0709a65SJohannes Berg 	__sta_info_set_tim_bit(sta->sdata->bss, sta);
519d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
520004c872eSJohannes Berg }
521004c872eSJohannes Berg 
522004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
523004c872eSJohannes Berg 				     struct sta_info *sta)
524004c872eSJohannes Berg {
5253e122be0SJohannes Berg 	BUG_ON(!bss);
5263e122be0SJohannes Berg 
52717741cdcSJohannes Berg 	__bss_tim_clear(bss, sta->sta.aid);
5283e122be0SJohannes Berg 
529d0709a65SJohannes Berg 	if (sta->local->ops->set_tim) {
530d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = true;
53124487981SJohannes Berg 		drv_set_tim(sta->local, &sta->sta, false);
532d0709a65SJohannes Berg 		sta->local->tim_in_locked_section = false;
533d0709a65SJohannes Berg 	}
534004c872eSJohannes Berg }
535004c872eSJohannes Berg 
536004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
537004c872eSJohannes Berg {
538d0709a65SJohannes Berg 	unsigned long flags;
539004c872eSJohannes Berg 
5403e122be0SJohannes Berg 	BUG_ON(!sta->sdata->bss);
5413e122be0SJohannes Berg 
542d0709a65SJohannes Berg 	spin_lock_irqsave(&sta->local->sta_lock, flags);
543d0709a65SJohannes Berg 	__sta_info_clear_tim_bit(sta->sdata->bss, sta);
544d0709a65SJohannes Berg 	spin_unlock_irqrestore(&sta->local->sta_lock, flags);
545004c872eSJohannes Berg }
546004c872eSJohannes Berg 
54757c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta,
548f0706e82SJiri Benc 				   struct sk_buff *skb)
549f0706e82SJiri Benc {
550e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
551f0706e82SJiri Benc 	int timeout;
552f0706e82SJiri Benc 
553f0706e82SJiri Benc 	if (!skb)
554f0706e82SJiri Benc 		return 0;
555f0706e82SJiri Benc 
556e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
557f0706e82SJiri Benc 
558f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
55957c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
56057c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
56157c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
562f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
563f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
564e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
565f0706e82SJiri Benc }
566f0706e82SJiri Benc 
567f0706e82SJiri Benc 
568f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
569f0706e82SJiri Benc 					     struct sta_info *sta)
570f0706e82SJiri Benc {
571f0706e82SJiri Benc 	unsigned long flags;
572f0706e82SJiri Benc 	struct sk_buff *skb;
573836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
574f0706e82SJiri Benc 
575f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
576f0706e82SJiri Benc 		return;
577f0706e82SJiri Benc 
578f0706e82SJiri Benc 	for (;;) {
579f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
580f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
58157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
582f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
583836341a7SJohannes Berg 		else
584f0706e82SJiri Benc 			skb = NULL;
585f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
586f0706e82SJiri Benc 
587836341a7SJohannes Berg 		if (!skb)
588836341a7SJohannes Berg 			break;
589836341a7SJohannes Berg 
590d0709a65SJohannes Berg 		sdata = sta->sdata;
591f0706e82SJiri Benc 		local->total_ps_buffered--;
592f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
5930c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
5940c68ae26SJohannes Berg 		       sta->sta.addr);
595f4ea83ddSJohannes Berg #endif
596f0706e82SJiri Benc 		dev_kfree_skb(skb);
597836341a7SJohannes Berg 
598004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
599004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
600f0706e82SJiri Benc 	}
601f0706e82SJiri Benc }
602f0706e82SJiri Benc 
60334e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta)
60434e89507SJohannes Berg {
60534e89507SJohannes Berg 	struct ieee80211_local *local;
60634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
60734e89507SJohannes Berg 	struct sk_buff *skb;
60834e89507SJohannes Berg 	unsigned long flags;
60934e89507SJohannes Berg 	int ret, i;
61034e89507SJohannes Berg 
61134e89507SJohannes Berg 	might_sleep();
61234e89507SJohannes Berg 
61334e89507SJohannes Berg 	if (!sta)
61434e89507SJohannes Berg 		return -ENOENT;
61534e89507SJohannes Berg 
61634e89507SJohannes Berg 	local = sta->local;
61734e89507SJohannes Berg 	sdata = sta->sdata;
61834e89507SJohannes Berg 
61934e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
62034e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
62134e89507SJohannes Berg 	/* this might still be the pending list ... which is fine */
62234e89507SJohannes Berg 	if (!ret)
62334e89507SJohannes Berg 		list_del(&sta->list);
62434e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
62534e89507SJohannes Berg 	if (ret)
62634e89507SJohannes Berg 		return ret;
62734e89507SJohannes Berg 
62834e89507SJohannes Berg 	if (sta->key) {
62934e89507SJohannes Berg 		ieee80211_key_free(sta->key);
63034e89507SJohannes Berg 		/*
63134e89507SJohannes Berg 		 * We have only unlinked the key, and actually destroying it
63234e89507SJohannes Berg 		 * may mean it is removed from hardware which requires that
63334e89507SJohannes Berg 		 * the key->sta pointer is still valid, so flush the key todo
63434e89507SJohannes Berg 		 * list here.
63534e89507SJohannes Berg 		 */
63634e89507SJohannes Berg 		ieee80211_key_todo();
63734e89507SJohannes Berg 
63834e89507SJohannes Berg 		WARN_ON(sta->key);
63934e89507SJohannes Berg 	}
64034e89507SJohannes Berg 
64134e89507SJohannes Berg 	sta->dead = true;
64234e89507SJohannes Berg 
64334e89507SJohannes Berg 	if (test_and_clear_sta_flags(sta,
64434e89507SJohannes Berg 				WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
64534e89507SJohannes Berg 		BUG_ON(!sdata->bss);
64634e89507SJohannes Berg 
64734e89507SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
64834e89507SJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, sta);
64934e89507SJohannes Berg 	}
65034e89507SJohannes Berg 
65134e89507SJohannes Berg 	local->num_sta--;
65234e89507SJohannes Berg 	local->sta_generation++;
65334e89507SJohannes Berg 
65434e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
65534e89507SJohannes Berg 		rcu_assign_pointer(sdata->u.vlan.sta, NULL);
65634e89507SJohannes Berg 
65734e89507SJohannes Berg 	if (sta->uploaded) {
65834e89507SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
65934e89507SJohannes Berg 			sdata = container_of(sdata->bss,
66034e89507SJohannes Berg 					     struct ieee80211_sub_if_data,
66134e89507SJohannes Berg 					     u.ap);
66234e89507SJohannes Berg 		drv_sta_remove(local, sdata, &sta->sta);
66334e89507SJohannes Berg 		sdata = sta->sdata;
66434e89507SJohannes Berg 	}
66534e89507SJohannes Berg 
666*e64b3795SJohannes Berg 	/*
667*e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
668*e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
669*e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
670*e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
671*e64b3795SJohannes Berg 	 */
672*e64b3795SJohannes Berg 	synchronize_rcu();
673*e64b3795SJohannes Berg 
67434e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
675*e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
67634e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
67734e89507SJohannes Berg #endif
67834e89507SJohannes Berg 
67934e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
68034e89507SJohannes Berg 	printk(KERN_DEBUG "%s: Removed STA %pM\n",
68134e89507SJohannes Berg 	       wiphy_name(local->hw.wiphy), sta->sta.addr);
68234e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
68334e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
68434e89507SJohannes Berg 
68534e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
68634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
68734e89507SJohannes Berg 
68834e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
68934e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
69034e89507SJohannes Berg 		mesh_plink_deactivate(sta);
69134e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
69234e89507SJohannes Berg 	}
69334e89507SJohannes Berg #endif
69434e89507SJohannes Berg 
69534e89507SJohannes Berg 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
69634e89507SJohannes Berg 		local->total_ps_buffered--;
69734e89507SJohannes Berg 		dev_kfree_skb_any(skb);
69834e89507SJohannes Berg 	}
69934e89507SJohannes Berg 
70034e89507SJohannes Berg 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
70134e89507SJohannes Berg 		dev_kfree_skb_any(skb);
70234e89507SJohannes Berg 
70334e89507SJohannes Berg 	for (i = 0; i <  STA_TID_NUM; i++) {
70434e89507SJohannes Berg 		struct tid_ampdu_rx *tid_rx;
70534e89507SJohannes Berg 		struct tid_ampdu_tx *tid_tx;
70634e89507SJohannes Berg 
70734e89507SJohannes Berg 		spin_lock_bh(&sta->lock);
70834e89507SJohannes Berg 		tid_rx = sta->ampdu_mlme.tid_rx[i];
70934e89507SJohannes Berg 		/* Make sure timer won't free the tid_rx struct, see below */
71034e89507SJohannes Berg 		if (tid_rx)
71134e89507SJohannes Berg 			tid_rx->shutdown = true;
71234e89507SJohannes Berg 
71334e89507SJohannes Berg 		spin_unlock_bh(&sta->lock);
71434e89507SJohannes Berg 
71534e89507SJohannes Berg 		/*
71634e89507SJohannes Berg 		 * Outside spinlock - shutdown is true now so that the timer
71734e89507SJohannes Berg 		 * won't free tid_rx, we have to do that now. Can't let the
71834e89507SJohannes Berg 		 * timer do it because we have to sync the timer outside the
71934e89507SJohannes Berg 		 * lock that it takes itself.
72034e89507SJohannes Berg 		 */
72134e89507SJohannes Berg 		if (tid_rx) {
72234e89507SJohannes Berg 			del_timer_sync(&tid_rx->session_timer);
72334e89507SJohannes Berg 			kfree(tid_rx);
72434e89507SJohannes Berg 		}
72534e89507SJohannes Berg 
72634e89507SJohannes Berg 		/*
72734e89507SJohannes Berg 		 * No need to do such complications for TX agg sessions, the
72834e89507SJohannes Berg 		 * path leading to freeing the tid_tx struct goes via a call
72934e89507SJohannes Berg 		 * from the driver, and thus needs to look up the sta struct
73034e89507SJohannes Berg 		 * again, which cannot be found when we get here. Hence, we
73134e89507SJohannes Berg 		 * just need to delete the timer and free the aggregation
73234e89507SJohannes Berg 		 * info; we won't be telling the peer about it then but that
73334e89507SJohannes Berg 		 * doesn't matter if we're not talking to it again anyway.
73434e89507SJohannes Berg 		 */
73534e89507SJohannes Berg 		tid_tx = sta->ampdu_mlme.tid_tx[i];
73634e89507SJohannes Berg 		if (tid_tx) {
73734e89507SJohannes Berg 			del_timer_sync(&tid_tx->addba_resp_timer);
73834e89507SJohannes Berg 			/*
73934e89507SJohannes Berg 			 * STA removed while aggregation session being
74034e89507SJohannes Berg 			 * started? Bit odd, but purge frames anyway.
74134e89507SJohannes Berg 			 */
74234e89507SJohannes Berg 			skb_queue_purge(&tid_tx->pending);
74334e89507SJohannes Berg 			kfree(tid_tx);
74434e89507SJohannes Berg 		}
74534e89507SJohannes Berg 	}
74634e89507SJohannes Berg 
74734e89507SJohannes Berg 	__sta_info_free(local, sta);
74834e89507SJohannes Berg 
74934e89507SJohannes Berg 	return 0;
75034e89507SJohannes Berg }
75134e89507SJohannes Berg 
75234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
75334e89507SJohannes Berg {
75434e89507SJohannes Berg 	struct sta_info *sta;
75534e89507SJohannes Berg 	int ret;
75634e89507SJohannes Berg 
75734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
75834e89507SJohannes Berg 	sta = sta_info_get(sdata, addr);
75934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
76034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
76134e89507SJohannes Berg 
76234e89507SJohannes Berg 	return ret;
76334e89507SJohannes Berg }
76434e89507SJohannes Berg 
76534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
76634e89507SJohannes Berg 			      const u8 *addr)
76734e89507SJohannes Berg {
76834e89507SJohannes Berg 	struct sta_info *sta;
76934e89507SJohannes Berg 	int ret;
77034e89507SJohannes Berg 
77134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
77234e89507SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
77334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
77434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
77534e89507SJohannes Berg 
77634e89507SJohannes Berg 	return ret;
77734e89507SJohannes Berg }
778f0706e82SJiri Benc 
779f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
780f0706e82SJiri Benc {
781f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
782f0706e82SJiri Benc 	struct sta_info *sta;
783f0706e82SJiri Benc 
784d0709a65SJohannes Berg 	rcu_read_lock();
785d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
786f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
787d0709a65SJohannes Berg 	rcu_read_unlock();
788f0706e82SJiri Benc 
7895bb644a0SJohannes Berg 	if (local->quiescing)
7905bb644a0SJohannes Berg 		return;
7915bb644a0SJohannes Berg 
7920d174406SJohannes Berg 	local->sta_cleanup.expires =
7930d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
794f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
795f0706e82SJiri Benc }
796f0706e82SJiri Benc 
797f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
798f0706e82SJiri Benc {
799d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
80034e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
801f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
80234e89507SJohannes Berg 	INIT_LIST_HEAD(&local->sta_pending_list);
80334e89507SJohannes Berg 	INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
804f0706e82SJiri Benc 
805b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
806b24b8a24SPavel Emelyanov 		    (unsigned long)local);
8070d174406SJohannes Berg 	local->sta_cleanup.expires =
8080d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
809f0706e82SJiri Benc }
810f0706e82SJiri Benc 
811f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
812f0706e82SJiri Benc {
813f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
814f0706e82SJiri Benc 	return 0;
815f0706e82SJiri Benc }
816f0706e82SJiri Benc 
817f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
818f0706e82SJiri Benc {
819f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
820be8755e1SMichael Wu 	sta_info_flush(local, NULL);
821f0706e82SJiri Benc }
822f0706e82SJiri Benc 
823f0706e82SJiri Benc /**
824f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
82544213b5eSJohannes Berg  *
82644213b5eSJohannes Berg  * Returns the number of removed STA entries.
82744213b5eSJohannes Berg  *
828f0706e82SJiri Benc  * @local: local interface data
829d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
830f0706e82SJiri Benc  */
83144213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
832d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
833f0706e82SJiri Benc {
834f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
83544213b5eSJohannes Berg 	int ret = 0;
836f0706e82SJiri Benc 
837d0709a65SJohannes Berg 	might_sleep();
838d0709a65SJohannes Berg 
83934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
840d0709a65SJohannes Berg 
84134e89507SJohannes Berg 	sta_info_finish_pending(local);
84234e89507SJohannes Berg 
84334e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
84434e89507SJohannes Berg 		if (!sdata || sdata == sta->sdata)
84534e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
84634e89507SJohannes Berg 	}
84734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
84844213b5eSJohannes Berg 
84944213b5eSJohannes Berg 	return ret;
850f0706e82SJiri Benc }
851dc6676b7SJohannes Berg 
85224723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
85324723d1bSJohannes Berg 			  unsigned long exp_time)
85424723d1bSJohannes Berg {
85524723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
85624723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
85724723d1bSJohannes Berg 
85834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
85924723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
86024723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
86124723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
8620c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
86347846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
86424723d1bSJohannes Berg #endif
86534e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
86624723d1bSJohannes Berg 		}
86734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
86824723d1bSJohannes Berg }
86917741cdcSJohannes Berg 
8705ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
87117741cdcSJohannes Berg 					       const u8 *addr)
87217741cdcSJohannes Berg {
873abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
87417741cdcSJohannes Berg 
875abe60632SJohannes Berg 	/* Just return a random station ... first in list ... */
876abe60632SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt)
87717741cdcSJohannes Berg 		return &sta->sta;
878abe60632SJohannes Berg 	return NULL;
87917741cdcSJohannes Berg }
8805ed176e1SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
8815ed176e1SJohannes Berg 
8825ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
8835ed176e1SJohannes Berg 					 const u8 *addr)
8845ed176e1SJohannes Berg {
8855ed176e1SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8865ed176e1SJohannes Berg 
8875ed176e1SJohannes Berg 	if (!vif)
8885ed176e1SJohannes Berg 		return NULL;
8895ed176e1SJohannes Berg 
8905ed176e1SJohannes Berg 	sdata = vif_to_sdata(vif);
8915ed176e1SJohannes Berg 
8925ed176e1SJohannes Berg 	return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
8935ed176e1SJohannes Berg }
89417741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
895af818581SJohannes Berg 
896af818581SJohannes Berg /* powersave support code */
897af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
898af818581SJohannes Berg {
899af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
900af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
901af818581SJohannes Berg 	int sent, buffered;
902af818581SJohannes Berg 
90312375ef9SJohannes Berg 	drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
904af818581SJohannes Berg 
905af818581SJohannes Berg 	if (!skb_queue_empty(&sta->ps_tx_buf))
906af818581SJohannes Berg 		sta_info_clear_tim_bit(sta);
907af818581SJohannes Berg 
908af818581SJohannes Berg 	/* Send all buffered frames to the station */
909af818581SJohannes Berg 	sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
910af818581SJohannes Berg 	buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
911af818581SJohannes Berg 	sent += buffered;
912af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
913af818581SJohannes Berg 
914af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
915af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
91647846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
917af818581SJohannes Berg 	       sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
918af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
919af818581SJohannes Berg }
920af818581SJohannes Berg 
921af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
922af818581SJohannes Berg {
923af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
924af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
925af818581SJohannes Berg 	struct sk_buff *skb;
926af818581SJohannes Berg 	int no_pending_pkts;
927af818581SJohannes Berg 
928af818581SJohannes Berg 	skb = skb_dequeue(&sta->tx_filtered);
929af818581SJohannes Berg 	if (!skb) {
930af818581SJohannes Berg 		skb = skb_dequeue(&sta->ps_tx_buf);
931af818581SJohannes Berg 		if (skb)
932af818581SJohannes Berg 			local->total_ps_buffered--;
933af818581SJohannes Berg 	}
934af818581SJohannes Berg 	no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
935af818581SJohannes Berg 		skb_queue_empty(&sta->ps_tx_buf);
936af818581SJohannes Berg 
937af818581SJohannes Berg 	if (skb) {
938af818581SJohannes Berg 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
939af818581SJohannes Berg 		struct ieee80211_hdr *hdr =
940af818581SJohannes Berg 			(struct ieee80211_hdr *) skb->data;
941af818581SJohannes Berg 
942af818581SJohannes Berg 		/*
943af818581SJohannes Berg 		 * Tell TX path to send this frame even though the STA may
944af818581SJohannes Berg 		 * still remain is PS mode after this frame exchange.
945af818581SJohannes Berg 		 */
946af818581SJohannes Berg 		info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
947af818581SJohannes Berg 
948af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
949af818581SJohannes Berg 		printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
950af818581SJohannes Berg 		       sta->sta.addr, sta->sta.aid,
951af818581SJohannes Berg 		       skb_queue_len(&sta->ps_tx_buf));
952af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
953af818581SJohannes Berg 
954af818581SJohannes Berg 		/* Use MoreData flag to indicate whether there are more
955af818581SJohannes Berg 		 * buffered frames for this STA */
956af818581SJohannes Berg 		if (no_pending_pkts)
957af818581SJohannes Berg 			hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
958af818581SJohannes Berg 		else
959af818581SJohannes Berg 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
960af818581SJohannes Berg 
961af818581SJohannes Berg 		ieee80211_add_pending_skb(local, skb);
962af818581SJohannes Berg 
963af818581SJohannes Berg 		if (no_pending_pkts)
964af818581SJohannes Berg 			sta_info_clear_tim_bit(sta);
965af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
966af818581SJohannes Berg 	} else {
967af818581SJohannes Berg 		/*
968af818581SJohannes Berg 		 * FIXME: This can be the result of a race condition between
969af818581SJohannes Berg 		 *	  us expiring a frame and the station polling for it.
970af818581SJohannes Berg 		 *	  Should we send it a null-func frame indicating we
971af818581SJohannes Berg 		 *	  have nothing buffered for it?
972af818581SJohannes Berg 		 */
973af818581SJohannes Berg 		printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
974af818581SJohannes Berg 		       "though there are no buffered frames for it\n",
97547846c9bSJohannes Berg 		       sdata->name, sta->sta.addr);
976af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
977af818581SJohannes Berg 	}
978af818581SJohannes Berg }
979af818581SJohannes Berg 
980af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
981af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
982af818581SJohannes Berg {
983af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
984af818581SJohannes Berg 
985af818581SJohannes Berg 	if (block)
986af818581SJohannes Berg 		set_sta_flags(sta, WLAN_STA_PS_DRIVER);
987af818581SJohannes Berg 	else
988af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
989af818581SJohannes Berg }
990af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
991