Home | History | Annotate | Line # | Download | only in route
route.c revision 1.102
      1 /*	$NetBSD: route.c,v 1.102 2006/09/23 21:51:05 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1989, 1991, 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 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1991, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)route.c	8.6 (Berkeley) 4/28/95";
     41 #else
     42 __RCSID("$NetBSD: route.c,v 1.102 2006/09/23 21:51:05 dyoung Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/file.h>
     48 #include <sys/socket.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/sysctl.h>
     52 
     53 #include <net/if.h>
     54 #include <net/route.h>
     55 #include <net/if_dl.h>
     56 #include <net80211/ieee80211_netbsd.h>
     57 #include <netinet/in.h>
     58 #include <netatalk/at.h>
     59 #include <netiso/iso.h>
     60 #include <arpa/inet.h>
     61 #include <netdb.h>
     62 
     63 #include <errno.h>
     64 #include <unistd.h>
     65 #include <stdio.h>
     66 #include <ctype.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <time.h>
     70 #include <paths.h>
     71 #include <err.h>
     72 
     73 #include "keywords.h"
     74 #include "extern.h"
     75 
     76 typedef union sockunion *sup;
     77 
     78 static char *any_ntoa(const struct sockaddr *);
     79 static const char *route_strerror(int);
     80 static void set_metric(char *, int);
     81 static int newroute(int, char **);
     82 static void inet_makenetandmask(u_int32_t, struct sockaddr_in *);
     83 #ifdef INET6
     84 static int inet6_makenetandmask(struct sockaddr_in6 *);
     85 #endif
     86 static int getaddr(int, char *, struct hostent **);
     87 static int flushroutes(int, char *[], int);
     88 static int prefixlen(const char *);
     89 #ifndef SMALL
     90 static void interfaces(void);
     91 static void monitor(void);
     92 static int print_getmsg(struct rt_msghdr *, int);
     93 static const char *linkstate(struct if_msghdr *);
     94 #endif /* SMALL */
     95 static int rtmsg(int, int );
     96 static void mask_addr(void);
     97 static void print_rtmsg(struct rt_msghdr *, int);
     98 static void pmsg_common(struct rt_msghdr *);
     99 static void pmsg_addrs(char *, int);
    100 static void bprintf(FILE *, int, const char *);
    101 static void sodump(sup, const char *);
    102 static void sockaddr(char *, struct sockaddr *);
    103 
    104 union	sockunion {
    105 	struct	sockaddr sa;
    106 	struct	sockaddr_in sin;
    107 #ifdef INET6
    108 	struct	sockaddr_in6 sin6;
    109 #endif
    110 	struct	sockaddr_at sat;
    111 	struct	sockaddr_dl sdl;
    112 #ifndef SMALL
    113 	struct	sockaddr_iso siso;
    114 #endif /* SMALL */
    115 } so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp;
    116 
    117 int	pid, rtm_addrs;
    118 int	sock;
    119 int	forcehost, forcenet, doflush, nflag, af, qflag, tflag, Sflag;
    120 int	iflag, verbose, aflen = sizeof(struct sockaddr_in);
    121 int	locking, lockrest, debugonly, shortoutput;
    122 struct	rt_metrics rt_metrics;
    123 u_int32_t  rtm_inits;
    124 short ns_nullh[] = {0,0,0};
    125 short ns_bh[] = {-1,-1,-1};
    126 
    127 
    128 void
    129 usage(const char *cp)
    130 {
    131 
    132 	if (cp)
    133 		warnx("botched keyword: %s", cp);
    134 	(void)fprintf(stderr,
    135 	    "Usage: %s [ -fnqSsv ] cmd [[ -<qualifers> ] args ]\n",
    136 	    getprogname());
    137 	exit(1);
    138 	/* NOTREACHED */
    139 }
    140 
    141 #define	PRIETHER	"02x:%02x:%02x:%02x:%02x:%02x"
    142 #define	PRIETHER_ARGS(__enaddr)	(__enaddr)[0], (__enaddr)[1], (__enaddr)[2], \
    143 				(__enaddr)[3], (__enaddr)[4], (__enaddr)[5]
    144 #define ROUNDUP(a) \
    145 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    146 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    147 
    148 int
    149 main(int argc, char **argv)
    150 {
    151 	int ch;
    152 
    153 	if (argc < 2)
    154 		usage(NULL);
    155 
    156 	while ((ch = getopt(argc, argv, "dfnqSstv")) != -1)
    157 		switch (ch) {
    158 		case 'd':
    159 			debugonly = 1;
    160 			break;
    161 		case 'f':
    162 			doflush = 1;
    163 			break;
    164 		case 'n':
    165 			nflag = 1;
    166 			break;
    167 		case 'q':
    168 			qflag = 1;
    169 			break;
    170 		case 'S':
    171 			Sflag = 1;
    172 			break;
    173 		case 's':
    174 			shortoutput = 1;
    175 			break;
    176 		case 't':
    177 			tflag = 1;
    178 			break;
    179 		case 'v':
    180 			verbose = 1;
    181 			break;
    182 		case '?':
    183 		default:
    184 			usage(NULL);
    185 			/*NOTREACHED*/
    186 		}
    187 	argc -= optind;
    188 	argv += optind;
    189 
    190 	pid = getpid();
    191 	if (tflag)
    192 		sock = open("/dev/null", O_WRONLY, 0);
    193 	else
    194 		sock = socket(PF_ROUTE, SOCK_RAW, 0);
    195 	if (sock < 0)
    196 		err(EXIT_FAILURE, "socket");
    197 
    198 	if (*argv == NULL) {
    199 		if (doflush)
    200 			ch = K_FLUSH;
    201 		else
    202 			goto no_cmd;
    203 	} else
    204 		ch = keyword(*argv);
    205 
    206 	switch (ch) {
    207 #ifndef SMALL
    208 	case K_GET:
    209 #endif /* SMALL */
    210 	case K_CHANGE:
    211 	case K_ADD:
    212 	case K_DELETE:
    213 		if (doflush)
    214 			(void)flushroutes(1, argv, 0);
    215 		return newroute(argc, argv);
    216 
    217 	case K_SHOW:
    218 		show(argc, argv);
    219 		return 0;
    220 
    221 #ifndef SMALL
    222 	case K_MONITOR:
    223 		monitor();
    224 		return 0;
    225 
    226 #endif /* SMALL */
    227 	case K_FLUSH:
    228 		return flushroutes(argc, argv, 0);
    229 
    230 	case K_FLUSHALL:
    231 		return flushroutes(argc, argv, 1);
    232 	no_cmd:
    233 	default:
    234 		usage(*argv);
    235 		/*NOTREACHED*/
    236 	}
    237 }
    238 
    239 /*
    240  * Purge all entries in the routing tables not
    241  * associated with network interfaces.
    242  */
    243 static int
    244 flushroutes(int argc, char *argv[], int doall)
    245 {
    246 	size_t needed;
    247 	int mib[6], rlen, seqno;
    248 	char *buf, *next, *lim;
    249 	struct rt_msghdr *rtm;
    250 
    251 	af = AF_UNSPEC;
    252 	shutdown(sock, SHUT_RD); /* Don't want to read back our messages */
    253 	if (argc > 1) {
    254 		argv++;
    255 		if (argc == 2 && **argv == '-') {
    256 			switch (keyword(*argv + 1)) {
    257 			case K_INET:
    258 				af = AF_INET;
    259 				break;
    260 #ifdef INET6
    261 			case K_INET6:
    262 				af = AF_INET6;
    263 				break;
    264 #endif
    265 #ifndef SMALL
    266 			case K_ATALK:
    267 				af = AF_APPLETALK;
    268 				break;
    269 #endif /* SMALL */
    270 			case K_LINK:
    271 				af = AF_LINK;
    272 				break;
    273 #ifndef SMALL
    274 			case K_ISO:
    275 			case K_OSI:
    276 				af = AF_ISO;
    277 				break;
    278 #endif /* SMALL */
    279 			default:
    280 				goto bad;
    281 			}
    282 		} else
    283 bad:			usage(*argv);
    284 	}
    285 	mib[0] = CTL_NET;
    286 	mib[1] = PF_ROUTE;
    287 	mib[2] = 0;		/* protocol */
    288 	mib[3] = 0;		/* wildcard address family */
    289 	mib[4] = NET_RT_DUMP;
    290 	mib[5] = 0;		/* no flags */
    291 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    292 		err(EXIT_FAILURE, "route-sysctl-estimate");
    293 	buf = lim = NULL;
    294 	if (needed) {
    295 		if ((buf = malloc(needed)) == NULL)
    296 			err(EXIT_FAILURE, "malloc");
    297 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    298 			err(EXIT_FAILURE, "actual retrieval of routing table");
    299 		lim = buf + needed;
    300 	}
    301 	if (verbose) {
    302 		(void)printf("Examining routing table from sysctl\n");
    303 		if (af != AF_UNSPEC)
    304 			printf("(address family %s)\n", (*argv + 1));
    305 	}
    306 	if (needed == 0)
    307 		return 0;
    308 	seqno = 0;		/* ??? */
    309 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    310 		rtm = (struct rt_msghdr *)next;
    311 		if (verbose)
    312 			print_rtmsg(rtm, rtm->rtm_msglen);
    313 		if (!(rtm->rtm_flags & (RTF_GATEWAY | RTF_STATIC |
    314 					RTF_LLINFO)) && !doall)
    315 			continue;
    316 		if (af != AF_UNSPEC) {
    317 			struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    318 
    319 			if (sa->sa_family != af)
    320 				continue;
    321 		}
    322 		if (debugonly)
    323 			continue;
    324 		rtm->rtm_type = RTM_DELETE;
    325 		rtm->rtm_seq = seqno;
    326 		if ((rlen = write(sock, next, rtm->rtm_msglen)) < 0) {
    327 			warnx("writing to routing socket: %s",
    328 			    route_strerror(errno));
    329 			return 1;
    330 		}
    331 		if (rlen < (int)rtm->rtm_msglen) {
    332 			warnx("write to routing socket, got %d for rlen", rlen);
    333 			return 1;
    334 		}
    335 		seqno++;
    336 		if (qflag)
    337 			continue;
    338 		if (verbose)
    339 			print_rtmsg(rtm, rlen);
    340 		else {
    341 			struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    342 			(void)printf("%-20.20s ",
    343 			    routename(sa, NULL, rtm->rtm_flags));
    344 			sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
    345 			(void)printf("%-20.20s ",
    346 			    routename(sa, NULL, RTF_HOST));
    347 			(void)printf("done\n");
    348 		}
    349 	}
    350 	free(buf);
    351 	return 0;
    352 }
    353 
    354 
    355 static char hexlist[] = "0123456789abcdef";
    356 
    357 static char *
    358 any_ntoa(const struct sockaddr *sa)
    359 {
    360 	static char obuf[3 * 256];
    361 	const char *in;
    362 	char *out;
    363 	int len;
    364 
    365 #if __GNUC__ > 2
    366 	len = sa->sa_len - offsetof(struct sockaddr, sa_data);
    367 #else
    368 	len = sa->sa_len - ((struct sockaddr*)&sa->sa_data - sa);
    369 #endif
    370 	in  = sa->sa_data;
    371 	out = obuf;
    372 
    373 	do {
    374 		*out++ = hexlist[(*in >> 4) & 15];
    375 		*out++ = hexlist[(*in++)    & 15];
    376 		*out++ = '.';
    377 	} while (--len > 0);
    378 	out[-1] = '\0';
    379 	return obuf;
    380 }
    381 
    382 int
    383 netmask_length(struct sockaddr *nm, int family)
    384 {
    385 	static int
    386 	    /* number of bits in a nibble */
    387 	    _t[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 },
    388 	    /* good nibbles are 1111, 1110, 1100, 1000, 0000 */
    389 	    _g[] = { 1,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1 };
    390 	int mask, good, zeroes, maskbytes, bit, i;
    391 	unsigned char *maskdata;
    392 
    393 	if (nm == NULL)
    394 		return 0;
    395 
    396 	mask = 0;
    397 	good = 1;
    398 	zeroes = 0;
    399 
    400 	switch (family) {
    401 	case AF_INET: {
    402 		struct sockaddr_in *nsin = (struct sockaddr_in *)nm;
    403 		maskdata = (unsigned char *)&nsin->sin_addr;
    404 		maskbytes = nsin->sin_len -
    405 		    ((caddr_t)&nsin->sin_addr - (caddr_t)nsin);
    406 		break;
    407 	}
    408 	case AF_INET6: {
    409 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nm;
    410 		maskdata = (unsigned char *)&sin6->sin6_addr;
    411 		maskbytes = sin6->sin6_len -
    412 		    ((caddr_t)&sin6->sin6_addr - (caddr_t)sin6);
    413 		break;
    414 	}
    415 	default:
    416 		return 0;
    417 	}
    418 
    419 	/*
    420 	 * Count the bits in the nibbles of the mask, and marking the
    421 	 * netmask as not good (or at best, non-standard and very
    422 	 * discouraged, in the case of AF_INET) if we find either of
    423 	 * a nibble with non-contiguous bits, or a non-zero nibble
    424 	 * after we've found a zero nibble.
    425 	 */
    426 	for (i = 0; i < maskbytes; i++) {
    427 		/* high nibble */
    428 		mask += bit = _t[maskdata[i] >> 4];
    429 		good &= _g[maskdata[i] >> 4];
    430 		if (zeroes && bit)
    431 			good = 0;
    432 		if (bit == 0)
    433 			zeroes = 1;
    434 		/* low nibble */
    435 		mask += bit = _t[maskdata[i] & 0xf];
    436 		good &= _g[maskdata[i] & 0xf];
    437 		if (zeroes && bit)
    438 			good = 0;
    439 		if (bit == 0)
    440 			zeroes = 1;
    441 	}
    442 
    443 	/*
    444 	 * Always return the number of bits found, but as a negative
    445 	 * if the mask wasn't one we like.
    446 	 */
    447 	return good ? mask : -mask;
    448 }
    449 
    450 char *
    451 netmask_string(struct sockaddr *mask, int len, int family)
    452 {
    453 	static char smask[INET6_ADDRSTRLEN];
    454 	struct sockaddr_in nsin;
    455 	struct sockaddr_in6 nsin6;
    456 
    457 	if (len >= 0)
    458 		snprintf(smask, sizeof(smask), "%d", len);
    459 	else {
    460 		switch (family) {
    461 		case AF_INET:
    462 			memset(&nsin, 0, sizeof(nsin));
    463 			memcpy(&nsin, mask, mask->sa_len);
    464 			snprintf(smask, sizeof(smask), "%s",
    465 			    inet_ntoa(nsin.sin_addr));
    466 			break;
    467 		case AF_INET6:
    468 			memset(&nsin6, 0, sizeof(nsin6));
    469 			memcpy(&nsin6, mask, mask->sa_len);
    470 			inet_ntop(family, &nsin6.sin6_addr, smask,
    471 			    sizeof(smask));
    472 			break;
    473 		default:
    474 			snprintf(smask, sizeof(smask), "%s", any_ntoa(mask));
    475 		}
    476 	}
    477 
    478 	return smask;
    479 }
    480 
    481 const char *
    482 routename(struct sockaddr *sa, struct sockaddr *nm, int flags)
    483 {
    484 	const char *cp;
    485 	static char line[50];
    486 	struct hostent *hp;
    487 	static char domain[MAXHOSTNAMELEN + 1];
    488 	static int first = 1;
    489 	struct in_addr in;
    490 	int nml;
    491 
    492 	if ((flags & RTF_HOST) == 0)
    493 		return netname(sa, nm);
    494 
    495 	if (first) {
    496 		first = 0;
    497 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
    498 		    (cp = strchr(domain, '.')))
    499 			(void)strlcpy(domain, cp + 1, sizeof(domain));
    500 		else
    501 			domain[0] = 0;
    502 	}
    503 
    504 	if (sa->sa_len == 0)
    505 		strlcpy(line, "default", sizeof(line));
    506 	else switch (sa->sa_family) {
    507 
    508 	case AF_INET:
    509 		in = ((struct sockaddr_in *)sa)->sin_addr;
    510 		nml = netmask_length(nm, AF_INET);
    511 
    512 		cp = 0;
    513 		if (in.s_addr == INADDR_ANY || sa->sa_len < 4) {
    514 			if (nml == 0)
    515 				cp = "default";
    516 			else {
    517 				static char notdefault[sizeof(NOTDEFSTRING)];
    518 
    519 				snprintf(notdefault, sizeof(notdefault),
    520 				    "0.0.0.0/%s",
    521 				    netmask_string(nm, nml, AF_INET));
    522 				cp = notdefault;
    523 			}
    524 		}
    525 		if (cp == 0 && !nflag) {
    526 			hp = gethostbyaddr((char *)&in, sizeof(struct in_addr),
    527 				AF_INET);
    528 			if (hp) {
    529 				char *ccp;
    530 				if ((ccp = strchr(hp->h_name, '.')) &&
    531 				    !strcmp(ccp + 1, domain))
    532 					*ccp = '\0';
    533 				cp = hp->h_name;
    534 			}
    535 		}
    536 		if (cp)
    537 			(void)strlcpy(line, cp, sizeof(line));
    538 		else
    539 			(void)strlcpy(line, inet_ntoa(in), sizeof(line));
    540 		break;
    541 
    542 	case AF_LINK:
    543 		return (link_ntoa((struct sockaddr_dl *)sa));
    544 
    545 #ifdef INET6
    546 	case AF_INET6:
    547 	    {
    548 		struct sockaddr_in6 sin6;
    549 		int niflags;
    550 		char nihost[NI_MAXHOST];
    551 
    552 		niflags = 0;
    553 		if (nflag)
    554 			niflags |= NI_NUMERICHOST;
    555 		memset(&sin6, 0, sizeof(sin6));
    556 		memcpy(&sin6, sa, sa->sa_len);
    557 		sin6.sin6_len = sizeof(struct sockaddr_in6);
    558 		sin6.sin6_family = AF_INET6;
    559 #ifdef __KAME__
    560 		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
    561 		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
    562 		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
    563 		    sin6.sin6_scope_id == 0) {
    564 			sin6.sin6_scope_id =
    565 			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
    566 			sin6.sin6_addr.s6_addr[2] = 0;
    567 			sin6.sin6_addr.s6_addr[3] = 0;
    568 		}
    569 #endif
    570 		nml = netmask_length(nm, AF_INET6);
    571 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
    572 			if (nml == 0)
    573 				strlcpy(line, "::", sizeof(line));
    574 			else
    575 				/* noncontiguous never happens in ipv6 */
    576 				snprintf(line, sizeof(line), "::/%d", nml);
    577 		}
    578 		else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
    579 		    nihost, sizeof(nihost), NULL, 0, niflags) != 0)
    580 			strlcpy(line, "invalid", sizeof(line));
    581 		else {
    582 			char *ccp;
    583 			if (!nflag && (ccp = strchr(nihost, '.')) &&
    584 			    strcmp(ccp + 1, domain) == 0)
    585 				*ccp = '\0';
    586 			strlcpy(line, nihost, sizeof(line));
    587 		}
    588 		break;
    589 	    }
    590 #endif
    591 
    592 #ifndef SMALL
    593 	case AF_ISO:
    594 		(void)snprintf(line, sizeof line, "iso %s",
    595 		    iso_ntoa(&((struct sockaddr_iso *)sa)->siso_addr));
    596 		break;
    597 
    598 	case AF_APPLETALK:
    599 		(void)snprintf(line, sizeof(line), "atalk %d.%d",
    600 		    ((struct sockaddr_at *)sa)->sat_addr.s_net,
    601 		    ((struct sockaddr_at *)sa)->sat_addr.s_node);
    602 		break;
    603 #endif /* SMALL */
    604 
    605 	default:
    606 		(void)snprintf(line, sizeof line, "(%d) %s",
    607 			sa->sa_family, any_ntoa(sa));
    608 		break;
    609 
    610 	}
    611 	return (line);
    612 }
    613 
    614 /*
    615  * Return the name of the network whose address is given.
    616  * The address is assumed to be that of a net or subnet, not a host.
    617  */
    618 const char *
    619 netname(struct sockaddr *sa, struct sockaddr *nm)
    620 {
    621 	const char *cp = 0;
    622 	static char line[50];
    623 	struct netent *np = 0;
    624 	u_int32_t net, mask;
    625 	u_int32_t i;
    626 	int subnetshift, nml;
    627 	struct in_addr in;
    628 
    629 	switch (sa->sa_family) {
    630 
    631 	case AF_INET:
    632 		in = ((struct sockaddr_in *)sa)->sin_addr;
    633 		i = ntohl(in.s_addr);
    634 		nml = netmask_length(nm, AF_INET);
    635 		if (i == 0) {
    636 			if (nml == 0)
    637 				cp = "default";
    638 			else {
    639 				static char notdefault[sizeof(NOTDEFSTRING)];
    640 
    641 				snprintf(notdefault, sizeof(notdefault),
    642 				    "0.0.0.0/%s",
    643 				    netmask_string(nm, nml, AF_INET));
    644 				cp = notdefault;
    645 			}
    646 		}
    647 		else if (!nflag) {
    648 			if (IN_CLASSA(i)) {
    649 				mask = IN_CLASSA_NET;
    650 				subnetshift = 8;
    651 			} else if (IN_CLASSB(i)) {
    652 				mask = IN_CLASSB_NET;
    653 				subnetshift = 8;
    654 			} else {
    655 				mask = IN_CLASSC_NET;
    656 				subnetshift = 4;
    657 			}
    658 			/*
    659 			 * If there are more bits than the standard mask
    660 			 * would suggest, subnets must be in use.
    661 			 * Guess at the subnet mask, assuming reasonable
    662 			 * width subnet fields.
    663 			 */
    664 			while (i &~ mask)
    665 				mask = (int32_t)mask >> subnetshift;
    666 			net = i & mask;
    667 			while ((mask & 1) == 0)
    668 				mask >>= 1, net >>= 1;
    669 			np = getnetbyaddr(net, AF_INET);
    670 			if (np)
    671 				cp = np->n_name;
    672 		}
    673 		if (cp)
    674 			(void)strlcpy(line, cp, sizeof(line));
    675 		else {
    676 			if (nml == 0)
    677 				strlcpy(line, inet_ntoa(in), sizeof(line));
    678 			else if (nml < 0) {
    679 				snprintf(line, sizeof(line), "%s&%s",
    680 				    inet_ntoa(in),
    681 				    netmask_string(nm, nml, AF_INET));
    682 			} else {
    683 				snprintf(line, sizeof(line), "%s/%d",
    684 				    inet_ntoa(in), nml);
    685 			}
    686 		}
    687 		break;
    688 
    689 	case AF_LINK:
    690 		return (link_ntoa((struct sockaddr_dl *)sa));
    691 
    692 #ifdef INET6
    693 	case AF_INET6:
    694 	    {
    695 		struct sockaddr_in6 sin6;
    696 		int niflags;
    697 
    698 		niflags = 0;
    699 		if (nflag)
    700 			niflags |= NI_NUMERICHOST;
    701 		memset(&sin6, 0, sizeof(sin6));
    702 		memcpy(&sin6, sa, sa->sa_len);
    703 		sin6.sin6_len = sizeof(struct sockaddr_in6);
    704 		sin6.sin6_family = AF_INET6;
    705 #ifdef __KAME__
    706 		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
    707 		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
    708 		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
    709 		    sin6.sin6_scope_id == 0) {
    710 			sin6.sin6_scope_id =
    711 			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
    712 			sin6.sin6_addr.s6_addr[2] = 0;
    713 			sin6.sin6_addr.s6_addr[3] = 0;
    714 		}
    715 #endif
    716 		nml = netmask_length(nm, AF_INET6);
    717 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
    718 			if (nml == 0)
    719 				strlcpy(line, "::", sizeof(line));
    720 			else
    721 				/* noncontiguous never happens in ipv6 */
    722 				snprintf(line, sizeof(line), "::/%d", nml);
    723 		}
    724 		else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
    725 		    line, sizeof(line), NULL, 0, niflags) != 0)
    726 			strlcpy(line, "invalid", sizeof(line));
    727 		break;
    728 	    }
    729 #endif
    730 
    731 #ifndef SMALL
    732 	case AF_ISO:
    733 		(void)snprintf(line, sizeof line, "iso %s",
    734 		    iso_ntoa(&((struct sockaddr_iso *)sa)->siso_addr));
    735 		break;
    736 
    737 	case AF_APPLETALK:
    738 		(void)snprintf(line, sizeof(line), "atalk %d.%d",
    739 		    ((struct sockaddr_at *)sa)->sat_addr.s_net,
    740 		    ((struct sockaddr_at *)sa)->sat_addr.s_node);
    741 		break;
    742 #endif /* SMALL */
    743 
    744 	default:
    745 		(void)snprintf(line, sizeof line, "af %d: %s",
    746 			sa->sa_family, any_ntoa(sa));
    747 		break;
    748 	}
    749 	return (line);
    750 }
    751 
    752 static const char *
    753 route_strerror(int error)
    754 {
    755 
    756 	switch (error) {
    757 	case ESRCH:
    758 		return "not in table";
    759 	case EBUSY:
    760 		return "entry in use";
    761 	case ENOBUFS:
    762 		return "routing table overflow";
    763 	default:
    764 		return strerror(error);
    765 	}
    766 }
    767 
    768 static void
    769 set_metric(char *value, int key)
    770 {
    771 	int flag = 0;
    772 	u_long noval, *valp = &noval;
    773 
    774 	switch (key) {
    775 #define caseof(x, y, z)	case x: valp = &rt_metrics.z; flag = y; break
    776 	caseof(K_MTU, RTV_MTU, rmx_mtu);
    777 	caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
    778 	caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
    779 	caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe);
    780 	caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe);
    781 	caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh);
    782 	caseof(K_RTT, RTV_RTT, rmx_rtt);
    783 	caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
    784 	}
    785 	rtm_inits |= flag;
    786 	if (lockrest || locking)
    787 		rt_metrics.rmx_locks |= flag;
    788 	if (locking)
    789 		locking = 0;
    790 	*valp = atoi(value);
    791 }
    792 
    793 static int
    794 newroute(int argc, char **argv)
    795 {
    796 	const char *cmd, *dest = "", *gateway = "";
    797 	int ishost = 0, ret, attempts, oerrno, flags = RTF_STATIC;
    798 	int key;
    799 	struct hostent *hp = 0;
    800 
    801 	cmd = argv[0];
    802 	af = AF_UNSPEC;
    803 	if (*cmd != 'g')
    804 		shutdown(sock, SHUT_RD); /* Don't want to read back our messages */
    805 	while (--argc > 0) {
    806 		if (**(++argv)== '-') {
    807 			switch (key = keyword(1 + *argv)) {
    808 
    809 			case K_SA:
    810 				af = PF_ROUTE;
    811 				aflen = sizeof(union sockunion);
    812 				break;
    813 
    814 #ifndef SMALL
    815 			case K_ATALK:
    816 				af = AF_APPLETALK;
    817 				aflen = sizeof(struct sockaddr_at);
    818 				break;
    819 #endif
    820 
    821 			case K_INET:
    822 				af = AF_INET;
    823 				aflen = sizeof(struct sockaddr_in);
    824 				break;
    825 
    826 #ifdef INET6
    827 			case K_INET6:
    828 				af = AF_INET6;
    829 				aflen = sizeof(struct sockaddr_in6);
    830 				break;
    831 #endif
    832 
    833 			case K_LINK:
    834 				af = AF_LINK;
    835 				aflen = sizeof(struct sockaddr_dl);
    836 				break;
    837 
    838 #ifndef SMALL
    839 			case K_OSI:
    840 			case K_ISO:
    841 				af = AF_ISO;
    842 				aflen = sizeof(struct sockaddr_iso);
    843 				break;
    844 #endif /* SMALL */
    845 
    846 			case K_IFACE:
    847 			case K_INTERFACE:
    848 				iflag++;
    849 				break;
    850 			case K_NOSTATIC:
    851 				flags &= ~RTF_STATIC;
    852 				break;
    853 			case K_LLINFO:
    854 				flags |= RTF_LLINFO;
    855 				break;
    856 			case K_LOCK:
    857 				locking = 1;
    858 				break;
    859 			case K_LOCKREST:
    860 				lockrest = 1;
    861 				break;
    862 			case K_HOST:
    863 				forcehost++;
    864 				break;
    865 			case K_REJECT:
    866 				flags |= RTF_REJECT;
    867 				break;
    868 			case K_BLACKHOLE:
    869 				flags |= RTF_BLACKHOLE;
    870 				break;
    871 			case K_CLONED:
    872 				flags |= RTF_CLONED;
    873 				break;
    874 			case K_NOCLONED:
    875 				flags &= ~RTF_CLONED;
    876 				break;
    877 			case K_PROTO1:
    878 				flags |= RTF_PROTO1;
    879 				break;
    880 			case K_PROTO2:
    881 				flags |= RTF_PROTO2;
    882 				break;
    883 			case K_CLONING:
    884 				flags |= RTF_CLONING;
    885 				break;
    886 			case K_NOCLONING:
    887 				flags &= ~RTF_CLONING;
    888 				break;
    889 			case K_XRESOLVE:
    890 				flags |= RTF_XRESOLVE;
    891 				break;
    892 			case K_STATIC:
    893 				flags |= RTF_STATIC;
    894 				break;
    895 			case K_IFA:
    896 				if (!--argc)
    897 					usage(1+*argv);
    898 				(void)getaddr(RTA_IFA, *++argv, 0);
    899 				break;
    900 			case K_IFP:
    901 				if (!--argc)
    902 					usage(1+*argv);
    903 				(void)getaddr(RTA_IFP, *++argv, 0);
    904 				break;
    905 			case K_GENMASK:
    906 				if (!--argc)
    907 					usage(1+*argv);
    908 				(void)getaddr(RTA_GENMASK, *++argv, 0);
    909 				break;
    910 			case K_GATEWAY:
    911 				if (!--argc)
    912 					usage(1+*argv);
    913 				(void)getaddr(RTA_GATEWAY, *++argv, 0);
    914 				break;
    915 			case K_DST:
    916 				if (!--argc)
    917 					usage(1+*argv);
    918 				ishost = getaddr(RTA_DST, *++argv, &hp);
    919 				dest = *argv;
    920 				break;
    921 			case K_NETMASK:
    922 				if (!--argc)
    923 					usage(1+*argv);
    924 				(void)getaddr(RTA_NETMASK, *++argv, 0);
    925 				/* FALLTHROUGH */
    926 			case K_NET:
    927 				forcenet++;
    928 				break;
    929 			case K_PREFIXLEN:
    930 				if (!--argc)
    931 					usage(1+*argv);
    932 				ishost = prefixlen(*++argv);
    933 				break;
    934 			case K_MTU:
    935 			case K_HOPCOUNT:
    936 			case K_EXPIRE:
    937 			case K_RECVPIPE:
    938 			case K_SENDPIPE:
    939 			case K_SSTHRESH:
    940 			case K_RTT:
    941 			case K_RTTVAR:
    942 				if (!--argc)
    943 					usage(1+*argv);
    944 				set_metric(*++argv, key);
    945 				break;
    946 			default:
    947 				usage(1+*argv);
    948 			}
    949 		} else {
    950 			if ((rtm_addrs & RTA_DST) == 0) {
    951 				dest = *argv;
    952 				ishost = getaddr(RTA_DST, *argv, &hp);
    953 			} else if ((rtm_addrs & RTA_GATEWAY) == 0) {
    954 				gateway = *argv;
    955 				(void)getaddr(RTA_GATEWAY, *argv, &hp);
    956 			} else {
    957 				ret = atoi(*argv);
    958 
    959 				if (ret == 0) {
    960 				    if (strcmp(*argv, "0") == 0) {
    961 					if (!qflag)  {
    962 					    warnx("%s, %s",
    963 						"old usage of trailing 0",
    964 						"assuming route to if");
    965 					}
    966 				    } else
    967 					usage(NULL);
    968 				    iflag = 1;
    969 				    continue;
    970 				} else if (ret > 0 && ret < 10) {
    971 				    if (!qflag) {
    972 					warnx("%s, %s",
    973 					    "old usage of trailing digit",
    974 					    "assuming route via gateway");
    975 				    }
    976 				    iflag = 0;
    977 				    continue;
    978 				}
    979 				(void)getaddr(RTA_NETMASK, *argv, 0);
    980 			}
    981 		}
    982 	}
    983 	if (forcehost && forcenet)
    984 		errx(EXIT_FAILURE, "-host and -net conflict");
    985 	else if (forcehost)
    986 		ishost = 1;
    987 	else if (forcenet)
    988 		ishost = 0;
    989 	flags |= RTF_UP;
    990 	if (ishost)
    991 		flags |= RTF_HOST;
    992 	if (iflag == 0)
    993 		flags |= RTF_GATEWAY;
    994 	for (attempts = 1; ; attempts++) {
    995 		errno = 0;
    996 		if ((ret = rtmsg(*cmd, flags)) == 0)
    997 			break;
    998 		if (errno != ENETUNREACH && errno != ESRCH)
    999 			break;
   1000 		if (af == AF_INET && *gateway && hp && hp->h_addr_list[1]) {
   1001 			hp->h_addr_list++;
   1002 			memmove(&so_gate.sin.sin_addr, hp->h_addr_list[0],
   1003 			    hp->h_length);
   1004 		} else
   1005 			break;
   1006 	}
   1007 	if (*cmd == 'g')
   1008 		return (ret != 0);
   1009 	if (!qflag) {
   1010 		oerrno = errno;
   1011 		(void)printf("%s %s %s", cmd, ishost? "host" : "net", dest);
   1012 		if (*gateway) {
   1013 			(void)printf(": gateway %s", gateway);
   1014 			if (attempts > 1 && ret == 0 && af == AF_INET)
   1015 			    (void)printf(" (%s)",
   1016 			        inet_ntoa(so_gate.sin.sin_addr));
   1017 		}
   1018 		if (ret == 0)
   1019 			(void)printf("\n");
   1020 		else
   1021 			(void)printf(": %s\n", route_strerror(oerrno));
   1022 	}
   1023 	return (ret != 0);
   1024 }
   1025 
   1026 static void
   1027 inet_makenetandmask(u_int32_t net, struct sockaddr_in *isin)
   1028 {
   1029 	u_int32_t addr, mask = 0;
   1030 	char *cp;
   1031 
   1032 	rtm_addrs |= RTA_NETMASK;
   1033 	if (net == 0)
   1034 		mask = addr = 0;
   1035 	else if (net < 128) {
   1036 		addr = net << IN_CLASSA_NSHIFT;
   1037 		mask = IN_CLASSA_NET;
   1038 	} else if (net < 192) {
   1039 		addr = net << IN_CLASSA_NSHIFT;
   1040 		mask = IN_CLASSB_NET;
   1041 	} else if (net < 224) {
   1042 		addr = net << IN_CLASSA_NSHIFT;
   1043 		mask = IN_CLASSC_NET;
   1044 	} else if (net < 256) {
   1045 		addr = net << IN_CLASSA_NSHIFT;
   1046 		mask = IN_CLASSD_NET;
   1047 	} else if (net < 49152) { /* 192 * 256 */
   1048 		addr = net << IN_CLASSB_NSHIFT;
   1049 		mask = IN_CLASSB_NET;
   1050 	} else if (net < 57344) { /* 224 * 256 */
   1051 		addr = net << IN_CLASSB_NSHIFT;
   1052 		mask = IN_CLASSC_NET;
   1053 	} else if (net < 65536) {
   1054 		addr = net << IN_CLASSB_NSHIFT;
   1055 		mask = IN_CLASSB_NET;
   1056 	} else if (net < 14680064L) { /* 224 * 65536 */
   1057 		addr = net << IN_CLASSC_NSHIFT;
   1058 		mask = IN_CLASSC_NET;
   1059 	} else if (net < 16777216L) {
   1060 		addr = net << IN_CLASSC_NSHIFT;
   1061 		mask = IN_CLASSD_NET;
   1062 	} else {
   1063 		addr = net;
   1064 		if ((addr & IN_CLASSA_HOST) == 0)
   1065 			mask =  IN_CLASSA_NET;
   1066 		else if ((addr & IN_CLASSB_HOST) == 0)
   1067 			mask =  IN_CLASSB_NET;
   1068 		else if ((addr & IN_CLASSC_HOST) == 0)
   1069 			mask =  IN_CLASSC_NET;
   1070 		else
   1071 			mask = -1;
   1072 	}
   1073 	isin->sin_addr.s_addr = htonl(addr);
   1074 	isin = &so_mask.sin;
   1075 	isin->sin_addr.s_addr = htonl(mask);
   1076 	isin->sin_len = 0;
   1077 	isin->sin_family = 0;
   1078 	cp = (char *)(&isin->sin_addr + 1);
   1079 	while (*--cp == 0 && cp > (char *)isin)
   1080 		;
   1081 	isin->sin_len = 1 + cp - (char *)isin;
   1082 	isin->sin_family = AF_INET;
   1083 }
   1084 
   1085 #ifdef INET6
   1086 /*
   1087  * XXX the function may need more improvement...
   1088  */
   1089 static int
   1090 inet6_makenetandmask(struct sockaddr_in6 *sin6)
   1091 {
   1092 	const char *plen;
   1093 	struct in6_addr in6;
   1094 
   1095 	plen = NULL;
   1096 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
   1097 	    sin6->sin6_scope_id == 0) {
   1098 		plen = "0";
   1099 	} else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) {
   1100 		/* aggregatable global unicast - RFC2374 */
   1101 		memset(&in6, 0, sizeof(in6));
   1102 		if (!memcmp(&sin6->sin6_addr.s6_addr[8], &in6.s6_addr[8], 8))
   1103 			plen = "64";
   1104 	}
   1105 
   1106 	if (!plen || strcmp(plen, "128") == 0)
   1107 		return 1;
   1108 	else {
   1109 		rtm_addrs |= RTA_NETMASK;
   1110 		(void)prefixlen(plen);
   1111 		return 0;
   1112 	}
   1113 }
   1114 #endif
   1115 
   1116 /*
   1117  * Interpret an argument as a network address of some kind,
   1118  * returning 1 if a host address, 0 if a network address.
   1119  */
   1120 static int
   1121 getaddr(int which, char *s, struct hostent **hpp)
   1122 {
   1123 	sup su;
   1124 	struct hostent *hp;
   1125 	struct netent *np;
   1126 	u_int32_t val;
   1127 	char *t;
   1128 	int afamily;  /* local copy of af so we can change it */
   1129 
   1130 	if (af == AF_UNSPEC) {
   1131 		af = AF_INET;
   1132 		aflen = sizeof(struct sockaddr_in);
   1133 	}
   1134 	afamily = af;
   1135 	rtm_addrs |= which;
   1136 	switch (which) {
   1137 	case RTA_DST:
   1138 		su = &so_dst;
   1139 		break;
   1140 	case RTA_GATEWAY:
   1141 		su = &so_gate;
   1142 		break;
   1143 	case RTA_NETMASK:
   1144 		su = &so_mask;
   1145 		break;
   1146 	case RTA_GENMASK:
   1147 		su = &so_genmask;
   1148 		break;
   1149 	case RTA_IFP:
   1150 		su = &so_ifp;
   1151 		afamily = AF_LINK;
   1152 		break;
   1153 	case RTA_IFA:
   1154 		su = &so_ifa;
   1155 		su->sa.sa_family = af;
   1156 		break;
   1157 	default:
   1158 		su = NULL;
   1159 		usage("Internal Error");
   1160 		/*NOTREACHED*/
   1161 	}
   1162 	su->sa.sa_len = aflen;
   1163 	su->sa.sa_family = afamily; /* cases that don't want it have left already */
   1164 	if (strcmp(s, "default") == 0) {
   1165 		switch (which) {
   1166 		case RTA_DST:
   1167 			forcenet++;
   1168 			(void)getaddr(RTA_NETMASK, s, 0);
   1169 			break;
   1170 		case RTA_NETMASK:
   1171 		case RTA_GENMASK:
   1172 			su->sa.sa_len = 0;
   1173 		}
   1174 		return (0);
   1175 	}
   1176 	switch (afamily) {
   1177 #ifdef INET6
   1178 	case AF_INET6:
   1179 	    {
   1180 		struct addrinfo hints, *res;
   1181 		char *slash = 0;
   1182 
   1183 		if (which == RTA_DST && (slash = (strrchr(s, '/'))) != 0)
   1184 			*slash = '\0';
   1185 		memset(&hints, 0, sizeof(hints));
   1186 		hints.ai_family = afamily;	/*AF_INET6*/
   1187 		hints.ai_flags = AI_NUMERICHOST;
   1188 		hints.ai_socktype = SOCK_DGRAM;		/*dummy*/
   1189 		if (getaddrinfo(s, "0", &hints, &res) != 0) {
   1190 			hints.ai_flags = 0;
   1191 			if (slash) {
   1192 				*slash = '/';
   1193 				slash = 0;
   1194 			}
   1195 			if (getaddrinfo(s, "0", &hints, &res) != 0)
   1196 				errx(EXIT_FAILURE, "%s: bad value", s);
   1197 		}
   1198 		if (slash)
   1199 			*slash = '/';
   1200 		if (sizeof(su->sin6) != res->ai_addrlen)
   1201 			errx(EXIT_FAILURE, "%s: bad value", s);
   1202 		if (res->ai_next) {
   1203 			errx(EXIT_FAILURE,
   1204 			    "%s: address resolved to multiple values", s);
   1205 		}
   1206 		memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
   1207 		freeaddrinfo(res);
   1208 #ifdef __KAME__
   1209 		if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
   1210 		     IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr)) &&
   1211 		    su->sin6.sin6_scope_id) {
   1212 			*(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
   1213 				htons(su->sin6.sin6_scope_id);
   1214 			su->sin6.sin6_scope_id = 0;
   1215 		}
   1216 #endif
   1217 		if (hints.ai_flags == AI_NUMERICHOST) {
   1218 			if (slash)
   1219 				return (prefixlen(slash + 1));
   1220 			if (which == RTA_DST)
   1221 				return (inet6_makenetandmask(&su->sin6));
   1222 			return (0);
   1223 		} else
   1224 			return (1);
   1225 	    }
   1226 #endif
   1227 
   1228 #ifndef SMALL
   1229 	case AF_OSI:
   1230 		su->siso.siso_addr = *iso_addr(s);
   1231 		if (which == RTA_NETMASK || which == RTA_GENMASK) {
   1232 			char *cp = (char *)TSEL(&su->siso);
   1233 			su->siso.siso_nlen = 0;
   1234 			do {--cp ;} while ((cp > (char *)su) && (*cp == 0));
   1235 			su->siso.siso_len = 1 + cp - (char *)su;
   1236 		}
   1237 		return (1);
   1238 #endif /* SMALL */
   1239 
   1240 	case PF_ROUTE:
   1241 		su->sa.sa_len = sizeof(*su);
   1242 		sockaddr(s, &su->sa);
   1243 		return (1);
   1244 
   1245 #ifndef SMALL
   1246 	case AF_APPLETALK:
   1247 		t = strchr (s, '.');
   1248 		if (!t) {
   1249 badataddr:
   1250 			errx(EXIT_FAILURE, "bad address: %s", s);
   1251 		}
   1252 		val = atoi (s);
   1253 		if (val > 65535)
   1254 			goto badataddr;
   1255 		su->sat.sat_addr.s_net = val;
   1256 		val = atoi (t);
   1257 		if (val > 256)
   1258 			goto badataddr;
   1259 		su->sat.sat_addr.s_node = val;
   1260 		rtm_addrs |= RTA_NETMASK;
   1261 		return(forcehost || su->sat.sat_addr.s_node != 0);
   1262 #endif
   1263 
   1264 	case AF_LINK:
   1265 		link_addr(s, &su->sdl);
   1266 		return (1);
   1267 
   1268 	case AF_INET:
   1269 	default:
   1270 		break;
   1271 	}
   1272 
   1273 	if (hpp == NULL)
   1274 		hpp = &hp;
   1275 	*hpp = NULL;
   1276 
   1277 	if ((t = strchr(s, '/')) != NULL && which == RTA_DST) {
   1278 		*t = '\0';
   1279 		if (forcenet == 0) {
   1280 			if ((val = inet_addr(s)) != INADDR_NONE) {
   1281 				inet_makenetandmask(htonl(val), &su->sin);
   1282 				return prefixlen(&t[1]);
   1283 			}
   1284 		} else {
   1285 			if ((val = inet_network(s)) != INADDR_NONE) {
   1286 				inet_makenetandmask(val, &su->sin);
   1287 				return prefixlen(&t[1]);
   1288 			}
   1289 		}
   1290 		*t = '/';
   1291 	}
   1292 	if (inet_aton(s, &su->sin.sin_addr) &&
   1293 	    (which != RTA_DST || forcenet == 0)) {
   1294 		val = su->sin.sin_addr.s_addr;
   1295 		if (inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
   1296 			return (1);
   1297 		else {
   1298 			val = ntohl(val);
   1299 			goto netdone;
   1300 		}
   1301 	}
   1302 	if ((val = inet_network(s)) != INADDR_NONE ||
   1303 	    ((np = getnetbyname(s)) != NULL && (val = np->n_net) != 0)) {
   1304 netdone:
   1305 		if (which == RTA_DST)
   1306 			inet_makenetandmask(val, &su->sin);
   1307 		return (0);
   1308 	}
   1309 	hp = gethostbyname(s);
   1310 	if (hp) {
   1311 		*hpp = hp;
   1312 		su->sin.sin_family = hp->h_addrtype;
   1313 		memmove(&su->sin.sin_addr, hp->h_addr, hp->h_length);
   1314 		return (1);
   1315 	}
   1316 	errx(EXIT_FAILURE, "%s: bad value", s);
   1317 	/*NOTREACHED*/
   1318 }
   1319 
   1320 int
   1321 prefixlen(const char *s)
   1322 {
   1323 	int len = atoi(s), q, r;
   1324 	int max;
   1325 
   1326 	switch (af) {
   1327 	case AF_INET:
   1328 		max = sizeof(struct in_addr) * 8;
   1329 		break;
   1330 #ifdef INET6
   1331 	case AF_INET6:
   1332 		max = sizeof(struct in6_addr) * 8;
   1333 		break;
   1334 #endif
   1335 	default:
   1336 		errx(EXIT_FAILURE, "prefixlen is not supported with af %d", af);
   1337 		/*NOTREACHED*/
   1338 	}
   1339 
   1340 	rtm_addrs |= RTA_NETMASK;
   1341 	if (len < -1 || len > max)
   1342 		errx(EXIT_FAILURE, "%s: bad value", s);
   1343 
   1344 	q = len >> 3;
   1345 	r = len & 7;
   1346 	switch (af) {
   1347 	case AF_INET:
   1348 		memset(&so_mask, 0, sizeof(so_mask));
   1349 		so_mask.sin.sin_family = AF_INET;
   1350 		so_mask.sin.sin_len = sizeof(struct sockaddr_in);
   1351 		so_mask.sin.sin_addr.s_addr = htonl(0xffffffff << (32 - len));
   1352 		break;
   1353 #ifdef INET6
   1354 	case AF_INET6:
   1355 		so_mask.sin6.sin6_family = AF_INET6;
   1356 		so_mask.sin6.sin6_len = sizeof(struct sockaddr_in6);
   1357 		memset((void *)&so_mask.sin6.sin6_addr, 0,
   1358 			sizeof(so_mask.sin6.sin6_addr));
   1359 		if (q > 0)
   1360 			memset((void *)&so_mask.sin6.sin6_addr, 0xff, q);
   1361 		if (r > 0)
   1362 			*((u_char *)&so_mask.sin6.sin6_addr + q) =
   1363 			    (0xff00 >> r) & 0xff;
   1364 		break;
   1365 #endif
   1366 	}
   1367 	return (len == max);
   1368 }
   1369 
   1370 #ifndef SMALL
   1371 static void
   1372 interfaces(void)
   1373 {
   1374 	size_t needed;
   1375 	int mib[6];
   1376 	char *buf, *lim, *next;
   1377 	struct rt_msghdr *rtm;
   1378 
   1379 	mib[0] = CTL_NET;
   1380 	mib[1] = PF_ROUTE;
   1381 	mib[2] = 0;		/* protocol */
   1382 	mib[3] = 0;		/* wildcard address family */
   1383 	mib[4] = NET_RT_IFLIST;
   1384 	mib[5] = 0;		/* no flags */
   1385 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
   1386 		err(EXIT_FAILURE, "route-sysctl-estimate");
   1387 	if (needed) {
   1388 		if ((buf = malloc(needed)) == NULL)
   1389 			err(EXIT_FAILURE, "malloc");
   1390 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
   1391 			err(EXIT_FAILURE,
   1392 			    "actual retrieval of interface table");
   1393 		}
   1394 		lim = buf + needed;
   1395 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
   1396 			rtm = (struct rt_msghdr *)next;
   1397 			print_rtmsg(rtm, rtm->rtm_msglen);
   1398 		}
   1399 		free(buf);
   1400 	}
   1401 }
   1402 
   1403 static void
   1404 monitor(void)
   1405 {
   1406 	int n;
   1407 	char msg[2048];
   1408 
   1409 	verbose = 1;
   1410 	if (debugonly) {
   1411 		interfaces();
   1412 		exit(0);
   1413 	}
   1414 	for(;;) {
   1415 		time_t now;
   1416 		n = read(sock, msg, 2048);
   1417 		now = time(NULL);
   1418 		(void)printf("got message of size %d on %s", n, ctime(&now));
   1419 		print_rtmsg((struct rt_msghdr *)msg, n);
   1420 	}
   1421 }
   1422 
   1423 #endif /* SMALL */
   1424 
   1425 
   1426 struct {
   1427 	struct	rt_msghdr m_rtm;
   1428 	char	m_space[512];
   1429 } m_rtmsg;
   1430 
   1431 static int
   1432 rtmsg(int cmd, int flags)
   1433 {
   1434 	static int seq;
   1435 	int rlen;
   1436 	char *cp = m_rtmsg.m_space;
   1437 	int l;
   1438 
   1439 #define NEXTADDR(w, u) \
   1440 	if (rtm_addrs & (w)) {\
   1441 	    l = ROUNDUP(u.sa.sa_len); memmove(cp, &(u), l); cp += l;\
   1442 	    if (verbose && ! shortoutput) sodump(&(u),#u);\
   1443 	}
   1444 
   1445 	errno = 0;
   1446 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
   1447 	if (cmd == 'a')
   1448 		cmd = RTM_ADD;
   1449 	else if (cmd == 'c')
   1450 		cmd = RTM_CHANGE;
   1451 	else if (cmd == 'g') {
   1452 #ifdef	SMALL
   1453 		return (-1);
   1454 #else	/* SMALL */
   1455 		cmd = RTM_GET;
   1456 		if (so_ifp.sa.sa_family == 0) {
   1457 			so_ifp.sa.sa_family = AF_LINK;
   1458 			so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
   1459 			rtm_addrs |= RTA_IFP;
   1460 		}
   1461 #endif	/* SMALL */
   1462 	} else
   1463 		cmd = RTM_DELETE;
   1464 #define rtm m_rtmsg.m_rtm
   1465 	rtm.rtm_type = cmd;
   1466 	rtm.rtm_flags = flags;
   1467 	rtm.rtm_version = RTM_VERSION;
   1468 	rtm.rtm_seq = ++seq;
   1469 	rtm.rtm_addrs = rtm_addrs;
   1470 	rtm.rtm_rmx = rt_metrics;
   1471 	rtm.rtm_inits = rtm_inits;
   1472 
   1473 	if (rtm_addrs & RTA_NETMASK)
   1474 		mask_addr();
   1475 	NEXTADDR(RTA_DST, so_dst);
   1476 	NEXTADDR(RTA_GATEWAY, so_gate);
   1477 	NEXTADDR(RTA_NETMASK, so_mask);
   1478 	NEXTADDR(RTA_GENMASK, so_genmask);
   1479 	NEXTADDR(RTA_IFP, so_ifp);
   1480 	NEXTADDR(RTA_IFA, so_ifa);
   1481 	rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
   1482 	if (verbose && ! shortoutput) {
   1483 		if (rtm_addrs)
   1484 			putchar('\n');
   1485 		print_rtmsg(&rtm, l);
   1486 	}
   1487 	if (debugonly)
   1488 		return (0);
   1489 	if ((rlen = write(sock, (char *)&m_rtmsg, l)) < 0) {
   1490 		warnx("writing to routing socket: %s", route_strerror(errno));
   1491 		return (-1);
   1492 	}
   1493 	if (rlen < l) {
   1494 		warnx("write to routing socket, got %d for rlen", rlen);
   1495 		return 1;
   1496 	}
   1497 #ifndef	SMALL
   1498 	if (cmd == RTM_GET) {
   1499 		do {
   1500 			l = read(sock, (char *)&m_rtmsg, sizeof(m_rtmsg));
   1501 		} while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
   1502 		if (l < 0)
   1503 			err(EXIT_FAILURE, "read from routing socket");
   1504 		else
   1505 			return print_getmsg(&rtm, l);
   1506 	}
   1507 #endif	/* SMALL */
   1508 #undef rtm
   1509 	return (0);
   1510 }
   1511 
   1512 static void
   1513 mask_addr(void)
   1514 {
   1515 	int olen = so_mask.sa.sa_len;
   1516 	char *cp1 = olen + (char *)&so_mask, *cp2;
   1517 
   1518 	for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
   1519 		if (*--cp1 != 0) {
   1520 			so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
   1521 			break;
   1522 		}
   1523 	if ((rtm_addrs & RTA_DST) == 0)
   1524 		return;
   1525 	switch (so_dst.sa.sa_family) {
   1526 	case AF_INET:
   1527 #ifdef INET6
   1528 	case AF_INET6:
   1529 #endif
   1530 #ifndef SMALL
   1531 	case AF_APPLETALK:
   1532 #endif /* SMALL */
   1533 	case 0:
   1534 		return;
   1535 #ifndef SMALL
   1536 	case AF_ISO:
   1537 		olen = MIN(so_dst.siso.siso_nlen,
   1538 			   MAX(so_mask.sa.sa_len - 6, 0));
   1539 		break;
   1540 #endif /* SMALL */
   1541 	}
   1542 	cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
   1543 	cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
   1544 	while (cp2 > cp1)
   1545 		*--cp2 = 0;
   1546 	cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
   1547 	while (cp1 > so_dst.sa.sa_data)
   1548 		*--cp1 &= *--cp2;
   1549 #ifndef SMALL
   1550 	switch (so_dst.sa.sa_family) {
   1551 	case AF_ISO:
   1552 		so_dst.siso.siso_nlen = olen;
   1553 		break;
   1554 	}
   1555 #endif /* SMALL */
   1556 }
   1557 
   1558 const char *msgtypes[] = {
   1559 	"",
   1560 	"RTM_ADD: Add Route",
   1561 	"RTM_DELETE: Delete Route",
   1562 	"RTM_CHANGE: Change Metrics or flags",
   1563 	"RTM_GET: Report Metrics",
   1564 	"RTM_LOSING: Kernel Suspects Partitioning",
   1565 	"RTM_REDIRECT: Told to use different route",
   1566 	"RTM_MISS: Lookup failed on this address",
   1567 	"RTM_LOCK: fix specified metrics",
   1568 	"RTM_OLDADD: caused by SIOCADDRT",
   1569 	"RTM_OLDDEL: caused by SIOCDELRT",
   1570 	"RTM_RESOLVE: Route created by cloning",
   1571 	"RTM_NEWADDR: address being added to iface",
   1572 	"RTM_DELADDR: address being removed from iface",
   1573 	"RTM_OIFINFO: iface status change (pre-1.5)",
   1574 	"RTM_IFINFO: iface status change",
   1575 	"RTM_IFANNOUNCE: iface arrival/departure",
   1576 	"RTM_IEEE80211: IEEE80211 wireless event",
   1577 	0,
   1578 };
   1579 
   1580 const char metricnames[] =
   1581 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount\1mtu";
   1582 const char routeflags[] =
   1583 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016CLONED\017PROTO2\020PROTO1";
   1584 const char ifnetflags[] =
   1585 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6NOTRAILERS\7RUNNING\010NOARP\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1\017LINK2\020MULTICAST";
   1586 const char addrnames[] =
   1587 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
   1588 
   1589 
   1590 #ifndef SMALL
   1591 static const char *
   1592 linkstate(struct if_msghdr *ifm)
   1593 {
   1594 	static char buf[64];
   1595 
   1596 	switch (ifm->ifm_data.ifi_link_state) {
   1597 	case LINK_STATE_UNKNOWN:
   1598 		return "carrier: unknown";
   1599 	case LINK_STATE_DOWN:
   1600 		return "carrier: no carrier";
   1601 	case LINK_STATE_UP:
   1602 		return "carrier: active";
   1603 	default:
   1604 		(void)snprintf(buf, sizeof(buf), "carrier: 0x%x",
   1605 		    ifm->ifm_data.ifi_link_state);
   1606 		return buf;
   1607 	}
   1608 }
   1609 #endif /* SMALL */
   1610 
   1611 static void
   1612 print_rtmsg(struct rt_msghdr *rtm, int msglen)
   1613 {
   1614 	struct if_msghdr *ifm;
   1615 	struct ifa_msghdr *ifam;
   1616 	struct if_announcemsghdr *ifan;
   1617 	union {
   1618 		struct ieee80211_join_event join;
   1619 		struct ieee80211_leave_event leave;
   1620 		struct ieee80211_replay_event replay;
   1621 		struct ieee80211_michael_event michael;
   1622 	} ev;
   1623 	size_t evlen = 0;
   1624 
   1625 	if (verbose == 0)
   1626 		return;
   1627 	if (rtm->rtm_version != RTM_VERSION) {
   1628 		(void)printf("routing message version %d not understood\n",
   1629 		    rtm->rtm_version);
   1630 		return;
   1631 	}
   1632 	if (msgtypes[rtm->rtm_type])
   1633 		(void)printf("%s: ", msgtypes[rtm->rtm_type]);
   1634 	else
   1635 		(void)printf("#%d: ", rtm->rtm_type);
   1636 	(void)printf("len %d, ", rtm->rtm_msglen);
   1637 	switch (rtm->rtm_type) {
   1638 	case RTM_IFINFO:
   1639 		ifm = (struct if_msghdr *)rtm;
   1640 		(void)printf("if# %d, %s, flags: ", ifm->ifm_index,
   1641 #ifdef SMALL
   1642 		    ""
   1643 #else
   1644 		    linkstate(ifm)
   1645 #endif /* SMALL */
   1646 		    );
   1647 		bprintf(stdout, ifm->ifm_flags, ifnetflags);
   1648 		pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
   1649 		break;
   1650 	case RTM_NEWADDR:
   1651 	case RTM_DELADDR:
   1652 		ifam = (struct ifa_msghdr *)rtm;
   1653 		(void)printf("metric %d, flags: ", ifam->ifam_metric);
   1654 		bprintf(stdout, ifam->ifam_flags, routeflags);
   1655 		pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
   1656 		break;
   1657 	case RTM_IEEE80211:
   1658 		ifan = (struct if_announcemsghdr *)rtm;
   1659 		(void)printf("if# %d, what: ", ifan->ifan_index);
   1660 		switch (ifan->ifan_what) {
   1661 		case RTM_IEEE80211_ASSOC:
   1662 			printf("associate");
   1663 			break;
   1664 		case RTM_IEEE80211_REASSOC:
   1665 			printf("re-associate");
   1666 			break;
   1667 		case RTM_IEEE80211_DISASSOC:
   1668 			printf("disassociate");
   1669 			break;
   1670 		case RTM_IEEE80211_SCAN:
   1671 			printf("scan complete");
   1672 			break;
   1673 		case RTM_IEEE80211_JOIN:
   1674 			evlen = sizeof(ev.join);
   1675 			printf("join");
   1676 			break;
   1677 		case RTM_IEEE80211_LEAVE:
   1678 			evlen = sizeof(ev.leave);
   1679 			printf("leave");
   1680 			break;
   1681 		case RTM_IEEE80211_MICHAEL:
   1682 			evlen = sizeof(ev.michael);
   1683 			printf("michael");
   1684 			break;
   1685 		case RTM_IEEE80211_REPLAY:
   1686 			evlen = sizeof(ev.replay);
   1687 			printf("replay");
   1688 			break;
   1689 		default:
   1690 			evlen = 0;
   1691 			printf("#%d", ifan->ifan_what);
   1692 			break;
   1693 		}
   1694 		if (sizeof(*ifan) + evlen > ifan->ifan_msglen) {
   1695 			printf(" (truncated)\n");
   1696 			break;
   1697 		}
   1698 		(void)memcpy(&ev, (ifan + 1), evlen);
   1699 		switch (ifan->ifan_what) {
   1700 		case RTM_IEEE80211_JOIN:
   1701 		case RTM_IEEE80211_LEAVE:
   1702 			printf(" mac %" PRIETHER,
   1703 			    PRIETHER_ARGS(ev.join.iev_addr));
   1704 			break;
   1705 		case RTM_IEEE80211_REPLAY:
   1706 		case RTM_IEEE80211_MICHAEL:
   1707 			printf(" src %" PRIETHER " dst %" PRIETHER
   1708 			       " cipher %" PRIu8 " keyix %" PRIu8,
   1709 			       PRIETHER_ARGS(ev.replay.iev_src),
   1710 			       PRIETHER_ARGS(ev.replay.iev_dst),
   1711 			       ev.replay.iev_cipher,
   1712 			       ev.replay.iev_keyix);
   1713 			if (ifan->ifan_what == RTM_IEEE80211_REPLAY) {
   1714 				printf(" key rsc %#" PRIx64
   1715 				       " frame rsc %#" PRIx64,
   1716 				       ev.replay.iev_keyrsc, ev.replay.iev_rsc);
   1717 			}
   1718 			break;
   1719 		default:
   1720 			break;
   1721 		}
   1722 		printf("\n");
   1723 		break;
   1724 	case RTM_IFANNOUNCE:
   1725 		ifan = (struct if_announcemsghdr *)rtm;
   1726 		(void)printf("if# %d, what: ", ifan->ifan_index);
   1727 		switch (ifan->ifan_what) {
   1728 		case IFAN_ARRIVAL:
   1729 			printf("arrival");
   1730 			break;
   1731 		case IFAN_DEPARTURE:
   1732 			printf("departure");
   1733 			break;
   1734 		default:
   1735 			printf("#%d", ifan->ifan_what);
   1736 			break;
   1737 		}
   1738 		printf("\n");
   1739 		break;
   1740 	default:
   1741 		(void)printf("pid %d, seq %d, errno %d, flags: ",
   1742 			rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
   1743 		bprintf(stdout, rtm->rtm_flags, routeflags);
   1744 		pmsg_common(rtm);
   1745 	}
   1746 }
   1747 
   1748 #ifndef	SMALL
   1749 static int
   1750 print_getmsg(struct rt_msghdr *rtm, int msglen)
   1751 {
   1752 	struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL, *ifa = NULL;
   1753 	struct sockaddr_dl *ifp = NULL;
   1754 	struct sockaddr *sa;
   1755 	char *cp;
   1756 	int i;
   1757 
   1758 	if (! shortoutput)
   1759 		(void)printf("   route to: %s\n",
   1760 		    routename((struct sockaddr *) &so_dst, NULL, RTF_HOST));
   1761 	if (rtm->rtm_version != RTM_VERSION) {
   1762 		warnx("routing message version %d not understood",
   1763 		    rtm->rtm_version);
   1764 		return 1;
   1765 	}
   1766 	if (rtm->rtm_msglen > msglen) {
   1767 		warnx("message length mismatch, in packet %d, returned %d",
   1768 		    rtm->rtm_msglen, msglen);
   1769 	}
   1770 	if (rtm->rtm_errno)  {
   1771 		warnx("RTM_GET: %s (errno %d)",
   1772 		    strerror(rtm->rtm_errno), rtm->rtm_errno);
   1773 		return 1;
   1774 	}
   1775 	cp = ((char *)(rtm + 1));
   1776 	if (rtm->rtm_addrs)
   1777 		for (i = 1; i; i <<= 1)
   1778 			if (i & rtm->rtm_addrs) {
   1779 				sa = (struct sockaddr *)cp;
   1780 				switch (i) {
   1781 				case RTA_DST:
   1782 					dst = sa;
   1783 					break;
   1784 				case RTA_GATEWAY:
   1785 					gate = sa;
   1786 					break;
   1787 				case RTA_NETMASK:
   1788 					mask = sa;
   1789 					break;
   1790 				case RTA_IFP:
   1791 					if (sa->sa_family == AF_LINK &&
   1792 					   ((struct sockaddr_dl *)sa)->sdl_nlen)
   1793 						ifp = (struct sockaddr_dl *)sa;
   1794 					break;
   1795 				case RTA_IFA:
   1796 					ifa = sa;
   1797 					break;
   1798 				}
   1799 				ADVANCE(cp, sa);
   1800 			}
   1801 	if (dst && mask)
   1802 		mask->sa_family = dst->sa_family;	/* XXX */
   1803 	if (dst && ! shortoutput)
   1804 		(void)printf("destination: %s\n",
   1805 		    routename(dst, mask, RTF_HOST));
   1806 	if (mask && ! shortoutput) {
   1807 		int savenflag = nflag;
   1808 
   1809 		nflag = 1;
   1810 		(void)printf("       mask: %s\n",
   1811 		    routename(mask, NULL, RTF_HOST));
   1812 		nflag = savenflag;
   1813 	}
   1814 	if (gate && rtm->rtm_flags & RTF_GATEWAY) {
   1815 		const char *name;
   1816 
   1817 		name = routename(gate, NULL, RTF_HOST);
   1818 		if (shortoutput) {
   1819 			if (*name == '\0')
   1820 				return (1);
   1821 			(void)printf("%s\n", name);
   1822 		} else
   1823 			(void)printf("    gateway: %s\n", name);
   1824 	}
   1825 	if (ifa && ! shortoutput)
   1826 		(void)printf(" local addr: %s\n",
   1827 		    routename(ifa, NULL, RTF_HOST));
   1828 	if (ifp && ! shortoutput)
   1829 		(void)printf("  interface: %.*s\n",
   1830 		    ifp->sdl_nlen, ifp->sdl_data);
   1831 	if (! shortoutput) {
   1832 		(void)printf("      flags: ");
   1833 		bprintf(stdout, rtm->rtm_flags, routeflags);
   1834 	}
   1835 
   1836 #define lock(f)	((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
   1837 #define msec(u)	(((u) + 500) / 1000)		/* usec to msec */
   1838 
   1839 	if (! shortoutput) {
   1840 		(void)printf("\n%s\n", "\
   1841  recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire");
   1842 		printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
   1843 		printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
   1844 		printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
   1845 		printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
   1846 		printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
   1847 		printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
   1848 		printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
   1849 		if (rtm->rtm_rmx.rmx_expire)
   1850 			rtm->rtm_rmx.rmx_expire -= time(0);
   1851 		printf("%8ld%c\n", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
   1852 	}
   1853 #undef lock
   1854 #undef msec
   1855 #define	RTA_IGN	(RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
   1856 
   1857 	if (shortoutput)
   1858 		return ((rtm->rtm_addrs & RTF_GATEWAY) == 0);
   1859 	else if (verbose)
   1860 		pmsg_common(rtm);
   1861 	else if (rtm->rtm_addrs &~ RTA_IGN) {
   1862 		(void)printf("sockaddrs: ");
   1863 		bprintf(stdout, rtm->rtm_addrs, addrnames);
   1864 		putchar('\n');
   1865 	}
   1866 	return 0;
   1867 #undef	RTA_IGN
   1868 }
   1869 #endif	/* SMALL */
   1870 
   1871 void
   1872 pmsg_common(struct rt_msghdr *rtm)
   1873 {
   1874 	(void)printf("\nlocks: ");
   1875 	bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
   1876 	(void)printf(" inits: ");
   1877 	bprintf(stdout, rtm->rtm_inits, metricnames);
   1878 	pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
   1879 }
   1880 
   1881 static void
   1882 pmsg_addrs(char *cp, int addrs)
   1883 {
   1884 	struct sockaddr *sa[RTAX_MAX];
   1885 	int i, nmf;
   1886 
   1887 	if (addrs != 0) {
   1888 		(void)printf("\nsockaddrs: ");
   1889 		bprintf(stdout, addrs, addrnames);
   1890 		(void)putchar('\n');
   1891 		nmf = -1;
   1892 		for (i = 0; i < RTAX_MAX; i++) {
   1893 			if ((1 << i) & addrs) {
   1894 				sa[i] = (struct sockaddr *)cp;
   1895 				if ((i == RTAX_DST || i == RTAX_IFA) &&
   1896 				    nmf == -1)
   1897 					nmf = sa[i]->sa_family;
   1898 				ADVANCE(cp, sa[i]);
   1899 			} else
   1900 				sa[i] = NULL;
   1901 		}
   1902 		for (i = 0; i < RTAX_MAX; i++) {
   1903 			if (sa[i] != NULL) {
   1904 				if (i == RTAX_NETMASK && sa[i]->sa_len)
   1905 					(void)printf(" %s",
   1906 					    netmask_string(sa[i], -1, nmf));
   1907 				else
   1908 					(void)printf(" %s",
   1909 					    routename(sa[i], NULL, RTF_HOST));
   1910 			}
   1911 		}
   1912 	}
   1913 	(void)putchar('\n');
   1914 	(void)fflush(stdout);
   1915 }
   1916 
   1917 static void
   1918 bprintf(FILE *fp, int b, const char *f)
   1919 {
   1920 	int i;
   1921 	int gotsome = 0;
   1922 	const uint8_t *s = (const uint8_t *)f;
   1923 
   1924 	if (b == 0)
   1925 		return;
   1926 	while ((i = *s++) != 0) {
   1927 		if (b & (1 << (i-1))) {
   1928 			if (gotsome == 0)
   1929 				i = '<';
   1930 			else
   1931 				i = ',';
   1932 			(void)putc(i, fp);
   1933 			gotsome = 1;
   1934 			for (; (i = *s) > 32; s++)
   1935 				(void)putc(i, fp);
   1936 		} else
   1937 			while (*s > 32)
   1938 				s++;
   1939 	}
   1940 	if (gotsome)
   1941 		(void)putc('>', fp);
   1942 }
   1943 
   1944 int
   1945 keyword(char *cp)
   1946 {
   1947 	struct keytab *kt = keywords;
   1948 
   1949 	while (kt->kt_cp && strcmp(kt->kt_cp, cp))
   1950 		kt++;
   1951 	return kt->kt_i;
   1952 }
   1953 
   1954 static void
   1955 sodump(sup su, const char *which)
   1956 {
   1957 #ifdef INET6
   1958 	char ntop_buf[NI_MAXHOST];
   1959 #endif
   1960 
   1961 	switch (su->sa.sa_family) {
   1962 	case AF_INET:
   1963 		(void)printf("%s: inet %s; ",
   1964 		    which, inet_ntoa(su->sin.sin_addr));
   1965 		break;
   1966 #ifndef SMALL
   1967 	case AF_APPLETALK:
   1968 		(void)printf("%s: atalk %d.%d; ",
   1969 		    which, su->sat.sat_addr.s_net, su->sat.sat_addr.s_node);
   1970 		break;
   1971 #endif
   1972 	case AF_LINK:
   1973 		(void)printf("%s: link %s; ",
   1974 		    which, link_ntoa(&su->sdl));
   1975 		break;
   1976 #ifdef INET6
   1977 	case AF_INET6:
   1978 		(void)printf("%s: inet6 %s; ",
   1979 		    which, inet_ntop(AF_INET6, &su->sin6.sin6_addr,
   1980 				     ntop_buf, sizeof(ntop_buf)));
   1981 		break;
   1982 #endif
   1983 #ifndef SMALL
   1984 	case AF_ISO:
   1985 		(void)printf("%s: iso %s; ",
   1986 		    which, iso_ntoa(&su->siso.siso_addr));
   1987 		break;
   1988 #endif /* SMALL */
   1989 	default:
   1990 		(void)printf("%s: (%d) %s; ",
   1991 		    which, su->sa.sa_family, any_ntoa(&su->sa));
   1992 	}
   1993 	(void)fflush(stdout);
   1994 }
   1995 
   1996 /* States*/
   1997 #define VIRGIN	0
   1998 #define GOTONE	1
   1999 #define GOTTWO	2
   2000 /* Inputs */
   2001 #define	DIGIT	(4*0)
   2002 #define	END	(4*1)
   2003 #define DELIM	(4*2)
   2004 
   2005 static void
   2006 sockaddr(char *addr, struct sockaddr *sa)
   2007 {
   2008 	char *cp = (char *)sa;
   2009 	int size = sa->sa_len;
   2010 	char *cplim = cp + size;
   2011 	int byte = 0, state = VIRGIN, new = 0;
   2012 
   2013 	(void)memset(cp, 0, size);
   2014 	cp++;
   2015 	do {
   2016 		if ((*addr >= '0') && (*addr <= '9')) {
   2017 			new = *addr - '0';
   2018 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
   2019 			new = *addr - 'a' + 10;
   2020 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
   2021 			new = *addr - 'A' + 10;
   2022 		} else if (*addr == 0)
   2023 			state |= END;
   2024 		else
   2025 			state |= DELIM;
   2026 		addr++;
   2027 		switch (state /* | INPUT */) {
   2028 		case GOTTWO | DIGIT:
   2029 			*cp++ = byte; /*FALLTHROUGH*/
   2030 		case VIRGIN | DIGIT:
   2031 			state = GOTONE; byte = new; continue;
   2032 		case GOTONE | DIGIT:
   2033 			state = GOTTWO; byte = new + (byte << 4); continue;
   2034 		default: /* | DELIM */
   2035 			state = VIRGIN; *cp++ = byte; byte = 0; continue;
   2036 		case GOTONE | END:
   2037 		case GOTTWO | END:
   2038 			*cp++ = byte; /* FALLTHROUGH */
   2039 		case VIRGIN | END:
   2040 			break;
   2041 		}
   2042 		break;
   2043 	} while (cp < cplim);
   2044 	sa->sa_len = cp - (char *)sa;
   2045 }
   2046