blob: c17172abb21c4f8e7baca64078a9dedcec0c762a [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_sta.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070023
24/* Caller must hold local->sta_lock */
25static void sta_info_hash_add(struct ieee80211_local *local,
26 struct sta_info *sta)
27{
28 sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
29 local->sta_hash[STA_HASH(sta->addr)] = sta;
30}
31
32
33/* Caller must hold local->sta_lock */
Michael Wube8755e2007-07-27 15:43:23 +020034static int sta_info_hash_del(struct ieee80211_local *local,
35 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070036{
37 struct sta_info *s;
38
39 s = local->sta_hash[STA_HASH(sta->addr)];
40 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020041 return -ENOENT;
42 if (s == sta) {
Jiri Bencf0706e82007-05-05 11:45:53 -070043 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020044 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070045 }
46
Michael Wube8755e2007-07-27 15:43:23 +020047 while (s->hnext && s->hnext != sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070048 s = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020049 if (s->hnext) {
50 s->hnext = sta->hnext;
51 return 0;
52 }
Jiri Bencf0706e82007-05-05 11:45:53 -070053
Michael Wube8755e2007-07-27 15:43:23 +020054 return -ENOENT;
Jiri Bencf0706e82007-05-05 11:45:53 -070055}
56
57struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
58{
59 struct sta_info *sta;
60
Michael Wube8755e2007-07-27 15:43:23 +020061 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070062 sta = local->sta_hash[STA_HASH(addr)];
63 while (sta) {
64 if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
65 __sta_info_get(sta);
66 break;
67 }
68 sta = sta->hnext;
69 }
Michael Wube8755e2007-07-27 15:43:23 +020070 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070071
72 return sta;
73}
74EXPORT_SYMBOL(sta_info_get);
75
76int sta_info_min_txrate_get(struct ieee80211_local *local)
77{
78 struct sta_info *sta;
79 struct ieee80211_hw_mode *mode;
80 int min_txrate = 9999999;
81 int i;
82
Michael Wube8755e2007-07-27 15:43:23 +020083 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070084 mode = local->oper_hw_mode;
85 for (i = 0; i < STA_HASH_SIZE; i++) {
86 sta = local->sta_hash[i];
87 while (sta) {
88 if (sta->txrate < min_txrate)
89 min_txrate = sta->txrate;
90 sta = sta->hnext;
91 }
92 }
Michael Wube8755e2007-07-27 15:43:23 +020093 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -070094 if (min_txrate == 9999999)
95 min_txrate = 0;
96
97 return mode->rates[min_txrate].rate;
98}
99
100
101static void sta_info_release(struct kref *kref)
102{
103 struct sta_info *sta = container_of(kref, struct sta_info, kref);
104 struct ieee80211_local *local = sta->local;
105 struct sk_buff *skb;
106
107 /* free sta structure; it has already been removed from
108 * hash table etc. external structures. Make sure that all
109 * buffered frames are release (one might have been added
110 * after sta_info_free() was called). */
111 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
112 local->total_ps_buffered--;
113 dev_kfree_skb_any(skb);
114 }
115 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
116 dev_kfree_skb_any(skb);
117 }
118 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
119 rate_control_put(sta->rate_ctrl);
120 kfree(sta);
121}
122
123
124void sta_info_put(struct sta_info *sta)
125{
126 kref_put(&sta->kref, sta_info_release);
127}
128EXPORT_SYMBOL(sta_info_put);
129
130
131struct sta_info * sta_info_add(struct ieee80211_local *local,
132 struct net_device *dev, u8 *addr, gfp_t gfp)
133{
134 struct sta_info *sta;
135
136 sta = kzalloc(sizeof(*sta), gfp);
137 if (!sta)
138 return NULL;
139
140 kref_init(&sta->kref);
141
142 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
143 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
144 if (!sta->rate_ctrl_priv) {
145 rate_control_put(sta->rate_ctrl);
Jiri Bencf0706e82007-05-05 11:45:53 -0700146 kfree(sta);
147 return NULL;
148 }
149
150 memcpy(sta->addr, addr, ETH_ALEN);
151 sta->local = local;
152 sta->dev = dev;
153 skb_queue_head_init(&sta->ps_tx_buf);
154 skb_queue_head_init(&sta->tx_filtered);
155 __sta_info_get(sta); /* sta used by caller, decremented by
156 * sta_info_put() */
Michael Wube8755e2007-07-27 15:43:23 +0200157 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700158 list_add(&sta->list, &local->sta_list);
159 local->num_sta++;
160 sta_info_hash_add(local, sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700161 if (local->ops->sta_table_notification)
162 local->ops->sta_table_notification(local_to_hw(local),
163 local->num_sta);
Michael Wube8755e2007-07-27 15:43:23 +0200164 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700165
166#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
167 printk(KERN_DEBUG "%s: Added STA " MAC_FMT "\n",
168 local->mdev->name, MAC_ARG(addr));
169#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
170
Jiri Bence9f207f2007-05-05 11:46:38 -0700171#ifdef CONFIG_MAC80211_DEBUGFS
Michael Wube8755e2007-07-27 15:43:23 +0200172 /* debugfs entry adding might sleep, so schedule process
173 * context task for adding entry for STAs that do not yet
174 * have one. */
175 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700176#endif
177
Jiri Bencf0706e82007-05-05 11:45:53 -0700178 return sta;
179}
180
Michael Wube8755e2007-07-27 15:43:23 +0200181/* Caller must hold local->sta_lock */
182void sta_info_remove(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700183{
184 struct ieee80211_local *local = sta->local;
185 struct ieee80211_sub_if_data *sdata;
186
Michael Wube8755e2007-07-27 15:43:23 +0200187 /* don't do anything if we've been removed already */
188 if (sta_info_hash_del(local, sta))
189 return;
190
Jiri Bencf0706e82007-05-05 11:45:53 -0700191 list_del(&sta->list);
192 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
193 if (sta->flags & WLAN_STA_PS) {
194 sta->flags &= ~WLAN_STA_PS;
195 if (sdata->bss)
196 atomic_dec(&sdata->bss->num_sta_ps);
197 }
198 local->num_sta--;
199 sta_info_remove_aid_ptr(sta);
Michael Wube8755e2007-07-27 15:43:23 +0200200
201 if (local->ops->sta_table_notification)
202 local->ops->sta_table_notification(local_to_hw(local),
203 local->num_sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700204}
205
Michael Wube8755e2007-07-27 15:43:23 +0200206void sta_info_free(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700207{
208 struct sk_buff *skb;
209 struct ieee80211_local *local = sta->local;
210
Michael Wube8755e2007-07-27 15:43:23 +0200211 might_sleep();
212
213 write_lock_bh(&local->sta_lock);
214 sta_info_remove(sta);
215 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700216
217 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
218 local->total_ps_buffered--;
Michael Wube8755e2007-07-27 15:43:23 +0200219 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700220 }
221 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
Michael Wube8755e2007-07-27 15:43:23 +0200222 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700223 }
224
Michael Wube8755e2007-07-27 15:43:23 +0200225#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 */
229
Johannes Berg11a843b2007-08-28 17:01:55 -0400230 ieee80211_key_free(sta->key);
231 sta->key = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200232
233 rate_control_remove_sta_debugfs(sta);
234 ieee80211_sta_debugfs_remove(sta);
235
236 sta_info_put(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700237}
238
239
240static inline int sta_info_buffer_expired(struct ieee80211_local *local,
241 struct sta_info *sta,
242 struct sk_buff *skb)
243{
244 struct ieee80211_tx_packet_data *pkt_data;
245 int timeout;
246
247 if (!skb)
248 return 0;
249
250 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
251
252 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
253 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
254 15625) * HZ;
255 if (timeout < STA_TX_BUFFER_EXPIRE)
256 timeout = STA_TX_BUFFER_EXPIRE;
257 return time_after(jiffies, pkt_data->jiffies + timeout);
258}
259
260
261static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
262 struct sta_info *sta)
263{
264 unsigned long flags;
265 struct sk_buff *skb;
266
267 if (skb_queue_empty(&sta->ps_tx_buf))
268 return;
269
270 for (;;) {
271 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
272 skb = skb_peek(&sta->ps_tx_buf);
273 if (sta_info_buffer_expired(local, sta, skb)) {
274 skb = __skb_dequeue(&sta->ps_tx_buf);
275 if (skb_queue_empty(&sta->ps_tx_buf))
276 sta->flags &= ~WLAN_STA_TIM;
277 } else
278 skb = NULL;
279 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
280
281 if (skb) {
282 local->total_ps_buffered--;
283 printk(KERN_DEBUG "Buffered frame expired (STA "
284 MAC_FMT ")\n", MAC_ARG(sta->addr));
285 dev_kfree_skb(skb);
286 } else
287 break;
288 }
289}
290
291
292static void sta_info_cleanup(unsigned long data)
293{
294 struct ieee80211_local *local = (struct ieee80211_local *) data;
295 struct sta_info *sta;
296
Michael Wube8755e2007-07-27 15:43:23 +0200297 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700298 list_for_each_entry(sta, &local->sta_list, list) {
299 __sta_info_get(sta);
300 sta_info_cleanup_expire_buffered(local, sta);
301 sta_info_put(sta);
302 }
Michael Wube8755e2007-07-27 15:43:23 +0200303 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700304
305 local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
306 add_timer(&local->sta_cleanup);
307}
308
Jiri Bence9f207f2007-05-05 11:46:38 -0700309#ifdef CONFIG_MAC80211_DEBUGFS
310static void sta_info_debugfs_add_task(struct work_struct *work)
311{
312 struct ieee80211_local *local =
313 container_of(work, struct ieee80211_local, sta_debugfs_add);
314 struct sta_info *sta, *tmp;
315
316 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700317 sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200318 read_lock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700319 list_for_each_entry(tmp, &local->sta_list, list) {
Michael Wube8755e2007-07-27 15:43:23 +0200320 if (!tmp->debugfs.dir) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700321 sta = tmp;
322 __sta_info_get(sta);
323 break;
324 }
325 }
Michael Wube8755e2007-07-27 15:43:23 +0200326 read_unlock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700327
328 if (!sta)
329 break;
330
Jiri Bence9f207f2007-05-05 11:46:38 -0700331 ieee80211_sta_debugfs_add(sta);
332 rate_control_add_sta_debugfs(sta);
333 sta_info_put(sta);
334 }
335}
336#endif
337
Jiri Bencf0706e82007-05-05 11:45:53 -0700338void sta_info_init(struct ieee80211_local *local)
339{
Michael Wube8755e2007-07-27 15:43:23 +0200340 rwlock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700341 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700342
343 init_timer(&local->sta_cleanup);
344 local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
345 local->sta_cleanup.data = (unsigned long) local;
346 local->sta_cleanup.function = sta_info_cleanup;
Jiri Bence9f207f2007-05-05 11:46:38 -0700347
348#ifdef CONFIG_MAC80211_DEBUGFS
349 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
350#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700351}
352
353int sta_info_start(struct ieee80211_local *local)
354{
355 add_timer(&local->sta_cleanup);
356 return 0;
357}
358
359void sta_info_stop(struct ieee80211_local *local)
360{
Jiri Bencf0706e82007-05-05 11:45:53 -0700361 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200362 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700363}
364
365void sta_info_remove_aid_ptr(struct sta_info *sta)
366{
367 struct ieee80211_sub_if_data *sdata;
368
369 if (sta->aid <= 0)
370 return;
371
372 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
373
374 if (sdata->local->ops->set_tim)
375 sdata->local->ops->set_tim(local_to_hw(sdata->local),
376 sta->aid, 0);
377 if (sdata->bss)
378 __bss_tim_clear(sdata->bss, sta->aid);
379}
380
381
382/**
383 * sta_info_flush - flush matching STA entries from the STA table
384 * @local: local interface data
385 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
386 */
387void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
388{
389 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200390 LIST_HEAD(tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700391
Michael Wube8755e2007-07-27 15:43:23 +0200392 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700393 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Michael Wube8755e2007-07-27 15:43:23 +0200394 if (!dev || dev == sta->dev) {
395 __sta_info_get(sta);
396 sta_info_remove(sta);
397 list_add_tail(&sta->list, &tmp_list);
398 }
399 write_unlock_bh(&local->sta_lock);
400
401 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
402 sta_info_free(sta);
403 sta_info_put(sta);
404 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700405}