reg.c 74 KB

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