reg.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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. #ifdef CONFIG_CFG80211_REG_DEBUG
  600. static const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
  601. {
  602. switch (initiator) {
  603. case NL80211_REGDOM_SET_BY_CORE:
  604. return "Set by core";
  605. case NL80211_REGDOM_SET_BY_USER:
  606. return "Set by user";
  607. case NL80211_REGDOM_SET_BY_DRIVER:
  608. return "Set by driver";
  609. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  610. return "Set by country IE";
  611. default:
  612. WARN_ON(1);
  613. return "Set by bug";
  614. }
  615. }
  616. #endif
  617. /*
  618. * Note that right now we assume the desired channel bandwidth
  619. * is always 20 MHz for each individual channel (HT40 uses 20 MHz
  620. * per channel, the primary and the extension channel). To support
  621. * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
  622. * new ieee80211_channel.target_bw and re run the regulatory check
  623. * on the wiphy with the target_bw specified. Then we can simply use
  624. * that below for the desired_bw_khz below.
  625. */
  626. static void handle_channel(struct wiphy *wiphy,
  627. enum nl80211_reg_initiator initiator,
  628. enum ieee80211_band band,
  629. unsigned int chan_idx)
  630. {
  631. int r;
  632. u32 flags, bw_flags = 0;
  633. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  634. const struct ieee80211_reg_rule *reg_rule = NULL;
  635. const struct ieee80211_power_rule *power_rule = NULL;
  636. const struct ieee80211_freq_range *freq_range = NULL;
  637. struct ieee80211_supported_band *sband;
  638. struct ieee80211_channel *chan;
  639. struct wiphy *request_wiphy = NULL;
  640. assert_cfg80211_lock();
  641. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  642. sband = wiphy->bands[band];
  643. BUG_ON(chan_idx >= sband->n_channels);
  644. chan = &sband->channels[chan_idx];
  645. flags = chan->orig_flags;
  646. r = freq_reg_info(wiphy,
  647. MHZ_TO_KHZ(chan->center_freq),
  648. desired_bw_khz,
  649. &reg_rule);
  650. if (r) {
  651. /*
  652. * We will disable all channels that do not match our
  653. * recieved regulatory rule unless the hint is coming
  654. * from a Country IE and the Country IE had no information
  655. * about a band. The IEEE 802.11 spec allows for an AP
  656. * to send only a subset of the regulatory rules allowed,
  657. * so an AP in the US that only supports 2.4 GHz may only send
  658. * a country IE with information for the 2.4 GHz band
  659. * while 5 GHz is still supported.
  660. */
  661. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  662. r == -ERANGE)
  663. return;
  664. REG_DBG_PRINT("cfg80211: Disabling freq %d MHz\n",
  665. chan->center_freq);
  666. chan->flags = IEEE80211_CHAN_DISABLED;
  667. return;
  668. }
  669. power_rule = &reg_rule->power_rule;
  670. freq_range = &reg_rule->freq_range;
  671. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  672. bw_flags = IEEE80211_CHAN_NO_HT40;
  673. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  674. request_wiphy && request_wiphy == wiphy &&
  675. request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  676. /*
  677. * This gaurantees the driver's requested regulatory domain
  678. * will always be used as a base for further regulatory
  679. * settings
  680. */
  681. chan->flags = chan->orig_flags =
  682. map_regdom_flags(reg_rule->flags) | bw_flags;
  683. chan->max_antenna_gain = chan->orig_mag =
  684. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  685. chan->max_power = chan->orig_mpwr =
  686. (int) MBM_TO_DBM(power_rule->max_eirp);
  687. return;
  688. }
  689. chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
  690. chan->max_antenna_gain = min(chan->orig_mag,
  691. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  692. if (chan->orig_mpwr)
  693. chan->max_power = min(chan->orig_mpwr,
  694. (int) MBM_TO_DBM(power_rule->max_eirp));
  695. else
  696. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  697. }
  698. static void handle_band(struct wiphy *wiphy,
  699. enum ieee80211_band band,
  700. enum nl80211_reg_initiator initiator)
  701. {
  702. unsigned int i;
  703. struct ieee80211_supported_band *sband;
  704. BUG_ON(!wiphy->bands[band]);
  705. sband = wiphy->bands[band];
  706. for (i = 0; i < sband->n_channels; i++)
  707. handle_channel(wiphy, initiator, band, i);
  708. }
  709. static bool ignore_reg_update(struct wiphy *wiphy,
  710. enum nl80211_reg_initiator initiator)
  711. {
  712. if (!last_request) {
  713. REG_DBG_PRINT("cfg80211: Ignoring regulatory request %s since "
  714. "last_request is not set\n",
  715. reg_initiator_name(initiator));
  716. return true;
  717. }
  718. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  719. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
  720. REG_DBG_PRINT("cfg80211: Ignoring regulatory request %s "
  721. "since the driver uses its own custom "
  722. "regulatory domain ",
  723. reg_initiator_name(initiator));
  724. return true;
  725. }
  726. /*
  727. * wiphy->regd will be set once the device has its own
  728. * desired regulatory domain set
  729. */
  730. if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
  731. initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  732. !is_world_regdom(last_request->alpha2)) {
  733. REG_DBG_PRINT("cfg80211: Ignoring regulatory request %s "
  734. "since the driver requires its own regulaotry "
  735. "domain to be set first",
  736. reg_initiator_name(initiator));
  737. return true;
  738. }
  739. return false;
  740. }
  741. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  742. {
  743. struct cfg80211_registered_device *rdev;
  744. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  745. wiphy_update_regulatory(&rdev->wiphy, initiator);
  746. }
  747. static void handle_reg_beacon(struct wiphy *wiphy,
  748. unsigned int chan_idx,
  749. struct reg_beacon *reg_beacon)
  750. {
  751. struct ieee80211_supported_band *sband;
  752. struct ieee80211_channel *chan;
  753. bool channel_changed = false;
  754. struct ieee80211_channel chan_before;
  755. assert_cfg80211_lock();
  756. sband = wiphy->bands[reg_beacon->chan.band];
  757. chan = &sband->channels[chan_idx];
  758. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  759. return;
  760. if (chan->beacon_found)
  761. return;
  762. chan->beacon_found = true;
  763. if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
  764. return;
  765. chan_before.center_freq = chan->center_freq;
  766. chan_before.flags = chan->flags;
  767. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  768. chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  769. channel_changed = true;
  770. }
  771. if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
  772. chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
  773. channel_changed = true;
  774. }
  775. if (channel_changed)
  776. nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
  777. }
  778. /*
  779. * Called when a scan on a wiphy finds a beacon on
  780. * new channel
  781. */
  782. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  783. struct reg_beacon *reg_beacon)
  784. {
  785. unsigned int i;
  786. struct ieee80211_supported_band *sband;
  787. assert_cfg80211_lock();
  788. if (!wiphy->bands[reg_beacon->chan.band])
  789. return;
  790. sband = wiphy->bands[reg_beacon->chan.band];
  791. for (i = 0; i < sband->n_channels; i++)
  792. handle_reg_beacon(wiphy, i, reg_beacon);
  793. }
  794. /*
  795. * Called upon reg changes or a new wiphy is added
  796. */
  797. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  798. {
  799. unsigned int i;
  800. struct ieee80211_supported_band *sband;
  801. struct reg_beacon *reg_beacon;
  802. assert_cfg80211_lock();
  803. if (list_empty(&reg_beacon_list))
  804. return;
  805. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  806. if (!wiphy->bands[reg_beacon->chan.band])
  807. continue;
  808. sband = wiphy->bands[reg_beacon->chan.band];
  809. for (i = 0; i < sband->n_channels; i++)
  810. handle_reg_beacon(wiphy, i, reg_beacon);
  811. }
  812. }
  813. static bool reg_is_world_roaming(struct wiphy *wiphy)
  814. {
  815. if (is_world_regdom(cfg80211_regdomain->alpha2) ||
  816. (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
  817. return true;
  818. if (last_request &&
  819. last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  820. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  821. return true;
  822. return false;
  823. }
  824. /* Reap the advantages of previously found beacons */
  825. static void reg_process_beacons(struct wiphy *wiphy)
  826. {
  827. /*
  828. * Means we are just firing up cfg80211, so no beacons would
  829. * have been processed yet.
  830. */
  831. if (!last_request)
  832. return;
  833. if (!reg_is_world_roaming(wiphy))
  834. return;
  835. wiphy_update_beacon_reg(wiphy);
  836. }
  837. static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
  838. {
  839. if (!chan)
  840. return true;
  841. if (chan->flags & IEEE80211_CHAN_DISABLED)
  842. return true;
  843. /* This would happen when regulatory rules disallow HT40 completely */
  844. if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
  845. return true;
  846. return false;
  847. }
  848. static void reg_process_ht_flags_channel(struct wiphy *wiphy,
  849. enum ieee80211_band band,
  850. unsigned int chan_idx)
  851. {
  852. struct ieee80211_supported_band *sband;
  853. struct ieee80211_channel *channel;
  854. struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  855. unsigned int i;
  856. assert_cfg80211_lock();
  857. sband = wiphy->bands[band];
  858. BUG_ON(chan_idx >= sband->n_channels);
  859. channel = &sband->channels[chan_idx];
  860. if (is_ht40_not_allowed(channel)) {
  861. channel->flags |= IEEE80211_CHAN_NO_HT40;
  862. return;
  863. }
  864. /*
  865. * We need to ensure the extension channels exist to
  866. * be able to use HT40- or HT40+, this finds them (or not)
  867. */
  868. for (i = 0; i < sband->n_channels; i++) {
  869. struct ieee80211_channel *c = &sband->channels[i];
  870. if (c->center_freq == (channel->center_freq - 20))
  871. channel_before = c;
  872. if (c->center_freq == (channel->center_freq + 20))
  873. channel_after = c;
  874. }
  875. /*
  876. * Please note that this assumes target bandwidth is 20 MHz,
  877. * if that ever changes we also need to change the below logic
  878. * to include that as well.
  879. */
  880. if (is_ht40_not_allowed(channel_before))
  881. channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
  882. else
  883. channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  884. if (is_ht40_not_allowed(channel_after))
  885. channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
  886. else
  887. channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  888. }
  889. static void reg_process_ht_flags_band(struct wiphy *wiphy,
  890. enum ieee80211_band band)
  891. {
  892. unsigned int i;
  893. struct ieee80211_supported_band *sband;
  894. BUG_ON(!wiphy->bands[band]);
  895. sband = wiphy->bands[band];
  896. for (i = 0; i < sband->n_channels; i++)
  897. reg_process_ht_flags_channel(wiphy, band, i);
  898. }
  899. static void reg_process_ht_flags(struct wiphy *wiphy)
  900. {
  901. enum ieee80211_band band;
  902. if (!wiphy)
  903. return;
  904. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  905. if (wiphy->bands[band])
  906. reg_process_ht_flags_band(wiphy, band);
  907. }
  908. }
  909. void wiphy_update_regulatory(struct wiphy *wiphy,
  910. enum nl80211_reg_initiator initiator)
  911. {
  912. enum ieee80211_band band;
  913. if (ignore_reg_update(wiphy, initiator))
  914. goto out;
  915. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  916. if (wiphy->bands[band])
  917. handle_band(wiphy, band, initiator);
  918. }
  919. out:
  920. reg_process_beacons(wiphy);
  921. reg_process_ht_flags(wiphy);
  922. if (wiphy->reg_notifier)
  923. wiphy->reg_notifier(wiphy, last_request);
  924. }
  925. static void handle_channel_custom(struct wiphy *wiphy,
  926. enum ieee80211_band band,
  927. unsigned int chan_idx,
  928. const struct ieee80211_regdomain *regd)
  929. {
  930. int r;
  931. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  932. u32 bw_flags = 0;
  933. const struct ieee80211_reg_rule *reg_rule = NULL;
  934. const struct ieee80211_power_rule *power_rule = NULL;
  935. const struct ieee80211_freq_range *freq_range = NULL;
  936. struct ieee80211_supported_band *sband;
  937. struct ieee80211_channel *chan;
  938. assert_reg_lock();
  939. sband = wiphy->bands[band];
  940. BUG_ON(chan_idx >= sband->n_channels);
  941. chan = &sband->channels[chan_idx];
  942. r = freq_reg_info_regd(wiphy,
  943. MHZ_TO_KHZ(chan->center_freq),
  944. desired_bw_khz,
  945. &reg_rule,
  946. regd);
  947. if (r) {
  948. chan->flags = IEEE80211_CHAN_DISABLED;
  949. return;
  950. }
  951. power_rule = &reg_rule->power_rule;
  952. freq_range = &reg_rule->freq_range;
  953. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  954. bw_flags = IEEE80211_CHAN_NO_HT40;
  955. chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
  956. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  957. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  958. }
  959. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  960. const struct ieee80211_regdomain *regd)
  961. {
  962. unsigned int i;
  963. struct ieee80211_supported_band *sband;
  964. BUG_ON(!wiphy->bands[band]);
  965. sband = wiphy->bands[band];
  966. for (i = 0; i < sband->n_channels; i++)
  967. handle_channel_custom(wiphy, band, i, regd);
  968. }
  969. /* Used by drivers prior to wiphy registration */
  970. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  971. const struct ieee80211_regdomain *regd)
  972. {
  973. enum ieee80211_band band;
  974. unsigned int bands_set = 0;
  975. mutex_lock(&reg_mutex);
  976. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  977. if (!wiphy->bands[band])
  978. continue;
  979. handle_band_custom(wiphy, band, regd);
  980. bands_set++;
  981. }
  982. mutex_unlock(&reg_mutex);
  983. /*
  984. * no point in calling this if it won't have any effect
  985. * on your device's supportd bands.
  986. */
  987. WARN_ON(!bands_set);
  988. }
  989. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  990. /*
  991. * Return value which can be used by ignore_request() to indicate
  992. * it has been determined we should intersect two regulatory domains
  993. */
  994. #define REG_INTERSECT 1
  995. /* This has the logic which determines when a new request
  996. * should be ignored. */
  997. static int ignore_request(struct wiphy *wiphy,
  998. struct regulatory_request *pending_request)
  999. {
  1000. struct wiphy *last_wiphy = NULL;
  1001. assert_cfg80211_lock();
  1002. /* All initial requests are respected */
  1003. if (!last_request)
  1004. return 0;
  1005. switch (pending_request->initiator) {
  1006. case NL80211_REGDOM_SET_BY_CORE:
  1007. return 0;
  1008. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1009. last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1010. if (unlikely(!is_an_alpha2(pending_request->alpha2)))
  1011. return -EINVAL;
  1012. if (last_request->initiator ==
  1013. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1014. if (last_wiphy != wiphy) {
  1015. /*
  1016. * Two cards with two APs claiming different
  1017. * Country IE alpha2s. We could
  1018. * intersect them, but that seems unlikely
  1019. * to be correct. Reject second one for now.
  1020. */
  1021. if (regdom_changes(pending_request->alpha2))
  1022. return -EOPNOTSUPP;
  1023. return -EALREADY;
  1024. }
  1025. /*
  1026. * Two consecutive Country IE hints on the same wiphy.
  1027. * This should be picked up early by the driver/stack
  1028. */
  1029. if (WARN_ON(regdom_changes(pending_request->alpha2)))
  1030. return 0;
  1031. return -EALREADY;
  1032. }
  1033. return 0;
  1034. case NL80211_REGDOM_SET_BY_DRIVER:
  1035. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
  1036. if (regdom_changes(pending_request->alpha2))
  1037. return 0;
  1038. return -EALREADY;
  1039. }
  1040. /*
  1041. * This would happen if you unplug and plug your card
  1042. * back in or if you add a new device for which the previously
  1043. * loaded card also agrees on the regulatory domain.
  1044. */
  1045. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1046. !regdom_changes(pending_request->alpha2))
  1047. return -EALREADY;
  1048. return REG_INTERSECT;
  1049. case NL80211_REGDOM_SET_BY_USER:
  1050. if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1051. return REG_INTERSECT;
  1052. /*
  1053. * If the user knows better the user should set the regdom
  1054. * to their country before the IE is picked up
  1055. */
  1056. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
  1057. last_request->intersect)
  1058. return -EOPNOTSUPP;
  1059. /*
  1060. * Process user requests only after previous user/driver/core
  1061. * requests have been processed
  1062. */
  1063. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1064. last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1065. last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
  1066. if (regdom_changes(last_request->alpha2))
  1067. return -EAGAIN;
  1068. }
  1069. if (!regdom_changes(pending_request->alpha2))
  1070. return -EALREADY;
  1071. return 0;
  1072. }
  1073. return -EINVAL;
  1074. }
  1075. /**
  1076. * __regulatory_hint - hint to the wireless core a regulatory domain
  1077. * @wiphy: if the hint comes from country information from an AP, this
  1078. * is required to be set to the wiphy that received the information
  1079. * @pending_request: the regulatory request currently being processed
  1080. *
  1081. * The Wireless subsystem can use this function to hint to the wireless core
  1082. * what it believes should be the current regulatory domain.
  1083. *
  1084. * Returns zero if all went fine, %-EALREADY if a regulatory domain had
  1085. * already been set or other standard error codes.
  1086. *
  1087. * Caller must hold &cfg80211_mutex and &reg_mutex
  1088. */
  1089. static int __regulatory_hint(struct wiphy *wiphy,
  1090. struct regulatory_request *pending_request)
  1091. {
  1092. bool intersect = false;
  1093. int r = 0;
  1094. assert_cfg80211_lock();
  1095. r = ignore_request(wiphy, pending_request);
  1096. if (r == REG_INTERSECT) {
  1097. if (pending_request->initiator ==
  1098. NL80211_REGDOM_SET_BY_DRIVER) {
  1099. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1100. if (r) {
  1101. kfree(pending_request);
  1102. return r;
  1103. }
  1104. }
  1105. intersect = true;
  1106. } else if (r) {
  1107. /*
  1108. * If the regulatory domain being requested by the
  1109. * driver has already been set just copy it to the
  1110. * wiphy
  1111. */
  1112. if (r == -EALREADY &&
  1113. pending_request->initiator ==
  1114. NL80211_REGDOM_SET_BY_DRIVER) {
  1115. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1116. if (r) {
  1117. kfree(pending_request);
  1118. return r;
  1119. }
  1120. r = -EALREADY;
  1121. goto new_request;
  1122. }
  1123. kfree(pending_request);
  1124. return r;
  1125. }
  1126. new_request:
  1127. kfree(last_request);
  1128. last_request = pending_request;
  1129. last_request->intersect = intersect;
  1130. pending_request = NULL;
  1131. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
  1132. user_alpha2[0] = last_request->alpha2[0];
  1133. user_alpha2[1] = last_request->alpha2[1];
  1134. }
  1135. /* When r == REG_INTERSECT we do need to call CRDA */
  1136. if (r < 0) {
  1137. /*
  1138. * Since CRDA will not be called in this case as we already
  1139. * have applied the requested regulatory domain before we just
  1140. * inform userspace we have processed the request
  1141. */
  1142. if (r == -EALREADY)
  1143. nl80211_send_reg_change_event(last_request);
  1144. return r;
  1145. }
  1146. return call_crda(last_request->alpha2);
  1147. }
  1148. /* This processes *all* regulatory hints */
  1149. static void reg_process_hint(struct regulatory_request *reg_request)
  1150. {
  1151. int r = 0;
  1152. struct wiphy *wiphy = NULL;
  1153. enum nl80211_reg_initiator initiator = reg_request->initiator;
  1154. BUG_ON(!reg_request->alpha2);
  1155. mutex_lock(&cfg80211_mutex);
  1156. mutex_lock(&reg_mutex);
  1157. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1158. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1159. if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1160. !wiphy) {
  1161. kfree(reg_request);
  1162. goto out;
  1163. }
  1164. r = __regulatory_hint(wiphy, reg_request);
  1165. /* This is required so that the orig_* parameters are saved */
  1166. if (r == -EALREADY && wiphy &&
  1167. wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
  1168. wiphy_update_regulatory(wiphy, initiator);
  1169. out:
  1170. mutex_unlock(&reg_mutex);
  1171. mutex_unlock(&cfg80211_mutex);
  1172. }
  1173. /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
  1174. static void reg_process_pending_hints(void)
  1175. {
  1176. struct regulatory_request *reg_request;
  1177. spin_lock(&reg_requests_lock);
  1178. while (!list_empty(&reg_requests_list)) {
  1179. reg_request = list_first_entry(&reg_requests_list,
  1180. struct regulatory_request,
  1181. list);
  1182. list_del_init(&reg_request->list);
  1183. spin_unlock(&reg_requests_lock);
  1184. reg_process_hint(reg_request);
  1185. spin_lock(&reg_requests_lock);
  1186. }
  1187. spin_unlock(&reg_requests_lock);
  1188. }
  1189. /* Processes beacon hints -- this has nothing to do with country IEs */
  1190. static void reg_process_pending_beacon_hints(void)
  1191. {
  1192. struct cfg80211_registered_device *rdev;
  1193. struct reg_beacon *pending_beacon, *tmp;
  1194. /*
  1195. * No need to hold the reg_mutex here as we just touch wiphys
  1196. * and do not read or access regulatory variables.
  1197. */
  1198. mutex_lock(&cfg80211_mutex);
  1199. /* This goes through the _pending_ beacon list */
  1200. spin_lock_bh(&reg_pending_beacons_lock);
  1201. if (list_empty(&reg_pending_beacons)) {
  1202. spin_unlock_bh(&reg_pending_beacons_lock);
  1203. goto out;
  1204. }
  1205. list_for_each_entry_safe(pending_beacon, tmp,
  1206. &reg_pending_beacons, list) {
  1207. list_del_init(&pending_beacon->list);
  1208. /* Applies the beacon hint to current wiphys */
  1209. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1210. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  1211. /* Remembers the beacon hint for new wiphys or reg changes */
  1212. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1213. }
  1214. spin_unlock_bh(&reg_pending_beacons_lock);
  1215. out:
  1216. mutex_unlock(&cfg80211_mutex);
  1217. }
  1218. static void reg_todo(struct work_struct *work)
  1219. {
  1220. reg_process_pending_hints();
  1221. reg_process_pending_beacon_hints();
  1222. }
  1223. static DECLARE_WORK(reg_work, reg_todo);
  1224. static void queue_regulatory_request(struct regulatory_request *request)
  1225. {
  1226. if (isalpha(request->alpha2[0]))
  1227. request->alpha2[0] = toupper(request->alpha2[0]);
  1228. if (isalpha(request->alpha2[1]))
  1229. request->alpha2[1] = toupper(request->alpha2[1]);
  1230. spin_lock(&reg_requests_lock);
  1231. list_add_tail(&request->list, &reg_requests_list);
  1232. spin_unlock(&reg_requests_lock);
  1233. schedule_work(&reg_work);
  1234. }
  1235. /*
  1236. * Core regulatory hint -- happens during cfg80211_init()
  1237. * and when we restore regulatory settings.
  1238. */
  1239. static int regulatory_hint_core(const char *alpha2)
  1240. {
  1241. struct regulatory_request *request;
  1242. kfree(last_request);
  1243. last_request = NULL;
  1244. request = kzalloc(sizeof(struct regulatory_request),
  1245. GFP_KERNEL);
  1246. if (!request)
  1247. return -ENOMEM;
  1248. request->alpha2[0] = alpha2[0];
  1249. request->alpha2[1] = alpha2[1];
  1250. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1251. /*
  1252. * This ensures last_request is populated once modules
  1253. * come swinging in and calling regulatory hints and
  1254. * wiphy_apply_custom_regulatory().
  1255. */
  1256. reg_process_hint(request);
  1257. return 0;
  1258. }
  1259. /* User hints */
  1260. int regulatory_hint_user(const char *alpha2)
  1261. {
  1262. struct regulatory_request *request;
  1263. BUG_ON(!alpha2);
  1264. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1265. if (!request)
  1266. return -ENOMEM;
  1267. request->wiphy_idx = WIPHY_IDX_STALE;
  1268. request->alpha2[0] = alpha2[0];
  1269. request->alpha2[1] = alpha2[1];
  1270. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1271. queue_regulatory_request(request);
  1272. return 0;
  1273. }
  1274. /* Driver hints */
  1275. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1276. {
  1277. struct regulatory_request *request;
  1278. BUG_ON(!alpha2);
  1279. BUG_ON(!wiphy);
  1280. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1281. if (!request)
  1282. return -ENOMEM;
  1283. request->wiphy_idx = get_wiphy_idx(wiphy);
  1284. /* Must have registered wiphy first */
  1285. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1286. request->alpha2[0] = alpha2[0];
  1287. request->alpha2[1] = alpha2[1];
  1288. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1289. queue_regulatory_request(request);
  1290. return 0;
  1291. }
  1292. EXPORT_SYMBOL(regulatory_hint);
  1293. /*
  1294. * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
  1295. * therefore cannot iterate over the rdev list here.
  1296. */
  1297. void regulatory_hint_11d(struct wiphy *wiphy,
  1298. enum ieee80211_band band,
  1299. u8 *country_ie,
  1300. u8 country_ie_len)
  1301. {
  1302. char alpha2[2];
  1303. enum environment_cap env = ENVIRON_ANY;
  1304. struct regulatory_request *request;
  1305. mutex_lock(&reg_mutex);
  1306. if (unlikely(!last_request))
  1307. goto out;
  1308. /* IE len must be evenly divisible by 2 */
  1309. if (country_ie_len & 0x01)
  1310. goto out;
  1311. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1312. goto out;
  1313. alpha2[0] = country_ie[0];
  1314. alpha2[1] = country_ie[1];
  1315. if (country_ie[2] == 'I')
  1316. env = ENVIRON_INDOOR;
  1317. else if (country_ie[2] == 'O')
  1318. env = ENVIRON_OUTDOOR;
  1319. /*
  1320. * We will run this only upon a successful connection on cfg80211.
  1321. * We leave conflict resolution to the workqueue, where can hold
  1322. * cfg80211_mutex.
  1323. */
  1324. if (likely(last_request->initiator ==
  1325. NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1326. wiphy_idx_valid(last_request->wiphy_idx)))
  1327. goto out;
  1328. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1329. if (!request)
  1330. goto out;
  1331. request->wiphy_idx = get_wiphy_idx(wiphy);
  1332. request->alpha2[0] = alpha2[0];
  1333. request->alpha2[1] = alpha2[1];
  1334. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  1335. request->country_ie_env = env;
  1336. mutex_unlock(&reg_mutex);
  1337. queue_regulatory_request(request);
  1338. return;
  1339. out:
  1340. mutex_unlock(&reg_mutex);
  1341. }
  1342. static void restore_alpha2(char *alpha2, bool reset_user)
  1343. {
  1344. /* indicates there is no alpha2 to consider for restoration */
  1345. alpha2[0] = '9';
  1346. alpha2[1] = '7';
  1347. /* The user setting has precedence over the module parameter */
  1348. if (is_user_regdom_saved()) {
  1349. /* Unless we're asked to ignore it and reset it */
  1350. if (reset_user) {
  1351. REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
  1352. "including user preference\n");
  1353. user_alpha2[0] = '9';
  1354. user_alpha2[1] = '7';
  1355. /*
  1356. * If we're ignoring user settings, we still need to
  1357. * check the module parameter to ensure we put things
  1358. * back as they were for a full restore.
  1359. */
  1360. if (!is_world_regdom(ieee80211_regdom)) {
  1361. REG_DBG_PRINT("cfg80211: Keeping preference on "
  1362. "module parameter ieee80211_regdom: %c%c\n",
  1363. ieee80211_regdom[0],
  1364. ieee80211_regdom[1]);
  1365. alpha2[0] = ieee80211_regdom[0];
  1366. alpha2[1] = ieee80211_regdom[1];
  1367. }
  1368. } else {
  1369. REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
  1370. "while preserving user preference for: %c%c\n",
  1371. user_alpha2[0],
  1372. user_alpha2[1]);
  1373. alpha2[0] = user_alpha2[0];
  1374. alpha2[1] = user_alpha2[1];
  1375. }
  1376. } else if (!is_world_regdom(ieee80211_regdom)) {
  1377. REG_DBG_PRINT("cfg80211: Keeping preference on "
  1378. "module parameter ieee80211_regdom: %c%c\n",
  1379. ieee80211_regdom[0],
  1380. ieee80211_regdom[1]);
  1381. alpha2[0] = ieee80211_regdom[0];
  1382. alpha2[1] = ieee80211_regdom[1];
  1383. } else
  1384. REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
  1385. }
  1386. /*
  1387. * Restoring regulatory settings involves ingoring any
  1388. * possibly stale country IE information and user regulatory
  1389. * settings if so desired, this includes any beacon hints
  1390. * learned as we could have traveled outside to another country
  1391. * after disconnection. To restore regulatory settings we do
  1392. * exactly what we did at bootup:
  1393. *
  1394. * - send a core regulatory hint
  1395. * - send a user regulatory hint if applicable
  1396. *
  1397. * Device drivers that send a regulatory hint for a specific country
  1398. * keep their own regulatory domain on wiphy->regd so that does does
  1399. * not need to be remembered.
  1400. */
  1401. static void restore_regulatory_settings(bool reset_user)
  1402. {
  1403. char alpha2[2];
  1404. struct reg_beacon *reg_beacon, *btmp;
  1405. mutex_lock(&cfg80211_mutex);
  1406. mutex_lock(&reg_mutex);
  1407. reset_regdomains();
  1408. restore_alpha2(alpha2, reset_user);
  1409. /* Clear beacon hints */
  1410. spin_lock_bh(&reg_pending_beacons_lock);
  1411. if (!list_empty(&reg_pending_beacons)) {
  1412. list_for_each_entry_safe(reg_beacon, btmp,
  1413. &reg_pending_beacons, list) {
  1414. list_del(&reg_beacon->list);
  1415. kfree(reg_beacon);
  1416. }
  1417. }
  1418. spin_unlock_bh(&reg_pending_beacons_lock);
  1419. if (!list_empty(&reg_beacon_list)) {
  1420. list_for_each_entry_safe(reg_beacon, btmp,
  1421. &reg_beacon_list, list) {
  1422. list_del(&reg_beacon->list);
  1423. kfree(reg_beacon);
  1424. }
  1425. }
  1426. /* First restore to the basic regulatory settings */
  1427. cfg80211_regdomain = cfg80211_world_regdom;
  1428. mutex_unlock(&reg_mutex);
  1429. mutex_unlock(&cfg80211_mutex);
  1430. regulatory_hint_core(cfg80211_regdomain->alpha2);
  1431. /*
  1432. * This restores the ieee80211_regdom module parameter
  1433. * preference or the last user requested regulatory
  1434. * settings, user regulatory settings takes precedence.
  1435. */
  1436. if (is_an_alpha2(alpha2))
  1437. regulatory_hint_user(user_alpha2);
  1438. }
  1439. void regulatory_hint_disconnect(void)
  1440. {
  1441. REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
  1442. "restore regulatory settings\n");
  1443. restore_regulatory_settings(false);
  1444. }
  1445. static bool freq_is_chan_12_13_14(u16 freq)
  1446. {
  1447. if (freq == ieee80211_channel_to_frequency(12) ||
  1448. freq == ieee80211_channel_to_frequency(13) ||
  1449. freq == ieee80211_channel_to_frequency(14))
  1450. return true;
  1451. return false;
  1452. }
  1453. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1454. struct ieee80211_channel *beacon_chan,
  1455. gfp_t gfp)
  1456. {
  1457. struct reg_beacon *reg_beacon;
  1458. if (likely((beacon_chan->beacon_found ||
  1459. (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
  1460. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1461. !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
  1462. return 0;
  1463. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1464. if (!reg_beacon)
  1465. return -ENOMEM;
  1466. REG_DBG_PRINT("cfg80211: Found new beacon on "
  1467. "frequency: %d MHz (Ch %d) on %s\n",
  1468. beacon_chan->center_freq,
  1469. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1470. wiphy_name(wiphy));
  1471. memcpy(&reg_beacon->chan, beacon_chan,
  1472. sizeof(struct ieee80211_channel));
  1473. /*
  1474. * Since we can be called from BH or and non-BH context
  1475. * we must use spin_lock_bh()
  1476. */
  1477. spin_lock_bh(&reg_pending_beacons_lock);
  1478. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1479. spin_unlock_bh(&reg_pending_beacons_lock);
  1480. schedule_work(&reg_work);
  1481. return 0;
  1482. }
  1483. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1484. {
  1485. unsigned int i;
  1486. const struct ieee80211_reg_rule *reg_rule = NULL;
  1487. const struct ieee80211_freq_range *freq_range = NULL;
  1488. const struct ieee80211_power_rule *power_rule = NULL;
  1489. printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
  1490. "(max_antenna_gain, max_eirp)\n");
  1491. for (i = 0; i < rd->n_reg_rules; i++) {
  1492. reg_rule = &rd->reg_rules[i];
  1493. freq_range = &reg_rule->freq_range;
  1494. power_rule = &reg_rule->power_rule;
  1495. /*
  1496. * There may not be documentation for max antenna gain
  1497. * in certain regions
  1498. */
  1499. if (power_rule->max_antenna_gain)
  1500. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1501. "(%d mBi, %d mBm)\n",
  1502. freq_range->start_freq_khz,
  1503. freq_range->end_freq_khz,
  1504. freq_range->max_bandwidth_khz,
  1505. power_rule->max_antenna_gain,
  1506. power_rule->max_eirp);
  1507. else
  1508. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1509. "(N/A, %d mBm)\n",
  1510. freq_range->start_freq_khz,
  1511. freq_range->end_freq_khz,
  1512. freq_range->max_bandwidth_khz,
  1513. power_rule->max_eirp);
  1514. }
  1515. }
  1516. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1517. {
  1518. if (is_intersected_alpha2(rd->alpha2)) {
  1519. if (last_request->initiator ==
  1520. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1521. struct cfg80211_registered_device *rdev;
  1522. rdev = cfg80211_rdev_by_wiphy_idx(
  1523. last_request->wiphy_idx);
  1524. if (rdev) {
  1525. printk(KERN_INFO "cfg80211: Current regulatory "
  1526. "domain updated by AP to: %c%c\n",
  1527. rdev->country_ie_alpha2[0],
  1528. rdev->country_ie_alpha2[1]);
  1529. } else
  1530. printk(KERN_INFO "cfg80211: Current regulatory "
  1531. "domain intersected:\n");
  1532. } else
  1533. printk(KERN_INFO "cfg80211: Current regulatory "
  1534. "domain intersected:\n");
  1535. } else if (is_world_regdom(rd->alpha2))
  1536. printk(KERN_INFO "cfg80211: World regulatory "
  1537. "domain updated:\n");
  1538. else {
  1539. if (is_unknown_alpha2(rd->alpha2))
  1540. printk(KERN_INFO "cfg80211: Regulatory domain "
  1541. "changed to driver built-in settings "
  1542. "(unknown country)\n");
  1543. else
  1544. printk(KERN_INFO "cfg80211: Regulatory domain "
  1545. "changed to country: %c%c\n",
  1546. rd->alpha2[0], rd->alpha2[1]);
  1547. }
  1548. print_rd_rules(rd);
  1549. }
  1550. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1551. {
  1552. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1553. rd->alpha2[0], rd->alpha2[1]);
  1554. print_rd_rules(rd);
  1555. }
  1556. /* Takes ownership of rd only if it doesn't fail */
  1557. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1558. {
  1559. const struct ieee80211_regdomain *intersected_rd = NULL;
  1560. struct cfg80211_registered_device *rdev = NULL;
  1561. struct wiphy *request_wiphy;
  1562. /* Some basic sanity checks first */
  1563. if (is_world_regdom(rd->alpha2)) {
  1564. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1565. return -EINVAL;
  1566. update_world_regdomain(rd);
  1567. return 0;
  1568. }
  1569. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1570. !is_unknown_alpha2(rd->alpha2))
  1571. return -EINVAL;
  1572. if (!last_request)
  1573. return -EINVAL;
  1574. /*
  1575. * Lets only bother proceeding on the same alpha2 if the current
  1576. * rd is non static (it means CRDA was present and was used last)
  1577. * and the pending request came in from a country IE
  1578. */
  1579. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1580. /*
  1581. * If someone else asked us to change the rd lets only bother
  1582. * checking if the alpha2 changes if CRDA was already called
  1583. */
  1584. if (!regdom_changes(rd->alpha2))
  1585. return -EINVAL;
  1586. }
  1587. /*
  1588. * Now lets set the regulatory domain, update all driver channels
  1589. * and finally inform them of what we have done, in case they want
  1590. * to review or adjust their own settings based on their own
  1591. * internal EEPROM data
  1592. */
  1593. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1594. return -EINVAL;
  1595. if (!is_valid_rd(rd)) {
  1596. printk(KERN_ERR "cfg80211: Invalid "
  1597. "regulatory domain detected:\n");
  1598. print_regdomain_info(rd);
  1599. return -EINVAL;
  1600. }
  1601. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1602. if (!last_request->intersect) {
  1603. int r;
  1604. if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
  1605. reset_regdomains();
  1606. cfg80211_regdomain = rd;
  1607. return 0;
  1608. }
  1609. /*
  1610. * For a driver hint, lets copy the regulatory domain the
  1611. * driver wanted to the wiphy to deal with conflicts
  1612. */
  1613. /*
  1614. * Userspace could have sent two replies with only
  1615. * one kernel request.
  1616. */
  1617. if (request_wiphy->regd)
  1618. return -EALREADY;
  1619. r = reg_copy_regd(&request_wiphy->regd, rd);
  1620. if (r)
  1621. return r;
  1622. reset_regdomains();
  1623. cfg80211_regdomain = rd;
  1624. return 0;
  1625. }
  1626. /* Intersection requires a bit more work */
  1627. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1628. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1629. if (!intersected_rd)
  1630. return -EINVAL;
  1631. /*
  1632. * We can trash what CRDA provided now.
  1633. * However if a driver requested this specific regulatory
  1634. * domain we keep it for its private use
  1635. */
  1636. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
  1637. request_wiphy->regd = rd;
  1638. else
  1639. kfree(rd);
  1640. rd = NULL;
  1641. reset_regdomains();
  1642. cfg80211_regdomain = intersected_rd;
  1643. return 0;
  1644. }
  1645. if (!intersected_rd)
  1646. return -EINVAL;
  1647. rdev = wiphy_to_dev(request_wiphy);
  1648. rdev->country_ie_alpha2[0] = rd->alpha2[0];
  1649. rdev->country_ie_alpha2[1] = rd->alpha2[1];
  1650. rdev->env = last_request->country_ie_env;
  1651. BUG_ON(intersected_rd == rd);
  1652. kfree(rd);
  1653. rd = NULL;
  1654. reset_regdomains();
  1655. cfg80211_regdomain = intersected_rd;
  1656. return 0;
  1657. }
  1658. /*
  1659. * Use this call to set the current regulatory domain. Conflicts with
  1660. * multiple drivers can be ironed out later. Caller must've already
  1661. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  1662. */
  1663. int set_regdom(const struct ieee80211_regdomain *rd)
  1664. {
  1665. int r;
  1666. assert_cfg80211_lock();
  1667. mutex_lock(&reg_mutex);
  1668. /* Note that this doesn't update the wiphys, this is done below */
  1669. r = __set_regdom(rd);
  1670. if (r) {
  1671. kfree(rd);
  1672. mutex_unlock(&reg_mutex);
  1673. return r;
  1674. }
  1675. /* This would make this whole thing pointless */
  1676. if (!last_request->intersect)
  1677. BUG_ON(rd != cfg80211_regdomain);
  1678. /* update all wiphys now with the new established regulatory domain */
  1679. update_all_wiphy_regulatory(last_request->initiator);
  1680. print_regdomain(cfg80211_regdomain);
  1681. nl80211_send_reg_change_event(last_request);
  1682. mutex_unlock(&reg_mutex);
  1683. return r;
  1684. }
  1685. /* Caller must hold cfg80211_mutex */
  1686. void reg_device_remove(struct wiphy *wiphy)
  1687. {
  1688. struct wiphy *request_wiphy = NULL;
  1689. assert_cfg80211_lock();
  1690. mutex_lock(&reg_mutex);
  1691. kfree(wiphy->regd);
  1692. if (last_request)
  1693. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1694. if (!request_wiphy || request_wiphy != wiphy)
  1695. goto out;
  1696. last_request->wiphy_idx = WIPHY_IDX_STALE;
  1697. last_request->country_ie_env = ENVIRON_ANY;
  1698. out:
  1699. mutex_unlock(&reg_mutex);
  1700. }
  1701. int __init regulatory_init(void)
  1702. {
  1703. int err = 0;
  1704. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1705. if (IS_ERR(reg_pdev))
  1706. return PTR_ERR(reg_pdev);
  1707. spin_lock_init(&reg_requests_lock);
  1708. spin_lock_init(&reg_pending_beacons_lock);
  1709. cfg80211_regdomain = cfg80211_world_regdom;
  1710. user_alpha2[0] = '9';
  1711. user_alpha2[1] = '7';
  1712. /* We always try to get an update for the static regdomain */
  1713. err = regulatory_hint_core(cfg80211_regdomain->alpha2);
  1714. if (err) {
  1715. if (err == -ENOMEM)
  1716. return err;
  1717. /*
  1718. * N.B. kobject_uevent_env() can fail mainly for when we're out
  1719. * memory which is handled and propagated appropriately above
  1720. * but it can also fail during a netlink_broadcast() or during
  1721. * early boot for call_usermodehelper(). For now treat these
  1722. * errors as non-fatal.
  1723. */
  1724. printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
  1725. "to call CRDA during init");
  1726. #ifdef CONFIG_CFG80211_REG_DEBUG
  1727. /* We want to find out exactly why when debugging */
  1728. WARN_ON(err);
  1729. #endif
  1730. }
  1731. /*
  1732. * Finally, if the user set the module parameter treat it
  1733. * as a user hint.
  1734. */
  1735. if (!is_world_regdom(ieee80211_regdom))
  1736. regulatory_hint_user(ieee80211_regdom);
  1737. return 0;
  1738. }
  1739. void /* __init_or_exit */ regulatory_exit(void)
  1740. {
  1741. struct regulatory_request *reg_request, *tmp;
  1742. struct reg_beacon *reg_beacon, *btmp;
  1743. cancel_work_sync(&reg_work);
  1744. mutex_lock(&cfg80211_mutex);
  1745. mutex_lock(&reg_mutex);
  1746. reset_regdomains();
  1747. kfree(last_request);
  1748. platform_device_unregister(reg_pdev);
  1749. spin_lock_bh(&reg_pending_beacons_lock);
  1750. if (!list_empty(&reg_pending_beacons)) {
  1751. list_for_each_entry_safe(reg_beacon, btmp,
  1752. &reg_pending_beacons, list) {
  1753. list_del(&reg_beacon->list);
  1754. kfree(reg_beacon);
  1755. }
  1756. }
  1757. spin_unlock_bh(&reg_pending_beacons_lock);
  1758. if (!list_empty(&reg_beacon_list)) {
  1759. list_for_each_entry_safe(reg_beacon, btmp,
  1760. &reg_beacon_list, list) {
  1761. list_del(&reg_beacon->list);
  1762. kfree(reg_beacon);
  1763. }
  1764. }
  1765. spin_lock(&reg_requests_lock);
  1766. if (!list_empty(&reg_requests_list)) {
  1767. list_for_each_entry_safe(reg_request, tmp,
  1768. &reg_requests_list, list) {
  1769. list_del(&reg_request->list);
  1770. kfree(reg_request);
  1771. }
  1772. }
  1773. spin_unlock(&reg_requests_lock);
  1774. mutex_unlock(&reg_mutex);
  1775. mutex_unlock(&cfg80211_mutex);
  1776. }