reg.c 114 KB

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