reg.c 67 KB

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