1 /*	$OpenBSD: route.c,v 1.66 2004/11/17 01:47:20 itojun Exp $	*/
2 /*	$NetBSD: route.c,v 1.15 1996/05/07 02:55:06 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
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  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <net-snmp/net-snmp-config.h>
34 
35 #if HAVE_SYS_TYPES_H
36 #include <sys/types.h>
37 #endif
38 
39 #if HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
44 #endif
45 
46 #include <net-snmp/net-snmp-includes.h>
47 
48 #if HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 #if HAVE_NETDB_H
52 #include <netdb.h>
53 #endif
54 
55 #ifndef INET
56 #define INET
57 #endif
58 
59 #include "main.h"
60 #include "netstat.h"
61 #include "ffs.h"
62 #if HAVE_WINSOCK_H
63 #include "winstub.h"
64 #endif
65 
66 #define SET_MASK 0x01
67 #define SET_GWAY 0x02
68 #define SET_IFNO 0x04
69 #define SET_TYPE 0x08
70 #define SET_PRTO 0x10
71 #define SET_ALL  0x1f
72 
73 struct route_entry {
74     in_addr_t       destination;
75     in_addr_t       mask;
76     in_addr_t       gateway;
77     int             ifNumber;
78     int             type;
79     int             proto;
80     int             af;
81     int             set_bits;
82     char            ifname[64];
83 };
84 
85 void p_rtnode( struct route_entry *rp );
86 
87 /*
88  * Print routing tables.
89  */
90 void
routepr(void)91 routepr(void)
92 {
93     struct route_entry  route, *rp = &route;
94     oid    rtcol_oid[]  = { 1,3,6,1,2,1,4,21,1,0 };
95     size_t rtcol_len    = OID_LENGTH( rtcol_oid );
96     union {
97         in_addr_t addr;
98         char      data[4];
99     } tmpAddr;
100     netsnmp_variable_list *var=NULL, *vp;
101     char  *cp;
102 
103     printf("Routing tables (ipRouteTable)\n");
104     pr_rthdr(AF_INET);
105 
106 #define ADD_RTVAR( x ) rtcol_oid[ rtcol_len-1 ] = x; \
107     snmp_varlist_add_variable( &var, rtcol_oid, rtcol_len, ASN_NULL, NULL,  0)
108     ADD_RTVAR( 2 );                 /* ipRouteIfIndex */
109     ADD_RTVAR( 7 );                 /* ipRouteNextHop */
110     ADD_RTVAR( 8 );                 /* ipRouteType    */
111     ADD_RTVAR( 9 );                 /* ipRouteProto   */
112     ADD_RTVAR( 11 );                /* ipRouteMask    */
113 #undef ADD_RTVAR
114 
115         /*
116 	 * Now walk the ipRouteTable, reporting the various route entries
117 	 */
118     while ( 1 ) {
119         if (netsnmp_query_getnext( var, ss ) != SNMP_ERR_NOERROR)
120             break;
121         rtcol_oid[ rtcol_len-1 ] = 2;	/* ifRouteIfIndex */
122         if ( snmp_oid_compare( rtcol_oid, rtcol_len,
123                                var->name, rtcol_len) != 0 )
124             break;    /* End of Table */
125 	if (var->type == SNMP_NOSUCHOBJECT ||
126                 var->type == SNMP_NOSUCHINSTANCE ||
127                 var->type == SNMP_ENDOFMIBVIEW)
128 	    break;
129         memset( &route, 0, sizeof( struct route_entry ));
130         /* Extract ipRouteDest index value */
131         cp = tmpAddr.data;
132         cp[0] = var->name[ 10 ] & 0xff;
133         cp[1] = var->name[ 11 ] & 0xff;
134         cp[2] = var->name[ 12 ] & 0xff;
135         cp[3] = var->name[ 13 ] & 0xff;
136         rp->destination = tmpAddr.addr;
137 
138         for ( vp=var; vp; vp=vp->next_variable ) {
139             switch ( vp->name[ 9 ] ) {
140             case 2:     /* ifRouteIfIndex */
141                 rp->ifNumber  = *vp->val.integer;
142                 rp->set_bits |= SET_IFNO;
143                 break;
144             case 7:                 /* ipRouteNextHop */
145                 memmove(&rp->gateway, vp->val.string, 4);
146                 rp->set_bits |= SET_GWAY;
147                 break;
148             case 8:                 /* ipRouteType    */
149                 rp->type      = *vp->val.integer;
150                 rp->set_bits |= SET_TYPE;
151                 break;
152             case 9:                 /* ipRouteProto   */
153                 rp->proto     = *vp->val.integer;
154                 rp->set_bits |= SET_PRTO;
155                 break;
156             case 11:                /* ipRouteMask    */
157                 memmove(&rp->mask, vp->val.string, 4);
158                 rp->set_bits |= SET_MASK;
159                 break;
160             }
161         }
162         if (rp->set_bits != SET_ALL) {
163             continue;   /* Incomplete query */
164         }
165 
166         p_rtnode( rp );
167     }
168 }
169 
170 
171 int
route4pr(int af)172 route4pr(int af)
173 {
174     struct route_entry  route, *rp = &route;
175     oid    rtcol_oid[]  = { 1,3,6,1,2,1,4,24,4,1,0 }; /* ipCidrRouteEntry */
176     size_t rtcol_len    = OID_LENGTH( rtcol_oid );
177     netsnmp_variable_list *var = NULL, *vp;
178     union {
179         in_addr_t addr;
180         unsigned char data[4];
181     } tmpAddr;
182     int printed = 0;
183     int hdr_af = AF_UNSPEC;
184 
185     if (af != AF_UNSPEC && af != AF_INET)
186         return 0;
187 
188 #define ADD_RTVAR( x ) rtcol_oid[ rtcol_len-1 ] = x; \
189     snmp_varlist_add_variable( &var, rtcol_oid, rtcol_len, ASN_NULL, NULL,  0)
190     ADD_RTVAR( 5 );                 /* ipCidrRouteIfIndex */
191     ADD_RTVAR( 6 );                 /* ipCidrRouteType    */
192     ADD_RTVAR( 7 );                 /* ipCidrRouteProto   */
193 #undef ADD_RTVAR
194 
195     /*
196      * Now walk the ipCidrRouteTable, reporting the various route entries
197      */
198     while ( 1 ) {
199         oid *op;
200         unsigned char *cp;
201 
202         if (netsnmp_query_getnext( var, ss ) != SNMP_ERR_NOERROR)
203             break;
204         rtcol_oid[ rtcol_len-1 ] = 5;        /* ipRouteIfIndex */
205         if ( snmp_oid_compare( rtcol_oid, rtcol_len,
206                                var->name, rtcol_len) != 0 )
207             break;    /* End of Table */
208         if (var->type == SNMP_NOSUCHOBJECT ||
209                 var->type == SNMP_NOSUCHINSTANCE ||
210                 var->type == SNMP_ENDOFMIBVIEW)
211             break;
212         memset( &route, 0, sizeof( struct route_entry ));
213 	rp->af = AF_INET;
214 	op = var->name+rtcol_len;
215         cp = tmpAddr.data;
216         cp[0] = *op++ & 0xff;
217         cp[1] = *op++ & 0xff;
218         cp[2] = *op++ & 0xff;
219         cp[3] = *op++ & 0xff;
220         rp->destination = tmpAddr.addr;
221         cp = tmpAddr.data;
222         cp[0] = *op++ & 0xff;
223         cp[1] = *op++ & 0xff;
224         cp[2] = *op++ & 0xff;
225         cp[3] = *op++ & 0xff;
226         rp->mask = tmpAddr.addr;
227 	op++; /* ipCidrRouteTos */
228         cp = tmpAddr.data;
229         cp[0] = *op++ & 0xff;
230         cp[1] = *op++ & 0xff;
231         cp[2] = *op++ & 0xff;
232         cp[3] = *op++ & 0xff;
233         rp->gateway = tmpAddr.addr;
234 	rp->set_bits = SET_MASK | SET_GWAY;
235 
236         for ( vp=var; vp; vp=vp->next_variable ) {
237             switch ( vp->name[ rtcol_len - 1 ] ) {
238             case 5:     /* ipCidrRouteIfIndex */
239                 rp->ifNumber  = *vp->val.integer;
240                 rp->set_bits |= SET_IFNO;
241                 break;
242             case 6:     /* ipCidrRouteType    */
243                 rp->type      = *vp->val.integer;
244                 rp->set_bits |= SET_TYPE;
245                 break;
246             case 7:     /* ipCidrRouteProto   */
247                 rp->proto     = *vp->val.integer;
248                 rp->set_bits |= SET_PRTO;
249                 break;
250             }
251         }
252         if (rp->set_bits != SET_ALL) {
253             continue;   /* Incomplete query */
254         }
255 
256         if (hdr_af != rp->af) {
257             if (hdr_af != AF_UNSPEC)
258                 printf("\n");
259             hdr_af = rp->af;
260 	    printf("Routing tables (ipCidrRouteTable)\n");
261             pr_rthdr(hdr_af);
262         }
263         p_rtnode( rp );
264         printed++;
265     }
266     snmp_free_varbind(var);
267     return printed;
268 }
269 
270 
271 struct iflist {
272     int             index;
273     char            name[64];
274     struct iflist  *next;
275 }              *Iflist = NULL;
276 
277 void
get_ifname(char * name,int ifIndex)278 get_ifname(char *name, int ifIndex)
279 {
280     oid    ifdescr_oid[]  = { 1,3,6,1,2,1,2,2,1,2,0 };
281     size_t ifdescr_len    = OID_LENGTH( ifdescr_oid );
282     oid    ifxcol_oid[] = { 1,3,6,1,2,1,31,1,1,1,1,0 };
283     size_t ifxcol_len   = OID_LENGTH( ifxcol_oid );
284     netsnmp_variable_list *var = NULL;
285     struct iflist         *ip;
286 
287     for (ip = Iflist; ip; ip = ip->next) {
288         if (ip->index == ifIndex)
289             break;
290     }
291     if (ip) {
292         strcpy(name, ip->name);
293         return;
294     }
295     ip = (struct iflist *) malloc(sizeof(struct iflist));
296     if (ip == NULL)
297         return;
298     ip->next = Iflist;
299     Iflist = ip;
300     ip->index = ifIndex;
301 
302     ifxcol_oid[ ifxcol_len-1 ] = ifIndex;
303     snmp_varlist_add_variable( &var, ifxcol_oid, ifxcol_len,
304                                ASN_NULL, NULL,  0);
305     if (netsnmp_query_get( var, ss ) == SNMP_ERR_NOERROR) {
306         if (var->val_len >= sizeof(ip->name))
307             var->val_len  = sizeof(ip->name) - 1;
308         memmove(ip->name, var->val.string, var->val_len);
309         ip->name[var->val_len] = '\0';
310 	strcpy(name, ip->name);
311 	snmp_free_varbind(var);
312 	return;
313     }
314 
315     ifdescr_oid[ ifdescr_len-1 ] = ifIndex;
316     snmp_varlist_add_variable( &var, ifdescr_oid, ifdescr_len,
317                                ASN_NULL, NULL,  0);
318     if (netsnmp_query_get( var, ss ) == SNMP_ERR_NOERROR) {
319         if (var->val_len >= sizeof(ip->name))
320             var->val_len  = sizeof(ip->name) - 1;
321         memmove(ip->name, var->val.string, var->val_len);
322         ip->name[var->val_len] = '\0';
323 	snmp_free_varbind(var);
324     } else {
325         sprintf(ip->name, "if%d", ifIndex);
326     }
327     strcpy(name, ip->name);
328 }
329 
330 /* column widths; each followed by one space */
331 #ifndef NETSNMP_ENABLE_IPV6
332 #define	WID_DST(af)	26	/* width of destination column */
333 #define	WID_GW(af)	18	/* width of gateway column */
334 #else
335 /* width of destination/gateway column */
336 #if 1
337 /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
338 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 34 : 26) : 26)
339 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
340 #else
341 /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
342 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
343 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
344 #endif
345 #endif /* NETSNMP_ENABLE_IPV6 */
346 
347 /*
348  * Print header for routing table columns.
349  */
350 void
pr_rthdr(int af)351 pr_rthdr(int af)
352 {
353    /*
354 	if (Aflag)
355 		printf("%-*.*s ", PLEN, PLEN, "Address");
356 	if (Sflag)
357 		printf("%-*.*s ",
358 		    WID_DST(af), WID_DST(af), "Source");
359     */
360 	printf("%-*.*s ",
361 	    WID_DST(af), WID_DST(af), "Destination");
362 	printf("%-*.*s %-6.6s  %s\n",
363 	    WID_GW(af), WID_GW(af), "Gateway",
364 	    "Flags", "Interface");
365 }
366 
367 /*
368  * Print header for PF_KEY entries.
369 void
370 pr_encaphdr(void)
371 {
372 	if (Aflag)
373 		printf("%-*s ", PLEN, "Address");
374 	printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
375 	    "Source", "Port", "Destination",
376 	    "Port", "Proto", "SA(Address/Proto/Type/Direction)");
377 }
378  */
379 
380 char *
routename(in_addr_t in)381 routename(in_addr_t in)
382 {
383 	char *cp;
384 	static char line[MAXHOSTNAMELEN];
385 	struct hostent *hp;
386 	static char domain[MAXHOSTNAMELEN];
387 	static int first = 1;
388 
389 	if (first) {
390 		first = 0;
391 		if (gethostname(line, sizeof line) == 0 &&
392 		    (cp = strchr(line, '.')))
393 			(void) strlcpy(domain, cp + 1, sizeof domain);
394 		else
395 			domain[0] = '\0';
396 	}
397 	cp = NULL;
398 	if (!nflag) {
399 		hp = netsnmp_gethostbyaddr((char *)&in, sizeof (struct in_addr),
400 		    AF_INET);
401 		if (hp) {
402 			if ((cp = strchr(hp->h_name, '.')) &&
403 			    !strcmp(cp + 1, domain))
404 				*cp = '\0';
405 			cp = hp->h_name;
406 		}
407 	}
408 	if (cp) {
409 		strlcpy(line, cp, sizeof(line));
410 	} else {
411 #define C(x)	(unsigned)((x) & 0xff)
412 		in = ntohl(in);
413 		snprintf(line, sizeof line, "%u.%u.%u.%u",
414 		    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
415 	}
416 	return (line);
417 }
418 
419 /*
420  * Return the name of the network whose address is given.
421  * The address is assumed to be that of a net or subnet, not a host.
422  */
423 char *
netname(in_addr_t in,in_addr_t mask)424 netname(in_addr_t in, in_addr_t mask)
425 {
426 	char *cp = NULL;
427 	static char line[MAXHOSTNAMELEN];
428 	struct netent *np = NULL;
429 	int mbits;
430 
431 	in = ntohl(in);
432 	mask = ntohl(mask);
433 	if (!nflag && in != INADDR_ANY) {
434 		if ((np = getnetbyaddr(in, AF_INET)) != NULL)
435 			cp = np->n_name;
436 	}
437 	mbits = mask ? 33 - _ffs(mask) : 0;
438 	if (cp) {
439 		strlcpy(line, cp, sizeof(line));
440 	} else if (mbits < 9)
441 		snprintf(line, sizeof line, "%u/%d", C(in >> 24), mbits);
442 	else if (mbits < 17)
443 		snprintf(line, sizeof line, "%u.%u/%d",
444 		    C(in >> 24) , C(in >> 16), mbits);
445 	else if (mbits < 25)
446 		snprintf(line, sizeof line, "%u.%u.%u/%d",
447 		    C(in >> 24), C(in >> 16), C(in >> 8), mbits);
448 	else
449 		snprintf(line, sizeof line, "%u.%u.%u.%u/%d", C(in >> 24),
450 		    C(in >> 16), C(in >> 8), C(in), mbits);
451 	return (line);
452 }
453 
454 #undef NETSNMP_ENABLE_IPV6
455 #ifdef NETSNMP_ENABLE_IPV6
456 char *
netname6(struct sockaddr_in6 * sa6,struct in6_addr * mask)457 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
458 {
459 	static char line[MAXHOSTNAMELEN + 1];
460 	struct sockaddr_in6 sin6;
461 	u_char *p;
462 	u_char *lim;
463 	int masklen, final = 0, illegal = 0;
464 	int i;
465 	char hbuf[NI_MAXHOST];
466 	int flag = 0;
467 	int error;
468 
469 	sin6 = *sa6;
470 
471 	masklen = 0;
472 	lim = (u_char *)(mask + 1);
473 	i = 0;
474 	if (mask) {
475 		for (p = (u_char *)mask; p < lim; p++) {
476 			if (final && *p) {
477 				illegal++;
478 				sin6.sin6_addr.s6_addr[i++] = 0x00;
479 				continue;
480 			}
481 
482 			switch (*p & 0xff) {
483 			case 0xff:
484 				masklen += 8;
485 				break;
486 			case 0xfe:
487 				masklen += 7;
488 				final++;
489 				break;
490 			case 0xfc:
491 				masklen += 6;
492 				final++;
493 				break;
494 			case 0xf8:
495 				masklen += 5;
496 				final++;
497 				break;
498 			case 0xf0:
499 				masklen += 4;
500 				final++;
501 				break;
502 			case 0xe0:
503 				masklen += 3;
504 				final++;
505 				break;
506 			case 0xc0:
507 				masklen += 2;
508 				final++;
509 				break;
510 			case 0x80:
511 				masklen += 1;
512 				final++;
513 				break;
514 			case 0x00:
515 				final++;
516 				break;
517 			default:
518 				final++;
519 				illegal++;
520 				break;
521 			}
522 
523 			if (!illegal)
524 				sin6.sin6_addr.s6_addr[i++] &= *p;
525 			else
526 				sin6.sin6_addr.s6_addr[i++] = 0x00;
527 		}
528 	} else
529 		masklen = 128;
530 
531 	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
532 		return("default");
533 
534 	if (illegal)
535 		fprintf(stderr, "illegal prefixlen\n");
536 
537 	if (nflag)
538 		flag |= NI_NUMERICHOST;
539 	error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
540 	    hbuf, sizeof(hbuf), NULL, 0, flag);
541 	if (error)
542 		snprintf(hbuf, sizeof(hbuf), "invalid");
543 
544 	snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
545 	return line;
546 }
547 
548 char *
routename6(struct sockaddr_in6 * sa6)549 routename6(struct sockaddr_in6 *sa6)
550 {
551 	static char line[NI_MAXHOST];
552 	const int niflag = NI_NUMERICHOST;
553 
554 	if (getnameinfo((struct sockaddr *)sa6, sa6->sin6_len,
555 	    line, sizeof(line), NULL, 0, niflag) != 0)
556 		strlcpy(line, "", sizeof line);
557 	return line;
558 }
559 #endif /*NETSNMP_ENABLE_IPV6*/
560 
561 char *
s_rtflags(struct route_entry * rp)562 s_rtflags( struct route_entry *rp )
563 {
564     static char flag_buf[10];
565     char  *cp = flag_buf;
566 
567     *cp++ = '<';
568     *cp++ = 'U';   /* route is in use */
569     if (rp->mask  == 0xffffffff)
570         *cp++ = 'H';   /* host */
571     if (rp->proto == 4)
572         *cp++ = 'D';   /* ICMP redirect */
573     if (rp->type  == 4)
574         *cp++ = 'G';   /* remote destination/net */
575     *cp++ = '>';
576     *cp = 0;
577     return flag_buf;
578 }
579 
580 void
p_rtnode(struct route_entry * rp)581 p_rtnode( struct route_entry *rp )
582 {
583     get_ifname(rp->ifname, rp->ifNumber);
584     printf("%-*.*s ",
585 	    WID_DST(AF_INET), WID_DST(AF_INET),
586             (rp->destination == INADDR_ANY) ? "default" :
587                 (rp->set_bits & SET_MASK) ?
588                     (rp->mask == 0xffffffff ?
589                         routename(rp->destination) :
590                         netname(rp->destination, rp->mask)) :
591                     netname(rp->destination, 0L));
592     printf("%-*.*s %-6.6s  %s\n",
593 	    WID_GW(af), WID_GW(af),
594 	    rp->gateway ? routename(rp->gateway) : "*",
595             s_rtflags(rp), rp->ifname);
596 }
597