reg.c 74 KB

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