reg.c 101 KB

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