reg.c 61 KB

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