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
|
/* $OpenBSD: mrt.c,v 1.14 2004/01/01 21:18:13 henning Exp $ */
/*
* Copyright (c) 2003 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 <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "bgpd.h"
#include "rde.h"
#include "session.h" /* needed for MSGSIZE_HEADER et al. */
#include "mrt.h"
/*
* XXX These functions break the imsg encapsulation.
* XXX The imsg API is way to basic, we need something like
* XXX imsg_create(), imsg_add(), imsg_close() ...
*/
static int mrt_dump_entry(struct mrt *, struct prefix *, u_int16_t,
struct peer_config *);
static void mrt_dump_header(struct buf *, u_int16_t, u_int16_t, u_int32_t);
static int mrt_open(struct mrtdump_config *);
#define DUMP_BYTE(x, b) \
do { \
u_char t = (b); \
if (buf_add((x), &t, sizeof(t)) == -1) \
fatalx("buf_add error"); \
} while (0)
#define DUMP_SHORT(x, s) \
do { \
u_int16_t t; \
t = htons((s)); \
if (buf_add((x), &t, sizeof(t)) == -1) \
fatalx("buf_add error"); \
} while (0)
#define DUMP_LONG(x, l) \
do { \
u_int32_t t; \
t = htonl((l)); \
if (buf_add((x), &t, sizeof(t)) == -1) \
fatalx("buf_add error"); \
} while (0)
#define DUMP_NLONG(x, l) \
do { \
u_int32_t t = (l); \
if (buf_add((x), &t, sizeof(t)) == -1) \
fatalx("buf_add error"); \
} while (0)
int
mrt_dump_bgp_msg(struct mrt *mrt, void *pkg, u_int16_t pkglen, int type,
struct peer_config *peer, struct bgpd_config *bgp)
{
struct buf *buf;
struct imsg_hdr hdr;
int i, n;
u_int16_t len;
len = pkglen + MRT_BGP4MP_HEADER_SIZE + type > 0 ? MSGSIZE_HEADER : 0;
hdr.len = len + IMSG_HEADER_SIZE + MRT_HEADER_SIZE;
hdr.type = IMSG_MRT_MSG;
hdr.peerid = mrt->id;
buf = buf_open(hdr.len);
if (buf == NULL)
fatal("mrt_dump_bgp_msg");
if (buf_add(buf, &hdr, sizeof(hdr)) == -1)
fatalx("buf_add error");
mrt_dump_header(buf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE, len);
DUMP_SHORT(buf, bgp->as);
DUMP_SHORT(buf, peer->remote_as);
DUMP_SHORT(buf, /* ifindex */ 0);
DUMP_SHORT(buf, 4);
DUMP_NLONG(buf, peer->local_addr.sin_addr.s_addr);
DUMP_NLONG(buf, peer->remote_addr.sin_addr.s_addr);
/* bgp header was chopped off so glue a new one together. */
if (type > 0) {
for (i = 0; i < MSGSIZE_HEADER_MARKER; i++)
DUMP_BYTE(buf, 0xff);
DUMP_SHORT(buf, pkglen + MSGSIZE_HEADER);
DUMP_BYTE(buf, type);
}
if (buf_add(buf, pkg, pkglen) == -1)
fatalx("buf_add error");
if ((n = buf_close(mrt->msgbuf, buf)) < 0)
fatalx("buf_close error");
return (n);
}
static int
mrt_dump_entry(struct mrt *mrt, struct prefix *p, u_int16_t snum,
struct peer_config *peer)
{
struct buf *buf;
void *bptr;
struct imsg_hdr hdr;
u_int16_t len, attr_len;
int n;
attr_len = attr_length(&p->aspath->flags);
len = MRT_DUMP_HEADER_SIZE + attr_len;
hdr.len = len + IMSG_HEADER_SIZE + MRT_HEADER_SIZE;
hdr.type = IMSG_MRT_MSG;
hdr.peerid = mrt->id;
buf = buf_open(hdr.len);
if (buf == NULL)
fatal("mrt_dump_entry");
if (buf_add(buf, &hdr, sizeof(hdr)) == -1)
fatalx("buf_add error");
mrt_dump_header(buf, MSG_TABLE_DUMP, AFI_IPv4, len);
DUMP_SHORT(buf, 0);
DUMP_SHORT(buf, snum);
DUMP_NLONG(buf, p->prefix->prefix.s_addr);
DUMP_BYTE(buf, p->prefix->prefixlen);
DUMP_BYTE(buf, 1);
DUMP_LONG(buf, p->lastchange); /* originated */
DUMP_NLONG(buf, peer->remote_addr.sin_addr.s_addr);
DUMP_SHORT(buf, peer->remote_as);
DUMP_SHORT(buf, attr_len);
if ((bptr = buf_reserve(buf, attr_len)) == NULL)
fatalx("buf_reserve error");
if (attr_dump(bptr, attr_len, &p->aspath->flags) == -1)
fatalx("attr_dump error");
if ((n = buf_close(mrt->msgbuf, buf)) < 0)
fatalx("buf_close error");
return (n);
}
static u_int16_t sequencenum = 0;
void
mrt_clear_seq(void)
{
sequencenum = 0;
}
void
mrt_dump_upcall(struct pt_entry *pt, void *ptr)
{
struct mrt *mrtbuf = ptr;
struct prefix *p;
/*
* dump all prefixes even the inactive ones. That is the way zebra
* dumps the table so we do the same. If only the active route should
* be dumped p should be set to p = pt->active.
*/
LIST_FOREACH(p, &pt->prefix_h, prefix_l)
mrt_dump_entry(mrtbuf, p, sequencenum++, &p->peer->conf);
}
static void
mrt_dump_header(struct buf *buf, u_int16_t type, u_int16_t subtype,
u_int32_t len)
{
struct mrt_header mrt;
time_t now;
now = time(NULL);
mrt.timestamp = htonl(now);
mrt.type = ntohs(type);
mrt.subtype = ntohs(subtype);
mrt.length = htonl(len);
if (buf_add(buf, &mrt, sizeof(mrt)) == -1)
fatalx("buf_add error");
}
static int
mrt_open(struct mrtdump_config *mconf)
{
time_t now;
now = time(NULL);
if (strftime(mconf->file, sizeof(mconf->file), mconf->name,
localtime(&now)) == 0) {
logit(LOG_CRIT, "mrt_open strftime failed");
mconf->msgbuf.sock = -1;
return -1;
}
mconf->msgbuf.sock = open(mconf->file,
O_WRONLY|O_NONBLOCK|O_CREAT|O_TRUNC, 0644);
if (mconf->msgbuf.sock == -1)
logit(LOG_CRIT, "mrt_open %s: %s",
mconf->file, strerror(errno));
return mconf->msgbuf.sock;
}
int
mrt_state(struct mrtdump_config *m, enum imsg_type type,
struct imsgbuf *buf)
{
switch (m->state) {
case MRT_STATE_DONE:
/* no dump expected */
return (0);
case MRT_STATE_CLOSE:
switch (type) {
case IMSG_NONE:
if (m->type == MRT_TABLE_DUMP)
imsg_compose(buf, IMSG_MRT_END, m->id, NULL, 0);
return (0);
case IMSG_MRT_END:
/* dump no longer valid */
close(m->msgbuf.sock);
LIST_REMOVE(m, list);
free(m);
return (0);
default:
break;
}
break;
case MRT_STATE_REOPEN:
switch (type) {
case IMSG_NONE:
if (m->type == MRT_TABLE_DUMP)
imsg_compose(buf, IMSG_MRT_END, m->id, NULL, 0);
return (0);
case IMSG_MRT_END:
if (m->msgbuf.sock != -1)
close(m->msgbuf.sock);
m->state = MRT_STATE_OPEN;
if (m->type == MRT_TABLE_DUMP)
imsg_compose(buf, IMSG_MRT_REQ, m->id, NULL, 0);
return (0);
default:
break;
}
break;
case MRT_STATE_OPEN:
switch (type) {
case IMSG_NONE:
if (m->type == MRT_TABLE_DUMP)
imsg_compose(buf, IMSG_MRT_REQ, m->id, NULL, 0);
return (0);
case IMSG_MRT_MSG:
mrt_open(m);
m->state = MRT_STATE_RUNNING;
break;
default:
return (0);
}
break;
case MRT_STATE_RUNNING:
if (type == IMSG_MRT_END) {
if (m->msgbuf.sock != -1)
close(m->msgbuf.sock);
m->msgbuf.sock = -1;
m->state = MRT_STATE_DONE;
return (0);
}
break;
}
return (1);
}
int
mrt_usr1(struct mrt_config *mconf, struct imsgbuf *buf)
{
struct mrtdump_config *m;
time_t now;
int interval = INT_MAX;
now = time(NULL);
LIST_FOREACH(m, mconf, list) {
if (m->type == MRT_TABLE_DUMP) {
switch (m->state) {
case MRT_STATE_REOPEN:
case MRT_STATE_CLOSE:
break;
case MRT_STATE_DONE:
m->state = MRT_STATE_OPEN;
imsg_compose(buf, IMSG_MRT_REQ, m->id, NULL, 0);
break;
default:
m->state = MRT_STATE_REOPEN;
imsg_compose(buf, IMSG_MRT_END, m->id, NULL, 0);
break;
}
m->ReopenTimer = now + m->ReopenTimerInterval;
}
if (m->ReopenTimer - now < interval)
interval = m->ReopenTimer - now;
}
if (interval != INT_MAX)
alarm(interval);
return (0);
}
int
mrt_alrm(struct mrt_config *mconf, struct imsgbuf *buf)
{
struct mrtdump_config *m;
time_t now;
int interval = INT_MAX;
now = time(NULL);
LIST_FOREACH(m, mconf, list) {
if (m->ReopenTimerInterval == 0)
continue;
if (m->ReopenTimer <= now) {
switch (m->state) {
case MRT_STATE_REOPEN:
case MRT_STATE_CLOSE:
break;
case MRT_STATE_DONE:
m->state = MRT_STATE_OPEN;
if (m->type == MRT_TABLE_DUMP)
imsg_compose(buf, IMSG_MRT_REQ, m->id,
NULL, 0);
break;
default:
m->state = MRT_STATE_REOPEN;
if (m->type == MRT_TABLE_DUMP)
imsg_compose(buf, IMSG_MRT_END, m->id,
NULL, 0);
break;
}
m->ReopenTimer = now + m->ReopenTimerInterval;
}
if (m->ReopenTimer - now < interval)
interval = m->ReopenTimer - now;
}
if (interval != INT_MAX)
alarm(interval);
return (0);
}
static u_int32_t max_id = 1;
static struct mrtdump_config *
getconf(struct mrt_config *c, struct mrtdump_config *m)
{
struct mrtdump_config *t;
LIST_FOREACH(t, c, list)
if (t->type == m->type)
return t;
return (NULL);
}
int
mrt_mergeconfig(struct mrt_config *xconf, struct mrt_config *nconf)
{
struct mrtdump_config *m, *xm;
time_t now;
int interval = INT_MAX;
now = time(NULL);
LIST_FOREACH(m, nconf, list)
if ((xm = getconf(xconf, m)) == NULL) {
/* NEW */
if ((xm = calloc(1, sizeof(struct mrtdump_config))) ==
NULL)
fatal("mrt_mergeconfig");
memcpy(xm, m, sizeof(struct mrtdump_config));
xm->id = max_id++;
if (xm->ReopenTimerInterval != 0) {
xm->ReopenTimer = now + xm->ReopenTimerInterval;
interval = xm->ReopenTimerInterval < interval ?
xm->ReopenTimerInterval : interval;
}
xm->state=MRT_STATE_OPEN;
LIST_INSERT_HEAD(xconf, xm, list);
} else {
/* MERGE */
if (strlcpy(xm->name, m->name, sizeof(xm->name)) >
sizeof(xm->name))
fatalx("mrt_mergeconfig: strlcpy");
xm->ReopenTimerInterval = m->ReopenTimerInterval;
if (xm->ReopenTimerInterval != 0) {
xm->ReopenTimer = now + xm->ReopenTimerInterval;
interval = xm->ReopenTimerInterval < interval ?
xm->ReopenTimerInterval : interval;
}
xm->state=MRT_STATE_REOPEN;
}
LIST_FOREACH(xm, xconf, list)
if (getconf(nconf, xm) == NULL)
/* REMOVE */
xm->state = MRT_STATE_CLOSE;
/* free config */
for (m = LIST_FIRST(nconf); m != LIST_END(nconf); m = xm) {
xm = LIST_NEXT(m, list);
free(m);
}
if (interval != INT_MAX)
alarm(interval);
return (0);
}
|