reg.c 54 KB

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