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