sta_info.c 61 KB
Newer Older
1 2 3
/*
 * Copyright 2002-2005, Instant802 Networks, Inc.
 * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4
 * Copyright 2013-2014  Intel Mobile Communications GmbH
5
 * Copyright (C) 2015 - 2016 Intel Deutschland GmbH
6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/init.h>
14
#include <linux/etherdevice.h>
15 16 17 18 19
#include <linux/netdevice.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
20
#include <linux/timer.h>
21
#include <linux/rtnetlink.h>
22 23 24

#include <net/mac80211.h>
#include "ieee80211_i.h"
25
#include "driver-ops.h"
Johannes Berg's avatar
Johannes Berg committed
26
#include "rate.h"
27
#include "sta_info.h"
28
#include "debugfs_sta.h"
29
#include "mesh.h"
30
#include "wme.h"
31

32 33 34 35 36 37 38
/**
 * DOC: STA information lifetime rules
 *
 * STA info structures (&struct sta_info) are managed in a hash table
 * for faster lookup and a list for iteration. They are managed using
 * RCU, i.e. access to the list and hash table is protected by RCU.
 *
39 40 41 42 43 44 45 46
 * Upon allocating a STA info structure with sta_info_alloc(), the caller
 * owns that structure. It must then insert it into the hash table using
 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
 * case (which acquires an rcu read section but must not be called from
 * within one) will the pointer still be valid after the call. Note that
 * the caller may not do much with the STA info before inserting it, in
 * particular, it may not start any mesh peer link management or add
 * encryption keys.
47 48 49
 *
 * When the insertion fails (sta_info_insert()) returns non-zero), the
 * structure will have been freed by sta_info_insert()!
50
 *
51
 * Station entries are added by mac80211 when you establish a link with a
52 53
 * peer. This means different things for the different type of interfaces
 * we support. For a regular station this mean we add the AP sta when we
Lucas De Marchi's avatar
Lucas De Marchi committed
54
 * receive an association response from the AP. For IBSS this occurs when
55
 * get to know about a peer on the same IBSS. For WDS we add the sta for
Lucas De Marchi's avatar
Lucas De Marchi committed
56
 * the peer immediately upon device open. When using AP mode we add stations
57
 * for each respective station upon request from userspace through nl80211.
58
 *
59 60
 * In order to remove a STA info structure, various sta_info_destroy_*()
 * calls are available.
61
 *
62 63 64 65
 * There is no concept of ownership on a STA entry, each structure is
 * owned by the global hash table/list until it is removed. All users of
 * the structure need to be RCU protected so that the structure won't be
 * freed before they are done using it.
66
 */
67

68 69
static const struct rhashtable_params sta_rht_params = {
	.nelem_hint = 3, /* start small */
70
	.insecure_elasticity = true, /* Disable chain-length checks. */
71
	.automatic_shrinking = true,
72
	.head_offset = offsetof(struct sta_info, hash_node),
73
	.key_offset = offsetof(struct sta_info, addr),
74 75
	.key_len = ETH_ALEN,
	.hashfn = sta_addr_hash,
76
	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
77 78
};

79
/* Caller must hold local->sta_mtx */
80 81
static int sta_info_hash_del(struct ieee80211_local *local,
			     struct sta_info *sta)
82
{
83 84
	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
				      sta_rht_params);
85 86
}

87
static void __cleanup_single_sta(struct sta_info *sta)
88 89 90 91 92
{
	int ac, i;
	struct tid_ampdu_tx *tid_tx;
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	struct ieee80211_local *local = sdata->local;
93
	struct ps_data *ps;
94

95
	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
96 97
	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
98 99 100
		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
			ps = &sdata->bss->ps;
101 102
		else if (ieee80211_vif_is_mesh(&sdata->vif))
			ps = &sdata->u.mesh.ps;
103 104
		else
			return;
105 106

		clear_sta_flag(sta, WLAN_STA_PS_STA);
107
		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
108
		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
109

110
		atomic_dec(&ps->num_sta_ps);
111 112
	}

