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"
25 /* Caller must hold local->sta_lock */
26 static void sta_info_hash_add(struct ieee80211_local *local,
29 sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
30 local->sta_hash[STA_HASH(sta->addr)] = sta;
34 /* Caller must hold local->sta_lock */
35 static int sta_info_hash_del(struct ieee80211_local *local,
40 s = local->sta_hash[STA_HASH(sta->addr)];
44 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
48 while (s->hnext && s->hnext != sta)
51 s->hnext = sta->hnext;
58 /* must hold local->sta_lock */
59 static struct sta_info *__sta_info_find(struct ieee80211_local *local,
64 sta = local->sta_hash[STA_HASH(addr)];
66 if (compare_ether_addr(sta->addr, addr) == 0)
73 struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
77 read_lock_bh(&local->sta_lock);
78 sta = __sta_info_find(local, addr);
81 read_unlock_bh(&local->sta_lock);
85 EXPORT_SYMBOL(sta_info_get);
88 static void sta_info_release(struct kref *kref)
90 struct sta_info *sta = container_of(kref, struct sta_info, kref);
91 struct ieee80211_local *local = sta->local;
95 /* free sta structure; it has already been removed from
96 * hash table etc. external structures. Make sure that all
97 * buffered frames are release (one might have been added
98 * after sta_info_free() was called). */
99 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
100 local->total_ps_buffered--;
101 dev_kfree_skb_any(skb);
103 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
104 dev_kfree_skb_any(skb);
106 for (i = 0; i < STA_TID_NUM; i++) {
107 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
108 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
110 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
111 rate_control_put(sta->rate_ctrl);
116 void sta_info_put(struct sta_info *sta)
118 kref_put(&sta->kref, sta_info_release);
120 EXPORT_SYMBOL(sta_info_put);
123 struct sta_info *sta_info_add(struct ieee80211_local *local,
124 struct net_device *dev, u8 *addr, gfp_t gfp)
126 struct sta_info *sta;
128 DECLARE_MAC_BUF(mac);
130 sta = kzalloc(sizeof(*sta), gfp);
132 return ERR_PTR(-ENOMEM);
134 kref_init(&sta->kref);
136 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
137 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
138 if (!sta->rate_ctrl_priv) {
139 rate_control_put(sta->rate_ctrl);
141 return ERR_PTR(-ENOMEM);
144 memcpy(sta->addr, addr, ETH_ALEN);
147 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
148 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
149 for (i = 0; i < STA_TID_NUM; i++) {
150 /* timer_to_tid must be initialized with identity mapping to
151 * enable session_timer's data differentiation. refer to
152 * sta_rx_agg_session_timer_expired for useage */
153 sta->timer_to_tid[i] = i;
154 /* tid to tx queue: initialize according to HW (0 is valid) */
155 sta->tid_to_tx_q[i] = local->hw.queues;
157 sta->ampdu_mlme.tid_rx[i].session_timer.function =
158 sta_rx_agg_session_timer_expired;
159 sta->ampdu_mlme.tid_rx[i].session_timer.data =
160 (unsigned long)&sta->timer_to_tid[i];
161 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
163 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
164 sta_addba_resp_timer_expired;
165 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
166 (unsigned long)&sta->timer_to_tid[i];
167 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
169 skb_queue_head_init(&sta->ps_tx_buf);
170 skb_queue_head_init(&sta->tx_filtered);
171 write_lock_bh(&local->sta_lock);
172 /* mark sta as used (by caller) */
174 /* check if STA exists already */
175 if (__sta_info_find(local, addr)) {
176 write_unlock_bh(&local->sta_lock);
178 return ERR_PTR(-EEXIST);
180 list_add(&sta->list, &local->sta_list);
182 sta_info_hash_add(local, sta);
183 if (local->ops->sta_notify) {
184 struct ieee80211_sub_if_data *sdata;
186 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
187 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
188 sdata = sdata->u.vlan.ap;
190 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
191 STA_NOTIFY_ADD, addr);
193 write_unlock_bh(&local->sta_lock);
195 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
196 printk(KERN_DEBUG "%s: Added STA %s\n",
197 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
198 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
200 #ifdef CONFIG_MAC80211_DEBUGFS
201 /* debugfs entry adding might sleep, so schedule process
202 * context task for adding entry for STAs that do not yet
204 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
210 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
213 * This format has been mandated by the IEEE specifications,
214 * so this line may not be changed to use the __set_bit() format.
216 bss->tim[aid / 8] |= (1 << (aid % 8));
219 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
222 * This format has been mandated by the IEEE specifications,
223 * so this line may not be changed to use the __clear_bit() format.
225 bss->tim[aid / 8] &= ~(1 << (aid % 8));
228 static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
229 struct sta_info *sta)
232 __bss_tim_set(bss, sta->aid);
233 if (sta->local->ops->set_tim)
234 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1);
237 void sta_info_set_tim_bit(struct sta_info *sta)
239 struct ieee80211_sub_if_data *sdata;
241 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
243 read_lock_bh(&sta->local->sta_lock);
244 __sta_info_set_tim_bit(sdata->bss, sta);
245 read_unlock_bh(&sta->local->sta_lock);
248 static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
249 struct sta_info *sta)
252 __bss_tim_clear(bss, sta->aid);
253 if (sta->local->ops->set_tim)
254 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0);
257 void sta_info_clear_tim_bit(struct sta_info *sta)
259 struct ieee80211_sub_if_data *sdata;
261 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
263 read_lock_bh(&sta->local->sta_lock);
264 __sta_info_clear_tim_bit(sdata->bss, sta);
265 read_unlock_bh(&sta->local->sta_lock);
268 /* Caller must hold local->sta_lock */
269 void sta_info_remove(struct sta_info *sta)
271 struct ieee80211_local *local = sta->local;
272 struct ieee80211_sub_if_data *sdata;
274 /* don't do anything if we've been removed already */
275 if (sta_info_hash_del(local, sta))
278 list_del(&sta->list);
279 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
280 if (sta->flags & WLAN_STA_PS) {
281 sta->flags &= ~WLAN_STA_PS;
283 atomic_dec(&sdata->bss->num_sta_ps);
284 __sta_info_clear_tim_bit(sdata->bss, sta);
289 void sta_info_free(struct sta_info *sta)
292 struct ieee80211_local *local = sta->local;
293 DECLARE_MAC_BUF(mac);
297 write_lock_bh(&local->sta_lock);
298 sta_info_remove(sta);
299 write_unlock_bh(&local->sta_lock);
301 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
302 local->total_ps_buffered--;
305 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
309 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
310 printk(KERN_DEBUG "%s: Removed STA %s\n",
311 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
312 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
314 ieee80211_key_free(sta->key);
317 if (local->ops->sta_notify) {
318 struct ieee80211_sub_if_data *sdata;
320 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
322 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
323 sdata = sdata->u.vlan.ap;
325 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
326 STA_NOTIFY_REMOVE, sta->addr);
329 rate_control_remove_sta_debugfs(sta);
330 ieee80211_sta_debugfs_remove(sta);
336 static inline int sta_info_buffer_expired(struct ieee80211_local *local,
337 struct sta_info *sta,
340 struct ieee80211_tx_packet_data *pkt_data;
346 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
348 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
349 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
351 if (timeout < STA_TX_BUFFER_EXPIRE)
352 timeout = STA_TX_BUFFER_EXPIRE;
353 return time_after(jiffies, pkt_data->jiffies + timeout);
357 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
358 struct sta_info *sta)
362 struct ieee80211_sub_if_data *sdata;
363 DECLARE_MAC_BUF(mac);
365 if (skb_queue_empty(&sta->ps_tx_buf))
369 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
370 skb = skb_peek(&sta->ps_tx_buf);
371 if (sta_info_buffer_expired(local, sta, skb))
372 skb = __skb_dequeue(&sta->ps_tx_buf);
375 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
380 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
381 local->total_ps_buffered--;
382 printk(KERN_DEBUG "Buffered frame expired (STA "
383 "%s)\n", print_mac(mac, sta->addr));
386 if (skb_queue_empty(&sta->ps_tx_buf))
387 sta_info_clear_tim_bit(sta);
392 static void sta_info_cleanup(unsigned long data)
394 struct ieee80211_local *local = (struct ieee80211_local *) data;
395 struct sta_info *sta;
397 read_lock_bh(&local->sta_lock);
398 list_for_each_entry(sta, &local->sta_list, list) {
400 sta_info_cleanup_expire_buffered(local, sta);
403 read_unlock_bh(&local->sta_lock);
405 local->sta_cleanup.expires =
406 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
407 add_timer(&local->sta_cleanup);
410 #ifdef CONFIG_MAC80211_DEBUGFS
411 static void sta_info_debugfs_add_task(struct work_struct *work)
413 struct ieee80211_local *local =
414 container_of(work, struct ieee80211_local, sta_debugfs_add);
415 struct sta_info *sta, *tmp;
419 read_lock_bh(&local->sta_lock);
420 list_for_each_entry(tmp, &local->sta_list, list) {
421 if (!tmp->debugfs.dir) {
427 read_unlock_bh(&local->sta_lock);
432 ieee80211_sta_debugfs_add(sta);
433 rate_control_add_sta_debugfs(sta);
439 void sta_info_init(struct ieee80211_local *local)
441 rwlock_init(&local->sta_lock);
442 INIT_LIST_HEAD(&local->sta_list);
444 setup_timer(&local->sta_cleanup, sta_info_cleanup,
445 (unsigned long)local);
446 local->sta_cleanup.expires =
447 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
449 #ifdef CONFIG_MAC80211_DEBUGFS
450 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
454 int sta_info_start(struct ieee80211_local *local)
456 add_timer(&local->sta_cleanup);
460 void sta_info_stop(struct ieee80211_local *local)
462 del_timer(&local->sta_cleanup);
463 sta_info_flush(local, NULL);
467 * sta_info_flush - flush matching STA entries from the STA table
468 * @local: local interface data
469 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
471 void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
473 struct sta_info *sta, *tmp;
476 write_lock_bh(&local->sta_lock);
477 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
478 if (!dev || dev == sta->dev) {
480 sta_info_remove(sta);
481 list_add_tail(&sta->list, &tmp_list);
483 write_unlock_bh(&local->sta_lock);
485 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {