reg.c 57 KB

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