reg.c 65 KB

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