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