113 114 115 116 117 118 119
	if (sta->sta.txq[0]) {
		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
			int n = skb_queue_len(&txqi->queue);

			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
120
			txqi->byte_cnt = 0;
121 122 123
		}
	}

124 125
	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
126 127
		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
128 129
	}

130 131
	if (ieee80211_vif_is_mesh(&sdata->vif))
		mesh_sta_cleanup(sta);
132

133
	cancel_work_sync(&sta->drv_deliver_wk);
134 135 136 137 138 139 140

	/*
	 * Destroy aggregation state here. It would be nice to wait for the
	 * driver to finish aggregation stop and then clean up, but for now
	 * drivers have to handle aggregation stop being requested, followed
	 * directly by station destruction.
	 */
141
	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
142
		kfree(sta->ampdu_mlme.tid_start_tx[i]);
143 144 145
		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
		if (!tid_tx)
			continue;
146
		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
147 148
		kfree(tid_tx);
	}
149
}
150

151 152 153 154 155 156
static void cleanup_single_sta(struct sta_info *sta)
{
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	struct ieee80211_local *local = sdata->local;

	__cleanup_single_sta(sta);
157 158 159
	sta_info_free(local, sta);
}

160
/* protected by RCU */
161 162
struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
			      const u8 *addr)
163
{
164
	struct ieee80211_local *local = sdata->local;
165 166 167
	struct sta_info *sta;
	struct rhash_head *tmp;
	const struct bucket_table *tbl;
168

169 170
	rcu_read_lock();
	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
171

172 173 174 175 176 177 178 179 180 181 182
	for_each_sta_info(local, tbl, addr, sta, tmp) {
		if (sta->sdata == sdata) {
			rcu_read_unlock();
			/* this is safe as the caller must already hold
			 * another rcu read section or the mutex
			 */
			return sta;
		}
	}
	rcu_read_unlock();
	return NULL;
183 184
}

185 186 187 188 189 190 191 192 193
/*
 * Get sta info either from the specified interface
 * or from one of its vlans
 */
struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
				  const u8 *addr)
{
	struct ieee80211_local *local = sdata->local;
	struct sta_info *sta;
194 195
	struct rhash_head *tmp;
	const struct bucket_table *tbl;
196

197 198 199 200 201 202 203 204 205 206 207 208
	rcu_read_lock();
	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);

	for_each_sta_info(local, tbl, addr, sta, tmp) {
		if (sta->sdata == sdata ||
		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
			rcu_read_unlock();
			/* this is safe as the caller must already hold
			 * another rcu read section or the mutex
			 */
			return sta;
		}
209
	}
210 211
	rcu_read_unlock();
	return NULL;
212 213
}

214 215
struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
				     int idx)
216
{
217
	struct ieee80211_local *local = sdata->local;
218 219 220
	struct sta_info *sta;
	int i = 0;

221
	list_for_each_entry_rcu(sta, &local->sta_list, list) {
222
		if (sdata != sta->sdata)
223
			continue;
224 225 226 227
		if (i < idx) {
			++i;
			continue;
		}
228
		return sta;
229 230 231 232
	}

	return NULL;
}
233

234
/**
235
 * sta_info_free - free STA
236
 *
237
 * @local: pointer to the global information
238 239 240
 * @sta: STA info to free
 *
 * This function must undo everything done by sta_info_alloc()
241 242 243
 * that may happen before sta_info_insert(). It may only be
 * called when sta_info_insert() has not been attempted (and
 * if that fails, the station is freed anyway.)
244
 */
245
void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
246
{
247
	if (sta->rate_ctrl)
248
		rate_control_free_sta(sta);
249

Johannes Berg's avatar
Johannes Berg committed
250
	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
251

252 253
	if (sta->sta.txq[0])
		kfree(to_txq_info(sta->sta.txq[0]));
254
	kfree(rcu_dereference_raw(sta->sta.rates));
255 256 257
#ifdef CONFIG_MAC80211_MESH
	kfree(sta->mesh);
#endif
258
	free_percpu(sta->pcpu_rx_stats);
259 260 261
	kfree(sta);
}

