reg.c 82.6 KB
Newer Older
1 2 3 4
/*
 * Copyright 2002-2005, Instant802 Networks, Inc.
 * Copyright 2005-2006, Devicescape Software, Inc.
 * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
5
 * Copyright 2008-2011	Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
6
 * Copyright 2013-2014  Intel Mobile Communications GmbH
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 20
 */

21

22 23
/**
 * DOC: Wireless regulatory infrastructure
24 25 26 27 28 29
 *
 * The usual implementation is for a driver to read a device EEPROM to
 * determine which regulatory domain it should be operating under, then
 * looking up the allowable channels in a driver-local table and finally
 * registering those channels in the wiphy structure.
 *
30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * Another set of compliance enforcement is for drivers to use their
 * own compliance limits which can be stored on the EEPROM. The host
 * driver or firmware may ensure these are used.
 *
 * In addition to all this we provide an extra layer of regulatory
 * conformance. For drivers which do not have any regulatory
 * information CRDA provides the complete regulatory solution.
 * For others it provides a community effort on further restrictions
 * to enhance compliance.
 *
 * Note: When number of rules --> infinity we will not be able to
 * index on alpha2 any more, instead we'll probably have to
 * rely on some SHA1 checksum of the regdomain for example.
 *
44
 */
45 46 47

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

48
#include <linux/kernel.h>
49
#include <linux/export.h>
50
#include <linux/slab.h>
51
#include <linux/list.h>
52
#include <linux/ctype.h>
53 54
#include <linux/nl80211.h>
#include <linux/platform_device.h>
55
#include <linux/moduleparam.h>
56
#include <net/cfg80211.h>
57
#include "core.h"
58
#include "reg.h"
59
#include "rdev-ops.h"
60
#include "regdb.h"
61
#include "nl80211.h"
62

