reg.c 65 KB

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