reg.c 51 KB

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