Linux Audio

Check our new training course

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