xref: /freebsd/contrib/bsnmp/snmp_mibII/mibII_route.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
1 /*
2  * Copyright (c) 2001-2003
3  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *	All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/snmp_mibII/mibII_route.c,v 1.7 2005/06/09 12:36:53 brandt_h Exp $
30  *
31  * Routing table
32  */
33 #include <sys/tree.h>
34 #include "mibII.h"
35 #include "mibII_oid.h"
36 
37 struct sroute {
38 	RB_ENTRY(sroute) link;
39 	uint32_t	ifindex;
40 	uint8_t		index[13];
41 	uint8_t		type;
42 	uint8_t		proto;
43 };
44 RB_HEAD(sroutes, sroute) sroutes = RB_INITIALIZER(&sroutes);
45 
46 RB_PROTOTYPE(sroutes, sroute, link, sroute_compare);
47 
48 #define	ROUTE_UPDATE_INTERVAL	(100 * 60 * 10)	/* 10 min */
49 static uint64_t route_tick;
50 static u_int route_total;
51 
52 /*
53  * Compare two routes
54  */
55 static int
56 sroute_compare(struct sroute *s1, struct sroute *s2)
57 {
58 
59 	return (memcmp(s1->index, s2->index, 13));
60 }
61 
62 static void
63 sroute_index_append(struct asn_oid *oid, u_int sub, const struct sroute *s)
64 {
65 	int i;
66 
67 	oid->len = sub + 13;
68 	for (i = 0; i < 13; i++)
69 		oid->subs[sub + i] = s->index[i];
70 }
71 
72 #if 0
73 static void
74 sroute_print(const struct sroute *r)
75 {
76 	u_int i;
77 
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);
81 }
82 #endif
83 
84 /*
85  * process routing message
86  */
87 void
88 mib_sroute_process(struct rt_msghdr *rtm, struct sockaddr *gw,
89     struct sockaddr *dst, struct sockaddr *mask)
90 {
91 	struct sockaddr_in *in_dst, *in_gw;
92 	struct in_addr in_mask;
93 	struct mibif *ifp;
94 	struct sroute key;
95 	struct sroute *r, *r1;
96 	in_addr_t ha;
97 
98 	if (dst == NULL || gw == NULL || dst->sa_family != AF_INET ||
99 	    gw->sa_family != AF_INET)
100 		return;
101 
102 	in_dst = (struct sockaddr_in *)(void *)dst;
103 	in_gw = (struct sockaddr_in *)(void *)gw;
104 
105 	if (rtm->rtm_flags & RTF_HOST)
106 		in_mask.s_addr = 0xffffffff;
107 	else if (mask == NULL || mask->sa_len == 0)
108 		in_mask.s_addr = 0;
109 	else
110 		in_mask = ((struct sockaddr_in *)(void *)mask)->sin_addr;
111 
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;
118 
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;
124 
125 	/* ToS */
126 	key.index[8] = 0;
127 
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;
133 
134 	if (rtm->rtm_type == RTM_DELETE) {
135 		r = RB_FIND(sroutes, &sroutes, &key);
136 		if (r == 0) {
137 #ifdef DEBUG_ROUTE
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],
144 			    key.index[12]);
145 #endif
146 			return;
147 		}
148 		RB_REMOVE(sroutes, &sroutes, r);
149 		free(r);
150 		route_total--;
151 #ifdef DEBUG_ROUTE
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],
158 		    key.index[12]);
159 #endif
160 		return;
161 	}
162 
163 	/* GET or ADD */
164 	ifp = NULL;
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);
169 			return;
170 		}
171 		mib_iflist_bad = 1;
172 	}
173 
174 	if ((r = malloc(sizeof(*r))) == NULL) {
175 		syslog(LOG_ERR, "%m");
176 		return;
177 	}
178 
179 	memcpy(r->index, key.index, sizeof(r->index));
180 	r->ifindex = (ifp == NULL) ? 0 : ifp->index;
181 
182 	r->type = (rtm->rtm_flags & RTF_LLINFO) ? 3 :
183 	    (rtm->rtm_flags & RTF_REJECT) ? 2 : 4;
184 
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;
189 
190 	r1 = RB_INSERT(sroutes, &sroutes, r);
191 	if (r1 != NULL) {
192 #ifdef DEBUG_ROUTE
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],
199 		    key.index[12]);
200 #endif
201 		r1->ifindex = r->ifindex;
202 		r1->type = r->type;
203 		r1->proto = r->proto;
204 		free(r);
205 		return;
206 	}
207 
208 	route_total++;
209 #ifdef DEBUG_ROUTE
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],
216 	    key.index[12]);
217 #endif
218 }
219 
220 int
221 mib_fetch_route(void)
222 {
223 	u_char *rtab, *next;
224 	size_t len;
225 	struct sroute *r, *r1;
226 	struct rt_msghdr *rtm;
227 	struct sockaddr *addrs[RTAX_MAX];
228 
229 	if (route_tick != 0 && route_tick + ROUTE_UPDATE_INTERVAL > this_tick)
230 		return (0);
231 
232 	/*
233 	 * Remove all routes
234 	 */
235 	r = RB_MIN(sroutes, &sroutes);
236 	while (r != NULL) {
237 		r1 = RB_NEXT(sroutes, &sroutes, r);
238 		RB_REMOVE(sroutes, &sroutes, r);
239 		free(r);
240 		r = r1;
241 	}
242 	route_total = 0;
243 
244 	if ((rtab = mib_fetch_rtab(AF_INET, NET_RT_DUMP, 0, &len)) == NULL)
245 		return (-1);
246 
247 	next = rtab;
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))
252 			continue;
253 		mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
254 
255 
256 		mib_sroute_process(rtm, addrs[RTAX_GATEWAY], addrs[RTAX_DST],
257 		    addrs[RTAX_NETMASK]);
258 	}
259 
260 #if 0
261 	u_int n = 0;
262 	r = RB_MIN(sroutes, &sroutes);
263 	while (r != NULL) {
264 		printf("%u: ", n++);
265 		sroute_print(r);
266 		printf("\n");
267 		r = RB_NEXT(sroutes, &sroutes, r);
268 	}
269 #endif
270 	free(rtab);
271 	route_tick = get_ticks();
272 
273 	return (0);
274 }
275 
276 /**
277  * Find a route in the table.
278  */
279 static struct sroute *
280 sroute_get(const struct asn_oid *oid, u_int sub)
281 {
282 	struct sroute key;
283 	int i;
284 
285 	if (oid->len - sub != 13)
286 		return (NULL);
287 	for (i = 0; i < 13; i++)
288 		key.index[i] = oid->subs[sub + i];
289 	return (RB_FIND(sroutes, &sroutes, &key));
290 }
291 
292 /**
293  * Find next route in the table. There is no such RB_ macro, so must
294  * dig into the innards of the RB stuff.
295  */
296 static struct sroute *
297 sroute_getnext(struct asn_oid *oid, u_int sub)
298 {
299 	u_int i;
300 	int comp;
301 	struct sroute key;
302 	struct sroute *best;
303 	struct sroute *s;
304 
305 	/*
306 	 * We now, that the OID is at least the tableEntry OID. If it is,
307 	 * the user wants the first route.
308 	 */
309 	if (oid->len == sub)
310 		return (RB_MIN(sroutes, &sroutes));
311 
312 	/*
313 	 * This is also true for any index that consists of zeros and is
314 	 * shorter than the full index.
315 	 */
316 	if (oid->len < sub + 13) {
317 		for (i = sub; i < oid->len; i++)
318 			if (oid->subs[i] != 0)
319 				break;
320 		if (i == oid->len)
321 			return (RB_MIN(sroutes, &sroutes));
322 
323 		/*
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.
327 		 */
328 		for (i = oid->len; i < sub + 13; i++)
329 			oid->subs[i] = 0;
330 
331 		for (i = sub + 13 - 1; i >= sub; i--) {
332 			if (oid->subs[i] != 0) {
333 				oid->subs[i]--;
334 				break;
335 			}
336 			oid->subs[i] = ASN_MAXID;
337 		}
338 		oid->len = sub + 13;
339 	}
340 
341 	/* build the index */
342 	for (i = sub; i < sub + 13; i++)
343 		key.index[i - sub] = oid->subs[i];
344 
345 	/* now find the element */
346 	best = NULL;
347 	s = RB_ROOT(&sroutes);
348 
349 	while (s != NULL) {
350 		comp = sroute_compare(&key, s);
351 		if (comp >= 0) {
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);
355 			continue;
356 		}
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 */
362 			best = s;
363 
364 		s = RB_LEFT(s, link);
365 	}
366 	return (best);
367 }
368 
369 /*
370  * Table
371  */
372 int
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)
375 {
376 	struct sroute *r;
377 
378 	if (mib_fetch_route() == -1)
379 		return (SNMP_ERR_GENERR);
380 
381 	switch (op) {
382 
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);
387 		break;
388 
389 	  case SNMP_OP_GET:
390 		if ((r = sroute_get(&value->var, sub)) == NULL)
391 			return (SNMP_ERR_NOSUCHNAME);
392 		break;
393 
394 	  case SNMP_OP_SET:
395 		if ((r = sroute_get(&value->var, sub)) == NULL)
396 			return (SNMP_ERR_NOSUCHNAME);
397 		return (SNMP_ERR_NOT_WRITEABLE);
398 
399 	  case SNMP_OP_ROLLBACK:
400 	  case SNMP_OP_COMMIT:
401 		abort();
402 
403 	  default:
404 		abort();
405 	}
406 
407 	switch (value->var.subs[sub - 1]) {
408 
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];
414 		break;
415 
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];
421 		break;
422 
423 	  case LEAF_ipCidrRouteTos:
424 		value->v.integer = r->index[8];
425 		break;
426 
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];
432 		break;
433 
434 	  case LEAF_ipCidrRouteIfIndex:
435 		value->v.integer = r->ifindex;
436 		break;
437 
438 	  case LEAF_ipCidrRouteType:
439 		value->v.integer = r->type;
440 		break;
441 
442 	  case LEAF_ipCidrRouteProto:
443 		value->v.integer = r->proto;
444 		break;
445 
446 	  case LEAF_ipCidrRouteAge:
447 		value->v.integer = 0;
448 		break;
449 
450 	  case LEAF_ipCidrRouteInfo:
451 		value->v.oid = oid_zeroDotZero;
452 		break;
453 
454 	  case LEAF_ipCidrRouteNextHopAS:
455 		value->v.integer = 0;
456 		break;
457 
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;
464 		break;
465 
466 	  case LEAF_ipCidrRouteStatus:
467 		value->v.integer = 1;
468 		break;
469 	}
470 	return (SNMP_ERR_NOERROR);
471 }
472 
473 /*
474  * scalars
475  */
476 int
477 op_route(struct snmp_context *ctx __unused, struct snmp_value *value,
478     u_int sub, u_int iidx __unused, enum snmp_op op)
479 {
480 	switch (op) {
481 
482 	  case SNMP_OP_GETNEXT:
483 		abort();
484 
485 	  case SNMP_OP_GET:
486 		break;
487 
488 	  case SNMP_OP_SET:
489 		return (SNMP_ERR_NOT_WRITEABLE);
490 
491 	  case SNMP_OP_ROLLBACK:
492 	  case SNMP_OP_COMMIT:
493 		abort();
494 	}
495 
496 	if (mib_fetch_route() == -1)
497 		return (SNMP_ERR_GENERR);
498 
499 	switch (value->var.subs[sub - 1]) {
500 
501 	  case LEAF_ipCidrRouteNumber:
502 		value->v.uint32 = route_total;
503 		break;
504 
505 	}
506 	return (SNMP_ERR_NOERROR);
507 }
508 
509 RB_GENERATE(sroutes, sroute, link, sroute_compare);
510