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