reg.c 53 KB

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