cfg.c 92.6 KB
Newer Older
1 2 3
/*
 * mac80211 configuration hooks for cfg80211
 *
4
 * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
5 6 7 8
 *
 * This file is GPLv2 as found in COPYING.
 */

9
#include <linux/ieee80211.h>
10 11
#include <linux/nl80211.h>
#include <linux/rtnetlink.h>
12
#include <linux/slab.h>
13
#include <net/net_namespace.h>
14
#include <linux/rcupdate.h>
15
#include <linux/if_ether.h>
16 17
#include <net/cfg80211.h>
#include "ieee80211_i.h"
18
#include "driver-ops.h"
19
#include "cfg.h"
Johannes Berg's avatar
Johannes Berg committed
20
#include "rate.h"
21 22
#include "mesh.h"

23 24
static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy,
						const char *name,
25 26 27
						enum nl80211_iftype type,
						u32 *flags,
						struct vif_params *params)
28 29
{
	struct ieee80211_local *local = wiphy_priv(wiphy);
30
	struct wireless_dev *wdev;
31 32
	struct ieee80211_sub_if_data *sdata;
	int err;
33

34
	err = ieee80211_if_add(local, name, &wdev, type, params);
35 36
	if (err)
		return ERR_PTR(err);
37

38
	if (type == NL80211_IFTYPE_MONITOR && flags) {
39
		sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
40 41 42
		sdata->u.mntr_flags = *flags;
	}

43
	return wdev;
44 45
}

46
static int ieee80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
47
{
48
	ieee80211_if_remove(IEEE80211_WDEV_TO_SUB_IF(wdev));
49

50
	return 0;
51 52
}

53 54
static int ieee80211_change_iface(struct wiphy *wiphy,
				  struct net_device *dev,
55 56
				  enum nl80211_iftype type, u32 *flags,
				  struct vif_params *params)
57
{
58
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
59
	int ret;
60

61
	ret = ieee80211_if_change_type(sdata, type);
62 63
	if (ret)
		return ret;
64

65 66
	if (type == NL80211_IFTYPE_AP_VLAN &&
	    params && params->use_4addr == 0)
67
		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
68 69 70 71
	else if (type == NL80211_IFTYPE_STATION &&
		 params && params->use_4addr >= 0)
		sdata->u.mgd.use_4addr = params->use_4addr;

72 73 74 75
	if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
		struct ieee80211_local *local = sdata->local;

		if (ieee80211_sdata_running(sdata)) {
76 77 78
			u32 mask = MONITOR_FLAG_COOK_FRAMES |
				   MONITOR_FLAG_ACTIVE;

79
			/*
80 81 82
			 * Prohibit MONITOR_FLAG_COOK_FRAMES and
			 * MONITOR_FLAG_ACTIVE to be changed while the
			 * interface is up.
83 84 85 86 87
			 * Else we would need to add a lot of cruft
			 * to update everything:
			 *	cooked_mntrs, monitor and all fif_* counters
			 *	reconfigure hardware
			 */
88
			if ((*flags & mask) != (sdata->u.mntr_flags & mask))
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
				return -EBUSY;

			ieee80211_adjust_monitor_flags(sdata, -1);
			sdata->u.mntr_flags = *flags;
			ieee80211_adjust_monitor_flags(sdata, 1);

			ieee80211_configure_filter(local);
		} else {
			/*
			 * Because the interface is down, ieee80211_do_stop
			 * and ieee80211_do_open take care of "everything"
			 * mentioned in the comment above.
			 */
			sdata->u.mntr_flags = *flags;
		}
	}
105

106 107 108
	return 0;
}

109 110 111 112 113 114 115 116 117 118 119 120
static int ieee80211_start_p2p_device(struct wiphy *wiphy,
				      struct wireless_dev *wdev)
{
	return ieee80211_do_open(wdev, true);
}

