blob: ddc1f47194dd10228830d06f34030e2458acf070 [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 Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200120 for (i = 0; i < STA_TID_NUM; i++) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200121 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200122 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
123 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700124 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
125 rate_control_put(sta->rate_ctrl);
126 kfree(sta);
127}
128
129
130void sta_info_put(struct sta_info *sta)
131{
132 kref_put(&sta->kref, sta_info_release);
133}
134EXPORT_SYMBOL(sta_info_put);
135
136
137struct sta_info * sta_info_add(struct ieee80211_local *local,
138 struct net_device *dev, u8 *addr, gfp_t gfp)
139{
140 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200141 int i;
Joe Perches0795af52007-10-03 17:59:30 -0700142 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700143
144 sta = kzalloc(sizeof(*sta), gfp);
145 if (!sta)
146 return NULL;
147
148 kref_init(&sta->kref);
149
150 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
151 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
152 if (!sta->rate_ctrl_priv) {
153 rate_control_put(sta->rate_ctrl);
Jiri Bencf0706e82007-05-05 11:45:53 -0700154 kfree(sta);
155 return NULL;
156 }
157
158 memcpy(sta->addr, addr, ETH_ALEN);
159 sta->local = local;
160 sta->dev = dev;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200161 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200162 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200163 for (i = 0; i < STA_TID_NUM; i++) {
164 /* timer_to_tid must be initialized with identity mapping to
165 * enable session_timer's data differentiation. refer to
166 * sta_rx_agg_session_timer_expired for useage */
167 sta->timer_to_tid[i] = i;
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200168 /* tid to tx queue: initialize according to HW (0 is valid) */
169 sta->tid_to_tx_q[i] = local->hw.queues;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200170 /* rx timers */
171 sta->ampdu_mlme.tid_rx[i].session_timer.function =
172 sta_rx_agg_session_timer_expired;
173 sta->ampdu_mlme.tid_rx[i].session_timer.data =
174 (unsigned long)&sta->timer_to_tid[i];
175 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200176 /* tx timers */
177 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
178 sta_addba_resp_timer_expired;
179 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
180 (unsigned long)&sta->timer_to_tid[i];
181 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200182 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700183 skb_queue_head_init(&sta->ps_tx_buf);
184 skb_queue_head_init(&sta->tx_filtered);
185 __sta_info_get(sta); /* sta used by caller, decremented by
186 * sta_info_put() */
Michael Wube8755e2007-07-27 15:43:23 +0200187 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700188 list_add(&sta->list, &local->sta_list);
189 local->num_sta++;
190 sta_info_hash_add(local, sta);
Johannes Berg32bfd352007-12-19 01:31:26 +0100191 if (local->ops->sta_notify) {
192 struct ieee80211_sub_if_data *sdata;
193
194 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100195 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
Johannes Berg32bfd352007-12-19 01:31:26 +0100196 sdata = sdata->u.vlan.ap;
197
198 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
199 STA_NOTIFY_ADD, addr);
200 }
Michael Wube8755e2007-07-27 15:43:23 +0200201 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700202
203#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700204 printk(KERN_DEBUG "%s: Added STA %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400205 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700206#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
207
Jiri Bence9f207f2007-05-05 11:46:38 -0700208#ifdef CONFIG_MAC80211_DEBUGFS
Michael Wube8755e2007-07-27 15:43:23 +0200209 /* debugfs entry adding might sleep, so schedule process
210 * context task for adding entry for STAs that do not yet
211 * have one. */
212 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700213#endif
214
Jiri Bencf0706e82007-05-05 11:45:53 -0700215 return sta;
216}
217
Michael Wube8755e2007-07-27 15:43:23 +0200218/* Caller must hold local->sta_lock */
219void sta_info_remove(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700220{
221 struct ieee80211_local *local = sta->local;
222 struct ieee80211_sub_if_data *sdata;
223
Michael Wube8755e2007-07-27 15:43:23 +0200224 /* don't do anything if we've been removed already */
225 if (sta_info_hash_del(local, sta))
226 return;
227
Jiri Bencf0706e82007-05-05 11:45:53 -0700228 list_del(&sta->list);
229 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
230 if (sta->flags & WLAN_STA_PS) {
231 sta->flags &= ~WLAN_STA_PS;
232 if (sdata->bss)
233 atomic_dec(&sdata->bss->num_sta_ps);
234 }
235 local->num_sta--;
236 sta_info_remove_aid_ptr(sta);
Michael Wube8755e2007-07-27 15:43:23 +0200237
Jiri Bencf0706e82007-05-05 11:45:53 -0700238}
239
Michael Wube8755e2007-07-27 15:43:23 +0200240void sta_info_free(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700241{
242 struct sk_buff *skb;
243 struct ieee80211_local *local = sta->local;
Joe Perches0795af52007-10-03 17:59:30 -0700244 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700245
Michael Wube8755e2007-07-27 15:43:23 +0200246 might_sleep();
247
248 write_lock_bh(&local->sta_lock);
249 sta_info_remove(sta);
250 write_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700251
252 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
253 local->total_ps_buffered--;
Michael Wube8755e2007-07-27 15:43:23 +0200254 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700255 }
256 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
Michael Wube8755e2007-07-27 15:43:23 +0200257 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700258 }
259
Michael Wube8755e2007-07-27 15:43:23 +0200260#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700261 printk(KERN_DEBUG "%s: Removed STA %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400262 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +0200263#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
264
Johannes Berg11a843b2007-08-28 17:01:55 -0400265 ieee80211_key_free(sta->key);
266 sta->key = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200267
Johannes Berg32bfd352007-12-19 01:31:26 +0100268 if (local->ops->sta_notify) {
269 struct ieee80211_sub_if_data *sdata;
270
271 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
272
Johannes Berg51fb61e2007-12-19 01:31:27 +0100273 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
Johannes Berg32bfd352007-12-19 01:31:26 +0100274 sdata = sdata->u.vlan.ap;
275
276 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
277 STA_NOTIFY_REMOVE, sta->addr);
278 }
Tomas Winkler478f8d22007-09-30 13:52:37 +0200279
Michael Wube8755e2007-07-27 15:43:23 +0200280 rate_control_remove_sta_debugfs(sta);
281 ieee80211_sta_debugfs_remove(sta);
282
283 sta_info_put(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700284}
285
286
287static inline int sta_info_buffer_expired(struct ieee80211_local *local,
288 struct sta_info *sta,
289 struct sk_buff *skb)
290{
291 struct ieee80211_tx_packet_data *pkt_data;
292 int timeout;
293
294 if (!skb)
295 return 0;
296
297 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
298
299 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
300 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
301 15625) * HZ;
302 if (timeout < STA_TX_BUFFER_EXPIRE)
303 timeout = STA_TX_BUFFER_EXPIRE;
304 return time_after(jiffies, pkt_data->jiffies + timeout);
305}
306
307
308static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
309 struct sta_info *sta)
310{
311 unsigned long flags;
312 struct sk_buff *skb;
Joe Perches0795af52007-10-03 17:59:30 -0700313 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700314
315 if (skb_queue_empty(&sta->ps_tx_buf))
316 return;
317
318 for (;;) {
319 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
320 skb = skb_peek(&sta->ps_tx_buf);
321 if (sta_info_buffer_expired(local, sta, skb)) {
322 skb = __skb_dequeue(&sta->ps_tx_buf);
323 if (skb_queue_empty(&sta->ps_tx_buf))
324 sta->flags &= ~WLAN_STA_TIM;
325 } else
326 skb = NULL;
327 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
328
329 if (skb) {
330 local->total_ps_buffered--;
331 printk(KERN_DEBUG "Buffered frame expired (STA "
Joe Perches0795af52007-10-03 17:59:30 -0700332 "%s)\n", print_mac(mac, sta->addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700333 dev_kfree_skb(skb);
334 } else
335 break;
336 }
337}
338
339
340static void sta_info_cleanup(unsigned long data)
341{
342 struct ieee80211_local *local = (struct ieee80211_local *) data;
343 struct sta_info *sta;
344
Michael Wube8755e2007-07-27 15:43:23 +0200345 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700346 list_for_each_entry(sta, &local->sta_list, list) {
347 __sta_info_get(sta);
348 sta_info_cleanup_expire_buffered(local, sta);
349 sta_info_put(sta);
350 }
Michael Wube8755e2007-07-27 15:43:23 +0200351 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700352
Johannes Berg0d174402007-12-17 15:07:43 +0100353 local->sta_cleanup.expires =
354 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700355 add_timer(&local->sta_cleanup);
356}
357
Jiri Bence9f207f2007-05-05 11:46:38 -0700358#ifdef CONFIG_MAC80211_DEBUGFS
359static void sta_info_debugfs_add_task(struct work_struct *work)
360{
361 struct ieee80211_local *local =
362 container_of(work, struct ieee80211_local, sta_debugfs_add);
363 struct sta_info *sta, *tmp;
364
365 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700366 sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200367 read_lock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700368 list_for_each_entry(tmp, &local->sta_list, list) {
Michael Wube8755e2007-07-27 15:43:23 +0200369 if (!tmp->debugfs.dir) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700370 sta = tmp;
371 __sta_info_get(sta);
372 break;
373 }
374 }
Michael Wube8755e2007-07-27 15:43:23 +0200375 read_unlock_bh(&local->sta_lock);
Jiri Bence9f207f2007-05-05 11:46:38 -0700376
377 if (!sta)
378 break;
379
Jiri Bence9f207f2007-05-05 11:46:38 -0700380 ieee80211_sta_debugfs_add(sta);
381 rate_control_add_sta_debugfs(sta);
382 sta_info_put(sta);
383 }
384}
385#endif
386
Jiri Bencf0706e82007-05-05 11:45:53 -0700387void sta_info_init(struct ieee80211_local *local)
388{
Michael Wube8755e2007-07-27 15:43:23 +0200389 rwlock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700390 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700391
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800392 setup_timer(&local->sta_cleanup, sta_info_cleanup,
393 (unsigned long)local);
Johannes Berg0d174402007-12-17 15:07:43 +0100394 local->sta_cleanup.expires =
395 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bence9f207f2007-05-05 11:46:38 -0700396
397#ifdef CONFIG_MAC80211_DEBUGFS
398 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
399#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700400}
401
402int sta_info_start(struct ieee80211_local *local)
403{
404 add_timer(&local->sta_cleanup);
405 return 0;
406}
407
408void sta_info_stop(struct ieee80211_local *local)
409{
Jiri Bencf0706e82007-05-05 11:45:53 -0700410 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200411 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700412}
413
414void sta_info_remove_aid_ptr(struct sta_info *sta)
415{
416 struct ieee80211_sub_if_data *sdata;
417
418 if (sta->aid <= 0)
419 return;
420
421 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
422
423 if (sdata->local->ops->set_tim)
424 sdata->local->ops->set_tim(local_to_hw(sdata->local),
425 sta->aid, 0);
426 if (sdata->bss)
427 __bss_tim_clear(sdata->bss, sta->aid);
428}
429
430
431/**
432 * sta_info_flush - flush matching STA entries from the STA table
433 * @local: local interface data
434 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
435 */
436void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
437{
438 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200439 LIST_HEAD(tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700440
Michael Wube8755e2007-07-27 15:43:23 +0200441 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700442 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Michael Wube8755e2007-07-27 15:43:23 +0200443 if (!dev || dev == sta->dev) {
444 __sta_info_get(sta);
445 sta_info_remove(sta);
446 list_add_tail(&sta->list, &tmp_list);
447 }
448 write_unlock_bh(&local->sta_lock);
449
450 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
451 sta_info_free(sta);
452 sta_info_put(sta);
453 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700454}