reg.c 70 KB

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