Upgrade b43 and mac80211.
[openwrt/staging/lynxis/omap.git] / package / mac80211 / src / net / mac80211 / sta_info.c
1 /*
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 #include <linux/timer.h>
18
19 #include <net/mac80211.h>
20 #include "ieee80211_i.h"
21 #include "ieee80211_rate.h"
22 #include "sta_info.h"
23 #include "debugfs_sta.h"
24
25 /* Caller must hold local->sta_lock */
26 static 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 */
35 static int sta_info_hash_del(struct ieee80211_local *local,
36 struct sta_info *sta)
37 {
38 struct sta_info *s;
39
40 s = local->sta_hash[STA_HASH(sta->addr)];
41 if (!s)
42 return -ENOENT;
43 if (s == sta) {
44 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
45 return 0;
46 }
47
48 while (s->hnext && s->hnext != sta)
49 s = s->hnext;
50 if (s->hnext) {
51 s->hnext = sta->hnext;
52 return 0;
53 }
54
55 return -ENOENT;
56 }
57
58 struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
59 {
60 struct sta_info *sta;
61
62 read_lock_bh(&local->sta_lock);
63 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 }
71 read_unlock_bh(&local->sta_lock);
72
73 return sta;
74 }
75 EXPORT_SYMBOL(sta_info_get);
76
77
78 static void sta_info_release(struct kref *kref)
79 {
80 struct sta_info *sta = container_of(kref, struct sta_info, kref);
81 struct ieee80211_local *local = sta->local;
82 struct sk_buff *skb;
83 int i;
84
85 /* free sta structure; it has already been removed from
86 * hash table etc. external structures. Make sure that all
87 * buffered frames are release (one might have been added
88 * after sta_info_free() was called). */
89 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
90 local->total_ps_buffered--;
91 dev_kfree_skb_any(skb);
92 }
93 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
94 dev_kfree_skb_any(skb);
95 }
96 for (i = 0; i < STA_TID_NUM; i++) {
97 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
98 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
99 }
100 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
101 rate_control_put(sta->rate_ctrl);
102 kfree(sta);
103 }
104
105
106 void sta_info_put(struct sta_info *sta)
107 {
108 kref_put(&sta->kref, sta_info_release);
109 }
110 EXPORT_SYMBOL(sta_info_put);
111
112
113 struct sta_info * sta_info_add(struct ieee80211_local *local,
114 struct net_device *dev, u8 *addr, gfp_t gfp)
115 {
116 struct sta_info *sta;
117 int i;
118 DECLARE_MAC_BUF(mac);
119
120 sta = kzalloc(sizeof(*sta), gfp);
121 if (!sta)
122 return NULL;
123
124 kref_init(&sta->kref);
125
126 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
127 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
128 if (!sta->rate_ctrl_priv) {
129 rate_control_put(sta->rate_ctrl);
130 kfree(sta);
131 return NULL;
132 }
133
134 memcpy(sta->addr, addr, ETH_ALEN);
135 sta->local = local;
136 sta->dev = dev;
137 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
138 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
139 for (i = 0; i < STA_TID_NUM; i++) {
140 /* timer_to_tid must be initialized with identity mapping to
141 * enable session_timer's data differentiation. refer to
142 * sta_rx_agg_session_timer_expired for useage */
143 sta->timer_to_tid[i] = i;
144 /* tid to tx queue: initialize according to HW (0 is valid) */
145 sta->tid_to_tx_q[i] = local->hw.queues;
146 /* rx timers */
147 sta->ampdu_mlme.tid_rx[i].session_timer.function =
148 sta_rx_agg_session_timer_expired;
149 sta->ampdu_mlme.tid_rx[i].session_timer.data =
150 (unsigned long)&sta->timer_to_tid[i];
151 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
152 /* tx timers */
153 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
154 sta_addba_resp_timer_expired;
155 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
156 (unsigned long)&sta->timer_to_tid[i];
157 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
158 }
159 skb_queue_head_init(&sta->ps_tx_buf);
160 skb_queue_head_init(&sta->tx_filtered);
161 __sta_info_get(sta); /* sta used by caller, decremented by
162 * sta_info_put() */
163 write_lock_bh(&local->sta_lock);
164 list_add(&sta->list, &local->sta_list);
165 local->num_sta++;
166 sta_info_hash_add(local, sta);
167 if (local->ops->sta_notify) {
168 struct ieee80211_sub_if_data *sdata;
169
170 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
171 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
172 sdata = sdata->u.vlan.ap;
173
174 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
175 STA_NOTIFY_ADD, addr);
176 }
177 write_unlock_bh(&local->sta_lock);
178
179 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
180 printk(KERN_DEBUG "%s: Added STA %s\n",
181 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
182 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
183
184 #ifdef CONFIG_MAC80211_DEBUGFS
185 /* debugfs entry adding might sleep, so schedule process
186 * context task for adding entry for STAs that do not yet
187 * have one. */
188 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
189 #endif
190
191 return sta;
192 }
193
194 /* Caller must hold local->sta_lock */
195 void sta_info_remove(struct sta_info *sta)
196 {
197 struct ieee80211_local *local = sta->local;
198 struct ieee80211_sub_if_data *sdata;
199
200 /* don't do anything if we've been removed already */
201 if (sta_info_hash_del(local, sta))
202 return;
203
204 list_del(&sta->list);
205 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
206 if (sta->flags & WLAN_STA_PS) {
207 sta->flags &= ~WLAN_STA_PS;
208 if (sdata->bss)
209 atomic_dec(&sdata->bss->num_sta_ps);
210 }
211 local->num_sta--;
212 sta_info_remove_aid_ptr(sta);
213
214 }
215
216 void sta_info_free(struct sta_info *sta)
217 {
218 struct sk_buff *skb;
219 struct ieee80211_local *local = sta->local;
220 DECLARE_MAC_BUF(mac);
221
222 might_sleep();
223
224 write_lock_bh(&local->sta_lock);
225 sta_info_remove(sta);
226 write_unlock_bh(&local->sta_lock);
227
228 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
229 local->total_ps_buffered--;
230 dev_kfree_skb(skb);
231 }
232 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
233 dev_kfree_skb(skb);
234 }
235
236 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
237 printk(KERN_DEBUG "%s: Removed STA %s\n",
238 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
239 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
240
241 ieee80211_key_free(sta->key);
242 sta->key = NULL;
243
244 if (local->ops->sta_notify) {
245 struct ieee80211_sub_if_data *sdata;
246
247 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
248
249 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
250 sdata = sdata->u.vlan.ap;
251
252 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
253 STA_NOTIFY_REMOVE, sta->addr);
254 }
255
256 rate_control_remove_sta_debugfs(sta);
257 ieee80211_sta_debugfs_remove(sta);
258
259 sta_info_put(sta);
260 }
261
262
263 static inline int sta_info_buffer_expired(struct ieee80211_local *local,
264 struct sta_info *sta,
265 struct sk_buff *skb)
266 {
267 struct ieee80211_tx_packet_data *pkt_data;
268 int timeout;
269
270 if (!skb)
271 return 0;
272
273 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
274
275 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
276 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
277 15625) * HZ;
278 if (timeout < STA_TX_BUFFER_EXPIRE)
279 timeout = STA_TX_BUFFER_EXPIRE;
280 return time_after(jiffies, pkt_data->jiffies + timeout);
281 }
282
283
284 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
285 struct sta_info *sta)
286 {
287 unsigned long flags;
288 struct sk_buff *skb;
289 DECLARE_MAC_BUF(mac);
290
291 if (skb_queue_empty(&sta->ps_tx_buf))
292 return;
293
294 for (;;) {
295 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
296 skb = skb_peek(&sta->ps_tx_buf);
297 if (sta_info_buffer_expired(local, sta, skb)) {
298 skb = __skb_dequeue(&sta->ps_tx_buf);
299 if (skb_queue_empty(&sta->ps_tx_buf))
300 sta->flags &= ~WLAN_STA_TIM;
301 } else
302 skb = NULL;
303 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
304
305 if (skb) {
306 local->total_ps_buffered--;
307 printk(KERN_DEBUG "Buffered frame expired (STA "
308 "%s)\n", print_mac(mac, sta->addr));
309 dev_kfree_skb(skb);
310 } else
311 break;
312 }
313 }
314
315
316 static void sta_info_cleanup(unsigned long data)
317 {
318 struct ieee80211_local *local = (struct ieee80211_local *) data;
319 struct sta_info *sta;
320
321 read_lock_bh(&local->sta_lock);
322 list_for_each_entry(sta, &local->sta_list, list) {
323 __sta_info_get(sta);
324 sta_info_cleanup_expire_buffered(local, sta);
325 sta_info_put(sta);
326 }
327 read_unlock_bh(&local->sta_lock);
328
329 local->sta_cleanup.expires =
330 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
331 add_timer(&local->sta_cleanup);
332 }
333
334 #ifdef CONFIG_MAC80211_DEBUGFS
335 static void sta_info_debugfs_add_task(struct work_struct *work)
336 {
337 struct ieee80211_local *local =
338 container_of(work, struct ieee80211_local, sta_debugfs_add);
339 struct sta_info *sta, *tmp;
340
341 while (1) {
342 sta = NULL;
343 read_lock_bh(&local->sta_lock);
344 list_for_each_entry(tmp, &local->sta_list, list) {
345 if (!tmp->debugfs.dir) {
346 sta = tmp;
347 __sta_info_get(sta);
348 break;
349 }
350 }
351 read_unlock_bh(&local->sta_lock);
352
353 if (!sta)
354 break;
355
356 ieee80211_sta_debugfs_add(sta);
357 rate_control_add_sta_debugfs(sta);
358 sta_info_put(sta);
359 }
360 }
361 #endif
362
363 void sta_info_init(struct ieee80211_local *local)
364 {
365 rwlock_init(&local->sta_lock);
366 INIT_LIST_HEAD(&local->sta_list);
367
368 init_timer(&local->sta_cleanup);
369 local->sta_cleanup.expires =
370 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
371 local->sta_cleanup.data = (unsigned long) local;
372 local->sta_cleanup.function = sta_info_cleanup;
373
374 #ifdef CONFIG_MAC80211_DEBUGFS
375 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
376 #endif
377 }
378
379 int sta_info_start(struct ieee80211_local *local)
380 {
381 add_timer(&local->sta_cleanup);
382 return 0;
383 }
384
385 void sta_info_stop(struct ieee80211_local *local)
386 {
387 del_timer(&local->sta_cleanup);
388 sta_info_flush(local, NULL);
389 }
390
391 void sta_info_remove_aid_ptr(struct sta_info *sta)
392 {
393 struct ieee80211_sub_if_data *sdata;
394
395 if (sta->aid <= 0)
396 return;
397
398 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
399
400 if (sdata->local->ops->set_tim)
401 sdata->local->ops->set_tim(local_to_hw(sdata->local),
402 sta->aid, 0);
403 if (sdata->bss)
404 __bss_tim_clear(sdata->bss, sta->aid);
405 }
406
407
408 /**
409 * sta_info_flush - flush matching STA entries from the STA table
410 * @local: local interface data
411 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
412 */
413 void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
414 {
415 struct sta_info *sta, *tmp;
416 LIST_HEAD(tmp_list);
417
418 write_lock_bh(&local->sta_lock);
419 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
420 if (!dev || dev == sta->dev) {
421 __sta_info_get(sta);
422 sta_info_remove(sta);
423 list_add_tail(&sta->list, &tmp_list);
424 }
425 write_unlock_bh(&local->sta_lock);
426
427 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
428 sta_info_free(sta);
429 sta_info_put(sta);
430 }
431 }