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