reg.c 76 KB

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