static void ieee80211_stop_p2p_device(struct wiphy *wiphy,
				      struct wireless_dev *wdev)
{
	ieee80211_sdata_stop(IEEE80211_WDEV_TO_SUB_IF(wdev));
}

121 122 123 124 125 126 127 128 129 130
static int ieee80211_set_noack_map(struct wiphy *wiphy,
				  struct net_device *dev,
				  u16 noack_map)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);

	sdata->noack_map = noack_map;
	return 0;
}

131
static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
132
			     u8 key_idx, bool pairwise, const u8 *mac_addr,
133 134
			     struct key_params *params)
{
135
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
136
	struct sta_info *sta = NULL;
137
	struct ieee80211_key *key;
138
	int err;
139

140
	if (!ieee80211_sdata_running(sdata))
Johannes Berg's avatar
Johannes Berg committed
141 142
		return -ENETDOWN;

143
	/* reject WEP and TKIP keys if WEP failed to initialize */
144 145 146
	switch (params->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_TKIP:
147 148 149
	case WLAN_CIPHER_SUITE_WEP104:
		if (IS_ERR(sdata->local->wep_tx_tfm))
			return -EINVAL;
150
		break;
151
	default:
152
		break;
153 154
	}

155 156
	key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
				  params->key, params->seq_len, params->seq);
157 158
	if (IS_ERR(key))
		return PTR_ERR(key);
159

160 161 162
	if (pairwise)
		key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;

Johannes Berg's avatar
Johannes Berg committed
163
	mutex_lock(&sdata->local->sta_mtx);
164

165
	if (mac_addr) {
166 167 168 169
		if (ieee80211_vif_is_mesh(&sdata->vif))
			sta = sta_info_get(sdata, mac_addr);
		else
			sta = sta_info_get_bss(sdata, mac_addr);
Johannes Berg's avatar
Johannes Berg committed
170 171 172 173 174 175 176 177 178 179 180
		/*
		 * The ASSOC test makes sure the driver is ready to
		 * receive the key. When wpa_supplicant has roamed
		 * using FT, it attempts to set the key before
		 * association has completed, this rejects that attempt
		 * so it will set the key again after assocation.
		 *
		 * TODO: accept the key if we have a station entry and
		 *       add it to the device after the station.
		 */
		if (!sta || !test_sta_flag(sta, WLAN_STA_ASSOC)) {
181
			ieee80211_key_free_unused(key);
182 183
			err = -ENOENT;
			goto out_unlock;
184
		}
185 186
	}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	switch (sdata->vif.type) {
	case NL80211_IFTYPE_STATION:
		if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
		break;
	case NL80211_IFTYPE_AP:
	case NL80211_IFTYPE_AP_VLAN:
		/* Keys without a station are used for TX only */
		if (key->sta && test_sta_flag(key->sta, WLAN_STA_MFP))
			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
		break;
	case NL80211_IFTYPE_ADHOC:
		/* no MFP (yet) */
		break;
	case NL80211_IFTYPE_MESH_POINT:
#ifdef CONFIG_MAC80211_MESH
		if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
			key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
		break;
#endif
	case NL80211_IFTYPE_WDS:
	case NL80211_IFTYPE_MONITOR:
	case NL80211_IFTYPE_P2P_DEVICE:
	case NL80211_IFTYPE_UNSPECIFIED:
	case NUM_NL80211_IFTYPES:
	case NL80211_IFTYPE_P2P_CLIENT:
	case NL80211_IFTYPE_P2P_GO:
		/* shouldn't happen */
		WARN_ON_ONCE(1);
		break;
	}

219
	err = ieee80211_key_link(key, sdata, sta);
220

221
 out_unlock:
Johannes Berg's avatar
Johannes Berg committed
222
	mutex_unlock(&sdata->local->sta_mtx);
223 224

	return err;
225 226 227
}

static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
228
			     u8 key_idx, bool pairwise, const u8 *mac_addr)
