reg.c 25 KB

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