bgpd.c revision 1.106
1/* $OpenBSD: bgpd.c,v 1.106 2004/09/15 18:30:42 otto 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 imsg_init(&ibuf_se, pipe_m2s[0]); 215 imsg_init(&ibuf_rde, pipe_m2r[0]); 216 mrt_init(&ibuf_rde, &ibuf_se); 217 if ((rfd = kr_init(!(conf.flags & BGPD_FLAG_NO_FIB_UPDATE))) == -1) 218 quit = 1; 219 if (pftable_clear_all() != 0) 220 quit = 1; 221 222 while ((net = TAILQ_FIRST(&net_l)) != NULL) { 223 TAILQ_REMOVE(&net_l, net, entry); 224 free(net); 225 } 226 227 while ((r = TAILQ_FIRST(rules_l)) != NULL) { 228 TAILQ_REMOVE(rules_l, r, entry); 229 free(r); 230 } 231 232 while ((la = TAILQ_FIRST(conf.listen_addrs)) != NULL) { 233 TAILQ_REMOVE(conf.listen_addrs, la, entry); 234 close(la->fd); 235 free(la); 236 } 237 238 mrt_reconfigure(&mrt_l); 239 240 while (quit == 0) { 241 pfd[PFD_PIPE_SESSION].fd = ibuf_se.fd; 242 pfd[PFD_PIPE_SESSION].events = POLLIN; 243 if (ibuf_se.w.queued) 244 pfd[PFD_PIPE_SESSION].events |= POLLOUT; 245 pfd[PFD_PIPE_ROUTE].fd = ibuf_rde.fd; 246 pfd[PFD_PIPE_ROUTE].events = POLLIN; 247 if (ibuf_rde.w.queued) 248 pfd[PFD_PIPE_ROUTE].events |= POLLOUT; 249 pfd[PFD_SOCK_ROUTE].fd = rfd; 250 pfd[PFD_SOCK_ROUTE].events = POLLIN; 251 252 timeout = mrt_timeout(&mrt_l); 253 if (timeout > MAX_TIMEOUT) 254 timeout = MAX_TIMEOUT; 255 256 if ((nfds = poll(pfd, POLL_MAX, timeout * 1000)) == -1) 257 if (errno != EINTR) { 258 log_warn("poll error"); 259 quit = 1; 260 } 261 262 if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT)) 263 if (msgbuf_write(&ibuf_se.w) < 0) { 264 log_warn("pipe write error (to SE)"); 265 quit = 1; 266 } 267 268 if (nfds > 0 && (pfd[PFD_PIPE_ROUTE].revents & POLLOUT)) 269 if (msgbuf_write(&ibuf_rde.w) < 0) { 270 log_warn("pipe write error (to RDE)"); 271 quit = 1; 272 } 273 274 if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) { 275 nfds--; 276 if (dispatch_imsg(&ibuf_se, PFD_PIPE_SESSION) == -1) 277 quit = 1; 278 } 279 280 if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) { 281 nfds--; 282 if (dispatch_imsg(&ibuf_rde, PFD_PIPE_ROUTE) == -1) 283 quit = 1; 284 } 285 286 if (nfds > 0 && pfd[PFD_SOCK_ROUTE].revents & POLLIN) { 287 nfds--; 288 if (kr_dispatch_msg() == -1) 289 quit = 1; 290 } 291 292 if (reconfig) { 293 reconfig = 0; 294 log_info("rereading config"); 295 reconfigure(conffile, &conf, &mrt_l, &peer_l, rules_l); 296 } 297 298 if (sigchld) { 299 sigchld = 0; 300 if (check_child(io_pid, "session engine")) 301 quit = 1; 302 if (check_child(rde_pid, "route decision engine")) 303 quit = 1; 304 } 305 306 if (mrtdump == 1) { 307 mrtdump = 0; 308 mrt_handler(&mrt_l); 309 } 310 } 311 312 signal(SIGCHLD, SIG_IGN); 313 314 if (io_pid) 315 kill(io_pid, SIGTERM); 316 317 if (rde_pid) 318 kill(rde_pid, SIGTERM); 319 320 while ((p = peer_l) != NULL) { 321 peer_l = p->next; 322 free(p); 323 } 324 while ((m = LIST_FIRST(&mrt_l)) != NULL) { 325 LIST_REMOVE(m, entry); 326 free(m); 327 } 328 329 free(rules_l); 330 control_cleanup(); 331 kr_shutdown(); 332 pftable_clear_all(); 333 free(conf.listen_addrs); 334 335 do { 336 if ((pid = wait(NULL)) == -1 && 337 errno != EINTR && errno != ECHILD) 338 fatal("wait"); 339 } while (pid != -1 || (pid == -1 && errno == EINTR)); 340 341 log_info("Terminating"); 342 return (0); 343} 344 345int 346check_child(pid_t pid, const char *pname) 347{ 348 int status; 349 350 if (waitpid(pid, &status, WNOHANG) > 0) { 351 if (WIFEXITED(status)) { 352 log_warnx("Lost child: %s exited", pname); 353 return (1); 354 } 355 if (WIFSIGNALED(status)) { 356 log_warnx("Lost child: %s terminated; signal %d", 357 pname, WTERMSIG(status)); 358 return (1); 359 } 360 } 361 362 return (0); 363} 364 365int 366reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l, 367 struct peer **peer_l, struct filter_head *rules_l) 368{ 369 struct network_head net_l; 370 struct network *n; 371 struct peer *p; 372 struct filter_rule *r; 373 struct listen_addr *la; 374 375 if (parse_config(conffile, conf, mrt_l, peer_l, &net_l, rules_l)) { 376 log_warnx("config file %s has errors, not reloading", 377 conffile); 378 return (-1); 379 } 380 381 prepare_listeners(conf); 382 383 if (imsg_compose(&ibuf_se, IMSG_RECONF_CONF, 0, 384 conf, sizeof(struct bgpd_config)) == -1) 385 return (-1); 386 if (imsg_compose(&ibuf_rde, IMSG_RECONF_CONF, 0, 387 conf, sizeof(struct bgpd_config)) == -1) 388 return (-1); 389 for (p = *peer_l; p != NULL; p = p->next) 390 if (imsg_compose(&ibuf_se, IMSG_RECONF_PEER, p->conf.id, 391 &p->conf, sizeof(struct peer_config)) == -1) 392 return (-1); 393 while ((n = TAILQ_FIRST(&net_l)) != NULL) { 394 if (imsg_compose(&ibuf_rde, IMSG_NETWORK_ADD, 0, 395 &n->net, sizeof(struct network_config)) == -1) 396 return (-1); 397 TAILQ_REMOVE(&net_l, n, entry); 398 free(n); 399 } 400 while ((r = TAILQ_FIRST(rules_l)) != NULL) { 401 if (imsg_compose(&ibuf_rde, IMSG_RECONF_FILTER, 0, 402 r, sizeof(struct filter_rule)) == -1) 403 return (-1); 404 TAILQ_REMOVE(rules_l, r, entry); 405 free(r); 406 } 407 while ((la = TAILQ_FIRST(conf->listen_addrs)) != NULL) { 408 if (imsg_compose_fdpass(&ibuf_se, IMSG_RECONF_LISTENER, la->fd, 409 la, sizeof(struct listen_addr)) == -1) 410 return (-1); 411 TAILQ_REMOVE(conf->listen_addrs, la, entry); 412 free(la); 413 } 414 free(conf->listen_addrs); 415 conf->listen_addrs = NULL; 416 417 if (imsg_compose(&ibuf_se, IMSG_RECONF_DONE, 0, NULL, 0) == -1 || 418 imsg_compose(&ibuf_rde, IMSG_RECONF_DONE, 0, NULL, 0) == -1) 419 return (-1); 420 421 /* mrt changes can be sent out of bound */ 422 mrt_reconfigure(mrt_l); 423 return (0); 424} 425 426int 427dispatch_imsg(struct imsgbuf *ibuf, int idx) 428{ 429 struct imsg imsg; 430 int n; 431 432 if ((n = imsg_read(ibuf)) == -1) 433 return (-1); 434 435 if (n == 0) { /* connection closed */ 436 log_warnx("dispatch_imsg in main: pipe closed"); 437 return (-1); 438 } 439 440 for (;;) { 441 if ((n = imsg_get(ibuf, &imsg)) == -1) 442 return (-1); 443 444 if (n == 0) 445 break; 446 447 switch (imsg.hdr.type) { 448 case IMSG_KROUTE_CHANGE: 449 if (idx != PFD_PIPE_ROUTE) 450 log_warnx("route request not from RDE"); 451 else if (kr_change(imsg.data)) 452 return (-1); 453 break; 454 case IMSG_KROUTE_DELETE: 455 if (idx != PFD_PIPE_ROUTE) 456 log_warnx("route request not from RDE"); 457 else if (kr_delete(imsg.data)) 458 return (-1); 459 break; 460 case IMSG_NEXTHOP_ADD: 461 if (idx != PFD_PIPE_ROUTE) 462 log_warnx("nexthop request not from RDE"); 463 else 464 if (imsg.hdr.len != IMSG_HEADER_SIZE + 465 sizeof(struct bgpd_addr)) 466 log_warnx("wrong imsg len"); 467 else if (kr_nexthop_add(imsg.data) == -1) 468 return (-1); 469 break; 470 case IMSG_NEXTHOP_REMOVE: 471 if (idx != PFD_PIPE_ROUTE) 472 log_warnx("nexthop request not from RDE"); 473 else 474 if (imsg.hdr.len != IMSG_HEADER_SIZE + 475 sizeof(struct bgpd_addr)) 476 log_warnx("wrong imsg len"); 477 else 478 kr_nexthop_delete(imsg.data); 479 break; 480 case IMSG_PFTABLE_ADD: 481 if (idx != PFD_PIPE_ROUTE) 482 log_warnx("pftable request not from RDE"); 483 else 484 if (imsg.hdr.len != IMSG_HEADER_SIZE + 485 sizeof(struct pftable_msg)) 486 log_warnx("wrong imsg len"); 487 else if (pftable_addr_add(imsg.data) != 0) 488 return (-1); 489 break; 490 case IMSG_PFTABLE_REMOVE: 491 if (idx != PFD_PIPE_ROUTE) 492 log_warnx("pftable request not from RDE"); 493 else 494 if (imsg.hdr.len != IMSG_HEADER_SIZE + 495 sizeof(struct pftable_msg)) 496 log_warnx("wrong imsg len"); 497 else if (pftable_addr_remove(imsg.data) != 0) 498 return (-1); 499 break; 500 case IMSG_PFTABLE_COMMIT: 501 if (idx != PFD_PIPE_ROUTE) 502 log_warnx("pftable request not from RDE"); 503 else 504 if (imsg.hdr.len != IMSG_HEADER_SIZE) 505 log_warnx("wrong imsg len"); 506 else if (pftable_commit() != 0) 507 return (-1); 508 break; 509 case IMSG_CTL_RELOAD: 510 if (idx != PFD_PIPE_SESSION) 511 log_warnx("reload request not from SE"); 512 else 513 reconfig = 1; 514 break; 515 case IMSG_CTL_FIB_COUPLE: 516 if (idx != PFD_PIPE_SESSION) 517 log_warnx("couple request not from SE"); 518 else 519 kr_fib_couple(); 520 break; 521 case IMSG_CTL_FIB_DECOUPLE: 522 if (idx != PFD_PIPE_SESSION) 523 log_warnx("decouple request not from SE"); 524 else 525 kr_fib_decouple(); 526 break; 527 case IMSG_CTL_KROUTE: 528 case IMSG_CTL_KROUTE_ADDR: 529 case IMSG_CTL_SHOW_NEXTHOP: 530 case IMSG_CTL_SHOW_INTERFACE: 531 if (idx != PFD_PIPE_SESSION) 532 log_warnx("kroute request not from SE"); 533 else 534 kr_show_route(&imsg); 535 break; 536 default: 537 break; 538 } 539 imsg_free(&imsg); 540 } 541 return (0); 542} 543 544void 545send_nexthop_update(struct kroute_nexthop *msg) 546{ 547 char *gw = NULL; 548 549 if (msg->gateway.af) 550 if (asprintf(&gw, ": via %s", 551 log_addr(&msg->gateway)) == -1) { 552 log_warn("send_nexthop_update"); 553 quit = 1; 554 } 555 556 log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop), 557 msg->valid ? "valid" : "invalid", 558 msg->connected ? ": directly connected" : "", 559 msg->gateway.af ? gw : ""); 560 561 free(gw); 562 563 if (imsg_compose(&ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 564 msg, sizeof(struct kroute_nexthop)) == -1) 565 quit = 1; 566} 567 568void 569send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen) 570{ 571 imsg_compose_pid(&ibuf_se, type, pid, data, datalen); 572} 573