reg.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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. static 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. struct ieee80211_country_ie_triplet *triplet =
  412. (struct ieee80211_country_ie_triplet *) country_ie;
  413. int cur_sub_max_channel = 0, cur_channel = 0;
  414. if (triplet->ext.reg_extension_id >=
  415. IEEE80211_COUNTRY_EXTENSION_ID) {
  416. country_ie += 3;
  417. country_ie_len -= 3;
  418. continue;
  419. }
  420. cur_channel = triplet->chans.first_channel;
  421. cur_sub_max_channel = ieee80211_channel_to_frequency(
  422. cur_channel + triplet->chans.num_channels);
  423. /* Basic sanity check */
  424. if (cur_sub_max_channel < cur_channel)
  425. return NULL;
  426. /* Do not allow overlapping channels. Also channels
  427. * passed in each subband must be monotonically
  428. * increasing */
  429. if (last_sub_max_channel) {
  430. if (cur_channel <= last_sub_max_channel)
  431. return NULL;
  432. if (cur_sub_max_channel <= last_sub_max_channel)
  433. return NULL;
  434. }
  435. /* When dot11RegulatoryClassesRequired is supported
  436. * we can throw ext triplets as part of this soup,
  437. * for now we don't care when those change as we
  438. * don't support them */
  439. *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
  440. ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
  441. ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
  442. last_sub_max_channel = cur_sub_max_channel;
  443. country_ie += 3;
  444. country_ie_len -= 3;
  445. num_rules++;
  446. /* Note: this is not a IEEE requirement but
  447. * simply a memory requirement */
  448. if (num_rules > NL80211_MAX_SUPP_REG_RULES)
  449. return NULL;
  450. }
  451. country_ie = triplets_start;
  452. country_ie_len = len_at_triplet;
  453. size_of_regd = sizeof(struct ieee80211_regdomain) +
  454. (num_rules * sizeof(struct ieee80211_reg_rule));
  455. rd = kzalloc(size_of_regd, GFP_KERNEL);
  456. if (!rd)
  457. return NULL;
  458. rd->n_reg_rules = num_rules;
  459. rd->alpha2[0] = alpha2[0];
  460. rd->alpha2[1] = alpha2[1];
  461. /* This time around we fill in the rd */
  462. while (country_ie_len >= 3) {
  463. int end_channel = 0;
  464. struct ieee80211_country_ie_triplet *triplet =
  465. (struct ieee80211_country_ie_triplet *) country_ie;
  466. struct ieee80211_reg_rule *reg_rule = NULL;
  467. struct ieee80211_freq_range *freq_range = NULL;
  468. struct ieee80211_power_rule *power_rule = NULL;
  469. /* Must parse if dot11RegulatoryClassesRequired is true,
  470. * we don't support this yet */
  471. if (triplet->ext.reg_extension_id >=
  472. IEEE80211_COUNTRY_EXTENSION_ID) {
  473. country_ie += 3;
  474. country_ie_len -= 3;
  475. continue;
  476. }
  477. reg_rule = &rd->reg_rules[i];
  478. freq_range = &reg_rule->freq_range;
  479. power_rule = &reg_rule->power_rule;
  480. reg_rule->flags = flags;
  481. /* 2 GHz */
  482. if (triplet->chans.first_channel <= 14)
  483. end_channel = triplet->chans.first_channel +
  484. triplet->chans.num_channels;
  485. else
  486. /*
  487. * 5 GHz -- For example in country IEs if the first
  488. * channel given is 36 and the number of channels is 4
  489. * then the individual channel numbers defined for the
  490. * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
  491. * and not 36, 37, 38, 39.
  492. *
  493. * See: http://tinyurl.com/11d-clarification
  494. */
  495. end_channel = triplet->chans.first_channel +
  496. (4 * (triplet->chans.num_channels - 1));
  497. /* The +10 is since the regulatory domain expects
  498. * the actual band edge, not the center of freq for
  499. * its start and end freqs, assuming 20 MHz bandwidth on
  500. * the channels passed */
  501. freq_range->start_freq_khz =
  502. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  503. triplet->chans.first_channel) - 10);
  504. freq_range->end_freq_khz =
  505. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  506. end_channel) + 10);
  507. /* Large arbitrary values, we intersect later */
  508. /* Increment this if we ever support >= 40 MHz channels
  509. * in IEEE 802.11 */
  510. freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
  511. power_rule->max_antenna_gain = DBI_TO_MBI(100);
  512. power_rule->max_eirp = DBM_TO_MBM(100);
  513. country_ie += 3;
  514. country_ie_len -= 3;
  515. i++;
  516. BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
  517. }
  518. return rd;
  519. }
  520. /* Helper for regdom_intersect(), this does the real
  521. * mathematical intersection fun */
  522. static int reg_rules_intersect(
  523. const struct ieee80211_reg_rule *rule1,
  524. const struct ieee80211_reg_rule *rule2,
  525. struct ieee80211_reg_rule *intersected_rule)
  526. {
  527. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  528. struct ieee80211_freq_range *freq_range;
  529. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  530. struct ieee80211_power_rule *power_rule;
  531. u32 freq_diff;
  532. freq_range1 = &rule1->freq_range;
  533. freq_range2 = &rule2->freq_range;
  534. freq_range = &intersected_rule->freq_range;
  535. power_rule1 = &rule1->power_rule;
  536. power_rule2 = &rule2->power_rule;
  537. power_rule = &intersected_rule->power_rule;
  538. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  539. freq_range2->start_freq_khz);
  540. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  541. freq_range2->end_freq_khz);
  542. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  543. freq_range2->max_bandwidth_khz);
  544. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  545. if (freq_range->max_bandwidth_khz > freq_diff)
  546. freq_range->max_bandwidth_khz = freq_diff;
  547. power_rule->max_eirp = min(power_rule1->max_eirp,
  548. power_rule2->max_eirp);
  549. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  550. power_rule2->max_antenna_gain);
  551. intersected_rule->flags = (rule1->flags | rule2->flags);
  552. if (!is_valid_reg_rule(intersected_rule))
  553. return -EINVAL;
  554. return 0;
  555. }
  556. /**
  557. * regdom_intersect - do the intersection between two regulatory domains
  558. * @rd1: first regulatory domain
  559. * @rd2: second regulatory domain
  560. *
  561. * Use this function to get the intersection between two regulatory domains.
  562. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  563. * as no one single alpha2 can represent this regulatory domain.
  564. *
  565. * Returns a pointer to the regulatory domain structure which will hold the
  566. * resulting intersection of rules between rd1 and rd2. We will
  567. * kzalloc() this structure for you.
  568. */
  569. static struct ieee80211_regdomain *regdom_intersect(
  570. const struct ieee80211_regdomain *rd1,
  571. const struct ieee80211_regdomain *rd2)
  572. {
  573. int r, size_of_regd;
  574. unsigned int x, y;
  575. unsigned int num_rules = 0, rule_idx = 0;
  576. const struct ieee80211_reg_rule *rule1, *rule2;
  577. struct ieee80211_reg_rule *intersected_rule;
  578. struct ieee80211_regdomain *rd;
  579. /* This is just a dummy holder to help us count */
  580. struct ieee80211_reg_rule irule;
  581. /* Uses the stack temporarily for counter arithmetic */
  582. intersected_rule = &irule;
  583. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  584. if (!rd1 || !rd2)
  585. return NULL;
  586. /* First we get a count of the rules we'll need, then we actually
  587. * build them. This is to so we can malloc() and free() a
  588. * regdomain once. The reason we use reg_rules_intersect() here
  589. * is it will return -EINVAL if the rule computed makes no sense.
  590. * All rules that do check out OK are valid. */
  591. for (x = 0; x < rd1->n_reg_rules; x++) {
  592. rule1 = &rd1->reg_rules[x];
  593. for (y = 0; y < rd2->n_reg_rules; y++) {
  594. rule2 = &rd2->reg_rules[y];
  595. if (!reg_rules_intersect(rule1, rule2,
  596. intersected_rule))
  597. num_rules++;
  598. memset(intersected_rule, 0,
  599. sizeof(struct ieee80211_reg_rule));
  600. }
  601. }
  602. if (!num_rules)
  603. return NULL;
  604. size_of_regd = sizeof(struct ieee80211_regdomain) +
  605. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  606. rd = kzalloc(size_of_regd, GFP_KERNEL);
  607. if (!rd)
  608. return NULL;
  609. for (x = 0; x < rd1->n_reg_rules; x++) {
  610. rule1 = &rd1->reg_rules[x];
  611. for (y = 0; y < rd2->n_reg_rules; y++) {
  612. rule2 = &rd2->reg_rules[y];
  613. /* This time around instead of using the stack lets
  614. * write to the target rule directly saving ourselves
  615. * a memcpy() */
  616. intersected_rule = &rd->reg_rules[rule_idx];
  617. r = reg_rules_intersect(rule1, rule2,
  618. intersected_rule);
  619. /* No need to memset here the intersected rule here as
  620. * we're not using the stack anymore */
  621. if (r)
  622. continue;
  623. rule_idx++;
  624. }
  625. }
  626. if (rule_idx != num_rules) {
  627. kfree(rd);
  628. return NULL;
  629. }
  630. rd->n_reg_rules = num_rules;
  631. rd->alpha2[0] = '9';
  632. rd->alpha2[1] = '8';
  633. return rd;
  634. }
  635. /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  636. * want to just have the channel structure use these */
  637. static u32 map_regdom_flags(u32 rd_flags)
  638. {
  639. u32 channel_flags = 0;
  640. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  641. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  642. if (rd_flags & NL80211_RRF_NO_IBSS)
  643. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  644. if (rd_flags & NL80211_RRF_DFS)
  645. channel_flags |= IEEE80211_CHAN_RADAR;
  646. return channel_flags;
  647. }
  648. static int freq_reg_info_regd(struct wiphy *wiphy,
  649. u32 center_freq,
  650. u32 *bandwidth,
  651. const struct ieee80211_reg_rule **reg_rule,
  652. const struct ieee80211_regdomain *custom_regd)
  653. {
  654. int i;
  655. bool band_rule_found = false;
  656. const struct ieee80211_regdomain *regd;
  657. u32 max_bandwidth = 0;
  658. regd = custom_regd ? custom_regd : cfg80211_regdomain;
  659. /* Follow the driver's regulatory domain, if present, unless a country
  660. * IE has been processed */
  661. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
  662. wiphy->regd)
  663. regd = wiphy->regd;
  664. if (!regd)
  665. return -EINVAL;
  666. for (i = 0; i < regd->n_reg_rules; i++) {
  667. const struct ieee80211_reg_rule *rr;
  668. const struct ieee80211_freq_range *fr = NULL;
  669. const struct ieee80211_power_rule *pr = NULL;
  670. rr = &regd->reg_rules[i];
  671. fr = &rr->freq_range;
  672. pr = &rr->power_rule;
  673. /* We only need to know if one frequency rule was
  674. * was in center_freq's band, that's enough, so lets
  675. * not overwrite it once found */
  676. if (!band_rule_found)
  677. band_rule_found = freq_in_rule_band(fr, center_freq);
  678. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  679. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  680. *reg_rule = rr;
  681. *bandwidth = max_bandwidth;
  682. break;
  683. }
  684. }
  685. if (!band_rule_found)
  686. return -ERANGE;
  687. return !max_bandwidth;
  688. }
  689. EXPORT_SYMBOL(freq_reg_info);
  690. int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
  691. const struct ieee80211_reg_rule **reg_rule)
  692. {
  693. return freq_reg_info_regd(wiphy, center_freq,
  694. bandwidth, reg_rule, NULL);
  695. }
  696. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  697. unsigned int chan_idx)
  698. {
  699. int r;
  700. u32 flags;
  701. u32 max_bandwidth = 0;
  702. const struct ieee80211_reg_rule *reg_rule = NULL;
  703. const struct ieee80211_power_rule *power_rule = NULL;
  704. struct ieee80211_supported_band *sband;
  705. struct ieee80211_channel *chan;
  706. sband = wiphy->bands[band];
  707. BUG_ON(chan_idx >= sband->n_channels);
  708. chan = &sband->channels[chan_idx];
  709. flags = chan->orig_flags;
  710. r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
  711. &max_bandwidth, &reg_rule);
  712. if (r) {
  713. /* This means no regulatory rule was found in the country IE
  714. * with a frequency range on the center_freq's band, since
  715. * IEEE-802.11 allows for a country IE to have a subset of the
  716. * regulatory information provided in a country we ignore
  717. * disabling the channel unless at least one reg rule was
  718. * found on the center_freq's band. For details see this
  719. * clarification:
  720. *
  721. * http://tinyurl.com/11d-clarification
  722. */
  723. if (r == -ERANGE &&
  724. last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  725. #ifdef CONFIG_CFG80211_REG_DEBUG
  726. printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
  727. "intact on %s - no rule found in band on "
  728. "Country IE\n",
  729. chan->center_freq, wiphy_name(wiphy));
  730. #endif
  731. } else {
  732. /* In this case we know the country IE has at least one reg rule
  733. * for the band so we respect its band definitions */
  734. #ifdef CONFIG_CFG80211_REG_DEBUG
  735. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  736. printk(KERN_DEBUG "cfg80211: Disabling "
  737. "channel %d MHz on %s due to "
  738. "Country IE\n",
  739. chan->center_freq, wiphy_name(wiphy));
  740. #endif
  741. flags |= IEEE80211_CHAN_DISABLED;
  742. chan->flags = flags;
  743. }
  744. return;
  745. }
  746. power_rule = &reg_rule->power_rule;
  747. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  748. chan->max_antenna_gain = min(chan->orig_mag,
  749. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  750. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  751. if (chan->orig_mpwr)
  752. chan->max_power = min(chan->orig_mpwr,
  753. (int) MBM_TO_DBM(power_rule->max_eirp));
  754. else
  755. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  756. }
  757. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  758. {
  759. unsigned int i;
  760. struct ieee80211_supported_band *sband;
  761. BUG_ON(!wiphy->bands[band]);
  762. sband = wiphy->bands[band];
  763. for (i = 0; i < sband->n_channels; i++)
  764. handle_channel(wiphy, band, i);
  765. }
  766. static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
  767. {
  768. if (!last_request)
  769. return true;
  770. if (setby == REGDOM_SET_BY_CORE &&
  771. wiphy->custom_regulatory)
  772. return true;
  773. return false;
  774. }
  775. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  776. {
  777. struct cfg80211_registered_device *drv;
  778. list_for_each_entry(drv, &cfg80211_drv_list, list)
  779. wiphy_update_regulatory(&drv->wiphy, setby);
  780. }
  781. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  782. {
  783. enum ieee80211_band band;
  784. if (ignore_reg_update(wiphy, setby))
  785. return;
  786. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  787. if (wiphy->bands[band])
  788. handle_band(wiphy, band);
  789. }
  790. if (wiphy->reg_notifier)
  791. wiphy->reg_notifier(wiphy, last_request);
  792. }
  793. static void handle_channel_custom(struct wiphy *wiphy,
  794. enum ieee80211_band band,
  795. unsigned int chan_idx,
  796. const struct ieee80211_regdomain *regd)
  797. {
  798. int r;
  799. u32 max_bandwidth = 0;
  800. const struct ieee80211_reg_rule *reg_rule = NULL;
  801. const struct ieee80211_power_rule *power_rule = NULL;
  802. struct ieee80211_supported_band *sband;
  803. struct ieee80211_channel *chan;
  804. sband = wiphy->bands[band];
  805. BUG_ON(chan_idx >= sband->n_channels);
  806. chan = &sband->channels[chan_idx];
  807. r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  808. &max_bandwidth, &reg_rule, regd);
  809. if (r) {
  810. chan->flags = IEEE80211_CHAN_DISABLED;
  811. return;
  812. }
  813. power_rule = &reg_rule->power_rule;
  814. chan->flags |= map_regdom_flags(reg_rule->flags);
  815. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  816. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  817. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  818. }
  819. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  820. const struct ieee80211_regdomain *regd)
  821. {
  822. unsigned int i;
  823. struct ieee80211_supported_band *sband;
  824. BUG_ON(!wiphy->bands[band]);
  825. sband = wiphy->bands[band];
  826. for (i = 0; i < sband->n_channels; i++)
  827. handle_channel_custom(wiphy, band, i, regd);
  828. }
  829. /* Used by drivers prior to wiphy registration */
  830. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  831. const struct ieee80211_regdomain *regd)
  832. {
  833. enum ieee80211_band band;
  834. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  835. if (wiphy->bands[band])
  836. handle_band_custom(wiphy, band, regd);
  837. }
  838. }
  839. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  840. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  841. const struct ieee80211_regdomain *src_regd)
  842. {
  843. struct ieee80211_regdomain *regd;
  844. int size_of_regd = 0;
  845. unsigned int i;
  846. size_of_regd = sizeof(struct ieee80211_regdomain) +
  847. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  848. regd = kzalloc(size_of_regd, GFP_KERNEL);
  849. if (!regd)
  850. return -ENOMEM;
  851. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  852. for (i = 0; i < src_regd->n_reg_rules; i++)
  853. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  854. sizeof(struct ieee80211_reg_rule));
  855. *dst_regd = regd;
  856. return 0;
  857. }
  858. /* Return value which can be used by ignore_request() to indicate
  859. * it has been determined we should intersect two regulatory domains */
  860. #define REG_INTERSECT 1
  861. /* This has the logic which determines when a new request
  862. * should be ignored. */
  863. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  864. const char *alpha2)
  865. {
  866. /* All initial requests are respected */
  867. if (!last_request)
  868. return 0;
  869. switch (set_by) {
  870. case REGDOM_SET_BY_INIT:
  871. return -EINVAL;
  872. case REGDOM_SET_BY_CORE:
  873. /*
  874. * Always respect new wireless core hints, should only happen
  875. * when updating the world regulatory domain at init.
  876. */
  877. return 0;
  878. case REGDOM_SET_BY_COUNTRY_IE:
  879. if (unlikely(!is_an_alpha2(alpha2)))
  880. return -EINVAL;
  881. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  882. if (last_request->wiphy != wiphy) {
  883. /*
  884. * Two cards with two APs claiming different
  885. * different Country IE alpha2s. We could
  886. * intersect them, but that seems unlikely
  887. * to be correct. Reject second one for now.
  888. */
  889. if (!alpha2_equal(alpha2,
  890. cfg80211_regdomain->alpha2))
  891. return -EOPNOTSUPP;
  892. return -EALREADY;
  893. }
  894. /* Two consecutive Country IE hints on the same wiphy.
  895. * This should be picked up early by the driver/stack */
  896. if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
  897. alpha2)))
  898. return 0;
  899. return -EALREADY;
  900. }
  901. return REG_INTERSECT;
  902. case REGDOM_SET_BY_DRIVER:
  903. if (last_request->initiator == REGDOM_SET_BY_CORE) {
  904. if (is_old_static_regdom(cfg80211_regdomain))
  905. return 0;
  906. if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  907. return 0;
  908. return -EALREADY;
  909. }
  910. return REG_INTERSECT;
  911. case REGDOM_SET_BY_USER:
  912. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  913. return REG_INTERSECT;
  914. /* If the user knows better the user should set the regdom
  915. * to their country before the IE is picked up */
  916. if (last_request->initiator == REGDOM_SET_BY_USER &&
  917. last_request->intersect)
  918. return -EOPNOTSUPP;
  919. /* Process user requests only after previous user/driver/core
  920. * requests have been processed */
  921. if (last_request->initiator == REGDOM_SET_BY_CORE ||
  922. last_request->initiator == REGDOM_SET_BY_DRIVER ||
  923. last_request->initiator == REGDOM_SET_BY_USER) {
  924. if (!alpha2_equal(last_request->alpha2,
  925. cfg80211_regdomain->alpha2))
  926. return -EAGAIN;
  927. }
  928. if (!is_old_static_regdom(cfg80211_regdomain) &&
  929. alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  930. return -EALREADY;
  931. return 0;
  932. }
  933. return -EINVAL;
  934. }
  935. /* Caller must hold &cfg80211_drv_mutex */
  936. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  937. const char *alpha2,
  938. u32 country_ie_checksum,
  939. enum environment_cap env)
  940. {
  941. struct regulatory_request *request;
  942. bool intersect = false;
  943. int r = 0;
  944. r = ignore_request(wiphy, set_by, alpha2);
  945. if (r == REG_INTERSECT) {
  946. if (set_by == REGDOM_SET_BY_DRIVER) {
  947. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  948. if (r)
  949. return r;
  950. }
  951. intersect = true;
  952. } else if (r) {
  953. /* If the regulatory domain being requested by the
  954. * driver has already been set just copy it to the
  955. * wiphy */
  956. if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
  957. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  958. if (r)
  959. return r;
  960. r = -EALREADY;
  961. goto new_request;
  962. }
  963. return r;
  964. }
  965. new_request:
  966. request = kzalloc(sizeof(struct regulatory_request),
  967. GFP_KERNEL);
  968. if (!request)
  969. return -ENOMEM;
  970. request->alpha2[0] = alpha2[0];
  971. request->alpha2[1] = alpha2[1];
  972. request->initiator = set_by;
  973. request->wiphy = wiphy;
  974. request->intersect = intersect;
  975. request->country_ie_checksum = country_ie_checksum;
  976. request->country_ie_env = env;
  977. kfree(last_request);
  978. last_request = request;
  979. /* When r == REG_INTERSECT we do need to call CRDA */
  980. if (r < 0)
  981. return r;
  982. /*
  983. * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
  984. * AND if CRDA is NOT present nothing will happen, if someone
  985. * wants to bother with 11d with OLD_REG you can add a timer.
  986. * If after x amount of time nothing happens you can call:
  987. *
  988. * return set_regdom(country_ie_regdomain);
  989. *
  990. * to intersect with the static rd
  991. */
  992. return call_crda(alpha2);
  993. }
  994. void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  995. {
  996. BUG_ON(!alpha2);
  997. mutex_lock(&cfg80211_drv_mutex);
  998. __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY);
  999. mutex_unlock(&cfg80211_drv_mutex);
  1000. }
  1001. EXPORT_SYMBOL(regulatory_hint);
  1002. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1003. u32 country_ie_checksum)
  1004. {
  1005. if (!last_request->wiphy)
  1006. return false;
  1007. if (likely(last_request->wiphy != wiphy))
  1008. return !country_ie_integrity_changes(country_ie_checksum);
  1009. /* We should not have let these through at this point, they
  1010. * should have been picked up earlier by the first alpha2 check
  1011. * on the device */
  1012. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1013. return true;
  1014. return false;
  1015. }
  1016. void regulatory_hint_11d(struct wiphy *wiphy,
  1017. u8 *country_ie,
  1018. u8 country_ie_len)
  1019. {
  1020. struct ieee80211_regdomain *rd = NULL;
  1021. char alpha2[2];
  1022. u32 checksum = 0;
  1023. enum environment_cap env = ENVIRON_ANY;
  1024. if (!last_request)
  1025. return;
  1026. mutex_lock(&cfg80211_drv_mutex);
  1027. /* IE len must be evenly divisible by 2 */
  1028. if (country_ie_len & 0x01)
  1029. goto out;
  1030. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1031. goto out;
  1032. /* Pending country IE processing, this can happen after we
  1033. * call CRDA and wait for a response if a beacon was received before
  1034. * we were able to process the last regulatory_hint_11d() call */
  1035. if (country_ie_regdomain)
  1036. goto out;
  1037. alpha2[0] = country_ie[0];
  1038. alpha2[1] = country_ie[1];
  1039. if (country_ie[2] == 'I')
  1040. env = ENVIRON_INDOOR;
  1041. else if (country_ie[2] == 'O')
  1042. env = ENVIRON_OUTDOOR;
  1043. /* We will run this for *every* beacon processed for the BSSID, so
  1044. * we optimize an early check to exit out early if we don't have to
  1045. * do anything */
  1046. if (likely(last_request->wiphy)) {
  1047. struct cfg80211_registered_device *drv_last_ie;
  1048. drv_last_ie = wiphy_to_dev(last_request->wiphy);
  1049. /* Lets keep this simple -- we trust the first AP
  1050. * after we intersect with CRDA */
  1051. if (likely(last_request->wiphy == wiphy)) {
  1052. /* Ignore IEs coming in on this wiphy with
  1053. * the same alpha2 and environment cap */
  1054. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1055. alpha2) &&
  1056. env == drv_last_ie->env)) {
  1057. goto out;
  1058. }
  1059. /* the wiphy moved on to another BSSID or the AP
  1060. * was reconfigured. XXX: We need to deal with the
  1061. * case where the user suspends and goes to goes
  1062. * to another country, and then gets IEs from an
  1063. * AP with different settings */
  1064. goto out;
  1065. } else {
  1066. /* Ignore IEs coming in on two separate wiphys with
  1067. * the same alpha2 and environment cap */
  1068. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1069. alpha2) &&
  1070. env == drv_last_ie->env)) {
  1071. goto out;
  1072. }
  1073. /* We could potentially intersect though */
  1074. goto out;
  1075. }
  1076. }
  1077. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1078. if (!rd)
  1079. goto out;
  1080. /* This will not happen right now but we leave it here for the
  1081. * the future when we want to add suspend/resume support and having
  1082. * the user move to another country after doing so, or having the user
  1083. * move to another AP. Right now we just trust the first AP. This is why
  1084. * this is marked as likley(). If we hit this before we add this support
  1085. * we want to be informed of it as it would indicate a mistake in the
  1086. * current design */
  1087. if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
  1088. goto out;
  1089. /* We keep this around for when CRDA comes back with a response so
  1090. * we can intersect with that */
  1091. country_ie_regdomain = rd;
  1092. __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
  1093. country_ie_regdomain->alpha2, checksum, env);
  1094. out:
  1095. mutex_unlock(&cfg80211_drv_mutex);
  1096. }
  1097. EXPORT_SYMBOL(regulatory_hint_11d);
  1098. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1099. {
  1100. unsigned int i;
  1101. const struct ieee80211_reg_rule *reg_rule = NULL;
  1102. const struct ieee80211_freq_range *freq_range = NULL;
  1103. const struct ieee80211_power_rule *power_rule = NULL;
  1104. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  1105. "(max_antenna_gain, max_eirp)\n");
  1106. for (i = 0; i < rd->n_reg_rules; i++) {
  1107. reg_rule = &rd->reg_rules[i];
  1108. freq_range = &reg_rule->freq_range;
  1109. power_rule = &reg_rule->power_rule;
  1110. /* There may not be documentation for max antenna gain
  1111. * in certain regions */
  1112. if (power_rule->max_antenna_gain)
  1113. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1114. "(%d mBi, %d mBm)\n",
  1115. freq_range->start_freq_khz,
  1116. freq_range->end_freq_khz,
  1117. freq_range->max_bandwidth_khz,
  1118. power_rule->max_antenna_gain,
  1119. power_rule->max_eirp);
  1120. else
  1121. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1122. "(N/A, %d mBm)\n",
  1123. freq_range->start_freq_khz,
  1124. freq_range->end_freq_khz,
  1125. freq_range->max_bandwidth_khz,
  1126. power_rule->max_eirp);
  1127. }
  1128. }
  1129. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1130. {
  1131. if (is_intersected_alpha2(rd->alpha2)) {
  1132. struct wiphy *wiphy = NULL;
  1133. struct cfg80211_registered_device *drv;
  1134. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  1135. if (last_request->wiphy) {
  1136. wiphy = last_request->wiphy;
  1137. drv = wiphy_to_dev(wiphy);
  1138. printk(KERN_INFO "cfg80211: Current regulatory "
  1139. "domain updated by AP to: %c%c\n",
  1140. drv->country_ie_alpha2[0],
  1141. drv->country_ie_alpha2[1]);
  1142. } else
  1143. printk(KERN_INFO "cfg80211: Current regulatory "
  1144. "domain intersected: \n");
  1145. } else
  1146. printk(KERN_INFO "cfg80211: Current regulatory "
  1147. "domain intersected: \n");
  1148. } else if (is_world_regdom(rd->alpha2))
  1149. printk(KERN_INFO "cfg80211: World regulatory "
  1150. "domain updated:\n");
  1151. else {
  1152. if (is_unknown_alpha2(rd->alpha2))
  1153. printk(KERN_INFO "cfg80211: Regulatory domain "
  1154. "changed to driver built-in settings "
  1155. "(unknown country)\n");
  1156. else
  1157. printk(KERN_INFO "cfg80211: Regulatory domain "
  1158. "changed to country: %c%c\n",
  1159. rd->alpha2[0], rd->alpha2[1]);
  1160. }
  1161. print_rd_rules(rd);
  1162. }
  1163. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1164. {
  1165. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1166. rd->alpha2[0], rd->alpha2[1]);
  1167. print_rd_rules(rd);
  1168. }
  1169. #ifdef CONFIG_CFG80211_REG_DEBUG
  1170. static void reg_country_ie_process_debug(
  1171. const struct ieee80211_regdomain *rd,
  1172. const struct ieee80211_regdomain *country_ie_regdomain,
  1173. const struct ieee80211_regdomain *intersected_rd)
  1174. {
  1175. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1176. print_regdomain_info(country_ie_regdomain);
  1177. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1178. print_regdomain_info(rd);
  1179. if (intersected_rd) {
  1180. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1181. "and get:\n");
  1182. print_regdomain_info(rd);
  1183. return;
  1184. }
  1185. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1186. }
  1187. #else
  1188. static inline void reg_country_ie_process_debug(
  1189. const struct ieee80211_regdomain *rd,
  1190. const struct ieee80211_regdomain *country_ie_regdomain,
  1191. const struct ieee80211_regdomain *intersected_rd)
  1192. {
  1193. }
  1194. #endif
  1195. /* Takes ownership of rd only if it doesn't fail */
  1196. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1197. {
  1198. const struct ieee80211_regdomain *intersected_rd = NULL;
  1199. struct cfg80211_registered_device *drv = NULL;
  1200. struct wiphy *wiphy = NULL;
  1201. /* Some basic sanity checks first */
  1202. if (is_world_regdom(rd->alpha2)) {
  1203. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1204. return -EINVAL;
  1205. update_world_regdomain(rd);
  1206. return 0;
  1207. }
  1208. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1209. !is_unknown_alpha2(rd->alpha2))
  1210. return -EINVAL;
  1211. if (!last_request)
  1212. return -EINVAL;
  1213. /* Lets only bother proceeding on the same alpha2 if the current
  1214. * rd is non static (it means CRDA was present and was used last)
  1215. * and the pending request came in from a country IE */
  1216. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1217. /* If someone else asked us to change the rd lets only bother
  1218. * checking if the alpha2 changes if CRDA was already called */
  1219. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1220. !regdom_changed(rd->alpha2))
  1221. return -EINVAL;
  1222. }
  1223. wiphy = last_request->wiphy;
  1224. /* Now lets set the regulatory domain, update all driver channels
  1225. * and finally inform them of what we have done, in case they want
  1226. * to review or adjust their own settings based on their own
  1227. * internal EEPROM data */
  1228. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1229. return -EINVAL;
  1230. if (!is_valid_rd(rd)) {
  1231. printk(KERN_ERR "cfg80211: Invalid "
  1232. "regulatory domain detected:\n");
  1233. print_regdomain_info(rd);
  1234. return -EINVAL;
  1235. }
  1236. if (!last_request->intersect) {
  1237. int r;
  1238. if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
  1239. reset_regdomains();
  1240. cfg80211_regdomain = rd;
  1241. return 0;
  1242. }
  1243. /* For a driver hint, lets copy the regulatory domain the
  1244. * driver wanted to the wiphy to deal with conflicts */
  1245. BUG_ON(last_request->wiphy->regd);
  1246. r = reg_copy_regd(&last_request->wiphy->regd, rd);
  1247. if (r)
  1248. return r;
  1249. reset_regdomains();
  1250. cfg80211_regdomain = rd;
  1251. return 0;
  1252. }
  1253. /* Intersection requires a bit more work */
  1254. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1255. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1256. if (!intersected_rd)
  1257. return -EINVAL;
  1258. /* We can trash what CRDA provided now.
  1259. * However if a driver requested this specific regulatory
  1260. * domain we keep it for its private use */
  1261. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  1262. last_request->wiphy->regd = rd;
  1263. else
  1264. kfree(rd);
  1265. rd = NULL;
  1266. reset_regdomains();
  1267. cfg80211_regdomain = intersected_rd;
  1268. return 0;
  1269. }
  1270. /*
  1271. * Country IE requests are handled a bit differently, we intersect
  1272. * the country IE rd with what CRDA believes that country should have
  1273. */
  1274. BUG_ON(!country_ie_regdomain);
  1275. if (rd != country_ie_regdomain) {
  1276. /* Intersect what CRDA returned and our what we
  1277. * had built from the Country IE received */
  1278. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1279. reg_country_ie_process_debug(rd, country_ie_regdomain,
  1280. intersected_rd);
  1281. kfree(country_ie_regdomain);
  1282. country_ie_regdomain = NULL;
  1283. } else {
  1284. /* This would happen when CRDA was not present and
  1285. * OLD_REGULATORY was enabled. We intersect our Country
  1286. * IE rd and what was set on cfg80211 originally */
  1287. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1288. }
  1289. if (!intersected_rd)
  1290. return -EINVAL;
  1291. drv = wiphy_to_dev(wiphy);
  1292. drv->country_ie_alpha2[0] = rd->alpha2[0];
  1293. drv->country_ie_alpha2[1] = rd->alpha2[1];
  1294. drv->env = last_request->country_ie_env;
  1295. BUG_ON(intersected_rd == rd);
  1296. kfree(rd);
  1297. rd = NULL;
  1298. reset_regdomains();
  1299. cfg80211_regdomain = intersected_rd;
  1300. return 0;
  1301. }
  1302. /* Use this call to set the current regulatory domain. Conflicts with
  1303. * multiple drivers can be ironed out later. Caller must've already
  1304. * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
  1305. int set_regdom(const struct ieee80211_regdomain *rd)
  1306. {
  1307. int r;
  1308. /* Note that this doesn't update the wiphys, this is done below */
  1309. r = __set_regdom(rd);
  1310. if (r) {
  1311. kfree(rd);
  1312. return r;
  1313. }
  1314. /* This would make this whole thing pointless */
  1315. if (!last_request->intersect)
  1316. BUG_ON(rd != cfg80211_regdomain);
  1317. /* update all wiphys now with the new established regulatory domain */
  1318. update_all_wiphy_regulatory(last_request->initiator);
  1319. print_regdomain(cfg80211_regdomain);
  1320. return r;
  1321. }
  1322. /* Caller must hold cfg80211_drv_mutex */
  1323. void reg_device_remove(struct wiphy *wiphy)
  1324. {
  1325. kfree(wiphy->regd);
  1326. if (!last_request || !last_request->wiphy)
  1327. return;
  1328. if (last_request->wiphy != wiphy)
  1329. return;
  1330. last_request->wiphy = NULL;
  1331. last_request->country_ie_env = ENVIRON_ANY;
  1332. }
  1333. int regulatory_init(void)
  1334. {
  1335. int err;
  1336. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1337. if (IS_ERR(reg_pdev))
  1338. return PTR_ERR(reg_pdev);
  1339. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  1340. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  1341. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  1342. print_regdomain_info(cfg80211_regdomain);
  1343. /* The old code still requests for a new regdomain and if
  1344. * you have CRDA you get it updated, otherwise you get
  1345. * stuck with the static values. We ignore "EU" code as
  1346. * that is not a valid ISO / IEC 3166 alpha2 */
  1347. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  1348. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  1349. ieee80211_regdom, 0, ENVIRON_ANY);
  1350. #else
  1351. cfg80211_regdomain = cfg80211_world_regdom;
  1352. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
  1353. if (err)
  1354. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  1355. "unable to update world regulatory domain, "
  1356. "using static definition\n");
  1357. #endif
  1358. return 0;
  1359. }
  1360. void regulatory_exit(void)
  1361. {
  1362. mutex_lock(&cfg80211_drv_mutex);
  1363. reset_regdomains();
  1364. kfree(country_ie_regdomain);
  1365. country_ie_regdomain = NULL;
  1366. kfree(last_request);
  1367. platform_device_unregister(reg_pdev);
  1368. mutex_unlock(&cfg80211_drv_mutex);
  1369. }