reg.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  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. assert_cfg80211_lock();
  786. return freq_reg_info_regd(wiphy, center_freq,
  787. bandwidth, reg_rule, NULL);
  788. }
  789. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  790. unsigned int chan_idx)
  791. {
  792. int r;
  793. u32 flags;
  794. u32 max_bandwidth = 0;
  795. const struct ieee80211_reg_rule *reg_rule = NULL;
  796. const struct ieee80211_power_rule *power_rule = NULL;
  797. struct ieee80211_supported_band *sband;
  798. struct ieee80211_channel *chan;
  799. struct wiphy *request_wiphy = NULL;
  800. assert_cfg80211_lock();
  801. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  802. sband = wiphy->bands[band];
  803. BUG_ON(chan_idx >= sband->n_channels);
  804. chan = &sband->channels[chan_idx];
  805. flags = chan->orig_flags;
  806. r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
  807. &max_bandwidth, &reg_rule);
  808. if (r) {
  809. /*
  810. * This means no regulatory rule was found in the country IE
  811. * with a frequency range on the center_freq's band, since
  812. * IEEE-802.11 allows for a country IE to have a subset of the
  813. * regulatory information provided in a country we ignore
  814. * disabling the channel unless at least one reg rule was
  815. * found on the center_freq's band. For details see this
  816. * clarification:
  817. *
  818. * http://tinyurl.com/11d-clarification
  819. */
  820. if (r == -ERANGE &&
  821. last_request->initiator ==
  822. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  823. #ifdef CONFIG_CFG80211_REG_DEBUG
  824. printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
  825. "intact on %s - no rule found in band on "
  826. "Country IE\n",
  827. chan->center_freq, wiphy_name(wiphy));
  828. #endif
  829. } else {
  830. /*
  831. * In this case we know the country IE has at least one reg rule
  832. * for the band so we respect its band definitions
  833. */
  834. #ifdef CONFIG_CFG80211_REG_DEBUG
  835. if (last_request->initiator ==
  836. NL80211_REGDOM_SET_BY_COUNTRY_IE)
  837. printk(KERN_DEBUG "cfg80211: Disabling "
  838. "channel %d MHz on %s due to "
  839. "Country IE\n",
  840. chan->center_freq, wiphy_name(wiphy));
  841. #endif
  842. flags |= IEEE80211_CHAN_DISABLED;
  843. chan->flags = flags;
  844. }
  845. return;
  846. }
  847. power_rule = &reg_rule->power_rule;
  848. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  849. request_wiphy && request_wiphy == wiphy &&
  850. request_wiphy->strict_regulatory) {
  851. /*
  852. * This gaurantees the driver's requested regulatory domain
  853. * will always be used as a base for further regulatory
  854. * settings
  855. */
  856. chan->flags = chan->orig_flags =
  857. map_regdom_flags(reg_rule->flags);
  858. chan->max_antenna_gain = chan->orig_mag =
  859. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  860. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  861. chan->max_power = chan->orig_mpwr =
  862. (int) MBM_TO_DBM(power_rule->max_eirp);
  863. return;
  864. }
  865. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  866. chan->max_antenna_gain = min(chan->orig_mag,
  867. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  868. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  869. if (chan->orig_mpwr)
  870. chan->max_power = min(chan->orig_mpwr,
  871. (int) MBM_TO_DBM(power_rule->max_eirp));
  872. else
  873. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  874. }
  875. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  876. {
  877. unsigned int i;
  878. struct ieee80211_supported_band *sband;
  879. BUG_ON(!wiphy->bands[band]);
  880. sband = wiphy->bands[band];
  881. for (i = 0; i < sband->n_channels; i++)
  882. handle_channel(wiphy, band, i);
  883. }
  884. static bool ignore_reg_update(struct wiphy *wiphy,
  885. enum nl80211_reg_initiator initiator)
  886. {
  887. if (!last_request)
  888. return true;
  889. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  890. wiphy->custom_regulatory)
  891. return true;
  892. /*
  893. * wiphy->regd will be set once the device has its own
  894. * desired regulatory domain set
  895. */
  896. if (wiphy->strict_regulatory && !wiphy->regd &&
  897. !is_world_regdom(last_request->alpha2))
  898. return true;
  899. return false;
  900. }
  901. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  902. {
  903. struct cfg80211_registered_device *drv;
  904. list_for_each_entry(drv, &cfg80211_drv_list, list)
  905. wiphy_update_regulatory(&drv->wiphy, initiator);
  906. }
  907. static void handle_reg_beacon(struct wiphy *wiphy,
  908. unsigned int chan_idx,
  909. struct reg_beacon *reg_beacon)
  910. {
  911. #ifdef CONFIG_CFG80211_REG_DEBUG
  912. #define REG_DEBUG_BEACON_FLAG(desc) \
  913. printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
  914. "frequency: %d MHz (Ch %d) on %s\n", \
  915. reg_beacon->chan.center_freq, \
  916. ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
  917. wiphy_name(wiphy));
  918. #else
  919. #define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
  920. #endif
  921. struct ieee80211_supported_band *sband;
  922. struct ieee80211_channel *chan;
  923. assert_cfg80211_lock();
  924. sband = wiphy->bands[reg_beacon->chan.band];
  925. chan = &sband->channels[chan_idx];
  926. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  927. return;
  928. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  929. chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  930. REG_DEBUG_BEACON_FLAG("active scanning");
  931. }
  932. if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
  933. chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
  934. REG_DEBUG_BEACON_FLAG("beaconing");
  935. }
  936. chan->beacon_found = true;
  937. #undef REG_DEBUG_BEACON_FLAG
  938. }
  939. /*
  940. * Called when a scan on a wiphy finds a beacon on
  941. * new channel
  942. */
  943. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  944. struct reg_beacon *reg_beacon)
  945. {
  946. unsigned int i;
  947. struct ieee80211_supported_band *sband;
  948. assert_cfg80211_lock();
  949. if (!wiphy->bands[reg_beacon->chan.band])
  950. return;
  951. sband = wiphy->bands[reg_beacon->chan.band];
  952. for (i = 0; i < sband->n_channels; i++)
  953. handle_reg_beacon(wiphy, i, reg_beacon);
  954. }
  955. /*
  956. * Called upon reg changes or a new wiphy is added
  957. */
  958. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  959. {
  960. unsigned int i;
  961. struct ieee80211_supported_band *sband;
  962. struct reg_beacon *reg_beacon;
  963. assert_cfg80211_lock();
  964. if (list_empty(&reg_beacon_list))
  965. return;
  966. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  967. if (!wiphy->bands[reg_beacon->chan.band])
  968. continue;
  969. sband = wiphy->bands[reg_beacon->chan.band];
  970. for (i = 0; i < sband->n_channels; i++)
  971. handle_reg_beacon(wiphy, i, reg_beacon);
  972. }
  973. }
  974. static bool reg_is_world_roaming(struct wiphy *wiphy)
  975. {
  976. if (is_world_regdom(cfg80211_regdomain->alpha2) ||
  977. (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
  978. return true;
  979. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  980. wiphy->custom_regulatory)
  981. return true;
  982. return false;
  983. }
  984. /* Reap the advantages of previously found beacons */
  985. static void reg_process_beacons(struct wiphy *wiphy)
  986. {
  987. if (!reg_is_world_roaming(wiphy))
  988. return;
  989. wiphy_update_beacon_reg(wiphy);
  990. }
  991. void wiphy_update_regulatory(struct wiphy *wiphy,
  992. enum nl80211_reg_initiator initiator)
  993. {
  994. enum ieee80211_band band;
  995. if (ignore_reg_update(wiphy, initiator))
  996. goto out;
  997. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  998. if (wiphy->bands[band])
  999. handle_band(wiphy, band);
  1000. }
  1001. out:
  1002. reg_process_beacons(wiphy);
  1003. if (wiphy->reg_notifier)
  1004. wiphy->reg_notifier(wiphy, last_request);
  1005. }
  1006. static void handle_channel_custom(struct wiphy *wiphy,
  1007. enum ieee80211_band band,
  1008. unsigned int chan_idx,
  1009. const struct ieee80211_regdomain *regd)
  1010. {
  1011. int r;
  1012. u32 max_bandwidth = 0;
  1013. const struct ieee80211_reg_rule *reg_rule = NULL;
  1014. const struct ieee80211_power_rule *power_rule = NULL;
  1015. struct ieee80211_supported_band *sband;
  1016. struct ieee80211_channel *chan;
  1017. assert_cfg80211_lock();
  1018. sband = wiphy->bands[band];
  1019. BUG_ON(chan_idx >= sband->n_channels);
  1020. chan = &sband->channels[chan_idx];
  1021. r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  1022. &max_bandwidth, &reg_rule, regd);
  1023. if (r) {
  1024. chan->flags = IEEE80211_CHAN_DISABLED;
  1025. return;
  1026. }
  1027. power_rule = &reg_rule->power_rule;
  1028. chan->flags |= map_regdom_flags(reg_rule->flags);
  1029. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1030. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  1031. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  1032. }
  1033. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  1034. const struct ieee80211_regdomain *regd)
  1035. {
  1036. unsigned int i;
  1037. struct ieee80211_supported_band *sband;
  1038. BUG_ON(!wiphy->bands[band]);
  1039. sband = wiphy->bands[band];
  1040. for (i = 0; i < sband->n_channels; i++)
  1041. handle_channel_custom(wiphy, band, i, regd);
  1042. }
  1043. /* Used by drivers prior to wiphy registration */
  1044. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  1045. const struct ieee80211_regdomain *regd)
  1046. {
  1047. enum ieee80211_band band;
  1048. mutex_lock(&cfg80211_mutex);
  1049. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1050. if (wiphy->bands[band])
  1051. handle_band_custom(wiphy, band, regd);
  1052. }
  1053. mutex_unlock(&cfg80211_mutex);
  1054. }
  1055. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  1056. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  1057. const struct ieee80211_regdomain *src_regd)
  1058. {
  1059. struct ieee80211_regdomain *regd;
  1060. int size_of_regd = 0;
  1061. unsigned int i;
  1062. size_of_regd = sizeof(struct ieee80211_regdomain) +
  1063. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  1064. regd = kzalloc(size_of_regd, GFP_KERNEL);
  1065. if (!regd)
  1066. return -ENOMEM;
  1067. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  1068. for (i = 0; i < src_regd->n_reg_rules; i++)
  1069. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  1070. sizeof(struct ieee80211_reg_rule));
  1071. *dst_regd = regd;
  1072. return 0;
  1073. }
  1074. /*
  1075. * Return value which can be used by ignore_request() to indicate
  1076. * it has been determined we should intersect two regulatory domains
  1077. */
  1078. #define REG_INTERSECT 1
  1079. /* This has the logic which determines when a new request
  1080. * should be ignored. */
  1081. static int ignore_request(struct wiphy *wiphy,
  1082. struct regulatory_request *pending_request)
  1083. {
  1084. struct wiphy *last_wiphy = NULL;
  1085. assert_cfg80211_lock();
  1086. /* All initial requests are respected */
  1087. if (!last_request)
  1088. return 0;
  1089. switch (pending_request->initiator) {
  1090. case NL80211_REGDOM_SET_BY_CORE:
  1091. return -EINVAL;
  1092. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1093. last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1094. if (unlikely(!is_an_alpha2(pending_request->alpha2)))
  1095. return -EINVAL;
  1096. if (last_request->initiator ==
  1097. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1098. if (last_wiphy != wiphy) {
  1099. /*
  1100. * Two cards with two APs claiming different
  1101. * different Country IE alpha2s. We could
  1102. * intersect them, but that seems unlikely
  1103. * to be correct. Reject second one for now.
  1104. */
  1105. if (regdom_changes(pending_request->alpha2))
  1106. return -EOPNOTSUPP;
  1107. return -EALREADY;
  1108. }
  1109. /*
  1110. * Two consecutive Country IE hints on the same wiphy.
  1111. * This should be picked up early by the driver/stack
  1112. */
  1113. if (WARN_ON(regdom_changes(pending_request->alpha2)))
  1114. return 0;
  1115. return -EALREADY;
  1116. }
  1117. return REG_INTERSECT;
  1118. case NL80211_REGDOM_SET_BY_DRIVER:
  1119. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
  1120. if (is_old_static_regdom(cfg80211_regdomain))
  1121. return 0;
  1122. if (regdom_changes(pending_request->alpha2))
  1123. return 0;
  1124. return -EALREADY;
  1125. }
  1126. /*
  1127. * This would happen if you unplug and plug your card
  1128. * back in or if you add a new device for which the previously
  1129. * loaded card also agrees on the regulatory domain.
  1130. */
  1131. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1132. !regdom_changes(pending_request->alpha2))
  1133. return -EALREADY;
  1134. return REG_INTERSECT;
  1135. case NL80211_REGDOM_SET_BY_USER:
  1136. if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1137. return REG_INTERSECT;
  1138. /*
  1139. * If the user knows better the user should set the regdom
  1140. * to their country before the IE is picked up
  1141. */
  1142. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
  1143. last_request->intersect)
  1144. return -EOPNOTSUPP;
  1145. /*
  1146. * Process user requests only after previous user/driver/core
  1147. * requests have been processed
  1148. */
  1149. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1150. last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1151. last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
  1152. if (regdom_changes(last_request->alpha2))
  1153. return -EAGAIN;
  1154. }
  1155. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1156. !regdom_changes(pending_request->alpha2))
  1157. return -EALREADY;
  1158. return 0;
  1159. }
  1160. return -EINVAL;
  1161. }
  1162. /**
  1163. * __regulatory_hint - hint to the wireless core a regulatory domain
  1164. * @wiphy: if the hint comes from country information from an AP, this
  1165. * is required to be set to the wiphy that received the information
  1166. * @pending_request: the regulatory request currently being processed
  1167. *
  1168. * The Wireless subsystem can use this function to hint to the wireless core
  1169. * what it believes should be the current regulatory domain.
  1170. *
  1171. * Returns zero if all went fine, %-EALREADY if a regulatory domain had
  1172. * already been set or other standard error codes.
  1173. *
  1174. * Caller must hold &cfg80211_mutex
  1175. */
  1176. static int __regulatory_hint(struct wiphy *wiphy,
  1177. struct regulatory_request *pending_request)
  1178. {
  1179. bool intersect = false;
  1180. int r = 0;
  1181. assert_cfg80211_lock();
  1182. r = ignore_request(wiphy, pending_request);
  1183. if (r == REG_INTERSECT) {
  1184. if (pending_request->initiator ==
  1185. NL80211_REGDOM_SET_BY_DRIVER) {
  1186. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1187. if (r) {
  1188. kfree(pending_request);
  1189. return r;
  1190. }
  1191. }
  1192. intersect = true;
  1193. } else if (r) {
  1194. /*
  1195. * If the regulatory domain being requested by the
  1196. * driver has already been set just copy it to the
  1197. * wiphy
  1198. */
  1199. if (r == -EALREADY &&
  1200. pending_request->initiator ==
  1201. NL80211_REGDOM_SET_BY_DRIVER) {
  1202. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1203. if (r) {
  1204. kfree(pending_request);
  1205. return r;
  1206. }
  1207. r = -EALREADY;
  1208. goto new_request;
  1209. }
  1210. kfree(pending_request);
  1211. return r;
  1212. }
  1213. new_request:
  1214. kfree(last_request);
  1215. last_request = pending_request;
  1216. last_request->intersect = intersect;
  1217. pending_request = NULL;
  1218. /* When r == REG_INTERSECT we do need to call CRDA */
  1219. if (r < 0) {
  1220. /*
  1221. * Since CRDA will not be called in this case as we already
  1222. * have applied the requested regulatory domain before we just
  1223. * inform userspace we have processed the request
  1224. */
  1225. if (r == -EALREADY)
  1226. nl80211_send_reg_change_event(last_request);
  1227. return r;
  1228. }
  1229. return call_crda(last_request->alpha2);
  1230. }
  1231. /* This currently only processes user and driver regulatory hints */
  1232. static void reg_process_hint(struct regulatory_request *reg_request)
  1233. {
  1234. int r = 0;
  1235. struct wiphy *wiphy = NULL;
  1236. BUG_ON(!reg_request->alpha2);
  1237. mutex_lock(&cfg80211_mutex);
  1238. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1239. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1240. if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1241. !wiphy) {
  1242. kfree(reg_request);
  1243. goto out;
  1244. }
  1245. r = __regulatory_hint(wiphy, reg_request);
  1246. /* This is required so that the orig_* parameters are saved */
  1247. if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
  1248. wiphy_update_regulatory(wiphy, reg_request->initiator);
  1249. out:
  1250. mutex_unlock(&cfg80211_mutex);
  1251. }
  1252. /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
  1253. static void reg_process_pending_hints(void)
  1254. {
  1255. struct regulatory_request *reg_request;
  1256. spin_lock(&reg_requests_lock);
  1257. while (!list_empty(&reg_requests_list)) {
  1258. reg_request = list_first_entry(&reg_requests_list,
  1259. struct regulatory_request,
  1260. list);
  1261. list_del_init(&reg_request->list);
  1262. spin_unlock(&reg_requests_lock);
  1263. reg_process_hint(reg_request);
  1264. spin_lock(&reg_requests_lock);
  1265. }
  1266. spin_unlock(&reg_requests_lock);
  1267. }
  1268. /* Processes beacon hints -- this has nothing to do with country IEs */
  1269. static void reg_process_pending_beacon_hints(void)
  1270. {
  1271. struct cfg80211_registered_device *drv;
  1272. struct reg_beacon *pending_beacon, *tmp;
  1273. mutex_lock(&cfg80211_mutex);
  1274. /* This goes through the _pending_ beacon list */
  1275. spin_lock_bh(&reg_pending_beacons_lock);
  1276. if (list_empty(&reg_pending_beacons)) {
  1277. spin_unlock_bh(&reg_pending_beacons_lock);
  1278. goto out;
  1279. }
  1280. list_for_each_entry_safe(pending_beacon, tmp,
  1281. &reg_pending_beacons, list) {
  1282. list_del_init(&pending_beacon->list);
  1283. /* Applies the beacon hint to current wiphys */
  1284. list_for_each_entry(drv, &cfg80211_drv_list, list)
  1285. wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
  1286. /* Remembers the beacon hint for new wiphys or reg changes */
  1287. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1288. }
  1289. spin_unlock_bh(&reg_pending_beacons_lock);
  1290. out:
  1291. mutex_unlock(&cfg80211_mutex);
  1292. }
  1293. static void reg_todo(struct work_struct *work)
  1294. {
  1295. reg_process_pending_hints();
  1296. reg_process_pending_beacon_hints();
  1297. }
  1298. static DECLARE_WORK(reg_work, reg_todo);
  1299. static void queue_regulatory_request(struct regulatory_request *request)
  1300. {
  1301. spin_lock(&reg_requests_lock);
  1302. list_add_tail(&request->list, &reg_requests_list);
  1303. spin_unlock(&reg_requests_lock);
  1304. schedule_work(&reg_work);
  1305. }
  1306. /* Core regulatory hint -- happens once during cfg80211_init() */
  1307. static int regulatory_hint_core(const char *alpha2)
  1308. {
  1309. struct regulatory_request *request;
  1310. BUG_ON(last_request);
  1311. request = kzalloc(sizeof(struct regulatory_request),
  1312. GFP_KERNEL);
  1313. if (!request)
  1314. return -ENOMEM;
  1315. request->alpha2[0] = alpha2[0];
  1316. request->alpha2[1] = alpha2[1];
  1317. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1318. queue_regulatory_request(request);
  1319. return 0;
  1320. }
  1321. /* User hints */
  1322. int regulatory_hint_user(const char *alpha2)
  1323. {
  1324. struct regulatory_request *request;
  1325. BUG_ON(!alpha2);
  1326. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1327. if (!request)
  1328. return -ENOMEM;
  1329. request->wiphy_idx = WIPHY_IDX_STALE;
  1330. request->alpha2[0] = alpha2[0];
  1331. request->alpha2[1] = alpha2[1];
  1332. request->initiator = NL80211_REGDOM_SET_BY_USER,
  1333. queue_regulatory_request(request);
  1334. return 0;
  1335. }
  1336. /* Driver hints */
  1337. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1338. {
  1339. struct regulatory_request *request;
  1340. BUG_ON(!alpha2);
  1341. BUG_ON(!wiphy);
  1342. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1343. if (!request)
  1344. return -ENOMEM;
  1345. request->wiphy_idx = get_wiphy_idx(wiphy);
  1346. /* Must have registered wiphy first */
  1347. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1348. request->alpha2[0] = alpha2[0];
  1349. request->alpha2[1] = alpha2[1];
  1350. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1351. queue_regulatory_request(request);
  1352. return 0;
  1353. }
  1354. EXPORT_SYMBOL(regulatory_hint);
  1355. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1356. u32 country_ie_checksum)
  1357. {
  1358. struct wiphy *request_wiphy;
  1359. assert_cfg80211_lock();
  1360. if (unlikely(last_request->initiator !=
  1361. NL80211_REGDOM_SET_BY_COUNTRY_IE))
  1362. return false;
  1363. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1364. if (!request_wiphy)
  1365. return false;
  1366. if (likely(request_wiphy != wiphy))
  1367. return !country_ie_integrity_changes(country_ie_checksum);
  1368. /*
  1369. * We should not have let these through at this point, they
  1370. * should have been picked up earlier by the first alpha2 check
  1371. * on the device
  1372. */
  1373. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1374. return true;
  1375. return false;
  1376. }
  1377. void regulatory_hint_11d(struct wiphy *wiphy,
  1378. u8 *country_ie,
  1379. u8 country_ie_len)
  1380. {
  1381. struct ieee80211_regdomain *rd = NULL;
  1382. char alpha2[2];
  1383. u32 checksum = 0;
  1384. enum environment_cap env = ENVIRON_ANY;
  1385. struct regulatory_request *request;
  1386. mutex_lock(&cfg80211_mutex);
  1387. if (unlikely(!last_request)) {
  1388. mutex_unlock(&cfg80211_mutex);
  1389. return;
  1390. }
  1391. /* IE len must be evenly divisible by 2 */
  1392. if (country_ie_len & 0x01)
  1393. goto out;
  1394. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1395. goto out;
  1396. /*
  1397. * Pending country IE processing, this can happen after we
  1398. * call CRDA and wait for a response if a beacon was received before
  1399. * we were able to process the last regulatory_hint_11d() call
  1400. */
  1401. if (country_ie_regdomain)
  1402. goto out;
  1403. alpha2[0] = country_ie[0];
  1404. alpha2[1] = country_ie[1];
  1405. if (country_ie[2] == 'I')
  1406. env = ENVIRON_INDOOR;
  1407. else if (country_ie[2] == 'O')
  1408. env = ENVIRON_OUTDOOR;
  1409. /*
  1410. * We will run this for *every* beacon processed for the BSSID, so
  1411. * we optimize an early check to exit out early if we don't have to
  1412. * do anything
  1413. */
  1414. if (likely(last_request->initiator ==
  1415. NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1416. wiphy_idx_valid(last_request->wiphy_idx))) {
  1417. struct cfg80211_registered_device *drv_last_ie;
  1418. drv_last_ie =
  1419. cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
  1420. /*
  1421. * Lets keep this simple -- we trust the first AP
  1422. * after we intersect with CRDA
  1423. */
  1424. if (likely(&drv_last_ie->wiphy == wiphy)) {
  1425. /*
  1426. * Ignore IEs coming in on this wiphy with
  1427. * the same alpha2 and environment cap
  1428. */
  1429. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1430. alpha2) &&
  1431. env == drv_last_ie->env)) {
  1432. goto out;
  1433. }
  1434. /*
  1435. * the wiphy moved on to another BSSID or the AP
  1436. * was reconfigured. XXX: We need to deal with the
  1437. * case where the user suspends and goes to goes
  1438. * to another country, and then gets IEs from an
  1439. * AP with different settings
  1440. */
  1441. goto out;
  1442. } else {
  1443. /*
  1444. * Ignore IEs coming in on two separate wiphys with
  1445. * the same alpha2 and environment cap
  1446. */
  1447. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1448. alpha2) &&
  1449. env == drv_last_ie->env)) {
  1450. goto out;
  1451. }
  1452. /* We could potentially intersect though */
  1453. goto out;
  1454. }
  1455. }
  1456. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1457. if (!rd)
  1458. goto out;
  1459. /*
  1460. * This will not happen right now but we leave it here for the
  1461. * the future when we want to add suspend/resume support and having
  1462. * the user move to another country after doing so, or having the user
  1463. * move to another AP. Right now we just trust the first AP.
  1464. *
  1465. * If we hit this before we add this support we want to be informed of
  1466. * it as it would indicate a mistake in the current design
  1467. */
  1468. if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
  1469. goto free_rd_out;
  1470. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1471. if (!request)
  1472. goto free_rd_out;
  1473. /*
  1474. * We keep this around for when CRDA comes back with a response so
  1475. * we can intersect with that
  1476. */
  1477. country_ie_regdomain = rd;
  1478. request->wiphy_idx = get_wiphy_idx(wiphy);
  1479. request->alpha2[0] = rd->alpha2[0];
  1480. request->alpha2[1] = rd->alpha2[1];
  1481. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  1482. request->country_ie_checksum = checksum;
  1483. request->country_ie_env = env;
  1484. mutex_unlock(&cfg80211_mutex);
  1485. queue_regulatory_request(request);
  1486. return;
  1487. free_rd_out:
  1488. kfree(rd);
  1489. out:
  1490. mutex_unlock(&cfg80211_mutex);
  1491. }
  1492. EXPORT_SYMBOL(regulatory_hint_11d);
  1493. static bool freq_is_chan_12_13_14(u16 freq)
  1494. {
  1495. if (freq == ieee80211_channel_to_frequency(12) ||
  1496. freq == ieee80211_channel_to_frequency(13) ||
  1497. freq == ieee80211_channel_to_frequency(14))
  1498. return true;
  1499. return false;
  1500. }
  1501. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1502. struct ieee80211_channel *beacon_chan,
  1503. gfp_t gfp)
  1504. {
  1505. struct reg_beacon *reg_beacon;
  1506. if (likely((beacon_chan->beacon_found ||
  1507. (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
  1508. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1509. !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
  1510. return 0;
  1511. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1512. if (!reg_beacon)
  1513. return -ENOMEM;
  1514. #ifdef CONFIG_CFG80211_REG_DEBUG
  1515. printk(KERN_DEBUG "cfg80211: Found new beacon on "
  1516. "frequency: %d MHz (Ch %d) on %s\n",
  1517. beacon_chan->center_freq,
  1518. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1519. wiphy_name(wiphy));
  1520. #endif
  1521. memcpy(&reg_beacon->chan, beacon_chan,
  1522. sizeof(struct ieee80211_channel));
  1523. /*
  1524. * Since we can be called from BH or and non-BH context
  1525. * we must use spin_lock_bh()
  1526. */
  1527. spin_lock_bh(&reg_pending_beacons_lock);
  1528. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1529. spin_unlock_bh(&reg_pending_beacons_lock);
  1530. schedule_work(&reg_work);
  1531. return 0;
  1532. }
  1533. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1534. {
  1535. unsigned int i;
  1536. const struct ieee80211_reg_rule *reg_rule = NULL;
  1537. const struct ieee80211_freq_range *freq_range = NULL;
  1538. const struct ieee80211_power_rule *power_rule = NULL;
  1539. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  1540. "(max_antenna_gain, max_eirp)\n");
  1541. for (i = 0; i < rd->n_reg_rules; i++) {
  1542. reg_rule = &rd->reg_rules[i];
  1543. freq_range = &reg_rule->freq_range;
  1544. power_rule = &reg_rule->power_rule;
  1545. /*
  1546. * There may not be documentation for max antenna gain
  1547. * in certain regions
  1548. */
  1549. if (power_rule->max_antenna_gain)
  1550. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1551. "(%d mBi, %d mBm)\n",
  1552. freq_range->start_freq_khz,
  1553. freq_range->end_freq_khz,
  1554. freq_range->max_bandwidth_khz,
  1555. power_rule->max_antenna_gain,
  1556. power_rule->max_eirp);
  1557. else
  1558. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1559. "(N/A, %d mBm)\n",
  1560. freq_range->start_freq_khz,
  1561. freq_range->end_freq_khz,
  1562. freq_range->max_bandwidth_khz,
  1563. power_rule->max_eirp);
  1564. }
  1565. }
  1566. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1567. {
  1568. if (is_intersected_alpha2(rd->alpha2)) {
  1569. if (last_request->initiator ==
  1570. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1571. struct cfg80211_registered_device *drv;
  1572. drv = cfg80211_drv_by_wiphy_idx(
  1573. last_request->wiphy_idx);
  1574. if (drv) {
  1575. printk(KERN_INFO "cfg80211: Current regulatory "
  1576. "domain updated by AP to: %c%c\n",
  1577. drv->country_ie_alpha2[0],
  1578. drv->country_ie_alpha2[1]);
  1579. } else
  1580. printk(KERN_INFO "cfg80211: Current regulatory "
  1581. "domain intersected: \n");
  1582. } else
  1583. printk(KERN_INFO "cfg80211: Current regulatory "
  1584. "domain intersected: \n");
  1585. } else if (is_world_regdom(rd->alpha2))
  1586. printk(KERN_INFO "cfg80211: World regulatory "
  1587. "domain updated:\n");
  1588. else {
  1589. if (is_unknown_alpha2(rd->alpha2))
  1590. printk(KERN_INFO "cfg80211: Regulatory domain "
  1591. "changed to driver built-in settings "
  1592. "(unknown country)\n");
  1593. else
  1594. printk(KERN_INFO "cfg80211: Regulatory domain "
  1595. "changed to country: %c%c\n",
  1596. rd->alpha2[0], rd->alpha2[1]);
  1597. }
  1598. print_rd_rules(rd);
  1599. }
  1600. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1601. {
  1602. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1603. rd->alpha2[0], rd->alpha2[1]);
  1604. print_rd_rules(rd);
  1605. }
  1606. #ifdef CONFIG_CFG80211_REG_DEBUG
  1607. static void reg_country_ie_process_debug(
  1608. const struct ieee80211_regdomain *rd,
  1609. const struct ieee80211_regdomain *country_ie_regdomain,
  1610. const struct ieee80211_regdomain *intersected_rd)
  1611. {
  1612. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1613. print_regdomain_info(country_ie_regdomain);
  1614. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1615. print_regdomain_info(rd);
  1616. if (intersected_rd) {
  1617. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1618. "and get:\n");
  1619. print_regdomain_info(intersected_rd);
  1620. return;
  1621. }
  1622. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1623. }
  1624. #else
  1625. static inline void reg_country_ie_process_debug(
  1626. const struct ieee80211_regdomain *rd,
  1627. const struct ieee80211_regdomain *country_ie_regdomain,
  1628. const struct ieee80211_regdomain *intersected_rd)
  1629. {
  1630. }
  1631. #endif
  1632. /* Takes ownership of rd only if it doesn't fail */
  1633. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1634. {
  1635. const struct ieee80211_regdomain *intersected_rd = NULL;
  1636. struct cfg80211_registered_device *drv = NULL;
  1637. struct wiphy *request_wiphy;
  1638. /* Some basic sanity checks first */
  1639. if (is_world_regdom(rd->alpha2)) {
  1640. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1641. return -EINVAL;
  1642. update_world_regdomain(rd);
  1643. return 0;
  1644. }
  1645. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1646. !is_unknown_alpha2(rd->alpha2))
  1647. return -EINVAL;
  1648. if (!last_request)
  1649. return -EINVAL;
  1650. /*
  1651. * Lets only bother proceeding on the same alpha2 if the current
  1652. * rd is non static (it means CRDA was present and was used last)
  1653. * and the pending request came in from a country IE
  1654. */
  1655. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1656. /*
  1657. * If someone else asked us to change the rd lets only bother
  1658. * checking if the alpha2 changes if CRDA was already called
  1659. */
  1660. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1661. !regdom_changes(rd->alpha2))
  1662. return -EINVAL;
  1663. }
  1664. /*
  1665. * Now lets set the regulatory domain, update all driver channels
  1666. * and finally inform them of what we have done, in case they want
  1667. * to review or adjust their own settings based on their own
  1668. * internal EEPROM data
  1669. */
  1670. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1671. return -EINVAL;
  1672. if (!is_valid_rd(rd)) {
  1673. printk(KERN_ERR "cfg80211: Invalid "
  1674. "regulatory domain detected:\n");
  1675. print_regdomain_info(rd);
  1676. return -EINVAL;
  1677. }
  1678. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1679. if (!last_request->intersect) {
  1680. int r;
  1681. if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
  1682. reset_regdomains();
  1683. cfg80211_regdomain = rd;
  1684. return 0;
  1685. }
  1686. /*
  1687. * For a driver hint, lets copy the regulatory domain the
  1688. * driver wanted to the wiphy to deal with conflicts
  1689. */
  1690. BUG_ON(request_wiphy->regd);
  1691. r = reg_copy_regd(&request_wiphy->regd, rd);
  1692. if (r)
  1693. return r;
  1694. reset_regdomains();
  1695. cfg80211_regdomain = rd;
  1696. return 0;
  1697. }
  1698. /* Intersection requires a bit more work */
  1699. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1700. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1701. if (!intersected_rd)
  1702. return -EINVAL;
  1703. /*
  1704. * We can trash what CRDA provided now.
  1705. * However if a driver requested this specific regulatory
  1706. * domain we keep it for its private use
  1707. */
  1708. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
  1709. request_wiphy->regd = rd;
  1710. else
  1711. kfree(rd);
  1712. rd = NULL;
  1713. reset_regdomains();
  1714. cfg80211_regdomain = intersected_rd;
  1715. return 0;
  1716. }
  1717. /*
  1718. * Country IE requests are handled a bit differently, we intersect
  1719. * the country IE rd with what CRDA believes that country should have
  1720. */
  1721. BUG_ON(!country_ie_regdomain);
  1722. BUG_ON(rd == country_ie_regdomain);
  1723. /*
  1724. * Intersect what CRDA returned and our what we
  1725. * had built from the Country IE received
  1726. */
  1727. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1728. reg_country_ie_process_debug(rd,
  1729. country_ie_regdomain,
  1730. intersected_rd);
  1731. kfree(country_ie_regdomain);
  1732. country_ie_regdomain = NULL;
  1733. if (!intersected_rd)
  1734. return -EINVAL;
  1735. drv = wiphy_to_dev(request_wiphy);
  1736. drv->country_ie_alpha2[0] = rd->alpha2[0];
  1737. drv->country_ie_alpha2[1] = rd->alpha2[1];
  1738. drv->env = last_request->country_ie_env;
  1739. BUG_ON(intersected_rd == rd);
  1740. kfree(rd);
  1741. rd = NULL;
  1742. reset_regdomains();
  1743. cfg80211_regdomain = intersected_rd;
  1744. return 0;
  1745. }
  1746. /*
  1747. * Use this call to set the current regulatory domain. Conflicts with
  1748. * multiple drivers can be ironed out later. Caller must've already
  1749. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  1750. */
  1751. int set_regdom(const struct ieee80211_regdomain *rd)
  1752. {
  1753. int r;
  1754. assert_cfg80211_lock();
  1755. /* Note that this doesn't update the wiphys, this is done below */
  1756. r = __set_regdom(rd);
  1757. if (r) {
  1758. kfree(rd);
  1759. return r;
  1760. }
  1761. /* This would make this whole thing pointless */
  1762. if (!last_request->intersect)
  1763. BUG_ON(rd != cfg80211_regdomain);
  1764. /* update all wiphys now with the new established regulatory domain */
  1765. update_all_wiphy_regulatory(last_request->initiator);
  1766. print_regdomain(cfg80211_regdomain);
  1767. nl80211_send_reg_change_event(last_request);
  1768. return r;
  1769. }
  1770. /* Caller must hold cfg80211_mutex */
  1771. void reg_device_remove(struct wiphy *wiphy)
  1772. {
  1773. struct wiphy *request_wiphy = NULL;
  1774. assert_cfg80211_lock();
  1775. if (last_request)
  1776. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1777. kfree(wiphy->regd);
  1778. if (!last_request || !request_wiphy)
  1779. return;
  1780. if (request_wiphy != wiphy)
  1781. return;
  1782. last_request->wiphy_idx = WIPHY_IDX_STALE;
  1783. last_request->country_ie_env = ENVIRON_ANY;
  1784. }
  1785. int regulatory_init(void)
  1786. {
  1787. int err = 0;
  1788. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1789. if (IS_ERR(reg_pdev))
  1790. return PTR_ERR(reg_pdev);
  1791. spin_lock_init(&reg_requests_lock);
  1792. spin_lock_init(&reg_pending_beacons_lock);
  1793. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  1794. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  1795. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  1796. print_regdomain_info(cfg80211_regdomain);
  1797. /*
  1798. * The old code still requests for a new regdomain and if
  1799. * you have CRDA you get it updated, otherwise you get
  1800. * stuck with the static values. Since "EU" is not a valid
  1801. * ISO / IEC 3166 alpha2 code we can't expect userpace to
  1802. * give us a regulatory domain for it. We need last_request
  1803. * iniitalized though so lets just send a request which we
  1804. * know will be ignored... this crap will be removed once
  1805. * OLD_REG dies.
  1806. */
  1807. err = regulatory_hint_core(ieee80211_regdom);
  1808. #else
  1809. cfg80211_regdomain = cfg80211_world_regdom;
  1810. err = regulatory_hint_core(ieee80211_regdom);
  1811. #endif
  1812. if (err) {
  1813. if (err == -ENOMEM)
  1814. return err;
  1815. /*
  1816. * N.B. kobject_uevent_env() can fail mainly for when we're out
  1817. * memory which is handled and propagated appropriately above
  1818. * but it can also fail during a netlink_broadcast() or during
  1819. * early boot for call_usermodehelper(). For now treat these
  1820. * errors as non-fatal.
  1821. */
  1822. printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
  1823. "to call CRDA during init");
  1824. #ifdef CONFIG_CFG80211_REG_DEBUG
  1825. /* We want to find out exactly why when debugging */
  1826. WARN_ON(err);
  1827. #endif
  1828. }
  1829. return 0;
  1830. }
  1831. void regulatory_exit(void)
  1832. {
  1833. struct regulatory_request *reg_request, *tmp;
  1834. struct reg_beacon *reg_beacon, *btmp;
  1835. cancel_work_sync(&reg_work);
  1836. mutex_lock(&cfg80211_mutex);
  1837. reset_regdomains();
  1838. kfree(country_ie_regdomain);
  1839. country_ie_regdomain = NULL;
  1840. kfree(last_request);
  1841. platform_device_unregister(reg_pdev);
  1842. spin_lock_bh(&reg_pending_beacons_lock);
  1843. if (!list_empty(&reg_pending_beacons)) {
  1844. list_for_each_entry_safe(reg_beacon, btmp,
  1845. &reg_pending_beacons, list) {
  1846. list_del(&reg_beacon->list);
  1847. kfree(reg_beacon);
  1848. }
  1849. }
  1850. spin_unlock_bh(&reg_pending_beacons_lock);
  1851. if (!list_empty(&reg_beacon_list)) {
  1852. list_for_each_entry_safe(reg_beacon, btmp,
  1853. &reg_beacon_list, list) {
  1854. list_del(&reg_beacon->list);
  1855. kfree(reg_beacon);
  1856. }
  1857. }
  1858. spin_lock(&reg_requests_lock);
  1859. if (!list_empty(&reg_requests_list)) {
  1860. list_for_each_entry_safe(reg_request, tmp,
  1861. &reg_requests_list, list) {
  1862. list_del(&reg_request->list);
  1863. kfree(reg_request);
  1864. }
  1865. }
  1866. spin_unlock(&reg_requests_lock);
  1867. mutex_unlock(&cfg80211_mutex);
  1868. }