reg.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007
  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. * Copyright 2017 Intel Deutschland GmbH
  8. * Copyright (C) 2018 Intel Corporation
  9. *
  10. * Permission to use, copy, modify, and/or distribute this software for any
  11. * purpose with or without fee is hereby granted, provided that the above
  12. * copyright notice and this permission notice appear in all copies.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  15. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  17. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  19. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  20. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. */
  22. /**
  23. * DOC: Wireless regulatory infrastructure
  24. *
  25. * The usual implementation is for a driver to read a device EEPROM to
  26. * determine which regulatory domain it should be operating under, then
  27. * looking up the allowable channels in a driver-local table and finally
  28. * registering those channels in the wiphy structure.
  29. *
  30. * Another set of compliance enforcement is for drivers to use their
  31. * own compliance limits which can be stored on the EEPROM. The host
  32. * driver or firmware may ensure these are used.
  33. *
  34. * In addition to all this we provide an extra layer of regulatory
  35. * conformance. For drivers which do not have any regulatory
  36. * information CRDA provides the complete regulatory solution.
  37. * For others it provides a community effort on further restrictions
  38. * to enhance compliance.
  39. *
  40. * Note: When number of rules --> infinity we will not be able to
  41. * index on alpha2 any more, instead we'll probably have to
  42. * rely on some SHA1 checksum of the regdomain for example.
  43. *
  44. */
  45. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  46. #include <linux/kernel.h>
  47. #include <linux/export.h>
  48. #include <linux/slab.h>
  49. #include <linux/list.h>
  50. #include <linux/ctype.h>
  51. #include <linux/nl80211.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/verification.h>
  54. #include <linux/moduleparam.h>
  55. #include <linux/firmware.h>
  56. #include <net/cfg80211.h>
  57. #include "core.h"
  58. #include "reg.h"
  59. #include "rdev-ops.h"
  60. #include "nl80211.h"
  61. /*
  62. * Grace period we give before making sure all current interfaces reside on
  63. * channels allowed by the current regulatory domain.
  64. */
  65. #define REG_ENFORCE_GRACE_MS 60000
  66. /**
  67. * enum reg_request_treatment - regulatory request treatment
  68. *
  69. * @REG_REQ_OK: continue processing the regulatory request
  70. * @REG_REQ_IGNORE: ignore the regulatory request
  71. * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
  72. * be intersected with the current one.
  73. * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
  74. * regulatory settings, and no further processing is required.
  75. */
  76. enum reg_request_treatment {
  77. REG_REQ_OK,
  78. REG_REQ_IGNORE,
  79. REG_REQ_INTERSECT,
  80. REG_REQ_ALREADY_SET,
  81. };
  82. static struct regulatory_request core_request_world = {
  83. .initiator = NL80211_REGDOM_SET_BY_CORE,
  84. .alpha2[0] = '0',
  85. .alpha2[1] = '0',
  86. .intersect = false,
  87. .processed = true,
  88. .country_ie_env = ENVIRON_ANY,
  89. };
  90. /*
  91. * Receipt of information from last regulatory request,
  92. * protected by RTNL (and can be accessed with RCU protection)
  93. */
  94. static struct regulatory_request __rcu *last_request =
  95. (void __force __rcu *)&core_request_world;
  96. /* To trigger userspace events and load firmware */
  97. static struct platform_device *reg_pdev;
  98. /*
  99. * Central wireless core regulatory domains, we only need two,
  100. * the current one and a world regulatory domain in case we have no
  101. * information to give us an alpha2.
  102. * (protected by RTNL, can be read under RCU)
  103. */
  104. const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
  105. /*
  106. * Number of devices that registered to the core
  107. * that support cellular base station regulatory hints
  108. * (protected by RTNL)
  109. */
  110. static int reg_num_devs_support_basehint;
  111. /*
  112. * State variable indicating if the platform on which the devices
  113. * are attached is operating in an indoor environment. The state variable
  114. * is relevant for all registered devices.
  115. */
  116. static bool reg_is_indoor;
  117. static spinlock_t reg_indoor_lock;
  118. /* Used to track the userspace process controlling the indoor setting */
  119. static u32 reg_is_indoor_portid;
  120. static void restore_regulatory_settings(bool reset_user);
  121. static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
  122. {
  123. return rcu_dereference_rtnl(cfg80211_regdomain);
  124. }
  125. const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
  126. {
  127. return rcu_dereference_rtnl(wiphy->regd);
  128. }
  129. static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
  130. {
  131. switch (dfs_region) {
  132. case NL80211_DFS_UNSET:
  133. return "unset";
  134. case NL80211_DFS_FCC:
  135. return "FCC";
  136. case NL80211_DFS_ETSI:
  137. return "ETSI";
  138. case NL80211_DFS_JP:
  139. return "JP";
  140. }
  141. return "Unknown";
  142. }
  143. enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
  144. {
  145. const struct ieee80211_regdomain *regd = NULL;
  146. const struct ieee80211_regdomain *wiphy_regd = NULL;
  147. regd = get_cfg80211_regdom();
  148. if (!wiphy)
  149. goto out;
  150. wiphy_regd = get_wiphy_regdom(wiphy);
  151. if (!wiphy_regd)
  152. goto out;
  153. if (wiphy_regd->dfs_region == regd->dfs_region)
  154. goto out;
  155. pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n",
  156. dev_name(&wiphy->dev),
  157. reg_dfs_region_str(wiphy_regd->dfs_region),
  158. reg_dfs_region_str(regd->dfs_region));
  159. out:
  160. return regd->dfs_region;
  161. }
  162. static void rcu_free_regdom(const struct ieee80211_regdomain *r)
  163. {
  164. if (!r)
  165. return;
  166. kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
  167. }
  168. static struct regulatory_request *get_last_request(void)
  169. {
  170. return rcu_dereference_rtnl(last_request);
  171. }
  172. /* Used to queue up regulatory hints */
  173. static LIST_HEAD(reg_requests_list);
  174. static spinlock_t reg_requests_lock;
  175. /* Used to queue up beacon hints for review */
  176. static LIST_HEAD(reg_pending_beacons);
  177. static spinlock_t reg_pending_beacons_lock;
  178. /* Used to keep track of processed beacon hints */
  179. static LIST_HEAD(reg_beacon_list);
  180. struct reg_beacon {
  181. struct list_head list;
  182. struct ieee80211_channel chan;
  183. };
  184. static void reg_check_chans_work(struct work_struct *work);
  185. static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
  186. static void reg_todo(struct work_struct *work);
  187. static DECLARE_WORK(reg_work, reg_todo);
  188. /* We keep a static world regulatory domain in case of the absence of CRDA */
  189. static const struct ieee80211_regdomain world_regdom = {
  190. .n_reg_rules = 8,
  191. .alpha2 = "00",
  192. .reg_rules = {
  193. /* IEEE 802.11b/g, channels 1..11 */
  194. REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
  195. /* IEEE 802.11b/g, channels 12..13. */
  196. REG_RULE(2467-10, 2472+10, 20, 6, 20,
  197. NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW),
  198. /* IEEE 802.11 channel 14 - Only JP enables
  199. * this and for 802.11b only */
  200. REG_RULE(2484-10, 2484+10, 20, 6, 20,
  201. NL80211_RRF_NO_IR |
  202. NL80211_RRF_NO_OFDM),
  203. /* IEEE 802.11a, channel 36..48 */
  204. REG_RULE(5180-10, 5240+10, 80, 6, 20,
  205. NL80211_RRF_NO_IR |
  206. NL80211_RRF_AUTO_BW),
  207. /* IEEE 802.11a, channel 52..64 - DFS required */
  208. REG_RULE(5260-10, 5320+10, 80, 6, 20,
  209. NL80211_RRF_NO_IR |
  210. NL80211_RRF_AUTO_BW |
  211. NL80211_RRF_DFS),
  212. /* IEEE 802.11a, channel 100..144 - DFS required */
  213. REG_RULE(5500-10, 5720+10, 160, 6, 20,
  214. NL80211_RRF_NO_IR |
  215. NL80211_RRF_DFS),
  216. /* IEEE 802.11a, channel 149..165 */
  217. REG_RULE(5745-10, 5825+10, 80, 6, 20,
  218. NL80211_RRF_NO_IR),
  219. /* IEEE 802.11ad (60GHz), channels 1..3 */
  220. REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
  221. }
  222. };
  223. /* protected by RTNL */
  224. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  225. &world_regdom;
  226. static char *ieee80211_regdom = "00";
  227. static char user_alpha2[2];
  228. module_param(ieee80211_regdom, charp, 0444);
  229. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  230. static void reg_free_request(struct regulatory_request *request)
  231. {
  232. if (request == &core_request_world)
  233. return;
  234. if (request != get_last_request())
  235. kfree(request);
  236. }
  237. static void reg_free_last_request(void)
  238. {
  239. struct regulatory_request *lr = get_last_request();
  240. if (lr != &core_request_world && lr)
  241. kfree_rcu(lr, rcu_head);
  242. }
  243. static void reg_update_last_request(struct regulatory_request *request)
  244. {
  245. struct regulatory_request *lr;
  246. lr = get_last_request();
  247. if (lr == request)
  248. return;
  249. reg_free_last_request();
  250. rcu_assign_pointer(last_request, request);
  251. }
  252. static void reset_regdomains(bool full_reset,
  253. const struct ieee80211_regdomain *new_regdom)
  254. {
  255. const struct ieee80211_regdomain *r;
  256. ASSERT_RTNL();
  257. r = get_cfg80211_regdom();
  258. /* avoid freeing static information or freeing something twice */
  259. if (r == cfg80211_world_regdom)
  260. r = NULL;
  261. if (cfg80211_world_regdom == &world_regdom)
  262. cfg80211_world_regdom = NULL;
  263. if (r == &world_regdom)
  264. r = NULL;
  265. rcu_free_regdom(r);
  266. rcu_free_regdom(cfg80211_world_regdom);
  267. cfg80211_world_regdom = &world_regdom;
  268. rcu_assign_pointer(cfg80211_regdomain, new_regdom);
  269. if (!full_reset)
  270. return;
  271. reg_update_last_request(&core_request_world);
  272. }
  273. /*
  274. * Dynamic world regulatory domain requested by the wireless
  275. * core upon initialization
  276. */
  277. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  278. {
  279. struct regulatory_request *lr;
  280. lr = get_last_request();
  281. WARN_ON(!lr);
  282. reset_regdomains(false, rd);
  283. cfg80211_world_regdom = rd;
  284. }
  285. bool is_world_regdom(const char *alpha2)
  286. {
  287. if (!alpha2)
  288. return false;
  289. return alpha2[0] == '0' && alpha2[1] == '0';
  290. }
  291. static bool is_alpha2_set(const char *alpha2)
  292. {
  293. if (!alpha2)
  294. return false;
  295. return alpha2[0] && alpha2[1];
  296. }
  297. static bool is_unknown_alpha2(const char *alpha2)
  298. {
  299. if (!alpha2)
  300. return false;
  301. /*
  302. * Special case where regulatory domain was built by driver
  303. * but a specific alpha2 cannot be determined
  304. */
  305. return alpha2[0] == '9' && alpha2[1] == '9';
  306. }
  307. static bool is_intersected_alpha2(const char *alpha2)
  308. {
  309. if (!alpha2)
  310. return false;
  311. /*
  312. * Special case where regulatory domain is the
  313. * result of an intersection between two regulatory domain
  314. * structures
  315. */
  316. return alpha2[0] == '9' && alpha2[1] == '8';
  317. }
  318. static bool is_an_alpha2(const char *alpha2)
  319. {
  320. if (!alpha2)
  321. return false;
  322. return isalpha(alpha2[0]) && isalpha(alpha2[1]);
  323. }
  324. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  325. {
  326. if (!alpha2_x || !alpha2_y)
  327. return false;
  328. return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
  329. }
  330. static bool regdom_changes(const char *alpha2)
  331. {
  332. const struct ieee80211_regdomain *r = get_cfg80211_regdom();
  333. if (!r)
  334. return true;
  335. return !alpha2_equal(r->alpha2, alpha2);
  336. }
  337. /*
  338. * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
  339. * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
  340. * has ever been issued.
  341. */
  342. static bool is_user_regdom_saved(void)
  343. {
  344. if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
  345. return false;
  346. /* This would indicate a mistake on the design */
  347. if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
  348. "Unexpected user alpha2: %c%c\n",
  349. user_alpha2[0], user_alpha2[1]))
  350. return false;
  351. return true;
  352. }
  353. static const struct ieee80211_regdomain *
  354. reg_copy_regd(const struct ieee80211_regdomain *src_regd)
  355. {
  356. struct ieee80211_regdomain *regd;
  357. int size_of_regd;
  358. unsigned int i;
  359. size_of_regd =
  360. sizeof(struct ieee80211_regdomain) +
  361. src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
  362. regd = kzalloc(size_of_regd, GFP_KERNEL);
  363. if (!regd)
  364. return ERR_PTR(-ENOMEM);
  365. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  366. for (i = 0; i < src_regd->n_reg_rules; i++)
  367. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  368. sizeof(struct ieee80211_reg_rule));
  369. return regd;
  370. }
  371. struct reg_regdb_apply_request {
  372. struct list_head list;
  373. const struct ieee80211_regdomain *regdom;
  374. };
  375. static LIST_HEAD(reg_regdb_apply_list);
  376. static DEFINE_MUTEX(reg_regdb_apply_mutex);
  377. static void reg_regdb_apply(struct work_struct *work)
  378. {
  379. struct reg_regdb_apply_request *request;
  380. rtnl_lock();
  381. mutex_lock(&reg_regdb_apply_mutex);
  382. while (!list_empty(&reg_regdb_apply_list)) {
  383. request = list_first_entry(&reg_regdb_apply_list,
  384. struct reg_regdb_apply_request,
  385. list);
  386. list_del(&request->list);
  387. set_regdom(request->regdom, REGD_SOURCE_INTERNAL_DB);
  388. kfree(request);
  389. }
  390. mutex_unlock(&reg_regdb_apply_mutex);
  391. rtnl_unlock();
  392. }
  393. static DECLARE_WORK(reg_regdb_work, reg_regdb_apply);
  394. static int reg_schedule_apply(const struct ieee80211_regdomain *regdom)
  395. {
  396. struct reg_regdb_apply_request *request;
  397. request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL);
  398. if (!request) {
  399. kfree(regdom);
  400. return -ENOMEM;
  401. }
  402. request->regdom = regdom;
  403. mutex_lock(&reg_regdb_apply_mutex);
  404. list_add_tail(&request->list, &reg_regdb_apply_list);
  405. mutex_unlock(&reg_regdb_apply_mutex);
  406. schedule_work(&reg_regdb_work);
  407. return 0;
  408. }
  409. #ifdef CONFIG_CFG80211_CRDA_SUPPORT
  410. /* Max number of consecutive attempts to communicate with CRDA */
  411. #define REG_MAX_CRDA_TIMEOUTS 10
  412. static u32 reg_crda_timeouts;
  413. static void crda_timeout_work(struct work_struct *work);
  414. static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work);
  415. static void crda_timeout_work(struct work_struct *work)
  416. {
  417. pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
  418. rtnl_lock();
  419. reg_crda_timeouts++;
  420. restore_regulatory_settings(true);
  421. rtnl_unlock();
  422. }
  423. static void cancel_crda_timeout(void)
  424. {
  425. cancel_delayed_work(&crda_timeout);
  426. }
  427. static void cancel_crda_timeout_sync(void)
  428. {
  429. cancel_delayed_work_sync(&crda_timeout);
  430. }
  431. static void reset_crda_timeouts(void)
  432. {
  433. reg_crda_timeouts = 0;
  434. }
  435. /*
  436. * This lets us keep regulatory code which is updated on a regulatory
  437. * basis in userspace.
  438. */
  439. static int call_crda(const char *alpha2)
  440. {
  441. char country[12];
  442. char *env[] = { country, NULL };
  443. int ret;
  444. snprintf(country, sizeof(country), "COUNTRY=%c%c",
  445. alpha2[0], alpha2[1]);
  446. if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
  447. pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n");
  448. return -EINVAL;
  449. }
  450. if (!is_world_regdom((char *) alpha2))
  451. pr_debug("Calling CRDA for country: %c%c\n",
  452. alpha2[0], alpha2[1]);
  453. else
  454. pr_debug("Calling CRDA to update world regulatory domain\n");
  455. ret = kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
  456. if (ret)
  457. return ret;
  458. queue_delayed_work(system_power_efficient_wq,
  459. &crda_timeout, msecs_to_jiffies(3142));
  460. return 0;
  461. }
  462. #else
  463. static inline void cancel_crda_timeout(void) {}
  464. static inline void cancel_crda_timeout_sync(void) {}
  465. static inline void reset_crda_timeouts(void) {}
  466. static inline int call_crda(const char *alpha2)
  467. {
  468. return -ENODATA;
  469. }
  470. #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
  471. /* code to directly load a firmware database through request_firmware */
  472. static const struct fwdb_header *regdb;
  473. struct fwdb_country {
  474. u8 alpha2[2];
  475. __be16 coll_ptr;
  476. /* this struct cannot be extended */
  477. } __packed __aligned(4);
  478. struct fwdb_collection {
  479. u8 len;
  480. u8 n_rules;
  481. u8 dfs_region;
  482. /* no optional data yet */
  483. /* aligned to 2, then followed by __be16 array of rule pointers */
  484. } __packed __aligned(4);
  485. enum fwdb_flags {
  486. FWDB_FLAG_NO_OFDM = BIT(0),
  487. FWDB_FLAG_NO_OUTDOOR = BIT(1),
  488. FWDB_FLAG_DFS = BIT(2),
  489. FWDB_FLAG_NO_IR = BIT(3),
  490. FWDB_FLAG_AUTO_BW = BIT(4),
  491. };
  492. struct fwdb_wmm_ac {
  493. u8 ecw;
  494. u8 aifsn;
  495. __be16 cot;
  496. } __packed;
  497. struct fwdb_wmm_rule {
  498. struct fwdb_wmm_ac client[IEEE80211_NUM_ACS];
  499. struct fwdb_wmm_ac ap[IEEE80211_NUM_ACS];
  500. } __packed;
  501. struct fwdb_rule {
  502. u8 len;
  503. u8 flags;
  504. __be16 max_eirp;
  505. __be32 start, end, max_bw;
  506. /* start of optional data */
  507. __be16 cac_timeout;
  508. __be16 wmm_ptr;
  509. } __packed __aligned(4);
  510. #define FWDB_MAGIC 0x52474442
  511. #define FWDB_VERSION 20
  512. struct fwdb_header {
  513. __be32 magic;
  514. __be32 version;
  515. struct fwdb_country country[];
  516. } __packed __aligned(4);
  517. static int ecw2cw(int ecw)
  518. {
  519. return (1 << ecw) - 1;
  520. }
  521. static bool valid_wmm(struct fwdb_wmm_rule *rule)
  522. {
  523. struct fwdb_wmm_ac *ac = (struct fwdb_wmm_ac *)rule;
  524. int i;
  525. for (i = 0; i < IEEE80211_NUM_ACS * 2; i++) {
  526. u16 cw_min = ecw2cw((ac[i].ecw & 0xf0) >> 4);
  527. u16 cw_max = ecw2cw(ac[i].ecw & 0x0f);
  528. u8 aifsn = ac[i].aifsn;
  529. if (cw_min >= cw_max)
  530. return false;
  531. if (aifsn < 1)
  532. return false;
  533. }
  534. return true;
  535. }
  536. static bool valid_rule(const u8 *data, unsigned int size, u16 rule_ptr)
  537. {
  538. struct fwdb_rule *rule = (void *)(data + (rule_ptr << 2));
  539. if ((u8 *)rule + sizeof(rule->len) > data + size)
  540. return false;
  541. /* mandatory fields */
  542. if (rule->len < offsetofend(struct fwdb_rule, max_bw))
  543. return false;
  544. if (rule->len >= offsetofend(struct fwdb_rule, wmm_ptr)) {
  545. u32 wmm_ptr = be16_to_cpu(rule->wmm_ptr) << 2;
  546. struct fwdb_wmm_rule *wmm;
  547. if (wmm_ptr + sizeof(struct fwdb_wmm_rule) > size)
  548. return false;
  549. wmm = (void *)(data + wmm_ptr);
  550. if (!valid_wmm(wmm))
  551. return false;
  552. }
  553. return true;
  554. }
  555. static bool valid_country(const u8 *data, unsigned int size,
  556. const struct fwdb_country *country)
  557. {
  558. unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
  559. struct fwdb_collection *coll = (void *)(data + ptr);
  560. __be16 *rules_ptr;
  561. unsigned int i;
  562. /* make sure we can read len/n_rules */
  563. if ((u8 *)coll + offsetofend(typeof(*coll), n_rules) > data + size)
  564. return false;
  565. /* make sure base struct and all rules fit */
  566. if ((u8 *)coll + ALIGN(coll->len, 2) +
  567. (coll->n_rules * 2) > data + size)
  568. return false;
  569. /* mandatory fields must exist */
  570. if (coll->len < offsetofend(struct fwdb_collection, dfs_region))
  571. return false;
  572. rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
  573. for (i = 0; i < coll->n_rules; i++) {
  574. u16 rule_ptr = be16_to_cpu(rules_ptr[i]);
  575. if (!valid_rule(data, size, rule_ptr))
  576. return false;
  577. }
  578. return true;
  579. }
  580. #ifdef CONFIG_CFG80211_REQUIRE_SIGNED_REGDB
  581. static struct key *builtin_regdb_keys;
  582. static void __init load_keys_from_buffer(const u8 *p, unsigned int buflen)
  583. {
  584. const u8 *end = p + buflen;
  585. size_t plen;
  586. key_ref_t key;
  587. while (p < end) {
  588. /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
  589. * than 256 bytes in size.
  590. */
  591. if (end - p < 4)
  592. goto dodgy_cert;
  593. if (p[0] != 0x30 &&
  594. p[1] != 0x82)
  595. goto dodgy_cert;
  596. plen = (p[2] << 8) | p[3];
  597. plen += 4;
  598. if (plen > end - p)
  599. goto dodgy_cert;
  600. key = key_create_or_update(make_key_ref(builtin_regdb_keys, 1),
  601. "asymmetric", NULL, p, plen,
  602. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  603. KEY_USR_VIEW | KEY_USR_READ),
  604. KEY_ALLOC_NOT_IN_QUOTA |
  605. KEY_ALLOC_BUILT_IN |
  606. KEY_ALLOC_BYPASS_RESTRICTION);
  607. if (IS_ERR(key)) {
  608. pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
  609. PTR_ERR(key));
  610. } else {
  611. pr_notice("Loaded X.509 cert '%s'\n",
  612. key_ref_to_ptr(key)->description);
  613. key_ref_put(key);
  614. }
  615. p += plen;
  616. }
  617. return;
  618. dodgy_cert:
  619. pr_err("Problem parsing in-kernel X.509 certificate list\n");
  620. }
  621. static int __init load_builtin_regdb_keys(void)
  622. {
  623. builtin_regdb_keys =
  624. keyring_alloc(".builtin_regdb_keys",
  625. KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
  626. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  627. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
  628. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  629. if (IS_ERR(builtin_regdb_keys))
  630. return PTR_ERR(builtin_regdb_keys);
  631. pr_notice("Loading compiled-in X.509 certificates for regulatory database\n");
  632. #ifdef CONFIG_CFG80211_USE_KERNEL_REGDB_KEYS
  633. load_keys_from_buffer(shipped_regdb_certs, shipped_regdb_certs_len);
  634. #endif
  635. #ifdef CONFIG_CFG80211_EXTRA_REGDB_KEYDIR
  636. if (CONFIG_CFG80211_EXTRA_REGDB_KEYDIR[0] != '\0')
  637. load_keys_from_buffer(extra_regdb_certs, extra_regdb_certs_len);
  638. #endif
  639. return 0;
  640. }
  641. static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
  642. {
  643. const struct firmware *sig;
  644. bool result;
  645. if (request_firmware(&sig, "regulatory.db.p7s", &reg_pdev->dev))
  646. return false;
  647. result = verify_pkcs7_signature(data, size, sig->data, sig->size,
  648. builtin_regdb_keys,
  649. VERIFYING_UNSPECIFIED_SIGNATURE,
  650. NULL, NULL) == 0;
  651. release_firmware(sig);
  652. return result;
  653. }
  654. static void free_regdb_keyring(void)
  655. {
  656. key_put(builtin_regdb_keys);
  657. }
  658. #else
  659. static int load_builtin_regdb_keys(void)
  660. {
  661. return 0;
  662. }
  663. static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
  664. {
  665. return true;
  666. }
  667. static void free_regdb_keyring(void)
  668. {
  669. }
  670. #endif /* CONFIG_CFG80211_REQUIRE_SIGNED_REGDB */
  671. static bool valid_regdb(const u8 *data, unsigned int size)
  672. {
  673. const struct fwdb_header *hdr = (void *)data;
  674. const struct fwdb_country *country;
  675. if (size < sizeof(*hdr))
  676. return false;
  677. if (hdr->magic != cpu_to_be32(FWDB_MAGIC))
  678. return false;
  679. if (hdr->version != cpu_to_be32(FWDB_VERSION))
  680. return false;
  681. if (!regdb_has_valid_signature(data, size))
  682. return false;
  683. country = &hdr->country[0];
  684. while ((u8 *)(country + 1) <= data + size) {
  685. if (!country->coll_ptr)
  686. break;
  687. if (!valid_country(data, size, country))
  688. return false;
  689. country++;
  690. }
  691. return true;
  692. }
  693. static void set_wmm_rule(const struct fwdb_header *db,
  694. const struct fwdb_country *country,
  695. const struct fwdb_rule *rule,
  696. struct ieee80211_reg_rule *rrule)
  697. {
  698. struct ieee80211_wmm_rule *wmm_rule = &rrule->wmm_rule;
  699. struct fwdb_wmm_rule *wmm;
  700. unsigned int i, wmm_ptr;
  701. wmm_ptr = be16_to_cpu(rule->wmm_ptr) << 2;
  702. wmm = (void *)((u8 *)db + wmm_ptr);
  703. if (!valid_wmm(wmm)) {
  704. pr_err("Invalid regulatory WMM rule %u-%u in domain %c%c\n",
  705. be32_to_cpu(rule->start), be32_to_cpu(rule->end),
  706. country->alpha2[0], country->alpha2[1]);
  707. return;
  708. }
  709. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  710. wmm_rule->client[i].cw_min =
  711. ecw2cw((wmm->client[i].ecw & 0xf0) >> 4);
  712. wmm_rule->client[i].cw_max = ecw2cw(wmm->client[i].ecw & 0x0f);
  713. wmm_rule->client[i].aifsn = wmm->client[i].aifsn;
  714. wmm_rule->client[i].cot =
  715. 1000 * be16_to_cpu(wmm->client[i].cot);
  716. wmm_rule->ap[i].cw_min = ecw2cw((wmm->ap[i].ecw & 0xf0) >> 4);
  717. wmm_rule->ap[i].cw_max = ecw2cw(wmm->ap[i].ecw & 0x0f);
  718. wmm_rule->ap[i].aifsn = wmm->ap[i].aifsn;
  719. wmm_rule->ap[i].cot = 1000 * be16_to_cpu(wmm->ap[i].cot);
  720. }
  721. rrule->has_wmm = true;
  722. }
  723. static int __regdb_query_wmm(const struct fwdb_header *db,
  724. const struct fwdb_country *country, int freq,
  725. struct ieee80211_reg_rule *rrule)
  726. {
  727. unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
  728. struct fwdb_collection *coll = (void *)((u8 *)db + ptr);
  729. int i;
  730. for (i = 0; i < coll->n_rules; i++) {
  731. __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
  732. unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2;
  733. struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr);
  734. if (rule->len < offsetofend(struct fwdb_rule, wmm_ptr))
  735. continue;
  736. if (freq >= KHZ_TO_MHZ(be32_to_cpu(rule->start)) &&
  737. freq <= KHZ_TO_MHZ(be32_to_cpu(rule->end))) {
  738. set_wmm_rule(db, country, rule, rrule);
  739. return 0;
  740. }
  741. }
  742. return -ENODATA;
  743. }
  744. int reg_query_regdb_wmm(char *alpha2, int freq, struct ieee80211_reg_rule *rule)
  745. {
  746. const struct fwdb_header *hdr = regdb;
  747. const struct fwdb_country *country;
  748. if (!regdb)
  749. return -ENODATA;
  750. if (IS_ERR(regdb))
  751. return PTR_ERR(regdb);
  752. country = &hdr->country[0];
  753. while (country->coll_ptr) {
  754. if (alpha2_equal(alpha2, country->alpha2))
  755. return __regdb_query_wmm(regdb, country, freq, rule);
  756. country++;
  757. }
  758. return -ENODATA;
  759. }
  760. EXPORT_SYMBOL(reg_query_regdb_wmm);
  761. static int regdb_query_country(const struct fwdb_header *db,
  762. const struct fwdb_country *country)
  763. {
  764. unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
  765. struct fwdb_collection *coll = (void *)((u8 *)db + ptr);
  766. struct ieee80211_regdomain *regdom;
  767. unsigned int size_of_regd, i;
  768. size_of_regd = sizeof(struct ieee80211_regdomain) +
  769. coll->n_rules * sizeof(struct ieee80211_reg_rule);
  770. regdom = kzalloc(size_of_regd, GFP_KERNEL);
  771. if (!regdom)
  772. return -ENOMEM;
  773. regdom->n_reg_rules = coll->n_rules;
  774. regdom->alpha2[0] = country->alpha2[0];
  775. regdom->alpha2[1] = country->alpha2[1];
  776. regdom->dfs_region = coll->dfs_region;
  777. for (i = 0; i < regdom->n_reg_rules; i++) {
  778. __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
  779. unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2;
  780. struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr);
  781. struct ieee80211_reg_rule *rrule = &regdom->reg_rules[i];
  782. rrule->freq_range.start_freq_khz = be32_to_cpu(rule->start);
  783. rrule->freq_range.end_freq_khz = be32_to_cpu(rule->end);
  784. rrule->freq_range.max_bandwidth_khz = be32_to_cpu(rule->max_bw);
  785. rrule->power_rule.max_antenna_gain = 0;
  786. rrule->power_rule.max_eirp = be16_to_cpu(rule->max_eirp);
  787. rrule->flags = 0;
  788. if (rule->flags & FWDB_FLAG_NO_OFDM)
  789. rrule->flags |= NL80211_RRF_NO_OFDM;
  790. if (rule->flags & FWDB_FLAG_NO_OUTDOOR)
  791. rrule->flags |= NL80211_RRF_NO_OUTDOOR;
  792. if (rule->flags & FWDB_FLAG_DFS)
  793. rrule->flags |= NL80211_RRF_DFS;
  794. if (rule->flags & FWDB_FLAG_NO_IR)
  795. rrule->flags |= NL80211_RRF_NO_IR;
  796. if (rule->flags & FWDB_FLAG_AUTO_BW)
  797. rrule->flags |= NL80211_RRF_AUTO_BW;
  798. rrule->dfs_cac_ms = 0;
  799. /* handle optional data */
  800. if (rule->len >= offsetofend(struct fwdb_rule, cac_timeout))
  801. rrule->dfs_cac_ms =
  802. 1000 * be16_to_cpu(rule->cac_timeout);
  803. if (rule->len >= offsetofend(struct fwdb_rule, wmm_ptr))
  804. set_wmm_rule(db, country, rule, rrule);
  805. }
  806. return reg_schedule_apply(regdom);
  807. }
  808. static int query_regdb(const char *alpha2)
  809. {
  810. const struct fwdb_header *hdr = regdb;
  811. const struct fwdb_country *country;
  812. ASSERT_RTNL();
  813. if (IS_ERR(regdb))
  814. return PTR_ERR(regdb);
  815. country = &hdr->country[0];
  816. while (country->coll_ptr) {
  817. if (alpha2_equal(alpha2, country->alpha2))
  818. return regdb_query_country(regdb, country);
  819. country++;
  820. }
  821. return -ENODATA;
  822. }
  823. static void regdb_fw_cb(const struct firmware *fw, void *context)
  824. {
  825. int set_error = 0;
  826. bool restore = true;
  827. void *db;
  828. if (!fw) {
  829. pr_info("failed to load regulatory.db\n");
  830. set_error = -ENODATA;
  831. } else if (!valid_regdb(fw->data, fw->size)) {
  832. pr_info("loaded regulatory.db is malformed or signature is missing/invalid\n");
  833. set_error = -EINVAL;
  834. }
  835. rtnl_lock();
  836. if (WARN_ON(regdb && !IS_ERR(regdb))) {
  837. /* just restore and free new db */
  838. } else if (set_error) {
  839. regdb = ERR_PTR(set_error);
  840. } else if (fw) {
  841. db = kmemdup(fw->data, fw->size, GFP_KERNEL);
  842. if (db) {
  843. regdb = db;
  844. restore = context && query_regdb(context);
  845. } else {
  846. restore = true;
  847. }
  848. }
  849. if (restore)
  850. restore_regulatory_settings(true);
  851. rtnl_unlock();
  852. kfree(context);
  853. release_firmware(fw);
  854. }
  855. static int query_regdb_file(const char *alpha2)
  856. {
  857. ASSERT_RTNL();
  858. if (regdb)
  859. return query_regdb(alpha2);
  860. alpha2 = kmemdup(alpha2, 2, GFP_KERNEL);
  861. if (!alpha2)
  862. return -ENOMEM;
  863. return request_firmware_nowait(THIS_MODULE, true, "regulatory.db",
  864. &reg_pdev->dev, GFP_KERNEL,
  865. (void *)alpha2, regdb_fw_cb);
  866. }
  867. int reg_reload_regdb(void)
  868. {
  869. const struct firmware *fw;
  870. void *db;
  871. int err;
  872. err = request_firmware(&fw, "regulatory.db", &reg_pdev->dev);
  873. if (err)
  874. return err;
  875. if (!valid_regdb(fw->data, fw->size)) {
  876. err = -ENODATA;
  877. goto out;
  878. }
  879. db = kmemdup(fw->data, fw->size, GFP_KERNEL);
  880. if (!db) {
  881. err = -ENOMEM;
  882. goto out;
  883. }
  884. rtnl_lock();
  885. if (!IS_ERR_OR_NULL(regdb))
  886. kfree(regdb);
  887. regdb = db;
  888. rtnl_unlock();
  889. out:
  890. release_firmware(fw);
  891. return err;
  892. }
  893. static bool reg_query_database(struct regulatory_request *request)
  894. {
  895. if (query_regdb_file(request->alpha2) == 0)
  896. return true;
  897. if (call_crda(request->alpha2) == 0)
  898. return true;
  899. return false;
  900. }
  901. bool reg_is_valid_request(const char *alpha2)
  902. {
  903. struct regulatory_request *lr = get_last_request();
  904. if (!lr || lr->processed)
  905. return false;
  906. return alpha2_equal(lr->alpha2, alpha2);
  907. }
  908. static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
  909. {
  910. struct regulatory_request *lr = get_last_request();
  911. /*
  912. * Follow the driver's regulatory domain, if present, unless a country
  913. * IE has been processed or a user wants to help complaince further
  914. */
  915. if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  916. lr->initiator != NL80211_REGDOM_SET_BY_USER &&
  917. wiphy->regd)
  918. return get_wiphy_regdom(wiphy);
  919. return get_cfg80211_regdom();
  920. }
  921. static unsigned int
  922. reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
  923. const struct ieee80211_reg_rule *rule)
  924. {
  925. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  926. const struct ieee80211_freq_range *freq_range_tmp;
  927. const struct ieee80211_reg_rule *tmp;
  928. u32 start_freq, end_freq, idx, no;
  929. for (idx = 0; idx < rd->n_reg_rules; idx++)
  930. if (rule == &rd->reg_rules[idx])
  931. break;
  932. if (idx == rd->n_reg_rules)
  933. return 0;
  934. /* get start_freq */
  935. no = idx;
  936. while (no) {
  937. tmp = &rd->reg_rules[--no];
  938. freq_range_tmp = &tmp->freq_range;
  939. if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
  940. break;
  941. freq_range = freq_range_tmp;
  942. }
  943. start_freq = freq_range->start_freq_khz;
  944. /* get end_freq */
  945. freq_range = &rule->freq_range;
  946. no = idx;
  947. while (no < rd->n_reg_rules - 1) {
  948. tmp = &rd->reg_rules[++no];
  949. freq_range_tmp = &tmp->freq_range;
  950. if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
  951. break;
  952. freq_range = freq_range_tmp;
  953. }
  954. end_freq = freq_range->end_freq_khz;
  955. return end_freq - start_freq;
  956. }
  957. unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
  958. const struct ieee80211_reg_rule *rule)
  959. {
  960. unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
  961. if (rule->flags & NL80211_RRF_NO_160MHZ)
  962. bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
  963. if (rule->flags & NL80211_RRF_NO_80MHZ)
  964. bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
  965. /*
  966. * HT40+/HT40- limits are handled per-channel. Only limit BW if both
  967. * are not allowed.
  968. */
  969. if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
  970. rule->flags & NL80211_RRF_NO_HT40PLUS)
  971. bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
  972. return bw;
  973. }
  974. /* Sanity check on a regulatory rule */
  975. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  976. {
  977. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  978. u32 freq_diff;
  979. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  980. return false;
  981. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  982. return false;
  983. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  984. if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
  985. freq_range->max_bandwidth_khz > freq_diff)
  986. return false;
  987. return true;
  988. }
  989. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  990. {
  991. const struct ieee80211_reg_rule *reg_rule = NULL;
  992. unsigned int i;
  993. if (!rd->n_reg_rules)
  994. return false;
  995. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  996. return false;
  997. for (i = 0; i < rd->n_reg_rules; i++) {
  998. reg_rule = &rd->reg_rules[i];
  999. if (!is_valid_reg_rule(reg_rule))
  1000. return false;
  1001. }
  1002. return true;
  1003. }
  1004. /**
  1005. * freq_in_rule_band - tells us if a frequency is in a frequency band
  1006. * @freq_range: frequency rule we want to query
  1007. * @freq_khz: frequency we are inquiring about
  1008. *
  1009. * This lets us know if a specific frequency rule is or is not relevant to
  1010. * a specific frequency's band. Bands are device specific and artificial
  1011. * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
  1012. * however it is safe for now to assume that a frequency rule should not be
  1013. * part of a frequency's band if the start freq or end freq are off by more
  1014. * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 20 GHz for the
  1015. * 60 GHz band.
  1016. * This resolution can be lowered and should be considered as we add
  1017. * regulatory rule support for other "bands".
  1018. **/
  1019. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  1020. u32 freq_khz)
  1021. {
  1022. #define ONE_GHZ_IN_KHZ 1000000
  1023. /*
  1024. * From 802.11ad: directional multi-gigabit (DMG):
  1025. * Pertaining to operation in a frequency band containing a channel
  1026. * with the Channel starting frequency above 45 GHz.
  1027. */
  1028. u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
  1029. 20 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
  1030. if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
  1031. return true;
  1032. if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
  1033. return true;
  1034. return false;
  1035. #undef ONE_GHZ_IN_KHZ
  1036. }
  1037. /*
  1038. * Later on we can perhaps use the more restrictive DFS
  1039. * region but we don't have information for that yet so
  1040. * for now simply disallow conflicts.
  1041. */
  1042. static enum nl80211_dfs_regions
  1043. reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
  1044. const enum nl80211_dfs_regions dfs_region2)
  1045. {
  1046. if (dfs_region1 != dfs_region2)
  1047. return NL80211_DFS_UNSET;
  1048. return dfs_region1;
  1049. }
  1050. static void reg_wmm_rules_intersect(const struct ieee80211_wmm_ac *wmm_ac1,
  1051. const struct ieee80211_wmm_ac *wmm_ac2,
  1052. struct ieee80211_wmm_ac *intersect)
  1053. {
  1054. intersect->cw_min = max_t(u16, wmm_ac1->cw_min, wmm_ac2->cw_min);
  1055. intersect->cw_max = max_t(u16, wmm_ac1->cw_max, wmm_ac2->cw_max);
  1056. intersect->cot = min_t(u16, wmm_ac1->cot, wmm_ac2->cot);
  1057. intersect->aifsn = max_t(u8, wmm_ac1->aifsn, wmm_ac2->aifsn);
  1058. }
  1059. /*
  1060. * Helper for regdom_intersect(), this does the real
  1061. * mathematical intersection fun
  1062. */
  1063. static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
  1064. const struct ieee80211_regdomain *rd2,
  1065. const struct ieee80211_reg_rule *rule1,
  1066. const struct ieee80211_reg_rule *rule2,
  1067. struct ieee80211_reg_rule *intersected_rule)
  1068. {
  1069. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  1070. struct ieee80211_freq_range *freq_range;
  1071. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  1072. struct ieee80211_power_rule *power_rule;
  1073. const struct ieee80211_wmm_rule *wmm_rule1, *wmm_rule2;
  1074. struct ieee80211_wmm_rule *wmm_rule;
  1075. u32 freq_diff, max_bandwidth1, max_bandwidth2;
  1076. freq_range1 = &rule1->freq_range;
  1077. freq_range2 = &rule2->freq_range;
  1078. freq_range = &intersected_rule->freq_range;
  1079. power_rule1 = &rule1->power_rule;
  1080. power_rule2 = &rule2->power_rule;
  1081. power_rule = &intersected_rule->power_rule;
  1082. wmm_rule1 = &rule1->wmm_rule;
  1083. wmm_rule2 = &rule2->wmm_rule;
  1084. wmm_rule = &intersected_rule->wmm_rule;
  1085. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  1086. freq_range2->start_freq_khz);
  1087. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  1088. freq_range2->end_freq_khz);
  1089. max_bandwidth1 = freq_range1->max_bandwidth_khz;
  1090. max_bandwidth2 = freq_range2->max_bandwidth_khz;
  1091. if (rule1->flags & NL80211_RRF_AUTO_BW)
  1092. max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
  1093. if (rule2->flags & NL80211_RRF_AUTO_BW)
  1094. max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
  1095. freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
  1096. intersected_rule->flags = rule1->flags | rule2->flags;
  1097. /*
  1098. * In case NL80211_RRF_AUTO_BW requested for both rules
  1099. * set AUTO_BW in intersected rule also. Next we will
  1100. * calculate BW correctly in handle_channel function.
  1101. * In other case remove AUTO_BW flag while we calculate
  1102. * maximum bandwidth correctly and auto calculation is
  1103. * not required.
  1104. */
  1105. if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
  1106. (rule2->flags & NL80211_RRF_AUTO_BW))
  1107. intersected_rule->flags |= NL80211_RRF_AUTO_BW;
  1108. else
  1109. intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
  1110. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  1111. if (freq_range->max_bandwidth_khz > freq_diff)
  1112. freq_range->max_bandwidth_khz = freq_diff;
  1113. power_rule->max_eirp = min(power_rule1->max_eirp,
  1114. power_rule2->max_eirp);
  1115. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  1116. power_rule2->max_antenna_gain);
  1117. intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
  1118. rule2->dfs_cac_ms);
  1119. if (rule1->has_wmm && rule2->has_wmm) {
  1120. u8 ac;
  1121. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1122. reg_wmm_rules_intersect(&wmm_rule1->client[ac],
  1123. &wmm_rule2->client[ac],
  1124. &wmm_rule->client[ac]);
  1125. reg_wmm_rules_intersect(&wmm_rule1->ap[ac],
  1126. &wmm_rule2->ap[ac],
  1127. &wmm_rule->ap[ac]);
  1128. }
  1129. intersected_rule->has_wmm = true;
  1130. } else if (rule1->has_wmm) {
  1131. *wmm_rule = *wmm_rule1;
  1132. intersected_rule->has_wmm = true;
  1133. } else if (rule2->has_wmm) {
  1134. *wmm_rule = *wmm_rule2;
  1135. intersected_rule->has_wmm = true;
  1136. } else {
  1137. intersected_rule->has_wmm = false;
  1138. }
  1139. if (!is_valid_reg_rule(intersected_rule))
  1140. return -EINVAL;
  1141. return 0;
  1142. }
  1143. /* check whether old rule contains new rule */
  1144. static bool rule_contains(struct ieee80211_reg_rule *r1,
  1145. struct ieee80211_reg_rule *r2)
  1146. {
  1147. /* for simplicity, currently consider only same flags */
  1148. if (r1->flags != r2->flags)
  1149. return false;
  1150. /* verify r1 is more restrictive */
  1151. if ((r1->power_rule.max_antenna_gain >
  1152. r2->power_rule.max_antenna_gain) ||
  1153. r1->power_rule.max_eirp > r2->power_rule.max_eirp)
  1154. return false;
  1155. /* make sure r2's range is contained within r1 */
  1156. if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
  1157. r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
  1158. return false;
  1159. /* and finally verify that r1.max_bw >= r2.max_bw */
  1160. if (r1->freq_range.max_bandwidth_khz <
  1161. r2->freq_range.max_bandwidth_khz)
  1162. return false;
  1163. return true;
  1164. }
  1165. /* add or extend current rules. do nothing if rule is already contained */
  1166. static void add_rule(struct ieee80211_reg_rule *rule,
  1167. struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
  1168. {
  1169. struct ieee80211_reg_rule *tmp_rule;
  1170. int i;
  1171. for (i = 0; i < *n_rules; i++) {
  1172. tmp_rule = &reg_rules[i];
  1173. /* rule is already contained - do nothing */
  1174. if (rule_contains(tmp_rule, rule))
  1175. return;
  1176. /* extend rule if possible */
  1177. if (rule_contains(rule, tmp_rule)) {
  1178. memcpy(tmp_rule, rule, sizeof(*rule));
  1179. return;
  1180. }
  1181. }
  1182. memcpy(&reg_rules[*n_rules], rule, sizeof(*rule));
  1183. (*n_rules)++;
  1184. }
  1185. /**
  1186. * regdom_intersect - do the intersection between two regulatory domains
  1187. * @rd1: first regulatory domain
  1188. * @rd2: second regulatory domain
  1189. *
  1190. * Use this function to get the intersection between two regulatory domains.
  1191. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  1192. * as no one single alpha2 can represent this regulatory domain.
  1193. *
  1194. * Returns a pointer to the regulatory domain structure which will hold the
  1195. * resulting intersection of rules between rd1 and rd2. We will
  1196. * kzalloc() this structure for you.
  1197. */
  1198. static struct ieee80211_regdomain *
  1199. regdom_intersect(const struct ieee80211_regdomain *rd1,
  1200. const struct ieee80211_regdomain *rd2)
  1201. {
  1202. int r, size_of_regd;
  1203. unsigned int x, y;
  1204. unsigned int num_rules = 0;
  1205. const struct ieee80211_reg_rule *rule1, *rule2;
  1206. struct ieee80211_reg_rule intersected_rule;
  1207. struct ieee80211_regdomain *rd;
  1208. if (!rd1 || !rd2)
  1209. return NULL;
  1210. /*
  1211. * First we get a count of the rules we'll need, then we actually
  1212. * build them. This is to so we can malloc() and free() a
  1213. * regdomain once. The reason we use reg_rules_intersect() here
  1214. * is it will return -EINVAL if the rule computed makes no sense.
  1215. * All rules that do check out OK are valid.
  1216. */
  1217. for (x = 0; x < rd1->n_reg_rules; x++) {
  1218. rule1 = &rd1->reg_rules[x];
  1219. for (y = 0; y < rd2->n_reg_rules; y++) {
  1220. rule2 = &rd2->reg_rules[y];
  1221. if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
  1222. &intersected_rule))
  1223. num_rules++;
  1224. }
  1225. }
  1226. if (!num_rules)
  1227. return NULL;
  1228. size_of_regd = sizeof(struct ieee80211_regdomain) +
  1229. num_rules * sizeof(struct ieee80211_reg_rule);
  1230. rd = kzalloc(size_of_regd, GFP_KERNEL);
  1231. if (!rd)
  1232. return NULL;
  1233. for (x = 0; x < rd1->n_reg_rules; x++) {
  1234. rule1 = &rd1->reg_rules[x];
  1235. for (y = 0; y < rd2->n_reg_rules; y++) {
  1236. rule2 = &rd2->reg_rules[y];
  1237. r = reg_rules_intersect(rd1, rd2, rule1, rule2,
  1238. &intersected_rule);
  1239. /*
  1240. * No need to memset here the intersected rule here as
  1241. * we're not using the stack anymore
  1242. */
  1243. if (r)
  1244. continue;
  1245. add_rule(&intersected_rule, rd->reg_rules,
  1246. &rd->n_reg_rules);
  1247. }
  1248. }
  1249. rd->alpha2[0] = '9';
  1250. rd->alpha2[1] = '8';
  1251. rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
  1252. rd2->dfs_region);
  1253. return rd;
  1254. }
  1255. /*
  1256. * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  1257. * want to just have the channel structure use these
  1258. */
  1259. static u32 map_regdom_flags(u32 rd_flags)
  1260. {
  1261. u32 channel_flags = 0;
  1262. if (rd_flags & NL80211_RRF_NO_IR_ALL)
  1263. channel_flags |= IEEE80211_CHAN_NO_IR;
  1264. if (rd_flags & NL80211_RRF_DFS)
  1265. channel_flags |= IEEE80211_CHAN_RADAR;
  1266. if (rd_flags & NL80211_RRF_NO_OFDM)
  1267. channel_flags |= IEEE80211_CHAN_NO_OFDM;
  1268. if (rd_flags & NL80211_RRF_NO_OUTDOOR)
  1269. channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
  1270. if (rd_flags & NL80211_RRF_IR_CONCURRENT)
  1271. channel_flags |= IEEE80211_CHAN_IR_CONCURRENT;
  1272. if (rd_flags & NL80211_RRF_NO_HT40MINUS)
  1273. channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
  1274. if (rd_flags & NL80211_RRF_NO_HT40PLUS)
  1275. channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
  1276. if (rd_flags & NL80211_RRF_NO_80MHZ)
  1277. channel_flags |= IEEE80211_CHAN_NO_80MHZ;
  1278. if (rd_flags & NL80211_RRF_NO_160MHZ)
  1279. channel_flags |= IEEE80211_CHAN_NO_160MHZ;
  1280. return channel_flags;
  1281. }
  1282. static const struct ieee80211_reg_rule *
  1283. freq_reg_info_regd(u32 center_freq,
  1284. const struct ieee80211_regdomain *regd, u32 bw)
  1285. {
  1286. int i;
  1287. bool band_rule_found = false;
  1288. bool bw_fits = false;
  1289. if (!regd)
  1290. return ERR_PTR(-EINVAL);
  1291. for (i = 0; i < regd->n_reg_rules; i++) {
  1292. const struct ieee80211_reg_rule *rr;
  1293. const struct ieee80211_freq_range *fr = NULL;
  1294. rr = &regd->reg_rules[i];
  1295. fr = &rr->freq_range;
  1296. /*
  1297. * We only need to know if one frequency rule was
  1298. * was in center_freq's band, that's enough, so lets
  1299. * not overwrite it once found
  1300. */
  1301. if (!band_rule_found)
  1302. band_rule_found = freq_in_rule_band(fr, center_freq);
  1303. bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw);
  1304. if (band_rule_found && bw_fits)
  1305. return rr;
  1306. }
  1307. if (!band_rule_found)
  1308. return ERR_PTR(-ERANGE);
  1309. return ERR_PTR(-EINVAL);
  1310. }
  1311. static const struct ieee80211_reg_rule *
  1312. __freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw)
  1313. {
  1314. const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy);
  1315. const struct ieee80211_reg_rule *reg_rule = NULL;
  1316. u32 bw;
  1317. for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
  1318. reg_rule = freq_reg_info_regd(center_freq, regd, bw);
  1319. if (!IS_ERR(reg_rule))
  1320. return reg_rule;
  1321. }
  1322. return reg_rule;
  1323. }
  1324. const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
  1325. u32 center_freq)
  1326. {
  1327. return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(20));
  1328. }
  1329. EXPORT_SYMBOL(freq_reg_info);
  1330. const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
  1331. {
  1332. switch (initiator) {
  1333. case NL80211_REGDOM_SET_BY_CORE:
  1334. return "core";
  1335. case NL80211_REGDOM_SET_BY_USER:
  1336. return "user";
  1337. case NL80211_REGDOM_SET_BY_DRIVER:
  1338. return "driver";
  1339. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1340. return "country element";
  1341. default:
  1342. WARN_ON(1);
  1343. return "bug";
  1344. }
  1345. }
  1346. EXPORT_SYMBOL(reg_initiator_name);
  1347. static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd,
  1348. const struct ieee80211_reg_rule *reg_rule,
  1349. const struct ieee80211_channel *chan)
  1350. {
  1351. const struct ieee80211_freq_range *freq_range = NULL;
  1352. u32 max_bandwidth_khz, bw_flags = 0;
  1353. freq_range = &reg_rule->freq_range;
  1354. max_bandwidth_khz = freq_range->max_bandwidth_khz;
  1355. /* Check if auto calculation requested */
  1356. if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  1357. max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
  1358. /* If we get a reg_rule we can assume that at least 5Mhz fit */
  1359. if (!cfg80211_does_bw_fit_range(freq_range,
  1360. MHZ_TO_KHZ(chan->center_freq),
  1361. MHZ_TO_KHZ(10)))
  1362. bw_flags |= IEEE80211_CHAN_NO_10MHZ;
  1363. if (!cfg80211_does_bw_fit_range(freq_range,
  1364. MHZ_TO_KHZ(chan->center_freq),
  1365. MHZ_TO_KHZ(20)))
  1366. bw_flags |= IEEE80211_CHAN_NO_20MHZ;
  1367. if (max_bandwidth_khz < MHZ_TO_KHZ(10))
  1368. bw_flags |= IEEE80211_CHAN_NO_10MHZ;
  1369. if (max_bandwidth_khz < MHZ_TO_KHZ(20))
  1370. bw_flags |= IEEE80211_CHAN_NO_20MHZ;
  1371. if (max_bandwidth_khz < MHZ_TO_KHZ(40))
  1372. bw_flags |= IEEE80211_CHAN_NO_HT40;
  1373. if (max_bandwidth_khz < MHZ_TO_KHZ(80))
  1374. bw_flags |= IEEE80211_CHAN_NO_80MHZ;
  1375. if (max_bandwidth_khz < MHZ_TO_KHZ(160))
  1376. bw_flags |= IEEE80211_CHAN_NO_160MHZ;
  1377. return bw_flags;
  1378. }
  1379. /*
  1380. * Note that right now we assume the desired channel bandwidth
  1381. * is always 20 MHz for each individual channel (HT40 uses 20 MHz
  1382. * per channel, the primary and the extension channel).
  1383. */
  1384. static void handle_channel(struct wiphy *wiphy,
  1385. enum nl80211_reg_initiator initiator,
  1386. struct ieee80211_channel *chan)
  1387. {
  1388. u32 flags, bw_flags = 0;
  1389. const struct ieee80211_reg_rule *reg_rule = NULL;
  1390. const struct ieee80211_power_rule *power_rule = NULL;
  1391. struct wiphy *request_wiphy = NULL;
  1392. struct regulatory_request *lr = get_last_request();
  1393. const struct ieee80211_regdomain *regd;
  1394. request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
  1395. flags = chan->orig_flags;
  1396. reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
  1397. if (IS_ERR(reg_rule)) {
  1398. /*
  1399. * We will disable all channels that do not match our
  1400. * received regulatory rule unless the hint is coming
  1401. * from a Country IE and the Country IE had no information
  1402. * about a band. The IEEE 802.11 spec allows for an AP
  1403. * to send only a subset of the regulatory rules allowed,
  1404. * so an AP in the US that only supports 2.4 GHz may only send
  1405. * a country IE with information for the 2.4 GHz band
  1406. * while 5 GHz is still supported.
  1407. */
  1408. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1409. PTR_ERR(reg_rule) == -ERANGE)
  1410. return;
  1411. if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1412. request_wiphy && request_wiphy == wiphy &&
  1413. request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
  1414. pr_debug("Disabling freq %d MHz for good\n",
  1415. chan->center_freq);
  1416. chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  1417. chan->flags = chan->orig_flags;
  1418. } else {
  1419. pr_debug("Disabling freq %d MHz\n",
  1420. chan->center_freq);
  1421. chan->flags |= IEEE80211_CHAN_DISABLED;
  1422. }
  1423. return;
  1424. }
  1425. regd = reg_get_regdomain(wiphy);
  1426. power_rule = &reg_rule->power_rule;
  1427. bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
  1428. if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1429. request_wiphy && request_wiphy == wiphy &&
  1430. request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
  1431. /*
  1432. * This guarantees the driver's requested regulatory domain
  1433. * will always be used as a base for further regulatory
  1434. * settings
  1435. */
  1436. chan->flags = chan->orig_flags =
  1437. map_regdom_flags(reg_rule->flags) | bw_flags;
  1438. chan->max_antenna_gain = chan->orig_mag =
  1439. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1440. chan->max_reg_power = chan->max_power = chan->orig_mpwr =
  1441. (int) MBM_TO_DBM(power_rule->max_eirp);
  1442. if (chan->flags & IEEE80211_CHAN_RADAR) {
  1443. chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
  1444. if (reg_rule->dfs_cac_ms)
  1445. chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
  1446. }
  1447. return;
  1448. }
  1449. chan->dfs_state = NL80211_DFS_USABLE;
  1450. chan->dfs_state_entered = jiffies;
  1451. chan->beacon_found = false;
  1452. chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
  1453. chan->max_antenna_gain =
  1454. min_t(int, chan->orig_mag,
  1455. MBI_TO_DBI(power_rule->max_antenna_gain));
  1456. chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  1457. if (chan->flags & IEEE80211_CHAN_RADAR) {
  1458. if (reg_rule->dfs_cac_ms)
  1459. chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
  1460. else
  1461. chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
  1462. }
  1463. if (chan->orig_mpwr) {
  1464. /*
  1465. * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
  1466. * will always follow the passed country IE power settings.
  1467. */
  1468. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1469. wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
  1470. chan->max_power = chan->max_reg_power;
  1471. else
  1472. chan->max_power = min(chan->orig_mpwr,
  1473. chan->max_reg_power);
  1474. } else
  1475. chan->max_power = chan->max_reg_power;
  1476. }
  1477. static void handle_band(struct wiphy *wiphy,
  1478. enum nl80211_reg_initiator initiator,
  1479. struct ieee80211_supported_band *sband)
  1480. {
  1481. unsigned int i;
  1482. if (!sband)
  1483. return;
  1484. for (i = 0; i < sband->n_channels; i++)
  1485. handle_channel(wiphy, initiator, &sband->channels[i]);
  1486. }
  1487. static bool reg_request_cell_base(struct regulatory_request *request)
  1488. {
  1489. if (request->initiator != NL80211_REGDOM_SET_BY_USER)
  1490. return false;
  1491. return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
  1492. }
  1493. bool reg_last_request_cell_base(void)
  1494. {
  1495. return reg_request_cell_base(get_last_request());
  1496. }
  1497. #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
  1498. /* Core specific check */
  1499. static enum reg_request_treatment
  1500. reg_ignore_cell_hint(struct regulatory_request *pending_request)
  1501. {
  1502. struct regulatory_request *lr = get_last_request();
  1503. if (!reg_num_devs_support_basehint)
  1504. return REG_REQ_IGNORE;
  1505. if (reg_request_cell_base(lr) &&
  1506. !regdom_changes(pending_request->alpha2))
  1507. return REG_REQ_ALREADY_SET;
  1508. return REG_REQ_OK;
  1509. }
  1510. /* Device specific check */
  1511. static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
  1512. {
  1513. return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
  1514. }
  1515. #else
  1516. static enum reg_request_treatment
  1517. reg_ignore_cell_hint(struct regulatory_request *pending_request)
  1518. {
  1519. return REG_REQ_IGNORE;
  1520. }
  1521. static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
  1522. {
  1523. return true;
  1524. }
  1525. #endif
  1526. static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
  1527. {
  1528. if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
  1529. !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
  1530. return true;
  1531. return false;
  1532. }
  1533. static bool ignore_reg_update(struct wiphy *wiphy,
  1534. enum nl80211_reg_initiator initiator)
  1535. {
  1536. struct regulatory_request *lr = get_last_request();
  1537. if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
  1538. return true;
  1539. if (!lr) {
  1540. pr_debug("Ignoring regulatory request set by %s since last_request is not set\n",
  1541. reg_initiator_name(initiator));
  1542. return true;
  1543. }
  1544. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  1545. wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
  1546. pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n",
  1547. reg_initiator_name(initiator));
  1548. return true;
  1549. }
  1550. /*
  1551. * wiphy->regd will be set once the device has its own
  1552. * desired regulatory domain set
  1553. */
  1554. if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
  1555. initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1556. !is_world_regdom(lr->alpha2)) {
  1557. pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n",
  1558. reg_initiator_name(initiator));
  1559. return true;
  1560. }
  1561. if (reg_request_cell_base(lr))
  1562. return reg_dev_ignore_cell_hint(wiphy);
  1563. return false;
  1564. }
  1565. static bool reg_is_world_roaming(struct wiphy *wiphy)
  1566. {
  1567. const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
  1568. const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
  1569. struct regulatory_request *lr = get_last_request();
  1570. if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
  1571. return true;
  1572. if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1573. wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
  1574. return true;
  1575. return false;
  1576. }
  1577. static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
  1578. struct reg_beacon *reg_beacon)
  1579. {
  1580. struct ieee80211_supported_band *sband;
  1581. struct ieee80211_channel *chan;
  1582. bool channel_changed = false;
  1583. struct ieee80211_channel chan_before;
  1584. sband = wiphy->bands[reg_beacon->chan.band];
  1585. chan = &sband->channels[chan_idx];
  1586. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  1587. return;
  1588. if (chan->beacon_found)
  1589. return;
  1590. chan->beacon_found = true;
  1591. if (!reg_is_world_roaming(wiphy))
  1592. return;
  1593. if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
  1594. return;
  1595. chan_before = *chan;
  1596. if (chan->flags & IEEE80211_CHAN_NO_IR) {
  1597. chan->flags &= ~IEEE80211_CHAN_NO_IR;
  1598. channel_changed = true;
  1599. }
  1600. if (channel_changed)
  1601. nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
  1602. }
  1603. /*
  1604. * Called when a scan on a wiphy finds a beacon on
  1605. * new channel
  1606. */
  1607. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  1608. struct reg_beacon *reg_beacon)
  1609. {
  1610. unsigned int i;
  1611. struct ieee80211_supported_band *sband;
  1612. if (!wiphy->bands[reg_beacon->chan.band])
  1613. return;
  1614. sband = wiphy->bands[reg_beacon->chan.band];
  1615. for (i = 0; i < sband->n_channels; i++)
  1616. handle_reg_beacon(wiphy, i, reg_beacon);
  1617. }
  1618. /*
  1619. * Called upon reg changes or a new wiphy is added
  1620. */
  1621. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  1622. {
  1623. unsigned int i;
  1624. struct ieee80211_supported_band *sband;
  1625. struct reg_beacon *reg_beacon;
  1626. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  1627. if (!wiphy->bands[reg_beacon->chan.band])
  1628. continue;
  1629. sband = wiphy->bands[reg_beacon->chan.band];
  1630. for (i = 0; i < sband->n_channels; i++)
  1631. handle_reg_beacon(wiphy, i, reg_beacon);
  1632. }
  1633. }
  1634. /* Reap the advantages of previously found beacons */
  1635. static void reg_process_beacons(struct wiphy *wiphy)
  1636. {
  1637. /*
  1638. * Means we are just firing up cfg80211, so no beacons would
  1639. * have been processed yet.
  1640. */
  1641. if (!last_request)
  1642. return;
  1643. wiphy_update_beacon_reg(wiphy);
  1644. }
  1645. static bool is_ht40_allowed(struct ieee80211_channel *chan)
  1646. {
  1647. if (!chan)
  1648. return false;
  1649. if (chan->flags & IEEE80211_CHAN_DISABLED)
  1650. return false;
  1651. /* This would happen when regulatory rules disallow HT40 completely */
  1652. if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
  1653. return false;
  1654. return true;
  1655. }
  1656. static void reg_process_ht_flags_channel(struct wiphy *wiphy,
  1657. struct ieee80211_channel *channel)
  1658. {
  1659. struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
  1660. struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  1661. const struct ieee80211_regdomain *regd;
  1662. unsigned int i;
  1663. u32 flags;
  1664. if (!is_ht40_allowed(channel)) {
  1665. channel->flags |= IEEE80211_CHAN_NO_HT40;
  1666. return;
  1667. }
  1668. /*
  1669. * We need to ensure the extension channels exist to
  1670. * be able to use HT40- or HT40+, this finds them (or not)
  1671. */
  1672. for (i = 0; i < sband->n_channels; i++) {
  1673. struct ieee80211_channel *c = &sband->channels[i];
  1674. if (c->center_freq == (channel->center_freq - 20))
  1675. channel_before = c;
  1676. if (c->center_freq == (channel->center_freq + 20))
  1677. channel_after = c;
  1678. }
  1679. flags = 0;
  1680. regd = get_wiphy_regdom(wiphy);
  1681. if (regd) {
  1682. const struct ieee80211_reg_rule *reg_rule =
  1683. freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq),
  1684. regd, MHZ_TO_KHZ(20));
  1685. if (!IS_ERR(reg_rule))
  1686. flags = reg_rule->flags;
  1687. }
  1688. /*
  1689. * Please note that this assumes target bandwidth is 20 MHz,
  1690. * if that ever changes we also need to change the below logic
  1691. * to include that as well.
  1692. */
  1693. if (!is_ht40_allowed(channel_before) ||
  1694. flags & NL80211_RRF_NO_HT40MINUS)
  1695. channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
  1696. else
  1697. channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  1698. if (!is_ht40_allowed(channel_after) ||
  1699. flags & NL80211_RRF_NO_HT40PLUS)
  1700. channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
  1701. else
  1702. channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  1703. }
  1704. static void reg_process_ht_flags_band(struct wiphy *wiphy,
  1705. struct ieee80211_supported_band *sband)
  1706. {
  1707. unsigned int i;
  1708. if (!sband)
  1709. return;
  1710. for (i = 0; i < sband->n_channels; i++)
  1711. reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
  1712. }
  1713. static void reg_process_ht_flags(struct wiphy *wiphy)
  1714. {
  1715. enum nl80211_band band;
  1716. if (!wiphy)
  1717. return;
  1718. for (band = 0; band < NUM_NL80211_BANDS; band++)
  1719. reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
  1720. }
  1721. static void reg_call_notifier(struct wiphy *wiphy,
  1722. struct regulatory_request *request)
  1723. {
  1724. if (wiphy->reg_notifier)
  1725. wiphy->reg_notifier(wiphy, request);
  1726. }
  1727. static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
  1728. {
  1729. struct cfg80211_chan_def chandef = {};
  1730. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1731. enum nl80211_iftype iftype;
  1732. wdev_lock(wdev);
  1733. iftype = wdev->iftype;
  1734. /* make sure the interface is active */
  1735. if (!wdev->netdev || !netif_running(wdev->netdev))
  1736. goto wdev_inactive_unlock;
  1737. switch (iftype) {
  1738. case NL80211_IFTYPE_AP:
  1739. case NL80211_IFTYPE_P2P_GO:
  1740. if (!wdev->beacon_interval)
  1741. goto wdev_inactive_unlock;
  1742. chandef = wdev->chandef;
  1743. break;
  1744. case NL80211_IFTYPE_ADHOC:
  1745. if (!wdev->ssid_len)
  1746. goto wdev_inactive_unlock;
  1747. chandef = wdev->chandef;
  1748. break;
  1749. case NL80211_IFTYPE_STATION:
  1750. case NL80211_IFTYPE_P2P_CLIENT:
  1751. if (!wdev->current_bss ||
  1752. !wdev->current_bss->pub.channel)
  1753. goto wdev_inactive_unlock;
  1754. if (!rdev->ops->get_channel ||
  1755. rdev_get_channel(rdev, wdev, &chandef))
  1756. cfg80211_chandef_create(&chandef,
  1757. wdev->current_bss->pub.channel,
  1758. NL80211_CHAN_NO_HT);
  1759. break;
  1760. case NL80211_IFTYPE_MONITOR:
  1761. case NL80211_IFTYPE_AP_VLAN:
  1762. case NL80211_IFTYPE_P2P_DEVICE:
  1763. /* no enforcement required */
  1764. break;
  1765. default:
  1766. /* others not implemented for now */
  1767. WARN_ON(1);
  1768. break;
  1769. }
  1770. wdev_unlock(wdev);
  1771. switch (iftype) {
  1772. case NL80211_IFTYPE_AP:
  1773. case NL80211_IFTYPE_P2P_GO:
  1774. case NL80211_IFTYPE_ADHOC:
  1775. return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype);
  1776. case NL80211_IFTYPE_STATION:
  1777. case NL80211_IFTYPE_P2P_CLIENT:
  1778. return cfg80211_chandef_usable(wiphy, &chandef,
  1779. IEEE80211_CHAN_DISABLED);
  1780. default:
  1781. break;
  1782. }
  1783. return true;
  1784. wdev_inactive_unlock:
  1785. wdev_unlock(wdev);
  1786. return true;
  1787. }
  1788. static void reg_leave_invalid_chans(struct wiphy *wiphy)
  1789. {
  1790. struct wireless_dev *wdev;
  1791. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1792. ASSERT_RTNL();
  1793. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
  1794. if (!reg_wdev_chan_valid(wiphy, wdev))
  1795. cfg80211_leave(rdev, wdev);
  1796. }
  1797. static void reg_check_chans_work(struct work_struct *work)
  1798. {
  1799. struct cfg80211_registered_device *rdev;
  1800. pr_debug("Verifying active interfaces after reg change\n");
  1801. rtnl_lock();
  1802. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1803. if (!(rdev->wiphy.regulatory_flags &
  1804. REGULATORY_IGNORE_STALE_KICKOFF))
  1805. reg_leave_invalid_chans(&rdev->wiphy);
  1806. rtnl_unlock();
  1807. }
  1808. static void reg_check_channels(void)
  1809. {
  1810. /*
  1811. * Give usermode a chance to do something nicer (move to another
  1812. * channel, orderly disconnection), before forcing a disconnection.
  1813. */
  1814. mod_delayed_work(system_power_efficient_wq,
  1815. &reg_check_chans,
  1816. msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
  1817. }
  1818. static void wiphy_update_regulatory(struct wiphy *wiphy,
  1819. enum nl80211_reg_initiator initiator)
  1820. {
  1821. enum nl80211_band band;
  1822. struct regulatory_request *lr = get_last_request();
  1823. if (ignore_reg_update(wiphy, initiator)) {
  1824. /*
  1825. * Regulatory updates set by CORE are ignored for custom
  1826. * regulatory cards. Let us notify the changes to the driver,
  1827. * as some drivers used this to restore its orig_* reg domain.
  1828. */
  1829. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  1830. wiphy->regulatory_flags & REGULATORY_CUSTOM_REG &&
  1831. !(wiphy->regulatory_flags &
  1832. REGULATORY_WIPHY_SELF_MANAGED))
  1833. reg_call_notifier(wiphy, lr);
  1834. return;
  1835. }
  1836. lr->dfs_region = get_cfg80211_regdom()->dfs_region;
  1837. for (band = 0; band < NUM_NL80211_BANDS; band++)
  1838. handle_band(wiphy, initiator, wiphy->bands[band]);
  1839. reg_process_beacons(wiphy);
  1840. reg_process_ht_flags(wiphy);
  1841. reg_call_notifier(wiphy, lr);
  1842. }
  1843. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  1844. {
  1845. struct cfg80211_registered_device *rdev;
  1846. struct wiphy *wiphy;
  1847. ASSERT_RTNL();
  1848. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  1849. wiphy = &rdev->wiphy;
  1850. wiphy_update_regulatory(wiphy, initiator);
  1851. }
  1852. reg_check_channels();
  1853. }
  1854. static void handle_channel_custom(struct wiphy *wiphy,
  1855. struct ieee80211_channel *chan,
  1856. const struct ieee80211_regdomain *regd,
  1857. u32 min_bw)
  1858. {
  1859. u32 bw_flags = 0;
  1860. const struct ieee80211_reg_rule *reg_rule = NULL;
  1861. const struct ieee80211_power_rule *power_rule = NULL;
  1862. u32 bw;
  1863. for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
  1864. reg_rule = freq_reg_info_regd(MHZ_TO_KHZ(chan->center_freq),
  1865. regd, bw);
  1866. if (!IS_ERR(reg_rule))
  1867. break;
  1868. }
  1869. if (IS_ERR_OR_NULL(reg_rule)) {
  1870. pr_debug("Disabling freq %d MHz as custom regd has no rule that fits it\n",
  1871. chan->center_freq);
  1872. if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
  1873. chan->flags |= IEEE80211_CHAN_DISABLED;
  1874. } else {
  1875. chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  1876. chan->flags = chan->orig_flags;
  1877. }
  1878. return;
  1879. }
  1880. power_rule = &reg_rule->power_rule;
  1881. bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
  1882. chan->dfs_state_entered = jiffies;
  1883. chan->dfs_state = NL80211_DFS_USABLE;
  1884. chan->beacon_found = false;
  1885. if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
  1886. chan->flags = chan->orig_flags | bw_flags |
  1887. map_regdom_flags(reg_rule->flags);
  1888. else
  1889. chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
  1890. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1891. chan->max_reg_power = chan->max_power =
  1892. (int) MBM_TO_DBM(power_rule->max_eirp);
  1893. if (chan->flags & IEEE80211_CHAN_RADAR) {
  1894. if (reg_rule->dfs_cac_ms)
  1895. chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
  1896. else
  1897. chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
  1898. }
  1899. chan->max_power = chan->max_reg_power;
  1900. }
  1901. static void handle_band_custom(struct wiphy *wiphy,
  1902. struct ieee80211_supported_band *sband,
  1903. const struct ieee80211_regdomain *regd)
  1904. {
  1905. unsigned int i;
  1906. if (!sband)
  1907. return;
  1908. /*
  1909. * We currently assume that you always want at least 20 MHz,
  1910. * otherwise channel 12 might get enabled if this rule is
  1911. * compatible to US, which permits 2402 - 2472 MHz.
  1912. */
  1913. for (i = 0; i < sband->n_channels; i++)
  1914. handle_channel_custom(wiphy, &sband->channels[i], regd,
  1915. MHZ_TO_KHZ(20));
  1916. }
  1917. /* Used by drivers prior to wiphy registration */
  1918. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  1919. const struct ieee80211_regdomain *regd)
  1920. {
  1921. enum nl80211_band band;
  1922. unsigned int bands_set = 0;
  1923. WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
  1924. "wiphy should have REGULATORY_CUSTOM_REG\n");
  1925. wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
  1926. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1927. if (!wiphy->bands[band])
  1928. continue;
  1929. handle_band_custom(wiphy, wiphy->bands[band], regd);
  1930. bands_set++;
  1931. }
  1932. /*
  1933. * no point in calling this if it won't have any effect
  1934. * on your device's supported bands.
  1935. */
  1936. WARN_ON(!bands_set);
  1937. }
  1938. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  1939. static void reg_set_request_processed(void)
  1940. {
  1941. bool need_more_processing = false;
  1942. struct regulatory_request *lr = get_last_request();
  1943. lr->processed = true;
  1944. spin_lock(&reg_requests_lock);
  1945. if (!list_empty(&reg_requests_list))
  1946. need_more_processing = true;
  1947. spin_unlock(&reg_requests_lock);
  1948. cancel_crda_timeout();
  1949. if (need_more_processing)
  1950. schedule_work(&reg_work);
  1951. }
  1952. /**
  1953. * reg_process_hint_core - process core regulatory requests
  1954. * @pending_request: a pending core regulatory request
  1955. *
  1956. * The wireless subsystem can use this function to process
  1957. * a regulatory request issued by the regulatory core.
  1958. */
  1959. static enum reg_request_treatment
  1960. reg_process_hint_core(struct regulatory_request *core_request)
  1961. {
  1962. if (reg_query_database(core_request)) {
  1963. core_request->intersect = false;
  1964. core_request->processed = false;
  1965. reg_update_last_request(core_request);
  1966. return REG_REQ_OK;
  1967. }
  1968. return REG_REQ_IGNORE;
  1969. }
  1970. static enum reg_request_treatment
  1971. __reg_process_hint_user(struct regulatory_request *user_request)
  1972. {
  1973. struct regulatory_request *lr = get_last_request();
  1974. if (reg_request_cell_base(user_request))
  1975. return reg_ignore_cell_hint(user_request);
  1976. if (reg_request_cell_base(lr))
  1977. return REG_REQ_IGNORE;
  1978. if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1979. return REG_REQ_INTERSECT;
  1980. /*
  1981. * If the user knows better the user should set the regdom
  1982. * to their country before the IE is picked up
  1983. */
  1984. if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
  1985. lr->intersect)
  1986. return REG_REQ_IGNORE;
  1987. /*
  1988. * Process user requests only after previous user/driver/core
  1989. * requests have been processed
  1990. */
  1991. if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1992. lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1993. lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
  1994. regdom_changes(lr->alpha2))
  1995. return REG_REQ_IGNORE;
  1996. if (!regdom_changes(user_request->alpha2))
  1997. return REG_REQ_ALREADY_SET;
  1998. return REG_REQ_OK;
  1999. }
  2000. /**
  2001. * reg_process_hint_user - process user regulatory requests
  2002. * @user_request: a pending user regulatory request
  2003. *
  2004. * The wireless subsystem can use this function to process
  2005. * a regulatory request initiated by userspace.
  2006. */
  2007. static enum reg_request_treatment
  2008. reg_process_hint_user(struct regulatory_request *user_request)
  2009. {
  2010. enum reg_request_treatment treatment;
  2011. treatment = __reg_process_hint_user(user_request);
  2012. if (treatment == REG_REQ_IGNORE ||
  2013. treatment == REG_REQ_ALREADY_SET)
  2014. return REG_REQ_IGNORE;
  2015. user_request->intersect = treatment == REG_REQ_INTERSECT;
  2016. user_request->processed = false;
  2017. if (reg_query_database(user_request)) {
  2018. reg_update_last_request(user_request);
  2019. user_alpha2[0] = user_request->alpha2[0];
  2020. user_alpha2[1] = user_request->alpha2[1];
  2021. return REG_REQ_OK;
  2022. }
  2023. return REG_REQ_IGNORE;
  2024. }
  2025. static enum reg_request_treatment
  2026. __reg_process_hint_driver(struct regulatory_request *driver_request)
  2027. {
  2028. struct regulatory_request *lr = get_last_request();
  2029. if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
  2030. if (regdom_changes(driver_request->alpha2))
  2031. return REG_REQ_OK;
  2032. return REG_REQ_ALREADY_SET;
  2033. }
  2034. /*
  2035. * This would happen if you unplug and plug your card
  2036. * back in or if you add a new device for which the previously
  2037. * loaded card also agrees on the regulatory domain.
  2038. */
  2039. if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  2040. !regdom_changes(driver_request->alpha2))
  2041. return REG_REQ_ALREADY_SET;
  2042. return REG_REQ_INTERSECT;
  2043. }
  2044. /**
  2045. * reg_process_hint_driver - process driver regulatory requests
  2046. * @driver_request: a pending driver regulatory request
  2047. *
  2048. * The wireless subsystem can use this function to process
  2049. * a regulatory request issued by an 802.11 driver.
  2050. *
  2051. * Returns one of the different reg request treatment values.
  2052. */
  2053. static enum reg_request_treatment
  2054. reg_process_hint_driver(struct wiphy *wiphy,
  2055. struct regulatory_request *driver_request)
  2056. {
  2057. const struct ieee80211_regdomain *regd, *tmp;
  2058. enum reg_request_treatment treatment;
  2059. treatment = __reg_process_hint_driver(driver_request);
  2060. switch (treatment) {
  2061. case REG_REQ_OK:
  2062. break;
  2063. case REG_REQ_IGNORE:
  2064. return REG_REQ_IGNORE;
  2065. case REG_REQ_INTERSECT:
  2066. case REG_REQ_ALREADY_SET:
  2067. regd = reg_copy_regd(get_cfg80211_regdom());
  2068. if (IS_ERR(regd))
  2069. return REG_REQ_IGNORE;
  2070. tmp = get_wiphy_regdom(wiphy);
  2071. rcu_assign_pointer(wiphy->regd, regd);
  2072. rcu_free_regdom(tmp);
  2073. }
  2074. driver_request->intersect = treatment == REG_REQ_INTERSECT;
  2075. driver_request->processed = false;
  2076. /*
  2077. * Since CRDA will not be called in this case as we already
  2078. * have applied the requested regulatory domain before we just
  2079. * inform userspace we have processed the request
  2080. */
  2081. if (treatment == REG_REQ_ALREADY_SET) {
  2082. nl80211_send_reg_change_event(driver_request);
  2083. reg_update_last_request(driver_request);
  2084. reg_set_request_processed();
  2085. return REG_REQ_ALREADY_SET;
  2086. }
  2087. if (reg_query_database(driver_request)) {
  2088. reg_update_last_request(driver_request);
  2089. return REG_REQ_OK;
  2090. }
  2091. return REG_REQ_IGNORE;
  2092. }
  2093. static enum reg_request_treatment
  2094. __reg_process_hint_country_ie(struct wiphy *wiphy,
  2095. struct regulatory_request *country_ie_request)
  2096. {
  2097. struct wiphy *last_wiphy = NULL;
  2098. struct regulatory_request *lr = get_last_request();
  2099. if (reg_request_cell_base(lr)) {
  2100. /* Trust a Cell base station over the AP's country IE */
  2101. if (regdom_changes(country_ie_request->alpha2))
  2102. return REG_REQ_IGNORE;
  2103. return REG_REQ_ALREADY_SET;
  2104. } else {
  2105. if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
  2106. return REG_REQ_IGNORE;
  2107. }
  2108. if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
  2109. return -EINVAL;
  2110. if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
  2111. return REG_REQ_OK;
  2112. last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
  2113. if (last_wiphy != wiphy) {
  2114. /*
  2115. * Two cards with two APs claiming different
  2116. * Country IE alpha2s. We could
  2117. * intersect them, but that seems unlikely
  2118. * to be correct. Reject second one for now.
  2119. */
  2120. if (regdom_changes(country_ie_request->alpha2))
  2121. return REG_REQ_IGNORE;
  2122. return REG_REQ_ALREADY_SET;
  2123. }
  2124. if (regdom_changes(country_ie_request->alpha2))
  2125. return REG_REQ_OK;
  2126. return REG_REQ_ALREADY_SET;
  2127. }
  2128. /**
  2129. * reg_process_hint_country_ie - process regulatory requests from country IEs
  2130. * @country_ie_request: a regulatory request from a country IE
  2131. *
  2132. * The wireless subsystem can use this function to process
  2133. * a regulatory request issued by a country Information Element.
  2134. *
  2135. * Returns one of the different reg request treatment values.
  2136. */
  2137. static enum reg_request_treatment
  2138. reg_process_hint_country_ie(struct wiphy *wiphy,
  2139. struct regulatory_request *country_ie_request)
  2140. {
  2141. enum reg_request_treatment treatment;
  2142. treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
  2143. switch (treatment) {
  2144. case REG_REQ_OK:
  2145. break;
  2146. case REG_REQ_IGNORE:
  2147. return REG_REQ_IGNORE;
  2148. case REG_REQ_ALREADY_SET:
  2149. reg_free_request(country_ie_request);
  2150. return REG_REQ_ALREADY_SET;
  2151. case REG_REQ_INTERSECT:
  2152. /*
  2153. * This doesn't happen yet, not sure we
  2154. * ever want to support it for this case.
  2155. */
  2156. WARN_ONCE(1, "Unexpected intersection for country elements");
  2157. return REG_REQ_IGNORE;
  2158. }
  2159. country_ie_request->intersect = false;
  2160. country_ie_request->processed = false;
  2161. if (reg_query_database(country_ie_request)) {
  2162. reg_update_last_request(country_ie_request);
  2163. return REG_REQ_OK;
  2164. }
  2165. return REG_REQ_IGNORE;
  2166. }
  2167. bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2)
  2168. {
  2169. const struct ieee80211_regdomain *wiphy1_regd = NULL;
  2170. const struct ieee80211_regdomain *wiphy2_regd = NULL;
  2171. const struct ieee80211_regdomain *cfg80211_regd = NULL;
  2172. bool dfs_domain_same;
  2173. rcu_read_lock();
  2174. cfg80211_regd = rcu_dereference(cfg80211_regdomain);
  2175. wiphy1_regd = rcu_dereference(wiphy1->regd);
  2176. if (!wiphy1_regd)
  2177. wiphy1_regd = cfg80211_regd;
  2178. wiphy2_regd = rcu_dereference(wiphy2->regd);
  2179. if (!wiphy2_regd)
  2180. wiphy2_regd = cfg80211_regd;
  2181. dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region;
  2182. rcu_read_unlock();
  2183. return dfs_domain_same;
  2184. }
  2185. static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan,
  2186. struct ieee80211_channel *src_chan)
  2187. {
  2188. if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) ||
  2189. !(src_chan->flags & IEEE80211_CHAN_RADAR))
  2190. return;
  2191. if (dst_chan->flags & IEEE80211_CHAN_DISABLED ||
  2192. src_chan->flags & IEEE80211_CHAN_DISABLED)
  2193. return;
  2194. if (src_chan->center_freq == dst_chan->center_freq &&
  2195. dst_chan->dfs_state == NL80211_DFS_USABLE) {
  2196. dst_chan->dfs_state = src_chan->dfs_state;
  2197. dst_chan->dfs_state_entered = src_chan->dfs_state_entered;
  2198. }
  2199. }
  2200. static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy,
  2201. struct wiphy *src_wiphy)
  2202. {
  2203. struct ieee80211_supported_band *src_sband, *dst_sband;
  2204. struct ieee80211_channel *src_chan, *dst_chan;
  2205. int i, j, band;
  2206. if (!reg_dfs_domain_same(dst_wiphy, src_wiphy))
  2207. return;
  2208. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  2209. dst_sband = dst_wiphy->bands[band];
  2210. src_sband = src_wiphy->bands[band];
  2211. if (!dst_sband || !src_sband)
  2212. continue;
  2213. for (i = 0; i < dst_sband->n_channels; i++) {
  2214. dst_chan = &dst_sband->channels[i];
  2215. for (j = 0; j < src_sband->n_channels; j++) {
  2216. src_chan = &src_sband->channels[j];
  2217. reg_copy_dfs_chan_state(dst_chan, src_chan);
  2218. }
  2219. }
  2220. }
  2221. }
  2222. static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy)
  2223. {
  2224. struct cfg80211_registered_device *rdev;
  2225. ASSERT_RTNL();
  2226. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  2227. if (wiphy == &rdev->wiphy)
  2228. continue;
  2229. wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy);
  2230. }
  2231. }
  2232. /* This processes *all* regulatory hints */
  2233. static void reg_process_hint(struct regulatory_request *reg_request)
  2234. {
  2235. struct wiphy *wiphy = NULL;
  2236. enum reg_request_treatment treatment;
  2237. enum nl80211_reg_initiator initiator = reg_request->initiator;
  2238. if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
  2239. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  2240. switch (initiator) {
  2241. case NL80211_REGDOM_SET_BY_CORE:
  2242. treatment = reg_process_hint_core(reg_request);
  2243. break;
  2244. case NL80211_REGDOM_SET_BY_USER:
  2245. treatment = reg_process_hint_user(reg_request);
  2246. break;
  2247. case NL80211_REGDOM_SET_BY_DRIVER:
  2248. if (!wiphy)
  2249. goto out_free;
  2250. treatment = reg_process_hint_driver(wiphy, reg_request);
  2251. break;
  2252. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  2253. if (!wiphy)
  2254. goto out_free;
  2255. treatment = reg_process_hint_country_ie(wiphy, reg_request);
  2256. break;
  2257. default:
  2258. WARN(1, "invalid initiator %d\n", initiator);
  2259. goto out_free;
  2260. }
  2261. if (treatment == REG_REQ_IGNORE)
  2262. goto out_free;
  2263. WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET,
  2264. "unexpected treatment value %d\n", treatment);
  2265. /* This is required so that the orig_* parameters are saved.
  2266. * NOTE: treatment must be set for any case that reaches here!
  2267. */
  2268. if (treatment == REG_REQ_ALREADY_SET && wiphy &&
  2269. wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
  2270. wiphy_update_regulatory(wiphy, initiator);
  2271. wiphy_all_share_dfs_chan_state(wiphy);
  2272. reg_check_channels();
  2273. }
  2274. return;
  2275. out_free:
  2276. reg_free_request(reg_request);
  2277. }
  2278. static void notify_self_managed_wiphys(struct regulatory_request *request)
  2279. {
  2280. struct cfg80211_registered_device *rdev;
  2281. struct wiphy *wiphy;
  2282. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  2283. wiphy = &rdev->wiphy;
  2284. if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
  2285. request->initiator == NL80211_REGDOM_SET_BY_USER &&
  2286. request->user_reg_hint_type ==
  2287. NL80211_USER_REG_HINT_CELL_BASE)
  2288. reg_call_notifier(wiphy, request);
  2289. }
  2290. }
  2291. /*
  2292. * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
  2293. * Regulatory hints come on a first come first serve basis and we
  2294. * must process each one atomically.
  2295. */
  2296. static void reg_process_pending_hints(void)
  2297. {
  2298. struct regulatory_request *reg_request, *lr;
  2299. lr = get_last_request();
  2300. /* When last_request->processed becomes true this will be rescheduled */
  2301. if (lr && !lr->processed) {
  2302. pr_debug("Pending regulatory request, waiting for it to be processed...\n");
  2303. return;
  2304. }
  2305. spin_lock(&reg_requests_lock);
  2306. if (list_empty(&reg_requests_list)) {
  2307. spin_unlock(&reg_requests_lock);
  2308. return;
  2309. }
  2310. reg_request = list_first_entry(&reg_requests_list,
  2311. struct regulatory_request,
  2312. list);
  2313. list_del_init(&reg_request->list);
  2314. spin_unlock(&reg_requests_lock);
  2315. notify_self_managed_wiphys(reg_request);
  2316. reg_process_hint(reg_request);
  2317. lr = get_last_request();
  2318. spin_lock(&reg_requests_lock);
  2319. if (!list_empty(&reg_requests_list) && lr && lr->processed)
  2320. schedule_work(&reg_work);
  2321. spin_unlock(&reg_requests_lock);
  2322. }
  2323. /* Processes beacon hints -- this has nothing to do with country IEs */
  2324. static void reg_process_pending_beacon_hints(void)
  2325. {
  2326. struct cfg80211_registered_device *rdev;
  2327. struct reg_beacon *pending_beacon, *tmp;
  2328. /* This goes through the _pending_ beacon list */
  2329. spin_lock_bh(&reg_pending_beacons_lock);
  2330. list_for_each_entry_safe(pending_beacon, tmp,
  2331. &reg_pending_beacons, list) {
  2332. list_del_init(&pending_beacon->list);
  2333. /* Applies the beacon hint to current wiphys */
  2334. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  2335. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  2336. /* Remembers the beacon hint for new wiphys or reg changes */
  2337. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  2338. }
  2339. spin_unlock_bh(&reg_pending_beacons_lock);
  2340. }
  2341. static void reg_process_self_managed_hints(void)
  2342. {
  2343. struct cfg80211_registered_device *rdev;
  2344. struct wiphy *wiphy;
  2345. const struct ieee80211_regdomain *tmp;
  2346. const struct ieee80211_regdomain *regd;
  2347. enum nl80211_band band;
  2348. struct regulatory_request request = {};
  2349. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  2350. wiphy = &rdev->wiphy;
  2351. spin_lock(&reg_requests_lock);
  2352. regd = rdev->requested_regd;
  2353. rdev->requested_regd = NULL;
  2354. spin_unlock(&reg_requests_lock);
  2355. if (regd == NULL)
  2356. continue;
  2357. tmp = get_wiphy_regdom(wiphy);
  2358. rcu_assign_pointer(wiphy->regd, regd);
  2359. rcu_free_regdom(tmp);
  2360. for (band = 0; band < NUM_NL80211_BANDS; band++)
  2361. handle_band_custom(wiphy, wiphy->bands[band], regd);
  2362. reg_process_ht_flags(wiphy);
  2363. request.wiphy_idx = get_wiphy_idx(wiphy);
  2364. request.alpha2[0] = regd->alpha2[0];
  2365. request.alpha2[1] = regd->alpha2[1];
  2366. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  2367. nl80211_send_wiphy_reg_change_event(&request);
  2368. }
  2369. reg_check_channels();
  2370. }
  2371. static void reg_todo(struct work_struct *work)
  2372. {
  2373. rtnl_lock();
  2374. reg_process_pending_hints();
  2375. reg_process_pending_beacon_hints();
  2376. reg_process_self_managed_hints();
  2377. rtnl_unlock();
  2378. }
  2379. static void queue_regulatory_request(struct regulatory_request *request)
  2380. {
  2381. request->alpha2[0] = toupper(request->alpha2[0]);
  2382. request->alpha2[1] = toupper(request->alpha2[1]);
  2383. spin_lock(&reg_requests_lock);
  2384. list_add_tail(&request->list, &reg_requests_list);
  2385. spin_unlock(&reg_requests_lock);
  2386. schedule_work(&reg_work);
  2387. }
  2388. /*
  2389. * Core regulatory hint -- happens during cfg80211_init()
  2390. * and when we restore regulatory settings.
  2391. */
  2392. static int regulatory_hint_core(const char *alpha2)
  2393. {
  2394. struct regulatory_request *request;
  2395. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  2396. if (!request)
  2397. return -ENOMEM;
  2398. request->alpha2[0] = alpha2[0];
  2399. request->alpha2[1] = alpha2[1];
  2400. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  2401. request->wiphy_idx = WIPHY_IDX_INVALID;
  2402. queue_regulatory_request(request);
  2403. return 0;
  2404. }
  2405. /* User hints */
  2406. int regulatory_hint_user(const char *alpha2,
  2407. enum nl80211_user_reg_hint_type user_reg_hint_type)
  2408. {
  2409. struct regulatory_request *request;
  2410. if (WARN_ON(!alpha2))
  2411. return -EINVAL;
  2412. if (!is_world_regdom(alpha2) && !is_an_alpha2(alpha2))
  2413. return -EINVAL;
  2414. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  2415. if (!request)
  2416. return -ENOMEM;
  2417. request->wiphy_idx = WIPHY_IDX_INVALID;
  2418. request->alpha2[0] = alpha2[0];
  2419. request->alpha2[1] = alpha2[1];
  2420. request->initiator = NL80211_REGDOM_SET_BY_USER;
  2421. request->user_reg_hint_type = user_reg_hint_type;
  2422. /* Allow calling CRDA again */
  2423. reset_crda_timeouts();
  2424. queue_regulatory_request(request);
  2425. return 0;
  2426. }
  2427. int regulatory_hint_indoor(bool is_indoor, u32 portid)
  2428. {
  2429. spin_lock(&reg_indoor_lock);
  2430. /* It is possible that more than one user space process is trying to
  2431. * configure the indoor setting. To handle such cases, clear the indoor
  2432. * setting in case that some process does not think that the device
  2433. * is operating in an indoor environment. In addition, if a user space
  2434. * process indicates that it is controlling the indoor setting, save its
  2435. * portid, i.e., make it the owner.
  2436. */
  2437. reg_is_indoor = is_indoor;
  2438. if (reg_is_indoor) {
  2439. if (!reg_is_indoor_portid)
  2440. reg_is_indoor_portid = portid;
  2441. } else {
  2442. reg_is_indoor_portid = 0;
  2443. }
  2444. spin_unlock(&reg_indoor_lock);
  2445. if (!is_indoor)
  2446. reg_check_channels();
  2447. return 0;
  2448. }
  2449. void regulatory_netlink_notify(u32 portid)
  2450. {
  2451. spin_lock(&reg_indoor_lock);
  2452. if (reg_is_indoor_portid != portid) {
  2453. spin_unlock(&reg_indoor_lock);
  2454. return;
  2455. }
  2456. reg_is_indoor = false;
  2457. reg_is_indoor_portid = 0;
  2458. spin_unlock(&reg_indoor_lock);
  2459. reg_check_channels();
  2460. }
  2461. /* Driver hints */
  2462. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  2463. {
  2464. struct regulatory_request *request;
  2465. if (WARN_ON(!alpha2 || !wiphy))
  2466. return -EINVAL;
  2467. wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
  2468. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  2469. if (!request)
  2470. return -ENOMEM;
  2471. request->wiphy_idx = get_wiphy_idx(wiphy);
  2472. request->alpha2[0] = alpha2[0];
  2473. request->alpha2[1] = alpha2[1];
  2474. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  2475. /* Allow calling CRDA again */
  2476. reset_crda_timeouts();
  2477. queue_regulatory_request(request);
  2478. return 0;
  2479. }
  2480. EXPORT_SYMBOL(regulatory_hint);
  2481. void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band,
  2482. const u8 *country_ie, u8 country_ie_len)
  2483. {
  2484. char alpha2[2];
  2485. enum environment_cap env = ENVIRON_ANY;
  2486. struct regulatory_request *request = NULL, *lr;
  2487. /* IE len must be evenly divisible by 2 */
  2488. if (country_ie_len & 0x01)
  2489. return;
  2490. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  2491. return;
  2492. request = kzalloc(sizeof(*request), GFP_KERNEL);
  2493. if (!request)
  2494. return;
  2495. alpha2[0] = country_ie[0];
  2496. alpha2[1] = country_ie[1];
  2497. if (country_ie[2] == 'I')
  2498. env = ENVIRON_INDOOR;
  2499. else if (country_ie[2] == 'O')
  2500. env = ENVIRON_OUTDOOR;
  2501. rcu_read_lock();
  2502. lr = get_last_request();
  2503. if (unlikely(!lr))
  2504. goto out;
  2505. /*
  2506. * We will run this only upon a successful connection on cfg80211.
  2507. * We leave conflict resolution to the workqueue, where can hold
  2508. * the RTNL.
  2509. */
  2510. if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  2511. lr->wiphy_idx != WIPHY_IDX_INVALID)
  2512. goto out;
  2513. request->wiphy_idx = get_wiphy_idx(wiphy);
  2514. request->alpha2[0] = alpha2[0];
  2515. request->alpha2[1] = alpha2[1];
  2516. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  2517. request->country_ie_env = env;
  2518. /* Allow calling CRDA again */
  2519. reset_crda_timeouts();
  2520. queue_regulatory_request(request);
  2521. request = NULL;
  2522. out:
  2523. kfree(request);
  2524. rcu_read_unlock();
  2525. }
  2526. static void restore_alpha2(char *alpha2, bool reset_user)
  2527. {
  2528. /* indicates there is no alpha2 to consider for restoration */
  2529. alpha2[0] = '9';
  2530. alpha2[1] = '7';
  2531. /* The user setting has precedence over the module parameter */
  2532. if (is_user_regdom_saved()) {
  2533. /* Unless we're asked to ignore it and reset it */
  2534. if (reset_user) {
  2535. pr_debug("Restoring regulatory settings including user preference\n");
  2536. user_alpha2[0] = '9';
  2537. user_alpha2[1] = '7';
  2538. /*
  2539. * If we're ignoring user settings, we still need to
  2540. * check the module parameter to ensure we put things
  2541. * back as they were for a full restore.
  2542. */
  2543. if (!is_world_regdom(ieee80211_regdom)) {
  2544. pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
  2545. ieee80211_regdom[0], ieee80211_regdom[1]);
  2546. alpha2[0] = ieee80211_regdom[0];
  2547. alpha2[1] = ieee80211_regdom[1];
  2548. }
  2549. } else {
  2550. pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n",
  2551. user_alpha2[0], user_alpha2[1]);
  2552. alpha2[0] = user_alpha2[0];
  2553. alpha2[1] = user_alpha2[1];
  2554. }
  2555. } else if (!is_world_regdom(ieee80211_regdom)) {
  2556. pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
  2557. ieee80211_regdom[0], ieee80211_regdom[1]);
  2558. alpha2[0] = ieee80211_regdom[0];
  2559. alpha2[1] = ieee80211_regdom[1];
  2560. } else
  2561. pr_debug("Restoring regulatory settings\n");
  2562. }
  2563. static void restore_custom_reg_settings(struct wiphy *wiphy)
  2564. {
  2565. struct ieee80211_supported_band *sband;
  2566. enum nl80211_band band;
  2567. struct ieee80211_channel *chan;
  2568. int i;
  2569. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  2570. sband = wiphy->bands[band];
  2571. if (!sband)
  2572. continue;
  2573. for (i = 0; i < sband->n_channels; i++) {
  2574. chan = &sband->channels[i];
  2575. chan->flags = chan->orig_flags;
  2576. chan->max_antenna_gain = chan->orig_mag;
  2577. chan->max_power = chan->orig_mpwr;
  2578. chan->beacon_found = false;
  2579. }
  2580. }
  2581. }
  2582. /*
  2583. * Restoring regulatory settings involves ingoring any
  2584. * possibly stale country IE information and user regulatory
  2585. * settings if so desired, this includes any beacon hints
  2586. * learned as we could have traveled outside to another country
  2587. * after disconnection. To restore regulatory settings we do
  2588. * exactly what we did at bootup:
  2589. *
  2590. * - send a core regulatory hint
  2591. * - send a user regulatory hint if applicable
  2592. *
  2593. * Device drivers that send a regulatory hint for a specific country
  2594. * keep their own regulatory domain on wiphy->regd so that does does
  2595. * not need to be remembered.
  2596. */
  2597. static void restore_regulatory_settings(bool reset_user)
  2598. {
  2599. char alpha2[2];
  2600. char world_alpha2[2];
  2601. struct reg_beacon *reg_beacon, *btmp;
  2602. LIST_HEAD(tmp_reg_req_list);
  2603. struct cfg80211_registered_device *rdev;
  2604. ASSERT_RTNL();
  2605. /*
  2606. * Clear the indoor setting in case that it is not controlled by user
  2607. * space, as otherwise there is no guarantee that the device is still
  2608. * operating in an indoor environment.
  2609. */
  2610. spin_lock(&reg_indoor_lock);
  2611. if (reg_is_indoor && !reg_is_indoor_portid) {
  2612. reg_is_indoor = false;
  2613. reg_check_channels();
  2614. }
  2615. spin_unlock(&reg_indoor_lock);
  2616. reset_regdomains(true, &world_regdom);
  2617. restore_alpha2(alpha2, reset_user);
  2618. /*
  2619. * If there's any pending requests we simply
  2620. * stash them to a temporary pending queue and
  2621. * add then after we've restored regulatory
  2622. * settings.
  2623. */
  2624. spin_lock(&reg_requests_lock);
  2625. list_splice_tail_init(&reg_requests_list, &tmp_reg_req_list);
  2626. spin_unlock(&reg_requests_lock);
  2627. /* Clear beacon hints */
  2628. spin_lock_bh(&reg_pending_beacons_lock);
  2629. list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
  2630. list_del(&reg_beacon->list);
  2631. kfree(reg_beacon);
  2632. }
  2633. spin_unlock_bh(&reg_pending_beacons_lock);
  2634. list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
  2635. list_del(&reg_beacon->list);
  2636. kfree(reg_beacon);
  2637. }
  2638. /* First restore to the basic regulatory settings */
  2639. world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
  2640. world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
  2641. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  2642. if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
  2643. continue;
  2644. if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
  2645. restore_custom_reg_settings(&rdev->wiphy);
  2646. }
  2647. regulatory_hint_core(world_alpha2);
  2648. /*
  2649. * This restores the ieee80211_regdom module parameter
  2650. * preference or the last user requested regulatory
  2651. * settings, user regulatory settings takes precedence.
  2652. */
  2653. if (is_an_alpha2(alpha2))
  2654. regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER);
  2655. spin_lock(&reg_requests_lock);
  2656. list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
  2657. spin_unlock(&reg_requests_lock);
  2658. pr_debug("Kicking the queue\n");
  2659. schedule_work(&reg_work);
  2660. }
  2661. static bool is_wiphy_all_set_reg_flag(enum ieee80211_regulatory_flags flag)
  2662. {
  2663. struct cfg80211_registered_device *rdev;
  2664. struct wireless_dev *wdev;
  2665. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  2666. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  2667. wdev_lock(wdev);
  2668. if (!(wdev->wiphy->regulatory_flags & flag)) {
  2669. wdev_unlock(wdev);
  2670. return false;
  2671. }
  2672. wdev_unlock(wdev);
  2673. }
  2674. }
  2675. return true;
  2676. }
  2677. void regulatory_hint_disconnect(void)
  2678. {
  2679. /* Restore of regulatory settings is not required when wiphy(s)
  2680. * ignore IE from connected access point but clearance of beacon hints
  2681. * is required when wiphy(s) supports beacon hints.
  2682. */
  2683. if (is_wiphy_all_set_reg_flag(REGULATORY_COUNTRY_IE_IGNORE)) {
  2684. struct reg_beacon *reg_beacon, *btmp;
  2685. if (is_wiphy_all_set_reg_flag(REGULATORY_DISABLE_BEACON_HINTS))
  2686. return;
  2687. spin_lock_bh(&reg_pending_beacons_lock);
  2688. list_for_each_entry_safe(reg_beacon, btmp,
  2689. &reg_pending_beacons, list) {
  2690. list_del(&reg_beacon->list);
  2691. kfree(reg_beacon);
  2692. }
  2693. spin_unlock_bh(&reg_pending_beacons_lock);
  2694. list_for_each_entry_safe(reg_beacon, btmp,
  2695. &reg_beacon_list, list) {
  2696. list_del(&reg_beacon->list);
  2697. kfree(reg_beacon);
  2698. }
  2699. return;
  2700. }
  2701. pr_debug("All devices are disconnected, going to restore regulatory settings\n");
  2702. restore_regulatory_settings(false);
  2703. }
  2704. static bool freq_is_chan_12_13_14(u16 freq)
  2705. {
  2706. if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) ||
  2707. freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) ||
  2708. freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ))
  2709. return true;
  2710. return false;
  2711. }
  2712. static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
  2713. {
  2714. struct reg_beacon *pending_beacon;
  2715. list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
  2716. if (beacon_chan->center_freq ==
  2717. pending_beacon->chan.center_freq)
  2718. return true;
  2719. return false;
  2720. }
  2721. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  2722. struct ieee80211_channel *beacon_chan,
  2723. gfp_t gfp)
  2724. {
  2725. struct reg_beacon *reg_beacon;
  2726. bool processing;
  2727. if (beacon_chan->beacon_found ||
  2728. beacon_chan->flags & IEEE80211_CHAN_RADAR ||
  2729. (beacon_chan->band == NL80211_BAND_2GHZ &&
  2730. !freq_is_chan_12_13_14(beacon_chan->center_freq)))
  2731. return 0;
  2732. spin_lock_bh(&reg_pending_beacons_lock);
  2733. processing = pending_reg_beacon(beacon_chan);
  2734. spin_unlock_bh(&reg_pending_beacons_lock);
  2735. if (processing)
  2736. return 0;
  2737. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  2738. if (!reg_beacon)
  2739. return -ENOMEM;
  2740. pr_debug("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
  2741. beacon_chan->center_freq,
  2742. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  2743. wiphy_name(wiphy));
  2744. memcpy(&reg_beacon->chan, beacon_chan,
  2745. sizeof(struct ieee80211_channel));
  2746. /*
  2747. * Since we can be called from BH or and non-BH context
  2748. * we must use spin_lock_bh()
  2749. */
  2750. spin_lock_bh(&reg_pending_beacons_lock);
  2751. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  2752. spin_unlock_bh(&reg_pending_beacons_lock);
  2753. schedule_work(&reg_work);
  2754. return 0;
  2755. }
  2756. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  2757. {
  2758. unsigned int i;
  2759. const struct ieee80211_reg_rule *reg_rule = NULL;
  2760. const struct ieee80211_freq_range *freq_range = NULL;
  2761. const struct ieee80211_power_rule *power_rule = NULL;
  2762. char bw[32], cac_time[32];
  2763. pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
  2764. for (i = 0; i < rd->n_reg_rules; i++) {
  2765. reg_rule = &rd->reg_rules[i];
  2766. freq_range = &reg_rule->freq_range;
  2767. power_rule = &reg_rule->power_rule;
  2768. if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  2769. snprintf(bw, sizeof(bw), "%d KHz, %u KHz AUTO",
  2770. freq_range->max_bandwidth_khz,
  2771. reg_get_max_bandwidth(rd, reg_rule));
  2772. else
  2773. snprintf(bw, sizeof(bw), "%d KHz",
  2774. freq_range->max_bandwidth_khz);
  2775. if (reg_rule->flags & NL80211_RRF_DFS)
  2776. scnprintf(cac_time, sizeof(cac_time), "%u s",
  2777. reg_rule->dfs_cac_ms/1000);
  2778. else
  2779. scnprintf(cac_time, sizeof(cac_time), "N/A");
  2780. /*
  2781. * There may not be documentation for max antenna gain
  2782. * in certain regions
  2783. */
  2784. if (power_rule->max_antenna_gain)
  2785. pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
  2786. freq_range->start_freq_khz,
  2787. freq_range->end_freq_khz,
  2788. bw,
  2789. power_rule->max_antenna_gain,
  2790. power_rule->max_eirp,
  2791. cac_time);
  2792. else
  2793. pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
  2794. freq_range->start_freq_khz,
  2795. freq_range->end_freq_khz,
  2796. bw,
  2797. power_rule->max_eirp,
  2798. cac_time);
  2799. }
  2800. }
  2801. bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
  2802. {
  2803. switch (dfs_region) {
  2804. case NL80211_DFS_UNSET:
  2805. case NL80211_DFS_FCC:
  2806. case NL80211_DFS_ETSI:
  2807. case NL80211_DFS_JP:
  2808. return true;
  2809. default:
  2810. pr_debug("Ignoring unknown DFS master region: %d\n", dfs_region);
  2811. return false;
  2812. }
  2813. }
  2814. static void print_regdomain(const struct ieee80211_regdomain *rd)
  2815. {
  2816. struct regulatory_request *lr = get_last_request();
  2817. if (is_intersected_alpha2(rd->alpha2)) {
  2818. if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  2819. struct cfg80211_registered_device *rdev;
  2820. rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
  2821. if (rdev) {
  2822. pr_debug("Current regulatory domain updated by AP to: %c%c\n",
  2823. rdev->country_ie_alpha2[0],
  2824. rdev->country_ie_alpha2[1]);
  2825. } else
  2826. pr_debug("Current regulatory domain intersected:\n");
  2827. } else
  2828. pr_debug("Current regulatory domain intersected:\n");
  2829. } else if (is_world_regdom(rd->alpha2)) {
  2830. pr_debug("World regulatory domain updated:\n");
  2831. } else {
  2832. if (is_unknown_alpha2(rd->alpha2))
  2833. pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n");
  2834. else {
  2835. if (reg_request_cell_base(lr))
  2836. pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n",
  2837. rd->alpha2[0], rd->alpha2[1]);
  2838. else
  2839. pr_debug("Regulatory domain changed to country: %c%c\n",
  2840. rd->alpha2[0], rd->alpha2[1]);
  2841. }
  2842. }
  2843. pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
  2844. print_rd_rules(rd);
  2845. }
  2846. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  2847. {
  2848. pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
  2849. print_rd_rules(rd);
  2850. }
  2851. static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
  2852. {
  2853. if (!is_world_regdom(rd->alpha2))
  2854. return -EINVAL;
  2855. update_world_regdomain(rd);
  2856. return 0;
  2857. }
  2858. static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
  2859. struct regulatory_request *user_request)
  2860. {
  2861. const struct ieee80211_regdomain *intersected_rd = NULL;
  2862. if (!regdom_changes(rd->alpha2))
  2863. return -EALREADY;
  2864. if (!is_valid_rd(rd)) {
  2865. pr_err("Invalid regulatory domain detected: %c%c\n",
  2866. rd->alpha2[0], rd->alpha2[1]);
  2867. print_regdomain_info(rd);
  2868. return -EINVAL;
  2869. }
  2870. if (!user_request->intersect) {
  2871. reset_regdomains(false, rd);
  2872. return 0;
  2873. }
  2874. intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
  2875. if (!intersected_rd)
  2876. return -EINVAL;
  2877. kfree(rd);
  2878. rd = NULL;
  2879. reset_regdomains(false, intersected_rd);
  2880. return 0;
  2881. }
  2882. static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
  2883. struct regulatory_request *driver_request)
  2884. {
  2885. const struct ieee80211_regdomain *regd;
  2886. const struct ieee80211_regdomain *intersected_rd = NULL;
  2887. const struct ieee80211_regdomain *tmp;
  2888. struct wiphy *request_wiphy;
  2889. if (is_world_regdom(rd->alpha2))
  2890. return -EINVAL;
  2891. if (!regdom_changes(rd->alpha2))
  2892. return -EALREADY;
  2893. if (!is_valid_rd(rd)) {
  2894. pr_err("Invalid regulatory domain detected: %c%c\n",
  2895. rd->alpha2[0], rd->alpha2[1]);
  2896. print_regdomain_info(rd);
  2897. return -EINVAL;
  2898. }
  2899. request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
  2900. if (!request_wiphy)
  2901. return -ENODEV;
  2902. if (!driver_request->intersect) {
  2903. if (request_wiphy->regd)
  2904. return -EALREADY;
  2905. regd = reg_copy_regd(rd);
  2906. if (IS_ERR(regd))
  2907. return PTR_ERR(regd);
  2908. rcu_assign_pointer(request_wiphy->regd, regd);
  2909. reset_regdomains(false, rd);
  2910. return 0;
  2911. }
  2912. intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
  2913. if (!intersected_rd)
  2914. return -EINVAL;
  2915. /*
  2916. * We can trash what CRDA provided now.
  2917. * However if a driver requested this specific regulatory
  2918. * domain we keep it for its private use
  2919. */
  2920. tmp = get_wiphy_regdom(request_wiphy);
  2921. rcu_assign_pointer(request_wiphy->regd, rd);
  2922. rcu_free_regdom(tmp);
  2923. rd = NULL;
  2924. reset_regdomains(false, intersected_rd);
  2925. return 0;
  2926. }
  2927. static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
  2928. struct regulatory_request *country_ie_request)
  2929. {
  2930. struct wiphy *request_wiphy;
  2931. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  2932. !is_unknown_alpha2(rd->alpha2))
  2933. return -EINVAL;
  2934. /*
  2935. * Lets only bother proceeding on the same alpha2 if the current
  2936. * rd is non static (it means CRDA was present and was used last)
  2937. * and the pending request came in from a country IE
  2938. */
  2939. if (!is_valid_rd(rd)) {
  2940. pr_err("Invalid regulatory domain detected: %c%c\n",
  2941. rd->alpha2[0], rd->alpha2[1]);
  2942. print_regdomain_info(rd);
  2943. return -EINVAL;
  2944. }
  2945. request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
  2946. if (!request_wiphy)
  2947. return -ENODEV;
  2948. if (country_ie_request->intersect)
  2949. return -EINVAL;
  2950. reset_regdomains(false, rd);
  2951. return 0;
  2952. }
  2953. /*
  2954. * Use this call to set the current regulatory domain. Conflicts with
  2955. * multiple drivers can be ironed out later. Caller must've already
  2956. * kmalloc'd the rd structure.
  2957. */
  2958. int set_regdom(const struct ieee80211_regdomain *rd,
  2959. enum ieee80211_regd_source regd_src)
  2960. {
  2961. struct regulatory_request *lr;
  2962. bool user_reset = false;
  2963. int r;
  2964. if (!reg_is_valid_request(rd->alpha2)) {
  2965. kfree(rd);
  2966. return -EINVAL;
  2967. }
  2968. if (regd_src == REGD_SOURCE_CRDA)
  2969. reset_crda_timeouts();
  2970. lr = get_last_request();
  2971. /* Note that this doesn't update the wiphys, this is done below */
  2972. switch (lr->initiator) {
  2973. case NL80211_REGDOM_SET_BY_CORE:
  2974. r = reg_set_rd_core(rd);
  2975. break;
  2976. case NL80211_REGDOM_SET_BY_USER:
  2977. r = reg_set_rd_user(rd, lr);
  2978. user_reset = true;
  2979. break;
  2980. case NL80211_REGDOM_SET_BY_DRIVER:
  2981. r = reg_set_rd_driver(rd, lr);
  2982. break;
  2983. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  2984. r = reg_set_rd_country_ie(rd, lr);
  2985. break;
  2986. default:
  2987. WARN(1, "invalid initiator %d\n", lr->initiator);
  2988. kfree(rd);
  2989. return -EINVAL;
  2990. }
  2991. if (r) {
  2992. switch (r) {
  2993. case -EALREADY:
  2994. reg_set_request_processed();
  2995. break;
  2996. default:
  2997. /* Back to world regulatory in case of errors */
  2998. restore_regulatory_settings(user_reset);
  2999. }
  3000. kfree(rd);
  3001. return r;
  3002. }
  3003. /* This would make this whole thing pointless */
  3004. if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
  3005. return -EINVAL;
  3006. /* update all wiphys now with the new established regulatory domain */
  3007. update_all_wiphy_regulatory(lr->initiator);
  3008. print_regdomain(get_cfg80211_regdom());
  3009. nl80211_send_reg_change_event(lr);
  3010. reg_set_request_processed();
  3011. return 0;
  3012. }
  3013. static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
  3014. struct ieee80211_regdomain *rd)
  3015. {
  3016. const struct ieee80211_regdomain *regd;
  3017. const struct ieee80211_regdomain *prev_regd;
  3018. struct cfg80211_registered_device *rdev;
  3019. if (WARN_ON(!wiphy || !rd))
  3020. return -EINVAL;
  3021. if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
  3022. "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
  3023. return -EPERM;
  3024. if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
  3025. print_regdomain_info(rd);
  3026. return -EINVAL;
  3027. }
  3028. regd = reg_copy_regd(rd);
  3029. if (IS_ERR(regd))
  3030. return PTR_ERR(regd);
  3031. rdev = wiphy_to_rdev(wiphy);
  3032. spin_lock(&reg_requests_lock);
  3033. prev_regd = rdev->requested_regd;
  3034. rdev->requested_regd = regd;
  3035. spin_unlock(&reg_requests_lock);
  3036. kfree(prev_regd);
  3037. return 0;
  3038. }
  3039. int regulatory_set_wiphy_regd(struct wiphy *wiphy,
  3040. struct ieee80211_regdomain *rd)
  3041. {
  3042. int ret = __regulatory_set_wiphy_regd(wiphy, rd);
  3043. if (ret)
  3044. return ret;
  3045. schedule_work(&reg_work);
  3046. return 0;
  3047. }
  3048. EXPORT_SYMBOL(regulatory_set_wiphy_regd);
  3049. int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
  3050. struct ieee80211_regdomain *rd)
  3051. {
  3052. int ret;
  3053. ASSERT_RTNL();
  3054. ret = __regulatory_set_wiphy_regd(wiphy, rd);
  3055. if (ret)
  3056. return ret;
  3057. /* process the request immediately */
  3058. reg_process_self_managed_hints();
  3059. return 0;
  3060. }
  3061. EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
  3062. void wiphy_regulatory_register(struct wiphy *wiphy)
  3063. {
  3064. struct regulatory_request *lr = get_last_request();
  3065. /* self-managed devices ignore beacon hints and country IE */
  3066. if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
  3067. wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
  3068. REGULATORY_COUNTRY_IE_IGNORE;
  3069. /*
  3070. * The last request may have been received before this
  3071. * registration call. Call the driver notifier if
  3072. * initiator is USER and user type is CELL_BASE.
  3073. */
  3074. if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
  3075. lr->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE)
  3076. reg_call_notifier(wiphy, lr);
  3077. }
  3078. if (!reg_dev_ignore_cell_hint(wiphy))
  3079. reg_num_devs_support_basehint++;
  3080. wiphy_update_regulatory(wiphy, lr->initiator);
  3081. wiphy_all_share_dfs_chan_state(wiphy);
  3082. }
  3083. void wiphy_regulatory_deregister(struct wiphy *wiphy)
  3084. {
  3085. struct wiphy *request_wiphy = NULL;
  3086. struct regulatory_request *lr;
  3087. lr = get_last_request();
  3088. if (!reg_dev_ignore_cell_hint(wiphy))
  3089. reg_num_devs_support_basehint--;
  3090. rcu_free_regdom(get_wiphy_regdom(wiphy));
  3091. RCU_INIT_POINTER(wiphy->regd, NULL);
  3092. if (lr)
  3093. request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
  3094. if (!request_wiphy || request_wiphy != wiphy)
  3095. return;
  3096. lr->wiphy_idx = WIPHY_IDX_INVALID;
  3097. lr->country_ie_env = ENVIRON_ANY;
  3098. }
  3099. /*
  3100. * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
  3101. * UNII band definitions
  3102. */
  3103. int cfg80211_get_unii(int freq)
  3104. {
  3105. /* UNII-1 */
  3106. if (freq >= 5150 && freq <= 5250)
  3107. return 0;
  3108. /* UNII-2A */
  3109. if (freq > 5250 && freq <= 5350)
  3110. return 1;
  3111. /* UNII-2B */
  3112. if (freq > 5350 && freq <= 5470)
  3113. return 2;
  3114. /* UNII-2C */
  3115. if (freq > 5470 && freq <= 5725)
  3116. return 3;
  3117. /* UNII-3 */
  3118. if (freq > 5725 && freq <= 5825)
  3119. return 4;
  3120. return -EINVAL;
  3121. }
  3122. bool regulatory_indoor_allowed(void)
  3123. {
  3124. return reg_is_indoor;
  3125. }
  3126. bool regulatory_pre_cac_allowed(struct wiphy *wiphy)
  3127. {
  3128. const struct ieee80211_regdomain *regd = NULL;
  3129. const struct ieee80211_regdomain *wiphy_regd = NULL;
  3130. bool pre_cac_allowed = false;
  3131. rcu_read_lock();
  3132. regd = rcu_dereference(cfg80211_regdomain);
  3133. wiphy_regd = rcu_dereference(wiphy->regd);
  3134. if (!wiphy_regd) {
  3135. if (regd->dfs_region == NL80211_DFS_ETSI)
  3136. pre_cac_allowed = true;
  3137. rcu_read_unlock();
  3138. return pre_cac_allowed;
  3139. }
  3140. if (regd->dfs_region == wiphy_regd->dfs_region &&
  3141. wiphy_regd->dfs_region == NL80211_DFS_ETSI)
  3142. pre_cac_allowed = true;
  3143. rcu_read_unlock();
  3144. return pre_cac_allowed;
  3145. }
  3146. static void cfg80211_check_and_end_cac(struct cfg80211_registered_device *rdev)
  3147. {
  3148. struct wireless_dev *wdev;
  3149. /* If we finished CAC or received radar, we should end any
  3150. * CAC running on the same channels.
  3151. * the check !cfg80211_chandef_dfs_usable contain 2 options:
  3152. * either all channels are available - those the CAC_FINISHED
  3153. * event has effected another wdev state, or there is a channel
  3154. * in unavailable state in wdev chandef - those the RADAR_DETECTED
  3155. * event has effected another wdev state.
  3156. * In both cases we should end the CAC on the wdev.
  3157. */
  3158. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  3159. if (wdev->cac_started &&
  3160. !cfg80211_chandef_dfs_usable(&rdev->wiphy, &wdev->chandef))
  3161. rdev_end_cac(rdev, wdev->netdev);
  3162. }
  3163. }
  3164. void regulatory_propagate_dfs_state(struct wiphy *wiphy,
  3165. struct cfg80211_chan_def *chandef,
  3166. enum nl80211_dfs_state dfs_state,
  3167. enum nl80211_radar_event event)
  3168. {
  3169. struct cfg80211_registered_device *rdev;
  3170. ASSERT_RTNL();
  3171. if (WARN_ON(!cfg80211_chandef_valid(chandef)))
  3172. return;
  3173. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  3174. if (wiphy == &rdev->wiphy)
  3175. continue;
  3176. if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
  3177. continue;
  3178. if (!ieee80211_get_channel(&rdev->wiphy,
  3179. chandef->chan->center_freq))
  3180. continue;
  3181. cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state);
  3182. if (event == NL80211_RADAR_DETECTED ||
  3183. event == NL80211_RADAR_CAC_FINISHED) {
  3184. cfg80211_sched_dfs_chan_update(rdev);
  3185. cfg80211_check_and_end_cac(rdev);
  3186. }
  3187. nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL);
  3188. }
  3189. }
  3190. static int __init regulatory_init_db(void)
  3191. {
  3192. int err;
  3193. /*
  3194. * It's possible that - due to other bugs/issues - cfg80211
  3195. * never called regulatory_init() below, or that it failed;
  3196. * in that case, don't try to do any further work here as
  3197. * it's doomed to lead to crashes.
  3198. */
  3199. if (IS_ERR_OR_NULL(reg_pdev))
  3200. return -EINVAL;
  3201. err = load_builtin_regdb_keys();
  3202. if (err)
  3203. return err;
  3204. /* We always try to get an update for the static regdomain */
  3205. err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
  3206. if (err) {
  3207. if (err == -ENOMEM) {
  3208. platform_device_unregister(reg_pdev);
  3209. return err;
  3210. }
  3211. /*
  3212. * N.B. kobject_uevent_env() can fail mainly for when we're out
  3213. * memory which is handled and propagated appropriately above
  3214. * but it can also fail during a netlink_broadcast() or during
  3215. * early boot for call_usermodehelper(). For now treat these
  3216. * errors as non-fatal.
  3217. */
  3218. pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
  3219. }
  3220. /*
  3221. * Finally, if the user set the module parameter treat it
  3222. * as a user hint.
  3223. */
  3224. if (!is_world_regdom(ieee80211_regdom))
  3225. regulatory_hint_user(ieee80211_regdom,
  3226. NL80211_USER_REG_HINT_USER);
  3227. return 0;
  3228. }
  3229. #ifndef MODULE
  3230. late_initcall(regulatory_init_db);
  3231. #endif
  3232. int __init regulatory_init(void)
  3233. {
  3234. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  3235. if (IS_ERR(reg_pdev))
  3236. return PTR_ERR(reg_pdev);
  3237. spin_lock_init(&reg_requests_lock);
  3238. spin_lock_init(&reg_pending_beacons_lock);
  3239. spin_lock_init(&reg_indoor_lock);
  3240. rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
  3241. user_alpha2[0] = '9';
  3242. user_alpha2[1] = '7';
  3243. #ifdef MODULE
  3244. return regulatory_init_db();
  3245. #else
  3246. return 0;
  3247. #endif
  3248. }
  3249. void regulatory_exit(void)
  3250. {
  3251. struct regulatory_request *reg_request, *tmp;
  3252. struct reg_beacon *reg_beacon, *btmp;
  3253. cancel_work_sync(&reg_work);
  3254. cancel_crda_timeout_sync();
  3255. cancel_delayed_work_sync(&reg_check_chans);
  3256. /* Lock to suppress warnings */
  3257. rtnl_lock();
  3258. reset_regdomains(true, NULL);
  3259. rtnl_unlock();
  3260. dev_set_uevent_suppress(&reg_pdev->dev, true);
  3261. platform_device_unregister(reg_pdev);
  3262. list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
  3263. list_del(&reg_beacon->list);
  3264. kfree(reg_beacon);
  3265. }
  3266. list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
  3267. list_del(&reg_beacon->list);
  3268. kfree(reg_beacon);
  3269. }
  3270. list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
  3271. list_del(&reg_request->list);
  3272. kfree(reg_request);
  3273. }
  3274. if (!IS_ERR_OR_NULL(regdb))
  3275. kfree(regdb);
  3276. free_regdb_keyring();
  3277. }