reg.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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 or a user wants to help complaince further */
  661. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
  662. last_request->initiator != REGDOM_SET_BY_USER &&
  663. wiphy->regd)
  664. regd = wiphy->regd;
  665. if (!regd)
  666. return -EINVAL;
  667. for (i = 0; i < regd->n_reg_rules; i++) {
  668. const struct ieee80211_reg_rule *rr;
  669. const struct ieee80211_freq_range *fr = NULL;
  670. const struct ieee80211_power_rule *pr = NULL;
  671. rr = &regd->reg_rules[i];
  672. fr = &rr->freq_range;
  673. pr = &rr->power_rule;
  674. /* We only need to know if one frequency rule was
  675. * was in center_freq's band, that's enough, so lets
  676. * not overwrite it once found */
  677. if (!band_rule_found)
  678. band_rule_found = freq_in_rule_band(fr, center_freq);
  679. max_bandwidth = freq_max_bandwidth(fr, center_freq);
  680. if (max_bandwidth && *bandwidth <= max_bandwidth) {
  681. *reg_rule = rr;
  682. *bandwidth = max_bandwidth;
  683. break;
  684. }
  685. }
  686. if (!band_rule_found)
  687. return -ERANGE;
  688. return !max_bandwidth;
  689. }
  690. EXPORT_SYMBOL(freq_reg_info);
  691. int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
  692. const struct ieee80211_reg_rule **reg_rule)
  693. {
  694. return freq_reg_info_regd(wiphy, center_freq,
  695. bandwidth, reg_rule, NULL);
  696. }
  697. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  698. unsigned int chan_idx)
  699. {
  700. int r;
  701. u32 flags;
  702. u32 max_bandwidth = 0;
  703. const struct ieee80211_reg_rule *reg_rule = NULL;
  704. const struct ieee80211_power_rule *power_rule = NULL;
  705. struct ieee80211_supported_band *sband;
  706. struct ieee80211_channel *chan;
  707. sband = wiphy->bands[band];
  708. BUG_ON(chan_idx >= sband->n_channels);
  709. chan = &sband->channels[chan_idx];
  710. flags = chan->orig_flags;
  711. r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
  712. &max_bandwidth, &reg_rule);
  713. if (r) {
  714. /* This means no regulatory rule was found in the country IE
  715. * with a frequency range on the center_freq's band, since
  716. * IEEE-802.11 allows for a country IE to have a subset of the
  717. * regulatory information provided in a country we ignore
  718. * disabling the channel unless at least one reg rule was
  719. * found on the center_freq's band. For details see this
  720. * clarification:
  721. *
  722. * http://tinyurl.com/11d-clarification
  723. */
  724. if (r == -ERANGE &&
  725. last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  726. #ifdef CONFIG_CFG80211_REG_DEBUG
  727. printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
  728. "intact on %s - no rule found in band on "
  729. "Country IE\n",
  730. chan->center_freq, wiphy_name(wiphy));
  731. #endif
  732. } else {
  733. /* In this case we know the country IE has at least one reg rule
  734. * for the band so we respect its band definitions */
  735. #ifdef CONFIG_CFG80211_REG_DEBUG
  736. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  737. printk(KERN_DEBUG "cfg80211: Disabling "
  738. "channel %d MHz on %s due to "
  739. "Country IE\n",
  740. chan->center_freq, wiphy_name(wiphy));
  741. #endif
  742. flags |= IEEE80211_CHAN_DISABLED;
  743. chan->flags = flags;
  744. }
  745. return;
  746. }
  747. power_rule = &reg_rule->power_rule;
  748. if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
  749. last_request->wiphy && last_request->wiphy == wiphy &&
  750. last_request->wiphy->strict_regulatory) {
  751. /* This gaurantees the driver's requested regulatory domain
  752. * will always be used as a base for further regulatory
  753. * settings */
  754. chan->flags = chan->orig_flags =
  755. map_regdom_flags(reg_rule->flags);
  756. chan->max_antenna_gain = chan->orig_mag =
  757. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  758. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  759. chan->max_power = chan->orig_mpwr =
  760. (int) MBM_TO_DBM(power_rule->max_eirp);
  761. return;
  762. }
  763. chan->flags = flags | map_regdom_flags(reg_rule->flags);
  764. chan->max_antenna_gain = min(chan->orig_mag,
  765. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  766. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  767. if (chan->orig_mpwr)
  768. chan->max_power = min(chan->orig_mpwr,
  769. (int) MBM_TO_DBM(power_rule->max_eirp));
  770. else
  771. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  772. }
  773. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  774. {
  775. unsigned int i;
  776. struct ieee80211_supported_band *sband;
  777. BUG_ON(!wiphy->bands[band]);
  778. sband = wiphy->bands[band];
  779. for (i = 0; i < sband->n_channels; i++)
  780. handle_channel(wiphy, band, i);
  781. }
  782. static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
  783. {
  784. if (!last_request)
  785. return true;
  786. if (setby == REGDOM_SET_BY_CORE &&
  787. wiphy->custom_regulatory)
  788. return true;
  789. /* wiphy->regd will be set once the device has its own
  790. * desired regulatory domain set */
  791. if (wiphy->strict_regulatory && !wiphy->regd &&
  792. !is_world_regdom(last_request->alpha2))
  793. return true;
  794. return false;
  795. }
  796. static void update_all_wiphy_regulatory(enum reg_set_by setby)
  797. {
  798. struct cfg80211_registered_device *drv;
  799. list_for_each_entry(drv, &cfg80211_drv_list, list)
  800. wiphy_update_regulatory(&drv->wiphy, setby);
  801. }
  802. void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
  803. {
  804. enum ieee80211_band band;
  805. if (ignore_reg_update(wiphy, setby))
  806. return;
  807. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  808. if (wiphy->bands[band])
  809. handle_band(wiphy, band);
  810. }
  811. if (wiphy->reg_notifier)
  812. wiphy->reg_notifier(wiphy, last_request);
  813. }
  814. static void handle_channel_custom(struct wiphy *wiphy,
  815. enum ieee80211_band band,
  816. unsigned int chan_idx,
  817. const struct ieee80211_regdomain *regd)
  818. {
  819. int r;
  820. u32 max_bandwidth = 0;
  821. const struct ieee80211_reg_rule *reg_rule = NULL;
  822. const struct ieee80211_power_rule *power_rule = NULL;
  823. struct ieee80211_supported_band *sband;
  824. struct ieee80211_channel *chan;
  825. sband = wiphy->bands[band];
  826. BUG_ON(chan_idx >= sband->n_channels);
  827. chan = &sband->channels[chan_idx];
  828. r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  829. &max_bandwidth, &reg_rule, regd);
  830. if (r) {
  831. chan->flags = IEEE80211_CHAN_DISABLED;
  832. return;
  833. }
  834. power_rule = &reg_rule->power_rule;
  835. chan->flags |= map_regdom_flags(reg_rule->flags);
  836. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  837. chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
  838. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  839. }
  840. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  841. const struct ieee80211_regdomain *regd)
  842. {
  843. unsigned int i;
  844. struct ieee80211_supported_band *sband;
  845. BUG_ON(!wiphy->bands[band]);
  846. sband = wiphy->bands[band];
  847. for (i = 0; i < sband->n_channels; i++)
  848. handle_channel_custom(wiphy, band, i, regd);
  849. }
  850. /* Used by drivers prior to wiphy registration */
  851. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  852. const struct ieee80211_regdomain *regd)
  853. {
  854. enum ieee80211_band band;
  855. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  856. if (wiphy->bands[band])
  857. handle_band_custom(wiphy, band, regd);
  858. }
  859. }
  860. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  861. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  862. const struct ieee80211_regdomain *src_regd)
  863. {
  864. struct ieee80211_regdomain *regd;
  865. int size_of_regd = 0;
  866. unsigned int i;
  867. size_of_regd = sizeof(struct ieee80211_regdomain) +
  868. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  869. regd = kzalloc(size_of_regd, GFP_KERNEL);
  870. if (!regd)
  871. return -ENOMEM;
  872. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  873. for (i = 0; i < src_regd->n_reg_rules; i++)
  874. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  875. sizeof(struct ieee80211_reg_rule));
  876. *dst_regd = regd;
  877. return 0;
  878. }
  879. /* Return value which can be used by ignore_request() to indicate
  880. * it has been determined we should intersect two regulatory domains */
  881. #define REG_INTERSECT 1
  882. /* This has the logic which determines when a new request
  883. * should be ignored. */
  884. static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
  885. const char *alpha2)
  886. {
  887. /* All initial requests are respected */
  888. if (!last_request)
  889. return 0;
  890. switch (set_by) {
  891. case REGDOM_SET_BY_INIT:
  892. return -EINVAL;
  893. case REGDOM_SET_BY_CORE:
  894. /*
  895. * Always respect new wireless core hints, should only happen
  896. * when updating the world regulatory domain at init.
  897. */
  898. return 0;
  899. case REGDOM_SET_BY_COUNTRY_IE:
  900. if (unlikely(!is_an_alpha2(alpha2)))
  901. return -EINVAL;
  902. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  903. if (last_request->wiphy != wiphy) {
  904. /*
  905. * Two cards with two APs claiming different
  906. * different Country IE alpha2s. We could
  907. * intersect them, but that seems unlikely
  908. * to be correct. Reject second one for now.
  909. */
  910. if (!alpha2_equal(alpha2,
  911. cfg80211_regdomain->alpha2))
  912. return -EOPNOTSUPP;
  913. return -EALREADY;
  914. }
  915. /* Two consecutive Country IE hints on the same wiphy.
  916. * This should be picked up early by the driver/stack */
  917. if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
  918. alpha2)))
  919. return 0;
  920. return -EALREADY;
  921. }
  922. return REG_INTERSECT;
  923. case REGDOM_SET_BY_DRIVER:
  924. if (last_request->initiator == REGDOM_SET_BY_CORE) {
  925. if (is_old_static_regdom(cfg80211_regdomain))
  926. return 0;
  927. if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  928. return 0;
  929. return -EALREADY;
  930. }
  931. return REG_INTERSECT;
  932. case REGDOM_SET_BY_USER:
  933. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
  934. return REG_INTERSECT;
  935. /* If the user knows better the user should set the regdom
  936. * to their country before the IE is picked up */
  937. if (last_request->initiator == REGDOM_SET_BY_USER &&
  938. last_request->intersect)
  939. return -EOPNOTSUPP;
  940. /* Process user requests only after previous user/driver/core
  941. * requests have been processed */
  942. if (last_request->initiator == REGDOM_SET_BY_CORE ||
  943. last_request->initiator == REGDOM_SET_BY_DRIVER ||
  944. last_request->initiator == REGDOM_SET_BY_USER) {
  945. if (!alpha2_equal(last_request->alpha2,
  946. cfg80211_regdomain->alpha2))
  947. return -EAGAIN;
  948. }
  949. if (!is_old_static_regdom(cfg80211_regdomain) &&
  950. alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  951. return -EALREADY;
  952. return 0;
  953. }
  954. return -EINVAL;
  955. }
  956. /* Caller must hold &cfg80211_drv_mutex */
  957. int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
  958. const char *alpha2,
  959. u32 country_ie_checksum,
  960. enum environment_cap env)
  961. {
  962. struct regulatory_request *request;
  963. bool intersect = false;
  964. int r = 0;
  965. r = ignore_request(wiphy, set_by, alpha2);
  966. if (r == REG_INTERSECT) {
  967. if (set_by == REGDOM_SET_BY_DRIVER) {
  968. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  969. if (r)
  970. return r;
  971. }
  972. intersect = true;
  973. } else if (r) {
  974. /* If the regulatory domain being requested by the
  975. * driver has already been set just copy it to the
  976. * wiphy */
  977. if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
  978. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  979. if (r)
  980. return r;
  981. r = -EALREADY;
  982. goto new_request;
  983. }
  984. return r;
  985. }
  986. new_request:
  987. request = kzalloc(sizeof(struct regulatory_request),
  988. GFP_KERNEL);
  989. if (!request)
  990. return -ENOMEM;
  991. request->alpha2[0] = alpha2[0];
  992. request->alpha2[1] = alpha2[1];
  993. request->initiator = set_by;
  994. request->wiphy = wiphy;
  995. request->intersect = intersect;
  996. request->country_ie_checksum = country_ie_checksum;
  997. request->country_ie_env = env;
  998. kfree(last_request);
  999. last_request = request;
  1000. /* When r == REG_INTERSECT we do need to call CRDA */
  1001. if (r < 0)
  1002. return r;
  1003. /*
  1004. * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
  1005. * AND if CRDA is NOT present nothing will happen, if someone
  1006. * wants to bother with 11d with OLD_REG you can add a timer.
  1007. * If after x amount of time nothing happens you can call:
  1008. *
  1009. * return set_regdom(country_ie_regdomain);
  1010. *
  1011. * to intersect with the static rd
  1012. */
  1013. return call_crda(alpha2);
  1014. }
  1015. void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1016. {
  1017. int r;
  1018. BUG_ON(!alpha2);
  1019. mutex_lock(&cfg80211_drv_mutex);
  1020. r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER,
  1021. alpha2, 0, ENVIRON_ANY);
  1022. /* This is required so that the orig_* parameters are saved */
  1023. if (r == -EALREADY && wiphy->strict_regulatory)
  1024. wiphy_update_regulatory(wiphy, REGDOM_SET_BY_DRIVER);
  1025. mutex_unlock(&cfg80211_drv_mutex);
  1026. }
  1027. EXPORT_SYMBOL(regulatory_hint);
  1028. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1029. u32 country_ie_checksum)
  1030. {
  1031. if (!last_request->wiphy)
  1032. return false;
  1033. if (likely(last_request->wiphy != wiphy))
  1034. return !country_ie_integrity_changes(country_ie_checksum);
  1035. /* We should not have let these through at this point, they
  1036. * should have been picked up earlier by the first alpha2 check
  1037. * on the device */
  1038. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1039. return true;
  1040. return false;
  1041. }
  1042. void regulatory_hint_11d(struct wiphy *wiphy,
  1043. u8 *country_ie,
  1044. u8 country_ie_len)
  1045. {
  1046. struct ieee80211_regdomain *rd = NULL;
  1047. char alpha2[2];
  1048. u32 checksum = 0;
  1049. enum environment_cap env = ENVIRON_ANY;
  1050. if (!last_request)
  1051. return;
  1052. mutex_lock(&cfg80211_drv_mutex);
  1053. /* IE len must be evenly divisible by 2 */
  1054. if (country_ie_len & 0x01)
  1055. goto out;
  1056. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1057. goto out;
  1058. /* Pending country IE processing, this can happen after we
  1059. * call CRDA and wait for a response if a beacon was received before
  1060. * we were able to process the last regulatory_hint_11d() call */
  1061. if (country_ie_regdomain)
  1062. goto out;
  1063. alpha2[0] = country_ie[0];
  1064. alpha2[1] = country_ie[1];
  1065. if (country_ie[2] == 'I')
  1066. env = ENVIRON_INDOOR;
  1067. else if (country_ie[2] == 'O')
  1068. env = ENVIRON_OUTDOOR;
  1069. /* We will run this for *every* beacon processed for the BSSID, so
  1070. * we optimize an early check to exit out early if we don't have to
  1071. * do anything */
  1072. if (likely(last_request->wiphy)) {
  1073. struct cfg80211_registered_device *drv_last_ie;
  1074. drv_last_ie = wiphy_to_dev(last_request->wiphy);
  1075. /* Lets keep this simple -- we trust the first AP
  1076. * after we intersect with CRDA */
  1077. if (likely(last_request->wiphy == wiphy)) {
  1078. /* Ignore IEs coming in on this wiphy with
  1079. * the same alpha2 and environment cap */
  1080. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1081. alpha2) &&
  1082. env == drv_last_ie->env)) {
  1083. goto out;
  1084. }
  1085. /* the wiphy moved on to another BSSID or the AP
  1086. * was reconfigured. XXX: We need to deal with the
  1087. * case where the user suspends and goes to goes
  1088. * to another country, and then gets IEs from an
  1089. * AP with different settings */
  1090. goto out;
  1091. } else {
  1092. /* Ignore IEs coming in on two separate wiphys with
  1093. * the same alpha2 and environment cap */
  1094. if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
  1095. alpha2) &&
  1096. env == drv_last_ie->env)) {
  1097. goto out;
  1098. }
  1099. /* We could potentially intersect though */
  1100. goto out;
  1101. }
  1102. }
  1103. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1104. if (!rd)
  1105. goto out;
  1106. /* This will not happen right now but we leave it here for the
  1107. * the future when we want to add suspend/resume support and having
  1108. * the user move to another country after doing so, or having the user
  1109. * move to another AP. Right now we just trust the first AP. This is why
  1110. * this is marked as likley(). If we hit this before we add this support
  1111. * we want to be informed of it as it would indicate a mistake in the
  1112. * current design */
  1113. if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
  1114. goto out;
  1115. /* We keep this around for when CRDA comes back with a response so
  1116. * we can intersect with that */
  1117. country_ie_regdomain = rd;
  1118. __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
  1119. country_ie_regdomain->alpha2, checksum, env);
  1120. out:
  1121. mutex_unlock(&cfg80211_drv_mutex);
  1122. }
  1123. EXPORT_SYMBOL(regulatory_hint_11d);
  1124. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1125. {
  1126. unsigned int i;
  1127. const struct ieee80211_reg_rule *reg_rule = NULL;
  1128. const struct ieee80211_freq_range *freq_range = NULL;
  1129. const struct ieee80211_power_rule *power_rule = NULL;
  1130. printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
  1131. "(max_antenna_gain, max_eirp)\n");
  1132. for (i = 0; i < rd->n_reg_rules; i++) {
  1133. reg_rule = &rd->reg_rules[i];
  1134. freq_range = &reg_rule->freq_range;
  1135. power_rule = &reg_rule->power_rule;
  1136. /* There may not be documentation for max antenna gain
  1137. * in certain regions */
  1138. if (power_rule->max_antenna_gain)
  1139. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1140. "(%d mBi, %d mBm)\n",
  1141. freq_range->start_freq_khz,
  1142. freq_range->end_freq_khz,
  1143. freq_range->max_bandwidth_khz,
  1144. power_rule->max_antenna_gain,
  1145. power_rule->max_eirp);
  1146. else
  1147. printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
  1148. "(N/A, %d mBm)\n",
  1149. freq_range->start_freq_khz,
  1150. freq_range->end_freq_khz,
  1151. freq_range->max_bandwidth_khz,
  1152. power_rule->max_eirp);
  1153. }
  1154. }
  1155. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1156. {
  1157. if (is_intersected_alpha2(rd->alpha2)) {
  1158. struct wiphy *wiphy = NULL;
  1159. struct cfg80211_registered_device *drv;
  1160. if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
  1161. if (last_request->wiphy) {
  1162. wiphy = last_request->wiphy;
  1163. drv = wiphy_to_dev(wiphy);
  1164. printk(KERN_INFO "cfg80211: Current regulatory "
  1165. "domain updated by AP to: %c%c\n",
  1166. drv->country_ie_alpha2[0],
  1167. drv->country_ie_alpha2[1]);
  1168. } else
  1169. printk(KERN_INFO "cfg80211: Current regulatory "
  1170. "domain intersected: \n");
  1171. } else
  1172. printk(KERN_INFO "cfg80211: Current regulatory "
  1173. "domain intersected: \n");
  1174. } else if (is_world_regdom(rd->alpha2))
  1175. printk(KERN_INFO "cfg80211: World regulatory "
  1176. "domain updated:\n");
  1177. else {
  1178. if (is_unknown_alpha2(rd->alpha2))
  1179. printk(KERN_INFO "cfg80211: Regulatory domain "
  1180. "changed to driver built-in settings "
  1181. "(unknown country)\n");
  1182. else
  1183. printk(KERN_INFO "cfg80211: Regulatory domain "
  1184. "changed to country: %c%c\n",
  1185. rd->alpha2[0], rd->alpha2[1]);
  1186. }
  1187. print_rd_rules(rd);
  1188. }
  1189. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1190. {
  1191. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1192. rd->alpha2[0], rd->alpha2[1]);
  1193. print_rd_rules(rd);
  1194. }
  1195. #ifdef CONFIG_CFG80211_REG_DEBUG
  1196. static void reg_country_ie_process_debug(
  1197. const struct ieee80211_regdomain *rd,
  1198. const struct ieee80211_regdomain *country_ie_regdomain,
  1199. const struct ieee80211_regdomain *intersected_rd)
  1200. {
  1201. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1202. print_regdomain_info(country_ie_regdomain);
  1203. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1204. print_regdomain_info(rd);
  1205. if (intersected_rd) {
  1206. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1207. "and get:\n");
  1208. print_regdomain_info(rd);
  1209. return;
  1210. }
  1211. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1212. }
  1213. #else
  1214. static inline void reg_country_ie_process_debug(
  1215. const struct ieee80211_regdomain *rd,
  1216. const struct ieee80211_regdomain *country_ie_regdomain,
  1217. const struct ieee80211_regdomain *intersected_rd)
  1218. {
  1219. }
  1220. #endif
  1221. /* Takes ownership of rd only if it doesn't fail */
  1222. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1223. {
  1224. const struct ieee80211_regdomain *intersected_rd = NULL;
  1225. struct cfg80211_registered_device *drv = NULL;
  1226. struct wiphy *wiphy = NULL;
  1227. /* Some basic sanity checks first */
  1228. if (is_world_regdom(rd->alpha2)) {
  1229. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1230. return -EINVAL;
  1231. update_world_regdomain(rd);
  1232. return 0;
  1233. }
  1234. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1235. !is_unknown_alpha2(rd->alpha2))
  1236. return -EINVAL;
  1237. if (!last_request)
  1238. return -EINVAL;
  1239. /* Lets only bother proceeding on the same alpha2 if the current
  1240. * rd is non static (it means CRDA was present and was used last)
  1241. * and the pending request came in from a country IE */
  1242. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1243. /* If someone else asked us to change the rd lets only bother
  1244. * checking if the alpha2 changes if CRDA was already called */
  1245. if (!is_old_static_regdom(cfg80211_regdomain) &&
  1246. !regdom_changed(rd->alpha2))
  1247. return -EINVAL;
  1248. }
  1249. wiphy = last_request->wiphy;
  1250. /* Now lets set the regulatory domain, update all driver channels
  1251. * and finally inform them of what we have done, in case they want
  1252. * to review or adjust their own settings based on their own
  1253. * internal EEPROM data */
  1254. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1255. return -EINVAL;
  1256. if (!is_valid_rd(rd)) {
  1257. printk(KERN_ERR "cfg80211: Invalid "
  1258. "regulatory domain detected:\n");
  1259. print_regdomain_info(rd);
  1260. return -EINVAL;
  1261. }
  1262. if (!last_request->intersect) {
  1263. int r;
  1264. if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
  1265. reset_regdomains();
  1266. cfg80211_regdomain = rd;
  1267. return 0;
  1268. }
  1269. /* For a driver hint, lets copy the regulatory domain the
  1270. * driver wanted to the wiphy to deal with conflicts */
  1271. BUG_ON(last_request->wiphy->regd);
  1272. r = reg_copy_regd(&last_request->wiphy->regd, rd);
  1273. if (r)
  1274. return r;
  1275. reset_regdomains();
  1276. cfg80211_regdomain = rd;
  1277. return 0;
  1278. }
  1279. /* Intersection requires a bit more work */
  1280. if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
  1281. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1282. if (!intersected_rd)
  1283. return -EINVAL;
  1284. /* We can trash what CRDA provided now.
  1285. * However if a driver requested this specific regulatory
  1286. * domain we keep it for its private use */
  1287. if (last_request->initiator == REGDOM_SET_BY_DRIVER)
  1288. last_request->wiphy->regd = rd;
  1289. else
  1290. kfree(rd);
  1291. rd = NULL;
  1292. reset_regdomains();
  1293. cfg80211_regdomain = intersected_rd;
  1294. return 0;
  1295. }
  1296. /*
  1297. * Country IE requests are handled a bit differently, we intersect
  1298. * the country IE rd with what CRDA believes that country should have
  1299. */
  1300. BUG_ON(!country_ie_regdomain);
  1301. if (rd != country_ie_regdomain) {
  1302. /* Intersect what CRDA returned and our what we
  1303. * had built from the Country IE received */
  1304. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1305. reg_country_ie_process_debug(rd, country_ie_regdomain,
  1306. intersected_rd);
  1307. kfree(country_ie_regdomain);
  1308. country_ie_regdomain = NULL;
  1309. } else {
  1310. /* This would happen when CRDA was not present and
  1311. * OLD_REGULATORY was enabled. We intersect our Country
  1312. * IE rd and what was set on cfg80211 originally */
  1313. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1314. }
  1315. if (!intersected_rd)
  1316. return -EINVAL;
  1317. drv = wiphy_to_dev(wiphy);
  1318. drv->country_ie_alpha2[0] = rd->alpha2[0];
  1319. drv->country_ie_alpha2[1] = rd->alpha2[1];
  1320. drv->env = last_request->country_ie_env;
  1321. BUG_ON(intersected_rd == rd);
  1322. kfree(rd);
  1323. rd = NULL;
  1324. reset_regdomains();
  1325. cfg80211_regdomain = intersected_rd;
  1326. return 0;
  1327. }
  1328. /* Use this call to set the current regulatory domain. Conflicts with
  1329. * multiple drivers can be ironed out later. Caller must've already
  1330. * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
  1331. int set_regdom(const struct ieee80211_regdomain *rd)
  1332. {
  1333. int r;
  1334. /* Note that this doesn't update the wiphys, this is done below */
  1335. r = __set_regdom(rd);
  1336. if (r) {
  1337. kfree(rd);
  1338. return r;
  1339. }
  1340. /* This would make this whole thing pointless */
  1341. if (!last_request->intersect)
  1342. BUG_ON(rd != cfg80211_regdomain);
  1343. /* update all wiphys now with the new established regulatory domain */
  1344. update_all_wiphy_regulatory(last_request->initiator);
  1345. print_regdomain(cfg80211_regdomain);
  1346. return r;
  1347. }
  1348. /* Caller must hold cfg80211_drv_mutex */
  1349. void reg_device_remove(struct wiphy *wiphy)
  1350. {
  1351. kfree(wiphy->regd);
  1352. if (!last_request || !last_request->wiphy)
  1353. return;
  1354. if (last_request->wiphy != wiphy)
  1355. return;
  1356. last_request->wiphy = NULL;
  1357. last_request->country_ie_env = ENVIRON_ANY;
  1358. }
  1359. int regulatory_init(void)
  1360. {
  1361. int err;
  1362. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1363. if (IS_ERR(reg_pdev))
  1364. return PTR_ERR(reg_pdev);
  1365. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  1366. cfg80211_regdomain = static_regdom(ieee80211_regdom);
  1367. printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
  1368. print_regdomain_info(cfg80211_regdomain);
  1369. /* The old code still requests for a new regdomain and if
  1370. * you have CRDA you get it updated, otherwise you get
  1371. * stuck with the static values. We ignore "EU" code as
  1372. * that is not a valid ISO / IEC 3166 alpha2 */
  1373. if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
  1374. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  1375. ieee80211_regdom, 0, ENVIRON_ANY);
  1376. #else
  1377. cfg80211_regdomain = cfg80211_world_regdom;
  1378. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
  1379. if (err)
  1380. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  1381. "unable to update world regulatory domain, "
  1382. "using static definition\n");
  1383. #endif
  1384. return 0;
  1385. }
  1386. void regulatory_exit(void)
  1387. {
  1388. mutex_lock(&cfg80211_drv_mutex);
  1389. reset_regdomains();
  1390. kfree(country_ie_regdomain);
  1391. country_ie_regdomain = NULL;
  1392. kfree(last_request);
  1393. platform_device_unregister(reg_pdev);
  1394. mutex_unlock(&cfg80211_drv_mutex);
  1395. }