blob: d9774ac2e0f7c1310f4cbf5cd8c5433c1768f85c [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"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040022#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070023#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 *
Johannes Berg03e44972008-02-27 09:56:40 +010034 * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
35 * that structure. It must then either destroy it using sta_info_destroy()
36 * (which is pretty useless) or insert it into the hash table using
37 * sta_info_insert() which demotes the reference from ownership to a regular
38 * RCU-protected reference; if the function is called without protection by an
Johannes Berg93e5deb2008-04-01 15:21:00 +020039 * RCU critical section the reference is instantly invalidated. Note that the
40 * caller may not do much with the STA info before inserting it, in particular,
41 * it may not start any mesh peer link management or add encryption keys.
42 *
43 * When the insertion fails (sta_info_insert()) returns non-zero), the
44 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010045 *
46 * Because there are debugfs entries for each station, and adding those
47 * must be able to sleep, it is also possible to "pin" a station entry,
48 * that means it can be removed from the hash table but not be freed.
Johannes Berg93e5deb2008-04-01 15:21:00 +020049 * See the comment in __sta_info_unlink() for more information, this is
50 * an internal capability only.
Johannes Bergd0709a62008-02-25 16:27:46 +010051 *
52 * In order to remove a STA info structure, the caller needs to first
Johannes Bergdbbea672008-02-26 14:34:06 +010053 * unlink it (sta_info_unlink()) from the list and hash tables and
Johannes Berg3b967662008-04-08 17:56:52 +020054 * then destroy it; sta_info_destroy() will wait for an RCU grace period
55 * to elapse before actually freeing it. Due to the pinning and the
56 * possibility of multiple callers trying to remove the same STA info at
57 * the same time, sta_info_unlink() can clear the STA info pointer it is
58 * passed to indicate that the STA info is owned by somebody else now.
Johannes Bergd0709a62008-02-25 16:27:46 +010059 *
Johannes Bergdbbea672008-02-26 14:34:06 +010060 * If sta_info_unlink() did not clear the pointer then the caller owns
Johannes Bergd0709a62008-02-25 16:27:46 +010061 * the STA info structure now and is responsible of destroying it with
Johannes Berg3b967662008-04-08 17:56:52 +020062 * a call to sta_info_destroy().
Johannes Bergd0709a62008-02-25 16:27:46 +010063 *
64 * In all other cases, there is no concept of ownership on a STA entry,
65 * each structure is owned by the global hash table/list until it is
66 * removed. All users of the structure need to be RCU protected so that
67 * the structure won't be freed before they are done using it.
68 */
Jiri Bencf0706e82007-05-05 11:45:53 -070069
70/* Caller must hold local->sta_lock */
Michael Wube8755e2007-07-27 15:43:23 +020071static int sta_info_hash_del(struct ieee80211_local *local,
72 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070073{
74 struct sta_info *s;
75
Johannes Berg17741cd2008-09-11 00:02:02 +020076 s = local->sta_hash[STA_HASH(sta->sta.addr)];
Jiri Bencf0706e82007-05-05 11:45:53 -070077 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020078 return -ENOENT;
79 if (s == sta) {
Johannes Berg17741cd2008-09-11 00:02:02 +020080 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
Johannes Bergd0709a62008-02-25 16:27:46 +010081 s->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020082 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070083 }
84
Michael Wube8755e2007-07-27 15:43:23 +020085 while (s->hnext && s->hnext != sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070086 s = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020087 if (s->hnext) {
Johannes Bergd0709a62008-02-25 16:27:46 +010088 rcu_assign_pointer(s->hnext, sta->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020089 return 0;
90 }
Jiri Bencf0706e82007-05-05 11:45:53 -070091
Michael Wube8755e2007-07-27 15:43:23 +020092 return -ENOENT;
Jiri Bencf0706e82007-05-05 11:45:53 -070093}
94
Johannes Bergd0709a62008-02-25 16:27:46 +010095/* protected by RCU */
Johannes Berg43ba7e92008-02-21 14:09:30 +010096static struct sta_info *__sta_info_find(struct ieee80211_local *local,
Johannes Berg17741cd2008-09-11 00:02:02 +020097 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +010098{
99 struct sta_info *sta;
100
Johannes Bergd0709a62008-02-25 16:27:46 +0100101 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100102 while (sta) {
Johannes Berg17741cd2008-09-11 00:02:02 +0200103 if (compare_ether_addr(sta->sta.addr, addr) == 0)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100104 break;
Johannes Bergd0709a62008-02-25 16:27:46 +0100105 sta = rcu_dereference(sta->hnext);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100106 }
107 return sta;
108}
109
Jiri Bencf0706e82007-05-05 11:45:53 -0700110struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
111{
Johannes Bergd0709a62008-02-25 16:27:46 +0100112 return __sta_info_find(local, addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700113}
114EXPORT_SYMBOL(sta_info_get);
115
Luis Carlos Coboee385852008-02-23 15:17:11 +0100116struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
117 struct net_device *dev)
118{
119 struct sta_info *sta;
120 int i = 0;
121
Johannes Bergd0709a62008-02-25 16:27:46 +0100122 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800123 if (dev && dev != sta->sdata->dev)
124 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100125 if (i < idx) {
126 ++i;
127 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100128 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800129 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100130 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100131
132 return NULL;
133}
Jiri Bencf0706e82007-05-05 11:45:53 -0700134
Johannes Berg93e5deb2008-04-01 15:21:00 +0200135/**
136 * __sta_info_free - internal STA free helper
137 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700138 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200139 * @sta: STA info to free
140 *
141 * This function must undo everything done by sta_info_alloc()
142 * that may happen before sta_info_insert().
143 */
144static void __sta_info_free(struct ieee80211_local *local,
145 struct sta_info *sta)
146{
147 DECLARE_MAC_BUF(mbuf);
148
149 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
150 rate_control_put(sta->rate_ctrl);
151
152#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
153 printk(KERN_DEBUG "%s: Destroyed STA %s\n",
Johannes Berg17741cd2008-09-11 00:02:02 +0200154 wiphy_name(local->hw.wiphy), print_mac(mbuf, sta->sta.addr));
Johannes Berg93e5deb2008-04-01 15:21:00 +0200155#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
156
157 kfree(sta);
158}
159
Johannes Bergd0709a62008-02-25 16:27:46 +0100160void sta_info_destroy(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700161{
Johannes Berg97bff8e2008-03-31 19:23:00 +0200162 struct ieee80211_local *local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700163 struct sk_buff *skb;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200164 int i;
Johannes Berg73651ee2008-02-25 16:27:47 +0100165
Johannes Berg97bff8e2008-03-31 19:23:00 +0200166 might_sleep();
167
Johannes Berg73651ee2008-02-25 16:27:47 +0100168 if (!sta)
169 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700170
Johannes Berg97bff8e2008-03-31 19:23:00 +0200171 local = sta->local;
Johannes Bergd0709a62008-02-25 16:27:46 +0100172
173 rate_control_remove_sta_debugfs(sta);
174 ieee80211_sta_debugfs_remove(sta);
175
176#ifdef CONFIG_MAC80211_MESH
177 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
178 mesh_plink_deactivate(sta);
179#endif
180
Johannes Berg3b967662008-04-08 17:56:52 +0200181 /*
182 * We have only unlinked the key, and actually destroying it
183 * may mean it is removed from hardware which requires that
184 * the key->sta pointer is still valid, so flush the key todo
185 * list here.
186 *
187 * ieee80211_key_todo() will synchronize_rcu() so after this
188 * nothing can reference this sta struct any more.
189 */
190 ieee80211_key_todo();
Johannes Bergd0709a62008-02-25 16:27:46 +0100191
192#ifdef CONFIG_MAC80211_MESH
193 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
194 del_timer_sync(&sta->plink_timer);
195#endif
196
Jiri Bencf0706e82007-05-05 11:45:53 -0700197 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
198 local->total_ps_buffered--;
199 dev_kfree_skb_any(skb);
200 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100201
202 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
Jiri Bencf0706e82007-05-05 11:45:53 -0700203 dev_kfree_skb_any(skb);
Johannes Bergd0709a62008-02-25 16:27:46 +0100204
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200205 for (i = 0; i < STA_TID_NUM; i++) {
Johannes Berg07346f812008-05-03 01:02:02 +0200206 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200207 if (sta->ampdu_mlme.tid_rx[i])
208 del_timer_sync(&sta->ampdu_mlme.tid_rx[i]->session_timer);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200209 if (sta->ampdu_mlme.tid_tx[i])
210 del_timer_sync(&sta->ampdu_mlme.tid_tx[i]->addba_resp_timer);
Johannes Berg07346f812008-05-03 01:02:02 +0200211 spin_unlock_bh(&sta->lock);
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200212 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200213
Johannes Berg93e5deb2008-04-01 15:21:00 +0200214 __sta_info_free(local, sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700215}
216
217
Johannes Bergd0709a62008-02-25 16:27:46 +0100218/* Caller must hold local->sta_lock */
219static void sta_info_hash_add(struct ieee80211_local *local,
220 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700221{
Johannes Berg17741cd2008-09-11 00:02:02 +0200222 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
223 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700224}
Jiri Bencf0706e82007-05-05 11:45:53 -0700225
Johannes Berg73651ee2008-02-25 16:27:47 +0100226struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
227 u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700228{
Johannes Bergd0709a62008-02-25 16:27:46 +0100229 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700230 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200231 int i;
Johannes Berg73651ee2008-02-25 16:27:47 +0100232 DECLARE_MAC_BUF(mbuf);
Jiri Bencf0706e82007-05-05 11:45:53 -0700233
Johannes Berg17741cd2008-09-11 00:02:02 +0200234 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700235 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100236 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700237
Johannes Berg07346f812008-05-03 01:02:02 +0200238 spin_lock_init(&sta->lock);
Johannes Berg5a9f7b02008-06-18 14:58:09 +0200239 spin_lock_init(&sta->flaglock);
Johannes Berg07346f812008-05-03 01:02:02 +0200240
Johannes Berg17741cd2008-09-11 00:02:02 +0200241 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100242 sta->local = local;
243 sta->sdata = sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700244
245 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
Johannes Bergd0709a62008-02-25 16:27:46 +0100246 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg73651ee2008-02-25 16:27:47 +0100247 gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700248 if (!sta->rate_ctrl_priv) {
249 rate_control_put(sta->rate_ctrl);
Jiri Bencf0706e82007-05-05 11:45:53 -0700250 kfree(sta);
Johannes Berg73651ee2008-02-25 16:27:47 +0100251 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700252 }
253
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200254 for (i = 0; i < STA_TID_NUM; i++) {
255 /* timer_to_tid must be initialized with identity mapping to
256 * enable session_timer's data differentiation. refer to
257 * sta_rx_agg_session_timer_expired for useage */
258 sta->timer_to_tid[i] = i;
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200259 /* tid to tx queue: initialize according to HW (0 is valid) */
Johannes Berge2530082008-05-17 00:57:14 +0200260 sta->tid_to_tx_q[i] = ieee80211_num_queues(&local->hw);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200261 /* rx */
262 sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
263 sta->ampdu_mlme.tid_rx[i] = NULL;
264 /* tx */
265 sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
266 sta->ampdu_mlme.tid_tx[i] = NULL;
267 sta->ampdu_mlme.addba_req_num[i] = 0;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200268 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700269 skb_queue_head_init(&sta->ps_tx_buf);
270 skb_queue_head_init(&sta->tx_filtered);
Johannes Berg73651ee2008-02-25 16:27:47 +0100271
272#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
273 printk(KERN_DEBUG "%s: Allocated STA %s\n",
Johannes Berg17741cd2008-09-11 00:02:02 +0200274 wiphy_name(local->hw.wiphy), print_mac(mbuf, sta->sta.addr));
Johannes Berg73651ee2008-02-25 16:27:47 +0100275#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
276
Johannes Berg03e44972008-02-27 09:56:40 +0100277#ifdef CONFIG_MAC80211_MESH
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800278 sta->plink_state = PLINK_LISTEN;
Johannes Berg03e44972008-02-27 09:56:40 +0100279 init_timer(&sta->plink_timer);
280#endif
281
Johannes Berg73651ee2008-02-25 16:27:47 +0100282 return sta;
283}
284
285int sta_info_insert(struct sta_info *sta)
286{
287 struct ieee80211_local *local = sta->local;
288 struct ieee80211_sub_if_data *sdata = sta->sdata;
289 unsigned long flags;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200290 int err = 0;
Johannes Berg73651ee2008-02-25 16:27:47 +0100291 DECLARE_MAC_BUF(mac);
292
Johannes Berg03e44972008-02-27 09:56:40 +0100293 /*
294 * Can't be a WARN_ON because it can be triggered through a race:
295 * something inserts a STA (on one CPU) without holding the RTNL
296 * and another CPU turns off the net device.
297 */
Johannes Berg93e5deb2008-04-01 15:21:00 +0200298 if (unlikely(!netif_running(sdata->dev))) {
299 err = -ENETDOWN;
300 goto out_free;
301 }
Johannes Berg03e44972008-02-27 09:56:40 +0100302
Johannes Berg17741cd2008-09-11 00:02:02 +0200303 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
304 is_multicast_ether_addr(sta->sta.addr))) {
Johannes Berg93e5deb2008-04-01 15:21:00 +0200305 err = -EINVAL;
306 goto out_free;
307 }
Johannes Berg44213b52008-02-25 16:27:49 +0100308
Johannes Bergd0709a62008-02-25 16:27:46 +0100309 spin_lock_irqsave(&local->sta_lock, flags);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100310 /* check if STA exists already */
Johannes Berg17741cd2008-09-11 00:02:02 +0200311 if (__sta_info_find(local, sta->sta.addr)) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100312 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200313 err = -EEXIST;
314 goto out_free;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100315 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700316 list_add(&sta->list, &local->sta_list);
317 local->num_sta++;
318 sta_info_hash_add(local, sta);
Johannes Berg32bfd352007-12-19 01:31:26 +0100319
Johannes Bergd0709a62008-02-25 16:27:46 +0100320 /* notify driver */
321 if (local->ops->sta_notify) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200322 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg3e122be2008-07-09 14:40:34 +0200323 sdata = container_of(sdata->bss,
324 struct ieee80211_sub_if_data,
325 u.ap);
Johannes Berg32bfd352007-12-19 01:31:26 +0100326
327 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
Johannes Berg17741cd2008-09-11 00:02:02 +0200328 STA_NOTIFY_ADD, &sta->sta);
Johannes Berg32bfd352007-12-19 01:31:26 +0100329 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100330
Jiri Bencf0706e82007-05-05 11:45:53 -0700331#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg73651ee2008-02-25 16:27:47 +0100332 printk(KERN_DEBUG "%s: Inserted STA %s\n",
Johannes Berg17741cd2008-09-11 00:02:02 +0200333 wiphy_name(local->hw.wiphy), print_mac(mac, sta->sta.addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700334#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
335
Johannes Berg73651ee2008-02-25 16:27:47 +0100336 spin_unlock_irqrestore(&local->sta_lock, flags);
337
Jiri Bence9f207f2007-05-05 11:46:38 -0700338#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Berg93e5deb2008-04-01 15:21:00 +0200339 /*
340 * Debugfs entry adding might sleep, so schedule process
Michael Wube8755e2007-07-27 15:43:23 +0200341 * context task for adding entry for STAs that do not yet
Johannes Berg93e5deb2008-04-01 15:21:00 +0200342 * have one.
343 * NOTE: due to auto-freeing semantics this may only be done
344 * if the insertion is successful!
345 */
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200346 schedule_work(&local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700347#endif
348
Johannes Berg73651ee2008-02-25 16:27:47 +0100349 if (ieee80211_vif_is_mesh(&sdata->vif))
350 mesh_accept_plinks_update(sdata);
351
352 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200353 out_free:
354 BUG_ON(!err);
355 __sta_info_free(local, sta);
356 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700357}
358
Johannes Berg004c8722008-02-20 11:21:35 +0100359static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
360{
361 /*
362 * This format has been mandated by the IEEE specifications,
363 * so this line may not be changed to use the __set_bit() format.
364 */
365 bss->tim[aid / 8] |= (1 << (aid % 8));
366}
367
368static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
369{
370 /*
371 * This format has been mandated by the IEEE specifications,
372 * so this line may not be changed to use the __clear_bit() format.
373 */
374 bss->tim[aid / 8] &= ~(1 << (aid % 8));
375}
376
377static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
378 struct sta_info *sta)
379{
Johannes Berg3e122be2008-07-09 14:40:34 +0200380 BUG_ON(!bss);
381
Johannes Berg17741cd2008-09-11 00:02:02 +0200382 __bss_tim_set(bss, sta->sta.aid);
Johannes Berg3e122be2008-07-09 14:40:34 +0200383
Johannes Bergd0709a62008-02-25 16:27:46 +0100384 if (sta->local->ops->set_tim) {
385 sta->local->tim_in_locked_section = true;
Johannes Berg17741cd2008-09-11 00:02:02 +0200386 sta->local->ops->set_tim(local_to_hw(sta->local),
387 &sta->sta, true);
Johannes Bergd0709a62008-02-25 16:27:46 +0100388 sta->local->tim_in_locked_section = false;
389 }
Johannes Berg004c8722008-02-20 11:21:35 +0100390}
391
392void sta_info_set_tim_bit(struct sta_info *sta)
393{
Johannes Bergd0709a62008-02-25 16:27:46 +0100394 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100395
Johannes Berg3e122be2008-07-09 14:40:34 +0200396 BUG_ON(!sta->sdata->bss);
397
Johannes Bergd0709a62008-02-25 16:27:46 +0100398 spin_lock_irqsave(&sta->local->sta_lock, flags);
399 __sta_info_set_tim_bit(sta->sdata->bss, sta);
400 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100401}
402
403static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
404 struct sta_info *sta)
405{
Johannes Berg3e122be2008-07-09 14:40:34 +0200406 BUG_ON(!bss);
407
Johannes Berg17741cd2008-09-11 00:02:02 +0200408 __bss_tim_clear(bss, sta->sta.aid);
Johannes Berg3e122be2008-07-09 14:40:34 +0200409
Johannes Bergd0709a62008-02-25 16:27:46 +0100410 if (sta->local->ops->set_tim) {
411 sta->local->tim_in_locked_section = true;
Johannes Berg17741cd2008-09-11 00:02:02 +0200412 sta->local->ops->set_tim(local_to_hw(sta->local),
413 &sta->sta, false);
Johannes Bergd0709a62008-02-25 16:27:46 +0100414 sta->local->tim_in_locked_section = false;
415 }
Johannes Berg004c8722008-02-20 11:21:35 +0100416}
417
418void sta_info_clear_tim_bit(struct sta_info *sta)
419{
Johannes Bergd0709a62008-02-25 16:27:46 +0100420 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100421
Johannes Berg3e122be2008-07-09 14:40:34 +0200422 BUG_ON(!sta->sdata->bss);
423
Johannes Bergd0709a62008-02-25 16:27:46 +0100424 spin_lock_irqsave(&sta->local->sta_lock, flags);
425 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
426 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100427}
428
Johannes Berg24723d12008-09-11 00:01:46 +0200429static void __sta_info_unlink(struct sta_info **sta)
Johannes Bergd0709a62008-02-25 16:27:46 +0100430{
431 struct ieee80211_local *local = (*sta)->local;
432 struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
433#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
434 DECLARE_MAC_BUF(mbuf);
435#endif
436 /*
437 * pull caller's reference if we're already gone.
438 */
439 if (sta_info_hash_del(local, *sta)) {
440 *sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200441 return;
Johannes Bergd0709a62008-02-25 16:27:46 +0100442 }
Michael Wube8755e2007-07-27 15:43:23 +0200443
Johannes Berg3b967662008-04-08 17:56:52 +0200444 if ((*sta)->key) {
445 ieee80211_key_free((*sta)->key);
446 WARN_ON((*sta)->key);
447 }
448
Johannes Berg7d1559f2008-04-08 13:08:20 +0200449 list_del(&(*sta)->list);
450
Johannes Berg07346f812008-05-03 01:02:02 +0200451 if (test_and_clear_sta_flags(*sta, WLAN_STA_PS)) {
Johannes Berg3e122be2008-07-09 14:40:34 +0200452 BUG_ON(!sdata->bss);
453
454 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg7d1559f2008-04-08 13:08:20 +0200455 __sta_info_clear_tim_bit(sdata->bss, *sta);
456 }
457
458 local->num_sta--;
459
460 if (local->ops->sta_notify) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200461 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg3e122be2008-07-09 14:40:34 +0200462 sdata = container_of(sdata->bss,
463 struct ieee80211_sub_if_data,
464 u.ap);
Johannes Berg7d1559f2008-04-08 13:08:20 +0200465
466 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
Johannes Berg17741cd2008-09-11 00:02:02 +0200467 STA_NOTIFY_REMOVE, &(*sta)->sta);
Johannes Berg7d1559f2008-04-08 13:08:20 +0200468 }
469
470 if (ieee80211_vif_is_mesh(&sdata->vif)) {
471 mesh_accept_plinks_update(sdata);
472#ifdef CONFIG_MAC80211_MESH
473 del_timer(&(*sta)->plink_timer);
474#endif
475 }
476
477#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
478 printk(KERN_DEBUG "%s: Removed STA %s\n",
Johannes Berg17741cd2008-09-11 00:02:02 +0200479 wiphy_name(local->hw.wiphy), print_mac(mbuf, (*sta)->sta.addr));
Johannes Berg7d1559f2008-04-08 13:08:20 +0200480#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
481
Johannes Bergd0709a62008-02-25 16:27:46 +0100482 /*
Johannes Berg7d1559f2008-04-08 13:08:20 +0200483 * Finally, pull caller's reference if the STA is pinned by the
Johannes Bergd0709a62008-02-25 16:27:46 +0100484 * task that is adding the debugfs entries. In that case, we
485 * leave the STA "to be freed".
486 *
487 * The rules are not trivial, but not too complex either:
488 * (1) pin_status is only modified under the sta_lock
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200489 * (2) STAs may only be pinned under the RTNL so that
490 * sta_info_flush() is guaranteed to actually destroy
491 * all STAs that are active for a given interface, this
492 * is required for correctness because otherwise we
493 * could notify a driver that an interface is going
494 * away and only after that (!) notify it about a STA
495 * on that interface going away.
496 * (3) sta_info_debugfs_add_work() will set the status
Johannes Bergd0709a62008-02-25 16:27:46 +0100497 * to PINNED when it found an item that needs a new
498 * debugfs directory created. In that case, that item
499 * must not be freed although all *RCU* users are done
500 * with it. Hence, we tell the caller of _unlink()
501 * that the item is already gone (as can happen when
502 * two tasks try to unlink/destroy at the same time)
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200503 * (4) We set the pin_status to DESTROY here when we
Johannes Bergd0709a62008-02-25 16:27:46 +0100504 * find such an item.
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200505 * (5) sta_info_debugfs_add_work() will reset the pin_status
Johannes Bergd0709a62008-02-25 16:27:46 +0100506 * from PINNED to NORMAL when it is done with the item,
507 * but will check for DESTROY before resetting it in
508 * which case it will free the item.
509 */
510 if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
511 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
512 *sta = NULL;
513 return;
514 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700515}
516
Johannes Bergd0709a62008-02-25 16:27:46 +0100517void sta_info_unlink(struct sta_info **sta)
518{
519 struct ieee80211_local *local = (*sta)->local;
520 unsigned long flags;
521
522 spin_lock_irqsave(&local->sta_lock, flags);
523 __sta_info_unlink(sta);
524 spin_unlock_irqrestore(&local->sta_lock, flags);
525}
Jiri Bencf0706e82007-05-05 11:45:53 -0700526
527static inline int sta_info_buffer_expired(struct ieee80211_local *local,
528 struct sta_info *sta,
529 struct sk_buff *skb)
530{
Johannes Berge039fa42008-05-15 12:55:29 +0200531 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700532 int timeout;
533
534 if (!skb)
535 return 0;
536
Johannes Berge039fa42008-05-15 12:55:29 +0200537 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700538
539 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
540 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
541 15625) * HZ;
542 if (timeout < STA_TX_BUFFER_EXPIRE)
543 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200544 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700545}
546
547
548static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
549 struct sta_info *sta)
550{
551 unsigned long flags;
552 struct sk_buff *skb;
Johannes Berg836341a2008-02-20 02:07:21 +0100553 struct ieee80211_sub_if_data *sdata;
Joe Perches0795af52007-10-03 17:59:30 -0700554 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700555
556 if (skb_queue_empty(&sta->ps_tx_buf))
557 return;
558
559 for (;;) {
560 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
561 skb = skb_peek(&sta->ps_tx_buf);
Johannes Berg836341a2008-02-20 02:07:21 +0100562 if (sta_info_buffer_expired(local, sta, skb))
Jiri Bencf0706e82007-05-05 11:45:53 -0700563 skb = __skb_dequeue(&sta->ps_tx_buf);
Johannes Berg836341a2008-02-20 02:07:21 +0100564 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700565 skb = NULL;
566 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
567
Johannes Berg836341a2008-02-20 02:07:21 +0100568 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700569 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100570
Johannes Bergd0709a62008-02-25 16:27:46 +0100571 sdata = sta->sdata;
Johannes Berg836341a2008-02-20 02:07:21 +0100572 local->total_ps_buffered--;
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200573#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg836341a2008-02-20 02:07:21 +0100574 printk(KERN_DEBUG "Buffered frame expired (STA "
Johannes Berg17741cd2008-09-11 00:02:02 +0200575 "%s)\n", print_mac(mac, sta->sta.addr));
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200576#endif
Johannes Berg836341a2008-02-20 02:07:21 +0100577 dev_kfree_skb(skb);
578
Johannes Berg004c8722008-02-20 11:21:35 +0100579 if (skb_queue_empty(&sta->ps_tx_buf))
580 sta_info_clear_tim_bit(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700581 }
582}
583
584
585static void sta_info_cleanup(unsigned long data)
586{
587 struct ieee80211_local *local = (struct ieee80211_local *) data;
588 struct sta_info *sta;
589
Johannes Bergd0709a62008-02-25 16:27:46 +0100590 rcu_read_lock();
591 list_for_each_entry_rcu(sta, &local->sta_list, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700592 sta_info_cleanup_expire_buffered(local, sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100593 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700594
Johannes Berg0d174402007-12-17 15:07:43 +0100595 local->sta_cleanup.expires =
596 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700597 add_timer(&local->sta_cleanup);
598}
599
Jiri Bence9f207f2007-05-05 11:46:38 -0700600#ifdef CONFIG_MAC80211_DEBUGFS
Jiri Slaby4d6141c32008-04-07 21:53:49 +0200601/*
602 * See comment in __sta_info_unlink,
603 * caller must hold local->sta_lock.
604 */
605static void __sta_info_pin(struct sta_info *sta)
606{
607 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
608 sta->pin_status = STA_INFO_PIN_STAT_PINNED;
609}
610
611/*
612 * See comment in __sta_info_unlink, returns sta if it
613 * needs to be destroyed.
614 */
615static struct sta_info *__sta_info_unpin(struct sta_info *sta)
616{
617 struct sta_info *ret = NULL;
618 unsigned long flags;
619
620 spin_lock_irqsave(&sta->local->sta_lock, flags);
621 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
622 sta->pin_status != STA_INFO_PIN_STAT_PINNED);
623 if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
624 ret = sta;
625 sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
626 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
627
628 return ret;
629}
630
Johannes Bergd0709a62008-02-25 16:27:46 +0100631static void sta_info_debugfs_add_work(struct work_struct *work)
Jiri Bence9f207f2007-05-05 11:46:38 -0700632{
633 struct ieee80211_local *local =
634 container_of(work, struct ieee80211_local, sta_debugfs_add);
635 struct sta_info *sta, *tmp;
Johannes Bergd0709a62008-02-25 16:27:46 +0100636 unsigned long flags;
Jiri Bence9f207f2007-05-05 11:46:38 -0700637
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200638 /* We need to keep the RTNL across the whole pinned status. */
639 rtnl_lock();
Jiri Bence9f207f2007-05-05 11:46:38 -0700640 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700641 sta = NULL;
Johannes Bergd0709a62008-02-25 16:27:46 +0100642
643 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700644 list_for_each_entry(tmp, &local->sta_list, list) {
Michael Wube8755e2007-07-27 15:43:23 +0200645 if (!tmp->debugfs.dir) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700646 sta = tmp;
Johannes Bergd0709a62008-02-25 16:27:46 +0100647 __sta_info_pin(sta);
Jiri Bence9f207f2007-05-05 11:46:38 -0700648 break;
649 }
650 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100651 spin_unlock_irqrestore(&local->sta_lock, flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700652
653 if (!sta)
654 break;
655
Jiri Bence9f207f2007-05-05 11:46:38 -0700656 ieee80211_sta_debugfs_add(sta);
657 rate_control_add_sta_debugfs(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100658
659 sta = __sta_info_unpin(sta);
Johannes Berg4f6fab42008-03-31 19:23:02 +0200660 sta_info_destroy(sta);
Jiri Bence9f207f2007-05-05 11:46:38 -0700661 }
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200662 rtnl_unlock();
Jiri Bence9f207f2007-05-05 11:46:38 -0700663}
664#endif
665
Johannes Berg3b967662008-04-08 17:56:52 +0200666static void __ieee80211_run_pending_flush(struct ieee80211_local *local)
Johannes Bergdc6676b2008-03-31 19:23:03 +0200667{
668 struct sta_info *sta;
669 unsigned long flags;
670
671 ASSERT_RTNL();
672
673 spin_lock_irqsave(&local->sta_lock, flags);
674 while (!list_empty(&local->sta_flush_list)) {
675 sta = list_first_entry(&local->sta_flush_list,
676 struct sta_info, list);
677 list_del(&sta->list);
678 spin_unlock_irqrestore(&local->sta_lock, flags);
679 sta_info_destroy(sta);
680 spin_lock_irqsave(&local->sta_lock, flags);
681 }
682 spin_unlock_irqrestore(&local->sta_lock, flags);
683}
684
685static void ieee80211_sta_flush_work(struct work_struct *work)
686{
687 struct ieee80211_local *local =
688 container_of(work, struct ieee80211_local, sta_flush_work);
689
690 rtnl_lock();
691 __ieee80211_run_pending_flush(local);
692 rtnl_unlock();
693}
694
Jiri Bencf0706e82007-05-05 11:45:53 -0700695void sta_info_init(struct ieee80211_local *local)
696{
Johannes Bergd0709a62008-02-25 16:27:46 +0100697 spin_lock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700698 INIT_LIST_HEAD(&local->sta_list);
Johannes Bergdc6676b2008-03-31 19:23:03 +0200699 INIT_LIST_HEAD(&local->sta_flush_list);
700 INIT_WORK(&local->sta_flush_work, ieee80211_sta_flush_work);
Jiri Bencf0706e82007-05-05 11:45:53 -0700701
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800702 setup_timer(&local->sta_cleanup, sta_info_cleanup,
703 (unsigned long)local);
Johannes Berg0d174402007-12-17 15:07:43 +0100704 local->sta_cleanup.expires =
705 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bence9f207f2007-05-05 11:46:38 -0700706
707#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Bergd0709a62008-02-25 16:27:46 +0100708 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
Jiri Bence9f207f2007-05-05 11:46:38 -0700709#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700710}
711
712int sta_info_start(struct ieee80211_local *local)
713{
714 add_timer(&local->sta_cleanup);
715 return 0;
716}
717
718void sta_info_stop(struct ieee80211_local *local)
719{
Jiri Bencf0706e82007-05-05 11:45:53 -0700720 del_timer(&local->sta_cleanup);
Johannes Bergdc6676b2008-03-31 19:23:03 +0200721 cancel_work_sync(&local->sta_flush_work);
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200722#ifdef CONFIG_MAC80211_DEBUGFS
723 /*
724 * Make sure the debugfs adding work isn't pending after this
725 * because we're about to be destroyed. It doesn't matter
726 * whether it ran or not since we're going to flush all STAs
727 * anyway.
728 */
729 cancel_work_sync(&local->sta_debugfs_add);
730#endif
Johannes Bergdc6676b2008-03-31 19:23:03 +0200731
732 rtnl_lock();
Michael Wube8755e2007-07-27 15:43:23 +0200733 sta_info_flush(local, NULL);
Johannes Bergdc6676b2008-03-31 19:23:03 +0200734 __ieee80211_run_pending_flush(local);
735 rtnl_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700736}
737
Jiri Bencf0706e82007-05-05 11:45:53 -0700738/**
739 * sta_info_flush - flush matching STA entries from the STA table
Johannes Berg44213b52008-02-25 16:27:49 +0100740 *
741 * Returns the number of removed STA entries.
742 *
Jiri Bencf0706e82007-05-05 11:45:53 -0700743 * @local: local interface data
Johannes Bergd0709a62008-02-25 16:27:46 +0100744 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
Jiri Bencf0706e82007-05-05 11:45:53 -0700745 */
Johannes Berg44213b52008-02-25 16:27:49 +0100746int sta_info_flush(struct ieee80211_local *local,
Johannes Bergd0709a62008-02-25 16:27:46 +0100747 struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -0700748{
749 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200750 LIST_HEAD(tmp_list);
Johannes Berg44213b52008-02-25 16:27:49 +0100751 int ret = 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100752 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -0700753
Johannes Bergd0709a62008-02-25 16:27:46 +0100754 might_sleep();
Johannes Bergdc6676b2008-03-31 19:23:03 +0200755 ASSERT_RTNL();
Johannes Bergd0709a62008-02-25 16:27:46 +0100756
757 spin_lock_irqsave(&local->sta_lock, flags);
758 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
759 if (!sdata || sdata == sta->sdata) {
760 __sta_info_unlink(&sta);
Johannes Berg44213b52008-02-25 16:27:49 +0100761 if (sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100762 list_add_tail(&sta->list, &tmp_list);
Johannes Berg44213b52008-02-25 16:27:49 +0100763 ret++;
764 }
Michael Wube8755e2007-07-27 15:43:23 +0200765 }
Michael Wube8755e2007-07-27 15:43:23 +0200766 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100767 spin_unlock_irqrestore(&local->sta_lock, flags);
768
Johannes Bergd0709a62008-02-25 16:27:46 +0100769 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
770 sta_info_destroy(sta);
Johannes Berg44213b52008-02-25 16:27:49 +0100771
772 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -0700773}
Johannes Bergdc6676b2008-03-31 19:23:03 +0200774
775/**
776 * sta_info_flush_delayed - flush matching STA entries from the STA table
777 *
778 * This function unlinks all stations for a given interface and queues
779 * them for freeing. Note that the workqueue function scheduled here has
780 * to run before any new keys can be added to the system to avoid set_key()
781 * callback ordering issues.
782 *
783 * @sdata: the interface
784 */
785void sta_info_flush_delayed(struct ieee80211_sub_if_data *sdata)
786{
787 struct ieee80211_local *local = sdata->local;
788 struct sta_info *sta, *tmp;
789 unsigned long flags;
790 bool work = false;
791
792 spin_lock_irqsave(&local->sta_lock, flags);
793 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
794 if (sdata == sta->sdata) {
795 __sta_info_unlink(&sta);
796 if (sta) {
797 list_add_tail(&sta->list,
798 &local->sta_flush_list);
799 work = true;
800 }
801 }
802 }
803 if (work)
804 schedule_work(&local->sta_flush_work);
805 spin_unlock_irqrestore(&local->sta_lock, flags);
806}
Johannes Berg24723d12008-09-11 00:01:46 +0200807
808void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
809 unsigned long exp_time)
810{
811 struct ieee80211_local *local = sdata->local;
812 struct sta_info *sta, *tmp;
813 LIST_HEAD(tmp_list);
814 DECLARE_MAC_BUF(mac);
815 unsigned long flags;
816
817 spin_lock_irqsave(&local->sta_lock, flags);
818 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
819 if (time_after(jiffies, sta->last_rx + exp_time)) {
820#ifdef CONFIG_MAC80211_IBSS_DEBUG
821 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
Johannes Berg17741cd2008-09-11 00:02:02 +0200822 sdata->dev->name, print_mac(mac, sta->sta.addr));
Johannes Berg24723d12008-09-11 00:01:46 +0200823#endif
824 __sta_info_unlink(&sta);
825 if (sta)
826 list_add(&sta->list, &tmp_list);
827 }
828 spin_unlock_irqrestore(&local->sta_lock, flags);
829
830 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
831 sta_info_destroy(sta);
832}
Johannes Berg17741cd2008-09-11 00:02:02 +0200833
834struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_hw *hw,
835 const u8 *addr)
836{
837 struct sta_info *sta = __sta_info_find(hw_to_local(hw), addr);
838
839 if (!sta)
840 return NULL;
841 return &sta->sta;
842}
843EXPORT_SYMBOL(ieee80211_find_sta);