63
#ifdef CONFIG_CFG80211_REG_DEBUG
64 65
#define REG_DBG_PRINT(format, args...)			\
	printk(KERN_DEBUG pr_fmt(format), ##args)
66
#else
67
#define REG_DBG_PRINT(args...)
68 69
#endif

70 71 72 73 74 75
/*
 * Grace period we give before making sure all current interfaces reside on
 * channels allowed by the current regulatory domain.
 */
#define REG_ENFORCE_GRACE_MS 60000

76 77 78 79 80 81 82 83 84 85
/**
 * enum reg_request_treatment - regulatory request treatment
 *
 * @REG_REQ_OK: continue processing the regulatory request
 * @REG_REQ_IGNORE: ignore the regulatory request
 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
 *	be intersected with the current one.
 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
 *	regulatory settings, and no further processing is required.
 */
86 87 88 89 90 91 92
enum reg_request_treatment {
	REG_REQ_OK,
	REG_REQ_IGNORE,
	REG_REQ_INTERSECT,
	REG_REQ_ALREADY_SET,
};

93 94 95 96 97 98 99 100 101
static struct regulatory_request core_request_world = {
	.initiator = NL80211_REGDOM_SET_BY_CORE,
	.alpha2[0] = '0',
	.alpha2[1] = '0',
	.intersect = false,
	.processed = true,
	.country_ie_env = ENVIRON_ANY,
};

Johannes Berg's avatar
Johannes Berg committed
102 103 104 105
/*
 * Receipt of information from last regulatory request,
 * protected by RTNL (and can be accessed with RCU protection)
 */
106
static struct regulatory_request __rcu *last_request =
107
	(void __force __rcu *)&core_request_world;
108

109 110
/* To trigger userspace events */
static struct platform_device *reg_pdev;
111

112 113
/*
 * Central wireless core regulatory domains, we only need two,
114
 * the current one and a world regulatory domain in case we have no
115
 * information to give us an alpha2.
Johannes Berg's avatar
Johannes Berg committed
116
 * (protected by RTNL, can be read under RCU)
117
 */
118
const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
119

120 121 122
/*
 * Number of devices that registered to the core
 * that support cellular base station regulatory hints
Johannes Berg's avatar
Johannes Berg committed
123
 * (protected by RTNL)
124 125 126
 */
static int reg_num_devs_support_basehint;

127 128 129 130 131 132
/*
 * State variable indicating if the platform on which the devices
 * are attached is operating in an indoor environment. The state variable
 * is relevant for all registered devices.
 */
static bool reg_is_indoor;
133 134 135 136
static spinlock_t reg_indoor_lock;

/* Used to track the userspace process controlling the indoor setting */
static u32 reg_is_indoor_portid;
137

138 139
static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
{
Johannes Berg's avatar
Johannes Berg committed
140
	return rtnl_dereference(cfg80211_regdomain);
141 142
}

143
const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
144
{
Johannes Berg's avatar
Johannes Berg committed
145
	return rtnl_dereference(wiphy->regd);
146 147
}

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
{
	switch (dfs_region) {
	case NL80211_DFS_UNSET:
		return "unset";
	case NL80211_DFS_FCC:
		return "FCC";
	case NL80211_DFS_ETSI:
		return "ETSI";
	case NL80211_DFS_JP:
		return "JP";
	}
	return "Unknown";
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
{
	const struct ieee80211_regdomain *regd = NULL;
	const struct ieee80211_regdomain *wiphy_regd = NULL;

	regd = get_cfg80211_regdom();
	if (!wiphy)
		goto out;

	wiphy_regd = get_wiphy_regdom(wiphy);
	if (!wiphy_regd)
		goto out;

	if (wiphy_regd->dfs_region == regd->dfs_region)
		goto out;

	REG_DBG_PRINT("%s: device specific dfs_region "
		      "(%s) disagrees with cfg80211's "
		      "central dfs_region (%s)\n",
		      dev_name(&wiphy->dev),
		      reg_dfs_region_str(wiphy_regd->dfs_region),
		      reg_dfs_region_str(regd->dfs_region));

out:
	return regd->dfs_region;
}

190 191 192 193 194 195 196
static void rcu_free_regdom(const struct ieee80211_regdomain *r)
{
	if (!r)
		return;
	kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
}

197 198
static struct regulatory_request *get_last_request(void)
{
Johannes Berg's avatar
Johannes Berg committed
199
	return rcu_dereference_rtnl(last_request);
200 201
}

202
/* Used to queue up regulatory hints */
203 204 205
static LIST_HEAD(reg_requests_list);
static spinlock_t reg_requests_lock;

206 207 208 209 210 211 212 213 214 215 216 217
/* Used to queue up beacon hints for review */
static LIST_HEAD(reg_pending_beacons);
static spinlock_t reg_pending_beacons_lock;

/* Used to keep track of processed beacon hints */
static LIST_HEAD(reg_beacon_list);

struct reg_beacon {
	struct list_head list;
	struct ieee80211_channel chan;
};

218 219 220
static void reg_check_chans_work(struct work_struct *work);
static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);

221 222 223
static void reg_todo(struct work_struct *work);
static DECLARE_WORK(reg_work, reg_todo);

224 225 226
static void reg_timeout_work(struct work_struct *work);
static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);

227 228
/* We keep a static world regulatory domain in case of the absence of CRDA */
static const struct ieee80211_regdomain world_regdom = {
229
	.n_reg_rules = 6,
230 231
	.alpha2 =  "00",
	.reg_rules = {
232 233
		/* IEEE 802.11b/g, channels 1..11 */
		REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
234 235
		/* IEEE 802.11b/g, channels 12..13. */
		REG_RULE(2467-10, 2472+10, 40, 6, 20,
236
			NL80211_RRF_NO_IR),
237 238 239
		/* IEEE 802.11 channel 14 - Only JP enables
		 * this and for 802.11b only */
		REG_RULE(2484-10, 2484+10, 20, 6, 20,
240
			NL80211_RRF_NO_IR |
241 242
			NL80211_RRF_NO_OFDM),
		/* IEEE 802.11a, channel 36..48 */
243
		REG_RULE(5180-10, 5240+10, 160, 6, 20,
244
                        NL80211_RRF_NO_IR),
245

246 247
		/* IEEE 802.11a, channel 52..64 - DFS required */
		REG_RULE(5260-10, 5320+10, 160, 6, 20,
248
			NL80211_RRF_NO_IR |
249 250 251 252
			NL80211_RRF_DFS),

		/* IEEE 802.11a, channel 100..144 - DFS required */
		REG_RULE(5500-10, 5720+10, 160, 6, 20,
253
			NL80211_RRF_NO_IR |
254
			NL80211_RRF_DFS),
255 256

		/* IEEE 802.11a, channel 149..165 */
257
		REG_RULE(5745-10, 5825+10, 80, 6, 20,
258
			NL80211_RRF_NO_IR),
259 260 261

		/* IEEE 802.11ad (60gHz), channels 1..3 */
		REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
262 263 264
	}
};

