ldpd.c revision 1.53
1/* $OpenBSD: ldpd.c,v 1.53 2016/06/18 01:30:28 renato Exp $ */ 2 3/* 4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org> 5 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 6 * Copyright (c) 2004, 2008 Esben Norby <norby@openbsd.org> 7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22#include <sys/types.h> 23#include <sys/wait.h> 24#include <err.h> 25#include <errno.h> 26#include <pwd.h> 27#include <stdio.h> 28#include <stdlib.h> 29#include <string.h> 30#include <signal.h> 31#include <unistd.h> 32 33#include "ldpd.h" 34#include "ldpe.h" 35#include "lde.h" 36#include "log.h" 37 38static void main_sig_handler(int, short, void *); 39static __dead void usage(void); 40static void ldpd_shutdown(void); 41static pid_t start_child(enum ldpd_process, char *, int, int, int); 42static int check_child(pid_t, const char *); 43static void main_dispatch_ldpe(int, short, void *); 44static void main_dispatch_lde(int, short, void *); 45static int main_imsg_compose_both(enum imsg_type, void *, 46 uint16_t); 47static int main_imsg_send_ipc_sockets(struct imsgbuf *, 48 struct imsgbuf *); 49static void main_imsg_send_net_sockets(int); 50static void main_imsg_send_net_socket(int, enum socket_type); 51static int main_imsg_send_config(struct ldpd_conf *); 52static int ldp_reload(void); 53static void merge_global(struct ldpd_conf *, struct ldpd_conf *); 54static void merge_af(int, struct ldpd_af_conf *, 55 struct ldpd_af_conf *); 56static void merge_ifaces(struct ldpd_conf *, struct ldpd_conf *); 57static void merge_iface_af(struct iface_af *, struct iface_af *); 58static void merge_tnbrs(struct ldpd_conf *, struct ldpd_conf *); 59static void merge_nbrps(struct ldpd_conf *, struct ldpd_conf *); 60static void merge_l2vpns(struct ldpd_conf *, struct ldpd_conf *); 61static void merge_l2vpn(struct ldpd_conf *, struct l2vpn *, 62 struct l2vpn *); 63 64struct ldpd_global global; 65struct ldpd_conf *ldpd_conf; 66 67static char *conffile; 68static struct imsgev *iev_ldpe; 69static struct imsgev *iev_lde; 70static pid_t ldpe_pid; 71static pid_t lde_pid; 72 73/* ARGSUSED */ 74static void 75main_sig_handler(int sig, short event, void *arg) 76{ 77 /* 78 * signal handler rules don't apply, libevent decouples for us 79 */ 80 81 int die = 0; 82 83 switch (sig) { 84 case SIGTERM: 85 case SIGINT: 86 die = 1; 87 /* FALLTHROUGH */ 88 case SIGCHLD: 89 if (check_child(ldpe_pid, "ldp engine")) { 90 ldpe_pid = 0; 91 die = 1; 92 } 93 if (check_child(lde_pid, "label decision engine")) { 94 lde_pid = 0; 95 die = 1; 96 } 97 if (die) 98 ldpd_shutdown(); 99 break; 100 case SIGHUP: 101 if (ldp_reload() == -1) 102 log_warnx("configuration reload failed"); 103 else 104 log_debug("configuration reloaded"); 105 break; 106 default: 107 fatalx("unexpected signal"); 108 /* NOTREACHED */ 109 } 110} 111 112static __dead void 113usage(void) 114{ 115 extern char *__progname; 116 117 fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n", 118 __progname); 119 exit(1); 120} 121 122int 123main(int argc, char *argv[]) 124{ 125 struct event ev_sigint, ev_sigterm, ev_sigchld, ev_sighup; 126 char *saved_argv0; 127 int ch; 128 int debug = 0, lflag = 0, eflag = 0; 129 int pipe_parent2ldpe[2]; 130 int pipe_parent2lde[2]; 131 132 conffile = CONF_FILE; 133 ldpd_process = PROC_MAIN; 134 135 log_init(1); /* log to stderr until daemonized */ 136 log_verbose(1); 137 138 saved_argv0 = argv[0]; 139 if (saved_argv0 == NULL) 140 saved_argv0 = "ldpd"; 141 142 while ((ch = getopt(argc, argv, "dD:f:nvLE")) != -1) { 143 switch (ch) { 144 case 'd': 145 debug = 1; 146 break; 147 case 'D': 148 if (cmdline_symset(optarg) < 0) 149 log_warnx("could not parse macro definition %s", 150 optarg); 151 break; 152 case 'f': 153 conffile = optarg; 154 break; 155 case 'n': 156 global.cmd_opts |= LDPD_OPT_NOACTION; 157 break; 158 case 'v': 159 if (global.cmd_opts & LDPD_OPT_VERBOSE) 160 global.cmd_opts |= LDPD_OPT_VERBOSE2; 161 global.cmd_opts |= LDPD_OPT_VERBOSE; 162 break; 163 case 'L': 164 lflag = 1; 165 break; 166 case 'E': 167 eflag = 1; 168 break; 169 default: 170 usage(); 171 /* NOTREACHED */ 172 } 173 } 174 175 argc -= optind; 176 argv += optind; 177 if (argc > 0 || (lflag && eflag)) 178 usage(); 179 180 if (lflag) 181 lde(debug, global.cmd_opts & LDPD_OPT_VERBOSE); 182 else if (eflag) 183 ldpe(debug, global.cmd_opts & LDPD_OPT_VERBOSE); 184 185 /* fetch interfaces early */ 186 kif_init(); 187 188 /* parse config file */ 189 if ((ldpd_conf = parse_config(conffile)) == NULL ) { 190 kif_clear(); 191 exit(1); 192 } 193 194 if (global.cmd_opts & LDPD_OPT_NOACTION) { 195 if (global.cmd_opts & LDPD_OPT_VERBOSE) 196 print_config(ldpd_conf); 197 else 198 fprintf(stderr, "configuration OK\n"); 199 kif_clear(); 200 exit(0); 201 } 202 203 /* check for root privileges */ 204 if (geteuid()) 205 errx(1, "need root privileges"); 206 207 /* check for ldpd user */ 208 if (getpwnam(LDPD_USER) == NULL) 209 errx(1, "unknown user %s", LDPD_USER); 210 211 log_init(debug); 212 log_verbose(global.cmd_opts & (LDPD_OPT_VERBOSE | LDPD_OPT_VERBOSE2)); 213 214 if (!debug) 215 daemon(1, 0); 216 217 log_info("startup"); 218 219 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 220 PF_UNSPEC, pipe_parent2ldpe) == -1) 221 fatal("socketpair"); 222 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 223 PF_UNSPEC, pipe_parent2lde) == -1) 224 fatal("socketpair"); 225 226 /* start children */ 227 lde_pid = start_child(PROC_LDE_ENGINE, saved_argv0, 228 pipe_parent2lde[1], debug, global.cmd_opts & LDPD_OPT_VERBOSE); 229 ldpe_pid = start_child(PROC_LDP_ENGINE, saved_argv0, 230 pipe_parent2ldpe[1], debug, global.cmd_opts & LDPD_OPT_VERBOSE); 231 232 event_init(); 233 234 /* setup signal handler */ 235 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL); 236 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL); 237 signal_set(&ev_sigchld, SIGCHLD, main_sig_handler, NULL); 238 signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL); 239 signal_add(&ev_sigint, NULL); 240 signal_add(&ev_sigterm, NULL); 241 signal_add(&ev_sigchld, NULL); 242 signal_add(&ev_sighup, NULL); 243 signal(SIGPIPE, SIG_IGN); 244 245 /* setup pipes to children */ 246 if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL || 247 (iev_lde = malloc(sizeof(struct imsgev))) == NULL) 248 fatal(NULL); 249 imsg_init(&iev_ldpe->ibuf, pipe_parent2ldpe[0]); 250 iev_ldpe->handler = main_dispatch_ldpe; 251 imsg_init(&iev_lde->ibuf, pipe_parent2lde[0]); 252 iev_lde->handler = main_dispatch_lde; 253 254 /* setup event handler */ 255 iev_ldpe->events = EV_READ; 256 event_set(&iev_ldpe->ev, iev_ldpe->ibuf.fd, iev_ldpe->events, 257 iev_ldpe->handler, iev_ldpe); 258 event_add(&iev_ldpe->ev, NULL); 259 260 iev_lde->events = EV_READ; 261 event_set(&iev_lde->ev, iev_lde->ibuf.fd, iev_lde->events, 262 iev_lde->handler, iev_lde); 263 event_add(&iev_lde->ev, NULL); 264 265 if (main_imsg_send_ipc_sockets(&iev_ldpe->ibuf, &iev_lde->ibuf)) 266 fatal("could not establish imsg links"); 267 main_imsg_send_config(ldpd_conf); 268 269 /* notify ldpe about existing interfaces and addresses */ 270 kif_redistribute(NULL); 271 272 if (kr_init(!(ldpd_conf->flags & F_LDPD_NO_FIB_UPDATE)) == -1) 273 fatalx("kr_init failed"); 274 275 if (ldpd_conf->ipv4.flags & F_LDPD_AF_ENABLED) 276 main_imsg_send_net_sockets(AF_INET); 277 if (ldpd_conf->ipv6.flags & F_LDPD_AF_ENABLED) 278 main_imsg_send_net_sockets(AF_INET6); 279 280 /* remove unneded stuff from config */ 281 /* ... */ 282 283 event_dispatch(); 284 285 ldpd_shutdown(); 286 /* NOTREACHED */ 287 return (0); 288} 289 290static void 291ldpd_shutdown(void) 292{ 293 pid_t pid; 294 295 if (ldpe_pid) 296 kill(ldpe_pid, SIGTERM); 297 298 if (lde_pid) 299 kill(lde_pid, SIGTERM); 300 301 kr_shutdown(); 302 303 do { 304 if ((pid = wait(NULL)) == -1 && 305 errno != EINTR && errno != ECHILD) 306 fatal("wait"); 307 } while (pid != -1 || (pid == -1 && errno == EINTR)); 308 309 config_clear(ldpd_conf); 310 311 msgbuf_clear(&iev_ldpe->ibuf.w); 312 free(iev_ldpe); 313 msgbuf_clear(&iev_lde->ibuf.w); 314 free(iev_lde); 315 316 log_info("terminating"); 317 exit(0); 318} 319 320static pid_t 321start_child(enum ldpd_process p, char *argv0, int fd, int debug, int verbose) 322{ 323 char *argv[5]; 324 int argc = 0; 325 pid_t pid; 326 327 switch (pid = fork()) { 328 case -1: 329 fatal("cannot fork"); 330 case 0: 331 break; 332 default: 333 close(fd); 334 return (pid); 335 } 336 337 if (dup2(fd, 3) == -1) 338 fatal("cannot setup imsg fd"); 339 340 argv[argc++] = argv0; 341 switch (p) { 342 case PROC_MAIN: 343 fatalx("Can not start main process"); 344 case PROC_LDE_ENGINE: 345 argv[argc++] = "-L"; 346 break; 347 case PROC_LDP_ENGINE: 348 argv[argc++] = "-E"; 349 break; 350 } 351 if (debug) 352 argv[argc++] = "-d"; 353 if (verbose) 354 argv[argc++] = "-v"; 355 argv[argc++] = NULL; 356 357 execvp(argv0, argv); 358 fatal("execvp"); 359} 360 361static int 362check_child(pid_t pid, const char *pname) 363{ 364 int status; 365 366 if (waitpid(pid, &status, WNOHANG) > 0) { 367 if (WIFEXITED(status)) { 368 log_warnx("lost child: %s exited", pname); 369 return (1); 370 } 371 if (WIFSIGNALED(status)) { 372 log_warnx("lost child: %s terminated; signal %d", 373 pname, WTERMSIG(status)); 374 return (1); 375 } 376 } 377 378 return (0); 379} 380 381/* imsg handling */ 382/* ARGSUSED */ 383static void 384main_dispatch_ldpe(int fd, short event, void *bula) 385{ 386 struct imsgev *iev = bula; 387 struct imsgbuf *ibuf = &iev->ibuf; 388 struct imsg imsg; 389 int af; 390 ssize_t n; 391 int shut = 0, verbose; 392 393 if (event & EV_READ) { 394 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 395 fatal("imsg_read error"); 396 if (n == 0) /* connection closed */ 397 shut = 1; 398 } 399 if (event & EV_WRITE) { 400 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 401 fatal("msgbuf_write"); 402 if (n == 0) 403 shut = 1; 404 } 405 406 for (;;) { 407 if ((n = imsg_get(ibuf, &imsg)) == -1) 408 fatal("imsg_get"); 409 410 if (n == 0) 411 break; 412 413 switch (imsg.hdr.type) { 414 case IMSG_REQUEST_SOCKETS: 415 af = imsg.hdr.pid; 416 main_imsg_send_net_sockets(af); 417 break; 418 case IMSG_CTL_RELOAD: 419 if (ldp_reload() == -1) 420 log_warnx("configuration reload failed"); 421 else 422 log_debug("configuration reloaded"); 423 break; 424 case IMSG_CTL_FIB_COUPLE: 425 kr_fib_couple(); 426 break; 427 case IMSG_CTL_FIB_DECOUPLE: 428 kr_fib_decouple(); 429 break; 430 case IMSG_CTL_KROUTE: 431 case IMSG_CTL_KROUTE_ADDR: 432 kr_show_route(&imsg); 433 break; 434 case IMSG_CTL_IFINFO: 435 if (imsg.hdr.len == IMSG_HEADER_SIZE) 436 kr_ifinfo(NULL, imsg.hdr.pid); 437 else if (imsg.hdr.len == IMSG_HEADER_SIZE + IFNAMSIZ) 438 kr_ifinfo(imsg.data, imsg.hdr.pid); 439 else 440 log_warnx("IFINFO request with wrong len"); 441 break; 442 case IMSG_CTL_LOG_VERBOSE: 443 /* already checked by ldpe */ 444 memcpy(&verbose, imsg.data, sizeof(verbose)); 445 log_verbose(verbose); 446 break; 447 default: 448 log_debug("%s: error handling imsg %d", __func__, 449 imsg.hdr.type); 450 break; 451 } 452 imsg_free(&imsg); 453 } 454 if (!shut) 455 imsg_event_add(iev); 456 else { 457 /* this pipe is dead, so remove the event handler */ 458 event_del(&iev->ev); 459 event_loopexit(NULL); 460 } 461} 462 463/* ARGSUSED */ 464static void 465main_dispatch_lde(int fd, short event, void *bula) 466{ 467 struct imsgev *iev = bula; 468 struct imsgbuf *ibuf = &iev->ibuf; 469 struct imsg imsg; 470 ssize_t n; 471 int shut = 0; 472 473 if (event & EV_READ) { 474 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 475 fatal("imsg_read error"); 476 if (n == 0) /* connection closed */ 477 shut = 1; 478 } 479 if (event & EV_WRITE) { 480 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 481 fatal("msgbuf_write"); 482 if (n == 0) 483 shut = 1; 484 } 485 486 for (;;) { 487 if ((n = imsg_get(ibuf, &imsg)) == -1) 488 fatal("imsg_get"); 489 490 if (n == 0) 491 break; 492 493 switch (imsg.hdr.type) { 494 case IMSG_KLABEL_CHANGE: 495 if (imsg.hdr.len - IMSG_HEADER_SIZE != 496 sizeof(struct kroute)) 497 fatalx("invalid size of IMSG_KLABEL_CHANGE"); 498 if (kr_change(imsg.data)) 499 log_warnx("%s: error changing route", __func__); 500 break; 501 case IMSG_KLABEL_DELETE: 502 if (imsg.hdr.len - IMSG_HEADER_SIZE != 503 sizeof(struct kroute)) 504 fatalx("invalid size of IMSG_KLABEL_DELETE"); 505 if (kr_delete(imsg.data)) 506 log_warnx("%s: error deleting route", __func__); 507 break; 508 case IMSG_KPWLABEL_CHANGE: 509 if (imsg.hdr.len - IMSG_HEADER_SIZE != 510 sizeof(struct kpw)) 511 fatalx("invalid size of IMSG_KPWLABEL_CHANGE"); 512 if (kmpw_set(imsg.data)) 513 log_warnx("%s: error changing pseudowire", 514 __func__); 515 break; 516 case IMSG_KPWLABEL_DELETE: 517 if (imsg.hdr.len - IMSG_HEADER_SIZE != 518 sizeof(struct kpw)) 519 fatalx("invalid size of IMSG_KPWLABEL_DELETE"); 520 if (kmpw_unset(imsg.data)) 521 log_warnx("%s: error unsetting pseudowire", 522 __func__); 523 break; 524 default: 525 log_debug("%s: error handling imsg %d", __func__, 526 imsg.hdr.type); 527 break; 528 } 529 imsg_free(&imsg); 530 } 531 if (!shut) 532 imsg_event_add(iev); 533 else { 534 /* this pipe is dead, so remove the event handler */ 535 event_del(&iev->ev); 536 event_loopexit(NULL); 537 } 538} 539 540void 541main_imsg_compose_ldpe(int type, pid_t pid, void *data, uint16_t datalen) 542{ 543 if (iev_ldpe == NULL) 544 return; 545 imsg_compose_event(iev_ldpe, type, 0, pid, -1, data, datalen); 546} 547 548void 549main_imsg_compose_lde(int type, pid_t pid, void *data, uint16_t datalen) 550{ 551 imsg_compose_event(iev_lde, type, 0, pid, -1, data, datalen); 552} 553 554static int 555main_imsg_compose_both(enum imsg_type type, void *buf, uint16_t len) 556{ 557 if (imsg_compose_event(iev_ldpe, type, 0, 0, -1, buf, len) == -1) 558 return (-1); 559 if (imsg_compose_event(iev_lde, type, 0, 0, -1, buf, len) == -1) 560 return (-1); 561 return (0); 562} 563 564void 565imsg_event_add(struct imsgev *iev) 566{ 567 iev->events = EV_READ; 568 if (iev->ibuf.w.queued) 569 iev->events |= EV_WRITE; 570 571 event_del(&iev->ev); 572 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); 573 event_add(&iev->ev, NULL); 574} 575 576int 577imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid, 578 pid_t pid, int fd, void *data, uint16_t datalen) 579{ 580 int ret; 581 582 if ((ret = imsg_compose(&iev->ibuf, type, peerid, 583 pid, fd, data, datalen)) != -1) 584 imsg_event_add(iev); 585 return (ret); 586} 587 588void 589evbuf_enqueue(struct evbuf *eb, struct ibuf *buf) 590{ 591 ibuf_close(&eb->wbuf, buf); 592 evbuf_event_add(eb); 593} 594 595void 596evbuf_event_add(struct evbuf *eb) 597{ 598 if (eb->wbuf.queued) 599 event_add(&eb->ev, NULL); 600} 601 602void 603evbuf_init(struct evbuf *eb, int fd, void (*handler)(int, short, void *), 604 void *arg) 605{ 606 msgbuf_init(&eb->wbuf); 607 eb->wbuf.fd = fd; 608 event_set(&eb->ev, eb->wbuf.fd, EV_WRITE, handler, arg); 609} 610 611void 612evbuf_clear(struct evbuf *eb) 613{ 614 event_del(&eb->ev); 615 msgbuf_clear(&eb->wbuf); 616 eb->wbuf.fd = -1; 617} 618 619static int 620main_imsg_send_ipc_sockets(struct imsgbuf *ldpe_buf, struct imsgbuf *lde_buf) 621{ 622 int pipe_ldpe2lde[2]; 623 624 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 625 PF_UNSPEC, pipe_ldpe2lde) == -1) 626 return (-1); 627 628 if (imsg_compose(ldpe_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[0], 629 NULL, 0) == -1) 630 return (-1); 631 if (imsg_compose(lde_buf, IMSG_SOCKET_IPC, 0, 0, pipe_ldpe2lde[1], 632 NULL, 0) == -1) 633 return (-1); 634 635 return (0); 636} 637 638static void 639main_imsg_send_net_sockets(int af) 640{ 641 main_imsg_send_net_socket(af, LDP_SOCKET_DISC); 642 main_imsg_send_net_socket(af, LDP_SOCKET_EDISC); 643 main_imsg_send_net_socket(af, LDP_SOCKET_SESSION); 644 imsg_compose_event(iev_ldpe, IMSG_SETUP_SOCKETS, af, 0, -1, NULL, 0); 645} 646 647static void 648main_imsg_send_net_socket(int af, enum socket_type type) 649{ 650 int fd; 651 652 fd = ldp_create_socket(af, type); 653 if (fd == -1) { 654 log_warnx("%s: failed to create %s socket for address-family " 655 "%s", __func__, socket_name(type), af_name(af)); 656 return; 657 } 658 659 imsg_compose_event(iev_ldpe, IMSG_SOCKET_NET, af, 0, fd, &type, 660 sizeof(type)); 661} 662 663struct ldpd_af_conf * 664ldp_af_conf_get(struct ldpd_conf *xconf, int af) 665{ 666 switch (af) { 667 case AF_INET: 668 return (&xconf->ipv4); 669 case AF_INET6: 670 return (&xconf->ipv6); 671 default: 672 fatalx("ldp_af_conf_get: unknown af"); 673 } 674} 675 676struct ldpd_af_global * 677ldp_af_global_get(struct ldpd_global *xglobal, int af) 678{ 679 switch (af) { 680 case AF_INET: 681 return (&xglobal->ipv4); 682 case AF_INET6: 683 return (&xglobal->ipv6); 684 default: 685 fatalx("ldp_af_global_get: unknown af"); 686 } 687} 688 689int 690ldp_is_dual_stack(struct ldpd_conf *xconf) 691{ 692 return ((xconf->ipv4.flags & F_LDPD_AF_ENABLED) && 693 (xconf->ipv6.flags & F_LDPD_AF_ENABLED)); 694} 695 696static int 697main_imsg_send_config(struct ldpd_conf *xconf) 698{ 699 struct iface *iface; 700 struct tnbr *tnbr; 701 struct nbr_params *nbrp; 702 struct l2vpn *l2vpn; 703 struct l2vpn_if *lif; 704 struct l2vpn_pw *pw; 705 706 if (main_imsg_compose_both(IMSG_RECONF_CONF, xconf, 707 sizeof(*xconf)) == -1) 708 return (-1); 709 710 LIST_FOREACH(iface, &xconf->iface_list, entry) { 711 if (main_imsg_compose_both(IMSG_RECONF_IFACE, iface, 712 sizeof(*iface)) == -1) 713 return (-1); 714 } 715 716 LIST_FOREACH(tnbr, &xconf->tnbr_list, entry) { 717 if (main_imsg_compose_both(IMSG_RECONF_TNBR, tnbr, 718 sizeof(*tnbr)) == -1) 719 return (-1); 720 } 721 722 LIST_FOREACH(nbrp, &xconf->nbrp_list, entry) { 723 if (main_imsg_compose_both(IMSG_RECONF_NBRP, nbrp, 724 sizeof(*nbrp)) == -1) 725 return (-1); 726 } 727 728 LIST_FOREACH(l2vpn, &xconf->l2vpn_list, entry) { 729 if (main_imsg_compose_both(IMSG_RECONF_L2VPN, l2vpn, 730 sizeof(*l2vpn)) == -1) 731 return (-1); 732 733 LIST_FOREACH(lif, &l2vpn->if_list, entry) { 734 if (main_imsg_compose_both(IMSG_RECONF_L2VPN_IF, lif, 735 sizeof(*lif)) == -1) 736 return (-1); 737 } 738 LIST_FOREACH(pw, &l2vpn->pw_list, entry) { 739 if (main_imsg_compose_both(IMSG_RECONF_L2VPN_PW, pw, 740 sizeof(*pw)) == -1) 741 return (-1); 742 } 743 } 744 745 if (main_imsg_compose_both(IMSG_RECONF_END, NULL, 0) == -1) 746 return (-1); 747 748 return (0); 749} 750 751static int 752ldp_reload(void) 753{ 754 struct ldpd_conf *xconf; 755 756 if ((xconf = parse_config(conffile)) == NULL) 757 return (-1); 758 759 if (main_imsg_send_config(xconf) == -1) 760 return (-1); 761 762 merge_config(ldpd_conf, xconf); 763 764 return (0); 765} 766 767void 768merge_config(struct ldpd_conf *conf, struct ldpd_conf *xconf) 769{ 770 merge_global(conf, xconf); 771 merge_af(AF_INET, &conf->ipv4, &xconf->ipv4); 772 merge_af(AF_INET6, &conf->ipv6, &xconf->ipv6); 773 merge_ifaces(conf, xconf); 774 merge_tnbrs(conf, xconf); 775 merge_nbrps(conf, xconf); 776 merge_l2vpns(conf, xconf); 777 free(xconf); 778} 779 780static void 781merge_global(struct ldpd_conf *conf, struct ldpd_conf *xconf) 782{ 783 /* change of router-id requires resetting all neighborships */ 784 if (conf->rtr_id.s_addr != xconf->rtr_id.s_addr) { 785 if (ldpd_process == PROC_LDP_ENGINE) { 786 ldpe_reset_nbrs(AF_INET); 787 ldpe_reset_nbrs(AF_INET6); 788 if (conf->rtr_id.s_addr == INADDR_ANY || 789 xconf->rtr_id.s_addr == INADDR_ANY) { 790 if_update_all(AF_UNSPEC); 791 tnbr_update_all(AF_UNSPEC); 792 } 793 } 794 conf->rtr_id = xconf->rtr_id; 795 } 796 797 if (conf->trans_pref != xconf->trans_pref) { 798 if (ldpd_process == PROC_LDP_ENGINE) 799 ldpe_reset_ds_nbrs(); 800 conf->trans_pref = xconf->trans_pref; 801 } 802 803 if ((conf->flags & F_LDPD_DS_CISCO_INTEROP) != 804 (xconf->flags & F_LDPD_DS_CISCO_INTEROP)) { 805 if (ldpd_process == PROC_LDP_ENGINE) 806 ldpe_reset_ds_nbrs(); 807 } 808 809 conf->flags = xconf->flags; 810} 811 812static void 813merge_af(int af, struct ldpd_af_conf *af_conf, struct ldpd_af_conf *xa) 814{ 815 int egress_label_changed = 0; 816 817 if (af_conf->keepalive != xa->keepalive) { 818 af_conf->keepalive = xa->keepalive; 819 if (ldpd_process == PROC_LDP_ENGINE) 820 ldpe_stop_init_backoff(af); 821 } 822 af_conf->thello_holdtime = xa->thello_holdtime; 823 af_conf->thello_interval = xa->thello_interval; 824 825 /* update flags */ 826 if (ldpd_process == PROC_LDP_ENGINE && 827 (af_conf->flags & F_LDPD_AF_THELLO_ACCEPT) && 828 !(xa->flags & F_LDPD_AF_THELLO_ACCEPT)) 829 ldpe_remove_dynamic_tnbrs(af); 830 831 if ((af_conf->flags & F_LDPD_AF_EXPNULL) != 832 (xa->flags & F_LDPD_AF_EXPNULL)) 833 egress_label_changed = 1; 834 835 af_conf->flags = xa->flags; 836 837 if (egress_label_changed) { 838 switch (ldpd_process) { 839 case PROC_LDE_ENGINE: 840 lde_change_egress_label(af, af_conf->flags & 841 F_LDPD_AF_EXPNULL); 842 break; 843 case PROC_MAIN: 844 kr_change_egress_label(af, af_conf->flags & 845 F_LDPD_AF_EXPNULL); 846 break; 847 default: 848 break; 849 } 850 } 851 852 if (ldp_addrcmp(af, &af_conf->trans_addr, &xa->trans_addr)) { 853 af_conf->trans_addr = xa->trans_addr; 854 if (ldpd_process == PROC_MAIN) 855 imsg_compose_event(iev_ldpe, IMSG_CLOSE_SOCKETS, af, 856 0, -1, NULL, 0); 857 } 858} 859 860static void 861merge_ifaces(struct ldpd_conf *conf, struct ldpd_conf *xconf) 862{ 863 struct iface *iface, *itmp, *xi; 864 865 LIST_FOREACH_SAFE(iface, &conf->iface_list, entry, itmp) { 866 /* find deleted interfaces */ 867 if ((xi = if_lookup(xconf, iface->ifindex)) == NULL) { 868 LIST_REMOVE(iface, entry); 869 if (ldpd_process == PROC_LDP_ENGINE) 870 if_del(iface); 871 else 872 free(iface); 873 } 874 } 875 LIST_FOREACH_SAFE(xi, &xconf->iface_list, entry, itmp) { 876 /* find new interfaces */ 877 if ((iface = if_lookup(conf, xi->ifindex)) == NULL) { 878 LIST_REMOVE(xi, entry); 879 LIST_INSERT_HEAD(&conf->iface_list, xi, entry); 880 881 /* resend addresses to activate new interfaces */ 882 if (ldpd_process == PROC_MAIN) 883 kif_redistribute(xi->name); 884 continue; 885 } 886 887 /* update existing interfaces */ 888 merge_iface_af(&iface->ipv4, &xi->ipv4); 889 merge_iface_af(&iface->ipv6, &xi->ipv6); 890 LIST_REMOVE(xi, entry); 891 free(xi); 892 } 893} 894 895static void 896merge_iface_af(struct iface_af *ia, struct iface_af *xi) 897{ 898 if (ia->enabled != xi->enabled) { 899 ia->enabled = xi->enabled; 900 if (ldpd_process == PROC_LDP_ENGINE) 901 if_update(ia->iface, ia->af); 902 } 903 ia->hello_holdtime = xi->hello_holdtime; 904 ia->hello_interval = xi->hello_interval; 905} 906 907static void 908merge_tnbrs(struct ldpd_conf *conf, struct ldpd_conf *xconf) 909{ 910 struct tnbr *tnbr, *ttmp, *xt; 911 912 LIST_FOREACH_SAFE(tnbr, &conf->tnbr_list, entry, ttmp) { 913 if (!(tnbr->flags & F_TNBR_CONFIGURED)) 914 continue; 915 916 /* find deleted tnbrs */ 917 if ((xt = tnbr_find(xconf, tnbr->af, &tnbr->addr)) == NULL) { 918 if (ldpd_process == PROC_LDP_ENGINE) { 919 tnbr->flags &= ~F_TNBR_CONFIGURED; 920 tnbr_check(tnbr); 921 } else { 922 LIST_REMOVE(tnbr, entry); 923 free(tnbr); 924 } 925 } 926 } 927 LIST_FOREACH_SAFE(xt, &xconf->tnbr_list, entry, ttmp) { 928 /* find new tnbrs */ 929 if ((tnbr = tnbr_find(conf, xt->af, &xt->addr)) == NULL) { 930 LIST_REMOVE(xt, entry); 931 LIST_INSERT_HEAD(&conf->tnbr_list, xt, entry); 932 933 if (ldpd_process == PROC_LDP_ENGINE) 934 tnbr_update(xt); 935 continue; 936 } 937 938 /* update existing tnbrs */ 939 if (!(tnbr->flags & F_TNBR_CONFIGURED)) 940 tnbr->flags |= F_TNBR_CONFIGURED; 941 tnbr->hello_holdtime = xt->hello_holdtime; 942 tnbr->hello_interval = xt->hello_interval; 943 LIST_REMOVE(xt, entry); 944 free(xt); 945 } 946} 947 948static void 949merge_nbrps(struct ldpd_conf *conf, struct ldpd_conf *xconf) 950{ 951 struct nbr_params *nbrp, *ntmp, *xn; 952 struct nbr *nbr; 953 int nbrp_changed; 954 955 LIST_FOREACH_SAFE(nbrp, &conf->nbrp_list, entry, ntmp) { 956 /* find deleted nbrps */ 957 if ((xn = nbr_params_find(xconf, nbrp->lsr_id)) == NULL) { 958 if (ldpd_process == PROC_LDP_ENGINE) { 959 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr); 960 if (nbr) { 961 session_shutdown(nbr, S_SHUTDOWN, 0, 0); 962 pfkey_remove(nbr); 963 if (nbr_session_active_role(nbr)) 964 nbr_establish_connection(nbr); 965 } 966 } 967 LIST_REMOVE(nbrp, entry); 968 free(nbrp); 969 } 970 } 971 LIST_FOREACH_SAFE(xn, &xconf->nbrp_list, entry, ntmp) { 972 /* find new nbrps */ 973 if ((nbrp = nbr_params_find(conf, xn->lsr_id)) == NULL) { 974 LIST_REMOVE(xn, entry); 975 LIST_INSERT_HEAD(&conf->nbrp_list, xn, entry); 976 977 if (ldpd_process == PROC_LDP_ENGINE) { 978 nbr = nbr_find_ldpid(xn->lsr_id.s_addr); 979 if (nbr) { 980 session_shutdown(nbr, S_SHUTDOWN, 0, 0); 981 if (pfkey_establish(nbr, xn) == -1) 982 fatalx("pfkey setup failed"); 983 if (nbr_session_active_role(nbr)) 984 nbr_establish_connection(nbr); 985 } 986 } 987 continue; 988 } 989 990 /* update existing nbrps */ 991 if (nbrp->keepalive != xn->keepalive || 992 nbrp->auth.method != xn->auth.method || 993 strcmp(nbrp->auth.md5key, xn->auth.md5key) != 0) 994 nbrp_changed = 1; 995 else 996 nbrp_changed = 0; 997 998 nbrp->keepalive = xn->keepalive; 999 nbrp->auth.method = xn->auth.method; 1000 strlcpy(nbrp->auth.md5key, xn->auth.md5key, 1001 sizeof(nbrp->auth.md5key)); 1002 nbrp->auth.md5key_len = xn->auth.md5key_len; 1003 nbrp->flags = xn->flags; 1004 1005 if (ldpd_process == PROC_LDP_ENGINE) { 1006 nbr = nbr_find_ldpid(nbrp->lsr_id.s_addr); 1007 if (nbr && nbrp_changed) { 1008 session_shutdown(nbr, S_SHUTDOWN, 0, 0); 1009 pfkey_remove(nbr); 1010 if (pfkey_establish(nbr, nbrp) == -1) 1011 fatalx("pfkey setup failed"); 1012 if (nbr_session_active_role(nbr)) 1013 nbr_establish_connection(nbr); 1014 } 1015 } 1016 LIST_REMOVE(xn, entry); 1017 free(xn); 1018 } 1019} 1020 1021static void 1022merge_l2vpns(struct ldpd_conf *conf, struct ldpd_conf *xconf) 1023{ 1024 struct l2vpn *l2vpn, *ltmp, *xl; 1025 1026 LIST_FOREACH_SAFE(l2vpn, &conf->l2vpn_list, entry, ltmp) { 1027 /* find deleted l2vpns */ 1028 if ((xl = l2vpn_find(xconf, l2vpn->name)) == NULL) { 1029 LIST_REMOVE(l2vpn, entry); 1030 1031 switch (ldpd_process) { 1032 case PROC_LDE_ENGINE: 1033 l2vpn_del(l2vpn); 1034 break; 1035 case PROC_LDP_ENGINE: 1036 ldpe_l2vpn_exit(l2vpn); 1037 free(l2vpn); 1038 break; 1039 case PROC_MAIN: 1040 free(l2vpn); 1041 break; 1042 } 1043 } 1044 } 1045 LIST_FOREACH_SAFE(xl, &xconf->l2vpn_list, entry, ltmp) { 1046 /* find new l2vpns */ 1047 if ((l2vpn = l2vpn_find(conf, xl->name)) == NULL) { 1048 LIST_REMOVE(xl, entry); 1049 LIST_INSERT_HEAD(&conf->l2vpn_list, xl, entry); 1050 1051 switch (ldpd_process) { 1052 case PROC_LDE_ENGINE: 1053 l2vpn_init(xl); 1054 break; 1055 case PROC_LDP_ENGINE: 1056 ldpe_l2vpn_init(xl); 1057 break; 1058 case PROC_MAIN: 1059 break; 1060 } 1061 continue; 1062 } 1063 1064 /* update existing l2vpns */ 1065 merge_l2vpn(conf, l2vpn, xl); 1066 LIST_REMOVE(xl, entry); 1067 free(xl); 1068 } 1069} 1070 1071static void 1072merge_l2vpn(struct ldpd_conf *xconf, struct l2vpn *l2vpn, struct l2vpn *xl) 1073{ 1074 struct l2vpn_if *lif, *ftmp, *xf; 1075 struct l2vpn_pw *pw, *ptmp, *xp; 1076 struct nbr *nbr; 1077 int reset_nbr, reinstall_pwfec, reinstall_tnbr; 1078 int previous_pw_type, previous_mtu; 1079 1080 previous_pw_type = l2vpn->pw_type; 1081 previous_mtu = l2vpn->mtu; 1082 1083 /* merge intefaces */ 1084 LIST_FOREACH_SAFE(lif, &l2vpn->if_list, entry, ftmp) { 1085 /* find deleted interfaces */ 1086 if ((xf = l2vpn_if_find(xl, lif->ifindex)) == NULL) { 1087 LIST_REMOVE(lif, entry); 1088 free(lif); 1089 } 1090 } 1091 LIST_FOREACH_SAFE(xf, &xl->if_list, entry, ftmp) { 1092 /* find new interfaces */ 1093 if ((lif = l2vpn_if_find(l2vpn, xf->ifindex)) == NULL) { 1094 LIST_REMOVE(xf, entry); 1095 LIST_INSERT_HEAD(&l2vpn->if_list, xf, entry); 1096 xf->l2vpn = l2vpn; 1097 continue; 1098 } 1099 1100 LIST_REMOVE(xf, entry); 1101 free(xf); 1102 } 1103 1104 /* merge pseudowires */ 1105 LIST_FOREACH_SAFE(pw, &l2vpn->pw_list, entry, ptmp) { 1106 /* find deleted pseudowires */ 1107 if ((xp = l2vpn_pw_find(xl, pw->ifindex)) == NULL) { 1108 switch (ldpd_process) { 1109 case PROC_LDE_ENGINE: 1110 l2vpn_pw_exit(pw); 1111 break; 1112 case PROC_LDP_ENGINE: 1113 ldpe_l2vpn_pw_exit(pw); 1114 break; 1115 case PROC_MAIN: 1116 break; 1117 } 1118 1119 LIST_REMOVE(pw, entry); 1120 free(pw); 1121 } 1122 } 1123 LIST_FOREACH_SAFE(xp, &xl->pw_list, entry, ptmp) { 1124 /* find new pseudowires */ 1125 if ((pw = l2vpn_pw_find(l2vpn, xp->ifindex)) == NULL) { 1126 LIST_REMOVE(xp, entry); 1127 LIST_INSERT_HEAD(&l2vpn->pw_list, xp, entry); 1128 xp->l2vpn = l2vpn; 1129 1130 switch (ldpd_process) { 1131 case PROC_LDE_ENGINE: 1132 l2vpn_pw_init(xp); 1133 break; 1134 case PROC_LDP_ENGINE: 1135 ldpe_l2vpn_pw_init(xp); 1136 break; 1137 case PROC_MAIN: 1138 break; 1139 } 1140 continue; 1141 } 1142 1143 /* update existing pseudowire */ 1144 if (pw->af != xp->af || 1145 ldp_addrcmp(pw->af, &pw->addr, &xp->addr)) 1146 reinstall_tnbr = 1; 1147 else 1148 reinstall_tnbr = 0; 1149 1150 /* changes that require a session restart */ 1151 if ((pw->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF)) != 1152 (xp->flags & (F_PW_STATUSTLV_CONF|F_PW_CWORD_CONF))) 1153 reset_nbr = 1; 1154 else 1155 reset_nbr = 0; 1156 1157 if (l2vpn->pw_type != xl->pw_type || l2vpn->mtu != xl->mtu || 1158 pw->pwid != xp->pwid || reinstall_tnbr || reset_nbr || 1159 pw->lsr_id.s_addr != xp->lsr_id.s_addr) 1160 reinstall_pwfec = 1; 1161 else 1162 reinstall_pwfec = 0; 1163 1164 if (ldpd_process == PROC_LDP_ENGINE) { 1165 if (reinstall_tnbr) 1166 ldpe_l2vpn_pw_exit(pw); 1167 if (reset_nbr) { 1168 nbr = nbr_find_ldpid(pw->lsr_id.s_addr); 1169 if (nbr && nbr->state == NBR_STA_OPER) 1170 session_shutdown(nbr, S_SHUTDOWN, 0, 0); 1171 } 1172 } 1173 if (ldpd_process == PROC_LDE_ENGINE && 1174 !reset_nbr && reinstall_pwfec) 1175 l2vpn_pw_exit(pw); 1176 pw->lsr_id = xp->lsr_id; 1177 pw->af = xp->af; 1178 pw->addr = xp->addr; 1179 pw->pwid = xp->pwid; 1180 strlcpy(pw->ifname, xp->ifname, sizeof(pw->ifname)); 1181 pw->ifindex = xp->ifindex; 1182 if (xp->flags & F_PW_CWORD_CONF) 1183 pw->flags |= F_PW_CWORD_CONF; 1184 else 1185 pw->flags &= ~F_PW_CWORD_CONF; 1186 if (xp->flags & F_PW_STATUSTLV_CONF) 1187 pw->flags |= F_PW_STATUSTLV_CONF; 1188 else 1189 pw->flags &= ~F_PW_STATUSTLV_CONF; 1190 if (ldpd_process == PROC_LDP_ENGINE && reinstall_tnbr) 1191 ldpe_l2vpn_pw_init(pw); 1192 if (ldpd_process == PROC_LDE_ENGINE && 1193 !reset_nbr && reinstall_pwfec) { 1194 l2vpn->pw_type = xl->pw_type; 1195 l2vpn->mtu = xl->mtu; 1196 l2vpn_pw_init(pw); 1197 l2vpn->pw_type = previous_pw_type; 1198 l2vpn->mtu = previous_mtu; 1199 } 1200 1201 LIST_REMOVE(xp, entry); 1202 free(xp); 1203 } 1204 1205 l2vpn->pw_type = xl->pw_type; 1206 l2vpn->mtu = xl->mtu; 1207 strlcpy(l2vpn->br_ifname, xl->br_ifname, sizeof(l2vpn->br_ifname)); 1208 l2vpn->br_ifindex = xl->br_ifindex; 1209} 1210 1211struct ldpd_conf * 1212config_new_empty(void) 1213{ 1214 struct ldpd_conf *xconf; 1215 1216 xconf = calloc(1, sizeof(*xconf)); 1217 if (xconf == NULL) 1218 fatal(NULL); 1219 1220 LIST_INIT(&xconf->iface_list); 1221 LIST_INIT(&xconf->tnbr_list); 1222 LIST_INIT(&xconf->nbrp_list); 1223 LIST_INIT(&xconf->l2vpn_list); 1224 1225 return (xconf); 1226} 1227 1228void 1229config_clear(struct ldpd_conf *conf) 1230{ 1231 struct ldpd_conf *xconf; 1232 1233 /* 1234 * Merge current config with an empty config, this will deactivate 1235 * and deallocate all the interfaces, pseudowires and so on. Before 1236 * merging, copy the router-id and other variables to avoid some 1237 * unnecessary operations, like trying to reset the neighborships. 1238 */ 1239 xconf = config_new_empty(); 1240 xconf->ipv4 = conf->ipv4; 1241 xconf->ipv6 = conf->ipv6; 1242 xconf->rtr_id = conf->rtr_id; 1243 xconf->trans_pref = conf->trans_pref; 1244 xconf->flags = conf->flags; 1245 merge_config(conf, xconf); 1246 free(conf); 1247} 1248