reg.c 58 KB

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