1 /* $OpenBSD: rde.c,v 1.62 2014/07/12 20:16:38 krw Exp $ */ 2 3 /* 4 * Copyright (c) 2004, 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/param.h> /* for MIN() */ 22 #include <sys/types.h> 23 #include <sys/socket.h> 24 #include <sys/queue.h> 25 #include <net/if_types.h> 26 #include <netinet/in.h> 27 #include <arpa/inet.h> 28 #include <err.h> 29 #include <errno.h> 30 #include <stdlib.h> 31 #include <signal.h> 32 #include <string.h> 33 #include <pwd.h> 34 #include <unistd.h> 35 #include <event.h> 36 37 #include "ospf6.h" 38 #include "ospf6d.h" 39 #include "ospfe.h" 40 #include "log.h" 41 #include "rde.h" 42 43 void rde_sig_handler(int sig, short, void *); 44 void rde_shutdown(void); 45 void rde_dispatch_imsg(int, short, void *); 46 void rde_dispatch_parent(int, short, void *); 47 void rde_dump_area(struct area *, int, pid_t); 48 49 void rde_send_summary(pid_t); 50 void rde_send_summary_area(struct area *, pid_t); 51 void rde_nbr_init(u_int32_t); 52 void rde_nbr_free(void); 53 struct rde_nbr *rde_nbr_new(u_int32_t, struct rde_nbr *); 54 void rde_nbr_del(struct rde_nbr *); 55 56 void rde_req_list_add(struct rde_nbr *, struct lsa_hdr *); 57 int rde_req_list_exists(struct rde_nbr *, struct lsa_hdr *); 58 void rde_req_list_del(struct rde_nbr *, struct lsa_hdr *); 59 void rde_req_list_free(struct rde_nbr *); 60 61 struct lsa *rde_asext_get(struct rroute *); 62 struct lsa *rde_asext_put(struct rroute *); 63 64 int comp_asext(struct lsa *, struct lsa *); 65 struct lsa *orig_asext_lsa(struct rroute *, u_int16_t); 66 struct lsa *orig_sum_lsa(struct rt_node *, struct area *, u_int8_t, int); 67 struct lsa *orig_intra_lsa_net(struct area *, struct iface *, 68 struct vertex *); 69 struct lsa *orig_intra_lsa_rtr(struct area *, struct vertex *); 70 void append_prefix_lsa(struct lsa **, u_int16_t *, 71 struct lsa_prefix *); 72 73 /* A 32-bit value != any ifindex. 74 * We assume ifindex is bound by [1, USHRT_MAX] inclusive. */ 75 #define LS_ID_INTRA_RTR 0x01000000 76 77 /* Tree of prefixes with global scope on given a link, 78 * see orig_intra_lsa_*() */ 79 struct prefix_node { 80 RB_ENTRY(prefix_node) entry; 81 struct lsa_prefix *prefix; 82 }; 83 RB_HEAD(prefix_tree, prefix_node); 84 RB_PROTOTYPE(prefix_tree, prefix_node, entry, prefix_compare); 85 int prefix_compare(struct prefix_node *, struct prefix_node *); 86 void prefix_tree_add(struct prefix_tree *, struct lsa_link *); 87 88 struct ospfd_conf *rdeconf = NULL, *nconf = NULL; 89 struct imsgev *iev_ospfe; 90 struct imsgev *iev_main; 91 struct rde_nbr *nbrself; 92 struct lsa_tree asext_tree; 93 94 /* ARGSUSED */ 95 void 96 rde_sig_handler(int sig, short event, void *arg) 97 { 98 /* 99 * signal handler rules don't apply, libevent decouples for us 100 */ 101 102 switch (sig) { 103 case SIGINT: 104 case SIGTERM: 105 rde_shutdown(); 106 /* NOTREACHED */ 107 default: 108 fatalx("unexpected signal"); 109 } 110 } 111 112 /* route decision engine */ 113 pid_t 114 rde(struct ospfd_conf *xconf, int pipe_parent2rde[2], int pipe_ospfe2rde[2], 115 int pipe_parent2ospfe[2]) 116 { 117 struct event ev_sigint, ev_sigterm; 118 struct timeval now; 119 struct passwd *pw; 120 struct redistribute *r; 121 pid_t pid; 122 123 switch (pid = fork()) { 124 case -1: 125 fatal("cannot fork"); 126 /* NOTREACHED */ 127 case 0: 128 break; 129 default: 130 return (pid); 131 } 132 133 rdeconf = xconf; 134 135 if ((pw = getpwnam(OSPF6D_USER)) == NULL) 136 fatal("getpwnam"); 137 138 if (chroot(pw->pw_dir) == -1) 139 fatal("chroot"); 140 if (chdir("/") == -1) 141 fatal("chdir(\"/\")"); 142 143 setproctitle("route decision engine"); 144 ospfd_process = PROC_RDE_ENGINE; 145 146 if (setgroups(1, &pw->pw_gid) || 147 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 148 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 149 fatal("can't drop privileges"); 150 151 event_init(); 152 rde_nbr_init(NBR_HASHSIZE); 153 lsa_init(&asext_tree); 154 155 /* setup signal handler */ 156 signal_set(&ev_sigint, SIGINT, rde_sig_handler, NULL); 157 signal_set(&ev_sigterm, SIGTERM, rde_sig_handler, NULL); 158 signal_add(&ev_sigint, NULL); 159 signal_add(&ev_sigterm, NULL); 160 signal(SIGPIPE, SIG_IGN); 161 signal(SIGHUP, SIG_IGN); 162 163 /* setup pipes */ 164 close(pipe_ospfe2rde[0]); 165 close(pipe_parent2rde[0]); 166 close(pipe_parent2ospfe[0]); 167 close(pipe_parent2ospfe[1]); 168 169 if ((iev_ospfe = malloc(sizeof(struct imsgev))) == NULL || 170 (iev_main = malloc(sizeof(struct imsgev))) == NULL) 171 fatal(NULL); 172 imsg_init(&iev_ospfe->ibuf, pipe_ospfe2rde[1]); 173 iev_ospfe->handler = rde_dispatch_imsg; 174 imsg_init(&iev_main->ibuf, pipe_parent2rde[1]); 175 iev_main->handler = rde_dispatch_parent; 176 177 /* setup event handler */ 178 iev_ospfe->events = EV_READ; 179 event_set(&iev_ospfe->ev, iev_ospfe->ibuf.fd, iev_ospfe->events, 180 iev_ospfe->handler, iev_ospfe); 181 event_add(&iev_ospfe->ev, NULL); 182 183 iev_main->events = EV_READ; 184 event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events, 185 iev_main->handler, iev_main); 186 event_add(&iev_main->ev, NULL); 187 188 evtimer_set(&rdeconf->ev, spf_timer, rdeconf); 189 cand_list_init(); 190 rt_init(); 191 192 while ((r = SIMPLEQ_FIRST(&rdeconf->redist_list)) != NULL) { 193 SIMPLEQ_REMOVE_HEAD(&rdeconf->redist_list, entry); 194 free(r); 195 } 196 197 gettimeofday(&now, NULL); 198 rdeconf->uptime = now.tv_sec; 199 200 event_dispatch(); 201 202 rde_shutdown(); 203 /* NOTREACHED */ 204 205 return (0); 206 } 207 208 void 209 rde_shutdown(void) 210 { 211 struct area *a; 212 213 stop_spf_timer(rdeconf); 214 cand_list_clr(); 215 rt_clear(); 216 217 while ((a = LIST_FIRST(&rdeconf->area_list)) != NULL) { 218 LIST_REMOVE(a, entry); 219 area_del(a); 220 } 221 rde_nbr_free(); 222 223 msgbuf_clear(&iev_ospfe->ibuf.w); 224 free(iev_ospfe); 225 msgbuf_clear(&iev_main->ibuf.w); 226 free(iev_main); 227 free(rdeconf); 228 229 log_info("route decision engine exiting"); 230 _exit(0); 231 } 232 233 int 234 rde_imsg_compose_ospfe(int type, u_int32_t peerid, pid_t pid, void *data, 235 u_int16_t datalen) 236 { 237 return (imsg_compose_event(iev_ospfe, type, peerid, pid, -1, 238 data, datalen)); 239 } 240 241 /* ARGSUSED */ 242 void 243 rde_dispatch_imsg(int fd, short event, void *bula) 244 { 245 struct imsgev *iev = bula; 246 struct imsgbuf *ibuf = &iev->ibuf; 247 struct imsg imsg; 248 struct in_addr aid; 249 struct ls_req_hdr req_hdr; 250 struct lsa_hdr lsa_hdr, *db_hdr; 251 struct rde_nbr rn, *nbr; 252 struct timespec tp; 253 struct lsa *lsa; 254 struct area *area; 255 struct vertex *v; 256 char *buf; 257 ssize_t n; 258 time_t now; 259 int r, state, self, shut = 0, verbose; 260 u_int16_t l; 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 ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 270 fatal("msgbuf_write"); 271 if (n == 0) /* connection closed */ 272 shut = 1; 273 } 274 275 clock_gettime(CLOCK_MONOTONIC, &tp); 276 now = tp.tv_sec; 277 278 for (;;) { 279 if ((n = imsg_get(ibuf, &imsg)) == -1) 280 fatal("rde_dispatch_imsg: imsg_read error"); 281 if (n == 0) 282 break; 283 284 switch (imsg.hdr.type) { 285 case IMSG_NEIGHBOR_UP: 286 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(rn)) 287 fatalx("invalid size of OE request"); 288 memcpy(&rn, imsg.data, sizeof(rn)); 289 290 if (rde_nbr_new(imsg.hdr.peerid, &rn) == NULL) 291 fatalx("rde_dispatch_imsg: " 292 "neighbor already exists"); 293 break; 294 case IMSG_NEIGHBOR_DOWN: 295 rde_nbr_del(rde_nbr_find(imsg.hdr.peerid)); 296 break; 297 case IMSG_NEIGHBOR_CHANGE: 298 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(state)) 299 fatalx("invalid size of OE request"); 300 memcpy(&state, imsg.data, sizeof(state)); 301 302 nbr = rde_nbr_find(imsg.hdr.peerid); 303 if (nbr == NULL) 304 break; 305 306 if (state != nbr->state && 307 (nbr->state & NBR_STA_FULL || 308 state & NBR_STA_FULL)) { 309 nbr->state = state; 310 area_track(nbr->area, state); 311 orig_intra_area_prefix_lsas(nbr->area); 312 } 313 314 nbr->state = state; 315 if (nbr->state & NBR_STA_FULL) 316 rde_req_list_free(nbr); 317 break; 318 case IMSG_DB_SNAPSHOT: 319 nbr = rde_nbr_find(imsg.hdr.peerid); 320 if (nbr == NULL) 321 break; 322 323 lsa_snap(nbr, imsg.hdr.peerid); 324 325 imsg_compose_event(iev_ospfe, IMSG_DB_END, imsg.hdr.peerid, 326 0, -1, NULL, 0); 327 break; 328 case IMSG_DD: 329 nbr = rde_nbr_find(imsg.hdr.peerid); 330 if (nbr == NULL) 331 break; 332 333 buf = imsg.data; 334 for (l = imsg.hdr.len - IMSG_HEADER_SIZE; 335 l >= sizeof(lsa_hdr); l -= sizeof(lsa_hdr)) { 336 memcpy(&lsa_hdr, buf, sizeof(lsa_hdr)); 337 buf += sizeof(lsa_hdr); 338 339 v = lsa_find(nbr->iface, lsa_hdr.type, 340 lsa_hdr.ls_id, lsa_hdr.adv_rtr); 341 if (v == NULL) 342 db_hdr = NULL; 343 else 344 db_hdr = &v->lsa->hdr; 345 346 if (lsa_newer(&lsa_hdr, db_hdr) > 0) { 347 /* 348 * only request LSAs that are 349 * newer or missing 350 */ 351 rde_req_list_add(nbr, &lsa_hdr); 352 imsg_compose_event(iev_ospfe, IMSG_DD, 353 imsg.hdr.peerid, 0, -1, &lsa_hdr, 354 sizeof(lsa_hdr)); 355 } 356 } 357 if (l != 0) 358 log_warnx("rde_dispatch_imsg: peerid %lu, " 359 "trailing garbage in Database Description " 360 "packet", imsg.hdr.peerid); 361 362 imsg_compose_event(iev_ospfe, IMSG_DD_END, 363 imsg.hdr.peerid, 0, -1, NULL, 0); 364 break; 365 case IMSG_LS_REQ: 366 nbr = rde_nbr_find(imsg.hdr.peerid); 367 if (nbr == NULL) 368 break; 369 370 buf = imsg.data; 371 for (l = imsg.hdr.len - IMSG_HEADER_SIZE; 372 l >= sizeof(req_hdr); l -= sizeof(req_hdr)) { 373 memcpy(&req_hdr, buf, sizeof(req_hdr)); 374 buf += sizeof(req_hdr); 375 376 if ((v = lsa_find(nbr->iface, 377 req_hdr.type, req_hdr.ls_id, 378 req_hdr.adv_rtr)) == NULL) { 379 imsg_compose_event(iev_ospfe, 380 IMSG_LS_BADREQ, imsg.hdr.peerid, 381 0, -1, NULL, 0); 382 continue; 383 } 384 imsg_compose_event(iev_ospfe, IMSG_LS_UPD, 385 imsg.hdr.peerid, 0, -1, v->lsa, 386 ntohs(v->lsa->hdr.len)); 387 } 388 if (l != 0) 389 log_warnx("rde_dispatch_imsg: peerid %lu, " 390 "trailing garbage in LS Request " 391 "packet", imsg.hdr.peerid); 392 break; 393 case IMSG_LS_UPD: 394 nbr = rde_nbr_find(imsg.hdr.peerid); 395 if (nbr == NULL) 396 break; 397 398 lsa = malloc(imsg.hdr.len - IMSG_HEADER_SIZE); 399 if (lsa == NULL) 400 fatal(NULL); 401 memcpy(lsa, imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 402 403 if (!lsa_check(nbr, lsa, 404 imsg.hdr.len - IMSG_HEADER_SIZE)) { 405 free(lsa); 406 break; 407 } 408 409 v = lsa_find(nbr->iface, lsa->hdr.type, lsa->hdr.ls_id, 410 lsa->hdr.adv_rtr); 411 if (v == NULL) 412 db_hdr = NULL; 413 else 414 db_hdr = &v->lsa->hdr; 415 416 if (nbr->self) { 417 lsa_merge(nbr, lsa, v); 418 /* lsa_merge frees the right lsa */ 419 break; 420 } 421 422 r = lsa_newer(&lsa->hdr, db_hdr); 423 if (r > 0) { 424 /* new LSA newer than DB */ 425 if (v && v->flooded && 426 v->changed + MIN_LS_ARRIVAL >= now) { 427 free(lsa); 428 break; 429 } 430 431 rde_req_list_del(nbr, &lsa->hdr); 432 433 self = lsa_self(lsa); 434 if (self) { 435 if (v == NULL) 436 /* LSA is no longer announced, 437 * remove by premature aging. */ 438 lsa_flush(nbr, lsa); 439 else 440 lsa_reflood(v, lsa); 441 } else if (lsa_add(nbr, lsa)) 442 /* delayed lsa, don't flood yet */ 443 break; 444 445 /* flood and perhaps ack LSA */ 446 imsg_compose_event(iev_ospfe, IMSG_LS_FLOOD, 447 imsg.hdr.peerid, 0, -1, lsa, 448 ntohs(lsa->hdr.len)); 449 450 /* reflood self originated LSA */ 451 if (self && v) 452 imsg_compose_event(iev_ospfe, 453 IMSG_LS_FLOOD, v->peerid, 0, -1, 454 v->lsa, ntohs(v->lsa->hdr.len)); 455 /* new LSA was not added so free it */ 456 if (self) 457 free(lsa); 458 } else if (r < 0) { 459 /* 460 * point 6 of "The Flooding Procedure" 461 * We are violating the RFC here because 462 * it does not make sense to reset a session 463 * because an equal LSA is already in the table. 464 * Only if the LSA sent is older than the one 465 * in the table we should reset the session. 466 */ 467 if (rde_req_list_exists(nbr, &lsa->hdr)) { 468 imsg_compose_event(iev_ospfe, 469 IMSG_LS_BADREQ, imsg.hdr.peerid, 470 0, -1, NULL, 0); 471 free(lsa); 472 break; 473 } 474 475 /* lsa no longer needed */ 476 free(lsa); 477 478 /* new LSA older than DB */ 479 if (ntohl(db_hdr->seq_num) == MAX_SEQ_NUM && 480 ntohs(db_hdr->age) == MAX_AGE) 481 /* seq-num wrap */ 482 break; 483 484 if (v->changed + MIN_LS_ARRIVAL >= now) 485 break; 486 487 /* directly send current LSA, no ack */ 488 imsg_compose_event(iev_ospfe, IMSG_LS_UPD, 489 imsg.hdr.peerid, 0, -1, v->lsa, 490 ntohs(v->lsa->hdr.len)); 491 } else { 492 /* LSA equal send direct ack */ 493 imsg_compose_event(iev_ospfe, IMSG_LS_ACK, 494 imsg.hdr.peerid, 0, -1, &lsa->hdr, 495 sizeof(lsa->hdr)); 496 free(lsa); 497 } 498 break; 499 case IMSG_LS_MAXAGE: 500 nbr = rde_nbr_find(imsg.hdr.peerid); 501 if (nbr == NULL) 502 break; 503 504 if (imsg.hdr.len != IMSG_HEADER_SIZE + 505 sizeof(struct lsa_hdr)) 506 fatalx("invalid size of OE request"); 507 memcpy(&lsa_hdr, imsg.data, sizeof(lsa_hdr)); 508 509 if (rde_nbr_loading(nbr->area)) 510 break; 511 512 v = lsa_find(nbr->iface, lsa_hdr.type, lsa_hdr.ls_id, 513 lsa_hdr.adv_rtr); 514 if (v == NULL) 515 db_hdr = NULL; 516 else 517 db_hdr = &v->lsa->hdr; 518 519 /* 520 * only delete LSA if the one in the db is not newer 521 */ 522 if (lsa_newer(db_hdr, &lsa_hdr) <= 0) 523 lsa_del(nbr, &lsa_hdr); 524 break; 525 case IMSG_CTL_SHOW_DATABASE: 526 case IMSG_CTL_SHOW_DB_EXT: 527 case IMSG_CTL_SHOW_DB_LINK: 528 case IMSG_CTL_SHOW_DB_NET: 529 case IMSG_CTL_SHOW_DB_RTR: 530 case IMSG_CTL_SHOW_DB_INTRA: 531 case IMSG_CTL_SHOW_DB_SELF: 532 case IMSG_CTL_SHOW_DB_SUM: 533 case IMSG_CTL_SHOW_DB_ASBR: 534 if (imsg.hdr.len != IMSG_HEADER_SIZE && 535 imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(aid)) { 536 log_warnx("rde_dispatch_imsg: wrong imsg len"); 537 break; 538 } 539 if (imsg.hdr.len == IMSG_HEADER_SIZE) { 540 LIST_FOREACH(area, &rdeconf->area_list, entry) { 541 rde_dump_area(area, imsg.hdr.type, 542 imsg.hdr.pid); 543 } 544 lsa_dump(&asext_tree, imsg.hdr.type, 545 imsg.hdr.pid); 546 } else { 547 memcpy(&aid, imsg.data, sizeof(aid)); 548 if ((area = area_find(rdeconf, aid)) != NULL) { 549 rde_dump_area(area, imsg.hdr.type, 550 imsg.hdr.pid); 551 if (!area->stub) 552 lsa_dump(&asext_tree, 553 imsg.hdr.type, 554 imsg.hdr.pid); 555 } 556 } 557 imsg_compose_event(iev_ospfe, IMSG_CTL_END, 0, 558 imsg.hdr.pid, -1, NULL, 0); 559 break; 560 case IMSG_CTL_SHOW_RIB: 561 LIST_FOREACH(area, &rdeconf->area_list, entry) { 562 imsg_compose_event(iev_ospfe, IMSG_CTL_AREA, 563 0, imsg.hdr.pid, -1, area, sizeof(*area)); 564 565 rt_dump(area->id, imsg.hdr.pid, RIB_RTR); 566 rt_dump(area->id, imsg.hdr.pid, RIB_NET); 567 } 568 aid.s_addr = 0; 569 rt_dump(aid, imsg.hdr.pid, RIB_EXT); 570 571 imsg_compose_event(iev_ospfe, IMSG_CTL_END, 0, 572 imsg.hdr.pid, -1, NULL, 0); 573 break; 574 case IMSG_CTL_SHOW_SUM: 575 rde_send_summary(imsg.hdr.pid); 576 LIST_FOREACH(area, &rdeconf->area_list, entry) 577 rde_send_summary_area(area, imsg.hdr.pid); 578 imsg_compose_event(iev_ospfe, IMSG_CTL_END, 0, 579 imsg.hdr.pid, -1, NULL, 0); 580 break; 581 case IMSG_IFINFO: 582 if (imsg.hdr.len != IMSG_HEADER_SIZE + 583 sizeof(int)) 584 fatalx("IFINFO imsg with wrong len"); 585 586 nbr = rde_nbr_find(imsg.hdr.peerid); 587 if (nbr == NULL) 588 fatalx("IFINFO imsg with bad peerid"); 589 memcpy(&nbr->iface->state, imsg.data, sizeof(int)); 590 591 /* Resend LSAs if interface state changes. */ 592 orig_intra_area_prefix_lsas(nbr->area); 593 break; 594 case IMSG_CTL_LOG_VERBOSE: 595 /* already checked by ospfe */ 596 memcpy(&verbose, imsg.data, sizeof(verbose)); 597 log_verbose(verbose); 598 break; 599 default: 600 log_debug("rde_dispatch_imsg: unexpected imsg %d", 601 imsg.hdr.type); 602 break; 603 } 604 imsg_free(&imsg); 605 } 606 if (!shut) 607 imsg_event_add(iev); 608 else { 609 /* this pipe is dead, so remove the event handler */ 610 event_del(&iev->ev); 611 event_loopexit(NULL); 612 } 613 } 614 615 /* ARGSUSED */ 616 void 617 rde_dispatch_parent(int fd, short event, void *bula) 618 { 619 static struct area *narea; 620 struct area *area; 621 struct iface *iface, *ifp; 622 struct ifaddrchange *ifc; 623 struct iface_addr *ia, *nia; 624 struct imsg imsg; 625 struct kroute kr; 626 struct rroute rr; 627 struct imsgev *iev = bula; 628 struct imsgbuf *ibuf = &iev->ibuf; 629 struct lsa *lsa; 630 struct vertex *v; 631 struct rt_node *rn; 632 ssize_t n; 633 int shut = 0, wasvalid; 634 unsigned int ifindex; 635 636 if (event & EV_READ) { 637 if ((n = imsg_read(ibuf)) == -1) 638 fatal("imsg_read error"); 639 if (n == 0) /* connection closed */ 640 shut = 1; 641 } 642 if (event & EV_WRITE) { 643 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 644 fatal("msgbuf_write"); 645 if (n == 0) /* connection closed */ 646 shut = 1; 647 } 648 649 for (;;) { 650 if ((n = imsg_get(ibuf, &imsg)) == -1) 651 fatal("rde_dispatch_parent: imsg_read error"); 652 if (n == 0) 653 break; 654 655 switch (imsg.hdr.type) { 656 case IMSG_NETWORK_ADD: 657 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(rr)) { 658 log_warnx("rde_dispatch_parent: " 659 "wrong imsg len"); 660 break; 661 } 662 memcpy(&rr, imsg.data, sizeof(rr)); 663 664 if ((lsa = rde_asext_get(&rr)) != NULL) { 665 v = lsa_find(NULL, lsa->hdr.type, 666 lsa->hdr.ls_id, lsa->hdr.adv_rtr); 667 668 lsa_merge(nbrself, lsa, v); 669 } 670 break; 671 case IMSG_NETWORK_DEL: 672 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(rr)) { 673 log_warnx("rde_dispatch_parent: " 674 "wrong imsg len"); 675 break; 676 } 677 memcpy(&rr, imsg.data, sizeof(rr)); 678 679 if ((lsa = rde_asext_put(&rr)) != NULL) { 680 v = lsa_find(NULL, lsa->hdr.type, 681 lsa->hdr.ls_id, lsa->hdr.adv_rtr); 682 683 /* 684 * if v == NULL no LSA is in the table and 685 * nothing has to be done. 686 */ 687 if (v) 688 lsa_merge(nbrself, lsa, v); 689 else 690 free(lsa); 691 } 692 break; 693 case IMSG_KROUTE_GET: 694 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(kr)) { 695 log_warnx("rde_dispatch_parent: " 696 "wrong imsg len"); 697 break; 698 } 699 memcpy(&kr, imsg.data, sizeof(kr)); 700 701 if ((rn = rt_find(&kr.prefix, kr.prefixlen, 702 DT_NET)) != NULL) 703 rde_send_change_kroute(rn); 704 else 705 /* should not happen */ 706 imsg_compose_event(iev_main, IMSG_KROUTE_DELETE, 707 0, 0, -1, &kr, sizeof(kr)); 708 break; 709 case IMSG_IFINFO: 710 if (imsg.hdr.len != IMSG_HEADER_SIZE + 711 sizeof(struct iface)) 712 fatalx("IFINFO imsg with wrong len"); 713 714 ifp = imsg.data; 715 iface = if_find(ifp->ifindex); 716 if (iface == NULL) 717 fatalx("interface lost in rde"); 718 719 wasvalid = (iface->flags & IFF_UP) && 720 LINK_STATE_IS_UP(iface->linkstate); 721 722 if_update(iface, ifp->mtu, ifp->flags, ifp->media_type, 723 ifp->linkstate, ifp->baudrate); 724 725 /* Resend LSAs if interface state changes. */ 726 if (wasvalid != (iface->flags & IFF_UP) && 727 LINK_STATE_IS_UP(iface->linkstate)) { 728 area = area_find(rdeconf, iface->area_id); 729 if (!area) 730 fatalx("interface lost area"); 731 orig_intra_area_prefix_lsas(area); 732 } 733 break; 734 case IMSG_IFADD: 735 if ((iface = malloc(sizeof(struct iface))) == NULL) 736 fatal(NULL); 737 memcpy(iface, imsg.data, sizeof(struct iface)); 738 739 LIST_INIT(&iface->nbr_list); 740 TAILQ_INIT(&iface->ls_ack_list); 741 RB_INIT(&iface->lsa_tree); 742 743 area = area_find(rdeconf, iface->area_id); 744 LIST_INSERT_HEAD(&area->iface_list, iface, entry); 745 break; 746 case IMSG_IFDELETE: 747 if (imsg.hdr.len != IMSG_HEADER_SIZE + 748 sizeof(ifindex)) 749 fatalx("IFDELETE imsg with wrong len"); 750 751 memcpy(&ifindex, imsg.data, sizeof(ifindex)); 752 iface = if_find(ifindex); 753 if (iface == NULL) 754 fatalx("interface lost in rde"); 755 756 LIST_REMOVE(iface, entry); 757 if_del(iface); 758 break; 759 case IMSG_IFADDRNEW: 760 if (imsg.hdr.len != IMSG_HEADER_SIZE + 761 sizeof(struct ifaddrchange)) 762 fatalx("IFADDRNEW imsg with wrong len"); 763 ifc = imsg.data; 764 765 iface = if_find(ifc->ifindex); 766 if (iface == NULL) 767 fatalx("IFADDRNEW interface lost in rde"); 768 769 if ((ia = calloc(1, sizeof(struct iface_addr))) == 770 NULL) 771 fatal("rde_dispatch_parent IFADDRNEW"); 772 ia->addr = ifc->addr; 773 ia->dstbrd = ifc->dstbrd; 774 ia->prefixlen = ifc->prefixlen; 775 776 TAILQ_INSERT_TAIL(&iface->ifa_list, ia, entry); 777 area = area_find(rdeconf, iface->area_id); 778 if (area) 779 orig_intra_area_prefix_lsas(area); 780 break; 781 case IMSG_IFADDRDEL: 782 if (imsg.hdr.len != IMSG_HEADER_SIZE + 783 sizeof(struct ifaddrchange)) 784 fatalx("IFADDRDEL imsg with wrong len"); 785 ifc = imsg.data; 786 787 iface = if_find(ifc->ifindex); 788 if (iface == NULL) 789 fatalx("IFADDRDEL interface lost in rde"); 790 791 for (ia = TAILQ_FIRST(&iface->ifa_list); ia != NULL; 792 ia = nia) { 793 nia = TAILQ_NEXT(ia, entry); 794 795 if (IN6_ARE_ADDR_EQUAL(&ia->addr, 796 &ifc->addr)) { 797 TAILQ_REMOVE(&iface->ifa_list, ia, 798 entry); 799 free(ia); 800 break; 801 } 802 } 803 area = area_find(rdeconf, iface->area_id); 804 if (area) 805 orig_intra_area_prefix_lsas(area); 806 break; 807 case IMSG_RECONF_CONF: 808 if ((nconf = malloc(sizeof(struct ospfd_conf))) == 809 NULL) 810 fatal(NULL); 811 memcpy(nconf, imsg.data, sizeof(struct ospfd_conf)); 812 813 LIST_INIT(&nconf->area_list); 814 LIST_INIT(&nconf->cand_list); 815 break; 816 case IMSG_RECONF_AREA: 817 if ((narea = area_new()) == NULL) 818 fatal(NULL); 819 memcpy(narea, imsg.data, sizeof(struct area)); 820 821 LIST_INIT(&narea->iface_list); 822 LIST_INIT(&narea->nbr_list); 823 RB_INIT(&narea->lsa_tree); 824 825 LIST_INSERT_HEAD(&nconf->area_list, narea, entry); 826 break; 827 case IMSG_RECONF_END: 828 merge_config(rdeconf, nconf); 829 nconf = NULL; 830 break; 831 default: 832 log_debug("rde_dispatch_parent: unexpected imsg %d", 833 imsg.hdr.type); 834 break; 835 } 836 imsg_free(&imsg); 837 } 838 if (!shut) 839 imsg_event_add(iev); 840 else { 841 /* this pipe is dead, so remove the event handler */ 842 event_del(&iev->ev); 843 event_loopexit(NULL); 844 } 845 } 846 847 void 848 rde_dump_area(struct area *area, int imsg_type, pid_t pid) 849 { 850 struct iface *iface; 851 852 /* dump header */ 853 imsg_compose_event(iev_ospfe, IMSG_CTL_AREA, 0, pid, -1, 854 area, sizeof(*area)); 855 856 /* dump link local lsa */ 857 LIST_FOREACH(iface, &area->iface_list, entry) { 858 imsg_compose_event(iev_ospfe, IMSG_CTL_IFACE, 859 0, pid, -1, iface, sizeof(*iface)); 860 lsa_dump(&iface->lsa_tree, imsg_type, pid); 861 } 862 863 /* dump area lsa */ 864 lsa_dump(&area->lsa_tree, imsg_type, pid); 865 } 866 867 u_int32_t 868 rde_router_id(void) 869 { 870 return (rdeconf->rtr_id.s_addr); 871 } 872 873 void 874 rde_send_change_kroute(struct rt_node *r) 875 { 876 struct kroute kr; 877 struct rt_nexthop *rn; 878 879 TAILQ_FOREACH(rn, &r->nexthop, entry) { 880 if (!rn->invalid) 881 break; 882 } 883 if (!rn) 884 fatalx("rde_send_change_kroute: no valid nexthop found"); 885 886 bzero(&kr, sizeof(kr)); 887 kr.prefix = r->prefix; 888 kr.nexthop = rn->nexthop; 889 if (IN6_IS_ADDR_LINKLOCAL(&rn->nexthop) || 890 IN6_IS_ADDR_MC_LINKLOCAL(&rn->nexthop)) 891 kr.scope = rn->ifindex; 892 kr.ifindex = rn->ifindex; 893 kr.prefixlen = r->prefixlen; 894 kr.ext_tag = r->ext_tag; 895 896 imsg_compose_event(iev_main, IMSG_KROUTE_CHANGE, 0, 0, -1, 897 &kr, sizeof(kr)); 898 } 899 900 void 901 rde_send_delete_kroute(struct rt_node *r) 902 { 903 struct kroute kr; 904 905 bzero(&kr, sizeof(kr)); 906 kr.prefix = r->prefix; 907 kr.prefixlen = r->prefixlen; 908 909 imsg_compose_event(iev_main, IMSG_KROUTE_DELETE, 0, 0, -1, 910 &kr, sizeof(kr)); 911 } 912 913 void 914 rde_send_summary(pid_t pid) 915 { 916 static struct ctl_sum sumctl; 917 struct timeval now; 918 struct area *area; 919 struct vertex *v; 920 921 bzero(&sumctl, sizeof(struct ctl_sum)); 922 923 sumctl.rtr_id.s_addr = rde_router_id(); 924 sumctl.spf_delay = rdeconf->spf_delay; 925 sumctl.spf_hold_time = rdeconf->spf_hold_time; 926 927 LIST_FOREACH(area, &rdeconf->area_list, entry) 928 sumctl.num_area++; 929 930 RB_FOREACH(v, lsa_tree, &asext_tree) 931 sumctl.num_ext_lsa++; 932 933 gettimeofday(&now, NULL); 934 if (rdeconf->uptime < now.tv_sec) 935 sumctl.uptime = now.tv_sec - rdeconf->uptime; 936 else 937 sumctl.uptime = 0; 938 939 rde_imsg_compose_ospfe(IMSG_CTL_SHOW_SUM, 0, pid, &sumctl, 940 sizeof(sumctl)); 941 } 942 943 void 944 rde_send_summary_area(struct area *area, pid_t pid) 945 { 946 static struct ctl_sum_area sumareactl; 947 struct iface *iface; 948 struct rde_nbr *nbr; 949 struct lsa_tree *tree = &area->lsa_tree; 950 struct vertex *v; 951 952 bzero(&sumareactl, sizeof(struct ctl_sum_area)); 953 954 sumareactl.area.s_addr = area->id.s_addr; 955 sumareactl.num_spf_calc = area->num_spf_calc; 956 957 LIST_FOREACH(iface, &area->iface_list, entry) 958 sumareactl.num_iface++; 959 960 LIST_FOREACH(nbr, &area->nbr_list, entry) 961 if (nbr->state == NBR_STA_FULL && !nbr->self) 962 sumareactl.num_adj_nbr++; 963 964 RB_FOREACH(v, lsa_tree, tree) 965 sumareactl.num_lsa++; 966 967 rde_imsg_compose_ospfe(IMSG_CTL_SHOW_SUM_AREA, 0, pid, &sumareactl, 968 sizeof(sumareactl)); 969 } 970 971 LIST_HEAD(rde_nbr_head, rde_nbr); 972 973 struct nbr_table { 974 struct rde_nbr_head *hashtbl; 975 u_int32_t hashmask; 976 } rdenbrtable; 977 978 #define RDE_NBR_HASH(x) \ 979 &rdenbrtable.hashtbl[(x) & rdenbrtable.hashmask] 980 981 void 982 rde_nbr_init(u_int32_t hashsize) 983 { 984 struct rde_nbr_head *head; 985 u_int32_t hs, i; 986 987 for (hs = 1; hs < hashsize; hs <<= 1) 988 ; 989 rdenbrtable.hashtbl = calloc(hs, sizeof(struct rde_nbr_head)); 990 if (rdenbrtable.hashtbl == NULL) 991 fatal("rde_nbr_init"); 992 993 for (i = 0; i < hs; i++) 994 LIST_INIT(&rdenbrtable.hashtbl[i]); 995 996 rdenbrtable.hashmask = hs - 1; 997 998 if ((nbrself = calloc(1, sizeof(*nbrself))) == NULL) 999 fatal("rde_nbr_init"); 1000 1001 nbrself->id.s_addr = rde_router_id(); 1002 nbrself->peerid = NBR_IDSELF; 1003 nbrself->state = NBR_STA_DOWN; 1004 nbrself->self = 1; 1005 head = RDE_NBR_HASH(NBR_IDSELF); 1006 LIST_INSERT_HEAD(head, nbrself, hash); 1007 } 1008 1009 void 1010 rde_nbr_free(void) 1011 { 1012 free(nbrself); 1013 free(rdenbrtable.hashtbl); 1014 } 1015 1016 struct rde_nbr * 1017 rde_nbr_find(u_int32_t peerid) 1018 { 1019 struct rde_nbr_head *head; 1020 struct rde_nbr *nbr; 1021 1022 head = RDE_NBR_HASH(peerid); 1023 1024 LIST_FOREACH(nbr, head, hash) { 1025 if (nbr->peerid == peerid) 1026 return (nbr); 1027 } 1028 1029 return (NULL); 1030 } 1031 1032 struct rde_nbr * 1033 rde_nbr_new(u_int32_t peerid, struct rde_nbr *new) 1034 { 1035 struct rde_nbr_head *head; 1036 struct rde_nbr *nbr; 1037 struct area *area; 1038 struct iface *iface; 1039 1040 if (rde_nbr_find(peerid)) 1041 return (NULL); 1042 if ((area = area_find(rdeconf, new->area_id)) == NULL) 1043 fatalx("rde_nbr_new: unknown area"); 1044 1045 if ((iface = if_find(new->ifindex)) == NULL) 1046 fatalx("rde_nbr_new: unknown interface"); 1047 1048 if ((nbr = calloc(1, sizeof(*nbr))) == NULL) 1049 fatal("rde_nbr_new"); 1050 1051 memcpy(nbr, new, sizeof(*nbr)); 1052 nbr->peerid = peerid; 1053 nbr->area = area; 1054 nbr->iface = iface; 1055 1056 TAILQ_INIT(&nbr->req_list); 1057 1058 head = RDE_NBR_HASH(peerid); 1059 LIST_INSERT_HEAD(head, nbr, hash); 1060 LIST_INSERT_HEAD(&area->nbr_list, nbr, entry); 1061 1062 return (nbr); 1063 } 1064 1065 void 1066 rde_nbr_del(struct rde_nbr *nbr) 1067 { 1068 if (nbr == NULL) 1069 return; 1070 1071 rde_req_list_free(nbr); 1072 1073 LIST_REMOVE(nbr, entry); 1074 LIST_REMOVE(nbr, hash); 1075 1076 free(nbr); 1077 } 1078 1079 int 1080 rde_nbr_loading(struct area *area) 1081 { 1082 struct rde_nbr *nbr; 1083 int checkall = 0; 1084 1085 if (area == NULL) { 1086 area = LIST_FIRST(&rdeconf->area_list); 1087 checkall = 1; 1088 } 1089 1090 while (area != NULL) { 1091 LIST_FOREACH(nbr, &area->nbr_list, entry) { 1092 if (nbr->self) 1093 continue; 1094 if (nbr->state & NBR_STA_XCHNG || 1095 nbr->state & NBR_STA_LOAD) 1096 return (1); 1097 } 1098 if (!checkall) 1099 break; 1100 area = LIST_NEXT(area, entry); 1101 } 1102 1103 return (0); 1104 } 1105 1106 struct rde_nbr * 1107 rde_nbr_self(struct area *area) 1108 { 1109 struct rde_nbr *nbr; 1110 1111 LIST_FOREACH(nbr, &area->nbr_list, entry) 1112 if (nbr->self) 1113 return (nbr); 1114 1115 /* this may not happen */ 1116 fatalx("rde_nbr_self: area without self"); 1117 return (NULL); 1118 } 1119 1120 /* 1121 * LSA req list 1122 */ 1123 void 1124 rde_req_list_add(struct rde_nbr *nbr, struct lsa_hdr *lsa) 1125 { 1126 struct rde_req_entry *le; 1127 1128 if ((le = calloc(1, sizeof(*le))) == NULL) 1129 fatal("rde_req_list_add"); 1130 1131 TAILQ_INSERT_TAIL(&nbr->req_list, le, entry); 1132 le->type = lsa->type; 1133 le->ls_id = lsa->ls_id; 1134 le->adv_rtr = lsa->adv_rtr; 1135 } 1136 1137 int 1138 rde_req_list_exists(struct rde_nbr *nbr, struct lsa_hdr *lsa_hdr) 1139 { 1140 struct rde_req_entry *le; 1141 1142 TAILQ_FOREACH(le, &nbr->req_list, entry) { 1143 if ((lsa_hdr->type == le->type) && 1144 (lsa_hdr->ls_id == le->ls_id) && 1145 (lsa_hdr->adv_rtr == le->adv_rtr)) 1146 return (1); 1147 } 1148 return (0); 1149 } 1150 1151 void 1152 rde_req_list_del(struct rde_nbr *nbr, struct lsa_hdr *lsa_hdr) 1153 { 1154 struct rde_req_entry *le; 1155 1156 TAILQ_FOREACH(le, &nbr->req_list, entry) { 1157 if ((lsa_hdr->type == le->type) && 1158 (lsa_hdr->ls_id == le->ls_id) && 1159 (lsa_hdr->adv_rtr == le->adv_rtr)) { 1160 TAILQ_REMOVE(&nbr->req_list, le, entry); 1161 free(le); 1162 return; 1163 } 1164 } 1165 } 1166 1167 void 1168 rde_req_list_free(struct rde_nbr *nbr) 1169 { 1170 struct rde_req_entry *le; 1171 1172 while ((le = TAILQ_FIRST(&nbr->req_list)) != NULL) { 1173 TAILQ_REMOVE(&nbr->req_list, le, entry); 1174 free(le); 1175 } 1176 } 1177 1178 /* 1179 * as-external LSA handling 1180 */ 1181 struct lsa * 1182 rde_asext_get(struct rroute *rr) 1183 { 1184 struct area *area; 1185 struct iface *iface; 1186 struct iface_addr *ia; 1187 struct in6_addr addr; 1188 1189 LIST_FOREACH(area, &rdeconf->area_list, entry) 1190 LIST_FOREACH(iface, &area->iface_list, entry) 1191 TAILQ_FOREACH(ia, &iface->ifa_list, entry) { 1192 if (IN6_IS_ADDR_LINKLOCAL(&ia->addr)) 1193 continue; 1194 1195 inet6applymask(&addr, &ia->addr, 1196 rr->kr.prefixlen); 1197 if (!memcmp(&addr, &rr->kr.prefix, 1198 sizeof(addr)) && rr->kr.prefixlen == 1199 ia->prefixlen) { 1200 /* already announced as Prefix LSA */ 1201 log_debug("rde_asext_get: %s/%d is " 1202 "part of prefix LSA", 1203 log_in6addr(&rr->kr.prefix), 1204 rr->kr.prefixlen); 1205 return (NULL); 1206 } 1207 } 1208 1209 /* update of seqnum is done by lsa_merge */ 1210 return (orig_asext_lsa(rr, DEFAULT_AGE)); 1211 } 1212 1213 struct lsa * 1214 rde_asext_put(struct rroute *rr) 1215 { 1216 /* 1217 * just try to remove the LSA. If the prefix is announced as 1218 * stub net LSA lsa_find() will fail later and nothing will happen. 1219 */ 1220 1221 /* remove by reflooding with MAX_AGE */ 1222 return (orig_asext_lsa(rr, MAX_AGE)); 1223 } 1224 1225 /* 1226 * summary LSA stuff 1227 */ 1228 void 1229 rde_summary_update(struct rt_node *rte, struct area *area) 1230 { 1231 struct vertex *v = NULL; 1232 //XXX struct lsa *lsa; 1233 u_int16_t type = 0; 1234 1235 /* first check if we actually need to announce this route */ 1236 if (!(rte->d_type == DT_NET || rte->flags & OSPF_RTR_E)) 1237 return; 1238 /* never create summaries for as-ext LSA */ 1239 if (rte->p_type == PT_TYPE1_EXT || rte->p_type == PT_TYPE2_EXT) 1240 return; 1241 /* no need for summary LSA in the originating area */ 1242 if (rte->area.s_addr == area->id.s_addr) 1243 return; 1244 /* no need to originate inter-area routes to the backbone */ 1245 if (rte->p_type == PT_INTER_AREA && area->id.s_addr == INADDR_ANY) 1246 return; 1247 /* TODO nexthop check, nexthop part of area -> no summary */ 1248 if (rte->cost >= LS_INFINITY) 1249 return; 1250 /* TODO AS border router specific checks */ 1251 /* TODO inter-area network route stuff */ 1252 /* TODO intra-area stuff -- condense LSA ??? */ 1253 1254 if (rte->d_type == DT_NET) { 1255 type = LSA_TYPE_INTER_A_PREFIX; 1256 } else if (rte->d_type == DT_RTR) { 1257 type = LSA_TYPE_INTER_A_ROUTER; 1258 } else 1259 1260 #if 0 /* XXX a lot todo */ 1261 /* update lsa but only if it was changed */ 1262 v = lsa_find(area, type, rte->prefix.s_addr, rde_router_id()); 1263 lsa = orig_sum_lsa(rte, area, type, rte->invalid); 1264 lsa_merge(rde_nbr_self(area), lsa, v); 1265 1266 if (v == NULL) 1267 v = lsa_find(area, type, rte->prefix.s_addr, rde_router_id()); 1268 #endif 1269 1270 /* suppressed/deleted routes are not found in the second lsa_find */ 1271 if (v) 1272 v->cost = rte->cost; 1273 } 1274 1275 /* 1276 * Functions for self-originated LSAs 1277 */ 1278 1279 /* Prefix LSAs have variable size. We have to be careful to copy the right 1280 * amount of bytes, and to realloc() the right amount of memory. */ 1281 void 1282 append_prefix_lsa(struct lsa **lsa, u_int16_t *len, struct lsa_prefix *prefix) 1283 { 1284 struct lsa_prefix *copy; 1285 unsigned int lsa_prefix_len; 1286 unsigned int new_len; 1287 char *new_lsa; 1288 1289 lsa_prefix_len = sizeof(struct lsa_prefix) 1290 + LSA_PREFIXSIZE(prefix->prefixlen); 1291 1292 new_len = *len + lsa_prefix_len; 1293 1294 /* Make sure we have enough space for this prefix. */ 1295 if ((new_lsa = realloc(*lsa, new_len)) == NULL) 1296 fatalx("append_prefix_lsa"); 1297 1298 /* Append prefix to LSA. */ 1299 copy = (struct lsa_prefix *)(new_lsa + *len); 1300 memcpy(copy, prefix, lsa_prefix_len); 1301 copy->metric = 0; 1302 1303 *lsa = (struct lsa *)new_lsa; 1304 *len = new_len; 1305 } 1306 1307 int 1308 prefix_compare(struct prefix_node *a, struct prefix_node *b) 1309 { 1310 struct lsa_prefix *p; 1311 struct lsa_prefix *q; 1312 int i; 1313 int len; 1314 1315 p = a->prefix; 1316 q = b->prefix; 1317 1318 len = MIN(LSA_PREFIXSIZE(p->prefixlen), LSA_PREFIXSIZE(q->prefixlen)); 1319 1320 i = memcmp(p + 1, q + 1, len); 1321 if (i) 1322 return (i); 1323 if (p->prefixlen < q->prefixlen) 1324 return (-1); 1325 if (p->prefixlen > q->prefixlen) 1326 return (1); 1327 return (0); 1328 } 1329 1330 void 1331 prefix_tree_add(struct prefix_tree *tree, struct lsa_link *lsa) 1332 { 1333 struct prefix_node *old; 1334 struct prefix_node *new; 1335 struct in6_addr addr; 1336 unsigned int len; 1337 unsigned int i; 1338 char *cur_prefix; 1339 1340 cur_prefix = (char *)(lsa + 1); 1341 1342 for (i = 0; i < ntohl(lsa->numprefix); i++) { 1343 if ((new = calloc(1, sizeof(*new))) == NULL) 1344 fatal("prefix_tree_add"); 1345 new->prefix = (struct lsa_prefix *)cur_prefix; 1346 1347 len = sizeof(*new->prefix) 1348 + LSA_PREFIXSIZE(new->prefix->prefixlen); 1349 1350 bzero(&addr, sizeof(addr)); 1351 memcpy(&addr, new->prefix + 1, 1352 LSA_PREFIXSIZE(new->prefix->prefixlen)); 1353 1354 if (!(IN6_IS_ADDR_LINKLOCAL(&addr)) && 1355 (new->prefix->options & OSPF_PREFIX_NU) == 0 && 1356 (new->prefix->options & OSPF_PREFIX_LA) == 0) { 1357 old = RB_INSERT(prefix_tree, tree, new); 1358 if (old != NULL) { 1359 old->prefix->options |= new->prefix->options; 1360 free(new); 1361 } 1362 } 1363 1364 cur_prefix = cur_prefix + len; 1365 } 1366 } 1367 1368 RB_GENERATE(prefix_tree, prefix_node, entry, prefix_compare) 1369 1370 struct lsa * 1371 orig_intra_lsa_net(struct area *area, struct iface *iface, struct vertex *old) 1372 { 1373 struct lsa *lsa; 1374 struct vertex *v; 1375 struct rde_nbr *nbr; 1376 struct prefix_node *node; 1377 struct prefix_tree tree; 1378 int num_full_nbr; 1379 u_int16_t len; 1380 u_int16_t numprefix; 1381 1382 log_debug("orig_intra_lsa_net: area %s, interface %s", 1383 inet_ntoa(area->id), iface->name); 1384 1385 RB_INIT(&tree); 1386 1387 if (iface->state & IF_STA_DR) { 1388 num_full_nbr = 0; 1389 LIST_FOREACH(nbr, &area->nbr_list, entry) { 1390 if (nbr->self || 1391 nbr->iface->ifindex != iface->ifindex || 1392 (nbr->state & NBR_STA_FULL) == 0) 1393 continue; 1394 num_full_nbr++; 1395 v = lsa_find(iface, htons(LSA_TYPE_LINK), 1396 htonl(nbr->iface_id), nbr->id.s_addr); 1397 if (v) 1398 prefix_tree_add(&tree, &v->lsa->data.link); 1399 } 1400 if (num_full_nbr == 0) { 1401 /* There are no adjacent neighbors on link. 1402 * If a copy of this LSA already exists in DB, 1403 * it needs to be flushed. orig_intra_lsa_rtr() 1404 * will take care of prefixes configured on 1405 * this interface. */ 1406 if (!old) 1407 return NULL; 1408 } else { 1409 /* Add our own prefixes configured for this link. */ 1410 v = lsa_find(iface, htons(LSA_TYPE_LINK), 1411 htonl(iface->ifindex), rde_router_id()); 1412 if (v) 1413 prefix_tree_add(&tree, &v->lsa->data.link); 1414 } 1415 /* Continue only if a copy of this LSA already exists in DB. 1416 * It needs to be flushed. */ 1417 } else if (!old) 1418 return NULL; 1419 1420 len = sizeof(struct lsa_hdr) + sizeof(struct lsa_intra_prefix); 1421 if ((lsa = calloc(1, len)) == NULL) 1422 fatal("orig_intra_lsa_net"); 1423 1424 lsa->data.pref_intra.ref_type = htons(LSA_TYPE_NETWORK); 1425 lsa->data.pref_intra.ref_ls_id = htonl(iface->ifindex); 1426 lsa->data.pref_intra.ref_adv_rtr = rde_router_id(); 1427 1428 numprefix = 0; 1429 RB_FOREACH(node, prefix_tree, &tree) { 1430 append_prefix_lsa(&lsa, &len, node->prefix); 1431 numprefix++; 1432 } 1433 1434 lsa->data.pref_intra.numprefix = htons(numprefix); 1435 1436 while (!RB_EMPTY(&tree)) 1437 free(RB_REMOVE(prefix_tree, &tree, RB_ROOT(&tree))); 1438 1439 /* LSA header */ 1440 /* If numprefix is zero, originate with MAX_AGE to flush LSA. */ 1441 lsa->hdr.age = numprefix == 0 ? htons(MAX_AGE) : htons(DEFAULT_AGE); 1442 lsa->hdr.type = htons(LSA_TYPE_INTRA_A_PREFIX); 1443 lsa->hdr.ls_id = htonl(iface->ifindex); 1444 lsa->hdr.adv_rtr = rde_router_id(); 1445 lsa->hdr.seq_num = htonl(INIT_SEQ_NUM); 1446 lsa->hdr.len = htons(len); 1447 lsa->hdr.ls_chksum = htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET)); 1448 1449 return lsa; 1450 } 1451 1452 struct lsa * 1453 orig_intra_lsa_rtr(struct area *area, struct vertex *old) 1454 { 1455 char lsa_prefix_buf[sizeof(struct lsa_prefix) 1456 + sizeof(struct in6_addr)]; 1457 struct lsa *lsa; 1458 struct lsa_prefix *lsa_prefix; 1459 struct in6_addr *prefix; 1460 struct iface *iface; 1461 struct iface_addr *ia; 1462 struct rde_nbr *nbr; 1463 u_int16_t len; 1464 u_int16_t numprefix; 1465 1466 len = sizeof(struct lsa_hdr) + sizeof(struct lsa_intra_prefix); 1467 if ((lsa = calloc(1, len)) == NULL) 1468 fatal("orig_intra_lsa_rtr"); 1469 1470 lsa->data.pref_intra.ref_type = htons(LSA_TYPE_ROUTER); 1471 lsa->data.pref_intra.ref_ls_id = 0; 1472 lsa->data.pref_intra.ref_adv_rtr = rde_router_id(); 1473 1474 numprefix = 0; 1475 LIST_FOREACH(iface, &area->iface_list, entry) { 1476 if (!((iface->flags & IFF_UP) && 1477 LINK_STATE_IS_UP(iface->linkstate))) 1478 /* interface or link state down */ 1479 continue; 1480 if ((iface->state & IF_STA_DOWN) && 1481 !(iface->cflags & F_IFACE_PASSIVE)) 1482 /* passive interfaces stay in state DOWN */ 1483 continue; 1484 1485 /* Broadcast links with adjacencies are handled 1486 * by orig_intra_lsa_net(), ignore. */ 1487 if (iface->type == IF_TYPE_BROADCAST || 1488 iface->type == IF_TYPE_NBMA) { 1489 if (iface->state & IF_STA_WAITING) 1490 /* Skip, we're still waiting for 1491 * adjacencies to form. */ 1492 continue; 1493 1494 LIST_FOREACH(nbr, &area->nbr_list, entry) 1495 if (!nbr->self && 1496 nbr->iface->ifindex == iface->ifindex && 1497 nbr->state & NBR_STA_FULL) 1498 break; 1499 if (nbr) 1500 continue; 1501 } 1502 1503 lsa_prefix = (struct lsa_prefix *)lsa_prefix_buf; 1504 1505 TAILQ_FOREACH(ia, &iface->ifa_list, entry) { 1506 if (IN6_IS_ADDR_LINKLOCAL(&ia->addr)) 1507 continue; 1508 1509 bzero(lsa_prefix_buf, sizeof(lsa_prefix_buf)); 1510 1511 if (iface->type == IF_TYPE_POINTOMULTIPOINT || 1512 iface->state & IF_STA_LOOPBACK) { 1513 lsa_prefix->prefixlen = 128; 1514 } else { 1515 lsa_prefix->prefixlen = ia->prefixlen; 1516 lsa_prefix->metric = htons(iface->metric); 1517 } 1518 1519 if (lsa_prefix->prefixlen == 128) 1520 lsa_prefix->options |= OSPF_PREFIX_LA; 1521 1522 log_debug("orig_intra_lsa_rtr: area %s, interface %s: " 1523 "%s/%d", inet_ntoa(area->id), 1524 iface->name, log_in6addr(&ia->addr), 1525 lsa_prefix->prefixlen); 1526 1527 prefix = (struct in6_addr *)(lsa_prefix + 1); 1528 inet6applymask(prefix, &ia->addr, 1529 lsa_prefix->prefixlen); 1530 append_prefix_lsa(&lsa, &len, lsa_prefix); 1531 numprefix++; 1532 } 1533 1534 /* TOD: Add prefixes of directly attached hosts, too */ 1535 /* TOD: Add prefixes for virtual links */ 1536 } 1537 1538 /* If no prefixes were included, continue only if a copy of this 1539 * LSA already exists in DB. It needs to be flushed. */ 1540 if (numprefix == 0 && !old) { 1541 free(lsa); 1542 return NULL; 1543 } 1544 1545 lsa->data.pref_intra.numprefix = htons(numprefix); 1546 1547 /* LSA header */ 1548 /* If numprefix is zero, originate with MAX_AGE to flush LSA. */ 1549 lsa->hdr.age = numprefix == 0 ? htons(MAX_AGE) : htons(DEFAULT_AGE); 1550 lsa->hdr.type = htons(LSA_TYPE_INTRA_A_PREFIX); 1551 lsa->hdr.ls_id = htonl(LS_ID_INTRA_RTR); 1552 lsa->hdr.adv_rtr = rde_router_id(); 1553 lsa->hdr.seq_num = htonl(INIT_SEQ_NUM); 1554 lsa->hdr.len = htons(len); 1555 lsa->hdr.ls_chksum = htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET)); 1556 1557 return lsa; 1558 } 1559 1560 void 1561 orig_intra_area_prefix_lsas(struct area *area) 1562 { 1563 struct lsa *lsa; 1564 struct vertex *old; 1565 struct iface *iface; 1566 1567 LIST_FOREACH(iface, &area->iface_list, entry) { 1568 if (iface->type == IF_TYPE_BROADCAST || 1569 iface->type == IF_TYPE_NBMA) { 1570 old = lsa_find(iface, htons(LSA_TYPE_INTRA_A_PREFIX), 1571 htonl(iface->ifindex), rde_router_id()); 1572 lsa = orig_intra_lsa_net(area, iface, old); 1573 if (lsa) 1574 lsa_merge(rde_nbr_self(area), lsa, old); 1575 } 1576 } 1577 1578 old = lsa_find_tree(&area->lsa_tree, htons(LSA_TYPE_INTRA_A_PREFIX), 1579 htonl(LS_ID_INTRA_RTR), rde_router_id()); 1580 lsa = orig_intra_lsa_rtr(area, old); 1581 if (lsa) 1582 lsa_merge(rde_nbr_self(area), lsa, old); 1583 } 1584 1585 int 1586 comp_asext(struct lsa *a, struct lsa *b) 1587 { 1588 /* compare prefixes, if they are equal or not */ 1589 if (a->data.asext.prefix.prefixlen != b->data.asext.prefix.prefixlen) 1590 return (-1); 1591 return (memcmp( 1592 (char *)a + sizeof(struct lsa_hdr) + sizeof(struct lsa_asext), 1593 (char *)b + sizeof(struct lsa_hdr) + sizeof(struct lsa_asext), 1594 LSA_PREFIXSIZE(a->data.asext.prefix.prefixlen))); 1595 } 1596 1597 struct lsa * 1598 orig_asext_lsa(struct rroute *rr, u_int16_t age) 1599 { 1600 struct lsa *lsa; 1601 u_int32_t ext_tag; 1602 u_int16_t len, ext_off; 1603 1604 len = sizeof(struct lsa_hdr) + sizeof(struct lsa_asext) + 1605 LSA_PREFIXSIZE(rr->kr.prefixlen); 1606 1607 /* 1608 * nexthop -- on connected routes we are the nexthop, 1609 * on all other cases we should announce the true nexthop 1610 * unless that nexthop is outside of the ospf cloud. 1611 * XXX for now we don't do this. 1612 */ 1613 1614 ext_off = len; 1615 if (rr->kr.ext_tag) { 1616 len += sizeof(ext_tag); 1617 } 1618 if ((lsa = calloc(1, len)) == NULL) 1619 fatal("orig_asext_lsa"); 1620 1621 log_debug("orig_asext_lsa: %s/%d age %d", 1622 log_in6addr(&rr->kr.prefix), rr->kr.prefixlen, age); 1623 1624 /* LSA header */ 1625 lsa->hdr.age = htons(age); 1626 lsa->hdr.type = htons(LSA_TYPE_EXTERNAL); 1627 lsa->hdr.adv_rtr = rdeconf->rtr_id.s_addr; 1628 lsa->hdr.seq_num = htonl(INIT_SEQ_NUM); 1629 lsa->hdr.len = htons(len); 1630 1631 lsa->data.asext.prefix.prefixlen = rr->kr.prefixlen; 1632 memcpy((char *)lsa + sizeof(struct lsa_hdr) + sizeof(struct lsa_asext), 1633 &rr->kr.prefix, LSA_PREFIXSIZE(rr->kr.prefixlen)); 1634 1635 lsa->hdr.ls_id = lsa_find_lsid(&asext_tree, lsa->hdr.type, 1636 lsa->hdr.adv_rtr, comp_asext, lsa); 1637 1638 if (age == MAX_AGE) { 1639 /* inherit metric and ext_tag from the current LSA, 1640 * some routers don't like to get withdraws that are 1641 * different from what they have in their table. 1642 */ 1643 struct vertex *v; 1644 v = lsa_find(NULL, lsa->hdr.type, lsa->hdr.ls_id, 1645 lsa->hdr.adv_rtr); 1646 if (v != NULL) { 1647 rr->metric = ntohl(v->lsa->data.asext.metric); 1648 if (rr->metric & LSA_ASEXT_T_FLAG) { 1649 memcpy(&ext_tag, (char *)v->lsa + ext_off, 1650 sizeof(ext_tag)); 1651 rr->kr.ext_tag = ntohl(ext_tag); 1652 } 1653 rr->metric &= LSA_METRIC_MASK; 1654 } 1655 } 1656 1657 if (rr->kr.ext_tag) { 1658 lsa->data.asext.metric = htonl(rr->metric | LSA_ASEXT_T_FLAG); 1659 ext_tag = htonl(rr->kr.ext_tag); 1660 memcpy((char *)lsa + ext_off, &ext_tag, sizeof(ext_tag)); 1661 } else { 1662 lsa->data.asext.metric = htonl(rr->metric); 1663 } 1664 1665 lsa->hdr.ls_chksum = 0; 1666 lsa->hdr.ls_chksum = 1667 htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET)); 1668 1669 return (lsa); 1670 } 1671 1672 struct lsa * 1673 orig_sum_lsa(struct rt_node *rte, struct area *area, u_int8_t type, int invalid) 1674 { 1675 #if 0 /* XXX a lot todo */ 1676 struct lsa *lsa; 1677 u_int16_t len; 1678 1679 len = sizeof(struct lsa_hdr) + sizeof(struct lsa_sum); 1680 if ((lsa = calloc(1, len)) == NULL) 1681 fatal("orig_sum_lsa"); 1682 1683 /* LSA header */ 1684 lsa->hdr.age = htons(invalid ? MAX_AGE : DEFAULT_AGE); 1685 lsa->hdr.type = type; 1686 lsa->hdr.adv_rtr = rdeconf->rtr_id.s_addr; 1687 lsa->hdr.seq_num = htonl(INIT_SEQ_NUM); 1688 lsa->hdr.len = htons(len); 1689 1690 /* prefix and mask */ 1691 /* 1692 * TODO ls_id must be unique, for overlapping routes this may 1693 * not be true. In this case a hack needs to be done to 1694 * make the ls_id unique. 1695 */ 1696 lsa->hdr.ls_id = rte->prefix.s_addr; 1697 if (type == LSA_TYPE_SUM_NETWORK) 1698 lsa->data.sum.mask = prefixlen2mask(rte->prefixlen); 1699 else 1700 lsa->data.sum.mask = 0; /* must be zero per RFC */ 1701 1702 lsa->data.sum.metric = htonl(rte->cost & LSA_METRIC_MASK); 1703 1704 lsa->hdr.ls_chksum = 0; 1705 lsa->hdr.ls_chksum = 1706 htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET)); 1707 1708 return (lsa); 1709 #endif 1710 return NULL; 1711 } 1712