From 43e70bb47b84874dbf5bedf42379b789cda2b076 Mon Sep 17 00:00:00 2001 From: claudio Date: Tue, 20 Jun 2023 15:19:55 +0000 Subject: [PATCH] Update ospfd to use new ibuf api. This mostly moves away from memcpy(ibuf_seek(buf, off, size), data, size) to ibuf_set(buf, off, data, size). Also ibuf_reserve() is replaced with ibuf_add_zero(). OK tb@ --- usr.sbin/ospfd/auth.c | 43 +++++++++++++++++++++++---------------- usr.sbin/ospfd/database.c | 9 ++++---- usr.sbin/ospfd/lsupdate.c | 15 +++++++------- usr.sbin/ospfd/ospfe.c | 37 +++++++++++++++++---------------- 4 files changed, 59 insertions(+), 45 deletions(-) diff --git a/usr.sbin/ospfd/auth.c b/usr.sbin/ospfd/auth.c index a50c3f0b9e4..204d4b33e6c 100644 --- a/usr.sbin/ospfd/auth.c +++ b/usr.sbin/ospfd/auth.c @@ -1,4 +1,4 @@ -/* $OpenBSD: auth.c,v 1.20 2015/05/05 01:26:37 jsg Exp $ */ +/* $OpenBSD: auth.c,v 1.21 2023/06/20 15:19:55 claudio Exp $ */ /* * Copyright (c) 2004, 2005 Esben Norby @@ -141,36 +141,45 @@ auth_gen(struct ibuf *buf, struct iface *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)); - - strncpy(ospf_hdr->auth_key.simple, iface->auth_key, - sizeof(ospf_hdr->auth_key.simple)); + 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"); + + 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, iface->auth_keyid)) == NULL) { diff --git a/usr.sbin/ospfd/database.c b/usr.sbin/ospfd/database.c index 17f855c261c..5fec976e819 100644 --- a/usr.sbin/ospfd/database.c +++ b/usr.sbin/ospfd/database.c @@ -1,4 +1,4 @@ -/* $OpenBSD: database.c,v 1.36 2023/03/08 04:43:14 guenther Exp $ */ +/* $OpenBSD: database.c,v 1.37 2023/06/20 15:19:55 claudio Exp $ */ /* * Copyright (c) 2005 Claudio Jeker @@ -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)) diff --git a/usr.sbin/ospfd/lsupdate.c b/usr.sbin/ospfd/lsupdate.c index 774e002b7b5..25c6b3ad350 100644 --- a/usr.sbin/ospfd/lsupdate.c +++ b/usr.sbin/ospfd/lsupdate.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lsupdate.c,v 1.51 2023/03/08 04:43:14 guenther Exp $ */ +/* $OpenBSD: lsupdate.c,v 1.52 2023/06/20 15:19:55 claudio Exp $ */ /* * Copyright (c) 2005 Claudio Jeker @@ -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 iface *iface, void *data, u_int16_t len, 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 iface *iface, struct in_addr addr, { 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; diff --git a/usr.sbin/ospfd/ospfe.c b/usr.sbin/ospfd/ospfe.c index ffc6faaa3dc..dda1090db38 100644 --- a/usr.sbin/ospfd/ospfe.c +++ b/usr.sbin/ospfd/ospfe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ospfe.c,v 1.111 2023/03/08 04:43:14 guenther Exp $ */ +/* $OpenBSD: ospfe.c,v 1.112 2023/06/20 15:19:55 claudio Exp $ */ /* * Copyright (c) 2005 Claudio Jeker @@ -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_n16 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_n16 failed"); imsg_compose_event(iev_rde, IMSG_LS_UPD, iface->self->peerid, 0, -1, buf->buf, ibuf_size(buf)); -- 2.20.1