reg.c 62 KB

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