reg.c 45 KB

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