Home | History | Annotate | Line # | Download | only in route
show.c revision 1.18
      1 /*	$NetBSD: show.c,v 1.18 2001/10/06 18:32:45 bjh21 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.18 2001/10/06 18:32:45 bjh21 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_CLONED,	'c' },
     94 	{ RTF_PROTO1,	'1' },
     95 	{ RTF_PROTO2,	'2' },
     96 	{ 0 }
     97 };
     98 
     99 static void pr_rthdr __P((void));
    100 static void p_rtentry __P((struct rt_msghdr *));
    101 static void pr_family __P((int));
    102 static void p_sockaddr __P((struct sockaddr *, int, int ));
    103 static void p_flags __P((int));
    104 
    105 /*
    106  * Print routing tables.
    107  */
    108 void
    109 show(argc, argv)
    110 	int argc;
    111 	char **argv;
    112 {
    113 	size_t needed;
    114 	int mib[6];
    115 	char *buf, *next, *lim;
    116 	struct rt_msghdr *rtm;
    117 
    118 	mib[0] = CTL_NET;
    119 	mib[1] = PF_ROUTE;
    120 	mib[2] = 0;
    121 	mib[3] = 0;
    122 	mib[4] = NET_RT_DUMP;
    123 	mib[5] = 0;
    124 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)	{
    125 		perror("route-sysctl-estimate");
    126 		exit(1);
    127 	}
    128 	if ((buf = malloc(needed)) == 0)
    129 		err(1, NULL);
    130 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    131 		err(1, "sysctl of routing table");
    132 	lim  = buf + needed;
    133 
    134 	printf("Routing tables\n");
    135 
    136 	/* for (i = 0; i <= AF_MAX; i++) ??? */
    137 	{
    138 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
    139 			rtm = (struct rt_msghdr *)next;
    140 			p_rtentry(rtm);
    141 		}
    142 	}
    143 }
    144 
    145 
    146 /* column widths; each followed by one space */
    147 #define	WID_DST		16	/* width of destination column */
    148 #define	WID_GW		18	/* width of gateway column */
    149 
    150 /*
    151  * Print header for routing table columns.
    152  */
    153 static void
    154 pr_rthdr()
    155 {
    156 
    157 	printf("%-*.*s %-*.*s %-6.6s\n",
    158 		WID_DST, WID_DST, "Destination",
    159 		WID_GW, WID_GW, "Gateway",
    160 		"Flags");
    161 }
    162 
    163 
    164 /*
    165  * Print a routing table entry.
    166  */
    167 static void
    168 p_rtentry(rtm)
    169 	struct rt_msghdr *rtm;
    170 {
    171 	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    172 #ifdef notdef
    173 	static int masks_done, banner_printed;
    174 #endif
    175 	static int old_af;
    176 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
    177 
    178 #ifdef notdef
    179 	/* for the moment, netmasks are skipped over */
    180 	if (!banner_printed) {
    181 		printf("Netmasks:\n");
    182 		banner_printed = 1;
    183 	}
    184 	if (masks_done == 0) {
    185 		if (rtm->rtm_addrs != RTA_DST ) {
    186 			masks_done = 1;
    187 			af = sa->sa_family;
    188 		}
    189 	} else
    190 #endif
    191 		af = sa->sa_family;
    192 	if (old_af != af) {
    193 		old_af = af;
    194 		pr_family(af);
    195 		pr_rthdr();
    196 	}
    197 	if (rtm->rtm_addrs == RTA_DST)
    198 		p_sockaddr(sa, 0, WID_DST + 1 + WID_GW + 1);
    199 	else {
    200 		p_sockaddr(sa, rtm->rtm_flags, WID_DST);
    201 		sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
    202 		p_sockaddr(sa, 0, WID_GW);
    203 	}
    204 	p_flags(rtm->rtm_flags & interesting);
    205 	putchar('\n');
    206 }
    207 
    208 
    209 /*
    210  * Print address family header before a section of the routing table.
    211  */
    212 static void
    213 pr_family(af)
    214 	int af;
    215 {
    216 	char *afname;
    217 
    218 	switch (af) {
    219 	case AF_INET:
    220 		afname = "Internet";
    221 		break;
    222 #ifndef SMALL
    223 #ifdef INET6
    224 	case AF_INET6:
    225 		afname = "Internet6";
    226 		break;
    227 #endif /* INET6 */
    228 	case AF_NS:
    229 		afname = "XNS";
    230 		break;
    231 	case AF_ISO:
    232 		afname = "ISO";
    233 		break;
    234 	case AF_CCITT:
    235 		afname = "X.25";
    236 		break;
    237 #endif /* SMALL */
    238 	case AF_APPLETALK:
    239 		afname = "AppleTalk";
    240 		break;
    241 	default:
    242 		afname = NULL;
    243 		break;
    244 	}
    245 	if (afname)
    246 		printf("\n%s:\n", afname);
    247 	else
    248 		printf("\nProtocol Family %d:\n", af);
    249 }
    250 
    251 
    252 static void
    253 p_sockaddr(sa, flags, width)
    254 	struct sockaddr *sa;
    255 	int flags, width;
    256 {
    257 	char workbuf[128], *cplim;
    258 	char *cp = workbuf;
    259 
    260 	switch(sa->sa_family) {
    261 
    262 	case AF_LINK:
    263 		if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
    264 		    NULL, 0, NI_NUMERICHOST) != 0)
    265 			strncpy(workbuf, "invalid", sizeof(workbuf));
    266 		cp = workbuf;
    267 		break;
    268 
    269 	case AF_INET:
    270 	    {
    271 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    272 
    273 		cp = (sin->sin_addr.s_addr == 0) ? "default" :
    274 			((flags & RTF_HOST) ?
    275 			routename(sa) :	netname(sa));
    276 		break;
    277 	    }
    278 
    279 #ifndef SMALL
    280 #ifdef INET6
    281 	case AF_INET6:
    282 	    {
    283 		struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
    284 
    285 		cp = IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "default" :
    286 			((flags & RTF_HOST) ?
    287 			routename(sa) :	netname(sa));
    288 		/* make sure numeric address is not truncated */
    289 		if (strchr(cp, ':') != NULL && strlen(cp) > width)
    290 			width = strlen(cp);
    291 		break;
    292 	    }
    293 #endif /* INET6 */
    294 
    295 	case AF_NS:
    296 		cp = ns_print((struct sockaddr_ns *)sa);
    297 		break;
    298 #endif /* SMALL */
    299 
    300 	default:
    301 	    {
    302 		u_char *s = (u_char *)sa->sa_data, *slim;
    303 
    304 		slim = sa->sa_len + (u_char *) sa;
    305 		cplim = cp + sizeof(workbuf) - 6;
    306 		cp += snprintf(cp, cplim - cp, "(%d)", sa->sa_family);
    307 		while (s < slim && cp < cplim) {
    308 			cp += snprintf(cp, cplim - cp, " %02x", *s++);
    309 			if (s < slim)
    310 			    cp += snprintf(cp, cplim - cp, "%02x", *s++);
    311 		}
    312 		cp = workbuf;
    313 	    }
    314 	}
    315 	if (width < 0 )
    316 		printf("%s ", cp);
    317 	else {
    318 		if (nflag)
    319 			printf("%-*s ", width, cp);
    320 		else
    321 			printf("%-*.*s ", width, width, cp);
    322 	}
    323 }
    324 
    325 static void
    326 p_flags(f)
    327 	int f;
    328 {
    329 	char name[33], *flags;
    330 	const struct bits *p = bits;
    331 
    332 	for (flags = name; p->b_mask; p++)
    333 		if (p->b_mask & f)
    334 			*flags++ = p->b_val;
    335 	*flags = '\0';
    336 	printf("%-6.6s ", name);
    337 }
    338 
    339