From 35f7e862b336c6e78ca8e88fb9646edddd191065 Mon Sep 17 00:00:00 2001 From: claudio Date: Mon, 26 Jun 2023 10:08:56 +0000 Subject: [PATCH] Update and refactor dvrmpd to use the new ibu API. Do the checksum calculation in send_packet() instead of doing it all over the place. This way the fixup only happens in one place. OK tb@ --- usr.sbin/dvmrpd/ask_nbrs2.c | 9 ++------- usr.sbin/dvmrpd/dvmrpe.h | 5 +++-- usr.sbin/dvmrpd/graft.c | 9 ++------- usr.sbin/dvmrpd/graft_ack.c | 9 ++------- usr.sbin/dvmrpd/igmp.c | 7 ++----- usr.sbin/dvmrpd/nbrs2.c | 9 ++------- usr.sbin/dvmrpd/packet.c | 17 ++++++++++++++--- usr.sbin/dvmrpd/probe.c | 9 ++------- usr.sbin/dvmrpd/prune.c | 9 ++------- usr.sbin/dvmrpd/report.c | 13 ++++--------- 10 files changed, 35 insertions(+), 61 deletions(-) diff --git a/usr.sbin/dvmrpd/ask_nbrs2.c b/usr.sbin/dvmrpd/ask_nbrs2.c index 608f289f543..40b730b2ed2 100644 --- a/usr.sbin/dvmrpd/ask_nbrs2.c +++ b/usr.sbin/dvmrpd/ask_nbrs2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ask_nbrs2.c,v 1.5 2015/12/07 19:17:18 mmcc Exp $ */ +/* $OpenBSD: ask_nbrs2.c,v 1.6 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2006 Esben Norby @@ -36,7 +36,6 @@ send_ask_nbrs2(struct iface *iface, struct in_addr addr, void *data, int len) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_ask_nbrs2: interface %s addr %s", @@ -56,11 +55,7 @@ send_ask_nbrs2(struct iface *iface, struct in_addr addr, void *data, int len) dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: diff --git a/usr.sbin/dvmrpd/dvmrpe.h b/usr.sbin/dvmrpd/dvmrpe.h index 535c29b5577..ffba1ca5595 100644 --- a/usr.sbin/dvmrpd/dvmrpe.h +++ b/usr.sbin/dvmrpd/dvmrpe.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dvmrpe.h,v 1.7 2021/01/19 12:23:30 claudio Exp $ */ +/* $OpenBSD: dvmrpe.h,v 1.8 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2004, 2005, 2006 Esben Norby @@ -183,7 +183,8 @@ struct ctl_nbr *nbr_to_ctl(struct nbr *); /* packet.c */ int gen_dvmrp_hdr(struct ibuf *, struct iface *, u_int8_t); -int send_packet(struct iface *, void *, size_t, struct sockaddr_in *); +int send_packet(struct iface *, struct ibuf *, + struct sockaddr_in *); void recv_packet(int, short, void *); /* probe.c */ diff --git a/usr.sbin/dvmrpd/graft.c b/usr.sbin/dvmrpd/graft.c index 4340f6b0628..4545176a23f 100644 --- a/usr.sbin/dvmrpd/graft.c +++ b/usr.sbin/dvmrpd/graft.c @@ -1,4 +1,4 @@ -/* $OpenBSD: graft.c,v 1.5 2015/12/07 19:17:18 mmcc Exp $ */ +/* $OpenBSD: graft.c,v 1.6 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2005, 2006 Esben Norby @@ -36,7 +36,6 @@ send_graft(struct iface *iface, struct in_addr addr, void *data, int len) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_graft: interface %s addr %s", @@ -56,11 +55,7 @@ send_graft(struct iface *iface, struct in_addr addr, void *data, int len) dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: diff --git a/usr.sbin/dvmrpd/graft_ack.c b/usr.sbin/dvmrpd/graft_ack.c index 364fac101c8..c609c855506 100644 --- a/usr.sbin/dvmrpd/graft_ack.c +++ b/usr.sbin/dvmrpd/graft_ack.c @@ -1,4 +1,4 @@ -/* $OpenBSD: graft_ack.c,v 1.5 2015/12/07 19:17:18 mmcc Exp $ */ +/* $OpenBSD: graft_ack.c,v 1.6 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2006 Esben Norby @@ -36,7 +36,6 @@ send_graft_ack(struct iface *iface, struct in_addr addr, void *data, int len) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_graft_ack: interface %s addr %s", @@ -56,11 +55,7 @@ send_graft_ack(struct iface *iface, struct in_addr addr, void *data, int len) dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: diff --git a/usr.sbin/dvmrpd/igmp.c b/usr.sbin/dvmrpd/igmp.c index 90196710250..c1c2b93c10d 100644 --- a/usr.sbin/dvmrpd/igmp.c +++ b/usr.sbin/dvmrpd/igmp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: igmp.c,v 1.4 2015/12/07 19:14:49 mmcc Exp $ */ +/* $OpenBSD: igmp.c,v 1.5 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2005, 2006 Esben Norby @@ -73,9 +73,6 @@ send_igmp_query(struct iface *iface, struct group *group) igmp_hdr.max_resp_time = iface->last_member_query_interval; } - /* update chksum */ - igmp_hdr.chksum = in_cksum(&igmp_hdr, sizeof(igmp_hdr)); - ibuf_add(buf, &igmp_hdr, sizeof(igmp_hdr)); /* set destination address */ @@ -83,7 +80,7 @@ send_igmp_query(struct iface *iface, struct group *group) dst.sin_len = sizeof(struct sockaddr_in); inet_aton(AllSystems, &dst.sin_addr); - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); } diff --git a/usr.sbin/dvmrpd/nbrs2.c b/usr.sbin/dvmrpd/nbrs2.c index 5217bb03a11..0e362ebf4b7 100644 --- a/usr.sbin/dvmrpd/nbrs2.c +++ b/usr.sbin/dvmrpd/nbrs2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nbrs2.c,v 1.4 2015/05/05 01:26:37 jsg Exp $ */ +/* $OpenBSD: nbrs2.c,v 1.5 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2006 Esben Norby @@ -37,7 +37,6 @@ send_nbrs2(struct iface *iface, struct in_addr addr, void *data, int len) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_nbrs2: interface %s addr %s", @@ -57,11 +56,7 @@ send_nbrs2(struct iface *iface, struct in_addr addr, void *data, int len) dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: diff --git a/usr.sbin/dvmrpd/packet.c b/usr.sbin/dvmrpd/packet.c index 0a7dd6d7a3b..f0b22d6eff9 100644 --- a/usr.sbin/dvmrpd/packet.c +++ b/usr.sbin/dvmrpd/packet.c @@ -1,4 +1,4 @@ -/* $OpenBSD: packet.c,v 1.7 2021/01/19 16:02:56 claudio Exp $ */ +/* $OpenBSD: packet.c,v 1.8 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2004, 2005, 2006 Esben Norby @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -61,8 +62,10 @@ gen_dvmrp_hdr(struct ibuf *buf, struct iface *iface, u_int8_t code) /* send and receive packets */ int -send_packet(struct iface *iface, void *pkt, size_t len, struct sockaddr_in *dst) +send_packet(struct iface *iface, struct ibuf *pkt, struct sockaddr_in *dst) { + u_int16_t chksum; + if (iface->passive) { log_warnx("send_packet: cannot send packet on passive " "interface %s", iface->name); @@ -77,7 +80,15 @@ send_packet(struct iface *iface, void *pkt, size_t len, struct sockaddr_in *dst) return (-1); } - if (sendto(iface->fd, pkt, len, 0, + /* update chksum */ + chksum = in_cksum(ibuf_data(pkt), ibuf_size(pkt)); + if (ibuf_set(pkt, offsetof(struct dvmrp_hdr, chksum), + &chksum, sizeof(chksum)) == -1) { + log_warn("send_packet: failed to update checksum"); + return (-1); + } + + if (sendto(iface->fd, ibuf_data(pkt), ibuf_size(pkt), 0, (struct sockaddr *)dst, sizeof(*dst)) == -1 ) { log_warn("send_packet: error sending packet on interface %s", iface->name); diff --git a/usr.sbin/dvmrpd/probe.c b/usr.sbin/dvmrpd/probe.c index 0a6e02f31a5..98b803c8d4c 100644 --- a/usr.sbin/dvmrpd/probe.c +++ b/usr.sbin/dvmrpd/probe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: probe.c,v 1.4 2015/05/05 01:26:37 jsg Exp $ */ +/* $OpenBSD: probe.c,v 1.5 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2005, 2006 Esben Norby @@ -39,7 +39,6 @@ send_probe(struct iface *iface) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; struct nbr *nbr; int ret = 0; @@ -67,11 +66,7 @@ send_probe(struct iface *iface) dst.sin_len = sizeof(struct sockaddr_in); inet_aton(AllDVMRPRouters, &dst.sin_addr); - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: diff --git a/usr.sbin/dvmrpd/prune.c b/usr.sbin/dvmrpd/prune.c index ef9355c3ea9..c7c4ff64d55 100644 --- a/usr.sbin/dvmrpd/prune.c +++ b/usr.sbin/dvmrpd/prune.c @@ -1,4 +1,4 @@ -/* $OpenBSD: prune.c,v 1.6 2015/12/07 19:14:49 mmcc Exp $ */ +/* $OpenBSD: prune.c,v 1.7 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2005, 2006 Esben Norby @@ -37,7 +37,6 @@ send_prune(struct nbr *nbr, struct prune *p) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; struct prune_hdr prune; int ret = 0; @@ -69,11 +68,7 @@ send_prune(struct nbr *nbr, struct prune *p) ibuf_add(buf, &prune, sizeof(prune)); - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(nbr->iface, buf->buf, buf->wpos, &dst); + ret = send_packet(nbr->iface, buf, &dst); ibuf_free(buf); return (ret); diff --git a/usr.sbin/dvmrpd/report.c b/usr.sbin/dvmrpd/report.c index b56cda5132d..cc8ed3a3c07 100644 --- a/usr.sbin/dvmrpd/report.c +++ b/usr.sbin/dvmrpd/report.c @@ -1,4 +1,4 @@ -/* $OpenBSD: report.c,v 1.11 2015/12/07 18:59:31 mmcc Exp $ */ +/* $OpenBSD: report.c,v 1.12 2023/06/26 10:08:56 claudio Exp $ */ /* * Copyright (c) 2005, 2006 Esben Norby @@ -41,7 +41,6 @@ send_report(struct iface *iface, struct in_addr addr, void *data, int len) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_report: interface %s addr %s", @@ -63,11 +62,7 @@ send_report(struct iface *iface, struct in_addr addr, void *data, int len) dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: @@ -247,7 +242,7 @@ rr_list_send(struct rr_head *rr_list, struct iface *xiface, struct nbr *nbr) prefixlen = 0; while (((le = TAILQ_FIRST(rr_list)) != NULL) && - (buf->wpos < 1000)) { + (ibuf_size(buf) < 1000)) { /* netmask */ netmask = le->re->mask.s_addr; if (prefixlen != mask2prefixlen(netmask)) { @@ -288,7 +283,7 @@ rr_list_send(struct rr_head *rr_list, struct iface *xiface, struct nbr *nbr) rr_list_remove(le->re); free(le); } - send_report(iface, addr, buf->buf, buf->wpos); + send_report(iface, addr, ibuf_data(buf), ibuf_size(buf)); ibuf_free(buf); } } -- 2.20.1