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

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

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

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

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

45
	return wdev;
46 47
}

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

52
	return 0;
53 54
}

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

63
	ret = ieee80211_if_change_type(sdata, type);
64 65
	if (ret)
		return ret;
66

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

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

		if (ieee80211_sdata_running(sdata)) {
78 79 80
			u32 mask = MONITOR_FLAG_COOK_FRAMES |
				   MONITOR_FLAG_ACTIVE;

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

108 109 110
	return 0;
}

111 112 113
static int ieee80211_start_p2p_device(struct wiphy *wiphy,
				      struct wireless_dev *wdev)
{
114 115 116 117 118 119 120 121 122
	struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
	int ret;

	mutex_lock(&sdata->local->chanctx_mtx);
	ret = ieee80211_check_combinations(sdata, NULL, 0, 0);
	mutex_unlock(&sdata->local->chanctx_mtx);
	if (ret < 0)
		return ret;

123 124 125 126 127 128 129 130 131
	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));
}

132 133 134 135 136 137 138 139 140 141
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;
}

142
static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
143
			     u8 key_idx, bool pairwise, const u8 *mac_addr,
144 145
			     struct key_params *params)
{
146
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
147
	struct ieee80211_local *local = sdata->local;
148
	struct sta_info *sta = NULL;
149
	const struct ieee80211_cipher_scheme *cs = NULL;
150
	struct ieee80211_key *key;
151
	int err;
152

153
	if (!ieee80211_sdata_running(sdata))
Johannes Berg's avatar
Johannes Berg committed
154 155
		return -ENETDOWN;

156
	/* reject WEP and TKIP keys if WEP failed to initialize */
157 158 159
	switch (params->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_TKIP:
160
	case WLAN_CIPHER_SUITE_WEP104:
161
		if (IS_ERR(local->wep_tx_tfm))
162
			return -EINVAL;
163
		break;
164 165 166 167
	case WLAN_CIPHER_SUITE_CCMP:
	case WLAN_CIPHER_SUITE_AES_CMAC:
	case WLAN_CIPHER_SUITE_GCMP:
		break;
168
	default:
169
		cs = ieee80211_cs_get(local, params->cipher, sdata->vif.type);
170
		break;
171 172
	}

173
	key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
174 175
				  params->key, params->seq_len, params->seq,
				  cs);
176 177
	if (IS_ERR(key))
		return PTR_ERR(key);
178

179 180 181
	if (pairwise)
		key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;

182
	mutex_lock(&local->sta_mtx);
183

184
	if (mac_addr) {
185 186 187 188
		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
189 190 191 192 193
		/*
		 * 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
194
		 * so it will set the key again after association.
Johannes Berg's avatar
Johannes Berg committed
195 196 197 198 199
		 *
		 * 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)) {
200
			ieee80211_key_free_unused(key);
201 202
			err = -ENOENT;
			goto out_unlock;
203
		}
204 205
	}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	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:
233
	case NL80211_IFTYPE_OCB:
234 235 236 237 238
		/* shouldn't happen */
		WARN_ON_ONCE(1);
		break;
	}

239 240 241
	if (sta)
		sta->cipher_scheme = cs;

242
	err = ieee80211_key_link(key, sdata, sta);
243

244
 out_unlock:
245
	mutex_unlock(&local->sta_mtx);
246 247

	return err;
248 249 250
}

static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
251
			     u8 key_idx, bool pairwise, const u8 *mac_addr)
252
{
253 254
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct ieee80211_local *local = sdata->local;
255
	struct sta_info *sta;
256
	struct ieee80211_key *key = NULL;
257 258
	int ret;

259 260
	mutex_lock(&local->sta_mtx);
	mutex_lock(&local->key_mtx);
261

262
	if (mac_addr) {
263 264
		ret = -ENOENT;

265
		sta = sta_info_get_bss(sdata, mac_addr);
266
		if (!sta)
267
			goto out_unlock;
268

269
		if (pairwise)
270
			key = key_mtx_dereference(local, sta->ptk[key_idx]);
271
		else
272
			key = key_mtx_dereference(local, sta->gtk[key_idx]);
273
	} else
274
		key = key_mtx_dereference(local, sdata->keys[key_idx]);
275

276
	if (!key) {
277 278 279
		ret = -ENOENT;
		goto out_unlock;
	}
280

281
	ieee80211_key_free(key, true);
282

283 284
	ret = 0;
 out_unlock:
285 286
	mutex_unlock(&local->key_mtx);
	mutex_unlock(&local->sta_mtx);
287 288

	return ret;
289 290
}

