1 /* $OpenBSD: ospf6d.c,v 1.47 2020/06/26 19:06:52 bket Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2004, 2007 Esben Norby <norby@openbsd.org> 6 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/types.h> 22 #include <sys/socket.h> 23 #include <sys/queue.h> 24 #include <sys/time.h> 25 #include <sys/stat.h> 26 #include <sys/wait.h> 27 #include <sys/sysctl.h> 28 #include <syslog.h> 29 30 #include <netinet/in.h> 31 #include <arpa/inet.h> 32 #include <net/if_types.h> 33 34 #include <event.h> 35 #include <err.h> 36 #include <errno.h> 37 #include <pwd.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <signal.h> 42 #include <unistd.h> 43 44 #include "ospf6d.h" 45 #include "ospf6.h" 46 #include "ospfe.h" 47 #include "control.h" 48 #include "log.h" 49 #include "rde.h" 50 51 void main_sig_handler(int, short, void *); 52 __dead void usage(void); 53 __dead void ospfd_shutdown(void); 54 55 void main_dispatch_ospfe(int, short, void *); 56 void main_dispatch_rde(int, short, void *); 57 58 int ospf_reload(void); 59 int ospf_sendboth(enum imsg_type, void *, u_int16_t); 60 int merge_interfaces(struct area *, struct area *); 61 struct iface *iface_lookup(struct area *, struct iface *); 62 63 int pipe_parent2ospfe[2]; 64 int pipe_parent2rde[2]; 65 int pipe_ospfe2rde[2]; 66 67 struct ospfd_conf *ospfd_conf = NULL; 68 struct imsgev *iev_ospfe; 69 struct imsgev *iev_rde; 70 char *conffile; 71 72 pid_t ospfe_pid = 0; 73 pid_t rde_pid = 0; 74 75 /* ARGSUSED */ 76 void 77 main_sig_handler(int sig, short event, void *arg) 78 { 79 /* signal handler rules don't apply, libevent decouples for us */ 80 switch (sig) { 81 case SIGTERM: 82 case SIGINT: 83 ospfd_shutdown(); 84 /* NOTREACHED */ 85 case SIGHUP: 86 if (ospf_reload() == -1) 87 log_warnx("configuration reload failed"); 88 else 89 log_debug("configuration reloaded"); 90 break; 91 default: 92 fatalx("unexpected signal"); 93 /* NOTREACHED */ 94 } 95 } 96 97 __dead void 98 usage(void) 99 { 100 extern char *__progname; 101 102 fprintf(stderr, "usage: %s [-dnv] [-D macro=value]" 103 " [-f file] [-s socket]\n", 104 __progname); 105 exit(1); 106 } 107 108 int 109 main(int argc, char *argv[]) 110 { 111 struct event ev_sigint, ev_sigterm, ev_sighup; 112 int ch, opts = 0; 113 int debug = 0; 114 int ipforwarding; 115 int mib[4]; 116 size_t len; 117 char *sockname = NULL; 118 int control_fd; 119 120 conffile = CONF_FILE; 121 ospfd_process = PROC_MAIN; 122 123 log_init(1, LOG_DAEMON); /* log to stderr until daemonized */ 124 log_procinit(log_procnames[ospfd_process]); 125 126 while ((ch = getopt(argc, argv, "cdD:f:s:nv")) != -1) { 127 switch (ch) { 128 case 'c': 129 opts |= OSPFD_OPT_FORCE_DEMOTE; 130 break; 131 case 'd': 132 debug = 1; 133 break; 134 case 'D': 135 if (cmdline_symset(optarg) < 0) 136 log_warnx("could not parse macro definition %s", 137 optarg); 138 break; 139 case 'f': 140 conffile = optarg; 141 break; 142 case 'n': 143 opts |= OSPFD_OPT_NOACTION; 144 break; 145 case 's': 146 sockname = optarg; 147 break; 148 case 'v': 149 if (opts & OSPFD_OPT_VERBOSE) 150 opts |= OSPFD_OPT_VERBOSE2; 151 opts |= OSPFD_OPT_VERBOSE; 152 log_setverbose(1); 153 break; 154 default: 155 usage(); 156 /* NOTREACHED */ 157 } 158 } 159 160 argc -= optind; 161 argv += optind; 162 if (argc > 0) 163 usage(); 164 165 mib[0] = CTL_NET; 166 mib[1] = PF_INET6; 167 mib[2] = IPPROTO_IPV6; 168 mib[3] = IPCTL_FORWARDING; 169 len = sizeof(ipforwarding); 170 if (sysctl(mib, 4, &ipforwarding, &len, NULL, 0) == -1) 171 err(1, "sysctl"); 172 173 if (ipforwarding != 1) { 174 log_warnx("WARNING: IPv6 forwarding NOT enabled, " 175 "running as stub router"); 176 opts |= OSPFD_OPT_STUB_ROUTER; 177 } 178 179 /* prepare and fetch interfaces early */ 180 if_init(); 181 182 /* parse config file */ 183 if ((ospfd_conf = parse_config(conffile, opts)) == NULL ) 184 exit(1); 185 186 if (sockname == NULL) { 187 if (asprintf(&sockname, "%s.%d", OSPF6D_SOCKET, 188 ospfd_conf->rdomain) == -1) 189 err(1, "asprintf"); 190 } 191 192 ospfd_conf->csock = sockname; 193 194 if (ospfd_conf->opts & OSPFD_OPT_NOACTION) { 195 if (ospfd_conf->opts & OSPFD_OPT_VERBOSE) 196 print_config(ospfd_conf); 197 else 198 fprintf(stderr, "configuration OK\n"); 199 exit(0); 200 } 201 202 /* check for root privileges */ 203 if (geteuid()) 204 errx(1, "need root privileges"); 205 206 /* check for ospfd user */ 207 if (getpwnam(OSPF6D_USER) == NULL) 208 errx(1, "unknown user %s", OSPF6D_USER); 209 210 log_init(debug, LOG_DAEMON); 211 log_setverbose(ospfd_conf->opts & OSPFD_OPT_VERBOSE); 212 213 if ((control_check(ospfd_conf->csock)) == -1) 214 fatalx("ospf6d already running"); 215 216 if (!debug) 217 daemon(1, 0); 218 219 log_info("startup"); 220 221 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 222 PF_UNSPEC, pipe_parent2ospfe) == -1) 223 fatal("socketpair"); 224 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 225 PF_UNSPEC, pipe_parent2rde) == -1) 226 fatal("socketpair"); 227 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 228 PF_UNSPEC, pipe_ospfe2rde) == -1) 229 fatal("socketpair"); 230 231 /* start children */ 232 rde_pid = rde(ospfd_conf, pipe_parent2rde, pipe_ospfe2rde, 233 pipe_parent2ospfe); 234 ospfe_pid = ospfe(ospfd_conf, pipe_parent2ospfe, pipe_ospfe2rde, 235 pipe_parent2rde); 236 237 event_init(); 238 239 /* setup signal handler */ 240 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL); 241 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL); 242 signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL); 243 signal_add(&ev_sigint, NULL); 244 signal_add(&ev_sigterm, NULL); 245 signal_add(&ev_sighup, NULL); 246 signal(SIGPIPE, SIG_IGN); 247 248 /* setup pipes to children */ 249 close(pipe_parent2ospfe[1]); 250 close(pipe_parent2rde[1]); 251 close(pipe_ospfe2rde[0]); 252 close(pipe_ospfe2rde[1]); 253 254 if ((iev_ospfe = malloc(sizeof(struct imsgev))) == NULL || 255 (iev_rde = malloc(sizeof(struct imsgev))) == NULL) 256 fatal(NULL); 257 imsg_init(&iev_ospfe->ibuf, pipe_parent2ospfe[0]); 258 iev_ospfe->handler = main_dispatch_ospfe; 259 imsg_init(&iev_rde->ibuf, pipe_parent2rde[0]); 260 iev_rde->handler = main_dispatch_rde; 261 262 /* setup event handler */ 263 iev_ospfe->events = EV_READ; 264 event_set(&iev_ospfe->ev, iev_ospfe->ibuf.fd, iev_ospfe->events, 265 iev_ospfe->handler, iev_ospfe); 266 event_add(&iev_ospfe->ev, NULL); 267 268 iev_rde->events = EV_READ; 269 event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events, 270 iev_rde->handler, iev_rde); 271 event_add(&iev_rde->ev, NULL); 272 273 if ((control_fd = control_init(ospfd_conf->csock)) == -1) 274 fatalx("control socket setup failed"); 275 main_imsg_compose_ospfe_fd(IMSG_CONTROLFD, 0, control_fd); 276 277 if (unveil(ospfd_conf->csock, "c") == -1) 278 fatal("unveil"); 279 if (unveil(NULL, NULL) == -1) 280 fatal("unveil"); 281 282 if (kr_init(!(ospfd_conf->flags & OSPFD_FLAG_NO_FIB_UPDATE), 283 ospfd_conf->rdomain, ospfd_conf->redist_label_or_prefix, 284 ospfd_conf->fib_priority) == -1) 285 fatalx("kr_init failed"); 286 287 event_dispatch(); 288 289 ospfd_shutdown(); 290 /* NOTREACHED */ 291 return (0); 292 } 293 294 __dead void 295 ospfd_shutdown(void) 296 { 297 pid_t pid; 298 int status; 299 300 /* close pipes */ 301 msgbuf_clear(&iev_ospfe->ibuf.w); 302 close(iev_ospfe->ibuf.fd); 303 msgbuf_clear(&iev_rde->ibuf.w); 304 close(iev_rde->ibuf.fd); 305 306 control_cleanup(ospfd_conf->csock); 307 kr_shutdown(); 308 carp_demote_shutdown(); 309 310 log_debug("waiting for children to terminate"); 311 do { 312 pid = wait(&status); 313 if (pid == -1) { 314 if (errno != EINTR && errno != ECHILD) 315 fatal("wait"); 316 } else if (WIFSIGNALED(status)) 317 log_warnx("%s terminated; signal %d", 318 (pid == rde_pid) ? "route decision engine" : 319 "ospf engine", WTERMSIG(status)); 320 } while (pid != -1 || (pid == -1 && errno == EINTR)); 321 322 free(iev_ospfe); 323 free(iev_rde); 324 free(ospfd_conf); 325 326 log_info("terminating"); 327 exit(0); 328 } 329 330 /* imsg handling */ 331 /* ARGSUSED */ 332 void 333 main_dispatch_ospfe(int fd, short event, void *bula) 334 { 335 struct imsgev *iev = bula; 336 struct imsgbuf *ibuf = &iev->ibuf; 337 struct imsg imsg; 338 struct demote_msg dmsg; 339 ssize_t n; 340 int shut = 0, verbose; 341 342 if (event & EV_READ) { 343 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 344 fatal("imsg_read error"); 345 if (n == 0) /* connection closed */ 346 shut = 1; 347 } 348 if (event & EV_WRITE) { 349 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 350 fatal("msgbuf_write"); 351 if (n == 0) /* connection closed */ 352 shut = 1; 353 } 354 355 for (;;) { 356 if ((n = imsg_get(ibuf, &imsg)) == -1) 357 fatal("imsg_get"); 358 359 if (n == 0) 360 break; 361 362 switch (imsg.hdr.type) { 363 case IMSG_CTL_RELOAD: 364 if (ospf_reload() == -1) 365 log_warnx("configuration reload failed"); 366 else 367 log_debug("configuration reloaded"); 368 break; 369 case IMSG_CTL_FIB_COUPLE: 370 kr_fib_couple(); 371 break; 372 case IMSG_CTL_FIB_DECOUPLE: 373 kr_fib_decouple(); 374 break; 375 case IMSG_CTL_KROUTE: 376 case IMSG_CTL_KROUTE_ADDR: 377 kr_show_route(&imsg); 378 break; 379 case IMSG_DEMOTE: 380 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(dmsg)) 381 fatalx("invalid size of OE request"); 382 memcpy(&dmsg, imsg.data, sizeof(dmsg)); 383 carp_demote_set(dmsg.demote_group, dmsg.level); 384 break; 385 case IMSG_CTL_LOG_VERBOSE: 386 /* already checked by ospfe */ 387 memcpy(&verbose, imsg.data, sizeof(verbose)); 388 log_setverbose(verbose); 389 break; 390 default: 391 log_debug("main_dispatch_ospfe: error handling imsg %d", 392 imsg.hdr.type); 393 break; 394 } 395 imsg_free(&imsg); 396 } 397 if (!shut) 398 imsg_event_add(iev); 399 else { 400 /* this pipe is dead, so remove the event handler */ 401 event_del(&iev->ev); 402 event_loopexit(NULL); 403 } 404 } 405 406 /* ARGSUSED */ 407 void 408 main_dispatch_rde(int fd, short event, void *bula) 409 { 410 struct imsgev *iev = bula; 411 struct imsgbuf *ibuf = &iev->ibuf; 412 struct imsg imsg; 413 ssize_t n; 414 int count, shut = 0; 415 416 if (event & EV_READ) { 417 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 418 fatal("imsg_read error"); 419 if (n == 0) /* connection closed */ 420 shut = 1; 421 } 422 if (event & EV_WRITE) { 423 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 424 fatal("msgbuf_write"); 425 if (n == 0) /* connection closed */ 426 shut = 1; 427 } 428 429 for (;;) { 430 if ((n = imsg_get(ibuf, &imsg)) == -1) 431 fatal("imsg_get"); 432 433 if (n == 0) 434 break; 435 436 switch (imsg.hdr.type) { 437 case IMSG_KROUTE_CHANGE: 438 count = (imsg.hdr.len - IMSG_HEADER_SIZE) / 439 sizeof(struct kroute); 440 if (kr_change(imsg.data, count)) 441 log_warn("main_dispatch_rde: error changing " 442 "route"); 443 break; 444 case IMSG_KROUTE_DELETE: 445 if (kr_delete(imsg.data)) 446 log_warn("main_dispatch_rde: error deleting " 447 "route"); 448 break; 449 default: 450 log_debug("main_dispatch_rde: error handling imsg %d", 451 imsg.hdr.type); 452 break; 453 } 454 imsg_free(&imsg); 455 } 456 if (!shut) 457 imsg_event_add(iev); 458 else { 459 /* this pipe is dead, so remove the event handler */ 460 event_del(&iev->ev); 461 event_loopexit(NULL); 462 } 463 } 464 465 void 466 main_imsg_compose_ospfe(int type, pid_t pid, void *data, u_int16_t datalen) 467 { 468 if (iev_ospfe == NULL) 469 return; 470 imsg_compose_event(iev_ospfe, type, 0, pid, -1, data, datalen); 471 } 472 473 void 474 main_imsg_compose_ospfe_fd(int type, pid_t pid, int fd) 475 { 476 if (iev_ospfe == NULL) 477 return; 478 imsg_compose_event(iev_ospfe, type, 0, pid, fd, NULL, 0); 479 } 480 481 void 482 main_imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen) 483 { 484 if (iev_rde == NULL) 485 return; 486 imsg_compose_event(iev_rde, type, 0, pid, -1, data, datalen); 487 } 488 489 void 490 imsg_event_add(struct imsgev *iev) 491 { 492 iev->events = EV_READ; 493 if (iev->ibuf.w.queued) 494 iev->events |= EV_WRITE; 495 496 event_del(&iev->ev); 497 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); 498 event_add(&iev->ev, NULL); 499 } 500 501 int 502 imsg_compose_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid, 503 pid_t pid, int fd, void *data, u_int16_t datalen) 504 { 505 int ret; 506 507 if ((ret = imsg_compose(&iev->ibuf, type, peerid, 508 pid, fd, data, datalen)) != -1) 509 imsg_event_add(iev); 510 return (ret); 511 } 512 513 int 514 ospf_redistribute(struct kroute *kr, u_int32_t *metric) 515 { 516 struct redistribute *r; 517 struct in6_addr ina, inb; 518 struct iface *iface; 519 u_int8_t is_default = 0; 520 int depend_ok; 521 522 /* only allow ::/0 via REDIST_DEFAULT */ 523 if (IN6_IS_ADDR_UNSPECIFIED(&kr->prefix) && kr->prefixlen == 0) 524 is_default = 1; 525 526 SIMPLEQ_FOREACH(r, &ospfd_conf->redist_list, entry) { 527 if (r->dependon[0] != '\0') { 528 if ((iface = if_findname(r->dependon))) 529 depend_ok = ifstate_is_up(iface); 530 else 531 depend_ok = 0; 532 } else 533 depend_ok = 1; 534 535 switch (r->type & ~REDIST_NO) { 536 case REDIST_LABEL: 537 if (kr->rtlabel == r->label) { 538 *metric = depend_ok ? r->metric : 539 r->metric | MAX_METRIC; 540 return (r->type & REDIST_NO ? 0 : 1); 541 } 542 break; 543 case REDIST_STATIC: 544 /* 545 * Dynamic routes are not redistributable. Placed here 546 * so that link local addresses can be redistributed 547 * via a rtlabel. 548 */ 549 if (is_default) 550 continue; 551 if (kr->flags & F_DYNAMIC) 552 continue; 553 if (kr->flags & F_STATIC) { 554 *metric = depend_ok ? r->metric : 555 r->metric | MAX_METRIC; 556 return (r->type & REDIST_NO ? 0 : 1); 557 } 558 break; 559 case REDIST_CONNECTED: 560 if (is_default) 561 continue; 562 if (kr->flags & F_DYNAMIC) 563 continue; 564 if (kr->flags & F_CONNECTED) { 565 *metric = depend_ok ? r->metric : 566 r->metric | MAX_METRIC; 567 return (r->type & REDIST_NO ? 0 : 1); 568 } 569 break; 570 case REDIST_ADDR: 571 if (kr->flags & F_DYNAMIC) 572 continue; 573 574 if (IN6_IS_ADDR_UNSPECIFIED(&r->addr) && 575 r->prefixlen == 0) { 576 if (is_default) { 577 *metric = depend_ok ? r->metric : 578 r->metric | MAX_METRIC; 579 return (r->type & REDIST_NO ? 0 : 1); 580 } else 581 return (0); 582 } 583 584 inet6applymask(&ina, &kr->prefix, r->prefixlen); 585 inet6applymask(&inb, &r->addr, r->prefixlen); 586 if (IN6_ARE_ADDR_EQUAL(&ina, &inb) && 587 kr->prefixlen >= r->prefixlen) { 588 *metric = depend_ok ? r->metric : 589 r->metric | MAX_METRIC; 590 return (r->type & REDIST_NO ? 0 : 1); 591 } 592 break; 593 case REDIST_DEFAULT: 594 if (is_default) { 595 *metric = depend_ok ? r->metric : 596 r->metric | MAX_METRIC; 597 return (r->type & REDIST_NO ? 0 : 1); 598 } 599 break; 600 } 601 } 602 603 return (0); 604 } 605 606 int 607 ospf_reload(void) 608 { 609 #ifdef notyet 610 struct area *area; 611 struct ospfd_conf *xconf; 612 613 if ((xconf = parse_config(conffile, ospfd_conf->opts)) == NULL) 614 return (-1); 615 616 /* XXX bail out if router-id changed */ 617 618 /* send config to childs */ 619 if (ospf_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1) 620 return (-1); 621 622 /* send areas, interfaces happen out of band */ 623 LIST_FOREACH(area, &xconf->area_list, entry) { 624 if (ospf_sendboth(IMSG_RECONF_AREA, area, sizeof(*area)) == -1) 625 return (-1); 626 } 627 628 if (ospf_sendboth(IMSG_RECONF_END, NULL, 0) == -1) 629 return (-1); 630 631 /* XXX send newly available interfaces to the childs */ 632 633 merge_config(ospfd_conf, xconf); 634 /* update redistribute lists */ 635 kr_reload(ospfd_conf->redist_label_or_prefix); 636 return (0); 637 #else 638 return (-1); 639 #endif 640 } 641 642 int 643 ospf_sendboth(enum imsg_type type, void *buf, u_int16_t len) 644 { 645 if (imsg_compose_event(iev_ospfe, type, 0, 0, -1, buf, len) == -1) 646 return (-1); 647 if (imsg_compose_event(iev_rde, type, 0, 0, -1, buf, len) == -1) 648 return (-1); 649 return (0); 650 } 651 652 void 653 merge_config(struct ospfd_conf *conf, struct ospfd_conf *xconf) 654 { 655 struct area *a, *xa, *na; 656 struct iface *iface; 657 struct redistribute *r; 658 int rchange = 0; 659 660 /* change of rtr_id needs a restart */ 661 conf->flags = xconf->flags; 662 conf->spf_delay = xconf->spf_delay; 663 conf->spf_hold_time = xconf->spf_hold_time; 664 if (SIMPLEQ_EMPTY(&conf->redist_list) != 665 SIMPLEQ_EMPTY(&xconf->redist_list)) 666 rchange = 1; 667 conf->redist_label_or_prefix = xconf->redist_label_or_prefix; 668 669 if (ospfd_process == PROC_MAIN) { 670 /* main process does neither use areas nor interfaces */ 671 while ((r = SIMPLEQ_FIRST(&conf->redist_list)) != NULL) { 672 SIMPLEQ_REMOVE_HEAD(&conf->redist_list, entry); 673 free(r); 674 } 675 SIMPLEQ_CONCAT(&conf->redist_list, &xconf->redist_list); 676 677 /* adjust FIB priority if changed */ 678 if (conf->fib_priority != xconf->fib_priority) { 679 kr_fib_decouple(); 680 kr_fib_update_prio(xconf->fib_priority); 681 conf->fib_priority = xconf->fib_priority; 682 kr_fib_couple(); 683 } 684 685 goto done; 686 } 687 688 /* merge areas and interfaces */ 689 for (a = LIST_FIRST(&conf->area_list); a != NULL; a = na) { 690 na = LIST_NEXT(a, entry); 691 /* find deleted areas */ 692 if ((xa = area_find(xconf, a->id)) == NULL) { 693 if (ospfd_process == PROC_OSPF_ENGINE) { 694 LIST_FOREACH(iface, &a->iface_list, entry) 695 if_fsm(iface, IF_EVT_DOWN); 696 } 697 LIST_REMOVE(a, entry); 698 area_del(a); 699 } 700 } 701 702 for (xa = LIST_FIRST(&xconf->area_list); xa != NULL; xa = na) { 703 na = LIST_NEXT(xa, entry); 704 if ((a = area_find(conf, xa->id)) == NULL) { 705 LIST_REMOVE(xa, entry); 706 LIST_INSERT_HEAD(&conf->area_list, xa, entry); 707 if (ospfd_process == PROC_OSPF_ENGINE) { 708 /* start interfaces */ 709 ospfe_demote_area(xa, 0); 710 LIST_FOREACH(iface, &xa->iface_list, entry) 711 if_start(conf, iface); 712 } 713 /* no need to merge interfaces */ 714 continue; 715 } 716 /* 717 * stub is not yet used but switching between stub and normal 718 * will be another painful job. 719 */ 720 a->stub = xa->stub; 721 a->stub_default_cost = xa->stub_default_cost; 722 if (ospfd_process == PROC_RDE_ENGINE) 723 a->dirty = 1; /* force SPF tree recalculation */ 724 725 /* merge interfaces */ 726 if (merge_interfaces(a, xa) && 727 ospfd_process == PROC_OSPF_ENGINE) 728 a->dirty = 1; /* force rtr LSA update */ 729 } 730 731 if (ospfd_process == PROC_OSPF_ENGINE) { 732 LIST_FOREACH(a, &conf->area_list, entry) { 733 LIST_FOREACH(iface, &a->iface_list, entry) { 734 if (iface->state == IF_STA_NEW) { 735 iface->state = IF_STA_DOWN; 736 if_start(conf, iface); 737 } 738 } 739 if (a->dirty) { 740 a->dirty = 0; 741 orig_rtr_lsa(LIST_FIRST(&a->iface_list)->area); 742 } 743 } 744 } 745 746 done: 747 while ((a = LIST_FIRST(&xconf->area_list)) != NULL) { 748 LIST_REMOVE(a, entry); 749 area_del(a); 750 } 751 free(xconf); 752 } 753 754 int 755 merge_interfaces(struct area *a, struct area *xa) 756 { 757 struct iface *i, *xi, *ni; 758 int dirty = 0; 759 760 /* problems: 761 * - new interfaces (easy) 762 * - deleted interfaces (needs to be done via fsm?) 763 * - changing passive (painful?) 764 */ 765 for (i = LIST_FIRST(&a->iface_list); i != NULL; i = ni) { 766 ni = LIST_NEXT(i, entry); 767 if (iface_lookup(xa, i) == NULL) { 768 log_debug("merge_interfaces: proc %d area %s removing " 769 "interface %s", ospfd_process, inet_ntoa(a->id), 770 i->name); 771 if (ospfd_process == PROC_OSPF_ENGINE) 772 if_fsm(i, IF_EVT_DOWN); 773 LIST_REMOVE(i, entry); 774 if_del(i); 775 } 776 } 777 778 for (xi = LIST_FIRST(&xa->iface_list); xi != NULL; xi = ni) { 779 ni = LIST_NEXT(xi, entry); 780 if ((i = iface_lookup(a, xi)) == NULL) { 781 /* new interface but delay initialisation */ 782 log_debug("merge_interfaces: proc %d area %s adding " 783 "interface %s", ospfd_process, inet_ntoa(a->id), 784 xi->name); 785 LIST_REMOVE(xi, entry); 786 LIST_INSERT_HEAD(&a->iface_list, xi, entry); 787 if (ospfd_process == PROC_OSPF_ENGINE) 788 xi->state = IF_STA_NEW; 789 continue; 790 } 791 log_debug("merge_interfaces: proc %d area %s merging " 792 "interface %s", ospfd_process, inet_ntoa(a->id), i->name); 793 i->addr = xi->addr; 794 i->dst = xi->dst; 795 i->abr_id = xi->abr_id; 796 i->baudrate = xi->baudrate; 797 i->dead_interval = xi->dead_interval; 798 i->mtu = xi->mtu; 799 i->transmit_delay = xi->transmit_delay; 800 i->hello_interval = xi->hello_interval; 801 i->rxmt_interval = xi->rxmt_interval; 802 if (i->metric != xi->metric) 803 dirty = 1; 804 i->metric = xi->metric; 805 i->priority = xi->priority; 806 if (i->self) 807 i->self->priority = i->priority; 808 i->flags = xi->flags; /* needed? */ 809 i->type = xi->type; /* needed? */ 810 i->if_type = xi->if_type; /* needed? */ 811 i->linkstate = xi->linkstate; /* needed? */ 812 813 #if 0 /* XXX needs some kind of love */ 814 if (i->passive != xi->passive) { 815 /* need to restart interface to cope with this change */ 816 if (ospfd_process == PROC_OSPF_ENGINE) 817 if_fsm(i, IF_EVT_DOWN); 818 i->passive = xi->passive; 819 if (ospfd_process == PROC_OSPF_ENGINE) 820 if_fsm(i, IF_EVT_UP); 821 } 822 #endif 823 } 824 return (dirty); 825 } 826 827 struct iface * 828 iface_lookup(struct area *area, struct iface *iface) 829 { 830 struct iface *i; 831 832 LIST_FOREACH(i, &area->iface_list, entry) 833 if (i->ifindex == iface->ifindex) 834 return (i); 835 return (NULL); 836 } 837 838 int 839 ifstate_is_up(struct iface *iface) 840 { 841 if (!(iface->flags & IFF_UP)) 842 return (0); 843 if (iface->if_type == IFT_CARP && 844 iface->linkstate == LINK_STATE_UNKNOWN) 845 return (0); 846 return LINK_STATE_IS_UP(iface->linkstate); 847 } 848