regulatory.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =======================================
  3. Linux wireless regulatory documentation
  4. =======================================
  5. This document gives a brief review over how the Linux wireless
  6. regulatory infrastructure works.
  7. More up to date information can be obtained at the project's web page:
  8. https://wireless.wiki.kernel.org/en/developers/Regulatory
  9. Keeping regulatory domains in userspace
  10. ---------------------------------------
  11. Due to the dynamic nature of regulatory domains we keep them
  12. in userspace and provide a framework for userspace to upload
  13. to the kernel one regulatory domain to be used as the central
  14. core regulatory domain all wireless devices should adhere to.
  15. How to get regulatory domains to the kernel
  16. -------------------------------------------
  17. When the regulatory domain is first set up, the kernel will request a
  18. database file (regulatory.db) containing all the regulatory rules. It
  19. will then use that database when it needs to look up the rules for a
  20. given country.
  21. How to get regulatory domains to the kernel (old CRDA solution)
  22. ---------------------------------------------------------------
  23. Userspace gets a regulatory domain in the kernel by having
  24. a userspace agent build it and send it via nl80211. Only
  25. expected regulatory domains will be respected by the kernel.
  26. A currently available userspace agent which can accomplish this
  27. is CRDA - central regulatory domain agent. Its documented here:
  28. https://wireless.wiki.kernel.org/en/developers/Regulatory/CRDA
  29. Essentially the kernel will send a udev event when it knows
  30. it needs a new regulatory domain. A udev rule can be put in place
  31. to trigger crda to send the respective regulatory domain for a
  32. specific ISO/IEC 3166 alpha2.
  33. Below is an example udev rule which can be used:
  34. # Example file, should be put in /etc/udev/rules.d/regulatory.rules
  35. KERNEL=="regulatory*", ACTION=="change", SUBSYSTEM=="platform", RUN+="/sbin/crda"
  36. The alpha2 is passed as an environment variable under the variable COUNTRY.
  37. Who asks for regulatory domains?
  38. --------------------------------
  39. * Users
  40. Users can use iw:
  41. https://wireless.wiki.kernel.org/en/users/Documentation/iw
  42. An example::
  43. # set regulatory domain to "Costa Rica"
  44. iw reg set CR
  45. This will request the kernel to set the regulatory domain to
  46. the specified alpha2. The kernel in turn will then ask userspace
  47. to provide a regulatory domain for the alpha2 specified by the user
  48. by sending a uevent.
  49. * Wireless subsystems for Country Information elements
  50. The kernel will send a uevent to inform userspace a new
  51. regulatory domain is required. More on this to be added
  52. as its integration is added.
  53. * Drivers
  54. If drivers determine they need a specific regulatory domain
  55. set they can inform the wireless core using regulatory_hint().
  56. They have two options -- they either provide an alpha2 so that
  57. crda can provide back a regulatory domain for that country or
  58. they can build their own regulatory domain based on internal
  59. custom knowledge so the wireless core can respect it.
  60. *Most* drivers will rely on the first mechanism of providing a
  61. regulatory hint with an alpha2. For these drivers there is an additional
  62. check that can be used to ensure compliance based on custom EEPROM
  63. regulatory data. This additional check can be used by drivers by
  64. registering on its struct wiphy a reg_notifier() callback. This notifier
  65. is called when the core's regulatory domain has been changed. The driver
  66. can use this to review the changes made and also review who made them
  67. (driver, user, country IE) and determine what to allow based on its
  68. internal EEPROM data. Devices drivers wishing to be capable of world
  69. roaming should use this callback. More on world roaming will be
  70. added to this document when its support is enabled.
  71. Device drivers who provide their own built regulatory domain
  72. do not need a callback as the channels registered by them are
  73. the only ones that will be allowed and therefore *additional*
  74. channels cannot be enabled.
  75. Example code - drivers hinting an alpha2:
  76. ------------------------------------------
  77. This example comes from the zd1211rw device driver. You can start
  78. by having a mapping of your device's EEPROM country/regulatory
  79. domain value to a specific alpha2 as follows::
  80. static struct zd_reg_alpha2_map reg_alpha2_map[] = {
  81. { ZD_REGDOMAIN_FCC, "US" },
  82. { ZD_REGDOMAIN_IC, "CA" },
  83. { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
  84. { ZD_REGDOMAIN_JAPAN, "JP" },
  85. { ZD_REGDOMAIN_JAPAN_ADD, "JP" },
  86. { ZD_REGDOMAIN_SPAIN, "ES" },
  87. { ZD_REGDOMAIN_FRANCE, "FR" },
  88. Then you can define a routine to map your read EEPROM value to an alpha2,
  89. as follows::
  90. static int zd_reg2alpha2(u8 regdomain, char *alpha2)
  91. {
  92. unsigned int i;
  93. struct zd_reg_alpha2_map *reg_map;
  94. for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
  95. reg_map = &reg_alpha2_map[i];
  96. if (regdomain == reg_map->reg) {
  97. alpha2[0] = reg_map->alpha2[0];
  98. alpha2[1] = reg_map->alpha2[1];
  99. return 0;
  100. }
  101. }
  102. return 1;
  103. }
  104. Lastly, you can then hint to the core of your discovered alpha2, if a match
  105. was found. You need to do this after you have registered your wiphy. You
  106. are expected to do this during initialization.
  107. ::
  108. r = zd_reg2alpha2(mac->regdomain, alpha2);
  109. if (!r)
  110. regulatory_hint(hw->wiphy, alpha2);
  111. Example code - drivers providing a built in regulatory domain:
  112. --------------------------------------------------------------
  113. [NOTE: This API is not currently available, it can be added when required]
  114. If you have regulatory information you can obtain from your
  115. driver and you *need* to use this we let you build a regulatory domain
  116. structure and pass it to the wireless core. To do this you should
  117. kmalloc() a structure big enough to hold your regulatory domain
  118. structure and you should then fill it with your data. Finally you simply
  119. call regulatory_hint() with the regulatory domain structure in it.
  120. Below is a simple example, with a regulatory domain cached using the stack.
  121. Your implementation may vary (read EEPROM cache instead, for example).
  122. Example cache of some regulatory domain::
  123. struct ieee80211_regdomain mydriver_jp_regdom = {
  124. .n_reg_rules = 3,
  125. .alpha2 = "JP",
  126. //.alpha2 = "99", /* If I have no alpha2 to map it to */
  127. .reg_rules = {
  128. /* IEEE 802.11b/g, channels 1..14 */
  129. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  130. /* IEEE 802.11a, channels 34..48 */
  131. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  132. NL80211_RRF_NO_IR),
  133. /* IEEE 802.11a, channels 52..64 */
  134. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  135. NL80211_RRF_NO_IR|
  136. NL80211_RRF_DFS),
  137. }
  138. };
  139. Then in some part of your code after your wiphy has been registered::
  140. struct ieee80211_regdomain *rd;
  141. int size_of_regd;
  142. int num_rules = mydriver_jp_regdom.n_reg_rules;
  143. unsigned int i;
  144. size_of_regd = sizeof(struct ieee80211_regdomain) +
  145. (num_rules * sizeof(struct ieee80211_reg_rule));
  146. rd = kzalloc(size_of_regd, GFP_KERNEL);
  147. if (!rd)
  148. return -ENOMEM;
  149. memcpy(rd, &mydriver_jp_regdom, sizeof(struct ieee80211_regdomain));
  150. for (i=0; i < num_rules; i++)
  151. memcpy(&rd->reg_rules[i],
  152. &mydriver_jp_regdom.reg_rules[i],
  153. sizeof(struct ieee80211_reg_rule));
  154. regulatory_struct_hint(rd);
  155. Statically compiled regulatory database
  156. ---------------------------------------
  157. When a database should be fixed into the kernel, it can be provided as a
  158. firmware file at build time that is then linked into the kernel.