reg.c 56 KB

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