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