reg.c 43 KB

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