reg.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. for (i = 0; i < rd->n_reg_rules; i++) {
  313. reg_rule = &rd->reg_rules[i];
  314. if (!is_valid_reg_rule(reg_rule))
  315. return false;
  316. }
  317. return true;
  318. }
  319. /* Returns value in KHz */
  320. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  321. u32 freq)
  322. {
  323. unsigned int i;
  324. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  325. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  326. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  327. if (start_freq_khz >= freq_range->start_freq_khz &&
  328. end_freq_khz <= freq_range->end_freq_khz)
  329. return supported_bandwidths[i];
  330. }
  331. return 0;
  332. }
  333. /* Helper for regdom_intersect(), this does the real
  334. * mathematical intersection fun */
  335. static int reg_rules_intersect(
  336. const struct ieee80211_reg_rule *rule1,
  337. const struct ieee80211_reg_rule *rule2,
  338. struct ieee80211_reg_rule *intersected_rule)
  339. {
  340. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  341. struct ieee80211_freq_range *freq_range;
  342. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  343. struct ieee80211_power_rule *power_rule;
  344. u32 freq_diff;
  345. freq_range1 = &rule1->freq_range;
  346. freq_range2 = &rule2->freq_range;
  347. freq_range = &intersected_rule->freq_range;
  348. power_rule1 = &rule1->power_rule;
  349. power_rule2 = &rule2->power_rule;
  350. power_rule = &intersected_rule->power_rule;
  351. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  352. freq_range2->start_freq_khz);
  353. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  354. freq_range2->end_freq_khz);
  355. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  356. freq_range2->max_bandwidth_khz);
  357. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  358. if (freq_range->max_bandwidth_khz > freq_diff)
  359. freq_range->max_bandwidth_khz = freq_diff;
  360. power_rule->max_eirp = min(power_rule1->max_eirp,
  361. power_rule2->max_eirp);
  362. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  363. power_rule2->max_antenna_gain);
  364. intersected_rule->flags = (rule1->flags | rule2->flags);
  365. if (!is_valid_reg_rule(intersected_rule))
  366. return -EINVAL;
  367. return 0;
  368. }
  369. /**
  370. * regdom_intersect - do the intersection between two regulatory domains
  371. * @rd1: first regulatory domain
  372. * @rd2: second regulatory domain
  373. *
  374. * Use this function to get the intersection between two regulatory domains.
  375. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  376. * as no one single alpha2 can represent this regulatory domain.
  377. *
  378. * Returns a pointer to the regulatory domain structure which will hold the
  379. * resulting intersection of rules between rd1 and rd2. We will
  380. * kzalloc() this structure for you.
  381. */
  382. static struct ieee80211_regdomain *regdom_intersect(
  383. const struct ieee80211_regdomain *rd1,
  384. const struct ieee80211_regdomain *rd2)
  385. {
  386. int r, size_of_regd;
  387. unsigned int x, y;
  388. unsigned int num_rules = 0, rule_idx = 0;
  389. const struct ieee80211_reg_rule *rule1, *rule2;
  390. struct ieee80211_reg_rule *intersected_rule;
  391. struct ieee80211_regdomain *rd;
  392. /* This is just a dummy holder to help us count */
  393. struct ieee80211_reg_rule irule;
  394. /* Uses the stack temporarily for counter arithmetic */
  395. intersected_rule = &irule;
  396. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  397. if (!rd1 || !rd2)
  398. return NULL;
  399. /* First we get a count of the rules we'll need, then we actually
  400. * build them. This is to so we can malloc() and free() a
  401. * regdomain once. The reason we use reg_rules_intersect() here
  402. * is it will return -EINVAL if the rule computed makes no sense.
  403. * All rules that do check out OK are valid. */
  404. for (x = 0; x < rd1->n_reg_rules; x++) {
  405. rule1 = &rd1->reg_rules[x];
  406. for (y = 0; y < rd2->n_reg_rules; y++) {
  407. rule2 = &rd2->reg_rules[y];
  408. if (!reg_rules_intersect(rule1, rule2,
  409. intersected_rule))
  410. num_rules++;
  411. memset(intersected_rule, 0,
  412. sizeof(struct ieee80211_reg_rule));
  413. }
  414. }
  415. if (!num_rules)
  416. return NULL;
  417. size_of_regd = sizeof(struct ieee80211_regdomain) +
  418. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  419. rd = kzalloc(size_of_regd, GFP_KERNEL);
  420. if (!rd)
  421. return NULL;
  422. for (x = 0; x < rd1->n_reg_rules; x++) {
  423. rule1 = &rd1->reg_rules[x];
  424. for (y = 0; y < rd2->n_reg_rules; y++) {
  425. rule2 = &rd2->reg_rules[y];
  426. /* This time around instead of using the stack lets
  427. * write to the target rule directly saving ourselves
  428. * a memcpy() */
  429. intersected_rule = &rd->reg_rules[rule_idx];
  430. r = reg_rules_intersect(rule1, rule2,
  431. intersected_rule);
  432. /* No need to memset here the intersected rule here as
  433. * we're not using the stack anymore */
  434. if (r)
  435. continue;
  436. rule_idx++;
  437. }
  438. }
  439. if (rule_idx != num_rules) {
  440. kfree(rd);
  441. return NULL;
  442. }
  443. rd->n_reg_rules = num_rules;
  444. rd->alpha2[0] = '9';
  445. rd->alpha2[1] = '8';
  446. return rd;
  447. }
  448. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  449. * want to just have the channel structure use these */
  450. static u32 map_regdom_flags(u32 rd_flags)
  451. {
  452. u32 channel_flags = 0;
  453. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  454. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  455. if (rd_flags & NL80211_RRF_NO_IBSS)
  456. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  457. if (rd_flags & NL80211_RRF_DFS)
  458. channel_flags |= IEEE80211_CHAN_RADAR;
  459. return channel_flags;
  460. }
  461. /**
  462. * freq_reg_info - get regulatory information for the given frequency
  463. * @center_freq: Frequency in KHz for which we want regulatory information for
  464. * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
  465. * you can set this to 0. If this frequency is allowed we then set
  466. * this value to the maximum allowed bandwidth.
  467. * @reg_rule: the regulatory rule which we have for this frequency
  468. *
  469. * Use this function to get the regulatory rule for a specific frequency.
  470. */
  471. static int freq_reg_info(u32 center_freq, u32 *bandwidth,
  472. const struct ieee80211_reg_rule **reg_rule)
  473. {
  474. int i;
  475. u32 max_bandwidth = 0;
  476. if (!cfg80211_regdomain)
  477. return -EINVAL;
  478. for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
  479. const struct ieee80211_reg_rule *rr;
  480. const struct ieee80211_freq_range *fr = NULL;
  481. const struct ieee80211_power_rule *pr = NULL;
  482. rr = &cfg80211_regdomain->reg_rules[i];
  483. fr = &rr->freq_range;
  484. pr = &rr->power_rule;
  485. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  486. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  487. *reg_rule = rr;
  488. *bandwidth = max_bandwidth;
  489. break;
  490. }
  491. }
  492. return !max_bandwidth;
  493. }
  494. static void handle_channel(struct ieee80211_channel *chan)
  495. {
  496. int r;
  497. u32 flags = chan->orig_flags;
  498. u32 max_bandwidth = 0;
  499. const struct ieee80211_reg_rule *reg_rule = NULL;
  500. const struct ieee80211_power_rule *power_rule = NULL;
  501. r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
  502. &max_bandwidth, &reg_rule);
  503. if (r) {
  504. flags |= IEEE80211_CHAN_DISABLED;
  505. chan->flags = flags;
  506. return;
  507. }
  508. power_rule = &reg_rule->power_rule;
  509. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  510. chan->max_antenna_gain = min(chan->orig_mag,
  511. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  512. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  513. if (chan->orig_mpwr)
  514. chan->max_power = min(chan->orig_mpwr,
  515. (int) MBM_TO_DBM(power_rule->max_eirp));
  516. else
  517. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  518. }
  519. static void handle_band(struct ieee80211_supported_band *sband)
  520. {
  521. int i;
  522. for (i = 0; i < sband->n_channels; i++)
  523. handle_channel(&sband->channels[i]);
  524. }
  525. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  526. {
  527. struct cfg80211_registered_device *drv;
  528. list_for_each_entry(drv, &cfg80211_drv_list, list)
  529. wiphy_update_regulatory(&drv->wiphy, setby);
  530. }
  531. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  532. {
  533. enum ieee80211_band band;
  534. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  535. if (wiphy->bands[band])
  536. handle_band(wiphy->bands[band]);
  537. if (wiphy->reg_notifier)
  538. wiphy->reg_notifier(wiphy, setby);
  539. }
  540. }
  541. /* Return value which can be used by ignore_request() to indicate
  542. * it has been determined we should intersect two regulatory domains */
  543. #define REG_INTERSECT 1
  544. /* This has the logic which determines when a new request
  545. * should be ignored. */
  546. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  547. const char *alpha2)
  548. {
  549. /* All initial requests are respected */
  550. if (!last_request)
  551. return 0;
  552. switch (set_by) {
  553. case REGDOM_SET_BY_INIT:
  554. return -EINVAL;
  555. case REGDOM_SET_BY_CORE:
  556. /*
  557. * Always respect new wireless core hints, should only happen
  558. * when updating the world regulatory domain at init.
  559. */
  560. return 0;
  561. case REGDOM_SET_BY_COUNTRY_IE:
  562. if (unlikely(!is_an_alpha2(alpha2)))
  563. return -EINVAL;
  564. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  565. if (last_request->wiphy != wiphy) {
  566. /*
  567. * Two cards with two APs claiming different
  568. * different Country IE alpha2s. We could
  569. * intersect them, but that seems unlikely
  570. * to be correct. Reject second one for now.
  571. */
  572. if (!alpha2_equal(alpha2,
  573. cfg80211_regdomain->alpha2))
  574. return -EOPNOTSUPP;
  575. return -EALREADY;
  576. }
  577. /* Two consecutive Country IE hints on the same wiphy */
  578. if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  579. return 0;
  580. return -EALREADY;
  581. }
  582. /*
  583. * Ignore Country IE hints for now, need to think about
  584. * what we need to do to support multi-domain operation.
  585. */
  586. return -EOPNOTSUPP;
  587. case REGDOM_SET_BY_DRIVER:
  588. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  589. return -EALREADY;
  590. return 0;
  591. case REGDOM_SET_BY_USER:
  592. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  593. return REG_INTERSECT;
  594. return 0;
  595. }
  596. return -EINVAL;
  597. }
  598. /* Caller must hold &cfg80211_drv_mutex */
  599. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  600. const char *alpha2)
  601. {
  602. struct regulatory_request *request;
  603. bool intersect = false;
  604. int r = 0;
  605. r = ignore_request(wiphy, set_by, alpha2);
  606. if (r == REG_INTERSECT)
  607. intersect = true;
  608. else if (r)
  609. return r;
  610. request = kzalloc(sizeof(struct regulatory_request),
  611. GFP_KERNEL);
  612. if (!request)
  613. return -ENOMEM;
  614. request->alpha2[0] = alpha2[0];
  615. request->alpha2[1] = alpha2[1];
  616. request->initiator = set_by;
  617. request->wiphy = wiphy;
  618. request->intersect = intersect;
  619. kfree(last_request);
  620. last_request = request;
  621. r = call_crda(alpha2);
  622. #ifndef CONFIG_WIRELESS_OLD_REGULATORY
  623. if (r)
  624. printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
  625. #endif
  626. return r;
  627. }
  628. void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  629. {
  630. BUG_ON(!alpha2);
  631. mutex_lock(&cfg80211_drv_mutex);
  632. __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2);
  633. mutex_unlock(&cfg80211_drv_mutex);
  634. }
  635. EXPORT_SYMBOL(regulatory_hint);
  636. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  637. {
  638. unsigned int i;
  639. const struct ieee80211_reg_rule *reg_rule = NULL;
  640. const struct ieee80211_freq_range *freq_range = NULL;
  641. const struct ieee80211_power_rule *power_rule = NULL;
  642. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  643. "(max_antenna_gain, max_eirp)\n");
  644. for (i = 0; i < rd->n_reg_rules; i++) {
  645. reg_rule = &rd->reg_rules[i];
  646. freq_range = &reg_rule->freq_range;
  647. power_rule = &reg_rule->power_rule;
  648. /* There may not be documentation for max antenna gain
  649. * in certain regions */
  650. if (power_rule->max_antenna_gain)
  651. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  652. "(%d mBi, %d mBm)\n",
  653. freq_range->start_freq_khz,
  654. freq_range->end_freq_khz,
  655. freq_range->max_bandwidth_khz,
  656. power_rule->max_antenna_gain,
  657. power_rule->max_eirp);
  658. else
  659. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  660. "(N/A, %d mBm)\n",
  661. freq_range->start_freq_khz,
  662. freq_range->end_freq_khz,
  663. freq_range->max_bandwidth_khz,
  664. power_rule->max_eirp);
  665. }
  666. }
  667. static void print_regdomain(const struct ieee80211_regdomain *rd)
  668. {
  669. if (is_world_regdom(rd->alpha2))
  670. printk(KERN_INFO "cfg80211: World regulatory "
  671. "domain updated:\n");
  672. else {
  673. if (is_unknown_alpha2(rd->alpha2))
  674. printk(KERN_INFO "cfg80211: Regulatory domain "
  675. "changed to driver built-in settings "
  676. "(unknown country)\n");
  677. else
  678. printk(KERN_INFO "cfg80211: Regulatory domain "
  679. "changed to country: %c%c\n",
  680. rd->alpha2[0], rd->alpha2[1]);
  681. }
  682. print_rd_rules(rd);
  683. }
  684. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  685. {
  686. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  687. rd->alpha2[0], rd->alpha2[1]);
  688. print_rd_rules(rd);
  689. }
  690. /* Takes ownership of rd only if it doesn't fail */
  691. static int __set_regdom(const struct ieee80211_regdomain *rd)
  692. {
  693. const struct ieee80211_regdomain *intersected_rd = NULL;
  694. /* Some basic sanity checks first */
  695. if (is_world_regdom(rd->alpha2)) {
  696. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  697. return -EINVAL;
  698. update_world_regdomain(rd);
  699. return 0;
  700. }
  701. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  702. !is_unknown_alpha2(rd->alpha2))
  703. return -EINVAL;
  704. if (!last_request)
  705. return -EINVAL;
  706. /* allow overriding the static definitions if CRDA is present */
  707. if (!is_old_static_regdom(cfg80211_regdomain) &&
  708. !regdom_changed(rd->alpha2))
  709. return -EINVAL;
  710. /* Now lets set the regulatory domain, update all driver channels
  711. * and finally inform them of what we have done, in case they want
  712. * to review or adjust their own settings based on their own
  713. * internal EEPROM data */
  714. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  715. return -EINVAL;
  716. reset_regdomains();
  717. /* Country IE parsing coming soon */
  718. if (!is_valid_rd(rd)) {
  719. printk(KERN_ERR "cfg80211: Invalid "
  720. "regulatory domain detected:\n");
  721. print_regdomain_info(rd);
  722. return -EINVAL;
  723. }
  724. if (unlikely(last_request->intersect)) {
  725. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  726. if (!intersected_rd)
  727. return -EINVAL;
  728. kfree(rd);
  729. rd = intersected_rd;
  730. }
  731. /* Tada! */
  732. cfg80211_regdomain = rd;
  733. return 0;
  734. }
  735. /* Use this call to set the current regulatory domain. Conflicts with
  736. * multiple drivers can be ironed out later. Caller must've already
  737. * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
  738. int set_regdom(const struct ieee80211_regdomain *rd)
  739. {
  740. int r;
  741. /* Note that this doesn't update the wiphys, this is done below */
  742. r = __set_regdom(rd);
  743. if (r) {
  744. kfree(rd);
  745. return r;
  746. }
  747. /* This would make this whole thing pointless */
  748. BUG_ON(rd != cfg80211_regdomain);
  749. /* update all wiphys now with the new established regulatory domain */
  750. update_all_wiphy_regulatory(last_request->initiator);
  751. print_regdomain(rd);
  752. return r;
  753. }
  754. int regulatory_init(void)
  755. {
  756. int err;
  757. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  758. if (IS_ERR(reg_pdev))
  759. return PTR_ERR(reg_pdev);
  760. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  761. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  762. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  763. print_regdomain_info(cfg80211_regdomain);
  764. /* The old code still requests for a new regdomain and if
  765. * you have CRDA you get it updated, otherwise you get
  766. * stuck with the static values. We ignore "EU" code as
  767. * that is not a valid ISO / IEC 3166 alpha2 */
  768. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  769. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  770. ieee80211_regdom);
  771. #else
  772. cfg80211_regdomain = cfg80211_world_regdom;
  773. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00");
  774. if (err)
  775. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  776. "unable to update world regulatory domain, "
  777. "using static definition\n");
  778. #endif
  779. return 0;
  780. }
  781. void regulatory_exit(void)
  782. {
  783. mutex_lock(&cfg80211_drv_mutex);
  784. reset_regdomains();
  785. kfree(last_request);
  786. platform_device_unregister(reg_pdev);
  787. mutex_unlock(&cfg80211_drv_mutex);
  788. }