summaryrefslogtreecommitdiff
path: root/regress/usr.sbin/bgpd/unittests/rde_trie_test.c
blob: fc78aac45c882ad4012a20e7a7ebc39264353744 (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
/*	$OpenBSD: rde_trie_test.c,v 1.9 2018/11/01 14:20:41 claudio Exp $ */

/*
 * Copyright (c) 2018 Claudio Jeker <claudio@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/socket.h>

#include <err.h>
#include <limits.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <util.h>

#include "bgpd.h"
#include "rde.h"

struct rde_memstats rdemem;

int roa;
int orlonger;

int
host_ip(const char *s, struct bgpd_addr *h, u_int8_t *len)
{
	struct addrinfo	hints, *res;
	int		bits;

	bzero(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM; /*dummy*/
	hints.ai_flags = AI_NUMERICHOST;
	if (getaddrinfo(s, NULL, &hints, &res) == 0) {
		*len = res->ai_family == AF_INET6 ? 128 : 32;
		sa2addr(res->ai_addr, h);
		freeaddrinfo(res);
	} else {        /* ie. for 10/8 parsing */
		if ((bits = inet_net_pton(AF_INET, s, &h->v4, sizeof(h->v4))) == -1)
			return (0);
		*len = bits;
		h->aid = AID_INET;
	}

	return (1);
}

int
host(const char *s, struct bgpd_addr *h, u_int8_t *len)
{
	int		 mask = 128;
	char		*p, *ps;
	const char	*errstr;

	if ((ps = strdup(s)) == NULL)
		errx(1, "%s: strdup", __func__);

	if ((p = strrchr(ps, '/')) != NULL) {
		mask = strtonum(p+1, 0, 128, &errstr);
		if (errstr) {
			warnx("prefixlen is %s: %s", errstr, p+1);
			return (0);
		}
		p[0] = '\0';
	}

	bzero(h, sizeof(*h));

	if (host_ip(ps, h, len) == 0) {
		free(ps);
		return (0);
	}

	if (p != NULL)
		*len = mask;

	free(ps);
	return (1);
}


static const char *
print_prefix(struct bgpd_addr *p)
{
	static char buf[48];

	if (p->aid == AID_INET) {
		if (inet_ntop(AF_INET, &p->ba, buf, sizeof(buf)) == NULL)
			return "?";
	} else if (p->aid == AID_INET6) {
		if (inet_ntop(AF_INET6, &p->ba, buf, sizeof(buf)) == NULL)
			return "?";
	} else {
		return "???";
	}
	return buf;
}

static void
parse_file(FILE *in, struct trie_head *th)
{
	const char *errstr;
	char *line, *s;
	struct bgpd_addr prefix;
	u_int8_t plen;

	while ((line = fparseln(in, NULL, NULL, NULL, FPARSELN_UNESCALL))) {
		int state = 0;
		u_int8_t min = 255, max = 255, maskmax = 0;

		while ((s = strsep(&line, " \t\n"))) {
			if (*s == '\0')
				continue;
			switch (state) {
			case 0:
				if (!host(s, &prefix, &plen))
					errx(1, "%s: could not parse "
					    "prefix \"%s\"", __func__, s);
				if (prefix.aid == AID_INET6)
					maskmax = 128;
				else
					maskmax = 32;
				break;
			case 1:	
				min = strtonum(s, 0, maskmax, &errstr);
				if (errstr != NULL)
					errx(1, "min is %s: %s", errstr, s);
				break;
			case 2:
				max = strtonum(s, 0, maskmax, &errstr);
				if (errstr != NULL)
					errx(1, "max is %s: %s", errstr, s);
				break;
			default:
				errx(1, "could not parse \"%s\", confused", s);
			}
			state++;
		}
		if (state == 0)
			continue;
		if (max == 255)
			max = maskmax;
		if (min == 255)
			min = plen;

		if (trie_add(th, &prefix, plen, min, max) != 0)
			errx(1, "trie_add(%s, %u, %u, %u) failed",
			    print_prefix(&prefix), plen, min, max);

		free(line);
	}
}

