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 struct sta_info
*sta_info_get(struct ieee80211_local
*local
, u8
*addr
)
62 read_lock_bh(&local
->sta_lock
);
63 sta
= local
->sta_hash
[STA_HASH(addr
)];
65 if (memcmp(sta
->addr
, addr
, ETH_ALEN
) == 0) {
71 read_unlock_bh(&local
->sta_lock
);
75 EXPORT_SYMBOL(sta_info_get
);
77 int sta_info_min_txrate_get(struct ieee80211_local
*local
)
80 struct ieee80211_hw_mode
*mode
;
81 int min_txrate
= 9999999;
84 read_lock_bh(&local
->sta_lock
);
85 mode
= local
->oper_hw_mode
;
86 for (i
= 0; i
< STA_HASH_SIZE
; i
++) {
87 sta
= local
->sta_hash
[i
];
89 if (sta
->txrate
< min_txrate
)
90 min_txrate
= sta
->txrate
;
94 read_unlock_bh(&local
->sta_lock
);
95 if (min_txrate
== 9999999)
98 return mode
->rates
[min_txrate
].rate
;
102 static void sta_info_release(struct kref
*kref
)
104 struct sta_info
*sta
= container_of(kref
, struct sta_info
, kref
);
105 struct ieee80211_local
*local
= sta
->local
;
109 /* free sta structure; it has already been removed from
110 * hash table etc. external structures. Make sure that all
111 * buffered frames are release (one might have been added
112 * after sta_info_free() was called). */
113 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
114 local
->total_ps_buffered
--;
115 dev_kfree_skb_any(skb
);
117 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
118 dev_kfree_skb_any(skb
);
120 for (i
= 0; i
< STA_TID_NUM
; i
++)
121 del_timer_sync(&sta
->ampdu_mlme
.tid_rx
[i
].session_timer
);
122 rate_control_free_sta(sta
->rate_ctrl
, sta
->rate_ctrl_priv
);
123 rate_control_put(sta
->rate_ctrl
);
128 void sta_info_put(struct sta_info
*sta
)
130 kref_put(&sta
->kref
, sta_info_release
);
132 EXPORT_SYMBOL(sta_info_put
);
135 struct sta_info
* sta_info_add(struct ieee80211_local
*local
,
136 struct net_device
*dev
, u8
*addr
, gfp_t gfp
)
138 struct sta_info
*sta
;
140 DECLARE_MAC_BUF(mac
);
142 sta
= kzalloc(sizeof(*sta
), gfp
);
146 kref_init(&sta
->kref
);
148 sta
->rate_ctrl
= rate_control_get(local
->rate_ctrl
);
149 sta
->rate_ctrl_priv
= rate_control_alloc_sta(sta
->rate_ctrl
, gfp
);
150 if (!sta
->rate_ctrl_priv
) {
151 rate_control_put(sta
->rate_ctrl
);
156 memcpy(sta
->addr
, addr
, ETH_ALEN
);
159 spin_lock_init(&sta
->ampdu_mlme
.ampdu_rx
);
160 for (i
= 0; i
< STA_TID_NUM
; i
++) {
161 /* timer_to_tid must be initialized with identity mapping to
162 * enable session_timer's data differentiation. refer to
163 * sta_rx_agg_session_timer_expired for useage */
164 sta
->timer_to_tid
[i
] = i
;
166 sta
->ampdu_mlme
.tid_rx
[i
].session_timer
.function
=
167 sta_rx_agg_session_timer_expired
;
168 sta
->ampdu_mlme
.tid_rx
[i
].session_timer
.data
=
169 (unsigned long)&sta
->timer_to_tid
[i
];
170 init_timer(&sta
->ampdu_mlme
.tid_rx
[i
].session_timer
);
172 skb_queue_head_init(&sta
->ps_tx_buf
);
173 skb_queue_head_init(&sta
->tx_filtered
);
174 __sta_info_get(sta
); /* sta used by caller, decremented by
176 write_lock_bh(&local
->sta_lock
);
177 list_add(&sta
->list
, &local
->sta_list
);
179 sta_info_hash_add(local
, sta
);
180 if (local
->ops
->sta_notify
)
181 local
->ops
->sta_notify(local_to_hw(local
), dev
->ifindex
,
182 STA_NOTIFY_ADD
, addr
);
183 write_unlock_bh(&local
->sta_lock
);
185 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
186 printk(KERN_DEBUG
"%s: Added STA %s\n",
187 wiphy_name(local
->hw
.wiphy
), print_mac(mac
, addr
));
188 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
190 #ifdef CONFIG_MAC80211_DEBUGFS
191 /* debugfs entry adding might sleep, so schedule process
192 * context task for adding entry for STAs that do not yet
194 queue_work(local
->hw
.workqueue
, &local
->sta_debugfs_add
);
200 /* Caller must hold local->sta_lock */
201 void sta_info_remove(struct sta_info
*sta
)
203 struct ieee80211_local
*local
= sta
->local
;
204 struct ieee80211_sub_if_data
*sdata
;
206 /* don't do anything if we've been removed already */
207 if (sta_info_hash_del(local
, sta
))
210 list_del(&sta
->list
);
211 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
212 if (sta
->flags
& WLAN_STA_PS
) {
213 sta
->flags
&= ~WLAN_STA_PS
;
215 atomic_dec(&sdata
->bss
->num_sta_ps
);
218 sta_info_remove_aid_ptr(sta
);
222 void sta_info_free(struct sta_info
*sta
)
225 struct ieee80211_local
*local
= sta
->local
;
226 DECLARE_MAC_BUF(mac
);
230 write_lock_bh(&local
->sta_lock
);
231 sta_info_remove(sta
);
232 write_unlock_bh(&local
->sta_lock
);
234 while ((skb
= skb_dequeue(&sta
->ps_tx_buf
)) != NULL
) {
235 local
->total_ps_buffered
--;
238 while ((skb
= skb_dequeue(&sta
->tx_filtered
)) != NULL
) {
242 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
243 printk(KERN_DEBUG
"%s: Removed STA %s\n",
244 wiphy_name(local
->hw
.wiphy
), print_mac(mac
, sta
->addr
));
245 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
247 ieee80211_key_free(sta
->key
);
250 if (local
->ops
->sta_notify
)
251 local
->ops
->sta_notify(local_to_hw(local
), sta
->dev
->ifindex
,
252 STA_NOTIFY_REMOVE
, sta
->addr
);
254 rate_control_remove_sta_debugfs(sta
);
255 ieee80211_sta_debugfs_remove(sta
);
261 static inline int sta_info_buffer_expired(struct ieee80211_local
*local
,
262 struct sta_info
*sta
,
265 struct ieee80211_tx_packet_data
*pkt_data
;
271 pkt_data
= (struct ieee80211_tx_packet_data
*) skb
->cb
;
273 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
274 timeout
= (sta
->listen_interval
* local
->hw
.conf
.beacon_int
* 32 /
276 if (timeout
< STA_TX_BUFFER_EXPIRE
)
277 timeout
= STA_TX_BUFFER_EXPIRE
;
278 return time_after(jiffies
, pkt_data
->jiffies
+ timeout
);
282 static void sta_info_cleanup_expire_buffered(struct ieee80211_local
*local
,
283 struct sta_info
*sta
)
287 DECLARE_MAC_BUF(mac
);
289 if (skb_queue_empty(&sta
->ps_tx_buf
))
293 spin_lock_irqsave(&sta
->ps_tx_buf
.lock
, flags
);
294 skb
= skb_peek(&sta
->ps_tx_buf
);
295 if (sta_info_buffer_expired(local
, sta
, skb
)) {
296 skb
= __skb_dequeue(&sta
->ps_tx_buf
);
297 if (skb_queue_empty(&sta
->ps_tx_buf
))
298 sta
->flags
&= ~WLAN_STA_TIM
;
301 spin_unlock_irqrestore(&sta
->ps_tx_buf
.lock
, flags
);
304 local
->total_ps_buffered
--;
305 printk(KERN_DEBUG
"Buffered frame expired (STA "
306 "%s)\n", print_mac(mac
, sta
->addr
));
314 static void sta_info_cleanup(unsigned long data
)
316 struct ieee80211_local
*local
= (struct ieee80211_local
*) data
;
317 struct sta_info
*sta
;
319 read_lock_bh(&local
->sta_lock
);
320 list_for_each_entry(sta
, &local
->sta_list
, list
) {
322 sta_info_cleanup_expire_buffered(local
, sta
);
325 read_unlock_bh(&local
->sta_lock
);
327 local
->sta_cleanup
.expires
=
328 round_jiffies(jiffies
+ STA_INFO_CLEANUP_INTERVAL
);
329 add_timer(&local
->sta_cleanup
);
332 #ifdef CONFIG_MAC80211_DEBUGFS
333 static void sta_info_debugfs_add_task(struct work_struct
*work
)
335 struct ieee80211_local
*local
=
336 container_of(work
, struct ieee80211_local
, sta_debugfs_add
);
337 struct sta_info
*sta
, *tmp
;
341 read_lock_bh(&local
->sta_lock
);
342 list_for_each_entry(tmp
, &local
->sta_list
, list
) {
343 if (!tmp
->debugfs
.dir
) {
349 read_unlock_bh(&local
->sta_lock
);
354 ieee80211_sta_debugfs_add(sta
);
355 rate_control_add_sta_debugfs(sta
);
361 void sta_info_init(struct ieee80211_local
*local
)
363 rwlock_init(&local
->sta_lock
);
364 INIT_LIST_HEAD(&local
->sta_list
);
366 setup_timer(&local
->sta_cleanup
, sta_info_cleanup
,
367 (unsigned long)local
);
368 local
->sta_cleanup
.expires
=
369 round_jiffies(jiffies
+ STA_INFO_CLEANUP_INTERVAL
);
371 #ifdef CONFIG_MAC80211_DEBUGFS
372 INIT_WORK(&local
->sta_debugfs_add
, sta_info_debugfs_add_task
);
376 int sta_info_start(struct ieee80211_local
*local
)
378 add_timer(&local
->sta_cleanup
);
382 void sta_info_stop(struct ieee80211_local
*local
)
384 del_timer(&local
->sta_cleanup
);
385 sta_info_flush(local
, NULL
);
388 void sta_info_remove_aid_ptr(struct sta_info
*sta
)
390 struct ieee80211_sub_if_data
*sdata
;
395 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
397 if (sdata
->local
->ops
->set_tim
)
398 sdata
->local
->ops
->set_tim(local_to_hw(sdata
->local
),
401 __bss_tim_clear(sdata
->bss
, sta
->aid
);
406 * sta_info_flush - flush matching STA entries from the STA table
407 * @local: local interface data
408 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
410 void sta_info_flush(struct ieee80211_local
*local
, struct net_device
*dev
)
412 struct sta_info
*sta
, *tmp
;
415 write_lock_bh(&local
->sta_lock
);
416 list_for_each_entry_safe(sta
, tmp
, &local
->sta_list
, list
)
417 if (!dev
|| dev
== sta
->dev
) {
419 sta_info_remove(sta
);
420 list_add_tail(&sta
->list
, &tmp_list
);
422 write_unlock_bh(&local
->sta_lock
);
424 list_for_each_entry_safe(sta
, tmp
, &tmp_list
, list
) {