cfg.c 103 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 ieee80211_local *local = sdata->local;
137
	struct sta_info *sta = NULL;
138
	const struct ieee80211_cipher_scheme *cs = NULL;
139
	struct ieee80211_key *key;
140
	int err;
141

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

145
	/* reject WEP and TKIP keys if WEP failed to initialize */
146 147 148
	switch (params->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_TKIP:
149
	case WLAN_CIPHER_SUITE_WEP104:
150
		if (IS_ERR(local->wep_tx_tfm))
151
			return -EINVAL;
152
		break;
153 154 155 156
	case WLAN_CIPHER_SUITE_CCMP:
	case WLAN_CIPHER_SUITE_AES_CMAC:
	case WLAN_CIPHER_SUITE_GCMP:
		break;
157
	default:
158
		cs = ieee80211_cs_get(local, params->cipher, sdata->vif.type);
159
		break;
160 161
	}

162
	key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
163 164
				  params->key, params->seq_len, params->seq,
				  cs);
165 166
	if (IS_ERR(key))
		return PTR_ERR(key);
167

168 169 170
	if (pairwise)
		key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;

171
	mutex_lock(&local->sta_mtx);
172

173
	if (mac_addr) {
174 175 176 177
		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
178 179 180 181 182 183 184 185 186 187 188
		/*
		 * 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)) {
189
			ieee80211_key_free_unused(key);
190 191
			err = -ENOENT;
			goto out_unlock;
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 219 220 221 222 223 224 225 226
	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;
	}

227 228 229
	if (sta)
		sta->cipher_scheme = cs;

230
	err = ieee80211_key_link(key, sdata, sta);
231

232
 out_unlock:
233
	mutex_unlock(&local->sta_mtx);
234 235

	return err;
236 237 238
}

static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
239
			     u8 key_idx, bool pairwise, const u8 *mac_addr)
240
{
241 242
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct ieee80211_local *local = sdata->local;
243
	struct sta_info *sta;
244
	struct ieee80211_key *key = NULL;
245 246
	int ret;

247 248
	mutex_lock(&local->sta_mtx);
	mutex_lock(&local->key_mtx);
249

250
	if (mac_addr) {
251 252
		ret = -ENOENT;

253
		sta = sta_info_get_bss(sdata, mac_addr);
254
		if (!sta)
255
			goto out_unlock;
256

257
		if (pairwise)
258
			key = key_mtx_dereference(local, sta->ptk[key_idx]);
259
		else
260
			key = key_mtx_dereference(local, sta->gtk[key_idx]);
261
	} else
262
		key = key_mtx_dereference(local, sdata->keys[key_idx]);
263

264
	if (!key) {
265 266 267
		ret = -ENOENT;
		goto out_unlock;
	}
268

269
	ieee80211_key_free(key, true);
270

271 272
	ret = 0;
 out_unlock:
273 274
	mutex_unlock(&local->key_mtx);
	mutex_unlock(&local->sta_mtx);
275 276

	return ret;
277 278
}

279
static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
280 281
			     u8 key_idx, bool pairwise, const u8 *mac_addr,
			     void *cookie,
282 283 284
			     void (*callback)(void *cookie,
					      struct key_params *params))
{
285
	struct ieee80211_sub_if_data *sdata;
286 287 288
	struct sta_info *sta = NULL;
	u8 seq[6] = {0};
	struct key_params params;
289
	struct ieee80211_key *key = NULL;
Johannes Berg's avatar
Johannes Berg committed
290
	u64 pn64;
291 292 293 294
	u32 iv32;
	u16 iv16;
	int err = -ENOENT;

295 296
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

297 298
	rcu_read_lock();

299
	if (mac_addr) {
300
		sta = sta_info_get_bss(sdata, mac_addr);
301 302 303
		if (!sta)
			goto out;

304
		if (pairwise)
305
			key = rcu_dereference(sta->ptk[key_idx]);
306
		else if (key_idx < NUM_DEFAULT_KEYS)
Johannes Berg's avatar
Johannes Berg committed
307
			key = rcu_dereference(sta->gtk[key_idx]);
308
	} else
Johannes Berg's avatar
Johannes Berg committed
309
		key = rcu_dereference(sdata->keys[key_idx]);
310 311 312 313 314 315

	if (!key)
		goto out;

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

316
	params.cipher = key->conf.cipher;
317

318 319
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_TKIP:
320 321
		iv32 = key->u.tkip.tx.iv32;
		iv16 = key->u.tkip.tx.iv16;
322

323 324 325 326
		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
			drv_get_tkip_seq(sdata->local,
					 key->conf.hw_key_idx,
					 &iv32, &iv16);
327 328 329 330 331 332 333 334 335 336

		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;
337
	case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg's avatar
Johannes Berg committed
338 339 340 341 342 343 344
		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;
345 346 347
		params.seq = seq;
		params.seq_len = 6;
		break;
348
	case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg's avatar
Johannes Berg committed
349 350 351 352 353 354 355
		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;
356 357 358
		params.seq = seq;
		params.seq_len = 6;
		break;
359 360 361 362 363 364 365 366 367
	}

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

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

 out:
368
	rcu_read_unlock();
369 370 371
	return err;
}

372 373
static int ieee80211_config_default_key(struct wiphy *wiphy,
					struct net_device *dev,
374 375
					u8 key_idx, bool uni,
					bool multi)
376
{
Johannes Berg's avatar
Johannes Berg committed
377
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
378

379
	ieee80211_set_default_key(sdata, key_idx, uni, multi);
380 381 382 383

	return 0;
}

384 385 386 387
static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
					     struct net_device *dev,
					     u8 key_idx)
{
388
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
389 390 391 392 393 394

	ieee80211_set_default_mgmt_key(sdata, key_idx);

	return 0;
}

395 396 397 398 399
void sta_set_rate_info_tx(struct sta_info *sta,
			  const struct ieee80211_tx_rate *rate,
			  struct rate_info *rinfo)
{
	rinfo->flags = 0;
400
	if (rate->flags & IEEE80211_TX_RC_MCS) {
401
		rinfo->flags |= RATE_INFO_FLAGS_MCS;
402 403 404 405 406 407 408
		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;
409 410 411
		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
		u16 brate;

412 413
		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
414 415
		brate = sband->bitrates[rate->idx].bitrate;
		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
416
	}
417 418
	if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
419 420 421 422
	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;
423 424 425 426
	if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
}

427 428 429 430 431 432 433 434 435 436 437 438 439
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;
440 441
		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
		u16 brate;
442 443 444

		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
445 446
		brate = sband->bitrates[sta->last_rx_rate_idx].bitrate;
		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
447 448 449 450 451 452 453 454 455 456 457 458 459 460
	}

	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;
}

461 462
static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
{
463
	struct ieee80211_sub_if_data *sdata = sta->sdata;
464
	struct ieee80211_local *local = sdata->local;
465
	struct timespec uptime;
466
	u64 packets = 0;
467
	int i, ac;
468

469 470
	sinfo->generation = sdata->local->sta_generation;

471
	sinfo->filled = STATION_INFO_INACTIVE_TIME |
472 473
			STATION_INFO_RX_BYTES64 |
			STATION_INFO_TX_BYTES64 |
474 475
			STATION_INFO_RX_PACKETS |
			STATION_INFO_TX_PACKETS |
476 477
			STATION_INFO_TX_RETRIES |
			STATION_INFO_TX_FAILED |
478
			STATION_INFO_TX_BITRATE |
479
			STATION_INFO_RX_BITRATE |
480
			STATION_INFO_RX_DROP_MISC |
481
			STATION_INFO_BSS_PARAM |
482
			STATION_INFO_CONNECTED_TIME |
483 484
			STATION_INFO_STA_FLAGS |
			STATION_INFO_BEACON_LOSS_COUNT;
485 486 487

	do_posix_clock_monotonic_gettime(&uptime);
	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
488 489

	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
490 491 492 493 494 495
	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;
496
	sinfo->rx_bytes = sta->rx_bytes;
497
	sinfo->rx_packets = sta->rx_packets;
498 499
	sinfo->tx_retries = sta->tx_retry_count;
	sinfo->tx_failed = sta->tx_retry_failed;
500
	sinfo->rx_dropped_misc = sta->rx_dropped;
501
	sinfo->beacon_loss_count = sta->beacon_loss_count;
502

503 504
	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
505
		sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
506 507 508
		if (!local->ops->get_rssi ||
		    drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
			sinfo->signal = (s8)sta->last_signal;
509
		sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
510
	}
511 512 513 514 515 516 517 518 519 520 521
	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]);
		}
	}
522

523
	sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
524
	sta_set_rate_info_rx(sta, &sinfo->rxrate);
525

Johannes Berg's avatar
Johannes Berg committed
526
	if (ieee80211_vif_is_mesh(&sdata->vif)) {
527 528 529
#ifdef CONFIG_MAC80211_MESH
		sinfo->filled |= STATION_INFO_LLID |
				 STATION_INFO_PLID |
530 531 532 533
				 STATION_INFO_PLINK_STATE |
				 STATION_INFO_LOCAL_PM |
				 STATION_INFO_PEER_PM |
				 STATION_INFO_NONPEER_PM;
534

535 536
		sinfo->llid = sta->llid;
		sinfo->plid = sta->plid;
537
		sinfo->plink_state = sta->plink_state;
538 539 540 541
		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
			sinfo->filled |= STATION_INFO_T_OFFSET;
			sinfo->t_offset = sta->t_offset;
		}
542 543 544
		sinfo->local_pm = sta->local_pm;
		sinfo->peer_pm = sta->peer_pm;
		sinfo->nonpeer_pm = sta->nonpeer_pm;
545
#endif
Johannes Berg's avatar
Johannes Berg committed
546
	}
547 548 549 550 551 552 553 554 555 556

	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;
557 558 559 560 561 562

	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) |
563
				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
564
				BIT(NL80211_STA_FLAG_ASSOCIATED) |
565
				BIT(NL80211_STA_FLAG_TDLS_PEER);
566 567 568 569 570 571 572 573 574 575
	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);
576 577
	if (test_sta_flag(sta, WLAN_STA_ASSOC))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
578 579
	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
580 581
}

582 583 584 585 586
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",
587 588 589
	"beacon_loss", "sta_state", "txrate", "rxrate", "signal",
	"channel", "noise", "ch_time", "ch_time_busy",
	"ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
590 591 592 593 594 595 596
};
#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)
{
597 598 599
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int rv = 0;

600
	if (sset == ETH_SS_STATS)
601 602 603
		rv += STA_STATS_LEN;

	rv += drv_get_et_sset_count(sdata, sset);
604

605 606 607
	if (rv == 0)
		return -EOPNOTSUPP;
	return rv;
608 609 610 611 612 613 614 615
}

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
616 617
	struct ieee80211_chanctx_conf *chanctx_conf;
	struct ieee80211_channel *channel;
618 619
	struct sta_info *sta;
	struct ieee80211_local *local = sdata->local;
620 621 622 623
	struct station_info sinfo;
	struct survey_info survey;
	int i, q;
#define STA_STATS_SURVEY_LEN 7
624 625 626 627 628 629 630 631 632 633 634 635

	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;		\
							\
636 637
		data[i++] += sinfo.tx_packets;		\
		data[i++] += sinfo.tx_bytes;		\
638 639 640 641 642 643 644 645 646 647 648 649 650
		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.
	 */

651
	mutex_lock(&local->sta_mtx);
652 653 654

	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
		sta = sta_info_get_bss(sdata, sdata->u.mgd.bssid);
655 656 657 658

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

659 660 661
		sinfo.filled = 0;
		sta_set_sinfo(sta, &sinfo);

662 663 664 665 666 667
		i = 0;
		ADD_STA_STATS(sta);

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


668
		if (sinfo.filled & STATION_INFO_TX_BITRATE)
669 670 671
			data[i] = 100000 *
				cfg80211_calculate_bitrate(&sinfo.txrate);
		i++;
672
		if (sinfo.filled & STATION_INFO_RX_BITRATE)
673 674 675 676
			data[i] = 100000 *
				cfg80211_calculate_bitrate(&sinfo.rxrate);
		i++;

677
		if (sinfo.filled & STATION_INFO_SIGNAL_AVG)
678 679
			data[i] = (u8)sinfo.signal_avg;
		i++;
680
	} else {
681
		list_for_each_entry(sta, &local->sta_list, list) {
682 683 684 685
			/* Make sure this station belongs to the proper dev */
			if (sta->sdata->dev != dev)
				continue;

686 687
			sinfo.filled = 0;
			sta_set_sinfo(sta, &sinfo);
688 689 690 691 692
			i = 0;
			ADD_STA_STATS(sta);
		}
	}

