Index: lib/Makefile =================================================================== --- lib/Makefile (revision 220181) +++ lib/Makefile (working copy) @@ -30,6 +30,7 @@ # Except it appears bind needs to be compiled last SUBDIR_ORDERED= ${_csu} \ + lib80211 \ libc \ libbsm \ libauditd \ Index: lib/lib80211/regdomain.c =================================================================== --- lib/lib80211/regdomain.c (revision 0) +++ lib/lib80211/regdomain.c (revision 0) @@ -0,0 +1,710 @@ +/*- + * Copyright (c) 2008 Sam Leffler, Errno Consulting + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef lint +static const char rcsid[] = "$FreeBSD$"; +#endif /* not lint */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "regdomain.h" + +#include + +#define MAXLEVEL 20 + +struct mystate { + XML_Parser parser; + struct regdata *rdp; + struct regdomain *rd; /* current domain */ + struct netband *netband; /* current netband */ + struct freqband *freqband; /* current freqband */ + struct country *country; /* current country */ + netband_head *curband; /* current netband list */ + int level; + struct sbuf *sbuf[MAXLEVEL]; + int nident; +}; + +typedef enum { + DOMAIN, COUNTRY, FREQBAND +} IDENT_TYPE; + +struct ident { + const void *id; + void *p; + IDENT_TYPE type; +}; + +static void +start_element(void *data, const char *name, const char **attr) +{ +#define iseq(a,b) (strcasecmp(a,b) == 0) + struct mystate *mt; + const void *id, *ref, *mode; + int i; + + mt = data; + if (++mt->level == MAXLEVEL) { + /* XXX force parser to abort */ + return; + } + mt->sbuf[mt->level] = sbuf_new_auto(); + id = ref = mode = NULL; + for (i = 0; attr[i] != NULL; i += 2) { + if (iseq(attr[i], "id")) { + id = attr[i+1]; + } else if (iseq(attr[i], "ref")) { + ref = attr[i+1]; + } else if (iseq(attr[i], "mode")) { + mode = attr[i+1]; + } else + printf("%*.*s[%s = %s]\n", mt->level + 1, + mt->level + 1, "", attr[i], attr[i+1]); + } + if (iseq(name, "rd") && mt->rd == NULL) { + if (mt->country == NULL) { + mt->rd = calloc(1, sizeof(struct regdomain)); + mt->rd->name = strdup(id); + mt->nident++; + LIST_INSERT_HEAD(&mt->rdp->domains, mt->rd, next); + } else + mt->country->rd = (void *)strdup(ref); + return; + } + if (iseq(name, "defcc") && mt->rd != NULL) { + mt->rd->cc = (void *)strdup(ref); + return; + } + if (iseq(name, "netband") && mt->curband == NULL && mt->rd != NULL) { + if (mode == NULL) { + warnx("no mode for netband at line %ld", + XML_GetCurrentLineNumber(mt->parser)); + return; + } + if (iseq(mode, "11b")) + mt->curband = &mt->rd->bands_11b; + else if (iseq(mode, "11g")) + mt->curband = &mt->rd->bands_11g; + else if (iseq(mode, "11a")) + mt->curband = &mt->rd->bands_11a; + else if (iseq(mode, "11ng")) + mt->curband = &mt->rd->bands_11ng; + else if (iseq(mode, "11na")) + mt->curband = &mt->rd->bands_11na; + else + warnx("unknown mode \"%s\" at line %ld", + __DECONST(char *, mode), + XML_GetCurrentLineNumber(mt->parser)); + return; + } + if (iseq(name, "band") && mt->netband == NULL) { + if (mt->curband == NULL) { + warnx("band without enclosing netband at line %ld", + XML_GetCurrentLineNumber(mt->parser)); + return; + } + mt->netband = calloc(1, sizeof(struct netband)); + LIST_INSERT_HEAD(mt->curband, mt->netband, next); + return; + } + if (iseq(name, "freqband") && mt->freqband == NULL && mt->netband != NULL) { + /* XXX handle inlines and merge into table? */ + if (mt->netband->band != NULL) { + warnx("duplicate freqband at line %ld ignored", + XML_GetCurrentLineNumber(mt->parser)); + /* XXX complain */ + } else + mt->netband->band = (void *)strdup(ref); + return; + } + + if (iseq(name, "country") && mt->country == NULL) { + mt->country = calloc(1, sizeof(struct country)); + mt->country->isoname = strdup(id); + mt->country->code = NO_COUNTRY; + mt->nident++; + LIST_INSERT_HEAD(&mt->rdp->countries, mt->country, next); + return; + } + + if (iseq(name, "freqband") && mt->freqband == NULL) { + mt->freqband = calloc(1, sizeof(struct freqband)); + mt->freqband->id = strdup(id); + mt->nident++; + LIST_INSERT_HEAD(&mt->rdp->freqbands, mt->freqband, next); + return; + } +#undef iseq +} + +static int +decode_flag(struct mystate *mt, const char *p, int len) +{ +#define iseq(a,b) (strcasecmp(a,b) == 0) + static const struct { + const char *name; + int len; + uint32_t value; + } flags[] = { +#define FLAG(x) { #x, sizeof(#x)-1, x } + FLAG(IEEE80211_CHAN_A), + FLAG(IEEE80211_CHAN_B), + FLAG(IEEE80211_CHAN_G), + FLAG(IEEE80211_CHAN_HT20), + FLAG(IEEE80211_CHAN_HT40), + FLAG(IEEE80211_CHAN_ST), + FLAG(IEEE80211_CHAN_TURBO), + FLAG(IEEE80211_CHAN_PASSIVE), + FLAG(IEEE80211_CHAN_DFS), + FLAG(IEEE80211_CHAN_CCK), + FLAG(IEEE80211_CHAN_OFDM), + FLAG(IEEE80211_CHAN_2GHZ), + FLAG(IEEE80211_CHAN_5GHZ), + FLAG(IEEE80211_CHAN_DYN), + FLAG(IEEE80211_CHAN_GFSK), + FLAG(IEEE80211_CHAN_GSM), + FLAG(IEEE80211_CHAN_STURBO), + FLAG(IEEE80211_CHAN_HALF), + FLAG(IEEE80211_CHAN_QUARTER), + FLAG(IEEE80211_CHAN_HT40U), + FLAG(IEEE80211_CHAN_HT40D), + FLAG(IEEE80211_CHAN_4MSXMIT), + FLAG(IEEE80211_CHAN_NOADHOC), + FLAG(IEEE80211_CHAN_NOHOSTAP), + FLAG(IEEE80211_CHAN_11D), + FLAG(IEEE80211_CHAN_FHSS), + FLAG(IEEE80211_CHAN_PUREG), + FLAG(IEEE80211_CHAN_108A), + FLAG(IEEE80211_CHAN_108G), +#undef FLAG + { "ECM", 3, REQ_ECM }, + { "INDOOR", 6, REQ_INDOOR }, + { "OUTDOOR", 7, REQ_OUTDOOR }, + }; + int i; + const int cnt = sizeof(flags)/sizeof(flags[0]); + + for (i = 0; i < cnt; i++) + if (len == flags[i].len && iseq(p, flags[i].name)) + return flags[i].value; + warnx("unknown flag \"%.*s\" at line %ld ignored", + len, p, XML_GetCurrentLineNumber(mt->parser)); + return 0; +#undef iseq +} + +static void +end_element(void *data, const char *name) +{ +#define iseq(a,b) (strcasecmp(a,b) == 0) + struct mystate *mt; + int len; + char *p; + + mt = data; + sbuf_finish(mt->sbuf[mt->level]); + p = sbuf_data(mt->sbuf[mt->level]); + len = sbuf_len(mt->sbuf[mt->level]); + + /* ... */ + if (iseq(name, "freqstart") && mt->freqband != NULL) { + mt->freqband->freqStart = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "freqend") && mt->freqband != NULL) { + mt->freqband->freqEnd = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "chanwidth") && mt->freqband != NULL) { + mt->freqband->chanWidth = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "chansep") && mt->freqband != NULL) { + mt->freqband->chanSep = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "flags")) { + if (mt->freqband != NULL) + mt->freqband->flags |= decode_flag(mt, p, len); + else if (mt->netband != NULL) + mt->netband->flags |= decode_flag(mt, p, len); + else { + warnx("flags without freqband or netband at line %ld ignored", + XML_GetCurrentLineNumber(mt->parser)); + } + goto done; + } + + /* ... */ + if (iseq(name, "name") && mt->rd != NULL) { + mt->rd->name = strdup(p); + goto done; + } + if (iseq(name, "sku") && mt->rd != NULL) { + mt->rd->sku = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "netband") && mt->rd != NULL) { + mt->curband = NULL; + goto done; + } + + /* ... */ + if (iseq(name, "freqband") && mt->netband != NULL) { + /* XXX handle inline freqbands */ + goto done; + } + if (iseq(name, "maxpower") && mt->netband != NULL) { + mt->netband->maxPower = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "maxpowerdfs") && mt->netband != NULL) { + mt->netband->maxPowerDFS = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "maxantgain") && mt->netband != NULL) { + mt->netband->maxAntGain = strtoul(p, NULL, 0); + goto done; + } + + /* ... */ + if (iseq(name, "isocc") && mt->country != NULL) { + mt->country->code = strtoul(p, NULL, 0); + goto done; + } + if (iseq(name, "name") && mt->country != NULL) { + mt->country->name = strdup(p); + goto done; + } + + if (len != 0) { + warnx("unexpected XML token \"%s\" data \"%s\" at line %ld", + name, p, XML_GetCurrentLineNumber(mt->parser)); + /* XXX goto done? */ + } + /* */ + if (iseq(name, "freqband") && mt->freqband != NULL) { + /* XXX must have start/end frequencies */ + /* XXX must have channel width/sep */ + mt->freqband = NULL; + goto done; + } + /* */ + if (iseq(name, "rd") && mt->rd != NULL) { + mt->rd = NULL; + goto done; + } + /* */ + if (iseq(name, "band") && mt->netband != NULL) { + if (mt->netband->band == NULL) { + warnx("no freqbands for band at line %ld", + XML_GetCurrentLineNumber(mt->parser)); + } + if (mt->netband->maxPower == 0) { + warnx("no maxpower for band at line %ld", + XML_GetCurrentLineNumber(mt->parser)); + } + /* default max power w/ DFS to max power */ + if (mt->netband->maxPowerDFS == 0) + mt->netband->maxPowerDFS = mt->netband->maxPower; + mt->netband = NULL; + goto done; + } + /* */ + if (iseq(name, "netband") && mt->netband != NULL) { + mt->curband = NULL; + goto done; + } + /* */ + if (iseq(name, "country") && mt->country != NULL) { + if (mt->country->code == NO_COUNTRY) { + warnx("no ISO cc for country at line %ld", + XML_GetCurrentLineNumber(mt->parser)); + } + if (mt->country->name == NULL) { + warnx("no name for country at line %ld", + XML_GetCurrentLineNumber(mt->parser)); + } + if (mt->country->rd == NULL) { + warnx("no regdomain reference for country at line %ld", + XML_GetCurrentLineNumber(mt->parser)); + } + mt->country = NULL; + goto done; + } +done: + sbuf_delete(mt->sbuf[mt->level]); + mt->sbuf[mt->level--] = NULL; +#undef iseq +} + +static void +char_data(void *data, const XML_Char *s, int len) +{ + struct mystate *mt; + const char *b, *e; + + mt = data; + + b = s; + e = s + len-1; + for (; isspace(*b) && b < e; b++) + ; + for (; isspace(*e) && e > b; e++) + ; + if (e != b || (*b != '\0' && !isspace(*b))) + sbuf_bcat(mt->sbuf[mt->level], b, e-b+1); +} + +static void * +findid(struct regdata *rdp, const void *id, IDENT_TYPE type) +{ + struct ident *ip; + + for (ip = rdp->ident; ip->id != NULL; ip++) + if (ip->type == type && strcasecmp(ip->id, id) == 0) + return ip->p; + return NULL; +} + +/* + * Parse an regdomain XML configuration and build the internal representation. + */ +int +lib80211_regdomain_readconfig(struct regdata *rdp, const void *p, size_t len) +{ + struct mystate *mt; + struct regdomain *dp; + struct country *cp; + struct freqband *fp; + struct netband *nb; + const void *id; + int i, errors; + + memset(rdp, 0, sizeof(struct regdata)); + mt = calloc(1, sizeof(struct mystate)); + if (mt == NULL) + return ENOMEM; + /* parse the XML input */ + mt->rdp = rdp; + mt->parser = XML_ParserCreate(NULL); + XML_SetUserData(mt->parser, mt); + XML_SetElementHandler(mt->parser, start_element, end_element); + XML_SetCharacterDataHandler(mt->parser, char_data); + if (XML_Parse(mt->parser, p, len, 1) != XML_STATUS_OK) { + warnx("%s: %s at line %ld", __func__, + XML_ErrorString(XML_GetErrorCode(mt->parser)), + XML_GetCurrentLineNumber(mt->parser)); + return -1; + } + XML_ParserFree(mt->parser); + + /* setup the identifer table */ + rdp->ident = calloc(sizeof(struct ident), mt->nident + 1); + if (rdp->ident == NULL) + return ENOMEM; + free(mt); + + errors = 0; + i = 0; + LIST_FOREACH(dp, &rdp->domains, next) { + rdp->ident[i].id = dp->name; + rdp->ident[i].p = dp; + rdp->ident[i].type = DOMAIN; + i++; + } + LIST_FOREACH(fp, &rdp->freqbands, next) { + rdp->ident[i].id = fp->id; + rdp->ident[i].p = fp; + rdp->ident[i].type = FREQBAND; + i++; + } + LIST_FOREACH(cp, &rdp->countries, next) { + rdp->ident[i].id = cp->isoname; + rdp->ident[i].p = cp; + rdp->ident[i].type = COUNTRY; + i++; + } + + /* patch references */ + LIST_FOREACH(dp, &rdp->domains, next) { + if (dp->cc != NULL) { + id = dp->cc; + dp->cc = findid(rdp, id, COUNTRY); + if (dp->cc == NULL) { + warnx("undefined country \"%s\"", + __DECONST(char *, id)); + errors++; + } + free(__DECONST(char *, id)); + } + LIST_FOREACH(nb, &dp->bands_11b, next) { + id = findid(rdp, nb->band, FREQBAND); + if (id == NULL) { + warnx("undefined 11b band \"%s\"", + __DECONST(char *, nb->band)); + errors++; + } + nb->band = id; + } + LIST_FOREACH(nb, &dp->bands_11g, next) { + id = findid(rdp, nb->band, FREQBAND); + if (id == NULL) { + warnx("undefined 11g band \"%s\"", + __DECONST(char *, nb->band)); + errors++; + } + nb->band = id; + } + LIST_FOREACH(nb, &dp->bands_11a, next) { + id = findid(rdp, nb->band, FREQBAND); + if (id == NULL) { + warnx("undefined 11a band \"%s\"", + __DECONST(char *, nb->band)); + errors++; + } + nb->band = id; + } + LIST_FOREACH(nb, &dp->bands_11ng, next) { + id = findid(rdp, nb->band, FREQBAND); + if (id == NULL) { + warnx("undefined 11ng band \"%s\"", + __DECONST(char *, nb->band)); + errors++; + } + nb->band = id; + } + LIST_FOREACH(nb, &dp->bands_11na, next) { + id = findid(rdp, nb->band, FREQBAND); + if (id == NULL) { + warnx("undefined 11na band \"%s\"", + __DECONST(char *, nb->band)); + errors++; + } + nb->band = id; + } + } + LIST_FOREACH(cp, &rdp->countries, next) { + id = cp->rd; + cp->rd = findid(rdp, id, DOMAIN); + if (cp->rd == NULL) { + warnx("undefined country \"%s\"", + __DECONST(char *, id)); + errors++; + } + free(__DECONST(char *, id)); + } + + return errors ? EINVAL : 0; +} + +static void +cleanup_bands(netband_head *head) +{ + struct netband *nb; + + for (;;) { + nb = LIST_FIRST(head); + if (nb == NULL) + break; + free(nb); + } +} + +/* + * Cleanup state/resources for a previously parsed regdomain database. + */ +void +lib80211_regdomain_cleanup(struct regdata *rdp) +{ + + free(rdp->ident); + rdp->ident = NULL; + for (;;) { + struct regdomain *dp = LIST_FIRST(&rdp->domains); + if (dp == NULL) + break; + LIST_REMOVE(dp, next); + cleanup_bands(&dp->bands_11b); + cleanup_bands(&dp->bands_11g); + cleanup_bands(&dp->bands_11a); + cleanup_bands(&dp->bands_11ng); + cleanup_bands(&dp->bands_11na); + if (dp->name != NULL) + free(__DECONST(char *, dp->name)); + } + for (;;) { + struct country *cp = LIST_FIRST(&rdp->countries); + if (cp == NULL) + break; + LIST_REMOVE(cp, next); + if (cp->name != NULL) + free(__DECONST(char *, cp->name)); + free(cp); + } + for (;;) { + struct freqband *fp = LIST_FIRST(&rdp->freqbands); + if (fp == NULL) + break; + LIST_REMOVE(fp, next); + free(fp); + } +} + +struct regdata * +lib80211_alloc_regdata(void) +{ + struct regdata *rdp; + struct stat sb; + void *xml; + int fd; + + rdp = calloc(1, sizeof(struct regdata)); + + fd = open(_PATH_REGDOMAIN, O_RDONLY); + if (fd < 0) { +#ifdef DEBUG + warn("%s: open(%s)", __func__, _PATH_REGDOMAIN); +#endif + free(rdp); + return NULL; + } + if (fstat(fd, &sb) < 0) { +#ifdef DEBUG + warn("%s: fstat(%s)", __func__, _PATH_REGDOMAIN); +#endif + close(fd); + free(rdp); + return NULL; + } + xml = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (xml == MAP_FAILED) { +#ifdef DEBUG + warn("%s: mmap", __func__); +#endif + close(fd); + free(rdp); + return NULL; + } + if (lib80211_regdomain_readconfig(rdp, xml, sb.st_size) != 0) { +#ifdef DEBUG + warn("%s: error reading regulatory database", __func__); +#endif + munmap(xml, sb.st_size); + close(fd); + free(rdp); + return NULL; + } + munmap(xml, sb.st_size); + close(fd); + + return rdp; +} + +void +lib80211_free_regdata(struct regdata *rdp) +{ + lib80211_regdomain_cleanup(rdp); + free(rdp); +} + +/* + * Lookup a regdomain by SKU. + */ +const struct regdomain * +lib80211_regdomain_findbysku(const struct regdata *rdp, enum RegdomainCode sku) +{ + const struct regdomain *dp; + + LIST_FOREACH(dp, &rdp->domains, next) { + if (dp->sku == sku) + return dp; + } + return NULL; +} + +/* + * Lookup a regdomain by name. + */ +const struct regdomain * +lib80211_regdomain_findbyname(const struct regdata *rdp, const char *name) +{ + const struct regdomain *dp; + + LIST_FOREACH(dp, &rdp->domains, next) { + if (strcasecmp(dp->name, name) == 0) + return dp; + } + return NULL; +} + +/* + * Lookup a country by ISO country code. + */ +const struct country * +lib80211_country_findbycc(const struct regdata *rdp, enum ISOCountryCode cc) +{ + const struct country *cp; + + LIST_FOREACH(cp, &rdp->countries, next) { + if (cp->code == cc) + return cp; + } + return NULL; +} + +/* + * Lookup a country by ISO/long name. + */ +const struct country * +lib80211_country_findbyname(const struct regdata *rdp, const char *name) +{ + const struct country *cp; + int len; + + len = strlen(name); + LIST_FOREACH(cp, &rdp->countries, next) { + if (strcasecmp(cp->isoname, name) == 0) + return cp; + } + LIST_FOREACH(cp, &rdp->countries, next) { + if (strncasecmp(cp->name, name, len) == 0) + return cp; + } + return NULL; +} Index: lib/lib80211/chanflags.c =================================================================== --- lib/lib80211/chanflags.c (revision 0) +++ lib/lib80211/chanflags.c (revision 0) @@ -0,0 +1,98 @@ +/*- + * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef lint +static const char rcsid[] = "$FreeBSD$"; +#endif /* not lint */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "chanflags.h" + +#include + +/* + * Return a string label for the given bit offset. + */ +const char * +lib80211_decode_channel_flag_bit(int ofs) +{ + static const struct { + const char *name; + uint32_t value; + const char *sname; + } flags[] = { +#define FLAG(x, a) { #x, (x), (a) } + FLAG(IEEE80211_CHAN_A, "11a"), + FLAG(IEEE80211_CHAN_B, "11b"), + FLAG(IEEE80211_CHAN_G, "11g"), + FLAG(IEEE80211_CHAN_HT20, "ht20"), + FLAG(IEEE80211_CHAN_HT40, "ht40"), + FLAG(IEEE80211_CHAN_ST, "st"), + FLAG(IEEE80211_CHAN_TURBO, "turbo"), + FLAG(IEEE80211_CHAN_PASSIVE, "passive"), + FLAG(IEEE80211_CHAN_DFS, "dfs"), + FLAG(IEEE80211_CHAN_CCK, "cck"), + FLAG(IEEE80211_CHAN_OFDM, "ofdm"), + FLAG(IEEE80211_CHAN_2GHZ, "2ghz"), + FLAG(IEEE80211_CHAN_5GHZ, "5ghz"), + FLAG(IEEE80211_CHAN_DYN, "dyn"), + FLAG(IEEE80211_CHAN_GFSK, "gfsk"), + FLAG(IEEE80211_CHAN_GSM, "gsm"), + FLAG(IEEE80211_CHAN_STURBO, "sturbo"), + FLAG(IEEE80211_CHAN_HALF, "half"), + FLAG(IEEE80211_CHAN_QUARTER, "quarter"), + FLAG(IEEE80211_CHAN_HT40U, "ht40+"), + FLAG(IEEE80211_CHAN_HT40D, "ht40-"), + FLAG(IEEE80211_CHAN_4MSXMIT, "4ms"), + FLAG(IEEE80211_CHAN_NOADHOC, "noadhoc"), + FLAG(IEEE80211_CHAN_NOHOSTAP, "nohostap"), + FLAG(IEEE80211_CHAN_11D, "11d"), + FLAG(IEEE80211_CHAN_FHSS, "fhss"), + FLAG(IEEE80211_CHAN_PUREG, "pureg"), + FLAG(IEEE80211_CHAN_108A, "108a"), + FLAG(IEEE80211_CHAN_108G, "108g"), +#undef FLAG + }; + int i; + + for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) + if ((uint32_t)(1 << ofs) == flags[i].value) + return flags[i].sname; + + return "unknown"; +} Index: lib/lib80211/regdomain.h =================================================================== --- lib/lib80211/regdomain.h (revision 0) +++ lib/lib80211/regdomain.h (revision 0) @@ -0,0 +1,121 @@ +/*- + * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ +#ifndef _LIB80211_H_ +#define _LIB80211_H_ + +#include +#include + +#include + +__BEGIN_DECLS + +struct freqband { + uint16_t freqStart; /* starting frequency (MHz) */ + uint16_t freqEnd; /* ending frequency (MHz) */ + uint8_t chanWidth; /* channel width (MHz) */ + uint8_t chanSep; /* channel sepaaration (MHz) */ + uint32_t flags; /* common operational constraints */ + + const void *id; + LIST_ENTRY(freqband) next; +}; + +/* private flags, don't pass to os */ +#define REQ_ECM 0x1 /* enable if ECM set */ +#define REQ_INDOOR 0x2 /* enable only for indoor operation */ +#define REQ_OUTDOOR 0x4 /* enable only for outdoor operation */ + +#define REQ_FLAGS (REQ_ECM|REQ_INDOOR|REQ_OUTDOOR) + +struct netband { + const struct freqband *band; /* channel list description */ + uint8_t maxPower; /* regulatory cap on tx power (dBm) */ + uint8_t maxPowerDFS; /* regulatory cap w/ DFS (dBm) */ + uint8_t maxAntGain; /* max allowed antenna gain (.5 dBm) */ + uint32_t flags; /* net80211 channel flags */ + + LIST_ENTRY(netband) next; +}; +typedef LIST_HEAD(, netband) netband_head; + +struct country; + +struct regdomain { + enum RegdomainCode sku; /* regdomain code/SKU */ + const char *name; /* printable name */ + const struct country *cc; /* country code for 1-1/default map */ + + netband_head bands_11b; /* 11b operation */ + netband_head bands_11g; /* 11g operation */ + netband_head bands_11a; /* 11a operation */ + netband_head bands_11ng;/* 11ng operation */ + netband_head bands_11na;/* 11na operation */ + + LIST_ENTRY(regdomain) next; +}; + +struct country { + enum ISOCountryCode code; +#define NO_COUNTRY 0xffff + const struct regdomain *rd; + const char* isoname; + const char* name; + + LIST_ENTRY(country) next; +}; + +struct ident; + +struct regdata { + LIST_HEAD(, country) countries; /* country code table */ + LIST_HEAD(, regdomain) domains; /* regulatory domains */ + LIST_HEAD(, freqband) freqbands; /* frequency band table */ + struct ident *ident; /* identifier table */ +}; + +#define _PATH_REGDOMAIN "/etc/regdomain.xml" + +struct regdata *lib80211_alloc_regdata(void); +void lib80211_free_regdata(struct regdata *); + +int lib80211_regdomain_readconfig(struct regdata *, const void *, size_t); +void lib80211_regdomain_cleanup(struct regdata *); + +const struct regdomain *lib80211_regdomain_findbysku(const struct regdata *, + enum RegdomainCode); +const struct regdomain *lib80211_regdomain_findbyname(const struct regdata *, + const char *); + +const struct country *lib80211_country_findbycc(const struct regdata *, + enum ISOCountryCode); +const struct country *lib80211_country_findbyname(const struct regdata *, + const char *); + +__END_DECLS + +#endif /* _LIB80211_H_ */ Index: lib/lib80211/chanflags.h =================================================================== --- lib/lib80211/chanflags.h (revision 0) +++ lib/lib80211/chanflags.h (revision 0) @@ -0,0 +1,33 @@ +/*- + * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef __CHANFLAGS_H__ +#define __CHANFLAGS_H__ + +extern const char * lib80211_decode_channel_flag_bit(int ofs); + +#endif /* __CHANFLAGS_H__ */ Index: lib/lib80211/Makefile =================================================================== --- lib/lib80211/Makefile (revision 0) +++ lib/lib80211/Makefile (revision 0) @@ -0,0 +1,8 @@ +.include + +LIB= 80211 +SRCS= regdomain.c regdomain.h chanflags.c chanflags.h +INCS= regdomain.h chanflags.h +INCSDIR= ${INCLUDEDIR}/lib80211 + +.include Index: etc/mtree/BSD.include.dist =================================================================== --- etc/mtree/BSD.include.dist (revision 220181) +++ etc/mtree/BSD.include.dist (working copy) @@ -225,6 +225,8 @@ .. kadm5 .. + lib80211 + .. libmilter .. lwres Index: tools/tools/net80211/wlanreg/wlanreg.c =================================================================== --- tools/tools/net80211/wlanreg/wlanreg.c (revision 0) +++ tools/tools/net80211/wlanreg/wlanreg.c (revision 0) @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static const char copyright[] = +"@(#) Copyright (c) 2011\n\ + Adrian Chadd, Xenion Pty Ltd. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static const char rcsid[] = + "$FreeBSD$"; +#endif /* not lint */ + +#include +#include +#include +#include +#include + +#include +#include + +#include + +static void +print_chan_flags(uint32_t flags) +{ + int i; + for (i = 0; i < 32; i++) { + if (flags & (1 << i)) + printf("%s ", lib80211_decode_channel_flag_bit(i)); + } +} + +static void +print_freqband(const struct freqband *f) +{ + printf("%d - %d MHz\t%d MHz\t%d MHz", + f->freqStart, f->freqEnd, f->chanWidth, f->chanSep); +} + +static void +print_netband(const struct netband *n) +{ + print_freqband(n->band); + printf("\t\t%d dBm\t%d dBm\t\t%.2f dBm\t", + n->maxPower, n->maxPowerDFS, (float) n->maxAntGain / 2.0); + print_chan_flags(n->flags | n->band->flags); + printf("\n"); +} + +static void +print_netband_list(const netband_head *head, const char *mode) +{ + const struct netband *n; + + LIST_FOREACH(n, head, next) { + print_netband(n); + } +} + +static void +print_regdomain(const struct regdomain *r) +{ + + print_netband_list(&r->bands_11b, "11b"); + print_netband_list(&r->bands_11g, "11g"); + print_netband_list(&r->bands_11a, "11a"); + print_netband_list(&r->bands_11ng, "11ng"); + print_netband_list(&r->bands_11na, "11na"); +} + +const struct regdomain * +find_regdomain_by_country(const struct regdata *rdp, const char *country) +{ + const struct country *c = NULL; + + /* Pull out country code pointer */ + c = lib80211_country_findbyname(rdp, country); + if (c == NULL) { + printf("Couldn't find country %s\n", country); + return NULL; + } + + /* 'c' has a country code, a regdomain, etc */ + printf("Country: %s\n", c->name); + printf("ISO: %d\n", c->code); + printf("ISO Name: %s\n", c->isoname); + return c->rd; +} + +const struct regdomain * +find_regdomain_by_sku(const struct regdata *rdp, const char *sku) +{ + const struct regdomain *r; + + r = lib80211_regdomain_findbyname(rdp, sku); + if (r == NULL) { + printf("Couldn't find sku %s\n", sku); + return NULL; + } + return r; +} + +static void +list_countries(const struct regdata *rdp) +{ + const struct country *cp; + const struct regdomain *dp; + int i; + + i = 0; + printf("\nCountry codes:\n"); + LIST_FOREACH(cp, &rdp->countries, next) { + printf("%2s %-15.15s%s", cp->isoname, + cp->name, ((i+1)%4) == 0 ? "\n" : " "); + i++; + } + i = 0; + printf("\nRegulatory domains:\n"); + LIST_FOREACH(dp, &rdp->domains, next) { + printf("%-15.15s%s", dp->name, ((i+1)%4) == 0 ? "\n" : " "); + i++; + } + printf("\n"); +} + + +int +main(int argc, const char *argv[]) +{ + struct regdata *rdp = NULL; + const struct regdomain *rd = NULL; + const char *code, *type = NULL; + + if (argc < 2) { + printf("Usage: %s (list | country | sku )\n", argv[0]); + exit(127); + } + + type = argv[1]; + code = argv[2]; + + /* Read in the regdomain database */ + rdp = lib80211_alloc_regdata(); + if (rdp == NULL) + errx(-1, "missing or corrupted regdomain database"); + + if (strcmp("list", type) == 0) { + list_countries(rdp); + exit(0); + } else if (argc < 3) { + printf("Usage: %s (list | country | sku )\n", argv[0]); + exit(127); + } else if (strcmp("country", type) == 0) { + rd = find_regdomain_by_country(rdp, code); + } else if (strcmp("sku", type) == 0) { + rd = find_regdomain_by_sku(rdp, code); + } else { + printf("Unknown type '%s'\n", type); + exit(127); + } + if (rd == NULL) + exit(1); + + printf("SKU: %s\n", rd->name); + printf("\n"); + + printf("Freq range\tWidth\tSeparation\tPower\tDFS Power\tAntenna Gain\tFlags\n"); + print_regdomain(rd); + exit(0); +} Index: tools/tools/net80211/wlanreg/Makefile =================================================================== --- tools/tools/net80211/wlanreg/Makefile (revision 0) +++ tools/tools/net80211/wlanreg/Makefile (revision 0) @@ -0,0 +1,7 @@ +PROG= wlanreg +SRCS= wlanreg.c +LDADD= -l80211 -lbsdxml -lsbuf + +NO_MAN= yes + +.include Index: tools/tools/net80211/Makefile =================================================================== --- tools/tools/net80211/Makefile (revision 220181) +++ tools/tools/net80211/Makefile (working copy) @@ -1,5 +1,5 @@ # $FreeBSD$ -SUBDIR= stumbler w00t wesside wlaninject wlanstats wlantxtime wlanwatch wlanwds +SUBDIR= stumbler w00t wesside wlaninject wlanreg wlanstats wlantxtime wlanwatch wlanwds .include