reg.c 59 KB

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