229
{
230 231
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct ieee80211_local *local = sdata->local;
232
	struct sta_info *sta;
233
	struct ieee80211_key *key = NULL;
234 235
	int ret;

236 237
	mutex_lock(&local->sta_mtx);
	mutex_lock(&local->key_mtx);
238

239
	if (mac_addr) {
240 241
		ret = -ENOENT;

242
		sta = sta_info_get_bss(sdata, mac_addr);
243
		if (!sta)
244
			goto out_unlock;
245

246
		if (pairwise)
247
			key = key_mtx_dereference(local, sta->ptk);
248
		else
249
			key = key_mtx_dereference(local, sta->gtk[key_idx]);
250
	} else
251
		key = key_mtx_dereference(local, sdata->keys[key_idx]);
252

253
	if (!key) {
254 255 256
		ret = -ENOENT;
		goto out_unlock;
	}
257

258
	ieee80211_key_free(key, true);
259

260 261
	ret = 0;
 out_unlock:
262 263
	mutex_unlock(&local->key_mtx);
	mutex_unlock(&local->sta_mtx);
264 265

	return ret;
266 267
}

268
static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
269 270
			     u8 key_idx, bool pairwise, const u8 *mac_addr,
			     void *cookie,
271 272 273
			     void (*callback)(void *cookie,
					      struct key_params *params))
{
274
	struct ieee80211_sub_if_data *sdata;
275 276 277
	struct sta_info *sta = NULL;
	u8 seq[6] = {0};
	struct key_params params;
278
	struct ieee80211_key *key = NULL;
Johannes Berg's avatar
Johannes Berg committed
279
	u64 pn64;
280 281 282 283
	u32 iv32;
	u16 iv16;
	int err = -ENOENT;

284 285
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

286 287
	rcu_read_lock();

288
	if (mac_addr) {
289
		sta = sta_info_get_bss(sdata, mac_addr);
290 291 292
		if (!sta)
			goto out;

293
		if (pairwise)
Johannes Berg's avatar
Johannes Berg committed
294
			key = rcu_dereference(sta->ptk);
295
		else if (key_idx < NUM_DEFAULT_KEYS)
Johannes Berg's avatar
Johannes Berg committed
296
			key = rcu_dereference(sta->gtk[key_idx]);
297
	} else
Johannes Berg's avatar
Johannes Berg committed
298
		key = rcu_dereference(sdata->keys[key_idx]);
299 300 301 302 303 304

	if (!key)
		goto out;

	memset(&params, 0, sizeof(params));

305
	params.cipher = key->conf.cipher;
306

307 308
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_TKIP:
309 310
		iv32 = key->u.tkip.tx.iv32;
		iv16 = key->u.tkip.tx.iv16;
311

312 313 314 315
		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
			drv_get_tkip_seq(sdata->local,
					 key->conf.hw_key_idx,
					 &iv32, &iv16);
316 317 318 319 320 321 322 323 324 325

		seq[0] = iv16 & 0xff;
		seq[1] = (iv16 >> 8) & 0xff;
		seq[2] = iv32 & 0xff;
		seq[3] = (iv32 >> 8) & 0xff;
		seq[4] = (iv32 >> 16) & 0xff;
		seq[5] = (iv32 >> 24) & 0xff;
		params.seq = seq;
		params.seq_len = 6;
		break;
326
	case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg's avatar
Johannes Berg committed
327 328 329 330 331 332 333
		pn64 = atomic64_read(&key->u.ccmp.tx_pn);
		seq[0] = pn64;
		seq[1] = pn64 >> 8;
		seq[2] = pn64 >> 16;
		seq[3] = pn64 >> 24;
		seq[4] = pn64 >> 32;
		seq[5] = pn64 >> 40;
334 335 336
		params.seq = seq;
		params.seq_len = 6;
		break;
337
	case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg's avatar
Johannes Berg committed
338 339 340 341 342 343 344
		pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
		seq[0] = pn64;
		seq[1] = pn64 >> 8;
		seq[2] = pn64 >> 16;
		seq[3] = pn64 >> 24;
		seq[4] = pn64 >> 32;
		seq[5] = pn64 >> 40;
345 346 347
		params.seq = seq;
		params.seq_len = 6;
		break;
348 349 350 351 352 353 354 355 356
	}

	params.key = key->conf.key;
	params.key_len = key->conf.keylen;

	callback(cookie, &params);
	err = 0;

 out:
