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