reg.c 86 KB

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