1 /**
2  * @file
3  * SNMP Agent message handling structures (internal API, do not use in client code).
4  */
5 
6 /*
7  * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
8  * Copyright (c) 2016 Elias Oenal.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * Author: Christiaan Simons <christiaan.simons@axon.tv>
34  *         Martin Hentschel <info@cl-soft.de>
35  *         Elias Oenal <lwip@eliasoenal.com>
36  */
37 
38 #ifndef LWIP_HDR_APPS_SNMP_MSG_H
39 #define LWIP_HDR_APPS_SNMP_MSG_H
40 
41 #include "lwip/apps/snmp_opts.h"
42 
43 #if LWIP_SNMP
44 
45 #include "lwip/apps/snmp.h"
46 #include "lwip/apps/snmp_core.h"
47 #include "snmp_pbuf_stream.h"
48 #include "lwip/ip_addr.h"
49 #include "lwip/err.h"
50 
51 #if LWIP_SNMP_V3
52 #include "snmpv3_priv.h"
53 #endif
54 
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /* version defines used in PDU */
61 #define SNMP_VERSION_1  0
62 #define SNMP_VERSION_2c 1
63 #define SNMP_VERSION_3  3
64 
65 struct snmp_varbind_enumerator {
66   struct snmp_pbuf_stream pbuf_stream;
67   u16_t varbind_count;
68 };
69 
70 typedef enum {
71   SNMP_VB_ENUMERATOR_ERR_OK            = 0,
72   SNMP_VB_ENUMERATOR_ERR_EOVB          = 1,
73   SNMP_VB_ENUMERATOR_ERR_ASN1ERROR     = 2,
74   SNMP_VB_ENUMERATOR_ERR_INVALIDLENGTH = 3
75 } snmp_vb_enumerator_err_t;
76 
77 void snmp_vb_enumerator_init(struct snmp_varbind_enumerator *enumerator, struct pbuf *p, u16_t offset, u16_t length);
78 snmp_vb_enumerator_err_t snmp_vb_enumerator_get_next(struct snmp_varbind_enumerator *enumerator, struct snmp_varbind *varbind);
79 
80 struct snmp_request {
81   /* Communication handle */
82   void *handle;
83   /* source IP address */
84   const ip_addr_t *source_ip;
85   /* source UDP port */
86   u16_t source_port;
87   /* incoming snmp version */
88   u8_t version;
89   /* community name (zero terminated) */
90   u8_t community[SNMP_MAX_COMMUNITY_STR_LEN + 1];
91   /* community string length (exclusive zero term) */
92   u16_t community_strlen;
93   /* request type */
94   u8_t request_type;
95   /* request ID */
96   s32_t request_id;
97   /* error status */
98   s32_t error_status;
99   /* error index */
100   s32_t error_index;
101   /* non-repeaters (getBulkRequest (SNMPv2c)) */
102   s32_t non_repeaters;
103   /* max-repetitions (getBulkRequest (SNMPv2c)) */
104   s32_t max_repetitions;
105 
106   /* Usually response-pdu (2). When snmpv3 errors are detected report-pdu(8) */
107   u8_t request_out_type;
108 
109 #if LWIP_SNMP_V3
110   s32_t msg_id;
111   s32_t msg_max_size;
112   u8_t  msg_flags;
113   s32_t msg_security_model;
114   u8_t  msg_authoritative_engine_id[SNMP_V3_MAX_ENGINE_ID_LENGTH];
115   u8_t  msg_authoritative_engine_id_len;
116   s32_t msg_authoritative_engine_boots;
117   s32_t msg_authoritative_engine_time;
118   u8_t  msg_user_name[SNMP_V3_MAX_USER_LENGTH];
119   u8_t  msg_user_name_len;
120   u8_t  msg_authentication_parameters[SNMP_V3_MAX_AUTH_PARAM_LENGTH];
121   u8_t  msg_authentication_parameters_len;
122   u8_t  msg_privacy_parameters[SNMP_V3_MAX_PRIV_PARAM_LENGTH];
123   u8_t  msg_privacy_parameters_len;
124   u8_t  context_engine_id[SNMP_V3_MAX_ENGINE_ID_LENGTH];
125   u8_t  context_engine_id_len;
126   u8_t  context_name[SNMP_V3_MAX_ENGINE_ID_LENGTH];
127   u8_t  context_name_len;
128 #endif
129 
130   struct pbuf *inbound_pbuf;
131   struct snmp_varbind_enumerator inbound_varbind_enumerator;
132   u16_t inbound_varbind_offset;
133   u16_t inbound_varbind_len;
134   u16_t inbound_padding_len;
135 
136   struct pbuf *outbound_pbuf;
137   struct snmp_pbuf_stream outbound_pbuf_stream;
138   u16_t outbound_pdu_offset;
139   u16_t outbound_error_status_offset;
140   u16_t outbound_error_index_offset;
141   u16_t outbound_varbind_offset;
142 #if LWIP_SNMP_V3
143   u16_t outbound_msg_global_data_offset;
144   u16_t outbound_msg_global_data_end;
145   u16_t outbound_msg_security_parameters_str_offset;
146   u16_t outbound_msg_security_parameters_seq_offset;
147   u16_t outbound_msg_security_parameters_end;
148   u16_t outbound_msg_authentication_parameters_offset;
149   u16_t outbound_scoped_pdu_seq_offset;
150   u16_t outbound_scoped_pdu_string_offset;
151 #endif
152 
153   u8_t value_buffer[SNMP_MAX_VALUE_SIZE];
154 };
155 
156 /** A helper struct keeping length information about varbinds */
157 struct snmp_varbind_len {
158   u8_t  vb_len_len;
159   u16_t vb_value_len;
160   u8_t  oid_len_len;
161   u16_t oid_value_len;
162   u8_t  value_len_len;
163   u16_t value_value_len;
164 };
165 
166 /** Agent community string */
167 extern const char *snmp_community;
168 /** Agent community string for write access */
169 extern const char *snmp_community_write;
170 /** handle for sending traps */
171 extern void *snmp_traps_handle;
172 
173 void snmp_receive(void *handle, struct pbuf *p, const ip_addr_t *source_ip, u16_t port);
174 err_t snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port);
175 u8_t snmp_get_local_ip_for_dst(void *handle, const ip_addr_t *dst, ip_addr_t *result);
176 err_t snmp_varbind_length(struct snmp_varbind *varbind, struct snmp_varbind_len *len);
177 err_t snmp_append_outbound_varbind(struct snmp_pbuf_stream *pbuf_stream, struct snmp_varbind *varbind);
178 
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 #endif /* LWIP_SNMP */
184 
185 #endif /* LWIP_HDR_APPS_SNMP_MSG_H */
186