reg.c 59 KB

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