1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * Management Information Base II (RFC1213) SNMP objects and functions.
4195972f6Sopenharmony_ci */
5195972f6Sopenharmony_ci
6195972f6Sopenharmony_ci/*
7195972f6Sopenharmony_ci * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
8195972f6Sopenharmony_ci * All rights reserved.
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
11195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
12195972f6Sopenharmony_ci *
13195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
14195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
15195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
16195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
17195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
18195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
19195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
20195972f6Sopenharmony_ci *
21195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30195972f6Sopenharmony_ci * OF SUCH DAMAGE.
31195972f6Sopenharmony_ci *
32195972f6Sopenharmony_ci * Author: Dirk Ziegelmeier <dziegel@gmx.de>
33195972f6Sopenharmony_ci *         Christiaan Simons <christiaan.simons@axon.tv>
34195972f6Sopenharmony_ci */
35195972f6Sopenharmony_ci
36195972f6Sopenharmony_ci#include "lwip/snmp.h"
37195972f6Sopenharmony_ci#include "lwip/apps/snmp.h"
38195972f6Sopenharmony_ci#include "lwip/apps/snmp_core.h"
39195972f6Sopenharmony_ci#include "lwip/apps/snmp_mib2.h"
40195972f6Sopenharmony_ci#include "lwip/apps/snmp_scalar.h"
41195972f6Sopenharmony_ci
42195972f6Sopenharmony_ci#if LWIP_SNMP && SNMP_LWIP_MIB2
43195972f6Sopenharmony_ci
44195972f6Sopenharmony_ci#define MIB2_AUTH_TRAPS_ENABLED  1
45195972f6Sopenharmony_ci#define MIB2_AUTH_TRAPS_DISABLED 2
46195972f6Sopenharmony_ci
47195972f6Sopenharmony_ci/* --- snmp .1.3.6.1.2.1.11 ----------------------------------------------------- */
48195972f6Sopenharmony_cistatic s16_t
49195972f6Sopenharmony_cisnmp_get_value(const struct snmp_scalar_array_node_def *node, void *value)
50195972f6Sopenharmony_ci{
51195972f6Sopenharmony_ci  u32_t *uint_ptr = (u32_t *)value;
52195972f6Sopenharmony_ci  switch (node->oid) {
53195972f6Sopenharmony_ci    case 1: /* snmpInPkts */
54195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.inpkts;
55195972f6Sopenharmony_ci      break;
56195972f6Sopenharmony_ci    case 2: /* snmpOutPkts */
57195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outpkts;
58195972f6Sopenharmony_ci      break;
59195972f6Sopenharmony_ci    case 3: /* snmpInBadVersions */
60195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.inbadversions;
61195972f6Sopenharmony_ci      break;
62195972f6Sopenharmony_ci    case 4: /* snmpInBadCommunityNames */
63195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.inbadcommunitynames;
64195972f6Sopenharmony_ci      break;
65195972f6Sopenharmony_ci    case 5: /* snmpInBadCommunityUses */
66195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.inbadcommunityuses;
67195972f6Sopenharmony_ci      break;
68195972f6Sopenharmony_ci    case 6: /* snmpInASNParseErrs */
69195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.inasnparseerrs;
70195972f6Sopenharmony_ci      break;
71195972f6Sopenharmony_ci    case 8: /* snmpInTooBigs */
72195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.intoobigs;
73195972f6Sopenharmony_ci      break;
74195972f6Sopenharmony_ci    case 9: /* snmpInNoSuchNames */
75195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.innosuchnames;
76195972f6Sopenharmony_ci      break;
77195972f6Sopenharmony_ci    case 10: /* snmpInBadValues */
78195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.inbadvalues;
79195972f6Sopenharmony_ci      break;
80195972f6Sopenharmony_ci    case 11: /* snmpInReadOnlys */
81195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.inreadonlys;
82195972f6Sopenharmony_ci      break;
83195972f6Sopenharmony_ci    case 12: /* snmpInGenErrs */
84195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.ingenerrs;
85195972f6Sopenharmony_ci      break;
86195972f6Sopenharmony_ci    case 13: /* snmpInTotalReqVars */
87195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.intotalreqvars;
88195972f6Sopenharmony_ci      break;
89195972f6Sopenharmony_ci    case 14: /* snmpInTotalSetVars */
90195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.intotalsetvars;
91195972f6Sopenharmony_ci      break;
92195972f6Sopenharmony_ci    case 15: /* snmpInGetRequests */
93195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.ingetrequests;
94195972f6Sopenharmony_ci      break;
95195972f6Sopenharmony_ci    case 16: /* snmpInGetNexts */
96195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.ingetnexts;
97195972f6Sopenharmony_ci      break;
98195972f6Sopenharmony_ci    case 17: /* snmpInSetRequests */
99195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.insetrequests;
100195972f6Sopenharmony_ci      break;
101195972f6Sopenharmony_ci    case 18: /* snmpInGetResponses */
102195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.ingetresponses;
103195972f6Sopenharmony_ci      break;
104195972f6Sopenharmony_ci    case 19: /* snmpInTraps */
105195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.intraps;
106195972f6Sopenharmony_ci      break;
107195972f6Sopenharmony_ci    case 20: /* snmpOutTooBigs */
108195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outtoobigs;
109195972f6Sopenharmony_ci      break;
110195972f6Sopenharmony_ci    case 21: /* snmpOutNoSuchNames */
111195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outnosuchnames;
112195972f6Sopenharmony_ci      break;
113195972f6Sopenharmony_ci    case 22: /* snmpOutBadValues */
114195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outbadvalues;
115195972f6Sopenharmony_ci      break;
116195972f6Sopenharmony_ci    case 24: /* snmpOutGenErrs */
117195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outgenerrs;
118195972f6Sopenharmony_ci      break;
119195972f6Sopenharmony_ci    case 25: /* snmpOutGetRequests */
120195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outgetrequests;
121195972f6Sopenharmony_ci      break;
122195972f6Sopenharmony_ci    case 26: /* snmpOutGetNexts */
123195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outgetnexts;
124195972f6Sopenharmony_ci      break;
125195972f6Sopenharmony_ci    case 27: /* snmpOutSetRequests */
126195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outsetrequests;
127195972f6Sopenharmony_ci      break;
128195972f6Sopenharmony_ci    case 28: /* snmpOutGetResponses */
129195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outgetresponses;
130195972f6Sopenharmony_ci      break;
131195972f6Sopenharmony_ci    case 29: /* snmpOutTraps */
132195972f6Sopenharmony_ci      *uint_ptr = snmp_stats.outtraps;
133195972f6Sopenharmony_ci      break;
134195972f6Sopenharmony_ci    case 30: /* snmpEnableAuthenTraps */
135195972f6Sopenharmony_ci      if (snmp_get_auth_traps_enabled() == SNMP_AUTH_TRAPS_DISABLED) {
136195972f6Sopenharmony_ci        *uint_ptr = MIB2_AUTH_TRAPS_DISABLED;
137195972f6Sopenharmony_ci      } else {
138195972f6Sopenharmony_ci        *uint_ptr = MIB2_AUTH_TRAPS_ENABLED;
139195972f6Sopenharmony_ci      }
140195972f6Sopenharmony_ci      break;
141195972f6Sopenharmony_ci    case 31: /* snmpSilentDrops */
142195972f6Sopenharmony_ci      *uint_ptr = 0; /* not supported */
143195972f6Sopenharmony_ci      break;
144195972f6Sopenharmony_ci    case 32: /* snmpProxyDrops */
145195972f6Sopenharmony_ci      *uint_ptr = 0; /* not supported */
146195972f6Sopenharmony_ci      break;
147195972f6Sopenharmony_ci    default:
148195972f6Sopenharmony_ci      LWIP_DEBUGF(SNMP_MIB_DEBUG, ("snmp_get_value(): unknown id: %"S32_F"\n", node->oid));
149195972f6Sopenharmony_ci      return 0;
150195972f6Sopenharmony_ci  }
151195972f6Sopenharmony_ci
152195972f6Sopenharmony_ci  return sizeof(*uint_ptr);
153195972f6Sopenharmony_ci}
154195972f6Sopenharmony_ci
155195972f6Sopenharmony_cistatic snmp_err_t
156195972f6Sopenharmony_cisnmp_set_test(const struct snmp_scalar_array_node_def *node, u16_t len, void *value)
157195972f6Sopenharmony_ci{
158195972f6Sopenharmony_ci  snmp_err_t ret = SNMP_ERR_WRONGVALUE;
159195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(len);
160195972f6Sopenharmony_ci
161195972f6Sopenharmony_ci  if (node->oid == 30) {
162195972f6Sopenharmony_ci    /* snmpEnableAuthenTraps */
163195972f6Sopenharmony_ci    s32_t *sint_ptr = (s32_t *)value;
164195972f6Sopenharmony_ci
165195972f6Sopenharmony_ci    /* we should have writable non-volatile mem here */
166195972f6Sopenharmony_ci    if ((*sint_ptr == MIB2_AUTH_TRAPS_DISABLED) || (*sint_ptr == MIB2_AUTH_TRAPS_ENABLED)) {
167195972f6Sopenharmony_ci      ret = SNMP_ERR_NOERROR;
168195972f6Sopenharmony_ci    }
169195972f6Sopenharmony_ci  }
170195972f6Sopenharmony_ci  return ret;
171195972f6Sopenharmony_ci}
172195972f6Sopenharmony_ci
173195972f6Sopenharmony_cistatic snmp_err_t
174195972f6Sopenharmony_cisnmp_set_value(const struct snmp_scalar_array_node_def *node, u16_t len, void *value)
175195972f6Sopenharmony_ci{
176195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(len);
177195972f6Sopenharmony_ci
178195972f6Sopenharmony_ci  if (node->oid == 30) {
179195972f6Sopenharmony_ci    /* snmpEnableAuthenTraps */
180195972f6Sopenharmony_ci    s32_t *sint_ptr = (s32_t *)value;
181195972f6Sopenharmony_ci    if (*sint_ptr == MIB2_AUTH_TRAPS_DISABLED) {
182195972f6Sopenharmony_ci      snmp_set_auth_traps_enabled(SNMP_AUTH_TRAPS_DISABLED);
183195972f6Sopenharmony_ci    } else {
184195972f6Sopenharmony_ci      snmp_set_auth_traps_enabled(SNMP_AUTH_TRAPS_ENABLED);
185195972f6Sopenharmony_ci    }
186195972f6Sopenharmony_ci  }
187195972f6Sopenharmony_ci
188195972f6Sopenharmony_ci  return SNMP_ERR_NOERROR;
189195972f6Sopenharmony_ci}
190195972f6Sopenharmony_ci
191195972f6Sopenharmony_ci/* the following nodes access variables in SNMP stack (snmp_stats) from SNMP worker thread -> OK, no sync needed */
192195972f6Sopenharmony_cistatic const struct snmp_scalar_array_node_def snmp_nodes[] = {
193195972f6Sopenharmony_ci  { 1, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInPkts */
194195972f6Sopenharmony_ci  { 2, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutPkts */
195195972f6Sopenharmony_ci  { 3, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadVersions */
196195972f6Sopenharmony_ci  { 4, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadCommunityNames */
197195972f6Sopenharmony_ci  { 5, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadCommunityUses */
198195972f6Sopenharmony_ci  { 6, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInASNParseErrs */
199195972f6Sopenharmony_ci  { 8, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTooBigs */
200195972f6Sopenharmony_ci  { 9, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInNoSuchNames */
201195972f6Sopenharmony_ci  {10, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadValues */
202195972f6Sopenharmony_ci  {11, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInReadOnlys */
203195972f6Sopenharmony_ci  {12, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGenErrs */
204195972f6Sopenharmony_ci  {13, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTotalReqVars */
205195972f6Sopenharmony_ci  {14, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTotalSetVars */
206195972f6Sopenharmony_ci  {15, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGetRequests */
207195972f6Sopenharmony_ci  {16, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGetNexts */
208195972f6Sopenharmony_ci  {17, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInSetRequests */
209195972f6Sopenharmony_ci  {18, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGetResponses */
210195972f6Sopenharmony_ci  {19, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTraps */
211195972f6Sopenharmony_ci  {20, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutTooBigs */
212195972f6Sopenharmony_ci  {21, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutNoSuchNames */
213195972f6Sopenharmony_ci  {22, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutBadValues */
214195972f6Sopenharmony_ci  {24, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGenErrs */
215195972f6Sopenharmony_ci  {25, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGetRequests */
216195972f6Sopenharmony_ci  {26, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGetNexts */
217195972f6Sopenharmony_ci  {27, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutSetRequests */
218195972f6Sopenharmony_ci  {28, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGetResponses */
219195972f6Sopenharmony_ci  {29, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutTraps */
220195972f6Sopenharmony_ci  {30, SNMP_ASN1_TYPE_INTEGER, SNMP_NODE_INSTANCE_READ_WRITE}, /* snmpEnableAuthenTraps */
221195972f6Sopenharmony_ci  {31, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpSilentDrops */
222195972f6Sopenharmony_ci  {32, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY}   /* snmpProxyDrops */
223195972f6Sopenharmony_ci};
224195972f6Sopenharmony_ci
225195972f6Sopenharmony_ciconst struct snmp_scalar_array_node snmp_mib2_snmp_root = SNMP_SCALAR_CREATE_ARRAY_NODE(11, snmp_nodes, snmp_get_value, snmp_set_test, snmp_set_value);
226195972f6Sopenharmony_ci
227195972f6Sopenharmony_ci#endif /* LWIP_SNMP && SNMP_LWIP_MIB2 */
228