291
static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
292 293
			     u8 key_idx, bool pairwise, const u8 *mac_addr,
			     void *cookie,
294 295 296
			     void (*callback)(void *cookie,
					      struct key_params *params))
{
297
	struct ieee80211_sub_if_data *sdata;
298 299 300
	struct sta_info *sta = NULL;
	u8 seq[6] = {0};
	struct key_params params;
301
	struct ieee80211_key *key = NULL;
Johannes Berg's avatar
Johannes Berg committed
302
	u64 pn64;
303 304 305 306
	u32 iv32;
	u16 iv16;
	int err = -ENOENT;

307 308
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

309 310
	rcu_read_lock();

311
	if (mac_addr) {
312
		sta = sta_info_get_bss(sdata, mac_addr);
313 314 315
		if (!sta)
			goto out;

316
		if (pairwise && key_idx < NUM_DEFAULT_KEYS)
317
			key = rcu_dereference(sta->ptk[key_idx]);
318 319
		else if (!pairwise &&
			 key_idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
Johannes Berg's avatar
Johannes Berg committed
320
			key = rcu_dereference(sta->gtk[key_idx]);
321
	} else
Johannes Berg's avatar
Johannes Berg committed
322
		key = rcu_dereference(sdata->keys[key_idx]);
323 324 325 326 327 328

	if (!key)
		goto out;

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

329
	params.cipher = key->conf.cipher;
330

331 332
	switch (key->conf.cipher) {
	case WLAN_CIPHER_SUITE_TKIP:
333 334
		iv32 = key->u.tkip.tx.iv32;
		iv16 = key->u.tkip.tx.iv16;
335

336 337 338 339
		if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
			drv_get_tkip_seq(sdata->local,
					 key->conf.hw_key_idx,
					 &iv32, &iv16);
340 341 342 343 344 345 346 347 348 349

		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;
350
	case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg's avatar
Johannes Berg committed
351 352 353 354 355 356 357
		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;
358 359 360
		params.seq = seq;
		params.seq_len = 6;
		break;
361
	case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg's avatar
Johannes Berg committed
362 363 364 365 366 367 368
		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;
369 370 371
		params.seq = seq;
		params.seq_len = 6;
		break;
372 373 374 375 376 377 378 379 380
	}

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

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

 out:
381
	rcu_read_unlock();
382 383 384
	return err;
}

385 386
static int ieee80211_config_default_key(struct wiphy *wiphy,
					struct net_device *dev,
387 388
					u8 key_idx, bool uni,
					bool multi)
389
{
Johannes Berg's avatar
Johannes Berg committed
390
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
391

392
	ieee80211_set_default_key(sdata, key_idx, uni, multi);
393 394 395 396

	return 0;
}

397 398 399 400
static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
					     struct net_device *dev,
					     u8 key_idx)
{
401
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
402 403 404 405 406 407

	ieee80211_set_default_mgmt_key(sdata, key_idx);

	return 0;
}

408 409 410 411 412
void sta_set_rate_info_tx(struct sta_info *sta,
			  const struct ieee80211_tx_rate *rate,
			  struct rate_info *rinfo)
{
	rinfo->flags = 0;
413
	if (rate->flags & IEEE80211_TX_RC_MCS) {
414
		rinfo->flags |= RATE_INFO_FLAGS_MCS;
415 416 417 418 419 420 421
		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;
422 423 424
		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
		u16 brate;

425 426
		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
427 428
		brate = sband->bitrates[rate->idx].bitrate;
		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
429
	}
430 431
	if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
		rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
432 433 434 435
	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;
436 437 438 439
	if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
}

