]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bsnmp/snmp_mibII/mibII_route.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bsnmp / snmp_mibII / mibII_route.c
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.9 2005/10/06 07:15:00 brandt_h Exp $
30  *
31  * Routing table
32  */
33 #include "support.h"
34
35 #ifdef HAVE_SYS_TREE_H
36 #include <sys/tree.h>
37 #else
38 #include "tree.h"
39 #endif
40
41 #include "mibII.h"
42 #include "mibII_oid.h"
43
44 struct sroute {
45         RB_ENTRY(sroute) link;
46         uint32_t        ifindex;
47         uint8_t         index[13];
48         uint8_t         type;
49         uint8_t         proto;
50 };
51 RB_HEAD(sroutes, sroute) sroutes = RB_INITIALIZER(&sroutes);
52
53 RB_PROTOTYPE(sroutes, sroute, link, sroute_compare);
54
55 #define ROUTE_UPDATE_INTERVAL   (100 * 60 * 10) /* 10 min */
56 static uint64_t route_tick;
57 static u_int route_total;
58
59 /*
60  * Compare two routes
61  */
62 static int
63 sroute_compare(struct sroute *s1, struct sroute *s2)
64 {
65
66         return (memcmp(s1->index, s2->index, 13));
67 }
68
69 static void
70 sroute_index_append(struct asn_oid *oid, u_int sub, const struct sroute *s)
71 {
72         int i;
73
74         oid->len = sub + 13;
75         for (i = 0; i < 13; i++)
76                 oid->subs[sub + i] = s->index[i];
77 }
78
79 #if 0
80 static void
81 sroute_print(const struct sroute *r)
82 {
83         u_int i;
84
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);
88 }
89 #endif
90
91 /*
92  * process routing message
93  */
94 void
95 mib_sroute_process(struct rt_msghdr *rtm, struct sockaddr *gw,
96     struct sockaddr *dst, struct sockaddr *mask)
97 {
98         struct sockaddr_in *in_dst, *in_gw;
99         struct in_addr in_mask;
100         struct mibif *ifp;
101         struct sroute key;
102         struct sroute *r, *r1;
103         in_addr_t ha;
104
105         if (dst == NULL || gw == NULL || dst->sa_family != AF_INET ||
106             gw->sa_family != AF_INET)
107                 return;
108
109         in_dst = (struct sockaddr_in *)(void *)dst;
110         in_gw = (struct sockaddr_in *)(void *)gw;
111
112         if (rtm->rtm_flags & RTF_HOST)
113                 in_mask.s_addr = 0xffffffff;
114         else if (mask == NULL || mask->sa_len == 0)
115                 in_mask.s_addr = 0;
116         else
117                 in_mask = ((struct sockaddr_in *)(void *)mask)->sin_addr;
118
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;
125
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;
131
132         /* ToS */
133         key.index[8] = 0;
134
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;
140
141         if (rtm->rtm_type == RTM_DELETE) {
142                 r = RB_FIND(sroutes, &sroutes, &key);
143                 if (r == 0) {
144 #ifdef DEBUG_ROUTE
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],
151                             key.index[12]);
152 #endif
153                         return;
154                 }
155                 RB_REMOVE(sroutes, &sroutes, r);
156                 free(r);
157                 route_total--;
158 #ifdef DEBUG_ROUTE
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],
165                     key.index[12]);
166 #endif
167                 return;
168         }
169
170         /* GET or ADD */
171         ifp = NULL;
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);
176                         return;
177                 }
178                 mib_iflist_bad = 1;
179         }
180
181         if ((r = malloc(sizeof(*r))) == NULL) {
182                 syslog(LOG_ERR, "%m");
183                 return;
184         }
185
186         memcpy(r->index, key.index, sizeof(r->index));
187         r->ifindex = (ifp == NULL) ? 0 : ifp->index;
188
189         r->type = (rtm->rtm_flags & RTF_REJECT) ? 2 : 4;
190
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;
195
196         r1 = RB_INSERT(sroutes, &sroutes, r);
197         if (r1 != NULL) {
198 #ifdef DEBUG_ROUTE
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],
205                     key.index[12]);
206 #endif
207                 r1->ifindex = r->ifindex;
208                 r1->type = r->type;
209                 r1->proto = r->proto;
210                 free(r);
211                 return;
212         }
213
214         route_total++;
215 #ifdef DEBUG_ROUTE
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],
222             key.index[12]);
223 #endif
224 }
225
226 int
227 mib_fetch_route(void)
228 {
229         u_char *rtab, *next;
230         size_t len;
231         struct sroute *r, *r1;
232         struct rt_msghdr *rtm;
233         struct sockaddr *addrs[RTAX_MAX];
234
235         if (route_tick != 0 && route_tick + ROUTE_UPDATE_INTERVAL > this_tick)
236                 return (0);
237
238         /*
239          * Remove all routes
240          */
241         r = RB_MIN(sroutes, &sroutes);
242         while (r != NULL) {
243                 r1 = RB_NEXT(sroutes, &sroutes, r);
244                 RB_REMOVE(sroutes, &sroutes, r);
245                 free(r);
246                 r = r1;
247         }
248         route_total = 0;
249
250         if ((rtab = mib_fetch_rtab(AF_INET, NET_RT_DUMP, 0, &len)) == NULL)
251                 return (-1);
252
253         next = rtab;
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))
258                         continue;
259                 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
260
261                 
262                 mib_sroute_process(rtm, addrs[RTAX_GATEWAY], addrs[RTAX_DST],
263                     addrs[RTAX_NETMASK]);
264         }
265
266 #if 0
267         u_int n = 0;
268         r = RB_MIN(sroutes, &sroutes);
269         while (r != NULL) {
270                 printf("%u: ", n++);
271                 sroute_print(r);
272                 printf("\n");
273                 r = RB_NEXT(sroutes, &sroutes, r);
274         }
275 #endif
276         free(rtab);
277         route_tick = get_ticks();
278
279         return (0);
280 }
281
282 /**
283  * Find a route in the table.
284  */
285 static struct sroute *
286 sroute_get(const struct asn_oid *oid, u_int sub)
287 {
288         struct sroute key;
289         int i;
290
291         if (oid->len - sub != 13)
292                 return (NULL);
293         for (i = 0; i < 13; i++)
294                 key.index[i] = oid->subs[sub + i];
295         return (RB_FIND(sroutes, &sroutes, &key));
296 }
297
298 /**
299  * Find next route in the table. There is no such RB_ macro, so must
300  * dig into the innards of the RB stuff.
301  */
302 static struct sroute *
303 sroute_getnext(struct asn_oid *oid, u_int sub)
304 {
305         u_int i;
306         int comp;
307         struct sroute key;
308         struct sroute *best;
309         struct sroute *s;
310
311         /*
312          * We now, that the OID is at least the tableEntry OID. If it is,
313          * the user wants the first route.
314          */
315         if (oid->len == sub)
316                 return (RB_MIN(sroutes, &sroutes));
317
318         /*
319          * This is also true for any index that consists of zeros and is
320          * shorter than the full index.
321          */
322         if (oid->len < sub + 13) {
323                 for (i = sub; i < oid->len; i++)
324                         if (oid->subs[i] != 0)
325                                 break;
326                 if (i == oid->len)
327                         return (RB_MIN(sroutes, &sroutes));
328
329                 /*
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.
333                  */
334                 for (i = oid->len; i < sub + 13; i++)
335                         oid->subs[i] = 0;
336
337                 for (i = sub + 13 - 1; i >= sub; i--) {
338                         if (oid->subs[i] != 0) {
339                                 oid->subs[i]--;
340                                 break;
341                         }
342                         oid->subs[i] = ASN_MAXID;
343                 }
344                 oid->len = sub + 13;
345         }
346
347         /* build the index */
348         for (i = sub; i < sub + 13; i++)
349                 key.index[i - sub] = oid->subs[i];
350
351         /* now find the element */
352         best = NULL;
353         s = RB_ROOT(&sroutes);
354
355         while (s != NULL) {
356                 comp = sroute_compare(&key, s);
357                 if (comp >= 0) {
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);
361                         continue;
362                 }
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 */
368                         best = s;
369
370                 s = RB_LEFT(s, link);
371         }
372         return (best);
373 }
374
375 /*
376  * Table
377  */
378 int
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)
381 {
382         struct sroute *r;
383
384         if (mib_fetch_route() == -1)
385                 return (SNMP_ERR_GENERR);
386
387         switch (op) {
388
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);
393                 break;
394
395           case SNMP_OP_GET:
396                 if ((r = sroute_get(&value->var, sub)) == NULL)
397                         return (SNMP_ERR_NOSUCHNAME);
398                 break;
399
400           case SNMP_OP_SET:
401                 if ((r = sroute_get(&value->var, sub)) == NULL)
402                         return (SNMP_ERR_NOSUCHNAME);
403                 return (SNMP_ERR_NOT_WRITEABLE);
404
405           case SNMP_OP_ROLLBACK:
406           case SNMP_OP_COMMIT:
407                 abort();
408
409           default:
410                 abort();
411         }
412
413         switch (value->var.subs[sub - 1]) {
414
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];
420                 break;
421
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];
427                 break;
428
429           case LEAF_ipCidrRouteTos:
430                 value->v.integer = r->index[8];
431                 break;
432
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];
438                 break;
439
440           case LEAF_ipCidrRouteIfIndex:
441                 value->v.integer = r->ifindex;
442                 break;
443
444           case LEAF_ipCidrRouteType:
445                 value->v.integer = r->type;
446                 break;
447
448           case LEAF_ipCidrRouteProto:
449                 value->v.integer = r->proto;
450                 break;
451
452           case LEAF_ipCidrRouteAge:
453                 value->v.integer = 0;
454                 break;
455
456           case LEAF_ipCidrRouteInfo:
457                 value->v.oid = oid_zeroDotZero;
458                 break;
459
460           case LEAF_ipCidrRouteNextHopAS:
461                 value->v.integer = 0;
462                 break;
463
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;
470                 break;
471
472           case LEAF_ipCidrRouteStatus:
473                 value->v.integer = 1;
474                 break;
475         }
476         return (SNMP_ERR_NOERROR);
477 }
478
479 /*
480  * scalars
481  */
482 int
483 op_route(struct snmp_context *ctx __unused, struct snmp_value *value,
484     u_int sub, u_int iidx __unused, enum snmp_op op)
485 {
486         switch (op) {
487
488           case SNMP_OP_GETNEXT:
489                 abort();
490
491           case SNMP_OP_GET:
492                 break;
493
494           case SNMP_OP_SET:
495                 return (SNMP_ERR_NOT_WRITEABLE);
496
497           case SNMP_OP_ROLLBACK:
498           case SNMP_OP_COMMIT:
499                 abort();
500         }
501
502         if (mib_fetch_route() == -1)
503                 return (SNMP_ERR_GENERR);
504
505         switch (value->var.subs[sub - 1]) {
506
507           case LEAF_ipCidrRouteNumber:
508                 value->v.uint32 = route_total;
509                 break;
510
511         }
512         return (SNMP_ERR_NOERROR);
513 }
514
515 RB_GENERATE(sroutes, sroute, link, sroute_compare);