]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/wireless/reg.c
cfg80211: mark regdomains with > NL80211_MAX_SUPP_REG_RULES invalid
[mirror_ubuntu-bionic-kernel.git] / net / wireless / reg.c
CommitLineData
8318d78a
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
b2e1b302 5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
8318d78a
JB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
b2e1b302
LR
12/**
13 * DOC: Wireless regulatory infrastructure
8318d78a
JB
14 *
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
19 *
b2e1b302
LR
20 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
23 *
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
29 *
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
33 *
8318d78a
JB
34 */
35#include <linux/kernel.h>
b2e1b302
LR
36#include <linux/list.h>
37#include <linux/random.h>
38#include <linux/nl80211.h>
39#include <linux/platform_device.h>
8318d78a 40#include <net/wireless.h>
b2e1b302 41#include <net/cfg80211.h>
8318d78a 42#include "core.h"
b2e1b302 43#include "reg.h"
8318d78a 44
5166ccd2
LR
45/**
46 * struct regulatory_request - receipt of last regulatory request
47 *
48 * @wiphy: this is set if this request's initiator is
49 * %REGDOM_SET_BY_COUNTRY_IE or %REGDOM_SET_BY_DRIVER. This
50 * can be used by the wireless core to deal with conflicts
51 * and potentially inform users of which devices specifically
52 * cased the conflicts.
53 * @initiator: indicates who sent this request, could be any of
54 * of those set in reg_set_by, %REGDOM_SET_BY_*
55 * @alpha2: the ISO / IEC 3166 alpha2 country code of the requested
56 * regulatory domain. We have a few special codes:
57 * 00 - World regulatory domain
58 * 99 - built by driver but a specific alpha2 cannot be determined
59 * 98 - result of an intersection between two regulatory domains
60 * @intersect: indicates whether the wireless core should intersect
61 * the requested regulatory domain with the presently set regulatory
62 * domain.
be3d4810 63 */
734366de 64struct regulatory_request {
734366de 65 struct wiphy *wiphy;
734366de
JB
66 enum reg_set_by initiator;
67 char alpha2[2];
9c96477d 68 bool intersect;
734366de
JB
69};
70
5166ccd2 71/* Receipt of information from last regulatory request */
f6037d09 72static struct regulatory_request *last_request;
734366de 73
b2e1b302
LR
74/* To trigger userspace events */
75static struct platform_device *reg_pdev;
8318d78a 76
b2e1b302
LR
77/* Keep the ordering from large to small */
78static u32 supported_bandwidths[] = {
79 MHZ_TO_KHZ(40),
80 MHZ_TO_KHZ(20),
8318d78a
JB
81};
82
734366de
JB
83/* Central wireless core regulatory domains, we only need two,
84 * the current one and a world regulatory domain in case we have no
85 * information to give us an alpha2 */
a3d2eaf0 86static const struct ieee80211_regdomain *cfg80211_regdomain;
734366de
JB
87
88/* We keep a static world regulatory domain in case of the absence of CRDA */
89static const struct ieee80211_regdomain world_regdom = {
90 .n_reg_rules = 1,
91 .alpha2 = "00",
92 .reg_rules = {
93 REG_RULE(2412-10, 2462+10, 40, 6, 20,
94 NL80211_RRF_PASSIVE_SCAN |
95 NL80211_RRF_NO_IBSS),
96 }
97};
98
a3d2eaf0
JB
99static const struct ieee80211_regdomain *cfg80211_world_regdom =
100 &world_regdom;
734366de
JB
101
102#ifdef CONFIG_WIRELESS_OLD_REGULATORY
103static char *ieee80211_regdom = "US";
104module_param(ieee80211_regdom, charp, 0444);
105MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
106
107/* We assume 40 MHz bandwidth for the old regulatory work.
108 * We make emphasis we are using the exact same frequencies
109 * as before */
110
111static const struct ieee80211_regdomain us_regdom = {
112 .n_reg_rules = 6,
113 .alpha2 = "US",
114 .reg_rules = {
115 /* IEEE 802.11b/g, channels 1..11 */
116 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
117 /* IEEE 802.11a, channel 36 */
118 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
119 /* IEEE 802.11a, channel 40 */
120 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
121 /* IEEE 802.11a, channel 44 */
122 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
123 /* IEEE 802.11a, channels 48..64 */
124 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
125 /* IEEE 802.11a, channels 149..165, outdoor */
126 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
127 }
128};
129
130static const struct ieee80211_regdomain jp_regdom = {
131 .n_reg_rules = 3,
132 .alpha2 = "JP",
133 .reg_rules = {
134 /* IEEE 802.11b/g, channels 1..14 */
135 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
136 /* IEEE 802.11a, channels 34..48 */
137 REG_RULE(5170-10, 5240+10, 40, 6, 20,
138 NL80211_RRF_PASSIVE_SCAN),
139 /* IEEE 802.11a, channels 52..64 */
140 REG_RULE(5260-10, 5320+10, 40, 6, 20,
141 NL80211_RRF_NO_IBSS |
142 NL80211_RRF_DFS),
143 }
144};
145
146static const struct ieee80211_regdomain eu_regdom = {
147 .n_reg_rules = 6,
148 /* This alpha2 is bogus, we leave it here just for stupid
149 * backward compatibility */
150 .alpha2 = "EU",
151 .reg_rules = {
152 /* IEEE 802.11b/g, channels 1..13 */
153 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
154 /* IEEE 802.11a, channel 36 */
155 REG_RULE(5180-10, 5180+10, 40, 6, 23,
156 NL80211_RRF_PASSIVE_SCAN),
157 /* IEEE 802.11a, channel 40 */
158 REG_RULE(5200-10, 5200+10, 40, 6, 23,
159 NL80211_RRF_PASSIVE_SCAN),
160 /* IEEE 802.11a, channel 44 */
161 REG_RULE(5220-10, 5220+10, 40, 6, 23,
162 NL80211_RRF_PASSIVE_SCAN),
163 /* IEEE 802.11a, channels 48..64 */
164 REG_RULE(5240-10, 5320+10, 40, 6, 20,
165 NL80211_RRF_NO_IBSS |
166 NL80211_RRF_DFS),
167 /* IEEE 802.11a, channels 100..140 */
168 REG_RULE(5500-10, 5700+10, 40, 6, 30,
169 NL80211_RRF_NO_IBSS |
170 NL80211_RRF_DFS),
171 }
172};
173
174static const struct ieee80211_regdomain *static_regdom(char *alpha2)
175{
176 if (alpha2[0] == 'U' && alpha2[1] == 'S')
177 return &us_regdom;
178 if (alpha2[0] == 'J' && alpha2[1] == 'P')
179 return &jp_regdom;
180 if (alpha2[0] == 'E' && alpha2[1] == 'U')
181 return &eu_regdom;
182 /* Default, as per the old rules */
183 return &us_regdom;
184}
185
a3d2eaf0 186static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
734366de
JB
187{
188 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
189 return true;
190 return false;
191}
942b25cf
JB
192#else
193static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
734366de 194{
942b25cf 195 return false;
734366de 196}
942b25cf
JB
197#endif
198
734366de
JB
199static void reset_regdomains(void)
200{
942b25cf
JB
201 /* avoid freeing static information or freeing something twice */
202 if (cfg80211_regdomain == cfg80211_world_regdom)
203 cfg80211_regdomain = NULL;
204 if (cfg80211_world_regdom == &world_regdom)
205 cfg80211_world_regdom = NULL;
206 if (cfg80211_regdomain == &world_regdom)
207 cfg80211_regdomain = NULL;
208 if (is_old_static_regdom(cfg80211_regdomain))
209 cfg80211_regdomain = NULL;
210
211 kfree(cfg80211_regdomain);
212 kfree(cfg80211_world_regdom);
734366de 213
a3d2eaf0 214 cfg80211_world_regdom = &world_regdom;
734366de
JB
215 cfg80211_regdomain = NULL;
216}
217
218/* Dynamic world regulatory domain requested by the wireless
219 * core upon initialization */
a3d2eaf0 220static void update_world_regdomain(const struct ieee80211_regdomain *rd)
734366de 221{
f6037d09 222 BUG_ON(!last_request);
734366de
JB
223
224 reset_regdomains();
225
226 cfg80211_world_regdom = rd;
227 cfg80211_regdomain = rd;
228}
734366de 229
a3d2eaf0 230bool is_world_regdom(const char *alpha2)
b2e1b302
LR
231{
232 if (!alpha2)
233 return false;
234 if (alpha2[0] == '0' && alpha2[1] == '0')
235 return true;
236 return false;
237}
8318d78a 238
a3d2eaf0 239static bool is_alpha2_set(const char *alpha2)
b2e1b302
LR
240{
241 if (!alpha2)
242 return false;
243 if (alpha2[0] != 0 && alpha2[1] != 0)
244 return true;
245 return false;
246}
8318d78a 247
b2e1b302
LR
248static bool is_alpha_upper(char letter)
249{
250 /* ASCII A - Z */
251 if (letter >= 65 && letter <= 90)
252 return true;
253 return false;
254}
8318d78a 255
a3d2eaf0 256static bool is_unknown_alpha2(const char *alpha2)
b2e1b302
LR
257{
258 if (!alpha2)
259 return false;
260 /* Special case where regulatory domain was built by driver
261 * but a specific alpha2 cannot be determined */
262 if (alpha2[0] == '9' && alpha2[1] == '9')
263 return true;
264 return false;
265}
8318d78a 266
a3d2eaf0 267static bool is_an_alpha2(const char *alpha2)
b2e1b302
LR
268{
269 if (!alpha2)
270 return false;
271 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
272 return true;
273 return false;
274}
8318d78a 275
a3d2eaf0 276static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
b2e1b302
LR
277{
278 if (!alpha2_x || !alpha2_y)
279 return false;
280 if (alpha2_x[0] == alpha2_y[0] &&
281 alpha2_x[1] == alpha2_y[1])
282 return true;
283 return false;
284}
285
a3d2eaf0 286static bool regdom_changed(const char *alpha2)
b2e1b302
LR
287{
288 if (!cfg80211_regdomain)
289 return true;
290 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
291 return false;
292 return true;
293}
294
295/* This lets us keep regulatory code which is updated on a regulatory
296 * basis in userspace. */
297static int call_crda(const char *alpha2)
298{
299 char country_env[9 + 2] = "COUNTRY=";
300 char *envp[] = {
301 country_env,
302 NULL
303 };
304
305 if (!is_world_regdom((char *) alpha2))
306 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
307 alpha2[0], alpha2[1]);
308 else
b2e1b302
LR
309 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
310 "regulatory domain\n");
b2e1b302
LR
311
312 country_env[8] = alpha2[0];
313 country_env[9] = alpha2[1];
314
315 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
316}
317
b2e1b302 318/* Used by nl80211 before kmalloc'ing our regulatory domain */
a3d2eaf0 319bool reg_is_valid_request(const char *alpha2)
b2e1b302 320{
f6037d09
JB
321 if (!last_request)
322 return false;
323
324 return alpha2_equal(last_request->alpha2, alpha2);
b2e1b302 325}
8318d78a 326
b2e1b302 327/* Sanity check on a regulatory rule */
a3d2eaf0 328static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
8318d78a 329{
a3d2eaf0 330 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
b2e1b302
LR
331 u32 freq_diff;
332
91e99004 333 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
b2e1b302
LR
334 return false;
335
336 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
337 return false;
338
339 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
340
d71aaf60 341 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
b2e1b302
LR
342 return false;
343
344 return true;
345}
346
a3d2eaf0 347static bool is_valid_rd(const struct ieee80211_regdomain *rd)
b2e1b302 348{
a3d2eaf0 349 const struct ieee80211_reg_rule *reg_rule = NULL;
b2e1b302 350 unsigned int i;
8318d78a 351
b2e1b302
LR
352 if (!rd->n_reg_rules)
353 return false;
8318d78a 354
88dc1c3f
LR
355 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
356 return false;
357
b2e1b302
LR
358 for (i = 0; i < rd->n_reg_rules; i++) {
359 reg_rule = &rd->reg_rules[i];
360 if (!is_valid_reg_rule(reg_rule))
361 return false;
362 }
363
364 return true;
8318d78a
JB
365}
366
b2e1b302
LR
367/* Returns value in KHz */
368static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
369 u32 freq)
370{
371 unsigned int i;
372 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
373 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
374 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
375 if (start_freq_khz >= freq_range->start_freq_khz &&
376 end_freq_khz <= freq_range->end_freq_khz)
377 return supported_bandwidths[i];
378 }
379 return 0;
380}
8318d78a 381
9c96477d
LR
382/* Helper for regdom_intersect(), this does the real
383 * mathematical intersection fun */
384static int reg_rules_intersect(
385 const struct ieee80211_reg_rule *rule1,
386 const struct ieee80211_reg_rule *rule2,
387 struct ieee80211_reg_rule *intersected_rule)
388{
389 const struct ieee80211_freq_range *freq_range1, *freq_range2;
390 struct ieee80211_freq_range *freq_range;
391 const struct ieee80211_power_rule *power_rule1, *power_rule2;
392 struct ieee80211_power_rule *power_rule;
393 u32 freq_diff;
394
395 freq_range1 = &rule1->freq_range;
396 freq_range2 = &rule2->freq_range;
397 freq_range = &intersected_rule->freq_range;
398
399 power_rule1 = &rule1->power_rule;
400 power_rule2 = &rule2->power_rule;
401 power_rule = &intersected_rule->power_rule;
402
403 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
404 freq_range2->start_freq_khz);
405 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
406 freq_range2->end_freq_khz);
407 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
408 freq_range2->max_bandwidth_khz);
409
410 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
411 if (freq_range->max_bandwidth_khz > freq_diff)
412 freq_range->max_bandwidth_khz = freq_diff;
413
414 power_rule->max_eirp = min(power_rule1->max_eirp,
415 power_rule2->max_eirp);
416 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
417 power_rule2->max_antenna_gain);
418
419 intersected_rule->flags = (rule1->flags | rule2->flags);
420
421 if (!is_valid_reg_rule(intersected_rule))
422 return -EINVAL;
423
424 return 0;
425}
426
427/**
428 * regdom_intersect - do the intersection between two regulatory domains
429 * @rd1: first regulatory domain
430 * @rd2: second regulatory domain
431 *
432 * Use this function to get the intersection between two regulatory domains.
433 * Once completed we will mark the alpha2 for the rd as intersected, "98",
434 * as no one single alpha2 can represent this regulatory domain.
435 *
436 * Returns a pointer to the regulatory domain structure which will hold the
437 * resulting intersection of rules between rd1 and rd2. We will
438 * kzalloc() this structure for you.
439 */
440static struct ieee80211_regdomain *regdom_intersect(
441 const struct ieee80211_regdomain *rd1,
442 const struct ieee80211_regdomain *rd2)
443{
444 int r, size_of_regd;
445 unsigned int x, y;
446 unsigned int num_rules = 0, rule_idx = 0;
447 const struct ieee80211_reg_rule *rule1, *rule2;
448 struct ieee80211_reg_rule *intersected_rule;
449 struct ieee80211_regdomain *rd;
450 /* This is just a dummy holder to help us count */
451 struct ieee80211_reg_rule irule;
452
453 /* Uses the stack temporarily for counter arithmetic */
454 intersected_rule = &irule;
455
456 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
457
458 if (!rd1 || !rd2)
459 return NULL;
460
461 /* First we get a count of the rules we'll need, then we actually
462 * build them. This is to so we can malloc() and free() a
463 * regdomain once. The reason we use reg_rules_intersect() here
464 * is it will return -EINVAL if the rule computed makes no sense.
465 * All rules that do check out OK are valid. */
466
467 for (x = 0; x < rd1->n_reg_rules; x++) {
468 rule1 = &rd1->reg_rules[x];
469 for (y = 0; y < rd2->n_reg_rules; y++) {
470 rule2 = &rd2->reg_rules[y];
471 if (!reg_rules_intersect(rule1, rule2,
472 intersected_rule))
473 num_rules++;
474 memset(intersected_rule, 0,
475 sizeof(struct ieee80211_reg_rule));
476 }
477 }
478
479 if (!num_rules)
480 return NULL;
481
482 size_of_regd = sizeof(struct ieee80211_regdomain) +
483 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
484
485 rd = kzalloc(size_of_regd, GFP_KERNEL);
486 if (!rd)
487 return NULL;
488
489 for (x = 0; x < rd1->n_reg_rules; x++) {
490 rule1 = &rd1->reg_rules[x];
491 for (y = 0; y < rd2->n_reg_rules; y++) {
492 rule2 = &rd2->reg_rules[y];
493 /* This time around instead of using the stack lets
494 * write to the target rule directly saving ourselves
495 * a memcpy() */
496 intersected_rule = &rd->reg_rules[rule_idx];
497 r = reg_rules_intersect(rule1, rule2,
498 intersected_rule);
499 /* No need to memset here the intersected rule here as
500 * we're not using the stack anymore */
501 if (r)
502 continue;
503 rule_idx++;
504 }
505 }
506
507 if (rule_idx != num_rules) {
508 kfree(rd);
509 return NULL;
510 }
511
512 rd->n_reg_rules = num_rules;
513 rd->alpha2[0] = '9';
514 rd->alpha2[1] = '8';
515
516 return rd;
517}
518
b2e1b302
LR
519/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
520 * want to just have the channel structure use these */
521static u32 map_regdom_flags(u32 rd_flags)
522{
523 u32 channel_flags = 0;
524 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
525 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
526 if (rd_flags & NL80211_RRF_NO_IBSS)
527 channel_flags |= IEEE80211_CHAN_NO_IBSS;
528 if (rd_flags & NL80211_RRF_DFS)
529 channel_flags |= IEEE80211_CHAN_RADAR;
530 return channel_flags;
531}
532
533/**
534 * freq_reg_info - get regulatory information for the given frequency
535 * @center_freq: Frequency in KHz for which we want regulatory information for
536 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
537 * you can set this to 0. If this frequency is allowed we then set
538 * this value to the maximum allowed bandwidth.
539 * @reg_rule: the regulatory rule which we have for this frequency
540 *
541 * Use this function to get the regulatory rule for a specific frequency.
542 */
543static int freq_reg_info(u32 center_freq, u32 *bandwidth,
544 const struct ieee80211_reg_rule **reg_rule)
8318d78a
JB
545{
546 int i;
b2e1b302 547 u32 max_bandwidth = 0;
8318d78a 548
b2e1b302
LR
549 if (!cfg80211_regdomain)
550 return -EINVAL;
551
552 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
553 const struct ieee80211_reg_rule *rr;
554 const struct ieee80211_freq_range *fr = NULL;
555 const struct ieee80211_power_rule *pr = NULL;
556
557 rr = &cfg80211_regdomain->reg_rules[i];
558 fr = &rr->freq_range;
559 pr = &rr->power_rule;
560 max_bandwidth = freq_max_bandwidth(fr, center_freq);
561 if (max_bandwidth && *bandwidth <= max_bandwidth) {
562 *reg_rule = rr;
563 *bandwidth = max_bandwidth;
8318d78a
JB
564 break;
565 }
566 }
567
b2e1b302
LR
568 return !max_bandwidth;
569}
570
571static void handle_channel(struct ieee80211_channel *chan)
572{
573 int r;
574 u32 flags = chan->orig_flags;
575 u32 max_bandwidth = 0;
576 const struct ieee80211_reg_rule *reg_rule = NULL;
577 const struct ieee80211_power_rule *power_rule = NULL;
578
579 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
580 &max_bandwidth, &reg_rule);
581
582 if (r) {
8318d78a
JB
583 flags |= IEEE80211_CHAN_DISABLED;
584 chan->flags = flags;
585 return;
586 }
587
b2e1b302
LR
588 power_rule = &reg_rule->power_rule;
589
590 chan->flags = flags | map_regdom_flags(reg_rule->flags);
8318d78a 591 chan->max_antenna_gain = min(chan->orig_mag,
b2e1b302
LR
592 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
593 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
253898c4 594 if (chan->orig_mpwr)
b2e1b302
LR
595 chan->max_power = min(chan->orig_mpwr,
596 (int) MBM_TO_DBM(power_rule->max_eirp));
253898c4 597 else
b2e1b302 598 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
8318d78a
JB
599}
600
b2e1b302 601static void handle_band(struct ieee80211_supported_band *sband)
8318d78a
JB
602{
603 int i;
604
605 for (i = 0; i < sband->n_channels; i++)
b2e1b302 606 handle_channel(&sband->channels[i]);
8318d78a
JB
607}
608
b2e1b302 609static void update_all_wiphy_regulatory(enum reg_set_by setby)
8318d78a 610{
b2e1b302 611 struct cfg80211_registered_device *drv;
8318d78a 612
b2e1b302
LR
613 list_for_each_entry(drv, &cfg80211_drv_list, list)
614 wiphy_update_regulatory(&drv->wiphy, setby);
615}
616
617void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
618{
619 enum ieee80211_band band;
620 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
8318d78a 621 if (wiphy->bands[band])
b2e1b302
LR
622 handle_band(wiphy->bands[band]);
623 if (wiphy->reg_notifier)
624 wiphy->reg_notifier(wiphy, setby);
625 }
626}
627
9c96477d
LR
628/* Return value which can be used by ignore_request() to indicate
629 * it has been determined we should intersect two regulatory domains */
630#define REG_INTERSECT 1
631
84fa4f43
JB
632/* This has the logic which determines when a new request
633 * should be ignored. */
634static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
635 const char *alpha2)
636{
637 /* All initial requests are respected */
638 if (!last_request)
639 return 0;
640
641 switch (set_by) {
642 case REGDOM_SET_BY_INIT:
643 return -EINVAL;
644 case REGDOM_SET_BY_CORE:
645 /*
646 * Always respect new wireless core hints, should only happen
647 * when updating the world regulatory domain at init.
648 */
649 return 0;
650 case REGDOM_SET_BY_COUNTRY_IE:
651 if (unlikely(!is_an_alpha2(alpha2)))
652 return -EINVAL;
653 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
654 if (last_request->wiphy != wiphy) {
655 /*
656 * Two cards with two APs claiming different
657 * different Country IE alpha2s. We could
658 * intersect them, but that seems unlikely
659 * to be correct. Reject second one for now.
660 */
661 if (!alpha2_equal(alpha2,
662 cfg80211_regdomain->alpha2))
663 return -EOPNOTSUPP;
664 return -EALREADY;
665 }
666 /* Two consecutive Country IE hints on the same wiphy */
667 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
668 return 0;
669 return -EALREADY;
670 }
671 /*
672 * Ignore Country IE hints for now, need to think about
673 * what we need to do to support multi-domain operation.
674 */
675 return -EOPNOTSUPP;
676 case REGDOM_SET_BY_DRIVER:
677 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
678 return -EALREADY;
679 return 0;
680 case REGDOM_SET_BY_USER:
84fa4f43 681 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
9c96477d 682 return REG_INTERSECT;
84fa4f43
JB
683 return 0;
684 }
685
686 return -EINVAL;
687}
688
b2e1b302
LR
689/* Caller must hold &cfg80211_drv_mutex */
690int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
be3d4810 691 const char *alpha2)
b2e1b302
LR
692{
693 struct regulatory_request *request;
9c96477d 694 bool intersect = false;
b2e1b302
LR
695 int r = 0;
696
be3d4810 697 r = ignore_request(wiphy, set_by, alpha2);
9c96477d
LR
698
699 if (r == REG_INTERSECT)
700 intersect = true;
701 else if (r)
b2e1b302
LR
702 return r;
703
5203cdb6
LR
704 request = kzalloc(sizeof(struct regulatory_request),
705 GFP_KERNEL);
706 if (!request)
707 return -ENOMEM;
708
709 request->alpha2[0] = alpha2[0];
710 request->alpha2[1] = alpha2[1];
711 request->initiator = set_by;
712 request->wiphy = wiphy;
713 request->intersect = intersect;
714
715 kfree(last_request);
716 last_request = request;
02ba0b32 717 return call_crda(alpha2);
b2e1b302
LR
718}
719
be3d4810 720void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
b2e1b302 721{
be3d4810 722 BUG_ON(!alpha2);
b2e1b302
LR
723
724 mutex_lock(&cfg80211_drv_mutex);
be3d4810 725 __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2);
b2e1b302 726 mutex_unlock(&cfg80211_drv_mutex);
b2e1b302
LR
727}
728EXPORT_SYMBOL(regulatory_hint);
729
730
a3d2eaf0 731static void print_rd_rules(const struct ieee80211_regdomain *rd)
b2e1b302
LR
732{
733 unsigned int i;
a3d2eaf0
JB
734 const struct ieee80211_reg_rule *reg_rule = NULL;
735 const struct ieee80211_freq_range *freq_range = NULL;
736 const struct ieee80211_power_rule *power_rule = NULL;
b2e1b302
LR
737
738 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
739 "(max_antenna_gain, max_eirp)\n");
740
741 for (i = 0; i < rd->n_reg_rules; i++) {
742 reg_rule = &rd->reg_rules[i];
743 freq_range = &reg_rule->freq_range;
744 power_rule = &reg_rule->power_rule;
745
746 /* There may not be documentation for max antenna gain
747 * in certain regions */
748 if (power_rule->max_antenna_gain)
749 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
750 "(%d mBi, %d mBm)\n",
751 freq_range->start_freq_khz,
752 freq_range->end_freq_khz,
753 freq_range->max_bandwidth_khz,
754 power_rule->max_antenna_gain,
755 power_rule->max_eirp);
756 else
757 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
758 "(N/A, %d mBm)\n",
759 freq_range->start_freq_khz,
760 freq_range->end_freq_khz,
761 freq_range->max_bandwidth_khz,
762 power_rule->max_eirp);
763 }
764}
765
a3d2eaf0 766static void print_regdomain(const struct ieee80211_regdomain *rd)
b2e1b302
LR
767{
768
769 if (is_world_regdom(rd->alpha2))
770 printk(KERN_INFO "cfg80211: World regulatory "
771 "domain updated:\n");
772 else {
773 if (is_unknown_alpha2(rd->alpha2))
774 printk(KERN_INFO "cfg80211: Regulatory domain "
775 "changed to driver built-in settings "
776 "(unknown country)\n");
777 else
778 printk(KERN_INFO "cfg80211: Regulatory domain "
779 "changed to country: %c%c\n",
780 rd->alpha2[0], rd->alpha2[1]);
781 }
782 print_rd_rules(rd);
783}
784
2df78167 785static void print_regdomain_info(const struct ieee80211_regdomain *rd)
b2e1b302
LR
786{
787 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
788 rd->alpha2[0], rd->alpha2[1]);
789 print_rd_rules(rd);
790}
791
d2372b31 792/* Takes ownership of rd only if it doesn't fail */
a3d2eaf0 793static int __set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 794{
9c96477d 795 const struct ieee80211_regdomain *intersected_rd = NULL;
b2e1b302
LR
796 /* Some basic sanity checks first */
797
b2e1b302 798 if (is_world_regdom(rd->alpha2)) {
f6037d09 799 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
800 return -EINVAL;
801 update_world_regdomain(rd);
802 return 0;
803 }
b2e1b302
LR
804
805 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
806 !is_unknown_alpha2(rd->alpha2))
807 return -EINVAL;
808
f6037d09 809 if (!last_request)
b2e1b302
LR
810 return -EINVAL;
811
942b25cf 812 /* allow overriding the static definitions if CRDA is present */
b2e1b302 813 if (!is_old_static_regdom(cfg80211_regdomain) &&
942b25cf 814 !regdom_changed(rd->alpha2))
b2e1b302 815 return -EINVAL;
b2e1b302
LR
816
817 /* Now lets set the regulatory domain, update all driver channels
818 * and finally inform them of what we have done, in case they want
819 * to review or adjust their own settings based on their own
820 * internal EEPROM data */
821
f6037d09 822 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
823 return -EINVAL;
824
8375af3b
LR
825 if (!is_valid_rd(rd)) {
826 printk(KERN_ERR "cfg80211: Invalid "
827 "regulatory domain detected:\n");
828 print_regdomain_info(rd);
829 return -EINVAL;
b2e1b302
LR
830 }
831
b8295acd
LR
832 if (!last_request->intersect) {
833 reset_regdomains();
834 cfg80211_regdomain = rd;
835 return 0;
836 }
837
838 /* Intersection requires a bit more work */
839
840 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
841
9c96477d
LR
842 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
843 if (!intersected_rd)
844 return -EINVAL;
b8295acd
LR
845
846 /* We can trash what CRDA provided now */
9c96477d 847 kfree(rd);
b8295acd
LR
848 rd = NULL;
849
850 reset_regdomains();
851 cfg80211_regdomain = intersected_rd;
852
853 return 0;
9c96477d
LR
854 }
855
b8295acd
LR
856 /* Country IE parsing coming soon */
857 reset_regdomains();
858 WARN_ON(1);
b2e1b302
LR
859
860 return 0;
861}
862
863
864/* Use this call to set the current regulatory domain. Conflicts with
865 * multiple drivers can be ironed out later. Caller must've already
d2372b31 866 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
a3d2eaf0 867int set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 868{
b2e1b302
LR
869 int r;
870
b2e1b302
LR
871 /* Note that this doesn't update the wiphys, this is done below */
872 r = __set_regdom(rd);
d2372b31
JB
873 if (r) {
874 kfree(rd);
b2e1b302 875 return r;
d2372b31 876 }
b2e1b302 877
b2e1b302 878 /* This would make this whole thing pointless */
a01ddafd
LR
879 if (!last_request->intersect)
880 BUG_ON(rd != cfg80211_regdomain);
b2e1b302
LR
881
882 /* update all wiphys now with the new established regulatory domain */
f6037d09 883 update_all_wiphy_regulatory(last_request->initiator);
b2e1b302 884
a01ddafd 885 print_regdomain(cfg80211_regdomain);
b2e1b302
LR
886
887 return r;
888}
889
890int regulatory_init(void)
891{
734366de
JB
892 int err;
893
b2e1b302
LR
894 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
895 if (IS_ERR(reg_pdev))
896 return PTR_ERR(reg_pdev);
734366de
JB
897
898#ifdef CONFIG_WIRELESS_OLD_REGULATORY
a3d2eaf0 899 cfg80211_regdomain = static_regdom(ieee80211_regdom);
734366de 900
942b25cf 901 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
734366de
JB
902 print_regdomain_info(cfg80211_regdomain);
903 /* The old code still requests for a new regdomain and if
904 * you have CRDA you get it updated, otherwise you get
905 * stuck with the static values. We ignore "EU" code as
906 * that is not a valid ISO / IEC 3166 alpha2 */
ac9440a4 907 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
734366de 908 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
be3d4810 909 ieee80211_regdom);
734366de 910#else
a3d2eaf0 911 cfg80211_regdomain = cfg80211_world_regdom;
734366de 912
be3d4810 913 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00");
734366de
JB
914 if (err)
915 printk(KERN_ERR "cfg80211: calling CRDA failed - "
916 "unable to update world regulatory domain, "
917 "using static definition\n");
918#endif
919
b2e1b302
LR
920 return 0;
921}
922
923void regulatory_exit(void)
924{
b2e1b302 925 mutex_lock(&cfg80211_drv_mutex);
734366de 926
b2e1b302 927 reset_regdomains();
734366de 928
f6037d09
JB
929 kfree(last_request);
930
b2e1b302 931 platform_device_unregister(reg_pdev);
734366de 932
b2e1b302 933 mutex_unlock(&cfg80211_drv_mutex);
8318d78a 934}