reg.c 39 KB

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