reg.c 82.8 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
555
556
557
static enum reg_request_treatment
reg_call_crda(struct regulatory_request *request)
{
	if (call_crda(request->alpha2))
		return REG_REQ_IGNORE;
	return REG_REQ_OK;
}

558
bool reg_is_valid_request(const char *alpha2)
559
{
560
	struct regulatory_request *lr = get_last_request();
561

562
	if (!lr || lr->processed)
563
564
		return false;

565
	return alpha2_equal(lr->alpha2, alpha2);
566
}
567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
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();
}

584
585
586
static unsigned int
reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
				 const struct ieee80211_reg_rule *rule)
587
588
589
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
{
	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;
}

634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
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;
}

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

661
	if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
662
663
664
665
666
667
668
		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;

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

	return true;
}

676
static bool is_valid_rd(const struct ieee80211_regdomain *rd)
677
{
678
	const struct ieee80211_reg_rule *reg_rule = NULL;
679
	unsigned int i;
680

681
682
	if (!rd->n_reg_rules)
		return false;
683

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

687
688
689
690
691
692
693
	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;
694
695
}

696
static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
697
			    u32 center_freq_khz, u32 bw_khz)
698
{
699
700
701
702
703
704
705
706
707
708
	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;
709
}
710

711
712
713
714
715
716
717
/**
 * 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
718
719
720
721
722
 * 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.
723
724
725
726
 * 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
727
			      u32 freq_khz)
728
729
{
#define ONE_GHZ_IN_KHZ	1000000
730
731
732
733
734
735
736
737
	/*
	 * 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)
738
		return true;
739
	if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
740
741
742
743
744
		return true;
	return false;
#undef ONE_GHZ_IN_KHZ
}

745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
 * 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;
}

759
760
761
762
/*
 * Helper for regdom_intersect(), this does the real
 * mathematical intersection fun
 */
763
764
765
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
766
767
			       const struct ieee80211_reg_rule *rule2,
			       struct ieee80211_reg_rule *intersected_rule)
768
769
770
771
772
{
	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;
773
	u32 freq_diff, max_bandwidth1, max_bandwidth2;
774
775
776
777
778
779
780
781
782
783

	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
784
					 freq_range2->start_freq_khz);
785
	freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg's avatar
Johannes Berg committed
786
				       freq_range2->end_freq_khz);
787
788
789
790

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

791
792
793
794
	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);
795
796

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

798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
	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;

814
815
816
817
818
819
820
821
822
	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);

823
824
825
	intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
					   rule2->dfs_cac_ms);

826
827
828
829
830
831
	if (!is_valid_reg_rule(intersected_rule))
		return -EINVAL;

	return 0;
}

832
833
834
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
/* 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)++;
}

883
884
885
886
887
888
889
890
891
892
893
894
895
/**
 * 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
896
897
898
static struct ieee80211_regdomain *
regdom_intersect(const struct ieee80211_regdomain *rd1,
		 const struct ieee80211_regdomain *rd2)
899
900
901
{
	int r, size_of_regd;
	unsigned int x, y;
902
	unsigned int num_rules = 0;
903
	const struct ieee80211_reg_rule *rule1, *rule2;
904
	struct ieee80211_reg_rule intersected_rule;
905
906
907
908
909
	struct ieee80211_regdomain *rd;

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

910
911
	/*
	 * First we get a count of the rules we'll need, then we actually
912
913
914
	 * 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.
915
916
	 * All rules that do check out OK are valid.
	 */
917
918
919
920
921

	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];
922
			if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
923
						 &intersected_rule))
924
925
926
927
928
929
930
931
				num_rules++;
		}
	}

	if (!num_rules)
		return NULL;

	size_of_regd = sizeof(struct ieee80211_regdomain) +
932
		       num_rules * sizeof(struct ieee80211_reg_rule);
933
934
935
936
937

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

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

951
952
953
			add_rule(&intersected_rule, rd->reg_rules,
				 &rd->n_reg_rules);
		}
954
955
956
957
	}

	rd->alpha2[0] = '9';
	rd->alpha2[1] = '8';
958
959
	rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
						  rd2->dfs_region);
960
961
962
963

	return rd;
}

964
965
966
967
/*
 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
 * want to just have the channel structure use these
 */
968
969
970
static u32 map_regdom_flags(u32 rd_flags)
{
	u32 channel_flags = 0;
971
972
	if (rd_flags & NL80211_RRF_NO_IR_ALL)
		channel_flags |= IEEE80211_CHAN_NO_IR;
973
974
	if (rd_flags & NL80211_RRF_DFS)
		channel_flags |= IEEE80211_CHAN_RADAR;
975
976
	if (rd_flags & NL80211_RRF_NO_OFDM)
		channel_flags |= IEEE80211_CHAN_NO_OFDM;
977
978
	if (rd_flags & NL80211_RRF_NO_OUTDOOR)
		channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
979
980
981
982
983
984
985
986
987
988
	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;
989
990
991
	return channel_flags;
}

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

1000
	if (!regd)
1001
		return ERR_PTR(-EINVAL);
1002

1003
	for (i = 0; i < regd->n_reg_rules; i++) {
1004
1005
1006
		const struct ieee80211_reg_rule *rr;
		const struct ieee80211_freq_range *fr = NULL;

1007
		rr = &regd->reg_rules[i];
1008
		fr = &rr->freq_range;
1009

1010
1011
		/*
		 * We only need to know if one frequency rule was
1012
		 * was in center_freq's band, that's enough, so lets
1013
1014
		 * not overwrite it once found
		 */
1015
1016
1017
		if (!band_rule_found)
			band_rule_found = freq_in_rule_band(fr, center_freq);

1018
		bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
1019

1020
1021
		if (band_rule_found && bw_fits)
			return rr;
1022
1023
	}

