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
|
/* $OpenBSD: bgpd.c,v 1.81 2004/03/01 16:53:48 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 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 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/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mrt.h"
#include "bgpd.h"
#include "session.h"
void sighdlr(int);
void usage(void);
int main(int, char *[]);
int check_child(pid_t, const char *);
int reconfigure(char *, struct bgpd_config *, struct mrt_head *,
struct peer **, struct filter_head *);
int dispatch_imsg(struct imsgbuf *, int, struct mrt_head *);
int rfd = -1;
volatile sig_atomic_t mrtdump = 0;
volatile sig_atomic_t quit = 0;
volatile sig_atomic_t reconfig = 0;
volatile sig_atomic_t sigchld = 0;
struct imsgbuf ibuf_se;
struct imsgbuf ibuf_rde;
void
sighdlr(int sig)
{
switch (sig) {
case SIGTERM:
case SIGINT:
quit = 1;
break;
case SIGCHLD:
sigchld = 1;
break;
case SIGHUP:
reconfig = 1;
break;
case SIGALRM:
case SIGUSR1:
mrtdump = 1;
break;
}
}
void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-dnv] ", __progname);
fprintf(stderr, "[-D macro=value] [-f file]\n");
exit(1);
}
#define POLL_MAX 8
#define PFD_PIPE_SESSION 0
#define PFD_PIPE_ROUTE 1
#define PFD_SOCK_ROUTE 2
#define PFD_MRT_START 3
int
main(int argc, char *argv[])
{
struct bgpd_config conf;
struct peer *peer_l;
struct mrt_head mrt_l;
struct network_head net_l;
struct filter_head *rules_l;
struct network *net;
struct filter_rule *r;
struct mrt *(mrt[POLL_MAX]);
struct pollfd pfd[POLL_MAX];
pid_t io_pid = 0, rde_pid = 0, pid;
char *conffile;
int debug = 0;
int ch, csock, i, j, n, nfds, timeout;
int pipe_m2s[2];
int pipe_m2r[2];
int pipe_s2r[2];
conffile = CONFFILE;
bgpd_process = PROC_MAIN;
log_init(1); /* log to stderr until daemonized */
if ((rules_l = calloc(1, sizeof(struct filter_head))) == NULL)
err(1, NULL);
bzero(&conf, sizeof(conf));
LIST_INIT(&mrt_l);
TAILQ_INIT(&net_l);
TAILQ_INIT(rules_l);
peer_l = NULL;
while ((ch = getopt(argc, argv, "dD:f:nv")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 'D':
if (cmdline_symset(optarg) < 0)
log_warnx("could not parse macro definition %s",
optarg);
break;
case 'f':
conffile = optarg;
break;
case 'n':
conf.opts |= BGPD_OPT_NOACTION;
break;
case 'v':
if (conf.opts & BGPD_OPT_VERBOSE)
conf.opts |= BGPD_OPT_VERBOSE2;
conf.opts |= BGPD_OPT_VERBOSE;
break;
default:
usage();
/* NOTREACHED */
}
}
if (parse_config(conffile, &conf, &mrt_l, &peer_l, &net_l, rules_l))
exit(1);
if (conf.opts & BGPD_OPT_NOACTION) {
if (conf.opts & BGPD_OPT_VERBOSE)
print_config(&conf, &net_l, peer_l, rules_l, &mrt_l);
else
fprintf(stderr, "configuration OK\n");
exit(0);
}
if (geteuid())
errx(1, "need root privileges");
if (getpwnam(BGPD_USER) == NULL)
errx(1, "unknown user %s", BGPD_USER);
endpwent();
log_init(debug);
if (!debug)
daemon(1, 0);
log_info("startup");
if (pipe(pipe_m2s) == -1)
fatal("pipe");
if (fcntl(pipe_m2s[0], F_SETFL, O_NONBLOCK) == -1 ||
fcntl(pipe_m2s[1], F_SETFL, O_NONBLOCK) == -1)
fatal("fcntl");
if (pipe(pipe_m2r) == -1)
fatal("pipe");
if (fcntl(pipe_m2r[0], F_SETFL, O_NONBLOCK) == -1 ||
fcntl(pipe_m2r[1], F_SETFL, O_NONBLOCK) == -1)
fatal("fcntl");
if (pipe(pipe_s2r) == -1)
fatal("pipe");
if (fcntl(pipe_s2r[0], F_SETFL, O_NONBLOCK) == -1 ||
fcntl(pipe_s2r[1], F_SETFL, O_NONBLOCK) == -1)
fatal("fcntl");
if ((csock = control_init()) == -1)
fatalx("control socket setup failed");
/* fork children */
rde_pid = rde_main(&conf, peer_l, &net_l, rules_l, pipe_m2r, pipe_s2r);
io_pid = session_main(&conf, peer_l, pipe_m2s, pipe_s2r);
setproctitle("parent");
signal(SIGTERM, sighdlr);
signal(SIGINT, sighdlr);
signal(SIGCHLD, sighdlr);
signal(SIGHUP, sighdlr);
signal(SIGALRM, sighdlr);
signal(SIGUSR1, sighdlr);
close(pipe_m2s[1]);
close(pipe_m2r[1]);
close(pipe_s2r[0]);
close(pipe_s2r[1]);
close(csock);
imsg_init(&ibuf_se, pipe_m2s[0]);
imsg_init(&ibuf_rde, pipe_m2r[0]);
mrt_init(&ibuf_rde, &ibuf_se);
if ((rfd = kr_init(!(conf.flags & BGPD_FLAG_NO_FIB_UPDATE))) == -1)
quit = 1;
while ((net = TAILQ_FIRST(&net_l)) != NULL) {
TAILQ_REMOVE(&net_l, net, network_l);
free(net);
}
while ((r = TAILQ_FIRST(rules_l)) != NULL) {
TAILQ_REMOVE(rules_l, r, entries);
free(r);
}
while (quit == 0) {
pfd[PFD_PIPE_SESSION].fd = ibuf_se.sock;
pfd[PFD_PIPE_SESSION].events = POLLIN;
if (ibuf_se.w.queued)
pfd[PFD_PIPE_SESSION].events |= POLLOUT;
pfd[PFD_PIPE_ROUTE].fd = ibuf_rde.sock;
pfd[PFD_PIPE_ROUTE].events = POLLIN;
if (ibuf_rde.w.queued)
pfd[PFD_PIPE_ROUTE].events |= POLLOUT;
pfd[PFD_SOCK_ROUTE].fd = rfd;
pfd[PFD_SOCK_ROUTE].events = POLLIN;
i = PFD_MRT_START;
i = mrt_select(&mrt_l, pfd, mrt, i, POLL_MAX, &timeout);
if ((nfds = poll(pfd, i, INFTIM)) == -1)
if (errno != EINTR) {
log_warn("poll error");
quit = 1;
}
if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT))
if ((n = msgbuf_write(&ibuf_se.w)) < 0) {
log_warn("pipe write error (to SE)");
quit = 1;
}
if (nfds > 0 && (pfd[PFD_PIPE_ROUTE].revents & POLLOUT))
if ((n = msgbuf_write(&ibuf_rde.w)) < 0) {
log_warn("pipe write error (to RDE)");
quit = 1;
}
if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) {
nfds--;
if (dispatch_imsg(&ibuf_se, PFD_PIPE_SESSION,
&mrt_l) == -1)
quit = 1;
}
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
nfds--;
if (dispatch_imsg(&ibuf_rde, PFD_PIPE_ROUTE,
&mrt_l) == -1)
quit = 1;
}
if (nfds > 0 && pfd[PFD_SOCK_ROUTE].revents & POLLIN) {
nfds--;
if (kr_dispatch_msg() == -1)
quit = 1;
}
for (j = PFD_MRT_START; j < i && nfds > 0 ; j++) {
if (pfd[j].revents & POLLOUT) {
if ((n = mrt_write(mrt[j])) < 0) {
log_warn("mrt write error");
}
}
}
if (reconfig) {
log_info("rereading config");
reconfigure(conffile, &conf, &mrt_l, &peer_l, rules_l);
reconfig = 0;
}
if (sigchld) {
if (check_child(io_pid, "session engine"))
quit = 1;
if (check_child(rde_pid, "route decision engine"))
quit = 1;
sigchld = 0;
}
if (mrtdump == 1) {
mrt_handler(&mrt_l);
mrtdump = 0;
}
}
signal(SIGCHLD, SIG_IGN);
if (io_pid)
kill(io_pid, SIGTERM);
if (rde_pid)
kill(rde_pid, SIGTERM);
do {
pid = waitpid(-1, NULL, WNOHANG);
} while (pid > 0 || (pid == -1 && errno == EINTR));
free(rules_l);
control_cleanup();
kr_shutdown();
log_info("Terminating");
return (0);
}
int
check_child(pid_t pid, const char *pname)
{
int status;
if (waitpid(pid, &status, WNOHANG) > 0) {
if (WIFEXITED(status)) {
log_warnx("Lost child: %s exited", pname);
return (1);
}
if (WIFSIGNALED(status)) {
log_warnx("Lost child: %s terminated; signal %d",
pname, WTERMSIG(status));
return (1);
}
}
return (0);
}
int
reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l,
struct peer **peer_l, struct filter_head *rules_l)
{
struct network_head net_l;
struct network *n;
struct peer *p;
struct filter_rule *r;
if (parse_config(conffile, conf, mrt_l, peer_l, &net_l, rules_l)) {
log_warnx("config file %s has errors, not reloading",
conffile);
return (-1);
}
if (imsg_compose(&ibuf_se, IMSG_RECONF_CONF, 0,
conf, sizeof(struct bgpd_config)) == -1)
return (-1);
if (imsg_compose(&ibuf_rde, IMSG_RECONF_CONF, 0,
conf, sizeof(struct bgpd_config)) == -1)
return (-1);
for (p = *peer_l; p != NULL; p = p->next) {
if (imsg_compose(&ibuf_se, IMSG_RECONF_PEER, p->conf.id,
&p->conf, sizeof(struct peer_config)) == -1)
return (-1);
if (imsg_compose(&ibuf_rde, IMSG_RECONF_PEER, p->conf.id,
&p->conf, sizeof(struct peer_config)) == -1)
return (-1);
}
while ((n = TAILQ_FIRST(&net_l)) != NULL) {
if (imsg_compose(&ibuf_rde, IMSG_RECONF_NETWORK, 0,
&n->net, sizeof(struct network_config)) == -1)
return (-1);
TAILQ_REMOVE(&net_l, n, network_l);
free(n);
}
while ((r = TAILQ_FIRST(rules_l)) != NULL) {
if (imsg_compose(&ibuf_rde, IMSG_RECONF_FILTER, 0,
r, sizeof(struct filter_rule)) == -1)
return (-1);
TAILQ_REMOVE(rules_l, r, entries);
free(r);
}
if (imsg_compose(&ibuf_se, IMSG_RECONF_DONE, 0, NULL, 0) == -1 ||
imsg_compose(&ibuf_rde, IMSG_RECONF_DONE, 0, NULL, 0) == -1)
return (-1);
return (0);
}
int
dispatch_imsg(struct imsgbuf *ibuf, int idx, struct mrt_head *mrt_l)
{
struct imsg imsg;
int n;
if ((n = imsg_read(ibuf)) == -1)
return (-1);
if (n == 0) { /* connection closed */
log_warnx("dispatch_imsg in main: pipe closed");
return (-1);
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
return (-1);
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_MRT_MSG:
case IMSG_MRT_END:
if (mrt_queue(mrt_l, &imsg) == -1)
log_warnx("mrt_queue failed.");
break;
case IMSG_KROUTE_CHANGE:
if (idx != PFD_PIPE_ROUTE)
log_warnx("route request not from RDE");
else if (kr_change(imsg.data))
return (-1);
break;
case IMSG_KROUTE_DELETE:
if (idx != PFD_PIPE_ROUTE)
log_warnx("route request not from RDE");
else if (kr_delete(imsg.data))
return (-1);
break;
case IMSG_NEXTHOP_ADD:
if (idx != PFD_PIPE_ROUTE)
log_warnx("nexthop request not from RDE");
else
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct bgpd_addr))
log_warnx("wrong imsg len");
else if (kr_nexthop_add(imsg.data) == -1)
return (-1);
break;
case IMSG_NEXTHOP_REMOVE:
if (idx != PFD_PIPE_ROUTE)
log_warnx("nexthop request not from RDE");
else
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct bgpd_addr))
log_warnx("wrong imsg len");
else kr_nexthop_delete(imsg.data);
break;
case IMSG_CTL_RELOAD:
if (idx != PFD_PIPE_SESSION)
log_warnx("reload request not from SE");
else
reconfig = 1;
break;
case IMSG_CTL_FIB_COUPLE:
if (idx != PFD_PIPE_SESSION)
log_warnx("couple request not from SE");
else
kr_fib_couple();
break;
case IMSG_CTL_FIB_DECOUPLE:
if (idx != PFD_PIPE_SESSION)
log_warnx("decouple request not from SE");
else
kr_fib_decouple();
break;
case IMSG_CTL_KROUTE:
case IMSG_CTL_KROUTE_ADDR:
case IMSG_CTL_SHOW_NEXTHOP:
case IMSG_CTL_SHOW_INTERFACE:
if (idx != PFD_PIPE_SESSION)
log_warnx("kroute request not from SE");
else
kr_show_route(&imsg);
break;
default:
break;
}
imsg_free(&imsg);
}
return (0);
}
void
send_nexthop_update(struct kroute_nexthop *msg)
{
char *gw = NULL;
if (msg->gateway.af)
if (asprintf(&gw, ": via %s",
log_addr(&msg->gateway)) == -1) {
log_warn("send_nexthop_update");
quit = 1;
}
log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop),
msg->valid ? "valid" : "invalid",
msg->connected ? ": directly connected" : "",
msg->gateway.af ? gw : "");
free(gw);
if (imsg_compose(&ibuf_rde, IMSG_NEXTHOP_UPDATE, 0,
msg, sizeof(struct kroute_nexthop)) == -1)
quit = 1;
}
void
send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen)
{
imsg_compose_pid(&ibuf_se, type, pid, data, datalen);
}
|