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