blob: 1257c7aab2a53774e9c2d5ee057f7781a8ac5d78 [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>
Johannes Berg0d174402007-12-17 15:07:43 +010017#include <linux/timer.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018
19#include <net/mac80211.h>
20#include "ieee80211_i.h"
21#include "ieee80211_rate.h"
22#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070023#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;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200107 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700108
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);
116 }
117 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
118 dev_kfree_skb_any(skb);
119 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200120 for (i = 0; i < STA_TID_NUM; i++)
121 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
Jiri Bencf0706e82007-05-05 11:45:53 -0700122 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
123 rate_control_put(sta->rate_ctrl);
124 kfree(sta);
125}
126
127
128void sta_info_put(struct sta_info *sta)
129{
130 kref_put(&sta->kref, sta_info_release);
131}
132EXPORT_SYMBOL(sta_info_put);
133
134
135struct sta_info * sta_info_add(struct ieee80211_local *local,
136 struct net_device *dev, u8 *addr, gfp_t gfp)
137{
138 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200139 int i;
Joe Perches0795af52007-10-03 17:59:30 -0700140 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700141
142 sta = kzalloc(sizeof(*sta), gfp);
143 if (!sta)
144 return NULL;
145
146 kref_init(&sta->kref);
147
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);
Jiri Bencf0706e82007-05-05 11:45:53 -0700152 kfree(sta);
153 return NULL;
154 }
155
156 memcpy(sta->addr, addr, ETH_ALEN);
157 sta->local = local;
158 sta->dev = dev;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200159 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;
165 /* rx timers */
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);
171 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700172 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
175 * sta_info_put() */
Michael Wube8755e2007-07-27 15:43:23 +0200176 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700177 list_add(&sta->list, &local->sta_list);
178 local->num_sta++;
179 sta_info_hash_add(local, sta);
Tomas Winkler478f8d22007-09-30 13:52:37 +0200180 if (local->ops->sta_notify)
181 local->ops->sta_notify(local_to_hw(local), dev->ifindex,
182 STA_NOTIFY_ADD, addr);
Michael Wube8755e2007-07-27 15:43:23 +0200183 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700184
185#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700186 printk(KERN_DEBUG "%s: Added STA %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400187 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700188#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
189
Jiri Bence9f207f2007-05-05 11:46:38 -0700190#ifdef CONFIG_MAC80211_DEBUGFS
Michael Wube8755e2007-07-27 15:43:23 +0200191 /* debugfs entry adding might sleep, so schedule process
192 * context task for adding entry for STAs that do not yet
193 * have one. */
194 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700195#endif
196
Jiri Bencf0706e82007-05-05 11:45:53 -0700197 return sta;
198}
199
Michael Wube8755e2007-07-27 15:43:23 +0200200/* Caller must hold local->sta_lock */
201void sta_info_remove(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700202{
203 struct ieee80211_local *local = sta->local;
204 struct ieee80211_sub_if_data *sdata;
205
Michael Wube8755e2007-07-27 15:43:23 +0200206 /* don't do anything if we've been removed already */
207 if (sta_info_hash_del(local, sta))
208 return;
209
Jiri Bencf0706e82007-05-05 11:45:53 -0700210 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;
214 if (sdata->bss)
215 atomic_dec(&sdata->bss->num_sta_ps);
216 }
217 local->num_sta--;
218 sta_info_remove_aid_ptr(sta);
Michael Wube8755e2007-07-27 15:43:23 +0200219
Jiri Bencf0706e82007-05-05 11:45:53 -0700220}
221
Michael Wube8755e2007-07-27 15:43:23 +0200222void sta_info_free(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700223{
224 struct sk_buff *skb;
225 struct ieee80211_local *local = sta->local;
Joe Perches0795af52007-10-03 17:59:30 -0700226 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700227
Michael Wube8755e2007-07-27 15:43:23 +0200228 might_sleep();
229
230 write_lock_bh(&local->sta_lock);
231 sta_info_remove(sta);
232 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700233
234 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
235 local->total_ps_buffered--;
Michael Wube8755e2007-07-27 15:43:23 +0200236 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700237 }
238 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
Michael Wube8755e2007-07-27 15:43:23 +0200239 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700240 }
241
Michael Wube8755e2007-07-27 15:43:23 +0200242#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700243 printk(KERN_DEBUG "%s: Removed STA %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400244 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +0200245#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
246
Johannes Berg11a843b2007-08-28 17:01:55 -0400247 ieee80211_key_free(sta->key);
248 sta->key = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200249
Tomas Winkler478f8d22007-09-30 13:52:37 +0200250 if (local->ops->sta_notify)
251 local->ops->sta_notify(local_to_hw(local), sta->dev->ifindex,
252 STA_NOTIFY_REMOVE, sta->addr);
253
Michael Wube8755e2007-07-27 15:43:23 +0200254 rate_control_remove_sta_debugfs(sta);
255 ieee80211_sta_debugfs_remove(sta);
256
257 sta_info_put(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700258}
259
260
261static inline int sta_info_buffer_expired(struct ieee80211_local *local,
262 struct sta_info *sta,
263 struct sk_buff *skb)
264{
265 struct ieee80211_tx_packet_data *pkt_data;
266 int timeout;
267
268 if (!skb)
269 return 0;
270
271 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
272
273 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
274 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
275 15625) * HZ;
276 if (timeout < STA_TX_BUFFER_EXPIRE)
277 timeout = STA_TX_BUFFER_EXPIRE;
278 return time_after(jiffies, pkt_data->jiffies + timeout);
279}
280
281
282static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
283 struct sta_info *sta)
284{
285 unsigned long flags;
286 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700287 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700288
289 if (skb_queue_empty(&sta->ps_tx_buf))
290 return;
291
292 for (;;) {
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;
299 } else
300 skb = NULL;
301 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
302
303 if (skb) {
304 local->total_ps_buffered--;
305 printk(KERN_DEBUG "Buffered frame expired (STA "
Joe Perches0795af52007-10-03 17:59:30 -0700306 "%s)\n", print_mac(mac, sta->addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700307 dev_kfree_skb(skb);
308 } else
309 break;
310 }
311}
312
313
314static void sta_info_cleanup(unsigned long data)
315{
316 struct ieee80211_local *local = (struct ieee80211_local *) data;
317 struct sta_info *sta;
318
Michael Wube8755e2007-07-27 15:43:23 +0200319 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700320 list_for_each_entry(sta, &local->sta_list, list) {
321 __sta_info_get(sta);
322 sta_info_cleanup_expire_buffered(local, sta);
323 sta_info_put(sta);
324 }
Michael Wube8755e2007-07-27 15:43:23 +0200325 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700326
Johannes Berg0d174402007-12-17 15:07:43 +0100327 local->sta_cleanup.expires =
328 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700329 add_timer(&local->sta_cleanup);
330}
331
Jiri Bence9f207f2007-05-05 11:46:38 -0700332#ifdef CONFIG_MAC80211_DEBUGFS
333static void sta_info_debugfs_add_task(struct work_struct *work)
334{
335 struct ieee80211_local *local =
336 container_of(work, struct ieee80211_local, sta_debugfs_add);
337 struct sta_info *sta, *tmp;
338
339 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700340 sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200341 read_lock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700342 list_for_each_entry(tmp, &local->sta_list, list) {
Michael Wube8755e2007-07-27 15:43:23 +0200343 if (!tmp->debugfs.dir) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700344 sta = tmp;
345 __sta_info_get(sta);
346 break;
347 }
348 }
Michael Wube8755e2007-07-27 15:43:23 +0200349 read_unlock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700350
351 if (!sta)
352 break;
353
Jiri Bence9f207f2007-05-05 11:46:38 -0700354 ieee80211_sta_debugfs_add(sta);
355 rate_control_add_sta_debugfs(sta);
356 sta_info_put(sta);
357 }
358}
359#endif
360
Jiri Bencf0706e82007-05-05 11:45:53 -0700361void sta_info_init(struct ieee80211_local *local)
362{
Michael Wube8755e2007-07-27 15:43:23 +0200363 rwlock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700364 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700365
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800366 setup_timer(&local->sta_cleanup, sta_info_cleanup,
367 (unsigned long)local);
Johannes Berg0d174402007-12-17 15:07:43 +0100368 local->sta_cleanup.expires =
369 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bence9f207f2007-05-05 11:46:38 -0700370
371#ifdef CONFIG_MAC80211_DEBUGFS
372 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
373#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700374}
375
376int sta_info_start(struct ieee80211_local *local)
377{
378 add_timer(&local->sta_cleanup);
379 return 0;
380}
381
382void sta_info_stop(struct ieee80211_local *local)
383{
Jiri Bencf0706e82007-05-05 11:45:53 -0700384 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200385 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700386}
387
388void sta_info_remove_aid_ptr(struct sta_info *sta)
389{
390 struct ieee80211_sub_if_data *sdata;
391
392 if (sta->aid <= 0)
393 return;
394
395 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
396
397 if (sdata->local->ops->set_tim)
398 sdata->local->ops->set_tim(local_to_hw(sdata->local),
399 sta->aid, 0);
400 if (sdata->bss)
401 __bss_tim_clear(sdata->bss, sta->aid);
402}
403
404
405/**
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
409 */
410void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
411{
412 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200413 LIST_HEAD(tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700414
Michael Wube8755e2007-07-27 15:43:23 +0200415 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700416 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Michael Wube8755e2007-07-27 15:43:23 +0200417 if (!dev || dev == sta->dev) {
418 __sta_info_get(sta);
419 sta_info_remove(sta);
420 list_add_tail(&sta->list, &tmp_list);
421 }
422 write_unlock_bh(&local->sta_lock);
423
424 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
425 sta_info_free(sta);
426 sta_info_put(sta);
427 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700428}