reg.c 63 KB

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