reg.c 65 KB

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