reg.c 83 KB

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