262
/* Caller must hold local->sta_mtx */
263 264
static int sta_info_hash_add(struct ieee80211_local *local,
			     struct sta_info *sta)
265
{
266 267
	return rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
				      sta_rht_params);
268 269
}

270
static void sta_deliver_ps_frames(struct work_struct *wk)
271 272 273
{
	struct sta_info *sta;

274
	sta = container_of(wk, struct sta_info, drv_deliver_wk);
275 276 277 278

	if (sta->dead)
		return;

279 280
	local_bh_disable();
	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
281
		ieee80211_sta_ps_deliver_wakeup(sta);
282
	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
283
		ieee80211_sta_ps_deliver_poll_response(sta);
284
	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg's avatar
Johannes Berg committed
285
		ieee80211_sta_ps_deliver_uapsd(sta);
286
	local_bh_enable();
287 288
}

289 290 291
static int sta_prepare_rate_control(struct ieee80211_local *local,
				    struct sta_info *sta, gfp_t gfp)
{
292
	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
293 294
		return 0;

295
	sta->rate_ctrl = local->rate_ctrl;
296
	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg's avatar
Johannes Berg committed
297
						     sta, gfp);
298
	if (!sta->rate_ctrl_priv)
299 300 301 302 303
		return -ENOMEM;

	return 0;
}

Johannes Berg's avatar
Johannes Berg committed
304
struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
305
				const u8 *addr, gfp_t gfp)
306
{
307
	struct ieee80211_local *local = sdata->local;
308
	struct ieee80211_hw *hw = &local->hw;
309
	struct sta_info *sta;
310
	int i;
311

312
	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
313
	if (!sta)
Johannes Berg's avatar
Johannes Berg committed
314
		return NULL;
315

316 317 318 319 320 321 322
	if (ieee80211_hw_check(hw, USES_RSS)) {
		sta->pcpu_rx_stats =
			alloc_percpu(struct ieee80211_sta_rx_stats);
		if (!sta->pcpu_rx_stats)
			goto free;
	}

323
	spin_lock_init(&sta->lock);
324
	spin_lock_init(&sta->ps_lock);
325
	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
326
	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
327
	mutex_init(&sta->ampdu_mlme.mtx);
328
#ifdef CONFIG_MAC80211_MESH
329 330 331 332 333 334 335 336 337 338
	if (ieee80211_vif_is_mesh(&sdata->vif)) {
		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
		if (!sta->mesh)
			goto free;
		spin_lock_init(&sta->mesh->plink_lock);
		if (ieee80211_vif_is_mesh(&sdata->vif) &&
		    !sdata->u.mesh.user_mpm)
			init_timer(&sta->mesh->plink_timer);
		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
	}
339
#endif
340

341
	memcpy(sta->addr, addr, ETH_ALEN);
342
	memcpy(sta->sta.addr, addr, ETH_ALEN);
343 344
	sta->local = local;
	sta->sdata = sdata;
345
	sta->rx_stats.last_rx = jiffies;
346

347 348
	u64_stats_init(&sta->rx_stats.syncp);

349 350
	sta->sta_state = IEEE80211_STA_NONE;

351 352 353
	/* Mark TID as unreserved */
	sta->reserved_tid = IEEE80211_TID_UNRESERVED;

354
	sta->last_connected = ktime_get_seconds();
355 356 357
	ewma_signal_init(&sta->rx_stats_avg.signal);
	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
358

359 360 361 362 363 364 365 366 367 368 369 370 371 372
	if (local->ops->wake_tx_queue) {
		void *txq_data;
		int size = sizeof(struct txq_info) +
			   ALIGN(hw->txq_data_size, sizeof(void *));

		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
		if (!txq_data)
			goto free;

		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
			struct txq_info *txq = txq_data + i * size;

			ieee80211_init_tx_queue(sdata, sta, txq, i);
		}
373
	}
374

