10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53055Sdanmcd  * Common Development and Distribution License (the "License").
63055Sdanmcd  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224064Smarkfen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <sys/sysconf.h>
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <sys/socket.h>
400Sstevel@tonic-gate #include <netdb.h>
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <arpa/inet.h>
430Sstevel@tonic-gate #include <net/pfkeyv2.h>
440Sstevel@tonic-gate #include <net/pfpolicy.h>
450Sstevel@tonic-gate #include <libintl.h>
460Sstevel@tonic-gate #include <setjmp.h>
470Sstevel@tonic-gate #include <libgen.h>
484235Smarkfen #include <libscf.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "ipsec_util.h"
510Sstevel@tonic-gate #include "ikedoor.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * This file contains support functions that are shared by the ipsec
55*4867Spwernau  * utilities and daemons including ipseckey(1m), ikeadm(1m) and in.iked(1m).
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
58*4867Spwernau 
59*4867Spwernau #define	EFD(file) (((file) == stdout) ? stderr : (file))
60*4867Spwernau 
610Sstevel@tonic-gate /* Set standard default/initial values for globals... */
620Sstevel@tonic-gate boolean_t pflag = B_FALSE;	/* paranoid w.r.t. printing keying material */
630Sstevel@tonic-gate boolean_t nflag = B_FALSE;	/* avoid nameservice? */
640Sstevel@tonic-gate boolean_t interactive = B_FALSE;	/* util not running on cmdline */
650Sstevel@tonic-gate boolean_t readfile = B_FALSE;	/* cmds are being read from a file */
660Sstevel@tonic-gate uint_t	lineno = 0;		/* track location if reading cmds from file */
674235Smarkfen uint_t	lines_added = 0;
684235Smarkfen uint_t	lines_parsed = 0;
690Sstevel@tonic-gate jmp_buf	env;		/* for error recovery in interactive/readfile modes */
704235Smarkfen char *my_fmri = NULL;
714235Smarkfen FILE *debugfile = stderr;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Print errno and exit if cmdline or readfile, reset state if interactive
754064Smarkfen  * The error string *what should be dgettext()'d before calling bail().
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate void
780Sstevel@tonic-gate bail(char *what)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	if (errno != 0)
810Sstevel@tonic-gate 		warn(what);
820Sstevel@tonic-gate 	else
834235Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "Error: %s"), what);
840Sstevel@tonic-gate 	if (readfile) {
854235Smarkfen 		return;
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	if (interactive && !readfile)
880Sstevel@tonic-gate 		longjmp(env, 2);
894235Smarkfen 	EXIT_FATAL(NULL);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Print caller-supplied variable-arg error msg, then exit if cmdline or
940Sstevel@tonic-gate  * readfile, or reset state if interactive.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate /*PRINTFLIKE1*/
970Sstevel@tonic-gate void
980Sstevel@tonic-gate bail_msg(char *fmt, ...)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	va_list	ap;
1010Sstevel@tonic-gate 	char	msgbuf[BUFSIZ];
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	va_start(ap, fmt);
1040Sstevel@tonic-gate 	(void) vsnprintf(msgbuf, BUFSIZ, fmt, ap);
1050Sstevel@tonic-gate 	va_end(ap);
1060Sstevel@tonic-gate 	if (readfile)
1074064Smarkfen 		warnx(dgettext(TEXT_DOMAIN,
1084064Smarkfen 		    "ERROR on line %u:\n%s\n"), lineno,  msgbuf);
1090Sstevel@tonic-gate 	else
1104064Smarkfen 		warnx(dgettext(TEXT_DOMAIN, "ERROR: %s\n"), msgbuf);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (interactive && !readfile)
1130Sstevel@tonic-gate 		longjmp(env, 1);
1140Sstevel@tonic-gate 
1154235Smarkfen 	EXIT_FATAL(NULL);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * dump_XXX functions produce ASCII output from various structures.
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * Because certain errors need to do this to stderr, dump_XXX functions
1230Sstevel@tonic-gate  * take a FILE pointer.
1240Sstevel@tonic-gate  *
1250Sstevel@tonic-gate  * If an error occured while writing to the specified file, these
1260Sstevel@tonic-gate  * functions return -1, zero otherwise.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate int
1303055Sdanmcd dump_sockaddr(struct sockaddr *sa, uint8_t prefixlen, boolean_t addr_only,
131*4867Spwernau     FILE *where, boolean_t ignore_nss)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	struct sockaddr_in	*sin;
1340Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
1350Sstevel@tonic-gate 	char			*printable_addr, *protocol;
1360Sstevel@tonic-gate 	uint8_t			*addrptr;
1373055Sdanmcd 	/* Add 4 chars to hold '/nnn' for prefixes. */
1383055Sdanmcd 	char			storage[INET6_ADDRSTRLEN + 4];
1390Sstevel@tonic-gate 	uint16_t		port;
1400Sstevel@tonic-gate 	boolean_t		unspec;
1410Sstevel@tonic-gate 	struct hostent		*hp;
1420Sstevel@tonic-gate 	int			getipnode_errno, addrlen;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	switch (sa->sa_family) {
1450Sstevel@tonic-gate 	case AF_INET:
1460Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1470Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
1480Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin->sin_addr;
1490Sstevel@tonic-gate 		port = sin->sin_port;
1500Sstevel@tonic-gate 		protocol = "AF_INET";
1510Sstevel@tonic-gate 		unspec = (sin->sin_addr.s_addr == 0);
1520Sstevel@tonic-gate 		addrlen = sizeof (sin->sin_addr);
1530Sstevel@tonic-gate 		break;
1540Sstevel@tonic-gate 	case AF_INET6:
1550Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1560Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
1570Sstevel@tonic-gate 		addrptr = (uint8_t *)&sin6->sin6_addr;
1580Sstevel@tonic-gate 		port = sin6->sin6_port;
1590Sstevel@tonic-gate 		protocol = "AF_INET6";
1600Sstevel@tonic-gate 		unspec = IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr);
1610Sstevel@tonic-gate 		addrlen = sizeof (sin6->sin6_addr);
1620Sstevel@tonic-gate 		break;
1630Sstevel@tonic-gate 	default:
1640Sstevel@tonic-gate 		return (0);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (inet_ntop(sa->sa_family, addrptr, storage, INET6_ADDRSTRLEN) ==
1680Sstevel@tonic-gate 	    NULL) {
1694064Smarkfen 		printable_addr = dgettext(TEXT_DOMAIN, "Invalid IP address.");
1700Sstevel@tonic-gate 	} else {
1713055Sdanmcd 		char prefix[5];	/* "/nnn" with terminator. */
1723055Sdanmcd 
1733055Sdanmcd 		(void) snprintf(prefix, sizeof (prefix), "/%d", prefixlen);
1740Sstevel@tonic-gate 		printable_addr = storage;
1753055Sdanmcd 		if (prefixlen != 0) {
1763055Sdanmcd 			(void) strlcat(printable_addr, prefix,
1773055Sdanmcd 			    sizeof (storage));
1783055Sdanmcd 		}
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	if (addr_only) {
1810Sstevel@tonic-gate 		if (fprintf(where, "%s", printable_addr) < 0)
1820Sstevel@tonic-gate 			return (-1);
1830Sstevel@tonic-gate 	} else {
1844064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
1854064Smarkfen 		    "%s: port %d, %s"), protocol,
1860Sstevel@tonic-gate 		    ntohs(port), printable_addr) < 0)
1870Sstevel@tonic-gate 			return (-1);
188*4867Spwernau 		if (ignore_nss == B_FALSE) {
1890Sstevel@tonic-gate 			/*
1900Sstevel@tonic-gate 			 * Do AF_independent reverse hostname lookup here.
1910Sstevel@tonic-gate 			 */
1920Sstevel@tonic-gate 			if (unspec) {
1930Sstevel@tonic-gate 				if (fprintf(where,
1944064Smarkfen 				    dgettext(TEXT_DOMAIN,
1954064Smarkfen 				    " <unspecified>")) < 0)
1960Sstevel@tonic-gate 					return (-1);
1970Sstevel@tonic-gate 			} else {
1980Sstevel@tonic-gate 				hp = getipnodebyaddr((char *)addrptr, addrlen,
1990Sstevel@tonic-gate 				    sa->sa_family, &getipnode_errno);
2000Sstevel@tonic-gate 				if (hp != NULL) {
2010Sstevel@tonic-gate 					if (fprintf(where,
2020Sstevel@tonic-gate 					    " (%s)", hp->h_name) < 0)
2030Sstevel@tonic-gate 						return (-1);
2040Sstevel@tonic-gate 					freehostent(hp);
2050Sstevel@tonic-gate 				} else {
2060Sstevel@tonic-gate 					if (fprintf(where,
2074064Smarkfen 					    dgettext(TEXT_DOMAIN,
2084064Smarkfen 					    " <unknown>")) < 0)
2090Sstevel@tonic-gate 						return (-1);
2100Sstevel@tonic-gate 				}
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 		}
2130Sstevel@tonic-gate 		if (fputs(".\n", where) == EOF)
2140Sstevel@tonic-gate 			return (-1);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	return (0);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * Dump a key and bitlen
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate int
2230Sstevel@tonic-gate dump_key(uint8_t *keyp, uint_t bitlen, FILE *where)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	int	numbytes;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	numbytes = SADB_1TO8(bitlen);
2280Sstevel@tonic-gate 	/* The & 0x7 is to check for leftover bits. */
2290Sstevel@tonic-gate 	if ((bitlen & 0x7) != 0)
2300Sstevel@tonic-gate 		numbytes++;
2310Sstevel@tonic-gate 	while (numbytes-- != 0) {
2320Sstevel@tonic-gate 		if (pflag) {
2330Sstevel@tonic-gate 			/* Print no keys if paranoid */
2340Sstevel@tonic-gate 			if (fprintf(where, "XX") < 0)
2350Sstevel@tonic-gate 				return (-1);
2360Sstevel@tonic-gate 		} else {
2370Sstevel@tonic-gate 			if (fprintf(where, "%02x", *keyp++) < 0)
2380Sstevel@tonic-gate 				return (-1);
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 	if (fprintf(where, "/%u", bitlen) < 0)
2420Sstevel@tonic-gate 		return (-1);
2430Sstevel@tonic-gate 	return (0);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * Print an authentication or encryption algorithm
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate static int
2500Sstevel@tonic-gate dump_generic_alg(uint8_t alg_num, int proto_num, FILE *where)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	struct ipsecalgent *alg;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_num, proto_num, NULL);
2550Sstevel@tonic-gate 	if (alg == NULL) {
2564064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
2574064Smarkfen 		    "<unknown %u>"), alg_num) < 0)
2580Sstevel@tonic-gate 			return (-1);
2590Sstevel@tonic-gate 		return (0);
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Special-case <none> for backward output compat.
2640Sstevel@tonic-gate 	 * Assume that SADB_AALG_NONE == SADB_EALG_NONE.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	if (alg_num == SADB_AALG_NONE) {
2674064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
2684064Smarkfen 		    "<none>"), where) == EOF)
2690Sstevel@tonic-gate 			return (-1);
2700Sstevel@tonic-gate 	} else {
2710Sstevel@tonic-gate 		if (fputs(alg->a_names[0], where) == EOF)
2720Sstevel@tonic-gate 			return (-1);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	freeipsecalgent(alg);
2760Sstevel@tonic-gate 	return (0);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate int
2800Sstevel@tonic-gate dump_aalg(uint8_t aalg, FILE *where)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	return (dump_generic_alg(aalg, IPSEC_PROTO_AH, where));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate int
2860Sstevel@tonic-gate dump_ealg(uint8_t ealg, FILE *where)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	return (dump_generic_alg(ealg, IPSEC_PROTO_ESP, where));
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * Print an SADB_IDENTTYPE string
2930Sstevel@tonic-gate  *
2940Sstevel@tonic-gate  * Also return TRUE if the actual ident may be printed, FALSE if not.
2950Sstevel@tonic-gate  *
2960Sstevel@tonic-gate  * If rc is not NULL, set its value to -1 if an error occured while writing
2970Sstevel@tonic-gate  * to the specified file, zero otherwise.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate boolean_t
3000Sstevel@tonic-gate dump_sadb_idtype(uint8_t idtype, FILE *where, int *rc)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	boolean_t canprint = B_TRUE;
3030Sstevel@tonic-gate 	int rc_val = 0;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	switch (idtype) {
3060Sstevel@tonic-gate 	case SADB_IDENTTYPE_PREFIX:
3074064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "prefix"), where) == EOF)
3080Sstevel@tonic-gate 			rc_val = -1;
3090Sstevel@tonic-gate 		break;
3100Sstevel@tonic-gate 	case SADB_IDENTTYPE_FQDN:
3114064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "FQDN"), where) == EOF)
3120Sstevel@tonic-gate 			rc_val = -1;
3130Sstevel@tonic-gate 		break;
3140Sstevel@tonic-gate 	case SADB_IDENTTYPE_USER_FQDN:
3154064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN,
3164064Smarkfen 		    "user-FQDN (mbox)"), where) == EOF)
3170Sstevel@tonic-gate 			rc_val = -1;
3180Sstevel@tonic-gate 		break;
3190Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_DN:
3204064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Distinguished Name"),
3210Sstevel@tonic-gate 		    where) == EOF)
3220Sstevel@tonic-gate 			rc_val = -1;
3230Sstevel@tonic-gate 		canprint = B_FALSE;
3240Sstevel@tonic-gate 		break;
3250Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_GN:
3264064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "ASN.1 DER Generic Name"),
3274064Smarkfen 		    where) == EOF)
3280Sstevel@tonic-gate 			rc_val = -1;
3290Sstevel@tonic-gate 		canprint = B_FALSE;
3300Sstevel@tonic-gate 		break;
3310Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_KEY_ID:
3324064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Generic key id"),
3334064Smarkfen 		    where) == EOF)
3340Sstevel@tonic-gate 			rc_val = -1;
3350Sstevel@tonic-gate 		break;
3360Sstevel@tonic-gate 	case SADB_X_IDENTTYPE_ADDR_RANGE:
3374064Smarkfen 		if (fputs(dgettext(TEXT_DOMAIN, "Address range"), where) == EOF)
3380Sstevel@tonic-gate 			rc_val = -1;
3390Sstevel@tonic-gate 		break;
3400Sstevel@tonic-gate 	default:
3414064Smarkfen 		if (fprintf(where, dgettext(TEXT_DOMAIN,
3424064Smarkfen 		    "<unknown %u>"), idtype) < 0)
3430Sstevel@tonic-gate 			rc_val = -1;
3440Sstevel@tonic-gate 		break;
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (rc != NULL)
3480Sstevel@tonic-gate 		*rc = rc_val;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	return (canprint);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * Slice an argv/argc vector from an interactive line or a read-file line.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate static int
3570Sstevel@tonic-gate create_argv(char *ibuf, int *newargc, char ***thisargv)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	unsigned int argvlen = START_ARG;
3600Sstevel@tonic-gate 	char **current;
3610Sstevel@tonic-gate 	boolean_t firstchar = B_TRUE;
3620Sstevel@tonic-gate 	boolean_t inquotes = B_FALSE;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	*thisargv = malloc(sizeof (char *) * argvlen);
3650Sstevel@tonic-gate 	if ((*thisargv) == NULL)
3660Sstevel@tonic-gate 		return (MEMORY_ALLOCATION);
3670Sstevel@tonic-gate 	current = *thisargv;
3680Sstevel@tonic-gate 	*current = NULL;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	for (; *ibuf != '\0'; ibuf++) {
3710Sstevel@tonic-gate 		if (isspace(*ibuf)) {
3720Sstevel@tonic-gate 			if (inquotes) {
3730Sstevel@tonic-gate 				continue;
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 			if (*current != NULL) {
3760Sstevel@tonic-gate 				*ibuf = '\0';
3770Sstevel@tonic-gate 				current++;
3780Sstevel@tonic-gate 				if (*thisargv + argvlen == current) {
3790Sstevel@tonic-gate 					/* Regrow ***thisargv. */
3800Sstevel@tonic-gate 					if (argvlen == TOO_MANY_ARGS) {
3810Sstevel@tonic-gate 						free(*thisargv);
3820Sstevel@tonic-gate 						return (TOO_MANY_TOKENS);
3830Sstevel@tonic-gate 					}
3840Sstevel@tonic-gate 					/* Double the allocation. */
3850Sstevel@tonic-gate 					current = realloc(*thisargv,
3860Sstevel@tonic-gate 					    sizeof (char *) * (argvlen << 1));
3870Sstevel@tonic-gate 					if (current == NULL) {
3880Sstevel@tonic-gate 						free(*thisargv);
3890Sstevel@tonic-gate 						return (MEMORY_ALLOCATION);
3900Sstevel@tonic-gate 					}
3910Sstevel@tonic-gate 					*thisargv = current;
3920Sstevel@tonic-gate 					current += argvlen;
3930Sstevel@tonic-gate 					argvlen <<= 1;	/* Double the size. */
3940Sstevel@tonic-gate 				}
3950Sstevel@tonic-gate 				*current = NULL;
3960Sstevel@tonic-gate 			}
3970Sstevel@tonic-gate 		} else {
3980Sstevel@tonic-gate 			if (firstchar) {
3990Sstevel@tonic-gate 				firstchar = B_FALSE;
4004235Smarkfen 				if (*ibuf == COMMENT_CHAR || *ibuf == '\n') {
4010Sstevel@tonic-gate 					free(*thisargv);
4020Sstevel@tonic-gate 					return (COMMENT_LINE);
4030Sstevel@tonic-gate 				}
4040Sstevel@tonic-gate 			}
4050Sstevel@tonic-gate 			if (*ibuf == QUOTE_CHAR) {
4060Sstevel@tonic-gate 				if (inquotes) {
4070Sstevel@tonic-gate 					inquotes = B_FALSE;
4080Sstevel@tonic-gate 					*ibuf = '\0';
4090Sstevel@tonic-gate 				} else {
4100Sstevel@tonic-gate 					inquotes = B_TRUE;
4110Sstevel@tonic-gate 				}
4120Sstevel@tonic-gate 				continue;
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 			if (*current == NULL) {
4150Sstevel@tonic-gate 				*current = ibuf;
4160Sstevel@tonic-gate 				(*newargc)++;
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/*
4220Sstevel@tonic-gate 	 * Tricky corner case...
4230Sstevel@tonic-gate 	 * I've parsed _exactly_ the amount of args as I have space.  It
4240Sstevel@tonic-gate 	 * won't return NULL-terminated, and bad things will happen to
4250Sstevel@tonic-gate 	 * the caller.
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	if (argvlen == *newargc) {
4280Sstevel@tonic-gate 		current = realloc(*thisargv, sizeof (char *) * (argvlen + 1));
4290Sstevel@tonic-gate 		if (current == NULL) {
4300Sstevel@tonic-gate 			free(*thisargv);
4310Sstevel@tonic-gate 			return (MEMORY_ALLOCATION);
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 		*thisargv = current;
4340Sstevel@tonic-gate 		current[argvlen] = NULL;
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	return (SUCCESS);
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate  * Enter a mode where commands are read from a file.  Treat stdin special.
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate void
4444235Smarkfen do_interactive(FILE *infile, char *configfile, char *promptstring,
4454235Smarkfen     char *my_fmri, parse_cmdln_fn parseit)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	char		ibuf[IBUF_SIZE], holder[IBUF_SIZE];
4484235Smarkfen 	char		*hptr, **thisargv, *ebuf;
4490Sstevel@tonic-gate 	int		thisargc;
4500Sstevel@tonic-gate 	boolean_t	continue_in_progress = B_FALSE;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	(void) setjmp(env);
4530Sstevel@tonic-gate 
4544235Smarkfen 	ebuf = NULL;
4550Sstevel@tonic-gate 	interactive = B_TRUE;
4560Sstevel@tonic-gate 	bzero(ibuf, IBUF_SIZE);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (infile == stdin) {
4590Sstevel@tonic-gate 		(void) printf("%s", promptstring);
4600Sstevel@tonic-gate 		(void) fflush(stdout);
4610Sstevel@tonic-gate 	} else {
4620Sstevel@tonic-gate 		readfile = B_TRUE;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	while (fgets(ibuf, IBUF_SIZE, infile) != NULL) {
4660Sstevel@tonic-gate 		if (readfile)
4670Sstevel@tonic-gate 			lineno++;
4680Sstevel@tonic-gate 		thisargc = 0;
4690Sstevel@tonic-gate 		thisargv = NULL;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		/*
4720Sstevel@tonic-gate 		 * Check byte IBUF_SIZE - 2, because byte IBUF_SIZE - 1 will
4730Sstevel@tonic-gate 		 * be null-terminated because of fgets().
4740Sstevel@tonic-gate 		 */
4750Sstevel@tonic-gate 		if (ibuf[IBUF_SIZE - 2] != '\0') {
4764235Smarkfen 			ipsecutil_exit(SERVICE_FATAL, my_fmri, debugfile,
4774235Smarkfen 			    dgettext(TEXT_DOMAIN, "Line %d too big."), lineno);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		if (!continue_in_progress) {
4810Sstevel@tonic-gate 			/* Use -2 because of \n from fgets. */
4820Sstevel@tonic-gate 			if (ibuf[strlen(ibuf) - 2] == CONT_CHAR) {
4830Sstevel@tonic-gate 				/*
4840Sstevel@tonic-gate 				 * Can use strcpy here, I've checked the
4850Sstevel@tonic-gate 				 * length already.
4860Sstevel@tonic-gate 				 */
4870Sstevel@tonic-gate 				(void) strcpy(holder, ibuf);
4880Sstevel@tonic-gate 				hptr = &(holder[strlen(holder)]);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
4910Sstevel@tonic-gate 				hptr[-2] = ' ';
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 				continue_in_progress = B_TRUE;
4940Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
4950Sstevel@tonic-gate 				continue;
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 		} else {
4980Sstevel@tonic-gate 			/* Handle continuations... */
4990Sstevel@tonic-gate 			(void) strncpy(hptr, ibuf,
5000Sstevel@tonic-gate 			    (size_t)(&(holder[IBUF_SIZE]) - hptr));
5010Sstevel@tonic-gate 			if (holder[IBUF_SIZE - 1] != '\0') {
5024235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
5034235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
5044235Smarkfen 				    "Command buffer overrun."));
5050Sstevel@tonic-gate 			}
5060Sstevel@tonic-gate 			/* Use - 2 because of \n from fgets. */
5070Sstevel@tonic-gate 			if (hptr[strlen(hptr) - 2] == CONT_CHAR) {
5080Sstevel@tonic-gate 				bzero(ibuf, IBUF_SIZE);
5090Sstevel@tonic-gate 				hptr += strlen(hptr);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 				/* Remove the CONT_CHAR from the string. */
5120Sstevel@tonic-gate 				hptr[-2] = ' ';
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 				continue;
5150Sstevel@tonic-gate 			} else {
5160Sstevel@tonic-gate 				continue_in_progress = B_FALSE;
5170Sstevel@tonic-gate 				/*
5180Sstevel@tonic-gate 				 * I've already checked the length...
5190Sstevel@tonic-gate 				 */
5200Sstevel@tonic-gate 				(void) strcpy(ibuf, holder);
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 
5244235Smarkfen 		/*
5254235Smarkfen 		 * Just in case the command fails keep a copy of the
5264235Smarkfen 		 * command buffer for diagnostic output.
5274235Smarkfen 		 */
5284235Smarkfen 		if (readfile) {
5294235Smarkfen 			/*
5304235Smarkfen 			 * The error buffer needs to be big enough to
5314235Smarkfen 			 * hold the longest command string, plus
5324235Smarkfen 			 * some extra text, see below.
5334235Smarkfen 			 */
5344235Smarkfen 			ebuf = calloc((IBUF_SIZE * 2), sizeof (char));
5354235Smarkfen 			if (ebuf == NULL) {
5364235Smarkfen 				ipsecutil_exit(SERVICE_FATAL, my_fmri,
5374235Smarkfen 				    debugfile, dgettext(TEXT_DOMAIN,
5384235Smarkfen 				    "Memory allocation error."));
5394235Smarkfen 			} else {
5404235Smarkfen 				(void) snprintf(ebuf, (IBUF_SIZE * 2),
5414235Smarkfen 				    dgettext(TEXT_DOMAIN,
5424235Smarkfen 				    "Config file entry near line %u "
5434235Smarkfen 				    "caused error(s) or warnings:\n\n%s\n\n"),
5444235Smarkfen 				    lineno, ibuf);
5454235Smarkfen 			}
5464235Smarkfen 		}
5474235Smarkfen 
5480Sstevel@tonic-gate 		switch (create_argv(ibuf, &thisargc, &thisargv)) {
5490Sstevel@tonic-gate 		case TOO_MANY_TOKENS:
5504235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5514235Smarkfen 			    dgettext(TEXT_DOMAIN, "Too many input tokens."));
5520Sstevel@tonic-gate 			break;
5530Sstevel@tonic-gate 		case MEMORY_ALLOCATION:
5544235Smarkfen 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5554235Smarkfen 			    dgettext(TEXT_DOMAIN, "Memory allocation error."));
5560Sstevel@tonic-gate 			break;
5570Sstevel@tonic-gate 		case COMMENT_LINE:
5580Sstevel@tonic-gate 			/* Comment line. */
5594235Smarkfen 			free(ebuf);
5600Sstevel@tonic-gate 			break;
5610Sstevel@tonic-gate 		default:
5624235Smarkfen 			if (thisargc != 0) {
5634235Smarkfen 				lines_parsed++;
5644235Smarkfen 				/* ebuf consumed */
5654342Spwernau 				parseit(thisargc, thisargv, ebuf, readfile);
5664235Smarkfen 			} else {
5674235Smarkfen 				free(ebuf);
5684235Smarkfen 			}
5690Sstevel@tonic-gate 			free(thisargv);
5700Sstevel@tonic-gate 			if (infile == stdin) {
5710Sstevel@tonic-gate 				(void) printf("%s", promptstring);
5720Sstevel@tonic-gate 				(void) fflush(stdout);
5730Sstevel@tonic-gate 			}
5740Sstevel@tonic-gate 			break;
5750Sstevel@tonic-gate 		}
5760Sstevel@tonic-gate 		bzero(ibuf, IBUF_SIZE);
5770Sstevel@tonic-gate 	}
5784342Spwernau 
5794342Spwernau 	/*
5804342Spwernau 	 * The following code is ipseckey specific. This should never be
5814342Spwernau 	 * used by ikeadm which also calls this function because ikeadm
5824342Spwernau 	 * only runs interactively. If this ever changes this code block
5834342Spwernau 	 * sould be revisited.
5844342Spwernau 	 */
5854342Spwernau 	if (readfile) {
5864342Spwernau 		if (lines_parsed != 0 && lines_added == 0) {
5874342Spwernau 			ipsecutil_exit(SERVICE_BADCONF, my_fmri, debugfile,
5884342Spwernau 			    dgettext(TEXT_DOMAIN, "Configuration file did not "
5894342Spwernau 			    "contain any valid SAs"));
5904342Spwernau 		}
5914342Spwernau 
5924342Spwernau 		/*
5934342Spwernau 		 * There were errors. Putting the service in maintenance mode.
5944342Spwernau 		 * When svc.startd(1M) allows services to degrade themselves,
5954342Spwernau 		 * this should be revisited.
5964342Spwernau 		 *
5974342Spwernau 		 * If this function was called from a program running as a
5984342Spwernau 		 * smf_method(5), print a warning message. Don't spew out the
5994342Spwernau 		 * errors as these will end up in the smf(5) log file which is
6004342Spwernau 		 * publically readable, the errors may contain sensitive
6014342Spwernau 		 * information.
6024342Spwernau 		 */
6034342Spwernau 		if ((lines_added < lines_parsed) && (configfile != NULL)) {
6044342Spwernau 			if (my_fmri != NULL) {
6054342Spwernau 				ipsecutil_exit(SERVICE_BADCONF, my_fmri,
6064342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
6074342Spwernau 				    "The configuration file contained %d "
6084342Spwernau 				    "errors.\n"
6094342Spwernau 				    "Manually check the configuration with:\n"
6104342Spwernau 				    "ipseckey -c %s\n"
6114342Spwernau 				    "Use svcadm(1M) to clear maintenance "
6124342Spwernau 				    "condition when errors are resolved.\n"),
6134342Spwernau 				    lines_parsed - lines_added, configfile);
6144342Spwernau 			} else {
6154342Spwernau 				EXIT_BADCONFIG(NULL);
6164342Spwernau 			}
6174342Spwernau 		} else {
6184342Spwernau 			if (my_fmri != NULL)
6194342Spwernau 				ipsecutil_exit(SERVICE_EXIT_OK, my_fmri,
6204342Spwernau 				    debugfile, dgettext(TEXT_DOMAIN,
6214342Spwernau 				    "%d actions successfully processed."),
6224342Spwernau 				    lines_added);
6234342Spwernau 		}
6244342Spwernau 	} else {
6250Sstevel@tonic-gate 		(void) putchar('\n');
6260Sstevel@tonic-gate 		(void) fflush(stdout);
6270Sstevel@tonic-gate 	}
6284235Smarkfen 	EXIT_OK(NULL);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate  * Functions to parse strings that represent a debug or privilege level.
6330Sstevel@tonic-gate  * These functions are copied from main.c and door.c in usr.lib/in.iked/common.
6340Sstevel@tonic-gate  * If this file evolves into a common library that may be used by in.iked
6350Sstevel@tonic-gate  * as well as the usr.sbin utilities, those duplicate functions should be
6360Sstevel@tonic-gate  * deleted.
6370Sstevel@tonic-gate  *
6380Sstevel@tonic-gate  * A privilege level may be represented by a simple keyword, corresponding
6390Sstevel@tonic-gate  * to one of the possible levels.  A debug level may be represented by a
6400Sstevel@tonic-gate  * series of keywords, separated by '+' or '-', indicating categories to
6410Sstevel@tonic-gate  * be added or removed from the set of categories in the debug level.
6420Sstevel@tonic-gate  * For example, +all-op corresponds to level 0xfffffffb (all flags except
6430Sstevel@tonic-gate  * for D_OP set); while p1+p2+pfkey corresponds to level 0x38.  Note that
6440Sstevel@tonic-gate  * the leading '+' is implicit; the first keyword in the list must be for
6450Sstevel@tonic-gate  * a category that is to be added.
6460Sstevel@tonic-gate  *
6470Sstevel@tonic-gate  * These parsing functions make use of a local version of strtok, strtok_d,
6480Sstevel@tonic-gate  * which includes an additional parameter, char *delim.  This param is filled
6490Sstevel@tonic-gate  * in with the character which ends the returned token.  In other words,
6500Sstevel@tonic-gate  * this version of strtok, in addition to returning the token, also returns
6510Sstevel@tonic-gate  * the single character delimiter from the original string which marked the
6520Sstevel@tonic-gate  * end of the token.
6530Sstevel@tonic-gate  */
6540Sstevel@tonic-gate static char *
6550Sstevel@tonic-gate strtok_d(char *string, const char *sepset, char *delim)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	static char	*lasts;
6580Sstevel@tonic-gate 	char		*q, *r;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/* first or subsequent call */
6610Sstevel@tonic-gate 	if (string == NULL)
6620Sstevel@tonic-gate 		string = lasts;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
6650Sstevel@tonic-gate 		return (NULL);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	q = string + strspn(string, sepset);	/* skip leading separators */
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if (*q == '\0')			/* return if no tokens remaining */
6700Sstevel@tonic-gate 		return (NULL);
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL) {		/* move past token */
6730Sstevel@tonic-gate 		lasts = 0;	/* indicate that this is last token */
6740Sstevel@tonic-gate 	} else {
6750Sstevel@tonic-gate 		*delim = *r;	/* save delimitor */
6760Sstevel@tonic-gate 		*r = '\0';
6770Sstevel@tonic-gate 		lasts = r + 1;
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 	return (q);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate static keywdtab_t	privtab[] = {
6830Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"base" },
6840Sstevel@tonic-gate 	{ IKE_PRIV_MODKEYS,	"modkeys" },
6850Sstevel@tonic-gate 	{ IKE_PRIV_KEYMAT,	"keymat" },
6860Sstevel@tonic-gate 	{ IKE_PRIV_MINIMUM,	"0" },
6870Sstevel@tonic-gate };
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate int
6900Sstevel@tonic-gate privstr2num(char *str)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate 	keywdtab_t	*pp;
6930Sstevel@tonic-gate 	char		*endp;
6940Sstevel@tonic-gate 	int		 priv;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	for (pp = privtab; pp < A_END(privtab); pp++) {
6970Sstevel@tonic-gate 		if (strcasecmp(str, pp->kw_str) == 0)
6980Sstevel@tonic-gate 			return (pp->kw_tag);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	priv = strtol(str, &endp, 0);
7020Sstevel@tonic-gate 	if (*endp == '\0')
7030Sstevel@tonic-gate 		return (priv);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	return (-1);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate static keywdtab_t	dbgtab[] = {
7090Sstevel@tonic-gate 	{ D_CERT,	"cert" },
7100Sstevel@tonic-gate 	{ D_KEY,	"key" },
7110Sstevel@tonic-gate 	{ D_OP,		"op" },
7120Sstevel@tonic-gate 	{ D_P1,		"p1" },
7130Sstevel@tonic-gate 	{ D_P1,		"phase1" },
7140Sstevel@tonic-gate 	{ D_P2,		"p2" },
7150Sstevel@tonic-gate 	{ D_P2,		"phase2" },
7160Sstevel@tonic-gate 	{ D_PFKEY,	"pfkey" },
7170Sstevel@tonic-gate 	{ D_POL,	"pol" },
7180Sstevel@tonic-gate 	{ D_POL,	"policy" },
7190Sstevel@tonic-gate 	{ D_PROP,	"prop" },
7200Sstevel@tonic-gate 	{ D_DOOR,	"door" },
7210Sstevel@tonic-gate 	{ D_CONFIG,	"config" },
7220Sstevel@tonic-gate 	{ D_ALL,	"all" },
7230Sstevel@tonic-gate 	{ 0,		"0" },
7240Sstevel@tonic-gate };
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate int
7270Sstevel@tonic-gate dbgstr2num(char *str)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	keywdtab_t	*dp;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	for (dp = dbgtab; dp < A_END(dbgtab); dp++) {
7320Sstevel@tonic-gate 		if (strcasecmp(str, dp->kw_str) == 0)
7330Sstevel@tonic-gate 			return (dp->kw_tag);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 	return (D_INVALID);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate int
7390Sstevel@tonic-gate parsedbgopts(char *optarg)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	char	*argp, *endp, op, nextop;
7420Sstevel@tonic-gate 	int	mask = 0, new;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	mask = strtol(optarg, &endp, 0);
7450Sstevel@tonic-gate 	if (*endp == '\0')
7460Sstevel@tonic-gate 		return (mask);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	op = optarg[0];
7490Sstevel@tonic-gate 	if (op != '-')
7500Sstevel@tonic-gate 		op = '+';
7510Sstevel@tonic-gate 	argp = strtok_d(optarg, "+-", &nextop);
7520Sstevel@tonic-gate 	do {
7530Sstevel@tonic-gate 		new = dbgstr2num(argp);
7540Sstevel@tonic-gate 		if (new == D_INVALID) {
7550Sstevel@tonic-gate 			/* we encountered an invalid keywd */
7560Sstevel@tonic-gate 			return (new);
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 		if (op == '+') {
7590Sstevel@tonic-gate 			mask |= new;
7600Sstevel@tonic-gate 		} else {
7610Sstevel@tonic-gate 			mask &= ~new;
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 		op = nextop;
7640Sstevel@tonic-gate 	} while ((argp = strtok_d(NULL, "+-", &nextop)) != NULL);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	return (mask);
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate /*
7710Sstevel@tonic-gate  * functions to manipulate the kmcookie-label mapping file
7720Sstevel@tonic-gate  */
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate /*
7750Sstevel@tonic-gate  * Open, lockf, fdopen the given file, returning a FILE * on success,
7760Sstevel@tonic-gate  * or NULL on failure.
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate FILE *
7790Sstevel@tonic-gate kmc_open_and_lock(char *name)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	int	fd, rtnerr;
7820Sstevel@tonic-gate 	FILE	*fp;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if ((fd = open(name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
7850Sstevel@tonic-gate 		return (NULL);
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	if (lockf(fd, F_LOCK, 0) < 0) {
7880Sstevel@tonic-gate 		return (NULL);
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	if ((fp = fdopen(fd, "a+")) == NULL) {
7910Sstevel@tonic-gate 		return (NULL);
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 	if (fseek(fp, 0, SEEK_SET) < 0) {
7940Sstevel@tonic-gate 		/* save errno in case fclose changes it */
7950Sstevel@tonic-gate 		rtnerr = errno;
7960Sstevel@tonic-gate 		(void) fclose(fp);
7970Sstevel@tonic-gate 		errno = rtnerr;
7980Sstevel@tonic-gate 		return (NULL);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 	return (fp);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate  * Extract an integer cookie and string label from a line from the
8050Sstevel@tonic-gate  * kmcookie-label file.  Return -1 on failure, 0 on success.
8060Sstevel@tonic-gate  */
8070Sstevel@tonic-gate int
8080Sstevel@tonic-gate kmc_parse_line(char *line, int *cookie, char **label)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	char	*cookiestr;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	*cookie = 0;
8130Sstevel@tonic-gate 	*label = NULL;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	cookiestr = strtok(line, " \t\n");
8160Sstevel@tonic-gate 	if (cookiestr == NULL) {
8170Sstevel@tonic-gate 		return (-1);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/* Everything that follows, up to the newline, is the label. */
8210Sstevel@tonic-gate 	*label = strtok(NULL, "\n");
8220Sstevel@tonic-gate 	if (*label == NULL) {
8230Sstevel@tonic-gate 		return (-1);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	*cookie = atoi(cookiestr);
8270Sstevel@tonic-gate 	return (0);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*
8310Sstevel@tonic-gate  * Insert a mapping into the file (if it's not already there), given the
8320Sstevel@tonic-gate  * new label.  Return the assigned cookie, or -1 on error.
8330Sstevel@tonic-gate  */
8340Sstevel@tonic-gate int
8350Sstevel@tonic-gate kmc_insert_mapping(char *label)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	FILE	*map;
8384757Sdanmcd 	char	linebuf[IBUF_SIZE];
8390Sstevel@tonic-gate 	char	*cur_label;
8400Sstevel@tonic-gate 	int	max_cookie = 0, cur_cookie, rtn_cookie;
8410Sstevel@tonic-gate 	int	rtnerr = 0;
8420Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	/* open and lock the file; will sleep until lock is available */
8450Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
8460Sstevel@tonic-gate 		/* kmc_open_and_lock() sets errno appropriately */
8470Sstevel@tonic-gate 		return (-1);
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
8510Sstevel@tonic-gate 
8524757Sdanmcd 		/* Skip blank lines, which often come near EOF. */
8534757Sdanmcd 		if (strlen(linebuf) == 0)
8544757Sdanmcd 			continue;
8554757Sdanmcd 
8560Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
8570Sstevel@tonic-gate 			rtnerr = EINVAL;
8580Sstevel@tonic-gate 			goto error;
8590Sstevel@tonic-gate 		}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 		if (cur_cookie > max_cookie)
8620Sstevel@tonic-gate 			max_cookie = cur_cookie;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		if ((!found) && (strcmp(cur_label, label) == 0)) {
8650Sstevel@tonic-gate 			found = B_TRUE;
8660Sstevel@tonic-gate 			rtn_cookie = cur_cookie;
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	if (!found) {
8710Sstevel@tonic-gate 		rtn_cookie = ++max_cookie;
8720Sstevel@tonic-gate 		if ((fprintf(map, "%u\t%s\n", rtn_cookie, label) < 0) ||
8730Sstevel@tonic-gate 		    (fflush(map) < 0)) {
8740Sstevel@tonic-gate 			rtnerr = errno;
8750Sstevel@tonic-gate 			goto error;
8760Sstevel@tonic-gate 		}
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 	(void) fclose(map);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	return (rtn_cookie);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate error:
8830Sstevel@tonic-gate 	(void) fclose(map);
8840Sstevel@tonic-gate 	errno = rtnerr;
8850Sstevel@tonic-gate 	return (-1);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*
8890Sstevel@tonic-gate  * Lookup the given cookie and return its corresponding label.  Return
8900Sstevel@tonic-gate  * a pointer to the label on success, NULL on error (or if the label is
8910Sstevel@tonic-gate  * not found).  Note that the returned label pointer points to a static
8920Sstevel@tonic-gate  * string, so the label will be overwritten by a subsequent call to the
8930Sstevel@tonic-gate  * function; the function is also not thread-safe as a result.
8940Sstevel@tonic-gate  */
8950Sstevel@tonic-gate char *
8960Sstevel@tonic-gate kmc_lookup_by_cookie(int cookie)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate 	FILE		*map;
8994757Sdanmcd 	static char	linebuf[IBUF_SIZE];
9000Sstevel@tonic-gate 	char		*cur_label;
9010Sstevel@tonic-gate 	int		cur_cookie;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	if ((map = kmc_open_and_lock(KMCFILE)) == NULL) {
9040Sstevel@tonic-gate 		return (NULL);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	while (fgets(linebuf, sizeof (linebuf), map) != NULL) {
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		if (kmc_parse_line(linebuf, &cur_cookie, &cur_label) < 0) {
9100Sstevel@tonic-gate 			(void) fclose(map);
9110Sstevel@tonic-gate 			return (NULL);
9120Sstevel@tonic-gate 		}
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 		if (cookie == cur_cookie) {
9150Sstevel@tonic-gate 			(void) fclose(map);
9160Sstevel@tonic-gate 			return (cur_label);
9170Sstevel@tonic-gate 		}
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate 	(void) fclose(map);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	return (NULL);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate /*
9250Sstevel@tonic-gate  * Parse basic extension headers and return in the passed-in pointer vector.
9260Sstevel@tonic-gate  * Return values include:
9270Sstevel@tonic-gate  *
9280Sstevel@tonic-gate  *	KGE_OK	Everything's nice and parsed out.
9290Sstevel@tonic-gate  *		If there are no extensions, place NULL in extv[0].
9300Sstevel@tonic-gate  *	KGE_DUP	There is a duplicate extension.
9310Sstevel@tonic-gate  *		First instance in appropriate bin.  First duplicate in
9320Sstevel@tonic-gate  *		extv[0].
9330Sstevel@tonic-gate  *	KGE_UNK	Unknown extension type encountered.  extv[0] contains
9340Sstevel@tonic-gate  *		unknown header.
9350Sstevel@tonic-gate  *	KGE_LEN	Extension length error.
9360Sstevel@tonic-gate  *	KGE_CHK	High-level reality check failed on specific extension.
9370Sstevel@tonic-gate  *
9380Sstevel@tonic-gate  * My apologies for some of the pointer arithmetic in here.  I'm thinking
9390Sstevel@tonic-gate  * like an assembly programmer, yet trying to make the compiler happy.
9400Sstevel@tonic-gate  */
9410Sstevel@tonic-gate int
9420Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize,
9430Sstevel@tonic-gate     char *diag_buf, uint_t diag_buf_len)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	int i;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	if (diag_buf != NULL)
9480Sstevel@tonic-gate 		diag_buf[0] = '\0';
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	for (i = 1; i <= SPD_EXT_MAX; i++)
9510Sstevel@tonic-gate 		extv[i] = NULL;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	i = 0;
9540Sstevel@tonic-gate 	/* Use extv[0] as the "current working pointer". */
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	extv[0] = (spd_ext_t *)(basehdr + 1);
9570Sstevel@tonic-gate 	msgsize = SPD_64TO8(msgsize);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	while ((char *)extv[0] < ((char *)basehdr + msgsize)) {
9600Sstevel@tonic-gate 		/* Check for unknown headers. */
9610Sstevel@tonic-gate 		i++;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 		if (extv[0]->spd_ext_type == 0 ||
9640Sstevel@tonic-gate 		    extv[0]->spd_ext_type > SPD_EXT_MAX) {
9650Sstevel@tonic-gate 			if (diag_buf != NULL) {
9660Sstevel@tonic-gate 				(void) snprintf(diag_buf, diag_buf_len,
9670Sstevel@tonic-gate 				    "spdsock ext 0x%X unknown: 0x%X",
9680Sstevel@tonic-gate 				    i, extv[0]->spd_ext_type);
9690Sstevel@tonic-gate 			}
9700Sstevel@tonic-gate 			return (KGE_UNK);
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 		/*
9740Sstevel@tonic-gate 		 * Check length.  Use uint64_t because extlen is in units
9750Sstevel@tonic-gate 		 * of 64-bit words.  If length goes beyond the msgsize,
9760Sstevel@tonic-gate 		 * return an error.  (Zero length also qualifies here.)
9770Sstevel@tonic-gate 		 */
9780Sstevel@tonic-gate 		if (extv[0]->spd_ext_len == 0 ||
9790Sstevel@tonic-gate 		    (uint8_t *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
9800Sstevel@tonic-gate 		    (uint8_t *)((uint8_t *)basehdr + msgsize))
9810Sstevel@tonic-gate 			return (KGE_LEN);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		/* Check for redundant headers. */
9840Sstevel@tonic-gate 		if (extv[extv[0]->spd_ext_type] != NULL)
9850Sstevel@tonic-gate 			return (KGE_DUP);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		/* If I make it here, assign the appropriate bin. */
9880Sstevel@tonic-gate 		extv[extv[0]->spd_ext_type] = extv[0];
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 		/* Advance pointer (See above for uint64_t ptr reasoning.) */
9910Sstevel@tonic-gate 		extv[0] = (spd_ext_t *)
9920Sstevel@tonic-gate 		    ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	/* Everything's cool. */
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * If extv[0] == NULL, then there are no extension headers in this
9990Sstevel@tonic-gate 	 * message.  Ensure that this is the case.
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate 	if (extv[0] == (spd_ext_t *)(basehdr + 1))
10020Sstevel@tonic-gate 		extv[0] = NULL;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	return (KGE_OK);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate const char *
10080Sstevel@tonic-gate spdsock_diag(int diagnostic)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	switch (diagnostic) {
10110Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NONE:
10124064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no error"));
10130Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNKNOWN_EXT:
10144064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown extension"));
10150Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_EXTLEN:
10164064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad extension length"));
10170Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_RULE_EXT:
10184064Smarkfen 		return (dgettext(TEXT_DOMAIN, "no rule extension"));
10190Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_ADDR_LEN:
10204064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad address len"));
10210Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MIXED_AF:
10224064Smarkfen 		return (dgettext(TEXT_DOMAIN, "mixed address family"));
10230Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_NO_MEM:
10244064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: no memory"));
10250Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT:
10264064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: wrong action count"));
10270Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_TYPE:
10284064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad type"));
10290Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_BAD_FLAGS:
10304064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: bad flags"));
10310Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ADD_INCON_FLAGS:
10324064Smarkfen 		return (dgettext(TEXT_DOMAIN, "add: inconsistent flags"));
10330Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLPORT:
10344064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local port"));
10350Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLPORT:
10364064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local port"));
10370Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMPORT:
10384064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote port"));
10390Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMPORT:
10404064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote port"));
10410Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_PROTO:
10424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed proto"));
10430Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_PROTO:
10444064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate proto"));
10450Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_LCLADDR:
10464064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed local address"));
10470Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_LCLADDR:
10484064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate local address"));
10490Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_REMADDR:
10504064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed remote address"));
10510Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_REMADDR:
10524064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate remote address"));
10530Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_ACTION:
10544064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed action"));
10550Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_ACTION:
10564064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate action"));
10570Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULE:
10584064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed rule"));
10590Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULE:
10604064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate rule"));
10610Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_MALFORMED_RULESET:
10624064Smarkfen 		return (dgettext(TEXT_DOMAIN, "malformed ruleset"));
10630Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_DUPLICATE_RULESET:
10644064Smarkfen 		return (dgettext(TEXT_DOMAIN, "duplicate ruleset"));
10650Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_INVALID_RULE_INDEX:
10664064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid rule index"));
10670Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_SPDID:
10684064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad spdid"));
10690Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_BAD_MSG_TYPE:
10704064Smarkfen 		return (dgettext(TEXT_DOMAIN, "bad message type"));
10710Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_ALG:
10724064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH algorithm"));
10730Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG:
10744064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10754064Smarkfen 		    "unsupported ESP encryption algorithm"));
10760Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG:
10774064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10784064Smarkfen 		    "unsupported ESP authentication algorithm"));
10790Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_AH_KEYSIZE:
10804064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unsupported AH key size"));
10810Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_KEYSIZE:
10824064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10834064Smarkfen 		    "unsupported ESP encryption key size"));
10840Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_KEYSIZE:
10854064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10864064Smarkfen 		    "unsupported ESP authentication key size"));
10870Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_NO_ACTION_EXT:
10884064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No ACTION extension"));
10890Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_ID_RANGE:
10904064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid algorithm identifer"));
10910Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES:
10924064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10934064Smarkfen 		    "number of key sizes inconsistent"));
10940Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES:
10954064Smarkfen 		return (dgettext(TEXT_DOMAIN,
10964064Smarkfen 		    "number of block sizes inconsistent"));
10970Sstevel@tonic-gate 	case SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN:
10984064Smarkfen 		return (dgettext(TEXT_DOMAIN, "invalid mechanism name length"));
10993055Sdanmcd 	case SPD_DIAGNOSTIC_NOT_GLOBAL_OP:
11004064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11014064Smarkfen 		    "operation not applicable to all policies"));
11023055Sdanmcd 	case SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS:
11034064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11044064Smarkfen 		    "using selectors on a transport-mode tunnel"));
11050Sstevel@tonic-gate 	default:
11064064Smarkfen 		return (dgettext(TEXT_DOMAIN, "unknown diagnostic"));
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate /*
11110Sstevel@tonic-gate  * PF_KEY Diagnostic table.
11120Sstevel@tonic-gate  *
11130Sstevel@tonic-gate  * PF_KEY NOTE:  If you change pfkeyv2.h's SADB_X_DIAGNOSTIC_* space, this is
11140Sstevel@tonic-gate  * where you need to add new messages.
11150Sstevel@tonic-gate  */
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate const char *
11180Sstevel@tonic-gate keysock_diag(int diagnostic)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	switch (diagnostic) {
11213055Sdanmcd 	case SADB_X_DIAGNOSTIC_NONE:
11224064Smarkfen 		return (dgettext(TEXT_DOMAIN, "No diagnostic"));
11230Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_MSG:
11244064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown message type"));
11250Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_EXT:
11264064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown extension type"));
11270Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EXTLEN:
11284064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad extension length"));
11290Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE:
11304064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11314064Smarkfen 		    "Unknown Security Association type"));
11320Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SATYPE_NEEDED:
11334064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11344064Smarkfen 		    "Specific Security Association type needed"));
11350Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_SADBS:
11364064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11374064Smarkfen 		    "No Security Association Databases present"));
11380Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_NO_EXT:
11394064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11404064Smarkfen 		    "No extensions needed for message"));
11410Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC_AF:
11424064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address family"));
11430Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST_AF:
11444064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11454064Smarkfen 		    "Bad destination address family"));
11460Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_PROXY_AF:
11474064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11484064Smarkfen 		    "Bad inner-source address family"));
11490Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AF_MISMATCH:
11504064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11514064Smarkfen 		    "Source/destination address family mismatch"));
11520Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SRC:
11534064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad source address value"));
11540Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_DST:
11554064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Bad destination address value"));
11560Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ALLOC_HSERR:
11574064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11584064Smarkfen 		    "Soft allocations limit more than hard limit"));
11590Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BYTES_HSERR:
11604064Smarkfen 		return (dgettext(TEXT_DOMAIN,
11614064Smarkfen 		    "Soft bytes limit more than hard limit"));
11620Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ADDTIME_HSERR:
11634064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft add expiration time later "
11640Sstevel@tonic-gate 		    "than hard expiration time"));
11650Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_USETIME_HSERR:
11664064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Soft use expiration time later "
11670Sstevel@tonic-gate 		    "than hard expiration time"));
11680Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SRC:
11694064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing source address"));
11700Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_DST:
11714064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing destination address"));
11720Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_SA:
11734064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SA extension"));
11740Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_EKEY:
11754064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing encryption key"));
11760Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_AKEY:
11774064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing authentication key"));
11780Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_RANGE:
11794064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing SPI range"));
11800Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SRC:
11814064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate source address"));
11820Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_DST:
11834064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate destination address"));
11840Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_SA:
11854064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SA extension"));
11860Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_EKEY:
11874064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate encryption key"));
11880Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_AKEY:
11894064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate authentication key"));
11900Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_RANGE:
11914064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate SPI range"));
11920Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SRC:
11934064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed source address"));
11940Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_DST:
11954064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed destination address"));
11960Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_SA:
11974064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SA extension"));
11980Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_EKEY:
11994064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed encryption key"));
12000Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_AKEY:
12014064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed authentication key"));
12020Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_RANGE:
12034064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed SPI range"));
12040Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_AKEY_PRESENT:
12054064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Authentication key not needed"));
12060Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_EKEY_PRESENT:
12074064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Encryption key not needed"));
12080Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_PROP_PRESENT:
12094064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Proposal extension not needed"));
12100Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_SUPP_PRESENT:
12114064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12124064Smarkfen 		    "Supported algorithms extension not needed"));
12130Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AALG:
12144064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12154064Smarkfen 		    "Unsupported authentication algorithm"));
12160Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EALG:
12174064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12184064Smarkfen 		    "Unsupported encryption algorithm"));
12190Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SAFLAGS:
12204064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA flags"));
12210Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_SASTATE:
12224064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Invalid SA state"));
12230Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_AKEYBITS:
12244064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12254064Smarkfen 		    "Bad number of authentication bits"));
12260Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_BAD_EKEYBITS:
12274064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12284064Smarkfen 		    "Bad number of encryption bits"));
12290Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_ENCR_NOTSUPP:
12304064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12314064Smarkfen 		    "Encryption not supported for this SA type"));
12320Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_EKEY:
12334064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak encryption key"));
12340Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_WEAK_AKEY:
12354064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Weak authentication key"));
12360Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMP:
12374064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12384064Smarkfen 		    "Duplicate key management protocol"));
12390Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_KMC:
12404064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12414064Smarkfen 		    "Duplicate key management cookie"));
12420Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_LOC:
12434064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T local address"));
12440Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MISSING_NATT_REM:
12454064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing NAT-T remote address"));
12460Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_LOC:
12474064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T local address"));
12480Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_REM:
12494064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12504064Smarkfen 		    "Duplicate NAT-T remote address"));
12510Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC:
12524064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Malformed NAT-T local address"));
12530Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM:
12544064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12554064Smarkfen 		    "Malformed NAT-T remote address"));
12560Sstevel@tonic-gate 	case SADB_X_DIAGNOSTIC_DUPLICATE_NATT_PORTS:
12574064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Duplicate NAT-T ports"));
12583055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_SRC:
12594064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Missing inner source address"));
12603055Sdanmcd 	case SADB_X_DIAGNOSTIC_MISSING_INNER_DST:
12614064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12624064Smarkfen 		    "Missing inner destination address"));
12633055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC:
12644064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12654064Smarkfen 		    "Duplicate inner source address"));
12663055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST:
12674064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12684064Smarkfen 		    "Duplicate inner destination address"));
12693055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC:
12704064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12714064Smarkfen 		    "Malformed inner source address"));
12723055Sdanmcd 	case SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST:
12734064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12744064Smarkfen 		    "Malformed inner destination address"));
12753055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC:
12764064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12774064Smarkfen 		    "Invalid inner-source prefix length "));
12783055Sdanmcd 	case SADB_X_DIAGNOSTIC_PREFIX_INNER_DST:
12794064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12804064Smarkfen 		    "Invalid inner-destination prefix length"));
12813055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF:
12824064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12834064Smarkfen 		    "Bad inner-destination address family"));
12843055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH:
12854064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12863055Sdanmcd 		    "Inner source/destination address family mismatch"));
12873055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF:
12884064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12894064Smarkfen 		    "Bad NAT-T remote address family"));
12903055Sdanmcd 	case SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF:
12914064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12924064Smarkfen 		    "Bad NAT-T local address family"));
12933055Sdanmcd 	case SADB_X_DIAGNOSTIC_PROTO_MISMATCH:
12944064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12954064Smarkfen 		    "Source/desination protocol mismatch"));
12963055Sdanmcd 	case SADB_X_DIAGNOSTIC_INNER_PROTO_MISMATCH:
12974064Smarkfen 		return (dgettext(TEXT_DOMAIN,
12984064Smarkfen 		    "Inner source/desination protocol mismatch"));
12993055Sdanmcd 	case SADB_X_DIAGNOSTIC_DUAL_PORT_SETS:
13004064Smarkfen 		return (dgettext(TEXT_DOMAIN,
13014064Smarkfen 		    "Both inner ports and outer ports are set"));
13020Sstevel@tonic-gate 	default:
13034064Smarkfen 		return (dgettext(TEXT_DOMAIN, "Unknown diagnostic code"));
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate }
13063055Sdanmcd 
13073055Sdanmcd /*
13083055Sdanmcd  * Convert an IPv6 mask to a prefix len.  I assume all IPv6 masks are
13093055Sdanmcd  * contiguous, so I stop at the first zero bit!
13103055Sdanmcd  */
13113055Sdanmcd int
13123055Sdanmcd in_masktoprefix(uint8_t *mask, boolean_t is_v4mapped)
13133055Sdanmcd {
13143055Sdanmcd 	int rc = 0;
13153055Sdanmcd 	uint8_t last;
13163055Sdanmcd 	int limit = IPV6_ABITS;
13173055Sdanmcd 
13183055Sdanmcd 	if (is_v4mapped) {
13193055Sdanmcd 		mask += ((IPV6_ABITS - IP_ABITS)/8);
13203055Sdanmcd 		limit = IP_ABITS;
13213055Sdanmcd 	}
13223055Sdanmcd 
13233055Sdanmcd 	while (*mask == 0xff) {
13243055Sdanmcd 		rc += 8;
13253055Sdanmcd 		if (rc == limit)
13263055Sdanmcd 			return (limit);
13273055Sdanmcd 		mask++;
13283055Sdanmcd 	}
13293055Sdanmcd 
13303055Sdanmcd 	last = *mask;
13313055Sdanmcd 	while (last != 0) {
13323055Sdanmcd 		rc++;
13333055Sdanmcd 		last = (last << 1) & 0xff;
13343055Sdanmcd 	}
13353055Sdanmcd 
13363055Sdanmcd 	return (rc);
13373055Sdanmcd }
13383055Sdanmcd 
13393055Sdanmcd /*
13403055Sdanmcd  * Expand the diagnostic code into a message.
13413055Sdanmcd  */
13423055Sdanmcd void
13433055Sdanmcd print_diagnostic(FILE *file, uint16_t diagnostic)
13443055Sdanmcd {
13453055Sdanmcd 	/* Use two spaces so above strings can fit on the line. */
13464064Smarkfen 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
13474064Smarkfen 	    "  Diagnostic code %u:  %s.\n"),
13483055Sdanmcd 	    diagnostic, keysock_diag(diagnostic));
13493055Sdanmcd }
13503055Sdanmcd 
13513055Sdanmcd /*
13523055Sdanmcd  * Prints the base PF_KEY message.
13533055Sdanmcd  */
13543055Sdanmcd void
1355*4867Spwernau print_sadb_msg(FILE *file, struct sadb_msg *samsg, time_t wallclock,
1356*4867Spwernau     boolean_t vflag)
13573055Sdanmcd {
13583055Sdanmcd 	if (wallclock != 0)
1359*4867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
13604064Smarkfen 		    "%sTimestamp: %s\n"), "", NULL,
13613055Sdanmcd 		    vflag);
13623055Sdanmcd 
1363*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
1364*4867Spwernau 	    "Base message (version %u) type "),
13653055Sdanmcd 	    samsg->sadb_msg_version);
13663055Sdanmcd 	switch (samsg->sadb_msg_type) {
13673055Sdanmcd 	case SADB_RESERVED:
1368*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
13694064Smarkfen 		    "RESERVED (warning: set to 0)"));
13703055Sdanmcd 		break;
13713055Sdanmcd 	case SADB_GETSPI:
1372*4867Spwernau 		(void) fprintf(file, "GETSPI");
13733055Sdanmcd 		break;
13743055Sdanmcd 	case SADB_UPDATE:
1375*4867Spwernau 		(void) fprintf(file, "UPDATE");
13763055Sdanmcd 		break;
13773055Sdanmcd 	case SADB_ADD:
1378*4867Spwernau 		(void) fprintf(file, "ADD");
13793055Sdanmcd 		break;
13803055Sdanmcd 	case SADB_DELETE:
1381*4867Spwernau 		(void) fprintf(file, "DELETE");
13823055Sdanmcd 		break;
13833055Sdanmcd 	case SADB_GET:
1384*4867Spwernau 		(void) fprintf(file, "GET");
13853055Sdanmcd 		break;
13863055Sdanmcd 	case SADB_ACQUIRE:
1387*4867Spwernau 		(void) fprintf(file, "ACQUIRE");
13883055Sdanmcd 		break;
13893055Sdanmcd 	case SADB_REGISTER:
1390*4867Spwernau 		(void) fprintf(file, "REGISTER");
13913055Sdanmcd 		break;
13923055Sdanmcd 	case SADB_EXPIRE:
1393*4867Spwernau 		(void) fprintf(file, "EXPIRE");
13943055Sdanmcd 		break;
13953055Sdanmcd 	case SADB_FLUSH:
1396*4867Spwernau 		(void) fprintf(file, "FLUSH");
13973055Sdanmcd 		break;
13983055Sdanmcd 	case SADB_DUMP:
1399*4867Spwernau 		(void) fprintf(file, "DUMP");
14003055Sdanmcd 		break;
14013055Sdanmcd 	case SADB_X_PROMISC:
1402*4867Spwernau 		(void) fprintf(file, "X_PROMISC");
14033055Sdanmcd 		break;
14043055Sdanmcd 	case SADB_X_INVERSE_ACQUIRE:
1405*4867Spwernau 		(void) fprintf(file, "X_INVERSE_ACQUIRE");
14063055Sdanmcd 		break;
14073055Sdanmcd 	default:
1408*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14094064Smarkfen 		    "Unknown (%u)"), samsg->sadb_msg_type);
14103055Sdanmcd 		break;
14113055Sdanmcd 	}
1412*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ", SA type "));
14133055Sdanmcd 
14143055Sdanmcd 	switch (samsg->sadb_msg_satype) {
14153055Sdanmcd 	case SADB_SATYPE_UNSPEC:
1416*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1417*4867Spwernau 		    "<unspecified/all>"));
14183055Sdanmcd 		break;
14193055Sdanmcd 	case SADB_SATYPE_AH:
1420*4867Spwernau 		(void) fprintf(file, "AH");
14213055Sdanmcd 		break;
14223055Sdanmcd 	case SADB_SATYPE_ESP:
1423*4867Spwernau 		(void) fprintf(file, "ESP");
14243055Sdanmcd 		break;
14253055Sdanmcd 	case SADB_SATYPE_RSVP:
1426*4867Spwernau 		(void) fprintf(file, "RSVP");
14273055Sdanmcd 		break;
14283055Sdanmcd 	case SADB_SATYPE_OSPFV2:
1429*4867Spwernau 		(void) fprintf(file, "OSPFv2");
14303055Sdanmcd 		break;
14313055Sdanmcd 	case SADB_SATYPE_RIPV2:
1432*4867Spwernau 		(void) fprintf(file, "RIPv2");
14333055Sdanmcd 		break;
14343055Sdanmcd 	case SADB_SATYPE_MIP:
1435*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Mobile IP"));
14363055Sdanmcd 		break;
14373055Sdanmcd 	default:
1438*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14394064Smarkfen 		    "<unknown %u>"), samsg->sadb_msg_satype);
14403055Sdanmcd 		break;
14413055Sdanmcd 	}
14423055Sdanmcd 
1443*4867Spwernau 	(void) fprintf(file, ".\n");
14443055Sdanmcd 
14453055Sdanmcd 	if (samsg->sadb_msg_errno != 0) {
1446*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1447*4867Spwernau 		    "Error %s from PF_KEY.\n"),
14483055Sdanmcd 		    strerror(samsg->sadb_msg_errno));
1449*4867Spwernau 		print_diagnostic(file, samsg->sadb_x_msg_diagnostic);
14503055Sdanmcd 	}
14513055Sdanmcd 
1452*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
14534064Smarkfen 	    "Message length %u bytes, seq=%u, pid=%u.\n"),
14543055Sdanmcd 	    SADB_64TO8(samsg->sadb_msg_len), samsg->sadb_msg_seq,
14553055Sdanmcd 	    samsg->sadb_msg_pid);
14563055Sdanmcd }
14573055Sdanmcd 
14583055Sdanmcd /*
14593055Sdanmcd  * Print the SA extension for PF_KEY.
14603055Sdanmcd  */
14613055Sdanmcd void
1462*4867Spwernau print_sa(FILE *file, char *prefix, struct sadb_sa *assoc)
14633055Sdanmcd {
14643055Sdanmcd 	if (assoc->sadb_sa_len != SADB_8TO64(sizeof (*assoc))) {
1465*4867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
14664064Smarkfen 		    "WARNING: SA info extension length (%u) is bad."),
14673055Sdanmcd 		    SADB_64TO8(assoc->sadb_sa_len));
14683055Sdanmcd 	}
14693055Sdanmcd 
1470*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
14714064Smarkfen 	    "%sSADB_ASSOC spi=0x%x, replay=%u, state="),
14723055Sdanmcd 	    prefix, ntohl(assoc->sadb_sa_spi), assoc->sadb_sa_replay);
14733055Sdanmcd 	switch (assoc->sadb_sa_state) {
14743055Sdanmcd 	case SADB_SASTATE_LARVAL:
1475*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "LARVAL"));
14763055Sdanmcd 		break;
14773055Sdanmcd 	case SADB_SASTATE_MATURE:
1478*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "MATURE"));
14793055Sdanmcd 		break;
14803055Sdanmcd 	case SADB_SASTATE_DYING:
1481*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DYING"));
14823055Sdanmcd 		break;
14833055Sdanmcd 	case SADB_SASTATE_DEAD:
1484*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "DEAD"));
14853055Sdanmcd 		break;
14863055Sdanmcd 	default:
1487*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14884064Smarkfen 		    "<unknown %u>"), assoc->sadb_sa_state);
14893055Sdanmcd 	}
14903055Sdanmcd 
14913055Sdanmcd 	if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
1492*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
14934064Smarkfen 		    "\n%sAuthentication algorithm = "),
14943055Sdanmcd 		    prefix);
1495*4867Spwernau 		(void) dump_aalg(assoc->sadb_sa_auth, file);
14963055Sdanmcd 	}
14973055Sdanmcd 
14983055Sdanmcd 	if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
1499*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15004064Smarkfen 		    "\n%sEncryption algorithm = "), prefix);
1501*4867Spwernau 		(void) dump_ealg(assoc->sadb_sa_encrypt, file);
15023055Sdanmcd 	}
15033055Sdanmcd 
1504*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%sflags=0x%x < "), prefix,
15053055Sdanmcd 	    assoc->sadb_sa_flags);
15063055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_PFS)
1507*4867Spwernau 		(void) fprintf(file, "PFS ");
15083055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_SAFLAGS_NOREPLAY)
1509*4867Spwernau 		(void) fprintf(file, "NOREPLAY ");
15103055Sdanmcd 
15113055Sdanmcd 	/* BEGIN Solaris-specific flags. */
15123055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_USED)
1513*4867Spwernau 		(void) fprintf(file, "X_USED ");
15143055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_UNIQUE)
1515*4867Spwernau 		(void) fprintf(file, "X_UNIQUE ");
15163055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG1)
1517*4867Spwernau 		(void) fprintf(file, "X_AALG1 ");
15183055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_AALG2)
1519*4867Spwernau 		(void) fprintf(file, "X_AALG2 ");
15203055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG1)
1521*4867Spwernau 		(void) fprintf(file, "X_EALG1 ");
15223055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_EALG2)
1523*4867Spwernau 		(void) fprintf(file, "X_EALG2 ");
15243055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC)
1525*4867Spwernau 		(void) fprintf(file, "X_NATT_LOC ");
15263055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM)
1527*4867Spwernau 		(void) fprintf(file, "X_NATT_REM ");
15283055Sdanmcd 	if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
1529*4867Spwernau 		(void) fprintf(file, "X_TUNNEL ");
15303055Sdanmcd 	/* END Solaris-specific flags. */
15313055Sdanmcd 
1532*4867Spwernau 	(void) fprintf(file, ">\n");
15333055Sdanmcd }
15343055Sdanmcd 
15353055Sdanmcd void
1536*4867Spwernau printsatime(FILE *file, int64_t lt, const char *msg, const char *pfx,
1537*4867Spwernau     const char *pfx2, boolean_t vflag)
15383055Sdanmcd {
15393055Sdanmcd 	char tbuf[TBUF_SIZE]; /* For strftime() call. */
15403055Sdanmcd 	const char *tp = tbuf;
15413055Sdanmcd 	time_t t = lt;
15423055Sdanmcd 	struct tm res;
15433055Sdanmcd 
15443055Sdanmcd 	if (t != lt) {
15453055Sdanmcd 		if (lt > 0)
15463055Sdanmcd 			t = LONG_MAX;
15473055Sdanmcd 		else
15483055Sdanmcd 			t = LONG_MIN;
15493055Sdanmcd 	}
15503055Sdanmcd 
15513055Sdanmcd 	if (strftime(tbuf, TBUF_SIZE, NULL, localtime_r(&t, &res)) == 0)
15524064Smarkfen 		tp = dgettext(TEXT_DOMAIN, "<time conversion failed>");
1553*4867Spwernau 	(void) fprintf(file, msg, pfx, tp);
15543055Sdanmcd 	if (vflag && (pfx2 != NULL))
1555*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15564064Smarkfen 		    "%s\t(raw time value %llu)\n"), pfx2, lt);
15573055Sdanmcd }
15583055Sdanmcd 
15593055Sdanmcd /*
15603055Sdanmcd  * Print the SA lifetime information.  (An SADB_EXT_LIFETIME_* extension.)
15613055Sdanmcd  */
15623055Sdanmcd void
1563*4867Spwernau print_lifetimes(FILE *file, time_t wallclock, struct sadb_lifetime *current,
15643055Sdanmcd     struct sadb_lifetime *hard, struct sadb_lifetime *soft, boolean_t vflag)
15653055Sdanmcd {
15663055Sdanmcd 	int64_t scratch;
15674064Smarkfen 	char *soft_prefix = dgettext(TEXT_DOMAIN, "SLT: ");
15684064Smarkfen 	char *hard_prefix = dgettext(TEXT_DOMAIN, "HLT: ");
15694064Smarkfen 	char *current_prefix = dgettext(TEXT_DOMAIN, "CLT: ");
15703055Sdanmcd 
15713055Sdanmcd 	if (current != NULL &&
15723055Sdanmcd 	    current->sadb_lifetime_len != SADB_8TO64(sizeof (*current))) {
1573*4867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
15744064Smarkfen 		    "WARNING: CURRENT lifetime extension length (%u) is bad."),
15753055Sdanmcd 		    SADB_64TO8(current->sadb_lifetime_len));
15763055Sdanmcd 	}
15773055Sdanmcd 
15783055Sdanmcd 	if (hard != NULL &&
15793055Sdanmcd 	    hard->sadb_lifetime_len != SADB_8TO64(sizeof (*hard))) {
1580*4867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
1581*4867Spwernau 		    "WARNING: HARD lifetime extension length (%u) is bad."),
15823055Sdanmcd 		    SADB_64TO8(hard->sadb_lifetime_len));
15833055Sdanmcd 	}
15843055Sdanmcd 
15853055Sdanmcd 	if (soft != NULL &&
15863055Sdanmcd 	    soft->sadb_lifetime_len != SADB_8TO64(sizeof (*soft))) {
1587*4867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
1588*4867Spwernau 		    "WARNING: SOFT lifetime extension length (%u) is bad."),
15893055Sdanmcd 		    SADB_64TO8(soft->sadb_lifetime_len));
15903055Sdanmcd 	}
15913055Sdanmcd 
1592*4867Spwernau 	(void) fprintf(file, " LT: Lifetime information\n");
15933055Sdanmcd 
15943055Sdanmcd 	if (current != NULL) {
15953055Sdanmcd 		/* Express values as current values. */
1596*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
15973055Sdanmcd 		    "%s%llu bytes protected, %u allocations used.\n"),
15983055Sdanmcd 		    current_prefix, current->sadb_lifetime_bytes,
15993055Sdanmcd 		    current->sadb_lifetime_allocations);
1600*4867Spwernau 		printsatime(file, current->sadb_lifetime_addtime,
16014064Smarkfen 		    dgettext(TEXT_DOMAIN, "%sSA added at time %s\n"),
16023055Sdanmcd 		    current_prefix, current_prefix, vflag);
16033055Sdanmcd 		if (current->sadb_lifetime_usetime != 0) {
1604*4867Spwernau 			printsatime(file, current->sadb_lifetime_usetime,
16054064Smarkfen 			    dgettext(TEXT_DOMAIN,
16064064Smarkfen 			    "%sSA first used at time %s\n"),
16073055Sdanmcd 			    current_prefix, current_prefix, vflag);
16083055Sdanmcd 		}
1609*4867Spwernau 		printsatime(file, wallclock, dgettext(TEXT_DOMAIN,
16104064Smarkfen 		    "%sTime now is %s\n"), current_prefix, current_prefix,
16114064Smarkfen 		    vflag);
16123055Sdanmcd 	}
16133055Sdanmcd 
16143055Sdanmcd 	if (soft != NULL) {
1615*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16164064Smarkfen 		    "%sSoft lifetime information:  "),
16173055Sdanmcd 		    soft_prefix);
1618*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16194064Smarkfen 		    "%llu bytes of lifetime, %u "
16203055Sdanmcd 		    "allocations.\n"), soft->sadb_lifetime_bytes,
16213055Sdanmcd 		    soft->sadb_lifetime_allocations);
1622*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16234064Smarkfen 		    "%s%llu seconds of post-add lifetime.\n"),
16243055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_addtime);
1625*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16264064Smarkfen 		    "%s%llu seconds of post-use lifetime.\n"),
16273055Sdanmcd 		    soft_prefix, soft->sadb_lifetime_usetime);
16283055Sdanmcd 		/* If possible, express values as time remaining. */
16293055Sdanmcd 		if (current != NULL) {
16303055Sdanmcd 			if (soft->sadb_lifetime_bytes != 0)
1631*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
16323055Sdanmcd 				    "%s%llu more bytes can be protected.\n"),
16333055Sdanmcd 				    soft_prefix,
16343055Sdanmcd 				    (soft->sadb_lifetime_bytes >
16354342Spwernau 				    current->sadb_lifetime_bytes) ?
16363055Sdanmcd 				    (soft->sadb_lifetime_bytes -
16374342Spwernau 				    current->sadb_lifetime_bytes) : (0));
16383055Sdanmcd 			if (soft->sadb_lifetime_addtime != 0 ||
16393055Sdanmcd 			    (soft->sadb_lifetime_usetime != 0 &&
16404342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
16413055Sdanmcd 				int64_t adddelta, usedelta;
16423055Sdanmcd 
16433055Sdanmcd 				if (soft->sadb_lifetime_addtime != 0) {
16443055Sdanmcd 					adddelta =
16453055Sdanmcd 					    current->sadb_lifetime_addtime +
16463055Sdanmcd 					    soft->sadb_lifetime_addtime -
16473055Sdanmcd 					    wallclock;
16483055Sdanmcd 				} else {
16493055Sdanmcd 					adddelta = TIME_MAX;
16503055Sdanmcd 				}
16513055Sdanmcd 
16523055Sdanmcd 				if (soft->sadb_lifetime_usetime != 0 &&
16533055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
16543055Sdanmcd 					usedelta =
16553055Sdanmcd 					    current->sadb_lifetime_usetime +
16563055Sdanmcd 					    soft->sadb_lifetime_usetime -
16573055Sdanmcd 					    wallclock;
16583055Sdanmcd 				} else {
16593055Sdanmcd 					usedelta = TIME_MAX;
16603055Sdanmcd 				}
1661*4867Spwernau 				(void) fprintf(file, "%s", soft_prefix);
16623055Sdanmcd 				scratch = MIN(adddelta, usedelta);
16633055Sdanmcd 				if (scratch >= 0) {
1664*4867Spwernau 					(void) fprintf(file,
1665*4867Spwernau 					    dgettext(TEXT_DOMAIN,
16664064Smarkfen 					    "Soft expiration occurs in %lld "
16674064Smarkfen 					    "seconds, "), scratch);
16683055Sdanmcd 				} else {
1669*4867Spwernau 					(void) fprintf(file,
1670*4867Spwernau 					    dgettext(TEXT_DOMAIN,
16713055Sdanmcd 					    "Soft expiration occurred "));
16723055Sdanmcd 				}
16733055Sdanmcd 				scratch += wallclock;
1674*4867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
16754064Smarkfen 				    "%sat %s.\n"), "", soft_prefix, vflag);
16763055Sdanmcd 			}
16773055Sdanmcd 		}
16783055Sdanmcd 	}
16793055Sdanmcd 
16803055Sdanmcd 	if (hard != NULL) {
1681*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16824064Smarkfen 		    "%sHard lifetime information:  "), hard_prefix);
1683*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1684*4867Spwernau 		    "%llu bytes of lifetime, %u allocations.\n"),
1685*4867Spwernau 		    hard->sadb_lifetime_bytes, hard->sadb_lifetime_allocations);
1686*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16874064Smarkfen 		    "%s%llu seconds of post-add lifetime.\n"),
16883055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_addtime);
1689*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
16904064Smarkfen 		    "%s%llu seconds of post-use lifetime.\n"),
16913055Sdanmcd 		    hard_prefix, hard->sadb_lifetime_usetime);
16923055Sdanmcd 		/* If possible, express values as time remaining. */
16933055Sdanmcd 		if (current != NULL) {
16943055Sdanmcd 			if (hard->sadb_lifetime_bytes != 0)
1695*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
16963055Sdanmcd 				    "%s%llu more bytes can be protected.\n"),
16973055Sdanmcd 				    hard_prefix,
16983055Sdanmcd 				    (hard->sadb_lifetime_bytes >
16994342Spwernau 				    current->sadb_lifetime_bytes) ?
17003055Sdanmcd 				    (hard->sadb_lifetime_bytes -
17014342Spwernau 				    current->sadb_lifetime_bytes) : (0));
17023055Sdanmcd 			if (hard->sadb_lifetime_addtime != 0 ||
17033055Sdanmcd 			    (hard->sadb_lifetime_usetime != 0 &&
17044342Spwernau 			    current->sadb_lifetime_usetime != 0)) {
17053055Sdanmcd 				int64_t adddelta, usedelta;
17063055Sdanmcd 
17073055Sdanmcd 				if (hard->sadb_lifetime_addtime != 0) {
17083055Sdanmcd 					adddelta =
17093055Sdanmcd 					    current->sadb_lifetime_addtime +
17103055Sdanmcd 					    hard->sadb_lifetime_addtime -
17113055Sdanmcd 					    wallclock;
17123055Sdanmcd 				} else {
17133055Sdanmcd 					adddelta = TIME_MAX;
17143055Sdanmcd 				}
17153055Sdanmcd 
17163055Sdanmcd 				if (hard->sadb_lifetime_usetime != 0 &&
17173055Sdanmcd 				    current->sadb_lifetime_usetime != 0) {
17183055Sdanmcd 					usedelta =
17193055Sdanmcd 					    current->sadb_lifetime_usetime +
17203055Sdanmcd 					    hard->sadb_lifetime_usetime -
17213055Sdanmcd 					    wallclock;
17223055Sdanmcd 				} else {
17233055Sdanmcd 					usedelta = TIME_MAX;
17243055Sdanmcd 				}
1725*4867Spwernau 				(void) fprintf(file, "%s", hard_prefix);
17263055Sdanmcd 				scratch = MIN(adddelta, usedelta);
17273055Sdanmcd 				if (scratch >= 0) {
1728*4867Spwernau 					(void) fprintf(file,
1729*4867Spwernau 					    dgettext(TEXT_DOMAIN,
17304064Smarkfen 					    "Hard expiration occurs in %lld "
17314064Smarkfen 					    "seconds, "), scratch);
17323055Sdanmcd 				} else {
1733*4867Spwernau 					(void) fprintf(file,
1734*4867Spwernau 					    dgettext(TEXT_DOMAIN,
17353055Sdanmcd 					    "Hard expiration occured "));
17363055Sdanmcd 				}
17373055Sdanmcd 				scratch += wallclock;
1738*4867Spwernau 				printsatime(file, scratch, dgettext(TEXT_DOMAIN,
17394064Smarkfen 				    "%sat %s.\n"), "", hard_prefix, vflag);
17403055Sdanmcd 			}
17413055Sdanmcd 		}
17423055Sdanmcd 	}
17433055Sdanmcd }
17443055Sdanmcd 
17453055Sdanmcd /*
17463055Sdanmcd  * Print an SADB_EXT_ADDRESS_* extension.
17473055Sdanmcd  */
17483055Sdanmcd void
1749*4867Spwernau print_address(FILE *file, char *prefix, struct sadb_address *addr,
1750*4867Spwernau     boolean_t ignore_nss)
17513055Sdanmcd {
17523055Sdanmcd 	struct protoent *pe;
17533055Sdanmcd 
1754*4867Spwernau 	(void) fprintf(file, "%s", prefix);
17553055Sdanmcd 	switch (addr->sadb_address_exttype) {
17563055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
1757*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source address "));
17583055Sdanmcd 		break;
17593055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
1760*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1761*4867Spwernau 		    "Inner source address "));
17623055Sdanmcd 		break;
17633055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
1764*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1765*4867Spwernau 		    "Destination address "));
17663055Sdanmcd 		break;
17673055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
1768*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
17694064Smarkfen 		    "Inner destination address "));
17703055Sdanmcd 		break;
17713055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
1772*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1773*4867Spwernau 		    "NAT-T local address "));
17743055Sdanmcd 		break;
17753055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
1776*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1777*4867Spwernau 		    "NAT-T remote address "));
17783055Sdanmcd 		break;
17793055Sdanmcd 	}
17803055Sdanmcd 
1781*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
17824064Smarkfen 	    "(proto=%d"), addr->sadb_address_proto);
1783*4867Spwernau 	if (ignore_nss == B_FALSE) {
17843055Sdanmcd 		if (addr->sadb_address_proto == 0) {
1785*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1786*4867Spwernau 			    "/<unspecified>"));
17873055Sdanmcd 		} else if ((pe = getprotobynumber(addr->sadb_address_proto))
17883055Sdanmcd 		    != NULL) {
1789*4867Spwernau 			(void) fprintf(file, "/%s", pe->p_name);
17903055Sdanmcd 		} else {
1791*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1792*4867Spwernau 			    "/<unknown>"));
17933055Sdanmcd 		}
17943055Sdanmcd 	}
1795*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, ")\n%s"), prefix);
17963055Sdanmcd 	(void) dump_sockaddr((struct sockaddr *)(addr + 1),
1797*4867Spwernau 	    addr->sadb_address_prefixlen, B_FALSE, file, ignore_nss);
17983055Sdanmcd }
17993055Sdanmcd 
18003055Sdanmcd /*
18013055Sdanmcd  * Print an SADB_EXT_KEY extension.
18023055Sdanmcd  */
18033055Sdanmcd void
1804*4867Spwernau print_key(FILE *file, char *prefix, struct sadb_key *key)
18053055Sdanmcd {
1806*4867Spwernau 	(void) fprintf(file, "%s", prefix);
18073055Sdanmcd 
18083055Sdanmcd 	switch (key->sadb_key_exttype) {
18093055Sdanmcd 	case SADB_EXT_KEY_AUTH:
1810*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Authentication"));
18113055Sdanmcd 		break;
18123055Sdanmcd 	case SADB_EXT_KEY_ENCRYPT:
1813*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Encryption"));
18143055Sdanmcd 		break;
18153055Sdanmcd 	}
18163055Sdanmcd 
1817*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " key.\n%s"), prefix);
1818*4867Spwernau 	(void) dump_key((uint8_t *)(key + 1), key->sadb_key_bits, file);
1819*4867Spwernau 	(void) fprintf(file, "\n");
18203055Sdanmcd }
18213055Sdanmcd 
18223055Sdanmcd /*
18233055Sdanmcd  * Print an SADB_EXT_IDENTITY_* extension.
18243055Sdanmcd  */
18253055Sdanmcd void
1826*4867Spwernau print_ident(FILE *file, char *prefix, struct sadb_ident *id)
18273055Sdanmcd {
18283055Sdanmcd 	boolean_t canprint = B_TRUE;
18293055Sdanmcd 
1830*4867Spwernau 	(void) fprintf(file, "%s", prefix);
18313055Sdanmcd 	switch (id->sadb_ident_exttype) {
18323055Sdanmcd 	case SADB_EXT_IDENTITY_SRC:
1833*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Source"));
18343055Sdanmcd 		break;
18353055Sdanmcd 	case SADB_EXT_IDENTITY_DST:
1836*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "Destination"));
18373055Sdanmcd 		break;
18383055Sdanmcd 	}
18393055Sdanmcd 
1840*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
18414064Smarkfen 	    " identity, uid=%d, type "), id->sadb_ident_id);
1842*4867Spwernau 	canprint = dump_sadb_idtype(id->sadb_ident_type, file, NULL);
1843*4867Spwernau 	(void) fprintf(file, "\n%s", prefix);
18443055Sdanmcd 	if (canprint)
1845*4867Spwernau 		(void) fprintf(file, "%s\n", (char *)(id + 1));
18463055Sdanmcd 	else
1847*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "<cannot print>\n"));
18483055Sdanmcd }
18493055Sdanmcd 
18503055Sdanmcd /*
18513055Sdanmcd  * Print an SADB_SENSITIVITY extension.
18523055Sdanmcd  */
18533055Sdanmcd void
1854*4867Spwernau print_sens(FILE *file, char *prefix, struct sadb_sens *sens)
18553055Sdanmcd {
18563055Sdanmcd 	uint64_t *bitmap = (uint64_t *)(sens + 1);
18573055Sdanmcd 	int i;
18583055Sdanmcd 
1859*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
18604064Smarkfen 	    "%sSensitivity DPD %d, sens level=%d, integ level=%d\n"),
18613055Sdanmcd 	    prefix, sens->sadb_sens_dpd, sens->sadb_sens_sens_level,
18623055Sdanmcd 	    sens->sadb_sens_integ_level);
18633055Sdanmcd 	for (i = 0; sens->sadb_sens_sens_len-- > 0; i++, bitmap++)
1864*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1865*4867Spwernau 		    "%s Sensitivity BM extended word %d 0x%llx\n"),
1866*4867Spwernau 		    prefix, i, *bitmap);
18673055Sdanmcd 	for (i = 0; sens->sadb_sens_integ_len-- > 0; i++, bitmap++)
1868*4867Spwernau 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1869*4867Spwernau 		    "%s Integrity BM extended word %d 0x%llx\n"),
1870*4867Spwernau 		    prefix, i, *bitmap);
18713055Sdanmcd }
18723055Sdanmcd 
18733055Sdanmcd /*
18743055Sdanmcd  * Print an SADB_EXT_PROPOSAL extension.
18753055Sdanmcd  */
18763055Sdanmcd void
1877*4867Spwernau print_prop(FILE *file, char *prefix, struct sadb_prop *prop)
18783055Sdanmcd {
18793055Sdanmcd 	struct sadb_comb *combs;
18803055Sdanmcd 	int i, numcombs;
18813055Sdanmcd 
1882*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
18834064Smarkfen 	    "%sProposal, replay counter = %u.\n"), prefix,
18843055Sdanmcd 	    prop->sadb_prop_replay);
18853055Sdanmcd 
18863055Sdanmcd 	numcombs = prop->sadb_prop_len - SADB_8TO64(sizeof (*prop));
18873055Sdanmcd 	numcombs /= SADB_8TO64(sizeof (*combs));
18883055Sdanmcd 
18893055Sdanmcd 	combs = (struct sadb_comb *)(prop + 1);
18903055Sdanmcd 
18913055Sdanmcd 	for (i = 0; i < numcombs; i++) {
1892*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
18934064Smarkfen 		    "%s Combination #%u "), prefix, i + 1);
18943055Sdanmcd 		if (combs[i].sadb_comb_auth != SADB_AALG_NONE) {
1895*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
18964064Smarkfen 			    "Authentication = "));
1897*4867Spwernau 			(void) dump_aalg(combs[i].sadb_comb_auth, file);
1898*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
18994064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
19003055Sdanmcd 			    combs[i].sadb_comb_auth_minbits,
19013055Sdanmcd 			    combs[i].sadb_comb_auth_maxbits, prefix);
19023055Sdanmcd 		}
19033055Sdanmcd 
19043055Sdanmcd 		if (combs[i].sadb_comb_encrypt != SADB_EALG_NONE) {
1905*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1906*4867Spwernau 			    "Encryption = "));
1907*4867Spwernau 			(void) dump_ealg(combs[i].sadb_comb_encrypt, file);
1908*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19094064Smarkfen 			    "  minbits=%u, maxbits=%u.\n%s "),
19103055Sdanmcd 			    combs[i].sadb_comb_encrypt_minbits,
19113055Sdanmcd 			    combs[i].sadb_comb_encrypt_maxbits, prefix);
19123055Sdanmcd 		}
19133055Sdanmcd 
1914*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "HARD: "));
19153055Sdanmcd 		if (combs[i].sadb_comb_hard_allocations)
1916*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
19173055Sdanmcd 			    combs[i].sadb_comb_hard_allocations);
19183055Sdanmcd 		if (combs[i].sadb_comb_hard_bytes)
1919*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1920*4867Spwernau 			    "bytes=%llu "), combs[i].sadb_comb_hard_bytes);
19213055Sdanmcd 		if (combs[i].sadb_comb_hard_addtime)
1922*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19234064Smarkfen 			    "post-add secs=%llu "),
19243055Sdanmcd 			    combs[i].sadb_comb_hard_addtime);
19253055Sdanmcd 		if (combs[i].sadb_comb_hard_usetime)
1926*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19274064Smarkfen 			    "post-use secs=%llu"),
19283055Sdanmcd 			    combs[i].sadb_comb_hard_usetime);
19293055Sdanmcd 
1930*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n%s SOFT: "),
1931*4867Spwernau 		    prefix);
19323055Sdanmcd 		if (combs[i].sadb_comb_soft_allocations)
1933*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u "),
19343055Sdanmcd 			    combs[i].sadb_comb_soft_allocations);
19353055Sdanmcd 		if (combs[i].sadb_comb_soft_bytes)
1936*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
1937*4867Spwernau 			    "bytes=%llu "), combs[i].sadb_comb_soft_bytes);
19383055Sdanmcd 		if (combs[i].sadb_comb_soft_addtime)
1939*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19404064Smarkfen 			    "post-add secs=%llu "),
19413055Sdanmcd 			    combs[i].sadb_comb_soft_addtime);
19423055Sdanmcd 		if (combs[i].sadb_comb_soft_usetime)
1943*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
19444064Smarkfen 			    "post-use secs=%llu"),
19453055Sdanmcd 			    combs[i].sadb_comb_soft_usetime);
1946*4867Spwernau 		(void) fprintf(file, "\n");
19473055Sdanmcd 	}
19483055Sdanmcd }
19493055Sdanmcd 
19503055Sdanmcd /*
19513055Sdanmcd  * Print an extended proposal (SADB_X_EXT_EPROP).
19523055Sdanmcd  */
19533055Sdanmcd void
1954*4867Spwernau print_eprop(FILE *file, char *prefix, struct sadb_prop *eprop)
19553055Sdanmcd {
19563055Sdanmcd 	uint64_t *sofar;
19573055Sdanmcd 	struct sadb_x_ecomb *ecomb;
19583055Sdanmcd 	struct sadb_x_algdesc *algdesc;
19593055Sdanmcd 	int i, j;
19603055Sdanmcd 
1961*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
19624064Smarkfen 	    "%sExtended Proposal, replay counter = %u, "), prefix,
19634064Smarkfen 	    eprop->sadb_prop_replay);
1964*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
1965*4867Spwernau 	    "number of combinations = %u.\n"), eprop->sadb_x_prop_numecombs);
19663055Sdanmcd 
19673055Sdanmcd 	sofar = (uint64_t *)(eprop + 1);
19683055Sdanmcd 	ecomb = (struct sadb_x_ecomb *)sofar;
19693055Sdanmcd 
19703055Sdanmcd 	for (i = 0; i < eprop->sadb_x_prop_numecombs; ) {
1971*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
19724064Smarkfen 		    "%s Extended combination #%u:\n"), prefix, ++i);
19733055Sdanmcd 
1974*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s HARD: "),
1975*4867Spwernau 		    prefix);
1976*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
19773055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_allocations);
1978*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "),
19793055Sdanmcd 		    ecomb->sadb_x_ecomb_hard_bytes);
1980*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1981*4867Spwernau 		    "post-add secs=%llu, "), ecomb->sadb_x_ecomb_hard_addtime);
1982*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1983*4867Spwernau 		    "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_hard_usetime);
19843055Sdanmcd 
1985*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "%s SOFT: "),
1986*4867Spwernau 		    prefix);
1987*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "alloc=%u, "),
19883055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_allocations);
1989*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "bytes=%llu, "),
19903055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_bytes);
1991*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1992*4867Spwernau 		    "post-add secs=%llu, "),
19933055Sdanmcd 		    ecomb->sadb_x_ecomb_soft_addtime);
1994*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
1995*4867Spwernau 		    "post-use secs=%llu\n"), ecomb->sadb_x_ecomb_soft_usetime);
19963055Sdanmcd 
19973055Sdanmcd 		sofar = (uint64_t *)(ecomb + 1);
19983055Sdanmcd 		algdesc = (struct sadb_x_algdesc *)sofar;
19993055Sdanmcd 
20003055Sdanmcd 		for (j = 0; j < ecomb->sadb_x_ecomb_numalgs; ) {
2001*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
20024064Smarkfen 			    "%s Alg #%u "), prefix, ++j);
20033055Sdanmcd 			switch (algdesc->sadb_x_algdesc_satype) {
20043055Sdanmcd 			case SADB_SATYPE_ESP:
2005*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20064064Smarkfen 				    "for ESP "));
20073055Sdanmcd 				break;
20083055Sdanmcd 			case SADB_SATYPE_AH:
2009*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
2010*4867Spwernau 				    "for AH "));
20113055Sdanmcd 				break;
20123055Sdanmcd 			default:
2013*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20144064Smarkfen 				    "for satype=%d "),
20153055Sdanmcd 				    algdesc->sadb_x_algdesc_satype);
20163055Sdanmcd 			}
20173055Sdanmcd 			switch (algdesc->sadb_x_algdesc_algtype) {
20183055Sdanmcd 			case SADB_X_ALGTYPE_CRYPT:
2019*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20204064Smarkfen 				    "Encryption = "));
20213055Sdanmcd 				(void) dump_ealg(algdesc->sadb_x_algdesc_alg,
2022*4867Spwernau 				    file);
20233055Sdanmcd 				break;
20243055Sdanmcd 			case SADB_X_ALGTYPE_AUTH:
2025*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20264064Smarkfen 				    "Authentication = "));
20273055Sdanmcd 				(void) dump_aalg(algdesc->sadb_x_algdesc_alg,
2028*4867Spwernau 				    file);
20293055Sdanmcd 				break;
20303055Sdanmcd 			default:
2031*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
20324064Smarkfen 				    "algtype(%d) = alg(%d)"),
20333055Sdanmcd 				    algdesc->sadb_x_algdesc_algtype,
20343055Sdanmcd 				    algdesc->sadb_x_algdesc_alg);
20353055Sdanmcd 				break;
20363055Sdanmcd 			}
20373055Sdanmcd 
2038*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
20394064Smarkfen 			    "  minbits=%u, maxbits=%u.\n"),
20403055Sdanmcd 			    algdesc->sadb_x_algdesc_minbits,
20413055Sdanmcd 			    algdesc->sadb_x_algdesc_maxbits);
20423055Sdanmcd 
20433055Sdanmcd 			sofar = (uint64_t *)(++algdesc);
20443055Sdanmcd 		}
20453055Sdanmcd 		ecomb = (struct sadb_x_ecomb *)sofar;
20463055Sdanmcd 	}
20473055Sdanmcd }
20483055Sdanmcd 
20493055Sdanmcd /*
20503055Sdanmcd  * Print an SADB_EXT_SUPPORTED extension.
20513055Sdanmcd  */
20523055Sdanmcd void
2053*4867Spwernau print_supp(FILE *file, char *prefix, struct sadb_supported *supp)
20543055Sdanmcd {
20553055Sdanmcd 	struct sadb_alg *algs;
20563055Sdanmcd 	int i, numalgs;
20573055Sdanmcd 
2058*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, "%sSupported "), prefix);
20593055Sdanmcd 	switch (supp->sadb_supported_exttype) {
20603055Sdanmcd 	case SADB_EXT_SUPPORTED_AUTH:
2061*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "authentication"));
20623055Sdanmcd 		break;
20633055Sdanmcd 	case SADB_EXT_SUPPORTED_ENCRYPT:
2064*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "encryption"));
20653055Sdanmcd 		break;
20663055Sdanmcd 	}
2067*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN, " algorithms.\n"));
20683055Sdanmcd 
20693055Sdanmcd 	algs = (struct sadb_alg *)(supp + 1);
20703055Sdanmcd 	numalgs = supp->sadb_supported_len - SADB_8TO64(sizeof (*supp));
20713055Sdanmcd 	numalgs /= SADB_8TO64(sizeof (*algs));
20723055Sdanmcd 	for (i = 0; i < numalgs; i++) {
2073*4867Spwernau 		(void) fprintf(file, "%s", prefix);
20743055Sdanmcd 		switch (supp->sadb_supported_exttype) {
20753055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
2076*4867Spwernau 			(void) dump_aalg(algs[i].sadb_alg_id, file);
20773055Sdanmcd 			break;
20783055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
2079*4867Spwernau 			(void) dump_ealg(algs[i].sadb_alg_id, file);
20803055Sdanmcd 			break;
20813055Sdanmcd 		}
2082*4867Spwernau 		(void) fprintf(file, dgettext(TEXT_DOMAIN,
20834064Smarkfen 		    " minbits=%u, maxbits=%u, ivlen=%u.\n"),
20843055Sdanmcd 		    algs[i].sadb_alg_minbits, algs[i].sadb_alg_maxbits,
20853055Sdanmcd 		    algs[i].sadb_alg_ivlen);
20863055Sdanmcd 	}
20873055Sdanmcd }
20883055Sdanmcd 
20893055Sdanmcd /*
20903055Sdanmcd  * Print an SADB_EXT_SPIRANGE extension.
20913055Sdanmcd  */
20923055Sdanmcd void
2093*4867Spwernau print_spirange(FILE *file, char *prefix, struct sadb_spirange *range)
20943055Sdanmcd {
2095*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
20964064Smarkfen 	    "%sSPI Range, min=0x%x, max=0x%x\n"), prefix,
20973055Sdanmcd 	    htonl(range->sadb_spirange_min),
20983055Sdanmcd 	    htonl(range->sadb_spirange_max));
20993055Sdanmcd }
21003055Sdanmcd 
21013055Sdanmcd /*
21023055Sdanmcd  * Print an SADB_X_EXT_KM_COOKIE extension.
21033055Sdanmcd  */
21043055Sdanmcd 
21053055Sdanmcd void
2106*4867Spwernau print_kmc(FILE *file, char *prefix, struct sadb_x_kmc *kmc)
21073055Sdanmcd {
21083055Sdanmcd 	char *cookie_label;
21093055Sdanmcd 
21103055Sdanmcd 	if ((cookie_label = kmc_lookup_by_cookie(kmc->sadb_x_kmc_cookie)) ==
21113055Sdanmcd 	    NULL)
21124064Smarkfen 		cookie_label = dgettext(TEXT_DOMAIN, "<Label not found.>");
21133055Sdanmcd 
2114*4867Spwernau 	(void) fprintf(file, dgettext(TEXT_DOMAIN,
21154064Smarkfen 	    "%sProtocol %u, cookie=\"%s\" (%u)\n"), prefix,
21163055Sdanmcd 	    kmc->sadb_x_kmc_proto, cookie_label, kmc->sadb_x_kmc_cookie);
21173055Sdanmcd }
21183055Sdanmcd 
21193055Sdanmcd /*
21203055Sdanmcd  * Take a PF_KEY message pointed to buffer and print it.  Useful for DUMP
21213055Sdanmcd  * and GET.
21223055Sdanmcd  */
21233055Sdanmcd void
2124*4867Spwernau print_samsg(FILE *file, uint64_t *buffer, boolean_t want_timestamp,
2125*4867Spwernau     boolean_t vflag, boolean_t ignore_nss)
21263055Sdanmcd {
21273055Sdanmcd 	uint64_t *current;
21283055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
21293055Sdanmcd 	struct sadb_ext *ext;
21303055Sdanmcd 	struct sadb_lifetime *currentlt = NULL, *hardlt = NULL, *softlt = NULL;
21313055Sdanmcd 	int i;
21323055Sdanmcd 	time_t wallclock;
21333055Sdanmcd 
21343055Sdanmcd 	(void) time(&wallclock);
21353055Sdanmcd 
2136*4867Spwernau 	print_sadb_msg(file, samsg, want_timestamp ? wallclock : 0, vflag);
21373055Sdanmcd 	current = (uint64_t *)(samsg + 1);
21383055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
21393055Sdanmcd 		int lenbytes;
21403055Sdanmcd 
21413055Sdanmcd 		ext = (struct sadb_ext *)current;
21423055Sdanmcd 		lenbytes = SADB_64TO8(ext->sadb_ext_len);
21433055Sdanmcd 		switch (ext->sadb_ext_type) {
21443055Sdanmcd 		case SADB_EXT_SA:
2145*4867Spwernau 			print_sa(file, dgettext(TEXT_DOMAIN,
21464064Smarkfen 			    "SA: "), (struct sadb_sa *)current);
21473055Sdanmcd 			break;
21483055Sdanmcd 		/*
21493055Sdanmcd 		 * Pluck out lifetimes and print them at the end.  This is
21503055Sdanmcd 		 * to show relative lifetimes.
21513055Sdanmcd 		 */
21523055Sdanmcd 		case SADB_EXT_LIFETIME_CURRENT:
21533055Sdanmcd 			currentlt = (struct sadb_lifetime *)current;
21543055Sdanmcd 			break;
21553055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
21563055Sdanmcd 			hardlt = (struct sadb_lifetime *)current;
21573055Sdanmcd 			break;
21583055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
21593055Sdanmcd 			softlt = (struct sadb_lifetime *)current;
21603055Sdanmcd 			break;
21613055Sdanmcd 
21623055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
2163*4867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "SRC: "),
2164*4867Spwernau 			    (struct sadb_address *)current, ignore_nss);
21653055Sdanmcd 			break;
21663055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
2167*4867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "INS: "),
2168*4867Spwernau 			    (struct sadb_address *)current, ignore_nss);
21693055Sdanmcd 			break;
21703055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
2171*4867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "DST: "),
2172*4867Spwernau 			    (struct sadb_address *)current, ignore_nss);
21733055Sdanmcd 			break;
21743055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
2175*4867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "IND: "),
2176*4867Spwernau 			    (struct sadb_address *)current, ignore_nss);
21773055Sdanmcd 			break;
21783055Sdanmcd 		case SADB_EXT_KEY_AUTH:
2179*4867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
21804064Smarkfen 			    "AKY: "), (struct sadb_key *)current);
21813055Sdanmcd 			break;
21823055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
2183*4867Spwernau 			print_key(file, dgettext(TEXT_DOMAIN,
21844064Smarkfen 			    "EKY: "), (struct sadb_key *)current);
21853055Sdanmcd 			break;
21863055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
2187*4867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "SID: "),
21883055Sdanmcd 			    (struct sadb_ident *)current);
21893055Sdanmcd 			break;
21903055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
2191*4867Spwernau 			print_ident(file, dgettext(TEXT_DOMAIN, "DID: "),
21923055Sdanmcd 			    (struct sadb_ident *)current);
21933055Sdanmcd 			break;
21943055Sdanmcd 		case SADB_EXT_SENSITIVITY:
2195*4867Spwernau 			print_sens(file, dgettext(TEXT_DOMAIN, "SNS: "),
21963055Sdanmcd 			    (struct sadb_sens *)current);
21973055Sdanmcd 			break;
21983055Sdanmcd 		case SADB_EXT_PROPOSAL:
2199*4867Spwernau 			print_prop(file, dgettext(TEXT_DOMAIN, "PRP: "),
22003055Sdanmcd 			    (struct sadb_prop *)current);
22013055Sdanmcd 			break;
22023055Sdanmcd 		case SADB_EXT_SUPPORTED_AUTH:
2203*4867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUA: "),
22043055Sdanmcd 			    (struct sadb_supported *)current);
22053055Sdanmcd 			break;
22063055Sdanmcd 		case SADB_EXT_SUPPORTED_ENCRYPT:
2207*4867Spwernau 			print_supp(file, dgettext(TEXT_DOMAIN, "SUE: "),
22083055Sdanmcd 			    (struct sadb_supported *)current);
22093055Sdanmcd 			break;
22103055Sdanmcd 		case SADB_EXT_SPIRANGE:
2211*4867Spwernau 			print_spirange(file, dgettext(TEXT_DOMAIN, "SPR: "),
22123055Sdanmcd 			    (struct sadb_spirange *)current);
22133055Sdanmcd 			break;
22143055Sdanmcd 		case SADB_X_EXT_EPROP:
2215*4867Spwernau 			print_eprop(file, dgettext(TEXT_DOMAIN, "EPR: "),
22163055Sdanmcd 			    (struct sadb_prop *)current);
22173055Sdanmcd 			break;
22183055Sdanmcd 		case SADB_X_EXT_KM_COOKIE:
2219*4867Spwernau 			print_kmc(file, dgettext(TEXT_DOMAIN, "KMC: "),
22203055Sdanmcd 			    (struct sadb_x_kmc *)current);
22213055Sdanmcd 			break;
22223055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
2223*4867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NRM: "),
2224*4867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22253055Sdanmcd 			break;
22263055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
2227*4867Spwernau 			print_address(file, dgettext(TEXT_DOMAIN, "NLC: "),
2228*4867Spwernau 			    (struct sadb_address *)current, ignore_nss);
22293055Sdanmcd 			break;
22303055Sdanmcd 		default:
2231*4867Spwernau 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
22323055Sdanmcd 			    "UNK: Unknown ext. %d, len %d.\n"),
22333055Sdanmcd 			    ext->sadb_ext_type, lenbytes);
22343055Sdanmcd 			for (i = 0; i < ext->sadb_ext_len; i++)
2235*4867Spwernau 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
22364064Smarkfen 				    "UNK: 0x%llx\n"), ((uint64_t *)ext)[i]);
22373055Sdanmcd 			break;
22383055Sdanmcd 		}
22393055Sdanmcd 		current += (lenbytes == 0) ?
22403055Sdanmcd 		    SADB_8TO64(sizeof (struct sadb_ext)) : ext->sadb_ext_len;
22413055Sdanmcd 	}
22423055Sdanmcd 	/*
22433055Sdanmcd 	 * Print lifetimes NOW.
22443055Sdanmcd 	 */
22453055Sdanmcd 	if (currentlt != NULL || hardlt != NULL || softlt != NULL)
2246*4867Spwernau 		print_lifetimes(file, wallclock, currentlt, hardlt, softlt,
2247*4867Spwernau 		    vflag);
22483055Sdanmcd 
22493055Sdanmcd 	if (current - buffer != samsg->sadb_msg_len) {
2250*4867Spwernau 		warnxfp(EFD(file), dgettext(TEXT_DOMAIN,
2251*4867Spwernau 		    "WARNING: insufficient buffer space or corrupt message."));
22523055Sdanmcd 	}
22533055Sdanmcd 
2254*4867Spwernau 	(void) fflush(file);	/* Make sure our message is out there. */
22553055Sdanmcd }
22563055Sdanmcd 
22573055Sdanmcd /*
22583055Sdanmcd  * save_XXX functions are used when "saving" the SA tables to either a
22593055Sdanmcd  * file or standard output.  They use the dump_XXX functions where needed,
22603055Sdanmcd  * but mostly they use the rparseXXX functions.
22613055Sdanmcd  */
22623055Sdanmcd 
22633055Sdanmcd /*
22643055Sdanmcd  * Print save information for a lifetime extension.
22653055Sdanmcd  *
22663055Sdanmcd  * NOTE : It saves the lifetime in absolute terms.  For example, if you
22673055Sdanmcd  * had a hard_usetime of 60 seconds, you'll save it as 60 seconds, even though
22683055Sdanmcd  * there may have been 59 seconds burned off the clock.
22693055Sdanmcd  */
22703055Sdanmcd boolean_t
22713055Sdanmcd save_lifetime(struct sadb_lifetime *lifetime, FILE *ofile)
22723055Sdanmcd {
22733055Sdanmcd 	char *prefix;
22743055Sdanmcd 
22753055Sdanmcd 	prefix = (lifetime->sadb_lifetime_exttype == SADB_EXT_LIFETIME_SOFT) ?
22763055Sdanmcd 	    "soft" : "hard";
22773055Sdanmcd 
22783055Sdanmcd 	if (putc('\t', ofile) == EOF)
22793055Sdanmcd 		return (B_FALSE);
22803055Sdanmcd 
22813055Sdanmcd 	if (lifetime->sadb_lifetime_allocations != 0 && fprintf(ofile,
22823055Sdanmcd 	    "%s_alloc %u ", prefix, lifetime->sadb_lifetime_allocations) < 0)
22833055Sdanmcd 		return (B_FALSE);
22843055Sdanmcd 
22853055Sdanmcd 	if (lifetime->sadb_lifetime_bytes != 0 && fprintf(ofile,
22863055Sdanmcd 	    "%s_bytes %llu ", prefix, lifetime->sadb_lifetime_bytes) < 0)
22873055Sdanmcd 		return (B_FALSE);
22883055Sdanmcd 
22893055Sdanmcd 	if (lifetime->sadb_lifetime_addtime != 0 && fprintf(ofile,
22903055Sdanmcd 	    "%s_addtime %llu ", prefix, lifetime->sadb_lifetime_addtime) < 0)
22913055Sdanmcd 		return (B_FALSE);
22923055Sdanmcd 
22933055Sdanmcd 	if (lifetime->sadb_lifetime_usetime != 0 && fprintf(ofile,
22943055Sdanmcd 	    "%s_usetime %llu ", prefix, lifetime->sadb_lifetime_usetime) < 0)
22953055Sdanmcd 		return (B_FALSE);
22963055Sdanmcd 
22973055Sdanmcd 	return (B_TRUE);
22983055Sdanmcd }
22993055Sdanmcd 
23003055Sdanmcd /*
23013055Sdanmcd  * Print save information for an address extension.
23023055Sdanmcd  */
23033055Sdanmcd boolean_t
23043055Sdanmcd save_address(struct sadb_address *addr, FILE *ofile)
23053055Sdanmcd {
23063055Sdanmcd 	char *printable_addr, buf[INET6_ADDRSTRLEN];
23073055Sdanmcd 	const char *prefix, *pprefix;
23083055Sdanmcd 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(addr + 1);
23093055Sdanmcd 	struct sockaddr_in *sin = (struct sockaddr_in *)sin6;
23103055Sdanmcd 	int af = sin->sin_family;
23113055Sdanmcd 
23123055Sdanmcd 	/*
23133055Sdanmcd 	 * Address-family reality check.
23143055Sdanmcd 	 */
23153055Sdanmcd 	if (af != AF_INET6 && af != AF_INET)
23163055Sdanmcd 		return (B_FALSE);
23173055Sdanmcd 
23183055Sdanmcd 	switch (addr->sadb_address_exttype) {
23193055Sdanmcd 	case SADB_EXT_ADDRESS_SRC:
23203055Sdanmcd 		prefix = "src";
23213055Sdanmcd 		pprefix = "sport";
23223055Sdanmcd 		break;
23233055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_SRC:
23243055Sdanmcd 		prefix = "isrc";
23253055Sdanmcd 		pprefix = "isport";
23263055Sdanmcd 		break;
23273055Sdanmcd 	case SADB_EXT_ADDRESS_DST:
23283055Sdanmcd 		prefix = "dst";
23293055Sdanmcd 		pprefix = "dport";
23303055Sdanmcd 		break;
23313055Sdanmcd 	case SADB_X_EXT_ADDRESS_INNER_DST:
23323055Sdanmcd 		prefix = "idst";
23333055Sdanmcd 		pprefix = "idport";
23343055Sdanmcd 		break;
23353055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_LOC:
23363055Sdanmcd 		prefix = "nat_loc ";
23373055Sdanmcd 		pprefix = "nat_lport";
23383055Sdanmcd 		break;
23393055Sdanmcd 	case SADB_X_EXT_ADDRESS_NATT_REM:
23403055Sdanmcd 		prefix = "nat_rem ";
23413055Sdanmcd 		pprefix = "nat_rport";
23423055Sdanmcd 		break;
23433055Sdanmcd 	}
23443055Sdanmcd 
23453055Sdanmcd 	if (fprintf(ofile, "    %s ", prefix) < 0)
23463055Sdanmcd 		return (B_FALSE);
23473055Sdanmcd 
23483055Sdanmcd 	/*
23493055Sdanmcd 	 * Do not do address-to-name translation, given that we live in
23503055Sdanmcd 	 * an age of names that explode into many addresses.
23513055Sdanmcd 	 */
23523055Sdanmcd 	printable_addr = (char *)inet_ntop(af,
23533055Sdanmcd 	    (af == AF_INET) ? (char *)&sin->sin_addr : (char *)&sin6->sin6_addr,
23543055Sdanmcd 	    buf, sizeof (buf));
23553055Sdanmcd 	if (printable_addr == NULL)
23564064Smarkfen 		printable_addr = "Invalid IP address.";
23573055Sdanmcd 	if (fprintf(ofile, "%s", printable_addr) < 0)
23583055Sdanmcd 		return (B_FALSE);
23593055Sdanmcd 	if (addr->sadb_address_prefixlen != 0 &&
23603055Sdanmcd 	    !((addr->sadb_address_prefixlen == 32 && af == AF_INET) ||
23614342Spwernau 	    (addr->sadb_address_prefixlen == 128 && af == AF_INET6))) {
23623055Sdanmcd 		if (fprintf(ofile, "/%d", addr->sadb_address_prefixlen) < 0)
23633055Sdanmcd 			return (B_FALSE);
23643055Sdanmcd 	}
23653055Sdanmcd 
23663055Sdanmcd 	/*
23673055Sdanmcd 	 * The port is in the same position for struct sockaddr_in and
23683055Sdanmcd 	 * struct sockaddr_in6.  We exploit that property here.
23693055Sdanmcd 	 */
23703055Sdanmcd 	if ((pprefix != NULL) && (sin->sin_port != 0))
23713055Sdanmcd 		(void) fprintf(ofile, " %s %d", pprefix, ntohs(sin->sin_port));
23723055Sdanmcd 
23733055Sdanmcd 	return (B_TRUE);
23743055Sdanmcd }
23753055Sdanmcd 
23763055Sdanmcd /*
23773055Sdanmcd  * Print save information for a key extension. Returns whether writing
23783055Sdanmcd  * to the specified output file was successful or not.
23793055Sdanmcd  */
23803055Sdanmcd boolean_t
23813055Sdanmcd save_key(struct sadb_key *key, FILE *ofile)
23823055Sdanmcd {
23833055Sdanmcd 	char *prefix;
23843055Sdanmcd 
23853055Sdanmcd 	if (putc('\t', ofile) == EOF)
23863055Sdanmcd 		return (B_FALSE);
23873055Sdanmcd 
23883055Sdanmcd 	prefix = (key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ? "auth" : "encr";
23893055Sdanmcd 
23903055Sdanmcd 	if (fprintf(ofile, "%skey ", prefix) < 0)
23913055Sdanmcd 		return (B_FALSE);
23923055Sdanmcd 
23933055Sdanmcd 	if (dump_key((uint8_t *)(key + 1), key->sadb_key_bits, ofile) == -1)
23943055Sdanmcd 		return (B_FALSE);
23953055Sdanmcd 
23963055Sdanmcd 	return (B_TRUE);
23973055Sdanmcd }
23983055Sdanmcd 
23993055Sdanmcd /*
24003055Sdanmcd  * Print save information for an identity extension.
24013055Sdanmcd  */
24023055Sdanmcd boolean_t
24033055Sdanmcd save_ident(struct sadb_ident *ident, FILE *ofile)
24043055Sdanmcd {
24053055Sdanmcd 	char *prefix;
24063055Sdanmcd 
24073055Sdanmcd 	if (putc('\t', ofile) == EOF)
24083055Sdanmcd 		return (B_FALSE);
24093055Sdanmcd 
24103055Sdanmcd 	prefix = (ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ? "src" :
24113055Sdanmcd 	    "dst";
24123055Sdanmcd 
24133055Sdanmcd 	if (fprintf(ofile, "%sidtype %s ", prefix,
24143055Sdanmcd 	    rparseidtype(ident->sadb_ident_type)) < 0)
24153055Sdanmcd 		return (B_FALSE);
24163055Sdanmcd 
24173055Sdanmcd 	if (ident->sadb_ident_type == SADB_X_IDENTTYPE_DN ||
24183055Sdanmcd 	    ident->sadb_ident_type == SADB_X_IDENTTYPE_GN) {
24194064Smarkfen 		if (fprintf(ofile, dgettext(TEXT_DOMAIN,
24204064Smarkfen 		    "<can-not-print>")) < 0)
24213055Sdanmcd 			return (B_FALSE);
24223055Sdanmcd 	} else {
24233055Sdanmcd 		if (fprintf(ofile, "%s", (char *)(ident + 1)) < 0)
24243055Sdanmcd 			return (B_FALSE);
24253055Sdanmcd 	}
24263055Sdanmcd 
24273055Sdanmcd 	return (B_TRUE);
24283055Sdanmcd }
24293055Sdanmcd 
24303055Sdanmcd /*
24313055Sdanmcd  * "Save" a security association to an output file.
24323055Sdanmcd  *
24334064Smarkfen  * NOTE the lack of calls to dgettext() because I'm outputting parseable stuff.
24343055Sdanmcd  * ALSO NOTE that if you change keywords (see parsecmd()), you'll have to
24353055Sdanmcd  * change them here as well.
24363055Sdanmcd  */
24373055Sdanmcd void
24383055Sdanmcd save_assoc(uint64_t *buffer, FILE *ofile)
24393055Sdanmcd {
24404064Smarkfen 	int terrno;
24413055Sdanmcd 	int seen_proto = 0;
24423055Sdanmcd 	uint64_t *current;
24433055Sdanmcd 	struct sadb_address *addr;
24443055Sdanmcd 	struct sadb_msg *samsg = (struct sadb_msg *)buffer;
24453055Sdanmcd 	struct sadb_ext *ext;
24463055Sdanmcd 
24474064Smarkfen #define	tidyup() \
24484064Smarkfen 	terrno = errno; (void) fclose(ofile); errno = terrno; \
24494064Smarkfen 	interactive = B_FALSE
24504064Smarkfen 
24514064Smarkfen #define	savenl() if (fputs(" \\\n", ofile) == EOF) \
24524064Smarkfen 	{ bail(dgettext(TEXT_DOMAIN, "savenl")); }
24533055Sdanmcd 
24543055Sdanmcd 	if (fputs("# begin assoc\n", ofile) == EOF)
24554064Smarkfen 		bail(dgettext(TEXT_DOMAIN,
24564064Smarkfen 		    "save_assoc: Opening comment of SA"));
24573055Sdanmcd 	if (fprintf(ofile, "add %s ", rparsesatype(samsg->sadb_msg_satype)) < 0)
24584064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: First line of SA"));
24593055Sdanmcd 	savenl();
24603055Sdanmcd 
24613055Sdanmcd 	current = (uint64_t *)(samsg + 1);
24623055Sdanmcd 	while (current - buffer < samsg->sadb_msg_len) {
24633055Sdanmcd 		struct sadb_sa *assoc;
24643055Sdanmcd 
24653055Sdanmcd 		ext = (struct sadb_ext *)current;
24663055Sdanmcd 		switch (ext->sadb_ext_type) {
24673055Sdanmcd 		case SADB_EXT_SA:
24683055Sdanmcd 			assoc = (struct sadb_sa *)ext;
24693055Sdanmcd 			if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) {
24703055Sdanmcd 				if (fprintf(ofile, "# WARNING: SA was dying "
24713055Sdanmcd 				    "or dead.\n") < 0) {
24724064Smarkfen 					tidyup();
24734064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24744064Smarkfen 					    "save_assoc: fprintf not mature"));
24753055Sdanmcd 				}
24763055Sdanmcd 			}
24773055Sdanmcd 			if (fprintf(ofile, "    spi 0x%x ",
24784064Smarkfen 			    ntohl(assoc->sadb_sa_spi)) < 0) {
24794064Smarkfen 				tidyup();
24804064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
24814064Smarkfen 				    "save_assoc: fprintf spi"));
24824064Smarkfen 			}
24833055Sdanmcd 			if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
24843055Sdanmcd 				if (fprintf(ofile, "encr_alg %s ",
24853055Sdanmcd 				    rparsealg(assoc->sadb_sa_encrypt,
24864342Spwernau 				    IPSEC_PROTO_ESP)) < 0) {
24874064Smarkfen 					tidyup();
24884064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24894064Smarkfen 					    "save_assoc: fprintf encrypt"));
24904064Smarkfen 				}
24913055Sdanmcd 			}
24923055Sdanmcd 			if (assoc->sadb_sa_auth != SADB_AALG_NONE) {
24933055Sdanmcd 				if (fprintf(ofile, "auth_alg %s ",
24943055Sdanmcd 				    rparsealg(assoc->sadb_sa_auth,
24954342Spwernau 				    IPSEC_PROTO_AH)) < 0) {
24964064Smarkfen 					tidyup();
24974064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
24984064Smarkfen 					    "save_assoc: fprintf auth"));
24994064Smarkfen 				}
25003055Sdanmcd 			}
25013055Sdanmcd 			if (fprintf(ofile, "replay %d ",
25024064Smarkfen 			    assoc->sadb_sa_replay) < 0) {
25034064Smarkfen 				tidyup();
25044064Smarkfen 				bail(dgettext(TEXT_DOMAIN,
25054064Smarkfen 				    "save_assoc: fprintf replay"));
25064064Smarkfen 			}
25073055Sdanmcd 			if (assoc->sadb_sa_flags & (SADB_X_SAFLAGS_NATT_LOC |
25083055Sdanmcd 			    SADB_X_SAFLAGS_NATT_REM)) {
25094064Smarkfen 				if (fprintf(ofile, "encap udp") < 0) {
25104064Smarkfen 					tidyup();
25114064Smarkfen 					bail(dgettext(TEXT_DOMAIN,
25124064Smarkfen 					    "save_assoc: fprintf encap"));
25134064Smarkfen 				}
25143055Sdanmcd 			}
25153055Sdanmcd 			savenl();
25163055Sdanmcd 			break;
25173055Sdanmcd 		case SADB_EXT_LIFETIME_HARD:
25183055Sdanmcd 		case SADB_EXT_LIFETIME_SOFT:
25194064Smarkfen 			if (!save_lifetime((struct sadb_lifetime *)ext,
25204064Smarkfen 			    ofile)) {
25214064Smarkfen 				tidyup();
25224064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_lifetime"));
25234064Smarkfen 			}
25243055Sdanmcd 			savenl();
25253055Sdanmcd 			break;
25263055Sdanmcd 		case SADB_EXT_ADDRESS_SRC:
25273055Sdanmcd 		case SADB_EXT_ADDRESS_DST:
25283055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_SRC:
25293055Sdanmcd 		case SADB_X_EXT_ADDRESS_INNER_DST:
25303055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_REM:
25313055Sdanmcd 		case SADB_X_EXT_ADDRESS_NATT_LOC:
25323055Sdanmcd 			addr = (struct sadb_address *)ext;
25333055Sdanmcd 			if (!seen_proto && addr->sadb_address_proto) {
25343055Sdanmcd 				(void) fprintf(ofile, "    proto %d",
25353055Sdanmcd 				    addr->sadb_address_proto);
25363055Sdanmcd 				savenl();
25373055Sdanmcd 				seen_proto = 1;
25383055Sdanmcd 			}
25394064Smarkfen 			if (!save_address(addr, ofile)) {
25404064Smarkfen 				tidyup();
25414064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25424064Smarkfen 			}
25433055Sdanmcd 			savenl();
25443055Sdanmcd 			break;
25453055Sdanmcd 		case SADB_EXT_KEY_AUTH:
25463055Sdanmcd 		case SADB_EXT_KEY_ENCRYPT:
25474064Smarkfen 			if (!save_key((struct sadb_key *)ext, ofile)) {
25484064Smarkfen 				tidyup();
25494064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25504064Smarkfen 			}
25513055Sdanmcd 			savenl();
25523055Sdanmcd 			break;
25533055Sdanmcd 		case SADB_EXT_IDENTITY_SRC:
25543055Sdanmcd 		case SADB_EXT_IDENTITY_DST:
25554064Smarkfen 			if (!save_ident((struct sadb_ident *)ext, ofile)) {
25564064Smarkfen 				tidyup();
25574064Smarkfen 				bail(dgettext(TEXT_DOMAIN, "save_address"));
25584064Smarkfen 			}
25593055Sdanmcd 			savenl();
25603055Sdanmcd 			break;
25613055Sdanmcd 		case SADB_EXT_SENSITIVITY:
25623055Sdanmcd 		default:
25633055Sdanmcd 			/* Skip over irrelevant extensions. */
25643055Sdanmcd 			break;
25653055Sdanmcd 		}
25663055Sdanmcd 		current += ext->sadb_ext_len;
25673055Sdanmcd 	}
25683055Sdanmcd 
25694064Smarkfen 	if (fputs(dgettext(TEXT_DOMAIN, "\n# end assoc\n\n"), ofile) == EOF) {
25704064Smarkfen 		tidyup();
25714064Smarkfen 		bail(dgettext(TEXT_DOMAIN, "save_assoc: last fputs"));
25724064Smarkfen 	}
25733055Sdanmcd }
25743055Sdanmcd 
25753055Sdanmcd /*
25763055Sdanmcd  * Open the output file for the "save" command.
25773055Sdanmcd  */
25783055Sdanmcd FILE *
25793055Sdanmcd opensavefile(char *filename)
25803055Sdanmcd {
25813055Sdanmcd 	int fd;
25823055Sdanmcd 	FILE *retval;
25833055Sdanmcd 	struct stat buf;
25843055Sdanmcd 
25853055Sdanmcd 	/*
25863055Sdanmcd 	 * If the user specifies "-" or doesn't give a filename, then
25873055Sdanmcd 	 * dump to stdout.  Make sure to document the dangers of files
25883055Sdanmcd 	 * that are NFS, directing your output to strange places, etc.
25893055Sdanmcd 	 */
25903055Sdanmcd 	if (filename == NULL || strcmp("-", filename) == 0)
25913055Sdanmcd 		return (stdout);
25923055Sdanmcd 
25933055Sdanmcd 	/*
25943055Sdanmcd 	 * open the file with the create bits set.  Since I check for
25953055Sdanmcd 	 * real UID == root in main(), I won't worry about the ownership
25963055Sdanmcd 	 * problem.
25973055Sdanmcd 	 */
25983055Sdanmcd 	fd = open(filename, O_WRONLY | O_EXCL | O_CREAT | O_TRUNC, S_IRUSR);
25993055Sdanmcd 	if (fd == -1) {
26003055Sdanmcd 		if (errno != EEXIST)
26014064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
26024064Smarkfen 			    "open error"),
26033055Sdanmcd 			    strerror(errno));
26043055Sdanmcd 		fd = open(filename, O_WRONLY | O_TRUNC, 0);
26053055Sdanmcd 		if (fd == -1)
26064064Smarkfen 			bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
26074064Smarkfen 			    "open error"), strerror(errno));
26083055Sdanmcd 		if (fstat(fd, &buf) == -1) {
26093055Sdanmcd 			(void) close(fd);
26103055Sdanmcd 			bail_msg("%s fstat: %s", filename, strerror(errno));
26113055Sdanmcd 		}
26123055Sdanmcd 		if (S_ISREG(buf.st_mode) &&
26133055Sdanmcd 		    ((buf.st_mode & S_IAMB) != S_IRUSR)) {
26144064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
26154064Smarkfen 			    "WARNING: Save file already exists with "
26164064Smarkfen 			    "permission %o."), buf.st_mode & S_IAMB);
26174064Smarkfen 			warnx(dgettext(TEXT_DOMAIN,
26184064Smarkfen 			    "Normal users may be able to read IPsec "
26194064Smarkfen 			    "keying material."));
26203055Sdanmcd 		}
26213055Sdanmcd 	}
26223055Sdanmcd 
26233055Sdanmcd 	/* Okay, we have an FD.  Assign it to a stdio FILE pointer. */
26243055Sdanmcd 	retval = fdopen(fd, "w");
26253055Sdanmcd 	if (retval == NULL) {
26263055Sdanmcd 		(void) close(fd);
26274064Smarkfen 		bail_msg("%s %s: %s", filename, dgettext(TEXT_DOMAIN,
26284064Smarkfen 		    "fdopen error"), strerror(errno));
26293055Sdanmcd 	}
26303055Sdanmcd 	return (retval);
26313055Sdanmcd }
26323055Sdanmcd 
26333055Sdanmcd const char *
26343055Sdanmcd do_inet_ntop(const void *addr, char *cp, size_t size)
26353055Sdanmcd {
26363055Sdanmcd 	boolean_t isv4;
26373055Sdanmcd 	struct in6_addr *inaddr6 = (struct in6_addr *)addr;
26383055Sdanmcd 	struct in_addr inaddr;
26393055Sdanmcd 
26403055Sdanmcd 	if ((isv4 = IN6_IS_ADDR_V4MAPPED(inaddr6)) == B_TRUE) {
26413055Sdanmcd 		IN6_V4MAPPED_TO_INADDR(inaddr6, &inaddr);
26423055Sdanmcd 	}
26433055Sdanmcd 
26443055Sdanmcd 	return (inet_ntop(isv4 ? AF_INET : AF_INET6,
26453055Sdanmcd 	    isv4 ? (void *)&inaddr : inaddr6, cp, size));
26463055Sdanmcd }
26473055Sdanmcd 
26483055Sdanmcd char numprint[NBUF_SIZE];
26493055Sdanmcd 
26503055Sdanmcd /*
26513055Sdanmcd  * Parse and reverse parse a specific SA type (AH, ESP, etc.).
26523055Sdanmcd  */
26533055Sdanmcd static struct typetable {
26543055Sdanmcd 	char *type;
26553055Sdanmcd 	int token;
26563055Sdanmcd } type_table[] = {
26573055Sdanmcd 	{"all", SADB_SATYPE_UNSPEC},
26583055Sdanmcd 	{"ah",  SADB_SATYPE_AH},
26593055Sdanmcd 	{"esp", SADB_SATYPE_ESP},
26603055Sdanmcd 	/* PF_KEY NOTE:  More to come if net/pfkeyv2.h gets updated. */
26613055Sdanmcd 	{NULL, 0}	/* Token value is irrelevant for this entry. */
26623055Sdanmcd };
26633055Sdanmcd 
26643055Sdanmcd char *
26653055Sdanmcd rparsesatype(int type)
26663055Sdanmcd {
26673055Sdanmcd 	struct typetable *tt = type_table;
26683055Sdanmcd 
26693055Sdanmcd 	while (tt->type != NULL && type != tt->token)
26703055Sdanmcd 		tt++;
26713055Sdanmcd 
26723055Sdanmcd 	if (tt->type == NULL) {
26733055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", type);
26743055Sdanmcd 	} else {
26753055Sdanmcd 		return (tt->type);
26763055Sdanmcd 	}
26773055Sdanmcd 
26783055Sdanmcd 	return (numprint);
26793055Sdanmcd }
26803055Sdanmcd 
26813055Sdanmcd 
26823055Sdanmcd /*
26833055Sdanmcd  * Return a string containing the name of the specified numerical algorithm
26843055Sdanmcd  * identifier.
26853055Sdanmcd  */
26863055Sdanmcd char *
26873055Sdanmcd rparsealg(uint8_t alg, int proto_num)
26883055Sdanmcd {
26893055Sdanmcd 	static struct ipsecalgent *holder = NULL; /* we're single-threaded */
26903055Sdanmcd 
26913055Sdanmcd 	if (holder != NULL)
26923055Sdanmcd 		freeipsecalgent(holder);
26933055Sdanmcd 
26943055Sdanmcd 	holder = getipsecalgbynum(alg, proto_num, NULL);
26953055Sdanmcd 	if (holder == NULL) {
26963055Sdanmcd 		(void) snprintf(numprint, NBUF_SIZE, "%d", alg);
26973055Sdanmcd 		return (numprint);
26983055Sdanmcd 	}
26993055Sdanmcd 
27003055Sdanmcd 	return (*(holder->a_names));
27013055Sdanmcd }
27023055Sdanmcd 
27033055Sdanmcd /*
27043055Sdanmcd  * Parse and reverse parse out a source/destination ID type.
27053055Sdanmcd  */
27063055Sdanmcd static struct idtypes {
27073055Sdanmcd 	char *idtype;
27083055Sdanmcd 	uint8_t retval;
27093055Sdanmcd } idtypes[] = {
27103055Sdanmcd 	{"prefix",	SADB_IDENTTYPE_PREFIX},
27113055Sdanmcd 	{"fqdn",	SADB_IDENTTYPE_FQDN},
27123055Sdanmcd 	{"domain",	SADB_IDENTTYPE_FQDN},
27133055Sdanmcd 	{"domainname",	SADB_IDENTTYPE_FQDN},
27143055Sdanmcd 	{"user_fqdn",	SADB_IDENTTYPE_USER_FQDN},
27153055Sdanmcd 	{"mailbox",	SADB_IDENTTYPE_USER_FQDN},
27163055Sdanmcd 	{"der_dn",	SADB_X_IDENTTYPE_DN},
27173055Sdanmcd 	{"der_gn",	SADB_X_IDENTTYPE_GN},
27183055Sdanmcd 	{NULL,		0}
27193055Sdanmcd };
27203055Sdanmcd 
27213055Sdanmcd char *
27223055Sdanmcd rparseidtype(uint16_t type)
27233055Sdanmcd {
27243055Sdanmcd 	struct idtypes *idp;
27253055Sdanmcd 
27263055Sdanmcd 	for (idp = idtypes; idp->idtype != NULL; idp++) {
27273055Sdanmcd 		if (type == idp->retval)
27283055Sdanmcd 			return (idp->idtype);
27293055Sdanmcd 	}
27303055Sdanmcd 
27313055Sdanmcd 	(void) snprintf(numprint, NBUF_SIZE, "%d", type);
27323055Sdanmcd 	return (numprint);
27333055Sdanmcd }
27344235Smarkfen 
27354235Smarkfen /*
27364235Smarkfen  * This is a general purpose exit function, calling functions can specify an
27374235Smarkfen  * error type. If the command calling this function was started by smf(5) the
27384235Smarkfen  * error type could be used as a hint to the restarter. In the future this
27394235Smarkfen  * function could be used to do something more intelligent with a process that
27404235Smarkfen  * encounters an error.
27414235Smarkfen  *
27424235Smarkfen  * The function will handle an optional variable args error message, this
27434235Smarkfen  * will be written to the error stream, typically a log file or stderr.
27444235Smarkfen  */
27454235Smarkfen void
27464235Smarkfen ipsecutil_exit(exit_type_t type, char *fmri, FILE *fp, const char *fmt, ...)
27474235Smarkfen {
27484235Smarkfen 	int exit_status;
27494235Smarkfen 	va_list args;
27504235Smarkfen 
27514235Smarkfen 	if (fp == NULL)
27524235Smarkfen 		fp = stderr;
27534235Smarkfen 	if (fmt != NULL) {
27544235Smarkfen 		va_start(args, fmt);
27554235Smarkfen 		vwarnxfp(fp, fmt, args);
27564235Smarkfen 		va_end(args);
27574235Smarkfen 	}
27584235Smarkfen 
27594235Smarkfen 	if (fmri == NULL) {
27604235Smarkfen 		/* Command being run directly from a shell. */
27614235Smarkfen 		switch (type) {
27624235Smarkfen 		case SERVICE_EXIT_OK:
27634235Smarkfen 			exit_status = 0;
27644235Smarkfen 			break;
27654235Smarkfen 		case SERVICE_DEGRADE:
27664235Smarkfen 			return;
27674235Smarkfen 			break;
27684235Smarkfen 		case SERVICE_BADPERM:
27694235Smarkfen 		case SERVICE_BADCONF:
27704235Smarkfen 		case SERVICE_MAINTAIN:
27714235Smarkfen 		case SERVICE_DISABLE:
27724235Smarkfen 		case SERVICE_FATAL:
27734235Smarkfen 		case SERVICE_RESTART:
27744235Smarkfen 			warnxfp(fp, "Fatal error - exiting.");
27754235Smarkfen 			exit_status = 1;
27764235Smarkfen 			break;
27774235Smarkfen 		}
27784235Smarkfen 	} else {
27794235Smarkfen 		/* Command being run as a smf(5) method. */
27804235Smarkfen 		switch (type) {
27814235Smarkfen 		case SERVICE_EXIT_OK:
27824235Smarkfen 			exit_status = SMF_EXIT_OK;
27834235Smarkfen 			break;
27844235Smarkfen 		case SERVICE_DEGRADE:
27854235Smarkfen 			return;
27864235Smarkfen 			break;
27874235Smarkfen 		case SERVICE_BADPERM:
27884235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
27894235Smarkfen 			    "Permission error with %s."), fmri);
27904235Smarkfen 			exit_status = SMF_EXIT_ERR_PERM;
27914235Smarkfen 			break;
27924235Smarkfen 		case SERVICE_BADCONF:
27934235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
27944235Smarkfen 			    "Bad configuration of service %s."), fmri);
27954235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
27964235Smarkfen 			break;
27974235Smarkfen 		case SERVICE_MAINTAIN:
27984235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
27994235Smarkfen 			    "Service %s needs maintenance."), fmri);
28004235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
28014235Smarkfen 			break;
28024235Smarkfen 		case SERVICE_DISABLE:
28034235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
28044235Smarkfen 			break;
28054235Smarkfen 		case SERVICE_FATAL:
28064235Smarkfen 			warnxfp(fp, dgettext(TEXT_DOMAIN,
28074235Smarkfen 			    "Service %s fatal error."), fmri);
28084235Smarkfen 			exit_status = SMF_EXIT_ERR_FATAL;
28094235Smarkfen 			break;
28104235Smarkfen 		case SERVICE_RESTART:
28114235Smarkfen 			exit_status = 1;
28124235Smarkfen 			break;
28134235Smarkfen 		}
28144235Smarkfen 	}
28154235Smarkfen 	(void) fflush(fp);
28164235Smarkfen 	(void) fclose(fp);
28174235Smarkfen 	exit(exit_status);
28184235Smarkfen }
2819