reg.c 82 KB

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