reg.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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/list.h>
  36. #include <linux/random.h>
  37. #include <linux/nl80211.h>
  38. #include <linux/platform_device.h>
  39. #include <net/wireless.h>
  40. #include <net/cfg80211.h>
  41. #include "core.h"
  42. #include "reg.h"
  43. /* Receipt of information from last regulatory request */
  44. static struct regulatory_request *last_request;
  45. /* To trigger userspace events */
  46. static struct platform_device *reg_pdev;
  47. /* Keep the ordering from large to small */
  48. static u32 supported_bandwidths[] = {
  49. MHZ_TO_KHZ(40),
  50. MHZ_TO_KHZ(20),
  51. };
  52. /* Central wireless core regulatory domains, we only need two,
  53. * the current one and a world regulatory domain in case we have no
  54. * information to give us an alpha2 */
  55. const struct ieee80211_regdomain *cfg80211_regdomain;
  56. /* We use this as a place for the rd structure built from the
  57. * last parsed country IE to rest until CRDA gets back to us with
  58. * what it thinks should apply for the same country */
  59. static const struct ieee80211_regdomain *country_ie_regdomain;
  60. /* We keep a static world regulatory domain in case of the absence of CRDA */
  61. static const struct ieee80211_regdomain world_regdom = {
  62. .n_reg_rules = 1,
  63. .alpha2 = "00",
  64. .reg_rules = {
  65. REG_RULE(2412-10, 2462+10, 40, 6, 20,
  66. NL80211_RRF_PASSIVE_SCAN |
  67. NL80211_RRF_NO_IBSS),
  68. }
  69. };
  70. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  71. &world_regdom;
  72. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  73. static char *ieee80211_regdom = "US";
  74. module_param(ieee80211_regdom, charp, 0444);
  75. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  76. /* We assume 40 MHz bandwidth for the old regulatory work.
  77. * We make emphasis we are using the exact same frequencies
  78. * as before */
  79. static const struct ieee80211_regdomain us_regdom = {
  80. .n_reg_rules = 6,
  81. .alpha2 = "US",
  82. .reg_rules = {
  83. /* IEEE 802.11b/g, channels 1..11 */
  84. REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
  85. /* IEEE 802.11a, channel 36 */
  86. REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
  87. /* IEEE 802.11a, channel 40 */
  88. REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
  89. /* IEEE 802.11a, channel 44 */
  90. REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
  91. /* IEEE 802.11a, channels 48..64 */
  92. REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
  93. /* IEEE 802.11a, channels 149..165, outdoor */
  94. REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
  95. }
  96. };
  97. static const struct ieee80211_regdomain jp_regdom = {
  98. .n_reg_rules = 3,
  99. .alpha2 = "JP",
  100. .reg_rules = {
  101. /* IEEE 802.11b/g, channels 1..14 */
  102. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  103. /* IEEE 802.11a, channels 34..48 */
  104. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  105. NL80211_RRF_PASSIVE_SCAN),
  106. /* IEEE 802.11a, channels 52..64 */
  107. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  108. NL80211_RRF_NO_IBSS |
  109. NL80211_RRF_DFS),
  110. }
  111. };
  112. static const struct ieee80211_regdomain eu_regdom = {
  113. .n_reg_rules = 6,
  114. /* This alpha2 is bogus, we leave it here just for stupid
  115. * backward compatibility */
  116. .alpha2 = "EU",
  117. .reg_rules = {
  118. /* IEEE 802.11b/g, channels 1..13 */
  119. REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
  120. /* IEEE 802.11a, channel 36 */
  121. REG_RULE(5180-10, 5180+10, 40, 6, 23,
  122. NL80211_RRF_PASSIVE_SCAN),
  123. /* IEEE 802.11a, channel 40 */
  124. REG_RULE(5200-10, 5200+10, 40, 6, 23,
  125. NL80211_RRF_PASSIVE_SCAN),
  126. /* IEEE 802.11a, channel 44 */
  127. REG_RULE(5220-10, 5220+10, 40, 6, 23,
  128. NL80211_RRF_PASSIVE_SCAN),
  129. /* IEEE 802.11a, channels 48..64 */
  130. REG_RULE(5240-10, 5320+10, 40, 6, 20,
  131. NL80211_RRF_NO_IBSS |
  132. NL80211_RRF_DFS),
  133. /* IEEE 802.11a, channels 100..140 */
  134. REG_RULE(5500-10, 5700+10, 40, 6, 30,
  135. NL80211_RRF_NO_IBSS |
  136. NL80211_RRF_DFS),
  137. }
  138. };
  139. static const struct ieee80211_regdomain *static_regdom(char *alpha2)
  140. {
  141. if (alpha2[0] == 'U' && alpha2[1] == 'S')
  142. return &us_regdom;
  143. if (alpha2[0] == 'J' && alpha2[1] == 'P')
  144. return &jp_regdom;
  145. if (alpha2[0] == 'E' && alpha2[1] == 'U')
  146. return &eu_regdom;
  147. /* Default, as per the old rules */
  148. return &us_regdom;
  149. }
  150. static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  151. {
  152. if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
  153. return true;
  154. return false;
  155. }
  156. #else
  157. static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
  158. {
  159. return false;
  160. }
  161. #endif
  162. static void reset_regdomains(void)
  163. {
  164. /* avoid freeing static information or freeing something twice */
  165. if (cfg80211_regdomain == cfg80211_world_regdom)
  166. cfg80211_regdomain = NULL;
  167. if (cfg80211_world_regdom == &world_regdom)
  168. cfg80211_world_regdom = NULL;
  169. if (cfg80211_regdomain == &world_regdom)
  170. cfg80211_regdomain = NULL;
  171. if (is_old_static_regdom(cfg80211_regdomain))
  172. cfg80211_regdomain = NULL;
  173. kfree(cfg80211_regdomain);
  174. kfree(cfg80211_world_regdom);
  175. cfg80211_world_regdom = &world_regdom;
  176. cfg80211_regdomain = NULL;
  177. }
  178. /* Dynamic world regulatory domain requested by the wireless
  179. * core upon initialization */
  180. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  181. {
  182. BUG_ON(!last_request);
  183. reset_regdomains();
  184. cfg80211_world_regdom = rd;
  185. cfg80211_regdomain = rd;
  186. }
  187. bool is_world_regdom(const char *alpha2)
  188. {
  189. if (!alpha2)
  190. return false;
  191. if (alpha2[0] == '0' && alpha2[1] == '0')
  192. return true;
  193. return false;
  194. }
  195. static bool is_alpha2_set(const char *alpha2)
  196. {
  197. if (!alpha2)
  198. return false;
  199. if (alpha2[0] != 0 && alpha2[1] != 0)
  200. return true;
  201. return false;
  202. }
  203. static bool is_alpha_upper(char letter)
  204. {
  205. /* ASCII A - Z */
  206. if (letter >= 65 && letter <= 90)
  207. return true;
  208. return false;
  209. }
  210. static bool is_unknown_alpha2(const char *alpha2)
  211. {
  212. if (!alpha2)
  213. return false;
  214. /* Special case where regulatory domain was built by driver
  215. * but a specific alpha2 cannot be determined */
  216. if (alpha2[0] == '9' && alpha2[1] == '9')
  217. return true;
  218. return false;
  219. }
  220. static bool is_intersected_alpha2(const char *alpha2)
  221. {
  222. if (!alpha2)
  223. return false;
  224. /* Special case where regulatory domain is the
  225. * result of an intersection between two regulatory domain
  226. * structures */
  227. if (alpha2[0] == '9' && alpha2[1] == '8')
  228. return true;
  229. return false;
  230. }
  231. static bool is_an_alpha2(const char *alpha2)
  232. {
  233. if (!alpha2)
  234. return false;
  235. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  236. return true;
  237. return false;
  238. }
  239. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  240. {
  241. if (!alpha2_x || !alpha2_y)
  242. return false;
  243. if (alpha2_x[0] == alpha2_y[0] &&
  244. alpha2_x[1] == alpha2_y[1])
  245. return true;
  246. return false;
  247. }
  248. static bool regdom_changed(const char *alpha2)
  249. {
  250. if (!cfg80211_regdomain)
  251. return true;
  252. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  253. return false;
  254. return true;
  255. }
  256. /**
  257. * country_ie_integrity_changes - tells us if the country IE has changed
  258. * @checksum: checksum of country IE of fields we are interested in
  259. *
  260. * If the country IE has not changed you can ignore it safely. This is
  261. * useful to determine if two devices are seeing two different country IEs
  262. * even on the same alpha2. Note that this will return false if no IE has
  263. * been set on the wireless core yet.
  264. */
  265. static bool country_ie_integrity_changes(u32 checksum)
  266. {
  267. /* If no IE has been set then the checksum doesn't change */
  268. if (unlikely(!last_request->country_ie_checksum))
  269. return false;
  270. if (unlikely(last_request->country_ie_checksum != checksum))
  271. return true;
  272. return false;
  273. }
  274. /* This lets us keep regulatory code which is updated on a regulatory
  275. * basis in userspace. */
  276. static int call_crda(const char *alpha2)
  277. {
  278. char country_env[9 + 2] = "COUNTRY=";
  279. char *envp[] = {
  280. country_env,
  281. NULL
  282. };
  283. if (!is_world_regdom((char *) alpha2))
  284. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  285. alpha2[0], alpha2[1]);
  286. else
  287. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  288. "regulatory domain\n");
  289. country_env[8] = alpha2[0];
  290. country_env[9] = alpha2[1];
  291. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  292. }
  293. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  294. bool reg_is_valid_request(const char *alpha2)
  295. {
  296. if (!last_request)
  297. return false;
  298. return alpha2_equal(last_request->alpha2, alpha2);
  299. }
  300. /* Sanity check on a regulatory rule */
  301. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  302. {
  303. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  304. u32 freq_diff;
  305. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  306. return false;
  307. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  308. return false;
  309. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  310. if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
  311. return false;
  312. return true;
  313. }
  314. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  315. {
  316. const struct ieee80211_reg_rule *reg_rule = NULL;
  317. unsigned int i;
  318. if (!rd->n_reg_rules)
  319. return false;
  320. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  321. return false;
  322. for (i = 0; i < rd->n_reg_rules; i++) {
  323. reg_rule = &rd->reg_rules[i];
  324. if (!is_valid_reg_rule(reg_rule))
  325. return false;
  326. }
  327. return true;
  328. }
  329. /* Returns value in KHz */
  330. static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
  331. u32 freq)
  332. {
  333. unsigned int i;
  334. for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
  335. u32 start_freq_khz = freq - supported_bandwidths[i]/2;
  336. u32 end_freq_khz = freq + supported_bandwidths[i]/2;
  337. if (start_freq_khz >= freq_range->start_freq_khz &&
  338. end_freq_khz <= freq_range->end_freq_khz)
  339. return supported_bandwidths[i];
  340. }
  341. return 0;
  342. }
  343. /**
  344. * freq_in_rule_band - tells us if a frequency is in a frequency band
  345. * @freq_range: frequency rule we want to query
  346. * @freq_khz: frequency we are inquiring about
  347. *
  348. * This lets us know if a specific frequency rule is or is not relevant to
  349. * a specific frequency's band. Bands are device specific and artificial
  350. * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
  351. * safe for now to assume that a frequency rule should not be part of a
  352. * frequency's band if the start freq or end freq are off by more than 2 GHz.
  353. * This resolution can be lowered and should be considered as we add
  354. * regulatory rule support for other "bands".
  355. **/
  356. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  357. u32 freq_khz)
  358. {
  359. #define ONE_GHZ_IN_KHZ 1000000
  360. if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  361. return true;
  362. if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  363. return true;
  364. return false;
  365. #undef ONE_GHZ_IN_KHZ
  366. }
  367. /* Converts a country IE to a regulatory domain. A regulatory domain
  368. * structure has a lot of information which the IE doesn't yet have,
  369. * so for the other values we use upper max values as we will intersect
  370. * with our userspace regulatory agent to get lower bounds. */
  371. static struct ieee80211_regdomain *country_ie_2_rd(
  372. u8 *country_ie,
  373. u8 country_ie_len,
  374. u32 *checksum)
  375. {
  376. struct ieee80211_regdomain *rd = NULL;
  377. unsigned int i = 0;
  378. char alpha2[2];
  379. u32 flags = 0;
  380. u32 num_rules = 0, size_of_regd = 0;
  381. u8 *triplets_start = NULL;
  382. u8 len_at_triplet = 0;
  383. /* the last channel we have registered in a subband (triplet) */
  384. int last_sub_max_channel = 0;
  385. *checksum = 0xDEADBEEF;
  386. /* Country IE requirements */
  387. BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
  388. country_ie_len & 0x01);
  389. alpha2[0] = country_ie[0];
  390. alpha2[1] = country_ie[1];
  391. /*
  392. * Third octet can be:
  393. * 'I' - Indoor
  394. * 'O' - Outdoor
  395. *
  396. * anything else we assume is no restrictions
  397. */
  398. if (country_ie[2] == 'I')
  399. flags = NL80211_RRF_NO_OUTDOOR;
  400. else if (country_ie[2] == 'O')
  401. flags = NL80211_RRF_NO_INDOOR;
  402. country_ie += 3;
  403. country_ie_len -= 3;
  404. triplets_start = country_ie;
  405. len_at_triplet = country_ie_len;
  406. *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
  407. /* We need to build a reg rule for each triplet, but first we must
  408. * calculate the number of reg rules we will need. We will need one
  409. * for each channel subband */
  410. while (country_ie_len >= 3) {
  411. int end_channel = 0;
  412. struct ieee80211_country_ie_triplet *triplet =
  413. (struct ieee80211_country_ie_triplet *) country_ie;
  414. int cur_sub_max_channel = 0, cur_channel = 0;
  415. if (triplet->ext.reg_extension_id >=
  416. IEEE80211_COUNTRY_EXTENSION_ID) {
  417. country_ie += 3;
  418. country_ie_len -= 3;
  419. continue;
  420. }
  421. /* 2 GHz */
  422. if (triplet->chans.first_channel <= 14)
  423. end_channel = triplet->chans.first_channel +
  424. triplet->chans.num_channels;
  425. else
  426. /*
  427. * 5 GHz -- For example in country IEs if the first
  428. * channel given is 36 and the number of channels is 4
  429. * then the individual channel numbers defined for the
  430. * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
  431. * and not 36, 37, 38, 39.
  432. *
  433. * See: http://tinyurl.com/11d-clarification
  434. */
  435. end_channel = triplet->chans.first_channel +
  436. (4 * (triplet->chans.num_channels - 1));
  437. cur_channel = triplet->chans.first_channel;
  438. cur_sub_max_channel = end_channel;
  439. /* Basic sanity check */
  440. if (cur_sub_max_channel < cur_channel)
  441. return NULL;
  442. /* Do not allow overlapping channels. Also channels
  443. * passed in each subband must be monotonically
  444. * increasing */
  445. if (last_sub_max_channel) {
  446. if (cur_channel <= last_sub_max_channel)
  447. return NULL;
  448. if (cur_sub_max_channel <= last_sub_max_channel)
  449. return NULL;
  450. }
  451. /* When dot11RegulatoryClassesRequired is supported
  452. * we can throw ext triplets as part of this soup,
  453. * for now we don't care when those change as we
  454. * don't support them */
  455. *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
  456. ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
  457. ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
  458. last_sub_max_channel = cur_sub_max_channel;
  459. country_ie += 3;
  460. country_ie_len -= 3;
  461. num_rules++;
  462. /* Note: this is not a IEEE requirement but
  463. * simply a memory requirement */
  464. if (num_rules > NL80211_MAX_SUPP_REG_RULES)
  465. return NULL;
  466. }
  467. country_ie = triplets_start;
  468. country_ie_len = len_at_triplet;
  469. size_of_regd = sizeof(struct ieee80211_regdomain) +
  470. (num_rules * sizeof(struct ieee80211_reg_rule));
  471. rd = kzalloc(size_of_regd, GFP_KERNEL);
  472. if (!rd)
  473. return NULL;
  474. rd->n_reg_rules = num_rules;
  475. rd->alpha2[0] = alpha2[0];
  476. rd->alpha2[1] = alpha2[1];
  477. /* This time around we fill in the rd */
  478. while (country_ie_len >= 3) {
  479. int end_channel = 0;
  480. struct ieee80211_country_ie_triplet *triplet =
  481. (struct ieee80211_country_ie_triplet *) country_ie;
  482. struct ieee80211_reg_rule *reg_rule = NULL;
  483. struct ieee80211_freq_range *freq_range = NULL;
  484. struct ieee80211_power_rule *power_rule = NULL;
  485. /* Must parse if dot11RegulatoryClassesRequired is true,
  486. * we don't support this yet */
  487. if (triplet->ext.reg_extension_id >=
  488. IEEE80211_COUNTRY_EXTENSION_ID) {
  489. country_ie += 3;
  490. country_ie_len -= 3;
  491. continue;
  492. }
  493. reg_rule = &rd->reg_rules[i];
  494. freq_range = &reg_rule->freq_range;
  495. power_rule = &reg_rule->power_rule;
  496. reg_rule->flags = flags;
  497. /* 2 GHz */
  498. if (triplet->chans.first_channel <= 14)
  499. end_channel = triplet->chans.first_channel +
  500. triplet->chans.num_channels;
  501. else
  502. end_channel = triplet->chans.first_channel +
  503. (4 * (triplet->chans.num_channels - 1));
  504. /* The +10 is since the regulatory domain expects
  505. * the actual band edge, not the center of freq for
  506. * its start and end freqs, assuming 20 MHz bandwidth on
  507. * the channels passed */
  508. freq_range->start_freq_khz =
  509. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  510. triplet->chans.first_channel) - 10);
  511. freq_range->end_freq_khz =
  512. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  513. end_channel) + 10);
  514. /* Large arbitrary values, we intersect later */
  515. /* Increment this if we ever support >= 40 MHz channels
  516. * in IEEE 802.11 */
  517. freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
  518. power_rule->max_antenna_gain = DBI_TO_MBI(100);
  519. power_rule->max_eirp = DBM_TO_MBM(100);
  520. country_ie += 3;
  521. country_ie_len -= 3;
  522. i++;
  523. BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
  524. }
  525. return rd;
  526. }
  527. /* Helper for regdom_intersect(), this does the real
  528. * mathematical intersection fun */
  529. static int reg_rules_intersect(
  530. const struct ieee80211_reg_rule *rule1,
  531. const struct ieee80211_reg_rule *rule2,
  532. struct ieee80211_reg_rule *intersected_rule)
  533. {
  534. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  535. struct ieee80211_freq_range *freq_range;
  536. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  537. struct ieee80211_power_rule *power_rule;
  538. u32 freq_diff;
  539. freq_range1 = &rule1->freq_range;
  540. freq_range2 = &rule2->freq_range;
  541. freq_range = &intersected_rule->freq_range;
  542. power_rule1 = &rule1->power_rule;
  543. power_rule2 = &rule2->power_rule;
  544. power_rule = &intersected_rule->power_rule;
  545. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  546. freq_range2->start_freq_khz);
  547. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  548. freq_range2->end_freq_khz);
  549. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  550. freq_range2->max_bandwidth_khz);
  551. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  552. if (freq_range->max_bandwidth_khz > freq_diff)
  553. freq_range->max_bandwidth_khz = freq_diff;
  554. power_rule->max_eirp = min(power_rule1->max_eirp,
  555. power_rule2->max_eirp);
  556. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  557. power_rule2->max_antenna_gain);
  558. intersected_rule->flags = (rule1->flags | rule2->flags);
  559. if (!is_valid_reg_rule(intersected_rule))
  560. return -EINVAL;
  561. return 0;
  562. }
  563. /**
  564. * regdom_intersect - do the intersection between two regulatory domains
  565. * @rd1: first regulatory domain
  566. * @rd2: second regulatory domain
  567. *
  568. * Use this function to get the intersection between two regulatory domains.
  569. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  570. * as no one single alpha2 can represent this regulatory domain.
  571. *
  572. * Returns a pointer to the regulatory domain structure which will hold the
  573. * resulting intersection of rules between rd1 and rd2. We will
  574. * kzalloc() this structure for you.
  575. */
  576. static struct ieee80211_regdomain *regdom_intersect(
  577. const struct ieee80211_regdomain *rd1,
  578. const struct ieee80211_regdomain *rd2)
  579. {
  580. int r, size_of_regd;
  581. unsigned int x, y;
  582. unsigned int num_rules = 0, rule_idx = 0;
  583. const struct ieee80211_reg_rule *rule1, *rule2;
  584. struct ieee80211_reg_rule *intersected_rule;
  585. struct ieee80211_regdomain *rd;
  586. /* This is just a dummy holder to help us count */
  587. struct ieee80211_reg_rule irule;
  588. /* Uses the stack temporarily for counter arithmetic */
  589. intersected_rule = &irule;
  590. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  591. if (!rd1 || !rd2)
  592. return NULL;
  593. /* First we get a count of the rules we'll need, then we actually
  594. * build them. This is to so we can malloc() and free() a
  595. * regdomain once. The reason we use reg_rules_intersect() here
  596. * is it will return -EINVAL if the rule computed makes no sense.
  597. * All rules that do check out OK are valid. */
  598. for (x = 0; x < rd1->n_reg_rules; x++) {
  599. rule1 = &rd1->reg_rules[x];
  600. for (y = 0; y < rd2->n_reg_rules; y++) {
  601. rule2 = &rd2->reg_rules[y];
  602. if (!reg_rules_intersect(rule1, rule2,
  603. intersected_rule))
  604. num_rules++;
  605. memset(intersected_rule, 0,
  606. sizeof(struct ieee80211_reg_rule));
  607. }
  608. }
  609. if (!num_rules)
  610. return NULL;
  611. size_of_regd = sizeof(struct ieee80211_regdomain) +
  612. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  613. rd = kzalloc(size_of_regd, GFP_KERNEL);
  614. if (!rd)
  615. return NULL;
  616. for (x = 0; x < rd1->n_reg_rules; x++) {
  617. rule1 = &rd1->reg_rules[x];
  618. for (y = 0; y < rd2->n_reg_rules; y++) {
  619. rule2 = &rd2->reg_rules[y];
  620. /* This time around instead of using the stack lets
  621. * write to the target rule directly saving ourselves
  622. * a memcpy() */
  623. intersected_rule = &rd->reg_rules[rule_idx];
  624. r = reg_rules_intersect(rule1, rule2,
  625. intersected_rule);
  626. /* No need to memset here the intersected rule here as
  627. * we're not using the stack anymore */
  628. if (r)
  629. continue;
  630. rule_idx++;
  631. }
  632. }
  633. if (rule_idx != num_rules) {
  634. kfree(rd);
  635. return NULL;
  636. }
  637. rd->n_reg_rules = num_rules;
  638. rd->alpha2[0] = '9';
  639. rd->alpha2[1] = '8';
  640. return rd;
  641. }
  642. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  643. * want to just have the channel structure use these */
  644. static u32 map_regdom_flags(u32 rd_flags)
  645. {
  646. u32 channel_flags = 0;
  647. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  648. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  649. if (rd_flags & NL80211_RRF_NO_IBSS)
  650. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  651. if (rd_flags & NL80211_RRF_DFS)
  652. channel_flags |= IEEE80211_CHAN_RADAR;
  653. return channel_flags;
  654. }
  655. static int freq_reg_info_regd(struct wiphy *wiphy,
  656. u32 center_freq,
  657. u32 *bandwidth,
  658. const struct ieee80211_reg_rule **reg_rule,
  659. const struct ieee80211_regdomain *custom_regd)
  660. {
  661. int i;
  662. bool band_rule_found = false;
  663. const struct ieee80211_regdomain *regd;
  664. u32 max_bandwidth = 0;
  665. regd = custom_regd ? custom_regd : cfg80211_regdomain;
  666. /* Follow the driver's regulatory domain, if present, unless a country
  667. * IE has been processed or a user wants to help complaince further */
  668. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
  669. last_request->initiator != REGDOM_SET_BY_USER &&
  670. wiphy->regd)
  671. regd = wiphy->regd;
  672. if (!regd)
  673. return -EINVAL;
  674. for (i = 0; i < regd->n_reg_rules; i++) {
  675. const struct ieee80211_reg_rule *rr;
  676. const struct ieee80211_freq_range *fr = NULL;
  677. const struct ieee80211_power_rule *pr = NULL;
  678. rr = &regd->reg_rules[i];
  679. fr = &rr->freq_range;
  680. pr = &rr->power_rule;
  681. /* We only need to know if one frequency rule was
  682. * was in center_freq's band, that's enough, so lets
  683. * not overwrite it once found */
  684. if (!band_rule_found)
  685. band_rule_found = freq_in_rule_band(fr, center_freq);
  686. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  687. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  688. *reg_rule = rr;
  689. *bandwidth = max_bandwidth;
  690. break;
  691. }
  692. }
  693. if (!band_rule_found)
  694. return -ERANGE;
  695. return !max_bandwidth;
  696. }
  697. EXPORT_SYMBOL(freq_reg_info);
  698. int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
  699. const struct ieee80211_reg_rule **reg_rule)
  700. {
  701. return freq_reg_info_regd(wiphy, center_freq,
  702. bandwidth, reg_rule, NULL);
  703. }
  704. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  705. unsigned int chan_idx)
  706. {
  707. int r;
  708. u32 flags;
  709. u32 max_bandwidth = 0;
  710. const struct ieee80211_reg_rule *reg_rule = NULL;
  711. const struct ieee80211_power_rule *power_rule = NULL;
  712. struct ieee80211_supported_band *sband;
  713. struct ieee80211_channel *chan;
  714. sband = wiphy->bands[band];
  715. BUG_ON(chan_idx >= sband->n_channels);
  716. chan = &sband->channels[chan_idx];
  717. flags = chan->orig_flags;
  718. r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
  719. &max_bandwidth, &reg_rule);
  720. if (r) {
  721. /* This means no regulatory rule was found in the country IE
  722. * with a frequency range on the center_freq's band, since
  723. * IEEE-802.11 allows for a country IE to have a subset of the
  724. * regulatory information provided in a country we ignore
  725. * disabling the channel unless at least one reg rule was
  726. * found on the center_freq's band. For details see this
  727. * clarification:
  728. *
  729. * http://tinyurl.com/11d-clarification
  730. */
  731. if (r == -ERANGE &&
  732. last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  733. #ifdef CONFIG_CFG80211_REG_DEBUG
  734. printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
  735. "intact on %s - no rule found in band on "
  736. "Country IE\n",
  737. chan->center_freq, wiphy_name(wiphy));
  738. #endif
  739. } else {
  740. /* In this case we know the country IE has at least one reg rule
  741. * for the band so we respect its band definitions */
  742. #ifdef CONFIG_CFG80211_REG_DEBUG
  743. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  744. printk(KERN_DEBUG "cfg80211: Disabling "
  745. "channel %d MHz on %s due to "
  746. "Country IE\n",
  747. chan->center_freq, wiphy_name(wiphy));
  748. #endif
  749. flags |= IEEE80211_CHAN_DISABLED;
  750. chan->flags = flags;
  751. }
  752. return;
  753. }
  754. power_rule = &reg_rule->power_rule;
  755. if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
  756. last_request->wiphy && last_request->wiphy == wiphy &&
  757. last_request->wiphy->strict_regulatory) {
  758. /* This gaurantees the driver's requested regulatory domain
  759. * will always be used as a base for further regulatory
  760. * settings */
  761. chan->flags = chan->orig_flags =
  762. map_regdom_flags(reg_rule->flags);
  763. chan->max_antenna_gain = chan->orig_mag =
  764. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  765. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  766. chan->max_power = chan->orig_mpwr =
  767. (int) MBM_TO_DBM(power_rule->max_eirp);
  768. return;
  769. }
  770. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  771. chan->max_antenna_gain = min(chan->orig_mag,
  772. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  773. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  774. if (chan->orig_mpwr)
  775. chan->max_power = min(chan->orig_mpwr,
  776. (int) MBM_TO_DBM(power_rule->max_eirp));
  777. else
  778. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  779. }
  780. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  781. {
  782. unsigned int i;
  783. struct ieee80211_supported_band *sband;
  784. BUG_ON(!wiphy->bands[band]);
  785. sband = wiphy->bands[band];
  786. for (i = 0; i < sband->n_channels; i++)
  787. handle_channel(wiphy, band, i);
  788. }
  789. static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
  790. {
  791. if (!last_request)
  792. return true;
  793. if (setby == REGDOM_SET_BY_CORE &&
  794. wiphy->custom_regulatory)
  795. return true;
  796. /* wiphy->regd will be set once the device has its own
  797. * desired regulatory domain set */
  798. if (wiphy->strict_regulatory && !wiphy->regd &&
  799. !is_world_regdom(last_request->alpha2))
  800. return true;
  801. return false;
  802. }
  803. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  804. {
  805. struct cfg80211_registered_device *drv;
  806. list_for_each_entry(drv, &cfg80211_drv_list, list)
  807. wiphy_update_regulatory(&drv->wiphy, setby);
  808. }
  809. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  810. {
  811. enum ieee80211_band band;
  812. if (ignore_reg_update(wiphy, setby))
  813. return;
  814. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  815. if (wiphy->bands[band])
  816. handle_band(wiphy, band);
  817. }
  818. if (wiphy->reg_notifier)
  819. wiphy->reg_notifier(wiphy, last_request);
  820. }
  821. static void handle_channel_custom(struct wiphy *wiphy,
  822. enum ieee80211_band band,
  823. unsigned int chan_idx,
  824. const struct ieee80211_regdomain *regd)
  825. {
  826. int r;
  827. u32 max_bandwidth = 0;
  828. const struct ieee80211_reg_rule *reg_rule = NULL;
  829. const struct ieee80211_power_rule *power_rule = NULL;
  830. struct ieee80211_supported_band *sband;
  831. struct ieee80211_channel *chan;
  832. sband = wiphy->bands[band];
  833. BUG_ON(chan_idx >= sband->n_channels);
  834. chan = &sband->channels[chan_idx];
  835. r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  836. &max_bandwidth, &reg_rule, regd);
  837. if (r) {
  838. chan->flags = IEEE80211_CHAN_DISABLED;
  839. return;
  840. }
  841. power_rule = &reg_rule->power_rule;
  842. chan->flags |= map_regdom_flags(reg_rule->flags);
  843. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  844. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  845. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  846. }
  847. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  848. const struct ieee80211_regdomain *regd)
  849. {
  850. unsigned int i;
  851. struct ieee80211_supported_band *sband;
  852. BUG_ON(!wiphy->bands[band]);
  853. sband = wiphy->bands[band];
  854. for (i = 0; i < sband->n_channels; i++)
  855. handle_channel_custom(wiphy, band, i, regd);
  856. }
  857. /* Used by drivers prior to wiphy registration */
  858. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  859. const struct ieee80211_regdomain *regd)
  860. {
  861. enum ieee80211_band band;
  862. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  863. if (wiphy->bands[band])
  864. handle_band_custom(wiphy, band, regd);
  865. }
  866. }
  867. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  868. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  869. const struct ieee80211_regdomain *src_regd)
  870. {
  871. struct ieee80211_regdomain *regd;
  872. int size_of_regd = 0;
  873. unsigned int i;
  874. size_of_regd = sizeof(struct ieee80211_regdomain) +
  875. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  876. regd = kzalloc(size_of_regd, GFP_KERNEL);
  877. if (!regd)
  878. return -ENOMEM;
  879. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  880. for (i = 0; i < src_regd->n_reg_rules; i++)
  881. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  882. sizeof(struct ieee80211_reg_rule));
  883. *dst_regd = regd;
  884. return 0;
  885. }
  886. /* Return value which can be used by ignore_request() to indicate
  887. * it has been determined we should intersect two regulatory domains */
  888. #define REG_INTERSECT 1
  889. /* This has the logic which determines when a new request
  890. * should be ignored. */
  891. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  892. const char *alpha2)
  893. {
  894. /* All initial requests are respected */
  895. if (!last_request)
  896. return 0;
  897. switch (set_by) {
  898. case REGDOM_SET_BY_INIT:
  899. return -EINVAL;
  900. case REGDOM_SET_BY_CORE:
  901. return -EINVAL;
  902. case REGDOM_SET_BY_COUNTRY_IE:
  903. if (unlikely(!is_an_alpha2(alpha2)))
  904. return -EINVAL;
  905. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  906. if (last_request->wiphy != wiphy) {
  907. /*
  908. * Two cards with two APs claiming different
  909. * different Country IE alpha2s. We could
  910. * intersect them, but that seems unlikely
  911. * to be correct. Reject second one for now.
  912. */
  913. if (!alpha2_equal(alpha2,
  914. cfg80211_regdomain->alpha2))
  915. return -EOPNOTSUPP;
  916. return -EALREADY;
  917. }
  918. /* Two consecutive Country IE hints on the same wiphy.
  919. * This should be picked up early by the driver/stack */
  920. if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
  921. alpha2)))
  922. return 0;
  923. return -EALREADY;
  924. }
  925. return REG_INTERSECT;
  926. case REGDOM_SET_BY_DRIVER:
  927. if (last_request->initiator == REGDOM_SET_BY_CORE) {
  928. if (is_old_static_regdom(cfg80211_regdomain))
  929. return 0;
  930. if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  931. return 0;
  932. return -EALREADY;
  933. }
  934. return REG_INTERSECT;
  935. case REGDOM_SET_BY_USER:
  936. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  937. return REG_INTERSECT;
  938. /* If the user knows better the user should set the regdom
  939. * to their country before the IE is picked up */
  940. if (last_request->initiator == REGDOM_SET_BY_USER &&
  941. last_request->intersect)
  942. return -EOPNOTSUPP;
  943. /* Process user requests only after previous user/driver/core
  944. * requests have been processed */
  945. if (last_request->initiator == REGDOM_SET_BY_CORE ||
  946. last_request->initiator == REGDOM_SET_BY_DRIVER ||
  947. last_request->initiator == REGDOM_SET_BY_USER) {
  948. if (!alpha2_equal(last_request->alpha2,
  949. cfg80211_regdomain->alpha2))
  950. return -EAGAIN;
  951. }
  952. if (!is_old_static_regdom(cfg80211_regdomain) &&
  953. alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  954. return -EALREADY;
  955. return 0;
  956. }
  957. return -EINVAL;
  958. }
  959. /* Caller must hold &cfg80211_mutex */
  960. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  961. const char *alpha2,
  962. u32 country_ie_checksum,
  963. enum environment_cap env)
  964. {
  965. struct regulatory_request *request;
  966. bool intersect = false;
  967. int r = 0;
  968. r = ignore_request(wiphy, set_by, alpha2);
  969. if (r == REG_INTERSECT) {
  970. if (set_by == REGDOM_SET_BY_DRIVER) {
  971. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  972. if (r)
  973. return r;
  974. }
  975. intersect = true;
  976. } else if (r) {
  977. /* If the regulatory domain being requested by the
  978. * driver has already been set just copy it to the
  979. * wiphy */
  980. if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
  981. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  982. if (r)
  983. return r;
  984. r = -EALREADY;
  985. goto new_request;
  986. }
  987. return r;
  988. }
  989. new_request:
  990. request = kzalloc(sizeof(struct regulatory_request),
  991. GFP_KERNEL);
  992. if (!request)
  993. return -ENOMEM;
  994. request->alpha2[0] = alpha2[0];
  995. request->alpha2[1] = alpha2[1];
  996. request->initiator = set_by;
  997. request->wiphy = wiphy;
  998. request->intersect = intersect;
  999. request->country_ie_checksum = country_ie_checksum;
  1000. request->country_ie_env = env;
  1001. kfree(last_request);
  1002. last_request = request;
  1003. /* When r == REG_INTERSECT we do need to call CRDA */
  1004. if (r < 0)
  1005. return r;
  1006. /*
  1007. * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
  1008. * AND if CRDA is NOT present nothing will happen, if someone
  1009. * wants to bother with 11d with OLD_REG you can add a timer.
  1010. * If after x amount of time nothing happens you can call:
  1011. *
  1012. * return set_regdom(country_ie_regdomain);
  1013. *
  1014. * to intersect with the static rd
  1015. */
  1016. return call_crda(alpha2);
  1017. }
  1018. static int regulatory_hint_core(const char *alpha2)
  1019. {
  1020. struct regulatory_request *request;
  1021. BUG_ON(last_request);
  1022. request = kzalloc(sizeof(struct regulatory_request),
  1023. GFP_KERNEL);
  1024. if (!request)
  1025. return -ENOMEM;
  1026. request->alpha2[0] = alpha2[0];
  1027. request->alpha2[1] = alpha2[1];
  1028. request->initiator = REGDOM_SET_BY_CORE;
  1029. last_request = request;
  1030. return call_crda(alpha2);
  1031. }
  1032. void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1033. {
  1034. int r;
  1035. BUG_ON(!alpha2);
  1036. mutex_lock(&cfg80211_mutex);
  1037. r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER,
  1038. alpha2, 0, ENVIRON_ANY);
  1039. /* This is required so that the orig_* parameters are saved */
  1040. if (r == -EALREADY && wiphy->strict_regulatory)
  1041. wiphy_update_regulatory(wiphy, REGDOM_SET_BY_DRIVER);
  1042. mutex_unlock(&cfg80211_mutex);
  1043. }
  1044. EXPORT_SYMBOL(regulatory_hint);
  1045. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1046. u32 country_ie_checksum)
  1047. {
  1048. if (!last_request->wiphy)
  1049. return false;
  1050. if (likely(last_request->wiphy != wiphy))
  1051. return !country_ie_integrity_changes(country_ie_checksum);
  1052. /* We should not have let these through at this point, they
  1053. * should have been picked up earlier by the first alpha2 check
  1054. * on the device */
  1055. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1056. return true;
  1057. return false;
  1058. }
  1059. void regulatory_hint_11d(struct wiphy *wiphy,
  1060. u8 *country_ie,
  1061. u8 country_ie_len)
  1062. {
  1063. struct ieee80211_regdomain *rd = NULL;
  1064. char alpha2[2];
  1065. u32 checksum = 0;
  1066. enum environment_cap env = ENVIRON_ANY;
  1067. if (!last_request)
  1068. return;
  1069. mutex_lock(&cfg80211_mutex);
  1070. /* IE len must be evenly divisible by 2 */
  1071. if (country_ie_len & 0x01)
  1072. goto out;
  1073. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1074. goto out;
  1075. /* Pending country IE processing, this can happen after we
  1076. * call CRDA and wait for a response if a beacon was received before
  1077. * we were able to process the last regulatory_hint_11d() call */
  1078. if (country_ie_regdomain)
  1079. goto out;
  1080. alpha2[0] = country_ie[0];
  1081. alpha2[1] = country_ie[1];
  1082. if (country_ie[2] == 'I')
  1083. env = ENVIRON_INDOOR;
  1084. else if (country_ie[2] == 'O')
  1085. env = ENVIRON_OUTDOOR;
  1086. /* We will run this for *every* beacon processed for the BSSID, so
  1087. * we optimize an early check to exit out early if we don't have to
  1088. * do anything */
  1089. if (likely(last_request->wiphy)) {
  1090. struct cfg80211_registered_device *drv_last_ie;
  1091. drv_last_ie = wiphy_to_dev(last_request->wiphy);
  1092. /* Lets keep this simple -- we trust the first AP
  1093. * after we intersect with CRDA */
  1094. if (likely(last_request->wiphy == wiphy)) {
  1095. /* Ignore IEs coming in on this wiphy with
  1096. * the same alpha2 and environment cap */
  1097. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1098. alpha2) &&
  1099. env == drv_last_ie->env)) {
  1100. goto out;
  1101. }
  1102. /* the wiphy moved on to another BSSID or the AP
  1103. * was reconfigured. XXX: We need to deal with the
  1104. * case where the user suspends and goes to goes
  1105. * to another country, and then gets IEs from an
  1106. * AP with different settings */
  1107. goto out;
  1108. } else {
  1109. /* Ignore IEs coming in on two separate wiphys with
  1110. * the same alpha2 and environment cap */
  1111. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1112. alpha2) &&
  1113. env == drv_last_ie->env)) {
  1114. goto out;
  1115. }
  1116. /* We could potentially intersect though */
  1117. goto out;
  1118. }
  1119. }
  1120. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1121. if (!rd)
  1122. goto out;
  1123. /* This will not happen right now but we leave it here for the
  1124. * the future when we want to add suspend/resume support and having
  1125. * the user move to another country after doing so, or having the user
  1126. * move to another AP. Right now we just trust the first AP. This is why
  1127. * this is marked as likley(). If we hit this before we add this support
  1128. * we want to be informed of it as it would indicate a mistake in the
  1129. * current design */
  1130. if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
  1131. goto out;
  1132. /* We keep this around for when CRDA comes back with a response so
  1133. * we can intersect with that */
  1134. country_ie_regdomain = rd;
  1135. __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
  1136. country_ie_regdomain->alpha2, checksum, env);
  1137. out:
  1138. mutex_unlock(&cfg80211_mutex);
  1139. }
  1140. EXPORT_SYMBOL(regulatory_hint_11d);
  1141. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1142. {
  1143. unsigned int i;
  1144. const struct ieee80211_reg_rule *reg_rule = NULL;
  1145. const struct ieee80211_freq_range *freq_range = NULL;
  1146. const struct ieee80211_power_rule *power_rule = NULL;
  1147. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  1148. "(max_antenna_gain, max_eirp)\n");
  1149. for (i = 0; i < rd->n_reg_rules; i++) {
  1150. reg_rule = &rd->reg_rules[i];
  1151. freq_range = &reg_rule->freq_range;
  1152. power_rule = &reg_rule->power_rule;
  1153. /* There may not be documentation for max antenna gain
  1154. * in certain regions */
  1155. if (power_rule->max_antenna_gain)
  1156. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1157. "(%d mBi, %d mBm)\n",
  1158. freq_range->start_freq_khz,
  1159. freq_range->end_freq_khz,
  1160. freq_range->max_bandwidth_khz,
  1161. power_rule->max_antenna_gain,
  1162. power_rule->max_eirp);
  1163. else
  1164. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1165. "(N/A, %d mBm)\n",
  1166. freq_range->start_freq_khz,
  1167. freq_range->end_freq_khz,
  1168. freq_range->max_bandwidth_khz,
  1169. power_rule->max_eirp);
  1170. }
  1171. }
  1172. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1173. {
  1174. if (is_intersected_alpha2(rd->alpha2)) {
  1175. struct wiphy *wiphy = NULL;
  1176. struct cfg80211_registered_device *drv;
  1177. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  1178. if (last_request->wiphy) {
  1179. wiphy = last_request->wiphy;
  1180. drv = wiphy_to_dev(wiphy);
  1181. printk(KERN_INFO "cfg80211: Current regulatory "
  1182. "domain updated by AP to: %c%c\n",
  1183. drv->country_ie_alpha2[0],
  1184. drv->country_ie_alpha2[1]);
  1185. } else
  1186. printk(KERN_INFO "cfg80211: Current regulatory "
  1187. "domain intersected: \n");
  1188. } else
  1189. printk(KERN_INFO "cfg80211: Current regulatory "
  1190. "domain intersected: \n");
  1191. } else if (is_world_regdom(rd->alpha2))
  1192. printk(KERN_INFO "cfg80211: World regulatory "
  1193. "domain updated:\n");
  1194. else {
  1195. if (is_unknown_alpha2(rd->alpha2))
  1196. printk(KERN_INFO "cfg80211: Regulatory domain "
  1197. "changed to driver built-in settings "
  1198. "(unknown country)\n");
  1199. else
  1200. printk(KERN_INFO "cfg80211: Regulatory domain "
  1201. "changed to country: %c%c\n",
  1202. rd->alpha2[0], rd->alpha2[1]);
  1203. }
  1204. print_rd_rules(rd);
  1205. }
  1206. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1207. {
  1208. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1209. rd->alpha2[0], rd->alpha2[1]);
  1210. print_rd_rules(rd);
  1211. }
  1212. #ifdef CONFIG_CFG80211_REG_DEBUG
  1213. static void reg_country_ie_process_debug(
  1214. const struct ieee80211_regdomain *rd,
  1215. const struct ieee80211_regdomain *country_ie_regdomain,
  1216. const struct ieee80211_regdomain *intersected_rd)
  1217. {
  1218. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1219. print_regdomain_info(country_ie_regdomain);
  1220. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1221. print_regdomain_info(rd);
  1222. if (intersected_rd) {
  1223. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1224. "and get:\n");
  1225. print_regdomain_info(intersected_rd);
  1226. return;
  1227. }
  1228. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1229. }
  1230. #else
  1231. static inline void reg_country_ie_process_debug(
  1232. const struct ieee80211_regdomain *rd,
  1233. const struct ieee80211_regdomain *country_ie_regdomain,
  1234. const struct ieee80211_regdomain *intersected_rd)
  1235. {
  1236. }
  1237. #endif
  1238. /* Takes ownership of rd only if it doesn't fail */
  1239. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1240. {
  1241. const struct ieee80211_regdomain *intersected_rd = NULL;
  1242. struct cfg80211_registered_device *drv = NULL;
  1243. struct wiphy *wiphy = NULL;
  1244. /* Some basic sanity checks first */
  1245. if (is_world_regdom(rd->alpha2)) {
  1246. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1247. return -EINVAL;
  1248. update_world_regdomain(rd);
  1249. return 0;
  1250. }
  1251. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1252. !is_unknown_alpha2(rd->alpha2))
  1253. return -EINVAL;
  1254. if (!last_request)
  1255. return -EINVAL;
  1256. /* Lets only bother proceeding on the same alpha2 if the current
  1257. * rd is non static (it means CRDA was present and was used last)
  1258. * and the pending request came in from a country IE */
  1259. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1260. /* If someone else asked us to change the rd lets only bother
  1261. * checking if the alpha2 changes if CRDA was already called */
  1262. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1263. !regdom_changed(rd->alpha2))
  1264. return -EINVAL;
  1265. }
  1266. wiphy = last_request->wiphy;
  1267. /* Now lets set the regulatory domain, update all driver channels
  1268. * and finally inform them of what we have done, in case they want
  1269. * to review or adjust their own settings based on their own
  1270. * internal EEPROM data */
  1271. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1272. return -EINVAL;
  1273. if (!is_valid_rd(rd)) {
  1274. printk(KERN_ERR "cfg80211: Invalid "
  1275. "regulatory domain detected:\n");
  1276. print_regdomain_info(rd);
  1277. return -EINVAL;
  1278. }
  1279. if (!last_request->intersect) {
  1280. int r;
  1281. if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
  1282. reset_regdomains();
  1283. cfg80211_regdomain = rd;
  1284. return 0;
  1285. }
  1286. /* For a driver hint, lets copy the regulatory domain the
  1287. * driver wanted to the wiphy to deal with conflicts */
  1288. BUG_ON(last_request->wiphy->regd);
  1289. r = reg_copy_regd(&last_request->wiphy->regd, rd);
  1290. if (r)
  1291. return r;
  1292. reset_regdomains();
  1293. cfg80211_regdomain = rd;
  1294. return 0;
  1295. }
  1296. /* Intersection requires a bit more work */
  1297. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1298. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1299. if (!intersected_rd)
  1300. return -EINVAL;
  1301. /* We can trash what CRDA provided now.
  1302. * However if a driver requested this specific regulatory
  1303. * domain we keep it for its private use */
  1304. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  1305. last_request->wiphy->regd = rd;
  1306. else
  1307. kfree(rd);
  1308. rd = NULL;
  1309. reset_regdomains();
  1310. cfg80211_regdomain = intersected_rd;
  1311. return 0;
  1312. }
  1313. /*
  1314. * Country IE requests are handled a bit differently, we intersect
  1315. * the country IE rd with what CRDA believes that country should have
  1316. */
  1317. BUG_ON(!country_ie_regdomain);
  1318. if (rd != country_ie_regdomain) {
  1319. /* Intersect what CRDA returned and our what we
  1320. * had built from the Country IE received */
  1321. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1322. reg_country_ie_process_debug(rd, country_ie_regdomain,
  1323. intersected_rd);
  1324. kfree(country_ie_regdomain);
  1325. country_ie_regdomain = NULL;
  1326. } else {
  1327. /* This would happen when CRDA was not present and
  1328. * OLD_REGULATORY was enabled. We intersect our Country
  1329. * IE rd and what was set on cfg80211 originally */
  1330. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1331. }
  1332. if (!intersected_rd)
  1333. return -EINVAL;
  1334. drv = wiphy_to_dev(wiphy);
  1335. drv->country_ie_alpha2[0] = rd->alpha2[0];
  1336. drv->country_ie_alpha2[1] = rd->alpha2[1];
  1337. drv->env = last_request->country_ie_env;
  1338. BUG_ON(intersected_rd == rd);
  1339. kfree(rd);
  1340. rd = NULL;
  1341. reset_regdomains();
  1342. cfg80211_regdomain = intersected_rd;
  1343. return 0;
  1344. }
  1345. /* Use this call to set the current regulatory domain. Conflicts with
  1346. * multiple drivers can be ironed out later. Caller must've already
  1347. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex */
  1348. int set_regdom(const struct ieee80211_regdomain *rd)
  1349. {
  1350. int r;
  1351. /* Note that this doesn't update the wiphys, this is done below */
  1352. r = __set_regdom(rd);
  1353. if (r) {
  1354. kfree(rd);
  1355. return r;
  1356. }
  1357. /* This would make this whole thing pointless */
  1358. if (!last_request->intersect)
  1359. BUG_ON(rd != cfg80211_regdomain);
  1360. /* update all wiphys now with the new established regulatory domain */
  1361. update_all_wiphy_regulatory(last_request->initiator);
  1362. print_regdomain(cfg80211_regdomain);
  1363. return r;
  1364. }
  1365. /* Caller must hold cfg80211_mutex */
  1366. void reg_device_remove(struct wiphy *wiphy)
  1367. {
  1368. kfree(wiphy->regd);
  1369. if (!last_request || !last_request->wiphy)
  1370. return;
  1371. if (last_request->wiphy != wiphy)
  1372. return;
  1373. last_request->wiphy = NULL;
  1374. last_request->country_ie_env = ENVIRON_ANY;
  1375. }
  1376. int regulatory_init(void)
  1377. {
  1378. int err;
  1379. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1380. if (IS_ERR(reg_pdev))
  1381. return PTR_ERR(reg_pdev);
  1382. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  1383. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  1384. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  1385. print_regdomain_info(cfg80211_regdomain);
  1386. /* The old code still requests for a new regdomain and if
  1387. * you have CRDA you get it updated, otherwise you get
  1388. * stuck with the static values. We ignore "EU" code as
  1389. * that is not a valid ISO / IEC 3166 alpha2 */
  1390. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  1391. err = regulatory_hint_core(ieee80211_regdom);
  1392. #else
  1393. cfg80211_regdomain = cfg80211_world_regdom;
  1394. err = regulatory_hint_core("00");
  1395. if (err) {
  1396. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  1397. "unable to update world regulatory domain, "
  1398. "using static definition\n");
  1399. }
  1400. #endif
  1401. return 0;
  1402. }
  1403. void regulatory_exit(void)
  1404. {
  1405. mutex_lock(&cfg80211_mutex);
  1406. reset_regdomains();
  1407. kfree(country_ie_regdomain);
  1408. country_ie_regdomain = NULL;
  1409. kfree(last_request);
  1410. platform_device_unregister(reg_pdev);
  1411. mutex_unlock(&cfg80211_mutex);
  1412. }