440 441 442 443 444 445 446 447 448 449 450 451 452
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;
453 454
		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
		u16 brate;
455 456 457

		sband = sta->local->hw.wiphy->bands[
				ieee80211_get_sdata_band(sta->sdata)];
458 459
		brate = sband->bitrates[sta->last_rx_rate_idx].bitrate;
		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
460 461 462 463 464 465
	}

	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;
466
	if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_80MHZ)
467
		rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
468
	if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_80P80MHZ)
469
		rinfo->flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
470
	if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_160MHZ)
471 472 473
		rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
}

474
static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
475
				  int idx, u8 *mac, struct station_info *sinfo)
476
{
477
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
478
	struct ieee80211_local *local = sdata->local;
479
	struct sta_info *sta;
480 481
	int ret = -ENOENT;

482
	mutex_lock(&local->sta_mtx);
483

484
	sta = sta_info_get_by_idx(sdata, idx);
485 486
	if (sta) {
		ret = 0;
487
		memcpy(mac, sta->sta.addr, ETH_ALEN);
488 489
		sta_set_sinfo(sta, sinfo);
	}
490

491
	mutex_unlock(&local->sta_mtx);
492

493
	return ret;
494 495
}

496 497 498 499 500 501 502 503
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);
}

504
static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
505
				 const u8 *mac, struct station_info *sinfo)
506
{
507
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
508
	struct ieee80211_local *local = sdata->local;
509
	struct sta_info *sta;
510
	int ret = -ENOENT;
511

512
	mutex_lock(&local->sta_mtx);
513

514
	sta = sta_info_get_bss(sdata, mac);
515 516 517 518 519
	if (sta) {
		ret = 0;
		sta_set_sinfo(sta, sinfo);
	}

520
	mutex_unlock(&local->sta_mtx);
521 522

	return ret;
523 524
}

Johannes Berg's avatar
Johannes Berg committed
525
static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
526
					 struct cfg80211_chan_def *chandef)
527 528
{
	struct ieee80211_local *local = wiphy_priv(wiphy);
Johannes Berg's avatar
Johannes Berg committed
529 530
	struct ieee80211_sub_if_data *sdata;
	int ret = 0;
531

532
	if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
Johannes Berg's avatar
Johannes Berg committed
533
		return 0;
534

535
	mutex_lock(&local->mtx);
Johannes Berg's avatar
Johannes Berg committed
536 537 538 539 540 541 542
	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);
543
			ret = ieee80211_vif_use_channel(sdata, chandef,
Johannes Berg's avatar
Johannes Berg committed
544 545 546
					IEEE80211_CHANCTX_EXCLUSIVE);
		}
	} else if (local->open_count == local->monitors) {
547
		local->_oper_chandef = *chandef;
Johannes Berg's avatar
Johannes Berg committed
548 549
		ieee80211_hw_config(local, 0);
	}
550

551 552
	if (ret == 0)
		local->monitor_chandef = *chandef;
Johannes Berg's avatar
Johannes Berg committed
553
	mutex_unlock(&local->iflist_mtx);
554
	mutex_unlock(&local->mtx);
555

Johannes Berg's avatar
Johannes Berg committed
556
	return ret;
557 558
}

559
static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
560 561
				    const u8 *resp, size_t resp_len,
				    const struct ieee80211_csa_settings *csa)
562
{
563
	struct probe_resp *new, *old;
564 565

	if (!resp || !resp_len)
566
		return 1;
567

568
	old = sdata_dereference(sdata->u.ap.probe_resp, sdata);
569

570
	new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
571 572 573
	if (!new)
		return -ENOMEM;

574 575
	new->len = resp_len;
	memcpy(new->data, resp, resp_len);
576

577 578 579 580 581
	if (csa)
		memcpy(new->csa_counter_offsets, csa->counter_offsets_presp,
		       csa->n_counter_offsets_presp *
		       sizeof(new->csa_counter_offsets[0]));

582
	rcu_assign_pointer(sdata->u.ap.probe_resp, new);
583 584
	if (old)
		kfree_rcu(old, rcu_head);
585 586 587 588

	return 0;
}

