reg.c 65 KB

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