uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
snmp_msg.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * Author: Christiaan Simons <christiaan.simons@axon.tv>
33  */
34 
35 #ifndef __LWIP_SNMP_MSG_H__
36 #define __LWIP_SNMP_MSG_H__
37 
38 #include "lwip/opt.h"
39 #include "lwip/snmp.h"
40 #include "lwip/snmp_structs.h"
41 #include "lwip/ip_addr.h"
42 #include "lwip/err.h"
43 
44 #if LWIP_SNMP
45 
46 #if SNMP_PRIVATE_MIB
47 /* When using a private MIB, you have to create a file 'private_mib.h' that contains
48  * a 'struct mib_array_node mib_private' which contains your MIB. */
49 #include "private_mib.h"
50 #endif
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /* The listen port of the SNMP agent. Clients have to make their requests to
57  this port. Most standard clients won't work if you change this! */
58 #ifndef SNMP_IN_PORT
59 #define SNMP_IN_PORT 161
60 #endif
61 /* The remote port the SNMP agent sends traps to. Most standard trap sinks won't
62  work if you change this! */
63 #ifndef SNMP_TRAP_PORT
64 #define SNMP_TRAP_PORT 162
65 #endif
66 
67 #define SNMP_ES_NOERROR 0
68 #define SNMP_ES_TOOBIG 1
69 #define SNMP_ES_NOSUCHNAME 2
70 #define SNMP_ES_BADVALUE 3
71 #define SNMP_ES_READONLY 4
72 #define SNMP_ES_GENERROR 5
73 
74 #define SNMP_GENTRAP_COLDSTART 0
75 #define SNMP_GENTRAP_WARMSTART 1
76 #define SNMP_GENTRAP_AUTHFAIL 4
77 #define SNMP_GENTRAP_ENTERPRISESPC 6
78 
79 struct snmp_varbind
80 {
81  /* next pointer, NULL for last in list */
82  struct snmp_varbind *next;
83  /* previous pointer, NULL for first in list */
84  struct snmp_varbind *prev;
85 
86  /* object identifier length (in s32_t) */
87  u8_t ident_len;
88  /* object identifier array */
89  s32_t *ident;
90 
91  /* object value ASN1 type */
92  u8_t value_type;
93  /* object value length (in u8_t) */
94  u8_t value_len;
95  /* object value */
96  void *value;
97 
98  /* encoding varbind seq length length */
99  u8_t seqlenlen;
100  /* encoding object identifier length length */
101  u8_t olenlen;
102  /* encoding object value length length */
103  u8_t vlenlen;
104  /* encoding varbind seq length */
105  u16_t seqlen;
106  /* encoding object identifier length */
107  u16_t olen;
108  /* encoding object value length */
109  u16_t vlen;
110 };
111 
112 struct snmp_varbind_root
113 {
114  struct snmp_varbind *head;
115  struct snmp_varbind *tail;
116  /* number of variable bindings in list */
117  u8_t count;
118  /* encoding varbind-list seq length length */
119  u8_t seqlenlen;
120  /* encoding varbind-list seq length */
121  u16_t seqlen;
122 };
123 
125 struct snmp_resp_header_lengths
126 {
127  /* encoding error-index length length */
128  u8_t erridxlenlen;
129  /* encoding error-status length length */
130  u8_t errstatlenlen;
131  /* encoding request id length length */
132  u8_t ridlenlen;
133  /* encoding pdu length length */
134  u8_t pdulenlen;
135  /* encoding community length length */
136  u8_t comlenlen;
137  /* encoding version length length */
138  u8_t verlenlen;
139  /* encoding sequence length length */
140  u8_t seqlenlen;
141 
142  /* encoding error-index length */
143  u16_t erridxlen;
144  /* encoding error-status length */
145  u16_t errstatlen;
146  /* encoding request id length */
147  u16_t ridlen;
148  /* encoding pdu length */
149  u16_t pdulen;
150  /* encoding community length */
151  u16_t comlen;
152  /* encoding version length */
153  u16_t verlen;
154  /* encoding sequence length */
155  u16_t seqlen;
156 };
157 
159 struct snmp_trap_header_lengths
160 {
161  /* encoding timestamp length length */
162  u8_t tslenlen;
163  /* encoding specific-trap length length */
164  u8_t strplenlen;
165  /* encoding generic-trap length length */
166  u8_t gtrplenlen;
167  /* encoding agent-addr length length */
168  u8_t aaddrlenlen;
169  /* encoding enterprise-id length length */
170  u8_t eidlenlen;
171  /* encoding pdu length length */
172  u8_t pdulenlen;
173  /* encoding community length length */
174  u8_t comlenlen;
175  /* encoding version length length */
176  u8_t verlenlen;
177  /* encoding sequence length length */
178  u8_t seqlenlen;
179 
180  /* encoding timestamp length */
181  u16_t tslen;
182  /* encoding specific-trap length */
183  u16_t strplen;
184  /* encoding generic-trap length */
185  u16_t gtrplen;
186  /* encoding agent-addr length */
187  u16_t aaddrlen;
188  /* encoding enterprise-id length */
189  u16_t eidlen;
190  /* encoding pdu length */
191  u16_t pdulen;
192  /* encoding community length */
193  u16_t comlen;
194  /* encoding version length */
195  u16_t verlen;
196  /* encoding sequence length */
197  u16_t seqlen;
198 };
199 
200 /* Accepting new SNMP messages. */
201 #define SNMP_MSG_EMPTY 0
202 /* Search for matching object for variable binding. */
203 #define SNMP_MSG_SEARCH_OBJ 1
204 /* Perform SNMP operation on in-memory object.
205  Pass-through states, for symmetry only. */
206 #define SNMP_MSG_INTERNAL_GET_OBJDEF 2
207 #define SNMP_MSG_INTERNAL_GET_VALUE 3
208 #define SNMP_MSG_INTERNAL_SET_TEST 4
209 #define SNMP_MSG_INTERNAL_GET_OBJDEF_S 5
210 #define SNMP_MSG_INTERNAL_SET_VALUE 6
211 /* Perform SNMP operation on object located externally.
212  In theory this could be used for building a proxy agent.
213  Practical use is for an enterprise spc. app. gateway. */
214 #define SNMP_MSG_EXTERNAL_GET_OBJDEF 7
215 #define SNMP_MSG_EXTERNAL_GET_VALUE 8
216 #define SNMP_MSG_EXTERNAL_SET_TEST 9
217 #define SNMP_MSG_EXTERNAL_GET_OBJDEF_S 10
218 #define SNMP_MSG_EXTERNAL_SET_VALUE 11
219 
220 #define SNMP_COMMUNITY_STR_LEN 64
221 struct snmp_msg_pstat
222 {
223  /* lwIP local port (161) binding */
224  struct udp_pcb *pcb;
225  /* source IP address */
226  ip_addr_t sip;
227  /* source UDP port */
228  u16_t sp;
229  /* request type */
230  u8_t rt;
231  /* request ID */
232  s32_t rid;
233  /* error status */
234  s32_t error_status;
235  /* error index */
236  s32_t error_index;
237  /* community name (zero terminated) */
238  u8_t community[SNMP_COMMUNITY_STR_LEN + 1];
239  /* community string length (exclusive zero term) */
240  u8_t com_strlen;
241  /* one out of MSG_EMPTY, MSG_DEMUX, MSG_INTERNAL, MSG_EXTERNAL_x */
242  u8_t state;
243  /* saved arguments for MSG_EXTERNAL_x */
244  struct mib_external_node *ext_mib_node;
245  struct snmp_name_ptr ext_name_ptr;
246  struct obj_def ext_object_def;
247  struct snmp_obj_id ext_oid;
248  /* index into input variable binding list */
249  u8_t vb_idx;
250  /* ptr into input variable binding list */
251  struct snmp_varbind *vb_ptr;
252  /* list of variable bindings from input */
253  struct snmp_varbind_root invb;
254  /* list of variable bindings to output */
255  struct snmp_varbind_root outvb;
256  /* output response lengths used in ASN encoding */
257  struct snmp_resp_header_lengths rhl;
258 };
259 
260 struct snmp_msg_trap
261 {
262  /* lwIP local port (161) binding */
263  struct udp_pcb *pcb;
264  /* destination IP address in network order */
265  ip_addr_t dip;
266 
267  /* source enterprise ID (sysObjectID) */
268  struct snmp_obj_id *enterprise;
269  /* source IP address, raw network order format */
270  u8_t sip_raw[4];
271  /* generic trap code */
272  u32_t gen_trap;
273  /* specific trap code */
274  u32_t spc_trap;
275  /* timestamp */
276  u32_t ts;
277  /* list of variable bindings to output */
278  struct snmp_varbind_root outvb;
279  /* output trap lengths used in ASN encoding */
280  struct snmp_trap_header_lengths thl;
281 };
282 
284 extern const s32_t snmp_version;
286 extern const char snmp_publiccommunity[7];
287 
288 extern struct snmp_msg_trap trap_msg;
289 
291 void snmp_init(void);
292 void snmp_trap_dst_enable(u8_t dst_idx, u8_t enable);
293 void snmp_trap_dst_ip_set(u8_t dst_idx, ip_addr_t *dst);
294 
296 struct snmp_varbind* snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len);
297 void snmp_varbind_free(struct snmp_varbind *vb);
298 void snmp_varbind_list_free(struct snmp_varbind_root *root);
299 void snmp_varbind_tail_add(struct snmp_varbind_root *root, struct snmp_varbind *vb);
300 struct snmp_varbind* snmp_varbind_tail_remove(struct snmp_varbind_root *root);
301 
303 void snmp_msg_event(u8_t request_id);
304 err_t snmp_send_response(struct snmp_msg_pstat *m_stat);
305 err_t snmp_send_trap(s8_t generic_trap, struct snmp_obj_id *eoid, s32_t specific_trap);
306 void snmp_coldstart_trap(void);
307 void snmp_authfail_trap(void);
308 
309 #ifdef __cplusplus
310 }
311 #endif
312 
313 #endif /* LWIP_SNMP */
314 
315 #endif /* __LWIP_SNMP_MSG_H__ */