reg.c 65 KB

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