blob: e8491554a5dc1129950d25f94022391304102598 [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;
Joe Perches0795af52007-10-03 17:59:30 -0700135 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700136
137 sta = kzalloc(sizeof(*sta), gfp);
138 if (!sta)
139 return NULL;
140
141 kref_init(&sta->kref);
142
143 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
144 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
145 if (!sta->rate_ctrl_priv) {
146 rate_control_put(sta->rate_ctrl);
Jiri Bencf0706e82007-05-05 11:45:53 -0700147 kfree(sta);
148 return NULL;
149 }
150
151 memcpy(sta->addr, addr, ETH_ALEN);
152 sta->local = local;
153 sta->dev = dev;
154 skb_queue_head_init(&sta->ps_tx_buf);
155 skb_queue_head_init(&sta->tx_filtered);
156 __sta_info_get(sta); /* sta used by caller, decremented by
157 * sta_info_put() */
Michael Wube8755e2007-07-27 15:43:23 +0200158 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700159 list_add(&sta->list, &local->sta_list);
160 local->num_sta++;
161 sta_info_hash_add(local, sta);
Tomas Winkler478f8d22007-09-30 13:52:37 +0200162 if (local->ops->sta_notify)
163 local->ops->sta_notify(local_to_hw(local), dev->ifindex,
164 STA_NOTIFY_ADD, addr);
Michael Wube8755e2007-07-27 15:43:23 +0200165 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700166
167#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700168 printk(KERN_DEBUG "%s: Added STA %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400169 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700170#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
171
Jiri Bence9f207f2007-05-05 11:46:38 -0700172#ifdef CONFIG_MAC80211_DEBUGFS
Michael Wube8755e2007-07-27 15:43:23 +0200173 /* debugfs entry adding might sleep, so schedule process
174 * context task for adding entry for STAs that do not yet
175 * have one. */
176 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700177#endif
178
Jiri Bencf0706e82007-05-05 11:45:53 -0700179 return sta;
180}
181
Michael Wube8755e2007-07-27 15:43:23 +0200182/* Caller must hold local->sta_lock */
183void sta_info_remove(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700184{
185 struct ieee80211_local *local = sta->local;
186 struct ieee80211_sub_if_data *sdata;
187
Michael Wube8755e2007-07-27 15:43:23 +0200188 /* don't do anything if we've been removed already */
189 if (sta_info_hash_del(local, sta))
190 return;
191
Jiri Bencf0706e82007-05-05 11:45:53 -0700192 list_del(&sta->list);
193 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
194 if (sta->flags & WLAN_STA_PS) {
195 sta->flags &= ~WLAN_STA_PS;
196 if (sdata->bss)
197 atomic_dec(&sdata->bss->num_sta_ps);
198 }
199 local->num_sta--;
200 sta_info_remove_aid_ptr(sta);
Michael Wube8755e2007-07-27 15:43:23 +0200201
Jiri Bencf0706e82007-05-05 11:45:53 -0700202}
203
Michael Wube8755e2007-07-27 15:43:23 +0200204void sta_info_free(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700205{
206 struct sk_buff *skb;
207 struct ieee80211_local *local = sta->local;
Joe Perches0795af52007-10-03 17:59:30 -0700208 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700209
Michael Wube8755e2007-07-27 15:43:23 +0200210 might_sleep();
211
212 write_lock_bh(&local->sta_lock);
213 sta_info_remove(sta);
214 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700215
216 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
217 local->total_ps_buffered--;
Michael Wube8755e2007-07-27 15:43:23 +0200218 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700219 }
220 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
Michael Wube8755e2007-07-27 15:43:23 +0200221 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700222 }
223
Michael Wube8755e2007-07-27 15:43:23 +0200224#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700225 printk(KERN_DEBUG "%s: Removed STA %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400226 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +0200227#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
228
Johannes Berg11a843b2007-08-28 17:01:55 -0400229 ieee80211_key_free(sta->key);
230 sta->key = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200231
Tomas Winkler478f8d22007-09-30 13:52:37 +0200232 if (local->ops->sta_notify)
233 local->ops->sta_notify(local_to_hw(local), sta->dev->ifindex,
234 STA_NOTIFY_REMOVE, sta->addr);
235
Michael Wube8755e2007-07-27 15:43:23 +0200236 rate_control_remove_sta_debugfs(sta);
237 ieee80211_sta_debugfs_remove(sta);
238
239 sta_info_put(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700240}
241
242
243static inline int sta_info_buffer_expired(struct ieee80211_local *local,
244 struct sta_info *sta,
245 struct sk_buff *skb)
246{
247 struct ieee80211_tx_packet_data *pkt_data;
248 int timeout;
249
250 if (!skb)
251 return 0;
252
253 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
254
255 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
256 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
257 15625) * HZ;
258 if (timeout < STA_TX_BUFFER_EXPIRE)
259 timeout = STA_TX_BUFFER_EXPIRE;
260 return time_after(jiffies, pkt_data->jiffies + timeout);
261}
262
263
264static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
265 struct sta_info *sta)
266{
267 unsigned long flags;
268 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700269 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700270
271 if (skb_queue_empty(&sta->ps_tx_buf))
272 return;
273
274 for (;;) {
275 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
276 skb = skb_peek(&sta->ps_tx_buf);
277 if (sta_info_buffer_expired(local, sta, skb)) {
278 skb = __skb_dequeue(&sta->ps_tx_buf);
279 if (skb_queue_empty(&sta->ps_tx_buf))
280 sta->flags &= ~WLAN_STA_TIM;
281 } else
282 skb = NULL;
283 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
284
285 if (skb) {
286 local->total_ps_buffered--;
287 printk(KERN_DEBUG "Buffered frame expired (STA "
Joe Perches0795af52007-10-03 17:59:30 -0700288 "%s)\n", print_mac(mac, sta->addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700289 dev_kfree_skb(skb);
290 } else
291 break;
292 }
293}
294
295
296static void sta_info_cleanup(unsigned long data)
297{
298 struct ieee80211_local *local = (struct ieee80211_local *) data;
299 struct sta_info *sta;
300
Michael Wube8755e2007-07-27 15:43:23 +0200301 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700302 list_for_each_entry(sta, &local->sta_list, list) {
303 __sta_info_get(sta);
304 sta_info_cleanup_expire_buffered(local, sta);
305 sta_info_put(sta);
306 }
Michael Wube8755e2007-07-27 15:43:23 +0200307 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700308
309 local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
310 add_timer(&local->sta_cleanup);
311}
312
Jiri Bence9f207f2007-05-05 11:46:38 -0700313#ifdef CONFIG_MAC80211_DEBUGFS
314static void sta_info_debugfs_add_task(struct work_struct *work)
315{
316 struct ieee80211_local *local =
317 container_of(work, struct ieee80211_local, sta_debugfs_add);
318 struct sta_info *sta, *tmp;
319
320 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700321 sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200322 read_lock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700323 list_for_each_entry(tmp, &local->sta_list, list) {
Michael Wube8755e2007-07-27 15:43:23 +0200324 if (!tmp->debugfs.dir) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700325 sta = tmp;
326 __sta_info_get(sta);
327 break;
328 }
329 }
Michael Wube8755e2007-07-27 15:43:23 +0200330 read_unlock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700331
332 if (!sta)
333 break;
334
Jiri Bence9f207f2007-05-05 11:46:38 -0700335 ieee80211_sta_debugfs_add(sta);
336 rate_control_add_sta_debugfs(sta);
337 sta_info_put(sta);
338 }
339}
340#endif
341
Jiri Bencf0706e82007-05-05 11:45:53 -0700342void sta_info_init(struct ieee80211_local *local)
343{
Michael Wube8755e2007-07-27 15:43:23 +0200344 rwlock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700345 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700346
347 init_timer(&local->sta_cleanup);
348 local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
349 local->sta_cleanup.data = (unsigned long) local;
350 local->sta_cleanup.function = sta_info_cleanup;
Jiri Bence9f207f2007-05-05 11:46:38 -0700351
352#ifdef CONFIG_MAC80211_DEBUGFS
353 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
354#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700355}
356
357int sta_info_start(struct ieee80211_local *local)
358{
359 add_timer(&local->sta_cleanup);
360 return 0;
361}
362
363void sta_info_stop(struct ieee80211_local *local)
364{
Jiri Bencf0706e82007-05-05 11:45:53 -0700365 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200366 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700367}
368
369void sta_info_remove_aid_ptr(struct sta_info *sta)
370{
371 struct ieee80211_sub_if_data *sdata;
372
373 if (sta->aid <= 0)
374 return;
375
376 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
377
378 if (sdata->local->ops->set_tim)
379 sdata->local->ops->set_tim(local_to_hw(sdata->local),
380 sta->aid, 0);
381 if (sdata->bss)
382 __bss_tim_clear(sdata->bss, sta->aid);
383}
384
385
386/**
387 * sta_info_flush - flush matching STA entries from the STA table
388 * @local: local interface data
389 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
390 */
391void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
392{
393 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200394 LIST_HEAD(tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700395
Michael Wube8755e2007-07-27 15:43:23 +0200396 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700397 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Michael Wube8755e2007-07-27 15:43:23 +0200398 if (!dev || dev == sta->dev) {
399 __sta_info_get(sta);
400 sta_info_remove(sta);
401 list_add_tail(&sta->list, &tmp_list);
402 }
403 write_unlock_bh(&local->sta_lock);
404
405 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
406 sta_info_free(sta);
407 sta_info_put(sta);
408 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700409}