reg.c 70 KB

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