Merge branch 'for-linus-4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[linux-2.6-block.git] / net / mac80211 / sta_info.c
CommitLineData
f0706e82
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
d98ad83e 4 * Copyright 2013-2014 Intel Mobile Communications GmbH
f0706e82
JB
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
888d04df 13#include <linux/etherdevice.h>
f0706e82
JB
14#include <linux/netdevice.h>
15#include <linux/types.h>
16#include <linux/slab.h>
17#include <linux/skbuff.h>
18#include <linux/if_arp.h>
0d174406 19#include <linux/timer.h>
d0709a65 20#include <linux/rtnetlink.h>
f0706e82
JB
21
22#include <net/mac80211.h>
23#include "ieee80211_i.h"
24487981 24#include "driver-ops.h"
2c8dccc7 25#include "rate.h"
f0706e82 26#include "sta_info.h"
e9f207f0 27#include "debugfs_sta.h"
ee385855 28#include "mesh.h"
ce662b44 29#include "wme.h"
f0706e82 30
d0709a65
JB
31/**
32 * DOC: STA information lifetime rules
33 *
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
37 *
34e89507
JB
38 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it, in
44 * particular, it may not start any mesh peer link management or add
45 * encryption keys.
93e5deb1
JB
46 *
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
d0709a65 49 *
34e89507 50 * Station entries are added by mac80211 when you establish a link with a
7e189a12
LR
51 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
25985edc 53 * receive an association response from the AP. For IBSS this occurs when
34e89507 54 * get to know about a peer on the same IBSS. For WDS we add the sta for
25985edc 55 * the peer immediately upon device open. When using AP mode we add stations
34e89507 56 * for each respective station upon request from userspace through nl80211.
7e189a12 57 *
34e89507
JB
58 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
d0709a65 60 *
34e89507
JB
61 * There is no concept of ownership on a STA entry, each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
d0709a65 65 */
f0706e82 66
7bedd0cf
JB
67static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
caf22d31 69 .automatic_shrinking = true,
7bedd0cf
JB
70 .head_offset = offsetof(struct sta_info, hash_node),
71 .key_offset = offsetof(struct sta_info, sta.addr),
72 .key_len = ETH_ALEN,
73 .hashfn = sta_addr_hash,
74};
75
4d33960b 76/* Caller must hold local->sta_mtx */
be8755e1
MW
77static int sta_info_hash_del(struct ieee80211_local *local,
78 struct sta_info *sta)
f0706e82 79{
7bedd0cf
JB
80 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
81 sta_rht_params);
f0706e82
JB
82}
83
5108ca82 84static void __cleanup_single_sta(struct sta_info *sta)
b22cfcfc 85{
b22cfcfc
EP
86 int ac, i;
87 struct tid_ampdu_tx *tid_tx;
88 struct ieee80211_sub_if_data *sdata = sta->sdata;
89 struct ieee80211_local *local = sdata->local;
d012a605 90 struct ps_data *ps;
b22cfcfc 91
e3685e03 92 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
5ac2e350
JB
93 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
94 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
d012a605
MP
95 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
97 ps = &sdata->bss->ps;
3f52b7e3
MP
98 else if (ieee80211_vif_is_mesh(&sdata->vif))
99 ps = &sdata->u.mesh.ps;
d012a605
MP
100 else
101 return;
b22cfcfc
EP
102
103 clear_sta_flag(sta, WLAN_STA_PS_STA);
e3685e03 104 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
5ac2e350 105 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
b22cfcfc 106
d012a605 107 atomic_dec(&ps->num_sta_ps);
b22cfcfc
EP
108 }
109
ba8c3d6f
FF
110 if (sta->sta.txq[0]) {
111 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
113 int n = skb_queue_len(&txqi->queue);
114
115 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
116 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
117 }
118 }
119
b22cfcfc
EP
120 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
121 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1f98ab7f
FF
122 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
123 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
b22cfcfc
EP
124 }
125
45b5028e
TP
126 if (ieee80211_vif_is_mesh(&sdata->vif))
127 mesh_sta_cleanup(sta);
b22cfcfc 128
5ac2e350 129 cancel_work_sync(&sta->drv_deliver_wk);
b22cfcfc
EP
130
131 /*
132 * Destroy aggregation state here. It would be nice to wait for the
133 * driver to finish aggregation stop and then clean up, but for now
134 * drivers have to handle aggregation stop being requested, followed
135 * directly by station destruction.
136 */
5a306f58 137 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
661eb381 138 kfree(sta->ampdu_mlme.tid_start_tx[i]);
b22cfcfc
EP
139 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
140 if (!tid_tx)
141 continue;
1f98ab7f 142 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
b22cfcfc
EP
143 kfree(tid_tx);
144 }
5108ca82 145}
b22cfcfc 146
5108ca82
JB
147static void cleanup_single_sta(struct sta_info *sta)
148{
149 struct ieee80211_sub_if_data *sdata = sta->sdata;
150 struct ieee80211_local *local = sdata->local;
151
152 __cleanup_single_sta(sta);
b22cfcfc
EP
153 sta_info_free(local, sta);
154}
155
d0709a65 156/* protected by RCU */
abe60632
JB
157struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
158 const u8 *addr)
f0706e82 159{
abe60632 160 struct ieee80211_local *local = sdata->local;
60f4b626
JB
161 struct sta_info *sta;
162 struct rhash_head *tmp;
163 const struct bucket_table *tbl;
164
165 rcu_read_lock();
166 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
f0706e82 167
60f4b626
JB
168 for_each_sta_info(local, tbl, addr, sta, tmp) {
169 if (sta->sdata == sdata) {
170 rcu_read_unlock();
171 /* this is safe as the caller must already hold
172 * another rcu read section or the mutex
173 */
174 return sta;
175 }
176 }
177 rcu_read_unlock();
178 return NULL;
43ba7e95
JB
179}
180
0e5ded5a
FF
181/*
182 * Get sta info either from the specified interface
183 * or from one of its vlans
184 */
185struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
186 const u8 *addr)
187{
188 struct ieee80211_local *local = sdata->local;
189 struct sta_info *sta;
7bedd0cf
JB
190 struct rhash_head *tmp;
191 const struct bucket_table *tbl;
0e5ded5a 192
7bedd0cf
JB
193 rcu_read_lock();
194 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
195
196 for_each_sta_info(local, tbl, addr, sta, tmp) {
197 if (sta->sdata == sdata ||
198 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
199 rcu_read_unlock();
200 /* this is safe as the caller must already hold
201 * another rcu read section or the mutex
202 */
203 return sta;
204 }
0e5ded5a 205 }
7bedd0cf
JB
206 rcu_read_unlock();
207 return NULL;
0e5ded5a
FF
208}
209
3b53fde8
JB
210struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
211 int idx)
ee385855 212{
3b53fde8 213 struct ieee80211_local *local = sdata->local;
ee385855
LCC
214 struct sta_info *sta;
215 int i = 0;
216
d0709a65 217 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3b53fde8 218 if (sdata != sta->sdata)
2a8ca29a 219 continue;
ee385855
LCC
220 if (i < idx) {
221 ++i;
222 continue;
ee385855 223 }
2a8ca29a 224 return sta;
ee385855 225 }
ee385855
LCC
226
227 return NULL;
228}
f0706e82 229
93e5deb1 230/**
d9a7ddb0 231 * sta_info_free - free STA
93e5deb1 232 *
6ef307bc 233 * @local: pointer to the global information
93e5deb1
JB
234 * @sta: STA info to free
235 *
236 * This function must undo everything done by sta_info_alloc()
d9a7ddb0
JB
237 * that may happen before sta_info_insert(). It may only be
238 * called when sta_info_insert() has not been attempted (and
239 * if that fails, the station is freed anyway.)
93e5deb1 240 */
d9a7ddb0 241void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
93e5deb1 242{
889cbb91 243 if (sta->rate_ctrl)
af65cd96 244 rate_control_free_sta(sta);
93e5deb1 245
bdcbd8e0 246 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
93e5deb1 247
ba8c3d6f
FF
248 if (sta->sta.txq[0])
249 kfree(to_txq_info(sta->sta.txq[0]));
53d04525 250 kfree(rcu_dereference_raw(sta->sta.rates));
93e5deb1
JB
251 kfree(sta);
252}
253
4d33960b 254/* Caller must hold local->sta_mtx */
d0709a65
JB
255static void sta_info_hash_add(struct ieee80211_local *local,
256 struct sta_info *sta)
f0706e82 257{
7bedd0cf
JB
258 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
259 sta_rht_params);
f0706e82 260}
f0706e82 261
5ac2e350 262static void sta_deliver_ps_frames(struct work_struct *wk)
af818581
JB
263{
264 struct sta_info *sta;
265
5ac2e350 266 sta = container_of(wk, struct sta_info, drv_deliver_wk);
af818581
JB
267
268 if (sta->dead)
269 return;
270
5ac2e350
JB
271 local_bh_disable();
272 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
af818581 273 ieee80211_sta_ps_deliver_wakeup(sta);
5ac2e350 274 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
af818581 275 ieee80211_sta_ps_deliver_poll_response(sta);
5ac2e350 276 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
47086fc5 277 ieee80211_sta_ps_deliver_uapsd(sta);
5ac2e350 278 local_bh_enable();
af818581
JB
279}
280
af65cd96
JB
281static int sta_prepare_rate_control(struct ieee80211_local *local,
282 struct sta_info *sta, gfp_t gfp)
283{
284 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
285 return 0;
286
889cbb91 287 sta->rate_ctrl = local->rate_ctrl;
af65cd96
JB
288 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
289 &sta->sta, gfp);
889cbb91 290 if (!sta->rate_ctrl_priv)
af65cd96 291 return -ENOMEM;
af65cd96
JB
292
293 return 0;
294}
295
73651ee6 296struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
56544160 297 const u8 *addr, gfp_t gfp)
f0706e82 298{
d0709a65 299 struct ieee80211_local *local = sdata->local;
ba8c3d6f 300 struct ieee80211_hw *hw = &local->hw;
f0706e82 301 struct sta_info *sta;
ebe27c91 302 struct timespec uptime;
16c5f15c 303 int i;
f0706e82 304
ba8c3d6f 305 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
f0706e82 306 if (!sta)
73651ee6 307 return NULL;
f0706e82 308
07346f81 309 spin_lock_init(&sta->lock);
1d147bfa 310 spin_lock_init(&sta->ps_lock);
5ac2e350 311 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
67c282c0 312 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
a93e3644 313 mutex_init(&sta->ampdu_mlme.mtx);
87f59c70
TP
314#ifdef CONFIG_MAC80211_MESH
315 if (ieee80211_vif_is_mesh(&sdata->vif) &&
316 !sdata->u.mesh.user_mpm)
317 init_timer(&sta->plink_timer);
6c7c4cbf 318 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
87f59c70 319#endif
07346f81 320
17741cdc 321 memcpy(sta->sta.addr, addr, ETH_ALEN);
d0709a65
JB
322 sta->local = local;
323 sta->sdata = sdata;
8bc8aecd 324 sta->last_rx = jiffies;
f0706e82 325
71ec375c
JB
326 sta->sta_state = IEEE80211_STA_NONE;
327
b6da911b
LK
328 /* Mark TID as unreserved */
329 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
330
18171520 331 ktime_get_ts(&uptime);
ebe27c91 332 sta->last_connected = uptime.tv_sec;
541a45a1 333 ewma_init(&sta->avg_signal, 1024, 8);
ef0621e8
FF
334 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
335 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
541a45a1 336
ba8c3d6f
FF
337 if (local->ops->wake_tx_queue) {
338 void *txq_data;
339 int size = sizeof(struct txq_info) +
340 ALIGN(hw->txq_data_size, sizeof(void *));
341
342 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
343 if (!txq_data)
344 goto free;
345
346 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
347 struct txq_info *txq = txq_data + i * size;
348
349 ieee80211_init_tx_queue(sdata, sta, txq, i);
350 }
abfbc3af 351 }
f0706e82 352
ba8c3d6f
FF
353 if (sta_prepare_rate_control(local, sta, gfp))
354 goto free_txq;
355
5a306f58 356 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
a622ab72
JB
357 /*
358 * timer_to_tid must be initialized with identity mapping
359 * to enable session_timer's data differentiation. See
360 * sta_rx_agg_session_timer_expired for usage.
361 */
16c5f15c 362 sta->timer_to_tid[i] = i;
16c5f15c 363 }
948d887d
JB
364 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
365 skb_queue_head_init(&sta->ps_tx_buf[i]);
366 skb_queue_head_init(&sta->tx_filtered[i]);
367 }
73651ee6 368
5a306f58 369 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4be929be 370 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
cccaec98 371
af0ed69b 372 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
687da132
EG
373 if (sdata->vif.type == NL80211_IFTYPE_AP ||
374 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
375 struct ieee80211_supported_band *sband =
ba8c3d6f 376 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
687da132
EG
377 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
378 IEEE80211_HT_CAP_SM_PS_SHIFT;
379 /*
380 * Assume that hostapd advertises our caps in the beacon and
381 * this is the known_smps_mode for a station that just assciated
382 */
383 switch (smps) {
384 case WLAN_HT_SMPS_CONTROL_DISABLED:
385 sta->known_smps_mode = IEEE80211_SMPS_OFF;
386 break;
387 case WLAN_HT_SMPS_CONTROL_STATIC:
388 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
389 break;
390 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
391 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
392 break;
393 default:
394 WARN_ON(1);
395 }
396 }
af0ed69b 397
bdcbd8e0 398 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
ef04a297 399
abfbc3af 400 return sta;
ba8c3d6f
FF
401
402free_txq:
403 if (sta->sta.txq[0])
404 kfree(to_txq_info(sta->sta.txq[0]));
405free:
406 kfree(sta);
407 return NULL;
73651ee6
JB
408}
409
8c71df7a 410static int sta_info_insert_check(struct sta_info *sta)
34e89507 411{
34e89507 412 struct ieee80211_sub_if_data *sdata = sta->sdata;
34e89507 413
03e4497e
JB
414 /*
415 * Can't be a WARN_ON because it can be triggered through a race:
416 * something inserts a STA (on one CPU) without holding the RTNL
417 * and another CPU turns off the net device.
418 */
8c71df7a
GE
419 if (unlikely(!ieee80211_sdata_running(sdata)))
420 return -ENETDOWN;
03e4497e 421
b203ca39 422 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
8c71df7a
GE
423 is_multicast_ether_addr(sta->sta.addr)))
424 return -EINVAL;
425
426 return 0;
427}
428
f09603a2
JB
429static int sta_info_insert_drv_state(struct ieee80211_local *local,
430 struct ieee80211_sub_if_data *sdata,
431 struct sta_info *sta)
432{
433 enum ieee80211_sta_state state;
434 int err = 0;
435
436 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
437 err = drv_sta_state(local, sdata, sta, state, state + 1);
438 if (err)
439 break;
440 }
441
442 if (!err) {
a4ec45a4
JB
443 /*
444 * Drivers using legacy sta_add/sta_remove callbacks only
445 * get uploaded set to true after sta_add is called.
446 */
447 if (!local->ops->sta_add)
448 sta->uploaded = true;
f09603a2
JB
449 return 0;
450 }
451
452 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
bdcbd8e0
JB
453 sdata_info(sdata,
454 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
455 sta->sta.addr, state + 1, err);
f09603a2
JB
456 err = 0;
457 }
458
459 /* unwind on error */
460 for (; state > IEEE80211_STA_NOTEXIST; state--)
461 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
462
463 return err;
464}
465
8c71df7a
GE
466/*
467 * should be called with sta_mtx locked
468 * this function replaces the mutex lock
469 * with a RCU lock
470 */
4d33960b 471static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8c71df7a
GE
472{
473 struct ieee80211_local *local = sta->local;
474 struct ieee80211_sub_if_data *sdata = sta->sdata;
7852e361 475 struct station_info sinfo;
8c71df7a
GE
476 int err = 0;
477
478 lockdep_assert_held(&local->sta_mtx);
34e89507 479
7852e361
JB
480 /* check if STA exists already */
481 if (sta_info_get_bss(sdata, sta->sta.addr)) {
482 err = -EEXIST;
483 goto out_err;
4d33960b 484 }
32bfd35d 485
7852e361
JB
486 local->num_sta++;
487 local->sta_generation++;
488 smp_mb();
4d33960b 489
5108ca82
JB
490 /* simplify things and don't accept BA sessions yet */
491 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
492
7852e361
JB
493 /* make the station visible */
494 sta_info_hash_add(local, sta);
83d5cc01 495
2bad7748 496 list_add_tail_rcu(&sta->list, &local->sta_list);
4d33960b 497
5108ca82
JB
498 /* notify driver */
499 err = sta_info_insert_drv_state(local, sdata, sta);
500 if (err)
501 goto out_remove;
502
7852e361 503 set_sta_flag(sta, WLAN_STA_INSERTED);
5108ca82
JB
504 /* accept BA sessions now */
505 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4d33960b 506
21f659bf 507 ieee80211_recalc_min_chandef(sdata);
7852e361
JB
508 ieee80211_sta_debugfs_add(sta);
509 rate_control_add_sta_debugfs(sta);
4d33960b 510
7852e361
JB
511 memset(&sinfo, 0, sizeof(sinfo));
512 sinfo.filled = 0;
513 sinfo.generation = local->sta_generation;
514 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
d0709a65 515
bdcbd8e0 516 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
f0706e82 517
34e89507
JB
518 /* move reference to rcu-protected */
519 rcu_read_lock();
520 mutex_unlock(&local->sta_mtx);
e9f207f0 521
73651ee6
JB
522 if (ieee80211_vif_is_mesh(&sdata->vif))
523 mesh_accept_plinks_update(sdata);
524
8c71df7a 525 return 0;
5108ca82
JB
526 out_remove:
527 sta_info_hash_del(local, sta);
528 list_del_rcu(&sta->list);
529 local->num_sta--;
530 synchronize_net();
531 __cleanup_single_sta(sta);
4d33960b
JB
532 out_err:
533 mutex_unlock(&local->sta_mtx);
534 rcu_read_lock();
535 return err;
8c71df7a
GE
536}
537
538int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
539{
540 struct ieee80211_local *local = sta->local;
308f7fcf 541 int err;
8c71df7a 542
4d33960b
JB
543 might_sleep();
544
8c71df7a
GE
545 err = sta_info_insert_check(sta);
546 if (err) {
547 rcu_read_lock();
548 goto out_free;
549 }
550
8c71df7a
GE
551 mutex_lock(&local->sta_mtx);
552
4d33960b 553 err = sta_info_insert_finish(sta);
8c71df7a
GE
554 if (err)
555 goto out_free;
556
73651ee6 557 return 0;
93e5deb1 558 out_free:
d9a7ddb0 559 sta_info_free(local, sta);
93e5deb1 560 return err;
f0706e82
JB
561}
562
34e89507
JB
563int sta_info_insert(struct sta_info *sta)
564{
565 int err = sta_info_insert_rcu(sta);
566
567 rcu_read_unlock();
568
569 return err;
570}
571
d012a605 572static inline void __bss_tim_set(u8 *tim, u16 id)
004c872e
JB
573{
574 /*
575 * This format has been mandated by the IEEE specifications,
576 * so this line may not be changed to use the __set_bit() format.
577 */
d012a605 578 tim[id / 8] |= (1 << (id % 8));
004c872e
JB
579}
580
d012a605 581static inline void __bss_tim_clear(u8 *tim, u16 id)
004c872e
JB
582{
583 /*
584 * This format has been mandated by the IEEE specifications,
585 * so this line may not be changed to use the __clear_bit() format.
586 */
d012a605 587 tim[id / 8] &= ~(1 << (id % 8));
004c872e
JB
588}
589
3d5839b6
IP
590static inline bool __bss_tim_get(u8 *tim, u16 id)
591{
592 /*
593 * This format has been mandated by the IEEE specifications,
594 * so this line may not be changed to use the test_bit() format.
595 */
596 return tim[id / 8] & (1 << (id % 8));
597}
598
948d887d 599static unsigned long ieee80211_tids_for_ac(int ac)
004c872e 600{
948d887d
JB
601 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
602 switch (ac) {
603 case IEEE80211_AC_VO:
604 return BIT(6) | BIT(7);
605 case IEEE80211_AC_VI:
606 return BIT(4) | BIT(5);
607 case IEEE80211_AC_BE:
608 return BIT(0) | BIT(3);
609 case IEEE80211_AC_BK:
610 return BIT(1) | BIT(2);
611 default:
612 WARN_ON(1);
613 return 0;
d0709a65 614 }
004c872e
JB
615}
616
9b7a86f3 617static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
004c872e 618{
c868cb35 619 struct ieee80211_local *local = sta->local;
d012a605 620 struct ps_data *ps;
948d887d
JB
621 bool indicate_tim = false;
622 u8 ignore_for_tim = sta->sta.uapsd_queues;
623 int ac;
d012a605
MP
624 u16 id;
625
626 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
627 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
628 if (WARN_ON_ONCE(!sta->sdata->bss))
629 return;
004c872e 630
d012a605
MP
631 ps = &sta->sdata->bss->ps;
632 id = sta->sta.aid;
3f52b7e3
MP
633#ifdef CONFIG_MAC80211_MESH
634 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
635 ps = &sta->sdata->u.mesh.ps;
204d1304 636 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6f101ef0 637 id = sta->plid % (IEEE80211_MAX_AID + 1);
3f52b7e3 638#endif
d012a605 639 } else {
c868cb35 640 return;
d012a605 641 }
3e122be0 642
c868cb35
JB
643 /* No need to do anything if the driver does all */
644 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
645 return;
004c872e 646
c868cb35
JB
647 if (sta->dead)
648 goto done;
3e122be0 649
948d887d
JB
650 /*
651 * If all ACs are delivery-enabled then we should build
652 * the TIM bit for all ACs anyway; if only some are then
653 * we ignore those and build the TIM bit using only the
654 * non-enabled ones.
655 */
656 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
657 ignore_for_tim = 0;
658
9b7a86f3
JB
659 if (ignore_pending)
660 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
661
948d887d
JB
662 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
663 unsigned long tids;
3e122be0 664
948d887d
JB
665 if (ignore_for_tim & BIT(ac))
666 continue;
667
668 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
669 !skb_queue_empty(&sta->ps_tx_buf[ac]);
670 if (indicate_tim)
671 break;
3e122be0 672
948d887d
JB
673 tids = ieee80211_tids_for_ac(ac);
674
675 indicate_tim |=
676 sta->driver_buffered_tids & tids;
ba8c3d6f
FF
677 indicate_tim |=
678 sta->txq_buffered_tids & tids;
d0709a65 679 }
004c872e 680
c868cb35 681 done:
65f704a5 682 spin_lock_bh(&local->tim_lock);
004c872e 683
3d5839b6
IP
684 if (indicate_tim == __bss_tim_get(ps->tim, id))
685 goto out_unlock;
686
948d887d 687 if (indicate_tim)
d012a605 688 __bss_tim_set(ps->tim, id);
c868cb35 689 else
d012a605 690 __bss_tim_clear(ps->tim, id);
004c872e 691
9b7a86f3 692 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
c868cb35 693 local->tim_in_locked_section = true;
948d887d 694 drv_set_tim(local, &sta->sta, indicate_tim);
c868cb35
JB
695 local->tim_in_locked_section = false;
696 }
3e122be0 697
3d5839b6 698out_unlock:
65f704a5 699 spin_unlock_bh(&local->tim_lock);
004c872e
JB
700}
701
9b7a86f3
JB
702void sta_info_recalc_tim(struct sta_info *sta)
703{
704 __sta_info_recalc_tim(sta, false);
705}
706
cd0b8d89 707static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
f0706e82 708{
e039fa4a 709 struct ieee80211_tx_info *info;
f0706e82
JB
710 int timeout;
711
712 if (!skb)
cd0b8d89 713 return false;
f0706e82 714
e039fa4a 715 info = IEEE80211_SKB_CB(skb);
f0706e82
JB
716
717 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
57c4d7b4
JB
718 timeout = (sta->listen_interval *
719 sta->sdata->vif.bss_conf.beacon_int *
720 32 / 15625) * HZ;
f0706e82
JB
721 if (timeout < STA_TX_BUFFER_EXPIRE)
722 timeout = STA_TX_BUFFER_EXPIRE;
e039fa4a 723 return time_after(jiffies, info->control.jiffies + timeout);
f0706e82
JB
724}
725
726
948d887d
JB
727static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
728 struct sta_info *sta, int ac)
f0706e82
JB
729{
730 unsigned long flags;
731 struct sk_buff *skb;
732
60750397
JB
733 /*
734 * First check for frames that should expire on the filtered
735 * queue. Frames here were rejected by the driver and are on
736 * a separate queue to avoid reordering with normal PS-buffered
737 * frames. They also aren't accounted for right now in the
738 * total_ps_buffered counter.
739 */
740 for (;;) {
948d887d
JB
741 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
742 skb = skb_peek(&sta->tx_filtered[ac]);
60750397 743 if (sta_info_buffer_expired(sta, skb))
948d887d 744 skb = __skb_dequeue(&sta->tx_filtered[ac]);
60750397
JB
745 else
746 skb = NULL;
948d887d 747 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
60750397
JB
748
749 /*
750 * Frames are queued in order, so if this one
751 * hasn't expired yet we can stop testing. If
752 * we actually reached the end of the queue we
753 * also need to stop, of course.
754 */
755 if (!skb)
756 break;
d4fa14cd 757 ieee80211_free_txskb(&local->hw, skb);
60750397
JB
758 }
759
760 /*
761 * Now also check the normal PS-buffered queue, this will
762 * only find something if the filtered queue was emptied
763 * since the filtered frames are all before the normal PS
764 * buffered frames.
765 */
f0706e82 766 for (;;) {
948d887d
JB
767 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
768 skb = skb_peek(&sta->ps_tx_buf[ac]);
57c4d7b4 769 if (sta_info_buffer_expired(sta, skb))
948d887d 770 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
836341a7 771 else
f0706e82 772 skb = NULL;
948d887d 773 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
f0706e82 774
60750397
JB
775 /*
776 * frames are queued in order, so if this one
777 * hasn't expired yet (or we reached the end of
778 * the queue) we can stop testing
779 */
836341a7 780 if (!skb)
f0706e82 781 break;
836341a7 782
836341a7 783 local->total_ps_buffered--;
bdcbd8e0
JB
784 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
785 sta->sta.addr);
d4fa14cd 786 ieee80211_free_txskb(&local->hw, skb);
f0706e82 787 }
3393a608 788
60750397
JB
789 /*
790 * Finally, recalculate the TIM bit for this station -- it might
791 * now be clear because the station was too slow to retrieve its
792 * frames.
793 */
794 sta_info_recalc_tim(sta);
795
796 /*
797 * Return whether there are any frames still buffered, this is
798 * used to check whether the cleanup timer still needs to run,
799 * if there are no frames we don't need to rearm the timer.
800 */
948d887d
JB
801 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
802 skb_queue_empty(&sta->tx_filtered[ac]));
803}
804
805static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
806 struct sta_info *sta)
807{
808 bool have_buffered = false;
809 int ac;
810
3f52b7e3
MP
811 /* This is only necessary for stations on BSS/MBSS interfaces */
812 if (!sta->sdata->bss &&
813 !ieee80211_vif_is_mesh(&sta->sdata->vif))
948d887d
JB
814 return false;
815
816 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
817 have_buffered |=
818 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
819
820 return have_buffered;
f0706e82
JB
821}
822
d778207b 823static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
f0706e82 824{
34e89507
JB
825 struct ieee80211_local *local;
826 struct ieee80211_sub_if_data *sdata;
6d10e46b 827 int ret;
f0706e82 828
34e89507 829 might_sleep();
f0706e82 830
34e89507
JB
831 if (!sta)
832 return -ENOENT;
5bb644a0 833
34e89507
JB
834 local = sta->local;
835 sdata = sta->sdata;
f0706e82 836
83d5cc01
JB
837 lockdep_assert_held(&local->sta_mtx);
838
098a6070
JB
839 /*
840 * Before removing the station from the driver and
841 * rate control, it might still start new aggregation
842 * sessions -- block that to make sure the tear-down
843 * will be sufficient.
844 */
c2c98fde 845 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
c82c4a80 846 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
098a6070 847
34e89507 848 ret = sta_info_hash_del(local, sta);
b01711be 849 if (WARN_ON(ret))
34e89507
JB
850 return ret;
851
a7a6bdd0
AN
852 /*
853 * for TDLS peers, make sure to return to the base channel before
854 * removal.
855 */
856 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
857 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
858 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
859 }
860
794454ce 861 list_del_rcu(&sta->list);
4d33960b 862
6a9d1b91
JB
863 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
864
a710c816
JB
865 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
866 rcu_access_pointer(sdata->u.vlan.sta) == sta)
867 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
868
d778207b
JB
869 return 0;
870}
871
872static void __sta_info_destroy_part2(struct sta_info *sta)
873{
874 struct ieee80211_local *local = sta->local;
875 struct ieee80211_sub_if_data *sdata = sta->sdata;
6f7a8d26 876 struct station_info sinfo = {};
d778207b
JB
877 int ret;
878
879 /*
880 * NOTE: This assumes at least synchronize_net() was done
881 * after _part1 and before _part2!
882 */
883
884 might_sleep();
885 lockdep_assert_held(&local->sta_mtx);
886
c8782078 887 /* now keys can no longer be reached */
6d10e46b 888 ieee80211_free_sta_keys(local, sta);
34e89507 889
9b7a86f3
JB
890 /* disable TIM bit - last chance to tell driver */
891 __sta_info_recalc_tim(sta, true);
892
34e89507
JB
893 sta->dead = true;
894
34e89507
JB
895 local->num_sta--;
896 local->sta_generation++;
897
83d5cc01 898 while (sta->sta_state > IEEE80211_STA_NONE) {
f09603a2
JB
899 ret = sta_info_move_state(sta, sta->sta_state - 1);
900 if (ret) {
83d5cc01
JB
901 WARN_ON_ONCE(1);
902 break;
903 }
904 }
d9a7ddb0 905
f09603a2 906 if (sta->uploaded) {
f09603a2
JB
907 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
908 IEEE80211_STA_NOTEXIST);
909 WARN_ON_ONCE(ret != 0);
910 }
34e89507 911
bdcbd8e0
JB
912 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
913
6f7a8d26
JB
914 sta_set_sinfo(sta, &sinfo);
915 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
ec15e68b 916
34e89507
JB
917 rate_control_remove_sta_debugfs(sta);
918 ieee80211_sta_debugfs_remove(sta);
21f659bf 919 ieee80211_recalc_min_chandef(sdata);
34e89507 920
d34ba216 921 cleanup_single_sta(sta);
d778207b
JB
922}
923
924int __must_check __sta_info_destroy(struct sta_info *sta)
925{
926 int err = __sta_info_destroy_part1(sta);
927
928 if (err)
929 return err;
930
931 synchronize_net();
932
933 __sta_info_destroy_part2(sta);
34e89507
JB
934
935 return 0;
4d6141c3
JS
936}
937
34e89507 938int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
4d6141c3 939{
34e89507
JB
940 struct sta_info *sta;
941 int ret;
4d6141c3 942
34e89507 943 mutex_lock(&sdata->local->sta_mtx);
7852e361 944 sta = sta_info_get(sdata, addr);
34e89507
JB
945 ret = __sta_info_destroy(sta);
946 mutex_unlock(&sdata->local->sta_mtx);
4d6141c3
JS
947
948 return ret;
949}
950
34e89507
JB
951int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
952 const u8 *addr)
e9f207f0 953{
34e89507
JB
954 struct sta_info *sta;
955 int ret;
e9f207f0 956
34e89507 957 mutex_lock(&sdata->local->sta_mtx);
7852e361 958 sta = sta_info_get_bss(sdata, addr);
34e89507
JB
959 ret = __sta_info_destroy(sta);
960 mutex_unlock(&sdata->local->sta_mtx);
d0709a65 961
34e89507
JB
962 return ret;
963}
e9f207f0 964
34e89507
JB
965static void sta_info_cleanup(unsigned long data)
966{
967 struct ieee80211_local *local = (struct ieee80211_local *) data;
968 struct sta_info *sta;
3393a608 969 bool timer_needed = false;
34e89507
JB
970
971 rcu_read_lock();
972 list_for_each_entry_rcu(sta, &local->sta_list, list)
3393a608
JO
973 if (sta_info_cleanup_expire_buffered(local, sta))
974 timer_needed = true;
34e89507 975 rcu_read_unlock();
e9f207f0 976
34e89507
JB
977 if (local->quiescing)
978 return;
d0709a65 979
3393a608
JO
980 if (!timer_needed)
981 return;
982
26d59535
JB
983 mod_timer(&local->sta_cleanup,
984 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
e9f207f0 985}
e9f207f0 986
7bedd0cf 987u32 sta_addr_hash(const void *key, u32 length, u32 seed)
f0706e82 988{
7bedd0cf
JB
989 return jhash(key, ETH_ALEN, seed);
990}
991
992int sta_info_init(struct ieee80211_local *local)
993{
994 int err;
995
996 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
997 if (err)
998 return err;
999
4d33960b 1000 spin_lock_init(&local->tim_lock);
34e89507 1001 mutex_init(&local->sta_mtx);
f0706e82 1002 INIT_LIST_HEAD(&local->sta_list);
f0706e82 1003
b24b8a24
PE
1004 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1005 (unsigned long)local);
7bedd0cf 1006 return 0;
f0706e82
JB
1007}
1008
1009void sta_info_stop(struct ieee80211_local *local)
1010{
a56f992c 1011 del_timer_sync(&local->sta_cleanup);
7bedd0cf 1012 rhashtable_destroy(&local->sta_hash);
f0706e82
JB
1013}
1014
051007d9 1015
e716251d 1016int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
f0706e82 1017{
b998e8bb 1018 struct ieee80211_local *local = sdata->local;
f0706e82 1019 struct sta_info *sta, *tmp;
d778207b 1020 LIST_HEAD(free_list);
44213b5e 1021 int ret = 0;
f0706e82 1022
d0709a65 1023 might_sleep();
be8755e1 1024
e716251d
JB
1025 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1026 WARN_ON(vlans && !sdata->bss);
1027
34e89507 1028 mutex_lock(&local->sta_mtx);
d0709a65 1029 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
e716251d
JB
1030 if (sdata == sta->sdata ||
1031 (vlans && sdata->bss == sta->sdata->bss)) {
d778207b
JB
1032 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1033 list_add(&sta->free_list, &free_list);
34316837
JB
1034 ret++;
1035 }
be8755e1 1036 }
d778207b
JB
1037
1038 if (!list_empty(&free_list)) {
1039 synchronize_net();
1040 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1041 __sta_info_destroy_part2(sta);
1042 }
34e89507 1043 mutex_unlock(&local->sta_mtx);
44213b5e 1044
051007d9
JB
1045 return ret;
1046}
1047
24723d1b
JB
1048void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1049 unsigned long exp_time)
1050{
1051 struct ieee80211_local *local = sdata->local;
1052 struct sta_info *sta, *tmp;
24723d1b 1053
34e89507 1054 mutex_lock(&local->sta_mtx);
e46a2cf9
MSS
1055
1056 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
ec2b774e
ML
1057 if (sdata != sta->sdata)
1058 continue;
1059
24723d1b 1060 if (time_after(jiffies, sta->last_rx + exp_time)) {
eea57d42
MSS
1061 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1062 sta->sta.addr);
3f52b7e3
MP
1063
1064 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1065 test_sta_flag(sta, WLAN_STA_PS_STA))
1066 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1067
34e89507 1068 WARN_ON(__sta_info_destroy(sta));
24723d1b 1069 }
e46a2cf9
MSS
1070 }
1071
34e89507 1072 mutex_unlock(&local->sta_mtx);
24723d1b 1073}
17741cdc 1074
686b9cb9 1075struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
7bedd0cf
JB
1076 const u8 *addr,
1077 const u8 *localaddr)
17741cdc 1078{
7bedd0cf
JB
1079 struct ieee80211_local *local = hw_to_local(hw);
1080 struct sta_info *sta;
1081 struct rhash_head *tmp;
1082 const struct bucket_table *tbl;
1083
1084 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
17741cdc 1085
686b9cb9
BG
1086 /*
1087 * Just return a random station if localaddr is NULL
1088 * ... first in list.
1089 */
7bedd0cf 1090 for_each_sta_info(local, tbl, addr, sta, tmp) {
686b9cb9 1091 if (localaddr &&
b203ca39 1092 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
686b9cb9 1093 continue;
f7c65594
JB
1094 if (!sta->uploaded)
1095 return NULL;
abe60632 1096 return &sta->sta;
f7c65594
JB
1097 }
1098
abe60632 1099 return NULL;
17741cdc 1100}
686b9cb9 1101EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
5ed176e1
JB
1102
1103struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1104 const u8 *addr)
1105{
f7c65594 1106 struct sta_info *sta;
5ed176e1
JB
1107
1108 if (!vif)
1109 return NULL;
1110
f7c65594
JB
1111 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1112 if (!sta)
1113 return NULL;
1114
1115 if (!sta->uploaded)
1116 return NULL;
5ed176e1 1117
f7c65594 1118 return &sta->sta;
5ed176e1 1119}
17741cdc 1120EXPORT_SYMBOL(ieee80211_find_sta);
af818581 1121
e3685e03
JB
1122/* powersave support code */
1123void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
50a9432d 1124{
608383bf 1125 struct ieee80211_sub_if_data *sdata = sta->sdata;
e3685e03
JB
1126 struct ieee80211_local *local = sdata->local;
1127 struct sk_buff_head pending;
ba8c3d6f 1128 int filtered = 0, buffered = 0, ac, i;
e3685e03 1129 unsigned long flags;
d012a605
MP
1130 struct ps_data *ps;
1131
3918edb0
FF
1132 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1133 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1134 u.ap);
1135
1136 if (sdata->vif.type == NL80211_IFTYPE_AP)
d012a605 1137 ps = &sdata->bss->ps;
3f52b7e3
MP
1138 else if (ieee80211_vif_is_mesh(&sdata->vif))
1139 ps = &sdata->u.mesh.ps;
d012a605
MP
1140 else
1141 return;
50a9432d 1142
c2c98fde 1143 clear_sta_flag(sta, WLAN_STA_SP);
47086fc5 1144
5a306f58 1145 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
948d887d 1146 sta->driver_buffered_tids = 0;
ba8c3d6f 1147 sta->txq_buffered_tids = 0;
af818581 1148
d057e5a3
AN
1149 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1150 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
af818581 1151
ba8c3d6f
FF
1152 if (sta->sta.txq[0]) {
1153 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1154 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1155
1156 if (!skb_queue_len(&txqi->queue))
1157 continue;
1158
1159 drv_wake_tx_queue(local, txqi);
1160 }
1161 }
1162
948d887d 1163 skb_queue_head_init(&pending);
af818581 1164
1d147bfa
EG
1165 /* sync with ieee80211_tx_h_unicast_ps_buf */
1166 spin_lock(&sta->ps_lock);
af818581 1167 /* Send all buffered frames to the station */
948d887d
JB
1168 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1169 int count = skb_queue_len(&pending), tmp;
1170
987c285c 1171 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
948d887d 1172 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
987c285c 1173 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
948d887d
JB
1174 tmp = skb_queue_len(&pending);
1175 filtered += tmp - count;
1176 count = tmp;
1177
987c285c 1178 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
948d887d 1179 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
987c285c 1180 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
948d887d
JB
1181 tmp = skb_queue_len(&pending);
1182 buffered += tmp - count;
1183 }
1184
e3685e03 1185 ieee80211_add_pending_skbs(local, &pending);
5ac2e350
JB
1186
1187 /* now we're no longer in the deliver code */
1188 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1189
1190 /* The station might have polled and then woken up before we responded,
1191 * so clear these flags now to avoid them sticking around.
1192 */
1193 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1194 clear_sta_flag(sta, WLAN_STA_UAPSD);
1d147bfa 1195 spin_unlock(&sta->ps_lock);
948d887d 1196
e3685e03
JB
1197 atomic_dec(&ps->num_sta_ps);
1198
687da132 1199 /* This station just woke up and isn't aware of our SMPS state */
062f1d6d
CYY
1200 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1201 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
687da132
EG
1202 sdata->smps_mode) &&
1203 sta->known_smps_mode != sdata->bss->req_smps &&
1204 sta_info_tx_streams(sta) != 1) {
1205 ht_dbg(sdata,
1206 "%pM just woke up and MIMO capable - update SMPS\n",
1207 sta->sta.addr);
1208 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1209 sta->sta.addr,
1210 sdata->vif.bss_conf.bssid);
1211 }
1212
af818581
JB
1213 local->total_ps_buffered -= buffered;
1214
c868cb35
JB
1215 sta_info_recalc_tim(sta);
1216
bdcbd8e0
JB
1217 ps_dbg(sdata,
1218 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1219 sta->sta.addr, sta->sta.aid, filtered, buffered);
af818581
JB
1220}
1221
ce662b44
JB
1222static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1223 struct sta_info *sta, int tid,
b77cf4f8
JB
1224 enum ieee80211_frame_release_type reason,
1225 bool call_driver)
af818581 1226{
af818581 1227 struct ieee80211_local *local = sdata->local;
ce662b44 1228 struct ieee80211_qos_hdr *nullfunc;
af818581 1229 struct sk_buff *skb;
ce662b44
JB
1230 int size = sizeof(*nullfunc);
1231 __le16 fc;
a74a8c84 1232 bool qos = sta->sta.wme;
ce662b44 1233 struct ieee80211_tx_info *info;
55de908a 1234 struct ieee80211_chanctx_conf *chanctx_conf;
af818581 1235
ce662b44
JB
1236 if (qos) {
1237 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1238 IEEE80211_STYPE_QOS_NULLFUNC |
1239 IEEE80211_FCTL_FROMDS);
1240 } else {
1241 size -= 2;
1242 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1243 IEEE80211_STYPE_NULLFUNC |
1244 IEEE80211_FCTL_FROMDS);
af818581 1245 }
af818581 1246
ce662b44
JB
1247 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1248 if (!skb)
1249 return;
1250
1251 skb_reserve(skb, local->hw.extra_tx_headroom);
1252
1253 nullfunc = (void *) skb_put(skb, size);
1254 nullfunc->frame_control = fc;
1255 nullfunc->duration_id = 0;
1256 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1257 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1258 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
864a6040 1259 nullfunc->seq_ctrl = 0;
ce662b44 1260
59b66255
JB
1261 skb->priority = tid;
1262 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
ce662b44 1263 if (qos) {
ce662b44
JB
1264 nullfunc->qos_ctrl = cpu_to_le16(tid);
1265
40b96408 1266 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
ce662b44
JB
1267 nullfunc->qos_ctrl |=
1268 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1269 }
1270
1271 info = IEEE80211_SKB_CB(skb);
1272
1273 /*
1274 * Tell TX path to send this frame even though the
1275 * STA may still remain is PS mode after this frame
deeaee19
JB
1276 * exchange. Also set EOSP to indicate this packet
1277 * ends the poll/service period.
ce662b44 1278 */
02f2f1a9 1279 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
deeaee19
JB
1280 IEEE80211_TX_STATUS_EOSP |
1281 IEEE80211_TX_CTL_REQ_TX_STATUS;
ce662b44 1282
6b127c71
SM
1283 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1284
b77cf4f8
JB
1285 if (call_driver)
1286 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1287 reason, false);
40b96408 1288
89afe614
JB
1289 skb->dev = sdata->dev;
1290
55de908a
JB
1291 rcu_read_lock();
1292 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1293 if (WARN_ON(!chanctx_conf)) {
1294 rcu_read_unlock();
1295 kfree_skb(skb);
1296 return;
1297 }
1298
73c4e195 1299 info->band = chanctx_conf->def.chan->band;
7c10770f 1300 ieee80211_xmit(sdata, sta, skb);
55de908a 1301 rcu_read_unlock();
ce662b44
JB
1302}
1303
0a1cb809
JB
1304static int find_highest_prio_tid(unsigned long tids)
1305{
1306 /* lower 3 TIDs aren't ordered perfectly */
1307 if (tids & 0xF8)
1308 return fls(tids) - 1;
1309 /* TID 0 is BE just like TID 3 */
1310 if (tids & BIT(0))
1311 return 0;
1312 return fls(tids) - 1;
1313}
1314
47086fc5
JB
1315static void
1316ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1317 int n_frames, u8 ignored_acs,
1318 enum ieee80211_frame_release_type reason)
af818581
JB
1319{
1320 struct ieee80211_sub_if_data *sdata = sta->sdata;
1321 struct ieee80211_local *local = sdata->local;
948d887d
JB
1322 bool more_data = false;
1323 int ac;
4049e09a 1324 unsigned long driver_release_tids = 0;
47086fc5 1325 struct sk_buff_head frames;
af818581 1326
deeaee19 1327 /* Service or PS-Poll period starts */
c2c98fde 1328 set_sta_flag(sta, WLAN_STA_SP);
deeaee19 1329
47086fc5 1330 __skb_queue_head_init(&frames);
948d887d 1331
f9f760b4 1332 /* Get response frame(s) and more data bit for the last one. */
948d887d 1333 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4049e09a
JB
1334 unsigned long tids;
1335
47086fc5 1336 if (ignored_acs & BIT(ac))
948d887d
JB
1337 continue;
1338
4049e09a
JB
1339 tids = ieee80211_tids_for_ac(ac);
1340
f9f760b4
JB
1341 /* if we already have frames from software, then we can't also
1342 * release from hardware queues
1343 */
ba8c3d6f 1344 if (skb_queue_empty(&frames)) {
f9f760b4 1345 driver_release_tids |= sta->driver_buffered_tids & tids;
ba8c3d6f
FF
1346 driver_release_tids |= sta->txq_buffered_tids & tids;
1347 }
948d887d 1348
f9f760b4
JB
1349 if (driver_release_tids) {
1350 /* If the driver has data on more than one TID then
4049e09a 1351 * certainly there's more data if we release just a
f9f760b4
JB
1352 * single frame now (from a single TID). This will
1353 * only happen for PS-Poll.
4049e09a 1354 */
47086fc5
JB
1355 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1356 hweight16(driver_release_tids) > 1) {
4049e09a
JB
1357 more_data = true;
1358 driver_release_tids =
0a1cb809
JB
1359 BIT(find_highest_prio_tid(
1360 driver_release_tids));
4049e09a
JB
1361 break;
1362 }
f9f760b4
JB
1363 } else {
1364 struct sk_buff *skb;
1365
1366 while (n_frames > 0) {
1367 skb = skb_dequeue(&sta->tx_filtered[ac]);
1368 if (!skb) {
1369 skb = skb_dequeue(
1370 &sta->ps_tx_buf[ac]);
1371 if (skb)
1372 local->total_ps_buffered--;
1373 }
1374 if (!skb)
1375 break;
1376 n_frames--;
1377 __skb_queue_tail(&frames, skb);
1378 }
4049e09a 1379 }
948d887d 1380
f9f760b4
JB
1381 /* If we have more frames buffered on this AC, then set the
1382 * more-data bit and abort the loop since we can't send more
1383 * data from other ACs before the buffered frames from this.
1384 */
948d887d
JB
1385 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1386 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1387 more_data = true;
1388 break;
1389 }
af818581 1390 }
af818581 1391
f9f760b4 1392 if (skb_queue_empty(&frames) && !driver_release_tids) {
ce662b44 1393 int tid;
af818581
JB
1394
1395 /*
ce662b44
JB
1396 * For PS-Poll, this can only happen due to a race condition
1397 * when we set the TIM bit and the station notices it, but
1398 * before it can poll for the frame we expire it.
1399 *
1400 * For uAPSD, this is said in the standard (11.2.1.5 h):
1401 * At each unscheduled SP for a non-AP STA, the AP shall
1402 * attempt to transmit at least one MSDU or MMPDU, but no
1403 * more than the value specified in the Max SP Length field
1404 * in the QoS Capability element from delivery-enabled ACs,
1405 * that are destined for the non-AP STA.
1406 *
1407 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
af818581 1408 */
af818581 1409
ce662b44
JB
1410 /* This will evaluate to 1, 3, 5 or 7. */
1411 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
af818581 1412
b77cf4f8 1413 ieee80211_send_null_response(sdata, sta, tid, reason, true);
f9f760b4 1414 } else if (!driver_release_tids) {
47086fc5
JB
1415 struct sk_buff_head pending;
1416 struct sk_buff *skb;
40b96408
JB
1417 int num = 0;
1418 u16 tids = 0;
b77cf4f8 1419 bool need_null = false;
af818581 1420
47086fc5 1421 skb_queue_head_init(&pending);
af818581 1422
47086fc5
JB
1423 while ((skb = __skb_dequeue(&frames))) {
1424 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1425 struct ieee80211_hdr *hdr = (void *) skb->data;
40b96408
JB
1426 u8 *qoshdr = NULL;
1427
1428 num++;
af818581 1429
47086fc5
JB
1430 /*
1431 * Tell TX path to send this frame even though the
1432 * STA may still remain is PS mode after this frame
1433 * exchange.
1434 */
6b127c71
SM
1435 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1436 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
47086fc5
JB
1437
1438 /*
1439 * Use MoreData flag to indicate whether there are
1440 * more buffered frames for this STA
1441 */
24b9c373 1442 if (more_data || !skb_queue_empty(&frames))
47086fc5
JB
1443 hdr->frame_control |=
1444 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
24b9c373
JD
1445 else
1446 hdr->frame_control &=
1447 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
47086fc5 1448
40b96408
JB
1449 if (ieee80211_is_data_qos(hdr->frame_control) ||
1450 ieee80211_is_qos_nullfunc(hdr->frame_control))
1451 qoshdr = ieee80211_get_qos_ctl(hdr);
1452
b77cf4f8 1453 tids |= BIT(skb->priority);
52a3f20c 1454
b77cf4f8
JB
1455 __skb_queue_tail(&pending, skb);
1456
1457 /* end service period after last frame or add one */
1458 if (!skb_queue_empty(&frames))
1459 continue;
1460
1461 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1462 /* for PS-Poll, there's only one frame */
52a3f20c
MP
1463 info->flags |= IEEE80211_TX_STATUS_EOSP |
1464 IEEE80211_TX_CTL_REQ_TX_STATUS;
b77cf4f8 1465 break;
52a3f20c 1466 }
deeaee19 1467
b77cf4f8
JB
1468 /* For uAPSD, things are a bit more complicated. If the
1469 * last frame has a QoS header (i.e. is a QoS-data or
1470 * QoS-nulldata frame) then just set the EOSP bit there
1471 * and be done.
1472 * If the frame doesn't have a QoS header (which means
1473 * it should be a bufferable MMPDU) then we can't set
1474 * the EOSP bit in the QoS header; add a QoS-nulldata
1475 * frame to the list to send it after the MMPDU.
1476 *
1477 * Note that this code is only in the mac80211-release
1478 * code path, we assume that the driver will not buffer
1479 * anything but QoS-data frames, or if it does, will
1480 * create the QoS-nulldata frame by itself if needed.
1481 *
1482 * Cf. 802.11-2012 10.2.1.10 (c).
1483 */
1484 if (qoshdr) {
1485 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
40b96408 1486
b77cf4f8
JB
1487 info->flags |= IEEE80211_TX_STATUS_EOSP |
1488 IEEE80211_TX_CTL_REQ_TX_STATUS;
1489 } else {
1490 /* The standard isn't completely clear on this
1491 * as it says the more-data bit should be set
1492 * if there are more BUs. The QoS-Null frame
1493 * we're about to send isn't buffered yet, we
1494 * only create it below, but let's pretend it
1495 * was buffered just in case some clients only
1496 * expect more-data=0 when eosp=1.
1497 */
1498 hdr->frame_control |=
1499 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1500 need_null = true;
1501 num++;
1502 }
1503 break;
47086fc5 1504 }
af818581 1505
40b96408
JB
1506 drv_allow_buffered_frames(local, sta, tids, num,
1507 reason, more_data);
1508
47086fc5 1509 ieee80211_add_pending_skbs(local, &pending);
af818581 1510
b77cf4f8
JB
1511 if (need_null)
1512 ieee80211_send_null_response(
1513 sdata, sta, find_highest_prio_tid(tids),
1514 reason, false);
1515
c868cb35 1516 sta_info_recalc_tim(sta);
af818581 1517 } else {
ba8c3d6f
FF
1518 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1519 int tid;
1520
af818581 1521 /*
4049e09a
JB
1522 * We need to release a frame that is buffered somewhere in the
1523 * driver ... it'll have to handle that.
f9f760b4
JB
1524 * Note that the driver also has to check the number of frames
1525 * on the TIDs we're releasing from - if there are more than
1526 * n_frames it has to set the more-data bit (if we didn't ask
1527 * it to set it anyway due to other buffered frames); if there
1528 * are fewer than n_frames it has to make sure to adjust that
1529 * to allow the service period to end properly.
4049e09a
JB
1530 */
1531 drv_release_buffered_frames(local, sta, driver_release_tids,
47086fc5 1532 n_frames, reason, more_data);
4049e09a
JB
1533
1534 /*
1535 * Note that we don't recalculate the TIM bit here as it would
1536 * most likely have no effect at all unless the driver told us
f9f760b4 1537 * that the TID(s) became empty before returning here from the
4049e09a 1538 * release function.
f9f760b4 1539 * Either way, however, when the driver tells us that the TID(s)
ba8c3d6f
FF
1540 * became empty or we find that a txq became empty, we'll do the
1541 * TIM recalculation.
af818581 1542 */
ba8c3d6f
FF
1543
1544 if (!sta->sta.txq[0])
1545 return;
1546
1547 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1548 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1549
1550 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1551 continue;
1552
1553 sta_info_recalc_tim(sta);
1554 break;
1555 }
af818581
JB
1556 }
1557}
1558
47086fc5
JB
1559void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1560{
1561 u8 ignore_for_response = sta->sta.uapsd_queues;
1562
1563 /*
1564 * If all ACs are delivery-enabled then we should reply
1565 * from any of them, if only some are enabled we reply
1566 * only from the non-enabled ones.
1567 */
1568 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1569 ignore_for_response = 0;
1570
1571 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1572 IEEE80211_FRAME_RELEASE_PSPOLL);
1573}
1574
1575void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1576{
1577 int n_frames = sta->sta.max_sp;
1578 u8 delivery_enabled = sta->sta.uapsd_queues;
1579
1580 /*
1581 * If we ever grow support for TSPEC this might happen if
1582 * the TSPEC update from hostapd comes in between a trigger
1583 * frame setting WLAN_STA_UAPSD in the RX path and this
1584 * actually getting called.
1585 */
1586 if (!delivery_enabled)
1587 return;
1588
47086fc5
JB
1589 switch (sta->sta.max_sp) {
1590 case 1:
1591 n_frames = 2;
1592 break;
1593 case 2:
1594 n_frames = 4;
1595 break;
1596 case 3:
1597 n_frames = 6;
1598 break;
1599 case 0:
1600 /* XXX: what is a good value? */
13a8098a 1601 n_frames = 128;
47086fc5
JB
1602 break;
1603 }
1604
1605 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1606 IEEE80211_FRAME_RELEASE_UAPSD);
1607}
1608
af818581
JB
1609void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1610 struct ieee80211_sta *pubsta, bool block)
1611{
1612 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1613
b5878a2d
JB
1614 trace_api_sta_block_awake(sta->local, pubsta, block);
1615
5ac2e350 1616 if (block) {
c2c98fde 1617 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
5ac2e350
JB
1618 return;
1619 }
1620
1621 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1622 return;
1623
1624 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1625 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1626 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1627 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1628 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1629 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1630 /* must be asleep in this case */
1631 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1632 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1633 } else {
1634 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1635 }
af818581
JB
1636}
1637EXPORT_SYMBOL(ieee80211_sta_block_awake);
dcf55fb5 1638
e943789e 1639void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
37fbd908
JB
1640{
1641 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1642 struct ieee80211_local *local = sta->local;
37fbd908
JB
1643
1644 trace_api_eosp(local, pubsta);
1645
e943789e 1646 clear_sta_flag(sta, WLAN_STA_SP);
37fbd908 1647}
e943789e 1648EXPORT_SYMBOL(ieee80211_sta_eosp);
37fbd908 1649
042ec453
JB
1650void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1651 u8 tid, bool buffered)
dcf55fb5
FF
1652{
1653 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1654
5a306f58 1655 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
042ec453
JB
1656 return;
1657
1b000789
JB
1658 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1659
948d887d
JB
1660 if (buffered)
1661 set_bit(tid, &sta->driver_buffered_tids);
1662 else
1663 clear_bit(tid, &sta->driver_buffered_tids);
1664
c868cb35 1665 sta_info_recalc_tim(sta);
dcf55fb5 1666}
042ec453 1667EXPORT_SYMBOL(ieee80211_sta_set_buffered);
d9a7ddb0 1668
83d5cc01
JB
1669int sta_info_move_state(struct sta_info *sta,
1670 enum ieee80211_sta_state new_state)
d9a7ddb0 1671{
8bf11d8d 1672 might_sleep();
d9a7ddb0
JB
1673
1674 if (sta->sta_state == new_state)
1675 return 0;
1676
f09603a2
JB
1677 /* check allowed transitions first */
1678
1679 switch (new_state) {
1680 case IEEE80211_STA_NONE:
1681 if (sta->sta_state != IEEE80211_STA_AUTH)
1682 return -EINVAL;
1683 break;
1684 case IEEE80211_STA_AUTH:
1685 if (sta->sta_state != IEEE80211_STA_NONE &&
1686 sta->sta_state != IEEE80211_STA_ASSOC)
1687 return -EINVAL;
1688 break;
1689 case IEEE80211_STA_ASSOC:
1690 if (sta->sta_state != IEEE80211_STA_AUTH &&
1691 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1692 return -EINVAL;
1693 break;
1694 case IEEE80211_STA_AUTHORIZED:
1695 if (sta->sta_state != IEEE80211_STA_ASSOC)
1696 return -EINVAL;
1697 break;
1698 default:
1699 WARN(1, "invalid state %d", new_state);
1700 return -EINVAL;
1701 }
1702
bdcbd8e0
JB
1703 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1704 sta->sta.addr, new_state);
f09603a2
JB
1705
1706 /*
1707 * notify the driver before the actual changes so it can
1708 * fail the transition
1709 */
1710 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1711 int err = drv_sta_state(sta->local, sta->sdata, sta,
1712 sta->sta_state, new_state);
1713 if (err)
1714 return err;
1715 }
1716
1717 /* reflect the change in all state variables */
1718
d9a7ddb0
JB
1719 switch (new_state) {
1720 case IEEE80211_STA_NONE:
1721 if (sta->sta_state == IEEE80211_STA_AUTH)
1722 clear_bit(WLAN_STA_AUTH, &sta->_flags);
d9a7ddb0
JB
1723 break;
1724 case IEEE80211_STA_AUTH:
1725 if (sta->sta_state == IEEE80211_STA_NONE)
1726 set_bit(WLAN_STA_AUTH, &sta->_flags);
1727 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1728 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
d9a7ddb0
JB
1729 break;
1730 case IEEE80211_STA_ASSOC:
29623892 1731 if (sta->sta_state == IEEE80211_STA_AUTH) {
d9a7ddb0 1732 set_bit(WLAN_STA_ASSOC, &sta->_flags);
29623892 1733 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
7e3ed02c
FF
1734 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1735 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1736 !sta->sdata->u.vlan.sta))
1737 atomic_dec(&sta->sdata->bss->num_mcast_sta);
d9a7ddb0 1738 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
f09603a2 1739 }
d9a7ddb0
JB
1740 break;
1741 case IEEE80211_STA_AUTHORIZED:
29623892 1742 if (sta->sta_state == IEEE80211_STA_ASSOC) {
7e3ed02c
FF
1743 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1744 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1745 !sta->sdata->u.vlan.sta))
1746 atomic_inc(&sta->sdata->bss->num_mcast_sta);
d9a7ddb0 1747 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
f09603a2 1748 }
d9a7ddb0
JB
1749 break;
1750 default:
f09603a2 1751 break;
d9a7ddb0
JB
1752 }
1753
d9a7ddb0
JB
1754 sta->sta_state = new_state;
1755
1756 return 0;
1757}
687da132
EG
1758
1759u8 sta_info_tx_streams(struct sta_info *sta)
1760{
1761 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1762 u8 rx_streams;
1763
1764 if (!sta->sta.ht_cap.ht_supported)
1765 return 1;
1766
1767 if (sta->sta.vht_cap.vht_supported) {
1768 int i;
1769 u16 tx_mcs_map =
1770 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1771
1772 for (i = 7; i >= 0; i--)
1773 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1774 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1775 return i + 1;
1776 }
1777
1778 if (ht_cap->mcs.rx_mask[3])
1779 rx_streams = 4;
1780 else if (ht_cap->mcs.rx_mask[2])
1781 rx_streams = 3;
1782 else if (ht_cap->mcs.rx_mask[1])
1783 rx_streams = 2;
1784 else
1785 rx_streams = 1;
1786
1787 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1788 return rx_streams;
1789
1790 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1791 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1792}
b7ffbd7e
JB
1793
1794void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1795{
1796 struct ieee80211_sub_if_data *sdata = sta->sdata;
1797 struct ieee80211_local *local = sdata->local;
9a244409 1798 struct rate_control_ref *ref = NULL;
b7ffbd7e 1799 struct timespec uptime;
b7ffbd7e
JB
1800 u32 thr = 0;
1801 int i, ac;
1802
9a244409
JL
1803 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1804 ref = local->rate_ctrl;
1805
b7ffbd7e
JB
1806 sinfo->generation = sdata->local->sta_generation;
1807
225b8189
JB
1808 /* do before driver, so beacon filtering drivers have a
1809 * chance to e.g. just add the number of filtered beacons
1810 * (or just modify the value entirely, of course)
1811 */
1812 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1813 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1814
2b9a7e1b
JB
1815 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1816
319090bf
JB
1817 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1818 BIT(NL80211_STA_INFO_STA_FLAGS) |
1819 BIT(NL80211_STA_INFO_BSS_PARAM) |
1820 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1821 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1822 BIT(NL80211_STA_INFO_BEACON_LOSS);
b7ffbd7e 1823
18171520 1824 ktime_get_ts(&uptime);
b7ffbd7e 1825 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
b7ffbd7e 1826 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
2b9a7e1b 1827
319090bf
JB
1828 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1829 BIT(NL80211_STA_INFO_TX_BYTES)))) {
2b9a7e1b
JB
1830 sinfo->tx_bytes = 0;
1831 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1832 sinfo->tx_bytes += sta->tx_bytes[ac];
319090bf 1833 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2b9a7e1b
JB
1834 }
1835
319090bf 1836 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
2b9a7e1b
JB
1837 sinfo->tx_packets = 0;
1838 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1839 sinfo->tx_packets += sta->tx_packets[ac];
319090bf 1840 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
2b9a7e1b
JB
1841 }
1842
319090bf
JB
1843 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1844 BIT(NL80211_STA_INFO_RX_BYTES)))) {
2b9a7e1b 1845 sinfo->rx_bytes = sta->rx_bytes;
319090bf 1846 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
2b9a7e1b
JB
1847 }
1848
319090bf 1849 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2b9a7e1b 1850 sinfo->rx_packets = sta->rx_packets;
319090bf 1851 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
2b9a7e1b
JB
1852 }
1853
319090bf 1854 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2b9a7e1b 1855 sinfo->tx_retries = sta->tx_retry_count;
319090bf 1856 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
2b9a7e1b
JB
1857 }
1858
319090bf 1859 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2b9a7e1b 1860 sinfo->tx_failed = sta->tx_retry_failed;
319090bf 1861 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
b7ffbd7e 1862 }
2b9a7e1b 1863
b7ffbd7e
JB
1864 sinfo->rx_dropped_misc = sta->rx_dropped;
1865 sinfo->beacon_loss_count = sta->beacon_loss_count;
1866
225b8189
JB
1867 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1868 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1869 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1870 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1871 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1872 }
1873
b7ffbd7e
JB
1874 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1875 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
319090bf 1876 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
b7ffbd7e 1877 sinfo->signal = (s8)sta->last_signal;
319090bf 1878 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2b9a7e1b
JB
1879 }
1880
319090bf 1881 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
2b9a7e1b 1882 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
319090bf 1883 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2b9a7e1b 1884 }
b7ffbd7e 1885 }
2b9a7e1b
JB
1886
1887 if (sta->chains &&
319090bf
JB
1888 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1889 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1890 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1891 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
b7ffbd7e
JB
1892
1893 sinfo->chains = sta->chains;
1894 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1895 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1896 sinfo->chain_signal_avg[i] =
1897 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1898 }
1899 }
1900
319090bf 1901 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2b9a7e1b 1902 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
319090bf 1903 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2b9a7e1b
JB
1904 }
1905
319090bf 1906 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2b9a7e1b 1907 sta_set_rate_info_rx(sta, &sinfo->rxrate);
319090bf 1908 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2b9a7e1b 1909 }
b7ffbd7e 1910
79c892b8
JB
1911 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1912 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1913 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1914
1915 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1916 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1917 tidstats->rx_msdu = sta->rx_msdu[i];
1918 }
1919
1920 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1921 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1922 tidstats->tx_msdu = sta->tx_msdu[i];
1923 }
1924
1925 if (!(tidstats->filled &
1926 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
1927 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1928 tidstats->filled |=
1929 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1930 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1931 }
1932
1933 if (!(tidstats->filled &
1934 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
1935 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1936 tidstats->filled |=
1937 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1938 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1939 }
1940 }
1941
b7ffbd7e
JB
1942 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1943#ifdef CONFIG_MAC80211_MESH
319090bf
JB
1944 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1945 BIT(NL80211_STA_INFO_PLID) |
1946 BIT(NL80211_STA_INFO_PLINK_STATE) |
1947 BIT(NL80211_STA_INFO_LOCAL_PM) |
1948 BIT(NL80211_STA_INFO_PEER_PM) |
1949 BIT(NL80211_STA_INFO_NONPEER_PM);
b7ffbd7e
JB
1950
1951 sinfo->llid = sta->llid;
1952 sinfo->plid = sta->plid;
1953 sinfo->plink_state = sta->plink_state;
1954 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
319090bf 1955 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
b7ffbd7e
JB
1956 sinfo->t_offset = sta->t_offset;
1957 }
1958 sinfo->local_pm = sta->local_pm;
1959 sinfo->peer_pm = sta->peer_pm;
1960 sinfo->nonpeer_pm = sta->nonpeer_pm;
1961#endif
1962 }
1963
1964 sinfo->bss_param.flags = 0;
1965 if (sdata->vif.bss_conf.use_cts_prot)
1966 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1967 if (sdata->vif.bss_conf.use_short_preamble)
1968 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1969 if (sdata->vif.bss_conf.use_short_slot)
1970 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
785e21a8 1971 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
b7ffbd7e
JB
1972 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1973
1974 sinfo->sta_flags.set = 0;
1975 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1976 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1977 BIT(NL80211_STA_FLAG_WME) |
1978 BIT(NL80211_STA_FLAG_MFP) |
1979 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1980 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1981 BIT(NL80211_STA_FLAG_TDLS_PEER);
1982 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1983 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1984 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1985 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
a74a8c84 1986 if (sta->sta.wme)
b7ffbd7e
JB
1987 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1988 if (test_sta_flag(sta, WLAN_STA_MFP))
1989 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1990 if (test_sta_flag(sta, WLAN_STA_AUTH))
1991 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1992 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1993 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1994 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1995 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1996
1997 /* check if the driver has a SW RC implementation */
1998 if (ref && ref->ops->get_expected_throughput)
1999 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2000 else
2001 thr = drv_get_expected_throughput(local, &sta->sta);
2002
2003 if (thr != 0) {
319090bf 2004 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
b7ffbd7e
JB
2005 sinfo->expected_throughput = thr;
2006 }
2007}