mac80211: implement uAPSD
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / sta_info.c
blobf9079e478f776f229a2dd9de7e93477fb6a841d7
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 association 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 immediately 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 = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
71 lockdep_is_held(&local->sta_lock));
72 if (!s)
73 return -ENOENT;
74 if (s == sta) {
75 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
76 s->hnext);
77 return 0;
80 while (rcu_access_pointer(s->hnext) &&
81 rcu_access_pointer(s->hnext) != sta)
82 s = rcu_dereference_protected(s->hnext,
83 lockdep_is_held(&local->sta_lock));
84 if (rcu_access_pointer(s->hnext)) {
85 rcu_assign_pointer(s->hnext, sta->hnext);
86 return 0;
89 return -ENOENT;
92 /* protected by RCU */
93 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
94 const u8 *addr)
96 struct ieee80211_local *local = sdata->local;
97 struct sta_info *sta;
99 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
100 lockdep_is_held(&local->sta_lock) ||
101 lockdep_is_held(&local->sta_mtx));
102 while (sta) {
103 if (sta->sdata == sdata && !sta->dummy &&
104 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
105 break;
106 sta = rcu_dereference_check(sta->hnext,
107 lockdep_is_held(&local->sta_lock) ||
108 lockdep_is_held(&local->sta_mtx));
110 return sta;
113 /* get a station info entry even if it is a dummy station*/
114 struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
115 const u8 *addr)
117 struct ieee80211_local *local = sdata->local;
118 struct sta_info *sta;
120 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
121 lockdep_is_held(&local->sta_lock) ||
122 lockdep_is_held(&local->sta_mtx));
123 while (sta) {
124 if (sta->sdata == sdata &&
125 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
126 break;
127 sta = rcu_dereference_check(sta->hnext,
128 lockdep_is_held(&local->sta_lock) ||
129 lockdep_is_held(&local->sta_mtx));
131 return sta;
135 * Get sta info either from the specified interface
136 * or from one of its vlans
138 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
139 const u8 *addr)
141 struct ieee80211_local *local = sdata->local;
142 struct sta_info *sta;
144 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
145 lockdep_is_held(&local->sta_lock) ||
146 lockdep_is_held(&local->sta_mtx));
147 while (sta) {
148 if ((sta->sdata == sdata ||
149 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
150 !sta->dummy &&
151 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
152 break;
153 sta = rcu_dereference_check(sta->hnext,
154 lockdep_is_held(&local->sta_lock) ||
155 lockdep_is_held(&local->sta_mtx));
157 return sta;
161 * Get sta info either from the specified interface
162 * or from one of its vlans (including dummy stations)
164 struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
165 const u8 *addr)
167 struct ieee80211_local *local = sdata->local;
168 struct sta_info *sta;
170 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
171 lockdep_is_held(&local->sta_lock) ||
172 lockdep_is_held(&local->sta_mtx));
173 while (sta) {
174 if ((sta->sdata == sdata ||
175 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
176 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
177 break;
178 sta = rcu_dereference_check(sta->hnext,
179 lockdep_is_held(&local->sta_lock) ||
180 lockdep_is_held(&local->sta_mtx));
182 return sta;
185 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
186 int idx)
188 struct ieee80211_local *local = sdata->local;
189 struct sta_info *sta;
190 int i = 0;
192 list_for_each_entry_rcu(sta, &local->sta_list, list) {
193 if (sdata != sta->sdata)
194 continue;
195 if (i < idx) {
196 ++i;
197 continue;
199 return sta;
202 return NULL;
206 * __sta_info_free - internal STA free helper
208 * @local: pointer to the global information
209 * @sta: STA info to free
211 * This function must undo everything done by sta_info_alloc()
212 * that may happen before sta_info_insert().
214 static void __sta_info_free(struct ieee80211_local *local,
215 struct sta_info *sta)
217 if (sta->rate_ctrl) {
218 rate_control_free_sta(sta);
219 rate_control_put(sta->rate_ctrl);
222 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
223 wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
224 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
226 kfree(sta);
229 /* Caller must hold local->sta_lock */
230 static void sta_info_hash_add(struct ieee80211_local *local,
231 struct sta_info *sta)
233 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
234 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
237 static void sta_unblock(struct work_struct *wk)
239 struct sta_info *sta;
241 sta = container_of(wk, struct sta_info, drv_unblock_wk);
243 if (sta->dead)
244 return;
246 if (!test_sta_flags(sta, WLAN_STA_PS_STA))
247 ieee80211_sta_ps_deliver_wakeup(sta);
248 else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) {
249 clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
250 ieee80211_sta_ps_deliver_poll_response(sta);
251 } else if (test_and_clear_sta_flags(sta, WLAN_STA_UAPSD)) {
252 clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
253 ieee80211_sta_ps_deliver_uapsd(sta);
254 } else
255 clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
258 static int sta_prepare_rate_control(struct ieee80211_local *local,
259 struct sta_info *sta, gfp_t gfp)
261 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
262 return 0;
264 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
265 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
266 &sta->sta, gfp);
267 if (!sta->rate_ctrl_priv) {
268 rate_control_put(sta->rate_ctrl);
269 return -ENOMEM;
272 return 0;
275 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
276 u8 *addr, gfp_t gfp)
278 struct ieee80211_local *local = sdata->local;
279 struct sta_info *sta;
280 struct timespec uptime;
281 int i;
283 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
284 if (!sta)
285 return NULL;
287 spin_lock_init(&sta->lock);
288 spin_lock_init(&sta->flaglock);
289 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
290 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
291 mutex_init(&sta->ampdu_mlme.mtx);
293 memcpy(sta->sta.addr, addr, ETH_ALEN);
294 sta->local = local;
295 sta->sdata = sdata;
296 sta->last_rx = jiffies;
298 do_posix_clock_monotonic_gettime(&uptime);
299 sta->last_connected = uptime.tv_sec;
300 ewma_init(&sta->avg_signal, 1024, 8);
302 if (sta_prepare_rate_control(local, sta, gfp)) {
303 kfree(sta);
304 return NULL;
307 for (i = 0; i < STA_TID_NUM; i++) {
309 * timer_to_tid must be initialized with identity mapping
310 * to enable session_timer's data differentiation. See
311 * sta_rx_agg_session_timer_expired for usage.
313 sta->timer_to_tid[i] = i;
315 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
316 skb_queue_head_init(&sta->ps_tx_buf[i]);
317 skb_queue_head_init(&sta->tx_filtered[i]);
320 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
321 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
323 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
324 wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
325 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
327 #ifdef CONFIG_MAC80211_MESH
328 sta->plink_state = NL80211_PLINK_LISTEN;
329 init_timer(&sta->plink_timer);
330 #endif
332 return sta;
335 static int sta_info_finish_insert(struct sta_info *sta,
336 bool async, bool dummy_reinsert)
338 struct ieee80211_local *local = sta->local;
339 struct ieee80211_sub_if_data *sdata = sta->sdata;
340 struct station_info sinfo;
341 unsigned long flags;
342 int err = 0;
344 lockdep_assert_held(&local->sta_mtx);
346 if (!sta->dummy || dummy_reinsert) {
347 /* notify driver */
348 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
349 sdata = container_of(sdata->bss,
350 struct ieee80211_sub_if_data,
351 u.ap);
352 err = drv_sta_add(local, sdata, &sta->sta);
353 if (err) {
354 if (!async)
355 return err;
356 printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
357 "driver (%d) - keeping it anyway.\n",
358 sdata->name, sta->sta.addr, err);
359 } else {
360 sta->uploaded = true;
361 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
362 if (async)
363 wiphy_debug(local->hw.wiphy,
364 "Finished adding IBSS STA %pM\n",
365 sta->sta.addr);
366 #endif
369 sdata = sta->sdata;
372 if (!dummy_reinsert) {
373 if (!async) {
374 local->num_sta++;
375 local->sta_generation++;
376 smp_mb();
378 /* make the station visible */
379 spin_lock_irqsave(&local->sta_lock, flags);
380 sta_info_hash_add(local, sta);
381 spin_unlock_irqrestore(&local->sta_lock, flags);
384 list_add(&sta->list, &local->sta_list);
385 } else {
386 sta->dummy = false;
389 if (!sta->dummy) {
390 ieee80211_sta_debugfs_add(sta);
391 rate_control_add_sta_debugfs(sta);
393 memset(&sinfo, 0, sizeof(sinfo));
394 sinfo.filled = 0;
395 sinfo.generation = local->sta_generation;
396 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
399 return 0;
402 static void sta_info_finish_pending(struct ieee80211_local *local)
404 struct sta_info *sta;
405 unsigned long flags;
407 spin_lock_irqsave(&local->sta_lock, flags);
408 while (!list_empty(&local->sta_pending_list)) {
409 sta = list_first_entry(&local->sta_pending_list,
410 struct sta_info, list);
411 list_del(&sta->list);
412 spin_unlock_irqrestore(&local->sta_lock, flags);
414 sta_info_finish_insert(sta, true, false);
416 spin_lock_irqsave(&local->sta_lock, flags);
418 spin_unlock_irqrestore(&local->sta_lock, flags);
421 static void sta_info_finish_work(struct work_struct *work)
423 struct ieee80211_local *local =
424 container_of(work, struct ieee80211_local, sta_finish_work);
426 mutex_lock(&local->sta_mtx);
427 sta_info_finish_pending(local);
428 mutex_unlock(&local->sta_mtx);
431 static int sta_info_insert_check(struct sta_info *sta)
433 struct ieee80211_sub_if_data *sdata = sta->sdata;
436 * Can't be a WARN_ON because it can be triggered through a race:
437 * something inserts a STA (on one CPU) without holding the RTNL
438 * and another CPU turns off the net device.
440 if (unlikely(!ieee80211_sdata_running(sdata)))
441 return -ENETDOWN;
443 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
444 is_multicast_ether_addr(sta->sta.addr)))
445 return -EINVAL;
447 return 0;
450 static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU)
452 struct ieee80211_local *local = sta->local;
453 struct ieee80211_sub_if_data *sdata = sta->sdata;
454 unsigned long flags;
456 spin_lock_irqsave(&local->sta_lock, flags);
457 /* check if STA exists already */
458 if (sta_info_get_bss_rx(sdata, sta->sta.addr)) {
459 spin_unlock_irqrestore(&local->sta_lock, flags);
460 rcu_read_lock();
461 return -EEXIST;
464 local->num_sta++;
465 local->sta_generation++;
466 smp_mb();
467 sta_info_hash_add(local, sta);
469 list_add_tail(&sta->list, &local->sta_pending_list);
471 rcu_read_lock();
472 spin_unlock_irqrestore(&local->sta_lock, flags);
474 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
475 wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
476 sta->sta.addr);
477 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
479 ieee80211_queue_work(&local->hw, &local->sta_finish_work);
481 return 0;
485 * should be called with sta_mtx locked
486 * this function replaces the mutex lock
487 * with a RCU lock
489 static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU)
491 struct ieee80211_local *local = sta->local;
492 struct ieee80211_sub_if_data *sdata = sta->sdata;
493 unsigned long flags;
494 struct sta_info *exist_sta;
495 bool dummy_reinsert = false;
496 int err = 0;
498 lockdep_assert_held(&local->sta_mtx);
501 * On first glance, this will look racy, because the code
502 * in this function, which inserts a station with sleeping,
503 * unlocks the sta_lock between checking existence in the
504 * hash table and inserting into it.
506 * However, it is not racy against itself because it keeps
507 * the mutex locked.
510 spin_lock_irqsave(&local->sta_lock, flags);
512 * check if STA exists already.
513 * only accept a scenario of a second call to sta_info_insert_non_ibss
514 * with a dummy station entry that was inserted earlier
515 * in that case - assume that the dummy station flag should
516 * be removed.
518 exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
519 if (exist_sta) {
520 if (exist_sta == sta && sta->dummy) {
521 dummy_reinsert = true;
522 } else {
523 spin_unlock_irqrestore(&local->sta_lock, flags);
524 mutex_unlock(&local->sta_mtx);
525 rcu_read_lock();
526 return -EEXIST;
530 spin_unlock_irqrestore(&local->sta_lock, flags);
532 err = sta_info_finish_insert(sta, false, dummy_reinsert);
533 if (err) {
534 mutex_unlock(&local->sta_mtx);
535 rcu_read_lock();
536 return err;
539 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
540 wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
541 sta->dummy ? "dummy " : "", sta->sta.addr);
542 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
544 /* move reference to rcu-protected */
545 rcu_read_lock();
546 mutex_unlock(&local->sta_mtx);
548 if (ieee80211_vif_is_mesh(&sdata->vif))
549 mesh_accept_plinks_update(sdata);
551 return 0;
554 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
556 struct ieee80211_local *local = sta->local;
557 struct ieee80211_sub_if_data *sdata = sta->sdata;
558 int err = 0;
560 err = sta_info_insert_check(sta);
561 if (err) {
562 rcu_read_lock();
563 goto out_free;
567 * In ad-hoc mode, we sometimes need to insert stations
568 * from tasklet context from the RX path. To avoid races,
569 * always do so in that case -- see the comment below.
571 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
572 err = sta_info_insert_ibss(sta);
573 if (err)
574 goto out_free;
576 return 0;
580 * It might seem that the function called below is in race against
581 * the function call above that atomically inserts the station... That,
582 * however, is not true because the above code can only
583 * be invoked for IBSS interfaces, and the below code will
584 * not be -- and the two do not race against each other as
585 * the hash table also keys off the interface.
588 might_sleep();
590 mutex_lock(&local->sta_mtx);
592 err = sta_info_insert_non_ibss(sta);
593 if (err)
594 goto out_free;
596 return 0;
597 out_free:
598 BUG_ON(!err);
599 __sta_info_free(local, sta);
600 return err;
603 int sta_info_insert(struct sta_info *sta)
605 int err = sta_info_insert_rcu(sta);
607 rcu_read_unlock();
609 return err;
612 /* Caller must hold sta->local->sta_mtx */
613 int sta_info_reinsert(struct sta_info *sta)
615 struct ieee80211_local *local = sta->local;
616 int err = 0;
618 err = sta_info_insert_check(sta);
619 if (err) {
620 mutex_unlock(&local->sta_mtx);
621 return err;
624 might_sleep();
626 err = sta_info_insert_non_ibss(sta);
627 rcu_read_unlock();
628 return err;
631 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
634 * This format has been mandated by the IEEE specifications,
635 * so this line may not be changed to use the __set_bit() format.
637 bss->tim[aid / 8] |= (1 << (aid % 8));
640 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
643 * This format has been mandated by the IEEE specifications,
644 * so this line may not be changed to use the __clear_bit() format.
646 bss->tim[aid / 8] &= ~(1 << (aid % 8));
649 static unsigned long ieee80211_tids_for_ac(int ac)
651 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
652 switch (ac) {
653 case IEEE80211_AC_VO:
654 return BIT(6) | BIT(7);
655 case IEEE80211_AC_VI:
656 return BIT(4) | BIT(5);
657 case IEEE80211_AC_BE:
658 return BIT(0) | BIT(3);
659 case IEEE80211_AC_BK:
660 return BIT(1) | BIT(2);
661 default:
662 WARN_ON(1);
663 return 0;
667 void sta_info_recalc_tim(struct sta_info *sta)
669 struct ieee80211_local *local = sta->local;
670 struct ieee80211_if_ap *bss = sta->sdata->bss;
671 unsigned long flags;
672 bool indicate_tim = false;
673 u8 ignore_for_tim = sta->sta.uapsd_queues;
674 int ac;
676 if (WARN_ON_ONCE(!sta->sdata->bss))
677 return;
679 /* No need to do anything if the driver does all */
680 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
681 return;
683 if (sta->dead)
684 goto done;
687 * If all ACs are delivery-enabled then we should build
688 * the TIM bit for all ACs anyway; if only some are then
689 * we ignore those and build the TIM bit using only the
690 * non-enabled ones.
692 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
693 ignore_for_tim = 0;
695 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
696 unsigned long tids;
698 if (ignore_for_tim & BIT(ac))
699 continue;
701 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
702 !skb_queue_empty(&sta->ps_tx_buf[ac]);
703 if (indicate_tim)
704 break;
706 tids = ieee80211_tids_for_ac(ac);
708 indicate_tim |=
709 sta->driver_buffered_tids & tids;
712 done:
713 spin_lock_irqsave(&local->sta_lock, flags);
715 if (indicate_tim)
716 __bss_tim_set(bss, sta->sta.aid);
717 else
718 __bss_tim_clear(bss, sta->sta.aid);
720 if (local->ops->set_tim) {
721 local->tim_in_locked_section = true;
722 drv_set_tim(local, &sta->sta, indicate_tim);
723 local->tim_in_locked_section = false;
726 spin_unlock_irqrestore(&local->sta_lock, flags);
729 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
731 struct ieee80211_tx_info *info;
732 int timeout;
734 if (!skb)
735 return false;
737 info = IEEE80211_SKB_CB(skb);
739 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
740 timeout = (sta->listen_interval *
741 sta->sdata->vif.bss_conf.beacon_int *
742 32 / 15625) * HZ;
743 if (timeout < STA_TX_BUFFER_EXPIRE)
744 timeout = STA_TX_BUFFER_EXPIRE;
745 return time_after(jiffies, info->control.jiffies + timeout);
749 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
750 struct sta_info *sta, int ac)
752 unsigned long flags;
753 struct sk_buff *skb;
756 * First check for frames that should expire on the filtered
757 * queue. Frames here were rejected by the driver and are on
758 * a separate queue to avoid reordering with normal PS-buffered
759 * frames. They also aren't accounted for right now in the
760 * total_ps_buffered counter.
762 for (;;) {
763 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
764 skb = skb_peek(&sta->tx_filtered[ac]);
765 if (sta_info_buffer_expired(sta, skb))
766 skb = __skb_dequeue(&sta->tx_filtered[ac]);
767 else
768 skb = NULL;
769 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
772 * Frames are queued in order, so if this one
773 * hasn't expired yet we can stop testing. If
774 * we actually reached the end of the queue we
775 * also need to stop, of course.
777 if (!skb)
778 break;
779 dev_kfree_skb(skb);
783 * Now also check the normal PS-buffered queue, this will
784 * only find something if the filtered queue was emptied
785 * since the filtered frames are all before the normal PS
786 * buffered frames.
788 for (;;) {
789 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
790 skb = skb_peek(&sta->ps_tx_buf[ac]);
791 if (sta_info_buffer_expired(sta, skb))
792 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
793 else
794 skb = NULL;
795 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
798 * frames are queued in order, so if this one
799 * hasn't expired yet (or we reached the end of
800 * the queue) we can stop testing
802 if (!skb)
803 break;
805 local->total_ps_buffered--;
806 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
807 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
808 sta->sta.addr);
809 #endif
810 dev_kfree_skb(skb);
814 * Finally, recalculate the TIM bit for this station -- it might
815 * now be clear because the station was too slow to retrieve its
816 * frames.
818 sta_info_recalc_tim(sta);
821 * Return whether there are any frames still buffered, this is
822 * used to check whether the cleanup timer still needs to run,
823 * if there are no frames we don't need to rearm the timer.
825 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
826 skb_queue_empty(&sta->tx_filtered[ac]));
829 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
830 struct sta_info *sta)
832 bool have_buffered = false;
833 int ac;
835 /* This is only necessary for stations on BSS interfaces */
836 if (!sta->sdata->bss)
837 return false;
839 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
840 have_buffered |=
841 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
843 return have_buffered;
846 static int __must_check __sta_info_destroy(struct sta_info *sta)
848 struct ieee80211_local *local;
849 struct ieee80211_sub_if_data *sdata;
850 unsigned long flags;
851 int ret, i, ac;
853 might_sleep();
855 if (!sta)
856 return -ENOENT;
858 local = sta->local;
859 sdata = sta->sdata;
862 * Before removing the station from the driver and
863 * rate control, it might still start new aggregation
864 * sessions -- block that to make sure the tear-down
865 * will be sufficient.
867 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
868 ieee80211_sta_tear_down_BA_sessions(sta, true);
870 spin_lock_irqsave(&local->sta_lock, flags);
871 ret = sta_info_hash_del(local, sta);
872 /* this might still be the pending list ... which is fine */
873 if (!ret)
874 list_del(&sta->list);
875 spin_unlock_irqrestore(&local->sta_lock, flags);
876 if (ret)
877 return ret;
879 mutex_lock(&local->key_mtx);
880 for (i = 0; i < NUM_DEFAULT_KEYS; i++)
881 __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
882 if (sta->ptk)
883 __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
884 mutex_unlock(&local->key_mtx);
886 sta->dead = true;
888 if (test_and_clear_sta_flags(sta,
889 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
890 BUG_ON(!sdata->bss);
892 atomic_dec(&sdata->bss->num_sta_ps);
893 sta_info_recalc_tim(sta);
896 local->num_sta--;
897 local->sta_generation++;
899 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
900 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
902 if (sta->uploaded) {
903 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
904 sdata = container_of(sdata->bss,
905 struct ieee80211_sub_if_data,
906 u.ap);
907 drv_sta_remove(local, sdata, &sta->sta);
908 sdata = sta->sdata;
912 * At this point, after we wait for an RCU grace period,
913 * neither mac80211 nor the driver can reference this
914 * sta struct any more except by still existing timers
915 * associated with this station that we clean up below.
917 synchronize_rcu();
919 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
920 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
921 __skb_queue_purge(&sta->ps_tx_buf[ac]);
922 __skb_queue_purge(&sta->tx_filtered[ac]);
925 #ifdef CONFIG_MAC80211_MESH
926 if (ieee80211_vif_is_mesh(&sdata->vif))
927 mesh_accept_plinks_update(sdata);
928 #endif
930 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
931 wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
932 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
933 cancel_work_sync(&sta->drv_unblock_wk);
935 cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
937 rate_control_remove_sta_debugfs(sta);
938 ieee80211_sta_debugfs_remove(sta);
940 #ifdef CONFIG_MAC80211_MESH
941 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
942 mesh_plink_deactivate(sta);
943 del_timer_sync(&sta->plink_timer);
945 #endif
947 __sta_info_free(local, sta);
949 return 0;
952 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
954 struct sta_info *sta;
955 int ret;
957 mutex_lock(&sdata->local->sta_mtx);
958 sta = sta_info_get_rx(sdata, addr);
959 ret = __sta_info_destroy(sta);
960 mutex_unlock(&sdata->local->sta_mtx);
962 return ret;
965 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
966 const u8 *addr)
968 struct sta_info *sta;
969 int ret;
971 mutex_lock(&sdata->local->sta_mtx);
972 sta = sta_info_get_bss_rx(sdata, addr);
973 ret = __sta_info_destroy(sta);
974 mutex_unlock(&sdata->local->sta_mtx);
976 return ret;
979 static void sta_info_cleanup(unsigned long data)
981 struct ieee80211_local *local = (struct ieee80211_local *) data;
982 struct sta_info *sta;
983 bool timer_needed = false;
985 rcu_read_lock();
986 list_for_each_entry_rcu(sta, &local->sta_list, list)
987 if (sta_info_cleanup_expire_buffered(local, sta))
988 timer_needed = true;
989 rcu_read_unlock();
991 if (local->quiescing)
992 return;
994 if (!timer_needed)
995 return;
997 mod_timer(&local->sta_cleanup,
998 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1001 void sta_info_init(struct ieee80211_local *local)
1003 spin_lock_init(&local->sta_lock);
1004 mutex_init(&local->sta_mtx);
1005 INIT_LIST_HEAD(&local->sta_list);
1006 INIT_LIST_HEAD(&local->sta_pending_list);
1007 INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
1009 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1010 (unsigned long)local);
1013 void sta_info_stop(struct ieee80211_local *local)
1015 del_timer(&local->sta_cleanup);
1016 sta_info_flush(local, NULL);
1020 * sta_info_flush - flush matching STA entries from the STA table
1022 * Returns the number of removed STA entries.
1024 * @local: local interface data
1025 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
1027 int sta_info_flush(struct ieee80211_local *local,
1028 struct ieee80211_sub_if_data *sdata)
1030 struct sta_info *sta, *tmp;
1031 int ret = 0;
1033 might_sleep();
1035 mutex_lock(&local->sta_mtx);
1037 sta_info_finish_pending(local);
1039 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1040 if (!sdata || sdata == sta->sdata)
1041 WARN_ON(__sta_info_destroy(sta));
1043 mutex_unlock(&local->sta_mtx);
1045 return ret;
1048 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1049 unsigned long exp_time)
1051 struct ieee80211_local *local = sdata->local;
1052 struct sta_info *sta, *tmp;
1054 mutex_lock(&local->sta_mtx);
1055 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1056 if (time_after(jiffies, sta->last_rx + exp_time)) {
1057 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1058 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
1059 sdata->name, sta->sta.addr);
1060 #endif
1061 WARN_ON(__sta_info_destroy(sta));
1063 mutex_unlock(&local->sta_mtx);
1066 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1067 const u8 *addr,
1068 const u8 *localaddr)
1070 struct sta_info *sta, *nxt;
1073 * Just return a random station if localaddr is NULL
1074 * ... first in list.
1076 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1077 if (localaddr &&
1078 compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
1079 continue;
1080 if (!sta->uploaded)
1081 return NULL;
1082 return &sta->sta;
1085 return NULL;
1087 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1089 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1090 const u8 *addr)
1092 struct sta_info *sta;
1094 if (!vif)
1095 return NULL;
1097 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1098 if (!sta)
1099 return NULL;
1101 if (!sta->uploaded)
1102 return NULL;
1104 return &sta->sta;
1106 EXPORT_SYMBOL(ieee80211_find_sta);
1108 static void clear_sta_ps_flags(void *_sta)
1110 struct sta_info *sta = _sta;
1112 clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA);
1115 /* powersave support code */
1116 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1118 struct ieee80211_sub_if_data *sdata = sta->sdata;
1119 struct ieee80211_local *local = sdata->local;
1120 struct sk_buff_head pending;
1121 int filtered = 0, buffered = 0, ac;
1123 clear_sta_flags(sta, WLAN_STA_SP);
1125 BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
1126 sta->driver_buffered_tids = 0;
1128 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1129 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1131 skb_queue_head_init(&pending);
1133 /* Send all buffered frames to the station */
1134 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1135 int count = skb_queue_len(&pending), tmp;
1137 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1138 tmp = skb_queue_len(&pending);
1139 filtered += tmp - count;
1140 count = tmp;
1142 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1143 tmp = skb_queue_len(&pending);
1144 buffered += tmp - count;
1147 ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1149 local->total_ps_buffered -= buffered;
1151 sta_info_recalc_tim(sta);
1153 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1154 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
1155 "since STA not sleeping anymore\n", sdata->name,
1156 sta->sta.addr, sta->sta.aid, filtered, buffered);
1157 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1160 static void
1161 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1162 int n_frames, u8 ignored_acs,
1163 enum ieee80211_frame_release_type reason)
1165 struct ieee80211_sub_if_data *sdata = sta->sdata;
1166 struct ieee80211_local *local = sdata->local;
1167 bool found = false;
1168 bool more_data = false;
1169 int ac;
1170 unsigned long driver_release_tids = 0;
1171 struct sk_buff_head frames;
1173 __skb_queue_head_init(&frames);
1176 * Get response frame(s) and more data bit for it.
1178 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1179 unsigned long tids;
1181 if (ignored_acs & BIT(ac))
1182 continue;
1184 tids = ieee80211_tids_for_ac(ac);
1186 if (!found) {
1187 driver_release_tids = sta->driver_buffered_tids & tids;
1188 if (driver_release_tids) {
1189 found = true;
1190 } else {
1191 struct sk_buff *skb;
1193 while (n_frames > 0) {
1194 skb = skb_dequeue(&sta->tx_filtered[ac]);
1195 if (!skb) {
1196 skb = skb_dequeue(
1197 &sta->ps_tx_buf[ac]);
1198 if (skb)
1199 local->total_ps_buffered--;
1201 if (!skb)
1202 break;
1203 n_frames--;
1204 found = true;
1205 __skb_queue_tail(&frames, skb);
1210 * If the driver has data on more than one TID then
1211 * certainly there's more data if we release just a
1212 * single frame now (from a single TID).
1214 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1215 hweight16(driver_release_tids) > 1) {
1216 more_data = true;
1217 driver_release_tids =
1218 BIT(ffs(driver_release_tids) - 1);
1219 break;
1223 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1224 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1225 more_data = true;
1226 break;
1230 if (!found) {
1231 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1233 * FIXME: This can be the result of a race condition between
1234 * us expiring a frame and the station polling for it.
1235 * Should we send it a null-func frame indicating we
1236 * have nothing buffered for it?
1238 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1239 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
1240 "though there are no buffered frames for it\n",
1241 sdata->name, sta->sta.addr);
1242 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1244 return;
1247 if (!driver_release_tids) {
1248 struct sk_buff_head pending;
1249 struct sk_buff *skb;
1251 skb_queue_head_init(&pending);
1253 while ((skb = __skb_dequeue(&frames))) {
1254 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1255 struct ieee80211_hdr *hdr = (void *) skb->data;
1258 * Tell TX path to send this frame even though the
1259 * STA may still remain is PS mode after this frame
1260 * exchange.
1262 info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
1265 * Use MoreData flag to indicate whether there are
1266 * more buffered frames for this STA
1268 if (!more_data)
1269 hdr->frame_control &=
1270 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1271 else
1272 hdr->frame_control |=
1273 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1275 if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
1276 skb_queue_empty(&frames)) {
1277 /* set EOSP for the frame */
1278 u8 *p = ieee80211_get_qos_ctl(hdr);
1279 *p |= IEEE80211_QOS_CTL_EOSP;
1280 info->flags |= IEEE80211_TX_STATUS_EOSP |
1281 IEEE80211_TX_CTL_REQ_TX_STATUS;
1284 __skb_queue_tail(&pending, skb);
1287 ieee80211_add_pending_skbs(local, &pending);
1289 sta_info_recalc_tim(sta);
1290 } else {
1292 * We need to release a frame that is buffered somewhere in the
1293 * driver ... it'll have to handle that.
1294 * Note that, as per the comment above, it'll also have to see
1295 * if there is more than just one frame on the specific TID that
1296 * we're releasing from, and it needs to set the more-data bit
1297 * accordingly if we tell it that there's no more data. If we do
1298 * tell it there's more data, then of course the more-data bit
1299 * needs to be set anyway.
1301 drv_release_buffered_frames(local, sta, driver_release_tids,
1302 n_frames, reason, more_data);
1305 * Note that we don't recalculate the TIM bit here as it would
1306 * most likely have no effect at all unless the driver told us
1307 * that the TID became empty before returning here from the
1308 * release function.
1309 * Either way, however, when the driver tells us that the TID
1310 * became empty we'll do the TIM recalculation.
1315 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1317 u8 ignore_for_response = sta->sta.uapsd_queues;
1320 * If all ACs are delivery-enabled then we should reply
1321 * from any of them, if only some are enabled we reply
1322 * only from the non-enabled ones.
1324 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1325 ignore_for_response = 0;
1327 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1328 IEEE80211_FRAME_RELEASE_PSPOLL);
1331 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1333 int n_frames = sta->sta.max_sp;
1334 u8 delivery_enabled = sta->sta.uapsd_queues;
1337 * If we ever grow support for TSPEC this might happen if
1338 * the TSPEC update from hostapd comes in between a trigger
1339 * frame setting WLAN_STA_UAPSD in the RX path and this
1340 * actually getting called.
1342 if (!delivery_enabled)
1343 return;
1345 /* Ohh, finally, the service period starts :-) */
1346 set_sta_flags(sta, WLAN_STA_SP);
1348 switch (sta->sta.max_sp) {
1349 case 1:
1350 n_frames = 2;
1351 break;
1352 case 2:
1353 n_frames = 4;
1354 break;
1355 case 3:
1356 n_frames = 6;
1357 break;
1358 case 0:
1359 /* XXX: what is a good value? */
1360 n_frames = 8;
1361 break;
1364 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1365 IEEE80211_FRAME_RELEASE_UAPSD);
1368 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1369 struct ieee80211_sta *pubsta, bool block)
1371 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1373 trace_api_sta_block_awake(sta->local, pubsta, block);
1375 if (block)
1376 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
1377 else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER))
1378 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1380 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1382 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1383 u8 tid, bool buffered)
1385 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1387 if (WARN_ON(tid >= STA_TID_NUM))
1388 return;
1390 if (buffered)
1391 set_bit(tid, &sta->driver_buffered_tids);
1392 else
1393 clear_bit(tid, &sta->driver_buffered_tids);
1395 sta_info_recalc_tim(sta);
1397 EXPORT_SYMBOL(ieee80211_sta_set_buffered);