375 376 377
	if (sta_prepare_rate_control(local, sta, gfp))
		goto free_txq;

378
	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
379 380 381 382 383
		/*
		 * timer_to_tid must be initialized with identity mapping
		 * to enable session_timer's data differentiation. See
		 * sta_rx_agg_session_timer_expired for usage.
		 */
384 385
		sta->timer_to_tid[i] = i;
	}
386 387 388 389
	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
		skb_queue_head_init(&sta->ps_tx_buf[i]);
		skb_queue_head_init(&sta->tx_filtered[i]);
	}
Johannes Berg's avatar
Johannes Berg committed
390

391
	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
392
		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
393

394
	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
395 396 397
	if (sdata->vif.type == NL80211_IFTYPE_AP ||
	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
		struct ieee80211_supported_band *sband =
398
			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
				IEEE80211_HT_CAP_SM_PS_SHIFT;
		/*
		 * Assume that hostapd advertises our caps in the beacon and
		 * this is the known_smps_mode for a station that just assciated
		 */
		switch (smps) {
		case WLAN_HT_SMPS_CONTROL_DISABLED:
			sta->known_smps_mode = IEEE80211_SMPS_OFF;
			break;
		case WLAN_HT_SMPS_CONTROL_STATIC:
			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
			break;
		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
			break;
		default:
			WARN_ON(1);
		}
	}
419

420 421
	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;

Johannes Berg's avatar
Johannes Berg committed
422
	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
423

424
	return sta;
425 426 427 428 429

free_txq:
	if (sta->sta.txq[0])
		kfree(to_txq_info(sta->sta.txq[0]));
free:
430 431 432
#ifdef CONFIG_MAC80211_MESH
	kfree(sta->mesh);
#endif
433 434
	kfree(sta);
	return NULL;
Johannes Berg's avatar
Johannes Berg committed
435 436
}

437
static int sta_info_insert_check(struct sta_info *sta)
438 439 440
{
	struct ieee80211_sub_if_data *sdata = sta->sdata;

441 442 443 444 445
	/*
	 * Can't be a WARN_ON because it can be triggered through a race:
	 * something inserts a STA (on one CPU) without holding the RTNL
	 * and another CPU turns off the net device.
	 */
446 447
	if (unlikely(!ieee80211_sdata_running(sdata)))
		return -ENETDOWN;
448

449
	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
450 451 452
		    is_multicast_ether_addr(sta->sta.addr)))
		return -EINVAL;

453 454 455 456 457 458 459 460 461 462 463 464 465
	/* Strictly speaking this isn't necessary as we hold the mutex, but
	 * the rhashtable code can't really deal with that distinction. We
	 * do require the mutex for correctness though.
	 */
	rcu_read_lock();
	lockdep_assert_held(&sdata->local->sta_mtx);
	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
		rcu_read_unlock();
		return -ENOTUNIQ;
	}
	rcu_read_unlock();

466 467 468
	return 0;
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482
static int sta_info_insert_drv_state(struct ieee80211_local *local,
				     struct ieee80211_sub_if_data *sdata,
				     struct sta_info *sta)
{
	enum ieee80211_sta_state state;
	int err = 0;

	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
		err = drv_sta_state(local, sdata, sta, state, state + 1);
		if (err)
			break;
	}

	if (!err) {
483 484 485 486 487 488
		/*
		 * Drivers using legacy sta_add/sta_remove callbacks only
		 * get uploaded set to true after sta_add is called.
		 */
		if (!local->ops->sta_add)
			sta->uploaded = true;
489 490 491 492
		return 0;
	}

	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg's avatar
Johannes Berg committed
493 494 495
		sdata_info(sdata,
			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
			   sta->sta.addr, state + 1, err);
496 497 498 499 500 501 502 503 504 505
		err = 0;
	}

	/* unwind on error */
	for (; state > IEEE80211_STA_NOTEXIST; state--)
		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));

	return err;
}

506 507 508 509 510
/*
 * should be called with sta_mtx locked
 * this function replaces the mutex lock
 * with a RCU lock
 */
