reg.c 56 KB

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