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