reg.c 100 KB

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