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