Home | History | Annotate | Line # | Download | only in route
show.c revision 1.32
      1 /*	$NetBSD: show.c,v 1.32 2006/09/23 21:11:53 dyoung 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.32 2006/09/23 21:11:53 dyoung 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 
     52 #include <sys/sysctl.h>
     53 
     54 #include <netdb.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 #include <err.h>
     60 
     61 #include "keywords.h"
     62 #include "extern.h"
     63 
     64 #define ROUNDUP(a) \
     65 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     66 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     67 
     68 /*
     69  * Definitions for showing gateway flags.
     70  */
     71 struct bits {
     72 	short	b_mask;
     73 	char	b_val;
     74 };
     75 static const struct bits bits[] = {
     76 	{ RTF_UP,	'U' },
     77 	{ RTF_GATEWAY,	'G' },
     78 	{ RTF_HOST,	'H' },
     79 	{ RTF_REJECT,	'R' },
     80 	{ RTF_DYNAMIC,	'D' },
     81 	{ RTF_MODIFIED,	'M' },
     82 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
     83 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
     84 	{ RTF_CLONING,	'C' },
     85 	{ RTF_XRESOLVE,	'X' },
     86 	{ RTF_LLINFO,	'L' },
     87 	{ RTF_STATIC,	'S' },
     88 	{ RTF_BLACKHOLE, 'B' },
     89 	{ RTF_CLONED,	'c' },
     90 	{ RTF_PROTO1,	'1' },
     91 	{ RTF_PROTO2,	'2' },
     92 	{ 0 }
     93 };
     94 
     95 static void pr_rthdr(int);
     96 static void p_rtentry(struct rt_msghdr *);
     97 static void pr_family(int);
     98 static void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int );
     99 static void p_flags(int);
    100 
    101 /*
    102  * Print routing tables.
    103  */
    104 void
    105 show(int argc, char **argv)
    106 {
    107 	size_t needed;
    108 	int af, mib[6];
    109 	char *buf, *next, *lim;
    110 	struct rt_msghdr *rtm;
    111 	struct sockaddr *sa;
    112 
    113 	af = AF_UNSPEC;
    114 	if (argc > 1) {
    115 		argv++;
    116 		if (argc == 2 && **argv == '-')
    117 		    switch (keyword(*argv + 1)) {
    118 			case K_INET:
    119 				af = AF_INET;
    120 				break;
    121 #ifdef INET6
    122 			case K_INET6:
    123 				af = AF_INET6;
    124 				break;
    125 #endif
    126 #ifndef SMALL
    127 			case K_ATALK:
    128 				af = AF_APPLETALK;
    129 				break;
    130 			case K_ISO:
    131 			case K_OSI:
    132 				af = AF_ISO;
    133 				break;
    134 #endif /* SMALL */
    135 			default:
    136 				goto bad;
    137 		} else
    138 bad:			usage(*argv);
    139 	}
    140 	mib[0] = CTL_NET;
    141 	mib[1] = PF_ROUTE;
    142 	mib[2] = 0;
    143 	mib[3] = 0;
    144 	mib[4] = NET_RT_DUMP;
    145 	mib[5] = 0;
    146 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    147 		err(1, "route-sysctl-estimate");
    148 	buf = lim = NULL;
    149 	if (needed) {
    150 		if ((buf = malloc(needed)) == 0)
    151 			err(1, "malloc");
    152 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    153 			err(1, "sysctl of routing table");
    154 		lim  = buf + needed;
    155 	}
    156 
    157 	printf("Routing table%s\n", (af == AF_UNSPEC)? "s" : "");
    158 
    159 	if (needed) {
    160 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
    161 			rtm = (struct rt_msghdr *)next;
    162 			sa = (struct sockaddr *)(rtm + 1);
    163 			if (af == AF_UNSPEC || af == sa->sa_family)
    164 				p_rtentry(rtm);
    165 		}
    166 		free(buf);
    167 	}
    168 }
    169 
    170 
    171 /* column widths; each followed by one space */
    172 #ifndef INET6
    173 #define	WID_DST(af)	18	/* width of destination column */
    174 #define	WID_GW(af)	18	/* width of gateway column */
    175 #else
    176 /* width of destination/gateway column */
    177 #if 1
    178 /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
    179 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
    180 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
    181 #else
    182 /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
    183 #define	WID_DST(af)	((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
    184 #define	WID_GW(af)	((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
    185 #endif
    186 #endif /* INET6 */
    187 
    188 /*
    189  * Print header for routing table columns.
    190  */
    191 static void
    192 pr_rthdr(int af)
    193 {
    194 
    195 	printf("%-*.*s %-*.*s %-6.6s\n",
    196 		WID_DST(af), WID_DST(af), "Destination",
    197 		WID_GW(af), WID_GW(af), "Gateway",
    198 		"Flags");
    199 }
    200 
    201 
    202 /*
    203  * Print a routing table entry.
    204  */
    205 static void
    206 p_rtentry(struct rt_msghdr *rtm)
    207 {
    208 	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    209 #ifdef notdef
    210 	static int masks_done, banner_printed;
    211 #endif
    212 	static int old_af;
    213 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_REJECT;
    214 
    215 #ifdef notdef
    216 	/* for the moment, netmasks are skipped over */
    217 	if (!banner_printed) {
    218 		printf("Netmasks:\n");
    219 		banner_printed = 1;
    220 	}
    221 	if (masks_done == 0) {
    222 		if (rtm->rtm_addrs != RTA_DST ) {
    223 			masks_done = 1;
    224 			af = sa->sa_family;
    225 		}
    226 	} else
    227 #endif
    228 		af = sa->sa_family;
    229 	if (old_af != af) {
    230 		old_af = af;
    231 		pr_family(af);
    232 		pr_rthdr(af);
    233 	}
    234 	if (rtm->rtm_addrs == RTA_DST)
    235 		p_sockaddr(sa, NULL, 0, WID_DST(af) + 1 + WID_GW(af) + 1);
    236 	else {
    237 		struct sockaddr *nm;
    238 
    239 		if ((rtm->rtm_addrs & RTA_NETMASK) == 0)
    240 			nm = NULL;
    241 		else {
    242 			/* skip to gateway */
    243 			nm = (struct sockaddr *)
    244 			    (ROUNDUP(sa->sa_len) + (char *)sa);
    245 			/* skip over gateway to netmask */
    246 			nm = (struct sockaddr *)
    247 			    (ROUNDUP(nm->sa_len) + (char *)nm);
    248 		}
    249 
    250 		p_sockaddr(sa, nm, rtm->rtm_flags, WID_DST(af));
    251 		sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
    252 		p_sockaddr(sa, NULL, 0, WID_GW(af));
    253 	}
    254 	p_flags(rtm->rtm_flags & interesting);
    255 	putchar('\n');
    256 }
    257 
    258 
    259 /*
    260  * Print address family header before a section of the routing table.
    261  */
    262 static void
    263 pr_family(int af)
    264 {
    265 	const char *afname;
    266 
    267 	switch (af) {
    268 	case AF_INET:
    269 		afname = "Internet";
    270 		break;
    271 #ifdef INET6
    272 	case AF_INET6:
    273 		afname = "Internet6";
    274 		break;
    275 #endif /* INET6 */
    276 #ifndef SMALL
    277 	case AF_ISO:
    278 		afname = "ISO";
    279 		break;
    280 #endif /* SMALL */
    281 	case AF_APPLETALK:
    282 		afname = "AppleTalk";
    283 		break;
    284 	default:
    285 		afname = NULL;
    286 		break;
    287 	}
    288 	if (afname)
    289 		printf("\n%s:\n", afname);
    290 	else
    291 		printf("\nProtocol Family %d:\n", af);
    292 }
    293 
    294 
    295 static void
    296 p_sockaddr(struct sockaddr *sa, struct sockaddr *nm, int flags, int width)
    297 {
    298 	char workbuf[128];
    299 	const char *cp;
    300 
    301 	switch(sa->sa_family) {
    302 
    303 	case AF_LINK:
    304 		if (getnameinfo(sa, sa->sa_len, workbuf, sizeof(workbuf),
    305 		    NULL, 0, NI_NUMERICHOST) != 0)
    306 			strlcpy(workbuf, "invalid", sizeof(workbuf));
    307 		cp = workbuf;
    308 		break;
    309 
    310 	case AF_INET:
    311 		cp = routename(sa, nm, flags);
    312 		break;
    313 
    314 #ifdef INET6
    315 	case AF_INET6:
    316 		cp = routename(sa, nm, flags);
    317 		/* make sure numeric address is not truncated */
    318 		if (strchr(cp, ':') != NULL && strlen(cp) > width)
    319 			width = strlen(cp);
    320 		break;
    321 #endif /* INET6 */
    322 
    323 #ifndef SMALL
    324 #endif /* SMALL */
    325 
    326 	default:
    327 	    {
    328 		u_char *s = (u_char *)sa->sa_data, *slim;
    329 		char *wp = workbuf, *wplim;
    330 
    331 		slim = sa->sa_len + (u_char *)sa;
    332 		wplim = wp + sizeof(workbuf) - 6;
    333 		wp += snprintf(wp, wplim - wp, "(%d)", sa->sa_family);
    334 		while (s < slim && wp < wplim) {
    335 			wp += snprintf(wp, wplim - wp, " %02x", *s++);
    336 			if (s < slim)
    337 			    wp += snprintf(wp, wplim - wp, "%02x", *s++);
    338 		}
    339 		cp = workbuf;
    340 	    }
    341 	}
    342 	if (width < 0 )
    343 		printf("%s ", cp);
    344 	else {
    345 		if (nflag)
    346 			printf("%-*s ", width, cp);
    347 		else
    348 			printf("%-*.*s ", width, width, cp);
    349 	}
    350 }
    351 
    352 static void
    353 p_flags(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 		else if (Sflag)
    362 			*flags++ = ' ';
    363 	*flags = '\0';
    364 	printf("%-6.6s ", name);
    365 }
    366 
    367