reg.c 63 KB

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