reg.c 63 KB

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