reg.c 68 KB

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