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