reglib.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #ifndef REG_LIB_H
  2. #define REG_LIB_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <sys/stat.h>
  8. #include <math.h>
  9. #include "regdb.h"
  10. /* Common regulatory structures, functions and helpers */
  11. /* This matches the kernel's data structures */
  12. struct ieee80211_freq_range {
  13. uint32_t start_freq_khz;
  14. uint32_t end_freq_khz;
  15. uint32_t max_bandwidth_khz;
  16. };
  17. struct ieee80211_power_rule {
  18. uint32_t max_antenna_gain;
  19. uint32_t max_eirp;
  20. };
  21. struct ieee80211_reg_rule {
  22. struct ieee80211_freq_range freq_range;
  23. struct ieee80211_power_rule power_rule;
  24. uint32_t flags;
  25. uint32_t dfs_cac_ms;
  26. };
  27. struct ieee80211_regdomain {
  28. uint32_t n_reg_rules;
  29. char alpha2[2];
  30. uint8_t dfs_region;
  31. struct ieee80211_reg_rule reg_rules[];
  32. };
  33. #define REGLIB_MHZ_TO_KHZ(freq) ((freq) * 1000)
  34. #define REGLIB_KHZ_TO_MHZ(freq) ((freq) / 1000)
  35. #define REGLIB_DBI_TO_MBI(gain) ((gain) * 100)
  36. #define REGLIB_MBI_TO_DBI(gain) ((gain) / 100)
  37. #define REGLIB_DBM_TO_MBM(gain) ((gain) * 100)
  38. #define REGLIB_MBM_TO_DBM(gain) ((gain) / 100)
  39. #define REGLIB_MW_TO_DBM(gain) (10 * log10(gain))
  40. #define REGLIB_MW_TO_MBM(gain) (REGLIB_DBM_TO_MBM(REGLIB_MW_TO_DBM(gain)))
  41. /**
  42. * struct reglib_regdb_ctx - reglib regdb context
  43. *
  44. * This can be used to interat with reglib without
  45. * having to open() / close() / mmap() / munmap()
  46. * and check the regdb binary file for integrity and
  47. * authorship.
  48. *
  49. * @fd: file descriptor of the db
  50. * @stat: @fd fstat()
  51. * @db: mmap() of the db of @real_dblen
  52. * @real_dblen: file size in bytes of @fd
  53. * @siglen: size in bytes of the signature at the end of the file
  54. * @dblen: database lenghth, this is the @real_dblen - @siglen
  55. * @verified: whether or not this regdb has been RSA verified.
  56. * This value is dependent on whether or not you enabled
  57. * signature verification with gcrypt, openssl, or none
  58. * at all. If no signature verification was not compiled
  59. * in then this will always be true otherwise this will
  60. * only be true if the RSA digital signature of the SHA1
  61. * sum of the regulatory database at the end of the
  62. * regulatory database can be verified with the one of
  63. * the trusted public keys.
  64. */
  65. struct reglib_regdb_ctx {
  66. int fd;
  67. struct stat stat;
  68. uint8_t *db;
  69. uint32_t real_dblen;
  70. uint32_t siglen;
  71. uint32_t dblen;
  72. bool verified;
  73. struct regdb_file_header *header;
  74. uint32_t num_countries;
  75. struct regdb_file_reg_country *countries;
  76. };
  77. static inline int reglib_is_world_regdom(const char *alpha2)
  78. {
  79. if (alpha2[0] == '0' && alpha2[1] == '0')
  80. return 1;
  81. return 0;
  82. }
  83. static inline int reglib_isalpha_upper(char letter)
  84. {
  85. if (letter >= 'A' && letter <= 'Z')
  86. return 1;
  87. return 0;
  88. }
  89. static inline int reglib_is_alpha2(const char *alpha2)
  90. {
  91. if (reglib_isalpha_upper(alpha2[0]) && reglib_isalpha_upper(alpha2[1]))
  92. return 1;
  93. return 0;
  94. }
  95. static inline int reglib_is_valid_regdom(const char *alpha2)
  96. {
  97. if (!reglib_is_alpha2(alpha2) && !reglib_is_world_regdom(alpha2))
  98. return 0;
  99. return 1;
  100. }
  101. static inline uint32_t reglib_max(uint32_t a, uint32_t b)
  102. {
  103. return (a > b) ? a : b;
  104. }
  105. static inline uint32_t reglib_min(uint32_t a, uint32_t b)
  106. {
  107. return (a > b) ? b : a;
  108. }
  109. void *
  110. reglib_get_file_ptr(uint8_t *db, size_t dblen, size_t structlen, uint32_t ptr);
  111. int reglib_verify_db_signature(uint8_t *db, size_t dblen, size_t siglen);
  112. /**
  113. * reglib_malloc_regdb_ctx - create a regdb context for usage with reglib
  114. *
  115. * @regdb_file: file name
  116. *
  117. * Most operations on reglib iterate over the database somehow and prior
  118. * to iterating over it it must check the signature. Use this context helper
  119. * to let you query the db within different contexts in your program and
  120. * just be sure to call reglib_free_regdb_ctx() when done. This helper will
  121. * open the file passed and mmap() it.
  122. */
  123. const struct reglib_regdb_ctx *reglib_malloc_regdb_ctx(const char *regdb_file);
  124. /**
  125. * reglib_free_regdb_ctx - free a regdb context used with reglib
  126. *
  127. * @regdb_ctx: the reglib regdb context created with reglib_malloc_regdb_ctx()
  128. *
  129. * This will do all the handy work to close up, munmap, and free the
  130. * reglib regdb context passed.
  131. */
  132. void reglib_free_regdb_ctx(const struct reglib_regdb_ctx *regdb_ctx);
  133. const struct ieee80211_regdomain *
  134. reglib_get_rd_idx(unsigned int idx, const struct reglib_regdb_ctx *ctx);
  135. #define reglib_for_each_country(__rd, __idx, __ctx) \
  136. for (__rd = reglib_get_rd_idx(__idx, __ctx); \
  137. __rd != NULL; \
  138. __rd = reglib_get_rd_idx(++__idx, __ctx)) \
  139. const struct ieee80211_regdomain *
  140. reglib_get_rd_alpha2(const char *alpha2, const char *file);
  141. /**
  142. * reglib_is_valid_rd - validate regulatory domain data structure
  143. *
  144. * @rd: regulatory domain data structure to validate
  145. *
  146. * You can use this to validate regulatory domain data structures
  147. * for possible inconsistencies.
  148. */
  149. int reglib_is_valid_rd(const struct ieee80211_regdomain *rd);
  150. /* reg helpers */
  151. void reglib_print_regdom(const struct ieee80211_regdomain *rd);
  152. struct ieee80211_regdomain *
  153. reglib_intersect_rds(const struct ieee80211_regdomain *rd1,
  154. const struct ieee80211_regdomain *rd2);
  155. /**
  156. * reglib_intersect_regdb - intersects a regulatory database
  157. *
  158. * @regdb_file: the regulatory database to intersect
  159. *
  160. * Goes through an entire regulatory database and intersects all regulatory
  161. * domains. This will skip any regulatory marked with an alpha2 of '00', which
  162. * is used to indicate a world regulatory domain. If intersection is able
  163. * to find rules that fit all regulatory domains it return a regulatory
  164. * domain with such rules otherwise it returns NULL.
  165. */
  166. const struct ieee80211_regdomain *
  167. reglib_intersect_regdb(const struct reglib_regdb_ctx *ctx);
  168. /**
  169. * @reglib_create_parse_stream - provide a clean new stream for processing
  170. *
  171. * @fp: FILE stream, could be stdin, or a stream from an open file.
  172. *
  173. * In order to parse a stream we recommend to create a new stream
  174. * using this helper. A new stream is preferred in order to work
  175. * with stdin, as otherwise we cannot rewind() and move around
  176. * the stream. This helper will create new stream using tmpfile()
  177. * and also remove all comments. It will be closed and the file
  178. * deleted when the process terminates.
  179. */
  180. FILE *reglib_create_parse_stream(FILE *fp);
  181. /**
  182. * @reglib_parse_country - parse stream to build a regulatory domain
  183. *
  184. * @fp: FILE stream, could be stdin, or a stream from an open file.
  185. *
  186. * Parse stream and return back a built regulatory domain. Returns
  187. * NULL if one could not be built.
  188. */
  189. struct ieee80211_regdomain *reglib_parse_country(FILE *fp);
  190. /**
  191. * @reglib_optimize_regdom - optimize a regulatory domain
  192. *
  193. * @rd: a regulatory domain to be optimized
  194. *
  195. * A regulatory domain may exist without optimal expressions
  196. * over its rules. This will look for regulatory rules that can
  197. * be combined together to reduce the size of the regulatory
  198. * domain and its expression.
  199. *
  200. * Regulatory rules will be combined if their max allowed
  201. * bandwidth, max EIRP, and flags all match.
  202. */
  203. struct ieee80211_regdomain *
  204. reglib_optimize_regdom(struct ieee80211_regdomain *rd);
  205. #define reglib_for_each_country_stream(__fp, __rd) \
  206. for (__rd = reglib_parse_country(__fp); \
  207. __rd != NULL; \
  208. __rd = reglib_parse_country(__fp)) \
  209. #endif