reg.c 62 KB

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