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_LLINFO) ? 3 : 190 (rtm->rtm_flags & RTF_REJECT) ? 2 : 4; 191 192 /* cannot really know, what protocol it runs */ 193 r->proto = (rtm->rtm_flags & RTF_LOCAL) ? 2 : 194 (rtm->rtm_flags & RTF_STATIC) ? 3 : 195 (rtm->rtm_flags & RTF_DYNAMIC) ? 4 : 10; 196 197 r1 = RB_INSERT(sroutes, &sroutes, r); 198 if (r1 != NULL) { 199 #ifdef DEBUG_ROUTE 200 syslog(LOG_WARNING, "%s: %u.%u.%u.%u " 201 "%u.%u.%u.%u %u %u.%u.%u.%u duplicate route", __func__, 202 key.index[0], key.index[1], key.index[2], 203 key.index[3], key.index[4], key.index[5], 204 key.index[6], key.index[7], key.index[8], 205 key.index[9], key.index[10], key.index[11], 206 key.index[12]); 207 #endif 208 r1->ifindex = r->ifindex; 209 r1->type = r->type; 210 r1->proto = r->proto; 211 free(r); 212 return; 213 } 214 215 route_total++; 216 #ifdef DEBUG_ROUTE 217 printf("%s: ADD/GET: %u.%u.%u.%u " 218 "%u.%u.%u.%u %u %u.%u.%u.%u\n", __func__, 219 key.index[0], key.index[1], key.index[2], 220 key.index[3], key.index[4], key.index[5], 221 key.index[6], key.index[7], key.index[8], 222 key.index[9], key.index[10], key.index[11], 223 key.index[12]); 224 #endif 225 } 226 227 int 228 mib_fetch_route(void) 229 { 230 u_char *rtab, *next; 231 size_t len; 232 struct sroute *r, *r1; 233 struct rt_msghdr *rtm; 234 struct sockaddr *addrs[RTAX_MAX]; 235 236 if (route_tick != 0 && route_tick + ROUTE_UPDATE_INTERVAL > this_tick) 237 return (0); 238 239 /* 240 * Remove all routes 241 */ 242 r = RB_MIN(sroutes, &sroutes); 243 while (r != NULL) { 244 r1 = RB_NEXT(sroutes, &sroutes, r); 245 RB_REMOVE(sroutes, &sroutes, r); 246 free(r); 247 r = r1; 248 } 249 route_total = 0; 250 251 if ((rtab = mib_fetch_rtab(AF_INET, NET_RT_DUMP, 0, &len)) == NULL) 252 return (-1); 253 254 next = rtab; 255 for (next = rtab; next < rtab + len; next += rtm->rtm_msglen) { 256 rtm = (struct rt_msghdr *)(void *)next; 257 if (rtm->rtm_type != RTM_GET || 258 !(rtm->rtm_flags & RTF_UP)) 259 continue; 260 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs); 261 262 263 mib_sroute_process(rtm, addrs[RTAX_GATEWAY], addrs[RTAX_DST], 264 addrs[RTAX_NETMASK]); 265 } 266 267 #if 0 268 u_int n = 0; 269 r = RB_MIN(sroutes, &sroutes); 270 while (r != NULL) { 271 printf("%u: ", n++); 272 sroute_print(r); 273 printf("\n"); 274 r = RB_NEXT(sroutes, &sroutes, r); 275 } 276 #endif 277 free(rtab); 278 route_tick = get_ticks(); 279 280 return (0); 281 } 282 283 /** 284 * Find a route in the table. 285 */ 286 static struct sroute * 287 sroute_get(const struct asn_oid *oid, u_int sub) 288 { 289 struct sroute key; 290 int i; 291 292 if (oid->len - sub != 13) 293 return (NULL); 294 for (i = 0; i < 13; i++) 295 key.index[i] = oid->subs[sub + i]; 296 return (RB_FIND(sroutes, &sroutes, &key)); 297 } 298 299 /** 300 * Find next route in the table. There is no such RB_ macro, so must 301 * dig into the innards of the RB stuff. 302 */ 303 static struct sroute * 304 sroute_getnext(struct asn_oid *oid, u_int sub) 305 { 306 u_int i; 307 int comp; 308 struct sroute key; 309 struct sroute *best; 310 struct sroute *s; 311 312 /* 313 * We now, that the OID is at least the tableEntry OID. If it is, 314 * the user wants the first route. 315 */ 316 if (oid->len == sub) 317 return (RB_MIN(sroutes, &sroutes)); 318 319 /* 320 * This is also true for any index that consists of zeros and is 321 * shorter than the full index. 322 */ 323 if (oid->len < sub + 13) { 324 for (i = sub; i < oid->len; i++) 325 if (oid->subs[i] != 0) 326 break; 327 if (i == oid->len) 328 return (RB_MIN(sroutes, &sroutes)); 329 330 /* 331 * Now if the index is too short, we fill it with zeros and then 332 * subtract one from the index. We can do this, because we now, 333 * that there is at least one index element that is not zero. 334 */ 335 for (i = oid->len; i < sub + 13; i++) 336 oid->subs[i] = 0; 337 338 for (i = sub + 13 - 1; i >= sub; i--) { 339 if (oid->subs[i] != 0) { 340 oid->subs[i]--; 341 break; 342 } 343 oid->subs[i] = ASN_MAXID; 344 } 345 oid->len = sub + 13; 346 } 347 348 /* build the index */ 349 for (i = sub; i < sub + 13; i++) 350 key.index[i - sub] = oid->subs[i]; 351 352 /* now find the element */ 353 best = NULL; 354 s = RB_ROOT(&sroutes); 355 356 while (s != NULL) { 357 comp = sroute_compare(&key, s); 358 if (comp >= 0) { 359 /* The current element is smaller than what we search. 360 * Forget about it and move to the right subtree. */ 361 s = RB_RIGHT(s, link); 362 continue; 363 } 364 /* the current element is larger than what we search. 365 * forget about the right subtree (its even larger), but 366 * the current element may be what we need. */ 367 if (best == NULL || sroute_compare(s, best) < 0) 368 /* this one's better */ 369 best = s; 370 371 s = RB_LEFT(s, link); 372 } 373 return (best); 374 } 375 376 /* 377 * Table 378 */ 379 int 380 op_route_table(struct snmp_context *ctx __unused, struct snmp_value *value, 381 u_int sub, u_int iidx __unused, enum snmp_op op) 382 { 383 struct sroute *r; 384 385 if (mib_fetch_route() == -1) 386 return (SNMP_ERR_GENERR); 387 388 switch (op) { 389 390 case SNMP_OP_GETNEXT: 391 if ((r = sroute_getnext(&value->var, sub)) == NULL) 392 return (SNMP_ERR_NOSUCHNAME); 393 sroute_index_append(&value->var, sub, r); 394 break; 395 396 case SNMP_OP_GET: 397 if ((r = sroute_get(&value->var, sub)) == NULL) 398 return (SNMP_ERR_NOSUCHNAME); 399 break; 400 401 case SNMP_OP_SET: 402 if ((r = sroute_get(&value->var, sub)) == NULL) 403 return (SNMP_ERR_NOSUCHNAME); 404 return (SNMP_ERR_NOT_WRITEABLE); 405 406 case SNMP_OP_ROLLBACK: 407 case SNMP_OP_COMMIT: 408 abort(); 409 410 default: 411 abort(); 412 } 413 414 switch (value->var.subs[sub - 1]) { 415 416 case LEAF_ipCidrRouteDest: 417 value->v.ipaddress[0] = r->index[0]; 418 value->v.ipaddress[1] = r->index[1]; 419 value->v.ipaddress[2] = r->index[2]; 420 value->v.ipaddress[3] = r->index[3]; 421 break; 422 423 case LEAF_ipCidrRouteMask: 424 value->v.ipaddress[0] = r->index[4]; 425 value->v.ipaddress[1] = r->index[5]; 426 value->v.ipaddress[2] = r->index[6]; 427 value->v.ipaddress[3] = r->index[7]; 428 break; 429 430 case LEAF_ipCidrRouteTos: 431 value->v.integer = r->index[8]; 432 break; 433 434 case LEAF_ipCidrRouteNextHop: 435 value->v.ipaddress[0] = r->index[9]; 436 value->v.ipaddress[1] = r->index[10]; 437 value->v.ipaddress[2] = r->index[11]; 438 value->v.ipaddress[3] = r->index[12]; 439 break; 440 441 case LEAF_ipCidrRouteIfIndex: 442 value->v.integer = r->ifindex; 443 break; 444 445 case LEAF_ipCidrRouteType: 446 value->v.integer = r->type; 447 break; 448 449 case LEAF_ipCidrRouteProto: 450 value->v.integer = r->proto; 451 break; 452 453 case LEAF_ipCidrRouteAge: 454 value->v.integer = 0; 455 break; 456 457 case LEAF_ipCidrRouteInfo: 458 value->v.oid = oid_zeroDotZero; 459 break; 460 461 case LEAF_ipCidrRouteNextHopAS: 462 value->v.integer = 0; 463 break; 464 465 case LEAF_ipCidrRouteMetric1: 466 case LEAF_ipCidrRouteMetric2: 467 case LEAF_ipCidrRouteMetric3: 468 case LEAF_ipCidrRouteMetric4: 469 case LEAF_ipCidrRouteMetric5: 470 value->v.integer = -1; 471 break; 472 473 case LEAF_ipCidrRouteStatus: 474 value->v.integer = 1; 475 break; 476 } 477 return (SNMP_ERR_NOERROR); 478 } 479 480 /* 481 * scalars 482 */ 483 int 484 op_route(struct snmp_context *ctx __unused, struct snmp_value *value, 485 u_int sub, u_int iidx __unused, enum snmp_op op) 486 { 487 switch (op) { 488 489 case SNMP_OP_GETNEXT: 490 abort(); 491 492 case SNMP_OP_GET: 493 break; 494 495 case SNMP_OP_SET: 496 return (SNMP_ERR_NOT_WRITEABLE); 497 498 case SNMP_OP_ROLLBACK: 499 case SNMP_OP_COMMIT: 500 abort(); 501 } 502 503 if (mib_fetch_route() == -1) 504 return (SNMP_ERR_GENERR); 505 506 switch (value->var.subs[sub - 1]) { 507 508 case LEAF_ipCidrRouteNumber: 509 value->v.uint32 = route_total; 510 break; 511 512 } 513 return (SNMP_ERR_NOERROR); 514 } 515 516 RB_GENERATE(sroutes, sroute, link, sroute_compare); 517