1 /* $OpenBSD: ospfe.c,v 1.64 2021/01/19 09:42:11 claudio Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2004 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 <netinet/in.h> 25 #include <arpa/inet.h> 26 #include <net/if_types.h> 27 #include <stdlib.h> 28 #include <signal.h> 29 #include <string.h> 30 #include <fcntl.h> 31 #include <pwd.h> 32 #include <unistd.h> 33 #include <event.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <stdio.h> 37 38 #include "ospf6.h" 39 #include "ospf6d.h" 40 #include "ospfe.h" 41 #include "rde.h" 42 #include "control.h" 43 #include "log.h" 44 45 void ospfe_sig_handler(int, short, void *); 46 __dead void ospfe_shutdown(void); 47 void orig_rtr_lsa_all(struct area *); 48 struct iface *find_vlink(struct abr_rtr *); 49 50 struct ospfd_conf *oeconf = NULL, *nconf; 51 struct imsgev *iev_main; 52 struct imsgev *iev_rde; 53 int oe_nofib; 54 55 /* ARGSUSED */ 56 void 57 ospfe_sig_handler(int sig, short event, void *bula) 58 { 59 switch (sig) { 60 case SIGINT: 61 case SIGTERM: 62 ospfe_shutdown(); 63 /* NOTREACHED */ 64 default: 65 fatalx("unexpected signal"); 66 } 67 } 68 69 /* ospf engine */ 70 pid_t 71 ospfe(struct ospfd_conf *xconf, int pipe_parent2ospfe[2], int pipe_ospfe2rde[2], 72 int pipe_parent2rde[2]) 73 { 74 struct area *area; 75 struct iface *iface; 76 struct passwd *pw; 77 struct event ev_sigint, ev_sigterm; 78 pid_t pid; 79 80 switch (pid = fork()) { 81 case -1: 82 fatal("cannot fork"); 83 case 0: 84 break; 85 default: 86 return (pid); 87 } 88 89 /* create the raw ip socket */ 90 if ((xconf->ospf_socket = socket(AF_INET6, 91 SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_OSPF)) == -1) 92 fatal("error creating raw socket"); 93 94 /* set some defaults */ 95 if (if_set_mcast_loop(xconf->ospf_socket) == -1) 96 fatal("if_set_mcast_loop"); 97 if (if_set_ipv6_checksum(xconf->ospf_socket) == -1) 98 fatal("if_set_ipv6_checksum"); 99 if (if_set_ipv6_pktinfo(xconf->ospf_socket, 1) == -1) 100 fatal("if_set_ipv6_pktinfo"); 101 if_set_sockbuf(xconf->ospf_socket); 102 103 oeconf = xconf; 104 if (oeconf->flags & OSPFD_FLAG_NO_FIB_UPDATE) 105 oe_nofib = 1; 106 107 if ((pw = getpwnam(OSPF6D_USER)) == NULL) 108 fatal("getpwnam"); 109 110 if (chroot(pw->pw_dir) == -1) 111 fatal("chroot"); 112 if (chdir("/") == -1) 113 fatal("chdir(\"/\")"); 114 115 setproctitle("ospf engine"); 116 /* 117 * XXX needed with fork+exec 118 * log_init(debug, LOG_DAEMON); 119 * log_setverbose(verbose); 120 */ 121 122 ospfd_process = PROC_OSPF_ENGINE; 123 log_procinit(log_procnames[ospfd_process]); 124 125 if (setgroups(1, &pw->pw_gid) || 126 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 127 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 128 fatal("can't drop privileges"); 129 130 if (pledge("stdio inet mcast recvfd", NULL) == -1) 131 fatal("pledge"); 132 133 event_init(); 134 nbr_init(NBR_HASHSIZE); 135 lsa_cache_init(LSA_HASHSIZE); 136 137 /* setup signal handler */ 138 signal_set(&ev_sigint, SIGINT, ospfe_sig_handler, NULL); 139 signal_set(&ev_sigterm, SIGTERM, ospfe_sig_handler, NULL); 140 signal_add(&ev_sigint, NULL); 141 signal_add(&ev_sigterm, NULL); 142 signal(SIGPIPE, SIG_IGN); 143 signal(SIGHUP, SIG_IGN); 144 145 /* setup pipes */ 146 close(pipe_parent2ospfe[0]); 147 close(pipe_ospfe2rde[1]); 148 close(pipe_parent2rde[0]); 149 close(pipe_parent2rde[1]); 150 151 if ((iev_rde = malloc(sizeof(struct imsgev))) == NULL || 152 (iev_main = malloc(sizeof(struct imsgev))) == NULL) 153 fatal(NULL); 154 imsg_init(&iev_rde->ibuf, pipe_ospfe2rde[0]); 155 iev_rde->handler = ospfe_dispatch_rde; 156 imsg_init(&iev_main->ibuf, pipe_parent2ospfe[1]); 157 iev_main->handler = ospfe_dispatch_main; 158 159 /* setup event handler */ 160 iev_rde->events = EV_READ; 161 event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events, 162 iev_rde->handler, iev_rde); 163 event_add(&iev_rde->ev, NULL); 164 165 iev_main->events = EV_READ; 166 event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events, 167 iev_main->handler, iev_main); 168 event_add(&iev_main->ev, NULL); 169 170 event_set(&oeconf->ev, oeconf->ospf_socket, EV_READ|EV_PERSIST, 171 recv_packet, oeconf); 172 event_add(&oeconf->ev, NULL); 173 174 /* remove unneeded config stuff */ 175 conf_clear_redist_list(&oeconf->redist_list); 176 177 if ((pkt_ptr = calloc(1, READ_BUF_SIZE)) == NULL) 178 fatal("ospfe"); 179 180 /* start interfaces */ 181 LIST_FOREACH(area, &oeconf->area_list, entry) { 182 ospfe_demote_area(area, 0); 183 LIST_FOREACH(iface, &area->iface_list, entry) 184 if_start(xconf, iface); 185 } 186 187 event_dispatch(); 188 189 ospfe_shutdown(); 190 /* NOTREACHED */ 191 return (0); 192 } 193 194 __dead void 195 ospfe_shutdown(void) 196 { 197 struct area *area; 198 struct iface *iface; 199 200 /* close pipes */ 201 msgbuf_write(&iev_rde->ibuf.w); 202 msgbuf_clear(&iev_rde->ibuf.w); 203 close(iev_rde->ibuf.fd); 204 msgbuf_write(&iev_main->ibuf.w); 205 msgbuf_clear(&iev_main->ibuf.w); 206 close(iev_main->ibuf.fd); 207 208 /* stop all interfaces and remove all areas */ 209 while ((area = LIST_FIRST(&oeconf->area_list)) != NULL) { 210 LIST_FOREACH(iface, &area->iface_list, entry) { 211 if (if_fsm(iface, IF_EVT_DOWN)) { 212 log_debug("error stopping interface %s", 213 iface->name); 214 } 215 } 216 LIST_REMOVE(area, entry); 217 area_del(area); 218 } 219 220 close(oeconf->ospf_socket); 221 222 /* clean up */ 223 free(iev_rde); 224 free(iev_main); 225 free(oeconf); 226 free(pkt_ptr); 227 228 log_info("ospf engine exiting"); 229 _exit(0); 230 } 231 232 /* imesg */ 233 int 234 ospfe_imsg_compose_parent(int type, pid_t pid, void *data, u_int16_t datalen) 235 { 236 return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen)); 237 } 238 239 int 240 ospfe_imsg_compose_rde(int type, u_int32_t peerid, pid_t pid, 241 void *data, u_int16_t datalen) 242 { 243 return (imsg_compose_event(iev_rde, type, peerid, pid, -1, 244 data, datalen)); 245 } 246 247 /* ARGSUSED */ 248 void 249 ospfe_dispatch_main(int fd, short event, void *bula) 250 { 251 static struct area *narea; 252 struct area *area; 253 struct iface *iface, *ifp, *i; 254 struct ifaddrchange *ifc; 255 struct iface_addr *ia, *nia; 256 struct imsg imsg; 257 struct imsgev *iev = bula; 258 struct imsgbuf *ibuf = &iev->ibuf; 259 int n, stub_changed, shut = 0, isvalid, wasvalid; 260 261 if (event & EV_READ) { 262 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 263 fatal("imsg_read error"); 264 if (n == 0) /* connection closed */ 265 shut = 1; 266 } 267 if (event & EV_WRITE) { 268 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 269 fatal("msgbuf_write"); 270 if (n == 0) /* connection closed */ 271 shut = 1; 272 } 273 274 for (;;) { 275 if ((n = imsg_get(ibuf, &imsg)) == -1) 276 fatal("ospfe_dispatch_main: imsg_get error"); 277 if (n == 0) 278 break; 279 280 switch (imsg.hdr.type) { 281 case IMSG_IFINFO: 282 if (imsg.hdr.len != IMSG_HEADER_SIZE + 283 sizeof(struct iface)) 284 fatalx("IFINFO imsg with wrong len"); 285 ifp = imsg.data; 286 287 LIST_FOREACH(area, &oeconf->area_list, entry) { 288 LIST_FOREACH(i, &area->iface_list, entry) { 289 if (strcmp(i->dependon, 290 ifp->name) == 0) { 291 log_warnx("interface %s" 292 " changed state, %s" 293 " depends on it", 294 ifp->name, i->name); 295 i->depend_ok = 296 ifstate_is_up(ifp); 297 if (ifstate_is_up(i)) 298 orig_rtr_lsa(i->area); 299 } 300 } 301 } 302 303 if (!(ifp->cflags & F_IFACE_CONFIGURED)) 304 break; 305 iface = if_find(ifp->ifindex); 306 if (iface == NULL) 307 fatalx("interface lost in ospfe"); 308 309 wasvalid = (iface->flags & IFF_UP) && 310 LINK_STATE_IS_UP(iface->linkstate); 311 312 if_update(iface, ifp->mtu, ifp->flags, ifp->if_type, 313 ifp->linkstate, ifp->baudrate, ifp->rdomain); 314 315 isvalid = (iface->flags & IFF_UP) && 316 LINK_STATE_IS_UP(iface->linkstate); 317 318 if (wasvalid == isvalid) 319 break; 320 321 if (isvalid) { 322 if_fsm(iface, IF_EVT_UP); 323 log_warnx("interface %s up", iface->name); 324 } else { 325 if_fsm(iface, IF_EVT_DOWN); 326 log_warnx("interface %s down", iface->name); 327 } 328 break; 329 case IMSG_IFADDRNEW: 330 if (imsg.hdr.len != IMSG_HEADER_SIZE + 331 sizeof(struct ifaddrchange)) 332 fatalx("IFADDRNEW imsg with wrong len"); 333 ifc = imsg.data; 334 335 iface = if_find(ifc->ifindex); 336 if (iface == NULL) 337 fatalx("IFADDRNEW interface lost in ospfe"); 338 339 if ((ia = calloc(1, sizeof(struct iface_addr))) == 340 NULL) 341 fatal("ospfe_dispatch_main IFADDRNEW"); 342 ia->addr = ifc->addr; 343 ia->dstbrd = ifc->dstbrd; 344 ia->prefixlen = ifc->prefixlen; 345 346 TAILQ_INSERT_TAIL(&iface->ifa_list, ia, entry); 347 orig_link_lsa(iface); 348 break; 349 case IMSG_IFADDRDEL: 350 if (imsg.hdr.len != IMSG_HEADER_SIZE + 351 sizeof(struct ifaddrchange)) 352 fatalx("IFADDRDEL imsg with wrong len"); 353 ifc = imsg.data; 354 355 iface = if_find(ifc->ifindex); 356 if (iface == NULL) 357 fatalx("IFADDRDEL interface lost in ospfe"); 358 359 for (ia = TAILQ_FIRST(&iface->ifa_list); ia != NULL; 360 ia = nia) { 361 nia = TAILQ_NEXT(ia, entry); 362 363 if (IN6_ARE_ADDR_EQUAL(&ia->addr, 364 &ifc->addr)) { 365 TAILQ_REMOVE(&iface->ifa_list, ia, 366 entry); 367 free(ia); 368 break; 369 } 370 } 371 orig_link_lsa(iface); 372 break; 373 case IMSG_RECONF_CONF: 374 if ((nconf = malloc(sizeof(struct ospfd_conf))) == 375 NULL) 376 fatal(NULL); 377 memcpy(nconf, imsg.data, sizeof(struct ospfd_conf)); 378 379 LIST_INIT(&nconf->area_list); 380 LIST_INIT(&nconf->cand_list); 381 break; 382 case IMSG_RECONF_AREA: 383 if ((narea = area_new()) == NULL) 384 fatal(NULL); 385 memcpy(narea, imsg.data, sizeof(struct area)); 386 387 LIST_INIT(&narea->iface_list); 388 LIST_INIT(&narea->nbr_list); 389 RB_INIT(&narea->lsa_tree); 390 391 LIST_INSERT_HEAD(&nconf->area_list, narea, entry); 392 break; 393 case IMSG_RECONF_END: 394 if ((oeconf->flags & OSPFD_FLAG_STUB_ROUTER) != 395 (nconf->flags & OSPFD_FLAG_STUB_ROUTER)) 396 stub_changed = 1; 397 else 398 stub_changed = 0; 399 merge_config(oeconf, nconf); 400 nconf = NULL; 401 if (stub_changed) 402 orig_rtr_lsa_all(NULL); 403 break; 404 case IMSG_CTL_KROUTE: 405 case IMSG_CTL_KROUTE_ADDR: 406 case IMSG_CTL_END: 407 control_imsg_relay(&imsg); 408 break; 409 case IMSG_CONTROLFD: 410 if ((fd = imsg.fd) == -1) 411 fatalx("%s: expected to receive imsg control" 412 "fd but didn't receive any", __func__); 413 /* Listen on control socket. */ 414 control_listen(fd); 415 if (pledge("stdio inet mcast", NULL) == -1) 416 fatal("pledge"); 417 break; 418 default: 419 log_debug("ospfe_dispatch_main: error handling imsg %d", 420 imsg.hdr.type); 421 break; 422 } 423 imsg_free(&imsg); 424 } 425 if (!shut) 426 imsg_event_add(iev); 427 else { 428 /* this pipe is dead, so remove the event handler */ 429 event_del(&iev->ev); 430 event_loopexit(NULL); 431 } 432 } 433 434 /* ARGSUSED */ 435 void 436 ospfe_dispatch_rde(int fd, short event, void *bula) 437 { 438 struct lsa_hdr lsa_hdr; 439 struct lsa_link lsa_link; 440 struct imsgev *iev = bula; 441 struct imsgbuf *ibuf = &iev->ibuf; 442 struct nbr *nbr; 443 struct lsa_hdr *lhp; 444 struct lsa_ref *ref; 445 struct area *area; 446 struct iface *iface; 447 struct lsa_entry *le; 448 struct imsg imsg; 449 struct abr_rtr ar; 450 int n, noack = 0, shut = 0; 451 u_int16_t l, age; 452 453 if (event & EV_READ) { 454 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 455 fatal("imsg_read error"); 456 if (n == 0) /* connection closed */ 457 shut = 1; 458 } 459 if (event & EV_WRITE) { 460 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 461 fatal("msgbuf_write"); 462 if (n == 0) /* connection closed */ 463 shut = 1; 464 } 465 466 for (;;) { 467 if ((n = imsg_get(ibuf, &imsg)) == -1) 468 fatal("ospfe_dispatch_rde: imsg_get error"); 469 if (n == 0) 470 break; 471 472 switch (imsg.hdr.type) { 473 case IMSG_DD: 474 nbr = nbr_find_peerid(imsg.hdr.peerid); 475 if (nbr == NULL) 476 break; 477 478 /* put these on my ls_req_list for retrieval */ 479 lhp = lsa_hdr_new(); 480 memcpy(lhp, imsg.data, sizeof(*lhp)); 481 ls_req_list_add(nbr, lhp); 482 break; 483 case IMSG_DD_END: 484 nbr = nbr_find_peerid(imsg.hdr.peerid); 485 if (nbr == NULL) 486 break; 487 488 nbr->dd_pending--; 489 if (nbr->dd_pending == 0 && nbr->state & NBR_STA_LOAD) { 490 if (ls_req_list_empty(nbr)) 491 nbr_fsm(nbr, NBR_EVT_LOAD_DONE); 492 else 493 start_ls_req_tx_timer(nbr); 494 } 495 break; 496 case IMSG_DB_SNAPSHOT: 497 nbr = nbr_find_peerid(imsg.hdr.peerid); 498 if (nbr == NULL) 499 break; 500 if (nbr->state != NBR_STA_SNAP) /* discard */ 501 break; 502 503 /* add LSA header to the neighbor db_sum_list */ 504 lhp = lsa_hdr_new(); 505 memcpy(lhp, imsg.data, sizeof(*lhp)); 506 db_sum_list_add(nbr, lhp); 507 break; 508 case IMSG_DB_END: 509 nbr = nbr_find_peerid(imsg.hdr.peerid); 510 if (nbr == NULL) 511 break; 512 513 nbr->dd_snapshot = 0; 514 if (nbr->state != NBR_STA_SNAP) 515 break; 516 517 /* snapshot done, start tx of dd packets */ 518 nbr_fsm(nbr, NBR_EVT_SNAP_DONE); 519 break; 520 case IMSG_LS_FLOOD: 521 nbr = nbr_find_peerid(imsg.hdr.peerid); 522 if (nbr == NULL) 523 break; 524 525 l = imsg.hdr.len - IMSG_HEADER_SIZE; 526 if (l < sizeof(lsa_hdr)) 527 fatalx("ospfe_dispatch_rde: " 528 "bad imsg size"); 529 memcpy(&lsa_hdr, imsg.data, sizeof(lsa_hdr)); 530 531 ref = lsa_cache_add(imsg.data, l); 532 533 if (lsa_hdr.type == htons(LSA_TYPE_EXTERNAL)) { 534 /* 535 * flood on all areas but stub areas and 536 * virtual links 537 */ 538 LIST_FOREACH(area, &oeconf->area_list, entry) { 539 if (area->stub) 540 continue; 541 LIST_FOREACH(iface, &area->iface_list, 542 entry) { 543 noack += lsa_flood(iface, nbr, 544 &lsa_hdr, imsg.data); 545 } 546 } 547 } else if (lsa_hdr.type == htons(LSA_TYPE_LINK)) { 548 /* 549 * Save link-LSA options of neighbor. 550 * This is needed to originate network-LSA. 551 */ 552 if (l - sizeof(lsa_hdr) < sizeof(lsa_link)) 553 fatalx("ospfe_dispatch_rde: " 554 "bad imsg link size"); 555 memcpy(&lsa_link, (char *)imsg.data + 556 sizeof(lsa_hdr), sizeof(lsa_link)); 557 nbr->link_options = lsa_link.opts & 558 htonl(LSA_24_MASK); 559 560 /* 561 * flood on interface only 562 */ 563 noack += lsa_flood(nbr->iface, nbr, 564 &lsa_hdr, imsg.data); 565 } else { 566 /* 567 * flood on all area interfaces on 568 * area 0.0.0.0 include also virtual links. 569 */ 570 LIST_FOREACH(iface, 571 &nbr->iface->area->iface_list, entry) { 572 noack += lsa_flood(iface, nbr, 573 &lsa_hdr, imsg.data); 574 } 575 /* XXX virtual links */ 576 } 577 578 /* remove from ls_req_list */ 579 le = ls_req_list_get(nbr, &lsa_hdr); 580 if (!(nbr->state & NBR_STA_FULL) && le != NULL) { 581 ls_req_list_free(nbr, le); 582 /* 583 * XXX no need to ack requested lsa 584 * the problem is that the RFC is very 585 * unclear about this. 586 */ 587 noack = 1; 588 } 589 590 if (!noack && nbr->iface != NULL && 591 nbr->iface->self != nbr) { 592 if (!(nbr->iface->state & IF_STA_BACKUP) || 593 nbr->iface->dr == nbr) { 594 /* delayed ack */ 595 lhp = lsa_hdr_new(); 596 memcpy(lhp, &lsa_hdr, sizeof(*lhp)); 597 ls_ack_list_add(nbr->iface, lhp); 598 } 599 } 600 601 lsa_cache_put(ref, nbr); 602 break; 603 case IMSG_LS_UPD: 604 case IMSG_LS_SNAP: 605 /* 606 * IMSG_LS_UPD is used in two cases: 607 * 1. as response to ls requests 608 * 2. as response to ls updates where the DB 609 * is newer then the sent LSA 610 * IMSG_LS_SNAP is used in one case: 611 * in EXSTART when the LSA has age MaxAge 612 */ 613 l = imsg.hdr.len - IMSG_HEADER_SIZE; 614 if (l < sizeof(lsa_hdr)) 615 fatalx("ospfe_dispatch_rde: " 616 "bad imsg size"); 617 618 nbr = nbr_find_peerid(imsg.hdr.peerid); 619 if (nbr == NULL) 620 break; 621 622 if (nbr->iface->self == nbr) 623 break; 624 625 if (imsg.hdr.type == IMSG_LS_SNAP && 626 nbr->state != NBR_STA_SNAP) 627 break; 628 629 memcpy(&age, imsg.data, sizeof(age)); 630 ref = lsa_cache_add(imsg.data, l); 631 if (ntohs(age) >= MAX_AGE) 632 /* add to retransmit list */ 633 ls_retrans_list_add(nbr, imsg.data, 0, 0); 634 else 635 ls_retrans_list_add(nbr, imsg.data, 0, 1); 636 637 lsa_cache_put(ref, nbr); 638 break; 639 case IMSG_LS_ACK: 640 /* 641 * IMSG_LS_ACK is used in two cases: 642 * 1. LSA was a duplicate 643 * 2. LS age is MaxAge and there is no current 644 * instance in the DB plus no neighbor in state 645 * Exchange or Loading 646 */ 647 nbr = nbr_find_peerid(imsg.hdr.peerid); 648 if (nbr == NULL) 649 break; 650 651 if (nbr->iface->self == nbr) 652 break; 653 654 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(lsa_hdr)) 655 fatalx("ospfe_dispatch_rde: bad imsg size"); 656 memcpy(&lsa_hdr, imsg.data, sizeof(lsa_hdr)); 657 658 /* for case one check for implied acks */ 659 if (nbr->iface->state & IF_STA_DROTHER) 660 if (ls_retrans_list_del(nbr->iface->self, 661 &lsa_hdr) == 0) 662 break; 663 if (ls_retrans_list_del(nbr, &lsa_hdr) == 0) 664 break; 665 666 /* send a direct acknowledgement */ 667 send_direct_ack(nbr->iface, nbr->addr, imsg.data, 668 imsg.hdr.len - IMSG_HEADER_SIZE); 669 670 break; 671 case IMSG_LS_BADREQ: 672 nbr = nbr_find_peerid(imsg.hdr.peerid); 673 if (nbr == NULL) 674 break; 675 676 if (nbr->iface->self == nbr) 677 fatalx("ospfe_dispatch_rde: " 678 "dummy neighbor got BADREQ"); 679 680 nbr_fsm(nbr, NBR_EVT_BAD_LS_REQ); 681 break; 682 case IMSG_ABR_UP: 683 memcpy(&ar, imsg.data, sizeof(ar)); 684 685 if ((iface = find_vlink(&ar)) != NULL && 686 iface->state == IF_STA_DOWN) 687 if (if_fsm(iface, IF_EVT_UP)) { 688 log_debug("error starting interface %s", 689 iface->name); 690 } 691 break; 692 case IMSG_ABR_DOWN: 693 memcpy(&ar, imsg.data, sizeof(ar)); 694 695 if ((iface = find_vlink(&ar)) != NULL && 696 iface->state == IF_STA_POINTTOPOINT) 697 if (if_fsm(iface, IF_EVT_DOWN)) { 698 log_debug("error stopping interface %s", 699 iface->name); 700 } 701 break; 702 case IMSG_CTL_AREA: 703 case IMSG_CTL_IFACE: 704 case IMSG_CTL_END: 705 case IMSG_CTL_SHOW_DATABASE: 706 case IMSG_CTL_SHOW_DB_EXT: 707 case IMSG_CTL_SHOW_DB_LINK: 708 case IMSG_CTL_SHOW_DB_NET: 709 case IMSG_CTL_SHOW_DB_RTR: 710 case IMSG_CTL_SHOW_DB_INTRA: 711 case IMSG_CTL_SHOW_DB_SELF: 712 case IMSG_CTL_SHOW_DB_SUM: 713 case IMSG_CTL_SHOW_DB_ASBR: 714 case IMSG_CTL_SHOW_RIB: 715 case IMSG_CTL_SHOW_SUM: 716 case IMSG_CTL_SHOW_SUM_AREA: 717 control_imsg_relay(&imsg); 718 break; 719 default: 720 log_debug("ospfe_dispatch_rde: error handling imsg %d", 721 imsg.hdr.type); 722 break; 723 } 724 imsg_free(&imsg); 725 } 726 if (!shut) 727 imsg_event_add(iev); 728 else { 729 /* this pipe is dead, so remove the event handler */ 730 event_del(&iev->ev); 731 event_loopexit(NULL); 732 } 733 } 734 735 struct iface * 736 find_vlink(struct abr_rtr *ar) 737 { 738 struct area *area; 739 struct iface *iface = NULL; 740 741 LIST_FOREACH(area, &oeconf->area_list, entry) 742 LIST_FOREACH(iface, &area->iface_list, entry) 743 if (iface->abr_id.s_addr == ar->abr_id.s_addr && 744 iface->type == IF_TYPE_VIRTUALLINK && 745 iface->area->id.s_addr == ar->area.s_addr) { 746 iface->dst = ar->dst_ip; 747 iface->addr = ar->addr; 748 iface->metric = ar->metric; 749 750 return (iface); 751 } 752 753 return (iface); 754 } 755 756 void 757 orig_rtr_lsa_all(struct area *area) 758 { 759 struct area *a; 760 761 /* 762 * update all router LSA in all areas except area itself, 763 * as this update is already running. 764 */ 765 LIST_FOREACH(a, &oeconf->area_list, entry) 766 if (a != area) 767 orig_rtr_lsa(a); 768 } 769 770 void 771 orig_rtr_lsa(struct area *area) 772 { 773 struct lsa_hdr lsa_hdr; 774 struct lsa_rtr lsa_rtr; 775 struct lsa_rtr_link rtr_link; 776 struct iface *iface; 777 struct ibuf *buf; 778 struct nbr *nbr, *self = NULL; 779 u_int32_t flags; 780 u_int16_t chksum; 781 u_int8_t border, virtual = 0; 782 783 log_debug("orig_rtr_lsa: area %s", inet_ntoa(area->id)); 784 785 /* XXX IBUF_READ_SIZE */ 786 if ((buf = ibuf_dynamic(sizeof(lsa_hdr), IBUF_READ_SIZE)) == NULL) 787 fatal("orig_rtr_lsa"); 788 789 /* reserve space for LSA header and LSA Router header */ 790 if (ibuf_reserve(buf, sizeof(lsa_hdr)) == NULL) 791 fatal("orig_rtr_lsa: ibuf_reserve failed"); 792 793 if (ibuf_reserve(buf, sizeof(lsa_rtr)) == NULL) 794 fatal("orig_rtr_lsa: ibuf_reserve failed"); 795 796 /* links */ 797 LIST_FOREACH(iface, &area->iface_list, entry) { 798 if (self == NULL && iface->self != NULL) 799 self = iface->self; 800 801 bzero(&rtr_link, sizeof(rtr_link)); 802 803 switch (iface->type) { 804 case IF_TYPE_POINTOPOINT: 805 LIST_FOREACH(nbr, &iface->nbr_list, entry) 806 if (nbr != iface->self && 807 nbr->state & NBR_STA_FULL) 808 break; 809 if (nbr && iface->state & IF_STA_POINTTOPOINT) { 810 log_debug("orig_rtr_lsa: point-to-point, " 811 "interface %s", iface->name); 812 rtr_link.type = LINK_TYPE_POINTTOPOINT; 813 if (iface->dependon[0] != '\0' && 814 iface->depend_ok == 0) 815 rtr_link.metric = MAX_METRIC; 816 else 817 rtr_link.metric = htons(iface->metric); 818 rtr_link.iface_id = htonl(iface->ifindex); 819 rtr_link.nbr_iface_id = htonl(nbr->iface_id); 820 rtr_link.nbr_rtr_id = nbr->id.s_addr; 821 if (ibuf_add(buf, &rtr_link, sizeof(rtr_link))) 822 fatalx("orig_rtr_lsa: ibuf_add failed"); 823 } 824 continue; 825 case IF_TYPE_BROADCAST: 826 case IF_TYPE_NBMA: 827 if ((iface->state & IF_STA_MULTI)) { 828 if (iface->dr == iface->self) { 829 LIST_FOREACH(nbr, &iface->nbr_list, 830 entry) 831 if (nbr != iface->self && 832 nbr->state & NBR_STA_FULL) 833 break; 834 } else 835 nbr = iface->dr; 836 837 if (nbr && nbr->state & NBR_STA_FULL) { 838 log_debug("orig_rtr_lsa: transit net, " 839 "interface %s", iface->name); 840 841 rtr_link.type = LINK_TYPE_TRANSIT_NET; 842 if (iface->dependon[0] != '\0' && 843 iface->depend_ok == 0) 844 rtr_link.metric = MAX_METRIC; 845 else 846 rtr_link.metric = 847 htons(iface->metric); 848 rtr_link.iface_id = htonl(iface->ifindex); 849 rtr_link.nbr_iface_id = htonl(iface->dr->iface_id); 850 rtr_link.nbr_rtr_id = iface->dr->id.s_addr; 851 if (ibuf_add(buf, &rtr_link, 852 sizeof(rtr_link))) 853 fatalx("orig_rtr_lsa: " 854 "ibuf_add failed"); 855 break; 856 } 857 } 858 break; 859 #if 0 /* TODO virtualllink/pointtomulti */ 860 case IF_TYPE_VIRTUALLINK: 861 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 862 if (nbr != iface->self && 863 nbr->state & NBR_STA_FULL) 864 break; 865 } 866 if (nbr) { 867 rtr_link.id = nbr->id.s_addr; 868 //XXX rtr_link.data = iface->addr.s_addr; 869 rtr_link.type = LINK_TYPE_VIRTUAL; 870 /* RFC 3137: stub router support */ 871 if (oeconf->flags & OSPFD_FLAG_STUB_ROUTER || 872 oe_nofib) 873 rtr_link.metric = 0xffff; 874 else 875 rtr_link.metric = htons(iface->metric); 876 virtual = 1; 877 if (ibuf_add(buf, &rtr_link, sizeof(rtr_link))) 878 fatalx("orig_rtr_lsa: ibuf_add failed"); 879 880 log_debug("orig_rtr_lsa: virtual link, " 881 "interface %s", iface->name); 882 } 883 continue; 884 case IF_TYPE_POINTOMULTIPOINT: 885 log_debug("orig_rtr_lsa: stub net, " 886 "interface %s", iface->name); 887 //XXX rtr_link.id = iface->addr.s_addr; 888 rtr_link.data = 0xffffffff; 889 rtr_link.type = LINK_TYPE_STUB_NET; 890 rtr_link.metric = htons(iface->metric); 891 if (ibuf_add(buf, &rtr_link, sizeof(rtr_link))) 892 fatalx("orig_rtr_lsa: ibuf_add failed"); 893 894 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 895 if (nbr != iface->self && 896 nbr->state & NBR_STA_FULL) { 897 bzero(&rtr_link, sizeof(rtr_link)); 898 log_debug("orig_rtr_lsa: " 899 "point-to-multipoint, interface %s", 900 iface->name); 901 //XXX rtr_link.id = nbr->addr.s_addr; 902 //XXX rtr_link.data = iface->addr.s_addr; 903 rtr_link.type = LINK_TYPE_POINTTOPOINT; 904 /* RFC 3137: stub router support */ 905 if (oe_nofib || oeconf->flags & 906 OSPFD_FLAG_STUB_ROUTER) 907 rtr_link.metric = MAX_METRIC; 908 else if (iface->dependon[0] != '\0' && 909 iface->dependon_ok == 0) 910 rtr_link.metric = MAX_METRIC; 911 else 912 rtr_link.metric = 913 htons(iface->metric); 914 if (ibuf_add(buf, &rtr_link, 915 sizeof(rtr_link))) 916 fatalx("orig_rtr_lsa: " 917 "ibuf_add failed"); 918 } 919 } 920 continue; 921 #endif /* TODO virtualllink/pointtomulti */ 922 default: 923 fatalx("orig_rtr_lsa: unknown interface type"); 924 } 925 } 926 927 /* LSA router header */ 928 lsa_rtr.opts = 0; 929 flags = 0; 930 931 /* 932 * Set the E bit as soon as an as-ext lsa may be redistributed, only 933 * setting it in case we redistribute something is not worth the fuss. 934 */ 935 if (oeconf->redistribute && !area->stub) 936 flags |= OSPF_RTR_E; 937 938 border = (area_border_router(oeconf) != 0); 939 if (border != oeconf->border) { 940 oeconf->border = border; 941 orig_rtr_lsa_all(area); 942 } 943 944 if (oeconf->border) 945 flags |= OSPF_RTR_B; 946 /* TODO set V flag if a active virtual link ends here and the 947 * area is the transit area for this link. */ 948 if (virtual) 949 flags |= OSPF_RTR_V; 950 951 LSA_24_SETLO(lsa_rtr.opts, area_ospf_options(area)); 952 LSA_24_SETHI(lsa_rtr.opts, flags); 953 lsa_rtr.opts = htonl(lsa_rtr.opts); 954 memcpy(ibuf_seek(buf, sizeof(lsa_hdr), sizeof(lsa_rtr)), 955 &lsa_rtr, sizeof(lsa_rtr)); 956 957 /* LSA header */ 958 lsa_hdr.age = htons(DEFAULT_AGE); 959 lsa_hdr.type = htons(LSA_TYPE_ROUTER); 960 /* XXX needs to be fixed if multiple router-lsa need to be announced */ 961 lsa_hdr.ls_id = 0; 962 lsa_hdr.adv_rtr = oeconf->rtr_id.s_addr; 963 lsa_hdr.seq_num = htonl(INIT_SEQ_NUM); 964 lsa_hdr.len = htons(buf->wpos); 965 lsa_hdr.ls_chksum = 0; /* updated later */ 966 memcpy(ibuf_seek(buf, 0, sizeof(lsa_hdr)), &lsa_hdr, sizeof(lsa_hdr)); 967 968 chksum = htons(iso_cksum(buf->buf, buf->wpos, LS_CKSUM_OFFSET)); 969 memcpy(ibuf_seek(buf, LS_CKSUM_OFFSET, sizeof(chksum)), 970 &chksum, sizeof(chksum)); 971 972 if (self) 973 imsg_compose_event(iev_rde, IMSG_LS_UPD, self->peerid, 0, 974 -1, buf->buf, buf->wpos); 975 else 976 log_warnx("orig_rtr_lsa: empty area %s", 977 inet_ntoa(area->id)); 978 979 ibuf_free(buf); 980 } 981 982 void 983 orig_net_lsa(struct iface *iface) 984 { 985 struct lsa_hdr lsa_hdr; 986 struct nbr *nbr; 987 struct ibuf *buf; 988 struct lsa_net lsa_net; 989 int num_rtr = 0; 990 u_int16_t chksum; 991 992 /* XXX IBUF_READ_SIZE */ 993 if ((buf = ibuf_dynamic(sizeof(lsa_hdr), IBUF_READ_SIZE)) == NULL) 994 fatal("orig_net_lsa"); 995 996 /* reserve space for LSA header and options field */ 997 if (ibuf_reserve(buf, sizeof(lsa_hdr) + sizeof(lsa_net)) == NULL) 998 fatal("orig_net_lsa: ibuf_reserve failed"); 999 1000 lsa_net.opts = 0; 1001 /* fully adjacent neighbors + self */ 1002 LIST_FOREACH(nbr, &iface->nbr_list, entry) 1003 if (nbr->state & NBR_STA_FULL) { 1004 if (ibuf_add(buf, &nbr->id, sizeof(nbr->id))) 1005 fatal("orig_net_lsa: ibuf_add failed"); 1006 lsa_net.opts |= nbr->link_options; 1007 num_rtr++; 1008 } 1009 1010 if (num_rtr == 1) { 1011 /* non transit net therefore no need to generate a net lsa */ 1012 ibuf_free(buf); 1013 return; 1014 } 1015 1016 /* LSA header */ 1017 if (iface->state & IF_STA_DR) 1018 lsa_hdr.age = htons(DEFAULT_AGE); 1019 else 1020 lsa_hdr.age = htons(MAX_AGE); 1021 1022 lsa_hdr.type = htons(LSA_TYPE_NETWORK); 1023 /* for network LSAs, the link state ID equals the interface ID */ 1024 lsa_hdr.ls_id = htonl(iface->ifindex); 1025 lsa_hdr.adv_rtr = oeconf->rtr_id.s_addr; 1026 lsa_hdr.seq_num = htonl(INIT_SEQ_NUM); 1027 lsa_hdr.len = htons(buf->wpos); 1028 lsa_hdr.ls_chksum = 0; /* updated later */ 1029 memcpy(ibuf_seek(buf, 0, sizeof(lsa_hdr)), &lsa_hdr, sizeof(lsa_hdr)); 1030 1031 lsa_net.opts &= lsa_net.opts & htonl(LSA_24_MASK); 1032 memcpy(ibuf_seek(buf, sizeof(lsa_hdr), sizeof(lsa_net)), &lsa_net, 1033 sizeof(lsa_net)); 1034 1035 chksum = htons(iso_cksum(buf->buf, buf->wpos, LS_CKSUM_OFFSET)); 1036 memcpy(ibuf_seek(buf, LS_CKSUM_OFFSET, sizeof(chksum)), 1037 &chksum, sizeof(chksum)); 1038 1039 imsg_compose_event(iev_rde, IMSG_LS_UPD, iface->self->peerid, 0, 1040 -1, buf->buf, buf->wpos); 1041 1042 ibuf_free(buf); 1043 } 1044 1045 void 1046 orig_link_lsa(struct iface *iface) 1047 { 1048 struct lsa_hdr lsa_hdr; 1049 struct lsa_link lsa_link; 1050 struct lsa_prefix lsa_prefix; 1051 struct ibuf *buf; 1052 struct iface_addr *ia; 1053 struct in6_addr prefix; 1054 unsigned int num_prefix = 0; 1055 u_int16_t chksum; 1056 u_int32_t options; 1057 1058 log_debug("orig_link_lsa: interface %s", iface->name); 1059 1060 switch (iface->type) { 1061 case IF_TYPE_VIRTUALLINK: /* forbidden by rfc5340 */ 1062 return; 1063 case IF_TYPE_BROADCAST: 1064 case IF_TYPE_NBMA: 1065 if ((iface->state & IF_STA_MULTI) == 0) 1066 return; 1067 break; 1068 case IF_TYPE_POINTOPOINT: 1069 case IF_TYPE_POINTOMULTIPOINT: 1070 if ((iface->state & IF_STA_POINTTOPOINT) == 0) 1071 return; 1072 break; 1073 default: 1074 fatalx("orig_link_lsa: unknown interface type"); 1075 } 1076 1077 /* XXX IBUF_READ_SIZE */ 1078 if ((buf = ibuf_dynamic(sizeof(lsa_hdr) + sizeof(lsa_link), 1079 IBUF_READ_SIZE)) == NULL) 1080 fatal("orig_link_lsa"); 1081 1082 /* reserve space for LSA header and LSA link header */ 1083 if (ibuf_reserve(buf, sizeof(lsa_hdr) + sizeof(lsa_link)) == NULL) 1084 fatal("orig_link_lsa: ibuf_reserve failed"); 1085 1086 /* link-local address, and all prefixes configured on interface */ 1087 TAILQ_FOREACH(ia, &iface->ifa_list, entry) { 1088 if (IN6_IS_ADDR_LINKLOCAL(&ia->addr)) { 1089 log_debug("orig_link_lsa: link local address %s", 1090 log_in6addr(&ia->addr)); 1091 lsa_link.lladdr = ia->addr; 1092 continue; 1093 } 1094 1095 lsa_prefix.prefixlen = ia->prefixlen; 1096 lsa_prefix.options = 0; 1097 lsa_prefix.metric = 0; 1098 inet6applymask(&prefix, &ia->addr, ia->prefixlen); 1099 log_debug("orig_link_lsa: prefix %s", log_in6addr(&prefix)); 1100 if (ibuf_add(buf, &lsa_prefix, sizeof(lsa_prefix))) 1101 fatal("orig_link_lsa: ibuf_add failed"); 1102 if (ibuf_add(buf, &prefix.s6_addr[0], 1103 LSA_PREFIXSIZE(ia->prefixlen))) 1104 fatal("orig_link_lsa: ibuf_add failed"); 1105 num_prefix++; 1106 } 1107 1108 /* LSA link header (lladdr has already been filled in above) */ 1109 LSA_24_SETHI(lsa_link.opts, iface->priority); 1110 options = area_ospf_options(iface->area); 1111 LSA_24_SETLO(lsa_link.opts, options); 1112 lsa_link.opts = htonl(lsa_link.opts); 1113 lsa_link.numprefix = htonl(num_prefix); 1114 memcpy(ibuf_seek(buf, sizeof(lsa_hdr), sizeof(lsa_link)), 1115 &lsa_link, sizeof(lsa_link)); 1116 1117 /* LSA header */ 1118 lsa_hdr.age = htons(DEFAULT_AGE); 1119 lsa_hdr.type = htons(LSA_TYPE_LINK); 1120 /* for link LSAs, the link state ID equals the interface ID */ 1121 lsa_hdr.ls_id = htonl(iface->ifindex); 1122 lsa_hdr.adv_rtr = oeconf->rtr_id.s_addr; 1123 lsa_hdr.seq_num = htonl(INIT_SEQ_NUM); 1124 lsa_hdr.len = htons(buf->wpos); 1125 lsa_hdr.ls_chksum = 0; /* updated later */ 1126 memcpy(ibuf_seek(buf, 0, sizeof(lsa_hdr)), &lsa_hdr, sizeof(lsa_hdr)); 1127 1128 chksum = htons(iso_cksum(buf->buf, buf->wpos, LS_CKSUM_OFFSET)); 1129 memcpy(ibuf_seek(buf, LS_CKSUM_OFFSET, sizeof(chksum)), 1130 &chksum, sizeof(chksum)); 1131 1132 imsg_compose_event(iev_rde, IMSG_LS_UPD, iface->self->peerid, 0, 1133 -1, buf->buf, buf->wpos); 1134 1135 ibuf_free(buf); 1136 } 1137 1138 u_int32_t 1139 ospfe_router_id(void) 1140 { 1141 return (oeconf->rtr_id.s_addr); 1142 } 1143 1144 void 1145 ospfe_fib_update(int type) 1146 { 1147 int old = oe_nofib; 1148 1149 if (type == IMSG_CTL_FIB_COUPLE) 1150 oe_nofib = 0; 1151 if (type == IMSG_CTL_FIB_DECOUPLE) 1152 oe_nofib = 1; 1153 if (old != oe_nofib) 1154 orig_rtr_lsa_all(NULL); 1155 } 1156 1157 void 1158 ospfe_iface_ctl(struct ctl_conn *c, unsigned int idx) 1159 { 1160 struct area *area; 1161 struct iface *iface; 1162 struct ctl_iface *ictl; 1163 1164 LIST_FOREACH(area, &oeconf->area_list, entry) 1165 LIST_FOREACH(iface, &area->iface_list, entry) 1166 if (idx == 0 || idx == iface->ifindex) { 1167 ictl = if_to_ctl(iface); 1168 imsg_compose_event(&c->iev, 1169 IMSG_CTL_SHOW_INTERFACE, 0, 0, -1, 1170 ictl, sizeof(struct ctl_iface)); 1171 } 1172 } 1173 1174 void 1175 ospfe_nbr_ctl(struct ctl_conn *c) 1176 { 1177 struct area *area; 1178 struct iface *iface; 1179 struct nbr *nbr; 1180 struct ctl_nbr *nctl; 1181 1182 LIST_FOREACH(area, &oeconf->area_list, entry) 1183 LIST_FOREACH(iface, &area->iface_list, entry) 1184 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 1185 if (iface->self != nbr) { 1186 nctl = nbr_to_ctl(nbr); 1187 imsg_compose_event(&c->iev, 1188 IMSG_CTL_SHOW_NBR, 0, 0, -1, nctl, 1189 sizeof(struct ctl_nbr)); 1190 } 1191 } 1192 1193 imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 0, -1, NULL, 0); 1194 } 1195 1196 void 1197 ospfe_demote_area(struct area *area, int active) 1198 { 1199 struct demote_msg dmsg; 1200 1201 if (ospfd_process != PROC_OSPF_ENGINE || 1202 area->demote_group[0] == '\0') 1203 return; 1204 1205 bzero(&dmsg, sizeof(dmsg)); 1206 strlcpy(dmsg.demote_group, area->demote_group, 1207 sizeof(dmsg.demote_group)); 1208 dmsg.level = area->demote_level; 1209 if (active) 1210 dmsg.level = -dmsg.level; 1211 1212 ospfe_imsg_compose_parent(IMSG_DEMOTE, 0, &dmsg, sizeof(dmsg)); 1213 } 1214 1215 void 1216 ospfe_demote_iface(struct iface *iface, int active) 1217 { 1218 struct demote_msg dmsg; 1219 1220 if (ospfd_process != PROC_OSPF_ENGINE || 1221 iface->demote_group[0] == '\0') 1222 return; 1223 1224 bzero(&dmsg, sizeof(dmsg)); 1225 strlcpy(dmsg.demote_group, iface->demote_group, 1226 sizeof(dmsg.demote_group)); 1227 if (active) 1228 dmsg.level = -1; 1229 else 1230 dmsg.level = 1; 1231 1232 log_warnx("ospfe_demote_iface: group %s level %d", dmsg.demote_group, 1233 dmsg.level); 1234 1235 ospfe_imsg_compose_parent(IMSG_DEMOTE, 0, &dmsg, sizeof(dmsg)); 1236 } 1237