reg.c 85 KB

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