Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / lwip / repo / lwip / src / apps / snmp / snmp_mib2_ip.c
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
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
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
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
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
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 = netif_list;
266   while (netif != NULL) {
267     if (ip4_addr_cmp(&ip, netif_ip4_addr(netif))) {
268       /* fill in object properties */
269       return ip_AddrTable_get_cell_value_core(netif, column, value, value_len);
270     }
271
272     netif = netif->next;
273   }
274
275   /* not found */
276   return SNMP_ERR_NOSUCHINSTANCE;
277 }
278
279 static snmp_err_t
280 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)
281 {
282   struct netif *netif;
283   struct snmp_next_oid_state state;
284   u32_t result_temp[LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)];
285
286   /* init struct to search next oid */
287   snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges));
288
289   /* iterate over all possible OIDs to find the next one */
290   netif = netif_list;
291   while (netif != NULL) {
292     u32_t test_oid[LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)];
293     snmp_ip4_to_oid(netif_ip4_addr(netif), &test_oid[0]);
294
295     /* check generated OID: is it a candidate for the next one? */
296     snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges), netif);
297
298     netif = netif->next;
299   }
300
301   /* did we find a next one? */
302   if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
303     snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
304     /* fill in object properties */
305     return ip_AddrTable_get_cell_value_core((struct netif*)state.reference, column, value, value_len);
306   }
307
308   /* not found */
309   return SNMP_ERR_NOSUCHINSTANCE;
310 }
311
312 /* --- ipRouteTable --- */
313
314 /* list of allowed value ranges for incoming OID */
315 static const struct snmp_oid_range ip_RouteTable_oid_ranges[] = {
316   { 0, 0xff }, /* IP A */
317   { 0, 0xff }, /* IP B */
318   { 0, 0xff }, /* IP C */
319   { 0, 0xff }, /* IP D */
320 };
321
322 static snmp_err_t
323 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)
324 {
325   switch (*column) {
326   case 1: /* ipRouteDest */
327     if (default_route) {
328        /* default rte has 0.0.0.0 dest */
329       value->u32 = IP4_ADDR_ANY4->addr;
330     } else {
331       /* netifs have netaddress dest */
332       ip4_addr_t tmp;
333       ip4_addr_get_network(&tmp, netif_ip4_addr(netif), netif_ip4_netmask(netif));
334       value->u32 = tmp.addr;
335     }
336     break;
337   case 2: /* ipRouteIfIndex */
338     value->u32 = netif_to_num(netif);
339     break;
340   case 3: /* ipRouteMetric1 */
341     if (default_route) {
342       value->s32 = 1; /* default */
343     } else {
344       value->s32 = 0; /* normal */
345     }
346     break;
347   case 4: /* ipRouteMetric2 */
348   case 5: /* ipRouteMetric3 */
349   case 6: /* ipRouteMetric4 */
350     value->s32 = -1; /* none */
351     break;
352   case 7: /* ipRouteNextHop */
353     if (default_route) {
354       /* default rte: gateway */
355       value->u32 = netif_ip4_gw(netif)->addr;
356     } else {
357       /* other rtes: netif ip_addr  */
358       value->u32 = netif_ip4_addr(netif)->addr;
359     }
360     break;
361   case 8: /* ipRouteType */
362     if (default_route) {
363       /* default rte is indirect */
364       value->u32 = 4; /* indirect */
365     } else {
366       /* other rtes are direct */
367       value->u32 = 3; /* direct */
368     }
369     break;
370   case 9: /* ipRouteProto */
371     /* locally defined routes */
372     value->u32 = 2; /* local */
373     break;
374   case 10: /* ipRouteAge */
375     /* @todo (sysuptime - timestamp last change) / 100 */
376     value->u32 = 0;
377     break;
378   case 11: /* ipRouteMask */
379     if (default_route) {
380       /* default rte use 0.0.0.0 mask */
381       value->u32 = IP4_ADDR_ANY4->addr;
382     } else {
383       /* other rtes use netmask */
384       value->u32 = netif_ip4_netmask(netif)->addr;
385     }
386     break;
387   case 12: /* ipRouteMetric5 */
388     value->s32 = -1; /* none */
389     break;
390   case 13: /* ipRouteInfo */
391     value->const_ptr = snmp_zero_dot_zero.id;
392     *value_len = snmp_zero_dot_zero.len * sizeof(u32_t);
393     break;
394   default:
395     return SNMP_ERR_NOSUCHINSTANCE;
396   }
397
398   return SNMP_ERR_NOERROR;
399 }
400
401 static snmp_err_t
402 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)
403 {
404   ip4_addr_t test_ip;
405   struct netif *netif;
406
407   /* check if incoming OID length and if values are in plausible range */
408   if (!snmp_oid_in_range(row_oid, row_oid_len, ip_RouteTable_oid_ranges, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges))) {
409     return SNMP_ERR_NOSUCHINSTANCE;
410   }
411
412   /* get IP and port from incoming OID */
413   snmp_oid_to_ip4(&row_oid[0], &test_ip); /* we know it succeeds because of oid_in_range check above */
414
415   /* default route is on default netif */
416   if (ip4_addr_isany_val(test_ip) && (netif_default != NULL)) {
417     /* fill in object properties */
418     return ip_RouteTable_get_cell_value_core(netif_default, 1, column, value, value_len);
419   }
420
421   /* find netif with requested route */
422   netif = netif_list;
423   while (netif != NULL) {
424     ip4_addr_t dst;
425     ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif));
426
427     if (ip4_addr_cmp(&dst, &test_ip)) {
428       /* fill in object properties */
429       return ip_RouteTable_get_cell_value_core(netif, 0, column, value, value_len);
430     }
431
432     netif = netif->next;
433   }
434
435   /* not found */
436   return SNMP_ERR_NOSUCHINSTANCE;
437 }
438
439 static snmp_err_t
440 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)
441 {
442   struct netif *netif;
443   struct snmp_next_oid_state state;
444   u32_t result_temp[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)];
445   u32_t test_oid[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)];
446
447   /* init struct to search next oid */
448   snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges));
449
450   /* check default route */
451   if (netif_default != NULL) {
452     snmp_ip4_to_oid(IP4_ADDR_ANY4, &test_oid[0]);
453     snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif_default);
454   }
455
456   /* iterate over all possible OIDs to find the next one */
457   netif = netif_list;
458   while (netif != NULL) {
459     ip4_addr_t dst;
460     ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif));
461
462     /* check generated OID: is it a candidate for the next one? */
463     if (!ip4_addr_isany_val(dst)) {
464       snmp_ip4_to_oid(&dst, &test_oid[0]);
465       snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif);
466     }
467
468     netif = netif->next;
469   }
470
471   /* did we find a next one? */
472   if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
473     ip4_addr_t dst;
474     snmp_oid_to_ip4(&result_temp[0], &dst);
475     snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
476     /* fill in object properties */
477     return ip_RouteTable_get_cell_value_core((struct netif*)state.reference, ip4_addr_isany_val(dst), column, value, value_len);
478   } else {
479     /* not found */
480     return SNMP_ERR_NOSUCHINSTANCE;
481   }
482 }
483
484 #if LWIP_ARP && LWIP_IPV4
485 /* --- ipNetToMediaTable --- */
486
487 /* list of allowed value ranges for incoming OID */
488 static const struct snmp_oid_range ip_NetToMediaTable_oid_ranges[] = {
489   { 1, 0xff }, /* IfIndex */
490   { 0, 0xff }, /* IP A    */
491   { 0, 0xff }, /* IP B    */
492   { 0, 0xff }, /* IP C    */
493   { 0, 0xff }  /* IP D    */
494 };
495
496 static snmp_err_t
497 ip_NetToMediaTable_get_cell_value_core(u8_t arp_table_index, const u32_t* column, union snmp_variant_value* value, u32_t* value_len)
498 {
499   ip4_addr_t *ip;
500   struct netif *netif;
501   struct eth_addr *ethaddr;
502
503   etharp_get_entry(arp_table_index, &ip, &netif, &ethaddr);
504
505   /* value */
506   switch (*column) {
507   case 1: /* atIfIndex / ipNetToMediaIfIndex */
508     value->u32 = netif_to_num(netif);
509     break;
510   case 2: /* atPhysAddress / ipNetToMediaPhysAddress */
511     value->ptr = ethaddr;
512     *value_len = sizeof(*ethaddr);
513     break;
514   case 3: /* atNetAddress / ipNetToMediaNetAddress */
515     value->u32 = ip->addr;
516     break;
517   case 4: /* ipNetToMediaType */
518     value->u32 = 3; /* dynamic*/
519     break;
520   default:
521     return SNMP_ERR_NOSUCHINSTANCE;
522   }
523
524   return SNMP_ERR_NOERROR;
525 }
526
527 static snmp_err_t
528 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)
529 {
530   ip4_addr_t ip_in;
531   u8_t netif_index;
532   u8_t i;
533
534   /* check if incoming OID length and if values are in plausible range */
535   if (!snmp_oid_in_range(row_oid, row_oid_len, ip_NetToMediaTable_oid_ranges, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges))) {
536     return SNMP_ERR_NOSUCHINSTANCE;
537   }
538
539   /* get IP from incoming OID */
540   netif_index = (u8_t)row_oid[0];
541   snmp_oid_to_ip4(&row_oid[1], &ip_in); /* we know it succeeds because of oid_in_range check above */
542
543   /* find requested entry */
544   for (i=0; i<ARP_TABLE_SIZE; i++) {
545     ip4_addr_t *ip;
546     struct netif *netif;
547     struct eth_addr *ethaddr;
548
549     if (etharp_get_entry(i, &ip, &netif, &ethaddr)) {
550       if ((netif_index == netif_to_num(netif)) && ip4_addr_cmp(&ip_in, ip)) {
551         /* fill in object properties */
552         return ip_NetToMediaTable_get_cell_value_core(i, column, value, value_len);
553       }
554     }
555   }
556
557   /* not found */
558   return SNMP_ERR_NOSUCHINSTANCE;
559 }
560
561 static snmp_err_t
562 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)
563 {
564   u8_t i;
565   struct snmp_next_oid_state state;
566   u32_t result_temp[LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)];
567
568   /* init struct to search next oid */
569   snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges));
570
571   /* iterate over all possible OIDs to find the next one */
572   for (i=0; i<ARP_TABLE_SIZE; i++) {
573     ip4_addr_t *ip;
574     struct netif *netif;
575     struct eth_addr *ethaddr;
576
577     if (etharp_get_entry(i, &ip, &netif, &ethaddr)) {
578       u32_t test_oid[LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)];
579
580       test_oid[0] = netif_to_num(netif);
581       snmp_ip4_to_oid(ip, &test_oid[1]);
582
583       /* check generated OID: is it a candidate for the next one? */
584       snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges), LWIP_PTR_NUMERIC_CAST(void*, i));
585     }
586   }
587
588   /* did we find a next one? */
589   if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
590     snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
591     /* fill in object properties */
592     return ip_NetToMediaTable_get_cell_value_core(LWIP_PTR_NUMERIC_CAST(u8_t, state.reference), column, value, value_len);
593   }
594
595   /* not found */
596   return SNMP_ERR_NOSUCHINSTANCE;
597 }
598
599 #endif /* LWIP_ARP && LWIP_IPV4 */
600
601 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);
602 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);
603 static const struct snmp_scalar_node ip_InReceives      = SNMP_SCALAR_CREATE_NODE_READONLY(3, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
604 static const struct snmp_scalar_node ip_InHdrErrors     = SNMP_SCALAR_CREATE_NODE_READONLY(4, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
605 static const struct snmp_scalar_node ip_InAddrErrors    = SNMP_SCALAR_CREATE_NODE_READONLY(5, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
606 static const struct snmp_scalar_node ip_ForwDatagrams   = SNMP_SCALAR_CREATE_NODE_READONLY(6, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
607 static const struct snmp_scalar_node ip_InUnknownProtos = SNMP_SCALAR_CREATE_NODE_READONLY(7, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
608 static const struct snmp_scalar_node ip_InDiscards      = SNMP_SCALAR_CREATE_NODE_READONLY(8, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
609 static const struct snmp_scalar_node ip_InDelivers      = SNMP_SCALAR_CREATE_NODE_READONLY(9, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
610 static const struct snmp_scalar_node ip_OutRequests     = SNMP_SCALAR_CREATE_NODE_READONLY(10, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
611 static const struct snmp_scalar_node ip_OutDiscards     = SNMP_SCALAR_CREATE_NODE_READONLY(11, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
612 static const struct snmp_scalar_node ip_OutNoRoutes     = SNMP_SCALAR_CREATE_NODE_READONLY(12, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
613 static const struct snmp_scalar_node ip_ReasmTimeout    = SNMP_SCALAR_CREATE_NODE_READONLY(13, SNMP_ASN1_TYPE_INTEGER, ip_get_value);
614 static const struct snmp_scalar_node ip_ReasmReqds      = SNMP_SCALAR_CREATE_NODE_READONLY(14, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
615 static const struct snmp_scalar_node ip_ReasmOKs        = SNMP_SCALAR_CREATE_NODE_READONLY(15, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
616 static const struct snmp_scalar_node ip_ReasmFails      = SNMP_SCALAR_CREATE_NODE_READONLY(16, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
617 static const struct snmp_scalar_node ip_FragOKs         = SNMP_SCALAR_CREATE_NODE_READONLY(17, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
618 static const struct snmp_scalar_node ip_FragFails       = SNMP_SCALAR_CREATE_NODE_READONLY(18, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
619 static const struct snmp_scalar_node ip_FragCreates     = SNMP_SCALAR_CREATE_NODE_READONLY(19, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
620 static const struct snmp_scalar_node ip_RoutingDiscards = SNMP_SCALAR_CREATE_NODE_READONLY(23, SNMP_ASN1_TYPE_COUNTER, ip_get_value);
621
622 static const struct snmp_table_simple_col_def ip_AddrTable_columns[] = {
623   { 1, SNMP_ASN1_TYPE_IPADDR,  SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntAddr */
624   { 2, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntIfIndex */
625   { 3, SNMP_ASN1_TYPE_IPADDR,  SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntNetMask */
626   { 4, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntBcastAddr */
627   { 5, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }  /* ipAdEntReasmMaxSize */
628 };
629
630 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);
631
632 static const struct snmp_table_simple_col_def ip_RouteTable_columns[] = {
633   {  1, SNMP_ASN1_TYPE_IPADDR,    SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteDest */
634   {  2, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteIfIndex */
635   {  3, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric1 */
636   {  4, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric2 */
637   {  5, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric3 */
638   {  6, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric4 */
639   {  7, SNMP_ASN1_TYPE_IPADDR,    SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteNextHop */
640   {  8, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteType */
641   {  9, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteProto */
642   { 10, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteAge */
643   { 11, SNMP_ASN1_TYPE_IPADDR,    SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteMask */
644   { 12, SNMP_ASN1_TYPE_INTEGER,   SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric5 */
645   { 13, SNMP_ASN1_TYPE_OBJECT_ID, SNMP_VARIANT_VALUE_TYPE_PTR }  /* ipRouteInfo */
646 };
647
648 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);
649 #endif /* LWIP_IPV4 */
650
651 #if LWIP_ARP && LWIP_IPV4
652 static const struct snmp_table_simple_col_def ip_NetToMediaTable_columns[] = {
653   {  1, SNMP_ASN1_TYPE_INTEGER,      SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipNetToMediaIfIndex */
654   {  2, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_VARIANT_VALUE_TYPE_PTR }, /* ipNetToMediaPhysAddress */
655   {  3, SNMP_ASN1_TYPE_IPADDR,       SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipNetToMediaNetAddress */
656   {  4, SNMP_ASN1_TYPE_INTEGER,      SNMP_VARIANT_VALUE_TYPE_U32 }  /* ipNetToMediaType */
657 };
658
659 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);
660 #endif /* LWIP_ARP && LWIP_IPV4 */
661
662 #if LWIP_IPV4
663 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */
664 CREATE_LWIP_SYNC_NODE( 1, ip_Forwarding)
665 CREATE_LWIP_SYNC_NODE( 2, ip_DefaultTTL)
666 CREATE_LWIP_SYNC_NODE( 3, ip_InReceives)
667 CREATE_LWIP_SYNC_NODE( 4, ip_InHdrErrors)
668 CREATE_LWIP_SYNC_NODE( 5, ip_InAddrErrors)
669 CREATE_LWIP_SYNC_NODE( 6, ip_ForwDatagrams)
670 CREATE_LWIP_SYNC_NODE( 7, ip_InUnknownProtos)
671 CREATE_LWIP_SYNC_NODE( 8, ip_InDiscards)
672 CREATE_LWIP_SYNC_NODE( 9, ip_InDelivers)
673 CREATE_LWIP_SYNC_NODE(10, ip_OutRequests)
674 CREATE_LWIP_SYNC_NODE(11, ip_OutDiscards)
675 CREATE_LWIP_SYNC_NODE(12, ip_OutNoRoutes)
676 CREATE_LWIP_SYNC_NODE(13, ip_ReasmTimeout)
677 CREATE_LWIP_SYNC_NODE(14, ip_ReasmReqds)
678 CREATE_LWIP_SYNC_NODE(15, ip_ReasmOKs)
679 CREATE_LWIP_SYNC_NODE(15, ip_ReasmFails)
680 CREATE_LWIP_SYNC_NODE(17, ip_FragOKs)
681 CREATE_LWIP_SYNC_NODE(18, ip_FragFails)
682 CREATE_LWIP_SYNC_NODE(19, ip_FragCreates)
683 CREATE_LWIP_SYNC_NODE(20, ip_AddrTable)
684 CREATE_LWIP_SYNC_NODE(21, ip_RouteTable)
685 #if LWIP_ARP
686 CREATE_LWIP_SYNC_NODE(22, ip_NetToMediaTable)
687 #endif /* LWIP_ARP */
688 CREATE_LWIP_SYNC_NODE(23, ip_RoutingDiscards)
689
690 static const struct snmp_node* const ip_nodes[] = {
691   &SYNC_NODE_NAME(ip_Forwarding).node.node,
692   &SYNC_NODE_NAME(ip_DefaultTTL).node.node,
693   &SYNC_NODE_NAME(ip_InReceives).node.node,
694   &SYNC_NODE_NAME(ip_InHdrErrors).node.node,
695   &SYNC_NODE_NAME(ip_InAddrErrors).node.node,
696   &SYNC_NODE_NAME(ip_ForwDatagrams).node.node,
697   &SYNC_NODE_NAME(ip_InUnknownProtos).node.node,
698   &SYNC_NODE_NAME(ip_InDiscards).node.node,
699   &SYNC_NODE_NAME(ip_InDelivers).node.node,
700   &SYNC_NODE_NAME(ip_OutRequests).node.node,
701   &SYNC_NODE_NAME(ip_OutDiscards).node.node,
702   &SYNC_NODE_NAME(ip_OutNoRoutes).node.node,
703   &SYNC_NODE_NAME(ip_ReasmTimeout).node.node,
704   &SYNC_NODE_NAME(ip_ReasmReqds).node.node,
705   &SYNC_NODE_NAME(ip_ReasmOKs).node.node,
706   &SYNC_NODE_NAME(ip_ReasmFails).node.node,
707   &SYNC_NODE_NAME(ip_FragOKs).node.node,
708   &SYNC_NODE_NAME(ip_FragFails).node.node,
709   &SYNC_NODE_NAME(ip_FragCreates).node.node,
710   &SYNC_NODE_NAME(ip_AddrTable).node.node,
711   &SYNC_NODE_NAME(ip_RouteTable).node.node,
712 #if LWIP_ARP
713   &SYNC_NODE_NAME(ip_NetToMediaTable).node.node,
714 #endif /* LWIP_ARP */
715   &SYNC_NODE_NAME(ip_RoutingDiscards).node.node
716 };
717
718 const struct snmp_tree_node snmp_mib2_ip_root = SNMP_CREATE_TREE_NODE(4, ip_nodes);
719 #endif /* LWIP_IPV4 */
720
721 /* --- at .1.3.6.1.2.1.3 ----------------------------------------------------- */
722
723 #if LWIP_ARP && LWIP_IPV4
724 /* at node table is a subset of ip_nettomedia table (same rows but less columns) */
725 static const struct snmp_table_simple_col_def at_Table_columns[] = {
726   { 1, SNMP_ASN1_TYPE_INTEGER,      SNMP_VARIANT_VALUE_TYPE_U32 }, /* atIfIndex */
727   { 2, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_VARIANT_VALUE_TYPE_PTR }, /* atPhysAddress */
728   { 3, SNMP_ASN1_TYPE_IPADDR,       SNMP_VARIANT_VALUE_TYPE_U32 }  /* atNetAddress */
729 };
730
731 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);
732
733 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */
734 CREATE_LWIP_SYNC_NODE(1, at_Table)
735
736 static const struct snmp_node* const at_nodes[] = {
737   &SYNC_NODE_NAME(at_Table).node.node
738 };
739
740 const struct snmp_tree_node snmp_mib2_at_root = SNMP_CREATE_TREE_NODE(3, at_nodes);
741 #endif /* LWIP_ARP && LWIP_IPV4 */
742
743 #endif /* LWIP_SNMP && SNMP_LWIP_MIB2 */