reg.c 63 KB

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