1 /* $OpenBSD: lsupdate.c,v 1.50 2021/11/03 21:40:03 sthen 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 /* ARGSUSED */ 434 void 435 ls_retrans_timer(int fd, short event, void *bula) 436 { 437 struct timeval tv; 438 struct timespec tp; 439 struct in_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_aton(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->addr, sizeof(addr)); 475 else 476 inet_aton(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 (%s) failed", 499 inet_ntoa(nbr->id), nbr->iface->name); 500 TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry); 501 nbr->ls_ret_cnt--; 502 le->le_when = nbr->iface->rxmt_interval; 503 ls_retrans_list_insert(nbr, le); 504 } 505 break; 506 } 507 nlsa++; 508 if (le->le_oneshot) 509 ls_retrans_list_free(nbr, le); 510 else { 511 TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry); 512 nbr->ls_ret_cnt--; 513 le->le_when = nbr->iface->rxmt_interval; 514 ls_retrans_list_insert(nbr, le); 515 } 516 } 517 if (nlsa) 518 send_ls_update(buf, nbr->iface, addr, nlsa); 519 else 520 ibuf_free(buf); 521 522 done: 523 if ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL) { 524 timerclear(&tv); 525 tv.tv_sec = le->le_when; 526 527 if (evtimer_add(&nbr->ls_retrans_timer, &tv) == -1) 528 fatal("ls_retrans_timer"); 529 } 530 } 531 532 LIST_HEAD(lsa_cache_head, lsa_ref); 533 534 struct lsa_cache { 535 struct lsa_cache_head *hashtbl; 536 u_int32_t hashmask; 537 } lsacache; 538 539 SIPHASH_KEY lsacachekey; 540 541 struct lsa_ref *lsa_cache_look(struct lsa_hdr *); 542 543 void 544 lsa_cache_init(u_int32_t hashsize) 545 { 546 u_int32_t hs, i; 547 548 for (hs = 1; hs < hashsize; hs <<= 1) 549 ; 550 lsacache.hashtbl = calloc(hs, sizeof(struct lsa_cache_head)); 551 if (lsacache.hashtbl == NULL) 552 fatal("lsa_cache_init"); 553 554 for (i = 0; i < hs; i++) 555 LIST_INIT(&lsacache.hashtbl[i]); 556 arc4random_buf(&lsacachekey, sizeof(lsacachekey)); 557 558 lsacache.hashmask = hs - 1; 559 } 560 561 static uint32_t 562 lsa_hash_hdr(const struct lsa_hdr *hdr) 563 { 564 return SipHash24(&lsacachekey, hdr, sizeof(*hdr)); 565 } 566 567 struct lsa_ref * 568 lsa_cache_add(void *data, u_int16_t len) 569 { 570 struct lsa_cache_head *head; 571 struct lsa_ref *ref, *old; 572 struct timespec tp; 573 574 if ((ref = calloc(1, sizeof(*ref))) == NULL) 575 fatal("lsa_cache_add"); 576 memcpy(&ref->hdr, data, sizeof(ref->hdr)); 577 578 if ((old = lsa_cache_look(&ref->hdr))) { 579 free(ref); 580 old->refcnt++; 581 return (old); 582 } 583 584 if ((ref->data = malloc(len)) == NULL) 585 fatal("lsa_cache_add"); 586 memcpy(ref->data, data, len); 587 588 clock_gettime(CLOCK_MONOTONIC, &tp); 589 ref->stamp = tp.tv_sec; 590 ref->len = len; 591 ref->refcnt = 1; 592 593 head = &lsacache.hashtbl[lsa_hash_hdr(&ref->hdr) & lsacache.hashmask]; 594 LIST_INSERT_HEAD(head, ref, entry); 595 return (ref); 596 } 597 598 struct lsa_ref * 599 lsa_cache_get(struct lsa_hdr *lsa_hdr) 600 { 601 struct lsa_ref *ref; 602 603 ref = lsa_cache_look(lsa_hdr); 604 if (ref) 605 ref->refcnt++; 606 607 return (ref); 608 } 609 610 void 611 lsa_cache_put(struct lsa_ref *ref, struct nbr *nbr) 612 { 613 if (--ref->refcnt > 0) 614 return; 615 616 if (ntohs(ref->hdr.age) >= MAX_AGE) 617 ospfe_imsg_compose_rde(IMSG_LS_MAXAGE, nbr->peerid, 0, 618 ref->data, sizeof(struct lsa_hdr)); 619 620 free(ref->data); 621 LIST_REMOVE(ref, entry); 622 free(ref); 623 } 624 625 struct lsa_ref * 626 lsa_cache_look(struct lsa_hdr *lsa_hdr) 627 { 628 struct lsa_cache_head *head; 629 struct lsa_ref *ref; 630 631 head = &lsacache.hashtbl[lsa_hash_hdr(lsa_hdr) & lsacache.hashmask]; 632 633 LIST_FOREACH(ref, head, entry) { 634 if (memcmp(&ref->hdr, lsa_hdr, sizeof(*lsa_hdr)) == 0) 635 /* found match */ 636 return (ref); 637 } 638 639 return (NULL); 640 } 641