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