reg.c 85 KB

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