357
	rcu_read_unlock();
358 359 360
	return err;
}

361 362
static int ieee80211_config_default_key(struct wiphy *wiphy,
					struct net_device *dev,
363 364
					u8 key_idx, bool uni,
					bool multi)
365
{
Johannes Berg's avatar
Johannes Berg committed
366
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
367

368
	ieee80211_set_default_key(sdata, key_idx, uni, multi);
369 370 371 372

	return 0;
}

373 374 375 376
static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
					     struct net_device *dev,
					     u8 key_idx)
{
377
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
378 379 380 381 382 383

	ieee80211_set_default_mgmt_key(sdata, key_idx);

	return 0;
}

384 385 386 387 388
void sta_set_rate_info_tx(struct sta_info *sta,
			  const struct ieee80211_tx_rate *rate,
			  struct rate_info *rinfo)
{
	rinfo->flags = 0;
389
	if (rate->flags & IEEE80211_TX_RC_MCS) {
390
		rinfo->flags |= RATE_INFO_FLAGS_MCS;
391 392 393 394 395 396 397 398 399 400 401
		rinfo->mcs = rate->idx;
	} else if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
		rinfo->mcs = ieee80211_rate_get_vht_mcs(rate);
		rinfo->nss = ieee80211_rate_get_vht_nss(rate);
	} else {
		struct ieee80211_supported_band *sband;
		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
		rinfo->legacy = sband->bitrates[rate->idx].bitrate;
	}
402 403
	if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
404 405 406 407
	if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
	if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