693 694 695
do_survey:
	i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
	/* Get survey stats for current channel */
Johannes Berg's avatar
Johannes Berg committed
696
	survey.filled = 0;
697

Johannes Berg's avatar
Johannes Berg committed
698 699 700
	rcu_read_lock();
	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
	if (chanctx_conf)
701
		channel = chanctx_conf->def.chan;
Johannes Berg's avatar
Johannes Berg committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715
	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);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	}

	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;

747
	mutex_unlock(&local->sta_mtx);
748

749 750 751
	if (WARN_ON(i != STA_STATS_LEN))
		return;

752
	drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
753 754 755 756 757 758
}

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

762
	if (sset == ETH_SS_STATS) {
763
		sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
764
		memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
765
	}
766
	drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
767
}
768 769 770 771

static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
				 int idx, u8 *mac, struct station_info *sinfo)
{
772
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
773
	struct ieee80211_local *local = sdata->local;
774
	struct sta_info *sta;
775 776
	int ret = -ENOENT;

777
	mutex_lock(&local->sta_mtx);
778

779
	sta = sta_info_get_by_idx(sdata, idx);
780 781
	if (sta) {
		ret = 0;
782
		memcpy(mac, sta->sta.addr, ETH_ALEN);
783 784
		sta_set_sinfo(sta, sinfo);
	}
785

786
	mutex_unlock(&local->sta_mtx);
787

788
	return ret;
789 790
}

