Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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