reg.c 59 KB

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