From 1346c272bea6b7378ecd97133f5d5f16a55e570e Mon Sep 17 00:00:00 2001 From: claudio Date: Fri, 27 Oct 2023 14:22:43 +0000 Subject: [PATCH] Simplify the handling of optparamlen and RFC9072 support. Use the ibuf_size() of the optional params (opb) when writing the various headers. Only use optparamlen to write the Opt Parm Len field of the OPEN message. Since there the size could be forced to 255 in case of a RFC9072 exended header. Also use size_t for optparamlen since the new ibuf API no longer depends on using the right sized type. OK tb@ --- usr.sbin/bgpd/session.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c index 5db8ebb1016..e7dbc3b32f1 100644 --- a/usr.sbin/bgpd/session.c +++ b/usr.sbin/bgpd/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.452 2023/10/27 09:40:27 claudio Exp $ */ +/* $OpenBSD: session.c,v 1.453 2023/10/27 14:22:43 claudio Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer @@ -1471,8 +1471,9 @@ session_open(struct peer *p) { struct bgp_msg *buf; struct ibuf *opb; - uint16_t len, optparamlen = 0, holdtime; - uint8_t i, op_type; + size_t optparamlen; + uint16_t holdtime; + uint8_t i; int errs = 0, extlen = 0; int mpcapa = 0; @@ -1556,16 +1557,16 @@ session_open(struct peer *p) if (optparamlen == 0) { /* nothing */ } else if (optparamlen + 2 >= 255) { - /* RFC9072: 2 byte length instead of 1 + 3 byte extra header */ - optparamlen += sizeof(op_type) + 2 + 3; + /* RFC9072: use 255 as magic size and request extra header */ optparamlen = 255; extlen = 1; } else { - optparamlen += sizeof(op_type) + 1; + /* regular capabilities header */ + optparamlen += 2; } - len = MSGSIZE_OPEN_MIN + optparamlen; - if (errs || (buf = session_newmsg(OPEN, len)) == NULL) { + if (errs || (buf = session_newmsg(OPEN, + MSGSIZE_OPEN_MIN + optparamlen)) == NULL) { ibuf_free(opb); bgp_fsm(p, EVNT_CON_FATAL); return; @@ -1584,20 +1585,19 @@ session_open(struct peer *p) errs += ibuf_add_n8(buf->buf, optparamlen); if (extlen) { - /* write RFC9072 extra header */ + /* RFC9072 extra header which spans over the capabilities hdr */ errs += ibuf_add_n8(buf->buf, OPT_PARAM_EXT_LEN); - errs += ibuf_add_n16(buf->buf, optparamlen - 3); + errs += ibuf_add_n16(buf->buf, ibuf_size(opb) + 1 + 2); } if (optparamlen) { errs += ibuf_add_n8(buf->buf, OPT_PARAM_CAPABILITIES); - optparamlen = ibuf_size(opb); if (extlen) { /* RFC9072: 2-byte extended length */ - errs += ibuf_add_n16(buf->buf, optparamlen); + errs += ibuf_add_n16(buf->buf, ibuf_size(opb)); } else { - errs += ibuf_add_n8(buf->buf, optparamlen); + errs += ibuf_add_n8(buf->buf, ibuf_size(opb)); } errs += ibuf_add_buf(buf->buf, opb); } -- 2.20.1