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>
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
22 #include "debugfs_sta.h"
24 /* Caller must hold local->sta_lock */
25 static void sta_info_hash_add(struct ieee80211_local
*local
,
28 sta
->hnext
= local
->sta_hash
[STA_HASH(sta
->addr
)];
29 local
->sta_hash
[STA_HASH(sta
->addr
)] = sta
;
33 /* Caller must hold local->sta_lock */
34 static int sta_info_hash_del(struct ieee80211_local
*local
,
39 s
= local
->sta_hash
[STA_HASH(sta
->addr
)];
43 local
->sta_hash
[STA_HASH(sta
->addr
)] = s
->hnext
;
47 while (s
->hnext
&& s
->hnext
!= sta
)
50 s
->hnext
= sta
->hnext
;
57 struct sta_info
*sta_info_get(struct ieee80211_local
*local
, u8
*addr
)
61 read_lock_bh(&local
->sta_lock
);
62 sta
= local
->sta_hash
[STA_HASH(addr
)];
64 if (memcmp(sta
->addr
, addr
, ETH_ALEN
) == 0) {
70 read_unlock_bh(&local
->sta_lock
);
74 EXPORT_SYMBOL(sta_info_get
);
76 int sta_info_min_txrate_get(struct ieee80211_local
*local
)
79 struct ieee80211_hw_mode
*mode
;
80 int min_txrate
= 9999999;
83 read_lock_bh(&local
->sta_lock
);
84 mode
= local
->oper_hw_mode
;
85 for (i
= 0; i
< STA_HASH_SIZE
; i
++) {
86 sta
= local
->sta_hash
[i
];
88 if (sta
->txrate
< min_txrate
)
89 min_txrate
= sta
->txrate
;
93 read_unlock_bh(&local
->sta_lock
);
94 if (min_txrate
== 9999999)
97 return mode
->rates
[min_txrate
].rate
;
101 static void sta_info_release(struct kref
*kref
)
103 struct sta_info
*sta
= container_of(kref
, struct sta_info
, kref
);
104 struct ieee80211_local
*local
= sta
->local
;
107 /* free sta structure; it has already been removed from
108 * hash table etc. external structures. Make sure that all
109 * buffered frames are release (one might have been added
110 * after sta_info_free() was called). */
111 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
112 local
->total_ps_buffered
--;
113 dev_kfree_skb_any(skb
);
115 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
116 dev_kfree_skb_any(skb
);
118 rate_control_free_sta(sta
->rate_ctrl
, sta
->rate_ctrl_priv
);
119 rate_control_put(sta
->rate_ctrl
);
124 void sta_info_put(struct sta_info
*sta
)
126 kref_put(&sta
->kref
, sta_info_release
);
128 EXPORT_SYMBOL(sta_info_put
);
131 struct sta_info
* sta_info_add(struct ieee80211_local
*local
,
132 struct net_device
*dev
, u8
*addr
, gfp_t gfp
)
134 struct sta_info
*sta
;
135 DECLARE_MAC_BUF(mac
);
137 sta
= kzalloc(sizeof(*sta
), gfp
);
141 kref_init(&sta
->kref
);
143 sta
->rate_ctrl
= rate_control_get(local
->rate_ctrl
);
144 sta
->rate_ctrl_priv
= rate_control_alloc_sta(sta
->rate_ctrl
, gfp
);
145 if (!sta
->rate_ctrl_priv
) {
146 rate_control_put(sta
->rate_ctrl
);
151 memcpy(sta
->addr
, addr
, ETH_ALEN
);
154 skb_queue_head_init(&sta
->ps_tx_buf
);
155 skb_queue_head_init(&sta
->tx_filtered
);
156 __sta_info_get(sta
); /* sta used by caller, decremented by
158 write_lock_bh(&local
->sta_lock
);
159 list_add(&sta
->list
, &local
->sta_list
);
161 sta_info_hash_add(local
, sta
);
162 if (local
->ops
->sta_notify
)
163 local
->ops
->sta_notify(local_to_hw(local
), dev
->ifindex
,
164 STA_NOTIFY_ADD
, addr
);
165 write_unlock_bh(&local
->sta_lock
);
167 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
168 printk(KERN_DEBUG
"%s: Added STA %s\n",
169 wiphy_name(local
->hw
.wiphy
), print_mac(mac
, addr
));
170 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
172 #ifdef CONFIG_MAC80211_DEBUGFS
173 /* debugfs entry adding might sleep, so schedule process
174 * context task for adding entry for STAs that do not yet
176 queue_work(local
->hw
.workqueue
, &local
->sta_debugfs_add
);
182 /* Caller must hold local->sta_lock */
183 void sta_info_remove(struct sta_info
*sta
)
185 struct ieee80211_local
*local
= sta
->local
;
186 struct ieee80211_sub_if_data
*sdata
;
188 /* don't do anything if we've been removed already */
189 if (sta_info_hash_del(local
, sta
))
192 list_del(&sta
->list
);
193 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
194 if (sta
->flags
& WLAN_STA_PS
) {
195 sta
->flags
&= ~WLAN_STA_PS
;
197 atomic_dec(&sdata
->bss
->num_sta_ps
);
200 sta_info_remove_aid_ptr(sta
);
204 void sta_info_free(struct sta_info
*sta
)
207 struct ieee80211_local
*local
= sta
->local
;
208 DECLARE_MAC_BUF(mac
);
212 write_lock_bh(&local
->sta_lock
);
213 sta_info_remove(sta
);
214 write_unlock_bh(&local
->sta_lock
);
216 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
217 local
->total_ps_buffered
--;
220 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
224 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
225 printk(KERN_DEBUG
"%s: Removed STA %s\n",
226 wiphy_name(local
->hw
.wiphy
), print_mac(mac
, sta
->addr
));
227 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
229 ieee80211_key_free(sta
->key
);
232 if (local
->ops
->sta_notify
)
233 local
->ops
->sta_notify(local_to_hw(local
), sta
->dev
->ifindex
,
234 STA_NOTIFY_REMOVE
, sta
->addr
);
236 rate_control_remove_sta_debugfs(sta
);
237 ieee80211_sta_debugfs_remove(sta
);
243 static inline int sta_info_buffer_expired(struct ieee80211_local
*local
,
244 struct sta_info
*sta
,
247 struct ieee80211_tx_packet_data
*pkt_data
;
253 pkt_data
= (struct ieee80211_tx_packet_data
*) skb
->cb
;
255 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
256 timeout
= (sta
->listen_interval
* local
->hw
.conf
.beacon_int
* 32 /
258 if (timeout
< STA_TX_BUFFER_EXPIRE
)
259 timeout
= STA_TX_BUFFER_EXPIRE
;
260 return time_after(jiffies
, pkt_data
->jiffies
+ timeout
);
264 static void sta_info_cleanup_expire_buffered(struct ieee80211_local
*local
,
265 struct sta_info
*sta
)
269 DECLARE_MAC_BUF(mac
);
271 if (skb_queue_empty(&sta
->ps_tx_buf
))
275 spin_lock_irqsave(&sta
->ps_tx_buf
.lock
, flags
);
276 skb
= skb_peek(&sta
->ps_tx_buf
);
277 if (sta_info_buffer_expired(local
, sta
, skb
)) {
278 skb
= __skb_dequeue(&sta
->ps_tx_buf
);
279 if (skb_queue_empty(&sta
->ps_tx_buf
))
280 sta
->flags
&= ~WLAN_STA_TIM
;
283 spin_unlock_irqrestore(&sta
->ps_tx_buf
.lock
, flags
);
286 local
->total_ps_buffered
--;
287 printk(KERN_DEBUG
"Buffered frame expired (STA "
288 "%s)\n", print_mac(mac
, sta
->addr
));
296 static void sta_info_cleanup(unsigned long data
)
298 struct ieee80211_local
*local
= (struct ieee80211_local
*) data
;
299 struct sta_info
*sta
;
301 read_lock_bh(&local
->sta_lock
);
302 list_for_each_entry(sta
, &local
->sta_list
, list
) {
304 sta_info_cleanup_expire_buffered(local
, sta
);
307 read_unlock_bh(&local
->sta_lock
);
309 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
310 add_timer(&local
->sta_cleanup
);
313 #ifdef CONFIG_MAC80211_DEBUGFS
314 static void sta_info_debugfs_add_task(struct work_struct
*work
)
316 struct ieee80211_local
*local
=
317 container_of(work
, struct ieee80211_local
, sta_debugfs_add
);
318 struct sta_info
*sta
, *tmp
;
322 read_lock_bh(&local
->sta_lock
);
323 list_for_each_entry(tmp
, &local
->sta_list
, list
) {
324 if (!tmp
->debugfs
.dir
) {
330 read_unlock_bh(&local
->sta_lock
);
335 ieee80211_sta_debugfs_add(sta
);
336 rate_control_add_sta_debugfs(sta
);
342 void sta_info_init(struct ieee80211_local
*local
)
344 rwlock_init(&local
->sta_lock
);
345 INIT_LIST_HEAD(&local
->sta_list
);
347 init_timer(&local
->sta_cleanup
);
348 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
349 local
->sta_cleanup
.data
= (unsigned long) local
;
350 local
->sta_cleanup
.function
= sta_info_cleanup
;
352 #ifdef CONFIG_MAC80211_DEBUGFS
353 INIT_WORK(&local
->sta_debugfs_add
, sta_info_debugfs_add_task
);
357 int sta_info_start(struct ieee80211_local
*local
)
359 add_timer(&local
->sta_cleanup
);
363 void sta_info_stop(struct ieee80211_local
*local
)
365 del_timer(&local
->sta_cleanup
);
366 sta_info_flush(local
, NULL
);
369 void sta_info_remove_aid_ptr(struct sta_info
*sta
)
371 struct ieee80211_sub_if_data
*sdata
;
376 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
378 if (sdata
->local
->ops
->set_tim
)
379 sdata
->local
->ops
->set_tim(local_to_hw(sdata
->local
),
382 __bss_tim_clear(sdata
->bss
, sta
->aid
);
387 * sta_info_flush - flush matching STA entries from the STA table
388 * @local: local interface data
389 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
391 void sta_info_flush(struct ieee80211_local
*local
, struct net_device
*dev
)
393 struct sta_info
*sta
, *tmp
;
396 write_lock_bh(&local
->sta_lock
);
397 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
)
398 if (!dev
|| dev
== sta
->dev
) {
400 sta_info_remove(sta
);
401 list_add_tail(&sta
->list
, &tmp_list
);
403 write_unlock_bh(&local
->sta_lock
);
405 list_for_each_entry_safe(sta
, tmp
, &tmp_list
, list
) {