Johannes Berg's avatar
Johannes Berg committed
265
/* protected by RTNL */
266 267
static const struct ieee80211_regdomain *cfg80211_world_regdom =
	&world_regdom;
268

269
static char *ieee80211_regdom = "00";
270
static char user_alpha2[2];
271

272 273 274
module_param(ieee80211_regdom, charp, 0444);
MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");

275
static void reg_free_request(struct regulatory_request *request)
276
{
277 278 279 280 281 282 283 284
	if (request != get_last_request())
		kfree(request);
}

static void reg_free_last_request(void)
{
	struct regulatory_request *lr = get_last_request();

285 286 287 288
	if (lr != &core_request_world && lr)
		kfree_rcu(lr, rcu_head);
}

289 290
static void reg_update_last_request(struct regulatory_request *request)
{
291 292 293 294 295 296
	struct regulatory_request *lr;

	lr = get_last_request();
	if (lr == request)
		return;

297
	reg_free_last_request();
298 299 300
	rcu_assign_pointer(last_request, request);
}

301 302
static void reset_regdomains(bool full_reset,
			     const struct ieee80211_regdomain *new_regdom)
303
{
304 305
	const struct ieee80211_regdomain *r;

Johannes Berg's avatar
Johannes Berg committed
306
	ASSERT_RTNL();
307

308 309
	r = get_cfg80211_regdom();

310
	/* avoid freeing static information or freeing something twice */
311 312
	if (r == cfg80211_world_regdom)
		r = NULL;
313 314
	if (cfg80211_world_regdom == &world_regdom)
		cfg80211_world_regdom = NULL;
315 316
	if (r == &world_regdom)
		r = NULL;
317

318 319
	rcu_free_regdom(r);
	rcu_free_regdom(cfg80211_world_regdom);
320

321
	cfg80211_world_regdom = &world_regdom;
322
	rcu_assign_pointer(cfg80211_regdomain, new_regdom);
323 324 325 326

	if (!full_reset)
		return;

327
	reg_update_last_request(&core_request_world);
328 329
}

330 331 332 333
/*
 * Dynamic world regulatory domain requested by the wireless
 * core upon initialization
 */
334
static void update_world_regdomain(const struct ieee80211_regdomain *rd)
335
{
336
	struct regulatory_request *lr;
337

338 339 340
	lr = get_last_request();

	WARN_ON(!lr);
341

342
	reset_regdomains(false, rd);
343 344 345 346

	cfg80211_world_regdom = rd;
}

347
bool is_world_regdom(const char *alpha2)
348 349 350
{
	if (!alpha2)
		return false;
Johannes Berg's avatar
Johannes Berg committed
351
	return alpha2[0] == '0' && alpha2[1] == '0';
352
}
353

354
static bool is_alpha2_set(const char *alpha2)
355 356 357
{
	if (!alpha2)
		return false;
Johannes Berg's avatar
Johannes Berg committed
358
	return alpha2[0] && alpha2[1];
359
}
360

361
static bool is_unknown_alpha2(const char *alpha2)
362 363 364
{
	if (!alpha2)
		return false;
365 366 367 368
	/*
	 * Special case where regulatory domain was built by driver
	 * but a specific alpha2 cannot be determined
	 */
Johannes Berg's avatar
Johannes Berg committed
369
	return alpha2[0] == '9' && alpha2[1] == '9';
370
}
371

