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_key.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 void sta_info_hash_del(struct ieee80211_local
*local
,
40 s
= local
->sta_hash
[STA_HASH(sta
->addr
)];
43 if (memcmp(s
->addr
, sta
->addr
, ETH_ALEN
) == 0) {
44 local
->sta_hash
[STA_HASH(sta
->addr
)] = s
->hnext
;
48 while (s
->hnext
&& memcmp(s
->hnext
->addr
, sta
->addr
, ETH_ALEN
) != 0)
51 s
->hnext
= s
->hnext
->hnext
;
53 printk(KERN_ERR
"%s: could not remove STA " MAC_FMT
" from "
54 "hash table\n", local
->mdev
->name
, MAC_ARG(sta
->addr
));
57 static inline void __sta_info_get(struct sta_info
*sta
)
62 struct sta_info
*sta_info_get(struct ieee80211_local
*local
, u8
*addr
)
66 spin_lock_bh(&local
->sta_lock
);
67 sta
= local
->sta_hash
[STA_HASH(addr
)];
69 if (memcmp(sta
->addr
, addr
, ETH_ALEN
) == 0) {
75 spin_unlock_bh(&local
->sta_lock
);
79 EXPORT_SYMBOL(sta_info_get
);
81 int sta_info_min_txrate_get(struct ieee80211_local
*local
)
84 struct ieee80211_hw_mode
*mode
;
85 int min_txrate
= 9999999;
88 spin_lock_bh(&local
->sta_lock
);
89 mode
= local
->oper_hw_mode
;
90 for (i
= 0; i
< STA_HASH_SIZE
; i
++) {
91 sta
= local
->sta_hash
[i
];
93 if (sta
->txrate
< min_txrate
)
94 min_txrate
= sta
->txrate
;
98 spin_unlock_bh(&local
->sta_lock
);
99 if (min_txrate
== 9999999)
102 return mode
->rates
[min_txrate
].rate
;
106 static void sta_info_release(struct kref
*kref
)
108 struct sta_info
*sta
= container_of(kref
, struct sta_info
, kref
);
109 struct ieee80211_local
*local
= sta
->local
;
112 /* free sta structure; it has already been removed from
113 * hash table etc. external structures. Make sure that all
114 * buffered frames are release (one might have been added
115 * after sta_info_free() was called). */
116 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
117 local
->total_ps_buffered
--;
118 dev_kfree_skb_any(skb
);
120 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
121 dev_kfree_skb_any(skb
);
123 rate_control_free_sta(sta
->rate_ctrl
, sta
->rate_ctrl_priv
);
124 rate_control_put(sta
->rate_ctrl
);
126 ieee80211_debugfs_key_sta_del(sta
->key
, sta
);
131 void sta_info_put(struct sta_info
*sta
)
133 kref_put(&sta
->kref
, sta_info_release
);
135 EXPORT_SYMBOL(sta_info_put
);
138 struct sta_info
* sta_info_add(struct ieee80211_local
*local
,
139 struct net_device
*dev
, u8
*addr
, gfp_t gfp
)
141 struct sta_info
*sta
;
143 sta
= kzalloc(sizeof(*sta
), gfp
);
147 kref_init(&sta
->kref
);
149 sta
->rate_ctrl
= rate_control_get(local
->rate_ctrl
);
150 sta
->rate_ctrl_priv
= rate_control_alloc_sta(sta
->rate_ctrl
, gfp
);
151 if (!sta
->rate_ctrl_priv
) {
152 rate_control_put(sta
->rate_ctrl
);
153 kref_put(&sta
->kref
, sta_info_release
);
158 memcpy(sta
->addr
, addr
, ETH_ALEN
);
161 skb_queue_head_init(&sta
->ps_tx_buf
);
162 skb_queue_head_init(&sta
->tx_filtered
);
163 __sta_info_get(sta
); /* sta used by caller, decremented by
165 spin_lock_bh(&local
->sta_lock
);
166 list_add(&sta
->list
, &local
->sta_list
);
168 sta_info_hash_add(local
, sta
);
169 spin_unlock_bh(&local
->sta_lock
);
170 if (local
->ops
->sta_table_notification
)
171 local
->ops
->sta_table_notification(local_to_hw(local
),
173 sta
->key_idx_compression
= HW_KEY_IDX_INVALID
;
175 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
176 printk(KERN_DEBUG
"%s: Added STA " MAC_FMT
"\n",
177 local
->mdev
->name
, MAC_ARG(addr
));
178 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
180 #ifdef CONFIG_MAC80211_DEBUGFS
181 if (!in_interrupt()) {
182 sta
->debugfs_registered
= 1;
183 ieee80211_sta_debugfs_add(sta
);
184 rate_control_add_sta_debugfs(sta
);
186 /* debugfs entry adding might sleep, so schedule process
187 * context task for adding entry for STAs that do not yet
189 queue_work(local
->hw
.workqueue
, &local
->sta_debugfs_add
);
196 static void finish_sta_info_free(struct ieee80211_local
*local
,
197 struct sta_info
*sta
)
199 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
200 printk(KERN_DEBUG
"%s: Removed STA " MAC_FMT
"\n",
201 local
->mdev
->name
, MAC_ARG(sta
->addr
));
202 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
205 ieee80211_debugfs_key_remove(sta
->key
);
206 ieee80211_key_free(sta
->key
);
210 rate_control_remove_sta_debugfs(sta
);
211 ieee80211_sta_debugfs_remove(sta
);
216 static void sta_info_remove(struct sta_info
*sta
)
218 struct ieee80211_local
*local
= sta
->local
;
219 struct ieee80211_sub_if_data
*sdata
;
221 sta_info_hash_del(local
, sta
);
222 list_del(&sta
->list
);
223 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
224 if (sta
->flags
& WLAN_STA_PS
) {
225 sta
->flags
&= ~WLAN_STA_PS
;
227 atomic_dec(&sdata
->bss
->num_sta_ps
);
230 sta_info_remove_aid_ptr(sta
);
233 void sta_info_free(struct sta_info
*sta
, int locked
)
236 struct ieee80211_local
*local
= sta
->local
;
239 spin_lock_bh(&local
->sta_lock
);
240 sta_info_remove(sta
);
241 spin_unlock_bh(&local
->sta_lock
);
243 sta_info_remove(sta
);
245 if (local
->ops
->sta_table_notification
)
246 local
->ops
->sta_table_notification(local_to_hw(local
),
249 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
250 local
->total_ps_buffered
--;
251 dev_kfree_skb_any(skb
);
253 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
254 dev_kfree_skb_any(skb
);
258 if (local
->ops
->set_key
) {
259 struct ieee80211_key_conf
*key
;
260 key
= ieee80211_key_data2conf(local
, sta
->key
);
262 local
->ops
->set_key(local_to_hw(local
),
264 sta
->addr
, key
, sta
->aid
);
268 } else if (sta
->key_idx_compression
!= HW_KEY_IDX_INVALID
) {
269 struct ieee80211_key_conf conf
;
270 memset(&conf
, 0, sizeof(conf
));
271 conf
.hw_key_idx
= sta
->key_idx_compression
;
273 conf
.flags
|= IEEE80211_KEY_FORCE_SW_ENCRYPT
;
274 local
->ops
->set_key(local_to_hw(local
), DISABLE_KEY
,
275 sta
->addr
, &conf
, sta
->aid
);
276 sta
->key_idx_compression
= HW_KEY_IDX_INVALID
;
279 #ifdef CONFIG_MAC80211_DEBUGFS
281 list_add(&sta
->list
, &local
->deleted_sta_list
);
282 queue_work(local
->hw
.workqueue
, &local
->sta_debugfs_add
);
285 finish_sta_info_free(local
, sta
);
289 static inline int sta_info_buffer_expired(struct ieee80211_local
*local
,
290 struct sta_info
*sta
,
293 struct ieee80211_tx_packet_data
*pkt_data
;
299 pkt_data
= (struct ieee80211_tx_packet_data
*) skb
->cb
;
301 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
302 timeout
= (sta
->listen_interval
* local
->hw
.conf
.beacon_int
* 32 /
304 if (timeout
< STA_TX_BUFFER_EXPIRE
)
305 timeout
= STA_TX_BUFFER_EXPIRE
;
306 return time_after(jiffies
, pkt_data
->jiffies
+ timeout
);
310 static void sta_info_cleanup_expire_buffered(struct ieee80211_local
*local
,
311 struct sta_info
*sta
)
316 if (skb_queue_empty(&sta
->ps_tx_buf
))
320 spin_lock_irqsave(&sta
->ps_tx_buf
.lock
, flags
);
321 skb
= skb_peek(&sta
->ps_tx_buf
);
322 if (sta_info_buffer_expired(local
, sta
, skb
)) {
323 skb
= __skb_dequeue(&sta
->ps_tx_buf
);
324 if (skb_queue_empty(&sta
->ps_tx_buf
))
325 sta
->flags
&= ~WLAN_STA_TIM
;
328 spin_unlock_irqrestore(&sta
->ps_tx_buf
.lock
, flags
);
331 local
->total_ps_buffered
--;
332 printk(KERN_DEBUG
"Buffered frame expired (STA "
333 MAC_FMT
")\n", MAC_ARG(sta
->addr
));
341 static void sta_info_cleanup(unsigned long data
)
343 struct ieee80211_local
*local
= (struct ieee80211_local
*) data
;
344 struct sta_info
*sta
;
346 spin_lock_bh(&local
->sta_lock
);
347 list_for_each_entry(sta
, &local
->sta_list
, list
) {
349 sta_info_cleanup_expire_buffered(local
, sta
);
352 spin_unlock_bh(&local
->sta_lock
);
354 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
355 add_timer(&local
->sta_cleanup
);
358 #ifdef CONFIG_MAC80211_DEBUGFS
359 static void sta_info_debugfs_add_task(struct work_struct
*work
)
361 struct ieee80211_local
*local
=
362 container_of(work
, struct ieee80211_local
, sta_debugfs_add
);
363 struct sta_info
*sta
, *tmp
;
366 spin_lock_bh(&local
->sta_lock
);
367 if (!list_empty(&local
->deleted_sta_list
)) {
368 sta
= list_entry(local
->deleted_sta_list
.next
,
369 struct sta_info
, list
);
370 list_del(local
->deleted_sta_list
.next
);
373 spin_unlock_bh(&local
->sta_lock
);
376 finish_sta_info_free(local
, sta
);
381 spin_lock_bh(&local
->sta_lock
);
382 list_for_each_entry(tmp
, &local
->sta_list
, list
) {
383 if (!tmp
->debugfs_registered
) {
389 spin_unlock_bh(&local
->sta_lock
);
394 sta
->debugfs_registered
= 1;
395 ieee80211_sta_debugfs_add(sta
);
396 rate_control_add_sta_debugfs(sta
);
402 void sta_info_init(struct ieee80211_local
*local
)
404 spin_lock_init(&local
->sta_lock
);
405 INIT_LIST_HEAD(&local
->sta_list
);
406 INIT_LIST_HEAD(&local
->deleted_sta_list
);
408 init_timer(&local
->sta_cleanup
);
409 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
410 local
->sta_cleanup
.data
= (unsigned long) local
;
411 local
->sta_cleanup
.function
= sta_info_cleanup
;
413 #ifdef CONFIG_MAC80211_DEBUGFS
414 INIT_WORK(&local
->sta_debugfs_add
, sta_info_debugfs_add_task
);
418 int sta_info_start(struct ieee80211_local
*local
)
420 add_timer(&local
->sta_cleanup
);
424 void sta_info_stop(struct ieee80211_local
*local
)
426 struct sta_info
*sta
, *tmp
;
428 del_timer(&local
->sta_cleanup
);
430 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
) {
431 /* sta_info_free must be called with 0 as the last
432 * parameter to ensure all debugfs sta entries are
433 * unregistered. We don't need locking at this
435 sta_info_free(sta
, 0);
439 void sta_info_remove_aid_ptr(struct sta_info
*sta
)
441 struct ieee80211_sub_if_data
*sdata
;
446 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
448 if (sdata
->local
->ops
->set_tim
)
449 sdata
->local
->ops
->set_tim(local_to_hw(sdata
->local
),
452 __bss_tim_clear(sdata
->bss
, sta
->aid
);
457 * sta_info_flush - flush matching STA entries from the STA table
458 * @local: local interface data
459 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
461 void sta_info_flush(struct ieee80211_local
*local
, struct net_device
*dev
)
463 struct sta_info
*sta
, *tmp
;
465 spin_lock_bh(&local
->sta_lock
);
466 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
)
467 if (!dev
|| dev
== sta
->dev
)
468 sta_info_free(sta
, 1);
469 spin_unlock_bh(&local
->sta_lock
);