reg.c 63 KB

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