From: claudio Date: Wed, 10 Jan 2024 13:31:09 +0000 (+0000) Subject: Implement log_roa() and log_aspa() and use these functions in printconf.c X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=6290e740bb35283f3fc2ba45c65132ffbe32108b;p=openbsd Implement log_roa() and log_aspa() and use these functions in printconf.c OK tb@ --- diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h index d65f329b765..0c05c831032 100644 --- a/usr.sbin/bgpd/bgpd.h +++ b/usr.sbin/bgpd/bgpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.h,v 1.479 2024/01/08 15:08:34 claudio Exp $ */ +/* $OpenBSD: bgpd.h,v 1.480 2024/01/10 13:31:09 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -1541,6 +1541,8 @@ const char *log_as(uint32_t); const char *log_rd(uint64_t); const char *log_ext_subtype(int, uint8_t); const char *log_reason(const char *); +const char *log_roa(struct roa *); +const char *log_aspa(struct aspa_set *); const char *log_rtr_error(enum rtr_error); const char *log_policy(enum role); int aspath_snprint(char *, size_t, void *, uint16_t); diff --git a/usr.sbin/bgpd/printconf.c b/usr.sbin/bgpd/printconf.c index 57dc8aeb02f..5f39b696c9d 100644 --- a/usr.sbin/bgpd/printconf.c +++ b/usr.sbin/bgpd/printconf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: printconf.c,v 1.168 2023/08/16 08:26:35 claudio Exp $ */ +/* $OpenBSD: printconf.c,v 1.169 2024/01/10 13:31:09 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -687,22 +687,13 @@ void print_roa(struct roa_tree *r) { struct roa *roa; - struct bgpd_addr addr; if (RB_EMPTY(r)) return; printf("roa-set {"); RB_FOREACH(roa, roa_tree, r) { - printf("\n\t"); - addr.aid = roa->aid; - addr.v6 = roa->prefix.inet6; - printf("%s/%u", log_addr(&addr), roa->prefixlen); - if (roa->prefixlen != roa->maxlen) - printf(" maxlen %u", roa->maxlen); - printf(" source-as %u", roa->asnum); - if (roa->expires != 0) - printf(" expires %lld", (long long)roa->expires); + printf("\n\t%s", log_roa(roa)); } printf("\n}\n\n"); } @@ -711,21 +702,13 @@ void print_aspa(struct aspa_tree *a) { struct aspa_set *aspa; - uint32_t i; if (RB_EMPTY(a)) return; printf("aspa-set {"); RB_FOREACH(aspa, aspa_tree, a) { - printf("\n\t"); - printf("customer-as %s", log_as(aspa->as)); - if (aspa->expires != 0) - printf(" expires %lld", (long long)aspa->expires); - printf(" provider-as { "); - for (i = 0; i < aspa->num; i++) - printf("%s ", log_as(aspa->tas[i])); - printf("}"); + printf("\n\t%s", log_aspa(aspa)); } printf("\n}\n\n"); } diff --git a/usr.sbin/bgpd/util.c b/usr.sbin/bgpd/util.c index a7ac90a3b5d..b28ff4e9ae6 100644 --- a/usr.sbin/bgpd/util.c +++ b/usr.sbin/bgpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.77 2023/04/17 08:02:21 claudio Exp $ */ +/* $OpenBSD: util.c,v 1.78 2024/01/10 13:31:09 claudio Exp $ */ /* * Copyright (c) 2006 Claudio Jeker @@ -165,6 +165,76 @@ log_reason(const char *communication) { return buf; } +static const char * +log_expires(time_t expires) +{ + static char buf[32]; + + buf[0] = '\0'; + if (expires != 0) + snprintf(buf, sizeof(buf), " expires %lld", (long long)expires); + return buf; +} + +const char * +log_roa(struct roa *roa) +{ + static char buf[256]; + struct bgpd_addr addr = { .aid = roa->aid, .v6 = roa->prefix.inet6 }; + char maxbuf[32]; + + maxbuf[0] = '\0'; + if (roa->prefixlen != roa->maxlen) + snprintf(maxbuf, sizeof(maxbuf), " maxlen %u", roa->maxlen); + snprintf(buf, sizeof(buf), "%s/%u%s source-as %u%s", log_addr(&addr), + roa->prefixlen, maxbuf, roa->asnum, log_expires(roa->expires)); + return buf; +} + +const char * +log_aspa(struct aspa_set *aspa) +{ + static char errbuf[256]; + static char *buf; + static size_t len; + char asbuf[16]; + size_t needed; + uint32_t i; + + /* include enough space for header and trailer */ + if ((uint64_t)aspa->num > (SIZE_MAX / sizeof(asbuf) - 72)) + goto fail; + needed = aspa->num * sizeof(asbuf) + 72; + if (needed > len) { + char *nbuf; + + if ((nbuf = realloc(buf, needed)) == NULL) + goto fail; + len = needed; + buf = nbuf; + } + + snprintf(buf, len, "customer-as %s%s provider-as { ", + log_as(aspa->as), log_expires(aspa->expires)); + + for (i = 0; i < aspa->num; i++) { + snprintf(asbuf, sizeof(asbuf), "%s ", log_as(aspa->tas[i])); + if (strlcat(buf, asbuf, len) >= len) + goto fail; + } + if (strlcat(buf, "}", len) >= len) + goto fail; + return buf; + + fail: + free(buf); + buf = NULL; + len = 0; + snprintf(errbuf, sizeof(errbuf), "customer-as %s%s provider-as { ... }", + log_as(aspa->as), log_expires(aspa->expires)); + return errbuf; +} + const char * log_rtr_error(enum rtr_error err) {