]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/mac80211/sta_info.c
wl1251: add NVS in EEPROM support
[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 *
03e4497e
JB
35 * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
36 * that structure. It must then either destroy it using sta_info_destroy()
37 * (which is pretty useless) or insert it into the hash table using
38 * sta_info_insert() which demotes the reference from ownership to a regular
39 * RCU-protected reference; if the function is called without protection by an
93e5deb1
JB
40 * RCU critical section the reference is instantly invalidated. Note that the
41 * caller may not do much with the STA info before inserting it, in particular,
42 * it may not start any mesh peer link management or add encryption keys.
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 *
7e189a12
LR
47 * sta entries are added by mac80211 when you establish a link with a
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
50 * receive an assocation response from the AP. For IBSS this occurs when
51 * we receive a probe response or a beacon from target IBSS network. For
52 * WDS we add the sta for the peer imediately upon device open. When using
53 * AP mode we add stations for each respective station upon request from
54 * userspace through nl80211.
55 *
d0709a65
JB
56 * Because there are debugfs entries for each station, and adding those
57 * must be able to sleep, it is also possible to "pin" a station entry,
58 * that means it can be removed from the hash table but not be freed.
93e5deb1
JB
59 * See the comment in __sta_info_unlink() for more information, this is
60 * an internal capability only.
d0709a65
JB
61 *
62 * In order to remove a STA info structure, the caller needs to first
dbbea671 63 * unlink it (sta_info_unlink()) from the list and hash tables and
3b96766f
JB
64 * then destroy it; sta_info_destroy() will wait for an RCU grace period
65 * to elapse before actually freeing it. Due to the pinning and the
66 * possibility of multiple callers trying to remove the same STA info at
67 * the same time, sta_info_unlink() can clear the STA info pointer it is
68 * passed to indicate that the STA info is owned by somebody else now.
d0709a65 69 *
dbbea671 70 * If sta_info_unlink() did not clear the pointer then the caller owns
d0709a65 71 * the STA info structure now and is responsible of destroying it with
3b96766f 72 * a call to sta_info_destroy().
d0709a65
JB
73 *
74 * In all other cases, there is no concept of ownership on a STA entry,
75 * each structure is owned by the global hash table/list until it is
76 * removed. All users of the structure need to be RCU protected so that
77 * the structure won't be freed before they are done using it.
78 */
f0706e82
JB
79
80/* Caller must hold local->sta_lock */
be8755e1
MW
81static int sta_info_hash_del(struct ieee80211_local *local,
82 struct sta_info *sta)
f0706e82
JB
83{
84 struct sta_info *s;
85
17741cdc 86 s = local->sta_hash[STA_HASH(sta->sta.addr)];
f0706e82 87 if (!s)
be8755e1
MW
88 return -ENOENT;
89 if (s == sta) {
17741cdc 90 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
d0709a65 91 s->hnext);
be8755e1 92 return 0;
f0706e82
JB
93 }
94
be8755e1 95 while (s->hnext && s->hnext != sta)
f0706e82 96 s = s->hnext;
be8755e1 97 if (s->hnext) {
d0709a65 98 rcu_assign_pointer(s->hnext, sta->hnext);
be8755e1
MW
99 return 0;
100 }
f0706e82 101
be8755e1 102 return -ENOENT;
f0706e82
JB
103}
104
d0709a65 105/* protected by RCU */
4b7679a5 106struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
f0706e82
JB
107{
108 struct sta_info *sta;
109
d0709a65 110 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
f0706e82 111 while (sta) {
5cf12e8d 112 if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
f0706e82 113 break;
d0709a65 114 sta = rcu_dereference(sta->hnext);
f0706e82 115 }
43ba7e95
JB
116 return sta;
117}
118
3b53fde8
JB
119struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
120 int idx)
ee385855 121{
3b53fde8 122 struct ieee80211_local *local = sdata->local;
ee385855
LCC
123 struct sta_info *sta;
124 int i = 0;
125
d0709a65 126 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3b53fde8 127 if (sdata != sta->sdata)
2a8ca29a 128 continue;
ee385855
LCC
129 if (i < idx) {
130 ++i;
131 continue;
ee385855 132 }
2a8ca29a 133 return sta;
ee385855 134 }
ee385855
LCC
135
136 return NULL;
137}
f0706e82 138
93e5deb1
JB
139/**
140 * __sta_info_free - internal STA free helper
141 *
6ef307bc 142 * @local: pointer to the global information
93e5deb1
JB
143 * @sta: STA info to free
144 *
145 * This function must undo everything done by sta_info_alloc()
146 * that may happen before sta_info_insert().
147 */
148static void __sta_info_free(struct ieee80211_local *local,
149 struct sta_info *sta)
150{
4b7679a5 151 rate_control_free_sta(sta);
93e5deb1
JB
152 rate_control_put(sta->rate_ctrl);
153
154#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0c68ae26
JB
155 printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
156 wiphy_name(local->hw.wiphy), sta->sta.addr);
93e5deb1
JB
157#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
158
159 kfree(sta);
160}
161
d0709a65 162void sta_info_destroy(struct sta_info *sta)
f0706e82 163{
97bff8ec 164 struct ieee80211_local *local;
f0706e82 165 struct sk_buff *skb;
07db2183 166 int i;
73651ee6 167
97bff8ec
JB
168 might_sleep();
169
73651ee6
JB
170 if (!sta)
171 return;
f0706e82 172
97bff8ec 173 local = sta->local;
d0709a65 174
af818581
JB
175 cancel_work_sync(&sta->drv_unblock_wk);
176
d0709a65
JB
177 rate_control_remove_sta_debugfs(sta);
178 ieee80211_sta_debugfs_remove(sta);
179
180#ifdef CONFIG_MAC80211_MESH
181 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
182 mesh_plink_deactivate(sta);
183#endif
184
3b96766f
JB
185 /*
186 * We have only unlinked the key, and actually destroying it
187 * may mean it is removed from hardware which requires that
188 * the key->sta pointer is still valid, so flush the key todo
189 * list here.
190 *
191 * ieee80211_key_todo() will synchronize_rcu() so after this
192 * nothing can reference this sta struct any more.
193 */
194 ieee80211_key_todo();
d0709a65
JB
195
196#ifdef CONFIG_MAC80211_MESH
197 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
198 del_timer_sync(&sta->plink_timer);
199#endif
200
f0706e82
JB
201 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
202 local->total_ps_buffered--;
203 dev_kfree_skb_any(skb);
204 }
d0709a65
JB
205
206 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
f0706e82 207 dev_kfree_skb_any(skb);
d0709a65 208
fe3bf0f5 209 for (i = 0; i < STA_TID_NUM; i++) {
55687e38
JB
210 struct tid_ampdu_rx *tid_rx;
211 struct tid_ampdu_tx *tid_tx;
212
07346f81 213 spin_lock_bh(&sta->lock);
55687e38
JB
214 tid_rx = sta->ampdu_mlme.tid_rx[i];
215 /* Make sure timer won't free the tid_rx struct, see below */
216 if (tid_rx)
217 tid_rx->shutdown = true;
96f5e66e 218
07346f81 219 spin_unlock_bh(&sta->lock);
55687e38
JB
220
221 /*
222 * Outside spinlock - shutdown is true now so that the timer
223 * won't free tid_rx, we have to do that now. Can't let the
224 * timer do it because we have to sync the timer outside the
225 * lock that it takes itself.
226 */
227 if (tid_rx) {
228 del_timer_sync(&tid_rx->session_timer);
229 kfree(tid_rx);
230 }
231
232 /*
233 * No need to do such complications for TX agg sessions, the
234 * path leading to freeing the tid_tx struct goes via a call
235 * from the driver, and thus needs to look up the sta struct
236 * again, which cannot be found when we get here. Hence, we
237 * just need to delete the timer and free the aggregation
238 * info; we won't be telling the peer about it then but that
239 * doesn't matter if we're not talking to it again anyway.
240 */
241 tid_tx = sta->ampdu_mlme.tid_tx[i];
242 if (tid_tx) {
243 del_timer_sync(&tid_tx->addba_resp_timer);
cd8ffc80
JB
244 /*
245 * STA removed while aggregation session being
246 * started? Bit odd, but purge frames anyway.
247 */
248 skb_queue_purge(&tid_tx->pending);
55687e38
JB
249 kfree(tid_tx);
250 }
fe3bf0f5 251 }
cee24a3e 252
93e5deb1 253 __sta_info_free(local, sta);
f0706e82
JB
254}
255
256
d0709a65
JB
257/* Caller must hold local->sta_lock */
258static void sta_info_hash_add(struct ieee80211_local *local,
259 struct sta_info *sta)
f0706e82 260{
17741cdc
JB
261 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
262 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
f0706e82 263}
f0706e82 264
af818581
JB
265static void sta_unblock(struct work_struct *wk)
266{
267 struct sta_info *sta;
268
269 sta = container_of(wk, struct sta_info, drv_unblock_wk);
270
271 if (sta->dead)
272 return;
273
274 if (!test_sta_flags(sta, WLAN_STA_PS_STA))
275 ieee80211_sta_ps_deliver_wakeup(sta);
276 else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
277 ieee80211_sta_ps_deliver_poll_response(sta);
278}
279
73651ee6
JB
280struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
281 u8 *addr, gfp_t gfp)
f0706e82 282{
d0709a65 283 struct ieee80211_local *local = sdata->local;
f0706e82 284 struct sta_info *sta;
16c5f15c 285 int i;
f0706e82 286
17741cdc 287 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
f0706e82 288 if (!sta)
73651ee6 289 return NULL;
f0706e82 290
07346f81 291 spin_lock_init(&sta->lock);
5a9f7b04 292 spin_lock_init(&sta->flaglock);
af818581 293 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
07346f81 294
17741cdc 295 memcpy(sta->sta.addr, addr, ETH_ALEN);
d0709a65
JB
296 sta->local = local;
297 sta->sdata = sdata;
f0706e82
JB
298
299 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
d0709a65 300 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
4b7679a5 301 &sta->sta, gfp);
f0706e82
JB
302 if (!sta->rate_ctrl_priv) {
303 rate_control_put(sta->rate_ctrl);
f0706e82 304 kfree(sta);
73651ee6 305 return NULL;
f0706e82
JB
306 }
307
16c5f15c
RR
308 for (i = 0; i < STA_TID_NUM; i++) {
309 /* timer_to_tid must be initialized with identity mapping to
310 * enable session_timer's data differentiation. refer to
311 * sta_rx_agg_session_timer_expired for useage */
312 sta->timer_to_tid[i] = i;
cee24a3e
RR
313 /* rx */
314 sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
315 sta->ampdu_mlme.tid_rx[i] = NULL;
316 /* tx */
317 sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
318 sta->ampdu_mlme.tid_tx[i] = NULL;
319 sta->ampdu_mlme.addba_req_num[i] = 0;
16c5f15c 320 }
f0706e82
JB
321 skb_queue_head_init(&sta->ps_tx_buf);
322 skb_queue_head_init(&sta->tx_filtered);
73651ee6 323
cccaec98
SB
324 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
325 sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
326
73651ee6 327#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0c68ae26
JB
328 printk(KERN_DEBUG "%s: Allocated STA %pM\n",
329 wiphy_name(local->hw.wiphy), sta->sta.addr);
73651ee6
JB
330#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
331
03e4497e 332#ifdef CONFIG_MAC80211_MESH
b4e08ea1 333 sta->plink_state = PLINK_LISTEN;
03e4497e
JB
334 init_timer(&sta->plink_timer);
335#endif
336
73651ee6
JB
337 return sta;
338}
339
340int sta_info_insert(struct sta_info *sta)
341{
342 struct ieee80211_local *local = sta->local;
343 struct ieee80211_sub_if_data *sdata = sta->sdata;
344 unsigned long flags;
93e5deb1 345 int err = 0;
73651ee6 346
03e4497e
JB
347 /*
348 * Can't be a WARN_ON because it can be triggered through a race:
349 * something inserts a STA (on one CPU) without holding the RTNL
350 * and another CPU turns off the net device.
351 */
93e5deb1
JB
352 if (unlikely(!netif_running(sdata->dev))) {
353 err = -ENETDOWN;
354 goto out_free;
355 }
03e4497e 356
17741cdc 357 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
c6a1fa12 358 is_multicast_ether_addr(sta->sta.addr))) {
93e5deb1
JB
359 err = -EINVAL;
360 goto out_free;
361 }
44213b5e 362
d0709a65 363 spin_lock_irqsave(&local->sta_lock, flags);
43ba7e95 364 /* check if STA exists already */
4b7679a5 365 if (sta_info_get(local, sta->sta.addr)) {
d0709a65 366 spin_unlock_irqrestore(&local->sta_lock, flags);
93e5deb1
JB
367 err = -EEXIST;
368 goto out_free;
43ba7e95 369 }
f0706e82 370 list_add(&sta->list, &local->sta_list);
f5ea9120 371 local->sta_generation++;
f0706e82
JB
372 local->num_sta++;
373 sta_info_hash_add(local, sta);
32bfd35d 374
d0709a65
JB
375 /* notify driver */
376 if (local->ops->sta_notify) {
05c914fe 377 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3e122be0
JB
378 sdata = container_of(sdata->bss,
379 struct ieee80211_sub_if_data,
380 u.ap);
32bfd35d 381
24487981 382 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
fbc44bf7 383 sdata = sta->sdata;
32bfd35d 384 }
d0709a65 385
f0706e82 386#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0c68ae26
JB
387 printk(KERN_DEBUG "%s: Inserted STA %pM\n",
388 wiphy_name(local->hw.wiphy), sta->sta.addr);
f0706e82
JB
389#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
390
73651ee6
JB
391 spin_unlock_irqrestore(&local->sta_lock, flags);
392
e9f207f0 393#ifdef CONFIG_MAC80211_DEBUGFS
93e5deb1
JB
394 /*
395 * Debugfs entry adding might sleep, so schedule process
be8755e1 396 * context task for adding entry for STAs that do not yet
93e5deb1
JB
397 * have one.
398 * NOTE: due to auto-freeing semantics this may only be done
399 * if the insertion is successful!
400 */
49ec6fa2 401 schedule_work(&local->sta_debugfs_add);
e9f207f0
JB
402#endif
403
73651ee6
JB
404 if (ieee80211_vif_is_mesh(&sdata->vif))
405 mesh_accept_plinks_update(sdata);
406
407 return 0;
93e5deb1
JB
408 out_free:
409 BUG_ON(!err);
410 __sta_info_free(local, sta);
411 return err;
f0706e82
JB
412}
413
004c872e
JB
414static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
415{
416 /*
417 * This format has been mandated by the IEEE specifications,
418 * so this line may not be changed to use the __set_bit() format.
419 */
420 bss->tim[aid / 8] |= (1 << (aid % 8));
421}
422
423static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
424{
425 /*
426 * This format has been mandated by the IEEE specifications,
427 * so this line may not be changed to use the __clear_bit() format.
428 */
429 bss->tim[aid / 8] &= ~(1 << (aid % 8));
430}
431
432static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
433 struct sta_info *sta)
434{
3e122be0
JB
435 BUG_ON(!bss);
436
17741cdc 437 __bss_tim_set(bss, sta->sta.aid);
3e122be0 438
d0709a65
JB
439 if (sta->local->ops->set_tim) {
440 sta->local->tim_in_locked_section = true;
24487981 441 drv_set_tim(sta->local, &sta->sta, true);
d0709a65
JB
442 sta->local->tim_in_locked_section = false;
443 }
004c872e
JB
444}
445
446void sta_info_set_tim_bit(struct sta_info *sta)
447{
d0709a65 448 unsigned long flags;
004c872e 449
3e122be0
JB
450 BUG_ON(!sta->sdata->bss);
451
d0709a65
JB
452 spin_lock_irqsave(&sta->local->sta_lock, flags);
453 __sta_info_set_tim_bit(sta->sdata->bss, sta);
454 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
004c872e
JB
455}
456
457static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
458 struct sta_info *sta)
459{
3e122be0
JB
460 BUG_ON(!bss);
461
17741cdc 462 __bss_tim_clear(bss, sta->sta.aid);
3e122be0 463
d0709a65
JB
464 if (sta->local->ops->set_tim) {
465 sta->local->tim_in_locked_section = true;
24487981 466 drv_set_tim(sta->local, &sta->sta, false);
d0709a65
JB
467 sta->local->tim_in_locked_section = false;
468 }
004c872e
JB
469}
470
471void sta_info_clear_tim_bit(struct sta_info *sta)
472{
d0709a65 473 unsigned long flags;
004c872e 474
3e122be0
JB
475 BUG_ON(!sta->sdata->bss);
476
d0709a65
JB
477 spin_lock_irqsave(&sta->local->sta_lock, flags);
478 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
479 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
004c872e
JB
480}
481
24723d1b 482static void __sta_info_unlink(struct sta_info **sta)
f0706e82 483{
d0709a65
JB
484 struct ieee80211_local *local = (*sta)->local;
485 struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
d0709a65
JB
486 /*
487 * pull caller's reference if we're already gone.
488 */
489 if (sta_info_hash_del(local, *sta)) {
490 *sta = NULL;
491 return;
492 }
be8755e1 493
3b96766f
JB
494 if ((*sta)->key) {
495 ieee80211_key_free((*sta)->key);
496 WARN_ON((*sta)->key);
497 }
498
7d1559f1 499 list_del(&(*sta)->list);
af818581 500 (*sta)->dead = true;
7d1559f1 501
af818581
JB
502 if (test_and_clear_sta_flags(*sta,
503 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
3e122be0
JB
504 BUG_ON(!sdata->bss);
505
506 atomic_dec(&sdata->bss->num_sta_ps);
7d1559f1
JB
507 __sta_info_clear_tim_bit(sdata->bss, *sta);
508 }
509
510 local->num_sta--;
f5ea9120 511 local->sta_generation++;
7d1559f1 512
f14543ee
FF
513 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
514 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
515
7d1559f1 516 if (local->ops->sta_notify) {
05c914fe 517 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3e122be0
JB
518 sdata = container_of(sdata->bss,
519 struct ieee80211_sub_if_data,
520 u.ap);
7d1559f1 521
24487981
JB
522 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
523 &(*sta)->sta);
fbc44bf7 524 sdata = (*sta)->sdata;
7d1559f1
JB
525 }
526
527 if (ieee80211_vif_is_mesh(&sdata->vif)) {
528 mesh_accept_plinks_update(sdata);
529#ifdef CONFIG_MAC80211_MESH
530 del_timer(&(*sta)->plink_timer);
531#endif
532 }
533
534#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0c68ae26
JB
535 printk(KERN_DEBUG "%s: Removed STA %pM\n",
536 wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
7d1559f1
JB
537#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
538
d0709a65 539 /*
7d1559f1 540 * Finally, pull caller's reference if the STA is pinned by the
d0709a65
JB
541 * task that is adding the debugfs entries. In that case, we
542 * leave the STA "to be freed".
543 *
544 * The rules are not trivial, but not too complex either:
545 * (1) pin_status is only modified under the sta_lock
49ec6fa2
JB
546 * (2) STAs may only be pinned under the RTNL so that
547 * sta_info_flush() is guaranteed to actually destroy
548 * all STAs that are active for a given interface, this
549 * is required for correctness because otherwise we
550 * could notify a driver that an interface is going
551 * away and only after that (!) notify it about a STA
552 * on that interface going away.
553 * (3) sta_info_debugfs_add_work() will set the status
d0709a65
JB
554 * to PINNED when it found an item that needs a new
555 * debugfs directory created. In that case, that item
556 * must not be freed although all *RCU* users are done
557 * with it. Hence, we tell the caller of _unlink()
558 * that the item is already gone (as can happen when
559 * two tasks try to unlink/destroy at the same time)
49ec6fa2 560 * (4) We set the pin_status to DESTROY here when we
d0709a65 561 * find such an item.
49ec6fa2 562 * (5) sta_info_debugfs_add_work() will reset the pin_status
d0709a65
JB
563 * from PINNED to NORMAL when it is done with the item,
564 * but will check for DESTROY before resetting it in
565 * which case it will free the item.
566 */
567 if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
568 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
569 *sta = NULL;
570 return;
571 }
f0706e82
JB
572}
573
d0709a65
JB
574void sta_info_unlink(struct sta_info **sta)
575{
576 struct ieee80211_local *local = (*sta)->local;
577 unsigned long flags;
578
579 spin_lock_irqsave(&local->sta_lock, flags);
580 __sta_info_unlink(sta);
581 spin_unlock_irqrestore(&local->sta_lock, flags);
582}
f0706e82 583
57c4d7b4
JB
584static int sta_info_buffer_expired(struct sta_info *sta,
585 struct sk_buff *skb)
f0706e82 586{
e039fa4a 587 struct ieee80211_tx_info *info;
f0706e82
JB
588 int timeout;
589
590 if (!skb)
591 return 0;
592
e039fa4a 593 info = IEEE80211_SKB_CB(skb);
f0706e82
JB
594
595 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
57c4d7b4
JB
596 timeout = (sta->listen_interval *
597 sta->sdata->vif.bss_conf.beacon_int *
598 32 / 15625) * HZ;
f0706e82
JB
599 if (timeout < STA_TX_BUFFER_EXPIRE)
600 timeout = STA_TX_BUFFER_EXPIRE;
e039fa4a 601 return time_after(jiffies, info->control.jiffies + timeout);
f0706e82
JB
602}
603
604
605static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
606 struct sta_info *sta)
607{
608 unsigned long flags;
609 struct sk_buff *skb;
836341a7 610 struct ieee80211_sub_if_data *sdata;
f0706e82
JB
611
612 if (skb_queue_empty(&sta->ps_tx_buf))
613 return;
614
615 for (;;) {
616 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
617 skb = skb_peek(&sta->ps_tx_buf);
57c4d7b4 618 if (sta_info_buffer_expired(sta, skb))
f0706e82 619 skb = __skb_dequeue(&sta->ps_tx_buf);
836341a7 620 else
f0706e82
JB
621 skb = NULL;
622 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
623
836341a7 624 if (!skb)
f0706e82 625 break;
836341a7 626
d0709a65 627 sdata = sta->sdata;
836341a7 628 local->total_ps_buffered--;
f4ea83dd 629#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0c68ae26
JB
630 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
631 sta->sta.addr);
f4ea83dd 632#endif
836341a7
JB
633 dev_kfree_skb(skb);
634
004c872e
JB
635 if (skb_queue_empty(&sta->ps_tx_buf))
636 sta_info_clear_tim_bit(sta);
f0706e82
JB
637 }
638}
639
640
641static void sta_info_cleanup(unsigned long data)
642{
643 struct ieee80211_local *local = (struct ieee80211_local *) data;
644 struct sta_info *sta;
645
d0709a65
JB
646 rcu_read_lock();
647 list_for_each_entry_rcu(sta, &local->sta_list, list)
f0706e82 648 sta_info_cleanup_expire_buffered(local, sta);
d0709a65 649 rcu_read_unlock();
f0706e82 650
5bb644a0
JB
651 if (local->quiescing)
652 return;
653
0d174406
JB
654 local->sta_cleanup.expires =
655 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
f0706e82
JB
656 add_timer(&local->sta_cleanup);
657}
658
e9f207f0 659#ifdef CONFIG_MAC80211_DEBUGFS
4d6141c3
JS
660/*
661 * See comment in __sta_info_unlink,
662 * caller must hold local->sta_lock.
663 */
664static void __sta_info_pin(struct sta_info *sta)
665{
666 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
667 sta->pin_status = STA_INFO_PIN_STAT_PINNED;
668}
669
670/*
671 * See comment in __sta_info_unlink, returns sta if it
672 * needs to be destroyed.
673 */
674static struct sta_info *__sta_info_unpin(struct sta_info *sta)
675{
676 struct sta_info *ret = NULL;
677 unsigned long flags;
678
679 spin_lock_irqsave(&sta->local->sta_lock, flags);
680 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
681 sta->pin_status != STA_INFO_PIN_STAT_PINNED);
682 if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
683 ret = sta;
684 sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
685 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
686
687 return ret;
688}
689
d0709a65 690static void sta_info_debugfs_add_work(struct work_struct *work)
e9f207f0
JB
691{
692 struct ieee80211_local *local =
693 container_of(work, struct ieee80211_local, sta_debugfs_add);
694 struct sta_info *sta, *tmp;
d0709a65 695 unsigned long flags;
e9f207f0 696
49ec6fa2
JB
697 /* We need to keep the RTNL across the whole pinned status. */
698 rtnl_lock();
e9f207f0
JB
699 while (1) {
700 sta = NULL;
d0709a65
JB
701
702 spin_lock_irqsave(&local->sta_lock, flags);
e9f207f0 703 list_for_each_entry(tmp, &local->sta_list, list) {
63044e9f
JB
704 /*
705 * debugfs.add_has_run will be set by
706 * ieee80211_sta_debugfs_add regardless
707 * of what else it does.
708 */
709 if (!tmp->debugfs.add_has_run) {
e9f207f0 710 sta = tmp;
d0709a65 711 __sta_info_pin(sta);
e9f207f0
JB
712 break;
713 }
714 }
d0709a65 715 spin_unlock_irqrestore(&local->sta_lock, flags);
e9f207f0
JB
716
717 if (!sta)
718 break;
719
e9f207f0
JB
720 ieee80211_sta_debugfs_add(sta);
721 rate_control_add_sta_debugfs(sta);
d0709a65
JB
722
723 sta = __sta_info_unpin(sta);
4f6fab47 724 sta_info_destroy(sta);
e9f207f0 725 }
49ec6fa2 726 rtnl_unlock();
e9f207f0
JB
727}
728#endif
729
f0706e82
JB
730void sta_info_init(struct ieee80211_local *local)
731{
d0709a65 732 spin_lock_init(&local->sta_lock);
f0706e82 733 INIT_LIST_HEAD(&local->sta_list);
f0706e82 734
b24b8a24
PE
735 setup_timer(&local->sta_cleanup, sta_info_cleanup,
736 (unsigned long)local);
0d174406
JB
737 local->sta_cleanup.expires =
738 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
e9f207f0
JB
739
740#ifdef CONFIG_MAC80211_DEBUGFS
d0709a65 741 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
e9f207f0 742#endif
f0706e82
JB
743}
744
745int sta_info_start(struct ieee80211_local *local)
746{
747 add_timer(&local->sta_cleanup);
748 return 0;
749}
750
751void sta_info_stop(struct ieee80211_local *local)
752{
f0706e82 753 del_timer(&local->sta_cleanup);
49ec6fa2
JB
754#ifdef CONFIG_MAC80211_DEBUGFS
755 /*
756 * Make sure the debugfs adding work isn't pending after this
757 * because we're about to be destroyed. It doesn't matter
758 * whether it ran or not since we're going to flush all STAs
759 * anyway.
760 */
761 cancel_work_sync(&local->sta_debugfs_add);
762#endif
dc6676b7 763
be8755e1 764 sta_info_flush(local, NULL);
f0706e82
JB
765}
766
f0706e82
JB
767/**
768 * sta_info_flush - flush matching STA entries from the STA table
44213b5e
JB
769 *
770 * Returns the number of removed STA entries.
771 *
f0706e82 772 * @local: local interface data
d0709a65 773 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
f0706e82 774 */
44213b5e 775int sta_info_flush(struct ieee80211_local *local,
af8cdcd8 776 struct ieee80211_sub_if_data *sdata)
f0706e82
JB
777{
778 struct sta_info *sta, *tmp;
be8755e1 779 LIST_HEAD(tmp_list);
44213b5e 780 int ret = 0;
d0709a65 781 unsigned long flags;
f0706e82 782
d0709a65 783 might_sleep();
be8755e1 784
d0709a65
JB
785 spin_lock_irqsave(&local->sta_lock, flags);
786 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
787 if (!sdata || sdata == sta->sdata) {
788 __sta_info_unlink(&sta);
44213b5e 789 if (sta) {
d0709a65 790 list_add_tail(&sta->list, &tmp_list);
44213b5e
JB
791 ret++;
792 }
d0709a65 793 }
be8755e1 794 }
d0709a65
JB
795 spin_unlock_irqrestore(&local->sta_lock, flags);
796
d0709a65
JB
797 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
798 sta_info_destroy(sta);
44213b5e
JB
799
800 return ret;
f0706e82 801}
dc6676b7 802
24723d1b
JB
803void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
804 unsigned long exp_time)
805{
806 struct ieee80211_local *local = sdata->local;
807 struct sta_info *sta, *tmp;
808 LIST_HEAD(tmp_list);
24723d1b
JB
809 unsigned long flags;
810
811 spin_lock_irqsave(&local->sta_lock, flags);
812 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
813 if (time_after(jiffies, sta->last_rx + exp_time)) {
814#ifdef CONFIG_MAC80211_IBSS_DEBUG
0c68ae26
JB
815 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
816 sdata->dev->name, sta->sta.addr);
24723d1b
JB
817#endif
818 __sta_info_unlink(&sta);
819 if (sta)
820 list_add(&sta->list, &tmp_list);
821 }
822 spin_unlock_irqrestore(&local->sta_lock, flags);
823
824 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
825 sta_info_destroy(sta);
826}
17741cdc 827
5ed176e1
JB
828struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
829 const u8 *addr)
17741cdc 830{
4b7679a5 831 struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
17741cdc
JB
832
833 if (!sta)
834 return NULL;
835 return &sta->sta;
836}
5ed176e1
JB
837EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
838
839struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
840 const u8 *addr)
841{
842 struct ieee80211_sub_if_data *sdata;
843
844 if (!vif)
845 return NULL;
846
847 sdata = vif_to_sdata(vif);
848
849 return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
850}
17741cdc 851EXPORT_SYMBOL(ieee80211_find_sta);
af818581
JB
852
853/* powersave support code */
854void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
855{
856 struct ieee80211_sub_if_data *sdata = sta->sdata;
857 struct ieee80211_local *local = sdata->local;
858 int sent, buffered;
859
860 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_AWAKE, &sta->sta);
861
862 if (!skb_queue_empty(&sta->ps_tx_buf))
863 sta_info_clear_tim_bit(sta);
864
865 /* Send all buffered frames to the station */
866 sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
867 buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
868 sent += buffered;
869 local->total_ps_buffered -= buffered;
870
871#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
872 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
873 "since STA not sleeping anymore\n", sdata->dev->name,
874 sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
875#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
876}
877
878void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
879{
880 struct ieee80211_sub_if_data *sdata = sta->sdata;
881 struct ieee80211_local *local = sdata->local;
882 struct sk_buff *skb;
883 int no_pending_pkts;
884
885 skb = skb_dequeue(&sta->tx_filtered);
886 if (!skb) {
887 skb = skb_dequeue(&sta->ps_tx_buf);
888 if (skb)
889 local->total_ps_buffered--;
890 }
891 no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
892 skb_queue_empty(&sta->ps_tx_buf);
893
894 if (skb) {
895 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
896 struct ieee80211_hdr *hdr =
897 (struct ieee80211_hdr *) skb->data;
898
899 /*
900 * Tell TX path to send this frame even though the STA may
901 * still remain is PS mode after this frame exchange.
902 */
903 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
904
905#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
906 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
907 sta->sta.addr, sta->sta.aid,
908 skb_queue_len(&sta->ps_tx_buf));
909#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
910
911 /* Use MoreData flag to indicate whether there are more
912 * buffered frames for this STA */
913 if (no_pending_pkts)
914 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
915 else
916 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
917
918 ieee80211_add_pending_skb(local, skb);
919
920 if (no_pending_pkts)
921 sta_info_clear_tim_bit(sta);
922#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
923 } else {
924 /*
925 * FIXME: This can be the result of a race condition between
926 * us expiring a frame and the station polling for it.
927 * Should we send it a null-func frame indicating we
928 * have nothing buffered for it?
929 */
930 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
931 "though there are no buffered frames for it\n",
932 sdata->dev->name, sta->sta.addr);
933#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
934 }
935}
936
937void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
938 struct ieee80211_sta *pubsta, bool block)
939{
940 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
941
942 if (block)
943 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
944 else
945 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
946}
947EXPORT_SYMBOL(ieee80211_sta_block_awake);