511
static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
512 513 514
{
	struct ieee80211_local *local = sta->local;
	struct ieee80211_sub_if_data *sdata = sta->sdata;
515
	struct station_info *sinfo;
516 517 518
	int err = 0;

	lockdep_assert_held(&local->sta_mtx);
519

520 521 522 523 524 525
	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
	if (!sinfo) {
		err = -ENOMEM;
		goto out_err;
	}

526 527 528 529
	/* check if STA exists already */
	if (sta_info_get_bss(sdata, sta->sta.addr)) {
		err = -EEXIST;
		goto out_err;
530
	}
531

532 533 534
	local->num_sta++;
	local->sta_generation++;
	smp_mb();
535

536 537 538
	/* simplify things and don't accept BA sessions yet */
	set_sta_flag(sta, WLAN_STA_BLOCK_BA);

539
	/* make the station visible */
540 541 542
	err = sta_info_hash_add(local, sta);
	if (err)
		goto out_drop_sta;
543

544
	list_add_tail_rcu(&sta->list, &local->sta_list);
545

546 547 548 549 550
	/* notify driver */
	err = sta_info_insert_drv_state(local, sdata, sta);
	if (err)
		goto out_remove;

551
	set_sta_flag(sta, WLAN_STA_INSERTED);
552 553
	/* accept BA sessions now */
	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
554

555 556
	ieee80211_sta_debugfs_add(sta);
	rate_control_add_sta_debugfs(sta);
557

558 559 560
	sinfo->generation = local->sta_generation;
	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
	kfree(sinfo);
561

Johannes Berg's avatar
Johannes Berg committed
562
	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
563

564 565 566
	/* move reference to rcu-protected */
	rcu_read_lock();
	mutex_unlock(&local->sta_mtx);
567

Johannes Berg's avatar
Johannes Berg committed
568 569 570
	if (ieee80211_vif_is_mesh(&sdata->vif))
		mesh_accept_plinks_update(sdata);

571
	return 0;
572 573 574
 out_remove:
	sta_info_hash_del(local, sta);
	list_del_rcu(&sta->list);
575
 out_drop_sta:
576 577 578
	local->num_sta--;
	synchronize_net();
	__cleanup_single_sta(sta);
579 580
 out_err:
	mutex_unlock(&local->sta_mtx);
Sudip Mukherjee's avatar
Sudip Mukherjee committed
581
	kfree(sinfo);
582 583
	rcu_read_lock();
	return err;
584 585 586 587 588
}

int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
{
	struct ieee80211_local *local = sta->local;
589
	int err;
590

591 592
	might_sleep();

593 594
	mutex_lock(&local->sta_mtx);

595 596
	err = sta_info_insert_check(sta);
	if (err) {
597
		mutex_unlock(&local->sta_mtx);
598 599 600 601
		rcu_read_lock();
		goto out_free;
	}

602
	err = sta_info_insert_finish(sta);
603 604 605
	if (err)
		goto out_free;

Johannes Berg's avatar
Johannes Berg committed
606
	return 0;
607
 out_free:
608
	sta_info_free(local, sta);
609
	return err;
610 611
}

612 613 614 615 616 617 618 619 620
int sta_info_insert(struct sta_info *sta)
{
	int err = sta_info_insert_rcu(sta);

	rcu_read_unlock();

	return err;
}

621
static inline void __bss_tim_set(u8 *tim, u16 id)
622 623 624 625 626
{
	/*
	 * This format has been mandated by the IEEE specifications,
	 * so this line may not be changed to use the __set_bit() format.
	 */
627
	tim[id / 8] |= (1 << (id % 8));
628 629
}

630
static inline void __bss_tim_clear(u8 *tim, u16 id)
631 632 633 634 635
{
	/*
	 * This format has been mandated by the IEEE specifications,
	 * so this line may not be changed to use the __clear_bit() format.
	 */
636
	tim[id / 8] &= ~(1 << (id % 8));
637 638
}

