From 1be0be1b71f0127740a4aa6f35d4a256d6c34fb9 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Fri, 28 Apr 2023 19:13:56 +0200 Subject: [PATCH] BGP: Save sent and received OPEN messages These are necessary for BMP Peer UP message and it is better to keep them in BGP than in BMP (so BMP could be restarted or added later). --- proto/bgp/bgp.c | 7 +++++++ proto/bgp/bgp.h | 6 ++++++ proto/bgp/packets.c | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c index c806765a..373b0392 100644 --- a/proto/bgp/bgp.c +++ b/proto/bgp/bgp.c @@ -380,6 +380,13 @@ bgp_close_conn(struct bgp_conn *conn) rfree(conn->sk); conn->sk = NULL; + mb_free(conn->local_open_msg); + conn->local_open_msg = NULL; + mb_free(conn->remote_open_msg); + conn->remote_open_msg = NULL; + conn->local_open_length = 0; + conn->remote_open_length = 0; + mb_free(conn->local_caps); conn->local_caps = NULL; mb_free(conn->remote_caps); diff --git a/proto/bgp/bgp.h b/proto/bgp/bgp.h index 7c96e851..5f8f183d 100644 --- a/proto/bgp/bgp.h +++ b/proto/bgp/bgp.h @@ -287,6 +287,11 @@ struct bgp_conn { u8 ext_messages; /* Session uses extended message length */ u32 received_as; /* ASN received in OPEN message */ + byte *local_open_msg; /* Saved OPEN messages (no header) */ + byte *remote_open_msg; + uint local_open_length; + uint remote_open_length; + struct bgp_caps *local_caps; struct bgp_caps *remote_caps; timer *connect_timer; @@ -487,6 +492,7 @@ struct bgp_parse_state { #define BGP_PORT 179 #define BGP_VERSION 4 #define BGP_HEADER_LENGTH 19 +#define BGP_HDR_MARKER_LENGTH 16 #define BGP_MAX_MESSAGE_LENGTH 4096 #define BGP_MAX_EXT_MSG_LENGTH 65535 #define BGP_RX_BUFFER_SIZE 4096 diff --git a/proto/bgp/packets.c b/proto/bgp/packets.c index b9537169..c6c12cf2 100644 --- a/proto/bgp/packets.c +++ b/proto/bgp/packets.c @@ -772,6 +772,14 @@ err: return -1; } +static byte * +bgp_copy_open(struct bgp_proto *p, const byte *pkt, uint len) +{ + char *buf = mb_alloc(p->p.pool, len - BGP_HEADER_LENGTH); + memcpy(buf, pkt + BGP_HEADER_LENGTH, len - BGP_HEADER_LENGTH); + return buf; +} + static byte * bgp_create_open(struct bgp_conn *conn, byte *buf) { @@ -846,6 +854,9 @@ bgp_rx_open(struct bgp_conn *conn, byte *pkt, uint len) id = get_u32(pkt+24); BGP_TRACE(D_PACKETS, "Got OPEN(as=%d,hold=%d,id=%R)", asn, hold, id); + conn->remote_open_msg = bgp_copy_open(p, pkt, len); + conn->remote_open_length = len - BGP_HEADER_LENGTH; + if (bgp_read_options(conn, pkt+29, pkt[28], len-29) < 0) return; @@ -2984,7 +2995,7 @@ bgp_send(struct bgp_conn *conn, uint type, uint len) conn->bgp->stats.tx_messages++; conn->bgp->stats.tx_bytes += len; - memset(buf, 0xff, 16); /* Marker */ + memset(buf, 0xff, BGP_HDR_MARKER_LENGTH); put_u16(buf+16, len); buf[18] = type; @@ -3032,6 +3043,10 @@ bgp_fire_tx(struct bgp_conn *conn) { conn->packets_to_send &= ~(1 << PKT_OPEN); end = bgp_create_open(conn, pkt); + + conn->local_open_msg = bgp_copy_open(p, buf, end - buf); + conn->local_open_length = end - buf - BGP_HEADER_LENGTH; + int rv = bgp_send(conn, PKT_OPEN, end - buf); if (rv >= 0) {