reg.c 71 KB

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