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
,
36 struct sta_info
*sta
, int dls
)
40 s
= local
->sta_hash
[STA_HASH(sta
->addr
)];
43 if (memcmp(s
->addr
, sta
->addr
, ETH_ALEN
) == 0) {
44 if (dls
&& !s
->dls_sta
)
46 local
->sta_hash
[STA_HASH(sta
->addr
)] = s
->hnext
;
50 while (s
->hnext
&& memcmp(s
->hnext
->addr
, sta
->addr
, ETH_ALEN
) != 0)
53 if (dls
&& !s
->hnext
->dls_sta
)
55 s
->hnext
= s
->hnext
->hnext
;
57 printk(KERN_ERR
"%s: could not remove STA " MAC_FMT
" from "
58 "hash table\n", local
->mdev
->name
, MAC_ARG(sta
->addr
));
61 static inline void __sta_info_get(struct sta_info
*sta
)
66 struct sta_info
*sta_info_get(struct ieee80211_local
*local
, u8
*addr
)
70 spin_lock_bh(&local
->sta_lock
);
71 sta
= local
->sta_hash
[STA_HASH(addr
)];
73 if (memcmp(sta
->addr
, addr
, ETH_ALEN
) == 0) {
79 spin_unlock_bh(&local
->sta_lock
);
83 EXPORT_SYMBOL(sta_info_get
);
85 struct sta_info
*dls_info_get(struct ieee80211_local
*local
, u8
*addr
)
89 spin_lock_bh(&local
->sta_lock
);
90 sta
= local
->sta_hash
[STA_HASH(addr
)];
92 if (memcmp(sta
->addr
, addr
, ETH_ALEN
) == 0) {
102 spin_unlock_bh(&local
->sta_lock
);
107 int sta_info_min_txrate_get(struct ieee80211_local
*local
)
109 struct sta_info
*sta
;
110 struct ieee80211_hw_mode
*mode
;
111 int min_txrate
= 9999999;
114 spin_lock_bh(&local
->sta_lock
);
115 mode
= local
->oper_hw_mode
;
116 for (i
= 0; i
< STA_HASH_SIZE
; i
++) {
117 sta
= local
->sta_hash
[i
];
119 if (sta
->txrate
< min_txrate
)
120 min_txrate
= sta
->txrate
;
124 spin_unlock_bh(&local
->sta_lock
);
125 if (min_txrate
== 9999999)
128 return mode
->rates
[min_txrate
].rate
;
132 static void sta_info_release(struct kref
*kref
)
134 struct sta_info
*sta
= container_of(kref
, struct sta_info
, kref
);
135 struct ieee80211_local
*local
= sta
->local
;
138 /* free sta structure; it has already been removed from
139 * hash table etc. external structures. Make sure that all
140 * buffered frames are release (one might have been added
141 * after sta_info_free() was called). */
142 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
143 local
->total_ps_buffered
--;
144 dev_kfree_skb_any(skb
);
146 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
147 dev_kfree_skb_any(skb
);
149 rate_control_free_sta(sta
->rate_ctrl
, sta
->rate_ctrl_priv
);
150 rate_control_put(sta
->rate_ctrl
);
152 ieee80211_debugfs_key_sta_del(sta
->key
, sta
);
157 void sta_info_put(struct sta_info
*sta
)
159 kref_put(&sta
->kref
, sta_info_release
);
161 EXPORT_SYMBOL(sta_info_put
);
164 struct sta_info
* sta_info_add(struct ieee80211_local
*local
,
165 struct net_device
*dev
, u8
*addr
, gfp_t gfp
)
167 struct sta_info
*sta
;
169 sta
= kzalloc(sizeof(*sta
), gfp
);
173 kref_init(&sta
->kref
);
175 sta
->rate_ctrl
= rate_control_get(local
->rate_ctrl
);
176 sta
->rate_ctrl_priv
= rate_control_alloc_sta(sta
->rate_ctrl
, gfp
);
177 if (!sta
->rate_ctrl_priv
) {
178 rate_control_put(sta
->rate_ctrl
);
179 kref_put(&sta
->kref
, sta_info_release
);
184 memcpy(sta
->addr
, addr
, ETH_ALEN
);
187 skb_queue_head_init(&sta
->ps_tx_buf
);
188 skb_queue_head_init(&sta
->tx_filtered
);
189 __sta_info_get(sta
); /* sta used by caller, decremented by
191 spin_lock_bh(&local
->sta_lock
);
192 list_add(&sta
->list
, &local
->sta_list
);
194 sta_info_hash_add(local
, sta
);
195 spin_unlock_bh(&local
->sta_lock
);
196 if (local
->ops
->sta_table_notification
)
197 local
->ops
->sta_table_notification(local_to_hw(local
),
199 sta
->key_idx_compression
= HW_KEY_IDX_INVALID
;
201 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
202 printk(KERN_DEBUG
"%s: Added STA " MAC_FMT
"\n",
203 local
->mdev
->name
, MAC_ARG(addr
));
204 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
206 #ifdef CONFIG_MAC80211_DEBUGFS
207 if (!in_interrupt()) {
208 sta
->debugfs_registered
= 1;
209 ieee80211_sta_debugfs_add(sta
);
210 rate_control_add_sta_debugfs(sta
);
212 /* debugfs entry adding might sleep, so schedule process
213 * context task for adding entry for STAs that do not yet
215 queue_work(local
->hw
.workqueue
, &local
->sta_debugfs_add
);
222 static void finish_sta_info_free(struct ieee80211_local
*local
,
223 struct sta_info
*sta
)
225 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
226 printk(KERN_DEBUG
"%s: Removed STA " MAC_FMT
"\n",
227 local
->mdev
->name
, MAC_ARG(sta
->addr
));
228 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
231 ieee80211_debugfs_key_remove(sta
->key
);
232 ieee80211_key_free(sta
->key
);
236 rate_control_remove_sta_debugfs(sta
);
237 ieee80211_sta_debugfs_remove(sta
);
242 static void sta_info_remove(struct sta_info
*sta
)
244 struct ieee80211_local
*local
= sta
->local
;
245 struct ieee80211_sub_if_data
*sdata
;
247 sta_info_hash_del(local
, sta
, 0);
248 list_del(&sta
->list
);
249 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
250 if (sta
->flags
& WLAN_STA_PS
) {
251 sta
->flags
&= ~WLAN_STA_PS
;
253 atomic_dec(&sdata
->bss
->num_sta_ps
);
256 sta_info_remove_aid_ptr(sta
);
259 void sta_info_free(struct sta_info
*sta
, int locked
)
262 struct ieee80211_local
*local
= sta
->local
;
265 spin_lock_bh(&local
->sta_lock
);
266 sta_info_remove(sta
);
267 spin_unlock_bh(&local
->sta_lock
);
269 sta_info_remove(sta
);
271 if (local
->ops
->sta_table_notification
)
272 local
->ops
->sta_table_notification(local_to_hw(local
),
275 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
276 local
->total_ps_buffered
--;
277 dev_kfree_skb_any(skb
);
279 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
280 dev_kfree_skb_any(skb
);
284 if (local
->ops
->set_key
) {
285 struct ieee80211_key_conf
*key
;
286 key
= ieee80211_key_data2conf(local
, sta
->key
);
288 local
->ops
->set_key(local_to_hw(local
),
290 sta
->addr
, key
, sta
->aid
);
294 } else if (sta
->key_idx_compression
!= HW_KEY_IDX_INVALID
) {
295 struct ieee80211_key_conf conf
;
296 memset(&conf
, 0, sizeof(conf
));
297 conf
.hw_key_idx
= sta
->key_idx_compression
;
299 conf
.flags
|= IEEE80211_KEY_FORCE_SW_ENCRYPT
;
300 local
->ops
->set_key(local_to_hw(local
), DISABLE_KEY
,
301 sta
->addr
, &conf
, sta
->aid
);
302 sta
->key_idx_compression
= HW_KEY_IDX_INVALID
;
305 #ifdef CONFIG_MAC80211_DEBUGFS
307 list_add(&sta
->list
, &local
->deleted_sta_list
);
308 queue_work(local
->hw
.workqueue
, &local
->sta_debugfs_add
);
311 finish_sta_info_free(local
, sta
);
315 static inline int sta_info_buffer_expired(struct ieee80211_local
*local
,
316 struct sta_info
*sta
,
319 struct ieee80211_tx_packet_data
*pkt_data
;
325 pkt_data
= (struct ieee80211_tx_packet_data
*) skb
->cb
;
327 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
328 timeout
= (sta
->listen_interval
* local
->hw
.conf
.beacon_int
* 32 /
330 if (timeout
< STA_TX_BUFFER_EXPIRE
)
331 timeout
= STA_TX_BUFFER_EXPIRE
;
332 return time_after(jiffies
, pkt_data
->jiffies
+ timeout
);
336 static void sta_info_cleanup_expire_buffered(struct ieee80211_local
*local
,
337 struct sta_info
*sta
)
342 if (skb_queue_empty(&sta
->ps_tx_buf
))
346 spin_lock_irqsave(&sta
->ps_tx_buf
.lock
, flags
);
347 skb
= skb_peek(&sta
->ps_tx_buf
);
348 if (sta_info_buffer_expired(local
, sta
, skb
)) {
349 skb
= __skb_dequeue(&sta
->ps_tx_buf
);
350 if (skb_queue_empty(&sta
->ps_tx_buf
))
351 sta
->flags
&= ~WLAN_STA_TIM
;
354 spin_unlock_irqrestore(&sta
->ps_tx_buf
.lock
, flags
);
357 local
->total_ps_buffered
--;
358 printk(KERN_DEBUG
"Buffered frame expired (STA "
359 MAC_FMT
")\n", MAC_ARG(sta
->addr
));
367 static void sta_info_cleanup(unsigned long data
)
369 struct ieee80211_local
*local
= (struct ieee80211_local
*) data
;
370 struct sta_info
*sta
;
372 spin_lock_bh(&local
->sta_lock
);
373 list_for_each_entry(sta
, &local
->sta_list
, list
) {
375 sta_info_cleanup_expire_buffered(local
, sta
);
378 spin_unlock_bh(&local
->sta_lock
);
380 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
381 add_timer(&local
->sta_cleanup
);
384 #ifdef CONFIG_MAC80211_DEBUGFS
385 static void sta_info_debugfs_add_task(struct work_struct
*work
)
387 struct ieee80211_local
*local
=
388 container_of(work
, struct ieee80211_local
, sta_debugfs_add
);
389 struct sta_info
*sta
, *tmp
;
392 spin_lock_bh(&local
->sta_lock
);
393 if (!list_empty(&local
->deleted_sta_list
)) {
394 sta
= list_entry(local
->deleted_sta_list
.next
,
395 struct sta_info
, list
);
396 list_del(local
->deleted_sta_list
.next
);
399 spin_unlock_bh(&local
->sta_lock
);
402 finish_sta_info_free(local
, sta
);
407 spin_lock_bh(&local
->sta_lock
);
408 list_for_each_entry(tmp
, &local
->sta_list
, list
) {
409 if (!tmp
->debugfs_registered
) {
415 spin_unlock_bh(&local
->sta_lock
);
420 sta
->debugfs_registered
= 1;
421 ieee80211_sta_debugfs_add(sta
);
422 rate_control_add_sta_debugfs(sta
);
428 void sta_info_init(struct ieee80211_local
*local
)
430 spin_lock_init(&local
->sta_lock
);
431 INIT_LIST_HEAD(&local
->sta_list
);
432 INIT_LIST_HEAD(&local
->deleted_sta_list
);
434 init_timer(&local
->sta_cleanup
);
435 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
436 local
->sta_cleanup
.data
= (unsigned long) local
;
437 local
->sta_cleanup
.function
= sta_info_cleanup
;
439 #ifdef CONFIG_MAC80211_DEBUGFS
440 INIT_WORK(&local
->sta_debugfs_add
, sta_info_debugfs_add_task
);
444 int sta_info_start(struct ieee80211_local
*local
)
446 add_timer(&local
->sta_cleanup
);
450 void sta_info_stop(struct ieee80211_local
*local
)
452 struct sta_info
*sta
, *tmp
;
454 del_timer(&local
->sta_cleanup
);
456 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
) {
457 /* sta_info_free must be called with 0 as the last
458 * parameter to ensure all debugfs sta entries are
459 * unregistered. We don't need locking at this
461 sta_info_free(sta
, 0);
465 void sta_info_remove_aid_ptr(struct sta_info
*sta
)
467 struct ieee80211_sub_if_data
*sdata
;
472 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
474 if (sdata
->local
->ops
->set_tim
)
475 sdata
->local
->ops
->set_tim(local_to_hw(sdata
->local
),
478 __bss_tim_clear(sdata
->bss
, sta
->aid
);
483 * sta_info_flush - flush matching STA entries from the STA table
484 * @local: local interface data
485 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
487 void sta_info_flush(struct ieee80211_local
*local
, struct net_device
*dev
)
489 struct sta_info
*sta
, *tmp
;
491 spin_lock_bh(&local
->sta_lock
);
492 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
)
493 if (!dev
|| dev
== sta
->dev
)
494 sta_info_free(sta
, 1);
495 spin_unlock_bh(&local
->sta_lock
);