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