bgpd.c revision 1.107
1/* $OpenBSD: bgpd.c,v 1.107 2004/09/16 00:25:12 henning Exp $ */ 2 3/* 4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <sys/types.h> 20#include <sys/socket.h> 21#include <sys/wait.h> 22#include <netinet/in.h> 23#include <arpa/inet.h> 24#include <err.h> 25#include <errno.h> 26#include <fcntl.h> 27#include <poll.h> 28#include <pwd.h> 29#include <signal.h> 30#include <stdio.h> 31#include <stdlib.h> 32#include <string.h> 33#include <unistd.h> 34 35#include "mrt.h" 36#include "bgpd.h" 37#include "session.h" 38 39void sighdlr(int); 40void usage(void); 41int main(int, char *[]); 42int check_child(pid_t, const char *); 43int reconfigure(char *, struct bgpd_config *, struct mrt_head *, 44 struct peer **, struct filter_head *); 45int dispatch_imsg(struct imsgbuf *, int); 46 47int rfd = -1; 48volatile sig_atomic_t mrtdump = 0; 49volatile sig_atomic_t quit = 0; 50volatile sig_atomic_t reconfig = 0; 51volatile sig_atomic_t sigchld = 0; 52struct imsgbuf *ibuf_se; 53struct imsgbuf *ibuf_rde; 54 55void 56sighdlr(int sig) 57{ 58 switch (sig) { 59 case SIGTERM: 60 case SIGINT: 61 quit = 1; 62 break; 63 case SIGCHLD: 64 sigchld = 1; 65 break; 66 case SIGHUP: 67 reconfig = 1; 68 break; 69 case SIGALRM: 70 case SIGUSR1: 71 mrtdump = 1; 72 break; 73 } 74} 75 76void 77usage(void) 78{ 79 extern char *__progname; 80 81 fprintf(stderr, "usage: %s [-dnv] ", __progname); 82 fprintf(stderr, "[-D macro=value] [-f file]\n"); 83 exit(1); 84} 85 86#define PFD_PIPE_SESSION 0 87#define PFD_PIPE_ROUTE 1 88#define PFD_SOCK_ROUTE 2 89#define POLL_MAX 3 90#define MAX_TIMEOUT 3600 91 92int 93main(int argc, char *argv[]) 94{ 95 struct bgpd_config conf; 96 struct peer *peer_l, *p; 97 struct mrt_head mrt_l; 98 struct network_head net_l; 99 struct filter_head *rules_l; 100 struct network *net; 101 struct filter_rule *r; 102 struct mrt *m; 103 struct listen_addr *la; 104 struct pollfd pfd[POLL_MAX]; 105 pid_t io_pid = 0, rde_pid = 0, pid; 106 char *conffile; 107 int debug = 0; 108 int ch, nfds, timeout; 109 int pipe_m2s[2]; 110 int pipe_m2r[2]; 111 int pipe_s2r[2]; 112 113 conffile = CONFFILE; 114 bgpd_process = PROC_MAIN; 115 116 log_init(1); /* log to stderr until daemonized */ 117 118 if ((rules_l = calloc(1, sizeof(struct filter_head))) == NULL) 119 err(1, NULL); 120 121 bzero(&conf, sizeof(conf)); 122 LIST_INIT(&mrt_l); 123 TAILQ_INIT(&net_l); 124 TAILQ_INIT(rules_l); 125 peer_l = NULL; 126 127 while ((ch = getopt(argc, argv, "dD:f:nv")) != -1) { 128 switch (ch) { 129 case 'd': 130 debug = 1; 131 break; 132 case 'D': 133 if (cmdline_symset(optarg) < 0) 134 log_warnx("could not parse macro definition %s", 135 optarg); 136 break; 137 case 'f': 138 conffile = optarg; 139 break; 140 case 'n': 141 conf.opts |= BGPD_OPT_NOACTION; 142 break; 143 case 'v': 144 if (conf.opts & BGPD_OPT_VERBOSE) 145 conf.opts |= BGPD_OPT_VERBOSE2; 146 conf.opts |= BGPD_OPT_VERBOSE; 147 break; 148 default: 149 usage(); 150 /* NOTREACHED */ 151 } 152 } 153 154 if (parse_config(conffile, &conf, &mrt_l, &peer_l, &net_l, rules_l)) 155 exit(1); 156 157 if (conf.opts & BGPD_OPT_NOACTION) { 158 if (conf.opts & BGPD_OPT_VERBOSE) 159 print_config(&conf, &net_l, peer_l, rules_l, &mrt_l); 160 else 161 fprintf(stderr, "configuration OK\n"); 162 exit(0); 163 } 164 165 if (geteuid()) 166 errx(1, "need root privileges"); 167 168 if (getpwnam(BGPD_USER) == NULL) 169 errx(1, "unknown user %s", BGPD_USER); 170 endpwent(); 171 172 log_init(debug); 173 174 if (!debug) 175 daemon(1, 0); 176 177 log_info("startup"); 178 179 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2s) == -1) 180 fatal("socketpair"); 181 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2r) == -1) 182 fatal("socketpair"); 183 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r) == -1) 184 fatal("socketpair"); 185 session_socket_blockmode(pipe_m2s[0], BM_NONBLOCK); 186 session_socket_blockmode(pipe_m2s[1], BM_NONBLOCK); 187 session_socket_blockmode(pipe_m2r[0], BM_NONBLOCK); 188 session_socket_blockmode(pipe_m2r[1], BM_NONBLOCK); 189 session_socket_blockmode(pipe_s2r[0], BM_NONBLOCK); 190 session_socket_blockmode(pipe_s2r[1], BM_NONBLOCK); 191 192 prepare_listeners(&conf); 193 194 /* fork children */ 195 rde_pid = rde_main(&conf, peer_l, &net_l, rules_l, &mrt_l, 196 pipe_m2r, pipe_s2r, pipe_m2s); 197 io_pid = session_main(&conf, peer_l, &net_l, rules_l, &mrt_l, 198 pipe_m2s, pipe_s2r, pipe_m2r); 199 200 setproctitle("parent"); 201 202 signal(SIGTERM, sighdlr); 203 signal(SIGINT, sighdlr); 204 signal(SIGCHLD, sighdlr); 205 signal(SIGHUP, sighdlr); 206 signal(SIGALRM, sighdlr); 207 signal(SIGUSR1, sighdlr); 208 209 close(pipe_m2s[1]); 210 close(pipe_m2r[1]); 211 close(pipe_s2r[0]); 212 close(pipe_s2r[1]); 213 214 if ((ibuf_se = malloc(sizeof(struct imsgbuf))) == NULL || 215 (ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL) 216 fatal(NULL); 217 imsg_init(ibuf_se, pipe_m2s[0]); 218 imsg_init(ibuf_rde, pipe_m2r[0]); 219 mrt_init(ibuf_rde, ibuf_se); 220 if ((rfd = kr_init(!(conf.flags & BGPD_FLAG_NO_FIB_UPDATE))) == -1) 221 quit = 1; 222 if (pftable_clear_all() != 0) 223 quit = 1; 224 225 while ((net = TAILQ_FIRST(&net_l)) != NULL) { 226 TAILQ_REMOVE(&net_l, net, entry); 227 free(net); 228 } 229 230 while ((r = TAILQ_FIRST(rules_l)) != NULL) { 231 TAILQ_REMOVE(rules_l, r, entry); 232 free(r); 233 } 234 235 while ((la = TAILQ_FIRST(conf.listen_addrs)) != NULL) { 236 TAILQ_REMOVE(conf.listen_addrs, la, entry); 237 close(la->fd); 238 free(la); 239 } 240 241 mrt_reconfigure(&mrt_l); 242 243 while (quit == 0) { 244 pfd[PFD_PIPE_SESSION].fd = ibuf_se->fd; 245 pfd[PFD_PIPE_SESSION].events = POLLIN; 246 if (ibuf_se->w.queued) 247 pfd[PFD_PIPE_SESSION].events |= POLLOUT; 248 pfd[PFD_PIPE_ROUTE].fd = ibuf_rde->fd; 249 pfd[PFD_PIPE_ROUTE].events = POLLIN; 250 if (ibuf_rde->w.queued) 251 pfd[PFD_PIPE_ROUTE].events |= POLLOUT; 252 pfd[PFD_SOCK_ROUTE].fd = rfd; 253 pfd[PFD_SOCK_ROUTE].events = POLLIN; 254 255 timeout = mrt_timeout(&mrt_l); 256 if (timeout > MAX_TIMEOUT) 257 timeout = MAX_TIMEOUT; 258 259 if ((nfds = poll(pfd, POLL_MAX, timeout * 1000)) == -1) 260 if (errno != EINTR) { 261 log_warn("poll error"); 262 quit = 1; 263 } 264 265 if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT)) 266 if (msgbuf_write(&ibuf_se->w) < 0) { 267 log_warn("pipe write error (to SE)"); 268 quit = 1; 269 } 270 271 if (nfds > 0 && (pfd[PFD_PIPE_ROUTE].revents & POLLOUT)) 272 if (msgbuf_write(&ibuf_rde->w) < 0) { 273 log_warn("pipe write error (to RDE)"); 274 quit = 1; 275 } 276 277 if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) { 278 nfds--; 279 if (dispatch_imsg(ibuf_se, PFD_PIPE_SESSION) == -1) 280 quit = 1; 281 } 282 283 if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) { 284 nfds--; 285 if (dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE) == -1) 286 quit = 1; 287 } 288 289 if (nfds > 0 && pfd[PFD_SOCK_ROUTE].revents & POLLIN) { 290 nfds--; 291 if (kr_dispatch_msg() == -1) 292 quit = 1; 293 } 294 295 if (reconfig) { 296 reconfig = 0; 297 log_info("rereading config"); 298 reconfigure(conffile, &conf, &mrt_l, &peer_l, rules_l); 299 } 300 301 if (sigchld) { 302 sigchld = 0; 303 if (check_child(io_pid, "session engine")) 304 quit = 1; 305 if (check_child(rde_pid, "route decision engine")) 306 quit = 1; 307 } 308 309 if (mrtdump == 1) { 310 mrtdump = 0; 311 mrt_handler(&mrt_l); 312 } 313 } 314 315 signal(SIGCHLD, SIG_IGN); 316 317 if (io_pid) 318 kill(io_pid, SIGTERM); 319 320 if (rde_pid) 321 kill(rde_pid, SIGTERM); 322 323 while ((p = peer_l) != NULL) { 324 peer_l = p->next; 325 free(p); 326 } 327 while ((m = LIST_FIRST(&mrt_l)) != NULL) { 328 LIST_REMOVE(m, entry); 329 free(m); 330 } 331 332 free(rules_l); 333 control_cleanup(); 334 kr_shutdown(); 335 pftable_clear_all(); 336 free(conf.listen_addrs); 337 338 do { 339 if ((pid = wait(NULL)) == -1 && 340 errno != EINTR && errno != ECHILD) 341 fatal("wait"); 342 } while (pid != -1 || (pid == -1 && errno == EINTR)); 343 344 msgbuf_clear(&ibuf_se->w); 345 free(ibuf_se); 346 msgbuf_clear(&ibuf_rde->w); 347 free(ibuf_rde); 348 349 log_info("Terminating"); 350 return (0); 351} 352 353int 354check_child(pid_t pid, const char *pname) 355{ 356 int status; 357 358 if (waitpid(pid, &status, WNOHANG) > 0) { 359 if (WIFEXITED(status)) { 360 log_warnx("Lost child: %s exited", pname); 361 return (1); 362 } 363 if (WIFSIGNALED(status)) { 364 log_warnx("Lost child: %s terminated; signal %d", 365 pname, WTERMSIG(status)); 366 return (1); 367 } 368 } 369 370 return (0); 371} 372 373int 374reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l, 375 struct peer **peer_l, struct filter_head *rules_l) 376{ 377 struct network_head net_l; 378 struct network *n; 379 struct peer *p; 380 struct filter_rule *r; 381 struct listen_addr *la; 382 383 if (parse_config(conffile, conf, mrt_l, peer_l, &net_l, rules_l)) { 384 log_warnx("config file %s has errors, not reloading", 385 conffile); 386 return (-1); 387 } 388 389 prepare_listeners(conf); 390 391 if (imsg_compose(ibuf_se, IMSG_RECONF_CONF, 0, 392 conf, sizeof(struct bgpd_config)) == -1) 393 return (-1); 394 if (imsg_compose(ibuf_rde, IMSG_RECONF_CONF, 0, 395 conf, sizeof(struct bgpd_config)) == -1) 396 return (-1); 397 for (p = *peer_l; p != NULL; p = p->next) 398 if (imsg_compose(ibuf_se, IMSG_RECONF_PEER, p->conf.id, 399 &p->conf, sizeof(struct peer_config)) == -1) 400 return (-1); 401 while ((n = TAILQ_FIRST(&net_l)) != NULL) { 402 if (imsg_compose(ibuf_rde, IMSG_NETWORK_ADD, 0, 403 &n->net, sizeof(struct network_config)) == -1) 404 return (-1); 405 TAILQ_REMOVE(&net_l, n, entry); 406 free(n); 407 } 408 while ((r = TAILQ_FIRST(rules_l)) != NULL) { 409 if (imsg_compose(ibuf_rde, IMSG_RECONF_FILTER, 0, 410 r, sizeof(struct filter_rule)) == -1) 411 return (-1); 412 TAILQ_REMOVE(rules_l, r, entry); 413 free(r); 414 } 415 while ((la = TAILQ_FIRST(conf->listen_addrs)) != NULL) { 416 if (imsg_compose_fdpass(ibuf_se, IMSG_RECONF_LISTENER, la->fd, 417 la, sizeof(struct listen_addr)) == -1) 418 return (-1); 419 TAILQ_REMOVE(conf->listen_addrs, la, entry); 420 free(la); 421 } 422 free(conf->listen_addrs); 423 conf->listen_addrs = NULL; 424 425 if (imsg_compose(ibuf_se, IMSG_RECONF_DONE, 0, NULL, 0) == -1 || 426 imsg_compose(ibuf_rde, IMSG_RECONF_DONE, 0, NULL, 0) == -1) 427 return (-1); 428 429 /* mrt changes can be sent out of bound */ 430 mrt_reconfigure(mrt_l); 431 return (0); 432} 433 434int 435dispatch_imsg(struct imsgbuf *ibuf, int idx) 436{ 437 struct imsg imsg; 438 int n; 439 440 if ((n = imsg_read(ibuf)) == -1) 441 return (-1); 442 443 if (n == 0) { /* connection closed */ 444 log_warnx("dispatch_imsg in main: pipe closed"); 445 return (-1); 446 } 447 448 for (;;) { 449 if ((n = imsg_get(ibuf, &imsg)) == -1) 450 return (-1); 451 452 if (n == 0) 453 break; 454 455 switch (imsg.hdr.type) { 456 case IMSG_KROUTE_CHANGE: 457 if (idx != PFD_PIPE_ROUTE) 458 log_warnx("route request not from RDE"); 459 else if (kr_change(imsg.data)) 460 return (-1); 461 break; 462 case IMSG_KROUTE_DELETE: 463 if (idx != PFD_PIPE_ROUTE) 464 log_warnx("route request not from RDE"); 465 else if (kr_delete(imsg.data)) 466 return (-1); 467 break; 468 case IMSG_NEXTHOP_ADD: 469 if (idx != PFD_PIPE_ROUTE) 470 log_warnx("nexthop request not from RDE"); 471 else 472 if (imsg.hdr.len != IMSG_HEADER_SIZE + 473 sizeof(struct bgpd_addr)) 474 log_warnx("wrong imsg len"); 475 else if (kr_nexthop_add(imsg.data) == -1) 476 return (-1); 477 break; 478 case IMSG_NEXTHOP_REMOVE: 479 if (idx != PFD_PIPE_ROUTE) 480 log_warnx("nexthop request not from RDE"); 481 else 482 if (imsg.hdr.len != IMSG_HEADER_SIZE + 483 sizeof(struct bgpd_addr)) 484 log_warnx("wrong imsg len"); 485 else 486 kr_nexthop_delete(imsg.data); 487 break; 488 case IMSG_PFTABLE_ADD: 489 if (idx != PFD_PIPE_ROUTE) 490 log_warnx("pftable request not from RDE"); 491 else 492 if (imsg.hdr.len != IMSG_HEADER_SIZE + 493 sizeof(struct pftable_msg)) 494 log_warnx("wrong imsg len"); 495 else if (pftable_addr_add(imsg.data) != 0) 496 return (-1); 497 break; 498 case IMSG_PFTABLE_REMOVE: 499 if (idx != PFD_PIPE_ROUTE) 500 log_warnx("pftable request not from RDE"); 501 else 502 if (imsg.hdr.len != IMSG_HEADER_SIZE + 503 sizeof(struct pftable_msg)) 504 log_warnx("wrong imsg len"); 505 else if (pftable_addr_remove(imsg.data) != 0) 506 return (-1); 507 break; 508 case IMSG_PFTABLE_COMMIT: 509 if (idx != PFD_PIPE_ROUTE) 510 log_warnx("pftable request not from RDE"); 511 else 512 if (imsg.hdr.len != IMSG_HEADER_SIZE) 513 log_warnx("wrong imsg len"); 514 else if (pftable_commit() != 0) 515 return (-1); 516 break; 517 case IMSG_CTL_RELOAD: 518 if (idx != PFD_PIPE_SESSION) 519 log_warnx("reload request not from SE"); 520 else 521 reconfig = 1; 522 break; 523 case IMSG_CTL_FIB_COUPLE: 524 if (idx != PFD_PIPE_SESSION) 525 log_warnx("couple request not from SE"); 526 else 527 kr_fib_couple(); 528 break; 529 case IMSG_CTL_FIB_DECOUPLE: 530 if (idx != PFD_PIPE_SESSION) 531 log_warnx("decouple request not from SE"); 532 else 533 kr_fib_decouple(); 534 break; 535 case IMSG_CTL_KROUTE: 536 case IMSG_CTL_KROUTE_ADDR: 537 case IMSG_CTL_SHOW_NEXTHOP: 538 case IMSG_CTL_SHOW_INTERFACE: 539 if (idx != PFD_PIPE_SESSION) 540 log_warnx("kroute request not from SE"); 541 else 542 kr_show_route(&imsg); 543 break; 544 default: 545 break; 546 } 547 imsg_free(&imsg); 548 } 549 return (0); 550} 551 552void 553send_nexthop_update(struct kroute_nexthop *msg) 554{ 555 char *gw = NULL; 556 557 if (msg->gateway.af) 558 if (asprintf(&gw, ": via %s", 559 log_addr(&msg->gateway)) == -1) { 560 log_warn("send_nexthop_update"); 561 quit = 1; 562 } 563 564 log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop), 565 msg->valid ? "valid" : "invalid", 566 msg->connected ? ": directly connected" : "", 567 msg->gateway.af ? gw : ""); 568 569 free(gw); 570 571 if (imsg_compose(ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 572 msg, sizeof(struct kroute_nexthop)) == -1) 573 quit = 1; 574} 575 576void 577send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen) 578{ 579 imsg_compose_pid(ibuf_se, type, pid, data, datalen); 580} 581