Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * This file contains helper code to handle channel
   4 * settings and keeping track of what is possible at
   5 * any point in time.
   6 *
   7 * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
   8 * Copyright 2013-2014  Intel Mobile Communications GmbH
   9 * Copyright 2018-2024	Intel Corporation
  10 */
  11
  12#include <linux/export.h>
  13#include <linux/bitfield.h>
  14#include <net/cfg80211.h>
  15#include "core.h"
  16#include "rdev-ops.h"
  17
  18static bool cfg80211_valid_60g_freq(u32 freq)
  19{
  20	return freq >= 58320 && freq <= 70200;
  21}
  22
  23void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
  24			     struct ieee80211_channel *chan,
  25			     enum nl80211_channel_type chan_type)
  26{
  27	if (WARN_ON(!chan))
  28		return;
  29
  30	*chandef = (struct cfg80211_chan_def) {
  31		.chan = chan,
  32		.freq1_offset = chan->freq_offset,
  33	};
  34
  35	switch (chan_type) {
  36	case NL80211_CHAN_NO_HT:
  37		chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  38		chandef->center_freq1 = chan->center_freq;
  39		break;
  40	case NL80211_CHAN_HT20:
  41		chandef->width = NL80211_CHAN_WIDTH_20;
  42		chandef->center_freq1 = chan->center_freq;
  43		break;
  44	case NL80211_CHAN_HT40PLUS:
  45		chandef->width = NL80211_CHAN_WIDTH_40;
  46		chandef->center_freq1 = chan->center_freq + 10;
  47		break;
  48	case NL80211_CHAN_HT40MINUS:
  49		chandef->width = NL80211_CHAN_WIDTH_40;
  50		chandef->center_freq1 = chan->center_freq - 10;
  51		break;
  52	default:
  53		WARN_ON(1);
  54	}
  55}
  56EXPORT_SYMBOL(cfg80211_chandef_create);
  57
  58struct cfg80211_per_bw_puncturing_values {
  59	u8 len;
  60	const u16 *valid_values;
  61};
  62
  63static const u16 puncturing_values_80mhz[] = {
  64	0x8, 0x4, 0x2, 0x1
  65};
  66
  67static const u16 puncturing_values_160mhz[] = {
  68	 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
  69};
  70
  71static const u16 puncturing_values_320mhz[] = {
  72	0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00,
  73	0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f,
  74	0x300f, 0xc0f, 0x30f, 0xcf, 0x3f
  75};
  76
  77#define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \
  78	{ \
  79		.len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \
  80		.valid_values = puncturing_values_ ## _bw ## mhz \
  81	}
  82
  83static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = {
  84	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80),
  85	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160),
  86	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320)
  87};
  88
  89static bool valid_puncturing_bitmap(const struct cfg80211_chan_def *chandef)
  90{
  91	u32 idx, i, start_freq, primary_center = chandef->chan->center_freq;
  92
  93	switch (chandef->width) {
  94	case NL80211_CHAN_WIDTH_80:
  95		idx = 0;
  96		start_freq = chandef->center_freq1 - 40;
  97		break;
  98	case NL80211_CHAN_WIDTH_160:
  99		idx = 1;
 100		start_freq = chandef->center_freq1 - 80;
 101		break;
 102	case NL80211_CHAN_WIDTH_320:
 103		idx = 2;
 104		start_freq = chandef->center_freq1 - 160;
 105		break;
 106	default:
 107		return chandef->punctured == 0;
 108	}
 109
 110	if (!chandef->punctured)
 111		return true;
 112
 113	/* check if primary channel is punctured */
 114	if (chandef->punctured & (u16)BIT((primary_center - start_freq) / 20))
 115		return false;
 116
 117	for (i = 0; i < per_bw_puncturing[idx].len; i++) {
 118		if (per_bw_puncturing[idx].valid_values[i] == chandef->punctured)
 119			return true;
 120	}
 121
 122	return false;
 123}
 124
 125static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
 126{
 127	int max_contiguous = 0;
 128	int num_of_enabled = 0;
 129	int contiguous = 0;
 130	int i;
 131
 132	if (!chandef->edmg.channels || !chandef->edmg.bw_config)
 133		return false;
 134
 135	if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
 136		return false;
 137
 138	for (i = 0; i < 6; i++) {
 139		if (chandef->edmg.channels & BIT(i)) {
 140			contiguous++;
 141			num_of_enabled++;
 142		} else {
 143			contiguous = 0;
 144		}
 145
 146		max_contiguous = max(contiguous, max_contiguous);
 147	}
 148	/* basic verification of edmg configuration according to
 149	 * IEEE P802.11ay/D4.0 section 9.4.2.251
 150	 */
 151	/* check bw_config against contiguous edmg channels */
 152	switch (chandef->edmg.bw_config) {
 153	case IEEE80211_EDMG_BW_CONFIG_4:
 154	case IEEE80211_EDMG_BW_CONFIG_8:
 155	case IEEE80211_EDMG_BW_CONFIG_12:
 156		if (max_contiguous < 1)
 157			return false;
 158		break;
 159	case IEEE80211_EDMG_BW_CONFIG_5:
 160	case IEEE80211_EDMG_BW_CONFIG_9:
 161	case IEEE80211_EDMG_BW_CONFIG_13:
 162		if (max_contiguous < 2)
 163			return false;
 164		break;
 165	case IEEE80211_EDMG_BW_CONFIG_6:
 166	case IEEE80211_EDMG_BW_CONFIG_10:
 167	case IEEE80211_EDMG_BW_CONFIG_14:
 168		if (max_contiguous < 3)
 169			return false;
 170		break;
 171	case IEEE80211_EDMG_BW_CONFIG_7:
 172	case IEEE80211_EDMG_BW_CONFIG_11:
 173	case IEEE80211_EDMG_BW_CONFIG_15:
 174		if (max_contiguous < 4)
 175			return false;
 176		break;
 177
 178	default:
 179		return false;
 180	}
 181
 182	/* check bw_config against aggregated (non contiguous) edmg channels */
 183	switch (chandef->edmg.bw_config) {
 184	case IEEE80211_EDMG_BW_CONFIG_4:
 185	case IEEE80211_EDMG_BW_CONFIG_5:
 186	case IEEE80211_EDMG_BW_CONFIG_6:
 187	case IEEE80211_EDMG_BW_CONFIG_7:
 188		break;
 189	case IEEE80211_EDMG_BW_CONFIG_8:
 190	case IEEE80211_EDMG_BW_CONFIG_9:
 191	case IEEE80211_EDMG_BW_CONFIG_10:
 192	case IEEE80211_EDMG_BW_CONFIG_11:
 193		if (num_of_enabled < 2)
 194			return false;
 195		break;
 196	case IEEE80211_EDMG_BW_CONFIG_12:
 197	case IEEE80211_EDMG_BW_CONFIG_13:
 198	case IEEE80211_EDMG_BW_CONFIG_14:
 199	case IEEE80211_EDMG_BW_CONFIG_15:
 200		if (num_of_enabled < 4 || max_contiguous < 2)
 201			return false;
 202		break;
 203	default:
 204		return false;
 205	}
 206
 207	return true;
 208}
 209
 210int nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width)
 211{
 212	int mhz;
 213
 214	switch (chan_width) {
 215	case NL80211_CHAN_WIDTH_1:
 216		mhz = 1;
 217		break;
 218	case NL80211_CHAN_WIDTH_2:
 219		mhz = 2;
 220		break;
 221	case NL80211_CHAN_WIDTH_4:
 222		mhz = 4;
 223		break;
 224	case NL80211_CHAN_WIDTH_8:
 225		mhz = 8;
 226		break;
 227	case NL80211_CHAN_WIDTH_16:
 228		mhz = 16;
 229		break;
 230	case NL80211_CHAN_WIDTH_5:
 231		mhz = 5;
 232		break;
 233	case NL80211_CHAN_WIDTH_10:
 234		mhz = 10;
 235		break;
 236	case NL80211_CHAN_WIDTH_20:
 237	case NL80211_CHAN_WIDTH_20_NOHT:
 238		mhz = 20;
 239		break;
 240	case NL80211_CHAN_WIDTH_40:
 241		mhz = 40;
 242		break;
 243	case NL80211_CHAN_WIDTH_80P80:
 244	case NL80211_CHAN_WIDTH_80:
 245		mhz = 80;
 246		break;
 247	case NL80211_CHAN_WIDTH_160:
 248		mhz = 160;
 249		break;
 250	case NL80211_CHAN_WIDTH_320:
 251		mhz = 320;
 252		break;
 253	default:
 254		WARN_ON_ONCE(1);
 255		return -1;
 256	}
 257	return mhz;
 258}
 259EXPORT_SYMBOL(nl80211_chan_width_to_mhz);
 260
 261static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
 262{
 263	return nl80211_chan_width_to_mhz(c->width);
 264}
 265
 266static bool cfg80211_valid_center_freq(u32 center,
 267				       enum nl80211_chan_width width)
 268{
 269	int bw;
 270	int step;
 271
 272	/* We only do strict verification on 6 GHz */
 273	if (center < 5955 || center > 7115)
 274		return true;
 275
 276	bw = nl80211_chan_width_to_mhz(width);
 277	if (bw < 0)
 278		return false;
 279
 280	/* Validate that the channels bw is entirely within the 6 GHz band */
 281	if (center - bw / 2 < 5945 || center + bw / 2 > 7125)
 282		return false;
 283
 284	/* With 320 MHz the permitted channels overlap */
 285	if (bw == 320)
 286		step = 160;
 287	else
 288		step = bw;
 289
 290	/*
 291	 * Valid channels are packed from lowest frequency towards higher ones.
 292	 * So test that the lower frequency aligns with one of these steps.
 293	 */
 294	return (center - bw / 2 - 5945) % step == 0;
 295}
 296
 297bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
 298{
 299	u32 control_freq, oper_freq;
 300	int oper_width, control_width;
 301
 302	if (!chandef->chan)
 303		return false;
 304
 305	if (chandef->freq1_offset >= 1000)
 306		return false;
 307
 308	control_freq = chandef->chan->center_freq;
 309
 310	switch (chandef->width) {
 311	case NL80211_CHAN_WIDTH_5:
 312	case NL80211_CHAN_WIDTH_10:
 313	case NL80211_CHAN_WIDTH_20:
 314	case NL80211_CHAN_WIDTH_20_NOHT:
 315		if (ieee80211_chandef_to_khz(chandef) !=
 316		    ieee80211_channel_to_khz(chandef->chan))
 317			return false;
 318		if (chandef->center_freq2)
 319			return false;
 320		break;
 321	case NL80211_CHAN_WIDTH_1:
 322	case NL80211_CHAN_WIDTH_2:
 323	case NL80211_CHAN_WIDTH_4:
 324	case NL80211_CHAN_WIDTH_8:
 325	case NL80211_CHAN_WIDTH_16:
 326		if (chandef->chan->band != NL80211_BAND_S1GHZ)
 327			return false;
 328
 329		control_freq = ieee80211_channel_to_khz(chandef->chan);
 330		oper_freq = ieee80211_chandef_to_khz(chandef);
 331		control_width = nl80211_chan_width_to_mhz(
 332					ieee80211_s1g_channel_width(
 333								chandef->chan));
 334		oper_width = cfg80211_chandef_get_width(chandef);
 335
 336		if (oper_width < 0 || control_width < 0)
 337			return false;
 338		if (chandef->center_freq2)
 339			return false;
 340
 341		if (control_freq + MHZ_TO_KHZ(control_width) / 2 >
 342		    oper_freq + MHZ_TO_KHZ(oper_width) / 2)
 343			return false;
 344
 345		if (control_freq - MHZ_TO_KHZ(control_width) / 2 <
 346		    oper_freq - MHZ_TO_KHZ(oper_width) / 2)
 347			return false;
 348		break;
 349	case NL80211_CHAN_WIDTH_80P80:
 
 
 
 
 
 350		if (!chandef->center_freq2)
 351			return false;
 352		/* adjacent is not allowed -- that's a 160 MHz channel */
 353		if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
 354		    chandef->center_freq2 - chandef->center_freq1 == 80)
 355			return false;
 356		break;
 357	default:
 
 
 
 
 
 358		if (chandef->center_freq2)
 359			return false;
 360		break;
 361	}
 362
 363	switch (chandef->width) {
 364	case NL80211_CHAN_WIDTH_5:
 365	case NL80211_CHAN_WIDTH_10:
 366	case NL80211_CHAN_WIDTH_20:
 367	case NL80211_CHAN_WIDTH_20_NOHT:
 368	case NL80211_CHAN_WIDTH_1:
 369	case NL80211_CHAN_WIDTH_2:
 370	case NL80211_CHAN_WIDTH_4:
 371	case NL80211_CHAN_WIDTH_8:
 372	case NL80211_CHAN_WIDTH_16:
 373		/* all checked above */
 374		break;
 375	case NL80211_CHAN_WIDTH_320:
 376		if (chandef->center_freq1 == control_freq + 150 ||
 377		    chandef->center_freq1 == control_freq + 130 ||
 378		    chandef->center_freq1 == control_freq + 110 ||
 379		    chandef->center_freq1 == control_freq + 90 ||
 380		    chandef->center_freq1 == control_freq - 90 ||
 381		    chandef->center_freq1 == control_freq - 110 ||
 382		    chandef->center_freq1 == control_freq - 130 ||
 383		    chandef->center_freq1 == control_freq - 150)
 384			break;
 385		fallthrough;
 386	case NL80211_CHAN_WIDTH_160:
 387		if (chandef->center_freq1 == control_freq + 70 ||
 388		    chandef->center_freq1 == control_freq + 50 ||
 389		    chandef->center_freq1 == control_freq - 50 ||
 390		    chandef->center_freq1 == control_freq - 70)
 391			break;
 392		fallthrough;
 393	case NL80211_CHAN_WIDTH_80P80:
 394	case NL80211_CHAN_WIDTH_80:
 395		if (chandef->center_freq1 == control_freq + 30 ||
 396		    chandef->center_freq1 == control_freq - 30)
 397			break;
 398		fallthrough;
 399	case NL80211_CHAN_WIDTH_40:
 400		if (chandef->center_freq1 == control_freq + 10 ||
 401		    chandef->center_freq1 == control_freq - 10)
 402			break;
 403		fallthrough;
 404	default:
 405		return false;
 406	}
 407
 408	if (!cfg80211_valid_center_freq(chandef->center_freq1, chandef->width))
 409		return false;
 410
 411	if (chandef->width == NL80211_CHAN_WIDTH_80P80 &&
 412	    !cfg80211_valid_center_freq(chandef->center_freq2, chandef->width))
 413		return false;
 414
 415	/* channel 14 is only for IEEE 802.11b */
 416	if (chandef->center_freq1 == 2484 &&
 417	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
 418		return false;
 419
 420	if (cfg80211_chandef_is_edmg(chandef) &&
 421	    !cfg80211_edmg_chandef_valid(chandef))
 422		return false;
 423
 424	return valid_puncturing_bitmap(chandef);
 425}
 426EXPORT_SYMBOL(cfg80211_chandef_valid);
 427
 428int cfg80211_chandef_primary(const struct cfg80211_chan_def *c,
 429			     enum nl80211_chan_width primary_chan_width,
 430			     u16 *punctured)
 431{
 432	int pri_width = nl80211_chan_width_to_mhz(primary_chan_width);
 433	int width = cfg80211_chandef_get_width(c);
 434	u32 control = c->chan->center_freq;
 435	u32 center = c->center_freq1;
 436	u16 _punct = 0;
 437
 438	if (WARN_ON_ONCE(pri_width < 0 || width < 0))
 439		return -1;
 440
 441	/* not intended to be called this way, can't determine */
 442	if (WARN_ON_ONCE(pri_width > width))
 443		return -1;
 444
 445	if (!punctured)
 446		punctured = &_punct;
 447
 448	*punctured = c->punctured;
 449
 450	while (width > pri_width) {
 451		unsigned int bits_to_drop = width / 20 / 2;
 452
 453		if (control > center) {
 454			center += width / 4;
 455			*punctured >>= bits_to_drop;
 456		} else {
 457			center -= width / 4;
 458			*punctured &= (1 << bits_to_drop) - 1;
 459		}
 460		width /= 2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 461	}
 462
 463	return center;
 464}
 465EXPORT_SYMBOL(cfg80211_chandef_primary);
 466
 467static const struct cfg80211_chan_def *
 468check_chandef_primary_compat(const struct cfg80211_chan_def *c1,
 469			     const struct cfg80211_chan_def *c2,
 470			     enum nl80211_chan_width primary_chan_width)
 471{
 472	u16 punct_c1 = 0, punct_c2 = 0;
 473
 474	/* check primary is compatible -> error if not */
 475	if (cfg80211_chandef_primary(c1, primary_chan_width, &punct_c1) !=
 476	    cfg80211_chandef_primary(c2, primary_chan_width, &punct_c2))
 477		return ERR_PTR(-EINVAL);
 478
 479	if (punct_c1 != punct_c2)
 480		return ERR_PTR(-EINVAL);
 481
 482	/* assumes c1 is smaller width, if that was just checked -> done */
 483	if (c1->width == primary_chan_width)
 484		return c2;
 485
 486	/* otherwise continue checking the next width */
 487	return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 488}
 489
 490static const struct cfg80211_chan_def *
 491_cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
 492			     const struct cfg80211_chan_def *c2)
 493{
 494	const struct cfg80211_chan_def *ret;
 495
 496	/* If they are identical, return */
 497	if (cfg80211_chandef_identical(c1, c2))
 498		return c2;
 499
 500	/* otherwise, must have same control channel */
 501	if (c1->chan != c2->chan)
 502		return NULL;
 503
 504	/*
 505	 * If they have the same width, but aren't identical,
 506	 * then they can't be compatible.
 507	 */
 508	if (c1->width == c2->width)
 509		return NULL;
 510
 511	/*
 512	 * can't be compatible if one of them is 5/10 MHz or S1G
 513	 * but they don't have the same width.
 514	 */
 515#define NARROW_OR_S1G(width)	((width) == NL80211_CHAN_WIDTH_5 || \
 516				 (width) == NL80211_CHAN_WIDTH_10 || \
 517				 (width) == NL80211_CHAN_WIDTH_1 || \
 518				 (width) == NL80211_CHAN_WIDTH_2 || \
 519				 (width) == NL80211_CHAN_WIDTH_4 || \
 520				 (width) == NL80211_CHAN_WIDTH_8 || \
 521				 (width) == NL80211_CHAN_WIDTH_16)
 522
 523	if (NARROW_OR_S1G(c1->width) || NARROW_OR_S1G(c2->width))
 524		return NULL;
 525
 526	/*
 527	 * Make sure that c1 is always the narrower one, so that later
 528	 * we either return NULL or c2 and don't have to check both
 529	 * directions.
 530	 */
 531	if (c1->width > c2->width)
 532		swap(c1, c2);
 533
 534	/*
 535	 * No further checks needed if the "narrower" one is only 20 MHz.
 536	 * Here "narrower" includes being a 20 MHz non-HT channel vs. a
 537	 * 20 MHz HT (or later) one.
 538	 */
 539	if (c1->width <= NL80211_CHAN_WIDTH_20)
 540		return c2;
 541
 542	ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_40);
 543	if (ret)
 544		return ret;
 545
 546	ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_80);
 547	if (ret)
 548		return ret;
 549
 550	/*
 551	 * If c1 is 80+80, then c2 is 160 or higher, but that cannot
 552	 * match. If c2 was also 80+80 it was already either accepted
 553	 * or rejected above (identical or not, respectively.)
 554	 */
 555	if (c1->width == NL80211_CHAN_WIDTH_80P80)
 556		return NULL;
 557
 558	ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_160);
 559	if (ret)
 560		return ret;
 561
 562	/*
 563	 * Getting here would mean they're both wider than 160, have the
 564	 * same primary 160, but are not identical - this cannot happen
 565	 * since they must be 320 (no wider chandefs exist, at least yet.)
 566	 */
 567	WARN_ON_ONCE(1);
 568
 569	return NULL;
 570}
 571
 572const struct cfg80211_chan_def *
 573cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
 574			    const struct cfg80211_chan_def *c2)
 575{
 576	const struct cfg80211_chan_def *ret;
 577
 578	ret = _cfg80211_chandef_compatible(c1, c2);
 579	if (IS_ERR(ret))
 580		return NULL;
 581	return ret;
 
 
 
 582}
 583EXPORT_SYMBOL(cfg80211_chandef_compatible);
 584
 585static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
 586					 u32 bandwidth,
 587					 enum nl80211_dfs_state dfs_state)
 588{
 589	struct ieee80211_channel *c;
 590	u32 freq;
 591
 592	for (freq = center_freq - bandwidth/2 + 10;
 593	     freq <= center_freq + bandwidth/2 - 10;
 594	     freq += 20) {
 595		c = ieee80211_get_channel(wiphy, freq);
 596		if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
 597			continue;
 598
 599		c->dfs_state = dfs_state;
 600		c->dfs_state_entered = jiffies;
 601	}
 602}
 603
 604void cfg80211_set_dfs_state(struct wiphy *wiphy,
 605			    const struct cfg80211_chan_def *chandef,
 606			    enum nl80211_dfs_state dfs_state)
 607{
 608	int width;
 609
 610	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 611		return;
 612
 613	width = cfg80211_chandef_get_width(chandef);
 614	if (width < 0)
 615		return;
 616
 617	cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
 618				     width, dfs_state);
 619
 620	if (!chandef->center_freq2)
 621		return;
 622	cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
 623				     width, dfs_state);
 624}
 625
 626static u32 cfg80211_get_start_freq(u32 center_freq,
 627				   u32 bandwidth)
 628{
 629	u32 start_freq;
 630
 631	bandwidth = MHZ_TO_KHZ(bandwidth);
 632	if (bandwidth <= MHZ_TO_KHZ(20))
 633		start_freq = center_freq;
 634	else
 635		start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
 636
 637	return start_freq;
 638}
 639
 640static u32 cfg80211_get_end_freq(u32 center_freq,
 641				 u32 bandwidth)
 642{
 643	u32 end_freq;
 644
 645	bandwidth = MHZ_TO_KHZ(bandwidth);
 646	if (bandwidth <= MHZ_TO_KHZ(20))
 647		end_freq = center_freq;
 648	else
 649		end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
 650
 651	return end_freq;
 652}
 653
 654static bool
 655cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device *rdev,
 656				   enum nl80211_iftype iftype,
 657				   struct wireless_dev *wdev,
 658				   struct ieee80211_channel *chan)
 659{
 660	unsigned int link_id;
 661
 662	for_each_valid_link(wdev, link_id) {
 663		struct ieee80211_channel *other_chan = NULL;
 664		struct cfg80211_chan_def chandef = {};
 665		int ret;
 666
 667		/* In order to avoid daisy chaining only allow BSS STA */
 668		if (wdev->iftype != NL80211_IFTYPE_STATION ||
 669		    !wdev->links[link_id].client.current_bss)
 670			continue;
 671
 672		other_chan =
 673			wdev->links[link_id].client.current_bss->pub.channel;
 674
 675		if (!other_chan)
 676			continue;
 677
 678		if (chan == other_chan)
 679			return true;
 680
 681		/* continue if we can't get the channel */
 682		ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
 683		if (ret)
 684			continue;
 685
 686		if (cfg80211_is_sub_chan(&chandef, chan, false))
 687			return true;
 688	}
 689
 690	return false;
 691}
 692
 693/*
 694 * Check if P2P GO is allowed to operate on a DFS channel
 695 */
 696static bool cfg80211_dfs_permissive_chan(struct wiphy *wiphy,
 697					 enum nl80211_iftype iftype,
 698					 struct ieee80211_channel *chan)
 699{
 700	struct wireless_dev *wdev;
 701	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
 702
 703	lockdep_assert_held(&rdev->wiphy.mtx);
 704
 705	if (!wiphy_ext_feature_isset(&rdev->wiphy,
 706				     NL80211_EXT_FEATURE_DFS_CONCURRENT) ||
 707	    !(chan->flags & IEEE80211_CHAN_DFS_CONCURRENT))
 708		return false;
 709
 710	/* only valid for P2P GO */
 711	if (iftype != NL80211_IFTYPE_P2P_GO)
 712		return false;
 713
 714	/*
 715	 * Allow only if there's a concurrent BSS
 716	 */
 717	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
 718		bool ret = cfg80211_dfs_permissive_check_wdev(rdev, iftype,
 719							      wdev, chan);
 720		if (ret)
 721			return ret;
 722	}
 723
 724	return false;
 725}
 726
 727static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
 728					    u32 center_freq,
 729					    u32 bandwidth,
 730					    enum nl80211_iftype iftype)
 731{
 732	struct ieee80211_channel *c;
 733	u32 freq, start_freq, end_freq;
 734
 735	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
 736	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
 737
 738	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
 739		c = ieee80211_get_channel_khz(wiphy, freq);
 740		if (!c)
 741			return -EINVAL;
 742
 743		if (c->flags & IEEE80211_CHAN_RADAR &&
 744		    !cfg80211_dfs_permissive_chan(wiphy, iftype, c))
 745			return 1;
 746	}
 747
 748	return 0;
 749}
 750
 751
 752int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
 753				  const struct cfg80211_chan_def *chandef,
 754				  enum nl80211_iftype iftype)
 755{
 756	int width;
 757	int ret;
 758
 759	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 760		return -EINVAL;
 761
 762	switch (iftype) {
 763	case NL80211_IFTYPE_ADHOC:
 764	case NL80211_IFTYPE_AP:
 765	case NL80211_IFTYPE_P2P_GO:
 766	case NL80211_IFTYPE_MESH_POINT:
 767		width = cfg80211_chandef_get_width(chandef);
 768		if (width < 0)
 769			return -EINVAL;
 770
 771		ret = cfg80211_get_chans_dfs_required(wiphy,
 772					ieee80211_chandef_to_khz(chandef),
 773					width, iftype);
 774		if (ret < 0)
 775			return ret;
 776		else if (ret > 0)
 777			return BIT(chandef->width);
 778
 779		if (!chandef->center_freq2)
 780			return 0;
 781
 782		ret = cfg80211_get_chans_dfs_required(wiphy,
 783					MHZ_TO_KHZ(chandef->center_freq2),
 784					width, iftype);
 785		if (ret < 0)
 786			return ret;
 787		else if (ret > 0)
 788			return BIT(chandef->width);
 789
 790		break;
 791	case NL80211_IFTYPE_STATION:
 792	case NL80211_IFTYPE_OCB:
 793	case NL80211_IFTYPE_P2P_CLIENT:
 794	case NL80211_IFTYPE_MONITOR:
 795	case NL80211_IFTYPE_AP_VLAN:
 
 796	case NL80211_IFTYPE_P2P_DEVICE:
 797	case NL80211_IFTYPE_NAN:
 798		break;
 799	case NL80211_IFTYPE_WDS:
 800	case NL80211_IFTYPE_UNSPECIFIED:
 801	case NUM_NL80211_IFTYPES:
 802		WARN_ON(1);
 803	}
 804
 805	return 0;
 806}
 807EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
 808
 809static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
 810					 u32 center_freq,
 811					 u32 bandwidth)
 812{
 813	struct ieee80211_channel *c;
 814	u32 freq, start_freq, end_freq;
 815	int count = 0;
 816
 817	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
 818	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
 819
 820	/*
 821	 * Check entire range of channels for the bandwidth.
 822	 * Check all channels are DFS channels (DFS_USABLE or
 823	 * DFS_AVAILABLE). Return number of usable channels
 824	 * (require CAC). Allow DFS and non-DFS channel mix.
 825	 */
 826	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
 827		c = ieee80211_get_channel_khz(wiphy, freq);
 828		if (!c)
 829			return -EINVAL;
 830
 831		if (c->flags & IEEE80211_CHAN_DISABLED)
 832			return -EINVAL;
 833
 834		if (c->flags & IEEE80211_CHAN_RADAR) {
 835			if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
 836				return -EINVAL;
 837
 838			if (c->dfs_state == NL80211_DFS_USABLE)
 839				count++;
 840		}
 841	}
 842
 843	return count;
 844}
 845
 846bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
 847				 const struct cfg80211_chan_def *chandef)
 848{
 849	int width;
 850	int r1, r2 = 0;
 851
 852	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 853		return false;
 854
 855	width = cfg80211_chandef_get_width(chandef);
 856	if (width < 0)
 857		return false;
 858
 859	r1 = cfg80211_get_chans_dfs_usable(wiphy,
 860					   MHZ_TO_KHZ(chandef->center_freq1),
 861					   width);
 862
 863	if (r1 < 0)
 864		return false;
 865
 866	switch (chandef->width) {
 867	case NL80211_CHAN_WIDTH_80P80:
 868		WARN_ON(!chandef->center_freq2);
 869		r2 = cfg80211_get_chans_dfs_usable(wiphy,
 870					MHZ_TO_KHZ(chandef->center_freq2),
 871					width);
 872		if (r2 < 0)
 873			return false;
 874		break;
 875	default:
 876		WARN_ON(chandef->center_freq2);
 877		break;
 878	}
 879
 880	return (r1 + r2 > 0);
 881}
 882EXPORT_SYMBOL(cfg80211_chandef_dfs_usable);
 883
 884/*
 885 * Checks if center frequency of chan falls with in the bandwidth
 886 * range of chandef.
 887 */
 888bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
 889			  struct ieee80211_channel *chan,
 890			  bool primary_only)
 891{
 892	int width;
 893	u32 freq;
 894
 895	if (!chandef->chan)
 896		return false;
 897
 898	if (chandef->chan->center_freq == chan->center_freq)
 899		return true;
 900
 901	if (primary_only)
 902		return false;
 903
 904	width = cfg80211_chandef_get_width(chandef);
 905	if (width <= 20)
 906		return false;
 907
 908	for (freq = chandef->center_freq1 - width / 2 + 10;
 909	     freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
 910		if (chan->center_freq == freq)
 911			return true;
 912	}
 913
 914	if (!chandef->center_freq2)
 915		return false;
 916
 917	for (freq = chandef->center_freq2 - width / 2 + 10;
 918	     freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
 919		if (chan->center_freq == freq)
 920			return true;
 921	}
 922
 923	return false;
 924}
 925
 926bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
 927{
 928	unsigned int link;
 929
 930	lockdep_assert_wiphy(wdev->wiphy);
 
 
 
 931
 932	switch (wdev->iftype) {
 933	case NL80211_IFTYPE_AP:
 934	case NL80211_IFTYPE_P2P_GO:
 935		for_each_valid_link(wdev, link) {
 936			if (wdev->links[link].ap.beacon_interval)
 937				return true;
 938		}
 939		break;
 940	case NL80211_IFTYPE_ADHOC:
 941		if (wdev->u.ibss.ssid_len)
 942			return true;
 943		break;
 944	case NL80211_IFTYPE_MESH_POINT:
 945		if (wdev->u.mesh.id_len)
 946			return true;
 947		break;
 948	case NL80211_IFTYPE_STATION:
 949	case NL80211_IFTYPE_OCB:
 950	case NL80211_IFTYPE_P2P_CLIENT:
 951	case NL80211_IFTYPE_MONITOR:
 952	case NL80211_IFTYPE_AP_VLAN:
 
 953	case NL80211_IFTYPE_P2P_DEVICE:
 954	/* Can NAN type be considered as beaconing interface? */
 955	case NL80211_IFTYPE_NAN:
 956		break;
 957	case NL80211_IFTYPE_UNSPECIFIED:
 958	case NL80211_IFTYPE_WDS:
 959	case NUM_NL80211_IFTYPES:
 960		WARN_ON(1);
 961	}
 962
 963	return false;
 964}
 965
 966bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev,
 967			       struct ieee80211_channel *chan,
 968			       bool primary_only)
 969{
 970	unsigned int link;
 971
 972	switch (wdev->iftype) {
 973	case NL80211_IFTYPE_AP:
 974	case NL80211_IFTYPE_P2P_GO:
 975		for_each_valid_link(wdev, link) {
 976			if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef,
 977						 chan, primary_only))
 978				return true;
 979		}
 980		break;
 981	case NL80211_IFTYPE_ADHOC:
 982		return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan,
 983					    primary_only);
 984	case NL80211_IFTYPE_MESH_POINT:
 985		return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan,
 986					    primary_only);
 987	default:
 988		break;
 989	}
 990
 991	return false;
 992}
 993
 994static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
 995					struct ieee80211_channel *chan)
 996{
 997	struct wireless_dev *wdev;
 998
 999	lockdep_assert_wiphy(wiphy);
1000
1001	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1002		if (!cfg80211_beaconing_iface_active(wdev))
 
 
1003			continue;
 
1004
1005		if (cfg80211_wdev_on_sub_chan(wdev, chan, false))
 
1006			return true;
 
 
1007	}
1008
1009	return false;
1010}
1011
1012static bool
1013cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
1014				 struct ieee80211_channel *channel)
1015{
1016	if (!rdev->background_radar_wdev)
1017		return false;
1018
1019	if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
1020		return false;
1021
1022	return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
1023				    false);
1024}
1025
1026bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
1027				  struct ieee80211_channel *chan)
1028{
1029	struct cfg80211_registered_device *rdev;
1030
1031	ASSERT_RTNL();
1032
1033	if (!(chan->flags & IEEE80211_CHAN_RADAR))
1034		return false;
1035
1036	for_each_rdev(rdev) {
1037		bool found;
1038
1039		if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
1040			continue;
1041
1042		wiphy_lock(&rdev->wiphy);
1043		found = cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan) ||
1044			cfg80211_offchan_chain_is_active(rdev, chan);
1045		wiphy_unlock(&rdev->wiphy);
1046
1047		if (found)
1048			return true;
1049	}
1050
1051	return false;
1052}
1053
1054static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
1055					     u32 center_freq,
1056					     u32 bandwidth)
1057{
1058	struct ieee80211_channel *c;
1059	u32 freq, start_freq, end_freq;
1060	bool dfs_offload;
1061
1062	dfs_offload = wiphy_ext_feature_isset(wiphy,
1063					      NL80211_EXT_FEATURE_DFS_OFFLOAD);
1064
1065	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1066	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1067
1068	/*
1069	 * Check entire range of channels for the bandwidth.
1070	 * If any channel in between is disabled or has not
1071	 * had gone through CAC return false
1072	 */
1073	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1074		c = ieee80211_get_channel_khz(wiphy, freq);
1075		if (!c)
1076			return false;
1077
1078		if (c->flags & IEEE80211_CHAN_DISABLED)
1079			return false;
1080
1081		if ((c->flags & IEEE80211_CHAN_RADAR) &&
1082		    (c->dfs_state != NL80211_DFS_AVAILABLE) &&
1083		    !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
1084			return false;
1085	}
1086
1087	return true;
1088}
1089
1090static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
1091				const struct cfg80211_chan_def *chandef)
1092{
1093	int width;
1094	int r;
1095
1096	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1097		return false;
1098
1099	width = cfg80211_chandef_get_width(chandef);
1100	if (width < 0)
1101		return false;
1102
1103	r = cfg80211_get_chans_dfs_available(wiphy,
1104					     MHZ_TO_KHZ(chandef->center_freq1),
1105					     width);
1106
1107	/* If any of channels unavailable for cf1 just return */
1108	if (!r)
1109		return r;
1110
1111	switch (chandef->width) {
1112	case NL80211_CHAN_WIDTH_80P80:
1113		WARN_ON(!chandef->center_freq2);
1114		r = cfg80211_get_chans_dfs_available(wiphy,
1115					MHZ_TO_KHZ(chandef->center_freq2),
1116					width);
1117		break;
1118	default:
1119		WARN_ON(chandef->center_freq2);
1120		break;
1121	}
1122
1123	return r;
1124}
1125
1126static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
1127						    u32 center_freq,
1128						    u32 bandwidth)
1129{
1130	struct ieee80211_channel *c;
1131	u32 start_freq, end_freq, freq;
1132	unsigned int dfs_cac_ms = 0;
1133
1134	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1135	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1136
1137	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1138		c = ieee80211_get_channel_khz(wiphy, freq);
1139		if (!c)
1140			return 0;
1141
1142		if (c->flags & IEEE80211_CHAN_DISABLED)
1143			return 0;
1144
1145		if (!(c->flags & IEEE80211_CHAN_RADAR))
1146			continue;
1147
1148		if (c->dfs_cac_ms > dfs_cac_ms)
1149			dfs_cac_ms = c->dfs_cac_ms;
1150	}
1151
1152	return dfs_cac_ms;
1153}
1154
1155unsigned int
1156cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
1157			      const struct cfg80211_chan_def *chandef)
1158{
1159	int width;
1160	unsigned int t1 = 0, t2 = 0;
1161
1162	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1163		return 0;
1164
1165	width = cfg80211_chandef_get_width(chandef);
1166	if (width < 0)
1167		return 0;
1168
1169	t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
1170					     MHZ_TO_KHZ(chandef->center_freq1),
1171					     width);
1172
1173	if (!chandef->center_freq2)
1174		return t1;
1175
1176	t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
1177					     MHZ_TO_KHZ(chandef->center_freq2),
1178					     width);
1179
1180	return max(t1, t2);
1181}
1182EXPORT_SYMBOL(cfg80211_chandef_dfs_cac_time);
1183
1184static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
1185					u32 center_freq, u32 bandwidth,
1186					u32 prohibited_flags,
1187					u32 permitting_flags)
1188{
1189	struct ieee80211_channel *c;
1190	u32 freq, start_freq, end_freq;
1191
1192	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1193	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1194
1195	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1196		c = ieee80211_get_channel_khz(wiphy, freq);
1197		if (!c)
1198			return false;
1199		if (c->flags & permitting_flags)
1200			continue;
1201		if (c->flags & prohibited_flags)
1202			return false;
1203	}
1204
1205	return true;
1206}
1207
1208/* check if the operating channels are valid and supported */
1209static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
1210				 enum ieee80211_edmg_bw_config edmg_bw_config,
1211				 int primary_channel,
1212				 struct ieee80211_edmg *edmg_cap)
1213{
1214	struct ieee80211_channel *chan;
1215	int i, freq;
1216	int channels_counter = 0;
1217
1218	if (!edmg_channels && !edmg_bw_config)
1219		return true;
1220
1221	if ((!edmg_channels && edmg_bw_config) ||
1222	    (edmg_channels && !edmg_bw_config))
1223		return false;
1224
1225	if (!(edmg_channels & BIT(primary_channel - 1)))
1226		return false;
1227
1228	/* 60GHz channels 1..6 */
1229	for (i = 0; i < 6; i++) {
1230		if (!(edmg_channels & BIT(i)))
1231			continue;
1232
1233		if (!(edmg_cap->channels & BIT(i)))
1234			return false;
1235
1236		channels_counter++;
1237
1238		freq = ieee80211_channel_to_frequency(i + 1,
1239						      NL80211_BAND_60GHZ);
1240		chan = ieee80211_get_channel(wiphy, freq);
1241		if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1242			return false;
1243	}
1244
1245	/* IEEE802.11 allows max 4 channels */
1246	if (channels_counter > 4)
1247		return false;
1248
1249	/* check bw_config is a subset of what driver supports
1250	 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
1251	 */
1252	if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
1253		return false;
1254
1255	if (edmg_bw_config > edmg_cap->bw_config)
1256		return false;
1257
1258	return true;
1259}
1260
1261bool _cfg80211_chandef_usable(struct wiphy *wiphy,
1262			      const struct cfg80211_chan_def *chandef,
1263			      u32 prohibited_flags,
1264			      u32 permitting_flags)
1265{
1266	struct ieee80211_sta_ht_cap *ht_cap;
1267	struct ieee80211_sta_vht_cap *vht_cap;
1268	struct ieee80211_edmg *edmg_cap;
1269	u32 width, control_freq, cap;
1270	bool ext_nss_cap, support_80_80 = false, support_320 = false;
1271	const struct ieee80211_sband_iftype_data *iftd;
1272	struct ieee80211_supported_band *sband;
1273	int i;
1274
1275	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1276		return false;
1277
1278	ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
1279	vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
1280	edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
1281	ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
1282			IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
1283
1284	if (edmg_cap->channels &&
1285	    !cfg80211_edmg_usable(wiphy,
1286				  chandef->edmg.channels,
1287				  chandef->edmg.bw_config,
1288				  chandef->chan->hw_value,
1289				  edmg_cap))
1290		return false;
1291
1292	control_freq = chandef->chan->center_freq;
1293
1294	switch (chandef->width) {
1295	case NL80211_CHAN_WIDTH_1:
1296		width = 1;
1297		break;
1298	case NL80211_CHAN_WIDTH_2:
1299		width = 2;
1300		break;
1301	case NL80211_CHAN_WIDTH_4:
1302		width = 4;
1303		break;
1304	case NL80211_CHAN_WIDTH_8:
1305		width = 8;
1306		break;
1307	case NL80211_CHAN_WIDTH_16:
1308		width = 16;
1309		break;
1310	case NL80211_CHAN_WIDTH_5:
1311		width = 5;
1312		break;
1313	case NL80211_CHAN_WIDTH_10:
1314		prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
1315		width = 10;
1316		break;
1317	case NL80211_CHAN_WIDTH_20:
1318		if (!ht_cap->ht_supported &&
1319		    chandef->chan->band != NL80211_BAND_6GHZ)
1320			return false;
1321		fallthrough;
1322	case NL80211_CHAN_WIDTH_20_NOHT:
1323		prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
1324		width = 20;
1325		break;
1326	case NL80211_CHAN_WIDTH_40:
1327		width = 40;
1328		if (chandef->chan->band == NL80211_BAND_6GHZ)
1329			break;
1330		if (!ht_cap->ht_supported)
1331			return false;
1332		if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1333		    ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1334			return false;
1335		if (chandef->center_freq1 < control_freq &&
1336		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1337			return false;
1338		if (chandef->center_freq1 > control_freq &&
1339		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1340			return false;
1341		break;
1342	case NL80211_CHAN_WIDTH_80P80:
1343		cap = vht_cap->cap;
1344		support_80_80 =
1345			(cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
1346			(cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1347			 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
1348			(ext_nss_cap &&
1349			 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
1350		if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
1351			return false;
1352		fallthrough;
1353	case NL80211_CHAN_WIDTH_80:
1354		prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
1355		width = 80;
1356		if (chandef->chan->band == NL80211_BAND_6GHZ)
1357			break;
1358		if (!vht_cap->vht_supported)
1359			return false;
 
 
1360		break;
1361	case NL80211_CHAN_WIDTH_160:
1362		prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1363		width = 160;
1364		if (chandef->chan->band == NL80211_BAND_6GHZ)
1365			break;
1366		if (!vht_cap->vht_supported)
1367			return false;
1368		cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1369		if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1370		    cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
1371		    !(ext_nss_cap &&
1372		      (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
1373			return false;
1374		break;
1375	case NL80211_CHAN_WIDTH_320:
1376		prohibited_flags |= IEEE80211_CHAN_NO_320MHZ;
1377		width = 320;
1378
1379		if (chandef->chan->band != NL80211_BAND_6GHZ)
1380			return false;
1381
1382		sband = wiphy->bands[NL80211_BAND_6GHZ];
1383		if (!sband)
1384			return false;
1385
1386		for_each_sband_iftype_data(sband, i, iftd) {
1387			if (!iftd->eht_cap.has_eht)
1388				continue;
1389
1390			if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] &
1391			    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
1392				support_320 = true;
1393				break;
1394			}
1395		}
1396
1397		if (!support_320)
1398			return false;
 
 
1399		break;
1400	default:
1401		WARN_ON_ONCE(1);
1402		return false;
1403	}
1404
1405	/*
1406	 * TODO: What if there are only certain 80/160/80+80 MHz channels
1407	 *	 allowed by the driver, or only certain combinations?
1408	 *	 For 40 MHz the driver can set the NO_HT40 flags, but for
1409	 *	 80/160 MHz and in particular 80+80 MHz this isn't really
1410	 *	 feasible and we only have NO_80MHZ/NO_160MHZ so far but
1411	 *	 no way to cover 80+80 MHz or more complex restrictions.
1412	 *	 Note that such restrictions also need to be advertised to
1413	 *	 userspace, for example for P2P channel selection.
1414	 */
1415
1416	if (width > 20)
1417		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1418
1419	/* 5 and 10 MHz are only defined for the OFDM PHY */
1420	if (width < 20)
1421		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1422
1423
1424	if (!cfg80211_secondary_chans_ok(wiphy,
1425					 ieee80211_chandef_to_khz(chandef),
1426					 width, prohibited_flags,
1427					 permitting_flags))
1428		return false;
1429
1430	if (!chandef->center_freq2)
1431		return true;
1432	return cfg80211_secondary_chans_ok(wiphy,
1433					   MHZ_TO_KHZ(chandef->center_freq2),
1434					   width, prohibited_flags,
1435					   permitting_flags);
1436}
 
1437
1438bool cfg80211_chandef_usable(struct wiphy *wiphy,
1439			     const struct cfg80211_chan_def *chandef,
1440			     u32 prohibited_flags)
 
 
 
 
 
 
 
 
 
1441{
1442	return _cfg80211_chandef_usable(wiphy, chandef, prohibited_flags, 0);
1443}
1444EXPORT_SYMBOL(cfg80211_chandef_usable);
 
 
 
 
 
1445
1446static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,
1447					      struct wireless_dev *wdev,
1448					      struct ieee80211_channel *chan)
1449{
1450	struct ieee80211_channel *other_chan = NULL;
1451	unsigned int link_id;
1452	int r1, r2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1453
1454	for_each_valid_link(wdev, link_id) {
1455		if (wdev->iftype == NL80211_IFTYPE_STATION &&
1456		    wdev->links[link_id].client.current_bss)
1457			other_chan = wdev->links[link_id].client.current_bss->pub.channel;
1458
1459		/*
1460		 * If a GO already operates on the same GO_CONCURRENT channel,
1461		 * this one (maybe the same one) can beacon as well. We allow
1462		 * the operation even if the station we relied on with
1463		 * GO_CONCURRENT is disconnected now. But then we must make sure
1464		 * we're not outdoor on an indoor-only channel.
1465		 */
1466		if (iftype == NL80211_IFTYPE_P2P_GO &&
1467		    wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1468		    wdev->links[link_id].ap.beacon_interval &&
1469		    !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1470			other_chan = wdev->links[link_id].ap.chandef.chan;
 
1471
1472		if (!other_chan)
1473			continue;
1474
1475		if (chan == other_chan)
1476			return true;
1477
1478		if (chan->band != NL80211_BAND_5GHZ &&
1479		    chan->band != NL80211_BAND_6GHZ)
1480			continue;
1481
1482		r1 = cfg80211_get_unii(chan->center_freq);
1483		r2 = cfg80211_get_unii(other_chan->center_freq);
1484
1485		if (r1 != -EINVAL && r1 == r2) {
1486			/*
1487			 * At some locations channels 149-165 are considered a
1488			 * bundle, but at other locations, e.g., Indonesia,
1489			 * channels 149-161 are considered a bundle while
1490			 * channel 165 is left out and considered to be in a
1491			 * different bundle. Thus, in case that there is a
1492			 * station interface connected to an AP on channel 165,
1493			 * it is assumed that channels 149-161 are allowed for
1494			 * GO operations. However, having a station interface
1495			 * connected to an AP on channels 149-161, does not
1496			 * allow GO operation on channel 165.
1497			 */
1498			if (chan->center_freq == 5825 &&
1499			    other_chan->center_freq != 5825)
1500				continue;
1501			return true;
1502		}
1503	}
1504
1505	return false;
1506}
1507
1508/*
1509 * Check if the channel can be used under permissive conditions mandated by
1510 * some regulatory bodies, i.e., the channel is marked with
1511 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1512 * associated to an AP on the same channel or on the same UNII band
1513 * (assuming that the AP is an authorized master).
1514 * In addition allow operation on a channel on which indoor operation is
1515 * allowed, iff we are currently operating in an indoor environment.
1516 */
1517static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1518					enum nl80211_iftype iftype,
1519					struct ieee80211_channel *chan)
1520{
1521	struct wireless_dev *wdev;
1522	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1523
1524	lockdep_assert_held(&rdev->wiphy.mtx);
1525
1526	if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1527	    !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1528		return false;
1529
1530	/* only valid for GO and TDLS off-channel (station/p2p-CL) */
1531	if (iftype != NL80211_IFTYPE_P2P_GO &&
1532	    iftype != NL80211_IFTYPE_STATION &&
1533	    iftype != NL80211_IFTYPE_P2P_CLIENT)
1534		return false;
1535
1536	if (regulatory_indoor_allowed() &&
1537	    (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1538		return true;
1539
1540	if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1541		return false;
1542
1543	/*
1544	 * Generally, it is possible to rely on another device/driver to allow
1545	 * the IR concurrent relaxation, however, since the device can further
1546	 * enforce the relaxation (by doing a similar verifications as this),
1547	 * and thus fail the GO instantiation, consider only the interfaces of
1548	 * the current registered device.
1549	 */
1550	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1551		bool ret;
1552
1553		ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
1554		if (ret)
1555			return ret;
1556	}
1557
1558	return false;
1559}
1560
1561static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1562				     struct cfg80211_chan_def *chandef,
1563				     enum nl80211_iftype iftype,
1564				     u32 prohibited_flags,
1565				     u32 permitting_flags)
1566{
1567	bool res, check_radar;
1568	int dfs_required;
1569
1570	trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype,
1571				      prohibited_flags,
1572				      permitting_flags);
1573
1574	if (!_cfg80211_chandef_usable(wiphy, chandef,
1575				      IEEE80211_CHAN_DISABLED, 0))
1576		return false;
1577
1578	dfs_required = cfg80211_chandef_dfs_required(wiphy, chandef, iftype);
1579	check_radar = dfs_required != 0;
1580
1581	if (dfs_required > 0 &&
1582	    cfg80211_chandef_dfs_available(wiphy, chandef)) {
1583		/* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1584		prohibited_flags &= ~IEEE80211_CHAN_NO_IR;
1585		check_radar = false;
1586	}
1587
1588	if (check_radar &&
1589	    !_cfg80211_chandef_usable(wiphy, chandef,
1590				      IEEE80211_CHAN_RADAR, 0))
1591		return false;
1592
1593	res = _cfg80211_chandef_usable(wiphy, chandef,
1594				       prohibited_flags,
1595				       permitting_flags);
1596
1597	trace_cfg80211_return_bool(res);
1598	return res;
1599}
1600
1601bool cfg80211_reg_check_beaconing(struct wiphy *wiphy,
1602				  struct cfg80211_chan_def *chandef,
1603				  struct cfg80211_beaconing_check_config *cfg)
1604{
1605	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1606	u32 permitting_flags = 0;
1607	bool check_no_ir = true;
 
 
 
 
 
 
 
 
1608
1609	/*
1610	 * Under certain conditions suggested by some regulatory bodies a
1611	 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1612	 * only if such relaxations are not enabled and the conditions are not
1613	 * met.
1614	 */
1615	if (cfg->relax) {
1616		lockdep_assert_held(&rdev->wiphy.mtx);
1617		check_no_ir = !cfg80211_ir_permissive_chan(wiphy, cfg->iftype,
1618							   chandef->chan);
1619	}
1620
1621	if (cfg->reg_power == IEEE80211_REG_VLP_AP)
1622		permitting_flags |= IEEE80211_CHAN_ALLOW_6GHZ_VLP_AP;
1623
1624	return _cfg80211_reg_can_beacon(wiphy, chandef, cfg->iftype,
1625					check_no_ir ? IEEE80211_CHAN_NO_IR : 0,
1626					permitting_flags);
1627}
1628EXPORT_SYMBOL(cfg80211_reg_check_beaconing);
1629
1630int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1631				 struct net_device *dev,
1632				 struct cfg80211_chan_def *chandef)
1633{
1634	if (!rdev->ops->set_monitor_channel)
1635		return -EOPNOTSUPP;
1636	if (!cfg80211_has_monitors_only(rdev))
1637		return -EBUSY;
1638
1639	return rdev_set_monitor_channel(rdev, dev, chandef);
1640}
1641
1642bool cfg80211_any_usable_channels(struct wiphy *wiphy,
1643				  unsigned long sband_mask,
1644				  u32 prohibited_flags)
 
 
1645{
1646	int idx;
1647
1648	prohibited_flags |= IEEE80211_CHAN_DISABLED;
1649
1650	for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) {
1651		struct ieee80211_supported_band *sband = wiphy->bands[idx];
1652		int chanidx;
1653
1654		if (!sband)
1655			continue;
1656
1657		for (chanidx = 0; chanidx < sband->n_channels; chanidx++) {
1658			struct ieee80211_channel *chan;
1659
1660			chan = &sband->channels[chanidx];
1661
1662			if (chan->flags & prohibited_flags)
1663				continue;
1664
1665			return true;
1666		}
1667	}
1668
1669	return false;
1670}
1671EXPORT_SYMBOL(cfg80211_any_usable_channels);
1672
1673struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
1674				       unsigned int link_id)
1675{
1676	lockdep_assert_wiphy(wdev->wiphy);
1677
1678	WARN_ON(wdev->valid_links && !(wdev->valid_links & BIT(link_id)));
1679	WARN_ON(!wdev->valid_links && link_id > 0);
1680
1681	switch (wdev->iftype) {
1682	case NL80211_IFTYPE_MESH_POINT:
1683		return &wdev->u.mesh.chandef;
1684	case NL80211_IFTYPE_ADHOC:
1685		return &wdev->u.ibss.chandef;
1686	case NL80211_IFTYPE_OCB:
1687		return &wdev->u.ocb.chandef;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1688	case NL80211_IFTYPE_AP:
1689	case NL80211_IFTYPE_P2P_GO:
1690		return &wdev->links[link_id].ap.chandef;
1691	default:
1692		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1693	}
1694}
1695EXPORT_SYMBOL(wdev_chandef);
v4.17
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * This file contains helper code to handle channel
   4 * settings and keeping track of what is possible at
   5 * any point in time.
   6 *
   7 * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
   8 * Copyright 2013-2014  Intel Mobile Communications GmbH
 
   9 */
  10
  11#include <linux/export.h>
 
  12#include <net/cfg80211.h>
  13#include "core.h"
  14#include "rdev-ops.h"
  15
 
 
 
 
 
  16void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
  17			     struct ieee80211_channel *chan,
  18			     enum nl80211_channel_type chan_type)
  19{
  20	if (WARN_ON(!chan))
  21		return;
  22
  23	chandef->chan = chan;
  24	chandef->center_freq2 = 0;
 
 
  25
  26	switch (chan_type) {
  27	case NL80211_CHAN_NO_HT:
  28		chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  29		chandef->center_freq1 = chan->center_freq;
  30		break;
  31	case NL80211_CHAN_HT20:
  32		chandef->width = NL80211_CHAN_WIDTH_20;
  33		chandef->center_freq1 = chan->center_freq;
  34		break;
  35	case NL80211_CHAN_HT40PLUS:
  36		chandef->width = NL80211_CHAN_WIDTH_40;
  37		chandef->center_freq1 = chan->center_freq + 10;
  38		break;
  39	case NL80211_CHAN_HT40MINUS:
  40		chandef->width = NL80211_CHAN_WIDTH_40;
  41		chandef->center_freq1 = chan->center_freq - 10;
  42		break;
  43	default:
  44		WARN_ON(1);
  45	}
  46}
  47EXPORT_SYMBOL(cfg80211_chandef_create);
  48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  49bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
  50{
  51	u32 control_freq;
 
  52
  53	if (!chandef->chan)
  54		return false;
  55
 
 
 
  56	control_freq = chandef->chan->center_freq;
  57
  58	switch (chandef->width) {
  59	case NL80211_CHAN_WIDTH_5:
  60	case NL80211_CHAN_WIDTH_10:
  61	case NL80211_CHAN_WIDTH_20:
  62	case NL80211_CHAN_WIDTH_20_NOHT:
  63		if (chandef->center_freq1 != control_freq)
 
  64			return false;
  65		if (chandef->center_freq2)
  66			return false;
  67		break;
  68	case NL80211_CHAN_WIDTH_40:
  69		if (chandef->center_freq1 != control_freq + 10 &&
  70		    chandef->center_freq1 != control_freq - 10)
 
 
 
 
 
 
 
 
 
 
 
 
 
  71			return false;
  72		if (chandef->center_freq2)
  73			return false;
 
 
 
 
 
 
 
 
  74		break;
  75	case NL80211_CHAN_WIDTH_80P80:
  76		if (chandef->center_freq1 != control_freq + 30 &&
  77		    chandef->center_freq1 != control_freq + 10 &&
  78		    chandef->center_freq1 != control_freq - 10 &&
  79		    chandef->center_freq1 != control_freq - 30)
  80			return false;
  81		if (!chandef->center_freq2)
  82			return false;
  83		/* adjacent is not allowed -- that's a 160 MHz channel */
  84		if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
  85		    chandef->center_freq2 - chandef->center_freq1 == 80)
  86			return false;
  87		break;
  88	case NL80211_CHAN_WIDTH_80:
  89		if (chandef->center_freq1 != control_freq + 30 &&
  90		    chandef->center_freq1 != control_freq + 10 &&
  91		    chandef->center_freq1 != control_freq - 10 &&
  92		    chandef->center_freq1 != control_freq - 30)
  93			return false;
  94		if (chandef->center_freq2)
  95			return false;
  96		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  97	case NL80211_CHAN_WIDTH_160:
  98		if (chandef->center_freq1 != control_freq + 70 &&
  99		    chandef->center_freq1 != control_freq + 50 &&
 100		    chandef->center_freq1 != control_freq + 30 &&
 101		    chandef->center_freq1 != control_freq + 10 &&
 102		    chandef->center_freq1 != control_freq - 10 &&
 103		    chandef->center_freq1 != control_freq - 30 &&
 104		    chandef->center_freq1 != control_freq - 50 &&
 105		    chandef->center_freq1 != control_freq - 70)
 106			return false;
 107		if (chandef->center_freq2)
 108			return false;
 109		break;
 
 
 
 
 
 110	default:
 111		return false;
 112	}
 113
 114	return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 115}
 116EXPORT_SYMBOL(cfg80211_chandef_valid);
 117
 118static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
 119				  u32 *pri40, u32 *pri80)
 120{
 121	int tmp;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 122
 123	switch (c->width) {
 124	case NL80211_CHAN_WIDTH_40:
 125		*pri40 = c->center_freq1;
 126		*pri80 = 0;
 127		break;
 128	case NL80211_CHAN_WIDTH_80:
 129	case NL80211_CHAN_WIDTH_80P80:
 130		*pri80 = c->center_freq1;
 131		/* n_P20 */
 132		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
 133		/* n_P40 */
 134		tmp /= 2;
 135		/* freq_P40 */
 136		*pri40 = c->center_freq1 - 20 + 40 * tmp;
 137		break;
 138	case NL80211_CHAN_WIDTH_160:
 139		/* n_P20 */
 140		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
 141		/* n_P40 */
 142		tmp /= 2;
 143		/* freq_P40 */
 144		*pri40 = c->center_freq1 - 60 + 40 * tmp;
 145		/* n_P80 */
 146		tmp /= 2;
 147		*pri80 = c->center_freq1 - 40 + 80 * tmp;
 148		break;
 149	default:
 150		WARN_ON_ONCE(1);
 151	}
 
 
 152}
 
 153
 154static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
 
 
 
 155{
 156	int width;
 
 
 
 
 
 
 
 
 
 
 
 
 157
 158	switch (c->width) {
 159	case NL80211_CHAN_WIDTH_5:
 160		width = 5;
 161		break;
 162	case NL80211_CHAN_WIDTH_10:
 163		width = 10;
 164		break;
 165	case NL80211_CHAN_WIDTH_20:
 166	case NL80211_CHAN_WIDTH_20_NOHT:
 167		width = 20;
 168		break;
 169	case NL80211_CHAN_WIDTH_40:
 170		width = 40;
 171		break;
 172	case NL80211_CHAN_WIDTH_80P80:
 173	case NL80211_CHAN_WIDTH_80:
 174		width = 80;
 175		break;
 176	case NL80211_CHAN_WIDTH_160:
 177		width = 160;
 178		break;
 179	default:
 180		WARN_ON_ONCE(1);
 181		return -1;
 182	}
 183	return width;
 184}
 185
 186const struct cfg80211_chan_def *
 187cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
 188			    const struct cfg80211_chan_def *c2)
 189{
 190	u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
 191
 192	/* If they are identical, return */
 193	if (cfg80211_chandef_identical(c1, c2))
 194		return c1;
 195
 196	/* otherwise, must have same control channel */
 197	if (c1->chan != c2->chan)
 198		return NULL;
 199
 200	/*
 201	 * If they have the same width, but aren't identical,
 202	 * then they can't be compatible.
 203	 */
 204	if (c1->width == c2->width)
 205		return NULL;
 206
 207	/*
 208	 * can't be compatible if one of them is 5 or 10 MHz,
 209	 * but they don't have the same width.
 210	 */
 211	if (c1->width == NL80211_CHAN_WIDTH_5 ||
 212	    c1->width == NL80211_CHAN_WIDTH_10 ||
 213	    c2->width == NL80211_CHAN_WIDTH_5 ||
 214	    c2->width == NL80211_CHAN_WIDTH_10)
 
 
 
 
 
 215		return NULL;
 216
 217	if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
 218	    c1->width == NL80211_CHAN_WIDTH_20)
 
 
 
 
 
 
 
 
 
 
 
 
 219		return c2;
 220
 221	if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
 222	    c2->width == NL80211_CHAN_WIDTH_20)
 223		return c1;
 
 
 
 
 224
 225	chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
 226	chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 227
 228	if (c1_pri40 != c2_pri40)
 229		return NULL;
 
 
 
 230
 231	WARN_ON(!c1_pri80 && !c2_pri80);
 232	if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
 233		return NULL;
 234
 235	if (c1->width > c2->width)
 236		return c1;
 237	return c2;
 238}
 239EXPORT_SYMBOL(cfg80211_chandef_compatible);
 240
 241static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
 242					 u32 bandwidth,
 243					 enum nl80211_dfs_state dfs_state)
 244{
 245	struct ieee80211_channel *c;
 246	u32 freq;
 247
 248	for (freq = center_freq - bandwidth/2 + 10;
 249	     freq <= center_freq + bandwidth/2 - 10;
 250	     freq += 20) {
 251		c = ieee80211_get_channel(wiphy, freq);
 252		if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
 253			continue;
 254
 255		c->dfs_state = dfs_state;
 256		c->dfs_state_entered = jiffies;
 257	}
 258}
 259
 260void cfg80211_set_dfs_state(struct wiphy *wiphy,
 261			    const struct cfg80211_chan_def *chandef,
 262			    enum nl80211_dfs_state dfs_state)
 263{
 264	int width;
 265
 266	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 267		return;
 268
 269	width = cfg80211_chandef_get_width(chandef);
 270	if (width < 0)
 271		return;
 272
 273	cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
 274				     width, dfs_state);
 275
 276	if (!chandef->center_freq2)
 277		return;
 278	cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
 279				     width, dfs_state);
 280}
 281
 282static u32 cfg80211_get_start_freq(u32 center_freq,
 283				   u32 bandwidth)
 284{
 285	u32 start_freq;
 286
 287	if (bandwidth <= 20)
 
 288		start_freq = center_freq;
 289	else
 290		start_freq = center_freq - bandwidth/2 + 10;
 291
 292	return start_freq;
 293}
 294
 295static u32 cfg80211_get_end_freq(u32 center_freq,
 296				 u32 bandwidth)
 297{
 298	u32 end_freq;
 299
 300	if (bandwidth <= 20)
 
 301		end_freq = center_freq;
 302	else
 303		end_freq = center_freq + bandwidth/2 - 10;
 304
 305	return end_freq;
 306}
 307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 308static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
 309					    u32 center_freq,
 310					    u32 bandwidth)
 
 311{
 312	struct ieee80211_channel *c;
 313	u32 freq, start_freq, end_freq;
 314
 315	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
 316	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
 317
 318	for (freq = start_freq; freq <= end_freq; freq += 20) {
 319		c = ieee80211_get_channel(wiphy, freq);
 320		if (!c)
 321			return -EINVAL;
 322
 323		if (c->flags & IEEE80211_CHAN_RADAR)
 
 324			return 1;
 325	}
 
 326	return 0;
 327}
 328
 329
 330int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
 331				  const struct cfg80211_chan_def *chandef,
 332				  enum nl80211_iftype iftype)
 333{
 334	int width;
 335	int ret;
 336
 337	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 338		return -EINVAL;
 339
 340	switch (iftype) {
 341	case NL80211_IFTYPE_ADHOC:
 342	case NL80211_IFTYPE_AP:
 343	case NL80211_IFTYPE_P2P_GO:
 344	case NL80211_IFTYPE_MESH_POINT:
 345		width = cfg80211_chandef_get_width(chandef);
 346		if (width < 0)
 347			return -EINVAL;
 348
 349		ret = cfg80211_get_chans_dfs_required(wiphy,
 350						      chandef->center_freq1,
 351						      width);
 352		if (ret < 0)
 353			return ret;
 354		else if (ret > 0)
 355			return BIT(chandef->width);
 356
 357		if (!chandef->center_freq2)
 358			return 0;
 359
 360		ret = cfg80211_get_chans_dfs_required(wiphy,
 361						      chandef->center_freq2,
 362						      width);
 363		if (ret < 0)
 364			return ret;
 365		else if (ret > 0)
 366			return BIT(chandef->width);
 367
 368		break;
 369	case NL80211_IFTYPE_STATION:
 370	case NL80211_IFTYPE_OCB:
 371	case NL80211_IFTYPE_P2P_CLIENT:
 372	case NL80211_IFTYPE_MONITOR:
 373	case NL80211_IFTYPE_AP_VLAN:
 374	case NL80211_IFTYPE_WDS:
 375	case NL80211_IFTYPE_P2P_DEVICE:
 376	case NL80211_IFTYPE_NAN:
 377		break;
 
 378	case NL80211_IFTYPE_UNSPECIFIED:
 379	case NUM_NL80211_IFTYPES:
 380		WARN_ON(1);
 381	}
 382
 383	return 0;
 384}
 385EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
 386
 387static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
 388					 u32 center_freq,
 389					 u32 bandwidth)
 390{
 391	struct ieee80211_channel *c;
 392	u32 freq, start_freq, end_freq;
 393	int count = 0;
 394
 395	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
 396	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
 397
 398	/*
 399	 * Check entire range of channels for the bandwidth.
 400	 * Check all channels are DFS channels (DFS_USABLE or
 401	 * DFS_AVAILABLE). Return number of usable channels
 402	 * (require CAC). Allow DFS and non-DFS channel mix.
 403	 */
 404	for (freq = start_freq; freq <= end_freq; freq += 20) {
 405		c = ieee80211_get_channel(wiphy, freq);
 406		if (!c)
 407			return -EINVAL;
 408
 409		if (c->flags & IEEE80211_CHAN_DISABLED)
 410			return -EINVAL;
 411
 412		if (c->flags & IEEE80211_CHAN_RADAR) {
 413			if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
 414				return -EINVAL;
 415
 416			if (c->dfs_state == NL80211_DFS_USABLE)
 417				count++;
 418		}
 419	}
 420
 421	return count;
 422}
 423
 424bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
 425				 const struct cfg80211_chan_def *chandef)
 426{
 427	int width;
 428	int r1, r2 = 0;
 429
 430	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 431		return false;
 432
 433	width = cfg80211_chandef_get_width(chandef);
 434	if (width < 0)
 435		return false;
 436
 437	r1 = cfg80211_get_chans_dfs_usable(wiphy, chandef->center_freq1,
 438					  width);
 
 439
 440	if (r1 < 0)
 441		return false;
 442
 443	switch (chandef->width) {
 444	case NL80211_CHAN_WIDTH_80P80:
 445		WARN_ON(!chandef->center_freq2);
 446		r2 = cfg80211_get_chans_dfs_usable(wiphy,
 447						   chandef->center_freq2,
 448						   width);
 449		if (r2 < 0)
 450			return false;
 451		break;
 452	default:
 453		WARN_ON(chandef->center_freq2);
 454		break;
 455	}
 456
 457	return (r1 + r2 > 0);
 458}
 
 459
 460/*
 461 * Checks if center frequency of chan falls with in the bandwidth
 462 * range of chandef.
 463 */
 464bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
 465			  struct ieee80211_channel *chan)
 
 466{
 467	int width;
 468	u32 freq;
 469
 
 
 
 470	if (chandef->chan->center_freq == chan->center_freq)
 471		return true;
 472
 
 
 
 473	width = cfg80211_chandef_get_width(chandef);
 474	if (width <= 20)
 475		return false;
 476
 477	for (freq = chandef->center_freq1 - width / 2 + 10;
 478	     freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
 479		if (chan->center_freq == freq)
 480			return true;
 481	}
 482
 483	if (!chandef->center_freq2)
 484		return false;
 485
 486	for (freq = chandef->center_freq2 - width / 2 + 10;
 487	     freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
 488		if (chan->center_freq == freq)
 489			return true;
 490	}
 491
 492	return false;
 493}
 494
 495bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
 496{
 497	bool active = false;
 498
 499	ASSERT_WDEV_LOCK(wdev);
 500
 501	if (!wdev->chandef.chan)
 502		return false;
 503
 504	switch (wdev->iftype) {
 505	case NL80211_IFTYPE_AP:
 506	case NL80211_IFTYPE_P2P_GO:
 507		active = wdev->beacon_interval != 0;
 
 
 
 508		break;
 509	case NL80211_IFTYPE_ADHOC:
 510		active = wdev->ssid_len != 0;
 
 511		break;
 512	case NL80211_IFTYPE_MESH_POINT:
 513		active = wdev->mesh_id_len != 0;
 
 514		break;
 515	case NL80211_IFTYPE_STATION:
 516	case NL80211_IFTYPE_OCB:
 517	case NL80211_IFTYPE_P2P_CLIENT:
 518	case NL80211_IFTYPE_MONITOR:
 519	case NL80211_IFTYPE_AP_VLAN:
 520	case NL80211_IFTYPE_WDS:
 521	case NL80211_IFTYPE_P2P_DEVICE:
 522	/* Can NAN type be considered as beaconing interface? */
 523	case NL80211_IFTYPE_NAN:
 524		break;
 525	case NL80211_IFTYPE_UNSPECIFIED:
 
 526	case NUM_NL80211_IFTYPES:
 527		WARN_ON(1);
 528	}
 529
 530	return active;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 531}
 532
 533static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
 534					struct ieee80211_channel *chan)
 535{
 536	struct wireless_dev *wdev;
 537
 
 
 538	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
 539		wdev_lock(wdev);
 540		if (!cfg80211_beaconing_iface_active(wdev)) {
 541			wdev_unlock(wdev);
 542			continue;
 543		}
 544
 545		if (cfg80211_is_sub_chan(&wdev->chandef, chan)) {
 546			wdev_unlock(wdev);
 547			return true;
 548		}
 549		wdev_unlock(wdev);
 550	}
 551
 552	return false;
 553}
 554
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 555bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
 556				  struct ieee80211_channel *chan)
 557{
 558	struct cfg80211_registered_device *rdev;
 559
 560	ASSERT_RTNL();
 561
 562	if (!(chan->flags & IEEE80211_CHAN_RADAR))
 563		return false;
 564
 565	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
 
 
 566		if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
 567			continue;
 568
 569		if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
 
 
 
 
 
 570			return true;
 571	}
 572
 573	return false;
 574}
 575
 576static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
 577					     u32 center_freq,
 578					     u32 bandwidth)
 579{
 580	struct ieee80211_channel *c;
 581	u32 freq, start_freq, end_freq;
 582	bool dfs_offload;
 583
 584	dfs_offload = wiphy_ext_feature_isset(wiphy,
 585					      NL80211_EXT_FEATURE_DFS_OFFLOAD);
 586
 587	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
 588	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
 589
 590	/*
 591	 * Check entire range of channels for the bandwidth.
 592	 * If any channel in between is disabled or has not
 593	 * had gone through CAC return false
 594	 */
 595	for (freq = start_freq; freq <= end_freq; freq += 20) {
 596		c = ieee80211_get_channel(wiphy, freq);
 597		if (!c)
 598			return false;
 599
 600		if (c->flags & IEEE80211_CHAN_DISABLED)
 601			return false;
 602
 603		if ((c->flags & IEEE80211_CHAN_RADAR) &&
 604		    (c->dfs_state != NL80211_DFS_AVAILABLE) &&
 605		    !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
 606			return false;
 607	}
 608
 609	return true;
 610}
 611
 612static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
 613				const struct cfg80211_chan_def *chandef)
 614{
 615	int width;
 616	int r;
 617
 618	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 619		return false;
 620
 621	width = cfg80211_chandef_get_width(chandef);
 622	if (width < 0)
 623		return false;
 624
 625	r = cfg80211_get_chans_dfs_available(wiphy, chandef->center_freq1,
 
 626					     width);
 627
 628	/* If any of channels unavailable for cf1 just return */
 629	if (!r)
 630		return r;
 631
 632	switch (chandef->width) {
 633	case NL80211_CHAN_WIDTH_80P80:
 634		WARN_ON(!chandef->center_freq2);
 635		r = cfg80211_get_chans_dfs_available(wiphy,
 636						     chandef->center_freq2,
 637						     width);
 638		break;
 639	default:
 640		WARN_ON(chandef->center_freq2);
 641		break;
 642	}
 643
 644	return r;
 645}
 646
 647static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
 648						    u32 center_freq,
 649						    u32 bandwidth)
 650{
 651	struct ieee80211_channel *c;
 652	u32 start_freq, end_freq, freq;
 653	unsigned int dfs_cac_ms = 0;
 654
 655	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
 656	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
 657
 658	for (freq = start_freq; freq <= end_freq; freq += 20) {
 659		c = ieee80211_get_channel(wiphy, freq);
 660		if (!c)
 661			return 0;
 662
 663		if (c->flags & IEEE80211_CHAN_DISABLED)
 664			return 0;
 665
 666		if (!(c->flags & IEEE80211_CHAN_RADAR))
 667			continue;
 668
 669		if (c->dfs_cac_ms > dfs_cac_ms)
 670			dfs_cac_ms = c->dfs_cac_ms;
 671	}
 672
 673	return dfs_cac_ms;
 674}
 675
 676unsigned int
 677cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
 678			      const struct cfg80211_chan_def *chandef)
 679{
 680	int width;
 681	unsigned int t1 = 0, t2 = 0;
 682
 683	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 684		return 0;
 685
 686	width = cfg80211_chandef_get_width(chandef);
 687	if (width < 0)
 688		return 0;
 689
 690	t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
 691					     chandef->center_freq1,
 692					     width);
 693
 694	if (!chandef->center_freq2)
 695		return t1;
 696
 697	t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
 698					     chandef->center_freq2,
 699					     width);
 700
 701	return max(t1, t2);
 702}
 
 703
 704static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
 705					u32 center_freq, u32 bandwidth,
 706					u32 prohibited_flags)
 
 707{
 708	struct ieee80211_channel *c;
 709	u32 freq, start_freq, end_freq;
 710
 711	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
 712	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
 713
 714	for (freq = start_freq; freq <= end_freq; freq += 20) {
 715		c = ieee80211_get_channel(wiphy, freq);
 716		if (!c || c->flags & prohibited_flags)
 
 
 
 
 717			return false;
 718	}
 719
 720	return true;
 721}
 722
 723bool cfg80211_chandef_usable(struct wiphy *wiphy,
 724			     const struct cfg80211_chan_def *chandef,
 725			     u32 prohibited_flags)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 726{
 727	struct ieee80211_sta_ht_cap *ht_cap;
 728	struct ieee80211_sta_vht_cap *vht_cap;
 
 729	u32 width, control_freq, cap;
 
 
 
 
 730
 731	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
 732		return false;
 733
 734	ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
 735	vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
 
 
 
 
 
 
 
 
 
 
 
 736
 737	control_freq = chandef->chan->center_freq;
 738
 739	switch (chandef->width) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 740	case NL80211_CHAN_WIDTH_5:
 741		width = 5;
 742		break;
 743	case NL80211_CHAN_WIDTH_10:
 744		prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
 745		width = 10;
 746		break;
 747	case NL80211_CHAN_WIDTH_20:
 748		if (!ht_cap->ht_supported)
 
 749			return false;
 
 750	case NL80211_CHAN_WIDTH_20_NOHT:
 751		prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
 752		width = 20;
 753		break;
 754	case NL80211_CHAN_WIDTH_40:
 755		width = 40;
 
 
 756		if (!ht_cap->ht_supported)
 757			return false;
 758		if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
 759		    ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
 760			return false;
 761		if (chandef->center_freq1 < control_freq &&
 762		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
 763			return false;
 764		if (chandef->center_freq1 > control_freq &&
 765		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
 766			return false;
 767		break;
 768	case NL80211_CHAN_WIDTH_80P80:
 769		cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
 770		if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
 
 
 
 
 
 
 771			return false;
 
 772	case NL80211_CHAN_WIDTH_80:
 
 
 
 
 773		if (!vht_cap->vht_supported)
 774			return false;
 775		prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
 776		width = 80;
 777		break;
 778	case NL80211_CHAN_WIDTH_160:
 
 
 
 
 779		if (!vht_cap->vht_supported)
 780			return false;
 781		cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
 782		if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
 783		    cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 784			return false;
 785		prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
 786		width = 160;
 787		break;
 788	default:
 789		WARN_ON_ONCE(1);
 790		return false;
 791	}
 792
 793	/*
 794	 * TODO: What if there are only certain 80/160/80+80 MHz channels
 795	 *	 allowed by the driver, or only certain combinations?
 796	 *	 For 40 MHz the driver can set the NO_HT40 flags, but for
 797	 *	 80/160 MHz and in particular 80+80 MHz this isn't really
 798	 *	 feasible and we only have NO_80MHZ/NO_160MHZ so far but
 799	 *	 no way to cover 80+80 MHz or more complex restrictions.
 800	 *	 Note that such restrictions also need to be advertised to
 801	 *	 userspace, for example for P2P channel selection.
 802	 */
 803
 804	if (width > 20)
 805		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
 806
 807	/* 5 and 10 MHz are only defined for the OFDM PHY */
 808	if (width < 20)
 809		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
 810
 811
 812	if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
 813					 width, prohibited_flags))
 
 
 814		return false;
 815
 816	if (!chandef->center_freq2)
 817		return true;
 818	return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
 819					   width, prohibited_flags);
 
 
 820}
 821EXPORT_SYMBOL(cfg80211_chandef_usable);
 822
 823/*
 824 * Check if the channel can be used under permissive conditions mandated by
 825 * some regulatory bodies, i.e., the channel is marked with
 826 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
 827 * associated to an AP on the same channel or on the same UNII band
 828 * (assuming that the AP is an authorized master).
 829 * In addition allow operation on a channel on which indoor operation is
 830 * allowed, iff we are currently operating in an indoor environment.
 831 */
 832static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
 833					enum nl80211_iftype iftype,
 834					struct ieee80211_channel *chan)
 835{
 836	struct wireless_dev *wdev;
 837	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
 838
 839	ASSERT_RTNL();
 840
 841	if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
 842	    !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
 843		return false;
 844
 845	/* only valid for GO and TDLS off-channel (station/p2p-CL) */
 846	if (iftype != NL80211_IFTYPE_P2P_GO &&
 847	    iftype != NL80211_IFTYPE_STATION &&
 848	    iftype != NL80211_IFTYPE_P2P_CLIENT)
 849		return false;
 850
 851	if (regulatory_indoor_allowed() &&
 852	    (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
 853		return true;
 854
 855	if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
 856		return false;
 857
 858	/*
 859	 * Generally, it is possible to rely on another device/driver to allow
 860	 * the IR concurrent relaxation, however, since the device can further
 861	 * enforce the relaxation (by doing a similar verifications as this),
 862	 * and thus fail the GO instantiation, consider only the interfaces of
 863	 * the current registered device.
 864	 */
 865	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
 866		struct ieee80211_channel *other_chan = NULL;
 867		int r1, r2;
 868
 869		wdev_lock(wdev);
 870		if (wdev->iftype == NL80211_IFTYPE_STATION &&
 871		    wdev->current_bss)
 872			other_chan = wdev->current_bss->pub.channel;
 873
 874		/*
 875		 * If a GO already operates on the same GO_CONCURRENT channel,
 876		 * this one (maybe the same one) can beacon as well. We allow
 877		 * the operation even if the station we relied on with
 878		 * GO_CONCURRENT is disconnected now. But then we must make sure
 879		 * we're not outdoor on an indoor-only channel.
 880		 */
 881		if (iftype == NL80211_IFTYPE_P2P_GO &&
 882		    wdev->iftype == NL80211_IFTYPE_P2P_GO &&
 883		    wdev->beacon_interval &&
 884		    !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
 885			other_chan = wdev->chandef.chan;
 886		wdev_unlock(wdev);
 887
 888		if (!other_chan)
 889			continue;
 890
 891		if (chan == other_chan)
 892			return true;
 893
 894		if (chan->band != NL80211_BAND_5GHZ)
 
 895			continue;
 896
 897		r1 = cfg80211_get_unii(chan->center_freq);
 898		r2 = cfg80211_get_unii(other_chan->center_freq);
 899
 900		if (r1 != -EINVAL && r1 == r2) {
 901			/*
 902			 * At some locations channels 149-165 are considered a
 903			 * bundle, but at other locations, e.g., Indonesia,
 904			 * channels 149-161 are considered a bundle while
 905			 * channel 165 is left out and considered to be in a
 906			 * different bundle. Thus, in case that there is a
 907			 * station interface connected to an AP on channel 165,
 908			 * it is assumed that channels 149-161 are allowed for
 909			 * GO operations. However, having a station interface
 910			 * connected to an AP on channels 149-161, does not
 911			 * allow GO operation on channel 165.
 912			 */
 913			if (chan->center_freq == 5825 &&
 914			    other_chan->center_freq != 5825)
 915				continue;
 916			return true;
 917		}
 918	}
 919
 920	return false;
 921}
 922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 923static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
 924				     struct cfg80211_chan_def *chandef,
 925				     enum nl80211_iftype iftype,
 926				     bool check_no_ir)
 
 927{
 928	bool res;
 929	u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
 930			       IEEE80211_CHAN_RADAR;
 
 
 
 931
 932	trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
 
 
 933
 934	if (check_no_ir)
 935		prohibited_flags |= IEEE80211_CHAN_NO_IR;
 936
 937	if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
 938	    cfg80211_chandef_dfs_available(wiphy, chandef)) {
 939		/* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
 940		prohibited_flags = IEEE80211_CHAN_DISABLED;
 
 941	}
 942
 943	res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
 
 
 
 
 
 
 
 944
 945	trace_cfg80211_return_bool(res);
 946	return res;
 947}
 948
 949bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
 950			     struct cfg80211_chan_def *chandef,
 951			     enum nl80211_iftype iftype)
 952{
 953	return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
 954}
 955EXPORT_SYMBOL(cfg80211_reg_can_beacon);
 956
 957bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
 958				   struct cfg80211_chan_def *chandef,
 959				   enum nl80211_iftype iftype)
 960{
 961	bool check_no_ir;
 962
 963	ASSERT_RTNL();
 964
 965	/*
 966	 * Under certain conditions suggested by some regulatory bodies a
 967	 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
 968	 * only if such relaxations are not enabled and the conditions are not
 969	 * met.
 970	 */
 971	check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
 972						   chandef->chan);
 
 
 
 973
 974	return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
 
 
 
 
 
 975}
 976EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
 977
 978int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
 
 979				 struct cfg80211_chan_def *chandef)
 980{
 981	if (!rdev->ops->set_monitor_channel)
 982		return -EOPNOTSUPP;
 983	if (!cfg80211_has_monitors_only(rdev))
 984		return -EBUSY;
 985
 986	return rdev_set_monitor_channel(rdev, chandef);
 987}
 988
 989void
 990cfg80211_get_chan_state(struct wireless_dev *wdev,
 991		        struct ieee80211_channel **chan,
 992		        enum cfg80211_chan_mode *chanmode,
 993		        u8 *radar_detect)
 994{
 995	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 996
 997	*chan = NULL;
 998	*chanmode = CHAN_MODE_UNDEFINED;
 999
1000	ASSERT_WDEV_LOCK(wdev);
 
 
1001
1002	if (wdev->netdev && !netif_running(wdev->netdev))
1003		return;
 
 
 
 
 
 
 
 
 
1004
1005	switch (wdev->iftype) {
 
 
1006	case NL80211_IFTYPE_ADHOC:
1007		if (wdev->current_bss) {
1008			*chan = wdev->current_bss->pub.channel;
1009			*chanmode = (wdev->ibss_fixed &&
1010				     !wdev->ibss_dfs_possible)
1011				  ? CHAN_MODE_SHARED
1012				  : CHAN_MODE_EXCLUSIVE;
1013
1014			/* consider worst-case - IBSS can try to return to the
1015			 * original user-specified channel as creator */
1016			if (wdev->ibss_dfs_possible)
1017				*radar_detect |= BIT(wdev->chandef.width);
1018			return;
1019		}
1020		break;
1021	case NL80211_IFTYPE_STATION:
1022	case NL80211_IFTYPE_P2P_CLIENT:
1023		if (wdev->current_bss) {
1024			*chan = wdev->current_bss->pub.channel;
1025			*chanmode = CHAN_MODE_SHARED;
1026			return;
1027		}
1028		break;
1029	case NL80211_IFTYPE_AP:
1030	case NL80211_IFTYPE_P2P_GO:
1031		if (wdev->cac_started) {
1032			*chan = wdev->chandef.chan;
1033			*chanmode = CHAN_MODE_SHARED;
1034			*radar_detect |= BIT(wdev->chandef.width);
1035		} else if (wdev->beacon_interval) {
1036			*chan = wdev->chandef.chan;
1037			*chanmode = CHAN_MODE_SHARED;
1038
1039			ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1040							    &wdev->chandef,
1041							    wdev->iftype);
1042			WARN_ON(ret < 0);
1043			if (ret > 0)
1044				*radar_detect |= BIT(wdev->chandef.width);
1045		}
1046		return;
1047	case NL80211_IFTYPE_MESH_POINT:
1048		if (wdev->mesh_id_len) {
1049			*chan = wdev->chandef.chan;
1050			*chanmode = CHAN_MODE_SHARED;
1051
1052			ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1053							    &wdev->chandef,
1054							    wdev->iftype);
1055			WARN_ON(ret < 0);
1056			if (ret > 0)
1057				*radar_detect |= BIT(wdev->chandef.width);
1058		}
1059		return;
1060	case NL80211_IFTYPE_OCB:
1061		if (wdev->chandef.chan) {
1062			*chan = wdev->chandef.chan;
1063			*chanmode = CHAN_MODE_SHARED;
1064			return;
1065		}
1066		break;
1067	case NL80211_IFTYPE_MONITOR:
1068	case NL80211_IFTYPE_AP_VLAN:
1069	case NL80211_IFTYPE_WDS:
1070	case NL80211_IFTYPE_P2P_DEVICE:
1071	case NL80211_IFTYPE_NAN:
1072		/* these interface types don't really have a channel */
1073		return;
1074	case NL80211_IFTYPE_UNSPECIFIED:
1075	case NUM_NL80211_IFTYPES:
1076		WARN_ON(1);
1077	}
1078}