From f22dd176345b899f78cfc303bd509b04e2d3431d Mon Sep 17 00:00:00 2001 From: Vojtech Vilimek Date: Fri, 16 Aug 2024 10:50:46 +0200 Subject: [PATCH] SNMP: Config grammar changes --- proto/snmp/bgp4_mib.c | 9 +-- proto/snmp/config.Y | 135 ++++++++++++++++++++++++------------------ proto/snmp/snmp.c | 31 +++++----- proto/snmp/snmp.h | 25 ++++---- 4 files changed, 109 insertions(+), 91 deletions(-) diff --git a/proto/snmp/bgp4_mib.c b/proto/snmp/bgp4_mib.c index fabe7cfb..8d3c54b2 100644 --- a/proto/snmp/bgp4_mib.c +++ b/proto/snmp/bgp4_mib.c @@ -308,7 +308,7 @@ fill_local_as(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { if (c->sr_vb_start->name.n_subid != 4) return SNMP_SEARCH_NO_INSTANCE; - snmp_varbind_int(c, c->p->bgp_local_as); + snmp_varbind_int(c, c->p->bgp4_local_as); return SNMP_SEARCH_OK; } @@ -663,7 +663,8 @@ fill_local_id(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { if (c->sr_vb_start->name.n_subid != 4) return SNMP_SEARCH_NO_INSTANCE; - snmp_varbind_ip4(c, c->p->bgp_local_id); + ip4_addr router_id_ip = ip4_from_u32(c->p->bgp4_local_id); + snmp_varbind_ip4(c, router_id_ip); return SNMP_SEARCH_OK; } @@ -749,8 +750,8 @@ void snmp_bgp4_show_info(struct snmp_proto *p) { cli_msg(-1006, " BGP4-MIB"); - cli_msg(-1006, " Local AS %u", p->bgp_local_as); - cli_msg(-1006, " Local router id %R", p->bgp_local_id); + cli_msg(-1006, " Local AS %u", p->bgp4_local_as); + cli_msg(-1006, " Local router id %R", p->bgp4_local_id); cli_msg(-1006, " BGP peers"); if (p->bgp_hash.count == 0) diff --git a/proto/snmp/config.Y b/proto/snmp/config.Y index 1aeef74a..dbe04057 100644 --- a/proto/snmp/config.Y +++ b/proto/snmp/config.Y @@ -18,8 +18,9 @@ CF_DEFINES CF_DECLS -CF_KEYWORDS(SNMP, PROTOCOL, BGP, LOCAL, AS, REMOTE, ADDRESS, PORT, DESCRIPTION, - TIMEOUT, PRIORITY, CONTEXT, DEFAULT, MESSAGE, VERBOSE) +CF_KEYWORDS(SNMP, PROTOCOL, LOCAL, AS, REMOTE, ADDRESS, PORT, DESCRIPTION, + TIMEOUT, PRIORITY, CONTEXT, DEFAULT, MESSAGE, VERBOSE, AGENTX, + SUBAGENT, MASTER, BGP4, MIB, REGISTRATION, PEER) CF_GRAMMAR @@ -30,48 +31,39 @@ snmp_proto: snmp_proto_item: proto_item - | snmp_bgp_bond - | LOCAL PORT expr { - if (($3 < 1) || ($3 > 65535)) cf_error("Invalid port number"); - SNMP_CFG->local_port = $3; - } - | REMOTE PORT expr { - if (($3 < 1) || ($3 > 65535)) cf_error("Invalid port number"); - SNMP_CFG->remote_port = $3; - } - | LOCAL ID IP4 { SNMP_CFG->bgp_local_id = $3; } - | LOCAL ADDRESS IP4 { SNMP_CFG->local_ip = $3; } - | REMOTE ADDRESS DEFAULT { + | SOURCE ADDRESS ipa { SNMP_CFG->local_ip = $3; } + | AGENTX MASTER ADDRESS DEFAULT { if (SNMP_CFG->trans_type != SNMP_TRANS_DEFAULT) cf_error("Duplicit option remote address"); } - | REMOTE ADDRESS ipa { + | AGENTX MASTER ADDRESS text { if (SNMP_CFG->trans_type != SNMP_TRANS_DEFAULT) cf_error("Duplicit option remote address"); - SNMP_CFG->remote_ip = $3; - SNMP_CFG->trans_type = SNMP_TRANS_TCP; - } - | REMOTE ADDRESS text { - if (SNMP_CFG->trans_type != SNMP_TRANS_DEFAULT) - cf_error("Duplicit option remote address"); - - if (strcmp($3, agentx_master_addr)) { - SNMP_CFG->remote_path = $3; + if (strcmp($4, agentx_master_addr)) { + SNMP_CFG->master_path = $4; SNMP_CFG->trans_type = SNMP_TRANS_UNIX; } } - | LOCAL AS expr { - if ($3 < 1 || $3 > UINT16_MAX) cf_error("Invalid local AS"); - SNMP_CFG->bgp_local_as = $3; + | AGENTX MASTER ADDRESS ipa { + if (SNMP_CFG->trans_type != SNMP_TRANS_DEFAULT) + cf_error("Duplicit option remote address"); + + SNMP_CFG->master_ip = $4; + SNMP_CFG->trans_type = SNMP_TRANS_TCP; } - | SNMP DESCRIPTION text { + | AGENTX MASTER PORT expr { + /* TODO better grammar name (maybe: AX MSTR ADDRESS ipa / ADDRESS ipa [PORT] expr) */ + if (($4 < 1) || ($4 > 65535)) cf_error("Invalid port number"); + SNMP_CFG->master_port = $4; + } + | SUBAGENT DESCRIPTION text { if (strlen($3) > UINT16_MAX - 1) cf_error("Description is too long"); SNMP_CFG->description = $3; } - | PRIORITY expr { - if ($2 > 255) cf_error("Registration priority must be in range 0-255"); - SNMP_CFG->priority = $2; + | REGISTRATION PRIORITY expr { + if ($3 > 255) cf_error("Registration priority must be in range 0-255"); + SNMP_CFG->priority = $3; } | MESSAGE TIMEOUT expr_us { /* TODO */ @@ -87,39 +79,43 @@ snmp_proto_item: / * TODO * / SNMP_CFG->error_timeout = $3; } + | RETRY TIME expr_us { + / * TODO * / + SNMP_CFG->retry_timeout = $3; + } */ | START DELAY TIME expr_us { SNMP_CFG->startup_delay = $4; } | VERBOSE bool { SNMP_CFG->verbose = $2; } ; -snmp_proto_opts: - /* empty */ - | snmp_proto_opts snmp_proto_item ';' +bgp4_mib: + BGP4 MIB '{' bgp4_mib_items '}' + | MIB BGP4 '{' bgp4_mib_items '}' ; -snmp_proto_start: proto_start SNMP -{ - this_proto = proto_config_new(&proto_snmp, $1); +bgp4_mib_items: + bgp4_mib_items_opt bgp4_mib_as bgp4_mib_items_opt bgp4_mib_id bgp4_mib_items_opt + ; - init_list(&SNMP_CFG->bgp_entries); - SNMP_CFG->bonds = 0; +bgp4_mib_as: + LOCAL AS expr ';' { + if ($3 < 1 || $3 > UINT16_MAX) cf_error("Invalid local AS for BGP4-MIB"); + SNMP_CFG->bgp4_local_as = $3; + } + /* TODO add option to follow some bgp peer local as */ + ; - SNMP_CFG->local_ip = IP4_NONE; - SNMP_CFG->remote_ip = IPA_NONE; - SNMP_CFG->remote_path = agentx_master_addr; - SNMP_CFG->trans_type = SNMP_TRANS_DEFAULT; - SNMP_CFG->bgp_local_id = IP4_NONE; - SNMP_CFG->local_port = 0; - SNMP_CFG->remote_port = 705; - SNMP_CFG->bgp_local_as = 0; - SNMP_CFG->verbose = 0; +bgp4_mib_id: + LOCAL ROUTER ID idval ';' { SNMP_CFG->bgp4_local_id = $4; } + /* TODO add option to inherit global router id, or follow some bgp peer */ + ; - SNMP_CFG->description = "bird"; - SNMP_CFG->timeout = 15; - SNMP_CFG->priority = AGENTX_PRIORITY; -} +bgp4_mib_items_opt: + /* empty */ + | bgp4_mib_items_opt bgp4_mib_peer ';' + ; -snmp_bgp_bond: BGP symbol +bgp4_mib_peer: PEER symbol { /* the snmp_context rule sets the correct value of this_bond */ cf_assert_symbol($2, SYM_PROTO); @@ -129,11 +125,6 @@ snmp_bgp_bond: BGP symbol cf_assert($2->proto->protocol == &proto_bgp, "SNMP BGP bond accepts only BGP protocols"); - struct bgp_config *bgp_config = SKIP_BACK(struct bgp_config, c, $2->proto); - - if (!ipa_is_ip4(bgp_config->remote_ip)) - cf_error("BGP4-MIB does not support IPv6 addresses."); - struct snmp_bond *this_bond = cfg_alloc(sizeof(struct snmp_bond)); this_bond->type = SNMP_BGP; this_bond->config = $2->proto; @@ -142,6 +133,34 @@ snmp_bgp_bond: BGP symbol SNMP_CFG->bonds++; } +snmp_proto_opts: + /* empty */ + | snmp_proto_opts snmp_proto_item ';' + | snmp_proto_opts bgp4_mib + ; + +snmp_proto_start: proto_start SNMP +{ + this_proto = proto_config_new(&proto_snmp, $1); + + init_list(&SNMP_CFG->bgp_entries); + SNMP_CFG->bonds = 0; + + SNMP_CFG->local_ip = IPA_NONE; + SNMP_CFG->master_ip = IPA_NONE; + SNMP_CFG->master_port = SNMP_PORT; + SNMP_CFG->master_path = agentx_master_addr; + SNMP_CFG->trans_type = SNMP_TRANS_DEFAULT; + SNMP_CFG->bgp4_local_id = 0; + SNMP_CFG->bgp4_local_as = 0; + SNMP_CFG->verbose = 0; + + SNMP_CFG->description = "bird"; + SNMP_CFG->timeout = 15; + SNMP_CFG->priority = AGENTX_PRIORITY; +} + + CF_CODE CF_END diff --git a/proto/snmp/snmp.c b/proto/snmp/snmp.c index eb01fabc..2617288a 100644 --- a/proto/snmp/snmp.c +++ b/proto/snmp/snmp.c @@ -328,8 +328,8 @@ snmp_set_state(struct snmp_proto *p, enum snmp_proto_state state) /* We need to lock the IP address */ struct object_lock *lock; lock = p->lock = olock_new(p->pool); - lock->addr = p->remote_ip; - lock->port = p->remote_port; + lock->addr = p->master_ip; + lock->port = p->master_port; lock->type = OBJLOCK_TCP; lock->hook = snmp_start_locked; lock->data = p; @@ -349,15 +349,15 @@ snmp_set_state(struct snmp_proto *p, enum snmp_proto_state state) if (cf->trans_type == SNMP_TRANS_TCP) { s->type = SK_TCP_ACTIVE; - s->daddr = p->remote_ip; - s->dport = p->remote_port; + s->daddr = p->master_ip; + s->dport = p->master_port; s->rbsize = SNMP_RX_BUFFER_SIZE; s->tbsize = SNMP_TX_BUFFER_SIZE; } else { s->type = SK_UNIX_ACTIVE; - s->host = cf->remote_path; /* daddr */ + s->host = cf->master_path; /* daddr */ s->rbsize = SNMP_RX_BUFFER_SIZE; s->tbsize = SNMP_TX_BUFFER_SIZE; } @@ -517,16 +517,16 @@ snmp_reconfigure_logic(struct snmp_proto *p, const struct snmp_config *new) return 0; if (old->trans_type == SNMP_TRANS_TCP && - (ipa_compare(old->remote_ip, new->remote_ip) - || old->remote_port != new->remote_port)) + (ipa_compare(old->master_ip, new->master_ip) + || old->master_port != new->master_port)) return 0; if (old->trans_type != SNMP_TRANS_TCP && - bstrcmp(old->remote_path, new->remote_path)) + bstrcmp(old->master_path, new->master_path)) return 0; - return !(ip4_compare(old->bgp_local_id, new->bgp_local_id) - || old->bgp_local_as != new->bgp_local_as + return (old->bgp4_local_id != new->bgp4_local_id + || old->bgp4_local_as != new->bgp4_local_as || old->timeout != new->timeout // TODO distinguish message timemout //(Open.timeout and timeout for timer) || old->priority != new->priority @@ -594,11 +594,10 @@ snmp_start(struct proto *P) struct snmp_config *cf = (struct snmp_config *) P->cf; p->local_ip = cf->local_ip; - p->remote_ip = cf->remote_ip; - p->local_port = cf->local_port; - p->remote_port = cf->remote_port; - p->bgp_local_as = cf->bgp_local_as; - p->bgp_local_id = cf->bgp_local_id; + p->master_ip = cf->master_ip; + p->master_port = cf->master_port; + p->bgp4_local_as = cf->bgp4_local_as; + p->bgp4_local_id = cf->bgp4_local_id; p->timeout = cf->timeout; p->startup_delay = cf->startup_delay; p->verbose = cf->verbose; @@ -651,7 +650,7 @@ snmp_postconfig(struct proto_config *CF) const struct snmp_config *cf = (struct snmp_config *) CF; /* Walk the BGP protocols and cache their references. */ - if (cf->bgp_local_as == 0) + if (cf->bgp4_local_as == 0) cf_error("local as not specified"); } diff --git a/proto/snmp/snmp.h b/proto/snmp/snmp.h index b60b0af9..06e90c6b 100644 --- a/proto/snmp/snmp.h +++ b/proto/snmp/snmp.h @@ -65,14 +65,13 @@ struct snmp_bgp_peer { struct snmp_config { struct proto_config cf; enum snmp_transport_type trans_type; - ip4_addr local_ip; - u16 local_port; - ip_addr remote_ip; /* master agentx IP address for TCP transport */ - u16 remote_port; - const char *remote_path; /* master agentx UNIX socket name */ + ip_addr local_ip; + ip_addr master_ip; /* master agentx IP address for TCP transport */ + u16 master_port; + const char *master_path; /* master agentx UNIX socket name */ - ip4_addr bgp_local_id; /* BGP4-MIB related fields */ - u32 bgp_local_as; + u32 bgp4_local_id; /* BGP4-MIB related fields */ + u32 bgp4_local_as; btime timeout; btime startup_delay; @@ -96,13 +95,13 @@ struct snmp_proto { enum snmp_proto_state state; - ip4_addr local_ip; - ip_addr remote_ip; - u16 local_port; - u16 remote_port; + ip_addr local_ip; + ip_addr master_ip; + u16 master_port; - ip4_addr bgp_local_id; /* BGP4-MIB related fields */ - u32 bgp_local_as; + /* TODO add struct for grouping BGP4-MIB data */ + u32 bgp4_local_id; /* BGP4-MIB related fields */ + u32 bgp4_local_as; sock *sock;