reg.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840
  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-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. *
  8. * Permission to use, copy, modify, and/or distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. /**
  21. * DOC: Wireless regulatory infrastructure
  22. *
  23. * The usual implementation is for a driver to read a device EEPROM to
  24. * determine which regulatory domain it should be operating under, then
  25. * looking up the allowable channels in a driver-local table and finally
  26. * registering those channels in the wiphy structure.
  27. *
  28. * Another set of compliance enforcement is for drivers to use their
  29. * own compliance limits which can be stored on the EEPROM. The host
  30. * driver or firmware may ensure these are used.
  31. *
  32. * In addition to all this we provide an extra layer of regulatory
  33. * conformance. For drivers which do not have any regulatory
  34. * information CRDA provides the complete regulatory solution.
  35. * For others it provides a community effort on further restrictions
  36. * to enhance compliance.
  37. *
  38. * Note: When number of rules --> infinity we will not be able to
  39. * index on alpha2 any more, instead we'll probably have to
  40. * rely on some SHA1 checksum of the regdomain for example.
  41. *
  42. */
  43. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  44. #include <linux/kernel.h>
  45. #include <linux/export.h>
  46. #include <linux/slab.h>
  47. #include <linux/list.h>
  48. #include <linux/ctype.h>
  49. #include <linux/nl80211.h>
  50. #include <linux/platform_device.h>
  51. #include <linux/moduleparam.h>
  52. #include <net/cfg80211.h>
  53. #include "core.h"
  54. #include "reg.h"
  55. #include "regdb.h"
  56. #include "nl80211.h"
  57. #ifdef CONFIG_CFG80211_REG_DEBUG
  58. #define REG_DBG_PRINT(format, args...) \
  59. printk(KERN_DEBUG pr_fmt(format), ##args)
  60. #else
  61. #define REG_DBG_PRINT(args...)
  62. #endif
  63. /**
  64. * enum reg_request_treatment - regulatory request treatment
  65. *
  66. * @REG_REQ_OK: continue processing the regulatory request
  67. * @REG_REQ_IGNORE: ignore the regulatory request
  68. * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
  69. * be intersected with the current one.
  70. * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
  71. * regulatory settings, and no further processing is required.
  72. * @REG_REQ_USER_HINT_HANDLED: a non alpha2 user hint was handled and no
  73. * further processing is required, i.e., not need to update last_request
  74. * etc. This should be used for user hints that do not provide an alpha2
  75. * but some other type of regulatory hint, i.e., indoor operation.
  76. */
  77. enum reg_request_treatment {
  78. REG_REQ_OK,
  79. REG_REQ_IGNORE,
  80. REG_REQ_INTERSECT,
  81. REG_REQ_ALREADY_SET,
  82. REG_REQ_USER_HINT_HANDLED,
  83. };
  84. static struct regulatory_request core_request_world = {
  85. .initiator = NL80211_REGDOM_SET_BY_CORE,
  86. .alpha2[0] = '0',
  87. .alpha2[1] = '0',
  88. .intersect = false,
  89. .processed = true,
  90. .country_ie_env = ENVIRON_ANY,
  91. };
  92. /*
  93. * Receipt of information from last regulatory request,
  94. * protected by RTNL (and can be accessed with RCU protection)
  95. */
  96. static struct regulatory_request __rcu *last_request =
  97. (void __rcu *)&core_request_world;
  98. /* To trigger userspace events */
  99. static struct platform_device *reg_pdev;
  100. /*
  101. * Central wireless core regulatory domains, we only need two,
  102. * the current one and a world regulatory domain in case we have no
  103. * information to give us an alpha2.
  104. * (protected by RTNL, can be read under RCU)
  105. */
  106. const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
  107. /*
  108. * Number of devices that registered to the core
  109. * that support cellular base station regulatory hints
  110. * (protected by RTNL)
  111. */
  112. static int reg_num_devs_support_basehint;
  113. /*
  114. * State variable indicating if the platform on which the devices
  115. * are attached is operating in an indoor environment. The state variable
  116. * is relevant for all registered devices.
  117. * (protected by RTNL)
  118. */
  119. static bool reg_is_indoor;
  120. static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
  121. {
  122. return rtnl_dereference(cfg80211_regdomain);
  123. }
  124. static const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
  125. {
  126. return rtnl_dereference(wiphy->regd);
  127. }
  128. static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
  129. {
  130. switch (dfs_region) {
  131. case NL80211_DFS_UNSET:
  132. return "unset";
  133. case NL80211_DFS_FCC:
  134. return "FCC";
  135. case NL80211_DFS_ETSI:
  136. return "ETSI";
  137. case NL80211_DFS_JP:
  138. return "JP";
  139. }
  140. return "Unknown";
  141. }
  142. enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
  143. {
  144. const struct ieee80211_regdomain *regd = NULL;
  145. const struct ieee80211_regdomain *wiphy_regd = NULL;
  146. regd = get_cfg80211_regdom();
  147. if (!wiphy)
  148. goto out;
  149. wiphy_regd = get_wiphy_regdom(wiphy);
  150. if (!wiphy_regd)
  151. goto out;
  152. if (wiphy_regd->dfs_region == regd->dfs_region)
  153. goto out;
  154. REG_DBG_PRINT("%s: device specific dfs_region "
  155. "(%s) disagrees with cfg80211's "
  156. "central dfs_region (%s)\n",
  157. dev_name(&wiphy->dev),
  158. reg_dfs_region_str(wiphy_regd->dfs_region),
  159. reg_dfs_region_str(regd->dfs_region));
  160. out:
  161. return regd->dfs_region;
  162. }
  163. static void rcu_free_regdom(const struct ieee80211_regdomain *r)
  164. {
  165. if (!r)
  166. return;
  167. kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
  168. }
  169. static struct regulatory_request *get_last_request(void)
  170. {
  171. return rcu_dereference_rtnl(last_request);
  172. }
  173. /* Used to queue up regulatory hints */
  174. static LIST_HEAD(reg_requests_list);
  175. static spinlock_t reg_requests_lock;
  176. /* Used to queue up beacon hints for review */
  177. static LIST_HEAD(reg_pending_beacons);
  178. static spinlock_t reg_pending_beacons_lock;
  179. /* Used to keep track of processed beacon hints */
  180. static LIST_HEAD(reg_beacon_list);
  181. struct reg_beacon {
  182. struct list_head list;
  183. struct ieee80211_channel chan;
  184. };
  185. static void reg_todo(struct work_struct *work);
  186. static DECLARE_WORK(reg_work, reg_todo);
  187. static void reg_timeout_work(struct work_struct *work);
  188. static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
  189. /* We keep a static world regulatory domain in case of the absence of CRDA */
  190. static const struct ieee80211_regdomain world_regdom = {
  191. .n_reg_rules = 6,
  192. .alpha2 = "00",
  193. .reg_rules = {
  194. /* IEEE 802.11b/g, channels 1..11 */
  195. REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
  196. /* IEEE 802.11b/g, channels 12..13. */
  197. REG_RULE(2467-10, 2472+10, 40, 6, 20,
  198. NL80211_RRF_NO_IR),
  199. /* IEEE 802.11 channel 14 - Only JP enables
  200. * this and for 802.11b only */
  201. REG_RULE(2484-10, 2484+10, 20, 6, 20,
  202. NL80211_RRF_NO_IR |
  203. NL80211_RRF_NO_OFDM),
  204. /* IEEE 802.11a, channel 36..48 */
  205. REG_RULE(5180-10, 5240+10, 160, 6, 20,
  206. NL80211_RRF_NO_IR),
  207. /* IEEE 802.11a, channel 52..64 - DFS required */
  208. REG_RULE(5260-10, 5320+10, 160, 6, 20,
  209. NL80211_RRF_NO_IR |
  210. NL80211_RRF_DFS),
  211. /* IEEE 802.11a, channel 100..144 - DFS required */
  212. REG_RULE(5500-10, 5720+10, 160, 6, 20,
  213. NL80211_RRF_NO_IR |
  214. NL80211_RRF_DFS),
  215. /* IEEE 802.11a, channel 149..165 */
  216. REG_RULE(5745-10, 5825+10, 80, 6, 20,
  217. NL80211_RRF_NO_IR),
  218. /* IEEE 802.11ad (60gHz), channels 1..3 */
  219. REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
  220. }
  221. };
  222. /* protected by RTNL */
  223. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  224. &world_regdom;
  225. static char *ieee80211_regdom = "00";
  226. static char user_alpha2[2];
  227. module_param(ieee80211_regdom, charp, 0444);
  228. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  229. static void reg_free_request(struct regulatory_request *request)
  230. {
  231. if (request != get_last_request())
  232. kfree(request);
  233. }
  234. static void reg_free_last_request(void)
  235. {
  236. struct regulatory_request *lr = get_last_request();
  237. if (lr != &core_request_world && lr)
  238. kfree_rcu(lr, rcu_head);
  239. }
  240. static void reg_update_last_request(struct regulatory_request *request)
  241. {
  242. struct regulatory_request *lr;
  243. lr = get_last_request();
  244. if (lr == request)
  245. return;
  246. reg_free_last_request();
  247. rcu_assign_pointer(last_request, request);
  248. }
  249. static void reset_regdomains(bool full_reset,
  250. const struct ieee80211_regdomain *new_regdom)
  251. {
  252. const struct ieee80211_regdomain *r;
  253. ASSERT_RTNL();
  254. r = get_cfg80211_regdom();
  255. /* avoid freeing static information or freeing something twice */
  256. if (r == cfg80211_world_regdom)
  257. r = NULL;
  258. if (cfg80211_world_regdom == &world_regdom)
  259. cfg80211_world_regdom = NULL;
  260. if (r == &world_regdom)
  261. r = NULL;
  262. rcu_free_regdom(r);
  263. rcu_free_regdom(cfg80211_world_regdom);
  264. cfg80211_world_regdom = &world_regdom;
  265. rcu_assign_pointer(cfg80211_regdomain, new_regdom);
  266. if (!full_reset)
  267. return;
  268. reg_update_last_request(&core_request_world);
  269. }
  270. /*
  271. * Dynamic world regulatory domain requested by the wireless
  272. * core upon initialization
  273. */
  274. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  275. {
  276. struct regulatory_request *lr;
  277. lr = get_last_request();
  278. WARN_ON(!lr);
  279. reset_regdomains(false, rd);
  280. cfg80211_world_regdom = rd;
  281. }
  282. bool is_world_regdom(const char *alpha2)
  283. {
  284. if (!alpha2)
  285. return false;
  286. return alpha2[0] == '0' && alpha2[1] == '0';
  287. }
  288. static bool is_alpha2_set(const char *alpha2)
  289. {
  290. if (!alpha2)
  291. return false;
  292. return alpha2[0] && alpha2[1];
  293. }
  294. static bool is_unknown_alpha2(const char *alpha2)
  295. {
  296. if (!alpha2)
  297. return false;
  298. /*
  299. * Special case where regulatory domain was built by driver
  300. * but a specific alpha2 cannot be determined
  301. */
  302. return alpha2[0] == '9' && alpha2[1] == '9';
  303. }
  304. static bool is_intersected_alpha2(const char *alpha2)
  305. {
  306. if (!alpha2)
  307. return false;
  308. /*
  309. * Special case where regulatory domain is the
  310. * result of an intersection between two regulatory domain
  311. * structures
  312. */
  313. return alpha2[0] == '9' && alpha2[1] == '8';
  314. }
  315. static bool is_an_alpha2(const char *alpha2)
  316. {
  317. if (!alpha2)
  318. return false;
  319. return isalpha(alpha2[0]) && isalpha(alpha2[1]);
  320. }
  321. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  322. {
  323. if (!alpha2_x || !alpha2_y)
  324. return false;
  325. return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
  326. }
  327. static bool regdom_changes(const char *alpha2)
  328. {
  329. const struct ieee80211_regdomain *r = get_cfg80211_regdom();
  330. if (!r)
  331. return true;
  332. return !alpha2_equal(r->alpha2, alpha2);
  333. }
  334. /*
  335. * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
  336. * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
  337. * has ever been issued.
  338. */
  339. static bool is_user_regdom_saved(void)
  340. {
  341. if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
  342. return false;
  343. /* This would indicate a mistake on the design */
  344. if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
  345. "Unexpected user alpha2: %c%c\n",
  346. user_alpha2[0], user_alpha2[1]))
  347. return false;
  348. return true;
  349. }
  350. static const struct ieee80211_regdomain *
  351. reg_copy_regd(const struct ieee80211_regdomain *src_regd)
  352. {
  353. struct ieee80211_regdomain *regd;
  354. int size_of_regd;
  355. unsigned int i;
  356. size_of_regd =
  357. sizeof(struct ieee80211_regdomain) +
  358. src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
  359. regd = kzalloc(size_of_regd, GFP_KERNEL);
  360. if (!regd)
  361. return ERR_PTR(-ENOMEM);
  362. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  363. for (i = 0; i < src_regd->n_reg_rules; i++)
  364. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  365. sizeof(struct ieee80211_reg_rule));
  366. return regd;
  367. }
  368. #ifdef CONFIG_CFG80211_INTERNAL_REGDB
  369. struct reg_regdb_search_request {
  370. char alpha2[2];
  371. struct list_head list;
  372. };
  373. static LIST_HEAD(reg_regdb_search_list);
  374. static DEFINE_MUTEX(reg_regdb_search_mutex);
  375. static void reg_regdb_search(struct work_struct *work)
  376. {
  377. struct reg_regdb_search_request *request;
  378. const struct ieee80211_regdomain *curdom, *regdom = NULL;
  379. int i;
  380. rtnl_lock();
  381. mutex_lock(&reg_regdb_search_mutex);
  382. while (!list_empty(&reg_regdb_search_list)) {
  383. request = list_first_entry(&reg_regdb_search_list,
  384. struct reg_regdb_search_request,
  385. list);
  386. list_del(&request->list);
  387. for (i = 0; i < reg_regdb_size; i++) {
  388. curdom = reg_regdb[i];
  389. if (alpha2_equal(request->alpha2, curdom->alpha2)) {
  390. regdom = reg_copy_regd(curdom);
  391. break;
  392. }
  393. }
  394. kfree(request);
  395. }
  396. mutex_unlock(&reg_regdb_search_mutex);
  397. if (!IS_ERR_OR_NULL(regdom))
  398. set_regdom(regdom);
  399. rtnl_unlock();
  400. }
  401. static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
  402. static void reg_regdb_query(const char *alpha2)
  403. {
  404. struct reg_regdb_search_request *request;
  405. if (!alpha2)
  406. return;
  407. request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
  408. if (!request)
  409. return;
  410. memcpy(request->alpha2, alpha2, 2);
  411. mutex_lock(&reg_regdb_search_mutex);
  412. list_add_tail(&request->list, &reg_regdb_search_list);
  413. mutex_unlock(&reg_regdb_search_mutex);
  414. schedule_work(&reg_regdb_work);
  415. }
  416. /* Feel free to add any other sanity checks here */
  417. static void reg_regdb_size_check(void)
  418. {
  419. /* We should ideally BUILD_BUG_ON() but then random builds would fail */
  420. WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
  421. }
  422. #else
  423. static inline void reg_regdb_size_check(void) {}
  424. static inline void reg_regdb_query(const char *alpha2) {}
  425. #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
  426. /*
  427. * This lets us keep regulatory code which is updated on a regulatory
  428. * basis in userspace.
  429. */
  430. static int call_crda(const char *alpha2)
  431. {
  432. char country[12];
  433. char *env[] = { country, NULL };
  434. snprintf(country, sizeof(country), "COUNTRY=%c%c",
  435. alpha2[0], alpha2[1]);
  436. if (!is_world_regdom((char *) alpha2))
  437. pr_info("Calling CRDA for country: %c%c\n",
  438. alpha2[0], alpha2[1]);
  439. else
  440. pr_info("Calling CRDA to update world regulatory domain\n");
  441. /* query internal regulatory database (if it exists) */
  442. reg_regdb_query(alpha2);
  443. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
  444. }
  445. static enum reg_request_treatment
  446. reg_call_crda(struct regulatory_request *request)
  447. {
  448. if (call_crda(request->alpha2))
  449. return REG_REQ_IGNORE;
  450. return REG_REQ_OK;
  451. }
  452. bool reg_is_valid_request(const char *alpha2)
  453. {
  454. struct regulatory_request *lr = get_last_request();
  455. if (!lr || lr->processed)
  456. return false;
  457. return alpha2_equal(lr->alpha2, alpha2);
  458. }
  459. static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
  460. {
  461. struct regulatory_request *lr = get_last_request();
  462. /*
  463. * Follow the driver's regulatory domain, if present, unless a country
  464. * IE has been processed or a user wants to help complaince further
  465. */
  466. if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  467. lr->initiator != NL80211_REGDOM_SET_BY_USER &&
  468. wiphy->regd)
  469. return get_wiphy_regdom(wiphy);
  470. return get_cfg80211_regdom();
  471. }
  472. unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
  473. const struct ieee80211_reg_rule *rule)
  474. {
  475. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  476. const struct ieee80211_freq_range *freq_range_tmp;
  477. const struct ieee80211_reg_rule *tmp;
  478. u32 start_freq, end_freq, idx, no;
  479. for (idx = 0; idx < rd->n_reg_rules; idx++)
  480. if (rule == &rd->reg_rules[idx])
  481. break;
  482. if (idx == rd->n_reg_rules)
  483. return 0;
  484. /* get start_freq */
  485. no = idx;
  486. while (no) {
  487. tmp = &rd->reg_rules[--no];
  488. freq_range_tmp = &tmp->freq_range;
  489. if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
  490. break;
  491. freq_range = freq_range_tmp;
  492. }
  493. start_freq = freq_range->start_freq_khz;
  494. /* get end_freq */
  495. freq_range = &rule->freq_range;
  496. no = idx;
  497. while (no < rd->n_reg_rules - 1) {
  498. tmp = &rd->reg_rules[++no];
  499. freq_range_tmp = &tmp->freq_range;
  500. if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
  501. break;
  502. freq_range = freq_range_tmp;
  503. }
  504. end_freq = freq_range->end_freq_khz;
  505. return end_freq - start_freq;
  506. }
  507. /* Sanity check on a regulatory rule */
  508. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  509. {
  510. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  511. u32 freq_diff;
  512. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  513. return false;
  514. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  515. return false;
  516. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  517. if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
  518. freq_range->max_bandwidth_khz > freq_diff)
  519. return false;
  520. return true;
  521. }
  522. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  523. {
  524. const struct ieee80211_reg_rule *reg_rule = NULL;
  525. unsigned int i;
  526. if (!rd->n_reg_rules)
  527. return false;
  528. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  529. return false;
  530. for (i = 0; i < rd->n_reg_rules; i++) {
  531. reg_rule = &rd->reg_rules[i];
  532. if (!is_valid_reg_rule(reg_rule))
  533. return false;
  534. }
  535. return true;
  536. }
  537. static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
  538. u32 center_freq_khz, u32 bw_khz)
  539. {
  540. u32 start_freq_khz, end_freq_khz;
  541. start_freq_khz = center_freq_khz - (bw_khz/2);
  542. end_freq_khz = center_freq_khz + (bw_khz/2);
  543. if (start_freq_khz >= freq_range->start_freq_khz &&
  544. end_freq_khz <= freq_range->end_freq_khz)
  545. return true;
  546. return false;
  547. }
  548. /**
  549. * freq_in_rule_band - tells us if a frequency is in a frequency band
  550. * @freq_range: frequency rule we want to query
  551. * @freq_khz: frequency we are inquiring about
  552. *
  553. * This lets us know if a specific frequency rule is or is not relevant to
  554. * a specific frequency's band. Bands are device specific and artificial
  555. * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
  556. * however it is safe for now to assume that a frequency rule should not be
  557. * part of a frequency's band if the start freq or end freq are off by more
  558. * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
  559. * 60 GHz band.
  560. * This resolution can be lowered and should be considered as we add
  561. * regulatory rule support for other "bands".
  562. **/
  563. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  564. u32 freq_khz)
  565. {
  566. #define ONE_GHZ_IN_KHZ 1000000
  567. /*
  568. * From 802.11ad: directional multi-gigabit (DMG):
  569. * Pertaining to operation in a frequency band containing a channel
  570. * with the Channel starting frequency above 45 GHz.
  571. */
  572. u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
  573. 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
  574. if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
  575. return true;
  576. if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
  577. return true;
  578. return false;
  579. #undef ONE_GHZ_IN_KHZ
  580. }
  581. /*
  582. * Later on we can perhaps use the more restrictive DFS
  583. * region but we don't have information for that yet so
  584. * for now simply disallow conflicts.
  585. */
  586. static enum nl80211_dfs_regions
  587. reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
  588. const enum nl80211_dfs_regions dfs_region2)
  589. {
  590. if (dfs_region1 != dfs_region2)
  591. return NL80211_DFS_UNSET;
  592. return dfs_region1;
  593. }
  594. /*
  595. * Helper for regdom_intersect(), this does the real
  596. * mathematical intersection fun
  597. */
  598. static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
  599. const struct ieee80211_regdomain *rd2,
  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, max_bandwidth1, max_bandwidth2;
  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. max_bandwidth1 = freq_range1->max_bandwidth_khz;
  620. max_bandwidth2 = freq_range2->max_bandwidth_khz;
  621. if (rule1->flags & NL80211_RRF_AUTO_BW)
  622. max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
  623. if (rule2->flags & NL80211_RRF_AUTO_BW)
  624. max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
  625. freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
  626. intersected_rule->flags = rule1->flags | rule2->flags;
  627. /*
  628. * In case NL80211_RRF_AUTO_BW requested for both rules
  629. * set AUTO_BW in intersected rule also. Next we will
  630. * calculate BW correctly in handle_channel function.
  631. * In other case remove AUTO_BW flag while we calculate
  632. * maximum bandwidth correctly and auto calculation is
  633. * not required.
  634. */
  635. if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
  636. (rule2->flags & NL80211_RRF_AUTO_BW))
  637. intersected_rule->flags |= NL80211_RRF_AUTO_BW;
  638. else
  639. intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
  640. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  641. if (freq_range->max_bandwidth_khz > freq_diff)
  642. freq_range->max_bandwidth_khz = freq_diff;
  643. power_rule->max_eirp = min(power_rule1->max_eirp,
  644. power_rule2->max_eirp);
  645. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  646. power_rule2->max_antenna_gain);
  647. intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
  648. rule2->dfs_cac_ms);
  649. if (!is_valid_reg_rule(intersected_rule))
  650. return -EINVAL;
  651. return 0;
  652. }
  653. /* check whether old rule contains new rule */
  654. static bool rule_contains(struct ieee80211_reg_rule *r1,
  655. struct ieee80211_reg_rule *r2)
  656. {
  657. /* for simplicity, currently consider only same flags */
  658. if (r1->flags != r2->flags)
  659. return false;
  660. /* verify r1 is more restrictive */
  661. if ((r1->power_rule.max_antenna_gain >
  662. r2->power_rule.max_antenna_gain) ||
  663. r1->power_rule.max_eirp > r2->power_rule.max_eirp)
  664. return false;
  665. /* make sure r2's range is contained within r1 */
  666. if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
  667. r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
  668. return false;
  669. /* and finally verify that r1.max_bw >= r2.max_bw */
  670. if (r1->freq_range.max_bandwidth_khz <
  671. r2->freq_range.max_bandwidth_khz)
  672. return false;
  673. return true;
  674. }
  675. /* add or extend current rules. do nothing if rule is already contained */
  676. static void add_rule(struct ieee80211_reg_rule *rule,
  677. struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
  678. {
  679. struct ieee80211_reg_rule *tmp_rule;
  680. int i;
  681. for (i = 0; i < *n_rules; i++) {
  682. tmp_rule = &reg_rules[i];
  683. /* rule is already contained - do nothing */
  684. if (rule_contains(tmp_rule, rule))
  685. return;
  686. /* extend rule if possible */
  687. if (rule_contains(rule, tmp_rule)) {
  688. memcpy(tmp_rule, rule, sizeof(*rule));
  689. return;
  690. }
  691. }
  692. memcpy(&reg_rules[*n_rules], rule, sizeof(*rule));
  693. (*n_rules)++;
  694. }
  695. /**
  696. * regdom_intersect - do the intersection between two regulatory domains
  697. * @rd1: first regulatory domain
  698. * @rd2: second regulatory domain
  699. *
  700. * Use this function to get the intersection between two regulatory domains.
  701. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  702. * as no one single alpha2 can represent this regulatory domain.
  703. *
  704. * Returns a pointer to the regulatory domain structure which will hold the
  705. * resulting intersection of rules between rd1 and rd2. We will
  706. * kzalloc() this structure for you.
  707. */
  708. static struct ieee80211_regdomain *
  709. regdom_intersect(const struct ieee80211_regdomain *rd1,
  710. const struct ieee80211_regdomain *rd2)
  711. {
  712. int r, size_of_regd;
  713. unsigned int x, y;
  714. unsigned int num_rules = 0;
  715. const struct ieee80211_reg_rule *rule1, *rule2;
  716. struct ieee80211_reg_rule intersected_rule;
  717. struct ieee80211_regdomain *rd;
  718. if (!rd1 || !rd2)
  719. return NULL;
  720. /*
  721. * First we get a count of the rules we'll need, then we actually
  722. * build them. This is to so we can malloc() and free() a
  723. * regdomain once. The reason we use reg_rules_intersect() here
  724. * is it will return -EINVAL if the rule computed makes no sense.
  725. * All rules that do check out OK are valid.
  726. */
  727. for (x = 0; x < rd1->n_reg_rules; x++) {
  728. rule1 = &rd1->reg_rules[x];
  729. for (y = 0; y < rd2->n_reg_rules; y++) {
  730. rule2 = &rd2->reg_rules[y];
  731. if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
  732. &intersected_rule))
  733. num_rules++;
  734. }
  735. }
  736. if (!num_rules)
  737. return NULL;
  738. size_of_regd = sizeof(struct ieee80211_regdomain) +
  739. num_rules * sizeof(struct ieee80211_reg_rule);
  740. rd = kzalloc(size_of_regd, GFP_KERNEL);
  741. if (!rd)
  742. return NULL;
  743. for (x = 0; x < rd1->n_reg_rules; x++) {
  744. rule1 = &rd1->reg_rules[x];
  745. for (y = 0; y < rd2->n_reg_rules; y++) {
  746. rule2 = &rd2->reg_rules[y];
  747. r = reg_rules_intersect(rd1, rd2, rule1, rule2,
  748. &intersected_rule);
  749. /*
  750. * No need to memset here the intersected rule here as
  751. * we're not using the stack anymore
  752. */
  753. if (r)
  754. continue;
  755. add_rule(&intersected_rule, rd->reg_rules,
  756. &rd->n_reg_rules);
  757. }
  758. }
  759. rd->alpha2[0] = '9';
  760. rd->alpha2[1] = '8';
  761. rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
  762. rd2->dfs_region);
  763. return rd;
  764. }
  765. /*
  766. * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  767. * want to just have the channel structure use these
  768. */
  769. static u32 map_regdom_flags(u32 rd_flags)
  770. {
  771. u32 channel_flags = 0;
  772. if (rd_flags & NL80211_RRF_NO_IR_ALL)
  773. channel_flags |= IEEE80211_CHAN_NO_IR;
  774. if (rd_flags & NL80211_RRF_DFS)
  775. channel_flags |= IEEE80211_CHAN_RADAR;
  776. if (rd_flags & NL80211_RRF_NO_OFDM)
  777. channel_flags |= IEEE80211_CHAN_NO_OFDM;
  778. if (rd_flags & NL80211_RRF_NO_OUTDOOR)
  779. channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
  780. return channel_flags;
  781. }
  782. static const struct ieee80211_reg_rule *
  783. freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
  784. const struct ieee80211_regdomain *regd)
  785. {
  786. int i;
  787. bool band_rule_found = false;
  788. bool bw_fits = false;
  789. if (!regd)
  790. return ERR_PTR(-EINVAL);
  791. for (i = 0; i < regd->n_reg_rules; i++) {
  792. const struct ieee80211_reg_rule *rr;
  793. const struct ieee80211_freq_range *fr = NULL;
  794. rr = &regd->reg_rules[i];
  795. fr = &rr->freq_range;
  796. /*
  797. * We only need to know if one frequency rule was
  798. * was in center_freq's band, that's enough, so lets
  799. * not overwrite it once found
  800. */
  801. if (!band_rule_found)
  802. band_rule_found = freq_in_rule_band(fr, center_freq);
  803. bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
  804. if (band_rule_found && bw_fits)
  805. return rr;
  806. }
  807. if (!band_rule_found)
  808. return ERR_PTR(-ERANGE);
  809. return ERR_PTR(-EINVAL);
  810. }
  811. const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
  812. u32 center_freq)
  813. {
  814. const struct ieee80211_regdomain *regd;
  815. regd = reg_get_regdomain(wiphy);
  816. return freq_reg_info_regd(wiphy, center_freq, regd);
  817. }
  818. EXPORT_SYMBOL(freq_reg_info);
  819. const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
  820. {
  821. switch (initiator) {
  822. case NL80211_REGDOM_SET_BY_CORE:
  823. return "core";
  824. case NL80211_REGDOM_SET_BY_USER:
  825. return "user";
  826. case NL80211_REGDOM_SET_BY_DRIVER:
  827. return "driver";
  828. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  829. return "country IE";
  830. default:
  831. WARN_ON(1);
  832. return "bug";
  833. }
  834. }
  835. EXPORT_SYMBOL(reg_initiator_name);
  836. #ifdef CONFIG_CFG80211_REG_DEBUG
  837. static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
  838. struct ieee80211_channel *chan,
  839. const struct ieee80211_reg_rule *reg_rule)
  840. {
  841. const struct ieee80211_power_rule *power_rule;
  842. const struct ieee80211_freq_range *freq_range;
  843. char max_antenna_gain[32], bw[32];
  844. power_rule = &reg_rule->power_rule;
  845. freq_range = &reg_rule->freq_range;
  846. if (!power_rule->max_antenna_gain)
  847. snprintf(max_antenna_gain, sizeof(max_antenna_gain), "N/A");
  848. else
  849. snprintf(max_antenna_gain, sizeof(max_antenna_gain), "%d",
  850. power_rule->max_antenna_gain);
  851. if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  852. snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
  853. freq_range->max_bandwidth_khz,
  854. reg_get_max_bandwidth(regd, reg_rule));
  855. else
  856. snprintf(bw, sizeof(bw), "%d KHz",
  857. freq_range->max_bandwidth_khz);
  858. REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
  859. chan->center_freq);
  860. REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
  861. freq_range->start_freq_khz, freq_range->end_freq_khz,
  862. bw, max_antenna_gain,
  863. power_rule->max_eirp);
  864. }
  865. #else
  866. static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
  867. struct ieee80211_channel *chan,
  868. const struct ieee80211_reg_rule *reg_rule)
  869. {
  870. return;
  871. }
  872. #endif
  873. /*
  874. * Note that right now we assume the desired channel bandwidth
  875. * is always 20 MHz for each individual channel (HT40 uses 20 MHz
  876. * per channel, the primary and the extension channel).
  877. */
  878. static void handle_channel(struct wiphy *wiphy,
  879. enum nl80211_reg_initiator initiator,
  880. struct ieee80211_channel *chan)
  881. {
  882. u32 flags, bw_flags = 0;
  883. const struct ieee80211_reg_rule *reg_rule = NULL;
  884. const struct ieee80211_power_rule *power_rule = NULL;
  885. const struct ieee80211_freq_range *freq_range = NULL;
  886. struct wiphy *request_wiphy = NULL;
  887. struct regulatory_request *lr = get_last_request();
  888. const struct ieee80211_regdomain *regd;
  889. u32 max_bandwidth_khz;
  890. request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
  891. flags = chan->orig_flags;
  892. reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
  893. if (IS_ERR(reg_rule)) {
  894. /*
  895. * We will disable all channels that do not match our
  896. * received regulatory rule unless the hint is coming
  897. * from a Country IE and the Country IE had no information
  898. * about a band. The IEEE 802.11 spec allows for an AP
  899. * to send only a subset of the regulatory rules allowed,
  900. * so an AP in the US that only supports 2.4 GHz may only send
  901. * a country IE with information for the 2.4 GHz band
  902. * while 5 GHz is still supported.
  903. */
  904. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  905. PTR_ERR(reg_rule) == -ERANGE)
  906. return;
  907. if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  908. request_wiphy && request_wiphy == wiphy &&
  909. request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
  910. REG_DBG_PRINT("Disabling freq %d MHz for good\n",
  911. chan->center_freq);
  912. chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  913. chan->flags = chan->orig_flags;
  914. } else {
  915. REG_DBG_PRINT("Disabling freq %d MHz\n",
  916. chan->center_freq);
  917. chan->flags |= IEEE80211_CHAN_DISABLED;
  918. }
  919. return;
  920. }
  921. regd = reg_get_regdomain(wiphy);
  922. chan_reg_rule_print_dbg(regd, chan, reg_rule);
  923. power_rule = &reg_rule->power_rule;
  924. freq_range = &reg_rule->freq_range;
  925. max_bandwidth_khz = freq_range->max_bandwidth_khz;
  926. /* Check if auto calculation requested */
  927. if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  928. max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
  929. if (max_bandwidth_khz < MHZ_TO_KHZ(40))
  930. bw_flags = IEEE80211_CHAN_NO_HT40;
  931. if (max_bandwidth_khz < MHZ_TO_KHZ(80))
  932. bw_flags |= IEEE80211_CHAN_NO_80MHZ;
  933. if (max_bandwidth_khz < MHZ_TO_KHZ(160))
  934. bw_flags |= IEEE80211_CHAN_NO_160MHZ;
  935. if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  936. request_wiphy && request_wiphy == wiphy &&
  937. request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
  938. /*
  939. * This guarantees the driver's requested regulatory domain
  940. * will always be used as a base for further regulatory
  941. * settings
  942. */
  943. chan->flags = chan->orig_flags =
  944. map_regdom_flags(reg_rule->flags) | bw_flags;
  945. chan->max_antenna_gain = chan->orig_mag =
  946. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  947. chan->max_reg_power = chan->max_power = chan->orig_mpwr =
  948. (int) MBM_TO_DBM(power_rule->max_eirp);
  949. if (chan->flags & IEEE80211_CHAN_RADAR) {
  950. chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
  951. if (reg_rule->dfs_cac_ms)
  952. chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
  953. }
  954. return;
  955. }
  956. chan->dfs_state = NL80211_DFS_USABLE;
  957. chan->dfs_state_entered = jiffies;
  958. chan->beacon_found = false;
  959. chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
  960. chan->max_antenna_gain =
  961. min_t(int, chan->orig_mag,
  962. MBI_TO_DBI(power_rule->max_antenna_gain));
  963. chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  964. if (chan->flags & IEEE80211_CHAN_RADAR) {
  965. if (reg_rule->dfs_cac_ms)
  966. chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
  967. else
  968. chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
  969. }
  970. if (chan->orig_mpwr) {
  971. /*
  972. * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
  973. * will always follow the passed country IE power settings.
  974. */
  975. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  976. wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
  977. chan->max_power = chan->max_reg_power;
  978. else
  979. chan->max_power = min(chan->orig_mpwr,
  980. chan->max_reg_power);
  981. } else
  982. chan->max_power = chan->max_reg_power;
  983. }
  984. static void handle_band(struct wiphy *wiphy,
  985. enum nl80211_reg_initiator initiator,
  986. struct ieee80211_supported_band *sband)
  987. {
  988. unsigned int i;
  989. if (!sband)
  990. return;
  991. for (i = 0; i < sband->n_channels; i++)
  992. handle_channel(wiphy, initiator, &sband->channels[i]);
  993. }
  994. static bool reg_request_cell_base(struct regulatory_request *request)
  995. {
  996. if (request->initiator != NL80211_REGDOM_SET_BY_USER)
  997. return false;
  998. return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
  999. }
  1000. static bool reg_request_indoor(struct regulatory_request *request)
  1001. {
  1002. if (request->initiator != NL80211_REGDOM_SET_BY_USER)
  1003. return false;
  1004. return request->user_reg_hint_type == NL80211_USER_REG_HINT_INDOOR;
  1005. }
  1006. bool reg_last_request_cell_base(void)
  1007. {
  1008. return reg_request_cell_base(get_last_request());
  1009. }
  1010. #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
  1011. /* Core specific check */
  1012. static enum reg_request_treatment
  1013. reg_ignore_cell_hint(struct regulatory_request *pending_request)
  1014. {
  1015. struct regulatory_request *lr = get_last_request();
  1016. if (!reg_num_devs_support_basehint)
  1017. return REG_REQ_IGNORE;
  1018. if (reg_request_cell_base(lr) &&
  1019. !regdom_changes(pending_request->alpha2))
  1020. return REG_REQ_ALREADY_SET;
  1021. return REG_REQ_OK;
  1022. }
  1023. /* Device specific check */
  1024. static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
  1025. {
  1026. return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
  1027. }
  1028. #else
  1029. static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
  1030. {
  1031. return REG_REQ_IGNORE;
  1032. }
  1033. static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
  1034. {
  1035. return true;
  1036. }
  1037. #endif
  1038. static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
  1039. {
  1040. if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
  1041. !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
  1042. return true;
  1043. return false;
  1044. }
  1045. static bool ignore_reg_update(struct wiphy *wiphy,
  1046. enum nl80211_reg_initiator initiator)
  1047. {
  1048. struct regulatory_request *lr = get_last_request();
  1049. if (!lr) {
  1050. REG_DBG_PRINT("Ignoring regulatory request set by %s "
  1051. "since last_request is not set\n",
  1052. reg_initiator_name(initiator));
  1053. return true;
  1054. }
  1055. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  1056. wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
  1057. REG_DBG_PRINT("Ignoring regulatory request set by %s "
  1058. "since the driver uses its own custom "
  1059. "regulatory domain\n",
  1060. reg_initiator_name(initiator));
  1061. return true;
  1062. }
  1063. /*
  1064. * wiphy->regd will be set once the device has its own
  1065. * desired regulatory domain set
  1066. */
  1067. if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
  1068. initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1069. !is_world_regdom(lr->alpha2)) {
  1070. REG_DBG_PRINT("Ignoring regulatory request set by %s "
  1071. "since the driver requires its own regulatory "
  1072. "domain to be set first\n",
  1073. reg_initiator_name(initiator));
  1074. return true;
  1075. }
  1076. if (reg_request_cell_base(lr))
  1077. return reg_dev_ignore_cell_hint(wiphy);
  1078. return false;
  1079. }
  1080. static bool reg_is_world_roaming(struct wiphy *wiphy)
  1081. {
  1082. const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
  1083. const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
  1084. struct regulatory_request *lr = get_last_request();
  1085. if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
  1086. return true;
  1087. if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1088. wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
  1089. return true;
  1090. return false;
  1091. }
  1092. static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
  1093. struct reg_beacon *reg_beacon)
  1094. {
  1095. struct ieee80211_supported_band *sband;
  1096. struct ieee80211_channel *chan;
  1097. bool channel_changed = false;
  1098. struct ieee80211_channel chan_before;
  1099. sband = wiphy->bands[reg_beacon->chan.band];
  1100. chan = &sband->channels[chan_idx];
  1101. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  1102. return;
  1103. if (chan->beacon_found)
  1104. return;
  1105. chan->beacon_found = true;
  1106. if (!reg_is_world_roaming(wiphy))
  1107. return;
  1108. if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
  1109. return;
  1110. chan_before.center_freq = chan->center_freq;
  1111. chan_before.flags = chan->flags;
  1112. if (chan->flags & IEEE80211_CHAN_NO_IR) {
  1113. chan->flags &= ~IEEE80211_CHAN_NO_IR;
  1114. channel_changed = true;
  1115. }
  1116. if (channel_changed)
  1117. nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
  1118. }
  1119. /*
  1120. * Called when a scan on a wiphy finds a beacon on
  1121. * new channel
  1122. */
  1123. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  1124. struct reg_beacon *reg_beacon)
  1125. {
  1126. unsigned int i;
  1127. struct ieee80211_supported_band *sband;
  1128. if (!wiphy->bands[reg_beacon->chan.band])
  1129. return;
  1130. sband = wiphy->bands[reg_beacon->chan.band];
  1131. for (i = 0; i < sband->n_channels; i++)
  1132. handle_reg_beacon(wiphy, i, reg_beacon);
  1133. }
  1134. /*
  1135. * Called upon reg changes or a new wiphy is added
  1136. */
  1137. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  1138. {
  1139. unsigned int i;
  1140. struct ieee80211_supported_band *sband;
  1141. struct reg_beacon *reg_beacon;
  1142. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  1143. if (!wiphy->bands[reg_beacon->chan.band])
  1144. continue;
  1145. sband = wiphy->bands[reg_beacon->chan.band];
  1146. for (i = 0; i < sband->n_channels; i++)
  1147. handle_reg_beacon(wiphy, i, reg_beacon);
  1148. }
  1149. }
  1150. /* Reap the advantages of previously found beacons */
  1151. static void reg_process_beacons(struct wiphy *wiphy)
  1152. {
  1153. /*
  1154. * Means we are just firing up cfg80211, so no beacons would
  1155. * have been processed yet.
  1156. */
  1157. if (!last_request)
  1158. return;
  1159. wiphy_update_beacon_reg(wiphy);
  1160. }
  1161. static bool is_ht40_allowed(struct ieee80211_channel *chan)
  1162. {
  1163. if (!chan)
  1164. return false;
  1165. if (chan->flags & IEEE80211_CHAN_DISABLED)
  1166. return false;
  1167. /* This would happen when regulatory rules disallow HT40 completely */
  1168. if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
  1169. return false;
  1170. return true;
  1171. }
  1172. static void reg_process_ht_flags_channel(struct wiphy *wiphy,
  1173. struct ieee80211_channel *channel)
  1174. {
  1175. struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
  1176. struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  1177. unsigned int i;
  1178. if (!is_ht40_allowed(channel)) {
  1179. channel->flags |= IEEE80211_CHAN_NO_HT40;
  1180. return;
  1181. }
  1182. /*
  1183. * We need to ensure the extension channels exist to
  1184. * be able to use HT40- or HT40+, this finds them (or not)
  1185. */
  1186. for (i = 0; i < sband->n_channels; i++) {
  1187. struct ieee80211_channel *c = &sband->channels[i];
  1188. if (c->center_freq == (channel->center_freq - 20))
  1189. channel_before = c;
  1190. if (c->center_freq == (channel->center_freq + 20))
  1191. channel_after = c;
  1192. }
  1193. /*
  1194. * Please note that this assumes target bandwidth is 20 MHz,
  1195. * if that ever changes we also need to change the below logic
  1196. * to include that as well.
  1197. */
  1198. if (!is_ht40_allowed(channel_before))
  1199. channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
  1200. else
  1201. channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  1202. if (!is_ht40_allowed(channel_after))
  1203. channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
  1204. else
  1205. channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  1206. }
  1207. static void reg_process_ht_flags_band(struct wiphy *wiphy,
  1208. struct ieee80211_supported_band *sband)
  1209. {
  1210. unsigned int i;
  1211. if (!sband)
  1212. return;
  1213. for (i = 0; i < sband->n_channels; i++)
  1214. reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
  1215. }
  1216. static void reg_process_ht_flags(struct wiphy *wiphy)
  1217. {
  1218. enum ieee80211_band band;
  1219. if (!wiphy)
  1220. return;
  1221. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  1222. reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
  1223. }
  1224. static void reg_call_notifier(struct wiphy *wiphy,
  1225. struct regulatory_request *request)
  1226. {
  1227. if (wiphy->reg_notifier)
  1228. wiphy->reg_notifier(wiphy, request);
  1229. }
  1230. static void wiphy_update_regulatory(struct wiphy *wiphy,
  1231. enum nl80211_reg_initiator initiator)
  1232. {
  1233. enum ieee80211_band band;
  1234. struct regulatory_request *lr = get_last_request();
  1235. if (ignore_reg_update(wiphy, initiator)) {
  1236. /*
  1237. * Regulatory updates set by CORE are ignored for custom
  1238. * regulatory cards. Let us notify the changes to the driver,
  1239. * as some drivers used this to restore its orig_* reg domain.
  1240. */
  1241. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  1242. wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
  1243. reg_call_notifier(wiphy, lr);
  1244. return;
  1245. }
  1246. lr->dfs_region = get_cfg80211_regdom()->dfs_region;
  1247. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  1248. handle_band(wiphy, initiator, wiphy->bands[band]);
  1249. reg_process_beacons(wiphy);
  1250. reg_process_ht_flags(wiphy);
  1251. reg_call_notifier(wiphy, lr);
  1252. }
  1253. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  1254. {
  1255. struct cfg80211_registered_device *rdev;
  1256. struct wiphy *wiphy;
  1257. ASSERT_RTNL();
  1258. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1259. wiphy = &rdev->wiphy;
  1260. wiphy_update_regulatory(wiphy, initiator);
  1261. }
  1262. }
  1263. static void handle_channel_custom(struct wiphy *wiphy,
  1264. struct ieee80211_channel *chan,
  1265. const struct ieee80211_regdomain *regd)
  1266. {
  1267. u32 bw_flags = 0;
  1268. const struct ieee80211_reg_rule *reg_rule = NULL;
  1269. const struct ieee80211_power_rule *power_rule = NULL;
  1270. const struct ieee80211_freq_range *freq_range = NULL;
  1271. u32 max_bandwidth_khz;
  1272. reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  1273. regd);
  1274. if (IS_ERR(reg_rule)) {
  1275. REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
  1276. chan->center_freq);
  1277. chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  1278. chan->flags = chan->orig_flags;
  1279. return;
  1280. }
  1281. chan_reg_rule_print_dbg(regd, chan, reg_rule);
  1282. power_rule = &reg_rule->power_rule;
  1283. freq_range = &reg_rule->freq_range;
  1284. max_bandwidth_khz = freq_range->max_bandwidth_khz;
  1285. /* Check if auto calculation requested */
  1286. if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  1287. max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
  1288. if (max_bandwidth_khz < MHZ_TO_KHZ(40))
  1289. bw_flags = IEEE80211_CHAN_NO_HT40;
  1290. if (max_bandwidth_khz < MHZ_TO_KHZ(80))
  1291. bw_flags |= IEEE80211_CHAN_NO_80MHZ;
  1292. if (max_bandwidth_khz < MHZ_TO_KHZ(160))
  1293. bw_flags |= IEEE80211_CHAN_NO_160MHZ;
  1294. chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
  1295. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1296. chan->max_reg_power = chan->max_power =
  1297. (int) MBM_TO_DBM(power_rule->max_eirp);
  1298. }
  1299. static void handle_band_custom(struct wiphy *wiphy,
  1300. struct ieee80211_supported_band *sband,
  1301. const struct ieee80211_regdomain *regd)
  1302. {
  1303. unsigned int i;
  1304. if (!sband)
  1305. return;
  1306. for (i = 0; i < sband->n_channels; i++)
  1307. handle_channel_custom(wiphy, &sband->channels[i], regd);
  1308. }
  1309. /* Used by drivers prior to wiphy registration */
  1310. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  1311. const struct ieee80211_regdomain *regd)
  1312. {
  1313. enum ieee80211_band band;
  1314. unsigned int bands_set = 0;
  1315. WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
  1316. "wiphy should have REGULATORY_CUSTOM_REG\n");
  1317. wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
  1318. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1319. if (!wiphy->bands[band])
  1320. continue;
  1321. handle_band_custom(wiphy, wiphy->bands[band], regd);
  1322. bands_set++;
  1323. }
  1324. /*
  1325. * no point in calling this if it won't have any effect
  1326. * on your device's supported bands.
  1327. */
  1328. WARN_ON(!bands_set);
  1329. }
  1330. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  1331. static void reg_set_request_processed(void)
  1332. {
  1333. bool need_more_processing = false;
  1334. struct regulatory_request *lr = get_last_request();
  1335. lr->processed = true;
  1336. spin_lock(&reg_requests_lock);
  1337. if (!list_empty(&reg_requests_list))
  1338. need_more_processing = true;
  1339. spin_unlock(&reg_requests_lock);
  1340. if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
  1341. cancel_delayed_work(&reg_timeout);
  1342. if (need_more_processing)
  1343. schedule_work(&reg_work);
  1344. }
  1345. /**
  1346. * reg_process_hint_core - process core regulatory requests
  1347. * @pending_request: a pending core regulatory request
  1348. *
  1349. * The wireless subsystem can use this function to process
  1350. * a regulatory request issued by the regulatory core.
  1351. *
  1352. * Returns one of the different reg request treatment values.
  1353. */
  1354. static enum reg_request_treatment
  1355. reg_process_hint_core(struct regulatory_request *core_request)
  1356. {
  1357. core_request->intersect = false;
  1358. core_request->processed = false;
  1359. reg_update_last_request(core_request);
  1360. return reg_call_crda(core_request);
  1361. }
  1362. static enum reg_request_treatment
  1363. __reg_process_hint_user(struct regulatory_request *user_request)
  1364. {
  1365. struct regulatory_request *lr = get_last_request();
  1366. if (reg_request_indoor(user_request)) {
  1367. reg_is_indoor = true;
  1368. return REG_REQ_USER_HINT_HANDLED;
  1369. }
  1370. if (reg_request_cell_base(user_request))
  1371. return reg_ignore_cell_hint(user_request);
  1372. if (reg_request_cell_base(lr))
  1373. return REG_REQ_IGNORE;
  1374. if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1375. return REG_REQ_INTERSECT;
  1376. /*
  1377. * If the user knows better the user should set the regdom
  1378. * to their country before the IE is picked up
  1379. */
  1380. if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
  1381. lr->intersect)
  1382. return REG_REQ_IGNORE;
  1383. /*
  1384. * Process user requests only after previous user/driver/core
  1385. * requests have been processed
  1386. */
  1387. if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1388. lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1389. lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
  1390. regdom_changes(lr->alpha2))
  1391. return REG_REQ_IGNORE;
  1392. if (!regdom_changes(user_request->alpha2))
  1393. return REG_REQ_ALREADY_SET;
  1394. return REG_REQ_OK;
  1395. }
  1396. /**
  1397. * reg_process_hint_user - process user regulatory requests
  1398. * @user_request: a pending user regulatory request
  1399. *
  1400. * The wireless subsystem can use this function to process
  1401. * a regulatory request initiated by userspace.
  1402. *
  1403. * Returns one of the different reg request treatment values.
  1404. */
  1405. static enum reg_request_treatment
  1406. reg_process_hint_user(struct regulatory_request *user_request)
  1407. {
  1408. enum reg_request_treatment treatment;
  1409. treatment = __reg_process_hint_user(user_request);
  1410. if (treatment == REG_REQ_IGNORE ||
  1411. treatment == REG_REQ_ALREADY_SET ||
  1412. treatment == REG_REQ_USER_HINT_HANDLED) {
  1413. reg_free_request(user_request);
  1414. return treatment;
  1415. }
  1416. user_request->intersect = treatment == REG_REQ_INTERSECT;
  1417. user_request->processed = false;
  1418. reg_update_last_request(user_request);
  1419. user_alpha2[0] = user_request->alpha2[0];
  1420. user_alpha2[1] = user_request->alpha2[1];
  1421. return reg_call_crda(user_request);
  1422. }
  1423. static enum reg_request_treatment
  1424. __reg_process_hint_driver(struct regulatory_request *driver_request)
  1425. {
  1426. struct regulatory_request *lr = get_last_request();
  1427. if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
  1428. if (regdom_changes(driver_request->alpha2))
  1429. return REG_REQ_OK;
  1430. return REG_REQ_ALREADY_SET;
  1431. }
  1432. /*
  1433. * This would happen if you unplug and plug your card
  1434. * back in or if you add a new device for which the previously
  1435. * loaded card also agrees on the regulatory domain.
  1436. */
  1437. if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1438. !regdom_changes(driver_request->alpha2))
  1439. return REG_REQ_ALREADY_SET;
  1440. return REG_REQ_INTERSECT;
  1441. }
  1442. /**
  1443. * reg_process_hint_driver - process driver regulatory requests
  1444. * @driver_request: a pending driver regulatory request
  1445. *
  1446. * The wireless subsystem can use this function to process
  1447. * a regulatory request issued by an 802.11 driver.
  1448. *
  1449. * Returns one of the different reg request treatment values.
  1450. */
  1451. static enum reg_request_treatment
  1452. reg_process_hint_driver(struct wiphy *wiphy,
  1453. struct regulatory_request *driver_request)
  1454. {
  1455. const struct ieee80211_regdomain *regd;
  1456. enum reg_request_treatment treatment;
  1457. treatment = __reg_process_hint_driver(driver_request);
  1458. switch (treatment) {
  1459. case REG_REQ_OK:
  1460. break;
  1461. case REG_REQ_IGNORE:
  1462. case REG_REQ_USER_HINT_HANDLED:
  1463. reg_free_request(driver_request);
  1464. return treatment;
  1465. case REG_REQ_INTERSECT:
  1466. /* fall through */
  1467. case REG_REQ_ALREADY_SET:
  1468. regd = reg_copy_regd(get_cfg80211_regdom());
  1469. if (IS_ERR(regd)) {
  1470. reg_free_request(driver_request);
  1471. return REG_REQ_IGNORE;
  1472. }
  1473. rcu_assign_pointer(wiphy->regd, regd);
  1474. }
  1475. driver_request->intersect = treatment == REG_REQ_INTERSECT;
  1476. driver_request->processed = false;
  1477. reg_update_last_request(driver_request);
  1478. /*
  1479. * Since CRDA will not be called in this case as we already
  1480. * have applied the requested regulatory domain before we just
  1481. * inform userspace we have processed the request
  1482. */
  1483. if (treatment == REG_REQ_ALREADY_SET) {
  1484. nl80211_send_reg_change_event(driver_request);
  1485. reg_set_request_processed();
  1486. return treatment;
  1487. }
  1488. return reg_call_crda(driver_request);
  1489. }
  1490. static enum reg_request_treatment
  1491. __reg_process_hint_country_ie(struct wiphy *wiphy,
  1492. struct regulatory_request *country_ie_request)
  1493. {
  1494. struct wiphy *last_wiphy = NULL;
  1495. struct regulatory_request *lr = get_last_request();
  1496. if (reg_request_cell_base(lr)) {
  1497. /* Trust a Cell base station over the AP's country IE */
  1498. if (regdom_changes(country_ie_request->alpha2))
  1499. return REG_REQ_IGNORE;
  1500. return REG_REQ_ALREADY_SET;
  1501. } else {
  1502. if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
  1503. return REG_REQ_IGNORE;
  1504. }
  1505. if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
  1506. return -EINVAL;
  1507. if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1508. return REG_REQ_OK;
  1509. last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
  1510. if (last_wiphy != wiphy) {
  1511. /*
  1512. * Two cards with two APs claiming different
  1513. * Country IE alpha2s. We could
  1514. * intersect them, but that seems unlikely
  1515. * to be correct. Reject second one for now.
  1516. */
  1517. if (regdom_changes(country_ie_request->alpha2))
  1518. return REG_REQ_IGNORE;
  1519. return REG_REQ_ALREADY_SET;
  1520. }
  1521. /*
  1522. * Two consecutive Country IE hints on the same wiphy.
  1523. * This should be picked up early by the driver/stack
  1524. */
  1525. if (WARN_ON(regdom_changes(country_ie_request->alpha2)))
  1526. return REG_REQ_OK;
  1527. return REG_REQ_ALREADY_SET;
  1528. }
  1529. /**
  1530. * reg_process_hint_country_ie - process regulatory requests from country IEs
  1531. * @country_ie_request: a regulatory request from a country IE
  1532. *
  1533. * The wireless subsystem can use this function to process
  1534. * a regulatory request issued by a country Information Element.
  1535. *
  1536. * Returns one of the different reg request treatment values.
  1537. */
  1538. static enum reg_request_treatment
  1539. reg_process_hint_country_ie(struct wiphy *wiphy,
  1540. struct regulatory_request *country_ie_request)
  1541. {
  1542. enum reg_request_treatment treatment;
  1543. treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
  1544. switch (treatment) {
  1545. case REG_REQ_OK:
  1546. break;
  1547. case REG_REQ_IGNORE:
  1548. case REG_REQ_USER_HINT_HANDLED:
  1549. /* fall through */
  1550. case REG_REQ_ALREADY_SET:
  1551. reg_free_request(country_ie_request);
  1552. return treatment;
  1553. case REG_REQ_INTERSECT:
  1554. reg_free_request(country_ie_request);
  1555. /*
  1556. * This doesn't happen yet, not sure we
  1557. * ever want to support it for this case.
  1558. */
  1559. WARN_ONCE(1, "Unexpected intersection for country IEs");
  1560. return REG_REQ_IGNORE;
  1561. }
  1562. country_ie_request->intersect = false;
  1563. country_ie_request->processed = false;
  1564. reg_update_last_request(country_ie_request);
  1565. return reg_call_crda(country_ie_request);
  1566. }
  1567. /* This processes *all* regulatory hints */
  1568. static void reg_process_hint(struct regulatory_request *reg_request)
  1569. {
  1570. struct wiphy *wiphy = NULL;
  1571. enum reg_request_treatment treatment;
  1572. if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
  1573. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1574. switch (reg_request->initiator) {
  1575. case NL80211_REGDOM_SET_BY_CORE:
  1576. reg_process_hint_core(reg_request);
  1577. return;
  1578. case NL80211_REGDOM_SET_BY_USER:
  1579. treatment = reg_process_hint_user(reg_request);
  1580. if (treatment == REG_REQ_IGNORE ||
  1581. treatment == REG_REQ_ALREADY_SET ||
  1582. treatment == REG_REQ_USER_HINT_HANDLED)
  1583. return;
  1584. queue_delayed_work(system_power_efficient_wq,
  1585. &reg_timeout, msecs_to_jiffies(3142));
  1586. return;
  1587. case NL80211_REGDOM_SET_BY_DRIVER:
  1588. if (!wiphy)
  1589. goto out_free;
  1590. treatment = reg_process_hint_driver(wiphy, reg_request);
  1591. break;
  1592. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1593. if (!wiphy)
  1594. goto out_free;
  1595. treatment = reg_process_hint_country_ie(wiphy, reg_request);
  1596. break;
  1597. default:
  1598. WARN(1, "invalid initiator %d\n", reg_request->initiator);
  1599. goto out_free;
  1600. }
  1601. /* This is required so that the orig_* parameters are saved */
  1602. if (treatment == REG_REQ_ALREADY_SET && wiphy &&
  1603. wiphy->regulatory_flags & REGULATORY_STRICT_REG)
  1604. wiphy_update_regulatory(wiphy, reg_request->initiator);
  1605. return;
  1606. out_free:
  1607. reg_free_request(reg_request);
  1608. }
  1609. /*
  1610. * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
  1611. * Regulatory hints come on a first come first serve basis and we
  1612. * must process each one atomically.
  1613. */
  1614. static void reg_process_pending_hints(void)
  1615. {
  1616. struct regulatory_request *reg_request, *lr;
  1617. lr = get_last_request();
  1618. /* When last_request->processed becomes true this will be rescheduled */
  1619. if (lr && !lr->processed) {
  1620. reg_process_hint(lr);
  1621. return;
  1622. }
  1623. spin_lock(&reg_requests_lock);
  1624. if (list_empty(&reg_requests_list)) {
  1625. spin_unlock(&reg_requests_lock);
  1626. return;
  1627. }
  1628. reg_request = list_first_entry(&reg_requests_list,
  1629. struct regulatory_request,
  1630. list);
  1631. list_del_init(&reg_request->list);
  1632. spin_unlock(&reg_requests_lock);
  1633. reg_process_hint(reg_request);
  1634. }
  1635. /* Processes beacon hints -- this has nothing to do with country IEs */
  1636. static void reg_process_pending_beacon_hints(void)
  1637. {
  1638. struct cfg80211_registered_device *rdev;
  1639. struct reg_beacon *pending_beacon, *tmp;
  1640. /* This goes through the _pending_ beacon list */
  1641. spin_lock_bh(&reg_pending_beacons_lock);
  1642. list_for_each_entry_safe(pending_beacon, tmp,
  1643. &reg_pending_beacons, list) {
  1644. list_del_init(&pending_beacon->list);
  1645. /* Applies the beacon hint to current wiphys */
  1646. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1647. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  1648. /* Remembers the beacon hint for new wiphys or reg changes */
  1649. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1650. }
  1651. spin_unlock_bh(&reg_pending_beacons_lock);
  1652. }
  1653. static void reg_todo(struct work_struct *work)
  1654. {
  1655. rtnl_lock();
  1656. reg_process_pending_hints();
  1657. reg_process_pending_beacon_hints();
  1658. rtnl_unlock();
  1659. }
  1660. static void queue_regulatory_request(struct regulatory_request *request)
  1661. {
  1662. request->alpha2[0] = toupper(request->alpha2[0]);
  1663. request->alpha2[1] = toupper(request->alpha2[1]);
  1664. spin_lock(&reg_requests_lock);
  1665. list_add_tail(&request->list, &reg_requests_list);
  1666. spin_unlock(&reg_requests_lock);
  1667. schedule_work(&reg_work);
  1668. }
  1669. /*
  1670. * Core regulatory hint -- happens during cfg80211_init()
  1671. * and when we restore regulatory settings.
  1672. */
  1673. static int regulatory_hint_core(const char *alpha2)
  1674. {
  1675. struct regulatory_request *request;
  1676. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1677. if (!request)
  1678. return -ENOMEM;
  1679. request->alpha2[0] = alpha2[0];
  1680. request->alpha2[1] = alpha2[1];
  1681. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1682. queue_regulatory_request(request);
  1683. return 0;
  1684. }
  1685. /* User hints */
  1686. int regulatory_hint_user(const char *alpha2,
  1687. enum nl80211_user_reg_hint_type user_reg_hint_type)
  1688. {
  1689. struct regulatory_request *request;
  1690. if (WARN_ON(!alpha2))
  1691. return -EINVAL;
  1692. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1693. if (!request)
  1694. return -ENOMEM;
  1695. request->wiphy_idx = WIPHY_IDX_INVALID;
  1696. request->alpha2[0] = alpha2[0];
  1697. request->alpha2[1] = alpha2[1];
  1698. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1699. request->user_reg_hint_type = user_reg_hint_type;
  1700. queue_regulatory_request(request);
  1701. return 0;
  1702. }
  1703. int regulatory_hint_indoor_user(void)
  1704. {
  1705. struct regulatory_request *request;
  1706. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1707. if (!request)
  1708. return -ENOMEM;
  1709. request->wiphy_idx = WIPHY_IDX_INVALID;
  1710. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1711. request->user_reg_hint_type = NL80211_USER_REG_HINT_INDOOR;
  1712. queue_regulatory_request(request);
  1713. return 0;
  1714. }
  1715. /* Driver hints */
  1716. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1717. {
  1718. struct regulatory_request *request;
  1719. if (WARN_ON(!alpha2 || !wiphy))
  1720. return -EINVAL;
  1721. wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
  1722. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1723. if (!request)
  1724. return -ENOMEM;
  1725. request->wiphy_idx = get_wiphy_idx(wiphy);
  1726. request->alpha2[0] = alpha2[0];
  1727. request->alpha2[1] = alpha2[1];
  1728. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1729. queue_regulatory_request(request);
  1730. return 0;
  1731. }
  1732. EXPORT_SYMBOL(regulatory_hint);
  1733. void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
  1734. const u8 *country_ie, u8 country_ie_len)
  1735. {
  1736. char alpha2[2];
  1737. enum environment_cap env = ENVIRON_ANY;
  1738. struct regulatory_request *request = NULL, *lr;
  1739. /* IE len must be evenly divisible by 2 */
  1740. if (country_ie_len & 0x01)
  1741. return;
  1742. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1743. return;
  1744. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1745. if (!request)
  1746. return;
  1747. alpha2[0] = country_ie[0];
  1748. alpha2[1] = country_ie[1];
  1749. if (country_ie[2] == 'I')
  1750. env = ENVIRON_INDOOR;
  1751. else if (country_ie[2] == 'O')
  1752. env = ENVIRON_OUTDOOR;
  1753. rcu_read_lock();
  1754. lr = get_last_request();
  1755. if (unlikely(!lr))
  1756. goto out;
  1757. /*
  1758. * We will run this only upon a successful connection on cfg80211.
  1759. * We leave conflict resolution to the workqueue, where can hold
  1760. * the RTNL.
  1761. */
  1762. if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1763. lr->wiphy_idx != WIPHY_IDX_INVALID)
  1764. goto out;
  1765. request->wiphy_idx = get_wiphy_idx(wiphy);
  1766. request->alpha2[0] = alpha2[0];
  1767. request->alpha2[1] = alpha2[1];
  1768. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  1769. request->country_ie_env = env;
  1770. queue_regulatory_request(request);
  1771. request = NULL;
  1772. out:
  1773. kfree(request);
  1774. rcu_read_unlock();
  1775. }
  1776. static void restore_alpha2(char *alpha2, bool reset_user)
  1777. {
  1778. /* indicates there is no alpha2 to consider for restoration */
  1779. alpha2[0] = '9';
  1780. alpha2[1] = '7';
  1781. /* The user setting has precedence over the module parameter */
  1782. if (is_user_regdom_saved()) {
  1783. /* Unless we're asked to ignore it and reset it */
  1784. if (reset_user) {
  1785. REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
  1786. user_alpha2[0] = '9';
  1787. user_alpha2[1] = '7';
  1788. /*
  1789. * If we're ignoring user settings, we still need to
  1790. * check the module parameter to ensure we put things
  1791. * back as they were for a full restore.
  1792. */
  1793. if (!is_world_regdom(ieee80211_regdom)) {
  1794. REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
  1795. ieee80211_regdom[0], ieee80211_regdom[1]);
  1796. alpha2[0] = ieee80211_regdom[0];
  1797. alpha2[1] = ieee80211_regdom[1];
  1798. }
  1799. } else {
  1800. REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
  1801. user_alpha2[0], user_alpha2[1]);
  1802. alpha2[0] = user_alpha2[0];
  1803. alpha2[1] = user_alpha2[1];
  1804. }
  1805. } else if (!is_world_regdom(ieee80211_regdom)) {
  1806. REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
  1807. ieee80211_regdom[0], ieee80211_regdom[1]);
  1808. alpha2[0] = ieee80211_regdom[0];
  1809. alpha2[1] = ieee80211_regdom[1];
  1810. } else
  1811. REG_DBG_PRINT("Restoring regulatory settings\n");
  1812. }
  1813. static void restore_custom_reg_settings(struct wiphy *wiphy)
  1814. {
  1815. struct ieee80211_supported_band *sband;
  1816. enum ieee80211_band band;
  1817. struct ieee80211_channel *chan;
  1818. int i;
  1819. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1820. sband = wiphy->bands[band];
  1821. if (!sband)
  1822. continue;
  1823. for (i = 0; i < sband->n_channels; i++) {
  1824. chan = &sband->channels[i];
  1825. chan->flags = chan->orig_flags;
  1826. chan->max_antenna_gain = chan->orig_mag;
  1827. chan->max_power = chan->orig_mpwr;
  1828. chan->beacon_found = false;
  1829. }
  1830. }
  1831. }
  1832. /*
  1833. * Restoring regulatory settings involves ingoring any
  1834. * possibly stale country IE information and user regulatory
  1835. * settings if so desired, this includes any beacon hints
  1836. * learned as we could have traveled outside to another country
  1837. * after disconnection. To restore regulatory settings we do
  1838. * exactly what we did at bootup:
  1839. *
  1840. * - send a core regulatory hint
  1841. * - send a user regulatory hint if applicable
  1842. *
  1843. * Device drivers that send a regulatory hint for a specific country
  1844. * keep their own regulatory domain on wiphy->regd so that does does
  1845. * not need to be remembered.
  1846. */
  1847. static void restore_regulatory_settings(bool reset_user)
  1848. {
  1849. char alpha2[2];
  1850. char world_alpha2[2];
  1851. struct reg_beacon *reg_beacon, *btmp;
  1852. struct regulatory_request *reg_request, *tmp;
  1853. LIST_HEAD(tmp_reg_req_list);
  1854. struct cfg80211_registered_device *rdev;
  1855. ASSERT_RTNL();
  1856. reg_is_indoor = false;
  1857. reset_regdomains(true, &world_regdom);
  1858. restore_alpha2(alpha2, reset_user);
  1859. /*
  1860. * If there's any pending requests we simply
  1861. * stash them to a temporary pending queue and
  1862. * add then after we've restored regulatory
  1863. * settings.
  1864. */
  1865. spin_lock(&reg_requests_lock);
  1866. list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
  1867. if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
  1868. continue;
  1869. list_move_tail(&reg_request->list, &tmp_reg_req_list);
  1870. }
  1871. spin_unlock(&reg_requests_lock);
  1872. /* Clear beacon hints */
  1873. spin_lock_bh(&reg_pending_beacons_lock);
  1874. list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
  1875. list_del(&reg_beacon->list);
  1876. kfree(reg_beacon);
  1877. }
  1878. spin_unlock_bh(&reg_pending_beacons_lock);
  1879. list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
  1880. list_del(&reg_beacon->list);
  1881. kfree(reg_beacon);
  1882. }
  1883. /* First restore to the basic regulatory settings */
  1884. world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
  1885. world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
  1886. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1887. if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
  1888. restore_custom_reg_settings(&rdev->wiphy);
  1889. }
  1890. regulatory_hint_core(world_alpha2);
  1891. /*
  1892. * This restores the ieee80211_regdom module parameter
  1893. * preference or the last user requested regulatory
  1894. * settings, user regulatory settings takes precedence.
  1895. */
  1896. if (is_an_alpha2(alpha2))
  1897. regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
  1898. spin_lock(&reg_requests_lock);
  1899. list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
  1900. spin_unlock(&reg_requests_lock);
  1901. REG_DBG_PRINT("Kicking the queue\n");
  1902. schedule_work(&reg_work);
  1903. }
  1904. void regulatory_hint_disconnect(void)
  1905. {
  1906. REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
  1907. restore_regulatory_settings(false);
  1908. }
  1909. static bool freq_is_chan_12_13_14(u16 freq)
  1910. {
  1911. if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
  1912. freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
  1913. freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
  1914. return true;
  1915. return false;
  1916. }
  1917. static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
  1918. {
  1919. struct reg_beacon *pending_beacon;
  1920. list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
  1921. if (beacon_chan->center_freq ==
  1922. pending_beacon->chan.center_freq)
  1923. return true;
  1924. return false;
  1925. }
  1926. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1927. struct ieee80211_channel *beacon_chan,
  1928. gfp_t gfp)
  1929. {
  1930. struct reg_beacon *reg_beacon;
  1931. bool processing;
  1932. if (beacon_chan->beacon_found ||
  1933. beacon_chan->flags & IEEE80211_CHAN_RADAR ||
  1934. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1935. !freq_is_chan_12_13_14(beacon_chan->center_freq)))
  1936. return 0;
  1937. spin_lock_bh(&reg_pending_beacons_lock);
  1938. processing = pending_reg_beacon(beacon_chan);
  1939. spin_unlock_bh(&reg_pending_beacons_lock);
  1940. if (processing)
  1941. return 0;
  1942. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1943. if (!reg_beacon)
  1944. return -ENOMEM;
  1945. REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
  1946. beacon_chan->center_freq,
  1947. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1948. wiphy_name(wiphy));
  1949. memcpy(&reg_beacon->chan, beacon_chan,
  1950. sizeof(struct ieee80211_channel));
  1951. /*
  1952. * Since we can be called from BH or and non-BH context
  1953. * we must use spin_lock_bh()
  1954. */
  1955. spin_lock_bh(&reg_pending_beacons_lock);
  1956. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1957. spin_unlock_bh(&reg_pending_beacons_lock);
  1958. schedule_work(&reg_work);
  1959. return 0;
  1960. }
  1961. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1962. {
  1963. unsigned int i;
  1964. const struct ieee80211_reg_rule *reg_rule = NULL;
  1965. const struct ieee80211_freq_range *freq_range = NULL;
  1966. const struct ieee80211_power_rule *power_rule = NULL;
  1967. char bw[32], cac_time[32];
  1968. pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
  1969. for (i = 0; i < rd->n_reg_rules; i++) {
  1970. reg_rule = &rd->reg_rules[i];
  1971. freq_range = &reg_rule->freq_range;
  1972. power_rule = &reg_rule->power_rule;
  1973. if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  1974. snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
  1975. freq_range->max_bandwidth_khz,
  1976. reg_get_max_bandwidth(rd, reg_rule));
  1977. else
  1978. snprintf(bw, sizeof(bw), "%d KHz",
  1979. freq_range->max_bandwidth_khz);
  1980. if (reg_rule->flags & NL80211_RRF_DFS)
  1981. scnprintf(cac_time, sizeof(cac_time), "%u s",
  1982. reg_rule->dfs_cac_ms/1000);
  1983. else
  1984. scnprintf(cac_time, sizeof(cac_time), "N/A");
  1985. /*
  1986. * There may not be documentation for max antenna gain
  1987. * in certain regions
  1988. */
  1989. if (power_rule->max_antenna_gain)
  1990. pr_info(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
  1991. freq_range->start_freq_khz,
  1992. freq_range->end_freq_khz,
  1993. bw,
  1994. power_rule->max_antenna_gain,
  1995. power_rule->max_eirp,
  1996. cac_time);
  1997. else
  1998. pr_info(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
  1999. freq_range->start_freq_khz,
  2000. freq_range->end_freq_khz,
  2001. bw,
  2002. power_rule->max_eirp,
  2003. cac_time);
  2004. }
  2005. }
  2006. bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
  2007. {
  2008. switch (dfs_region) {
  2009. case NL80211_DFS_UNSET:
  2010. case NL80211_DFS_FCC:
  2011. case NL80211_DFS_ETSI:
  2012. case NL80211_DFS_JP:
  2013. return true;
  2014. default:
  2015. REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
  2016. dfs_region);
  2017. return false;
  2018. }
  2019. }
  2020. static void print_regdomain(const struct ieee80211_regdomain *rd)
  2021. {
  2022. struct regulatory_request *lr = get_last_request();
  2023. if (is_intersected_alpha2(rd->alpha2)) {
  2024. if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  2025. struct cfg80211_registered_device *rdev;
  2026. rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
  2027. if (rdev) {
  2028. pr_info("Current regulatory domain updated by AP to: %c%c\n",
  2029. rdev->country_ie_alpha2[0],
  2030. rdev->country_ie_alpha2[1]);
  2031. } else
  2032. pr_info("Current regulatory domain intersected:\n");
  2033. } else
  2034. pr_info("Current regulatory domain intersected:\n");
  2035. } else if (is_world_regdom(rd->alpha2)) {
  2036. pr_info("World regulatory domain updated:\n");
  2037. } else {
  2038. if (is_unknown_alpha2(rd->alpha2))
  2039. pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
  2040. else {
  2041. if (reg_request_cell_base(lr))
  2042. pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
  2043. rd->alpha2[0], rd->alpha2[1]);
  2044. else
  2045. pr_info("Regulatory domain changed to country: %c%c\n",
  2046. rd->alpha2[0], rd->alpha2[1]);
  2047. }
  2048. }
  2049. pr_info(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
  2050. print_rd_rules(rd);
  2051. }
  2052. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  2053. {
  2054. pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
  2055. print_rd_rules(rd);
  2056. }
  2057. static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
  2058. {
  2059. if (!is_world_regdom(rd->alpha2))
  2060. return -EINVAL;
  2061. update_world_regdomain(rd);
  2062. return 0;
  2063. }
  2064. static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
  2065. struct regulatory_request *user_request)
  2066. {
  2067. const struct ieee80211_regdomain *intersected_rd = NULL;
  2068. if (!regdom_changes(rd->alpha2))
  2069. return -EALREADY;
  2070. if (!is_valid_rd(rd)) {
  2071. pr_err("Invalid regulatory domain detected:\n");
  2072. print_regdomain_info(rd);
  2073. return -EINVAL;
  2074. }
  2075. if (!user_request->intersect) {
  2076. reset_regdomains(false, rd);
  2077. return 0;
  2078. }
  2079. intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
  2080. if (!intersected_rd)
  2081. return -EINVAL;
  2082. kfree(rd);
  2083. rd = NULL;
  2084. reset_regdomains(false, intersected_rd);
  2085. return 0;
  2086. }
  2087. static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
  2088. struct regulatory_request *driver_request)
  2089. {
  2090. const struct ieee80211_regdomain *regd;
  2091. const struct ieee80211_regdomain *intersected_rd = NULL;
  2092. const struct ieee80211_regdomain *tmp;
  2093. struct wiphy *request_wiphy;
  2094. if (is_world_regdom(rd->alpha2))
  2095. return -EINVAL;
  2096. if (!regdom_changes(rd->alpha2))
  2097. return -EALREADY;
  2098. if (!is_valid_rd(rd)) {
  2099. pr_err("Invalid regulatory domain detected:\n");
  2100. print_regdomain_info(rd);
  2101. return -EINVAL;
  2102. }
  2103. request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
  2104. if (!request_wiphy) {
  2105. queue_delayed_work(system_power_efficient_wq,
  2106. &reg_timeout, 0);
  2107. return -ENODEV;
  2108. }
  2109. if (!driver_request->intersect) {
  2110. if (request_wiphy->regd)
  2111. return -EALREADY;
  2112. regd = reg_copy_regd(rd);
  2113. if (IS_ERR(regd))
  2114. return PTR_ERR(regd);
  2115. rcu_assign_pointer(request_wiphy->regd, regd);
  2116. reset_regdomains(false, rd);
  2117. return 0;
  2118. }
  2119. intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
  2120. if (!intersected_rd)
  2121. return -EINVAL;
  2122. /*
  2123. * We can trash what CRDA provided now.
  2124. * However if a driver requested this specific regulatory
  2125. * domain we keep it for its private use
  2126. */
  2127. tmp = get_wiphy_regdom(request_wiphy);
  2128. rcu_assign_pointer(request_wiphy->regd, rd);
  2129. rcu_free_regdom(tmp);
  2130. rd = NULL;
  2131. reset_regdomains(false, intersected_rd);
  2132. return 0;
  2133. }
  2134. static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
  2135. struct regulatory_request *country_ie_request)
  2136. {
  2137. struct wiphy *request_wiphy;
  2138. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  2139. !is_unknown_alpha2(rd->alpha2))
  2140. return -EINVAL;
  2141. /*
  2142. * Lets only bother proceeding on the same alpha2 if the current
  2143. * rd is non static (it means CRDA was present and was used last)
  2144. * and the pending request came in from a country IE
  2145. */
  2146. if (!is_valid_rd(rd)) {
  2147. pr_err("Invalid regulatory domain detected:\n");
  2148. print_regdomain_info(rd);
  2149. return -EINVAL;
  2150. }
  2151. request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
  2152. if (!request_wiphy) {
  2153. queue_delayed_work(system_power_efficient_wq,
  2154. &reg_timeout, 0);
  2155. return -ENODEV;
  2156. }
  2157. if (country_ie_request->intersect)
  2158. return -EINVAL;
  2159. reset_regdomains(false, rd);
  2160. return 0;
  2161. }
  2162. /*
  2163. * Use this call to set the current regulatory domain. Conflicts with
  2164. * multiple drivers can be ironed out later. Caller must've already
  2165. * kmalloc'd the rd structure.
  2166. */
  2167. int set_regdom(const struct ieee80211_regdomain *rd)
  2168. {
  2169. struct regulatory_request *lr;
  2170. bool user_reset = false;
  2171. int r;
  2172. if (!reg_is_valid_request(rd->alpha2)) {
  2173. kfree(rd);
  2174. return -EINVAL;
  2175. }
  2176. lr = get_last_request();
  2177. /* Note that this doesn't update the wiphys, this is done below */
  2178. switch (lr->initiator) {
  2179. case NL80211_REGDOM_SET_BY_CORE:
  2180. r = reg_set_rd_core(rd);
  2181. break;
  2182. case NL80211_REGDOM_SET_BY_USER:
  2183. r = reg_set_rd_user(rd, lr);
  2184. user_reset = true;
  2185. break;
  2186. case NL80211_REGDOM_SET_BY_DRIVER:
  2187. r = reg_set_rd_driver(rd, lr);
  2188. break;
  2189. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  2190. r = reg_set_rd_country_ie(rd, lr);
  2191. break;
  2192. default:
  2193. WARN(1, "invalid initiator %d\n", lr->initiator);
  2194. return -EINVAL;
  2195. }
  2196. if (r) {
  2197. switch (r) {
  2198. case -EALREADY:
  2199. reg_set_request_processed();
  2200. break;
  2201. default:
  2202. /* Back to world regulatory in case of errors */
  2203. restore_regulatory_settings(user_reset);
  2204. }
  2205. kfree(rd);
  2206. return r;
  2207. }
  2208. /* This would make this whole thing pointless */
  2209. if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
  2210. return -EINVAL;
  2211. /* update all wiphys now with the new established regulatory domain */
  2212. update_all_wiphy_regulatory(lr->initiator);
  2213. print_regdomain(get_cfg80211_regdom());
  2214. nl80211_send_reg_change_event(lr);
  2215. reg_set_request_processed();
  2216. return 0;
  2217. }
  2218. void wiphy_regulatory_register(struct wiphy *wiphy)
  2219. {
  2220. struct regulatory_request *lr;
  2221. if (!reg_dev_ignore_cell_hint(wiphy))
  2222. reg_num_devs_support_basehint++;
  2223. lr = get_last_request();
  2224. wiphy_update_regulatory(wiphy, lr->initiator);
  2225. }
  2226. void wiphy_regulatory_deregister(struct wiphy *wiphy)
  2227. {
  2228. struct wiphy *request_wiphy = NULL;
  2229. struct regulatory_request *lr;
  2230. lr = get_last_request();
  2231. if (!reg_dev_ignore_cell_hint(wiphy))
  2232. reg_num_devs_support_basehint--;
  2233. rcu_free_regdom(get_wiphy_regdom(wiphy));
  2234. RCU_INIT_POINTER(wiphy->regd, NULL);
  2235. if (lr)
  2236. request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
  2237. if (!request_wiphy || request_wiphy != wiphy)
  2238. return;
  2239. lr->wiphy_idx = WIPHY_IDX_INVALID;
  2240. lr->country_ie_env = ENVIRON_ANY;
  2241. }
  2242. static void reg_timeout_work(struct work_struct *work)
  2243. {
  2244. REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
  2245. rtnl_lock();
  2246. restore_regulatory_settings(true);
  2247. rtnl_unlock();
  2248. }
  2249. /*
  2250. * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
  2251. * UNII band definitions
  2252. */
  2253. int cfg80211_get_unii(int freq)
  2254. {
  2255. /* UNII-1 */
  2256. if (freq >= 5150 && freq <= 5250)
  2257. return 0;
  2258. /* UNII-2A */
  2259. if (freq > 5250 && freq <= 5350)
  2260. return 1;
  2261. /* UNII-2B */
  2262. if (freq > 5350 && freq <= 5470)
  2263. return 2;
  2264. /* UNII-2C */
  2265. if (freq > 5470 && freq <= 5725)
  2266. return 3;
  2267. /* UNII-3 */
  2268. if (freq > 5725 && freq <= 5825)
  2269. return 4;
  2270. return -EINVAL;
  2271. }
  2272. bool regulatory_indoor_allowed(void)
  2273. {
  2274. return reg_is_indoor;
  2275. }
  2276. int __init regulatory_init(void)
  2277. {
  2278. int err = 0;
  2279. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  2280. if (IS_ERR(reg_pdev))
  2281. return PTR_ERR(reg_pdev);
  2282. spin_lock_init(&reg_requests_lock);
  2283. spin_lock_init(&reg_pending_beacons_lock);
  2284. reg_regdb_size_check();
  2285. rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
  2286. user_alpha2[0] = '9';
  2287. user_alpha2[1] = '7';
  2288. /* We always try to get an update for the static regdomain */
  2289. err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
  2290. if (err) {
  2291. if (err == -ENOMEM)
  2292. return err;
  2293. /*
  2294. * N.B. kobject_uevent_env() can fail mainly for when we're out
  2295. * memory which is handled and propagated appropriately above
  2296. * but it can also fail during a netlink_broadcast() or during
  2297. * early boot for call_usermodehelper(). For now treat these
  2298. * errors as non-fatal.
  2299. */
  2300. pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
  2301. }
  2302. /*
  2303. * Finally, if the user set the module parameter treat it
  2304. * as a user hint.
  2305. */
  2306. if (!is_world_regdom(ieee80211_regdom))
  2307. regulatory_hint_user(ieee80211_regdom,
  2308. NL80211_USER_REG_HINT_USER);
  2309. return 0;
  2310. }
  2311. void regulatory_exit(void)
  2312. {
  2313. struct regulatory_request *reg_request, *tmp;
  2314. struct reg_beacon *reg_beacon, *btmp;
  2315. cancel_work_sync(&reg_work);
  2316. cancel_delayed_work_sync(&reg_timeout);
  2317. /* Lock to suppress warnings */
  2318. rtnl_lock();
  2319. reset_regdomains(true, NULL);
  2320. rtnl_unlock();
  2321. dev_set_uevent_suppress(&reg_pdev->dev, true);
  2322. platform_device_unregister(reg_pdev);
  2323. list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
  2324. list_del(&reg_beacon->list);
  2325. kfree(reg_beacon);
  2326. }
  2327. list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
  2328. list_del(&reg_beacon->list);
  2329. kfree(reg_beacon);
  2330. }
  2331. list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
  2332. list_del(&reg_request->list);
  2333. kfree(reg_request);
  2334. }
  2335. }