BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file
3 * Management Information Base II (RFC1213) SYSTEM objects and functions.
4 */
5  
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: Dirk Ziegelmeier <dziegel@gmx.de>
33 * Christiaan Simons <christiaan.simons@axon.tv>
34 */
35  
36 #include "lwip/snmp.h"
37 #include "lwip/apps/snmp.h"
38 #include "lwip/apps/snmp_core.h"
39 #include "lwip/apps/snmp_mib2.h"
40 #include "lwip/apps/snmp_table.h"
41 #include "lwip/apps/snmp_scalar.h"
42 #include "lwip/sys.h"
43  
44 #include <string.h>
45  
46 #if LWIP_SNMP && SNMP_LWIP_MIB2
47  
48 #if SNMP_USE_NETCONN
49 #define SYNC_NODE_NAME(node_name) node_name ## _synced
50 #define CREATE_LWIP_SYNC_NODE(oid, node_name) \
51 static const struct snmp_threadsync_node node_name ## _synced = SNMP_CREATE_THREAD_SYNC_NODE(oid, &node_name.node, &snmp_mib2_lwip_locks);
52 #else
53 #define SYNC_NODE_NAME(node_name) node_name
54 #define CREATE_LWIP_SYNC_NODE(oid, node_name)
55 #endif
56  
57 /* --- system .1.3.6.1.2.1.1 ----------------------------------------------------- */
58  
59 /** mib-2.system.sysDescr */
60 static const u8_t sysdescr_default[] = SNMP_LWIP_MIB2_SYSDESC;
61 static const u8_t *sysdescr = sysdescr_default;
62 static const u16_t *sysdescr_len = NULL; /* use strlen for determining len */
63  
64 /** mib-2.system.sysContact */
65 static const u8_t syscontact_default[] = SNMP_LWIP_MIB2_SYSCONTACT;
66 static const u8_t *syscontact = syscontact_default;
67 static const u16_t *syscontact_len = NULL; /* use strlen for determining len */
68 static u8_t *syscontact_wr = NULL; /* if writable, points to the same buffer as syscontact (required for correct constness) */
69 static u16_t *syscontact_wr_len = NULL; /* if writable, points to the same buffer as syscontact_len (required for correct constness) */
70 static u16_t syscontact_bufsize = 0; /* 0=not writable */
71  
72 /** mib-2.system.sysName */
73 static const u8_t sysname_default[] = SNMP_LWIP_MIB2_SYSNAME;
74 static const u8_t *sysname = sysname_default;
75 static const u16_t *sysname_len = NULL; /* use strlen for determining len */
76 static u8_t *sysname_wr = NULL; /* if writable, points to the same buffer as sysname (required for correct constness) */
77 static u16_t *sysname_wr_len = NULL; /* if writable, points to the same buffer as sysname_len (required for correct constness) */
78 static u16_t sysname_bufsize = 0; /* 0=not writable */
79  
80 /** mib-2.system.sysLocation */
81 static const u8_t syslocation_default[] = SNMP_LWIP_MIB2_SYSLOCATION;
82 static const u8_t *syslocation = syslocation_default;
83 static const u16_t *syslocation_len = NULL; /* use strlen for determining len */
84 static u8_t *syslocation_wr = NULL; /* if writable, points to the same buffer as syslocation (required for correct constness) */
85 static u16_t *syslocation_wr_len = NULL; /* if writable, points to the same buffer as syslocation_len (required for correct constness) */
86 static u16_t syslocation_bufsize = 0; /* 0=not writable */
87  
88 /**
89 * @ingroup snmp_mib2
90 * Initializes sysDescr pointers.
91 *
92 * @param str if non-NULL then copy str pointer
93 * @param len points to string length, excluding zero terminator
94 */
95 void
96 snmp_mib2_set_sysdescr(const u8_t *str, const u16_t *len)
97 {
98 if (str != NULL) {
99 sysdescr = str;
100 sysdescr_len = len;
101 }
102 }
103  
104 /**
105 * @ingroup snmp_mib2
106 * Initializes sysContact pointers
107 *
108 * @param ocstr if non-NULL then copy str pointer
109 * @param ocstrlen points to string length, excluding zero terminator.
110 * if set to NULL it is assumed that ocstr is NULL-terminated.
111 * @param bufsize size of the buffer in bytes.
112 * (this is required because the buffer can be overwritten by snmp-set)
113 * if ocstrlen is NULL buffer needs space for terminating 0 byte.
114 * otherwise complete buffer is used for string.
115 * if bufsize is set to 0, the value is regarded as read-only.
116 */
117 void
118 snmp_mib2_set_syscontact(u8_t *ocstr, u16_t *ocstrlen, u16_t bufsize)
119 {
120 if (ocstr != NULL) {
121 syscontact = ocstr;
122 syscontact_wr = ocstr;
123 syscontact_len = ocstrlen;
124 syscontact_wr_len = ocstrlen;
125 syscontact_bufsize = bufsize;
126 }
127 }
128  
129 /**
130 * @ingroup snmp_mib2
131 * see \ref snmp_mib2_set_syscontact but set pointer to readonly memory
132 */
133 void
134 snmp_mib2_set_syscontact_readonly(const u8_t *ocstr, const u16_t *ocstrlen)
135 {
136 if (ocstr != NULL) {
137 syscontact = ocstr;
138 syscontact_len = ocstrlen;
139 syscontact_wr = NULL;
140 syscontact_wr_len = NULL;
141 syscontact_bufsize = 0;
142 }
143 }
144  
145  
146 /**
147 * @ingroup snmp_mib2
148 * Initializes sysName pointers
149 *
150 * @param ocstr if non-NULL then copy str pointer
151 * @param ocstrlen points to string length, excluding zero terminator.
152 * if set to NULL it is assumed that ocstr is NULL-terminated.
153 * @param bufsize size of the buffer in bytes.
154 * (this is required because the buffer can be overwritten by snmp-set)
155 * if ocstrlen is NULL buffer needs space for terminating 0 byte.
156 * otherwise complete buffer is used for string.
157 * if bufsize is set to 0, the value is regarded as read-only.
158 */
159 void
160 snmp_mib2_set_sysname(u8_t *ocstr, u16_t *ocstrlen, u16_t bufsize)
161 {
162 if (ocstr != NULL) {
163 sysname = ocstr;
164 sysname_wr = ocstr;
165 sysname_len = ocstrlen;
166 sysname_wr_len = ocstrlen;
167 sysname_bufsize = bufsize;
168 }
169 }
170  
171 /**
172 * @ingroup snmp_mib2
173 * see \ref snmp_mib2_set_sysname but set pointer to readonly memory
174 */
175 void
176 snmp_mib2_set_sysname_readonly(const u8_t *ocstr, const u16_t *ocstrlen)
177 {
178 if (ocstr != NULL) {
179 sysname = ocstr;
180 sysname_len = ocstrlen;
181 sysname_wr = NULL;
182 sysname_wr_len = NULL;
183 sysname_bufsize = 0;
184 }
185 }
186  
187 /**
188 * @ingroup snmp_mib2
189 * Initializes sysLocation pointers
190 *
191 * @param ocstr if non-NULL then copy str pointer
192 * @param ocstrlen points to string length, excluding zero terminator.
193 * if set to NULL it is assumed that ocstr is NULL-terminated.
194 * @param bufsize size of the buffer in bytes.
195 * (this is required because the buffer can be overwritten by snmp-set)
196 * if ocstrlen is NULL buffer needs space for terminating 0 byte.
197 * otherwise complete buffer is used for string.
198 * if bufsize is set to 0, the value is regarded as read-only.
199 */
200 void
201 snmp_mib2_set_syslocation(u8_t *ocstr, u16_t *ocstrlen, u16_t bufsize)
202 {
203 if (ocstr != NULL) {
204 syslocation = ocstr;
205 syslocation_wr = ocstr;
206 syslocation_len = ocstrlen;
207 syslocation_wr_len = ocstrlen;
208 syslocation_bufsize = bufsize;
209 }
210 }
211  
212 /**
213 * @ingroup snmp_mib2
214 * see \ref snmp_mib2_set_syslocation but set pointer to readonly memory
215 */
216 void
217 snmp_mib2_set_syslocation_readonly(const u8_t *ocstr, const u16_t *ocstrlen)
218 {
219 if (ocstr != NULL) {
220 syslocation = ocstr;
221 syslocation_len = ocstrlen;
222 syslocation_wr = NULL;
223 syslocation_wr_len = NULL;
224 syslocation_bufsize = 0;
225 }
226 }
227  
228  
229 static s16_t
230 system_get_value(const struct snmp_scalar_array_node_def *node, void *value)
231 {
232 const u8_t *var = NULL;
233 const s16_t *var_len;
234 u16_t result;
235  
236 switch (node->oid) {
237 case 1: /* sysDescr */
238 var = sysdescr;
239 var_len = (const s16_t *)sysdescr_len;
240 break;
241 case 2: { /* sysObjectID */
242 const struct snmp_obj_id *dev_enterprise_oid = snmp_get_device_enterprise_oid();
243 MEMCPY(value, dev_enterprise_oid->id, dev_enterprise_oid->len * sizeof(u32_t));
244 return dev_enterprise_oid->len * sizeof(u32_t);
245 }
246 case 3: /* sysUpTime */
247 MIB2_COPY_SYSUPTIME_TO((u32_t *)value);
248 return sizeof(u32_t);
249 case 4: /* sysContact */
250 var = syscontact;
251 var_len = (const s16_t *)syscontact_len;
252 break;
253 case 5: /* sysName */
254 var = sysname;
255 var_len = (const s16_t *)sysname_len;
256 break;
257 case 6: /* sysLocation */
258 var = syslocation;
259 var_len = (const s16_t *)syslocation_len;
260 break;
261 case 7: /* sysServices */
262 *(s32_t *)value = SNMP_SYSSERVICES;
263 return sizeof(s32_t);
264 default:
265 LWIP_DEBUGF(SNMP_MIB_DEBUG, ("system_get_value(): unknown id: %"S32_F"\n", node->oid));
266 return 0;
267 }
268  
269 /* handle string values (OID 1,4,5 and 6) */
270 LWIP_ASSERT("", (value != NULL));
271 if (var_len == NULL) {
272 result = (s16_t)strlen((const char *)var);
273 } else {
274 result = *var_len;
275 }
276 MEMCPY(value, var, result);
277 return result;
278 }
279  
280 static snmp_err_t
281 system_set_test(const struct snmp_scalar_array_node_def *node, u16_t len, void *value)
282 {
283 snmp_err_t ret = SNMP_ERR_WRONGVALUE;
284 const u16_t *var_bufsize = NULL;
285 const u16_t *var_wr_len;
286  
287 LWIP_UNUSED_ARG(value);
288  
289 switch (node->oid) {
290 case 4: /* sysContact */
291 var_bufsize = &syscontact_bufsize;
292 var_wr_len = syscontact_wr_len;
293 break;
294 case 5: /* sysName */
295 var_bufsize = &sysname_bufsize;
296 var_wr_len = sysname_wr_len;
297 break;
298 case 6: /* sysLocation */
299 var_bufsize = &syslocation_bufsize;
300 var_wr_len = syslocation_wr_len;
301 break;
302 default:
303 LWIP_DEBUGF(SNMP_MIB_DEBUG, ("system_set_test(): unknown id: %"S32_F"\n", node->oid));
304 return ret;
305 }
306  
307 /* check if value is writable at all */
308 if (*var_bufsize > 0) {
309 if (var_wr_len == NULL) {
310 /* we have to take the terminating 0 into account */
311 if (len < *var_bufsize) {
312 ret = SNMP_ERR_NOERROR;
313 }
314 } else {
315 if (len <= *var_bufsize) {
316 ret = SNMP_ERR_NOERROR;
317 }
318 }
319 } else {
320 ret = SNMP_ERR_NOTWRITABLE;
321 }
322  
323 return ret;
324 }
325  
326 static snmp_err_t
327 system_set_value(const struct snmp_scalar_array_node_def *node, u16_t len, void *value)
328 {
329 u8_t *var_wr = NULL;
330 u16_t *var_wr_len;
331  
332 switch (node->oid) {
333 case 4: /* sysContact */
334 var_wr = syscontact_wr;
335 var_wr_len = syscontact_wr_len;
336 break;
337 case 5: /* sysName */
338 var_wr = sysname_wr;
339 var_wr_len = sysname_wr_len;
340 break;
341 case 6: /* sysLocation */
342 var_wr = syslocation_wr;
343 var_wr_len = syslocation_wr_len;
344 break;
345 default:
346 LWIP_DEBUGF(SNMP_MIB_DEBUG, ("system_set_value(): unknown id: %"S32_F"\n", node->oid));
347 return SNMP_ERR_GENERROR;
348 }
349  
350 /* no need to check size of target buffer, this was already done in set_test method */
351 LWIP_ASSERT("", var_wr != NULL);
352 MEMCPY(var_wr, value, len);
353  
354 if (var_wr_len == NULL) {
355 /* add terminating 0 */
356 var_wr[len] = 0;
357 } else {
358 *var_wr_len = len;
359 }
360  
361 return SNMP_ERR_NOERROR;
362 }
363  
364 static const struct snmp_scalar_array_node_def system_nodes[] = {
365 {1, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_NODE_INSTANCE_READ_ONLY}, /* sysDescr */
366 {2, SNMP_ASN1_TYPE_OBJECT_ID, SNMP_NODE_INSTANCE_READ_ONLY}, /* sysObjectID */
367 {3, SNMP_ASN1_TYPE_TIMETICKS, SNMP_NODE_INSTANCE_READ_ONLY}, /* sysUpTime */
368 {4, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_NODE_INSTANCE_READ_WRITE}, /* sysContact */
369 {5, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_NODE_INSTANCE_READ_WRITE}, /* sysName */
370 {6, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_NODE_INSTANCE_READ_WRITE}, /* sysLocation */
371 {7, SNMP_ASN1_TYPE_INTEGER, SNMP_NODE_INSTANCE_READ_ONLY} /* sysServices */
372 };
373  
374 const struct snmp_scalar_array_node snmp_mib2_system_node = SNMP_SCALAR_CREATE_ARRAY_NODE(1, system_nodes, system_get_value, system_set_test, system_set_value);
375  
376 #endif /* LWIP_SNMP && SNMP_LWIP_MIB2 */