reg.c 114 KB

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