reg.c 83 KB

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