I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1 /**
tax 0:66300c77c6e9 2 * @file
tax 0:66300c77c6e9 3 * SNMP input message processing (RFC1157).
tax 0:66300c77c6e9 4 */
tax 0:66300c77c6e9 5
tax 0:66300c77c6e9 6 /*
tax 0:66300c77c6e9 7 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
tax 0:66300c77c6e9 8 * All rights reserved.
tax 0:66300c77c6e9 9 *
tax 0:66300c77c6e9 10 * Redistribution and use in source and binary forms, with or without modification,
tax 0:66300c77c6e9 11 * are permitted provided that the following conditions are met:
tax 0:66300c77c6e9 12 *
tax 0:66300c77c6e9 13 * 1. Redistributions of source code must retain the above copyright notice,
tax 0:66300c77c6e9 14 * this list of conditions and the following disclaimer.
tax 0:66300c77c6e9 15 * 2. Redistributions in binary form must reproduce the above copyright notice,
tax 0:66300c77c6e9 16 * this list of conditions and the following disclaimer in the documentation
tax 0:66300c77c6e9 17 * and/or other materials provided with the distribution.
tax 0:66300c77c6e9 18 * 3. The name of the author may not be used to endorse or promote products
tax 0:66300c77c6e9 19 * derived from this software without specific prior written permission.
tax 0:66300c77c6e9 20 *
tax 0:66300c77c6e9 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
tax 0:66300c77c6e9 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
tax 0:66300c77c6e9 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
tax 0:66300c77c6e9 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
tax 0:66300c77c6e9 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
tax 0:66300c77c6e9 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
tax 0:66300c77c6e9 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
tax 0:66300c77c6e9 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
tax 0:66300c77c6e9 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
tax 0:66300c77c6e9 30 * OF SUCH DAMAGE.
tax 0:66300c77c6e9 31 *
tax 0:66300c77c6e9 32 * Author: Christiaan Simons <christiaan.simons@axon.tv>
tax 0:66300c77c6e9 33 */
tax 0:66300c77c6e9 34
tax 0:66300c77c6e9 35 #include "lwip/opt.h"
tax 0:66300c77c6e9 36
tax 0:66300c77c6e9 37 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
tax 0:66300c77c6e9 38
tax 0:66300c77c6e9 39 #include "lwip/snmp.h"
tax 0:66300c77c6e9 40 #include "lwip/snmp_asn1.h"
tax 0:66300c77c6e9 41 #include "lwip/snmp_msg.h"
tax 0:66300c77c6e9 42 #include "lwip/snmp_structs.h"
tax 0:66300c77c6e9 43 #include "lwip/ip_addr.h"
tax 0:66300c77c6e9 44 #include "lwip/memp.h"
tax 0:66300c77c6e9 45 #include "lwip/udp.h"
tax 0:66300c77c6e9 46 #include "lwip/stats.h"
tax 0:66300c77c6e9 47
tax 0:66300c77c6e9 48 #include <string.h>
tax 0:66300c77c6e9 49
tax 0:66300c77c6e9 50 /* public (non-static) constants */
tax 0:66300c77c6e9 51 /** SNMP v1 == 0 */
tax 0:66300c77c6e9 52 const s32_t snmp_version = 0;
tax 0:66300c77c6e9 53 /** default SNMP community string */
tax 0:66300c77c6e9 54 const char snmp_publiccommunity[7] = "public";
tax 0:66300c77c6e9 55
tax 0:66300c77c6e9 56 /* statically allocated buffers for SNMP_CONCURRENT_REQUESTS */
tax 0:66300c77c6e9 57 struct snmp_msg_pstat msg_input_list[SNMP_CONCURRENT_REQUESTS];
tax 0:66300c77c6e9 58 /* UDP Protocol Control Block */
tax 0:66300c77c6e9 59 struct udp_pcb *snmp1_pcb;
tax 0:66300c77c6e9 60
tax 0:66300c77c6e9 61 static void snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
tax 0:66300c77c6e9 62 static err_t snmp_pdu_header_check(struct pbuf *p, u16_t ofs, u16_t pdu_len, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat);
tax 0:66300c77c6e9 63 static err_t snmp_pdu_dec_varbindlist(struct pbuf *p, u16_t ofs, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat);
tax 0:66300c77c6e9 64
tax 0:66300c77c6e9 65
tax 0:66300c77c6e9 66 /**
tax 0:66300c77c6e9 67 * Starts SNMP Agent.
tax 0:66300c77c6e9 68 * Allocates UDP pcb and binds it to IP_ADDR_ANY port 161.
tax 0:66300c77c6e9 69 */
tax 0:66300c77c6e9 70 void
tax 0:66300c77c6e9 71 snmp_init(void)
tax 0:66300c77c6e9 72 {
tax 0:66300c77c6e9 73 struct snmp_msg_pstat *msg_ps;
tax 0:66300c77c6e9 74 u8_t i;
tax 0:66300c77c6e9 75
tax 0:66300c77c6e9 76 snmp1_pcb = udp_new();
tax 0:66300c77c6e9 77 if (snmp1_pcb != NULL)
tax 0:66300c77c6e9 78 {
tax 0:66300c77c6e9 79 udp_recv(snmp1_pcb, snmp_recv, (void *)SNMP_IN_PORT);
tax 0:66300c77c6e9 80 udp_bind(snmp1_pcb, IP_ADDR_ANY, SNMP_IN_PORT);
tax 0:66300c77c6e9 81 }
tax 0:66300c77c6e9 82 msg_ps = &msg_input_list[0];
tax 0:66300c77c6e9 83 for (i=0; i<SNMP_CONCURRENT_REQUESTS; i++)
tax 0:66300c77c6e9 84 {
tax 0:66300c77c6e9 85 msg_ps->state = SNMP_MSG_EMPTY;
tax 0:66300c77c6e9 86 msg_ps->error_index = 0;
tax 0:66300c77c6e9 87 msg_ps->error_status = SNMP_ES_NOERROR;
tax 0:66300c77c6e9 88 msg_ps++;
tax 0:66300c77c6e9 89 }
tax 0:66300c77c6e9 90 trap_msg.pcb = snmp1_pcb;
tax 0:66300c77c6e9 91
tax 0:66300c77c6e9 92 #ifdef SNMP_PRIVATE_MIB_INIT
tax 0:66300c77c6e9 93 /* If defined, rhis must be a function-like define to initialize the
tax 0:66300c77c6e9 94 * private MIB after the stack has been initialized.
tax 0:66300c77c6e9 95 * The private MIB can also be initialized in tcpip_callback (or after
tax 0:66300c77c6e9 96 * the stack is initialized), this define is only for convenience. */
tax 0:66300c77c6e9 97 SNMP_PRIVATE_MIB_INIT();
tax 0:66300c77c6e9 98 #endif /* SNMP_PRIVATE_MIB_INIT */
tax 0:66300c77c6e9 99
tax 0:66300c77c6e9 100 /* The coldstart trap will only be output
tax 0:66300c77c6e9 101 if our outgoing interface is up & configured */
tax 0:66300c77c6e9 102 snmp_coldstart_trap();
tax 0:66300c77c6e9 103 }
tax 0:66300c77c6e9 104
tax 0:66300c77c6e9 105 static void
tax 0:66300c77c6e9 106 snmp_error_response(struct snmp_msg_pstat *msg_ps, u8_t error)
tax 0:66300c77c6e9 107 {
tax 0:66300c77c6e9 108 snmp_varbind_list_free(&msg_ps->outvb);
tax 0:66300c77c6e9 109 msg_ps->outvb = msg_ps->invb;
tax 0:66300c77c6e9 110 msg_ps->invb.head = NULL;
tax 0:66300c77c6e9 111 msg_ps->invb.tail = NULL;
tax 0:66300c77c6e9 112 msg_ps->invb.count = 0;
tax 0:66300c77c6e9 113 msg_ps->error_status = error;
tax 0:66300c77c6e9 114 msg_ps->error_index = 1 + msg_ps->vb_idx;
tax 0:66300c77c6e9 115 snmp_send_response(msg_ps);
tax 0:66300c77c6e9 116 snmp_varbind_list_free(&msg_ps->outvb);
tax 0:66300c77c6e9 117 msg_ps->state = SNMP_MSG_EMPTY;
tax 0:66300c77c6e9 118 }
tax 0:66300c77c6e9 119
tax 0:66300c77c6e9 120 static void
tax 0:66300c77c6e9 121 snmp_ok_response(struct snmp_msg_pstat *msg_ps)
tax 0:66300c77c6e9 122 {
tax 0:66300c77c6e9 123 err_t err_ret;
tax 0:66300c77c6e9 124
tax 0:66300c77c6e9 125 err_ret = snmp_send_response(msg_ps);
tax 0:66300c77c6e9 126 if (err_ret == ERR_MEM)
tax 0:66300c77c6e9 127 {
tax 0:66300c77c6e9 128 /* serious memory problem, can't return tooBig */
tax 0:66300c77c6e9 129 }
tax 0:66300c77c6e9 130 else
tax 0:66300c77c6e9 131 {
tax 0:66300c77c6e9 132 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event = %"S32_F"\n",msg_ps->error_status));
tax 0:66300c77c6e9 133 }
tax 0:66300c77c6e9 134 /* free varbinds (if available) */
tax 0:66300c77c6e9 135 snmp_varbind_list_free(&msg_ps->invb);
tax 0:66300c77c6e9 136 snmp_varbind_list_free(&msg_ps->outvb);
tax 0:66300c77c6e9 137 msg_ps->state = SNMP_MSG_EMPTY;
tax 0:66300c77c6e9 138 }
tax 0:66300c77c6e9 139
tax 0:66300c77c6e9 140 /**
tax 0:66300c77c6e9 141 * Service an internal or external event for SNMP GET.
tax 0:66300c77c6e9 142 *
tax 0:66300c77c6e9 143 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
tax 0:66300c77c6e9 144 * @param msg_ps points to the assosicated message process state
tax 0:66300c77c6e9 145 */
tax 0:66300c77c6e9 146 static void
tax 0:66300c77c6e9 147 snmp_msg_get_event(u8_t request_id, struct snmp_msg_pstat *msg_ps)
tax 0:66300c77c6e9 148 {
tax 0:66300c77c6e9 149 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_get_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state));
tax 0:66300c77c6e9 150
tax 0:66300c77c6e9 151 if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF)
tax 0:66300c77c6e9 152 {
tax 0:66300c77c6e9 153 struct mib_external_node *en;
tax 0:66300c77c6e9 154 struct snmp_name_ptr np;
tax 0:66300c77c6e9 155
tax 0:66300c77c6e9 156 /* get_object_def() answer*/
tax 0:66300c77c6e9 157 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 158 np = msg_ps->ext_name_ptr;
tax 0:66300c77c6e9 159
tax 0:66300c77c6e9 160 /* translate answer into a known lifeform */
tax 0:66300c77c6e9 161 en->get_object_def_a(request_id, np.ident_len, np.ident, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 162 if ((msg_ps->ext_object_def.instance != MIB_OBJECT_NONE) &&
tax 0:66300c77c6e9 163 (msg_ps->ext_object_def.access & MIB_ACCESS_READ))
tax 0:66300c77c6e9 164 {
tax 0:66300c77c6e9 165 msg_ps->state = SNMP_MSG_EXTERNAL_GET_VALUE;
tax 0:66300c77c6e9 166 en->get_value_q(request_id, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 167 }
tax 0:66300c77c6e9 168 else
tax 0:66300c77c6e9 169 {
tax 0:66300c77c6e9 170 en->get_object_def_pc(request_id, np.ident_len, np.ident);
tax 0:66300c77c6e9 171 /* search failed, object id points to unknown object (nosuchname) */
tax 0:66300c77c6e9 172 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 173 }
tax 0:66300c77c6e9 174 }
tax 0:66300c77c6e9 175 else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_VALUE)
tax 0:66300c77c6e9 176 {
tax 0:66300c77c6e9 177 struct mib_external_node *en;
tax 0:66300c77c6e9 178 struct snmp_varbind *vb;
tax 0:66300c77c6e9 179
tax 0:66300c77c6e9 180 /* get_value() answer */
tax 0:66300c77c6e9 181 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 182
tax 0:66300c77c6e9 183 /* allocate output varbind */
tax 0:66300c77c6e9 184 vb = (struct snmp_varbind *)memp_malloc(MEMP_SNMP_VARBIND);
tax 0:66300c77c6e9 185 LWIP_ASSERT("vb != NULL",vb != NULL);
tax 0:66300c77c6e9 186 if (vb != NULL)
tax 0:66300c77c6e9 187 {
tax 0:66300c77c6e9 188 vb->next = NULL;
tax 0:66300c77c6e9 189 vb->prev = NULL;
tax 0:66300c77c6e9 190
tax 0:66300c77c6e9 191 /* move name from invb to outvb */
tax 0:66300c77c6e9 192 vb->ident = msg_ps->vb_ptr->ident;
tax 0:66300c77c6e9 193 vb->ident_len = msg_ps->vb_ptr->ident_len;
tax 0:66300c77c6e9 194 /* ensure this memory is refereced once only */
tax 0:66300c77c6e9 195 msg_ps->vb_ptr->ident = NULL;
tax 0:66300c77c6e9 196 msg_ps->vb_ptr->ident_len = 0;
tax 0:66300c77c6e9 197
tax 0:66300c77c6e9 198 vb->value_type = msg_ps->ext_object_def.asn_type;
tax 0:66300c77c6e9 199 LWIP_ASSERT("invalid length", msg_ps->ext_object_def.v_len <= 0xff);
tax 0:66300c77c6e9 200 vb->value_len = (u8_t)msg_ps->ext_object_def.v_len;
tax 0:66300c77c6e9 201 if (vb->value_len > 0)
tax 0:66300c77c6e9 202 {
tax 0:66300c77c6e9 203 LWIP_ASSERT("SNMP_MAX_OCTET_STRING_LEN is configured too low", vb->value_len <= SNMP_MAX_VALUE_SIZE);
tax 0:66300c77c6e9 204 vb->value = memp_malloc(MEMP_SNMP_VALUE);
tax 0:66300c77c6e9 205 LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
tax 0:66300c77c6e9 206 if (vb->value != NULL)
tax 0:66300c77c6e9 207 {
tax 0:66300c77c6e9 208 en->get_value_a(request_id, &msg_ps->ext_object_def, vb->value_len, vb->value);
tax 0:66300c77c6e9 209 snmp_varbind_tail_add(&msg_ps->outvb, vb);
tax 0:66300c77c6e9 210 /* search again (if vb_idx < msg_ps->invb.count) */
tax 0:66300c77c6e9 211 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 212 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 213 }
tax 0:66300c77c6e9 214 else
tax 0:66300c77c6e9 215 {
tax 0:66300c77c6e9 216 en->get_value_pc(request_id, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 217 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no variable space\n"));
tax 0:66300c77c6e9 218 msg_ps->vb_ptr->ident = vb->ident;
tax 0:66300c77c6e9 219 msg_ps->vb_ptr->ident_len = vb->ident_len;
tax 0:66300c77c6e9 220 memp_free(MEMP_SNMP_VARBIND, vb);
tax 0:66300c77c6e9 221 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
tax 0:66300c77c6e9 222 }
tax 0:66300c77c6e9 223 }
tax 0:66300c77c6e9 224 else
tax 0:66300c77c6e9 225 {
tax 0:66300c77c6e9 226 /* vb->value_len == 0, empty value (e.g. empty string) */
tax 0:66300c77c6e9 227 en->get_value_a(request_id, &msg_ps->ext_object_def, 0, NULL);
tax 0:66300c77c6e9 228 vb->value = NULL;
tax 0:66300c77c6e9 229 snmp_varbind_tail_add(&msg_ps->outvb, vb);
tax 0:66300c77c6e9 230 /* search again (if vb_idx < msg_ps->invb.count) */
tax 0:66300c77c6e9 231 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 232 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 233 }
tax 0:66300c77c6e9 234 }
tax 0:66300c77c6e9 235 else
tax 0:66300c77c6e9 236 {
tax 0:66300c77c6e9 237 en->get_value_pc(request_id, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 238 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no outvb space\n"));
tax 0:66300c77c6e9 239 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
tax 0:66300c77c6e9 240 }
tax 0:66300c77c6e9 241 }
tax 0:66300c77c6e9 242
tax 0:66300c77c6e9 243 while ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
tax 0:66300c77c6e9 244 (msg_ps->vb_idx < msg_ps->invb.count))
tax 0:66300c77c6e9 245 {
tax 0:66300c77c6e9 246 struct mib_node *mn;
tax 0:66300c77c6e9 247 struct snmp_name_ptr np;
tax 0:66300c77c6e9 248
tax 0:66300c77c6e9 249 if (msg_ps->vb_idx == 0)
tax 0:66300c77c6e9 250 {
tax 0:66300c77c6e9 251 msg_ps->vb_ptr = msg_ps->invb.head;
tax 0:66300c77c6e9 252 }
tax 0:66300c77c6e9 253 else
tax 0:66300c77c6e9 254 {
tax 0:66300c77c6e9 255 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
tax 0:66300c77c6e9 256 }
tax 0:66300c77c6e9 257 /** test object identifier for .iso.org.dod.internet prefix */
tax 0:66300c77c6e9 258 if (snmp_iso_prefix_tst(msg_ps->vb_ptr->ident_len, msg_ps->vb_ptr->ident))
tax 0:66300c77c6e9 259 {
tax 0:66300c77c6e9 260 mn = snmp_search_tree((struct mib_node*)&internet, msg_ps->vb_ptr->ident_len - 4,
tax 0:66300c77c6e9 261 msg_ps->vb_ptr->ident + 4, &np);
tax 0:66300c77c6e9 262 if (mn != NULL)
tax 0:66300c77c6e9 263 {
tax 0:66300c77c6e9 264 if (mn->node_type == MIB_NODE_EX)
tax 0:66300c77c6e9 265 {
tax 0:66300c77c6e9 266 /* external object */
tax 0:66300c77c6e9 267 struct mib_external_node *en = (struct mib_external_node*)mn;
tax 0:66300c77c6e9 268
tax 0:66300c77c6e9 269 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF;
tax 0:66300c77c6e9 270 /* save en && args in msg_ps!! */
tax 0:66300c77c6e9 271 msg_ps->ext_mib_node = en;
tax 0:66300c77c6e9 272 msg_ps->ext_name_ptr = np;
tax 0:66300c77c6e9 273
tax 0:66300c77c6e9 274 en->get_object_def_q(en->addr_inf, request_id, np.ident_len, np.ident);
tax 0:66300c77c6e9 275 }
tax 0:66300c77c6e9 276 else
tax 0:66300c77c6e9 277 {
tax 0:66300c77c6e9 278 /* internal object */
tax 0:66300c77c6e9 279 struct obj_def object_def;
tax 0:66300c77c6e9 280
tax 0:66300c77c6e9 281 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF;
tax 0:66300c77c6e9 282 mn->get_object_def(np.ident_len, np.ident, &object_def);
tax 0:66300c77c6e9 283 if ((object_def.instance != MIB_OBJECT_NONE) &&
tax 0:66300c77c6e9 284 (object_def.access & MIB_ACCESS_READ))
tax 0:66300c77c6e9 285 {
tax 0:66300c77c6e9 286 mn = mn;
tax 0:66300c77c6e9 287 }
tax 0:66300c77c6e9 288 else
tax 0:66300c77c6e9 289 {
tax 0:66300c77c6e9 290 /* search failed, object id points to unknown object (nosuchname) */
tax 0:66300c77c6e9 291 mn = NULL;
tax 0:66300c77c6e9 292 }
tax 0:66300c77c6e9 293 if (mn != NULL)
tax 0:66300c77c6e9 294 {
tax 0:66300c77c6e9 295 struct snmp_varbind *vb;
tax 0:66300c77c6e9 296
tax 0:66300c77c6e9 297 msg_ps->state = SNMP_MSG_INTERNAL_GET_VALUE;
tax 0:66300c77c6e9 298 /* allocate output varbind */
tax 0:66300c77c6e9 299 vb = (struct snmp_varbind *)memp_malloc(MEMP_SNMP_VARBIND);
tax 0:66300c77c6e9 300 LWIP_ASSERT("vb != NULL",vb != NULL);
tax 0:66300c77c6e9 301 if (vb != NULL)
tax 0:66300c77c6e9 302 {
tax 0:66300c77c6e9 303 vb->next = NULL;
tax 0:66300c77c6e9 304 vb->prev = NULL;
tax 0:66300c77c6e9 305
tax 0:66300c77c6e9 306 /* move name from invb to outvb */
tax 0:66300c77c6e9 307 vb->ident = msg_ps->vb_ptr->ident;
tax 0:66300c77c6e9 308 vb->ident_len = msg_ps->vb_ptr->ident_len;
tax 0:66300c77c6e9 309 /* ensure this memory is refereced once only */
tax 0:66300c77c6e9 310 msg_ps->vb_ptr->ident = NULL;
tax 0:66300c77c6e9 311 msg_ps->vb_ptr->ident_len = 0;
tax 0:66300c77c6e9 312
tax 0:66300c77c6e9 313 vb->value_type = object_def.asn_type;
tax 0:66300c77c6e9 314 LWIP_ASSERT("invalid length", object_def.v_len <= 0xff);
tax 0:66300c77c6e9 315 vb->value_len = (u8_t)object_def.v_len;
tax 0:66300c77c6e9 316 if (vb->value_len > 0)
tax 0:66300c77c6e9 317 {
tax 0:66300c77c6e9 318 LWIP_ASSERT("SNMP_MAX_OCTET_STRING_LEN is configured too low",
tax 0:66300c77c6e9 319 vb->value_len <= SNMP_MAX_VALUE_SIZE);
tax 0:66300c77c6e9 320 vb->value = memp_malloc(MEMP_SNMP_VALUE);
tax 0:66300c77c6e9 321 LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
tax 0:66300c77c6e9 322 if (vb->value != NULL)
tax 0:66300c77c6e9 323 {
tax 0:66300c77c6e9 324 mn->get_value(&object_def, vb->value_len, vb->value);
tax 0:66300c77c6e9 325 snmp_varbind_tail_add(&msg_ps->outvb, vb);
tax 0:66300c77c6e9 326 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 327 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 328 }
tax 0:66300c77c6e9 329 else
tax 0:66300c77c6e9 330 {
tax 0:66300c77c6e9 331 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: couldn't allocate variable space\n"));
tax 0:66300c77c6e9 332 msg_ps->vb_ptr->ident = vb->ident;
tax 0:66300c77c6e9 333 msg_ps->vb_ptr->ident_len = vb->ident_len;
tax 0:66300c77c6e9 334 memp_free(MEMP_SNMP_VARBIND, vb);
tax 0:66300c77c6e9 335 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
tax 0:66300c77c6e9 336 }
tax 0:66300c77c6e9 337 }
tax 0:66300c77c6e9 338 else
tax 0:66300c77c6e9 339 {
tax 0:66300c77c6e9 340 /* vb->value_len == 0, empty value (e.g. empty string) */
tax 0:66300c77c6e9 341 vb->value = NULL;
tax 0:66300c77c6e9 342 snmp_varbind_tail_add(&msg_ps->outvb, vb);
tax 0:66300c77c6e9 343 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 344 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 345 }
tax 0:66300c77c6e9 346 }
tax 0:66300c77c6e9 347 else
tax 0:66300c77c6e9 348 {
tax 0:66300c77c6e9 349 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: couldn't allocate outvb space\n"));
tax 0:66300c77c6e9 350 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
tax 0:66300c77c6e9 351 }
tax 0:66300c77c6e9 352 }
tax 0:66300c77c6e9 353 }
tax 0:66300c77c6e9 354 }
tax 0:66300c77c6e9 355 }
tax 0:66300c77c6e9 356 else
tax 0:66300c77c6e9 357 {
tax 0:66300c77c6e9 358 mn = NULL;
tax 0:66300c77c6e9 359 }
tax 0:66300c77c6e9 360 if (mn == NULL)
tax 0:66300c77c6e9 361 {
tax 0:66300c77c6e9 362 /* mn == NULL, noSuchName */
tax 0:66300c77c6e9 363 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 364 }
tax 0:66300c77c6e9 365 }
tax 0:66300c77c6e9 366 if ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
tax 0:66300c77c6e9 367 (msg_ps->vb_idx == msg_ps->invb.count))
tax 0:66300c77c6e9 368 {
tax 0:66300c77c6e9 369 snmp_ok_response(msg_ps);
tax 0:66300c77c6e9 370 }
tax 0:66300c77c6e9 371 }
tax 0:66300c77c6e9 372
tax 0:66300c77c6e9 373 /**
tax 0:66300c77c6e9 374 * Service an internal or external event for SNMP GETNEXT.
tax 0:66300c77c6e9 375 *
tax 0:66300c77c6e9 376 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
tax 0:66300c77c6e9 377 * @param msg_ps points to the assosicated message process state
tax 0:66300c77c6e9 378 */
tax 0:66300c77c6e9 379 static void
tax 0:66300c77c6e9 380 snmp_msg_getnext_event(u8_t request_id, struct snmp_msg_pstat *msg_ps)
tax 0:66300c77c6e9 381 {
tax 0:66300c77c6e9 382 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_getnext_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state));
tax 0:66300c77c6e9 383
tax 0:66300c77c6e9 384 if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF)
tax 0:66300c77c6e9 385 {
tax 0:66300c77c6e9 386 struct mib_external_node *en;
tax 0:66300c77c6e9 387
tax 0:66300c77c6e9 388 /* get_object_def() answer*/
tax 0:66300c77c6e9 389 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 390
tax 0:66300c77c6e9 391 /* translate answer into a known lifeform */
tax 0:66300c77c6e9 392 en->get_object_def_a(request_id, 1, &msg_ps->ext_oid.id[msg_ps->ext_oid.len - 1], &msg_ps->ext_object_def);
tax 0:66300c77c6e9 393 if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE)
tax 0:66300c77c6e9 394 {
tax 0:66300c77c6e9 395 msg_ps->state = SNMP_MSG_EXTERNAL_GET_VALUE;
tax 0:66300c77c6e9 396 en->get_value_q(request_id, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 397 }
tax 0:66300c77c6e9 398 else
tax 0:66300c77c6e9 399 {
tax 0:66300c77c6e9 400 en->get_object_def_pc(request_id, 1, &msg_ps->ext_oid.id[msg_ps->ext_oid.len - 1]);
tax 0:66300c77c6e9 401 /* search failed, object id points to unknown object (nosuchname) */
tax 0:66300c77c6e9 402 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 403 }
tax 0:66300c77c6e9 404 }
tax 0:66300c77c6e9 405 else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_VALUE)
tax 0:66300c77c6e9 406 {
tax 0:66300c77c6e9 407 struct mib_external_node *en;
tax 0:66300c77c6e9 408 struct snmp_varbind *vb;
tax 0:66300c77c6e9 409
tax 0:66300c77c6e9 410 /* get_value() answer */
tax 0:66300c77c6e9 411 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 412
tax 0:66300c77c6e9 413 LWIP_ASSERT("invalid length", msg_ps->ext_object_def.v_len <= 0xff);
tax 0:66300c77c6e9 414 vb = snmp_varbind_alloc(&msg_ps->ext_oid,
tax 0:66300c77c6e9 415 msg_ps->ext_object_def.asn_type,
tax 0:66300c77c6e9 416 (u8_t)msg_ps->ext_object_def.v_len);
tax 0:66300c77c6e9 417 if (vb != NULL)
tax 0:66300c77c6e9 418 {
tax 0:66300c77c6e9 419 en->get_value_a(request_id, &msg_ps->ext_object_def, vb->value_len, vb->value);
tax 0:66300c77c6e9 420 snmp_varbind_tail_add(&msg_ps->outvb, vb);
tax 0:66300c77c6e9 421 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 422 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 423 }
tax 0:66300c77c6e9 424 else
tax 0:66300c77c6e9 425 {
tax 0:66300c77c6e9 426 en->get_value_pc(request_id, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 427 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_getnext_event: couldn't allocate outvb space\n"));
tax 0:66300c77c6e9 428 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
tax 0:66300c77c6e9 429 }
tax 0:66300c77c6e9 430 }
tax 0:66300c77c6e9 431
tax 0:66300c77c6e9 432 while ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
tax 0:66300c77c6e9 433 (msg_ps->vb_idx < msg_ps->invb.count))
tax 0:66300c77c6e9 434 {
tax 0:66300c77c6e9 435 struct mib_node *mn;
tax 0:66300c77c6e9 436 struct snmp_obj_id oid;
tax 0:66300c77c6e9 437
tax 0:66300c77c6e9 438 if (msg_ps->vb_idx == 0)
tax 0:66300c77c6e9 439 {
tax 0:66300c77c6e9 440 msg_ps->vb_ptr = msg_ps->invb.head;
tax 0:66300c77c6e9 441 }
tax 0:66300c77c6e9 442 else
tax 0:66300c77c6e9 443 {
tax 0:66300c77c6e9 444 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
tax 0:66300c77c6e9 445 }
tax 0:66300c77c6e9 446 if (snmp_iso_prefix_expand(msg_ps->vb_ptr->ident_len, msg_ps->vb_ptr->ident, &oid))
tax 0:66300c77c6e9 447 {
tax 0:66300c77c6e9 448 if (msg_ps->vb_ptr->ident_len > 3)
tax 0:66300c77c6e9 449 {
tax 0:66300c77c6e9 450 /* can offset ident_len and ident */
tax 0:66300c77c6e9 451 mn = snmp_expand_tree((struct mib_node*)&internet,
tax 0:66300c77c6e9 452 msg_ps->vb_ptr->ident_len - 4,
tax 0:66300c77c6e9 453 msg_ps->vb_ptr->ident + 4, &oid);
tax 0:66300c77c6e9 454 }
tax 0:66300c77c6e9 455 else
tax 0:66300c77c6e9 456 {
tax 0:66300c77c6e9 457 /* can't offset ident_len -4, ident + 4 */
tax 0:66300c77c6e9 458 mn = snmp_expand_tree((struct mib_node*)&internet, 0, NULL, &oid);
tax 0:66300c77c6e9 459 }
tax 0:66300c77c6e9 460 }
tax 0:66300c77c6e9 461 else
tax 0:66300c77c6e9 462 {
tax 0:66300c77c6e9 463 mn = NULL;
tax 0:66300c77c6e9 464 }
tax 0:66300c77c6e9 465 if (mn != NULL)
tax 0:66300c77c6e9 466 {
tax 0:66300c77c6e9 467 if (mn->node_type == MIB_NODE_EX)
tax 0:66300c77c6e9 468 {
tax 0:66300c77c6e9 469 /* external object */
tax 0:66300c77c6e9 470 struct mib_external_node *en = (struct mib_external_node*)mn;
tax 0:66300c77c6e9 471
tax 0:66300c77c6e9 472 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF;
tax 0:66300c77c6e9 473 /* save en && args in msg_ps!! */
tax 0:66300c77c6e9 474 msg_ps->ext_mib_node = en;
tax 0:66300c77c6e9 475 msg_ps->ext_oid = oid;
tax 0:66300c77c6e9 476
tax 0:66300c77c6e9 477 en->get_object_def_q(en->addr_inf, request_id, 1, &oid.id[oid.len - 1]);
tax 0:66300c77c6e9 478 }
tax 0:66300c77c6e9 479 else
tax 0:66300c77c6e9 480 {
tax 0:66300c77c6e9 481 /* internal object */
tax 0:66300c77c6e9 482 struct obj_def object_def;
tax 0:66300c77c6e9 483 struct snmp_varbind *vb;
tax 0:66300c77c6e9 484
tax 0:66300c77c6e9 485 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF;
tax 0:66300c77c6e9 486 mn->get_object_def(1, &oid.id[oid.len - 1], &object_def);
tax 0:66300c77c6e9 487
tax 0:66300c77c6e9 488 LWIP_ASSERT("invalid length", object_def.v_len <= 0xff);
tax 0:66300c77c6e9 489 vb = snmp_varbind_alloc(&oid, object_def.asn_type, (u8_t)object_def.v_len);
tax 0:66300c77c6e9 490 if (vb != NULL)
tax 0:66300c77c6e9 491 {
tax 0:66300c77c6e9 492 msg_ps->state = SNMP_MSG_INTERNAL_GET_VALUE;
tax 0:66300c77c6e9 493 mn->get_value(&object_def, object_def.v_len, vb->value);
tax 0:66300c77c6e9 494 snmp_varbind_tail_add(&msg_ps->outvb, vb);
tax 0:66300c77c6e9 495 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 496 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 497 }
tax 0:66300c77c6e9 498 else
tax 0:66300c77c6e9 499 {
tax 0:66300c77c6e9 500 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate outvb space\n"));
tax 0:66300c77c6e9 501 snmp_error_response(msg_ps,SNMP_ES_TOOBIG);
tax 0:66300c77c6e9 502 }
tax 0:66300c77c6e9 503 }
tax 0:66300c77c6e9 504 }
tax 0:66300c77c6e9 505 if (mn == NULL)
tax 0:66300c77c6e9 506 {
tax 0:66300c77c6e9 507 /* mn == NULL, noSuchName */
tax 0:66300c77c6e9 508 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 509 }
tax 0:66300c77c6e9 510 }
tax 0:66300c77c6e9 511 if ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
tax 0:66300c77c6e9 512 (msg_ps->vb_idx == msg_ps->invb.count))
tax 0:66300c77c6e9 513 {
tax 0:66300c77c6e9 514 snmp_ok_response(msg_ps);
tax 0:66300c77c6e9 515 }
tax 0:66300c77c6e9 516 }
tax 0:66300c77c6e9 517
tax 0:66300c77c6e9 518 /**
tax 0:66300c77c6e9 519 * Service an internal or external event for SNMP SET.
tax 0:66300c77c6e9 520 *
tax 0:66300c77c6e9 521 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
tax 0:66300c77c6e9 522 * @param msg_ps points to the assosicated message process state
tax 0:66300c77c6e9 523 */
tax 0:66300c77c6e9 524 static void
tax 0:66300c77c6e9 525 snmp_msg_set_event(u8_t request_id, struct snmp_msg_pstat *msg_ps)
tax 0:66300c77c6e9 526 {
tax 0:66300c77c6e9 527 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_set_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state));
tax 0:66300c77c6e9 528
tax 0:66300c77c6e9 529 if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF)
tax 0:66300c77c6e9 530 {
tax 0:66300c77c6e9 531 struct mib_external_node *en;
tax 0:66300c77c6e9 532 struct snmp_name_ptr np;
tax 0:66300c77c6e9 533
tax 0:66300c77c6e9 534 /* get_object_def() answer*/
tax 0:66300c77c6e9 535 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 536 np = msg_ps->ext_name_ptr;
tax 0:66300c77c6e9 537
tax 0:66300c77c6e9 538 /* translate answer into a known lifeform */
tax 0:66300c77c6e9 539 en->get_object_def_a(request_id, np.ident_len, np.ident, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 540 if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE)
tax 0:66300c77c6e9 541 {
tax 0:66300c77c6e9 542 msg_ps->state = SNMP_MSG_EXTERNAL_SET_TEST;
tax 0:66300c77c6e9 543 en->set_test_q(request_id, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 544 }
tax 0:66300c77c6e9 545 else
tax 0:66300c77c6e9 546 {
tax 0:66300c77c6e9 547 en->get_object_def_pc(request_id, np.ident_len, np.ident);
tax 0:66300c77c6e9 548 /* search failed, object id points to unknown object (nosuchname) */
tax 0:66300c77c6e9 549 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 550 }
tax 0:66300c77c6e9 551 }
tax 0:66300c77c6e9 552 else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_TEST)
tax 0:66300c77c6e9 553 {
tax 0:66300c77c6e9 554 struct mib_external_node *en;
tax 0:66300c77c6e9 555
tax 0:66300c77c6e9 556 /* set_test() answer*/
tax 0:66300c77c6e9 557 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 558
tax 0:66300c77c6e9 559 if (msg_ps->ext_object_def.access & MIB_ACCESS_WRITE)
tax 0:66300c77c6e9 560 {
tax 0:66300c77c6e9 561 if ((msg_ps->ext_object_def.asn_type == msg_ps->vb_ptr->value_type) &&
tax 0:66300c77c6e9 562 (en->set_test_a(request_id,&msg_ps->ext_object_def,
tax 0:66300c77c6e9 563 msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value) != 0))
tax 0:66300c77c6e9 564 {
tax 0:66300c77c6e9 565 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 566 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 567 }
tax 0:66300c77c6e9 568 else
tax 0:66300c77c6e9 569 {
tax 0:66300c77c6e9 570 en->set_test_pc(request_id,&msg_ps->ext_object_def);
tax 0:66300c77c6e9 571 /* bad value */
tax 0:66300c77c6e9 572 snmp_error_response(msg_ps,SNMP_ES_BADVALUE);
tax 0:66300c77c6e9 573 }
tax 0:66300c77c6e9 574 }
tax 0:66300c77c6e9 575 else
tax 0:66300c77c6e9 576 {
tax 0:66300c77c6e9 577 en->set_test_pc(request_id,&msg_ps->ext_object_def);
tax 0:66300c77c6e9 578 /* object not available for set */
tax 0:66300c77c6e9 579 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 580 }
tax 0:66300c77c6e9 581 }
tax 0:66300c77c6e9 582 else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF_S)
tax 0:66300c77c6e9 583 {
tax 0:66300c77c6e9 584 struct mib_external_node *en;
tax 0:66300c77c6e9 585 struct snmp_name_ptr np;
tax 0:66300c77c6e9 586
tax 0:66300c77c6e9 587 /* get_object_def() answer*/
tax 0:66300c77c6e9 588 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 589 np = msg_ps->ext_name_ptr;
tax 0:66300c77c6e9 590
tax 0:66300c77c6e9 591 /* translate answer into a known lifeform */
tax 0:66300c77c6e9 592 en->get_object_def_a(request_id, np.ident_len, np.ident, &msg_ps->ext_object_def);
tax 0:66300c77c6e9 593 if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE)
tax 0:66300c77c6e9 594 {
tax 0:66300c77c6e9 595 msg_ps->state = SNMP_MSG_EXTERNAL_SET_VALUE;
tax 0:66300c77c6e9 596 en->set_value_q(request_id, &msg_ps->ext_object_def,
tax 0:66300c77c6e9 597 msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value);
tax 0:66300c77c6e9 598 }
tax 0:66300c77c6e9 599 else
tax 0:66300c77c6e9 600 {
tax 0:66300c77c6e9 601 en->get_object_def_pc(request_id, np.ident_len, np.ident);
tax 0:66300c77c6e9 602 /* set_value failed, object has disappeared for some odd reason?? */
tax 0:66300c77c6e9 603 snmp_error_response(msg_ps,SNMP_ES_GENERROR);
tax 0:66300c77c6e9 604 }
tax 0:66300c77c6e9 605 }
tax 0:66300c77c6e9 606 else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_VALUE)
tax 0:66300c77c6e9 607 {
tax 0:66300c77c6e9 608 struct mib_external_node *en;
tax 0:66300c77c6e9 609
tax 0:66300c77c6e9 610 /** set_value_a() */
tax 0:66300c77c6e9 611 en = msg_ps->ext_mib_node;
tax 0:66300c77c6e9 612 en->set_value_a(request_id, &msg_ps->ext_object_def,
tax 0:66300c77c6e9 613 msg_ps->vb_ptr->value_len, msg_ps->vb_ptr->value);
tax 0:66300c77c6e9 614
tax 0:66300c77c6e9 615 /** @todo use set_value_pc() if toobig */
tax 0:66300c77c6e9 616 msg_ps->state = SNMP_MSG_INTERNAL_SET_VALUE;
tax 0:66300c77c6e9 617 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 618 }
tax 0:66300c77c6e9 619
tax 0:66300c77c6e9 620 /* test all values before setting */
tax 0:66300c77c6e9 621 while ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
tax 0:66300c77c6e9 622 (msg_ps->vb_idx < msg_ps->invb.count))
tax 0:66300c77c6e9 623 {
tax 0:66300c77c6e9 624 struct mib_node *mn;
tax 0:66300c77c6e9 625 struct snmp_name_ptr np;
tax 0:66300c77c6e9 626
tax 0:66300c77c6e9 627 if (msg_ps->vb_idx == 0)
tax 0:66300c77c6e9 628 {
tax 0:66300c77c6e9 629 msg_ps->vb_ptr = msg_ps->invb.head;
tax 0:66300c77c6e9 630 }
tax 0:66300c77c6e9 631 else
tax 0:66300c77c6e9 632 {
tax 0:66300c77c6e9 633 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
tax 0:66300c77c6e9 634 }
tax 0:66300c77c6e9 635 /** test object identifier for .iso.org.dod.internet prefix */
tax 0:66300c77c6e9 636 if (snmp_iso_prefix_tst(msg_ps->vb_ptr->ident_len, msg_ps->vb_ptr->ident))
tax 0:66300c77c6e9 637 {
tax 0:66300c77c6e9 638 mn = snmp_search_tree((struct mib_node*)&internet, msg_ps->vb_ptr->ident_len - 4,
tax 0:66300c77c6e9 639 msg_ps->vb_ptr->ident + 4, &np);
tax 0:66300c77c6e9 640 if (mn != NULL)
tax 0:66300c77c6e9 641 {
tax 0:66300c77c6e9 642 if (mn->node_type == MIB_NODE_EX)
tax 0:66300c77c6e9 643 {
tax 0:66300c77c6e9 644 /* external object */
tax 0:66300c77c6e9 645 struct mib_external_node *en = (struct mib_external_node*)mn;
tax 0:66300c77c6e9 646
tax 0:66300c77c6e9 647 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF;
tax 0:66300c77c6e9 648 /* save en && args in msg_ps!! */
tax 0:66300c77c6e9 649 msg_ps->ext_mib_node = en;
tax 0:66300c77c6e9 650 msg_ps->ext_name_ptr = np;
tax 0:66300c77c6e9 651
tax 0:66300c77c6e9 652 en->get_object_def_q(en->addr_inf, request_id, np.ident_len, np.ident);
tax 0:66300c77c6e9 653 }
tax 0:66300c77c6e9 654 else
tax 0:66300c77c6e9 655 {
tax 0:66300c77c6e9 656 /* internal object */
tax 0:66300c77c6e9 657 struct obj_def object_def;
tax 0:66300c77c6e9 658
tax 0:66300c77c6e9 659 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF;
tax 0:66300c77c6e9 660 mn->get_object_def(np.ident_len, np.ident, &object_def);
tax 0:66300c77c6e9 661 if (object_def.instance != MIB_OBJECT_NONE)
tax 0:66300c77c6e9 662 {
tax 0:66300c77c6e9 663 mn = mn;
tax 0:66300c77c6e9 664 }
tax 0:66300c77c6e9 665 else
tax 0:66300c77c6e9 666 {
tax 0:66300c77c6e9 667 /* search failed, object id points to unknown object (nosuchname) */
tax 0:66300c77c6e9 668 mn = NULL;
tax 0:66300c77c6e9 669 }
tax 0:66300c77c6e9 670 if (mn != NULL)
tax 0:66300c77c6e9 671 {
tax 0:66300c77c6e9 672 msg_ps->state = SNMP_MSG_INTERNAL_SET_TEST;
tax 0:66300c77c6e9 673
tax 0:66300c77c6e9 674 if (object_def.access & MIB_ACCESS_WRITE)
tax 0:66300c77c6e9 675 {
tax 0:66300c77c6e9 676 if ((object_def.asn_type == msg_ps->vb_ptr->value_type) &&
tax 0:66300c77c6e9 677 (mn->set_test(&object_def,msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value) != 0))
tax 0:66300c77c6e9 678 {
tax 0:66300c77c6e9 679 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 680 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 681 }
tax 0:66300c77c6e9 682 else
tax 0:66300c77c6e9 683 {
tax 0:66300c77c6e9 684 /* bad value */
tax 0:66300c77c6e9 685 snmp_error_response(msg_ps,SNMP_ES_BADVALUE);
tax 0:66300c77c6e9 686 }
tax 0:66300c77c6e9 687 }
tax 0:66300c77c6e9 688 else
tax 0:66300c77c6e9 689 {
tax 0:66300c77c6e9 690 /* object not available for set */
tax 0:66300c77c6e9 691 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 692 }
tax 0:66300c77c6e9 693 }
tax 0:66300c77c6e9 694 }
tax 0:66300c77c6e9 695 }
tax 0:66300c77c6e9 696 }
tax 0:66300c77c6e9 697 else
tax 0:66300c77c6e9 698 {
tax 0:66300c77c6e9 699 mn = NULL;
tax 0:66300c77c6e9 700 }
tax 0:66300c77c6e9 701 if (mn == NULL)
tax 0:66300c77c6e9 702 {
tax 0:66300c77c6e9 703 /* mn == NULL, noSuchName */
tax 0:66300c77c6e9 704 snmp_error_response(msg_ps,SNMP_ES_NOSUCHNAME);
tax 0:66300c77c6e9 705 }
tax 0:66300c77c6e9 706 }
tax 0:66300c77c6e9 707
tax 0:66300c77c6e9 708 if ((msg_ps->state == SNMP_MSG_SEARCH_OBJ) &&
tax 0:66300c77c6e9 709 (msg_ps->vb_idx == msg_ps->invb.count))
tax 0:66300c77c6e9 710 {
tax 0:66300c77c6e9 711 msg_ps->vb_idx = 0;
tax 0:66300c77c6e9 712 msg_ps->state = SNMP_MSG_INTERNAL_SET_VALUE;
tax 0:66300c77c6e9 713 }
tax 0:66300c77c6e9 714
tax 0:66300c77c6e9 715 /* set all values "atomically" (be as "atomic" as possible) */
tax 0:66300c77c6e9 716 while ((msg_ps->state == SNMP_MSG_INTERNAL_SET_VALUE) &&
tax 0:66300c77c6e9 717 (msg_ps->vb_idx < msg_ps->invb.count))
tax 0:66300c77c6e9 718 {
tax 0:66300c77c6e9 719 struct mib_node *mn;
tax 0:66300c77c6e9 720 struct snmp_name_ptr np;
tax 0:66300c77c6e9 721
tax 0:66300c77c6e9 722 if (msg_ps->vb_idx == 0)
tax 0:66300c77c6e9 723 {
tax 0:66300c77c6e9 724 msg_ps->vb_ptr = msg_ps->invb.head;
tax 0:66300c77c6e9 725 }
tax 0:66300c77c6e9 726 else
tax 0:66300c77c6e9 727 {
tax 0:66300c77c6e9 728 msg_ps->vb_ptr = msg_ps->vb_ptr->next;
tax 0:66300c77c6e9 729 }
tax 0:66300c77c6e9 730 /* skip iso prefix test, was done previously while settesting() */
tax 0:66300c77c6e9 731 mn = snmp_search_tree((struct mib_node*)&internet, msg_ps->vb_ptr->ident_len - 4,
tax 0:66300c77c6e9 732 msg_ps->vb_ptr->ident + 4, &np);
tax 0:66300c77c6e9 733 /* check if object is still available
tax 0:66300c77c6e9 734 (e.g. external hot-plug thingy present?) */
tax 0:66300c77c6e9 735 if (mn != NULL)
tax 0:66300c77c6e9 736 {
tax 0:66300c77c6e9 737 if (mn->node_type == MIB_NODE_EX)
tax 0:66300c77c6e9 738 {
tax 0:66300c77c6e9 739 /* external object */
tax 0:66300c77c6e9 740 struct mib_external_node *en = (struct mib_external_node*)mn;
tax 0:66300c77c6e9 741
tax 0:66300c77c6e9 742 msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF_S;
tax 0:66300c77c6e9 743 /* save en && args in msg_ps!! */
tax 0:66300c77c6e9 744 msg_ps->ext_mib_node = en;
tax 0:66300c77c6e9 745 msg_ps->ext_name_ptr = np;
tax 0:66300c77c6e9 746
tax 0:66300c77c6e9 747 en->get_object_def_q(en->addr_inf, request_id, np.ident_len, np.ident);
tax 0:66300c77c6e9 748 }
tax 0:66300c77c6e9 749 else
tax 0:66300c77c6e9 750 {
tax 0:66300c77c6e9 751 /* internal object */
tax 0:66300c77c6e9 752 struct obj_def object_def;
tax 0:66300c77c6e9 753
tax 0:66300c77c6e9 754 msg_ps->state = SNMP_MSG_INTERNAL_GET_OBJDEF_S;
tax 0:66300c77c6e9 755 mn->get_object_def(np.ident_len, np.ident, &object_def);
tax 0:66300c77c6e9 756 msg_ps->state = SNMP_MSG_INTERNAL_SET_VALUE;
tax 0:66300c77c6e9 757 mn->set_value(&object_def,msg_ps->vb_ptr->value_len,msg_ps->vb_ptr->value);
tax 0:66300c77c6e9 758 msg_ps->vb_idx += 1;
tax 0:66300c77c6e9 759 }
tax 0:66300c77c6e9 760 }
tax 0:66300c77c6e9 761 }
tax 0:66300c77c6e9 762 if ((msg_ps->state == SNMP_MSG_INTERNAL_SET_VALUE) &&
tax 0:66300c77c6e9 763 (msg_ps->vb_idx == msg_ps->invb.count))
tax 0:66300c77c6e9 764 {
tax 0:66300c77c6e9 765 /* simply echo the input if we can set it
tax 0:66300c77c6e9 766 @todo do we need to return the actual value?
tax 0:66300c77c6e9 767 e.g. if value is silently modified or behaves sticky? */
tax 0:66300c77c6e9 768 msg_ps->outvb = msg_ps->invb;
tax 0:66300c77c6e9 769 msg_ps->invb.head = NULL;
tax 0:66300c77c6e9 770 msg_ps->invb.tail = NULL;
tax 0:66300c77c6e9 771 msg_ps->invb.count = 0;
tax 0:66300c77c6e9 772 snmp_ok_response(msg_ps);
tax 0:66300c77c6e9 773 }
tax 0:66300c77c6e9 774 }
tax 0:66300c77c6e9 775
tax 0:66300c77c6e9 776
tax 0:66300c77c6e9 777 /**
tax 0:66300c77c6e9 778 * Handle one internal or external event.
tax 0:66300c77c6e9 779 * Called for one async event. (recv external/private answer)
tax 0:66300c77c6e9 780 *
tax 0:66300c77c6e9 781 * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
tax 0:66300c77c6e9 782 */
tax 0:66300c77c6e9 783 void
tax 0:66300c77c6e9 784 snmp_msg_event(u8_t request_id)
tax 0:66300c77c6e9 785 {
tax 0:66300c77c6e9 786 struct snmp_msg_pstat *msg_ps;
tax 0:66300c77c6e9 787
tax 0:66300c77c6e9 788 if (request_id < SNMP_CONCURRENT_REQUESTS)
tax 0:66300c77c6e9 789 {
tax 0:66300c77c6e9 790 msg_ps = &msg_input_list[request_id];
tax 0:66300c77c6e9 791 if (msg_ps->rt == SNMP_ASN1_PDU_GET_NEXT_REQ)
tax 0:66300c77c6e9 792 {
tax 0:66300c77c6e9 793 snmp_msg_getnext_event(request_id, msg_ps);
tax 0:66300c77c6e9 794 }
tax 0:66300c77c6e9 795 else if (msg_ps->rt == SNMP_ASN1_PDU_GET_REQ)
tax 0:66300c77c6e9 796 {
tax 0:66300c77c6e9 797 snmp_msg_get_event(request_id, msg_ps);
tax 0:66300c77c6e9 798 }
tax 0:66300c77c6e9 799 else if(msg_ps->rt == SNMP_ASN1_PDU_SET_REQ)
tax 0:66300c77c6e9 800 {
tax 0:66300c77c6e9 801 snmp_msg_set_event(request_id, msg_ps);
tax 0:66300c77c6e9 802 }
tax 0:66300c77c6e9 803 }
tax 0:66300c77c6e9 804 }
tax 0:66300c77c6e9 805
tax 0:66300c77c6e9 806
tax 0:66300c77c6e9 807 /* lwIP UDP receive callback function */
tax 0:66300c77c6e9 808 static void
tax 0:66300c77c6e9 809 snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
tax 0:66300c77c6e9 810 {
tax 0:66300c77c6e9 811 struct snmp_msg_pstat *msg_ps;
tax 0:66300c77c6e9 812 u8_t req_idx;
tax 0:66300c77c6e9 813 err_t err_ret;
tax 0:66300c77c6e9 814 u16_t payload_len = p->tot_len;
tax 0:66300c77c6e9 815 u16_t payload_ofs = 0;
tax 0:66300c77c6e9 816 u16_t varbind_ofs = 0;
tax 0:66300c77c6e9 817
tax 0:66300c77c6e9 818 /* suppress unused argument warning */
tax 0:66300c77c6e9 819 LWIP_UNUSED_ARG(arg);
tax 0:66300c77c6e9 820
tax 0:66300c77c6e9 821 /* traverse input message process list, look for SNMP_MSG_EMPTY */
tax 0:66300c77c6e9 822 msg_ps = &msg_input_list[0];
tax 0:66300c77c6e9 823 req_idx = 0;
tax 0:66300c77c6e9 824 while ((req_idx < SNMP_CONCURRENT_REQUESTS) && (msg_ps->state != SNMP_MSG_EMPTY))
tax 0:66300c77c6e9 825 {
tax 0:66300c77c6e9 826 req_idx++;
tax 0:66300c77c6e9 827 msg_ps++;
tax 0:66300c77c6e9 828 }
tax 0:66300c77c6e9 829 if (req_idx == SNMP_CONCURRENT_REQUESTS)
tax 0:66300c77c6e9 830 {
tax 0:66300c77c6e9 831 /* exceeding number of concurrent requests */
tax 0:66300c77c6e9 832 pbuf_free(p);
tax 0:66300c77c6e9 833 return;
tax 0:66300c77c6e9 834 }
tax 0:66300c77c6e9 835
tax 0:66300c77c6e9 836 /* accepting request */
tax 0:66300c77c6e9 837 snmp_inc_snmpinpkts();
tax 0:66300c77c6e9 838 /* record used 'protocol control block' */
tax 0:66300c77c6e9 839 msg_ps->pcb = pcb;
tax 0:66300c77c6e9 840 /* source address (network order) */
tax 0:66300c77c6e9 841 msg_ps->sip = *addr;
tax 0:66300c77c6e9 842 /* source port (host order (lwIP oddity)) */
tax 0:66300c77c6e9 843 msg_ps->sp = port;
tax 0:66300c77c6e9 844
tax 0:66300c77c6e9 845 /* check total length, version, community, pdu type */
tax 0:66300c77c6e9 846 err_ret = snmp_pdu_header_check(p, payload_ofs, payload_len, &varbind_ofs, msg_ps);
tax 0:66300c77c6e9 847 /* Only accept requests and requests without error (be robust) */
tax 0:66300c77c6e9 848 /* Reject response and trap headers or error requests as input! */
tax 0:66300c77c6e9 849 if ((err_ret != ERR_OK) ||
tax 0:66300c77c6e9 850 ((msg_ps->rt != SNMP_ASN1_PDU_GET_REQ) &&
tax 0:66300c77c6e9 851 (msg_ps->rt != SNMP_ASN1_PDU_GET_NEXT_REQ) &&
tax 0:66300c77c6e9 852 (msg_ps->rt != SNMP_ASN1_PDU_SET_REQ)) ||
tax 0:66300c77c6e9 853 ((msg_ps->error_status != SNMP_ES_NOERROR) ||
tax 0:66300c77c6e9 854 (msg_ps->error_index != 0)) )
tax 0:66300c77c6e9 855 {
tax 0:66300c77c6e9 856 /* header check failed drop request silently, do not return error! */
tax 0:66300c77c6e9 857 pbuf_free(p);
tax 0:66300c77c6e9 858 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_header_check() failed\n"));
tax 0:66300c77c6e9 859 return;
tax 0:66300c77c6e9 860 }
tax 0:66300c77c6e9 861 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv ok, community %s\n", msg_ps->community));
tax 0:66300c77c6e9 862
tax 0:66300c77c6e9 863 /* Builds a list of variable bindings. Copy the varbinds from the pbuf
tax 0:66300c77c6e9 864 chain to glue them when these are divided over two or more pbuf's. */
tax 0:66300c77c6e9 865 err_ret = snmp_pdu_dec_varbindlist(p, varbind_ofs, &varbind_ofs, msg_ps);
tax 0:66300c77c6e9 866 /* we've decoded the incoming message, release input msg now */
tax 0:66300c77c6e9 867 pbuf_free(p);
tax 0:66300c77c6e9 868 if ((err_ret != ERR_OK) || (msg_ps->invb.count == 0))
tax 0:66300c77c6e9 869 {
tax 0:66300c77c6e9 870 /* varbind-list decode failed, or varbind list empty.
tax 0:66300c77c6e9 871 drop request silently, do not return error!
tax 0:66300c77c6e9 872 (errors are only returned for a specific varbind failure) */
tax 0:66300c77c6e9 873 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_dec_varbindlist() failed\n"));
tax 0:66300c77c6e9 874 return;
tax 0:66300c77c6e9 875 }
tax 0:66300c77c6e9 876
tax 0:66300c77c6e9 877 msg_ps->error_status = SNMP_ES_NOERROR;
tax 0:66300c77c6e9 878 msg_ps->error_index = 0;
tax 0:66300c77c6e9 879 /* find object for each variable binding */
tax 0:66300c77c6e9 880 msg_ps->state = SNMP_MSG_SEARCH_OBJ;
tax 0:66300c77c6e9 881 /* first variable binding from list to inspect */
tax 0:66300c77c6e9 882 msg_ps->vb_idx = 0;
tax 0:66300c77c6e9 883
tax 0:66300c77c6e9 884 LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv varbind cnt=%"U16_F"\n",(u16_t)msg_ps->invb.count));
tax 0:66300c77c6e9 885
tax 0:66300c77c6e9 886 /* handle input event and as much objects as possible in one go */
tax 0:66300c77c6e9 887 snmp_msg_event(req_idx);
tax 0:66300c77c6e9 888 }
tax 0:66300c77c6e9 889
tax 0:66300c77c6e9 890 /**
tax 0:66300c77c6e9 891 * Checks and decodes incoming SNMP message header, logs header errors.
tax 0:66300c77c6e9 892 *
tax 0:66300c77c6e9 893 * @param p points to pbuf chain of SNMP message (UDP payload)
tax 0:66300c77c6e9 894 * @param ofs points to first octet of SNMP message
tax 0:66300c77c6e9 895 * @param pdu_len the length of the UDP payload
tax 0:66300c77c6e9 896 * @param ofs_ret returns the ofset of the variable bindings
tax 0:66300c77c6e9 897 * @param m_stat points to the current message request state return
tax 0:66300c77c6e9 898 * @return
tax 0:66300c77c6e9 899 * - ERR_OK SNMP header is sane and accepted
tax 0:66300c77c6e9 900 * - ERR_ARG SNMP header is either malformed or rejected
tax 0:66300c77c6e9 901 */
tax 0:66300c77c6e9 902 static err_t
tax 0:66300c77c6e9 903 snmp_pdu_header_check(struct pbuf *p, u16_t ofs, u16_t pdu_len, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat)
tax 0:66300c77c6e9 904 {
tax 0:66300c77c6e9 905 err_t derr;
tax 0:66300c77c6e9 906 u16_t len, ofs_base;
tax 0:66300c77c6e9 907 u8_t len_octets;
tax 0:66300c77c6e9 908 u8_t type;
tax 0:66300c77c6e9 909 s32_t version;
tax 0:66300c77c6e9 910
tax 0:66300c77c6e9 911 ofs_base = ofs;
tax 0:66300c77c6e9 912 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 913 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 914 if ((derr != ERR_OK) ||
tax 0:66300c77c6e9 915 (pdu_len != (1 + len_octets + len)) ||
tax 0:66300c77c6e9 916 (type != (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)))
tax 0:66300c77c6e9 917 {
tax 0:66300c77c6e9 918 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 919 return ERR_ARG;
tax 0:66300c77c6e9 920 }
tax 0:66300c77c6e9 921 ofs += (1 + len_octets);
tax 0:66300c77c6e9 922 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 923 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 924 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
tax 0:66300c77c6e9 925 {
tax 0:66300c77c6e9 926 /* can't decode or no integer (version) */
tax 0:66300c77c6e9 927 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 928 return ERR_ARG;
tax 0:66300c77c6e9 929 }
tax 0:66300c77c6e9 930 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &version);
tax 0:66300c77c6e9 931 if (derr != ERR_OK)
tax 0:66300c77c6e9 932 {
tax 0:66300c77c6e9 933 /* can't decode */
tax 0:66300c77c6e9 934 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 935 return ERR_ARG;
tax 0:66300c77c6e9 936 }
tax 0:66300c77c6e9 937 if (version != 0)
tax 0:66300c77c6e9 938 {
tax 0:66300c77c6e9 939 /* not version 1 */
tax 0:66300c77c6e9 940 snmp_inc_snmpinbadversions();
tax 0:66300c77c6e9 941 return ERR_ARG;
tax 0:66300c77c6e9 942 }
tax 0:66300c77c6e9 943 ofs += (1 + len_octets + len);
tax 0:66300c77c6e9 944 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 945 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 946 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR)))
tax 0:66300c77c6e9 947 {
tax 0:66300c77c6e9 948 /* can't decode or no octet string (community) */
tax 0:66300c77c6e9 949 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 950 return ERR_ARG;
tax 0:66300c77c6e9 951 }
tax 0:66300c77c6e9 952 derr = snmp_asn1_dec_raw(p, ofs + 1 + len_octets, len, SNMP_COMMUNITY_STR_LEN, m_stat->community);
tax 0:66300c77c6e9 953 if (derr != ERR_OK)
tax 0:66300c77c6e9 954 {
tax 0:66300c77c6e9 955 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 956 return ERR_ARG;
tax 0:66300c77c6e9 957 }
tax 0:66300c77c6e9 958 /* add zero terminator */
tax 0:66300c77c6e9 959 len = ((len < (SNMP_COMMUNITY_STR_LEN))?(len):(SNMP_COMMUNITY_STR_LEN));
tax 0:66300c77c6e9 960 m_stat->community[len] = 0;
tax 0:66300c77c6e9 961 m_stat->com_strlen = (u8_t)len;
tax 0:66300c77c6e9 962 if (strncmp(snmp_publiccommunity, (const char*)m_stat->community, SNMP_COMMUNITY_STR_LEN) != 0)
tax 0:66300c77c6e9 963 {
tax 0:66300c77c6e9 964 /** @todo: move this if we need to check more names */
tax 0:66300c77c6e9 965 snmp_inc_snmpinbadcommunitynames();
tax 0:66300c77c6e9 966 snmp_authfail_trap();
tax 0:66300c77c6e9 967 return ERR_ARG;
tax 0:66300c77c6e9 968 }
tax 0:66300c77c6e9 969 ofs += (1 + len_octets + len);
tax 0:66300c77c6e9 970 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 971 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 972 if (derr != ERR_OK)
tax 0:66300c77c6e9 973 {
tax 0:66300c77c6e9 974 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 975 return ERR_ARG;
tax 0:66300c77c6e9 976 }
tax 0:66300c77c6e9 977 switch(type)
tax 0:66300c77c6e9 978 {
tax 0:66300c77c6e9 979 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_REQ):
tax 0:66300c77c6e9 980 /* GetRequest PDU */
tax 0:66300c77c6e9 981 snmp_inc_snmpingetrequests();
tax 0:66300c77c6e9 982 derr = ERR_OK;
tax 0:66300c77c6e9 983 break;
tax 0:66300c77c6e9 984 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_NEXT_REQ):
tax 0:66300c77c6e9 985 /* GetNextRequest PDU */
tax 0:66300c77c6e9 986 snmp_inc_snmpingetnexts();
tax 0:66300c77c6e9 987 derr = ERR_OK;
tax 0:66300c77c6e9 988 break;
tax 0:66300c77c6e9 989 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_RESP):
tax 0:66300c77c6e9 990 /* GetResponse PDU */
tax 0:66300c77c6e9 991 snmp_inc_snmpingetresponses();
tax 0:66300c77c6e9 992 derr = ERR_ARG;
tax 0:66300c77c6e9 993 break;
tax 0:66300c77c6e9 994 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_SET_REQ):
tax 0:66300c77c6e9 995 /* SetRequest PDU */
tax 0:66300c77c6e9 996 snmp_inc_snmpinsetrequests();
tax 0:66300c77c6e9 997 derr = ERR_OK;
tax 0:66300c77c6e9 998 break;
tax 0:66300c77c6e9 999 case (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_TRAP):
tax 0:66300c77c6e9 1000 /* Trap PDU */
tax 0:66300c77c6e9 1001 snmp_inc_snmpintraps();
tax 0:66300c77c6e9 1002 derr = ERR_ARG;
tax 0:66300c77c6e9 1003 break;
tax 0:66300c77c6e9 1004 default:
tax 0:66300c77c6e9 1005 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1006 derr = ERR_ARG;
tax 0:66300c77c6e9 1007 break;
tax 0:66300c77c6e9 1008 }
tax 0:66300c77c6e9 1009 if (derr != ERR_OK)
tax 0:66300c77c6e9 1010 {
tax 0:66300c77c6e9 1011 /* unsupported input PDU for this agent (no parse error) */
tax 0:66300c77c6e9 1012 return ERR_ARG;
tax 0:66300c77c6e9 1013 }
tax 0:66300c77c6e9 1014 m_stat->rt = type & 0x1F;
tax 0:66300c77c6e9 1015 ofs += (1 + len_octets);
tax 0:66300c77c6e9 1016 if (len != (pdu_len - (ofs - ofs_base)))
tax 0:66300c77c6e9 1017 {
tax 0:66300c77c6e9 1018 /* decoded PDU length does not equal actual payload length */
tax 0:66300c77c6e9 1019 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1020 return ERR_ARG;
tax 0:66300c77c6e9 1021 }
tax 0:66300c77c6e9 1022 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 1023 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 1024 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
tax 0:66300c77c6e9 1025 {
tax 0:66300c77c6e9 1026 /* can't decode or no integer (request ID) */
tax 0:66300c77c6e9 1027 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1028 return ERR_ARG;
tax 0:66300c77c6e9 1029 }
tax 0:66300c77c6e9 1030 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &m_stat->rid);
tax 0:66300c77c6e9 1031 if (derr != ERR_OK)
tax 0:66300c77c6e9 1032 {
tax 0:66300c77c6e9 1033 /* can't decode */
tax 0:66300c77c6e9 1034 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1035 return ERR_ARG;
tax 0:66300c77c6e9 1036 }
tax 0:66300c77c6e9 1037 ofs += (1 + len_octets + len);
tax 0:66300c77c6e9 1038 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 1039 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 1040 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
tax 0:66300c77c6e9 1041 {
tax 0:66300c77c6e9 1042 /* can't decode or no integer (error-status) */
tax 0:66300c77c6e9 1043 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1044 return ERR_ARG;
tax 0:66300c77c6e9 1045 }
tax 0:66300c77c6e9 1046 /* must be noError (0) for incoming requests.
tax 0:66300c77c6e9 1047 log errors for mib-2 completeness and for debug purposes */
tax 0:66300c77c6e9 1048 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &m_stat->error_status);
tax 0:66300c77c6e9 1049 if (derr != ERR_OK)
tax 0:66300c77c6e9 1050 {
tax 0:66300c77c6e9 1051 /* can't decode */
tax 0:66300c77c6e9 1052 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1053 return ERR_ARG;
tax 0:66300c77c6e9 1054 }
tax 0:66300c77c6e9 1055 switch (m_stat->error_status)
tax 0:66300c77c6e9 1056 {
tax 0:66300c77c6e9 1057 case SNMP_ES_TOOBIG:
tax 0:66300c77c6e9 1058 snmp_inc_snmpintoobigs();
tax 0:66300c77c6e9 1059 break;
tax 0:66300c77c6e9 1060 case SNMP_ES_NOSUCHNAME:
tax 0:66300c77c6e9 1061 snmp_inc_snmpinnosuchnames();
tax 0:66300c77c6e9 1062 break;
tax 0:66300c77c6e9 1063 case SNMP_ES_BADVALUE:
tax 0:66300c77c6e9 1064 snmp_inc_snmpinbadvalues();
tax 0:66300c77c6e9 1065 break;
tax 0:66300c77c6e9 1066 case SNMP_ES_READONLY:
tax 0:66300c77c6e9 1067 snmp_inc_snmpinreadonlys();
tax 0:66300c77c6e9 1068 break;
tax 0:66300c77c6e9 1069 case SNMP_ES_GENERROR:
tax 0:66300c77c6e9 1070 snmp_inc_snmpingenerrs();
tax 0:66300c77c6e9 1071 break;
tax 0:66300c77c6e9 1072 }
tax 0:66300c77c6e9 1073 ofs += (1 + len_octets + len);
tax 0:66300c77c6e9 1074 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 1075 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 1076 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)))
tax 0:66300c77c6e9 1077 {
tax 0:66300c77c6e9 1078 /* can't decode or no integer (error-index) */
tax 0:66300c77c6e9 1079 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1080 return ERR_ARG;
tax 0:66300c77c6e9 1081 }
tax 0:66300c77c6e9 1082 /* must be 0 for incoming requests.
tax 0:66300c77c6e9 1083 decode anyway to catch bad integers (and dirty tricks) */
tax 0:66300c77c6e9 1084 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, &m_stat->error_index);
tax 0:66300c77c6e9 1085 if (derr != ERR_OK)
tax 0:66300c77c6e9 1086 {
tax 0:66300c77c6e9 1087 /* can't decode */
tax 0:66300c77c6e9 1088 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1089 return ERR_ARG;
tax 0:66300c77c6e9 1090 }
tax 0:66300c77c6e9 1091 ofs += (1 + len_octets + len);
tax 0:66300c77c6e9 1092 *ofs_ret = ofs;
tax 0:66300c77c6e9 1093 return ERR_OK;
tax 0:66300c77c6e9 1094 }
tax 0:66300c77c6e9 1095
tax 0:66300c77c6e9 1096 static err_t
tax 0:66300c77c6e9 1097 snmp_pdu_dec_varbindlist(struct pbuf *p, u16_t ofs, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat)
tax 0:66300c77c6e9 1098 {
tax 0:66300c77c6e9 1099 err_t derr;
tax 0:66300c77c6e9 1100 u16_t len, vb_len;
tax 0:66300c77c6e9 1101 u8_t len_octets;
tax 0:66300c77c6e9 1102 u8_t type;
tax 0:66300c77c6e9 1103
tax 0:66300c77c6e9 1104 /* variable binding list */
tax 0:66300c77c6e9 1105 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 1106 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &vb_len);
tax 0:66300c77c6e9 1107 if ((derr != ERR_OK) ||
tax 0:66300c77c6e9 1108 (type != (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)))
tax 0:66300c77c6e9 1109 {
tax 0:66300c77c6e9 1110 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1111 return ERR_ARG;
tax 0:66300c77c6e9 1112 }
tax 0:66300c77c6e9 1113 ofs += (1 + len_octets);
tax 0:66300c77c6e9 1114
tax 0:66300c77c6e9 1115 /* start with empty list */
tax 0:66300c77c6e9 1116 m_stat->invb.count = 0;
tax 0:66300c77c6e9 1117 m_stat->invb.head = NULL;
tax 0:66300c77c6e9 1118 m_stat->invb.tail = NULL;
tax 0:66300c77c6e9 1119
tax 0:66300c77c6e9 1120 while (vb_len > 0)
tax 0:66300c77c6e9 1121 {
tax 0:66300c77c6e9 1122 struct snmp_obj_id oid, oid_value;
tax 0:66300c77c6e9 1123 struct snmp_varbind *vb;
tax 0:66300c77c6e9 1124
tax 0:66300c77c6e9 1125 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 1126 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 1127 if ((derr != ERR_OK) ||
tax 0:66300c77c6e9 1128 (type != (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)) ||
tax 0:66300c77c6e9 1129 (len == 0) || (len > vb_len))
tax 0:66300c77c6e9 1130 {
tax 0:66300c77c6e9 1131 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1132 /* free varbinds (if available) */
tax 0:66300c77c6e9 1133 snmp_varbind_list_free(&m_stat->invb);
tax 0:66300c77c6e9 1134 return ERR_ARG;
tax 0:66300c77c6e9 1135 }
tax 0:66300c77c6e9 1136 ofs += (1 + len_octets);
tax 0:66300c77c6e9 1137 vb_len -= (1 + len_octets);
tax 0:66300c77c6e9 1138
tax 0:66300c77c6e9 1139 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 1140 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 1141 if ((derr != ERR_OK) || (type != (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID)))
tax 0:66300c77c6e9 1142 {
tax 0:66300c77c6e9 1143 /* can't decode object name length */
tax 0:66300c77c6e9 1144 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1145 /* free varbinds (if available) */
tax 0:66300c77c6e9 1146 snmp_varbind_list_free(&m_stat->invb);
tax 0:66300c77c6e9 1147 return ERR_ARG;
tax 0:66300c77c6e9 1148 }
tax 0:66300c77c6e9 1149 derr = snmp_asn1_dec_oid(p, ofs + 1 + len_octets, len, &oid);
tax 0:66300c77c6e9 1150 if (derr != ERR_OK)
tax 0:66300c77c6e9 1151 {
tax 0:66300c77c6e9 1152 /* can't decode object name */
tax 0:66300c77c6e9 1153 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1154 /* free varbinds (if available) */
tax 0:66300c77c6e9 1155 snmp_varbind_list_free(&m_stat->invb);
tax 0:66300c77c6e9 1156 return ERR_ARG;
tax 0:66300c77c6e9 1157 }
tax 0:66300c77c6e9 1158 ofs += (1 + len_octets + len);
tax 0:66300c77c6e9 1159 vb_len -= (1 + len_octets + len);
tax 0:66300c77c6e9 1160
tax 0:66300c77c6e9 1161 snmp_asn1_dec_type(p, ofs, &type);
tax 0:66300c77c6e9 1162 derr = snmp_asn1_dec_length(p, ofs+1, &len_octets, &len);
tax 0:66300c77c6e9 1163 if (derr != ERR_OK)
tax 0:66300c77c6e9 1164 {
tax 0:66300c77c6e9 1165 /* can't decode object value length */
tax 0:66300c77c6e9 1166 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1167 /* free varbinds (if available) */
tax 0:66300c77c6e9 1168 snmp_varbind_list_free(&m_stat->invb);
tax 0:66300c77c6e9 1169 return ERR_ARG;
tax 0:66300c77c6e9 1170 }
tax 0:66300c77c6e9 1171
tax 0:66300c77c6e9 1172 switch (type)
tax 0:66300c77c6e9 1173 {
tax 0:66300c77c6e9 1174 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
tax 0:66300c77c6e9 1175 vb = snmp_varbind_alloc(&oid, type, sizeof(s32_t));
tax 0:66300c77c6e9 1176 if (vb != NULL)
tax 0:66300c77c6e9 1177 {
tax 0:66300c77c6e9 1178 s32_t *vptr = (s32_t*)vb->value;
tax 0:66300c77c6e9 1179
tax 0:66300c77c6e9 1180 derr = snmp_asn1_dec_s32t(p, ofs + 1 + len_octets, len, vptr);
tax 0:66300c77c6e9 1181 snmp_varbind_tail_add(&m_stat->invb, vb);
tax 0:66300c77c6e9 1182 }
tax 0:66300c77c6e9 1183 else
tax 0:66300c77c6e9 1184 {
tax 0:66300c77c6e9 1185 derr = ERR_ARG;
tax 0:66300c77c6e9 1186 }
tax 0:66300c77c6e9 1187 break;
tax 0:66300c77c6e9 1188 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
tax 0:66300c77c6e9 1189 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
tax 0:66300c77c6e9 1190 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
tax 0:66300c77c6e9 1191 vb = snmp_varbind_alloc(&oid, type, sizeof(u32_t));
tax 0:66300c77c6e9 1192 if (vb != NULL)
tax 0:66300c77c6e9 1193 {
tax 0:66300c77c6e9 1194 u32_t *vptr = (u32_t*)vb->value;
tax 0:66300c77c6e9 1195
tax 0:66300c77c6e9 1196 derr = snmp_asn1_dec_u32t(p, ofs + 1 + len_octets, len, vptr);
tax 0:66300c77c6e9 1197 snmp_varbind_tail_add(&m_stat->invb, vb);
tax 0:66300c77c6e9 1198 }
tax 0:66300c77c6e9 1199 else
tax 0:66300c77c6e9 1200 {
tax 0:66300c77c6e9 1201 derr = ERR_ARG;
tax 0:66300c77c6e9 1202 }
tax 0:66300c77c6e9 1203 break;
tax 0:66300c77c6e9 1204 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
tax 0:66300c77c6e9 1205 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
tax 0:66300c77c6e9 1206 LWIP_ASSERT("invalid length", len <= 0xff);
tax 0:66300c77c6e9 1207 vb = snmp_varbind_alloc(&oid, type, (u8_t)len);
tax 0:66300c77c6e9 1208 if (vb != NULL)
tax 0:66300c77c6e9 1209 {
tax 0:66300c77c6e9 1210 derr = snmp_asn1_dec_raw(p, ofs + 1 + len_octets, len, vb->value_len, (u8_t*)vb->value);
tax 0:66300c77c6e9 1211 snmp_varbind_tail_add(&m_stat->invb, vb);
tax 0:66300c77c6e9 1212 }
tax 0:66300c77c6e9 1213 else
tax 0:66300c77c6e9 1214 {
tax 0:66300c77c6e9 1215 derr = ERR_ARG;
tax 0:66300c77c6e9 1216 }
tax 0:66300c77c6e9 1217 break;
tax 0:66300c77c6e9 1218 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
tax 0:66300c77c6e9 1219 vb = snmp_varbind_alloc(&oid, type, 0);
tax 0:66300c77c6e9 1220 if (vb != NULL)
tax 0:66300c77c6e9 1221 {
tax 0:66300c77c6e9 1222 snmp_varbind_tail_add(&m_stat->invb, vb);
tax 0:66300c77c6e9 1223 derr = ERR_OK;
tax 0:66300c77c6e9 1224 }
tax 0:66300c77c6e9 1225 else
tax 0:66300c77c6e9 1226 {
tax 0:66300c77c6e9 1227 derr = ERR_ARG;
tax 0:66300c77c6e9 1228 }
tax 0:66300c77c6e9 1229 break;
tax 0:66300c77c6e9 1230 case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
tax 0:66300c77c6e9 1231 derr = snmp_asn1_dec_oid(p, ofs + 1 + len_octets, len, &oid_value);
tax 0:66300c77c6e9 1232 if (derr == ERR_OK)
tax 0:66300c77c6e9 1233 {
tax 0:66300c77c6e9 1234 vb = snmp_varbind_alloc(&oid, type, oid_value.len * sizeof(s32_t));
tax 0:66300c77c6e9 1235 if (vb != NULL)
tax 0:66300c77c6e9 1236 {
tax 0:66300c77c6e9 1237 u8_t i = oid_value.len;
tax 0:66300c77c6e9 1238 s32_t *vptr = (s32_t*)vb->value;
tax 0:66300c77c6e9 1239
tax 0:66300c77c6e9 1240 while(i > 0)
tax 0:66300c77c6e9 1241 {
tax 0:66300c77c6e9 1242 i--;
tax 0:66300c77c6e9 1243 vptr[i] = oid_value.id[i];
tax 0:66300c77c6e9 1244 }
tax 0:66300c77c6e9 1245 snmp_varbind_tail_add(&m_stat->invb, vb);
tax 0:66300c77c6e9 1246 derr = ERR_OK;
tax 0:66300c77c6e9 1247 }
tax 0:66300c77c6e9 1248 else
tax 0:66300c77c6e9 1249 {
tax 0:66300c77c6e9 1250 derr = ERR_ARG;
tax 0:66300c77c6e9 1251 }
tax 0:66300c77c6e9 1252 }
tax 0:66300c77c6e9 1253 break;
tax 0:66300c77c6e9 1254 case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
tax 0:66300c77c6e9 1255 if (len == 4)
tax 0:66300c77c6e9 1256 {
tax 0:66300c77c6e9 1257 /* must be exactly 4 octets! */
tax 0:66300c77c6e9 1258 vb = snmp_varbind_alloc(&oid, type, 4);
tax 0:66300c77c6e9 1259 if (vb != NULL)
tax 0:66300c77c6e9 1260 {
tax 0:66300c77c6e9 1261 derr = snmp_asn1_dec_raw(p, ofs + 1 + len_octets, len, vb->value_len, (u8_t*)vb->value);
tax 0:66300c77c6e9 1262 snmp_varbind_tail_add(&m_stat->invb, vb);
tax 0:66300c77c6e9 1263 }
tax 0:66300c77c6e9 1264 else
tax 0:66300c77c6e9 1265 {
tax 0:66300c77c6e9 1266 derr = ERR_ARG;
tax 0:66300c77c6e9 1267 }
tax 0:66300c77c6e9 1268 }
tax 0:66300c77c6e9 1269 else
tax 0:66300c77c6e9 1270 {
tax 0:66300c77c6e9 1271 derr = ERR_ARG;
tax 0:66300c77c6e9 1272 }
tax 0:66300c77c6e9 1273 break;
tax 0:66300c77c6e9 1274 default:
tax 0:66300c77c6e9 1275 derr = ERR_ARG;
tax 0:66300c77c6e9 1276 break;
tax 0:66300c77c6e9 1277 }
tax 0:66300c77c6e9 1278 if (derr != ERR_OK)
tax 0:66300c77c6e9 1279 {
tax 0:66300c77c6e9 1280 snmp_inc_snmpinasnparseerrs();
tax 0:66300c77c6e9 1281 /* free varbinds (if available) */
tax 0:66300c77c6e9 1282 snmp_varbind_list_free(&m_stat->invb);
tax 0:66300c77c6e9 1283 return ERR_ARG;
tax 0:66300c77c6e9 1284 }
tax 0:66300c77c6e9 1285 ofs += (1 + len_octets + len);
tax 0:66300c77c6e9 1286 vb_len -= (1 + len_octets + len);
tax 0:66300c77c6e9 1287 }
tax 0:66300c77c6e9 1288
tax 0:66300c77c6e9 1289 if (m_stat->rt == SNMP_ASN1_PDU_SET_REQ)
tax 0:66300c77c6e9 1290 {
tax 0:66300c77c6e9 1291 snmp_add_snmpintotalsetvars(m_stat->invb.count);
tax 0:66300c77c6e9 1292 }
tax 0:66300c77c6e9 1293 else
tax 0:66300c77c6e9 1294 {
tax 0:66300c77c6e9 1295 snmp_add_snmpintotalreqvars(m_stat->invb.count);
tax 0:66300c77c6e9 1296 }
tax 0:66300c77c6e9 1297
tax 0:66300c77c6e9 1298 *ofs_ret = ofs;
tax 0:66300c77c6e9 1299 return ERR_OK;
tax 0:66300c77c6e9 1300 }
tax 0:66300c77c6e9 1301
tax 0:66300c77c6e9 1302 struct snmp_varbind*
tax 0:66300c77c6e9 1303 snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len)
tax 0:66300c77c6e9 1304 {
tax 0:66300c77c6e9 1305 struct snmp_varbind *vb;
tax 0:66300c77c6e9 1306
tax 0:66300c77c6e9 1307 vb = (struct snmp_varbind *)memp_malloc(MEMP_SNMP_VARBIND);
tax 0:66300c77c6e9 1308 LWIP_ASSERT("vb != NULL",vb != NULL);
tax 0:66300c77c6e9 1309 if (vb != NULL)
tax 0:66300c77c6e9 1310 {
tax 0:66300c77c6e9 1311 u8_t i;
tax 0:66300c77c6e9 1312
tax 0:66300c77c6e9 1313 vb->next = NULL;
tax 0:66300c77c6e9 1314 vb->prev = NULL;
tax 0:66300c77c6e9 1315 i = oid->len;
tax 0:66300c77c6e9 1316 vb->ident_len = i;
tax 0:66300c77c6e9 1317 if (i > 0)
tax 0:66300c77c6e9 1318 {
tax 0:66300c77c6e9 1319 LWIP_ASSERT("SNMP_MAX_TREE_DEPTH is configured too low", i <= SNMP_MAX_TREE_DEPTH);
tax 0:66300c77c6e9 1320 /* allocate array of s32_t for our object identifier */
tax 0:66300c77c6e9 1321 vb->ident = (s32_t*)memp_malloc(MEMP_SNMP_VALUE);
tax 0:66300c77c6e9 1322 LWIP_ASSERT("vb->ident != NULL",vb->ident != NULL);
tax 0:66300c77c6e9 1323 if (vb->ident == NULL)
tax 0:66300c77c6e9 1324 {
tax 0:66300c77c6e9 1325 memp_free(MEMP_SNMP_VARBIND, vb);
tax 0:66300c77c6e9 1326 return NULL;
tax 0:66300c77c6e9 1327 }
tax 0:66300c77c6e9 1328 while(i > 0)
tax 0:66300c77c6e9 1329 {
tax 0:66300c77c6e9 1330 i--;
tax 0:66300c77c6e9 1331 vb->ident[i] = oid->id[i];
tax 0:66300c77c6e9 1332 }
tax 0:66300c77c6e9 1333 }
tax 0:66300c77c6e9 1334 else
tax 0:66300c77c6e9 1335 {
tax 0:66300c77c6e9 1336 /* i == 0, pass zero length object identifier */
tax 0:66300c77c6e9 1337 vb->ident = NULL;
tax 0:66300c77c6e9 1338 }
tax 0:66300c77c6e9 1339 vb->value_type = type;
tax 0:66300c77c6e9 1340 vb->value_len = len;
tax 0:66300c77c6e9 1341 if (len > 0)
tax 0:66300c77c6e9 1342 {
tax 0:66300c77c6e9 1343 LWIP_ASSERT("SNMP_MAX_OCTET_STRING_LEN is configured too low", vb->value_len <= SNMP_MAX_VALUE_SIZE);
tax 0:66300c77c6e9 1344 /* allocate raw bytes for our object value */
tax 0:66300c77c6e9 1345 vb->value = memp_malloc(MEMP_SNMP_VALUE);
tax 0:66300c77c6e9 1346 LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
tax 0:66300c77c6e9 1347 if (vb->value == NULL)
tax 0:66300c77c6e9 1348 {
tax 0:66300c77c6e9 1349 if (vb->ident != NULL)
tax 0:66300c77c6e9 1350 {
tax 0:66300c77c6e9 1351 memp_free(MEMP_SNMP_VALUE, vb->ident);
tax 0:66300c77c6e9 1352 }
tax 0:66300c77c6e9 1353 memp_free(MEMP_SNMP_VARBIND, vb);
tax 0:66300c77c6e9 1354 return NULL;
tax 0:66300c77c6e9 1355 }
tax 0:66300c77c6e9 1356 }
tax 0:66300c77c6e9 1357 else
tax 0:66300c77c6e9 1358 {
tax 0:66300c77c6e9 1359 /* ASN1_NUL type, or zero length ASN1_OC_STR */
tax 0:66300c77c6e9 1360 vb->value = NULL;
tax 0:66300c77c6e9 1361 }
tax 0:66300c77c6e9 1362 }
tax 0:66300c77c6e9 1363 return vb;
tax 0:66300c77c6e9 1364 }
tax 0:66300c77c6e9 1365
tax 0:66300c77c6e9 1366 void
tax 0:66300c77c6e9 1367 snmp_varbind_free(struct snmp_varbind *vb)
tax 0:66300c77c6e9 1368 {
tax 0:66300c77c6e9 1369 if (vb->value != NULL )
tax 0:66300c77c6e9 1370 {
tax 0:66300c77c6e9 1371 memp_free(MEMP_SNMP_VALUE, vb->value);
tax 0:66300c77c6e9 1372 }
tax 0:66300c77c6e9 1373 if (vb->ident != NULL )
tax 0:66300c77c6e9 1374 {
tax 0:66300c77c6e9 1375 memp_free(MEMP_SNMP_VALUE, vb->ident);
tax 0:66300c77c6e9 1376 }
tax 0:66300c77c6e9 1377 memp_free(MEMP_SNMP_VARBIND, vb);
tax 0:66300c77c6e9 1378 }
tax 0:66300c77c6e9 1379
tax 0:66300c77c6e9 1380 void
tax 0:66300c77c6e9 1381 snmp_varbind_list_free(struct snmp_varbind_root *root)
tax 0:66300c77c6e9 1382 {
tax 0:66300c77c6e9 1383 struct snmp_varbind *vb, *prev;
tax 0:66300c77c6e9 1384
tax 0:66300c77c6e9 1385 vb = root->tail;
tax 0:66300c77c6e9 1386 while ( vb != NULL )
tax 0:66300c77c6e9 1387 {
tax 0:66300c77c6e9 1388 prev = vb->prev;
tax 0:66300c77c6e9 1389 snmp_varbind_free(vb);
tax 0:66300c77c6e9 1390 vb = prev;
tax 0:66300c77c6e9 1391 }
tax 0:66300c77c6e9 1392 root->count = 0;
tax 0:66300c77c6e9 1393 root->head = NULL;
tax 0:66300c77c6e9 1394 root->tail = NULL;
tax 0:66300c77c6e9 1395 }
tax 0:66300c77c6e9 1396
tax 0:66300c77c6e9 1397 void
tax 0:66300c77c6e9 1398 snmp_varbind_tail_add(struct snmp_varbind_root *root, struct snmp_varbind *vb)
tax 0:66300c77c6e9 1399 {
tax 0:66300c77c6e9 1400 if (root->count == 0)
tax 0:66300c77c6e9 1401 {
tax 0:66300c77c6e9 1402 /* add first varbind to list */
tax 0:66300c77c6e9 1403 root->head = vb;
tax 0:66300c77c6e9 1404 root->tail = vb;
tax 0:66300c77c6e9 1405 }
tax 0:66300c77c6e9 1406 else
tax 0:66300c77c6e9 1407 {
tax 0:66300c77c6e9 1408 /* add nth varbind to list tail */
tax 0:66300c77c6e9 1409 root->tail->next = vb;
tax 0:66300c77c6e9 1410 vb->prev = root->tail;
tax 0:66300c77c6e9 1411 root->tail = vb;
tax 0:66300c77c6e9 1412 }
tax 0:66300c77c6e9 1413 root->count += 1;
tax 0:66300c77c6e9 1414 }
tax 0:66300c77c6e9 1415
tax 0:66300c77c6e9 1416 struct snmp_varbind*
tax 0:66300c77c6e9 1417 snmp_varbind_tail_remove(struct snmp_varbind_root *root)
tax 0:66300c77c6e9 1418 {
tax 0:66300c77c6e9 1419 struct snmp_varbind* vb;
tax 0:66300c77c6e9 1420
tax 0:66300c77c6e9 1421 if (root->count > 0)
tax 0:66300c77c6e9 1422 {
tax 0:66300c77c6e9 1423 /* remove tail varbind */
tax 0:66300c77c6e9 1424 vb = root->tail;
tax 0:66300c77c6e9 1425 root->tail = vb->prev;
tax 0:66300c77c6e9 1426 vb->prev->next = NULL;
tax 0:66300c77c6e9 1427 root->count -= 1;
tax 0:66300c77c6e9 1428 }
tax 0:66300c77c6e9 1429 else
tax 0:66300c77c6e9 1430 {
tax 0:66300c77c6e9 1431 /* nothing to remove */
tax 0:66300c77c6e9 1432 vb = NULL;
tax 0:66300c77c6e9 1433 }
tax 0:66300c77c6e9 1434 return vb;
tax 0:66300c77c6e9 1435 }
tax 0:66300c77c6e9 1436
tax 0:66300c77c6e9 1437 #endif /* LWIP_SNMP */