639 640 641 642 643 644 645 646 647
static inline bool __bss_tim_get(u8 *tim, u16 id)
{
	/*
	 * This format has been mandated by the IEEE specifications,
	 * so this line may not be changed to use the test_bit() format.
	 */
	return tim[id / 8] & (1 << (id % 8));
}

648
static unsigned long ieee80211_tids_for_ac(int ac)
649
{
650 651 652 653 654 655 656 657 658 659 660 661 662
	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
	switch (ac) {
	case IEEE80211_AC_VO:
		return BIT(6) | BIT(7);
	case IEEE80211_AC_VI:
		return BIT(4) | BIT(5);
	case IEEE80211_AC_BE:
		return BIT(0) | BIT(3);
	case IEEE80211_AC_BK:
		return BIT(1) | BIT(2);
	default:
		WARN_ON(1);
		return 0;
663
	}
664 665
}

666
static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
667
{
668
	struct ieee80211_local *local = sta->local;
669
	struct ps_data *ps;
670 671 672
	bool indicate_tim = false;
	u8 ignore_for_tim = sta->sta.uapsd_queues;
	int ac;
673
	u16 id = sta->sta.aid;
674 675 676 677 678

	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
		if (WARN_ON_ONCE(!sta->sdata->bss))
			return;
679

680
		ps = &sta->sdata->bss->ps;
681 682 683 684
#ifdef CONFIG_MAC80211_MESH
	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
		ps = &sta->sdata->u.mesh.ps;
#endif
685
	} else {
686
		return;
687
	}
688

689
	/* No need to do anything if the driver does all */
690
	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
691
		return;
692

693 694
	if (sta->dead)
		goto done;
695

696 697 698 699 700 701 702 703 704
	/*
	 * If all ACs are delivery-enabled then we should build
	 * the TIM bit for all ACs anyway; if only some are then
	 * we ignore those and build the TIM bit using only the
	 * non-enabled ones.
	 */
	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
		ignore_for_tim = 0;

705 706 707
	if (ignore_pending)
		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;

708 709
	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
		unsigned long tids;
710

711 712 713 714 715 716 717
		if (ignore_for_tim & BIT(ac))
			continue;

		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
				!skb_queue_empty(&sta->ps_tx_buf[ac]);
		if (indicate_tim)
			break;
718

719 720 721 722
		tids = ieee80211_tids_for_ac(ac);

		indicate_tim |=
			sta->driver_buffered_tids & tids;
723 724
		indicate_tim |=
			sta->txq_buffered_tids & tids;
725
	}
726

727
 done:
728
	spin_lock_bh(&local->tim_lock);
729

730 731 732
	if (indicate_tim == __bss_tim_get(ps->tim, id))
		goto out_unlock;

733
	if (indicate_tim)
734
		__bss_tim_set(ps->tim, id);
735
	else
736
		__bss_tim_clear(ps->tim, id);
737

738
	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
739
		local->tim_in_locked_section = true;
740
		drv_set_tim(local, &sta->sta, indicate_tim);
741 742
		local->tim_in_locked_section = false;
	}
743

744
out_unlock:
745
	spin_unlock_bh(&local->tim_lock);
746 747
}

748 749 750 751 752
void sta_info_recalc_tim(struct sta_info *sta)
{
	__sta_info_recalc_tim(sta, false);
}

753
static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
754
{
755
	struct ieee80211_tx_info *info;
756 757 758
	int timeout;

	if (!skb)
759
		return false;
760

761
	info = IEEE80211_SKB_CB(skb);
762 763

	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
764 765 766
	timeout = (sta->listen_interval *
		   sta->sdata->vif.bss_conf.beacon_int *
		   32 / 15625) * HZ;
767 768
	if (timeout < STA_TX_BUFFER_EXPIRE)
		timeout = STA_TX_BUFFER_EXPIRE;
769
	return time_after(jiffies, info->control.jiffies + timeout);
770 771 772
}


773 774
static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
						struct sta_info *sta, int ac)
