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