Home | History | Annotate | Line # | Download | only in route
show.c revision 1.28
      1 /*	$NetBSD: show.c,v 1.28 2005/08/31 02:58:30 ginsbach Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from: @(#)route.c	8.3 (Berkeley) 3/9/94";
     36 #else
     37 __RCSID("$NetBSD: show.c,v 1.28 2005/08/31 02:58:30 ginsbach Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/protosw.h>
     43 #include <sys/socket.h>
     44 #include <sys/mbuf.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_dl.h>
     48 #include <net/if_types.h>
     49 #include <net/route.h>
     50 #include <netinet/in.h>
     51 #include <netns/ns.h>
     52 
     53 #include <sys/sysctl.h>
     54 
     55 #include <netdb.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 #include <err.h>
     61 
     62 #include "keywords.h"
     63 #include "extern.h"
     64 
     65 #define ROUNDUP(a) \
     66 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     67 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     68 
     69 /*
     70  * Definitions for showing gateway flags.
     71  */
     72 struct bits {
     73 	short	b_mask;
     74 	char	b_val;
     75 };
     76 static const struct bits bits[] = {
     77 	{ RTF_UP,	'U' },
     78 	{ RTF_GATEWAY,	'G' },
     79 	{ RTF_HOST,	'H' },
     80 	{ RTF_REJECT,	'R' },
     81 	{ RTF_DYNAMIC,	'D' },
     82 	{ RTF_MODIFIED,	'M' },
     83 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
     84 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
     85 	{ RTF_CLONING,	'C' },
     86 	{ RTF_XRESOLVE,	'X' },
     87 	{ RTF_LLINFO,	'L' },
     88 	{ RTF_STATIC,	'S' },
     89 	{ RTF_BLACKHOLE, 'B' },
     90 	{ RTF_CLONED,	'c' },
     91 	{ RTF_PROTO1,	'1' },
     92 	{ RTF_PROTO2,	'2' },
     93 	{ 0 }
     94 };
     95 
     96 static void pr_rthdr(int);
     97 static void p_rtentry(struct rt_msghdr *);
     98 static void pr_family(int);
     99 static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int );
    100 static void p_flags(int);
    101 
    102 /*
    103  * Print routing tables.
    104  */
    105 void
    106 show(int argc, char **argv)
    107 {
    108 	size_t needed;
    109 	int af, mib[6];
    110 	char *buf, *next, *lim;
    111 	struct rt_msghdr *rtm;
    112 	struct sockaddr *sa;
    113 
    114 	af = AF_UNSPEC;
    115 	if (argc > 1) {
    116 		argv++;
    117 		if (argc == 2 && **argv == '-')
    118 		    switch (keyword(*argv + 1)) {
    119 			case K_INET:
    120 				af = AF_INET;
    121 				break;
    122 #ifdef INET6
    123 			case K_INET6:
    124 				af = AF_INET6;
    125 				break;
    126 #endif
    127 #ifndef SMALL
    128 			case K_ATALK:
    129 				af = AF_APPLETALK;
    130 				break;
    131 			case K_XNS:
    132 				af = AF_NS;
    133 				break;
    134 #endif /* SMALL */
    135 #if 0
    136 			/* XXX Links are never destinations */
    137 			case K_LINK:
    138 				af = AF_LINK;
    139 				break;
    140 #endif
    141 #ifndef SMALL
    142 			case K_ISO:
    143 			case K_OSI:
    144 				af = AF_ISO;
    145 				break;
    146 			case K_X25:
    147 				af = AF_CCITT;
    148 #endif /* SMALL */
    149 			default:
    150 				goto bad;
    151 		} else
    152 bad:			usage(*argv);
    153 	}
    154 	mib[0] = CTL_NET;
    155 	mib[1] = PF_ROUTE;
    156 	mib[2] = 0;
    157 	mib[3] = 0;
    158 	mib[4] = NET_RT_DUMP;
    159 	mib[5] = 0;
    160 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    161 		err(1, "route-sysctl-estimate");
    162 	buf = lim = NULL;
    163 	if (needed) {
    164 		if ((buf = malloc(needed)) == 0)
    165 			err(1, "malloc");
    166 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    167 			err(1, "sysctl of routing table");
    168 		lim  = buf + needed;
    169 	}
    170 
    171 	printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : "");
    172 
    173 	if (needed) {
    174 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
    175 			rtm = (struct rt_msghdr *)next;
    176 			sa = (struct sockaddr *)(rtm + 1);
    177 			if (af == AF_UNSPEC || af == sa->sa_family)
    178 				p_rtentry(rtm);
    179 		}
    180 		free(buf);
    181 	}
    182 }
    183 
    184 
    185 /* column widths; each followed by one space */
    186 #ifndef INET6
    187 #define	WID_DST(af)	18	/* width of destination column */
    188 #define	WID_GW(af)	18	/* width of gateway column */
    189 #else
    190 /* width of destination/gateway column */
    191 #if 1
    192 /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
    193 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
    194 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
    195 #else
    196 /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
    197 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
    198 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
    199 #endif
    200 #endif /* INET6 */
    201 
    202 /*
    203  * Print header for routing table columns.
    204  */
    205 static void
    206 pr_rthdr(int af)
    207 {
    208 
    209 	printf("%-*.*s %-*.*s %-6.6s\n",
    210 		WID_DST(af), WID_DST(af), "Destination",
    211 		WID_GW(af), WID_GW(af), "Gateway",
    212 		"Flags");
    213 }
    214 
    215 
    216 /*
    217  * Print a routing table entry.
    218  */
    219 static void
    220 p_rtentry(struct rt_msghdr *rtm)
    221 {
    222 	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    223 #ifdef notdef
    224 	static int masks_done, banner_printed;
    225 #endif
    226 	static int old_af;
    227 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_REJECT;
    228 
    229 #ifdef notdef
    230 	/* for the moment, netmasks are skipped over */
    231 	if (!banner_printed) {
    232 		printf("Netmasks:\n");
    233 		banner_printed = 1;
    234 	}
    235 	if (masks_done == 0) {
    236 		if (rtm->rtm_addrs != RTA_DST ) {
    237 			masks_done = 1;
    238 			af = sa->sa_family;
    239 		}
    240 	} else
    241 #endif
    242 		af = sa->sa_family;
    243 	if (old_af != af) {
    244 		old_af = af;
    245 		pr_family(af);
    246 		pr_rthdr(af);
    247 	}
    248 	if (rtm->rtm_addrs == RTA_DST)
    249 		p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1);
    250 	else {
    251 		struct sockaddr *nm;
    252 
    253 		if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
    254 			nm = NULL;
    255 		else {
    256 			/* skip to gateway */
    257 			nm = (struct sockaddr *)
    258 			    (ROUNDUP(sa->sa_len) + (char *)sa);
    259 			/* skip over gateway to netmask */
    260 			nm = (struct sockaddr *)
    261 			    (ROUNDUP(nm->sa_len) + (char *)nm);
    262 		}
    263 
    264 		p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af));
    265 		sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
    266 		p_sockaddr(sa, NULL, 0, WID_GW(af));
    267 	}
    268 	p_flags(rtm->rtm_flags & interesting);
    269 	putchar('\n');
    270 }
    271 
    272 
    273 /*
    274  * Print address family header before a section of the routing table.
    275  */
    276 static void
    277 pr_family(int af)
    278 {
    279 	const char *afname;
    280 
    281 	switch (af) {
    282 	case AF_INET:
    283 		afname = "Internet";
    284 		break;
    285 #ifdef INET6
    286 	case AF_INET6:
    287 		afname = "Internet6";
    288 		break;
    289 #endif /* INET6 */
    290 #ifndef SMALL
    291 	case AF_NS:
    292 		afname = "XNS";
    293 		break;
    294 	case AF_ISO:
    295 		afname = "ISO";
    296 		break;
    297 	case AF_CCITT:
    298 		afname = "X.25";
    299 		break;
    300 #endif /* SMALL */
    301 	case AF_APPLETALK:
    302 		afname = "AppleTalk";
    303 		break;
    304 	default:
    305 		afname = NULL;
    306 		break;
    307 	}
    308 	if (afname)
    309 		printf("\n%s:\n", afname);
    310 	else
    311 		printf("\nProtocol Family %d:\n", af);
    312 }
    313 
    314 
    315 static void
    316 p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width)
    317 {
    318 	char workbuf[128];
    319 	const char *cp;
    320 
    321 	switch(sa->sa_family) {
    322 
    323 	case AF_LINK:
    324 		if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
    325 		    NULL, 0, NI_NUMERICHOST) != 0)
    326 			strlcpy(workbuf, "invalid", sizeof(workbuf));
    327 		cp = workbuf;
    328 		break;
    329 
    330 	case AF_INET:
    331 		cp = routename(sa, nm, flags);
    332 		break;
    333 
    334 #ifdef INET6
    335 	case AF_INET6:
    336 		cp = routename(sa, nm, flags);
    337 		/* make sure numeric address is not truncated */
    338 		if (strchr(cp, ':') != NULL && strlen(cp) > width)
    339 			width = strlen(cp);
    340 		break;
    341 #endif /* INET6 */
    342 
    343 #ifndef SMALL
    344 	case AF_NS:
    345 		cp = ns_print((struct sockaddr_ns *)sa);
    346 		break;
    347 #endif /* SMALL */
    348 
    349 	default:
    350 	    {
    351 		u_char *s = (u_char *)sa->sa_data, *slim;
    352 		char *wp = workbuf, *wplim;
    353 
    354 		slim = sa->sa_len + (u_char *)sa;
    355 		wplim = wp + sizeof(workbuf) - 6;
    356 		wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family);
    357 		while (s < slim && wp < wplim) {
    358 			wp += snprintf(wp, wplim - wp, " %02x", *s++);
    359 			if (s < slim)
    360 			    wp += snprintf(wp, wplim - wp, "%02x", *s++);
    361 		}
    362 		cp = workbuf;
    363 	    }
    364 	}
    365 	if (width < 0 )
    366 		printf("%s ", cp);
    367 	else {
    368 		if (nflag)
    369 			printf("%-*s ", width, cp);
    370 		else
    371 			printf("%-*.*s ", width, width, cp);
    372 	}
    373 }
    374 
    375 static void
    376 p_flags(int f)
    377 {
    378 	char name[33], *flags;
    379 	const struct bits *p = bits;
    380 
    381 	for (flags = name; p->b_mask; p++)
    382 		if (p->b_mask & f)
    383 			*flags++ = p->b_val;
    384 	*flags = '\0';
    385 	printf("%-6.6s ", name);
    386 }
    387 
    388