reg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. /**
  12. * DOC: Wireless regulatory infrastructure
  13. *
  14. * The usual implementation is for a driver to read a device EEPROM to
  15. * determine which regulatory domain it should be operating under, then
  16. * looking up the allowable channels in a driver-local table and finally
  17. * registering those channels in the wiphy structure.
  18. *
  19. * Another set of compliance enforcement is for drivers to use their
  20. * own compliance limits which can be stored on the EEPROM. The host
  21. * driver or firmware may ensure these are used.
  22. *
  23. * In addition to all this we provide an extra layer of regulatory
  24. * conformance. For drivers which do not have any regulatory
  25. * information CRDA provides the complete regulatory solution.
  26. * For others it provides a community effort on further restrictions
  27. * to enhance compliance.
  28. *
  29. * Note: When number of rules --> infinity we will not be able to
  30. * index on alpha2 any more, instead we'll probably have to
  31. * rely on some SHA1 checksum of the regdomain for example.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/list.h>
  36. #include <linux/random.h>
  37. #include <linux/nl80211.h>
  38. #include <linux/platform_device.h>
  39. #include <net/wireless.h>
  40. #include <net/cfg80211.h>
  41. #include "core.h"
  42. #include "reg.h"
  43. /* wiphy is set if this request's initiator is REGDOM_SET_BY_DRIVER */
  44. struct regulatory_request {
  45. struct list_head list;
  46. struct wiphy *wiphy;
  47. int granted;
  48. enum reg_set_by initiator;
  49. char alpha2[2];
  50. };
  51. static LIST_HEAD(regulatory_requests);
  52. DEFINE_MUTEX(cfg80211_reg_mutex);
  53. /* To trigger userspace events */
  54. static struct platform_device *reg_pdev;
  55. /* Keep the ordering from large to small */
  56. static u32 supported_bandwidths[] = {
  57. MHZ_TO_KHZ(40),
  58. MHZ_TO_KHZ(20),
  59. };
  60. static struct list_head regulatory_requests;
  61. /* Central wireless core regulatory domains, we only need two,
  62. * the current one and a world regulatory domain in case we have no
  63. * information to give us an alpha2 */
  64. static const struct ieee80211_regdomain *cfg80211_regdomain;
  65. /* We keep a static world regulatory domain in case of the absence of CRDA */
  66. static const struct ieee80211_regdomain world_regdom = {
  67. .n_reg_rules = 1,
  68. .alpha2 = "00",
  69. .reg_rules = {
  70. REG_RULE(2412-10, 2462+10, 40, 6, 20,
  71. NL80211_RRF_PASSIVE_SCAN |
  72. NL80211_RRF_NO_IBSS),
  73. }
  74. };
  75. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  76. &world_regdom;
  77. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  78. static char *ieee80211_regdom = "US";
  79. module_param(ieee80211_regdom, charp, 0444);
  80. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  81. /* We assume 40 MHz bandwidth for the old regulatory work.
  82. * We make emphasis we are using the exact same frequencies
  83. * as before */
  84. static const struct ieee80211_regdomain us_regdom = {
  85. .n_reg_rules = 6,
  86. .alpha2 = "US",
  87. .reg_rules = {
  88. /* IEEE 802.11b/g, channels 1..11 */
  89. REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
  90. /* IEEE 802.11a, channel 36 */
  91. REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
  92. /* IEEE 802.11a, channel 40 */
  93. REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
  94. /* IEEE 802.11a, channel 44 */
  95. REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
  96. /* IEEE 802.11a, channels 48..64 */
  97. REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
  98. /* IEEE 802.11a, channels 149..165, outdoor */
  99. REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
  100. }
  101. };
  102. static const struct ieee80211_regdomain jp_regdom = {
  103. .n_reg_rules = 3,
  104. .alpha2 = "JP",
  105. .reg_rules = {
  106. /* IEEE 802.11b/g, channels 1..14 */
  107. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  108. /* IEEE 802.11a, channels 34..48 */
  109. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  110. NL80211_RRF_PASSIVE_SCAN),
  111. /* IEEE 802.11a, channels 52..64 */
  112. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  113. NL80211_RRF_NO_IBSS |
  114. NL80211_RRF_DFS),
  115. }
  116. };
  117. static const struct ieee80211_regdomain eu_regdom = {
  118. .n_reg_rules = 6,
  119. /* This alpha2 is bogus, we leave it here just for stupid
  120. * backward compatibility */
  121. .alpha2 = "EU",
  122. .reg_rules = {
  123. /* IEEE 802.11b/g, channels 1..13 */
  124. REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
  125. /* IEEE 802.11a, channel 36 */
  126. REG_RULE(5180-10, 5180+10, 40, 6, 23,
  127. NL80211_RRF_PASSIVE_SCAN),
  128. /* IEEE 802.11a, channel 40 */
  129. REG_RULE(5200-10, 5200+10, 40, 6, 23,
  130. NL80211_RRF_PASSIVE_SCAN),
  131. /* IEEE 802.11a, channel 44 */
  132. REG_RULE(5220-10, 5220+10, 40, 6, 23,
  133. NL80211_RRF_PASSIVE_SCAN),
  134. /* IEEE 802.11a, channels 48..64 */
  135. REG_RULE(5240-10, 5320+10, 40, 6, 20,
  136. NL80211_RRF_NO_IBSS |
  137. NL80211_RRF_DFS),
  138. /* IEEE 802.11a, channels 100..140 */
  139. REG_RULE(5500-10, 5700+10, 40, 6, 30,
  140. NL80211_RRF_NO_IBSS |
  141. NL80211_RRF_DFS),
  142. }
  143. };
  144. static const struct ieee80211_regdomain *static_regdom(char *alpha2)
  145. {
  146. if (alpha2[0] == 'U' && alpha2[1] == 'S')
  147. return &us_regdom;
  148. if (alpha2[0] == 'J' && alpha2[1] == 'P')
  149. return &jp_regdom;
  150. if (alpha2[0] == 'E' && alpha2[1] == 'U')
  151. return &eu_regdom;
  152. /* Default, as per the old rules */
  153. return &us_regdom;
  154. }
  155. static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  156. {
  157. if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
  158. return true;
  159. return false;
  160. }
  161. /* The old crap never deals with a world regulatory domain, it only
  162. * deals with the static regulatory domain passed and if possible
  163. * an updated "US" or "JP" regulatory domain. We do however store the
  164. * old static regulatory domain in cfg80211_world_regdom for convenience
  165. * of use here */
  166. static void reset_regdomains_static(void)
  167. {
  168. if (!is_old_static_regdom(cfg80211_regdomain))
  169. kfree(cfg80211_regdomain);
  170. /* This is setting the regdom to the old static regdom */
  171. cfg80211_regdomain =
  172. (struct ieee80211_regdomain *) cfg80211_world_regdom;
  173. }
  174. #else
  175. static void reset_regdomains(void)
  176. {
  177. if (cfg80211_world_regdom && cfg80211_world_regdom != &world_regdom) {
  178. if (cfg80211_world_regdom == cfg80211_regdomain) {
  179. kfree(cfg80211_regdomain);
  180. } else {
  181. kfree(cfg80211_world_regdom);
  182. kfree(cfg80211_regdomain);
  183. }
  184. } else if (cfg80211_regdomain && cfg80211_regdomain != &world_regdom)
  185. kfree(cfg80211_regdomain);
  186. cfg80211_world_regdom = &world_regdom;
  187. cfg80211_regdomain = NULL;
  188. }
  189. /* Dynamic world regulatory domain requested by the wireless
  190. * core upon initialization */
  191. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  192. {
  193. BUG_ON(list_empty(&regulatory_requests));
  194. reset_regdomains();
  195. cfg80211_world_regdom = rd;
  196. cfg80211_regdomain = rd;
  197. }
  198. #endif
  199. bool is_world_regdom(const char *alpha2)
  200. {
  201. if (!alpha2)
  202. return false;
  203. if (alpha2[0] == '0' && alpha2[1] == '0')
  204. return true;
  205. return false;
  206. }
  207. static bool is_alpha2_set(const char *alpha2)
  208. {
  209. if (!alpha2)
  210. return false;
  211. if (alpha2[0] != 0 && alpha2[1] != 0)
  212. return true;
  213. return false;
  214. }
  215. static bool is_alpha_upper(char letter)
  216. {
  217. /* ASCII A - Z */
  218. if (letter >= 65 && letter <= 90)
  219. return true;
  220. return false;
  221. }
  222. static bool is_unknown_alpha2(const char *alpha2)
  223. {
  224. if (!alpha2)
  225. return false;
  226. /* Special case where regulatory domain was built by driver
  227. * but a specific alpha2 cannot be determined */
  228. if (alpha2[0] == '9' && alpha2[1] == '9')
  229. return true;
  230. return false;
  231. }
  232. static bool is_an_alpha2(const char *alpha2)
  233. {
  234. if (!alpha2)
  235. return false;
  236. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  237. return true;
  238. return false;
  239. }
  240. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  241. {
  242. if (!alpha2_x || !alpha2_y)
  243. return false;
  244. if (alpha2_x[0] == alpha2_y[0] &&
  245. alpha2_x[1] == alpha2_y[1])
  246. return true;
  247. return false;
  248. }
  249. static bool regdom_changed(const char *alpha2)
  250. {
  251. if (!cfg80211_regdomain)
  252. return true;
  253. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  254. return false;
  255. return true;
  256. }
  257. /* This lets us keep regulatory code which is updated on a regulatory
  258. * basis in userspace. */
  259. static int call_crda(const char *alpha2)
  260. {
  261. char country_env[9 + 2] = "COUNTRY=";
  262. char *envp[] = {
  263. country_env,
  264. NULL
  265. };
  266. if (!is_world_regdom((char *) alpha2))
  267. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  268. alpha2[0], alpha2[1]);
  269. else
  270. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  271. return -EINVAL;
  272. #else
  273. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  274. "regulatory domain\n");
  275. #endif
  276. country_env[8] = alpha2[0];
  277. country_env[9] = alpha2[1];
  278. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  279. }
  280. /* This has the logic which determines when a new request
  281. * should be ignored. */
  282. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  283. char *alpha2, struct ieee80211_regdomain *rd)
  284. {
  285. struct regulatory_request *last_request = NULL;
  286. /* All initial requests are respected */
  287. if (list_empty(&regulatory_requests))
  288. return 0;
  289. last_request = list_first_entry(&regulatory_requests,
  290. struct regulatory_request, list);
  291. switch (set_by) {
  292. case REGDOM_SET_BY_INIT:
  293. return -EINVAL;
  294. case REGDOM_SET_BY_CORE:
  295. /* Always respect new wireless core hints, should only
  296. * come in for updating the world regulatory domain at init
  297. * anyway */
  298. return 0;
  299. case REGDOM_SET_BY_COUNTRY_IE:
  300. if (last_request->initiator == set_by) {
  301. if (last_request->wiphy != wiphy) {
  302. /* Two cards with two APs claiming different
  303. * different Country IE alpha2s!
  304. * You're special!! */
  305. if (!alpha2_equal(last_request->alpha2,
  306. cfg80211_regdomain->alpha2)) {
  307. /* XXX: Deal with conflict, consider
  308. * building a new one out of the
  309. * intersection */
  310. WARN_ON(1);
  311. return -EOPNOTSUPP;
  312. }
  313. return -EALREADY;
  314. }
  315. /* Two consecutive Country IE hints on the same wiphy */
  316. if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  317. return 0;
  318. return -EALREADY;
  319. }
  320. if (WARN_ON(!is_alpha2_set(alpha2) || !is_an_alpha2(alpha2)),
  321. "Invalid Country IE regulatory hint passed "
  322. "to the wireless core\n")
  323. return -EINVAL;
  324. /* We ignore Country IE hints for now, as we haven't yet
  325. * added the dot11MultiDomainCapabilityEnabled flag
  326. * for wiphys */
  327. return 1;
  328. case REGDOM_SET_BY_DRIVER:
  329. BUG_ON(!wiphy);
  330. if (last_request->initiator == set_by) {
  331. /* Two separate drivers hinting different things,
  332. * this is possible if you have two devices present
  333. * on a system with different EEPROM regulatory
  334. * readings. XXX: Do intersection, we support only
  335. * the first regulatory hint for now */
  336. if (last_request->wiphy != wiphy)
  337. return -EALREADY;
  338. if (rd)
  339. return -EALREADY;
  340. /* Driver should not be trying to hint different
  341. * regulatory domains! */
  342. BUG_ON(!alpha2_equal(alpha2,
  343. cfg80211_regdomain->alpha2));
  344. return -EALREADY;
  345. }
  346. if (last_request->initiator == REGDOM_SET_BY_CORE)
  347. return 0;
  348. /* XXX: Handle intersection, and add the
  349. * dot11MultiDomainCapabilityEnabled flag to wiphy. For now
  350. * we assume the driver has this set to false, following the
  351. * 802.11d dot11MultiDomainCapabilityEnabled documentation */
  352. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  353. return 0;
  354. return 0;
  355. case REGDOM_SET_BY_USER:
  356. if (last_request->initiator == set_by ||
  357. last_request->initiator == REGDOM_SET_BY_CORE)
  358. return 0;
  359. /* Drivers can use their wiphy's reg_notifier()
  360. * to override any information */
  361. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  362. return 0;
  363. /* XXX: Handle intersection */
  364. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  365. return -EOPNOTSUPP;
  366. return 0;
  367. default:
  368. return -EINVAL;
  369. }
  370. }
  371. static bool __reg_is_valid_request(const char *alpha2,
  372. struct regulatory_request **request)
  373. {
  374. struct regulatory_request *req;
  375. if (list_empty(&regulatory_requests))
  376. return false;
  377. list_for_each_entry(req, &regulatory_requests, list) {
  378. if (alpha2_equal(req->alpha2, alpha2)) {
  379. *request = req;
  380. return true;
  381. }
  382. }
  383. return false;
  384. }
  385. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  386. bool reg_is_valid_request(const char *alpha2)
  387. {
  388. struct regulatory_request *request = NULL;
  389. return __reg_is_valid_request(alpha2, &request);
  390. }
  391. /* Sanity check on a regulatory rule */
  392. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  393. {
  394. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  395. u32 freq_diff;
  396. if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
  397. return false;
  398. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  399. return false;
  400. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  401. if (freq_range->max_bandwidth_khz > freq_diff)
  402. return false;
  403. return true;
  404. }
  405. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  406. {
  407. const struct ieee80211_reg_rule *reg_rule = NULL;
  408. unsigned int i;
  409. if (!rd->n_reg_rules)
  410. return false;
  411. for (i = 0; i < rd->n_reg_rules; i++) {
  412. reg_rule = &rd->reg_rules[i];
  413. if (!is_valid_reg_rule(reg_rule))
  414. return false;
  415. }
  416. return true;
  417. }
  418. /* Returns value in KHz */
  419. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  420. u32 freq)
  421. {
  422. unsigned int i;
  423. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  424. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  425. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  426. if (start_freq_khz >= freq_range->start_freq_khz &&
  427. end_freq_khz <= freq_range->end_freq_khz)
  428. return supported_bandwidths[i];
  429. }
  430. return 0;
  431. }
  432. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  433. * want to just have the channel structure use these */
  434. static u32 map_regdom_flags(u32 rd_flags)
  435. {
  436. u32 channel_flags = 0;
  437. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  438. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  439. if (rd_flags & NL80211_RRF_NO_IBSS)
  440. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  441. if (rd_flags & NL80211_RRF_DFS)
  442. channel_flags |= IEEE80211_CHAN_RADAR;
  443. return channel_flags;
  444. }
  445. /**
  446. * freq_reg_info - get regulatory information for the given frequency
  447. * @center_freq: Frequency in KHz for which we want regulatory information for
  448. * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
  449. * you can set this to 0. If this frequency is allowed we then set
  450. * this value to the maximum allowed bandwidth.
  451. * @reg_rule: the regulatory rule which we have for this frequency
  452. *
  453. * Use this function to get the regulatory rule for a specific frequency.
  454. */
  455. static int freq_reg_info(u32 center_freq, u32 *bandwidth,
  456. const struct ieee80211_reg_rule **reg_rule)
  457. {
  458. int i;
  459. u32 max_bandwidth = 0;
  460. if (!cfg80211_regdomain)
  461. return -EINVAL;
  462. for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
  463. const struct ieee80211_reg_rule *rr;
  464. const struct ieee80211_freq_range *fr = NULL;
  465. const struct ieee80211_power_rule *pr = NULL;
  466. rr = &cfg80211_regdomain->reg_rules[i];
  467. fr = &rr->freq_range;
  468. pr = &rr->power_rule;
  469. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  470. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  471. *reg_rule = rr;
  472. *bandwidth = max_bandwidth;
  473. break;
  474. }
  475. }
  476. return !max_bandwidth;
  477. }
  478. static void handle_channel(struct ieee80211_channel *chan)
  479. {
  480. int r;
  481. u32 flags = chan->orig_flags;
  482. u32 max_bandwidth = 0;
  483. const struct ieee80211_reg_rule *reg_rule = NULL;
  484. const struct ieee80211_power_rule *power_rule = NULL;
  485. r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
  486. &max_bandwidth, &reg_rule);
  487. if (r) {
  488. flags |= IEEE80211_CHAN_DISABLED;
  489. chan->flags = flags;
  490. return;
  491. }
  492. power_rule = &reg_rule->power_rule;
  493. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  494. chan->max_antenna_gain = min(chan->orig_mag,
  495. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  496. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  497. if (chan->orig_mpwr)
  498. chan->max_power = min(chan->orig_mpwr,
  499. (int) MBM_TO_DBM(power_rule->max_eirp));
  500. else
  501. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  502. }
  503. static void handle_band(struct ieee80211_supported_band *sband)
  504. {
  505. int i;
  506. for (i = 0; i < sband->n_channels; i++)
  507. handle_channel(&sband->channels[i]);
  508. }
  509. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  510. {
  511. struct cfg80211_registered_device *drv;
  512. list_for_each_entry(drv, &cfg80211_drv_list, list)
  513. wiphy_update_regulatory(&drv->wiphy, setby);
  514. }
  515. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  516. {
  517. enum ieee80211_band band;
  518. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  519. if (wiphy->bands[band])
  520. handle_band(wiphy->bands[band]);
  521. if (wiphy->reg_notifier)
  522. wiphy->reg_notifier(wiphy, setby);
  523. }
  524. }
  525. /* Caller must hold &cfg80211_drv_mutex */
  526. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  527. const char *alpha2, struct ieee80211_regdomain *rd)
  528. {
  529. struct regulatory_request *request;
  530. char *rd_alpha2;
  531. int r = 0;
  532. r = ignore_request(wiphy, set_by, (char *) alpha2, rd);
  533. if (r)
  534. return r;
  535. if (rd)
  536. rd_alpha2 = rd->alpha2;
  537. else
  538. rd_alpha2 = (char *) alpha2;
  539. switch (set_by) {
  540. case REGDOM_SET_BY_CORE:
  541. case REGDOM_SET_BY_COUNTRY_IE:
  542. case REGDOM_SET_BY_DRIVER:
  543. case REGDOM_SET_BY_USER:
  544. request = kzalloc(sizeof(struct regulatory_request),
  545. GFP_KERNEL);
  546. if (!request)
  547. return -ENOMEM;
  548. request->alpha2[0] = rd_alpha2[0];
  549. request->alpha2[1] = rd_alpha2[1];
  550. request->initiator = set_by;
  551. request->wiphy = wiphy;
  552. list_add_tail(&request->list, &regulatory_requests);
  553. if (rd)
  554. break;
  555. r = call_crda(alpha2);
  556. #ifndef CONFIG_WIRELESS_OLD_REGULATORY
  557. if (r)
  558. printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
  559. #endif
  560. break;
  561. default:
  562. r = -ENOTSUPP;
  563. break;
  564. }
  565. return r;
  566. }
  567. /* If rd is not NULL and if this call fails the caller must free it */
  568. int regulatory_hint(struct wiphy *wiphy, const char *alpha2,
  569. struct ieee80211_regdomain *rd)
  570. {
  571. int r;
  572. BUG_ON(!rd && !alpha2);
  573. mutex_lock(&cfg80211_drv_mutex);
  574. r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, rd);
  575. if (r || !rd)
  576. goto unlock_and_exit;
  577. /* If the driver passed a regulatory domain we skipped asking
  578. * userspace for one so we can now go ahead and set it */
  579. r = set_regdom(rd);
  580. unlock_and_exit:
  581. mutex_unlock(&cfg80211_drv_mutex);
  582. return r;
  583. }
  584. EXPORT_SYMBOL(regulatory_hint);
  585. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  586. {
  587. unsigned int i;
  588. const struct ieee80211_reg_rule *reg_rule = NULL;
  589. const struct ieee80211_freq_range *freq_range = NULL;
  590. const struct ieee80211_power_rule *power_rule = NULL;
  591. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  592. "(max_antenna_gain, max_eirp)\n");
  593. for (i = 0; i < rd->n_reg_rules; i++) {
  594. reg_rule = &rd->reg_rules[i];
  595. freq_range = &reg_rule->freq_range;
  596. power_rule = &reg_rule->power_rule;
  597. /* There may not be documentation for max antenna gain
  598. * in certain regions */
  599. if (power_rule->max_antenna_gain)
  600. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  601. "(%d mBi, %d mBm)\n",
  602. freq_range->start_freq_khz,
  603. freq_range->end_freq_khz,
  604. freq_range->max_bandwidth_khz,
  605. power_rule->max_antenna_gain,
  606. power_rule->max_eirp);
  607. else
  608. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  609. "(N/A, %d mBm)\n",
  610. freq_range->start_freq_khz,
  611. freq_range->end_freq_khz,
  612. freq_range->max_bandwidth_khz,
  613. power_rule->max_eirp);
  614. }
  615. }
  616. static void print_regdomain(const struct ieee80211_regdomain *rd)
  617. {
  618. if (is_world_regdom(rd->alpha2))
  619. printk(KERN_INFO "cfg80211: World regulatory "
  620. "domain updated:\n");
  621. else {
  622. if (is_unknown_alpha2(rd->alpha2))
  623. printk(KERN_INFO "cfg80211: Regulatory domain "
  624. "changed to driver built-in settings "
  625. "(unknown country)\n");
  626. else
  627. printk(KERN_INFO "cfg80211: Regulatory domain "
  628. "changed to country: %c%c\n",
  629. rd->alpha2[0], rd->alpha2[1]);
  630. }
  631. print_rd_rules(rd);
  632. }
  633. void print_regdomain_info(const struct ieee80211_regdomain *rd)
  634. {
  635. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  636. rd->alpha2[0], rd->alpha2[1]);
  637. print_rd_rules(rd);
  638. }
  639. static int __set_regdom(const struct ieee80211_regdomain *rd)
  640. {
  641. struct regulatory_request *request = NULL;
  642. /* Some basic sanity checks first */
  643. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  644. /* We ignore the world regdom with the old static regdomains setup
  645. * as there is no point to it with static regulatory definitions :(
  646. * Don't worry this shit will be removed soon... */
  647. if (is_world_regdom(rd->alpha2))
  648. return -EINVAL;
  649. #else
  650. if (is_world_regdom(rd->alpha2)) {
  651. if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
  652. return -EINVAL;
  653. update_world_regdomain(rd);
  654. return 0;
  655. }
  656. #endif
  657. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  658. !is_unknown_alpha2(rd->alpha2))
  659. return -EINVAL;
  660. if (list_empty(&regulatory_requests))
  661. return -EINVAL;
  662. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  663. /* Static "US" and "JP" will be overridden, but just once */
  664. if (!is_old_static_regdom(cfg80211_regdomain) &&
  665. !regdom_changed(rd->alpha2))
  666. return -EINVAL;
  667. #else
  668. if (!regdom_changed(rd->alpha2))
  669. return -EINVAL;
  670. #endif
  671. /* Now lets set the regulatory domain, update all driver channels
  672. * and finally inform them of what we have done, in case they want
  673. * to review or adjust their own settings based on their own
  674. * internal EEPROM data */
  675. if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
  676. return -EINVAL;
  677. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  678. reset_regdomains_static();
  679. #else
  680. reset_regdomains();
  681. #endif
  682. /* Country IE parsing coming soon */
  683. switch (request->initiator) {
  684. case REGDOM_SET_BY_CORE:
  685. case REGDOM_SET_BY_DRIVER:
  686. case REGDOM_SET_BY_USER:
  687. if (!is_valid_rd(rd)) {
  688. printk(KERN_ERR "cfg80211: Invalid "
  689. "regulatory domain detected:\n");
  690. print_regdomain_info(rd);
  691. return -EINVAL;
  692. }
  693. break;
  694. case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
  695. WARN_ON(1);
  696. default:
  697. return -EOPNOTSUPP;
  698. }
  699. /* Tada! */
  700. cfg80211_regdomain = rd;
  701. request->granted = 1;
  702. return 0;
  703. }
  704. /* Use this call to set the current regulatory domain. Conflicts with
  705. * multiple drivers can be ironed out later. Caller must've already
  706. * kmalloc'd the rd structure. If this calls fails you should kfree()
  707. * the passed rd. Caller must hold cfg80211_drv_mutex */
  708. int set_regdom(const struct ieee80211_regdomain *rd)
  709. {
  710. struct regulatory_request *this_request = NULL, *prev_request = NULL;
  711. int r;
  712. if (!list_empty(&regulatory_requests))
  713. prev_request = list_first_entry(&regulatory_requests,
  714. struct regulatory_request, list);
  715. /* Note that this doesn't update the wiphys, this is done below */
  716. r = __set_regdom(rd);
  717. if (r)
  718. return r;
  719. BUG_ON((!__reg_is_valid_request(rd->alpha2, &this_request)));
  720. /* The initial standard core update of the world regulatory domain, no
  721. * need to keep that request info around if it didn't fail. */
  722. if (is_world_regdom(rd->alpha2) &&
  723. this_request->initiator == REGDOM_SET_BY_CORE &&
  724. this_request->granted) {
  725. list_del(&this_request->list);
  726. kfree(this_request);
  727. this_request = NULL;
  728. }
  729. /* Remove old requests, we only leave behind the last one */
  730. if (prev_request) {
  731. list_del(&prev_request->list);
  732. kfree(prev_request);
  733. prev_request = NULL;
  734. }
  735. /* This would make this whole thing pointless */
  736. BUG_ON(rd != cfg80211_regdomain);
  737. /* update all wiphys now with the new established regulatory domain */
  738. update_all_wiphy_regulatory(this_request->initiator);
  739. print_regdomain(rd);
  740. return r;
  741. }
  742. int regulatory_init(void)
  743. {
  744. int err;
  745. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  746. if (IS_ERR(reg_pdev))
  747. return PTR_ERR(reg_pdev);
  748. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  749. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  750. /* Used during reset_regdomains_static() */
  751. cfg80211_world_regdom = cfg80211_regdomain;
  752. printk(KERN_INFO "cfg80211: Using old static regulatory domain:\n");
  753. print_regdomain_info(cfg80211_regdomain);
  754. /* The old code still requests for a new regdomain and if
  755. * you have CRDA you get it updated, otherwise you get
  756. * stuck with the static values. We ignore "EU" code as
  757. * that is not a valid ISO / IEC 3166 alpha2 */
  758. if (ieee80211_regdom[0] != 'E' && ieee80211_regdom[1] != 'U')
  759. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  760. ieee80211_regdom, NULL);
  761. #else
  762. cfg80211_regdomain = cfg80211_world_regdom;
  763. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
  764. if (err)
  765. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  766. "unable to update world regulatory domain, "
  767. "using static definition\n");
  768. #endif
  769. return 0;
  770. }
  771. void regulatory_exit(void)
  772. {
  773. struct regulatory_request *req, *req_tmp;
  774. mutex_lock(&cfg80211_drv_mutex);
  775. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  776. reset_regdomains_static();
  777. #else
  778. reset_regdomains();
  779. #endif
  780. list_for_each_entry_safe(req, req_tmp, &regulatory_requests, list) {
  781. list_del(&req->list);
  782. kfree(req);
  783. }
  784. platform_device_unregister(reg_pdev);
  785. mutex_unlock(&cfg80211_drv_mutex);
  786. }