791 792 793 794 795 796 797 798
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);
}

799
static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
800
				 u8 *mac, struct station_info *sinfo)
801
{
802
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
803
	struct ieee80211_local *local = sdata->local;
804
	struct sta_info *sta;
805
	int ret = -ENOENT;
806

807
	mutex_lock(&local->sta_mtx);
808

809
	sta = sta_info_get_bss(sdata, mac);
810 811 812 813 814
	if (sta) {
		ret = 0;
		sta_set_sinfo(sta, sinfo);
	}

815
	mutex_unlock(&local->sta_mtx);
816 817

	return ret;
818 819
}

Johannes Berg's avatar
Johannes Berg committed
820
static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
821
					 struct cfg80211_chan_def *chandef)
822 823
{
	struct ieee80211_local *local = wiphy_priv(wiphy);
Johannes Berg's avatar
Johannes Berg committed
824 825
	struct ieee80211_sub_if_data *sdata;
	int ret = 0;
826

827
	if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
Johannes Berg's avatar
Johannes Berg committed
828
		return 0;
829

Johannes Berg's avatar
Johannes Berg committed
830 831 832 833 834 835 836
	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);
837
			ret = ieee80211_vif_use_channel(sdata, chandef,
Johannes Berg's avatar
Johannes Berg committed
838 839 840
					IEEE80211_CHANCTX_EXCLUSIVE);
		}
	} else if (local->open_count == local->monitors) {
841
		local->_oper_chandef = *chandef;
Johannes Berg's avatar
Johannes Berg committed
842 843
		ieee80211_hw_config(local, 0);
	}
