reg.c 64 KB

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