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