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