reg.c 61 KB

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