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) UDP 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/udp.h"
43 #include "lwip/stats.h"
44  
45 #include <string.h>
46  
47 #if LWIP_SNMP && SNMP_LWIP_MIB2 && LWIP_UDP
48  
49 #if SNMP_USE_NETCONN
50 #define SYNC_NODE_NAME(node_name) node_name ## _synced
51 #define CREATE_LWIP_SYNC_NODE(oid, node_name) \
52 static const struct snmp_threadsync_node node_name ## _synced = SNMP_CREATE_THREAD_SYNC_NODE(oid, &node_name.node, &snmp_mib2_lwip_locks);
53 #else
54 #define SYNC_NODE_NAME(node_name) node_name
55 #define CREATE_LWIP_SYNC_NODE(oid, node_name)
56 #endif
57  
58 /* --- udp .1.3.6.1.2.1.7 ----------------------------------------------------- */
59  
60 static s16_t
61 udp_get_value(struct snmp_node_instance *instance, void *value)
62 {
63 u32_t *uint_ptr = (u32_t *)value;
64  
65 switch (instance->node->oid) {
66 case 1: /* udpInDatagrams */
67 *uint_ptr = STATS_GET(mib2.udpindatagrams);
68 return sizeof(*uint_ptr);
69 case 2: /* udpNoPorts */
70 *uint_ptr = STATS_GET(mib2.udpnoports);
71 return sizeof(*uint_ptr);
72 case 3: /* udpInErrors */
73 *uint_ptr = STATS_GET(mib2.udpinerrors);
74 return sizeof(*uint_ptr);
75 case 4: /* udpOutDatagrams */
76 *uint_ptr = STATS_GET(mib2.udpoutdatagrams);
77 return sizeof(*uint_ptr);
78 #if LWIP_HAVE_INT64
79 case 8: { /* udpHCInDatagrams */
80 /* use the 32 bit counter for now... */
81 u64_t val64 = STATS_GET(mib2.udpindatagrams);
82 *((u64_t *)value) = val64;
83 }
84 return sizeof(u64_t);
85 case 9: { /* udpHCOutDatagrams */
86 /* use the 32 bit counter for now... */
87 u64_t val64 = STATS_GET(mib2.udpoutdatagrams);
88 *((u64_t *)value) = val64;
89 }
90 return sizeof(u64_t);
91 #endif
92 default:
93 LWIP_DEBUGF(SNMP_MIB_DEBUG, ("udp_get_value(): unknown id: %"S32_F"\n", instance->node->oid));
94 break;
95 }
96  
97 return 0;
98 }
99  
100 /* --- udpEndpointTable --- */
101  
102 static snmp_err_t
103 udp_endpointTable_get_cell_value_core(const u32_t *column, union snmp_variant_value *value)
104 {
105 /* all items except udpEndpointProcess are declared as not-accessible */
106 switch (*column) {
107 case 8: /* udpEndpointProcess */
108 value->u32 = 0; /* not supported */
109 break;
110 default:
111 return SNMP_ERR_NOSUCHINSTANCE;
112 }
113  
114 return SNMP_ERR_NOERROR;
115 }
116  
117 static snmp_err_t
118 udp_endpointTable_get_cell_value(const u32_t *column, const u32_t *row_oid, u8_t row_oid_len, union snmp_variant_value *value, u32_t *value_len)
119 {
120 ip_addr_t local_ip, remote_ip;
121 u16_t local_port, remote_port;
122 struct udp_pcb *pcb;
123 u8_t idx = 0;
124  
125 LWIP_UNUSED_ARG(value_len);
126  
127 /* udpEndpointLocalAddressType + udpEndpointLocalAddress + udpEndpointLocalPort */
128 idx += snmp_oid_to_ip_port(&row_oid[idx], row_oid_len - idx, &local_ip, &local_port);
129 if (idx == 0) {
130 return SNMP_ERR_NOSUCHINSTANCE;
131 }
132  
133 /* udpEndpointRemoteAddressType + udpEndpointRemoteAddress + udpEndpointRemotePort */
134 idx += snmp_oid_to_ip_port(&row_oid[idx], row_oid_len - idx, &remote_ip, &remote_port);
135 if (idx == 0) {
136 return SNMP_ERR_NOSUCHINSTANCE;
137 }
138  
139 /* udpEndpointInstance */
140 if (row_oid_len < (idx + 1)) {
141 return SNMP_ERR_NOSUCHINSTANCE;
142 }
143 if (row_oid[idx] != 0) {
144 return SNMP_ERR_NOSUCHINSTANCE;
145 }
146  
147 /* find udp_pcb with requested ip and port*/
148 pcb = udp_pcbs;
149 while (pcb != NULL) {
150 if (ip_addr_cmp(&local_ip, &pcb->local_ip) &&
151 (local_port == pcb->local_port) &&
152 ip_addr_cmp(&remote_ip, &pcb->remote_ip) &&
153 (remote_port == pcb->remote_port)) {
154 /* fill in object properties */
155 return udp_endpointTable_get_cell_value_core(column, value);
156 }
157 pcb = pcb->next;
158 }
159  
160 /* not found */
161 return SNMP_ERR_NOSUCHINSTANCE;
162 }
163  
164 static snmp_err_t
165 udp_endpointTable_get_next_cell_instance_and_value(const u32_t *column, struct snmp_obj_id *row_oid, union snmp_variant_value *value, u32_t *value_len)
166 {
167 struct udp_pcb *pcb;
168 struct snmp_next_oid_state state;
169 /* 1x udpEndpointLocalAddressType + 1x OID len + 16x udpEndpointLocalAddress + 1x udpEndpointLocalPort +
170 * 1x udpEndpointRemoteAddressType + 1x OID len + 16x udpEndpointRemoteAddress + 1x udpEndpointRemotePort +
171 * 1x udpEndpointInstance = 39
172 */
173 u32_t result_temp[39];
174  
175 LWIP_UNUSED_ARG(value_len);
176  
177 /* init struct to search next oid */
178 snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(result_temp));
179  
180 /* iterate over all possible OIDs to find the next one */
181 pcb = udp_pcbs;
182 while (pcb != NULL) {
183 u32_t test_oid[LWIP_ARRAYSIZE(result_temp)];
184 u8_t idx = 0;
185  
186 /* udpEndpointLocalAddressType + udpEndpointLocalAddress + udpEndpointLocalPort */
187 idx += snmp_ip_port_to_oid(&pcb->local_ip, pcb->local_port, &test_oid[idx]);
188  
189 /* udpEndpointRemoteAddressType + udpEndpointRemoteAddress + udpEndpointRemotePort */
190 idx += snmp_ip_port_to_oid(&pcb->remote_ip, pcb->remote_port, &test_oid[idx]);
191  
192 test_oid[idx] = 0; /* udpEndpointInstance */
193 idx++;
194  
195 /* check generated OID: is it a candidate for the next one? */
196 snmp_next_oid_check(&state, test_oid, idx, NULL);
197  
198 pcb = pcb->next;
199 }
200  
201 /* did we find a next one? */
202 if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
203 snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
204 /* fill in object properties */
205 return udp_endpointTable_get_cell_value_core(column, value);
206 } else {
207 /* not found */
208 return SNMP_ERR_NOSUCHINSTANCE;
209 }
210 }
211  
212 /* --- udpTable --- */
213  
214 #if LWIP_IPV4
215  
216 /* list of allowed value ranges for incoming OID */
217 static const struct snmp_oid_range udp_Table_oid_ranges[] = {
218 { 0, 0xff }, /* IP A */
219 { 0, 0xff }, /* IP B */
220 { 0, 0xff }, /* IP C */
221 { 0, 0xff }, /* IP D */
222 { 1, 0xffff } /* Port */
223 };
224  
225 static snmp_err_t
226 udp_Table_get_cell_value_core(struct udp_pcb *pcb, const u32_t *column, union snmp_variant_value *value, u32_t *value_len)
227 {
228 LWIP_UNUSED_ARG(value_len);
229  
230 switch (*column) {
231 case 1: /* udpLocalAddress */
232 /* set reference to PCB local IP and return a generic node that copies IP4 addresses */
233 value->u32 = ip_2_ip4(&pcb->local_ip)->addr;
234 break;
235 case 2: /* udpLocalPort */
236 /* set reference to PCB local port and return a generic node that copies u16_t values */
237 value->u32 = pcb->local_port;
238 break;
239 default:
240 return SNMP_ERR_NOSUCHINSTANCE;
241 }
242  
243 return SNMP_ERR_NOERROR;
244 }
245  
246 static snmp_err_t
247 udp_Table_get_cell_value(const u32_t *column, const u32_t *row_oid, u8_t row_oid_len, union snmp_variant_value *value, u32_t *value_len)
248 {
249 ip4_addr_t ip;
250 u16_t port;
251 struct udp_pcb *pcb;
252  
253 /* check if incoming OID length and if values are in plausible range */
254 if (!snmp_oid_in_range(row_oid, row_oid_len, udp_Table_oid_ranges, LWIP_ARRAYSIZE(udp_Table_oid_ranges))) {
255 return SNMP_ERR_NOSUCHINSTANCE;
256 }
257  
258 /* get IP and port from incoming OID */
259 snmp_oid_to_ip4(&row_oid[0], &ip); /* we know it succeeds because of oid_in_range check above */
260 port = (u16_t)row_oid[4];
261  
262 /* find udp_pcb with requested ip and port*/
263 pcb = udp_pcbs;
264 while (pcb != NULL) {
265 if (IP_IS_V4_VAL(pcb->local_ip)) {
266 if (ip4_addr_cmp(&ip, ip_2_ip4(&pcb->local_ip)) && (port == pcb->local_port)) {
267 /* fill in object properties */
268 return udp_Table_get_cell_value_core(pcb, column, value, value_len);
269 }
270 }
271 pcb = pcb->next;
272 }
273  
274 /* not found */
275 return SNMP_ERR_NOSUCHINSTANCE;
276 }
277  
278 static snmp_err_t
279 udp_Table_get_next_cell_instance_and_value(const u32_t *column, struct snmp_obj_id *row_oid, union snmp_variant_value *value, u32_t *value_len)
280 {
281 struct udp_pcb *pcb;
282 struct snmp_next_oid_state state;
283 u32_t result_temp[LWIP_ARRAYSIZE(udp_Table_oid_ranges)];
284  
285 /* init struct to search next oid */
286 snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(udp_Table_oid_ranges));
287  
288 /* iterate over all possible OIDs to find the next one */
289 pcb = udp_pcbs;
290 while (pcb != NULL) {
291 u32_t test_oid[LWIP_ARRAYSIZE(udp_Table_oid_ranges)];
292  
293 if (IP_IS_V4_VAL(pcb->local_ip)) {
294 snmp_ip4_to_oid(ip_2_ip4(&pcb->local_ip), &test_oid[0]);
295 test_oid[4] = pcb->local_port;
296  
297 /* check generated OID: is it a candidate for the next one? */
298 snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(udp_Table_oid_ranges), pcb);
299 }
300  
301 pcb = pcb->next;
302 }
303  
304 /* did we find a next one? */
305 if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
306 snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
307 /* fill in object properties */
308 return udp_Table_get_cell_value_core((struct udp_pcb *)state.reference, column, value, value_len);
309 } else {
310 /* not found */
311 return SNMP_ERR_NOSUCHINSTANCE;
312 }
313 }
314  
315 #endif /* LWIP_IPV4 */
316  
317 static const struct snmp_scalar_node udp_inDatagrams = SNMP_SCALAR_CREATE_NODE_READONLY(1, SNMP_ASN1_TYPE_COUNTER, udp_get_value);
318 static const struct snmp_scalar_node udp_noPorts = SNMP_SCALAR_CREATE_NODE_READONLY(2, SNMP_ASN1_TYPE_COUNTER, udp_get_value);
319 static const struct snmp_scalar_node udp_inErrors = SNMP_SCALAR_CREATE_NODE_READONLY(3, SNMP_ASN1_TYPE_COUNTER, udp_get_value);
320 static const struct snmp_scalar_node udp_outDatagrams = SNMP_SCALAR_CREATE_NODE_READONLY(4, SNMP_ASN1_TYPE_COUNTER, udp_get_value);
321 #if LWIP_HAVE_INT64
322 static const struct snmp_scalar_node udp_HCInDatagrams = SNMP_SCALAR_CREATE_NODE_READONLY(8, SNMP_ASN1_TYPE_COUNTER64, udp_get_value);
323 static const struct snmp_scalar_node udp_HCOutDatagrams = SNMP_SCALAR_CREATE_NODE_READONLY(9, SNMP_ASN1_TYPE_COUNTER64, udp_get_value);
324 #endif
325  
326 #if LWIP_IPV4
327 static const struct snmp_table_simple_col_def udp_Table_columns[] = {
328 { 1, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 }, /* udpLocalAddress */
329 { 2, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 } /* udpLocalPort */
330 };
331 static const struct snmp_table_simple_node udp_Table = SNMP_TABLE_CREATE_SIMPLE(5, udp_Table_columns, udp_Table_get_cell_value, udp_Table_get_next_cell_instance_and_value);
332 #endif /* LWIP_IPV4 */
333  
334 static const struct snmp_table_simple_col_def udp_endpointTable_columns[] = {
335 /* all items except udpEndpointProcess are declared as not-accessible */
336 { 8, SNMP_ASN1_TYPE_UNSIGNED32, SNMP_VARIANT_VALUE_TYPE_U32 } /* udpEndpointProcess */
337 };
338  
339 static const struct snmp_table_simple_node udp_endpointTable = SNMP_TABLE_CREATE_SIMPLE(7, udp_endpointTable_columns, udp_endpointTable_get_cell_value, udp_endpointTable_get_next_cell_instance_and_value);
340  
341 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */
342 CREATE_LWIP_SYNC_NODE(1, udp_inDatagrams)
343 CREATE_LWIP_SYNC_NODE(2, udp_noPorts)
344 CREATE_LWIP_SYNC_NODE(3, udp_inErrors)
345 CREATE_LWIP_SYNC_NODE(4, udp_outDatagrams)
346 #if LWIP_IPV4
347 CREATE_LWIP_SYNC_NODE(5, udp_Table)
348 #endif /* LWIP_IPV4 */
349 CREATE_LWIP_SYNC_NODE(7, udp_endpointTable)
350 #if LWIP_HAVE_INT64
351 CREATE_LWIP_SYNC_NODE(8, udp_HCInDatagrams)
352 CREATE_LWIP_SYNC_NODE(9, udp_HCOutDatagrams)
353 #endif
354  
355 static const struct snmp_node *const udp_nodes[] = {
356 &SYNC_NODE_NAME(udp_inDatagrams).node.node,
357 &SYNC_NODE_NAME(udp_noPorts).node.node,
358 &SYNC_NODE_NAME(udp_inErrors).node.node,
359 &SYNC_NODE_NAME(udp_outDatagrams).node.node,
360 #if LWIP_IPV4
361 &SYNC_NODE_NAME(udp_Table).node.node,
362 #endif /* LWIP_IPV4 */
363 &SYNC_NODE_NAME(udp_endpointTable).node.node
364 #if LWIP_HAVE_INT64
365 ,
366 &SYNC_NODE_NAME(udp_HCInDatagrams).node.node,
367 &SYNC_NODE_NAME(udp_HCOutDatagrams).node.node
368 #endif
369 };
370  
371 const struct snmp_tree_node snmp_mib2_udp_root = SNMP_CREATE_TREE_NODE(7, udp_nodes);
372 #endif /* LWIP_SNMP && SNMP_LWIP_MIB2 && LWIP_UDP */