2 * Copyright (c) 2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 static const char rcsid[] = "$FreeBSD$";
29 #include <sys/types.h>
30 #include <sys/errno.h>
31 #include <sys/param.h>
45 #include "lib80211_regdomain.h"
47 #include <net80211/_ieee80211.h>
54 struct regdomain *rd; /* current domain */
55 struct netband *netband; /* current netband */
56 struct freqband *freqband; /* current freqband */
57 struct country *country; /* current country */
58 netband_head *curband; /* current netband list */
60 struct sbuf *sbuf[MAXLEVEL];
67 enum { DOMAIN, COUNTRY, FREQBAND } type;
71 start_element(void *data, const char *name, const char **attr)
73 #define iseq(a,b) (strcasecmp(a,b) == 0)
75 const void *id, *ref, *mode;
79 if (++mt->level == MAXLEVEL) {
80 /* XXX force parser to abort */
83 mt->sbuf[mt->level] = sbuf_new_auto();
84 id = ref = mode = NULL;
85 for (i = 0; attr[i] != NULL; i += 2) {
86 if (iseq(attr[i], "id")) {
88 } else if (iseq(attr[i], "ref")) {
90 } else if (iseq(attr[i], "mode")) {
93 printf("%*.*s[%s = %s]\n", mt->level + 1,
94 mt->level + 1, "", attr[i], attr[i+1]);
96 if (iseq(name, "rd") && mt->rd == NULL) {
97 if (mt->country == NULL) {
98 mt->rd = calloc(1, sizeof(struct regdomain));
99 mt->rd->name = strdup(id);
101 LIST_INSERT_HEAD(&mt->rdp->domains, mt->rd, next);
103 mt->country->rd = (void *)strdup(ref);
106 if (iseq(name, "defcc") && mt->rd != NULL) {
107 mt->rd->cc = (void *)strdup(ref);
110 if (iseq(name, "netband") && mt->curband == NULL && mt->rd != NULL) {
112 warnx("no mode for netband at line %ld",
113 XML_GetCurrentLineNumber(mt->parser));
116 if (iseq(mode, "11b"))
117 mt->curband = &mt->rd->bands_11b;
118 else if (iseq(mode, "11g"))
119 mt->curband = &mt->rd->bands_11g;
120 else if (iseq(mode, "11a"))
121 mt->curband = &mt->rd->bands_11a;
122 else if (iseq(mode, "11ng"))
123 mt->curband = &mt->rd->bands_11ng;
124 else if (iseq(mode, "11na"))
125 mt->curband = &mt->rd->bands_11na;
126 else if (iseq(mode, "11ac"))
127 mt->curband = &mt->rd->bands_11ac;
128 else if (iseq(mode, "11acg"))
129 mt->curband = &mt->rd->bands_11acg;
131 warnx("unknown mode \"%s\" at line %ld",
132 __DECONST(char *, mode),
133 XML_GetCurrentLineNumber(mt->parser));
136 if (iseq(name, "band") && mt->netband == NULL) {
137 if (mt->curband == NULL) {
138 warnx("band without enclosing netband at line %ld",
139 XML_GetCurrentLineNumber(mt->parser));
142 mt->netband = calloc(1, sizeof(struct netband));
143 LIST_INSERT_HEAD(mt->curband, mt->netband, next);
146 if (iseq(name, "freqband") && mt->freqband == NULL && mt->netband != NULL) {
147 /* XXX handle inlines and merge into table? */
148 if (mt->netband->band != NULL) {
149 warnx("duplicate freqband at line %ld ignored",
150 XML_GetCurrentLineNumber(mt->parser));
153 mt->netband->band = (void *)strdup(ref);
157 if (iseq(name, "country") && mt->country == NULL) {
158 mt->country = calloc(1, sizeof(struct country));
159 mt->country->isoname = strdup(id);
160 mt->country->code = NO_COUNTRY;
162 LIST_INSERT_HEAD(&mt->rdp->countries, mt->country, next);
166 if (iseq(name, "freqband") && mt->freqband == NULL) {
167 mt->freqband = calloc(1, sizeof(struct freqband));
168 mt->freqband->id = strdup(id);
170 LIST_INSERT_HEAD(&mt->rdp->freqbands, mt->freqband, next);
177 decode_flag(struct mystate *mt, const char *p, int len)
179 #define iseq(a,b) (strcasecmp(a,b) == 0)
180 static const struct {
185 #define FLAG(x) { #x, sizeof(#x)-1, x }
186 FLAG(IEEE80211_CHAN_A),
187 FLAG(IEEE80211_CHAN_B),
188 FLAG(IEEE80211_CHAN_G),
189 FLAG(IEEE80211_CHAN_HT20),
190 FLAG(IEEE80211_CHAN_HT40),
191 FLAG(IEEE80211_CHAN_VHT20),
192 FLAG(IEEE80211_CHAN_VHT40),
193 FLAG(IEEE80211_CHAN_VHT80),
194 FLAG(IEEE80211_CHAN_VHT160),
196 * XXX VHT80P80? This likely should be done by
197 * 80MHz chan logic in net80211 / ifconfig.
199 FLAG(IEEE80211_CHAN_ST),
200 FLAG(IEEE80211_CHAN_TURBO),
201 FLAG(IEEE80211_CHAN_PASSIVE),
202 FLAG(IEEE80211_CHAN_DFS),
203 FLAG(IEEE80211_CHAN_CCK),
204 FLAG(IEEE80211_CHAN_OFDM),
205 FLAG(IEEE80211_CHAN_2GHZ),
206 FLAG(IEEE80211_CHAN_5GHZ),
207 FLAG(IEEE80211_CHAN_DYN),
208 FLAG(IEEE80211_CHAN_GFSK),
209 FLAG(IEEE80211_CHAN_GSM),
210 FLAG(IEEE80211_CHAN_STURBO),
211 FLAG(IEEE80211_CHAN_HALF),
212 FLAG(IEEE80211_CHAN_QUARTER),
213 FLAG(IEEE80211_CHAN_HT40U),
214 FLAG(IEEE80211_CHAN_HT40D),
215 FLAG(IEEE80211_CHAN_4MSXMIT),
216 FLAG(IEEE80211_CHAN_NOADHOC),
217 FLAG(IEEE80211_CHAN_NOHOSTAP),
218 FLAG(IEEE80211_CHAN_11D),
219 FLAG(IEEE80211_CHAN_FHSS),
220 FLAG(IEEE80211_CHAN_PUREG),
221 FLAG(IEEE80211_CHAN_108A),
222 FLAG(IEEE80211_CHAN_108G),
224 { "ECM", 3, REQ_ECM },
225 { "INDOOR", 6, REQ_INDOOR },
226 { "OUTDOOR", 7, REQ_OUTDOOR },
230 for (i = 0; i < nitems(flags); i++)
231 if (len == flags[i].len && iseq(p, flags[i].name))
232 return flags[i].value;
233 warnx("unknown flag \"%.*s\" at line %ld ignored",
234 len, p, XML_GetCurrentLineNumber(mt->parser));
240 end_element(void *data, const char *name)
242 #define iseq(a,b) (strcasecmp(a,b) == 0)
248 sbuf_finish(mt->sbuf[mt->level]);
249 p = sbuf_data(mt->sbuf[mt->level]);
250 len = sbuf_len(mt->sbuf[mt->level]);
252 /* <freqband>...</freqband> */
253 if (iseq(name, "freqstart") && mt->freqband != NULL) {
254 mt->freqband->freqStart = strtoul(p, NULL, 0);
257 if (iseq(name, "freqend") && mt->freqband != NULL) {
258 mt->freqband->freqEnd = strtoul(p, NULL, 0);
261 if (iseq(name, "chanwidth") && mt->freqband != NULL) {
262 mt->freqband->chanWidth = strtoul(p, NULL, 0);
265 if (iseq(name, "chansep") && mt->freqband != NULL) {
266 mt->freqband->chanSep = strtoul(p, NULL, 0);
269 if (iseq(name, "flags")) {
270 if (mt->freqband != NULL)
271 mt->freqband->flags |= decode_flag(mt, p, len);
272 else if (mt->netband != NULL)
273 mt->netband->flags |= decode_flag(mt, p, len);
275 warnx("flags without freqband or netband at line %ld ignored",
276 XML_GetCurrentLineNumber(mt->parser));
282 if (iseq(name, "name") && mt->rd != NULL) {
283 mt->rd->name = strdup(p);
286 if (iseq(name, "sku") && mt->rd != NULL) {
287 mt->rd->sku = strtoul(p, NULL, 0);
290 if (iseq(name, "netband") && mt->rd != NULL) {
295 /* <band> ... </band> */
296 if (iseq(name, "freqband") && mt->netband != NULL) {
297 /* XXX handle inline freqbands */
300 if (iseq(name, "maxpower") && mt->netband != NULL) {
301 mt->netband->maxPower = strtoul(p, NULL, 0);
304 if (iseq(name, "maxpowerdfs") && mt->netband != NULL) {
305 mt->netband->maxPowerDFS = strtoul(p, NULL, 0);
308 if (iseq(name, "maxantgain") && mt->netband != NULL) {
309 mt->netband->maxAntGain = strtoul(p, NULL, 0);
313 /* <country>...</country> */
314 if (iseq(name, "isocc") && mt->country != NULL) {
315 mt->country->code = strtoul(p, NULL, 0);
318 if (iseq(name, "name") && mt->country != NULL) {
319 mt->country->name = strdup(p);
324 warnx("unexpected XML token \"%s\" data \"%s\" at line %ld",
325 name, p, XML_GetCurrentLineNumber(mt->parser));
329 if (iseq(name, "freqband") && mt->freqband != NULL) {
330 /* XXX must have start/end frequencies */
331 /* XXX must have channel width/sep */
336 if (iseq(name, "rd") && mt->rd != NULL) {
341 if (iseq(name, "band") && mt->netband != NULL) {
342 if (mt->netband->band == NULL) {
343 warnx("no freqbands for band at line %ld",
344 XML_GetCurrentLineNumber(mt->parser));
346 if (mt->netband->maxPower == 0) {
347 warnx("no maxpower for band at line %ld",
348 XML_GetCurrentLineNumber(mt->parser));
350 /* default max power w/ DFS to max power */
351 if (mt->netband->maxPowerDFS == 0)
352 mt->netband->maxPowerDFS = mt->netband->maxPower;
357 if (iseq(name, "netband") && mt->netband != NULL) {
362 if (iseq(name, "country") && mt->country != NULL) {
363 /* XXX NO_COUNTRY should be in the net80211 country enum */
364 if ((int) mt->country->code == NO_COUNTRY) {
365 warnx("no ISO cc for country at line %ld",
366 XML_GetCurrentLineNumber(mt->parser));
368 if (mt->country->name == NULL) {
369 warnx("no name for country at line %ld",
370 XML_GetCurrentLineNumber(mt->parser));
372 if (mt->country->rd == NULL) {
373 warnx("no regdomain reference for country at line %ld",
374 XML_GetCurrentLineNumber(mt->parser));
380 sbuf_delete(mt->sbuf[mt->level]);
381 mt->sbuf[mt->level--] = NULL;
386 char_data(void *data, const XML_Char *s, int len)
395 for (; isspace(*b) && b < e; b++)
397 for (; isspace(*e) && e > b; e++)
399 if (e != b || (*b != '\0' && !isspace(*b)))
400 sbuf_bcat(mt->sbuf[mt->level], b, e-b+1);
404 findid(struct regdata *rdp, const void *id, int type)
408 for (ip = rdp->ident; ip->id != NULL; ip++)
409 if ((int) ip->type == type && strcasecmp(ip->id, id) == 0)
415 * Parse an regdomain XML configuration and build the internal representation.
418 lib80211_regdomain_readconfig(struct regdata *rdp, const void *p, size_t len)
421 struct regdomain *dp;
428 memset(rdp, 0, sizeof(struct regdata));
429 mt = calloc(1, sizeof(struct mystate));
432 /* parse the XML input */
434 mt->parser = XML_ParserCreate(NULL);
435 XML_SetUserData(mt->parser, mt);
436 XML_SetElementHandler(mt->parser, start_element, end_element);
437 XML_SetCharacterDataHandler(mt->parser, char_data);
438 if (XML_Parse(mt->parser, p, len, 1) != XML_STATUS_OK) {
439 warnx("%s: %s at line %ld", __func__,
440 XML_ErrorString(XML_GetErrorCode(mt->parser)),
441 XML_GetCurrentLineNumber(mt->parser));
444 XML_ParserFree(mt->parser);
446 /* setup the identifer table */
447 rdp->ident = calloc(sizeof(struct ident), mt->nident + 1);
448 if (rdp->ident == NULL)
454 LIST_FOREACH(dp, &rdp->domains, next) {
455 rdp->ident[i].id = dp->name;
456 rdp->ident[i].p = dp;
457 rdp->ident[i].type = DOMAIN;
460 LIST_FOREACH(fp, &rdp->freqbands, next) {
461 rdp->ident[i].id = fp->id;
462 rdp->ident[i].p = fp;
463 rdp->ident[i].type = FREQBAND;
466 LIST_FOREACH(cp, &rdp->countries, next) {
467 rdp->ident[i].id = cp->isoname;
468 rdp->ident[i].p = cp;
469 rdp->ident[i].type = COUNTRY;
473 /* patch references */
474 LIST_FOREACH(dp, &rdp->domains, next) {
475 if (dp->cc != NULL) {
477 dp->cc = findid(rdp, id, COUNTRY);
478 if (dp->cc == NULL) {
479 warnx("undefined country \"%s\"",
480 __DECONST(char *, id));
483 free(__DECONST(char *, id));
485 LIST_FOREACH(nb, &dp->bands_11b, next) {
486 id = findid(rdp, nb->band, FREQBAND);
488 warnx("undefined 11b band \"%s\"",
489 __DECONST(char *, nb->band));
494 LIST_FOREACH(nb, &dp->bands_11g, next) {
495 id = findid(rdp, nb->band, FREQBAND);
497 warnx("undefined 11g band \"%s\"",
498 __DECONST(char *, nb->band));
503 LIST_FOREACH(nb, &dp->bands_11a, next) {
504 id = findid(rdp, nb->band, FREQBAND);
506 warnx("undefined 11a band \"%s\"",
507 __DECONST(char *, nb->band));
512 LIST_FOREACH(nb, &dp->bands_11ng, next) {
513 id = findid(rdp, nb->band, FREQBAND);
515 warnx("undefined 11ng band \"%s\"",
516 __DECONST(char *, nb->band));
521 LIST_FOREACH(nb, &dp->bands_11na, next) {
522 id = findid(rdp, nb->band, FREQBAND);
524 warnx("undefined 11na band \"%s\"",
525 __DECONST(char *, nb->band));
530 LIST_FOREACH(nb, &dp->bands_11ac, next) {
531 id = findid(rdp, nb->band, FREQBAND);
533 warnx("undefined 11ac band \"%s\"",
534 __DECONST(char *, nb->band));
539 LIST_FOREACH(nb, &dp->bands_11acg, next) {
540 id = findid(rdp, nb->band, FREQBAND);
542 warnx("undefined 11acg band \"%s\"",
543 __DECONST(char *, nb->band));
549 LIST_FOREACH(cp, &rdp->countries, next) {
551 cp->rd = findid(rdp, id, DOMAIN);
552 if (cp->rd == NULL) {
553 warnx("undefined country \"%s\"",
554 __DECONST(char *, id));
557 free(__DECONST(char *, id));
560 return errors ? EINVAL : 0;
564 cleanup_bands(netband_head *head)
569 nb = LIST_FIRST(head);
572 LIST_REMOVE(nb, next);
578 * Cleanup state/resources for a previously parsed regdomain database.
581 lib80211_regdomain_cleanup(struct regdata *rdp)
587 struct regdomain *dp = LIST_FIRST(&rdp->domains);
590 LIST_REMOVE(dp, next);
591 cleanup_bands(&dp->bands_11b);
592 cleanup_bands(&dp->bands_11g);
593 cleanup_bands(&dp->bands_11a);
594 cleanup_bands(&dp->bands_11ng);
595 cleanup_bands(&dp->bands_11na);
596 cleanup_bands(&dp->bands_11ac);
597 cleanup_bands(&dp->bands_11acg);
598 if (dp->name != NULL)
599 free(__DECONST(char *, dp->name));
602 struct country *cp = LIST_FIRST(&rdp->countries);
605 LIST_REMOVE(cp, next);
606 if (cp->name != NULL)
607 free(__DECONST(char *, cp->name));
611 struct freqband *fp = LIST_FIRST(&rdp->freqbands);
614 LIST_REMOVE(fp, next);
620 lib80211_alloc_regdata(void)
627 rdp = calloc(1, sizeof(struct regdata));
629 fd = open(_PATH_REGDOMAIN, O_RDONLY);
632 warn("%s: open(%s)", __func__, _PATH_REGDOMAIN);
637 if (fstat(fd, &sb) < 0) {
639 warn("%s: fstat(%s)", __func__, _PATH_REGDOMAIN);
645 xml = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
646 if (xml == MAP_FAILED) {
648 warn("%s: mmap", __func__);
654 if (lib80211_regdomain_readconfig(rdp, xml, sb.st_size) != 0) {
656 warn("%s: error reading regulatory database", __func__);
658 munmap(xml, sb.st_size);
663 munmap(xml, sb.st_size);
670 lib80211_free_regdata(struct regdata *rdp)
672 lib80211_regdomain_cleanup(rdp);
677 * Lookup a regdomain by SKU.
679 const struct regdomain *
680 lib80211_regdomain_findbysku(const struct regdata *rdp, enum RegdomainCode sku)
682 const struct regdomain *dp;
684 LIST_FOREACH(dp, &rdp->domains, next) {
692 * Lookup a regdomain by name.
694 const struct regdomain *
695 lib80211_regdomain_findbyname(const struct regdata *rdp, const char *name)
697 const struct regdomain *dp;
699 LIST_FOREACH(dp, &rdp->domains, next) {
700 if (strcasecmp(dp->name, name) == 0)
707 * Lookup a country by ISO country code.
709 const struct country *
710 lib80211_country_findbycc(const struct regdata *rdp, enum ISOCountryCode cc)
712 const struct country *cp;
714 LIST_FOREACH(cp, &rdp->countries, next) {
722 * Lookup a country by ISO/long name.
724 const struct country *
725 lib80211_country_findbyname(const struct regdata *rdp, const char *name)
727 const struct country *cp;
731 LIST_FOREACH(cp, &rdp->countries, next) {
732 if (strcasecmp(cp->isoname, name) == 0)
735 LIST_FOREACH(cp, &rdp->countries, next) {
736 if (strncasecmp(cp->name, name, len) == 0)