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>
19 #include <net/mac80211.h>
20 #include "ieee80211_i.h"
21 #include "ieee80211_rate.h"
23 #include "debugfs_sta.h"
24 #ifdef CONFIG_MAC80211_MESH
28 /* Caller must hold local->sta_lock */
29 static void sta_info_hash_add(struct ieee80211_local *local,
32 sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
33 local->sta_hash[STA_HASH(sta->addr)] = sta;
37 /* Caller must hold local->sta_lock */
38 static int sta_info_hash_del(struct ieee80211_local *local,
43 s = local->sta_hash[STA_HASH(sta->addr)];
47 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
51 while (s->hnext && s->hnext != sta)
54 s->hnext = sta->hnext;
61 /* must hold local->sta_lock */
62 static struct sta_info *__sta_info_find(struct ieee80211_local *local,
67 sta = local->sta_hash[STA_HASH(addr)];
69 if (compare_ether_addr(sta->addr, addr) == 0)
76 struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
80 read_lock_bh(&local->sta_lock);
81 sta = __sta_info_find(local, addr);
84 read_unlock_bh(&local->sta_lock);
88 EXPORT_SYMBOL(sta_info_get);
90 struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
91 struct net_device *dev)
96 read_lock_bh(&local->sta_lock);
97 list_for_each_entry(sta, &local->sta_list, list) {
101 } else if (!dev || dev == sta->dev) {
103 read_unlock_bh(&local->sta_lock);
107 read_unlock_bh(&local->sta_lock);
112 static void sta_info_release(struct kref *kref)
114 struct sta_info *sta = container_of(kref, struct sta_info, kref);
115 struct ieee80211_local *local = sta->local;
119 /* free sta structure; it has already been removed from
120 * hash table etc. external structures. Make sure that all
121 * buffered frames are release (one might have been added
122 * after sta_info_free() was called). */
123 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
124 local->total_ps_buffered--;
125 dev_kfree_skb_any(skb);
127 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
128 dev_kfree_skb_any(skb);
130 for (i = 0; i < STA_TID_NUM; i++) {
131 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
132 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
134 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
135 rate_control_put(sta->rate_ctrl);
140 void sta_info_put(struct sta_info *sta)
142 kref_put(&sta->kref, sta_info_release);
144 EXPORT_SYMBOL(sta_info_put);
147 struct sta_info *sta_info_add(struct ieee80211_local *local,
148 struct net_device *dev, u8 *addr, gfp_t gfp)
150 struct sta_info *sta;
152 DECLARE_MAC_BUF(mac);
154 sta = kzalloc(sizeof(*sta), gfp);
156 return ERR_PTR(-ENOMEM);
158 kref_init(&sta->kref);
160 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
161 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
162 if (!sta->rate_ctrl_priv) {
163 rate_control_put(sta->rate_ctrl);
165 return ERR_PTR(-ENOMEM);
168 memcpy(sta->addr, addr, ETH_ALEN);
171 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
172 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
173 for (i = 0; i < STA_TID_NUM; i++) {
174 /* timer_to_tid must be initialized with identity mapping to
175 * enable session_timer's data differentiation. refer to
176 * sta_rx_agg_session_timer_expired for useage */
177 sta->timer_to_tid[i] = i;
178 /* tid to tx queue: initialize according to HW (0 is valid) */
179 sta->tid_to_tx_q[i] = local->hw.queues;
181 sta->ampdu_mlme.tid_rx[i].session_timer.function =
182 sta_rx_agg_session_timer_expired;
183 sta->ampdu_mlme.tid_rx[i].session_timer.data =
184 (unsigned long)&sta->timer_to_tid[i];
185 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
187 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
188 sta_addba_resp_timer_expired;
189 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
190 (unsigned long)&sta->timer_to_tid[i];
191 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
193 skb_queue_head_init(&sta->ps_tx_buf);
194 skb_queue_head_init(&sta->tx_filtered);
195 write_lock_bh(&local->sta_lock);
196 /* mark sta as used (by caller) */
198 /* check if STA exists already */
199 if (__sta_info_find(local, addr)) {
200 write_unlock_bh(&local->sta_lock);
202 return ERR_PTR(-EEXIST);
204 list_add(&sta->list, &local->sta_list);
206 sta_info_hash_add(local, sta);
207 if (local->ops->sta_notify) {
208 struct ieee80211_sub_if_data *sdata;
210 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
211 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
212 sdata = sdata->u.vlan.ap;
214 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
215 STA_NOTIFY_ADD, addr);
217 write_unlock_bh(&local->sta_lock);
219 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
220 printk(KERN_DEBUG "%s: Added STA %s\n",
221 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
222 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
224 #ifdef CONFIG_MAC80211_DEBUGFS
225 /* debugfs entry adding might sleep, so schedule process
226 * context task for adding entry for STAs that do not yet
228 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
234 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
237 * This format has been mandated by the IEEE specifications,
238 * so this line may not be changed to use the __set_bit() format.
240 bss->tim[aid / 8] |= (1 << (aid % 8));
243 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
246 * This format has been mandated by the IEEE specifications,
247 * so this line may not be changed to use the __clear_bit() format.
249 bss->tim[aid / 8] &= ~(1 << (aid % 8));
252 static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
253 struct sta_info *sta)
256 __bss_tim_set(bss, sta->aid);
257 if (sta->local->ops->set_tim)
258 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1);
261 void sta_info_set_tim_bit(struct sta_info *sta)
263 struct ieee80211_sub_if_data *sdata;
265 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
267 read_lock_bh(&sta->local->sta_lock);
268 __sta_info_set_tim_bit(sdata->bss, sta);
269 read_unlock_bh(&sta->local->sta_lock);
272 static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
273 struct sta_info *sta)
276 __bss_tim_clear(bss, sta->aid);
277 if (sta->local->ops->set_tim)
278 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0);
281 void sta_info_clear_tim_bit(struct sta_info *sta)
283 struct ieee80211_sub_if_data *sdata;
285 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
287 read_lock_bh(&sta->local->sta_lock);
288 __sta_info_clear_tim_bit(sdata->bss, sta);
289 read_unlock_bh(&sta->local->sta_lock);
292 /* Caller must hold local->sta_lock */
293 void sta_info_remove(struct sta_info *sta)
295 struct ieee80211_local *local = sta->local;
296 struct ieee80211_sub_if_data *sdata;
298 /* don't do anything if we've been removed already */
299 if (sta_info_hash_del(local, sta))
302 list_del(&sta->list);
303 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
304 if (sta->flags & WLAN_STA_PS) {
305 sta->flags &= ~WLAN_STA_PS;
307 atomic_dec(&sdata->bss->num_sta_ps);
308 __sta_info_clear_tim_bit(sdata->bss, sta);
312 #ifdef CONFIG_MAC80211_MESH
313 if (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
314 mesh_accept_plinks_update(sdata->dev);
318 void sta_info_free(struct sta_info *sta)
321 struct ieee80211_local *local = sta->local;
322 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
324 DECLARE_MAC_BUF(mac);
328 write_lock_bh(&local->sta_lock);
329 sta_info_remove(sta);
330 write_unlock_bh(&local->sta_lock);
332 #ifdef CONFIG_MAC80211_MESH
333 if (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT) {
334 spin_lock_bh(&sta->plink_lock);
335 mesh_plink_deactivate(sta);
336 spin_unlock_bh(&sta->plink_lock);
340 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
341 local->total_ps_buffered--;
344 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
348 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
349 printk(KERN_DEBUG "%s: Removed STA %s\n",
350 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
351 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
353 ieee80211_key_free(sta->key);
356 if (local->ops->sta_notify) {
358 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
359 sdata = sdata->u.vlan.ap;
361 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
362 STA_NOTIFY_REMOVE, sta->addr);
365 rate_control_remove_sta_debugfs(sta);
366 ieee80211_sta_debugfs_remove(sta);
372 static inline int sta_info_buffer_expired(struct ieee80211_local *local,
373 struct sta_info *sta,
376 struct ieee80211_tx_packet_data *pkt_data;
382 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
384 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
385 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
387 if (timeout < STA_TX_BUFFER_EXPIRE)
388 timeout = STA_TX_BUFFER_EXPIRE;
389 return time_after(jiffies, pkt_data->jiffies + timeout);
393 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
394 struct sta_info *sta)
398 struct ieee80211_sub_if_data *sdata;
399 DECLARE_MAC_BUF(mac);
401 if (skb_queue_empty(&sta->ps_tx_buf))
405 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
406 skb = skb_peek(&sta->ps_tx_buf);
407 if (sta_info_buffer_expired(local, sta, skb))
408 skb = __skb_dequeue(&sta->ps_tx_buf);
411 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
416 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
417 local->total_ps_buffered--;
418 printk(KERN_DEBUG "Buffered frame expired (STA "
419 "%s)\n", print_mac(mac, sta->addr));
422 if (skb_queue_empty(&sta->ps_tx_buf))
423 sta_info_clear_tim_bit(sta);
428 static void sta_info_cleanup(unsigned long data)
430 struct ieee80211_local *local = (struct ieee80211_local *) data;
431 struct sta_info *sta;
433 read_lock_bh(&local->sta_lock);
434 list_for_each_entry(sta, &local->sta_list, list) {
436 sta_info_cleanup_expire_buffered(local, sta);
439 read_unlock_bh(&local->sta_lock);
441 local->sta_cleanup.expires =
442 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
443 add_timer(&local->sta_cleanup);
446 #ifdef CONFIG_MAC80211_DEBUGFS
447 static void sta_info_debugfs_add_task(struct work_struct *work)
449 struct ieee80211_local *local =
450 container_of(work, struct ieee80211_local, sta_debugfs_add);
451 struct sta_info *sta, *tmp;
455 read_lock_bh(&local->sta_lock);
456 list_for_each_entry(tmp, &local->sta_list, list) {
457 if (!tmp->debugfs.dir) {
463 read_unlock_bh(&local->sta_lock);
468 ieee80211_sta_debugfs_add(sta);
469 rate_control_add_sta_debugfs(sta);
475 void sta_info_init(struct ieee80211_local *local)
477 rwlock_init(&local->sta_lock);
478 INIT_LIST_HEAD(&local->sta_list);
480 setup_timer(&local->sta_cleanup, sta_info_cleanup,
481 (unsigned long)local);
482 local->sta_cleanup.expires =
483 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
485 #ifdef CONFIG_MAC80211_DEBUGFS
486 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
490 int sta_info_start(struct ieee80211_local *local)
492 add_timer(&local->sta_cleanup);
496 void sta_info_stop(struct ieee80211_local *local)
498 del_timer(&local->sta_cleanup);
499 sta_info_flush(local, NULL);
503 * sta_info_flush - flush matching STA entries from the STA table
504 * @local: local interface data
505 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
507 void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
509 struct sta_info *sta, *tmp;
512 write_lock_bh(&local->sta_lock);
513 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
514 if (!dev || dev == sta->dev) {
516 sta_info_remove(sta);
517 list_add_tail(&sta->list, &tmp_list);
519 write_unlock_bh(&local->sta_lock);
521 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {