reg.c 63 KB

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