]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac80211/sta_info.c
mac80211: fix race condition between assoc_done and first EAP packet
[mirror_ubuntu-bionic-kernel.git] / net / mac80211 / sta_info.c
CommitLineData
f0706e82
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/netdevice.h>
13#include <linux/types.h>
14#include <linux/slab.h>
15#include <linux/skbuff.h>
16#include <linux/if_arp.h>
0d174406 17#include <linux/timer.h>
d0709a65 18#include <linux/rtnetlink.h>
f0706e82
JB
19
20#include <net/mac80211.h>
21#include "ieee80211_i.h"
24487981 22#include "driver-ops.h"
2c8dccc7 23#include "rate.h"
f0706e82 24#include "sta_info.h"
e9f207f0 25#include "debugfs_sta.h"
ee385855 26#include "mesh.h"
f0706e82 27
d0709a65
JB
28/**
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 *
34e89507
JB
35 * 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.
93e5deb1
JB
43 *
44 * When the insertion fails (sta_info_insert()) returns non-zero), the
45 * structure will have been freed by sta_info_insert()!
d0709a65 46 *
34e89507 47 * Station entries are added by mac80211 when you establish a link with a
7e189a12
LR
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
25985edc 50 * receive an association response from the AP. For IBSS this occurs when
34e89507 51 * get to know about a peer on the same IBSS. For WDS we add the sta for
25985edc 52 * the peer immediately upon device open. When using AP mode we add stations
34e89507 53 * for each respective station upon request from userspace through nl80211.
7e189a12 54 *
34e89507
JB
55 * In order to remove a STA info structure, various sta_info_destroy_*()
56 * calls are available.
d0709a65 57 *
34e89507
JB
58 * 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.
d0709a65 62 */
f0706e82
JB
63
64/* Caller must hold local->sta_lock */
be8755e1
MW
65static int sta_info_hash_del(struct ieee80211_local *local,
66 struct sta_info *sta)
f0706e82
JB
67{
68 struct sta_info *s;
69
40b275b6
JB
70 s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
71 lockdep_is_held(&local->sta_lock));
f0706e82 72 if (!s)
be8755e1
MW
73 return -ENOENT;
74 if (s == sta) {
17741cdc 75 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
d0709a65 76 s->hnext);
be8755e1 77 return 0;
f0706e82
JB
78 }
79
40b275b6
JB
80 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)) {
d0709a65 85 rcu_assign_pointer(s->hnext, sta->hnext);
be8755e1
MW
86 return 0;
87 }
f0706e82 88
be8755e1 89 return -ENOENT;
f0706e82
JB
90}
91
d0709a65 92/* protected by RCU */
abe60632
JB
93struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
94 const u8 *addr)
f0706e82 95{
abe60632 96 struct ieee80211_local *local = sdata->local;
f0706e82
JB
97 struct sta_info *sta;
98
2a33bee2
GE
99 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
100 lockdep_is_held(&local->sta_lock) ||
101 lockdep_is_held(&local->sta_mtx));
102 while (sta) {
103 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
0379185b 120 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
0379185b
JB
121 lockdep_is_held(&local->sta_lock) ||
122 lockdep_is_held(&local->sta_mtx));
f0706e82 123 while (sta) {
abe60632
JB
124 if (sta->sdata == sdata &&
125 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
f0706e82 126 break;
0379185b 127 sta = rcu_dereference_check(sta->hnext,
0379185b
JB
128 lockdep_is_held(&local->sta_lock) ||
129 lockdep_is_held(&local->sta_mtx));
f0706e82 130 }
43ba7e95
JB
131 return sta;
132}
133
0e5ded5a
FF
134/*
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
2a33bee2
GE
144 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
145 lockdep_is_held(&local->sta_lock) ||
146 lockdep_is_held(&local->sta_mtx));
147 while (sta) {
148 if ((sta->sdata == sdata ||
149 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
150 !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
0379185b 170 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
0379185b
JB
171 lockdep_is_held(&local->sta_lock) ||
172 lockdep_is_held(&local->sta_mtx));
0e5ded5a
FF
173 while (sta) {
174 if ((sta->sdata == sdata ||
a2c1e3da 175 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
0e5ded5a
FF
176 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
177 break;
0379185b 178 sta = rcu_dereference_check(sta->hnext,
0379185b
JB
179 lockdep_is_held(&local->sta_lock) ||
180 lockdep_is_held(&local->sta_mtx));
0e5ded5a
FF
181 }
182 return sta;
183}
184
3b53fde8
JB
185struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
186 int idx)
ee385855 187{
3b53fde8 188 struct ieee80211_local *local = sdata->local;
ee385855
LCC
189 struct sta_info *sta;
190 int i = 0;
191
d0709a65 192 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3b53fde8 193 if (sdata != sta->sdata)
2a8ca29a 194 continue;
ee385855
LCC
195 if (i < idx) {
196 ++i;
197 continue;
ee385855 198 }
2a8ca29a 199 return sta;
ee385855 200 }
ee385855
LCC
201
202 return NULL;
203}
f0706e82 204
93e5deb1
JB
205/**
206 * __sta_info_free - internal STA free helper
207 *
6ef307bc 208 * @local: pointer to the global information
93e5deb1
JB
209 * @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{
af65cd96
JB
217 if (sta->rate_ctrl) {
218 rate_control_free_sta(sta);
219 rate_control_put(sta->rate_ctrl);
220 }
93e5deb1
JB
221
222#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0fb9a9ec 223 wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
93e5deb1
JB
224#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
225
226 kfree(sta);
227}
228
d0709a65
JB
229/* Caller must hold local->sta_lock */
230static void sta_info_hash_add(struct ieee80211_local *local,
231 struct sta_info *sta)
f0706e82 232{
17741cdc
JB
233 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
234 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
f0706e82 235}
f0706e82 236
af818581
JB
237static 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);
50a9432d
JB
248 else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) {
249 clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
af818581 250 ieee80211_sta_ps_deliver_poll_response(sta);
50a9432d
JB
251 } else
252 clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
af818581
JB
253}
254
af65cd96
JB
255static 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
73651ee6
JB
272struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
273 u8 *addr, gfp_t gfp)
f0706e82 274{
d0709a65 275 struct ieee80211_local *local = sdata->local;
f0706e82 276 struct sta_info *sta;
ebe27c91 277 struct timespec uptime;
16c5f15c 278 int i;
f0706e82 279
17741cdc 280 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
f0706e82 281 if (!sta)
73651ee6 282 return NULL;
f0706e82 283
07346f81 284 spin_lock_init(&sta->lock);
5a9f7b04 285 spin_lock_init(&sta->flaglock);
af818581 286 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
67c282c0 287 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
a93e3644 288 mutex_init(&sta->ampdu_mlme.mtx);
07346f81 289
17741cdc 290 memcpy(sta->sta.addr, addr, ETH_ALEN);
d0709a65
JB
291 sta->local = local;
292 sta->sdata = sdata;
8bc8aecd 293 sta->last_rx = jiffies;
f0706e82 294
ebe27c91
MSS
295 do_posix_clock_monotonic_gettime(&uptime);
296 sta->last_connected = uptime.tv_sec;
541a45a1
BR
297 ewma_init(&sta->avg_signal, 1024, 8);
298
af65cd96 299 if (sta_prepare_rate_control(local, sta, gfp)) {
f0706e82 300 kfree(sta);
73651ee6 301 return NULL;
f0706e82
JB
302 }
303
16c5f15c 304 for (i = 0; i < STA_TID_NUM; i++) {
a622ab72
JB
305 /*
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 */
16c5f15c 310 sta->timer_to_tid[i] = i;
16c5f15c 311 }
f0706e82
JB
312 skb_queue_head_init(&sta->ps_tx_buf);
313 skb_queue_head_init(&sta->tx_filtered);
73651ee6 314
cccaec98 315 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
4be929be 316 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
cccaec98 317
73651ee6 318#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0fb9a9ec 319 wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
73651ee6
JB
320#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
321
03e4497e 322#ifdef CONFIG_MAC80211_MESH
57cf8043 323 sta->plink_state = NL80211_PLINK_LISTEN;
03e4497e
JB
324 init_timer(&sta->plink_timer);
325#endif
326
73651ee6
JB
327 return sta;
328}
329
2a33bee2
GE
330static int sta_info_finish_insert(struct sta_info *sta,
331 bool async, bool dummy_reinsert)
73651ee6
JB
332{
333 struct ieee80211_local *local = sta->local;
334 struct ieee80211_sub_if_data *sdata = sta->sdata;
98b62183 335 struct station_info sinfo;
73651ee6 336 unsigned long flags;
93e5deb1 337 int err = 0;
73651ee6 338
46a5ebaf 339 lockdep_assert_held(&local->sta_mtx);
34e89507 340
2a33bee2
GE
341 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;
34e89507 356#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2a33bee2
GE
357 if (async)
358 wiphy_debug(local->hw.wiphy,
359 "Finished adding IBSS STA %pM\n",
360 sta->sta.addr);
34e89507 361#endif
2a33bee2
GE
362 }
363
364 sdata = sta->sdata;
34e89507
JB
365 }
366
2a33bee2
GE
367 if (!dummy_reinsert) {
368 if (!async) {
369 local->num_sta++;
370 local->sta_generation++;
371 smp_mb();
34e89507 372
2a33bee2
GE
373 /* 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 }
34e89507 378
2a33bee2
GE
379 list_add(&sta->list, &local->sta_list);
380 } else {
381 sta->dummy = false;
34e89507
JB
382 }
383
2a33bee2
GE
384 if (!sta->dummy) {
385 ieee80211_sta_debugfs_add(sta);
386 rate_control_add_sta_debugfs(sta);
34e89507 387
2a33bee2
GE
388 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 }
34e89507
JB
393
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
2a33bee2 409 sta_info_finish_insert(sta, true, false);
34e89507
JB
410
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
8c71df7a 426static int sta_info_insert_check(struct sta_info *sta)
34e89507 427{
34e89507 428 struct ieee80211_sub_if_data *sdata = sta->sdata;
34e89507 429
03e4497e
JB
430 /*
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 */
8c71df7a
GE
435 if (unlikely(!ieee80211_sdata_running(sdata)))
436 return -ENETDOWN;
03e4497e 437
47846c9b 438 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
8c71df7a
GE
439 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 */
2a33bee2 453 if (sta_info_get_bss_rx(sdata, sta->sta.addr)) {
8c71df7a 454 spin_unlock_irqrestore(&local->sta_lock, flags);
34e89507 455 rcu_read_lock();
8c71df7a 456 return -EEXIST;
93e5deb1 457 }
44213b5e 458
8c71df7a
GE
459 local->num_sta++;
460 local->sta_generation++;
461 smp_mb();
462 sta_info_hash_add(local, sta);
34e89507 463
8c71df7a 464 list_add_tail(&sta->list, &local->sta_pending_list);
34e89507 465
8c71df7a
GE
466 rcu_read_lock();
467 spin_unlock_irqrestore(&local->sta_lock, flags);
34e89507
JB
468
469#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
8c71df7a
GE
470 wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
471 sta->sta.addr);
34e89507
JB
472#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
473
8c71df7a 474 ieee80211_queue_work(&local->hw, &local->sta_finish_work);
34e89507 475
8c71df7a
GE
476 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;
2a33bee2
GE
489 struct sta_info *exist_sta;
490 bool dummy_reinsert = false;
8c71df7a
GE
491 int err = 0;
492
493 lockdep_assert_held(&local->sta_mtx);
34e89507
JB
494
495 /*
496 * On first glance, this will look racy, because the code
8c71df7a 497 * in this function, which inserts a station with sleeping,
34e89507
JB
498 * 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
8c71df7a 502 * the mutex locked.
34e89507
JB
503 */
504
d0709a65 505 spin_lock_irqsave(&local->sta_lock, flags);
2a33bee2
GE
506 /*
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 }
43ba7e95 523 }
32bfd35d 524
34e89507 525 spin_unlock_irqrestore(&local->sta_lock, flags);
32bfd35d 526
2a33bee2 527 err = sta_info_finish_insert(sta, false, dummy_reinsert);
34e89507
JB
528 if (err) {
529 mutex_unlock(&local->sta_mtx);
530 rcu_read_lock();
8c71df7a 531 return err;
32bfd35d 532 }
d0709a65 533
f0706e82 534#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2a33bee2
GE
535 wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
536 sta->dummy ? "dummy " : "", sta->sta.addr);
f0706e82
JB
537#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
538
34e89507
JB
539 /* move reference to rcu-protected */
540 rcu_read_lock();
541 mutex_unlock(&local->sta_mtx);
e9f207f0 542
73651ee6
JB
543 if (ieee80211_vif_is_mesh(&sdata->vif))
544 mesh_accept_plinks_update(sdata);
545
8c71df7a
GE
546 return 0;
547}
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
73651ee6 591 return 0;
93e5deb1
JB
592 out_free:
593 BUG_ON(!err);
594 __sta_info_free(local, sta);
595 return err;
f0706e82
JB
596}
597
34e89507
JB
598int 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
2a33bee2
GE
607/* 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
004c872e
JB
626static 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
644static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
645 struct sta_info *sta)
646{
3e122be0
JB
647 BUG_ON(!bss);
648
17741cdc 649 __bss_tim_set(bss, sta->sta.aid);
3e122be0 650
d0709a65
JB
651 if (sta->local->ops->set_tim) {
652 sta->local->tim_in_locked_section = true;
24487981 653 drv_set_tim(sta->local, &sta->sta, true);
d0709a65
JB
654 sta->local->tim_in_locked_section = false;
655 }
004c872e
JB
656}
657
658void sta_info_set_tim_bit(struct sta_info *sta)
659{
d0709a65 660 unsigned long flags;
004c872e 661
3e122be0
JB
662 BUG_ON(!sta->sdata->bss);
663
d0709a65
JB
664 spin_lock_irqsave(&sta->local->sta_lock, flags);
665 __sta_info_set_tim_bit(sta->sdata->bss, sta);
666 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
004c872e
JB
667}
668
669static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
670 struct sta_info *sta)
671{
3e122be0
JB
672 BUG_ON(!bss);
673
17741cdc 674 __bss_tim_clear(bss, sta->sta.aid);
3e122be0 675
d0709a65
JB
676 if (sta->local->ops->set_tim) {
677 sta->local->tim_in_locked_section = true;
24487981 678 drv_set_tim(sta->local, &sta->sta, false);
d0709a65
JB
679 sta->local->tim_in_locked_section = false;
680 }
004c872e
JB
681}
682
683void sta_info_clear_tim_bit(struct sta_info *sta)
684{
d0709a65 685 unsigned long flags;
004c872e 686
3e122be0
JB
687 BUG_ON(!sta->sdata->bss);
688
d0709a65
JB
689 spin_lock_irqsave(&sta->local->sta_lock, flags);
690 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
691 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
004c872e
JB
692}
693
57c4d7b4
JB
694static int sta_info_buffer_expired(struct sta_info *sta,
695 struct sk_buff *skb)
f0706e82 696{
e039fa4a 697 struct ieee80211_tx_info *info;
f0706e82
JB
698 int timeout;
699
700 if (!skb)
701 return 0;
702
e039fa4a 703 info = IEEE80211_SKB_CB(skb);
f0706e82
JB
704
705 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
57c4d7b4
JB
706 timeout = (sta->listen_interval *
707 sta->sdata->vif.bss_conf.beacon_int *
708 32 / 15625) * HZ;
f0706e82
JB
709 if (timeout < STA_TX_BUFFER_EXPIRE)
710 timeout = STA_TX_BUFFER_EXPIRE;
e039fa4a 711 return time_after(jiffies, info->control.jiffies + timeout);
f0706e82
JB
712}
713
714
3393a608 715static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
f0706e82
JB
716 struct sta_info *sta)
717{
718 unsigned long flags;
719 struct sk_buff *skb;
720
721 if (skb_queue_empty(&sta->ps_tx_buf))
3393a608 722 return false;
f0706e82
JB
723
724 for (;;) {
725 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
726 skb = skb_peek(&sta->ps_tx_buf);
57c4d7b4 727 if (sta_info_buffer_expired(sta, skb))
f0706e82 728 skb = __skb_dequeue(&sta->ps_tx_buf);
836341a7 729 else
f0706e82
JB
730 skb = NULL;
731 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
732
836341a7 733 if (!skb)
f0706e82 734 break;
836341a7 735
836341a7 736 local->total_ps_buffered--;
f4ea83dd 737#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0c68ae26
JB
738 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
739 sta->sta.addr);
f4ea83dd 740#endif
836341a7
JB
741 dev_kfree_skb(skb);
742
dcf55fb5
FF
743 if (skb_queue_empty(&sta->ps_tx_buf) &&
744 !test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF))
004c872e 745 sta_info_clear_tim_bit(sta);
f0706e82 746 }
3393a608
JO
747
748 return true;
f0706e82
JB
749}
750
34e89507 751static int __must_check __sta_info_destroy(struct sta_info *sta)
f0706e82 752{
34e89507
JB
753 struct ieee80211_local *local;
754 struct ieee80211_sub_if_data *sdata;
755 struct sk_buff *skb;
756 unsigned long flags;
e31b8213 757 int ret, i;
f0706e82 758
34e89507 759 might_sleep();
f0706e82 760
34e89507
JB
761 if (!sta)
762 return -ENOENT;
5bb644a0 763
34e89507
JB
764 local = sta->local;
765 sdata = sta->sdata;
f0706e82 766
098a6070
JB
767 /*
768 * Before removing the station from the driver and
769 * rate control, it might still start new aggregation
770 * sessions -- block that to make sure the tear-down
771 * will be sufficient.
772 */
773 set_sta_flags(sta, WLAN_STA_BLOCK_BA);
53f73c09 774 ieee80211_sta_tear_down_BA_sessions(sta, true);
098a6070 775
34e89507
JB
776 spin_lock_irqsave(&local->sta_lock, flags);
777 ret = sta_info_hash_del(local, sta);
778 /* this might still be the pending list ... which is fine */
779 if (!ret)
780 list_del(&sta->list);
781 spin_unlock_irqrestore(&local->sta_lock, flags);
782 if (ret)
783 return ret;
784
8cb23153 785 mutex_lock(&local->key_mtx);
e31b8213 786 for (i = 0; i < NUM_DEFAULT_KEYS; i++)
40b275b6 787 __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
e31b8213 788 if (sta->ptk)
40b275b6 789 __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
8cb23153 790 mutex_unlock(&local->key_mtx);
34e89507
JB
791
792 sta->dead = true;
793
794 if (test_and_clear_sta_flags(sta,
795 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
796 BUG_ON(!sdata->bss);
797
798 atomic_dec(&sdata->bss->num_sta_ps);
799 __sta_info_clear_tim_bit(sdata->bss, sta);
800 }
801
802 local->num_sta--;
803 local->sta_generation++;
804
805 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
806 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
807
808 if (sta->uploaded) {
809 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
810 sdata = container_of(sdata->bss,
811 struct ieee80211_sub_if_data,
812 u.ap);
813 drv_sta_remove(local, sdata, &sta->sta);
814 sdata = sta->sdata;
815 }
816
e64b3795
JB
817 /*
818 * At this point, after we wait for an RCU grace period,
819 * neither mac80211 nor the driver can reference this
820 * sta struct any more except by still existing timers
821 * associated with this station that we clean up below.
822 */
823 synchronize_rcu();
824
34e89507 825#ifdef CONFIG_MAC80211_MESH
e64b3795 826 if (ieee80211_vif_is_mesh(&sdata->vif))
34e89507 827 mesh_accept_plinks_update(sdata);
34e89507
JB
828#endif
829
830#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0fb9a9ec 831 wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
34e89507
JB
832#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
833 cancel_work_sync(&sta->drv_unblock_wk);
834
ec15e68b
JM
835 cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
836
34e89507
JB
837 rate_control_remove_sta_debugfs(sta);
838 ieee80211_sta_debugfs_remove(sta);
839
840#ifdef CONFIG_MAC80211_MESH
841 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
842 mesh_plink_deactivate(sta);
843 del_timer_sync(&sta->plink_timer);
844 }
845#endif
846
847 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
848 local->total_ps_buffered--;
849 dev_kfree_skb_any(skb);
850 }
851
852 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
853 dev_kfree_skb_any(skb);
854
34e89507
JB
855 __sta_info_free(local, sta);
856
857 return 0;
4d6141c3
JS
858}
859
34e89507 860int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
4d6141c3 861{
34e89507
JB
862 struct sta_info *sta;
863 int ret;
4d6141c3 864
34e89507 865 mutex_lock(&sdata->local->sta_mtx);
2a33bee2 866 sta = sta_info_get_rx(sdata, addr);
34e89507
JB
867 ret = __sta_info_destroy(sta);
868 mutex_unlock(&sdata->local->sta_mtx);
4d6141c3
JS
869
870 return ret;
871}
872
34e89507
JB
873int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
874 const u8 *addr)
e9f207f0 875{
34e89507
JB
876 struct sta_info *sta;
877 int ret;
e9f207f0 878
34e89507 879 mutex_lock(&sdata->local->sta_mtx);
2a33bee2 880 sta = sta_info_get_bss_rx(sdata, addr);
34e89507
JB
881 ret = __sta_info_destroy(sta);
882 mutex_unlock(&sdata->local->sta_mtx);
d0709a65 883
34e89507
JB
884 return ret;
885}
e9f207f0 886
34e89507
JB
887static void sta_info_cleanup(unsigned long data)
888{
889 struct ieee80211_local *local = (struct ieee80211_local *) data;
890 struct sta_info *sta;
3393a608 891 bool timer_needed = false;
34e89507
JB
892
893 rcu_read_lock();
894 list_for_each_entry_rcu(sta, &local->sta_list, list)
3393a608
JO
895 if (sta_info_cleanup_expire_buffered(local, sta))
896 timer_needed = true;
34e89507 897 rcu_read_unlock();
e9f207f0 898
34e89507
JB
899 if (local->quiescing)
900 return;
d0709a65 901
3393a608
JO
902 if (!timer_needed)
903 return;
904
26d59535
JB
905 mod_timer(&local->sta_cleanup,
906 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
e9f207f0 907}
e9f207f0 908
f0706e82
JB
909void sta_info_init(struct ieee80211_local *local)
910{
d0709a65 911 spin_lock_init(&local->sta_lock);
34e89507 912 mutex_init(&local->sta_mtx);
f0706e82 913 INIT_LIST_HEAD(&local->sta_list);
34e89507
JB
914 INIT_LIST_HEAD(&local->sta_pending_list);
915 INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
f0706e82 916
b24b8a24
PE
917 setup_timer(&local->sta_cleanup, sta_info_cleanup,
918 (unsigned long)local);
f0706e82
JB
919}
920
921void sta_info_stop(struct ieee80211_local *local)
922{
f0706e82 923 del_timer(&local->sta_cleanup);
be8755e1 924 sta_info_flush(local, NULL);
f0706e82
JB
925}
926
f0706e82
JB
927/**
928 * sta_info_flush - flush matching STA entries from the STA table
44213b5e
JB
929 *
930 * Returns the number of removed STA entries.
931 *
f0706e82 932 * @local: local interface data
d0709a65 933 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
f0706e82 934 */
44213b5e 935int sta_info_flush(struct ieee80211_local *local,
af8cdcd8 936 struct ieee80211_sub_if_data *sdata)
f0706e82
JB
937{
938 struct sta_info *sta, *tmp;
44213b5e 939 int ret = 0;
f0706e82 940
d0709a65 941 might_sleep();
be8755e1 942
34e89507
JB
943 mutex_lock(&local->sta_mtx);
944
945 sta_info_finish_pending(local);
946
d0709a65 947 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
34e89507
JB
948 if (!sdata || sdata == sta->sdata)
949 WARN_ON(__sta_info_destroy(sta));
be8755e1 950 }
34e89507 951 mutex_unlock(&local->sta_mtx);
44213b5e
JB
952
953 return ret;
f0706e82 954}
dc6676b7 955
24723d1b
JB
956void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
957 unsigned long exp_time)
958{
959 struct ieee80211_local *local = sdata->local;
960 struct sta_info *sta, *tmp;
24723d1b 961
34e89507 962 mutex_lock(&local->sta_mtx);
24723d1b
JB
963 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
964 if (time_after(jiffies, sta->last_rx + exp_time)) {
965#ifdef CONFIG_MAC80211_IBSS_DEBUG
0c68ae26 966 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
47846c9b 967 sdata->name, sta->sta.addr);
24723d1b 968#endif
34e89507 969 WARN_ON(__sta_info_destroy(sta));
24723d1b 970 }
34e89507 971 mutex_unlock(&local->sta_mtx);
24723d1b 972}
17741cdc 973
686b9cb9
BG
974struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
975 const u8 *addr,
976 const u8 *localaddr)
17741cdc 977{
abe60632 978 struct sta_info *sta, *nxt;
17741cdc 979
686b9cb9
BG
980 /*
981 * Just return a random station if localaddr is NULL
982 * ... first in list.
983 */
f7c65594 984 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
686b9cb9
BG
985 if (localaddr &&
986 compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
987 continue;
f7c65594
JB
988 if (!sta->uploaded)
989 return NULL;
abe60632 990 return &sta->sta;
f7c65594
JB
991 }
992
abe60632 993 return NULL;
17741cdc 994}
686b9cb9 995EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
5ed176e1
JB
996
997struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
998 const u8 *addr)
999{
f7c65594 1000 struct sta_info *sta;
5ed176e1
JB
1001
1002 if (!vif)
1003 return NULL;
1004
f7c65594
JB
1005 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1006 if (!sta)
1007 return NULL;
1008
1009 if (!sta->uploaded)
1010 return NULL;
5ed176e1 1011
f7c65594 1012 return &sta->sta;
5ed176e1 1013}
17741cdc 1014EXPORT_SYMBOL(ieee80211_find_sta);
af818581 1015
50a9432d
JB
1016static void clear_sta_ps_flags(void *_sta)
1017{
1018 struct sta_info *sta = _sta;
1019
1020 clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA);
1021}
1022
af818581
JB
1023/* powersave support code */
1024void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1025{
1026 struct ieee80211_sub_if_data *sdata = sta->sdata;
1027 struct ieee80211_local *local = sdata->local;
1028 int sent, buffered;
1029
dcf55fb5 1030 clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
d057e5a3
AN
1031 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1032 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
af818581
JB
1033
1034 if (!skb_queue_empty(&sta->ps_tx_buf))
1035 sta_info_clear_tim_bit(sta);
1036
1037 /* Send all buffered frames to the station */
1038 sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
50a9432d
JB
1039 buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf,
1040 clear_sta_ps_flags, sta);
af818581
JB
1041 sent += buffered;
1042 local->total_ps_buffered -= buffered;
1043
1044#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1045 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
47846c9b 1046 "since STA not sleeping anymore\n", sdata->name,
af818581
JB
1047 sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
1048#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1049}
1050
1051void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1052{
1053 struct ieee80211_sub_if_data *sdata = sta->sdata;
1054 struct ieee80211_local *local = sdata->local;
1055 struct sk_buff *skb;
1056 int no_pending_pkts;
1057
1058 skb = skb_dequeue(&sta->tx_filtered);
1059 if (!skb) {
1060 skb = skb_dequeue(&sta->ps_tx_buf);
1061 if (skb)
1062 local->total_ps_buffered--;
1063 }
1064 no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
1065 skb_queue_empty(&sta->ps_tx_buf);
1066
1067 if (skb) {
1068 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1069 struct ieee80211_hdr *hdr =
1070 (struct ieee80211_hdr *) skb->data;
1071
1072 /*
1073 * Tell TX path to send this frame even though the STA may
1074 * still remain is PS mode after this frame exchange.
1075 */
1076 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
1077
1078#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1079 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
1080 sta->sta.addr, sta->sta.aid,
1081 skb_queue_len(&sta->ps_tx_buf));
1082#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1083
1084 /* Use MoreData flag to indicate whether there are more
1085 * buffered frames for this STA */
1086 if (no_pending_pkts)
1087 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1088 else
1089 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1090
1091 ieee80211_add_pending_skb(local, skb);
1092
1093 if (no_pending_pkts)
1094 sta_info_clear_tim_bit(sta);
1095#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1096 } else {
1097 /*
1098 * FIXME: This can be the result of a race condition between
1099 * us expiring a frame and the station polling for it.
1100 * Should we send it a null-func frame indicating we
1101 * have nothing buffered for it?
1102 */
1103 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
1104 "though there are no buffered frames for it\n",
47846c9b 1105 sdata->name, sta->sta.addr);
af818581
JB
1106#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1107 }
1108}
1109
1110void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1111 struct ieee80211_sta *pubsta, bool block)
1112{
1113 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1114
b5878a2d
JB
1115 trace_api_sta_block_awake(sta->local, pubsta, block);
1116
af818581
JB
1117 if (block)
1118 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
50a9432d 1119 else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER))
af818581
JB
1120 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1121}
1122EXPORT_SYMBOL(ieee80211_sta_block_awake);
dcf55fb5
FF
1123
1124void ieee80211_sta_set_tim(struct ieee80211_sta *pubsta)
1125{
1126 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1127
1128 set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
1129 sta_info_set_tim_bit(sta);
1130}
1131EXPORT_SYMBOL(ieee80211_sta_set_tim);