reg.c 71 KB

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