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.
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"
25 #include "debugfs_sta.h"
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 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
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.
44 * When the insertion fails (sta_info_insert()) returns non-zero), the
45 * structure will have been freed by sta_info_insert()!
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.
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.
59 * See the comment in __sta_info_unlink() for more information, this is
60 * an internal capability only.
62 * In order to remove a STA info structure, the caller needs to first
63 * unlink it (sta_info_unlink()) from the list and hash tables and
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.
70 * If sta_info_unlink() did not clear the pointer then the caller owns
71 * the STA info structure now and is responsible of destroying it with
72 * a call to sta_info_destroy().
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.
80 /* Caller must hold local->sta_lock */
81 static int sta_info_hash_del(struct ieee80211_local *local,
86 s = local->sta_hash[STA_HASH(sta->sta.addr)];
90 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
95 while (s->hnext && s->hnext != sta)
98 rcu_assign_pointer(s->hnext, sta->hnext);
105 /* protected by RCU */
106 struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
108 struct sta_info *sta;
110 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
112 if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
114 sta = rcu_dereference(sta->hnext);
119 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
122 struct ieee80211_local *local = sdata->local;
123 struct sta_info *sta;
126 list_for_each_entry_rcu(sta, &local->sta_list, list) {
127 if (sdata != sta->sdata)
140 * __sta_info_free - internal STA free helper
142 * @local: pointer to the global information
143 * @sta: STA info to free
145 * This function must undo everything done by sta_info_alloc()
146 * that may happen before sta_info_insert().
148 static void __sta_info_free(struct ieee80211_local *local,
149 struct sta_info *sta)
151 rate_control_free_sta(sta);
152 rate_control_put(sta->rate_ctrl);
154 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
155 printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
156 wiphy_name(local->hw.wiphy), sta->sta.addr);
157 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
162 void sta_info_destroy(struct sta_info *sta)
164 struct ieee80211_local *local;
175 cancel_work_sync(&sta->drv_unblock_wk);
177 rate_control_remove_sta_debugfs(sta);
178 ieee80211_sta_debugfs_remove(sta);
180 #ifdef CONFIG_MAC80211_MESH
181 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
182 mesh_plink_deactivate(sta);
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
191 * ieee80211_key_todo() will synchronize_rcu() so after this
192 * nothing can reference this sta struct any more.
194 ieee80211_key_todo();
196 #ifdef CONFIG_MAC80211_MESH
197 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
198 del_timer_sync(&sta->plink_timer);
201 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
202 local->total_ps_buffered--;
203 dev_kfree_skb_any(skb);
206 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
207 dev_kfree_skb_any(skb);
209 for (i = 0; i < STA_TID_NUM; i++) {
210 struct tid_ampdu_rx *tid_rx;
211 struct tid_ampdu_tx *tid_tx;
213 spin_lock_bh(&sta->lock);
214 tid_rx = sta->ampdu_mlme.tid_rx[i];
215 /* Make sure timer won't free the tid_rx struct, see below */
217 tid_rx->shutdown = true;
219 spin_unlock_bh(&sta->lock);
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.
228 del_timer_sync(&tid_rx->session_timer);
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.
241 tid_tx = sta->ampdu_mlme.tid_tx[i];
243 del_timer_sync(&tid_tx->addba_resp_timer);
245 * STA removed while aggregation session being
246 * started? Bit odd, but purge frames anyway.
248 skb_queue_purge(&tid_tx->pending);
253 __sta_info_free(local, sta);
257 /* Caller must hold local->sta_lock */
258 static void sta_info_hash_add(struct ieee80211_local *local,
259 struct sta_info *sta)
261 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
262 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
265 static void sta_unblock(struct work_struct *wk)
267 struct sta_info *sta;
269 sta = container_of(wk, struct sta_info, drv_unblock_wk);
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);
280 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
283 struct ieee80211_local *local = sdata->local;
284 struct sta_info *sta;
287 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
291 spin_lock_init(&sta->lock);
292 spin_lock_init(&sta->flaglock);
293 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
295 memcpy(sta->sta.addr, addr, ETH_ALEN);
299 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
300 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
302 if (!sta->rate_ctrl_priv) {
303 rate_control_put(sta->rate_ctrl);
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;
314 sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
315 sta->ampdu_mlme.tid_rx[i] = NULL;
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;
321 skb_queue_head_init(&sta->ps_tx_buf);
322 skb_queue_head_init(&sta->tx_filtered);
324 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
325 sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
327 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
328 printk(KERN_DEBUG "%s: Allocated STA %pM\n",
329 wiphy_name(local->hw.wiphy), sta->sta.addr);
330 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
332 #ifdef CONFIG_MAC80211_MESH
333 sta->plink_state = PLINK_LISTEN;
334 init_timer(&sta->plink_timer);
340 int sta_info_insert(struct sta_info *sta)
342 struct ieee80211_local *local = sta->local;
343 struct ieee80211_sub_if_data *sdata = sta->sdata;
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.
352 if (unlikely(!netif_running(sdata->dev))) {
357 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
358 is_multicast_ether_addr(sta->sta.addr))) {
363 spin_lock_irqsave(&local->sta_lock, flags);
364 /* check if STA exists already */
365 if (sta_info_get(local, sta->sta.addr)) {
366 spin_unlock_irqrestore(&local->sta_lock, flags);
370 list_add(&sta->list, &local->sta_list);
371 local->sta_generation++;
373 sta_info_hash_add(local, sta);
376 if (local->ops->sta_notify) {
377 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
378 sdata = container_of(sdata->bss,
379 struct ieee80211_sub_if_data,
382 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
386 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
387 printk(KERN_DEBUG "%s: Inserted STA %pM\n",
388 wiphy_name(local->hw.wiphy), sta->sta.addr);
389 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
391 spin_unlock_irqrestore(&local->sta_lock, flags);
393 #ifdef CONFIG_MAC80211_DEBUGFS
395 * Debugfs entry adding might sleep, so schedule process
396 * context task for adding entry for STAs that do not yet
398 * NOTE: due to auto-freeing semantics this may only be done
399 * if the insertion is successful!
401 schedule_work(&local->sta_debugfs_add);
404 if (ieee80211_vif_is_mesh(&sdata->vif))
405 mesh_accept_plinks_update(sdata);
410 __sta_info_free(local, sta);
414 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
417 * This format has been mandated by the IEEE specifications,
418 * so this line may not be changed to use the __set_bit() format.
420 bss->tim[aid / 8] |= (1 << (aid % 8));
423 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
426 * This format has been mandated by the IEEE specifications,
427 * so this line may not be changed to use the __clear_bit() format.
429 bss->tim[aid / 8] &= ~(1 << (aid % 8));
432 static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
433 struct sta_info *sta)
437 __bss_tim_set(bss, sta->sta.aid);
439 if (sta->local->ops->set_tim) {
440 sta->local->tim_in_locked_section = true;
441 drv_set_tim(sta->local, &sta->sta, true);
442 sta->local->tim_in_locked_section = false;
446 void sta_info_set_tim_bit(struct sta_info *sta)
450 BUG_ON(!sta->sdata->bss);
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);
457 static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
458 struct sta_info *sta)
462 __bss_tim_clear(bss, sta->sta.aid);
464 if (sta->local->ops->set_tim) {
465 sta->local->tim_in_locked_section = true;
466 drv_set_tim(sta->local, &sta->sta, false);
467 sta->local->tim_in_locked_section = false;
471 void sta_info_clear_tim_bit(struct sta_info *sta)
475 BUG_ON(!sta->sdata->bss);
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);
482 static void __sta_info_unlink(struct sta_info **sta)
484 struct ieee80211_local *local = (*sta)->local;
485 struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
487 * pull caller's reference if we're already gone.
489 if (sta_info_hash_del(local, *sta)) {
495 ieee80211_key_free((*sta)->key);
496 WARN_ON((*sta)->key);
499 list_del(&(*sta)->list);
502 if (test_and_clear_sta_flags(*sta,
503 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
506 atomic_dec(&sdata->bss->num_sta_ps);
507 __sta_info_clear_tim_bit(sdata->bss, *sta);
511 local->sta_generation++;
513 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
514 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
516 if (local->ops->sta_notify) {
517 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
518 sdata = container_of(sdata->bss,
519 struct ieee80211_sub_if_data,
522 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
524 sdata = (*sta)->sdata;
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);
534 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
535 printk(KERN_DEBUG "%s: Removed STA %pM\n",
536 wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
537 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
540 * Finally, pull caller's reference if the STA is pinned by the
541 * task that is adding the debugfs entries. In that case, we
542 * leave the STA "to be freed".
544 * The rules are not trivial, but not too complex either:
545 * (1) pin_status is only modified under the sta_lock
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
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)
560 * (4) We set the pin_status to DESTROY here when we
562 * (5) sta_info_debugfs_add_work() will reset the pin_status
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.
567 if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
568 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
574 void sta_info_unlink(struct sta_info **sta)
576 struct ieee80211_local *local = (*sta)->local;
579 spin_lock_irqsave(&local->sta_lock, flags);
580 __sta_info_unlink(sta);
581 spin_unlock_irqrestore(&local->sta_lock, flags);
584 static int sta_info_buffer_expired(struct sta_info *sta,
587 struct ieee80211_tx_info *info;
593 info = IEEE80211_SKB_CB(skb);
595 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
596 timeout = (sta->listen_interval *
597 sta->sdata->vif.bss_conf.beacon_int *
599 if (timeout < STA_TX_BUFFER_EXPIRE)
600 timeout = STA_TX_BUFFER_EXPIRE;
601 return time_after(jiffies, info->control.jiffies + timeout);
605 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
606 struct sta_info *sta)
610 struct ieee80211_sub_if_data *sdata;
612 if (skb_queue_empty(&sta->ps_tx_buf))
616 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
617 skb = skb_peek(&sta->ps_tx_buf);
618 if (sta_info_buffer_expired(sta, skb))
619 skb = __skb_dequeue(&sta->ps_tx_buf);
622 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
628 local->total_ps_buffered--;
629 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
630 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
635 if (skb_queue_empty(&sta->ps_tx_buf))
636 sta_info_clear_tim_bit(sta);
641 static void sta_info_cleanup(unsigned long data)
643 struct ieee80211_local *local = (struct ieee80211_local *) data;
644 struct sta_info *sta;
647 list_for_each_entry_rcu(sta, &local->sta_list, list)
648 sta_info_cleanup_expire_buffered(local, sta);
651 if (local->quiescing)
654 local->sta_cleanup.expires =
655 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
656 add_timer(&local->sta_cleanup);
659 #ifdef CONFIG_MAC80211_DEBUGFS
661 * See comment in __sta_info_unlink,
662 * caller must hold local->sta_lock.
664 static void __sta_info_pin(struct sta_info *sta)
666 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
667 sta->pin_status = STA_INFO_PIN_STAT_PINNED;
671 * See comment in __sta_info_unlink, returns sta if it
672 * needs to be destroyed.
674 static struct sta_info *__sta_info_unpin(struct sta_info *sta)
676 struct sta_info *ret = NULL;
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)
684 sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
685 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
690 static void sta_info_debugfs_add_work(struct work_struct *work)
692 struct ieee80211_local *local =
693 container_of(work, struct ieee80211_local, sta_debugfs_add);
694 struct sta_info *sta, *tmp;
697 /* We need to keep the RTNL across the whole pinned status. */
702 spin_lock_irqsave(&local->sta_lock, flags);
703 list_for_each_entry(tmp, &local->sta_list, list) {
705 * debugfs.add_has_run will be set by
706 * ieee80211_sta_debugfs_add regardless
707 * of what else it does.
709 if (!tmp->debugfs.add_has_run) {
715 spin_unlock_irqrestore(&local->sta_lock, flags);
720 ieee80211_sta_debugfs_add(sta);
721 rate_control_add_sta_debugfs(sta);
723 sta = __sta_info_unpin(sta);
724 sta_info_destroy(sta);
730 void sta_info_init(struct ieee80211_local *local)
732 spin_lock_init(&local->sta_lock);
733 INIT_LIST_HEAD(&local->sta_list);
735 setup_timer(&local->sta_cleanup, sta_info_cleanup,
736 (unsigned long)local);
737 local->sta_cleanup.expires =
738 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
740 #ifdef CONFIG_MAC80211_DEBUGFS
741 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
745 int sta_info_start(struct ieee80211_local *local)
747 add_timer(&local->sta_cleanup);
751 void sta_info_stop(struct ieee80211_local *local)
753 del_timer(&local->sta_cleanup);
754 #ifdef CONFIG_MAC80211_DEBUGFS
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
761 cancel_work_sync(&local->sta_debugfs_add);
764 sta_info_flush(local, NULL);
768 * sta_info_flush - flush matching STA entries from the STA table
770 * Returns the number of removed STA entries.
772 * @local: local interface data
773 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
775 int sta_info_flush(struct ieee80211_local *local,
776 struct ieee80211_sub_if_data *sdata)
778 struct sta_info *sta, *tmp;
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);
790 list_add_tail(&sta->list, &tmp_list);
795 spin_unlock_irqrestore(&local->sta_lock, flags);
797 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
798 sta_info_destroy(sta);
803 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
804 unsigned long exp_time)
806 struct ieee80211_local *local = sdata->local;
807 struct sta_info *sta, *tmp;
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
815 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
816 sdata->dev->name, sta->sta.addr);
818 __sta_info_unlink(&sta);
820 list_add(&sta->list, &tmp_list);
822 spin_unlock_irqrestore(&local->sta_lock, flags);
824 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
825 sta_info_destroy(sta);
828 struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
831 struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
837 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
839 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
842 struct ieee80211_sub_if_data *sdata;
847 sdata = vif_to_sdata(vif);
849 return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
851 EXPORT_SYMBOL(ieee80211_find_sta);
853 /* powersave support code */
854 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
856 struct ieee80211_sub_if_data *sdata = sta->sdata;
857 struct ieee80211_local *local = sdata->local;
860 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_AWAKE, &sta->sta);
862 if (!skb_queue_empty(&sta->ps_tx_buf))
863 sta_info_clear_tim_bit(sta);
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);
869 local->total_ps_buffered -= buffered;
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 */
878 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
880 struct ieee80211_sub_if_data *sdata = sta->sdata;
881 struct ieee80211_local *local = sdata->local;
885 skb = skb_dequeue(&sta->tx_filtered);
887 skb = skb_dequeue(&sta->ps_tx_buf);
889 local->total_ps_buffered--;
891 no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
892 skb_queue_empty(&sta->ps_tx_buf);
895 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
896 struct ieee80211_hdr *hdr =
897 (struct ieee80211_hdr *) skb->data;
900 * Tell TX path to send this frame even though the STA may
901 * still remain is PS mode after this frame exchange.
903 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
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 */
911 /* Use MoreData flag to indicate whether there are more
912 * buffered frames for this STA */
914 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
916 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
918 ieee80211_add_pending_skb(local, skb);
921 sta_info_clear_tim_bit(sta);
922 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
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?
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 */
937 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
938 struct ieee80211_sta *pubsta, bool block)
940 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
943 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
945 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
947 EXPORT_SYMBOL(ieee80211_sta_block_awake);