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