10Sstevel@tonic-gate /*
26119Spwernau  *
30Sstevel@tonic-gate  * CDDL HEADER START
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
63055Sdanmcd  * Common Development and Distribution License (the "License").
73055Sdanmcd  * You may not use this file except in compliance with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
235906Svk199839  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <unistd.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <stdarg.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <sys/sysconf.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <sys/socket.h>
390Sstevel@tonic-gate #include <netdb.h>
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <arpa/inet.h>
420Sstevel@tonic-gate #include <net/pfkeyv2.h>
430Sstevel@tonic-gate #include <net/pfpolicy.h>
440Sstevel@tonic-gate #include <libintl.h>
450Sstevel@tonic-gate #include <setjmp.h>
460Sstevel@tonic-gate #include <libgen.h>
474235Smarkfen #include <libscf.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include "ipsec_util.h"
500Sstevel@tonic-gate #include "ikedoor.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * This file contains support functions that are shared by the ipsec
544867Spwernau  * utilities and daemons including ipseckey(1m), ikeadm(1m) and in.iked(1m).
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
574867Spwernau 
584867Spwernau #define	EFD(file) (((file) == stdout) ? stderr : (file))
594867Spwernau 
600Sstevel@tonic-gate /* Set standard default/initial values for globals... */
610Sstevel@tonic-gate boolean_t pflag = B_FALSE;	/* paranoid w.r.t. printing keying material */
620Sstevel@tonic-gate boolean_t nflag = B_FALSE;	/* avoid nameservice? */
630Sstevel@tonic-gate boolean_t interactive = B_FALSE;	/* util not running on cmdline */
640Sstevel@tonic-gate boolean_t readfile = B_FALSE;	/* cmds are being read from a file */
650Sstevel@tonic-gate uint_t	lineno = 0;		/* track location if reading cmds from file */
664235Smarkfen uint_t	lines_added = 0;
674235Smarkfen uint_t	lines_parsed = 0;
680Sstevel@tonic-gate jmp_buf	env;		/* for error recovery in interactive/readfile modes */
694235Smarkfen char *my_fmri = NULL;
704235Smarkfen FILE *debugfile = stderr;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Print errno and exit if cmdline or readfile, reset state if interactive
744064Smarkfen  * The error string *what should be dgettext()'d before calling bail().
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate void
770Sstevel@tonic-gate bail(char *what)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	if (errno != 0)
800Sstevel@tonic-gate 		warn(what);
810Sstevel@tonic-gate 	else
824235Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
830Sstevel@tonic-gate 	if (readfile) {
844235Smarkfen 		return;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	if (interactive && !readfile)
870Sstevel@tonic-gate 		longjmp(env, 2);
884235Smarkfen 	EXIT_FATAL(NULL);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Print caller-supplied variable-arg error msg, then exit if cmdline or
930Sstevel@tonic-gate  * readfile, or reset state if interactive.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate /*PRINTFLIKE1*/
960Sstevel@tonic-gate void
970Sstevel@tonic-gate bail_msg(char *fmt, ...)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	va_list	ap;
1000Sstevel@tonic-gate 	char	msgbuf[BUFSIZ];
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	va_start(ap, fmt);
1030Sstevel@tonic-gate 	(void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
1040Sstevel@tonic-gate 	va_end(ap);
1050Sstevel@tonic-gate 	if (readfile)
1064064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
1074064Smarkfen 		    "ERROR on line %u:\n%s\n"), lineno,  msgbuf);
1080Sstevel@tonic-gate 	else
1094064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (interactive && !readfile)
1120Sstevel@tonic-gate 		longjmp(env, 1);
1130Sstevel@tonic-gate 
1144235Smarkfen 	EXIT_FATAL(NULL);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * dump_XXX functions produce ASCII output from various structures.
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * Because certain errors need to do this to stderr, dump_XXX functions
1220Sstevel@tonic-gate  * take a FILE pointer.
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  * If an error occured while writing to the specified file, these
1250Sstevel@tonic-gate  * functions return -1, zero otherwise.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate int
1293055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
1304867Spwernau     FILE *where, boolean_t ignore_nss)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	struct sockaddr_in	*sin;
1330Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
1340Sstevel@tonic-gate 	char			*printable_addr, *protocol;
1350Sstevel@tonic-gate 	uint8_t			*addrptr;
1363055Sdanmcd 	/* Add 4 chars to hold '/nnn' for prefixes. */
1373055Sdanmcd 	char			storage[INET6_ADDRSTRLEN + 4];
1380Sstevel@tonic-gate 	uint16_t		port;
1390Sstevel@tonic-gate 	boolean_t		unspec;
1400Sstevel@tonic-gate 	struct hostent		*hp;
1410Sstevel@tonic-gate 	int			getipnode_errno, addrlen;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	switch (sa->sa_family) {
1440Sstevel@tonic-gate 	case AF_INET:
1450Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1460Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
1470Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin->sin_addr;
1480Sstevel@tonic-gate 		port = sin->sin_port;
1490Sstevel@tonic-gate 		protocol = "AF_INET";
1500Sstevel@tonic-gate 		unspec = (sin->sin_addr.s_addr == 0);
1510Sstevel@tonic-gate 		addrlen = sizeof (sin->sin_addr);
1520Sstevel@tonic-gate 		break;
1530Sstevel@tonic-gate 	case AF_INET6:
1540Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1550Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
1560Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin6->sin6_addr;
1570Sstevel@tonic-gate 		port = sin6->sin6_port;
1580Sstevel@tonic-gate 		protocol = "AF_INET6";
1590Sstevel@tonic-gate 		unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
1600Sstevel@tonic-gate 		addrlen = sizeof (sin6->sin6_addr);
1610Sstevel@tonic-gate 		break;
1620Sstevel@tonic-gate 	default:
1630Sstevel@tonic-gate 		return (0);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
1670Sstevel@tonic-gate 	    NULL) {
1684064Smarkfen 		printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
1690Sstevel@tonic-gate 	} else {
1703055Sdanmcd 		char prefix[5];	/* "/nnn" with terminator. */
1713055Sdanmcd 
1723055Sdanmcd 		(void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
1730Sstevel@tonic-gate 		printable_addr = storage;
1743055Sdanmcd 		if (prefixlen != 0) {
1753055Sdanmcd 			(void) strlcat(printable_addr, prefix,
1763055Sdanmcd 			    sizeof (storage));
1773055Sdanmcd 		}
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 	if (addr_only) {
1800Sstevel@tonic-gate 		if (fprintf(where, "%s", printable_addr) < 0)
1810Sstevel@tonic-gate 			return (-1);
1820Sstevel@tonic-gate 	} else {
1834064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
1844064Smarkfen 		    "%s: port %d, %s"), protocol,
1850Sstevel@tonic-gate 		    ntohs(port), printable_addr) < 0)
1860Sstevel@tonic-gate 			return (-1);
1874867Spwernau 		if (ignore_nss == B_FALSE) {
1880Sstevel@tonic-gate 			/*
1890Sstevel@tonic-gate 			 * Do AF_independent reverse hostname lookup here.
1900Sstevel@tonic-gate 			 */
1910Sstevel@tonic-gate 			if (unspec) {
1920Sstevel@tonic-gate 				if (fprintf(where,
1934064Smarkfen 				    dgettext(TEXT_DOMAIN,
1944064Smarkfen 				    " <unspecified>")) < 0)
1950Sstevel@tonic-gate 					return (-1);
1960Sstevel@tonic-gate 			} else {
1970Sstevel@tonic-gate 				hp = getipnodebyaddr((char *)addrptr, addrlen,
1980Sstevel@tonic-gate 				    sa->sa_family, &getipnode_errno);
1990Sstevel@tonic-gate 				if (hp != NULL) {
2000Sstevel@tonic-gate 					if (fprintf(where,
2010Sstevel@tonic-gate 					    " (%s)", hp->h_name) < 0)
2020Sstevel@tonic-gate 						return (-1);
2030Sstevel@tonic-gate 					freehostent(hp);
2040Sstevel@tonic-gate 				} else {
2050Sstevel@tonic-gate 					if (fprintf(where,
2064064Smarkfen 					    dgettext(TEXT_DOMAIN,
2074064Smarkfen 					    " <unknown>")) < 0)
2080Sstevel@tonic-gate 						return (-1);
2090Sstevel@tonic-gate 				}
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 		if (fputs(".\n", where) == EOF)
2130Sstevel@tonic-gate 			return (-1);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	return (0);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * Dump a key and bitlen
2200Sstevel@tonic-gate  */
2210Sstevel@tonic-gate int
2220Sstevel@tonic-gate dump_key(uint8_t *keyp, uint_t bitlen, FILE *where)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	int	numbytes;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	numbytes = SADB_1TO8(bitlen);
2270Sstevel@tonic-gate 	/* The & 0x7 is to check for leftover bits. */
2280Sstevel@tonic-gate 	if ((bitlen & 0x7) != 0)
2290Sstevel@tonic-gate 		numbytes++;
2300Sstevel@tonic-gate 	while (numbytes-- != 0) {
2310Sstevel@tonic-gate 		if (pflag) {
2320Sstevel@tonic-gate 			/* Print no keys if paranoid */
2330Sstevel@tonic-gate 			if (fprintf(where, "XX") < 0)
2340Sstevel@tonic-gate 				return (-1);
2350Sstevel@tonic-gate 		} else {
2360Sstevel@tonic-gate 			if (fprintf(where, "%02x", *keyp++) < 0)
2370Sstevel@tonic-gate 				return (-1);
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 	if (fprintf(where, "/%u", bitlen) < 0)
2410Sstevel@tonic-gate 		return (-1);
2420Sstevel@tonic-gate 	return (0);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Print an authentication or encryption algorithm
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate static int
2490Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	struct ipsecalgent *alg;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_num, proto_num, NULL);
2540Sstevel@tonic-gate 	if (alg == NULL) {
2554064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
2564064Smarkfen 		    "<unknown %u>"), alg_num) < 0)
2570Sstevel@tonic-gate 			return (-1);
2580Sstevel@tonic-gate 		return (0);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * Special-case <none> for backward output compat.
2630Sstevel@tonic-gate 	 * Assume that SADB_AALG_NONE == SADB_EALG_NONE.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if (alg_num == SADB_AALG_NONE) {
2664064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
2674064Smarkfen 		    "<none>"), where) == EOF)
2680Sstevel@tonic-gate 			return (-1);
2690Sstevel@tonic-gate 	} else {
2700Sstevel@tonic-gate 		if (fputs(alg->a_names[0], where) == EOF)
2710Sstevel@tonic-gate 			return (-1);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	freeipsecalgent(alg);
2750Sstevel@tonic-gate 	return (0);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate int
2790Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate int
2850Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Print an SADB_IDENTTYPE string
2920Sstevel@tonic-gate  *
2930Sstevel@tonic-gate  * Also return TRUE if the actual ident may be printed, FALSE if not.
2940Sstevel@tonic-gate  *
2950Sstevel@tonic-gate  * If rc is not NULL, set its value to -1 if an error occured while writing
2960Sstevel@tonic-gate  * to the specified file, zero otherwise.
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate boolean_t
2990Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	boolean_t canprint = B_TRUE;
3020Sstevel@tonic-gate 	int rc_val = 0;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	switch (idtype) {
3050Sstevel@tonic-gate 	case SADB_IDENTTYPE_PREFIX:
3064064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
3070Sstevel@tonic-gate 			rc_val = -1;
3080Sstevel@tonic-gate 		break;
3090Sstevel@tonic-gate 	case SADB_IDENTTYPE_FQDN:
3104064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
3110Sstevel@tonic-gate 			rc_val = -1;
3120Sstevel@tonic-gate 		break;
3130Sstevel@tonic-gate 	case SADB_IDENTTYPE_USER_FQDN:
3144064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
3154064Smarkfen 		    "user-FQDN (mbox)"), where) == EOF)
3160Sstevel@tonic-gate 			rc_val = -1;
3170Sstevel@tonic-gate 		break;
3180Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_DN:
3194064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
3200Sstevel@tonic-gate 		    where) == EOF)
3210Sstevel@tonic-gate 			rc_val = -1;
3220Sstevel@tonic-gate 		canprint = B_FALSE;
3230Sstevel@tonic-gate 		break;
3240Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_GN:
3254064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
3264064Smarkfen 		    where) == EOF)
3270Sstevel@tonic-gate 			rc_val = -1;
3280Sstevel@tonic-gate 		canprint = B_FALSE;
3290Sstevel@tonic-gate 		break;
3300Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_KEY_ID:
3314064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
3324064Smarkfen 		    where) == EOF)
3330Sstevel@tonic-gate 			rc_val = -1;
3340Sstevel@tonic-gate 		break;
3350Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_ADDR_RANGE:
3364064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
3370Sstevel@tonic-gate 			rc_val = -1;
3380Sstevel@tonic-gate 		break;
3390Sstevel@tonic-gate 	default:
3404064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
3414064Smarkfen 		    "<unknown %u>"), idtype) < 0)
3420Sstevel@tonic-gate 			rc_val = -1;
3430Sstevel@tonic-gate 		break;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (rc != NULL)
3470Sstevel@tonic-gate 		*rc = rc_val;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	return (canprint);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate  * Slice an argv/argc vector from an interactive line or a read-file line.
3540Sstevel@tonic-gate  */
3550Sstevel@tonic-gate static int
3560Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	unsigned int argvlen = START_ARG;
3590Sstevel@tonic-gate 	char **current;
3600Sstevel@tonic-gate 	boolean_t firstchar = B_TRUE;
3610Sstevel@tonic-gate 	boolean_t inquotes = B_FALSE;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	*thisargv = malloc(sizeof (char *) * argvlen);
3640Sstevel@tonic-gate 	if ((*thisargv) == NULL)
3650Sstevel@tonic-gate 		return (MEMORY_ALLOCATION);
3660Sstevel@tonic-gate 	current = *thisargv;
3670Sstevel@tonic-gate 	*current = NULL;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	for (; *ibuf != '\0'; ibuf++) {
3700Sstevel@tonic-gate 		if (isspace(*ibuf)) {
3710Sstevel@tonic-gate 			if (inquotes) {
3720Sstevel@tonic-gate 				continue;
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 			if (*current != NULL) {
3750Sstevel@tonic-gate 				*ibuf = '\0';
3760Sstevel@tonic-gate 				current++;
3770Sstevel@tonic-gate 				if (*thisargv + argvlen == current) {
3780Sstevel@tonic-gate 					/* Regrow ***thisargv. */
3790Sstevel@tonic-gate 					if (argvlen == TOO_MANY_ARGS) {
3800Sstevel@tonic-gate 						free(*thisargv);
3810Sstevel@tonic-gate 						return (TOO_MANY_TOKENS);
3820Sstevel@tonic-gate 					}
3830Sstevel@tonic-gate 					/* Double the allocation. */
3840Sstevel@tonic-gate 					current = realloc(*thisargv,
3850Sstevel@tonic-gate 					    sizeof (char *) * (argvlen << 1));
3860Sstevel@tonic-gate 					if (current == NULL) {
3870Sstevel@tonic-gate 						free(*thisargv);
3880Sstevel@tonic-gate 						return (MEMORY_ALLOCATION);
3890Sstevel@tonic-gate 					}
3900Sstevel@tonic-gate 					*thisargv = current;
3910Sstevel@tonic-gate 					current += argvlen;
3920Sstevel@tonic-gate 					argvlen <<= 1;	/* Double the size. */
3930Sstevel@tonic-gate 				}
3940Sstevel@tonic-gate 				*current = NULL;
3950Sstevel@tonic-gate 			}
3960Sstevel@tonic-gate 		} else {
3970Sstevel@tonic-gate 			if (firstchar) {
3980Sstevel@tonic-gate 				firstchar = B_FALSE;
3994235Smarkfen 				if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
4000Sstevel@tonic-gate 					free(*thisargv);
4010Sstevel@tonic-gate 					return (COMMENT_LINE);
4020Sstevel@tonic-gate 				}
4030Sstevel@tonic-gate 			}
4040Sstevel@tonic-gate 			if (*ibuf == QUOTE_CHAR) {
4050Sstevel@tonic-gate 				if (inquotes) {
4060Sstevel@tonic-gate 					inquotes = B_FALSE;
4070Sstevel@tonic-gate 					*ibuf = '\0';
4080Sstevel@tonic-gate 				} else {
4090Sstevel@tonic-gate 					inquotes = B_TRUE;
4100Sstevel@tonic-gate 				}
4110Sstevel@tonic-gate 				continue;
4120Sstevel@tonic-gate 			}
4130Sstevel@tonic-gate 			if (*current == NULL) {
4140Sstevel@tonic-gate 				*current = ibuf;
4150Sstevel@tonic-gate 				(*newargc)++;
4160Sstevel@tonic-gate 			}
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * Tricky corner case...
4220Sstevel@tonic-gate 	 * I've parsed _exactly_ the amount of args as I have space.  It
4230Sstevel@tonic-gate 	 * won't return NULL-terminated, and bad things will happen to
4240Sstevel@tonic-gate 	 * the caller.
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 	if (argvlen == *newargc) {
4270Sstevel@tonic-gate 		current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
4280Sstevel@tonic-gate 		if (current == NULL) {
4290Sstevel@tonic-gate 			free(*thisargv);
4300Sstevel@tonic-gate 			return (MEMORY_ALLOCATION);
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 		*thisargv = current;
4330Sstevel@tonic-gate 		current[argvlen] = NULL;
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	return (SUCCESS);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4400Sstevel@tonic-gate  * Enter a mode where commands are read from a file.  Treat stdin special.
4410Sstevel@tonic-gate  */
4420Sstevel@tonic-gate void
4434235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring,
4444235Smarkfen     char *my_fmri, parse_cmdln_fn parseit)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	char		ibuf[IBUF_SIZE], holder[IBUF_SIZE];
4474235Smarkfen 	char		*hptr, **thisargv, *ebuf;
4480Sstevel@tonic-gate 	int		thisargc;
4490Sstevel@tonic-gate 	boolean_t	continue_in_progress = B_FALSE;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	(void) setjmp(env);
4520Sstevel@tonic-gate 
4534235Smarkfen 	ebuf = NULL;
4540Sstevel@tonic-gate 	interactive = B_TRUE;
4550Sstevel@tonic-gate 	bzero(ibuf, IBUF_SIZE);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (infile == stdin) {
4580Sstevel@tonic-gate 		(void) printf("%s", promptstring);
4590Sstevel@tonic-gate 		(void) fflush(stdout);
4600Sstevel@tonic-gate 	} else {
4610Sstevel@tonic-gate 		readfile = B_TRUE;
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	while (fgets(ibuf, IBUF_SIZE, infile) != NULL) {
4650Sstevel@tonic-gate 		if (readfile)
4660Sstevel@tonic-gate 			lineno++;
4670Sstevel@tonic-gate 		thisargc = 0;
4680Sstevel@tonic-gate 		thisargv = NULL;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		/*
4710Sstevel@tonic-gate 		 * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
4720Sstevel@tonic-gate 		 * be null-terminated because of fgets().
4730Sstevel@tonic-gate 		 */
4740Sstevel@tonic-gate 		if (ibuf[IBUF_SIZE - 2] != '\0') {
4754235Smarkfen 			ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile,
4764235Smarkfen 			    dgettext(TEXT_DOMAIN, "Line %d too big."), lineno);
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		if (!continue_in_progress) {
4800Sstevel@tonic-gate 			/* Use -2 because of \n from fgets. */
4810Sstevel@tonic-gate 			if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
4820Sstevel@tonic-gate 				/*
4830Sstevel@tonic-gate 				 * Can use strcpy here, I've checked the
4840Sstevel@tonic-gate 				 * length already.
4850Sstevel@tonic-gate 				 */
4860Sstevel@tonic-gate 				(void) strcpy(holder, ibuf);
4870Sstevel@tonic-gate 				hptr = &(holder[strlen(holder)]);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
4900Sstevel@tonic-gate 				hptr[-2] = ' ';
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 				continue_in_progress = B_TRUE;
4930Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
4940Sstevel@tonic-gate 				continue;
4950Sstevel@tonic-gate 			}
4960Sstevel@tonic-gate 		} else {
4970Sstevel@tonic-gate 			/* Handle continuations... */
4980Sstevel@tonic-gate 			(void) strncpy(hptr, ibuf,
4990Sstevel@tonic-gate 			    (size_t)(&(holder[IBUF_SIZE]) - hptr));
5000Sstevel@tonic-gate 			if (holder[IBUF_SIZE - 1] != '\0') {
5014235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
5024235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
5034235Smarkfen 				    "Command buffer overrun."));
5040Sstevel@tonic-gate 			}
5050Sstevel@tonic-gate 			/* Use - 2 because of \n from fgets. */
5060Sstevel@tonic-gate 			if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
5070Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
5080Sstevel@tonic-gate 				hptr += strlen(hptr);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
5110Sstevel@tonic-gate 				hptr[-2] = ' ';
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 				continue;
5140Sstevel@tonic-gate 			} else {
5150Sstevel@tonic-gate 				continue_in_progress = B_FALSE;
5160Sstevel@tonic-gate 				/*
5170Sstevel@tonic-gate 				 * I've already checked the length...
5180Sstevel@tonic-gate 				 */
5190Sstevel@tonic-gate 				(void) strcpy(ibuf, holder);
5200Sstevel@tonic-gate 			}
5210Sstevel@tonic-gate 		}
5220Sstevel@tonic-gate 
5234235Smarkfen 		/*
5244235Smarkfen 		 * Just in case the command fails keep a copy of the
5254235Smarkfen 		 * command buffer for diagnostic output.
5264235Smarkfen 		 */
5274235Smarkfen 		if (readfile) {
5284235Smarkfen 			/*
5294235Smarkfen 			 * The error buffer needs to be big enough to
5304235Smarkfen 			 * hold the longest command string, plus
5314235Smarkfen 			 * some extra text, see below.
5324235Smarkfen 			 */
5334235Smarkfen 			ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
5344235Smarkfen 			if (ebuf == NULL) {
5354235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
5364235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
5374235Smarkfen 				    "Memory allocation error."));
5384235Smarkfen 			} else {
5394235Smarkfen 				(void) snprintf(ebuf, (IBUF_SIZE * 2),
5404235Smarkfen 				    dgettext(TEXT_DOMAIN,
5414235Smarkfen 				    "Config file entry near line %u "
5424235Smarkfen 				    "caused error(s) or warnings:\n\n%s\n\n"),
5434235Smarkfen 				    lineno, ibuf);
5444235Smarkfen 			}
5454235Smarkfen 		}
5464235Smarkfen 
5470Sstevel@tonic-gate 		switch (create_argv(ibuf, &thisargc, &thisargv)) {
5480Sstevel@tonic-gate 		case TOO_MANY_TOKENS:
5494235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5504235Smarkfen 			    dgettext(TEXT_DOMAIN, "Too many input tokens."));
5510Sstevel@tonic-gate 			break;
5520Sstevel@tonic-gate 		case MEMORY_ALLOCATION:
5534235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5544235Smarkfen 			    dgettext(TEXT_DOMAIN, "Memory allocation error."));
5550Sstevel@tonic-gate 			break;
5560Sstevel@tonic-gate 		case COMMENT_LINE:
5570Sstevel@tonic-gate 			/* Comment line. */
5584235Smarkfen 			free(ebuf);
5590Sstevel@tonic-gate 			break;
5600Sstevel@tonic-gate 		default:
5614235Smarkfen 			if (thisargc != 0) {
5624235Smarkfen 				lines_parsed++;
5634235Smarkfen 				/* ebuf consumed */
5644342Spwernau 				parseit(thisargc, thisargv, ebuf, readfile);
5654235Smarkfen 			} else {
5664235Smarkfen 				free(ebuf);
5674235Smarkfen 			}
5680Sstevel@tonic-gate 			free(thisargv);
5690Sstevel@tonic-gate 			if (infile == stdin) {
5700Sstevel@tonic-gate 				(void) printf("%s", promptstring);
5710Sstevel@tonic-gate 				(void) fflush(stdout);
5720Sstevel@tonic-gate 			}
5730Sstevel@tonic-gate 			break;
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 		bzero(ibuf, IBUF_SIZE);
5760Sstevel@tonic-gate 	}
5774342Spwernau 
5784342Spwernau 	/*
5794342Spwernau 	 * The following code is ipseckey specific. This should never be
5804342Spwernau 	 * used by ikeadm which also calls this function because ikeadm
5814342Spwernau 	 * only runs interactively. If this ever changes this code block
5824342Spwernau 	 * sould be revisited.
5834342Spwernau 	 */
5844342Spwernau 	if (readfile) {
5854342Spwernau 		if (lines_parsed != 0 && lines_added == 0) {
5864342Spwernau 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5874342Spwernau 			    dgettext(TEXT_DOMAIN, "Configuration file did not "
5884342Spwernau 			    "contain any valid SAs"));
5894342Spwernau 		}
5904342Spwernau 
5914342Spwernau 		/*
5924342Spwernau 		 * There were errors. Putting the service in maintenance mode.
5934342Spwernau 		 * When svc.startd(1M) allows services to degrade themselves,
5944342Spwernau 		 * this should be revisited.
5954342Spwernau 		 *
5964342Spwernau 		 * If this function was called from a program running as a
5974342Spwernau 		 * smf_method(5), print a warning message. Don't spew out the
5984342Spwernau 		 * errors as these will end up in the smf(5) log file which is
5994342Spwernau 		 * publically readable, the errors may contain sensitive
6004342Spwernau 		 * information.
6014342Spwernau 		 */
6024342Spwernau 		if ((lines_added < lines_parsed) && (configfile != NULL)) {
6034342Spwernau 			if (my_fmri != NULL) {
6044342Spwernau 				ipsecutil_exit(SERVICE_BADCONF, my_fmri,
6054342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
6064342Spwernau 				    "The configuration file contained %d "
6074342Spwernau 				    "errors.\n"
6084342Spwernau 				    "Manually check the configuration with:\n"
6094342Spwernau 				    "ipseckey -c %s\n"
6104342Spwernau 				    "Use svcadm(1M) to clear maintenance "
6114342Spwernau 				    "condition when errors are resolved.\n"),
6124342Spwernau 				    lines_parsed - lines_added, configfile);
6134342Spwernau 			} else {
6144342Spwernau 				EXIT_BADCONFIG(NULL);
6154342Spwernau 			}
6164342Spwernau 		} else {
6174342Spwernau 			if (my_fmri != NULL)
6184342Spwernau 				ipsecutil_exit(SERVICE_EXIT_OK, my_fmri,
6194342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
6204342Spwernau 				    "%d actions successfully processed."),
6214342Spwernau 				    lines_added);
6224342Spwernau 		}
6234342Spwernau 	} else {
6240Sstevel@tonic-gate 		(void) putchar('\n');
6250Sstevel@tonic-gate 		(void) fflush(stdout);
6260Sstevel@tonic-gate 	}
6274235Smarkfen 	EXIT_OK(NULL);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  * Functions to parse strings that represent a debug or privilege level.
6320Sstevel@tonic-gate  * These functions are copied from main.c and door.c in usr.lib/in.iked/common.
6330Sstevel@tonic-gate  * If this file evolves into a common library that may be used by in.iked
6340Sstevel@tonic-gate  * as well as the usr.sbin utilities, those duplicate functions should be
6350Sstevel@tonic-gate  * deleted.
6360Sstevel@tonic-gate  *
6370Sstevel@tonic-gate  * A privilege level may be represented by a simple keyword, corresponding
6380Sstevel@tonic-gate  * to one of the possible levels.  A debug level may be represented by a
6390Sstevel@tonic-gate  * series of keywords, separated by '+' or '-', indicating categories to
6400Sstevel@tonic-gate  * be added or removed from the set of categories in the debug level.
6410Sstevel@tonic-gate  * For example, +all-op corresponds to level 0xfffffffb (all flags except
6420Sstevel@tonic-gate  * for D_OP set); while p1+p2+pfkey corresponds to level 0x38.  Note that
6430Sstevel@tonic-gate  * the leading '+' is implicit; the first keyword in the list must be for
6440Sstevel@tonic-gate  * a category that is to be added.
6450Sstevel@tonic-gate  *
6460Sstevel@tonic-gate  * These parsing functions make use of a local version of strtok, strtok_d,
6470Sstevel@tonic-gate  * which includes an additional parameter, char *delim.  This param is filled
6480Sstevel@tonic-gate  * in with the character which ends the returned token.  In other words,
6490Sstevel@tonic-gate  * this version of strtok, in addition to returning the token, also returns
6500Sstevel@tonic-gate  * the single character delimiter from the original string which marked the
6510Sstevel@tonic-gate  * end of the token.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate static char *
6540Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	static char	*lasts;
6570Sstevel@tonic-gate 	char		*q, *r;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/* first or subsequent call */
6600Sstevel@tonic-gate 	if (string == NULL)
6610Sstevel@tonic-gate 		string = lasts;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
6640Sstevel@tonic-gate 		return (NULL);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	q = string + strspn(string, sepset);	/* skip leading separators */
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (*q == '\0')			/* return if no tokens remaining */
6690Sstevel@tonic-gate 		return (NULL);
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL) {		/* move past token */
6720Sstevel@tonic-gate 		lasts = 0;	/* indicate that this is last token */
6730Sstevel@tonic-gate 	} else {
6740Sstevel@tonic-gate 		*delim = *r;	/* save delimitor */
6750Sstevel@tonic-gate 		*r = '\0';
6760Sstevel@tonic-gate 		lasts = r + 1;
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 	return (q);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate static keywdtab_t	privtab[] = {
6820Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"base" },
6830Sstevel@tonic-gate 	{ IKE_PRIV_MODKEYS,	"modkeys" },
6840Sstevel@tonic-gate 	{ IKE_PRIV_KEYMAT,	"keymat" },
6850Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"0" },
6860Sstevel@tonic-gate };
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate int
6890Sstevel@tonic-gate privstr2num(char *str)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	keywdtab_t	*pp;
6920Sstevel@tonic-gate 	char		*endp;
6930Sstevel@tonic-gate 	int		 priv;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	for (pp = privtab; pp < A_END(privtab); pp++) {
6960Sstevel@tonic-gate 		if (strcasecmp(str, pp->kw_str) == 0)
6970Sstevel@tonic-gate 			return (pp->kw_tag);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	priv = strtol(str, &endp, 0);
7010Sstevel@tonic-gate 	if (*endp == '\0')
7020Sstevel@tonic-gate 		return (priv);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	return (-1);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate static keywdtab_t	dbgtab[] = {
7080Sstevel@tonic-gate 	{ D_CERT,	"cert" },
7090Sstevel@tonic-gate 	{ D_KEY,	"key" },
7100Sstevel@tonic-gate 	{ D_OP,		"op" },
7110Sstevel@tonic-gate 	{ D_P1,		"p1" },
7120Sstevel@tonic-gate 	{ D_P1,		"phase1" },
7130Sstevel@tonic-gate 	{ D_P2,		"p2" },
7140Sstevel@tonic-gate 	{ D_P2,		"phase2" },
7150Sstevel@tonic-gate 	{ D_PFKEY,	"pfkey" },
7160Sstevel@tonic-gate 	{ D_POL,	"pol" },
7170Sstevel@tonic-gate 	{ D_POL,	"policy" },
7180Sstevel@tonic-gate 	{ D_PROP,	"prop" },
7190Sstevel@tonic-gate 	{ D_DOOR,	"door" },
7200Sstevel@tonic-gate 	{ D_CONFIG,	"config" },
7210Sstevel@tonic-gate 	{ D_ALL,	"all" },
7220Sstevel@tonic-gate 	{ 0,		"0" },
7230Sstevel@tonic-gate };
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate int
7260Sstevel@tonic-gate dbgstr2num(char *str)
7270Sstevel@tonic-gate {
7280Sstevel@tonic-gate 	keywdtab_t	*dp;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
7310Sstevel@tonic-gate 		if (strcasecmp(str, dp->kw_str) == 0)
7320Sstevel@tonic-gate 			return (dp->kw_tag);
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 	return (D_INVALID);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate int
7380Sstevel@tonic-gate parsedbgopts(char *optarg)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	char	*argp, *endp, op, nextop;
7410Sstevel@tonic-gate 	int	mask = 0, new;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	mask = strtol(optarg, &endp, 0);
7440Sstevel@tonic-gate 	if (*endp == '\0')
7450Sstevel@tonic-gate 		return (mask);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	op = optarg[0];
7480Sstevel@tonic-gate 	if (op != '-')
7490Sstevel@tonic-gate 		op = '+';
7500Sstevel@tonic-gate 	argp = strtok_d(optarg, "+-", &nextop);
7510Sstevel@tonic-gate 	do {
7520Sstevel@tonic-gate 		new = dbgstr2num(argp);
7530Sstevel@tonic-gate 		if (new == D_INVALID) {
7540Sstevel@tonic-gate 			/* we encountered an invalid keywd */
7550Sstevel@tonic-gate 			return (new);
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 		if (op == '+') {
7580Sstevel@tonic-gate 			mask |= new;
7590Sstevel@tonic-gate 		} else {
7600Sstevel@tonic-gate 			mask &= ~new;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 		op = nextop;
7630Sstevel@tonic-gate 	} while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	return (mask);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate  * functions to manipulate the kmcookie-label mapping file
7710Sstevel@tonic-gate  */
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * Open, lockf, fdopen the given file, returning a FILE * on success,
7750Sstevel@tonic-gate  * or NULL on failure.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate FILE *
7780Sstevel@tonic-gate kmc_open_and_lock(char *name)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	int	fd, rtnerr;
7810Sstevel@tonic-gate 	FILE	*fp;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
7840Sstevel@tonic-gate 		return (NULL);
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 	if (lockf(fd, F_LOCK, 0) < 0) {
7870Sstevel@tonic-gate 		return (NULL);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	if ((fp = fdopen(fd, "a+")) == NULL) {
7900Sstevel@tonic-gate 		return (NULL);
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 	if (fseek(fp, 0, SEEK_SET) < 0) {
7930Sstevel@tonic-gate 		/* save errno in case fclose changes it */
7940Sstevel@tonic-gate 		rtnerr = errno;
7950Sstevel@tonic-gate 		(void) fclose(fp);
7960Sstevel@tonic-gate 		errno = rtnerr;
7970Sstevel@tonic-gate 		return (NULL);
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 	return (fp);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate  * Extract an integer cookie and string label from a line from the
8040Sstevel@tonic-gate  * kmcookie-label file.  Return -1 on failure, 0 on success.
8050Sstevel@tonic-gate  */
8060Sstevel@tonic-gate int
8070Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	char	*cookiestr;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	*cookie = 0;
8120Sstevel@tonic-gate 	*label = NULL;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	cookiestr = strtok(line, " \t\n");
8150Sstevel@tonic-gate 	if (cookiestr == NULL) {
8160Sstevel@tonic-gate 		return (-1);
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/* Everything that follows, up to the newline, is the label. */
8200Sstevel@tonic-gate 	*label = strtok(NULL, "\n");
8210Sstevel@tonic-gate 	if (*label == NULL) {
8220Sstevel@tonic-gate 		return (-1);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	*cookie = atoi(cookiestr);
8260Sstevel@tonic-gate 	return (0);
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate  * Insert a mapping into the file (if it's not already there), given the
8310Sstevel@tonic-gate  * new label.  Return the assigned cookie, or -1 on error.
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate int
8340Sstevel@tonic-gate kmc_insert_mapping(char *label)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	FILE	*map;
8374757Sdanmcd 	char	linebuf[IBUF_SIZE];
8380Sstevel@tonic-gate 	char	*cur_label;
8390Sstevel@tonic-gate 	int	max_cookie = 0, cur_cookie, rtn_cookie;
8400Sstevel@tonic-gate 	int	rtnerr = 0;
8410Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/* open and lock the file; will sleep until lock is available */
8440Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
8450Sstevel@tonic-gate 		/* kmc_open_and_lock() sets errno appropriately */
8460Sstevel@tonic-gate 		return (-1);
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
8500Sstevel@tonic-gate 
8514757Sdanmcd 		/* Skip blank lines, which often come near EOF. */
8524757Sdanmcd 		if (strlen(linebuf) == 0)
8534757Sdanmcd 			continue;
8544757Sdanmcd 
8550Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
8560Sstevel@tonic-gate 			rtnerr = EINVAL;
8570Sstevel@tonic-gate 			goto error;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		if (cur_cookie > max_cookie)
8610Sstevel@tonic-gate 			max_cookie = cur_cookie;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		if ((!found) && (strcmp(cur_label, label) == 0)) {
8640Sstevel@tonic-gate 			found = B_TRUE;
8650Sstevel@tonic-gate 			rtn_cookie = cur_cookie;
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	if (!found) {
8700Sstevel@tonic-gate 		rtn_cookie = ++max_cookie;
8710Sstevel@tonic-gate 		if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
8720Sstevel@tonic-gate 		    (fflush(map) < 0)) {
8730Sstevel@tonic-gate 			rtnerr = errno;
8740Sstevel@tonic-gate 			goto error;
8750Sstevel@tonic-gate 		}
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 	(void) fclose(map);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	return (rtn_cookie);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate error:
8820Sstevel@tonic-gate 	(void) fclose(map);
8830Sstevel@tonic-gate 	errno = rtnerr;
8840Sstevel@tonic-gate 	return (-1);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate  * Lookup the given cookie and return its corresponding label.  Return
8890Sstevel@tonic-gate  * a pointer to the label on success, NULL on error (or if the label is
8900Sstevel@tonic-gate  * not found).  Note that the returned label pointer points to a static
8910Sstevel@tonic-gate  * string, so the label will be overwritten by a subsequent call to the
8920Sstevel@tonic-gate  * function; the function is also not thread-safe as a result.
8930Sstevel@tonic-gate  */
8940Sstevel@tonic-gate char *
8950Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	FILE		*map;
8984757Sdanmcd 	static char	linebuf[IBUF_SIZE];
8990Sstevel@tonic-gate 	char		*cur_label;
9000Sstevel@tonic-gate 	int		cur_cookie;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
9030Sstevel@tonic-gate 		return (NULL);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
9090Sstevel@tonic-gate 			(void) fclose(map);
9100Sstevel@tonic-gate 			return (NULL);
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		if (cookie == cur_cookie) {
9140Sstevel@tonic-gate 			(void) fclose(map);
9150Sstevel@tonic-gate 			return (cur_label);
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 	(void) fclose(map);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	return (NULL);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate /*
9240Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
9250Sstevel@tonic-gate  * Return values include:
9260Sstevel@tonic-gate  *
9270Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
9280Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
9290Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
9300Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
9310Sstevel@tonic-gate  *		extv[0].
9320Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
9330Sstevel@tonic-gate  *		unknown header.
9340Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
9350Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
9360Sstevel@tonic-gate  *
9370Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
9380Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
9390Sstevel@tonic-gate  */
9400Sstevel@tonic-gate int
9410Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
9420Sstevel@tonic-gate     char *diag_buf, uint_t diag_buf_len)
9430Sstevel@tonic-gate {
9440Sstevel@tonic-gate 	int i;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (diag_buf != NULL)
9470Sstevel@tonic-gate 		diag_buf[0] = '\0';
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	for (i = 1; i <= SPD_EXT_MAX; i++)
9500Sstevel@tonic-gate 		extv[i] = NULL;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	i = 0;
9530Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
9560Sstevel@tonic-gate 	msgsize = SPD_64TO8(msgsize);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
9590Sstevel@tonic-gate 		/* Check for unknown headers. */
9600Sstevel@tonic-gate 		i++;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
9630Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX) {
9640Sstevel@tonic-gate 			if (diag_buf != NULL) {
9650Sstevel@tonic-gate 				(void) snprintf(diag_buf, diag_buf_len,
9660Sstevel@tonic-gate 				    "spdsock ext 0x%X unknown: 0x%X",
9670Sstevel@tonic-gate 				    i, extv[0]->spd_ext_type);
9680Sstevel@tonic-gate 			}
9690Sstevel@tonic-gate 			return (KGE_UNK);
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		/*
9730Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
9740Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
9750Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
9760Sstevel@tonic-gate 		 */
9770Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
9780Sstevel@tonic-gate 		    (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
9790Sstevel@tonic-gate 		    (uint8_t *)((uint8_t *)basehdr + msgsize))
9800Sstevel@tonic-gate 			return (KGE_LEN);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 		/* Check for redundant headers. */
9830Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
9840Sstevel@tonic-gate 			return (KGE_DUP);
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
9870Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
9900Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
9910Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	/* Everything's cool. */
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
9980Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
9990Sstevel@tonic-gate 	 */
10000Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
10010Sstevel@tonic-gate 		extv[0] = NULL;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	return (KGE_OK);
10040Sstevel@tonic-gate }
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate const char *
10070Sstevel@tonic-gate spdsock_diag(int diagnostic)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate 	switch (diagnostic) {
10100Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NONE:
10114064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no error"));
10120Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNKNOWN_EXT:
10134064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown extension"));
10140Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_EXTLEN:
10154064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad extension length"));
10160Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_RULE_EXT:
10174064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no rule extension"));
10180Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
10194064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad address len"));
10200Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MIXED_AF:
10214064Smarkfen 		return (dgettext(TEXT_DOMAIN, "mixed address family"));
10220Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_NO_MEM:
10234064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: no memory"));
10240Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
10254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
10260Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
10274064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad type"));
10280Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
10294064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad flags"));
10300Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
10314064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
10320Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
10334064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local port"));
10340Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
10354064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local port"));
10360Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
10374064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote port"));
10380Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
10394064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
10400Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_PROTO:
10414064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed proto"));
10420Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
10434064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate proto"));
10440Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
10454064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local address"));
10460Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
10474064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local address"));
10480Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
10494064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote address"));
10500Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
10514064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
10520Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_ACTION:
10534064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed action"));
10540Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
10554064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate action"));
10560Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULE:
10574064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed rule"));
10580Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULE:
10594064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate rule"));
10600Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULESET:
10614064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
10620Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
10634064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
10640Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
10654064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid rule index"));
10660Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_SPDID:
10674064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad spdid"));
10680Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
10694064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad message type"));
10700Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
10714064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
10720Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
10734064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10744064Smarkfen 		    "unsupported ESP encryption algorithm"));
10750Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
10764064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10774064Smarkfen 		    "unsupported ESP authentication algorithm"));
10780Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
10794064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
10800Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
10814064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10824064Smarkfen 		    "unsupported ESP encryption key size"));
10830Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
10844064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10854064Smarkfen 		    "unsupported ESP authentication key size"));
10860Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_ACTION_EXT:
10874064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
10880Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_ID_RANGE:
10894064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
10900Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
10914064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10924064Smarkfen 		    "number of key sizes inconsistent"));
10930Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
10944064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10954064Smarkfen 		    "number of block sizes inconsistent"));
10960Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
10974064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
10983055Sdanmcd 	case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
10994064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11004064Smarkfen 		    "operation not applicable to all policies"));
11013055Sdanmcd 	case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
11024064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11034064Smarkfen 		    "using selectors on a transport-mode tunnel"));
11040Sstevel@tonic-gate 	default:
11054064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate }
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate /*
11100Sstevel@tonic-gate  * PF_KEY Diagnostic table.
11110Sstevel@tonic-gate  *
11120Sstevel@tonic-gate  * PF_KEY NOTE:  If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
11130Sstevel@tonic-gate  * where you need to add new messages.
11140Sstevel@tonic-gate  */
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate const char *
11170Sstevel@tonic-gate keysock_diag(int diagnostic)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	switch (diagnostic) {
11203055Sdanmcd 	case SADB_X_DIAGNOSTIC_NONE:
11214064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No diagnostic"));
11220Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
11234064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown message type"));
11240Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
11254064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
11260Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
11274064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad extension length"));
11280Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
11294064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11304064Smarkfen 		    "Unknown Security Association type"));
11310Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
11324064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11334064Smarkfen 		    "Specific Security Association type needed"));
11340Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_SADBS:
11354064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11364064Smarkfen 		    "No Security Association Databases present"));
11370Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_EXT:
11384064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11394064Smarkfen 		    "No extensions needed for message"));
11400Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
11414064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address family"));
11420Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST_AF:
11434064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11444064Smarkfen 		    "Bad destination address family"));
11450Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
11464064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11474064Smarkfen 		    "Bad inner-source address family"));
11480Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AF_MISMATCH:
11494064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11504064Smarkfen 		    "Source/destination address family mismatch"));
11510Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC:
11524064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address value"));
11530Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST:
11544064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
11550Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
11564064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11574064Smarkfen 		    "Soft allocations limit more than hard limit"));
11580Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BYTES_HSERR:
11594064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11604064Smarkfen 		    "Soft bytes limit more than hard limit"));
11610Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
11624064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
11630Sstevel@tonic-gate 		    "than hard expiration time"));
11640Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_USETIME_HSERR:
11654064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
11660Sstevel@tonic-gate 		    "than hard expiration time"));
11670Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SRC:
11684064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing source address"));
11690Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_DST:
11704064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing destination address"));
11710Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SA:
11724064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
11730Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_EKEY:
11744064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
11750Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_AKEY:
11764064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
11770Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_RANGE:
11784064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
11790Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
11804064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
11810Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
11824064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
11830Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
11844064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
11850Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
11864064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
11870Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
11884064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
11890Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
11904064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
11910Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
11924064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed source address"));
11930Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_DST:
11944064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
11950Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SA:
11964064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
11970Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
11984064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
11990Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
12004064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
12010Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
12024064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
12030Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
12044064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
12050Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
12064064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
12070Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_PROP_PRESENT:
12084064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
12090Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
12104064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12114064Smarkfen 		    "Supported algorithms extension not needed"));
12120Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AALG:
12134064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12144064Smarkfen 		    "Unsupported authentication algorithm"));
12150Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EALG:
12164064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12174064Smarkfen 		    "Unsupported encryption algorithm"));
12180Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
12194064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
12200Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SASTATE:
12214064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
12220Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
12234064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12244064Smarkfen 		    "Bad number of authentication bits"));
12250Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
12264064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12274064Smarkfen 		    "Bad number of encryption bits"));
12280Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
12294064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12304064Smarkfen 		    "Encryption not supported for this SA type"));
12310Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_EKEY:
12324064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
12330Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_AKEY:
12344064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
12350Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
12364064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12374064Smarkfen 		    "Duplicate key management protocol"));
12380Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
12394064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12404064Smarkfen 		    "Duplicate key management cookie"));
12410Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
12424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
12430Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
12444064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
12450Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
12464064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
12470Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
12484064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12494064Smarkfen 		    "Duplicate NAT-T remote address"));
12500Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
12514064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
12520Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
12534064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12544064Smarkfen 		    "Malformed NAT-T remote address"));
12550Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
12564064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
12573055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
12584064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
12593055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
12604064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12614064Smarkfen 		    "Missing inner destination address"));
12623055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
12634064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12644064Smarkfen 		    "Duplicate inner source address"));
12653055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
12664064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12674064Smarkfen 		    "Duplicate inner destination address"));
12683055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
12694064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12704064Smarkfen 		    "Malformed inner source address"));
12713055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
12724064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12734064Smarkfen 		    "Malformed inner destination address"));
12743055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
12754064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12764064Smarkfen 		    "Invalid inner-source prefix length "));
12773055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
12784064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12794064Smarkfen 		    "Invalid inner-destination prefix length"));
12803055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
12814064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12824064Smarkfen 		    "Bad inner-destination address family"));
12833055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
12844064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12853055Sdanmcd 		    "Inner source/destination address family mismatch"));
12863055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
12874064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12884064Smarkfen 		    "Bad NAT-T remote address family"));
12893055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
12904064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12914064Smarkfen 		    "Bad NAT-T local address family"));
12923055Sdanmcd 	case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
12934064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12944064Smarkfen 		    "Source/desination protocol mismatch"));
12953055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
12964064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12974064Smarkfen 		    "Inner source/desination protocol mismatch"));
12983055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
12994064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13004064Smarkfen 		    "Both inner ports and outer ports are set"));
13016668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE:
13026668Smarkfen 		return (dgettext(TEXT_DOMAIN,
13036668Smarkfen 		    "Pairing failed, target SA unsuitable for pairing"));
13046668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_ADD_MISMATCH:
13056668Smarkfen 		return (dgettext(TEXT_DOMAIN,
13066668Smarkfen 		    "Source/destination address differs from pair SA"));
13076668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_ALREADY:
13086668Smarkfen 		return (dgettext(TEXT_DOMAIN,
13096668Smarkfen 		    "Already paired with another security association"));
13106668Smarkfen 	case SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND:
13116668Smarkfen 		return (dgettext(TEXT_DOMAIN,
13126668Smarkfen 		    "Command failed, pair security association not found"));
13136668Smarkfen 	case SADB_X_DIAGNOSTIC_BAD_SA_DIRECTION:
13146668Smarkfen 		return (dgettext(TEXT_DOMAIN,
13156668Smarkfen 		    "Inappropriate SA direction"));
13166668Smarkfen 	case SADB_X_DIAGNOSTIC_SA_NOTFOUND:
13176668Smarkfen 		return (dgettext(TEXT_DOMAIN,
13186668Smarkfen 		    "Security association not found"));
13196668Smarkfen 	case SADB_X_DIAGNOSTIC_SA_EXPIRED:
13206668Smarkfen 		return (dgettext(TEXT_DOMAIN,
13216668Smarkfen 		    "Security association is not valid"));
13220Sstevel@tonic-gate 	default:
13234064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate }
13263055Sdanmcd 
13273055Sdanmcd /*
13283055Sdanmcd  * Convert an IPv6 mask to a prefix len.  I assume all IPv6 masks are
13293055Sdanmcd  * contiguous, so I stop at the first zero bit!
13303055Sdanmcd  */
13313055Sdanmcd int
13323055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
13333055Sdanmcd {
13343055Sdanmcd 	int rc = 0;
13353055Sdanmcd 	uint8_t last;
13363055Sdanmcd 	int limit = IPV6_ABITS;
13373055Sdanmcd 
13383055Sdanmcd 	if (is_v4mapped) {
13393055Sdanmcd 		mask += ((IPV6_ABITS - IP_ABITS)/8);
13403055Sdanmcd 		limit = IP_ABITS;
13413055Sdanmcd 	}
13423055Sdanmcd 
13433055Sdanmcd 	while (*mask == 0xff) {
13443055Sdanmcd 		rc += 8;
13453055Sdanmcd 		if (rc == limit)
13463055Sdanmcd 			return (limit);
13473055Sdanmcd 		mask++;
13483055Sdanmcd 	}
13493055Sdanmcd 
13503055Sdanmcd 	last = *mask;
13513055Sdanmcd 	while (last != 0) {
13523055Sdanmcd 		rc++;
13533055Sdanmcd 		last = (last << 1) & 0xff;
13543055Sdanmcd 	}
13553055Sdanmcd 
13563055Sdanmcd 	return (rc);
13573055Sdanmcd }
13583055Sdanmcd 
13593055Sdanmcd /*
13603055Sdanmcd  * Expand the diagnostic code into a message.
13613055Sdanmcd  */
13623055Sdanmcd void
13633055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic)
13643055Sdanmcd {
13653055Sdanmcd 	/* Use two spaces so above strings can fit on the line. */
13664064Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
13674064Smarkfen 	    "  Diagnostic code %u:  %s.\n"),
13683055Sdanmcd 	    diagnostic, keysock_diag(diagnostic));
13693055Sdanmcd }
13703055Sdanmcd 
13713055Sdanmcd /*
13723055Sdanmcd  * Prints the base PF_KEY message.
13733055Sdanmcd  */
13743055Sdanmcd void
13754867Spwernau print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock,
13764867Spwernau     boolean_t vflag)
13773055Sdanmcd {
13783055Sdanmcd 	if (wallclock != 0)
13794867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
13804064Smarkfen 		    "%sTimestamp: %s\n"), "", NULL,
13813055Sdanmcd 		    vflag);
13823055Sdanmcd 
13834867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
13844867Spwernau 	    "Base message (version %u) type "),
13853055Sdanmcd 	    samsg->sadb_msg_version);
13863055Sdanmcd 	switch (samsg->sadb_msg_type) {
13873055Sdanmcd 	case SADB_RESERVED:
13884867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
13894064Smarkfen 		    "RESERVED (warning: set to 0)"));
13903055Sdanmcd 		break;
13913055Sdanmcd 	case SADB_GETSPI:
13924867Spwernau 		(void) fprintf(file, "GETSPI");
13933055Sdanmcd 		break;
13943055Sdanmcd 	case SADB_UPDATE:
13954867Spwernau 		(void) fprintf(file, "UPDATE");
13963055Sdanmcd 		break;
13976668Smarkfen 	case SADB_X_UPDATEPAIR:
13986668Smarkfen 		(void) fprintf(file, "UPDATE PAIR");
13996668Smarkfen 		break;
14003055Sdanmcd 	case SADB_ADD:
14014867Spwernau 		(void) fprintf(file, "ADD");
14023055Sdanmcd 		break;
14033055Sdanmcd 	case SADB_DELETE:
14044867Spwernau 		(void) fprintf(file, "DELETE");
14053055Sdanmcd 		break;
14066668Smarkfen 	case SADB_X_DELPAIR:
14076668Smarkfen 		(void) fprintf(file, "DELETE PAIR");
14086668Smarkfen 		break;
14093055Sdanmcd 	case SADB_GET:
14104867Spwernau 		(void) fprintf(file, "GET");
14113055Sdanmcd 		break;
14123055Sdanmcd 	case SADB_ACQUIRE:
14134867Spwernau 		(void) fprintf(file, "ACQUIRE");
14143055Sdanmcd 		break;
14153055Sdanmcd 	case SADB_REGISTER:
14164867Spwernau 		(void) fprintf(file, "REGISTER");
14173055Sdanmcd 		break;
14183055Sdanmcd 	case SADB_EXPIRE:
14194867Spwernau 		(void) fprintf(file, "EXPIRE");
14203055Sdanmcd 		break;
14213055Sdanmcd 	case SADB_FLUSH:
14224867Spwernau 		(void) fprintf(file, "FLUSH");
14233055Sdanmcd 		break;
14243055Sdanmcd 	case SADB_DUMP:
14254867Spwernau 		(void) fprintf(file, "DUMP");
14263055Sdanmcd 		break;
14273055Sdanmcd 	case SADB_X_PROMISC:
14284867Spwernau 		(void) fprintf(file, "X_PROMISC");
14293055Sdanmcd 		break;
14303055Sdanmcd 	case SADB_X_INVERSE_ACQUIRE:
14314867Spwernau 		(void) fprintf(file, "X_INVERSE_ACQUIRE");
14323055Sdanmcd 		break;
14333055Sdanmcd 	default:
14344867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14354064Smarkfen 		    "Unknown (%u)"), samsg->sadb_msg_type);
14363055Sdanmcd 		break;
14373055Sdanmcd 	}
14384867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type "));
14393055Sdanmcd 
14403055Sdanmcd 	switch (samsg->sadb_msg_satype) {
14413055Sdanmcd 	case SADB_SATYPE_UNSPEC:
14424867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14434867Spwernau 		    "<unspecified/all>"));
14443055Sdanmcd 		break;
14453055Sdanmcd 	case SADB_SATYPE_AH:
14464867Spwernau 		(void) fprintf(file, "AH");
14473055Sdanmcd 		break;
14483055Sdanmcd 	case SADB_SATYPE_ESP:
14494867Spwernau 		(void) fprintf(file, "ESP");
14503055Sdanmcd 		break;
14513055Sdanmcd 	case SADB_SATYPE_RSVP:
14524867Spwernau 		(void) fprintf(file, "RSVP");
14533055Sdanmcd 		break;
14543055Sdanmcd 	case SADB_SATYPE_OSPFV2:
14554867Spwernau 		(void) fprintf(file, "OSPFv2");
14563055Sdanmcd 		break;
14573055Sdanmcd 	case SADB_SATYPE_RIPV2:
14584867Spwernau 		(void) fprintf(file, "RIPv2");
14593055Sdanmcd 		break;
14603055Sdanmcd 	case SADB_SATYPE_MIP:
14614867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP"));
14623055Sdanmcd 		break;
14633055Sdanmcd 	default:
14644867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14654064Smarkfen 		    "<unknown %u>"), samsg->sadb_msg_satype);
14663055Sdanmcd 		break;
14673055Sdanmcd 	}
14683055Sdanmcd 
14694867Spwernau 	(void) fprintf(file, ".\n");
14703055Sdanmcd 
14713055Sdanmcd 	if (samsg->sadb_msg_errno != 0) {
14724867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14734867Spwernau 		    "Error %s from PF_KEY.\n"),
14743055Sdanmcd 		    strerror(samsg->sadb_msg_errno));
14754867Spwernau 		print_diagnostic(file, samsg->sadb_x_msg_diagnostic);
14763055Sdanmcd 	}
14773055Sdanmcd 
14784867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
14794064Smarkfen 	    "Message length %u bytes, seq=%u, pid=%u.\n"),
14803055Sdanmcd 	    SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
14813055Sdanmcd 	    samsg->sadb_msg_pid);
14823055Sdanmcd }
14833055Sdanmcd 
14843055Sdanmcd /*
14853055Sdanmcd  * Print the SA extension for PF_KEY.
14863055Sdanmcd  */
14873055Sdanmcd void
14884867Spwernau print_sa(FILE *file, char *prefix, struct sadb_sa *assoc)
14893055Sdanmcd {
14903055Sdanmcd 	if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
14914867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
14924064Smarkfen 		    "WARNING: SA info extension length (%u) is bad."),
14933055Sdanmcd 		    SADB_64TO8(assoc->sadb_sa_len));
14943055Sdanmcd 	}
14953055Sdanmcd 
14964867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
14974064Smarkfen 	    "%sSADB_ASSOC spi=0x%x, replay=%u, state="),
14983055Sdanmcd 	    prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
14993055Sdanmcd 	switch (assoc->sadb_sa_state) {
15003055Sdanmcd 	case SADB_SASTATE_LARVAL:
15014867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL"));
15023055Sdanmcd 		break;
15033055Sdanmcd 	case SADB_SASTATE_MATURE:
15044867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE"));
15053055Sdanmcd 		break;
15063055Sdanmcd 	case SADB_SASTATE_DYING:
15074867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING"));
15083055Sdanmcd 		break;
15093055Sdanmcd 	case SADB_SASTATE_DEAD:
15104867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD"));
15113055Sdanmcd 		break;
15123055Sdanmcd 	default:
15134867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15144064Smarkfen 		    "<unknown %u>"), assoc->sadb_sa_state);
15153055Sdanmcd 	}
15163055Sdanmcd 
15173055Sdanmcd 	if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
15184867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15194064Smarkfen 		    "\n%sAuthentication algorithm = "),
15203055Sdanmcd 		    prefix);
15214867Spwernau 		(void) dump_aalg(assoc->sadb_sa_auth, file);
15223055Sdanmcd 	}
15233055Sdanmcd 
15243055Sdanmcd 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
15254867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15264064Smarkfen 		    "\n%sEncryption algorithm = "), prefix);
15274867Spwernau 		(void) dump_ealg(assoc->sadb_sa_encrypt, file);
15283055Sdanmcd 	}
15293055Sdanmcd 
15304867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
15313055Sdanmcd 	    assoc->sadb_sa_flags);
15323055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
15334867Spwernau 		(void) fprintf(file, "PFS ");
15343055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
15354867Spwernau 		(void) fprintf(file, "NOREPLAY ");
15363055Sdanmcd 
15373055Sdanmcd 	/* BEGIN Solaris-specific flags. */
15383055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
15394867Spwernau 		(void) fprintf(file, "X_USED ");
15406668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_PAIRED)
15416668Smarkfen 		(void) fprintf(file, "X_PAIRED ");
15426668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_OUTBOUND)
15436668Smarkfen 		(void) fprintf(file, "X_OUTBOUND ");
15446668Smarkfen 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_INBOUND)
15456668Smarkfen 		(void) fprintf(file, "X_INBOUND ");
15463055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
15474867Spwernau 		(void) fprintf(file, "X_UNIQUE ");
15483055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
15494867Spwernau 		(void) fprintf(file, "X_AALG1 ");
15503055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
15514867Spwernau 		(void) fprintf(file, "X_AALG2 ");
15523055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
15534867Spwernau 		(void) fprintf(file, "X_EALG1 ");
15543055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
15554867Spwernau 		(void) fprintf(file, "X_EALG2 ");
15563055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
15574867Spwernau 		(void) fprintf(file, "X_NATT_LOC ");
15583055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
15594867Spwernau 		(void) fprintf(file, "X_NATT_REM ");
15603055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
15614867Spwernau 		(void) fprintf(file, "X_TUNNEL ");
15627066Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATTED)
15637066Sdanmcd 		(void) fprintf(file, "X_NATTED ");
15643055Sdanmcd 	/* END Solaris-specific flags. */
15653055Sdanmcd 
15664867Spwernau 	(void) fprintf(file, ">\n");
15673055Sdanmcd }
15683055Sdanmcd 
15693055Sdanmcd void
15704867Spwernau printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx,
15714867Spwernau     const char *pfx2, boolean_t vflag)
15723055Sdanmcd {
15733055Sdanmcd 	char tbuf[TBUF_SIZE]; /* For strftime() call. */
15743055Sdanmcd 	const char *tp = tbuf;
15753055Sdanmcd 	time_t t = lt;
15763055Sdanmcd 	struct tm res;
15773055Sdanmcd 
15783055Sdanmcd 	if (t != lt) {
15793055Sdanmcd 		if (lt > 0)
15803055Sdanmcd 			t = LONG_MAX;
15813055Sdanmcd 		else
15823055Sdanmcd 			t = LONG_MIN;
15833055Sdanmcd 	}
15843055Sdanmcd 
15853055Sdanmcd 	if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
15864064Smarkfen 		tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
15874867Spwernau 	(void) fprintf(file, msg, pfx, tp);
15883055Sdanmcd 	if (vflag && (pfx2 != NULL))
15894867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1590*7320Sdanmcd@sun.com 		    "%s\t(raw time value %" PRIu64 ")\n"), pfx2, lt);
15913055Sdanmcd }
15923055Sdanmcd 
15933055Sdanmcd /*
15943055Sdanmcd  * Print the SA lifetime information.  (An SADB_EXT_LIFETIME_* extension.)
15953055Sdanmcd  */
15963055Sdanmcd void
15974867Spwernau print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current,
15983055Sdanmcd     struct sadb_lifetime *hard, struct sadb_lifetime *soft, boolean_t vflag)
15993055Sdanmcd {
16003055Sdanmcd 	int64_t scratch;
16014064Smarkfen 	char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
16024064Smarkfen 	char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
16034064Smarkfen 	char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
16043055Sdanmcd 
16053055Sdanmcd 	if (current != NULL &&
16063055Sdanmcd 	    current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
16074867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
16084064Smarkfen 		    "WARNING: CURRENT lifetime extension length (%u) is bad."),
16093055Sdanmcd 		    SADB_64TO8(current->sadb_lifetime_len));
16103055Sdanmcd 	}
16113055Sdanmcd 
16123055Sdanmcd 	if (hard != NULL &&
16133055Sdanmcd 	    hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
16144867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
16154867Spwernau 		    "WARNING: HARD lifetime extension length (%u) is bad."),
16163055Sdanmcd 		    SADB_64TO8(hard->sadb_lifetime_len));
16173055Sdanmcd 	}
16183055Sdanmcd 
16193055Sdanmcd 	if (soft != NULL &&
16203055Sdanmcd 	    soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
16214867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
16224867Spwernau 		    "WARNING: SOFT lifetime extension length (%u) is bad."),
16233055Sdanmcd 		    SADB_64TO8(soft->sadb_lifetime_len));
16243055Sdanmcd 	}
16253055Sdanmcd 
16264867Spwernau 	(void) fprintf(file, " LT: Lifetime information\n");
16273055Sdanmcd 
16283055Sdanmcd 	if (current != NULL) {
16293055Sdanmcd 		/* Express values as current values. */
16304867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1631*7320Sdanmcd@sun.com 		    "%s%" PRIu64 " bytes protected, %u allocations used.\n"),
16323055Sdanmcd 		    current_prefix, current->sadb_lifetime_bytes,
16333055Sdanmcd 		    current->sadb_lifetime_allocations);
16344867Spwernau 		printsatime(file, current->sadb_lifetime_addtime,
16354064Smarkfen 		    dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"),
16363055Sdanmcd 		    current_prefix, current_prefix, vflag);
16373055Sdanmcd 		if (current->sadb_lifetime_usetime != 0) {
16384867Spwernau 			printsatime(file, current->sadb_lifetime_usetime,
16394064Smarkfen 			    dgettext(TEXT_DOMAIN,
16404064Smarkfen 			    "%sSA first used at time %s\n"),
16413055Sdanmcd 			    current_prefix, current_prefix, vflag);
16423055Sdanmcd 		}
16434867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
16444064Smarkfen 		    "%sTime now is %s\n"), current_prefix, current_prefix,
16454064Smarkfen 		    vflag);
16463055Sdanmcd 	}
16473055Sdanmcd 
16483055Sdanmcd 	if (soft != NULL) {
16494867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16504064Smarkfen 		    "%sSoft lifetime information:  "),
16513055Sdanmcd 		    soft_prefix);
16524867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1653*7320Sdanmcd@sun.com 		    "%" PRIu64 " bytes of lifetime, %u "
16543055Sdanmcd 		    "allocations.\n"), soft->sadb_lifetime_bytes,
16553055Sdanmcd 		    soft->sadb_lifetime_allocations);
16564867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1657*7320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-add lifetime.\n"),
16583055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_addtime);
16594867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1660*7320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-use lifetime.\n"),
16613055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_usetime);
16623055Sdanmcd 		/* If possible, express values as time remaining. */
16633055Sdanmcd 		if (current != NULL) {
16643055Sdanmcd 			if (soft->sadb_lifetime_bytes != 0)
1665*7320Sdanmcd@sun.com 				(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s%"
1666*7320Sdanmcd@sun.com 				    PRIu64 " more bytes can be protected.\n"),
16673055Sdanmcd 				    soft_prefix,
16683055Sdanmcd 				    (soft->sadb_lifetime_bytes >
16694342Spwernau 				    current->sadb_lifetime_bytes) ?
16703055Sdanmcd 				    (soft->sadb_lifetime_bytes -
16714342Spwernau 				    current->sadb_lifetime_bytes) : (0));
16723055Sdanmcd 			if (soft->sadb_lifetime_addtime != 0 ||
16733055Sdanmcd 			    (soft->sadb_lifetime_usetime != 0 &&
16744342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
16753055Sdanmcd 				int64_t adddelta, usedelta;
16763055Sdanmcd 
16773055Sdanmcd 				if (soft->sadb_lifetime_addtime != 0) {
16783055Sdanmcd 					adddelta =
16793055Sdanmcd 					    current->sadb_lifetime_addtime +
16803055Sdanmcd 					    soft->sadb_lifetime_addtime -
16813055Sdanmcd 					    wallclock;
16823055Sdanmcd 				} else {
16833055Sdanmcd 					adddelta = TIME_MAX;
16843055Sdanmcd 				}
16853055Sdanmcd 
16863055Sdanmcd 				if (soft->sadb_lifetime_usetime != 0 &&
16873055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
16883055Sdanmcd 					usedelta =
16893055Sdanmcd 					    current->sadb_lifetime_usetime +
16903055Sdanmcd 					    soft->sadb_lifetime_usetime -
16913055Sdanmcd 					    wallclock;
16923055Sdanmcd 				} else {
16933055Sdanmcd 					usedelta = TIME_MAX;
16943055Sdanmcd 				}
16954867Spwernau 				(void) fprintf(file, "%s", soft_prefix);
16963055Sdanmcd 				scratch = MIN(adddelta, usedelta);
16973055Sdanmcd 				if (scratch >= 0) {
16984867Spwernau 					(void) fprintf(file,
16994867Spwernau 					    dgettext(TEXT_DOMAIN,
1700*7320Sdanmcd@sun.com 					    "Soft expiration occurs in %"
1701*7320Sdanmcd@sun.com 					    PRId64 " seconds, "), scratch);
17023055Sdanmcd 				} else {
17034867Spwernau 					(void) fprintf(file,
17044867Spwernau 					    dgettext(TEXT_DOMAIN,
17053055Sdanmcd 					    "Soft expiration occurred "));
17063055Sdanmcd 				}
17073055Sdanmcd 				scratch += wallclock;
17084867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
17094064Smarkfen 				    "%sat %s.\n"), "", soft_prefix, vflag);
17103055Sdanmcd 			}
17113055Sdanmcd 		}
17123055Sdanmcd 	}
17133055Sdanmcd 
17143055Sdanmcd 	if (hard != NULL) {
17154867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17164064Smarkfen 		    "%sHard lifetime information:  "), hard_prefix);
17174867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1718*7320Sdanmcd@sun.com 		    "%" PRIu64 " bytes of lifetime, %u allocations.\n"),
17194867Spwernau 		    hard->sadb_lifetime_bytes, hard->sadb_lifetime_allocations);
17204867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1721*7320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-add lifetime.\n"),
17223055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_addtime);
17234867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1724*7320Sdanmcd@sun.com 		    "%s%" PRIu64 " seconds of post-use lifetime.\n"),
17253055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_usetime);
17263055Sdanmcd 		/* If possible, express values as time remaining. */
17273055Sdanmcd 		if (current != NULL) {
17283055Sdanmcd 			if (hard->sadb_lifetime_bytes != 0)
1729*7320Sdanmcd@sun.com 				(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s%"
1730*7320Sdanmcd@sun.com 				    PRIu64 " more bytes can be protected.\n"),
17313055Sdanmcd 				    hard_prefix,
17323055Sdanmcd 				    (hard->sadb_lifetime_bytes >
17334342Spwernau 				    current->sadb_lifetime_bytes) ?
17343055Sdanmcd 				    (hard->sadb_lifetime_bytes -
17354342Spwernau 				    current->sadb_lifetime_bytes) : (0));
17363055Sdanmcd 			if (hard->sadb_lifetime_addtime != 0 ||
17373055Sdanmcd 			    (hard->sadb_lifetime_usetime != 0 &&
17384342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
17393055Sdanmcd 				int64_t adddelta, usedelta;
17403055Sdanmcd 
17413055Sdanmcd 				if (hard->sadb_lifetime_addtime != 0) {
17423055Sdanmcd 					adddelta =
17433055Sdanmcd 					    current->sadb_lifetime_addtime +
17443055Sdanmcd 					    hard->sadb_lifetime_addtime -
17453055Sdanmcd 					    wallclock;
17463055Sdanmcd 				} else {
17473055Sdanmcd 					adddelta = TIME_MAX;
17483055Sdanmcd 				}
17493055Sdanmcd 
17503055Sdanmcd 				if (hard->sadb_lifetime_usetime != 0 &&
17513055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
17523055Sdanmcd 					usedelta =
17533055Sdanmcd 					    current->sadb_lifetime_usetime +
17543055Sdanmcd 					    hard->sadb_lifetime_usetime -
17553055Sdanmcd 					    wallclock;
17563055Sdanmcd 				} else {
17573055Sdanmcd 					usedelta = TIME_MAX;
17583055Sdanmcd 				}
17594867Spwernau 				(void) fprintf(file, "%s", hard_prefix);
17603055Sdanmcd 				scratch = MIN(adddelta, usedelta);
17613055Sdanmcd 				if (scratch >= 0) {
17624867Spwernau 					(void) fprintf(file,
17634867Spwernau 					    dgettext(TEXT_DOMAIN,
1764*7320Sdanmcd@sun.com 					    "Hard expiration occurs in %"
1765*7320Sdanmcd@sun.com 					    PRId64 " seconds, "), scratch);
17663055Sdanmcd 				} else {
17674867Spwernau 					(void) fprintf(file,
17684867Spwernau 					    dgettext(TEXT_DOMAIN,
17693055Sdanmcd 					    "Hard expiration occured "));
17703055Sdanmcd 				}
17713055Sdanmcd 				scratch += wallclock;
17724867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
17734064Smarkfen 				    "%sat %s.\n"), "", hard_prefix, vflag);
17743055Sdanmcd 			}
17753055Sdanmcd 		}
17763055Sdanmcd 	}
17773055Sdanmcd }
17783055Sdanmcd 
17793055Sdanmcd /*
17803055Sdanmcd  * Print an SADB_EXT_ADDRESS_* extension.
17813055Sdanmcd  */
17823055Sdanmcd void
17834867Spwernau print_address(FILE *file, char *prefix, struct sadb_address *addr,
17844867Spwernau     boolean_t ignore_nss)
17853055Sdanmcd {
17863055Sdanmcd 	struct protoent *pe;
17873055Sdanmcd 
17884867Spwernau 	(void) fprintf(file, "%s", prefix);
17893055Sdanmcd 	switch (addr->sadb_address_exttype) {
17903055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
17914867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address "));
17923055Sdanmcd 		break;
17933055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
17944867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17954867Spwernau 		    "Inner source address "));
17963055Sdanmcd 		break;
17973055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
17984867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17994867Spwernau 		    "Destination address "));
18003055Sdanmcd 		break;
18013055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
18024867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18034064Smarkfen 		    "Inner destination address "));
18043055Sdanmcd 		break;
18053055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
18064867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18074867Spwernau 		    "NAT-T local address "));
18083055Sdanmcd 		break;
18093055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
18104867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18114867Spwernau 		    "NAT-T remote address "));
18123055Sdanmcd 		break;
18133055Sdanmcd 	}
18143055Sdanmcd 
18154867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
18164064Smarkfen 	    "(proto=%d"), addr->sadb_address_proto);
18174867Spwernau 	if (ignore_nss == B_FALSE) {
18183055Sdanmcd 		if (addr->sadb_address_proto == 0) {
18194867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
18204867Spwernau 			    "/<unspecified>"));
18213055Sdanmcd 		} else if ((pe = getprotobynumber(addr->sadb_address_proto))
18223055Sdanmcd 		    != NULL) {
18234867Spwernau 			(void) fprintf(file, "/%s", pe->p_name);
18243055Sdanmcd 		} else {
18254867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
18264867Spwernau 			    "/<unknown>"));
18273055Sdanmcd 		}
18283055Sdanmcd 	}
18294867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
18303055Sdanmcd 	(void) dump_sockaddr((struct sockaddr *)(addr + 1),
18314867Spwernau 	    addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss);
18323055Sdanmcd }
18333055Sdanmcd 
18343055Sdanmcd /*
18353055Sdanmcd  * Print an SADB_EXT_KEY extension.
18363055Sdanmcd  */
18373055Sdanmcd void
18384867Spwernau print_key(FILE *file, char *prefix, struct sadb_key *key)
18393055Sdanmcd {
18404867Spwernau 	(void) fprintf(file, "%s", prefix);
18413055Sdanmcd 
18423055Sdanmcd 	switch (key->sadb_key_exttype) {
18433055Sdanmcd 	case SADB_EXT_KEY_AUTH:
18444867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication"));
18453055Sdanmcd 		break;
18463055Sdanmcd 	case SADB_EXT_KEY_ENCRYPT:
18474867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption"));
18483055Sdanmcd 		break;
18493055Sdanmcd 	}
18503055Sdanmcd 
18514867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
18524867Spwernau 	(void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits, file);
18534867Spwernau 	(void) fprintf(file, "\n");
18543055Sdanmcd }
18553055Sdanmcd 
18563055Sdanmcd /*
18573055Sdanmcd  * Print an SADB_EXT_IDENTITY_* extension.
18583055Sdanmcd  */
18593055Sdanmcd void
18604867Spwernau print_ident(FILE *file, char *prefix, struct sadb_ident *id)
18613055Sdanmcd {
18623055Sdanmcd 	boolean_t canprint = B_TRUE;
18633055Sdanmcd 
18644867Spwernau 	(void) fprintf(file, "%s", prefix);
18653055Sdanmcd 	switch (id->sadb_ident_exttype) {
18663055Sdanmcd 	case SADB_EXT_IDENTITY_SRC:
18674867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source"));
18683055Sdanmcd 		break;
18693055Sdanmcd 	case SADB_EXT_IDENTITY_DST:
18704867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination"));
18713055Sdanmcd 		break;
18723055Sdanmcd 	}
18733055Sdanmcd 
18744867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
18754064Smarkfen 	    " identity, uid=%d, type "), id->sadb_ident_id);
18764867Spwernau 	canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL);
18774867Spwernau 	(void) fprintf(file, "\n%s", prefix);
18786119Spwernau 	if (canprint) {
18794867Spwernau 		(void) fprintf(file, "%s\n", (char *)(id + 1));
18806119Spwernau 	} else {
18816119Spwernau 		print_asn1_name(file, (const unsigned char *)(id + 1),
18826119Spwernau 		    SADB_64TO8(id->sadb_ident_len) - sizeof (sadb_ident_t));
18836119Spwernau 	}
18843055Sdanmcd }
18853055Sdanmcd 
18863055Sdanmcd /*
18873055Sdanmcd  * Print an SADB_SENSITIVITY extension.
18883055Sdanmcd  */
18893055Sdanmcd void
18904867Spwernau print_sens(FILE *file, char *prefix, struct sadb_sens *sens)
18913055Sdanmcd {
18923055Sdanmcd 	uint64_t *bitmap = (uint64_t *)(sens + 1);
18933055Sdanmcd 	int i;
18943055Sdanmcd 
18954867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
18964064Smarkfen 	    "%sSensitivity DPD %d, sens level=%d, integ level=%d\n"),
18973055Sdanmcd 	    prefix, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
18983055Sdanmcd 	    sens->sadb_sens_integ_level);
18993055Sdanmcd 	for (i = 0; sens->sadb_sens_sens_len-- > 0; i++, bitmap++)
19004867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1901*7320Sdanmcd@sun.com 		    "%s Sensitivity BM extended word %d 0x%" PRIx64 "\n"),
19024867Spwernau 		    prefix, i, *bitmap);
19033055Sdanmcd 	for (i = 0; sens->sadb_sens_integ_len-- > 0; i++, bitmap++)
19044867Spwernau 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1905*7320Sdanmcd@sun.com 		    "%s Integrity BM extended word %d 0x%" PRIx64 "\n"),
19064867Spwernau 		    prefix, i, *bitmap);
19073055Sdanmcd }
19083055Sdanmcd 
19093055Sdanmcd /*
19103055Sdanmcd  * Print an SADB_EXT_PROPOSAL extension.
19113055Sdanmcd  */
19123055Sdanmcd void
19134867Spwernau print_prop(FILE *file, char *prefix, struct sadb_prop *prop)
19143055Sdanmcd {
19153055Sdanmcd 	struct sadb_comb *combs;
19163055Sdanmcd 	int i, numcombs;
19173055Sdanmcd 
19184867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
19194064Smarkfen 	    "%sProposal, replay counter = %u.\n"), prefix,
19203055Sdanmcd 	    prop->sadb_prop_replay);
19213055Sdanmcd 
19223055Sdanmcd 	numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
19233055Sdanmcd 	numcombs /= SADB_8TO64(sizeof (*combs));
19243055Sdanmcd 
19253055Sdanmcd 	combs = (struct sadb_comb *)(prop + 1);
19263055Sdanmcd 
19273055Sdanmcd 	for (i = 0; i < numcombs; i++) {
19284867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19294064Smarkfen 		    "%s Combination #%u "), prefix, i + 1);
19303055Sdanmcd 		if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
19314867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19324064Smarkfen 			    "Authentication = "));
19334867Spwernau 			(void) dump_aalg(combs[i].sadb_comb_auth, file);
19344867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19354064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
19363055Sdanmcd 			    combs[i].sadb_comb_auth_minbits,
19373055Sdanmcd 			    combs[i].sadb_comb_auth_maxbits, prefix);
19383055Sdanmcd 		}
19393055Sdanmcd 
19403055Sdanmcd 		if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
19414867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19424867Spwernau 			    "Encryption = "));
19434867Spwernau 			(void) dump_ealg(combs[i].sadb_comb_encrypt, file);
19444867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19454064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
19463055Sdanmcd 			    combs[i].sadb_comb_encrypt_minbits,
19473055Sdanmcd 			    combs[i].sadb_comb_encrypt_maxbits, prefix);
19483055Sdanmcd 		}
19493055Sdanmcd 
19504867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: "));
19513055Sdanmcd 		if (combs[i].sadb_comb_hard_allocations)
19524867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
19533055Sdanmcd 			    combs[i].sadb_comb_hard_allocations);
19543055Sdanmcd 		if (combs[i].sadb_comb_hard_bytes)
1955*7320Sdanmcd@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
1956*7320Sdanmcd@sun.com 			    PRIu64 " "), combs[i].sadb_comb_hard_bytes);
19573055Sdanmcd 		if (combs[i].sadb_comb_hard_addtime)
19584867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1959*7320Sdanmcd@sun.com 			    "post-add secs=%" PRIu64 " "),
19603055Sdanmcd 			    combs[i].sadb_comb_hard_addtime);
19613055Sdanmcd 		if (combs[i].sadb_comb_hard_usetime)
19624867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1963*7320Sdanmcd@sun.com 			    "post-use secs=%" PRIu64 ""),
19643055Sdanmcd 			    combs[i].sadb_comb_hard_usetime);
19653055Sdanmcd 
19664867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "),
19674867Spwernau 		    prefix);
19683055Sdanmcd 		if (combs[i].sadb_comb_soft_allocations)
19694867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
19703055Sdanmcd 			    combs[i].sadb_comb_soft_allocations);
19713055Sdanmcd 		if (combs[i].sadb_comb_soft_bytes)
1972*7320Sdanmcd@sun.com 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%"
1973*7320Sdanmcd@sun.com 			    PRIu64 " "), combs[i].sadb_comb_soft_bytes);
19743055Sdanmcd 		if (combs[i].sadb_comb_soft_addtime)
19754867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1976*7320Sdanmcd@sun.com 			    "post-add secs=%" PRIu64 " "),
19773055Sdanmcd 			    combs[i].sadb_comb_soft_addtime);
19783055Sdanmcd 		if (combs[i].sadb_comb_soft_usetime)
19794867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1980*7320Sdanmcd@sun.com 			    "post-use secs=%" PRIu64 ""),
19813055Sdanmcd 			    combs[i].sadb_comb_soft_usetime);
19824867Spwernau 		(void) fprintf(file, "\n");
19833055Sdanmcd 	}
19843055Sdanmcd }
19853055Sdanmcd 
19863055Sdanmcd /*
19873055Sdanmcd  * Print an extended proposal (SADB_X_EXT_EPROP).
19883055Sdanmcd  */
19893055Sdanmcd void
19904867Spwernau print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop)
19913055Sdanmcd {
19923055Sdanmcd 	uint64_t *sofar;
19933055Sdanmcd 	struct sadb_x_ecomb *ecomb;
19943055Sdanmcd 	struct sadb_x_algdesc *algdesc;
19953055Sdanmcd 	int i, j;
19963055Sdanmcd 
19974867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
19984064Smarkfen 	    "%sExtended Proposal, replay counter = %u, "), prefix,
19994064Smarkfen 	    eprop->sadb_prop_replay);
20004867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
20014867Spwernau 	    "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs);
20023055Sdanmcd 
20033055Sdanmcd 	sofar = (uint64_t *)(eprop + 1);
20043055Sdanmcd 	ecomb = (struct sadb_x_ecomb *)sofar;
20053055Sdanmcd 
20063055Sdanmcd 	for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
20074867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
20084064Smarkfen 		    "%s Extended combination #%u:\n"), prefix, ++i);
20093055Sdanmcd 
20104867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "),
20114867Spwernau 		    prefix);
20124867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
20133055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_allocations);
2014*7320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%" PRIu64
2015*7320Sdanmcd@sun.com 		    ", "), ecomb->sadb_x_ecomb_hard_bytes);
2016*7320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-add secs=%"
2017*7320Sdanmcd@sun.com 		    PRIu64 ", "), ecomb->sadb_x_ecomb_hard_addtime);
2018*7320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
2019*7320Sdanmcd@sun.com 		    PRIu64 "\n"), ecomb->sadb_x_ecomb_hard_usetime);
20203055Sdanmcd 
20214867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "),
20224867Spwernau 		    prefix);
20234867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
20243055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_allocations);
2025*7320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2026*7320Sdanmcd@sun.com 		    "bytes=%" PRIu64 ", "), ecomb->sadb_x_ecomb_soft_bytes);
20274867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
2028*7320Sdanmcd@sun.com 		    "post-add secs=%" PRIu64 ", "),
20293055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_addtime);
2030*7320Sdanmcd@sun.com 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "post-use secs=%"
2031*7320Sdanmcd@sun.com 		    PRIu64 "\n"), ecomb->sadb_x_ecomb_soft_usetime);
20323055Sdanmcd 
20333055Sdanmcd 		sofar = (uint64_t *)(ecomb + 1);
20343055Sdanmcd 		algdesc = (struct sadb_x_algdesc *)sofar;
20353055Sdanmcd 
20363055Sdanmcd 		for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
20374867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
20384064Smarkfen 			    "%s Alg #%u "), prefix, ++j);
20393055Sdanmcd 			switch (algdesc->sadb_x_algdesc_satype) {
20403055Sdanmcd 			case SADB_SATYPE_ESP:
20414867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20424064Smarkfen 				    "for ESP "));
20433055Sdanmcd 				break;
20443055Sdanmcd 			case SADB_SATYPE_AH:
20454867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20464867Spwernau 				    "for AH "));
20473055Sdanmcd 				break;
20483055Sdanmcd 			default:
20494867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20504064Smarkfen 				    "for satype=%d "),
20513055Sdanmcd 				    algdesc->sadb_x_algdesc_satype);
20523055Sdanmcd 			}
20533055Sdanmcd 			switch (algdesc->sadb_x_algdesc_algtype) {
20543055Sdanmcd 			case SADB_X_ALGTYPE_CRYPT:
20554867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20564064Smarkfen 				    "Encryption = "));
20573055Sdanmcd 				(void) dump_ealg(algdesc->sadb_x_algdesc_alg,
20584867Spwernau 				    file);
20593055Sdanmcd 				break;
20603055Sdanmcd 			case SADB_X_ALGTYPE_AUTH:
20614867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20624064Smarkfen 				    "Authentication = "));
20633055Sdanmcd 				(void) dump_aalg(algdesc->sadb_x_algdesc_alg,
20644867Spwernau 				    file);
20653055Sdanmcd 				break;
20663055Sdanmcd 			default:
20674867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20684064Smarkfen 				    "algtype(%d) = alg(%d)"),
20693055Sdanmcd 				    algdesc->sadb_x_algdesc_algtype,
20703055Sdanmcd 				    algdesc->sadb_x_algdesc_alg);
20713055Sdanmcd 				break;
20723055Sdanmcd 			}
20733055Sdanmcd 
20744867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
20754064Smarkfen 			    "  minbits=%u, maxbits=%u.\n"),
20763055Sdanmcd 			    algdesc->sadb_x_algdesc_minbits,
20773055Sdanmcd 			    algdesc->sadb_x_algdesc_maxbits);
20783055Sdanmcd 
20793055Sdanmcd 			sofar = (uint64_t *)(++algdesc);
20803055Sdanmcd 		}
20813055Sdanmcd 		ecomb = (struct sadb_x_ecomb *)sofar;
20823055Sdanmcd 	}
20833055Sdanmcd }
20843055Sdanmcd 
20853055Sdanmcd /*
20863055Sdanmcd  * Print an SADB_EXT_SUPPORTED extension.
20873055Sdanmcd  */
20883055Sdanmcd void
20894867Spwernau print_supp(FILE *file, char *prefix, struct sadb_supported *supp)
20903055Sdanmcd {
20913055Sdanmcd 	struct sadb_alg *algs;
20923055Sdanmcd 	int i, numalgs;
20933055Sdanmcd 
20944867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
20953055Sdanmcd 	switch (supp->sadb_supported_exttype) {
20963055Sdanmcd 	case SADB_EXT_SUPPORTED_AUTH:
20974867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication"));
20983055Sdanmcd 		break;
20993055Sdanmcd 	case SADB_EXT_SUPPORTED_ENCRYPT:
21004867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption"));
21013055Sdanmcd 		break;
21023055Sdanmcd 	}
21034867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n"));
21043055Sdanmcd 
21053055Sdanmcd 	algs = (struct sadb_alg *)(supp + 1);
21063055Sdanmcd 	numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
21073055Sdanmcd 	numalgs /= SADB_8TO64(sizeof (*algs));
21083055Sdanmcd 	for (i = 0; i < numalgs; i++) {
21095906Svk199839 		uint16_t exttype = supp->sadb_supported_exttype;
21105906Svk199839 
21114867Spwernau 		(void) fprintf(file, "%s", prefix);
21125906Svk199839 		switch (exttype) {
21133055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
21144867Spwernau 			(void) dump_aalg(algs[i].sadb_alg_id, file);
21153055Sdanmcd 			break;
21163055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
21174867Spwernau 			(void) dump_ealg(algs[i].sadb_alg_id, file);
21183055Sdanmcd 			break;
21193055Sdanmcd 		}
21204867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
21215906Svk199839 		    " minbits=%u, maxbits=%u, ivlen=%u"),
21223055Sdanmcd 		    algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
21233055Sdanmcd 		    algs[i].sadb_alg_ivlen);
21245906Svk199839 		if (exttype == SADB_EXT_SUPPORTED_ENCRYPT)
21255906Svk199839 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
21265906Svk199839 			    ", increment=%u"), algs[i].sadb_x_alg_increment);
21275906Svk199839 		(void) fprintf(file, dgettext(TEXT_DOMAIN, ".\n"));
21283055Sdanmcd 	}
21293055Sdanmcd }
21303055Sdanmcd 
21313055Sdanmcd /*
21323055Sdanmcd  * Print an SADB_EXT_SPIRANGE extension.
21333055Sdanmcd  */
21343055Sdanmcd void
21354867Spwernau print_spirange(FILE *file, char *prefix, struct sadb_spirange *range)
21363055Sdanmcd {
21374867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
21384064Smarkfen 	    "%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
21393055Sdanmcd 	    htonl(range->sadb_spirange_min),
21403055Sdanmcd 	    htonl(range->sadb_spirange_max));
21413055Sdanmcd }
21423055Sdanmcd 
21433055Sdanmcd /*
21443055Sdanmcd  * Print an SADB_X_EXT_KM_COOKIE extension.
21453055Sdanmcd  */
21463055Sdanmcd 
21473055Sdanmcd void
21484867Spwernau print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc)
21493055Sdanmcd {
21503055Sdanmcd 	char *cookie_label;
21513055Sdanmcd 
21523055Sdanmcd 	if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) ==
21533055Sdanmcd 	    NULL)
21544064Smarkfen 		cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>");
21553055Sdanmcd 
21564867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
21574064Smarkfen 	    "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix,
21583055Sdanmcd 	    kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie);
21593055Sdanmcd }
21606668Smarkfen /*
21616668Smarkfen  * Print an SADB_X_EXT_PAIR extension.
21626668Smarkfen  */
21636668Smarkfen static void
21646668Smarkfen print_pair(FILE *file, char *prefix, struct sadb_x_pair *pair)
21656668Smarkfen {
21666668Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sPaired with spi=0x%x\n"),
21676668Smarkfen 	    prefix, ntohl(pair->sadb_x_pair_spi));
21686668Smarkfen }
21693055Sdanmcd 
21703055Sdanmcd /*
21713055Sdanmcd  * Take a PF_KEY message pointed to buffer and print it.  Useful for DUMP
21723055Sdanmcd  * and GET.
21733055Sdanmcd  */
21743055Sdanmcd void
21754867Spwernau print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp,
21764867Spwernau     boolean_t vflag, boolean_t ignore_nss)
21773055Sdanmcd {
21783055Sdanmcd 	uint64_t *current;
21793055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
21803055Sdanmcd 	struct sadb_ext *ext;
21813055Sdanmcd 	struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
21823055Sdanmcd 	int i;
21833055Sdanmcd 	time_t wallclock;
21843055Sdanmcd 
21853055Sdanmcd 	(void) time(&wallclock);
21863055Sdanmcd 
21874867Spwernau 	print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag);
21883055Sdanmcd 	current = (uint64_t *)(samsg + 1);
21893055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
21903055Sdanmcd 		int lenbytes;
21913055Sdanmcd 
21923055Sdanmcd 		ext = (struct sadb_ext *)current;
21933055Sdanmcd 		lenbytes = SADB_64TO8(ext->sadb_ext_len);
21943055Sdanmcd 		switch (ext->sadb_ext_type) {
21953055Sdanmcd 		case SADB_EXT_SA:
21964867Spwernau 			print_sa(file, dgettext(TEXT_DOMAIN,
21974064Smarkfen 			    "SA: "), (struct sadb_sa *)current);
21983055Sdanmcd 			break;
21993055Sdanmcd 		/*
22003055Sdanmcd 		 * Pluck out lifetimes and print them at the end.  This is
22013055Sdanmcd 		 * to show relative lifetimes.
22023055Sdanmcd 		 */
22033055Sdanmcd 		case SADB_EXT_LIFETIME_CURRENT:
22043055Sdanmcd 			currentlt = (struct sadb_lifetime *)current;
22053055Sdanmcd 			break;
22063055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
22073055Sdanmcd 			hardlt = (struct sadb_lifetime *)current;
22083055Sdanmcd 			break;
22093055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
22103055Sdanmcd 			softlt = (struct sadb_lifetime *)current;
22113055Sdanmcd 			break;
22123055Sdanmcd 
22133055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
22144867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "SRC: "),
22154867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22163055Sdanmcd 			break;
22173055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
22184867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "INS: "),
22194867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22203055Sdanmcd 			break;
22213055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
22224867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "DST: "),
22234867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22243055Sdanmcd 			break;
22253055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
22264867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "IND: "),
22274867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22283055Sdanmcd 			break;
22293055Sdanmcd 		case SADB_EXT_KEY_AUTH:
22304867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
22314064Smarkfen 			    "AKY: "), (struct sadb_key *)current);
22323055Sdanmcd 			break;
22333055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
22344867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
22354064Smarkfen 			    "EKY: "), (struct sadb_key *)current);
22363055Sdanmcd 			break;
22373055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
22384867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "SID: "),
22393055Sdanmcd 			    (struct sadb_ident *)current);
22403055Sdanmcd 			break;
22413055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
22424867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "DID: "),
22433055Sdanmcd 			    (struct sadb_ident *)current);
22443055Sdanmcd 			break;
22453055Sdanmcd 		case SADB_EXT_SENSITIVITY:
22464867Spwernau 			print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "),
22473055Sdanmcd 			    (struct sadb_sens *)current);
22483055Sdanmcd 			break;
22493055Sdanmcd 		case SADB_EXT_PROPOSAL:
22504867Spwernau 			print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "),
22513055Sdanmcd 			    (struct sadb_prop *)current);
22523055Sdanmcd 			break;
22533055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
22544867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "),
22553055Sdanmcd 			    (struct sadb_supported *)current);
22563055Sdanmcd 			break;
22573055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
22584867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "),
22593055Sdanmcd 			    (struct sadb_supported *)current);
22603055Sdanmcd 			break;
22613055Sdanmcd 		case SADB_EXT_SPIRANGE:
22624867Spwernau 			print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "),
22633055Sdanmcd 			    (struct sadb_spirange *)current);
22643055Sdanmcd 			break;
22653055Sdanmcd 		case SADB_X_EXT_EPROP:
22664867Spwernau 			print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "),
22673055Sdanmcd 			    (struct sadb_prop *)current);
22683055Sdanmcd 			break;
22693055Sdanmcd 		case SADB_X_EXT_KM_COOKIE:
22704867Spwernau 			print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "),
22713055Sdanmcd 			    (struct sadb_x_kmc *)current);
22723055Sdanmcd 			break;
22733055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
22744867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NRM: "),
22754867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22763055Sdanmcd 			break;
22773055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
22784867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NLC: "),
22794867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22803055Sdanmcd 			break;
22816668Smarkfen 		case SADB_X_EXT_PAIR:
22826668Smarkfen 			print_pair(file, dgettext(TEXT_DOMAIN, "OTH: "),
22836668Smarkfen 			    (struct sadb_x_pair *)current);
22846668Smarkfen 			break;
22853055Sdanmcd 		default:
22864867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22873055Sdanmcd 			    "UNK: Unknown ext. %d, len %d.\n"),
22883055Sdanmcd 			    ext->sadb_ext_type, lenbytes);
22893055Sdanmcd 			for (i = 0; i < ext->sadb_ext_len; i++)
22904867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
2291*7320Sdanmcd@sun.com 				    "UNK: 0x%" PRIx64 "\n"),
2292*7320Sdanmcd@sun.com 				    ((uint64_t *)ext)[i]);
22933055Sdanmcd 			break;
22943055Sdanmcd 		}
22953055Sdanmcd 		current += (lenbytes == 0) ?
22963055Sdanmcd 		    SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
22973055Sdanmcd 	}
22983055Sdanmcd 	/*
22993055Sdanmcd 	 * Print lifetimes NOW.
23003055Sdanmcd 	 */
23013055Sdanmcd 	if (currentlt != NULL || hardlt != NULL || softlt != NULL)
23024867Spwernau 		print_lifetimes(file, wallclock, currentlt, hardlt, softlt,
23034867Spwernau 		    vflag);
23043055Sdanmcd 
23053055Sdanmcd 	if (current - buffer != samsg->sadb_msg_len) {
23064867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
23074867Spwernau 		    "WARNING: insufficient buffer space or corrupt message."));
23083055Sdanmcd 	}
23093055Sdanmcd 
23104867Spwernau 	(void) fflush(file);	/* Make sure our message is out there. */
23113055Sdanmcd }
23123055Sdanmcd 
23133055Sdanmcd /*
23143055Sdanmcd  * save_XXX functions are used when "saving" the SA tables to either a
23153055Sdanmcd  * file or standard output.  They use the dump_XXX functions where needed,
23163055Sdanmcd  * but mostly they use the rparseXXX functions.
23173055Sdanmcd  */
23183055Sdanmcd 
23193055Sdanmcd /*
23203055Sdanmcd  * Print save information for a lifetime extension.
23213055Sdanmcd  *
23223055Sdanmcd  * NOTE : It saves the lifetime in absolute terms.  For example, if you
23233055Sdanmcd  * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
23243055Sdanmcd  * there may have been 59 seconds burned off the clock.
23253055Sdanmcd  */
23263055Sdanmcd boolean_t
23273055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
23283055Sdanmcd {
23293055Sdanmcd 	char *prefix;
23303055Sdanmcd 
23313055Sdanmcd 	prefix = (lifetime->sadb_lifetime_exttype == SADB_EXT_LIFETIME_SOFT) ?
23323055Sdanmcd 	    "soft" : "hard";
23333055Sdanmcd 
23343055Sdanmcd 	if (putc('\t', ofile) == EOF)
23353055Sdanmcd 		return (B_FALSE);
23363055Sdanmcd 
23373055Sdanmcd 	if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
23383055Sdanmcd 	    "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
23393055Sdanmcd 		return (B_FALSE);
23403055Sdanmcd 
23413055Sdanmcd 	if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
2342*7320Sdanmcd@sun.com 	    "%s_bytes %" PRIu64 " ", prefix, lifetime->sadb_lifetime_bytes) < 0)
23433055Sdanmcd 		return (B_FALSE);
23443055Sdanmcd 
23453055Sdanmcd 	if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
2346*7320Sdanmcd@sun.com 	    "%s_addtime %" PRIu64 " ", prefix,
2347*7320Sdanmcd@sun.com 	    lifetime->sadb_lifetime_addtime) < 0)
23483055Sdanmcd 		return (B_FALSE);
23493055Sdanmcd 
23503055Sdanmcd 	if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
2351*7320Sdanmcd@sun.com 	    "%s_usetime %" PRIu64 " ", prefix,
2352*7320Sdanmcd@sun.com 	    lifetime->sadb_lifetime_usetime) < 0)
23533055Sdanmcd 		return (B_FALSE);
23543055Sdanmcd 
23553055Sdanmcd 	return (B_TRUE);
23563055Sdanmcd }
23573055Sdanmcd 
23583055Sdanmcd /*
23593055Sdanmcd  * Print save information for an address extension.
23603055Sdanmcd  */
23613055Sdanmcd boolean_t
23623055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile)
23633055Sdanmcd {
23643055Sdanmcd 	char *printable_addr, buf[INET6_ADDRSTRLEN];
23653055Sdanmcd 	const char *prefix, *pprefix;
23663055Sdanmcd 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
23673055Sdanmcd 	struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
23683055Sdanmcd 	int af = sin->sin_family;
23693055Sdanmcd 
23703055Sdanmcd 	/*
23713055Sdanmcd 	 * Address-family reality check.
23723055Sdanmcd 	 */
23733055Sdanmcd 	if (af != AF_INET6 && af != AF_INET)
23743055Sdanmcd 		return (B_FALSE);
23753055Sdanmcd 
23763055Sdanmcd 	switch (addr->sadb_address_exttype) {
23773055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
23783055Sdanmcd 		prefix = "src";
23793055Sdanmcd 		pprefix = "sport";
23803055Sdanmcd 		break;
23813055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
23823055Sdanmcd 		prefix = "isrc";
23833055Sdanmcd 		pprefix = "isport";
23843055Sdanmcd 		break;
23853055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
23863055Sdanmcd 		prefix = "dst";
23873055Sdanmcd 		pprefix = "dport";
23883055Sdanmcd 		break;
23893055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
23903055Sdanmcd 		prefix = "idst";
23913055Sdanmcd 		pprefix = "idport";
23923055Sdanmcd 		break;
23933055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
23943055Sdanmcd 		prefix = "nat_loc ";
23953055Sdanmcd 		pprefix = "nat_lport";
23963055Sdanmcd 		break;
23973055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
23983055Sdanmcd 		prefix = "nat_rem ";
23993055Sdanmcd 		pprefix = "nat_rport";
24003055Sdanmcd 		break;
24013055Sdanmcd 	}
24023055Sdanmcd 
24033055Sdanmcd 	if (fprintf(ofile, "    %s ", prefix) < 0)
24043055Sdanmcd 		return (B_FALSE);
24053055Sdanmcd 
24063055Sdanmcd 	/*
24073055Sdanmcd 	 * Do not do address-to-name translation, given that we live in
24083055Sdanmcd 	 * an age of names that explode into many addresses.
24093055Sdanmcd 	 */
24103055Sdanmcd 	printable_addr = (char *)inet_ntop(af,
24113055Sdanmcd 	    (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
24123055Sdanmcd 	    buf, sizeof (buf));
24133055Sdanmcd 	if (printable_addr == NULL)
24144064Smarkfen 		printable_addr = "Invalid IP address.";
24153055Sdanmcd 	if (fprintf(ofile, "%s", printable_addr) < 0)
24163055Sdanmcd 		return (B_FALSE);
24173055Sdanmcd 	if (addr->sadb_address_prefixlen != 0 &&
24183055Sdanmcd 	    !((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
24194342Spwernau 	    (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
24203055Sdanmcd 		if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
24213055Sdanmcd 			return (B_FALSE);
24223055Sdanmcd 	}
24233055Sdanmcd 
24243055Sdanmcd 	/*
24253055Sdanmcd 	 * The port is in the same position for struct sockaddr_in and
24263055Sdanmcd 	 * struct sockaddr_in6.  We exploit that property here.
24273055Sdanmcd 	 */
24283055Sdanmcd 	if ((pprefix != NULL) && (sin->sin_port != 0))
24293055Sdanmcd 		(void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
24303055Sdanmcd 
24313055Sdanmcd 	return (B_TRUE);
24323055Sdanmcd }
24333055Sdanmcd 
24343055Sdanmcd /*
24353055Sdanmcd  * Print save information for a key extension. Returns whether writing
24363055Sdanmcd  * to the specified output file was successful or not.
24373055Sdanmcd  */
24383055Sdanmcd boolean_t
24393055Sdanmcd save_key(struct sadb_key *key, FILE *ofile)
24403055Sdanmcd {
24413055Sdanmcd 	char *prefix;
24423055Sdanmcd 
24433055Sdanmcd 	if (putc('\t', ofile) == EOF)
24443055Sdanmcd 		return (B_FALSE);
24453055Sdanmcd 
24463055Sdanmcd 	prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
24473055Sdanmcd 
24483055Sdanmcd 	if (fprintf(ofile, "%skey ", prefix) < 0)
24493055Sdanmcd 		return (B_FALSE);
24503055Sdanmcd 
24513055Sdanmcd 	if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1)
24523055Sdanmcd 		return (B_FALSE);
24533055Sdanmcd 
24543055Sdanmcd 	return (B_TRUE);
24553055Sdanmcd }
24563055Sdanmcd 
24573055Sdanmcd /*
24583055Sdanmcd  * Print save information for an identity extension.
24593055Sdanmcd  */
24603055Sdanmcd boolean_t
24613055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile)
24623055Sdanmcd {
24633055Sdanmcd 	char *prefix;
24643055Sdanmcd 
24653055Sdanmcd 	if (putc('\t', ofile) == EOF)
24663055Sdanmcd 		return (B_FALSE);
24673055Sdanmcd 
24683055Sdanmcd 	prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
24693055Sdanmcd 	    "dst";
24703055Sdanmcd 
24713055Sdanmcd 	if (fprintf(ofile, "%sidtype %s ", prefix,
24723055Sdanmcd 	    rparseidtype(ident->sadb_ident_type)) < 0)
24733055Sdanmcd 		return (B_FALSE);
24743055Sdanmcd 
24753055Sdanmcd 	if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
24763055Sdanmcd 	    ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
24774064Smarkfen 		if (fprintf(ofile, dgettext(TEXT_DOMAIN,
24784064Smarkfen 		    "<can-not-print>")) < 0)
24793055Sdanmcd 			return (B_FALSE);
24803055Sdanmcd 	} else {
24813055Sdanmcd 		if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
24823055Sdanmcd 			return (B_FALSE);
24833055Sdanmcd 	}
24843055Sdanmcd 
24853055Sdanmcd 	return (B_TRUE);
24863055Sdanmcd }
24873055Sdanmcd 
24883055Sdanmcd /*
24893055Sdanmcd  * "Save" a security association to an output file.
24903055Sdanmcd  *
24914064Smarkfen  * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
24923055Sdanmcd  * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
24933055Sdanmcd  * change them here as well.
24943055Sdanmcd  */
24953055Sdanmcd void
24963055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile)
24973055Sdanmcd {
24984064Smarkfen 	int terrno;
24994987Sdanmcd 	boolean_t seen_proto = B_FALSE, seen_iproto = B_FALSE;
25003055Sdanmcd 	uint64_t *current;
25013055Sdanmcd 	struct sadb_address *addr;
25023055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
25033055Sdanmcd 	struct sadb_ext *ext;
25043055Sdanmcd 
25054064Smarkfen #define	tidyup() \
25064064Smarkfen 	terrno = errno; (void) fclose(ofile); errno = terrno; \
25074064Smarkfen 	interactive = B_FALSE
25084064Smarkfen 
25094064Smarkfen #define	savenl() if (fputs(" \\\n", ofile) == EOF) \
25104064Smarkfen 	{ bail(dgettext(TEXT_DOMAIN, "savenl")); }
25113055Sdanmcd 
25123055Sdanmcd 	if (fputs("# begin assoc\n", ofile) == EOF)
25134064Smarkfen 		bail(dgettext(TEXT_DOMAIN,
25144064Smarkfen 		    "save_assoc: Opening comment of SA"));
25153055Sdanmcd 	if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0)
25164064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA"));
25173055Sdanmcd 	savenl();
25183055Sdanmcd 
25193055Sdanmcd 	current = (uint64_t *)(samsg + 1);
25203055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
25213055Sdanmcd 		struct sadb_sa *assoc;
25223055Sdanmcd 
25233055Sdanmcd 		ext = (struct sadb_ext *)current;
25244987Sdanmcd 		addr = (struct sadb_address *)ext;  /* Just in case... */
25253055Sdanmcd 		switch (ext->sadb_ext_type) {
25263055Sdanmcd 		case SADB_EXT_SA:
25273055Sdanmcd 			assoc = (struct sadb_sa *)ext;
25283055Sdanmcd 			if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
25293055Sdanmcd 				if (fprintf(ofile, "# WARNING: SA was dying "
25303055Sdanmcd 				    "or dead.\n") < 0) {
25314064Smarkfen 					tidyup();
25324064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
25334064Smarkfen 					    "save_assoc: fprintf not mature"));
25343055Sdanmcd 				}
25353055Sdanmcd 			}
25363055Sdanmcd 			if (fprintf(ofile, "    spi 0x%x ",
25374064Smarkfen 			    ntohl(assoc->sadb_sa_spi)) < 0) {
25384064Smarkfen 				tidyup();
25394064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
25404064Smarkfen 				    "save_assoc: fprintf spi"));
25414064Smarkfen 			}
25423055Sdanmcd 			if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
25433055Sdanmcd 				if (fprintf(ofile, "encr_alg %s ",
25443055Sdanmcd 				    rparsealg(assoc->sadb_sa_encrypt,
25454342Spwernau 				    IPSEC_PROTO_ESP)) < 0) {
25464064Smarkfen 					tidyup();
25474064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
25484064Smarkfen 					    "save_assoc: fprintf encrypt"));
25494064Smarkfen 				}
25503055Sdanmcd 			}
25513055Sdanmcd 			if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
25523055Sdanmcd 				if (fprintf(ofile, "auth_alg %s ",
25533055Sdanmcd 				    rparsealg(assoc->sadb_sa_auth,
25544342Spwernau 				    IPSEC_PROTO_AH)) < 0) {
25554064Smarkfen 					tidyup();
25564064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
25574064Smarkfen 					    "save_assoc: fprintf auth"));
25584064Smarkfen 				}
25593055Sdanmcd 			}
25603055Sdanmcd 			if (fprintf(ofile, "replay %d ",
25614064Smarkfen 			    assoc->sadb_sa_replay) < 0) {
25624064Smarkfen 				tidyup();
25634064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
25644064Smarkfen 				    "save_assoc: fprintf replay"));
25654064Smarkfen 			}
25663055Sdanmcd 			if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
25673055Sdanmcd 			    SADB_X_SAFLAGS_NATT_REM)) {
25684064Smarkfen 				if (fprintf(ofile, "encap udp") < 0) {
25694064Smarkfen 					tidyup();
25704064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
25714064Smarkfen 					    "save_assoc: fprintf encap"));
25724064Smarkfen 				}
25733055Sdanmcd 			}
25743055Sdanmcd 			savenl();
25753055Sdanmcd 			break;
25763055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
25773055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
25784064Smarkfen 			if (!save_lifetime((struct sadb_lifetime *)ext,
25794064Smarkfen 			    ofile)) {
25804064Smarkfen 				tidyup();
25814064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
25824064Smarkfen 			}
25833055Sdanmcd 			savenl();
25843055Sdanmcd 			break;
25853055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
25863055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
25874987Sdanmcd 			if (!seen_iproto && addr->sadb_address_proto) {
25884987Sdanmcd 				(void) fprintf(ofile, "    iproto %d",
25894987Sdanmcd 				    addr->sadb_address_proto);
25904987Sdanmcd 				savenl();
25914987Sdanmcd 				seen_iproto = B_TRUE;
25924987Sdanmcd 			}
25934987Sdanmcd 			goto skip_srcdst;  /* Hack to avoid cases below... */
25944987Sdanmcd 			/* FALLTHRU */
25954987Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
25964987Sdanmcd 		case SADB_EXT_ADDRESS_DST:
25973055Sdanmcd 			if (!seen_proto && addr->sadb_address_proto) {
25983055Sdanmcd 				(void) fprintf(ofile, "    proto %d",
25993055Sdanmcd 				    addr->sadb_address_proto);
26003055Sdanmcd 				savenl();
26014987Sdanmcd 				seen_proto = B_TRUE;
26023055Sdanmcd 			}
26034987Sdanmcd 			/* FALLTHRU */
26044987Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
26054987Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
26064987Sdanmcd skip_srcdst:
26074064Smarkfen 			if (!save_address(addr, ofile)) {
26084064Smarkfen 				tidyup();
26094064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
26104064Smarkfen 			}
26113055Sdanmcd 			savenl();
26123055Sdanmcd 			break;
26133055Sdanmcd 		case SADB_EXT_KEY_AUTH:
26143055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
26154064Smarkfen 			if (!save_key((struct sadb_key *)ext, ofile)) {
26164064Smarkfen 				tidyup();
26174064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
26184064Smarkfen 			}
26193055Sdanmcd 			savenl();
26203055Sdanmcd 			break;
26213055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
26223055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
26234064Smarkfen 			if (!save_ident((struct sadb_ident *)ext, ofile)) {
26244064Smarkfen 				tidyup();
26254064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
26264064Smarkfen 			}
26273055Sdanmcd 			savenl();
26283055Sdanmcd 			break;
26293055Sdanmcd 		case SADB_EXT_SENSITIVITY:
26303055Sdanmcd 		default:
26313055Sdanmcd 			/* Skip over irrelevant extensions. */
26323055Sdanmcd 			break;
26333055Sdanmcd 		}
26343055Sdanmcd 		current += ext->sadb_ext_len;
26353055Sdanmcd 	}
26363055Sdanmcd 
26374064Smarkfen 	if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
26384064Smarkfen 		tidyup();
26394064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
26404064Smarkfen 	}
26413055Sdanmcd }
26423055Sdanmcd 
26433055Sdanmcd /*
26443055Sdanmcd  * Open the output file for the "save" command.
26453055Sdanmcd  */
26463055Sdanmcd FILE *
26473055Sdanmcd opensavefile(char *filename)
26483055Sdanmcd {
26493055Sdanmcd 	int fd;
26503055Sdanmcd 	FILE *retval;
26513055Sdanmcd 	struct stat buf;
26523055Sdanmcd 
26533055Sdanmcd 	/*
26543055Sdanmcd 	 * If the user specifies "-" or doesn't give a filename, then
26553055Sdanmcd 	 * dump to stdout.  Make sure to document the dangers of files
26563055Sdanmcd 	 * that are NFS, directing your output to strange places, etc.
26573055Sdanmcd 	 */
26583055Sdanmcd 	if (filename == NULL || strcmp("-", filename) == 0)
26593055Sdanmcd 		return (stdout);
26603055Sdanmcd 
26613055Sdanmcd 	/*
26623055Sdanmcd 	 * open the file with the create bits set.  Since I check for
26633055Sdanmcd 	 * real UID == root in main(), I won't worry about the ownership
26643055Sdanmcd 	 * problem.
26653055Sdanmcd 	 */
26663055Sdanmcd 	fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
26673055Sdanmcd 	if (fd == -1) {
26683055Sdanmcd 		if (errno != EEXIST)
26694064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
26704064Smarkfen 			    "open error"),
26713055Sdanmcd 			    strerror(errno));
26723055Sdanmcd 		fd = open(filename, O_WRONLY | O_TRUNC, 0);
26733055Sdanmcd 		if (fd == -1)
26744064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
26754064Smarkfen 			    "open error"), strerror(errno));
26763055Sdanmcd 		if (fstat(fd, &buf) == -1) {
26773055Sdanmcd 			(void) close(fd);
26783055Sdanmcd 			bail_msg("%s fstat: %s", filename, strerror(errno));
26793055Sdanmcd 		}
26803055Sdanmcd 		if (S_ISREG(buf.st_mode) &&
26813055Sdanmcd 		    ((buf.st_mode & S_IAMB) != S_IRUSR)) {
26824064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
26834064Smarkfen 			    "WARNING: Save file already exists with "
26844064Smarkfen 			    "permission %o."), buf.st_mode & S_IAMB);
26854064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
26864064Smarkfen 			    "Normal users may be able to read IPsec "
26874064Smarkfen 			    "keying material."));
26883055Sdanmcd 		}
26893055Sdanmcd 	}
26903055Sdanmcd 
26913055Sdanmcd 	/* Okay, we have an FD.  Assign it to a stdio FILE pointer. */
26923055Sdanmcd 	retval = fdopen(fd, "w");
26933055Sdanmcd 	if (retval == NULL) {
26943055Sdanmcd 		(void) close(fd);
26954064Smarkfen 		bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
26964064Smarkfen 		    "fdopen error"), strerror(errno));
26973055Sdanmcd 	}
26983055Sdanmcd 	return (retval);
26993055Sdanmcd }
27003055Sdanmcd 
27013055Sdanmcd const char *
27023055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size)
27033055Sdanmcd {
27043055Sdanmcd 	boolean_t isv4;
27053055Sdanmcd 	struct in6_addr *inaddr6 = (struct in6_addr *)addr;
27063055Sdanmcd 	struct in_addr inaddr;
27073055Sdanmcd 
27083055Sdanmcd 	if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
27093055Sdanmcd 		IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
27103055Sdanmcd 	}
27113055Sdanmcd 
27123055Sdanmcd 	return (inet_ntop(isv4 ? AF_INET : AF_INET6,
27133055Sdanmcd 	    isv4 ? (void *)&inaddr : inaddr6, cp, size));
27143055Sdanmcd }
27153055Sdanmcd 
27163055Sdanmcd char numprint[NBUF_SIZE];
27173055Sdanmcd 
27183055Sdanmcd /*
27193055Sdanmcd  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
27203055Sdanmcd  */
27213055Sdanmcd static struct typetable {
27223055Sdanmcd 	char *type;
27233055Sdanmcd 	int token;
27243055Sdanmcd } type_table[] = {
27253055Sdanmcd 	{"all", SADB_SATYPE_UNSPEC},
27263055Sdanmcd 	{"ah",  SADB_SATYPE_AH},
27273055Sdanmcd 	{"esp", SADB_SATYPE_ESP},
27283055Sdanmcd 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
27293055Sdanmcd 	{NULL, 0}	/* Token value is irrelevant for this entry. */
27303055Sdanmcd };
27313055Sdanmcd 
27323055Sdanmcd char *
27333055Sdanmcd rparsesatype(int type)
27343055Sdanmcd {
27353055Sdanmcd 	struct typetable *tt = type_table;
27363055Sdanmcd 
27373055Sdanmcd 	while (tt->type != NULL && type != tt->token)
27383055Sdanmcd 		tt++;
27393055Sdanmcd 
27403055Sdanmcd 	if (tt->type == NULL) {
27413055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", type);
27423055Sdanmcd 	} else {
27433055Sdanmcd 		return (tt->type);
27443055Sdanmcd 	}
27453055Sdanmcd 
27463055Sdanmcd 	return (numprint);
27473055Sdanmcd }
27483055Sdanmcd 
27493055Sdanmcd 
27503055Sdanmcd /*
27513055Sdanmcd  * Return a string containing the name of the specified numerical algorithm
27523055Sdanmcd  * identifier.
27533055Sdanmcd  */
27543055Sdanmcd char *
27553055Sdanmcd rparsealg(uint8_t alg, int proto_num)
27563055Sdanmcd {
27573055Sdanmcd 	static struct ipsecalgent *holder = NULL; /* we're single-threaded */
27583055Sdanmcd 
27593055Sdanmcd 	if (holder != NULL)
27603055Sdanmcd 		freeipsecalgent(holder);
27613055Sdanmcd 
27623055Sdanmcd 	holder = getipsecalgbynum(alg, proto_num, NULL);
27633055Sdanmcd 	if (holder == NULL) {
27643055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
27653055Sdanmcd 		return (numprint);
27663055Sdanmcd 	}
27673055Sdanmcd 
27683055Sdanmcd 	return (*(holder->a_names));
27693055Sdanmcd }
27703055Sdanmcd 
27713055Sdanmcd /*
27723055Sdanmcd  * Parse and reverse parse out a source/destination ID type.
27733055Sdanmcd  */
27743055Sdanmcd static struct idtypes {
27753055Sdanmcd 	char *idtype;
27763055Sdanmcd 	uint8_t retval;
27773055Sdanmcd } idtypes[] = {
27783055Sdanmcd 	{"prefix",	SADB_IDENTTYPE_PREFIX},
27793055Sdanmcd 	{"fqdn",	SADB_IDENTTYPE_FQDN},
27803055Sdanmcd 	{"domain",	SADB_IDENTTYPE_FQDN},
27813055Sdanmcd 	{"domainname",	SADB_IDENTTYPE_FQDN},
27823055Sdanmcd 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
27833055Sdanmcd 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
27843055Sdanmcd 	{"der_dn",	SADB_X_IDENTTYPE_DN},
27853055Sdanmcd 	{"der_gn",	SADB_X_IDENTTYPE_GN},
27863055Sdanmcd 	{NULL,		0}
27873055Sdanmcd };
27883055Sdanmcd 
27893055Sdanmcd char *
27903055Sdanmcd rparseidtype(uint16_t type)
27913055Sdanmcd {
27923055Sdanmcd 	struct idtypes *idp;
27933055Sdanmcd 
27943055Sdanmcd 	for (idp = idtypes; idp->idtype != NULL; idp++) {
27953055Sdanmcd 		if (type == idp->retval)
27963055Sdanmcd 			return (idp->idtype);
27973055Sdanmcd 	}
27983055Sdanmcd 
27993055Sdanmcd 	(void) snprintf(numprint, NBUF_SIZE, "%d", type);
28003055Sdanmcd 	return (numprint);
28013055Sdanmcd }
28024235Smarkfen 
28034235Smarkfen /*
28044235Smarkfen  * This is a general purpose exit function, calling functions can specify an
28054235Smarkfen  * error type. If the command calling this function was started by smf(5) the
28064235Smarkfen  * error type could be used as a hint to the restarter. In the future this
28074235Smarkfen  * function could be used to do something more intelligent with a process that
28084235Smarkfen  * encounters an error.
28094235Smarkfen  *
28104235Smarkfen  * The function will handle an optional variable args error message, this
28114235Smarkfen  * will be written to the error stream, typically a log file or stderr.
28124235Smarkfen  */
28134235Smarkfen void
28144235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
28154235Smarkfen {
28164235Smarkfen 	int exit_status;
28174235Smarkfen 	va_list args;
28184235Smarkfen 
28194235Smarkfen 	if (fp == NULL)
28204235Smarkfen 		fp = stderr;
28214235Smarkfen 	if (fmt != NULL) {
28224235Smarkfen 		va_start(args, fmt);
28234235Smarkfen 		vwarnxfp(fp, fmt, args);
28244235Smarkfen 		va_end(args);
28254235Smarkfen 	}
28264235Smarkfen 
28274235Smarkfen 	if (fmri == NULL) {
28284235Smarkfen 		/* Command being run directly from a shell. */
28294235Smarkfen 		switch (type) {
28304235Smarkfen 		case SERVICE_EXIT_OK:
28314235Smarkfen 			exit_status = 0;
28324235Smarkfen 			break;
28334235Smarkfen 		case SERVICE_DEGRADE:
28344235Smarkfen 			return;
28354235Smarkfen 			break;
28364235Smarkfen 		case SERVICE_BADPERM:
28374235Smarkfen 		case SERVICE_BADCONF:
28384235Smarkfen 		case SERVICE_MAINTAIN:
28394235Smarkfen 		case SERVICE_DISABLE:
28404235Smarkfen 		case SERVICE_FATAL:
28414235Smarkfen 		case SERVICE_RESTART:
28424235Smarkfen 			warnxfp(fp, "Fatal error - exiting.");
28434235Smarkfen 			exit_status = 1;
28444235Smarkfen 			break;
28454235Smarkfen 		}
28464235Smarkfen 	} else {
28474235Smarkfen 		/* Command being run as a smf(5) method. */
28484235Smarkfen 		switch (type) {
28494235Smarkfen 		case SERVICE_EXIT_OK:
28504235Smarkfen 			exit_status = SMF_EXIT_OK;
28514235Smarkfen 			break;
28524235Smarkfen 		case SERVICE_DEGRADE:
28534235Smarkfen 			return;
28544235Smarkfen 			break;
28554235Smarkfen 		case SERVICE_BADPERM:
28564235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
28574235Smarkfen 			    "Permission error with %s."), fmri);
28584235Smarkfen 			exit_status = SMF_EXIT_ERR_PERM;
28594235Smarkfen 			break;
28604235Smarkfen 		case SERVICE_BADCONF:
28614235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
28624235Smarkfen 			    "Bad configuration of service %s."), fmri);
28634235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
28644235Smarkfen 			break;
28654235Smarkfen 		case SERVICE_MAINTAIN:
28664235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
28674235Smarkfen 			    "Service %s needs maintenance."), fmri);
28684235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
28694235Smarkfen 			break;
28704235Smarkfen 		case SERVICE_DISABLE:
28714235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
28724235Smarkfen 			break;
28734235Smarkfen 		case SERVICE_FATAL:
28744235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
28754235Smarkfen 			    "Service %s fatal error."), fmri);
28764235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
28774235Smarkfen 			break;
28784235Smarkfen 		case SERVICE_RESTART:
28794235Smarkfen 			exit_status = 1;
28804235Smarkfen 			break;
28814235Smarkfen 		}
28824235Smarkfen 	}
28834235Smarkfen 	(void) fflush(fp);
28844235Smarkfen 	(void) fclose(fp);
28854235Smarkfen 	exit(exit_status);
28864235Smarkfen }
2887