]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/lwip/lib/contrib/src/core/snmp/msg_out.c
Update
[l4.git] / l4 / pkg / lwip / lib / contrib / src / core / snmp / msg_out.c
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 <christiaan.simons@axon.tv>
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_mib2.h"
53 #include "lwip/snmp_asn1.h"
54 #include "lwip/snmp_msg.h"
55 #include "lwip/sys.h"
56
57 #include <string.h>
58
59 #if !SNMP_COMMUNITY_EXT
60 #define snmp_community_trap snmp_community
61 #endif
62
63 struct snmp_trap_dst
64 {
65   /* destination IP address in network order */
66   ip_addr_t dip;
67   /* set to 0 when disabled, >0 when enabled */
68   u8_t enable;
69 };
70 struct snmp_trap_dst trap_dst[SNMP_TRAP_DESTINATIONS];
71
72 /** TRAP message structure */
73 struct snmp_msg_trap trap_msg;
74
75 static u16_t snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len);
76 static u16_t snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len);
77 static u16_t snmp_varbind_list_sum(struct snmp_varbind_root *root);
78
79 static u16_t snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p);
80 static u16_t snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p);
81 static u16_t snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs);
82
83 /**
84  * Sets enable switch for this trap destination.
85  * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
86  * @param enable switch if 0 destination is disabled >0 enabled.
87  */
88 void
89 snmp_trap_dst_enable(u8_t dst_idx, u8_t enable)
90 {
91   if (dst_idx < SNMP_TRAP_DESTINATIONS) {
92     trap_dst[dst_idx].enable = enable;
93   }
94 }
95
96 /**
97  * Sets IPv4 address for this trap destination.
98  * @param dst_idx index in 0 .. SNMP_TRAP_DESTINATIONS-1
99  * @param dst IPv4 address in host order.
100  */
101 void
102 snmp_trap_dst_ip_set(u8_t dst_idx, const ip_addr_t *dst)
103 {
104   if (dst_idx < SNMP_TRAP_DESTINATIONS) {
105     ip_addr_set(&trap_dst[dst_idx].dip, dst);
106   }
107 }
108
109 /**
110  * Sends a 'getresponse' message to the request originator.
111  *
112  * @param m_stat points to the current message request state source
113  * @return ERR_OK when success, ERR_MEM if we're out of memory
114  *
115  * @note the caller is responsible for filling in outvb in the m_stat
116  * and provide error-status and index (except for tooBig errors) ...
117  */
118 err_t
119 snmp_send_response(struct snmp_msg_pstat *m_stat)
120 {
121   struct snmp_varbind_root emptyvb = {NULL, NULL, 0, 0, 0};
122   struct pbuf *p;
123   u16_t tot_len;
124   err_t err;
125
126   /* pass 0, calculate length fields */
127   tot_len = snmp_varbind_list_sum(&m_stat->outvb);
128   tot_len = snmp_resp_header_sum(m_stat, tot_len);
129
130   /* try allocating pbuf(s) for complete response */
131   p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
132   if (p == NULL) {
133     LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() tooBig\n"));
134
135     /* can't construct reply, return error-status tooBig */
136     m_stat->error_status = SNMP_ES_TOOBIG;
137     m_stat->error_index = 0;
138     /* pass 0, recalculate lengths, for empty varbind-list */
139     tot_len = snmp_varbind_list_sum(&emptyvb);
140     tot_len = snmp_resp_header_sum(m_stat, tot_len);
141     /* retry allocation once for header and empty varbind-list */
142     p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
143   }
144   if (p != NULL) {
145     /* first pbuf alloc try or retry alloc success */
146     u16_t ofs;
147
148     LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_snd_response() p != NULL\n"));
149
150     /* pass 1, size error, encode packet ino the pbuf(s) */
151     ofs = snmp_resp_header_enc(m_stat, p);
152     snmp_varbind_list_enc(&m_stat->outvb, p, ofs);
153
154     switch (m_stat->error_status) {
155     case SNMP_ES_NOERROR:
156       /* nothing to do */
157       break;
158     case SNMP_ES_TOOBIG:
159       mib2_inc_snmpouttoobigs();
160       break;
161     case SNMP_ES_NOSUCHNAME:
162       mib2_inc_snmpoutnosuchnames();
163       break;
164     case SNMP_ES_BADVALUE:
165       mib2_inc_snmpoutbadvalues();
166       break;
167     case SNMP_ES_GENERROR:
168       mib2_inc_snmpoutgenerrs();
169       break;
170     default:
171       LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_send_response(): unknown error_status: %d\n", (int)m_stat->error_status));
172       break;
173     }
174     mib2_inc_snmpoutgetresponses();
175     mib2_inc_snmpoutpkts();
176
177     /** @todo do we need separate rx and tx pcbs for threaded case? */
178     /** connect to the originating source */
179     udp_connect(m_stat->pcb, &m_stat->sip, m_stat->sp);
180     err = udp_send(m_stat->pcb, p);
181     if (err == ERR_MEM) {
182       /** @todo release some memory, retry and return tooBig? tooMuchHassle? */
183       err = ERR_MEM;
184     } else {
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   } else {
194     /* first pbuf alloc try or retry alloc failed
195        very low on memory, couldn't return tooBig */
196     return ERR_MEM;
197   }
198 }
199
200
201 /**
202  * Sends an generic or enterprise specific trap message.
203  *
204  * @param generic_trap is the trap code
205  * @param eoid points to enterprise object identifier
206  * @param specific_trap used for enterprise traps when generic_trap == 6
207  * @return ERR_OK when success, ERR_MEM if we're out of memory
208  *
209  * @note the caller is responsible for filling in outvb in the trap_msg
210  * @note the use of the enterprise identifier field
211  * is per RFC1215.
212  * Use .iso.org.dod.internet.mgmt.mib-2.snmp for generic traps
213  * and .iso.org.dod.internet.private.enterprises.yourenterprise
214  * (sysObjectID) for specific traps.
215  */
216 err_t
217 snmp_send_trap(s8_t generic_trap, const struct snmp_obj_id *eoid, s32_t specific_trap)
218 {
219   struct snmp_trap_dst *td;
220   struct netif *dst_if;
221   const ip_addr_t* dst_ip;
222   struct pbuf *p;
223   u16_t i,tot_len;
224   err_t err = ERR_OK;
225
226   for (i = 0, td = &trap_dst[0]; i < SNMP_TRAP_DESTINATIONS; i++, td++) {
227     if ((td->enable != 0) && !ip_addr_isany(&td->dip)) {
228       /* network order trap destination */
229       ip_addr_copy(trap_msg.dip, td->dip);
230       /* lookup current source address for this dst */
231       ip_route_get_local_ip(PCB_ISIPV6(trap_msg.pcb), &trap_msg.pcb->local_ip,
232         &td->dip, dst_if, dst_ip);
233       if ((dst_if != NULL) && (dst_ip != NULL)) {
234         trap_msg.sip_raw_len = (IP_IS_V6_VAL(*dst_ip) ? 16 : 4);
235         memcpy(trap_msg.sip_raw, dst_ip, trap_msg.sip_raw_len);
236         trap_msg.gen_trap = generic_trap;
237         trap_msg.spc_trap = specific_trap;
238         if (generic_trap == SNMP_GENTRAP_ENTERPRISESPC) {
239           /* enterprise-Specific trap */
240           trap_msg.enterprise = eoid;
241         } else {
242           /* generic (MIB-II) trap */
243           mib2_get_snmpgrpid_ptr(&trap_msg.enterprise);
244         }
245
246         MIB2_COPY_SYSUPTIME_TO(&trap_msg.ts);
247
248         /* pass 0, calculate length fields */
249         tot_len = snmp_varbind_list_sum(&trap_msg.outvb);
250         tot_len = snmp_trap_header_sum(&trap_msg, tot_len);
251
252         /* allocate pbuf(s) */
253         p = pbuf_alloc(PBUF_TRANSPORT, tot_len, PBUF_RAM);
254         if (p != NULL) {
255           u16_t ofs;
256
257           /* pass 1, encode packet ino the pbuf(s) */
258           ofs = snmp_trap_header_enc(&trap_msg, p);
259           snmp_varbind_list_enc(&trap_msg.outvb, p, ofs);
260
261           mib2_inc_snmpouttraps();
262           mib2_inc_snmpoutpkts();
263
264           /** send to the TRAP destination */
265           udp_sendto(trap_msg.pcb, p, &trap_msg.dip, SNMP_TRAP_PORT);
266
267           pbuf_free(p);
268         } else {
269           err = ERR_MEM;
270         }
271       } else {
272         /* routing error */
273         err = ERR_RTE;
274       }
275     }
276   }
277   return err;
278 }
279
280 void
281 snmp_coldstart_trap(void)
282 {
283   trap_msg.outvb.head = NULL;
284   trap_msg.outvb.tail = NULL;
285   trap_msg.outvb.count = 0;
286   snmp_send_trap(SNMP_GENTRAP_COLDSTART, NULL, 0);
287 }
288
289 void
290 snmp_authfail_trap(void)
291 {
292   u8_t enable;
293   mib2_get_snmpenableauthentraps(&enable);
294   if (enable == 1) {
295     trap_msg.outvb.head = NULL;
296     trap_msg.outvb.tail = NULL;
297     trap_msg.outvb.count = 0;
298     snmp_send_trap(SNMP_GENTRAP_AUTHFAIL, NULL, 0);
299   }
300 }
301
302 /**
303  * Sums response header field lengths from tail to head and
304  * returns resp_header_lengths for second encoding pass.
305  *
306  * @param vb_len varbind-list length
307  * @param rhl points to returned header lengths
308  * @return the required length for encoding the response header
309  */
310 static u16_t
311 snmp_resp_header_sum(struct snmp_msg_pstat *m_stat, u16_t vb_len)
312 {
313   u16_t tot_len;
314   s32_t snmp_req_ver;
315   struct snmp_resp_header_lengths *rhl;
316
317   rhl = &m_stat->rhl;
318   tot_len = vb_len;
319   snmp_asn1_enc_s32t_cnt(m_stat->error_index, &rhl->erridxlen);
320   snmp_asn1_enc_length_cnt(rhl->erridxlen, &rhl->erridxlenlen);
321   tot_len += 1 + rhl->erridxlenlen + rhl->erridxlen;
322
323   snmp_asn1_enc_s32t_cnt(m_stat->error_status, &rhl->errstatlen);
324   snmp_asn1_enc_length_cnt(rhl->errstatlen, &rhl->errstatlenlen);
325   tot_len += 1 + rhl->errstatlenlen + rhl->errstatlen;
326
327   snmp_asn1_enc_s32t_cnt(m_stat->rid, &rhl->ridlen);
328   snmp_asn1_enc_length_cnt(rhl->ridlen, &rhl->ridlenlen);
329   tot_len += 1 + rhl->ridlenlen + rhl->ridlen;
330
331   rhl->pdulen = tot_len;
332   snmp_asn1_enc_length_cnt(rhl->pdulen, &rhl->pdulenlen);
333   tot_len += 1 + rhl->pdulenlen;
334
335   rhl->comlen = m_stat->com_strlen;
336   snmp_asn1_enc_length_cnt(rhl->comlen, &rhl->comlenlen);
337   tot_len += 1 + rhl->comlenlen + rhl->comlen;
338
339   snmp_req_ver = m_stat->version;
340   snmp_asn1_enc_s32t_cnt(snmp_req_ver, &rhl->verlen);
341   snmp_asn1_enc_length_cnt(rhl->verlen, &rhl->verlenlen);
342   tot_len += 1 + rhl->verlen + rhl->verlenlen;
343
344   rhl->seqlen = tot_len;
345   snmp_asn1_enc_length_cnt(rhl->seqlen, &rhl->seqlenlen);
346   tot_len += 1 + rhl->seqlenlen;
347
348   return tot_len;
349 }
350
351 /**
352  * Sums trap header field lengths from tail to head and
353  * returns trap_header_lengths for second encoding pass.
354  *
355  * @param vb_len varbind-list length
356  * @param thl points to returned header lengths
357  * @return the required length for encoding the trap header
358  */
359 static u16_t
360 snmp_trap_header_sum(struct snmp_msg_trap *m_trap, u16_t vb_len)
361 {
362   u16_t tot_len;
363   struct snmp_trap_header_lengths *thl;
364
365   thl = &m_trap->thl;
366   tot_len = vb_len;
367
368   snmp_asn1_enc_u32t_cnt(m_trap->ts, &thl->tslen);
369   snmp_asn1_enc_length_cnt(thl->tslen, &thl->tslenlen);
370   tot_len += 1 + thl->tslen + thl->tslenlen;
371
372   snmp_asn1_enc_s32t_cnt(m_trap->spc_trap, &thl->strplen);
373   snmp_asn1_enc_length_cnt(thl->strplen, &thl->strplenlen);
374   tot_len += 1 + thl->strplen + thl->strplenlen;
375
376   snmp_asn1_enc_s32t_cnt(m_trap->gen_trap, &thl->gtrplen);
377   snmp_asn1_enc_length_cnt(thl->gtrplen, &thl->gtrplenlen);
378   tot_len += 1 + thl->gtrplen + thl->gtrplenlen;
379
380   thl->aaddrlen = m_trap->sip_raw_len;
381   snmp_asn1_enc_length_cnt(thl->aaddrlen, &thl->aaddrlenlen);
382   tot_len += 1 + thl->aaddrlen + thl->aaddrlenlen;
383
384   snmp_asn1_enc_oid_cnt(m_trap->enterprise->len, &m_trap->enterprise->id[0], &thl->eidlen);
385   snmp_asn1_enc_length_cnt(thl->eidlen, &thl->eidlenlen);
386   tot_len += 1 + thl->eidlen + thl->eidlenlen;
387
388   thl->pdulen = tot_len;
389   snmp_asn1_enc_length_cnt(thl->pdulen, &thl->pdulenlen);
390   tot_len += 1 + thl->pdulenlen;
391
392   thl->comlen = (u16_t)strlen(snmp_community_trap);
393   snmp_asn1_enc_length_cnt(thl->comlen, &thl->comlenlen);
394   tot_len += 1 + thl->comlenlen + thl->comlen;
395
396   snmp_asn1_enc_s32t_cnt(snmp_version, &thl->verlen);
397   snmp_asn1_enc_length_cnt(thl->verlen, &thl->verlenlen);
398   tot_len += 1 + thl->verlen + thl->verlenlen;
399
400   thl->seqlen = tot_len;
401   snmp_asn1_enc_length_cnt(thl->seqlen, &thl->seqlenlen);
402   tot_len += 1 + thl->seqlenlen;
403
404   return tot_len;
405 }
406
407 /**
408  * Sums varbind lengths from tail to head and
409  * annotates lengths in varbind for second encoding pass.
410  *
411  * @param root points to the root of the variable binding list
412  * @return the required length for encoding the variable bindings
413  */
414 static u16_t
415 snmp_varbind_list_sum(struct snmp_varbind_root *root)
416 {
417   struct snmp_varbind *vb;
418   u32_t *uint_ptr;
419   s32_t *sint_ptr;
420   u16_t tot_len;
421
422   tot_len = 0;
423   vb = root->tail;
424   while (vb != NULL) {
425     /* encoded value lenght depends on type */
426     switch (vb->value_type) {
427     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
428       sint_ptr = (s32_t*)vb->value;
429       snmp_asn1_enc_s32t_cnt(*sint_ptr, &vb->vlen);
430       break;
431     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
432     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
433     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
434       uint_ptr = (u32_t*)vb->value;
435       snmp_asn1_enc_u32t_cnt(*uint_ptr, &vb->vlen);
436       break;
437     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
438     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
439     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
440     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
441       vb->vlen = vb->value_len;
442       break;
443     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
444       sint_ptr = (s32_t*)vb->value;
445       snmp_asn1_enc_oid_cnt(vb->value_len / sizeof(s32_t), sint_ptr, &vb->vlen);
446       break;
447     default:
448       /* unsupported type */
449       vb->vlen = 0;
450       break;
451     }
452     /* encoding length of value length field */
453     snmp_asn1_enc_length_cnt(vb->vlen, &vb->vlenlen);
454     snmp_asn1_enc_oid_cnt(vb->ident_len, vb->ident, &vb->olen);
455     snmp_asn1_enc_length_cnt(vb->olen, &vb->olenlen);
456
457     vb->seqlen = 1 + vb->vlenlen + vb->vlen;
458     vb->seqlen += 1 + vb->olenlen + vb->olen;
459     snmp_asn1_enc_length_cnt(vb->seqlen, &vb->seqlenlen);
460
461     /* varbind seq */
462     tot_len += 1 + vb->seqlenlen + vb->seqlen;
463
464     vb = vb->prev;
465   }
466
467   /* varbind-list seq */
468   root->seqlen = tot_len;
469   snmp_asn1_enc_length_cnt(root->seqlen, &root->seqlenlen);
470   tot_len += 1 + root->seqlenlen;
471
472   return tot_len;
473 }
474
475 /**
476  * Encodes response header from head to tail.
477  */
478 static u16_t
479 snmp_resp_header_enc(struct snmp_msg_pstat *m_stat, struct pbuf *p)
480 {
481   u16_t ofs;
482   s32_t snmp_req_ver;
483
484   ofs = 0;
485   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
486   ofs += 1;
487   snmp_asn1_enc_length(p, ofs, m_stat->rhl.seqlen);
488   ofs += m_stat->rhl.seqlenlen;
489
490   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
491   ofs += 1;
492   snmp_asn1_enc_length(p, ofs, m_stat->rhl.verlen);
493   ofs += m_stat->rhl.verlenlen;
494   snmp_req_ver = m_stat->version;
495   snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.verlen, snmp_req_ver);
496   ofs += m_stat->rhl.verlen;
497
498   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
499   ofs += 1;
500   snmp_asn1_enc_length(p, ofs, m_stat->rhl.comlen);
501   ofs += m_stat->rhl.comlenlen;
502   snmp_asn1_enc_raw(p, ofs, m_stat->rhl.comlen, m_stat->community);
503   ofs += m_stat->rhl.comlen;
504
505   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_GET_RESP));
506   ofs += 1;
507   snmp_asn1_enc_length(p, ofs, m_stat->rhl.pdulen);
508   ofs += m_stat->rhl.pdulenlen;
509
510   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
511   ofs += 1;
512   snmp_asn1_enc_length(p, ofs, m_stat->rhl.ridlen);
513   ofs += m_stat->rhl.ridlenlen;
514   snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.ridlen, m_stat->rid);
515   ofs += m_stat->rhl.ridlen;
516
517   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
518   ofs += 1;
519   snmp_asn1_enc_length(p, ofs, m_stat->rhl.errstatlen);
520   ofs += m_stat->rhl.errstatlenlen;
521   snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.errstatlen, m_stat->error_status);
522   ofs += m_stat->rhl.errstatlen;
523
524   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
525   ofs += 1;
526   snmp_asn1_enc_length(p, ofs, m_stat->rhl.erridxlen);
527   ofs += m_stat->rhl.erridxlenlen;
528   snmp_asn1_enc_s32t(p, ofs, m_stat->rhl.erridxlen, m_stat->error_index);
529   ofs += m_stat->rhl.erridxlen;
530
531   return ofs;
532 }
533
534 /**
535  * Encodes trap header from head to tail.
536  */
537 static u16_t
538 snmp_trap_header_enc(struct snmp_msg_trap *m_trap, struct pbuf *p)
539 {
540   u16_t ofs;
541
542   ofs = 0;
543   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
544   ofs += 1;
545   snmp_asn1_enc_length(p, ofs, m_trap->thl.seqlen);
546   ofs += m_trap->thl.seqlenlen;
547
548   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
549   ofs += 1;
550   snmp_asn1_enc_length(p, ofs, m_trap->thl.verlen);
551   ofs += m_trap->thl.verlenlen;
552   snmp_asn1_enc_s32t(p, ofs, m_trap->thl.verlen, snmp_version);
553   ofs += m_trap->thl.verlen;
554
555   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR));
556   ofs += 1;
557   snmp_asn1_enc_length(p, ofs, m_trap->thl.comlen);
558   ofs += m_trap->thl.comlenlen;
559   snmp_asn1_enc_raw(p, ofs, m_trap->thl.comlen, (const u8_t *)&snmp_community_trap[0]);
560   ofs += m_trap->thl.comlen;
561
562   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_CONTXT | SNMP_ASN1_CONSTR | SNMP_ASN1_PDU_TRAP));
563   ofs += 1;
564   snmp_asn1_enc_length(p, ofs, m_trap->thl.pdulen);
565   ofs += m_trap->thl.pdulenlen;
566
567   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
568   ofs += 1;
569   snmp_asn1_enc_length(p, ofs, m_trap->thl.eidlen);
570   ofs += m_trap->thl.eidlenlen;
571   snmp_asn1_enc_oid(p, ofs, m_trap->enterprise->len, &m_trap->enterprise->id[0]);
572   ofs += m_trap->thl.eidlen;
573
574   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR));
575   ofs += 1;
576   snmp_asn1_enc_length(p, ofs, m_trap->thl.aaddrlen);
577   ofs += m_trap->thl.aaddrlenlen;
578   snmp_asn1_enc_raw(p, ofs, m_trap->thl.aaddrlen, &m_trap->sip_raw[0]);
579   ofs += m_trap->thl.aaddrlen;
580
581   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
582   ofs += 1;
583   snmp_asn1_enc_length(p, ofs, m_trap->thl.gtrplen);
584   ofs += m_trap->thl.gtrplenlen;
585   snmp_asn1_enc_u32t(p, ofs, m_trap->thl.gtrplen, m_trap->gen_trap);
586   ofs += m_trap->thl.gtrplen;
587
588   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG));
589   ofs += 1;
590   snmp_asn1_enc_length(p, ofs, m_trap->thl.strplen);
591   ofs += m_trap->thl.strplenlen;
592   snmp_asn1_enc_u32t(p, ofs, m_trap->thl.strplen, m_trap->spc_trap);
593   ofs += m_trap->thl.strplen;
594
595   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS));
596   ofs += 1;
597   snmp_asn1_enc_length(p, ofs, m_trap->thl.tslen);
598   ofs += m_trap->thl.tslenlen;
599   snmp_asn1_enc_u32t(p, ofs, m_trap->thl.tslen, m_trap->ts);
600   ofs += m_trap->thl.tslen;
601
602   return ofs;
603 }
604
605 /**
606  * Encodes varbind list from head to tail.
607  */
608 static u16_t
609 snmp_varbind_list_enc(struct snmp_varbind_root *root, struct pbuf *p, u16_t ofs)
610 {
611   struct snmp_varbind *vb;
612   s32_t *sint_ptr;
613   u32_t *uint_ptr;
614   u8_t *raw_ptr;
615
616   snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
617   ofs += 1;
618   snmp_asn1_enc_length(p, ofs, root->seqlen);
619   ofs += root->seqlenlen;
620
621   vb = root->head;
622   while (vb != NULL) {
623     snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ));
624     ofs += 1;
625     snmp_asn1_enc_length(p, ofs, vb->seqlen);
626     ofs += vb->seqlenlen;
627
628     snmp_asn1_enc_type(p, ofs, (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID));
629     ofs += 1;
630     snmp_asn1_enc_length(p, ofs, vb->olen);
631     ofs += vb->olenlen;
632     snmp_asn1_enc_oid(p, ofs, vb->ident_len, &vb->ident[0]);
633     ofs += vb->olen;
634
635     snmp_asn1_enc_type(p, ofs, vb->value_type);
636     ofs += 1;
637     snmp_asn1_enc_length(p, ofs, vb->vlen);
638     ofs += vb->vlenlen;
639
640     switch (vb->value_type) {
641     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_INTEG):
642       sint_ptr = (s32_t*)vb->value;
643       snmp_asn1_enc_s32t(p, ofs, vb->vlen, *sint_ptr);
644       break;
645     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_COUNTER):
646     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_GAUGE):
647     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_TIMETICKS):
648       uint_ptr = (u32_t*)vb->value;
649       snmp_asn1_enc_u32t(p, ofs, vb->vlen, *uint_ptr);
650       break;
651     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OC_STR):
652     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_IPADDR):
653     case (SNMP_ASN1_APPLIC | SNMP_ASN1_PRIMIT | SNMP_ASN1_OPAQUE):
654       raw_ptr = (u8_t*)vb->value;
655       snmp_asn1_enc_raw(p, ofs, vb->vlen, raw_ptr);
656       break;
657     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_NUL):
658       break;
659     case (SNMP_ASN1_UNIV | SNMP_ASN1_PRIMIT | SNMP_ASN1_OBJ_ID):
660       sint_ptr = (s32_t*)vb->value;
661       snmp_asn1_enc_oid(p, ofs, vb->value_len / sizeof(s32_t), sint_ptr);
662       break;
663     default:
664       /* unsupported type */
665       break;
666     }
667     ofs += vb->vlen;
668     vb = vb->next;
669   }
670   return ofs;
671 }
672
673 #endif /* LWIP_SNMP */