reg.c 46 KB

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