1 /* $OpenBSD: lsupdate.c,v 1.15 2019/12/28 09:25:24 denis Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2004, 2005, 2007 Esben Norby <norby@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/socket.h> 22 #include <netinet/in.h> 23 #include <netinet/ip6.h> 24 #include <netinet/ip_ah.h> 25 #include <arpa/inet.h> 26 27 #include <stdlib.h> 28 #include <string.h> 29 #include <siphash.h> 30 31 #include "ospf6.h" 32 #include "ospf6d.h" 33 #include "log.h" 34 #include "ospfe.h" 35 #include "rde.h" 36 37 extern struct ospfd_conf *oeconf; 38 extern struct imsgev *iev_rde; 39 40 struct ibuf *prepare_ls_update(struct iface *); 41 int add_ls_update(struct ibuf *, struct iface *, void *, u_int16_t, 42 u_int16_t); 43 int send_ls_update(struct ibuf *, struct iface *, struct in6_addr, 44 u_int32_t); 45 46 void ls_retrans_list_insert(struct nbr *, struct lsa_entry *); 47 void ls_retrans_list_remove(struct nbr *, struct lsa_entry *); 48 49 /* link state update packet handling */ 50 int 51 lsa_flood(struct iface *iface, struct nbr *originator, struct lsa_hdr *lsa_hdr, 52 void *data) 53 { 54 struct nbr *nbr; 55 struct lsa_entry *le = NULL; 56 int queued = 0, dont_ack = 0; 57 int r; 58 59 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 60 if (nbr == iface->self) 61 continue; 62 if (!(nbr->state & NBR_STA_FLOOD)) 63 continue; 64 65 if (iface->state & IF_STA_DROTHER && !queued) 66 while ((le = ls_retrans_list_get(iface->self, lsa_hdr))) 67 ls_retrans_list_free(iface->self, le); 68 69 while ((le = ls_retrans_list_get(nbr, lsa_hdr))) 70 ls_retrans_list_free(nbr, le); 71 72 if (!(nbr->state & NBR_STA_FULL) && 73 (le = ls_req_list_get(nbr, lsa_hdr)) != NULL) { 74 r = lsa_newer(lsa_hdr, le->le_lsa); 75 if (r > 0) { 76 /* to flood LSA is newer than requested */ 77 ls_req_list_free(nbr, le); 78 /* new needs to be flooded */ 79 } else if (r < 0) { 80 /* to flood LSA is older than requested */ 81 continue; 82 } else { 83 /* LSA are equal */ 84 ls_req_list_free(nbr, le); 85 continue; 86 } 87 } 88 89 if (nbr == originator) { 90 dont_ack++; 91 continue; 92 } 93 94 /* non DR or BDR router keep all lsa in one retrans list */ 95 if (iface->state & IF_STA_DROTHER) { 96 if (!queued) 97 ls_retrans_list_add(iface->self, data, 98 iface->rxmt_interval, 0); 99 queued = 1; 100 } else { 101 ls_retrans_list_add(nbr, data, iface->rxmt_interval, 0); 102 queued = 1; 103 } 104 } 105 106 if (!queued) 107 return (0); 108 109 if (iface == originator->iface && iface->self != originator) { 110 if (iface->dr == originator || iface->bdr == originator) 111 return (0); 112 if (iface->state & IF_STA_BACKUP) 113 return (0); 114 dont_ack++; 115 } 116 117 /* 118 * initial flood needs to be queued separately, timeout is zero 119 * and oneshot has to be set because the retransimssion queues 120 * are already loaded. 121 */ 122 switch (iface->type) { 123 case IF_TYPE_POINTOPOINT: 124 case IF_TYPE_BROADCAST: 125 ls_retrans_list_add(iface->self, data, 0, 1); 126 break; 127 case IF_TYPE_NBMA: 128 case IF_TYPE_POINTOMULTIPOINT: 129 case IF_TYPE_VIRTUALLINK: 130 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 131 if (nbr == iface->self) 132 continue; 133 if (!(nbr->state & NBR_STA_FLOOD)) 134 continue; 135 if (!TAILQ_EMPTY(&nbr->ls_retrans_list)) { 136 le = TAILQ_LAST(&nbr->ls_retrans_list, 137 lsa_head); 138 if (lsa_hdr->type != le->le_lsa->type || 139 lsa_hdr->ls_id != le->le_lsa->ls_id || 140 lsa_hdr->adv_rtr != le->le_lsa->adv_rtr) 141 continue; 142 } 143 ls_retrans_list_add(nbr, data, 0, 1); 144 } 145 break; 146 default: 147 fatalx("lsa_flood: unknown interface type"); 148 } 149 150 return (dont_ack == 2); 151 } 152 153 struct ibuf * 154 prepare_ls_update(struct iface *iface) 155 { 156 struct ibuf *buf; 157 size_t reserved; 158 159 /* 160 * Reserve space for optional ah or esp encryption. The 161 * algorithm is taken from ah_output and esp_output, the 162 * values are the maxima of crypto/xform.c. 163 */ 164 reserved = max( 165 /* base-ah-header replay authsize */ 166 AH_FLENGTH + sizeof(u_int32_t) + 32, 167 /* spi sequence ivlen blocksize pad-length next-header authsize */ 168 2 * sizeof(u_int32_t) + 16 + 16 + 2 * sizeof(u_int8_t) + 32); 169 170 if ((buf = ibuf_dynamic(IPV6_MMTU - sizeof(struct ip6_hdr) - reserved, 171 IPV6_MAXPACKET - sizeof(struct ip6_hdr) - reserved)) == NULL) 172 fatal("prepare_ls_update"); 173 174 /* OSPF header */ 175 if (gen_ospf_hdr(buf, iface, PACKET_TYPE_LS_UPDATE)) 176 goto fail; 177 178 /* reserve space for number of lsa field */ 179 if (ibuf_reserve(buf, sizeof(u_int32_t)) == NULL) 180 goto fail; 181 182 return (buf); 183 fail: 184 log_warn("prepare_ls_update"); 185 ibuf_free(buf); 186 return (NULL); 187 } 188 189 int 190 add_ls_update(struct ibuf *buf, struct iface *iface, void *data, u_int16_t len, 191 u_int16_t older) 192 { 193 void *lsage; 194 u_int16_t age; 195 196 if (buf->wpos + len >= buf->max) 197 return (0); 198 199 lsage = ibuf_reserve(buf, 0); 200 if (ibuf_add(buf, data, len)) { 201 log_warn("add_ls_update"); 202 return (0); 203 } 204 205 /* age LSA before sending it out */ 206 memcpy(&age, data, sizeof(age)); 207 age = ntohs(age); 208 if ((age += older + iface->transmit_delay) >= MAX_AGE) 209 age = MAX_AGE; 210 age = htons(age); 211 memcpy(lsage, &age, sizeof(age)); 212 213 return (1); 214 } 215 216 int 217 send_ls_update(struct ibuf *buf, struct iface *iface, struct in6_addr addr, 218 u_int32_t nlsa) 219 { 220 nlsa = htonl(nlsa); 221 memcpy(ibuf_seek(buf, sizeof(struct ospf_hdr), sizeof(nlsa)), 222 &nlsa, sizeof(nlsa)); 223 /* calculate checksum */ 224 if (upd_ospf_hdr(buf, iface)) 225 goto fail; 226 227 if (send_packet(iface, buf, &addr) == -1) 228 goto fail; 229 230 ibuf_free(buf); 231 return (0); 232 fail: 233 log_warn("send_ls_update"); 234 ibuf_free(buf); 235 return (-1); 236 } 237 238 void 239 recv_ls_update(struct nbr *nbr, char *buf, u_int16_t len) 240 { 241 struct lsa_hdr lsa; 242 u_int32_t nlsa; 243 244 if (len < sizeof(nlsa)) { 245 log_warnx("recv_ls_update: bad packet size, neighbor ID %s", 246 inet_ntoa(nbr->id)); 247 return; 248 } 249 memcpy(&nlsa, buf, sizeof(nlsa)); 250 nlsa = ntohl(nlsa); 251 buf += sizeof(nlsa); 252 len -= sizeof(nlsa); 253 254 switch (nbr->state) { 255 case NBR_STA_DOWN: 256 case NBR_STA_ATTEMPT: 257 case NBR_STA_INIT: 258 case NBR_STA_2_WAY: 259 case NBR_STA_XSTRT: 260 case NBR_STA_SNAP: 261 log_debug("recv_ls_update: packet ignored in state %s, " 262 "neighbor ID %s", nbr_state_name(nbr->state), 263 inet_ntoa(nbr->id)); 264 break; 265 case NBR_STA_XCHNG: 266 case NBR_STA_LOAD: 267 case NBR_STA_FULL: 268 for (; nlsa > 0 && len > 0; nlsa--) { 269 if (len < sizeof(lsa)) { 270 log_warnx("recv_ls_update: bad packet size, " 271 "neighbor ID %s", inet_ntoa(nbr->id)); 272 return; 273 } 274 memcpy(&lsa, buf, sizeof(lsa)); 275 if (len < ntohs(lsa.len)) { 276 log_warnx("recv_ls_update: bad packet size, " 277 "neighbor ID %s", inet_ntoa(nbr->id)); 278 return; 279 } 280 imsg_compose_event(iev_rde, IMSG_LS_UPD, nbr->peerid, 0, 281 -1, buf, ntohs(lsa.len)); 282 buf += ntohs(lsa.len); 283 len -= ntohs(lsa.len); 284 } 285 if (nlsa > 0 || len > 0) { 286 log_warnx("recv_ls_update: bad packet size, " 287 "neighbor ID %s", inet_ntoa(nbr->id)); 288 return; 289 } 290 break; 291 default: 292 fatalx("recv_ls_update: unknown neighbor state"); 293 } 294 } 295 296 /* link state retransmit list */ 297 void 298 ls_retrans_list_add(struct nbr *nbr, struct lsa_hdr *lsa, 299 unsigned short timeout, unsigned short oneshot) 300 { 301 struct timeval tv; 302 struct lsa_entry *le; 303 struct lsa_ref *ref; 304 305 if ((ref = lsa_cache_get(lsa)) == NULL) 306 fatalx("King Bula sez: somebody forgot to lsa_cache_add"); 307 308 if ((le = calloc(1, sizeof(*le))) == NULL) 309 fatal("ls_retrans_list_add"); 310 311 le->le_ref = ref; 312 le->le_when = timeout; 313 le->le_oneshot = oneshot; 314 315 ls_retrans_list_insert(nbr, le); 316 317 if (!evtimer_pending(&nbr->ls_retrans_timer, NULL)) { 318 timerclear(&tv); 319 tv.tv_sec = TAILQ_FIRST(&nbr->ls_retrans_list)->le_when; 320 321 if (evtimer_add(&nbr->ls_retrans_timer, &tv) == -1) 322 fatal("ls_retrans_list_add"); 323 } 324 } 325 326 int 327 ls_retrans_list_del(struct nbr *nbr, struct lsa_hdr *lsa_hdr) 328 { 329 struct lsa_entry *le; 330 331 if ((le = ls_retrans_list_get(nbr, lsa_hdr)) == NULL) 332 return (-1); 333 /* 334 * Compare LSA with the Ack by comparing not only the seq_num and 335 * checksum but also the age field. Since we only care about MAX_AGE 336 * vs. non-MAX_AGE LSA, a simple >= comparison is good enough. This 337 * ensures that a LSA withdrawal is not acked by a previous update. 338 */ 339 if (lsa_hdr->seq_num == le->le_ref->hdr.seq_num && 340 lsa_hdr->ls_chksum == le->le_ref->hdr.ls_chksum && 341 ntohs(lsa_hdr->age) >= ntohs(le->le_ref->hdr.age)) { 342 ls_retrans_list_free(nbr, le); 343 return (0); 344 } 345 346 return (-1); 347 } 348 349 struct lsa_entry * 350 ls_retrans_list_get(struct nbr *nbr, struct lsa_hdr *lsa_hdr) 351 { 352 struct lsa_entry *le; 353 354 TAILQ_FOREACH(le, &nbr->ls_retrans_list, entry) { 355 if ((lsa_hdr->type == le->le_ref->hdr.type) && 356 (lsa_hdr->ls_id == le->le_ref->hdr.ls_id) && 357 (lsa_hdr->adv_rtr == le->le_ref->hdr.adv_rtr)) 358 return (le); 359 } 360 return (NULL); 361 } 362 363 void 364 ls_retrans_list_insert(struct nbr *nbr, struct lsa_entry *new) 365 { 366 struct lsa_entry *le; 367 unsigned short when = new->le_when; 368 369 TAILQ_FOREACH(le, &nbr->ls_retrans_list, entry) { 370 if (when < le->le_when) { 371 new->le_when = when; 372 TAILQ_INSERT_BEFORE(le, new, entry); 373 nbr->ls_ret_cnt++; 374 return; 375 } 376 when -= le->le_when; 377 } 378 new->le_when = when; 379 TAILQ_INSERT_TAIL(&nbr->ls_retrans_list, new, entry); 380 nbr->ls_ret_cnt++; 381 } 382 383 void 384 ls_retrans_list_remove(struct nbr *nbr, struct lsa_entry *le) 385 { 386 struct timeval tv; 387 struct lsa_entry *next = TAILQ_NEXT(le, entry); 388 int reset = 0; 389 390 /* adjust timeout of next entry */ 391 if (next) 392 next->le_when += le->le_when; 393 394 if (TAILQ_FIRST(&nbr->ls_retrans_list) == le && 395 evtimer_pending(&nbr->ls_retrans_timer, NULL)) 396 reset = 1; 397 398 TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry); 399 nbr->ls_ret_cnt--; 400 401 if (reset && TAILQ_FIRST(&nbr->ls_retrans_list)) { 402 if (evtimer_del(&nbr->ls_retrans_timer) == -1) 403 fatal("ls_retrans_list_remove"); 404 405 timerclear(&tv); 406 tv.tv_sec = TAILQ_FIRST(&nbr->ls_retrans_list)->le_when; 407 408 if (evtimer_add(&nbr->ls_retrans_timer, &tv) == -1) 409 fatal("ls_retrans_list_remove"); 410 } 411 } 412 413 void 414 ls_retrans_list_free(struct nbr *nbr, struct lsa_entry *le) 415 { 416 ls_retrans_list_remove(nbr, le); 417 418 lsa_cache_put(le->le_ref, nbr); 419 free(le); 420 } 421 422 void 423 ls_retrans_list_clr(struct nbr *nbr) 424 { 425 struct lsa_entry *le; 426 427 while ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL) 428 ls_retrans_list_free(nbr, le); 429 430 nbr->ls_ret_cnt = 0; 431 } 432 433 /* ARGSUSED */ 434 void 435 ls_retrans_timer(int fd, short event, void *bula) 436 { 437 struct timeval tv; 438 struct timespec tp; 439 struct in6_addr addr; 440 struct nbr *nbr = bula; 441 struct lsa_entry *le; 442 struct ibuf *buf; 443 time_t now; 444 int d; 445 u_int32_t nlsa = 0; 446 447 if ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL) 448 le->le_when = 0; /* timer fired */ 449 else 450 return; /* queue empty, nothing to do */ 451 452 clock_gettime(CLOCK_MONOTONIC, &tp); 453 now = tp.tv_sec; 454 455 if (nbr->iface->self == nbr) { 456 /* 457 * oneshot needs to be set for lsa queued for flooding, 458 * if oneshot is not set then the lsa needs to be converted 459 * because the router switched lately to DR or BDR 460 */ 461 if (le->le_oneshot && nbr->iface->state & IF_STA_DRORBDR) 462 inet_pton(AF_INET6, AllSPFRouters, &addr); 463 else if (nbr->iface->state & IF_STA_DRORBDR) { 464 /* 465 * old retransmission needs to be converted into 466 * flood by rerunning the lsa_flood. 467 */ 468 lsa_flood(nbr->iface, nbr, &le->le_ref->hdr, 469 le->le_ref->data); 470 ls_retrans_list_free(nbr, le); 471 /* ls_retrans_list_free retriggers the timer */ 472 return; 473 } else if (nbr->iface->type == IF_TYPE_POINTOPOINT) 474 memcpy(&addr, &nbr->iface->dst, sizeof(addr)); 475 else 476 inet_pton(AF_INET6, AllDRouters, &addr); 477 } else 478 memcpy(&addr, &nbr->addr, sizeof(addr)); 479 480 if ((buf = prepare_ls_update(nbr->iface)) == NULL) { 481 le->le_when = 1; 482 goto done; 483 } 484 485 while ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL && 486 le->le_when == 0) { 487 d = now - le->le_ref->stamp; 488 if (d < 0) 489 d = 0; 490 else if (d > MAX_AGE) 491 d = MAX_AGE; 492 493 if (add_ls_update(buf, nbr->iface, le->le_ref->data, 494 le->le_ref->len, d) == 0) { 495 if (nlsa == 0) { 496 /* something bad happened retry later */ 497 log_warnx("ls_retrans_timer: sending LS update " 498 "to neighbor ID %s failed", 499 inet_ntoa(nbr->id)); 500 log_debug("ls_retrans_timer: type: %04x len: %u", 501 ntohs(le->le_ref->hdr.type), 502 le->le_ref->len); 503 TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry); 504 nbr->ls_ret_cnt--; 505 le->le_when = nbr->iface->rxmt_interval; 506 ls_retrans_list_insert(nbr, le); 507 } 508 break; 509 } 510 nlsa++; 511 if (le->le_oneshot) 512 ls_retrans_list_free(nbr, le); 513 else { 514 TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry); 515 nbr->ls_ret_cnt--; 516 le->le_when = nbr->iface->rxmt_interval; 517 ls_retrans_list_insert(nbr, le); 518 } 519 } 520 if (nlsa) 521 send_ls_update(buf, nbr->iface, addr, nlsa); 522 else 523 ibuf_free(buf); 524 525 done: 526 if ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL) { 527 timerclear(&tv); 528 tv.tv_sec = le->le_when; 529 530 if (evtimer_add(&nbr->ls_retrans_timer, &tv) == -1) 531 fatal("ls_retrans_timer"); 532 } 533 } 534 535 LIST_HEAD(lsa_cache_head, lsa_ref); 536 537 struct lsa_cache { 538 struct lsa_cache_head *hashtbl; 539 u_int32_t hashmask; 540 } lsacache; 541 542 SIPHASH_KEY lsacachekey; 543 544 struct lsa_ref *lsa_cache_look(struct lsa_hdr *); 545 546 void 547 lsa_cache_init(u_int32_t hashsize) 548 { 549 u_int32_t hs, i; 550 551 for (hs = 1; hs < hashsize; hs <<= 1) 552 ; 553 lsacache.hashtbl = calloc(hs, sizeof(struct lsa_cache_head)); 554 if (lsacache.hashtbl == NULL) 555 fatal("lsa_cache_init"); 556 557 for (i = 0; i < hs; i++) 558 LIST_INIT(&lsacache.hashtbl[i]); 559 arc4random_buf(&lsacachekey, sizeof(lsacachekey)); 560 561 lsacache.hashmask = hs - 1; 562 } 563 564 static uint32_t 565 lsa_hash_hdr(const struct lsa_hdr *hdr) 566 { 567 return SipHash24(&lsacachekey, hdr, sizeof(*hdr)); 568 } 569 570 struct lsa_ref * 571 lsa_cache_add(void *data, u_int16_t len) 572 { 573 struct lsa_cache_head *head; 574 struct lsa_ref *ref, *old; 575 struct timespec tp; 576 577 if ((ref = calloc(1, sizeof(*ref))) == NULL) 578 fatal("lsa_cache_add"); 579 memcpy(&ref->hdr, data, sizeof(ref->hdr)); 580 581 if ((old = lsa_cache_look(&ref->hdr))) { 582 free(ref); 583 old->refcnt++; 584 return (old); 585 } 586 587 if ((ref->data = malloc(len)) == NULL) 588 fatal("lsa_cache_add"); 589 memcpy(ref->data, data, len); 590 591 clock_gettime(CLOCK_MONOTONIC, &tp); 592 ref->stamp = tp.tv_sec; 593 ref->len = len; 594 ref->refcnt = 1; 595 596 head = &lsacache.hashtbl[lsa_hash_hdr(&ref->hdr) & lsacache.hashmask]; 597 LIST_INSERT_HEAD(head, ref, entry); 598 return (ref); 599 } 600 601 struct lsa_ref * 602 lsa_cache_get(struct lsa_hdr *lsa_hdr) 603 { 604 struct lsa_ref *ref; 605 606 ref = lsa_cache_look(lsa_hdr); 607 if (ref) 608 ref->refcnt++; 609 610 return (ref); 611 } 612 613 void 614 lsa_cache_put(struct lsa_ref *ref, struct nbr *nbr) 615 { 616 if (--ref->refcnt > 0) 617 return; 618 619 if (ntohs(ref->hdr.age) >= MAX_AGE) 620 ospfe_imsg_compose_rde(IMSG_LS_MAXAGE, nbr->peerid, 0, 621 ref->data, sizeof(struct lsa_hdr)); 622 623 free(ref->data); 624 LIST_REMOVE(ref, entry); 625 free(ref); 626 } 627 628 struct lsa_ref * 629 lsa_cache_look(struct lsa_hdr *lsa_hdr) 630 { 631 struct lsa_cache_head *head; 632 struct lsa_ref *ref; 633 634 head = &lsacache.hashtbl[lsa_hash_hdr(lsa_hdr) & lsacache.hashmask]; 635 636 LIST_FOREACH(ref, head, entry) { 637 if (memcmp(&ref->hdr, lsa_hdr, sizeof(*lsa_hdr)) == 0) 638 /* found match */ 639 return (ref); 640 } 641 642 return (NULL); 643 } 644