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