xref: /openbmc/linux/net/mac80211/sta_info.c (revision 02f2f1a951f87644166926862ec32fb13511e2f3)
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"
27ce662b44SJohannes Berg #include "wme.h"
28f0706e82SJiri Benc 
29d0709a65SJohannes Berg /**
30d0709a65SJohannes Berg  * DOC: STA information lifetime rules
31d0709a65SJohannes Berg  *
32d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
33d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
34d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
35d0709a65SJohannes Berg  *
3634e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3734e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3834e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
3934e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4034e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4134e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4234e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4334e89507SJohannes Berg  * encryption keys.
4493e5deb1SJohannes Berg  *
4593e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4693e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
47d0709a65SJohannes Berg  *
4834e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
497e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
507e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5125985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5234e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5325985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5434e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
557e189a12SLuis R. Rodriguez  *
5634e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5734e89507SJohannes Berg  * calls are available.
58d0709a65SJohannes Berg  *
5934e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6034e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6134e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6234e89507SJohannes Berg  * freed before they are done using it.
63d0709a65SJohannes Berg  */
64f0706e82SJiri Benc 
654d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
66be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
67f0706e82SJiri Benc 			     struct sta_info *sta)
68f0706e82SJiri Benc {
69f0706e82SJiri Benc 	struct sta_info *s;
70f0706e82SJiri Benc 
7140b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
724d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
73f0706e82SJiri Benc 	if (!s)
74be8755e1SMichael Wu 		return -ENOENT;
75be8755e1SMichael Wu 	if (s == sta) {
76cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
77d0709a65SJohannes Berg 				   s->hnext);
78be8755e1SMichael Wu 		return 0;
79f0706e82SJiri Benc 	}
80f0706e82SJiri Benc 
8140b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8240b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8340b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
844d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8540b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
86cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
87be8755e1SMichael Wu 		return 0;
88f0706e82SJiri Benc 	}
89f0706e82SJiri Benc 
90be8755e1SMichael Wu 	return -ENOENT;
91f0706e82SJiri Benc }
92f0706e82SJiri Benc 
93d0709a65SJohannes Berg /* protected by RCU */
94abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
95abe60632SJohannes Berg 			      const u8 *addr)
9643ba7e95SJohannes Berg {
97abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
9843ba7e95SJohannes Berg 	struct sta_info *sta;
9943ba7e95SJohannes Berg 
1000379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1010379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
10243ba7e95SJohannes Berg 	while (sta) {
103abe60632SJohannes Berg 		if (sta->sdata == sdata &&
104abe60632SJohannes Berg 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
10543ba7e95SJohannes Berg 			break;
1060379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1070379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
10843ba7e95SJohannes Berg 	}
10943ba7e95SJohannes Berg 	return sta;
11043ba7e95SJohannes Berg }
11143ba7e95SJohannes Berg 
1120e5ded5aSFelix Fietkau /*
1130e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1140e5ded5aSFelix Fietkau  * or from one of its vlans
1150e5ded5aSFelix Fietkau  */
1160e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1170e5ded5aSFelix Fietkau 				  const u8 *addr)
1180e5ded5aSFelix Fietkau {
1190e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1200e5ded5aSFelix Fietkau 	struct sta_info *sta;
1210e5ded5aSFelix Fietkau 
1220379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1230379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1240e5ded5aSFelix Fietkau 	while (sta) {
1250e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
126a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
1270e5ded5aSFelix Fietkau 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1280e5ded5aSFelix Fietkau 			break;
1290379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1300379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1310e5ded5aSFelix Fietkau 	}
1320e5ded5aSFelix Fietkau 	return sta;
1330e5ded5aSFelix Fietkau }
1340e5ded5aSFelix Fietkau 
1353b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1363b53fde8SJohannes Berg 				     int idx)
137ee385855SLuis Carlos Cobo {
1383b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
139ee385855SLuis Carlos Cobo 	struct sta_info *sta;
140ee385855SLuis Carlos Cobo 	int i = 0;
141ee385855SLuis Carlos Cobo 
142d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1433b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1442a8ca29aSLuis Carlos Cobo 			continue;
145ee385855SLuis Carlos Cobo 		if (i < idx) {
146ee385855SLuis Carlos Cobo 			++i;
147ee385855SLuis Carlos Cobo 			continue;
148ee385855SLuis Carlos Cobo 		}
1492a8ca29aSLuis Carlos Cobo 		return sta;
150ee385855SLuis Carlos Cobo 	}
151ee385855SLuis Carlos Cobo 
152ee385855SLuis Carlos Cobo 	return NULL;
153ee385855SLuis Carlos Cobo }
154f0706e82SJiri Benc 
15593e5deb1SJohannes Berg /**
156d9a7ddb0SJohannes Berg  * sta_info_free - free STA
15793e5deb1SJohannes Berg  *
1586ef307bcSRandy Dunlap  * @local: pointer to the global information
15993e5deb1SJohannes Berg  * @sta: STA info to free
16093e5deb1SJohannes Berg  *
16193e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
162d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
163d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
164d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
16593e5deb1SJohannes Berg  */
166d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
16793e5deb1SJohannes Berg {
168889cbb91SJohannes Berg 	if (sta->rate_ctrl)
1694b7679a5SJohannes Berg 		rate_control_free_sta(sta);
17093e5deb1SJohannes Berg 
17193e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1720fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
17393e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
17493e5deb1SJohannes Berg 
17593e5deb1SJohannes Berg 	kfree(sta);
17693e5deb1SJohannes Berg }
17793e5deb1SJohannes Berg 
1784d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
179d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
180d0709a65SJohannes Berg 			      struct sta_info *sta)
181f0706e82SJiri Benc {
1824d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
18317741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
184cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
185f0706e82SJiri Benc }
186f0706e82SJiri Benc 
187af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
188af818581SJohannes Berg {
189af818581SJohannes Berg 	struct sta_info *sta;
190af818581SJohannes Berg 
191af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
192af818581SJohannes Berg 
193af818581SJohannes Berg 	if (sta->dead)
194af818581SJohannes Berg 		return;
195af818581SJohannes Berg 
19654420473SHelmut Schaa 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
19754420473SHelmut Schaa 		local_bh_disable();
198af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
19954420473SHelmut Schaa 		local_bh_enable();
20054420473SHelmut Schaa 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
201c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
202ce662b44SJohannes Berg 
203ce662b44SJohannes Berg 		local_bh_disable();
204af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
205ce662b44SJohannes Berg 		local_bh_enable();
206c2c98fdeSJohannes Berg 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
207c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
208ce662b44SJohannes Berg 
209ce662b44SJohannes Berg 		local_bh_disable();
21047086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
211ce662b44SJohannes Berg 		local_bh_enable();
21250a9432dSJohannes Berg 	} else
213c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
214af818581SJohannes Berg }
215af818581SJohannes Berg 
216af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
217af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
218af65cd96SJohannes Berg {
219af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
220af65cd96SJohannes Berg 		return 0;
221af65cd96SJohannes Berg 
222889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
223af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
224af65cd96SJohannes Berg 						     &sta->sta, gfp);
225889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
226af65cd96SJohannes Berg 		return -ENOMEM;
227af65cd96SJohannes Berg 
228af65cd96SJohannes Berg 	return 0;
229af65cd96SJohannes Berg }
230af65cd96SJohannes Berg 
23173651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
23256544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
233f0706e82SJiri Benc {
234d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
235f0706e82SJiri Benc 	struct sta_info *sta;
236ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
23716c5f15cSRon Rindjunsky 	int i;
238f0706e82SJiri Benc 
23917741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
240f0706e82SJiri Benc 	if (!sta)
24173651ee6SJohannes Berg 		return NULL;
242f0706e82SJiri Benc 
24307346f81SJohannes Berg 	spin_lock_init(&sta->lock);
244af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
24567c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
246a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
24707346f81SJohannes Berg 
24817741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
249d0709a65SJohannes Berg 	sta->local = local;
250d0709a65SJohannes Berg 	sta->sdata = sdata;
2518bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
252f0706e82SJiri Benc 
25371ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
25471ec375cSJohannes Berg 
255ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
256ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
257541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
258541a45a1SBruno Randolf 
259af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
260f0706e82SJiri Benc 		kfree(sta);
26173651ee6SJohannes Berg 		return NULL;
262f0706e82SJiri Benc 	}
263f0706e82SJiri Benc 
26416c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
265a622ab72SJohannes Berg 		/*
266a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
267a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
268a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
269a622ab72SJohannes Berg 		 */
27016c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
27116c5f15cSRon Rindjunsky 	}
272948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
273948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
274948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
275948d887dSJohannes Berg 	}
27673651ee6SJohannes Berg 
277cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
2784be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
279cccaec98SSenthil Balasubramanian 
28073651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2810fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
28273651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
28373651ee6SJohannes Berg 
28403e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
28557cf8043SJavier Cardona 	sta->plink_state = NL80211_PLINK_LISTEN;
28603e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
28703e4497eSJohannes Berg #endif
28803e4497eSJohannes Berg 
28973651ee6SJohannes Berg 	return sta;
29073651ee6SJohannes Berg }
29173651ee6SJohannes Berg 
2928c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
29334e89507SJohannes Berg {
29434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
29534e89507SJohannes Berg 
29603e4497eSJohannes Berg 	/*
29703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
29803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
29903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
30003e4497eSJohannes Berg 	 */
3018c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
3028c71df7aSGuy Eilam 		return -ENETDOWN;
30303e4497eSJohannes Berg 
30447846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
3058c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
3068c71df7aSGuy Eilam 		return -EINVAL;
3078c71df7aSGuy Eilam 
3088c71df7aSGuy Eilam 	return 0;
30993e5deb1SJohannes Berg }
31044213b5eSJohannes Berg 
311f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
312f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
313f09603a2SJohannes Berg 				     struct sta_info *sta)
314f09603a2SJohannes Berg {
315f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
316f09603a2SJohannes Berg 	int err = 0;
317f09603a2SJohannes Berg 
318f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
319f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
320f09603a2SJohannes Berg 		if (err)
321f09603a2SJohannes Berg 			break;
322f09603a2SJohannes Berg 	}
323f09603a2SJohannes Berg 
324f09603a2SJohannes Berg 	if (!err) {
325a4ec45a4SJohannes Berg 		/*
326a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
327a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
328a4ec45a4SJohannes Berg 		 */
329a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
330f09603a2SJohannes Berg 			sta->uploaded = true;
331f09603a2SJohannes Berg 		return 0;
332f09603a2SJohannes Berg 	}
333f09603a2SJohannes Berg 
334f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
335f09603a2SJohannes Berg 		printk(KERN_DEBUG
336f09603a2SJohannes Berg 		       "%s: failed to move IBSS STA %pM to state %d (%d) - keeping it anyway.\n",
337f09603a2SJohannes Berg 		       sdata->name, sta->sta.addr, state + 1, err);
338f09603a2SJohannes Berg 		err = 0;
339f09603a2SJohannes Berg 	}
340f09603a2SJohannes Berg 
341f09603a2SJohannes Berg 	/* unwind on error */
342f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
343f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
344f09603a2SJohannes Berg 
345f09603a2SJohannes Berg 	return err;
346f09603a2SJohannes Berg }
347f09603a2SJohannes Berg 
34834e89507SJohannes Berg /*
3498c71df7aSGuy Eilam  * should be called with sta_mtx locked
3508c71df7aSGuy Eilam  * this function replaces the mutex lock
3518c71df7aSGuy Eilam  * with a RCU lock
3528c71df7aSGuy Eilam  */
3534d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
3548c71df7aSGuy Eilam {
3558c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
3568c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3577852e361SJohannes Berg 	struct station_info sinfo;
3588c71df7aSGuy Eilam 	int err = 0;
3598c71df7aSGuy Eilam 
3608c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
3618c71df7aSGuy Eilam 
3627852e361SJohannes Berg 	/* check if STA exists already */
3637852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
3644d33960bSJohannes Berg 		err = -EEXIST;
3654d33960bSJohannes Berg 		goto out_err;
36634e89507SJohannes Berg 	}
36734e89507SJohannes Berg 
3684d33960bSJohannes Berg 	/* notify driver */
369f09603a2SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
370f09603a2SJohannes Berg 	if (err)
371f09603a2SJohannes Berg 		goto out_err;
3724d33960bSJohannes Berg 
3734d33960bSJohannes Berg 	local->num_sta++;
3744d33960bSJohannes Berg 	local->sta_generation++;
3754d33960bSJohannes Berg 	smp_mb();
3764d33960bSJohannes Berg 
3774d33960bSJohannes Berg 	/* make the station visible */
3784d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
3794d33960bSJohannes Berg 
3804d33960bSJohannes Berg 	list_add(&sta->list, &local->sta_list);
38183d5cc01SJohannes Berg 
38283d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
3834d33960bSJohannes Berg 
3844d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
3854d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
3864d33960bSJohannes Berg 
3874d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
3884d33960bSJohannes Berg 	sinfo.filled = 0;
3894d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
3904d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
391d0709a65SJohannes Berg 
392f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3937852e361SJohannes Berg 	wiphy_debug(local->hw.wiphy, "Inserted STA %pM\n", sta->sta.addr);
394f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
395f0706e82SJiri Benc 
39634e89507SJohannes Berg 	/* move reference to rcu-protected */
39734e89507SJohannes Berg 	rcu_read_lock();
39834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
399e9f207f0SJiri Benc 
40073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
40173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
40273651ee6SJohannes Berg 
40373651ee6SJohannes Berg 	return 0;
4044d33960bSJohannes Berg  out_err:
4054d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
4064d33960bSJohannes Berg 	rcu_read_lock();
4074d33960bSJohannes Berg 	return err;
4088c71df7aSGuy Eilam }
4098c71df7aSGuy Eilam 
4108c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
4118c71df7aSGuy Eilam {
4128c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4138c71df7aSGuy Eilam 	int err = 0;
4148c71df7aSGuy Eilam 
4154d33960bSJohannes Berg 	might_sleep();
4164d33960bSJohannes Berg 
4178c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
4188c71df7aSGuy Eilam 	if (err) {
4198c71df7aSGuy Eilam 		rcu_read_lock();
4208c71df7aSGuy Eilam 		goto out_free;
4218c71df7aSGuy Eilam 	}
4228c71df7aSGuy Eilam 
423004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
424004c872eSJohannes Berg 
4254d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
4268c71df7aSGuy Eilam 	if (err)
427004c872eSJohannes Berg 		goto out_free;
428d0709a65SJohannes Berg 
429004c872eSJohannes Berg 	return 0;
43093e5deb1SJohannes Berg  out_free:
43193e5deb1SJohannes Berg 	BUG_ON(!err);
432d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
43393e5deb1SJohannes Berg 	return err;
434f0706e82SJiri Benc }
435f0706e82SJiri Benc 
43634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
43734e89507SJohannes Berg {
43834e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
43934e89507SJohannes Berg 
44034e89507SJohannes Berg 	rcu_read_unlock();
44134e89507SJohannes Berg 
44234e89507SJohannes Berg 	return err;
44334e89507SJohannes Berg }
44434e89507SJohannes Berg 
445f0706e82SJiri Benc static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
446004c872eSJohannes Berg {
447004c872eSJohannes Berg 	/*
448004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
449004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
450004c872eSJohannes Berg 	 */
451004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
452004c872eSJohannes Berg }
453004c872eSJohannes Berg 
454004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
455004c872eSJohannes Berg {
456004c872eSJohannes Berg 	/*
457004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
458004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
459004c872eSJohannes Berg 	 */
460004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
461004c872eSJohannes Berg }
462004c872eSJohannes Berg 
463948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
464004c872eSJohannes Berg {
465948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
466948d887dSJohannes Berg 	switch (ac) {
467948d887dSJohannes Berg 	case IEEE80211_AC_VO:
468948d887dSJohannes Berg 		return BIT(6) | BIT(7);
469948d887dSJohannes Berg 	case IEEE80211_AC_VI:
470948d887dSJohannes Berg 		return BIT(4) | BIT(5);
471948d887dSJohannes Berg 	case IEEE80211_AC_BE:
472948d887dSJohannes Berg 		return BIT(0) | BIT(3);
473948d887dSJohannes Berg 	case IEEE80211_AC_BK:
474948d887dSJohannes Berg 		return BIT(1) | BIT(2);
475948d887dSJohannes Berg 	default:
476948d887dSJohannes Berg 		WARN_ON(1);
477948d887dSJohannes Berg 		return 0;
478d0709a65SJohannes Berg 	}
479004c872eSJohannes Berg }
480004c872eSJohannes Berg 
481c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
482004c872eSJohannes Berg {
483c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
484c868cb35SJohannes Berg 	struct ieee80211_if_ap *bss = sta->sdata->bss;
485d0709a65SJohannes Berg 	unsigned long flags;
486948d887dSJohannes Berg 	bool indicate_tim = false;
487948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
488948d887dSJohannes Berg 	int ac;
489004c872eSJohannes Berg 
490c868cb35SJohannes Berg 	if (WARN_ON_ONCE(!sta->sdata->bss))
491c868cb35SJohannes Berg 		return;
4923e122be0SJohannes Berg 
493c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
494c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
495c868cb35SJohannes Berg 		return;
496004c872eSJohannes Berg 
497c868cb35SJohannes Berg 	if (sta->dead)
498c868cb35SJohannes Berg 		goto done;
4993e122be0SJohannes Berg 
500948d887dSJohannes Berg 	/*
501948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
502948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
503948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
504948d887dSJohannes Berg 	 * non-enabled ones.
505948d887dSJohannes Berg 	 */
506948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
507948d887dSJohannes Berg 		ignore_for_tim = 0;
508948d887dSJohannes Berg 
509948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
510948d887dSJohannes Berg 		unsigned long tids;
511948d887dSJohannes Berg 
512948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
513948d887dSJohannes Berg 			continue;
514948d887dSJohannes Berg 
515948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
516948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
517948d887dSJohannes Berg 		if (indicate_tim)
518948d887dSJohannes Berg 			break;
519948d887dSJohannes Berg 
520948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
521948d887dSJohannes Berg 
522948d887dSJohannes Berg 		indicate_tim |=
523948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
524004c872eSJohannes Berg 	}
525004c872eSJohannes Berg 
526c868cb35SJohannes Berg  done:
5274d33960bSJohannes Berg 	spin_lock_irqsave(&local->tim_lock, flags);
528004c872eSJohannes Berg 
529948d887dSJohannes Berg 	if (indicate_tim)
530c868cb35SJohannes Berg 		__bss_tim_set(bss, sta->sta.aid);
531c868cb35SJohannes Berg 	else
53217741cdcSJohannes Berg 		__bss_tim_clear(bss, sta->sta.aid);
5333e122be0SJohannes Berg 
534c868cb35SJohannes Berg 	if (local->ops->set_tim) {
535c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
536948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
537c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
538004c872eSJohannes Berg 	}
539004c872eSJohannes Berg 
5404d33960bSJohannes Berg 	spin_unlock_irqrestore(&local->tim_lock, flags);
541004c872eSJohannes Berg }
542004c872eSJohannes Berg 
543cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
544f0706e82SJiri Benc {
545e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
546f0706e82SJiri Benc 	int timeout;
547f0706e82SJiri Benc 
548f0706e82SJiri Benc 	if (!skb)
549cd0b8d89SJohannes Berg 		return false;
550f0706e82SJiri Benc 
551e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
552f0706e82SJiri Benc 
553f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
55457c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
55557c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
55657c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
557f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
558f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
559e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
560f0706e82SJiri Benc }
561f0706e82SJiri Benc 
562f0706e82SJiri Benc 
563948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
564948d887dSJohannes Berg 						struct sta_info *sta, int ac)
565f0706e82SJiri Benc {
566f0706e82SJiri Benc 	unsigned long flags;
567f0706e82SJiri Benc 	struct sk_buff *skb;
568f0706e82SJiri Benc 
56960750397SJohannes Berg 	/*
57060750397SJohannes Berg 	 * First check for frames that should expire on the filtered
57160750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
57260750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
57360750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
57460750397SJohannes Berg 	 * total_ps_buffered counter.
57560750397SJohannes Berg 	 */
576f0706e82SJiri Benc 	for (;;) {
577948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
578948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
57957c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
580948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
581836341a7SJohannes Berg 		else
582f0706e82SJiri Benc 			skb = NULL;
583948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
584f0706e82SJiri Benc 
58560750397SJohannes Berg 		/*
58660750397SJohannes Berg 		 * Frames are queued in order, so if this one
58760750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
58860750397SJohannes Berg 		 * we actually reached the end of the queue we
58960750397SJohannes Berg 		 * also need to stop, of course.
59060750397SJohannes Berg 		 */
59160750397SJohannes Berg 		if (!skb)
59260750397SJohannes Berg 			break;
59360750397SJohannes Berg 		dev_kfree_skb(skb);
59460750397SJohannes Berg 	}
59560750397SJohannes Berg 
59660750397SJohannes Berg 	/*
59760750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
59860750397SJohannes Berg 	 * only find something if the filtered queue was emptied
59960750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
60060750397SJohannes Berg 	 * buffered frames.
60160750397SJohannes Berg 	 */
602f0706e82SJiri Benc 	for (;;) {
603948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
604948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
605f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
606948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
607f0706e82SJiri Benc 		else
608f0706e82SJiri Benc 			skb = NULL;
609948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
610f0706e82SJiri Benc 
61160750397SJohannes Berg 		/*
61260750397SJohannes Berg 		 * frames are queued in order, so if this one
61360750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
61460750397SJohannes Berg 		 * the queue) we can stop testing
61560750397SJohannes Berg 		 */
616836341a7SJohannes Berg 		if (!skb)
617836341a7SJohannes Berg 			break;
618836341a7SJohannes Berg 
619f0706e82SJiri Benc 		local->total_ps_buffered--;
620f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
6210c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
6220c68ae26SJohannes Berg 		       sta->sta.addr);
623f4ea83ddSJohannes Berg #endif
624f0706e82SJiri Benc 		dev_kfree_skb(skb);
625f0706e82SJiri Benc 	}
6263393a608SJuuso Oikarinen 
62760750397SJohannes Berg 	/*
62860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
62960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
63060750397SJohannes Berg 	 * frames.
63160750397SJohannes Berg 	 */
63260750397SJohannes Berg 	sta_info_recalc_tim(sta);
63360750397SJohannes Berg 
63460750397SJohannes Berg 	/*
63560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
63660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
63760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
63860750397SJohannes Berg 	 */
639948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
640948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
641948d887dSJohannes Berg }
642948d887dSJohannes Berg 
643948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
644948d887dSJohannes Berg 					     struct sta_info *sta)
645948d887dSJohannes Berg {
646948d887dSJohannes Berg 	bool have_buffered = false;
647948d887dSJohannes Berg 	int ac;
648948d887dSJohannes Berg 
649948d887dSJohannes Berg 	/* This is only necessary for stations on BSS interfaces */
650948d887dSJohannes Berg 	if (!sta->sdata->bss)
651948d887dSJohannes Berg 		return false;
652948d887dSJohannes Berg 
653948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
654948d887dSJohannes Berg 		have_buffered |=
655948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
656948d887dSJohannes Berg 
657948d887dSJohannes Berg 	return have_buffered;
658f0706e82SJiri Benc }
659f0706e82SJiri Benc 
66083d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
66134e89507SJohannes Berg {
66234e89507SJohannes Berg 	struct ieee80211_local *local;
66334e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
664948d887dSJohannes Berg 	int ret, i, ac;
66542624d49SYogesh Ashok Powar 	struct tid_ampdu_tx *tid_tx;
66634e89507SJohannes Berg 
66734e89507SJohannes Berg 	might_sleep();
66834e89507SJohannes Berg 
66934e89507SJohannes Berg 	if (!sta)
67034e89507SJohannes Berg 		return -ENOENT;
67134e89507SJohannes Berg 
67234e89507SJohannes Berg 	local = sta->local;
67334e89507SJohannes Berg 	sdata = sta->sdata;
67434e89507SJohannes Berg 
67583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
67683d5cc01SJohannes Berg 
677098a6070SJohannes Berg 	/*
678098a6070SJohannes Berg 	 * Before removing the station from the driver and
679098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
680098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
681098a6070SJohannes Berg 	 * will be sufficient.
682098a6070SJohannes Berg 	 */
683c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
68453f73c09SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, true);
685098a6070SJohannes Berg 
68634e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
68734e89507SJohannes Berg 	if (ret)
68834e89507SJohannes Berg 		return ret;
68934e89507SJohannes Berg 
6904d33960bSJohannes Berg 	list_del(&sta->list);
6914d33960bSJohannes Berg 
6928cb23153SJohannes Berg 	mutex_lock(&local->key_mtx);
693e31b8213SJohannes Berg 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
69440b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
695e31b8213SJohannes Berg 	if (sta->ptk)
69640b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
6978cb23153SJohannes Berg 	mutex_unlock(&local->key_mtx);
69834e89507SJohannes Berg 
69934e89507SJohannes Berg 	sta->dead = true;
70034e89507SJohannes Berg 
70134e89507SJohannes Berg 	local->num_sta--;
70234e89507SJohannes Berg 	local->sta_generation++;
70334e89507SJohannes Berg 
70434e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
705a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
70634e89507SJohannes Berg 
70783d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
708f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
709f09603a2SJohannes Berg 		if (ret) {
71083d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
71183d5cc01SJohannes Berg 			break;
71283d5cc01SJohannes Berg 		}
71383d5cc01SJohannes Berg 	}
714d9a7ddb0SJohannes Berg 
715f09603a2SJohannes Berg 	if (sta->uploaded) {
716f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
717f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
718f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
719f09603a2SJohannes Berg 	}
72034e89507SJohannes Berg 
721e64b3795SJohannes Berg 	/*
722e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
723e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
724e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
725e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
726e64b3795SJohannes Berg 	 */
727e64b3795SJohannes Berg 	synchronize_rcu();
728e64b3795SJohannes Berg 
7294f3eb0baSHelmut Schaa 	if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
7304f3eb0baSHelmut Schaa 		BUG_ON(!sdata->bss);
7314f3eb0baSHelmut Schaa 
7324f3eb0baSHelmut Schaa 		clear_sta_flag(sta, WLAN_STA_PS_STA);
7334f3eb0baSHelmut Schaa 
7344f3eb0baSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
7354f3eb0baSHelmut Schaa 		sta_info_recalc_tim(sta);
7364f3eb0baSHelmut Schaa 	}
7374f3eb0baSHelmut Schaa 
738948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
739948d887dSJohannes Berg 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
740948d887dSJohannes Berg 		__skb_queue_purge(&sta->ps_tx_buf[ac]);
741948d887dSJohannes Berg 		__skb_queue_purge(&sta->tx_filtered[ac]);
742948d887dSJohannes Berg 	}
743c868cb35SJohannes Berg 
74434e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
745e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
74634e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
74734e89507SJohannes Berg #endif
74834e89507SJohannes Berg 
74934e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
7500fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
75134e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
75234e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
75334e89507SJohannes Berg 
754ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
755ec15e68bSJouni Malinen 
75634e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
75734e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
75834e89507SJohannes Berg 
75934e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
76034e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
76134e89507SJohannes Berg 		mesh_plink_deactivate(sta);
76234e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
76334e89507SJohannes Berg 	}
76434e89507SJohannes Berg #endif
76534e89507SJohannes Berg 
766151a02f6SJohannes Berg 	/*
767151a02f6SJohannes Berg 	 * Destroy aggregation state here. It would be nice to wait for the
768151a02f6SJohannes Berg 	 * driver to finish aggregation stop and then clean up, but for now
769151a02f6SJohannes Berg 	 * drivers have to handle aggregation stop being requested, followed
770151a02f6SJohannes Berg 	 * directly by station destruction.
77142624d49SYogesh Ashok Powar 	 */
77242624d49SYogesh Ashok Powar 	for (i = 0; i < STA_TID_NUM; i++) {
773151a02f6SJohannes Berg 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
77427bf8882SYogesh Ashok Powar 		if (!tid_tx)
77542624d49SYogesh Ashok Powar 			continue;
77642624d49SYogesh Ashok Powar 		__skb_queue_purge(&tid_tx->pending);
777151a02f6SJohannes Berg 		kfree(tid_tx);
77842624d49SYogesh Ashok Powar 	}
77927bf8882SYogesh Ashok Powar 
780d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
78134e89507SJohannes Berg 
78234e89507SJohannes Berg 	return 0;
78334e89507SJohannes Berg }
78434e89507SJohannes Berg 
78534e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
78634e89507SJohannes Berg {
78734e89507SJohannes Berg 	struct sta_info *sta;
78834e89507SJohannes Berg 	int ret;
78934e89507SJohannes Berg 
79034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
7917852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
79234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
79334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
79434e89507SJohannes Berg 
79534e89507SJohannes Berg 	return ret;
79634e89507SJohannes Berg }
79734e89507SJohannes Berg 
79834e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
79934e89507SJohannes Berg 			      const u8 *addr)
80034e89507SJohannes Berg {
80134e89507SJohannes Berg 	struct sta_info *sta;
80234e89507SJohannes Berg 	int ret;
80334e89507SJohannes Berg 
80434e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
8057852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
80634e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
80734e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
80834e89507SJohannes Berg 
80934e89507SJohannes Berg 	return ret;
81034e89507SJohannes Berg }
811f0706e82SJiri Benc 
812f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
813f0706e82SJiri Benc {
814f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
815f0706e82SJiri Benc 	struct sta_info *sta;
8163393a608SJuuso Oikarinen 	bool timer_needed = false;
817f0706e82SJiri Benc 
818d0709a65SJohannes Berg 	rcu_read_lock();
819d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
8203393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
8213393a608SJuuso Oikarinen 			timer_needed = true;
822d0709a65SJohannes Berg 	rcu_read_unlock();
823f0706e82SJiri Benc 
8245bb644a0SJohannes Berg 	if (local->quiescing)
8255bb644a0SJohannes Berg 		return;
8265bb644a0SJohannes Berg 
8273393a608SJuuso Oikarinen 	if (!timer_needed)
8283393a608SJuuso Oikarinen 		return;
8293393a608SJuuso Oikarinen 
83026d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
83126d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
832f0706e82SJiri Benc }
833f0706e82SJiri Benc 
834f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
835f0706e82SJiri Benc {
8364d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
83734e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
838f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
839f0706e82SJiri Benc 
840b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
841b24b8a24SPavel Emelyanov 		    (unsigned long)local);
842f0706e82SJiri Benc }
843f0706e82SJiri Benc 
844f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
845f0706e82SJiri Benc {
846f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
847be8755e1SMichael Wu 	sta_info_flush(local, NULL);
848f0706e82SJiri Benc }
849f0706e82SJiri Benc 
850f0706e82SJiri Benc /**
851f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
85244213b5eSJohannes Berg  *
85344213b5eSJohannes Berg  * Returns the number of removed STA entries.
85444213b5eSJohannes Berg  *
855f0706e82SJiri Benc  * @local: local interface data
856d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
857f0706e82SJiri Benc  */
85844213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
859d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
860f0706e82SJiri Benc {
861f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
86244213b5eSJohannes Berg 	int ret = 0;
863f0706e82SJiri Benc 
864d0709a65SJohannes Berg 	might_sleep();
865d0709a65SJohannes Berg 
86634e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
86734e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
86834316837SJohannes Berg 		if (!sdata || sdata == sta->sdata) {
86934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
87034316837SJohannes Berg 			ret++;
87134316837SJohannes Berg 		}
87234e89507SJohannes Berg 	}
87334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
87444213b5eSJohannes Berg 
87544213b5eSJohannes Berg 	return ret;
876f0706e82SJiri Benc }
877dc6676b7SJohannes Berg 
87824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
87924723d1bSJohannes Berg 			  unsigned long exp_time)
88024723d1bSJohannes Berg {
88124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
88224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
88324723d1bSJohannes Berg 
88434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
885e46a2cf9SMohammed Shafi Shajakhan 
886e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
887ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
888ec2b774eSMarek Lindner 			continue;
889ec2b774eSMarek Lindner 
89024723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
89124723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
8920c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
89347846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
89424723d1bSJohannes Berg #endif
89534e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
89624723d1bSJohannes Berg 		}
897e46a2cf9SMohammed Shafi Shajakhan 	}
898e46a2cf9SMohammed Shafi Shajakhan 
89934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
90024723d1bSJohannes Berg }
90117741cdcSJohannes Berg 
902686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
903686b9cb9SBen Greear 					       const u8 *addr,
904686b9cb9SBen Greear 					       const u8 *localaddr)
90517741cdcSJohannes Berg {
906abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
90717741cdcSJohannes Berg 
908686b9cb9SBen Greear 	/*
909686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
910686b9cb9SBen Greear 	 * ... first in list.
911686b9cb9SBen Greear 	 */
912f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
913686b9cb9SBen Greear 		if (localaddr &&
914686b9cb9SBen Greear 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
915686b9cb9SBen Greear 			continue;
916f7c65594SJohannes Berg 		if (!sta->uploaded)
917f7c65594SJohannes Berg 			return NULL;
91817741cdcSJohannes Berg 		return &sta->sta;
919f7c65594SJohannes Berg 	}
920f7c65594SJohannes Berg 
921abe60632SJohannes Berg 	return NULL;
92217741cdcSJohannes Berg }
923686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
9245ed176e1SJohannes Berg 
9255ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
9265ed176e1SJohannes Berg 					 const u8 *addr)
9275ed176e1SJohannes Berg {
928f7c65594SJohannes Berg 	struct sta_info *sta;
9295ed176e1SJohannes Berg 
9305ed176e1SJohannes Berg 	if (!vif)
9315ed176e1SJohannes Berg 		return NULL;
9325ed176e1SJohannes Berg 
933f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
934f7c65594SJohannes Berg 	if (!sta)
935f7c65594SJohannes Berg 		return NULL;
9365ed176e1SJohannes Berg 
937f7c65594SJohannes Berg 	if (!sta->uploaded)
938f7c65594SJohannes Berg 		return NULL;
939f7c65594SJohannes Berg 
940f7c65594SJohannes Berg 	return &sta->sta;
9415ed176e1SJohannes Berg }
94217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
943af818581SJohannes Berg 
94450a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
94550a9432dSJohannes Berg {
94650a9432dSJohannes Berg 	struct sta_info *sta = _sta;
947608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
94850a9432dSJohannes Berg 
949c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
950608383bfSHelmut Schaa 	if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
951608383bfSHelmut Schaa 		atomic_dec(&sdata->bss->num_sta_ps);
95250a9432dSJohannes Berg }
95350a9432dSJohannes Berg 
954af818581SJohannes Berg /* powersave support code */
955af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
956af818581SJohannes Berg {
957af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
958af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
959948d887dSJohannes Berg 	struct sk_buff_head pending;
960948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
961af818581SJohannes Berg 
962c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
96347086fc5SJohannes Berg 
964948d887dSJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
965948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
966948d887dSJohannes Berg 
967d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
96812375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
969af818581SJohannes Berg 
970948d887dSJohannes Berg 	skb_queue_head_init(&pending);
971af818581SJohannes Berg 
972af818581SJohannes Berg 	/* Send all buffered frames to the station */
973948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
974948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
975948d887dSJohannes Berg 
976948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
977948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
978948d887dSJohannes Berg 		filtered += tmp - count;
979948d887dSJohannes Berg 		count = tmp;
980948d887dSJohannes Berg 
981948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
982948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
983948d887dSJohannes Berg 		buffered += tmp - count;
984948d887dSJohannes Berg 	}
985948d887dSJohannes Berg 
986948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
987948d887dSJohannes Berg 
988af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
989af818581SJohannes Berg 
990c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
991c868cb35SJohannes Berg 
992af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
993af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
99447846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
995948d887dSJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
996af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
997af818581SJohannes Berg }
998af818581SJohannes Berg 
999ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1000ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
100140b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1002ce662b44SJohannes Berg {
1003ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1004ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1005ce662b44SJohannes Berg 	struct sk_buff *skb;
1006ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1007ce662b44SJohannes Berg 	__le16 fc;
1008c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1009ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
1010ce662b44SJohannes Berg 
1011ce662b44SJohannes Berg 	if (qos) {
1012ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1013ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1014ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1015ce662b44SJohannes Berg 	} else {
1016ce662b44SJohannes Berg 		size -= 2;
1017ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1018ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1019ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1020ce662b44SJohannes Berg 	}
1021ce662b44SJohannes Berg 
1022ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1023ce662b44SJohannes Berg 	if (!skb)
1024ce662b44SJohannes Berg 		return;
1025ce662b44SJohannes Berg 
1026ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1027ce662b44SJohannes Berg 
1028ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1029ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1030ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1031ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1032ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1033ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1034ce662b44SJohannes Berg 
1035ce662b44SJohannes Berg 	skb->priority = tid;
1036ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
103759b66255SJohannes Berg 	if (qos) {
1038ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1039ce662b44SJohannes Berg 
104040b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1041ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1042ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1043ce662b44SJohannes Berg 	}
1044ce662b44SJohannes Berg 
1045ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1046ce662b44SJohannes Berg 
1047ce662b44SJohannes Berg 	/*
1048ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1049ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1050deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1051deeaee19SJohannes Berg 	 * ends the poll/service period.
1052ce662b44SJohannes Berg 	 */
1053*02f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1054deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1055ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1056ce662b44SJohannes Berg 
105740b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
105840b96408SJohannes Berg 
1059ce662b44SJohannes Berg 	ieee80211_xmit(sdata, skb);
1060ce662b44SJohannes Berg }
1061ce662b44SJohannes Berg 
106247086fc5SJohannes Berg static void
106347086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
106447086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
106547086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1066af818581SJohannes Berg {
1067af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1068af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
10694049e09aSJohannes Berg 	bool found = false;
1070948d887dSJohannes Berg 	bool more_data = false;
1071948d887dSJohannes Berg 	int ac;
10724049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
107347086fc5SJohannes Berg 	struct sk_buff_head frames;
107447086fc5SJohannes Berg 
1075deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1076c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1077deeaee19SJohannes Berg 
107847086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1079af818581SJohannes Berg 
1080948d887dSJohannes Berg 	/*
108147086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1082948d887dSJohannes Berg 	 */
1083948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
10844049e09aSJohannes Berg 		unsigned long tids;
10854049e09aSJohannes Berg 
108647086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1087948d887dSJohannes Berg 			continue;
1088948d887dSJohannes Berg 
10894049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
10904049e09aSJohannes Berg 
10914049e09aSJohannes Berg 		if (!found) {
10924049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
10934049e09aSJohannes Berg 			if (driver_release_tids) {
10944049e09aSJohannes Berg 				found = true;
10954049e09aSJohannes Berg 			} else {
109647086fc5SJohannes Berg 				struct sk_buff *skb;
109747086fc5SJohannes Berg 
109847086fc5SJohannes Berg 				while (n_frames > 0) {
1099948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1100948d887dSJohannes Berg 					if (!skb) {
110147086fc5SJohannes Berg 						skb = skb_dequeue(
110247086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1103af818581SJohannes Berg 						if (skb)
1104af818581SJohannes Berg 							local->total_ps_buffered--;
1105af818581SJohannes Berg 					}
110647086fc5SJohannes Berg 					if (!skb)
110747086fc5SJohannes Berg 						break;
110847086fc5SJohannes Berg 					n_frames--;
11094049e09aSJohannes Berg 					found = true;
111047086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
111147086fc5SJohannes Berg 				}
1112948d887dSJohannes Berg 			}
1113948d887dSJohannes Berg 
11144049e09aSJohannes Berg 			/*
11154049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
11164049e09aSJohannes Berg 			 * certainly there's more data if we release just a
11174049e09aSJohannes Berg 			 * single frame now (from a single TID).
11184049e09aSJohannes Berg 			 */
111947086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
112047086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
11214049e09aSJohannes Berg 				more_data = true;
11224049e09aSJohannes Berg 				driver_release_tids =
11234049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
11244049e09aSJohannes Berg 				break;
11254049e09aSJohannes Berg 			}
11264049e09aSJohannes Berg 		}
1127948d887dSJohannes Berg 
1128948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1129948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1130948d887dSJohannes Berg 			more_data = true;
1131948d887dSJohannes Berg 			break;
1132948d887dSJohannes Berg 		}
1133948d887dSJohannes Berg 	}
1134af818581SJohannes Berg 
11354049e09aSJohannes Berg 	if (!found) {
1136ce662b44SJohannes Berg 		int tid;
11374049e09aSJohannes Berg 
1138ce662b44SJohannes Berg 		/*
1139ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1140ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1141ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1142ce662b44SJohannes Berg 		 *
1143ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1144ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1145ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1146ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1147ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1148ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1149ce662b44SJohannes Berg 		 *
1150ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1151ce662b44SJohannes Berg 		 */
1152ce662b44SJohannes Berg 
1153ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1154ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1155ce662b44SJohannes Berg 
115640b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
11574049e09aSJohannes Berg 		return;
11584049e09aSJohannes Berg 	}
11594049e09aSJohannes Berg 
116047086fc5SJohannes Berg 	if (!driver_release_tids) {
116147086fc5SJohannes Berg 		struct sk_buff_head pending;
116247086fc5SJohannes Berg 		struct sk_buff *skb;
116340b96408SJohannes Berg 		int num = 0;
116440b96408SJohannes Berg 		u16 tids = 0;
116547086fc5SJohannes Berg 
116647086fc5SJohannes Berg 		skb_queue_head_init(&pending);
116747086fc5SJohannes Berg 
116847086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1169af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
117047086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
117140b96408SJohannes Berg 			u8 *qoshdr = NULL;
117240b96408SJohannes Berg 
117340b96408SJohannes Berg 			num++;
1174af818581SJohannes Berg 
1175af818581SJohannes Berg 			/*
117647086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
117747086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
117847086fc5SJohannes Berg 			 * exchange.
1179af818581SJohannes Berg 			 */
1180*02f2f1a9SJohannes Berg 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1181af818581SJohannes Berg 
118247086fc5SJohannes Berg 			/*
118347086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
118447086fc5SJohannes Berg 			 * more buffered frames for this STA
118547086fc5SJohannes Berg 			 */
118624b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
118747086fc5SJohannes Berg 				hdr->frame_control |=
118847086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
118924b9c373SJanusz.Dziedzic@tieto.com 			else
119024b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
119124b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1192af818581SJohannes Berg 
119340b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
119440b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
119540b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
119640b96408SJohannes Berg 
119747086fc5SJohannes Berg 			/* set EOSP for the frame */
119840b96408SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
119940b96408SJohannes Berg 			    qoshdr && skb_queue_empty(&frames))
120040b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1201deeaee19SJohannes Berg 
120247086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_STATUS_EOSP |
120347086fc5SJohannes Berg 				       IEEE80211_TX_CTL_REQ_TX_STATUS;
120447086fc5SJohannes Berg 
120540b96408SJohannes Berg 			if (qoshdr)
120640b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
120740b96408SJohannes Berg 			else
120840b96408SJohannes Berg 				tids |= BIT(0);
120940b96408SJohannes Berg 
121047086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
121147086fc5SJohannes Berg 		}
121247086fc5SJohannes Berg 
121340b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
121440b96408SJohannes Berg 					  reason, more_data);
121540b96408SJohannes Berg 
121647086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1217af818581SJohannes Berg 
1218c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1219af818581SJohannes Berg 	} else {
1220af818581SJohannes Berg 		/*
12214049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
12224049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
12234049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
12244049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
12254049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
12264049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
12274049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
12284049e09aSJohannes Berg 		 * needs to be set anyway.
1229af818581SJohannes Berg 		 */
12304049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
123147086fc5SJohannes Berg 					    n_frames, reason, more_data);
12324049e09aSJohannes Berg 
12334049e09aSJohannes Berg 		/*
12344049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
12354049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
12364049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
12374049e09aSJohannes Berg 		 * release function.
12384049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
12394049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
12404049e09aSJohannes Berg 		 */
1241af818581SJohannes Berg 	}
1242af818581SJohannes Berg }
1243af818581SJohannes Berg 
1244af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1245af818581SJohannes Berg {
124647086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1247af818581SJohannes Berg 
1248af818581SJohannes Berg 	/*
124947086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
125047086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
125147086fc5SJohannes Berg 	 * only from the non-enabled ones.
1252af818581SJohannes Berg 	 */
125347086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
125447086fc5SJohannes Berg 		ignore_for_response = 0;
1255af818581SJohannes Berg 
125647086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
125747086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1258af818581SJohannes Berg }
125947086fc5SJohannes Berg 
126047086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
126147086fc5SJohannes Berg {
126247086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
126347086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
126447086fc5SJohannes Berg 
126547086fc5SJohannes Berg 	/*
126647086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
126747086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
126847086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
126947086fc5SJohannes Berg 	 * actually getting called.
127047086fc5SJohannes Berg 	 */
127147086fc5SJohannes Berg 	if (!delivery_enabled)
127247086fc5SJohannes Berg 		return;
127347086fc5SJohannes Berg 
127447086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
127547086fc5SJohannes Berg 	case 1:
127647086fc5SJohannes Berg 		n_frames = 2;
127747086fc5SJohannes Berg 		break;
127847086fc5SJohannes Berg 	case 2:
127947086fc5SJohannes Berg 		n_frames = 4;
128047086fc5SJohannes Berg 		break;
128147086fc5SJohannes Berg 	case 3:
128247086fc5SJohannes Berg 		n_frames = 6;
128347086fc5SJohannes Berg 		break;
128447086fc5SJohannes Berg 	case 0:
128547086fc5SJohannes Berg 		/* XXX: what is a good value? */
128647086fc5SJohannes Berg 		n_frames = 8;
128747086fc5SJohannes Berg 		break;
128847086fc5SJohannes Berg 	}
128947086fc5SJohannes Berg 
129047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
129147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1292af818581SJohannes Berg }
1293af818581SJohannes Berg 
1294af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1295af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1296af818581SJohannes Berg {
1297af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1298af818581SJohannes Berg 
1299b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1300b5878a2dSJohannes Berg 
1301af818581SJohannes Berg 	if (block)
1302c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1303c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1304af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1305af818581SJohannes Berg }
1306af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1307dcf55fb5SFelix Fietkau 
130837fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
130937fbd908SJohannes Berg {
131037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
131137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
131237fbd908SJohannes Berg 	struct sk_buff *skb;
131337fbd908SJohannes Berg 	struct skb_eosp_msg_data *data;
131437fbd908SJohannes Berg 
131537fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
131637fbd908SJohannes Berg 
131737fbd908SJohannes Berg 	skb = alloc_skb(0, GFP_ATOMIC);
131837fbd908SJohannes Berg 	if (!skb) {
131937fbd908SJohannes Berg 		/* too bad ... but race is better than loss */
132037fbd908SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_SP);
132137fbd908SJohannes Berg 		return;
132237fbd908SJohannes Berg 	}
132337fbd908SJohannes Berg 
132437fbd908SJohannes Berg 	data = (void *)skb->cb;
132537fbd908SJohannes Berg 	memcpy(data->sta, pubsta->addr, ETH_ALEN);
132637fbd908SJohannes Berg 	memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
132737fbd908SJohannes Berg 	skb->pkt_type = IEEE80211_EOSP_MSG;
132837fbd908SJohannes Berg 	skb_queue_tail(&local->skb_queue, skb);
132937fbd908SJohannes Berg 	tasklet_schedule(&local->tasklet);
133037fbd908SJohannes Berg }
133137fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
133237fbd908SJohannes Berg 
1333042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1334042ec453SJohannes Berg 				u8 tid, bool buffered)
1335dcf55fb5SFelix Fietkau {
1336dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1337dcf55fb5SFelix Fietkau 
1338948d887dSJohannes Berg 	if (WARN_ON(tid >= STA_TID_NUM))
1339042ec453SJohannes Berg 		return;
1340042ec453SJohannes Berg 
1341948d887dSJohannes Berg 	if (buffered)
1342948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1343948d887dSJohannes Berg 	else
1344948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1345948d887dSJohannes Berg 
1346c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1347dcf55fb5SFelix Fietkau }
1348042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1349d9a7ddb0SJohannes Berg 
135083d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1351d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1352d9a7ddb0SJohannes Berg {
13538bf11d8dSJohannes Berg 	might_sleep();
1354d9a7ddb0SJohannes Berg 
1355d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1356d9a7ddb0SJohannes Berg 		return 0;
1357d9a7ddb0SJohannes Berg 
1358f09603a2SJohannes Berg 	/* check allowed transitions first */
1359f09603a2SJohannes Berg 
1360d9a7ddb0SJohannes Berg 	switch (new_state) {
1361d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1362f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1363d9a7ddb0SJohannes Berg 			return -EINVAL;
1364d9a7ddb0SJohannes Berg 		break;
1365d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1366f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1367f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1368d9a7ddb0SJohannes Berg 			return -EINVAL;
1369d9a7ddb0SJohannes Berg 		break;
1370d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1371f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1372f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1373d9a7ddb0SJohannes Berg 			return -EINVAL;
1374d9a7ddb0SJohannes Berg 		break;
1375d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1376f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1377d9a7ddb0SJohannes Berg 			return -EINVAL;
1378d9a7ddb0SJohannes Berg 		break;
1379d9a7ddb0SJohannes Berg 	default:
1380d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1381d9a7ddb0SJohannes Berg 		return -EINVAL;
1382d9a7ddb0SJohannes Berg 	}
1383d9a7ddb0SJohannes Berg 
138479027596SFelix Fietkau #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1385d9a7ddb0SJohannes Berg 	printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
1386d9a7ddb0SJohannes Berg 		sta->sdata->name, sta->sta.addr, new_state);
138779027596SFelix Fietkau #endif
1388f09603a2SJohannes Berg 
1389f09603a2SJohannes Berg 	/*
1390f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1391f09603a2SJohannes Berg 	 * fail the transition
1392f09603a2SJohannes Berg 	 */
1393f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1394f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1395f09603a2SJohannes Berg 					sta->sta_state, new_state);
1396f09603a2SJohannes Berg 		if (err)
1397f09603a2SJohannes Berg 			return err;
1398f09603a2SJohannes Berg 	}
1399f09603a2SJohannes Berg 
1400f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1401f09603a2SJohannes Berg 
1402f09603a2SJohannes Berg 	switch (new_state) {
1403f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1404f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1405f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1406f09603a2SJohannes Berg 		break;
1407f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1408f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1409f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1410f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1411f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1412f09603a2SJohannes Berg 		break;
1413f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1414f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1415f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1416f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1417f09603a2SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1418f09603a2SJohannes Berg 				atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
1419f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1420f09603a2SJohannes Berg 		}
1421f09603a2SJohannes Berg 		break;
1422f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1423f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
1424f09603a2SJohannes Berg 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1425f09603a2SJohannes Berg 				atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
1426f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1427f09603a2SJohannes Berg 		}
1428f09603a2SJohannes Berg 		break;
1429f09603a2SJohannes Berg 	default:
1430f09603a2SJohannes Berg 		break;
1431f09603a2SJohannes Berg 	}
1432f09603a2SJohannes Berg 
1433d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1434d9a7ddb0SJohannes Berg 
1435d9a7ddb0SJohannes Berg 	return 0;
1436d9a7ddb0SJohannes Berg }
1437