2022-11-22 13:16:09 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- Simple Network Management Protocol (SNMP) helper functions
|
|
|
|
*
|
|
|
|
* (c) 2022 Vojtech Vilimek <vojtech.vilimek@nic.cz>
|
|
|
|
* (c) 2022 CZ.NIC z.s.p.o
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "snmp_utils.h"
|
2024-04-22 10:55:33 +00:00
|
|
|
#include <stdio.h>
|
2022-11-22 13:16:09 +00:00
|
|
|
|
2023-11-15 10:29:19 +00:00
|
|
|
inline void
|
2024-07-17 11:03:26 +00:00
|
|
|
snmp_pdu_context(struct snmp_pdu *pdu, struct snmp_proto *p, sock *sk)
|
2023-11-15 10:29:19 +00:00
|
|
|
{
|
2024-07-17 11:03:26 +00:00
|
|
|
pdu->p = p;
|
2023-11-15 10:29:19 +00:00
|
|
|
pdu->error = AGENTX_RES_NO_ERROR;
|
2024-01-23 23:23:31 +00:00
|
|
|
pdu->buffer = sk->tpos;
|
|
|
|
pdu->size = sk->tbuf + sk->tbsize - sk->tpos;
|
|
|
|
pdu->index = 0;
|
2024-05-21 13:43:41 +00:00
|
|
|
pdu->sr_vb_start = NULL;
|
|
|
|
pdu->sr_o_end = NULL;
|
2023-11-15 10:29:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
/**
|
|
|
|
* snmp_session - store packet ids from protocol to header
|
|
|
|
* @p: source SNMP protocol instance
|
|
|
|
* @h: dest PDU header
|
|
|
|
*/
|
2023-11-15 10:29:19 +00:00
|
|
|
inline void
|
|
|
|
snmp_session(const struct snmp_proto *p, struct agentx_header *h)
|
|
|
|
{
|
2024-01-10 11:21:46 +00:00
|
|
|
STORE_U32(h->session_id, p->session_id);
|
|
|
|
STORE_U32(h->transaction_id, p->transaction_id);
|
|
|
|
STORE_U32(h->packet_id, p->packet_id);
|
2023-11-15 10:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int
|
|
|
|
snmp_has_context(const struct agentx_header *h)
|
|
|
|
{
|
|
|
|
return h->flags & AGENTX_NON_DEFAULT_CONTEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void *
|
|
|
|
snmp_varbind_data(const struct agentx_varbind *vb)
|
|
|
|
{
|
|
|
|
uint name_size = snmp_oid_size(&vb->name);
|
|
|
|
return (void *)&vb->name + name_size;
|
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
struct oid *
|
|
|
|
snmp_varbind_set_name_len(struct snmp_proto *p, struct agentx_varbind **vb, u8 len, struct snmp_pdu *c)
|
|
|
|
{
|
|
|
|
struct oid *oid = &(*vb)->name;
|
|
|
|
|
|
|
|
if (LOAD_U8(oid->n_subid) >= len)
|
|
|
|
{
|
|
|
|
c->size += (LOAD_U8(oid->n_subid) - len) * sizeof(u32);
|
|
|
|
STORE_U8(oid->n_subid, len);
|
|
|
|
return oid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We need more space */
|
|
|
|
ASSUME(len >= LOAD_U8(oid->n_subid));
|
|
|
|
uint diff_size = (len - LOAD_U8(oid->n_subid)) * sizeof(u32);
|
|
|
|
if (c->size < diff_size)
|
|
|
|
{
|
2024-07-04 14:33:44 +00:00
|
|
|
snmp_log("varbind_set_name_len small buffer");
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_manage_tbuf(p, c);
|
2024-04-22 10:55:33 +00:00
|
|
|
oid = &(*vb)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(c->size >= diff_size);
|
|
|
|
c->size -= diff_size;
|
|
|
|
STORE_U8(oid->n_subid, len);
|
|
|
|
return &(*vb)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
snmp_varbind_duplicate_hdr(struct snmp_proto *p, struct agentx_varbind **vb, struct snmp_pdu *c)
|
|
|
|
{
|
|
|
|
ASSUME(vb != NULL && *vb != NULL);
|
|
|
|
uint hdr_size = snmp_varbind_header_size(*vb);
|
|
|
|
if (c->size < hdr_size)
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
|
|
|
snmp_log("varbind_duplicate small buffer");
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_manage_tbuf(p, c);
|
2024-07-04 14:33:44 +00:00
|
|
|
}
|
2024-04-22 10:55:33 +00:00
|
|
|
|
|
|
|
ASSERT(c->size >= hdr_size);
|
|
|
|
byte *buffer = c->buffer;
|
|
|
|
ADVANCE(c->buffer, c->size, hdr_size);
|
|
|
|
memcpy(buffer, *vb, hdr_size);
|
|
|
|
*vb = (struct agentx_varbind *) buffer;
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:16:09 +00:00
|
|
|
/**
|
2022-12-17 17:16:19 +00:00
|
|
|
* snmp_is_oid_empty - check if oid is null-valued
|
2022-11-22 13:16:09 +00:00
|
|
|
* @oid: object identifier to check
|
|
|
|
*
|
2024-01-23 23:23:31 +00:00
|
|
|
* Test if the oid header is full of zeroes. For NULL-pointer @oid returns 0.
|
2024-04-22 10:55:33 +00:00
|
|
|
* We ignore include field to prevent weird behaviour.
|
2022-11-22 13:16:09 +00:00
|
|
|
*/
|
2024-04-22 10:55:33 +00:00
|
|
|
inline int
|
2023-07-26 12:02:23 +00:00
|
|
|
snmp_is_oid_empty(const struct oid *oid)
|
2022-11-22 13:16:09 +00:00
|
|
|
{
|
2024-01-23 23:23:31 +00:00
|
|
|
/* We intentionaly ignore padding that should be zeroed */
|
2022-11-22 13:16:09 +00:00
|
|
|
if (oid != NULL)
|
2024-04-22 10:55:33 +00:00
|
|
|
return LOAD_U8(oid->n_subid) == 0 && LOAD_U8(oid->prefix) == 0;
|
2022-11-22 13:16:09 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
/*
|
|
|
|
* snmp_oid_is_prefixable - check for prefixed form conversion possibility
|
|
|
|
* @oid: object identfier to check
|
|
|
|
*
|
|
|
|
* Check if it is possible to convert @oid to prefixed form. The condition of
|
|
|
|
* that is standart .1.3.6.1 internet prefix and 5-th id that fits in one byte.
|
|
|
|
*/
|
|
|
|
inline int
|
|
|
|
snmp_oid_is_prefixable(const struct oid *oid)
|
|
|
|
{
|
|
|
|
if (oid->n_subid < 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
if (LOAD_U32(oid->ids[i]) != snmp_internet[i])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (LOAD_U32(oid->ids[4]) >= 256)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-22 13:16:09 +00:00
|
|
|
/**
|
|
|
|
* snmp_pkt_len - returns size of SNMP packet payload (without header)
|
|
|
|
* @buf: packet first byte
|
|
|
|
* @pkt: first byte past packet end
|
|
|
|
*/
|
2023-07-26 12:02:23 +00:00
|
|
|
uint
|
2023-10-18 11:30:14 +00:00
|
|
|
snmp_pkt_len(const byte *start, const byte *end)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
|
|
|
return (end - start) - AGENTX_HEADER_SIZE;
|
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* snmp_oid_copy - copy OID from one place to another
|
|
|
|
* @dest: destination to use
|
|
|
|
* @src: OID to be copied from
|
2023-07-26 12:02:23 +00:00
|
|
|
*/
|
2023-10-18 11:30:14 +00:00
|
|
|
void
|
|
|
|
snmp_oid_copy(struct oid *dest, const struct oid *src)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
|
|
|
STORE_U8(dest->n_subid, src->n_subid);
|
2024-01-23 23:23:31 +00:00
|
|
|
STORE_U8(dest->prefix, src->prefix);
|
2023-09-04 11:58:59 +00:00
|
|
|
STORE_U8(dest->include, src->include ? 1 : 0);
|
2024-01-23 23:23:31 +00:00
|
|
|
STORE_U8(dest->reserved, 0);
|
2023-07-26 12:02:23 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
for (int i = 0; i < LOAD_U8(src->n_subid); i++)
|
2023-07-26 12:02:23 +00:00
|
|
|
STORE_U32(dest->ids[i], src->ids[i]);
|
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
/* this function assumes enougth space inside dest is allocated */
|
|
|
|
void
|
|
|
|
snmp_oid_copy2(struct oid *dest, const struct oid *src)
|
|
|
|
{
|
|
|
|
/* The STORE_U8() and LOAD_U8() cancel out */
|
|
|
|
dest->n_subid = src->n_subid;
|
|
|
|
dest->prefix = src->prefix;
|
|
|
|
dest->include = src->include ? 1 : 0;
|
|
|
|
dest->reserved = 0;
|
|
|
|
|
|
|
|
/* The STORE_U32() and LOAD_U32 cancel out */
|
|
|
|
memcpy(dest->ids, src->ids, LOAD_U8(src->n_subid) * sizeof(u32));
|
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* snmp_oid_duplicate - duplicate an OID from memory pool
|
|
|
|
* @pool: pool to use
|
|
|
|
* @oid: OID to be duplicated
|
2023-07-26 12:02:23 +00:00
|
|
|
*/
|
|
|
|
struct oid *
|
|
|
|
snmp_oid_duplicate(pool *pool, const struct oid *oid)
|
2022-11-22 13:16:09 +00:00
|
|
|
{
|
2023-07-26 12:02:23 +00:00
|
|
|
struct oid *res = mb_alloc(pool, snmp_oid_size(oid));
|
|
|
|
memcpy(res, oid, snmp_oid_size(oid));
|
|
|
|
return res;
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 17:08:00 +00:00
|
|
|
/**
|
2024-01-23 23:23:31 +00:00
|
|
|
* snmp_oid_blank - create new null oid (blank)
|
2022-12-10 17:08:00 +00:00
|
|
|
* @p: pool hodling snmp_proto structure
|
|
|
|
*/
|
|
|
|
struct oid *
|
|
|
|
snmp_oid_blank(struct snmp_proto *p)
|
|
|
|
{
|
|
|
|
return mb_allocz(p->p.pool, sizeof(struct oid));
|
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/**
|
2024-01-23 23:23:31 +00:00
|
|
|
* snmp_str_size_from_len - return in-buffer octet string size
|
2024-01-10 11:21:46 +00:00
|
|
|
* @len: length of C-string, returned from strlen()
|
|
|
|
*/
|
2024-01-23 23:23:31 +00:00
|
|
|
inline size_t
|
2023-07-26 12:02:23 +00:00
|
|
|
snmp_str_size_from_len(uint len)
|
|
|
|
{
|
|
|
|
return 4 + BIRD_ALIGN(len, 4);
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:16:09 +00:00
|
|
|
/**
|
|
|
|
* snmp_str_size - return in packet size of supplied string
|
|
|
|
* @str: measured string
|
|
|
|
*
|
|
|
|
* Returned value is string length aligned to 4 byte with 32bit length
|
|
|
|
* annotation included.
|
|
|
|
*/
|
2023-07-26 12:02:23 +00:00
|
|
|
inline size_t
|
2022-11-22 13:16:09 +00:00
|
|
|
snmp_str_size(const char *str)
|
|
|
|
{
|
2023-07-26 12:02:23 +00:00
|
|
|
return snmp_str_size_from_len(strlen(str));
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* snmp_oid_size - measure size of oid in bytes
|
|
|
|
* @o: object identifier to use
|
|
|
|
*/
|
|
|
|
uint
|
2023-07-26 12:02:23 +00:00
|
|
|
snmp_oid_size(const struct oid *o)
|
2022-11-22 13:16:09 +00:00
|
|
|
{
|
2024-01-23 23:23:31 +00:00
|
|
|
return 4 + (LOAD_U8(o->n_subid) * 4);
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 17:08:00 +00:00
|
|
|
/**
|
|
|
|
* snmp_get_size - calculate size for allocation
|
|
|
|
* @n_subid: number of ids in oid
|
|
|
|
*/
|
|
|
|
inline size_t
|
2024-01-23 23:23:31 +00:00
|
|
|
snmp_oid_size_from_len(uint n_subid)
|
2022-12-10 17:08:00 +00:00
|
|
|
{
|
|
|
|
return sizeof(struct oid) + n_subid * sizeof(u32);
|
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* snmp_varbind_hdr_size_from_oid - return in-buffer size of VarBind
|
|
|
|
* @oid: OID used as VarBind's name
|
|
|
|
*
|
|
|
|
* This function assume @oid to be not NULL.
|
|
|
|
*/
|
|
|
|
uint
|
|
|
|
snmp_varbind_hdr_size_from_oid(const struct oid *oid)
|
|
|
|
{
|
|
|
|
ASSUME(oid);
|
|
|
|
return snmp_oid_size(oid) + OFFSETOF(struct agentx_varbind, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* snmp_set_varbind_type - set VarBind's type field
|
|
|
|
* @vb: Varbind inside TX-buffer
|
|
|
|
* @t: a valid type to be set
|
|
|
|
*
|
|
|
|
* This function assumes valid @t.
|
|
|
|
*/
|
2024-05-24 13:20:30 +00:00
|
|
|
inline enum snmp_search_res
|
2024-01-10 11:21:46 +00:00
|
|
|
snmp_set_varbind_type(struct agentx_varbind *vb, enum agentx_type t)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
2024-01-10 11:21:46 +00:00
|
|
|
ASSUME(t != AGENTX_INVALID);
|
|
|
|
STORE_U16(vb->type, t);
|
2024-04-22 10:55:33 +00:00
|
|
|
STORE_U16(vb->reserved, 0);
|
2024-05-24 13:20:30 +00:00
|
|
|
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case AGENTX_END_OF_MIB_VIEW:
|
|
|
|
return SNMP_SEARCH_END_OF_VIEW;
|
|
|
|
case AGENTX_NO_SUCH_OBJECT:
|
|
|
|
return SNMP_SEARCH_NO_OBJECT;
|
|
|
|
case AGENTX_NO_SUCH_INSTANCE:
|
|
|
|
return SNMP_SEARCH_NO_INSTANCE;
|
|
|
|
|
|
|
|
/* valid varbind types */
|
|
|
|
case AGENTX_INTEGER:
|
|
|
|
case AGENTX_OCTET_STRING:
|
|
|
|
case AGENTX_NULL:
|
|
|
|
case AGENTX_OBJECT_ID:
|
|
|
|
case AGENTX_IP_ADDRESS:
|
|
|
|
case AGENTX_COUNTER_32:
|
|
|
|
case AGENTX_GAUGE_32:
|
|
|
|
case AGENTX_TIME_TICKS:
|
|
|
|
case AGENTX_OPAQUE:
|
|
|
|
case AGENTX_COUNTER_64:
|
|
|
|
return SNMP_SEARCH_OK;
|
|
|
|
|
|
|
|
default:
|
2024-07-04 14:33:44 +00:00
|
|
|
die("invalid varbind type %d", (int) t);
|
2024-05-24 13:20:30 +00:00
|
|
|
}
|
2024-01-10 11:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal wrapper */
|
|
|
|
static inline u16
|
|
|
|
snmp_load_varbind_type(const struct agentx_varbind *vb)
|
|
|
|
{
|
|
|
|
return LOAD_U16(vb->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* snmp_get_varbind_type - loads a VarBind type
|
|
|
|
* @vb: VarBind pointer to TX-buffer
|
|
|
|
*
|
|
|
|
* This function assumes VarBind with valid type, always call snmp_test_varbind
|
|
|
|
* for in TX-buffer VarBinds!
|
|
|
|
*/
|
|
|
|
inline enum agentx_type
|
|
|
|
snmp_get_varbind_type(const struct agentx_varbind *vb)
|
|
|
|
{
|
|
|
|
ASSUME(snmp_test_varbind(vb));
|
|
|
|
return (enum agentx_type) snmp_load_varbind_type(vb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint
|
|
|
|
snmp_get_octet_size(const struct agentx_octet_str *str)
|
|
|
|
{
|
|
|
|
return LOAD_U32(str->length);
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:16:09 +00:00
|
|
|
/**
|
2024-01-10 11:21:46 +00:00
|
|
|
* snmp_varbind_header_size - measure size of VarBind without data in bytes
|
|
|
|
* @vb: VarBind to use
|
|
|
|
*
|
|
|
|
* Return size including whole OID as well as the VarBind header.
|
2022-11-22 13:16:09 +00:00
|
|
|
*/
|
|
|
|
uint
|
2024-01-10 11:21:46 +00:00
|
|
|
snmp_varbind_header_size(const struct agentx_varbind *vb)
|
2022-11-22 13:16:09 +00:00
|
|
|
{
|
2023-07-26 12:02:23 +00:00
|
|
|
return snmp_varbind_hdr_size_from_oid(&vb->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint
|
2024-01-10 11:21:46 +00:00
|
|
|
snmp_varbind_size_unsafe(const struct agentx_varbind *vb)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
2024-01-10 11:21:46 +00:00
|
|
|
ASSUME(snmp_test_varbind(vb));
|
2023-07-26 12:02:23 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
enum agentx_type type = snmp_get_varbind_type(vb);
|
|
|
|
int value_size = agentx_type_size(type);
|
2023-07-26 12:02:23 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
uint vb_header = snmp_varbind_header_size(vb);
|
2023-07-26 12:02:23 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
if (value_size == 0)
|
|
|
|
return vb_header;
|
2023-07-26 12:02:23 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
if (value_size > 0)
|
|
|
|
return vb_header + value_size;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case AGENTX_OBJECT_ID:;
|
|
|
|
struct oid *oid = snmp_varbind_data(vb);
|
|
|
|
return vb_header + snmp_oid_size(oid);
|
|
|
|
|
|
|
|
case AGENTX_OCTET_STRING:
|
|
|
|
case AGENTX_IP_ADDRESS:
|
|
|
|
case AGENTX_OPAQUE:;
|
|
|
|
struct agentx_octet_str *string = snmp_varbind_data(vb);
|
|
|
|
return vb_header + snmp_get_octet_size(string);
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Shouldn't happen */
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/**
|
|
|
|
* snmp_varbind_size - get size of in-buffer VarBind
|
|
|
|
* @vb: VarBind to measure
|
|
|
|
* @limit: upper limit of bytes that can be used
|
|
|
|
*
|
|
|
|
* This functions assumes valid VarBind type.
|
|
|
|
* Return 0 for Varbinds longer than limit, Varbind's size otherwise.
|
|
|
|
*/
|
|
|
|
uint
|
|
|
|
snmp_varbind_size(const struct agentx_varbind *vb, uint limit)
|
|
|
|
{
|
|
|
|
ASSUME(snmp_test_varbind(vb));
|
|
|
|
|
|
|
|
if (limit < sizeof(struct agentx_varbind))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
enum agentx_type type = agentx_type_size(snmp_get_varbind_type(vb));
|
|
|
|
int s = agentx_type_size(type);
|
|
|
|
uint vb_header = snmp_varbind_header_size(vb);
|
|
|
|
|
|
|
|
if (limit < vb_header)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s == 0)
|
|
|
|
return vb_header;
|
|
|
|
|
|
|
|
if (s > 0 && vb_header + s <= limit)
|
|
|
|
return vb_header + s;
|
|
|
|
else if (s > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case AGENTX_OBJECT_ID:;
|
|
|
|
struct oid *oid = snmp_varbind_data(vb);
|
|
|
|
return vb_header + snmp_oid_size(oid);
|
|
|
|
|
|
|
|
case AGENTX_OCTET_STRING:
|
|
|
|
case AGENTX_IP_ADDRESS:
|
|
|
|
case AGENTX_OPAQUE:;
|
|
|
|
struct agentx_octet_str *os = snmp_varbind_data(vb);
|
|
|
|
return vb_header + snmp_get_octet_size(os);
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* This should not happen */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
/**
|
|
|
|
* snmp_varbind_size_from_len - get size in-buffer VarBind for known OID and data
|
|
|
|
* @n_subid: number of subidentifiers of the VarBind's OID name
|
|
|
|
* @type: type of VarBind
|
|
|
|
* @len: length of variably long data
|
|
|
|
*
|
|
|
|
* For types with fixed size the @len is not used. For types such as Octet
|
|
|
|
* String, or OID the @len is used directly.
|
|
|
|
*
|
|
|
|
* Return number of bytes used by VarBind in specified form.
|
|
|
|
*/
|
|
|
|
inline size_t
|
|
|
|
snmp_varbind_size_from_len(uint n_subid, enum agentx_type type, uint len)
|
|
|
|
{
|
|
|
|
size_t sz = snmp_oid_size_from_len(n_subid)
|
|
|
|
+ sizeof(struct agentx_varbind) - sizeof(struct oid);
|
|
|
|
|
|
|
|
int data_sz = agentx_type_size(type);
|
|
|
|
if (data_sz < 0)
|
|
|
|
sz += len;
|
|
|
|
else
|
|
|
|
sz += data_sz;
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* snmp_test_varbind - test validity of VarBind's type
|
|
|
|
* @vb: VarBind to test
|
|
|
|
*/
|
2023-10-18 11:30:14 +00:00
|
|
|
int
|
|
|
|
snmp_test_varbind(const struct agentx_varbind *vb)
|
|
|
|
{
|
2024-01-10 11:21:46 +00:00
|
|
|
ASSUME(vb);
|
|
|
|
|
|
|
|
u16 type = snmp_load_varbind_type(vb);
|
|
|
|
if (type == AGENTX_INTEGER ||
|
|
|
|
type == AGENTX_OCTET_STRING ||
|
|
|
|
type == AGENTX_NULL ||
|
|
|
|
type == AGENTX_OBJECT_ID ||
|
|
|
|
type == AGENTX_IP_ADDRESS ||
|
|
|
|
type == AGENTX_COUNTER_32 ||
|
|
|
|
type == AGENTX_GAUGE_32 ||
|
|
|
|
type == AGENTX_TIME_TICKS ||
|
|
|
|
type == AGENTX_OPAQUE ||
|
|
|
|
type == AGENTX_COUNTER_64 ||
|
|
|
|
type == AGENTX_NO_SUCH_OBJECT ||
|
|
|
|
type == AGENTX_NO_SUCH_INSTANCE ||
|
|
|
|
type == AGENTX_END_OF_MIB_VIEW)
|
2023-10-18 11:30:14 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* snmp_create_varbind - create a null-typed VarBind in buffer
|
|
|
|
* @buf: buffer to use
|
|
|
|
*/
|
|
|
|
struct agentx_varbind *
|
|
|
|
snmp_create_varbind_null(byte *buf)
|
|
|
|
{
|
|
|
|
struct oid o = { 0 };
|
|
|
|
struct agentx_varbind *vb = snmp_create_varbind(buf, &o);
|
|
|
|
snmp_set_varbind_type(vb, AGENTX_NULL);
|
|
|
|
return vb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* snmp_create_varbind - initialize in-buffer non-typed VarBind
|
|
|
|
* @buf: pointer to first unused buffer byte
|
|
|
|
* @oid: OID to use as VarBind name
|
|
|
|
*/
|
2022-11-22 13:16:09 +00:00
|
|
|
struct agentx_varbind *
|
|
|
|
snmp_create_varbind(byte *buf, struct oid *oid)
|
|
|
|
{
|
2024-01-10 11:21:46 +00:00
|
|
|
struct agentx_varbind *vb = (void *) buf;
|
2024-01-23 23:23:31 +00:00
|
|
|
STORE_U16(vb->reserved, 0);
|
2023-08-08 15:00:20 +00:00
|
|
|
snmp_oid_copy(&vb->name, oid);
|
2022-11-22 13:16:09 +00:00
|
|
|
return vb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* snmp_oid_ip4_index - check IPv4 address validity in oid
|
|
|
|
* @o: object identifier holding ip address
|
|
|
|
* @start: index of first address id
|
|
|
|
*/
|
|
|
|
int
|
2023-07-26 12:02:23 +00:00
|
|
|
snmp_valid_ip4_index(const struct oid *o, uint start)
|
2022-11-22 13:16:09 +00:00
|
|
|
{
|
2024-01-23 23:23:31 +00:00
|
|
|
if (start + 3 < LOAD_U8(o->n_subid))
|
2022-11-22 13:16:09 +00:00
|
|
|
return snmp_valid_ip4_index_unsafe(o, start);
|
|
|
|
else
|
2023-07-26 12:34:01 +00:00
|
|
|
return 0;
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* snmp_valid_ip4_index_unsafe - check validity of IPv4 address in oid
|
|
|
|
* @o: object identifier holding ip address
|
|
|
|
* @start: index of first address id
|
|
|
|
*
|
|
|
|
* This function is unsafe - no checks of object identifier ids
|
|
|
|
* length sufficiency is done.
|
|
|
|
*/
|
|
|
|
int
|
2023-07-26 12:02:23 +00:00
|
|
|
snmp_valid_ip4_index_unsafe(const struct oid *o, uint start)
|
2022-11-22 13:16:09 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; i++)
|
2024-01-23 23:23:31 +00:00
|
|
|
if (LOAD_U32(o->ids[start + i]) >= 256)
|
2023-07-26 12:34:01 +00:00
|
|
|
return 0;
|
2023-03-24 14:00:54 +00:00
|
|
|
|
2023-07-26 12:34:01 +00:00
|
|
|
return 1;
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* snmp_put_nstr - copy c-string into buffer with limit
|
|
|
|
* @buf: destination buffer
|
|
|
|
* @str: string to use
|
|
|
|
* @len: number of characters to use from string
|
|
|
|
*/
|
2023-07-26 12:02:23 +00:00
|
|
|
byte *
|
|
|
|
snmp_put_nstr(byte *buf, const char *str, uint len)
|
|
|
|
{
|
|
|
|
uint alen = BIRD_ALIGN(len, 4);
|
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
struct agentx_octet_str *octet = (void *) buf;
|
|
|
|
STORE_U32(octet->length, len);
|
|
|
|
memcpy(&octet->data, str, len);
|
|
|
|
buf += len + sizeof(octet->length);
|
2023-07-26 12:02:23 +00:00
|
|
|
|
|
|
|
/* Insert zero padding in the gap at the end */
|
|
|
|
for (uint i = 0; i < alen - len; i++)
|
2024-01-10 11:21:46 +00:00
|
|
|
STORE_U8(buf[i], '\0');
|
2023-07-26 12:02:23 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
return buf + (alen - len);
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:16:09 +00:00
|
|
|
/**
|
|
|
|
* snmp_put_str - put string into SNMP PDU transcieve buffer
|
|
|
|
* @buf: pointer to first unoccupied buffer byte
|
|
|
|
* @str: string to place
|
|
|
|
*
|
|
|
|
* Handles all conditions specified by RFC, namely string length annotation
|
|
|
|
* and padding 4 byte alignment with zeroes. Return NULL if string is too large
|
|
|
|
* for SNMP message.
|
|
|
|
*/
|
|
|
|
byte *
|
|
|
|
snmp_put_str(byte *buf, const char *str)
|
|
|
|
{
|
|
|
|
uint len = strlen(str);
|
2023-07-26 12:02:23 +00:00
|
|
|
return snmp_put_nstr(buf, str, len);
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 14:00:54 +00:00
|
|
|
byte *
|
2023-07-26 12:02:23 +00:00
|
|
|
snmp_put_ip4(byte *buf, ip4_addr addr)
|
2023-03-24 14:00:54 +00:00
|
|
|
{
|
|
|
|
/* octet string has size 4 bytes */
|
2024-04-22 10:55:33 +00:00
|
|
|
STATIC_ASSERT(sizeof(ip4_addr) == sizeof(u32));
|
|
|
|
STORE_PTR(buf, sizeof(ip4_addr));
|
2023-03-24 14:00:54 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
/* Always use Network byte order */
|
2023-07-26 12:02:23 +00:00
|
|
|
put_u32(buf+4, ip4_to_u32(addr));
|
2023-03-24 14:00:54 +00:00
|
|
|
|
|
|
|
return buf + 8;
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:16:09 +00:00
|
|
|
byte *
|
|
|
|
snmp_put_blank(byte *buf)
|
|
|
|
{
|
|
|
|
STORE_PTR(buf, 0);
|
|
|
|
return buf + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* snmp_put_oid - put oid into SNMP PDU transcieve buffer
|
|
|
|
* @buf: pointer to first free buffer byte
|
|
|
|
* @oid: object identifier to use
|
|
|
|
*/
|
|
|
|
byte *
|
|
|
|
snmp_put_oid(byte *buf, struct oid *oid)
|
|
|
|
{
|
2023-09-04 11:58:59 +00:00
|
|
|
struct oid *oid_buf = (void *) buf;
|
|
|
|
snmp_oid_copy(oid_buf, oid);
|
|
|
|
return buf + snmp_oid_size(oid);
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* snmp_put_fbyte - put one padded byte to SNMP PDU transcieve buffer
|
|
|
|
* @buf: pointer to free buffer byte
|
|
|
|
* @data: byte to use
|
|
|
|
*
|
|
|
|
* Put @data into buffer @buf with 3B zeroed padding.
|
|
|
|
*/
|
|
|
|
byte *
|
|
|
|
snmp_put_fbyte(byte *buf, u8 data)
|
|
|
|
{
|
2024-01-10 11:21:46 +00:00
|
|
|
STORE_U8(*buf++, data);
|
|
|
|
memset(buf, 0, 3); /* we fill the 24bit padding with zeros */
|
2022-11-22 13:16:09 +00:00
|
|
|
return buf + 3;
|
|
|
|
}
|
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
/*
|
|
|
|
* snmp_oid_ip4_index - OID append IPv4 index
|
|
|
|
* @o: OID to use
|
|
|
|
* @start: index of IP addr's MSB
|
|
|
|
* @addr: IPv4 address to use
|
|
|
|
*
|
|
|
|
* The indices from start to (inclusive) start+3 are overwritten by @addr bytes.
|
|
|
|
*/
|
2022-11-22 13:16:09 +00:00
|
|
|
void
|
|
|
|
snmp_oid_ip4_index(struct oid *o, uint start, ip4_addr addr)
|
|
|
|
{
|
|
|
|
u32 temp = ip4_to_u32(addr);
|
2023-07-26 12:02:23 +00:00
|
|
|
STORE_U32(o->ids[start], temp >> 24);
|
|
|
|
STORE_U32(o->ids[start + 1], (temp >> 16) & 0xFF);
|
|
|
|
STORE_U32(o->ids[start + 2], (temp >> 8) & 0xFF);
|
|
|
|
STORE_U32(o->ids[start + 3], temp & 0xFF);
|
2022-11-22 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 15:30:20 +00:00
|
|
|
|
2024-07-04 14:33:44 +00:00
|
|
|
/**
|
|
|
|
* snmp_oid_compare - find the lexicographical order relation between @left and @right
|
2022-11-29 15:30:20 +00:00
|
|
|
* @left: left object id relation operant
|
|
|
|
* @right: right object id relation operant
|
|
|
|
*
|
2024-07-04 14:33:44 +00:00
|
|
|
* both @left and @right has to be non-blank.
|
2023-03-24 14:00:54 +00:00
|
|
|
* function returns 0 if left == right,
|
|
|
|
* -1 if left < right,
|
|
|
|
* and 1 otherwise
|
2022-11-29 15:30:20 +00:00
|
|
|
*/
|
|
|
|
int
|
2023-07-26 12:02:23 +00:00
|
|
|
snmp_oid_compare(const struct oid *left, const struct oid *right)
|
2022-11-29 15:30:20 +00:00
|
|
|
{
|
2024-04-22 10:55:33 +00:00
|
|
|
const u8 left_subids = LOAD_U8(left->n_subid);
|
2024-07-09 14:56:16 +00:00
|
|
|
u8 right_subids = LOAD_U8(right->n_subid); /* see hack for more info */
|
2024-04-22 10:55:33 +00:00
|
|
|
|
|
|
|
const u8 left_prefix = LOAD_U8(left->prefix);
|
|
|
|
const u8 right_prefix = LOAD_U8(right->prefix);
|
|
|
|
|
|
|
|
if (left_prefix == 0 && right_prefix == 0)
|
2022-11-29 15:30:20 +00:00
|
|
|
goto test_ids;
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
if (right_prefix == 0)
|
2023-03-24 14:00:54 +00:00
|
|
|
return (-1) * snmp_oid_compare(right, left);
|
2022-11-29 15:30:20 +00:00
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
if (left_prefix == 0)
|
2022-11-29 15:30:20 +00:00
|
|
|
{
|
2024-07-09 14:30:04 +00:00
|
|
|
uint bound = MIN((uint) left_subids, (uint) ARRAY_SIZE(snmp_internet));
|
|
|
|
for (uint idx = 0; idx < bound; idx++)
|
2024-04-22 10:55:33 +00:00
|
|
|
{
|
|
|
|
u32 id = LOAD_U32(left->ids[idx]);
|
|
|
|
if (id < snmp_internet[idx])
|
2022-11-29 15:30:20 +00:00
|
|
|
return -1;
|
2024-04-22 10:55:33 +00:00
|
|
|
else if (id > snmp_internet[idx])
|
2022-11-29 15:30:20 +00:00
|
|
|
return 1;
|
2024-04-22 10:55:33 +00:00
|
|
|
}
|
2022-11-29 15:30:20 +00:00
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
if (left_subids <= ARRAY_SIZE(snmp_internet))
|
|
|
|
return -1;
|
|
|
|
|
2024-05-21 13:43:41 +00:00
|
|
|
/* check prefix */
|
2024-04-22 10:55:33 +00:00
|
|
|
if (LOAD_U32(left->ids[4]) < (u32) right_prefix)
|
|
|
|
return -1;
|
2024-05-24 13:20:30 +00:00
|
|
|
else if (LOAD_U32(left->ids[4]) > (u32) right_prefix)
|
2024-04-22 10:55:33 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-05-21 13:43:41 +00:00
|
|
|
/* the right prefix is already checked (+1) */
|
|
|
|
int limit = MIN(left_subids - (int) (ARRAY_SIZE(snmp_internet) + 1),
|
2024-04-22 10:55:33 +00:00
|
|
|
(int) right_subids);
|
|
|
|
for (int i = 0; i < limit; i++)
|
|
|
|
{
|
2024-05-21 13:43:41 +00:00
|
|
|
u32 left_id = LOAD_U32(left->ids[i + ARRAY_SIZE(snmp_internet) + 1]);
|
2024-04-22 10:55:33 +00:00
|
|
|
u32 right_id = LOAD_U32(right->ids[i]);
|
|
|
|
if (left_id < right_id)
|
2022-11-29 15:30:20 +00:00
|
|
|
return -1;
|
2024-04-22 10:55:33 +00:00
|
|
|
else if (left_id > right_id)
|
2022-11-29 15:30:20 +00:00
|
|
|
return 1;
|
2024-04-22 10:55:33 +00:00
|
|
|
}
|
2022-11-29 15:30:20 +00:00
|
|
|
|
2024-05-21 13:43:41 +00:00
|
|
|
/* hack: we known at this point that right has >= 5 subids
|
|
|
|
* (implicit in snmp_internet and oid->prefix), so
|
|
|
|
* we simplify to common case by altering left_subids */
|
|
|
|
right_subids += 5;
|
2022-11-29 15:30:20 +00:00
|
|
|
goto all_same;
|
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
if (left_prefix < right_prefix)
|
2022-11-29 15:30:20 +00:00
|
|
|
return -1;
|
2024-04-22 10:55:33 +00:00
|
|
|
else if (left_prefix > right_prefix)
|
2022-11-29 15:30:20 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
test_ids:
|
|
|
|
for (int i = 0; i < MIN(left->n_subid, right->n_subid); i++)
|
2024-04-22 10:55:33 +00:00
|
|
|
{
|
|
|
|
u32 left_id = LOAD_U32(left->ids[i]);
|
|
|
|
u32 right_id = LOAD_U32(right->ids[i]);
|
|
|
|
if (left_id < right_id)
|
2022-11-29 15:30:20 +00:00
|
|
|
return -1;
|
2024-04-22 10:55:33 +00:00
|
|
|
else if (left_id > right_id)
|
2022-11-29 15:30:20 +00:00
|
|
|
return 1;
|
2024-04-22 10:55:33 +00:00
|
|
|
}
|
2022-11-29 15:30:20 +00:00
|
|
|
|
|
|
|
all_same:
|
|
|
|
/* shorter sequence is before longer in lexicografical order */
|
2024-04-22 10:55:33 +00:00
|
|
|
if (left_subids < right_subids)
|
2022-11-29 15:30:20 +00:00
|
|
|
return -1;
|
2024-04-22 10:55:33 +00:00
|
|
|
else if (left_subids > right_subids)
|
2022-11-29 15:30:20 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2022-12-10 17:08:00 +00:00
|
|
|
|
2023-11-15 14:03:55 +00:00
|
|
|
struct snmp_registration *
|
|
|
|
snmp_registration_create(struct snmp_proto *p, u8 mib_class)
|
2022-12-10 17:08:00 +00:00
|
|
|
{
|
2023-11-15 14:03:55 +00:00
|
|
|
struct snmp_registration *r;
|
|
|
|
r = mb_alloc(p->p.pool, sizeof(struct snmp_registration));
|
2022-12-10 17:08:00 +00:00
|
|
|
|
|
|
|
r->n.prev = r->n.next = NULL;
|
|
|
|
|
|
|
|
r->session_id = p->session_id;
|
2023-11-15 11:37:10 +00:00
|
|
|
/* will be incremented by snmp_session() macro during packet assembly */
|
2022-12-10 17:08:00 +00:00
|
|
|
r->transaction_id = p->transaction_id;
|
2023-08-08 15:00:20 +00:00
|
|
|
r->packet_id = p->packet_id + 1;
|
2024-07-04 14:33:44 +00:00
|
|
|
snmp_log("using registration packet_id %u", r->packet_id);
|
2022-12-10 17:08:00 +00:00
|
|
|
|
|
|
|
r->mib_class = mib_class;
|
|
|
|
|
2023-11-15 14:03:55 +00:00
|
|
|
add_tail(&p->registration_queue, &r->n);
|
2023-10-25 14:57:46 +00:00
|
|
|
|
2022-12-10 17:08:00 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-11-15 14:03:55 +00:00
|
|
|
snmp_registration_match(struct snmp_registration *r, struct agentx_header *h, u8 class)
|
2022-12-10 17:08:00 +00:00
|
|
|
{
|
2024-07-04 14:33:44 +00:00
|
|
|
snmp_log("snmp_reg_same() r->packet_id %u p->packet_id %u", r->packet_id, h->packet_id);
|
2022-12-10 17:08:00 +00:00
|
|
|
return
|
|
|
|
(r->mib_class == class) &&
|
|
|
|
(r->session_id == h->session_id) &&
|
|
|
|
(r->transaction_id == h->transaction_id) &&
|
|
|
|
(r->packet_id == h->packet_id);
|
|
|
|
}
|
|
|
|
|
2022-12-17 17:16:19 +00:00
|
|
|
|
2023-10-18 16:06:24 +00:00
|
|
|
void UNUSED
|
|
|
|
snmp_dump_packet(byte UNUSED *pkt, uint size)
|
2022-12-17 17:16:19 +00:00
|
|
|
{
|
2023-11-15 14:03:55 +00:00
|
|
|
DBG("dump");
|
2023-03-14 13:09:45 +00:00
|
|
|
for (uint i = 0; i < size; i += 4)
|
2023-11-15 14:03:55 +00:00
|
|
|
DBG("pkt [%d] 0x%02x%02x%02x%02x", i, pkt[i],pkt[i+1],pkt[i+2],pkt[i+3]);
|
|
|
|
DBG("end dump");
|
2022-12-10 17:08:00 +00:00
|
|
|
}
|
2023-07-26 12:02:23 +00:00
|
|
|
|
|
|
|
/*
|
2024-01-10 11:21:46 +00:00
|
|
|
* agentx_type_size - get in packet VarBind type size
|
|
|
|
* @type: VarBind type
|
|
|
|
*
|
|
|
|
* Returns length of agentx_type @type in bytes, Variable length types result in
|
|
|
|
* -1.
|
2023-07-26 12:02:23 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
agentx_type_size(enum agentx_type type)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* AGENTX_NULL, AGENTX_NO_SUCH_OBJECT, AGENTX_NO_SUCH_INSTANCE,
|
|
|
|
* AGENTX_END_OF_MIB_VIEW
|
|
|
|
*/
|
|
|
|
if (type >= AGENTX_NO_SUCH_OBJECT || type == AGENTX_NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* AGENTX_INTEGER, AGENTX_COUNTER_32, AGENTX_GAUGE_32, AGENTX_TIME_TICKS */
|
|
|
|
if (type >= AGENTX_COUNTER_32 && type <= AGENTX_TIME_TICKS ||
|
|
|
|
type == AGENTX_INTEGER)
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
if (type == AGENTX_COUNTER_64)
|
|
|
|
return 8;
|
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
if (AGENTX_IP_ADDRESS)
|
|
|
|
return snmp_str_size_from_len(4);
|
|
|
|
|
|
|
|
/* AGENTX_OBJECT_ID, AGENTX_OCTET_STRING, AGENTX_OPAQUE */
|
2023-07-26 12:02:23 +00:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
static inline void
|
|
|
|
snmp_varbind_type32(struct agentx_varbind *vb, struct snmp_pdu *c, enum agentx_type type, u32 val)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
2024-04-22 10:55:33 +00:00
|
|
|
ASSUME(agentx_type_size(type) == 4); /* type as 4B representation */
|
2023-07-26 12:02:23 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
snmp_set_varbind_type(vb, type);
|
2023-11-15 10:29:19 +00:00
|
|
|
u32 *data = snmp_varbind_data(vb);
|
2024-04-22 10:55:33 +00:00
|
|
|
STORE_PTR(data, val);
|
2024-01-10 11:21:46 +00:00
|
|
|
data++;
|
2024-04-22 10:55:33 +00:00
|
|
|
c->buffer = (byte *) data;
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
inline void
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_int(struct snmp_pdu *c, u32 val)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_type32(c->sr_vb_start, c, AGENTX_INTEGER, val);
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
inline void
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_counter32(struct snmp_pdu *c, u32 val)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_type32(c->sr_vb_start, c, AGENTX_COUNTER_32, val);
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
inline void
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_ticks(struct snmp_pdu *c, u32 val)
|
2023-08-08 15:00:20 +00:00
|
|
|
{
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_type32(c->sr_vb_start, c, AGENTX_TIME_TICKS, val);
|
2023-08-08 15:00:20 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
inline void
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_gauge32(struct snmp_pdu *c, s64 time)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_type32(c->sr_vb_start, c,
|
|
|
|
AGENTX_GAUGE_32, MAX(0, MIN(time, UINT32_MAX)));
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
inline void
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_ip4(struct snmp_pdu *c, ip4_addr addr)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_set_varbind_type(c->sr_vb_start, AGENTX_IP_ADDRESS);
|
|
|
|
c->buffer = snmp_put_ip4(snmp_varbind_data(c->sr_vb_start), addr);
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline byte *
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_nstr2(struct snmp_pdu *c, uint size, const char *str, uint len)
|
2023-07-26 12:02:23 +00:00
|
|
|
{
|
|
|
|
if (size < snmp_str_size_from_len(len))
|
|
|
|
return NULL;
|
|
|
|
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_set_varbind_type(c->sr_vb_start, AGENTX_OCTET_STRING);
|
|
|
|
return snmp_put_nstr(snmp_varbind_data(c->sr_vb_start), str, len);
|
2023-07-26 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 10:55:33 +00:00
|
|
|
/*
|
|
|
|
* snmp_varbind_nstr - fill varbind context with octet string
|
|
|
|
* @vb: VarBind to use
|
|
|
|
* @c: PDU information
|
|
|
|
* @str: C-string to put as the VarBind data
|
|
|
|
* @len: length of the string @str
|
|
|
|
*
|
|
|
|
* Beware: this function assumes there is enough space in the underlaying
|
|
|
|
* TX-buffer. The caller has to provide that, see snmp_str_size_from_len() for
|
|
|
|
* more info.
|
|
|
|
*/
|
|
|
|
void
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_varbind_nstr(struct snmp_pdu *c, const char *str, uint len)
|
2024-04-22 10:55:33 +00:00
|
|
|
{
|
2024-05-24 13:20:30 +00:00
|
|
|
snmp_set_varbind_type(c->sr_vb_start, AGENTX_OCTET_STRING);
|
|
|
|
c->buffer = snmp_put_nstr(snmp_varbind_data(c->sr_vb_start), str, len);
|
2024-04-22 10:55:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 12:02:23 +00:00
|
|
|
inline enum agentx_type
|
|
|
|
snmp_search_res_to_type(enum snmp_search_res r)
|
|
|
|
{
|
|
|
|
ASSUME(r != SNMP_SEARCH_OK);
|
2024-01-10 11:21:46 +00:00
|
|
|
enum agentx_type type_arr[] = {
|
2023-07-26 12:02:23 +00:00
|
|
|
[SNMP_SEARCH_NO_OBJECT] = AGENTX_NO_SUCH_OBJECT,
|
|
|
|
[SNMP_SEARCH_NO_INSTANCE] = AGENTX_NO_SUCH_INSTANCE,
|
|
|
|
[SNMP_SEARCH_END_OF_VIEW] = AGENTX_END_OF_MIB_VIEW,
|
|
|
|
};
|
|
|
|
|
|
|
|
return type_arr[r];
|
|
|
|
}
|
2023-09-11 11:06:20 +00:00
|
|
|
|
2024-01-10 11:21:46 +00:00
|
|
|
inline int
|
|
|
|
snmp_test_close_reason(byte value)
|
|
|
|
{
|
|
|
|
if (value >= (byte) AGENTX_CLOSE_OTHER &&
|
|
|
|
value <= (byte) AGENTX_CLOSE_BY_MANAGER)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2024-01-23 23:23:31 +00:00
|
|
|
* Debugging
|
2024-01-10 11:21:46 +00:00
|
|
|
*/
|
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
void UNUSED
|
|
|
|
snmp_oid_dump(const struct oid *oid)
|
2024-01-10 11:21:46 +00:00
|
|
|
{
|
2024-01-23 23:23:31 +00:00
|
|
|
log(L_WARN "OID DUMP ========");
|
2024-01-10 11:21:46 +00:00
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
if (oid == NULL)
|
|
|
|
{
|
|
|
|
log(L_WARN "is eqaul to NULL");
|
|
|
|
log(L_WARN "OID DUMP END ====");
|
|
|
|
log(L_WARN ".");
|
|
|
|
return;
|
|
|
|
}
|
2024-01-10 11:21:46 +00:00
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
else if (snmp_is_oid_empty(oid))
|
|
|
|
{
|
|
|
|
log(L_WARN "is empty");
|
|
|
|
log(L_WARN "OID DUMP END ====");
|
|
|
|
log(L_WARN ".");
|
|
|
|
return;
|
|
|
|
}
|
2024-01-10 11:21:46 +00:00
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
log(L_WARN " #ids: %4u prefix %3u include: %5s",
|
|
|
|
oid->n_subid, oid->prefix, (oid->include)? "true" : "false");
|
|
|
|
log(L_WARN "IDS -------------");
|
2024-01-10 11:21:46 +00:00
|
|
|
|
2024-01-23 23:23:31 +00:00
|
|
|
for (int i = 0; i < oid->n_subid; i++)
|
|
|
|
log(L_WARN " %2u: %11u ~ 0x%08X", i, oid->ids[i], oid->ids[i]);
|
|
|
|
|
|
|
|
log(L_WARN "OID DUMP END ====");
|
|
|
|
log(L_WARN);
|
|
|
|
}
|
2024-04-22 10:55:33 +00:00
|
|
|
|
|
|
|
void UNUSED
|
|
|
|
snmp_oid_log(const struct oid *oid)
|
|
|
|
{
|
|
|
|
char buf[1024] = { };
|
|
|
|
char *pos = buf;
|
|
|
|
|
|
|
|
if (snmp_oid_is_prefixed(oid))
|
|
|
|
{
|
|
|
|
for (uint i = 0; i < ARRAY_SIZE(snmp_internet); i++)
|
|
|
|
pos += snprintf(pos, buf + 1024 - pos, ".%u", snmp_internet[i]);
|
|
|
|
|
|
|
|
pos += snprintf(pos, buf + 1024 - pos, ".%u", oid->prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int id = 0; id < oid->n_subid; id++)
|
|
|
|
pos += snprintf(pos, buf + 1024 - pos, ".%u", oid->ids[id]);
|
|
|
|
|
2024-07-04 14:33:44 +00:00
|
|
|
snmp_log("%s", buf);
|
2024-04-22 10:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* snmp_oid_common_ancestor - find a common ancestor
|
|
|
|
* @left: first OID
|
|
|
|
* @right: second OID
|
|
|
|
* @out: buffer for result
|
|
|
|
*
|
|
|
|
* The @out must be large enough to always fit the resulting OID, a safe value
|
|
|
|
* is minimum between number of left subids and right subids. The result might
|
|
|
|
* be NULL OID in cases where there is no common subid. The result could be also
|
2024-05-21 13:43:41 +00:00
|
|
|
* viewed as longest common prefix. Note that if both @left and @right are
|
|
|
|
* prefixable but not prefixed the result in @out will also not be prefixed.
|
2024-04-22 10:55:33 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
snmp_oid_common_ancestor(const struct oid *left, const struct oid *right, struct oid *out)
|
|
|
|
{
|
|
|
|
ASSERT(left && right && out);
|
|
|
|
|
|
|
|
STORE_U8(out->include, 0);
|
|
|
|
STORE_U8(out->reserved, 0);
|
2024-05-21 13:43:41 +00:00
|
|
|
STORE_U8(out->prefix, 0);
|
2024-04-22 10:55:33 +00:00
|
|
|
|
|
|
|
u32 offset = 0;
|
|
|
|
u8 left_ids = LOAD_U8(left->n_subid), right_ids = LOAD_U8(right->n_subid);
|
|
|
|
|
|
|
|
int l = snmp_oid_is_prefixed(left), r = snmp_oid_is_prefixed(right);
|
|
|
|
if (l && r)
|
|
|
|
{
|
|
|
|
if (LOAD_U8(left->prefix) != LOAD_U8(right->prefix))
|
|
|
|
{
|
|
|
|
STORE_U8(out->n_subid, 4);
|
|
|
|
|
|
|
|
for (uint id = 0; id < ARRAY_SIZE(snmp_internet); id++)
|
|
|
|
STORE_U32(out->ids[id], snmp_internet[id]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
STORE_U8(out->prefix, LOAD_U8(left->prefix));
|
|
|
|
}
|
|
|
|
else if (!l && r)
|
|
|
|
{
|
|
|
|
if (left_ids == 0)
|
|
|
|
{
|
|
|
|
/* finish creating NULL OID */
|
|
|
|
STORE_U8(out->n_subid, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint id = 0; id < MIN(ARRAY_SIZE(snmp_internet), left_ids); id++)
|
|
|
|
{
|
|
|
|
if (LOAD_U32(left->ids[id]) != snmp_internet[id])
|
|
|
|
{
|
|
|
|
STORE_U8(out->n_subid, id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
STORE_U32(out->ids[id], snmp_internet[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left_ids <= ARRAY_SIZE(snmp_internet))
|
|
|
|
{
|
|
|
|
STORE_U8(out->n_subid, left_ids);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* index 4 is conresponding to the prefix in prefixed OID */
|
|
|
|
if (LOAD_U32(left->ids[4]) != (u32) LOAD_U8(right->prefix))
|
|
|
|
{
|
|
|
|
STORE_U8(out->n_subid, ARRAY_SIZE(snmp_internet));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete snmp_internet from out->ids and store OID prefix */
|
|
|
|
offset = ARRAY_SIZE(snmp_internet) + 1;
|
|
|
|
STORE_U8(out->n_subid, LOAD_U8(out->n_subid) - ARRAY_SIZE(snmp_internet));
|
|
|
|
STORE_U8(out->prefix, LOAD_U8(right->prefix));
|
|
|
|
}
|
|
|
|
else if (l && !r)
|
|
|
|
{
|
|
|
|
snmp_oid_common_ancestor(right, left, out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(offset <= left_ids);
|
|
|
|
|
|
|
|
u8 subids = 0;
|
|
|
|
for (u32 id = 0; id < MIN(left_ids - offset, right_ids); id++)
|
|
|
|
{
|
|
|
|
if (left->ids[offset + id] == right->ids[id])
|
|
|
|
{
|
|
|
|
subids++;
|
|
|
|
STORE_U32(out->ids[id], LOAD_U32(right->ids[id]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
STORE_U8(out->n_subid, subids);
|
|
|
|
}
|
2024-05-21 13:43:41 +00:00
|
|
|
|
2024-07-04 14:33:44 +00:00
|
|
|
/*
|
|
|
|
* SNMP MIB tree walking
|
|
|
|
*/
|
|
|
|
struct mib_leaf *
|
2024-07-17 11:03:26 +00:00
|
|
|
snmp_walk_init(struct mib_tree *tree, struct mib_walk_state *walk, const struct oid *oid, struct snmp_pdu *c)
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
|
|
|
mib_tree_walk_init(walk, tree);
|
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
snmp_vb_to_tx(c->p, oid, c);
|
2024-07-04 14:33:44 +00:00
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
mib_node_u *node = mib_tree_find(tree, walk, &c->sr_vb_start->name);
|
2024-07-04 14:33:44 +00:00
|
|
|
|
|
|
|
// TODO hide me in mib_tree code
|
|
|
|
/* mib_tree_find() returns NULL if the oid is longer than existing any path */
|
|
|
|
if (node == NULL && walk->stack_pos > 0)
|
|
|
|
node = walk->stack[walk->stack_pos - 1];
|
|
|
|
|
|
|
|
return (!node || !mib_node_is_leaf(node)) ? NULL : &node->leaf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO alter the varbind
|
|
|
|
struct mib_leaf *
|
2024-07-17 11:03:26 +00:00
|
|
|
snmp_walk_next(struct mib_tree *tree, struct mib_walk_state *walk, struct snmp_pdu *c)
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
|
|
|
ASSUME(tree && walk);
|
|
|
|
|
|
|
|
if (!walk->stack_pos)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mib_node_u *node = walk->stack[walk->stack_pos - 1];
|
|
|
|
|
|
|
|
int found = 0;
|
|
|
|
struct mib_leaf *leaf = &node->leaf;
|
2024-07-09 14:30:04 +00:00
|
|
|
|
|
|
|
if (mib_node_is_leaf(node) && leaf->call_next)
|
|
|
|
{
|
2024-07-17 11:03:26 +00:00
|
|
|
const struct oid *oid = &c->sr_vb_start->name;
|
2024-07-09 14:30:04 +00:00
|
|
|
if (mib_tree_walk_oid_compare(walk, oid) > 0)
|
|
|
|
{
|
2024-07-17 11:03:26 +00:00
|
|
|
int old = snmp_oid_size(&c->sr_vb_start->name);
|
2024-07-09 14:30:04 +00:00
|
|
|
if (mib_tree_walk_to_oid(walk,
|
2024-07-17 11:03:26 +00:00
|
|
|
&c->sr_vb_start->name, 20 * sizeof(u32)))
|
2024-07-09 14:30:04 +00:00
|
|
|
{
|
|
|
|
snmp_log("walk_next copy failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
int new = snmp_oid_size(&c->sr_vb_start->name);
|
|
|
|
c->buffer += (new - old);
|
2024-07-09 14:30:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
found = !leaf->call_next(walk, c);
|
2024-07-09 14:30:04 +00:00
|
|
|
}
|
2024-07-17 11:03:26 +00:00
|
|
|
else if (mib_node_is_leaf(node) && LOAD_U8(c->sr_vb_start->name.include))
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
|
|
|
found = 1;
|
2024-07-17 11:03:26 +00:00
|
|
|
STORE_U8(c->sr_vb_start->name.include, 0);
|
2024-07-04 14:33:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
const struct oid *oid = &c->sr_vb_start->name;
|
2024-07-09 14:30:04 +00:00
|
|
|
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)
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
2024-07-09 14:30:04 +00:00
|
|
|
/* mib_tree_walk_next() forces VarBind's name OID overwriting */
|
2024-07-17 11:03:26 +00:00
|
|
|
int old = snmp_oid_size(&c->sr_vb_start->name);
|
2024-07-09 14:30:04 +00:00
|
|
|
// TODO autogrow
|
2024-07-17 11:03:26 +00:00
|
|
|
if (mib_tree_walk_to_oid(walk, &c->sr_vb_start->name, 20 * sizeof(u32)))
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
|
|
|
snmp_log("walk_next copy failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
int new = snmp_oid_size(&c->sr_vb_start->name);
|
|
|
|
c->buffer += (new - old);
|
2024-07-04 14:33:44 +00:00
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
if (leaf->call_next && !leaf->call_next(walk, c))
|
2024-07-04 14:33:44 +00:00
|
|
|
found = 1;
|
|
|
|
else if (!leaf->call_next)
|
|
|
|
found = 1;
|
2024-07-09 14:30:04 +00:00
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
oid = &c->sr_vb_start->name;
|
2024-07-09 14:30:04 +00:00
|
|
|
skip = (walk->id_pos < LOAD_U8(oid->n_subid)) ?
|
|
|
|
LOAD_U32(oid->ids[walk->id_pos]) : 0;
|
2024-07-04 14:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return leaf;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum snmp_search_res
|
2024-07-17 11:03:26 +00:00
|
|
|
snmp_walk_fill(struct mib_leaf *leaf, struct mib_walk_state *walk, struct snmp_pdu *c)
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
2024-07-17 11:03:26 +00:00
|
|
|
struct agentx_varbind *vb = c->sr_vb_start;
|
2024-07-04 14:33:44 +00:00
|
|
|
|
|
|
|
if (!leaf)
|
|
|
|
return SNMP_SEARCH_NO_OBJECT;
|
|
|
|
|
|
|
|
uint size = 0;
|
|
|
|
if (leaf->size >= 0)
|
|
|
|
{
|
|
|
|
if (leaf->type == AGENTX_OCTET_STRING || leaf->type == AGENTX_OPAQUE ||
|
|
|
|
leaf->type == AGENTX_OBJECT_ID)
|
|
|
|
{
|
|
|
|
snmp_set_varbind_type(vb, leaf->type);
|
|
|
|
size = leaf->size;
|
|
|
|
}
|
|
|
|
else if (leaf->type != AGENTX_INVALID)
|
|
|
|
{
|
|
|
|
snmp_set_varbind_type(vb, leaf->type);
|
|
|
|
size = agentx_type_size(leaf->type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
size = leaf->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
snmp_log("walk_fill got size %u based on lt %u ls %u, calling filler()", size, leaf->type, leaf->size);
|
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
if (size >= c->size)
|
2024-07-04 14:33:44 +00:00
|
|
|
{
|
2024-07-17 11:03:26 +00:00
|
|
|
snmp_log("walk_fill small buffer size %d to %d", size, c->size);
|
|
|
|
snmp_manage_tbuf(c->p, c);
|
2024-07-04 14:33:44 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
enum snmp_search_res res = leaf->filler(walk, c);
|
2024-07-04 14:33:44 +00:00
|
|
|
|
2024-07-17 11:03:26 +00:00
|
|
|
vb = c->sr_vb_start;
|
2024-07-04 14:33:44 +00:00
|
|
|
|
|
|
|
if (res != SNMP_SEARCH_OK)
|
|
|
|
snmp_set_varbind_type(vb, snmp_search_res_to_type(res));
|
|
|
|
|
|
|
|
u16 type = snmp_load_varbind_type(vb);
|
|
|
|
ASSUME(type == leaf->type || type == AGENTX_END_OF_MIB_VIEW || type == AGENTX_NO_SUCH_OBJECT ||
|
|
|
|
type == AGENTX_NO_SUCH_INSTANCE);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|