775 776 777 778
{
	unsigned long flags;
	struct sk_buff *skb;

779 780 781 782 783 784 785 786
	/*
	 * First check for frames that should expire on the filtered
	 * queue. Frames here were rejected by the driver and are on
	 * a separate queue to avoid reordering with normal PS-buffered
	 * frames. They also aren't accounted for right now in the
	 * total_ps_buffered counter.
	 */
	for (;;) {
787 788
		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
		skb = skb_peek(&sta->tx_filtered[ac]);
789
		if (sta_info_buffer_expired(sta, skb))
790
			skb = __skb_dequeue(&sta->tx_filtered[ac]);
791 792
		else
			skb = NULL;
793
		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
794 795 796 797 798 799 800 801 802

		/*
		 * Frames are queued in order, so if this one
		 * hasn't expired yet we can stop testing. If
		 * we actually reached the end of the queue we
		 * also need to stop, of course.
		 */
		if (!skb)
			break;
803
		ieee80211_free_txskb(&local->hw, skb);
804 805 806 807 808 809 810 811
	}

	/*
	 * Now also check the normal PS-buffered queue, this will
	 * only find something if the filtered queue was emptied
	 * since the filtered frames are all before the normal PS
	 * buffered frames.
	 */
812
	for (;;) {
813 814
		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
		skb = skb_peek(&sta->ps_tx_buf[ac]);
815
		if (sta_info_buffer_expired(sta, skb))
816
			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
817
		else
818
			skb = NULL;
819
		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
820

821 822 823 824 825
		/*
		 * frames are queued in order, so if this one
		 * hasn't expired yet (or we reached the end of
		 * the queue) we can stop testing
		 */
826
		if (!skb)
827
			break;
828 829

		local->total_ps_buffered--;
Johannes Berg's avatar
Johannes Berg committed
830 831
		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
		       sta->sta.addr);
832
		ieee80211_free_txskb(&local->hw, skb);
833
	}
834

835 836 837 838 839 840 841 842 843 844 845 846
	/*
	 * Finally, recalculate the TIM bit for this station -- it might
	 * now be clear because the station was too slow to retrieve its
	 * frames.
	 */
	sta_info_recalc_tim(sta);

	/*
	 * Return whether there are any frames still buffered, this is
	 * used to check whether the cleanup timer still needs to run,
	 * if there are no frames we don't need to rearm the timer.
	 */
847 848 849 850 851 852 853 854 855 856
	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
		 skb_queue_empty(&sta->tx_filtered[ac]));
}

static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
					     struct sta_info *sta)
{
	bool have_buffered = false;
	int ac;

857 858 859
	/* This is only necessary for stations on BSS/MBSS interfaces */
	if (!sta->sdata->bss &&
	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
860 861 862 863 864 865 866
		return false;

	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
		have_buffered |=
			sta_info_cleanup_expire_buffered_ac(local, sta, ac);

	return have_buffered;
867 868
}

869
static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
870
{
871 872
	struct ieee80211_local *local;
	struct ieee80211_sub_if_data *sdata;
873
	int ret;
874

875
	might_sleep();
876

877 878
	if (!sta)
		return -ENOENT;
879

880 881
	local = sta->local;
	sdata = sta->sdata;
882

883 884
	lockdep_assert_held(&local->sta_mtx);

885 886 887 888 889 890
	/*
	 * Before removing the station from the driver and
	 * rate control, it might still start new aggregation
	 * sessions -- block that to make sure the tear-down
	 * will be sufficient.
	 */
891
	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
892
	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
893

894 895 896 897 898 899 900
	/*
	 * Before removing the station from the driver there might be pending
	 * rx frames on RSS queues sent prior to the disassociation - wait for
	 * all such frames to be processed.
	 */
	drv_sync_rx_queues(local, sta);

901
	ret = sta_info_hash_del(local, sta);
902
	if (WARN_ON(ret))
903 904
		return ret;

905 906 907 908 909 910 911 912 913
	/*
	 * for TDLS peers, make sure to return to the base channel before
	 * removal.
	 */
	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
	}

914
	list_del_rcu(&sta->list);
915
	sta->removed = true;
916

917 918
	drv_sta_pre_rcu_remove(local, sta->sdata, sta);

919 920 921 922
	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);