589
static int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
590 591
				   struct cfg80211_beacon_data *params,
				   const struct ieee80211_csa_settings *csa)
592 593 594
{
	struct beacon_data *new, *old;
	int new_head_len, new_tail_len;
595 596
	int size, err;
	u32 changed = BSS_CHANGED_BEACON;
597

598 599
	old = sdata_dereference(sdata->u.ap.beacon, sdata);

600 601 602

	/* Need to have a beacon head if we don't have one yet */
	if (!params->head && !old)
603
		return -EINVAL;
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

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

635 636 637 638 639 640 641
	if (csa) {
		new->csa_current_counter = csa->count;
		memcpy(new->csa_counter_offsets, csa->counter_offsets_beacon,
		       csa->n_counter_offsets_beacon *
		       sizeof(new->csa_counter_offsets[0]));
	}

642 643 644 645 646 647 648 649 650 651 652 653 654
	/* 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);

655
	err = ieee80211_set_probe_resp(sdata, params->probe_resp,
656
				       params->probe_resp_len, csa);
657 658 659
	if (err < 0)
		return err;
	if (err == 0)
660 661
		changed |= BSS_CHANGED_AP_PROBE_RESP;

662 663 664 665
	rcu_assign_pointer(sdata->u.ap.beacon, new);

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

667
	return changed;
668 669
}

670 671
static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
			      struct cfg80211_ap_settings *params)
672
{
673
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
674
	struct ieee80211_local *local = sdata->local;
675
	struct beacon_data *old;
676
	struct ieee80211_sub_if_data *vlan;
677 678 679
	u32 changed = BSS_CHANGED_BEACON_INT |
		      BSS_CHANGED_BEACON_ENABLED |
		      BSS_CHANGED_BEACON |
680 681
		      BSS_CHANGED_SSID |
		      BSS_CHANGED_P2P_PS;
682
	int err;
683

684
	old = sdata_dereference(sdata->u.ap.beacon, sdata);
685 686 687
	if (old)
		return -EALREADY;

688 689 690 691 692 693 694 695 696 697 698 699 700
	switch (params->smps_mode) {
	case NL80211_SMPS_OFF:
		sdata->smps_mode = IEEE80211_SMPS_OFF;
		break;
	case NL80211_SMPS_STATIC:
		sdata->smps_mode = IEEE80211_SMPS_STATIC;
		break;
	case NL80211_SMPS_DYNAMIC:
		sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
		break;
	default:
		return -EINVAL;
	}
701 702
	sdata->needed_rx_chains = sdata->local->rx_chains;

703
	mutex_lock(&local->mtx);
704
	err = ieee80211_vif_use_channel(sdata, &params->chandef,
Johannes Berg's avatar
Johannes Berg committed
705
					IEEE80211_CHANCTX_SHARED);
706 707
	if (!err)
		ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
708
	mutex_unlock(&local->mtx);
709 710 711
	if (err)
		return err;

712 713 714 715 716 717
	/*
	 * Apply control port protocol, this allows us to
	 * not encrypt dynamic WEP control frames.
	 */
	sdata->control_port_protocol = params->crypto.control_port_ethertype;
	sdata->control_port_no_encrypt = params->crypto.control_port_no_encrypt;
718 719 720 721
	sdata->encrypt_headroom = ieee80211_cs_headroom(sdata->local,
							&params->crypto,
							sdata->vif.type);

722 723 724 725 726
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
		vlan->control_port_protocol =
			params->crypto.control_port_ethertype;
		vlan->control_port_no_encrypt =
			params->crypto.control_port_no_encrypt;
727 728 729 730
		vlan->encrypt_headroom =
			ieee80211_cs_headroom(sdata->local,
					      &params->crypto,
					      vlan->vif.type);
731 732
	}

733 734
	sdata->vif.bss_conf.beacon_int = params->beacon_interval;
	sdata->vif.bss_conf.dtim_period = params->dtim_period;
