From 46d5331a5690e9580ab458fc7f641fc56e426f38 Mon Sep 17 00:00:00 2001 From: claudio Date: Tue, 30 Jan 2024 13:51:13 +0000 Subject: [PATCH] Adjust bgpctl to work with the modified aspath functions from util.c While doing that convert IMSG_CTL_SHOW_RIB over to the new ibuf api. OK tb@ --- usr.sbin/bgpctl/bgpctl.c | 21 ++++++++---------- usr.sbin/bgpctl/bgpctl.h | 4 ++-- usr.sbin/bgpctl/output.c | 41 +++++++++++++++++------------------ usr.sbin/bgpctl/output_json.c | 31 +++++++++++++------------- 4 files changed, 47 insertions(+), 50 deletions(-) diff --git a/usr.sbin/bgpctl/bgpctl.c b/usr.sbin/bgpctl/bgpctl.c index 014b3000a55..22032bf2f36 100644 --- a/usr.sbin/bgpctl/bgpctl.c +++ b/usr.sbin/bgpctl/bgpctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpctl.c,v 1.302 2024/01/25 09:54:21 claudio Exp $ */ +/* $OpenBSD: bgpctl.c,v 1.303 2024/01/30 13:51:13 claudio Exp $ */ /* * Copyright (c) 2003 Henning Brauer @@ -471,9 +471,7 @@ show(struct imsg *imsg, struct parse_result *res) struct ctl_show_rib rib; struct rde_memstats stats; struct ibuf ibuf; - u_char *asdata; u_int rescode, ilen; - size_t aslen; switch (imsg->hdr.type) { case IMSG_CTL_SHOW_NEIGHBOR: @@ -528,16 +526,13 @@ show(struct imsg *imsg, struct parse_result *res) output->fib_table(&kt); break; case IMSG_CTL_SHOW_RIB: - if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(rib)) - errx(1, "wrong imsg len"); if (output->rib == NULL) break; - /* XXX */ - memcpy(&rib, imsg->data, sizeof(rib)); - aslen = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof(rib); - asdata = imsg->data; - asdata += sizeof(rib); - output->rib(&rib, asdata, aslen, res); + if (imsg_get_ibuf(imsg, &ibuf) == -1) + err(1, "imsg_get_ibuf"); + if (ibuf_get(&ibuf, &rib, sizeof(rib)) == -1) + err(1, "imsg_get_ibuf"); + output->rib(&rib, &ibuf, res); break; case IMSG_CTL_SHOW_RIB_COMMUNITIES: if (output->communities == NULL) @@ -1231,6 +1226,7 @@ show_mrt_dump(struct mrt_rib *mr, struct mrt_peer *mp, void *arg) struct parse_result res; struct ctl_show_rib_request *req = arg; struct mrt_rib_entry *mre; + struct ibuf ibuf; time_t now; uint16_t i, j; @@ -1296,7 +1292,8 @@ show_mrt_dump(struct mrt_rib *mr, struct mrt_peer *mp, void *arg) !match_aspath(mre->aspath, mre->aspath_len, &req->as)) continue; - output->rib(&ctl, mre->aspath, mre->aspath_len, &res); + ibuf_from_buffer(&ibuf, mre->aspath, mre->aspath_len); + output->rib(&ctl, &ibuf, &res); if (req->flags & F_CTL_DETAIL) { for (j = 0; j < mre->nattrs; j++) output->attr(mre->attrs[j].attr, diff --git a/usr.sbin/bgpctl/bgpctl.h b/usr.sbin/bgpctl/bgpctl.h index db9e206c17e..4ec108688a1 100644 --- a/usr.sbin/bgpctl/bgpctl.h +++ b/usr.sbin/bgpctl/bgpctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpctl.h,v 1.22 2024/01/25 09:54:21 claudio Exp $ */ +/* $OpenBSD: bgpctl.h,v 1.23 2024/01/30 13:51:13 claudio Exp $ */ /* * Copyright (c) 2019 Claudio Jeker @@ -29,7 +29,7 @@ struct output { void (*interface)(struct ctl_show_interface *); void (*attr)(u_char *, size_t, int, int); void (*communities)(struct ibuf *, struct parse_result *); - void (*rib)(struct ctl_show_rib *, u_char *, size_t, + void (*rib)(struct ctl_show_rib *, struct ibuf *, struct parse_result *); void (*rib_mem)(struct rde_memstats *); void (*set)(struct ctl_show_set *); diff --git a/usr.sbin/bgpctl/output.c b/usr.sbin/bgpctl/output.c index 3a5e59cf4ee..05f151a0ffa 100644 --- a/usr.sbin/bgpctl/output.c +++ b/usr.sbin/bgpctl/output.c @@ -1,4 +1,4 @@ -/* $OpenBSD: output.c,v 1.48 2024/01/25 09:54:21 claudio Exp $ */ +/* $OpenBSD: output.c,v 1.49 2024/01/30 13:51:13 claudio Exp $ */ /* * Copyright (c) 2003 Henning Brauer @@ -769,10 +769,9 @@ show_ext_community(u_char *data, uint16_t len) static void show_attr(u_char *data, size_t len, int reqflags, int addpath) { - u_char *path; struct in_addr id; struct bgpd_addr prefix; - struct ibuf ibuf, *buf = &ibuf; + struct ibuf ibuf, *buf = &ibuf, asbuf, *path = NULL; char *aspath; uint32_t as, pathid; uint16_t alen, ioff, short_as, afi; @@ -820,25 +819,27 @@ show_attr(u_char *data, size_t len, int reqflags, int addpath) break; case ATTR_ASPATH: case ATTR_AS4_PATH: + ibuf_from_buffer(buf, data, alen); /* prefer 4-byte AS here */ - e4 = aspath_verify(data, alen, 1, 0); - e2 = aspath_verify(data, alen, 0, 0); + e4 = aspath_verify(buf, 1, 0); + e2 = aspath_verify(buf, 0, 0); if (e4 == 0 || e4 == AS_ERR_SOFT) { - path = data; + ibuf_from_ibuf(&asbuf, buf); } else if (e2 == 0 || e2 == AS_ERR_SOFT) { - path = aspath_inflate(data, alen, &alen); - if (path == NULL) - errx(1, "aspath_inflate failed"); + if ((path = aspath_inflate(buf)) == NULL) { + printf("aspath_inflate failed"); + break; + } + ibuf_from_ibuf(&asbuf, path); } else { printf("bad AS-Path"); break; } - if (aspath_asprint(&aspath, path, alen) == -1) + if (aspath_asprint(&aspath, &asbuf) == -1) err(1, NULL); printf("%s", aspath); free(aspath); - if (path != data) - free(path); + ibuf_free(path); break; case ATTR_NEXTHOP: if (alen == 4) { @@ -1013,7 +1014,7 @@ show_attr(u_char *data, size_t len, int reqflags, int addpath) } static void -show_rib_brief(struct ctl_show_rib *r, u_char *asdata, size_t aslen) +show_rib_brief(struct ctl_show_rib *r, struct ibuf *asbuf) { char *p, *aspath; @@ -1025,7 +1026,7 @@ show_rib_brief(struct ctl_show_rib *r, u_char *asdata, size_t aslen) log_addr(&r->exit_nexthop), r->local_pref, r->med); free(p); - if (aspath_asprint(&aspath, asdata, aslen) == -1) + if (aspath_asprint(&aspath, asbuf) == -1) err(1, NULL); if (strlen(aspath) > 0) printf("%s ", aspath); @@ -1035,8 +1036,7 @@ show_rib_brief(struct ctl_show_rib *r, u_char *asdata, size_t aslen) } static void -show_rib_detail(struct ctl_show_rib *r, u_char *asdata, size_t aslen, - int flag0) +show_rib_detail(struct ctl_show_rib *r, struct ibuf *asbuf, int flag0) { struct in_addr id; char *aspath, *s; @@ -1045,7 +1045,7 @@ show_rib_detail(struct ctl_show_rib *r, u_char *asdata, size_t aslen, log_addr(&r->prefix), r->prefixlen, EOL0(flag0)); - if (aspath_asprint(&aspath, asdata, aslen) == -1) + if (aspath_asprint(&aspath, asbuf) == -1) err(1, NULL); if (strlen(aspath) > 0) printf(" %s%c", aspath, EOL0(flag0)); @@ -1072,13 +1072,12 @@ show_rib_detail(struct ctl_show_rib *r, u_char *asdata, size_t aslen, } static void -show_rib(struct ctl_show_rib *r, u_char *asdata, size_t aslen, - struct parse_result *res) +show_rib(struct ctl_show_rib *r, struct ibuf *aspath, struct parse_result *res) { if (res->flags & F_CTL_DETAIL) - show_rib_detail(r, asdata, aslen, res->flags); + show_rib_detail(r, aspath, res->flags); else - show_rib_brief(r, asdata, aslen); + show_rib_brief(r, aspath); } static void diff --git a/usr.sbin/bgpctl/output_json.c b/usr.sbin/bgpctl/output_json.c index 1ebd27b40da..d18672392fc 100644 --- a/usr.sbin/bgpctl/output_json.c +++ b/usr.sbin/bgpctl/output_json.c @@ -1,4 +1,4 @@ -/* $OpenBSD: output_json.c,v 1.40 2024/01/25 09:54:21 claudio Exp $ */ +/* $OpenBSD: output_json.c,v 1.41 2024/01/30 13:51:13 claudio Exp $ */ /* * Copyright (c) 2020 Claudio Jeker @@ -589,9 +589,8 @@ json_attr(u_char *data, size_t len, int reqflags, int addpath) { struct bgpd_addr prefix; struct in_addr id; - struct ibuf ibuf, *buf = &ibuf; + struct ibuf ibuf, *buf = &ibuf, asbuf, *path = NULL; char *aspath; - u_char *path; uint32_t as, pathid; uint16_t alen, afi, off, short_as; uint8_t flags, type, safi, aid, prefixlen; @@ -645,25 +644,28 @@ json_attr(u_char *data, size_t len, int reqflags, int addpath) break; case ATTR_ASPATH: case ATTR_AS4_PATH: + ibuf_from_buffer(buf, data, alen); /* prefer 4-byte AS here */ - e4 = aspath_verify(data, alen, 1, 0); - e2 = aspath_verify(data, alen, 0, 0); + e4 = aspath_verify(buf, 1, 0); + e2 = aspath_verify(buf, 0, 0); if (e4 == 0 || e4 == AS_ERR_SOFT) { - path = data; + ibuf_from_ibuf(&asbuf, buf); } else if (e2 == 0 || e2 == AS_ERR_SOFT) { - path = aspath_inflate(data, alen, &alen); - if (path == NULL) - errx(1, "aspath_inflate failed"); + if ((path = aspath_inflate(buf)) == NULL) { + json_do_string("error", + "aspath_inflate failed"); + break; + } + ibuf_from_ibuf(&asbuf, path); } else { json_do_string("error", "bad AS-Path"); break; } - if (aspath_asprint(&aspath, path, alen) == -1) + if (aspath_asprint(&aspath, &asbuf) == -1) err(1, NULL); json_do_string("aspath", aspath); free(aspath); - if (path != data) - free(path); + ibuf_free(path); break; case ATTR_NEXTHOP: if (alen == 4) { @@ -841,8 +843,7 @@ bad_len: } static void -json_rib(struct ctl_show_rib *r, u_char *asdata, size_t aslen, - struct parse_result *res) +json_rib(struct ctl_show_rib *r, struct ibuf *asbuf, struct parse_result *res) { struct in_addr id; char *aspath; @@ -853,7 +854,7 @@ json_rib(struct ctl_show_rib *r, u_char *asdata, size_t aslen, json_do_printf("prefix", "%s/%u", log_addr(&r->prefix), r->prefixlen); - if (aspath_asprint(&aspath, asdata, aslen) == -1) + if (aspath_asprint(&aspath, asbuf) == -1) err(1, NULL); json_do_string("aspath", aspath); free(aspath); -- 2.20.1