reg.c 57 KB

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