mac80211: allow station add/remove to sleep
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / sta_info.c
blob211c475f73c6f19083654760214b5ad3582d6c34
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
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 */
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>
17 #include <linux/timer.h>
18 #include <linux/rtnetlink.h>
20 #include <net/mac80211.h>
21 #include "ieee80211_i.h"
22 #include "driver-ops.h"
23 #include "rate.h"
24 #include "sta_info.h"
25 #include "debugfs_sta.h"
26 #include "mesh.h"
28 /**
29 * DOC: STA information lifetime rules
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.
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.
44 * When the insertion fails (sta_info_insert()) returns non-zero), the
45 * structure will have been freed by sta_info_insert()!
47 * Station 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 * 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.
55 * In order to remove a STA info structure, various sta_info_destroy_*()
56 * calls are available.
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.
64 /* Caller must hold local->sta_lock */
65 static int sta_info_hash_del(struct ieee80211_local *local,
66 struct sta_info *sta)
68 struct sta_info *s;
70 s = local->sta_hash[STA_HASH(sta->sta.addr)];
71 if (!s)
72 return -ENOENT;
73 if (s == sta) {
74 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
75 s->hnext);
76 return 0;
79 while (s->hnext && s->hnext != sta)
80 s = s->hnext;
81 if (s->hnext) {
82 rcu_assign_pointer(s->hnext, sta->hnext);
83 return 0;
86 return -ENOENT;
89 /* protected by RCU */
90 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
91 const u8 *addr)
93 struct ieee80211_local *local = sdata->local;
94 struct sta_info *sta;
96 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
97 while (sta) {
98 if (sta->sdata == sdata &&
99 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
100 break;
101 sta = rcu_dereference(sta->hnext);
103 return sta;
107 * Get sta info either from the specified interface
108 * or from one of its vlans
110 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
111 const u8 *addr)
113 struct ieee80211_local *local = sdata->local;
114 struct sta_info *sta;
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);
124 return sta;
127 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
128 int idx)
130 struct ieee80211_local *local = sdata->local;
131 struct sta_info *sta;
132 int i = 0;
134 list_for_each_entry_rcu(sta, &local->sta_list, list) {
135 if (sdata != sta->sdata)
136 continue;
137 if (i < idx) {
138 ++i;
139 continue;
141 return sta;
144 return NULL;
148 * __sta_info_free - internal STA free helper
150 * @local: pointer to the global information
151 * @sta: STA info to free
153 * This function must undo everything done by sta_info_alloc()
154 * that may happen before sta_info_insert().
156 static void __sta_info_free(struct ieee80211_local *local,
157 struct sta_info *sta)
159 if (sta->rate_ctrl) {
160 rate_control_free_sta(sta);
161 rate_control_put(sta->rate_ctrl);
164 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
165 printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
166 wiphy_name(local->hw.wiphy), sta->sta.addr);
167 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
169 kfree(sta);
172 /* Caller must hold local->sta_lock */
173 static void sta_info_hash_add(struct ieee80211_local *local,
174 struct sta_info *sta)
176 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
177 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
180 static void sta_unblock(struct work_struct *wk)
182 struct sta_info *sta;
184 sta = container_of(wk, struct sta_info, drv_unblock_wk);
186 if (sta->dead)
187 return;
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);
195 static int sta_prepare_rate_control(struct ieee80211_local *local,
196 struct sta_info *sta, gfp_t gfp)
198 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
199 return 0;
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;
209 return 0;
212 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
213 u8 *addr, gfp_t gfp)
215 struct ieee80211_local *local = sdata->local;
216 struct sta_info *sta;
217 int i;
219 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
220 if (!sta)
221 return NULL;
223 spin_lock_init(&sta->lock);
224 spin_lock_init(&sta->flaglock);
225 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
227 memcpy(sta->sta.addr, addr, ETH_ALEN);
228 sta->local = local;
229 sta->sdata = sdata;
231 if (sta_prepare_rate_control(local, sta, gfp)) {
232 kfree(sta);
233 return NULL;
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;
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;
249 skb_queue_head_init(&sta->ps_tx_buf);
250 skb_queue_head_init(&sta->tx_filtered);
252 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
253 sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
255 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
256 printk(KERN_DEBUG "%s: Allocated STA %pM\n",
257 wiphy_name(local->hw.wiphy), sta->sta.addr);
258 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
260 #ifdef CONFIG_MAC80211_MESH
261 sta->plink_state = PLINK_LISTEN;
262 init_timer(&sta->plink_timer);
263 #endif
265 return sta;
268 static int sta_info_finish_insert(struct sta_info *sta, bool async)
270 struct ieee80211_local *local = sta->local;
271 struct ieee80211_sub_if_data *sdata = sta->sdata;
272 struct station_info sinfo;
273 unsigned long flags;
274 int err = 0;
276 WARN_ON(!mutex_is_locked(&local->sta_mtx));
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
299 sdata = sta->sdata;
301 if (!async) {
302 local->num_sta++;
303 local->sta_generation++;
304 smp_mb();
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);
312 list_add(&sta->list, &local->sta_list);
314 ieee80211_sta_debugfs_add(sta);
315 rate_control_add_sta_debugfs(sta);
317 sinfo.filled = 0;
318 sinfo.generation = local->sta_generation;
319 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
322 return 0;
325 static void sta_info_finish_pending(struct ieee80211_local *local)
327 struct sta_info *sta;
328 unsigned long flags;
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);
337 sta_info_finish_insert(sta, true);
339 spin_lock_irqsave(&local->sta_lock, flags);
341 spin_unlock_irqrestore(&local->sta_lock, flags);
344 static void sta_info_finish_work(struct work_struct *work)
346 struct ieee80211_local *local =
347 container_of(work, struct ieee80211_local, sta_finish_work);
349 mutex_lock(&local->sta_mtx);
350 sta_info_finish_pending(local);
351 mutex_unlock(&local->sta_mtx);
354 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
356 struct ieee80211_local *local = sta->local;
357 struct ieee80211_sub_if_data *sdata = sta->sdata;
358 unsigned long flags;
359 int err = 0;
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.
366 if (unlikely(!ieee80211_sdata_running(sdata))) {
367 err = -ENETDOWN;
368 rcu_read_lock();
369 goto out_free;
372 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
373 is_multicast_ether_addr(sta->sta.addr))) {
374 err = -EINVAL;
375 rcu_read_lock();
376 goto out_free;
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.
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;
394 local->num_sta++;
395 local->sta_generation++;
396 smp_mb();
397 sta_info_hash_add(local, sta);
399 list_add_tail(&sta->list, &local->sta_pending_list);
401 rcu_read_lock();
402 spin_unlock_irqrestore(&local->sta_lock, flags);
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 */
409 ieee80211_queue_work(&local->hw, &local->sta_finish_work);
411 return 0;
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.
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.
429 might_sleep();
431 mutex_lock(&local->sta_mtx);
433 spin_lock_irqsave(&local->sta_lock, flags);
434 /* check if STA exists already */
435 if (sta_info_get_bss(sdata, sta->sta.addr)) {
436 spin_unlock_irqrestore(&local->sta_lock, flags);
437 rcu_read_lock();
438 err = -EEXIST;
439 goto out_free;
442 spin_unlock_irqrestore(&local->sta_lock, flags);
444 err = sta_info_finish_insert(sta, false);
445 if (err) {
446 mutex_unlock(&local->sta_mtx);
447 rcu_read_lock();
448 goto out_free;
451 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
452 printk(KERN_DEBUG "%s: Inserted STA %pM\n",
453 wiphy_name(local->hw.wiphy), sta->sta.addr);
454 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
456 /* move reference to rcu-protected */
457 rcu_read_lock();
458 mutex_unlock(&local->sta_mtx);
460 if (ieee80211_vif_is_mesh(&sdata->vif))
461 mesh_accept_plinks_update(sdata);
463 return 0;
464 out_free:
465 BUG_ON(!err);
466 __sta_info_free(local, sta);
467 return err;
470 int sta_info_insert(struct sta_info *sta)
472 int err = sta_info_insert_rcu(sta);
474 rcu_read_unlock();
476 return err;
479 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
482 * This format has been mandated by the IEEE specifications,
483 * so this line may not be changed to use the __set_bit() format.
485 bss->tim[aid / 8] |= (1 << (aid % 8));
488 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
491 * This format has been mandated by the IEEE specifications,
492 * so this line may not be changed to use the __clear_bit() format.
494 bss->tim[aid / 8] &= ~(1 << (aid % 8));
497 static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
498 struct sta_info *sta)
500 BUG_ON(!bss);
502 __bss_tim_set(bss, sta->sta.aid);
504 if (sta->local->ops->set_tim) {
505 sta->local->tim_in_locked_section = true;
506 drv_set_tim(sta->local, &sta->sta, true);
507 sta->local->tim_in_locked_section = false;
511 void sta_info_set_tim_bit(struct sta_info *sta)
513 unsigned long flags;
515 BUG_ON(!sta->sdata->bss);
517 spin_lock_irqsave(&sta->local->sta_lock, flags);
518 __sta_info_set_tim_bit(sta->sdata->bss, sta);
519 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
522 static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
523 struct sta_info *sta)
525 BUG_ON(!bss);
527 __bss_tim_clear(bss, sta->sta.aid);
529 if (sta->local->ops->set_tim) {
530 sta->local->tim_in_locked_section = true;
531 drv_set_tim(sta->local, &sta->sta, false);
532 sta->local->tim_in_locked_section = false;
536 void sta_info_clear_tim_bit(struct sta_info *sta)
538 unsigned long flags;
540 BUG_ON(!sta->sdata->bss);
542 spin_lock_irqsave(&sta->local->sta_lock, flags);
543 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
544 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
547 static int sta_info_buffer_expired(struct sta_info *sta,
548 struct sk_buff *skb)
550 struct ieee80211_tx_info *info;
551 int timeout;
553 if (!skb)
554 return 0;
556 info = IEEE80211_SKB_CB(skb);
558 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
559 timeout = (sta->listen_interval *
560 sta->sdata->vif.bss_conf.beacon_int *
561 32 / 15625) * HZ;
562 if (timeout < STA_TX_BUFFER_EXPIRE)
563 timeout = STA_TX_BUFFER_EXPIRE;
564 return time_after(jiffies, info->control.jiffies + timeout);
568 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
569 struct sta_info *sta)
571 unsigned long flags;
572 struct sk_buff *skb;
573 struct ieee80211_sub_if_data *sdata;
575 if (skb_queue_empty(&sta->ps_tx_buf))
576 return;
578 for (;;) {
579 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
580 skb = skb_peek(&sta->ps_tx_buf);
581 if (sta_info_buffer_expired(sta, skb))
582 skb = __skb_dequeue(&sta->ps_tx_buf);
583 else
584 skb = NULL;
585 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
587 if (!skb)
588 break;
590 sdata = sta->sdata;
591 local->total_ps_buffered--;
592 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
593 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
594 sta->sta.addr);
595 #endif
596 dev_kfree_skb(skb);
598 if (skb_queue_empty(&sta->ps_tx_buf))
599 sta_info_clear_tim_bit(sta);
603 static int __must_check __sta_info_destroy(struct sta_info *sta)
605 struct ieee80211_local *local;
606 struct ieee80211_sub_if_data *sdata;
607 struct sk_buff *skb;
608 unsigned long flags;
609 int ret, i;
611 might_sleep();
613 if (!sta)
614 return -ENOENT;
616 local = sta->local;
617 sdata = sta->sdata;
619 spin_lock_irqsave(&local->sta_lock, flags);
620 ret = sta_info_hash_del(local, sta);
621 /* this might still be the pending list ... which is fine */
622 if (!ret)
623 list_del(&sta->list);
624 spin_unlock_irqrestore(&local->sta_lock, flags);
625 if (ret)
626 return ret;
628 if (sta->key) {
629 ieee80211_key_free(sta->key);
631 * We have only unlinked the key, and actually destroying it
632 * may mean it is removed from hardware which requires that
633 * the key->sta pointer is still valid, so flush the key todo
634 * list here.
636 * ieee80211_key_todo() will synchronize_rcu() so after this
637 * nothing can reference this sta struct any more.
639 ieee80211_key_todo();
641 WARN_ON(sta->key);
644 sta->dead = true;
646 if (test_and_clear_sta_flags(sta,
647 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
648 BUG_ON(!sdata->bss);
650 atomic_dec(&sdata->bss->num_sta_ps);
651 __sta_info_clear_tim_bit(sdata->bss, sta);
654 local->num_sta--;
655 local->sta_generation++;
657 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
658 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
660 if (sta->uploaded) {
661 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
662 sdata = container_of(sdata->bss,
663 struct ieee80211_sub_if_data,
664 u.ap);
665 drv_sta_remove(local, sdata, &sta->sta);
666 sdata = sta->sdata;
669 #ifdef CONFIG_MAC80211_MESH
670 if (ieee80211_vif_is_mesh(&sdata->vif)) {
671 mesh_accept_plinks_update(sdata);
672 del_timer(&sta->plink_timer);
674 #endif
676 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
677 printk(KERN_DEBUG "%s: Removed STA %pM\n",
678 wiphy_name(local->hw.wiphy), sta->sta.addr);
679 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
680 cancel_work_sync(&sta->drv_unblock_wk);
682 rate_control_remove_sta_debugfs(sta);
683 ieee80211_sta_debugfs_remove(sta);
685 #ifdef CONFIG_MAC80211_MESH
686 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
687 mesh_plink_deactivate(sta);
688 del_timer_sync(&sta->plink_timer);
690 #endif
692 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
693 local->total_ps_buffered--;
694 dev_kfree_skb_any(skb);
697 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
698 dev_kfree_skb_any(skb);
700 for (i = 0; i < STA_TID_NUM; i++) {
701 struct tid_ampdu_rx *tid_rx;
702 struct tid_ampdu_tx *tid_tx;
704 spin_lock_bh(&sta->lock);
705 tid_rx = sta->ampdu_mlme.tid_rx[i];
706 /* Make sure timer won't free the tid_rx struct, see below */
707 if (tid_rx)
708 tid_rx->shutdown = true;
710 spin_unlock_bh(&sta->lock);
713 * Outside spinlock - shutdown is true now so that the timer
714 * won't free tid_rx, we have to do that now. Can't let the
715 * timer do it because we have to sync the timer outside the
716 * lock that it takes itself.
718 if (tid_rx) {
719 del_timer_sync(&tid_rx->session_timer);
720 kfree(tid_rx);
724 * No need to do such complications for TX agg sessions, the
725 * path leading to freeing the tid_tx struct goes via a call
726 * from the driver, and thus needs to look up the sta struct
727 * again, which cannot be found when we get here. Hence, we
728 * just need to delete the timer and free the aggregation
729 * info; we won't be telling the peer about it then but that
730 * doesn't matter if we're not talking to it again anyway.
732 tid_tx = sta->ampdu_mlme.tid_tx[i];
733 if (tid_tx) {
734 del_timer_sync(&tid_tx->addba_resp_timer);
736 * STA removed while aggregation session being
737 * started? Bit odd, but purge frames anyway.
739 skb_queue_purge(&tid_tx->pending);
740 kfree(tid_tx);
744 __sta_info_free(local, sta);
746 return 0;
749 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
751 struct sta_info *sta;
752 int ret;
754 mutex_lock(&sdata->local->sta_mtx);
755 sta = sta_info_get(sdata, addr);
756 ret = __sta_info_destroy(sta);
757 mutex_unlock(&sdata->local->sta_mtx);
759 return ret;
762 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
763 const u8 *addr)
765 struct sta_info *sta;
766 int ret;
768 mutex_lock(&sdata->local->sta_mtx);
769 sta = sta_info_get_bss(sdata, addr);
770 ret = __sta_info_destroy(sta);
771 mutex_unlock(&sdata->local->sta_mtx);
773 return ret;
776 static void sta_info_cleanup(unsigned long data)
778 struct ieee80211_local *local = (struct ieee80211_local *) data;
779 struct sta_info *sta;
781 rcu_read_lock();
782 list_for_each_entry_rcu(sta, &local->sta_list, list)
783 sta_info_cleanup_expire_buffered(local, sta);
784 rcu_read_unlock();
786 if (local->quiescing)
787 return;
789 local->sta_cleanup.expires =
790 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
791 add_timer(&local->sta_cleanup);
794 void sta_info_init(struct ieee80211_local *local)
796 spin_lock_init(&local->sta_lock);
797 mutex_init(&local->sta_mtx);
798 INIT_LIST_HEAD(&local->sta_list);
799 INIT_LIST_HEAD(&local->sta_pending_list);
800 INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
802 setup_timer(&local->sta_cleanup, sta_info_cleanup,
803 (unsigned long)local);
804 local->sta_cleanup.expires =
805 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
808 int sta_info_start(struct ieee80211_local *local)
810 add_timer(&local->sta_cleanup);
811 return 0;
814 void sta_info_stop(struct ieee80211_local *local)
816 del_timer(&local->sta_cleanup);
817 sta_info_flush(local, NULL);
821 * sta_info_flush - flush matching STA entries from the STA table
823 * Returns the number of removed STA entries.
825 * @local: local interface data
826 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
828 int sta_info_flush(struct ieee80211_local *local,
829 struct ieee80211_sub_if_data *sdata)
831 struct sta_info *sta, *tmp;
832 int ret = 0;
834 might_sleep();
836 mutex_lock(&local->sta_mtx);
838 sta_info_finish_pending(local);
840 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
841 if (!sdata || sdata == sta->sdata)
842 WARN_ON(__sta_info_destroy(sta));
844 mutex_unlock(&local->sta_mtx);
846 return ret;
849 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
850 unsigned long exp_time)
852 struct ieee80211_local *local = sdata->local;
853 struct sta_info *sta, *tmp;
855 mutex_lock(&local->sta_mtx);
856 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
857 if (time_after(jiffies, sta->last_rx + exp_time)) {
858 #ifdef CONFIG_MAC80211_IBSS_DEBUG
859 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
860 sdata->name, sta->sta.addr);
861 #endif
862 WARN_ON(__sta_info_destroy(sta));
864 mutex_unlock(&local->sta_mtx);
867 struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
868 const u8 *addr)
870 struct sta_info *sta, *nxt;
872 /* Just return a random station ... first in list ... */
873 for_each_sta_info(hw_to_local(hw), addr, sta, nxt)
874 return &sta->sta;
875 return NULL;
877 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
879 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
880 const u8 *addr)
882 struct ieee80211_sub_if_data *sdata;
884 if (!vif)
885 return NULL;
887 sdata = vif_to_sdata(vif);
889 return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
891 EXPORT_SYMBOL(ieee80211_find_sta);
893 /* powersave support code */
894 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
896 struct ieee80211_sub_if_data *sdata = sta->sdata;
897 struct ieee80211_local *local = sdata->local;
898 int sent, buffered;
900 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
902 if (!skb_queue_empty(&sta->ps_tx_buf))
903 sta_info_clear_tim_bit(sta);
905 /* Send all buffered frames to the station */
906 sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
907 buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
908 sent += buffered;
909 local->total_ps_buffered -= buffered;
911 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
912 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
913 "since STA not sleeping anymore\n", sdata->name,
914 sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
915 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
918 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
920 struct ieee80211_sub_if_data *sdata = sta->sdata;
921 struct ieee80211_local *local = sdata->local;
922 struct sk_buff *skb;
923 int no_pending_pkts;
925 skb = skb_dequeue(&sta->tx_filtered);
926 if (!skb) {
927 skb = skb_dequeue(&sta->ps_tx_buf);
928 if (skb)
929 local->total_ps_buffered--;
931 no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
932 skb_queue_empty(&sta->ps_tx_buf);
934 if (skb) {
935 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
936 struct ieee80211_hdr *hdr =
937 (struct ieee80211_hdr *) skb->data;
940 * Tell TX path to send this frame even though the STA may
941 * still remain is PS mode after this frame exchange.
943 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
945 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
946 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
947 sta->sta.addr, sta->sta.aid,
948 skb_queue_len(&sta->ps_tx_buf));
949 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
951 /* Use MoreData flag to indicate whether there are more
952 * buffered frames for this STA */
953 if (no_pending_pkts)
954 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
955 else
956 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
958 ieee80211_add_pending_skb(local, skb);
960 if (no_pending_pkts)
961 sta_info_clear_tim_bit(sta);
962 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
963 } else {
965 * FIXME: This can be the result of a race condition between
966 * us expiring a frame and the station polling for it.
967 * Should we send it a null-func frame indicating we
968 * have nothing buffered for it?
970 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
971 "though there are no buffered frames for it\n",
972 sdata->name, sta->sta.addr);
973 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
977 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
978 struct ieee80211_sta *pubsta, bool block)
980 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
982 if (block)
983 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
984 else
985 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
987 EXPORT_SYMBOL(ieee80211_sta_block_awake);