reg.c 57 KB

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