reg.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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. /**
  44. * struct regulatory_request - receipt of last regulatory request
  45. *
  46. * @wiphy: this is set if this request's initiator is
  47. * %REGDOM_SET_BY_COUNTRY_IE or %REGDOM_SET_BY_DRIVER. This
  48. * can be used by the wireless core to deal with conflicts
  49. * and potentially inform users of which devices specifically
  50. * cased the conflicts.
  51. * @initiator: indicates who sent this request, could be any of
  52. * of those set in reg_set_by, %REGDOM_SET_BY_*
  53. * @alpha2: the ISO / IEC 3166 alpha2 country code of the requested
  54. * regulatory domain. We have a few special codes:
  55. * 00 - World regulatory domain
  56. * 99 - built by driver but a specific alpha2 cannot be determined
  57. * 98 - result of an intersection between two regulatory domains
  58. * @intersect: indicates whether the wireless core should intersect
  59. * the requested regulatory domain with the presently set regulatory
  60. * domain.
  61. * @country_ie_checksum: checksum of the last processed and accepted
  62. * country IE
  63. * @country_ie_env: lets us know if the AP is telling us we are outdoor,
  64. * indoor, or if it doesn't matter
  65. */
  66. struct regulatory_request {
  67. struct wiphy *wiphy;
  68. enum reg_set_by initiator;
  69. char alpha2[2];
  70. bool intersect;
  71. u32 country_ie_checksum;
  72. enum environment_cap country_ie_env;
  73. };
  74. /* Receipt of information from last regulatory request */
  75. static struct regulatory_request *last_request;
  76. /* To trigger userspace events */
  77. static struct platform_device *reg_pdev;
  78. /* Keep the ordering from large to small */
  79. static u32 supported_bandwidths[] = {
  80. MHZ_TO_KHZ(40),
  81. MHZ_TO_KHZ(20),
  82. };
  83. /* Central wireless core regulatory domains, we only need two,
  84. * the current one and a world regulatory domain in case we have no
  85. * information to give us an alpha2 */
  86. static const struct ieee80211_regdomain *cfg80211_regdomain;
  87. /* We use this as a place for the rd structure built from the
  88. * last parsed country IE to rest until CRDA gets back to us with
  89. * what it thinks should apply for the same country */
  90. static const struct ieee80211_regdomain *country_ie_regdomain;
  91. /* We keep a static world regulatory domain in case of the absence of CRDA */
  92. static const struct ieee80211_regdomain world_regdom = {
  93. .n_reg_rules = 1,
  94. .alpha2 = "00",
  95. .reg_rules = {
  96. REG_RULE(2412-10, 2462+10, 40, 6, 20,
  97. NL80211_RRF_PASSIVE_SCAN |
  98. NL80211_RRF_NO_IBSS),
  99. }
  100. };
  101. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  102. &world_regdom;
  103. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  104. static char *ieee80211_regdom = "US";
  105. module_param(ieee80211_regdom, charp, 0444);
  106. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  107. /* We assume 40 MHz bandwidth for the old regulatory work.
  108. * We make emphasis we are using the exact same frequencies
  109. * as before */
  110. static const struct ieee80211_regdomain us_regdom = {
  111. .n_reg_rules = 6,
  112. .alpha2 = "US",
  113. .reg_rules = {
  114. /* IEEE 802.11b/g, channels 1..11 */
  115. REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
  116. /* IEEE 802.11a, channel 36 */
  117. REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
  118. /* IEEE 802.11a, channel 40 */
  119. REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
  120. /* IEEE 802.11a, channel 44 */
  121. REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
  122. /* IEEE 802.11a, channels 48..64 */
  123. REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
  124. /* IEEE 802.11a, channels 149..165, outdoor */
  125. REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
  126. }
  127. };
  128. static const struct ieee80211_regdomain jp_regdom = {
  129. .n_reg_rules = 3,
  130. .alpha2 = "JP",
  131. .reg_rules = {
  132. /* IEEE 802.11b/g, channels 1..14 */
  133. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  134. /* IEEE 802.11a, channels 34..48 */
  135. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  136. NL80211_RRF_PASSIVE_SCAN),
  137. /* IEEE 802.11a, channels 52..64 */
  138. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  139. NL80211_RRF_NO_IBSS |
  140. NL80211_RRF_DFS),
  141. }
  142. };
  143. static const struct ieee80211_regdomain eu_regdom = {
  144. .n_reg_rules = 6,
  145. /* This alpha2 is bogus, we leave it here just for stupid
  146. * backward compatibility */
  147. .alpha2 = "EU",
  148. .reg_rules = {
  149. /* IEEE 802.11b/g, channels 1..13 */
  150. REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
  151. /* IEEE 802.11a, channel 36 */
  152. REG_RULE(5180-10, 5180+10, 40, 6, 23,
  153. NL80211_RRF_PASSIVE_SCAN),
  154. /* IEEE 802.11a, channel 40 */
  155. REG_RULE(5200-10, 5200+10, 40, 6, 23,
  156. NL80211_RRF_PASSIVE_SCAN),
  157. /* IEEE 802.11a, channel 44 */
  158. REG_RULE(5220-10, 5220+10, 40, 6, 23,
  159. NL80211_RRF_PASSIVE_SCAN),
  160. /* IEEE 802.11a, channels 48..64 */
  161. REG_RULE(5240-10, 5320+10, 40, 6, 20,
  162. NL80211_RRF_NO_IBSS |
  163. NL80211_RRF_DFS),
  164. /* IEEE 802.11a, channels 100..140 */
  165. REG_RULE(5500-10, 5700+10, 40, 6, 30,
  166. NL80211_RRF_NO_IBSS |
  167. NL80211_RRF_DFS),
  168. }
  169. };
  170. static const struct ieee80211_regdomain *static_regdom(char *alpha2)
  171. {
  172. if (alpha2[0] == 'U' && alpha2[1] == 'S')
  173. return &us_regdom;
  174. if (alpha2[0] == 'J' && alpha2[1] == 'P')
  175. return &jp_regdom;
  176. if (alpha2[0] == 'E' && alpha2[1] == 'U')
  177. return &eu_regdom;
  178. /* Default, as per the old rules */
  179. return &us_regdom;
  180. }
  181. static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  182. {
  183. if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
  184. return true;
  185. return false;
  186. }
  187. #else
  188. static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  189. {
  190. return false;
  191. }
  192. #endif
  193. static void reset_regdomains(void)
  194. {
  195. /* avoid freeing static information or freeing something twice */
  196. if (cfg80211_regdomain == cfg80211_world_regdom)
  197. cfg80211_regdomain = NULL;
  198. if (cfg80211_world_regdom == &world_regdom)
  199. cfg80211_world_regdom = NULL;
  200. if (cfg80211_regdomain == &world_regdom)
  201. cfg80211_regdomain = NULL;
  202. if (is_old_static_regdom(cfg80211_regdomain))
  203. cfg80211_regdomain = NULL;
  204. kfree(cfg80211_regdomain);
  205. kfree(cfg80211_world_regdom);
  206. cfg80211_world_regdom = &world_regdom;
  207. cfg80211_regdomain = NULL;
  208. }
  209. /* Dynamic world regulatory domain requested by the wireless
  210. * core upon initialization */
  211. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  212. {
  213. BUG_ON(!last_request);
  214. reset_regdomains();
  215. cfg80211_world_regdom = rd;
  216. cfg80211_regdomain = rd;
  217. }
  218. bool is_world_regdom(const char *alpha2)
  219. {
  220. if (!alpha2)
  221. return false;
  222. if (alpha2[0] == '0' && alpha2[1] == '0')
  223. return true;
  224. return false;
  225. }
  226. static bool is_alpha2_set(const char *alpha2)
  227. {
  228. if (!alpha2)
  229. return false;
  230. if (alpha2[0] != 0 && alpha2[1] != 0)
  231. return true;
  232. return false;
  233. }
  234. static bool is_alpha_upper(char letter)
  235. {
  236. /* ASCII A - Z */
  237. if (letter >= 65 && letter <= 90)
  238. return true;
  239. return false;
  240. }
  241. static bool is_unknown_alpha2(const char *alpha2)
  242. {
  243. if (!alpha2)
  244. return false;
  245. /* Special case where regulatory domain was built by driver
  246. * but a specific alpha2 cannot be determined */
  247. if (alpha2[0] == '9' && alpha2[1] == '9')
  248. return true;
  249. return false;
  250. }
  251. static bool is_intersected_alpha2(const char *alpha2)
  252. {
  253. if (!alpha2)
  254. return false;
  255. /* Special case where regulatory domain is the
  256. * result of an intersection between two regulatory domain
  257. * structures */
  258. if (alpha2[0] == '9' && alpha2[1] == '8')
  259. return true;
  260. return false;
  261. }
  262. static bool is_an_alpha2(const char *alpha2)
  263. {
  264. if (!alpha2)
  265. return false;
  266. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  267. return true;
  268. return false;
  269. }
  270. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  271. {
  272. if (!alpha2_x || !alpha2_y)
  273. return false;
  274. if (alpha2_x[0] == alpha2_y[0] &&
  275. alpha2_x[1] == alpha2_y[1])
  276. return true;
  277. return false;
  278. }
  279. static bool regdom_changed(const char *alpha2)
  280. {
  281. if (!cfg80211_regdomain)
  282. return true;
  283. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  284. return false;
  285. return true;
  286. }
  287. /**
  288. * country_ie_integrity_changes - tells us if the country IE has changed
  289. * @checksum: checksum of country IE of fields we are interested in
  290. *
  291. * If the country IE has not changed you can ignore it safely. This is
  292. * useful to determine if two devices are seeing two different country IEs
  293. * even on the same alpha2. Note that this will return false if no IE has
  294. * been set on the wireless core yet.
  295. */
  296. static bool country_ie_integrity_changes(u32 checksum)
  297. {
  298. /* If no IE has been set then the checksum doesn't change */
  299. if (unlikely(!last_request->country_ie_checksum))
  300. return false;
  301. if (unlikely(last_request->country_ie_checksum != checksum))
  302. return true;
  303. return false;
  304. }
  305. /* This lets us keep regulatory code which is updated on a regulatory
  306. * basis in userspace. */
  307. static int call_crda(const char *alpha2)
  308. {
  309. char country_env[9 + 2] = "COUNTRY=";
  310. char *envp[] = {
  311. country_env,
  312. NULL
  313. };
  314. if (!is_world_regdom((char *) alpha2))
  315. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  316. alpha2[0], alpha2[1]);
  317. else
  318. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  319. "regulatory domain\n");
  320. country_env[8] = alpha2[0];
  321. country_env[9] = alpha2[1];
  322. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  323. }
  324. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  325. bool reg_is_valid_request(const char *alpha2)
  326. {
  327. if (!last_request)
  328. return false;
  329. return alpha2_equal(last_request->alpha2, alpha2);
  330. }
  331. /* Sanity check on a regulatory rule */
  332. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  333. {
  334. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  335. u32 freq_diff;
  336. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  337. return false;
  338. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  339. return false;
  340. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  341. if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
  342. return false;
  343. return true;
  344. }
  345. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  346. {
  347. const struct ieee80211_reg_rule *reg_rule = NULL;
  348. unsigned int i;
  349. if (!rd->n_reg_rules)
  350. return false;
  351. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  352. return false;
  353. for (i = 0; i < rd->n_reg_rules; i++) {
  354. reg_rule = &rd->reg_rules[i];
  355. if (!is_valid_reg_rule(reg_rule))
  356. return false;
  357. }
  358. return true;
  359. }
  360. /* Returns value in KHz */
  361. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  362. u32 freq)
  363. {
  364. unsigned int i;
  365. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  366. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  367. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  368. if (start_freq_khz >= freq_range->start_freq_khz &&
  369. end_freq_khz <= freq_range->end_freq_khz)
  370. return supported_bandwidths[i];
  371. }
  372. return 0;
  373. }
  374. /**
  375. * freq_in_rule_band - tells us if a frequency is in a frequency band
  376. * @freq_range: frequency rule we want to query
  377. * @freq_khz: frequency we are inquiring about
  378. *
  379. * This lets us know if a specific frequency rule is or is not relevant to
  380. * a specific frequency's band. Bands are device specific and artificial
  381. * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
  382. * safe for now to assume that a frequency rule should not be part of a
  383. * frequency's band if the start freq or end freq are off by more than 2 GHz.
  384. * This resolution can be lowered and should be considered as we add
  385. * regulatory rule support for other "bands".
  386. **/
  387. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  388. u32 freq_khz)
  389. {
  390. #define ONE_GHZ_IN_KHZ 1000000
  391. if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  392. return true;
  393. if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  394. return true;
  395. return false;
  396. #undef ONE_GHZ_IN_KHZ
  397. }
  398. /* Converts a country IE to a regulatory domain. A regulatory domain
  399. * structure has a lot of information which the IE doesn't yet have,
  400. * so for the other values we use upper max values as we will intersect
  401. * with our userspace regulatory agent to get lower bounds. */
  402. static struct ieee80211_regdomain *country_ie_2_rd(
  403. u8 *country_ie,
  404. u8 country_ie_len,
  405. u32 *checksum)
  406. {
  407. struct ieee80211_regdomain *rd = NULL;
  408. unsigned int i = 0;
  409. char alpha2[2];
  410. u32 flags = 0;
  411. u32 num_rules = 0, size_of_regd = 0;
  412. u8 *triplets_start = NULL;
  413. u8 len_at_triplet = 0;
  414. /* the last channel we have registered in a subband (triplet) */
  415. int last_sub_max_channel = 0;
  416. *checksum = 0xDEADBEEF;
  417. /* Country IE requirements */
  418. BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
  419. country_ie_len & 0x01);
  420. alpha2[0] = country_ie[0];
  421. alpha2[1] = country_ie[1];
  422. /*
  423. * Third octet can be:
  424. * 'I' - Indoor
  425. * 'O' - Outdoor
  426. *
  427. * anything else we assume is no restrictions
  428. */
  429. if (country_ie[2] == 'I')
  430. flags = NL80211_RRF_NO_OUTDOOR;
  431. else if (country_ie[2] == 'O')
  432. flags = NL80211_RRF_NO_INDOOR;
  433. country_ie += 3;
  434. country_ie_len -= 3;
  435. triplets_start = country_ie;
  436. len_at_triplet = country_ie_len;
  437. *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
  438. /* We need to build a reg rule for each triplet, but first we must
  439. * calculate the number of reg rules we will need. We will need one
  440. * for each channel subband */
  441. while (country_ie_len >= 3) {
  442. struct ieee80211_country_ie_triplet *triplet =
  443. (struct ieee80211_country_ie_triplet *) country_ie;
  444. int cur_sub_max_channel = 0, cur_channel = 0;
  445. if (triplet->ext.reg_extension_id >=
  446. IEEE80211_COUNTRY_EXTENSION_ID) {
  447. country_ie += 3;
  448. country_ie_len -= 3;
  449. continue;
  450. }
  451. cur_channel = triplet->chans.first_channel;
  452. cur_sub_max_channel = ieee80211_channel_to_frequency(
  453. cur_channel + triplet->chans.num_channels);
  454. /* Basic sanity check */
  455. if (cur_sub_max_channel < cur_channel)
  456. return NULL;
  457. /* Do not allow overlapping channels. Also channels
  458. * passed in each subband must be monotonically
  459. * increasing */
  460. if (last_sub_max_channel) {
  461. if (cur_channel <= last_sub_max_channel)
  462. return NULL;
  463. if (cur_sub_max_channel <= last_sub_max_channel)
  464. return NULL;
  465. }
  466. /* When dot11RegulatoryClassesRequired is supported
  467. * we can throw ext triplets as part of this soup,
  468. * for now we don't care when those change as we
  469. * don't support them */
  470. *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
  471. ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
  472. ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
  473. last_sub_max_channel = cur_sub_max_channel;
  474. country_ie += 3;
  475. country_ie_len -= 3;
  476. num_rules++;
  477. /* Note: this is not a IEEE requirement but
  478. * simply a memory requirement */
  479. if (num_rules > NL80211_MAX_SUPP_REG_RULES)
  480. return NULL;
  481. }
  482. country_ie = triplets_start;
  483. country_ie_len = len_at_triplet;
  484. size_of_regd = sizeof(struct ieee80211_regdomain) +
  485. (num_rules * sizeof(struct ieee80211_reg_rule));
  486. rd = kzalloc(size_of_regd, GFP_KERNEL);
  487. if (!rd)
  488. return NULL;
  489. rd->n_reg_rules = num_rules;
  490. rd->alpha2[0] = alpha2[0];
  491. rd->alpha2[1] = alpha2[1];
  492. /* This time around we fill in the rd */
  493. while (country_ie_len >= 3) {
  494. int end_channel = 0;
  495. struct ieee80211_country_ie_triplet *triplet =
  496. (struct ieee80211_country_ie_triplet *) country_ie;
  497. struct ieee80211_reg_rule *reg_rule = NULL;
  498. struct ieee80211_freq_range *freq_range = NULL;
  499. struct ieee80211_power_rule *power_rule = NULL;
  500. /* Must parse if dot11RegulatoryClassesRequired is true,
  501. * we don't support this yet */
  502. if (triplet->ext.reg_extension_id >=
  503. IEEE80211_COUNTRY_EXTENSION_ID) {
  504. country_ie += 3;
  505. country_ie_len -= 3;
  506. continue;
  507. }
  508. reg_rule = &rd->reg_rules[i];
  509. freq_range = &reg_rule->freq_range;
  510. power_rule = &reg_rule->power_rule;
  511. reg_rule->flags = flags;
  512. /* 2 GHz */
  513. if (triplet->chans.first_channel <= 14)
  514. end_channel = triplet->chans.first_channel +
  515. triplet->chans.num_channels;
  516. else
  517. /*
  518. * 5 GHz -- For example in country IEs if the first
  519. * channel given is 36 and the number of channels is 4
  520. * then the individual channel numbers defined for the
  521. * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
  522. * and not 36, 37, 38, 39.
  523. *
  524. * See: http://tinyurl.com/11d-clarification
  525. */
  526. end_channel = triplet->chans.first_channel +
  527. (4 * (triplet->chans.num_channels - 1));
  528. /* The +10 is since the regulatory domain expects
  529. * the actual band edge, not the center of freq for
  530. * its start and end freqs, assuming 20 MHz bandwidth on
  531. * the channels passed */
  532. freq_range->start_freq_khz =
  533. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  534. triplet->chans.first_channel) - 10);
  535. freq_range->end_freq_khz =
  536. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  537. end_channel) + 10);
  538. /* Large arbitrary values, we intersect later */
  539. /* Increment this if we ever support >= 40 MHz channels
  540. * in IEEE 802.11 */
  541. freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
  542. power_rule->max_antenna_gain = DBI_TO_MBI(100);
  543. power_rule->max_eirp = DBM_TO_MBM(100);
  544. country_ie += 3;
  545. country_ie_len -= 3;
  546. i++;
  547. BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
  548. }
  549. return rd;
  550. }
  551. /* Helper for regdom_intersect(), this does the real
  552. * mathematical intersection fun */
  553. static int reg_rules_intersect(
  554. const struct ieee80211_reg_rule *rule1,
  555. const struct ieee80211_reg_rule *rule2,
  556. struct ieee80211_reg_rule *intersected_rule)
  557. {
  558. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  559. struct ieee80211_freq_range *freq_range;
  560. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  561. struct ieee80211_power_rule *power_rule;
  562. u32 freq_diff;
  563. freq_range1 = &rule1->freq_range;
  564. freq_range2 = &rule2->freq_range;
  565. freq_range = &intersected_rule->freq_range;
  566. power_rule1 = &rule1->power_rule;
  567. power_rule2 = &rule2->power_rule;
  568. power_rule = &intersected_rule->power_rule;
  569. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  570. freq_range2->start_freq_khz);
  571. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  572. freq_range2->end_freq_khz);
  573. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  574. freq_range2->max_bandwidth_khz);
  575. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  576. if (freq_range->max_bandwidth_khz > freq_diff)
  577. freq_range->max_bandwidth_khz = freq_diff;
  578. power_rule->max_eirp = min(power_rule1->max_eirp,
  579. power_rule2->max_eirp);
  580. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  581. power_rule2->max_antenna_gain);
  582. intersected_rule->flags = (rule1->flags | rule2->flags);
  583. if (!is_valid_reg_rule(intersected_rule))
  584. return -EINVAL;
  585. return 0;
  586. }
  587. /**
  588. * regdom_intersect - do the intersection between two regulatory domains
  589. * @rd1: first regulatory domain
  590. * @rd2: second regulatory domain
  591. *
  592. * Use this function to get the intersection between two regulatory domains.
  593. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  594. * as no one single alpha2 can represent this regulatory domain.
  595. *
  596. * Returns a pointer to the regulatory domain structure which will hold the
  597. * resulting intersection of rules between rd1 and rd2. We will
  598. * kzalloc() this structure for you.
  599. */
  600. static struct ieee80211_regdomain *regdom_intersect(
  601. const struct ieee80211_regdomain *rd1,
  602. const struct ieee80211_regdomain *rd2)
  603. {
  604. int r, size_of_regd;
  605. unsigned int x, y;
  606. unsigned int num_rules = 0, rule_idx = 0;
  607. const struct ieee80211_reg_rule *rule1, *rule2;
  608. struct ieee80211_reg_rule *intersected_rule;
  609. struct ieee80211_regdomain *rd;
  610. /* This is just a dummy holder to help us count */
  611. struct ieee80211_reg_rule irule;
  612. /* Uses the stack temporarily for counter arithmetic */
  613. intersected_rule = &irule;
  614. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  615. if (!rd1 || !rd2)
  616. return NULL;
  617. /* First we get a count of the rules we'll need, then we actually
  618. * build them. This is to so we can malloc() and free() a
  619. * regdomain once. The reason we use reg_rules_intersect() here
  620. * is it will return -EINVAL if the rule computed makes no sense.
  621. * All rules that do check out OK are valid. */
  622. for (x = 0; x < rd1->n_reg_rules; x++) {
  623. rule1 = &rd1->reg_rules[x];
  624. for (y = 0; y < rd2->n_reg_rules; y++) {
  625. rule2 = &rd2->reg_rules[y];
  626. if (!reg_rules_intersect(rule1, rule2,
  627. intersected_rule))
  628. num_rules++;
  629. memset(intersected_rule, 0,
  630. sizeof(struct ieee80211_reg_rule));
  631. }
  632. }
  633. if (!num_rules)
  634. return NULL;
  635. size_of_regd = sizeof(struct ieee80211_regdomain) +
  636. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  637. rd = kzalloc(size_of_regd, GFP_KERNEL);
  638. if (!rd)
  639. return NULL;
  640. for (x = 0; x < rd1->n_reg_rules; x++) {
  641. rule1 = &rd1->reg_rules[x];
  642. for (y = 0; y < rd2->n_reg_rules; y++) {
  643. rule2 = &rd2->reg_rules[y];
  644. /* This time around instead of using the stack lets
  645. * write to the target rule directly saving ourselves
  646. * a memcpy() */
  647. intersected_rule = &rd->reg_rules[rule_idx];
  648. r = reg_rules_intersect(rule1, rule2,
  649. intersected_rule);
  650. /* No need to memset here the intersected rule here as
  651. * we're not using the stack anymore */
  652. if (r)
  653. continue;
  654. rule_idx++;
  655. }
  656. }
  657. if (rule_idx != num_rules) {
  658. kfree(rd);
  659. return NULL;
  660. }
  661. rd->n_reg_rules = num_rules;
  662. rd->alpha2[0] = '9';
  663. rd->alpha2[1] = '8';
  664. return rd;
  665. }
  666. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  667. * want to just have the channel structure use these */
  668. static u32 map_regdom_flags(u32 rd_flags)
  669. {
  670. u32 channel_flags = 0;
  671. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  672. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  673. if (rd_flags & NL80211_RRF_NO_IBSS)
  674. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  675. if (rd_flags & NL80211_RRF_DFS)
  676. channel_flags |= IEEE80211_CHAN_RADAR;
  677. return channel_flags;
  678. }
  679. static int freq_reg_info_regd(struct wiphy *wiphy,
  680. u32 center_freq,
  681. u32 *bandwidth,
  682. const struct ieee80211_reg_rule **reg_rule,
  683. const struct ieee80211_regdomain *custom_regd)
  684. {
  685. int i;
  686. bool band_rule_found = false;
  687. const struct ieee80211_regdomain *regd;
  688. u32 max_bandwidth = 0;
  689. regd = custom_regd ? custom_regd : cfg80211_regdomain;
  690. /* Follow the driver's regulatory domain, if present, unless a country
  691. * IE has been processed */
  692. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
  693. wiphy->regd)
  694. regd = wiphy->regd;
  695. if (!regd)
  696. return -EINVAL;
  697. for (i = 0; i < regd->n_reg_rules; i++) {
  698. const struct ieee80211_reg_rule *rr;
  699. const struct ieee80211_freq_range *fr = NULL;
  700. const struct ieee80211_power_rule *pr = NULL;
  701. rr = &regd->reg_rules[i];
  702. fr = &rr->freq_range;
  703. pr = &rr->power_rule;
  704. /* We only need to know if one frequency rule was
  705. * was in center_freq's band, that's enough, so lets
  706. * not overwrite it once found */
  707. if (!band_rule_found)
  708. band_rule_found = freq_in_rule_band(fr, center_freq);
  709. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  710. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  711. *reg_rule = rr;
  712. *bandwidth = max_bandwidth;
  713. break;
  714. }
  715. }
  716. if (!band_rule_found)
  717. return -ERANGE;
  718. return !max_bandwidth;
  719. }
  720. EXPORT_SYMBOL(freq_reg_info);
  721. int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
  722. const struct ieee80211_reg_rule **reg_rule)
  723. {
  724. return freq_reg_info_regd(wiphy, center_freq,
  725. bandwidth, reg_rule, NULL);
  726. }
  727. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  728. unsigned int chan_idx)
  729. {
  730. int r;
  731. u32 flags;
  732. u32 max_bandwidth = 0;
  733. const struct ieee80211_reg_rule *reg_rule = NULL;
  734. const struct ieee80211_power_rule *power_rule = NULL;
  735. struct ieee80211_supported_band *sband;
  736. struct ieee80211_channel *chan;
  737. sband = wiphy->bands[band];
  738. BUG_ON(chan_idx >= sband->n_channels);
  739. chan = &sband->channels[chan_idx];
  740. flags = chan->orig_flags;
  741. r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
  742. &max_bandwidth, &reg_rule);
  743. if (r) {
  744. /* This means no regulatory rule was found in the country IE
  745. * with a frequency range on the center_freq's band, since
  746. * IEEE-802.11 allows for a country IE to have a subset of the
  747. * regulatory information provided in a country we ignore
  748. * disabling the channel unless at least one reg rule was
  749. * found on the center_freq's band. For details see this
  750. * clarification:
  751. *
  752. * http://tinyurl.com/11d-clarification
  753. */
  754. if (r == -ERANGE &&
  755. last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  756. #ifdef CONFIG_CFG80211_REG_DEBUG
  757. printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
  758. "intact on %s - no rule found in band on "
  759. "Country IE\n",
  760. chan->center_freq, wiphy_name(wiphy));
  761. #endif
  762. } else {
  763. /* In this case we know the country IE has at least one reg rule
  764. * for the band so we respect its band definitions */
  765. #ifdef CONFIG_CFG80211_REG_DEBUG
  766. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  767. printk(KERN_DEBUG "cfg80211: Disabling "
  768. "channel %d MHz on %s due to "
  769. "Country IE\n",
  770. chan->center_freq, wiphy_name(wiphy));
  771. #endif
  772. flags |= IEEE80211_CHAN_DISABLED;
  773. chan->flags = flags;
  774. }
  775. return;
  776. }
  777. power_rule = &reg_rule->power_rule;
  778. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  779. chan->max_antenna_gain = min(chan->orig_mag,
  780. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  781. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  782. if (chan->orig_mpwr)
  783. chan->max_power = min(chan->orig_mpwr,
  784. (int) MBM_TO_DBM(power_rule->max_eirp));
  785. else
  786. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  787. }
  788. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  789. {
  790. unsigned int i;
  791. struct ieee80211_supported_band *sband;
  792. BUG_ON(!wiphy->bands[band]);
  793. sband = wiphy->bands[band];
  794. for (i = 0; i < sband->n_channels; i++)
  795. handle_channel(wiphy, band, i);
  796. }
  797. static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
  798. {
  799. if (!last_request)
  800. return true;
  801. if (setby == REGDOM_SET_BY_CORE &&
  802. wiphy->fw_handles_regulatory)
  803. return true;
  804. return false;
  805. }
  806. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  807. {
  808. struct cfg80211_registered_device *drv;
  809. list_for_each_entry(drv, &cfg80211_drv_list, list)
  810. if (!ignore_reg_update(&drv->wiphy, setby))
  811. wiphy_update_regulatory(&drv->wiphy, setby);
  812. }
  813. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  814. {
  815. enum ieee80211_band band;
  816. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  817. if (wiphy->bands[band])
  818. handle_band(wiphy, band);
  819. }
  820. if (wiphy->reg_notifier)
  821. wiphy->reg_notifier(wiphy, setby);
  822. }
  823. static void handle_channel_custom(struct wiphy *wiphy,
  824. enum ieee80211_band band,
  825. unsigned int chan_idx,
  826. const struct ieee80211_regdomain *regd)
  827. {
  828. int r;
  829. u32 max_bandwidth = 0;
  830. const struct ieee80211_reg_rule *reg_rule = NULL;
  831. const struct ieee80211_power_rule *power_rule = NULL;
  832. struct ieee80211_supported_band *sband;
  833. struct ieee80211_channel *chan;
  834. sband = wiphy->bands[band];
  835. BUG_ON(chan_idx >= sband->n_channels);
  836. chan = &sband->channels[chan_idx];
  837. r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  838. &max_bandwidth, &reg_rule, regd);
  839. if (r) {
  840. chan->flags = IEEE80211_CHAN_DISABLED;
  841. return;
  842. }
  843. power_rule = &reg_rule->power_rule;
  844. chan->flags |= map_regdom_flags(reg_rule->flags);
  845. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  846. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  847. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  848. }
  849. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  850. const struct ieee80211_regdomain *regd)
  851. {
  852. unsigned int i;
  853. struct ieee80211_supported_band *sband;
  854. BUG_ON(!wiphy->bands[band]);
  855. sband = wiphy->bands[band];
  856. for (i = 0; i < sband->n_channels; i++)
  857. handle_channel_custom(wiphy, band, i, regd);
  858. }
  859. /* Used by drivers prior to wiphy registration */
  860. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  861. const struct ieee80211_regdomain *regd)
  862. {
  863. enum ieee80211_band band;
  864. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  865. if (wiphy->bands[band])
  866. handle_band_custom(wiphy, band, regd);
  867. }
  868. }
  869. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  870. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  871. const struct ieee80211_regdomain *src_regd)
  872. {
  873. struct ieee80211_regdomain *regd;
  874. int size_of_regd = 0;
  875. unsigned int i;
  876. size_of_regd = sizeof(struct ieee80211_regdomain) +
  877. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  878. regd = kzalloc(size_of_regd, GFP_KERNEL);
  879. if (!regd)
  880. return -ENOMEM;
  881. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  882. for (i = 0; i < src_regd->n_reg_rules; i++)
  883. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  884. sizeof(struct ieee80211_reg_rule));
  885. *dst_regd = regd;
  886. return 0;
  887. }
  888. /* Return value which can be used by ignore_request() to indicate
  889. * it has been determined we should intersect two regulatory domains */
  890. #define REG_INTERSECT 1
  891. /* This has the logic which determines when a new request
  892. * should be ignored. */
  893. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  894. const char *alpha2)
  895. {
  896. /* All initial requests are respected */
  897. if (!last_request)
  898. return 0;
  899. switch (set_by) {
  900. case REGDOM_SET_BY_INIT:
  901. return -EINVAL;
  902. case REGDOM_SET_BY_CORE:
  903. /*
  904. * Always respect new wireless core hints, should only happen
  905. * when updating the world regulatory domain at init.
  906. */
  907. return 0;
  908. case REGDOM_SET_BY_COUNTRY_IE:
  909. if (unlikely(!is_an_alpha2(alpha2)))
  910. return -EINVAL;
  911. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  912. if (last_request->wiphy != wiphy) {
  913. /*
  914. * Two cards with two APs claiming different
  915. * different Country IE alpha2s. We could
  916. * intersect them, but that seems unlikely
  917. * to be correct. Reject second one for now.
  918. */
  919. if (!alpha2_equal(alpha2,
  920. cfg80211_regdomain->alpha2))
  921. return -EOPNOTSUPP;
  922. return -EALREADY;
  923. }
  924. /* Two consecutive Country IE hints on the same wiphy.
  925. * This should be picked up early by the driver/stack */
  926. if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
  927. alpha2)))
  928. return 0;
  929. return -EALREADY;
  930. }
  931. return REG_INTERSECT;
  932. case REGDOM_SET_BY_DRIVER:
  933. if (last_request->initiator == REGDOM_SET_BY_CORE)
  934. return 0;
  935. return REG_INTERSECT;
  936. case REGDOM_SET_BY_USER:
  937. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  938. return REG_INTERSECT;
  939. /* If the user knows better the user should set the regdom
  940. * to their country before the IE is picked up */
  941. if (last_request->initiator == REGDOM_SET_BY_USER &&
  942. last_request->intersect)
  943. return -EOPNOTSUPP;
  944. return 0;
  945. }
  946. return -EINVAL;
  947. }
  948. /* Caller must hold &cfg80211_drv_mutex */
  949. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  950. const char *alpha2,
  951. u32 country_ie_checksum,
  952. enum environment_cap env)
  953. {
  954. struct regulatory_request *request;
  955. bool intersect = false;
  956. int r = 0;
  957. r = ignore_request(wiphy, set_by, alpha2);
  958. if (r == REG_INTERSECT) {
  959. if (set_by == REGDOM_SET_BY_DRIVER) {
  960. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  961. if (r)
  962. return r;
  963. }
  964. intersect = true;
  965. } else if (r) {
  966. /* If the regulatory domain being requested by the
  967. * driver has already been set just copy it to the
  968. * wiphy */
  969. if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
  970. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  971. if (r)
  972. return r;
  973. r = -EALREADY;
  974. goto new_request;
  975. }
  976. return r;
  977. }
  978. new_request:
  979. request = kzalloc(sizeof(struct regulatory_request),
  980. GFP_KERNEL);
  981. if (!request)
  982. return -ENOMEM;
  983. request->alpha2[0] = alpha2[0];
  984. request->alpha2[1] = alpha2[1];
  985. request->initiator = set_by;
  986. request->wiphy = wiphy;
  987. request->intersect = intersect;
  988. request->country_ie_checksum = country_ie_checksum;
  989. request->country_ie_env = env;
  990. kfree(last_request);
  991. last_request = request;
  992. /* When r == REG_INTERSECT we do need to call CRDA */
  993. if (r < 0)
  994. return r;
  995. /*
  996. * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
  997. * AND if CRDA is NOT present nothing will happen, if someone
  998. * wants to bother with 11d with OLD_REG you can add a timer.
  999. * If after x amount of time nothing happens you can call:
  1000. *
  1001. * return set_regdom(country_ie_regdomain);
  1002. *
  1003. * to intersect with the static rd
  1004. */
  1005. return call_crda(alpha2);
  1006. }
  1007. void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1008. {
  1009. BUG_ON(!alpha2);
  1010. mutex_lock(&cfg80211_drv_mutex);
  1011. __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY);
  1012. mutex_unlock(&cfg80211_drv_mutex);
  1013. }
  1014. EXPORT_SYMBOL(regulatory_hint);
  1015. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1016. u32 country_ie_checksum)
  1017. {
  1018. if (!last_request->wiphy)
  1019. return false;
  1020. if (likely(last_request->wiphy != wiphy))
  1021. return !country_ie_integrity_changes(country_ie_checksum);
  1022. /* We should not have let these through at this point, they
  1023. * should have been picked up earlier by the first alpha2 check
  1024. * on the device */
  1025. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1026. return true;
  1027. return false;
  1028. }
  1029. void regulatory_hint_11d(struct wiphy *wiphy,
  1030. u8 *country_ie,
  1031. u8 country_ie_len)
  1032. {
  1033. struct ieee80211_regdomain *rd = NULL;
  1034. char alpha2[2];
  1035. u32 checksum = 0;
  1036. enum environment_cap env = ENVIRON_ANY;
  1037. if (!last_request)
  1038. return;
  1039. mutex_lock(&cfg80211_drv_mutex);
  1040. /* IE len must be evenly divisible by 2 */
  1041. if (country_ie_len & 0x01)
  1042. goto out;
  1043. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1044. goto out;
  1045. /* Pending country IE processing, this can happen after we
  1046. * call CRDA and wait for a response if a beacon was received before
  1047. * we were able to process the last regulatory_hint_11d() call */
  1048. if (country_ie_regdomain)
  1049. goto out;
  1050. alpha2[0] = country_ie[0];
  1051. alpha2[1] = country_ie[1];
  1052. if (country_ie[2] == 'I')
  1053. env = ENVIRON_INDOOR;
  1054. else if (country_ie[2] == 'O')
  1055. env = ENVIRON_OUTDOOR;
  1056. /* We will run this for *every* beacon processed for the BSSID, so
  1057. * we optimize an early check to exit out early if we don't have to
  1058. * do anything */
  1059. if (likely(last_request->wiphy)) {
  1060. struct cfg80211_registered_device *drv_last_ie;
  1061. drv_last_ie = wiphy_to_dev(last_request->wiphy);
  1062. /* Lets keep this simple -- we trust the first AP
  1063. * after we intersect with CRDA */
  1064. if (likely(last_request->wiphy == wiphy)) {
  1065. /* Ignore IEs coming in on this wiphy with
  1066. * the same alpha2 and environment cap */
  1067. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1068. alpha2) &&
  1069. env == drv_last_ie->env)) {
  1070. goto out;
  1071. }
  1072. /* the wiphy moved on to another BSSID or the AP
  1073. * was reconfigured. XXX: We need to deal with the
  1074. * case where the user suspends and goes to goes
  1075. * to another country, and then gets IEs from an
  1076. * AP with different settings */
  1077. goto out;
  1078. } else {
  1079. /* Ignore IEs coming in on two separate wiphys with
  1080. * the same alpha2 and environment cap */
  1081. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1082. alpha2) &&
  1083. env == drv_last_ie->env)) {
  1084. goto out;
  1085. }
  1086. /* We could potentially intersect though */
  1087. goto out;
  1088. }
  1089. }
  1090. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1091. if (!rd)
  1092. goto out;
  1093. /* This will not happen right now but we leave it here for the
  1094. * the future when we want to add suspend/resume support and having
  1095. * the user move to another country after doing so, or having the user
  1096. * move to another AP. Right now we just trust the first AP. This is why
  1097. * this is marked as likley(). If we hit this before we add this support
  1098. * we want to be informed of it as it would indicate a mistake in the
  1099. * current design */
  1100. if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
  1101. goto out;
  1102. /* We keep this around for when CRDA comes back with a response so
  1103. * we can intersect with that */
  1104. country_ie_regdomain = rd;
  1105. __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
  1106. country_ie_regdomain->alpha2, checksum, env);
  1107. out:
  1108. mutex_unlock(&cfg80211_drv_mutex);
  1109. }
  1110. EXPORT_SYMBOL(regulatory_hint_11d);
  1111. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1112. {
  1113. unsigned int i;
  1114. const struct ieee80211_reg_rule *reg_rule = NULL;
  1115. const struct ieee80211_freq_range *freq_range = NULL;
  1116. const struct ieee80211_power_rule *power_rule = NULL;
  1117. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  1118. "(max_antenna_gain, max_eirp)\n");
  1119. for (i = 0; i < rd->n_reg_rules; i++) {
  1120. reg_rule = &rd->reg_rules[i];
  1121. freq_range = &reg_rule->freq_range;
  1122. power_rule = &reg_rule->power_rule;
  1123. /* There may not be documentation for max antenna gain
  1124. * in certain regions */
  1125. if (power_rule->max_antenna_gain)
  1126. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1127. "(%d mBi, %d mBm)\n",
  1128. freq_range->start_freq_khz,
  1129. freq_range->end_freq_khz,
  1130. freq_range->max_bandwidth_khz,
  1131. power_rule->max_antenna_gain,
  1132. power_rule->max_eirp);
  1133. else
  1134. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1135. "(N/A, %d mBm)\n",
  1136. freq_range->start_freq_khz,
  1137. freq_range->end_freq_khz,
  1138. freq_range->max_bandwidth_khz,
  1139. power_rule->max_eirp);
  1140. }
  1141. }
  1142. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1143. {
  1144. if (is_intersected_alpha2(rd->alpha2)) {
  1145. struct wiphy *wiphy = NULL;
  1146. struct cfg80211_registered_device *drv;
  1147. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  1148. if (last_request->wiphy) {
  1149. wiphy = last_request->wiphy;
  1150. drv = wiphy_to_dev(wiphy);
  1151. printk(KERN_INFO "cfg80211: Current regulatory "
  1152. "domain updated by AP to: %c%c\n",
  1153. drv->country_ie_alpha2[0],
  1154. drv->country_ie_alpha2[1]);
  1155. } else
  1156. printk(KERN_INFO "cfg80211: Current regulatory "
  1157. "domain intersected: \n");
  1158. } else
  1159. printk(KERN_INFO "cfg80211: Current regulatory "
  1160. "domain intersected: \n");
  1161. } else if (is_world_regdom(rd->alpha2))
  1162. printk(KERN_INFO "cfg80211: World regulatory "
  1163. "domain updated:\n");
  1164. else {
  1165. if (is_unknown_alpha2(rd->alpha2))
  1166. printk(KERN_INFO "cfg80211: Regulatory domain "
  1167. "changed to driver built-in settings "
  1168. "(unknown country)\n");
  1169. else
  1170. printk(KERN_INFO "cfg80211: Regulatory domain "
  1171. "changed to country: %c%c\n",
  1172. rd->alpha2[0], rd->alpha2[1]);
  1173. }
  1174. print_rd_rules(rd);
  1175. }
  1176. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1177. {
  1178. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1179. rd->alpha2[0], rd->alpha2[1]);
  1180. print_rd_rules(rd);
  1181. }
  1182. #ifdef CONFIG_CFG80211_REG_DEBUG
  1183. static void reg_country_ie_process_debug(
  1184. const struct ieee80211_regdomain *rd,
  1185. const struct ieee80211_regdomain *country_ie_regdomain,
  1186. const struct ieee80211_regdomain *intersected_rd)
  1187. {
  1188. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1189. print_regdomain_info(country_ie_regdomain);
  1190. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1191. print_regdomain_info(rd);
  1192. if (intersected_rd) {
  1193. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1194. "and get:\n");
  1195. print_regdomain_info(rd);
  1196. return;
  1197. }
  1198. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1199. }
  1200. #else
  1201. static inline void reg_country_ie_process_debug(
  1202. const struct ieee80211_regdomain *rd,
  1203. const struct ieee80211_regdomain *country_ie_regdomain,
  1204. const struct ieee80211_regdomain *intersected_rd)
  1205. {
  1206. }
  1207. #endif
  1208. /* Takes ownership of rd only if it doesn't fail */
  1209. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1210. {
  1211. const struct ieee80211_regdomain *intersected_rd = NULL;
  1212. struct cfg80211_registered_device *drv = NULL;
  1213. struct wiphy *wiphy = NULL;
  1214. /* Some basic sanity checks first */
  1215. if (is_world_regdom(rd->alpha2)) {
  1216. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1217. return -EINVAL;
  1218. update_world_regdomain(rd);
  1219. return 0;
  1220. }
  1221. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1222. !is_unknown_alpha2(rd->alpha2))
  1223. return -EINVAL;
  1224. if (!last_request)
  1225. return -EINVAL;
  1226. /* Lets only bother proceeding on the same alpha2 if the current
  1227. * rd is non static (it means CRDA was present and was used last)
  1228. * and the pending request came in from a country IE */
  1229. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1230. /* If someone else asked us to change the rd lets only bother
  1231. * checking if the alpha2 changes if CRDA was already called */
  1232. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1233. !regdom_changed(rd->alpha2))
  1234. return -EINVAL;
  1235. }
  1236. wiphy = last_request->wiphy;
  1237. /* Now lets set the regulatory domain, update all driver channels
  1238. * and finally inform them of what we have done, in case they want
  1239. * to review or adjust their own settings based on their own
  1240. * internal EEPROM data */
  1241. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1242. return -EINVAL;
  1243. if (!is_valid_rd(rd)) {
  1244. printk(KERN_ERR "cfg80211: Invalid "
  1245. "regulatory domain detected:\n");
  1246. print_regdomain_info(rd);
  1247. return -EINVAL;
  1248. }
  1249. if (!last_request->intersect) {
  1250. int r;
  1251. if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
  1252. reset_regdomains();
  1253. cfg80211_regdomain = rd;
  1254. return 0;
  1255. }
  1256. /* For a driver hint, lets copy the regulatory domain the
  1257. * driver wanted to the wiphy to deal with conflicts */
  1258. BUG_ON(last_request->wiphy->regd);
  1259. r = reg_copy_regd(&last_request->wiphy->regd, rd);
  1260. if (r)
  1261. return r;
  1262. reset_regdomains();
  1263. cfg80211_regdomain = rd;
  1264. return 0;
  1265. }
  1266. /* Intersection requires a bit more work */
  1267. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1268. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1269. if (!intersected_rd)
  1270. return -EINVAL;
  1271. /* We can trash what CRDA provided now.
  1272. * However if a driver requested this specific regulatory
  1273. * domain we keep it for its private use */
  1274. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  1275. last_request->wiphy->regd = rd;
  1276. else
  1277. kfree(rd);
  1278. rd = NULL;
  1279. reset_regdomains();
  1280. cfg80211_regdomain = intersected_rd;
  1281. return 0;
  1282. }
  1283. /*
  1284. * Country IE requests are handled a bit differently, we intersect
  1285. * the country IE rd with what CRDA believes that country should have
  1286. */
  1287. BUG_ON(!country_ie_regdomain);
  1288. if (rd != country_ie_regdomain) {
  1289. /* Intersect what CRDA returned and our what we
  1290. * had built from the Country IE received */
  1291. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1292. reg_country_ie_process_debug(rd, country_ie_regdomain,
  1293. intersected_rd);
  1294. kfree(country_ie_regdomain);
  1295. country_ie_regdomain = NULL;
  1296. } else {
  1297. /* This would happen when CRDA was not present and
  1298. * OLD_REGULATORY was enabled. We intersect our Country
  1299. * IE rd and what was set on cfg80211 originally */
  1300. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1301. }
  1302. if (!intersected_rd)
  1303. return -EINVAL;
  1304. drv = wiphy_to_dev(wiphy);
  1305. drv->country_ie_alpha2[0] = rd->alpha2[0];
  1306. drv->country_ie_alpha2[1] = rd->alpha2[1];
  1307. drv->env = last_request->country_ie_env;
  1308. BUG_ON(intersected_rd == rd);
  1309. kfree(rd);
  1310. rd = NULL;
  1311. reset_regdomains();
  1312. cfg80211_regdomain = intersected_rd;
  1313. return 0;
  1314. }
  1315. /* Use this call to set the current regulatory domain. Conflicts with
  1316. * multiple drivers can be ironed out later. Caller must've already
  1317. * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
  1318. int set_regdom(const struct ieee80211_regdomain *rd)
  1319. {
  1320. int r;
  1321. /* Note that this doesn't update the wiphys, this is done below */
  1322. r = __set_regdom(rd);
  1323. if (r) {
  1324. kfree(rd);
  1325. return r;
  1326. }
  1327. /* This would make this whole thing pointless */
  1328. if (!last_request->intersect)
  1329. BUG_ON(rd != cfg80211_regdomain);
  1330. /* update all wiphys now with the new established regulatory domain */
  1331. update_all_wiphy_regulatory(last_request->initiator);
  1332. print_regdomain(cfg80211_regdomain);
  1333. return r;
  1334. }
  1335. /* Caller must hold cfg80211_drv_mutex */
  1336. void reg_device_remove(struct wiphy *wiphy)
  1337. {
  1338. kfree(wiphy->regd);
  1339. if (!last_request || !last_request->wiphy)
  1340. return;
  1341. if (last_request->wiphy != wiphy)
  1342. return;
  1343. last_request->wiphy = NULL;
  1344. last_request->country_ie_env = ENVIRON_ANY;
  1345. }
  1346. int regulatory_init(void)
  1347. {
  1348. int err;
  1349. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1350. if (IS_ERR(reg_pdev))
  1351. return PTR_ERR(reg_pdev);
  1352. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  1353. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  1354. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  1355. print_regdomain_info(cfg80211_regdomain);
  1356. /* The old code still requests for a new regdomain and if
  1357. * you have CRDA you get it updated, otherwise you get
  1358. * stuck with the static values. We ignore "EU" code as
  1359. * that is not a valid ISO / IEC 3166 alpha2 */
  1360. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  1361. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  1362. ieee80211_regdom, 0, ENVIRON_ANY);
  1363. #else
  1364. cfg80211_regdomain = cfg80211_world_regdom;
  1365. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
  1366. if (err)
  1367. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  1368. "unable to update world regulatory domain, "
  1369. "using static definition\n");
  1370. #endif
  1371. return 0;
  1372. }
  1373. void regulatory_exit(void)
  1374. {
  1375. mutex_lock(&cfg80211_drv_mutex);
  1376. reset_regdomains();
  1377. kfree(country_ie_regdomain);
  1378. country_ie_regdomain = NULL;
  1379. kfree(last_request);
  1380. platform_device_unregister(reg_pdev);
  1381. mutex_unlock(&cfg80211_drv_mutex);
  1382. }