reg.c 87 KB

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