reg.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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. enum nl80211_reg_initiator initiator = reg_request->initiator;
  1103. BUG_ON(!reg_request->alpha2);
  1104. mutex_lock(&cfg80211_mutex);
  1105. mutex_lock(&reg_mutex);
  1106. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1107. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1108. if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1109. !wiphy) {
  1110. kfree(reg_request);
  1111. goto out;
  1112. }
  1113. r = __regulatory_hint(wiphy, reg_request);
  1114. /* This is required so that the orig_* parameters are saved */
  1115. if (r == -EALREADY && wiphy &&
  1116. wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
  1117. wiphy_update_regulatory(wiphy, initiator);
  1118. out:
  1119. mutex_unlock(&reg_mutex);
  1120. mutex_unlock(&cfg80211_mutex);
  1121. }
  1122. /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
  1123. static void reg_process_pending_hints(void)
  1124. {
  1125. struct regulatory_request *reg_request;
  1126. spin_lock(&reg_requests_lock);
  1127. while (!list_empty(&reg_requests_list)) {
  1128. reg_request = list_first_entry(&reg_requests_list,
  1129. struct regulatory_request,
  1130. list);
  1131. list_del_init(&reg_request->list);
  1132. spin_unlock(&reg_requests_lock);
  1133. reg_process_hint(reg_request);
  1134. spin_lock(&reg_requests_lock);
  1135. }
  1136. spin_unlock(&reg_requests_lock);
  1137. }
  1138. /* Processes beacon hints -- this has nothing to do with country IEs */
  1139. static void reg_process_pending_beacon_hints(void)
  1140. {
  1141. struct cfg80211_registered_device *rdev;
  1142. struct reg_beacon *pending_beacon, *tmp;
  1143. /*
  1144. * No need to hold the reg_mutex here as we just touch wiphys
  1145. * and do not read or access regulatory variables.
  1146. */
  1147. mutex_lock(&cfg80211_mutex);
  1148. /* This goes through the _pending_ beacon list */
  1149. spin_lock_bh(&reg_pending_beacons_lock);
  1150. if (list_empty(&reg_pending_beacons)) {
  1151. spin_unlock_bh(&reg_pending_beacons_lock);
  1152. goto out;
  1153. }
  1154. list_for_each_entry_safe(pending_beacon, tmp,
  1155. &reg_pending_beacons, list) {
  1156. list_del_init(&pending_beacon->list);
  1157. /* Applies the beacon hint to current wiphys */
  1158. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1159. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  1160. /* Remembers the beacon hint for new wiphys or reg changes */
  1161. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1162. }
  1163. spin_unlock_bh(&reg_pending_beacons_lock);
  1164. out:
  1165. mutex_unlock(&cfg80211_mutex);
  1166. }
  1167. static void reg_todo(struct work_struct *work)
  1168. {
  1169. reg_process_pending_hints();
  1170. reg_process_pending_beacon_hints();
  1171. }
  1172. static DECLARE_WORK(reg_work, reg_todo);
  1173. static void queue_regulatory_request(struct regulatory_request *request)
  1174. {
  1175. spin_lock(&reg_requests_lock);
  1176. list_add_tail(&request->list, &reg_requests_list);
  1177. spin_unlock(&reg_requests_lock);
  1178. schedule_work(&reg_work);
  1179. }
  1180. /*
  1181. * Core regulatory hint -- happens during cfg80211_init()
  1182. * and when we restore regulatory settings.
  1183. */
  1184. static int regulatory_hint_core(const char *alpha2)
  1185. {
  1186. struct regulatory_request *request;
  1187. kfree(last_request);
  1188. last_request = NULL;
  1189. request = kzalloc(sizeof(struct regulatory_request),
  1190. GFP_KERNEL);
  1191. if (!request)
  1192. return -ENOMEM;
  1193. request->alpha2[0] = alpha2[0];
  1194. request->alpha2[1] = alpha2[1];
  1195. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1196. /*
  1197. * This ensures last_request is populated once modules
  1198. * come swinging in and calling regulatory hints and
  1199. * wiphy_apply_custom_regulatory().
  1200. */
  1201. reg_process_hint(request);
  1202. return 0;
  1203. }
  1204. /* User hints */
  1205. int regulatory_hint_user(const char *alpha2)
  1206. {
  1207. struct regulatory_request *request;
  1208. BUG_ON(!alpha2);
  1209. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1210. if (!request)
  1211. return -ENOMEM;
  1212. request->wiphy_idx = WIPHY_IDX_STALE;
  1213. request->alpha2[0] = alpha2[0];
  1214. request->alpha2[1] = alpha2[1];
  1215. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1216. queue_regulatory_request(request);
  1217. return 0;
  1218. }
  1219. /* Driver hints */
  1220. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1221. {
  1222. struct regulatory_request *request;
  1223. BUG_ON(!alpha2);
  1224. BUG_ON(!wiphy);
  1225. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1226. if (!request)
  1227. return -ENOMEM;
  1228. request->wiphy_idx = get_wiphy_idx(wiphy);
  1229. /* Must have registered wiphy first */
  1230. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1231. request->alpha2[0] = alpha2[0];
  1232. request->alpha2[1] = alpha2[1];
  1233. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1234. queue_regulatory_request(request);
  1235. return 0;
  1236. }
  1237. EXPORT_SYMBOL(regulatory_hint);
  1238. /*
  1239. * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
  1240. * therefore cannot iterate over the rdev list here.
  1241. */
  1242. void regulatory_hint_11d(struct wiphy *wiphy,
  1243. enum ieee80211_band band,
  1244. u8 *country_ie,
  1245. u8 country_ie_len)
  1246. {
  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 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. out:
  1285. mutex_unlock(&reg_mutex);
  1286. }
  1287. static void restore_alpha2(char *alpha2, bool reset_user)
  1288. {
  1289. /* indicates there is no alpha2 to consider for restoration */
  1290. alpha2[0] = '9';
  1291. alpha2[1] = '7';
  1292. /* The user setting has precedence over the module parameter */
  1293. if (is_user_regdom_saved()) {
  1294. /* Unless we're asked to ignore it and reset it */
  1295. if (reset_user) {
  1296. REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
  1297. "including user preference\n");
  1298. user_alpha2[0] = '9';
  1299. user_alpha2[1] = '7';
  1300. /*
  1301. * If we're ignoring user settings, we still need to
  1302. * check the module parameter to ensure we put things
  1303. * back as they were for a full restore.
  1304. */
  1305. if (!is_world_regdom(ieee80211_regdom)) {
  1306. REG_DBG_PRINT("cfg80211: Keeping preference on "
  1307. "module parameter ieee80211_regdom: %c%c\n",
  1308. ieee80211_regdom[0],
  1309. ieee80211_regdom[1]);
  1310. alpha2[0] = ieee80211_regdom[0];
  1311. alpha2[1] = ieee80211_regdom[1];
  1312. }
  1313. } else {
  1314. REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
  1315. "while preserving user preference for: %c%c\n",
  1316. user_alpha2[0],
  1317. user_alpha2[1]);
  1318. alpha2[0] = user_alpha2[0];
  1319. alpha2[1] = user_alpha2[1];
  1320. }
  1321. } else if (!is_world_regdom(ieee80211_regdom)) {
  1322. REG_DBG_PRINT("cfg80211: Keeping preference on "
  1323. "module parameter ieee80211_regdom: %c%c\n",
  1324. ieee80211_regdom[0],
  1325. ieee80211_regdom[1]);
  1326. alpha2[0] = ieee80211_regdom[0];
  1327. alpha2[1] = ieee80211_regdom[1];
  1328. } else
  1329. REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
  1330. }
  1331. /*
  1332. * Restoring regulatory settings involves ingoring any
  1333. * possibly stale country IE information and user regulatory
  1334. * settings if so desired, this includes any beacon hints
  1335. * learned as we could have traveled outside to another country
  1336. * after disconnection. To restore regulatory settings we do
  1337. * exactly what we did at bootup:
  1338. *
  1339. * - send a core regulatory hint
  1340. * - send a user regulatory hint if applicable
  1341. *
  1342. * Device drivers that send a regulatory hint for a specific country
  1343. * keep their own regulatory domain on wiphy->regd so that does does
  1344. * not need to be remembered.
  1345. */
  1346. static void restore_regulatory_settings(bool reset_user)
  1347. {
  1348. char alpha2[2];
  1349. struct reg_beacon *reg_beacon, *btmp;
  1350. mutex_lock(&cfg80211_mutex);
  1351. mutex_lock(&reg_mutex);
  1352. reset_regdomains();
  1353. restore_alpha2(alpha2, reset_user);
  1354. /* Clear beacon hints */
  1355. spin_lock_bh(&reg_pending_beacons_lock);
  1356. if (!list_empty(&reg_pending_beacons)) {
  1357. list_for_each_entry_safe(reg_beacon, btmp,
  1358. &reg_pending_beacons, list) {
  1359. list_del(&reg_beacon->list);
  1360. kfree(reg_beacon);
  1361. }
  1362. }
  1363. spin_unlock_bh(&reg_pending_beacons_lock);
  1364. if (!list_empty(&reg_beacon_list)) {
  1365. list_for_each_entry_safe(reg_beacon, btmp,
  1366. &reg_beacon_list, list) {
  1367. list_del(&reg_beacon->list);
  1368. kfree(reg_beacon);
  1369. }
  1370. }
  1371. /* First restore to the basic regulatory settings */
  1372. cfg80211_regdomain = cfg80211_world_regdom;
  1373. mutex_unlock(&reg_mutex);
  1374. mutex_unlock(&cfg80211_mutex);
  1375. regulatory_hint_core(cfg80211_regdomain->alpha2);
  1376. /*
  1377. * This restores the ieee80211_regdom module parameter
  1378. * preference or the last user requested regulatory
  1379. * settings, user regulatory settings takes precedence.
  1380. */
  1381. if (is_an_alpha2(alpha2))
  1382. regulatory_hint_user(user_alpha2);
  1383. }
  1384. void regulatory_hint_disconnect(void)
  1385. {
  1386. REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
  1387. "restore regulatory settings\n");
  1388. restore_regulatory_settings(false);
  1389. }
  1390. static bool freq_is_chan_12_13_14(u16 freq)
  1391. {
  1392. if (freq == ieee80211_channel_to_frequency(12) ||
  1393. freq == ieee80211_channel_to_frequency(13) ||
  1394. freq == ieee80211_channel_to_frequency(14))
  1395. return true;
  1396. return false;
  1397. }
  1398. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1399. struct ieee80211_channel *beacon_chan,
  1400. gfp_t gfp)
  1401. {
  1402. struct reg_beacon *reg_beacon;
  1403. if (likely((beacon_chan->beacon_found ||
  1404. (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
  1405. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1406. !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
  1407. return 0;
  1408. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1409. if (!reg_beacon)
  1410. return -ENOMEM;
  1411. REG_DBG_PRINT("cfg80211: Found new beacon on "
  1412. "frequency: %d MHz (Ch %d) on %s\n",
  1413. beacon_chan->center_freq,
  1414. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1415. wiphy_name(wiphy));
  1416. memcpy(&reg_beacon->chan, beacon_chan,
  1417. sizeof(struct ieee80211_channel));
  1418. /*
  1419. * Since we can be called from BH or and non-BH context
  1420. * we must use spin_lock_bh()
  1421. */
  1422. spin_lock_bh(&reg_pending_beacons_lock);
  1423. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1424. spin_unlock_bh(&reg_pending_beacons_lock);
  1425. schedule_work(&reg_work);
  1426. return 0;
  1427. }
  1428. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1429. {
  1430. unsigned int i;
  1431. const struct ieee80211_reg_rule *reg_rule = NULL;
  1432. const struct ieee80211_freq_range *freq_range = NULL;
  1433. const struct ieee80211_power_rule *power_rule = NULL;
  1434. printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
  1435. "(max_antenna_gain, max_eirp)\n");
  1436. for (i = 0; i < rd->n_reg_rules; i++) {
  1437. reg_rule = &rd->reg_rules[i];
  1438. freq_range = &reg_rule->freq_range;
  1439. power_rule = &reg_rule->power_rule;
  1440. /*
  1441. * There may not be documentation for max antenna gain
  1442. * in certain regions
  1443. */
  1444. if (power_rule->max_antenna_gain)
  1445. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1446. "(%d mBi, %d mBm)\n",
  1447. freq_range->start_freq_khz,
  1448. freq_range->end_freq_khz,
  1449. freq_range->max_bandwidth_khz,
  1450. power_rule->max_antenna_gain,
  1451. power_rule->max_eirp);
  1452. else
  1453. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1454. "(N/A, %d mBm)\n",
  1455. freq_range->start_freq_khz,
  1456. freq_range->end_freq_khz,
  1457. freq_range->max_bandwidth_khz,
  1458. power_rule->max_eirp);
  1459. }
  1460. }
  1461. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1462. {
  1463. if (is_intersected_alpha2(rd->alpha2)) {
  1464. if (last_request->initiator ==
  1465. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1466. struct cfg80211_registered_device *rdev;
  1467. rdev = cfg80211_rdev_by_wiphy_idx(
  1468. last_request->wiphy_idx);
  1469. if (rdev) {
  1470. printk(KERN_INFO "cfg80211: Current regulatory "
  1471. "domain updated by AP to: %c%c\n",
  1472. rdev->country_ie_alpha2[0],
  1473. rdev->country_ie_alpha2[1]);
  1474. } else
  1475. printk(KERN_INFO "cfg80211: Current regulatory "
  1476. "domain intersected:\n");
  1477. } else
  1478. printk(KERN_INFO "cfg80211: Current regulatory "
  1479. "domain intersected:\n");
  1480. } else if (is_world_regdom(rd->alpha2))
  1481. printk(KERN_INFO "cfg80211: World regulatory "
  1482. "domain updated:\n");
  1483. else {
  1484. if (is_unknown_alpha2(rd->alpha2))
  1485. printk(KERN_INFO "cfg80211: Regulatory domain "
  1486. "changed to driver built-in settings "
  1487. "(unknown country)\n");
  1488. else
  1489. printk(KERN_INFO "cfg80211: Regulatory domain "
  1490. "changed to country: %c%c\n",
  1491. rd->alpha2[0], rd->alpha2[1]);
  1492. }
  1493. print_rd_rules(rd);
  1494. }
  1495. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1496. {
  1497. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1498. rd->alpha2[0], rd->alpha2[1]);
  1499. print_rd_rules(rd);
  1500. }
  1501. /* Takes ownership of rd only if it doesn't fail */
  1502. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1503. {
  1504. const struct ieee80211_regdomain *intersected_rd = NULL;
  1505. struct cfg80211_registered_device *rdev = NULL;
  1506. struct wiphy *request_wiphy;
  1507. /* Some basic sanity checks first */
  1508. if (is_world_regdom(rd->alpha2)) {
  1509. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1510. return -EINVAL;
  1511. update_world_regdomain(rd);
  1512. return 0;
  1513. }
  1514. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1515. !is_unknown_alpha2(rd->alpha2))
  1516. return -EINVAL;
  1517. if (!last_request)
  1518. return -EINVAL;
  1519. /*
  1520. * Lets only bother proceeding on the same alpha2 if the current
  1521. * rd is non static (it means CRDA was present and was used last)
  1522. * and the pending request came in from a country IE
  1523. */
  1524. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1525. /*
  1526. * If someone else asked us to change the rd lets only bother
  1527. * checking if the alpha2 changes if CRDA was already called
  1528. */
  1529. if (!regdom_changes(rd->alpha2))
  1530. return -EINVAL;
  1531. }
  1532. /*
  1533. * Now lets set the regulatory domain, update all driver channels
  1534. * and finally inform them of what we have done, in case they want
  1535. * to review or adjust their own settings based on their own
  1536. * internal EEPROM data
  1537. */
  1538. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1539. return -EINVAL;
  1540. if (!is_valid_rd(rd)) {
  1541. printk(KERN_ERR "cfg80211: Invalid "
  1542. "regulatory domain detected:\n");
  1543. print_regdomain_info(rd);
  1544. return -EINVAL;
  1545. }
  1546. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1547. if (!last_request->intersect) {
  1548. int r;
  1549. if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
  1550. reset_regdomains();
  1551. cfg80211_regdomain = rd;
  1552. return 0;
  1553. }
  1554. /*
  1555. * For a driver hint, lets copy the regulatory domain the
  1556. * driver wanted to the wiphy to deal with conflicts
  1557. */
  1558. /*
  1559. * Userspace could have sent two replies with only
  1560. * one kernel request.
  1561. */
  1562. if (request_wiphy->regd)
  1563. return -EALREADY;
  1564. r = reg_copy_regd(&request_wiphy->regd, rd);
  1565. if (r)
  1566. return r;
  1567. reset_regdomains();
  1568. cfg80211_regdomain = rd;
  1569. return 0;
  1570. }
  1571. /* Intersection requires a bit more work */
  1572. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1573. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1574. if (!intersected_rd)
  1575. return -EINVAL;
  1576. /*
  1577. * We can trash what CRDA provided now.
  1578. * However if a driver requested this specific regulatory
  1579. * domain we keep it for its private use
  1580. */
  1581. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
  1582. request_wiphy->regd = rd;
  1583. else
  1584. kfree(rd);
  1585. rd = NULL;
  1586. reset_regdomains();
  1587. cfg80211_regdomain = intersected_rd;
  1588. return 0;
  1589. }
  1590. if (!intersected_rd)
  1591. return -EINVAL;
  1592. rdev = wiphy_to_dev(request_wiphy);
  1593. rdev->country_ie_alpha2[0] = rd->alpha2[0];
  1594. rdev->country_ie_alpha2[1] = rd->alpha2[1];
  1595. rdev->env = last_request->country_ie_env;
  1596. BUG_ON(intersected_rd == rd);
  1597. kfree(rd);
  1598. rd = NULL;
  1599. reset_regdomains();
  1600. cfg80211_regdomain = intersected_rd;
  1601. return 0;
  1602. }
  1603. /*
  1604. * Use this call to set the current regulatory domain. Conflicts with
  1605. * multiple drivers can be ironed out later. Caller must've already
  1606. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  1607. */
  1608. int set_regdom(const struct ieee80211_regdomain *rd)
  1609. {
  1610. int r;
  1611. assert_cfg80211_lock();
  1612. mutex_lock(&reg_mutex);
  1613. /* Note that this doesn't update the wiphys, this is done below */
  1614. r = __set_regdom(rd);
  1615. if (r) {
  1616. kfree(rd);
  1617. mutex_unlock(&reg_mutex);
  1618. return r;
  1619. }
  1620. /* This would make this whole thing pointless */
  1621. if (!last_request->intersect)
  1622. BUG_ON(rd != cfg80211_regdomain);
  1623. /* update all wiphys now with the new established regulatory domain */
  1624. update_all_wiphy_regulatory(last_request->initiator);
  1625. print_regdomain(cfg80211_regdomain);
  1626. nl80211_send_reg_change_event(last_request);
  1627. mutex_unlock(&reg_mutex);
  1628. return r;
  1629. }
  1630. /* Caller must hold cfg80211_mutex */
  1631. void reg_device_remove(struct wiphy *wiphy)
  1632. {
  1633. struct wiphy *request_wiphy = NULL;
  1634. assert_cfg80211_lock();
  1635. mutex_lock(&reg_mutex);
  1636. kfree(wiphy->regd);
  1637. if (last_request)
  1638. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1639. if (!request_wiphy || request_wiphy != wiphy)
  1640. goto out;
  1641. last_request->wiphy_idx = WIPHY_IDX_STALE;
  1642. last_request->country_ie_env = ENVIRON_ANY;
  1643. out:
  1644. mutex_unlock(&reg_mutex);
  1645. }
  1646. int __init regulatory_init(void)
  1647. {
  1648. int err = 0;
  1649. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1650. if (IS_ERR(reg_pdev))
  1651. return PTR_ERR(reg_pdev);
  1652. spin_lock_init(&reg_requests_lock);
  1653. spin_lock_init(&reg_pending_beacons_lock);
  1654. cfg80211_regdomain = cfg80211_world_regdom;
  1655. user_alpha2[0] = '9';
  1656. user_alpha2[1] = '7';
  1657. /* We always try to get an update for the static regdomain */
  1658. err = regulatory_hint_core(cfg80211_regdomain->alpha2);
  1659. if (err) {
  1660. if (err == -ENOMEM)
  1661. return err;
  1662. /*
  1663. * N.B. kobject_uevent_env() can fail mainly for when we're out
  1664. * memory which is handled and propagated appropriately above
  1665. * but it can also fail during a netlink_broadcast() or during
  1666. * early boot for call_usermodehelper(). For now treat these
  1667. * errors as non-fatal.
  1668. */
  1669. printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
  1670. "to call CRDA during init");
  1671. #ifdef CONFIG_CFG80211_REG_DEBUG
  1672. /* We want to find out exactly why when debugging */
  1673. WARN_ON(err);
  1674. #endif
  1675. }
  1676. /*
  1677. * Finally, if the user set the module parameter treat it
  1678. * as a user hint.
  1679. */
  1680. if (!is_world_regdom(ieee80211_regdom))
  1681. regulatory_hint_user(ieee80211_regdom);
  1682. return 0;
  1683. }
  1684. void /* __init_or_exit */ regulatory_exit(void)
  1685. {
  1686. struct regulatory_request *reg_request, *tmp;
  1687. struct reg_beacon *reg_beacon, *btmp;
  1688. cancel_work_sync(&reg_work);
  1689. mutex_lock(&cfg80211_mutex);
  1690. mutex_lock(&reg_mutex);
  1691. reset_regdomains();
  1692. kfree(last_request);
  1693. platform_device_unregister(reg_pdev);
  1694. spin_lock_bh(&reg_pending_beacons_lock);
  1695. if (!list_empty(&reg_pending_beacons)) {
  1696. list_for_each_entry_safe(reg_beacon, btmp,
  1697. &reg_pending_beacons, list) {
  1698. list_del(&reg_beacon->list);
  1699. kfree(reg_beacon);
  1700. }
  1701. }
  1702. spin_unlock_bh(&reg_pending_beacons_lock);
  1703. if (!list_empty(&reg_beacon_list)) {
  1704. list_for_each_entry_safe(reg_beacon, btmp,
  1705. &reg_beacon_list, list) {
  1706. list_del(&reg_beacon->list);
  1707. kfree(reg_beacon);
  1708. }
  1709. }
  1710. spin_lock(&reg_requests_lock);
  1711. if (!list_empty(&reg_requests_list)) {
  1712. list_for_each_entry_safe(reg_request, tmp,
  1713. &reg_requests_list, list) {
  1714. list_del(&reg_request->list);
  1715. kfree(reg_request);
  1716. }
  1717. }
  1718. spin_unlock(&reg_requests_lock);
  1719. mutex_unlock(&reg_mutex);
  1720. mutex_unlock(&cfg80211_mutex);
  1721. }