reg.c 46 KB

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