From c2371130ed453b74aea3b026dfb0e3b084815f8e Mon Sep 17 00:00:00 2001 From: claudio Date: Fri, 21 Apr 2023 10:49:01 +0000 Subject: [PATCH] Adjust ext community handling to support the generic transitive communities introduced with flowspec. OK tb@ --- usr.sbin/bgpctl/bgpctl.c | 40 ++++++++++++++++++++++------------- usr.sbin/bgpctl/output.c | 5 ++--- usr.sbin/bgpctl/output_json.c | 5 ++--- usr.sbin/bgpctl/parser.c | 12 ++++++++--- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/usr.sbin/bgpctl/bgpctl.c b/usr.sbin/bgpctl/bgpctl.c index 91387b0ab01..e1304a9e4bb 100644 --- a/usr.sbin/bgpctl/bgpctl.c +++ b/usr.sbin/bgpctl/bgpctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpctl.c,v 1.292 2023/04/21 09:12:41 claudio Exp $ */ +/* $OpenBSD: bgpctl.c,v 1.293 2023/04/21 10:49:01 claudio Exp $ */ /* * Copyright (c) 2003 Henning Brauer @@ -1051,6 +1051,7 @@ fmt_ext_community(uint8_t *data) switch (type) { case EXT_COMMUNITY_TRANS_TWO_AS: + case EXT_COMMUNITY_GEN_TWO_AS: memcpy(&as2, data + 2, sizeof(as2)); memcpy(&u32, data + 4, sizeof(u32)); snprintf(buf, sizeof(buf), "%s %s:%u", @@ -1058,6 +1059,7 @@ fmt_ext_community(uint8_t *data) log_as(ntohs(as2)), ntohl(u32)); return buf; case EXT_COMMUNITY_TRANS_IPV4: + case EXT_COMMUNITY_GEN_IPV4: memcpy(&ip, data + 2, sizeof(ip)); memcpy(&u16, data + 6, sizeof(u16)); snprintf(buf, sizeof(buf), "%s %s:%hu", @@ -1065,6 +1067,7 @@ fmt_ext_community(uint8_t *data) inet_ntoa(ip), ntohs(u16)); return buf; case EXT_COMMUNITY_TRANS_FOUR_AS: + case EXT_COMMUNITY_GEN_FOUR_AS: memcpy(&as4, data + 2, sizeof(as4)); memcpy(&u16, data + 6, sizeof(u16)); snprintf(buf, sizeof(buf), "%s %s:%hu", @@ -1081,20 +1084,27 @@ fmt_ext_community(uint8_t *data) case EXT_COMMUNITY_NON_TRANS_OPAQUE: memcpy(&ext, data, sizeof(ext)); ext = be64toh(ext) & 0xffffffffffffLL; - switch (ext) { - case EXT_COMMUNITY_OVS_VALID: - snprintf(buf, sizeof(buf), "%s valid", - log_ext_subtype(type, subtype)); - return buf; - case EXT_COMMUNITY_OVS_NOTFOUND: - snprintf(buf, sizeof(buf), "%s not-found", - log_ext_subtype(type, subtype)); - return buf; - case EXT_COMMUNITY_OVS_INVALID: - snprintf(buf, sizeof(buf), "%s invalid", - log_ext_subtype(type, subtype)); - return buf; - default: + if (subtype == EXT_COMMUNITY_SUBTYPE_OVS) { + switch (ext) { + case EXT_COMMUNITY_OVS_VALID: + snprintf(buf, sizeof(buf), "%s valid", + log_ext_subtype(type, subtype)); + return buf; + case EXT_COMMUNITY_OVS_NOTFOUND: + snprintf(buf, sizeof(buf), "%s not-found", + log_ext_subtype(type, subtype)); + return buf; + case EXT_COMMUNITY_OVS_INVALID: + snprintf(buf, sizeof(buf), "%s invalid", + log_ext_subtype(type, subtype)); + return buf; + default: + snprintf(buf, sizeof(buf), "%s 0x%llx", + log_ext_subtype(type, subtype), + (unsigned long long)ext); + return buf; + } + } else { snprintf(buf, sizeof(buf), "%s 0x%llx", log_ext_subtype(type, subtype), (unsigned long long)ext); diff --git a/usr.sbin/bgpctl/output.c b/usr.sbin/bgpctl/output.c index a1bcb31e32f..b12b363187a 100644 --- a/usr.sbin/bgpctl/output.c +++ b/usr.sbin/bgpctl/output.c @@ -1,4 +1,4 @@ -/* $OpenBSD: output.c,v 1.39 2023/04/20 14:01:50 claudio Exp $ */ +/* $OpenBSD: output.c,v 1.40 2023/04/21 10:49:01 claudio Exp $ */ /* * Copyright (c) 2003 Henning Brauer @@ -674,11 +674,10 @@ show_communities(u_char *data, size_t len, struct parse_result *res) break; case COMMUNITY_TYPE_EXT: ext = (uint64_t)c.data3 << 48; - switch (c.data3 >> 8) { + switch ((c.data3 >> 8) & EXT_COMMUNITY_VALUE) { case EXT_COMMUNITY_TRANS_TWO_AS: case EXT_COMMUNITY_TRANS_OPAQUE: case EXT_COMMUNITY_TRANS_EVPN: - case EXT_COMMUNITY_NON_TRANS_OPAQUE: ext |= ((uint64_t)c.data1 & 0xffff) << 32; ext |= (uint64_t)c.data2; break; diff --git a/usr.sbin/bgpctl/output_json.c b/usr.sbin/bgpctl/output_json.c index 3a702917da6..6e623015a35 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.31 2023/03/28 12:07:09 claudio Exp $ */ +/* $OpenBSD: output_json.c,v 1.32 2023/04/21 10:49:01 claudio Exp $ */ /* * Copyright (c) 2020 Claudio Jeker @@ -495,11 +495,10 @@ json_communities(u_char *data, size_t len, struct parse_result *res) break; case COMMUNITY_TYPE_EXT: ext = (uint64_t)c.data3 << 48; - switch (c.data3 >> 8) { + switch ((c.data3 >> 8) & EXT_COMMUNITY_VALUE) { case EXT_COMMUNITY_TRANS_TWO_AS: case EXT_COMMUNITY_TRANS_OPAQUE: case EXT_COMMUNITY_TRANS_EVPN: - case EXT_COMMUNITY_NON_TRANS_OPAQUE: ext |= ((uint64_t)c.data1 & 0xffff) << 32; ext |= (uint64_t)c.data2; break; diff --git a/usr.sbin/bgpctl/parser.c b/usr.sbin/bgpctl/parser.c index defe91007eb..ff16473082c 100644 --- a/usr.sbin/bgpctl/parser.c +++ b/usr.sbin/bgpctl/parser.c @@ -1,4 +1,4 @@ -/* $OpenBSD: parser.c,v 1.131 2023/04/21 09:12:41 claudio Exp $ */ +/* $OpenBSD: parser.c,v 1.132 2023/04/21 10:49:01 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -741,7 +741,7 @@ match_token(int argc, char *argv[], const struct token table[], int *argsused) rd |= ((uint64_t)ext.data1 & 0xffff) << 32; rd |= (uint64_t)ext.data2; - break; + break; case EXT_COMMUNITY_TRANS_IPV4: rd = (1ULL << 48); rd |= (uint64_t)ext.data1 << 16; @@ -1298,7 +1298,7 @@ parseextvalue(int type, char *s, uint32_t *v, uint32_t *flag) type = EXT_COMMUNITY_TRANS_IPV4; } - switch (type) { + switch (type & EXT_COMMUNITY_VALUE) { case EXT_COMMUNITY_TRANS_TWO_AS: uval = strtonum(s, 0, USHRT_MAX, &errstr); if (errstr) @@ -1355,6 +1355,9 @@ parseextcommunity(struct community *c, const char *t, char *s) case EXT_COMMUNITY_TRANS_TWO_AS: case EXT_COMMUNITY_TRANS_FOUR_AS: case EXT_COMMUNITY_TRANS_IPV4: + case EXT_COMMUNITY_GEN_TWO_AS: + case EXT_COMMUNITY_GEN_FOUR_AS: + case EXT_COMMUNITY_GEN_IPV4: case -1: if (strcmp(s, "*") == 0) { dflag1 = COMMUNITY_ANY; @@ -1367,10 +1370,13 @@ parseextcommunity(struct community *c, const char *t, char *s) switch (type) { case EXT_COMMUNITY_TRANS_TWO_AS: + case EXT_COMMUNITY_GEN_TWO_AS: getcommunity(p, 1, &uval2, &dflag2); break; case EXT_COMMUNITY_TRANS_IPV4: case EXT_COMMUNITY_TRANS_FOUR_AS: + case EXT_COMMUNITY_GEN_IPV4: + case EXT_COMMUNITY_GEN_FOUR_AS: getcommunity(p, 0, &uval2, &dflag2); break; default: -- 2.20.1