reg.c 61 KB

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