ef2a1c47c7f5e9f159b04dc2e1421e0eeec17338
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / mibII / snmp_mib.c
1 #ifdef BUILD_SNMP_SNMP_MIB
2
3 /*
4  *  SNMPv1 MIB group implementation - snmp.c
5  *
6  */
7
8 #include <net-snmp/net-snmp-config.h>
9 #include <sys/types.h>
10 #if HAVE_STRING_H
11 #include <string.h>
12 #else
13 #include <strings.h>
14 #endif
15 #if HAVE_NETINET_IN_H
16 #include <netinet/in.h>
17 #endif
18
19 #if HAVE_WINSOCK_H
20 #include <winsock.h>
21 #endif
22
23 #include <net-snmp/net-snmp-includes.h>
24 #include <net-snmp/agent/net-snmp-agent-includes.h>
25
26 #include "util_funcs.h"
27 #include "snmp_mib.h"
28
29 #ifdef BUILD_SNMP_SYSOR_MIB
30 #include "sysORTable.h"
31 #endif /* BUILD_SNMP_SYSOR_MIB */
32
33         /*********************
34          *
35          *  Kernel & interface information,
36          *   and internal forward declarations
37          *
38          *********************/
39
40 extern int      snmp_enableauthentraps;
41 extern int      snmp_enableauthentrapsset;
42 int             old_snmp_enableauthentraps;
43
44 /*********************
45  *
46  *  Initialisation & common implementation functions
47  *
48  *********************/
49
50 /*
51  * define the structure we're going to ask the agent to register our
52  * information at 
53  */
54 struct variable1 snmp_variables[] = {
55     {SNMPINPKTS, ASN_COUNTER, RONLY, var_snmp, 1, {1}},
56     {SNMPOUTPKTS, ASN_COUNTER, RONLY, var_snmp, 1, {2}},
57     {SNMPINBADVERSIONS, ASN_COUNTER, RONLY, var_snmp, 1, {3}},
58     {SNMPINBADCOMMUNITYNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {4}},
59     {SNMPINBADCOMMUNITYUSES, ASN_COUNTER, RONLY, var_snmp, 1, {5}},
60     {SNMPINASNPARSEERRORS, ASN_COUNTER, RONLY, var_snmp, 1, {6}},
61     {SNMPINTOOBIGS, ASN_COUNTER, RONLY, var_snmp, 1, {8}},
62     {SNMPINNOSUCHNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {9}},
63     {SNMPINBADVALUES, ASN_COUNTER, RONLY, var_snmp, 1, {10}},
64     {SNMPINREADONLYS, ASN_COUNTER, RONLY, var_snmp, 1, {11}},
65     {SNMPINGENERRS, ASN_COUNTER, RONLY, var_snmp, 1, {12}},
66     {SNMPINTOTALREQVARS, ASN_COUNTER, RONLY, var_snmp, 1, {13}},
67     {SNMPINTOTALSETVARS, ASN_COUNTER, RONLY, var_snmp, 1, {14}},
68     {SNMPINGETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {15}},
69     {SNMPINGETNEXTS, ASN_COUNTER, RONLY, var_snmp, 1, {16}},
70     {SNMPINSETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {17}},
71     {SNMPINGETRESPONSES, ASN_COUNTER, RONLY, var_snmp, 1, {18}},
72     {SNMPINTRAPS, ASN_COUNTER, RONLY, var_snmp, 1, {19}},
73     {SNMPOUTTOOBIGS, ASN_COUNTER, RONLY, var_snmp, 1, {20}},
74     {SNMPOUTNOSUCHNAMES, ASN_COUNTER, RONLY, var_snmp, 1, {21}},
75     {SNMPOUTBADVALUES, ASN_COUNTER, RONLY, var_snmp, 1, {22}},
76     {SNMPOUTGENERRS, ASN_COUNTER, RONLY, var_snmp, 1, {24}},
77     {SNMPOUTGETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {25}},
78     {SNMPOUTGETNEXTS, ASN_COUNTER, RONLY, var_snmp, 1, {26}},
79     {SNMPOUTSETREQUESTS, ASN_COUNTER, RONLY, var_snmp, 1, {27}},
80     {SNMPOUTGETRESPONSES, ASN_COUNTER, RONLY, var_snmp, 1, {28}},
81     {SNMPOUTTRAPS, ASN_COUNTER, RONLY, var_snmp, 1, {29}},
82     {SNMPENABLEAUTHENTRAPS, ASN_INTEGER, RWRITE, var_snmp, 1, {30}},
83     {SNMPSILENTDROPS, ASN_COUNTER, RONLY, var_snmp, 1, {31}},
84     {SNMPPROXYDROPS, ASN_COUNTER, RONLY, var_snmp, 1, {32}}
85 };
86
87 /*
88  * Define the OID pointer to the top of the mib tree that we're
89  * registering underneath 
90  */
91 oid             snmp_variables_oid[] = { SNMP_OID_MIB2, 11 };
92 #ifdef USING_MIBII_SYSTEM_MIB_MODULE
93 extern oid      system_module_oid[];
94 extern int      system_module_oid_len;
95 extern int      system_module_count;
96 #endif
97
98 static int
99 snmp_enableauthentraps_store(int a, int b, void *c, void *d)
100 {
101     char            line[SNMP_MAXBUF_SMALL];
102
103     if (snmp_enableauthentrapsset > 0) {
104         snprintf(line, SNMP_MAXBUF_SMALL, "pauthtrapenable %d",
105                  snmp_enableauthentraps);
106         snmpd_store_config(line);
107     }
108     return 0;
109 }
110
111 void
112 init_snmp_mib(void)
113 {
114     /*
115      * register ourselves with the agent to handle our mib tree 
116      */
117     REGISTER_MIB("mibII/snmp", snmp_variables, variable1,
118                  snmp_variables_oid);
119
120 #ifdef BUILD_SNMP_SYSOR_MIB
121 #ifdef USING_MIBII_SYSTEM_MIB_MODULE
122     if (++system_module_count == 3)
123         REGISTER_SYSOR_TABLE(system_module_oid, system_module_oid_len,
124                              "The MIB module for SNMPv2 entities");
125 #endif
126 #endif /* BUILD_SNMP_SYSOR_MIB */
127     snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
128                            snmp_enableauthentraps_store, NULL);
129 }
130
131 /*
132  * header_snmp(...
133  * Arguments:
134  * vp     IN      - pointer to variable entry that points here
135  * name    IN/OUT  - IN/name requested, OUT/name found
136  * length  IN/OUT  - length of IN/OUT oid's 
137  * exact   IN      - TRUE if an exact match was requested
138  * var_len OUT     - length of variable or 0 if function returned
139  * write_method
140  * 
141  */
142
143         /*********************
144          *
145          *  System specific implementation functions
146          *      (actually common!)
147          *
148          *********************/
149
150
151 u_char         *
152 var_snmp(struct variable *vp,
153          oid * name,
154          size_t * length,
155          int exact, size_t * var_len, WriteMethod ** write_method)
156 {
157     static long     long_ret;
158
159     *write_method = 0;          /* assume it isnt writable for the time being */
160     *var_len = sizeof(long_ret);        /* assume an integer and change later if not */
161
162     if (header_generic(vp, name, length, exact, var_len, write_method)
163         == MATCH_FAILED)
164         return NULL;
165
166     /*
167      * this is where we do the value assignments for the mib results. 
168      */
169     if (vp->magic == SNMPENABLEAUTHENTRAPS) {
170         *write_method = write_snmp;
171         long_return = snmp_enableauthentraps;
172         return (u_char *) & long_return;
173     } else if ((vp->magic >= 1)
174                && (vp->magic <=
175                    (STAT_SNMP_STATS_END - STAT_SNMP_STATS_START + 1))) {
176         long_ret =
177             snmp_get_statistic(vp->magic + STAT_SNMP_STATS_START - 1);
178         return (unsigned char *) &long_ret;
179     }
180     return NULL;
181 }
182
183 /*
184  * only for snmpEnableAuthenTraps:
185  */
186
187 int
188 write_snmp(int action,
189            u_char * var_val,
190            u_char var_val_type,
191            size_t var_val_len, u_char * statP, oid * name, size_t name_len)
192 {
193     long            intval = 0;
194
195     switch (action) {
196     case RESERVE1:             /* Check values for acceptability */
197         if (var_val_type != ASN_INTEGER) {
198             DEBUGMSGTL(("mibII/snmp_mib", "%x not integer type",
199                         var_val_type));
200             return SNMP_ERR_WRONGTYPE;
201         }
202
203         intval = *((long *) var_val);
204         if (intval != 1 && intval != 2) {
205             DEBUGMSGTL(("mibII/snmp_mib", "not valid %x\n", intval));
206             return SNMP_ERR_WRONGVALUE;
207         }
208         if (snmp_enableauthentrapsset < 0) {
209             /*
210              * The object is set in a read-only configuration file.  
211              */
212             return SNMP_ERR_NOTWRITABLE;
213         }
214         break;
215
216     case RESERVE2:             /* Allocate memory and similar resources */
217
218         /*
219          * Using static variables, so nothing needs to be done 
220          */
221         break;
222
223     case ACTION:               /* Perform the SET action (if reversible) */
224
225         /*
226          * Save the old value, in case of UNDO 
227          */
228         intval = *((long *) var_val);
229         old_snmp_enableauthentraps = snmp_enableauthentraps;
230         snmp_enableauthentraps = intval;
231         break;
232
233     case UNDO:                 /* Reverse the SET action and free resources */
234
235         snmp_enableauthentraps = old_snmp_enableauthentraps;
236         break;
237
238     case COMMIT:
239         snmp_enableauthentrapsset = 1;
240         snmp_save_persistent(netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE));
241         (void) snmp_call_callbacks(SNMP_CALLBACK_LIBRARY,
242                                    SNMP_CALLBACK_STORE_DATA, NULL);
243         snmp_clean_persistent(netsnmp_ds_get_string
244                               (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE));
245         break;
246
247     case FREE:                 /* Free any resources allocated */
248         break;
249     }
250     return SNMP_ERR_NOERROR;
251 }
252
253 /*********************
254  *
255  *  Internal implementation functions
256  *
257  *********************/
258
259 #else
260 void
261 init_snmp_mib(void)
262 {
263 }
264 #endif /* BUILD_SNMP_SNMP_MIB */