reg.c 85 KB

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