923 924 925 926 927 928 929
	return 0;
}

static void __sta_info_destroy_part2(struct sta_info *sta)
{
	struct ieee80211_local *local = sta->local;
	struct ieee80211_sub_if_data *sdata = sta->sdata;
930
	struct station_info *sinfo;
931 932 933 934 935 936 937 938 939 940
	int ret;

	/*
	 * NOTE: This assumes at least synchronize_net() was done
	 *	 after _part1 and before _part2!
	 */

	might_sleep();
	lockdep_assert_held(&local->sta_mtx);

941
	/* now keys can no longer be reached */
942
	ieee80211_free_sta_keys(local, sta);
943

944 945 946
	/* disable TIM bit - last chance to tell driver */
	__sta_info_recalc_tim(sta, true);

947 948 949 950 951
	sta->dead = true;

	local->num_sta--;
	local->sta_generation++;

952
	while (sta->sta_state > IEEE80211_STA_NONE) {
953 954
		ret = sta_info_move_state(sta, sta->sta_state - 1);
		if (ret) {
955 956 957 958
			WARN_ON_ONCE(1);
			break;
		}
	}
959

960 961 962 963 964
	if (sta->uploaded) {
		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
				    IEEE80211_STA_NOTEXIST);
		WARN_ON_ONCE(ret != 0);
	}
965

Johannes Berg's avatar
Johannes Berg committed
966 967
	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);

968 969 970 971 972
	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
	if (sinfo)
		sta_set_sinfo(sta, sinfo);
	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
	kfree(sinfo);
973

974 975 976
	rate_control_remove_sta_debugfs(sta);
	ieee80211_sta_debugfs_remove(sta);

977
	cleanup_single_sta(sta);
978 979 980 981 982 983 984 985 986 987 988 989
}

int __must_check __sta_info_destroy(struct sta_info *sta)
{
	int err = __sta_info_destroy_part1(sta);

	if (err)
		return err;

	synchronize_net();

	__sta_info_destroy_part2(sta);
990 991

	return 0;
992 993
}

994
int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
995
{
996 997
	struct sta_info *sta;
	int ret;
998

999
	mutex_lock(&sdata->local->sta_mtx);
1000
	sta = sta_info_get(sdata, addr);
1001 1002
	ret = __sta_info_destroy(sta);
	mutex_unlock(&sdata->local->sta_mtx);
1003 1004 1005 1006

	return ret;
}

1007 1008
int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
			      const u8 *addr)
1009
{
1010 1011
	struct sta_info *sta;
	int ret;
1012

1013
	mutex_lock(&sdata->local->sta_mtx);
1014
	sta = sta_info_get_bss(sdata, addr);
1015 1016
	ret = __sta_info_destroy(sta);
	mutex_unlock(&sdata->local->sta_mtx);
1017

1018 1019
	return ret;
}
1020

1021 1022 1023 1024
static void sta_info_cleanup(unsigned long data)
{
	struct ieee80211_local *local = (struct ieee80211_local *) data;
	struct sta_info *sta;
1025
	bool timer_needed = false;
1026 1027 1028

	rcu_read_lock();
	list_for_each_entry_rcu(sta, &local->sta_list, list)
1029 1030
		if (sta_info_cleanup_expire_buffered(local, sta))
			timer_needed = true;
1031
	rcu_read_unlock();
1032

1033 1034
	if (local->quiescing)
		return;
1035

1036 1037 1038
	if (!timer_needed)
		return;

1039 1040
	mod_timer(&local->sta_cleanup,
		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1041 1042
}

1043
u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1044
{
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	return jhash(key, ETH_ALEN, seed);
}

int sta_info_init(struct ieee80211_local *local)
{
	int err;

	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
	if (err)
		return err;

1056
	spin_lock_init(&local->tim_lock);
Johannes Berg's avatar