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