reg.c 58 KB

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