reg.c 63 KB

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