From 373b343e0c6f7864b234557baf7268543054f1e1 Mon Sep 17 00:00:00 2001 From: Vojtech Vilimek Date: Wed, 17 Jul 2024 13:03:26 +0200 Subject: [PATCH] SNMP: snmp_pdu simplification --- proto/snmp/bgp4_mib.c | 159 ++++++++++++++++++++-------------------- proto/snmp/mib_tree.h | 4 +- proto/snmp/snmp_utils.c | 55 +++++++------- proto/snmp/snmp_utils.h | 8 +- proto/snmp/subagent.c | 43 +++++------ proto/snmp/subagent.h | 14 +--- 6 files changed, 132 insertions(+), 151 deletions(-) diff --git a/proto/snmp/bgp4_mib.c b/proto/snmp/bgp4_mib.c index 9cf4c230..366f49ef 100644 --- a/proto/snmp/bgp4_mib.c +++ b/proto/snmp/bgp4_mib.c @@ -36,7 +36,7 @@ const struct bgp_stats UNUSED *stats; \ const struct bgp_config UNUSED *config -#define POPULATE_BGP4(addr, proto, conn, stats, config) populate_bgp4(d, &(addr), &(proto), &(conn), &(stats), &(config)) +#define POPULATE_BGP4(addr, proto, conn, stats, config) populate_bgp4(c, &(addr), &(proto), &(conn), &(stats), &(config)) static inline void ip4_to_oid(struct oid *oid, ip4_addr addr); @@ -234,7 +234,7 @@ snmp_bgp_notify_backward_trans(struct snmp_proto *p, struct bgp_proto *bgp) void snmp_bgp4_register(struct snmp_proto *p) { - u32 bgp_mib_prefix[] = { 1, BGP4_MIB }; // TODO remove constant + u32 bgp_mib_prefix[] = { SNMP_MIB_2, BGP4_MIB }; { /* Register the whole BGP4-MIB::bgp root tree node */ @@ -332,10 +332,10 @@ print_bgp_record(const struct bgp_proto *bgp_proto) static inline enum snmp_search_res -populate_bgp4(struct snmp_data *d, ip4_addr *addr, const struct bgp_proto **proto, const struct bgp_conn +populate_bgp4(struct snmp_pdu *c, ip4_addr *addr, const struct bgp_proto **proto, const struct bgp_conn **conn, const struct bgp_stats **stats, const struct bgp_config **config) { - const struct oid * const oid = &d->c->sr_vb_start->name; + const struct oid * const oid = &c->sr_vb_start->name; if (snmp_bgp_valid_ip4(oid) && LOAD_U8(oid->n_subid) == 9) *addr = ip4_from_oid(oid); else @@ -344,7 +344,7 @@ populate_bgp4(struct snmp_data *d, ip4_addr *addr, const struct bgp_proto **prot return SNMP_SEARCH_NO_INSTANCE; } - struct snmp_bgp_peer *pe = snmp_hash_find(d->p, *addr); + struct snmp_bgp_peer *pe = snmp_hash_find(c->p, *addr); if (!pe) { snmp_log("populate() hash find failed"); @@ -356,7 +356,7 @@ populate_bgp4(struct snmp_data *d, ip4_addr *addr, const struct bgp_proto **prot if (!ipa_is_ip4(bgp_proto->remote_ip)) { log(L_ERR "%s: Found BGP protocol instance with IPv6 address", bgp_proto->p.name); - d->c->error = AGENTX_RES_GEN_ERROR; + c->error = AGENTX_RES_GEN_ERROR; return SNMP_SEARCH_NO_INSTANCE; } @@ -366,7 +366,7 @@ populate_bgp4(struct snmp_data *d, ip4_addr *addr, const struct bgp_proto **prot /* Here, we could be in problem as the bgp_proto IP address could be changed */ log(L_ERR "%s: Stored hash key IP address and peer remote address differ " "(%I, %I).", bgp_proto->p.name, proto_ip, pe->peer_ip); - d->c->error = AGENTX_RES_GEN_ERROR; + c->error = AGENTX_RES_GEN_ERROR; return SNMP_SEARCH_NO_INSTANCE; } @@ -379,28 +379,28 @@ populate_bgp4(struct snmp_data *d, ip4_addr *addr, const struct bgp_proto **prot } static enum snmp_search_res -fill_bgp_version(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_bgp_version(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill ver"); - if (LOAD_U8(d->c->sr_vb_start->name.n_subid) != 3) + if (LOAD_U8(c->sr_vb_start->name.n_subid) != 3) return SNMP_SEARCH_NO_INSTANCE; - d->c->size -= snmp_str_size_from_len(1); - snmp_varbind_nstr(d->c, BGP4_VERSIONS, 1); + c->size -= snmp_str_size_from_len(1); + snmp_varbind_nstr(c, BGP4_VERSIONS, 1); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_local_as(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_local_as(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill as"); - if (LOAD_U8(d->c->sr_vb_start->name.n_subid) != 3) + if (LOAD_U8(c->sr_vb_start->name.n_subid) != 3) return SNMP_SEARCH_NO_INSTANCE; - snmp_varbind_int(d->c, d->p->bgp_local_as); + snmp_varbind_int(c, c->p->bgp_local_as); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_peer_id(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_peer_id(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill peer id"); enum snmp_search_res res; @@ -413,14 +413,14 @@ fill_peer_id(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (fsm_state == BGP4_MIB_OPENCONFIRM || fsm_state == BGP4_MIB_ESTABLISHED) // TODO last - snmp_varbind_ip4(d->c, ip4_from_u32(bgp_proto->remote_id)); + snmp_varbind_ip4(c, ip4_from_u32(bgp_proto->remote_id)); else - snmp_varbind_ip4(d->c, IP4_NONE); + snmp_varbind_ip4(c, IP4_NONE); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_peer_state(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_peer_state(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill peer state"); enum snmp_search_res res; @@ -431,12 +431,12 @@ fill_peer_state(struct mib_walk_state *walk UNUSED, struct snmp_data *d) uint fsm_state = snmp_bgp_fsm_state(bgp_proto); - snmp_varbind_int(d->c, fsm_state); + snmp_varbind_int(c, fsm_state); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_admin_status(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_admin_status(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill adm status"); enum snmp_search_res res; @@ -446,14 +446,14 @@ fill_admin_status(struct mib_walk_state *walk UNUSED, struct snmp_data *d) return res; if (bgp_proto->p.disabled) - snmp_varbind_int(d->c, AGENTX_ADMIN_STOP); + snmp_varbind_int(c, AGENTX_ADMIN_STOP); else - snmp_varbind_int(d->c, AGENTX_ADMIN_START); + snmp_varbind_int(c, AGENTX_ADMIN_START); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_neg_version(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_neg_version(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill neg ver"); enum snmp_search_res res; @@ -465,14 +465,14 @@ fill_neg_version(struct mib_walk_state *walk UNUSED, struct snmp_data *d) uint fsm_state = snmp_bgp_fsm_state(bgp_proto); if (fsm_state == BGP4_MIB_ESTABLISHED || fsm_state == BGP4_MIB_ESTABLISHED) - snmp_varbind_int(d->c, BGP4_MIB_NEGOTIATED_VER_VALUE); + snmp_varbind_int(c, BGP4_MIB_NEGOTIATED_VER_VALUE); else - snmp_varbind_int(d->c, BGP4_MIB_NEGOTIATED_VER_NO_VALUE); + snmp_varbind_int(c, BGP4_MIB_NEGOTIATED_VER_NO_VALUE); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_local_addr(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_local_addr(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("bgp4_mib fill local addr"); enum snmp_search_res res; @@ -484,12 +484,12 @@ fill_local_addr(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_ip4(d->c, ipa_to_ip4(bgp_proto->local_ip)); + snmp_varbind_ip4(c, ipa_to_ip4(bgp_proto->local_ip)); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_local_port(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_local_port(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("bgp4_mib fill local port"); enum snmp_search_res res; @@ -498,12 +498,12 @@ fill_local_port(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_int(d->c, bgp_conf->local_port); + snmp_varbind_int(c, bgp_conf->local_port); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_remote_addr(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_remote_addr(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("bgp4_mib fill remote addr"); enum snmp_search_res res; @@ -512,12 +512,12 @@ fill_remote_addr(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_ip4(d->c, ipa_to_ip4(bgp_proto->remote_ip)); + snmp_varbind_ip4(c, ipa_to_ip4(bgp_proto->remote_ip)); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_remote_port(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_remote_port(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("bgp4_mib fill remote port"); enum snmp_search_res res; @@ -526,12 +526,12 @@ fill_remote_port(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_int(d->c, bgp_conf->remote_port); + snmp_varbind_int(c, bgp_conf->remote_port); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_remote_as(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_remote_as(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill rem as"); enum snmp_search_res res; @@ -540,12 +540,12 @@ fill_remote_as(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_int(d->c, bgp_proto->remote_as); + snmp_varbind_int(c, bgp_proto->remote_as); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_in_updates(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_in_updates(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill in updates"); enum snmp_search_res res; @@ -554,12 +554,12 @@ fill_in_updates(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_counter32(d->c, bgp_stats->rx_updates); + snmp_varbind_counter32(c, bgp_stats->rx_updates); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_out_updates(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_out_updates(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill out updates"); enum snmp_search_res res; @@ -568,12 +568,12 @@ fill_out_updates(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_counter32(d->c, bgp_stats->tx_updates); + snmp_varbind_counter32(c, bgp_stats->tx_updates); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_in_total_msg(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_in_total_msg(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill in total"); enum snmp_search_res res; @@ -582,12 +582,12 @@ fill_in_total_msg(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_counter32(d->c, bgp_stats->rx_messages); + snmp_varbind_counter32(c, bgp_stats->rx_messages); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_out_total_msg(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_out_total_msg(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill out total"); enum snmp_search_res res; @@ -596,12 +596,12 @@ fill_out_total_msg(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_counter32(d->c, bgp_stats->tx_messages); + snmp_varbind_counter32(c, bgp_stats->tx_messages); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_last_err(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_last_err(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill last err"); enum snmp_search_res res; @@ -613,12 +613,12 @@ fill_last_err(struct mib_walk_state *walk UNUSED, struct snmp_data *d) char last_error[2]; snmp_bgp_last_error(bgp_proto, last_error); - snmp_varbind_nstr(d->c, last_error, 2); + snmp_varbind_nstr(c, last_error, 2); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_established_trans(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_established_trans(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill est trans"); enum snmp_search_res res; @@ -627,13 +627,13 @@ fill_established_trans(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_counter32(d->c, + snmp_varbind_counter32(c, bgp_stats->fsm_established_transitions); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_established_time(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_established_time(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill est time"); enum snmp_search_res res; @@ -643,13 +643,13 @@ fill_established_time(struct mib_walk_state *walk UNUSED, struct snmp_data *d) return res; - snmp_varbind_gauge32(d->c, + snmp_varbind_gauge32(c, (current_time() - bgp_proto->last_established) TO_S); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_retry_interval(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_retry_interval(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill retry int"); enum snmp_search_res res; @@ -658,12 +658,12 @@ fill_retry_interval(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_int(d->c, bgp_conf->connect_retry_time); + snmp_varbind_int(c, bgp_conf->connect_retry_time); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_hold_time(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_hold_time(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill hold time"); enum snmp_search_res res; @@ -672,12 +672,12 @@ fill_hold_time(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_int(d->c, (bgp_conn) ? bgp_conn->hold_time : 0); + snmp_varbind_int(c, (bgp_conn) ? bgp_conn->hold_time : 0); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_keep_alive(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_keep_alive(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill keepalive"); enum snmp_search_res res; @@ -687,15 +687,15 @@ fill_keep_alive(struct mib_walk_state *walk UNUSED, struct snmp_data *d) return res; if (!bgp_conf->hold_time) - snmp_varbind_int(d->c, 0); + snmp_varbind_int(c, 0); else - snmp_varbind_int(d->c, + snmp_varbind_int(c, (bgp_conn) ? bgp_conn->keepalive_time : 0); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_hold_time_conf(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_hold_time_conf(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill hold time c"); enum snmp_search_res res; @@ -704,12 +704,12 @@ fill_hold_time_conf(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_int(d->c, bgp_conf->hold_time); + snmp_varbind_int(c, bgp_conf->hold_time); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_keep_alive_conf(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_keep_alive_conf(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill keepalive c"); enum snmp_search_res res; @@ -720,15 +720,15 @@ fill_keep_alive_conf(struct mib_walk_state *walk UNUSED, struct snmp_data *d) if (!bgp_conf->keepalive_time) - snmp_varbind_int(d->c, 0); + snmp_varbind_int(c, 0); else - snmp_varbind_int(d->c, + snmp_varbind_int(c, (bgp_conn) ? bgp_conn->keepalive_time : 0); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_min_as_org_interval(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_min_as_org_interval(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill min org int"); enum snmp_search_res res; @@ -738,12 +738,12 @@ fill_min_as_org_interval(struct mib_walk_state *walk UNUSED, struct snmp_data *d return res; /* value should be in 1..65535 but is not supported by bird */ - snmp_varbind_int(d->c, 0); + snmp_varbind_int(c, 0); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_route_adv_interval(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_route_adv_interval(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill rt adv int"); enum snmp_search_res res; @@ -753,12 +753,12 @@ fill_route_adv_interval(struct mib_walk_state *walk UNUSED, struct snmp_data *d) return res; /* value should be in 1..65535 but is not supported by bird */ - snmp_varbind_int(d->c, 0); + snmp_varbind_int(c, 0); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_in_update_elapsed_time(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_in_update_elapsed_time(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fil in elapsed"); enum snmp_search_res res; @@ -767,19 +767,19 @@ fill_in_update_elapsed_time(struct mib_walk_state *walk UNUSED, struct snmp_data if (res != SNMP_SEARCH_OK) return res; - snmp_varbind_gauge32(d->c, + snmp_varbind_gauge32(c, (current_time() - bgp_proto->last_rx_update) TO_S ); return SNMP_SEARCH_OK; } static enum snmp_search_res -fill_local_id(struct mib_walk_state *walk UNUSED, struct snmp_data *d) +fill_local_id(struct mib_walk_state *walk UNUSED, struct snmp_pdu *c) { snmp_log("fill local id"); - if (LOAD_U8(d->c->sr_vb_start->name.n_subid) != 3) + if (LOAD_U8(c->sr_vb_start->name.n_subid) != 3) return SNMP_SEARCH_NO_INSTANCE; - snmp_varbind_ip4(d->c, d->p->bgp_local_id); + snmp_varbind_ip4(c, c->p->bgp_local_id); return SNMP_SEARCH_OK; } @@ -787,10 +787,9 @@ fill_local_id(struct mib_walk_state *walk UNUSED, struct snmp_data *d) * bgp4_next_peer */ static int -bgp4_next_peer(struct mib_walk_state *state, struct snmp_data *data) +bgp4_next_peer(struct mib_walk_state *state, struct snmp_pdu *c) { - //struct agentx_varbind *vb = data->c->sr_vb_start; - struct oid *oid = &data->c->sr_vb_start->name; + struct oid *oid = &c->sr_vb_start->name; /* BGP4-MIB::bgpPeerIdentifier */ STATIC_OID(9) bgp4_peer_id = { @@ -816,14 +815,14 @@ bgp4_next_peer(struct mib_walk_state *state, struct snmp_data *data) int old = snmp_oid_size(oid); int new = snmp_oid_size(peer_oid); - if (new - old > 0 && (uint) new - old > data->c->size) + if (new - old > 0 && (uint) new - old > c->size) { snmp_log("bgp4_next_peer small buffer"); - snmp_manage_tbuf(data->p, data->c); - oid = &data->c->sr_vb_start->name; // TODO fix sr_vb_start in manage_tbuf + snmp_manage_tbuf(c->p, c); + oid = &c->sr_vb_start->name; // TODO fix sr_vb_start in manage_tbuf } - data->c->buffer += (new - old); + c->buffer += (new - old); snmp_oid_copy(oid, peer_oid); STORE_U8(oid->include, 1); @@ -840,7 +839,7 @@ bgp4_next_peer(struct mib_walk_state *state, struct snmp_data *data) net_fill_ip4(&net, ip4, IP4_MAX_PREFIX_LENGTH); struct f_trie_walk_state ws; - int match = trie_walk_init(&ws, data->p->bgp_trie, &net, 1); + int match = trie_walk_init(&ws, c->p->bgp_trie, &net, 1); if (match && LOAD_U8(oid->include) && precise) { @@ -931,7 +930,7 @@ snmp_bgp4_start(struct snmp_proto *p) struct { u32 id; - enum snmp_search_res (*filler)(struct mib_walk_state *state, struct snmp_data *data); + enum snmp_search_res (*filler)(struct mib_walk_state *state, struct snmp_pdu *c); enum agentx_type type; int size; } leafs[] = { @@ -977,7 +976,7 @@ snmp_bgp4_start(struct snmp_proto *p) }; struct { - enum snmp_search_res (*filler)(struct mib_walk_state *state, struct snmp_data *data); + enum snmp_search_res (*filler)(struct mib_walk_state *state, struct snmp_pdu *c); enum agentx_type type; int size; } entry_leafs[] = { diff --git a/proto/snmp/mib_tree.h b/proto/snmp/mib_tree.h index 4b15e6d0..58629e57 100644 --- a/proto/snmp/mib_tree.h +++ b/proto/snmp/mib_tree.h @@ -41,7 +41,7 @@ struct mib_leaf { * AGENTX_INVALID, it is expected that filler() hook will also fill * the VarBind type. */ - enum snmp_search_res (*filler)(struct mib_walk_state *state, struct snmp_data *data); + enum snmp_search_res (*filler)(struct mib_walk_state *state, struct snmp_pdu *context); /** * call_next - signal multileaf @@ -56,7 +56,7 @@ struct mib_leaf { * Hook may be NULL meaning the leaf node is not multileaf/subtree. * */ - int (*call_next)(struct mib_walk_state *state, struct snmp_data *data); + int (*call_next)(struct mib_walk_state *state, struct snmp_pdu *context); /** * type of produced VarBind, may be replaced in packet instanciation by diff --git a/proto/snmp/snmp_utils.c b/proto/snmp/snmp_utils.c index 394df758..e3fd96ee 100644 --- a/proto/snmp/snmp_utils.c +++ b/proto/snmp/snmp_utils.c @@ -12,8 +12,9 @@ #include inline void -snmp_pdu_context(struct snmp_pdu *pdu, sock *sk) +snmp_pdu_context(struct snmp_pdu *pdu, struct snmp_proto *p, sock *sk) { + pdu->p = p; pdu->error = AGENTX_RES_NO_ERROR; pdu->buffer = sk->tpos; pdu->size = sk->tbuf + sk->tbsize - sk->tpos; @@ -1061,13 +1062,13 @@ snmp_oid_common_ancestor(const struct oid *left, const struct oid *right, struct * SNMP MIB tree walking */ struct mib_leaf * -snmp_walk_init(struct mib_tree *tree, struct mib_walk_state *walk, const struct oid *oid, struct snmp_data *data) +snmp_walk_init(struct mib_tree *tree, struct mib_walk_state *walk, const struct oid *oid, struct snmp_pdu *c) { mib_tree_walk_init(walk, tree); - snmp_vb_to_tx(data->p, oid, data->c); + snmp_vb_to_tx(c->p, oid, c); - mib_node_u *node = mib_tree_find(tree, walk, &data->c->sr_vb_start->name); + mib_node_u *node = mib_tree_find(tree, walk, &c->sr_vb_start->name); // TODO hide me in mib_tree code /* mib_tree_find() returns NULL if the oid is longer than existing any path */ @@ -1079,7 +1080,7 @@ snmp_walk_init(struct mib_tree *tree, struct mib_walk_state *walk, const struct // TODO alter the varbind struct mib_leaf * -snmp_walk_next(struct mib_tree *tree, struct mib_walk_state *walk, struct snmp_data *data) +snmp_walk_next(struct mib_tree *tree, struct mib_walk_state *walk, struct snmp_pdu *c) { ASSUME(tree && walk); @@ -1093,52 +1094,52 @@ snmp_walk_next(struct mib_tree *tree, struct mib_walk_state *walk, struct snmp_d if (mib_node_is_leaf(node) && leaf->call_next) { - const struct oid *oid = &data->c->sr_vb_start->name; + const struct oid *oid = &c->sr_vb_start->name; if (mib_tree_walk_oid_compare(walk, oid) > 0) { - int old = snmp_oid_size(&data->c->sr_vb_start->name); + int old = snmp_oid_size(&c->sr_vb_start->name); if (mib_tree_walk_to_oid(walk, - &data->c->sr_vb_start->name, 20 * sizeof(u32))) + &c->sr_vb_start->name, 20 * sizeof(u32))) { snmp_log("walk_next copy failed"); return NULL; } - int new = snmp_oid_size(&data->c->sr_vb_start->name); - data->c->buffer += (new - old); + int new = snmp_oid_size(&c->sr_vb_start->name); + c->buffer += (new - old); } - found = !leaf->call_next(walk, data); + found = !leaf->call_next(walk, c); } - else if (mib_node_is_leaf(node) && LOAD_U8(data->c->sr_vb_start->name.include)) + else if (mib_node_is_leaf(node) && LOAD_U8(c->sr_vb_start->name.include)) { found = 1; - STORE_U8(data->c->sr_vb_start->name.include, 0); + STORE_U8(c->sr_vb_start->name.include, 0); } - const struct oid *oid = &data->c->sr_vb_start->name; + const struct oid *oid = &c->sr_vb_start->name; u32 skip = (walk->id_pos < LOAD_U8(oid->n_subid)) ? LOAD_U32(oid->ids[walk->id_pos]) : 0; while (!found && (leaf = mib_tree_walk_next_leaf(tree, walk, skip)) != NULL) { /* mib_tree_walk_next() forces VarBind's name OID overwriting */ - int old = snmp_oid_size(&data->c->sr_vb_start->name); + int old = snmp_oid_size(&c->sr_vb_start->name); // TODO autogrow - if (mib_tree_walk_to_oid(walk, &data->c->sr_vb_start->name, 20 * sizeof(u32))) + if (mib_tree_walk_to_oid(walk, &c->sr_vb_start->name, 20 * sizeof(u32))) { snmp_log("walk_next copy failed"); return NULL; } - int new = snmp_oid_size(&data->c->sr_vb_start->name); - data->c->buffer += (new - old); + int new = snmp_oid_size(&c->sr_vb_start->name); + c->buffer += (new - old); - if (leaf->call_next && !leaf->call_next(walk, data)) + if (leaf->call_next && !leaf->call_next(walk, c)) found = 1; else if (!leaf->call_next) found = 1; - oid = &data->c->sr_vb_start->name; + oid = &c->sr_vb_start->name; skip = (walk->id_pos < LOAD_U8(oid->n_subid)) ? LOAD_U32(oid->ids[walk->id_pos]) : 0; } @@ -1150,9 +1151,9 @@ snmp_walk_next(struct mib_tree *tree, struct mib_walk_state *walk, struct snmp_d } enum snmp_search_res -snmp_walk_fill(struct mib_leaf *leaf, struct mib_walk_state *walk, struct snmp_data *data) +snmp_walk_fill(struct mib_leaf *leaf, struct mib_walk_state *walk, struct snmp_pdu *c) { - struct agentx_varbind *vb = data->c->sr_vb_start; + struct agentx_varbind *vb = c->sr_vb_start; if (!leaf) return SNMP_SEARCH_NO_OBJECT; @@ -1177,15 +1178,15 @@ snmp_walk_fill(struct mib_leaf *leaf, struct mib_walk_state *walk, struct snmp_d snmp_log("walk_fill got size %u based on lt %u ls %u, calling filler()", size, leaf->type, leaf->size); - if (size >= data->c->size) + if (size >= c->size) { - snmp_log("walk_fill small buffer size %d to %d", size, data->c->size); - snmp_manage_tbuf(data->p, data->c); + snmp_log("walk_fill small buffer size %d to %d", size, c->size); + snmp_manage_tbuf(c->p, c); } - enum snmp_search_res res = leaf->filler(walk, data); + enum snmp_search_res res = leaf->filler(walk, c); - vb = data->c->sr_vb_start; + vb = c->sr_vb_start; if (res != SNMP_SEARCH_OK) snmp_set_varbind_type(vb, snmp_search_res_to_type(res)); diff --git a/proto/snmp/snmp_utils.h b/proto/snmp/snmp_utils.h index 43243c68..c4b252e7 100644 --- a/proto/snmp/snmp_utils.h +++ b/proto/snmp/snmp_utils.h @@ -62,7 +62,7 @@ void snmp_varbind_duplicate_hdr(struct snmp_proto *p, struct agentx_varbind **vb */ void snmp_session(const struct snmp_proto *p, struct agentx_header *h); int snmp_has_context(const struct agentx_header *h); -void snmp_pdu_context(struct snmp_pdu *pdu, sock *sk); +void snmp_pdu_context(struct snmp_pdu *pdu, struct snmp_proto *p, sock *sk); struct oid *snmp_oid_duplicate(pool *pool, const struct oid *oid); struct oid *snmp_oid_blank(struct snmp_proto *p); @@ -114,8 +114,8 @@ enum agentx_type snmp_search_res_to_type(enum snmp_search_res res); /* * SNMP MIB tree walking */ -struct mib_leaf *snmp_walk_init(struct mib_tree *tree, struct mib_walk_state *state, const struct oid *start_rx, struct snmp_data *data); -struct mib_leaf *snmp_walk_next(struct mib_tree *tree, struct mib_walk_state *state, struct snmp_data *data); -enum snmp_search_res snmp_walk_fill(struct mib_leaf *leaf, struct mib_walk_state *state, struct snmp_data *data); +struct mib_leaf *snmp_walk_init(struct mib_tree *tree, struct mib_walk_state *state, const struct oid *start_rx, struct snmp_pdu *context); +struct mib_leaf *snmp_walk_next(struct mib_tree *tree, struct mib_walk_state *state, struct snmp_pdu *context); +enum snmp_search_res snmp_walk_fill(struct mib_leaf *leaf, struct mib_walk_state *state, struct snmp_pdu *context); #endif diff --git a/proto/snmp/subagent.c b/proto/snmp/subagent.c index d3094b5e..db77347c 100644 --- a/proto/snmp/subagent.c +++ b/proto/snmp/subagent.c @@ -169,7 +169,7 @@ snmp_simple_response(struct snmp_proto *p, enum agentx_response_errs error, u16 { sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); ASSUME(c.size >= sizeof(struct agentx_response)); @@ -193,7 +193,7 @@ open_pdu(struct snmp_proto *p, struct oid *oid) sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); #define TIMEOUT_SIZE sizeof(u32) /* 1B timeout, 3B zero padding */ @@ -247,7 +247,7 @@ snmp_notify_pdu(struct snmp_proto *p, struct oid *oid, void *data, uint size, in sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); #define UPTIME_SIZE \ sizeof( struct { u32 vb_type; u32 oid_hdr; u32 ids[4]; } ) @@ -352,7 +352,7 @@ un_register_pdu(struct snmp_proto *p, struct oid *oid, u32 bound, uint index, en const struct snmp_config *cf = SKIP_BACK(struct snmp_config, cf, p->p.cf); sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); #define BOUND_SIZE sizeof(u32) /* conditional +4 for upper-bound (optinal field) */ @@ -441,7 +441,7 @@ close_pdu(struct snmp_proto *p, enum agentx_close_reasons reason) { sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); #define REASON_SIZE sizeof(u32) if (c.size < AGENTX_HEADER_SIZE + REASON_SIZE) @@ -538,7 +538,7 @@ parse_test_set_pdu(struct snmp_proto *p, byte * const pkt_start) sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); if (c.size < AGENTX_HEADER_SIZE) { @@ -601,7 +601,7 @@ parse_sets_pdu(struct snmp_proto *p, byte * const pkt_start, enum agentx_respons } struct snmp_pdu c; - snmp_pdu_context(&c, p->sock); + snmp_pdu_context(&c, p, p->sock); if (c.size < sizeof(struct agentx_response)) { snmp_log("parse_sets_pdu small buffer"); @@ -1081,18 +1081,14 @@ void snmp_get_pdu(struct snmp_proto *p, struct snmp_pdu *c, const struct oid *o_start, struct mib_walk_state *walk) { snmp_log("snmp_get_pdu()"); - struct snmp_data d = { - .p = p, - .c = c, - }; struct mib_leaf *leaf; - leaf = snmp_walk_init(p->mib_tree, walk, o_start, &d); + leaf = snmp_walk_init(p->mib_tree, walk, o_start, c); snmp_log("found node %p", leaf); enum snmp_search_res res; - res = snmp_walk_fill(leaf, walk, &d); + res = snmp_walk_fill(leaf, walk, c); snmp_log("fill result %u", res); @@ -1103,16 +1099,11 @@ snmp_get_pdu(struct snmp_proto *p, struct snmp_pdu *c, const struct oid *o_start int snmp_get_next_pdu(struct snmp_proto *p, struct snmp_pdu *c, const struct oid *o_start, struct mib_walk_state *walk) { - struct snmp_data d = { - .p = p, - .c = c, - }; - - (void) snmp_walk_init(p->mib_tree, walk, o_start, &d); - struct mib_leaf *leaf = snmp_walk_next(p->mib_tree, walk, &d); + (void) snmp_walk_init(p->mib_tree, walk, o_start, c); + struct mib_leaf *leaf = snmp_walk_next(p->mib_tree, walk, c); enum snmp_search_res res; - res = snmp_walk_fill(leaf, walk, &d); + res = snmp_walk_fill(leaf, walk, c); if (res != SNMP_SEARCH_OK) snmp_set_varbind_type(c->sr_vb_start, AGENTX_END_OF_MIB_VIEW); @@ -1204,7 +1195,7 @@ parse_gets_pdu(struct snmp_proto *p, byte * const pkt_start) sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); /* * Get-Bulk processing stops if all the varbind have type END_OF_MIB_VIEW @@ -1396,7 +1387,7 @@ snmp_ping(struct snmp_proto *p) sock *sk = p->sock; struct snmp_pdu c; - snmp_pdu_context(&c, sk); + snmp_pdu_context(&c, p, sk); if (c.size < AGENTX_HEADER_SIZE) return; @@ -1480,11 +1471,11 @@ snmp_manage_tbuf2(struct snmp_proto *p, void **ptr, struct snmp_pdu *c) } void -snmp_tbuf_reserve(struct snmp_data *data, size_t size) +snmp_tbuf_reserve(struct snmp_pdu *c, size_t size) { - if (size > data->c->size) + if (size > c->size) { - snmp_manage_tbuf(data->p, data->c); + snmp_manage_tbuf(c->p, c); } } diff --git a/proto/snmp/subagent.h b/proto/snmp/subagent.h index 09ac3e53..21fd61d1 100644 --- a/proto/snmp/subagent.h +++ b/proto/snmp/subagent.h @@ -305,7 +305,8 @@ enum agentx_response_errs { /* SNMP PDU info */ struct snmp_pdu { - /* TX buffer */ + struct snmp_proto *p; + /* TX buffer */ byte *buffer; /* pointer to buffer */ uint size; /* unused space in buffer */ @@ -316,17 +317,6 @@ struct snmp_pdu { /* Control */ enum agentx_response_errs error; /* storage for result of current action */ u32 index; /* index on which the error was found */ - -}; - -/* - * snmp_data - Comprehensive hadle for Agentx PDU state - * @p: SNMP protocol instance - * @c: contextual data for currrently constructed AgentX PDU - */ -struct snmp_data { - struct snmp_proto *p; - struct snmp_pdu *c; }; struct snmp_packet_info {