File Index
Files and directories
Sign in
FILE

chan.c

File preview with syntax highlighting.

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