reg.c 83 KB

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