reg.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  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. /* Receipt of information from last regulatory request */
  44. static struct regulatory_request *last_request;
  45. /* To trigger userspace events */
  46. static struct platform_device *reg_pdev;
  47. /* Keep the ordering from large to small */
  48. static u32 supported_bandwidths[] = {
  49. MHZ_TO_KHZ(40),
  50. MHZ_TO_KHZ(20),
  51. };
  52. /*
  53. * Central wireless core regulatory domains, we only need two,
  54. * the current one and a world regulatory domain in case we have no
  55. * information to give us an alpha2
  56. */
  57. const struct ieee80211_regdomain *cfg80211_regdomain;
  58. /*
  59. * We use this as a place for the rd structure built from the
  60. * last parsed country IE to rest until CRDA gets back to us with
  61. * what it thinks should apply for the same country
  62. */
  63. static const struct ieee80211_regdomain *country_ie_regdomain;
  64. static LIST_HEAD(reg_requests_list);
  65. static spinlock_t reg_requests_lock;
  66. /* We keep a static world regulatory domain in case of the absence of CRDA */
  67. static const struct ieee80211_regdomain world_regdom = {
  68. .n_reg_rules = 1,
  69. .alpha2 = "00",
  70. .reg_rules = {
  71. /* IEEE 802.11b/g, channels 1..11 */
  72. REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
  73. }
  74. };
  75. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  76. &world_regdom;
  77. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  78. static char *ieee80211_regdom = "US";
  79. module_param(ieee80211_regdom, charp, 0444);
  80. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  81. /*
  82. * We assume 40 MHz bandwidth for the old regulatory work.
  83. * We make emphasis we are using the exact same frequencies
  84. * as before
  85. */
  86. static const struct ieee80211_regdomain us_regdom = {
  87. .n_reg_rules = 6,
  88. .alpha2 = "US",
  89. .reg_rules = {
  90. /* IEEE 802.11b/g, channels 1..11 */
  91. REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
  92. /* IEEE 802.11a, channel 36 */
  93. REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
  94. /* IEEE 802.11a, channel 40 */
  95. REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
  96. /* IEEE 802.11a, channel 44 */
  97. REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
  98. /* IEEE 802.11a, channels 48..64 */
  99. REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
  100. /* IEEE 802.11a, channels 149..165, outdoor */
  101. REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
  102. }
  103. };
  104. static const struct ieee80211_regdomain jp_regdom = {
  105. .n_reg_rules = 3,
  106. .alpha2 = "JP",
  107. .reg_rules = {
  108. /* IEEE 802.11b/g, channels 1..14 */
  109. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  110. /* IEEE 802.11a, channels 34..48 */
  111. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  112. NL80211_RRF_PASSIVE_SCAN),
  113. /* IEEE 802.11a, channels 52..64 */
  114. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  115. NL80211_RRF_NO_IBSS |
  116. NL80211_RRF_DFS),
  117. }
  118. };
  119. static const struct ieee80211_regdomain eu_regdom = {
  120. .n_reg_rules = 6,
  121. /*
  122. * This alpha2 is bogus, we leave it here just for stupid
  123. * backward compatibility
  124. */
  125. .alpha2 = "EU",
  126. .reg_rules = {
  127. /* IEEE 802.11b/g, channels 1..13 */
  128. REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
  129. /* IEEE 802.11a, channel 36 */
  130. REG_RULE(5180-10, 5180+10, 40, 6, 23,
  131. NL80211_RRF_PASSIVE_SCAN),
  132. /* IEEE 802.11a, channel 40 */
  133. REG_RULE(5200-10, 5200+10, 40, 6, 23,
  134. NL80211_RRF_PASSIVE_SCAN),
  135. /* IEEE 802.11a, channel 44 */
  136. REG_RULE(5220-10, 5220+10, 40, 6, 23,
  137. NL80211_RRF_PASSIVE_SCAN),
  138. /* IEEE 802.11a, channels 48..64 */
  139. REG_RULE(5240-10, 5320+10, 40, 6, 20,
  140. NL80211_RRF_NO_IBSS |
  141. NL80211_RRF_DFS),
  142. /* IEEE 802.11a, channels 100..140 */
  143. REG_RULE(5500-10, 5700+10, 40, 6, 30,
  144. NL80211_RRF_NO_IBSS |
  145. NL80211_RRF_DFS),
  146. }
  147. };
  148. static const struct ieee80211_regdomain *static_regdom(char *alpha2)
  149. {
  150. if (alpha2[0] == 'U' && alpha2[1] == 'S')
  151. return &us_regdom;
  152. if (alpha2[0] == 'J' && alpha2[1] == 'P')
  153. return &jp_regdom;
  154. if (alpha2[0] == 'E' && alpha2[1] == 'U')
  155. return &eu_regdom;
  156. /* Default, as per the old rules */
  157. return &us_regdom;
  158. }
  159. static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  160. {
  161. if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
  162. return true;
  163. return false;
  164. }
  165. #else
  166. static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  167. {
  168. return false;
  169. }
  170. #endif
  171. static void reset_regdomains(void)
  172. {
  173. /* avoid freeing static information or freeing something twice */
  174. if (cfg80211_regdomain == cfg80211_world_regdom)
  175. cfg80211_regdomain = NULL;
  176. if (cfg80211_world_regdom == &world_regdom)
  177. cfg80211_world_regdom = NULL;
  178. if (cfg80211_regdomain == &world_regdom)
  179. cfg80211_regdomain = NULL;
  180. if (is_old_static_regdom(cfg80211_regdomain))
  181. cfg80211_regdomain = NULL;
  182. kfree(cfg80211_regdomain);
  183. kfree(cfg80211_world_regdom);
  184. cfg80211_world_regdom = &world_regdom;
  185. cfg80211_regdomain = NULL;
  186. }
  187. /*
  188. * Dynamic world regulatory domain requested by the wireless
  189. * core upon initialization
  190. */
  191. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  192. {
  193. BUG_ON(!last_request);
  194. reset_regdomains();
  195. cfg80211_world_regdom = rd;
  196. cfg80211_regdomain = rd;
  197. }
  198. bool is_world_regdom(const char *alpha2)
  199. {
  200. if (!alpha2)
  201. return false;
  202. if (alpha2[0] == '0' && alpha2[1] == '0')
  203. return true;
  204. return false;
  205. }
  206. static bool is_alpha2_set(const char *alpha2)
  207. {
  208. if (!alpha2)
  209. return false;
  210. if (alpha2[0] != 0 && alpha2[1] != 0)
  211. return true;
  212. return false;
  213. }
  214. static bool is_alpha_upper(char letter)
  215. {
  216. /* ASCII A - Z */
  217. if (letter >= 65 && letter <= 90)
  218. return true;
  219. return false;
  220. }
  221. static bool is_unknown_alpha2(const char *alpha2)
  222. {
  223. if (!alpha2)
  224. return false;
  225. /*
  226. * Special case where regulatory domain was built by driver
  227. * but a specific alpha2 cannot be determined
  228. */
  229. if (alpha2[0] == '9' && alpha2[1] == '9')
  230. return true;
  231. return false;
  232. }
  233. static bool is_intersected_alpha2(const char *alpha2)
  234. {
  235. if (!alpha2)
  236. return false;
  237. /*
  238. * Special case where regulatory domain is the
  239. * result of an intersection between two regulatory domain
  240. * structures
  241. */
  242. if (alpha2[0] == '9' && alpha2[1] == '8')
  243. return true;
  244. return false;
  245. }
  246. static bool is_an_alpha2(const char *alpha2)
  247. {
  248. if (!alpha2)
  249. return false;
  250. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  251. return true;
  252. return false;
  253. }
  254. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  255. {
  256. if (!alpha2_x || !alpha2_y)
  257. return false;
  258. if (alpha2_x[0] == alpha2_y[0] &&
  259. alpha2_x[1] == alpha2_y[1])
  260. return true;
  261. return false;
  262. }
  263. static bool regdom_changes(const char *alpha2)
  264. {
  265. assert_cfg80211_lock();
  266. if (!cfg80211_regdomain)
  267. return true;
  268. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  269. return false;
  270. return true;
  271. }
  272. /**
  273. * country_ie_integrity_changes - tells us if the country IE has changed
  274. * @checksum: checksum of country IE of fields we are interested in
  275. *
  276. * If the country IE has not changed you can ignore it safely. This is
  277. * useful to determine if two devices are seeing two different country IEs
  278. * even on the same alpha2. Note that this will return false if no IE has
  279. * been set on the wireless core yet.
  280. */
  281. static bool country_ie_integrity_changes(u32 checksum)
  282. {
  283. /* If no IE has been set then the checksum doesn't change */
  284. if (unlikely(!last_request->country_ie_checksum))
  285. return false;
  286. if (unlikely(last_request->country_ie_checksum != checksum))
  287. return true;
  288. return false;
  289. }
  290. /*
  291. * This lets us keep regulatory code which is updated on a regulatory
  292. * basis in userspace.
  293. */
  294. static int call_crda(const char *alpha2)
  295. {
  296. char country_env[9 + 2] = "COUNTRY=";
  297. char *envp[] = {
  298. country_env,
  299. NULL
  300. };
  301. if (!is_world_regdom((char *) alpha2))
  302. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  303. alpha2[0], alpha2[1]);
  304. else
  305. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  306. "regulatory domain\n");
  307. country_env[8] = alpha2[0];
  308. country_env[9] = alpha2[1];
  309. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  310. }
  311. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  312. bool reg_is_valid_request(const char *alpha2)
  313. {
  314. if (!last_request)
  315. return false;
  316. return alpha2_equal(last_request->alpha2, alpha2);
  317. }
  318. /* Sanity check on a regulatory rule */
  319. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  320. {
  321. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  322. u32 freq_diff;
  323. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  324. return false;
  325. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  326. return false;
  327. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  328. if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
  329. return false;
  330. return true;
  331. }
  332. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  333. {
  334. const struct ieee80211_reg_rule *reg_rule = NULL;
  335. unsigned int i;
  336. if (!rd->n_reg_rules)
  337. return false;
  338. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  339. return false;
  340. for (i = 0; i < rd->n_reg_rules; i++) {
  341. reg_rule = &rd->reg_rules[i];
  342. if (!is_valid_reg_rule(reg_rule))
  343. return false;
  344. }
  345. return true;
  346. }
  347. /* Returns value in KHz */
  348. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  349. u32 freq)
  350. {
  351. unsigned int i;
  352. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  353. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  354. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  355. if (start_freq_khz >= freq_range->start_freq_khz &&
  356. end_freq_khz <= freq_range->end_freq_khz)
  357. return supported_bandwidths[i];
  358. }
  359. return 0;
  360. }
  361. /**
  362. * freq_in_rule_band - tells us if a frequency is in a frequency band
  363. * @freq_range: frequency rule we want to query
  364. * @freq_khz: frequency we are inquiring about
  365. *
  366. * This lets us know if a specific frequency rule is or is not relevant to
  367. * a specific frequency's band. Bands are device specific and artificial
  368. * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
  369. * safe for now to assume that a frequency rule should not be part of a
  370. * frequency's band if the start freq or end freq are off by more than 2 GHz.
  371. * This resolution can be lowered and should be considered as we add
  372. * regulatory rule support for other "bands".
  373. **/
  374. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  375. u32 freq_khz)
  376. {
  377. #define ONE_GHZ_IN_KHZ 1000000
  378. if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  379. return true;
  380. if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  381. return true;
  382. return false;
  383. #undef ONE_GHZ_IN_KHZ
  384. }
  385. /*
  386. * Converts a country IE to a regulatory domain. A regulatory domain
  387. * structure has a lot of information which the IE doesn't yet have,
  388. * so for the other values we use upper max values as we will intersect
  389. * with our userspace regulatory agent to get lower bounds.
  390. */
  391. static struct ieee80211_regdomain *country_ie_2_rd(
  392. u8 *country_ie,
  393. u8 country_ie_len,
  394. u32 *checksum)
  395. {
  396. struct ieee80211_regdomain *rd = NULL;
  397. unsigned int i = 0;
  398. char alpha2[2];
  399. u32 flags = 0;
  400. u32 num_rules = 0, size_of_regd = 0;
  401. u8 *triplets_start = NULL;
  402. u8 len_at_triplet = 0;
  403. /* the last channel we have registered in a subband (triplet) */
  404. int last_sub_max_channel = 0;
  405. *checksum = 0xDEADBEEF;
  406. /* Country IE requirements */
  407. BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
  408. country_ie_len & 0x01);
  409. alpha2[0] = country_ie[0];
  410. alpha2[1] = country_ie[1];
  411. /*
  412. * Third octet can be:
  413. * 'I' - Indoor
  414. * 'O' - Outdoor
  415. *
  416. * anything else we assume is no restrictions
  417. */
  418. if (country_ie[2] == 'I')
  419. flags = NL80211_RRF_NO_OUTDOOR;
  420. else if (country_ie[2] == 'O')
  421. flags = NL80211_RRF_NO_INDOOR;
  422. country_ie += 3;
  423. country_ie_len -= 3;
  424. triplets_start = country_ie;
  425. len_at_triplet = country_ie_len;
  426. *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
  427. /*
  428. * We need to build a reg rule for each triplet, but first we must
  429. * calculate the number of reg rules we will need. We will need one
  430. * for each channel subband
  431. */
  432. while (country_ie_len >= 3) {
  433. int end_channel = 0;
  434. struct ieee80211_country_ie_triplet *triplet =
  435. (struct ieee80211_country_ie_triplet *) country_ie;
  436. int cur_sub_max_channel = 0, cur_channel = 0;
  437. if (triplet->ext.reg_extension_id >=
  438. IEEE80211_COUNTRY_EXTENSION_ID) {
  439. country_ie += 3;
  440. country_ie_len -= 3;
  441. continue;
  442. }
  443. /* 2 GHz */
  444. if (triplet->chans.first_channel <= 14)
  445. end_channel = triplet->chans.first_channel +
  446. triplet->chans.num_channels;
  447. else
  448. /*
  449. * 5 GHz -- For example in country IEs if the first
  450. * channel given is 36 and the number of channels is 4
  451. * then the individual channel numbers defined for the
  452. * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
  453. * and not 36, 37, 38, 39.
  454. *
  455. * See: http://tinyurl.com/11d-clarification
  456. */
  457. end_channel = triplet->chans.first_channel +
  458. (4 * (triplet->chans.num_channels - 1));
  459. cur_channel = triplet->chans.first_channel;
  460. cur_sub_max_channel = end_channel;
  461. /* Basic sanity check */
  462. if (cur_sub_max_channel < cur_channel)
  463. return NULL;
  464. /*
  465. * Do not allow overlapping channels. Also channels
  466. * passed in each subband must be monotonically
  467. * increasing
  468. */
  469. if (last_sub_max_channel) {
  470. if (cur_channel <= last_sub_max_channel)
  471. return NULL;
  472. if (cur_sub_max_channel <= last_sub_max_channel)
  473. return NULL;
  474. }
  475. /*
  476. * When dot11RegulatoryClassesRequired is supported
  477. * we can throw ext triplets as part of this soup,
  478. * for now we don't care when those change as we
  479. * don't support them
  480. */
  481. *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
  482. ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
  483. ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
  484. last_sub_max_channel = cur_sub_max_channel;
  485. country_ie += 3;
  486. country_ie_len -= 3;
  487. num_rules++;
  488. /*
  489. * Note: this is not a IEEE requirement but
  490. * simply a memory requirement
  491. */
  492. if (num_rules > NL80211_MAX_SUPP_REG_RULES)
  493. return NULL;
  494. }
  495. country_ie = triplets_start;
  496. country_ie_len = len_at_triplet;
  497. size_of_regd = sizeof(struct ieee80211_regdomain) +
  498. (num_rules * sizeof(struct ieee80211_reg_rule));
  499. rd = kzalloc(size_of_regd, GFP_KERNEL);
  500. if (!rd)
  501. return NULL;
  502. rd->n_reg_rules = num_rules;
  503. rd->alpha2[0] = alpha2[0];
  504. rd->alpha2[1] = alpha2[1];
  505. /* This time around we fill in the rd */
  506. while (country_ie_len >= 3) {
  507. int end_channel = 0;
  508. struct ieee80211_country_ie_triplet *triplet =
  509. (struct ieee80211_country_ie_triplet *) country_ie;
  510. struct ieee80211_reg_rule *reg_rule = NULL;
  511. struct ieee80211_freq_range *freq_range = NULL;
  512. struct ieee80211_power_rule *power_rule = NULL;
  513. /*
  514. * Must parse if dot11RegulatoryClassesRequired is true,
  515. * we don't support this yet
  516. */
  517. if (triplet->ext.reg_extension_id >=
  518. IEEE80211_COUNTRY_EXTENSION_ID) {
  519. country_ie += 3;
  520. country_ie_len -= 3;
  521. continue;
  522. }
  523. reg_rule = &rd->reg_rules[i];
  524. freq_range = &reg_rule->freq_range;
  525. power_rule = &reg_rule->power_rule;
  526. reg_rule->flags = flags;
  527. /* 2 GHz */
  528. if (triplet->chans.first_channel <= 14)
  529. end_channel = triplet->chans.first_channel +
  530. triplet->chans.num_channels;
  531. else
  532. end_channel = triplet->chans.first_channel +
  533. (4 * (triplet->chans.num_channels - 1));
  534. /*
  535. * The +10 is since the regulatory domain expects
  536. * the actual band edge, not the center of freq for
  537. * its start and end freqs, assuming 20 MHz bandwidth on
  538. * the channels passed
  539. */
  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. /*
  547. * These are large arbitrary values we use to intersect later.
  548. * Increment this if we ever support >= 40 MHz channels
  549. * in IEEE 802.11
  550. */
  551. freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
  552. power_rule->max_antenna_gain = DBI_TO_MBI(100);
  553. power_rule->max_eirp = DBM_TO_MBM(100);
  554. country_ie += 3;
  555. country_ie_len -= 3;
  556. i++;
  557. BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
  558. }
  559. return rd;
  560. }
  561. /*
  562. * Helper for regdom_intersect(), this does the real
  563. * mathematical intersection fun
  564. */
  565. static int reg_rules_intersect(
  566. const struct ieee80211_reg_rule *rule1,
  567. const struct ieee80211_reg_rule *rule2,
  568. struct ieee80211_reg_rule *intersected_rule)
  569. {
  570. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  571. struct ieee80211_freq_range *freq_range;
  572. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  573. struct ieee80211_power_rule *power_rule;
  574. u32 freq_diff;
  575. freq_range1 = &rule1->freq_range;
  576. freq_range2 = &rule2->freq_range;
  577. freq_range = &intersected_rule->freq_range;
  578. power_rule1 = &rule1->power_rule;
  579. power_rule2 = &rule2->power_rule;
  580. power_rule = &intersected_rule->power_rule;
  581. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  582. freq_range2->start_freq_khz);
  583. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  584. freq_range2->end_freq_khz);
  585. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  586. freq_range2->max_bandwidth_khz);
  587. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  588. if (freq_range->max_bandwidth_khz > freq_diff)
  589. freq_range->max_bandwidth_khz = freq_diff;
  590. power_rule->max_eirp = min(power_rule1->max_eirp,
  591. power_rule2->max_eirp);
  592. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  593. power_rule2->max_antenna_gain);
  594. intersected_rule->flags = (rule1->flags | rule2->flags);
  595. if (!is_valid_reg_rule(intersected_rule))
  596. return -EINVAL;
  597. return 0;
  598. }
  599. /**
  600. * regdom_intersect - do the intersection between two regulatory domains
  601. * @rd1: first regulatory domain
  602. * @rd2: second regulatory domain
  603. *
  604. * Use this function to get the intersection between two regulatory domains.
  605. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  606. * as no one single alpha2 can represent this regulatory domain.
  607. *
  608. * Returns a pointer to the regulatory domain structure which will hold the
  609. * resulting intersection of rules between rd1 and rd2. We will
  610. * kzalloc() this structure for you.
  611. */
  612. static struct ieee80211_regdomain *regdom_intersect(
  613. const struct ieee80211_regdomain *rd1,
  614. const struct ieee80211_regdomain *rd2)
  615. {
  616. int r, size_of_regd;
  617. unsigned int x, y;
  618. unsigned int num_rules = 0, rule_idx = 0;
  619. const struct ieee80211_reg_rule *rule1, *rule2;
  620. struct ieee80211_reg_rule *intersected_rule;
  621. struct ieee80211_regdomain *rd;
  622. /* This is just a dummy holder to help us count */
  623. struct ieee80211_reg_rule irule;
  624. /* Uses the stack temporarily for counter arithmetic */
  625. intersected_rule = &irule;
  626. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  627. if (!rd1 || !rd2)
  628. return NULL;
  629. /*
  630. * First we get a count of the rules we'll need, then we actually
  631. * build them. This is to so we can malloc() and free() a
  632. * regdomain once. The reason we use reg_rules_intersect() here
  633. * is it will return -EINVAL if the rule computed makes no sense.
  634. * All rules that do check out OK are valid.
  635. */
  636. for (x = 0; x < rd1->n_reg_rules; x++) {
  637. rule1 = &rd1->reg_rules[x];
  638. for (y = 0; y < rd2->n_reg_rules; y++) {
  639. rule2 = &rd2->reg_rules[y];
  640. if (!reg_rules_intersect(rule1, rule2,
  641. intersected_rule))
  642. num_rules++;
  643. memset(intersected_rule, 0,
  644. sizeof(struct ieee80211_reg_rule));
  645. }
  646. }
  647. if (!num_rules)
  648. return NULL;
  649. size_of_regd = sizeof(struct ieee80211_regdomain) +
  650. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  651. rd = kzalloc(size_of_regd, GFP_KERNEL);
  652. if (!rd)
  653. return NULL;
  654. for (x = 0; x < rd1->n_reg_rules; x++) {
  655. rule1 = &rd1->reg_rules[x];
  656. for (y = 0; y < rd2->n_reg_rules; y++) {
  657. rule2 = &rd2->reg_rules[y];
  658. /*
  659. * This time around instead of using the stack lets
  660. * write to the target rule directly saving ourselves
  661. * a memcpy()
  662. */
  663. intersected_rule = &rd->reg_rules[rule_idx];
  664. r = reg_rules_intersect(rule1, rule2,
  665. intersected_rule);
  666. /*
  667. * No need to memset here the intersected rule here as
  668. * we're not using the stack anymore
  669. */
  670. if (r)
  671. continue;
  672. rule_idx++;
  673. }
  674. }
  675. if (rule_idx != num_rules) {
  676. kfree(rd);
  677. return NULL;
  678. }
  679. rd->n_reg_rules = num_rules;
  680. rd->alpha2[0] = '9';
  681. rd->alpha2[1] = '8';
  682. return rd;
  683. }
  684. /*
  685. * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  686. * want to just have the channel structure use these
  687. */
  688. static u32 map_regdom_flags(u32 rd_flags)
  689. {
  690. u32 channel_flags = 0;
  691. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  692. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  693. if (rd_flags & NL80211_RRF_NO_IBSS)
  694. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  695. if (rd_flags & NL80211_RRF_DFS)
  696. channel_flags |= IEEE80211_CHAN_RADAR;
  697. return channel_flags;
  698. }
  699. static int freq_reg_info_regd(struct wiphy *wiphy,
  700. u32 center_freq,
  701. u32 *bandwidth,
  702. const struct ieee80211_reg_rule **reg_rule,
  703. const struct ieee80211_regdomain *custom_regd)
  704. {
  705. int i;
  706. bool band_rule_found = false;
  707. const struct ieee80211_regdomain *regd;
  708. u32 max_bandwidth = 0;
  709. regd = custom_regd ? custom_regd : cfg80211_regdomain;
  710. /*
  711. * Follow the driver's regulatory domain, if present, unless a country
  712. * IE has been processed or a user wants to help complaince further
  713. */
  714. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
  715. last_request->initiator != REGDOM_SET_BY_USER &&
  716. wiphy->regd)
  717. regd = wiphy->regd;
  718. if (!regd)
  719. return -EINVAL;
  720. for (i = 0; i < regd->n_reg_rules; i++) {
  721. const struct ieee80211_reg_rule *rr;
  722. const struct ieee80211_freq_range *fr = NULL;
  723. const struct ieee80211_power_rule *pr = NULL;
  724. rr = &regd->reg_rules[i];
  725. fr = &rr->freq_range;
  726. pr = &rr->power_rule;
  727. /*
  728. * We only need to know if one frequency rule was
  729. * was in center_freq's band, that's enough, so lets
  730. * not overwrite it once found
  731. */
  732. if (!band_rule_found)
  733. band_rule_found = freq_in_rule_band(fr, center_freq);
  734. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  735. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  736. *reg_rule = rr;
  737. *bandwidth = max_bandwidth;
  738. break;
  739. }
  740. }
  741. if (!band_rule_found)
  742. return -ERANGE;
  743. return !max_bandwidth;
  744. }
  745. EXPORT_SYMBOL(freq_reg_info);
  746. int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
  747. const struct ieee80211_reg_rule **reg_rule)
  748. {
  749. return freq_reg_info_regd(wiphy, center_freq,
  750. bandwidth, reg_rule, NULL);
  751. }
  752. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  753. unsigned int chan_idx)
  754. {
  755. int r;
  756. u32 flags;
  757. u32 max_bandwidth = 0;
  758. const struct ieee80211_reg_rule *reg_rule = NULL;
  759. const struct ieee80211_power_rule *power_rule = NULL;
  760. struct ieee80211_supported_band *sband;
  761. struct ieee80211_channel *chan;
  762. struct wiphy *request_wiphy = NULL;
  763. assert_cfg80211_lock();
  764. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  765. sband = wiphy->bands[band];
  766. BUG_ON(chan_idx >= sband->n_channels);
  767. chan = &sband->channels[chan_idx];
  768. flags = chan->orig_flags;
  769. r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
  770. &max_bandwidth, &reg_rule);
  771. if (r) {
  772. /*
  773. * This means no regulatory rule was found in the country IE
  774. * with a frequency range on the center_freq's band, since
  775. * IEEE-802.11 allows for a country IE to have a subset of the
  776. * regulatory information provided in a country we ignore
  777. * disabling the channel unless at least one reg rule was
  778. * found on the center_freq's band. For details see this
  779. * clarification:
  780. *
  781. * http://tinyurl.com/11d-clarification
  782. */
  783. if (r == -ERANGE &&
  784. last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  785. #ifdef CONFIG_CFG80211_REG_DEBUG
  786. printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
  787. "intact on %s - no rule found in band on "
  788. "Country IE\n",
  789. chan->center_freq, wiphy_name(wiphy));
  790. #endif
  791. } else {
  792. /*
  793. * In this case we know the country IE has at least one reg rule
  794. * for the band so we respect its band definitions
  795. */
  796. #ifdef CONFIG_CFG80211_REG_DEBUG
  797. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  798. printk(KERN_DEBUG "cfg80211: Disabling "
  799. "channel %d MHz on %s due to "
  800. "Country IE\n",
  801. chan->center_freq, wiphy_name(wiphy));
  802. #endif
  803. flags |= IEEE80211_CHAN_DISABLED;
  804. chan->flags = flags;
  805. }
  806. return;
  807. }
  808. power_rule = &reg_rule->power_rule;
  809. if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
  810. request_wiphy && request_wiphy == wiphy &&
  811. request_wiphy->strict_regulatory) {
  812. /*
  813. * This gaurantees the driver's requested regulatory domain
  814. * will always be used as a base for further regulatory
  815. * settings
  816. */
  817. chan->flags = chan->orig_flags =
  818. map_regdom_flags(reg_rule->flags);
  819. chan->max_antenna_gain = chan->orig_mag =
  820. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  821. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  822. chan->max_power = chan->orig_mpwr =
  823. (int) MBM_TO_DBM(power_rule->max_eirp);
  824. return;
  825. }
  826. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  827. chan->max_antenna_gain = min(chan->orig_mag,
  828. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  829. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  830. if (chan->orig_mpwr)
  831. chan->max_power = min(chan->orig_mpwr,
  832. (int) MBM_TO_DBM(power_rule->max_eirp));
  833. else
  834. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  835. }
  836. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  837. {
  838. unsigned int i;
  839. struct ieee80211_supported_band *sband;
  840. BUG_ON(!wiphy->bands[band]);
  841. sband = wiphy->bands[band];
  842. for (i = 0; i < sband->n_channels; i++)
  843. handle_channel(wiphy, band, i);
  844. }
  845. static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
  846. {
  847. if (!last_request)
  848. return true;
  849. if (setby == REGDOM_SET_BY_CORE &&
  850. wiphy->custom_regulatory)
  851. return true;
  852. /*
  853. * wiphy->regd will be set once the device has its own
  854. * desired regulatory domain set
  855. */
  856. if (wiphy->strict_regulatory && !wiphy->regd &&
  857. !is_world_regdom(last_request->alpha2))
  858. return true;
  859. return false;
  860. }
  861. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  862. {
  863. struct cfg80211_registered_device *drv;
  864. list_for_each_entry(drv, &cfg80211_drv_list, list)
  865. wiphy_update_regulatory(&drv->wiphy, setby);
  866. }
  867. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  868. {
  869. enum ieee80211_band band;
  870. if (ignore_reg_update(wiphy, setby))
  871. return;
  872. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  873. if (wiphy->bands[band])
  874. handle_band(wiphy, band);
  875. }
  876. if (wiphy->reg_notifier)
  877. wiphy->reg_notifier(wiphy, last_request);
  878. }
  879. static void handle_channel_custom(struct wiphy *wiphy,
  880. enum ieee80211_band band,
  881. unsigned int chan_idx,
  882. const struct ieee80211_regdomain *regd)
  883. {
  884. int r;
  885. u32 max_bandwidth = 0;
  886. const struct ieee80211_reg_rule *reg_rule = NULL;
  887. const struct ieee80211_power_rule *power_rule = NULL;
  888. struct ieee80211_supported_band *sband;
  889. struct ieee80211_channel *chan;
  890. sband = wiphy->bands[band];
  891. BUG_ON(chan_idx >= sband->n_channels);
  892. chan = &sband->channels[chan_idx];
  893. r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  894. &max_bandwidth, &reg_rule, regd);
  895. if (r) {
  896. chan->flags = IEEE80211_CHAN_DISABLED;
  897. return;
  898. }
  899. power_rule = &reg_rule->power_rule;
  900. chan->flags |= map_regdom_flags(reg_rule->flags);
  901. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  902. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  903. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  904. }
  905. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  906. const struct ieee80211_regdomain *regd)
  907. {
  908. unsigned int i;
  909. struct ieee80211_supported_band *sband;
  910. BUG_ON(!wiphy->bands[band]);
  911. sband = wiphy->bands[band];
  912. for (i = 0; i < sband->n_channels; i++)
  913. handle_channel_custom(wiphy, band, i, regd);
  914. }
  915. /* Used by drivers prior to wiphy registration */
  916. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  917. const struct ieee80211_regdomain *regd)
  918. {
  919. enum ieee80211_band band;
  920. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  921. if (wiphy->bands[band])
  922. handle_band_custom(wiphy, band, regd);
  923. }
  924. }
  925. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  926. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  927. const struct ieee80211_regdomain *src_regd)
  928. {
  929. struct ieee80211_regdomain *regd;
  930. int size_of_regd = 0;
  931. unsigned int i;
  932. size_of_regd = sizeof(struct ieee80211_regdomain) +
  933. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  934. regd = kzalloc(size_of_regd, GFP_KERNEL);
  935. if (!regd)
  936. return -ENOMEM;
  937. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  938. for (i = 0; i < src_regd->n_reg_rules; i++)
  939. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  940. sizeof(struct ieee80211_reg_rule));
  941. *dst_regd = regd;
  942. return 0;
  943. }
  944. /*
  945. * Return value which can be used by ignore_request() to indicate
  946. * it has been determined we should intersect two regulatory domains
  947. */
  948. #define REG_INTERSECT 1
  949. /* This has the logic which determines when a new request
  950. * should be ignored. */
  951. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  952. const char *alpha2)
  953. {
  954. struct wiphy *last_wiphy = NULL;
  955. assert_cfg80211_lock();
  956. /* All initial requests are respected */
  957. if (!last_request)
  958. return 0;
  959. switch (set_by) {
  960. case REGDOM_SET_BY_INIT:
  961. return -EINVAL;
  962. case REGDOM_SET_BY_CORE:
  963. return -EINVAL;
  964. case REGDOM_SET_BY_COUNTRY_IE:
  965. last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  966. if (unlikely(!is_an_alpha2(alpha2)))
  967. return -EINVAL;
  968. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  969. if (last_wiphy != wiphy) {
  970. /*
  971. * Two cards with two APs claiming different
  972. * different Country IE alpha2s. We could
  973. * intersect them, but that seems unlikely
  974. * to be correct. Reject second one for now.
  975. */
  976. if (regdom_changes(alpha2))
  977. return -EOPNOTSUPP;
  978. return -EALREADY;
  979. }
  980. /*
  981. * Two consecutive Country IE hints on the same wiphy.
  982. * This should be picked up early by the driver/stack
  983. */
  984. if (WARN_ON(regdom_changes(alpha2)))
  985. return 0;
  986. return -EALREADY;
  987. }
  988. return REG_INTERSECT;
  989. case REGDOM_SET_BY_DRIVER:
  990. if (last_request->initiator == REGDOM_SET_BY_CORE) {
  991. if (is_old_static_regdom(cfg80211_regdomain))
  992. return 0;
  993. if (regdom_changes(alpha2))
  994. return 0;
  995. return -EALREADY;
  996. }
  997. /*
  998. * This would happen if you unplug and plug your card
  999. * back in or if you add a new device for which the previously
  1000. * loaded card also agrees on the regulatory domain.
  1001. */
  1002. if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
  1003. !regdom_changes(alpha2))
  1004. return -EALREADY;
  1005. return REG_INTERSECT;
  1006. case REGDOM_SET_BY_USER:
  1007. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  1008. return REG_INTERSECT;
  1009. /*
  1010. * If the user knows better the user should set the regdom
  1011. * to their country before the IE is picked up
  1012. */
  1013. if (last_request->initiator == REGDOM_SET_BY_USER &&
  1014. last_request->intersect)
  1015. return -EOPNOTSUPP;
  1016. /*
  1017. * Process user requests only after previous user/driver/core
  1018. * requests have been processed
  1019. */
  1020. if (last_request->initiator == REGDOM_SET_BY_CORE ||
  1021. last_request->initiator == REGDOM_SET_BY_DRIVER ||
  1022. last_request->initiator == REGDOM_SET_BY_USER) {
  1023. if (regdom_changes(last_request->alpha2))
  1024. return -EAGAIN;
  1025. }
  1026. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1027. !regdom_changes(alpha2))
  1028. return -EALREADY;
  1029. return 0;
  1030. }
  1031. return -EINVAL;
  1032. }
  1033. /* Caller must hold &cfg80211_mutex */
  1034. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  1035. const char *alpha2,
  1036. u32 country_ie_checksum,
  1037. enum environment_cap env)
  1038. {
  1039. struct regulatory_request *request;
  1040. bool intersect = false;
  1041. int r = 0;
  1042. assert_cfg80211_lock();
  1043. r = ignore_request(wiphy, set_by, alpha2);
  1044. if (r == REG_INTERSECT) {
  1045. if (set_by == REGDOM_SET_BY_DRIVER) {
  1046. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1047. if (r)
  1048. return r;
  1049. }
  1050. intersect = true;
  1051. } else if (r) {
  1052. /*
  1053. * If the regulatory domain being requested by the
  1054. * driver has already been set just copy it to the
  1055. * wiphy
  1056. */
  1057. if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
  1058. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1059. if (r)
  1060. return r;
  1061. r = -EALREADY;
  1062. goto new_request;
  1063. }
  1064. return r;
  1065. }
  1066. new_request:
  1067. request = kzalloc(sizeof(struct regulatory_request),
  1068. GFP_KERNEL);
  1069. if (!request)
  1070. return -ENOMEM;
  1071. request->alpha2[0] = alpha2[0];
  1072. request->alpha2[1] = alpha2[1];
  1073. request->initiator = set_by;
  1074. request->wiphy_idx = get_wiphy_idx(wiphy);
  1075. request->intersect = intersect;
  1076. request->country_ie_checksum = country_ie_checksum;
  1077. request->country_ie_env = env;
  1078. kfree(last_request);
  1079. last_request = request;
  1080. /* When r == REG_INTERSECT we do need to call CRDA */
  1081. if (r < 0)
  1082. return r;
  1083. /*
  1084. * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
  1085. * AND if CRDA is NOT present nothing will happen, if someone
  1086. * wants to bother with 11d with OLD_REG you can add a timer.
  1087. * If after x amount of time nothing happens you can call:
  1088. *
  1089. * return set_regdom(country_ie_regdomain);
  1090. *
  1091. * to intersect with the static rd
  1092. */
  1093. return call_crda(alpha2);
  1094. }
  1095. /* This currently only processes user and driver regulatory hints */
  1096. static int reg_process_hint(struct regulatory_request *reg_request)
  1097. {
  1098. int r = 0;
  1099. struct wiphy *wiphy = NULL;
  1100. BUG_ON(!reg_request->alpha2);
  1101. mutex_lock(&cfg80211_mutex);
  1102. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1103. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1104. if (reg_request->initiator == REGDOM_SET_BY_DRIVER &&
  1105. !wiphy) {
  1106. r = -ENODEV;
  1107. goto out;
  1108. }
  1109. r = __regulatory_hint(wiphy,
  1110. reg_request->initiator,
  1111. reg_request->alpha2,
  1112. reg_request->country_ie_checksum,
  1113. reg_request->country_ie_env);
  1114. /* This is required so that the orig_* parameters are saved */
  1115. if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
  1116. wiphy_update_regulatory(wiphy, reg_request->initiator);
  1117. out:
  1118. mutex_unlock(&cfg80211_mutex);
  1119. if (r == -EALREADY)
  1120. r = 0;
  1121. return r;
  1122. }
  1123. static void reg_process_pending_hints(void)
  1124. {
  1125. struct regulatory_request *reg_request;
  1126. int r;
  1127. spin_lock(&reg_requests_lock);
  1128. while (!list_empty(&reg_requests_list)) {
  1129. reg_request = list_first_entry(&reg_requests_list,
  1130. struct regulatory_request,
  1131. list);
  1132. list_del_init(&reg_request->list);
  1133. spin_unlock(&reg_requests_lock);
  1134. r = reg_process_hint(reg_request);
  1135. #ifdef CONFIG_CFG80211_REG_DEBUG
  1136. if (r && (reg_request->initiator == REGDOM_SET_BY_DRIVER ||
  1137. reg_request->initiator == REGDOM_SET_BY_COUNTRY_IE))
  1138. printk(KERN_ERR "cfg80211: wiphy_idx %d sent a "
  1139. "regulatory hint for %c%c but now has "
  1140. "gone fishing, ignoring request\n",
  1141. reg_request->wiphy_idx,
  1142. reg_request->alpha2[0],
  1143. reg_request->alpha2[1]);
  1144. #endif
  1145. kfree(reg_request);
  1146. spin_lock(&reg_requests_lock);
  1147. }
  1148. spin_unlock(&reg_requests_lock);
  1149. }
  1150. static void reg_todo(struct work_struct *work)
  1151. {
  1152. reg_process_pending_hints();
  1153. }
  1154. static DECLARE_WORK(reg_work, reg_todo);
  1155. static void queue_regulatory_request(struct regulatory_request *request)
  1156. {
  1157. spin_lock(&reg_requests_lock);
  1158. list_add_tail(&request->list, &reg_requests_list);
  1159. spin_unlock(&reg_requests_lock);
  1160. schedule_work(&reg_work);
  1161. }
  1162. /* Core regulatory hint -- happens once during cfg80211_init() */
  1163. static int regulatory_hint_core(const char *alpha2)
  1164. {
  1165. struct regulatory_request *request;
  1166. BUG_ON(last_request);
  1167. request = kzalloc(sizeof(struct regulatory_request),
  1168. GFP_KERNEL);
  1169. if (!request)
  1170. return -ENOMEM;
  1171. request->alpha2[0] = alpha2[0];
  1172. request->alpha2[1] = alpha2[1];
  1173. request->initiator = REGDOM_SET_BY_CORE;
  1174. queue_regulatory_request(request);
  1175. return 0;
  1176. }
  1177. /* User hints */
  1178. int regulatory_hint_user(const char *alpha2)
  1179. {
  1180. struct regulatory_request *request;
  1181. BUG_ON(!alpha2);
  1182. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1183. if (!request)
  1184. return -ENOMEM;
  1185. request->wiphy_idx = WIPHY_IDX_STALE;
  1186. request->alpha2[0] = alpha2[0];
  1187. request->alpha2[1] = alpha2[1];
  1188. request->initiator = REGDOM_SET_BY_USER,
  1189. queue_regulatory_request(request);
  1190. return 0;
  1191. }
  1192. /* Driver hints */
  1193. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1194. {
  1195. struct regulatory_request *request;
  1196. BUG_ON(!alpha2);
  1197. BUG_ON(!wiphy);
  1198. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1199. if (!request)
  1200. return -ENOMEM;
  1201. request->wiphy_idx = get_wiphy_idx(wiphy);
  1202. /* Must have registered wiphy first */
  1203. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1204. request->alpha2[0] = alpha2[0];
  1205. request->alpha2[1] = alpha2[1];
  1206. request->initiator = REGDOM_SET_BY_DRIVER;
  1207. queue_regulatory_request(request);
  1208. return 0;
  1209. }
  1210. EXPORT_SYMBOL(regulatory_hint);
  1211. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1212. u32 country_ie_checksum)
  1213. {
  1214. struct wiphy *request_wiphy;
  1215. assert_cfg80211_lock();
  1216. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1217. if (!request_wiphy)
  1218. return false;
  1219. if (likely(request_wiphy != wiphy))
  1220. return !country_ie_integrity_changes(country_ie_checksum);
  1221. /*
  1222. * We should not have let these through at this point, they
  1223. * should have been picked up earlier by the first alpha2 check
  1224. * on the device
  1225. */
  1226. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1227. return true;
  1228. return false;
  1229. }
  1230. void regulatory_hint_11d(struct wiphy *wiphy,
  1231. u8 *country_ie,
  1232. u8 country_ie_len)
  1233. {
  1234. struct ieee80211_regdomain *rd = NULL;
  1235. char alpha2[2];
  1236. u32 checksum = 0;
  1237. enum environment_cap env = ENVIRON_ANY;
  1238. struct regulatory_request *request;
  1239. mutex_lock(&cfg80211_mutex);
  1240. if (unlikely(!last_request)) {
  1241. mutex_unlock(&cfg80211_mutex);
  1242. return;
  1243. }
  1244. /* IE len must be evenly divisible by 2 */
  1245. if (country_ie_len & 0x01)
  1246. goto out;
  1247. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1248. goto out;
  1249. /*
  1250. * Pending country IE processing, this can happen after we
  1251. * call CRDA and wait for a response if a beacon was received before
  1252. * we were able to process the last regulatory_hint_11d() call
  1253. */
  1254. if (country_ie_regdomain)
  1255. goto out;
  1256. alpha2[0] = country_ie[0];
  1257. alpha2[1] = country_ie[1];
  1258. if (country_ie[2] == 'I')
  1259. env = ENVIRON_INDOOR;
  1260. else if (country_ie[2] == 'O')
  1261. env = ENVIRON_OUTDOOR;
  1262. /*
  1263. * We will run this for *every* beacon processed for the BSSID, so
  1264. * we optimize an early check to exit out early if we don't have to
  1265. * do anything
  1266. */
  1267. if (likely(wiphy_idx_valid(last_request->wiphy_idx))) {
  1268. struct cfg80211_registered_device *drv_last_ie;
  1269. drv_last_ie =
  1270. cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
  1271. /*
  1272. * Lets keep this simple -- we trust the first AP
  1273. * after we intersect with CRDA
  1274. */
  1275. if (likely(&drv_last_ie->wiphy == wiphy)) {
  1276. /*
  1277. * Ignore IEs coming in on this wiphy with
  1278. * the same alpha2 and environment cap
  1279. */
  1280. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1281. alpha2) &&
  1282. env == drv_last_ie->env)) {
  1283. goto out;
  1284. }
  1285. /*
  1286. * the wiphy moved on to another BSSID or the AP
  1287. * was reconfigured. XXX: We need to deal with the
  1288. * case where the user suspends and goes to goes
  1289. * to another country, and then gets IEs from an
  1290. * AP with different settings
  1291. */
  1292. goto out;
  1293. } else {
  1294. /*
  1295. * Ignore IEs coming in on two separate wiphys with
  1296. * the same alpha2 and environment cap
  1297. */
  1298. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1299. alpha2) &&
  1300. env == drv_last_ie->env)) {
  1301. goto out;
  1302. }
  1303. /* We could potentially intersect though */
  1304. goto out;
  1305. }
  1306. }
  1307. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1308. if (!rd)
  1309. goto out;
  1310. /*
  1311. * This will not happen right now but we leave it here for the
  1312. * the future when we want to add suspend/resume support and having
  1313. * the user move to another country after doing so, or having the user
  1314. * move to another AP. Right now we just trust the first AP.
  1315. *
  1316. * If we hit this before we add this support we want to be informed of
  1317. * it as it would indicate a mistake in the current design
  1318. */
  1319. if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
  1320. goto free_rd_out;
  1321. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1322. if (!request)
  1323. goto free_rd_out;
  1324. /*
  1325. * We keep this around for when CRDA comes back with a response so
  1326. * we can intersect with that
  1327. */
  1328. country_ie_regdomain = rd;
  1329. request->wiphy_idx = get_wiphy_idx(wiphy);
  1330. request->alpha2[0] = rd->alpha2[0];
  1331. request->alpha2[1] = rd->alpha2[1];
  1332. request->initiator = REGDOM_SET_BY_COUNTRY_IE;
  1333. request->country_ie_checksum = checksum;
  1334. request->country_ie_env = env;
  1335. mutex_unlock(&cfg80211_mutex);
  1336. queue_regulatory_request(request);
  1337. return;
  1338. free_rd_out:
  1339. kfree(rd);
  1340. out:
  1341. mutex_unlock(&cfg80211_mutex);
  1342. }
  1343. EXPORT_SYMBOL(regulatory_hint_11d);
  1344. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1345. {
  1346. unsigned int i;
  1347. const struct ieee80211_reg_rule *reg_rule = NULL;
  1348. const struct ieee80211_freq_range *freq_range = NULL;
  1349. const struct ieee80211_power_rule *power_rule = NULL;
  1350. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  1351. "(max_antenna_gain, max_eirp)\n");
  1352. for (i = 0; i < rd->n_reg_rules; i++) {
  1353. reg_rule = &rd->reg_rules[i];
  1354. freq_range = &reg_rule->freq_range;
  1355. power_rule = &reg_rule->power_rule;
  1356. /*
  1357. * There may not be documentation for max antenna gain
  1358. * in certain regions
  1359. */
  1360. if (power_rule->max_antenna_gain)
  1361. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1362. "(%d mBi, %d mBm)\n",
  1363. freq_range->start_freq_khz,
  1364. freq_range->end_freq_khz,
  1365. freq_range->max_bandwidth_khz,
  1366. power_rule->max_antenna_gain,
  1367. power_rule->max_eirp);
  1368. else
  1369. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1370. "(N/A, %d mBm)\n",
  1371. freq_range->start_freq_khz,
  1372. freq_range->end_freq_khz,
  1373. freq_range->max_bandwidth_khz,
  1374. power_rule->max_eirp);
  1375. }
  1376. }
  1377. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1378. {
  1379. if (is_intersected_alpha2(rd->alpha2)) {
  1380. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  1381. struct cfg80211_registered_device *drv;
  1382. drv = cfg80211_drv_by_wiphy_idx(
  1383. last_request->wiphy_idx);
  1384. if (drv) {
  1385. printk(KERN_INFO "cfg80211: Current regulatory "
  1386. "domain updated by AP to: %c%c\n",
  1387. drv->country_ie_alpha2[0],
  1388. drv->country_ie_alpha2[1]);
  1389. } else
  1390. printk(KERN_INFO "cfg80211: Current regulatory "
  1391. "domain intersected: \n");
  1392. } else
  1393. printk(KERN_INFO "cfg80211: Current regulatory "
  1394. "domain intersected: \n");
  1395. } else if (is_world_regdom(rd->alpha2))
  1396. printk(KERN_INFO "cfg80211: World regulatory "
  1397. "domain updated:\n");
  1398. else {
  1399. if (is_unknown_alpha2(rd->alpha2))
  1400. printk(KERN_INFO "cfg80211: Regulatory domain "
  1401. "changed to driver built-in settings "
  1402. "(unknown country)\n");
  1403. else
  1404. printk(KERN_INFO "cfg80211: Regulatory domain "
  1405. "changed to country: %c%c\n",
  1406. rd->alpha2[0], rd->alpha2[1]);
  1407. }
  1408. print_rd_rules(rd);
  1409. }
  1410. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1411. {
  1412. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1413. rd->alpha2[0], rd->alpha2[1]);
  1414. print_rd_rules(rd);
  1415. }
  1416. #ifdef CONFIG_CFG80211_REG_DEBUG
  1417. static void reg_country_ie_process_debug(
  1418. const struct ieee80211_regdomain *rd,
  1419. const struct ieee80211_regdomain *country_ie_regdomain,
  1420. const struct ieee80211_regdomain *intersected_rd)
  1421. {
  1422. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1423. print_regdomain_info(country_ie_regdomain);
  1424. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1425. print_regdomain_info(rd);
  1426. if (intersected_rd) {
  1427. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1428. "and get:\n");
  1429. print_regdomain_info(intersected_rd);
  1430. return;
  1431. }
  1432. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1433. }
  1434. #else
  1435. static inline void reg_country_ie_process_debug(
  1436. const struct ieee80211_regdomain *rd,
  1437. const struct ieee80211_regdomain *country_ie_regdomain,
  1438. const struct ieee80211_regdomain *intersected_rd)
  1439. {
  1440. }
  1441. #endif
  1442. /* Takes ownership of rd only if it doesn't fail */
  1443. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1444. {
  1445. const struct ieee80211_regdomain *intersected_rd = NULL;
  1446. struct cfg80211_registered_device *drv = NULL;
  1447. struct wiphy *request_wiphy;
  1448. /* Some basic sanity checks first */
  1449. if (is_world_regdom(rd->alpha2)) {
  1450. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1451. return -EINVAL;
  1452. update_world_regdomain(rd);
  1453. return 0;
  1454. }
  1455. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1456. !is_unknown_alpha2(rd->alpha2))
  1457. return -EINVAL;
  1458. if (!last_request)
  1459. return -EINVAL;
  1460. /*
  1461. * Lets only bother proceeding on the same alpha2 if the current
  1462. * rd is non static (it means CRDA was present and was used last)
  1463. * and the pending request came in from a country IE
  1464. */
  1465. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1466. /*
  1467. * If someone else asked us to change the rd lets only bother
  1468. * checking if the alpha2 changes if CRDA was already called
  1469. */
  1470. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1471. !regdom_changes(rd->alpha2))
  1472. return -EINVAL;
  1473. }
  1474. /*
  1475. * Now lets set the regulatory domain, update all driver channels
  1476. * and finally inform them of what we have done, in case they want
  1477. * to review or adjust their own settings based on their own
  1478. * internal EEPROM data
  1479. */
  1480. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1481. return -EINVAL;
  1482. if (!is_valid_rd(rd)) {
  1483. printk(KERN_ERR "cfg80211: Invalid "
  1484. "regulatory domain detected:\n");
  1485. print_regdomain_info(rd);
  1486. return -EINVAL;
  1487. }
  1488. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1489. if (!last_request->intersect) {
  1490. int r;
  1491. if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
  1492. reset_regdomains();
  1493. cfg80211_regdomain = rd;
  1494. return 0;
  1495. }
  1496. /*
  1497. * For a driver hint, lets copy the regulatory domain the
  1498. * driver wanted to the wiphy to deal with conflicts
  1499. */
  1500. BUG_ON(request_wiphy->regd);
  1501. r = reg_copy_regd(&request_wiphy->regd, rd);
  1502. if (r)
  1503. return r;
  1504. reset_regdomains();
  1505. cfg80211_regdomain = rd;
  1506. return 0;
  1507. }
  1508. /* Intersection requires a bit more work */
  1509. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1510. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1511. if (!intersected_rd)
  1512. return -EINVAL;
  1513. /*
  1514. * We can trash what CRDA provided now.
  1515. * However if a driver requested this specific regulatory
  1516. * domain we keep it for its private use
  1517. */
  1518. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  1519. request_wiphy->regd = rd;
  1520. else
  1521. kfree(rd);
  1522. rd = NULL;
  1523. reset_regdomains();
  1524. cfg80211_regdomain = intersected_rd;
  1525. return 0;
  1526. }
  1527. /*
  1528. * Country IE requests are handled a bit differently, we intersect
  1529. * the country IE rd with what CRDA believes that country should have
  1530. */
  1531. BUG_ON(!country_ie_regdomain);
  1532. if (rd != country_ie_regdomain) {
  1533. /*
  1534. * Intersect what CRDA returned and our what we
  1535. * had built from the Country IE received
  1536. */
  1537. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1538. reg_country_ie_process_debug(rd, country_ie_regdomain,
  1539. intersected_rd);
  1540. kfree(country_ie_regdomain);
  1541. country_ie_regdomain = NULL;
  1542. } else {
  1543. /*
  1544. * This would happen when CRDA was not present and
  1545. * OLD_REGULATORY was enabled. We intersect our Country
  1546. * IE rd and what was set on cfg80211 originally
  1547. */
  1548. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1549. }
  1550. if (!intersected_rd)
  1551. return -EINVAL;
  1552. drv = wiphy_to_dev(request_wiphy);
  1553. drv->country_ie_alpha2[0] = rd->alpha2[0];
  1554. drv->country_ie_alpha2[1] = rd->alpha2[1];
  1555. drv->env = last_request->country_ie_env;
  1556. BUG_ON(intersected_rd == rd);
  1557. kfree(rd);
  1558. rd = NULL;
  1559. reset_regdomains();
  1560. cfg80211_regdomain = intersected_rd;
  1561. return 0;
  1562. }
  1563. /*
  1564. * Use this call to set the current regulatory domain. Conflicts with
  1565. * multiple drivers can be ironed out later. Caller must've already
  1566. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  1567. */
  1568. int set_regdom(const struct ieee80211_regdomain *rd)
  1569. {
  1570. int r;
  1571. assert_cfg80211_lock();
  1572. /* Note that this doesn't update the wiphys, this is done below */
  1573. r = __set_regdom(rd);
  1574. if (r) {
  1575. kfree(rd);
  1576. return r;
  1577. }
  1578. /* This would make this whole thing pointless */
  1579. if (!last_request->intersect)
  1580. BUG_ON(rd != cfg80211_regdomain);
  1581. /* update all wiphys now with the new established regulatory domain */
  1582. update_all_wiphy_regulatory(last_request->initiator);
  1583. print_regdomain(cfg80211_regdomain);
  1584. return r;
  1585. }
  1586. /* Caller must hold cfg80211_mutex */
  1587. void reg_device_remove(struct wiphy *wiphy)
  1588. {
  1589. struct wiphy *request_wiphy;
  1590. assert_cfg80211_lock();
  1591. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1592. kfree(wiphy->regd);
  1593. if (!last_request || !request_wiphy)
  1594. return;
  1595. if (request_wiphy != wiphy)
  1596. return;
  1597. last_request->wiphy_idx = WIPHY_IDX_STALE;
  1598. last_request->country_ie_env = ENVIRON_ANY;
  1599. }
  1600. int regulatory_init(void)
  1601. {
  1602. int err = 0;
  1603. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1604. if (IS_ERR(reg_pdev))
  1605. return PTR_ERR(reg_pdev);
  1606. spin_lock_init(&reg_requests_lock);
  1607. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  1608. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  1609. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  1610. print_regdomain_info(cfg80211_regdomain);
  1611. /*
  1612. * The old code still requests for a new regdomain and if
  1613. * you have CRDA you get it updated, otherwise you get
  1614. * stuck with the static values. We ignore "EU" code as
  1615. * that is not a valid ISO / IEC 3166 alpha2
  1616. */
  1617. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  1618. err = regulatory_hint_core(ieee80211_regdom);
  1619. #else
  1620. cfg80211_regdomain = cfg80211_world_regdom;
  1621. err = regulatory_hint_core("00");
  1622. #endif
  1623. if (err) {
  1624. if (err == -ENOMEM)
  1625. return err;
  1626. /*
  1627. * N.B. kobject_uevent_env() can fail mainly for when we're out
  1628. * memory which is handled and propagated appropriately above
  1629. * but it can also fail during a netlink_broadcast() or during
  1630. * early boot for call_usermodehelper(). For now treat these
  1631. * errors as non-fatal.
  1632. */
  1633. printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
  1634. "to call CRDA during init");
  1635. #ifdef CONFIG_CFG80211_REG_DEBUG
  1636. /* We want to find out exactly why when debugging */
  1637. WARN_ON(err);
  1638. #endif
  1639. }
  1640. return 0;
  1641. }
  1642. void regulatory_exit(void)
  1643. {
  1644. struct regulatory_request *reg_request, *tmp;
  1645. cancel_work_sync(&reg_work);
  1646. mutex_lock(&cfg80211_mutex);
  1647. reset_regdomains();
  1648. kfree(country_ie_regdomain);
  1649. country_ie_regdomain = NULL;
  1650. kfree(last_request);
  1651. platform_device_unregister(reg_pdev);
  1652. spin_lock(&reg_requests_lock);
  1653. if (!list_empty(&reg_requests_list)) {
  1654. list_for_each_entry_safe(reg_request, tmp,
  1655. &reg_requests_list, list) {
  1656. list_del(&reg_request->list);
  1657. kfree(reg_request);
  1658. }
  1659. }
  1660. spin_unlock(&reg_requests_lock);
  1661. mutex_unlock(&cfg80211_mutex);
  1662. }