]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
BGP: Save sent and received OPEN messages
authorOndrej Zajicek <santiago@crfreenet.org>
Fri, 28 Apr 2023 17:13:56 +0000 (19:13 +0200)
committerOndrej Zajicek <santiago@crfreenet.org>
Mon, 1 May 2023 02:01:16 +0000 (04:01 +0200)
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
proto/bgp/bgp.h
proto/bgp/packets.c

index c806765a7117b5bd595a1beeb82655cecac7675c..373b039268ec37d54ca4618c6ae8ea9a64f4fb03 100644 (file)
@@ -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);
index 7c96e85133d35d6ae21ab9d1589a26b32f53644e..5f8f183d37d8fe128318dcbc504b3a27051ccf98 100644 (file)
@@ -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
index b953716997155a9ced06212113425f2b75877531..c6c12cf26d462479510677d99213a35323a57c02 100644 (file)
@@ -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)
     {