reg.c 57 KB

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