reg.c 58 KB

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