reg.c 85 KB

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