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"
23 /* Caller must hold local->sta_lock */
24 static void sta_info_hash_add(struct ieee80211_local
*local
,
27 sta
->hnext
= local
->sta_hash
[STA_HASH(sta
->addr
)];
28 local
->sta_hash
[STA_HASH(sta
->addr
)] = sta
;
32 /* Caller must hold local->sta_lock */
33 static void sta_info_hash_del(struct ieee80211_local
*local
,
38 s
= local
->sta_hash
[STA_HASH(sta
->addr
)];
41 if (memcmp(s
->addr
, sta
->addr
, ETH_ALEN
) == 0) {
42 local
->sta_hash
[STA_HASH(sta
->addr
)] = s
->hnext
;
46 while (s
->hnext
&& memcmp(s
->hnext
->addr
, sta
->addr
, ETH_ALEN
) != 0)
49 s
->hnext
= s
->hnext
->hnext
;
51 printk(KERN_ERR
"%s: could not remove STA " MAC_FMT
" from "
52 "hash table\n", local
->mdev
->name
, MAC_ARG(sta
->addr
));
55 static inline void __sta_info_get(struct sta_info
*sta
)
60 struct sta_info
*sta_info_get(struct ieee80211_local
*local
, u8
*addr
)
64 spin_lock_bh(&local
->sta_lock
);
65 sta
= local
->sta_hash
[STA_HASH(addr
)];
67 if (memcmp(sta
->addr
, addr
, ETH_ALEN
) == 0) {
73 spin_unlock_bh(&local
->sta_lock
);
77 EXPORT_SYMBOL(sta_info_get
);
79 int sta_info_min_txrate_get(struct ieee80211_local
*local
)
82 struct ieee80211_hw_mode
*mode
;
83 int min_txrate
= 9999999;
86 spin_lock_bh(&local
->sta_lock
);
87 mode
= local
->oper_hw_mode
;
88 for (i
= 0; i
< STA_HASH_SIZE
; i
++) {
89 sta
= local
->sta_hash
[i
];
91 if (sta
->txrate
< min_txrate
)
92 min_txrate
= sta
->txrate
;
96 spin_unlock_bh(&local
->sta_lock
);
97 if (min_txrate
== 9999999)
100 return mode
->rates
[min_txrate
].rate
;
104 static void sta_info_release(struct kref
*kref
)
106 struct sta_info
*sta
= container_of(kref
, struct sta_info
, kref
);
107 struct ieee80211_local
*local
= sta
->local
;
110 /* free sta structure; it has already been removed from
111 * hash table etc. external structures. Make sure that all
112 * buffered frames are release (one might have been added
113 * after sta_info_free() was called). */
114 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
115 local
->total_ps_buffered
--;
116 dev_kfree_skb_any(skb
);
118 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
119 dev_kfree_skb_any(skb
);
121 rate_control_free_sta(sta
->rate_ctrl
, sta
->rate_ctrl_priv
);
122 rate_control_put(sta
->rate_ctrl
);
127 void sta_info_put(struct sta_info
*sta
)
129 kref_put(&sta
->kref
, sta_info_release
);
131 EXPORT_SYMBOL(sta_info_put
);
134 struct sta_info
* sta_info_add(struct ieee80211_local
*local
,
135 struct net_device
*dev
, u8
*addr
, gfp_t gfp
)
137 struct sta_info
*sta
;
139 sta
= kzalloc(sizeof(*sta
), gfp
);
143 kref_init(&sta
->kref
);
145 sta
->rate_ctrl
= rate_control_get(local
->rate_ctrl
);
146 sta
->rate_ctrl_priv
= rate_control_alloc_sta(sta
->rate_ctrl
, gfp
);
147 if (!sta
->rate_ctrl_priv
) {
148 rate_control_put(sta
->rate_ctrl
);
149 kref_put(&sta
->kref
, sta_info_release
);
154 memcpy(sta
->addr
, addr
, ETH_ALEN
);
157 skb_queue_head_init(&sta
->ps_tx_buf
);
158 skb_queue_head_init(&sta
->tx_filtered
);
159 __sta_info_get(sta
); /* sta used by caller, decremented by
161 spin_lock_bh(&local
->sta_lock
);
162 list_add(&sta
->list
, &local
->sta_list
);
164 sta_info_hash_add(local
, sta
);
165 spin_unlock_bh(&local
->sta_lock
);
166 if (local
->ops
->sta_table_notification
)
167 local
->ops
->sta_table_notification(local_to_hw(local
),
169 sta
->key_idx_compression
= HW_KEY_IDX_INVALID
;
171 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
172 printk(KERN_DEBUG
"%s: Added STA " MAC_FMT
"\n",
173 local
->mdev
->name
, MAC_ARG(addr
));
174 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
179 static void sta_info_remove(struct sta_info
*sta
)
181 struct ieee80211_local
*local
= sta
->local
;
182 struct ieee80211_sub_if_data
*sdata
;
184 sta_info_hash_del(local
, sta
);
185 list_del(&sta
->list
);
186 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
187 if (sta
->flags
& WLAN_STA_PS
) {
188 sta
->flags
&= ~WLAN_STA_PS
;
190 atomic_dec(&sdata
->bss
->num_sta_ps
);
193 sta_info_remove_aid_ptr(sta
);
196 void sta_info_free(struct sta_info
*sta
, int locked
)
199 struct ieee80211_local
*local
= sta
->local
;
202 spin_lock_bh(&local
->sta_lock
);
203 sta_info_remove(sta
);
204 spin_unlock_bh(&local
->sta_lock
);
206 sta_info_remove(sta
);
208 if (local
->ops
->sta_table_notification
)
209 local
->ops
->sta_table_notification(local_to_hw(local
),
212 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
213 local
->total_ps_buffered
--;
214 dev_kfree_skb_any(skb
);
216 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
217 dev_kfree_skb_any(skb
);
221 if (local
->ops
->set_key
) {
222 struct ieee80211_key_conf
*key
;
223 key
= ieee80211_key_data2conf(local
, sta
->key
);
225 local
->ops
->set_key(local_to_hw(local
),
227 sta
->addr
, key
, sta
->aid
);
231 } else if (sta
->key_idx_compression
!= HW_KEY_IDX_INVALID
) {
232 struct ieee80211_key_conf conf
;
233 memset(&conf
, 0, sizeof(conf
));
234 conf
.hw_key_idx
= sta
->key_idx_compression
;
236 conf
.flags
|= IEEE80211_KEY_FORCE_SW_ENCRYPT
;
237 local
->ops
->set_key(local_to_hw(local
), DISABLE_KEY
,
238 sta
->addr
, &conf
, sta
->aid
);
239 sta
->key_idx_compression
= HW_KEY_IDX_INVALID
;
242 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
243 printk(KERN_DEBUG
"%s: Removed STA " MAC_FMT
"\n",
244 local
->mdev
->name
, MAC_ARG(sta
->addr
));
245 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
248 ieee80211_key_free(sta
->key
);
256 static inline int sta_info_buffer_expired(struct ieee80211_local
*local
,
257 struct sta_info
*sta
,
260 struct ieee80211_tx_packet_data
*pkt_data
;
266 pkt_data
= (struct ieee80211_tx_packet_data
*) skb
->cb
;
268 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
269 timeout
= (sta
->listen_interval
* local
->hw
.conf
.beacon_int
* 32 /
271 if (timeout
< STA_TX_BUFFER_EXPIRE
)
272 timeout
= STA_TX_BUFFER_EXPIRE
;
273 return time_after(jiffies
, pkt_data
->jiffies
+ timeout
);
277 static void sta_info_cleanup_expire_buffered(struct ieee80211_local
*local
,
278 struct sta_info
*sta
)
283 if (skb_queue_empty(&sta
->ps_tx_buf
))
287 spin_lock_irqsave(&sta
->ps_tx_buf
.lock
, flags
);
288 skb
= skb_peek(&sta
->ps_tx_buf
);
289 if (sta_info_buffer_expired(local
, sta
, skb
)) {
290 skb
= __skb_dequeue(&sta
->ps_tx_buf
);
291 if (skb_queue_empty(&sta
->ps_tx_buf
))
292 sta
->flags
&= ~WLAN_STA_TIM
;
295 spin_unlock_irqrestore(&sta
->ps_tx_buf
.lock
, flags
);
298 local
->total_ps_buffered
--;
299 printk(KERN_DEBUG
"Buffered frame expired (STA "
300 MAC_FMT
")\n", MAC_ARG(sta
->addr
));
308 static void sta_info_cleanup(unsigned long data
)
310 struct ieee80211_local
*local
= (struct ieee80211_local
*) data
;
311 struct sta_info
*sta
;
313 spin_lock_bh(&local
->sta_lock
);
314 list_for_each_entry(sta
, &local
->sta_list
, list
) {
316 sta_info_cleanup_expire_buffered(local
, sta
);
319 spin_unlock_bh(&local
->sta_lock
);
321 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
322 add_timer(&local
->sta_cleanup
);
325 void sta_info_init(struct ieee80211_local
*local
)
327 spin_lock_init(&local
->sta_lock
);
328 INIT_LIST_HEAD(&local
->sta_list
);
329 INIT_LIST_HEAD(&local
->deleted_sta_list
);
331 init_timer(&local
->sta_cleanup
);
332 local
->sta_cleanup
.expires
= jiffies
+ STA_INFO_CLEANUP_INTERVAL
;
333 local
->sta_cleanup
.data
= (unsigned long) local
;
334 local
->sta_cleanup
.function
= sta_info_cleanup
;
337 int sta_info_start(struct ieee80211_local
*local
)
339 add_timer(&local
->sta_cleanup
);
343 void sta_info_stop(struct ieee80211_local
*local
)
345 struct sta_info
*sta
, *tmp
;
347 del_timer(&local
->sta_cleanup
);
349 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
) {
350 /* We don't need locking at this point. */
351 sta_info_free(sta
, 0);
355 void sta_info_remove_aid_ptr(struct sta_info
*sta
)
357 struct ieee80211_sub_if_data
*sdata
;
362 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
364 if (sdata
->local
->ops
->set_tim
)
365 sdata
->local
->ops
->set_tim(local_to_hw(sdata
->local
),
368 __bss_tim_clear(sdata
->bss
, sta
->aid
);
373 * sta_info_flush - flush matching STA entries from the STA table
374 * @local: local interface data
375 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
377 void sta_info_flush(struct ieee80211_local
*local
, struct net_device
*dev
)
379 struct sta_info
*sta
, *tmp
;
381 spin_lock_bh(&local
->sta_lock
);
382 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
)
383 if (!dev
|| dev
== sta
->dev
)
384 sta_info_free(sta
, 1);
385 spin_unlock_bh(&local
->sta_lock
);