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.7 2005/06/09 12:36:53 brandt_h Exp $
35 #include "mibII_oid.h"
38 RB_ENTRY(sroute) link;
44 RB_HEAD(sroutes, sroute) sroutes = RB_INITIALIZER(&sroutes);
46 RB_PROTOTYPE(sroutes, sroute, link, sroute_compare);
48 #define ROUTE_UPDATE_INTERVAL (100 * 60 * 10) /* 10 min */
49 static uint64_t route_tick;
50 static u_int route_total;
56 sroute_compare(struct sroute *s1, struct sroute *s2)
59 return (memcmp(s1->index, s2->index, 13));
63 sroute_index_append(struct asn_oid *oid, u_int sub, const struct sroute *s)
68 for (i = 0; i < 13; i++)
69 oid->subs[sub + i] = s->index[i];
74 sroute_print(const struct sroute *r)
78 for (i = 0; i < 13 - 1; i++)
79 printf("%u.", r->index[i]);
80 printf("%u proto=%u type=%u", r->index[i], r->proto, r->type);
85 * process routing message
88 mib_sroute_process(struct rt_msghdr *rtm, struct sockaddr *gw,
89 struct sockaddr *dst, struct sockaddr *mask)
91 struct sockaddr_in *in_dst, *in_gw;
92 struct in_addr in_mask;
95 struct sroute *r, *r1;
98 if (dst == NULL || gw == NULL || dst->sa_family != AF_INET ||
99 gw->sa_family != AF_INET)
102 in_dst = (struct sockaddr_in *)(void *)dst;
103 in_gw = (struct sockaddr_in *)(void *)gw;
105 if (rtm->rtm_flags & RTF_HOST)
106 in_mask.s_addr = 0xffffffff;
107 else if (mask == NULL || mask->sa_len == 0)
110 in_mask = ((struct sockaddr_in *)(void *)mask)->sin_addr;
112 /* build the index */
113 ha = ntohl(in_dst->sin_addr.s_addr);
114 key.index[0] = (ha >> 24) & 0xff;
115 key.index[1] = (ha >> 16) & 0xff;
116 key.index[2] = (ha >> 8) & 0xff;
117 key.index[3] = (ha >> 0) & 0xff;
119 ha = ntohl(in_mask.s_addr);
120 key.index[4] = (ha >> 24) & 0xff;
121 key.index[5] = (ha >> 16) & 0xff;
122 key.index[6] = (ha >> 8) & 0xff;
123 key.index[7] = (ha >> 0) & 0xff;
128 ha = ntohl(in_gw->sin_addr.s_addr);
129 key.index[9] = (ha >> 24) & 0xff;
130 key.index[10] = (ha >> 16) & 0xff;
131 key.index[11] = (ha >> 8) & 0xff;
132 key.index[12] = (ha >> 0) & 0xff;
134 if (rtm->rtm_type == RTM_DELETE) {
135 r = RB_FIND(sroutes, &sroutes, &key);
138 syslog(LOG_WARNING, "%s: DELETE: %u.%u.%u.%u "
139 "%u.%u.%u.%u %u %u.%u.%u.%u not found", __func__,
140 key.index[0], key.index[1], key.index[2],
141 key.index[3], key.index[4], key.index[5],
142 key.index[6], key.index[7], key.index[8],
143 key.index[9], key.index[10], key.index[11],
148 RB_REMOVE(sroutes, &sroutes, r);
152 printf("%s: DELETE: %u.%u.%u.%u "
153 "%u.%u.%u.%u %u %u.%u.%u.%u\n", __func__,
154 key.index[0], key.index[1], key.index[2],
155 key.index[3], key.index[4], key.index[5],
156 key.index[6], key.index[7], key.index[8],
157 key.index[9], key.index[10], key.index[11],
165 if ((ifp = mib_find_if_sys(rtm->rtm_index)) == NULL) {
166 if (rtm->rtm_type == RTM_ADD) {
167 /* make it a get so the kernel fills the index */
168 mib_send_rtmsg(rtm, gw, dst, mask);
174 if ((r = malloc(sizeof(*r))) == NULL) {
175 syslog(LOG_ERR, "%m");
179 memcpy(r->index, key.index, sizeof(r->index));
180 r->ifindex = (ifp == NULL) ? 0 : ifp->index;
182 r->type = (rtm->rtm_flags & RTF_LLINFO) ? 3 :
183 (rtm->rtm_flags & RTF_REJECT) ? 2 : 4;
185 /* cannot really know, what protocol it runs */
186 r->proto = (rtm->rtm_flags & RTF_LOCAL) ? 2 :
187 (rtm->rtm_flags & RTF_STATIC) ? 3 :
188 (rtm->rtm_flags & RTF_DYNAMIC) ? 4 : 10;
190 r1 = RB_INSERT(sroutes, &sroutes, r);
193 syslog(LOG_WARNING, "%s: %u.%u.%u.%u "
194 "%u.%u.%u.%u %u %u.%u.%u.%u duplicate route", __func__,
195 key.index[0], key.index[1], key.index[2],
196 key.index[3], key.index[4], key.index[5],
197 key.index[6], key.index[7], key.index[8],
198 key.index[9], key.index[10], key.index[11],
201 r1->ifindex = r->ifindex;
203 r1->proto = r->proto;
210 printf("%s: ADD/GET: %u.%u.%u.%u "
211 "%u.%u.%u.%u %u %u.%u.%u.%u\n", __func__,
212 key.index[0], key.index[1], key.index[2],
213 key.index[3], key.index[4], key.index[5],
214 key.index[6], key.index[7], key.index[8],
215 key.index[9], key.index[10], key.index[11],
221 mib_fetch_route(void)
225 struct sroute *r, *r1;
226 struct rt_msghdr *rtm;
227 struct sockaddr *addrs[RTAX_MAX];
229 if (route_tick != 0 && route_tick + ROUTE_UPDATE_INTERVAL > this_tick)
235 r = RB_MIN(sroutes, &sroutes);
237 r1 = RB_NEXT(sroutes, &sroutes, r);
238 RB_REMOVE(sroutes, &sroutes, r);
244 if ((rtab = mib_fetch_rtab(AF_INET, NET_RT_DUMP, 0, &len)) == NULL)
248 for (next = rtab; next < rtab + len; next += rtm->rtm_msglen) {
249 rtm = (struct rt_msghdr *)(void *)next;
250 if (rtm->rtm_type != RTM_GET ||
251 !(rtm->rtm_flags & RTF_UP))
253 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
256 mib_sroute_process(rtm, addrs[RTAX_GATEWAY], addrs[RTAX_DST],
257 addrs[RTAX_NETMASK]);
262 r = RB_MIN(sroutes, &sroutes);
267 r = RB_NEXT(sroutes, &sroutes, r);
271 route_tick = get_ticks();
277 * Find a route in the table.
279 static struct sroute *
280 sroute_get(const struct asn_oid *oid, u_int sub)
285 if (oid->len - sub != 13)
287 for (i = 0; i < 13; i++)
288 key.index[i] = oid->subs[sub + i];
289 return (RB_FIND(sroutes, &sroutes, &key));
293 * Find next route in the table. There is no such RB_ macro, so must
294 * dig into the innards of the RB stuff.
296 static struct sroute *
297 sroute_getnext(struct asn_oid *oid, u_int sub)
306 * We now, that the OID is at least the tableEntry OID. If it is,
307 * the user wants the first route.
310 return (RB_MIN(sroutes, &sroutes));
313 * This is also true for any index that consists of zeros and is
314 * shorter than the full index.
316 if (oid->len < sub + 13) {
317 for (i = sub; i < oid->len; i++)
318 if (oid->subs[i] != 0)
321 return (RB_MIN(sroutes, &sroutes));
324 * Now if the index is too short, we fill it with zeros and then
325 * subtract one from the index. We can do this, because we now,
326 * that there is at least one index element that is not zero.
328 for (i = oid->len; i < sub + 13; i++)
331 for (i = sub + 13 - 1; i >= sub; i--) {
332 if (oid->subs[i] != 0) {
336 oid->subs[i] = ASN_MAXID;
341 /* build the index */
342 for (i = sub; i < sub + 13; i++)
343 key.index[i - sub] = oid->subs[i];
345 /* now find the element */
347 s = RB_ROOT(&sroutes);
350 comp = sroute_compare(&key, s);
352 /* The current element is smaller than what we search.
353 * Forget about it and move to the right subtree. */
354 s = RB_RIGHT(s, link);
357 /* the current element is larger than what we search.
358 * forget about the right subtree (its even larger), but
359 * the current element may be what we need. */
360 if (best == NULL || sroute_compare(s, best) < 0)
361 /* this one's better */
364 s = RB_LEFT(s, link);
373 op_route_table(struct snmp_context *ctx __unused, struct snmp_value *value,
374 u_int sub, u_int iidx __unused, enum snmp_op op)
378 if (mib_fetch_route() == -1)
379 return (SNMP_ERR_GENERR);
383 case SNMP_OP_GETNEXT:
384 if ((r = sroute_getnext(&value->var, sub)) == NULL)
385 return (SNMP_ERR_NOSUCHNAME);
386 sroute_index_append(&value->var, sub, r);
390 if ((r = sroute_get(&value->var, sub)) == NULL)
391 return (SNMP_ERR_NOSUCHNAME);
395 if ((r = sroute_get(&value->var, sub)) == NULL)
396 return (SNMP_ERR_NOSUCHNAME);
397 return (SNMP_ERR_NOT_WRITEABLE);
399 case SNMP_OP_ROLLBACK:
407 switch (value->var.subs[sub - 1]) {
409 case LEAF_ipCidrRouteDest:
410 value->v.ipaddress[0] = r->index[0];
411 value->v.ipaddress[1] = r->index[1];
412 value->v.ipaddress[2] = r->index[2];
413 value->v.ipaddress[3] = r->index[3];
416 case LEAF_ipCidrRouteMask:
417 value->v.ipaddress[0] = r->index[4];
418 value->v.ipaddress[1] = r->index[5];
419 value->v.ipaddress[2] = r->index[6];
420 value->v.ipaddress[3] = r->index[7];
423 case LEAF_ipCidrRouteTos:
424 value->v.integer = r->index[8];
427 case LEAF_ipCidrRouteNextHop:
428 value->v.ipaddress[0] = r->index[9];
429 value->v.ipaddress[1] = r->index[10];
430 value->v.ipaddress[2] = r->index[11];
431 value->v.ipaddress[3] = r->index[12];
434 case LEAF_ipCidrRouteIfIndex:
435 value->v.integer = r->ifindex;
438 case LEAF_ipCidrRouteType:
439 value->v.integer = r->type;
442 case LEAF_ipCidrRouteProto:
443 value->v.integer = r->proto;
446 case LEAF_ipCidrRouteAge:
447 value->v.integer = 0;
450 case LEAF_ipCidrRouteInfo:
451 value->v.oid = oid_zeroDotZero;
454 case LEAF_ipCidrRouteNextHopAS:
455 value->v.integer = 0;
458 case LEAF_ipCidrRouteMetric1:
459 case LEAF_ipCidrRouteMetric2:
460 case LEAF_ipCidrRouteMetric3:
461 case LEAF_ipCidrRouteMetric4:
462 case LEAF_ipCidrRouteMetric5:
463 value->v.integer = -1;
466 case LEAF_ipCidrRouteStatus:
467 value->v.integer = 1;
470 return (SNMP_ERR_NOERROR);
477 op_route(struct snmp_context *ctx __unused, struct snmp_value *value,
478 u_int sub, u_int iidx __unused, enum snmp_op op)
482 case SNMP_OP_GETNEXT:
489 return (SNMP_ERR_NOT_WRITEABLE);
491 case SNMP_OP_ROLLBACK:
496 if (mib_fetch_route() == -1)
497 return (SNMP_ERR_GENERR);
499 switch (value->var.subs[sub - 1]) {
501 case LEAF_ipCidrRouteNumber:
502 value->v.uint32 = route_total;
506 return (SNMP_ERR_NOERROR);
509 RB_GENERATE(sroutes, sroute, link, sroute_compare);