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 $
35 #ifdef HAVE_SYS_TREE_H
42 #include "mibII_oid.h"
45 RB_ENTRY(sroute) link;
51 RB_HEAD(sroutes, sroute) sroutes = RB_INITIALIZER(&sroutes);
53 RB_PROTOTYPE(sroutes, sroute, link, sroute_compare);
55 #define ROUTE_UPDATE_INTERVAL (100 * 60 * 10) /* 10 min */
56 static uint64_t route_tick;
57 static u_int route_total;
63 sroute_compare(struct sroute *s1, struct sroute *s2)
66 return (memcmp(s1->index, s2->index, 13));
70 sroute_index_append(struct asn_oid *oid, u_int sub, const struct sroute *s)
75 for (i = 0; i < 13; i++)
76 oid->subs[sub + i] = s->index[i];
81 sroute_print(const struct sroute *r)
85 for (i = 0; i < 13 - 1; i++)
86 printf("%u.", r->index[i]);
87 printf("%u proto=%u type=%u", r->index[i], r->proto, r->type);
92 * process routing message
95 mib_sroute_process(struct rt_msghdr *rtm, struct sockaddr *gw,
96 struct sockaddr *dst, struct sockaddr *mask)
98 struct sockaddr_in *in_dst, *in_gw;
99 struct in_addr in_mask;
102 struct sroute *r, *r1;
105 if (dst == NULL || gw == NULL || dst->sa_family != AF_INET ||
106 gw->sa_family != AF_INET)
109 in_dst = (struct sockaddr_in *)(void *)dst;
110 in_gw = (struct sockaddr_in *)(void *)gw;
112 if (rtm->rtm_flags & RTF_HOST)
113 in_mask.s_addr = 0xffffffff;
114 else if (mask == NULL || mask->sa_len == 0)
117 in_mask = ((struct sockaddr_in *)(void *)mask)->sin_addr;
119 /* build the index */
120 ha = ntohl(in_dst->sin_addr.s_addr);
121 key.index[0] = (ha >> 24) & 0xff;
122 key.index[1] = (ha >> 16) & 0xff;
123 key.index[2] = (ha >> 8) & 0xff;
124 key.index[3] = (ha >> 0) & 0xff;
126 ha = ntohl(in_mask.s_addr);
127 key.index[4] = (ha >> 24) & 0xff;
128 key.index[5] = (ha >> 16) & 0xff;
129 key.index[6] = (ha >> 8) & 0xff;
130 key.index[7] = (ha >> 0) & 0xff;
135 ha = ntohl(in_gw->sin_addr.s_addr);
136 key.index[9] = (ha >> 24) & 0xff;
137 key.index[10] = (ha >> 16) & 0xff;
138 key.index[11] = (ha >> 8) & 0xff;
139 key.index[12] = (ha >> 0) & 0xff;
141 if (rtm->rtm_type == RTM_DELETE) {
142 r = RB_FIND(sroutes, &sroutes, &key);
145 syslog(LOG_WARNING, "%s: DELETE: %u.%u.%u.%u "
146 "%u.%u.%u.%u %u %u.%u.%u.%u not found", __func__,
147 key.index[0], key.index[1], key.index[2],
148 key.index[3], key.index[4], key.index[5],
149 key.index[6], key.index[7], key.index[8],
150 key.index[9], key.index[10], key.index[11],
155 RB_REMOVE(sroutes, &sroutes, r);
159 printf("%s: DELETE: %u.%u.%u.%u "
160 "%u.%u.%u.%u %u %u.%u.%u.%u\n", __func__,
161 key.index[0], key.index[1], key.index[2],
162 key.index[3], key.index[4], key.index[5],
163 key.index[6], key.index[7], key.index[8],
164 key.index[9], key.index[10], key.index[11],
172 if ((ifp = mib_find_if_sys(rtm->rtm_index)) == NULL) {
173 if (rtm->rtm_type == RTM_ADD) {
174 /* make it a get so the kernel fills the index */
175 mib_send_rtmsg(rtm, gw, dst, mask);
181 if ((r = malloc(sizeof(*r))) == NULL) {
182 syslog(LOG_ERR, "%m");
186 memcpy(r->index, key.index, sizeof(r->index));
187 r->ifindex = (ifp == NULL) ? 0 : ifp->index;
189 r->type = (rtm->rtm_flags & RTF_REJECT) ? 2 : 4;
191 /* cannot really know, what protocol it runs */
192 r->proto = (rtm->rtm_flags & RTF_LOCAL) ? 2 :
193 (rtm->rtm_flags & RTF_STATIC) ? 3 :
194 (rtm->rtm_flags & RTF_DYNAMIC) ? 4 : 10;
196 r1 = RB_INSERT(sroutes, &sroutes, r);
199 syslog(LOG_WARNING, "%s: %u.%u.%u.%u "
200 "%u.%u.%u.%u %u %u.%u.%u.%u duplicate route", __func__,
201 key.index[0], key.index[1], key.index[2],
202 key.index[3], key.index[4], key.index[5],
203 key.index[6], key.index[7], key.index[8],
204 key.index[9], key.index[10], key.index[11],
207 r1->ifindex = r->ifindex;
209 r1->proto = r->proto;
216 printf("%s: ADD/GET: %u.%u.%u.%u "
217 "%u.%u.%u.%u %u %u.%u.%u.%u\n", __func__,
218 key.index[0], key.index[1], key.index[2],
219 key.index[3], key.index[4], key.index[5],
220 key.index[6], key.index[7], key.index[8],
221 key.index[9], key.index[10], key.index[11],
227 mib_fetch_route(void)
231 struct sroute *r, *r1;
232 struct rt_msghdr *rtm;
233 struct sockaddr *addrs[RTAX_MAX];
235 if (route_tick != 0 && route_tick + ROUTE_UPDATE_INTERVAL > this_tick)
241 r = RB_MIN(sroutes, &sroutes);
243 r1 = RB_NEXT(sroutes, &sroutes, r);
244 RB_REMOVE(sroutes, &sroutes, r);
250 if ((rtab = mib_fetch_rtab(AF_INET, NET_RT_DUMP, 0, &len)) == NULL)
254 for (next = rtab; next < rtab + len; next += rtm->rtm_msglen) {
255 rtm = (struct rt_msghdr *)(void *)next;
256 if (rtm->rtm_type != RTM_GET ||
257 !(rtm->rtm_flags & RTF_UP))
259 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
262 mib_sroute_process(rtm, addrs[RTAX_GATEWAY], addrs[RTAX_DST],
263 addrs[RTAX_NETMASK]);
268 r = RB_MIN(sroutes, &sroutes);
273 r = RB_NEXT(sroutes, &sroutes, r);
277 route_tick = get_ticks();
283 * Find a route in the table.
285 static struct sroute *
286 sroute_get(const struct asn_oid *oid, u_int sub)
291 if (oid->len - sub != 13)
293 for (i = 0; i < 13; i++)
294 key.index[i] = oid->subs[sub + i];
295 return (RB_FIND(sroutes, &sroutes, &key));
299 * Find next route in the table. There is no such RB_ macro, so must
300 * dig into the innards of the RB stuff.
302 static struct sroute *
303 sroute_getnext(struct asn_oid *oid, u_int sub)
312 * We now, that the OID is at least the tableEntry OID. If it is,
313 * the user wants the first route.
316 return (RB_MIN(sroutes, &sroutes));
319 * This is also true for any index that consists of zeros and is
320 * shorter than the full index.
322 if (oid->len < sub + 13) {
323 for (i = sub; i < oid->len; i++)
324 if (oid->subs[i] != 0)
327 return (RB_MIN(sroutes, &sroutes));
330 * Now if the index is too short, we fill it with zeros and then
331 * subtract one from the index. We can do this, because we now,
332 * that there is at least one index element that is not zero.
334 for (i = oid->len; i < sub + 13; i++)
337 for (i = sub + 13 - 1; i >= sub; i--) {
338 if (oid->subs[i] != 0) {
342 oid->subs[i] = ASN_MAXID;
347 /* build the index */
348 for (i = sub; i < sub + 13; i++)
349 key.index[i - sub] = oid->subs[i];
351 /* now find the element */
353 s = RB_ROOT(&sroutes);
356 comp = sroute_compare(&key, s);
358 /* The current element is smaller than what we search.
359 * Forget about it and move to the right subtree. */
360 s = RB_RIGHT(s, link);
363 /* the current element is larger than what we search.
364 * forget about the right subtree (its even larger), but
365 * the current element may be what we need. */
366 if (best == NULL || sroute_compare(s, best) < 0)
367 /* this one's better */
370 s = RB_LEFT(s, link);
379 op_route_table(struct snmp_context *ctx __unused, struct snmp_value *value,
380 u_int sub, u_int iidx __unused, enum snmp_op op)
384 if (mib_fetch_route() == -1)
385 return (SNMP_ERR_GENERR);
389 case SNMP_OP_GETNEXT:
390 if ((r = sroute_getnext(&value->var, sub)) == NULL)
391 return (SNMP_ERR_NOSUCHNAME);
392 sroute_index_append(&value->var, sub, r);
396 if ((r = sroute_get(&value->var, sub)) == NULL)
397 return (SNMP_ERR_NOSUCHNAME);
401 if ((r = sroute_get(&value->var, sub)) == NULL)
402 return (SNMP_ERR_NOSUCHNAME);
403 return (SNMP_ERR_NOT_WRITEABLE);
405 case SNMP_OP_ROLLBACK:
413 switch (value->var.subs[sub - 1]) {
415 case LEAF_ipCidrRouteDest:
416 value->v.ipaddress[0] = r->index[0];
417 value->v.ipaddress[1] = r->index[1];
418 value->v.ipaddress[2] = r->index[2];
419 value->v.ipaddress[3] = r->index[3];
422 case LEAF_ipCidrRouteMask:
423 value->v.ipaddress[0] = r->index[4];
424 value->v.ipaddress[1] = r->index[5];
425 value->v.ipaddress[2] = r->index[6];
426 value->v.ipaddress[3] = r->index[7];
429 case LEAF_ipCidrRouteTos:
430 value->v.integer = r->index[8];
433 case LEAF_ipCidrRouteNextHop:
434 value->v.ipaddress[0] = r->index[9];
435 value->v.ipaddress[1] = r->index[10];
436 value->v.ipaddress[2] = r->index[11];
437 value->v.ipaddress[3] = r->index[12];
440 case LEAF_ipCidrRouteIfIndex:
441 value->v.integer = r->ifindex;
444 case LEAF_ipCidrRouteType:
445 value->v.integer = r->type;
448 case LEAF_ipCidrRouteProto:
449 value->v.integer = r->proto;
452 case LEAF_ipCidrRouteAge:
453 value->v.integer = 0;
456 case LEAF_ipCidrRouteInfo:
457 value->v.oid = oid_zeroDotZero;
460 case LEAF_ipCidrRouteNextHopAS:
461 value->v.integer = 0;
464 case LEAF_ipCidrRouteMetric1:
465 case LEAF_ipCidrRouteMetric2:
466 case LEAF_ipCidrRouteMetric3:
467 case LEAF_ipCidrRouteMetric4:
468 case LEAF_ipCidrRouteMetric5:
469 value->v.integer = -1;
472 case LEAF_ipCidrRouteStatus:
473 value->v.integer = 1;
476 return (SNMP_ERR_NOERROR);
483 op_route(struct snmp_context *ctx __unused, struct snmp_value *value,
484 u_int sub, u_int iidx __unused, enum snmp_op op)
488 case SNMP_OP_GETNEXT:
495 return (SNMP_ERR_NOT_WRITEABLE);
497 case SNMP_OP_ROLLBACK:
502 if (mib_fetch_route() == -1)
503 return (SNMP_ERR_GENERR);
505 switch (value->var.subs[sub - 1]) {
507 case LEAF_ipCidrRouteNumber:
508 value->v.uint32 = route_total;
512 return (SNMP_ERR_NOERROR);
515 RB_GENERATE(sroutes, sroute, link, sroute_compare);