blob: aa017a56afc80b140ca98450996e076efc145138 [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 Berg24487982009-04-23 18:52:52 +020022#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040023#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070024#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070025#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010026#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070027
Johannes Bergd0709a62008-02-25 16:27:46 +010028/**
29 * DOC: STA information lifetime rules
30 *
31 * STA info structures (&struct sta_info) are managed in a hash table
32 * for faster lookup and a list for iteration. They are managed using
33 * RCU, i.e. access to the list and hash table is protected by RCU.
34 *
Johannes Berg03e44972008-02-27 09:56:40 +010035 * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
36 * that structure. It must then either destroy it using sta_info_destroy()
37 * (which is pretty useless) or insert it into the hash table using
38 * sta_info_insert() which demotes the reference from ownership to a regular
39 * RCU-protected reference; if the function is called without protection by an
Johannes Berg93e5deb2008-04-01 15:21:00 +020040 * RCU critical section the reference is instantly invalidated. Note that the
41 * caller may not do much with the STA info before inserting it, in particular,
42 * it may not start any mesh peer link management or add encryption keys.
43 *
44 * When the insertion fails (sta_info_insert()) returns non-zero), the
45 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010046 *
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040047 * sta entries are added by mac80211 when you establish a link with a
48 * peer. This means different things for the different type of interfaces
49 * we support. For a regular station this mean we add the AP sta when we
50 * receive an assocation response from the AP. For IBSS this occurs when
51 * we receive a probe response or a beacon from target IBSS network. For
52 * WDS we add the sta for the peer imediately upon device open. When using
53 * AP mode we add stations for each respective station upon request from
54 * userspace through nl80211.
55 *
Johannes Bergd0709a62008-02-25 16:27:46 +010056 * Because there are debugfs entries for each station, and adding those
57 * must be able to sleep, it is also possible to "pin" a station entry,
58 * that means it can be removed from the hash table but not be freed.
Johannes Berg93e5deb2008-04-01 15:21:00 +020059 * See the comment in __sta_info_unlink() for more information, this is
60 * an internal capability only.
Johannes Bergd0709a62008-02-25 16:27:46 +010061 *
62 * In order to remove a STA info structure, the caller needs to first
Johannes Bergdbbea672008-02-26 14:34:06 +010063 * unlink it (sta_info_unlink()) from the list and hash tables and
Johannes Berg3b967662008-04-08 17:56:52 +020064 * then destroy it; sta_info_destroy() will wait for an RCU grace period
65 * to elapse before actually freeing it. Due to the pinning and the
66 * possibility of multiple callers trying to remove the same STA info at
67 * the same time, sta_info_unlink() can clear the STA info pointer it is
68 * passed to indicate that the STA info is owned by somebody else now.
Johannes Bergd0709a62008-02-25 16:27:46 +010069 *
Johannes Bergdbbea672008-02-26 14:34:06 +010070 * If sta_info_unlink() did not clear the pointer then the caller owns
Johannes Bergd0709a62008-02-25 16:27:46 +010071 * the STA info structure now and is responsible of destroying it with
Johannes Berg3b967662008-04-08 17:56:52 +020072 * a call to sta_info_destroy().
Johannes Bergd0709a62008-02-25 16:27:46 +010073 *
74 * In all other cases, there is no concept of ownership on a STA entry,
75 * each structure is owned by the global hash table/list until it is
76 * removed. All users of the structure need to be RCU protected so that
77 * the structure won't be freed before they are done using it.
78 */
Jiri Bencf0706e82007-05-05 11:45:53 -070079
80/* Caller must hold local->sta_lock */
Michael Wube8755e2007-07-27 15:43:23 +020081static int sta_info_hash_del(struct ieee80211_local *local,
82 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070083{
84 struct sta_info *s;
85
Johannes Berg17741cd2008-09-11 00:02:02 +020086 s = local->sta_hash[STA_HASH(sta->sta.addr)];
Jiri Bencf0706e82007-05-05 11:45:53 -070087 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020088 return -ENOENT;
89 if (s == sta) {
Johannes Berg17741cd2008-09-11 00:02:02 +020090 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
Johannes Bergd0709a62008-02-25 16:27:46 +010091 s->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020092 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070093 }
94
Michael Wube8755e2007-07-27 15:43:23 +020095 while (s->hnext && s->hnext != sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070096 s = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020097 if (s->hnext) {
Johannes Bergd0709a62008-02-25 16:27:46 +010098 rcu_assign_pointer(s->hnext, sta->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020099 return 0;
100 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700101
Michael Wube8755e2007-07-27 15:43:23 +0200102 return -ENOENT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700103}
104
Johannes Bergd0709a62008-02-25 16:27:46 +0100105/* protected by RCU */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200106struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100107{
108 struct sta_info *sta;
109
Johannes Bergd0709a62008-02-25 16:27:46 +0100110 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100111 while (sta) {
Shaddy Baddah5cf12e82008-11-28 17:08:10 +1100112 if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100113 break;
Johannes Bergd0709a62008-02-25 16:27:46 +0100114 sta = rcu_dereference(sta->hnext);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100115 }
116 return sta;
117}
118
Johannes Berg3b53fde82009-11-16 12:00:37 +0100119struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
120 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100121{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100122 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100123 struct sta_info *sta;
124 int i = 0;
125
Johannes Bergd0709a62008-02-25 16:27:46 +0100126 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100127 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800128 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100129 if (i < idx) {
130 ++i;
131 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100132 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800133 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100134 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100135
136 return NULL;
137}
Jiri Bencf0706e82007-05-05 11:45:53 -0700138
Johannes Berg93e5deb2008-04-01 15:21:00 +0200139/**
140 * __sta_info_free - internal STA free helper
141 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700142 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200143 * @sta: STA info to free
144 *
145 * This function must undo everything done by sta_info_alloc()
146 * that may happen before sta_info_insert().
147 */
148static void __sta_info_free(struct ieee80211_local *local,
149 struct sta_info *sta)
150{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200151 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200152 rate_control_put(sta->rate_ctrl);
153
154#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700155 printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
156 wiphy_name(local->hw.wiphy), sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200157#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
158
159 kfree(sta);
160}
161
Johannes Bergd0709a62008-02-25 16:27:46 +0100162void sta_info_destroy(struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700163{
Johannes Berg97bff8e2008-03-31 19:23:00 +0200164 struct ieee80211_local *local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700165 struct sk_buff *skb;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200166 int i;
Johannes Berg73651ee2008-02-25 16:27:47 +0100167
Johannes Berg97bff8e2008-03-31 19:23:00 +0200168 might_sleep();
169
Johannes Berg73651ee2008-02-25 16:27:47 +0100170 if (!sta)
171 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700172
Johannes Berg97bff8e2008-03-31 19:23:00 +0200173 local = sta->local;
Johannes Bergd0709a62008-02-25 16:27:46 +0100174
Johannes Bergaf818582009-11-06 11:35:50 +0100175 cancel_work_sync(&sta->drv_unblock_wk);
176
Johannes Bergd0709a62008-02-25 16:27:46 +0100177 rate_control_remove_sta_debugfs(sta);
178 ieee80211_sta_debugfs_remove(sta);
179
180#ifdef CONFIG_MAC80211_MESH
181 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
182 mesh_plink_deactivate(sta);
183#endif
184
Johannes Berg3b967662008-04-08 17:56:52 +0200185 /*
186 * We have only unlinked the key, and actually destroying it
187 * may mean it is removed from hardware which requires that
188 * the key->sta pointer is still valid, so flush the key todo
189 * list here.
190 *
191 * ieee80211_key_todo() will synchronize_rcu() so after this
192 * nothing can reference this sta struct any more.
193 */
194 ieee80211_key_todo();
Johannes Bergd0709a62008-02-25 16:27:46 +0100195
196#ifdef CONFIG_MAC80211_MESH
197 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
198 del_timer_sync(&sta->plink_timer);
199#endif
200
Jiri Bencf0706e82007-05-05 11:45:53 -0700201 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
202 local->total_ps_buffered--;
203 dev_kfree_skb_any(skb);
204 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100205
206 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
Jiri Bencf0706e82007-05-05 11:45:53 -0700207 dev_kfree_skb_any(skb);
Johannes Bergd0709a62008-02-25 16:27:46 +0100208
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200209 for (i = 0; i < STA_TID_NUM; i++) {
Johannes Berg55687e32009-02-10 21:25:51 +0100210 struct tid_ampdu_rx *tid_rx;
211 struct tid_ampdu_tx *tid_tx;
212
Johannes Berg07346f812008-05-03 01:02:02 +0200213 spin_lock_bh(&sta->lock);
Johannes Berg55687e32009-02-10 21:25:51 +0100214 tid_rx = sta->ampdu_mlme.tid_rx[i];
215 /* Make sure timer won't free the tid_rx struct, see below */
216 if (tid_rx)
217 tid_rx->shutdown = true;
Johannes Berg96f5e662009-02-12 00:51:53 +0100218
Johannes Berg07346f812008-05-03 01:02:02 +0200219 spin_unlock_bh(&sta->lock);
Johannes Berg55687e32009-02-10 21:25:51 +0100220
221 /*
222 * Outside spinlock - shutdown is true now so that the timer
223 * won't free tid_rx, we have to do that now. Can't let the
224 * timer do it because we have to sync the timer outside the
225 * lock that it takes itself.
226 */
227 if (tid_rx) {
228 del_timer_sync(&tid_rx->session_timer);
229 kfree(tid_rx);
230 }
231
232 /*
233 * No need to do such complications for TX agg sessions, the
234 * path leading to freeing the tid_tx struct goes via a call
235 * from the driver, and thus needs to look up the sta struct
236 * again, which cannot be found when we get here. Hence, we
237 * just need to delete the timer and free the aggregation
238 * info; we won't be telling the peer about it then but that
239 * doesn't matter if we're not talking to it again anyway.
240 */
241 tid_tx = sta->ampdu_mlme.tid_tx[i];
242 if (tid_tx) {
243 del_timer_sync(&tid_tx->addba_resp_timer);
Johannes Bergcd8ffc82009-03-23 17:28:41 +0100244 /*
245 * STA removed while aggregation session being
246 * started? Bit odd, but purge frames anyway.
247 */
248 skb_queue_purge(&tid_tx->pending);
Johannes Berg55687e32009-02-10 21:25:51 +0100249 kfree(tid_tx);
250 }
Ron Rindjunskyfe3bf0f2008-01-28 14:07:19 +0200251 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200252
Johannes Berg93e5deb2008-04-01 15:21:00 +0200253 __sta_info_free(local, sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700254}
255
256
Johannes Bergd0709a62008-02-25 16:27:46 +0100257/* Caller must hold local->sta_lock */
258static void sta_info_hash_add(struct ieee80211_local *local,
259 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700260{
Johannes Berg17741cd2008-09-11 00:02:02 +0200261 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
262 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700263}
Jiri Bencf0706e82007-05-05 11:45:53 -0700264
Johannes Bergaf818582009-11-06 11:35:50 +0100265static void sta_unblock(struct work_struct *wk)
266{
267 struct sta_info *sta;
268
269 sta = container_of(wk, struct sta_info, drv_unblock_wk);
270
271 if (sta->dead)
272 return;
273
274 if (!test_sta_flags(sta, WLAN_STA_PS_STA))
275 ieee80211_sta_ps_deliver_wakeup(sta);
276 else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
277 ieee80211_sta_ps_deliver_poll_response(sta);
278}
279
Johannes Berg73651ee2008-02-25 16:27:47 +0100280struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
281 u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700282{
Johannes Bergd0709a62008-02-25 16:27:46 +0100283 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700284 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200285 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700286
Johannes Berg17741cd2008-09-11 00:02:02 +0200287 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700288 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100289 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700290
Johannes Berg07346f812008-05-03 01:02:02 +0200291 spin_lock_init(&sta->lock);
Johannes Berg5a9f7b02008-06-18 14:58:09 +0200292 spin_lock_init(&sta->flaglock);
Johannes Bergaf818582009-11-06 11:35:50 +0100293 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
Johannes Berg07346f812008-05-03 01:02:02 +0200294
Johannes Berg17741cd2008-09-11 00:02:02 +0200295 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100296 sta->local = local;
297 sta->sdata = sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700298
299 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
Johannes Bergd0709a62008-02-25 16:27:46 +0100300 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200301 &sta->sta, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700302 if (!sta->rate_ctrl_priv) {
303 rate_control_put(sta->rate_ctrl);
Jiri Bencf0706e82007-05-05 11:45:53 -0700304 kfree(sta);
Johannes Berg73651ee2008-02-25 16:27:47 +0100305 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700306 }
307
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200308 for (i = 0; i < STA_TID_NUM; i++) {
309 /* timer_to_tid must be initialized with identity mapping to
310 * enable session_timer's data differentiation. refer to
311 * sta_rx_agg_session_timer_expired for useage */
312 sta->timer_to_tid[i] = i;
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200313 /* rx */
314 sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
315 sta->ampdu_mlme.tid_rx[i] = NULL;
316 /* tx */
317 sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
318 sta->ampdu_mlme.tid_tx[i] = NULL;
319 sta->ampdu_mlme.addba_req_num[i] = 0;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200320 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700321 skb_queue_head_init(&sta->ps_tx_buf);
322 skb_queue_head_init(&sta->tx_filtered);
Johannes Berg73651ee2008-02-25 16:27:47 +0100323
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530324 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
325 sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
326
Johannes Berg73651ee2008-02-25 16:27:47 +0100327#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700328 printk(KERN_DEBUG "%s: Allocated STA %pM\n",
329 wiphy_name(local->hw.wiphy), sta->sta.addr);
Johannes Berg73651ee2008-02-25 16:27:47 +0100330#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
331
Johannes Berg03e44972008-02-27 09:56:40 +0100332#ifdef CONFIG_MAC80211_MESH
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800333 sta->plink_state = PLINK_LISTEN;
Johannes Berg03e44972008-02-27 09:56:40 +0100334 init_timer(&sta->plink_timer);
335#endif
336
Johannes Berg73651ee2008-02-25 16:27:47 +0100337 return sta;
338}
339
340int sta_info_insert(struct sta_info *sta)
341{
342 struct ieee80211_local *local = sta->local;
343 struct ieee80211_sub_if_data *sdata = sta->sdata;
344 unsigned long flags;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200345 int err = 0;
Johannes Berg73651ee2008-02-25 16:27:47 +0100346
Johannes Berg03e44972008-02-27 09:56:40 +0100347 /*
348 * Can't be a WARN_ON because it can be triggered through a race:
349 * something inserts a STA (on one CPU) without holding the RTNL
350 * and another CPU turns off the net device.
351 */
Johannes Berg93e5deb2008-04-01 15:21:00 +0200352 if (unlikely(!netif_running(sdata->dev))) {
353 err = -ENETDOWN;
354 goto out_free;
355 }
Johannes Berg03e44972008-02-27 09:56:40 +0100356
Johannes Berg17741cd2008-09-11 00:02:02 +0200357 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200358 is_multicast_ether_addr(sta->sta.addr))) {
Johannes Berg93e5deb2008-04-01 15:21:00 +0200359 err = -EINVAL;
360 goto out_free;
361 }
Johannes Berg44213b52008-02-25 16:27:49 +0100362
Johannes Bergd0709a62008-02-25 16:27:46 +0100363 spin_lock_irqsave(&local->sta_lock, flags);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100364 /* check if STA exists already */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200365 if (sta_info_get(local, sta->sta.addr)) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100366 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200367 err = -EEXIST;
368 goto out_free;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100369 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700370 list_add(&sta->list, &local->sta_list);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200371 local->sta_generation++;
Jiri Bencf0706e82007-05-05 11:45:53 -0700372 local->num_sta++;
373 sta_info_hash_add(local, sta);
Johannes Berg32bfd352007-12-19 01:31:26 +0100374
Johannes Bergd0709a62008-02-25 16:27:46 +0100375 /* notify driver */
376 if (local->ops->sta_notify) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200377 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg3e122be2008-07-09 14:40:34 +0200378 sdata = container_of(sdata->bss,
379 struct ieee80211_sub_if_data,
380 u.ap);
Johannes Berg32bfd352007-12-19 01:31:26 +0100381
Johannes Berg24487982009-04-23 18:52:52 +0200382 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
Johannes Bergfbc44bf2009-10-01 22:06:29 +0200383 sdata = sta->sdata;
Johannes Berg32bfd352007-12-19 01:31:26 +0100384 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100385
Jiri Bencf0706e82007-05-05 11:45:53 -0700386#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700387 printk(KERN_DEBUG "%s: Inserted STA %pM\n",
388 wiphy_name(local->hw.wiphy), sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700389#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
390
Johannes Berg73651ee2008-02-25 16:27:47 +0100391 spin_unlock_irqrestore(&local->sta_lock, flags);
392
Jiri Bence9f207f2007-05-05 11:46:38 -0700393#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Berg93e5deb2008-04-01 15:21:00 +0200394 /*
395 * Debugfs entry adding might sleep, so schedule process
Michael Wube8755e2007-07-27 15:43:23 +0200396 * context task for adding entry for STAs that do not yet
Johannes Berg93e5deb2008-04-01 15:21:00 +0200397 * have one.
398 * NOTE: due to auto-freeing semantics this may only be done
399 * if the insertion is successful!
400 */
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200401 schedule_work(&local->sta_debugfs_add);
Jiri Bence9f207f2007-05-05 11:46:38 -0700402#endif
403
Johannes Berg73651ee2008-02-25 16:27:47 +0100404 if (ieee80211_vif_is_mesh(&sdata->vif))
405 mesh_accept_plinks_update(sdata);
406
407 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200408 out_free:
409 BUG_ON(!err);
410 __sta_info_free(local, sta);
411 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700412}
413
Johannes Berg004c8722008-02-20 11:21:35 +0100414static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
415{
416 /*
417 * This format has been mandated by the IEEE specifications,
418 * so this line may not be changed to use the __set_bit() format.
419 */
420 bss->tim[aid / 8] |= (1 << (aid % 8));
421}
422
423static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
424{
425 /*
426 * This format has been mandated by the IEEE specifications,
427 * so this line may not be changed to use the __clear_bit() format.
428 */
429 bss->tim[aid / 8] &= ~(1 << (aid % 8));
430}
431
432static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
433 struct sta_info *sta)
434{
Johannes Berg3e122be2008-07-09 14:40:34 +0200435 BUG_ON(!bss);
436
Johannes Berg17741cd2008-09-11 00:02:02 +0200437 __bss_tim_set(bss, sta->sta.aid);
Johannes Berg3e122be2008-07-09 14:40:34 +0200438
Johannes Bergd0709a62008-02-25 16:27:46 +0100439 if (sta->local->ops->set_tim) {
440 sta->local->tim_in_locked_section = true;
Johannes Berg24487982009-04-23 18:52:52 +0200441 drv_set_tim(sta->local, &sta->sta, true);
Johannes Bergd0709a62008-02-25 16:27:46 +0100442 sta->local->tim_in_locked_section = false;
443 }
Johannes Berg004c8722008-02-20 11:21:35 +0100444}
445
446void sta_info_set_tim_bit(struct sta_info *sta)
447{
Johannes Bergd0709a62008-02-25 16:27:46 +0100448 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100449
Johannes Berg3e122be2008-07-09 14:40:34 +0200450 BUG_ON(!sta->sdata->bss);
451
Johannes Bergd0709a62008-02-25 16:27:46 +0100452 spin_lock_irqsave(&sta->local->sta_lock, flags);
453 __sta_info_set_tim_bit(sta->sdata->bss, sta);
454 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100455}
456
457static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
458 struct sta_info *sta)
459{
Johannes Berg3e122be2008-07-09 14:40:34 +0200460 BUG_ON(!bss);
461
Johannes Berg17741cd2008-09-11 00:02:02 +0200462 __bss_tim_clear(bss, sta->sta.aid);
Johannes Berg3e122be2008-07-09 14:40:34 +0200463
Johannes Bergd0709a62008-02-25 16:27:46 +0100464 if (sta->local->ops->set_tim) {
465 sta->local->tim_in_locked_section = true;
Johannes Berg24487982009-04-23 18:52:52 +0200466 drv_set_tim(sta->local, &sta->sta, false);
Johannes Bergd0709a62008-02-25 16:27:46 +0100467 sta->local->tim_in_locked_section = false;
468 }
Johannes Berg004c8722008-02-20 11:21:35 +0100469}
470
471void sta_info_clear_tim_bit(struct sta_info *sta)
472{
Johannes Bergd0709a62008-02-25 16:27:46 +0100473 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100474
Johannes Berg3e122be2008-07-09 14:40:34 +0200475 BUG_ON(!sta->sdata->bss);
476
Johannes Bergd0709a62008-02-25 16:27:46 +0100477 spin_lock_irqsave(&sta->local->sta_lock, flags);
478 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
479 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100480}
481
Johannes Berg24723d12008-09-11 00:01:46 +0200482static void __sta_info_unlink(struct sta_info **sta)
Johannes Bergd0709a62008-02-25 16:27:46 +0100483{
484 struct ieee80211_local *local = (*sta)->local;
485 struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
Johannes Bergd0709a62008-02-25 16:27:46 +0100486 /*
487 * pull caller's reference if we're already gone.
488 */
489 if (sta_info_hash_del(local, *sta)) {
490 *sta = NULL;
Michael Wube8755e2007-07-27 15:43:23 +0200491 return;
Johannes Bergd0709a62008-02-25 16:27:46 +0100492 }
Michael Wube8755e2007-07-27 15:43:23 +0200493
Johannes Berg3b967662008-04-08 17:56:52 +0200494 if ((*sta)->key) {
495 ieee80211_key_free((*sta)->key);
496 WARN_ON((*sta)->key);
497 }
498
Johannes Berg7d1559f2008-04-08 13:08:20 +0200499 list_del(&(*sta)->list);
Johannes Bergaf818582009-11-06 11:35:50 +0100500 (*sta)->dead = true;
Johannes Berg7d1559f2008-04-08 13:08:20 +0200501
Johannes Bergaf818582009-11-06 11:35:50 +0100502 if (test_and_clear_sta_flags(*sta,
503 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
Johannes Berg3e122be2008-07-09 14:40:34 +0200504 BUG_ON(!sdata->bss);
505
506 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg7d1559f2008-04-08 13:08:20 +0200507 __sta_info_clear_tim_bit(sdata->bss, *sta);
508 }
509
510 local->num_sta--;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200511 local->sta_generation++;
Johannes Berg7d1559f2008-04-08 13:08:20 +0200512
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100513 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
514 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
515
Johannes Berg7d1559f2008-04-08 13:08:20 +0200516 if (local->ops->sta_notify) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200517 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg3e122be2008-07-09 14:40:34 +0200518 sdata = container_of(sdata->bss,
519 struct ieee80211_sub_if_data,
520 u.ap);
Johannes Berg7d1559f2008-04-08 13:08:20 +0200521
Johannes Berg24487982009-04-23 18:52:52 +0200522 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
523 &(*sta)->sta);
Johannes Bergfbc44bf2009-10-01 22:06:29 +0200524 sdata = (*sta)->sdata;
Johannes Berg7d1559f2008-04-08 13:08:20 +0200525 }
526
527 if (ieee80211_vif_is_mesh(&sdata->vif)) {
528 mesh_accept_plinks_update(sdata);
529#ifdef CONFIG_MAC80211_MESH
530 del_timer(&(*sta)->plink_timer);
531#endif
532 }
533
534#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700535 printk(KERN_DEBUG "%s: Removed STA %pM\n",
536 wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
Johannes Berg7d1559f2008-04-08 13:08:20 +0200537#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
538
Johannes Bergd0709a62008-02-25 16:27:46 +0100539 /*
Johannes Berg7d1559f2008-04-08 13:08:20 +0200540 * Finally, pull caller's reference if the STA is pinned by the
Johannes Bergd0709a62008-02-25 16:27:46 +0100541 * task that is adding the debugfs entries. In that case, we
542 * leave the STA "to be freed".
543 *
544 * The rules are not trivial, but not too complex either:
545 * (1) pin_status is only modified under the sta_lock
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200546 * (2) STAs may only be pinned under the RTNL so that
547 * sta_info_flush() is guaranteed to actually destroy
548 * all STAs that are active for a given interface, this
549 * is required for correctness because otherwise we
550 * could notify a driver that an interface is going
551 * away and only after that (!) notify it about a STA
552 * on that interface going away.
553 * (3) sta_info_debugfs_add_work() will set the status
Johannes Bergd0709a62008-02-25 16:27:46 +0100554 * to PINNED when it found an item that needs a new
555 * debugfs directory created. In that case, that item
556 * must not be freed although all *RCU* users are done
557 * with it. Hence, we tell the caller of _unlink()
558 * that the item is already gone (as can happen when
559 * two tasks try to unlink/destroy at the same time)
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200560 * (4) We set the pin_status to DESTROY here when we
Johannes Bergd0709a62008-02-25 16:27:46 +0100561 * find such an item.
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200562 * (5) sta_info_debugfs_add_work() will reset the pin_status
Johannes Bergd0709a62008-02-25 16:27:46 +0100563 * from PINNED to NORMAL when it is done with the item,
564 * but will check for DESTROY before resetting it in
565 * which case it will free the item.
566 */
567 if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
568 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
569 *sta = NULL;
570 return;
571 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700572}
573
Johannes Bergd0709a62008-02-25 16:27:46 +0100574void sta_info_unlink(struct sta_info **sta)
575{
576 struct ieee80211_local *local = (*sta)->local;
577 unsigned long flags;
578
579 spin_lock_irqsave(&local->sta_lock, flags);
580 __sta_info_unlink(sta);
581 spin_unlock_irqrestore(&local->sta_lock, flags);
582}
Jiri Bencf0706e82007-05-05 11:45:53 -0700583
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200584static int sta_info_buffer_expired(struct sta_info *sta,
585 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700586{
Johannes Berge039fa42008-05-15 12:55:29 +0200587 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700588 int timeout;
589
590 if (!skb)
591 return 0;
592
Johannes Berge039fa42008-05-15 12:55:29 +0200593 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700594
595 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200596 timeout = (sta->listen_interval *
597 sta->sdata->vif.bss_conf.beacon_int *
598 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700599 if (timeout < STA_TX_BUFFER_EXPIRE)
600 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200601 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700602}
603
604
605static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
606 struct sta_info *sta)
607{
608 unsigned long flags;
609 struct sk_buff *skb;
Johannes Berg836341a2008-02-20 02:07:21 +0100610 struct ieee80211_sub_if_data *sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700611
612 if (skb_queue_empty(&sta->ps_tx_buf))
613 return;
614
615 for (;;) {
616 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
617 skb = skb_peek(&sta->ps_tx_buf);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200618 if (sta_info_buffer_expired(sta, skb))
Jiri Bencf0706e82007-05-05 11:45:53 -0700619 skb = __skb_dequeue(&sta->ps_tx_buf);
Johannes Berg836341a2008-02-20 02:07:21 +0100620 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700621 skb = NULL;
622 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
623
Johannes Berg836341a2008-02-20 02:07:21 +0100624 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700625 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100626
Johannes Bergd0709a62008-02-25 16:27:46 +0100627 sdata = sta->sdata;
Johannes Berg836341a2008-02-20 02:07:21 +0100628 local->total_ps_buffered--;
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200629#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700630 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
631 sta->sta.addr);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200632#endif
Johannes Berg836341a2008-02-20 02:07:21 +0100633 dev_kfree_skb(skb);
634
Johannes Berg004c8722008-02-20 11:21:35 +0100635 if (skb_queue_empty(&sta->ps_tx_buf))
636 sta_info_clear_tim_bit(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700637 }
638}
639
640
641static void sta_info_cleanup(unsigned long data)
642{
643 struct ieee80211_local *local = (struct ieee80211_local *) data;
644 struct sta_info *sta;
645
Johannes Bergd0709a62008-02-25 16:27:46 +0100646 rcu_read_lock();
647 list_for_each_entry_rcu(sta, &local->sta_list, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700648 sta_info_cleanup_expire_buffered(local, sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100649 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700650
Johannes Berg5bb644a2009-05-17 11:40:42 +0200651 if (local->quiescing)
652 return;
653
Johannes Berg0d174402007-12-17 15:07:43 +0100654 local->sta_cleanup.expires =
655 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700656 add_timer(&local->sta_cleanup);
657}
658
Jiri Bence9f207f2007-05-05 11:46:38 -0700659#ifdef CONFIG_MAC80211_DEBUGFS
Jiri Slaby4d6141c32008-04-07 21:53:49 +0200660/*
661 * See comment in __sta_info_unlink,
662 * caller must hold local->sta_lock.
663 */
664static void __sta_info_pin(struct sta_info *sta)
665{
666 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
667 sta->pin_status = STA_INFO_PIN_STAT_PINNED;
668}
669
670/*
671 * See comment in __sta_info_unlink, returns sta if it
672 * needs to be destroyed.
673 */
674static struct sta_info *__sta_info_unpin(struct sta_info *sta)
675{
676 struct sta_info *ret = NULL;
677 unsigned long flags;
678
679 spin_lock_irqsave(&sta->local->sta_lock, flags);
680 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
681 sta->pin_status != STA_INFO_PIN_STAT_PINNED);
682 if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
683 ret = sta;
684 sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
685 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
686
687 return ret;
688}
689
Johannes Bergd0709a62008-02-25 16:27:46 +0100690static void sta_info_debugfs_add_work(struct work_struct *work)
Jiri Bence9f207f2007-05-05 11:46:38 -0700691{
692 struct ieee80211_local *local =
693 container_of(work, struct ieee80211_local, sta_debugfs_add);
694 struct sta_info *sta, *tmp;
Johannes Bergd0709a62008-02-25 16:27:46 +0100695 unsigned long flags;
Jiri Bence9f207f2007-05-05 11:46:38 -0700696
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200697 /* We need to keep the RTNL across the whole pinned status. */
698 rtnl_lock();
Jiri Bence9f207f2007-05-05 11:46:38 -0700699 while (1) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700700 sta = NULL;
Johannes Bergd0709a62008-02-25 16:27:46 +0100701
702 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700703 list_for_each_entry(tmp, &local->sta_list, list) {
Johannes Berg63044e92008-10-07 12:04:29 +0200704 /*
705 * debugfs.add_has_run will be set by
706 * ieee80211_sta_debugfs_add regardless
707 * of what else it does.
708 */
709 if (!tmp->debugfs.add_has_run) {
Jiri Bence9f207f2007-05-05 11:46:38 -0700710 sta = tmp;
Johannes Bergd0709a62008-02-25 16:27:46 +0100711 __sta_info_pin(sta);
Jiri Bence9f207f2007-05-05 11:46:38 -0700712 break;
713 }
714 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100715 spin_unlock_irqrestore(&local->sta_lock, flags);
Jiri Bence9f207f2007-05-05 11:46:38 -0700716
717 if (!sta)
718 break;
719
Jiri Bence9f207f2007-05-05 11:46:38 -0700720 ieee80211_sta_debugfs_add(sta);
721 rate_control_add_sta_debugfs(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100722
723 sta = __sta_info_unpin(sta);
Johannes Berg4f6fab42008-03-31 19:23:02 +0200724 sta_info_destroy(sta);
Jiri Bence9f207f2007-05-05 11:46:38 -0700725 }
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200726 rtnl_unlock();
Jiri Bence9f207f2007-05-05 11:46:38 -0700727}
728#endif
729
Jiri Bencf0706e82007-05-05 11:45:53 -0700730void sta_info_init(struct ieee80211_local *local)
731{
Johannes Bergd0709a62008-02-25 16:27:46 +0100732 spin_lock_init(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -0700733 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700734
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800735 setup_timer(&local->sta_cleanup, sta_info_cleanup,
736 (unsigned long)local);
Johannes Berg0d174402007-12-17 15:07:43 +0100737 local->sta_cleanup.expires =
738 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bence9f207f2007-05-05 11:46:38 -0700739
740#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Bergd0709a62008-02-25 16:27:46 +0100741 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
Jiri Bence9f207f2007-05-05 11:46:38 -0700742#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700743}
744
745int sta_info_start(struct ieee80211_local *local)
746{
747 add_timer(&local->sta_cleanup);
748 return 0;
749}
750
751void sta_info_stop(struct ieee80211_local *local)
752{
Jiri Bencf0706e82007-05-05 11:45:53 -0700753 del_timer(&local->sta_cleanup);
Johannes Berg49ec6fa22008-04-03 14:31:05 +0200754#ifdef CONFIG_MAC80211_DEBUGFS
755 /*
756 * Make sure the debugfs adding work isn't pending after this
757 * because we're about to be destroyed. It doesn't matter
758 * whether it ran or not since we're going to flush all STAs
759 * anyway.
760 */
761 cancel_work_sync(&local->sta_debugfs_add);
762#endif
Johannes Bergdc6676b2008-03-31 19:23:03 +0200763
Michael Wube8755e2007-07-27 15:43:23 +0200764 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700765}
766
Jiri Bencf0706e82007-05-05 11:45:53 -0700767/**
768 * sta_info_flush - flush matching STA entries from the STA table
Johannes Berg44213b52008-02-25 16:27:49 +0100769 *
770 * Returns the number of removed STA entries.
771 *
Jiri Bencf0706e82007-05-05 11:45:53 -0700772 * @local: local interface data
Johannes Bergd0709a62008-02-25 16:27:46 +0100773 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
Jiri Bencf0706e82007-05-05 11:45:53 -0700774 */
Johannes Berg44213b52008-02-25 16:27:49 +0100775int sta_info_flush(struct ieee80211_local *local,
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200776 struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -0700777{
778 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +0200779 LIST_HEAD(tmp_list);
Johannes Berg44213b52008-02-25 16:27:49 +0100780 int ret = 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100781 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -0700782
Johannes Bergd0709a62008-02-25 16:27:46 +0100783 might_sleep();
784
785 spin_lock_irqsave(&local->sta_lock, flags);
786 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
787 if (!sdata || sdata == sta->sdata) {
788 __sta_info_unlink(&sta);
Johannes Berg44213b52008-02-25 16:27:49 +0100789 if (sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100790 list_add_tail(&sta->list, &tmp_list);
Johannes Berg44213b52008-02-25 16:27:49 +0100791 ret++;
792 }
Michael Wube8755e2007-07-27 15:43:23 +0200793 }
Michael Wube8755e2007-07-27 15:43:23 +0200794 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100795 spin_unlock_irqrestore(&local->sta_lock, flags);
796
Johannes Bergd0709a62008-02-25 16:27:46 +0100797 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
798 sta_info_destroy(sta);
Johannes Berg44213b52008-02-25 16:27:49 +0100799
800 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -0700801}
Johannes Bergdc6676b2008-03-31 19:23:03 +0200802
Johannes Berg24723d12008-09-11 00:01:46 +0200803void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
804 unsigned long exp_time)
805{
806 struct ieee80211_local *local = sdata->local;
807 struct sta_info *sta, *tmp;
808 LIST_HEAD(tmp_list);
Johannes Berg24723d12008-09-11 00:01:46 +0200809 unsigned long flags;
810
811 spin_lock_irqsave(&local->sta_lock, flags);
812 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
813 if (time_after(jiffies, sta->last_rx + exp_time)) {
814#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700815 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
816 sdata->dev->name, sta->sta.addr);
Johannes Berg24723d12008-09-11 00:01:46 +0200817#endif
818 __sta_info_unlink(&sta);
819 if (sta)
820 list_add(&sta->list, &tmp_list);
821 }
822 spin_unlock_irqrestore(&local->sta_lock, flags);
823
824 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
825 sta_info_destroy(sta);
826}
Johannes Berg17741cd2008-09-11 00:02:02 +0200827
Johannes Berg5ed176e2009-11-04 14:42:28 +0100828struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
829 const u8 *addr)
Johannes Berg17741cd2008-09-11 00:02:02 +0200830{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200831 struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
Johannes Berg17741cd2008-09-11 00:02:02 +0200832
833 if (!sta)
834 return NULL;
835 return &sta->sta;
836}
Johannes Berg5ed176e2009-11-04 14:42:28 +0100837EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
838
839struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
840 const u8 *addr)
841{
842 struct ieee80211_sub_if_data *sdata;
843
844 if (!vif)
845 return NULL;
846
847 sdata = vif_to_sdata(vif);
848
849 return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
850}
Johannes Berg17741cd2008-09-11 00:02:02 +0200851EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +0100852
853/* powersave support code */
854void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
855{
856 struct ieee80211_sub_if_data *sdata = sta->sdata;
857 struct ieee80211_local *local = sdata->local;
858 int sent, buffered;
859
860 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_AWAKE, &sta->sta);
861
862 if (!skb_queue_empty(&sta->ps_tx_buf))
863 sta_info_clear_tim_bit(sta);
864
865 /* Send all buffered frames to the station */
866 sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
867 buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
868 sent += buffered;
869 local->total_ps_buffered -= buffered;
870
871#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
872 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
873 "since STA not sleeping anymore\n", sdata->dev->name,
874 sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
875#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
876}
877
878void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
879{
880 struct ieee80211_sub_if_data *sdata = sta->sdata;
881 struct ieee80211_local *local = sdata->local;
882 struct sk_buff *skb;
883 int no_pending_pkts;
884
885 skb = skb_dequeue(&sta->tx_filtered);
886 if (!skb) {
887 skb = skb_dequeue(&sta->ps_tx_buf);
888 if (skb)
889 local->total_ps_buffered--;
890 }
891 no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
892 skb_queue_empty(&sta->ps_tx_buf);
893
894 if (skb) {
895 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
896 struct ieee80211_hdr *hdr =
897 (struct ieee80211_hdr *) skb->data;
898
899 /*
900 * Tell TX path to send this frame even though the STA may
901 * still remain is PS mode after this frame exchange.
902 */
903 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
904
905#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
906 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
907 sta->sta.addr, sta->sta.aid,
908 skb_queue_len(&sta->ps_tx_buf));
909#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
910
911 /* Use MoreData flag to indicate whether there are more
912 * buffered frames for this STA */
913 if (no_pending_pkts)
914 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
915 else
916 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
917
918 ieee80211_add_pending_skb(local, skb);
919
920 if (no_pending_pkts)
921 sta_info_clear_tim_bit(sta);
922#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
923 } else {
924 /*
925 * FIXME: This can be the result of a race condition between
926 * us expiring a frame and the station polling for it.
927 * Should we send it a null-func frame indicating we
928 * have nothing buffered for it?
929 */
930 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
931 "though there are no buffered frames for it\n",
932 sdata->dev->name, sta->sta.addr);
933#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
934 }
935}
936
937void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
938 struct ieee80211_sta *pubsta, bool block)
939{
940 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
941
942 if (block)
943 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
944 else
945 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
946}
947EXPORT_SYMBOL(ieee80211_sta_block_awake);