reg.c 61 KB

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