372 373 374 375
static bool is_intersected_alpha2(const char *alpha2)
{
	if (!alpha2)
		return false;
376 377
	/*
	 * Special case where regulatory domain is the
378
	 * result of an intersection between two regulatory domain
379 380
	 * structures
	 */
Johannes Berg's avatar
Johannes Berg committed
381
	return alpha2[0] == '9' && alpha2[1] == '8';
382 383
}

384
static bool is_an_alpha2(const char *alpha2)
385 386 387
{
	if (!alpha2)
		return false;
Johannes Berg's avatar
Johannes Berg committed
388
	return isalpha(alpha2[0]) && isalpha(alpha2[1]);
389
}
390

391
static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
392 393 394
{
	if (!alpha2_x || !alpha2_y)
		return false;
Johannes Berg's avatar
Johannes Berg committed
395
	return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
396 397
}

398
static bool regdom_changes(const char *alpha2)
399
{
400
	const struct ieee80211_regdomain *r = get_cfg80211_regdom();
401

402
	if (!r)
403
		return true;
404
	return !alpha2_equal(r->alpha2, alpha2);
405 406
}

407 408 409 410 411 412 413 414 415 416 417
/*
 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
 * has ever been issued.
 */
static bool is_user_regdom_saved(void)
{
	if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
		return false;

	/* This would indicate a mistake on the design */
Johannes Berg's avatar
Johannes Berg committed
418
	if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
419
		 "Unexpected user alpha2: %c%c\n",
Johannes Berg's avatar
Johannes Berg committed
420
		 user_alpha2[0], user_alpha2[1]))
421 422 423 424 425
		return false;

	return true;
}

426 427
static const struct ieee80211_regdomain *
reg_copy_regd(const struct ieee80211_regdomain *src_regd)
428 429
{
	struct ieee80211_regdomain *regd;
430
	int size_of_regd;
431 432
	unsigned int i;

433 434 435
	size_of_regd =
		sizeof(struct ieee80211_regdomain) +
		src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
436 437 438

	regd = kzalloc(size_of_regd, GFP_KERNEL);
	if (!regd)
439
		return ERR_PTR(-ENOMEM);
440 441 442 443 444

	memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));

	for (i = 0; i < src_regd->n_reg_rules; i++)
		memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
445
		       sizeof(struct ieee80211_reg_rule));
446

447
	return regd;
448 449 450 451 452 453 454 455 456
}

#ifdef CONFIG_CFG80211_INTERNAL_REGDB
struct reg_regdb_search_request {
	char alpha2[2];
	struct list_head list;
};

static LIST_HEAD(reg_regdb_search_list);
457
static DEFINE_MUTEX(reg_regdb_search_mutex);
458 459 460 461

static void reg_regdb_search(struct work_struct *work)
{
	struct reg_regdb_search_request *request;
462 463
	const struct ieee80211_regdomain *curdom, *regdom = NULL;
	int i;
464

465
	rtnl_lock();
466

467
	mutex_lock(&reg_regdb_search_mutex);
468 469 470 471 472 473
	while (!list_empty(&reg_regdb_search_list)) {
		request = list_first_entry(&reg_regdb_search_list,
					   struct reg_regdb_search_request,
					   list);
		list_del(&request->list);

Johannes Berg's avatar
Johannes Berg committed
474
		for (i = 0; i < reg_regdb_size; i++) {
475 476
			curdom = reg_regdb[i];

Johannes Berg's avatar
Johannes Berg committed
477
			if (alpha2_equal(request->alpha2, curdom->alpha2)) {
478
				regdom = reg_copy_regd(curdom);
479 480 481 482 483 484
				break;
			}
		}

		kfree(request);
	}
485
	mutex_unlock(&reg_regdb_search_mutex);
486

487
	if (!IS_ERR_OR_NULL(regdom))
488 489
		set_regdom(regdom);

490
	rtnl_unlock();
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
}

static DECLARE_WORK(reg_regdb_work, reg_regdb_search);

