1 /* 2 * Copyright 2002-2005, Instant802 Networks, Inc. 3 * Copyright 2005-2006, Devicescape Software, Inc. 4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net> 5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com> 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 12 /** 13 * DOC: Wireless regulatory infrastructure 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 * 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 * 34 */ 35 #include <linux/kernel.h> 36 #include <linux/slab.h> 37 #include <linux/list.h> 38 #include <linux/random.h> 39 #include <linux/nl80211.h> 40 #include <linux/platform_device.h> 41 #include <net/cfg80211.h> 42 #include "core.h" 43 #include "reg.h" 44 #include "regdb.h" 45 #include "nl80211.h" 46 47 #ifdef CONFIG_CFG80211_REG_DEBUG 48 #define REG_DBG_PRINT(format, args...) \ 49 do { \ 50 printk(KERN_DEBUG format , ## args); \ 51 } while (0) 52 #else 53 #define REG_DBG_PRINT(args...) 54 #endif 55 56 /* Receipt of information from last regulatory request */ 57 static struct regulatory_request *last_request; 58 59 /* To trigger userspace events */ 60 static struct platform_device *reg_pdev; 61 62 /* 63 * Central wireless core regulatory domains, we only need two, 64 * the current one and a world regulatory domain in case we have no 65 * information to give us an alpha2 66 */ 67 const struct ieee80211_regdomain *cfg80211_regdomain; 68 69 /* 70 * We use this as a place for the rd structure built from the 71 * last parsed country IE to rest until CRDA gets back to us with 72 * what it thinks should apply for the same country 73 */ 74 static const struct ieee80211_regdomain *country_ie_regdomain; 75 76 /* 77 * Protects static reg.c components: 78 * - cfg80211_world_regdom 79 * - cfg80211_regdom 80 * - country_ie_regdomain 81 * - last_request 82 */ 83 DEFINE_MUTEX(reg_mutex); 84 #define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex)) 85 86 /* Used to queue up regulatory hints */ 87 static LIST_HEAD(reg_requests_list); 88 static spinlock_t reg_requests_lock; 89 90 /* Used to queue up beacon hints for review */ 91 static LIST_HEAD(reg_pending_beacons); 92 static spinlock_t reg_pending_beacons_lock; 93 94 /* Used to keep track of processed beacon hints */ 95 static LIST_HEAD(reg_beacon_list); 96 97 struct reg_beacon { 98 struct list_head list; 99 struct ieee80211_channel chan; 100 }; 101 102 /* We keep a static world regulatory domain in case of the absence of CRDA */ 103 static const struct ieee80211_regdomain world_regdom = { 104 .n_reg_rules = 5, 105 .alpha2 = "00", 106 .reg_rules = { 107 /* IEEE 802.11b/g, channels 1..11 */ 108 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0), 109 /* IEEE 802.11b/g, channels 12..13. No HT40 110 * channel fits here. */ 111 REG_RULE(2467-10, 2472+10, 20, 6, 20, 112 NL80211_RRF_PASSIVE_SCAN | 113 NL80211_RRF_NO_IBSS), 114 /* IEEE 802.11 channel 14 - Only JP enables 115 * this and for 802.11b only */ 116 REG_RULE(2484-10, 2484+10, 20, 6, 20, 117 NL80211_RRF_PASSIVE_SCAN | 118 NL80211_RRF_NO_IBSS | 119 NL80211_RRF_NO_OFDM), 120 /* IEEE 802.11a, channel 36..48 */ 121 REG_RULE(5180-10, 5240+10, 40, 6, 20, 122 NL80211_RRF_PASSIVE_SCAN | 123 NL80211_RRF_NO_IBSS), 124 125 /* NB: 5260 MHz - 5700 MHz requies DFS */ 126 127 /* IEEE 802.11a, channel 149..165 */ 128 REG_RULE(5745-10, 5825+10, 40, 6, 20, 129 NL80211_RRF_PASSIVE_SCAN | 130 NL80211_RRF_NO_IBSS), 131 } 132 }; 133 134 static const struct ieee80211_regdomain *cfg80211_world_regdom = 135 &world_regdom; 136 137 static char *ieee80211_regdom = "00"; 138 static char user_alpha2[2]; 139 140 module_param(ieee80211_regdom, charp, 0444); 141 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); 142 143 static void reset_regdomains(void) 144 { 145 /* avoid freeing static information or freeing something twice */ 146 if (cfg80211_regdomain == cfg80211_world_regdom) 147 cfg80211_regdomain = NULL; 148 if (cfg80211_world_regdom == &world_regdom) 149 cfg80211_world_regdom = NULL; 150 if (cfg80211_regdomain == &world_regdom) 151 cfg80211_regdomain = NULL; 152 153 kfree(cfg80211_regdomain); 154 kfree(cfg80211_world_regdom); 155 156 cfg80211_world_regdom = &world_regdom; 157 cfg80211_regdomain = NULL; 158 } 159 160 /* 161 * Dynamic world regulatory domain requested by the wireless 162 * core upon initialization 163 */ 164 static void update_world_regdomain(const struct ieee80211_regdomain *rd) 165 { 166 BUG_ON(!last_request); 167 168 reset_regdomains(); 169 170 cfg80211_world_regdom = rd; 171 cfg80211_regdomain = rd; 172 } 173 174 bool is_world_regdom(const char *alpha2) 175 { 176 if (!alpha2) 177 return false; 178 if (alpha2[0] == '0' && alpha2[1] == '0') 179 return true; 180 return false; 181 } 182 183 static bool is_alpha2_set(const char *alpha2) 184 { 185 if (!alpha2) 186 return false; 187 if (alpha2[0] != 0 && alpha2[1] != 0) 188 return true; 189 return false; 190 } 191 192 static bool is_alpha_upper(char letter) 193 { 194 /* ASCII A - Z */ 195 if (letter >= 65 && letter <= 90) 196 return true; 197 return false; 198 } 199 200 static bool is_unknown_alpha2(const char *alpha2) 201 { 202 if (!alpha2) 203 return false; 204 /* 205 * Special case where regulatory domain was built by driver 206 * but a specific alpha2 cannot be determined 207 */ 208 if (alpha2[0] == '9' && alpha2[1] == '9') 209 return true; 210 return false; 211 } 212 213 static bool is_intersected_alpha2(const char *alpha2) 214 { 215 if (!alpha2) 216 return false; 217 /* 218 * Special case where regulatory domain is the 219 * result of an intersection between two regulatory domain 220 * structures 221 */ 222 if (alpha2[0] == '9' && alpha2[1] == '8') 223 return true; 224 return false; 225 } 226 227 static bool is_an_alpha2(const char *alpha2) 228 { 229 if (!alpha2) 230 return false; 231 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1])) 232 return true; 233 return false; 234 } 235 236 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y) 237 { 238 if (!alpha2_x || !alpha2_y) 239 return false; 240 if (alpha2_x[0] == alpha2_y[0] && 241 alpha2_x[1] == alpha2_y[1]) 242 return true; 243 return false; 244 } 245 246 static bool regdom_changes(const char *alpha2) 247 { 248 assert_cfg80211_lock(); 249 250 if (!cfg80211_regdomain) 251 return true; 252 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2)) 253 return false; 254 return true; 255 } 256 257 /* 258 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets 259 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER 260 * has ever been issued. 261 */ 262 static bool is_user_regdom_saved(void) 263 { 264 if (user_alpha2[0] == '9' && user_alpha2[1] == '7') 265 return false; 266 267 /* This would indicate a mistake on the design */ 268 if (WARN((!is_world_regdom(user_alpha2) && 269 !is_an_alpha2(user_alpha2)), 270 "Unexpected user alpha2: %c%c\n", 271 user_alpha2[0], 272 user_alpha2[1])) 273 return false; 274 275 return true; 276 } 277 278 /** 279 * country_ie_integrity_changes - tells us if the country IE has changed 280 * @checksum: checksum of country IE of fields we are interested in 281 * 282 * If the country IE has not changed you can ignore it safely. This is 283 * useful to determine if two devices are seeing two different country IEs 284 * even on the same alpha2. Note that this will return false if no IE has 285 * been set on the wireless core yet. 286 */ 287 static bool country_ie_integrity_changes(u32 checksum) 288 { 289 /* If no IE has been set then the checksum doesn't change */ 290 if (unlikely(!last_request->country_ie_checksum)) 291 return false; 292 if (unlikely(last_request->country_ie_checksum != checksum)) 293 return true; 294 return false; 295 } 296 297 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd, 298 const struct ieee80211_regdomain *src_regd) 299 { 300 struct ieee80211_regdomain *regd; 301 int size_of_regd = 0; 302 unsigned int i; 303 304 size_of_regd = sizeof(struct ieee80211_regdomain) + 305 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule)); 306 307 regd = kzalloc(size_of_regd, GFP_KERNEL); 308 if (!regd) 309 return -ENOMEM; 310 311 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain)); 312 313 for (i = 0; i < src_regd->n_reg_rules; i++) 314 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i], 315 sizeof(struct ieee80211_reg_rule)); 316 317 *dst_regd = regd; 318 return 0; 319 } 320 321 #ifdef CONFIG_CFG80211_INTERNAL_REGDB 322 struct reg_regdb_search_request { 323 char alpha2[2]; 324 struct list_head list; 325 }; 326 327 static LIST_HEAD(reg_regdb_search_list); 328 static DEFINE_MUTEX(reg_regdb_search_mutex); 329 330 static void reg_regdb_search(struct work_struct *work) 331 { 332 struct reg_regdb_search_request *request; 333 const struct ieee80211_regdomain *curdom, *regdom; 334 int i, r; 335 336 mutex_lock(®_regdb_search_mutex); 337 while (!list_empty(®_regdb_search_list)) { 338 request = list_first_entry(®_regdb_search_list, 339 struct reg_regdb_search_request, 340 list); 341 list_del(&request->list); 342 343 for (i=0; i<reg_regdb_size; i++) { 344 curdom = reg_regdb[i]; 345 346 if (!memcmp(request->alpha2, curdom->alpha2, 2)) { 347 r = reg_copy_regd(®dom, curdom); 348 if (r) 349 break; 350 mutex_lock(&cfg80211_mutex); 351 set_regdom(regdom); 352 mutex_unlock(&cfg80211_mutex); 353 break; 354 } 355 } 356 357 kfree(request); 358 } 359 mutex_unlock(®_regdb_search_mutex); 360 } 361 362 static DECLARE_WORK(reg_regdb_work, reg_regdb_search); 363 364 static void reg_regdb_query(const char *alpha2) 365 { 366 struct reg_regdb_search_request *request; 367 368 if (!alpha2) 369 return; 370 371 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL); 372 if (!request) 373 return; 374 375 memcpy(request->alpha2, alpha2, 2); 376 377 mutex_lock(®_regdb_search_mutex); 378 list_add_tail(&request->list, ®_regdb_search_list); 379 mutex_unlock(®_regdb_search_mutex); 380 381 schedule_work(®_regdb_work); 382 } 383 #else 384 static inline void reg_regdb_query(const char *alpha2) {} 385 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */ 386 387 /* 388 * This lets us keep regulatory code which is updated on a regulatory 389 * basis in userspace. 390 */ 391 static int call_crda(const char *alpha2) 392 { 393 char country_env[9 + 2] = "COUNTRY="; 394 char *envp[] = { 395 country_env, 396 NULL 397 }; 398 399 if (!is_world_regdom((char *) alpha2)) 400 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n", 401 alpha2[0], alpha2[1]); 402 else 403 printk(KERN_INFO "cfg80211: Calling CRDA to update world " 404 "regulatory domain\n"); 405 406 /* query internal regulatory database (if it exists) */ 407 reg_regdb_query(alpha2); 408 409 country_env[8] = alpha2[0]; 410 country_env[9] = alpha2[1]; 411 412 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp); 413 } 414 415 /* Used by nl80211 before kmalloc'ing our regulatory domain */ 416 bool reg_is_valid_request(const char *alpha2) 417 { 418 assert_cfg80211_lock(); 419 420 if (!last_request) 421 return false; 422 423 return alpha2_equal(last_request->alpha2, alpha2); 424 } 425 426 /* Sanity check on a regulatory rule */ 427 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule) 428 { 429 const struct ieee80211_freq_range *freq_range = &rule->freq_range; 430 u32 freq_diff; 431 432 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0) 433 return false; 434 435 if (freq_range->start_freq_khz > freq_range->end_freq_khz) 436 return false; 437 438 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; 439 440 if (freq_range->end_freq_khz <= freq_range->start_freq_khz || 441 freq_range->max_bandwidth_khz > freq_diff) 442 return false; 443 444 return true; 445 } 446 447 static bool is_valid_rd(const struct ieee80211_regdomain *rd) 448 { 449 const struct ieee80211_reg_rule *reg_rule = NULL; 450 unsigned int i; 451 452 if (!rd->n_reg_rules) 453 return false; 454 455 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES)) 456 return false; 457 458 for (i = 0; i < rd->n_reg_rules; i++) { 459 reg_rule = &rd->reg_rules[i]; 460 if (!is_valid_reg_rule(reg_rule)) 461 return false; 462 } 463 464 return true; 465 } 466 467 static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range, 468 u32 center_freq_khz, 469 u32 bw_khz) 470 { 471 u32 start_freq_khz, end_freq_khz; 472 473 start_freq_khz = center_freq_khz - (bw_khz/2); 474 end_freq_khz = center_freq_khz + (bw_khz/2); 475 476 if (start_freq_khz >= freq_range->start_freq_khz && 477 end_freq_khz <= freq_range->end_freq_khz) 478 return true; 479 480 return false; 481 } 482 483 /** 484 * freq_in_rule_band - tells us if a frequency is in a frequency band 485 * @freq_range: frequency rule we want to query 486 * @freq_khz: frequency we are inquiring about 487 * 488 * This lets us know if a specific frequency rule is or is not relevant to 489 * a specific frequency's band. Bands are device specific and artificial 490 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is 491 * safe for now to assume that a frequency rule should not be part of a 492 * frequency's band if the start freq or end freq are off by more than 2 GHz. 493 * This resolution can be lowered and should be considered as we add 494 * regulatory rule support for other "bands". 495 **/ 496 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range, 497 u32 freq_khz) 498 { 499 #define ONE_GHZ_IN_KHZ 1000000 500 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) 501 return true; 502 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) 503 return true; 504 return false; 505 #undef ONE_GHZ_IN_KHZ 506 } 507 508 /* 509 * This is a work around for sanity checking ieee80211_channel_to_frequency()'s 510 * work. ieee80211_channel_to_frequency() can for example currently provide a 511 * 2 GHz channel when in fact a 5 GHz channel was desired. An example would be 512 * an AP providing channel 8 on a country IE triplet when it sent this on the 513 * 5 GHz band, that channel is designed to be channel 8 on 5 GHz, not a 2 GHz 514 * channel. 515 * 516 * This can be removed once ieee80211_channel_to_frequency() takes in a band. 517 */ 518 static bool chan_in_band(int chan, enum ieee80211_band band) 519 { 520 int center_freq = ieee80211_channel_to_frequency(chan); 521 522 switch (band) { 523 case IEEE80211_BAND_2GHZ: 524 if (center_freq <= 2484) 525 return true; 526 return false; 527 case IEEE80211_BAND_5GHZ: 528 if (center_freq >= 5005) 529 return true; 530 return false; 531 default: 532 return false; 533 } 534 } 535 536 /* 537 * Some APs may send a country IE triplet for each channel they 538 * support and while this is completely overkill and silly we still 539 * need to support it. We avoid making a single rule for each channel 540 * though and to help us with this we use this helper to find the 541 * actual subband end channel. These type of country IE triplet 542 * scenerios are handled then, all yielding two regulaotry rules from 543 * parsing a country IE: 544 * 545 * [1] 546 * [2] 547 * [36] 548 * [40] 549 * 550 * [1] 551 * [2-4] 552 * [5-12] 553 * [36] 554 * [40-44] 555 * 556 * [1-4] 557 * [5-7] 558 * [36-44] 559 * [48-64] 560 * 561 * [36-36] 562 * [40-40] 563 * [44-44] 564 * [48-48] 565 * [52-52] 566 * [56-56] 567 * [60-60] 568 * [64-64] 569 * [100-100] 570 * [104-104] 571 * [108-108] 572 * [112-112] 573 * [116-116] 574 * [120-120] 575 * [124-124] 576 * [128-128] 577 * [132-132] 578 * [136-136] 579 * [140-140] 580 * 581 * Returns 0 if the IE has been found to be invalid in the middle 582 * somewhere. 583 */ 584 static int max_subband_chan(enum ieee80211_band band, 585 int orig_cur_chan, 586 int orig_end_channel, 587 s8 orig_max_power, 588 u8 **country_ie, 589 u8 *country_ie_len) 590 { 591 u8 *triplets_start = *country_ie; 592 u8 len_at_triplet = *country_ie_len; 593 int end_subband_chan = orig_end_channel; 594 595 /* 596 * We'll deal with padding for the caller unless 597 * its not immediate and we don't process any channels 598 */ 599 if (*country_ie_len == 1) { 600 *country_ie += 1; 601 *country_ie_len -= 1; 602 return orig_end_channel; 603 } 604 605 /* Move to the next triplet and then start search */ 606 *country_ie += 3; 607 *country_ie_len -= 3; 608 609 if (!chan_in_band(orig_cur_chan, band)) 610 return 0; 611 612 while (*country_ie_len >= 3) { 613 int end_channel = 0; 614 struct ieee80211_country_ie_triplet *triplet = 615 (struct ieee80211_country_ie_triplet *) *country_ie; 616 int cur_channel = 0, next_expected_chan; 617 618 /* means last triplet is completely unrelated to this one */ 619 if (triplet->ext.reg_extension_id >= 620 IEEE80211_COUNTRY_EXTENSION_ID) { 621 *country_ie -= 3; 622 *country_ie_len += 3; 623 break; 624 } 625 626 if (triplet->chans.first_channel == 0) { 627 *country_ie += 1; 628 *country_ie_len -= 1; 629 if (*country_ie_len != 0) 630 return 0; 631 break; 632 } 633 634 if (triplet->chans.num_channels == 0) 635 return 0; 636 637 /* Monitonically increasing channel order */ 638 if (triplet->chans.first_channel <= end_subband_chan) 639 return 0; 640 641 if (!chan_in_band(triplet->chans.first_channel, band)) 642 return 0; 643 644 /* 2 GHz */ 645 if (triplet->chans.first_channel <= 14) { 646 end_channel = triplet->chans.first_channel + 647 triplet->chans.num_channels - 1; 648 } 649 else { 650 end_channel = triplet->chans.first_channel + 651 (4 * (triplet->chans.num_channels - 1)); 652 } 653 654 if (!chan_in_band(end_channel, band)) 655 return 0; 656 657 if (orig_max_power != triplet->chans.max_power) { 658 *country_ie -= 3; 659 *country_ie_len += 3; 660 break; 661 } 662 663 cur_channel = triplet->chans.first_channel; 664 665 /* The key is finding the right next expected channel */ 666 if (band == IEEE80211_BAND_2GHZ) 667 next_expected_chan = end_subband_chan + 1; 668 else 669 next_expected_chan = end_subband_chan + 4; 670 671 if (cur_channel != next_expected_chan) { 672 *country_ie -= 3; 673 *country_ie_len += 3; 674 break; 675 } 676 677 end_subband_chan = end_channel; 678 679 /* Move to the next one */ 680 *country_ie += 3; 681 *country_ie_len -= 3; 682 683 /* 684 * Padding needs to be dealt with if we processed 685 * some channels. 686 */ 687 if (*country_ie_len == 1) { 688 *country_ie += 1; 689 *country_ie_len -= 1; 690 break; 691 } 692 693 /* If seen, the IE is invalid */ 694 if (*country_ie_len == 2) 695 return 0; 696 } 697 698 if (end_subband_chan == orig_end_channel) { 699 *country_ie = triplets_start; 700 *country_ie_len = len_at_triplet; 701 return orig_end_channel; 702 } 703 704 return end_subband_chan; 705 } 706 707 /* 708 * Converts a country IE to a regulatory domain. A regulatory domain 709 * structure has a lot of information which the IE doesn't yet have, 710 * so for the other values we use upper max values as we will intersect 711 * with our userspace regulatory agent to get lower bounds. 712 */ 713 static struct ieee80211_regdomain *country_ie_2_rd( 714 enum ieee80211_band band, 715 u8 *country_ie, 716 u8 country_ie_len, 717 u32 *checksum) 718 { 719 struct ieee80211_regdomain *rd = NULL; 720 unsigned int i = 0; 721 char alpha2[2]; 722 u32 flags = 0; 723 u32 num_rules = 0, size_of_regd = 0; 724 u8 *triplets_start = NULL; 725 u8 len_at_triplet = 0; 726 /* the last channel we have registered in a subband (triplet) */ 727 int last_sub_max_channel = 0; 728 729 *checksum = 0xDEADBEEF; 730 731 /* Country IE requirements */ 732 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN || 733 country_ie_len & 0x01); 734 735 alpha2[0] = country_ie[0]; 736 alpha2[1] = country_ie[1]; 737 738 /* 739 * Third octet can be: 740 * 'I' - Indoor 741 * 'O' - Outdoor 742 * 743 * anything else we assume is no restrictions 744 */ 745 if (country_ie[2] == 'I') 746 flags = NL80211_RRF_NO_OUTDOOR; 747 else if (country_ie[2] == 'O') 748 flags = NL80211_RRF_NO_INDOOR; 749 750 country_ie += 3; 751 country_ie_len -= 3; 752 753 triplets_start = country_ie; 754 len_at_triplet = country_ie_len; 755 756 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8); 757 758 /* 759 * We need to build a reg rule for each triplet, but first we must 760 * calculate the number of reg rules we will need. We will need one 761 * for each channel subband 762 */ 763 while (country_ie_len >= 3) { 764 int end_channel = 0; 765 struct ieee80211_country_ie_triplet *triplet = 766 (struct ieee80211_country_ie_triplet *) country_ie; 767 int cur_sub_max_channel = 0, cur_channel = 0; 768 769 if (triplet->ext.reg_extension_id >= 770 IEEE80211_COUNTRY_EXTENSION_ID) { 771 country_ie += 3; 772 country_ie_len -= 3; 773 continue; 774 } 775 776 /* 777 * APs can add padding to make length divisible 778 * by two, required by the spec. 779 */ 780 if (triplet->chans.first_channel == 0) { 781 country_ie++; 782 country_ie_len--; 783 /* This is expected to be at the very end only */ 784 if (country_ie_len != 0) 785 return NULL; 786 break; 787 } 788 789 if (triplet->chans.num_channels == 0) 790 return NULL; 791 792 if (!chan_in_band(triplet->chans.first_channel, band)) 793 return NULL; 794 795 /* 2 GHz */ 796 if (band == IEEE80211_BAND_2GHZ) 797 end_channel = triplet->chans.first_channel + 798 triplet->chans.num_channels - 1; 799 else 800 /* 801 * 5 GHz -- For example in country IEs if the first 802 * channel given is 36 and the number of channels is 4 803 * then the individual channel numbers defined for the 804 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48 805 * and not 36, 37, 38, 39. 806 * 807 * See: http://tinyurl.com/11d-clarification 808 */ 809 end_channel = triplet->chans.first_channel + 810 (4 * (triplet->chans.num_channels - 1)); 811 812 cur_channel = triplet->chans.first_channel; 813 814 /* 815 * Enhancement for APs that send a triplet for every channel 816 * or for whatever reason sends triplets with multiple channels 817 * separated when in fact they should be together. 818 */ 819 end_channel = max_subband_chan(band, 820 cur_channel, 821 end_channel, 822 triplet->chans.max_power, 823 &country_ie, 824 &country_ie_len); 825 if (!end_channel) 826 return NULL; 827 828 if (!chan_in_band(end_channel, band)) 829 return NULL; 830 831 cur_sub_max_channel = end_channel; 832 833 /* Basic sanity check */ 834 if (cur_sub_max_channel < cur_channel) 835 return NULL; 836 837 /* 838 * Do not allow overlapping channels. Also channels 839 * passed in each subband must be monotonically 840 * increasing 841 */ 842 if (last_sub_max_channel) { 843 if (cur_channel <= last_sub_max_channel) 844 return NULL; 845 if (cur_sub_max_channel <= last_sub_max_channel) 846 return NULL; 847 } 848 849 /* 850 * When dot11RegulatoryClassesRequired is supported 851 * we can throw ext triplets as part of this soup, 852 * for now we don't care when those change as we 853 * don't support them 854 */ 855 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) | 856 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) | 857 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24); 858 859 last_sub_max_channel = cur_sub_max_channel; 860 861 num_rules++; 862 863 if (country_ie_len >= 3) { 864 country_ie += 3; 865 country_ie_len -= 3; 866 } 867 868 /* 869 * Note: this is not a IEEE requirement but 870 * simply a memory requirement 871 */ 872 if (num_rules > NL80211_MAX_SUPP_REG_RULES) 873 return NULL; 874 } 875 876 country_ie = triplets_start; 877 country_ie_len = len_at_triplet; 878 879 size_of_regd = sizeof(struct ieee80211_regdomain) + 880 (num_rules * sizeof(struct ieee80211_reg_rule)); 881 882 rd = kzalloc(size_of_regd, GFP_KERNEL); 883 if (!rd) 884 return NULL; 885 886 rd->n_reg_rules = num_rules; 887 rd->alpha2[0] = alpha2[0]; 888 rd->alpha2[1] = alpha2[1]; 889 890 /* This time around we fill in the rd */ 891 while (country_ie_len >= 3) { 892 int end_channel = 0; 893 struct ieee80211_country_ie_triplet *triplet = 894 (struct ieee80211_country_ie_triplet *) country_ie; 895 struct ieee80211_reg_rule *reg_rule = NULL; 896 struct ieee80211_freq_range *freq_range = NULL; 897 struct ieee80211_power_rule *power_rule = NULL; 898 899 /* 900 * Must parse if dot11RegulatoryClassesRequired is true, 901 * we don't support this yet 902 */ 903 if (triplet->ext.reg_extension_id >= 904 IEEE80211_COUNTRY_EXTENSION_ID) { 905 country_ie += 3; 906 country_ie_len -= 3; 907 continue; 908 } 909 910 if (triplet->chans.first_channel == 0) { 911 country_ie++; 912 country_ie_len--; 913 break; 914 } 915 916 reg_rule = &rd->reg_rules[i]; 917 freq_range = ®_rule->freq_range; 918 power_rule = ®_rule->power_rule; 919 920 reg_rule->flags = flags; 921 922 /* 2 GHz */ 923 if (band == IEEE80211_BAND_2GHZ) 924 end_channel = triplet->chans.first_channel + 925 triplet->chans.num_channels -1; 926 else 927 end_channel = triplet->chans.first_channel + 928 (4 * (triplet->chans.num_channels - 1)); 929 930 end_channel = max_subband_chan(band, 931 triplet->chans.first_channel, 932 end_channel, 933 triplet->chans.max_power, 934 &country_ie, 935 &country_ie_len); 936 937 /* 938 * The +10 is since the regulatory domain expects 939 * the actual band edge, not the center of freq for 940 * its start and end freqs, assuming 20 MHz bandwidth on 941 * the channels passed 942 */ 943 freq_range->start_freq_khz = 944 MHZ_TO_KHZ(ieee80211_channel_to_frequency( 945 triplet->chans.first_channel) - 10); 946 freq_range->end_freq_khz = 947 MHZ_TO_KHZ(ieee80211_channel_to_frequency( 948 end_channel) + 10); 949 950 /* 951 * These are large arbitrary values we use to intersect later. 952 * Increment this if we ever support >= 40 MHz channels 953 * in IEEE 802.11 954 */ 955 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40); 956 power_rule->max_antenna_gain = DBI_TO_MBI(100); 957 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power); 958 959 i++; 960 961 if (country_ie_len >= 3) { 962 country_ie += 3; 963 country_ie_len -= 3; 964 } 965 966 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES); 967 } 968 969 return rd; 970 } 971 972 973 /* 974 * Helper for regdom_intersect(), this does the real 975 * mathematical intersection fun 976 */ 977 static int reg_rules_intersect( 978 const struct ieee80211_reg_rule *rule1, 979 const struct ieee80211_reg_rule *rule2, 980 struct ieee80211_reg_rule *intersected_rule) 981 { 982 const struct ieee80211_freq_range *freq_range1, *freq_range2; 983 struct ieee80211_freq_range *freq_range; 984 const struct ieee80211_power_rule *power_rule1, *power_rule2; 985 struct ieee80211_power_rule *power_rule; 986 u32 freq_diff; 987 988 freq_range1 = &rule1->freq_range; 989 freq_range2 = &rule2->freq_range; 990 freq_range = &intersected_rule->freq_range; 991 992 power_rule1 = &rule1->power_rule; 993 power_rule2 = &rule2->power_rule; 994 power_rule = &intersected_rule->power_rule; 995 996 freq_range->start_freq_khz = max(freq_range1->start_freq_khz, 997 freq_range2->start_freq_khz); 998 freq_range->end_freq_khz = min(freq_range1->end_freq_khz, 999 freq_range2->end_freq_khz); 1000 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz, 1001 freq_range2->max_bandwidth_khz); 1002 1003 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; 1004 if (freq_range->max_bandwidth_khz > freq_diff) 1005 freq_range->max_bandwidth_khz = freq_diff; 1006 1007 power_rule->max_eirp = min(power_rule1->max_eirp, 1008 power_rule2->max_eirp); 1009 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, 1010 power_rule2->max_antenna_gain); 1011 1012 intersected_rule->flags = (rule1->flags | rule2->flags); 1013 1014 if (!is_valid_reg_rule(intersected_rule)) 1015 return -EINVAL; 1016 1017 return 0; 1018 } 1019 1020 /** 1021 * regdom_intersect - do the intersection between two regulatory domains 1022 * @rd1: first regulatory domain 1023 * @rd2: second regulatory domain 1024 * 1025 * Use this function to get the intersection between two regulatory domains. 1026 * Once completed we will mark the alpha2 for the rd as intersected, "98", 1027 * as no one single alpha2 can represent this regulatory domain. 1028 * 1029 * Returns a pointer to the regulatory domain structure which will hold the 1030 * resulting intersection of rules between rd1 and rd2. We will 1031 * kzalloc() this structure for you. 1032 */ 1033 static struct ieee80211_regdomain *regdom_intersect( 1034 const struct ieee80211_regdomain *rd1, 1035 const struct ieee80211_regdomain *rd2) 1036 { 1037 int r, size_of_regd; 1038 unsigned int x, y; 1039 unsigned int num_rules = 0, rule_idx = 0; 1040 const struct ieee80211_reg_rule *rule1, *rule2; 1041 struct ieee80211_reg_rule *intersected_rule; 1042 struct ieee80211_regdomain *rd; 1043 /* This is just a dummy holder to help us count */ 1044 struct ieee80211_reg_rule irule; 1045 1046 /* Uses the stack temporarily for counter arithmetic */ 1047 intersected_rule = &irule; 1048 1049 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule)); 1050 1051 if (!rd1 || !rd2) 1052 return NULL; 1053 1054 /* 1055 * First we get a count of the rules we'll need, then we actually 1056 * build them. This is to so we can malloc() and free() a 1057 * regdomain once. The reason we use reg_rules_intersect() here 1058 * is it will return -EINVAL if the rule computed makes no sense. 1059 * All rules that do check out OK are valid. 1060 */ 1061 1062 for (x = 0; x < rd1->n_reg_rules; x++) { 1063 rule1 = &rd1->reg_rules[x]; 1064 for (y = 0; y < rd2->n_reg_rules; y++) { 1065 rule2 = &rd2->reg_rules[y]; 1066 if (!reg_rules_intersect(rule1, rule2, 1067 intersected_rule)) 1068 num_rules++; 1069 memset(intersected_rule, 0, 1070 sizeof(struct ieee80211_reg_rule)); 1071 } 1072 } 1073 1074 if (!num_rules) 1075 return NULL; 1076 1077 size_of_regd = sizeof(struct ieee80211_regdomain) + 1078 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule)); 1079 1080 rd = kzalloc(size_of_regd, GFP_KERNEL); 1081 if (!rd) 1082 return NULL; 1083 1084 for (x = 0; x < rd1->n_reg_rules; x++) { 1085 rule1 = &rd1->reg_rules[x]; 1086 for (y = 0; y < rd2->n_reg_rules; y++) { 1087 rule2 = &rd2->reg_rules[y]; 1088 /* 1089 * This time around instead of using the stack lets 1090 * write to the target rule directly saving ourselves 1091 * a memcpy() 1092 */ 1093 intersected_rule = &rd->reg_rules[rule_idx]; 1094 r = reg_rules_intersect(rule1, rule2, 1095 intersected_rule); 1096 /* 1097 * No need to memset here the intersected rule here as 1098 * we're not using the stack anymore 1099 */ 1100 if (r) 1101 continue; 1102 rule_idx++; 1103 } 1104 } 1105 1106 if (rule_idx != num_rules) { 1107 kfree(rd); 1108 return NULL; 1109 } 1110 1111 rd->n_reg_rules = num_rules; 1112 rd->alpha2[0] = '9'; 1113 rd->alpha2[1] = '8'; 1114 1115 return rd; 1116 } 1117 1118 /* 1119 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may 1120 * want to just have the channel structure use these 1121 */ 1122 static u32 map_regdom_flags(u32 rd_flags) 1123 { 1124 u32 channel_flags = 0; 1125 if (rd_flags & NL80211_RRF_PASSIVE_SCAN) 1126 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN; 1127 if (rd_flags & NL80211_RRF_NO_IBSS) 1128 channel_flags |= IEEE80211_CHAN_NO_IBSS; 1129 if (rd_flags & NL80211_RRF_DFS) 1130 channel_flags |= IEEE80211_CHAN_RADAR; 1131 return channel_flags; 1132 } 1133 1134 static int freq_reg_info_regd(struct wiphy *wiphy, 1135 u32 center_freq, 1136 u32 desired_bw_khz, 1137 const struct ieee80211_reg_rule **reg_rule, 1138 const struct ieee80211_regdomain *custom_regd) 1139 { 1140 int i; 1141 bool band_rule_found = false; 1142 const struct ieee80211_regdomain *regd; 1143 bool bw_fits = false; 1144 1145 if (!desired_bw_khz) 1146 desired_bw_khz = MHZ_TO_KHZ(20); 1147 1148 regd = custom_regd ? custom_regd : cfg80211_regdomain; 1149 1150 /* 1151 * Follow the driver's regulatory domain, if present, unless a country 1152 * IE has been processed or a user wants to help complaince further 1153 */ 1154 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && 1155 last_request->initiator != NL80211_REGDOM_SET_BY_USER && 1156 wiphy->regd) 1157 regd = wiphy->regd; 1158 1159 if (!regd) 1160 return -EINVAL; 1161 1162 for (i = 0; i < regd->n_reg_rules; i++) { 1163 const struct ieee80211_reg_rule *rr; 1164 const struct ieee80211_freq_range *fr = NULL; 1165 const struct ieee80211_power_rule *pr = NULL; 1166 1167 rr = ®d->reg_rules[i]; 1168 fr = &rr->freq_range; 1169 pr = &rr->power_rule; 1170 1171 /* 1172 * We only need to know if one frequency rule was 1173 * was in center_freq's band, that's enough, so lets 1174 * not overwrite it once found 1175 */ 1176 if (!band_rule_found) 1177 band_rule_found = freq_in_rule_band(fr, center_freq); 1178 1179 bw_fits = reg_does_bw_fit(fr, 1180 center_freq, 1181 desired_bw_khz); 1182 1183 if (band_rule_found && bw_fits) { 1184 *reg_rule = rr; 1185 return 0; 1186 } 1187 } 1188 1189 if (!band_rule_found) 1190 return -ERANGE; 1191 1192 return -EINVAL; 1193 } 1194 EXPORT_SYMBOL(freq_reg_info); 1195 1196 int freq_reg_info(struct wiphy *wiphy, 1197 u32 center_freq, 1198 u32 desired_bw_khz, 1199 const struct ieee80211_reg_rule **reg_rule) 1200 { 1201 assert_cfg80211_lock(); 1202 return freq_reg_info_regd(wiphy, 1203 center_freq, 1204 desired_bw_khz, 1205 reg_rule, 1206 NULL); 1207 } 1208 1209 /* 1210 * Note that right now we assume the desired channel bandwidth 1211 * is always 20 MHz for each individual channel (HT40 uses 20 MHz 1212 * per channel, the primary and the extension channel). To support 1213 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a 1214 * new ieee80211_channel.target_bw and re run the regulatory check 1215 * on the wiphy with the target_bw specified. Then we can simply use 1216 * that below for the desired_bw_khz below. 1217 */ 1218 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band, 1219 unsigned int chan_idx) 1220 { 1221 int r; 1222 u32 flags, bw_flags = 0; 1223 u32 desired_bw_khz = MHZ_TO_KHZ(20); 1224 const struct ieee80211_reg_rule *reg_rule = NULL; 1225 const struct ieee80211_power_rule *power_rule = NULL; 1226 const struct ieee80211_freq_range *freq_range = NULL; 1227 struct ieee80211_supported_band *sband; 1228 struct ieee80211_channel *chan; 1229 struct wiphy *request_wiphy = NULL; 1230 1231 assert_cfg80211_lock(); 1232 1233 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); 1234 1235 sband = wiphy->bands[band]; 1236 BUG_ON(chan_idx >= sband->n_channels); 1237 chan = &sband->channels[chan_idx]; 1238 1239 flags = chan->orig_flags; 1240 1241 r = freq_reg_info(wiphy, 1242 MHZ_TO_KHZ(chan->center_freq), 1243 desired_bw_khz, 1244 ®_rule); 1245 1246 if (r) { 1247 /* 1248 * This means no regulatory rule was found in the country IE 1249 * with a frequency range on the center_freq's band, since 1250 * IEEE-802.11 allows for a country IE to have a subset of the 1251 * regulatory information provided in a country we ignore 1252 * disabling the channel unless at least one reg rule was 1253 * found on the center_freq's band. For details see this 1254 * clarification: 1255 * 1256 * http://tinyurl.com/11d-clarification 1257 */ 1258 if (r == -ERANGE && 1259 last_request->initiator == 1260 NL80211_REGDOM_SET_BY_COUNTRY_IE) { 1261 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz " 1262 "intact on %s - no rule found in band on " 1263 "Country IE\n", 1264 chan->center_freq, wiphy_name(wiphy)); 1265 } else { 1266 /* 1267 * In this case we know the country IE has at least one reg rule 1268 * for the band so we respect its band definitions 1269 */ 1270 if (last_request->initiator == 1271 NL80211_REGDOM_SET_BY_COUNTRY_IE) 1272 REG_DBG_PRINT("cfg80211: Disabling " 1273 "channel %d MHz on %s due to " 1274 "Country IE\n", 1275 chan->center_freq, wiphy_name(wiphy)); 1276 flags |= IEEE80211_CHAN_DISABLED; 1277 chan->flags = flags; 1278 } 1279 return; 1280 } 1281 1282 power_rule = ®_rule->power_rule; 1283 freq_range = ®_rule->freq_range; 1284 1285 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40)) 1286 bw_flags = IEEE80211_CHAN_NO_HT40; 1287 1288 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER && 1289 request_wiphy && request_wiphy == wiphy && 1290 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) { 1291 /* 1292 * This gaurantees the driver's requested regulatory domain 1293 * will always be used as a base for further regulatory 1294 * settings 1295 */ 1296 chan->flags = chan->orig_flags = 1297 map_regdom_flags(reg_rule->flags) | bw_flags; 1298 chan->max_antenna_gain = chan->orig_mag = 1299 (int) MBI_TO_DBI(power_rule->max_antenna_gain); 1300 chan->max_power = chan->orig_mpwr = 1301 (int) MBM_TO_DBM(power_rule->max_eirp); 1302 return; 1303 } 1304 1305 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags); 1306 chan->max_antenna_gain = min(chan->orig_mag, 1307 (int) MBI_TO_DBI(power_rule->max_antenna_gain)); 1308 if (chan->orig_mpwr) 1309 chan->max_power = min(chan->orig_mpwr, 1310 (int) MBM_TO_DBM(power_rule->max_eirp)); 1311 else 1312 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); 1313 } 1314 1315 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band) 1316 { 1317 unsigned int i; 1318 struct ieee80211_supported_band *sband; 1319 1320 BUG_ON(!wiphy->bands[band]); 1321 sband = wiphy->bands[band]; 1322 1323 for (i = 0; i < sband->n_channels; i++) 1324 handle_channel(wiphy, band, i); 1325 } 1326 1327 static bool ignore_reg_update(struct wiphy *wiphy, 1328 enum nl80211_reg_initiator initiator) 1329 { 1330 if (!last_request) 1331 return true; 1332 if (initiator == NL80211_REGDOM_SET_BY_CORE && 1333 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) 1334 return true; 1335 /* 1336 * wiphy->regd will be set once the device has its own 1337 * desired regulatory domain set 1338 */ 1339 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd && 1340 !is_world_regdom(last_request->alpha2)) 1341 return true; 1342 return false; 1343 } 1344 1345 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator) 1346 { 1347 struct cfg80211_registered_device *rdev; 1348 1349 list_for_each_entry(rdev, &cfg80211_rdev_list, list) 1350 wiphy_update_regulatory(&rdev->wiphy, initiator); 1351 } 1352 1353 static void handle_reg_beacon(struct wiphy *wiphy, 1354 unsigned int chan_idx, 1355 struct reg_beacon *reg_beacon) 1356 { 1357 struct ieee80211_supported_band *sband; 1358 struct ieee80211_channel *chan; 1359 bool channel_changed = false; 1360 struct ieee80211_channel chan_before; 1361 1362 assert_cfg80211_lock(); 1363 1364 sband = wiphy->bands[reg_beacon->chan.band]; 1365 chan = &sband->channels[chan_idx]; 1366 1367 if (likely(chan->center_freq != reg_beacon->chan.center_freq)) 1368 return; 1369 1370 if (chan->beacon_found) 1371 return; 1372 1373 chan->beacon_found = true; 1374 1375 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS) 1376 return; 1377 1378 chan_before.center_freq = chan->center_freq; 1379 chan_before.flags = chan->flags; 1380 1381 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) { 1382 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN; 1383 channel_changed = true; 1384 } 1385 1386 if (chan->flags & IEEE80211_CHAN_NO_IBSS) { 1387 chan->flags &= ~IEEE80211_CHAN_NO_IBSS; 1388 channel_changed = true; 1389 } 1390 1391 if (channel_changed) 1392 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan); 1393 } 1394 1395 /* 1396 * Called when a scan on a wiphy finds a beacon on 1397 * new channel 1398 */ 1399 static void wiphy_update_new_beacon(struct wiphy *wiphy, 1400 struct reg_beacon *reg_beacon) 1401 { 1402 unsigned int i; 1403 struct ieee80211_supported_band *sband; 1404 1405 assert_cfg80211_lock(); 1406 1407 if (!wiphy->bands[reg_beacon->chan.band]) 1408 return; 1409 1410 sband = wiphy->bands[reg_beacon->chan.band]; 1411 1412 for (i = 0; i < sband->n_channels; i++) 1413 handle_reg_beacon(wiphy, i, reg_beacon); 1414 } 1415 1416 /* 1417 * Called upon reg changes or a new wiphy is added 1418 */ 1419 static void wiphy_update_beacon_reg(struct wiphy *wiphy) 1420 { 1421 unsigned int i; 1422 struct ieee80211_supported_band *sband; 1423 struct reg_beacon *reg_beacon; 1424 1425 assert_cfg80211_lock(); 1426 1427 if (list_empty(®_beacon_list)) 1428 return; 1429 1430 list_for_each_entry(reg_beacon, ®_beacon_list, list) { 1431 if (!wiphy->bands[reg_beacon->chan.band]) 1432 continue; 1433 sband = wiphy->bands[reg_beacon->chan.band]; 1434 for (i = 0; i < sband->n_channels; i++) 1435 handle_reg_beacon(wiphy, i, reg_beacon); 1436 } 1437 } 1438 1439 static bool reg_is_world_roaming(struct wiphy *wiphy) 1440 { 1441 if (is_world_regdom(cfg80211_regdomain->alpha2) || 1442 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2))) 1443 return true; 1444 if (last_request && 1445 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && 1446 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) 1447 return true; 1448 return false; 1449 } 1450 1451 /* Reap the advantages of previously found beacons */ 1452 static void reg_process_beacons(struct wiphy *wiphy) 1453 { 1454 /* 1455 * Means we are just firing up cfg80211, so no beacons would 1456 * have been processed yet. 1457 */ 1458 if (!last_request) 1459 return; 1460 if (!reg_is_world_roaming(wiphy)) 1461 return; 1462 wiphy_update_beacon_reg(wiphy); 1463 } 1464 1465 static bool is_ht40_not_allowed(struct ieee80211_channel *chan) 1466 { 1467 if (!chan) 1468 return true; 1469 if (chan->flags & IEEE80211_CHAN_DISABLED) 1470 return true; 1471 /* This would happen when regulatory rules disallow HT40 completely */ 1472 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40))) 1473 return true; 1474 return false; 1475 } 1476 1477 static void reg_process_ht_flags_channel(struct wiphy *wiphy, 1478 enum ieee80211_band band, 1479 unsigned int chan_idx) 1480 { 1481 struct ieee80211_supported_band *sband; 1482 struct ieee80211_channel *channel; 1483 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL; 1484 unsigned int i; 1485 1486 assert_cfg80211_lock(); 1487 1488 sband = wiphy->bands[band]; 1489 BUG_ON(chan_idx >= sband->n_channels); 1490 channel = &sband->channels[chan_idx]; 1491 1492 if (is_ht40_not_allowed(channel)) { 1493 channel->flags |= IEEE80211_CHAN_NO_HT40; 1494 return; 1495 } 1496 1497 /* 1498 * We need to ensure the extension channels exist to 1499 * be able to use HT40- or HT40+, this finds them (or not) 1500 */ 1501 for (i = 0; i < sband->n_channels; i++) { 1502 struct ieee80211_channel *c = &sband->channels[i]; 1503 if (c->center_freq == (channel->center_freq - 20)) 1504 channel_before = c; 1505 if (c->center_freq == (channel->center_freq + 20)) 1506 channel_after = c; 1507 } 1508 1509 /* 1510 * Please note that this assumes target bandwidth is 20 MHz, 1511 * if that ever changes we also need to change the below logic 1512 * to include that as well. 1513 */ 1514 if (is_ht40_not_allowed(channel_before)) 1515 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS; 1516 else 1517 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS; 1518 1519 if (is_ht40_not_allowed(channel_after)) 1520 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS; 1521 else 1522 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS; 1523 } 1524 1525 static void reg_process_ht_flags_band(struct wiphy *wiphy, 1526 enum ieee80211_band band) 1527 { 1528 unsigned int i; 1529 struct ieee80211_supported_band *sband; 1530 1531 BUG_ON(!wiphy->bands[band]); 1532 sband = wiphy->bands[band]; 1533 1534 for (i = 0; i < sband->n_channels; i++) 1535 reg_process_ht_flags_channel(wiphy, band, i); 1536 } 1537 1538 static void reg_process_ht_flags(struct wiphy *wiphy) 1539 { 1540 enum ieee80211_band band; 1541 1542 if (!wiphy) 1543 return; 1544 1545 for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 1546 if (wiphy->bands[band]) 1547 reg_process_ht_flags_band(wiphy, band); 1548 } 1549 1550 } 1551 1552 void wiphy_update_regulatory(struct wiphy *wiphy, 1553 enum nl80211_reg_initiator initiator) 1554 { 1555 enum ieee80211_band band; 1556 1557 if (ignore_reg_update(wiphy, initiator)) 1558 goto out; 1559 for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 1560 if (wiphy->bands[band]) 1561 handle_band(wiphy, band); 1562 } 1563 out: 1564 reg_process_beacons(wiphy); 1565 reg_process_ht_flags(wiphy); 1566 if (wiphy->reg_notifier) 1567 wiphy->reg_notifier(wiphy, last_request); 1568 } 1569 1570 static void handle_channel_custom(struct wiphy *wiphy, 1571 enum ieee80211_band band, 1572 unsigned int chan_idx, 1573 const struct ieee80211_regdomain *regd) 1574 { 1575 int r; 1576 u32 desired_bw_khz = MHZ_TO_KHZ(20); 1577 u32 bw_flags = 0; 1578 const struct ieee80211_reg_rule *reg_rule = NULL; 1579 const struct ieee80211_power_rule *power_rule = NULL; 1580 const struct ieee80211_freq_range *freq_range = NULL; 1581 struct ieee80211_supported_band *sband; 1582 struct ieee80211_channel *chan; 1583 1584 assert_reg_lock(); 1585 1586 sband = wiphy->bands[band]; 1587 BUG_ON(chan_idx >= sband->n_channels); 1588 chan = &sband->channels[chan_idx]; 1589 1590 r = freq_reg_info_regd(wiphy, 1591 MHZ_TO_KHZ(chan->center_freq), 1592 desired_bw_khz, 1593 ®_rule, 1594 regd); 1595 1596 if (r) { 1597 chan->flags = IEEE80211_CHAN_DISABLED; 1598 return; 1599 } 1600 1601 power_rule = ®_rule->power_rule; 1602 freq_range = ®_rule->freq_range; 1603 1604 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40)) 1605 bw_flags = IEEE80211_CHAN_NO_HT40; 1606 1607 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags; 1608 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); 1609 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); 1610 } 1611 1612 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band, 1613 const struct ieee80211_regdomain *regd) 1614 { 1615 unsigned int i; 1616 struct ieee80211_supported_band *sband; 1617 1618 BUG_ON(!wiphy->bands[band]); 1619 sband = wiphy->bands[band]; 1620 1621 for (i = 0; i < sband->n_channels; i++) 1622 handle_channel_custom(wiphy, band, i, regd); 1623 } 1624 1625 /* Used by drivers prior to wiphy registration */ 1626 void wiphy_apply_custom_regulatory(struct wiphy *wiphy, 1627 const struct ieee80211_regdomain *regd) 1628 { 1629 enum ieee80211_band band; 1630 unsigned int bands_set = 0; 1631 1632 mutex_lock(®_mutex); 1633 for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 1634 if (!wiphy->bands[band]) 1635 continue; 1636 handle_band_custom(wiphy, band, regd); 1637 bands_set++; 1638 } 1639 mutex_unlock(®_mutex); 1640 1641 /* 1642 * no point in calling this if it won't have any effect 1643 * on your device's supportd bands. 1644 */ 1645 WARN_ON(!bands_set); 1646 } 1647 EXPORT_SYMBOL(wiphy_apply_custom_regulatory); 1648 1649 /* 1650 * Return value which can be used by ignore_request() to indicate 1651 * it has been determined we should intersect two regulatory domains 1652 */ 1653 #define REG_INTERSECT 1 1654 1655 /* This has the logic which determines when a new request 1656 * should be ignored. */ 1657 static int ignore_request(struct wiphy *wiphy, 1658 struct regulatory_request *pending_request) 1659 { 1660 struct wiphy *last_wiphy = NULL; 1661 1662 assert_cfg80211_lock(); 1663 1664 /* All initial requests are respected */ 1665 if (!last_request) 1666 return 0; 1667 1668 switch (pending_request->initiator) { 1669 case NL80211_REGDOM_SET_BY_CORE: 1670 return 0; 1671 case NL80211_REGDOM_SET_BY_COUNTRY_IE: 1672 1673 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); 1674 1675 if (unlikely(!is_an_alpha2(pending_request->alpha2))) 1676 return -EINVAL; 1677 if (last_request->initiator == 1678 NL80211_REGDOM_SET_BY_COUNTRY_IE) { 1679 if (last_wiphy != wiphy) { 1680 /* 1681 * Two cards with two APs claiming different 1682 * Country IE alpha2s. We could 1683 * intersect them, but that seems unlikely 1684 * to be correct. Reject second one for now. 1685 */ 1686 if (regdom_changes(pending_request->alpha2)) 1687 return -EOPNOTSUPP; 1688 return -EALREADY; 1689 } 1690 /* 1691 * Two consecutive Country IE hints on the same wiphy. 1692 * This should be picked up early by the driver/stack 1693 */ 1694 if (WARN_ON(regdom_changes(pending_request->alpha2))) 1695 return 0; 1696 return -EALREADY; 1697 } 1698 return REG_INTERSECT; 1699 case NL80211_REGDOM_SET_BY_DRIVER: 1700 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) { 1701 if (regdom_changes(pending_request->alpha2)) 1702 return 0; 1703 return -EALREADY; 1704 } 1705 1706 /* 1707 * This would happen if you unplug and plug your card 1708 * back in or if you add a new device for which the previously 1709 * loaded card also agrees on the regulatory domain. 1710 */ 1711 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER && 1712 !regdom_changes(pending_request->alpha2)) 1713 return -EALREADY; 1714 1715 return REG_INTERSECT; 1716 case NL80211_REGDOM_SET_BY_USER: 1717 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) 1718 return REG_INTERSECT; 1719 /* 1720 * If the user knows better the user should set the regdom 1721 * to their country before the IE is picked up 1722 */ 1723 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER && 1724 last_request->intersect) 1725 return -EOPNOTSUPP; 1726 /* 1727 * Process user requests only after previous user/driver/core 1728 * requests have been processed 1729 */ 1730 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE || 1731 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER || 1732 last_request->initiator == NL80211_REGDOM_SET_BY_USER) { 1733 if (regdom_changes(last_request->alpha2)) 1734 return -EAGAIN; 1735 } 1736 1737 if (!regdom_changes(pending_request->alpha2)) 1738 return -EALREADY; 1739 1740 return 0; 1741 } 1742 1743 return -EINVAL; 1744 } 1745 1746 /** 1747 * __regulatory_hint - hint to the wireless core a regulatory domain 1748 * @wiphy: if the hint comes from country information from an AP, this 1749 * is required to be set to the wiphy that received the information 1750 * @pending_request: the regulatory request currently being processed 1751 * 1752 * The Wireless subsystem can use this function to hint to the wireless core 1753 * what it believes should be the current regulatory domain. 1754 * 1755 * Returns zero if all went fine, %-EALREADY if a regulatory domain had 1756 * already been set or other standard error codes. 1757 * 1758 * Caller must hold &cfg80211_mutex and ®_mutex 1759 */ 1760 static int __regulatory_hint(struct wiphy *wiphy, 1761 struct regulatory_request *pending_request) 1762 { 1763 bool intersect = false; 1764 int r = 0; 1765 1766 assert_cfg80211_lock(); 1767 1768 r = ignore_request(wiphy, pending_request); 1769 1770 if (r == REG_INTERSECT) { 1771 if (pending_request->initiator == 1772 NL80211_REGDOM_SET_BY_DRIVER) { 1773 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); 1774 if (r) { 1775 kfree(pending_request); 1776 return r; 1777 } 1778 } 1779 intersect = true; 1780 } else if (r) { 1781 /* 1782 * If the regulatory domain being requested by the 1783 * driver has already been set just copy it to the 1784 * wiphy 1785 */ 1786 if (r == -EALREADY && 1787 pending_request->initiator == 1788 NL80211_REGDOM_SET_BY_DRIVER) { 1789 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); 1790 if (r) { 1791 kfree(pending_request); 1792 return r; 1793 } 1794 r = -EALREADY; 1795 goto new_request; 1796 } 1797 kfree(pending_request); 1798 return r; 1799 } 1800 1801 new_request: 1802 kfree(last_request); 1803 1804 last_request = pending_request; 1805 last_request->intersect = intersect; 1806 1807 pending_request = NULL; 1808 1809 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) { 1810 user_alpha2[0] = last_request->alpha2[0]; 1811 user_alpha2[1] = last_request->alpha2[1]; 1812 } 1813 1814 /* When r == REG_INTERSECT we do need to call CRDA */ 1815 if (r < 0) { 1816 /* 1817 * Since CRDA will not be called in this case as we already 1818 * have applied the requested regulatory domain before we just 1819 * inform userspace we have processed the request 1820 */ 1821 if (r == -EALREADY) 1822 nl80211_send_reg_change_event(last_request); 1823 return r; 1824 } 1825 1826 return call_crda(last_request->alpha2); 1827 } 1828 1829 /* This processes *all* regulatory hints */ 1830 static void reg_process_hint(struct regulatory_request *reg_request) 1831 { 1832 int r = 0; 1833 struct wiphy *wiphy = NULL; 1834 1835 BUG_ON(!reg_request->alpha2); 1836 1837 mutex_lock(&cfg80211_mutex); 1838 mutex_lock(®_mutex); 1839 1840 if (wiphy_idx_valid(reg_request->wiphy_idx)) 1841 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx); 1842 1843 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER && 1844 !wiphy) { 1845 kfree(reg_request); 1846 goto out; 1847 } 1848 1849 r = __regulatory_hint(wiphy, reg_request); 1850 /* This is required so that the orig_* parameters are saved */ 1851 if (r == -EALREADY && wiphy && 1852 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) 1853 wiphy_update_regulatory(wiphy, reg_request->initiator); 1854 out: 1855 mutex_unlock(®_mutex); 1856 mutex_unlock(&cfg80211_mutex); 1857 } 1858 1859 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */ 1860 static void reg_process_pending_hints(void) 1861 { 1862 struct regulatory_request *reg_request; 1863 1864 spin_lock(®_requests_lock); 1865 while (!list_empty(®_requests_list)) { 1866 reg_request = list_first_entry(®_requests_list, 1867 struct regulatory_request, 1868 list); 1869 list_del_init(®_request->list); 1870 1871 spin_unlock(®_requests_lock); 1872 reg_process_hint(reg_request); 1873 spin_lock(®_requests_lock); 1874 } 1875 spin_unlock(®_requests_lock); 1876 } 1877 1878 /* Processes beacon hints -- this has nothing to do with country IEs */ 1879 static void reg_process_pending_beacon_hints(void) 1880 { 1881 struct cfg80211_registered_device *rdev; 1882 struct reg_beacon *pending_beacon, *tmp; 1883 1884 /* 1885 * No need to hold the reg_mutex here as we just touch wiphys 1886 * and do not read or access regulatory variables. 1887 */ 1888 mutex_lock(&cfg80211_mutex); 1889 1890 /* This goes through the _pending_ beacon list */ 1891 spin_lock_bh(®_pending_beacons_lock); 1892 1893 if (list_empty(®_pending_beacons)) { 1894 spin_unlock_bh(®_pending_beacons_lock); 1895 goto out; 1896 } 1897 1898 list_for_each_entry_safe(pending_beacon, tmp, 1899 ®_pending_beacons, list) { 1900 1901 list_del_init(&pending_beacon->list); 1902 1903 /* Applies the beacon hint to current wiphys */ 1904 list_for_each_entry(rdev, &cfg80211_rdev_list, list) 1905 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon); 1906 1907 /* Remembers the beacon hint for new wiphys or reg changes */ 1908 list_add_tail(&pending_beacon->list, ®_beacon_list); 1909 } 1910 1911 spin_unlock_bh(®_pending_beacons_lock); 1912 out: 1913 mutex_unlock(&cfg80211_mutex); 1914 } 1915 1916 static void reg_todo(struct work_struct *work) 1917 { 1918 reg_process_pending_hints(); 1919 reg_process_pending_beacon_hints(); 1920 } 1921 1922 static DECLARE_WORK(reg_work, reg_todo); 1923 1924 static void queue_regulatory_request(struct regulatory_request *request) 1925 { 1926 spin_lock(®_requests_lock); 1927 list_add_tail(&request->list, ®_requests_list); 1928 spin_unlock(®_requests_lock); 1929 1930 schedule_work(®_work); 1931 } 1932 1933 /* 1934 * Core regulatory hint -- happens during cfg80211_init() 1935 * and when we restore regulatory settings. 1936 */ 1937 static int regulatory_hint_core(const char *alpha2) 1938 { 1939 struct regulatory_request *request; 1940 1941 kfree(last_request); 1942 last_request = NULL; 1943 1944 request = kzalloc(sizeof(struct regulatory_request), 1945 GFP_KERNEL); 1946 if (!request) 1947 return -ENOMEM; 1948 1949 request->alpha2[0] = alpha2[0]; 1950 request->alpha2[1] = alpha2[1]; 1951 request->initiator = NL80211_REGDOM_SET_BY_CORE; 1952 1953 /* 1954 * This ensures last_request is populated once modules 1955 * come swinging in and calling regulatory hints and 1956 * wiphy_apply_custom_regulatory(). 1957 */ 1958 reg_process_hint(request); 1959 1960 return 0; 1961 } 1962 1963 /* User hints */ 1964 int regulatory_hint_user(const char *alpha2) 1965 { 1966 struct regulatory_request *request; 1967 1968 BUG_ON(!alpha2); 1969 1970 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); 1971 if (!request) 1972 return -ENOMEM; 1973 1974 request->wiphy_idx = WIPHY_IDX_STALE; 1975 request->alpha2[0] = alpha2[0]; 1976 request->alpha2[1] = alpha2[1]; 1977 request->initiator = NL80211_REGDOM_SET_BY_USER; 1978 1979 queue_regulatory_request(request); 1980 1981 return 0; 1982 } 1983 1984 /* Driver hints */ 1985 int regulatory_hint(struct wiphy *wiphy, const char *alpha2) 1986 { 1987 struct regulatory_request *request; 1988 1989 BUG_ON(!alpha2); 1990 BUG_ON(!wiphy); 1991 1992 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); 1993 if (!request) 1994 return -ENOMEM; 1995 1996 request->wiphy_idx = get_wiphy_idx(wiphy); 1997 1998 /* Must have registered wiphy first */ 1999 BUG_ON(!wiphy_idx_valid(request->wiphy_idx)); 2000 2001 request->alpha2[0] = alpha2[0]; 2002 request->alpha2[1] = alpha2[1]; 2003 request->initiator = NL80211_REGDOM_SET_BY_DRIVER; 2004 2005 queue_regulatory_request(request); 2006 2007 return 0; 2008 } 2009 EXPORT_SYMBOL(regulatory_hint); 2010 2011 /* Caller must hold reg_mutex */ 2012 static bool reg_same_country_ie_hint(struct wiphy *wiphy, 2013 u32 country_ie_checksum) 2014 { 2015 struct wiphy *request_wiphy; 2016 2017 assert_reg_lock(); 2018 2019 if (unlikely(last_request->initiator != 2020 NL80211_REGDOM_SET_BY_COUNTRY_IE)) 2021 return false; 2022 2023 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); 2024 2025 if (!request_wiphy) 2026 return false; 2027 2028 if (likely(request_wiphy != wiphy)) 2029 return !country_ie_integrity_changes(country_ie_checksum); 2030 /* 2031 * We should not have let these through at this point, they 2032 * should have been picked up earlier by the first alpha2 check 2033 * on the device 2034 */ 2035 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum))) 2036 return true; 2037 return false; 2038 } 2039 2040 /* 2041 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and 2042 * therefore cannot iterate over the rdev list here. 2043 */ 2044 void regulatory_hint_11d(struct wiphy *wiphy, 2045 enum ieee80211_band band, 2046 u8 *country_ie, 2047 u8 country_ie_len) 2048 { 2049 struct ieee80211_regdomain *rd = NULL; 2050 char alpha2[2]; 2051 u32 checksum = 0; 2052 enum environment_cap env = ENVIRON_ANY; 2053 struct regulatory_request *request; 2054 2055 mutex_lock(®_mutex); 2056 2057 if (unlikely(!last_request)) 2058 goto out; 2059 2060 /* IE len must be evenly divisible by 2 */ 2061 if (country_ie_len & 0x01) 2062 goto out; 2063 2064 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) 2065 goto out; 2066 2067 /* 2068 * Pending country IE processing, this can happen after we 2069 * call CRDA and wait for a response if a beacon was received before 2070 * we were able to process the last regulatory_hint_11d() call 2071 */ 2072 if (country_ie_regdomain) 2073 goto out; 2074 2075 alpha2[0] = country_ie[0]; 2076 alpha2[1] = country_ie[1]; 2077 2078 if (country_ie[2] == 'I') 2079 env = ENVIRON_INDOOR; 2080 else if (country_ie[2] == 'O') 2081 env = ENVIRON_OUTDOOR; 2082 2083 /* 2084 * We will run this only upon a successful connection on cfg80211. 2085 * We leave conflict resolution to the workqueue, where can hold 2086 * cfg80211_mutex. 2087 */ 2088 if (likely(last_request->initiator == 2089 NL80211_REGDOM_SET_BY_COUNTRY_IE && 2090 wiphy_idx_valid(last_request->wiphy_idx))) 2091 goto out; 2092 2093 rd = country_ie_2_rd(band, country_ie, country_ie_len, &checksum); 2094 if (!rd) { 2095 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n"); 2096 goto out; 2097 } 2098 2099 /* 2100 * This will not happen right now but we leave it here for the 2101 * the future when we want to add suspend/resume support and having 2102 * the user move to another country after doing so, or having the user 2103 * move to another AP. Right now we just trust the first AP. 2104 * 2105 * If we hit this before we add this support we want to be informed of 2106 * it as it would indicate a mistake in the current design 2107 */ 2108 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum))) 2109 goto free_rd_out; 2110 2111 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); 2112 if (!request) 2113 goto free_rd_out; 2114 2115 /* 2116 * We keep this around for when CRDA comes back with a response so 2117 * we can intersect with that 2118 */ 2119 country_ie_regdomain = rd; 2120 2121 request->wiphy_idx = get_wiphy_idx(wiphy); 2122 request->alpha2[0] = rd->alpha2[0]; 2123 request->alpha2[1] = rd->alpha2[1]; 2124 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE; 2125 request->country_ie_checksum = checksum; 2126 request->country_ie_env = env; 2127 2128 mutex_unlock(®_mutex); 2129 2130 queue_regulatory_request(request); 2131 2132 return; 2133 2134 free_rd_out: 2135 kfree(rd); 2136 out: 2137 mutex_unlock(®_mutex); 2138 } 2139 2140 static void restore_alpha2(char *alpha2, bool reset_user) 2141 { 2142 /* indicates there is no alpha2 to consider for restoration */ 2143 alpha2[0] = '9'; 2144 alpha2[1] = '7'; 2145 2146 /* The user setting has precedence over the module parameter */ 2147 if (is_user_regdom_saved()) { 2148 /* Unless we're asked to ignore it and reset it */ 2149 if (reset_user) { 2150 REG_DBG_PRINT("cfg80211: Restoring regulatory settings " 2151 "including user preference\n"); 2152 user_alpha2[0] = '9'; 2153 user_alpha2[1] = '7'; 2154 2155 /* 2156 * If we're ignoring user settings, we still need to 2157 * check the module parameter to ensure we put things 2158 * back as they were for a full restore. 2159 */ 2160 if (!is_world_regdom(ieee80211_regdom)) { 2161 REG_DBG_PRINT("cfg80211: Keeping preference on " 2162 "module parameter ieee80211_regdom: %c%c\n", 2163 ieee80211_regdom[0], 2164 ieee80211_regdom[1]); 2165 alpha2[0] = ieee80211_regdom[0]; 2166 alpha2[1] = ieee80211_regdom[1]; 2167 } 2168 } else { 2169 REG_DBG_PRINT("cfg80211: Restoring regulatory settings " 2170 "while preserving user preference for: %c%c\n", 2171 user_alpha2[0], 2172 user_alpha2[1]); 2173 alpha2[0] = user_alpha2[0]; 2174 alpha2[1] = user_alpha2[1]; 2175 } 2176 } else if (!is_world_regdom(ieee80211_regdom)) { 2177 REG_DBG_PRINT("cfg80211: Keeping preference on " 2178 "module parameter ieee80211_regdom: %c%c\n", 2179 ieee80211_regdom[0], 2180 ieee80211_regdom[1]); 2181 alpha2[0] = ieee80211_regdom[0]; 2182 alpha2[1] = ieee80211_regdom[1]; 2183 } else 2184 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n"); 2185 } 2186 2187 /* 2188 * Restoring regulatory settings involves ingoring any 2189 * possibly stale country IE information and user regulatory 2190 * settings if so desired, this includes any beacon hints 2191 * learned as we could have traveled outside to another country 2192 * after disconnection. To restore regulatory settings we do 2193 * exactly what we did at bootup: 2194 * 2195 * - send a core regulatory hint 2196 * - send a user regulatory hint if applicable 2197 * 2198 * Device drivers that send a regulatory hint for a specific country 2199 * keep their own regulatory domain on wiphy->regd so that does does 2200 * not need to be remembered. 2201 */ 2202 static void restore_regulatory_settings(bool reset_user) 2203 { 2204 char alpha2[2]; 2205 struct reg_beacon *reg_beacon, *btmp; 2206 2207 mutex_lock(&cfg80211_mutex); 2208 mutex_lock(®_mutex); 2209 2210 reset_regdomains(); 2211 restore_alpha2(alpha2, reset_user); 2212 2213 /* Clear beacon hints */ 2214 spin_lock_bh(®_pending_beacons_lock); 2215 if (!list_empty(®_pending_beacons)) { 2216 list_for_each_entry_safe(reg_beacon, btmp, 2217 ®_pending_beacons, list) { 2218 list_del(®_beacon->list); 2219 kfree(reg_beacon); 2220 } 2221 } 2222 spin_unlock_bh(®_pending_beacons_lock); 2223 2224 if (!list_empty(®_beacon_list)) { 2225 list_for_each_entry_safe(reg_beacon, btmp, 2226 ®_beacon_list, list) { 2227 list_del(®_beacon->list); 2228 kfree(reg_beacon); 2229 } 2230 } 2231 2232 /* First restore to the basic regulatory settings */ 2233 cfg80211_regdomain = cfg80211_world_regdom; 2234 2235 mutex_unlock(®_mutex); 2236 mutex_unlock(&cfg80211_mutex); 2237 2238 regulatory_hint_core(cfg80211_regdomain->alpha2); 2239 2240 /* 2241 * This restores the ieee80211_regdom module parameter 2242 * preference or the last user requested regulatory 2243 * settings, user regulatory settings takes precedence. 2244 */ 2245 if (is_an_alpha2(alpha2)) 2246 regulatory_hint_user(user_alpha2); 2247 } 2248 2249 2250 void regulatory_hint_disconnect(void) 2251 { 2252 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to " 2253 "restore regulatory settings\n"); 2254 restore_regulatory_settings(false); 2255 } 2256 2257 static bool freq_is_chan_12_13_14(u16 freq) 2258 { 2259 if (freq == ieee80211_channel_to_frequency(12) || 2260 freq == ieee80211_channel_to_frequency(13) || 2261 freq == ieee80211_channel_to_frequency(14)) 2262 return true; 2263 return false; 2264 } 2265 2266 int regulatory_hint_found_beacon(struct wiphy *wiphy, 2267 struct ieee80211_channel *beacon_chan, 2268 gfp_t gfp) 2269 { 2270 struct reg_beacon *reg_beacon; 2271 2272 if (likely((beacon_chan->beacon_found || 2273 (beacon_chan->flags & IEEE80211_CHAN_RADAR) || 2274 (beacon_chan->band == IEEE80211_BAND_2GHZ && 2275 !freq_is_chan_12_13_14(beacon_chan->center_freq))))) 2276 return 0; 2277 2278 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp); 2279 if (!reg_beacon) 2280 return -ENOMEM; 2281 2282 REG_DBG_PRINT("cfg80211: Found new beacon on " 2283 "frequency: %d MHz (Ch %d) on %s\n", 2284 beacon_chan->center_freq, 2285 ieee80211_frequency_to_channel(beacon_chan->center_freq), 2286 wiphy_name(wiphy)); 2287 2288 memcpy(®_beacon->chan, beacon_chan, 2289 sizeof(struct ieee80211_channel)); 2290 2291 2292 /* 2293 * Since we can be called from BH or and non-BH context 2294 * we must use spin_lock_bh() 2295 */ 2296 spin_lock_bh(®_pending_beacons_lock); 2297 list_add_tail(®_beacon->list, ®_pending_beacons); 2298 spin_unlock_bh(®_pending_beacons_lock); 2299 2300 schedule_work(®_work); 2301 2302 return 0; 2303 } 2304 2305 static void print_rd_rules(const struct ieee80211_regdomain *rd) 2306 { 2307 unsigned int i; 2308 const struct ieee80211_reg_rule *reg_rule = NULL; 2309 const struct ieee80211_freq_range *freq_range = NULL; 2310 const struct ieee80211_power_rule *power_rule = NULL; 2311 2312 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), " 2313 "(max_antenna_gain, max_eirp)\n"); 2314 2315 for (i = 0; i < rd->n_reg_rules; i++) { 2316 reg_rule = &rd->reg_rules[i]; 2317 freq_range = ®_rule->freq_range; 2318 power_rule = ®_rule->power_rule; 2319 2320 /* 2321 * There may not be documentation for max antenna gain 2322 * in certain regions 2323 */ 2324 if (power_rule->max_antenna_gain) 2325 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), " 2326 "(%d mBi, %d mBm)\n", 2327 freq_range->start_freq_khz, 2328 freq_range->end_freq_khz, 2329 freq_range->max_bandwidth_khz, 2330 power_rule->max_antenna_gain, 2331 power_rule->max_eirp); 2332 else 2333 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), " 2334 "(N/A, %d mBm)\n", 2335 freq_range->start_freq_khz, 2336 freq_range->end_freq_khz, 2337 freq_range->max_bandwidth_khz, 2338 power_rule->max_eirp); 2339 } 2340 } 2341 2342 static void print_regdomain(const struct ieee80211_regdomain *rd) 2343 { 2344 2345 if (is_intersected_alpha2(rd->alpha2)) { 2346 2347 if (last_request->initiator == 2348 NL80211_REGDOM_SET_BY_COUNTRY_IE) { 2349 struct cfg80211_registered_device *rdev; 2350 rdev = cfg80211_rdev_by_wiphy_idx( 2351 last_request->wiphy_idx); 2352 if (rdev) { 2353 printk(KERN_INFO "cfg80211: Current regulatory " 2354 "domain updated by AP to: %c%c\n", 2355 rdev->country_ie_alpha2[0], 2356 rdev->country_ie_alpha2[1]); 2357 } else 2358 printk(KERN_INFO "cfg80211: Current regulatory " 2359 "domain intersected: \n"); 2360 } else 2361 printk(KERN_INFO "cfg80211: Current regulatory " 2362 "domain intersected: \n"); 2363 } else if (is_world_regdom(rd->alpha2)) 2364 printk(KERN_INFO "cfg80211: World regulatory " 2365 "domain updated:\n"); 2366 else { 2367 if (is_unknown_alpha2(rd->alpha2)) 2368 printk(KERN_INFO "cfg80211: Regulatory domain " 2369 "changed to driver built-in settings " 2370 "(unknown country)\n"); 2371 else 2372 printk(KERN_INFO "cfg80211: Regulatory domain " 2373 "changed to country: %c%c\n", 2374 rd->alpha2[0], rd->alpha2[1]); 2375 } 2376 print_rd_rules(rd); 2377 } 2378 2379 static void print_regdomain_info(const struct ieee80211_regdomain *rd) 2380 { 2381 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n", 2382 rd->alpha2[0], rd->alpha2[1]); 2383 print_rd_rules(rd); 2384 } 2385 2386 #ifdef CONFIG_CFG80211_REG_DEBUG 2387 static void reg_country_ie_process_debug( 2388 const struct ieee80211_regdomain *rd, 2389 const struct ieee80211_regdomain *country_ie_regdomain, 2390 const struct ieee80211_regdomain *intersected_rd) 2391 { 2392 printk(KERN_DEBUG "cfg80211: Received country IE:\n"); 2393 print_regdomain_info(country_ie_regdomain); 2394 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n"); 2395 print_regdomain_info(rd); 2396 if (intersected_rd) { 2397 printk(KERN_DEBUG "cfg80211: We intersect both of these " 2398 "and get:\n"); 2399 print_regdomain_info(intersected_rd); 2400 return; 2401 } 2402 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n"); 2403 } 2404 #else 2405 static inline void reg_country_ie_process_debug( 2406 const struct ieee80211_regdomain *rd, 2407 const struct ieee80211_regdomain *country_ie_regdomain, 2408 const struct ieee80211_regdomain *intersected_rd) 2409 { 2410 } 2411 #endif 2412 2413 /* Takes ownership of rd only if it doesn't fail */ 2414 static int __set_regdom(const struct ieee80211_regdomain *rd) 2415 { 2416 const struct ieee80211_regdomain *intersected_rd = NULL; 2417 struct cfg80211_registered_device *rdev = NULL; 2418 struct wiphy *request_wiphy; 2419 /* Some basic sanity checks first */ 2420 2421 if (is_world_regdom(rd->alpha2)) { 2422 if (WARN_ON(!reg_is_valid_request(rd->alpha2))) 2423 return -EINVAL; 2424 update_world_regdomain(rd); 2425 return 0; 2426 } 2427 2428 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && 2429 !is_unknown_alpha2(rd->alpha2)) 2430 return -EINVAL; 2431 2432 if (!last_request) 2433 return -EINVAL; 2434 2435 /* 2436 * Lets only bother proceeding on the same alpha2 if the current 2437 * rd is non static (it means CRDA was present and was used last) 2438 * and the pending request came in from a country IE 2439 */ 2440 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) { 2441 /* 2442 * If someone else asked us to change the rd lets only bother 2443 * checking if the alpha2 changes if CRDA was already called 2444 */ 2445 if (!regdom_changes(rd->alpha2)) 2446 return -EINVAL; 2447 } 2448 2449 /* 2450 * Now lets set the regulatory domain, update all driver channels 2451 * and finally inform them of what we have done, in case they want 2452 * to review or adjust their own settings based on their own 2453 * internal EEPROM data 2454 */ 2455 2456 if (WARN_ON(!reg_is_valid_request(rd->alpha2))) 2457 return -EINVAL; 2458 2459 if (!is_valid_rd(rd)) { 2460 printk(KERN_ERR "cfg80211: Invalid " 2461 "regulatory domain detected:\n"); 2462 print_regdomain_info(rd); 2463 return -EINVAL; 2464 } 2465 2466 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); 2467 2468 if (!last_request->intersect) { 2469 int r; 2470 2471 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) { 2472 reset_regdomains(); 2473 cfg80211_regdomain = rd; 2474 return 0; 2475 } 2476 2477 /* 2478 * For a driver hint, lets copy the regulatory domain the 2479 * driver wanted to the wiphy to deal with conflicts 2480 */ 2481 2482 /* 2483 * Userspace could have sent two replies with only 2484 * one kernel request. 2485 */ 2486 if (request_wiphy->regd) 2487 return -EALREADY; 2488 2489 r = reg_copy_regd(&request_wiphy->regd, rd); 2490 if (r) 2491 return r; 2492 2493 reset_regdomains(); 2494 cfg80211_regdomain = rd; 2495 return 0; 2496 } 2497 2498 /* Intersection requires a bit more work */ 2499 2500 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) { 2501 2502 intersected_rd = regdom_intersect(rd, cfg80211_regdomain); 2503 if (!intersected_rd) 2504 return -EINVAL; 2505 2506 /* 2507 * We can trash what CRDA provided now. 2508 * However if a driver requested this specific regulatory 2509 * domain we keep it for its private use 2510 */ 2511 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER) 2512 request_wiphy->regd = rd; 2513 else 2514 kfree(rd); 2515 2516 rd = NULL; 2517 2518 reset_regdomains(); 2519 cfg80211_regdomain = intersected_rd; 2520 2521 return 0; 2522 } 2523 2524 /* 2525 * Country IE requests are handled a bit differently, we intersect 2526 * the country IE rd with what CRDA believes that country should have 2527 */ 2528 2529 /* 2530 * Userspace could have sent two replies with only 2531 * one kernel request. By the second reply we would have 2532 * already processed and consumed the country_ie_regdomain. 2533 */ 2534 if (!country_ie_regdomain) 2535 return -EALREADY; 2536 BUG_ON(rd == country_ie_regdomain); 2537 2538 /* 2539 * Intersect what CRDA returned and our what we 2540 * had built from the Country IE received 2541 */ 2542 2543 intersected_rd = regdom_intersect(rd, country_ie_regdomain); 2544 2545 reg_country_ie_process_debug(rd, 2546 country_ie_regdomain, 2547 intersected_rd); 2548 2549 kfree(country_ie_regdomain); 2550 country_ie_regdomain = NULL; 2551 2552 if (!intersected_rd) 2553 return -EINVAL; 2554 2555 rdev = wiphy_to_dev(request_wiphy); 2556 2557 rdev->country_ie_alpha2[0] = rd->alpha2[0]; 2558 rdev->country_ie_alpha2[1] = rd->alpha2[1]; 2559 rdev->env = last_request->country_ie_env; 2560 2561 BUG_ON(intersected_rd == rd); 2562 2563 kfree(rd); 2564 rd = NULL; 2565 2566 reset_regdomains(); 2567 cfg80211_regdomain = intersected_rd; 2568 2569 return 0; 2570 } 2571 2572 2573 /* 2574 * Use this call to set the current regulatory domain. Conflicts with 2575 * multiple drivers can be ironed out later. Caller must've already 2576 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex 2577 */ 2578 int set_regdom(const struct ieee80211_regdomain *rd) 2579 { 2580 int r; 2581 2582 assert_cfg80211_lock(); 2583 2584 mutex_lock(®_mutex); 2585 2586 /* Note that this doesn't update the wiphys, this is done below */ 2587 r = __set_regdom(rd); 2588 if (r) { 2589 kfree(rd); 2590 mutex_unlock(®_mutex); 2591 return r; 2592 } 2593 2594 /* This would make this whole thing pointless */ 2595 if (!last_request->intersect) 2596 BUG_ON(rd != cfg80211_regdomain); 2597 2598 /* update all wiphys now with the new established regulatory domain */ 2599 update_all_wiphy_regulatory(last_request->initiator); 2600 2601 print_regdomain(cfg80211_regdomain); 2602 2603 nl80211_send_reg_change_event(last_request); 2604 2605 mutex_unlock(®_mutex); 2606 2607 return r; 2608 } 2609 2610 /* Caller must hold cfg80211_mutex */ 2611 void reg_device_remove(struct wiphy *wiphy) 2612 { 2613 struct wiphy *request_wiphy = NULL; 2614 2615 assert_cfg80211_lock(); 2616 2617 mutex_lock(®_mutex); 2618 2619 kfree(wiphy->regd); 2620 2621 if (last_request) 2622 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); 2623 2624 if (!request_wiphy || request_wiphy != wiphy) 2625 goto out; 2626 2627 last_request->wiphy_idx = WIPHY_IDX_STALE; 2628 last_request->country_ie_env = ENVIRON_ANY; 2629 out: 2630 mutex_unlock(®_mutex); 2631 } 2632 2633 int regulatory_init(void) 2634 { 2635 int err = 0; 2636 2637 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); 2638 if (IS_ERR(reg_pdev)) 2639 return PTR_ERR(reg_pdev); 2640 2641 spin_lock_init(®_requests_lock); 2642 spin_lock_init(®_pending_beacons_lock); 2643 2644 cfg80211_regdomain = cfg80211_world_regdom; 2645 2646 user_alpha2[0] = '9'; 2647 user_alpha2[1] = '7'; 2648 2649 /* We always try to get an update for the static regdomain */ 2650 err = regulatory_hint_core(cfg80211_regdomain->alpha2); 2651 if (err) { 2652 if (err == -ENOMEM) 2653 return err; 2654 /* 2655 * N.B. kobject_uevent_env() can fail mainly for when we're out 2656 * memory which is handled and propagated appropriately above 2657 * but it can also fail during a netlink_broadcast() or during 2658 * early boot for call_usermodehelper(). For now treat these 2659 * errors as non-fatal. 2660 */ 2661 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable " 2662 "to call CRDA during init"); 2663 #ifdef CONFIG_CFG80211_REG_DEBUG 2664 /* We want to find out exactly why when debugging */ 2665 WARN_ON(err); 2666 #endif 2667 } 2668 2669 /* 2670 * Finally, if the user set the module parameter treat it 2671 * as a user hint. 2672 */ 2673 if (!is_world_regdom(ieee80211_regdom)) 2674 regulatory_hint_user(ieee80211_regdom); 2675 2676 return 0; 2677 } 2678 2679 void regulatory_exit(void) 2680 { 2681 struct regulatory_request *reg_request, *tmp; 2682 struct reg_beacon *reg_beacon, *btmp; 2683 2684 cancel_work_sync(®_work); 2685 2686 mutex_lock(&cfg80211_mutex); 2687 mutex_lock(®_mutex); 2688 2689 reset_regdomains(); 2690 2691 kfree(country_ie_regdomain); 2692 country_ie_regdomain = NULL; 2693 2694 kfree(last_request); 2695 2696 platform_device_unregister(reg_pdev); 2697 2698 spin_lock_bh(®_pending_beacons_lock); 2699 if (!list_empty(®_pending_beacons)) { 2700 list_for_each_entry_safe(reg_beacon, btmp, 2701 ®_pending_beacons, list) { 2702 list_del(®_beacon->list); 2703 kfree(reg_beacon); 2704 } 2705 } 2706 spin_unlock_bh(®_pending_beacons_lock); 2707 2708 if (!list_empty(®_beacon_list)) { 2709 list_for_each_entry_safe(reg_beacon, btmp, 2710 ®_beacon_list, list) { 2711 list_del(®_beacon->list); 2712 kfree(reg_beacon); 2713 } 2714 } 2715 2716 spin_lock(®_requests_lock); 2717 if (!list_empty(®_requests_list)) { 2718 list_for_each_entry_safe(reg_request, tmp, 2719 ®_requests_list, list) { 2720 list_del(®_request->list); 2721 kfree(reg_request); 2722 } 2723 } 2724 spin_unlock(®_requests_lock); 2725 2726 mutex_unlock(®_mutex); 2727 mutex_unlock(&cfg80211_mutex); 2728 } 2729