reg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. #else
  162. static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  163. {
  164. return false;
  165. }
  166. #endif
  167. static void reset_regdomains(void)
  168. {
  169. /* avoid freeing static information or freeing something twice */
  170. if (cfg80211_regdomain == cfg80211_world_regdom)
  171. cfg80211_regdomain = NULL;
  172. if (cfg80211_world_regdom == &world_regdom)
  173. cfg80211_world_regdom = NULL;
  174. if (cfg80211_regdomain == &world_regdom)
  175. cfg80211_regdomain = NULL;
  176. if (is_old_static_regdom(cfg80211_regdomain))
  177. cfg80211_regdomain = NULL;
  178. kfree(cfg80211_regdomain);
  179. kfree(cfg80211_world_regdom);
  180. cfg80211_world_regdom = &world_regdom;
  181. cfg80211_regdomain = NULL;
  182. }
  183. /* Dynamic world regulatory domain requested by the wireless
  184. * core upon initialization */
  185. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  186. {
  187. BUG_ON(list_empty(&regulatory_requests));
  188. reset_regdomains();
  189. cfg80211_world_regdom = rd;
  190. cfg80211_regdomain = rd;
  191. }
  192. bool is_world_regdom(const char *alpha2)
  193. {
  194. if (!alpha2)
  195. return false;
  196. if (alpha2[0] == '0' && alpha2[1] == '0')
  197. return true;
  198. return false;
  199. }
  200. static bool is_alpha2_set(const char *alpha2)
  201. {
  202. if (!alpha2)
  203. return false;
  204. if (alpha2[0] != 0 && alpha2[1] != 0)
  205. return true;
  206. return false;
  207. }
  208. static bool is_alpha_upper(char letter)
  209. {
  210. /* ASCII A - Z */
  211. if (letter >= 65 && letter <= 90)
  212. return true;
  213. return false;
  214. }
  215. static bool is_unknown_alpha2(const char *alpha2)
  216. {
  217. if (!alpha2)
  218. return false;
  219. /* Special case where regulatory domain was built by driver
  220. * but a specific alpha2 cannot be determined */
  221. if (alpha2[0] == '9' && alpha2[1] == '9')
  222. return true;
  223. return false;
  224. }
  225. static bool is_an_alpha2(const char *alpha2)
  226. {
  227. if (!alpha2)
  228. return false;
  229. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  230. return true;
  231. return false;
  232. }
  233. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  234. {
  235. if (!alpha2_x || !alpha2_y)
  236. return false;
  237. if (alpha2_x[0] == alpha2_y[0] &&
  238. alpha2_x[1] == alpha2_y[1])
  239. return true;
  240. return false;
  241. }
  242. static bool regdom_changed(const char *alpha2)
  243. {
  244. if (!cfg80211_regdomain)
  245. return true;
  246. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  247. return false;
  248. return true;
  249. }
  250. /* This lets us keep regulatory code which is updated on a regulatory
  251. * basis in userspace. */
  252. static int call_crda(const char *alpha2)
  253. {
  254. char country_env[9 + 2] = "COUNTRY=";
  255. char *envp[] = {
  256. country_env,
  257. NULL
  258. };
  259. if (!is_world_regdom((char *) alpha2))
  260. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  261. alpha2[0], alpha2[1]);
  262. else
  263. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  264. "regulatory domain\n");
  265. country_env[8] = alpha2[0];
  266. country_env[9] = alpha2[1];
  267. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  268. }
  269. /* This has the logic which determines when a new request
  270. * should be ignored. */
  271. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  272. char *alpha2, struct ieee80211_regdomain *rd)
  273. {
  274. struct regulatory_request *last_request = NULL;
  275. /* All initial requests are respected */
  276. if (list_empty(&regulatory_requests))
  277. return 0;
  278. last_request = list_first_entry(&regulatory_requests,
  279. struct regulatory_request, list);
  280. switch (set_by) {
  281. case REGDOM_SET_BY_INIT:
  282. return -EINVAL;
  283. case REGDOM_SET_BY_CORE:
  284. /* Always respect new wireless core hints, should only
  285. * come in for updating the world regulatory domain at init
  286. * anyway */
  287. return 0;
  288. case REGDOM_SET_BY_COUNTRY_IE:
  289. if (last_request->initiator == set_by) {
  290. if (last_request->wiphy != wiphy) {
  291. /* Two cards with two APs claiming different
  292. * different Country IE alpha2s!
  293. * You're special!! */
  294. if (!alpha2_equal(last_request->alpha2,
  295. cfg80211_regdomain->alpha2)) {
  296. /* XXX: Deal with conflict, consider
  297. * building a new one out of the
  298. * intersection */
  299. WARN_ON(1);
  300. return -EOPNOTSUPP;
  301. }
  302. return -EALREADY;
  303. }
  304. /* Two consecutive Country IE hints on the same wiphy */
  305. if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  306. return 0;
  307. return -EALREADY;
  308. }
  309. if (WARN_ON(!is_alpha2_set(alpha2) || !is_an_alpha2(alpha2)),
  310. "Invalid Country IE regulatory hint passed "
  311. "to the wireless core\n")
  312. return -EINVAL;
  313. /* We ignore Country IE hints for now, as we haven't yet
  314. * added the dot11MultiDomainCapabilityEnabled flag
  315. * for wiphys */
  316. return 1;
  317. case REGDOM_SET_BY_DRIVER:
  318. BUG_ON(!wiphy);
  319. if (last_request->initiator == set_by) {
  320. /* Two separate drivers hinting different things,
  321. * this is possible if you have two devices present
  322. * on a system with different EEPROM regulatory
  323. * readings. XXX: Do intersection, we support only
  324. * the first regulatory hint for now */
  325. if (last_request->wiphy != wiphy)
  326. return -EALREADY;
  327. if (rd)
  328. return -EALREADY;
  329. /* Driver should not be trying to hint different
  330. * regulatory domains! */
  331. BUG_ON(!alpha2_equal(alpha2,
  332. cfg80211_regdomain->alpha2));
  333. return -EALREADY;
  334. }
  335. if (last_request->initiator == REGDOM_SET_BY_CORE)
  336. return 0;
  337. /* XXX: Handle intersection, and add the
  338. * dot11MultiDomainCapabilityEnabled flag to wiphy. For now
  339. * we assume the driver has this set to false, following the
  340. * 802.11d dot11MultiDomainCapabilityEnabled documentation */
  341. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  342. return 0;
  343. return 0;
  344. case REGDOM_SET_BY_USER:
  345. if (last_request->initiator == set_by ||
  346. last_request->initiator == REGDOM_SET_BY_CORE)
  347. return 0;
  348. /* Drivers can use their wiphy's reg_notifier()
  349. * to override any information */
  350. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  351. return 0;
  352. /* XXX: Handle intersection */
  353. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  354. return -EOPNOTSUPP;
  355. return 0;
  356. default:
  357. return -EINVAL;
  358. }
  359. }
  360. static bool __reg_is_valid_request(const char *alpha2,
  361. struct regulatory_request **request)
  362. {
  363. struct regulatory_request *req;
  364. if (list_empty(&regulatory_requests))
  365. return false;
  366. list_for_each_entry(req, &regulatory_requests, list) {
  367. if (alpha2_equal(req->alpha2, alpha2)) {
  368. *request = req;
  369. return true;
  370. }
  371. }
  372. return false;
  373. }
  374. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  375. bool reg_is_valid_request(const char *alpha2)
  376. {
  377. struct regulatory_request *request = NULL;
  378. return __reg_is_valid_request(alpha2, &request);
  379. }
  380. /* Sanity check on a regulatory rule */
  381. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  382. {
  383. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  384. u32 freq_diff;
  385. if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
  386. return false;
  387. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  388. return false;
  389. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  390. if (freq_range->max_bandwidth_khz > freq_diff)
  391. return false;
  392. return true;
  393. }
  394. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  395. {
  396. const struct ieee80211_reg_rule *reg_rule = NULL;
  397. unsigned int i;
  398. if (!rd->n_reg_rules)
  399. return false;
  400. for (i = 0; i < rd->n_reg_rules; i++) {
  401. reg_rule = &rd->reg_rules[i];
  402. if (!is_valid_reg_rule(reg_rule))
  403. return false;
  404. }
  405. return true;
  406. }
  407. /* Returns value in KHz */
  408. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  409. u32 freq)
  410. {
  411. unsigned int i;
  412. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  413. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  414. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  415. if (start_freq_khz >= freq_range->start_freq_khz &&
  416. end_freq_khz <= freq_range->end_freq_khz)
  417. return supported_bandwidths[i];
  418. }
  419. return 0;
  420. }
  421. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  422. * want to just have the channel structure use these */
  423. static u32 map_regdom_flags(u32 rd_flags)
  424. {
  425. u32 channel_flags = 0;
  426. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  427. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  428. if (rd_flags & NL80211_RRF_NO_IBSS)
  429. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  430. if (rd_flags & NL80211_RRF_DFS)
  431. channel_flags |= IEEE80211_CHAN_RADAR;
  432. return channel_flags;
  433. }
  434. /**
  435. * freq_reg_info - get regulatory information for the given frequency
  436. * @center_freq: Frequency in KHz for which we want regulatory information for
  437. * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
  438. * you can set this to 0. If this frequency is allowed we then set
  439. * this value to the maximum allowed bandwidth.
  440. * @reg_rule: the regulatory rule which we have for this frequency
  441. *
  442. * Use this function to get the regulatory rule for a specific frequency.
  443. */
  444. static int freq_reg_info(u32 center_freq, u32 *bandwidth,
  445. const struct ieee80211_reg_rule **reg_rule)
  446. {
  447. int i;
  448. u32 max_bandwidth = 0;
  449. if (!cfg80211_regdomain)
  450. return -EINVAL;
  451. for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
  452. const struct ieee80211_reg_rule *rr;
  453. const struct ieee80211_freq_range *fr = NULL;
  454. const struct ieee80211_power_rule *pr = NULL;
  455. rr = &cfg80211_regdomain->reg_rules[i];
  456. fr = &rr->freq_range;
  457. pr = &rr->power_rule;
  458. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  459. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  460. *reg_rule = rr;
  461. *bandwidth = max_bandwidth;
  462. break;
  463. }
  464. }
  465. return !max_bandwidth;
  466. }
  467. static void handle_channel(struct ieee80211_channel *chan)
  468. {
  469. int r;
  470. u32 flags = chan->orig_flags;
  471. u32 max_bandwidth = 0;
  472. const struct ieee80211_reg_rule *reg_rule = NULL;
  473. const struct ieee80211_power_rule *power_rule = NULL;
  474. r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
  475. &max_bandwidth, &reg_rule);
  476. if (r) {
  477. flags |= IEEE80211_CHAN_DISABLED;
  478. chan->flags = flags;
  479. return;
  480. }
  481. power_rule = &reg_rule->power_rule;
  482. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  483. chan->max_antenna_gain = min(chan->orig_mag,
  484. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  485. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  486. if (chan->orig_mpwr)
  487. chan->max_power = min(chan->orig_mpwr,
  488. (int) MBM_TO_DBM(power_rule->max_eirp));
  489. else
  490. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  491. }
  492. static void handle_band(struct ieee80211_supported_band *sband)
  493. {
  494. int i;
  495. for (i = 0; i < sband->n_channels; i++)
  496. handle_channel(&sband->channels[i]);
  497. }
  498. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  499. {
  500. struct cfg80211_registered_device *drv;
  501. list_for_each_entry(drv, &cfg80211_drv_list, list)
  502. wiphy_update_regulatory(&drv->wiphy, setby);
  503. }
  504. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  505. {
  506. enum ieee80211_band band;
  507. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  508. if (wiphy->bands[band])
  509. handle_band(wiphy->bands[band]);
  510. if (wiphy->reg_notifier)
  511. wiphy->reg_notifier(wiphy, setby);
  512. }
  513. }
  514. /* Caller must hold &cfg80211_drv_mutex */
  515. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  516. const char *alpha2, struct ieee80211_regdomain *rd)
  517. {
  518. struct regulatory_request *request;
  519. char *rd_alpha2;
  520. int r = 0;
  521. r = ignore_request(wiphy, set_by, (char *) alpha2, rd);
  522. if (r)
  523. return r;
  524. if (rd)
  525. rd_alpha2 = rd->alpha2;
  526. else
  527. rd_alpha2 = (char *) alpha2;
  528. switch (set_by) {
  529. case REGDOM_SET_BY_CORE:
  530. case REGDOM_SET_BY_COUNTRY_IE:
  531. case REGDOM_SET_BY_DRIVER:
  532. case REGDOM_SET_BY_USER:
  533. request = kzalloc(sizeof(struct regulatory_request),
  534. GFP_KERNEL);
  535. if (!request)
  536. return -ENOMEM;
  537. request->alpha2[0] = rd_alpha2[0];
  538. request->alpha2[1] = rd_alpha2[1];
  539. request->initiator = set_by;
  540. request->wiphy = wiphy;
  541. list_add_tail(&request->list, &regulatory_requests);
  542. if (rd)
  543. break;
  544. r = call_crda(alpha2);
  545. #ifndef CONFIG_WIRELESS_OLD_REGULATORY
  546. if (r)
  547. printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
  548. #endif
  549. break;
  550. default:
  551. r = -ENOTSUPP;
  552. break;
  553. }
  554. return r;
  555. }
  556. /* If rd is not NULL and if this call fails the caller must free it */
  557. int regulatory_hint(struct wiphy *wiphy, const char *alpha2,
  558. struct ieee80211_regdomain *rd)
  559. {
  560. int r;
  561. BUG_ON(!rd && !alpha2);
  562. mutex_lock(&cfg80211_drv_mutex);
  563. r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, rd);
  564. if (r || !rd)
  565. goto unlock_and_exit;
  566. /* If the driver passed a regulatory domain we skipped asking
  567. * userspace for one so we can now go ahead and set it */
  568. r = set_regdom(rd);
  569. unlock_and_exit:
  570. mutex_unlock(&cfg80211_drv_mutex);
  571. return r;
  572. }
  573. EXPORT_SYMBOL(regulatory_hint);
  574. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  575. {
  576. unsigned int i;
  577. const struct ieee80211_reg_rule *reg_rule = NULL;
  578. const struct ieee80211_freq_range *freq_range = NULL;
  579. const struct ieee80211_power_rule *power_rule = NULL;
  580. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  581. "(max_antenna_gain, max_eirp)\n");
  582. for (i = 0; i < rd->n_reg_rules; i++) {
  583. reg_rule = &rd->reg_rules[i];
  584. freq_range = &reg_rule->freq_range;
  585. power_rule = &reg_rule->power_rule;
  586. /* There may not be documentation for max antenna gain
  587. * in certain regions */
  588. if (power_rule->max_antenna_gain)
  589. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  590. "(%d mBi, %d mBm)\n",
  591. freq_range->start_freq_khz,
  592. freq_range->end_freq_khz,
  593. freq_range->max_bandwidth_khz,
  594. power_rule->max_antenna_gain,
  595. power_rule->max_eirp);
  596. else
  597. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  598. "(N/A, %d mBm)\n",
  599. freq_range->start_freq_khz,
  600. freq_range->end_freq_khz,
  601. freq_range->max_bandwidth_khz,
  602. power_rule->max_eirp);
  603. }
  604. }
  605. static void print_regdomain(const struct ieee80211_regdomain *rd)
  606. {
  607. if (is_world_regdom(rd->alpha2))
  608. printk(KERN_INFO "cfg80211: World regulatory "
  609. "domain updated:\n");
  610. else {
  611. if (is_unknown_alpha2(rd->alpha2))
  612. printk(KERN_INFO "cfg80211: Regulatory domain "
  613. "changed to driver built-in settings "
  614. "(unknown country)\n");
  615. else
  616. printk(KERN_INFO "cfg80211: Regulatory domain "
  617. "changed to country: %c%c\n",
  618. rd->alpha2[0], rd->alpha2[1]);
  619. }
  620. print_rd_rules(rd);
  621. }
  622. void print_regdomain_info(const struct ieee80211_regdomain *rd)
  623. {
  624. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  625. rd->alpha2[0], rd->alpha2[1]);
  626. print_rd_rules(rd);
  627. }
  628. static int __set_regdom(const struct ieee80211_regdomain *rd)
  629. {
  630. struct regulatory_request *request = NULL;
  631. /* Some basic sanity checks first */
  632. if (is_world_regdom(rd->alpha2)) {
  633. if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
  634. return -EINVAL;
  635. update_world_regdomain(rd);
  636. return 0;
  637. }
  638. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  639. !is_unknown_alpha2(rd->alpha2))
  640. return -EINVAL;
  641. if (list_empty(&regulatory_requests))
  642. return -EINVAL;
  643. /* allow overriding the static definitions if CRDA is present */
  644. if (!is_old_static_regdom(cfg80211_regdomain) &&
  645. !regdom_changed(rd->alpha2))
  646. return -EINVAL;
  647. /* Now lets set the regulatory domain, update all driver channels
  648. * and finally inform them of what we have done, in case they want
  649. * to review or adjust their own settings based on their own
  650. * internal EEPROM data */
  651. if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
  652. return -EINVAL;
  653. reset_regdomains();
  654. /* Country IE parsing coming soon */
  655. switch (request->initiator) {
  656. case REGDOM_SET_BY_CORE:
  657. case REGDOM_SET_BY_DRIVER:
  658. case REGDOM_SET_BY_USER:
  659. if (!is_valid_rd(rd)) {
  660. printk(KERN_ERR "cfg80211: Invalid "
  661. "regulatory domain detected:\n");
  662. print_regdomain_info(rd);
  663. return -EINVAL;
  664. }
  665. break;
  666. case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
  667. WARN_ON(1);
  668. default:
  669. return -EOPNOTSUPP;
  670. }
  671. /* Tada! */
  672. cfg80211_regdomain = rd;
  673. request->granted = 1;
  674. return 0;
  675. }
  676. /* Use this call to set the current regulatory domain. Conflicts with
  677. * multiple drivers can be ironed out later. Caller must've already
  678. * kmalloc'd the rd structure. If this calls fails you should kfree()
  679. * the passed rd. Caller must hold cfg80211_drv_mutex */
  680. int set_regdom(const struct ieee80211_regdomain *rd)
  681. {
  682. struct regulatory_request *this_request = NULL, *prev_request = NULL;
  683. int r;
  684. if (!list_empty(&regulatory_requests))
  685. prev_request = list_first_entry(&regulatory_requests,
  686. struct regulatory_request, list);
  687. /* Note that this doesn't update the wiphys, this is done below */
  688. r = __set_regdom(rd);
  689. if (r)
  690. return r;
  691. BUG_ON((!__reg_is_valid_request(rd->alpha2, &this_request)));
  692. /* The initial standard core update of the world regulatory domain, no
  693. * need to keep that request info around if it didn't fail. */
  694. if (is_world_regdom(rd->alpha2) &&
  695. this_request->initiator == REGDOM_SET_BY_CORE &&
  696. this_request->granted) {
  697. list_del(&this_request->list);
  698. kfree(this_request);
  699. this_request = NULL;
  700. }
  701. /* Remove old requests, we only leave behind the last one */
  702. if (prev_request) {
  703. list_del(&prev_request->list);
  704. kfree(prev_request);
  705. prev_request = NULL;
  706. }
  707. /* This would make this whole thing pointless */
  708. BUG_ON(rd != cfg80211_regdomain);
  709. /* update all wiphys now with the new established regulatory domain */
  710. update_all_wiphy_regulatory(this_request->initiator);
  711. print_regdomain(rd);
  712. return r;
  713. }
  714. int regulatory_init(void)
  715. {
  716. int err;
  717. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  718. if (IS_ERR(reg_pdev))
  719. return PTR_ERR(reg_pdev);
  720. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  721. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  722. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  723. print_regdomain_info(cfg80211_regdomain);
  724. /* The old code still requests for a new regdomain and if
  725. * you have CRDA you get it updated, otherwise you get
  726. * stuck with the static values. We ignore "EU" code as
  727. * that is not a valid ISO / IEC 3166 alpha2 */
  728. if (ieee80211_regdom[0] != 'E' && ieee80211_regdom[1] != 'U')
  729. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  730. ieee80211_regdom, NULL);
  731. #else
  732. cfg80211_regdomain = cfg80211_world_regdom;
  733. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
  734. if (err)
  735. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  736. "unable to update world regulatory domain, "
  737. "using static definition\n");
  738. #endif
  739. return 0;
  740. }
  741. void regulatory_exit(void)
  742. {
  743. struct regulatory_request *req, *req_tmp;
  744. mutex_lock(&cfg80211_drv_mutex);
  745. reset_regdomains();
  746. list_for_each_entry_safe(req, req_tmp, &regulatory_requests, list) {
  747. list_del(&req->list);
  748. kfree(req);
  749. }
  750. platform_device_unregister(reg_pdev);
  751. mutex_unlock(&cfg80211_drv_mutex);
  752. }