408 409 410 411
	if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
{
	rinfo->flags = 0;

	if (sta->last_rx_rate_flag & RX_FLAG_HT) {
		rinfo->flags |= RATE_INFO_FLAGS_MCS;
		rinfo->mcs = sta->last_rx_rate_idx;
	} else if (sta->last_rx_rate_flag & RX_FLAG_VHT) {
		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
		rinfo->nss = sta->last_rx_rate_vht_nss;
		rinfo->mcs = sta->last_rx_rate_idx;
	} else {
		struct ieee80211_supported_band *sband;

		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
		rinfo->legacy =
			sband->bitrates[sta->last_rx_rate_idx].bitrate;
	}

	if (sta->last_rx_rate_flag & RX_FLAG_40MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
	if (sta->last_rx_rate_flag & RX_FLAG_SHORT_GI)
		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
	if (sta->last_rx_rate_flag & RX_FLAG_80MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
	if (sta->last_rx_rate_flag & RX_FLAG_80P80MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
	if (sta->last_rx_rate_flag & RX_FLAG_160MHZ)
		rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
}

444 445
static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
{
446
	struct ieee80211_sub_if_data *sdata = sta->sdata;
447
	struct ieee80211_local *local = sdata->local;
448
	struct timespec uptime;
449
	u64 packets = 0;
450
	int i, ac;
451

452 453
	sinfo->generation = sdata->local->sta_generation;

454
	sinfo->filled = STATION_INFO_INACTIVE_TIME |
455 456
			STATION_INFO_RX_BYTES64 |
			STATION_INFO_TX_BYTES64 |
457 458
			STATION_INFO_RX_PACKETS |
			STATION_INFO_TX_PACKETS |
459 460
			STATION_INFO_TX_RETRIES |
			STATION_INFO_TX_FAILED |
461
			STATION_INFO_TX_BITRATE |
462
			STATION_INFO_RX_BITRATE |
463
			STATION_INFO_RX_DROP_MISC |
464
			STATION_INFO_BSS_PARAM |
465
			STATION_INFO_CONNECTED_TIME |
466 467
			STATION_INFO_STA_FLAGS |
			STATION_INFO_BEACON_LOSS_COUNT;
468 469 470

	do_posix_clock_monotonic_gettime(&uptime);
	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
471 472

	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
473 474 475 476 477 478
	sinfo->tx_bytes = 0;
	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
		sinfo->tx_bytes += sta->tx_bytes[ac];
		packets += sta->tx_packets[ac];
	}
	sinfo->tx_packets = packets;
479
	sinfo->rx_bytes = sta->rx_bytes;
480
	sinfo->rx_packets = sta->rx_packets;
481 482
	sinfo->tx_retries = sta->tx_retry_count;
	sinfo->tx_failed = sta->tx_retry_failed;
483
	sinfo->rx_dropped_misc = sta->rx_dropped;
484
	sinfo->beacon_loss_count = sta->beacon_loss_count;
485

486 487
	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
488
		sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
489 490 491
		if (!local->ops->get_rssi ||
		    drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
			sinfo->signal = (s8)sta->last_signal;
492
		sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
493
	}
494 495 496 497 498 499 500 501 502 503 504
	if (sta->chains) {
		sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
				 STATION_INFO_CHAIN_SIGNAL_AVG;

		sinfo->chains = sta->chains;
		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
			sinfo->chain_signal[i] = sta->chain_signal_last[i];
			sinfo->chain_signal_avg[i] =
				(s8) -ewma_read(&sta->chain_signal_avg[i]);
		}
	}
505

506
	sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
507
	sta_set_rate_info_rx(sta, &sinfo->rxrate);
508

Johannes Berg's avatar
Johannes Berg committed
509
	if (ieee80211_vif_is_mesh(&sdata->vif)) {
510 511 512
#ifdef CONFIG_MAC80211_MESH
		sinfo->filled |= STATION_INFO_LLID |
				 STATION_INFO_PLID |
513 514 515 516
				 STATION_INFO_PLINK_STATE |
				 STATION_INFO_LOCAL_PM |
				 STATION_INFO_PEER_PM |
				 STATION_INFO_NONPEER_PM;
517 518 519 520

		sinfo->llid = le16_to_cpu(sta->llid);
		sinfo->plid = le16_to_cpu(sta->plid);
		sinfo->plink_state = sta->plink_state;
521 522 523 524
		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
			sinfo->filled |= STATION_INFO_T_OFFSET;
			sinfo->t_offset = sta->t_offset;
		}
525 526 527
		sinfo->local_pm = sta->local_pm;
		sinfo->peer_pm = sta->peer_pm;
		sinfo->nonpeer_pm = sta->nonpeer_pm;
528
#endif
Johannes Berg's avatar
Johannes Berg committed
529
	}
530 531 532 533 534 535 536 537 538 539

	sinfo->bss_param.flags = 0;
	if (sdata->vif.bss_conf.use_cts_prot)
		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
	if (sdata->vif.bss_conf.use_short_preamble)
		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
	if (sdata->vif.bss_conf.use_short_slot)
		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
	sinfo->bss_param.dtim_period = sdata->local->hw.conf.ps_dtim_period;
	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
540 541 542 543 544 545

	sinfo->sta_flags.set = 0;
	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
				BIT(NL80211_STA_FLAG_WME) |
				BIT(NL80211_STA_FLAG_MFP) |
546
				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
547
				BIT(NL80211_STA_FLAG_ASSOCIATED) |
548
				BIT(NL80211_STA_FLAG_TDLS_PEER);
549 550 551 552 553 554 555 556 557 558
	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
	if (test_sta_flag(sta, WLAN_STA_WME))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
	if (test_sta_flag(sta, WLAN_STA_MFP))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
	if (test_sta_flag(sta, WLAN_STA_AUTH))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
559 560
	if (test_sta_flag(sta, WLAN_STA_ASSOC))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
561 562
	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
563 564
}

