summaryrefslogtreecommitdiffstats
path: root/lib/lib80211/lib80211_regdomain.c
blob: 32901a6b56e7a292c1233d38c557689fffb96cd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*-
 * 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 <sys/types.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/sbuf.h>
#include <sys/stat.h>

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <err.h>
#include <unistd.h>

#include <bsdxml.h>

#include "lib80211_regdomain.h"

#include <net80211/_ieee80211.h>

#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;
};

struct ident {
	const void *id;
	void *p;
	enum { DOMAIN, COUNTRY, FREQBAND } 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 },
	};
	unsigned int i;

	for (i = 0; i < nitems(flags); 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]);

	/* <freqband>...</freqband> */
	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;
	}

	/* <rd> ... </rd> */
	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;
	}

	/* <band> ... </band> */
	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;
	}

	/* <country>...</country> */
	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? */
	}
	/* </freqband> */
	if (iseq(name, "freqband") && mt->freqband != NULL) {
		/* XXX must have start/end frequencies */
		/* XXX must have channel width/sep */
		mt->freqband = NULL;
		goto done;
	}
	/* </rd> */
	if (iseq(name, "rd") && mt->rd != NULL) {
		mt->rd = NULL;
		goto done;
	}
	/* </band> */
	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;
	}
	/* </netband> */
	if (iseq(name, "netband") && mt->netband != NULL) {
		mt->curband = NULL;
		goto done;
	}
	/* </country> */
	if (iseq(name, "country") && mt->country != NULL) {
		/* XXX NO_COUNTRY should be in the net80211 country enum */
		if ((int) 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, int type)
{
	struct ident *ip;

	for (ip = rdp->ident; ip->id != NULL; ip++)
		if ((int) 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;
}
OpenPOWER on IntegriCloud