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
|
/* $OpenBSD: irr_prefix.c,v 1.21 2015/10/05 14:18:33 deraadt Exp $ */
/*
* Copyright (c) 2007 Henning Brauer <henning@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 MIND, 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 <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "irrfilter.h"
#include "bgpd.h"
void prefixset_aggregate(struct prefix_set *);
int prefix_aggregate(struct irr_prefix *, const struct irr_prefix *);
int irr_prefix_cmp(const void *, const void *);
int prefix_set_compare(struct prefix_set *, struct prefix_set *);
struct prefix_set
*prefix_set_find(char *);
RB_HEAD(prefix_set_h, prefix_set) prefix_set_h;
RB_PROTOTYPE(prefix_set_h, prefix_set, entry, prefix_set_compare)
RB_GENERATE(prefix_set_h, prefix_set, entry, prefix_set_compare)
struct prefix_set *curpfxs = NULL;
struct prefix_set *
prefixset_get(char *as)
{
struct prefix_set *pfxs;
if ((pfxs = prefix_set_find(as)) != NULL)
return (pfxs);
/* nothing found, resolve and store */
if ((pfxs = calloc(1, sizeof(*pfxs))) == NULL)
err(1, "get_prefixset calloc");
if ((pfxs->as = strdup(as)) == NULL)
err(1, "get_prefixset strdup");
RB_INSERT(prefix_set_h, &prefix_set_h, pfxs);
if (irrverbose >= 3) {
fprintf(stdout, "query routes for %s... ", as);
fflush(stdout);
}
curpfxs = pfxs;
if ((irrflags & F_IPV4) && whois(as, QTYPE_ROUTE) == -1)
errx(1, "whois error, prefixset_get %s", as);
if ((irrflags & F_IPV6) && whois(as, QTYPE_ROUTE6) == -1)
errx(1, "whois error, prefixset_get %s", as);
if (whois(as, QTYPE_ROUTE6) == -1)
errx(1, "whois error, prefixset_get %s", as);
curpfxs = NULL;
if (irrverbose >= 3)
fprintf(stdout, "done\n");
prefixset_aggregate(pfxs);
return (pfxs);
}
int
prefixset_addmember(char *s)
{
void *p;
u_int i;
struct irr_prefix *pfx;
int len, ret;
char *slash;
const char *errstr;
if ((slash = strchr(s, '/')) == NULL) {
fprintf(stderr, "%s: prefix %s does not have the len "
"specified, ignoring\n", curpfxs->as, s);
return (0);
}
if ((pfx = calloc(1, sizeof(*pfx))) == NULL)
err(1, "prefixset_addmember calloc");
if ((len = inet_net_pton(AF_INET, s, &pfx->addr.in,
sizeof(pfx->addr.in))) != -1) {
pfx->af = AF_INET;
} else {
len = strtonum(slash + 1, 0, 128, &errstr);
if (errstr)
errx(1, "prefixset_addmember %s prefix %s: prefixlen "
"is %s", curpfxs->as, s, errstr);
*slash = '\0';
if ((ret = inet_pton(AF_INET6, s, &pfx->addr.in6)) == -1)
err(1, "prefixset_addmember %s prefix \"%s\"",
curpfxs->as, s);
else if (ret == 0) {
fprintf(stderr, "prefixset_addmember %s prefix \"%s\": "
"No matching address family found", curpfxs->as, s);
free(pfx);
return (0);
}
pfx->af = AF_INET6;
}
pfx->len = pfx->maxlen = len;
/* yes, there are dupes... e. g. from multiple sources */
for (i = 0; i < curpfxs->prefixcnt; i++)
if (irr_prefix_cmp(&curpfxs->prefix[i], &pfx) == 0) {
free(pfx);
return (0);
}
if ((p = reallocarray(curpfxs->prefix,
curpfxs->prefixcnt + 1, sizeof(void *))) == NULL)
err(1, "prefixset_addmember realloc");
curpfxs->prefix = p;
curpfxs->prefixcnt++;
curpfxs->prefix[curpfxs->prefixcnt - 1] = pfx;
return (1);
}
void
prefixset_aggregate(struct prefix_set *pfxs)
{
u_int i, cnt, newcnt;
int res;
struct irr_prefix *cur, *last;
void *p;
qsort(pfxs->prefix, pfxs->prefixcnt, sizeof(void *), irr_prefix_cmp);
cnt = pfxs->prefixcnt;
do {
last = cur = NULL;
for (i = 0, newcnt = 0; i < cnt; i++) {
cur = pfxs->prefix[i];
if (last != NULL && last->af == cur->af) {
if (cur->af == AF_INET)
res = prefix_aggregate(last, cur);
else
res = 0;
if (res == 1) { /* cur is covered by last */
if (cur->len > last->maxlen)
last->maxlen = cur->len;
free(pfxs->prefix[i]);
pfxs->prefix[i] = cur = NULL;
}
}
if (cur != NULL) {
pfxs->prefix[newcnt++] = cur;
last = cur;
}
}
cnt = newcnt;
} while (newcnt < i);
if (newcnt == pfxs->prefixcnt)
return;
if (irrverbose >= 2)
printf("%s: prefix aggregation: %u -> %u\n",
pfxs->as, pfxs->prefixcnt, newcnt);
if ((p = reallocarray(pfxs->prefix, newcnt, sizeof(void *))) == NULL)
err(1, "prefixset_aggregate realloc");
pfxs->prefix = p;
pfxs->prefixcnt = newcnt;
}
int
prefix_aggregate(struct irr_prefix *a, const struct irr_prefix *b)
{
in_addr_t mask;
struct in6_addr ma;
struct in6_addr mb;
if (a->len == 0)
return (1);
if (a->af != b->af)
/* We cannot aggregate addresses of different families. */
return (0);
if (a->af == AF_INET) {
mask = htonl(prefixlen2mask(a->len));
if ((a->addr.in.s_addr & mask) == (b->addr.in.s_addr & mask))
return (1);
} else if (a->af == AF_INET6) {
inet6applymask(&ma, &a->addr.in6, a->len);
inet6applymask(&mb, &b->addr.in6, a->len);
if (IN6_ARE_ADDR_EQUAL(&ma, &mb))
return (1);
}
/* see whether we can fold them in one */
if (a->len == b->len && a->len > 1) {
if (a->af == AF_INET) {
mask = htonl(prefixlen2mask(a->len - 1));
if ((a->addr.in.s_addr & mask) ==
(b->addr.in.s_addr & mask)) {
a->len--;
a->addr.in.s_addr &= mask;
return (1);
}
} else if (a->af == AF_INET6) {
inet6applymask(&ma, &a->addr.in6, a->len - 1);
inet6applymask(&mb, &b->addr.in6, a->len - 1);
if (IN6_ARE_ADDR_EQUAL(&ma, &mb)) {
a->len--;
memcpy(&a->addr.in6, &ma, sizeof(ma));
return (1);
}
}
}
return (0);
}
int
irr_prefix_cmp(const void *a, const void *b)
{
const struct irr_prefix *pa;
const struct irr_prefix *pb;
int r;
pa = *((const struct irr_prefix * const *)a);
pb = *((const struct irr_prefix * const *)b);
if ((r = pa->af - pb->af) != 0)
return (r);
if (pa->af == AF_INET) {
if (ntohl(pa->addr.in.s_addr) <
ntohl(pb->addr.in.s_addr))
return (-1);
if (ntohl(pa->addr.in.s_addr) >
ntohl(pb->addr.in.s_addr))
return (1);
} else if (pa->af == AF_INET6) {
for (r = 0; r < 16; r++) {
if (pa->addr.in6.s6_addr[r] < pb->addr.in6.s6_addr[r])
return (-1);
if (pa->addr.in6.s6_addr[r] > pb->addr.in6.s6_addr[r])
return (1);
}
} else
errx(1, "irr_prefix_cmp unknown af %u", pa->af);
if ((r = pa->len - pb->len) != 0)
return (r);
return (0);
}
/* RB helpers */
int
prefix_set_compare(struct prefix_set *a, struct prefix_set *b)
{
return (strcmp(a->as, b->as));
}
struct prefix_set *
prefix_set_find(char *as)
{
struct prefix_set s;
s.as = as;
return (RB_FIND(prefix_set_h, &prefix_set_h, &s));
}
|