static void reg_regdb_query(const char *alpha2)
{
	struct reg_regdb_search_request *request;

	if (!alpha2)
		return;

	request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
	if (!request)
		return;

	memcpy(request->alpha2, alpha2, 2);

508
	mutex_lock(&reg_regdb_search_mutex);
509
	list_add_tail(&request->list, &reg_regdb_search_list);
510
	mutex_unlock(&reg_regdb_search_mutex);
511 512 513

	schedule_work(&reg_regdb_work);
}
514 515 516 517 518 519 520

/* Feel free to add any other sanity checks here */
static void reg_regdb_size_check(void)
{
	/* We should ideally BUILD_BUG_ON() but then random builds would fail */
	WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
}
521
#else
522
static inline void reg_regdb_size_check(void) {}
523 524 525
static inline void reg_regdb_query(const char *alpha2) {}
#endif /* CONFIG_CFG80211_INTERNAL_REGDB */

526 527
/*
 * This lets us keep regulatory code which is updated on a regulatory
528
 * basis in userspace.
529
 */
530 531
static int call_crda(const char *alpha2)
{
532 533 534 535 536 537
	char country[12];
	char *env[] = { country, NULL };

	snprintf(country, sizeof(country), "COUNTRY=%c%c",
		 alpha2[0], alpha2[1]);

538
	if (!is_world_regdom((char *) alpha2))
539
		pr_info("Calling CRDA for country: %c%c\n",
540 541
			alpha2[0], alpha2[1]);
	else
542
		pr_info("Calling CRDA to update world regulatory domain\n");
543

544 545 546
	/* query internal regulatory database (if it exists) */
	reg_regdb_query(alpha2);

547
	return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
548 549
}

550 551 552 553 554
static enum reg_request_treatment
reg_call_crda(struct regulatory_request *request)
{
	if (call_crda(request->alpha2))
		return REG_REQ_IGNORE;
555 556 557

	queue_delayed_work(system_power_efficient_wq,
			   &reg_timeout, msecs_to_jiffies(3142));
558 559 560
	return REG_REQ_OK;
}

561
bool reg_is_valid_request(const char *alpha2)
562
{
563
	struct regulatory_request *lr = get_last_request();
564

565
	if (!lr || lr->processed)
566 567
		return false;

568
	return alpha2_equal(lr->alpha2, alpha2);
569
}
570

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
{
	struct regulatory_request *lr = get_last_request();

	/*
	 * Follow the driver's regulatory domain, if present, unless a country
	 * IE has been processed or a user wants to help complaince further
	 */
	if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
	    lr->initiator != NL80211_REGDOM_SET_BY_USER &&
	    wiphy->regd)
		return get_wiphy_regdom(wiphy);

	return get_cfg80211_regdom();
}

587 588 589
static unsigned int
reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
				 const struct ieee80211_reg_rule *rule)
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
{
	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
	const struct ieee80211_freq_range *freq_range_tmp;
	const struct ieee80211_reg_rule *tmp;
	u32 start_freq, end_freq, idx, no;

	for (idx = 0; idx < rd->n_reg_rules; idx++)
		if (rule == &rd->reg_rules[idx])
			break;

	if (idx == rd->n_reg_rules)
		return 0;

	/* get start_freq */
	no = idx;

	while (no) {
		tmp = &rd->reg_rules[--no];
		freq_range_tmp = &tmp->freq_range;

		if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
			break;

		freq_range = freq_range_tmp;
	}

	start_freq = freq_range->start_freq_khz;

	/* get end_freq */
	freq_range = &rule->freq_range;
	no = idx;

	while (no < rd->n_reg_rules - 1) {
		tmp = &rd->reg_rules[++no];
		freq_range_tmp = &tmp->freq_range;

		if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
			break;

		freq_range = freq_range_tmp;
	}

	end_freq = freq_range->end_freq_khz;

	return end_freq - start_freq;
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
				   const struct ieee80211_reg_rule *rule)
{
	unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);

	if (rule->flags & NL80211_RRF_NO_160MHZ)
		bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
	if (rule->flags & NL80211_RRF_NO_80MHZ)
		bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));

	/*
	 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
	 * are not allowed.
	 */
	if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
	    rule->flags & NL80211_RRF_NO_HT40PLUS)
		bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));

	return bw;
}

