This diff updates ospfd to use the new ibuf API. It mainly removes the use of ibuf_seek() and replaces these calls with ibuf_set().
Regress still passes with this diff in. -- :wq Claudio Index: auth.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/auth.c,v retrieving revision 1.20 diff -u -p -r1.20 auth.c --- auth.c 5 May 2015 01:26:37 -0000 1.20 +++ auth.c 16 Jun 2023 10:25:48 -0000 @@ -141,35 +141,44 @@ auth_gen(struct ibuf *buf, struct iface { MD5_CTX hash; u_int8_t digest[MD5_DIGEST_LENGTH]; - struct ospf_hdr *ospf_hdr; + struct crypt crypt; struct auth_md *md; - - if ((ospf_hdr = ibuf_seek(buf, 0, sizeof(*ospf_hdr))) == NULL) - fatalx("auth_gen: buf_seek failed"); + u_int16_t chksum; /* update length */ if (ibuf_size(buf) > USHRT_MAX) fatalx("auth_gen: resulting ospf packet too big"); - ospf_hdr->len = htons(ibuf_size(buf)); - /* clear auth_key field */ - bzero(ospf_hdr->auth_key.simple, sizeof(ospf_hdr->auth_key.simple)); + if (ibuf_set_n16(buf, offsetof(struct ospf_hdr, len), + ibuf_size(buf)) == -1) + fatalx("auth_gen: ibuf_set_n16 failed"); switch (iface->auth_type) { case AUTH_NONE: - ospf_hdr->chksum = in_cksum(buf->buf, ibuf_size(buf)); + chksum = in_cksum(buf->buf, ibuf_size(buf)); + if (ibuf_set(buf, offsetof(struct ospf_hdr, chksum), + &chksum, sizeof(chksum)) == -1) + fatalx("auth_gen: ibuf_set failed"); break; case AUTH_SIMPLE: - ospf_hdr->chksum = in_cksum(buf->buf, ibuf_size(buf)); + chksum = in_cksum(buf->buf, ibuf_size(buf)); + if (ibuf_set(buf, offsetof(struct ospf_hdr, chksum), + &chksum, sizeof(chksum)) == -1) + fatalx("auth_gen: ibuf_set failed"); - strncpy(ospf_hdr->auth_key.simple, iface->auth_key, - sizeof(ospf_hdr->auth_key.simple)); + if (ibuf_set(buf, offsetof(struct ospf_hdr, auth_key), + iface->auth_key, strlen(iface->auth_key)) == -1) + fatalx("auth_gen: ibuf_set failed"); break; case AUTH_CRYPT: - ospf_hdr->chksum = 0; - ospf_hdr->auth_key.crypt.keyid = iface->auth_keyid; - ospf_hdr->auth_key.crypt.seq_num = htonl(iface->crypt_seq_num); - ospf_hdr->auth_key.crypt.len = MD5_DIGEST_LENGTH; + bzero(&crypt, sizeof(crypt)); + crypt.keyid = iface->auth_keyid; + crypt.seq_num = htonl(iface->crypt_seq_num); + crypt.len = MD5_DIGEST_LENGTH; iface->crypt_seq_num++; + + if (ibuf_set(buf, offsetof(struct ospf_hdr, auth_key), + &crypt, sizeof(crypt)) == -1) + fatalx("auth_gen: ibuf_set failed"); /* insert plaintext key */ if ((md = md_list_find(&iface->auth_md_list, Index: database.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/database.c,v retrieving revision 1.36 diff -u -p -r1.36 database.c --- database.c 8 Mar 2023 04:43:14 -0000 1.36 +++ database.c 16 Jun 2023 10:26:00 -0000 @@ -53,7 +53,7 @@ send_db_description(struct nbr *nbr) goto fail; /* reserve space for database description header */ - if (ibuf_reserve(buf, sizeof(dd_hdr)) == NULL) + if (ibuf_add_zero(buf, sizeof(dd_hdr)) == -1) goto fail; switch (nbr->state) { @@ -140,8 +140,9 @@ send_db_description(struct nbr *nbr) dd_hdr.bits = bits; dd_hdr.dd_seq_num = htonl(nbr->dd_seq_num); - memcpy(ibuf_seek(buf, sizeof(struct ospf_hdr), sizeof(dd_hdr)), - &dd_hdr, sizeof(dd_hdr)); + if (ibuf_set(buf, sizeof(struct ospf_hdr), &dd_hdr, + sizeof(dd_hdr)) == -1) + goto fail; /* update authentication and calculate checksum */ if (auth_gen(buf, nbr->iface)) Index: lsupdate.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/lsupdate.c,v retrieving revision 1.51 diff -u -p -r1.51 lsupdate.c --- lsupdate.c 8 Mar 2023 04:43:14 -0000 1.51 +++ lsupdate.c 16 Jun 2023 10:26:17 -0000 @@ -158,7 +158,7 @@ prepare_ls_update(struct iface *iface) goto fail; /* reserve space for number of lsa field */ - if (ibuf_reserve(buf, sizeof(u_int32_t)) == NULL) + if (ibuf_add_zero(buf, sizeof(u_int32_t)) == -1) goto fail; return (buf); @@ -194,8 +194,10 @@ add_ls_update(struct ibuf *buf, struct i age = ntohs(age); if ((age += older + iface->transmit_delay) >= MAX_AGE) age = MAX_AGE; - age = htons(age); - memcpy(ibuf_seek(buf, ageoff, sizeof(age)), &age, sizeof(age)); + if (ibuf_set_n16(buf, ageoff, age) == -1) { + log_warn("add_ls_update"); + return (0); + } return (1); } @@ -208,9 +210,8 @@ send_ls_update(struct ibuf *buf, struct { struct sockaddr_in dst; - nlsa = htonl(nlsa); - memcpy(ibuf_seek(buf, sizeof(struct ospf_hdr), sizeof(nlsa)), - &nlsa, sizeof(nlsa)); + if (ibuf_set_n32(buf, sizeof(struct ospf_hdr), nlsa) == -1) + goto fail; /* update authentication and calculate checksum */ if (auth_gen(buf, iface)) goto fail; Index: ospfe.c =================================================================== RCS file: /cvs/src/usr.sbin/ospfd/ospfe.c,v retrieving revision 1.111 diff -u -p -r1.111 ospfe.c --- ospfe.c 8 Mar 2023 04:43:14 -0000 1.111 +++ ospfe.c 16 Jun 2023 15:06:04 -0000 @@ -842,11 +842,11 @@ orig_rtr_lsa(struct area *area) fatal("orig_rtr_lsa"); /* reserve space for LSA header and LSA Router header */ - if (ibuf_reserve(buf, sizeof(lsa_hdr)) == NULL) - fatal("orig_rtr_lsa: ibuf_reserve failed"); + if (ibuf_add_zero(buf, sizeof(lsa_hdr)) == -1) + fatal("orig_rtr_lsa: ibuf_add_zero failed"); - if (ibuf_reserve(buf, sizeof(lsa_rtr)) == NULL) - fatal("orig_rtr_lsa: ibuf_reserve failed"); + if (ibuf_add_zero(buf, sizeof(lsa_rtr)) == -1) + fatal("orig_rtr_lsa: ibuf_add_zero failed"); /* links */ LIST_FOREACH(iface, &area->iface_list, entry) { @@ -1083,8 +1083,9 @@ orig_rtr_lsa(struct area *area) lsa_rtr.dummy = 0; lsa_rtr.nlinks = htons(num_links); - memcpy(ibuf_seek(buf, sizeof(lsa_hdr), sizeof(lsa_rtr)), - &lsa_rtr, sizeof(lsa_rtr)); + if (ibuf_set(buf, sizeof(lsa_hdr), &lsa_rtr, sizeof(lsa_rtr)) == + -1) + fatal("orig_rtr_lsa: ibuf_set failed"); /* LSA header */ lsa_hdr.age = htons(DEFAULT_AGE); @@ -1095,11 +1096,12 @@ orig_rtr_lsa(struct area *area) lsa_hdr.seq_num = htonl(INIT_SEQ_NUM); lsa_hdr.len = htons(ibuf_size(buf)); lsa_hdr.ls_chksum = 0; /* updated later */ - memcpy(ibuf_seek(buf, 0, sizeof(lsa_hdr)), &lsa_hdr, sizeof(lsa_hdr)); + if (ibuf_set(buf, 0, &lsa_hdr, sizeof(lsa_hdr)) == -1) + fatal("orig_rtr_lsa: ibuf_set failed"); - chksum = htons(iso_cksum(buf->buf, ibuf_size(buf), LS_CKSUM_OFFSET)); - memcpy(ibuf_seek(buf, LS_CKSUM_OFFSET, sizeof(chksum)), - &chksum, sizeof(chksum)); + chksum = iso_cksum(buf->buf, ibuf_size(buf), LS_CKSUM_OFFSET); + if (ibuf_set_n16(buf, LS_CKSUM_OFFSET, chksum) == -1) + fatal("orig_rtr_lsa: ibuf_set failed"); if (self && num_links) imsg_compose_event(iev_rde, IMSG_LS_UPD, self->peerid, 0, @@ -1126,8 +1128,8 @@ orig_net_lsa(struct iface *iface) fatal("orig_net_lsa"); /* reserve space for LSA header and LSA Router header */ - if (ibuf_reserve(buf, sizeof(lsa_hdr)) == NULL) - fatal("orig_net_lsa: ibuf_reserve failed"); + if (ibuf_add_zero(buf, sizeof(lsa_hdr)) == -1) + fatal("orig_net_lsa: ibuf_add_zero failed"); /* LSA net mask and then all fully adjacent routers */ if (ibuf_add(buf, &iface->mask, sizeof(iface->mask))) @@ -1160,11 +1162,12 @@ orig_net_lsa(struct iface *iface) lsa_hdr.seq_num = htonl(INIT_SEQ_NUM); lsa_hdr.len = htons(ibuf_size(buf)); lsa_hdr.ls_chksum = 0; /* updated later */ - memcpy(ibuf_seek(buf, 0, sizeof(lsa_hdr)), &lsa_hdr, sizeof(lsa_hdr)); + if (ibuf_set(buf, 0, &lsa_hdr, sizeof(lsa_hdr)) == -1) + fatal("orig_net_lsa: ibuf_set failed"); - chksum = htons(iso_cksum(buf->buf, ibuf_size(buf), LS_CKSUM_OFFSET)); - memcpy(ibuf_seek(buf, LS_CKSUM_OFFSET, sizeof(chksum)), - &chksum, sizeof(chksum)); + chksum = iso_cksum(buf->buf, ibuf_size(buf), LS_CKSUM_OFFSET); + if (ibuf_set_n16(buf, LS_CKSUM_OFFSET, chksum) == -1) + fatal("orig_net_lsa: ibuf_set failed"); imsg_compose_event(iev_rde, IMSG_LS_UPD, iface->self->peerid, 0, -1, buf->buf, ibuf_size(buf));