3 * MIB tree access/construction functions.
7 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * Author: Christiaan Simons <christiaan.simons@axon.tv>
37 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
39 #include "lwip/snmp_structs.h"
40 #include "lwip/memp.h"
41 #include "lwip/netif.h"
43 /** .iso.org.dod.internet address prefix, @see snmp_iso_*() */
44 const s32_t prefix[4] = {1, 3, 6, 1};
46 #define NODE_STACK_SIZE (LWIP_SNMP_OBJ_ID_LEN)
47 /** node stack entry (old news?) */
51 struct mib_node* r_ptr;
52 /** right child identifier */
54 /** right child next level */
57 static u8_t node_stack_cnt;
58 static struct nse node_stack[NODE_STACK_SIZE];
61 * Pushes nse struct onto stack.
64 push_node(struct nse* node)
66 LWIP_ASSERT("node_stack_cnt < NODE_STACK_SIZE",node_stack_cnt < NODE_STACK_SIZE);
67 LWIP_DEBUGF(SNMP_MIB_DEBUG,("push_node() node=%p id=%"S32_F"\n",(void*)(node->r_ptr),node->r_id));
68 if (node_stack_cnt < NODE_STACK_SIZE)
70 node_stack[node_stack_cnt] = *node;
76 * Pops nse struct from stack.
79 pop_node(struct nse* node)
81 if (node_stack_cnt > 0)
84 *node = node_stack[node_stack_cnt];
86 LWIP_DEBUGF(SNMP_MIB_DEBUG,("pop_node() node=%p id=%"S32_F"\n",(void *)(node->r_ptr),node->r_id));
90 * Conversion from ifIndex to lwIP netif
91 * @param ifindex is a s32_t object sub-identifier
92 * @param netif points to returned netif struct pointer
95 snmp_ifindextonetif(s32_t ifindex, struct netif **netif)
97 struct netif *nif = netif_list;
102 while ((nif != NULL) && (i < ifidx))
111 * Conversion from lwIP netif to ifIndex
112 * @param netif points to a netif struct
113 * @param ifidx points to s32_t object sub-identifier
116 snmp_netiftoifindex(struct netif *netif, s32_t *ifidx)
118 struct netif *nif = netif_list;
122 while ((nif != NULL) && (nif != netif))
131 * Conversion from oid to lwIP ip_addr
132 * @param ident points to s32_t ident[4] input
133 * @param ip points to output struct
136 snmp_oidtoip(s32_t *ident, ip_addr_t *ip)
138 IP4_ADDR(ip, ident[0], ident[1], ident[2], ident[3]);
142 * Conversion from lwIP ip_addr to oid
143 * @param ip points to input struct
144 * @param ident points to s32_t ident[4] output
147 snmp_iptooid(ip_addr_t *ip, s32_t *ident)
149 ident[0] = ip4_addr1(ip);
150 ident[1] = ip4_addr2(ip);
151 ident[2] = ip4_addr3(ip);
152 ident[3] = ip4_addr4(ip);
155 struct mib_list_node *
156 snmp_mib_ln_alloc(s32_t id)
158 struct mib_list_node *ln;
160 ln = (struct mib_list_node *)memp_malloc(MEMP_SNMP_NODE);
172 snmp_mib_ln_free(struct mib_list_node *ln)
174 memp_free(MEMP_SNMP_NODE, ln);
177 struct mib_list_rootnode *
178 snmp_mib_lrn_alloc(void)
180 struct mib_list_rootnode *lrn;
182 lrn = (struct mib_list_rootnode*)memp_malloc(MEMP_SNMP_ROOTNODE);
185 lrn->get_object_def = noleafs_get_object_def;
186 lrn->get_value = noleafs_get_value;
187 lrn->set_test = noleafs_set_test;
188 lrn->set_value = noleafs_set_value;
189 lrn->node_type = MIB_NODE_LR;
199 snmp_mib_lrn_free(struct mib_list_rootnode *lrn)
201 memp_free(MEMP_SNMP_ROOTNODE, lrn);
205 * Inserts node in idx list in a sorted
206 * (ascending order) fashion and
207 * allocates the node if needed.
209 * @param rn points to the root node
210 * @param objid is the object sub identifier
211 * @param insn points to a pointer to the inserted node
212 * used for constructing the tree.
213 * @return -1 if failed, 1 if inserted, 2 if present.
216 snmp_mib_node_insert(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **insn)
218 struct mib_list_node *nn;
221 LWIP_ASSERT("rn != NULL",rn != NULL);
223 /* -1 = malloc failure, 0 = not inserted, 1 = inserted, 2 = was present */
225 if (rn->head == NULL)
227 /* empty list, add first node */
228 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc empty list objid==%"S32_F"\n",objid));
229 nn = snmp_mib_ln_alloc(objid);
244 struct mib_list_node *n;
245 /* at least one node is present */
247 while ((n != NULL) && (insert == 0))
249 if (n->objid == objid)
251 /* node is already there */
252 LWIP_DEBUGF(SNMP_MIB_DEBUG,("node already there objid==%"S32_F"\n",objid));
256 else if (n->objid < objid)
260 /* alloc and insert at the tail */
261 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins tail objid==%"S32_F"\n",objid));
262 nn = snmp_mib_ln_alloc(objid);
274 /* insertion failure */
280 /* there's more to explore: traverse list */
281 LWIP_DEBUGF(SNMP_MIB_DEBUG,("traverse list\n"));
287 /* n->objid > objid */
288 /* alloc and insert between n->prev and n */
289 LWIP_DEBUGF(SNMP_MIB_DEBUG,("alloc ins n->prev, objid==%"S32_F", n\n",objid));
290 nn = snmp_mib_ln_alloc(objid);
295 /* insert at the head */
303 /* insert in the middle */
314 /* insertion failure */
324 LWIP_ASSERT("insert != 0",insert != 0);
329 * Finds node in idx list and returns deletion mark.
331 * @param rn points to the root node
332 * @param objid is the object sub identifier
333 * @param fn returns pointer to found node
334 * @return 0 if not found, 1 if deletable,
335 * 2 can't delete (2 or more children), 3 not a list_node
338 snmp_mib_node_find(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **fn)
341 struct mib_list_node *n;
343 LWIP_ASSERT("rn != NULL",rn != NULL);
345 while ((n != NULL) && (n->objid != objid))
353 else if (n->nptr == NULL)
355 /* leaf, can delete node */
360 struct mib_list_rootnode *r;
362 if (n->nptr->node_type == MIB_NODE_LR)
364 r = (struct mib_list_rootnode *)n->nptr;
367 /* can't delete node */
372 /* count <= 1, can delete node */
378 /* other node type */
387 * Removes node from idx list
388 * if it has a single child left.
390 * @param rn points to the root node
391 * @param n points to the node to delete
392 * @return the nptr to be freed by caller
394 struct mib_list_rootnode *
395 snmp_mib_node_delete(struct mib_list_rootnode *rn, struct mib_list_node *n)
397 struct mib_list_rootnode *next;
399 LWIP_ASSERT("rn != NULL",rn != NULL);
400 LWIP_ASSERT("n != NULL",n != NULL);
402 /* caller must remove this sub-tree */
403 next = (struct mib_list_rootnode*)(n->nptr);
411 /* not last node, new list begin */
412 n->next->prev = NULL;
415 else if (n == rn->tail)
420 /* not last node, new list end */
421 n->prev->next = NULL;
426 /* node must be in the middle */
427 n->prev->next = n->next;
428 n->next->prev = n->prev;
430 LWIP_DEBUGF(SNMP_MIB_DEBUG,("free list objid==%"S32_F"\n",n->objid));
443 * Searches tree for the supplied (scalar?) object identifier.
445 * @param node points to the root of the tree ('.internet')
446 * @param ident_len the length of the supplied object identifier
447 * @param ident points to the array of sub identifiers
448 * @param np points to the found object instance (return)
449 * @return pointer to the requested parent (!) node if success, NULL otherwise
452 snmp_search_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_name_ptr *np)
454 u8_t node_type, ext_level;
457 LWIP_DEBUGF(SNMP_MIB_DEBUG,("node==%p *ident==%"S32_F"\n",(void*)node,*ident));
460 node_type = node->node_type;
461 if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
463 struct mib_array_node *an;
468 /* array node (internal ROM or RAM, fixed length) */
469 an = (struct mib_array_node *)node;
471 while ((i < an->maxlength) && (an->objid[i] != *ident))
475 if (i < an->maxlength)
477 /* found it, if available proceed to child, otherwise inspect leaf */
478 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->objid[i],*ident));
479 if (an->nptr[i] == NULL)
481 /* a scalar leaf OR table,
482 inspect remaining instance number / table index */
483 np->ident_len = ident_len;
485 return (struct mib_node*)an;
489 /* follow next child pointer */
497 /* search failed, identifier mismatch (nosuchname) */
498 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed *ident==%"S32_F"\n",*ident));
504 /* search failed, short object identifier (nosuchname) */
505 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an search failed, short object identifier\n"));
509 else if(node_type == MIB_NODE_LR)
511 struct mib_list_rootnode *lrn;
512 struct mib_list_node *ln;
516 /* list root node (internal 'RAM', variable length) */
517 lrn = (struct mib_list_rootnode *)node;
519 /* iterate over list, head to tail */
520 while ((ln != NULL) && (ln->objid != *ident))
526 /* found it, proceed to child */;
527 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
528 if (ln->nptr == NULL)
530 np->ident_len = ident_len;
532 return (struct mib_node*)lrn;
536 /* follow next child pointer */
545 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed *ident==%"S32_F"\n",*ident));
551 /* search failed, short object identifier (nosuchname) */
552 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln search failed, short object identifier\n"));
556 else if(node_type == MIB_NODE_EX)
558 struct mib_external_node *en;
563 /* external node (addressing and access via functions) */
564 en = (struct mib_external_node *)node;
567 len = en->level_length(en->addr_inf,ext_level);
568 while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) != 0))
576 en->get_objid(en->addr_inf,ext_level,i,&debug_id);
577 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid==%"S32_F" *ident==%"S32_F"\n",debug_id,*ident));
578 if ((ext_level + 1) == en->tree_levels)
580 np->ident_len = ident_len;
582 return (struct mib_node*)en;
586 /* found it, proceed to child */
595 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed *ident==%"S32_F"\n",*ident));
601 /* search failed, short object identifier (nosuchname) */
602 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en search failed, short object identifier\n"));
606 else if (node_type == MIB_NODE_SC)
610 sn = (mib_scalar_node *)node;
611 if ((ident_len == 1) && (*ident == 0))
613 np->ident_len = ident_len;
615 return (struct mib_node*)sn;
619 /* search failed, short object identifier (nosuchname) */
620 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed, invalid object identifier length\n"));
626 /* unknown node_type */
627 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node_type %"U16_F" unkown\n",(u16_t)node_type));
631 /* done, found nothing */
632 LWIP_DEBUGF(SNMP_MIB_DEBUG,("search failed node==%p\n",(void*)node));
637 * Test table for presence of at least one table entry.
640 empty_table(struct mib_node *node)
647 node_type = node->node_type;
648 if (node_type == MIB_NODE_LR)
650 struct mib_list_rootnode *lrn;
651 lrn = (struct mib_list_rootnode *)node;
652 if ((lrn->count == 0) || (lrn->head == NULL))
657 else if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
659 struct mib_array_node *an;
660 an = (struct mib_array_node *)node;
661 if ((an->maxlength == 0) || (an->nptr == NULL))
666 else if (node_type == MIB_NODE_EX)
668 struct mib_external_node *en;
669 en = (struct mib_external_node *)node;
670 if (en->tree_levels == 0)
683 snmp_expand_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
685 u8_t node_type, ext_level, climb_tree;
688 /* reset node stack */
693 node_type = node->node_type;
694 if ((node_type == MIB_NODE_AR) || (node_type == MIB_NODE_RA))
696 struct mib_array_node *an;
699 /* array node (internal ROM or RAM, fixed length) */
700 an = (struct mib_array_node *)node;
704 while ((i < an->maxlength) && (an->objid[i] < *ident))
708 if (i < an->maxlength)
710 LWIP_DEBUGF(SNMP_MIB_DEBUG,("an->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,an->objid[i],*ident));
711 /* add identifier to oidret */
712 oidret->id[oidret->len] = an->objid[i];
715 if (an->nptr[i] == NULL)
717 LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
718 /* leaf node (e.g. in a fixed size table) */
719 if (an->objid[i] > *ident)
721 return (struct mib_node*)an;
723 else if ((i + 1) < an->maxlength)
725 /* an->objid[i] == *ident */
727 oidret->id[oidret->len] = an->objid[i + 1];
729 return (struct mib_node*)an;
733 /* (i + 1) == an->maxlength */
743 LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
744 /* non-leaf, store right child ptr and id */
745 LWIP_ASSERT("i < 0xff", i < 0xff);
747 while ((j < an->maxlength) && (empty_table(an->nptr[j])))
751 if (j < an->maxlength)
753 cur_node.r_ptr = an->nptr[j];
754 cur_node.r_id = an->objid[j];
759 cur_node.r_ptr = NULL;
761 push_node(&cur_node);
762 if (an->objid[i] == *ident)
769 /* an->objid[i] < *ident */
772 /* follow next child pointer */
778 /* i == an->maxlength */
785 /* ident_len == 0, complete with leftmost '.thing' */
787 while ((j < an->maxlength) && empty_table(an->nptr[j]))
791 if (j < an->maxlength)
793 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left an->objid[j]==%"S32_F"\n",an->objid[j]));
794 oidret->id[oidret->len] = an->objid[j];
796 if (an->nptr[j] == NULL)
799 return (struct mib_node*)an;
803 /* no leaf, continue */
809 /* j == an->maxlength */
814 else if(node_type == MIB_NODE_LR)
816 struct mib_list_rootnode *lrn;
817 struct mib_list_node *ln;
819 /* list root node (internal 'RAM', variable length) */
820 lrn = (struct mib_list_rootnode *)node;
824 /* iterate over list, head to tail */
825 while ((ln != NULL) && (ln->objid < *ident))
831 LWIP_DEBUGF(SNMP_MIB_DEBUG,("ln->objid==%"S32_F" *ident==%"S32_F"\n",ln->objid,*ident));
832 oidret->id[oidret->len] = ln->objid;
834 if (ln->nptr == NULL)
837 if (ln->objid > *ident)
839 return (struct mib_node*)lrn;
841 else if (ln->next != NULL)
843 /* ln->objid == *ident */
845 oidret->id[oidret->len] = ln->next->objid;
847 return (struct mib_node*)lrn;
851 /* ln->next == NULL */
858 struct mib_list_node *jn;
861 /* non-leaf, store right child ptr and id */
863 while ((jn != NULL) && empty_table(jn->nptr))
869 cur_node.r_ptr = jn->nptr;
870 cur_node.r_id = jn->objid;
875 cur_node.r_ptr = NULL;
877 push_node(&cur_node);
878 if (ln->objid == *ident)
885 /* ln->objid < *ident */
888 /* follow next child pointer */
901 struct mib_list_node *jn;
902 /* ident_len == 0, complete with leftmost '.thing' */
904 while ((jn != NULL) && empty_table(jn->nptr))
910 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left jn->objid==%"S32_F"\n",jn->objid));
911 oidret->id[oidret->len] = jn->objid;
913 if (jn->nptr == NULL)
916 LWIP_DEBUGF(SNMP_MIB_DEBUG,("jn->nptr == NULL\n"));
917 return (struct mib_node*)lrn;
921 /* no leaf, continue */
932 else if(node_type == MIB_NODE_EX)
934 struct mib_external_node *en;
937 /* external node (addressing and access via functions) */
938 en = (struct mib_external_node *)node;
944 len = en->level_length(en->addr_inf,ext_level);
945 while ((i < len) && (en->ident_cmp(en->addr_inf,ext_level,i,*ident) < 0))
951 /* add identifier to oidret */
952 en->get_objid(en->addr_inf,ext_level,i,&ex_id);
953 LWIP_DEBUGF(SNMP_MIB_DEBUG,("en->objid[%"U16_F"]==%"S32_F" *ident==%"S32_F"\n",i,ex_id,*ident));
954 oidret->id[oidret->len] = ex_id;
957 if ((ext_level + 1) == en->tree_levels)
959 LWIP_DEBUGF(SNMP_MIB_DEBUG,("leaf node\n"));
963 return (struct mib_node*)en;
965 else if ((i + 1) < len)
967 /* ex_id == *ident */
968 en->get_objid(en->addr_inf,ext_level,i + 1,&ex_id);
970 oidret->id[oidret->len] = ex_id;
972 return (struct mib_node*)en;
986 LWIP_DEBUGF(SNMP_MIB_DEBUG,("non-leaf node\n"));
987 /* non-leaf, store right child ptr and id */
988 LWIP_ASSERT("i < 0xff", i < 0xff);
992 /* right node is the current external node */
993 cur_node.r_ptr = node;
994 en->get_objid(en->addr_inf,ext_level,j,&cur_node.r_id);
995 cur_node.r_nl = ext_level + 1;
999 cur_node.r_ptr = NULL;
1001 push_node(&cur_node);
1002 if (en->ident_cmp(en->addr_inf,ext_level,i,*ident) == 0)
1009 /* external id < *ident */
1012 /* proceed to child */
1018 /* i == len (en->level_len()) */
1024 /* ident_len == 0, complete with leftmost '.thing' */
1025 en->get_objid(en->addr_inf,ext_level,0,&ex_id);
1026 LWIP_DEBUGF(SNMP_MIB_DEBUG,("left en->objid==%"S32_F"\n",ex_id));
1027 oidret->id[oidret->len] = ex_id;
1029 if ((ext_level + 1) == en->tree_levels)
1032 LWIP_DEBUGF(SNMP_MIB_DEBUG,("(ext_level + 1) == en->tree_levels\n"));
1033 return (struct mib_node*)en;
1037 /* no leaf, proceed to child */
1042 else if(node_type == MIB_NODE_SC)
1044 mib_scalar_node *sn;
1047 sn = (mib_scalar_node *)node;
1055 /* ident_len == 0, complete object identifier */
1056 oidret->id[oidret->len] = 0;
1059 LWIP_DEBUGF(SNMP_MIB_DEBUG,("completed scalar leaf\n"));
1060 return (struct mib_node*)sn;
1065 /* unknown/unhandled node_type */
1066 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node_type %"U16_F" unkown\n",(u16_t)node_type));
1074 /* find right child ptr */
1078 while ((node_stack_cnt > 0) && (child.r_ptr == NULL))
1081 /* trim returned oid */
1084 if (child.r_ptr != NULL)
1086 /* incoming ident is useless beyond this point */
1088 oidret->id[oidret->len] = child.r_id;
1091 ext_level = child.r_nl;
1095 /* tree ends here ... */
1096 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed, tree ends here\n"));
1101 /* done, found nothing */
1102 LWIP_DEBUGF(SNMP_MIB_DEBUG,("expand failed node==%p\n",(void*)node));
1107 * Test object identifier for the iso.org.dod.internet prefix.
1109 * @param ident_len the length of the supplied object identifier
1110 * @param ident points to the array of sub identifiers
1111 * @return 1 if it matches, 0 otherwise
1114 snmp_iso_prefix_tst(u8_t ident_len, s32_t *ident)
1116 if ((ident_len > 3) &&
1117 (ident[0] == 1) && (ident[1] == 3) &&
1118 (ident[2] == 6) && (ident[3] == 1))
1129 * Expands object identifier to the iso.org.dod.internet
1130 * prefix for use in getnext operation.
1132 * @param ident_len the length of the supplied object identifier
1133 * @param ident points to the array of sub identifiers
1134 * @param oidret points to returned expanded object identifier
1135 * @return 1 if it matches, 0 otherwise
1137 * @note ident_len 0 is allowed, expanding to the first known object id!!
1140 snmp_iso_prefix_expand(u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret)
1142 const s32_t *prefix_ptr;
1147 prefix_ptr = &prefix[0];
1148 ret_ptr = &oidret->id[0];
1149 ident_len = ((ident_len < 4)?ident_len:4);
1150 while ((i < ident_len) && ((*ident) <= (*prefix_ptr)))
1152 *ret_ptr++ = *prefix_ptr++;
1158 /* match, complete missing bits */
1161 *ret_ptr++ = *prefix_ptr++;
1169 /* i != ident_len */
1174 #endif /* LWIP_SNMP */