565 566 567 568 569
static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
	"rx_packets", "rx_bytes", "wep_weak_iv_count",
	"rx_duplicates", "rx_fragments", "rx_dropped",
	"tx_packets", "tx_bytes", "tx_fragments",
	"tx_filtered", "tx_retry_failed", "tx_retries",
570 571 572
	"beacon_loss", "sta_state", "txrate", "rxrate", "signal",
	"channel", "noise", "ch_time", "ch_time_busy",
	"ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
573 574 575 576 577 578 579
};
#define STA_STATS_LEN	ARRAY_SIZE(ieee80211_gstrings_sta_stats)

static int ieee80211_get_et_sset_count(struct wiphy *wiphy,
				       struct net_device *dev,
				       int sset)
{
580 581 582
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int rv = 0;

583
	if (sset == ETH_SS_STATS)
584 585 586
		rv += STA_STATS_LEN;

	rv += drv_get_et_sset_count(sdata, sset);
587

588 589 590
	if (rv == 0)
		return -EOPNOTSUPP;
	return rv;
591 592 593 594 595 596 597 598
}

static void ieee80211_get_et_stats(struct wiphy *wiphy,
				   struct net_device *dev,
				   struct ethtool_stats *stats,
				   u64 *data)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg's avatar
Johannes Berg committed
599 600
	struct ieee80211_chanctx_conf *chanctx_conf;
	struct ieee80211_channel *channel;
601 602
	struct sta_info *sta;
	struct ieee80211_local *local = sdata->local;
603 604 605 606
	struct station_info sinfo;
	struct survey_info survey;
	int i, q;
#define STA_STATS_SURVEY_LEN 7
607 608 609 610 611 612 613 614 615 616 617 618

	memset(data, 0, sizeof(u64) * STA_STATS_LEN);

#define ADD_STA_STATS(sta)				\
	do {						\
		data[i++] += sta->rx_packets;		\
		data[i++] += sta->rx_bytes;		\
		data[i++] += sta->wep_weak_iv_count;	\
		data[i++] += sta->num_duplicates;	\
		data[i++] += sta->rx_fragments;		\
		data[i++] += sta->rx_dropped;		\
							\
619 620
		data[i++] += sinfo.tx_packets;		\
		data[i++] += sinfo.tx_bytes;		\
621 622 623 624 625 626 627 628 629 630 631 632 633
		data[i++] += sta->tx_fragments;		\
		data[i++] += sta->tx_filtered_count;	\
		data[i++] += sta->tx_retry_failed;	\
		data[i++] += sta->tx_retry_count;	\
		data[i++] += sta->beacon_loss_count;	\
	} while (0)

	/* For Managed stations, find the single station based on BSSID
	 * and use that.  For interface types, iterate through all available
	 * stations and add stats for any station that is assigned to this
	 * network device.
	 */

634
	mutex_lock(&local->sta_mtx);
635 636 637

	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
		sta = sta_info_get_bss(sdata, sdata->u.mgd.bssid);
638 639 640 641

		if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
			goto do_survey;

642 643 644
		sinfo.filled = 0;
		sta_set_sinfo(sta, &sinfo);

645 646 647 648 649 650
		i = 0;
		ADD_STA_STATS(sta);

		data[i++] = sta->sta_state;


651
		if (sinfo.filled & STATION_INFO_TX_BITRATE)
652 653 654
			data[i] = 100000 *
				cfg80211_calculate_bitrate(&sinfo.txrate);
		i++;
655
		if (sinfo.filled & STATION_INFO_RX_BITRATE)
656 657 658 659
			data[i] = 100000 *
				cfg80211_calculate_bitrate(&sinfo.rxrate);
		i++;

660
		if (sinfo.filled & STATION_INFO_SIGNAL_AVG)
661 662
			data[i] = (u8)sinfo.signal_avg;
		i++;
