reg.c 63 KB

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