reg.c 58 KB

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