reg.c 114 KB

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