reg.c 63 KB

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