Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_snmp_mib2_interfaces.c Source File

lwip_snmp_mib2_interfaces.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Management Information Base II (RFC1213) INTERFACES 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/netif.h"
00043 #include "lwip/stats.h"
00044 
00045 #include <string.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 
00059 /* --- interfaces .1.3.6.1.2.1.2 ----------------------------------------------------- */
00060 
00061 static s16_t
00062 interfaces_get_value(struct snmp_node_instance *instance, void *value)
00063 {
00064   if (instance->node->oid == 1) {
00065     s32_t *sint_ptr = (s32_t *)value;
00066     s32_t num_netifs = 0;
00067 
00068     struct netif *netif;
00069     NETIF_FOREACH(netif) {
00070       num_netifs++;
00071     }
00072 
00073     *sint_ptr = num_netifs;
00074     return sizeof(*sint_ptr);
00075   }
00076 
00077   return 0;
00078 }
00079 
00080 /* list of allowed value ranges for incoming OID */
00081 static const struct snmp_oid_range interfaces_Table_oid_ranges[] = {
00082   { 1, 0xff } /* netif->num is u8_t */
00083 };
00084 
00085 static const u8_t iftable_ifOutQLen         = 0;
00086 
00087 static const u8_t iftable_ifOperStatus_up   = 1;
00088 static const u8_t iftable_ifOperStatus_down = 2;
00089 
00090 static const u8_t iftable_ifAdminStatus_up             = 1;
00091 static const u8_t iftable_ifAdminStatus_lowerLayerDown = 7;
00092 static const u8_t iftable_ifAdminStatus_down           = 2;
00093 
00094 static snmp_err_t
00095 interfaces_Table_get_cell_instance(const u32_t *column, const u32_t *row_oid, u8_t row_oid_len, struct snmp_node_instance *cell_instance)
00096 {
00097   u32_t ifIndex;
00098   struct netif *netif;
00099 
00100   LWIP_UNUSED_ARG(column);
00101 
00102   /* check if incoming OID length and if values are in plausible range */
00103   if (!snmp_oid_in_range(row_oid, row_oid_len, interfaces_Table_oid_ranges, LWIP_ARRAYSIZE(interfaces_Table_oid_ranges))) {
00104     return SNMP_ERR_NOSUCHINSTANCE;
00105   }
00106 
00107   /* get netif index from incoming OID */
00108   ifIndex = row_oid[0];
00109 
00110   /* find netif with index */
00111   NETIF_FOREACH(netif) {
00112     if (netif_to_num(netif) == ifIndex) {
00113       /* store netif pointer for subsequent operations (get/test/set) */
00114       cell_instance->reference.ptr = netif;
00115       return SNMP_ERR_NOERROR;
00116     }
00117   }
00118 
00119   /* not found */
00120   return SNMP_ERR_NOSUCHINSTANCE;
00121 }
00122 
00123 static snmp_err_t
00124 interfaces_Table_get_next_cell_instance(const u32_t *column, struct snmp_obj_id *row_oid, struct snmp_node_instance *cell_instance)
00125 {
00126   struct netif *netif;
00127   struct snmp_next_oid_state state;
00128   u32_t result_temp[LWIP_ARRAYSIZE(interfaces_Table_oid_ranges)];
00129 
00130   LWIP_UNUSED_ARG(column);
00131 
00132   /* init struct to search next oid */
00133   snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(interfaces_Table_oid_ranges));
00134 
00135   /* iterate over all possible OIDs to find the next one */
00136   NETIF_FOREACH(netif) {
00137     u32_t test_oid[LWIP_ARRAYSIZE(interfaces_Table_oid_ranges)];
00138     test_oid[0] = netif_to_num(netif);
00139 
00140     /* check generated OID: is it a candidate for the next one? */
00141     snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(interfaces_Table_oid_ranges), netif);
00142   }
00143 
00144   /* did we find a next one? */
00145   if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
00146     snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
00147     /* store netif pointer for subsequent operations (get/test/set) */
00148     cell_instance->reference.ptr = /* (struct netif*) */state.reference;
00149     return SNMP_ERR_NOERROR;
00150   }
00151 
00152   /* not found */
00153   return SNMP_ERR_NOSUCHINSTANCE;
00154 }
00155 
00156 static s16_t
00157 interfaces_Table_get_value (struct snmp_node_instance *instance, void *value)
00158 {
00159   struct netif *netif = (struct netif *)instance->reference.ptr;
00160   u32_t *value_u32 = (u32_t *)value;
00161   s32_t *value_s32 = (s32_t *)value;
00162   u16_t value_len;
00163 
00164   switch (SNMP_TABLE_GET_COLUMN_FROM_OID(instance->instance_oid.id)) {
00165     case 1: /* ifIndex */
00166       *value_s32 = netif_to_num(netif);
00167       value_len = sizeof(*value_s32);
00168       break;
00169     case 2: /* ifDescr */
00170       value_len = sizeof(netif->name);
00171       MEMCPY(value, netif->name, value_len);
00172       break;
00173     case 3: /* ifType */
00174       *value_s32 = netif->link_type;
00175       value_len = sizeof(*value_s32);
00176       break;
00177     case 4: /* ifMtu */
00178       *value_s32 = netif->mtu;
00179       value_len = sizeof(*value_s32);
00180       break;
00181     case 5: /* ifSpeed */
00182       *value_u32 = netif->link_speed;
00183       value_len = sizeof(*value_u32);
00184       break;
00185     case 6: /* ifPhysAddress */
00186       value_len = sizeof(netif->hwaddr);
00187       MEMCPY(value, &netif->hwaddr, value_len);
00188       break;
00189     case 7: /* ifAdminStatus */
00190       if (netif_is_up(netif)) {
00191         *value_s32 = iftable_ifOperStatus_up;
00192       } else {
00193         *value_s32 = iftable_ifOperStatus_down;
00194       }
00195       value_len = sizeof(*value_s32);
00196       break;
00197     case 8: /* ifOperStatus */
00198       if (netif_is_up(netif)) {
00199         if (netif_is_link_up(netif)) {
00200           *value_s32 = iftable_ifAdminStatus_up;
00201         } else {
00202           *value_s32 = iftable_ifAdminStatus_lowerLayerDown;
00203         }
00204       } else {
00205         *value_s32 = iftable_ifAdminStatus_down;
00206       }
00207       value_len = sizeof(*value_s32);
00208       break;
00209     case 9: /* ifLastChange */
00210       *value_u32 = netif->ts;
00211       value_len = sizeof(*value_u32);
00212       break;
00213     case 10: /* ifInOctets */
00214       *value_u32 = netif->mib2_counters.ifinoctets;
00215       value_len = sizeof(*value_u32);
00216       break;
00217     case 11: /* ifInUcastPkts */
00218       *value_u32 = netif->mib2_counters.ifinucastpkts;
00219       value_len = sizeof(*value_u32);
00220       break;
00221     case 12: /* ifInNUcastPkts */
00222       *value_u32 = netif->mib2_counters.ifinnucastpkts;
00223       value_len = sizeof(*value_u32);
00224       break;
00225     case 13: /* ifInDiscards */
00226       *value_u32 = netif->mib2_counters.ifindiscards;
00227       value_len = sizeof(*value_u32);
00228       break;
00229     case 14: /* ifInErrors */
00230       *value_u32 = netif->mib2_counters.ifinerrors;
00231       value_len = sizeof(*value_u32);
00232       break;
00233     case 15: /* ifInUnkownProtos */
00234       *value_u32 = netif->mib2_counters.ifinunknownprotos;
00235       value_len = sizeof(*value_u32);
00236       break;
00237     case 16: /* ifOutOctets */
00238       *value_u32 = netif->mib2_counters.ifoutoctets;
00239       value_len = sizeof(*value_u32);
00240       break;
00241     case 17: /* ifOutUcastPkts */
00242       *value_u32 = netif->mib2_counters.ifoutucastpkts;
00243       value_len = sizeof(*value_u32);
00244       break;
00245     case 18: /* ifOutNUcastPkts */
00246       *value_u32 = netif->mib2_counters.ifoutnucastpkts;
00247       value_len = sizeof(*value_u32);
00248       break;
00249     case 19: /* ifOutDiscarts */
00250       *value_u32 = netif->mib2_counters.ifoutdiscards;
00251       value_len = sizeof(*value_u32);
00252       break;
00253     case 20: /* ifOutErrors */
00254       *value_u32 = netif->mib2_counters.ifouterrors;
00255       value_len = sizeof(*value_u32);
00256       break;
00257     case 21: /* ifOutQLen */
00258       *value_u32 = iftable_ifOutQLen;
00259       value_len = sizeof(*value_u32);
00260       break;
00261     /** @note returning zeroDotZero (0.0) no media specific MIB support */
00262     case 22: /* ifSpecific */
00263       value_len = snmp_zero_dot_zero.len * sizeof(u32_t);
00264       MEMCPY(value, snmp_zero_dot_zero.id, value_len);
00265       break;
00266     default:
00267       return 0;
00268   }
00269 
00270   return value_len;
00271 }
00272 
00273 #if !SNMP_SAFE_REQUESTS
00274 
00275 static snmp_err_t
00276 interfaces_Table_set_test(struct snmp_node_instance *instance, u16_t len, void *value)
00277 {
00278   s32_t *sint_ptr = (s32_t *)value;
00279 
00280   /* stack should never call this method for another column,
00281   because all other columns are set to readonly */
00282   LWIP_ASSERT("Invalid column", (SNMP_TABLE_GET_COLUMN_FROM_OID(instance->instance_oid.id) == 7));
00283   LWIP_UNUSED_ARG(len);
00284 
00285   if (*sint_ptr == 1 || *sint_ptr == 2) {
00286     return SNMP_ERR_NOERROR;
00287   }
00288 
00289   return SNMP_ERR_WRONGVALUE;
00290 }
00291 
00292 static snmp_err_t
00293 interfaces_Table_set_value(struct snmp_node_instance *instance, u16_t len, void *value)
00294 {
00295   struct netif *netif = (struct netif *)instance->reference.ptr;
00296   s32_t *sint_ptr = (s32_t *)value;
00297 
00298   /* stack should never call this method for another column,
00299   because all other columns are set to readonly */
00300   LWIP_ASSERT("Invalid column", (SNMP_TABLE_GET_COLUMN_FROM_OID(instance->instance_oid.id) == 7));
00301   LWIP_UNUSED_ARG(len);
00302 
00303   if (*sint_ptr == 1) {
00304     netif_set_up(netif);
00305   } else if (*sint_ptr == 2) {
00306     netif_set_down(netif);
00307   }
00308 
00309   return SNMP_ERR_NOERROR;
00310 }
00311 
00312 #endif /* SNMP_SAFE_REQUESTS */
00313 
00314 static const struct snmp_scalar_node interfaces_Number = SNMP_SCALAR_CREATE_NODE_READONLY(1, SNMP_ASN1_TYPE_INTEGER, interfaces_get_value);
00315 
00316 static const struct snmp_table_col_def interfaces_Table_columns[] = {
00317   {  1, SNMP_ASN1_TYPE_INTEGER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifIndex */
00318   {  2, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_NODE_INSTANCE_READ_ONLY }, /* ifDescr */
00319   {  3, SNMP_ASN1_TYPE_INTEGER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifType */
00320   {  4, SNMP_ASN1_TYPE_INTEGER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifMtu */
00321   {  5, SNMP_ASN1_TYPE_GAUGE,        SNMP_NODE_INSTANCE_READ_ONLY }, /* ifSpeed */
00322   {  6, SNMP_ASN1_TYPE_OCTET_STRING, SNMP_NODE_INSTANCE_READ_ONLY }, /* ifPhysAddress */
00323 #if !SNMP_SAFE_REQUESTS
00324   {  7, SNMP_ASN1_TYPE_INTEGER,      SNMP_NODE_INSTANCE_READ_WRITE }, /* ifAdminStatus */
00325 #else
00326   {  7, SNMP_ASN1_TYPE_INTEGER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifAdminStatus */
00327 #endif
00328   {  8, SNMP_ASN1_TYPE_INTEGER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifOperStatus */
00329   {  9, SNMP_ASN1_TYPE_TIMETICKS,    SNMP_NODE_INSTANCE_READ_ONLY }, /* ifLastChange */
00330   { 10, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifInOctets */
00331   { 11, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifInUcastPkts */
00332   { 12, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifInNUcastPkts */
00333   { 13, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifInDiscarts */
00334   { 14, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifInErrors */
00335   { 15, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifInUnkownProtos */
00336   { 16, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifOutOctets */
00337   { 17, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifOutUcastPkts */
00338   { 18, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifOutNUcastPkts */
00339   { 19, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifOutDiscarts */
00340   { 20, SNMP_ASN1_TYPE_COUNTER,      SNMP_NODE_INSTANCE_READ_ONLY }, /* ifOutErrors */
00341   { 21, SNMP_ASN1_TYPE_GAUGE,        SNMP_NODE_INSTANCE_READ_ONLY }, /* ifOutQLen */
00342   { 22, SNMP_ASN1_TYPE_OBJECT_ID,    SNMP_NODE_INSTANCE_READ_ONLY }  /* ifSpecific */
00343 };
00344 
00345 #if !SNMP_SAFE_REQUESTS
00346 static const struct snmp_table_node interfaces_Table = SNMP_TABLE_CREATE(
00347       2, interfaces_Table_columns,
00348       interfaces_Table_get_cell_instance, interfaces_Table_get_next_cell_instance,
00349       interfaces_Table_get_value , interfaces_Table_set_test, interfaces_Table_set_value);
00350 #else
00351 static const struct snmp_table_node interfaces_Table = SNMP_TABLE_CREATE(
00352       2, interfaces_Table_columns,
00353       interfaces_Table_get_cell_instance, interfaces_Table_get_next_cell_instance,
00354       interfaces_Table_get_value , NULL, NULL);
00355 #endif
00356 
00357 /* the following nodes access variables in LWIP stack from SNMP worker thread and must therefore be synced to LWIP (TCPIP) thread */
00358 CREATE_LWIP_SYNC_NODE(1, interfaces_Number)
00359 CREATE_LWIP_SYNC_NODE(2, interfaces_Table)
00360 
00361 static const struct snmp_node *const interface_nodes[] = {
00362   &SYNC_NODE_NAME(interfaces_Number).node.node,
00363   &SYNC_NODE_NAME(interfaces_Table).node.node
00364 };
00365 
00366 const struct snmp_tree_node snmp_mib2_interface_root = SNMP_CREATE_TREE_NODE(2, interface_nodes);
00367 
00368 #endif /* LWIP_SNMP && SNMP_LWIP_MIB2 */