static void
parse_roa_file(FILE *in, struct trie_head *th)
{
	const char *errstr;
	char *line, *s;
	struct set_table *set = NULL;
	struct roa_set rs;
	struct bgpd_addr prefix;
	u_int8_t plen;

	while ((line = fparseln(in, NULL, NULL, NULL, FPARSELN_UNESCALL))) {
		int state = 0;
		u_int32_t as;
		u_int8_t max = 0;

		while ((s = strsep(&line, " \t\n"))) {
			if (*s == '\0')
				continue;
			if (strcmp(s, "source-as") == 0) {
				state = 4;
				continue;
			}
			if (strcmp(s, "maxlen") == 0) {
				state = 2;
				continue;
			}
			if (strcmp(s, "prefix") == 0) {
				state = 0;
				continue;
			}
			switch (state) {
			case 0:
				if (!host(s, &prefix, &plen))
					errx(1, "%s: could not parse "
					    "prefix \"%s\"", __func__, s);
				break;
			case 2:
				max = strtonum(s, 0, 128, &errstr);
				if (errstr != NULL)
					errx(1, "max is %s: %s", errstr, s);
				break;
			case 4:
				as = strtonum(s, 0, UINT_MAX, &errstr);
				if (errstr != NULL)
					errx(1, "source-as is %s: %s", errstr,
					    s);
				break;
			default:
				errx(1, "could not parse \"%s\", confused", s);
			}
		}

		if (state == 0) {
			set_prep(set);
			if (trie_roa_add(th, &prefix, plen, set) != 0)
				errx(1, "trie_roa_add(%s, %u) failed",
				    print_prefix(&prefix), plen);
			set = NULL;
		} else {
			if (set == NULL) {
				if ((set = set_new(1, sizeof(rs))) == NULL)
					err(1, "set_new");
			}
			rs.as = as;
			rs.maxlen = max;
			if (set_add(set, &rs, 1) != 0)
				err(1, "set_add");
		}

		free(line);
	}
}

static void
test_file(FILE *in, struct trie_head *th)
{
	char *line;
	struct bgpd_addr prefix;
	u_int8_t plen;

	while ((line = fparseln(in, NULL, NULL, NULL, FPARSELN_UNESCALL))) {
		if (!host(line, &prefix, &plen))
			errx(1, "%s: could not parse prefix \"%s\"",
			    __func__, line);
		printf("%s/%u ", print_prefix(&prefix), plen);
		if (trie_match(th, &prefix, plen, orlonger))
			printf("MATCH\n");
		else
			printf("miss\n");
		free(line);
	}
}

static void
test_roa_file(FILE *in, struct trie_head *th)
{
	const char *errstr;
	char *line, *s;
	struct bgpd_addr prefix;
	u_int8_t plen;
	u_int32_t as;
	int r;

	while ((line = fparseln(in, NULL, NULL, NULL, FPARSELN_UNESCALL))) {
		s = strchr(line, ' ');
		if (s)
			*s++ = '\0';
		if (!host(line, &prefix, &plen))
			errx(1, "%s: could not parse prefix \"%s\"",
			    __func__, line);
		if (s)
			s = strstr(s, "source-as");
		if (s) {
			s += strlen("source-as");
			as = strtonum(s, 0, UINT_MAX, &errstr);
			if (errstr != NULL)
				errx(1, "source-as is %s: %s", errstr, s);
		} else
			as = 0;
		printf("%s/%u source-as %u is ",
		    print_prefix(&prefix), plen, as);
		r = trie_roa_check(th, &prefix, plen, as);
		switch (r) {
		case ROA_NOTFOUND:
			printf("not found\n");
			break;
		case ROA_VALID:
			printf("VALID\n");
			break;
		case ROA_INVALID:
			printf("invalid\n");
			break;
		default:
			printf("UNEXPECTED %d\n", r);
			break;
		}
		free(line);
	}
}

static void
usage(void)
{
        extern char *__progname;
	fprintf(stderr, "usage: %s [-or] prefixfile testfile\n", __progname);
	exit(1);
}

int
main(int argc, char **argv)
{
	struct trie_head th = { 0 };
	FILE *in, *tin;
	int ch;

	while ((ch = getopt(argc, argv, "or")) != -1) {
		switch (ch) {
		case 'o':
			orlonger = 1;
			break;
		case 'r':
			roa = 1;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 2)
		usage();

	in = fopen(argv[0], "r");
	if (in == NULL)
		err(1, "fopen(%s)", argv[0]);
	tin = fopen(argv[1], "r");
	if (tin == NULL)
		err(1, "fopen(%s)", argv[1]);

	if (roa)
		parse_roa_file(in, &th);
	else
		parse_file(in, &th);
	/* trie_dump(&th); */
	if (trie_equal(&th, &th) == 0)
		errx(1, "trie_equal failure");
	if (roa)
		test_roa_file(tin, &th);
	else
		test_file(tin, &th);

	trie_free(&th);
}