7 #include <arpa/inet.h> /* ntohl */
12 /* Intersects regulatory domains, this will skip any regulatory marked with
13 * an alpha2 of '00', which is used to indicate a regulatory domain */
15 #define BUG_ON(foo) do { \
22 /* Helper for regdom_intersect(), this does the real
23 * mathematical intersection fun */
24 static int reg_rules_intersect(
25 struct ieee80211_reg_rule *rule1,
26 struct ieee80211_reg_rule *rule2,
27 struct ieee80211_reg_rule *intersected_rule)
29 struct ieee80211_freq_range *freq_range1, *freq_range2, *freq_range;
30 struct ieee80211_power_rule *power_rule1, *power_rule2, *power_rule;
33 freq_range1 = &rule1->freq_range;
34 freq_range2 = &rule2->freq_range;
35 freq_range = &intersected_rule->freq_range;
37 power_rule1 = &rule1->power_rule;
38 power_rule2 = &rule2->power_rule;
39 power_rule = &intersected_rule->power_rule;
41 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
42 freq_range2->start_freq_khz);
43 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
44 freq_range2->end_freq_khz);
45 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
46 freq_range2->max_bandwidth_khz);
48 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
49 if (freq_range->max_bandwidth_khz > freq_diff)
50 freq_range->max_bandwidth_khz = freq_diff;
52 power_rule->max_eirp = min(power_rule1->max_eirp,
53 power_rule2->max_eirp);
54 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
55 power_rule2->max_antenna_gain);
57 intersected_rule->flags = (rule1->flags | rule2->flags);
59 if (!is_valid_reg_rule(intersected_rule))
66 * regdom_intersect - do the intersection between two regulatory domains
67 * @rd1: first regulatory domain
68 * @rd2: second regulatory domain
70 * Use this function to get the intersection between two regulatory domains.
71 * Once completed we will mark the alpha2 for the rd as intersected, "98",
72 * as no one single alpha2 can represent this regulatory domain.
74 * Returns a pointer to the regulatory domain structure which will hold the
75 * resulting intersection of rules between rd1 and rd2. We will
76 * malloc() this structure for you.
78 struct ieee80211_regdomain *regdom_intersect(
79 struct ieee80211_regdomain *rd1,
80 struct ieee80211_regdomain *rd2)
84 unsigned int num_rules = 0, rule_idx = 0;
85 struct ieee80211_reg_rule *rule1, *rule2, *intersected_rule;
86 struct ieee80211_regdomain *rd;
87 /* This is just a dummy holder to help us count */
88 struct ieee80211_reg_rule irule;
90 /* Uses the stack temporarily for counter arithmetic */
91 intersected_rule = &irule;
93 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
96 fprintf(stderr, "rd1 or or rd2 is null\n");
100 /* First we get a count of the rules we'll need, then we actually
101 * build them. This is to so we can malloc() and free() a
102 * regdomain once. The reason we use reg_rules_intersect() here
103 * is it will return -EINVAL if the rule computed makes no sense.
104 * All rules that do check out OK are valid. */
106 for (x = 0; x < rd1->n_reg_rules; x++) {
107 rule1 = &rd1->reg_rules[x];
108 for (y = 0; y < rd2->n_reg_rules; y++) {
109 rule2 = &rd2->reg_rules[y];
110 if (!reg_rules_intersect(rule1, rule2,
113 memset(intersected_rule, 0,
114 sizeof(struct ieee80211_reg_rule));
119 fprintf(stderr, "error: num_rules == 0\n");
123 size_of_regd = sizeof(struct ieee80211_regdomain) +
124 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
126 rd = malloc(size_of_regd);
128 fprintf(stderr, "no memory left\n");
132 memset(rd, 0, size_of_regd);
134 for (x = 0; x < rd1->n_reg_rules; x++) {
135 rule1 = &rd1->reg_rules[x];
136 for (y = 0; y < rd2->n_reg_rules; y++) {
137 rule2 = &rd2->reg_rules[y];
138 /* This time around instead of using the stack lets
139 * write to the target rule directly saving ourselves
141 intersected_rule = &rd->reg_rules[rule_idx];
142 r = reg_rules_intersect(rule1, rule2,
150 if (rule_idx != num_rules) {
151 fprintf(stderr, "Error while doing regdom interesection :(\n");
156 rd->n_reg_rules = num_rules;
163 int main(int argc, char **argv)
168 struct regdb_file_header *header;
169 struct regdb_file_reg_country *countries;
170 int dblen, siglen, num_countries, i, r = 0;
171 struct ieee80211_regdomain *prev_world = NULL, *rd = NULL, *world = NULL;
175 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
179 fd = open(argv[1], O_RDONLY);
181 perror("failed to open db file");
185 if (fstat(fd, &stat)) {
186 perror("failed to fstat db file");
190 dblen = stat.st_size;
192 db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
193 if (db == MAP_FAILED) {
194 perror("failed to mmap db file");
198 header = crda_get_file_ptr(db, dblen, sizeof(*header), 0);
200 if (ntohl(header->magic) != REGDB_MAGIC) {
201 fprintf(stderr, "Invalid database magic\n");
205 if (ntohl(header->version) != REGDB_VERSION) {
206 fprintf(stderr, "Invalid database version\n");
210 siglen = ntohl(header->signature_length);
211 /* adjust dblen so later sanity checks don't run into the signature */
214 if (dblen <= (int)sizeof(*header)) {
215 fprintf(stderr, "Invalid signature length %d\n", siglen);
219 /* verify signature */
220 if (!crda_verify_db_signature(db, dblen, siglen))
223 num_countries = ntohl(header->reg_country_num);
225 if (num_countries <= 0)
228 countries = crda_get_file_ptr(db, dblen,
229 sizeof(struct regdb_file_reg_country) * num_countries,
230 header->reg_country_ptr);
232 /* We intersect only when we have to rd structures ready */
233 for (i = 0; i < num_countries; i++) {
234 struct regdb_file_reg_country *country = countries + i;
236 if (is_world_regdom((const char *) country->alpha2))
239 /* Gets the rd for the current country */
240 rd = country2rd(db, dblen, country);
243 fprintf(stderr, "Could not covert country "
244 "(%.2s) to rd\n", country->alpha2);
248 if (num_countries == 1) {
265 world = regdom_intersect(prev_world, rd);
267 /* Could be something else but we'll live with this */
270 fprintf(stderr, "Could not intersect world "
271 "with country (%.2s)\n",
274 fprintf(stderr, "Could not intersect country (%.2s) "
275 "with country (%.2s)\n",
282 /* Use UTF-8 Intersection symbol ? (0xE2,0x88,0xA9) :) */
283 printf("WW (%d) intersect %c%c (%d) ==> %d rules\n",
284 prev_world->n_reg_rules,
290 printf("%c%c (%d) intersect %c%c (%d) ==> %d rules\n",
291 prev_world->alpha2[0],
292 prev_world->alpha2[1],
293 prev_world->n_reg_rules,
302 printf("%d regulatory domains intersected\n", intersected);
304 printf("Only one intersection completed\n");
307 printf("== World regulatory domain: ==\n");
315 if (intersected > 1) {