mac80211: split ieee80211_txrx_data
[linux-block.git] / net / mac80211 / sta_info.c
CommitLineData
f0706e82
JB
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>
0d174406 17#include <linux/timer.h>
f0706e82
JB
18
19#include <net/mac80211.h>
20#include "ieee80211_i.h"
21#include "ieee80211_rate.h"
22#include "sta_info.h"
e9f207f0 23#include "debugfs_sta.h"
ee385855 24#include "mesh.h"
f0706e82
JB
25
26/* Caller must hold local->sta_lock */
27static 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 */
be8755e1
MW
36static int sta_info_hash_del(struct ieee80211_local *local,
37 struct sta_info *sta)
f0706e82
JB
38{
39 struct sta_info *s;
40
41 s = local->sta_hash[STA_HASH(sta->addr)];
42 if (!s)
be8755e1
MW
43 return -ENOENT;
44 if (s == sta) {
f0706e82 45 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
be8755e1 46 return 0;
f0706e82
JB
47 }
48
be8755e1 49 while (s->hnext && s->hnext != sta)
f0706e82 50 s = s->hnext;
be8755e1
MW
51 if (s->hnext) {
52 s->hnext = sta->hnext;
53 return 0;
54 }
f0706e82 55
be8755e1 56 return -ENOENT;
f0706e82
JB
57}
58
43ba7e95
JB
59/* must hold local->sta_lock */
60static struct sta_info *__sta_info_find(struct ieee80211_local *local,
61 u8 *addr)
f0706e82
JB
62{
63 struct sta_info *sta;
64
f0706e82
JB
65 sta = local->sta_hash[STA_HASH(addr)];
66 while (sta) {
43ba7e95 67 if (compare_ether_addr(sta->addr, addr) == 0)
f0706e82 68 break;
f0706e82
JB
69 sta = sta->hnext;
70 }
43ba7e95
JB
71 return sta;
72}
73
74struct 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);
be8755e1 82 read_unlock_bh(&local->sta_lock);
f0706e82
JB
83
84 return sta;
85}
86EXPORT_SYMBOL(sta_info_get);
87
ee385855
LCC
88struct 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}
f0706e82
JB
109
110static 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;
07db2183 115 int i;
f0706e82
JB
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 }
fe3bf0f5 128 for (i = 0; i < STA_TID_NUM; i++) {
07db2183 129 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
fe3bf0f5
RR
130 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
131 }
f0706e82
JB
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
138void sta_info_put(struct sta_info *sta)
139{
140 kref_put(&sta->kref, sta_info_release);
141}
142EXPORT_SYMBOL(sta_info_put);
143
144
43ba7e95
JB
145struct sta_info *sta_info_add(struct ieee80211_local *local,
146 struct net_device *dev, u8 *addr, gfp_t gfp)
f0706e82
JB
147{
148 struct sta_info *sta;
16c5f15c 149 int i;
0795af57 150 DECLARE_MAC_BUF(mac);
f0706e82
JB
151
152 sta = kzalloc(sizeof(*sta), gfp);
153 if (!sta)
43ba7e95 154 return ERR_PTR(-ENOMEM);
f0706e82
JB
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);
f0706e82 162 kfree(sta);
43ba7e95 163 return ERR_PTR(-ENOMEM);
f0706e82
JB
164 }
165
166 memcpy(sta->addr, addr, ETH_ALEN);
167 sta->local = local;
168 sta->dev = dev;
16c5f15c 169 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
fe3bf0f5 170 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
16c5f15c
RR
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;
fe3bf0f5
RR
176 /* tid to tx queue: initialize according to HW (0 is valid) */
177 sta->tid_to_tx_q[i] = local->hw.queues;
16c5f15c
RR
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);
fe3bf0f5
RR
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);
16c5f15c 190 }
f0706e82
JB
191 skb_queue_head_init(&sta->ps_tx_buf);
192 skb_queue_head_init(&sta->tx_filtered);
be8755e1 193 write_lock_bh(&local->sta_lock);
43ba7e95
JB
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 }
f0706e82
JB
202 list_add(&sta->list, &local->sta_list);
203 local->num_sta++;
204 sta_info_hash_add(local, sta);
32bfd35d
JB
205 if (local->ops->sta_notify) {
206 struct ieee80211_sub_if_data *sdata;
207
208 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
51fb61e7 209 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
32bfd35d
JB
210 sdata = sdata->u.vlan.ap;
211
212 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
213 STA_NOTIFY_ADD, addr);
214 }
be8755e1 215 write_unlock_bh(&local->sta_lock);
f0706e82
JB
216
217#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0795af57 218 printk(KERN_DEBUG "%s: Added STA %s\n",
dd1cd4c6 219 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
f0706e82
JB
220#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
221
e9f207f0 222#ifdef CONFIG_MAC80211_DEBUGFS
be8755e1
MW
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);
e9f207f0
JB
227#endif
228
f0706e82
JB
229 return sta;
230}
231
004c872e
JB
232static 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
241static 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
250static 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
259void 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
270static 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
279void 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
be8755e1
MW
290/* Caller must hold local->sta_lock */
291void sta_info_remove(struct sta_info *sta)
f0706e82
JB
292{
293 struct ieee80211_local *local = sta->local;
294 struct ieee80211_sub_if_data *sdata;
295
be8755e1
MW
296 /* don't do anything if we've been removed already */
297 if (sta_info_hash_del(local, sta))
298 return;
299
f0706e82
JB
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);
004c872e 306 __sta_info_clear_tim_bit(sdata->bss, sta);
f0706e82
JB
307 }
308 local->num_sta--;
ee385855 309
902acc78 310 if (ieee80211_vif_is_mesh(&sdata->vif))
ee385855 311 mesh_accept_plinks_update(sdata->dev);
f0706e82
JB
312}
313
be8755e1 314void sta_info_free(struct sta_info *sta)
f0706e82
JB
315{
316 struct sk_buff *skb;
317 struct ieee80211_local *local = sta->local;
ee385855
LCC
318 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
319
0795af57 320 DECLARE_MAC_BUF(mac);
f0706e82 321
be8755e1
MW
322 might_sleep();
323
324 write_lock_bh(&local->sta_lock);
325 sta_info_remove(sta);
326 write_unlock_bh(&local->sta_lock);
f0706e82 327
902acc78 328 if (ieee80211_vif_is_mesh(&sdata->vif))
ee385855 329 mesh_plink_deactivate(sta);
ee385855 330
f0706e82
JB
331 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
332 local->total_ps_buffered--;
be8755e1 333 dev_kfree_skb(skb);
f0706e82
JB
334 }
335 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
be8755e1 336 dev_kfree_skb(skb);
f0706e82
JB
337 }
338
be8755e1 339#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0795af57 340 printk(KERN_DEBUG "%s: Removed STA %s\n",
dd1cd4c6 341 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
be8755e1
MW
342#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
343
11a843b7 344 ieee80211_key_free(sta->key);
db4d1169 345 WARN_ON(sta->key);
be8755e1 346
32bfd35d 347 if (local->ops->sta_notify) {
32bfd35d 348
51fb61e7 349 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
32bfd35d
JB
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 }
478f8d2b 355
be8755e1
MW
356 rate_control_remove_sta_debugfs(sta);
357 ieee80211_sta_debugfs_remove(sta);
358
359 sta_info_put(sta);
f0706e82
JB
360}
361
362
363static 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
384static 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;
836341a7 389 struct ieee80211_sub_if_data *sdata;
0795af57 390 DECLARE_MAC_BUF(mac);
f0706e82
JB
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);
836341a7 398 if (sta_info_buffer_expired(local, sta, skb))
f0706e82 399 skb = __skb_dequeue(&sta->ps_tx_buf);
836341a7 400 else
f0706e82
JB
401 skb = NULL;
402 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
403
836341a7 404 if (!skb)
f0706e82 405 break;
836341a7
JB
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
004c872e
JB
413 if (skb_queue_empty(&sta->ps_tx_buf))
414 sta_info_clear_tim_bit(sta);
f0706e82
JB
415 }
416}
417
418
419static void sta_info_cleanup(unsigned long data)
420{
421 struct ieee80211_local *local = (struct ieee80211_local *) data;
422 struct sta_info *sta;
423
be8755e1 424 read_lock_bh(&local->sta_lock);
f0706e82
JB
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 }
be8755e1 430 read_unlock_bh(&local->sta_lock);
f0706e82 431
0d174406
JB
432 local->sta_cleanup.expires =
433 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
f0706e82
JB
434 add_timer(&local->sta_cleanup);
435}
436
e9f207f0
JB
437#ifdef CONFIG_MAC80211_DEBUGFS
438static 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
e9f207f0
JB
444 while (1) {
445 sta = NULL;
be8755e1 446 read_lock_bh(&local->sta_lock);
e9f207f0 447 list_for_each_entry(tmp, &local->sta_list, list) {
be8755e1 448 if (!tmp->debugfs.dir) {
e9f207f0
JB
449 sta = tmp;
450 __sta_info_get(sta);
451 break;
452 }
453 }
be8755e1 454 read_unlock_bh(&local->sta_lock);
e9f207f0
JB
455
456 if (!sta)
457 break;
458
e9f207f0
JB
459 ieee80211_sta_debugfs_add(sta);
460 rate_control_add_sta_debugfs(sta);
461 sta_info_put(sta);
462 }
463}
464#endif
465
f0706e82
JB
466void sta_info_init(struct ieee80211_local *local)
467{
be8755e1 468 rwlock_init(&local->sta_lock);
f0706e82 469 INIT_LIST_HEAD(&local->sta_list);
f0706e82 470
b24b8a24
PE
471 setup_timer(&local->sta_cleanup, sta_info_cleanup,
472 (unsigned long)local);
0d174406
JB
473 local->sta_cleanup.expires =
474 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
e9f207f0
JB
475
476#ifdef CONFIG_MAC80211_DEBUGFS
477 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
478#endif
f0706e82
JB
479}
480
481int sta_info_start(struct ieee80211_local *local)
482{
483 add_timer(&local->sta_cleanup);
484 return 0;
485}
486
487void sta_info_stop(struct ieee80211_local *local)
488{
f0706e82 489 del_timer(&local->sta_cleanup);
be8755e1 490 sta_info_flush(local, NULL);
f0706e82
JB
491}
492
f0706e82
JB
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 */
498void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
499{
500 struct sta_info *sta, *tmp;
be8755e1 501 LIST_HEAD(tmp_list);
f0706e82 502
be8755e1 503 write_lock_bh(&local->sta_lock);
f0706e82 504 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
be8755e1
MW
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 }
f0706e82 516}