reg.c 101 KB

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