reg.c 83 KB

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