reg.c 60 KB

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