735
	sdata->vif.bss_conf.enable_beacon = true;
736 737 738 739 740 741 742 743

	sdata->vif.bss_conf.ssid_len = params->ssid_len;
	if (params->ssid_len)
		memcpy(sdata->vif.bss_conf.ssid, params->ssid,
		       params->ssid_len);
	sdata->vif.bss_conf.hidden_ssid =
		(params->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE);

744 745 746 747 748 749 750
	memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
	       sizeof(sdata->vif.bss_conf.p2p_noa_attr));
	sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow =
		params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
	if (params->p2p_opp_ps)
		sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
					IEEE80211_P2P_OPPPS_ENABLE_BIT;
751

752
	err = ieee80211_assign_beacon(sdata, &params->beacon, NULL);
753 754
	if (err < 0) {
		ieee80211_vif_release_channel(sdata);
755
		return err;
756
	}
757 758
	changed |= err;

759 760
	err = drv_start_ap(sdata->local, sdata);
	if (err) {
761 762
		old = sdata_dereference(sdata->u.ap.beacon, sdata);

763 764 765
		if (old)
			kfree_rcu(old, rcu_head);
		RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
766
		ieee80211_vif_release_channel(sdata);
767 768 769
		return err;
	}

770
	ieee80211_recalc_dtim(local, sdata);
771 772
	ieee80211_bss_info_change_notify(sdata, changed);

773 774 775 776
	netif_carrier_on(dev);
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
		netif_carrier_on(vlan->dev);

777
	return 0;
778 779
}

780 781
static int ieee80211_change_beacon(struct wiphy *wiphy, struct net_device *dev,
				   struct cfg80211_beacon_data *params)
782
{
783
	struct ieee80211_sub_if_data *sdata;
784
	struct beacon_data *old;
785
	int err;
786

787
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
788
	sdata_assert_lock(sdata);
789

790 791 792 793 794 795
	/* don't allow changing the beacon while CSA is in place - offset
	 * of channel switch counter may change
	 */
	if (sdata->vif.csa_active)
		return -EBUSY;

796
	old = sdata_dereference(sdata->u.ap.beacon, sdata);
797 798 799
	if (!old)
		return -ENOENT;

800
	err = ieee80211_assign_beacon(sdata, params, NULL);
801 802 803 804
	if (err < 0)
		return err;
	ieee80211_bss_info_change_notify(sdata, err);
	return 0;
805 806
}

807
static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
808
{
809 810 811 812 813
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct ieee80211_sub_if_data *vlan;
	struct ieee80211_local *local = sdata->local;
	struct beacon_data *old_beacon;
	struct probe_resp *old_probe_resp;
814
	struct cfg80211_chan_def chandef;
815

816 817
	sdata_assert_lock(sdata);

818
	old_beacon = sdata_dereference(sdata->u.ap.beacon, sdata);
819
	if (!old_beacon)
820
		return -ENOENT;
821
	old_probe_resp = sdata_dereference(sdata->u.ap.probe_resp, sdata);
822

823
	/* abort any running channel switch */
824
	mutex_lock(&local->mtx);
825
	sdata->vif.csa_active = false;
826 827 828 829 830 831
	if (sdata->csa_block_tx) {
		ieee80211_wake_vif_queues(local, sdata,
					  IEEE80211_QUEUE_STOP_REASON_CSA);
		sdata->csa_block_tx = false;
	}

832 833
	mutex_unlock(&local->mtx);

834 835 836
	kfree(sdata->u.ap.next_beacon);
	sdata->u.ap.next_beacon = NULL;

837
	/* turn off carrier for this interface and dependent VLANs */
838 839 840 841
	list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
		netif_carrier_off(vlan->dev);
	netif_carrier_off(dev);

842
	/* remove beacon and probe response */
843
	RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
844 845 846 847
	RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
	kfree_rcu(old_beacon, rcu_head);
	if (old_probe_resp)
		kfree_rcu(old_probe_resp, rcu_head);
848
	sdata->u.ap.driver_smps_mode = IEEE80211_SMPS_OFF;