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 "ieee80211_rate.h"
24 #include "debugfs_sta.h"
28 * DOC: STA information lifetime rules
30 * STA info structures (&struct sta_info) are managed in a hash table
31 * for faster lookup and a list for iteration. They are managed using
32 * RCU, i.e. access to the list and hash table is protected by RCU.
34 * STA info structures are always "alive" when they are added with
35 * @sta_info_add() [this may be changed in the future to allow allocating
36 * outside of a critical section!], they are then added to the hash
37 * table and list. Therefore, @sta_info_add() must also be RCU protected,
38 * also, the caller of @sta_info_add() cannot assume that it owns the
41 * Because there are debugfs entries for each station, and adding those
42 * must be able to sleep, it is also possible to "pin" a station entry,
43 * that means it can be removed from the hash table but not be freed.
44 * See the comment in @__sta_info_unlink() for more information.
46 * In order to remove a STA info structure, the caller needs to first
47 * unlink it (@sta_info_unlink()) from the list and hash tables and
48 * then wait for an RCU synchronisation before it can be freed. Due to
49 * the pinning and the possibility of multiple callers trying to remove
50 * the same STA info at the same time, @sta_info_unlink() can clear the
51 * STA info pointer it is passed to indicate that the STA info is owned
52 * by somebody else now.
54 * If @sta_info_unlink() did not clear the pointer then the caller owns
55 * the STA info structure now and is responsible of destroying it with
56 * a call to @sta_info_destroy(), not before RCU synchronisation, of
57 * course. Note that sta_info_destroy() must be protected by the RTNL.
59 * In all other cases, there is no concept of ownership on a STA entry,
60 * each structure is owned by the global hash table/list until it is
61 * removed. All users of the structure need to be RCU protected so that
62 * the structure won't be freed before they are done using it.
65 /* Caller must hold local->sta_lock */
66 static int sta_info_hash_del(struct ieee80211_local *local,
71 s = local->sta_hash[STA_HASH(sta->addr)];
75 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)],
80 while (s->hnext && s->hnext != sta)
83 rcu_assign_pointer(s->hnext, sta->hnext);
90 /* protected by RCU */
91 static struct sta_info *__sta_info_find(struct ieee80211_local *local,
96 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
98 if (compare_ether_addr(sta->addr, addr) == 0)
100 sta = rcu_dereference(sta->hnext);
105 struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
107 return __sta_info_find(local, addr);
109 EXPORT_SYMBOL(sta_info_get);
111 struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
112 struct net_device *dev)
114 struct sta_info *sta;
117 list_for_each_entry_rcu(sta, &local->sta_list, list) {
121 } else if (!dev || dev == sta->sdata->dev) {
129 void sta_info_destroy(struct sta_info *sta)
131 struct ieee80211_local *local = sta->local;
138 rate_control_remove_sta_debugfs(sta);
139 ieee80211_sta_debugfs_remove(sta);
141 #ifdef CONFIG_MAC80211_MESH
142 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
143 mesh_plink_deactivate(sta);
147 * NOTE: This will call synchronize_rcu() internally to
148 * make sure no key references can be in use. We rely on
149 * that here for the mesh code!
151 ieee80211_key_free(sta->key);
154 #ifdef CONFIG_MAC80211_MESH
155 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
156 del_timer_sync(&sta->plink_timer);
159 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
160 local->total_ps_buffered--;
161 dev_kfree_skb_any(skb);
164 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
165 dev_kfree_skb_any(skb);
167 for (i = 0; i < STA_TID_NUM; i++) {
168 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
169 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
171 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
172 rate_control_put(sta->rate_ctrl);
178 /* Caller must hold local->sta_lock */
179 static void sta_info_hash_add(struct ieee80211_local *local,
180 struct sta_info *sta)
182 sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
183 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)], sta);
186 struct sta_info *sta_info_add(struct ieee80211_sub_if_data *sdata,
189 struct ieee80211_local *local = sdata->local;
190 struct sta_info *sta;
192 DECLARE_MAC_BUF(mac);
195 sta = kzalloc(sizeof(*sta), GFP_ATOMIC);
197 return ERR_PTR(-ENOMEM);
199 memcpy(sta->addr, addr, ETH_ALEN);
203 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
204 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
206 if (!sta->rate_ctrl_priv) {
207 rate_control_put(sta->rate_ctrl);
209 return ERR_PTR(-ENOMEM);
212 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
213 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
214 for (i = 0; i < STA_TID_NUM; i++) {
215 /* timer_to_tid must be initialized with identity mapping to
216 * enable session_timer's data differentiation. refer to
217 * sta_rx_agg_session_timer_expired for useage */
218 sta->timer_to_tid[i] = i;
219 /* tid to tx queue: initialize according to HW (0 is valid) */
220 sta->tid_to_tx_q[i] = local->hw.queues;
222 sta->ampdu_mlme.tid_rx[i].session_timer.function =
223 sta_rx_agg_session_timer_expired;
224 sta->ampdu_mlme.tid_rx[i].session_timer.data =
225 (unsigned long)&sta->timer_to_tid[i];
226 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
228 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
229 sta_addba_resp_timer_expired;
230 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
231 (unsigned long)&sta->timer_to_tid[i];
232 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
234 skb_queue_head_init(&sta->ps_tx_buf);
235 skb_queue_head_init(&sta->tx_filtered);
236 spin_lock_irqsave(&local->sta_lock, flags);
237 /* check if STA exists already */
238 if (__sta_info_find(local, addr)) {
239 spin_unlock_irqrestore(&local->sta_lock, flags);
240 return ERR_PTR(-EEXIST);
242 list_add(&sta->list, &local->sta_list);
244 sta_info_hash_add(local, sta);
247 if (local->ops->sta_notify) {
248 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
249 sdata = sdata->u.vlan.ap;
251 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
252 STA_NOTIFY_ADD, addr);
255 spin_unlock_irqrestore(&local->sta_lock, flags);
257 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
258 printk(KERN_DEBUG "%s: Added STA %s\n",
259 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
260 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
262 #ifdef CONFIG_MAC80211_DEBUGFS
263 /* debugfs entry adding might sleep, so schedule process
264 * context task for adding entry for STAs that do not yet
266 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
272 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
275 * This format has been mandated by the IEEE specifications,
276 * so this line may not be changed to use the __set_bit() format.
278 bss->tim[aid / 8] |= (1 << (aid % 8));
281 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
284 * This format has been mandated by the IEEE specifications,
285 * so this line may not be changed to use the __clear_bit() format.
287 bss->tim[aid / 8] &= ~(1 << (aid % 8));
290 static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
291 struct sta_info *sta)
294 __bss_tim_set(bss, sta->aid);
295 if (sta->local->ops->set_tim) {
296 sta->local->tim_in_locked_section = true;
297 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1);
298 sta->local->tim_in_locked_section = false;
302 void sta_info_set_tim_bit(struct sta_info *sta)
306 spin_lock_irqsave(&sta->local->sta_lock, flags);
307 __sta_info_set_tim_bit(sta->sdata->bss, sta);
308 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
311 static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
312 struct sta_info *sta)
315 __bss_tim_clear(bss, sta->aid);
316 if (sta->local->ops->set_tim) {
317 sta->local->tim_in_locked_section = true;
318 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0);
319 sta->local->tim_in_locked_section = false;
323 void sta_info_clear_tim_bit(struct sta_info *sta)
327 spin_lock_irqsave(&sta->local->sta_lock, flags);
328 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
329 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
333 * See comment in __sta_info_unlink,
334 * caller must hold local->sta_lock.
336 static void __sta_info_pin(struct sta_info *sta)
338 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
339 sta->pin_status = STA_INFO_PIN_STAT_PINNED;
343 * See comment in __sta_info_unlink, returns sta if it
344 * needs to be destroyed.
346 static struct sta_info *__sta_info_unpin(struct sta_info *sta)
348 struct sta_info *ret = NULL;
351 spin_lock_irqsave(&sta->local->sta_lock, flags);
352 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
353 sta->pin_status != STA_INFO_PIN_STAT_PINNED);
354 if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
356 sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
357 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
362 static void __sta_info_unlink(struct sta_info **sta)
364 struct ieee80211_local *local = (*sta)->local;
365 struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
366 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
367 DECLARE_MAC_BUF(mbuf);
370 * pull caller's reference if we're already gone.
372 if (sta_info_hash_del(local, *sta)) {
378 * Also pull caller's reference if the STA is pinned by the
379 * task that is adding the debugfs entries. In that case, we
380 * leave the STA "to be freed".
382 * The rules are not trivial, but not too complex either:
383 * (1) pin_status is only modified under the sta_lock
384 * (2) sta_info_debugfs_add_work() will set the status
385 * to PINNED when it found an item that needs a new
386 * debugfs directory created. In that case, that item
387 * must not be freed although all *RCU* users are done
388 * with it. Hence, we tell the caller of _unlink()
389 * that the item is already gone (as can happen when
390 * two tasks try to unlink/destroy at the same time)
391 * (3) We set the pin_status to DESTROY here when we
393 * (4) sta_info_debugfs_add_work() will reset the pin_status
394 * from PINNED to NORMAL when it is done with the item,
395 * but will check for DESTROY before resetting it in
396 * which case it will free the item.
398 if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
399 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
404 list_del(&(*sta)->list);
406 if ((*sta)->flags & WLAN_STA_PS) {
407 (*sta)->flags &= ~WLAN_STA_PS;
409 atomic_dec(&sdata->bss->num_sta_ps);
410 __sta_info_clear_tim_bit(sdata->bss, *sta);
415 if (local->ops->sta_notify) {
416 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
417 sdata = sdata->u.vlan.ap;
419 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
420 STA_NOTIFY_REMOVE, (*sta)->addr);
423 if (ieee80211_vif_is_mesh(&sdata->vif)) {
424 mesh_accept_plinks_update(sdata);
425 #ifdef CONFIG_MAC80211_MESH
426 del_timer(&(*sta)->plink_timer);
430 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
431 printk(KERN_DEBUG "%s: Removed STA %s\n",
432 wiphy_name(local->hw.wiphy), print_mac(mbuf, (*sta)->addr));
433 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
436 void sta_info_unlink(struct sta_info **sta)
438 struct ieee80211_local *local = (*sta)->local;
441 spin_lock_irqsave(&local->sta_lock, flags);
442 __sta_info_unlink(sta);
443 spin_unlock_irqrestore(&local->sta_lock, flags);
446 static inline int sta_info_buffer_expired(struct ieee80211_local *local,
447 struct sta_info *sta,
450 struct ieee80211_tx_packet_data *pkt_data;
456 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
458 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
459 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
461 if (timeout < STA_TX_BUFFER_EXPIRE)
462 timeout = STA_TX_BUFFER_EXPIRE;
463 return time_after(jiffies, pkt_data->jiffies + timeout);
467 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
468 struct sta_info *sta)
472 struct ieee80211_sub_if_data *sdata;
473 DECLARE_MAC_BUF(mac);
475 if (skb_queue_empty(&sta->ps_tx_buf))
479 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
480 skb = skb_peek(&sta->ps_tx_buf);
481 if (sta_info_buffer_expired(local, sta, skb))
482 skb = __skb_dequeue(&sta->ps_tx_buf);
485 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
491 local->total_ps_buffered--;
492 printk(KERN_DEBUG "Buffered frame expired (STA "
493 "%s)\n", print_mac(mac, sta->addr));
496 if (skb_queue_empty(&sta->ps_tx_buf))
497 sta_info_clear_tim_bit(sta);
502 static void sta_info_cleanup(unsigned long data)
504 struct ieee80211_local *local = (struct ieee80211_local *) data;
505 struct sta_info *sta;
508 list_for_each_entry_rcu(sta, &local->sta_list, list)
509 sta_info_cleanup_expire_buffered(local, sta);
512 local->sta_cleanup.expires =
513 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
514 add_timer(&local->sta_cleanup);
517 #ifdef CONFIG_MAC80211_DEBUGFS
518 static void sta_info_debugfs_add_work(struct work_struct *work)
520 struct ieee80211_local *local =
521 container_of(work, struct ieee80211_local, sta_debugfs_add);
522 struct sta_info *sta, *tmp;
528 spin_lock_irqsave(&local->sta_lock, flags);
529 list_for_each_entry(tmp, &local->sta_list, list) {
530 if (!tmp->debugfs.dir) {
536 spin_unlock_irqrestore(&local->sta_lock, flags);
541 ieee80211_sta_debugfs_add(sta);
542 rate_control_add_sta_debugfs(sta);
544 sta = __sta_info_unpin(sta);
548 sta_info_destroy(sta);
554 void sta_info_init(struct ieee80211_local *local)
556 spin_lock_init(&local->sta_lock);
557 INIT_LIST_HEAD(&local->sta_list);
559 setup_timer(&local->sta_cleanup, sta_info_cleanup,
560 (unsigned long)local);
561 local->sta_cleanup.expires =
562 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
564 #ifdef CONFIG_MAC80211_DEBUGFS
565 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
569 int sta_info_start(struct ieee80211_local *local)
571 add_timer(&local->sta_cleanup);
575 void sta_info_stop(struct ieee80211_local *local)
577 del_timer(&local->sta_cleanup);
578 sta_info_flush(local, NULL);
582 * sta_info_flush - flush matching STA entries from the STA table
583 * @local: local interface data
584 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
586 void sta_info_flush(struct ieee80211_local *local,
587 struct ieee80211_sub_if_data *sdata)
589 struct sta_info *sta, *tmp;
595 spin_lock_irqsave(&local->sta_lock, flags);
596 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
597 if (!sdata || sdata == sta->sdata) {
598 __sta_info_unlink(&sta);
600 list_add_tail(&sta->list, &tmp_list);
603 spin_unlock_irqrestore(&local->sta_lock, flags);
607 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
608 sta_info_destroy(sta);