New: mac80211 stack from the wireless-dev tree
[librecmc/librecmc.git] / package / mac80211 / src / 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
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
21 #include "sta_info.h"
22 #include "debugfs_key.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 void sta_info_hash_del(struct ieee80211_local *local,
36                               struct sta_info *sta, int dls)
37 {
38         struct sta_info *s;
39
40         s = local->sta_hash[STA_HASH(sta->addr)];
41         if (!s)
42                 return;
43         if (memcmp(s->addr, sta->addr, ETH_ALEN) == 0) {
44                 if (dls && !s->dls_sta)
45                         return;
46                 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
47                 return;
48         }
49
50         while (s->hnext && memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
51                 s = s->hnext;
52         if (s->hnext) {
53                 if (dls && !s->hnext->dls_sta)
54                         return;
55                 s->hnext = s->hnext->hnext;
56         } else
57                 printk(KERN_ERR "%s: could not remove STA " MAC_FMT " from "
58                        "hash table\n", local->mdev->name, MAC_ARG(sta->addr));
59 }
60
61 static inline void __sta_info_get(struct sta_info *sta)
62 {
63         kref_get(&sta->kref);
64 }
65
66 struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
67 {
68         struct sta_info *sta;
69
70         spin_lock_bh(&local->sta_lock);
71         sta = local->sta_hash[STA_HASH(addr)];
72         while (sta) {
73                 if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
74                         __sta_info_get(sta);
75                         break;
76                 }
77                 sta = sta->hnext;
78         }
79         spin_unlock_bh(&local->sta_lock);
80
81         return sta;
82 }
83 EXPORT_SYMBOL(sta_info_get);
84
85 struct sta_info *dls_info_get(struct ieee80211_local *local, u8 *addr)
86 {
87         struct sta_info *sta;
88
89         spin_lock_bh(&local->sta_lock);
90         sta = local->sta_hash[STA_HASH(addr)];
91         while (sta) {
92                 if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
93                         if (!sta->dls_sta) {
94                                 sta = NULL;
95                                 break;
96                         }
97                         __sta_info_get(sta);
98                         break;
99                 }
100                 sta = sta->hnext;
101         }
102         spin_unlock_bh(&local->sta_lock);
103
104         return sta;
105 }
106
107 int sta_info_min_txrate_get(struct ieee80211_local *local)
108 {
109         struct sta_info *sta;
110         struct ieee80211_hw_mode *mode;
111         int min_txrate = 9999999;
112         int i;
113
114         spin_lock_bh(&local->sta_lock);
115         mode = local->oper_hw_mode;
116         for (i = 0; i < STA_HASH_SIZE; i++) {
117                 sta = local->sta_hash[i];
118                 while (sta) {
119                         if (sta->txrate < min_txrate)
120                                 min_txrate = sta->txrate;
121                         sta = sta->hnext;
122                 }
123         }
124         spin_unlock_bh(&local->sta_lock);
125         if (min_txrate == 9999999)
126                 min_txrate = 0;
127
128         return mode->rates[min_txrate].rate;
129 }
130
131
132 static void sta_info_release(struct kref *kref)
133 {
134         struct sta_info *sta = container_of(kref, struct sta_info, kref);
135         struct ieee80211_local *local = sta->local;
136         struct sk_buff *skb;
137
138         /* free sta structure; it has already been removed from
139          * hash table etc. external structures. Make sure that all
140          * buffered frames are release (one might have been added
141          * after sta_info_free() was called). */
142         while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
143                 local->total_ps_buffered--;
144                 dev_kfree_skb_any(skb);
145         }
146         while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
147                 dev_kfree_skb_any(skb);
148         }
149         rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
150         rate_control_put(sta->rate_ctrl);
151         if (sta->key)
152                 ieee80211_debugfs_key_sta_del(sta->key, sta);
153         kfree(sta);
154 }
155
156
157 void sta_info_put(struct sta_info *sta)
158 {
159         kref_put(&sta->kref, sta_info_release);
160 }
161 EXPORT_SYMBOL(sta_info_put);
162
163
164 struct sta_info * sta_info_add(struct ieee80211_local *local,
165                                struct net_device *dev, u8 *addr, gfp_t gfp)
166 {
167         struct sta_info *sta;
168
169         sta = kzalloc(sizeof(*sta), gfp);
170         if (!sta)
171                 return NULL;
172
173         kref_init(&sta->kref);
174
175         sta->rate_ctrl = rate_control_get(local->rate_ctrl);
176         sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
177         if (!sta->rate_ctrl_priv) {
178                 rate_control_put(sta->rate_ctrl);
179                 kref_put(&sta->kref, sta_info_release);
180                 kfree(sta);
181                 return NULL;
182         }
183
184         memcpy(sta->addr, addr, ETH_ALEN);
185         sta->local = local;
186         sta->dev = dev;
187         skb_queue_head_init(&sta->ps_tx_buf);
188         skb_queue_head_init(&sta->tx_filtered);
189         __sta_info_get(sta);    /* sta used by caller, decremented by
190                                  * sta_info_put() */
191         spin_lock_bh(&local->sta_lock);
192         list_add(&sta->list, &local->sta_list);
193         local->num_sta++;
194         sta_info_hash_add(local, sta);
195         spin_unlock_bh(&local->sta_lock);
196         if (local->ops->sta_table_notification)
197                 local->ops->sta_table_notification(local_to_hw(local),
198                                                   local->num_sta);
199         sta->key_idx_compression = HW_KEY_IDX_INVALID;
200
201 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
202         printk(KERN_DEBUG "%s: Added STA " MAC_FMT "\n",
203                local->mdev->name, MAC_ARG(addr));
204 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
205
206 #ifdef CONFIG_MAC80211_DEBUGFS
207         if (!in_interrupt()) {
208                 sta->debugfs_registered = 1;
209                 ieee80211_sta_debugfs_add(sta);
210                 rate_control_add_sta_debugfs(sta);
211         } else {
212                 /* debugfs entry adding might sleep, so schedule process
213                  * context task for adding entry for STAs that do not yet
214                  * have one. */
215                 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
216         }
217 #endif
218
219         return sta;
220 }
221
222 static void finish_sta_info_free(struct ieee80211_local *local,
223                                  struct sta_info *sta)
224 {
225 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
226         printk(KERN_DEBUG "%s: Removed STA " MAC_FMT "\n",
227                local->mdev->name, MAC_ARG(sta->addr));
228 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
229
230         if (sta->key) {
231                 ieee80211_debugfs_key_remove(sta->key);
232                 ieee80211_key_free(sta->key);
233                 sta->key = NULL;
234         }
235
236         rate_control_remove_sta_debugfs(sta);
237         ieee80211_sta_debugfs_remove(sta);
238
239         sta_info_put(sta);
240 }
241
242 static void sta_info_remove(struct sta_info *sta)
243 {
244         struct ieee80211_local *local = sta->local;
245         struct ieee80211_sub_if_data *sdata;
246
247         sta_info_hash_del(local, sta, 0);
248         list_del(&sta->list);
249         sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
250         if (sta->flags & WLAN_STA_PS) {
251                 sta->flags &= ~WLAN_STA_PS;
252                 if (sdata->bss)
253                         atomic_dec(&sdata->bss->num_sta_ps);
254         }
255         local->num_sta--;
256         sta_info_remove_aid_ptr(sta);
257 }
258
259 void sta_info_free(struct sta_info *sta, int locked)
260 {
261         struct sk_buff *skb;
262         struct ieee80211_local *local = sta->local;
263
264         if (!locked) {
265                 spin_lock_bh(&local->sta_lock);
266                 sta_info_remove(sta);
267                 spin_unlock_bh(&local->sta_lock);
268         } else {
269                 sta_info_remove(sta);
270         }
271         if (local->ops->sta_table_notification)
272                 local->ops->sta_table_notification(local_to_hw(local),
273                                                   local->num_sta);
274
275         while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
276                 local->total_ps_buffered--;
277                 dev_kfree_skb_any(skb);
278         }
279         while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
280                 dev_kfree_skb_any(skb);
281         }
282
283         if (sta->key) {
284                 if (local->ops->set_key) {
285                         struct ieee80211_key_conf *key;
286                         key = ieee80211_key_data2conf(local, sta->key);
287                         if (key) {
288                                 local->ops->set_key(local_to_hw(local),
289                                                    DISABLE_KEY,
290                                                    sta->addr, key, sta->aid);
291                                 kfree(key);
292                         }
293                 }
294         } else if (sta->key_idx_compression != HW_KEY_IDX_INVALID) {
295                 struct ieee80211_key_conf conf;
296                 memset(&conf, 0, sizeof(conf));
297                 conf.hw_key_idx = sta->key_idx_compression;
298                 conf.alg = ALG_NULL;
299                 conf.flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
300                 local->ops->set_key(local_to_hw(local), DISABLE_KEY,
301                                    sta->addr, &conf, sta->aid);
302                 sta->key_idx_compression = HW_KEY_IDX_INVALID;
303         }
304
305 #ifdef CONFIG_MAC80211_DEBUGFS
306         if (in_atomic()) {
307                 list_add(&sta->list, &local->deleted_sta_list);
308                 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
309         } else
310 #endif
311                 finish_sta_info_free(local, sta);
312 }
313
314
315 static inline int sta_info_buffer_expired(struct ieee80211_local *local,
316                                           struct sta_info *sta,
317                                           struct sk_buff *skb)
318 {
319         struct ieee80211_tx_packet_data *pkt_data;
320         int timeout;
321
322         if (!skb)
323                 return 0;
324
325         pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
326
327         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
328         timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
329                    15625) * HZ;
330         if (timeout < STA_TX_BUFFER_EXPIRE)
331                 timeout = STA_TX_BUFFER_EXPIRE;
332         return time_after(jiffies, pkt_data->jiffies + timeout);
333 }
334
335
336 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
337                                              struct sta_info *sta)
338 {
339         unsigned long flags;
340         struct sk_buff *skb;
341
342         if (skb_queue_empty(&sta->ps_tx_buf))
343                 return;
344
345         for (;;) {
346                 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
347                 skb = skb_peek(&sta->ps_tx_buf);
348                 if (sta_info_buffer_expired(local, sta, skb)) {
349                         skb = __skb_dequeue(&sta->ps_tx_buf);
350                         if (skb_queue_empty(&sta->ps_tx_buf))
351                                 sta->flags &= ~WLAN_STA_TIM;
352                 } else
353                         skb = NULL;
354                 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
355
356                 if (skb) {
357                         local->total_ps_buffered--;
358                         printk(KERN_DEBUG "Buffered frame expired (STA "
359                                MAC_FMT ")\n", MAC_ARG(sta->addr));
360                         dev_kfree_skb(skb);
361                 } else
362                         break;
363         }
364 }
365
366
367 static void sta_info_cleanup(unsigned long data)
368 {
369         struct ieee80211_local *local = (struct ieee80211_local *) data;
370         struct sta_info *sta;
371
372         spin_lock_bh(&local->sta_lock);
373         list_for_each_entry(sta, &local->sta_list, list) {
374                 __sta_info_get(sta);
375                 sta_info_cleanup_expire_buffered(local, sta);
376                 sta_info_put(sta);
377         }
378         spin_unlock_bh(&local->sta_lock);
379
380         local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
381         add_timer(&local->sta_cleanup);
382 }
383
384 #ifdef CONFIG_MAC80211_DEBUGFS
385 static void sta_info_debugfs_add_task(struct work_struct *work)
386 {
387         struct ieee80211_local *local =
388                 container_of(work, struct ieee80211_local, sta_debugfs_add);
389         struct sta_info *sta, *tmp;
390
391         while (1) {
392                 spin_lock_bh(&local->sta_lock);
393                 if (!list_empty(&local->deleted_sta_list)) {
394                         sta = list_entry(local->deleted_sta_list.next,
395                                          struct sta_info, list);
396                         list_del(local->deleted_sta_list.next);
397                 } else
398                         sta = NULL;
399                 spin_unlock_bh(&local->sta_lock);
400                 if (!sta)
401                         break;
402                 finish_sta_info_free(local, sta);
403         }
404
405         while (1) {
406                 sta = NULL;
407                 spin_lock_bh(&local->sta_lock);
408                 list_for_each_entry(tmp, &local->sta_list, list) {
409                         if (!tmp->debugfs_registered) {
410                                 sta = tmp;
411                                 __sta_info_get(sta);
412                                 break;
413                         }
414                 }
415                 spin_unlock_bh(&local->sta_lock);
416
417                 if (!sta)
418                         break;
419
420                 sta->debugfs_registered = 1;
421                 ieee80211_sta_debugfs_add(sta);
422                 rate_control_add_sta_debugfs(sta);
423                 sta_info_put(sta);
424         }
425 }
426 #endif
427
428 void sta_info_init(struct ieee80211_local *local)
429 {
430         spin_lock_init(&local->sta_lock);
431         INIT_LIST_HEAD(&local->sta_list);
432         INIT_LIST_HEAD(&local->deleted_sta_list);
433
434         init_timer(&local->sta_cleanup);
435         local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
436         local->sta_cleanup.data = (unsigned long) local;
437         local->sta_cleanup.function = sta_info_cleanup;
438
439 #ifdef CONFIG_MAC80211_DEBUGFS
440         INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
441 #endif
442 }
443
444 int sta_info_start(struct ieee80211_local *local)
445 {
446         add_timer(&local->sta_cleanup);
447         return 0;
448 }
449
450 void sta_info_stop(struct ieee80211_local *local)
451 {
452         struct sta_info *sta, *tmp;
453
454         del_timer(&local->sta_cleanup);
455
456         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
457                 /* sta_info_free must be called with 0 as the last
458                  * parameter to ensure all debugfs sta entries are
459                  * unregistered. We don't need locking at this
460                  * point. */
461                 sta_info_free(sta, 0);
462         }
463 }
464
465 void sta_info_remove_aid_ptr(struct sta_info *sta)
466 {
467         struct ieee80211_sub_if_data *sdata;
468
469         if (sta->aid <= 0)
470                 return;
471
472         sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
473
474         if (sdata->local->ops->set_tim)
475                 sdata->local->ops->set_tim(local_to_hw(sdata->local),
476                                           sta->aid, 0);
477         if (sdata->bss)
478                 __bss_tim_clear(sdata->bss, sta->aid);
479 }
480
481
482 /**
483  * sta_info_flush - flush matching STA entries from the STA table
484  * @local: local interface data
485  * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
486  */
487 void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
488 {
489         struct sta_info *sta, *tmp;
490
491         spin_lock_bh(&local->sta_lock);
492         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
493                 if (!dev || dev == sta->dev)
494                         sta_info_free(sta, 1);
495         spin_unlock_bh(&local->sta_lock);
496 }