658
/* Sanity check on a regulatory rule */
659
static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
660
{
661
	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
662 663
	u32 freq_diff;

664
	if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
665 666 667 668 669 670 671
		return false;

	if (freq_range->start_freq_khz > freq_range->end_freq_khz)
		return false;

	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;

672
	if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg's avatar
Johannes Berg committed
673
	    freq_range->max_bandwidth_khz > freq_diff)
674 675 676 677 678
		return false;

	return true;
}

679
static bool is_valid_rd(const struct ieee80211_regdomain *rd)
680
{
681
	const struct ieee80211_reg_rule *reg_rule = NULL;
682
	unsigned int i;
683

684 685
	if (!rd->n_reg_rules)
		return false;
686

687 688 689
	if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
		return false;

690 691 692 693 694 695 696
	for (i = 0; i < rd->n_reg_rules; i++) {
		reg_rule = &rd->reg_rules[i];
		if (!is_valid_reg_rule(reg_rule))
			return false;
	}

	return true;
697 698
}

699
static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
700
			    u32 center_freq_khz, u32 bw_khz)
701
{
702 703 704 705 706 707 708 709 710 711
	u32 start_freq_khz, end_freq_khz;

	start_freq_khz = center_freq_khz - (bw_khz/2);
	end_freq_khz = center_freq_khz + (bw_khz/2);

	if (start_freq_khz >= freq_range->start_freq_khz &&
	    end_freq_khz <= freq_range->end_freq_khz)
		return true;

	return false;
712
}
713

714 715 716 717 718 719 720
/**
 * freq_in_rule_band - tells us if a frequency is in a frequency band
 * @freq_range: frequency rule we want to query
 * @freq_khz: frequency we are inquiring about
 *
 * This lets us know if a specific frequency rule is or is not relevant to
 * a specific frequency's band. Bands are device specific and artificial
721 722 723 724 725
 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
 * however it is safe for now to assume that a frequency rule should not be
 * part of a frequency's band if the start freq or end freq are off by more
 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
 * 60 GHz band.
726 727 728 729
 * This resolution can be lowered and should be considered as we add
 * regulatory rule support for other "bands".
 **/
static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg's avatar
Johannes Berg committed
730
			      u32 freq_khz)
731 732
{
#define ONE_GHZ_IN_KHZ	1000000
733 734 735 736 737 738 739 740
	/*
	 * From 802.11ad: directional multi-gigabit (DMG):
	 * Pertaining to operation in a frequency band containing a channel
	 * with the Channel starting frequency above 45 GHz.
	 */
	u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
			10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
	if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
741
		return true;
742
	if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
743 744 745 746 747
		return true;
	return false;
#undef ONE_GHZ_IN_KHZ
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761
/*
 * Later on we can perhaps use the more restrictive DFS
 * region but we don't have information for that yet so
 * for now simply disallow conflicts.
 */
static enum nl80211_dfs_regions
reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
			 const enum nl80211_dfs_regions dfs_region2)
{
	if (dfs_region1 != dfs_region2)
		return NL80211_DFS_UNSET;
	return dfs_region1;
}

762 763 764 765
/*
 * Helper for regdom_intersect(), this does the real
 * mathematical intersection fun
 */
766 767 768
static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
			       const struct ieee80211_regdomain *rd2,
			       const struct ieee80211_reg_rule *rule1,
Johannes Berg's avatar
Johannes Berg committed
769 770
			       const struct ieee80211_reg_rule *rule2,
			       struct ieee80211_reg_rule *intersected_rule)
771 772 773 774 775
{
	const struct ieee80211_freq_range *freq_range1, *freq_range2;
	struct ieee80211_freq_range *freq_range;
	const struct ieee80211_power_rule *power_rule1, *power_rule2;
	struct ieee80211_power_rule *power_rule;
776
	u32 freq_diff, max_bandwidth1, max_bandwidth2;
777 778 779 780 781 782 783 784 785 786

	freq_range1 = &rule1->freq_range;
	freq_range2 = &rule2->freq_range;
	freq_range = &intersected_rule->freq_range;

	power_rule1 = &rule1->power_rule;
	power_rule2 = &rule2->power_rule;
	power_rule = &intersected_rule->power_rule;

	freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg's avatar
Johannes Berg committed
787
					 freq_range2->start_freq_khz);
