blob: fba2d79e4d2b6dd934231bb86fa9fff962a532f5 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
4 *
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.
8 */
9
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
18#include <net/mac80211.h>
19#include "ieee80211_i.h"
20#include "ieee80211_rate.h"
21#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070022#include "debugfs_key.h"
23#include "debugfs_sta.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070024
25/* Caller must hold local->sta_lock */
26static void sta_info_hash_add(struct ieee80211_local *local,
27 struct sta_info *sta)
28{
29 sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
30 local->sta_hash[STA_HASH(sta->addr)] = sta;
31}
32
33
34/* Caller must hold local->sta_lock */
Michael Wube8755e2007-07-27 15:43:23 +020035static int sta_info_hash_del(struct ieee80211_local *local,
36 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070037{
38 struct sta_info *s;
39
40 s = local->sta_hash[STA_HASH(sta->addr)];
41 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020042 return -ENOENT;
43 if (s == sta) {
Jiri Bencf0706e82007-05-05 11:45:53 -070044 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020045 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070046 }
47
Michael Wube8755e2007-07-27 15:43:23 +020048 while (s->hnext && s->hnext != sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070049 s = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020050 if (s->hnext) {
51 s->hnext = sta->hnext;
52 return 0;
53 }
Jiri Bencf0706e82007-05-05 11:45:53 -070054
Michael Wube8755e2007-07-27 15:43:23 +020055 return -ENOENT;
Jiri Bencf0706e82007-05-05 11:45:53 -070056}
57
58struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
59{
60 struct sta_info *sta;
61
Michael Wube8755e2007-07-27 15:43:23 +020062 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070063 sta = local->sta_hash[STA_HASH(addr)];
64 while (sta) {
65 if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
66 __sta_info_get(sta);
67 break;
68 }
69 sta = sta->hnext;
70 }
Michael Wube8755e2007-07-27 15:43:23 +020071 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070072
73 return sta;
74}
75EXPORT_SYMBOL(sta_info_get);
76
77int sta_info_min_txrate_get(struct ieee80211_local *local)
78{
79 struct sta_info *sta;
80 struct ieee80211_hw_mode *mode;
81 int min_txrate = 9999999;
82 int i;
83
Michael Wube8755e2007-07-27 15:43:23 +020084 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070085 mode = local->oper_hw_mode;
86 for (i = 0; i < STA_HASH_SIZE; i++) {
87 sta = local->sta_hash[i];
88 while (sta) {
89 if (sta->txrate < min_txrate)
90 min_txrate = sta->txrate;
91 sta = sta->hnext;
92 }
93 }
Michael Wube8755e2007-07-27 15:43:23 +020094 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070095 if (min_txrate == 9999999)
96 min_txrate = 0;
97
98 return mode->rates[min_txrate].rate;
99}
100
101
102static void sta_info_release(struct kref *kref)
103{
104 struct sta_info *sta = container_of(kref, struct sta_info, kref);
105 struct ieee80211_local *local = sta->local;
106 struct sk_buff *skb;
107
108 /* free sta structure; it has already been removed from
109 * hash table etc. external structures. Make sure that all
110 * buffered frames are release (one might have been added
111 * after sta_info_free() was called). */
112 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
113 local->total_ps_buffered--;
114 dev_kfree_skb_any(skb);
115 }
116 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
117 dev_kfree_skb_any(skb);
118 }
119 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
120 rate_control_put(sta->rate_ctrl);
Jiri Bence9f207f2007-05-05 11:46:38 -0700121 if (sta->key)
122 ieee80211_debugfs_key_sta_del(sta->key, sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700123 kfree(sta);
124}
125
126
127void sta_info_put(struct sta_info *sta)
128{
129 kref_put(&sta->kref, sta_info_release);
130}
131EXPORT_SYMBOL(sta_info_put);
132
133
134struct sta_info * sta_info_add(struct ieee80211_local *local,
135 struct net_device *dev, u8 *addr, gfp_t gfp)
136{
137 struct sta_info *sta;
138
139 sta = kzalloc(sizeof(*sta), gfp);
140 if (!sta)
141 return NULL;
142
143 kref_init(&sta->kref);
144
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);
Jiri Bencf0706e82007-05-05 11:45:53 -0700149 kfree(sta);
150 return NULL;
151 }
152
153 memcpy(sta->addr, addr, ETH_ALEN);
154 sta->local = local;
155 sta->dev = dev;
156 skb_queue_head_init(&sta->ps_tx_buf);
157 skb_queue_head_init(&sta->tx_filtered);
158 __sta_info_get(sta); /* sta used by caller, decremented by
159 * sta_info_put() */
Michael Wube8755e2007-07-27 15:43:23 +0200160 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700161 list_add(&sta->list, &local->sta_list);
162 local->num_sta++;
163 sta_info_hash_add(local, sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700164 if (local->ops->sta_table_notification)
165 local->ops->sta_table_notification(local_to_hw(local),
166 local->num_sta);
Michael Wube8755e2007-07-27 15:43:23 +0200167 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700168
169#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
170 printk(KERN_DEBUG "%s: Added STA " MAC_FMT "\n",
171 local->mdev->name, MAC_ARG(addr));
172#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
173
Jiri Bence9f207f2007-05-05 11:46:38 -0700174#ifdef CONFIG_MAC80211_DEBUGFS
Michael Wube8755e2007-07-27 15:43:23 +0200175 /* debugfs entry adding might sleep, so schedule process
176 * context task for adding entry for STAs that do not yet
177 * have one. */
178 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700179#endif
180
Jiri Bencf0706e82007-05-05 11:45:53 -0700181 return sta;
182}
183
Michael Wube8755e2007-07-27 15:43:23 +0200184/* Caller must hold local->sta_lock */
185void sta_info_remove(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700186{
187 struct ieee80211_local *local = sta->local;
188 struct ieee80211_sub_if_data *sdata;
189
Michael Wube8755e2007-07-27 15:43:23 +0200190 /* don't do anything if we've been removed already */
191 if (sta_info_hash_del(local, sta))
192 return;
193
Jiri Bencf0706e82007-05-05 11:45:53 -0700194 list_del(&sta->list);
195 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
196 if (sta->flags & WLAN_STA_PS) {
197 sta->flags &= ~WLAN_STA_PS;
198 if (sdata->bss)
199 atomic_dec(&sdata->bss->num_sta_ps);
200 }
201 local->num_sta--;
202 sta_info_remove_aid_ptr(sta);
Michael Wube8755e2007-07-27 15:43:23 +0200203
204 if (local->ops->sta_table_notification)
205 local->ops->sta_table_notification(local_to_hw(local),
206 local->num_sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700207}
208
Michael Wube8755e2007-07-27 15:43:23 +0200209void sta_info_free(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700210{
211 struct sk_buff *skb;
212 struct ieee80211_local *local = sta->local;
213
Michael Wube8755e2007-07-27 15:43:23 +0200214 might_sleep();
215
216 write_lock_bh(&local->sta_lock);
217 sta_info_remove(sta);
218 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700219
220 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
221 local->total_ps_buffered--;
Michael Wube8755e2007-07-27 15:43:23 +0200222 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700223 }
224 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
Michael Wube8755e2007-07-27 15:43:23 +0200225 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700226 }
227
Michael Wube8755e2007-07-27 15:43:23 +0200228#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
229 printk(KERN_DEBUG "%s: Removed STA " MAC_FMT "\n",
230 local->mdev->name, MAC_ARG(sta->addr));
231#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
232
233 if (sta->key) {
234 ieee80211_debugfs_key_remove(sta->key);
235 ieee80211_key_free(sta->key);
236 sta->key = NULL;
237 }
238
239 rate_control_remove_sta_debugfs(sta);
240 ieee80211_sta_debugfs_remove(sta);
241
242 sta_info_put(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700243}
244
245
246static inline int sta_info_buffer_expired(struct ieee80211_local *local,
247 struct sta_info *sta,
248 struct sk_buff *skb)
249{
250 struct ieee80211_tx_packet_data *pkt_data;
251 int timeout;
252
253 if (!skb)
254 return 0;
255
256 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
257
258 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
259 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
260 15625) * HZ;
261 if (timeout < STA_TX_BUFFER_EXPIRE)
262 timeout = STA_TX_BUFFER_EXPIRE;
263 return time_after(jiffies, pkt_data->jiffies + timeout);
264}
265
266
267static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
268 struct sta_info *sta)
269{
270 unsigned long flags;
271 struct sk_buff *skb;
272
273 if (skb_queue_empty(&sta->ps_tx_buf))
274 return;
275
276 for (;;) {
277 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
278 skb = skb_peek(&sta->ps_tx_buf);
279 if (sta_info_buffer_expired(local, sta, skb)) {
280 skb = __skb_dequeue(&sta->ps_tx_buf);
281 if (skb_queue_empty(&sta->ps_tx_buf))
282 sta->flags &= ~WLAN_STA_TIM;
283 } else
284 skb = NULL;
285 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
286
287 if (skb) {
288 local->total_ps_buffered--;
289 printk(KERN_DEBUG "Buffered frame expired (STA "
290 MAC_FMT ")\n", MAC_ARG(sta->addr));
291 dev_kfree_skb(skb);
292 } else
293 break;
294 }
295}
296
297
298static void sta_info_cleanup(unsigned long data)
299{
300 struct ieee80211_local *local = (struct ieee80211_local *) data;
301 struct sta_info *sta;
302
Michael Wube8755e2007-07-27 15:43:23 +0200303 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700304 list_for_each_entry(sta, &local->sta_list, list) {
305 __sta_info_get(sta);
306 sta_info_cleanup_expire_buffered(local, sta);
307 sta_info_put(sta);
308 }
Michael Wube8755e2007-07-27 15:43:23 +0200309 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700310
311 local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
312 add_timer(&local->sta_cleanup);
313}
314
Jiri Bence9f207f2007-05-05 11:46:38 -0700315#ifdef CONFIG_MAC80211_DEBUGFS
316static void sta_info_debugfs_add_task(struct work_struct *work)
317{
318 struct ieee80211_local *local =
319 container_of(work, struct ieee80211_local, sta_debugfs_add);
320 struct sta_info *sta, *tmp;
321
322 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700323 sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200324 read_lock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700325 list_for_each_entry(tmp, &local->sta_list, list) {
Michael Wube8755e2007-07-27 15:43:23 +0200326 if (!tmp->debugfs.dir) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700327 sta = tmp;
328 __sta_info_get(sta);
329 break;
330 }
331 }
Michael Wube8755e2007-07-27 15:43:23 +0200332 read_unlock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700333
334 if (!sta)
335 break;
336
Jiri Bence9f207f2007-05-05 11:46:38 -0700337 ieee80211_sta_debugfs_add(sta);
338 rate_control_add_sta_debugfs(sta);
339 sta_info_put(sta);
340 }
341}
342#endif
343
Jiri Bencf0706e82007-05-05 11:45:53 -0700344void sta_info_init(struct ieee80211_local *local)
345{
Michael Wube8755e2007-07-27 15:43:23 +0200346 rwlock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700347 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700348
349 init_timer(&local->sta_cleanup);
350 local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
351 local->sta_cleanup.data = (unsigned long) local;
352 local->sta_cleanup.function = sta_info_cleanup;
Jiri Bence9f207f2007-05-05 11:46:38 -0700353
354#ifdef CONFIG_MAC80211_DEBUGFS
355 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
356#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700357}
358
359int sta_info_start(struct ieee80211_local *local)
360{
361 add_timer(&local->sta_cleanup);
362 return 0;
363}
364
365void sta_info_stop(struct ieee80211_local *local)
366{
Jiri Bencf0706e82007-05-05 11:45:53 -0700367 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200368 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700369}
370
371void sta_info_remove_aid_ptr(struct sta_info *sta)
372{
373 struct ieee80211_sub_if_data *sdata;
374
375 if (sta->aid <= 0)
376 return;
377
378 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
379
380 if (sdata->local->ops->set_tim)
381 sdata->local->ops->set_tim(local_to_hw(sdata->local),
382 sta->aid, 0);
383 if (sdata->bss)
384 __bss_tim_clear(sdata->bss, sta->aid);
385}
386
387
388/**
389 * sta_info_flush - flush matching STA entries from the STA table
390 * @local: local interface data
391 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
392 */
393void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
394{
395 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200396 LIST_HEAD(tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700397
Michael Wube8755e2007-07-27 15:43:23 +0200398 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700399 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Michael Wube8755e2007-07-27 15:43:23 +0200400 if (!dev || dev == sta->dev) {
401 __sta_info_get(sta);
402 sta_info_remove(sta);
403 list_add_tail(&sta->list, &tmp_list);
404 }
405 write_unlock_bh(&local->sta_lock);
406
407 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
408 sta_info_free(sta);
409 sta_info_put(sta);
410 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700411}