reg.c 61 KB

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