788
	freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg's avatar
Johannes Berg committed
789
				       freq_range2->end_freq_khz);
790 791 792 793

	max_bandwidth1 = freq_range1->max_bandwidth_khz;
	max_bandwidth2 = freq_range2->max_bandwidth_khz;

794 795 796 797
	if (rule1->flags & NL80211_RRF_AUTO_BW)
		max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
	if (rule2->flags & NL80211_RRF_AUTO_BW)
		max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
798 799

	freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
800

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	intersected_rule->flags = rule1->flags | rule2->flags;

	/*
	 * In case NL80211_RRF_AUTO_BW requested for both rules
	 * set AUTO_BW in intersected rule also. Next we will
	 * calculate BW correctly in handle_channel function.
	 * In other case remove AUTO_BW flag while we calculate
	 * maximum bandwidth correctly and auto calculation is
	 * not required.
	 */
	if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
	    (rule2->flags & NL80211_RRF_AUTO_BW))
		intersected_rule->flags |= NL80211_RRF_AUTO_BW;
	else
		intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;

817 818 819 820 821 822 823 824 825
	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
	if (freq_range->max_bandwidth_khz > freq_diff)
		freq_range->max_bandwidth_khz = freq_diff;

	power_rule->max_eirp = min(power_rule1->max_eirp,
		power_rule2->max_eirp);
	power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
		power_rule2->max_antenna_gain);

826 827 828
	intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
					   rule2->dfs_cac_ms);

829 830 831 832 833 834
	if (!is_valid_reg_rule(intersected_rule))
		return -EINVAL;

	return 0;
}

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
/* check whether old rule contains new rule */
static bool rule_contains(struct ieee80211_reg_rule *r1,
			  struct ieee80211_reg_rule *r2)
{
	/* for simplicity, currently consider only same flags */
	if (r1->flags != r2->flags)
		return false;

	/* verify r1 is more restrictive */
	if ((r1->power_rule.max_antenna_gain >
	     r2->power_rule.max_antenna_gain) ||
	    r1->power_rule.max_eirp > r2->power_rule.max_eirp)
		return false;

	/* make sure r2's range is contained within r1 */
	if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
	    r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
		return false;

	/* and finally verify that r1.max_bw >= r2.max_bw */
	if (r1->freq_range.max_bandwidth_khz <
	    r2->freq_range.max_bandwidth_khz)
		return false;

	return true;
}

/* add or extend current rules. do nothing if rule is already contained */
static void add_rule(struct ieee80211_reg_rule *rule,
		     struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
{
	struct ieee80211_reg_rule *tmp_rule;
	int i;

	for (i = 0; i < *n_rules; i++) {
		tmp_rule = &reg_rules[i];
		/* rule is already contained - do nothing */
		if (rule_contains(tmp_rule, rule))
			return;

		/* extend rule if possible */
		if (rule_contains(rule, tmp_rule)) {
			memcpy(tmp_rule, rule, sizeof(*rule));
			return;
		}
	}

	memcpy(&reg_rules[*n_rules], rule, sizeof(*rule));
	(*n_rules)++;
}

886 887 888 889 890 891 892 893 894 895 896 897 898
/**
 * regdom_intersect - do the intersection between two regulatory domains
 * @rd1: first regulatory domain
 * @rd2: second regulatory domain
 *
 * Use this function to get the intersection between two regulatory domains.
 * Once completed we will mark the alpha2 for the rd as intersected, "98",
 * as no one single alpha2 can represent this regulatory domain.
 *
 * Returns a pointer to the regulatory domain structure which will hold the
 * resulting intersection of rules between rd1 and rd2. We will
 * kzalloc() this structure for you.
 */
Johannes Berg's avatar
Johannes Berg committed
899 900 901
static struct ieee80211_regdomain *
regdom_intersect(const struct ieee80211_regdomain *rd1,
		 const struct ieee80211_regdomain *rd2)
902 903 904
{
	int r, size_of_regd;
	unsigned int x, y;
905
	unsigned int num_rules = 0;
906
	const struct ieee80211_reg_rule *rule1, *rule2;
907
	struct ieee80211_reg_rule intersected_rule;
908 909 910 911 912
	struct ieee80211_regdomain *rd;

	if (!rd1 || !rd2)
		return NULL;

913 914
	/*
	 * First we get a count of the rules we'll need, then we actually
915 916 917
	 * build them. This is to so we can malloc() and free() a
	 * regdomain once. The reason we use reg_rules_intersect() here
	 * is it will return -EINVAL if the rule computed makes no sense.
918 919
	 * All rules that do check out OK are valid.
	 */
920 921 922 923 924

	for (x = 0; x < rd1->n_reg_rules; x++) {
		rule1 = &rd1->reg_rules[x];
		for (y = 0; y < rd2->n_reg_rules; y++) {
			rule2 = &rd2->reg_rules[y];
925
			if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
926
						 &intersected_rule))
927 928 929 930 931 932 933 934
				num_rules++;
		}
	}

	if (!num_rules)
		return NULL;

	size_of_regd = sizeof(struct ieee80211_regdomain) +
