reg.c 62 KB

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