reg.c 53 KB

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