blob: 8dabe66fc37ff7b915444491c9961f32d434da34 [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 Berg34e89502010-02-03 13:59:58 +010035 * Upon allocating a STA info structure with sta_info_alloc(), the caller
36 * owns that structure. It must then insert it into the hash table using
37 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
38 * case (which acquires an rcu read section but must not be called from
39 * within one) will the pointer still be valid after the call. Note that
40 * the caller may not do much with the STA info before inserting it, in
41 * particular, it may not start any mesh peer link management or add
42 * encryption keys.
Johannes Berg93e5deb2008-04-01 15:21:00 +020043 *
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 *
Johannes Berg34e89502010-02-03 13:59:58 +010047 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040048 * 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030050 * receive an association response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010051 * get to know about a peer on the same IBSS. For WDS we add the sta for
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * the peer immediately upon device open. When using AP mode we add stations
Johannes Berg34e89502010-02-03 13:59:58 +010053 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040054 *
Johannes Berg34e89502010-02-03 13:59:58 +010055 * In order to remove a STA info structure, various sta_info_destroy_*()
56 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010057 *
Johannes Berg34e89502010-02-03 13:59:58 +010058 * There is no concept of ownership on a STA entry, each structure is
59 * owned by the global hash table/list until it is removed. All users of
60 * the structure need to be RCU protected so that the structure won't be
61 * freed before they are done using it.
Johannes Bergd0709a62008-02-25 16:27:46 +010062 */
Jiri Bencf0706e82007-05-05 11:45:53 -070063
64/* Caller must hold local->sta_lock */
Michael Wube8755e2007-07-27 15:43:23 +020065static int sta_info_hash_del(struct ieee80211_local *local,
66 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070067{
68 struct sta_info *s;
69
Johannes Berg40b275b2011-05-13 14:15:49 +020070 s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
71 lockdep_is_held(&local->sta_lock));
Jiri Bencf0706e82007-05-05 11:45:53 -070072 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020073 return -ENOENT;
74 if (s == sta) {
Johannes Berg17741cd2008-09-11 00:02:02 +020075 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
Johannes Bergd0709a62008-02-25 16:27:46 +010076 s->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020077 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070078 }
79
Johannes Berg40b275b2011-05-13 14:15:49 +020080 while (rcu_access_pointer(s->hnext) &&
81 rcu_access_pointer(s->hnext) != sta)
82 s = rcu_dereference_protected(s->hnext,
83 lockdep_is_held(&local->sta_lock));
84 if (rcu_access_pointer(s->hnext)) {
Johannes Bergd0709a62008-02-25 16:27:46 +010085 rcu_assign_pointer(s->hnext, sta->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020086 return 0;
87 }
Jiri Bencf0706e82007-05-05 11:45:53 -070088
Michael Wube8755e2007-07-27 15:43:23 +020089 return -ENOENT;
Jiri Bencf0706e82007-05-05 11:45:53 -070090}
91
Johannes Bergd0709a62008-02-25 16:27:46 +010092/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +010093struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
94 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +010095{
Johannes Bergabe60632009-11-25 17:46:18 +010096 struct ieee80211_local *local = sdata->local;
Johannes Berg43ba7e92008-02-21 14:09:30 +010097 struct sta_info *sta;
98
Johannes Berg03791852010-04-06 11:18:42 +020099 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
Johannes Berg03791852010-04-06 11:18:42 +0200100 lockdep_is_held(&local->sta_lock) ||
101 lockdep_is_held(&local->sta_mtx));
Johannes Berg43ba7e92008-02-21 14:09:30 +0100102 while (sta) {
Guy Eilam2a33bee2011-08-17 15:18:15 +0300103 if (sta->sdata == sdata && !sta->dummy &&
104 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
105 break;
106 sta = rcu_dereference_check(sta->hnext,
107 lockdep_is_held(&local->sta_lock) ||
108 lockdep_is_held(&local->sta_mtx));
109 }
110 return sta;
111}
112
113/* get a station info entry even if it is a dummy station*/
114struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
115 const u8 *addr)
116{
117 struct ieee80211_local *local = sdata->local;
118 struct sta_info *sta;
119
120 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
121 lockdep_is_held(&local->sta_lock) ||
122 lockdep_is_held(&local->sta_mtx));
123 while (sta) {
Johannes Bergabe60632009-11-25 17:46:18 +0100124 if (sta->sdata == sdata &&
125 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100126 break;
Johannes Berg03791852010-04-06 11:18:42 +0200127 sta = rcu_dereference_check(sta->hnext,
Johannes Berg03791852010-04-06 11:18:42 +0200128 lockdep_is_held(&local->sta_lock) ||
129 lockdep_is_held(&local->sta_mtx));
Johannes Berg43ba7e92008-02-21 14:09:30 +0100130 }
131 return sta;
132}
133
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100134/*
135 * Get sta info either from the specified interface
136 * or from one of its vlans
137 */
138struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
139 const u8 *addr)
140{
141 struct ieee80211_local *local = sdata->local;
142 struct sta_info *sta;
143
Johannes Berg03791852010-04-06 11:18:42 +0200144 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
Johannes Berg03791852010-04-06 11:18:42 +0200145 lockdep_is_held(&local->sta_lock) ||
146 lockdep_is_held(&local->sta_mtx));
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100147 while (sta) {
148 if ((sta->sdata == sdata ||
Johannes Berga2c1e3d2010-09-14 21:34:14 +0200149 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
Guy Eilam2a33bee2011-08-17 15:18:15 +0300150 !sta->dummy &&
151 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
152 break;
153 sta = rcu_dereference_check(sta->hnext,
154 lockdep_is_held(&local->sta_lock) ||
155 lockdep_is_held(&local->sta_mtx));
156 }
157 return sta;
158}
159
160/*
161 * Get sta info either from the specified interface
162 * or from one of its vlans (including dummy stations)
163 */
164struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
165 const u8 *addr)
166{
167 struct ieee80211_local *local = sdata->local;
168 struct sta_info *sta;
169
170 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
171 lockdep_is_held(&local->sta_lock) ||
172 lockdep_is_held(&local->sta_mtx));
173 while (sta) {
174 if ((sta->sdata == sdata ||
175 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100176 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
177 break;
Johannes Berg03791852010-04-06 11:18:42 +0200178 sta = rcu_dereference_check(sta->hnext,
Johannes Berg03791852010-04-06 11:18:42 +0200179 lockdep_is_held(&local->sta_lock) ||
180 lockdep_is_held(&local->sta_mtx));
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100181 }
182 return sta;
183}
184
Johannes Berg3b53fde82009-11-16 12:00:37 +0100185struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
186 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100187{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100188 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100189 struct sta_info *sta;
190 int i = 0;
191
Johannes Bergd0709a62008-02-25 16:27:46 +0100192 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100193 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800194 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100195 if (i < idx) {
196 ++i;
197 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100198 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800199 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100200 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100201
202 return NULL;
203}
Jiri Bencf0706e82007-05-05 11:45:53 -0700204
Johannes Berg93e5deb2008-04-01 15:21:00 +0200205/**
206 * __sta_info_free - internal STA free helper
207 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700208 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200209 * @sta: STA info to free
210 *
211 * This function must undo everything done by sta_info_alloc()
212 * that may happen before sta_info_insert().
213 */
214static void __sta_info_free(struct ieee80211_local *local,
215 struct sta_info *sta)
216{
Johannes Bergaf65cd962009-11-17 18:18:36 +0100217 if (sta->rate_ctrl) {
218 rate_control_free_sta(sta);
219 rate_control_put(sta->rate_ctrl);
220 }
Johannes Berg93e5deb2008-04-01 15:21:00 +0200221
222#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700223 wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200224#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
225
226 kfree(sta);
227}
228
Johannes Bergd0709a62008-02-25 16:27:46 +0100229/* Caller must hold local->sta_lock */
230static void sta_info_hash_add(struct ieee80211_local *local,
231 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700232{
Johannes Berg17741cd2008-09-11 00:02:02 +0200233 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
234 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700235}
Jiri Bencf0706e82007-05-05 11:45:53 -0700236
Johannes Bergaf818582009-11-06 11:35:50 +0100237static void sta_unblock(struct work_struct *wk)
238{
239 struct sta_info *sta;
240
241 sta = container_of(wk, struct sta_info, drv_unblock_wk);
242
243 if (sta->dead)
244 return;
245
246 if (!test_sta_flags(sta, WLAN_STA_PS_STA))
247 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg50a94322010-11-16 11:50:28 -0800248 else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) {
249 clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
Johannes Bergaf818582009-11-06 11:35:50 +0100250 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg50a94322010-11-16 11:50:28 -0800251 } else
252 clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
Johannes Bergaf818582009-11-06 11:35:50 +0100253}
254
Johannes Bergaf65cd962009-11-17 18:18:36 +0100255static int sta_prepare_rate_control(struct ieee80211_local *local,
256 struct sta_info *sta, gfp_t gfp)
257{
258 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
259 return 0;
260
261 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
262 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
263 &sta->sta, gfp);
264 if (!sta->rate_ctrl_priv) {
265 rate_control_put(sta->rate_ctrl);
266 return -ENOMEM;
267 }
268
269 return 0;
270}
271
Johannes Berg73651ee2008-02-25 16:27:47 +0100272struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
273 u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700274{
Johannes Bergd0709a62008-02-25 16:27:46 +0100275 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700276 struct sta_info *sta;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530277 struct timespec uptime;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200278 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700279
Johannes Berg17741cd2008-09-11 00:02:02 +0200280 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700281 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100282 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700283
Johannes Berg07346f812008-05-03 01:02:02 +0200284 spin_lock_init(&sta->lock);
Johannes Berg5a9f7b02008-06-18 14:58:09 +0200285 spin_lock_init(&sta->flaglock);
Johannes Bergaf818582009-11-06 11:35:50 +0100286 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
Johannes Berg67c282c2010-06-10 10:21:43 +0200287 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200288 mutex_init(&sta->ampdu_mlme.mtx);
Johannes Berg07346f812008-05-03 01:02:02 +0200289
Johannes Berg17741cd2008-09-11 00:02:02 +0200290 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100291 sta->local = local;
292 sta->sdata = sdata;
Felix Fietkau8bc8aec2011-03-21 20:01:00 +0100293 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700294
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530295 do_posix_clock_monotonic_gettime(&uptime);
296 sta->last_connected = uptime.tv_sec;
Bruno Randolf541a45a2010-12-02 19:12:43 +0900297 ewma_init(&sta->avg_signal, 1024, 8);
298
Johannes Bergaf65cd962009-11-17 18:18:36 +0100299 if (sta_prepare_rate_control(local, sta, gfp)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700300 kfree(sta);
Johannes Berg73651ee2008-02-25 16:27:47 +0100301 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700302 }
303
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200304 for (i = 0; i < STA_TID_NUM; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200305 /*
306 * timer_to_tid must be initialized with identity mapping
307 * to enable session_timer's data differentiation. See
308 * sta_rx_agg_session_timer_expired for usage.
309 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200310 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200311 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700312 skb_queue_head_init(&sta->ps_tx_buf);
313 skb_queue_head_init(&sta->tx_filtered);
Johannes Berg73651ee2008-02-25 16:27:47 +0100314
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530315 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700316 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530317
Johannes Berg73651ee2008-02-25 16:27:47 +0100318#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700319 wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
Johannes Berg73651ee2008-02-25 16:27:47 +0100320#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
321
Johannes Berg03e44972008-02-27 09:56:40 +0100322#ifdef CONFIG_MAC80211_MESH
Javier Cardona57cf8042011-05-13 10:45:43 -0700323 sta->plink_state = NL80211_PLINK_LISTEN;
Johannes Berg03e44972008-02-27 09:56:40 +0100324 init_timer(&sta->plink_timer);
325#endif
326
Johannes Berg73651ee2008-02-25 16:27:47 +0100327 return sta;
328}
329
Guy Eilam2a33bee2011-08-17 15:18:15 +0300330static int sta_info_finish_insert(struct sta_info *sta,
331 bool async, bool dummy_reinsert)
Johannes Berg73651ee2008-02-25 16:27:47 +0100332{
333 struct ieee80211_local *local = sta->local;
334 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg98b62182009-12-23 13:15:44 +0100335 struct station_info sinfo;
Johannes Berg73651ee2008-02-25 16:27:47 +0100336 unsigned long flags;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200337 int err = 0;
Johannes Berg73651ee2008-02-25 16:27:47 +0100338
Johannes Berg46a5eba2010-09-15 13:28:15 +0200339 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100340
Guy Eilam2a33bee2011-08-17 15:18:15 +0300341 if (!sta->dummy || dummy_reinsert) {
342 /* notify driver */
343 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
344 sdata = container_of(sdata->bss,
345 struct ieee80211_sub_if_data,
346 u.ap);
347 err = drv_sta_add(local, sdata, &sta->sta);
348 if (err) {
349 if (!async)
350 return err;
351 printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
352 "driver (%d) - keeping it anyway.\n",
353 sdata->name, sta->sta.addr, err);
354 } else {
355 sta->uploaded = true;
Johannes Berg34e89502010-02-03 13:59:58 +0100356#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Guy Eilam2a33bee2011-08-17 15:18:15 +0300357 if (async)
358 wiphy_debug(local->hw.wiphy,
359 "Finished adding IBSS STA %pM\n",
360 sta->sta.addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100361#endif
Guy Eilam2a33bee2011-08-17 15:18:15 +0300362 }
363
364 sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100365 }
366
Guy Eilam2a33bee2011-08-17 15:18:15 +0300367 if (!dummy_reinsert) {
368 if (!async) {
369 local->num_sta++;
370 local->sta_generation++;
371 smp_mb();
Johannes Berg34e89502010-02-03 13:59:58 +0100372
Guy Eilam2a33bee2011-08-17 15:18:15 +0300373 /* make the station visible */
374 spin_lock_irqsave(&local->sta_lock, flags);
375 sta_info_hash_add(local, sta);
376 spin_unlock_irqrestore(&local->sta_lock, flags);
377 }
Johannes Berg34e89502010-02-03 13:59:58 +0100378
Guy Eilam2a33bee2011-08-17 15:18:15 +0300379 list_add(&sta->list, &local->sta_list);
380 } else {
381 sta->dummy = false;
Johannes Berg34e89502010-02-03 13:59:58 +0100382 }
383
Guy Eilam2a33bee2011-08-17 15:18:15 +0300384 if (!sta->dummy) {
385 ieee80211_sta_debugfs_add(sta);
386 rate_control_add_sta_debugfs(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100387
Guy Eilam2a33bee2011-08-17 15:18:15 +0300388 memset(&sinfo, 0, sizeof(sinfo));
389 sinfo.filled = 0;
390 sinfo.generation = local->sta_generation;
391 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
392 }
Johannes Berg34e89502010-02-03 13:59:58 +0100393
394 return 0;
395}
396
397static void sta_info_finish_pending(struct ieee80211_local *local)
398{
399 struct sta_info *sta;
400 unsigned long flags;
401
402 spin_lock_irqsave(&local->sta_lock, flags);
403 while (!list_empty(&local->sta_pending_list)) {
404 sta = list_first_entry(&local->sta_pending_list,
405 struct sta_info, list);
406 list_del(&sta->list);
407 spin_unlock_irqrestore(&local->sta_lock, flags);
408
Guy Eilam2a33bee2011-08-17 15:18:15 +0300409 sta_info_finish_insert(sta, true, false);
Johannes Berg34e89502010-02-03 13:59:58 +0100410
411 spin_lock_irqsave(&local->sta_lock, flags);
412 }
413 spin_unlock_irqrestore(&local->sta_lock, flags);
414}
415
416static void sta_info_finish_work(struct work_struct *work)
417{
418 struct ieee80211_local *local =
419 container_of(work, struct ieee80211_local, sta_finish_work);
420
421 mutex_lock(&local->sta_mtx);
422 sta_info_finish_pending(local);
423 mutex_unlock(&local->sta_mtx);
424}
425
Guy Eilam8c71df72011-08-17 15:18:14 +0300426static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100427{
Johannes Berg34e89502010-02-03 13:59:58 +0100428 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100429
Johannes Berg03e44972008-02-27 09:56:40 +0100430 /*
431 * Can't be a WARN_ON because it can be triggered through a race:
432 * something inserts a STA (on one CPU) without holding the RTNL
433 * and another CPU turns off the net device.
434 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300435 if (unlikely(!ieee80211_sdata_running(sdata)))
436 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100437
Johannes Berg47846c92009-11-25 17:46:19 +0100438 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300439 is_multicast_ether_addr(sta->sta.addr)))
440 return -EINVAL;
441
442 return 0;
443}
444
445static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU)
446{
447 struct ieee80211_local *local = sta->local;
448 struct ieee80211_sub_if_data *sdata = sta->sdata;
449 unsigned long flags;
450
451 spin_lock_irqsave(&local->sta_lock, flags);
452 /* check if STA exists already */
Guy Eilam2a33bee2011-08-17 15:18:15 +0300453 if (sta_info_get_bss_rx(sdata, sta->sta.addr)) {
Guy Eilam8c71df72011-08-17 15:18:14 +0300454 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg34e89502010-02-03 13:59:58 +0100455 rcu_read_lock();
Guy Eilam8c71df72011-08-17 15:18:14 +0300456 return -EEXIST;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200457 }
Johannes Berg44213b52008-02-25 16:27:49 +0100458
Guy Eilam8c71df72011-08-17 15:18:14 +0300459 local->num_sta++;
460 local->sta_generation++;
461 smp_mb();
462 sta_info_hash_add(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100463
Guy Eilam8c71df72011-08-17 15:18:14 +0300464 list_add_tail(&sta->list, &local->sta_pending_list);
Johannes Berg34e89502010-02-03 13:59:58 +0100465
Guy Eilam8c71df72011-08-17 15:18:14 +0300466 rcu_read_lock();
467 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg34e89502010-02-03 13:59:58 +0100468
469#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Guy Eilam8c71df72011-08-17 15:18:14 +0300470 wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
471 sta->sta.addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100472#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
473
Guy Eilam8c71df72011-08-17 15:18:14 +0300474 ieee80211_queue_work(&local->hw, &local->sta_finish_work);
Johannes Berg34e89502010-02-03 13:59:58 +0100475
Guy Eilam8c71df72011-08-17 15:18:14 +0300476 return 0;
477}
478
479/*
480 * should be called with sta_mtx locked
481 * this function replaces the mutex lock
482 * with a RCU lock
483 */
484static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU)
485{
486 struct ieee80211_local *local = sta->local;
487 struct ieee80211_sub_if_data *sdata = sta->sdata;
488 unsigned long flags;
Guy Eilam2a33bee2011-08-17 15:18:15 +0300489 struct sta_info *exist_sta;
490 bool dummy_reinsert = false;
Guy Eilam8c71df72011-08-17 15:18:14 +0300491 int err = 0;
492
493 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100494
495 /*
496 * On first glance, this will look racy, because the code
Guy Eilam8c71df72011-08-17 15:18:14 +0300497 * in this function, which inserts a station with sleeping,
Johannes Berg34e89502010-02-03 13:59:58 +0100498 * unlocks the sta_lock between checking existence in the
499 * hash table and inserting into it.
500 *
501 * However, it is not racy against itself because it keeps
Guy Eilam8c71df72011-08-17 15:18:14 +0300502 * the mutex locked.
Johannes Berg34e89502010-02-03 13:59:58 +0100503 */
504
Johannes Bergd0709a62008-02-25 16:27:46 +0100505 spin_lock_irqsave(&local->sta_lock, flags);
Guy Eilam2a33bee2011-08-17 15:18:15 +0300506 /*
507 * check if STA exists already.
508 * only accept a scenario of a second call to sta_info_insert_non_ibss
509 * with a dummy station entry that was inserted earlier
510 * in that case - assume that the dummy station flag should
511 * be removed.
512 */
513 exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
514 if (exist_sta) {
515 if (exist_sta == sta && sta->dummy) {
516 dummy_reinsert = true;
517 } else {
518 spin_unlock_irqrestore(&local->sta_lock, flags);
519 mutex_unlock(&local->sta_mtx);
520 rcu_read_lock();
521 return -EEXIST;
522 }
Johannes Berg43ba7e92008-02-21 14:09:30 +0100523 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100524
Johannes Berg34e89502010-02-03 13:59:58 +0100525 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +0100526
Guy Eilam2a33bee2011-08-17 15:18:15 +0300527 err = sta_info_finish_insert(sta, false, dummy_reinsert);
Johannes Berg34e89502010-02-03 13:59:58 +0100528 if (err) {
529 mutex_unlock(&local->sta_mtx);
530 rcu_read_lock();
Guy Eilam8c71df72011-08-17 15:18:14 +0300531 return err;
Johannes Berg32bfd352007-12-19 01:31:26 +0100532 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100533
Jiri Bencf0706e82007-05-05 11:45:53 -0700534#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Guy Eilam2a33bee2011-08-17 15:18:15 +0300535 wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
536 sta->dummy ? "dummy " : "", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700537#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
538
Johannes Berg34e89502010-02-03 13:59:58 +0100539 /* move reference to rcu-protected */
540 rcu_read_lock();
541 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700542
Johannes Berg73651ee2008-02-25 16:27:47 +0100543 if (ieee80211_vif_is_mesh(&sdata->vif))
544 mesh_accept_plinks_update(sdata);
545
546 return 0;
Guy Eilam8c71df72011-08-17 15:18:14 +0300547}
548
549int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
550{
551 struct ieee80211_local *local = sta->local;
552 struct ieee80211_sub_if_data *sdata = sta->sdata;
553 int err = 0;
554
555 err = sta_info_insert_check(sta);
556 if (err) {
557 rcu_read_lock();
558 goto out_free;
559 }
560
561 /*
562 * In ad-hoc mode, we sometimes need to insert stations
563 * from tasklet context from the RX path. To avoid races,
564 * always do so in that case -- see the comment below.
565 */
566 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
567 err = sta_info_insert_ibss(sta);
568 if (err)
569 goto out_free;
570
571 return 0;
572 }
573
574 /*
575 * It might seem that the function called below is in race against
576 * the function call above that atomically inserts the station... That,
577 * however, is not true because the above code can only
578 * be invoked for IBSS interfaces, and the below code will
579 * not be -- and the two do not race against each other as
580 * the hash table also keys off the interface.
581 */
582
583 might_sleep();
584
585 mutex_lock(&local->sta_mtx);
586
587 err = sta_info_insert_non_ibss(sta);
588 if (err)
589 goto out_free;
590
591 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200592 out_free:
593 BUG_ON(!err);
594 __sta_info_free(local, sta);
595 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700596}
597
Johannes Berg34e89502010-02-03 13:59:58 +0100598int sta_info_insert(struct sta_info *sta)
599{
600 int err = sta_info_insert_rcu(sta);
601
602 rcu_read_unlock();
603
604 return err;
605}
606
Guy Eilam2a33bee2011-08-17 15:18:15 +0300607/* Caller must hold sta->local->sta_mtx */
608int sta_info_reinsert(struct sta_info *sta)
609{
610 struct ieee80211_local *local = sta->local;
611 int err = 0;
612
613 err = sta_info_insert_check(sta);
614 if (err) {
615 mutex_unlock(&local->sta_mtx);
616 return err;
617 }
618
619 might_sleep();
620
621 err = sta_info_insert_non_ibss(sta);
622 rcu_read_unlock();
623 return err;
624}
625
Johannes Berg004c8722008-02-20 11:21:35 +0100626static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
627{
628 /*
629 * This format has been mandated by the IEEE specifications,
630 * so this line may not be changed to use the __set_bit() format.
631 */
632 bss->tim[aid / 8] |= (1 << (aid % 8));
633}
634
635static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
636{
637 /*
638 * This format has been mandated by the IEEE specifications,
639 * so this line may not be changed to use the __clear_bit() format.
640 */
641 bss->tim[aid / 8] &= ~(1 << (aid % 8));
642}
643
Johannes Bergc868cb352011-09-29 16:04:27 +0200644void sta_info_recalc_tim(struct sta_info *sta)
Johannes Berg004c8722008-02-20 11:21:35 +0100645{
Johannes Bergc868cb352011-09-29 16:04:27 +0200646 struct ieee80211_local *local = sta->local;
647 struct ieee80211_if_ap *bss = sta->sdata->bss;
Johannes Bergd0709a62008-02-25 16:27:46 +0100648 unsigned long flags;
Johannes Bergc868cb352011-09-29 16:04:27 +0200649 bool have_data = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100650
Johannes Bergc868cb352011-09-29 16:04:27 +0200651 if (WARN_ON_ONCE(!sta->sdata->bss))
652 return;
Johannes Berg3e122be2008-07-09 14:40:34 +0200653
Johannes Bergc868cb352011-09-29 16:04:27 +0200654 /* No need to do anything if the driver does all */
655 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
656 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100657
Johannes Bergc868cb352011-09-29 16:04:27 +0200658 if (sta->dead)
659 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200660
Johannes Bergc868cb352011-09-29 16:04:27 +0200661 have_data = test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF) ||
662 !skb_queue_empty(&sta->tx_filtered) ||
663 !skb_queue_empty(&sta->ps_tx_buf);
Johannes Berg3e122be2008-07-09 14:40:34 +0200664
Johannes Bergc868cb352011-09-29 16:04:27 +0200665 done:
666 spin_lock_irqsave(&local->sta_lock, flags);
667
668 if (have_data)
669 __bss_tim_set(bss, sta->sta.aid);
670 else
671 __bss_tim_clear(bss, sta->sta.aid);
672
673 if (local->ops->set_tim) {
674 local->tim_in_locked_section = true;
675 drv_set_tim(local, &sta->sta, have_data);
676 local->tim_in_locked_section = false;
Johannes Bergd0709a62008-02-25 16:27:46 +0100677 }
Johannes Berg004c8722008-02-20 11:21:35 +0100678
Johannes Bergc868cb352011-09-29 16:04:27 +0200679 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100680}
681
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200682static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700683{
Johannes Berge039fa42008-05-15 12:55:29 +0200684 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700685 int timeout;
686
687 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200688 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700689
Johannes Berge039fa42008-05-15 12:55:29 +0200690 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700691
692 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200693 timeout = (sta->listen_interval *
694 sta->sdata->vif.bss_conf.beacon_int *
695 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700696 if (timeout < STA_TX_BUFFER_EXPIRE)
697 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200698 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700699}
700
701
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300702static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -0700703 struct sta_info *sta)
704{
705 unsigned long flags;
706 struct sk_buff *skb;
707
Johannes Bergc868cb352011-09-29 16:04:27 +0200708 /* This is only necessary for stations on BSS interfaces */
709 if (!sta->sdata->bss)
710 return false;
711
Johannes Berg60750392011-09-29 16:04:28 +0200712 /*
713 * First check for frames that should expire on the filtered
714 * queue. Frames here were rejected by the driver and are on
715 * a separate queue to avoid reordering with normal PS-buffered
716 * frames. They also aren't accounted for right now in the
717 * total_ps_buffered counter.
718 */
719 for (;;) {
720 spin_lock_irqsave(&sta->tx_filtered.lock, flags);
721 skb = skb_peek(&sta->tx_filtered);
722 if (sta_info_buffer_expired(sta, skb))
723 skb = __skb_dequeue(&sta->tx_filtered);
724 else
725 skb = NULL;
726 spin_unlock_irqrestore(&sta->tx_filtered.lock, flags);
727
728 /*
729 * Frames are queued in order, so if this one
730 * hasn't expired yet we can stop testing. If
731 * we actually reached the end of the queue we
732 * also need to stop, of course.
733 */
734 if (!skb)
735 break;
736 dev_kfree_skb(skb);
737 }
738
739 /*
740 * Now also check the normal PS-buffered queue, this will
741 * only find something if the filtered queue was emptied
742 * since the filtered frames are all before the normal PS
743 * buffered frames.
744 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700745 for (;;) {
746 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
747 skb = skb_peek(&sta->ps_tx_buf);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200748 if (sta_info_buffer_expired(sta, skb))
Jiri Bencf0706e82007-05-05 11:45:53 -0700749 skb = __skb_dequeue(&sta->ps_tx_buf);
Johannes Berg836341a2008-02-20 02:07:21 +0100750 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 skb = NULL;
752 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
753
Johannes Berg60750392011-09-29 16:04:28 +0200754 /*
755 * frames are queued in order, so if this one
756 * hasn't expired yet (or we reached the end of
757 * the queue) we can stop testing
758 */
Johannes Berg836341a2008-02-20 02:07:21 +0100759 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700760 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100761
Johannes Berg836341a2008-02-20 02:07:21 +0100762 local->total_ps_buffered--;
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200763#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700764 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
765 sta->sta.addr);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200766#endif
Johannes Berg836341a2008-02-20 02:07:21 +0100767 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700768 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300769
Johannes Berg60750392011-09-29 16:04:28 +0200770 /*
771 * Finally, recalculate the TIM bit for this station -- it might
772 * now be clear because the station was too slow to retrieve its
773 * frames.
774 */
775 sta_info_recalc_tim(sta);
776
777 /*
778 * Return whether there are any frames still buffered, this is
779 * used to check whether the cleanup timer still needs to run,
780 * if there are no frames we don't need to rearm the timer.
781 */
782 return !(skb_queue_empty(&sta->ps_tx_buf) &&
783 skb_queue_empty(&sta->tx_filtered));
Jiri Bencf0706e82007-05-05 11:45:53 -0700784}
785
Johannes Berg34e89502010-02-03 13:59:58 +0100786static int __must_check __sta_info_destroy(struct sta_info *sta)
787{
788 struct ieee80211_local *local;
789 struct ieee80211_sub_if_data *sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100790 unsigned long flags;
Johannes Berge31b8212010-10-05 19:39:30 +0200791 int ret, i;
Johannes Berg34e89502010-02-03 13:59:58 +0100792
793 might_sleep();
794
795 if (!sta)
796 return -ENOENT;
797
798 local = sta->local;
799 sdata = sta->sdata;
800
Johannes Berg098a6072010-04-06 11:18:47 +0200801 /*
802 * Before removing the station from the driver and
803 * rate control, it might still start new aggregation
804 * sessions -- block that to make sure the tear-down
805 * will be sufficient.
806 */
807 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +0200808 ieee80211_sta_tear_down_BA_sessions(sta, true);
Johannes Berg098a6072010-04-06 11:18:47 +0200809
Johannes Berg34e89502010-02-03 13:59:58 +0100810 spin_lock_irqsave(&local->sta_lock, flags);
811 ret = sta_info_hash_del(local, sta);
812 /* this might still be the pending list ... which is fine */
813 if (!ret)
814 list_del(&sta->list);
815 spin_unlock_irqrestore(&local->sta_lock, flags);
816 if (ret)
817 return ret;
818
Johannes Berg8cb23152011-05-12 15:07:15 +0200819 mutex_lock(&local->key_mtx);
Johannes Berge31b8212010-10-05 19:39:30 +0200820 for (i = 0; i < NUM_DEFAULT_KEYS; i++)
Johannes Berg40b275b2011-05-13 14:15:49 +0200821 __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
Johannes Berge31b8212010-10-05 19:39:30 +0200822 if (sta->ptk)
Johannes Berg40b275b2011-05-13 14:15:49 +0200823 __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
Johannes Berg8cb23152011-05-12 15:07:15 +0200824 mutex_unlock(&local->key_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100825
826 sta->dead = true;
827
828 if (test_and_clear_sta_flags(sta,
829 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
830 BUG_ON(!sdata->bss);
831
832 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Bergc868cb352011-09-29 16:04:27 +0200833 sta_info_recalc_tim(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100834 }
835
836 local->num_sta--;
837 local->sta_generation++;
838
839 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
840 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
841
842 if (sta->uploaded) {
843 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
844 sdata = container_of(sdata->bss,
845 struct ieee80211_sub_if_data,
846 u.ap);
847 drv_sta_remove(local, sdata, &sta->sta);
848 sdata = sta->sdata;
849 }
850
Johannes Berge64b3792010-04-06 11:18:43 +0200851 /*
852 * At this point, after we wait for an RCU grace period,
853 * neither mac80211 nor the driver can reference this
854 * sta struct any more except by still existing timers
855 * associated with this station that we clean up below.
856 */
857 synchronize_rcu();
858
Johannes Bergc868cb352011-09-29 16:04:27 +0200859 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf);
860 __skb_queue_purge(&sta->ps_tx_buf);
861 __skb_queue_purge(&sta->tx_filtered);
862
Johannes Berg34e89502010-02-03 13:59:58 +0100863#ifdef CONFIG_MAC80211_MESH
Johannes Berge64b3792010-04-06 11:18:43 +0200864 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Berg34e89502010-02-03 13:59:58 +0100865 mesh_accept_plinks_update(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100866#endif
867
868#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700869 wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100870#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
871 cancel_work_sync(&sta->drv_unblock_wk);
872
Jouni Malinenec15e682011-03-23 15:29:52 +0200873 cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
874
Johannes Berg34e89502010-02-03 13:59:58 +0100875 rate_control_remove_sta_debugfs(sta);
876 ieee80211_sta_debugfs_remove(sta);
877
878#ifdef CONFIG_MAC80211_MESH
879 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
880 mesh_plink_deactivate(sta);
881 del_timer_sync(&sta->plink_timer);
882 }
883#endif
884
Johannes Berg34e89502010-02-03 13:59:58 +0100885 __sta_info_free(local, sta);
886
887 return 0;
888}
889
890int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
891{
892 struct sta_info *sta;
893 int ret;
894
895 mutex_lock(&sdata->local->sta_mtx);
Guy Eilam2a33bee2011-08-17 15:18:15 +0300896 sta = sta_info_get_rx(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100897 ret = __sta_info_destroy(sta);
898 mutex_unlock(&sdata->local->sta_mtx);
899
900 return ret;
901}
902
903int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
904 const u8 *addr)
905{
906 struct sta_info *sta;
907 int ret;
908
909 mutex_lock(&sdata->local->sta_mtx);
Guy Eilam2a33bee2011-08-17 15:18:15 +0300910 sta = sta_info_get_bss_rx(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100911 ret = __sta_info_destroy(sta);
912 mutex_unlock(&sdata->local->sta_mtx);
913
914 return ret;
915}
Jiri Bencf0706e82007-05-05 11:45:53 -0700916
917static void sta_info_cleanup(unsigned long data)
918{
919 struct ieee80211_local *local = (struct ieee80211_local *) data;
920 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300921 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700922
Johannes Bergd0709a62008-02-25 16:27:46 +0100923 rcu_read_lock();
924 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300925 if (sta_info_cleanup_expire_buffered(local, sta))
926 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100927 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700928
Johannes Berg5bb644a2009-05-17 11:40:42 +0200929 if (local->quiescing)
930 return;
931
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300932 if (!timer_needed)
933 return;
934
Johannes Berg26d59532011-04-01 13:52:48 +0200935 mod_timer(&local->sta_cleanup,
936 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -0700937}
938
939void sta_info_init(struct ieee80211_local *local)
940{
Johannes Bergd0709a62008-02-25 16:27:46 +0100941 spin_lock_init(&local->sta_lock);
Johannes Berg34e89502010-02-03 13:59:58 +0100942 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700943 INIT_LIST_HEAD(&local->sta_list);
Johannes Berg34e89502010-02-03 13:59:58 +0100944 INIT_LIST_HEAD(&local->sta_pending_list);
945 INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
Jiri Bencf0706e82007-05-05 11:45:53 -0700946
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800947 setup_timer(&local->sta_cleanup, sta_info_cleanup,
948 (unsigned long)local);
Jiri Bencf0706e82007-05-05 11:45:53 -0700949}
950
951void sta_info_stop(struct ieee80211_local *local)
952{
Jiri Bencf0706e82007-05-05 11:45:53 -0700953 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200954 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700955}
956
Jiri Bencf0706e82007-05-05 11:45:53 -0700957/**
958 * sta_info_flush - flush matching STA entries from the STA table
Johannes Berg44213b52008-02-25 16:27:49 +0100959 *
960 * Returns the number of removed STA entries.
961 *
Jiri Bencf0706e82007-05-05 11:45:53 -0700962 * @local: local interface data
Johannes Bergd0709a62008-02-25 16:27:46 +0100963 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
Jiri Bencf0706e82007-05-05 11:45:53 -0700964 */
Johannes Berg44213b52008-02-25 16:27:49 +0100965int sta_info_flush(struct ieee80211_local *local,
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200966 struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -0700967{
968 struct sta_info *sta, *tmp;
Johannes Berg44213b52008-02-25 16:27:49 +0100969 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700970
Johannes Bergd0709a62008-02-25 16:27:46 +0100971 might_sleep();
972
Johannes Berg34e89502010-02-03 13:59:58 +0100973 mutex_lock(&local->sta_mtx);
Johannes Bergd0709a62008-02-25 16:27:46 +0100974
Johannes Berg34e89502010-02-03 13:59:58 +0100975 sta_info_finish_pending(local);
976
977 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
978 if (!sdata || sdata == sta->sdata)
979 WARN_ON(__sta_info_destroy(sta));
980 }
981 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +0100982
983 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -0700984}
Johannes Bergdc6676b2008-03-31 19:23:03 +0200985
Johannes Berg24723d12008-09-11 00:01:46 +0200986void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
987 unsigned long exp_time)
988{
989 struct ieee80211_local *local = sdata->local;
990 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +0200991
Johannes Berg34e89502010-02-03 13:59:58 +0100992 mutex_lock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +0200993 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
994 if (time_after(jiffies, sta->last_rx + exp_time)) {
995#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700996 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100997 sdata->name, sta->sta.addr);
Johannes Berg24723d12008-09-11 00:01:46 +0200998#endif
Johannes Berg34e89502010-02-03 13:59:58 +0100999 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001000 }
Johannes Berg34e89502010-02-03 13:59:58 +01001001 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001002}
Johannes Berg17741cd2008-09-11 00:02:02 +02001003
Ben Greear686b9cb2010-09-23 09:44:36 -07001004struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1005 const u8 *addr,
1006 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001007{
Johannes Bergabe60632009-11-25 17:46:18 +01001008 struct sta_info *sta, *nxt;
Johannes Berg17741cd2008-09-11 00:02:02 +02001009
Ben Greear686b9cb2010-09-23 09:44:36 -07001010 /*
1011 * Just return a random station if localaddr is NULL
1012 * ... first in list.
1013 */
Johannes Bergf7c65592010-04-30 13:48:36 +02001014 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001015 if (localaddr &&
1016 compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
1017 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001018 if (!sta->uploaded)
1019 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001020 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001021 }
1022
Johannes Bergabe60632009-11-25 17:46:18 +01001023 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001024}
Ben Greear686b9cb2010-09-23 09:44:36 -07001025EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001026
1027struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1028 const u8 *addr)
1029{
Johannes Bergf7c65592010-04-30 13:48:36 +02001030 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001031
1032 if (!vif)
1033 return NULL;
1034
Johannes Bergf7c65592010-04-30 13:48:36 +02001035 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1036 if (!sta)
1037 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001038
Johannes Bergf7c65592010-04-30 13:48:36 +02001039 if (!sta->uploaded)
1040 return NULL;
1041
1042 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001043}
Johannes Berg17741cd2008-09-11 00:02:02 +02001044EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001045
Johannes Berg50a94322010-11-16 11:50:28 -08001046static void clear_sta_ps_flags(void *_sta)
1047{
1048 struct sta_info *sta = _sta;
1049
1050 clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA);
1051}
1052
Johannes Bergaf818582009-11-06 11:35:50 +01001053/* powersave support code */
1054void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1055{
1056 struct ieee80211_sub_if_data *sdata = sta->sdata;
1057 struct ieee80211_local *local = sdata->local;
1058 int sent, buffered;
1059
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001060 clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001061 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1062 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001063
Johannes Bergaf818582009-11-06 11:35:50 +01001064 /* Send all buffered frames to the station */
1065 sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
Johannes Berg50a94322010-11-16 11:50:28 -08001066 buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf,
1067 clear_sta_ps_flags, sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001068 sent += buffered;
1069 local->total_ps_buffered -= buffered;
1070
Johannes Bergc868cb352011-09-29 16:04:27 +02001071 sta_info_recalc_tim(sta);
1072
Johannes Bergaf818582009-11-06 11:35:50 +01001073#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1074 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
Johannes Berg47846c92009-11-25 17:46:19 +01001075 "since STA not sleeping anymore\n", sdata->name,
Johannes Bergaf818582009-11-06 11:35:50 +01001076 sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
1077#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1078}
1079
1080void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1081{
1082 struct ieee80211_sub_if_data *sdata = sta->sdata;
1083 struct ieee80211_local *local = sdata->local;
1084 struct sk_buff *skb;
1085 int no_pending_pkts;
1086
1087 skb = skb_dequeue(&sta->tx_filtered);
1088 if (!skb) {
1089 skb = skb_dequeue(&sta->ps_tx_buf);
1090 if (skb)
1091 local->total_ps_buffered--;
1092 }
1093 no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
1094 skb_queue_empty(&sta->ps_tx_buf);
1095
1096 if (skb) {
1097 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1098 struct ieee80211_hdr *hdr =
1099 (struct ieee80211_hdr *) skb->data;
1100
1101 /*
1102 * Tell TX path to send this frame even though the STA may
1103 * still remain is PS mode after this frame exchange.
1104 */
1105 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
1106
1107#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1108 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
1109 sta->sta.addr, sta->sta.aid,
1110 skb_queue_len(&sta->ps_tx_buf));
1111#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1112
1113 /* Use MoreData flag to indicate whether there are more
1114 * buffered frames for this STA */
1115 if (no_pending_pkts)
1116 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1117 else
1118 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1119
1120 ieee80211_add_pending_skb(local, skb);
1121
Johannes Bergc868cb352011-09-29 16:04:27 +02001122 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001123#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1124 } else {
1125 /*
1126 * FIXME: This can be the result of a race condition between
1127 * us expiring a frame and the station polling for it.
1128 * Should we send it a null-func frame indicating we
1129 * have nothing buffered for it?
1130 */
1131 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
1132 "though there are no buffered frames for it\n",
Johannes Berg47846c92009-11-25 17:46:19 +01001133 sdata->name, sta->sta.addr);
Johannes Bergaf818582009-11-06 11:35:50 +01001134#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1135 }
1136}
1137
1138void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1139 struct ieee80211_sta *pubsta, bool block)
1140{
1141 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1142
Johannes Bergb5878a22010-04-07 16:48:40 +02001143 trace_api_sta_block_awake(sta->local, pubsta, block);
1144
Johannes Bergaf818582009-11-06 11:35:50 +01001145 if (block)
1146 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
Johannes Berg50a94322010-11-16 11:50:28 -08001147 else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER))
Johannes Bergaf818582009-11-06 11:35:50 +01001148 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1149}
1150EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001151
Johannes Berg042ec452011-09-29 16:04:26 +02001152void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1153 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001154{
1155 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1156
Johannes Berg042ec452011-09-29 16:04:26 +02001157 if (!buffered)
1158 return;
1159
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001160 set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
Johannes Bergc868cb352011-09-29 16:04:27 +02001161 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001162}
Johannes Berg042ec452011-09-29 16:04:26 +02001163EXPORT_SYMBOL(ieee80211_sta_set_buffered);