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
|
/* $OpenBSD: bgpd.c,v 1.7 2003/12/20 20:53:30 henning Exp $ */
/*
* Copyright (c) 2003 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 <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mrt.h"
#include "bgpd.h"
void sighdlr(int);
void usage(void);
int main(int, char *[]);
int reconfigure(char *, int, int *, int, int *, struct bgpd_config *,
struct mrt_config *);
int dispatch_imsg(int, int, struct mrt_config *, int, int *);
int mrtfd = -1;
volatile sig_atomic_t mrtdump = 0;
volatile sig_atomic_t quit = 0;
volatile sig_atomic_t reconfig = 0;
void
sighdlr(int sig)
{
switch (sig) {
case SIGTERM:
case SIGKILL:
case SIGINT:
case SIGCHLD:
quit = 1;
break;
case SIGHUP:
reconfig = 1;
break;
case SIGALRM:
mrtdump = 1;
break;
case SIGUSR1:
mrtdump = 2;
break;
}
}
void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-dv] ", __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_MRT_START 2
int
main(int argc, char *argv[])
{
struct bgpd_config conf;
struct mrt_config mrtconf;
struct mrtdump_config *mconf, *(mrt[POLL_MAX]);
struct pollfd pfd[POLL_MAX];
pid_t io_pid = 0, rde_pid = 0;
char *conffile;
int debug = 0;
int ch, i, j, n, nfds;
int pipe_m2s[2];
int pipe_m2r[2];
int pipe_s2r[2];
int m2s_writes_queued = 0;
int m2r_writes_queued = 0;
conffile = CONFFILE;
bgpd_process = PROC_MAIN;
log_init(1); /* log to stderr until daemonized */
if (geteuid())
fatal("need root privileges", 0);
bzero(&conf, sizeof(conf));
bzero(&mrtconf, sizeof(mrtconf));
LIST_INIT(&mrtconf);
while ((ch = getopt(argc, argv, "dD:f:v")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 'D':
if (cmdline_symset(optarg) < 0)
logit(LOG_CRIT,
"could not parse macro definition %s",
optarg);
break;
case 'f':
conffile = optarg;
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, &mrtconf))
fatal("config file has errors", 0);
signal(SIGTERM, sighdlr);
signal(SIGKILL, sighdlr);
signal(SIGINT, sighdlr);
signal(SIGCHLD, sighdlr);
signal(SIGHUP, sighdlr);
signal(SIGALRM, sighdlr);
signal(SIGUSR1, sighdlr);
log_init(debug);
if (!debug)
daemon(1, 0);
logit(LOG_INFO, "startup");
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2s) == -1)
fatal("socketpair", errno);
if (fcntl(pipe_m2s[0], F_SETFL, O_NONBLOCK) == -1 ||
fcntl(pipe_m2s[1], F_SETFL, O_NONBLOCK) == -1)
fatal("fcntl", errno);
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2r) == -1)
fatal("socketpair", errno);
if (fcntl(pipe_m2r[0], F_SETFL, O_NONBLOCK) == -1 ||
fcntl(pipe_m2r[1], F_SETFL, O_NONBLOCK) == -1)
fatal("fcntl", errno);
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r) == -1)
fatal("socketpair", errno);
if (fcntl(pipe_s2r[0], F_SETFL, O_NONBLOCK) == -1 ||
fcntl(pipe_s2r[1], F_SETFL, O_NONBLOCK) == -1)
fatal("fcntl", errno);
if ((rde_pid = rde_main(&conf, pipe_m2r, pipe_s2r)) < 0)
fatal("could not start route decision engine", 0);
if ((io_pid = session_main(&conf, pipe_m2s, pipe_s2r)) < 0)
fatal("could not start session engine", 0);
setproctitle("parent");
close(pipe_m2s[1]);
close(pipe_m2r[1]);
close(pipe_s2r[0]);
close(pipe_s2r[1]);
init_imsg_buf();
while (quit == 0) {
pfd[PFD_PIPE_SESSION].fd = pipe_m2s[0];
pfd[PFD_PIPE_SESSION].events = POLLIN;
if (m2s_writes_queued)
pfd[PFD_PIPE_SESSION].events |= POLLOUT;
pfd[PFD_PIPE_ROUTE].fd = pipe_m2r[0];
pfd[PFD_PIPE_ROUTE].events = POLLIN;
if (m2r_writes_queued)
pfd[PFD_PIPE_ROUTE].events |= POLLOUT;
i = PFD_MRT_START;
LIST_FOREACH(mconf, &mrtconf, list)
if (mconf->queued_writes) {
pfd[i].fd = mconf->fd;
pfd[i].events |= POLLOUT;
mrt[i++] = mconf;
}
if ((nfds = poll(pfd, 2, INFTIM)) == -1)
if (errno != EINTR)
fatal("poll error", errno);
if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT) &&
m2s_writes_queued) {
if ((n = buf_sock_write(pfd[PFD_PIPE_SESSION].fd)) ==
-1)
fatal("pipe write error", errno);
m2s_writes_queued -= n;
}
if (nfds > 0 && (pfd[PFD_PIPE_ROUTE].revents & POLLOUT) &&
m2r_writes_queued) {
if ((n = buf_sock_write(pfd[PFD_PIPE_ROUTE].fd)) == -1)
fatal("pipe write error", errno);
m2r_writes_queued -= n;
}
if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) {
nfds--;
dispatch_imsg(pfd[PFD_PIPE_SESSION].fd, pipe_m2s[0],
&mrtconf, pipe_m2r[0], &m2r_writes_queued);
}
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
nfds--;
dispatch_imsg(pfd[PFD_PIPE_ROUTE].fd, pipe_m2r[0],
&mrtconf, pipe_m2r[0], &m2r_writes_queued);
}
for (j = PFD_MRT_START; j < i && nfds > 0 ; j++) {
if ((pfd[j].revents & POLLOUT) &&
mrt[i]->queued_writes) {
if ((n = buf_sock_write(pfd[i].fd)) == -1)
fatal("pipe write error", errno);
mrt[i]->queued_writes -= n;
}
}
if (reconfig) {
logit(LOG_CRIT, "rereading config");
reconfigure(conffile, pipe_m2s[0], &m2s_writes_queued,
pipe_m2r[0], &m2r_writes_queued, &conf, &mrtconf);
LIST_FOREACH(mconf, &mrtconf, list)
mrt_state(mconf, IMSG_NONE, pipe_m2r[0],
&m2r_writes_queued);
reconfig = 0;
}
if (mrtdump == 1) {
mrt_alrm(&mrtconf, pipe_m2r[0], &m2r_writes_queued);
mrtdump = 0;
} else if (mrtdump == 2) {
mrt_usr1(&mrtconf, pipe_m2r[0], &m2r_writes_queued);
mrtdump = 0;
}
}
signal(SIGCHLD, SIG_IGN);
if (io_pid)
kill(io_pid, SIGTERM);
if (rde_pid)
kill(rde_pid, SIGTERM);
do {
i = waitpid(-1, NULL, WNOHANG);
} while (i > 0 || (i == -1 && errno == EINTR));
logit(LOG_CRIT, "Terminating");
return (0);
}
int
reconfigure(char *conffile, int se_fd, int *se_waiting, int rde_fd,
int *rde_waiting, struct bgpd_config *conf, struct mrt_config *mrtc)
{
struct peer *p;
if (parse_config(conffile, conf, mrtc)) {
logit(LOG_CRIT, "config file %s has errors, not reloading",
conffile);
return (-1);
}
*se_waiting += imsg_compose(se_fd, IMSG_RECONF_CONF, 0,
(u_char *)conf, sizeof(struct bgpd_config));
*rde_waiting += imsg_compose(rde_fd, IMSG_RECONF_CONF, 0,
(u_char *)conf, sizeof(struct bgpd_config));
for (p = conf->peers; p != NULL; p = p->next) {
*se_waiting += imsg_compose(se_fd, IMSG_RECONF_PEER,
p->conf.id, (u_char *)&p->conf, sizeof(struct peer_config));
*rde_waiting += imsg_compose(rde_fd, IMSG_RECONF_PEER,
p->conf.id, (u_char *)&p->conf, sizeof(struct peer_config));
}
*se_waiting += imsg_compose(se_fd, IMSG_RECONF_DONE, 0, NULL, 0);
*rde_waiting += imsg_compose(rde_fd, IMSG_RECONF_DONE, 0, NULL, 0);
return (0);
}
/*
* XXX currently messages are only buffered for mrt files.
*/
int
dispatch_imsg(int fd, int idx, struct mrt_config *conf,
int rfd, int *rwait /*, int sfd, int *swait */)
{
struct buf *wbuf;
struct mrtdump_config *m;
struct imsg imsg;
ssize_t len;
int n;
if (get_imsg(fd, &imsg) > 0) {
switch (imsg.hdr.type) {
case IMSG_MRT_MSG:
case IMSG_MRT_END:
LIST_FOREACH(m, conf, list) {
if (m->id != imsg.hdr.peerid)
continue;
if (mrt_state(m, imsg.hdr.type,
rfd, rwait) == 0)
break;
if (m->fd == -1)
break;
len = imsg.hdr.len - IMSG_HEADER_SIZE;
wbuf = buf_open(NULL, m->fd, len);
if (wbuf == NULL)
fatal("buf_open error", 0);
if (buf_add(wbuf, imsg.data, len) == -1)
fatal("buf_add error", 0);
if ((n = buf_close(wbuf)) == -1)
fatal("buf_close error", 0);
m->queued_writes += n;
break;
}
break;
default:
break;
}
imsg_free(&imsg);
}
return (0);
}
|