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