663
	} else {
664
		list_for_each_entry(sta, &local->sta_list, list) {
665 666 667 668 669 670 671 672 673
			/* Make sure this station belongs to the proper dev */
			if (sta->sdata->dev != dev)
				continue;

			i = 0;
			ADD_STA_STATS(sta);
		}
	}

674 675 676
do_survey:
	i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
	/* Get survey stats for current channel */
Johannes Berg's avatar
Johannes Berg committed
677
	survey.filled = 0;
678

Johannes Berg's avatar
Johannes Berg committed
679 680 681
	rcu_read_lock();
	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
	if (chanctx_conf)
682
		channel = chanctx_conf->def.chan;
Johannes Berg's avatar
Johannes Berg committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696
	else
		channel = NULL;
	rcu_read_unlock();

	if (channel) {
		q = 0;
		do {
			survey.filled = 0;
			if (drv_get_survey(local, q, &survey) != 0) {
				survey.filled = 0;
				break;
			}
			q++;
		} while (channel != survey.channel);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	}

	if (survey.filled)
		data[i++] = survey.channel->center_freq;
	else
		data[i++] = 0;
	if (survey.filled & SURVEY_INFO_NOISE_DBM)
		data[i++] = (u8)survey.noise;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME)
		data[i++] = survey.channel_time;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
		data[i++] = survey.channel_time_busy;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
		data[i++] = survey.channel_time_ext_busy;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_RX)
		data[i++] = survey.channel_time_rx;
	else
		data[i++] = -1LL;
	if (survey.filled & SURVEY_INFO_CHANNEL_TIME_TX)
		data[i++] = survey.channel_time_tx;
	else
		data[i++] = -1LL;

728
	mutex_unlock(&local->sta_mtx);
729

730 731 732
	if (WARN_ON(i != STA_STATS_LEN))
		return;

733
	drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
734 735 736 737 738 739
}

static void ieee80211_get_et_strings(struct wiphy *wiphy,
				     struct net_device *dev,
				     u32 sset, u8 *data)
{
740 741 742
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int sz_sta_stats = 0;

743
	if (sset == ETH_SS_STATS) {
744
		sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
745
		memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
746
	}
747
	drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
748
}
749 750 751 752

static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
				 int idx, u8 *mac, struct station_info *sinfo)
{
753
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
754
	struct ieee80211_local *local = sdata->local;
755
	struct sta_info *sta;
756 757
	int ret = -ENOENT;

758
	mutex_lock(&local->sta_mtx);
759

760
	sta = sta_info_get_by_idx(sdata, idx);
761 762
	if (sta) {
		ret = 0;
763
		memcpy(mac, sta->sta.addr, ETH_ALEN);
764 765
		sta_set_sinfo(sta, sinfo);
	}
766

767
	mutex_unlock(&local->sta_mtx);
768

769
	return ret;
770 771
}

772 773 774 775 776 777 778 779
static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
				 int idx, struct survey_info *survey)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	return drv_get_survey(local, idx, survey);
}

780
static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
781
				 u8 *mac, struct station_info *sinfo)
782
{
783
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
784
	struct ieee80211_local *local = sdata->local;
785
	struct sta_info *sta;
786
	int ret = -ENOENT;
787

788
	mutex_lock(&local->sta_mtx);
789

790
	sta = sta_info_get_bss(sdata, mac);
791 792 793 794 795
	if (sta) {
		ret = 0;
		sta_set_sinfo(sta, sinfo);
	}

796
	mutex_unlock(&local->sta_mtx);
797 798

	return ret;
799 800
}

Johannes Berg's avatar
Johannes Berg committed
801
static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
802
					 struct cfg80211_chan_def *chandef)
