2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/snmp_mibII/mibII_route.c,v 1.9 2005/10/06 07:15:00 brandt_h Exp $
34 #ifdef HAVE_SYS_TREE_H
41 #include "mibII_oid.h"
44 RB_ENTRY(sroute) link;
50 RB_HEAD(sroutes, sroute) sroutes = RB_INITIALIZER(&sroutes);
52 RB_PROTOTYPE(sroutes, sroute, link, sroute_compare);
54 #define ROUTE_UPDATE_INTERVAL (100 * 60 * 10) /* 10 min */
55 static uint64_t route_tick;
56 static u_int route_total;
62 sroute_compare(struct sroute *s1, struct sroute *s2)
65 return (memcmp(s1->index, s2->index, 13));
69 sroute_index_append(struct asn_oid *oid, u_int sub, const struct sroute *s)
74 for (i = 0; i < 13; i++)
75 oid->subs[sub + i] = s->index[i];
80 sroute_print(const struct sroute *r)
84 for (i = 0; i < 13 - 1; i++)
85 printf("%u.", r->index[i]);
86 printf("%u proto=%u type=%u", r->index[i], r->proto, r->type);
91 * process routing message
94 mib_sroute_process(struct rt_msghdr *rtm, struct sockaddr *gw,
95 struct sockaddr *dst, struct sockaddr *mask)
97 struct sockaddr_in *in_dst, *in_gw;
98 struct in_addr in_mask;
101 struct sroute *r, *r1;
104 if (dst == NULL || gw == NULL || dst->sa_family != AF_INET ||
105 gw->sa_family != AF_INET)
108 in_dst = (struct sockaddr_in *)(void *)dst;
109 in_gw = (struct sockaddr_in *)(void *)gw;
111 if (rtm->rtm_flags & RTF_HOST)
112 in_mask.s_addr = 0xffffffff;
113 else if (mask == NULL || mask->sa_len == 0)
116 in_mask = ((struct sockaddr_in *)(void *)mask)->sin_addr;
118 /* build the index */
119 ha = ntohl(in_dst->sin_addr.s_addr);
120 key.index[0] = (ha >> 24) & 0xff;
121 key.index[1] = (ha >> 16) & 0xff;
122 key.index[2] = (ha >> 8) & 0xff;
123 key.index[3] = (ha >> 0) & 0xff;
125 ha = ntohl(in_mask.s_addr);
126 key.index[4] = (ha >> 24) & 0xff;
127 key.index[5] = (ha >> 16) & 0xff;
128 key.index[6] = (ha >> 8) & 0xff;
129 key.index[7] = (ha >> 0) & 0xff;
134 ha = ntohl(in_gw->sin_addr.s_addr);
135 key.index[9] = (ha >> 24) & 0xff;
136 key.index[10] = (ha >> 16) & 0xff;
137 key.index[11] = (ha >> 8) & 0xff;
138 key.index[12] = (ha >> 0) & 0xff;
140 if (rtm->rtm_type == RTM_DELETE) {
141 r = RB_FIND(sroutes, &sroutes, &key);
144 syslog(LOG_WARNING, "%s: DELETE: %u.%u.%u.%u "
145 "%u.%u.%u.%u %u %u.%u.%u.%u not found", __func__,
146 key.index[0], key.index[1], key.index[2],
147 key.index[3], key.index[4], key.index[5],
148 key.index[6], key.index[7], key.index[8],
149 key.index[9], key.index[10], key.index[11],
154 RB_REMOVE(sroutes, &sroutes, r);
158 printf("%s: DELETE: %u.%u.%u.%u "
159 "%u.%u.%u.%u %u %u.%u.%u.%u\n", __func__,
160 key.index[0], key.index[1], key.index[2],
161 key.index[3], key.index[4], key.index[5],
162 key.index[6], key.index[7], key.index[8],
163 key.index[9], key.index[10], key.index[11],
171 if ((ifp = mib_find_if_sys(rtm->rtm_index)) == NULL) {
172 if (rtm->rtm_type == RTM_ADD) {
173 /* make it a get so the kernel fills the index */
174 mib_send_rtmsg(rtm, gw, dst, mask);
180 if ((r = malloc(sizeof(*r))) == NULL) {
181 syslog(LOG_ERR, "%m");
185 memcpy(r->index, key.index, sizeof(r->index));
186 r->ifindex = (ifp == NULL) ? 0 : ifp->index;
188 r->type = (rtm->rtm_flags & RTF_REJECT) ? 2 : 4;
190 /* cannot really know, what protocol it runs */
191 r->proto = (rtm->rtm_flags & RTF_LOCAL) ? 2 :
192 (rtm->rtm_flags & RTF_STATIC) ? 3 :
193 (rtm->rtm_flags & RTF_DYNAMIC) ? 4 : 10;
195 r1 = RB_INSERT(sroutes, &sroutes, r);
198 syslog(LOG_WARNING, "%s: %u.%u.%u.%u "
199 "%u.%u.%u.%u %u %u.%u.%u.%u duplicate route", __func__,
200 key.index[0], key.index[1], key.index[2],
201 key.index[3], key.index[4], key.index[5],
202 key.index[6], key.index[7], key.index[8],
203 key.index[9], key.index[10], key.index[11],
206 r1->ifindex = r->ifindex;
208 r1->proto = r->proto;
215 printf("%s: ADD/GET: %u.%u.%u.%u "
216 "%u.%u.%u.%u %u %u.%u.%u.%u\n", __func__,
217 key.index[0], key.index[1], key.index[2],
218 key.index[3], key.index[4], key.index[5],
219 key.index[6], key.index[7], key.index[8],
220 key.index[9], key.index[10], key.index[11],
226 mib_fetch_route(void)
230 struct sroute *r, *r1;
231 struct rt_msghdr *rtm;
232 struct sockaddr *addrs[RTAX_MAX];
234 if (route_tick != 0 && route_tick + ROUTE_UPDATE_INTERVAL > this_tick)
240 r = RB_MIN(sroutes, &sroutes);
242 r1 = RB_NEXT(sroutes, &sroutes, r);
243 RB_REMOVE(sroutes, &sroutes, r);
249 if ((rtab = mib_fetch_rtab(AF_INET, NET_RT_DUMP, 0, &len)) == NULL)
253 for (next = rtab; next < rtab + len; next += rtm->rtm_msglen) {
254 rtm = (struct rt_msghdr *)(void *)next;
255 if (rtm->rtm_type != RTM_GET ||
256 !(rtm->rtm_flags & RTF_UP))
258 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
261 mib_sroute_process(rtm, addrs[RTAX_GATEWAY], addrs[RTAX_DST],
262 addrs[RTAX_NETMASK]);
267 r = RB_MIN(sroutes, &sroutes);
272 r = RB_NEXT(sroutes, &sroutes, r);
276 route_tick = get_ticks();
282 * Find a route in the table.
284 static struct sroute *
285 sroute_get(const struct asn_oid *oid, u_int sub)
290 if (oid->len - sub != 13)
292 for (i = 0; i < 13; i++)
293 key.index[i] = oid->subs[sub + i];
294 return (RB_FIND(sroutes, &sroutes, &key));
298 * Find next route in the table. There is no such RB_ macro, so must
299 * dig into the innards of the RB stuff.
301 static struct sroute *
302 sroute_getnext(struct asn_oid *oid, u_int sub)
311 * We now, that the OID is at least the tableEntry OID. If it is,
312 * the user wants the first route.
315 return (RB_MIN(sroutes, &sroutes));
318 * This is also true for any index that consists of zeros and is
319 * shorter than the full index.
321 if (oid->len < sub + 13) {
322 for (i = sub; i < oid->len; i++)
323 if (oid->subs[i] != 0)
326 return (RB_MIN(sroutes, &sroutes));
329 * Now if the index is too short, we fill it with zeros and then
330 * subtract one from the index. We can do this, because we now,
331 * that there is at least one index element that is not zero.
333 for (i = oid->len; i < sub + 13; i++)
336 for (i = sub + 13 - 1; i >= sub; i--) {
337 if (oid->subs[i] != 0) {
341 oid->subs[i] = ASN_MAXID;
346 /* build the index */
347 for (i = sub; i < sub + 13; i++)
348 key.index[i - sub] = oid->subs[i];
350 /* now find the element */
352 s = RB_ROOT(&sroutes);
355 comp = sroute_compare(&key, s);
357 /* The current element is smaller than what we search.
358 * Forget about it and move to the right subtree. */
359 s = RB_RIGHT(s, link);
362 /* the current element is larger than what we search.
363 * forget about the right subtree (its even larger), but
364 * the current element may be what we need. */
365 if (best == NULL || sroute_compare(s, best) < 0)
366 /* this one's better */
369 s = RB_LEFT(s, link);
378 op_route_table(struct snmp_context *ctx __unused, struct snmp_value *value,
379 u_int sub, u_int iidx __unused, enum snmp_op op)
383 if (mib_fetch_route() == -1)
384 return (SNMP_ERR_GENERR);
388 case SNMP_OP_GETNEXT:
389 if ((r = sroute_getnext(&value->var, sub)) == NULL)
390 return (SNMP_ERR_NOSUCHNAME);
391 sroute_index_append(&value->var, sub, r);
395 if ((r = sroute_get(&value->var, sub)) == NULL)
396 return (SNMP_ERR_NOSUCHNAME);
400 if ((r = sroute_get(&value->var, sub)) == NULL)
401 return (SNMP_ERR_NOSUCHNAME);
402 return (SNMP_ERR_NOT_WRITEABLE);
404 case SNMP_OP_ROLLBACK:
412 switch (value->var.subs[sub - 1]) {
414 case LEAF_ipCidrRouteDest:
415 value->v.ipaddress[0] = r->index[0];
416 value->v.ipaddress[1] = r->index[1];
417 value->v.ipaddress[2] = r->index[2];
418 value->v.ipaddress[3] = r->index[3];
421 case LEAF_ipCidrRouteMask:
422 value->v.ipaddress[0] = r->index[4];
423 value->v.ipaddress[1] = r->index[5];
424 value->v.ipaddress[2] = r->index[6];
425 value->v.ipaddress[3] = r->index[7];
428 case LEAF_ipCidrRouteTos:
429 value->v.integer = r->index[8];
432 case LEAF_ipCidrRouteNextHop:
433 value->v.ipaddress[0] = r->index[9];
434 value->v.ipaddress[1] = r->index[10];
435 value->v.ipaddress[2] = r->index[11];
436 value->v.ipaddress[3] = r->index[12];
439 case LEAF_ipCidrRouteIfIndex:
440 value->v.integer = r->ifindex;
443 case LEAF_ipCidrRouteType:
444 value->v.integer = r->type;
447 case LEAF_ipCidrRouteProto:
448 value->v.integer = r->proto;
451 case LEAF_ipCidrRouteAge:
452 value->v.integer = 0;
455 case LEAF_ipCidrRouteInfo:
456 value->v.oid = oid_zeroDotZero;
459 case LEAF_ipCidrRouteNextHopAS:
460 value->v.integer = 0;
463 case LEAF_ipCidrRouteMetric1:
464 case LEAF_ipCidrRouteMetric2:
465 case LEAF_ipCidrRouteMetric3:
466 case LEAF_ipCidrRouteMetric4:
467 case LEAF_ipCidrRouteMetric5:
468 value->v.integer = -1;
471 case LEAF_ipCidrRouteStatus:
472 value->v.integer = 1;
475 return (SNMP_ERR_NOERROR);
482 op_route(struct snmp_context *ctx __unused, struct snmp_value *value,
483 u_int sub, u_int iidx __unused, enum snmp_op op)
487 case SNMP_OP_GETNEXT:
494 return (SNMP_ERR_NOT_WRITEABLE);
496 case SNMP_OP_ROLLBACK:
501 if (mib_fetch_route() == -1)
502 return (SNMP_ERR_GENERR);
504 switch (value->var.subs[sub - 1]) {
506 case LEAF_ipCidrRouteNumber:
507 value->v.uint32 = route_total;
511 return (SNMP_ERR_NOERROR);
514 RB_GENERATE(sroutes, sroute, link, sroute_compare);