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