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 /* The listen port of the SNMP agent. Clients have to make their requests to
61    this port. Most standard clients won't work if you change this! */
62 #ifndef SNMP_IN_PORT
63 #define SNMP_IN_PORT 161
64 #endif
65 /* The remote port the SNMP agent sends traps to. Most standard trap sinks won't
66    work if you change this! */
67 #ifndef SNMP_TRAP_PORT
68 #define SNMP_TRAP_PORT 162
69 #endif
70 
71 /* version defines used in PDU */
72 #define SNMP_VERSION_1  0
73 #define SNMP_VERSION_2c 1
74 #define SNMP_VERSION_3  3
75 
76 struct snmp_varbind_enumerator
77 {
78   struct snmp_pbuf_stream pbuf_stream;
79   u16_t varbind_count;
80 };
81 
82 typedef enum {
83   SNMP_VB_ENUMERATOR_ERR_OK            = 0,
84   SNMP_VB_ENUMERATOR_ERR_EOVB          = 1,
85   SNMP_VB_ENUMERATOR_ERR_ASN1ERROR     = 2,
86   SNMP_VB_ENUMERATOR_ERR_INVALIDLENGTH = 3
87 } snmp_vb_enumerator_err_t;
88 
89 void snmp_vb_enumerator_init(struct snmp_varbind_enumerator* enumerator, struct pbuf* p, u16_t offset, u16_t length);
90 snmp_vb_enumerator_err_t snmp_vb_enumerator_get_next(struct snmp_varbind_enumerator* enumerator, struct snmp_varbind* varbind);
91 
92 struct snmp_request
93 {
94   /* Communication handle */
95   void *handle;
96   /* source IP address */
97   const ip_addr_t *source_ip;
98   /* source UDP port */
99   u16_t source_port;
100   /* incoming snmp version */
101   u8_t version;
102   /* community name (zero terminated) */
103   u8_t community[SNMP_MAX_COMMUNITY_STR_LEN + 1];
104   /* community string length (exclusive zero term) */
105   u16_t community_strlen;
106   /* request type */
107   u8_t request_type;
108   /* request ID */
109   s32_t request_id;
110   /* error status */
111   s32_t error_status;
112   /* error index */
113   s32_t error_index;
114   /* non-repeaters (getBulkRequest (SNMPv2c)) */
115   s32_t non_repeaters;
116   /* max-repetitions (getBulkRequest (SNMPv2c)) */
117   s32_t max_repetitions;
118 
119 #if LWIP_SNMP_V3
120   s32_t msg_id;
121   s32_t msg_max_size;
122   u8_t  msg_flags;
123   s32_t msg_security_model;
124   u8_t  msg_authoritative_engine_id[SNMP_V3_MAX_ENGINE_ID_LENGTH];
125   u8_t  msg_authoritative_engine_id_len;
126   s32_t msg_authoritative_engine_boots;
127   s32_t msg_authoritative_engine_time;
128   u8_t  msg_user_name[SNMP_V3_MAX_USER_LENGTH];
129   u8_t  msg_user_name_len;
130   u8_t  msg_authentication_parameters[SNMP_V3_MAX_AUTH_PARAM_LENGTH];
131   u8_t  msg_privacy_parameters[SNMP_V3_MAX_PRIV_PARAM_LENGTH];
132   u8_t  context_engine_id[SNMP_V3_MAX_ENGINE_ID_LENGTH];
133   u8_t  context_engine_id_len;
134   u8_t  context_name[SNMP_V3_MAX_ENGINE_ID_LENGTH];
135   u8_t  context_name_len;
136 #endif
137 
138   struct pbuf *inbound_pbuf;
139   struct snmp_varbind_enumerator inbound_varbind_enumerator;
140   u16_t inbound_varbind_offset;
141   u16_t inbound_varbind_len;
142   u16_t inbound_padding_len;
143 
144   struct pbuf *outbound_pbuf;
145   struct snmp_pbuf_stream outbound_pbuf_stream;
146   u16_t outbound_pdu_offset;
147   u16_t outbound_error_status_offset;
148   u16_t outbound_error_index_offset;
149   u16_t outbound_varbind_offset;
150 #if LWIP_SNMP_V3
151   u16_t outbound_msg_global_data_offset;
152   u16_t outbound_msg_global_data_end;
153   u16_t outbound_msg_security_parameters_str_offset;
154   u16_t outbound_msg_security_parameters_seq_offset;
155   u16_t outbound_msg_security_parameters_end;
156   u16_t outbound_msg_authentication_parameters_offset;
157   u16_t outbound_scoped_pdu_seq_offset;
158   u16_t outbound_scoped_pdu_string_offset;
159 #endif
160 
161   u8_t value_buffer[SNMP_MAX_VALUE_SIZE];
162 };
163 
164 /** A helper struct keeping length information about varbinds */
165 struct snmp_varbind_len
166 {
167   u8_t  vb_len_len;
168   u16_t vb_value_len;
169   u8_t  oid_len_len;
170   u16_t oid_value_len;
171   u8_t  value_len_len;
172   u16_t value_value_len;
173 };
174 
175 /** Agent community string */
176 extern const char *snmp_community;
177 /** Agent community string for write access */
178 extern const char *snmp_community_write;
179 /** handle for sending traps */
180 extern void* snmp_traps_handle;
181 
182 void snmp_receive(void *handle, struct pbuf *p, const ip_addr_t *source_ip, u16_t port);
183 err_t snmp_sendto(void *handle, struct pbuf *p, const ip_addr_t *dst, u16_t port);
184 u8_t snmp_get_local_ip_for_dst(void* handle, const ip_addr_t *dst, ip_addr_t *result);
185 err_t snmp_varbind_length(struct snmp_varbind *varbind, struct snmp_varbind_len *len);
186 err_t snmp_append_outbound_varbind(struct snmp_pbuf_stream *pbuf_stream, struct snmp_varbind* varbind);
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
192 #endif /* LWIP_SNMP */
193 
194 #endif /* LWIP_HDR_APPS_SNMP_MSG_H */
195