• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * Management Information Base II (RFC1213) SNMP 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_scalar.h"
41 
42 #if LWIP_SNMP && SNMP_LWIP_MIB2
43 
44 #define MIB2_AUTH_TRAPS_ENABLED  1
45 #define MIB2_AUTH_TRAPS_DISABLED 2
46 
47 /* --- snmp .1.3.6.1.2.1.11 ----------------------------------------------------- */
48 static s16_t
snmp_get_value(const struct snmp_scalar_array_node_def * node,void * value)49 snmp_get_value(const struct snmp_scalar_array_node_def *node, void *value)
50 {
51   u32_t *uint_ptr = (u32_t *)value;
52   switch (node->oid) {
53     case 1: /* snmpInPkts */
54       *uint_ptr = snmp_stats.inpkts;
55       break;
56     case 2: /* snmpOutPkts */
57       *uint_ptr = snmp_stats.outpkts;
58       break;
59     case 3: /* snmpInBadVersions */
60       *uint_ptr = snmp_stats.inbadversions;
61       break;
62     case 4: /* snmpInBadCommunityNames */
63       *uint_ptr = snmp_stats.inbadcommunitynames;
64       break;
65     case 5: /* snmpInBadCommunityUses */
66       *uint_ptr = snmp_stats.inbadcommunityuses;
67       break;
68     case 6: /* snmpInASNParseErrs */
69       *uint_ptr = snmp_stats.inasnparseerrs;
70       break;
71     case 8: /* snmpInTooBigs */
72       *uint_ptr = snmp_stats.intoobigs;
73       break;
74     case 9: /* snmpInNoSuchNames */
75       *uint_ptr = snmp_stats.innosuchnames;
76       break;
77     case 10: /* snmpInBadValues */
78       *uint_ptr = snmp_stats.inbadvalues;
79       break;
80     case 11: /* snmpInReadOnlys */
81       *uint_ptr = snmp_stats.inreadonlys;
82       break;
83     case 12: /* snmpInGenErrs */
84       *uint_ptr = snmp_stats.ingenerrs;
85       break;
86     case 13: /* snmpInTotalReqVars */
87       *uint_ptr = snmp_stats.intotalreqvars;
88       break;
89     case 14: /* snmpInTotalSetVars */
90       *uint_ptr = snmp_stats.intotalsetvars;
91       break;
92     case 15: /* snmpInGetRequests */
93       *uint_ptr = snmp_stats.ingetrequests;
94       break;
95     case 16: /* snmpInGetNexts */
96       *uint_ptr = snmp_stats.ingetnexts;
97       break;
98     case 17: /* snmpInSetRequests */
99       *uint_ptr = snmp_stats.insetrequests;
100       break;
101     case 18: /* snmpInGetResponses */
102       *uint_ptr = snmp_stats.ingetresponses;
103       break;
104     case 19: /* snmpInTraps */
105       *uint_ptr = snmp_stats.intraps;
106       break;
107     case 20: /* snmpOutTooBigs */
108       *uint_ptr = snmp_stats.outtoobigs;
109       break;
110     case 21: /* snmpOutNoSuchNames */
111       *uint_ptr = snmp_stats.outnosuchnames;
112       break;
113     case 22: /* snmpOutBadValues */
114       *uint_ptr = snmp_stats.outbadvalues;
115       break;
116     case 24: /* snmpOutGenErrs */
117       *uint_ptr = snmp_stats.outgenerrs;
118       break;
119     case 25: /* snmpOutGetRequests */
120       *uint_ptr = snmp_stats.outgetrequests;
121       break;
122     case 26: /* snmpOutGetNexts */
123       *uint_ptr = snmp_stats.outgetnexts;
124       break;
125     case 27: /* snmpOutSetRequests */
126       *uint_ptr = snmp_stats.outsetrequests;
127       break;
128     case 28: /* snmpOutGetResponses */
129       *uint_ptr = snmp_stats.outgetresponses;
130       break;
131     case 29: /* snmpOutTraps */
132       *uint_ptr = snmp_stats.outtraps;
133       break;
134     case 30: /* snmpEnableAuthenTraps */
135       if (snmp_get_auth_traps_enabled() == SNMP_AUTH_TRAPS_DISABLED) {
136         *uint_ptr = MIB2_AUTH_TRAPS_DISABLED;
137       } else {
138         *uint_ptr = MIB2_AUTH_TRAPS_ENABLED;
139       }
140       break;
141     case 31: /* snmpSilentDrops */
142       *uint_ptr = 0; /* not supported */
143       break;
144     case 32: /* snmpProxyDrops */
145       *uint_ptr = 0; /* not supported */
146       break;
147     default:
148       LWIP_DEBUGF(SNMP_MIB_DEBUG, ("snmp_get_value(): unknown id: %"S32_F"\n", node->oid));
149       return 0;
150   }
151 
152   return sizeof(*uint_ptr);
153 }
154 
155 static snmp_err_t
snmp_set_test(const struct snmp_scalar_array_node_def * node,u16_t len,void * value)156 snmp_set_test(const struct snmp_scalar_array_node_def *node, u16_t len, void *value)
157 {
158   snmp_err_t ret = SNMP_ERR_WRONGVALUE;
159   LWIP_UNUSED_ARG(len);
160 
161   if (node->oid == 30) {
162     /* snmpEnableAuthenTraps */
163     s32_t *sint_ptr = (s32_t *)value;
164 
165     /* we should have writable non-volatile mem here */
166     if ((*sint_ptr == MIB2_AUTH_TRAPS_DISABLED) || (*sint_ptr == MIB2_AUTH_TRAPS_ENABLED)) {
167       ret = SNMP_ERR_NOERROR;
168     }
169   }
170   return ret;
171 }
172 
173 static snmp_err_t
snmp_set_value(const struct snmp_scalar_array_node_def * node,u16_t len,void * value)174 snmp_set_value(const struct snmp_scalar_array_node_def *node, u16_t len, void *value)
175 {
176   LWIP_UNUSED_ARG(len);
177 
178   if (node->oid == 30) {
179     /* snmpEnableAuthenTraps */
180     s32_t *sint_ptr = (s32_t *)value;
181     if (*sint_ptr == MIB2_AUTH_TRAPS_DISABLED) {
182       snmp_set_auth_traps_enabled(SNMP_AUTH_TRAPS_DISABLED);
183     } else {
184       snmp_set_auth_traps_enabled(SNMP_AUTH_TRAPS_ENABLED);
185     }
186   }
187 
188   return SNMP_ERR_NOERROR;
189 }
190 
191 /* the following nodes access variables in SNMP stack (snmp_stats) from SNMP worker thread -> OK, no sync needed */
192 static const struct snmp_scalar_array_node_def snmp_nodes[] = {
193   { 1, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInPkts */
194   { 2, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutPkts */
195   { 3, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadVersions */
196   { 4, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadCommunityNames */
197   { 5, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadCommunityUses */
198   { 6, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInASNParseErrs */
199   { 8, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTooBigs */
200   { 9, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInNoSuchNames */
201   {10, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInBadValues */
202   {11, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInReadOnlys */
203   {12, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGenErrs */
204   {13, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTotalReqVars */
205   {14, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTotalSetVars */
206   {15, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGetRequests */
207   {16, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGetNexts */
208   {17, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInSetRequests */
209   {18, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInGetResponses */
210   {19, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpInTraps */
211   {20, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutTooBigs */
212   {21, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutNoSuchNames */
213   {22, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutBadValues */
214   {24, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGenErrs */
215   {25, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGetRequests */
216   {26, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGetNexts */
217   {27, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutSetRequests */
218   {28, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutGetResponses */
219   {29, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpOutTraps */
220   {30, SNMP_ASN1_TYPE_INTEGER, SNMP_NODE_INSTANCE_READ_WRITE}, /* snmpEnableAuthenTraps */
221   {31, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY},  /* snmpSilentDrops */
222   {32, SNMP_ASN1_TYPE_COUNTER, SNMP_NODE_INSTANCE_READ_ONLY}   /* snmpProxyDrops */
223 };
224 
225 const 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);
226 
227 #endif /* LWIP_SNMP && SNMP_LWIP_MIB2 */
228