blob: ee5b66abc0f1df7e8eecd9a5b533a2df46b55701 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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>
Johannes Berg0d174402007-12-17 15:07:43 +010017#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010018#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070019
20#include <net/mac80211.h>
21#include "ieee80211_i.h"
22#include "ieee80211_rate.h"
23#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070024#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010025#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070026
Johannes Bergd0709a62008-02-25 16:27:46 +010027/**
28 * DOC: STA information lifetime rules
29 *
30 * STA info structures (&struct sta_info) are managed in a hash table
31 * for faster lookup and a list for iteration. They are managed using
32 * RCU, i.e. access to the list and hash table is protected by RCU.
33 *
34 * STA info structures are always "alive" when they are added with
35 * @sta_info_add() [this may be changed in the future to allow allocating
36 * outside of a critical section!], they are then added to the hash
37 * table and list. Therefore, @sta_info_add() must also be RCU protected,
38 * also, the caller of @sta_info_add() cannot assume that it owns the
39 * structure.
40 *
41 * Because there are debugfs entries for each station, and adding those
42 * must be able to sleep, it is also possible to "pin" a station entry,
43 * that means it can be removed from the hash table but not be freed.
44 * See the comment in @__sta_info_unlink() for more information.
45 *
46 * In order to remove a STA info structure, the caller needs to first
47 * unlink it (@sta_info_unlink()) from the list and hash tables and
48 * then wait for an RCU synchronisation before it can be freed. Due to
49 * the pinning and the possibility of multiple callers trying to remove
50 * the same STA info at the same time, @sta_info_unlink() can clear the
51 * STA info pointer it is passed to indicate that the STA info is owned
52 * by somebody else now.
53 *
54 * If @sta_info_unlink() did not clear the pointer then the caller owns
55 * the STA info structure now and is responsible of destroying it with
56 * a call to @sta_info_destroy(), not before RCU synchronisation, of
57 * course. Note that sta_info_destroy() must be protected by the RTNL.
58 *
59 * In all other cases, there is no concept of ownership on a STA entry,
60 * each structure is owned by the global hash table/list until it is
61 * removed. All users of the structure need to be RCU protected so that
62 * the structure won't be freed before they are done using it.
63 */
Jiri Bencf0706e82007-05-05 11:45:53 -070064
65/* Caller must hold local->sta_lock */
Michael Wube8755e2007-07-27 15:43:23 +020066static int sta_info_hash_del(struct ieee80211_local *local,
67 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070068{
69 struct sta_info *s;
70
71 s = local->sta_hash[STA_HASH(sta->addr)];
72 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020073 return -ENOENT;
74 if (s == sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +010075 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)],
76 s->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020077 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070078 }
79
Michael Wube8755e2007-07-27 15:43:23 +020080 while (s->hnext && s->hnext != sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070081 s = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020082 if (s->hnext) {
Johannes Bergd0709a62008-02-25 16:27:46 +010083 rcu_assign_pointer(s->hnext, sta->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020084 return 0;
85 }
Jiri Bencf0706e82007-05-05 11:45:53 -070086
Michael Wube8755e2007-07-27 15:43:23 +020087 return -ENOENT;
Jiri Bencf0706e82007-05-05 11:45:53 -070088}
89
Johannes Bergd0709a62008-02-25 16:27:46 +010090/* protected by RCU */
Johannes Berg43ba7e92008-02-21 14:09:30 +010091static struct sta_info *__sta_info_find(struct ieee80211_local *local,
92 u8 *addr)
93{
94 struct sta_info *sta;
95
Johannes Bergd0709a62008-02-25 16:27:46 +010096 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
Johannes Berg43ba7e92008-02-21 14:09:30 +010097 while (sta) {
98 if (compare_ether_addr(sta->addr, addr) == 0)
99 break;
Johannes Bergd0709a62008-02-25 16:27:46 +0100100 sta = rcu_dereference(sta->hnext);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100101 }
102 return sta;
103}
104
Jiri Bencf0706e82007-05-05 11:45:53 -0700105struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
106{
Johannes Bergd0709a62008-02-25 16:27:46 +0100107 return __sta_info_find(local, addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700108}
109EXPORT_SYMBOL(sta_info_get);
110
Luis Carlos Coboee385852008-02-23 15:17:11 +0100111struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
112 struct net_device *dev)
113{
114 struct sta_info *sta;
115 int i = 0;
116
Johannes Bergd0709a62008-02-25 16:27:46 +0100117 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Luis Carlos Coboee385852008-02-23 15:17:11 +0100118 if (i < idx) {
119 ++i;
120 continue;
Johannes Bergd0709a62008-02-25 16:27:46 +0100121 } else if (!dev || dev == sta->sdata->dev) {
Luis Carlos Coboee385852008-02-23 15:17:11 +0100122 return sta;
123 }
124 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100125
126 return NULL;
127}
Jiri Bencf0706e82007-05-05 11:45:53 -0700128
Johannes Bergd0709a62008-02-25 16:27:46 +0100129void sta_info_destroy(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700130{
Jiri Bencf0706e82007-05-05 11:45:53 -0700131 struct ieee80211_local *local = sta->local;
132 struct sk_buff *skb;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200133 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700134
Johannes Bergd0709a62008-02-25 16:27:46 +0100135 ASSERT_RTNL();
136 might_sleep();
137
138 rate_control_remove_sta_debugfs(sta);
139 ieee80211_sta_debugfs_remove(sta);
140
141#ifdef CONFIG_MAC80211_MESH
142 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
143 mesh_plink_deactivate(sta);
144#endif
145
146 /*
147 * NOTE: This will call synchronize_rcu() internally to
148 * make sure no key references can be in use. We rely on
149 * that here for the mesh code!
150 */
151 ieee80211_key_free(sta->key);
152 WARN_ON(sta->key);
153
154#ifdef CONFIG_MAC80211_MESH
155 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
156 del_timer_sync(&sta->plink_timer);
157#endif
158
Jiri Bencf0706e82007-05-05 11:45:53 -0700159 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
160 local->total_ps_buffered--;
161 dev_kfree_skb_any(skb);
162 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100163
164 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
Jiri Bencf0706e82007-05-05 11:45:53 -0700165 dev_kfree_skb_any(skb);
Johannes Bergd0709a62008-02-25 16:27:46 +0100166
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200167 for (i = 0; i < STA_TID_NUM; i++) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200168 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200169 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
170 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700171 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
172 rate_control_put(sta->rate_ctrl);
Johannes Bergd0709a62008-02-25 16:27:46 +0100173
Jiri Bencf0706e82007-05-05 11:45:53 -0700174 kfree(sta);
175}
176
177
Johannes Bergd0709a62008-02-25 16:27:46 +0100178/* Caller must hold local->sta_lock */
179static void sta_info_hash_add(struct ieee80211_local *local,
180 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700181{
Johannes Bergd0709a62008-02-25 16:27:46 +0100182 sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
183 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)], sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700184}
Jiri Bencf0706e82007-05-05 11:45:53 -0700185
Johannes Bergd0709a62008-02-25 16:27:46 +0100186struct sta_info *sta_info_add(struct ieee80211_sub_if_data *sdata,
187 u8 *addr)
Jiri Bencf0706e82007-05-05 11:45:53 -0700188{
Johannes Bergd0709a62008-02-25 16:27:46 +0100189 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700190 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200191 int i;
Joe Perches0795af52007-10-03 17:59:30 -0700192 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +0100193 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -0700194
Johannes Bergd0709a62008-02-25 16:27:46 +0100195 sta = kzalloc(sizeof(*sta), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -0700196 if (!sta)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100197 return ERR_PTR(-ENOMEM);
Jiri Bencf0706e82007-05-05 11:45:53 -0700198
Johannes Bergd0709a62008-02-25 16:27:46 +0100199 memcpy(sta->addr, addr, ETH_ALEN);
200 sta->local = local;
201 sta->sdata = sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700202
203 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
Johannes Bergd0709a62008-02-25 16:27:46 +0100204 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
205 GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -0700206 if (!sta->rate_ctrl_priv) {
207 rate_control_put(sta->rate_ctrl);
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 kfree(sta);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100209 return ERR_PTR(-ENOMEM);
Jiri Bencf0706e82007-05-05 11:45:53 -0700210 }
211
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200212 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200213 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200214 for (i = 0; i < STA_TID_NUM; i++) {
215 /* timer_to_tid must be initialized with identity mapping to
216 * enable session_timer's data differentiation. refer to
217 * sta_rx_agg_session_timer_expired for useage */
218 sta->timer_to_tid[i] = i;
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200219 /* tid to tx queue: initialize according to HW (0 is valid) */
220 sta->tid_to_tx_q[i] = local->hw.queues;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200221 /* rx timers */
222 sta->ampdu_mlme.tid_rx[i].session_timer.function =
223 sta_rx_agg_session_timer_expired;
224 sta->ampdu_mlme.tid_rx[i].session_timer.data =
225 (unsigned long)&sta->timer_to_tid[i];
226 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200227 /* tx timers */
228 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
229 sta_addba_resp_timer_expired;
230 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
231 (unsigned long)&sta->timer_to_tid[i];
232 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200233 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700234 skb_queue_head_init(&sta->ps_tx_buf);
235 skb_queue_head_init(&sta->tx_filtered);
Johannes Bergd0709a62008-02-25 16:27:46 +0100236 spin_lock_irqsave(&local->sta_lock, flags);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100237 /* check if STA exists already */
238 if (__sta_info_find(local, addr)) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100239 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100240 return ERR_PTR(-EEXIST);
241 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700242 list_add(&sta->list, &local->sta_list);
243 local->num_sta++;
244 sta_info_hash_add(local, sta);
Johannes Berg32bfd352007-12-19 01:31:26 +0100245
Johannes Bergd0709a62008-02-25 16:27:46 +0100246 /* notify driver */
247 if (local->ops->sta_notify) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100248 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
Johannes Berg32bfd352007-12-19 01:31:26 +0100249 sdata = sdata->u.vlan.ap;
250
251 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
252 STA_NOTIFY_ADD, addr);
253 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100254
255 spin_unlock_irqrestore(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700256
257#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700258 printk(KERN_DEBUG "%s: Added STA %s\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400259 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700260#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
261
Jiri Bence9f207f2007-05-05 11:46:38 -0700262#ifdef CONFIG_MAC80211_DEBUGFS
Michael Wube8755e2007-07-27 15:43:23 +0200263 /* debugfs entry adding might sleep, so schedule process
264 * context task for adding entry for STAs that do not yet
265 * have one. */
266 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700267#endif
268
Jiri Bencf0706e82007-05-05 11:45:53 -0700269 return sta;
270}
271
Johannes Berg004c8722008-02-20 11:21:35 +0100272static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
273{
274 /*
275 * This format has been mandated by the IEEE specifications,
276 * so this line may not be changed to use the __set_bit() format.
277 */
278 bss->tim[aid / 8] |= (1 << (aid % 8));
279}
280
281static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
282{
283 /*
284 * This format has been mandated by the IEEE specifications,
285 * so this line may not be changed to use the __clear_bit() format.
286 */
287 bss->tim[aid / 8] &= ~(1 << (aid % 8));
288}
289
290static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
291 struct sta_info *sta)
292{
293 if (bss)
294 __bss_tim_set(bss, sta->aid);
Johannes Bergd0709a62008-02-25 16:27:46 +0100295 if (sta->local->ops->set_tim) {
296 sta->local->tim_in_locked_section = true;
Johannes Berg004c8722008-02-20 11:21:35 +0100297 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1);
Johannes Bergd0709a62008-02-25 16:27:46 +0100298 sta->local->tim_in_locked_section = false;
299 }
Johannes Berg004c8722008-02-20 11:21:35 +0100300}
301
302void sta_info_set_tim_bit(struct sta_info *sta)
303{
Johannes Bergd0709a62008-02-25 16:27:46 +0100304 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100305
Johannes Bergd0709a62008-02-25 16:27:46 +0100306 spin_lock_irqsave(&sta->local->sta_lock, flags);
307 __sta_info_set_tim_bit(sta->sdata->bss, sta);
308 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100309}
310
311static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
312 struct sta_info *sta)
313{
314 if (bss)
315 __bss_tim_clear(bss, sta->aid);
Johannes Bergd0709a62008-02-25 16:27:46 +0100316 if (sta->local->ops->set_tim) {
317 sta->local->tim_in_locked_section = true;
Johannes Berg004c8722008-02-20 11:21:35 +0100318 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0);
Johannes Bergd0709a62008-02-25 16:27:46 +0100319 sta->local->tim_in_locked_section = false;
320 }
Johannes Berg004c8722008-02-20 11:21:35 +0100321}
322
323void sta_info_clear_tim_bit(struct sta_info *sta)
324{
Johannes Bergd0709a62008-02-25 16:27:46 +0100325 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100326
Johannes Bergd0709a62008-02-25 16:27:46 +0100327 spin_lock_irqsave(&sta->local->sta_lock, flags);
328 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
329 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100330}
331
Johannes Bergd0709a62008-02-25 16:27:46 +0100332/*
333 * See comment in __sta_info_unlink,
334 * caller must hold local->sta_lock.
335 */
336static void __sta_info_pin(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700337{
Johannes Bergd0709a62008-02-25 16:27:46 +0100338 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
339 sta->pin_status = STA_INFO_PIN_STAT_PINNED;
340}
Jiri Bencf0706e82007-05-05 11:45:53 -0700341
Johannes Bergd0709a62008-02-25 16:27:46 +0100342/*
343 * See comment in __sta_info_unlink, returns sta if it
344 * needs to be destroyed.
345 */
346static struct sta_info *__sta_info_unpin(struct sta_info *sta)
347{
348 struct sta_info *ret = NULL;
349 unsigned long flags;
350
351 spin_lock_irqsave(&sta->local->sta_lock, flags);
352 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
353 sta->pin_status != STA_INFO_PIN_STAT_PINNED);
354 if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
355 ret = sta;
356 sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
357 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
358
359 return ret;
360}
361
362static void __sta_info_unlink(struct sta_info **sta)
363{
364 struct ieee80211_local *local = (*sta)->local;
365 struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
366#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
367 DECLARE_MAC_BUF(mbuf);
368#endif
369 /*
370 * pull caller's reference if we're already gone.
371 */
372 if (sta_info_hash_del(local, *sta)) {
373 *sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200374 return;
Johannes Bergd0709a62008-02-25 16:27:46 +0100375 }
Michael Wube8755e2007-07-27 15:43:23 +0200376
Johannes Bergd0709a62008-02-25 16:27:46 +0100377 /*
378 * Also pull caller's reference if the STA is pinned by the
379 * task that is adding the debugfs entries. In that case, we
380 * leave the STA "to be freed".
381 *
382 * The rules are not trivial, but not too complex either:
383 * (1) pin_status is only modified under the sta_lock
384 * (2) sta_info_debugfs_add_work() will set the status
385 * to PINNED when it found an item that needs a new
386 * debugfs directory created. In that case, that item
387 * must not be freed although all *RCU* users are done
388 * with it. Hence, we tell the caller of _unlink()
389 * that the item is already gone (as can happen when
390 * two tasks try to unlink/destroy at the same time)
391 * (3) We set the pin_status to DESTROY here when we
392 * find such an item.
393 * (4) sta_info_debugfs_add_work() will reset the pin_status
394 * from PINNED to NORMAL when it is done with the item,
395 * but will check for DESTROY before resetting it in
396 * which case it will free the item.
397 */
398 if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
399 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
400 *sta = NULL;
401 return;
402 }
403
404 list_del(&(*sta)->list);
405
406 if ((*sta)->flags & WLAN_STA_PS) {
407 (*sta)->flags &= ~WLAN_STA_PS;
Jiri Bencf0706e82007-05-05 11:45:53 -0700408 if (sdata->bss)
409 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Bergd0709a62008-02-25 16:27:46 +0100410 __sta_info_clear_tim_bit(sdata->bss, *sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700411 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100412
Jiri Bencf0706e82007-05-05 11:45:53 -0700413 local->num_sta--;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100414
Johannes Berg32bfd352007-12-19 01:31:26 +0100415 if (local->ops->sta_notify) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100416 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
Johannes Berg32bfd352007-12-19 01:31:26 +0100417 sdata = sdata->u.vlan.ap;
418
419 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
Johannes Bergd0709a62008-02-25 16:27:46 +0100420 STA_NOTIFY_REMOVE, (*sta)->addr);
Johannes Berg32bfd352007-12-19 01:31:26 +0100421 }
Tomas Winkler478f8d22007-09-30 13:52:37 +0200422
Johannes Bergd0709a62008-02-25 16:27:46 +0100423 if (ieee80211_vif_is_mesh(&sdata->vif)) {
424 mesh_accept_plinks_update(sdata);
425#ifdef CONFIG_MAC80211_MESH
426 del_timer(&(*sta)->plink_timer);
427#endif
428 }
Michael Wube8755e2007-07-27 15:43:23 +0200429
Johannes Bergd0709a62008-02-25 16:27:46 +0100430#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
431 printk(KERN_DEBUG "%s: Removed STA %s\n",
432 wiphy_name(local->hw.wiphy), print_mac(mbuf, (*sta)->addr));
433#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
Jiri Bencf0706e82007-05-05 11:45:53 -0700434}
435
Johannes Bergd0709a62008-02-25 16:27:46 +0100436void sta_info_unlink(struct sta_info **sta)
437{
438 struct ieee80211_local *local = (*sta)->local;
439 unsigned long flags;
440
441 spin_lock_irqsave(&local->sta_lock, flags);
442 __sta_info_unlink(sta);
443 spin_unlock_irqrestore(&local->sta_lock, flags);
444}
Jiri Bencf0706e82007-05-05 11:45:53 -0700445
446static inline int sta_info_buffer_expired(struct ieee80211_local *local,
447 struct sta_info *sta,
448 struct sk_buff *skb)
449{
450 struct ieee80211_tx_packet_data *pkt_data;
451 int timeout;
452
453 if (!skb)
454 return 0;
455
456 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
457
458 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
459 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
460 15625) * HZ;
461 if (timeout < STA_TX_BUFFER_EXPIRE)
462 timeout = STA_TX_BUFFER_EXPIRE;
463 return time_after(jiffies, pkt_data->jiffies + timeout);
464}
465
466
467static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
468 struct sta_info *sta)
469{
470 unsigned long flags;
471 struct sk_buff *skb;
Johannes Berg836341a2008-02-20 02:07:21 +0100472 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700473 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700474
475 if (skb_queue_empty(&sta->ps_tx_buf))
476 return;
477
478 for (;;) {
479 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
480 skb = skb_peek(&sta->ps_tx_buf);
Johannes Berg836341a2008-02-20 02:07:21 +0100481 if (sta_info_buffer_expired(local, sta, skb))
Jiri Bencf0706e82007-05-05 11:45:53 -0700482 skb = __skb_dequeue(&sta->ps_tx_buf);
Johannes Berg836341a2008-02-20 02:07:21 +0100483 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700484 skb = NULL;
485 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
486
Johannes Berg836341a2008-02-20 02:07:21 +0100487 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700488 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100489
Johannes Bergd0709a62008-02-25 16:27:46 +0100490 sdata = sta->sdata;
Johannes Berg836341a2008-02-20 02:07:21 +0100491 local->total_ps_buffered--;
492 printk(KERN_DEBUG "Buffered frame expired (STA "
493 "%s)\n", print_mac(mac, sta->addr));
494 dev_kfree_skb(skb);
495
Johannes Berg004c8722008-02-20 11:21:35 +0100496 if (skb_queue_empty(&sta->ps_tx_buf))
497 sta_info_clear_tim_bit(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700498 }
499}
500
501
502static void sta_info_cleanup(unsigned long data)
503{
504 struct ieee80211_local *local = (struct ieee80211_local *) data;
505 struct sta_info *sta;
506
Johannes Bergd0709a62008-02-25 16:27:46 +0100507 rcu_read_lock();
508 list_for_each_entry_rcu(sta, &local->sta_list, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700509 sta_info_cleanup_expire_buffered(local, sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100510 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700511
Johannes Berg0d174402007-12-17 15:07:43 +0100512 local->sta_cleanup.expires =
513 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700514 add_timer(&local->sta_cleanup);
515}
516
Jiri Bence9f207f2007-05-05 11:46:38 -0700517#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Bergd0709a62008-02-25 16:27:46 +0100518static void sta_info_debugfs_add_work(struct work_struct *work)
Jiri Bence9f207f2007-05-05 11:46:38 -0700519{
520 struct ieee80211_local *local =
521 container_of(work, struct ieee80211_local, sta_debugfs_add);
522 struct sta_info *sta, *tmp;
Johannes Bergd0709a62008-02-25 16:27:46 +0100523 unsigned long flags;
Jiri Bence9f207f2007-05-05 11:46:38 -0700524
525 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700526 sta = NULL;
Johannes Bergd0709a62008-02-25 16:27:46 +0100527
528 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700529 list_for_each_entry(tmp, &local->sta_list, list) {
Michael Wube8755e2007-07-27 15:43:23 +0200530 if (!tmp->debugfs.dir) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700531 sta = tmp;
Johannes Bergd0709a62008-02-25 16:27:46 +0100532 __sta_info_pin(sta);
Jiri Bence9f207f2007-05-05 11:46:38 -0700533 break;
534 }
535 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100536 spin_unlock_irqrestore(&local->sta_lock, flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700537
538 if (!sta)
539 break;
540
Jiri Bence9f207f2007-05-05 11:46:38 -0700541 ieee80211_sta_debugfs_add(sta);
542 rate_control_add_sta_debugfs(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100543
544 sta = __sta_info_unpin(sta);
545
546 if (sta) {
547 synchronize_rcu();
548 sta_info_destroy(sta);
549 }
Jiri Bence9f207f2007-05-05 11:46:38 -0700550 }
551}
552#endif
553
Jiri Bencf0706e82007-05-05 11:45:53 -0700554void sta_info_init(struct ieee80211_local *local)
555{
Johannes Bergd0709a62008-02-25 16:27:46 +0100556 spin_lock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700557 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700558
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800559 setup_timer(&local->sta_cleanup, sta_info_cleanup,
560 (unsigned long)local);
Johannes Berg0d174402007-12-17 15:07:43 +0100561 local->sta_cleanup.expires =
562 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bence9f207f2007-05-05 11:46:38 -0700563
564#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Bergd0709a62008-02-25 16:27:46 +0100565 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
Jiri Bence9f207f2007-05-05 11:46:38 -0700566#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700567}
568
569int sta_info_start(struct ieee80211_local *local)
570{
571 add_timer(&local->sta_cleanup);
572 return 0;
573}
574
575void sta_info_stop(struct ieee80211_local *local)
576{
Jiri Bencf0706e82007-05-05 11:45:53 -0700577 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200578 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700579}
580
Jiri Bencf0706e82007-05-05 11:45:53 -0700581/**
582 * sta_info_flush - flush matching STA entries from the STA table
583 * @local: local interface data
Johannes Bergd0709a62008-02-25 16:27:46 +0100584 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
Jiri Bencf0706e82007-05-05 11:45:53 -0700585 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100586void sta_info_flush(struct ieee80211_local *local,
587 struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -0700588{
589 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200590 LIST_HEAD(tmp_list);
Johannes Bergd0709a62008-02-25 16:27:46 +0100591 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -0700592
Johannes Bergd0709a62008-02-25 16:27:46 +0100593 might_sleep();
594
595 spin_lock_irqsave(&local->sta_lock, flags);
596 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
597 if (!sdata || sdata == sta->sdata) {
598 __sta_info_unlink(&sta);
599 if (sta)
600 list_add_tail(&sta->list, &tmp_list);
Michael Wube8755e2007-07-27 15:43:23 +0200601 }
Michael Wube8755e2007-07-27 15:43:23 +0200602 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100603 spin_unlock_irqrestore(&local->sta_lock, flags);
604
605 synchronize_rcu();
606
607 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
608 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700609}