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