803 804
{
	struct ieee80211_local *local = wiphy_priv(wiphy);
Johannes Berg's avatar
Johannes Berg committed
805 806
	struct ieee80211_sub_if_data *sdata;
	int ret = 0;
807

808
	if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
Johannes Berg's avatar
Johannes Berg committed
809
		return 0;
810

Johannes Berg's avatar
Johannes Berg committed
811 812 813 814 815 816 817
	mutex_lock(&local->iflist_mtx);
	if (local->use_chanctx) {
		sdata = rcu_dereference_protected(
				local->monitor_sdata,
				lockdep_is_held(&local->iflist_mtx));
		if (sdata) {
			ieee80211_vif_release_channel(sdata);
818
			ret = ieee80211_vif_use_channel(sdata, chandef,
Johannes Berg's avatar
Johannes Berg committed
819 820 821
					IEEE80211_CHANCTX_EXCLUSIVE);
		}
	} else if (local->open_count == local->monitors) {
822
		local->_oper_chandef = *chandef;
Johannes Berg's avatar
Johannes Berg committed
823 824
		ieee80211_hw_config(local, 0);
	}
825

826 827
	if (ret == 0)
		local->monitor_chandef = *chandef;
Johannes Berg's avatar
Johannes Berg committed
828
	mutex_unlock(&local->iflist_mtx);
829

Johannes Berg's avatar
Johannes Berg committed
830
	return ret;
831 832
}

833
static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
834
				    const u8 *resp, size_t resp_len)
835
{
836
	struct probe_resp *new, *old;
837 838

	if (!resp || !resp_len)
839
		return 1;
840

841
	old = rtnl_dereference(sdata->u.ap.probe_resp);
842

843
	new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
844 845 846
	if (!new)
		return -ENOMEM;

847 848
	new->len = resp_len;
	memcpy(new->data, resp, resp_len);
849 850

	rcu_assign_pointer(sdata->u.ap.probe_resp, new);
851 852
	if (old)
		kfree_rcu(old, rcu_head);
853 854 855 856

	return 0;
}

857 858
static int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
				   struct cfg80211_beacon_data *params)
859 860 861
{
	struct beacon_data *new, *old;
	int new_head_len, new_tail_len;
862 863
	int size, err;
	u32 changed = BSS_CHANGED_BEACON;
864

865
	old = rtnl_dereference(sdata->u.ap.beacon);
866 867 868

	/* Need to have a beacon head if we don't have one yet */
	if (!params->head && !old)
869
		return -EINVAL;
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

	/* new or old head? */
	if (params->head)
		new_head_len = params->head_len;
	else
		new_head_len = old->head_len;

	/* new or old tail? */
	if (params->tail || !old)
		/* params->tail_len will be zero for !params->tail */
		new_tail_len = params->tail_len;
	else
		new_tail_len = old->tail_len;

	size = sizeof(*new) + new_head_len + new_tail_len;

	new = kzalloc(size, GFP_KERNEL);
	if (!new)
		return -ENOMEM;

	/* start filling the new info now */

	/*
	 * pointers go into the block we allocated,
	 * memory is | beacon_data | head | tail |
	 */
	new->head = ((u8 *) new) + sizeof(*new);
	new->tail = new->head + new_head_len;
	new->head_len = new_head_len;
	new->tail_len = new_tail_len;

	/* copy in head */
	if (params->head)
		memcpy(new->head, params->head, new_head_len);
	else
		memcpy(new->head, old->head, new_head_len);

	/* copy in optional tail */
	if (params->tail)
		memcpy(new->tail, params->tail, new_tail_len);
	else
		if (old)
			memcpy(new->tail, old->tail, new_tail_len);

914 915
	err = ieee80211_set_probe_resp(sdata, params->probe_resp,
				       params->probe_resp_len);
916 917 918
	if (err < 0)
		return err;
	if (err == 0)
919 920
		changed |= BSS_CHANGED_AP_PROBE_RESP;

921 922 923 924
	rcu_assign_pointer(sdata->u.ap.beacon, new);

	if (old)
		kfree_rcu(old, rcu_head);
925

926
	return changed;