reg.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. * @country_ie_checksum: checksum of the last processed and accepted
  62. * country IE
  63. * @country_ie_env: lets us know if the AP is telling us we are outdoor,
  64. * indoor, or if it doesn't matter
  65. */
  66. struct regulatory_request {
  67. struct wiphy *wiphy;
  68. enum reg_set_by initiator;
  69. char alpha2[2];
  70. bool intersect;
  71. u32 country_ie_checksum;
  72. enum environment_cap country_ie_env;
  73. };
  74. /* Receipt of information from last regulatory request */
  75. static struct regulatory_request *last_request;
  76. /* To trigger userspace events */
  77. static struct platform_device *reg_pdev;
  78. /* Keep the ordering from large to small */
  79. static u32 supported_bandwidths[] = {
  80. MHZ_TO_KHZ(40),
  81. MHZ_TO_KHZ(20),
  82. };
  83. /* Central wireless core regulatory domains, we only need two,
  84. * the current one and a world regulatory domain in case we have no
  85. * information to give us an alpha2 */
  86. static const struct ieee80211_regdomain *cfg80211_regdomain;
  87. /* We use this as a place for the rd structure built from the
  88. * last parsed country IE to rest until CRDA gets back to us with
  89. * what it thinks should apply for the same country */
  90. static const struct ieee80211_regdomain *country_ie_regdomain;
  91. /* We keep a static world regulatory domain in case of the absence of CRDA */
  92. static const struct ieee80211_regdomain world_regdom = {
  93. .n_reg_rules = 1,
  94. .alpha2 = "00",
  95. .reg_rules = {
  96. REG_RULE(2412-10, 2462+10, 40, 6, 20,
  97. NL80211_RRF_PASSIVE_SCAN |
  98. NL80211_RRF_NO_IBSS),
  99. }
  100. };
  101. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  102. &world_regdom;
  103. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  104. static char *ieee80211_regdom = "US";
  105. module_param(ieee80211_regdom, charp, 0444);
  106. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  107. /* We assume 40 MHz bandwidth for the old regulatory work.
  108. * We make emphasis we are using the exact same frequencies
  109. * as before */
  110. static const struct ieee80211_regdomain us_regdom = {
  111. .n_reg_rules = 6,
  112. .alpha2 = "US",
  113. .reg_rules = {
  114. /* IEEE 802.11b/g, channels 1..11 */
  115. REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
  116. /* IEEE 802.11a, channel 36 */
  117. REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
  118. /* IEEE 802.11a, channel 40 */
  119. REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
  120. /* IEEE 802.11a, channel 44 */
  121. REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
  122. /* IEEE 802.11a, channels 48..64 */
  123. REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
  124. /* IEEE 802.11a, channels 149..165, outdoor */
  125. REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
  126. }
  127. };
  128. static const struct ieee80211_regdomain jp_regdom = {
  129. .n_reg_rules = 3,
  130. .alpha2 = "JP",
  131. .reg_rules = {
  132. /* IEEE 802.11b/g, channels 1..14 */
  133. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  134. /* IEEE 802.11a, channels 34..48 */
  135. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  136. NL80211_RRF_PASSIVE_SCAN),
  137. /* IEEE 802.11a, channels 52..64 */
  138. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  139. NL80211_RRF_NO_IBSS |
  140. NL80211_RRF_DFS),
  141. }
  142. };
  143. static const struct ieee80211_regdomain eu_regdom = {
  144. .n_reg_rules = 6,
  145. /* This alpha2 is bogus, we leave it here just for stupid
  146. * backward compatibility */
  147. .alpha2 = "EU",
  148. .reg_rules = {
  149. /* IEEE 802.11b/g, channels 1..13 */
  150. REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
  151. /* IEEE 802.11a, channel 36 */
  152. REG_RULE(5180-10, 5180+10, 40, 6, 23,
  153. NL80211_RRF_PASSIVE_SCAN),
  154. /* IEEE 802.11a, channel 40 */
  155. REG_RULE(5200-10, 5200+10, 40, 6, 23,
  156. NL80211_RRF_PASSIVE_SCAN),
  157. /* IEEE 802.11a, channel 44 */
  158. REG_RULE(5220-10, 5220+10, 40, 6, 23,
  159. NL80211_RRF_PASSIVE_SCAN),
  160. /* IEEE 802.11a, channels 48..64 */
  161. REG_RULE(5240-10, 5320+10, 40, 6, 20,
  162. NL80211_RRF_NO_IBSS |
  163. NL80211_RRF_DFS),
  164. /* IEEE 802.11a, channels 100..140 */
  165. REG_RULE(5500-10, 5700+10, 40, 6, 30,
  166. NL80211_RRF_NO_IBSS |
  167. NL80211_RRF_DFS),
  168. }
  169. };
  170. static const struct ieee80211_regdomain *static_regdom(char *alpha2)
  171. {
  172. if (alpha2[0] == 'U' && alpha2[1] == 'S')
  173. return &us_regdom;
  174. if (alpha2[0] == 'J' && alpha2[1] == 'P')
  175. return &jp_regdom;
  176. if (alpha2[0] == 'E' && alpha2[1] == 'U')
  177. return &eu_regdom;
  178. /* Default, as per the old rules */
  179. return &us_regdom;
  180. }
  181. static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  182. {
  183. if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
  184. return true;
  185. return false;
  186. }
  187. #else
  188. static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  189. {
  190. return false;
  191. }
  192. #endif
  193. static void reset_regdomains(void)
  194. {
  195. /* avoid freeing static information or freeing something twice */
  196. if (cfg80211_regdomain == cfg80211_world_regdom)
  197. cfg80211_regdomain = NULL;
  198. if (cfg80211_world_regdom == &world_regdom)
  199. cfg80211_world_regdom = NULL;
  200. if (cfg80211_regdomain == &world_regdom)
  201. cfg80211_regdomain = NULL;
  202. if (is_old_static_regdom(cfg80211_regdomain))
  203. cfg80211_regdomain = NULL;
  204. kfree(cfg80211_regdomain);
  205. kfree(cfg80211_world_regdom);
  206. cfg80211_world_regdom = &world_regdom;
  207. cfg80211_regdomain = NULL;
  208. }
  209. /* Dynamic world regulatory domain requested by the wireless
  210. * core upon initialization */
  211. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  212. {
  213. BUG_ON(!last_request);
  214. reset_regdomains();
  215. cfg80211_world_regdom = rd;
  216. cfg80211_regdomain = rd;
  217. }
  218. bool is_world_regdom(const char *alpha2)
  219. {
  220. if (!alpha2)
  221. return false;
  222. if (alpha2[0] == '0' && alpha2[1] == '0')
  223. return true;
  224. return false;
  225. }
  226. static bool is_alpha2_set(const char *alpha2)
  227. {
  228. if (!alpha2)
  229. return false;
  230. if (alpha2[0] != 0 && alpha2[1] != 0)
  231. return true;
  232. return false;
  233. }
  234. static bool is_alpha_upper(char letter)
  235. {
  236. /* ASCII A - Z */
  237. if (letter >= 65 && letter <= 90)
  238. return true;
  239. return false;
  240. }
  241. static bool is_unknown_alpha2(const char *alpha2)
  242. {
  243. if (!alpha2)
  244. return false;
  245. /* Special case where regulatory domain was built by driver
  246. * but a specific alpha2 cannot be determined */
  247. if (alpha2[0] == '9' && alpha2[1] == '9')
  248. return true;
  249. return false;
  250. }
  251. static bool is_intersected_alpha2(const char *alpha2)
  252. {
  253. if (!alpha2)
  254. return false;
  255. /* Special case where regulatory domain is the
  256. * result of an intersection between two regulatory domain
  257. * structures */
  258. if (alpha2[0] == '9' && alpha2[1] == '8')
  259. return true;
  260. return false;
  261. }
  262. static bool is_an_alpha2(const char *alpha2)
  263. {
  264. if (!alpha2)
  265. return false;
  266. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  267. return true;
  268. return false;
  269. }
  270. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  271. {
  272. if (!alpha2_x || !alpha2_y)
  273. return false;
  274. if (alpha2_x[0] == alpha2_y[0] &&
  275. alpha2_x[1] == alpha2_y[1])
  276. return true;
  277. return false;
  278. }
  279. static bool regdom_changed(const char *alpha2)
  280. {
  281. if (!cfg80211_regdomain)
  282. return true;
  283. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  284. return false;
  285. return true;
  286. }
  287. /**
  288. * country_ie_integrity_changes - tells us if the country IE has changed
  289. * @checksum: checksum of country IE of fields we are interested in
  290. *
  291. * If the country IE has not changed you can ignore it safely. This is
  292. * useful to determine if two devices are seeing two different country IEs
  293. * even on the same alpha2. Note that this will return false if no IE has
  294. * been set on the wireless core yet.
  295. */
  296. static bool country_ie_integrity_changes(u32 checksum)
  297. {
  298. /* If no IE has been set then the checksum doesn't change */
  299. if (unlikely(!last_request->country_ie_checksum))
  300. return false;
  301. if (unlikely(last_request->country_ie_checksum != checksum))
  302. return true;
  303. return false;
  304. }
  305. /* This lets us keep regulatory code which is updated on a regulatory
  306. * basis in userspace. */
  307. static int call_crda(const char *alpha2)
  308. {
  309. char country_env[9 + 2] = "COUNTRY=";
  310. char *envp[] = {
  311. country_env,
  312. NULL
  313. };
  314. if (!is_world_regdom((char *) alpha2))
  315. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  316. alpha2[0], alpha2[1]);
  317. else
  318. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  319. "regulatory domain\n");
  320. country_env[8] = alpha2[0];
  321. country_env[9] = alpha2[1];
  322. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  323. }
  324. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  325. bool reg_is_valid_request(const char *alpha2)
  326. {
  327. if (!last_request)
  328. return false;
  329. return alpha2_equal(last_request->alpha2, alpha2);
  330. }
  331. /* Sanity check on a regulatory rule */
  332. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  333. {
  334. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  335. u32 freq_diff;
  336. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  337. return false;
  338. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  339. return false;
  340. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  341. if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
  342. return false;
  343. return true;
  344. }
  345. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  346. {
  347. const struct ieee80211_reg_rule *reg_rule = NULL;
  348. unsigned int i;
  349. if (!rd->n_reg_rules)
  350. return false;
  351. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  352. return false;
  353. for (i = 0; i < rd->n_reg_rules; i++) {
  354. reg_rule = &rd->reg_rules[i];
  355. if (!is_valid_reg_rule(reg_rule))
  356. return false;
  357. }
  358. return true;
  359. }
  360. /* Returns value in KHz */
  361. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  362. u32 freq)
  363. {
  364. unsigned int i;
  365. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  366. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  367. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  368. if (start_freq_khz >= freq_range->start_freq_khz &&
  369. end_freq_khz <= freq_range->end_freq_khz)
  370. return supported_bandwidths[i];
  371. }
  372. return 0;
  373. }
  374. /**
  375. * freq_in_rule_band - tells us if a frequency is in a frequency band
  376. * @freq_range: frequency rule we want to query
  377. * @freq_khz: frequency we are inquiring about
  378. *
  379. * This lets us know if a specific frequency rule is or is not relevant to
  380. * a specific frequency's band. Bands are device specific and artificial
  381. * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
  382. * safe for now to assume that a frequency rule should not be part of a
  383. * frequency's band if the start freq or end freq are off by more than 2 GHz.
  384. * This resolution can be lowered and should be considered as we add
  385. * regulatory rule support for other "bands".
  386. **/
  387. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  388. u32 freq_khz)
  389. {
  390. #define ONE_GHZ_IN_KHZ 1000000
  391. if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  392. return true;
  393. if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  394. return true;
  395. return false;
  396. #undef ONE_GHZ_IN_KHZ
  397. }
  398. /* Converts a country IE to a regulatory domain. A regulatory domain
  399. * structure has a lot of information which the IE doesn't yet have,
  400. * so for the other values we use upper max values as we will intersect
  401. * with our userspace regulatory agent to get lower bounds. */
  402. static struct ieee80211_regdomain *country_ie_2_rd(
  403. u8 *country_ie,
  404. u8 country_ie_len,
  405. u32 *checksum)
  406. {
  407. struct ieee80211_regdomain *rd = NULL;
  408. unsigned int i = 0;
  409. char alpha2[2];
  410. u32 flags = 0;
  411. u32 num_rules = 0, size_of_regd = 0;
  412. u8 *triplets_start = NULL;
  413. u8 len_at_triplet = 0;
  414. /* the last channel we have registered in a subband (triplet) */
  415. int last_sub_max_channel = 0;
  416. *checksum = 0xDEADBEEF;
  417. /* Country IE requirements */
  418. BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
  419. country_ie_len & 0x01);
  420. alpha2[0] = country_ie[0];
  421. alpha2[1] = country_ie[1];
  422. /*
  423. * Third octet can be:
  424. * 'I' - Indoor
  425. * 'O' - Outdoor
  426. *
  427. * anything else we assume is no restrictions
  428. */
  429. if (country_ie[2] == 'I')
  430. flags = NL80211_RRF_NO_OUTDOOR;
  431. else if (country_ie[2] == 'O')
  432. flags = NL80211_RRF_NO_INDOOR;
  433. country_ie += 3;
  434. country_ie_len -= 3;
  435. triplets_start = country_ie;
  436. len_at_triplet = country_ie_len;
  437. *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
  438. /* We need to build a reg rule for each triplet, but first we must
  439. * calculate the number of reg rules we will need. We will need one
  440. * for each channel subband */
  441. while (country_ie_len >= 3) {
  442. struct ieee80211_country_ie_triplet *triplet =
  443. (struct ieee80211_country_ie_triplet *) country_ie;
  444. int cur_sub_max_channel = 0, cur_channel = 0;
  445. if (triplet->ext.reg_extension_id >=
  446. IEEE80211_COUNTRY_EXTENSION_ID) {
  447. country_ie += 3;
  448. country_ie_len -= 3;
  449. continue;
  450. }
  451. cur_channel = triplet->chans.first_channel;
  452. cur_sub_max_channel = ieee80211_channel_to_frequency(
  453. cur_channel + triplet->chans.num_channels);
  454. /* Basic sanity check */
  455. if (cur_sub_max_channel < cur_channel)
  456. return NULL;
  457. /* Do not allow overlapping channels. Also channels
  458. * passed in each subband must be monotonically
  459. * increasing */
  460. if (last_sub_max_channel) {
  461. if (cur_channel <= last_sub_max_channel)
  462. return NULL;
  463. if (cur_sub_max_channel <= last_sub_max_channel)
  464. return NULL;
  465. }
  466. /* When dot11RegulatoryClassesRequired is supported
  467. * we can throw ext triplets as part of this soup,
  468. * for now we don't care when those change as we
  469. * don't support them */
  470. *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
  471. ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
  472. ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
  473. last_sub_max_channel = cur_sub_max_channel;
  474. country_ie += 3;
  475. country_ie_len -= 3;
  476. num_rules++;
  477. /* Note: this is not a IEEE requirement but
  478. * simply a memory requirement */
  479. if (num_rules > NL80211_MAX_SUPP_REG_RULES)
  480. return NULL;
  481. }
  482. country_ie = triplets_start;
  483. country_ie_len = len_at_triplet;
  484. size_of_regd = sizeof(struct ieee80211_regdomain) +
  485. (num_rules * sizeof(struct ieee80211_reg_rule));
  486. rd = kzalloc(size_of_regd, GFP_KERNEL);
  487. if (!rd)
  488. return NULL;
  489. rd->n_reg_rules = num_rules;
  490. rd->alpha2[0] = alpha2[0];
  491. rd->alpha2[1] = alpha2[1];
  492. /* This time around we fill in the rd */
  493. while (country_ie_len >= 3) {
  494. int end_channel = 0;
  495. struct ieee80211_country_ie_triplet *triplet =
  496. (struct ieee80211_country_ie_triplet *) country_ie;
  497. struct ieee80211_reg_rule *reg_rule = NULL;
  498. struct ieee80211_freq_range *freq_range = NULL;
  499. struct ieee80211_power_rule *power_rule = NULL;
  500. /* Must parse if dot11RegulatoryClassesRequired is true,
  501. * we don't support this yet */
  502. if (triplet->ext.reg_extension_id >=
  503. IEEE80211_COUNTRY_EXTENSION_ID) {
  504. country_ie += 3;
  505. country_ie_len -= 3;
  506. continue;
  507. }
  508. reg_rule = &rd->reg_rules[i];
  509. freq_range = &reg_rule->freq_range;
  510. power_rule = &reg_rule->power_rule;
  511. reg_rule->flags = flags;
  512. /* 2 GHz */
  513. if (triplet->chans.first_channel <= 14)
  514. end_channel = triplet->chans.first_channel +
  515. triplet->chans.num_channels;
  516. else
  517. /*
  518. * 5 GHz -- For example in country IEs if the first
  519. * channel given is 36 and the number of channels is 4
  520. * then the individual channel numbers defined for the
  521. * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
  522. * and not 36, 37, 38, 39.
  523. *
  524. * See: http://tinyurl.com/11d-clarification
  525. */
  526. end_channel = triplet->chans.first_channel +
  527. (4 * (triplet->chans.num_channels - 1));
  528. /* The +10 is since the regulatory domain expects
  529. * the actual band edge, not the center of freq for
  530. * its start and end freqs, assuming 20 MHz bandwidth on
  531. * the channels passed */
  532. freq_range->start_freq_khz =
  533. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  534. triplet->chans.first_channel) - 10);
  535. freq_range->end_freq_khz =
  536. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  537. end_channel) + 10);
  538. /* Large arbitrary values, we intersect later */
  539. /* Increment this if we ever support >= 40 MHz channels
  540. * in IEEE 802.11 */
  541. freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
  542. power_rule->max_antenna_gain = DBI_TO_MBI(100);
  543. power_rule->max_eirp = DBM_TO_MBM(100);
  544. country_ie += 3;
  545. country_ie_len -= 3;
  546. i++;
  547. BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
  548. }
  549. return rd;
  550. }
  551. /* Helper for regdom_intersect(), this does the real
  552. * mathematical intersection fun */
  553. static int reg_rules_intersect(
  554. const struct ieee80211_reg_rule *rule1,
  555. const struct ieee80211_reg_rule *rule2,
  556. struct ieee80211_reg_rule *intersected_rule)
  557. {
  558. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  559. struct ieee80211_freq_range *freq_range;
  560. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  561. struct ieee80211_power_rule *power_rule;
  562. u32 freq_diff;
  563. freq_range1 = &rule1->freq_range;
  564. freq_range2 = &rule2->freq_range;
  565. freq_range = &intersected_rule->freq_range;
  566. power_rule1 = &rule1->power_rule;
  567. power_rule2 = &rule2->power_rule;
  568. power_rule = &intersected_rule->power_rule;
  569. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  570. freq_range2->start_freq_khz);
  571. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  572. freq_range2->end_freq_khz);
  573. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  574. freq_range2->max_bandwidth_khz);
  575. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  576. if (freq_range->max_bandwidth_khz > freq_diff)
  577. freq_range->max_bandwidth_khz = freq_diff;
  578. power_rule->max_eirp = min(power_rule1->max_eirp,
  579. power_rule2->max_eirp);
  580. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  581. power_rule2->max_antenna_gain);
  582. intersected_rule->flags = (rule1->flags | rule2->flags);
  583. if (!is_valid_reg_rule(intersected_rule))
  584. return -EINVAL;
  585. return 0;
  586. }
  587. /**
  588. * regdom_intersect - do the intersection between two regulatory domains
  589. * @rd1: first regulatory domain
  590. * @rd2: second regulatory domain
  591. *
  592. * Use this function to get the intersection between two regulatory domains.
  593. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  594. * as no one single alpha2 can represent this regulatory domain.
  595. *
  596. * Returns a pointer to the regulatory domain structure which will hold the
  597. * resulting intersection of rules between rd1 and rd2. We will
  598. * kzalloc() this structure for you.
  599. */
  600. static struct ieee80211_regdomain *regdom_intersect(
  601. const struct ieee80211_regdomain *rd1,
  602. const struct ieee80211_regdomain *rd2)
  603. {
  604. int r, size_of_regd;
  605. unsigned int x, y;
  606. unsigned int num_rules = 0, rule_idx = 0;
  607. const struct ieee80211_reg_rule *rule1, *rule2;
  608. struct ieee80211_reg_rule *intersected_rule;
  609. struct ieee80211_regdomain *rd;
  610. /* This is just a dummy holder to help us count */
  611. struct ieee80211_reg_rule irule;
  612. /* Uses the stack temporarily for counter arithmetic */
  613. intersected_rule = &irule;
  614. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  615. if (!rd1 || !rd2)
  616. return NULL;
  617. /* First we get a count of the rules we'll need, then we actually
  618. * build them. This is to so we can malloc() and free() a
  619. * regdomain once. The reason we use reg_rules_intersect() here
  620. * is it will return -EINVAL if the rule computed makes no sense.
  621. * All rules that do check out OK are valid. */
  622. for (x = 0; x < rd1->n_reg_rules; x++) {
  623. rule1 = &rd1->reg_rules[x];
  624. for (y = 0; y < rd2->n_reg_rules; y++) {
  625. rule2 = &rd2->reg_rules[y];
  626. if (!reg_rules_intersect(rule1, rule2,
  627. intersected_rule))
  628. num_rules++;
  629. memset(intersected_rule, 0,
  630. sizeof(struct ieee80211_reg_rule));
  631. }
  632. }
  633. if (!num_rules)
  634. return NULL;
  635. size_of_regd = sizeof(struct ieee80211_regdomain) +
  636. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  637. rd = kzalloc(size_of_regd, GFP_KERNEL);
  638. if (!rd)
  639. return NULL;
  640. for (x = 0; x < rd1->n_reg_rules; x++) {
  641. rule1 = &rd1->reg_rules[x];
  642. for (y = 0; y < rd2->n_reg_rules; y++) {
  643. rule2 = &rd2->reg_rules[y];
  644. /* This time around instead of using the stack lets
  645. * write to the target rule directly saving ourselves
  646. * a memcpy() */
  647. intersected_rule = &rd->reg_rules[rule_idx];
  648. r = reg_rules_intersect(rule1, rule2,
  649. intersected_rule);
  650. /* No need to memset here the intersected rule here as
  651. * we're not using the stack anymore */
  652. if (r)
  653. continue;
  654. rule_idx++;
  655. }
  656. }
  657. if (rule_idx != num_rules) {
  658. kfree(rd);
  659. return NULL;
  660. }
  661. rd->n_reg_rules = num_rules;
  662. rd->alpha2[0] = '9';
  663. rd->alpha2[1] = '8';
  664. return rd;
  665. }
  666. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  667. * want to just have the channel structure use these */
  668. static u32 map_regdom_flags(u32 rd_flags)
  669. {
  670. u32 channel_flags = 0;
  671. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  672. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  673. if (rd_flags & NL80211_RRF_NO_IBSS)
  674. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  675. if (rd_flags & NL80211_RRF_DFS)
  676. channel_flags |= IEEE80211_CHAN_RADAR;
  677. return channel_flags;
  678. }
  679. /**
  680. * freq_reg_info - get regulatory information for the given frequency
  681. * @center_freq: Frequency in KHz for which we want regulatory information for
  682. * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
  683. * you can set this to 0. If this frequency is allowed we then set
  684. * this value to the maximum allowed bandwidth.
  685. * @reg_rule: the regulatory rule which we have for this frequency
  686. *
  687. * Use this function to get the regulatory rule for a specific frequency on
  688. * a given wireless device. If the device has a specific regulatory domain
  689. * it wants to follow we respect that unless a country IE has been received
  690. * and processed already.
  691. *
  692. * Returns 0 if it was able to find a valid regulatory rule which does
  693. * apply to the given center_freq otherwise it returns non-zero. It will
  694. * also return -ERANGE if we determine the given center_freq does not even have
  695. * a regulatory rule for a frequency range in the center_freq's band. See
  696. * freq_in_rule_band() for our current definition of a band -- this is purely
  697. * subjective and right now its 802.11 specific.
  698. */
  699. static int freq_reg_info(u32 center_freq, u32 *bandwidth,
  700. const struct ieee80211_reg_rule **reg_rule)
  701. {
  702. int i;
  703. bool band_rule_found = false;
  704. u32 max_bandwidth = 0;
  705. if (!cfg80211_regdomain)
  706. return -EINVAL;
  707. for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
  708. const struct ieee80211_reg_rule *rr;
  709. const struct ieee80211_freq_range *fr = NULL;
  710. const struct ieee80211_power_rule *pr = NULL;
  711. rr = &cfg80211_regdomain->reg_rules[i];
  712. fr = &rr->freq_range;
  713. pr = &rr->power_rule;
  714. /* We only need to know if one frequency rule was
  715. * was in center_freq's band, that's enough, so lets
  716. * not overwrite it once found */
  717. if (!band_rule_found)
  718. band_rule_found = freq_in_rule_band(fr, center_freq);
  719. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  720. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  721. *reg_rule = rr;
  722. *bandwidth = max_bandwidth;
  723. break;
  724. }
  725. }
  726. if (!band_rule_found)
  727. return -ERANGE;
  728. return !max_bandwidth;
  729. }
  730. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  731. unsigned int chan_idx)
  732. {
  733. int r;
  734. u32 flags;
  735. u32 max_bandwidth = 0;
  736. const struct ieee80211_reg_rule *reg_rule = NULL;
  737. const struct ieee80211_power_rule *power_rule = NULL;
  738. struct ieee80211_supported_band *sband;
  739. struct ieee80211_channel *chan;
  740. sband = wiphy->bands[band];
  741. BUG_ON(chan_idx >= sband->n_channels);
  742. chan = &sband->channels[chan_idx];
  743. flags = chan->orig_flags;
  744. r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
  745. &max_bandwidth, &reg_rule);
  746. if (r) {
  747. /* This means no regulatory rule was found in the country IE
  748. * with a frequency range on the center_freq's band, since
  749. * IEEE-802.11 allows for a country IE to have a subset of the
  750. * regulatory information provided in a country we ignore
  751. * disabling the channel unless at least one reg rule was
  752. * found on the center_freq's band. For details see this
  753. * clarification:
  754. *
  755. * http://tinyurl.com/11d-clarification
  756. */
  757. if (r == -ERANGE &&
  758. last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  759. #ifdef CONFIG_CFG80211_REG_DEBUG
  760. printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
  761. "intact on %s - no rule found in band on "
  762. "Country IE\n",
  763. chan->center_freq, wiphy_name(wiphy));
  764. #endif
  765. } else {
  766. /* In this case we know the country IE has at least one reg rule
  767. * for the band so we respect its band definitions */
  768. #ifdef CONFIG_CFG80211_REG_DEBUG
  769. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  770. printk(KERN_DEBUG "cfg80211: Disabling "
  771. "channel %d MHz on %s due to "
  772. "Country IE\n",
  773. chan->center_freq, wiphy_name(wiphy));
  774. #endif
  775. flags |= IEEE80211_CHAN_DISABLED;
  776. chan->flags = flags;
  777. }
  778. return;
  779. }
  780. power_rule = &reg_rule->power_rule;
  781. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  782. chan->max_antenna_gain = min(chan->orig_mag,
  783. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  784. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  785. if (chan->orig_mpwr)
  786. chan->max_power = min(chan->orig_mpwr,
  787. (int) MBM_TO_DBM(power_rule->max_eirp));
  788. else
  789. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  790. }
  791. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  792. {
  793. unsigned int i;
  794. struct ieee80211_supported_band *sband;
  795. BUG_ON(!wiphy->bands[band]);
  796. sband = wiphy->bands[band];
  797. for (i = 0; i < sband->n_channels; i++)
  798. handle_channel(wiphy, band, i);
  799. }
  800. static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
  801. {
  802. if (!last_request)
  803. return true;
  804. if (setby == REGDOM_SET_BY_CORE &&
  805. wiphy->fw_handles_regulatory)
  806. return true;
  807. return false;
  808. }
  809. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  810. {
  811. struct cfg80211_registered_device *drv;
  812. list_for_each_entry(drv, &cfg80211_drv_list, list)
  813. if (!ignore_reg_update(&drv->wiphy, setby))
  814. wiphy_update_regulatory(&drv->wiphy, setby);
  815. }
  816. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  817. {
  818. enum ieee80211_band band;
  819. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  820. if (wiphy->bands[band])
  821. handle_band(wiphy, band);
  822. if (wiphy->reg_notifier)
  823. wiphy->reg_notifier(wiphy, setby);
  824. }
  825. }
  826. /* Return value which can be used by ignore_request() to indicate
  827. * it has been determined we should intersect two regulatory domains */
  828. #define REG_INTERSECT 1
  829. /* This has the logic which determines when a new request
  830. * should be ignored. */
  831. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  832. const char *alpha2)
  833. {
  834. /* All initial requests are respected */
  835. if (!last_request)
  836. return 0;
  837. switch (set_by) {
  838. case REGDOM_SET_BY_INIT:
  839. return -EINVAL;
  840. case REGDOM_SET_BY_CORE:
  841. /*
  842. * Always respect new wireless core hints, should only happen
  843. * when updating the world regulatory domain at init.
  844. */
  845. return 0;
  846. case REGDOM_SET_BY_COUNTRY_IE:
  847. if (unlikely(!is_an_alpha2(alpha2)))
  848. return -EINVAL;
  849. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  850. if (last_request->wiphy != wiphy) {
  851. /*
  852. * Two cards with two APs claiming different
  853. * different Country IE alpha2s. We could
  854. * intersect them, but that seems unlikely
  855. * to be correct. Reject second one for now.
  856. */
  857. if (!alpha2_equal(alpha2,
  858. cfg80211_regdomain->alpha2))
  859. return -EOPNOTSUPP;
  860. return -EALREADY;
  861. }
  862. /* Two consecutive Country IE hints on the same wiphy.
  863. * This should be picked up early by the driver/stack */
  864. if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
  865. alpha2)))
  866. return 0;
  867. return -EALREADY;
  868. }
  869. return REG_INTERSECT;
  870. case REGDOM_SET_BY_DRIVER:
  871. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  872. return -EALREADY;
  873. return 0;
  874. case REGDOM_SET_BY_USER:
  875. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  876. return REG_INTERSECT;
  877. /* If the user knows better the user should set the regdom
  878. * to their country before the IE is picked up */
  879. if (last_request->initiator == REGDOM_SET_BY_USER &&
  880. last_request->intersect)
  881. return -EOPNOTSUPP;
  882. return 0;
  883. }
  884. return -EINVAL;
  885. }
  886. /* Caller must hold &cfg80211_drv_mutex */
  887. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  888. const char *alpha2,
  889. u32 country_ie_checksum,
  890. enum environment_cap env)
  891. {
  892. struct regulatory_request *request;
  893. bool intersect = false;
  894. int r = 0;
  895. r = ignore_request(wiphy, set_by, alpha2);
  896. if (r == REG_INTERSECT)
  897. intersect = true;
  898. else if (r)
  899. return r;
  900. request = kzalloc(sizeof(struct regulatory_request),
  901. GFP_KERNEL);
  902. if (!request)
  903. return -ENOMEM;
  904. request->alpha2[0] = alpha2[0];
  905. request->alpha2[1] = alpha2[1];
  906. request->initiator = set_by;
  907. request->wiphy = wiphy;
  908. request->intersect = intersect;
  909. request->country_ie_checksum = country_ie_checksum;
  910. request->country_ie_env = env;
  911. kfree(last_request);
  912. last_request = request;
  913. /*
  914. * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
  915. * AND if CRDA is NOT present nothing will happen, if someone
  916. * wants to bother with 11d with OLD_REG you can add a timer.
  917. * If after x amount of time nothing happens you can call:
  918. *
  919. * return set_regdom(country_ie_regdomain);
  920. *
  921. * to intersect with the static rd
  922. */
  923. return call_crda(alpha2);
  924. }
  925. void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  926. {
  927. BUG_ON(!alpha2);
  928. mutex_lock(&cfg80211_drv_mutex);
  929. __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY);
  930. mutex_unlock(&cfg80211_drv_mutex);
  931. }
  932. EXPORT_SYMBOL(regulatory_hint);
  933. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  934. u32 country_ie_checksum)
  935. {
  936. if (!last_request->wiphy)
  937. return false;
  938. if (likely(last_request->wiphy != wiphy))
  939. return !country_ie_integrity_changes(country_ie_checksum);
  940. /* We should not have let these through at this point, they
  941. * should have been picked up earlier by the first alpha2 check
  942. * on the device */
  943. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  944. return true;
  945. return false;
  946. }
  947. void regulatory_hint_11d(struct wiphy *wiphy,
  948. u8 *country_ie,
  949. u8 country_ie_len)
  950. {
  951. struct ieee80211_regdomain *rd = NULL;
  952. char alpha2[2];
  953. u32 checksum = 0;
  954. enum environment_cap env = ENVIRON_ANY;
  955. if (!last_request)
  956. return;
  957. mutex_lock(&cfg80211_drv_mutex);
  958. /* IE len must be evenly divisible by 2 */
  959. if (country_ie_len & 0x01)
  960. goto out;
  961. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  962. goto out;
  963. /* Pending country IE processing, this can happen after we
  964. * call CRDA and wait for a response if a beacon was received before
  965. * we were able to process the last regulatory_hint_11d() call */
  966. if (country_ie_regdomain)
  967. goto out;
  968. alpha2[0] = country_ie[0];
  969. alpha2[1] = country_ie[1];
  970. if (country_ie[2] == 'I')
  971. env = ENVIRON_INDOOR;
  972. else if (country_ie[2] == 'O')
  973. env = ENVIRON_OUTDOOR;
  974. /* We will run this for *every* beacon processed for the BSSID, so
  975. * we optimize an early check to exit out early if we don't have to
  976. * do anything */
  977. if (likely(last_request->wiphy)) {
  978. struct cfg80211_registered_device *drv_last_ie;
  979. drv_last_ie = wiphy_to_dev(last_request->wiphy);
  980. /* Lets keep this simple -- we trust the first AP
  981. * after we intersect with CRDA */
  982. if (likely(last_request->wiphy == wiphy)) {
  983. /* Ignore IEs coming in on this wiphy with
  984. * the same alpha2 and environment cap */
  985. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  986. alpha2) &&
  987. env == drv_last_ie->env)) {
  988. goto out;
  989. }
  990. /* the wiphy moved on to another BSSID or the AP
  991. * was reconfigured. XXX: We need to deal with the
  992. * case where the user suspends and goes to goes
  993. * to another country, and then gets IEs from an
  994. * AP with different settings */
  995. goto out;
  996. } else {
  997. /* Ignore IEs coming in on two separate wiphys with
  998. * the same alpha2 and environment cap */
  999. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1000. alpha2) &&
  1001. env == drv_last_ie->env)) {
  1002. goto out;
  1003. }
  1004. /* We could potentially intersect though */
  1005. goto out;
  1006. }
  1007. }
  1008. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1009. if (!rd)
  1010. goto out;
  1011. /* This will not happen right now but we leave it here for the
  1012. * the future when we want to add suspend/resume support and having
  1013. * the user move to another country after doing so, or having the user
  1014. * move to another AP. Right now we just trust the first AP. This is why
  1015. * this is marked as likley(). If we hit this before we add this support
  1016. * we want to be informed of it as it would indicate a mistake in the
  1017. * current design */
  1018. if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
  1019. goto out;
  1020. /* We keep this around for when CRDA comes back with a response so
  1021. * we can intersect with that */
  1022. country_ie_regdomain = rd;
  1023. __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
  1024. country_ie_regdomain->alpha2, checksum, env);
  1025. out:
  1026. mutex_unlock(&cfg80211_drv_mutex);
  1027. }
  1028. EXPORT_SYMBOL(regulatory_hint_11d);
  1029. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1030. {
  1031. unsigned int i;
  1032. const struct ieee80211_reg_rule *reg_rule = NULL;
  1033. const struct ieee80211_freq_range *freq_range = NULL;
  1034. const struct ieee80211_power_rule *power_rule = NULL;
  1035. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  1036. "(max_antenna_gain, max_eirp)\n");
  1037. for (i = 0; i < rd->n_reg_rules; i++) {
  1038. reg_rule = &rd->reg_rules[i];
  1039. freq_range = &reg_rule->freq_range;
  1040. power_rule = &reg_rule->power_rule;
  1041. /* There may not be documentation for max antenna gain
  1042. * in certain regions */
  1043. if (power_rule->max_antenna_gain)
  1044. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1045. "(%d mBi, %d mBm)\n",
  1046. freq_range->start_freq_khz,
  1047. freq_range->end_freq_khz,
  1048. freq_range->max_bandwidth_khz,
  1049. power_rule->max_antenna_gain,
  1050. power_rule->max_eirp);
  1051. else
  1052. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1053. "(N/A, %d mBm)\n",
  1054. freq_range->start_freq_khz,
  1055. freq_range->end_freq_khz,
  1056. freq_range->max_bandwidth_khz,
  1057. power_rule->max_eirp);
  1058. }
  1059. }
  1060. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1061. {
  1062. if (is_intersected_alpha2(rd->alpha2)) {
  1063. struct wiphy *wiphy = NULL;
  1064. struct cfg80211_registered_device *drv;
  1065. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  1066. if (last_request->wiphy) {
  1067. wiphy = last_request->wiphy;
  1068. drv = wiphy_to_dev(wiphy);
  1069. printk(KERN_INFO "cfg80211: Current regulatory "
  1070. "domain updated by AP to: %c%c\n",
  1071. drv->country_ie_alpha2[0],
  1072. drv->country_ie_alpha2[1]);
  1073. } else
  1074. printk(KERN_INFO "cfg80211: Current regulatory "
  1075. "domain intersected: \n");
  1076. } else
  1077. printk(KERN_INFO "cfg80211: Current regulatory "
  1078. "intersected: \n");
  1079. } else if (is_world_regdom(rd->alpha2))
  1080. printk(KERN_INFO "cfg80211: World regulatory "
  1081. "domain updated:\n");
  1082. else {
  1083. if (is_unknown_alpha2(rd->alpha2))
  1084. printk(KERN_INFO "cfg80211: Regulatory domain "
  1085. "changed to driver built-in settings "
  1086. "(unknown country)\n");
  1087. else
  1088. printk(KERN_INFO "cfg80211: Regulatory domain "
  1089. "changed to country: %c%c\n",
  1090. rd->alpha2[0], rd->alpha2[1]);
  1091. }
  1092. print_rd_rules(rd);
  1093. }
  1094. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1095. {
  1096. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1097. rd->alpha2[0], rd->alpha2[1]);
  1098. print_rd_rules(rd);
  1099. }
  1100. #ifdef CONFIG_CFG80211_REG_DEBUG
  1101. static void reg_country_ie_process_debug(
  1102. const struct ieee80211_regdomain *rd,
  1103. const struct ieee80211_regdomain *country_ie_regdomain,
  1104. const struct ieee80211_regdomain *intersected_rd)
  1105. {
  1106. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1107. print_regdomain_info(country_ie_regdomain);
  1108. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1109. print_regdomain_info(rd);
  1110. if (intersected_rd) {
  1111. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1112. "and get:\n");
  1113. print_regdomain_info(rd);
  1114. return;
  1115. }
  1116. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1117. }
  1118. #else
  1119. static inline void reg_country_ie_process_debug(
  1120. const struct ieee80211_regdomain *rd,
  1121. const struct ieee80211_regdomain *country_ie_regdomain,
  1122. const struct ieee80211_regdomain *intersected_rd)
  1123. {
  1124. }
  1125. #endif
  1126. /* Takes ownership of rd only if it doesn't fail */
  1127. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1128. {
  1129. const struct ieee80211_regdomain *intersected_rd = NULL;
  1130. struct cfg80211_registered_device *drv = NULL;
  1131. struct wiphy *wiphy = NULL;
  1132. /* Some basic sanity checks first */
  1133. if (is_world_regdom(rd->alpha2)) {
  1134. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1135. return -EINVAL;
  1136. update_world_regdomain(rd);
  1137. return 0;
  1138. }
  1139. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1140. !is_unknown_alpha2(rd->alpha2))
  1141. return -EINVAL;
  1142. if (!last_request)
  1143. return -EINVAL;
  1144. /* Lets only bother proceeding on the same alpha2 if the current
  1145. * rd is non static (it means CRDA was present and was used last)
  1146. * and the pending request came in from a country IE */
  1147. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1148. /* If someone else asked us to change the rd lets only bother
  1149. * checking if the alpha2 changes if CRDA was already called */
  1150. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1151. !regdom_changed(rd->alpha2))
  1152. return -EINVAL;
  1153. }
  1154. wiphy = last_request->wiphy;
  1155. /* Now lets set the regulatory domain, update all driver channels
  1156. * and finally inform them of what we have done, in case they want
  1157. * to review or adjust their own settings based on their own
  1158. * internal EEPROM data */
  1159. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1160. return -EINVAL;
  1161. if (!is_valid_rd(rd)) {
  1162. printk(KERN_ERR "cfg80211: Invalid "
  1163. "regulatory domain detected:\n");
  1164. print_regdomain_info(rd);
  1165. return -EINVAL;
  1166. }
  1167. if (!last_request->intersect) {
  1168. reset_regdomains();
  1169. cfg80211_regdomain = rd;
  1170. return 0;
  1171. }
  1172. /* Intersection requires a bit more work */
  1173. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1174. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1175. if (!intersected_rd)
  1176. return -EINVAL;
  1177. /* We can trash what CRDA provided now */
  1178. kfree(rd);
  1179. rd = NULL;
  1180. reset_regdomains();
  1181. cfg80211_regdomain = intersected_rd;
  1182. return 0;
  1183. }
  1184. /*
  1185. * Country IE requests are handled a bit differently, we intersect
  1186. * the country IE rd with what CRDA believes that country should have
  1187. */
  1188. BUG_ON(!country_ie_regdomain);
  1189. if (rd != country_ie_regdomain) {
  1190. /* Intersect what CRDA returned and our what we
  1191. * had built from the Country IE received */
  1192. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1193. reg_country_ie_process_debug(rd, country_ie_regdomain,
  1194. intersected_rd);
  1195. kfree(country_ie_regdomain);
  1196. country_ie_regdomain = NULL;
  1197. } else {
  1198. /* This would happen when CRDA was not present and
  1199. * OLD_REGULATORY was enabled. We intersect our Country
  1200. * IE rd and what was set on cfg80211 originally */
  1201. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1202. }
  1203. if (!intersected_rd)
  1204. return -EINVAL;
  1205. drv = wiphy_to_dev(wiphy);
  1206. drv->country_ie_alpha2[0] = rd->alpha2[0];
  1207. drv->country_ie_alpha2[1] = rd->alpha2[1];
  1208. drv->env = last_request->country_ie_env;
  1209. BUG_ON(intersected_rd == rd);
  1210. kfree(rd);
  1211. rd = NULL;
  1212. reset_regdomains();
  1213. cfg80211_regdomain = intersected_rd;
  1214. return 0;
  1215. }
  1216. /* Use this call to set the current regulatory domain. Conflicts with
  1217. * multiple drivers can be ironed out later. Caller must've already
  1218. * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
  1219. int set_regdom(const struct ieee80211_regdomain *rd)
  1220. {
  1221. int r;
  1222. /* Note that this doesn't update the wiphys, this is done below */
  1223. r = __set_regdom(rd);
  1224. if (r) {
  1225. kfree(rd);
  1226. return r;
  1227. }
  1228. /* This would make this whole thing pointless */
  1229. if (!last_request->intersect)
  1230. BUG_ON(rd != cfg80211_regdomain);
  1231. /* update all wiphys now with the new established regulatory domain */
  1232. update_all_wiphy_regulatory(last_request->initiator);
  1233. print_regdomain(cfg80211_regdomain);
  1234. return r;
  1235. }
  1236. /* Caller must hold cfg80211_drv_mutex */
  1237. void reg_device_remove(struct wiphy *wiphy)
  1238. {
  1239. if (!last_request || !last_request->wiphy)
  1240. return;
  1241. if (last_request->wiphy != wiphy)
  1242. return;
  1243. last_request->wiphy = NULL;
  1244. last_request->country_ie_env = ENVIRON_ANY;
  1245. }
  1246. int regulatory_init(void)
  1247. {
  1248. int err;
  1249. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1250. if (IS_ERR(reg_pdev))
  1251. return PTR_ERR(reg_pdev);
  1252. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  1253. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  1254. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  1255. print_regdomain_info(cfg80211_regdomain);
  1256. /* The old code still requests for a new regdomain and if
  1257. * you have CRDA you get it updated, otherwise you get
  1258. * stuck with the static values. We ignore "EU" code as
  1259. * that is not a valid ISO / IEC 3166 alpha2 */
  1260. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  1261. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  1262. ieee80211_regdom, 0, ENVIRON_ANY);
  1263. #else
  1264. cfg80211_regdomain = cfg80211_world_regdom;
  1265. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
  1266. if (err)
  1267. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  1268. "unable to update world regulatory domain, "
  1269. "using static definition\n");
  1270. #endif
  1271. return 0;
  1272. }
  1273. void regulatory_exit(void)
  1274. {
  1275. mutex_lock(&cfg80211_drv_mutex);
  1276. reset_regdomains();
  1277. kfree(country_ie_regdomain);
  1278. country_ie_regdomain = NULL;
  1279. kfree(last_request);
  1280. platform_device_unregister(reg_pdev);
  1281. mutex_unlock(&cfg80211_drv_mutex);
  1282. }