mac80211_hwsim: use CLOCK_MONOTONIC_RAW
[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>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
888d04df 12#include <linux/etherdevice.h>
f0706e82
JB
13#include <linux/netdevice.h>
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <linux/skbuff.h>
17#include <linux/if_arp.h>
0d174406 18#include <linux/timer.h>
d0709a65 19#include <linux/rtnetlink.h>
f0706e82
JB
20
21#include <net/mac80211.h>
22#include "ieee80211_i.h"
24487981 23#include "driver-ops.h"
2c8dccc7 24#include "rate.h"
f0706e82 25#include "sta_info.h"
e9f207f0 26#include "debugfs_sta.h"
ee385855 27#include "mesh.h"
ce662b44 28#include "wme.h"
f0706e82 29
d0709a65
JB
30/**
31 * DOC: STA information lifetime rules
32 *
33 * STA info structures (&struct sta_info) are managed in a hash table
34 * for faster lookup and a list for iteration. They are managed using
35 * RCU, i.e. access to the list and hash table is protected by RCU.
36 *
34e89507
JB
37 * Upon allocating a STA info structure with sta_info_alloc(), the caller
38 * owns that structure. It must then insert it into the hash table using
39 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
40 * case (which acquires an rcu read section but must not be called from
41 * within one) will the pointer still be valid after the call. Note that
42 * the caller may not do much with the STA info before inserting it, in
43 * particular, it may not start any mesh peer link management or add
44 * encryption keys.
93e5deb1
JB
45 *
46 * When the insertion fails (sta_info_insert()) returns non-zero), the
47 * structure will have been freed by sta_info_insert()!
d0709a65 48 *
34e89507 49 * Station entries are added by mac80211 when you establish a link with a
7e189a12
LR
50 * peer. This means different things for the different type of interfaces
51 * we support. For a regular station this mean we add the AP sta when we
25985edc 52 * receive an association response from the AP. For IBSS this occurs when
34e89507 53 * get to know about a peer on the same IBSS. For WDS we add the sta for
25985edc 54 * the peer immediately upon device open. When using AP mode we add stations
34e89507 55 * for each respective station upon request from userspace through nl80211.
7e189a12 56 *
34e89507
JB
57 * In order to remove a STA info structure, various sta_info_destroy_*()
58 * calls are available.
d0709a65 59 *
34e89507
JB
60 * There is no concept of ownership on a STA entry, each structure is
61 * owned by the global hash table/list until it is removed. All users of
62 * the structure need to be RCU protected so that the structure won't be
63 * freed before they are done using it.
d0709a65 64 */
f0706e82 65
4d33960b 66/* Caller must hold local->sta_mtx */
be8755e1
MW
67static int sta_info_hash_del(struct ieee80211_local *local,
68 struct sta_info *sta)
f0706e82
JB
69{
70 struct sta_info *s;
71
40b275b6 72 s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
4d33960b 73 lockdep_is_held(&local->sta_mtx));
f0706e82 74 if (!s)
be8755e1
MW
75 return -ENOENT;
76 if (s == sta) {
cf778b00 77 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
d0709a65 78 s->hnext);
be8755e1 79 return 0;
f0706e82
JB
80 }
81
40b275b6
JB
82 while (rcu_access_pointer(s->hnext) &&
83 rcu_access_pointer(s->hnext) != sta)
84 s = rcu_dereference_protected(s->hnext,
4d33960b 85 lockdep_is_held(&local->sta_mtx));
40b275b6 86 if (rcu_access_pointer(s->hnext)) {
cf778b00 87 rcu_assign_pointer(s->hnext, sta->hnext);
be8755e1
MW
88 return 0;
89 }
f0706e82 90
be8755e1 91 return -ENOENT;
f0706e82
JB
92}
93
97f97b1f 94static void cleanup_single_sta(struct sta_info *sta)
b22cfcfc 95{
b22cfcfc
EP
96 int ac, i;
97 struct tid_ampdu_tx *tid_tx;
98 struct ieee80211_sub_if_data *sdata = sta->sdata;
99 struct ieee80211_local *local = sdata->local;
d012a605 100 struct ps_data *ps;
b22cfcfc
EP
101
102 /*
103 * At this point, when being called as call_rcu callback,
104 * neither mac80211 nor the driver can reference this
105 * sta struct any more except by still existing timers
106 * associated with this station that we clean up below.
051007d9
JB
107 *
108 * Note though that this still uses the sdata and even
109 * calls the driver in AP and mesh mode, so interfaces
110 * of those types mush use call sta_info_flush_cleanup()
111 * (typically via sta_info_flush()) before deconfiguring
112 * the driver.
113 *
114 * In station mode, nothing happens here so it doesn't
115 * have to (and doesn't) do that, this is intentional to
116 * speed up roaming.
b22cfcfc
EP
117 */
118
119 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
d012a605
MP
120 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
121 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
122 ps = &sdata->bss->ps;
3f52b7e3
MP
123 else if (ieee80211_vif_is_mesh(&sdata->vif))
124 ps = &sdata->u.mesh.ps;
d012a605
MP
125 else
126 return;
b22cfcfc
EP
127
128 clear_sta_flag(sta, WLAN_STA_PS_STA);
129
d012a605 130 atomic_dec(&ps->num_sta_ps);
b22cfcfc
EP
131 sta_info_recalc_tim(sta);
132 }
133
134 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
135 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1f98ab7f
FF
136 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
137 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
b22cfcfc
EP
138 }
139
45b5028e
TP
140 if (ieee80211_vif_is_mesh(&sdata->vif))
141 mesh_sta_cleanup(sta);
b22cfcfc
EP
142
143 cancel_work_sync(&sta->drv_unblock_wk);
144
145 /*
146 * Destroy aggregation state here. It would be nice to wait for the
147 * driver to finish aggregation stop and then clean up, but for now
148 * drivers have to handle aggregation stop being requested, followed
149 * directly by station destruction.
150 */
5a306f58 151 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
661eb381 152 kfree(sta->ampdu_mlme.tid_start_tx[i]);
b22cfcfc
EP
153 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
154 if (!tid_tx)
155 continue;
1f98ab7f 156 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
b22cfcfc
EP
157 kfree(tid_tx);
158 }
159
160 sta_info_free(local, sta);
161}
162
97f97b1f
JB
163void ieee80211_cleanup_sdata_stas(struct ieee80211_sub_if_data *sdata)
164{
165 struct sta_info *sta;
166
167 spin_lock_bh(&sdata->cleanup_stations_lock);
168 while (!list_empty(&sdata->cleanup_stations)) {
169 sta = list_first_entry(&sdata->cleanup_stations,
170 struct sta_info, list);
171 list_del(&sta->list);
172 spin_unlock_bh(&sdata->cleanup_stations_lock);
173
174 cleanup_single_sta(sta);
175
176 spin_lock_bh(&sdata->cleanup_stations_lock);
177 }
178
179 spin_unlock_bh(&sdata->cleanup_stations_lock);
180}
181
b22cfcfc
EP
182static void free_sta_rcu(struct rcu_head *h)
183{
184 struct sta_info *sta = container_of(h, struct sta_info, rcu_head);
97f97b1f 185 struct ieee80211_sub_if_data *sdata = sta->sdata;
b22cfcfc 186
97f97b1f
JB
187 spin_lock(&sdata->cleanup_stations_lock);
188 list_add_tail(&sta->list, &sdata->cleanup_stations);
189 spin_unlock(&sdata->cleanup_stations_lock);
190
191 ieee80211_queue_work(&sdata->local->hw, &sdata->cleanup_stations_wk);
b22cfcfc
EP
192}
193
d0709a65 194/* protected by RCU */
abe60632
JB
195struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
196 const u8 *addr)
f0706e82 197{
abe60632 198 struct ieee80211_local *local = sdata->local;
f0706e82
JB
199 struct sta_info *sta;
200
0379185b 201 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
0379185b 202 lockdep_is_held(&local->sta_mtx));
f0706e82 203 while (sta) {
abe60632 204 if (sta->sdata == sdata &&
b203ca39 205 ether_addr_equal(sta->sta.addr, addr))
f0706e82 206 break;
0379185b 207 sta = rcu_dereference_check(sta->hnext,
0379185b 208 lockdep_is_held(&local->sta_mtx));
f0706e82 209 }
43ba7e95
JB
210 return sta;
211}
212
0e5ded5a
FF
213/*
214 * Get sta info either from the specified interface
215 * or from one of its vlans
216 */
217struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
218 const u8 *addr)
219{
220 struct ieee80211_local *local = sdata->local;
221 struct sta_info *sta;
222
0379185b 223 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
0379185b 224 lockdep_is_held(&local->sta_mtx));
0e5ded5a
FF
225 while (sta) {
226 if ((sta->sdata == sdata ||
a2c1e3da 227 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
b203ca39 228 ether_addr_equal(sta->sta.addr, addr))
0e5ded5a 229 break;
0379185b 230 sta = rcu_dereference_check(sta->hnext,
0379185b 231 lockdep_is_held(&local->sta_mtx));
0e5ded5a
FF
232 }
233 return sta;
234}
235
3b53fde8
JB
236struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
237 int idx)
ee385855 238{
3b53fde8 239 struct ieee80211_local *local = sdata->local;
ee385855
LCC
240 struct sta_info *sta;
241 int i = 0;
242
d0709a65 243 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3b53fde8 244 if (sdata != sta->sdata)
2a8ca29a 245 continue;
ee385855
LCC
246 if (i < idx) {
247 ++i;
248 continue;
ee385855 249 }
2a8ca29a 250 return sta;
ee385855 251 }
ee385855
LCC
252
253 return NULL;
254}
f0706e82 255
93e5deb1 256/**
d9a7ddb0 257 * sta_info_free - free STA
93e5deb1 258 *
6ef307bc 259 * @local: pointer to the global information
93e5deb1
JB
260 * @sta: STA info to free
261 *
262 * This function must undo everything done by sta_info_alloc()
d9a7ddb0
JB
263 * that may happen before sta_info_insert(). It may only be
264 * called when sta_info_insert() has not been attempted (and
265 * if that fails, the station is freed anyway.)
93e5deb1 266 */
d9a7ddb0 267void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
93e5deb1 268{
889cbb91 269 if (sta->rate_ctrl)
af65cd96 270 rate_control_free_sta(sta);
93e5deb1 271
bdcbd8e0 272 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
93e5deb1
JB
273
274 kfree(sta);
275}
276
4d33960b 277/* Caller must hold local->sta_mtx */
d0709a65
JB
278static void sta_info_hash_add(struct ieee80211_local *local,
279 struct sta_info *sta)
f0706e82 280{
4d33960b 281 lockdep_assert_held(&local->sta_mtx);
17741cdc 282 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
cf778b00 283 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
f0706e82 284}
f0706e82 285
af818581
JB
286static void sta_unblock(struct work_struct *wk)
287{
288 struct sta_info *sta;
289
290 sta = container_of(wk, struct sta_info, drv_unblock_wk);
291
292 if (sta->dead)
293 return;
294
54420473
HS
295 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
296 local_bh_disable();
af818581 297 ieee80211_sta_ps_deliver_wakeup(sta);
54420473
HS
298 local_bh_enable();
299 } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
c2c98fde 300 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
ce662b44
JB
301
302 local_bh_disable();
af818581 303 ieee80211_sta_ps_deliver_poll_response(sta);
ce662b44 304 local_bh_enable();
c2c98fde
JB
305 } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
306 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
ce662b44
JB
307
308 local_bh_disable();
47086fc5 309 ieee80211_sta_ps_deliver_uapsd(sta);
ce662b44 310 local_bh_enable();
50a9432d 311 } else
c2c98fde 312 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
af818581
JB
313}
314
af65cd96
JB
315static int sta_prepare_rate_control(struct ieee80211_local *local,
316 struct sta_info *sta, gfp_t gfp)
317{
318 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
319 return 0;
320
889cbb91 321 sta->rate_ctrl = local->rate_ctrl;
af65cd96
JB
322 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
323 &sta->sta, gfp);
889cbb91 324 if (!sta->rate_ctrl_priv)
af65cd96 325 return -ENOMEM;
af65cd96
JB
326
327 return 0;
328}
329
73651ee6 330struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
56544160 331 const u8 *addr, gfp_t gfp)
f0706e82 332{
d0709a65 333 struct ieee80211_local *local = sdata->local;
f0706e82 334 struct sta_info *sta;
ebe27c91 335 struct timespec uptime;
16c5f15c 336 int i;
f0706e82 337
17741cdc 338 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
f0706e82 339 if (!sta)
73651ee6 340 return NULL;
f0706e82 341
07346f81 342 spin_lock_init(&sta->lock);
af818581 343 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
67c282c0 344 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
a93e3644 345 mutex_init(&sta->ampdu_mlme.mtx);
87f59c70
TP
346#ifdef CONFIG_MAC80211_MESH
347 if (ieee80211_vif_is_mesh(&sdata->vif) &&
348 !sdata->u.mesh.user_mpm)
349 init_timer(&sta->plink_timer);
6c7c4cbf 350 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
87f59c70 351#endif
07346f81 352
17741cdc 353 memcpy(sta->sta.addr, addr, ETH_ALEN);
d0709a65
JB
354 sta->local = local;
355 sta->sdata = sdata;
8bc8aecd 356 sta->last_rx = jiffies;
f0706e82 357
71ec375c
JB
358 sta->sta_state = IEEE80211_STA_NONE;
359
ebe27c91
MSS
360 do_posix_clock_monotonic_gettime(&uptime);
361 sta->last_connected = uptime.tv_sec;
541a45a1 362 ewma_init(&sta->avg_signal, 1024, 8);
ef0621e8
FF
363 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
364 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
541a45a1 365
af65cd96 366 if (sta_prepare_rate_control(local, sta, gfp)) {
f0706e82 367 kfree(sta);
73651ee6 368 return NULL;
f0706e82
JB
369 }
370
5a306f58 371 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
a622ab72
JB
372 /*
373 * timer_to_tid must be initialized with identity mapping
374 * to enable session_timer's data differentiation. See
375 * sta_rx_agg_session_timer_expired for usage.
376 */
16c5f15c 377 sta->timer_to_tid[i] = i;
16c5f15c 378 }
948d887d
JB
379 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
380 skb_queue_head_init(&sta->ps_tx_buf[i]);
381 skb_queue_head_init(&sta->tx_filtered[i]);
382 }
73651ee6 383
5a306f58 384 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4be929be 385 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
cccaec98 386
af0ed69b 387 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
687da132
EG
388 if (sdata->vif.type == NL80211_IFTYPE_AP ||
389 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
390 struct ieee80211_supported_band *sband =
391 local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
392 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
393 IEEE80211_HT_CAP_SM_PS_SHIFT;
394 /*
395 * Assume that hostapd advertises our caps in the beacon and
396 * this is the known_smps_mode for a station that just assciated
397 */
398 switch (smps) {
399 case WLAN_HT_SMPS_CONTROL_DISABLED:
400 sta->known_smps_mode = IEEE80211_SMPS_OFF;
401 break;
402 case WLAN_HT_SMPS_CONTROL_STATIC:
403 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
404 break;
405 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
406 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
407 break;
408 default:
409 WARN_ON(1);
410 }
411 }
af0ed69b 412
bdcbd8e0 413 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
73651ee6
JB
414
415 return sta;
416}
417
8c71df7a 418static int sta_info_insert_check(struct sta_info *sta)
34e89507 419{
34e89507 420 struct ieee80211_sub_if_data *sdata = sta->sdata;
34e89507 421
03e4497e
JB
422 /*
423 * Can't be a WARN_ON because it can be triggered through a race:
424 * something inserts a STA (on one CPU) without holding the RTNL
425 * and another CPU turns off the net device.
426 */
8c71df7a
GE
427 if (unlikely(!ieee80211_sdata_running(sdata)))
428 return -ENETDOWN;
03e4497e 429
b203ca39 430 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
8c71df7a
GE
431 is_multicast_ether_addr(sta->sta.addr)))
432 return -EINVAL;
433
434 return 0;
435}
436
f09603a2
JB
437static int sta_info_insert_drv_state(struct ieee80211_local *local,
438 struct ieee80211_sub_if_data *sdata,
439 struct sta_info *sta)
440{
441 enum ieee80211_sta_state state;
442 int err = 0;
443
444 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
445 err = drv_sta_state(local, sdata, sta, state, state + 1);
446 if (err)
447 break;
448 }
449
450 if (!err) {
a4ec45a4
JB
451 /*
452 * Drivers using legacy sta_add/sta_remove callbacks only
453 * get uploaded set to true after sta_add is called.
454 */
455 if (!local->ops->sta_add)
456 sta->uploaded = true;
f09603a2
JB
457 return 0;
458 }
459
460 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
bdcbd8e0
JB
461 sdata_info(sdata,
462 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
463 sta->sta.addr, state + 1, err);
f09603a2
JB
464 err = 0;
465 }
466
467 /* unwind on error */
468 for (; state > IEEE80211_STA_NOTEXIST; state--)
469 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
470
471 return err;
472}
473
8c71df7a
GE
474/*
475 * should be called with sta_mtx locked
476 * this function replaces the mutex lock
477 * with a RCU lock
478 */
4d33960b 479static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8c71df7a
GE
480{
481 struct ieee80211_local *local = sta->local;
482 struct ieee80211_sub_if_data *sdata = sta->sdata;
7852e361 483 struct station_info sinfo;
8c71df7a
GE
484 int err = 0;
485
486 lockdep_assert_held(&local->sta_mtx);
34e89507 487
7852e361
JB
488 /* check if STA exists already */
489 if (sta_info_get_bss(sdata, sta->sta.addr)) {
490 err = -EEXIST;
491 goto out_err;
4d33960b 492 }
32bfd35d 493
7852e361
JB
494 /* notify driver */
495 err = sta_info_insert_drv_state(local, sdata, sta);
496 if (err)
497 goto out_err;
4d33960b 498
7852e361
JB
499 local->num_sta++;
500 local->sta_generation++;
501 smp_mb();
4d33960b 502
7852e361
JB
503 /* make the station visible */
504 sta_info_hash_add(local, sta);
83d5cc01 505
794454ce 506 list_add_rcu(&sta->list, &local->sta_list);
4d33960b 507
7852e361 508 set_sta_flag(sta, WLAN_STA_INSERTED);
4d33960b 509
21f659bf 510 ieee80211_recalc_min_chandef(sdata);
7852e361
JB
511 ieee80211_sta_debugfs_add(sta);
512 rate_control_add_sta_debugfs(sta);
4d33960b 513
7852e361
JB
514 memset(&sinfo, 0, sizeof(sinfo));
515 sinfo.filled = 0;
516 sinfo.generation = local->sta_generation;
517 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
d0709a65 518
bdcbd8e0 519 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
f0706e82 520
34e89507
JB
521 /* move reference to rcu-protected */
522 rcu_read_lock();
523 mutex_unlock(&local->sta_mtx);
e9f207f0 524
73651ee6
JB
525 if (ieee80211_vif_is_mesh(&sdata->vif))
526 mesh_accept_plinks_update(sdata);
527
8c71df7a 528 return 0;
4d33960b
JB
529 out_err:
530 mutex_unlock(&local->sta_mtx);
531 rcu_read_lock();
532 return err;
8c71df7a
GE
533}
534
535int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
536{
537 struct ieee80211_local *local = sta->local;
8c71df7a
GE
538 int err = 0;
539
4d33960b
JB
540 might_sleep();
541
8c71df7a
GE
542 err = sta_info_insert_check(sta);
543 if (err) {
544 rcu_read_lock();
545 goto out_free;
546 }
547
8c71df7a
GE
548 mutex_lock(&local->sta_mtx);
549
4d33960b 550 err = sta_info_insert_finish(sta);
8c71df7a
GE
551 if (err)
552 goto out_free;
553
73651ee6 554 return 0;
93e5deb1
JB
555 out_free:
556 BUG_ON(!err);
d9a7ddb0 557 sta_info_free(local, sta);
93e5deb1 558 return err;
f0706e82
JB
559}
560
34e89507
JB
561int sta_info_insert(struct sta_info *sta)
562{
563 int err = sta_info_insert_rcu(sta);
564
565 rcu_read_unlock();
566
567 return err;
568}
569
d012a605 570static inline void __bss_tim_set(u8 *tim, u16 id)
004c872e
JB
571{
572 /*
573 * This format has been mandated by the IEEE specifications,
574 * so this line may not be changed to use the __set_bit() format.
575 */
d012a605 576 tim[id / 8] |= (1 << (id % 8));
004c872e
JB
577}
578
d012a605 579static inline void __bss_tim_clear(u8 *tim, u16 id)
004c872e
JB
580{
581 /*
582 * This format has been mandated by the IEEE specifications,
583 * so this line may not be changed to use the __clear_bit() format.
584 */
d012a605 585 tim[id / 8] &= ~(1 << (id % 8));
004c872e
JB
586}
587
3d5839b6
IP
588static inline bool __bss_tim_get(u8 *tim, u16 id)
589{
590 /*
591 * This format has been mandated by the IEEE specifications,
592 * so this line may not be changed to use the test_bit() format.
593 */
594 return tim[id / 8] & (1 << (id % 8));
595}
596
948d887d 597static unsigned long ieee80211_tids_for_ac(int ac)
004c872e 598{
948d887d
JB
599 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
600 switch (ac) {
601 case IEEE80211_AC_VO:
602 return BIT(6) | BIT(7);
603 case IEEE80211_AC_VI:
604 return BIT(4) | BIT(5);
605 case IEEE80211_AC_BE:
606 return BIT(0) | BIT(3);
607 case IEEE80211_AC_BK:
608 return BIT(1) | BIT(2);
609 default:
610 WARN_ON(1);
611 return 0;
d0709a65 612 }
004c872e
JB
613}
614
c868cb35 615void sta_info_recalc_tim(struct sta_info *sta)
004c872e 616{
c868cb35 617 struct ieee80211_local *local = sta->local;
d012a605 618 struct ps_data *ps;
948d887d
JB
619 bool indicate_tim = false;
620 u8 ignore_for_tim = sta->sta.uapsd_queues;
621 int ac;
d012a605
MP
622 u16 id;
623
624 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
625 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
626 if (WARN_ON_ONCE(!sta->sdata->bss))
627 return;
004c872e 628
d012a605
MP
629 ps = &sta->sdata->bss->ps;
630 id = sta->sta.aid;
3f52b7e3
MP
631#ifdef CONFIG_MAC80211_MESH
632 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
633 ps = &sta->sdata->u.mesh.ps;
204d1304 634 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6f101ef0 635 id = sta->plid % (IEEE80211_MAX_AID + 1);
3f52b7e3 636#endif
d012a605 637 } else {
c868cb35 638 return;
d012a605 639 }
3e122be0 640
c868cb35
JB
641 /* No need to do anything if the driver does all */
642 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
643 return;
004c872e 644
c868cb35
JB
645 if (sta->dead)
646 goto done;
3e122be0 647
948d887d
JB
648 /*
649 * If all ACs are delivery-enabled then we should build
650 * the TIM bit for all ACs anyway; if only some are then
651 * we ignore those and build the TIM bit using only the
652 * non-enabled ones.
653 */
654 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
655 ignore_for_tim = 0;
656
657 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
658 unsigned long tids;
3e122be0 659
948d887d
JB
660 if (ignore_for_tim & BIT(ac))
661 continue;
662
663 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
664 !skb_queue_empty(&sta->ps_tx_buf[ac]);
665 if (indicate_tim)
666 break;
3e122be0 667
948d887d
JB
668 tids = ieee80211_tids_for_ac(ac);
669
670 indicate_tim |=
671 sta->driver_buffered_tids & tids;
d0709a65 672 }
004c872e 673
c868cb35 674 done:
65f704a5 675 spin_lock_bh(&local->tim_lock);
004c872e 676
3d5839b6
IP
677 if (indicate_tim == __bss_tim_get(ps->tim, id))
678 goto out_unlock;
679
948d887d 680 if (indicate_tim)
d012a605 681 __bss_tim_set(ps->tim, id);
c868cb35 682 else
d012a605 683 __bss_tim_clear(ps->tim, id);
004c872e 684
c868cb35
JB
685 if (local->ops->set_tim) {
686 local->tim_in_locked_section = true;
948d887d 687 drv_set_tim(local, &sta->sta, indicate_tim);
c868cb35
JB
688 local->tim_in_locked_section = false;
689 }
3e122be0 690
3d5839b6 691out_unlock:
65f704a5 692 spin_unlock_bh(&local->tim_lock);
004c872e
JB
693}
694
cd0b8d89 695static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
f0706e82 696{
e039fa4a 697 struct ieee80211_tx_info *info;
f0706e82
JB
698 int timeout;
699
700 if (!skb)
cd0b8d89 701 return false;
f0706e82 702
e039fa4a 703 info = IEEE80211_SKB_CB(skb);
f0706e82
JB
704
705 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
57c4d7b4
JB
706 timeout = (sta->listen_interval *
707 sta->sdata->vif.bss_conf.beacon_int *
708 32 / 15625) * HZ;
f0706e82
JB
709 if (timeout < STA_TX_BUFFER_EXPIRE)
710 timeout = STA_TX_BUFFER_EXPIRE;
e039fa4a 711 return time_after(jiffies, info->control.jiffies + timeout);
f0706e82
JB
712}
713
714
948d887d
JB
715static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
716 struct sta_info *sta, int ac)
f0706e82
JB
717{
718 unsigned long flags;
719 struct sk_buff *skb;
720
60750397
JB
721 /*
722 * First check for frames that should expire on the filtered
723 * queue. Frames here were rejected by the driver and are on
724 * a separate queue to avoid reordering with normal PS-buffered
725 * frames. They also aren't accounted for right now in the
726 * total_ps_buffered counter.
727 */
728 for (;;) {
948d887d
JB
729 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
730 skb = skb_peek(&sta->tx_filtered[ac]);
60750397 731 if (sta_info_buffer_expired(sta, skb))
948d887d 732 skb = __skb_dequeue(&sta->tx_filtered[ac]);
60750397
JB
733 else
734 skb = NULL;
948d887d 735 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
60750397
JB
736
737 /*
738 * Frames are queued in order, so if this one
739 * hasn't expired yet we can stop testing. If
740 * we actually reached the end of the queue we
741 * also need to stop, of course.
742 */
743 if (!skb)
744 break;
d4fa14cd 745 ieee80211_free_txskb(&local->hw, skb);
60750397
JB
746 }
747
748 /*
749 * Now also check the normal PS-buffered queue, this will
750 * only find something if the filtered queue was emptied
751 * since the filtered frames are all before the normal PS
752 * buffered frames.
753 */
f0706e82 754 for (;;) {
948d887d
JB
755 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
756 skb = skb_peek(&sta->ps_tx_buf[ac]);
57c4d7b4 757 if (sta_info_buffer_expired(sta, skb))
948d887d 758 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
836341a7 759 else
f0706e82 760 skb = NULL;
948d887d 761 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
f0706e82 762
60750397
JB
763 /*
764 * frames are queued in order, so if this one
765 * hasn't expired yet (or we reached the end of
766 * the queue) we can stop testing
767 */
836341a7 768 if (!skb)
f0706e82 769 break;
836341a7 770
836341a7 771 local->total_ps_buffered--;
bdcbd8e0
JB
772 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
773 sta->sta.addr);
d4fa14cd 774 ieee80211_free_txskb(&local->hw, skb);
f0706e82 775 }
3393a608 776
60750397
JB
777 /*
778 * Finally, recalculate the TIM bit for this station -- it might
779 * now be clear because the station was too slow to retrieve its
780 * frames.
781 */
782 sta_info_recalc_tim(sta);
783
784 /*
785 * Return whether there are any frames still buffered, this is
786 * used to check whether the cleanup timer still needs to run,
787 * if there are no frames we don't need to rearm the timer.
788 */
948d887d
JB
789 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
790 skb_queue_empty(&sta->tx_filtered[ac]));
791}
792
793static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
794 struct sta_info *sta)
795{
796 bool have_buffered = false;
797 int ac;
798
3f52b7e3
MP
799 /* This is only necessary for stations on BSS/MBSS interfaces */
800 if (!sta->sdata->bss &&
801 !ieee80211_vif_is_mesh(&sta->sdata->vif))
948d887d
JB
802 return false;
803
804 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
805 have_buffered |=
806 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
807
808 return have_buffered;
f0706e82
JB
809}
810
83d5cc01 811int __must_check __sta_info_destroy(struct sta_info *sta)
f0706e82 812{
34e89507
JB
813 struct ieee80211_local *local;
814 struct ieee80211_sub_if_data *sdata;
6d10e46b 815 int ret;
f0706e82 816
34e89507 817 might_sleep();
f0706e82 818
34e89507
JB
819 if (!sta)
820 return -ENOENT;
5bb644a0 821
34e89507
JB
822 local = sta->local;
823 sdata = sta->sdata;
f0706e82 824
83d5cc01
JB
825 lockdep_assert_held(&local->sta_mtx);
826
098a6070
JB
827 /*
828 * Before removing the station from the driver and
829 * rate control, it might still start new aggregation
830 * sessions -- block that to make sure the tear-down
831 * will be sufficient.
832 */
c2c98fde 833 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
c82c4a80 834 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
098a6070 835
34e89507 836 ret = sta_info_hash_del(local, sta);
34e89507
JB
837 if (ret)
838 return ret;
839
794454ce 840 list_del_rcu(&sta->list);
4d33960b 841
6d10e46b
JB
842 /* this always calls synchronize_net() */
843 ieee80211_free_sta_keys(local, sta);
34e89507
JB
844
845 sta->dead = true;
846
34e89507
JB
847 local->num_sta--;
848 local->sta_generation++;
849
850 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
a9b3cd7f 851 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
34e89507 852
83d5cc01 853 while (sta->sta_state > IEEE80211_STA_NONE) {
f09603a2
JB
854 ret = sta_info_move_state(sta, sta->sta_state - 1);
855 if (ret) {
83d5cc01
JB
856 WARN_ON_ONCE(1);
857 break;
858 }
859 }
d9a7ddb0 860
f09603a2 861 if (sta->uploaded) {
f09603a2
JB
862 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
863 IEEE80211_STA_NOTEXIST);
864 WARN_ON_ONCE(ret != 0);
865 }
34e89507 866
bdcbd8e0
JB
867 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
868
ec15e68b
JM
869 cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
870
34e89507
JB
871 rate_control_remove_sta_debugfs(sta);
872 ieee80211_sta_debugfs_remove(sta);
21f659bf 873 ieee80211_recalc_min_chandef(sdata);
34e89507 874
b22cfcfc 875 call_rcu(&sta->rcu_head, free_sta_rcu);
34e89507
JB
876
877 return 0;
4d6141c3
JS
878}
879
34e89507 880int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
4d6141c3 881{
34e89507
JB
882 struct sta_info *sta;
883 int ret;
4d6141c3 884
34e89507 885 mutex_lock(&sdata->local->sta_mtx);
7852e361 886 sta = sta_info_get(sdata, addr);
34e89507
JB
887 ret = __sta_info_destroy(sta);
888 mutex_unlock(&sdata->local->sta_mtx);
4d6141c3
JS
889
890 return ret;
891}
892
34e89507
JB
893int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
894 const u8 *addr)
e9f207f0 895{
34e89507
JB
896 struct sta_info *sta;
897 int ret;
e9f207f0 898
34e89507 899 mutex_lock(&sdata->local->sta_mtx);
7852e361 900 sta = sta_info_get_bss(sdata, addr);
34e89507
JB
901 ret = __sta_info_destroy(sta);
902 mutex_unlock(&sdata->local->sta_mtx);
d0709a65 903
34e89507
JB
904 return ret;
905}
e9f207f0 906
34e89507
JB
907static void sta_info_cleanup(unsigned long data)
908{
909 struct ieee80211_local *local = (struct ieee80211_local *) data;
910 struct sta_info *sta;
3393a608 911 bool timer_needed = false;
34e89507
JB
912
913 rcu_read_lock();
914 list_for_each_entry_rcu(sta, &local->sta_list, list)
3393a608
JO
915 if (sta_info_cleanup_expire_buffered(local, sta))
916 timer_needed = true;
34e89507 917 rcu_read_unlock();
e9f207f0 918
34e89507
JB
919 if (local->quiescing)
920 return;
d0709a65 921
3393a608
JO
922 if (!timer_needed)
923 return;
924
26d59535
JB
925 mod_timer(&local->sta_cleanup,
926 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
e9f207f0 927}
e9f207f0 928
f0706e82
JB
929void sta_info_init(struct ieee80211_local *local)
930{
4d33960b 931 spin_lock_init(&local->tim_lock);
34e89507 932 mutex_init(&local->sta_mtx);
f0706e82 933 INIT_LIST_HEAD(&local->sta_list);
f0706e82 934
b24b8a24
PE
935 setup_timer(&local->sta_cleanup, sta_info_cleanup,
936 (unsigned long)local);
f0706e82
JB
937}
938
939void sta_info_stop(struct ieee80211_local *local)
940{
a56f992c 941 del_timer_sync(&local->sta_cleanup);
f0706e82
JB
942}
943
051007d9
JB
944
945int sta_info_flush_defer(struct ieee80211_sub_if_data *sdata)
f0706e82 946{
b998e8bb 947 struct ieee80211_local *local = sdata->local;
f0706e82 948 struct sta_info *sta, *tmp;
44213b5e 949 int ret = 0;
f0706e82 950
d0709a65 951 might_sleep();
be8755e1 952
34e89507 953 mutex_lock(&local->sta_mtx);
d0709a65 954 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
b998e8bb 955 if (sdata == sta->sdata) {
34e89507 956 WARN_ON(__sta_info_destroy(sta));
34316837
JB
957 ret++;
958 }
be8755e1 959 }
34e89507 960 mutex_unlock(&local->sta_mtx);
44213b5e 961
051007d9
JB
962 return ret;
963}
964
965void sta_info_flush_cleanup(struct ieee80211_sub_if_data *sdata)
966{
b998e8bb
JB
967 ieee80211_cleanup_sdata_stas(sdata);
968 cancel_work_sync(&sdata->cleanup_stations_wk);
f0706e82 969}
dc6676b7 970
24723d1b
JB
971void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
972 unsigned long exp_time)
973{
974 struct ieee80211_local *local = sdata->local;
975 struct sta_info *sta, *tmp;
24723d1b 976
34e89507 977 mutex_lock(&local->sta_mtx);
e46a2cf9
MSS
978
979 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
ec2b774e
ML
980 if (sdata != sta->sdata)
981 continue;
982
24723d1b 983 if (time_after(jiffies, sta->last_rx + exp_time)) {
eea57d42
MSS
984 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
985 sta->sta.addr);
3f52b7e3
MP
986
987 if (ieee80211_vif_is_mesh(&sdata->vif) &&
988 test_sta_flag(sta, WLAN_STA_PS_STA))
989 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
990
34e89507 991 WARN_ON(__sta_info_destroy(sta));
24723d1b 992 }
e46a2cf9
MSS
993 }
994
34e89507 995 mutex_unlock(&local->sta_mtx);
24723d1b 996}
17741cdc 997
686b9cb9
BG
998struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
999 const u8 *addr,
1000 const u8 *localaddr)
17741cdc 1001{
abe60632 1002 struct sta_info *sta, *nxt;
17741cdc 1003
686b9cb9
BG
1004 /*
1005 * Just return a random station if localaddr is NULL
1006 * ... first in list.
1007 */
f7c65594 1008 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
686b9cb9 1009 if (localaddr &&
b203ca39 1010 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
686b9cb9 1011 continue;
f7c65594
JB
1012 if (!sta->uploaded)
1013 return NULL;
abe60632 1014 return &sta->sta;
f7c65594
JB
1015 }
1016
abe60632 1017 return NULL;
17741cdc 1018}
686b9cb9 1019EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
5ed176e1
JB
1020
1021struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1022 const u8 *addr)
1023{
f7c65594 1024 struct sta_info *sta;
5ed176e1
JB
1025
1026 if (!vif)
1027 return NULL;
1028
f7c65594
JB
1029 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1030 if (!sta)
1031 return NULL;
1032
1033 if (!sta->uploaded)
1034 return NULL;
5ed176e1 1035
f7c65594 1036 return &sta->sta;
5ed176e1 1037}
17741cdc 1038EXPORT_SYMBOL(ieee80211_find_sta);
af818581 1039
50a9432d
JB
1040static void clear_sta_ps_flags(void *_sta)
1041{
1042 struct sta_info *sta = _sta;
608383bf 1043 struct ieee80211_sub_if_data *sdata = sta->sdata;
d012a605
MP
1044 struct ps_data *ps;
1045
1046 if (sdata->vif.type == NL80211_IFTYPE_AP ||
1047 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1048 ps = &sdata->bss->ps;
3f52b7e3
MP
1049 else if (ieee80211_vif_is_mesh(&sdata->vif))
1050 ps = &sdata->u.mesh.ps;
d012a605
MP
1051 else
1052 return;
50a9432d 1053
c2c98fde 1054 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
608383bf 1055 if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
d012a605 1056 atomic_dec(&ps->num_sta_ps);
50a9432d
JB
1057}
1058
af818581
JB
1059/* powersave support code */
1060void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1061{
1062 struct ieee80211_sub_if_data *sdata = sta->sdata;
1063 struct ieee80211_local *local = sdata->local;
948d887d
JB
1064 struct sk_buff_head pending;
1065 int filtered = 0, buffered = 0, ac;
987c285c 1066 unsigned long flags;
948d887d 1067
c2c98fde 1068 clear_sta_flag(sta, WLAN_STA_SP);
47086fc5 1069
5a306f58 1070 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
948d887d 1071 sta->driver_buffered_tids = 0;
af818581 1072
d057e5a3
AN
1073 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1074 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
af818581 1075
948d887d 1076 skb_queue_head_init(&pending);
af818581
JB
1077
1078 /* Send all buffered frames to the station */
948d887d
JB
1079 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1080 int count = skb_queue_len(&pending), tmp;
1081
987c285c 1082 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
948d887d 1083 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
987c285c 1084 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
948d887d
JB
1085 tmp = skb_queue_len(&pending);
1086 filtered += tmp - count;
1087 count = tmp;
1088
987c285c 1089 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
948d887d 1090 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
987c285c 1091 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
948d887d
JB
1092 tmp = skb_queue_len(&pending);
1093 buffered += tmp - count;
1094 }
1095
1096 ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1097
687da132
EG
1098 /* This station just woke up and isn't aware of our SMPS state */
1099 if (!ieee80211_smps_is_restrictive(sta->known_smps_mode,
1100 sdata->smps_mode) &&
1101 sta->known_smps_mode != sdata->bss->req_smps &&
1102 sta_info_tx_streams(sta) != 1) {
1103 ht_dbg(sdata,
1104 "%pM just woke up and MIMO capable - update SMPS\n",
1105 sta->sta.addr);
1106 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1107 sta->sta.addr,
1108 sdata->vif.bss_conf.bssid);
1109 }
1110
af818581
JB
1111 local->total_ps_buffered -= buffered;
1112
c868cb35
JB
1113 sta_info_recalc_tim(sta);
1114
bdcbd8e0
JB
1115 ps_dbg(sdata,
1116 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1117 sta->sta.addr, sta->sta.aid, filtered, buffered);
af818581
JB
1118}
1119
ce662b44
JB
1120static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1121 struct sta_info *sta, int tid,
40b96408 1122 enum ieee80211_frame_release_type reason)
af818581 1123{
af818581 1124 struct ieee80211_local *local = sdata->local;
ce662b44 1125 struct ieee80211_qos_hdr *nullfunc;
af818581 1126 struct sk_buff *skb;
ce662b44
JB
1127 int size = sizeof(*nullfunc);
1128 __le16 fc;
c2c98fde 1129 bool qos = test_sta_flag(sta, WLAN_STA_WME);
ce662b44 1130 struct ieee80211_tx_info *info;
55de908a 1131 struct ieee80211_chanctx_conf *chanctx_conf;
af818581 1132
ce662b44
JB
1133 if (qos) {
1134 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1135 IEEE80211_STYPE_QOS_NULLFUNC |
1136 IEEE80211_FCTL_FROMDS);
1137 } else {
1138 size -= 2;
1139 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1140 IEEE80211_STYPE_NULLFUNC |
1141 IEEE80211_FCTL_FROMDS);
af818581 1142 }
af818581 1143
ce662b44
JB
1144 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1145 if (!skb)
1146 return;
1147
1148 skb_reserve(skb, local->hw.extra_tx_headroom);
1149
1150 nullfunc = (void *) skb_put(skb, size);
1151 nullfunc->frame_control = fc;
1152 nullfunc->duration_id = 0;
1153 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1154 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1155 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1156
59b66255
JB
1157 skb->priority = tid;
1158 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
ce662b44 1159 if (qos) {
ce662b44
JB
1160 nullfunc->qos_ctrl = cpu_to_le16(tid);
1161
40b96408 1162 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
ce662b44
JB
1163 nullfunc->qos_ctrl |=
1164 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1165 }
1166
1167 info = IEEE80211_SKB_CB(skb);
1168
1169 /*
1170 * Tell TX path to send this frame even though the
1171 * STA may still remain is PS mode after this frame
deeaee19
JB
1172 * exchange. Also set EOSP to indicate this packet
1173 * ends the poll/service period.
ce662b44 1174 */
02f2f1a9 1175 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
d6d23de2 1176 IEEE80211_TX_CTL_PS_RESPONSE |
deeaee19
JB
1177 IEEE80211_TX_STATUS_EOSP |
1178 IEEE80211_TX_CTL_REQ_TX_STATUS;
ce662b44 1179
40b96408
JB
1180 drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
1181
89afe614
JB
1182 skb->dev = sdata->dev;
1183
55de908a
JB
1184 rcu_read_lock();
1185 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1186 if (WARN_ON(!chanctx_conf)) {
1187 rcu_read_unlock();
1188 kfree_skb(skb);
1189 return;
1190 }
1191
4bf88530 1192 ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
55de908a 1193 rcu_read_unlock();
ce662b44
JB
1194}
1195
47086fc5
JB
1196static void
1197ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1198 int n_frames, u8 ignored_acs,
1199 enum ieee80211_frame_release_type reason)
af818581
JB
1200{
1201 struct ieee80211_sub_if_data *sdata = sta->sdata;
1202 struct ieee80211_local *local = sdata->local;
4049e09a 1203 bool found = false;
948d887d
JB
1204 bool more_data = false;
1205 int ac;
4049e09a 1206 unsigned long driver_release_tids = 0;
47086fc5 1207 struct sk_buff_head frames;
af818581 1208
deeaee19 1209 /* Service or PS-Poll period starts */
c2c98fde 1210 set_sta_flag(sta, WLAN_STA_SP);
deeaee19 1211
47086fc5 1212 __skb_queue_head_init(&frames);
948d887d
JB
1213
1214 /*
47086fc5 1215 * Get response frame(s) and more data bit for it.
948d887d
JB
1216 */
1217 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4049e09a
JB
1218 unsigned long tids;
1219
47086fc5 1220 if (ignored_acs & BIT(ac))
948d887d
JB
1221 continue;
1222
4049e09a
JB
1223 tids = ieee80211_tids_for_ac(ac);
1224
1225 if (!found) {
1226 driver_release_tids = sta->driver_buffered_tids & tids;
1227 if (driver_release_tids) {
1228 found = true;
1229 } else {
47086fc5
JB
1230 struct sk_buff *skb;
1231
1232 while (n_frames > 0) {
1233 skb = skb_dequeue(&sta->tx_filtered[ac]);
1234 if (!skb) {
1235 skb = skb_dequeue(
1236 &sta->ps_tx_buf[ac]);
1237 if (skb)
1238 local->total_ps_buffered--;
1239 }
1240 if (!skb)
1241 break;
1242 n_frames--;
4049e09a 1243 found = true;
47086fc5
JB
1244 __skb_queue_tail(&frames, skb);
1245 }
948d887d 1246 }
948d887d 1247
4049e09a
JB
1248 /*
1249 * If the driver has data on more than one TID then
1250 * certainly there's more data if we release just a
1251 * single frame now (from a single TID).
1252 */
47086fc5
JB
1253 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1254 hweight16(driver_release_tids) > 1) {
4049e09a
JB
1255 more_data = true;
1256 driver_release_tids =
1257 BIT(ffs(driver_release_tids) - 1);
1258 break;
1259 }
1260 }
948d887d
JB
1261
1262 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1263 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1264 more_data = true;
1265 break;
1266 }
af818581 1267 }
af818581 1268
4049e09a 1269 if (!found) {
ce662b44 1270 int tid;
af818581
JB
1271
1272 /*
ce662b44
JB
1273 * For PS-Poll, this can only happen due to a race condition
1274 * when we set the TIM bit and the station notices it, but
1275 * before it can poll for the frame we expire it.
1276 *
1277 * For uAPSD, this is said in the standard (11.2.1.5 h):
1278 * At each unscheduled SP for a non-AP STA, the AP shall
1279 * attempt to transmit at least one MSDU or MMPDU, but no
1280 * more than the value specified in the Max SP Length field
1281 * in the QoS Capability element from delivery-enabled ACs,
1282 * that are destined for the non-AP STA.
1283 *
1284 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
af818581 1285 */
af818581 1286
ce662b44
JB
1287 /* This will evaluate to 1, 3, 5 or 7. */
1288 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
af818581 1289
40b96408 1290 ieee80211_send_null_response(sdata, sta, tid, reason);
4049e09a
JB
1291 return;
1292 }
af818581 1293
47086fc5
JB
1294 if (!driver_release_tids) {
1295 struct sk_buff_head pending;
1296 struct sk_buff *skb;
40b96408
JB
1297 int num = 0;
1298 u16 tids = 0;
af818581 1299
47086fc5 1300 skb_queue_head_init(&pending);
af818581 1301
47086fc5
JB
1302 while ((skb = __skb_dequeue(&frames))) {
1303 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1304 struct ieee80211_hdr *hdr = (void *) skb->data;
40b96408
JB
1305 u8 *qoshdr = NULL;
1306
1307 num++;
af818581 1308
47086fc5
JB
1309 /*
1310 * Tell TX path to send this frame even though the
1311 * STA may still remain is PS mode after this frame
1312 * exchange.
1313 */
d6d23de2
FF
1314 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1315 IEEE80211_TX_CTL_PS_RESPONSE;
47086fc5
JB
1316
1317 /*
1318 * Use MoreData flag to indicate whether there are
1319 * more buffered frames for this STA
1320 */
24b9c373 1321 if (more_data || !skb_queue_empty(&frames))
47086fc5
JB
1322 hdr->frame_control |=
1323 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
24b9c373
JD
1324 else
1325 hdr->frame_control &=
1326 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
47086fc5 1327
40b96408
JB
1328 if (ieee80211_is_data_qos(hdr->frame_control) ||
1329 ieee80211_is_qos_nullfunc(hdr->frame_control))
1330 qoshdr = ieee80211_get_qos_ctl(hdr);
1331
52a3f20c
MP
1332 /* end service period after last frame */
1333 if (skb_queue_empty(&frames)) {
1334 if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
1335 qoshdr)
1336 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1337
1338 info->flags |= IEEE80211_TX_STATUS_EOSP |
1339 IEEE80211_TX_CTL_REQ_TX_STATUS;
1340 }
deeaee19 1341
40b96408
JB
1342 if (qoshdr)
1343 tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
1344 else
1345 tids |= BIT(0);
1346
47086fc5
JB
1347 __skb_queue_tail(&pending, skb);
1348 }
af818581 1349
40b96408
JB
1350 drv_allow_buffered_frames(local, sta, tids, num,
1351 reason, more_data);
1352
47086fc5 1353 ieee80211_add_pending_skbs(local, &pending);
af818581 1354
c868cb35 1355 sta_info_recalc_tim(sta);
af818581
JB
1356 } else {
1357 /*
4049e09a
JB
1358 * We need to release a frame that is buffered somewhere in the
1359 * driver ... it'll have to handle that.
1360 * Note that, as per the comment above, it'll also have to see
1361 * if there is more than just one frame on the specific TID that
1362 * we're releasing from, and it needs to set the more-data bit
1363 * accordingly if we tell it that there's no more data. If we do
1364 * tell it there's more data, then of course the more-data bit
1365 * needs to be set anyway.
1366 */
1367 drv_release_buffered_frames(local, sta, driver_release_tids,
47086fc5 1368 n_frames, reason, more_data);
4049e09a
JB
1369
1370 /*
1371 * Note that we don't recalculate the TIM bit here as it would
1372 * most likely have no effect at all unless the driver told us
1373 * that the TID became empty before returning here from the
1374 * release function.
1375 * Either way, however, when the driver tells us that the TID
1376 * became empty we'll do the TIM recalculation.
af818581 1377 */
af818581
JB
1378 }
1379}
1380
47086fc5
JB
1381void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1382{
1383 u8 ignore_for_response = sta->sta.uapsd_queues;
1384
1385 /*
1386 * If all ACs are delivery-enabled then we should reply
1387 * from any of them, if only some are enabled we reply
1388 * only from the non-enabled ones.
1389 */
1390 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1391 ignore_for_response = 0;
1392
1393 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1394 IEEE80211_FRAME_RELEASE_PSPOLL);
1395}
1396
1397void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1398{
1399 int n_frames = sta->sta.max_sp;
1400 u8 delivery_enabled = sta->sta.uapsd_queues;
1401
1402 /*
1403 * If we ever grow support for TSPEC this might happen if
1404 * the TSPEC update from hostapd comes in between a trigger
1405 * frame setting WLAN_STA_UAPSD in the RX path and this
1406 * actually getting called.
1407 */
1408 if (!delivery_enabled)
1409 return;
1410
47086fc5
JB
1411 switch (sta->sta.max_sp) {
1412 case 1:
1413 n_frames = 2;
1414 break;
1415 case 2:
1416 n_frames = 4;
1417 break;
1418 case 3:
1419 n_frames = 6;
1420 break;
1421 case 0:
1422 /* XXX: what is a good value? */
1423 n_frames = 8;
1424 break;
1425 }
1426
1427 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1428 IEEE80211_FRAME_RELEASE_UAPSD);
1429}
1430
af818581
JB
1431void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1432 struct ieee80211_sta *pubsta, bool block)
1433{
1434 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1435
b5878a2d
JB
1436 trace_api_sta_block_awake(sta->local, pubsta, block);
1437
af818581 1438 if (block)
c2c98fde
JB
1439 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1440 else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
af818581
JB
1441 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1442}
1443EXPORT_SYMBOL(ieee80211_sta_block_awake);
dcf55fb5 1444
e943789e 1445void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
37fbd908
JB
1446{
1447 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1448 struct ieee80211_local *local = sta->local;
37fbd908
JB
1449
1450 trace_api_eosp(local, pubsta);
1451
e943789e 1452 clear_sta_flag(sta, WLAN_STA_SP);
37fbd908 1453}
e943789e 1454EXPORT_SYMBOL(ieee80211_sta_eosp);
37fbd908 1455
042ec453
JB
1456void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1457 u8 tid, bool buffered)
dcf55fb5
FF
1458{
1459 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1460
5a306f58 1461 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
042ec453
JB
1462 return;
1463
948d887d
JB
1464 if (buffered)
1465 set_bit(tid, &sta->driver_buffered_tids);
1466 else
1467 clear_bit(tid, &sta->driver_buffered_tids);
1468
c868cb35 1469 sta_info_recalc_tim(sta);
dcf55fb5 1470}
042ec453 1471EXPORT_SYMBOL(ieee80211_sta_set_buffered);
d9a7ddb0 1472
83d5cc01
JB
1473int sta_info_move_state(struct sta_info *sta,
1474 enum ieee80211_sta_state new_state)
d9a7ddb0 1475{
8bf11d8d 1476 might_sleep();
d9a7ddb0
JB
1477
1478 if (sta->sta_state == new_state)
1479 return 0;
1480
f09603a2
JB
1481 /* check allowed transitions first */
1482
1483 switch (new_state) {
1484 case IEEE80211_STA_NONE:
1485 if (sta->sta_state != IEEE80211_STA_AUTH)
1486 return -EINVAL;
1487 break;
1488 case IEEE80211_STA_AUTH:
1489 if (sta->sta_state != IEEE80211_STA_NONE &&
1490 sta->sta_state != IEEE80211_STA_ASSOC)
1491 return -EINVAL;
1492 break;
1493 case IEEE80211_STA_ASSOC:
1494 if (sta->sta_state != IEEE80211_STA_AUTH &&
1495 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1496 return -EINVAL;
1497 break;
1498 case IEEE80211_STA_AUTHORIZED:
1499 if (sta->sta_state != IEEE80211_STA_ASSOC)
1500 return -EINVAL;
1501 break;
1502 default:
1503 WARN(1, "invalid state %d", new_state);
1504 return -EINVAL;
1505 }
1506
bdcbd8e0
JB
1507 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1508 sta->sta.addr, new_state);
f09603a2
JB
1509
1510 /*
1511 * notify the driver before the actual changes so it can
1512 * fail the transition
1513 */
1514 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1515 int err = drv_sta_state(sta->local, sta->sdata, sta,
1516 sta->sta_state, new_state);
1517 if (err)
1518 return err;
1519 }
1520
1521 /* reflect the change in all state variables */
1522
d9a7ddb0
JB
1523 switch (new_state) {
1524 case IEEE80211_STA_NONE:
1525 if (sta->sta_state == IEEE80211_STA_AUTH)
1526 clear_bit(WLAN_STA_AUTH, &sta->_flags);
d9a7ddb0
JB
1527 break;
1528 case IEEE80211_STA_AUTH:
1529 if (sta->sta_state == IEEE80211_STA_NONE)
1530 set_bit(WLAN_STA_AUTH, &sta->_flags);
1531 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1532 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
d9a7ddb0
JB
1533 break;
1534 case IEEE80211_STA_ASSOC:
29623892 1535 if (sta->sta_state == IEEE80211_STA_AUTH) {
d9a7ddb0 1536 set_bit(WLAN_STA_ASSOC, &sta->_flags);
29623892 1537 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
7e3ed02c
FF
1538 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1539 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1540 !sta->sdata->u.vlan.sta))
1541 atomic_dec(&sta->sdata->bss->num_mcast_sta);
d9a7ddb0 1542 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
f09603a2 1543 }
d9a7ddb0
JB
1544 break;
1545 case IEEE80211_STA_AUTHORIZED:
29623892 1546 if (sta->sta_state == IEEE80211_STA_ASSOC) {
7e3ed02c
FF
1547 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1548 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1549 !sta->sdata->u.vlan.sta))
1550 atomic_inc(&sta->sdata->bss->num_mcast_sta);
d9a7ddb0 1551 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
f09603a2 1552 }
d9a7ddb0
JB
1553 break;
1554 default:
f09603a2 1555 break;
d9a7ddb0
JB
1556 }
1557
d9a7ddb0
JB
1558 sta->sta_state = new_state;
1559
1560 return 0;
1561}
687da132
EG
1562
1563u8 sta_info_tx_streams(struct sta_info *sta)
1564{
1565 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1566 u8 rx_streams;
1567
1568 if (!sta->sta.ht_cap.ht_supported)
1569 return 1;
1570
1571 if (sta->sta.vht_cap.vht_supported) {
1572 int i;
1573 u16 tx_mcs_map =
1574 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1575
1576 for (i = 7; i >= 0; i--)
1577 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1578 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1579 return i + 1;
1580 }
1581
1582 if (ht_cap->mcs.rx_mask[3])
1583 rx_streams = 4;
1584 else if (ht_cap->mcs.rx_mask[2])
1585 rx_streams = 3;
1586 else if (ht_cap->mcs.rx_mask[1])
1587 rx_streams = 2;
1588 else
1589 rx_streams = 1;
1590
1591 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1592 return rx_streams;
1593
1594 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1595 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1596}
This page took 0.608503 seconds and 5 git commands to generate.