From 584470fb5fc611580367ff2cf15b3ab8e07ef92c Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Wed, 24 Apr 2019 17:19:09 +0200 Subject: [PATCH] bgpd: add & use bgp packet dump hook The MRT dump code is already hooked in at the right places to write out packets; the BMP code needs exactly the same access so let's make this a hook. Signed-off-by: David Lamparter --- bgpd/bgp_dump.c | 7 ++++++- bgpd/bgp_dump.h | 1 - bgpd/bgp_fsm.c | 8 +++++--- bgpd/bgp_packet.c | 8 ++++++-- bgpd/bgp_packet.h | 7 +++++++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bgpd/bgp_dump.c b/bgpd/bgp_dump.c index 751140850a..d12c0b6c75 100644 --- a/bgpd/bgp_dump.c +++ b/bgpd/bgp_dump.c @@ -37,6 +37,7 @@ #include "bgpd/bgp_attr.h" #include "bgpd/bgp_dump.h" #include "bgpd/bgp_errors.h" +#include "bgpd/bgp_packet.h" enum bgp_dump_type { BGP_DUMP_ALL, @@ -555,7 +556,8 @@ static void bgp_dump_packet_func(struct bgp_dump *bgp_dump, struct peer *peer, } /* Called from bgp_packet.c when BGP packet is received. */ -void bgp_dump_packet(struct peer *peer, int type, struct stream *packet) +static int bgp_dump_packet(struct peer *peer, uint8_t type, bgp_size_t size, + struct stream *packet) { /* bgp_dump_all. */ bgp_dump_packet_func(&bgp_dump_all, peer, packet); @@ -563,6 +565,7 @@ void bgp_dump_packet(struct peer *peer, int type, struct stream *packet) /* bgp_dump_updates. */ if (type == BGP_MSG_UPDATE) bgp_dump_packet_func(&bgp_dump_updates, peer, packet); + return 0; } static unsigned int bgp_dump_parse_time(const char *str) @@ -862,6 +865,8 @@ void bgp_dump_init(void) install_element(CONFIG_NODE, &dump_bgp_all_cmd); install_element(CONFIG_NODE, &no_dump_bgp_all_cmd); + + hook_register(bgp_packet_dump, bgp_dump_packet); } void bgp_dump_finish(void) diff --git a/bgpd/bgp_dump.h b/bgpd/bgp_dump.h index f73081b2e2..5ec0561b05 100644 --- a/bgpd/bgp_dump.h +++ b/bgpd/bgp_dump.h @@ -52,6 +52,5 @@ extern void bgp_dump_init(void); extern void bgp_dump_finish(void); extern void bgp_dump_state(struct peer *, int, int); -extern void bgp_dump_packet(struct peer *, int, struct stream *); #endif /* _QUAGGA_BGP_DUMP_H */ diff --git a/bgpd/bgp_fsm.c b/bgpd/bgp_fsm.c index dd765731dc..4348e6b240 100644 --- a/bgpd/bgp_fsm.c +++ b/bgpd/bgp_fsm.c @@ -184,9 +184,11 @@ static struct peer *peer_xfer_conn(struct peer *from_peer) EC_BGP_PKT_PROCESS, "[%s] Dropping pending packet on connection transfer:", peer->host); - uint16_t type = stream_getc_from(peer->curr, - BGP_MARKER_SIZE + 2); - bgp_dump_packet(peer, type, peer->curr); + /* there used to be a bgp_packet_dump call here, but + * that's extremely confusing since there's no way to + * identify the packet in MRT dumps or BMP as dropped + * due to connection transfer. + */ stream_free(peer->curr); peer->curr = NULL; } diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c index e907c6a10d..da00c7b3fe 100644 --- a/bgpd/bgp_packet.c +++ b/bgpd/bgp_packet.c @@ -63,6 +63,11 @@ #include "bgpd/bgp_keepalives.h" #include "bgpd/bgp_flowspec.h" +DEFINE_HOOK(bgp_packet_dump, + (struct peer *peer, uint8_t type, bgp_size_t size, + struct stream *s), + (peer, type, size, s)) + /** * Sets marker and type fields for a BGP message. * @@ -2242,8 +2247,7 @@ int bgp_process_packet(struct thread *thread) size = stream_getw(peer->curr); type = stream_getc(peer->curr); - /* BGP packet dump function. */ - bgp_dump_packet(peer, type, peer->curr); + hook_call(bgp_packet_dump, peer, type, size, peer->curr); /* adjust size to exclude the marker + length + type */ size -= BGP_HEADER_SIZE; diff --git a/bgpd/bgp_packet.h b/bgpd/bgp_packet.h index 06a190585b..91031589a7 100644 --- a/bgpd/bgp_packet.h +++ b/bgpd/bgp_packet.h @@ -21,6 +21,13 @@ #ifndef _QUAGGA_BGP_PACKET_H #define _QUAGGA_BGP_PACKET_H +#include "hook.h" + +DECLARE_HOOK(bgp_packet_dump, + (struct peer *peer, uint8_t type, bgp_size_t size, + struct stream *s), + (peer, type, size, s)) + #define BGP_NLRI_LENGTH 1U #define BGP_TOTAL_ATTR_LEN 2U #define BGP_UNFEASIBLE_LEN 2U