reg.c 65 KB

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