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.
25 * $FreeBSD: head/sbin/ifconfig/regdomain.c 200587 2009-12-15 20:44:12Z gavin $
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/errno.h>
44 #include "regdomain.h"
46 #include <netproto/802_11/_ieee80211.h>
53 struct regdomain *rd; /* current domain */
54 struct netband *netband; /* current netband */
55 struct freqband *freqband; /* current freqband */
56 struct country *country; /* current country */
57 netband_head *curband; /* current netband list */
59 struct sbuf *sbuf[MAXLEVEL];
64 iseq(const char *a, const char *b)
66 return (strcasecmp(a, b) == 0);
70 start_element(void *data, const char *name, const char **attr)
73 const void *id, *ref, *mode;
77 if (++mt->level == MAXLEVEL) {
78 /* XXX force parser to abort */
81 mt->sbuf[mt->level] = sbuf_new_auto();
82 id = ref = mode = NULL;
83 for (i = 0; attr[i] != NULL; i += 2) {
84 if (iseq(attr[i], "id")) {
86 } else if (iseq(attr[i], "ref")) {
88 } else if (iseq(attr[i], "mode")) {
91 printf("%*.*s[%s = %s]\n", mt->level + 1,
92 mt->level + 1, "", attr[i], attr[i+1]);
94 if (iseq(name, "rd") && mt->rd == NULL) {
95 if (mt->country == NULL) {
96 mt->rd = calloc(1, sizeof(struct regdomain));
97 mt->rd->name = strdup(id);
99 LIST_INSERT_HEAD(&mt->rdp->domains, mt->rd, next);
101 mt->country->rd = (void *)strdup(ref);
104 if (iseq(name, "defcc") && mt->rd != NULL) {
105 mt->rd->cc = (void *)strdup(ref);
108 if (iseq(name, "netband") && mt->curband == NULL && mt->rd != NULL) {
110 warnx("no mode for netband at line %ld",
111 XML_GetCurrentLineNumber(mt->parser));
114 if (iseq(mode, "11b"))
115 mt->curband = &mt->rd->bands_11b;
116 else if (iseq(mode, "11g"))
117 mt->curband = &mt->rd->bands_11g;
118 else if (iseq(mode, "11a"))
119 mt->curband = &mt->rd->bands_11a;
120 else if (iseq(mode, "11ng"))
121 mt->curband = &mt->rd->bands_11ng;
122 else if (iseq(mode, "11na"))
123 mt->curband = &mt->rd->bands_11na;
125 warnx("unknown mode \"%s\" at line %ld",
126 __DECONST(char *, mode),
127 XML_GetCurrentLineNumber(mt->parser));
130 if (iseq(name, "band") && mt->netband == NULL) {
131 if (mt->curband == NULL) {
132 warnx("band without enclosing netband at line %ld",
133 XML_GetCurrentLineNumber(mt->parser));
136 mt->netband = calloc(1, sizeof(struct netband));
137 LIST_INSERT_HEAD(mt->curband, mt->netband, next);
140 if (iseq(name, "freqband") && mt->freqband == NULL && mt->netband != NULL) {
141 /* XXX handle inlines and merge into table? */
142 if (mt->netband->band != NULL) {
143 warnx("duplicate freqband at line %ld ignored",
144 XML_GetCurrentLineNumber(mt->parser));
147 mt->netband->band = (void *)strdup(ref);
151 if (iseq(name, "country") && mt->country == NULL) {
152 mt->country = calloc(1, sizeof(struct country));
153 mt->country->isoname = strdup(id);
154 mt->country->code = NO_COUNTRY;
156 LIST_INSERT_HEAD(&mt->rdp->countries, mt->country, next);
160 if (iseq(name, "freqband") && mt->freqband == NULL) {
161 mt->freqband = calloc(1, sizeof(struct freqband));
162 mt->freqband->id = strdup(id);
164 LIST_INSERT_HEAD(&mt->rdp->freqbands, mt->freqband, next);
170 decode_flag(struct mystate *mt, const char *p, int len)
172 static const struct {
177 #define FLAG(x) { #x, sizeof(#x)-1, x }
178 FLAG(IEEE80211_CHAN_A),
179 FLAG(IEEE80211_CHAN_B),
180 FLAG(IEEE80211_CHAN_G),
181 FLAG(IEEE80211_CHAN_HT20),
182 FLAG(IEEE80211_CHAN_HT40),
183 FLAG(IEEE80211_CHAN_ST),
184 FLAG(IEEE80211_CHAN_TURBO),
185 FLAG(IEEE80211_CHAN_PASSIVE),
186 FLAG(IEEE80211_CHAN_DFS),
187 FLAG(IEEE80211_CHAN_CCK),
188 FLAG(IEEE80211_CHAN_OFDM),
189 FLAG(IEEE80211_CHAN_2GHZ),
190 FLAG(IEEE80211_CHAN_5GHZ),
191 FLAG(IEEE80211_CHAN_DYN),
192 FLAG(IEEE80211_CHAN_GFSK),
193 FLAG(IEEE80211_CHAN_GSM),
194 FLAG(IEEE80211_CHAN_STURBO),
195 FLAG(IEEE80211_CHAN_HALF),
196 FLAG(IEEE80211_CHAN_QUARTER),
197 FLAG(IEEE80211_CHAN_HT40U),
198 FLAG(IEEE80211_CHAN_HT40D),
199 FLAG(IEEE80211_CHAN_4MSXMIT),
200 FLAG(IEEE80211_CHAN_NOADHOC),
201 FLAG(IEEE80211_CHAN_NOHOSTAP),
202 FLAG(IEEE80211_CHAN_11D),
203 FLAG(IEEE80211_CHAN_FHSS),
204 FLAG(IEEE80211_CHAN_PUREG),
205 FLAG(IEEE80211_CHAN_108A),
206 FLAG(IEEE80211_CHAN_108G),
208 { "ECM", 3, REQ_ECM },
209 { "INDOOR", 6, REQ_INDOOR },
210 { "OUTDOOR", 7, REQ_OUTDOOR },
214 for (i = 0; i < nitems(flags); i++)
215 if (len == flags[i].len && iseq(p, flags[i].name))
216 return flags[i].value;
217 warnx("unknown flag \"%.*s\" at line %ld ignored",
218 len, p, XML_GetCurrentLineNumber(mt->parser));
223 end_element(void *data, const char *name)
230 sbuf_finish(mt->sbuf[mt->level]);
231 p = sbuf_data(mt->sbuf[mt->level]);
232 len = sbuf_len(mt->sbuf[mt->level]);
234 /* <freqband>...</freqband> */
235 if (iseq(name, "freqstart") && mt->freqband != NULL) {
236 mt->freqband->freqStart = strtoul(p, NULL, 0);
239 if (iseq(name, "freqend") && mt->freqband != NULL) {
240 mt->freqband->freqEnd = strtoul(p, NULL, 0);
243 if (iseq(name, "chanwidth") && mt->freqband != NULL) {
244 mt->freqband->chanWidth = strtoul(p, NULL, 0);
247 if (iseq(name, "chansep") && mt->freqband != NULL) {
248 mt->freqband->chanSep = strtoul(p, NULL, 0);
251 if (iseq(name, "flags")) {
252 if (mt->freqband != NULL)
253 mt->freqband->flags |= decode_flag(mt, p, len);
254 else if (mt->netband != NULL)
255 mt->netband->flags |= decode_flag(mt, p, len);
257 warnx("flags without freqband or netband at line %ld ignored",
258 XML_GetCurrentLineNumber(mt->parser));
264 if (iseq(name, "name") && mt->rd != NULL) {
265 mt->rd->name = strdup(p);
268 if (iseq(name, "sku") && mt->rd != NULL) {
269 mt->rd->sku = strtoul(p, NULL, 0);
272 if (iseq(name, "netband") && mt->rd != NULL) {
277 /* <band> ... </band> */
278 if (iseq(name, "freqband") && mt->netband != NULL) {
279 /* XXX handle inline freqbands */
282 if (iseq(name, "maxpower") && mt->netband != NULL) {
283 mt->netband->maxPower = strtoul(p, NULL, 0);
286 if (iseq(name, "maxpowerdfs") && mt->netband != NULL) {
287 mt->netband->maxPowerDFS = strtoul(p, NULL, 0);
290 if (iseq(name, "maxantgain") && mt->netband != NULL) {
291 mt->netband->maxAntGain = strtoul(p, NULL, 0);
295 /* <country>...</country> */
296 if (iseq(name, "isocc") && mt->country != NULL) {
297 mt->country->code = strtoul(p, NULL, 0);
300 if (iseq(name, "name") && mt->country != NULL) {
301 mt->country->name = strdup(p);
306 warnx("unexpected XML token \"%s\" data \"%s\" at line %ld",
307 name, p, XML_GetCurrentLineNumber(mt->parser));
311 if (iseq(name, "freqband") && mt->freqband != NULL) {
312 /* XXX must have start/end frequencies */
313 /* XXX must have channel width/sep */
318 if (iseq(name, "rd") && mt->rd != NULL) {
323 if (iseq(name, "band") && mt->netband != NULL) {
324 if (mt->netband->band == NULL) {
325 warnx("no freqbands for band at line %ld",
326 XML_GetCurrentLineNumber(mt->parser));
328 if (mt->netband->maxPower == 0) {
329 warnx("no maxpower for band at line %ld",
330 XML_GetCurrentLineNumber(mt->parser));
332 /* default max power w/ DFS to max power */
333 if (mt->netband->maxPowerDFS == 0)
334 mt->netband->maxPowerDFS = mt->netband->maxPower;
339 if (iseq(name, "netband") && mt->netband != NULL) {
344 if (iseq(name, "country") && mt->country != NULL) {
345 if (mt->country->code == NO_COUNTRY) {
346 warnx("no ISO cc for country at line %ld",
347 XML_GetCurrentLineNumber(mt->parser));
349 if (mt->country->name == NULL) {
350 warnx("no name for country at line %ld",
351 XML_GetCurrentLineNumber(mt->parser));
353 if (mt->country->rd == NULL) {
354 warnx("no regdomain reference for country at line %ld",
355 XML_GetCurrentLineNumber(mt->parser));
361 sbuf_delete(mt->sbuf[mt->level]);
362 mt->sbuf[mt->level--] = NULL;
366 char_data(void *data, const XML_Char *s, int len)
375 for (; isspace(*b) && b < e; b++)
377 for (; isspace(*e) && e > b; e++)
379 if (e != b || (*b != '\0' && !isspace(*b)))
380 sbuf_bcat(mt->sbuf[mt->level], b, e-b+1);
384 findid(struct regdata *rdp, const void *id, enum IdentType type)
388 for (ip = rdp->ident; ip->id != NULL; ip++)
389 if (ip->type == type && iseq(ip->id, id))
395 * Parse an regdomain XML configuration and build the internal representation.
398 lib80211_regdomain_readconfig(struct regdata *rdp, const void *p, size_t len)
401 struct regdomain *dp;
408 memset(rdp, 0, sizeof(struct regdata));
409 mt = calloc(1, sizeof(struct mystate));
412 /* parse the XML input */
414 mt->parser = XML_ParserCreate(NULL);
415 XML_SetUserData(mt->parser, mt);
416 XML_SetElementHandler(mt->parser, start_element, end_element);
417 XML_SetCharacterDataHandler(mt->parser, char_data);
418 if (XML_Parse(mt->parser, p, len, 1) != XML_STATUS_OK) {
419 warnx("%s: %s at line %ld", __func__,
420 XML_ErrorString(XML_GetErrorCode(mt->parser)),
421 XML_GetCurrentLineNumber(mt->parser));
424 XML_ParserFree(mt->parser);
426 /* setup the identifer table */
427 rdp->ident = calloc(sizeof(struct ident), mt->nident + 1);
428 if (rdp->ident == NULL)
434 LIST_FOREACH(dp, &rdp->domains, next) {
435 rdp->ident[i].id = dp->name;
436 rdp->ident[i].p = dp;
437 rdp->ident[i].type = DOMAIN;
440 LIST_FOREACH(fp, &rdp->freqbands, next) {
441 rdp->ident[i].id = fp->id;
442 rdp->ident[i].p = fp;
443 rdp->ident[i].type = FREQBAND;
446 LIST_FOREACH(cp, &rdp->countries, next) {
447 rdp->ident[i].id = cp->isoname;
448 rdp->ident[i].p = cp;
449 rdp->ident[i].type = COUNTRY;
453 /* patch references */
454 LIST_FOREACH(dp, &rdp->domains, next) {
455 if (dp->cc != NULL) {
457 dp->cc = findid(rdp, id, COUNTRY);
458 if (dp->cc == NULL) {
459 warnx("undefined country \"%s\"",
460 __DECONST(char *, id));
463 free(__DECONST(char *, id));
465 LIST_FOREACH(nb, &dp->bands_11b, next) {
466 id = findid(rdp, nb->band, FREQBAND);
468 warnx("undefined 11b band \"%s\"",
469 __DECONST(char *, nb->band));
474 LIST_FOREACH(nb, &dp->bands_11g, next) {
475 id = findid(rdp, nb->band, FREQBAND);
477 warnx("undefined 11g band \"%s\"",
478 __DECONST(char *, nb->band));
483 LIST_FOREACH(nb, &dp->bands_11a, next) {
484 id = findid(rdp, nb->band, FREQBAND);
486 warnx("undefined 11a band \"%s\"",
487 __DECONST(char *, nb->band));
492 LIST_FOREACH(nb, &dp->bands_11ng, next) {
493 id = findid(rdp, nb->band, FREQBAND);
495 warnx("undefined 11ng band \"%s\"",
496 __DECONST(char *, nb->band));
501 LIST_FOREACH(nb, &dp->bands_11na, next) {
502 id = findid(rdp, nb->band, FREQBAND);
504 warnx("undefined 11na band \"%s\"",
505 __DECONST(char *, nb->band));
511 LIST_FOREACH(cp, &rdp->countries, next) {
513 cp->rd = findid(rdp, id, DOMAIN);
514 if (cp->rd == NULL) {
515 warnx("undefined country \"%s\"",
516 __DECONST(char *, id));
519 free(__DECONST(char *, id));
522 return errors ? EINVAL : 0;
526 cleanup_bands(netband_head *head)
531 nb = LIST_FIRST(head);
539 * Cleanup state/resources for a previously parsed regdomain database.
542 lib80211_regdomain_cleanup(struct regdata *rdp)
548 struct regdomain *dp = LIST_FIRST(&rdp->domains);
551 LIST_REMOVE(dp, next);
552 cleanup_bands(&dp->bands_11b);
553 cleanup_bands(&dp->bands_11g);
554 cleanup_bands(&dp->bands_11a);
555 cleanup_bands(&dp->bands_11ng);
556 cleanup_bands(&dp->bands_11na);
557 if (dp->name != NULL)
558 free(__DECONST(char *, dp->name));
561 struct country *cp = LIST_FIRST(&rdp->countries);
564 LIST_REMOVE(cp, next);
565 if (cp->name != NULL)
566 free(__DECONST(char *, cp->name));
570 struct freqband *fp = LIST_FIRST(&rdp->freqbands);
573 LIST_REMOVE(fp, next);
579 lib80211_alloc_regdata(void)
586 rdp = calloc(1, sizeof(struct regdata));
588 fd = open(_PATH_REGDOMAIN, O_RDONLY);
591 warn("%s: open(%s)", __func__, _PATH_REGDOMAIN);
596 if (fstat(fd, &sb) < 0) {
598 warn("%s: fstat(%s)", __func__, _PATH_REGDOMAIN);
604 xml = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
605 if (xml == MAP_FAILED) {
607 warn("%s: mmap", __func__);
613 if (lib80211_regdomain_readconfig(rdp, xml, sb.st_size) != 0) {
615 warn("%s: error reading regulatory database", __func__);
617 munmap(xml, sb.st_size);
622 munmap(xml, sb.st_size);
629 lib80211_free_regdata(struct regdata *rdp)
631 lib80211_regdomain_cleanup(rdp);
636 * Lookup a regdomain by SKU.
638 const struct regdomain *
639 lib80211_regdomain_findbysku(const struct regdata *rdp, enum RegdomainCode sku)
641 const struct regdomain *dp;
643 LIST_FOREACH(dp, &rdp->domains, next) {
651 * Lookup a regdomain by name.
653 const struct regdomain *
654 lib80211_regdomain_findbyname(const struct regdata *rdp, const char *name)
656 const struct regdomain *dp;
658 LIST_FOREACH(dp, &rdp->domains, next) {
659 if (iseq(dp->name, name))
666 * Lookup a country by ISO country code.
668 const struct country *
669 lib80211_country_findbycc(const struct regdata *rdp, enum ISOCountryCode cc)
671 const struct country *cp;
673 LIST_FOREACH(cp, &rdp->countries, next) {
681 * Lookup a country by ISO/long name.
683 const struct country *
684 lib80211_country_findbyname(const struct regdata *rdp, const char *name)
686 const struct country *cp;
689 LIST_FOREACH(cp, &rdp->countries, next) {
690 if (iseq(cp->isoname, name))
694 LIST_FOREACH(cp, &rdp->countries, next) {
695 if (strncasecmp(cp->name, name, len) == 0)