reg.c 58 KB

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