reg.c 65 KB

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