| 1 |
/** |
| 2 |
* @file |
| 3 |
* SNMP output message processing (RFC1157). |
| 4 |
* |
| 5 |
* Output responses and traps are build in two passes: |
| 6 |
* |
| 7 |
* Pass 0: iterate over the output message backwards to determine encoding lengths |
| 8 |
* Pass 1: the actual forward encoding of internal form into ASN1 |
| 9 |
* |
| 10 |
* The single-pass encoding method described by Comer & Stevens |
| 11 |
* requires extra buffer space and copying for reversal of the packet. |
| 12 |
* The buffer requirement can be prohibitively large for big payloads |
| 13 |
* (>= 484) therefore we use the two encoding passes. |
| 14 |
*/ |
| 15 |
|
| 16 |
/* |
| 17 |
* Copyright (c) 2006 Axon Digital Design B.V., The Netherlands. |
| 18 |
* All rights reserved. |
| 19 |
* |
| 20 |
* Redistribution and use in source and binary forms, with or without modification, |
| 21 |
* are permitted provided that the following conditions are met: |
| 22 |
* |
| 23 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 24 |
* this list of conditions and the following disclaimer. |
| 25 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 26 |
* this list of conditions and the following disclaimer in the documentation |
| 27 |
* and/or other materials provided with the distribution. |
| 28 |
* 3. The name of the author may not be used to endorse or promote products |
| 29 |
* derived from this software without specific prior written permission. |
| 30 |
* |
| 31 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 32 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 33 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 34 |
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 35 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 36 |
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 37 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 38 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 39 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 40 |
* OF SUCH DAMAGE. |
| 41 |
* |
| 42 |
* Author: Christiaan Simons <[email protected]> |
| 43 |
*/ |
| 44 |
|
| 45 |
#include "lwip/opt.h" |
| 46 |
|
| 47 |
#if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */ |
| 48 |
|
| 49 |
#include "lwip/udp.h" |
| 50 |
#include "lwip/netif.h" |
| 51 |
#include "lwip/snmp.h" |
| 52 |
#include "lwip/snmp_asn1.h" |
| 53 |
#include "lwip/snmp_msg.h" |
| 54 |
|
| 55 |
struct snmp_trap_dst |
| 56 |
{ |
| 57 |
/* destination IP address in network order */ |
| 58 |
ip_addr_t dip; |
| 59 |
/* set to 0 when disabled, >0 when enabled */ |
| 60 |
u8_t enable; |
| 61 |
}; |
| 62 |
struct snmp_trap_dst trap_dst[SNMP_TRAP_DESTINATIONS]; |
| 63 |
|
| 64 |
/** TRAP message structure */ |
| 65 |
struct snmp_msg_trap trap_msg; |
| 66 |
|
| 67 |
static u16_t snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len); |
| 68 |
static u16_t snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len); |
| 69 |
static u16_t snmp_varbind_list_sum(struct snmp_varbind_root *root); |
| 70 |
|
| 71 |
static u16_t snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p); |
| 72 |
static u16_t snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p); |
| 73 |
static u16_t snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs); |
| 74 |
|
| 75 |
/** |
| 76 |
* Sets enable switch for this trap destination. |
| 77 |
* @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1 |
| 78 |
* @param enable switch if 0 destination is disabled >0 enabled. |
| 79 |
*/ |
| 80 |
void |
| 81 |
snmp_trap_dst_enable(u8_t dst_idx, u8_t enable) |
| 82 |
{ |
| 83 |
if (dst_idx < SNMP_TRAP_DESTINATIONS) |
| 84 |
{ |
| 85 |
trap_dst[dst_idx].enable = enable; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Sets IPv4 address for this trap destination. |
| 91 |
* @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1 |
| 92 |
* @param dst IPv4 address in host order. |
| 93 |
*/ |
| 94 |
void |
| 95 |
snmp_trap_dst_ip_set(u8_t dst_idx, ip_addr_t *dst) |
| 96 |
{ |
| 97 |
if (dst_idx < SNMP_TRAP_DESTINATIONS) |
| 98 |
{ |
| 99 |
ip_addr_set(&trap_dst[dst_idx].dip, dst); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Sends a 'getresponse' message to the request originator. |
| 105 |
* |
| 106 |
* @param m_stat points to the current message request state source |
| 107 |
* @return ERR_OK when success, ERR_MEM if we're out of memory |
| 108 |
* |
| 109 |
* @note the caller is responsible for filling in outvb in the m_stat |
| 110 |
* and provide error-status and index (except for tooBig errors) ... |
| 111 |
*/ |
| 112 |
err_t |
| 113 |
snmp_send_response(struct snmp_msg_pstat *m_stat) |
| 114 |
{ |
| 115 |
struct snmp_varbind_root emptyvb = {NULL, NULL, 0, 0, 0}; |
| 116 |
struct pbuf *p; |
| 117 |
u16_t tot_len; |
| 118 |
err_t err; |
| 119 |
|
| 120 |
/* pass 0, calculate length fields */ |
| 121 |
tot_len = snmp_varbind_list_sum(&m_stat->outvb); |
| 122 |
tot_len = snmp_resp_header_sum(m_stat, tot_len); |
| 123 |
|
| 124 |
/* try allocating pbuf(s) for complete response */ |
| 125 |
p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL); |
| 126 |
if (p == NULL) |
| 127 |
{ |
| 128 |
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() tooBig\n")); |
| 129 |
|
| 130 |
/* can't construct reply, return error-status tooBig */ |
| 131 |
m_stat->error_status = SNMP_ES_TOOBIG; |
| 132 |
m_stat->error_index = 0; |
| 133 |
/* pass 0, recalculate lengths, for empty varbind-list */ |
| 134 |
tot_len = snmp_varbind_list_sum(&emptyvb); |
| 135 |
tot_len = snmp_resp_header_sum(m_stat, tot_len); |
| 136 |
/* retry allocation once for header and empty varbind-list */ |
| 137 |
p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL); |
| 138 |
} |
| 139 |
if (p != NULL) |
| 140 |
{ |
| 141 |
/* first pbuf alloc try or retry alloc success */ |
| 142 |
u16_t ofs; |
| 143 |
|
| 144 |
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() p != NULL\n")); |
| 145 |
|
| 146 |
/* pass 1, size error, encode packet ino the pbuf(s) */ |
| 147 |
ofs = snmp_resp_header_enc(m_stat, p); |
| 148 |
snmp_varbind_list_enc(&m_stat->outvb, p, ofs); |
| 149 |
|
| 150 |
switch (m_stat->error_status) |
| 151 |
{ |
| 152 |
case SNMP_ES_NOERROR: |
| 153 |
/* nothing to do */ |
| 154 |
break; |
| 155 |
case SNMP_ES_TOOBIG: |
| 156 |
snmp_inc_snmpouttoobigs(); |
| 157 |
break; |
| 158 |
case SNMP_ES_NOSUCHNAME: |
| 159 |
snmp_inc_snmpoutnosuchnames(); |
| 160 |
break; |
| 161 |
case SNMP_ES_BADVALUE: |
| 162 |
snmp_inc_snmpoutbadvalues(); |
| 163 |
break; |
| 164 |
case SNMP_ES_GENERROR: |
| 165 |
snmp_inc_snmpoutgenerrs(); |
| 166 |
break; |
| 167 |
default: |
| 168 |
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_send_response(): unknown error_status: %d\n", m_stat->error_status)); |
| 169 |
break; |
| 170 |
} |
| 171 |
snmp_inc_snmpoutgetresponses(); |
| 172 |
snmp_inc_snmpoutpkts(); |
| 173 |
|
| 174 |
/** @todo do we need separate rx and tx pcbs for threaded case? */ |
| 175 |
/** connect to the originating source */ |
| 176 |
udp_connect(m_stat->pcb, &m_stat->sip, m_stat->sp); |
| 177 |
err = udp_send(m_stat->pcb, p); |
| 178 |
if (err == ERR_MEM) |
| 179 |
{ |
| 180 |
/** @todo release some memory, retry and return tooBig? tooMuchHassle? */ |
| 181 |
err = ERR_MEM; |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
err = ERR_OK; |
| 186 |
} |
| 187 |
/** disassociate remote address and port with this pcb */ |
| 188 |
udp_disconnect(m_stat->pcb); |
| 189 |
|
| 190 |
pbuf_free(p); |
| 191 |
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() done\n")); |
| 192 |
return err; |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
/* first pbuf alloc try or retry alloc failed |
| 197 |
very low on memory, couldn't return tooBig */ |
| 198 |
return ERR_MEM; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Sends an generic or enterprise specific trap message. |
| 205 |
* |
| 206 |
* @param generic_trap is the trap code |
| 207 |
* @param eoid points to enterprise object identifier |
| 208 |
* @param specific_trap used for enterprise traps when generic_trap == 6 |
| 209 |
* @return ERR_OK when success, ERR_MEM if we're out of memory |
| 210 |
* |
| 211 |
* @note the caller is responsible for filling in outvb in the trap_msg |
| 212 |
* @note the use of the enterpise identifier field |
| 213 |
* is per RFC1215. |
| 214 |
* Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps |
| 215 |
* and .iso.org.dod.internet.private.enterprises.yourenterprise |
| 216 |
* (sysObjectID) for specific traps. |
| 217 |
*/ |
| 218 |
err_t |
| 219 |
snmp_send_trap(s8_t generic_trap, struct snmp_obj_id *eoid, s32_t specific_trap) |
| 220 |
{ |
| 221 |
struct snmp_trap_dst *td; |
| 222 |
struct netif *dst_if; |
| 223 |
ip_addr_t dst_ip; |
| 224 |
struct pbuf *p; |
| 225 |
u16_t i,tot_len; |
| 226 |
err_t err = ERR_OK; |
| 227 |
|
| 228 |
for (i=0, td = &trap_dst[0]; i<SNMP_TRAP_DESTINATIONS; i++, td++) |
| 229 |
{ |
| 230 |
if ((td->enable != 0) && !ip_addr_isany(&td->dip)) |
| 231 |
{ |
| 232 |
/* network order trap destination */ |
| 233 |
ip_addr_copy(trap_msg.dip, td->dip); |
| 234 |
/* lookup current source address for this dst */ |
| 235 |
dst_if = ip_route(&td->dip); |
| 236 |
if (dst_if != NULL) { |
| 237 |
ip_addr_copy(dst_ip, dst_if->ip_addr); |
| 238 |
/* @todo: what about IPv6? */ |
| 239 |
trap_msg.sip_raw[0] = ip4_addr1(&dst_ip); |
| 240 |
trap_msg.sip_raw[1] = ip4_addr2(&dst_ip); |
| 241 |
trap_msg.sip_raw[2] = ip4_addr3(&dst_ip); |
| 242 |
trap_msg.sip_raw[3] = ip4_addr4(&dst_ip); |
| 243 |
trap_msg.gen_trap = generic_trap; |
| 244 |
trap_msg.spc_trap = specific_trap; |
| 245 |
if (generic_trap == SNMP_GENTRAP_ENTERPRISESPC) |
| 246 |
{ |
| 247 |
/* enterprise-Specific trap */ |
| 248 |
trap_msg.enterprise = eoid; |
| 249 |
} |
| 250 |
else |
| 251 |
{ |
| 252 |
/* generic (MIB-II) trap */ |
| 253 |
snmp_get_snmpgrpid_ptr(&trap_msg.enterprise); |
| 254 |
} |
| 255 |
snmp_get_sysuptime(&trap_msg.ts); |
| 256 |
|
| 257 |
/* pass 0, calculate length fields */ |
| 258 |
tot_len = snmp_varbind_list_sum(&trap_msg.outvb); |
| 259 |
tot_len = snmp_trap_header_sum(&trap_msg, tot_len); |
| 260 |
|
| 261 |
/* allocate pbuf(s) */ |
| 262 |
p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_POOL); |
| 263 |
if (p != NULL) |
| 264 |
{ |
| 265 |
u16_t ofs; |
| 266 |
|
| 267 |
/* pass 1, encode packet ino the pbuf(s) */ |
| 268 |
ofs = snmp_trap_header_enc(&trap_msg, p); |
| 269 |
snmp_varbind_list_enc(&trap_msg.outvb, p, ofs); |
| 270 |
|
| 271 |
snmp_inc_snmpouttraps(); |
| 272 |
snmp_inc_snmpoutpkts(); |
| 273 |
|
| 274 |
/** send to the TRAP destination */ |
| 275 |
udp_sendto(trap_msg.pcb, p, &trap_msg.dip, SNMP_TRAP_PORT); |
| 276 |
|
| 277 |
pbuf_free(p); |
| 278 |
} else { |
| 279 |
err = ERR_MEM; |
| 280 |
} |
| 281 |
} else { |
| 282 |
/* routing error */ |
| 283 |
err = ERR_RTE; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
return err; |
| 288 |
} |
| 289 |
|
| 290 |
void |
| 291 |
snmp_coldstart_trap(void) |
| 292 |
{ |
| 293 |
trap_msg.outvb.head = NULL; |
| 294 |
trap_msg.outvb.tail = NULL; |
| 295 |
trap_msg.outvb.count = 0; |
| 296 |
snmp_send_trap(SNMP_GENTRAP_COLDSTART, NULL, 0); |
| 297 |
} |
| 298 |
|
| 299 |
void |
| 300 |
snmp_authfail_trap(void) |
| 301 |
{ |
| 302 |
u8_t enable; |
| 303 |
snmp_get_snmpenableauthentraps(&enable); |
| 304 |
if (enable == 1) |
| 305 |
{ |
| 306 |
trap_msg.outvb.head = NULL; |
| 307 |
trap_msg.outvb.tail = NULL; |
| 308 |
trap_msg.outvb.count = 0; |
| 309 |
snmp_send_trap(SNMP_GENTRAP_AUTHFAIL, NULL, 0); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Sums response header field lengths from tail to head and |
| 315 |
* returns resp_header_lengths for second encoding pass. |
| 316 |
* |
| 317 |
* @param vb_len varbind-list length |
| 318 |
* @param rhl points to returned header lengths |
| 319 |
* @return the required lenght for encoding the response header |
| 320 |
*/ |
| 321 |
static u16_t |
| 322 |
snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len) |
| 323 |
{ |
| 324 |
u16_t tot_len; |
| 325 |
struct snmp_resp_header_lengths *rhl; |
| 326 |
|
| 327 |
rhl = &m_stat->rhl; |
| 328 |
tot_len = vb_len; |
| 329 |
snmp_asn1_enc_s32t_cnt(m_stat->error_index, &rhl->erridxlen); |
| 330 |
snmp_asn1_enc_length_cnt(rhl->erridxlen, &rhl->erridxlenlen); |
| 331 |
tot_len += 1 + rhl->erridxlenlen + rhl->erridxlen; |
| 332 |
|
| 333 |
snmp_asn1_enc_s32t_cnt(m_stat->error_status, &rhl->errstatlen); |
| 334 |
snmp_asn1_enc_length_cnt(rhl->errstatlen, &rhl->errstatlenlen); |
| 335 |
tot_len += 1 + rhl->errstatlenlen + rhl->errstatlen; |
| 336 |
|
| 337 |
snmp_asn1_enc_s32t_cnt(m_stat->rid, &rhl->ridlen); |
| 338 |
snmp_asn1_enc_length_cnt(rhl->ridlen, &rhl->ridlenlen); |
| 339 |
tot_len += 1 + rhl->ridlenlen + rhl->ridlen; |
| 340 |
|
| 341 |
rhl->pdulen = tot_len; |
| 342 |
snmp_asn1_enc_length_cnt(rhl->pdulen, &rhl->pdulenlen); |
| 343 |
tot_len += 1 + rhl->pdulenlen; |
| 344 |
|
| 345 |
rhl->comlen = m_stat->com_strlen; |
| 346 |
snmp_asn1_enc_length_cnt(rhl->comlen, &rhl->comlenlen); |
| 347 |
tot_len += 1 + rhl->comlenlen + rhl->comlen; |
| 348 |
|
| 349 |
snmp_asn1_enc_s32t_cnt(snmp_version, &rhl->verlen); |
| 350 |
snmp_asn1_enc_length_cnt(rhl->verlen, &rhl->verlenlen); |
| 351 |
tot_len += 1 + rhl->verlen + rhl->verlenlen; |
| 352 |
|
| 353 |
rhl->seqlen = tot_len; |
| 354 |
snmp_asn1_enc_length_cnt(rhl->seqlen, &rhl->seqlenlen); |
| 355 |
tot_len += 1 + rhl->seqlenlen; |
| 356 |
|
| 357 |
return tot_len; |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Sums trap header field lengths from tail to head and |
| 362 |
* returns trap_header_lengths for second encoding pass. |
| 363 |
* |
| 364 |
* @param vb_len varbind-list length |
| 365 |
* @param thl points to returned header lengths |
| 366 |
* @return the required lenght for encoding the trap header |
| 367 |
*/ |
| 368 |
static u16_t |
| 369 |
snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len) |
| 370 |
{ |
| 371 |
u16_t tot_len; |
| 372 |
struct snmp_trap_header_lengths *thl; |
| 373 |
|
| 374 |
thl = &m_trap->thl; |
| 375 |
tot_len = vb_len; |
| 376 |
|
| 377 |
snmp_asn1_enc_u32t_cnt(m_trap->ts, &thl->tslen); |
| 378 |
snmp_asn1_enc_length_cnt(thl->tslen, &thl->tslenlen); |
| 379 |
tot_len += 1 + thl->tslen + thl->tslenlen; |
| 380 |
|
| 381 |
snmp_asn1_enc_s32t_cnt(m_trap->spc_trap, &thl->strplen); |
| 382 |
snmp_asn1_enc_length_cnt(thl->strplen, &thl->strplenlen); |
| 383 |
tot_len += 1 + thl->strplen + thl->strplenlen; |
| 384 |
|
| 385 |
snmp_asn1_enc_s32t_cnt(m_trap->gen_trap, &thl->gtrplen); |
| 386 |
snmp_asn1_enc_length_cnt(thl->gtrplen, &thl->gtrplenlen); |
| 387 |
tot_len += 1 + thl->gtrplen + thl->gtrplenlen; |
| 388 |
|
| 389 |
thl->aaddrlen = 4; |
| 390 |
snmp_asn1_enc_length_cnt(thl->aaddrlen, &thl->aaddrlenlen); |
| 391 |
tot_len += 1 + thl->aaddrlen + thl->aaddrlenlen; |
| 392 |
|
| 393 |
snmp_asn1_enc_oid_cnt(m_trap->enterprise->len, &m_trap->enterprise->id[0], &thl->eidlen); |
| 394 |
snmp_asn1_enc_length_cnt(thl->eidlen, &thl->eidlenlen); |
| 395 |
tot_len += 1 + thl->eidlen + thl->eidlenlen; |
| 396 |
|
| 397 |
thl->pdulen = tot_len; |
| 398 |
snmp_asn1_enc_length_cnt(thl->pdulen, &thl->pdulenlen); |
| 399 |
tot_len += 1 + thl->pdulenlen; |
| 400 |
|
| 401 |
thl->comlen = sizeof(snmp_publiccommunity) - 1; |
| 402 |
snmp_asn1_enc_length_cnt(thl->comlen, &thl->comlenlen); |
| 403 |
tot_len += 1 + thl->comlenlen + thl->comlen; |
| 404 |
|
| 405 |
snmp_asn1_enc_s32t_cnt(snmp_version, &thl->verlen); |
| 406 |
snmp_asn1_enc_length_cnt(thl->verlen, &thl->verlenlen); |
| 407 |
tot_len += 1 + thl->verlen + thl->verlenlen; |
| 408 |
|
| 409 |
thl->seqlen = tot_len; |
| 410 |
snmp_asn1_enc_length_cnt(thl->seqlen, &thl->seqlenlen); |
| 411 |
tot_len += 1 + thl->seqlenlen; |
| 412 |
|
| 413 |
return tot_len; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Sums varbind lengths from tail to head and |
| 418 |
* annotates lengths in varbind for second encoding pass. |
| 419 |
* |
| 420 |
* @param root points to the root of the variable binding list |
| 421 |
* @return the required lenght for encoding the variable bindings |
| 422 |
*/ |
| 423 |
static u16_t |
| 424 |
snmp_varbind_list_sum(struct snmp_varbind_root *root) |
| 425 |
{ |
| 426 |
struct snmp_varbind *vb; |
| 427 |
u32_t *uint_ptr; |
| 428 |
s32_t *sint_ptr; |
| 429 |
u16_t tot_len; |
| 430 |
|
| 431 |
tot_len = 0; |
| 432 |
vb = root->tail; |
| 433 |
while ( vb != NULL ) |
| 434 |
{ |
| 435 |
/* encoded value lenght depends on type */ |
| 436 |
switch (vb->value_type) |
| 437 |
{ |
| 438 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG): |
| 439 |
sint_ptr = (s32_t*)vb->value; |
| 440 |
snmp_asn1_enc_s32t_cnt(*sint_ptr, &vb->vlen); |
| 441 |
break; |
| 442 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER): |
| 443 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE): |
| 444 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS): |
| 445 |
uint_ptr = (u32_t*)vb->value; |
| 446 |
snmp_asn1_enc_u32t_cnt(*uint_ptr, &vb->vlen); |
| 447 |
break; |
| 448 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR): |
| 449 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL): |
| 450 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR): |
| 451 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE): |
| 452 |
vb->vlen = vb->value_len; |
| 453 |
break; |
| 454 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID): |
| 455 |
sint_ptr = (s32_t*)vb->value; |
| 456 |
snmp_asn1_enc_oid_cnt(vb->value_len / sizeof(s32_t), sint_ptr, &vb->vlen); |
| 457 |
break; |
| 458 |
default: |
| 459 |
/* unsupported type */ |
| 460 |
vb->vlen = 0; |
| 461 |
break; |
| 462 |
}; |
| 463 |
/* encoding length of value length field */ |
| 464 |
snmp_asn1_enc_length_cnt(vb->vlen, &vb->vlenlen); |
| 465 |
snmp_asn1_enc_oid_cnt(vb->ident_len, vb->ident, &vb->olen); |
| 466 |
snmp_asn1_enc_length_cnt(vb->olen, &vb->olenlen); |
| 467 |
|
| 468 |
vb->seqlen = 1 + vb->vlenlen + vb->vlen; |
| 469 |
vb->seqlen += 1 + vb->olenlen + vb->olen; |
| 470 |
snmp_asn1_enc_length_cnt(vb->seqlen, &vb->seqlenlen); |
| 471 |
|
| 472 |
/* varbind seq */ |
| 473 |
tot_len += 1 + vb->seqlenlen + vb->seqlen; |
| 474 |
|
| 475 |
vb = vb->prev; |
| 476 |
} |
| 477 |
|
| 478 |
/* varbind-list seq */ |
| 479 |
root->seqlen = tot_len; |
| 480 |
snmp_asn1_enc_length_cnt(root->seqlen, &root->seqlenlen); |
| 481 |
tot_len += 1 + root->seqlenlen; |
| 482 |
|
| 483 |
return tot_len; |
| 484 |
} |
| 485 |
|
| 486 |
/** |
| 487 |
* Encodes response header from head to tail. |
| 488 |
*/ |
| 489 |
static u16_t |
| 490 |
snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p) |
| 491 |
{ |
| 492 |
u16_t ofs; |
| 493 |
|
| 494 |
ofs = 0; |
| 495 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)); |
| 496 |
ofs += 1; |
| 497 |
snmp_asn1_enc_length(p, ofs, m_stat->rhl.seqlen); |
| 498 |
ofs += m_stat->rhl.seqlenlen; |
| 499 |
|
| 500 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)); |
| 501 |
ofs += 1; |
| 502 |
snmp_asn1_enc_length(p, ofs, m_stat->rhl.verlen); |
| 503 |
ofs += m_stat->rhl.verlenlen; |
| 504 |
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.verlen, snmp_version); |
| 505 |
ofs += m_stat->rhl.verlen; |
| 506 |
|
| 507 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR)); |
| 508 |
ofs += 1; |
| 509 |
snmp_asn1_enc_length(p, ofs, m_stat->rhl.comlen); |
| 510 |
ofs += m_stat->rhl.comlenlen; |
| 511 |
snmp_asn1_enc_raw(p, ofs, m_stat->rhl.comlen, m_stat->community); |
| 512 |
ofs += m_stat->rhl.comlen; |
| 513 |
|
| 514 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_RESP)); |
| 515 |
ofs += 1; |
| 516 |
snmp_asn1_enc_length(p, ofs, m_stat->rhl.pdulen); |
| 517 |
ofs += m_stat->rhl.pdulenlen; |
| 518 |
|
| 519 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)); |
| 520 |
ofs += 1; |
| 521 |
snmp_asn1_enc_length(p, ofs, m_stat->rhl.ridlen); |
| 522 |
ofs += m_stat->rhl.ridlenlen; |
| 523 |
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.ridlen, m_stat->rid); |
| 524 |
ofs += m_stat->rhl.ridlen; |
| 525 |
|
| 526 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)); |
| 527 |
ofs += 1; |
| 528 |
snmp_asn1_enc_length(p, ofs, m_stat->rhl.errstatlen); |
| 529 |
ofs += m_stat->rhl.errstatlenlen; |
| 530 |
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.errstatlen, m_stat->error_status); |
| 531 |
ofs += m_stat->rhl.errstatlen; |
| 532 |
|
| 533 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)); |
| 534 |
ofs += 1; |
| 535 |
snmp_asn1_enc_length(p, ofs, m_stat->rhl.erridxlen); |
| 536 |
ofs += m_stat->rhl.erridxlenlen; |
| 537 |
snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.erridxlen, m_stat->error_index); |
| 538 |
ofs += m_stat->rhl.erridxlen; |
| 539 |
|
| 540 |
return ofs; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Encodes trap header from head to tail. |
| 545 |
*/ |
| 546 |
static u16_t |
| 547 |
snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p) |
| 548 |
{ |
| 549 |
u16_t ofs; |
| 550 |
|
| 551 |
ofs = 0; |
| 552 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)); |
| 553 |
ofs += 1; |
| 554 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.seqlen); |
| 555 |
ofs += m_trap->thl.seqlenlen; |
| 556 |
|
| 557 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)); |
| 558 |
ofs += 1; |
| 559 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.verlen); |
| 560 |
ofs += m_trap->thl.verlenlen; |
| 561 |
snmp_asn1_enc_s32t(p, ofs, m_trap->thl.verlen, snmp_version); |
| 562 |
ofs += m_trap->thl.verlen; |
| 563 |
|
| 564 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR)); |
| 565 |
ofs += 1; |
| 566 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.comlen); |
| 567 |
ofs += m_trap->thl.comlenlen; |
| 568 |
snmp_asn1_enc_raw(p, ofs, m_trap->thl.comlen, (u8_t *)&snmp_publiccommunity[0]); |
| 569 |
ofs += m_trap->thl.comlen; |
| 570 |
|
| 571 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_TRAP)); |
| 572 |
ofs += 1; |
| 573 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.pdulen); |
| 574 |
ofs += m_trap->thl.pdulenlen; |
| 575 |
|
| 576 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID)); |
| 577 |
ofs += 1; |
| 578 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.eidlen); |
| 579 |
ofs += m_trap->thl.eidlenlen; |
| 580 |
snmp_asn1_enc_oid(p, ofs, m_trap->enterprise->len, &m_trap->enterprise->id[0]); |
| 581 |
ofs += m_trap->thl.eidlen; |
| 582 |
|
| 583 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR)); |
| 584 |
ofs += 1; |
| 585 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.aaddrlen); |
| 586 |
ofs += m_trap->thl.aaddrlenlen; |
| 587 |
snmp_asn1_enc_raw(p, ofs, m_trap->thl.aaddrlen, &m_trap->sip_raw[0]); |
| 588 |
ofs += m_trap->thl.aaddrlen; |
| 589 |
|
| 590 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)); |
| 591 |
ofs += 1; |
| 592 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.gtrplen); |
| 593 |
ofs += m_trap->thl.gtrplenlen; |
| 594 |
snmp_asn1_enc_u32t(p, ofs, m_trap->thl.gtrplen, m_trap->gen_trap); |
| 595 |
ofs += m_trap->thl.gtrplen; |
| 596 |
|
| 597 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG)); |
| 598 |
ofs += 1; |
| 599 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.strplen); |
| 600 |
ofs += m_trap->thl.strplenlen; |
| 601 |
snmp_asn1_enc_u32t(p, ofs, m_trap->thl.strplen, m_trap->spc_trap); |
| 602 |
ofs += m_trap->thl.strplen; |
| 603 |
|
| 604 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS)); |
| 605 |
ofs += 1; |
| 606 |
snmp_asn1_enc_length(p, ofs, m_trap->thl.tslen); |
| 607 |
ofs += m_trap->thl.tslenlen; |
| 608 |
snmp_asn1_enc_u32t(p, ofs, m_trap->thl.tslen, m_trap->ts); |
| 609 |
ofs += m_trap->thl.tslen; |
| 610 |
|
| 611 |
return ofs; |
| 612 |
} |
| 613 |
|
| 614 |
/** |
| 615 |
* Encodes varbind list from head to tail. |
| 616 |
*/ |
| 617 |
static u16_t |
| 618 |
snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs) |
| 619 |
{ |
| 620 |
struct snmp_varbind *vb; |
| 621 |
s32_t *sint_ptr; |
| 622 |
u32_t *uint_ptr; |
| 623 |
u8_t *raw_ptr; |
| 624 |
|
| 625 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)); |
| 626 |
ofs += 1; |
| 627 |
snmp_asn1_enc_length(p, ofs, root->seqlen); |
| 628 |
ofs += root->seqlenlen; |
| 629 |
|
| 630 |
vb = root->head; |
| 631 |
while ( vb != NULL ) |
| 632 |
{ |
| 633 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)); |
| 634 |
ofs += 1; |
| 635 |
snmp_asn1_enc_length(p, ofs, vb->seqlen); |
| 636 |
ofs += vb->seqlenlen; |
| 637 |
|
| 638 |
snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID)); |
| 639 |
ofs += 1; |
| 640 |
snmp_asn1_enc_length(p, ofs, vb->olen); |
| 641 |
ofs += vb->olenlen; |
| 642 |
snmp_asn1_enc_oid(p, ofs, vb->ident_len, &vb->ident[0]); |
| 643 |
ofs += vb->olen; |
| 644 |
|
| 645 |
snmp_asn1_enc_type(p, ofs, vb->value_type); |
| 646 |
ofs += 1; |
| 647 |
snmp_asn1_enc_length(p, ofs, vb->vlen); |
| 648 |
ofs += vb->vlenlen; |
| 649 |
|
| 650 |
switch (vb->value_type) |
| 651 |
{ |
| 652 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG): |
| 653 |
sint_ptr = (s32_t*)vb->value; |
| 654 |
snmp_asn1_enc_s32t(p, ofs, vb->vlen, *sint_ptr); |
| 655 |
break; |
| 656 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER): |
| 657 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE): |
| 658 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS): |
| 659 |
uint_ptr = (u32_t*)vb->value; |
| 660 |
snmp_asn1_enc_u32t(p, ofs, vb->vlen, *uint_ptr); |
| 661 |
break; |
| 662 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR): |
| 663 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR): |
| 664 |
case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE): |
| 665 |
raw_ptr = (u8_t*)vb->value; |
| 666 |
snmp_asn1_enc_raw(p, ofs, vb->vlen, raw_ptr); |
| 667 |
break; |
| 668 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL): |
| 669 |
break; |
| 670 |
case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID): |
| 671 |
sint_ptr = (s32_t*)vb->value; |
| 672 |
snmp_asn1_enc_oid(p, ofs, vb->value_len / sizeof(s32_t), sint_ptr); |
| 673 |
break; |
| 674 |
default: |
| 675 |
/* unsupported type */ |
| 676 |
break; |
| 677 |
}; |
| 678 |
ofs += vb->vlen; |
| 679 |
vb = vb->next; |
| 680 |
} |
| 681 |
return ofs; |
| 682 |
} |
| 683 |
|
| 684 |
#endif /* LWIP_SNMP */ |