reg.c 65 KB

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