Completely decouple SNMP stack from lwIP core by using private memory pools;

Move SNMP stack to apps;
API breaking change: Users need to call snmp_init() now!
This commit is contained in:
Dirk Ziegelmeier
2015-11-12 21:21:14 +01:00
parent 92a241a29e
commit 5f642eb3e3
16 changed files with 273 additions and 216 deletions

View File

@@ -50,7 +50,6 @@
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/snmp.h"
#include "lwip/autoip.h"
#include "lwip/igmp.h"
#include "lwip/dns.h"
@@ -80,9 +79,6 @@
#if (!LWIP_UDP && LWIP_MULTICAST_TX_OPTIONS)
#error "If you want to use IGMP/LWIP_MULTICAST_TX_OPTIONS, you have to define LWIP_UDP=1 in your lwipopts.h"
#endif
#if (!LWIP_UDP && LWIP_SNMP)
#error "If you want to use SNMP, you have to define LWIP_UDP=1 in your lwipopts.h"
#endif
#if (!LWIP_UDP && LWIP_DNS)
#error "If you want to use DNS, you have to define LWIP_UDP=1 in your lwipopts.h"
#endif
@@ -173,12 +169,6 @@
#if (!LWIP_ARP && LWIP_AUTOIP)
#error "If you want to use AUTOIP, you have to define LWIP_ARP=1 in your lwipopts.h"
#endif
#if (LWIP_SNMP && (SNMP_CONCURRENT_REQUESTS<=0))
#error "If you want to use SNMP, you have to define SNMP_CONCURRENT_REQUESTS>=1 in your lwipopts.h"
#endif
#if (LWIP_SNMP && (SNMP_TRAP_DESTINATIONS<=0))
#error "If you want to use SNMP, you have to define SNMP_TRAP_DESTINATIONS>=1 in your lwipopts.h"
#endif
#if (LWIP_TCP && ((LWIP_EVENT_API && LWIP_CALLBACK_API) || (!LWIP_EVENT_API && !LWIP_CALLBACK_API)))
#error "One and exactly one of LWIP_EVENT_API and LWIP_CALLBACK_API has to be enabled in your lwipopts.h"
#endif
@@ -343,9 +333,6 @@ lwip_init(void)
#if LWIP_TCP
tcp_init();
#endif /* LWIP_TCP */
#if LWIP_SNMP
snmp_init();
#endif /* LWIP_SNMP */
#if LWIP_AUTOIP
autoip_init();
#endif /* LWIP_AUTOIP */

View File

@@ -53,8 +53,6 @@
#include "lwip/stats.h"
#include "netif/etharp.h"
#include "lwip/ip_frag.h"
#include "lwip/snmp_structs.h"
#include "lwip/snmp_msg.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "netif/ppp/ppp.h"

View File

