Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
lwip_snmp_mib2_ip.c
00001 /** 00002 * @file 00003 * Management Information Base II (RFC1213) IP objects and functions. 00004 */ 00005 00006 /* 00007 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands. 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without modification, 00011 * are permitted provided that the following conditions are met: 00012 * 00013 * 1. Redistributions of source code must retain the above copyright notice, 00014 * this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright notice, 00016 * this list of conditions and the following disclaimer in the documentation 00017 * and/or other materials provided with the distribution. 00018 * 3. The name of the author may not be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00022 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00024 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00026 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00029 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00030 * OF SUCH DAMAGE. 00031 * 00032 * Author: Dirk Ziegelmeier <dziegel@gmx.de> 00033 * Christiaan Simons <christiaan.simons@axon.tv> 00034 */ 00035 00036 #include "lwip/snmp.h" 00037 #include "lwip/apps/snmp.h" 00038 #include "lwip/apps/snmp_core.h" 00039 #include "lwip/apps/snmp_mib2.h" 00040 #include "lwip/apps/snmp_table.h" 00041 #include "lwip/apps/snmp_scalar.h" 00042 #include "lwip/stats.h" 00043 #include "lwip/netif.h" 00044 #include "lwip/ip.h" 00045 #include "lwip/etharp.h" 00046 00047 #if LWIP_SNMP && SNMP_LWIP_MIB2 00048 00049 #if SNMP_USE_NETCONN 00050 #define SYNC_NODE_NAME(node_name) node_name ## _synced 00051 #define CREATE_LWIP_SYNC_NODE(oid, node_name) \ 00052 static const struct snmp_threadsync_node node_name ## _synced = SNMP_CREATE_THREAD_SYNC_NODE(oid, &node_name.node, &snmp_mib2_lwip_locks); 00053 #else 00054 #define SYNC_NODE_NAME(node_name) node_name 00055 #define CREATE_LWIP_SYNC_NODE(oid, node_name) 00056 #endif 00057 00058 #if LWIP_IPV4 00059 /* --- ip .1.3.6.1.2.1.4 ----------------------------------------------------- */ 00060 00061 static s16_t 00062 ip_get_value(struct snmp_node_instance *instance, void *value) 00063 { 00064 s32_t *sint_ptr = (s32_t *)value; 00065 u32_t *uint_ptr = (u32_t *)value; 00066 00067 switch (instance->node->oid) { 00068 case 1: /* ipForwarding */ 00069 #if IP_FORWARD 00070 /* forwarding */ 00071 *sint_ptr = 1; 00072 #else 00073 /* not-forwarding */ 00074 *sint_ptr = 2; 00075 #endif 00076 return sizeof(*sint_ptr); 00077 case 2: /* ipDefaultTTL */ 00078 *sint_ptr = IP_DEFAULT_TTL; 00079 return sizeof(*sint_ptr); 00080 case 3: /* ipInReceives */ 00081 *uint_ptr = STATS_GET(mib2.ipinreceives); 00082 return sizeof(*uint_ptr); 00083 case 4: /* ipInHdrErrors */ 00084 *uint_ptr = STATS_GET(mib2.ipinhdrerrors); 00085 return sizeof(*uint_ptr); 00086 case 5: /* ipInAddrErrors */ 00087 *uint_ptr = STATS_GET(mib2.ipinaddrerrors); 00088 return sizeof(*uint_ptr); 00089 case 6: /* ipForwDatagrams */ 00090 *uint_ptr = STATS_GET(mib2.ipforwdatagrams); 00091 return sizeof(*uint_ptr); 00092 case 7: /* ipInUnknownProtos */ 00093 *uint_ptr = STATS_GET(mib2.ipinunknownprotos); 00094 return sizeof(*uint_ptr); 00095 case 8: /* ipInDiscards */ 00096 *uint_ptr = STATS_GET(mib2.ipindiscards); 00097 return sizeof(*uint_ptr); 00098 case 9: /* ipInDelivers */ 00099 *uint_ptr = STATS_GET(mib2.ipindelivers); 00100 return sizeof(*uint_ptr); 00101 case 10: /* ipOutRequests */ 00102 *uint_ptr = STATS_GET(mib2.ipoutrequests); 00103 return sizeof(*uint_ptr); 00104 case 11: /* ipOutDiscards */ 00105 *uint_ptr = STATS_GET(mib2.ipoutdiscards); 00106 return sizeof(*uint_ptr); 00107 case 12: /* ipOutNoRoutes */ 00108 *uint_ptr = STATS_GET(mib2.ipoutnoroutes); 00109 return sizeof(*uint_ptr); 00110 case 13: /* ipReasmTimeout */ 00111 #if IP_REASSEMBLY 00112 *sint_ptr = IP_REASS_MAXAGE; 00113 #else 00114 *sint_ptr = 0; 00115 #endif 00116 return sizeof(*sint_ptr); 00117 case 14: /* ipReasmReqds */ 00118 *uint_ptr = STATS_GET(mib2.ipreasmreqds); 00119 return sizeof(*uint_ptr); 00120 case 15: /* ipReasmOKs */ 00121 *uint_ptr = STATS_GET(mib2.ipreasmoks); 00122 return sizeof(*uint_ptr); 00123 case 16: /* ipReasmFails */ 00124 *uint_ptr = STATS_GET(mib2.ipreasmfails); 00125 return sizeof(*uint_ptr); 00126 case 17: /* ipFragOKs */ 00127 *uint_ptr = STATS_GET(mib2.ipfragoks); 00128 return sizeof(*uint_ptr); 00129 case 18: /* ipFragFails */ 00130 *uint_ptr = STATS_GET(mib2.ipfragfails); 00131 return sizeof(*uint_ptr); 00132 case 19: /* ipFragCreates */ 00133 *uint_ptr = STATS_GET(mib2.ipfragcreates); 00134 return sizeof(*uint_ptr); 00135 case 23: /* ipRoutingDiscards: not supported -> always 0 */ 00136 *uint_ptr = 0; 00137 return sizeof(*uint_ptr); 00138 default: 00139 LWIP_DEBUGF(SNMP_MIB_DEBUG, ("ip_get_value(): unknown id: %"S32_F"\n", instance->node->oid)); 00140 break; 00141 } 00142 00143 return 0; 00144 } 00145 00146 /** 00147 * Test ip object value before setting. 00148 * 00149 * @param instance node instance 00150 * @param len return value space (in bytes) 00151 * @param value points to (varbind) space to copy value from. 00152 * 00153 * @note we allow set if the value matches the hardwired value, 00154 * otherwise return badvalue. 00155 */ 00156 static snmp_err_t 00157 ip_set_test(struct snmp_node_instance *instance, u16_t len, void *value) 00158 { 00159 snmp_err_t ret = SNMP_ERR_WRONGVALUE; 00160 s32_t *sint_ptr = (s32_t *)value; 00161 00162 LWIP_UNUSED_ARG(len); 00163 switch (instance->node->oid) { 00164 case 1: /* ipForwarding */ 00165 #if IP_FORWARD 00166 /* forwarding */ 00167 if (*sint_ptr == 1) 00168 #else 00169 /* not-forwarding */ 00170 if (*sint_ptr == 2) 00171 #endif 00172 { 00173 ret = SNMP_ERR_NOERROR; 00174 } 00175 break; 00176 case 2: /* ipDefaultTTL */ 00177 if (*sint_ptr == IP_DEFAULT_TTL) { 00178 ret = SNMP_ERR_NOERROR; 00179 } 00180 break; 00181 default: 00182 LWIP_DEBUGF(SNMP_MIB_DEBUG, ("ip_set_test(): unknown id: %"S32_F"\n", instance->node->oid)); 00183 break; 00184 } 00185 00186 return ret; 00187 } 00188 00189 static snmp_err_t 00190 ip_set_value(struct snmp_node_instance *instance, u16_t len, void *value) 00191 { 00192 LWIP_UNUSED_ARG(instance); 00193 LWIP_UNUSED_ARG(len); 00194 LWIP_UNUSED_ARG(value); 00195 /* 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 */ 00196 return SNMP_ERR_NOERROR; 00197 } 00198 00199 /* --- ipAddrTable --- */ 00200 00201 /* list of allowed value ranges for incoming OID */ 00202 static const struct snmp_oid_range ip_AddrTable_oid_ranges[] = { 00203 { 0, 0xff }, /* IP A */ 00204 { 0, 0xff }, /* IP B */ 00205 { 0, 0xff }, /* IP C */ 00206 { 0, 0xff } /* IP D */ 00207 }; 00208 00209 static snmp_err_t 00210 ip_AddrTable_get_cell_value_core (struct netif *netif, const u32_t *column, union snmp_variant_value *value, u32_t *value_len) 00211 { 00212 LWIP_UNUSED_ARG(value_len); 00213 00214 switch (*column) { 00215 case 1: /* ipAdEntAddr */ 00216 value->u32 = netif_ip4_addr(netif)->addr; 00217 break; 00218 case 2: /* ipAdEntIfIndex */ 00219 value->u32 = netif_to_num(netif); 00220 break; 00221 case 3: /* ipAdEntNetMask */ 00222 value->u32 = netif_ip4_netmask(netif)->addr; 00223 break; 00224 case 4: /* ipAdEntBcastAddr */ 00225 /* lwIP oddity, there's no broadcast 00226 address in the netif we can rely on */ 00227 value->u32 = IPADDR_BROADCAST & 1; 00228 break; 00229 case 5: /* ipAdEntReasmMaxSize */ 00230 #if IP_REASSEMBLY 00231 /* @todo The theoretical maximum is IP_REASS_MAX_PBUFS * size of the pbufs, 00232 * but only if receiving one fragmented packet at a time. 00233 * The current solution is to calculate for 2 simultaneous packets... 00234 */ 00235 value->u32 = (IP_HLEN + ((IP_REASS_MAX_PBUFS / 2) * 00236 (PBUF_POOL_BUFSIZE - PBUF_LINK_ENCAPSULATION_HLEN - PBUF_LINK_HLEN - IP_HLEN))); 00237 #else 00238 /** @todo returning MTU would be a bad thing and 00239 returning a wild guess like '576' isn't good either */ 00240 value->u32 = 0; 00241 #endif 00242 break; 00243 default: 00244 return SNMP_ERR_NOSUCHINSTANCE; 00245 } 00246 00247 return SNMP_ERR_NOERROR; 00248 } 00249 00250 static snmp_err_t 00251 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) 00252 { 00253 ip4_addr_t ip; 00254 struct netif *netif; 00255 00256 /* check if incoming OID length and if values are in plausible range */ 00257 if (!snmp_oid_in_range(row_oid, row_oid_len, ip_AddrTable_oid_ranges, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges))) { 00258 return SNMP_ERR_NOSUCHINSTANCE; 00259 } 00260 00261 /* get IP from incoming OID */ 00262 snmp_oid_to_ip4(&row_oid[0], &ip); /* we know it succeeds because of oid_in_range check above */ 00263 00264 /* find netif with requested ip */ 00265 NETIF_FOREACH(netif) { 00266 if (ip4_addr_cmp(&ip, netif_ip4_addr(netif))) { 00267 /* fill in object properties */ 00268 return ip_AddrTable_get_cell_value_core (netif, column, value, value_len); 00269 } 00270 } 00271 00272 /* not found */ 00273 return SNMP_ERR_NOSUCHINSTANCE; 00274 } 00275 00276 static snmp_err_t 00277 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) 00278 { 00279 struct netif *netif; 00280 struct snmp_next_oid_state state; 00281 u32_t result_temp[LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)]; 00282 00283 /* init struct to search next oid */ 00284 snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)); 00285 00286 /* iterate over all possible OIDs to find the next one */ 00287 NETIF_FOREACH(netif) { 00288 u32_t test_oid[LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)]; 00289 snmp_ip4_to_oid(netif_ip4_addr(netif), &test_oid[0]); 00290 00291 /* check generated OID: is it a candidate for the next one? */ 00292 snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges), netif); 00293 } 00294 00295 /* did we find a next one? */ 00296 if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) { 00297 snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len); 00298 /* fill in object properties */ 00299 return ip_AddrTable_get_cell_value_core ((struct netif *)state.reference, column, value, value_len); 00300 } 00301 00302 /* not found */ 00303 return SNMP_ERR_NOSUCHINSTANCE; 00304 } 00305 00306 /* --- ipRouteTable --- */ 00307 00308 /* list of allowed value ranges for incoming OID */ 00309 static const struct snmp_oid_range ip_RouteTable_oid_ranges[] = { 00310 { 0, 0xff }, /* IP A */ 00311 { 0, 0xff }, /* IP B */ 00312 { 0, 0xff }, /* IP C */ 00313 { 0, 0xff }, /* IP D */ 00314 }; 00315 00316 static snmp_err_t 00317 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) 00318 { 00319 switch (*column) { 00320 case 1: /* ipRouteDest */ 00321 if (default_route) { 00322 /* default rte has 0.0.0.0 dest */ 00323 value->u32 = IP4_ADDR_ANY4->addr; 00324 } else { 00325 /* netifs have netaddress dest */ 00326 ip4_addr_t tmp; 00327 ip4_addr_get_network(&tmp, netif_ip4_addr(netif), netif_ip4_netmask(netif)); 00328 value->u32 = tmp.addr; 00329 } 00330 break; 00331 case 2: /* ipRouteIfIndex */ 00332 value->u32 = netif_to_num(netif); 00333 break; 00334 case 3: /* ipRouteMetric1 */ 00335 if (default_route) { 00336 value->s32 = 1; /* default */ 00337 } else { 00338 value->s32 = 0; /* normal */ 00339 } 00340 break; 00341 case 4: /* ipRouteMetric2 */ 00342 case 5: /* ipRouteMetric3 */ 00343 case 6: /* ipRouteMetric4 */ 00344 value->s32 = -1; /* none */ 00345 break; 00346 case 7: /* ipRouteNextHop */ 00347 if (default_route) { 00348 /* default rte: gateway */ 00349 value->u32 = netif_ip4_gw(netif)->addr; 00350 } else { 00351 /* other rtes: netif ip_addr */ 00352 value->u32 = netif_ip4_addr(netif)->addr; 00353 } 00354 break; 00355 case 8: /* ipRouteType */ 00356 if (default_route) { 00357 /* default rte is indirect */ 00358 value->u32 = 4; /* indirect */ 00359 } else { 00360 /* other rtes are direct */ 00361 value->u32 = 3; /* direct */ 00362 } 00363 break; 00364 case 9: /* ipRouteProto */ 00365 /* locally defined routes */ 00366 value->u32 = 2; /* local */ 00367 break; 00368 case 10: /* ipRouteAge */ 00369 /* @todo (sysuptime - timestamp last change) / 100 */ 00370 value->u32 = 0; 00371 break; 00372 case 11: /* ipRouteMask */ 00373 if (default_route) { 00374 /* default rte use 0.0.0.0 mask */ 00375 value->u32 = IP4_ADDR_ANY4->addr; 00376 } else { 00377 /* other rtes use netmask */ 00378 value->u32 = netif_ip4_netmask(netif)->addr; 00379 } 00380 break; 00381 case 12: /* ipRouteMetric5 */ 00382 value->s32 = -1; /* none */ 00383 break; 00384 case 13: /* ipRouteInfo */ 00385 value->const_ptr = snmp_zero_dot_zero.id; 00386 *value_len = snmp_zero_dot_zero.len * sizeof(u32_t); 00387 break; 00388 default: 00389 return SNMP_ERR_NOSUCHINSTANCE; 00390 } 00391 00392 return SNMP_ERR_NOERROR; 00393 } 00394 00395 static snmp_err_t 00396 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) 00397 { 00398 ip4_addr_t test_ip; 00399 struct netif *netif; 00400 00401 /* check if incoming OID length and if values are in plausible range */ 00402 if (!snmp_oid_in_range(row_oid, row_oid_len, ip_RouteTable_oid_ranges, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges))) { 00403 return SNMP_ERR_NOSUCHINSTANCE; 00404 } 00405 00406 /* get IP and port from incoming OID */ 00407 snmp_oid_to_ip4(&row_oid[0], &test_ip); /* we know it succeeds because of oid_in_range check above */ 00408 00409 /* default route is on default netif */ 00410 if (ip4_addr_isany_val(test_ip) && (netif_default != NULL)) { 00411 /* fill in object properties */ 00412 return ip_RouteTable_get_cell_value_core(netif_default, 1, column, value, value_len); 00413 } 00414 00415 /* find netif with requested route */ 00416 NETIF_FOREACH(netif) { 00417 ip4_addr_t dst; 00418 ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif)); 00419 00420 if (ip4_addr_cmp(&dst, &test_ip)) { 00421 /* fill in object properties */ 00422 return ip_RouteTable_get_cell_value_core(netif, 0, column, value, value_len); 00423 } 00424 } 00425 00426 /* not found */ 00427 return SNMP_ERR_NOSUCHINSTANCE; 00428 } 00429 00430 static snmp_err_t 00431 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) 00432 { 00433 struct netif *netif; 00434 struct snmp_next_oid_state state; 00435 u32_t result_temp[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)]; 00436 u32_t test_oid[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)]; 00437 00438 /* init struct to search next oid */ 00439 snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)); 00440 00441 /* check default route */ 00442 if (netif_default != NULL) { 00443 snmp_ip4_to_oid(IP4_ADDR_ANY4, &test_oid[0]); 00444 snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif_default); 00445 } 00446 00447 /* iterate over all possible OIDs to find the next one */ 00448 NETIF_FOREACH(netif) { 00449 ip4_addr_t dst; 00450 ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif)); 00451 00452 /* check generated OID: is it a candidate for the next one? */ 00453 if (!ip4_addr_isany_val(dst)) { 00454 snmp_ip4_to_oid(&dst, &test_oid[0]); 00455 snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif); 00456 } 00457 } 00458 00459 /* did we find a next one? */ 00460 if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) { 00461 ip4_addr_t dst; 00462 snmp_oid_to_ip4(&result_temp[0], &dst); 00463 snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len); 00464 /* fill in object properties */ 00465 return ip_RouteTable_get_cell_value_core((struct netif *)state.reference, ip4_addr_isany_val(dst), column, value, value_len); 00466 } else { 00467 /* not found */ 00468 return SNMP_ERR_NOSUCHINSTANCE; 00469 } 00470 } 00471 00472 #if LWIP_ARP && LWIP_IPV4 00473 /* --- ipNetToMediaTable --- */ 00474 00475 /* list of allowed value ranges for incoming OID */ 00476 static const struct snmp_oid_range ip_NetToMediaTable_oid_ranges[] = { 00477 { 1, 0xff }, /* IfIndex */ 00478 { 0, 0xff }, /* IP A */ 00479 { 0, 0xff }, /* IP B */ 00480 { 0, 0xff }, /* IP C */ 00481 { 0, 0xff } /* IP D */ 00482 }; 00483 00484 static snmp_err_t 00485 ip_NetToMediaTable_get_cell_value_core(size_t arp_table_index, const u32_t *column, union snmp_variant_value *value, u32_t *value_len) 00486 { 00487 ip4_addr_t *ip; 00488 struct netif *netif; 00489 struct eth_addr *ethaddr; 00490 00491 etharp_get_entry(arp_table_index, &ip, &netif, ðaddr); 00492 00493 /* value */ 00494 switch (*column) { 00495 case 1: /* atIfIndex / ipNetToMediaIfIndex */ 00496 value->u32 = netif_to_num(netif); 00497 break; 00498 case 2: /* atPhysAddress / ipNetToMediaPhysAddress */ 00499 value->ptr = ethaddr; 00500 *value_len = sizeof(*ethaddr); 00501 break; 00502 case 3: /* atNetAddress / ipNetToMediaNetAddress */ 00503 value->u32 = ip->addr; 00504 break; 00505 case 4: /* ipNetToMediaType */ 00506 value->u32 = 3; /* dynamic*/ 00507 break; 00508 default: 00509 return SNMP_ERR_NOSUCHINSTANCE; 00510 } 00511 00512 return SNMP_ERR_NOERROR; 00513 } 00514 00515 static snmp_err_t 00516 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) 00517 { 00518 ip4_addr_t ip_in; 00519 u8_t netif_index; 00520 size_t i; 00521 00522 /* check if incoming OID length and if values are in plausible range */ 00523 if (!snmp_oid_in_range(row_oid, row_oid_len, ip_NetToMediaTable_oid_ranges, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges))) { 00524 return SNMP_ERR_NOSUCHINSTANCE; 00525 } 00526 00527 /* get IP from incoming OID */ 00528 netif_index = (u8_t)row_oid[0]; 00529 snmp_oid_to_ip4(&row_oid[1], &ip_in); /* we know it succeeds because of oid_in_range check above */ 00530 00531 /* find requested entry */ 00532 for (i = 0; i < ARP_TABLE_SIZE; i++) { 00533 ip4_addr_t *ip; 00534 struct netif *netif; 00535 struct eth_addr *ethaddr; 00536 00537 if (etharp_get_entry(i, &ip, &netif, ðaddr)) { 00538 if ((netif_index == netif_to_num(netif)) && ip4_addr_cmp(&ip_in, ip)) { 00539 /* fill in object properties */ 00540 return ip_NetToMediaTable_get_cell_value_core(i, column, value, value_len); 00541 } 00542 } 00543 } 00544 00545 /* not found */ 00546 return SNMP_ERR_NOSUCHINSTANCE; 00547 } 00548 00549 static snmp_err_t 00550 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) 00551 { 00552 size_t i; 00553 struct snmp_next_oid_state state; 00554 u32_t result_temp[LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)]; 00555 00556 /* init struct to search next oid */ 00557 snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)); 00558 00559 /* iterate over all possible OIDs to find the next one */ 00560 for (i = 0; i < ARP_TABLE_SIZE; i++) { 00561 ip4_addr_t *ip; 00562 struct netif *netif; 00563 struct eth_addr *ethaddr; 00564 00565 if (etharp_get_entry(i, &ip, &netif, ðaddr)) { 00566 u32_t test_oid[LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)]; 00567 00568 test_oid[0] = netif_to_num(netif); 00569 snmp_ip4_to_oid(ip, &test_oid[1]); 00570 00571 /* check generated OID: is it a candidate for the next one? */ 00572 snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges), LWIP_PTR_NUMERIC_CAST(void *, i)); 00573 } 00574 } 00575 00576 /* did we find a next one? */ 00577 if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) { 00578 snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len); 00579 /* fill in object properties */ 00580 return ip_NetToMediaTable_get_cell_value_core(LWIP_PTR_NUMERIC_CAST(size_t, state.reference), column, value, value_len); 00581 } 00582 00583 /* not found */ 00584 return SNMP_ERR_NOSUCHINSTANCE; 00585 } 00586 00587 #endif /* LWIP_ARP && LWIP_IPV4 */ 00588 00589 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); 00590 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); 00591 static const struct snmp_scalar_node ip_InReceives = SNMP_SCALAR_CREATE_NODE_READONLY(3, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00592 static const struct snmp_scalar_node ip_InHdrErrors = SNMP_SCALAR_CREATE_NODE_READONLY(4, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00593 static const struct snmp_scalar_node ip_InAddrErrors = SNMP_SCALAR_CREATE_NODE_READONLY(5, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00594 static const struct snmp_scalar_node ip_ForwDatagrams = SNMP_SCALAR_CREATE_NODE_READONLY(6, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00595 static const struct snmp_scalar_node ip_InUnknownProtos = SNMP_SCALAR_CREATE_NODE_READONLY(7, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00596 static const struct snmp_scalar_node ip_InDiscards = SNMP_SCALAR_CREATE_NODE_READONLY(8, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00597 static const struct snmp_scalar_node ip_InDelivers = SNMP_SCALAR_CREATE_NODE_READONLY(9, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00598 static const struct snmp_scalar_node ip_OutRequests = SNMP_SCALAR_CREATE_NODE_READONLY(10, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00599 static const struct snmp_scalar_node ip_OutDiscards = SNMP_SCALAR_CREATE_NODE_READONLY(11, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00600 static const struct snmp_scalar_node ip_OutNoRoutes = SNMP_SCALAR_CREATE_NODE_READONLY(12, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00601 static const struct snmp_scalar_node ip_ReasmTimeout = SNMP_SCALAR_CREATE_NODE_READONLY(13, SNMP_ASN1_TYPE_INTEGER, ip_get_value); 00602 static const struct snmp_scalar_node ip_ReasmReqds = SNMP_SCALAR_CREATE_NODE_READONLY(14, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00603 static const struct snmp_scalar_node ip_ReasmOKs = SNMP_SCALAR_CREATE_NODE_READONLY(15, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00604 static const struct snmp_scalar_node ip_ReasmFails = SNMP_SCALAR_CREATE_NODE_READONLY(16, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00605 static const struct snmp_scalar_node ip_FragOKs = SNMP_SCALAR_CREATE_NODE_READONLY(17, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00606 static const struct snmp_scalar_node ip_FragFails = SNMP_SCALAR_CREATE_NODE_READONLY(18, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00607 static const struct snmp_scalar_node ip_FragCreates = SNMP_SCALAR_CREATE_NODE_READONLY(19, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00608 static const struct snmp_scalar_node ip_RoutingDiscards = SNMP_SCALAR_CREATE_NODE_READONLY(23, SNMP_ASN1_TYPE_COUNTER, ip_get_value); 00609 00610 static const struct snmp_table_simple_col_def ip_AddrTable_columns[] = { 00611 { 1, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntAddr */ 00612 { 2, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntIfIndex */ 00613 { 3, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntNetMask */ 00614 { 4, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipAdEntBcastAddr */ 00615 { 5, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 } /* ipAdEntReasmMaxSize */ 00616 }; 00617 00618 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); 00619 00620 static const struct snmp_table_simple_col_def ip_RouteTable_columns[] = { 00621 { 1, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteDest */ 00622 { 2, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteIfIndex */ 00623 { 3, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric1 */ 00624 { 4, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric2 */ 00625 { 5, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric3 */ 00626 { 6, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric4 */ 00627 { 7, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteNextHop */ 00628 { 8, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteType */ 00629 { 9, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteProto */ 00630 { 10, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteAge */ 00631 { 11, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipRouteMask */ 00632 { 12, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_S32 }, /* ipRouteMetric5 */ 00633 { 13, SNMP_ASN1_TYPE_OBJECT_ID, SNMP_VARIANT_VALUE_TYPE_PTR } /* ipRouteInfo */ 00634 }; 00635 00636 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); 00637 #endif /* LWIP_IPV4 */ 00638 00639 #if LWIP_ARP && LWIP_IPV4 00640 static const struct snmp_table_simple_col_def ip_NetToMediaTable_columns[] = { 00641 { 1, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipNetToMediaIfIndex */ 00642 { 2, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_VARIANT_VALUE_TYPE_PTR }, /* ipNetToMediaPhysAddress */ 00643 { 3, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 }, /* ipNetToMediaNetAddress */ 00644 { 4, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 } /* ipNetToMediaType */ 00645 }; 00646 00647 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); 00648 #endif /* LWIP_ARP && LWIP_IPV4 */ 00649 00650 #if LWIP_IPV4 00651 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */ 00652 CREATE_LWIP_SYNC_NODE( 1, ip_Forwarding) 00653 CREATE_LWIP_SYNC_NODE( 2, ip_DefaultTTL) 00654 CREATE_LWIP_SYNC_NODE( 3, ip_InReceives) 00655 CREATE_LWIP_SYNC_NODE( 4, ip_InHdrErrors) 00656 CREATE_LWIP_SYNC_NODE( 5, ip_InAddrErrors) 00657 CREATE_LWIP_SYNC_NODE( 6, ip_ForwDatagrams) 00658 CREATE_LWIP_SYNC_NODE( 7, ip_InUnknownProtos) 00659 CREATE_LWIP_SYNC_NODE( 8, ip_InDiscards) 00660 CREATE_LWIP_SYNC_NODE( 9, ip_InDelivers) 00661 CREATE_LWIP_SYNC_NODE(10, ip_OutRequests) 00662 CREATE_LWIP_SYNC_NODE(11, ip_OutDiscards) 00663 CREATE_LWIP_SYNC_NODE(12, ip_OutNoRoutes) 00664 CREATE_LWIP_SYNC_NODE(13, ip_ReasmTimeout) 00665 CREATE_LWIP_SYNC_NODE(14, ip_ReasmReqds) 00666 CREATE_LWIP_SYNC_NODE(15, ip_ReasmOKs) 00667 CREATE_LWIP_SYNC_NODE(15, ip_ReasmFails) 00668 CREATE_LWIP_SYNC_NODE(17, ip_FragOKs) 00669 CREATE_LWIP_SYNC_NODE(18, ip_FragFails) 00670 CREATE_LWIP_SYNC_NODE(19, ip_FragCreates) 00671 CREATE_LWIP_SYNC_NODE(20, ip_AddrTable) 00672 CREATE_LWIP_SYNC_NODE(21, ip_RouteTable) 00673 #if LWIP_ARP 00674 CREATE_LWIP_SYNC_NODE(22, ip_NetToMediaTable) 00675 #endif /* LWIP_ARP */ 00676 CREATE_LWIP_SYNC_NODE(23, ip_RoutingDiscards) 00677 00678 static const struct snmp_node *const ip_nodes[] = { 00679 &SYNC_NODE_NAME(ip_Forwarding).node.node, 00680 &SYNC_NODE_NAME(ip_DefaultTTL).node.node, 00681 &SYNC_NODE_NAME(ip_InReceives).node.node, 00682 &SYNC_NODE_NAME(ip_InHdrErrors).node.node, 00683 &SYNC_NODE_NAME(ip_InAddrErrors).node.node, 00684 &SYNC_NODE_NAME(ip_ForwDatagrams).node.node, 00685 &SYNC_NODE_NAME(ip_InUnknownProtos).node.node, 00686 &SYNC_NODE_NAME(ip_InDiscards).node.node, 00687 &SYNC_NODE_NAME(ip_InDelivers).node.node, 00688 &SYNC_NODE_NAME(ip_OutRequests).node.node, 00689 &SYNC_NODE_NAME(ip_OutDiscards).node.node, 00690 &SYNC_NODE_NAME(ip_OutNoRoutes).node.node, 00691 &SYNC_NODE_NAME(ip_ReasmTimeout).node.node, 00692 &SYNC_NODE_NAME(ip_ReasmReqds).node.node, 00693 &SYNC_NODE_NAME(ip_ReasmOKs).node.node, 00694 &SYNC_NODE_NAME(ip_ReasmFails).node.node, 00695 &SYNC_NODE_NAME(ip_FragOKs).node.node, 00696 &SYNC_NODE_NAME(ip_FragFails).node.node, 00697 &SYNC_NODE_NAME(ip_FragCreates).node.node, 00698 &SYNC_NODE_NAME(ip_AddrTable).node.node, 00699 &SYNC_NODE_NAME(ip_RouteTable).node.node, 00700 #if LWIP_ARP 00701 &SYNC_NODE_NAME(ip_NetToMediaTable).node.node, 00702 #endif /* LWIP_ARP */ 00703 &SYNC_NODE_NAME(ip_RoutingDiscards).node.node 00704 }; 00705 00706 const struct snmp_tree_node snmp_mib2_ip_root = SNMP_CREATE_TREE_NODE(4, ip_nodes); 00707 #endif /* LWIP_IPV4 */ 00708 00709 /* --- at .1.3.6.1.2.1.3 ----------------------------------------------------- */ 00710 00711 #if LWIP_ARP && LWIP_IPV4 00712 /* at node table is a subset of ip_nettomedia table (same rows but less columns) */ 00713 static const struct snmp_table_simple_col_def at_Table_columns[] = { 00714 { 1, SNMP_ASN1_TYPE_INTEGER, SNMP_VARIANT_VALUE_TYPE_U32 }, /* atIfIndex */ 00715 { 2, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_VARIANT_VALUE_TYPE_PTR }, /* atPhysAddress */ 00716 { 3, SNMP_ASN1_TYPE_IPADDR, SNMP_VARIANT_VALUE_TYPE_U32 } /* atNetAddress */ 00717 }; 00718 00719 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); 00720 00721 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */ 00722 CREATE_LWIP_SYNC_NODE(1, at_Table) 00723 00724 static const struct snmp_node *const at_nodes[] = { 00725 &SYNC_NODE_NAME(at_Table).node.node 00726 }; 00727 00728 const struct snmp_tree_node snmp_mib2_at_root = SNMP_CREATE_TREE_NODE(3, at_nodes); 00729 #endif /* LWIP_ARP && LWIP_IPV4 */ 00730 00731 #endif /* LWIP_SNMP && SNMP_LWIP_MIB2 */
Generated on Tue Jul 12 2022 13:54:29 by
