bgpd.c revision 1.173
1/* $OpenBSD: bgpd.c,v 1.173 2013/11/13 09:14:48 florian 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 "bgpd.h" 36#include "mrt.h" 37#include "session.h" 38 39void sighdlr(int); 40__dead void usage(void); 41int main(int, char *[]); 42int check_child(pid_t, const char *); 43int send_filterset(struct imsgbuf *, struct filter_set_head *); 44int reconfigure(char *, struct bgpd_config *, struct mrt_head *, 45 struct peer **); 46int dispatch_imsg(struct imsgbuf *, int, struct bgpd_config *); 47int control_setup(struct bgpd_config *); 48 49int rfd = -1; 50int cflags; 51volatile sig_atomic_t mrtdump; 52volatile sig_atomic_t quit; 53volatile sig_atomic_t sigchld; 54volatile sig_atomic_t reconfig; 55pid_t reconfpid; 56int reconfpending; 57struct imsgbuf *ibuf_se; 58struct imsgbuf *ibuf_rde; 59struct rib_names ribnames = SIMPLEQ_HEAD_INITIALIZER(ribnames); 60char *cname; 61char *rcname; 62 63void 64sighdlr(int sig) 65{ 66 switch (sig) { 67 case SIGTERM: 68 case SIGINT: 69 quit = 1; 70 break; 71 case SIGCHLD: 72 sigchld = 1; 73 break; 74 case SIGHUP: 75 reconfig = 1; 76 break; 77 case SIGALRM: 78 case SIGUSR1: 79 mrtdump = 1; 80 break; 81 } 82} 83 84__dead void 85usage(void) 86{ 87 extern char *__progname; 88 89 fprintf(stderr, "usage: %s [-cdnv] [-D macro=value] [-f file]\n", 90 __progname); 91 exit(1); 92} 93 94#define PFD_PIPE_SESSION 0 95#define PFD_PIPE_ROUTE 1 96#define PFD_SOCK_ROUTE 2 97#define POLL_MAX 3 98#define MAX_TIMEOUT 3600 99 100int 101main(int argc, char *argv[]) 102{ 103 struct bgpd_config conf; 104 struct mrt_head mrt_l; 105 struct peer *peer_l, *p; 106 struct mrt *m; 107 struct listen_addr *la; 108 struct pollfd pfd[POLL_MAX]; 109 pid_t io_pid = 0, rde_pid = 0, pid; 110 char *conffile; 111 int debug = 0; 112 int ch, timeout, nfds; 113 int pipe_m2s[2]; 114 int pipe_m2r[2]; 115 int pipe_s2r[2]; 116 int pipe_s2r_c[2]; 117 118 conffile = CONFFILE; 119 bgpd_process = PROC_MAIN; 120 121 log_init(1); /* log to stderr until daemonized */ 122 log_verbose(1); 123 124 bzero(&conf, sizeof(conf)); 125 LIST_INIT(&mrt_l); 126 peer_l = NULL; 127 128 while ((ch = getopt(argc, argv, "cdD:f:nv")) != -1) { 129 switch (ch) { 130 case 'c': 131 conf.opts |= BGPD_OPT_FORCE_DEMOTE; 132 break; 133 case 'd': 134 debug = 1; 135 break; 136 case 'D': 137 if (cmdline_symset(optarg) < 0) 138 log_warnx("could not parse macro definition %s", 139 optarg); 140 break; 141 case 'f': 142 conffile = optarg; 143 break; 144 case 'n': 145 conf.opts |= BGPD_OPT_NOACTION; 146 break; 147 case 'v': 148 if (conf.opts & BGPD_OPT_VERBOSE) 149 conf.opts |= BGPD_OPT_VERBOSE2; 150 conf.opts |= BGPD_OPT_VERBOSE; 151 log_verbose(1); 152 break; 153 default: 154 usage(); 155 /* NOTREACHED */ 156 } 157 } 158 159 argc -= optind; 160 argv += optind; 161 if (argc > 0) 162 usage(); 163 164 if (conf.opts & BGPD_OPT_NOACTION) { 165 struct network_head net_l; 166 struct rdomain_head rdom_l; 167 struct filter_head rules_l; 168 169 if (parse_config(conffile, &conf, &mrt_l, &peer_l, &net_l, 170 &rules_l, &rdom_l)) 171 exit(1); 172 173 if (conf.opts & BGPD_OPT_VERBOSE) 174 print_config(&conf, &ribnames, &net_l, peer_l, &rules_l, 175 &mrt_l, &rdom_l); 176 else 177 fprintf(stderr, "configuration OK\n"); 178 exit(0); 179 } 180 181 if (geteuid()) 182 errx(1, "need root privileges"); 183 184 if (getpwnam(BGPD_USER) == NULL) 185 errx(1, "unknown user %s", BGPD_USER); 186 187 log_init(debug); 188 log_verbose(conf.opts & BGPD_OPT_VERBOSE); 189 190 if (!debug) 191 daemon(1, 0); 192 193 log_info("startup"); 194 195 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2s) == -1) 196 fatal("socketpair"); 197 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2r) == -1) 198 fatal("socketpair"); 199 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r) == -1) 200 fatal("socketpair"); 201 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r_c) == -1) 202 fatal("socketpair"); 203 session_socket_blockmode(pipe_m2s[0], BM_NONBLOCK); 204 session_socket_blockmode(pipe_m2s[1], BM_NONBLOCK); 205 session_socket_blockmode(pipe_m2r[0], BM_NONBLOCK); 206 session_socket_blockmode(pipe_m2r[1], BM_NONBLOCK); 207 session_socket_blockmode(pipe_s2r[0], BM_NONBLOCK); 208 session_socket_blockmode(pipe_s2r[1], BM_NONBLOCK); 209 session_socket_blockmode(pipe_s2r_c[0], BM_NONBLOCK); 210 session_socket_blockmode(pipe_s2r_c[1], BM_NONBLOCK); 211 212 /* fork children */ 213 rde_pid = rde_main(pipe_m2r, pipe_s2r, pipe_m2s, pipe_s2r_c, debug); 214 io_pid = session_main(pipe_m2s, pipe_s2r, pipe_m2r, pipe_s2r_c); 215 216 setproctitle("parent"); 217 218 signal(SIGTERM, sighdlr); 219 signal(SIGINT, sighdlr); 220 signal(SIGCHLD, sighdlr); 221 signal(SIGHUP, sighdlr); 222 signal(SIGALRM, sighdlr); 223 signal(SIGUSR1, sighdlr); 224 signal(SIGPIPE, SIG_IGN); 225 226 close(pipe_m2s[1]); 227 close(pipe_m2r[1]); 228 close(pipe_s2r[0]); 229 close(pipe_s2r[1]); 230 231 if ((ibuf_se = malloc(sizeof(struct imsgbuf))) == NULL || 232 (ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL) 233 fatal(NULL); 234 imsg_init(ibuf_se, pipe_m2s[0]); 235 imsg_init(ibuf_rde, pipe_m2r[0]); 236 mrt_init(ibuf_rde, ibuf_se); 237 if ((rfd = kr_init()) == -1) 238 quit = 1; 239 quit = reconfigure(conffile, &conf, &mrt_l, &peer_l); 240 if (pftable_clear_all() != 0) 241 quit = 1; 242 243 while (quit == 0) { 244 bzero(pfd, sizeof(pfd)); 245 pfd[PFD_PIPE_SESSION].fd = ibuf_se->fd; 246 pfd[PFD_PIPE_SESSION].events = POLLIN; 247 if (ibuf_se->w.queued) 248 pfd[PFD_PIPE_SESSION].events |= POLLOUT; 249 pfd[PFD_PIPE_ROUTE].fd = ibuf_rde->fd; 250 pfd[PFD_PIPE_ROUTE].events = POLLIN; 251 if (ibuf_rde->w.queued) 252 pfd[PFD_PIPE_ROUTE].events |= POLLOUT; 253 pfd[PFD_SOCK_ROUTE].fd = rfd; 254 pfd[PFD_SOCK_ROUTE].events = POLLIN; 255 256 timeout = mrt_timeout(&mrt_l); 257 if (timeout > MAX_TIMEOUT) 258 timeout = MAX_TIMEOUT; 259 260 if ((nfds = poll(pfd, POLL_MAX, timeout * 1000)) == -1) 261 if (errno != EINTR) { 262 log_warn("poll error"); 263 quit = 1; 264 } 265 266 if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLOUT) 267 if (msgbuf_write(&ibuf_se->w) < 0) { 268 log_warn("pipe write error (to SE)"); 269 quit = 1; 270 } 271 272 if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLOUT) 273 if (msgbuf_write(&ibuf_rde->w) < 0) { 274 log_warn("pipe write error (to RDE)"); 275 quit = 1; 276 } 277 278 if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) { 279 if (dispatch_imsg(ibuf_se, PFD_PIPE_SESSION, &conf) == 280 -1) 281 quit = 1; 282 } 283 284 if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) { 285 if (dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE, &conf) == 286 -1) 287 quit = 1; 288 } 289 290 if (nfds > 0 && pfd[PFD_SOCK_ROUTE].revents & POLLIN) { 291 if (kr_dispatch_msg() == -1) 292 quit = 1; 293 } 294 295 if (reconfig) { 296 u_int error; 297 298 reconfig = 0; 299 switch (reconfigure(conffile, &conf, &mrt_l, &peer_l)) { 300 case -1: /* fatal error */ 301 quit = 1; 302 break; 303 case 0: /* all OK */ 304 error = 0; 305 break; 306 case 2: 307 error = CTL_RES_PENDING; 308 break; 309 default: /* parse error */ 310 error = CTL_RES_PARSE_ERROR; 311 break; 312 } 313 if (reconfpid != 0) { 314 send_imsg_session(IMSG_CTL_RESULT, reconfpid, 315 &error, sizeof(error)); 316 reconfpid = 0; 317 } 318 } 319 320 if (sigchld) { 321 sigchld = 0; 322 if (check_child(io_pid, "session engine")) { 323 quit = 1; 324 io_pid = 0; 325 } 326 if (check_child(rde_pid, "route decision engine")) { 327 quit = 1; 328 rde_pid = 0; 329 } 330 } 331 332 if (mrtdump) { 333 mrtdump = 0; 334 mrt_handler(&mrt_l); 335 } 336 } 337 338 signal(SIGCHLD, SIG_IGN); 339 340 if (io_pid) 341 kill(io_pid, SIGTERM); 342 343 if (rde_pid) 344 kill(rde_pid, SIGTERM); 345 346 while ((p = peer_l) != NULL) { 347 peer_l = p->next; 348 free(p); 349 } 350 while ((m = LIST_FIRST(&mrt_l)) != NULL) { 351 LIST_REMOVE(m, entry); 352 free(m); 353 } 354 if (conf.listen_addrs) 355 while ((la = TAILQ_FIRST(conf.listen_addrs)) != NULL) { 356 TAILQ_REMOVE(conf.listen_addrs, la, entry); 357 close(la->fd); 358 free(la); 359 } 360 361 control_cleanup(conf.csock); 362 control_cleanup(conf.rcsock); 363 carp_demote_shutdown(); 364 kr_shutdown(conf.fib_priority); 365 pftable_clear_all(); 366 free(conf.listen_addrs); 367 368 do { 369 if ((pid = wait(NULL)) == -1 && 370 errno != EINTR && errno != ECHILD) 371 fatal("wait"); 372 } while (pid != -1 || (pid == -1 && errno == EINTR)); 373 374 msgbuf_clear(&ibuf_se->w); 375 free(ibuf_se); 376 msgbuf_clear(&ibuf_rde->w); 377 free(ibuf_rde); 378 free(rcname); 379 free(cname); 380 381 log_info("Terminating"); 382 return (0); 383} 384 385int 386check_child(pid_t pid, const char *pname) 387{ 388 int status; 389 390 if (waitpid(pid, &status, WNOHANG) > 0) { 391 if (WIFEXITED(status)) { 392 log_warnx("Lost child: %s exited", pname); 393 return (1); 394 } 395 if (WIFSIGNALED(status)) { 396 log_warnx("Lost child: %s terminated; signal %d", 397 pname, WTERMSIG(status)); 398 return (1); 399 } 400 } 401 402 return (0); 403} 404 405int 406send_filterset(struct imsgbuf *i, struct filter_set_head *set) 407{ 408 struct filter_set *s; 409 410 TAILQ_FOREACH(s, set, entry) 411 if (imsg_compose(i, IMSG_FILTER_SET, 0, 0, -1, s, 412 sizeof(struct filter_set)) == -1) 413 return (-1); 414 return (0); 415} 416 417int 418reconfigure(char *conffile, struct bgpd_config *conf, struct mrt_head *mrt_l, 419 struct peer **peer_l) 420{ 421 struct network_head net_l; 422 struct rdomain_head rdom_l; 423 struct filter_head rules_l; 424 struct peer *p; 425 struct filter_rule *r; 426 struct listen_addr *la; 427 struct rde_rib *rr; 428 struct rdomain *rd; 429 430 if (reconfpending) { 431 log_info("previous reload still running"); 432 return (2); 433 } 434 reconfpending = 2; /* one per child */ 435 436 log_info("rereading config"); 437 if (parse_config(conffile, conf, mrt_l, peer_l, &net_l, &rules_l, 438 &rdom_l)) { 439 log_warnx("config file %s has errors, not reloading", 440 conffile); 441 reconfpending = 0; 442 return (1); 443 } 444 445 cflags = conf->flags; 446 prepare_listeners(conf); 447 448 /* start reconfiguration */ 449 if (imsg_compose(ibuf_se, IMSG_RECONF_CONF, 0, 0, -1, 450 conf, sizeof(struct bgpd_config)) == -1) 451 return (-1); 452 if (imsg_compose(ibuf_rde, IMSG_RECONF_CONF, 0, 0, -1, 453 conf, sizeof(struct bgpd_config)) == -1) 454 return (-1); 455 456 TAILQ_FOREACH(la, conf->listen_addrs, entry) { 457 if (imsg_compose(ibuf_se, IMSG_RECONF_LISTENER, 0, 0, la->fd, 458 la, sizeof(struct listen_addr)) == -1) 459 return (-1); 460 la->fd = -1; 461 } 462 463 if (control_setup(conf) == -1) 464 return (-1); 465 466 /* adjust fib syncing on reload */ 467 ktable_preload(); 468 469 /* RIBs for the RDE */ 470 while ((rr = SIMPLEQ_FIRST(&ribnames))) { 471 SIMPLEQ_REMOVE_HEAD(&ribnames, entry); 472 if (ktable_update(rr->rtableid, rr->name, NULL, 473 rr->flags, conf->fib_priority) == -1) { 474 log_warnx("failed to load rdomain %d", 475 rr->rtableid); 476 return (-1); 477 } 478 if (imsg_compose(ibuf_rde, IMSG_RECONF_RIB, 0, 0, -1, 479 rr, sizeof(struct rde_rib)) == -1) 480 return (-1); 481 free(rr); 482 } 483 484 /* send peer list to the SE */ 485 for (p = *peer_l; p != NULL; p = p->next) { 486 if (imsg_compose(ibuf_se, IMSG_RECONF_PEER, p->conf.id, 0, -1, 487 &p->conf, sizeof(struct peer_config)) == -1) 488 return (-1); 489 } 490 491 /* networks go via kroute to the RDE */ 492 if (kr_net_reload(0, &net_l)) 493 return (-1); 494 495 /* filters for the RDE */ 496 while ((r = TAILQ_FIRST(&rules_l)) != NULL) { 497 TAILQ_REMOVE(&rules_l, r, entry); 498 if (imsg_compose(ibuf_rde, IMSG_RECONF_FILTER, 0, 0, -1, 499 r, sizeof(struct filter_rule)) == -1) 500 return (-1); 501 if (send_filterset(ibuf_rde, &r->set) == -1) 502 return (-1); 503 filterset_free(&r->set); 504 free(r); 505 } 506 507 while ((rd = SIMPLEQ_FIRST(&rdom_l)) != NULL) { 508 SIMPLEQ_REMOVE_HEAD(&rdom_l, entry); 509 if (ktable_update(rd->rtableid, rd->descr, rd->ifmpe, 510 rd->flags, conf->fib_priority) == -1) { 511 log_warnx("failed to load rdomain %d", 512 rd->rtableid); 513 return (-1); 514 } 515 /* networks go via kroute to the RDE */ 516 if (kr_net_reload(rd->rtableid, &rd->net_l)) 517 return (-1); 518 519 if (imsg_compose(ibuf_rde, IMSG_RECONF_RDOMAIN, 0, 0, -1, 520 rd, sizeof(*rd)) == -1) 521 return (-1); 522 523 /* export targets */ 524 if (imsg_compose(ibuf_rde, IMSG_RECONF_RDOMAIN_EXPORT, 0, 0, 525 -1, NULL, 0) == -1) 526 return (-1); 527 if (send_filterset(ibuf_rde, &rd->export) == -1) 528 return (-1); 529 filterset_free(&rd->export); 530 531 /* import targets */ 532 if (imsg_compose(ibuf_rde, IMSG_RECONF_RDOMAIN_IMPORT, 0, 0, 533 -1, NULL, 0) == -1) 534 return (-1); 535 if (send_filterset(ibuf_rde, &rd->import) == -1) 536 return (-1); 537 filterset_free(&rd->import); 538 539 if (imsg_compose(ibuf_rde, IMSG_RECONF_RDOMAIN_DONE, 0, 0, 540 -1, NULL, 0) == -1) 541 return (-1); 542 543 free(rd); 544 } 545 546 /* signal the SE first then the RDE to activate the new config */ 547 if (imsg_compose(ibuf_se, IMSG_RECONF_DONE, 0, 0, -1, NULL, 0) == -1) 548 return (-1); 549 550 /* mrt changes can be sent out of bound */ 551 mrt_reconfigure(mrt_l); 552 return (0); 553} 554 555int 556dispatch_imsg(struct imsgbuf *ibuf, int idx, struct bgpd_config *conf) 557{ 558 struct imsg imsg; 559 ssize_t n; 560 int rv, verbose; 561 562 if ((n = imsg_read(ibuf)) == -1) 563 return (-1); 564 565 if (n == 0) { /* connection closed */ 566 log_warnx("dispatch_imsg in main: pipe closed"); 567 return (-1); 568 } 569 570 rv = 0; 571 for (;;) { 572 if ((n = imsg_get(ibuf, &imsg)) == -1) 573 return (-1); 574 575 if (n == 0) 576 break; 577 578 switch (imsg.hdr.type) { 579 case IMSG_KROUTE_CHANGE: 580 if (idx != PFD_PIPE_ROUTE) 581 log_warnx("route request not from RDE"); 582 else if (imsg.hdr.len != IMSG_HEADER_SIZE + 583 sizeof(struct kroute_full)) 584 log_warnx("wrong imsg len"); 585 else if (kr_change(imsg.hdr.peerid, imsg.data, 586 conf->fib_priority)) 587 rv = -1; 588 break; 589 case IMSG_KROUTE_DELETE: 590 if (idx != PFD_PIPE_ROUTE) 591 log_warnx("route request not from RDE"); 592 else if (imsg.hdr.len != IMSG_HEADER_SIZE + 593 sizeof(struct kroute_full)) 594 log_warnx("wrong imsg len"); 595 else if (kr_delete(imsg.hdr.peerid, imsg.data, 596 conf->fib_priority)) 597 rv = -1; 598 break; 599 case IMSG_NEXTHOP_ADD: 600 if (idx != PFD_PIPE_ROUTE) 601 log_warnx("nexthop request not from RDE"); 602 else if (imsg.hdr.len != IMSG_HEADER_SIZE + 603 sizeof(struct bgpd_addr)) 604 log_warnx("wrong imsg len"); 605 else if (kr_nexthop_add(imsg.hdr.peerid, imsg.data) == 606 -1) 607 rv = -1; 608 break; 609 case IMSG_NEXTHOP_REMOVE: 610 if (idx != PFD_PIPE_ROUTE) 611 log_warnx("nexthop request not from RDE"); 612 else if (imsg.hdr.len != IMSG_HEADER_SIZE + 613 sizeof(struct bgpd_addr)) 614 log_warnx("wrong imsg len"); 615 else 616 kr_nexthop_delete(imsg.hdr.peerid, imsg.data); 617 break; 618 case IMSG_PFTABLE_ADD: 619 if (idx != PFD_PIPE_ROUTE) 620 log_warnx("pftable request not from RDE"); 621 else 622 if (imsg.hdr.len != IMSG_HEADER_SIZE + 623 sizeof(struct pftable_msg)) 624 log_warnx("wrong imsg len"); 625 else if (pftable_addr_add(imsg.data) != 0) 626 rv = -1; 627 break; 628 case IMSG_PFTABLE_REMOVE: 629 if (idx != PFD_PIPE_ROUTE) 630 log_warnx("pftable request not from RDE"); 631 else 632 if (imsg.hdr.len != IMSG_HEADER_SIZE + 633 sizeof(struct pftable_msg)) 634 log_warnx("wrong imsg len"); 635 else if (pftable_addr_remove(imsg.data) != 0) 636 rv = -1; 637 break; 638 case IMSG_PFTABLE_COMMIT: 639 if (idx != PFD_PIPE_ROUTE) 640 log_warnx("pftable request not from RDE"); 641 else 642 if (imsg.hdr.len != IMSG_HEADER_SIZE) 643 log_warnx("wrong imsg len"); 644 else if (pftable_commit() != 0) 645 rv = -1; 646 break; 647 case IMSG_CTL_RELOAD: 648 if (idx != PFD_PIPE_SESSION) 649 log_warnx("reload request not from SE"); 650 else { 651 reconfig = 1; 652 reconfpid = imsg.hdr.pid; 653 } 654 break; 655 case IMSG_CTL_FIB_COUPLE: 656 if (idx != PFD_PIPE_SESSION) 657 log_warnx("couple request not from SE"); 658 else 659 kr_fib_couple(imsg.hdr.peerid, 660 conf->fib_priority); 661 break; 662 case IMSG_CTL_FIB_DECOUPLE: 663 if (idx != PFD_PIPE_SESSION) 664 log_warnx("decouple request not from SE"); 665 else 666 kr_fib_decouple(imsg.hdr.peerid, 667 conf->fib_priority); 668 break; 669 case IMSG_CTL_KROUTE: 670 case IMSG_CTL_KROUTE_ADDR: 671 case IMSG_CTL_SHOW_NEXTHOP: 672 case IMSG_CTL_SHOW_INTERFACE: 673 case IMSG_CTL_SHOW_FIB_TABLES: 674 if (idx != PFD_PIPE_SESSION) 675 log_warnx("kroute request not from SE"); 676 else 677 kr_show_route(&imsg); 678 break; 679 case IMSG_IFINFO: 680 if (idx != PFD_PIPE_SESSION) 681 log_warnx("IFINFO request not from SE"); 682 else if (imsg.hdr.len != IMSG_HEADER_SIZE + IFNAMSIZ) 683 log_warnx("IFINFO request with wrong len"); 684 else 685 kr_ifinfo(imsg.data); 686 break; 687 case IMSG_DEMOTE: 688 if (idx != PFD_PIPE_SESSION) 689 log_warnx("demote request not from SE"); 690 else if (imsg.hdr.len != IMSG_HEADER_SIZE + 691 sizeof(struct demote_msg)) 692 log_warnx("DEMOTE request with wrong len"); 693 else { 694 struct demote_msg *msg; 695 696 msg = imsg.data; 697 carp_demote_set(msg->demote_group, msg->level); 698 } 699 break; 700 case IMSG_CTL_LOG_VERBOSE: 701 /* already checked by SE */ 702 memcpy(&verbose, imsg.data, sizeof(verbose)); 703 log_verbose(verbose); 704 break; 705 case IMSG_RECONF_DONE: 706 if (reconfpending == 0) 707 log_warnx("unexpected RECONF_DONE received"); 708 else if (reconfpending == 2) { 709 imsg_compose(ibuf_rde, IMSG_RECONF_DONE, 0, 710 0, -1, NULL, 0); 711 712 /* finally fix kroute information */ 713 ktable_postload(conf->fib_priority); 714 715 /* redistribute list needs to be reloaded too */ 716 kr_reload(); 717 } 718 reconfpending--; 719 break; 720 default: 721 break; 722 } 723 imsg_free(&imsg); 724 if (rv != 0) 725 return (rv); 726 } 727 return (0); 728} 729 730void 731send_nexthop_update(struct kroute_nexthop *msg) 732{ 733 char *gw = NULL; 734 735 if (msg->gateway.aid) 736 if (asprintf(&gw, ": via %s", 737 log_addr(&msg->gateway)) == -1) { 738 log_warn("send_nexthop_update"); 739 quit = 1; 740 } 741 742 log_info("nexthop %s now %s%s%s", log_addr(&msg->nexthop), 743 msg->valid ? "valid" : "invalid", 744 msg->connected ? ": directly connected" : "", 745 msg->gateway.aid ? gw : ""); 746 747 free(gw); 748 749 if (imsg_compose(ibuf_rde, IMSG_NEXTHOP_UPDATE, 0, 0, -1, 750 msg, sizeof(struct kroute_nexthop)) == -1) 751 quit = 1; 752} 753 754void 755send_imsg_session(int type, pid_t pid, void *data, u_int16_t datalen) 756{ 757 imsg_compose(ibuf_se, type, 0, pid, -1, data, datalen); 758} 759 760int 761send_network(int type, struct network_config *net, struct filter_set_head *h) 762{ 763 if (imsg_compose(ibuf_rde, type, 0, 0, -1, net, 764 sizeof(struct network_config)) == -1) 765 return (-1); 766 /* networks that get deleted don't need to send the filter set */ 767 if (type == IMSG_NETWORK_REMOVE) 768 return (0); 769 if (send_filterset(ibuf_rde, h) == -1) 770 return (-1); 771 if (imsg_compose(ibuf_rde, IMSG_NETWORK_DONE, 0, 0, -1, NULL, 0) == -1) 772 return (-1); 773 774 return (0); 775} 776 777int 778bgpd_filternexthop(struct kroute *kr, struct kroute6 *kr6) 779{ 780 /* kernel routes are never filtered */ 781 if (kr && kr->flags & F_KERNEL && kr->prefixlen != 0) 782 return (0); 783 if (kr6 && kr6->flags & F_KERNEL && kr6->prefixlen != 0) 784 return (0); 785 786 if (cflags & BGPD_FLAG_NEXTHOP_BGP) { 787 if (kr && kr->flags & F_BGPD_INSERTED) 788 return (0); 789 if (kr6 && kr6->flags & F_BGPD_INSERTED) 790 return (0); 791 } 792 793 if (cflags & BGPD_FLAG_NEXTHOP_DEFAULT) { 794 if (kr && kr->prefixlen == 0) 795 return (0); 796 if (kr6 && kr6->prefixlen == 0) 797 return (0); 798 } 799 800 return (1); 801} 802 803int 804control_setup(struct bgpd_config *conf) 805{ 806 int fd, restricted; 807 808 /* control socket is outside chroot */ 809 if (!cname || strcmp(cname, conf->csock)) { 810 if (cname) { 811 control_cleanup(cname); 812 free(cname); 813 } 814 if ((cname = strdup(conf->csock)) == NULL) 815 fatal("strdup"); 816 if ((fd = control_init(0, cname)) == -1) 817 fatalx("control socket setup failed"); 818 restricted = 0; 819 if (imsg_compose(ibuf_se, IMSG_RECONF_CTRL, 0, 0, fd, 820 &restricted, sizeof(restricted)) == -1) 821 return (-1); 822 } 823 if (!conf->rcsock) { 824 /* remove restricted socket */ 825 control_cleanup(rcname); 826 free(rcname); 827 rcname = NULL; 828 } else if (!rcname || strcmp(rcname, conf->rcsock)) { 829 if (rcname) { 830 control_cleanup(rcname); 831 free(rcname); 832 } 833 if ((rcname = strdup(conf->rcsock)) == NULL) 834 fatal("strdup"); 835 if ((fd = control_init(1, rcname)) == -1) 836 fatalx("control socket setup failed"); 837 restricted = 1; 838 if (imsg_compose(ibuf_se, IMSG_RECONF_CTRL, 0, 0, fd, 839 &restricted, sizeof(restricted)) == -1) 840 return (-1); 841 } 842 return (0); 843} 844