@@ -1,614 +0,0 @@
/**
* @file
* Abstract Syntax Notation One (ISO 8824, 8825) decoding
*
* @todo not optimised (yet), favor correctness over speed, favor speed over size
*/
/*
* Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* Author: Christiaan Simons <christiaan.simons@axon.tv>
*/
#include "lwip/opt.h"
#if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
#include "lwip/snmp_asn1.h"
/**
* Retrieves type field from incoming pbuf chain.
*
* @param p points to a pbuf holding an ASN1 coded type field
* @param ofs points to the offset within the pbuf chain of the ASN1 coded type field
* @param type return ASN1 type
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) decode
*/
err_t
snmp_asn1_dec_type(struct pbuf *p, u16_t ofs, u8_t *type)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
*type = *msg_ptr;
return ERR_OK;
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Decodes length field from incoming pbuf chain into host length.
*
* @param p points to a pbuf holding an ASN1 coded length
* @param ofs points to the offset within the pbuf chain of the ASN1 coded length
* @param octets_used returns number of octets used by the length code
* @param length return host order length, up to 64k
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) decode
*/
err_t
snmp_asn1_dec_length(struct pbuf *p, u16_t ofs, u8_t *octets_used, u16_t *length)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
if (*msg_ptr < 0x80) {
/* primitive definite length format */
*octets_used = 1;
*length = *msg_ptr;
return ERR_OK;
} else if (*msg_ptr == 0x80) {
/* constructed indefinite length format, termination with two zero octets */
u8_t zeros;
u8_t i;
*length = 0;
zeros = 0;
while (zeros != 2) {
i = 2;
while (i > 0) {
i--;
(*length) += 1;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
if (*msg_ptr == 0) {
zeros++;
if (zeros == 2) {
/* stop while (i > 0) */
i = 0;
}
} else {
zeros = 0;
}
}
}
*octets_used = 1;
return ERR_OK;
} else if (*msg_ptr == 0x81) {
/* constructed definite length format, one octet */
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
*length = *msg_ptr;
*octets_used = 2;
return ERR_OK;
} else if (*msg_ptr == 0x82) {
u8_t i;
/* constructed definite length format, two octets */
i = 2;
while (i > 0) {
i--;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
if (i == 0) {
/* least significant length octet */
*length |= *msg_ptr;
} else {
/* most significant length octet */
*length = (*msg_ptr) << 8;
}
}
*octets_used = 3;
return ERR_OK;
} else {
/* constructed definite length format 3..127 octets, this is too big (>64k) */
/** @todo: do we need to accept inefficient codings with many leading zero's? */
*octets_used = 1 + ((*msg_ptr) & 0x7f);
return ERR_ARG;
}
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Decodes positive integer (counter, gauge, timeticks) into u32_t.
*
* @param p points to a pbuf holding an ASN1 coded integer
* @param ofs points to the offset within the pbuf chain of the ASN1 coded integer
* @param len length of the coded integer field
* @param value return host order integer
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) decode
*
* @note ASN coded integers are _always_ signed. E.g. +0xFFFF is coded
* as 0x00,0xFF,0xFF. Note the leading sign octet. A positive value
* of 0xFFFFFFFF is preceded with 0x00 and the length is 5 octets!!
*/
err_t
snmp_asn1_dec_u32t(struct pbuf *p, u16_t ofs, u16_t len, u32_t *value)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
if ((len > 0) && (len < 6)) {
/* start from zero */
*value = 0;
if (*msg_ptr & 0x80) {
/* negative, expecting zero sign bit! */
return ERR_ARG;
} else {
/* positive */
if ((len > 1) && (*msg_ptr == 0)) {
/* skip leading "sign byte" octet 0x00 */
len--;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
}
/* OR octets with value */
while (len > 1) {
len--;
*value |= *msg_ptr;
*value <<= 8;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
*value |= *msg_ptr;
return ERR_OK;
} else {
return ERR_ARG;
}
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Decodes integer into s32_t.
*
* @param p points to a pbuf holding an ASN1 coded integer
* @param ofs points to the offset within the pbuf chain of the ASN1 coded integer
* @param len length of the coded integer field
* @param value return host order integer
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) decode
*
* @note ASN coded integers are _always_ signed!
*/
err_t
snmp_asn1_dec_s32t(struct pbuf *p, u16_t ofs, u16_t len, s32_t *value)
{
u16_t plen, base;
u8_t *msg_ptr;
#if BYTE_ORDER == LITTLE_ENDIAN
u8_t *lsb_ptr = (u8_t*)value;
#endif
#if BYTE_ORDER == BIG_ENDIAN
u8_t *lsb_ptr = (u8_t*)value + sizeof(s32_t) - 1;
#endif
u8_t sign;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
if ((len > 0) && (len < 5)) {
if (*msg_ptr & 0x80) {
/* negative, start from -1 */
*value = -1;
sign = 1;
} else {
/* positive, start from 0 */
*value = 0;
sign = 0;
}
/* OR/AND octets with value */
while (len > 1) {
len--;
if (sign) {
*lsb_ptr &= *msg_ptr;
*value <<= 8;
*lsb_ptr |= 255;
} else {
*lsb_ptr |= *msg_ptr;
*value <<= 8;
}
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
if (sign) {
*lsb_ptr &= *msg_ptr;
} else {
*lsb_ptr |= *msg_ptr;
}
return ERR_OK;
} else {
return ERR_ARG;
}
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Decodes object identifier from incoming message into array of s32_t.
*
* @param p points to a pbuf holding an ASN1 coded object identifier
* @param ofs points to the offset within the pbuf chain of the ASN1 coded object identifier
* @param len length of the coded object identifier
* @param oid return object identifier struct
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) decode
*/
err_t
snmp_asn1_dec_oid(struct pbuf *p, u16_t ofs, u16_t len, struct snmp_obj_id *oid)
{
u16_t plen, base;
u8_t *msg_ptr;
s32_t *oid_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
oid->len = 0;
oid_ptr = &oid->id[0];
if (len > 0) {
/* first compressed octet */
if (*msg_ptr == 0x2B) {
/* (most) common case 1.3 (iso.org) */
*oid_ptr = 1;
oid_ptr++;
*oid_ptr = 3;
oid_ptr++;
} else if (*msg_ptr < 40) {
*oid_ptr = 0;
oid_ptr++;
*oid_ptr = *msg_ptr;
oid_ptr++;
} else if (*msg_ptr < 80) {
*oid_ptr = 1;
oid_ptr++;
*oid_ptr = (*msg_ptr) - 40;
oid_ptr++;
} else {
*oid_ptr = 2;
oid_ptr++;
*oid_ptr = (*msg_ptr) - 80;
oid_ptr++;
}
oid->len = 2;
} else {
/* accepting zero length identifiers e.g. for
getnext operation. uncommon but valid */
return ERR_OK;
}
len--;
if (len > 0) {
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
while ((len > 0) && (oid->len < LWIP_SNMP_OBJ_ID_LEN)) {
/* sub-identifier uses multiple octets */
if (*msg_ptr & 0x80) {
s32_t sub_id = 0;
while ((*msg_ptr & 0x80) && (len > 1)) {
len--;
sub_id = (sub_id << 7) + (*msg_ptr & ~0x80);
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
if (!(*msg_ptr & 0x80) && (len > 0)) {
/* last octet sub-identifier */
len--;
sub_id = (sub_id << 7) + *msg_ptr;
*oid_ptr = sub_id;
}
} else {
/* !(*msg_ptr & 0x80) sub-identifier uses single octet */
len--;
*oid_ptr = *msg_ptr;
}
if (len > 0) {
/* remaining oid bytes available ... */
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
oid_ptr++;
oid->len++;
}
if (len == 0) {
/* len == 0, end of oid */
return ERR_OK;
} else {
/* len > 0, oid->len == LWIP_SNMP_OBJ_ID_LEN or malformed encoding */
return ERR_ARG;
}
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Decodes (copies) raw data (ip-addresses, octet strings, opaque encoding)
* from incoming message into array.
*
* @param p points to a pbuf holding an ASN1 coded raw data
* @param ofs points to the offset within the pbuf chain of the ASN1 coded raw data
* @param len length of the coded raw data (zero is valid, e.g. empty string!)
* @param raw_len length of the raw return value
* @param raw return raw bytes
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) decode
*/
err_t
snmp_asn1_dec_raw(struct pbuf *p, u16_t ofs, u16_t len, u16_t raw_len, u8_t *raw)
{
u16_t plen, base;
u8_t *msg_ptr;
if (len > 0) {
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
if (raw_len >= len) {
while (len > 1) {
/* copy len - 1 octets */
len--;
*raw = *msg_ptr;
raw++;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
/* copy last octet */
*raw = *msg_ptr;
return ERR_OK;
} else {
/* raw_len < len, not enough dst space */
return ERR_ARG;
}
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
} else {
/* len == 0, empty string */
return ERR_OK;
}
}
/**
* Decodes BITS pseudotype value from ASN.1 OctetString.
*
* @note Because BITS pseudo type is encoded as OCTET STRING, it cannot directly
* be encoded/decoded by the agent. Instead call this function as required from
* get/test/set methods.
*
* @param buf points to a buffer holding the ASN1 octet string
* @param buf_len length of octet string
* @param bit_value decoded Bit value with Bit0 == LSB
* @return ERR_OK if successful, ERR_ARG if bit value contains more than 32 bit
*/
err_t
snmp_asn1_dec_bits(const u8_t *buf, u32_t buf_len, u32_t *bit_value)
{
u8_t b;
u8_t bits_processed = 0;
*bit_value = 0;
while (buf_len > 0) {
/* any bit set in this byte? */
if (*buf != 0x00) {
if (bits_processed >= 32) {
/* accept more than 4 bytes, but only when no bits are set */
return ERR_ARG;
}
b = *buf;
do {
if (b & 0x80) {
*bit_value |= (1 << bits_processed);
}
bits_processed++;
b <<= 1;
}
while ((bits_processed % 8) != 0);
} else {
bits_processed += 8;
}
buf_len--;
buf++;
}
return ERR_OK;
}
#endif /* LWIP_SNMP */

View File

@@ -1,580 +0,0 @@
/**
* @file
* Abstract Syntax Notation One (ISO 8824, 8825) encoding
*
* @todo not optimised (yet), favor correctness over speed, favor speed over size
*/
/*
* Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* Author: Christiaan Simons <christiaan.simons@axon.tv>
*/
#include "lwip/opt.h"
#if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
#include "lwip/snmp_asn1.h"
/**
* Returns octet count for length.
*
* @param length
* @param octets_needed points to the return value
*/
void
snmp_asn1_enc_length_cnt(u16_t length, u8_t *octets_needed)
{
if (length < 0x80U) {
*octets_needed = 1;
} else if (length < 0x100U) {
*octets_needed = 2;
} else {
*octets_needed = 3;
}
}
/**
* Returns octet count for an u32_t.
*
* @param value
* @param octets_needed points to the return value
*
* @note ASN coded integers are _always_ signed. E.g. +0xFFFF is coded
* as 0x00,0xFF,0xFF. Note the leading sign octet. A positive value
* of 0xFFFFFFFF is preceded with 0x00 and the length is 5 octets!!
*/
void
snmp_asn1_enc_u32t_cnt(u32_t value, u16_t *octets_needed)
{
if (value < 0x80UL) {
*octets_needed = 1;
} else if (value < 0x8000UL) {
*octets_needed = 2;
} else if (value < 0x800000UL) {
*octets_needed = 3;
} else if (value < 0x80000000UL) {
*octets_needed = 4;
} else {
*octets_needed = 5;
}
}
/**
* Returns octet count for an s32_t.
*
* @param value
* @param octets_needed points to the return value
*
* @note ASN coded integers are _always_ signed.
*/
void
snmp_asn1_enc_s32t_cnt(s32_t value, u16_t *octets_needed)
{
if (value < 0) {
value = ~value;
} if (value < 0x80L) {
*octets_needed = 1;
} else if (value < 0x8000L) {
*octets_needed = 2;
} else if (value < 0x800000L) {
*octets_needed = 3;
} else {
*octets_needed = 4;
}
}
/**
* Returns octet count for an object identifier.
*
* @param ident_len object identifier array length
* @param ident points to object identifier array
* @param octets_needed points to the return value
*/
void
snmp_asn1_enc_oid_cnt(u16_t ident_len, const s32_t *ident, u16_t *octets_needed)
{
s32_t sub_id;
u8_t cnt;
cnt = 0;
if (ident_len > 1) {
/* compressed prefix in one octet */
cnt++;
ident_len -= 2;
ident += 2;
}
while (ident_len > 0) {
ident_len--;
sub_id = *ident;
sub_id >>= 7;
cnt++;
while (sub_id > 0) {
sub_id >>= 7;
cnt++;
}
ident++;
}
*octets_needed = cnt;
}
/**
* Encodes ASN type field into a pbuf chained ASN1 msg.
*
* @param p points to output pbuf to encode value into
* @param ofs points to the offset within the pbuf chain
* @param type input ASN1 type
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) encode
*/
err_t
snmp_asn1_enc_type(struct pbuf *p, u16_t ofs, u8_t type)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
*msg_ptr = type;
return ERR_OK;
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Encodes host order length field into a pbuf chained ASN1 msg.
*
* @param p points to output pbuf to encode length into
* @param ofs points to the offset within the pbuf chain
* @param length is the host order length to be encoded
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) encode
*/
err_t
snmp_asn1_enc_length(struct pbuf *p, u16_t ofs, u16_t length)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
if (length < 0x80) {
*msg_ptr = (u8_t)length;
return ERR_OK;
} else if (length < 0x100) {
*msg_ptr = 0x81;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
*msg_ptr = (u8_t)length;
return ERR_OK;
} else {
u8_t i;
/* length >= 0x100 && length <= 0xFFFF */
*msg_ptr = 0x82;
i = 2;
while (i > 0) {
i--;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
if (i == 0) {
/* least significant length octet */
*msg_ptr = (u8_t)length;
} else {
/* most significant length octet */
*msg_ptr = (u8_t)(length >> 8);
}
}
return ERR_OK;
}
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Encodes u32_t (counter, gauge, timeticks) into a pbuf chained ASN1 msg.
*
* @param p points to output pbuf to encode value into
* @param ofs points to the offset within the pbuf chain
* @param octets_needed encoding length (from snmp_asn1_enc_u32t_cnt())
* @param value is the host order u32_t value to be encoded
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) encode
*
* @see snmp_asn1_enc_u32t_cnt()
*/
err_t
snmp_asn1_enc_u32t(struct pbuf *p, u16_t ofs, u16_t octets_needed, u32_t value)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
if (octets_needed == 5) {
/* not enough bits in 'value' add leading 0x00 */
octets_needed--;
*msg_ptr = 0x00;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
while (octets_needed > 1) {
octets_needed--;
*msg_ptr = (u8_t)(value >> (octets_needed << 3));
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
/* (only) one least significant octet */
*msg_ptr = (u8_t)value;
return ERR_OK;
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Encodes s32_t integer into a pbuf chained ASN1 msg.
*
* @param p points to output pbuf to encode value into
* @param ofs points to the offset within the pbuf chain
* @param octets_needed encoding length (from snmp_asn1_enc_s32t_cnt())
* @param value is the host order s32_t value to be encoded
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) encode
*
* @see snmp_asn1_enc_s32t_cnt()
*/
err_t
snmp_asn1_enc_s32t(struct pbuf *p, u16_t ofs, u16_t octets_needed, s32_t value)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
while (octets_needed > 1) {
octets_needed--;
*msg_ptr = (u8_t)(value >> (octets_needed << 3));
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
/* (only) one least significant octet */
*msg_ptr = (u8_t)value;
return ERR_OK;
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Encodes object identifier into a pbuf chained ASN1 msg.
*
* @param p points to output pbuf to encode oid into
* @param ofs points to the offset within the pbuf chain
* @param ident_len object identifier array length
* @param ident points to object identifier array
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) encode
*/
err_t
snmp_asn1_enc_oid(struct pbuf *p, u16_t ofs, u16_t ident_len, const s32_t *ident)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
if (ident_len > 1) {
if ((ident[0] == 1) && (ident[1] == 3)) {
/* compressed (most common) prefix .iso.org */
*msg_ptr = 0x2b;
} else {
/* calculate prefix */
*msg_ptr = (u8_t)((ident[0] * 40) + ident[1]);
}
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
ident_len -= 2;
ident += 2;
} else {
/* @bug: allow empty varbinds for symmetry (we must decode them for getnext), allow partial compression?? */
/* ident_len <= 1, at least we need zeroDotZero (0.0) (ident_len == 2) */
return ERR_ARG;
}
while (ident_len > 0) {
s32_t sub_id;
u8_t shift, tail;
ident_len--;
sub_id = *ident;
tail = 0;
shift = 28;
while (shift > 0) {
u8_t code;
code = (u8_t)(sub_id >> shift);
if ((code != 0) || (tail != 0)) {
tail = 1;
*msg_ptr = code | 0x80;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
shift -= 7;
}
*msg_ptr = (u8_t)sub_id & 0x7F;
if (ident_len > 0) {
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
/* proceed to next sub-identifier */
ident++;
}
return ERR_OK;
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Encodes raw data (octet string, opaque) into a pbuf chained ASN1 msg.
*
* @param p points to output pbuf to encode raw data into
* @param ofs points to the offset within the pbuf chain
* @param raw_len raw data length
* @param raw points raw data
* @return ERR_OK if successful, ERR_ARG if we can't (or won't) encode
*/
err_t
snmp_asn1_enc_raw(struct pbuf *p, u16_t ofs, u16_t raw_len, const u8_t *raw)
{
u16_t plen, base;
u8_t *msg_ptr;
plen = 0;
while (p != NULL) {
base = plen;
plen += p->len;
if (ofs < plen) {
msg_ptr = (u8_t*)p->payload;
msg_ptr += ofs - base;
while (raw_len > 1) {
/* copy raw_len - 1 octets */
raw_len--;
*msg_ptr = *raw;
raw++;
ofs += 1;
if (ofs >= plen) {
/* next octet in next pbuf */
p = p->next;
if (p == NULL) {
return ERR_ARG;
}
msg_ptr = (u8_t*)p->payload;
plen += p->len;
} else {
/* next octet in same pbuf */
msg_ptr++;
}
}
if (raw_len > 0) {
/* copy last or single octet */
*msg_ptr = *raw;
}
return ERR_OK;
}
p = p->next;
}
/* p == NULL, ofs >= plen */
return ERR_ARG;
}
/**
* Encodes BITS pseudotype value into ASN.1 OctetString.
*
* @note Because BITS pseudo type is encoded as OCTET STRING, it cannot directly
* be encoded/decoded by the agent. Instead call this function as required from
* get/test/set methods.
*
* @param buf points to a buffer where the resulting ASN1 octet string is stored to
* @param buf_len max length of the bufffer
* @param bit_value Bit value to encode with Bit0 == LSB
* @return number of bytes used from buffer to store the resulting OctetString
*/
u8_t
snmp_asn1_enc_bits(u8_t *buf, u32_t buf_len, u32_t bit_value)
{
int i = 0;
u8_t len = 0;
u8_t *buf_ptr = (u8_t *)buf;
while ((buf_len > 0) && (bit_value != 0x00)) {
*buf_ptr = 0x00;
i = 8;
while (i > 0) {
if (bit_value & 0x01) {
*buf_ptr |= 0x01;
}
bit_value >>= 1;
*buf_ptr <<= 1;
i--;
}
buf_ptr++;
buf_len--;
len++;
}
return len;
}
#endif /* LWIP_SNMP */

File diff suppressed because it is too large Load Diff

View File

@@ -1,991 +0,0 @@
/**
* @file
* MIB tree access/construction functions.
*/
/*
* Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* Author: Christiaan Simons <christiaan.simons@axon.tv>
*/
#include "lwip/opt.h"
#if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
#include "lwip/snmp_structs.h"
#include "lwip/memp.h"
#include "lwip/netif.h"
/** .iso.org.dod.internet address prefix, @see snmp_iso_*() */
const s32_t prefix[4] = {1, 3, 6, 1};
#define NODE_STACK_SIZE (LWIP_SNMP_OBJ_ID_LEN)
/** node stack entry (old news?) */
struct nse
{
/** right child */
const struct mib_node* r_ptr;
/** right child identifier */
s32_t r_id;
/** right child next level */
u8_t r_nl;
};
static u8_t node_stack_cnt;
static struct nse node_stack[NODE_STACK_SIZE];
const struct nse node_null = {NULL, 0, 0};
/**
* Pushes nse struct onto stack.
*/
static void
push_node(const struct nse* node)
{
LWIP_ASSERT("node_stack_cnt < NODE_STACK_SIZE",node_stack_cnt < NODE_STACK_SIZE);
LWIP_DEBUGF(SNMP_MIB_DEBUG,("push_node() node=%p id=%"S32_F"\n",(const void*)(node->r_ptr),node->r_id));
if (node_stack_cnt < NODE_STACK_SIZE) {
node_stack[node_stack_cnt] = *node;
node_stack_cnt++;
}
}
/**
* Pops nse struct from stack.
*/
static void
pop_node(struct nse* node)
{
if (node_stack_cnt > 0) {
node_stack_cnt--;
*node = node_stack[node_stack_cnt];
}
LWIP_DEBUGF(SNMP_MIB_DEBUG,("pop_node() node=%p id=%"S32_F"\n",(const void *)(node->r_ptr),node->r_id));
}
/**
* Conversion from ifIndex to lwIP netif
* @param ifindex is a s32_t object sub-identifier
* @param netif points to returned netif struct pointer
*/
void
snmp_ifindextonetif(s32_t ifindex, struct netif **netif)
{
struct netif *nif = netif_list;
s32_t i, ifidx;
ifidx = ifindex - 1;
i = 0;
while ((nif != NULL) && (i < ifidx)) {
nif = nif->next;
i++;
}
*netif = nif;
}
/**
* Conversion from lwIP netif to ifIndex
* @param netif points to a netif struct
* @param ifidx points to s32_t object sub-identifier
*/
void
snmp_netiftoifindex(struct netif *netif, s32_t *ifidx)
{
struct netif *nif = netif_list;
u16_t i;
i = 0;
while ((nif != NULL) && (nif != netif)) {
nif = nif->next;
i++;
}
*ifidx = i+1;
}
/**
* Conversion from oid to lwIP ip_addr
* @param ident points to s32_t ident[4] input
* @param ip points to output struct
*/
void
snmp_oidtoip(s32_t *ident, ip4_addr_t *ip)
{
IP4_ADDR(ip, ident[0], ident[1], ident[2], ident[3]);
}
/**
* Conversion from lwIP ip_addr to oid
* @param ip points to input struct
* @param ident points to s32_t ident[4] output
*/
void
snmp_iptooid(const ip4_addr_t *ip, s32_t *ident)
{
ident[0] = ip4_addr1(ip);
ident[1] = ip4_addr2(ip);
ident[2] = ip4_addr3(ip);
ident[3] = ip4_addr4(ip);
}
struct mib_list_node *
snmp_mib_ln_alloc(s32_t id)
{
struct mib_list_node *ln;
ln = (struct mib_list_node *)memp_malloc(MEMP_SNMP_NODE);
if (ln != NULL) {
ln->prev = NULL;
ln->next = NULL;
ln->objid = id;
ln->nptr = NULL;
}
return ln;
}
void
snmp_mib_ln_free(struct mib_list_node *ln)
{
memp_free(MEMP_SNMP_NODE, ln);
}
struct mib_list_rootnode *
snmp_mib_lrn_alloc(void)
{
struct mib_list_rootnode *lrn;
lrn = (struct mib_list_rootnode*)memp_malloc(MEMP_SNMP_ROOTNODE);
if (lrn != NULL) {
lrn->scalar.get_object_def = noleafs_get_object_def;
lrn->scalar.get_value = noleafs_get_value;
lrn->scalar.set_test = noleafs_set_test;
lrn->scalar.set_value = noleafs_set_value;
lrn->scalar.node.node_type = MIB_NODE_LR;
lrn->head = NULL;
lrn->tail = NULL;
lrn->count = 0;
}
return lrn;
}
void
snmp_mib_lrn_free(struct mib_list_rootnode *lrn)
{
memp_free(MEMP_SNMP_ROOTNODE, lrn);
}
/**
* Inserts node in idx list in a sorted
* (ascending order) fashion and
* allocates the node if needed.
*
* @param rn points to the root node
* @param objid is the object sub identifier
* @param insn points to a pointer to the inserted node
* used for constructing the tree.
* @return -1 if failed, 1 if inserted, 2 if present.
*/
s8_t
snmp_mib_node_insert(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **insn)
{
struct mib_list_node *nn;
s8_t insert;
LWIP_ASSERT("rn != NULL",rn != NULL);
/* -1 = malloc failure, 0 = not inserted, 1 = inserted, 2 = was present */
insert = 0;
if (rn->head == NULL) {
/* empty list, add first node */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc empty list objid==%"S32_F"\n",objid));
nn = snmp_mib_ln_alloc(objid);
if (nn != NULL) {
rn->head = nn;
rn->tail = nn;
*insn = nn;
insert = 1;
} else {
insert = -1;
}
} else {
struct mib_list_node *n;
/* at least one node is present */
n = rn->head;
while ((n != NULL) && (insert == 0)) {
if (n->objid == objid) {
/* node is already there */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("node already there objid==%"S32_F"\n",objid));
*insn = n;
insert = 2;
} else if (n->objid < objid) {
if (n->next == NULL) {
/* alloc and insert at the tail */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins tail objid==%"S32_F"\n",objid));
nn = snmp_mib_ln_alloc(objid);
if (nn != NULL) {
nn->next = NULL;
nn->prev = n;
n->next = nn;
rn->tail = nn;
*insn = nn;
insert = 1;
} else {
/* insertion failure */
insert = -1;
}
} else {
/* there's more to explore: traverse list */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("traverse list\n"));
n = n->next;
}
} else {
/* n->objid > objid */
/* alloc and insert between n->prev and n */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins n->prev, objid==%"S32_F", n\n",objid));
nn = snmp_mib_ln_alloc(objid);
if (nn != NULL) {
if (n->prev == NULL) {
/* insert at the head */
nn->next = n;
nn->prev = NULL;
rn->head = nn;
n->prev = nn;
} else {
/* insert in the middle */
nn->next = n;
nn->prev = n->prev;
n->prev->next = nn;
n->prev = nn;
}
*insn = nn;
insert = 1;
} else {
/* insertion failure */
insert = -1;
}
}
}
}
if (insert == 1) {
rn->count += 1;
}
LWIP_ASSERT("insert != 0",insert != 0);
return insert;
}
/**
* Finds node in idx list and returns deletion mark.
*
* @param rn points to the root node
* @param objid is the object sub identifier
* @param fn returns pointer to found node
* @return 0 if not found, 1 if deletable,
* 2 can't delete (2 or more children), 3 not a list_node
*/
s8_t
snmp_mib_node_find(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **fn)
{
s8_t fc;
struct mib_list_node *n;
LWIP_ASSERT("rn != NULL",rn != NULL);
n = rn->head;
while ((n != NULL) && (n->objid != objid)) {
n = n->next;
}
if (n == NULL) {
fc = 0;
} else if (n->nptr == NULL) {
/* leaf, can delete node */
fc = 1;
} else {
struct mib_list_rootnode *r;
if (n->nptr->node_type == MIB_NODE_LR) {
r = (struct mib_list_rootnode*)(void*)n->nptr;
if (r->count > 1) {
/* can't delete node */
fc = 2;
} else {
/* count <= 1, can delete node */
fc = 1;
}
} else {
/* other node type */
fc = 3;
}
}
*fn = n;
return fc;
}
/**
* Removes node from idx list
* if it has a single child left.
*
* @param rn points to the root node
* @param n points to the node to delete
* @return the nptr to be freed by caller
*/
struct mib_list_rootnode *
snmp_mib_node_delete(struct mib_list_rootnode *rn, struct mib_list_node *n)
{
struct mib_list_rootnode *next;
LWIP_ASSERT("rn != NULL",rn != NULL);
LWIP_ASSERT("n != NULL",n != NULL);
/* caller must remove this sub-tree */
next = (struct mib_list_rootnode*)(void*)n->nptr;
rn->count -= 1;
if (n == rn->head) {
rn->head = n->next;
if (n->next != NULL) {
/* not last node, new list begin */
n->next->prev = NULL;
}
} else if (n == rn->tail) {
rn->tail = n->prev;
if (n->prev != NULL) {
/* not last node, new list end */
n->prev->next = NULL;
}
} else {
/* node must be in the middle */
n->prev->next = n->next;
n->next->prev = n->prev;
}
LWIP_DEBUGF(SNMP_MIB_DEBUG,("free list objid==%"S32_F"\n",n->objid));
snmp_mib_ln_free(n);
if (rn->count == 0) {
rn->head = NULL;
rn->tail = NULL;
}
return next;
}
/**
* Searches tree for the supplied (scalar?) object identifier.
*
* @param node points to the root of the tree ('.internet')
* @param ident_len the length of the supplied object identifier
* @param ident points to the array of sub identifiers
* @param np points to the found object instance (return)
* @return pointer to the requested parent (!) node if success, NULL otherwise
*/
const struct mib_node *
snmp_search_tree(const struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_name_ptr *np)
{
u8_t node_type, ext_level;
ext_level = 0;
LWIP_DEBUGF(SNMP_MIB_DEBUG,("node==%p *ident==%"S32_F"\n",(const void*)node,*ident));
while (node != NULL) {
node_type = node->node_type;
if (node_type == MIB_NODE_AR) {
const struct mib_array_node *an;
u16_t i;
if (ident_len > 0) {
/* array node (internal ROM or RAM, fixed length) */
an = (const struct mib_array_node*)(const void*)node;
i = 0;
while ((i < an->maxlength) && (an->entries[i].objid != *ident)) {
i++;
}
if (i < an->maxlength) {
/* found it, if available proceed to child, otherwise inspect leaf */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->entries[i].objid,*ident));
if (an->entries[i].nptr == NULL) {
/* a scalar leaf OR table,
inspect remaining instance number / table index */
np->ident_len = ident_len;
np->ident = ident;
return &an->node;
} else {
/* follow next child pointer */
ident++;
ident_len--;
node = an->entries[i].nptr;
}
} else {
/* search failed, identifier mismatch (nosuchname) */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed *ident==%"S32_F"\n",*ident));
return NULL;
}
} else {
/* search failed, short object identifier (nosuchname) */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed, short object identifier\n"));
return NULL;
}
} else if (node_type == MIB_NODE_LR) {
const struct mib_list_rootnode *lrn;
struct mib_list_node *ln;
if (ident_len > 0) {
/* list root node (internal 'RAM', variable length) */
lrn = (const struct mib_list_rootnode*)(const void*)node;
ln = lrn->head;
/* iterate over list, head to tail */
while ((ln != NULL) && (ln->objid != *ident)) {
ln = ln->next;
}
if (ln != NULL) {
/* found it, proceed to child */;
LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
if (ln->nptr == NULL) {
np->ident_len = ident_len;
np->ident = ident;
return &lrn->scalar.node;
} else {
/* follow next child pointer */
ident_len--;
ident++;
node = ln->nptr;
}
} else {
/* search failed */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed *ident==%"S32_F"\n",*ident));
return NULL;
}
} else {
/* search failed, short object identifier (nosuchname) */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed, short object identifier\n"));
return NULL;
}
} else if (node_type == MIB_NODE_EX) {
const struct mib_external_node *en;
u16_t i, len;
if (ident_len > 0) {
/* external node (addressing and access via functions) */
en = (const struct mib_external_node*)(const void*)node;
i = 0;
len = en->level_length(en->addr_inf,ext_level);
while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) != 0)) {
i++;
}
if (i < len) {
s32_t debug_id;
en->get_objid(en->addr_inf,ext_level,i,&debug_id);
LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid==%"S32_F" *ident==%"S32_F"\n",debug_id,*ident));
if ((ext_level + 1) == en->tree_levels) {
np->ident_len = ident_len;
np->ident = ident;
return &en->node;
} else {
/* found it, proceed to child */
ident_len--;
ident++;
ext_level++;
}
} else {
/* search failed */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed *ident==%"S32_F"\n",*ident));
return NULL;
}
} else {
/* search failed, short object identifier (nosuchname) */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed, short object identifier\n"));
return NULL;
}
} else if (node_type == MIB_NODE_SC) {
/* scalar node */
if ((ident_len == 1) && (*ident == 0)) {
np->ident_len = ident_len;
np->ident = ident;
return node;
} else {
/* search failed, short object identifier (nosuchname) */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed, invalid object identifier length\n"));
return NULL;
}
} else {
/* unknown node_type */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node_type %"U16_F" unkown\n",(u16_t)node_type));
return NULL;
}
}
/* done, found nothing */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node==%p\n",(const void*)node));
return NULL;
}
/**
* Test table for presence of at least one table entry.
*/
static u8_t
empty_table(const struct mib_node *node)
{
u8_t node_type;
u8_t empty = 0;
if (node != NULL) {
node_type = node->node_type;
if (node_type == MIB_NODE_LR) {
const struct mib_list_rootnode *lrn;
lrn = (const struct mib_list_rootnode*)(const void*)node;
if ((lrn->count == 0) || (lrn->head == NULL)) {
empty = 1;
}
} else if (node_type == MIB_NODE_AR) {
const struct mib_array_node *an;
an = (const struct mib_array_node*)(const void*)node;
if ((an->maxlength == 0) || (an->entries == NULL)) {
empty = 1;
}
} else if (node_type == MIB_NODE_EX) {
const struct mib_external_node *en;
en = (const struct mib_external_node*)(const void*)node;
if (en->tree_levels == 0) {
empty = 1;
}
}
}
return empty;
}
/**
* Tree expansion.
*/
const struct mib_node *
snmp_expand_tree(const struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
{
u8_t node_type, ext_level, climb_tree;
ext_level = 0;
/* reset node stack */
node_stack_cnt = 0;
while (node != NULL) {
climb_tree = 0;
node_type = node->node_type;
if (node_type == MIB_NODE_AR) {
const struct mib_array_node *an;
u16_t i;
/* array node (internal ROM or RAM, fixed length) */
an = (const struct mib_array_node*)(const void*)node;
if (ident_len > 0) {
i = 0;
while ((i < an->maxlength) && (an->entries[i].objid < *ident)) {
i++;
}
if (i < an->maxlength) {
LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->entries[i].objid,*ident));
/* add identifier to oidret */
oidret->id[oidret->len] = an->entries[i].objid;
(oidret->len)++;
if (an->entries[i].nptr == NULL) {
LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
/* leaf node (e.g. in a fixed size table) */
if (an->entries[i].objid > *ident) {
return &an->node;
} else if ((i + 1) < an->maxlength) {
/* an->entries[i].objid == *ident */
(oidret->len)--;
oidret->id[oidret->len] = an->entries[i + 1].objid;
(oidret->len)++;
return &an->node;
} else {
/* (i + 1) == an->maxlength */
(oidret->len)--;
climb_tree = 1;
}
} else {
u16_t j;
LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
/* non-leaf, store right child ptr and id */
LWIP_ASSERT("i < 0xff", i < 0xff);
j = i + 1;
while ((j < an->maxlength) && (empty_table(an->entries[j].nptr))) {
j++;
}
if (j < an->maxlength) {
struct nse cur_node;
cur_node.r_ptr = an->entries[j].nptr;
cur_node.r_id = an->entries[j].objid;
cur_node.r_nl = 0;
push_node(&cur_node);
} else {
push_node(&node_null);
}
if (an->entries[i].objid == *ident) {
ident_len--;
ident++;
} else {
/* an->entries[i].objid < *ident */
ident_len = 0;
}
/* follow next child pointer */
node = an->entries[i].nptr;
}
} else {
/* i == an->maxlength */
climb_tree = 1;
}
} else {
u16_t j;
/* ident_len == 0, complete with leftmost '.thing' */
j = 0;
while ((j < an->maxlength) && empty_table(an->entries[j].nptr)) {
j++;
}
if (j < an->maxlength) {
LWIP_DEBUGF(SNMP_MIB_DEBUG,("left an->entries[j].objid==%"S32_F"\n",an->entries[j].objid));
oidret->id[oidret->len] = an->entries[j].objid;
(oidret->len)++;
if (an->entries[j].nptr == NULL) {
/* leaf node */
return &an->node;
} else {
/* no leaf, continue */
node = an->entries[j].nptr;
}
} else {
/* j == an->maxlength */
climb_tree = 1;
}
}
} else if (node_type == MIB_NODE_LR) {
const struct mib_list_rootnode *lrn;
struct mib_list_node *ln;
/* list root node (internal 'RAM', variable length) */
lrn = (const struct mib_list_rootnode*)(const void*)node;
if (ident_len > 0) {
ln = lrn->head;
/* iterate over list, head to tail */
while ((ln != NULL) && (ln->objid < *ident)) {
ln = ln->next;
}
if (ln != NULL) {
LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
oidret->id[oidret->len] = ln->objid;
(oidret->len)++;
if (ln->nptr == NULL) {
/* leaf node */
if (ln->objid > *ident) {
return &lrn->scalar.node;
} else if (ln->next != NULL) {
/* ln->objid == *ident */
(oidret->len)--;
oidret->id[oidret->len] = ln->next->objid;
(oidret->len)++;
return &lrn->scalar.node;
} else {
/* ln->next == NULL */
(oidret->len)--;
climb_tree = 1;
}
} else {
struct mib_list_node *jn;
/* non-leaf, store right child ptr and id */
jn = ln->next;
while ((jn != NULL) && empty_table(jn->nptr)) {
jn = jn->next;
}
if (jn != NULL) {
struct nse cur_node;
cur_node.r_ptr = jn->nptr;
cur_node.r_id = jn->objid;
cur_node.r_nl = 0;
push_node(&cur_node);
} else {
push_node(&node_null);
}
if (ln->objid == *ident) {
ident_len--;
ident++;
} else {
/* ln->objid < *ident */
ident_len = 0;
}
/* follow next child pointer */
node = ln->nptr;
}
} else {
/* ln == NULL */
climb_tree = 1;
}
} else {
struct mib_list_node *jn;
/* ident_len == 0, complete with leftmost '.thing' */
jn = lrn->head;
while ((jn != NULL) && empty_table(jn->nptr)) {
jn = jn->next;
}
if (jn != NULL) {
LWIP_DEBUGF(SNMP_MIB_DEBUG,("left jn->objid==%"S32_F"\n",jn->objid));
oidret->id[oidret->len] = jn->objid;
(oidret->len)++;
if (jn->nptr == NULL) {
/* leaf node */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("jn->nptr == NULL\n"));
return &lrn->scalar.node;
} else {
/* no leaf, continue */
node = jn->nptr;
}
} else {
/* jn == NULL */
climb_tree = 1;
}
}
} else if (node_type == MIB_NODE_EX) {
const struct mib_external_node *en;
s32_t ex_id;
/* external node (addressing and access via functions) */
en = (const struct mib_external_node*)(const void*)node;
if (ident_len > 0) {
u16_t i, len;
i = 0;
len = en->level_length(en->addr_inf,ext_level);
while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) < 0)) {
i++;
}
if (i < len) {
/* add identifier to oidret */
en->get_objid(en->addr_inf,ext_level,i,&ex_id);
LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,ex_id,*ident));
oidret->id[oidret->len] = ex_id;
(oidret->len)++;
if ((ext_level + 1) == en->tree_levels) {
LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
/* leaf node */
if (ex_id > *ident) {
return &en->node;
} else if ((i + 1) < len) {
/* ex_id == *ident */
en->get_objid(en->addr_inf,ext_level,i + 1,&ex_id);
(oidret->len)--;
oidret->id[oidret->len] = ex_id;
(oidret->len)++;
return &en->node;
} else {
/* (i + 1) == len */
(oidret->len)--;
climb_tree = 1;
}
} else {
u16_t j;
LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
/* non-leaf, store right child ptr and id */
LWIP_ASSERT("i < 0xff", i < 0xff);
j = i + 1;
if (j < len) {
struct nse cur_node;
/* right node is the current external node */
cur_node.r_ptr = node;
en->get_objid(en->addr_inf,ext_level,j,&cur_node.r_id);
cur_node.r_nl = ext_level + 1;
push_node(&cur_node);
} else {
push_node(&node_null);
}
if (en->ident_cmp(en->addr_inf,ext_level,i,*ident) == 0) {
ident_len--;
ident++;
} else {
/* external id < *ident */
ident_len = 0;
}
/* proceed to child */
ext_level++;
}
} else {
/* i == len (en->level_len()) */
climb_tree = 1;
}
} else {
/* ident_len == 0, complete with leftmost '.thing' */
en->get_objid(en->addr_inf,ext_level,0,&ex_id);
LWIP_DEBUGF(SNMP_MIB_DEBUG,("left en->objid==%"S32_F"\n",ex_id));
oidret->id[oidret->len] = ex_id;
(oidret->len)++;
if ((ext_level + 1) == en->tree_levels) {
/* leaf node */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("(ext_level + 1) == en->tree_levels\n"));
return &en->node;
} else {
/* no leaf, proceed to child */
ext_level++;
}
}
} else if (node_type == MIB_NODE_SC) {
/* scalar node */
if (ident_len > 0) {
/* at .0 */
climb_tree = 1;
} else {
/* ident_len == 0, complete object identifier */
oidret->id[oidret->len] = 0;
(oidret->len)++;
/* leaf node */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("completed scalar leaf\n"));
return node;
}
} else {
/* unknown/unhandled node_type */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node_type %"U16_F" unkown\n",(u16_t)node_type));
return NULL;
}
if (climb_tree) {
struct nse child;
/* find right child ptr */
child.r_ptr = NULL;
child.r_id = 0;
child.r_nl = 0;
while ((node_stack_cnt > 0) && (child.r_ptr == NULL)) {
pop_node(&child);
/* trim returned oid */
(oidret->len)--;
}
if (child.r_ptr != NULL) {
/* incoming ident is useless beyond this point */
ident_len = 0;
oidret->id[oidret->len] = child.r_id;
oidret->len++;
node = child.r_ptr;
ext_level = child.r_nl;
} else {
/* tree ends here ... */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed, tree ends here\n"));
return NULL;
}
}
}
/* done, found nothing */
LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node==%p\n",(const void*)node));
return NULL;
}
/**
* Test object identifier for the iso.org.dod.internet prefix.
*
* @param ident_len the length of the supplied object identifier
* @param ident points to the array of sub identifiers
* @return 1 if it matches, 0 otherwise
*/
u8_t
snmp_iso_prefix_tst(u8_t ident_len, s32_t *ident)
{
if ((ident_len > 3) &&
(ident[0] == 1) && (ident[1] == 3) &&
(ident[2] == 6) && (ident[3] == 1)) {
return 1;
} else {
return 0;
}
}
/**
* Expands object identifier to the iso.org.dod.internet
* prefix for use in getnext operation.
*
* @param ident_len the length of the supplied object identifier
* @param ident points to the array of sub identifiers
* @param oidret points to returned expanded object identifier
* @return 1 if it matches, 0 otherwise
*
* @note ident_len 0 is allowed, expanding to the first known object id!!
*/
u8_t
snmp_iso_prefix_expand(u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
{
const s32_t *prefix_ptr;
s32_t *ret_ptr;
u8_t i;
i = 0;
prefix_ptr = &prefix[0];
ret_ptr = &oidret->id[0];
ident_len = ((ident_len < 4)?ident_len:4);
while ((i < ident_len) && ((*ident) <= (*prefix_ptr))) {
*ret_ptr++ = *prefix_ptr++;
ident++;
i++;
}
if (i == ident_len) {
/* match, complete missing bits */
while (i < 4) {
*ret_ptr++ = *prefix_ptr++;
i++;
}
oidret->len = i;
return 1;
} else {
/* i != ident_len */
return 0;
}
}
void
noleafs_get_object_def(u8_t ident_len, s32_t *ident, struct obj_def *od)
{
LWIP_UNUSED_ARG(ident_len);
LWIP_UNUSED_ARG(ident);
od->instance = MIB_OBJECT_NONE;
}
u16_t
noleafs_get_value(struct obj_def *od, void *value)
{
LWIP_UNUSED_ARG(od);
LWIP_UNUSED_ARG(value);
return 0;
}
u8_t
noleafs_set_test(struct obj_def *od, u16_t len, void *value)
{
LWIP_UNUSED_ARG(od);
LWIP_UNUSED_ARG(len);
LWIP_UNUSED_ARG(value);
/* can't set */
return 0;
}
void
noleafs_set_value(struct obj_def *od, u16_t len, void *value)
{
LWIP_UNUSED_ARG(od);
LWIP_UNUSED_ARG(len);
LWIP_UNUSED_ARG(value);
}
#endif /* LWIP_SNMP */

File diff suppressed because it is too large Load Diff

View File

@@ -1,673 +0,0 @@
/**
* @file
* SNMP output message processing (RFC1157).
*
* Output responses and traps are build in two passes:
*
* Pass 0: iterate over the output message backwards to determine encoding lengths
* Pass 1: the actual forward encoding of internal form into ASN1
*
* The single-pass encoding method described by Comer & Stevens
* requires extra buffer space and copying for reversal of the packet.
* The buffer requirement can be prohibitively large for big payloads
* (>= 484) therefore we use the two encoding passes.
*/
/*
* Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* Author: Christiaan Simons <christiaan.simons@axon.tv>
*/
#include "lwip/opt.h"
#if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
#include "lwip/udp.h"
#include "lwip/netif.h"
#include "lwip/snmp.h"
#include "lwip/snmp_mib2.h"
#include "lwip/snmp_asn1.h"
#include "lwip/snmp_msg.h"
#include "lwip/sys.h"
#include <string.h>
#if !SNMP_COMMUNITY_EXT
#define snmp_community_trap snmp_community
#endif
struct snmp_trap_dst
{
/* destination IP address in network order */
ip_addr_t dip;
/* set to 0 when disabled, >0 when enabled */
u8_t enable;
};
struct snmp_trap_dst trap_dst[SNMP_TRAP_DESTINATIONS];
/** TRAP message structure */
struct snmp_msg_trap trap_msg;
static u16_t snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len);
static u16_t snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len);
static u16_t snmp_varbind_list_sum(struct snmp_varbind_root *root);
static u16_t snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p);
static u16_t snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p);
static u16_t snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs);
/**
* Sets enable switch for this trap destination.
* @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
* @param enable switch if 0 destination is disabled >0 enabled.
*/
void
snmp_trap_dst_enable(u8_t dst_idx, u8_t enable)
{
if (dst_idx < SNMP_TRAP_DESTINATIONS) {
trap_dst[dst_idx].enable = enable;
}
}
/**
* Sets IPv4 address for this trap destination.
* @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
* @param dst IPv4 address in host order.
*/
void
snmp_trap_dst_ip_set(u8_t dst_idx, const ip_addr_t *dst)
{
if (dst_idx < SNMP_TRAP_DESTINATIONS) {
ip_addr_set(&trap_dst[dst_idx].dip, dst);
}
}
/**
* Sends a 'getresponse' message to the request originator.
*
* @param m_stat points to the current message request state source
* @return ERR_OK when success, ERR_MEM if we're out of memory
*
* @note the caller is responsible for filling in outvb in the m_stat
* and provide error-status and index (except for tooBig errors) ...
*/
err_t
snmp_send_response(struct snmp_msg_pstat *m_stat)
{
struct snmp_varbind_root emptyvb = {NULL, NULL, 0, 0, 0};
struct pbuf *p;
u16_t tot_len;
err_t err;
/* pass 0, calculate length fields */
tot_len = snmp_varbind_list_sum(&m_stat->outvb);
tot_len = snmp_resp_header_sum(m_stat, tot_len);
/* try allocating pbuf(s) for complete response */
p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
if (p == NULL) {
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() tooBig\n"));
/* can't construct reply, return error-status tooBig */
m_stat->error_status = SNMP_ES_TOOBIG;
m_stat->error_index = 0;
/* pass 0, recalculate lengths, for empty varbind-list */
tot_len = snmp_varbind_list_sum(&emptyvb);
tot_len = snmp_resp_header_sum(m_stat, tot_len);
/* retry allocation once for header and empty varbind-list */
p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
}
if (p != NULL) {
/* first pbuf alloc try or retry alloc success */
u16_t ofs;
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() p != NULL\n"));
/* pass 1, size error, encode packet ino the pbuf(s) */
ofs = snmp_resp_header_enc(m_stat, p);
snmp_varbind_list_enc(&m_stat->outvb, p, ofs);
switch (m_stat->error_status) {
case SNMP_ES_NOERROR:
/* nothing to do */
break;
case SNMP_ES_TOOBIG:
mib2_inc_snmpouttoobigs();
break;
case SNMP_ES_NOSUCHNAME:
mib2_inc_snmpoutnosuchnames();
break;
case SNMP_ES_BADVALUE:
mib2_inc_snmpoutbadvalues();
break;
case SNMP_ES_GENERROR:
mib2_inc_snmpoutgenerrs();
break;
default:
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_send_response(): unknown error_status: %d\n", (int)m_stat->error_status));
break;
}
mib2_inc_snmpoutgetresponses();
mib2_inc_snmpoutpkts();
/** @todo do we need separate rx and tx pcbs for threaded case? */
/** connect to the originating source */
udp_connect(m_stat->pcb, &m_stat->sip, m_stat->sp);
err = udp_send(m_stat->pcb, p);
if (err == ERR_MEM) {
/** @todo release some memory, retry and return tooBig? tooMuchHassle? */
err = ERR_MEM;
} else {
err = ERR_OK;
}
/** disassociate remote address and port with this pcb */
udp_disconnect(m_stat->pcb);
pbuf_free(p);
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() done\n"));
return err;
} else {
/* first pbuf alloc try or retry alloc failed
very low on memory, couldn't return tooBig */
return ERR_MEM;
}
}
/**
* Sends an generic or enterprise specific trap message.
*
* @param generic_trap is the trap code
* @param eoid points to enterprise object identifier
* @param specific_trap used for enterprise traps when generic_trap == 6
* @return ERR_OK when success, ERR_MEM if we're out of memory
*
* @note the caller is responsible for filling in outvb in the trap_msg
* @note the use of the enterprise identifier field
* is per RFC1215.
* Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps
* and .iso.org.dod.internet.private.enterprises.yourenterprise
* (sysObjectID) for specific traps.
*/
err_t
snmp_send_trap(s8_t generic_trap, const struct snmp_obj_id *eoid, s32_t specific_trap)
{
struct snmp_trap_dst *td;
struct netif *dst_if;
const ip_addr_t* dst_ip;
struct pbuf *p;
u16_t i,tot_len;
err_t err = ERR_OK;
for (i = 0, td = &trap_dst[0]; i < SNMP_TRAP_DESTINATIONS; i++, td++) {
if ((td->enable != 0) && !ip_addr_isany(&td->dip)) {
/* network order trap destination */
ip_addr_copy(trap_msg.dip, td->dip);
/* lookup current source address for this dst */
ip_route_get_local_ip(PCB_ISIPV6(trap_msg.pcb), &trap_msg.pcb->local_ip,
&td->dip, dst_if, dst_ip);
if ((dst_if != NULL) && (dst_ip != NULL)) {
trap_msg.sip_raw_len = (IP_IS_V6_VAL(*dst_ip) ? 16 : 4);
memcpy(trap_msg.sip_raw, dst_ip, trap_msg.sip_raw_len);
trap_msg.gen_trap = generic_trap;
trap_msg.spc_trap = specific_trap;
if (generic_trap == SNMP_GENTRAP_ENTERPRISESPC) {
/* enterprise-Specific trap */
trap_msg.enterprise = eoid;
} else {
/* generic (MIB-II) trap */
mib2_get_snmpgrpid_ptr(&trap_msg.enterprise);
}
MIB2_COPY_SYSUPTIME_TO(&trap_msg.ts);
/* pass 0, calculate length fields */
tot_len = snmp_varbind_list_sum(&trap_msg.outvb);
tot_len = snmp_trap_header_sum(&trap_msg, tot_len);
/* allocate pbuf(s) */
p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
if (p != NULL) {
u16_t ofs;
/* pass 1, encode packet ino the pbuf(s) */
ofs = snmp_trap_header_enc(&trap_msg, p);
snmp_varbind_list_enc(&trap_msg.outvb, p, ofs);
mib2_inc_snmpouttraps();
mib2_inc_snmpoutpkts();
/** send to the TRAP destination */
udp_sendto(trap_msg.pcb, p, &trap_msg.dip, SNMP_TRAP_PORT);
pbuf_free(p);
} else {
err = ERR_MEM;
}
} else {
/* routing error */
err = ERR_RTE;
}
}
}
return err;
}
void
snmp_coldstart_trap(void)
{
trap_msg.outvb.head = NULL;
trap_msg.outvb.tail = NULL;
trap_msg.outvb.count = 0;
snmp_send_trap(SNMP_GENTRAP_COLDSTART, NULL, 0);
}
void
snmp_authfail_trap(void)
{
u8_t enable;
mib2_get_snmpenableauthentraps(&enable);
if (enable == 1) {
trap_msg.outvb.head = NULL;
trap_msg.outvb.tail = NULL;
trap_msg.outvb.count = 0;
snmp_send_trap(SNMP_GENTRAP_AUTHFAIL, NULL, 0);
}
}
/**
* Sums response header field lengths from tail to head and
* returns resp_header_lengths for second encoding pass.
*
* @param vb_len varbind-list length
* @param rhl points to returned header lengths
* @return the required length for encoding the response header
*/
static u16_t
snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len)
{
u16_t tot_len;
s32_t snmp_req_ver;
struct snmp_resp_header_lengths *rhl;
rhl = &m_stat->rhl;
tot_len = vb_len;
snmp_asn1_enc_s32t_cnt(m_stat->error_index, &rhl->erridxlen);
snmp_asn1_enc_length_cnt(rhl->erridxlen, &rhl->erridxlenlen);
tot_len += 1 + rhl->erridxlenlen + rhl->erridxlen;
snmp_asn1_enc_s32t_cnt(m_stat->error_status, &rhl->errstatlen);
snmp_asn1_enc_length_cnt(rhl->errstatlen, &rhl->errstatlenlen);
tot_len += 1 + rhl->errstatlenlen + rhl->errstatlen;
snmp_asn1_enc_s32t_cnt(m_stat->rid, &rhl->ridlen);
snmp_asn1_enc_length_cnt(rhl->ridlen, &rhl->ridlenlen);
tot_len += 1 + rhl->ridlenlen + rhl->ridlen;
rhl->pdulen = tot_len;
snmp_asn1_enc_length_cnt(rhl->pdulen, &rhl->pdulenlen);
tot_len += 1 + rhl->pdulenlen;
rhl->comlen = m_stat->com_strlen;
snmp_asn1_enc_length_cnt(rhl->comlen, &rhl->comlenlen);
tot_len += 1 + rhl->comlenlen + rhl->comlen;
snmp_req_ver = m_stat->version;
snmp_asn1_enc_s32t_cnt(snmp_req_ver, &rhl->verlen);
snmp_asn1_enc_length_cnt(rhl->verlen, &rhl->verlenlen);
tot_len += 1 + rhl->verlen + rhl->verlenlen;
rhl->seqlen = tot_len;
snmp_asn1_enc_length_cnt(rhl->seqlen, &rhl->seqlenlen);
tot_len += 1 + rhl->seqlenlen;
return tot_len;
}
/**
* Sums trap header field lengths from tail to head and
* returns trap_header_lengths for second encoding pass.
*
* @param vb_len varbind-list length
* @param thl points to returned header lengths
* @return the required length for encoding the trap header
*/
static u16_t
snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len)
{
u16_t tot_len;
struct snmp_trap_header_lengths *thl;
thl = &m_trap->thl;
tot_len = vb_len;
snmp_asn1_enc_u32t_cnt(m_trap->ts, &thl->tslen);
snmp_asn1_enc_length_cnt(thl->tslen, &thl->tslenlen);
tot_len += 1 + thl->tslen + thl->tslenlen;
snmp_asn1_enc_s32t_cnt(m_trap->spc_trap, &thl->strplen);
snmp_asn1_enc_length_cnt(thl->strplen, &thl->strplenlen);
tot_len += 1 + thl->strplen + thl->strplenlen;
snmp_asn1_enc_s32t_cnt(m_trap->gen_trap, &thl->gtrplen);
snmp_asn1_enc_length_cnt(thl->gtrplen, &thl->gtrplenlen);
tot_len += 1 + thl->gtrplen + thl->gtrplenlen;
thl->aaddrlen = m_trap->sip_raw_len;
snmp_asn1_enc_length_cnt(thl->aaddrlen, &thl->aaddrlenlen);
tot_len += 1 + thl->aaddrlen + thl->aaddrlenlen;
snmp_asn1_enc_oid_cnt(m_trap->enterprise->len, &m_trap->enterprise->id[0], &thl->eidlen);
snmp_asn1_enc_length_cnt(thl->eidlen, &thl->eidlenlen);
tot_len += 1 + thl->eidlen + thl->eidlenlen;
thl->pdulen = tot_len;
snmp_asn1_enc_length_cnt(thl->pdulen, &thl->pdulenlen);
tot_len += 1 + thl->pdulenlen;
thl->comlen = (u16_t)strlen(snmp_community_trap);
snmp_asn1_enc_length_cnt(thl->comlen, &thl->comlenlen);
tot_len += 1 + thl->comlenlen + thl->comlen;
snmp_asn1_enc_s32t_cnt(snmp_version, &thl->verlen);
snmp_asn1_enc_length_cnt(thl->verlen, &thl->verlenlen);
tot_len += 1 + thl->verlen + thl->verlenlen;
thl->seqlen = tot_len;
snmp_asn1_enc_length_cnt(thl->seqlen, &thl->seqlenlen);
tot_len += 1 + thl->seqlenlen;
return tot_len;
}
/**
* Sums varbind lengths from tail to head and
* annotates lengths in varbind for second encoding pass.
*
* @param root points to the root of the variable binding list
* @return the required length for encoding the variable bindings
*/
static u16_t
snmp_varbind_list_sum(struct snmp_varbind_root *root)
{
struct snmp_varbind *vb;
u32_t *uint_ptr;
s32_t *sint_ptr;
u16_t tot_len;
tot_len = 0;
vb = root->tail;
while (vb != NULL) {
/* encoded value lenght depends on type */
switch (vb->value_type) {
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
sint_ptr = (s32_t*)vb->value;
snmp_asn1_enc_s32t_cnt(*sint_ptr, &vb->vlen);
break;
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
uint_ptr = (u32_t*)vb->value;
snmp_asn1_enc_u32t_cnt(*uint_ptr, &vb->vlen);
break;
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
vb->vlen = vb->value_len;
break;
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
sint_ptr = (s32_t*)vb->value;
snmp_asn1_enc_oid_cnt(vb->value_len / sizeof(s32_t), sint_ptr, &vb->vlen);
break;
default:
/* unsupported type */
vb->vlen = 0;
break;
}
/* encoding length of value length field */
snmp_asn1_enc_length_cnt(vb->vlen, &vb->vlenlen);
snmp_asn1_enc_oid_cnt(vb->ident_len, vb->ident, &vb->olen);
snmp_asn1_enc_length_cnt(vb->olen, &vb->olenlen);
vb->seqlen = 1 + vb->vlenlen + vb->vlen;
vb->seqlen += 1 + vb->olenlen + vb->olen;
snmp_asn1_enc_length_cnt(vb->seqlen, &vb->seqlenlen);
/* varbind seq */
tot_len += 1 + vb->seqlenlen + vb->seqlen;
vb = vb->prev;
}
/* varbind-list seq */
root->seqlen = tot_len;
snmp_asn1_enc_length_cnt(root->seqlen, &root->seqlenlen);
tot_len += 1 + root->seqlenlen;
return tot_len;
}
/**
* Encodes response header from head to tail.
*/
static u16_t
snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p)
{
u16_t ofs;
s32_t snmp_req_ver;
ofs = 0;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_stat->rhl.seqlen);
ofs += m_stat->rhl.seqlenlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_stat->rhl.verlen);
ofs += m_stat->rhl.verlenlen;
snmp_req_ver = m_stat->version;
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.verlen, snmp_req_ver);
ofs += m_stat->rhl.verlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_stat->rhl.comlen);
ofs += m_stat->rhl.comlenlen;
snmp_asn1_enc_raw(p, ofs, m_stat->rhl.comlen, m_stat->community);
ofs += m_stat->rhl.comlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_RESP));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_stat->rhl.pdulen);
ofs += m_stat->rhl.pdulenlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_stat->rhl.ridlen);
ofs += m_stat->rhl.ridlenlen;
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.ridlen, m_stat->rid);
ofs += m_stat->rhl.ridlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_stat->rhl.errstatlen);
ofs += m_stat->rhl.errstatlenlen;
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.errstatlen, m_stat->error_status);
ofs += m_stat->rhl.errstatlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_stat->rhl.erridxlen);
ofs += m_stat->rhl.erridxlenlen;
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.erridxlen, m_stat->error_index);
ofs += m_stat->rhl.erridxlen;
return ofs;
}
/**
* Encodes trap header from head to tail.
*/
static u16_t
snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p)
{
u16_t ofs;
ofs = 0;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.seqlen);
ofs += m_trap->thl.seqlenlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.verlen);
ofs += m_trap->thl.verlenlen;
snmp_asn1_enc_s32t(p, ofs, m_trap->thl.verlen, snmp_version);
ofs += m_trap->thl.verlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.comlen);
ofs += m_trap->thl.comlenlen;
snmp_asn1_enc_raw(p, ofs, m_trap->thl.comlen, (const u8_t *)&snmp_community_trap[0]);
ofs += m_trap->thl.comlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_TRAP));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.pdulen);
ofs += m_trap->thl.pdulenlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.eidlen);
ofs += m_trap->thl.eidlenlen;
snmp_asn1_enc_oid(p, ofs, m_trap->enterprise->len, &m_trap->enterprise->id[0]);
ofs += m_trap->thl.eidlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.aaddrlen);
ofs += m_trap->thl.aaddrlenlen;
snmp_asn1_enc_raw(p, ofs, m_trap->thl.aaddrlen, &m_trap->sip_raw[0]);
ofs += m_trap->thl.aaddrlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.gtrplen);
ofs += m_trap->thl.gtrplenlen;
snmp_asn1_enc_u32t(p, ofs, m_trap->thl.gtrplen, m_trap->gen_trap);
ofs += m_trap->thl.gtrplen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.strplen);
ofs += m_trap->thl.strplenlen;
snmp_asn1_enc_u32t(p, ofs, m_trap->thl.strplen, m_trap->spc_trap);
ofs += m_trap->thl.strplen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS));
ofs += 1;
snmp_asn1_enc_length(p, ofs, m_trap->thl.tslen);
ofs += m_trap->thl.tslenlen;
snmp_asn1_enc_u32t(p, ofs, m_trap->thl.tslen, m_trap->ts);
ofs += m_trap->thl.tslen;
return ofs;
}
/**
* Encodes varbind list from head to tail.
*/
static u16_t
snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs)
{
struct snmp_varbind *vb;
s32_t *sint_ptr;
u32_t *uint_ptr;
u8_t *raw_ptr;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
ofs += 1;
snmp_asn1_enc_length(p, ofs, root->seqlen);
ofs += root->seqlenlen;
vb = root->head;
while (vb != NULL) {
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
ofs += 1;
snmp_asn1_enc_length(p, ofs, vb->seqlen);
ofs += vb->seqlenlen;
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
ofs += 1;
snmp_asn1_enc_length(p, ofs, vb->olen);
ofs += vb->olenlen;
snmp_asn1_enc_oid(p, ofs, vb->ident_len, &vb->ident[0]);
ofs += vb->olen;
snmp_asn1_enc_type(p, ofs, vb->value_type);
ofs += 1;
snmp_asn1_enc_length(p, ofs, vb->vlen);
ofs += vb->vlenlen;
switch (vb->value_type) {
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
sint_ptr = (s32_t*)vb->value;
snmp_asn1_enc_s32t(p, ofs, vb->vlen, *sint_ptr);
break;
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
uint_ptr = (u32_t*)vb->value;
snmp_asn1_enc_u32t(p, ofs, vb->vlen, *uint_ptr);
break;
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
raw_ptr = (u8_t*)vb->value;
snmp_asn1_enc_raw(p, ofs, vb->vlen, raw_ptr);
break;
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
break;
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
sint_ptr = (s32_t*)vb->value;
snmp_asn1_enc_oid(p, ofs, vb->value_len / sizeof(s32_t), sint_ptr);
break;
default:
/* unsupported type */
break;
}
ofs += vb->vlen;
vb = vb->next;
}
return ofs;
}
#endif /* LWIP_SNMP */