reg.c 83 KB

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