reg.c 58 KB

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