1024
	if (!band_rule_found)
1025
		return ERR_PTR(-ERANGE);
1026

1027
	return ERR_PTR(-EINVAL);
1028
1029
}

1030
1031
const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
					       u32 center_freq)
1032
{
1033
	const struct ieee80211_regdomain *regd;
Johannes Berg's avatar
Johannes Berg committed
1034

1035
	regd = reg_get_regdomain(wiphy);
1036

1037
	return freq_reg_info_regd(wiphy, center_freq, regd);
1038
}
1039
EXPORT_SYMBOL(freq_reg_info);
1040

1041
const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
1042
1043
1044
{
	switch (initiator) {
	case NL80211_REGDOM_SET_BY_CORE:
1045
		return "core";
1046
	case NL80211_REGDOM_SET_BY_USER:
1047
		return "user";
1048
	case NL80211_REGDOM_SET_BY_DRIVER:
1049
		return "driver";
1050
	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1051
		return "country IE";
1052
1053
	default:
		WARN_ON(1);
1054
		return "bug";
1055
1056
	}
}
1057
EXPORT_SYMBOL(reg_initiator_name);
1058

1059
#ifdef CONFIG_CFG80211_REG_DEBUG
1060
1061
static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
				    struct ieee80211_channel *chan,
1062
1063
1064
1065
				    const struct ieee80211_reg_rule *reg_rule)
{
	const struct ieee80211_power_rule *power_rule;
	const struct ieee80211_freq_range *freq_range;
1066
	char max_antenna_gain[32], bw[32];
1067
1068
1069
1070
1071

	power_rule = &reg_rule->power_rule;
	freq_range = &reg_rule->freq_range;

	if (!power_rule->max_antenna_gain)
1072
		snprintf(max_antenna_gain, sizeof(max_antenna_gain), "N/A");
1073
	else
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
		snprintf(max_antenna_gain, sizeof(max_antenna_gain), "%d",
			 power_rule->max_antenna_gain);

	if (reg_rule->flags & NL80211_RRF_AUTO_BW)
		snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
			 freq_range->max_bandwidth_khz,
			 reg_get_max_bandwidth(regd, reg_rule));
	else
		snprintf(bw, sizeof(bw), "%d KHz",
			 freq_range->max_bandwidth_khz);
1084

1085
1086
	REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
		      chan->center_freq);
1087

1088
	REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
Johannes Berg's avatar
Johannes Berg committed
1089
		      freq_range->start_freq_khz, freq_range->end_freq_khz,
1090
		      bw, max_antenna_gain,
1091
1092
1093
		      power_rule->max_eirp);
}
#else
1094
1095
static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
				    struct ieee80211_channel *chan,
1096
1097
1098
1099
				    const struct ieee80211_reg_rule *reg_rule)
{
	return;
}
1100
1101
#endif

1102
1103
1104
1105
/*
 * Note that right now we assume the desired channel bandwidth
 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
 * per channel, the primary and the extension channel).
1106
 */
1107
1108
static void handle_channel(struct wiphy *wiphy,
			   enum nl80211_reg_initiator initiator,
Johannes Berg's avatar
Johannes Berg committed
1109
			   struct ieee80211_channel *chan)
1110
{
1111
	u32 flags, bw_flags = 0;
1112
1113
	const struct ieee80211_reg_rule *reg_rule = NULL;
	const struct ieee80211_power_rule *power_rule = NULL;
1114
	const struct ieee80211_freq_range *freq_range = NULL;
1115
	struct wiphy *request_wiphy = NULL;
1116
	struct regulatory_request *lr = get_last_request();
1117
1118
	const struct ieee80211_regdomain *regd;
	u32 max_bandwidth_khz;
1119

1120
	request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1121
1122

	flags = chan->orig_flags;
1123

1124
1125
	reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
	if (IS_ERR(reg_rule)) {
1126
1127
		/*
		 * We will disable all channels that do not match our
Lucas De Marchi's avatar
Lucas De Marchi committed
1128
		 * received regulatory rule unless the hint is coming
1129
1130
1131
1132
1133
1134
1135
1136
		 * from a Country IE and the Country IE had no information
		 * about a band. The IEEE 802.11 spec allows for an AP
		 * to send only a subset of the regulatory rules allowed,
		 * so an AP in the US that only supports 2.4 GHz may only send
		 * a country IE with information for the 2.4 GHz band
		 * while 5 GHz is still supported.
		 */
		if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1137
		    PTR_ERR(reg_rule) == -ERANGE)
1138
1139
			return;

1140
1141
		if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
		    request_wiphy && request_wiphy == wiphy &&
1142
		    request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
1143
1144
1145
1146
1147
1148
1149
1150
1151
			REG_DBG_PRINT("Disabling freq %d MHz for good\n",
				      chan->center_freq);
			chan->orig_flags |= IEEE80211_CHAN_DISABLED;
			chan->flags = chan->orig_flags;
		} else {
			REG_DBG_PRINT("Disabling freq %d MHz\n",
				      chan->center_freq);
			chan->flags |= IEEE80211_CHAN_DISABLED;
		}
1152
		return;