Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / smux / snmp_bgp.c
1 /*
2  * $Id: snmp_bgp.c,v 5.0 2002/04/20 07:30:06 hardaker Exp $ 
3  */
4
5 /*
6  * Smux module authored by Rohit Dube.
7  */
8
9 #include <net-snmp/net-snmp-config.h>
10
11 #include <stdio.h>
12 #if HAVE_STDLIB_H
13 #include <stdlib.h>
14 #endif
15 #if HAVE_STRING_H
16 #include <string.h>
17 #else
18 #include <strings.h>
19 #endif
20 #if HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #if HAVE_ERR_H
24 #include <err.h>
25 #endif
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
28 # include <time.h>
29 #else
30 # if HAVE_SYS_TIME_H
31 #  include <sys/time.h>
32 # else
33 #  include <time.h>
34 # endif
35 #endif
36 #include <errno.h>
37 #include <netdb.h>
38
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41 #if HAVE_SYS_FILIO_H
42 #include <sys/filio.h>
43 #endif
44
45 #if HAVE_NETINET_IN_H
46 #include <netinet/in.h>
47 #endif
48
49 #include <net-snmp/net-snmp-includes.h>
50 #include <net-snmp/agent/net-snmp-agent-includes.h>
51 #include "smux.h"
52 #include "snmp_bgp.h"
53
54 struct variable13 bgp_variables[] = {
55     {BGPVERSION, ASN_OCTET_STR, RONLY, var_bgp, 1, {1}},
56     {BGPLOCALAS, ASN_INTEGER, RONLY, var_bgp, 1, {2}},
57     {BGPIDENTIFIER, ASN_IPADDRESS, RONLY, var_bgp, 1, {4}},
58     {BGPPEERIDENTIFIER, ASN_IPADDRESS, RONLY, var_bgp, 3, {3, 1, 1}},
59     {BGPPEERSTATE, ASN_INTEGER, RONLY, var_bgp, 3, {3, 1, 2}},
60     {BGPPEERADMINSTATUS, ASN_INTEGER, RONLY, var_bgp, 3, {3, 1, 3}},
61     {BGPPEERNEGOTIATEDVERSION, ASN_INTEGER, RONLY, var_bgp, 3, {3, 1, 4}},
62     {BGPPEERLOCALADDR, ASN_IPADDRESS, RONLY, var_bgp, 3, {3, 1, 5}},
63     {BGPPEERLOCALPORT, ASN_INTEGER, RONLY, var_bgp, 3, {3, 1, 6}},
64     {BGPPEERREMOTEADDR, ASN_IPADDRESS, RONLY, var_bgp, 3, {3, 1, 7}},
65     {BGPPEERREMOTEPORT, ASN_INTEGER, RONLY, var_bgp, 3, {3, 1, 8}},
66     {BGPPEERREMOTEAS, ASN_INTEGER, RONLY, var_bgp, 3, {3, 1, 9}},
67     {BGPPEERINUPDATES, ASN_COUNTER, RONLY, var_bgp, 3, {3, 1, 10}},
68     {BGPPEEROUTUPDATES, ASN_COUNTER, RONLY, var_bgp, 3, {3, 1, 11}},
69     {BGPPEERINTOTALMESSAGES, ASN_COUNTER, RONLY, var_bgp, 3, {3, 1, 12}},
70     {BGPPEEROUTTOTALMESSAGES, ASN_COUNTER, RONLY, var_bgp, 3, {3, 1, 13}},
71     {BGPPEERLASTERROR, ASN_OCTET_STR, RONLY, var_bgp, 3, {3, 1, 14}},
72     {BGPPATHATTRPEER, ASN_IPADDRESS, RONLY, var_bgp, 3, {5, 1, 1}},
73     {BGPPATHATTRDESTNETWORK, ASN_IPADDRESS, RONLY, var_bgp, 3, {5, 1, 2}},
74     {BGPPATHATTRORIGIN, ASN_INTEGER, RONLY, var_bgp, 3, {5, 1, 3}},
75     {BGPPATHATTRASPATH, ASN_OCTET_STR, RONLY, var_bgp, 3, {5, 1, 4}},
76     {BGPPATHATTRNEXTHOP, ASN_INTEGER, RONLY, var_bgp, 3, {5, 1, 5}},
77     {BGPPATHATTRINTERASMETRIC, ASN_OCTET_STR, RONLY, var_bgp, 3, {5, 1, 6}}
78 };
79
80 oid             bgp_variables_oid[] = { MIB, 15 };
81
82 void
83 init_snmp_bdp(void)
84 {
85     REGISTER_MIB("smux/snmp_bgp", bgp_variables, variable13,
86                  bgp_variables_oid);
87 }
88
89
90 #endif
91 static oid      max_bgp_mib[] =
92     { 1, 3, 6, 1, 2, 1, 15, 5, 1, 6, 255, 255, 255, 255 };
93 static oid      min_bgp_mib[] = { 1, 3, 6, 1, 2, 1, 15, 1, 0 };
94 extern u_char   smux_type;
95
96 u_char         *
97 var_bgp(struct variable *vp,
98         oid * name,
99         int *length, int exact, int *var_len, WriteMethod ** write_method)
100 {
101     u_char         *var;
102     int             result;
103
104     DEBUGMSGTL(("smux/snmp_bgp",
105                 "[var_bgp] var len %d, oid requested Len %d-", *var_len,
106                 *length));
107     DEBUGMSGOID(("smux/snmp_bgp", name, *length));
108     DEBUGMSG(("smux/snmp_bgp", "\n"));
109
110     /*
111      * Pass on the request to Gated.
112      * If the request sent out was a get next, check to see if
113      * it lies in the bgp range. If it doesn't, return NULL.
114      * In either case, make sure that errors are checked on the
115      * returned packets.
116      */
117
118     /*
119      * No writes for now 
120      */
121     *write_method = NULL;
122
123     /*
124      * Donot allow access to the peer stuff as it crashes gated.
125      * However A GetNext on the last 23.3.1.9 variable will force gated into
126      * the peer stuff and cause it to crash.
127      * The only way to fix this is to either solve the Gated problem, or 
128      * remove the peer variables from Gated itself and cause it to return
129      * NULL at the crossing. Currently doing the later.
130      */
131
132     /*
133      * Reject GET and GETNEXT for anything above bgpifconf range 
134      */
135     result = snmp_oid_compare(name, *length, max_bgp_mib,
136                               sizeof(max_bgp_mib) / sizeof(u_int));
137
138     if (result >= 0) {
139         DEBUGMSGTL(("smux/snmp_bgp", "Over shot\n"));
140         return NULL;
141     }
142
143     /*
144      * for GETs we need to be in the bgp range so reject anything below 
145      */
146     result = snmp_oid_compare(name, *length, min_bgp_mib,
147                               sizeof(min_bgp_mib) / sizeof(u_int));
148     if (exact && (result < 0)) {
149         DEBUGMSGTL(("smux/snmp_bgp",
150                     "Exact but doesn't match length %d, size %d\n",
151                     *length, sizeof(min_bgp_mib)));
152         return NULL;
153     }
154
155     /*
156      * On return, 'var' points to the value returned which is of length
157      * '*var_len'. 'name' points to the new (same as the one passed in for 
158      * GETs) oid which has 'length' suboids.
159      * 'smux_type' contains the type of the variable.
160      */
161     var = smux_snmp_process(exact, name, length, var_len);
162
163     DEBUGMSGTL(("smux/snmp_bgp",
164                 "[var_bgp] var len %d, oid obtained Len %d-", *var_len,
165                 *length));
166     DEBUGMSGOID(("smux/snmp_bgp", name, *length));
167     DEBUGMSG(("smux/snmp_bgp", "\n"));
168
169     vp->type = smux_type;
170
171     /*
172      * XXX Need a mechanism to return errors in gated's responses 
173      */
174
175     if (var == NULL)
176         return NULL;
177
178     /*
179      * Any resullt returned should be within the bgp tree.
180      * bgp_mib - static u_int bgp_mib[] = {1, 3, 6, 1, 2, 1, 15};
181      */
182     if (memcmp(bgp_mib, name, sizeof(bgp_mib)) != 0) {
183         return NULL;
184     } else {
185         return var;
186     }
187 }