1 /* $OpenBSD: lsupdate.c,v 1.51 2023/03/08 04:43:14 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2004, 2005 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 <arpa/inet.h> 24 25 #include <stdlib.h> 26 #include <string.h> 27 #include <siphash.h> 28 29 #include "ospf.h" 30 #include "ospfd.h" 31 #include "log.h" 32 #include "ospfe.h" 33 #include "rde.h" 34 35 struct ibuf *prepare_ls_update(struct iface *); 36 int add_ls_update(struct ibuf *, struct iface *, void *, u_int16_t, 37 u_int16_t); 38 int send_ls_update(struct ibuf *, struct iface *, struct in_addr, u_int32_t); 39 40 void ls_retrans_list_insert(struct nbr *, struct lsa_entry *); 41 void ls_retrans_list_remove(struct nbr *, struct lsa_entry *); 42 43 /* link state update packet handling */ 44 int 45 lsa_flood(struct iface *iface, struct nbr *originator, struct lsa_hdr *lsa_hdr, 46 void *data) 47 { 48 struct nbr *nbr; 49 struct lsa_entry *le = NULL; 50 int queued = 0, dont_ack = 0; 51 int r; 52 53 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 54 if (nbr == iface->self) 55 continue; 56 if (!(nbr->state & NBR_STA_FLOOD)) 57 continue; 58 59 if (iface->state & IF_STA_DROTHER && !queued) 60 while ((le = ls_retrans_list_get(iface->self, lsa_hdr))) 61 ls_retrans_list_free(iface->self, le); 62 63 while ((le = ls_retrans_list_get(nbr, lsa_hdr))) 64 ls_retrans_list_free(nbr, le); 65 66 if (!(nbr->state & NBR_STA_FULL) && 67 (le = ls_req_list_get(nbr, lsa_hdr)) != NULL) { 68 r = lsa_newer(lsa_hdr, le->le_lsa); 69 if (r > 0) { 70 /* to flood LSA is newer than requested */ 71 ls_req_list_free(nbr, le); 72 /* new needs to be flooded */ 73 } else if (r < 0) { 74 /* to flood LSA is older than requested */ 75 continue; 76 } else { 77 /* LSA are equal */ 78 ls_req_list_free(nbr, le); 79 continue; 80 } 81 } 82 83 if (nbr == originator) { 84 dont_ack++; 85 continue; 86 } 87 88 /* non DR or BDR router keep all lsa in one retrans list */ 89 if (iface->state & IF_STA_DROTHER) { 90 if (!queued) 91 ls_retrans_list_add(iface->self, data, 92 iface->rxmt_interval, 0); 93 queued = 1; 94 } else { 95 ls_retrans_list_add(nbr, data, iface->rxmt_interval, 0); 96 queued = 1; 97 } 98 } 99 100 if (!queued) 101 return (0); 102 103 if (iface == originator->iface && iface->self != originator) { 104 if (iface->dr == originator || iface->bdr == originator) 105 return (0); 106 if (iface->state & IF_STA_BACKUP) 107 return (0); 108 dont_ack++; 109 } 110 111 /* 112 * initial flood needs to be queued separately, timeout is zero 113 * and oneshot has to be set because the retransimssion queues 114 * are already loaded. 115 */ 116 switch (iface->type) { 117 case IF_TYPE_POINTOPOINT: 118 case IF_TYPE_BROADCAST: 119 ls_retrans_list_add(iface->self, data, 0, 1); 120 break; 121 case IF_TYPE_NBMA: 122 case IF_TYPE_POINTOMULTIPOINT: 123 case IF_TYPE_VIRTUALLINK: 124 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 125 if (nbr == iface->self) 126 continue; 127 if (!(nbr->state & NBR_STA_FLOOD)) 128 continue; 129 if (!TAILQ_EMPTY(&nbr->ls_retrans_list)) { 130 le = TAILQ_LAST(&nbr->ls_retrans_list, 131 lsa_head); 132 if (lsa_hdr->type != le->le_lsa->type || 133 lsa_hdr->ls_id != le->le_lsa->ls_id || 134 lsa_hdr->adv_rtr != le->le_lsa->adv_rtr) 135 continue; 136 } 137 ls_retrans_list_add(nbr, data, 0, 1); 138 } 139 break; 140 default: 141 fatalx("lsa_flood: unknown interface type"); 142 } 143 144 return (dont_ack == 2); 145 } 146 147 struct ibuf * 148 prepare_ls_update(struct iface *iface) 149 { 150 struct ibuf *buf; 151 152 if ((buf = ibuf_dynamic(iface->mtu - sizeof(struct ip), 153 IP_MAXPACKET - sizeof(struct ip))) == NULL) 154 fatal("prepare_ls_update"); 155 156 /* OSPF header */ 157 if (gen_ospf_hdr(buf, iface, PACKET_TYPE_LS_UPDATE)) 158 goto fail; 159 160 /* reserve space for number of lsa field */ 161 if (ibuf_reserve(buf, sizeof(u_int32_t)) == NULL) 162 goto fail; 163 164 return (buf); 165 fail: 166 log_warn("prepare_ls_update"); 167 ibuf_free(buf); 168 return (NULL); 169 } 170 171 int 172 add_ls_update(struct ibuf *buf, struct iface *iface, void *data, u_int16_t len, 173 u_int16_t older) 174 { 175 size_t ageoff; 176 u_int16_t age; 177 178 if ((size_t)iface->mtu < sizeof(struct ip) + sizeof(struct ospf_hdr) + 179 sizeof(u_int32_t) + ibuf_size(buf) + len + MD5_DIGEST_LENGTH) { 180 /* start new packet unless this is the first LSA to pack */ 181 if (ibuf_size(buf) > sizeof(struct ospf_hdr) + 182 sizeof(u_int32_t)) 183 return (0); 184 } 185 186 ageoff = ibuf_size(buf); 187 if (ibuf_add(buf, data, len)) { 188 log_warn("add_ls_update"); 189 return (0); 190 } 191 192 /* age LSA before sending it out */ 193 memcpy(&age, data, sizeof(age)); 194 age = ntohs(age); 195 if ((age += older + iface->transmit_delay) >= MAX_AGE) 196 age = MAX_AGE; 197 age = htons(age); 198 memcpy(ibuf_seek(buf, ageoff, sizeof(age)), &age, sizeof(age)); 199 200 return (1); 201 } 202 203 204 205 int 206 send_ls_update(struct ibuf *buf, struct iface *iface, struct in_addr addr, 207 u_int32_t nlsa) 208 { 209 struct sockaddr_in dst; 210 211 nlsa = htonl(nlsa); 212 memcpy(ibuf_seek(buf, sizeof(struct ospf_hdr), sizeof(nlsa)), 213 &nlsa, sizeof(nlsa)); 214 /* update authentication and calculate checksum */ 215 if (auth_gen(buf, iface)) 216 goto fail; 217 218 /* set destination */ 219 dst.sin_family = AF_INET; 220 dst.sin_len = sizeof(struct sockaddr_in); 221 dst.sin_addr.s_addr = addr.s_addr; 222 223 if (send_packet(iface, buf, &dst) == -1) 224 goto fail; 225 226 ibuf_free(buf); 227 return (0); 228 fail: 229 log_warn("%s", __func__); 230 ibuf_free(buf); 231 return (-1); 232 } 233 234 void 235 recv_ls_update(struct nbr *nbr, char *buf, u_int16_t len) 236 { 237 struct lsa_hdr lsa; 238 u_int32_t nlsa; 239 240 if (len < sizeof(nlsa)) { 241 log_warnx("recv_ls_update: bad packet size, " 242 "neighbor ID %s (%s)", inet_ntoa(nbr->id), 243 nbr->iface->name); 244 return; 245 } 246 memcpy(&nlsa, buf, sizeof(nlsa)); 247 nlsa = ntohl(nlsa); 248 buf += sizeof(nlsa); 249 len -= sizeof(nlsa); 250 251 switch (nbr->state) { 252 case NBR_STA_DOWN: 253 case NBR_STA_ATTEMPT: 254 case NBR_STA_INIT: 255 case NBR_STA_2_WAY: 256 case NBR_STA_XSTRT: 257 case NBR_STA_SNAP: 258 log_debug("recv_ls_update: packet ignored in state %s, " 259 "neighbor ID %s (%s)", nbr_state_name(nbr->state), 260 inet_ntoa(nbr->id), nbr->iface->name); 261 break; 262 case NBR_STA_XCHNG: 263 case NBR_STA_LOAD: 264 case NBR_STA_FULL: 265 for (; nlsa > 0 && len > 0; nlsa--) { 266 if (len < sizeof(lsa)) { 267 log_warnx("recv_ls_update: bad packet size, " 268 "neighbor ID %s (%s)", inet_ntoa(nbr->id), 269 nbr->iface->name); 270 return; 271 } 272 memcpy(&lsa, buf, sizeof(lsa)); 273 if (len < ntohs(lsa.len)) { 274 log_warnx("recv_ls_update: bad packet size, " 275 "neighbor ID %s (%s)", inet_ntoa(nbr->id), 276 nbr->iface->name); 277 return; 278 } 279 ospfe_imsg_compose_rde(IMSG_LS_UPD, nbr->peerid, 0, 280 buf, ntohs(lsa.len)); 281 buf += ntohs(lsa.len); 282 len -= ntohs(lsa.len); 283 } 284 if (nlsa > 0 || len > 0) { 285 log_warnx("recv_ls_update: bad packet size, " 286 "neighbor ID %s (%s)", inet_ntoa(nbr->id), 287 nbr->iface->name); 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 void 434 ls_retrans_timer(int fd, short event, void *bula) 435 { 436 struct timeval tv; 437 struct timespec tp; 438 struct in_addr addr; 439 struct nbr *nbr = bula; 440 struct lsa_entry *le; 441 struct ibuf *buf; 442 time_t now; 443 int d; 444 u_int32_t nlsa = 0; 445 446 if ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL) 447 le->le_when = 0; /* timer fired */ 448 else 449 return; /* queue empty, nothing to do */ 450 451 clock_gettime(CLOCK_MONOTONIC, &tp); 452 now = tp.tv_sec; 453 454 if (nbr->iface->self == nbr) { 455 /* 456 * oneshot needs to be set for lsa queued for flooding, 457 * if oneshot is not set then the lsa needs to be converted 458 * because the router switched lately to DR or BDR 459 */ 460 if (le->le_oneshot && nbr->iface->state & IF_STA_DRORBDR) 461 inet_aton(AllSPFRouters, &addr); 462 else if (nbr->iface->state & IF_STA_DRORBDR) { 463 /* 464 * old retransmission needs to be converted into 465 * flood by rerunning the lsa_flood. 466 */ 467 lsa_flood(nbr->iface, nbr, &le->le_ref->hdr, 468 le->le_ref->data); 469 ls_retrans_list_free(nbr, le); 470 /* ls_retrans_list_free retriggers the timer */ 471 return; 472 } else if (nbr->iface->type == IF_TYPE_POINTOPOINT) 473 memcpy(&addr, &nbr->addr, sizeof(addr)); 474 else 475 inet_aton(AllDRouters, &addr); 476 } else 477 memcpy(&addr, &nbr->addr, sizeof(addr)); 478 479 if ((buf = prepare_ls_update(nbr->iface)) == NULL) { 480 le->le_when = 1; 481 goto done; 482 } 483 484 while ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL && 485 le->le_when == 0) { 486 d = now - le->le_ref->stamp; 487 if (d < 0) 488 d = 0; 489 else if (d > MAX_AGE) 490 d = MAX_AGE; 491 492 if (add_ls_update(buf, nbr->iface, le->le_ref->data, 493 le->le_ref->len, d) == 0) { 494 if (nlsa == 0) { 495 /* something bad happened retry later */ 496 log_warnx("ls_retrans_timer: sending LS update " 497 "to neighbor ID %s (%s) failed", 498 inet_ntoa(nbr->id), nbr->iface->name); 499 TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry); 500 nbr->ls_ret_cnt--; 501 le->le_when = nbr->iface->rxmt_interval; 502 ls_retrans_list_insert(nbr, le); 503 } 504 break; 505 } 506 nlsa++; 507 if (le->le_oneshot) 508 ls_retrans_list_free(nbr, le); 509 else { 510 TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry); 511 nbr->ls_ret_cnt--; 512 le->le_when = nbr->iface->rxmt_interval; 513 ls_retrans_list_insert(nbr, le); 514 } 515 } 516 if (nlsa) 517 send_ls_update(buf, nbr->iface, addr, nlsa); 518 else 519 ibuf_free(buf); 520 521 done: 522 if ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL) { 523 timerclear(&tv); 524 tv.tv_sec = le->le_when; 525 526 if (evtimer_add(&nbr->ls_retrans_timer, &tv) == -1) 527 fatal("ls_retrans_timer"); 528 } 529 } 530 531 LIST_HEAD(lsa_cache_head, lsa_ref); 532 533 struct lsa_cache { 534 struct lsa_cache_head *hashtbl; 535 u_int32_t hashmask; 536 } lsacache; 537 538 SIPHASH_KEY lsacachekey; 539 540 struct lsa_ref *lsa_cache_look(struct lsa_hdr *); 541 542 void 543 lsa_cache_init(u_int32_t hashsize) 544 { 545 u_int32_t hs, i; 546 547 for (hs = 1; hs < hashsize; hs <<= 1) 548 ; 549 lsacache.hashtbl = calloc(hs, sizeof(struct lsa_cache_head)); 550 if (lsacache.hashtbl == NULL) 551 fatal("lsa_cache_init"); 552 553 for (i = 0; i < hs; i++) 554 LIST_INIT(&lsacache.hashtbl[i]); 555 arc4random_buf(&lsacachekey, sizeof(lsacachekey)); 556 557 lsacache.hashmask = hs - 1; 558 } 559 560 static uint32_t 561 lsa_hash_hdr(const struct lsa_hdr *hdr) 562 { 563 return SipHash24(&lsacachekey, hdr, sizeof(*hdr)); 564 } 565 566 struct lsa_ref * 567 lsa_cache_add(void *data, u_int16_t len) 568 { 569 struct lsa_cache_head *head; 570 struct lsa_ref *ref, *old; 571 struct timespec tp; 572 573 if ((ref = calloc(1, sizeof(*ref))) == NULL) 574 fatal("lsa_cache_add"); 575 memcpy(&ref->hdr, data, sizeof(ref->hdr)); 576 577 if ((old = lsa_cache_look(&ref->hdr))) { 578 free(ref); 579 old->refcnt++; 580 return (old); 581 } 582 583 if ((ref->data = malloc(len)) == NULL) 584 fatal("lsa_cache_add"); 585 memcpy(ref->data, data, len); 586 587 clock_gettime(CLOCK_MONOTONIC, &tp); 588 ref->stamp = tp.tv_sec; 589 ref->len = len; 590 ref->refcnt = 1; 591 592 head = &lsacache.hashtbl[lsa_hash_hdr(&ref->hdr) & lsacache.hashmask]; 593 LIST_INSERT_HEAD(head, ref, entry); 594 return (ref); 595 } 596 597 struct lsa_ref * 598 lsa_cache_get(struct lsa_hdr *lsa_hdr) 599 { 600 struct lsa_ref *ref; 601 602 ref = lsa_cache_look(lsa_hdr); 603 if (ref) 604 ref->refcnt++; 605 606 return (ref); 607 } 608 609 void 610 lsa_cache_put(struct lsa_ref *ref, struct nbr *nbr) 611 { 612 if (--ref->refcnt > 0) 613 return; 614 615 if (ntohs(ref->hdr.age) >= MAX_AGE) 616 ospfe_imsg_compose_rde(IMSG_LS_MAXAGE, nbr->peerid, 0, 617 ref->data, sizeof(struct lsa_hdr)); 618 619 free(ref->data); 620 LIST_REMOVE(ref, entry); 621 free(ref); 622 } 623 624 struct lsa_ref * 625 lsa_cache_look(struct lsa_hdr *lsa_hdr) 626 { 627 struct lsa_cache_head *head; 628 struct lsa_ref *ref; 629 630 head = &lsacache.hashtbl[lsa_hash_hdr(lsa_hdr) & lsacache.hashmask]; 631 632 LIST_FOREACH(ref, head, entry) { 633 if (memcmp(&ref->hdr, lsa_hdr, sizeof(*lsa_hdr)) == 0) 634 /* found match */ 635 return (ref); 636 } 637 638 return (NULL); 639 } 640