935
		       num_rules * sizeof(struct ieee80211_reg_rule);
936 937 938 939 940

	rd = kzalloc(size_of_regd, GFP_KERNEL);
	if (!rd)
		return NULL;

941
	for (x = 0; x < rd1->n_reg_rules; x++) {
942
		rule1 = &rd1->reg_rules[x];
943
		for (y = 0; y < rd2->n_reg_rules; y++) {
944
			rule2 = &rd2->reg_rules[y];
945
			r = reg_rules_intersect(rd1, rd2, rule1, rule2,
946
						&intersected_rule);
947 948 949 950
			/*
			 * No need to memset here the intersected rule here as
			 * we're not using the stack anymore
			 */
951 952 953
			if (r)
				continue;

954 955 956
			add_rule(&intersected_rule, rd->reg_rules,
				 &rd->n_reg_rules);
		}
957 958 959 960
	}

	rd->alpha2[0] = '9';
	rd->alpha2[1] = '8';
961 962
	rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
						  rd2->dfs_region);
963 964 965 966

	return rd;
}

967 968 969 970
/*
 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
 * want to just have the channel structure use these
 */
971 972 973
static u32 map_regdom_flags(u32 rd_flags)
{
	u32 channel_flags = 0;
974 975
	if (rd_flags & NL80211_RRF_NO_IR_ALL)
		channel_flags |= IEEE80211_CHAN_NO_IR;
976 977
	if (rd_flags & NL80211_RRF_DFS)
		channel_flags |= IEEE80211_CHAN_RADAR;
978 979
	if (rd_flags & NL80211_RRF_NO_OFDM)
		channel_flags |= IEEE80211_CHAN_NO_OFDM;
980 981
	if (rd_flags & NL80211_RRF_NO_OUTDOOR)
		channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
982 983 984 985 986 987 988 989 990 991
	if (rd_flags & NL80211_RRF_GO_CONCURRENT)
		channel_flags |= IEEE80211_CHAN_GO_CONCURRENT;
	if (rd_flags & NL80211_RRF_NO_HT40MINUS)
		channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
	if (rd_flags & NL80211_RRF_NO_HT40PLUS)
		channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
	if (rd_flags & NL80211_RRF_NO_80MHZ)
		channel_flags |= IEEE80211_CHAN_NO_80MHZ;
	if (rd_flags & NL80211_RRF_NO_160MHZ)
		channel_flags |= IEEE80211_CHAN_NO_160MHZ;
992 993 994
	return channel_flags;
}

995 996 997
static const struct ieee80211_reg_rule *
freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
		   const struct ieee80211_regdomain *regd)
998 999
{
	int i;
1000
	bool band_rule_found = false;
1001 1002
	bool bw_fits = false;

1003
	if (!regd)
1004
		return ERR_PTR(-EINVAL);
1005

1006
	for (i = 0; i < regd->n_reg_rules; i++) {
1007 1008 1009
		const struct ieee80211_reg_rule *rr;
		const struct ieee80211_freq_range *fr = NULL;