reg.c 59 KB

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