844

845 846
	if (ret == 0)
		local->monitor_chandef = *chandef;
Johannes Berg's avatar
Johannes Berg committed
847
	mutex_unlock(&local->iflist_mtx);
848

Johannes Berg's avatar
Johannes Berg committed
849
	return ret;
850 851
}

852
static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
853
				    const u8 *resp, size_t resp_len)
854
{
855
	struct probe_resp *new, *old;
856 857

	if (!resp || !resp_len)
858
		return 1;
859

860
	old = sdata_dereference(sdata->u.ap.probe_resp, sdata);
861

862
	new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
863 864 865
	if (!new)
		return -ENOMEM;

866 867
	new->len = resp_len;
	memcpy(new->data, resp, resp_len);
868 869

	rcu_assign_pointer(sdata->u.ap.probe_resp, new);
870 871
	if (old)
		kfree_rcu(old, rcu_head);
872 873 874 875

	return 0;
}

876 877
int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
			    struct cfg80211_beacon_data *params)
878 879 880
{
	struct beacon_data *new, *old;
	int new_head_len, new_tail_len;
881 882
	int size, err;
	u32 changed = BSS_CHANGED_BEACON;
883

884 885
	old = sdata_dereference(sdata->u.ap.beacon, sdata);

886 887 888

	/* Need to have a beacon head if we don't have one yet */
	if (!params->head && !old)
889
		return -EINVAL;
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933

	/* 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);

934 935
	err = ieee80211_set_probe_resp(sdata, params->probe_resp,
				       params->probe_resp_len);
936 937 938
	if (err < 0)
		return err;
	if (err == 0)
939 940
		changed |= BSS_CHANGED_AP_PROBE_RESP;

941 942 943 944
	rcu_assign_pointer(sdata->u.ap.beacon, new);

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

946
	return changed;
947 948
}

949 950
static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
			      struct cfg80211_ap_settings *params)
951
{
952
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
953
	struct beacon_data *old;
954
	struct ieee80211_sub_if_data *vlan;
955 956 957
	u32 changed = BSS_CHANGED_BEACON_INT |
		      BSS_CHANGED_BEACON_ENABLED |
		      BSS_CHANGED_BEACON |
958 959
		      BSS_CHANGED_SSID |
		      BSS_CHANGED_P2P_PS;
960
	int err;
961

962
	old = sdata_dereference(sdata->u.ap.beacon, sdata);
963 964 965
	if (old)
		return -EALREADY;

966 967 968
	/* TODO: make hostapd tell us what it wants */
	sdata->smps_mode = IEEE80211_SMPS_OFF;
	sdata->needed_rx_chains = sdata->local->rx_chains;
969
	sdata->radar_required = params->radar_required;
970