reg.c 51 KB

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