• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * Management Information Base II (RFC1213) IP 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/stats.h"
43 #include "lwip/netif.h"
44 #include "lwip/ip.h"
45 #include "lwip/etharp.h"
46 
47 #if LWIP_SNMP && SNMP_LWIP_MIB2
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 #if LWIP_IPV4
59 /* --- ip .1.3.6.1.2.1.4 ----------------------------------------------------- */
60 
61 static s16_t
ip_get_value(struct snmp_node_instance * instance,void * value)62 ip_get_value(struct snmp_node_instance *instance, void *value)
63 {
64   s32_t *sint_ptr = (s32_t *)value;
65   u32_t *uint_ptr = (u32_t *)value;
66 
67   switch (instance->node->oid) {
68     case 1: /* ipForwarding */
69 #if IP_FORWARD
70       /* forwarding */
71       *sint_ptr = 1;
72 #else
73       /* not-forwarding */
74       *sint_ptr = 2;
75 #endif
76       return sizeof(*sint_ptr);
77     case 2: /* ipDefaultTTL */
78       *sint_ptr = IP_DEFAULT_TTL;
79       return sizeof(*sint_ptr);
80     case 3: /* ipInReceives */
81       *uint_ptr = STATS_GET(mib2.ipinreceives);
82       return sizeof(*uint_ptr);
83     case 4: /* ipInHdrErrors */
84       *uint_ptr = STATS_GET(mib2.ipinhdrerrors);
85       return sizeof(*uint_ptr);
86     case 5: /* ipInAddrErrors */
87       *uint_ptr = STATS_GET(mib2.ipinaddrerrors);
88       return sizeof(*uint_ptr);
89     case 6: /* ipForwDatagrams */
90       *uint_ptr = STATS_GET(mib2.ipforwdatagrams);
91       return sizeof(*uint_ptr);
92     case 7: /* ipInUnknownProtos */
93       *uint_ptr = STATS_GET(mib2.ipinunknownprotos);
94       return sizeof(*uint_ptr);
95     case 8: /* ipInDiscards */
96       *uint_ptr = STATS_GET(mib2.ipindiscards);
97       return sizeof(*uint_ptr);
98     case 9: /* ipInDelivers */
99       *uint_ptr = STATS_GET(mib2.ipindelivers);
100       return sizeof(*uint_ptr);
101     case 10: /* ipOutRequests */
102       *uint_ptr = STATS_GET(mib2.ipoutrequests);
103       return sizeof(*uint_ptr);
104     case 11: /* ipOutDiscards */
105       *uint_ptr = STATS_GET(mib2.ipoutdiscards);
106       return sizeof(*uint_ptr);
107     case 12: /* ipOutNoRoutes */
108       *uint_ptr = STATS_GET(mib2.ipoutnoroutes);
109       return sizeof(*uint_ptr);
110     case 13: /* ipReasmTimeout */
111 #if IP_REASSEMBLY
112       *sint_ptr = IP_REASS_MAXAGE;
113 #else
114       *sint_ptr = 0;
115 #endif
116       return sizeof(*sint_ptr);
117     case 14: /* ipReasmReqds */
118       *uint_ptr = STATS_GET(mib2.ipreasmreqds);
119       return sizeof(*uint_ptr);
120     case 15: /* ipReasmOKs */
121       *uint_ptr = STATS_GET(mib2.ipreasmoks);
122       return sizeof(*uint_ptr);
123     case 16: /* ipReasmFails */
124       *uint_ptr = STATS_GET(mib2.ipreasmfails);
125       return sizeof(*uint_ptr);
126     case 17: /* ipFragOKs */
127       *uint_ptr = STATS_GET(mib2.ipfragoks);
128       return sizeof(*uint_ptr);
129     case 18: /* ipFragFails */
130       *uint_ptr = STATS_GET(mib2.ipfragfails);
131       return sizeof(*uint_ptr);
132     case 19: /* ipFragCreates */
133       *uint_ptr = STATS_GET(mib2.ipfragcreates);
134       return sizeof(*uint_ptr);
135     case 23: /* ipRoutingDiscards: not supported -> always 0 */
136       *uint_ptr = 0;
137       return sizeof(*uint_ptr);
138     default:
139       LWIP_DEBUGF(SNMP_MIB_DEBUG, ("ip_get_value(): unknown id: %"S32_F"\n", instance->node->oid));
140       break;
141   }
142 
143   return 0;
144 }
145 
146 /**
147  * Test ip object value before setting.
148  *
149  * @param instance node instance
150  * @param len return value space (in bytes)
151  * @param value points to (varbind) space to copy value from.
152  *
153  * @note we allow set if the value matches the hardwired value,
154  *   otherwise return badvalue.
155  */
156 static snmp_err_t
ip_set_test(struct snmp_node_instance * instance,u16_t len,void * value)157 ip_set_test(struct snmp_node_instance *instance, u16_t len, void *value)
158 {
159   snmp_err_t ret = SNMP_ERR_WRONGVALUE;
160   s32_t *sint_ptr = (s32_t *)value;
161 
162   LWIP_UNUSED_ARG(len);
163   switch (instance->node->oid) {
164     case 1: /* ipForwarding */
165 #if IP_FORWARD
166       /* forwarding */
167       if (*sint_ptr == 1)
168 #else
169       /* not-forwarding */
170       if (*sint_ptr == 2)
171 #endif
172       {
173         ret = SNMP_ERR_NOERROR;
174       }
175       break;
176     case 2: /* ipDefaultTTL */
177       if (*sint_ptr == IP_DEFAULT_TTL) {
178         ret = SNMP_ERR_NOERROR;
179       }
180       break;
181     default:
182       LWIP_DEBUGF(SNMP_MIB_DEBUG, ("ip_set_test(): unknown id: %"S32_F"\n", instance->node->oid));
183       break;
184   }
185 
186   return ret;
187 }
188 
189 static snmp_err_t
ip_set_value(struct snmp_node_instance * instance,u16_t len,void * value)190 ip_set_value(struct snmp_node_instance *instance, u16_t len, void *value)
191 {
192   LWIP_UNUSED_ARG(instance);
193   LWIP_UNUSED_ARG(len);
194   LWIP_UNUSED_ARG(value);
195   /* nothing to do here because in set_test we only accept values being the same as our own stored value -> no need to store anything */
196   return SNMP_ERR_NOERROR;
197 }
198 
199 /* --- ipAddrTable --- */
200 
201 /* list of allowed value ranges for incoming OID */
202 static const struct snmp_oid_range ip_AddrTable_oid_ranges[] = {
203   { 0, 0xff }, /* IP A */
204   { 0, 0xff }, /* IP B */
205   { 0, 0xff }, /* IP C */
206   { 0, 0xff }  /* IP D */
207 };
208 
209 static snmp_err_t
ip_AddrTable_get_cell_value_core(struct netif * netif,const u32_t * column,union snmp_variant_value * value,u32_t * value_len)210 ip_AddrTable_get_cell_value_core(struct netif *netif, const u32_t *column, union snmp_variant_value *value, u32_t *value_len)
211 {
212   LWIP_UNUSED_ARG(value_len);
213 
214   switch (*column) {
215     case 1: /* ipAdEntAddr */
216       value->u32 = netif_ip4_addr(netif)->addr;
217       break;
218     case 2: /* ipAdEntIfIndex */
219       value->u32 = netif_to_num(netif);
220       break;
221     case 3: /* ipAdEntNetMask */
222       value->u32 = netif_ip4_netmask(netif)->addr;
223       break;
224     case 4: /* ipAdEntBcastAddr */
225       /* lwIP oddity, there's no broadcast
226          address in the netif we can rely on */
227       value->u32 = IPADDR_BROADCAST & 1;
228       break;
229     case 5: /* ipAdEntReasmMaxSize */
230 #if IP_REASSEMBLY
231       /* @todo The theoretical maximum is IP_REASS_MAX_PBUFS * size of the pbufs,
232        * but only if receiving one fragmented packet at a time.
233        * The current solution is to calculate for 2 simultaneous packets...
234        */
235       value->u32 = (IP_HLEN + ((IP_REASS_MAX_PBUFS / 2) *
236                                (PBUF_POOL_BUFSIZE - PBUF_LINK_ENCAPSULATION_HLEN - PBUF_LINK_HLEN - IP_HLEN)));
237 #else
238       /** @todo returning MTU would be a bad thing and
239           returning a wild guess like '576' isn't good either */
240       value->u32 = 0;
241 #endif
242       break;
243     default:
244       return SNMP_ERR_NOSUCHINSTANCE;
245   }
246 
247   return SNMP_ERR_NOERROR;
248 }
249 
250 static snmp_err_t
ip_AddrTable_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)251 ip_AddrTable_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)
252 {
253   ip4_addr_t ip;
254   struct netif *netif;
255 
256   /* check if incoming OID length and if values are in plausible range */
257   if (!snmp_oid_in_range(row_oid, row_oid_len, ip_AddrTable_oid_ranges, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges))) {
258     return SNMP_ERR_NOSUCHINSTANCE;
259   }
260 
261   /* get IP from incoming OID */
262   snmp_oid_to_ip4(&row_oid[0], &ip); /* we know it succeeds because of oid_in_range check above */
263 
264   /* find netif with requested ip */
265   NETIF_FOREACH(netif) {
266     if (ip4_addr_cmp(&ip, netif_ip4_addr(netif))) {
267       /* fill in object properties */
268       return ip_AddrTable_get_cell_value_core(netif, column, value, value_len);
269     }
270   }
271 
272   /* not found */
273   return SNMP_ERR_NOSUCHINSTANCE;
274 }
275 
276 static snmp_err_t
ip_AddrTable_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)277 ip_AddrTable_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)
278 {
279   struct netif *netif;
280   struct snmp_next_oid_state state;
281   u32_t result_temp[LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)];
282 
283   /* init struct to search next oid */
284   snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges));
285 
286   /* iterate over all possible OIDs to find the next one */
287   NETIF_FOREACH(netif) {
288     u32_t test_oid[LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)];
289     snmp_ip4_to_oid(netif_ip4_addr(netif), &test_oid[0]);
290 
291     /* check generated OID: is it a candidate for the next one? */
292     snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges), netif);
293   }
294 
295   /* did we find a next one? */
296   if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
297     snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
298     /* fill in object properties */
299     return ip_AddrTable_get_cell_value_core((struct netif *)state.reference, column, value, value_len);
300   }
301 
302   /* not found */
303   return SNMP_ERR_NOSUCHINSTANCE;
304 }
305 
306 /* --- ipRouteTable --- */
307 
308 /* list of allowed value ranges for incoming OID */
309 static const struct snmp_oid_range ip_RouteTable_oid_ranges[] = {
310   { 0, 0xff }, /* IP A */
311   { 0, 0xff }, /* IP B */
312   { 0, 0xff }, /* IP C */
313   { 0, 0xff }, /* IP D */
314 };
315 
316 static snmp_err_t
ip_RouteTable_get_cell_value_core(struct netif * netif,u8_t default_route,const u32_t * column,union snmp_variant_value * value,u32_t * value_len)317 ip_RouteTable_get_cell_value_core(struct netif *netif, u8_t default_route, const u32_t *column, union snmp_variant_value *value, u32_t *value_len)
318 {
319   switch (*column) {
320     case 1: /* ipRouteDest */
321       if (default_route) {
322         /* default rte has 0.0.0.0 dest */
323         value->u32 = IP4_ADDR_ANY4->addr;
324       } else {
325         /* netifs have netaddress dest */
326         ip4_addr_t tmp;
327         ip4_addr_get_network(&tmp, netif_ip4_addr(netif), netif_ip4_netmask(netif));
328         value->u32 = tmp.addr;
329       }
330       break;
331     case 2: /* ipRouteIfIndex */
332       value->u32 = netif_to_num(netif);
333       break;
334     case 3: /* ipRouteMetric1 */
335       if (default_route) {
336         value->s32 = 1; /* default */
337       } else {
338         value->s32 = 0; /* normal */
339       }
340       break;
341     case 4: /* ipRouteMetric2 */
342     case 5: /* ipRouteMetric3 */
343     case 6: /* ipRouteMetric4 */
344       value->s32 = -1; /* none */
345       break;
346     case 7: /* ipRouteNextHop */
347       if (default_route) {
348         /* default rte: gateway */
349         value->u32 = netif_ip4_gw(netif)->addr;
350       } else {
351         /* other rtes: netif ip_addr  */
352         value->u32 = netif_ip4_addr(netif)->addr;
353       }
354       break;
355     case 8: /* ipRouteType */
356       if (default_route) {
357         /* default rte is indirect */
358         value->u32 = 4; /* indirect */
359       } else {
360         /* other rtes are direct */
361         value->u32 = 3; /* direct */
362       }
363       break;
364     case 9: /* ipRouteProto */
365       /* locally defined routes */
366       value->u32 = 2; /* local */
367       break;
368     case 10: /* ipRouteAge */
369       /* @todo (sysuptime - timestamp last change) / 100 */
370       value->u32 = 0;
371       break;
372     case 11: /* ipRouteMask */
373       if (default_route) {
374         /* default rte use 0.0.0.0 mask */
375         value->u32 = IP4_ADDR_ANY4->addr;
376       } else {
377         /* other rtes use netmask */
378         value->u32 = netif_ip4_netmask(netif)->addr;
379       }
380       break;
381     case 12: /* ipRouteMetric5 */
382       value->s32 = -1; /* none */
383       break;
384     case 13: /* ipRouteInfo */
385       value->const_ptr = snmp_zero_dot_zero.id;
386       *value_len = snmp_zero_dot_zero.len * sizeof(u32_t);
387       break;
388     default:
389       return SNMP_ERR_NOSUCHINSTANCE;
390   }
391 
392   return SNMP_ERR_NOERROR;
393 }
394 
395 static snmp_err_t
ip_RouteTable_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)396 ip_RouteTable_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)
397 {
398   ip4_addr_t test_ip;
399   struct netif *netif;
400 
401   /* check if incoming OID length and if values are in plausible range */
402   if (!snmp_oid_in_range(row_oid, row_oid_len, ip_RouteTable_oid_ranges, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges))) {
403     return SNMP_ERR_NOSUCHINSTANCE;
404   }
405 
406   /* get IP and port from incoming OID */
407   snmp_oid_to_ip4(&row_oid[0], &test_ip); /* we know it succeeds because of oid_in_range check above */
408 
409   /* default route is on default netif */
410   if (ip4_addr_isany_val(test_ip) && (netif_default != NULL)) {
411     /* fill in object properties */
412     return ip_RouteTable_get_cell_value_core(netif_default, 1, column, value, value_len);
413   }
414 
415   /* find netif with requested route */
416   NETIF_FOREACH(netif) {
417     ip4_addr_t dst;
418     ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif));
419 
420     if (ip4_addr_cmp(&dst, &test_ip)) {
421       /* fill in object properties */
422       return ip_RouteTable_get_cell_value_core(netif, 0, column, value, value_len);
423     }
424   }
425 
426   /* not found */
427   return SNMP_ERR_NOSUCHINSTANCE;
428 }
429 
430 static snmp_err_t
ip_RouteTable_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)431 ip_RouteTable_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)
432 {
433   struct netif *netif;
434   struct snmp_next_oid_state state;
435   u32_t result_temp[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)];
436   u32_t test_oid[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)];
437 
438   /* init struct to search next oid */
439   snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges));
440 
441   /* check default route */
442   if (netif_default != NULL) {
443     snmp_ip4_to_oid(IP4_ADDR_ANY4, &test_oid[0]);
444     snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif_default);
445   }
446 
447   /* iterate over all possible OIDs to find the next one */
448   NETIF_FOREACH(netif) {
449     ip4_addr_t dst;
450     ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif));
451 
452     /* check generated OID: is it a candidate for the next one? */
453     if (!ip4_addr_isany_val(dst)) {
454       snmp_ip4_to_oid(&dst, &test_oid[0]);
455       snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif);
456     }
457   }
458 
459   /* did we find a next one? */
460   if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
461     ip4_addr_t dst;
462     snmp_oid_to_ip4(&result_temp[0], &dst);
463     snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
464     /* fill in object properties */
465     return ip_RouteTable_get_cell_value_core((struct netif *)state.reference, ip4_addr_isany_val(dst), column, value, value_len);
466   } else {
467     /* not found */
468     return SNMP_ERR_NOSUCHINSTANCE;
469   }
470 }
471 
472 #if LWIP_ARP && LWIP_IPV4
473 /* --- ipNetToMediaTable --- */
474 
475 /* list of allowed value ranges for incoming OID */
476 static const struct snmp_oid_range ip_NetToMediaTable_oid_ranges[] = {
477   { 1, 0xff }, /* IfIndex */
478   { 0, 0xff }, /* IP A    */
479   { 0, 0xff }, /* IP B    */
480   { 0, 0xff }, /* IP C    */
481   { 0, 0xff }  /* IP D    */
482 };
483 
484 static snmp_err_t
ip_NetToMediaTable_get_cell_value_core(size_t arp_table_index,const u32_t * column,union snmp_variant_value * value,u32_t * value_len)485 ip_NetToMediaTable_get_cell_value_core(size_t arp_table_index, const u32_t *column, union snmp_variant_value *value, u32_t *value_len)
486 {
487   ip4_addr_t *ip;
488   struct netif *netif;
489   struct eth_addr *ethaddr;
490 
491   etharp_get_entry(arp_table_index, &ip, &netif, &ethaddr);
492 
493   /* value */
494   switch (*column) {
495     case 1: /* atIfIndex / ipNetToMediaIfIndex */
496       value->u32 = netif_to_num(netif);
497       break;
498     case 2: /* atPhysAddress / ipNetToMediaPhysAddress */
499       value->ptr = ethaddr;
500       *value_len = sizeof(*ethaddr);
501       break;
502     case 3: /* atNetAddress / ipNetToMediaNetAddress */
503       value->u32 = ip->addr;
504       break;
505     case 4: /* ipNetToMediaType */
506       value->u32 = 3; /* dynamic*/
507       break;
508     default:
509       return SNMP_ERR_NOSUCHINSTANCE;
510   }
511 
512   return SNMP_ERR_NOERROR;
513 }
514 
515 static snmp_err_t
ip_NetToMediaTable_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)516 ip_NetToMediaTable_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)
517 {
518   ip4_addr_t ip_in;
519   u8_t netif_index;
520   size_t i;
521 
522   /* check if incoming OID length and if values are in plausible range */
523   if (!snmp_oid_in_range(row_oid, row_oid_len, ip_NetToMediaTable_oid_ranges, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges))) {
524     return SNMP_ERR_NOSUCHINSTANCE;
525   }
526 
527   /* get IP from incoming OID */
528   netif_index = (u8_t)row_oid[0];
529   snmp_oid_to_ip4(&row_oid[1], &ip_in); /* we know it succeeds because of oid_in_range check above */
530 
531   /* find requested entry */
532   for (i = 0; i < ARP_TABLE_SIZE; i++) {
533     ip4_addr_t *ip;
534     struct netif *netif;
535     struct eth_addr *ethaddr;
536 
537     if (etharp_get_entry(i, &ip, &netif, &ethaddr)) {
538       if ((netif_index == netif_to_num(netif)) && ip4_addr_cmp(&ip_in, ip)) {
539         /* fill in object properties */
540         return ip_NetToMediaTable_get_cell_value_core(i, column, value, value_len);
541       }
542     }
543   }
544 
545   /* not found */
546   return SNMP_ERR_NOSUCHINSTANCE;
547 }
548 
549 static snmp_err_t
ip_NetToMediaTable_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)550 ip_NetToMediaTable_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)
551 {
552   size_t i;
553   struct snmp_next_oid_state state;
554   u32_t result_temp[LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)];
555 
556   /* init struct to search next oid */
557   snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges));
558 
559   /* iterate over all possible OIDs to find the next one */
560   for (i = 0; i < ARP_TABLE_SIZE; i++) {
561     ip4_addr_t *ip;
562     struct netif *netif;
563     struct eth_addr *ethaddr;
564 
565     if (etharp_get_entry(i, &ip, &netif, &ethaddr)) {
566       u32_t test_oid[LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)];
567 
568       test_oid[0] = netif_to_num(netif);
569       snmp_ip4_to_oid(ip, &test_oid[1]);
570 
571       /* check generated OID: is it a candidate for the next one? */
572       snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges), LWIP_PTR_NUMERIC_CAST(void *, i));
573     }
574   }
575 
576   /* did we find a next one? */
577   if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
578     snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
579     /* fill in object properties */
580     return ip_NetToMediaTable_get_cell_value_core(LWIP_PTR_NUMERIC_CAST(size_t, state.reference), column, value, value_len);
581   }
582 
583   /* not found */
584   return SNMP_ERR_NOSUCHINSTANCE;
585 }
586 
587 #endif /* LWIP_ARP && LWIP_IPV4 */
588 
589 static const struct snmp_scalar_node ip_Forwarding      = SNMP_SCALAR_CREATE_NODE(1, SNMP_NODE_INSTANCE_READ_WRITE, SNMP_ASN1_TYPE_INTEGER, ip_get_value, ip_set_test, ip_set_value);
590 static const struct snmp_scalar_node ip_DefaultTTL      = SNMP_SCALAR_CREATE_NODE(2, SNMP_NODE_INSTANCE_READ_WRITE, SNMP_ASN1_TYPE_INTEGER, ip_get_value, ip_set_test, ip_set_value);
591 static const struct snmp_scalar_node ip_InReceives      = SNMP_SCALAR_CREATE_NODE_READONLY(3, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
592 static const struct snmp_scalar_node ip_InHdrErrors     = SNMP_SCALAR_CREATE_NODE_READONLY(4, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
593 static const struct snmp_scalar_node ip_InAddrErrors    = SNMP_SCALAR_CREATE_NODE_READONLY(5, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
594 static const struct snmp_scalar_node ip_ForwDatagrams   = SNMP_SCALAR_CREATE_NODE_READONLY(6, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
595 static const struct snmp_scalar_node ip_InUnknownProtos = SNMP_SCALAR_CREATE_NODE_READONLY(7, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
596 static const struct snmp_scalar_node ip_InDiscards      = SNMP_SCALAR_CREATE_NODE_READONLY(8, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
597 static const struct snmp_scalar_node ip_InDelivers      = SNMP_SCALAR_CREATE_NODE_READONLY(9, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
598 static const struct snmp_scalar_node ip_OutRequests     = SNMP_SCALAR_CREATE_NODE_READONLY(10, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
599 static const struct snmp_scalar_node ip_OutDiscards     = SNMP_SCALAR_CREATE_NODE_READONLY(11, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
600 static const struct snmp_scalar_node ip_OutNoRoutes     = SNMP_SCALAR_CREATE_NODE_READONLY(12, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
601 static const struct snmp_scalar_node ip_ReasmTimeout    = SNMP_SCALAR_CREATE_NODE_READONLY(13, SNMP_ASN1_TYPE_INTEGER, ip_get_value);
602 static const struct snmp_scalar_node ip_ReasmReqds      = SNMP_SCALAR_CREATE_NODE_READONLY(14, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
603 static const struct snmp_scalar_node ip_ReasmOKs        = SNMP_SCALAR_CREATE_NODE_READONLY(15, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
604 static const struct snmp_scalar_node ip_ReasmFails      = SNMP_SCALAR_CREATE_NODE_READONLY(16, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
605 static const struct snmp_scalar_node ip_FragOKs         = SNMP_SCALAR_CREATE_NODE_READONLY(17, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
606 static const struct snmp_scalar_node ip_FragFails       = SNMP_SCALAR_CREATE_NODE_READONLY(18, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
607 static const struct snmp_scalar_node ip_FragCreates     = SNMP_SCALAR_CREATE_NODE_READONLY(19, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
608 static const struct snmp_scalar_node ip_RoutingDiscards = SNMP_SCALAR_CREATE_NODE_READONLY(23, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
609 
610 static const struct snmp_table_simple_col_def ip_AddrTable_columns[] = {
611   { 1, SNMP_ASN1_TYPE_IPADDR,  SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntAddr */
612   { 2, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntIfIndex */
613   { 3, SNMP_ASN1_TYPE_IPADDR,  SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntNetMask */
614   { 4, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntBcastAddr */
615   { 5, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }  /* ipAdEntReasmMaxSize */
616 };
617 
618 static const struct snmp_table_simple_node ip_AddrTable = SNMP_TABLE_CREATE_SIMPLE(20, ip_AddrTable_columns, ip_AddrTable_get_cell_value, ip_AddrTable_get_next_cell_instance_and_value);
619 
620 static const struct snmp_table_simple_col_def ip_RouteTable_columns[] = {
621   {  1, SNMP_ASN1_TYPE_IPADDR,    SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteDest */
622   {  2, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteIfIndex */
623   {  3, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric1 */
624   {  4, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric2 */
625   {  5, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric3 */
626   {  6, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric4 */
627   {  7, SNMP_ASN1_TYPE_IPADDR,    SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteNextHop */
628   {  8, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteType */
629   {  9, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteProto */
630   { 10, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteAge */
631   { 11, SNMP_ASN1_TYPE_IPADDR,    SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteMask */
632   { 12, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric5 */
633   { 13, SNMP_ASN1_TYPE_OBJECT_ID, SNMP_VARIANT_VALUE_TYPE_PTR }  /* ipRouteInfo */
634 };
635 
636 static const struct snmp_table_simple_node ip_RouteTable = SNMP_TABLE_CREATE_SIMPLE(21, ip_RouteTable_columns, ip_RouteTable_get_cell_value, ip_RouteTable_get_next_cell_instance_and_value);
637 #endif /* LWIP_IPV4 */
638 
639 #if LWIP_ARP && LWIP_IPV4
640 static const struct snmp_table_simple_col_def ip_NetToMediaTable_columns[] = {
641   {  1, SNMP_ASN1_TYPE_INTEGER,      SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipNetToMediaIfIndex */
642   {  2, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_VARIANT_VALUE_TYPE_PTR }, /* ipNetToMediaPhysAddress */
643   {  3, SNMP_ASN1_TYPE_IPADDR,       SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipNetToMediaNetAddress */
644   {  4, SNMP_ASN1_TYPE_INTEGER,      SNMP_VARIANT_VALUE_TYPE_U32 }  /* ipNetToMediaType */
645 };
646 
647 static const struct snmp_table_simple_node ip_NetToMediaTable = SNMP_TABLE_CREATE_SIMPLE(22, ip_NetToMediaTable_columns, ip_NetToMediaTable_get_cell_value, ip_NetToMediaTable_get_next_cell_instance_and_value);
648 #endif /* LWIP_ARP && LWIP_IPV4 */
649 
650 #if LWIP_IPV4
651 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */
652 CREATE_LWIP_SYNC_NODE( 1, ip_Forwarding)
653 CREATE_LWIP_SYNC_NODE( 2, ip_DefaultTTL)
654 CREATE_LWIP_SYNC_NODE( 3, ip_InReceives)
655 CREATE_LWIP_SYNC_NODE( 4, ip_InHdrErrors)
656 CREATE_LWIP_SYNC_NODE( 5, ip_InAddrErrors)
657 CREATE_LWIP_SYNC_NODE( 6, ip_ForwDatagrams)
658 CREATE_LWIP_SYNC_NODE( 7, ip_InUnknownProtos)
659 CREATE_LWIP_SYNC_NODE( 8, ip_InDiscards)
660 CREATE_LWIP_SYNC_NODE( 9, ip_InDelivers)
661 CREATE_LWIP_SYNC_NODE(10, ip_OutRequests)
662 CREATE_LWIP_SYNC_NODE(11, ip_OutDiscards)
663 CREATE_LWIP_SYNC_NODE(12, ip_OutNoRoutes)
664 CREATE_LWIP_SYNC_NODE(13, ip_ReasmTimeout)
665 CREATE_LWIP_SYNC_NODE(14, ip_ReasmReqds)
666 CREATE_LWIP_SYNC_NODE(15, ip_ReasmOKs)
667 CREATE_LWIP_SYNC_NODE(15, ip_ReasmFails)
668 CREATE_LWIP_SYNC_NODE(17, ip_FragOKs)
669 CREATE_LWIP_SYNC_NODE(18, ip_FragFails)
670 CREATE_LWIP_SYNC_NODE(19, ip_FragCreates)
671 CREATE_LWIP_SYNC_NODE(20, ip_AddrTable)
672 CREATE_LWIP_SYNC_NODE(21, ip_RouteTable)
673 #if LWIP_ARP
674 CREATE_LWIP_SYNC_NODE(22, ip_NetToMediaTable)
675 #endif /* LWIP_ARP */
676 CREATE_LWIP_SYNC_NODE(23, ip_RoutingDiscards)
677 
678 static const struct snmp_node *const ip_nodes[] = {
679   &SYNC_NODE_NAME(ip_Forwarding).node.node,
680   &SYNC_NODE_NAME(ip_DefaultTTL).node.node,
681   &SYNC_NODE_NAME(ip_InReceives).node.node,
682   &SYNC_NODE_NAME(ip_InHdrErrors).node.node,
683   &SYNC_NODE_NAME(ip_InAddrErrors).node.node,
684   &SYNC_NODE_NAME(ip_ForwDatagrams).node.node,
685   &SYNC_NODE_NAME(ip_InUnknownProtos).node.node,
686   &SYNC_NODE_NAME(ip_InDiscards).node.node,
687   &SYNC_NODE_NAME(ip_InDelivers).node.node,
688   &SYNC_NODE_NAME(ip_OutRequests).node.node,
689   &SYNC_NODE_NAME(ip_OutDiscards).node.node,
690   &SYNC_NODE_NAME(ip_OutNoRoutes).node.node,
691   &SYNC_NODE_NAME(ip_ReasmTimeout).node.node,
692   &SYNC_NODE_NAME(ip_ReasmReqds).node.node,
693   &SYNC_NODE_NAME(ip_ReasmOKs).node.node,
694   &SYNC_NODE_NAME(ip_ReasmFails).node.node,
695   &SYNC_NODE_NAME(ip_FragOKs).node.node,
696   &SYNC_NODE_NAME(ip_FragFails).node.node,
697   &SYNC_NODE_NAME(ip_FragCreates).node.node,
698   &SYNC_NODE_NAME(ip_AddrTable).node.node,
699   &SYNC_NODE_NAME(ip_RouteTable).node.node,
700 #if LWIP_ARP
701   &SYNC_NODE_NAME(ip_NetToMediaTable).node.node,
702 #endif /* LWIP_ARP */
703   &SYNC_NODE_NAME(ip_RoutingDiscards).node.node
704 };
705 
706 const struct snmp_tree_node snmp_mib2_ip_root = SNMP_CREATE_TREE_NODE(4, ip_nodes);
707 #endif /* LWIP_IPV4 */
708 
709 /* --- at .1.3.6.1.2.1.3 ----------------------------------------------------- */
710 
711 #if LWIP_ARP && LWIP_IPV4
712 /* at node table is a subset of ip_nettomedia table (same rows but less columns) */
713 static const struct snmp_table_simple_col_def at_Table_columns[] = {
714   { 1, SNMP_ASN1_TYPE_INTEGER,      SNMP_VARIANT_VALUE_TYPE_U32 }, /* atIfIndex */
715   { 2, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_VARIANT_VALUE_TYPE_PTR }, /* atPhysAddress */
716   { 3, SNMP_ASN1_TYPE_IPADDR,       SNMP_VARIANT_VALUE_TYPE_U32 }  /* atNetAddress */
717 };
718 
719 static const struct snmp_table_simple_node at_Table = SNMP_TABLE_CREATE_SIMPLE(1, at_Table_columns, ip_NetToMediaTable_get_cell_value, ip_NetToMediaTable_get_next_cell_instance_and_value);
720 
721 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */
722 CREATE_LWIP_SYNC_NODE(1, at_Table)
723 
724 static const struct snmp_node *const at_nodes[] = {
725   &SYNC_NODE_NAME(at_Table).node.node
726 };
727 
728 const struct snmp_tree_node snmp_mib2_at_root = SNMP_CREATE_TREE_NODE(3, at_nodes);
729 #endif /* LWIP_ARP && LWIP_IPV4 */
730 
731 #endif /* LWIP_SNMP && SNMP_LWIP_MIB2 */
732